-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_libs.cpp
364 lines (313 loc) · 7.92 KB
/
sim_libs.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
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
/*
Copyright 2013, Vasudevan Venkateshwaran, Garde group @ RPI
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
sim_libs.cpp
Functions for carrying out various calculations
*/
// Headers
#include <vector>
#include <cmath>
#include <fftw3.h>
#include "constants.h"
#include "grid.h"
#include "random.h"
void init_velocity ( double *hdot, int nx, int ny, double temp )
{
/*
Velocity initialization: Maxwell distribution with rescaling
*/
int num;
double v_sum = 0;
double v2_sum = 0;
double sf;
num = nx * ny;
for ( int i = 0; i < num; i++ )
{
hdot[i] = marsaglia();
v_sum = v_sum + hdot[i];
v2_sum = v2_sum + hdot[i]*hdot[i];
}
v_sum = v_sum / (double) num; //average velocity
v2_sum = v2_sum / (double) num; //average velocity square
sf = sqrt ( 1.0 * temp / v2_sum ); //scaling factor to desired temperature
//Rescale the velocity to set the initial temperature
for ( int i = 0; i < num; i++ )
{
hdot[i] = ( hdot[i] - v_sum ) *sf;
}
}
void dftreal ( double *hfunc, fftw_complex *dfthfunc,
int nx, int ny, int nh )
{
/*
Fourier transform of a 2D array using fftw3 library
*/
fftw_plan plan_forward;
plan_forward = fftw_plan_dft_r2c_2d ( nx, ny, hfunc,
dfthfunc, FFTW_ESTIMATE );
fftw_execute ( plan_forward );
fftw_destroy_plan ( plan_forward );
}
void idftreal ( fftw_complex *dfthfunc, double *hfunc,
int nx, int ny, int nh )
{
/*
Inverse Fourier transform of a 2D array using fftw3 library
*/
fftw_plan plan_backward;
plan_backward = fftw_plan_dft_c2r_2d ( nx, ny, dfthfunc,
hfunc, FFTW_ESTIMATE );
fftw_execute ( plan_backward );
fftw_destroy_plan( plan_backward );
}
void normalize_fft_array ( fftw_complex *h, int nx, int ny, int nyh )
{
/*
Normalize a fftw_complex array using the symmetric normalization
condition
*/
int kx, ky;
int el, cDIM;
for ( kx = 0; kx < nx; kx++ )
{
for ( ky = 0; ky < nyh; ky++ )
{
el = kx*nyh + ky;
for ( cDIM = 0; cDIM < 2; cDIM++ )
{
h[el][cDIM] = h[el][cDIM] / sqrt( (double) (nx * ny) );
}
}
}
}
void normalize_ifft_array ( double *h, int nx, int ny )
{
/*
Normalize a double array returned by inverse Fourier transform
using the symmetric normalization condition
*/
int kx;
for ( kx = 0; kx < nx * ny; kx++ )
{
h[kx] = h[kx] / sqrt( (double) (nx * ny) );
}
}
void duplicate_fftw_array ( fftw_complex *h, fftw_complex* hc, int nx, int ny )
{
/*
Duplicate a fftw_complex datatype
*/
int kx, ky;
int el, cDIM;
for ( kx = 0; kx < nx; kx++ )
{
for ( ky = 0; ky < ny; ky++ )
{
el = kx*ny + ky;
for ( cDIM = 0; cDIM < 2; cDIM++ )
{
hc[el][cDIM] = h[el][cDIM];
}
}
}
}
void generate_psitwiddle(fftw_complex *psitwiddle, int nx, int ny, int nh,
double mean, double var)
{
/*
Generate the Langevin noise term in fourier space
*/
int i,kx,ky,n,p;
std::vector<double> x,y;
int halfmode;
double sqrtvar;
double twosqrtvar;
sqrtvar = sqrt ( var );
twosqrtvar = sqrt ( 2.0 * var );
n = nx * nh;
x.reserve(n);
y.reserve(n);
// Generate the random array
marsaglia_array(x,n);
marsaglia_array(y,n);
// Need to check for the pure real modes
if ( nx % 2 == 0 )
{
halfmode = nx/2 - 1;
}
else
{
halfmode = (int) ( nx/2 );
}
for ( kx = 0; kx < nx; kx++ )
{
for ( ky = 0; ky < nh; ky++ )
{
int el = kx*nh + ky;
if ( el == 0 )
{
x[el] = twosqrtvar*x[el] + mean;
y[el] = 0.0;
}
else
{
x[el] = sqrtvar*x[el] + mean;
y[el] = sqrtvar*y[el] + mean;
}
psitwiddle[el][0] = x[el];
psitwiddle[el][1] = y[el];
}
}
x.clear();
y.clear();
}
void calcforce(fftw_complex *htwiddle, fftw_complex *psitwiddle,
fftw_complex *fpintwiddle, fftw_complex* Gt,
double *modksq, double gamma, double dx, double dy,
int elstart, int nfftwpoints)
{
/*
Calculate the force due a particular configuration specified
by htwiddle. Add the contribution of Langevin damping terms
to the force
*/
int el;
double gd;
gd = gamma * dx * dy;
for ( el = elstart; el < nfftwpoints; el++ )
{
for ( int cDIM = 0; cDIM < 2; cDIM++ )
{
Gt[el][cDIM] = psitwiddle[el][cDIM] -
gd * modksq[el] * htwiddle[el][cDIM]
- fpintwiddle[el][cDIM];
}
}
if ( elstart != 0 )
{
Gt[0][0] = 0.0;
Gt[0][1] = 0.0;
}
}
// void calcforce(fftw_complex *htwiddle, fftw_complex *psitwiddle,
// fftw_complex *fpintwiddle, fftw_complex* Gt, double *modksq,
// double gamma, double dx, double dy, int nx, int ny, int nh)
// {
// /*
// Calculate the force due a particular configuration specified
// by htwiddle. Add the contribution of Langevin damping terms
// to the force
// */
// // int kx,ky;
// int el;
// double gd;
//
// gd = gamma * dx * dy;
//
// for ( el = elstart; el < nfftwpoints; el++ )
// {
// for ( int cDIM = 0; cDIM < 2; cDIM++ )
// {
// Gt[el][cDIM] = psitwiddle[el][cDIM] -
// gd * modksq[el] * htwiddle[el][cDIM]
// - fpintwiddle[el][cDIM];
// }
// }
//
// // for ( kx = 0; kx < nx; kx++ )
// // {
// // for ( ky = 0; ky < nh; ky++ )
// // {
// // int el = kx * nh + ky;
// // for ( int cDIM = 0; cDIM < 2; cDIM++ )
// // {
// // Gt[el][cDIM] = psitwiddle[el][cDIM] -
// // gd * modksq[el] * htwiddle[el][cDIM]
// // - fpintwiddle[el][cDIM];
// // }
// // }
// // }
// }
void generate_modksq ( double *modksq, t_Grid* grid )
{
/*
Generate the modulus-squared of the k space wave-vectors
*/
int kx, ky;
double tplx, tply;
tplx = TWOPI / grid->LX;
tply = TWOPI / grid->LY;
for ( kx = 0; kx < grid->nx; kx++ )
{
for ( ky = 0; ky < grid->nyh; ky++ )
{
int el = kx*grid->nyh+ky;
if ( grid->nx % 2 == 0 )
{
if ( kx > grid->nx/2 )
{
modksq[el] = ( (tplx*(kx-grid->nx)) *
(tplx*(kx-grid->nx)) +
(tply*ky) * (tply*ky) );
}
else
{
modksq[el] = ( (tplx*kx) * (tplx*kx) +
(tply*ky) * (tply*ky) );
}
}
else
{
if ( kx > (grid->nx-1)/2 )
{
modksq[el] = ( (tplx*(kx-grid->nx)) *
(tplx*(kx-grid->nx)) +
(tply*ky) * (tply*ky) );
}
else
{
modksq[el] = ( (tplx*kx) * (tplx*kx) +
(tply*ky) * (tply*ky) );
}
}
}
}
}
double calculate_com ( double *h, int nx, int ny )
{
/*
Calculate the center of mass of a given configuration
*/
double zinit = 0;
int num;
num = nx * ny;
for ( int i = 0; i < num; i++ )
{
zinit = zinit + h[i];
}
zinit = zinit/(double)(num); //center of mass
return zinit;
}
void recenter_comm ( double *h, int nx, int ny, double zinit )
{
/*
Recenter the configuration to a given z location
*/
int num;
double sum = 0;
sum = calculate_com ( h, nx, ny );
// Rescale the coordinates
for ( int i = 0; i < num; i++ )
{
h[i] = h[i] - sum + zinit;
}
}