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

Comparing trunk/OOPSE/libmdtools/NPTi.cpp (file contents):
Revision 645 by tim, Tue Jul 22 19:54:52 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 isotropic thermostating and barostating via the Melchionna
17   // modification of the Hoover algorithm:
# Line 23 | Line 26 | template<typename T> NPTi<T>::NPTi ( SimInfo *theInfo,
26   template<typename T> NPTi<T>::NPTi ( SimInfo *theInfo, ForceFields* the_ff):
27    T( theInfo, the_ff )
28   {
26  chi = 0.0;
29    eta = 0.0;
30 <  have_tau_thermostat = 0;
29 <  have_tau_barostat = 0;
30 <  have_target_temp = 0;
31 <  have_target_pressure = 0;
30 >  oldEta = 0.0;
31   }
32  
33 < template<typename T> void NPTi<T>::moveA() {
34 <  
35 <  int i, j;
37 <  DirectionalAtom* dAtom;
38 <  double Tb[3], ji[3];
39 <  double A[3][3], I[3][3];
40 <  double angle, mass;
41 <  double vel[3], pos[3], frc[3];
33 > template<typename T> NPTi<T>::~NPTi() {
34 >  //nothing for now
35 > }
36  
37 <  double rj[3];
38 <  double instaTemp, instaPress, instaVol;
39 <  double tt2, tb2, scaleFactor;
37 > template<typename T> void NPTi<T>::resetIntegrator() {
38 >  eta = 0.0;
39 >  T::resetIntegrator();
40 > }
41  
42 <  tt2 = tauThermostat * tauThermostat;
43 <  tb2 = tauBarostat * tauBarostat;
42 > template<typename T> void NPTi<T>::evolveEtaA() {
43 >  eta += dt2 * ( instaVol * (instaPress - targetPressure) /
44 >                 (p_convert*NkBT*tb2));
45 >  oldEta = eta;
46 > }
47  
48 <  instaTemp = tStats->getTemperature();
51 <  instaPress = tStats->getPressure();
52 <  instaVol = tStats->getVolume();
53 <  
54 <   // first evolve chi a half step
48 > template<typename T> void NPTi<T>::evolveEtaB() {
49    
50 <  chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2;
51 <  eta += dt2 * ( instaVol * (instaPress - targetPressure) /
50 >  prevEta = eta;
51 >  eta = oldEta + dt2 * ( instaVol * (instaPress - targetPressure) /
52                   (p_convert*NkBT*tb2));
53 + }
54  
55 <  for( i=0; i<nAtoms; i++ ){
56 <    atoms[i]->getVel( vel );
62 <    atoms[i]->getPos( pos );
63 <    atoms[i]->getFrc( frc );
55 > template<typename T> void NPTi<T>::getVelScaleA(double sc[3], double vel[3]) {
56 >  int i;
57  
58 <    mass = atoms[i]->getMass();
58 >  for(i=0; i<3; i++) sc[i] = vel[i] * ( chi + eta );
59 > }
60  
61 <    for (j=0; j < 3; j++) {
62 <      vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*(chi+eta));
69 <      rj[j] = pos[j];
70 <    }
61 > template<typename T> void NPTi<T>::getVelScaleB(double sc[3], int index ){
62 >  int i;
63  
64 <    atoms[i]->setVel( vel );
64 >  for(i=0; i<3; i++) sc[i] = oldVel[index*3 + i] * ( chi + eta );
65 > }
66  
74    info->wrapVector(rj);
67  
68 <    for (j = 0; j < 3; j++)
69 <      pos[j] += dt * (vel[j] + eta*rj[j]);
68 > template<typename T> void NPTi<T>::getPosScale(double pos[3], double COM[3],
69 >                                               int index, double sc[3]){
70 >  int j;
71  
72 <    atoms[i]->setPos( pos );
72 >  for(j=0; j<3; j++)
73 >    sc[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j];
74  
75 <    if( atoms[i]->isDirectional() ){
75 >  for(j=0; j<3; j++)
76 >    sc[j] *= eta;
77 > }
78  
79 <      dAtom = (DirectionalAtom *)atoms[i];
84 <          
85 <      // get and convert the torque to body frame
86 <      
87 <      dAtom->getTrq( Tb );
88 <      dAtom->lab2Body( Tb );
89 <      
90 <      // get the angular momentum, and propagate a half step
79 > template<typename T> void NPTi<T>::scaleSimBox( void ){
80  
81 <      dAtom->getJ( ji );
81 >  double scaleFactor;
82  
94      for (j=0; j < 3; j++)
95        ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);
96      
97      // use the angular velocities to propagate the rotation matrix a
98      // full time step
99
100      dAtom->getA(A);
101      dAtom->getI(I);
102    
103      // rotate about the x-axis      
104      angle = dt2 * ji[0] / I[0][0];
105      this->rotate( 1, 2, angle, ji, A );
106
107      // rotate about the y-axis
108      angle = dt2 * ji[1] / I[1][1];
109      this->rotate( 2, 0, angle, ji, A );
110      
111      // rotate about the z-axis
112      angle = dt * ji[2] / I[2][2];
113      this->rotate( 0, 1, angle, ji, A);
114      
115      // rotate about the y-axis
116      angle = dt2 * ji[1] / I[1][1];
117      this->rotate( 2, 0, angle, ji, A );
118      
119       // rotate about the x-axis
120      angle = dt2 * ji[0] / I[0][0];
121      this->rotate( 1, 2, angle, ji, A );
122      
123      dAtom->setJ( ji );
124      dAtom->setA( A  );    
125    }                
126
127  }
128
129  // Scale the box after all the positions have been moved:
130  
83    scaleFactor = exp(dt*eta);
84  
85    if ((scaleFactor > 1.1) || (scaleFactor < 0.9)) {
# Line 139 | Line 91 | template<typename T> void NPTi<T>::moveA() {
91      painCave.isFatal = 1;
92      simError();
93    } else {        
94 <    info->scaleBox(exp(dt*eta));      
95 <  }
94 >    info->scaleBox(scaleFactor);      
95 >  }  
96  
97   }
98  
99 < template<typename T> void NPTi<T>::moveB( void ){
99 > template<typename T> bool NPTi<T>::etaConverged() {
100  
101 <  int i, j;
102 <  DirectionalAtom* dAtom;
151 <  double Tb[3], ji[3];
152 <  double vel[3], frc[3];
153 <  double mass;
101 >  return ( fabs(prevEta - eta) <= etaTolerance );
102 > }
103  
104 <  double instaTemp, instaPress, instaVol;
156 <  double tt2, tb2;
157 <  
158 <  tt2 = tauThermostat * tauThermostat;
159 <  tb2 = tauBarostat * tauBarostat;
104 > template<typename T> double NPTi<T>::getConservedQuantity(void){
105  
106 <  instaTemp = tStats->getTemperature();
107 <  instaPress = tStats->getPressure();
108 <  instaVol = tStats->getVolume();
109 <
110 <  chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2;
111 <  eta += dt2 * ( instaVol * (instaPress - targetPressure) /
167 <                 (p_convert*NkBT*tb2));
106 >  double conservedQuantity;
107 >  double Energy;
108 >  double thermostat_kinetic;
109 >  double thermostat_potential;
110 >  double barostat_kinetic;
111 >  double barostat_potential;
112    
113 <  for( i=0; i<nAtoms; i++ ){
113 >  Energy = tStats->getTotalE();
114  
115 <    atoms[i]->getVel( vel );
116 <    atoms[i]->getFrc( frc );
115 >  thermostat_kinetic = fkBT* tt2 * chi * chi /
116 >    (2.0 * eConvert);
117  
118 <    mass = atoms[i]->getMass();
118 >  thermostat_potential = fkBT* integralOfChidt / eConvert;
119  
176    // velocity half step
177    for (j=0; j < 3; j++)
178      vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*(chi+eta));
179    
180    atoms[i]->setVel( vel );
120  
121 <    if( atoms[i]->isDirectional() ){
122 <
184 <      dAtom = (DirectionalAtom *)atoms[i];
185 <
186 <      // get and convert the torque to body frame      
187 <
188 <      dAtom->getTrq( Tb );
189 <      dAtom->lab2Body( Tb );
190 <
191 <      // get the angular momentum, and propagate a half step
192 <
193 <      dAtom->getJ( ji );
194 <
195 <      for (j=0; j < 3; j++)
196 <        ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);    
197 <
198 <      dAtom->setJ( ji );
199 <    }
200 <  }
201 < }
202 <
203 < template<typename T> int NPTi<T>::readyCheck() {
204 <
205 <  // First check to see if we have a target temperature.
206 <  // Not having one is fatal.
121 >  barostat_kinetic = 3.0 * NkBT * tb2 * eta * eta /
122 >    (2.0 * eConvert);
123    
124 <  if (!have_target_temp) {
125 <    sprintf( painCave.errMsg,
210 <             "NPTi error: You can't use the NPTi integrator\n"
211 <             "   without a targetTemp!\n"
212 <             );
213 <    painCave.isFatal = 1;
214 <    simError();
215 <    return -1;
216 <  }
124 >  barostat_potential = (targetPressure * tStats->getVolume() / p_convert) /
125 >    eConvert;
126  
127 <  if (!have_target_pressure) {
128 <    sprintf( painCave.errMsg,
220 <             "NPTi error: You can't use the NPTi integrator\n"
221 <             "   without a targetPressure!\n"
222 <             );
223 <    painCave.isFatal = 1;
224 <    simError();
225 <    return -1;
226 <  }
127 >  conservedQuantity = Energy + thermostat_kinetic + thermostat_potential +
128 >    barostat_kinetic + barostat_potential;
129    
130 <  // We must set tauThermostat.
131 <  
230 <  if (!have_tau_thermostat) {
231 <    sprintf( painCave.errMsg,
232 <             "NPTi error: If you use the NPTi\n"
233 <             "   integrator, you must set tauThermostat.\n");
234 <    painCave.isFatal = 1;
235 <    simError();
236 <    return -1;
237 <  }    
130 > //   cout.width(8);
131 > //   cout.precision(8);
132  
133 <  // We must set tauBarostat.
134 <  
135 <  if (!have_tau_barostat) {
136 <    sprintf( painCave.errMsg,
243 <             "NPTi error: If you use the NPTi\n"
244 <             "   integrator, you must set tauBarostat.\n");
245 <    painCave.isFatal = 1;
246 <    simError();
247 <    return -1;
248 <  }    
249 <
250 <  // We need NkBT a lot, so just set it here:
251 <
252 <  NkBT = (double)info->ndf * kB * targetTemp;
253 <
254 <  return 1;
133 > //   cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic <<
134 > //       "\t" << thermostat_potential << "\t" << barostat_kinetic <<
135 > //       "\t" << barostat_potential << "\t" << conservedQuantity << endl;
136 >  return conservedQuantity;
137   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines