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 597 by mmeineke, Mon Jul 14 21:28:54 2003 UTC vs.
Revision 768 by mmeineke, Wed Sep 17 14:22:15 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines