ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Integrator.cpp
Revision: 614
Committed: Tue Jul 15 17:57:04 2003 UTC (20 years, 11 months ago) by mmeineke
File size: 15168 byte(s)
Log Message:
fixed some bugs, Changed entry_plug to info where appropriate

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
224 if( info->setTemp ){
225 if( currTime >= currThermal ){
226 tStats->velocitize();
227 currThermal += thermalTime;
228 }
229 }
230
231 if( currTime >= currSample ){
232 dumpOut->writeDump( currTime );
233 currSample += sampleTime;
234 }
235
236 if( currTime >= currStatus ){
237 statOut->writeStat( currTime );
238 calcPot = 0;
239 calcStress = 0;
240 currStatus += statusTime;
241 }
242
243 #ifdef IS_MPI
244 strcpy( checkPointMsg,
245 "successfully took a time step." );
246 MPIcheckPoint();
247 #endif // is_mpi
248
249 }
250
251 dumpOut->writeFinal(currTime);
252
253 delete dumpOut;
254 delete statOut;
255 }
256
257 void Integrator::integrateStep( int calcPot, int calcStress ){
258
259
260
261 // Position full step, and velocity half step
262
263 preMove();
264 moveA();
265 if( nConstrained ) constrainA();
266
267
268 #ifdef IS_MPI
269 strcpy( checkPointMsg, "Succesful moveA\n" );
270 MPIcheckPoint();
271 #endif // is_mpi
272
273
274 // calc forces
275
276 myFF->doForces(calcPot,calcStress);
277
278 #ifdef IS_MPI
279 strcpy( checkPointMsg, "Succesful doForces\n" );
280 MPIcheckPoint();
281 #endif // is_mpi
282
283
284 // finish the velocity half step
285
286 moveB();
287 if( nConstrained ) constrainB();
288
289 #ifdef IS_MPI
290 strcpy( checkPointMsg, "Succesful moveB\n" );
291 MPIcheckPoint();
292 #endif // is_mpi
293
294
295 }
296
297
298 void Integrator::moveA( void ){
299
300 int i, j;
301 DirectionalAtom* dAtom;
302 double Tb[3], ji[3];
303 double A[3][3], I[3][3];
304 double angle;
305 double vel[3], pos[3], frc[3];
306 double mass;
307
308 for( i=0; i<nAtoms; i++ ){
309
310 atoms[i]->getVel( vel );
311 atoms[i]->getPos( pos );
312 atoms[i]->getFrc( frc );
313
314 mass = atoms[i]->getMass();
315
316 for (j=0; j < 3; j++) {
317 // velocity half step
318 vel[j] += ( dt2 * frc[j] / mass ) * eConvert;
319 // position whole step
320 pos[j] += dt * vel[j];
321 }
322
323 atoms[i]->setVel( vel );
324 atoms[i]->setPos( pos );
325
326 if( atoms[i]->isDirectional() ){
327
328 dAtom = (DirectionalAtom *)atoms[i];
329
330 // get and convert the torque to body frame
331
332 dAtom->getTrq( Tb );
333 dAtom->lab2Body( Tb );
334
335 // get the angular momentum, and propagate a half step
336
337 dAtom->getJ( ji );
338
339 for (j=0; j < 3; j++)
340 ji[j] += (dt2 * Tb[j]) * eConvert;
341
342 // use the angular velocities to propagate the rotation matrix a
343 // full time step
344
345 dAtom->getA(A);
346 dAtom->getI(I);
347
348 // rotate about the x-axis
349 angle = dt2 * ji[0] / I[0][0];
350 this->rotate( 1, 2, 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 z-axis
357 angle = dt * ji[2] / I[2][2];
358 this->rotate( 0, 1, angle, ji, A);
359
360 // rotate about the y-axis
361 angle = dt2 * ji[1] / I[1][1];
362 this->rotate( 2, 0, angle, ji, A );
363
364 // rotate about the x-axis
365 angle = dt2 * ji[0] / I[0][0];
366 this->rotate( 1, 2, angle, ji, A );
367
368
369 dAtom->setJ( ji );
370 dAtom->setA( A );
371
372 }
373 }
374 }
375
376
377 void Integrator::moveB( void ){
378 int i, j;
379 DirectionalAtom* dAtom;
380 double Tb[3], ji[3];
381 double vel[3], frc[3];
382 double mass;
383
384 for( i=0; i<nAtoms; i++ ){
385
386 atoms[i]->getVel( vel );
387 atoms[i]->getFrc( frc );
388
389 mass = atoms[i]->getMass();
390
391 // velocity half step
392 for (j=0; j < 3; j++)
393 vel[j] += ( dt2 * frc[j] / mass ) * eConvert;
394
395 atoms[i]->setVel( vel );
396
397 if( atoms[i]->isDirectional() ){
398
399 dAtom = (DirectionalAtom *)atoms[i];
400
401 // get and convert the torque to body frame
402
403 dAtom->getTrq( Tb );
404 dAtom->lab2Body( Tb );
405
406 // get the angular momentum, and propagate a half step
407
408 dAtom->getJ( ji );
409
410 for (j=0; j < 3; j++)
411 ji[j] += (dt2 * Tb[j]) * eConvert;
412
413
414 dAtom->setJ( ji );
415 }
416 }
417 }
418
419 void Integrator::preMove( void ){
420 int i, j;
421 double pos[3];
422
423 if( nConstrained ){
424
425 for(i=0; i < nAtoms; i++) {
426
427 atoms[i]->getPos( pos );
428
429 for (j = 0; j < 3; j++) {
430 oldPos[3*i + j] = pos[j];
431 }
432
433 }
434 }
435 }
436
437 void Integrator::constrainA(){
438
439 int i,j,k;
440 int done;
441 double posA[3], posB[3];
442 double velA[3], velB[3];
443 double pab[3];
444 double rab[3];
445 int a, b, ax, ay, az, bx, by, bz;
446 double rma, rmb;
447 double dx, dy, dz;
448 double rpab;
449 double rabsq, pabsq, rpabsq;
450 double diffsq;
451 double gab;
452 int iteration;
453
454 for( i=0; i<nAtoms; i++){
455 moving[i] = 0;
456 moved[i] = 1;
457 }
458
459 iteration = 0;
460 done = 0;
461 while( !done && (iteration < maxIteration )){
462
463 done = 1;
464 for(i=0; i<nConstrained; i++){
465
466 a = constrainedA[i];
467 b = constrainedB[i];
468
469 ax = (a*3) + 0;
470 ay = (a*3) + 1;
471 az = (a*3) + 2;
472
473 bx = (b*3) + 0;
474 by = (b*3) + 1;
475 bz = (b*3) + 2;
476
477 if( moved[a] || moved[b] ){
478
479 atoms[a]->getPos( posA );
480 atoms[b]->getPos( posB );
481
482 for (j = 0; j < 3; j++ )
483 pab[j] = posA[j] - posB[j];
484
485 //periodic boundary condition
486
487 info->wrapVector( pab );
488
489 pabsq = pab[0] * pab[0] + pab[1] * pab[1] + pab[2] * pab[2];
490
491 rabsq = constrainedDsqr[i];
492 diffsq = rabsq - pabsq;
493
494 // the original rattle code from alan tidesley
495 if (fabs(diffsq) > (tol*rabsq*2)) {
496 rab[0] = oldPos[ax] - oldPos[bx];
497 rab[1] = oldPos[ay] - oldPos[by];
498 rab[2] = oldPos[az] - oldPos[bz];
499
500 info->wrapVector( rab );
501
502 rpab = rab[0] * pab[0] + rab[1] * pab[1] + rab[2] * pab[2];
503
504 rpabsq = rpab * rpab;
505
506
507 if (rpabsq < (rabsq * -diffsq)){
508
509 #ifdef IS_MPI
510 a = atoms[a]->getGlobalIndex();
511 b = atoms[b]->getGlobalIndex();
512 #endif //is_mpi
513 sprintf( painCave.errMsg,
514 "Constraint failure in constrainA at atom %d and %d.\n",
515 a, b );
516 painCave.isFatal = 1;
517 simError();
518 }
519
520 rma = 1.0 / atoms[a]->getMass();
521 rmb = 1.0 / atoms[b]->getMass();
522
523 gab = diffsq / ( 2.0 * ( rma + rmb ) * rpab );
524
525 dx = rab[0] * gab;
526 dy = rab[1] * gab;
527 dz = rab[2] * gab;
528
529 posA[0] += rma * dx;
530 posA[1] += rma * dy;
531 posA[2] += rma * dz;
532
533 atoms[a]->setPos( posA );
534
535 posB[0] -= rmb * dx;
536 posB[1] -= rmb * dy;
537 posB[2] -= rmb * dz;
538
539 atoms[b]->setPos( posB );
540
541 dx = dx / dt;
542 dy = dy / dt;
543 dz = dz / dt;
544
545 atoms[a]->getVel( velA );
546
547 velA[0] += rma * dx;
548 velA[1] += rma * dy;
549 velA[2] += rma * dz;
550
551 atoms[a]->setVel( velA );
552
553 atoms[b]->getVel( velB );
554
555 velB[0] -= rmb * dx;
556 velB[1] -= rmb * dy;
557 velB[2] -= rmb * dz;
558
559 atoms[b]->setVel( velB );
560
561 moving[a] = 1;
562 moving[b] = 1;
563 done = 0;
564 }
565 }
566 }
567
568 for(i=0; i<nAtoms; i++){
569
570 moved[i] = moving[i];
571 moving[i] = 0;
572 }
573
574 iteration++;
575 }
576
577 if( !done ){
578
579 sprintf( painCave.errMsg,
580 "Constraint failure in constrainA, too many iterations: %d\n",
581 iteration );
582 painCave.isFatal = 1;
583 simError();
584 }
585
586 }
587
588 void Integrator::constrainB( void ){
589
590 int i,j,k;
591 int done;
592 double posA[3], posB[3];
593 double velA[3], velB[3];
594 double vxab, vyab, vzab;
595 double rab[3];
596 int a, b, ax, ay, az, bx, by, bz;
597 double rma, rmb;
598 double dx, dy, dz;
599 double rabsq, pabsq, rvab;
600 double diffsq;
601 double gab;
602 int iteration;
603
604 for(i=0; i<nAtoms; i++){
605 moving[i] = 0;
606 moved[i] = 1;
607 }
608
609 done = 0;
610 iteration = 0;
611 while( !done && (iteration < maxIteration ) ){
612
613 done = 1;
614
615 for(i=0; i<nConstrained; i++){
616
617 a = constrainedA[i];
618 b = constrainedB[i];
619
620 ax = (a*3) + 0;
621 ay = (a*3) + 1;
622 az = (a*3) + 2;
623
624 bx = (b*3) + 0;
625 by = (b*3) + 1;
626 bz = (b*3) + 2;
627
628 if( moved[a] || moved[b] ){
629
630 atoms[a]->getVel( velA );
631 atoms[b]->getVel( velB );
632
633 vxab = velA[0] - velB[0];
634 vyab = velA[1] - velB[1];
635 vzab = velA[2] - velB[2];
636
637 atoms[a]->getPos( posA );
638 atoms[b]->getPos( posB );
639
640 for (j = 0; j < 3; j++)
641 rab[j] = posA[j] - posB[j];
642
643 info->wrapVector( rab );
644
645 rma = 1.0 / atoms[a]->getMass();
646 rmb = 1.0 / atoms[b]->getMass();
647
648 rvab = rab[0] * vxab + rab[1] * vyab + rab[2] * vzab;
649
650 gab = -rvab / ( ( rma + rmb ) * constrainedDsqr[i] );
651
652 if (fabs(gab) > tol) {
653
654 dx = rab[0] * gab;
655 dy = rab[1] * gab;
656 dz = rab[2] * gab;
657
658 velA[0] += rma * dx;
659 velA[1] += rma * dy;
660 velA[2] += rma * dz;
661
662 atoms[a]->setVel( velA );
663
664 velB[0] -= rmb * dx;
665 velB[1] -= rmb * dy;
666 velB[2] -= rmb * dz;
667
668 atoms[b]->setVel( velB );
669
670 moving[a] = 1;
671 moving[b] = 1;
672 done = 0;
673 }
674 }
675 }
676
677 for(i=0; i<nAtoms; i++){
678 moved[i] = moving[i];
679 moving[i] = 0;
680 }
681
682 iteration++;
683 }
684
685 if( !done ){
686
687
688 sprintf( painCave.errMsg,
689 "Constraint failure in constrainB, too many iterations: %d\n",
690 iteration );
691 painCave.isFatal = 1;
692 simError();
693 }
694
695 }
696
697 void Integrator::rotate( int axes1, int axes2, double angle, double ji[3],
698 double A[3][3] ){
699
700 int i,j,k;
701 double sinAngle;
702 double cosAngle;
703 double angleSqr;
704 double angleSqrOver4;
705 double top, bottom;
706 double rot[3][3];
707 double tempA[3][3];
708 double tempJ[3];
709
710 // initialize the tempA
711
712 for(i=0; i<3; i++){
713 for(j=0; j<3; j++){
714 tempA[j][i] = A[i][j];
715 }
716 }
717
718 // initialize the tempJ
719
720 for( i=0; i<3; i++) tempJ[i] = ji[i];
721
722 // initalize rot as a unit matrix
723
724 rot[0][0] = 1.0;
725 rot[0][1] = 0.0;
726 rot[0][2] = 0.0;
727
728 rot[1][0] = 0.0;
729 rot[1][1] = 1.0;
730 rot[1][2] = 0.0;
731
732 rot[2][0] = 0.0;
733 rot[2][1] = 0.0;
734 rot[2][2] = 1.0;
735
736 // use a small angle aproximation for sin and cosine
737
738 angleSqr = angle * angle;
739 angleSqrOver4 = angleSqr / 4.0;
740 top = 1.0 - angleSqrOver4;
741 bottom = 1.0 + angleSqrOver4;
742
743 cosAngle = top / bottom;
744 sinAngle = angle / bottom;
745
746 rot[axes1][axes1] = cosAngle;
747 rot[axes2][axes2] = cosAngle;
748
749 rot[axes1][axes2] = sinAngle;
750 rot[axes2][axes1] = -sinAngle;
751
752 // rotate the momentum acoording to: ji[] = rot[][] * ji[]
753
754 for(i=0; i<3; i++){
755 ji[i] = 0.0;
756 for(k=0; k<3; k++){
757 ji[i] += rot[i][k] * tempJ[k];
758 }
759 }
760
761 // rotate the Rotation matrix acording to:
762 // A[][] = A[][] * transpose(rot[][])
763
764
765 // NOte for as yet unknown reason, we are performing the
766 // calculation as:
767 // transpose(A[][]) = transpose(A[][]) * transpose(rot[][])
768
769 for(i=0; i<3; i++){
770 for(j=0; j<3; j++){
771 A[j][i] = 0.0;
772 for(k=0; k<3; k++){
773 A[j][i] += tempA[i][k] * rot[j][k];
774 }
775 }
776 }
777 }