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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines