1 |
#include <iostream> |
2 |
#include <stdlib.h> |
3 |
|
4 |
#include "Atom.hpp" |
5 |
#include "SRI.hpp" |
6 |
#include "LRI.hpp" |
7 |
#include "Integrator.hpp" |
8 |
#include "SimInfo.hpp" |
9 |
#include "Thermo.hpp" |
10 |
#include "ReadWrite.hpp" |
11 |
|
12 |
extern "C"{ |
13 |
|
14 |
void v_constrain_a_( double &dt, int &n_atoms, double* mass, |
15 |
double* Rx, double* Ry, double* Rz, |
16 |
double* Vx, double* Vy, double* Vz, |
17 |
double* Fx, double* Fy, double* Fz, |
18 |
int &n_constrained, double *constr_sqr, |
19 |
int* constr_i, int* constr_j, |
20 |
double &box_x, double &box_y, double &box_z ); |
21 |
|
22 |
void v_constrain_b_( double &dt, int &n_atoms, double* mass, |
23 |
double* Rx, double* Ry, double* Rz, |
24 |
double* Vx, double* Vy, double* Vz, |
25 |
double* Fx, double* Fy, double* Fz, |
26 |
double &Kinetic, |
27 |
int &n_constrained, double *constr_sqr, |
28 |
int* constr_i, int* constr_j, |
29 |
double &box_x, double &box_y, double &box_z ); |
30 |
} |
31 |
|
32 |
|
33 |
Verlet::Verlet( SimInfo &info ){ |
34 |
|
35 |
// get what information we need from the SimInfo object |
36 |
|
37 |
entry_plug = &info; |
38 |
|
39 |
c_natoms = info.n_atoms; |
40 |
c_atoms = info.atoms; |
41 |
c_sr_interactions = info.sr_interactions; |
42 |
longRange = info.longRange; |
43 |
c_n_SRI = info.n_SRI; |
44 |
c_is_constrained = 0; |
45 |
c_box_x = info.box_x; |
46 |
c_box_y = info.box_y; |
47 |
c_box_z = info.box_z; |
48 |
|
49 |
// give a little love back to the SimInfo object |
50 |
|
51 |
if( info.the_integrator != NULL ) delete info.the_integrator; |
52 |
info.the_integrator = this; |
53 |
|
54 |
// the rest are initialization issues |
55 |
|
56 |
is_first = 1; // let the integrate method know when the first call is |
57 |
|
58 |
// mass array setup |
59 |
|
60 |
c_mass = new double[c_natoms]; |
61 |
|
62 |
for(int i = 0; i < c_natoms; i++){ |
63 |
c_mass[i] = c_atoms[i]->getMass(); |
64 |
} |
65 |
|
66 |
// check for constraints |
67 |
|
68 |
Constraint *temp_con; |
69 |
Constraint *dummy_plug; |
70 |
temp_con = new Constraint[c_n_SRI]; |
71 |
|
72 |
c_n_constrained = 0; |
73 |
int constrained = 0; |
74 |
|
75 |
for(int i = 0; i < c_n_SRI; i++){ |
76 |
|
77 |
constrained = c_sr_interactions[i]->is_constrained(); |
78 |
|
79 |
if(constrained){ |
80 |
|
81 |
dummy_plug = c_sr_interactions[i]->get_constraint(); |
82 |
temp_con[c_n_constrained].set_a( dummy_plug->get_a() ); |
83 |
temp_con[c_n_constrained].set_b( dummy_plug->get_b() ); |
84 |
temp_con[c_n_constrained].set_dsqr( dummy_plug->get_dsqr() ); |
85 |
|
86 |
c_n_constrained++; |
87 |
constrained = 0; |
88 |
} |
89 |
} |
90 |
|
91 |
if(c_n_constrained > 0){ |
92 |
|
93 |
c_is_constrained = 1; |
94 |
c_constrained_i = new int[c_n_constrained]; |
95 |
c_constrained_j = new int[c_n_constrained]; |
96 |
c_constrained_dsqr = new double[c_n_constrained]; |
97 |
|
98 |
for( int i = 0; i < c_n_constrained; i++){ |
99 |
|
100 |
/* add 1 to the index for the fortran arrays. */ |
101 |
|
102 |
c_constrained_i[i] = temp_con[i].get_a() + 1; |
103 |
c_constrained_j[i] = temp_con[i].get_b() + 1; |
104 |
c_constrained_dsqr[i] = temp_con[i].get_dsqr(); |
105 |
} |
106 |
} |
107 |
|
108 |
delete[] temp_con; |
109 |
} |
110 |
|
111 |
|
112 |
Verlet::~Verlet(){ |
113 |
|
114 |
if( c_is_constrained ){ |
115 |
|
116 |
delete[] c_constrained_i; |
117 |
delete[] c_constrained_j; |
118 |
delete[] c_constrained_dsqr; |
119 |
} |
120 |
|
121 |
delete[] c_mass; |
122 |
c_mass = 0; |
123 |
} |
124 |
|
125 |
|
126 |
|
127 |
void Verlet::integrate_b( double time_length, double dt, |
128 |
int n_bond_0, int n_bond_f, |
129 |
int n_bend_0, int n_bend_f, |
130 |
int n_torsion_0, int n_torsion_f, |
131 |
bool do_bonds, bool do_bends, bool do_torsions, |
132 |
bool do_LRI ){ |
133 |
|
134 |
// double percent_tolerance = 0.001; |
135 |
// int max_iterations = 10000; |
136 |
|
137 |
int i, j; /* loop counters */ |
138 |
double n_loops = time_length / dt; |
139 |
|
140 |
// the first time integrate is called, the forces need to be initialized |
141 |
|
142 |
if(is_first){ |
143 |
is_first = 0; |
144 |
|
145 |
for(i = 0; i < c_natoms; i++){ |
146 |
c_atoms[i]->zeroForces(); |
147 |
} |
148 |
|
149 |
if( do_bonds ){ |
150 |
for(i = n_bond_0; i <= n_bond_f; i++){ |
151 |
c_sr_interactions[i]->calc_forces(); |
152 |
} |
153 |
} |
154 |
|
155 |
if( do_bends ){ |
156 |
for(i = n_bend_0; i <= n_bend_f; i++){ |
157 |
c_sr_interactions[i]->calc_forces(); |
158 |
} |
159 |
} |
160 |
|
161 |
if( do_torsions ){ |
162 |
for(i = n_torsion_0; i <= n_torsion_f; i++){ |
163 |
c_sr_interactions[i]->calc_forces(); |
164 |
} |
165 |
} |
166 |
|
167 |
if( do_LRI ) longRange->calc_forces(); |
168 |
} |
169 |
|
170 |
for(i = 0; i < n_loops; i++){ |
171 |
|
172 |
move_a( dt ); |
173 |
|
174 |
// calculate the forces |
175 |
|
176 |
for(j = 0; j < c_natoms; j++){ |
177 |
c_atoms[j]->zeroForces(); |
178 |
} |
179 |
|
180 |
|
181 |
if( do_bonds ){ |
182 |
for(i = n_bond_0; i <= n_bond_f; i++){ |
183 |
c_sr_interactions[i]->calc_forces(); |
184 |
} |
185 |
} |
186 |
|
187 |
if( do_bends ){ |
188 |
for(i = n_bend_0; i <= n_bend_f; i++){ |
189 |
c_sr_interactions[i]->calc_forces(); |
190 |
} |
191 |
} |
192 |
|
193 |
if( do_torsions ){ |
194 |
for(i = n_torsion_0; i <= n_torsion_f; i++){ |
195 |
c_sr_interactions[i]->calc_forces(); |
196 |
} |
197 |
} |
198 |
|
199 |
if( do_LRI ) longRange->calc_forces(); |
200 |
|
201 |
|
202 |
// complete the verlet move |
203 |
|
204 |
move_b( dt ); |
205 |
} |
206 |
} |
207 |
|
208 |
|
209 |
void Verlet::integrate( void ){ |
210 |
|
211 |
int i, j; /* loop counters */ |
212 |
|
213 |
double kE; |
214 |
|
215 |
double *Rx = new double[c_natoms]; |
216 |
double *Ry = new double[c_natoms]; |
217 |
double *Rz = new double[c_natoms]; |
218 |
|
219 |
double *Vx = new double[c_natoms]; |
220 |
double *Vy = new double[c_natoms]; |
221 |
double *Vz = new double[c_natoms]; |
222 |
|
223 |
double *Fx = new double[c_natoms]; |
224 |
double *Fy = new double[c_natoms]; |
225 |
double *Fz = new double[c_natoms]; |
226 |
|
227 |
int time; |
228 |
|
229 |
double dt = entry_plug->dt; |
230 |
double runTime = entry_plug->run_time; |
231 |
double sampleTime = entry_plug->sampleTime; |
232 |
double statusTime = entry_plug->statusTime; |
233 |
double thermalTime = entry_plug->thermalTime; |
234 |
|
235 |
int n_loops = (int)( runTime / dt ); |
236 |
int sample_n = (int)( sampleTime / dt ); |
237 |
int status_n = (int)( statusTime / dt ); |
238 |
int vel_n = (int)( thermalTime / dt ); |
239 |
|
240 |
Thermo *tStats = new Thermo( entry_plug ); |
241 |
|
242 |
StatWriter* e_out = new StatWriter( entry_plug ); |
243 |
DumpWriter* dump_out = new DumpWriter( entry_plug ); |
244 |
|
245 |
// the first time integrate is called, the forces need to be initialized |
246 |
|
247 |
|
248 |
for(i = 0; i < c_natoms; i++){ |
249 |
c_atoms[i]->zeroForces(); |
250 |
} |
251 |
|
252 |
for(i = 0; i < c_n_SRI; i++){ |
253 |
c_sr_interactions[i]->calc_forces(); |
254 |
} |
255 |
|
256 |
longRange->calc_forces(); |
257 |
|
258 |
if( entry_plug->setTemp ){ |
259 |
tStats->velocitize(); |
260 |
} |
261 |
|
262 |
dump_out->writeDump( 0.0 ); |
263 |
e_out->writeStat( 0.0 ); |
264 |
|
265 |
if( c_is_constrained ){ |
266 |
for(i = 0; i < n_loops; i++){ |
267 |
|
268 |
// fill R, V, and F arrays and RATTLE in fortran |
269 |
|
270 |
for( j=0; j<c_natoms; j++ ){ |
271 |
|
272 |
Rx[j] = c_atoms[j]->getX(); |
273 |
Ry[j] = c_atoms[j]->getY(); |
274 |
Rz[j] = c_atoms[j]->getZ(); |
275 |
|
276 |
Vx[j] = c_atoms[j]->get_vx(); |
277 |
Vy[j] = c_atoms[j]->get_vy(); |
278 |
Vz[j] = c_atoms[j]->get_vz(); |
279 |
|
280 |
Fx[j] = c_atoms[j]->getFx(); |
281 |
Fy[j] = c_atoms[j]->getFy(); |
282 |
Fz[j] = c_atoms[j]->getFz(); |
283 |
|
284 |
} |
285 |
|
286 |
v_constrain_a_( dt, c_natoms, c_mass, Rx, Ry, Rz, Vx, Vy, Vz, |
287 |
Fx, Fy, Fz, |
288 |
c_n_constrained, c_constrained_dsqr, |
289 |
c_constrained_i, c_constrained_j, |
290 |
c_box_x, c_box_y, c_box_z ); |
291 |
|
292 |
for( j=0; j<c_natoms; j++ ){ |
293 |
|
294 |
c_atoms[j]->setX(Rx[j]); |
295 |
c_atoms[j]->setY(Ry[j]); |
296 |
c_atoms[j]->setZ(Rz[j]); |
297 |
|
298 |
c_atoms[j]->set_vx(Vx[j]); |
299 |
c_atoms[j]->set_vy(Vy[j]); |
300 |
c_atoms[j]->set_vz(Vz[j]); |
301 |
} |
302 |
|
303 |
// calculate the forces |
304 |
|
305 |
for(j = 0; j < c_natoms; j++){ |
306 |
c_atoms[j]->zeroForces(); |
307 |
} |
308 |
|
309 |
for(j = 0; j < c_n_SRI; j++){ |
310 |
c_sr_interactions[j]->calc_forces(); |
311 |
} |
312 |
|
313 |
longRange->calc_forces(); |
314 |
|
315 |
// finish the constrain move ( same as above. ) |
316 |
|
317 |
for( j=0; j<c_natoms; j++ ){ |
318 |
|
319 |
Rx[j] = c_atoms[j]->getX(); |
320 |
Ry[j] = c_atoms[j]->getY(); |
321 |
Rz[j] = c_atoms[j]->getZ(); |
322 |
|
323 |
Vx[j] = c_atoms[j]->get_vx(); |
324 |
Vy[j] = c_atoms[j]->get_vy(); |
325 |
Vz[j] = c_atoms[j]->get_vz(); |
326 |
|
327 |
Fx[j] = c_atoms[j]->getFx(); |
328 |
Fy[j] = c_atoms[j]->getFy(); |
329 |
Fz[j] = c_atoms[j]->getFz(); |
330 |
} |
331 |
|
332 |
v_constrain_b_( dt, c_natoms, c_mass, Rx, Ry, Rz, Vx, Vy, Vz, |
333 |
Fx, Fy, Fz, |
334 |
kE, c_n_constrained, c_constrained_dsqr, |
335 |
c_constrained_i, c_constrained_j, |
336 |
c_box_x, c_box_y, c_box_z ); |
337 |
|
338 |
for( j=0; j<c_natoms; j++ ){ |
339 |
|
340 |
c_atoms[j]->setX(Rx[j]); |
341 |
c_atoms[j]->setY(Ry[j]); |
342 |
c_atoms[j]->setZ(Rz[j]); |
343 |
|
344 |
c_atoms[j]->set_vx(Vx[j]); |
345 |
c_atoms[j]->set_vy(Vy[j]); |
346 |
c_atoms[j]->set_vz(Vz[j]); |
347 |
} |
348 |
|
349 |
time = i + 1; |
350 |
|
351 |
if( entry_plug->setTemp ){ |
352 |
if( !(time % vel_n) ) tStats->velocitize(); |
353 |
} |
354 |
if( !(time % sample_n) ) dump_out->writeDump( time * dt ); |
355 |
if( !(time % status_n) ) e_out->writeStat( time * dt ); |
356 |
} |
357 |
} |
358 |
else{ |
359 |
for(i = 0; i < n_loops; i++){ |
360 |
|
361 |
move_a( dt ); |
362 |
|
363 |
// calculate the forces |
364 |
|
365 |
for(j = 0; j < c_natoms; j++){ |
366 |
c_atoms[j]->zeroForces(); |
367 |
} |
368 |
|
369 |
for(j = 0; j < c_n_SRI; j++){ |
370 |
c_sr_interactions[j]->calc_forces(); |
371 |
} |
372 |
|
373 |
longRange->calc_forces(); |
374 |
|
375 |
// complete the verlet move |
376 |
|
377 |
move_b( dt ); |
378 |
|
379 |
time = i + 1; |
380 |
|
381 |
if( entry_plug->setTemp ){ |
382 |
if( !(time % vel_n) ) tStats->velocitize(); |
383 |
} |
384 |
if( !(time % sample_n) ) dump_out->writeDump( time * dt ); |
385 |
if( !(time % status_n) ) e_out->writeStat( time * dt ); |
386 |
} |
387 |
} |
388 |
|
389 |
dump_out->writeFinal(); |
390 |
|
391 |
delete dump_out; |
392 |
delete e_out; |
393 |
|
394 |
} |
395 |
|
396 |
|
397 |
void Verlet::move_a(double dt){ |
398 |
|
399 |
const double e_convert = 4.184e-4; // converts kcal/mol -> amu*A^2/fs^2 |
400 |
|
401 |
double qx, qy, qz; |
402 |
double vx, vy, vz; |
403 |
int ma; |
404 |
double h_dt = 0.5 * dt; |
405 |
double h_dt2 = h_dt * dt; |
406 |
|
407 |
for( ma = 0; ma < c_natoms; ma++){ |
408 |
|
409 |
qx = c_atoms[ma]->getX() + dt * c_atoms[ma]->get_vx() + |
410 |
h_dt2 * c_atoms[ma]->getFx() * e_convert / c_atoms[ma]->getMass(); |
411 |
qy = c_atoms[ma]->getY() + dt * c_atoms[ma]->get_vy() + |
412 |
h_dt2 * c_atoms[ma]->getFy() * e_convert / c_atoms[ma]->getMass(); |
413 |
qz = c_atoms[ma]->getZ() + dt * c_atoms[ma]->get_vz() + |
414 |
h_dt2 * c_atoms[ma]->getFz() * e_convert / c_atoms[ma]->getMass(); |
415 |
|
416 |
vx = c_atoms[ma]->get_vx() + |
417 |
h_dt * c_atoms[ma]->getFx() * e_convert / c_atoms[ma]->getMass(); |
418 |
vy = c_atoms[ma]->get_vy() + |
419 |
h_dt * c_atoms[ma]->getFy() * e_convert / c_atoms[ma]->getMass(); |
420 |
vz = c_atoms[ma]->get_vz() + |
421 |
h_dt * c_atoms[ma]->getFz() * e_convert / c_atoms[ma]->getMass(); |
422 |
|
423 |
c_atoms[ma]->setX(qx); |
424 |
c_atoms[ma]->setY(qy); |
425 |
c_atoms[ma]->setZ(qz); |
426 |
|
427 |
c_atoms[ma]->set_vx(vx); |
428 |
c_atoms[ma]->set_vy(vy); |
429 |
c_atoms[ma]->set_vz(vz); |
430 |
} |
431 |
} |
432 |
|
433 |
void Verlet::move_b( double dt ){ |
434 |
|
435 |
const double e_convert = 4.184e-4; // converts kcal/mol -> amu*A^2/fs^2 |
436 |
|
437 |
double vx, vy, vz; |
438 |
double v_sqr; |
439 |
int mb; |
440 |
double h_dt = 0.5 * dt; |
441 |
|
442 |
|
443 |
for( mb = 0; mb < c_natoms; mb++){ |
444 |
|
445 |
vx = c_atoms[mb]->get_vx() + |
446 |
h_dt * c_atoms[mb]->getFx() * e_convert / c_atoms[mb]->getMass(); |
447 |
vy = c_atoms[mb]->get_vy() + |
448 |
h_dt * c_atoms[mb]->getFy() * e_convert / c_atoms[mb]->getMass(); |
449 |
vz = c_atoms[mb]->get_vz() + |
450 |
h_dt * c_atoms[mb]->getFz() * e_convert / c_atoms[mb]->getMass(); |
451 |
|
452 |
c_atoms[mb]->set_vx(vx); |
453 |
c_atoms[mb]->set_vy(vy); |
454 |
c_atoms[mb]->set_vz(vz); |
455 |
} |
456 |
} |