This repository was archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialconditions.cpp
More file actions
250 lines (230 loc) · 7.07 KB
/
initialconditions.cpp
File metadata and controls
250 lines (230 loc) · 7.07 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
/*
initialconditions.cpp
Copyright (c) Michael Strickland
GNU General Public License (GPLv3)
See detailed text in license directory
*/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cerrno>
#include <sys/stat.h>
#include <sys/time.h>
#include <string>
#include <vector>
#include <sstream>
#include <complex>
using namespace std;
#include "mpisolve.h"
#include "grid.h"
#include "initialconditions.h"
#include "random.h"
// initializes the variables
void setInitialConditions(int seedMult)
{
double sig=SIG; // standard deviation
int sx,sy,sz,tx,ty,tz;
double dx,dy,dz,r,costheta,cosphi,md,temp,temp2;
fstream input;
char fname[32];
string line;
vector<string> lines;
int inputLatticeSize,inputLatticeSizeX,stridein=1,strideout=1,linenumber;
//cout << "==> Initializing variables\n";
srand(((unsigned int)time(0))*seedMult);
//srand(19691123);
switch (INITCONDTYPE) {
case 0:
// read from file
sprintf(fname,"data/wavefunction_0_%d.dat",nodeID);
input.open(fname, ios::in);
if (nodeID==1) cout << "==> Initial wavefunction : From file" << endl;
if (!input) {
cout << "==> Error : Unable to open wavefunction file " << fname << ". Using random Gaussian instead." << endl;
for (sx=0;sx<NUMX+2;sx++)
for (sy=0;sy<NUM+2;sy++)
for (sz=0; sz<NUM+2;sz++)
w[sx][sy][sz] = randGauss(sig);
}
while( getline( input, line ) ) lines.push_back( line ) ;
inputLatticeSize = round(pow((numNodes-1)*lines.size(),1/3.));
inputLatticeSizeX = inputLatticeSize/(numNodes-1);
if (NUMX > inputLatticeSizeX) strideout = NUMX/inputLatticeSizeX;
if (NUMX < inputLatticeSizeX) stridein = inputLatticeSizeX/NUMX;
for (sx=1;sx<=NUMX;sx++)
for (sy=1;sy<=NUM;sy++)
for (sz=1; sz<=NUM;sz++) {
if (debug && nodeID==1) cout << "Mark : " << sx << ", " << sy << ", " << sz << endl;
if (strideout==1 && strideout==1) {
linenumber = (sx-1)*inputLatticeSize*inputLatticeSize + (sy-1)*inputLatticeSize + (sz-1);
}
if (strideout>1) {
// If input wavefunction has lower resolution, spread it out
tx = ceil(sx/((double)strideout));
ty = ceil(sy/((double)strideout));
tz = ceil(sz/((double)strideout));
linenumber = (tx-1)*inputLatticeSize*inputLatticeSize + (ty-1)*inputLatticeSize + (tz-1);
if (debug && nodeID==1) cout << "Respond : " << tx << ", " << ty << ", " << tz << endl;
}
if (stridein>1) {
// If input wavefunction has higher resolution, sample it
tx = sx*stridein;
ty = sy*stridein;
tz = sz*stridein;
linenumber = (tx-1)*inputLatticeSize*inputLatticeSize + (ty-1)*inputLatticeSize + (tz-1);
if (debug && nodeID==1) cout << "Respond : " << tx << ", " << ty << ", " << tz << endl;
}
line = lines.at(linenumber);
int space_index = line.find_last_of("\t");
string linemod = line.substr(0,space_index-1);
int space_index2 = linemod.find_last_of("\t");
std::istringstream stream;
stream.str(line.substr(space_index,line.length()-space_index));
stream >> temp; // imaginary part
std::istringstream stream2;
stream2.str(line.substr(space_index2,space_index-space_index2));
stream2 >> temp2; // real part
if (debug && nodeID==1) cout << "line #" << linenumber << " : " << line << endl;
if (debug && nodeID==1) cout << "real : " << temp2 << endl;
if (debug && nodeID==1) cout << "imag : " << temp << endl;
w[sx][sy][sz] = dcomp(temp2,temp);
}
input.close();
break;
case 1:
// random
if (nodeID==1) cout << "==> Initial wavefunction : Random" << endl;
for (sx=0;sx<NUMX+2;sx++)
for (sy=0;sy<NUM+2;sy++)
for (sz=0; sz<NUM+2;sz++)
w[sx][sy][sz] = dcomp(randGauss(sig),0.);
break;
case 2:
// coulomb like
if (nodeID==1) cout << "==> Initial wavefunction : Coulomb" << endl;
for (sx=0;sx<=NUM+1;sx++)
for (sy=0;sy<=NUM+1;sy++)
for (sz=0; sz<=NUM+1;sz++) {
// coordinate system is centered in simulation volume
dx = sx - ((double)NUMX+1.)/2. + ( ((double)nodeID) - ((double)numNodes)/2. )*NUMX;
dy = sy - ((double)NUM+1.)/2.;
dz = sz - ((double)NUM+1.)/2.;
r = A*sqrt(dx*dx+dy*dy+dz*dz);
costheta = A*dz/r;
cosphi = A*dx/r;
w[sx][sy][sz] = exp(-MASS*r); // n=1
w[sx][sy][sz] += (2-MASS*r)*exp(-MASS*r/2); // n=2,l=0
w[sx][sy][sz] += MASS*r*exp(-MASS*r/2)*costheta; // n=2,l=1,m=0
w[sx][sy][sz] += MASS*r*exp(-MASS*r/2)*sqrt(1-costheta*costheta)*cosphi; //n=2,l=1,m=+-1
}
break;
case 3:
// constant
if (nodeID==1) cout << "==> Initial wavefunction : Constant" << endl;
for (sx=0;sx<=NUMX+1;sx++)
for (sy=0;sy<=NUM+1;sy++)
for (sz=0; sz<=NUM+1;sz++)
w[sx][sy][sz] = 0.1;
break;
case 4:
// test grid
if (nodeID==1) cout << "==> Initial wavefunction : Boolean Test" << endl;
for (sx=0;sx<=NUMX+1;sx++)
for (sy=0;sy<=NUM+1;sy++)
for (sz=0; sz<=NUM+1;sz++)
w[sx][sy][sz] = (sx%2)*(sy%2)*(sz%2);
break;
default:
cout << "Invalid initial condition type" << endl;
exit(0);
break;
}
// enforce BCs
for (sx=0;sx<=NUMX+1;sx++)
for (sy=0;sy<=NUM+1;sy++) {
w[sx][sy][0] = 0;
w[sx][sy][NUM+1] = 0;
}
for (sz=0;sz<=NUM+1;sz++)
for (sy=0;sy<=NUM+1;sy++) {
w[0][sy][sz] = 0;
w[NUMX+1][sy][sz] = 0;
}
for (sx=0;sx<=NUMX+1;sx++)
for (sz=0;sz<=NUM+1;sz++) {
w[sx][0][sz] = 0;
w[sx][NUM+1][sz] = 0;
}
// zero out updated wavefnc for safety's sake
for (sx=0;sx<NUMX+2;sx++)
for (sy=0;sy<NUM+2;sy++)
for (sz=0;sz<NUM+2;sz++)
W[sx][sy][sz] = 0;
// symmetrize the intial condition
symmetrizeWavefunction();
}
void symmetrizeWavefunction()
{
int sx,sy,sz,x,y,z,sign=1;
double check=0;
// set sign for inversions; convention is that odd numbers
// are symmetric and even numbers antisymmetric
sign = 2*(INITSYMMETRY%2)-1;
switch (INITSYMMETRY) {
case 0:
// no symmetry
break;
case 1:
// symmetric in z
case 2:
// antisymmetric in z
for (sx=0;sx<NUMX+2;sx++)
{
x=sx;
for (sy=1;sy<=NUM;sy++)
{
y=sy;
for (sz=1;sz<=NUM;sz++)
{
z=sz;
if (z>NUM/2)
z = NUM + 1 - z;
if (sz>NUM/2)
w[sx][sy][sz] = ((dcomp)sign)*w[x][y][z];
}
}
}
break;
case 3:
// symmetric in y
case 4:
// antisymmetric in y
for (sx=0;sx<NUMX+2;sx++)
{
x=sx;
for (sy=1;sy<=NUM;sy++)
{
y=sy;
if (y>NUM/2)
y = NUM + 1 - y;
for (sz=1;sz<=NUM;sz++)
{
z=sz;
if (sy>NUM/2)
w[sx][sy][sz] = ((dcomp)sign)*w[x][y][z];
}
}
}
break;
default:
cout << "Invalid symmetry type" << endl;
exit(0);
break;
}
}