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