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

Comparing trunk/OOPSE/libmdtools/NPTf.cpp (file contents):
Revision 763 by tim, Mon Sep 15 16:52:02 2003 UTC vs.
Revision 787 by mmeineke, Thu Sep 25 19:27:15 2003 UTC

# Line 9 | Line 9
9   #include "Integrator.hpp"
10   #include "simError.h"
11  
12 + #ifdef IS_MPI
13 + #include "mpiSimulation.hpp"
14 + #endif
15  
16   // Basic non-isotropic thermostating and barostating via the Melchionna
17   // modification of the Hoover algorithm:
# Line 23 | Line 26 | template<typename T> NPTf<T>::NPTf ( SimInfo *theInfo,
26   template<typename T> NPTf<T>::NPTf ( SimInfo *theInfo, ForceFields* the_ff):
27    T( theInfo, the_ff )
28   {
29 <  int i, j;
30 <  chi = 0.0;
31 <  integralOfChidt = 0.0;
29 >  
30 >  int i,j;
31 >  
32 >  for(i = 0; i < 3; i++){
33 >    for (j = 0; j < 3; j++){
34 >      
35 >      eta[i][j] = 0.0;
36 >      oldEta[i][j] = 0.0;
37 >    }
38 >  }
39 > }
40  
41 <  for(i = 0; i < 3; i++)
42 <    for (j = 0; j < 3; j++)
41 > template<typename T> NPTf<T>::~NPTf() {
42 >
43 >  // empty for now
44 > }
45 >
46 > template<typename T> void NPTf<T>::resetIntegrator() {
47 >  
48 >  int i, j;
49 >  
50 >  for(i = 0; i < 3; i++)
51 >    for (j = 0; j < 3; j++)
52        eta[i][j] = 0.0;
53 +  
54 +  T::resetIntegrator();
55 + }
56  
57 <  have_tau_thermostat = 0;
58 <  have_tau_barostat = 0;
59 <  have_target_temp = 0;
60 <  have_target_pressure = 0;
57 > template<typename T> void NPTf<T>::evolveEtaA() {
58 >  
59 >  int i, j;
60 >  
61 >  for(i = 0; i < 3; i ++){
62 >    for(j = 0; j < 3; j++){
63 >      if( i == j)
64 >        eta[i][j] += dt2 *  instaVol *
65 >          (press[i][j] - targetPressure/p_convert) / (NkBT*tb2);
66 >      else
67 >        eta[i][j] += dt2 * instaVol * press[i][j] / (NkBT*tb2);
68 >    }
69 >  }
70 >  
71 >  for(i = 0; i < 3; i++)
72 >    for (j = 0; j < 3; j++)
73 >      oldEta[i][j] = eta[i][j];
74   }
75  
76 < template<typename T> void NPTf<T>::moveA() {
76 > template<typename T> void NPTf<T>::evolveEtaB() {
77    
78 <  int i, j, k;
43 <  DirectionalAtom* dAtom;
44 <  double Tb[3], ji[3];
45 <  double A[3][3], I[3][3];
46 <  double angle, mass;
47 <  double vel[3], pos[3], frc[3];
78 >  int i,j;
79  
80 <  double rj[3];
81 <  double instaTemp, instaPress, instaVol;
82 <  double tt2, tb2;
52 <  double sc[3];
53 <  double eta2ij;
54 <  double press[3][3], vScale[3][3], hm[3][3], hmnew[3][3], scaleMat[3][3];
55 <  double bigScale, smallScale, offDiagMax;
80 >  for(i = 0; i < 3; i++)
81 >    for (j = 0; j < 3; j++)
82 >      prevEta[i][j] = eta[i][j];
83  
84 <  tt2 = tauThermostat * tauThermostat;
85 <  tb2 = tauBarostat * tauBarostat;
84 >  for(i = 0; i < 3; i ++){
85 >    for(j = 0; j < 3; j++){
86 >      if( i == j) {
87 >        eta[i][j] = oldEta[i][j] + dt2 *  instaVol *
88 >          (press[i][j] - targetPressure/p_convert) / (NkBT*tb2);
89 >      } else {
90 >        eta[i][j] = oldEta[i][j] + dt2 * instaVol * press[i][j] / (NkBT*tb2);
91 >      }
92 >    }
93 >  }
94 > }
95  
96 <  instaTemp = tStats->getTemperature();
97 <  tStats->getPressureTensor(press);
98 <  instaVol = tStats->getVolume();
63 <  
64 <  // first evolve chi a half step
65 <  
66 <  chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2;
96 > template<typename T> void NPTf<T>::getVelScaleA(double sc[3], double vel[3]) {
97 >  int i,j;
98 >  double vScale[3][3];
99  
100    for (i = 0; i < 3; i++ ) {
101      for (j = 0; j < 3; j++ ) {
102 +      vScale[i][j] = eta[i][j];
103 +      
104        if (i == j) {
105 <        
106 <        eta[i][j] += dt2 * instaVol *
73 <          (press[i][j] - targetPressure/p_convert) / (NkBT*tb2);
74 <        
75 <        vScale[i][j] = eta[i][j] + chi;
76 <          
77 <      } else {
78 <        
79 <        eta[i][j] += dt2 * instaVol * press[i][j] / (NkBT*tb2);
80 <
81 <        vScale[i][j] = eta[i][j];
82 <        
83 <      }
105 >        vScale[i][j] += chi;          
106 >      }              
107      }
108    }
109 +  
110 +  info->matVecMul3( vScale, vel, sc );
111 + }
112  
113 <  for( i=0; i<nAtoms; i++ ){
113 > template<typename T> void NPTf<T>::getVelScaleB(double sc[3], int index ){
114 >  int i,j;
115 >  double myVel[3];
116 >  double vScale[3][3];
117  
118 <    atoms[i]->getVel( vel );
119 <    atoms[i]->getPos( pos );
120 <    atoms[i]->getFrc( frc );
121 <
122 <    mass = atoms[i]->getMass();
123 <    
124 <    // velocity half step
96 <        
97 <    info->matVecMul3( vScale, vel, sc );
98 <    
99 <    for (j = 0; j < 3; j++) {
100 <      vel[j] += dt2 * ((frc[j]  / mass) * eConvert - sc[j]);
101 <      rj[j] = pos[j];
118 >  for (i = 0; i < 3; i++ ) {
119 >    for (j = 0; j < 3; j++ ) {
120 >      vScale[i][j] = eta[i][j];
121 >      
122 >      if (i == j) {
123 >        vScale[i][j] += chi;          
124 >      }              
125      }
126 +  }
127 +  
128 +  for (j = 0; j < 3; j++)
129 +    myVel[j] = oldVel[3*index + j];
130  
131 <    atoms[i]->setVel( vel );
131 >  info->matVecMul3( vScale, myVel, sc );
132 > }
133  
134 <    // position whole step    
134 > template<typename T> void NPTf<T>::getPosScale(double pos[3], double COM[3],
135 >                                               int index, double sc[3]){
136 >  int j;
137 >  double rj[3];
138  
139 <    info->wrapVector(rj);
139 >  for(j=0; j<3; j++)
140 >    rj[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j];
141  
142 <    info->matVecMul3( eta, rj, sc );
142 >  info->matVecMul3( eta, rj, sc );
143 > }
144  
145 <    for (j = 0; j < 3; j++ )
113 <      pos[j] += dt * (vel[j] + sc[j]);
145 > template<typename T> void NPTf<T>::scaleSimBox( void ){
146  
147 <    atoms[i]->setPos( pos );
148 <  
149 <    if( atoms[i]->isDirectional() ){
147 >  int i,j,k;
148 >  double scaleMat[3][3];
149 >  double eta2ij;
150 >  double bigScale, smallScale, offDiagMax;
151 >  double hm[3][3], hmnew[3][3];
152 >  
153  
119      dAtom = (DirectionalAtom *)atoms[i];
120          
121      // get and convert the torque to body frame
122      
123      dAtom->getTrq( Tb );
124      dAtom->lab2Body( Tb );
125      
126      // get the angular momentum, and propagate a half step
154  
128      dAtom->getJ( ji );
129
130      for (j=0; j < 3; j++)
131        ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);
132      
133      // use the angular velocities to propagate the rotation matrix a
134      // full time step
135
136      dAtom->getA(A);
137      dAtom->getI(I);
138    
139      // rotate about the x-axis      
140      angle = dt2 * ji[0] / I[0][0];
141      this->rotate( 1, 2, angle, ji, A );
142
143      // rotate about the y-axis
144      angle = dt2 * ji[1] / I[1][1];
145      this->rotate( 2, 0, angle, ji, A );
146      
147      // rotate about the z-axis
148      angle = dt * ji[2] / I[2][2];
149      this->rotate( 0, 1, angle, ji, A);
150      
151      // rotate about the y-axis
152      angle = dt2 * ji[1] / I[1][1];
153      this->rotate( 2, 0, angle, ji, A );
154      
155       // rotate about the x-axis
156      angle = dt2 * ji[0] / I[0][0];
157      this->rotate( 1, 2, angle, ji, A );
158      
159      dAtom->setJ( ji );
160      dAtom->setA( A  );    
161    }                    
162  }
163  
155    // Scale the box after all the positions have been moved:
156    
157    // Use a taylor expansion for eta products:  Hmat = Hmat . exp(dt * etaMat)
# Line 225 | Line 216 | template<typename T> void NPTf<T>::moveA() {
216      info->matMul3(hm, scaleMat, hmnew);
217      info->setBoxM(hmnew);
218    }
228  
219   }
220  
221 < template<typename T> void NPTf<T>::moveB( void ){
221 > template<typename T> bool NPTf<T>::etaConverged() {
222 >  int i;
223 >  double diffEta, sumEta;
224  
225 <  int i, j;
226 <  DirectionalAtom* dAtom;
227 <  double Tb[3], ji[3];
236 <  double vel[3], frc[3];
237 <  double mass;
238 <
239 <  double instaTemp, instaPress, instaVol;
240 <  double tt2, tb2;
241 <  double sc[3];
242 <  double press[3][3], vScale[3][3];
225 >  sumEta = 0;
226 >  for(i = 0; i < 3; i++)
227 >    sumEta += pow(prevEta[i][i] - eta[i][i], 2);    
228    
229 <  tt2 = tauThermostat * tauThermostat;
245 <  tb2 = tauBarostat * tauBarostat;
246 <
247 <  instaTemp = tStats->getTemperature();
248 <  tStats->getPressureTensor(press);
249 <  instaVol = tStats->getVolume();
250 <  
251 <  // first evolve chi a half step
229 >  diffEta = sqrt( sumEta / 3.0 );
230    
231 <  chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2;
254 <  
255 <  for (i = 0; i < 3; i++ ) {
256 <    for (j = 0; j < 3; j++ ) {
257 <      if (i == j) {
258 <
259 <        eta[i][j] += dt2 * instaVol *
260 <          (press[i][j] - targetPressure/p_convert) / (NkBT*tb2);
261 <
262 <        vScale[i][j] = eta[i][j] + chi;
263 <        
264 <      } else {
265 <        
266 <        eta[i][j] += dt2 * instaVol * press[i][j] / (NkBT*tb2);
267 <
268 <        vScale[i][j] = eta[i][j];
269 <        
270 <      }
271 <    }
272 <  }
273 <
274 <  for( i=0; i<nAtoms; i++ ){
275 <
276 <    atoms[i]->getVel( vel );
277 <    atoms[i]->getFrc( frc );
278 <
279 <    mass = atoms[i]->getMass();
280 <    
281 <    // velocity half step
282 <        
283 <    info->matVecMul3( vScale, vel, sc );
284 <    
285 <    for (j = 0; j < 3; j++) {
286 <      vel[j] += dt2 * ((frc[j]  / mass) * eConvert - sc[j]);
287 <    }
288 <
289 <    atoms[i]->setVel( vel );
290 <    
291 <    if( atoms[i]->isDirectional() ){
292 <
293 <      dAtom = (DirectionalAtom *)atoms[i];
294 <          
295 <      // get and convert the torque to body frame
296 <      
297 <      dAtom->getTrq( Tb );
298 <      dAtom->lab2Body( Tb );
299 <      
300 <      // get the angular momentum, and propagate a half step
301 <      
302 <      dAtom->getJ( ji );
303 <      
304 <      for (j=0; j < 3; j++)
305 <        ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);
306 <      
307 <      dAtom->setJ( ji );
308 <
309 <    }                    
310 <  }
231 >  return ( diffEta <= etaTolerance );
232   }
233  
234 < template<typename T> void NPTf<T>::resetIntegrator() {
314 <  int i,j;
234 > template<typename T> double NPTf<T>::getConservedQuantity(void){
235    
236 <  chi = 0.0;
236 >  double conservedQuantity;
237 >  double totalEnergy;
238 >  double thermostat_kinetic;
239 >  double thermostat_potential;
240 >  double barostat_kinetic;
241 >  double barostat_potential;
242 >  double trEta;
243 >  double a[3][3], b[3][3];
244  
245 <  for(i = 0; i < 3; i++)
319 <    for (j = 0; j < 3; j++)
320 <      eta[i][j] = 0.0;
245 >  totalEnergy = tStats->getTotalE();
246  
247 < }
247 >  thermostat_kinetic = fkBT * tt2 * chi * chi /
248 >    (2.0 * eConvert);
249  
250 < template<typename T> int NPTf<T>::readyCheck() {
250 >  thermostat_potential = fkBT* integralOfChidt / eConvert;
251  
252 <  //check parent's readyCheck() first
253 <  if (T::readyCheck() == -1)
254 <    return -1;
255 <
256 <  // First check to see if we have a target temperature.
257 <  // Not having one is fatal.
252 >  info->transposeMat3(eta, a);
253 >  info->matMul3(a, eta, b);
254 >  trEta = info->matTrace3(b);
255 >
256 >  barostat_kinetic = NkBT * tb2 * trEta /
257 >    (2.0 * eConvert);
258    
259 <  if (!have_target_temp) {
260 <    sprintf( painCave.errMsg,
335 <             "NPTf error: You can't use the NPTf integrator\n"
336 <             "   without a targetTemp!\n"
337 <             );
338 <    painCave.isFatal = 1;
339 <    simError();
340 <    return -1;
341 <  }
259 >  barostat_potential = (targetPressure * tStats->getVolume() / p_convert) /
260 >    eConvert;
261  
262 <  if (!have_target_pressure) {
263 <    sprintf( painCave.errMsg,
345 <             "NPTf error: You can't use the NPTf integrator\n"
346 <             "   without a targetPressure!\n"
347 <             );
348 <    painCave.isFatal = 1;
349 <    simError();
350 <    return -1;
351 <  }
262 >  conservedQuantity = totalEnergy + thermostat_kinetic + thermostat_potential +
263 >    barostat_kinetic + barostat_potential;
264    
265 <  // We must set tauThermostat.
266 <  
355 <  if (!have_tau_thermostat) {
356 <    sprintf( painCave.errMsg,
357 <             "NPTf error: If you use the NPTf\n"
358 <             "   integrator, you must set tauThermostat.\n");
359 <    painCave.isFatal = 1;
360 <    simError();
361 <    return -1;
362 <  }    
265 > //   cout.width(8);
266 > //   cout.precision(8);
267  
268 <  // We must set tauBarostat.
269 <  
270 <  if (!have_tau_barostat) {
367 <    sprintf( painCave.errMsg,
368 <             "NPTf error: If you use the NPTf\n"
369 <             "   integrator, you must set tauBarostat.\n");
370 <    painCave.isFatal = 1;
371 <    simError();
372 <    return -1;
373 <  }    
268 > //   cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic <<
269 > //       "\t" << thermostat_potential << "\t" << barostat_kinetic <<
270 > //       "\t" << barostat_potential << "\t" << conservedQuantity << endl;
271  
375  // We need NkBT a lot, so just set it here:
376
377  NkBT = (double)info->ndf * kB * targetTemp;
378
379  return 1;
380 }
381
382 template<typename T> double NPTf<T>::getConservedQuantity(void){
383
384  double conservedQuantity;
385  double tb2;
386  double eta2[3][3];  
387  double trEta;
388
389  //HNVE
390  conservedQuantity = tStats->getTotalE();
391
392  //HNVT
393  conservedQuantity += (info->getNDF() * kB * targetTemp *
394    (integralOfChidt + tauThermostat * tauThermostat * chi * chi /2)) / eConvert;
395
396  //HNPT
397  tb2 = tauBarostat *tauBarostat;
398
399  trEta = info->matTrace3(eta);
400  
401  conservedQuantity += (targetPressure * tStats->getVolume() / p_convert +
402                        3*NkBT/2 * tb2 * trEta * trEta) / eConvert;
403  
272    return conservedQuantity;
273 +  
274   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines