-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_run.cpp
335 lines (285 loc) · 9.82 KB
/
sim_run.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
/*
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_run.cpp
Routine which propagates the equations of motion using a velocity-Verlet
integrator.
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <vector>
#include <ctime>
#include "unistd.h"
#include "xdrfile.h"
#include "xdrfile_xtc.h"
#include "fftw3.h"
#include "constants.h"
#include "main.h"
#include "sim_io.h"
#include "sim_libs.h"
#include "grid.h"
#include "sim_field.h"
#define new_fftw(m) (fftw_complex*) fftw_malloc ( sizeof ( fftw_complex ) * m )
void run_simulation ( inputrec* ir )
{
// Program variables
int nx; // Number of x-grid points
int ny; // Number of y-grid points
int nyh; // Number of y-grid points in fft
XDRFILE* cnutraj; // File id for output
float xtctime; // Output file time
int step; // Step number
double beta; // 1/(k_B T)
double eta; // Langevin damping parameter
int kx,ky; // Counter variables;
double mean; // Mean of gaussian random noise
double var; // Variance of gaussian random noise
matrix xtcbox; // Box needed for xtc
double m; // mass of elemental area
double cutoffk; // Cutoff for k vector
double zcominit; // initial z center of mass
std::ofstream logfile;
std::ofstream gridfile;
// Coefficients to simplify computation
double vxcoeff;
double fxcoeff;
double vvcoeff;
double fvcoeff;
double vvdenom;
// Allocatables
double* h; // h(x,y) defines the interface
double* hzref; // h(x,y) defines the interface
double* hdot; // Fictitious velocities of h(x,y)
double* modksq; // Modulus square of the wave-vector
fftw_complex* htwiddle; // Fourier transform of h(x,y)
fftw_complex* htwiddlecopy; // Fourier transform of h(x,y)
fftw_complex* hdottwiddle; // Fourier transform of hdot(x,y)
fftw_complex* hdottwiddlecopy; // Fourier transform of hdot(x,y)
fftw_complex* psitwiddle; // Random noise term
fftw_complex* Gt; // Force vector at time t
fftw_complex* Gtdt; // Force vector at time t+dt
// Terms to deal with external forces
double* fpin;
fftw_complex* fpintwiddle;
t_Field fd;
// Diffusion
int elstart;
// Open the log file
if ( ir->nsteps > 0 )
{
logfile.open ( ir->log.c_str() );
gridfile.open ( ir->grf.c_str() );
}
else
{
std::cout << "Nothing to run\n.";
std::cout << "If you have not specified the number of steps \n"
<< "in the parameter file the simulation will not run.\n";
exit (EXIT_SUCCESS);
}
// Print the grid information and close grid_file
if ( gridfile.is_open() )
{
gridfile << ir->grid.box[XX] << " "
<< ir->grid.box[YY] << " "
<< ir->grid.box[ZZ] << "\n";
gridfile << ir->grid.dx << " " << ir->grid.dy << "\n";
gridfile << ir->grid.nx << " " << ir->grid.ny << "\n";
gridfile.close();
}
if ( ir->bdiffusion )
elstart = 0;
else
elstart = 1;
std::cout << "elstart = " << elstart << "\n";
// Declare some variables based on input
nx = ir->grid.nx;
ny = ir->grid.ny;
nyh = ir->grid.nyh;
beta = 1.0 / ( k_b * ir->T );
m = ir->mu * ir->grid.dx * ir->grid.dy;
eta = m / ir->tau;
cutoffk = ( TWOPI / ir->l ) * ( TWOPI / ir->l );
mean = 0.0;
var = eta * k_b * ir->T / ir->dt;
vxcoeff = ir->dt * ( 1 - ir->dt / ( 2.0 * ir->tau ) );
fxcoeff = 0.5* ir->dt *ir->dt / m ;
vvcoeff = 1 - ir->dt / ( 2.0 * ir->tau ) ;
fvcoeff = 0.5 * ir->dt / m ;
vvdenom = 1 + ir->dt / ( 2.0 * ir->tau ) ;
// Write box information in a form that will be used by the xtc file
for ( int cDIM = 0; cDIM < 3; cDIM++ )
{
for ( int dDIM = 0; dDIM < 3; dDIM++ )
{
xtcbox[cDIM][dDIM] = 0;
}
}
xtcbox[0][0] = ir->grid.box[XX];
xtcbox[1][1] = ir->grid.box[YY];
xtcbox[2][2] = ir->grid.box[ZZ];
// Write stuff to log file
print_input_parameters ( ir );
printlog_input_parameters ( ir, logfile );
// Allocate arrays
int ngridpoints = ir->grid.nx * ir->grid.ny;
int nfftwpoints = ir->grid.nx * ir->grid.nyh;
h = new double[ngridpoints];
hdot = new double[ngridpoints];
fpin = new double[ngridpoints];
modksq = new double[nfftwpoints];
htwiddle = new_fftw ( nfftwpoints );
hdottwiddle = new_fftw ( nfftwpoints );
fpintwiddle = new_fftw ( nfftwpoints );
htwiddlecopy = new_fftw ( nfftwpoints );
hdottwiddlecopy = new_fftw ( nfftwpoints );
psitwiddle = new_fftw ( nfftwpoints );
Gt = new_fftw ( nfftwpoints );
Gtdt = new_fftw ( nfftwpoints );
// Initialize the coordinates and velocities
srand((unsigned)time(NULL));
for ( int i = 0; i < ngridpoints; i++ )
{
h[i] = ir->hinit;
hdot[i] = 0.0;
}
//init_velocity(hdot,nx,ny,ir->T);
// Open output file
if ( ir->nsteps > 0 )
{
write_gro ( ir->gro.c_str(), h, &ir->grid );
cnutraj = xdrfile_open(ir->traj.c_str(),"w");
writeframe(h, &ir->grid,cnutraj,0,0.0,xtcbox);
}
// Preprocess some variables before entering loop
// Calculate force due to constraint hamiltonian here
generate_modksq ( modksq, &ir->grid );
if ( ir->bfield )
{
init_fields(ir->fieldfile.c_str(), &fd, ir->T, logfile );
field_potential ( h, fpin, &ir->grid, &fd );
dftreal ( fpin, fpintwiddle, nx, ny, nyh );
normalize_fft_array ( fpintwiddle, nx, ny, nyh );
}
else
{
// Set the force array to zero
for ( int el = 0; el < nfftwpoints; el++ )
for ( int cDIM = 0; cDIM < 2; cDIM++ )
fpintwiddle[el][cDIM] = 0.0;
}
dftreal ( h, htwiddle, nx, ny, nyh );
dftreal ( hdot, hdottwiddle, nx, ny, nyh );
normalize_fft_array ( htwiddle, nx, ny, nyh );
normalize_fft_array ( hdottwiddle, nx, ny, nyh );
generate_psitwiddle ( psitwiddle, nx, ny, nyh, mean, var );
calcforce(htwiddle,psitwiddle,fpintwiddle,Gt,modksq,ir->gamma,
ir->grid.dx,ir->grid.dy,elstart,nfftwpoints);
// Main loop
std::cout << "Starting the integration\n";
for ( step = 1; step <= ir->nsteps; step++ )
{
xtctime = step*ir->dt;
if ( step % 1000 == 0 )
{
std::cout << "\r" << "Finished " << step << " of " << ir->nsteps << " steps " << std::flush;
}
// Generate the coordinates
for ( int el = elstart; el < nfftwpoints; el++ )
{
if ( modksq[el] < cutoffk )
{
for ( int cDIM = 0; cDIM < 2; cDIM++ )
{
htwiddle[el][cDIM] +=
vxcoeff * hdottwiddle[el][cDIM] +
fxcoeff * Gt[el][cDIM];
}
}
else
{
for ( int cDIM = 0; cDIM < 2; cDIM++ )
htwiddle[el][cDIM] = 0.0;
}
}
// Duplicate arrays since the inverse transform destroys the original arrays
duplicate_fftw_array ( htwiddle, htwiddlecopy, nx, nyh );
idftreal( htwiddlecopy, h, nx, ny, nyh );
normalize_ifft_array ( h, nx, ny );
// Evaluate force due to constrain hamiltonian terms in real space
if ( ir->bfield )
{
field_potential ( h, fpin, &ir->grid, &fd );
dftreal ( fpin, fpintwiddle, nx, ny, nyh );
normalize_fft_array ( fpintwiddle, nx, ny, nyh );
}
// Generate psitwiddle(t+dt)
generate_psitwiddle(psitwiddle,nx,ny,nyh,mean,var);
// Compute the force in the current configuration
calcforce( htwiddle,psitwiddle,fpintwiddle,Gtdt,modksq,ir->gamma,
ir->grid.dx,ir->grid.dy,elstart,nfftwpoints);
// Update the velocities
for ( int el = elstart; el < nfftwpoints; el++ )
{
if ( modksq[el] < cutoffk )
{
for ( int cDIM = 0; cDIM < 2; cDIM++ )
{
hdottwiddle[el][cDIM] =
vvcoeff * hdottwiddle[el][cDIM] +
fvcoeff * ( Gt[el][cDIM] + Gtdt[el][cDIM] );
hdottwiddle[el][cDIM] =
hdottwiddle[el][cDIM] / vvdenom;
}
}
else
{
for ( int cDIM = 0; cDIM < 2; cDIM++ )
hdottwiddle[el][cDIM] = 0.0;
}
}
// Duplicate arrays since the inverse transform destroys the original arrays
duplicate_fftw_array ( hdottwiddle, hdottwiddlecopy, nx, nyh );
idftreal( hdottwiddlecopy, hdot, nx, ny, nyh );
normalize_ifft_array ( hdot, nx, ny );
// Write output
if ( step % ir->nxtc == 0 )
writeframe(h,&ir->grid,cnutraj,step,xtctime,xtcbox);
// Initialize stuff for next step
duplicate_fftw_array ( Gtdt, Gt, nx, nyh );
}
// Deallocate arrays and close files
delete [] h;
delete [] hdot;
delete [] fpin;
delete [] modksq;
fftw_free ( htwiddle );
fftw_free ( hdottwiddle );
fftw_free ( psitwiddle );
fftw_free ( htwiddlecopy );
fftw_free ( hdottwiddlecopy );
fftw_free ( Gt );
fftw_free ( Gtdt );
fftw_free ( fpintwiddle );
if ( ir->bfield )
finish_fields( &fd );
if ( cnutraj )
xdrfile_close(cnutraj);
if ( logfile.is_open() )
logfile.close();
std::cout << "\n";
}