-
Notifications
You must be signed in to change notification settings - Fork 1
/
old-mc-rpa.cpp
130 lines (110 loc) · 4.62 KB
/
old-mc-rpa.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
#include "rpa.hpp"
#include <cstring>
#include <chrono>
#include <cstdlib>
double t=1;
double U_prime=2;
int L=4;
MatrixXd sigma;
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 H_spa = H0 - U_prime/2*matrixelement_sigmaz(sigma);
pair<MatrixXcd,VectorXd> spa_spectrum = Eigenspectrum(H_spa);
double free_energy = rpa_free_energy(spa_spectrum.second, spa_spectrum.first, final_temp);
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="data/m_length_tda_"+ current_time_str()+latticedata+".txt"; ofstream outfile_mlength(filename);
filename="data/rpa_results_"+current_time_str()+latticedata+".txt"; ofstream outfile_freeenergy(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=10; i>=2; 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);
pair<MatrixXcd,VectorXd> suggested_spa_spectrum = Eigenspectrum(suggested_Hspa);
double suggested_free_energy = rpa_free_energy(suggested_spa_spectrum.second, suggested_spa_spectrum.first, temperature);
double move_prob = exp(-(suggested_free_energy-free_energy)/temperature);
double uniform_rv = ran0(&idum);
if(uniform_rv <= move_prob)
{
sigma = suggested_sigma;
free_energy = suggested_free_energy;
}
else
{
suggested_sigma=sigma;
}
}
cout << "\r sweep = " << sweep << " done."; cout.flush();
}
double final_free_energy = 0.0;
double S_pi = 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);
pair<MatrixXcd,VectorXd> suggested_spa_spectrum = Eigenspectrum(suggested_Hspa);
double suggested_free_energy = rpa_free_energy(suggested_spa_spectrum.second, suggested_spa_spectrum.first, temperature);
double move_prob = exp(-(suggested_free_energy-free_energy)/temperature);
double uniform_rv = ran0(&idum);
if(uniform_rv <= move_prob)
{
sigma = suggested_sigma;
free_energy = suggested_free_energy;
}
else
{
suggested_sigma=sigma;
}
}
final_free_energy += free_energy;
S_pi += get_spi(sigma);
cout << "\r sweep = " << sweep << " done."; cout.flush();
}
outfile_mlength << temperature << " " << sigma.col(2).transpose() << endl;
outfile_freeenergy << temperature << " " << final_free_energy/double(N_meas) << " " << S_pi/double(N_meas) << endl;
// cout << "\rtemperature = " << temperature << " done."; cout.flush();
}
}
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_freeenergy.close();
return 0;
}