ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Integrator.cpp
Revision: 637
Committed: Thu Jul 17 21:50:01 2003 UTC (20 years, 11 months ago) by gezelter
File size: 15197 byte(s)
Log Message:
Started work on a DumpReader

File Contents

# Content
1 #include <iostream>
2 #include <cstdlib>
3 #include <cmath>
4
5 #ifdef IS_MPI
6 #include "mpiSimulation.hpp"
7 #include <unistd.h>
8 #endif //is_mpi
9
10 #include "Integrator.hpp"
11 #include "simError.h"
12
13
14 Integrator::Integrator( SimInfo *theInfo, ForceFields* the_ff ){
15
16 info = theInfo;
17 myFF = the_ff;
18 isFirst = 1;
19
20 molecules = info->molecules;
21 nMols = info->n_mol;
22
23 // give a little love back to the SimInfo object
24
25 if( info->the_integrator != NULL ) delete info->the_integrator;
26 info->the_integrator = this;
27
28 nAtoms = info->n_atoms;
29
30 // check for constraints
31
32 constrainedA = NULL;
33 constrainedB = NULL;
34 constrainedDsqr = NULL;
35 moving = NULL;
36 moved = NULL;
37 oldPos = NULL;
38
39 nConstrained = 0;
40
41 checkConstraints();
42 }
43
44 Integrator::~Integrator() {
45
46 if( nConstrained ){
47 delete[] constrainedA;
48 delete[] constrainedB;
49 delete[] constrainedDsqr;
50 delete[] moving;
51 delete[] moved;
52 delete[] oldPos;
53 }
54
55 }
56
57 void Integrator::checkConstraints( void ){
58
59
60 isConstrained = 0;
61
62 Constraint *temp_con;
63 Constraint *dummy_plug;
64 temp_con = new Constraint[info->n_SRI];
65 nConstrained = 0;
66 int constrained = 0;
67
68 SRI** theArray;
69 for(int i = 0; i < nMols; i++){
70
71 theArray = (SRI**) molecules[i].getMyBonds();
72 for(int j=0; j<molecules[i].getNBonds(); j++){
73
74 constrained = theArray[j]->is_constrained();
75
76 if(constrained){
77
78 dummy_plug = theArray[j]->get_constraint();
79 temp_con[nConstrained].set_a( dummy_plug->get_a() );
80 temp_con[nConstrained].set_b( dummy_plug->get_b() );
81 temp_con[nConstrained].set_dsqr( dummy_plug->get_dsqr() );
82
83 nConstrained++;
84 constrained = 0;
85 }
86 }
87
88 theArray = (SRI**) molecules[i].getMyBends();
89 for(int j=0; j<molecules[i].getNBends(); j++){
90
91 constrained = theArray[j]->is_constrained();
92
93 if(constrained){
94
95 dummy_plug = theArray[j]->get_constraint();
96 temp_con[nConstrained].set_a( dummy_plug->get_a() );
97 temp_con[nConstrained].set_b( dummy_plug->get_b() );
98 temp_con[nConstrained].set_dsqr( dummy_plug->get_dsqr() );
99
100 nConstrained++;
101 constrained = 0;
102 }
103 }
104
105 theArray = (SRI**) molecules[i].getMyTorsions();
106 for(int j=0; j<molecules[i].getNTorsions(); j++){
107
108 constrained = theArray[j]->is_constrained();
109
110 if(constrained){
111
112 dummy_plug = theArray[j]->get_constraint();
113 temp_con[nConstrained].set_a( dummy_plug->get_a() );
114 temp_con[nConstrained].set_b( dummy_plug->get_b() );
115 temp_con[nConstrained].set_dsqr( dummy_plug->get_dsqr() );
116
117 nConstrained++;
118 constrained = 0;
119 }
120 }
121 }
122
123 if(nConstrained > 0){
124
125 isConstrained = 1;
126
127 if(constrainedA != NULL ) delete[] constrainedA;
128 if(constrainedB != NULL ) delete[] constrainedB;
129 if(constrainedDsqr != NULL ) delete[] constrainedDsqr;
130
131 constrainedA = new int[nConstrained];
132 constrainedB = new int[nConstrained];
133 constrainedDsqr = new double[nConstrained];
134
135 for( int i = 0; i < nConstrained; i++){
136
137 constrainedA[i] = temp_con[i].get_a();
138 constrainedB[i] = temp_con[i].get_b();
139 constrainedDsqr[i] = temp_con[i].get_dsqr();
140
141 }
142
143
144 // save oldAtoms to check for lode balanceing later on.
145
146 oldAtoms = nAtoms;
147
148 moving = new int[nAtoms];
149 moved = new int[nAtoms];
150
151 oldPos = new double[nAtoms*3];
152 }
153
154 delete[] temp_con;
155 }
156
157
158 void Integrator::integrate( void ){
159
160 int i, j; // loop counters
161
162 double runTime = info->run_time;
163 double sampleTime = info->sampleTime;
164 double statusTime = info->statusTime;
165 double thermalTime = info->thermalTime;
166
167 double currSample;
168 double currThermal;
169 double currStatus;
170 double currTime;
171
172 int calcPot, calcStress;
173 int isError;
174
175 tStats = new Thermo( info );
176 statOut = new StatWriter( info );
177 dumpOut = new DumpWriter( info );
178
179 atoms = info->atoms;
180 DirectionalAtom* dAtom;
181
182 dt = info->dt;
183 dt2 = 0.5 * dt;
184
185 // initialize the forces before the first step
186
187 myFF->doForces(1,1);
188
189 if( info->setTemp ){
190
191 tStats->velocitize();
192 }
193
194 dumpOut->writeDump( 0.0 );
195 statOut->writeStat( 0.0 );
196
197 calcPot = 0;
198 calcStress = 0;
199 currSample = sampleTime;
200 currThermal = thermalTime;
201 currStatus = statusTime;
202 currTime = 0.0;;
203
204
205 readyCheck();
206
207 #ifdef IS_MPI
208 strcpy( checkPointMsg,
209 "The integrator is ready to go." );
210 MPIcheckPoint();
211 #endif // is_mpi
212
213 while( currTime < runTime ){
214
215 if( (currTime+dt) >= currStatus ){
216 calcPot = 1;
217 calcStress = 1;
218 }
219
220 integrateStep( calcPot, calcStress );
221
222 currTime += dt;
223 info->setTime(currTime);
224
225 if( info->setTemp ){
226 if( currTime >= currThermal ){
227 tStats->velocitize();
228 currThermal += thermalTime;
229 }
230 }
231
232 if( currTime >= currSample ){
233 dumpOut->writeDump( currTime );
234 currSample += sampleTime;
235 }
236
237 if( currTime >= currStatus ){
238 statOut->writeStat( currTime );
239 calcPot = 0;
240 calcStress = 0;
241 currStatus += statusTime;
242 }
243
244 #ifdef IS_MPI
245 strcpy( checkPointMsg,
246 "successfully took a time step." );
247 MPIcheckPoint();
248 #endif // is_mpi
249
250 }
251
252 dumpOut->writeFinal(currTime);
253
254 delete dumpOut;
255 delete statOut;
256 }
257
258 void Integrator::integrateStep( int calcPot, int calcStress ){
259
260
261
262 // Position full step, and velocity half step
263
264 preMove();
265 moveA();
266 if( nConstrained ) constrainA();
267
268
269 #ifdef IS_MPI
270 strcpy( checkPointMsg, "Succesful moveA\n" );
271 MPIcheckPoint();
272 #endif // is_mpi
273
274
275 // calc forces
276
277 myFF->doForces(calcPot,calcStress);
278
279 #ifdef IS_MPI
280 strcpy( checkPointMsg, "Succesful doForces\n" );
281 MPIcheckPoint();
282 #endif // is_mpi
283
284
285 // finish the velocity half step
286
287 moveB();
288 if( nConstrained ) constrainB();
289
290 #ifdef IS_MPI
291 strcpy( checkPointMsg, "Succesful moveB\n" );
292 MPIcheckPoint();
293 #endif // is_mpi
294
295
296 }
297
298
299 void Integrator::moveA( void ){
300
301 int i, j;
302 DirectionalAtom* dAtom;
303 double Tb[3], ji[3];
304 double A[3][3], I[3][3];
305 double angle;
306 double vel[3], pos[3], frc[3];
307 double mass;
308
309 for( i=0; i<nAtoms; i++ ){
310
311 atoms[i]->getVel( vel );
312 atoms[i]->getPos( pos );
313 atoms[i]->getFrc( frc );
314
315 mass = atoms[i]->getMass();
316
317 for (j=0; j < 3; j++) {
318 // velocity half step
319 vel[j] += ( dt2 * frc[j] / mass ) * eConvert;
320 // position whole step
321 pos[j] += dt * vel[j];
322 }
323
324 atoms[i]->setVel( vel );
325 atoms[i]->setPos( pos );
326
327 if( atoms[i]->isDirectional() ){
328
329 dAtom = (DirectionalAtom *)atoms[i];
330
331 // get and convert the torque to body frame
332
333 dAtom->getTrq( Tb );
334 dAtom->lab2Body( Tb );
335
336 // get the angular momentum, and propagate a half step
337
338 dAtom->getJ( ji );
339
340 for (j=0; j < 3; j++)
341 ji[j] += (dt2 * Tb[j]) * eConvert;
342
343 // use the angular velocities to propagate the rotation matrix a
344 // full time step
345
346 dAtom->getA(A);
347 dAtom->getI(I);
348
349 // rotate about the x-axis
350 angle = dt2 * ji[0] / I[0][0];
351 this->rotate( 1, 2, angle, ji, A );
352
353 // rotate about the y-axis
354 angle = dt2 * ji[1] / I[1][1];
355 this->rotate( 2, 0, angle, ji, A );
356
357 // rotate about the z-axis
358 angle = dt * ji[2] / I[2][2];
359 this->rotate( 0, 1, angle, ji, A);
360
361 // rotate about the y-axis
362 angle = dt2 * ji[1] / I[1][1];
363 this->rotate( 2, 0, angle, ji, A );
364
365 // rotate about the x-axis
366 angle = dt2 * ji[0] / I[0][0];
367 this->rotate( 1, 2, angle, ji, A );
368
369
370 dAtom->setJ( ji );
371 dAtom->setA( A );
372
373 }
374 }
375 }
376
377
378 void Integrator::moveB( void ){
379 int i, j;
380 DirectionalAtom* dAtom;
381 double Tb[3], ji[3];
382 double vel[3], frc[3];
383 double mass;
384
385 for( i=0; i<nAtoms; i++ ){
386
387 atoms[i]->getVel( vel );
388 atoms[i]->getFrc( frc );
389
390 mass = atoms[i]->getMass();
391
392 // velocity half step
393 for (j=0; j < 3; j++)
394 vel[j] += ( dt2 * frc[j] / mass ) * eConvert;
395
396 atoms[i]->setVel( vel );
397
398 if( atoms[i]->isDirectional() ){
399
400 dAtom = (DirectionalAtom *)atoms[i];
401
402 // get and convert the torque to body frame
403
404 dAtom->getTrq( Tb );
405 dAtom->lab2Body( Tb );
406
407 // get the angular momentum, and propagate a half step
408
409 dAtom->getJ( ji );
410
411 for (j=0; j < 3; j++)
412 ji[j] += (dt2 * Tb[j]) * eConvert;
413
414
415 dAtom->setJ( ji );
416 }
417 }
418 }
419
420 void Integrator::preMove( void ){
421 int i, j;
422 double pos[3];
423
424 if( nConstrained ){
425
426 for(i=0; i < nAtoms; i++) {
427
428 atoms[i]->getPos( pos );
429
430 for (j = 0; j < 3; j++) {
431 oldPos[3*i + j] = pos[j];
432 }
433
434 }
435 }
436 }
437
438 void Integrator::constrainA(){
439
440 int i,j,k;
441 int done;
442 double posA[3], posB[3];
443 double velA[3], velB[3];
444 double pab[3];
445 double rab[3];
446 int a, b, ax, ay, az, bx, by, bz;
447 double rma, rmb;
448 double dx, dy, dz;
449 double rpab;
450 double rabsq, pabsq, rpabsq;
451 double diffsq;
452 double gab;
453 int iteration;
454
455 for( i=0; i<nAtoms; i++){
456 moving[i] = 0;
457 moved[i] = 1;
458 }
459
460 iteration = 0;
461 done = 0;
462 while( !done && (iteration < maxIteration )){
463
464 done = 1;
465 for(i=0; i<nConstrained; i++){
466
467 a = constrainedA[i];
468 b = constrainedB[i];
469
470 ax = (a*3) + 0;
471 ay = (a*3) + 1;
472 az = (a*3) + 2;
473
474 bx = (b*3) + 0;
475 by = (b*3) + 1;
476 bz = (b*3) + 2;
477
478 if( moved[a] || moved[b] ){
479
480 atoms[a]->getPos( posA );
481 atoms[b]->getPos( posB );
482
483 for (j = 0; j < 3; j++ )
484 pab[j] = posA[j] - posB[j];
485
486 //periodic boundary condition
487
488 info->wrapVector( pab );
489
490 pabsq = pab[0] * pab[0] + pab[1] * pab[1] + pab[2] * pab[2];
491
492 rabsq = constrainedDsqr[i];
493 diffsq = rabsq - pabsq;
494
495 // the original rattle code from alan tidesley
496 if (fabs(diffsq) > (tol*rabsq*2)) {
497 rab[0] = oldPos[ax] - oldPos[bx];
498 rab[1] = oldPos[ay] - oldPos[by];
499 rab[2] = oldPos[az] - oldPos[bz];
500
501 info->wrapVector( rab );
502
503 rpab = rab[0] * pab[0] + rab[1] * pab[1] + rab[2] * pab[2];
504
505 rpabsq = rpab * rpab;
506
507
508 if (rpabsq < (rabsq * -diffsq)){
509
510 #ifdef IS_MPI
511 a = atoms[a]->getGlobalIndex();
512 b = atoms[b]->getGlobalIndex();
513 #endif //is_mpi
514 sprintf( painCave.errMsg,
515 "Constraint failure in constrainA at atom %d and %d.\n",
516 a, b );
517 painCave.isFatal = 1;
518 simError();
519 }
520
521 rma = 1.0 / atoms[a]->getMass();
522 rmb = 1.0 / atoms[b]->getMass();
523
524 gab = diffsq / ( 2.0 * ( rma + rmb ) * rpab );
525
526 dx = rab[0] * gab;
527 dy = rab[1] * gab;
528 dz = rab[2] * gab;
529
530 posA[0] += rma * dx;
531 posA[1] += rma * dy;
532 posA[2] += rma * dz;
533
534 atoms[a]->setPos( posA );
535
536 posB[0] -= rmb * dx;
537 posB[1] -= rmb * dy;
538 posB[2] -= rmb * dz;
539
540 atoms[b]->setPos( posB );
541
542 dx = dx / dt;
543 dy = dy / dt;
544 dz = dz / dt;
545
546 atoms[a]->getVel( velA );
547
548 velA[0] += rma * dx;
549 velA[1] += rma * dy;
550 velA[2] += rma * dz;
551
552 atoms[a]->setVel( velA );
553
554 atoms[b]->getVel( velB );
555
556 velB[0] -= rmb * dx;
557 velB[1] -= rmb * dy;
558 velB[2] -= rmb * dz;
559
560 atoms[b]->setVel( velB );
561
562 moving[a] = 1;
563 moving[b] = 1;
564 done = 0;
565 }
566 }
567 }
568
569 for(i=0; i<nAtoms; i++){
570
571 moved[i] = moving[i];
572 moving[i] = 0;
573 }
574
575 iteration++;
576 }
577
578 if( !done ){
579
580 sprintf( painCave.errMsg,
581 "Constraint failure in constrainA, too many iterations: %d\n",
582 iteration );
583 painCave.isFatal = 1;
584 simError();
585 }
586
587 }
588
589 void Integrator::constrainB( void ){
590
591 int i,j,k;
592 int done;
593 double posA[3], posB[3];
594 double velA[3], velB[3];
595 double vxab, vyab, vzab;
596 double rab[3];
597 int a, b, ax, ay, az, bx, by, bz;
598 double rma, rmb;
599 double dx, dy, dz;
600 double rabsq, pabsq, rvab;
601 double diffsq;
602 double gab;
603 int iteration;
604
605 for(i=0; i<nAtoms; i++){
606 moving[i] = 0;
607 moved[i] = 1;
608 }
609
610 done = 0;
611 iteration = 0;
612 while( !done && (iteration < maxIteration ) ){
613
614 done = 1;
615
616 for(i=0; i<nConstrained; i++){
617
618 a = constrainedA[i];
619 b = constrainedB[i];
620
621 ax = (a*3) + 0;
622 ay = (a*3) + 1;
623 az = (a*3) + 2;
624
625 bx = (b*3) + 0;
626 by = (b*3) + 1;
627 bz = (b*3) + 2;
628
629 if( moved[a] || moved[b] ){
630
631 atoms[a]->getVel( velA );
632 atoms[b]->getVel( velB );
633
634 vxab = velA[0] - velB[0];
635 vyab = velA[1] - velB[1];
636 vzab = velA[2] - velB[2];
637
638 atoms[a]->getPos( posA );
639 atoms[b]->getPos( posB );
640
641 for (j = 0; j < 3; j++)
642 rab[j] = posA[j] - posB[j];
643
644 info->wrapVector( rab );
645
646 rma = 1.0 / atoms[a]->getMass();
647 rmb = 1.0 / atoms[b]->getMass();
648
649 rvab = rab[0] * vxab + rab[1] * vyab + rab[2] * vzab;
650
651 gab = -rvab / ( ( rma + rmb ) * constrainedDsqr[i] );
652
653 if (fabs(gab) > tol) {
654
655 dx = rab[0] * gab;
656 dy = rab[1] * gab;
657 dz = rab[2] * gab;
658
659 velA[0] += rma * dx;
660 velA[1] += rma * dy;
661 velA[2] += rma * dz;
662
663 atoms[a]->setVel( velA );
664
665 velB[0] -= rmb * dx;
666 velB[1] -= rmb * dy;
667 velB[2] -= rmb * dz;
668
669 atoms[b]->setVel( velB );
670
671 moving[a] = 1;
672 moving[b] = 1;
673 done = 0;
674 }
675 }
676 }
677
678 for(i=0; i<nAtoms; i++){
679 moved[i] = moving[i];
680 moving[i] = 0;
681 }
682
683 iteration++;
684 }
685
686 if( !done ){
687
688
689 sprintf( painCave.errMsg,
690 "Constraint failure in constrainB, too many iterations: %d\n",
691 iteration );
692 painCave.isFatal = 1;
693 simError();
694 }
695
696 }
697
698 void Integrator::rotate( int axes1, int axes2, double angle, double ji[3],
699 double A[3][3] ){
700
701 int i,j,k;
702 double sinAngle;
703 double cosAngle;
704 double angleSqr;
705 double angleSqrOver4;
706 double top, bottom;
707 double rot[3][3];
708 double tempA[3][3];
709 double tempJ[3];
710
711 // initialize the tempA
712
713 for(i=0; i<3; i++){
714 for(j=0; j<3; j++){
715 tempA[j][i] = A[i][j];
716 }
717 }
718
719 // initialize the tempJ
720
721 for( i=0; i<3; i++) tempJ[i] = ji[i];
722
723 // initalize rot as a unit matrix
724
725 rot[0][0] = 1.0;
726 rot[0][1] = 0.0;
727 rot[0][2] = 0.0;
728
729 rot[1][0] = 0.0;
730 rot[1][1] = 1.0;
731 rot[1][2] = 0.0;
732
733 rot[2][0] = 0.0;
734 rot[2][1] = 0.0;
735 rot[2][2] = 1.0;
736
737 // use a small angle aproximation for sin and cosine
738
739 angleSqr = angle * angle;
740 angleSqrOver4 = angleSqr / 4.0;
741 top = 1.0 - angleSqrOver4;
742 bottom = 1.0 + angleSqrOver4;
743
744 cosAngle = top / bottom;
745 sinAngle = angle / bottom;
746
747 rot[axes1][axes1] = cosAngle;
748 rot[axes2][axes2] = cosAngle;
749
750 rot[axes1][axes2] = sinAngle;
751 rot[axes2][axes1] = -sinAngle;
752
753 // rotate the momentum acoording to: ji[] = rot[][] * ji[]
754
755 for(i=0; i<3; i++){
756 ji[i] = 0.0;
757 for(k=0; k<3; k++){
758 ji[i] += rot[i][k] * tempJ[k];
759 }
760 }
761
762 // rotate the Rotation matrix acording to:
763 // A[][] = A[][] * transpose(rot[][])
764
765
766 // NOte for as yet unknown reason, we are performing the
767 // calculation as:
768 // transpose(A[][]) = transpose(A[][]) * transpose(rot[][])
769
770 for(i=0; i<3; i++){
771 for(j=0; j<3; j++){
772 A[j][i] = 0.0;
773 for(k=0; k<3; k++){
774 A[j][i] += tempA[i][k] * rot[j][k];
775 }
776 }
777 }
778 }