-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP5lib.cpp
More file actions
381 lines (328 loc) · 13.4 KB
/
P5lib.cpp
File metadata and controls
381 lines (328 loc) · 13.4 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*Function library*/
#include <cmath>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <random>
using namespace std;
#include "P5lib.h"
/*--------------------------------
| Main-Algorithm functions |
/*------------------------------*/
void MetropolisSampling(int N_P, int D, int MCcycles, double* &StoreValues, double alpha, double beta, double omega, double h_R,
double (*trialFunction)(int, int, double** &, double, double, double),
double* (*localEnergy) (int, int, double** &, double, double, double, double, double,
double (*trialFunction)(int, int, double** &, double, double, double)),
double (*localEnergyAnalytic)(int, int, double** &, double, double, double) )
{
//Prepare mersenne twister
std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_real_distribution<double> positionDistribution(-1.0, 1.0); // When assigning initial positions
std::uniform_real_distribution<double> uniformDistribution(0, 1); // When performing metropolis test
std::uniform_real_distribution<double> moveDistribution(-0.5, 0.5); // When performing translation of particles
// Allocate memory for old and new positions matrices
double** r_curr = MatrixAlloc(N_P, D);
double** r_new = MatrixAlloc(N_P, D);
double psi_new, psi_curr; // To keep tabs of wavefunction evaluation
for(int i = 0; i < N_P; i++)
{
for(int j = 0; j < D; j++) r_new[i][j] = r_curr[i][j] = 0;
}
// initialize quantities
double* E_Local_arr = new double[2]; // Array for unloading kinetic and potential energy
double T_Energy = 0; double U_Energy = 0; // For expectation value of kinetic and potential energy
double Energy = 0; double Energy2 = 0; // For expectation value of total energy and total energy squared
double E_analytical = 0; double Delta_E = 0; // For analytical local energy and temporary storage of energy
double Delta_E_analytical = 0; double Dist_r1r2 = 0; // For temporary storage of analytical total energy, and distance between particles
double Probability_ratio = 0;
int Accepted = 0; //To store number of accepted transitions
// Set initial positions of particles randomly within interval [-a,a], a real number.
for (int i = 0; i < N_P; i++)
{
for (int j = 0; j < D; j++) r_curr[i][j] = positionDistribution(gen);
}
psi_curr = trialFunction(N_P, D, r_curr, alpha, beta, omega);
// Begin Monte Carlo cycle
for (int cycle = 1; cycle <= MCcycles; cycle++)
{
// Loop over particles and dimensions
for(int particle = 0; particle < N_P; particle++)
{
for(int dimension = 0; dimension < D; dimension++)
{
// Translate particles randomly
r_new[particle][dimension] = r_curr[particle][dimension] + moveDistribution(gen) * h_R;
}
}
// Evaluate the trial wavefunction at the new positions
psi_new = trialFunction(N_P, D, r_new, alpha, beta, omega);
Probability_ratio = (psi_new*psi_new)/(psi_curr*psi_curr);
// Metropolis test of probability ratio
if( Probability_ratio >= uniformDistribution(gen) )
// Accept
{
// update quantities
for(int i = 0; i < N_P; i++)
{
for(int j = 0; j < D; j++) r_curr[i][j] = r_new[i][j];
}
psi_curr = psi_new;
// Compute local energy and store kinetic energy as first element and potential as second
E_Local_arr = localEnergy(N_P, D, r_curr, psi_curr, h_R, alpha, beta, omega, trialFunction);
// Store local energy in Delta_E
Delta_E = E_Local_arr[0] + E_Local_arr[1];
// Compute analytical local energy
Delta_E_analytical = localEnergyAnalytic(N_P, D, r_curr, alpha, beta, omega);
T_Energy += E_Local_arr[0]; U_Energy += E_Local_arr[1];
Energy += Delta_E; Energy2 += Delta_E*Delta_E;
E_analytical += Delta_E_analytical;
Accepted += 1;
Dist_r1r2 += sqrt( r_12_squared(N_P, D, r_curr) );
}
else
// Keep position, but add contributions to update
{
// Compute local energy and store kinetic energy as first element and potential as second
E_Local_arr = localEnergy(N_P, D, r_curr, psi_curr, h_R, alpha, beta, omega, trialFunction);
// Store local energy in Delta_E
Delta_E = E_Local_arr[0] + E_Local_arr[1];
// Compute analytical local energy
Delta_E_analytical = localEnergyAnalytic(N_P, D, r_curr, alpha, beta, omega);
T_Energy += E_Local_arr[0]; U_Energy += E_Local_arr[1];
Energy += Delta_E; Energy2 += Delta_E*Delta_E;
E_analytical += Delta_E_analytical;
Dist_r1r2 += sqrt( r_12_squared(N_P, D, r_curr) ); }
}
// update expectation values for local node
StoreValues[0] = T_Energy; StoreValues[1] = U_Energy;
StoreValues[2] = Energy; StoreValues[3] = Energy2;
StoreValues[4] = E_analytical; StoreValues[5] = Accepted;
StoreValues[6] = Dist_r1r2;
//Deallocate memory
MatrixDeAlloc(r_curr, N_P);
MatrixDeAlloc(r_new, N_P);
} // end of Metropolis sampling function
double LocateOptimal_h_R(int N_P, int D, double alpha, double beta, double omega,
double (*trialFunction)(int, int, double** &, double, double, double) )
{
//Prepare mersenne twister
std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_real_distribution<double> positionDistribution(-1.0, 1.0);
std::uniform_real_distribution<double> uniformDistribution(0, 1);
std::uniform_real_distribution<double> moveDistribution(-0.5, 0.5);
double h_R = 15;
int MC = 1000;
double epsilon = 0.05; //Five percent
int Accepted = 0; //To store number of accepted transitions
double AcceptRatio = Accepted/( (double) MC );
double Probability_ratio = 0;
double psi_new, psi_curr;
bool condition = (0.5 - epsilon) < AcceptRatio && AcceptRatio < (0.5 + epsilon);
int runNumber = 0;
while( condition != 1 && h_R > 0 )
{
// Allocate memory for old and new positions matrices
double** r_curr = MatrixAlloc(N_P, D);
double** r_new = MatrixAlloc(N_P, D);
for(int i = 0; i < N_P; i++)
{
for(int j = 0; j < D; j++) r_new[i][j] = r_curr[i][j] = 0;
}
// Set initial positions of particles randomly within [-2,2]
for (int i = 0; i < N_P; i++)
{
for (int j = 0; j < D; j++) r_curr[i][j] = positionDistribution(gen);
}
psi_curr = trialFunction(N_P, D, r_curr, alpha, beta, omega);
// Begin Monte Carlo cycle
for (int cycle = 1; cycle <= MC; cycle++)
{
// Loop over particles and dimensions
for(int particle = 0; particle < N_P; particle++)
{
for(int dimension = 0; dimension < D; dimension++)
{
// Translating particles randomly
r_new[particle][dimension] = r_curr[particle][dimension] + moveDistribution(gen) * h_R;
}
}
// Evaluate the trial wavefunction at the new positions
psi_new = trialFunction(N_P, D, r_new, alpha, beta, omega);
// Metropolis test of probability ratio
Probability_ratio = (psi_new*psi_new)/(psi_curr*psi_curr);
if(Probability_ratio >= uniformDistribution(gen))
// Accept
{
// update energies and position
for(int i = 0; i < N_P; i++)
{
for(int j = 0; j < D; j++) r_curr[i][j] = r_new[i][j];
}
Accepted += 1;
psi_curr = psi_new;
}
else
// keep position
{
}
}
MatrixDeAlloc(r_curr, N_P);
MatrixDeAlloc(r_new, N_P);
AcceptRatio = Accepted/( (double) MC );
h_R -= 0.001;
Accepted = 0;
condition = AcceptRatio > 0.5 + epsilon;
}
return h_R;
}
/*---------------------------------------------
| Energy-specific computational functions |
/*-------------------------------------------*/
double U_Harmonic_osc(int N_P, int D, double omega, double** &pos)
// harmonic oscillator potential
{
return 0.5*omega*omega*sum_r1r2_squared(N_P, D, pos);
}
double U_ee_Repulsion(int N_P, int D, double** &pos)
// harmonic oscillator potential
{
double r_12 = 0; double U_ee = 0;
for (int i = 0; i < N_P - 1; i++)
{
for (int j = i + 1; j < N_P; j++)
{
r_12 = 0;
for (int k = 0; k < D; k++)
{
r_12 += ( pos[i][k] - pos[j][k] )*( pos[i][k]-pos[j][k] );
}
U_ee += 1/sqrt(r_12);
}
}
}
double Kinetic_Local(int N_P, int D, double** &pos, double psi_curr, double h_R, double alpha, double beta, double omega,
double (*trialFunction)(int, int, double** &, double, double, double) )
{
int i, j;
double psi_minus, psi_plus, T_Local;
double hh_R = h_R*h_R;
// allocate matrices which contain the position of the particles
double** r_plus = MatrixAlloc(N_P, D);
double** r_minus = MatrixAlloc(N_P, D);
for (i = 0; i < N_P; i++)
{
for ( j = 0; j < D; j++)
{
r_plus[i][j] = r_minus[i][j] = pos[i][j];
}
}
// compute the kinetic energy
T_Local = 0;
for (i = 0; i < N_P; i++)
{
for (j = 0; j < D; j++)
{
r_plus[i][j] = pos[i][j] + h_der;
r_minus[i][j] = pos[i][j] - h_der;
psi_minus = trialFunction(N_P, D, r_minus, alpha, beta, omega);
psi_plus = trialFunction(N_P, D, r_plus, alpha, beta, omega);
T_Local = T_Local - (psi_minus + psi_plus - 2*psi_curr);
r_plus[i][j] = pos[i][j];
r_minus[i][j] = pos[i][j];
}
}
T_Local = 0.5*h2_der*T_Local/(psi_curr);
MatrixDeAlloc(r_plus, N_P); // free memory
MatrixDeAlloc(r_minus, N_P);
return T_Local;
}
double TrialWaveFunction1(int N_P, int D, double** &pos, double alpha, double beta, double omega)
{
double argument, wavefunction, single_particle;
argument = wavefunction = 0;
for(int i = 0; i < N_P; i++)
{
single_particle = 0;
for(int j = 0; j < D; j++)
{
single_particle += pos[i][j]*pos[i][j];
}
argument += single_particle;
}
wavefunction = exp(-0.5*alpha*omega*argument);
return wavefunction;
}
double TrialWaveFunction2(int N_P, int D, double** &pos, double alpha, double beta, double omega)
{
double r_12 = sqrt( r_12_squared(N_P, D, pos) );
double factor = 1.0 + beta*r_12;
double Jastrow = exp( r_12/(2*factor) );
double wf1 = TrialWaveFunction1(N_P, D, pos, alpha, beta, omega);
return wf1*Jastrow;
}
double r_12_squared(int N_P, int D, double** &pos)
// Distance between electrons squared
{
double value = 0;
for(int i = 0; i < D; i++)
{
value += ( pos[0][i] - pos[1][i] ) * ( pos[0][i] - pos[1][i] );
}
return value;
}
double sum_r1r2_squared(int N_P, int D, double** &pos)
// sum of the norms of positions^2
{
double r_sq = 0;
for(int i = 0; i < N_P; i++)
{
r_sq += (pos[i][0]*pos[i][0] + pos[i][1]*pos[i][1] + pos[i][2]*pos[i][2]);
}
return r_sq;
}
double E_Local_Analytic_T1U(int N_P, int D, double** &pos, double alpha, double beta, double omega)
{
return 0.5*omega*omega*sum_r1r2_squared(N_P, D, pos)*(1 - alpha*alpha) + 3*alpha*omega;
}
double E_Local_Analytic_T1P(int N_P, int D, double** &pos, double alpha, double beta, double omega)
{
double r_12 = sqrt(r_12_squared(N_P, D, pos));
return E_Local_Analytic_T1U(N_P, D, pos, alpha, beta, omega) + 1.0/r_12;
}
double E_Local_Analytic_T2P(int N_P, int D, double** &pos, double alpha, double beta, double omega)
{
double r_12 = sqrt( r_12_squared(N_P, D, pos) );
double arg = 1 + beta*r_12;
double arg2 = 1/(2*arg*arg);
double term = arg2 * ( alpha*omega*r_12 - arg2 -2/r_12 + (2*beta)/arg );
double ET1P = E_Local_Analytic_T1P(N_P, D, pos, alpha, beta, omega);
// cout << ET1P + term;
return ET1P + term;
}
double** MatrixAlloc(int N_P, int D)
/*Function which returns dynamically allocates memory for a square nxn matrix*/
{
double** Matrix = new double*[N_P];
for(int i = 0; i < N_P; i++)
{
Matrix[i] = new double[D];
}
return Matrix;
}
void MatrixDeAlloc(double** &Matrix, int N_P)
/*Void for deallocating dynamic matrix*/
{
for(int i = 0; i < N_P; i++) delete[] Matrix[i];
delete[] Matrix;
}
void PrintMatrix(double** &Matr, int N_P, int D)
{
for(int i = 0; i < N_P ; i++)
{
for(int j = 0; j < D; j++) cout << setw(7) << Matr[i][j] << " ";
cout << endl;
}
}