-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnls.h
255 lines (246 loc) · 6.8 KB
/
dnls.h
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
#ifndef DNLS_H_
#define DNLS_H_
/*********************** Import Libraries **************************/
#include <stdlib.h>
#include <stdio.h>
#include<random>
#include<math.h>
#include<time.h>
#include<complex.h>
#include<fftw3.h>
/************************ Macro Definitions ***********************/
#ifndef NX
#define NX 1024 // Number of Lattice Sites
#endif
#ifndef NT
#define NT 1024 // Number of Time Records
#endif
#ifndef BATCH
#define BATCH 1024 // Number of Realizations
#endif
#ifndef TAU
#define TAU 6.283185307179586476925286766559 // 2*PI
#endif
/*********************** Macro Functions ***********************/
/************************ De-CUDA'd Kernels ************************/
void Evolve_Potential(dcomplex *Y, double *V, double g, dcomplex tau)
{
for(int k=0; k<NX*BATCH; k++)
Y[k] *= cexp( tau * (V[k] + g*pow( cuCabs(Y[k]), 2 )) );
}
/*===================================*/
void Evolve_Kinetic_Rotate(dcomplex *Y, double *ROT, dcomplex tau)
{
for(int k=0; k<NX; k++){
for(int j=0; j<BATCH; j++){
Y[k + j*NX] *= cexp( tau * ROT[k] );
Y[k + j*NX] /= (double)NX;
}
}
}
/********************** Class Definitions *********************/
class Time
{
public:
std::complex<double> SIC[5];
double t=0.0f, dt = 1e-2, tmax = 1e4, alf, tss;
bool logf=false;
clock_t tic;
int j=0;
/*=======================================================*/
Time(double dt_, double tmax_, bool logf_) :
dt(dt_), tmax(tmax_), logf(logf_)
{
if(logf)
{
alf = exp( log(tmax/dt) / (double)(NT-1) );
tss = dt * pow( alf, j );
}
else
{
alf = (tmax/dt) / (double)NT;
tss = (j+1) * alf;
}
if(dt > tss) {
printf("ERROR: step (dt) > snapshot (tss)\n");
exit(1);
}
tic = clock();
double C[3] = {
1.0f/6.0f,
1.0f/2.0f,
2.0f/3.0f };
for(int k=0; k<3; k++) SIC[k] = I*dt*C[k];
SIC[3] = 2.0f*SIC[0];
SIC[4] = -1.0f*SIC[0];
}
/*=======================================================*/
void update()
{
j+=1;
if(logf){ tss = dt * pow( alf, j ); }
else{ tss = (j+1) * alf; }
}
/*=======================================================*/
void reset()
{
j=0; t=0.0f;
if(logf) { tss = dt * pow( alf, j ); }
else { tss = (j+1) * alf; }
}
/*=======================================================*/
double toc(void)
{ return ((double)(clock() - tic))/CLOCKS_PER_SEC; }
/*=======================================================*/
};
/****************************************************************/
class Lattice
{
public:
size_t SZ[3] = {
NX*BATCH*sizeof(dcomplex),
NX*BATCH*sizeof(double),
NX*sizeof(double) };
std::complex<double> *Y;
double *V, *ROT;
double g;
cufftHandle plan;
/*=======================================================*/
Lattice(double g_, double W) : g(g_)
/* At some point, we'll need a constructor that has input
* codes for ICs and different potentials.
* Also, need to have device query and perhaps setting
* NX,NT, & BATCH by results. */
{
cudaMalloc( (void**)&Y, SZ[0] );
cudaMalloc( (void**)&V, SZ[1] );
cudaMalloc( (void**)&ROT, SZ[2] );
cufftPlan1d(&plan, NX, CUFFT_Z2Z, BATCH);
//
Build_Full_Packet_Random_Phase(1.0);
Build_Anderson_Potential(W);
Build_Rotator();
}
/*=======================================================*/
~Lattice(void)
{
cufftDestroy(plan);
cudaFree(Y); cudaFree(V);
cudaFree(ROT);
}
/*=======================================================*/
void Build_Full_Packet_Random_Phase(double S0)
{
std::default_random_engine gen;
std::uniform_real_distribution<double> ang(0.0f, TAU);
//
dcomplex *cpuY = (dcomplex*) malloc(SZ[0]);
for(int k=0; k<NX*BATCH; k++)
cpuY[k] = cuCmul(
make_cuDoubleComplex(sqrt(S0),0.0f),
cuExp( make_cuDoubleComplex(0.0f, ang(gen)) )
);
cudaMemcpy(Y, cpuY, SZ[0], cudaMemcpyHostToDevice);
free(cpuY);
}
void Build_Single_Site_Random_Phase(double S0)
{
std::default_random_engine gen;
std::uniform_real_distribution<double> ang(0.0f, TAU);
//
dcomplex *cpuY = (dcomplex*) malloc(SZ[0]);
double arg;
for(int k=0; k<NX*BATCH; k++)
cpuY[k] = make_cuDoubleComplex(0.0f,0.0f);
for(int k=0; k<BATCH; k++)
{
arg = ang(gen);
cpuY[(NX/2) + k*NX].x = sqrt(S0)*cos(arg);
cpuY[(NX/2) + k*NX].y = sqrt(S0)*sin(arg);
}
cudaMemcpy(Y, cpuY, SZ[0], cudaMemcpyHostToDevice);
free(cpuY);
}
/*=======================================================*/
void Build_Anderson_Potential(double W)
{ /* This will eventually need a non-CUDA branch! */
std::default_random_engine gen;
std::uniform_real_distribution<double> distro(-W/2.0,W/2.0);
double *cpuV = (double*) malloc(SZ[1]);
for(int k=0; k<NX*BATCH; k++) cpuV[k] = distro(gen);
cudaMemcpy(V, cpuV, SZ[1], cudaMemcpyHostToDevice);
free(cpuV);
}
/*=======================================================*/
void Build_Rotator()
{ /* This will eventually need a non-CUDA branch! */
double *cpuROT = (double*) malloc(SZ[2]);
for(int k=0; k<NX; k++)
cpuROT[k] = 2.0f*cos( (TAU*k)/(double)NX );
cudaMemcpy(ROT, cpuROT, SZ[2], cudaMemcpyHostToDevice);
free(cpuROT);
}
/*=======================================================*/
void Evolve(Time tobj, std::string fbase)
{
Evolve_Potential<<<NX,BATCH>>>(Y,V,g,tobj.SIC[0]);
while(tobj.t < tobj.tmax)
{
Evolve_Kinetic(tobj.SIC[1]);
Evolve_Potential<<<NX,BATCH>>>(Y,V,g,tobj.SIC[2]);
Evolve_Kinetic(tobj.SIC[1]);
Evolve_Potential<<<NX,BATCH>>>(Y,V,g,tobj.SIC[3]);
tobj.t += tobj.dt;
//
if(tobj.t >= tobj.tss)
{
Evolve_Potential<<<NX,BATCH>>>(Y,V,g,tobj.SIC[4]);
Y2Txt(tobj.t, tobj.j, fbase);
Evolve_Potential<<<NX,BATCH>>>(Y,V,g,tobj.SIC[0]);
tobj.update();
}
}
}
/*=======================================================*/
void Evolve_Kinetic(dcomplex tau)
{
cufftExecZ2Z(plan,Y,Y,CUFFT_FORWARD);
Evolve_Kinetic_Rotate<<<NX,BATCH>>>(Y,ROT,tau);
cufftExecZ2Z(plan,Y,Y,CUFFT_INVERSE);
}
/*=======================================================*/
void Y2Txt(double t, int rec, std::string fbase, bool eikon=true)
{
dcomplex *hY = (dcomplex*) malloc(SZ[0]);
cudaMemcpy(hY,Y,SZ[0],cudaMemcpyDeviceToHost);
std::string ext("_Y.dat");
FILE *fid = fopen( (fbase+ext).c_str(), "a");
fprintf(fid, "%.6e\t", t);
if(eikon==true)
{
for(int k=0; k<NX*BATCH; k++)
fprintf( fid, "%.6e\t%.6e\t", cuCabs(hY[k]), cuCarg(hY[k]) );
}
else
{
for(int k=0; k<NX*BATCH; k++)
fprintf(fid, "%.6e\t%.6e\t", hY[k].x, hY[k].y);
}
fprintf(fid, "\n");
fclose(fid);
free(hY);
}
/*=======================================================*/
void Y2Bin(int rec, std::string fbase)
{
dcomplex *hY = (dcomplex*) malloc(SZ[0]);
cudaMemcpy(hY,Y,SZ[0],cudaMemcpyDeviceToHost);
std::string ext("_Y.bin");
FILE *fid = fopen( (fbase+ext).c_str(), "ab");
fwrite(&hY, SZ[0],1,fid);
fclose(fid);
free(hY);
}
/*=======================================================*/
};
#endif /* DNLS_CUH_ */