-
Notifications
You must be signed in to change notification settings - Fork 1
/
mc-spa.cpp
147 lines (126 loc) · 5.62 KB
/
mc-spa.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "rpa.hpp"
#include <cstring>
#include <chrono>
#include <cstdlib>
double t=1;
double U_prime=2;
int L=8;
MatrixXd sigma;
MatrixXcd U;
using namespace std::chrono;
void greens_sigma_generate(MatrixXd& suggested_sigma, int lattice_index, long & idum)
{
if(ran0(&idum)<=0.5) suggested_sigma(lattice_index,2) *= -1;
}
int main(int argc, char* argv[])
{
if(argc!=4) {cerr << "Enter (1) lattice size, (2) U and (3) no of sweeps.\n"; exit(1);}
L = atoi(argv[1]);
U_prime = atof(argv[2]);
int no_sweeps = atoi(argv[3]);
int N_therm = 0.5*no_sweeps;
int N_meas = no_sweeps-N_therm;
int initial_exp = -3;
int final_exp = 0;
double final_temp = 10*pow(10,final_exp);
milliseconds begin_ms, end_ms;
long idum = time(NULL);
sigma = MatrixXd::Zero(L,3);
sigma.col(2) = VectorXd::Constant(L,1);
for(int i=0; i<L; i++) greens_sigma_generate(sigma, i, idum);
MatrixXd suggested_sigma = sigma;
MatrixXcd H0 = construct_h0();
MatrixXcd Id = MatrixXcd::Identity(H0.rows(), H0.cols());
MatrixXcd H_spa = H0 - U_prime/2*matrixelement_sigmaz(sigma) + U_prime/4*sigma.unaryExpr(&Sqr).sum()*Id;
pair<MatrixXcd,VectorXd> spa_spectrum = Eigenspectrum(H_spa);
double free_energy_spa = spa_free_energy(spa_spectrum.second, final_temp);
// cout << spa_spectrum.second.transpose() << endl << free_energy_spa << endl; exit(1);
string filename, latticedata;
latticedata = "_U="+to_string(int(U_prime))+"_size="+to_string(L)+"_sweeps="+to_string(no_sweeps);
// filename="data/spin_arrangement"+current_time_str()+latticedata+".nb"; ofstream outfile_spinarr(filename);
// spinarrangement_Mathematica_output(sigma,outfile_spinarr);
filename="spa/m_length_spa_"+ current_time_str()+latticedata+".dat"; ofstream outfile_mlength;//(filename);
filename="spa/spa_results_"+current_time_str()+latticedata+".dat"; ofstream outfile_results(filename);
// filename="data/mcdetails"+current_time_str()+latticedata+".txt"; ofstream outfile_mcdetails(filename);
cout << "==============================\n"<< "filename is: " << filename << "\n========================\n";
for(int j=final_exp; j>=initial_exp; j--)
{
for(double i=9; i>=1; i-=1)
{
double temperature = i*pow(10,j);
for(int sweep=0; sweep<N_therm; sweep++)
{
for(int lattice_index=0; lattice_index<L; lattice_index++)
{
greens_sigma_generate(suggested_sigma,lattice_index, idum);
MatrixXcd suggested_Hspa = H0-U_prime/2*matrixelement_sigmaz(suggested_sigma)+U_prime/4*sigma.unaryExpr(&Sqr).sum()*Id;
pair<MatrixXcd,VectorXd> suggested_spa_spectrum = Eigenspectrum(suggested_Hspa);
double suggested_free_energy_spa = spa_free_energy(suggested_spa_spectrum.second,temperature);
double uniform_rv = ran0(&idum); double move_prob = exp((free_energy_spa - suggested_free_energy_spa)/temperature);
if(uniform_rv <= move_prob)
{
free_energy_spa = suggested_free_energy_spa;
sigma = suggested_sigma;
}
else
{
suggested_sigma=sigma;
}
}
cout << "\r sweep = " << sweep << " done."; cout.flush();
}
double final_free_energy_spa = 0.0;
double magnetisation = 0.0;
double S_pi = 0.0;
double internal_energy = 0.0;
for(int sweep= N_therm; sweep<no_sweeps; sweep++)
{
for(int lattice_index=0; lattice_index<L; lattice_index++)
{
greens_sigma_generate(suggested_sigma,lattice_index, idum);
MatrixXcd suggested_Hspa = H0-U_prime/2*matrixelement_sigmaz(suggested_sigma)+U_prime/4*sigma.unaryExpr(&Sqr).sum()*Id;
pair<MatrixXcd,VectorXd> suggested_spa_spectrum = Eigenspectrum(suggested_Hspa);
double suggested_free_energy_spa = spa_free_energy(suggested_spa_spectrum.second,temperature);
double uniform_rv = ran0(&idum); double move_prob = exp((free_energy_spa - suggested_free_energy_spa)/temperature);
if(uniform_rv <= move_prob)
{
free_energy_spa = suggested_free_energy_spa;
sigma = suggested_sigma;
}
else
{
suggested_sigma=sigma;
}
}
MatrixXcd H_spa_afterSweep = H0-U_prime/2*matrixelement_sigmaz(suggested_sigma) + U_prime/4*sigma.unaryExpr(&Sqr).sum()*Id;
internal_energy += spa_internal_energy(H_spa_afterSweep,temperature);
final_free_energy_spa += free_energy_spa;
double sq = 0.0;
for(int i=0; i<L; i++)
{
for(int j=0; j<L; j++)
{
sq += sigma(i,2)*sigma(j,2)*pow(-1,i-j);
}
}
S_pi += sq/pow(L,2);
cout << "\r sweep = " << sweep << " done."; cout.flush();
}
outfile_mlength << temperature << " " << sigma.col(2).transpose() << " " << Eigenvalues(H0-U_prime/2*matrixelement_sigmaz(suggested_sigma)).transpose() << endl;
outfile_results << temperature << " " << final_free_energy_spa/N_meas << " " << internal_energy/N_meas << " " << S_pi/N_meas << endl;
cout << "\rtemperature = " << temperature << " done."; cout.flush();
}
}
ofstream fout("final_eivals.dat");
MatrixXcd final_Hspa = H0-U_prime/2*matrixelement_sigmaz(sigma)+U_prime/4*sigma.unaryExpr(&Sqr).sum()*Id;
fout << Eigenvalues(final_Hspa) << endl;
cout << endl;
end_ms = duration_cast< milliseconds >(system_clock::now().time_since_epoch());
// show_time(begin_ms, end_ms,"MC calculation");
// spinarrangement_Mathematica_output(sigma,outfile_spinarr);
// outfile_spinarr.close();
// outfile_mcdetails.close();
outfile_mlength.close();
outfile_results.close();
return 0;
}