ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-3.0/src/integrators/Integrator.cpp
(Generate patch)

Comparing branches/new_design/OOPSE-3.0/src/integrators/Integrator.cpp (file contents):
Revision 1694, Thu Oct 28 22:34:02 2004 UTC vs.
Revision 1695 by tim, Mon Nov 1 22:52:57 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines