ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Integrator.cpp
(Generate patch)

Comparing trunk/OOPSE/libmdtools/Integrator.cpp (file contents):
Revision 755 by mmeineke, Tue Sep 9 20:35:25 2003 UTC vs.
Revision 1178 by gezelter, Thu May 13 21:08:05 2004 UTC

# Line 1 | Line 1
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  
# Line 25 | Line 29 | template<typename T> Integrator<T>::Integrator(SimInfo
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  
# Line 65 | Line 69 | template<typename T> void Integrator<T>::checkConstrai
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  
# Line 111 | Line 116 | template<typename T> void Integrator<T>::checkConstrai
116      }
117    }
118  
119 +
120    if (nConstrained > 0){
121      isConstrained = 1;
122  
# Line 132 | Line 138 | template<typename T> void Integrator<T>::checkConstrai
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  
# Line 147 | Line 153 | template<typename T> void Integrator<T>::integrate(voi
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;
# Line 155 | Line 160 | template<typename T> void Integrator<T>::integrate(voi
160    double thermalTime = info->thermalTime;
161    double resetTime = info->resetTime;
162  
163 <
163 >  double difference;
164    double currSample;
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 +  // remove center of mass drift velocity (in case we passed in a configuration
183 +  // that was drifting
184 +  tStats->removeCOMdrift();
185 +
186    // initialize the forces before the first step
187  
188    calcForce(1, 1);
189 +  
190 +  if (nConstrained){
191 +    preMove();
192 +    constrainA();
193 +    calcForce(1, 1);
194 +    constrainB();
195 +  }
196    
197    if (info->setTemp){
198      thermalize();
# Line 192 | Line 208 | template<typename T> void Integrator<T>::integrate(voi
208    dumpOut->writeDump(info->getTime());
209    statOut->writeStat(info->getTime());
210  
195  readyCheck();
211  
212   #ifdef IS_MPI
213    strcpy(checkPointMsg, "The integrator is ready to go.");
214    MPIcheckPoint();
215   #endif // is_mpi
216  
217 <  while (info->getTime() < runTime){
218 <    if ((info->getTime() + dt) >= currStatus){
217 >  while (info->getTime() < runTime && !stopIntegrator()){
218 >    difference = info->getTime() + dt - currStatus;
219 >    if (difference > 0 || fabs(difference) < 1e-4 ){
220        calcPot = 1;
221        calcStress = 1;
222      }
223  
224 + #ifdef PROFILE
225 +    startProfile( pro1 );
226 + #endif
227 +    
228      integrateStep(calcPot, calcStress);
229  
230 + #ifdef PROFILE
231 +    endProfile( pro1 );
232 +
233 +    startProfile( pro2 );
234 + #endif // profile
235 +
236      info->incrTime(dt);
237  
238      if (info->setTemp){
# Line 222 | Line 248 | template<typename T> void Integrator<T>::integrate(voi
248      }
249  
250      if (info->getTime() >= currStatus){
251 <      statOut->writeStat(info->getTime());
252 <      calcPot = 0;
251 >      statOut->writeStat(info->getTime());
252 >      calcPot = 0;
253        calcStress = 0;
254        currStatus += statusTime;
255 <    }
255 >    }
256  
257      if (info->resetIntegrator){
258        if (info->getTime() >= currReset){
# Line 234 | Line 260 | template<typename T> void Integrator<T>::integrate(voi
260          currReset += resetTime;
261        }
262      }
263 +    
264 + #ifdef PROFILE
265 +    endProfile( pro2 );
266 + #endif //profile
267  
268   #ifdef IS_MPI
269      strcpy(checkPointMsg, "successfully took a time step.");
# Line 241 | Line 271 | template<typename T> void Integrator<T>::integrate(voi
271   #endif // is_mpi
272    }
273  
244  dumpOut->writeFinal(info->getTime());
245
274    delete dumpOut;
275    delete statOut;
276   }
# Line 250 | Line 278 | template<typename T> void Integrator<T>::integrateStep
278   template<typename T> void Integrator<T>::integrateStep(int calcPot,
279                                                         int calcStress){
280    // Position full step, and velocity half step
281 +
282 + #ifdef PROFILE
283 +  startProfile(pro3);
284 + #endif //profile
285 +
286    preMove();
287  
288 + #ifdef PROFILE
289 +  endProfile(pro3);
290 +
291 +  startProfile(pro4);
292 + #endif // profile
293 +
294    moveA();
295  
296 <  if (nConstrained){
297 <    constrainA();
298 <  }
296 > #ifdef PROFILE
297 >  endProfile(pro4);
298 >  
299 >  startProfile(pro5);
300 > #endif//profile
301  
302  
303   #ifdef IS_MPI
# Line 274 | Line 315 | template<typename T> void Integrator<T>::integrateStep
315    MPIcheckPoint();
316   #endif // is_mpi
317  
318 + #ifdef PROFILE
319 +  endProfile( pro5 );
320  
321 +  startProfile( pro6 );
322 + #endif //profile
323 +
324    // finish the velocity  half step
325  
326    moveB();
327  
328 <  if (nConstrained){
329 <    constrainB();
330 <  }
328 > #ifdef PROFILE
329 >  endProfile(pro6);
330 > #endif // profile
331  
332   #ifdef IS_MPI
333    strcpy(checkPointMsg, "Succesful moveB\n");
# Line 291 | Line 337 | template<typename T> void Integrator<T>::moveA(void){
337  
338  
339   template<typename T> void Integrator<T>::moveA(void){
340 <  int i, j;
340 >  size_t i, j;
341    DirectionalAtom* dAtom;
342    double Tb[3], ji[3];
297  double A[3][3], I[3][3];
298  double angle;
343    double vel[3], pos[3], frc[3];
344    double mass;
345 +
346 +  for (i = 0; i < integrableObjects.size() ; i++){
347 +    integrableObjects[i]->getVel(vel);
348 +    integrableObjects[i]->getPos(pos);
349 +    integrableObjects[i]->getFrc(frc);
350 +    
351 +    mass = integrableObjects[i]->getMass();
352  
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
353      for (j = 0; j < 3; j++){
354        // velocity half step
355        vel[j] += (dt2 * frc[j] / mass) * eConvert;
# Line 313 | Line 357 | template<typename T> void Integrator<T>::moveA(void){
357        pos[j] += dt * vel[j];
358      }
359  
360 <    atoms[i]->setVel(vel);
361 <    atoms[i]->setPos(pos);
360 >    integrableObjects[i]->setVel(vel);
361 >    integrableObjects[i]->setPos(pos);
362  
363 <    if (atoms[i]->isDirectional()){
320 <      dAtom = (DirectionalAtom *) atoms[i];
363 >    if (integrableObjects[i]->isDirectional()){
364  
365        // get and convert the torque to body frame
366  
367 <      dAtom->getTrq(Tb);
368 <      dAtom->lab2Body(Tb);
367 >      integrableObjects[i]->getTrq(Tb);
368 >      integrableObjects[i]->lab2Body(Tb);
369  
370        // get the angular momentum, and propagate a half step
371  
372 <      dAtom->getJ(ji);
372 >      integrableObjects[i]->getJ(ji);
373  
374        for (j = 0; j < 3; j++)
375          ji[j] += (dt2 * Tb[j]) * eConvert;
376  
377 <      // use the angular velocities to propagate the rotation matrix a
335 <      // full time step
377 >      this->rotationPropagation( integrableObjects[i], ji );
378  
379 <      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 <      dAtom->setJ(ji);
361 <      dAtom->setA(A);
379 >      integrableObjects[i]->setJ(ji);
380      }
381    }
382 +
383 +  if (nConstrained){
384 +    constrainA();
385 +  }
386   }
387  
388  
389   template<typename T> void Integrator<T>::moveB(void){
390    int i, j;
369  DirectionalAtom* dAtom;
391    double Tb[3], ji[3];
392    double vel[3], frc[3];
393    double mass;
394  
395 <  for (i = 0; i < nAtoms; i++){
396 <    atoms[i]->getVel(vel);
397 <    atoms[i]->getFrc(frc);
395 >  for (i = 0; i < integrableObjects.size(); i++){
396 >    integrableObjects[i]->getVel(vel);
397 >    integrableObjects[i]->getFrc(frc);
398  
399 <    mass = atoms[i]->getMass();
399 >    mass = integrableObjects[i]->getMass();
400  
401      // velocity half step
402      for (j = 0; j < 3; j++)
403        vel[j] += (dt2 * frc[j] / mass) * eConvert;
404  
405 <    atoms[i]->setVel(vel);
405 >    integrableObjects[i]->setVel(vel);
406  
407 <    if (atoms[i]->isDirectional()){
387 <      dAtom = (DirectionalAtom *) atoms[i];
407 >    if (integrableObjects[i]->isDirectional()){
408  
409 <      // get and convert the torque to body frame      
409 >      // get and convert the torque to body frame
410  
411 <      dAtom->getTrq(Tb);
412 <      dAtom->lab2Body(Tb);
411 >      integrableObjects[i]->getTrq(Tb);
412 >      integrableObjects[i]->lab2Body(Tb);
413  
414        // get the angular momentum, and propagate a half step
415  
416 <      dAtom->getJ(ji);
416 >      integrableObjects[i]->getJ(ji);
417  
418        for (j = 0; j < 3; j++)
419          ji[j] += (dt2 * Tb[j]) * eConvert;
420  
421  
422 <      dAtom->setJ(ji);
422 >      integrableObjects[i]->setJ(ji);
423      }
424    }
425 +
426 +  if (nConstrained){
427 +    constrainB();
428 +  }
429   }
430  
431   template<typename T> void Integrator<T>::preMove(void){
# Line 420 | Line 444 | template<typename T> void Integrator<T>::constrainA(){
444   }
445  
446   template<typename T> void Integrator<T>::constrainA(){
447 <  int i, j, k;
447 >  int i, j;
448    int done;
449    double posA[3], posB[3];
450    double velA[3], velB[3];
# Line 560 | Line 584 | template<typename T> void Integrator<T>::constrainA(){
584      painCave.isFatal = 1;
585      simError();
586    }
587 +
588   }
589  
590   template<typename T> void Integrator<T>::constrainB(void){
591 <  int i, j, k;
591 >  int i, j;
592    int done;
593    double posA[3], posB[3];
594    double velA[3], velB[3];
# Line 572 | Line 597 | template<typename T> void Integrator<T>::constrainB(vo
597    int a, b, ax, ay, az, bx, by, bz;
598    double rma, rmb;
599    double dx, dy, dz;
600 <  double rabsq, pabsq, rvab;
576 <  double diffsq;
600 >  double rvab;
601    double gab;
602    int iteration;
603  
# Line 663 | Line 687 | template<typename T> void Integrator<T>::rotate(int ax
687    }
688   }
689  
690 + template<typename T> void Integrator<T>::rotationPropagation
691 + ( StuntDouble* sd, double ji[3] ){
692 +
693 +  double angle;
694 +  double A[3][3], I[3][3];
695 +  int i, j, k;
696 +
697 +  // use the angular velocities to propagate the rotation matrix a
698 +  // full time step
699 +
700 +  sd->getA(A);
701 +  sd->getI(I);
702 +
703 +  if (sd->isLinear()) {
704 +    i = sd->linearAxis();
705 +    j = (i+1)%3;
706 +    k = (i+2)%3;
707 +    
708 +    angle = dt2 * ji[j] / I[j][j];
709 +    this->rotate( k, i, angle, ji, A );
710 +
711 +    angle = dt * ji[k] / I[k][k];
712 +    this->rotate( i, j, angle, ji, A);
713 +
714 +    angle = dt2 * ji[j] / I[j][j];
715 +    this->rotate( k, i, angle, ji, A );
716 +
717 +  } else {
718 +    // rotate about the x-axis
719 +    angle = dt2 * ji[0] / I[0][0];
720 +    this->rotate( 1, 2, angle, ji, A );
721 +    
722 +    // rotate about the y-axis
723 +    angle = dt2 * ji[1] / I[1][1];
724 +    this->rotate( 2, 0, angle, ji, A );
725 +    
726 +    // rotate about the z-axis
727 +    angle = dt * ji[2] / I[2][2];
728 +    this->rotate( 0, 1, angle, ji, A);
729 +    
730 +    // rotate about the y-axis
731 +    angle = dt2 * ji[1] / I[1][1];
732 +    this->rotate( 2, 0, angle, ji, A );
733 +    
734 +    // rotate about the x-axis
735 +    angle = dt2 * ji[0] / I[0][0];
736 +    this->rotate( 1, 2, angle, ji, A );
737 +    
738 +  }
739 +  sd->setA( A  );
740 + }
741 +
742   template<typename T> void Integrator<T>::rotate(int axes1, int axes2,
743                                                  double angle, double ji[3],
744                                                  double A[3][3]){
# Line 728 | Line 804 | template<typename T> void Integrator<T>::rotate(int ax
804      }
805    }
806  
807 <  // rotate the Rotation matrix acording to:
807 >  // rotate the Rotation matrix acording to:
808    //            A[][] = A[][] * transpose(rot[][])
809  
810  
# Line 753 | Line 829 | template<typename T> void Integrator<T>::thermalize(){
829   template<typename T> void Integrator<T>::thermalize(){
830    tStats->velocitize();
831   }
832 +
833 + template<typename T> double Integrator<T>::getConservedQuantity(void){
834 +  return tStats->getTotalE();
835 + }
836 + template<typename T> string Integrator<T>::getAdditionalParameters(void){
837 +  //By default, return a null string
838 +  //The reason we use string instead of char* is that if we use char*, we will
839 +  //return a pointer point to local variable which might cause problem
840 +  return string();
841 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines