/
trialfct.cpp
251 lines (189 loc) · 6.39 KB
/
trialfct.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
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
#include "trialfct.h"
// default constructor
TrialFct::TrialFct()
{
stepwidth = 0.001;
stepwidthsqr = stepwidth*stepwidth;
}
// constructor taking parameters from the config file
TrialFct::TrialFct(Config * parameters) : function(parameters)
{
stepwidth = parameters->lookup("stepwidth");
stepwidthsqr = stepwidth*stepwidth;
}
// calculate the value of the trial function the position given in the variable position.
// Uses hydrogen-like wave functions.
double TrialFct::getValue(positions * R)
{
double result = 0.0;
// create Slater matrix
mat Slaterup = zeros(nParticleshalf,nParticleshalf);
mat Slaterdown = zeros(nParticleshalf,nParticleshalf);
for (int i=0;i<nParticleshalf;i++)
{
for (int j=0;j<nParticleshalf;j++)
{
Slaterup(i,j) = hydrogen(i,j,R);
Slaterdown(i,j) = hydrogen(i+nParticleshalf,j,R);
}
}
result = det(Slaterup)*det(Slaterdown);
// jastrow factor
double jastrow = 0.0;
double dist;
for (int i=0;i<nParticleshalf;i++)
{ // equal spin, factor 1/2
for (int j=0;j<i;j++)
{
dist = R->get_rr(i-1,j);
jastrow += dist/(2+2*funcParameters[1]*dist);
dist = R->get_rr(nParticleshalf+i-1,nParticleshalf+j);
jastrow += dist/(2+2*funcParameters[1]*dist);
}
// opposite spin, factor 1/4
for (int j=nParticleshalf;j<nParticles;j++)
{
dist = R->get_rr(j-1,i);
jastrow += dist/(4+4*funcParameters[1]*dist);
}
}
result *= exp(jastrow);
return result;
}
// calculate the sum of the numerical second derivatives acting on the trail function ( \nabla^2_i f(x_1,...x_i,...x_n) ),
// derivatives act on the postion of particle n according to the given argument
// uses the position given in the privat variable position.
// using the numerical derivative: f''(x) = 1/h^2 * (f(x+h) + f(x-h) -2*f(x))
double TrialFct::getDivGradOverFct(int particleNumber, positions * R)
{
double currentValue = getValue(R);
double value =-2*ndim*currentValue;
for (int i = 0; i<ndim; i++)
{
// move forward: r+h*e_i
R->step(stepwidth,i,particleNumber);
value += getValue(R);
// move backwards: r-h*e_i
R->step(-2*stepwidth,i,particleNumber);
value += getValue(R);
// move to middle
R->step(stepwidth,i,particleNumber);
}
value /=stepwidthsqr*currentValue;
return value;
}
// calculates numerically the quantum force, defined by 2/(f) * grad(f), where grad(f) is the gradient of f.
// particle number refers to the particle, on whichs position the derivatives act.
vec TrialFct::quantumForce(int particleNumber, positions *R)
{
vec gradient = zeros(ndim);
for (int i =0; i<ndim; i++)
{
R->step(stepwidth,i,particleNumber);
gradient(i)+=getValue(R);
R->step(-2*stepwidth,i,particleNumber);
gradient(i)-=getValue(R);
R->step(stepwidth,i,particleNumber);
}
gradient/=stepwidth*getValue(R);
return gradient;
}
//=============================================================================================
// calculate the ratio brut-force
double TrialFct::SlaterRatio(int particleNumber ,positions * Rold,positions * Rnew)
{
mat Slaternew = zeros(nParticleshalf,nParticleshalf);
mat Slaterold = zeros(nParticleshalf,nParticleshalf);
if (particleNumber<nParticleshalf)
{
for (int i=0;i<nParticleshalf;i++)
{
for (int j=0;j<nParticleshalf;j++)
{
Slaternew(i,j) = hydrogen(i,j,Rnew);
Slaterold(i,j) = hydrogen(i,j,Rold);
}
}
}
else
{
for (int i=0;i<nParticleshalf;i++)
{
for (int j=0;j<nParticleshalf;j++)
{
Slaternew(i,j) = hydrogen(i+nParticleshalf,j,Rnew);
Slaterold(i,j) = hydrogen(i+nParticleshalf,j,Rold);
}
}
}
return det(Slaternew)/det(Slaterold);
}
void TrialFct::setSlaterinv(positions * R)
{
// create Slater matrix
mat Slaterup = zeros(nParticleshalf,nParticleshalf);
mat Slaterdown = zeros(nParticleshalf,nParticleshalf);
for (int i=0;i<nParticleshalf;i++)
{
for (int j=0;j<nParticleshalf;j++)
{
Slaterup(i,j) = hydrogen(i, j,R);
Slaterdown(i,j) = hydrogen(i+nParticleshalf,j,R);
}
}
// calculate the inverse of the Slatermatrix for spin up and down
// respectively. This is used for later updates of the position.
inverseSlaterDown = inv(Slaterdown);
inverseSlaterUp = inv(Slaterup);
}
// Ratio of Jastrow factors, also Brut-Force.
double TrialFct::JastrowRatio(int particleNumber, positions * Rold, positions * Rnew)
{
// jastrow factor new
double jastrow_new = 0.0;
double dist;
for (int i=0;i<nParticleshalf;i++)
{ // equal spin, factor 1/2
for (int j=0;j<i;j++)
{
dist = Rnew->get_rr(i-1,j);
jastrow_new += dist/(2+2*funcParameters[1]*dist);
dist = Rnew->get_rr(nParticleshalf+i-1,nParticleshalf+j);
jastrow_new += dist/(2+2*funcParameters[1]*dist);
}
// opposite spin, factor 1/4
for (int j=nParticleshalf;j<nParticles;j++)
{
dist = Rnew->get_rr(j-1,i);
jastrow_new += dist/(4+4*funcParameters[1]*dist);
}
}
// jastrow factor old
double jastrow_old = 0.0;
for (int i=0;i<nParticleshalf;i++)
{ // equal spin, factor 1/2
for (int j=0;j<i;j++)
{
dist = Rold->get_rr(i-1,j);
jastrow_old += dist/(2+2*funcParameters[1]*dist);
dist = Rold->get_rr(nParticleshalf+i-1,nParticleshalf+j);
jastrow_old += dist/(2+2*funcParameters[1]*dist);
}
// opposite spin, factor 1/4
for (int j=nParticleshalf;j<nParticles;j++)
{
dist = Rold->get_rr(j-1,i);
jastrow_old += dist/(4+4*funcParameters[1]*dist);
}
}
return exp(jastrow_new-jastrow_old);
}
void TrialFct::updateSlaterinv(int particleNumber, positions* Rnew, double ratio)
{
// nothing to do here, since the numerical derivative doesn't use the inverse Slater matrix
}
// to be done later
double TrialFct::ParamDerivativeOverFct(positions *R,int parameterNumber)
{
return 0.0;
}