-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathGaussian_HMC_multi_variables.cpp
187 lines (166 loc) · 4.92 KB
/
Gaussian_HMC_multi_variables.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <iostream>
#include <cmath>
#include<fstream>
const int niter=100;
const int ntau=20;
const double dtau=0.5e0;
const int ndim=3; //number of variables
/******************************************************************/
/*** Gaussian Random Number Generator with Box Muller Algorithm ***/
/******************************************************************/
int BoxMuller(double& p, double& q){
double pi=2e0*asin(1e0);
//uniform random numbers between 0 and 1
double r = (double)rand()/RAND_MAX;
double s = (double)rand()/RAND_MAX;
//Gaussian random numbers,
//with weights proportional to e^{-p^2/2} and e^{-q^2/2}
p=sqrt(-2e0*log(r))*sin(2e0*pi*s);
q=sqrt(-2e0*log(r))*cos(2e0*pi*s);
return 0;
}
/*********************************/
/*** Calculation of the action ***/
/*********************************/
// When you change the action, you should also change dH/dx,
// specified in "calc_delh".
double calc_action(const double x[ndim],const double A[ndim][ndim]){
double action=0e0;
for(int idim=0; idim!=ndim; idim++){
for(int jdim=0; jdim!=idim; jdim++){
action=action+x[idim]*A[idim][jdim]*x[jdim];
}
action=action+0.5e0*x[idim]*A[idim][idim]*x[idim];
}
return action;
}
/**************************************/
/*** Calculation of the Hamiltonian ***/
/**************************************/
double calc_hamiltonian(const double x[ndim], const double p[ndim],
const double A[ndim][ndim]){
double ham=calc_action(x,A);
for(int idim=0; idim!=ndim; idim++){
ham=ham+0.5e0*p[idim]*p[idim];
}
return ham;
}
/****************************/
/*** Calculation of dH/dx ***/
/****************************/
// Derivative of the Hamiltonian with respect to x,
// which is equivalent to the derivative of the action.
// When you change "calc_action", you have to change this part as well.
int calc_delh(const double x[ndim],const double A[ndim][ndim],
double (&delh)[ndim]){
for(int idim=0; idim!=ndim; idim++){
delh[idim]=0e0;
}
for(int idim=0; idim!=ndim; idim++){
for(int jdim=0; jdim!=ndim; jdim++){
delh[idim]=delh[idim]+A[idim][jdim]*x[jdim];
}
}
return 0;
}
/***************************/
/*** Molecular evolution ***/
/***************************/
int Molecular_Dynamics(double (&x)[ndim],const double A[ndim][ndim],
double& ham_init,double& ham_fin){
double p[ndim];
double delh[ndim];
double r1,r2;
for(int idim=0; idim!=ndim; idim++){
BoxMuller(r1,r2);
p[idim]=r1;
}
//*** calculate Hamiltonian ***
ham_init=calc_hamiltonian(x,p,A);
//*** first step of leap frog ***
for(int idim=0; idim!=ndim; idim++){
x[idim]=x[idim]+p[idim]*0.5e0*dtau;
}
//*** 2nd, ..., Ntau-th steps ***
for(int step=1; step!=ntau; step++){
calc_delh(x,A,delh);
for(int idim=0; idim!=ndim; idim++){
p[idim]=p[idim]-delh[idim]*dtau;
}
for(int idim=0; idim!=ndim; idim++){
x[idim]=x[idim]+p[idim]*dtau;
}
}
//*** last step of leap frog ***
calc_delh(x,A,delh);
for(int idim=0; idim!=ndim; idim++){
p[idim]=p[idim]-delh[idim]*dtau;
}
for(int idim=0; idim!=ndim; idim++){
x[idim]=x[idim]+p[idim]*0.5e0*dtau;
}
//*** calculate Hamiltonian again ***
ham_fin=calc_hamiltonian(x,p,A);
return 0;
}
int main()
{
double x[ndim];
double A[ndim][ndim];
A[0][0]=1e0;A[1][1]=2e0;A[2][2]=2e0;
A[0][1]=1e0;A[0][2]=1e0;A[1][2]=1e0;
for(int idim=1; idim!=ndim; idim++){
for(int jdim=0; jdim!=idim; jdim++){
A[idim][jdim]=A[jdim][idim];
}
}
srand((unsigned)time(NULL));
/*********************************/
/* Set the initial configuration */
/*********************************/
for(int idim=0; idim!=ndim; idim++){
x[idim]=0e0;
}
/*****************/
/*** Main part ***/
/*****************/
std::ofstream outputfile("output.txt");
int naccept=0;//counter for the number of acceptance
for(int iter=0; iter!=niter; iter++){
double backup_x[ndim];
for(int idim=0; idim!=ndim; idim++){
backup_x[idim]=x[idim];
}
double ham_init,ham_fin;
Molecular_Dynamics(x,A,ham_init,ham_fin);
double metropolis = (double)rand()/RAND_MAX;
if(exp(ham_init-ham_fin) > metropolis){
//accept
naccept=naccept+1;
}else{
//reject
for(int idim=0; idim!=ndim; idim++){
x[idim]=backup_x[idim];
}
}
/*******************/
/*** data output ***/
/*******************/
if((iter+1)%10 == 0){
std::cout << std::fixed << std::setprecision(6)
<< x[0] << " "
<< x[1] << " "
<< x[2] << " "
<< ((double)naccept)/((double)iter+1)
<< std::endl;
outputfile << std::fixed << std::setprecision(6)
<< x[0] << " "
<< x[1] << " "
<< x[2] << " "
<< ((double)naccept)/((double)iter+1)
<< std::endl;
}
}
outputfile.close();
return 0;
}