1 |
|
#include <iostream> |
2 |
< |
#include <cstdlib> |
3 |
< |
#include <cmath> |
2 |
> |
#include <stdlib.h> |
3 |
> |
#include <math.h> |
4 |
|
|
5 |
|
#ifdef IS_MPI |
6 |
|
#include "mpiSimulation.hpp" |
7 |
|
#include <unistd.h> |
8 |
|
#endif //is_mpi |
9 |
|
|
10 |
+ |
#ifdef PROFILE |
11 |
+ |
#include "mdProfile.hpp" |
12 |
+ |
#endif // profile |
13 |
+ |
|
14 |
|
#include "Integrator.hpp" |
15 |
|
#include "simError.h" |
16 |
|
|
29 |
|
if (info->the_integrator != NULL){ |
30 |
|
delete info->the_integrator; |
31 |
|
} |
28 |
– |
info->the_integrator = this; |
32 |
|
|
33 |
|
nAtoms = info->n_atoms; |
34 |
+ |
integrableObjects = info->integrableObjects; |
35 |
|
|
36 |
|
// check for constraints |
37 |
|
|
69 |
|
|
70 |
|
SRI** theArray; |
71 |
|
for (int i = 0; i < nMols; i++){ |
72 |
< |
theArray = (SRI * *) molecules[i].getMyBonds(); |
72 |
> |
|
73 |
> |
theArray = (SRI * *) molecules[i].getMyBonds(); |
74 |
|
for (int j = 0; j < molecules[i].getNBonds(); j++){ |
75 |
|
constrained = theArray[j]->is_constrained(); |
76 |
|
|
116 |
|
} |
117 |
|
} |
118 |
|
|
119 |
+ |
|
120 |
|
if (nConstrained > 0){ |
121 |
|
isConstrained = 1; |
122 |
|
|
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
< |
// save oldAtoms to check for lode balanceing later on. |
141 |
> |
// save oldAtoms to check for lode balancing later on. |
142 |
|
|
143 |
|
oldAtoms = nAtoms; |
144 |
|
|
153 |
|
|
154 |
|
|
155 |
|
template<typename T> void Integrator<T>::integrate(void){ |
150 |
– |
int i, j; // loop counters |
156 |
|
|
157 |
|
double runTime = info->run_time; |
158 |
|
double sampleTime = info->sampleTime; |
165 |
|
double currThermal; |
166 |
|
double currStatus; |
167 |
|
double currReset; |
168 |
< |
|
168 |
> |
|
169 |
|
int calcPot, calcStress; |
165 |
– |
int isError; |
170 |
|
|
171 |
|
tStats = new Thermo(info); |
172 |
|
statOut = new StatWriter(info); |
173 |
|
dumpOut = new DumpWriter(info); |
174 |
|
|
175 |
|
atoms = info->atoms; |
172 |
– |
DirectionalAtom* dAtom; |
176 |
|
|
177 |
|
dt = info->dt; |
178 |
|
dt2 = 0.5 * dt; |
179 |
|
|
180 |
+ |
readyCheck(); |
181 |
+ |
|
182 |
|
// initialize the forces before the first step |
183 |
|
|
184 |
|
calcForce(1, 1); |
185 |
+ |
|
186 |
+ |
//temp test |
187 |
+ |
tStats->getPotential(); |
188 |
+ |
|
189 |
+ |
if (nConstrained){ |
190 |
+ |
preMove(); |
191 |
+ |
constrainA(); |
192 |
+ |
calcForce(1, 1); |
193 |
+ |
constrainB(); |
194 |
+ |
} |
195 |
|
|
196 |
|
if (info->setTemp){ |
197 |
|
thermalize(); |
207 |
|
dumpOut->writeDump(info->getTime()); |
208 |
|
statOut->writeStat(info->getTime()); |
209 |
|
|
195 |
– |
readyCheck(); |
210 |
|
|
211 |
|
#ifdef IS_MPI |
212 |
|
strcpy(checkPointMsg, "The integrator is ready to go."); |
219 |
|
calcStress = 1; |
220 |
|
} |
221 |
|
|
222 |
+ |
#ifdef PROFILE |
223 |
+ |
startProfile( pro1 ); |
224 |
+ |
#endif |
225 |
+ |
|
226 |
|
integrateStep(calcPot, calcStress); |
227 |
|
|
228 |
+ |
#ifdef PROFILE |
229 |
+ |
endProfile( pro1 ); |
230 |
+ |
|
231 |
+ |
startProfile( pro2 ); |
232 |
+ |
#endif // profile |
233 |
+ |
|
234 |
|
info->incrTime(dt); |
235 |
|
|
236 |
|
if (info->setTemp){ |
246 |
|
} |
247 |
|
|
248 |
|
if (info->getTime() >= currStatus){ |
249 |
< |
statOut->writeStat(info->getTime()); |
250 |
< |
calcPot = 0; |
249 |
> |
statOut->writeStat(info->getTime()); |
250 |
> |
calcPot = 0; |
251 |
|
calcStress = 0; |
252 |
|
currStatus += statusTime; |
253 |
< |
} |
253 |
> |
} |
254 |
|
|
255 |
|
if (info->resetIntegrator){ |
256 |
|
if (info->getTime() >= currReset){ |
258 |
|
currReset += resetTime; |
259 |
|
} |
260 |
|
} |
261 |
+ |
|
262 |
+ |
#ifdef PROFILE |
263 |
+ |
endProfile( pro2 ); |
264 |
+ |
#endif //profile |
265 |
|
|
266 |
|
#ifdef IS_MPI |
267 |
|
strcpy(checkPointMsg, "successfully took a time step."); |
269 |
|
#endif // is_mpi |
270 |
|
} |
271 |
|
|
244 |
– |
dumpOut->writeFinal(info->getTime()); |
245 |
– |
|
272 |
|
delete dumpOut; |
273 |
|
delete statOut; |
274 |
|
} |
276 |
|
template<typename T> void Integrator<T>::integrateStep(int calcPot, |
277 |
|
int calcStress){ |
278 |
|
// Position full step, and velocity half step |
279 |
+ |
|
280 |
+ |
#ifdef PROFILE |
281 |
+ |
startProfile(pro3); |
282 |
+ |
#endif //profile |
283 |
+ |
|
284 |
|
preMove(); |
285 |
+ |
|
286 |
+ |
#ifdef PROFILE |
287 |
+ |
endProfile(pro3); |
288 |
|
|
289 |
+ |
startProfile(pro4); |
290 |
+ |
#endif // profile |
291 |
+ |
|
292 |
|
moveA(); |
293 |
|
|
294 |
< |
if (nConstrained){ |
295 |
< |
constrainA(); |
296 |
< |
} |
294 |
> |
#ifdef PROFILE |
295 |
> |
endProfile(pro4); |
296 |
> |
|
297 |
> |
startProfile(pro5); |
298 |
> |
#endif//profile |
299 |
|
|
300 |
|
|
301 |
|
#ifdef IS_MPI |
313 |
|
MPIcheckPoint(); |
314 |
|
#endif // is_mpi |
315 |
|
|
316 |
+ |
#ifdef PROFILE |
317 |
+ |
endProfile( pro5 ); |
318 |
|
|
319 |
+ |
startProfile( pro6 ); |
320 |
+ |
#endif //profile |
321 |
+ |
|
322 |
|
// finish the velocity half step |
323 |
|
|
324 |
|
moveB(); |
325 |
|
|
326 |
< |
if (nConstrained){ |
327 |
< |
constrainB(); |
328 |
< |
} |
326 |
> |
#ifdef PROFILE |
327 |
> |
endProfile(pro6); |
328 |
> |
#endif // profile |
329 |
|
|
330 |
|
#ifdef IS_MPI |
331 |
|
strcpy(checkPointMsg, "Succesful moveB\n"); |
335 |
|
|
336 |
|
|
337 |
|
template<typename T> void Integrator<T>::moveA(void){ |
338 |
< |
int i, j; |
338 |
> |
size_t i, j; |
339 |
|
DirectionalAtom* dAtom; |
340 |
|
double Tb[3], ji[3]; |
297 |
– |
double A[3][3], I[3][3]; |
298 |
– |
double angle; |
341 |
|
double vel[3], pos[3], frc[3]; |
342 |
|
double mass; |
343 |
+ |
|
344 |
+ |
for (i = 0; i < integrableObjects.size() ; i++){ |
345 |
+ |
integrableObjects[i]->getVel(vel); |
346 |
+ |
integrableObjects[i]->getPos(pos); |
347 |
+ |
integrableObjects[i]->getFrc(frc); |
348 |
+ |
|
349 |
+ |
mass = integrableObjects[i]->getMass(); |
350 |
|
|
302 |
– |
for (i = 0; i < nAtoms; i++){ |
303 |
– |
atoms[i]->getVel(vel); |
304 |
– |
atoms[i]->getPos(pos); |
305 |
– |
atoms[i]->getFrc(frc); |
306 |
– |
|
307 |
– |
mass = atoms[i]->getMass(); |
308 |
– |
|
351 |
|
for (j = 0; j < 3; j++){ |
352 |
|
// velocity half step |
353 |
|
vel[j] += (dt2 * frc[j] / mass) * eConvert; |
355 |
|
pos[j] += dt * vel[j]; |
356 |
|
} |
357 |
|
|
358 |
< |
atoms[i]->setVel(vel); |
359 |
< |
atoms[i]->setPos(pos); |
358 |
> |
integrableObjects[i]->setVel(vel); |
359 |
> |
integrableObjects[i]->setPos(pos); |
360 |
|
|
361 |
< |
if (atoms[i]->isDirectional()){ |
320 |
< |
dAtom = (DirectionalAtom *) atoms[i]; |
361 |
> |
if (integrableObjects[i]->isDirectional()){ |
362 |
|
|
363 |
|
// get and convert the torque to body frame |
364 |
|
|
365 |
< |
dAtom->getTrq(Tb); |
366 |
< |
dAtom->lab2Body(Tb); |
365 |
> |
integrableObjects[i]->getTrq(Tb); |
366 |
> |
integrableObjects[i]->lab2Body(Tb); |
367 |
|
|
368 |
|
// get the angular momentum, and propagate a half step |
369 |
|
|
370 |
< |
dAtom->getJ(ji); |
370 |
> |
integrableObjects[i]->getJ(ji); |
371 |
|
|
372 |
|
for (j = 0; j < 3; j++) |
373 |
|
ji[j] += (dt2 * Tb[j]) * eConvert; |
374 |
|
|
375 |
< |
// use the angular velocities to propagate the rotation matrix a |
335 |
< |
// full time step |
375 |
> |
this->rotationPropagation( integrableObjects[i], ji ); |
376 |
|
|
377 |
< |
dAtom->getA(A); |
338 |
< |
dAtom->getI(I); |
339 |
< |
|
340 |
< |
// rotate about the x-axis |
341 |
< |
angle = dt2 * ji[0] / I[0][0]; |
342 |
< |
this->rotate(1, 2, angle, ji, A); |
343 |
< |
|
344 |
< |
// rotate about the y-axis |
345 |
< |
angle = dt2 * ji[1] / I[1][1]; |
346 |
< |
this->rotate(2, 0, angle, ji, A); |
347 |
< |
|
348 |
< |
// rotate about the z-axis |
349 |
< |
angle = dt * ji[2] / I[2][2]; |
350 |
< |
this->rotate(0, 1, angle, ji, A); |
351 |
< |
|
352 |
< |
// rotate about the y-axis |
353 |
< |
angle = dt2 * ji[1] / I[1][1]; |
354 |
< |
this->rotate(2, 0, angle, ji, A); |
355 |
< |
|
356 |
< |
// rotate about the x-axis |
357 |
< |
angle = dt2 * ji[0] / I[0][0]; |
358 |
< |
this->rotate(1, 2, angle, ji, A); |
359 |
< |
|
360 |
< |
|
361 |
< |
dAtom->setJ(ji); |
362 |
< |
dAtom->setA(A); |
377 |
> |
integrableObjects[i]->setJ(ji); |
378 |
|
} |
379 |
|
} |
380 |
+ |
|
381 |
+ |
if (nConstrained){ |
382 |
+ |
constrainA(); |
383 |
+ |
} |
384 |
|
} |
385 |
|
|
386 |
|
|
387 |
|
template<typename T> void Integrator<T>::moveB(void){ |
388 |
|
int i, j; |
370 |
– |
DirectionalAtom* dAtom; |
389 |
|
double Tb[3], ji[3]; |
390 |
|
double vel[3], frc[3]; |
391 |
|
double mass; |
392 |
|
|
393 |
< |
for (i = 0; i < nAtoms; i++){ |
394 |
< |
atoms[i]->getVel(vel); |
395 |
< |
atoms[i]->getFrc(frc); |
393 |
> |
for (i = 0; i < integrableObjects.size(); i++){ |
394 |
> |
integrableObjects[i]->getVel(vel); |
395 |
> |
integrableObjects[i]->getFrc(frc); |
396 |
|
|
397 |
< |
mass = atoms[i]->getMass(); |
397 |
> |
mass = integrableObjects[i]->getMass(); |
398 |
|
|
399 |
|
// velocity half step |
400 |
|
for (j = 0; j < 3; j++) |
401 |
|
vel[j] += (dt2 * frc[j] / mass) * eConvert; |
402 |
|
|
403 |
< |
atoms[i]->setVel(vel); |
403 |
> |
integrableObjects[i]->setVel(vel); |
404 |
|
|
405 |
< |
if (atoms[i]->isDirectional()){ |
388 |
< |
dAtom = (DirectionalAtom *) atoms[i]; |
405 |
> |
if (integrableObjects[i]->isDirectional()){ |
406 |
|
|
407 |
< |
// get and convert the torque to body frame |
407 |
> |
// get and convert the torque to body frame |
408 |
|
|
409 |
< |
dAtom->getTrq(Tb); |
410 |
< |
dAtom->lab2Body(Tb); |
409 |
> |
integrableObjects[i]->getTrq(Tb); |
410 |
> |
integrableObjects[i]->lab2Body(Tb); |
411 |
|
|
412 |
|
// get the angular momentum, and propagate a half step |
413 |
|
|
414 |
< |
dAtom->getJ(ji); |
414 |
> |
integrableObjects[i]->getJ(ji); |
415 |
|
|
416 |
|
for (j = 0; j < 3; j++) |
417 |
|
ji[j] += (dt2 * Tb[j]) * eConvert; |
418 |
|
|
419 |
|
|
420 |
< |
dAtom->setJ(ji); |
420 |
> |
integrableObjects[i]->setJ(ji); |
421 |
|
} |
422 |
|
} |
423 |
+ |
|
424 |
+ |
if (nConstrained){ |
425 |
+ |
constrainB(); |
426 |
+ |
} |
427 |
|
} |
428 |
|
|
429 |
|
template<typename T> void Integrator<T>::preMove(void){ |
442 |
|
} |
443 |
|
|
444 |
|
template<typename T> void Integrator<T>::constrainA(){ |
445 |
< |
int i, j, k; |
445 |
> |
int i, j; |
446 |
|
int done; |
447 |
|
double posA[3], posB[3]; |
448 |
|
double velA[3], velB[3]; |
582 |
|
painCave.isFatal = 1; |
583 |
|
simError(); |
584 |
|
} |
585 |
+ |
|
586 |
|
} |
587 |
|
|
588 |
|
template<typename T> void Integrator<T>::constrainB(void){ |
589 |
< |
int i, j, k; |
589 |
> |
int i, j; |
590 |
|
int done; |
591 |
|
double posA[3], posB[3]; |
592 |
|
double velA[3], velB[3]; |
595 |
|
int a, b, ax, ay, az, bx, by, bz; |
596 |
|
double rma, rmb; |
597 |
|
double dx, dy, dz; |
598 |
< |
double rabsq, pabsq, rvab; |
577 |
< |
double diffsq; |
598 |
> |
double rvab; |
599 |
|
double gab; |
600 |
|
int iteration; |
601 |
|
|
685 |
|
} |
686 |
|
} |
687 |
|
|
688 |
+ |
template<typename T> void Integrator<T>::rotationPropagation |
689 |
+ |
( StuntDouble* sd, double ji[3] ){ |
690 |
+ |
|
691 |
+ |
double angle; |
692 |
+ |
double A[3][3], I[3][3]; |
693 |
+ |
|
694 |
+ |
// use the angular velocities to propagate the rotation matrix a |
695 |
+ |
// full time step |
696 |
+ |
|
697 |
+ |
sd->getA(A); |
698 |
+ |
sd->getI(I); |
699 |
+ |
|
700 |
+ |
// rotate about the x-axis |
701 |
+ |
angle = dt2 * ji[0] / I[0][0]; |
702 |
+ |
this->rotate( 1, 2, angle, ji, A ); |
703 |
+ |
|
704 |
+ |
// rotate about the y-axis |
705 |
+ |
angle = dt2 * ji[1] / I[1][1]; |
706 |
+ |
this->rotate( 2, 0, angle, ji, A ); |
707 |
+ |
|
708 |
+ |
// rotate about the z-axis |
709 |
+ |
angle = dt * ji[2] / I[2][2]; |
710 |
+ |
this->rotate( 0, 1, angle, ji, A); |
711 |
+ |
|
712 |
+ |
// rotate about the y-axis |
713 |
+ |
angle = dt2 * ji[1] / I[1][1]; |
714 |
+ |
this->rotate( 2, 0, angle, ji, A ); |
715 |
+ |
|
716 |
+ |
// rotate about the x-axis |
717 |
+ |
angle = dt2 * ji[0] / I[0][0]; |
718 |
+ |
this->rotate( 1, 2, angle, ji, A ); |
719 |
+ |
|
720 |
+ |
sd->setA( A ); |
721 |
+ |
} |
722 |
+ |
|
723 |
|
template<typename T> void Integrator<T>::rotate(int axes1, int axes2, |
724 |
|
double angle, double ji[3], |
725 |
|
double A[3][3]){ |
785 |
|
} |
786 |
|
} |
787 |
|
|
788 |
< |
// rotate the Rotation matrix acording to: |
788 |
> |
// rotate the Rotation matrix acording to: |
789 |
|
// A[][] = A[][] * transpose(rot[][]) |
790 |
|
|
791 |
|
|
810 |
|
template<typename T> void Integrator<T>::thermalize(){ |
811 |
|
tStats->velocitize(); |
812 |
|
} |
813 |
+ |
|
814 |
+ |
template<typename T> double Integrator<T>::getConservedQuantity(void){ |
815 |
+ |
return tStats->getTotalE(); |
816 |
+ |
} |
817 |
+ |
template<typename T> string Integrator<T>::getAdditionalParameters(void){ |
818 |
+ |
//By default, return a null string |
819 |
+ |
//The reason we use string instead of char* is that if we use char*, we will |
820 |
+ |
//return a pointer point to local variable which might cause problem |
821 |
+ |
return string(); |
822 |
+ |
} |