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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines