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 580 by gezelter, Wed Jul 9 13:56:36 2003 UTC vs.
Revision 829 by gezelter, Tue Oct 28 16:03:37 2003 UTC

# Line 1 | Line 1
1 + #include <math.h>
2   #include "Atom.hpp"
3   #include "SRI.hpp"
4   #include "AbstractClasses.hpp"
# Line 8 | 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 19 | Line 23 | NPTf::NPTf ( SimInfo *theInfo, ForceFields* the_ff):
23   //
24   //    Hoover, W. G., 1986, Phys. Rev. A, 34, 2499.
25  
26 < NPTf::NPTf ( SimInfo *theInfo, ForceFields* the_ff):
27 <  Integrator( theInfo, the_ff )
26 > template<typename T> NPTf<T>::NPTf ( SimInfo *theInfo, ForceFields* the_ff):
27 >  T( theInfo, the_ff )
28   {
29 <  int i;
30 <  chi = 0.0;
31 <  for(i = 0; i < 9; i++) eta[i] = 0.0;
32 <  have_tau_thermostat = 0;
33 <  have_tau_barostat = 0;
34 <  have_target_temp = 0;
35 <  have_target_pressure = 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 < void NPTf::moveA() {
35 <  
36 <  int i,j,k;
37 <  int atomIndex, aMatIndex;
38 <  DirectionalAtom* dAtom;
39 <  double Tb[3];
40 <  double ji[3];
41 <  double rj[3];
42 <  double ident[3][3], eta1[3][3], eta2[3][3], hmnew[3][3];
43 <  double hm[9];
44 <  double vx, vy, vz;
45 <  double scx, scy, scz;
46 <  double instaTemp, instaPress, instaVol;
47 <  double tt2, tb2;
48 <  double angle;
49 <  double press[9];
50 <  const double p_convert = 1.63882576e8;
41 > template<typename T> NPTf<T>::~NPTf() {
42  
43 <  tt2 = tauThermostat * tauThermostat;
44 <  tb2 = tauBarostat * tauBarostat;
43 >  // empty for now
44 > }
45  
46 <  instaTemp = tStats->getTemperature();
56 <  tStats->getPressureTensor(press);
57 <
58 <  for (i=0; i < 9; i++) press[i] *= p_convert;
59 <
60 <  instaVol = tStats->getVolume();
61 <  
62 <  // first evolve chi a half step
46 > template<typename T> void NPTf<T>::resetIntegrator() {
47    
48 <  chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2;
48 >  int i, j;
49    
50 <  eta[0] += dt2 * instaVol * (press[0] - targetPressure) / (NkBT*tb2);
51 <  eta[1] += dt2 * instaVol * press[1] / (NkBT*tb2);
52 <  eta[2] += dt2 * instaVol * press[2] / (NkBT*tb2);
69 <  eta[3] += dt2 * instaVol * press[3] / (NkBT*tb2);
70 <  eta[4] += dt2 * instaVol * (press[4] - targetPressure) / (NkBT*tb2);
71 <  eta[5] += dt2 * instaVol * press[5] / (NkBT*tb2);
72 <  eta[6] += dt2 * instaVol * press[6] / (NkBT*tb2);
73 <  eta[7] += dt2 * instaVol * press[7] / (NkBT*tb2);
74 <  eta[8] += dt2 * instaVol * (press[8] - targetPressure) / (NkBT*tb2);
50 >  for(i = 0; i < 3; i++)
51 >    for (j = 0; j < 3; j++)
52 >      eta[i][j] = 0.0;
53    
54 <  for( i=0; i<nAtoms; i++ ){
55 <    atomIndex = i * 3;
78 <    aMatIndex = i * 9;
79 <    
80 <    // velocity half step
81 <    
82 <    vx = vel[atomIndex];
83 <    vy = vel[atomIndex+1];
84 <    vz = vel[atomIndex+2];
85 <    
86 <    scx = (chi + eta[0])*vx + eta[1]*vy + eta[2]*vz;
87 <    scy = eta[3]*vx + (chi + eta[4])*vy + eta[5]*vz;
88 <    scz = eta[6]*vx + eta[7]*vy + (chi + eta[8])*vz;
89 <    
90 <    vx += dt2 * ((frc[atomIndex]  /atoms[i]->getMass())*eConvert - scx);
91 <    vy += dt2 * ((frc[atomIndex+1]/atoms[i]->getMass())*eConvert - scy);
92 <    vz += dt2 * ((frc[atomIndex+2]/atoms[i]->getMass())*eConvert - scz);
54 >  T::resetIntegrator();
55 > }
56  
57 <    vel[atomIndex] = vx;
58 <    vel[atomIndex+1] = vy;
59 <    vel[atomIndex+2] = vz;
60 <
61 <    // position whole step    
62 <
63 <    rj[0] = pos[atomIndex];
64 <    rj[1] = pos[atomIndex+1];
65 <    rj[2] = pos[atomIndex+2];
66 <
67 <    info->wrapVector(rj);
105 <
106 <    scx = eta[0]*rj[0] + eta[1]*rj[1] + eta[2]*rj[2];
107 <    scy = eta[3]*rj[0] + eta[4]*rj[1] + eta[5]*rj[2];
108 <    scz = eta[6]*rj[0] + eta[7]*rj[1] + eta[8]*rj[2];
109 <
110 <    pos[atomIndex] += dt * (vel[atomIndex] + scx);
111 <    pos[atomIndex+1] += dt * (vel[atomIndex+1] + scy);
112 <    pos[atomIndex+2] += dt * (vel[atomIndex+2] + scz);
113 <  
114 <    if( atoms[i]->isDirectional() ){
115 <
116 <      dAtom = (DirectionalAtom *)atoms[i];
117 <          
118 <      // get and convert the torque to body frame
119 <      
120 <      Tb[0] = dAtom->getTx();
121 <      Tb[1] = dAtom->getTy();
122 <      Tb[2] = dAtom->getTz();
123 <      
124 <      dAtom->lab2Body( Tb );
125 <      
126 <      // get the angular momentum, and propagate a half step
127 <
128 <      ji[0] = dAtom->getJx();
129 <      ji[1] = dAtom->getJy();
130 <      ji[2] = dAtom->getJz();
131 <      
132 <      ji[0] += dt2 * (Tb[0] * eConvert - ji[0]*chi);
133 <      ji[1] += dt2 * (Tb[1] * eConvert - ji[1]*chi);
134 <      ji[2] += dt2 * (Tb[2] * eConvert - ji[2]*chi);
135 <      
136 <      // use the angular velocities to propagate the rotation matrix a
137 <      // full time step
138 <      
139 <      // rotate about the x-axis      
140 <      angle = dt2 * ji[0] / dAtom->getIxx();
141 <      this->rotate( 1, 2, angle, ji, &Amat[aMatIndex] );
142 <      
143 <      // rotate about the y-axis
144 <      angle = dt2 * ji[1] / dAtom->getIyy();
145 <      this->rotate( 2, 0, angle, ji, &Amat[aMatIndex] );
146 <      
147 <      // rotate about the z-axis
148 <      angle = dt * ji[2] / dAtom->getIzz();
149 <      this->rotate( 0, 1, angle, ji, &Amat[aMatIndex] );
150 <      
151 <      // rotate about the y-axis
152 <      angle = dt2 * ji[1] / dAtom->getIyy();
153 <      this->rotate( 2, 0, angle, ji, &Amat[aMatIndex] );
154 <      
155 <       // rotate about the x-axis
156 <      angle = dt2 * ji[0] / dAtom->getIxx();
157 <      this->rotate( 1, 2, angle, ji, &Amat[aMatIndex] );
158 <      
159 <      dAtom->setJx( ji[0] );
160 <      dAtom->setJy( ji[1] );
161 <      dAtom->setJz( ji[2] );
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      }
163    
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 <  // Scale the box after all the positions have been moved:
76 > template<typename T> void NPTf<T>::evolveEtaB() {
77 >  
78 >  int i,j;
79  
80 <  // Use a taylor expansion for eta products:  Hmat = Hmat . exp(dt * etaMat)
81 <  //  Hmat = Hmat . ( Ident + dt * etaMat  + dt^2 * etaMat*etaMat / 2)
80 >  for(i = 0; i < 3; i++)
81 >    for (j = 0; j < 3; j++)
82 >      prevEta[i][j] = eta[i][j];
83  
84 <
85 <  for(i=0; i<3; i++){
86 <    for(j=0; j<3; j++){
87 <      ident[i][j] = 0.0;
88 <      eta1[i][j] = eta[3*i+j];
89 <      eta2[i][j] = 0.0;
90 <      for(k=0; k<3; k++){
178 <        eta2[i][j] += eta[3*i+k] * eta[3*k+j];
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      }
181    ident[i][i] = 1.0;
93    }
94 + }
95  
96 <  
97 <  info->getBoxM(hm);
98 <
99 <  for(i=0; i<3; i++){
100 <    for(j=0; j<3; j++){      
101 <      hmnew[i][j] = 0.0;
102 <      for(k=0; k<3; k++){
103 <        // remember that hmat has transpose ordering for Fortran compat:
104 <        hmnew[i][j] += hm[3*k+i] * (ident[k][j]
105 <                                    + dt * eta1[k][j]
106 <                                    + 0.5 * dt * dt * eta2[k][j]);
195 <      }
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 >        vScale[i][j] += chi;          
106 >      }              
107      }
108    }
109    
110 <  for (i = 0; i < 3; i++) {
111 <    for (j = 0; j < 3; j++) {
112 <      // remember that hmat has transpose ordering for Fortran compat:
113 <      hm[3*j + 1] = hmnew[i][j];
110 >  info->matVecMul3( vScale, vel, sc );
111 > }
112 >
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 >  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    }
205
206  info->setBoxM(hm);
127    
128 +  for (j = 0; j < 3; j++)
129 +    myVel[j] = oldVel[3*index + j];
130 +
131 +  info->matVecMul3( vScale, myVel, sc );
132   }
133  
134 < void NPTf::moveB( void ){
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 >  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 );
143 > }
144 >
145 > template<typename T> void NPTf<T>::scaleSimBox( void ){
146 >
147    int i,j,k;
148 <  int atomIndex;
149 <  DirectionalAtom* dAtom;
150 <  double Tb[3];
151 <  double ji[3];
216 <  double press[9];
217 <  double instaTemp, instaVol;
218 <  double tt2, tb2;
219 <  double vx, vy, vz;
220 <  double scx, scy, scz;
221 <  const double p_convert = 1.63882576e8;
148 >  double scaleMat[3][3];
149 >  double eta2ij;
150 >  double bigScale, smallScale, offDiagMax;
151 >  double hm[3][3], hmnew[3][3];
152    
223  tt2 = tauThermostat * tauThermostat;
224  tb2 = tauBarostat * tauBarostat;
153  
226  instaTemp = tStats->getTemperature();
227  tStats->getPressureTensor(press);
154  
155 <  for (i=0; i < 9; i++) press[i] *= p_convert;
230 <
231 <  instaVol = tStats->getVolume();
232 <  
233 <  // first evolve chi a half step
155 >  // Scale the box after all the positions have been moved:
156    
157 <  chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2;
157 >  // Use a taylor expansion for eta products:  Hmat = Hmat . exp(dt * etaMat)
158 >  //  Hmat = Hmat . ( Ident + dt * etaMat  + dt^2 * etaMat*etaMat / 2)
159    
160 <  eta[0] += dt2 * instaVol * (press[0] - targetPressure) / (NkBT*tb2);
161 <  eta[1] += dt2 * instaVol * press[1] / (NkBT*tb2);
162 <  eta[2] += dt2 * instaVol * press[2] / (NkBT*tb2);
163 <  eta[3] += dt2 * instaVol * press[3] / (NkBT*tb2);
164 <  eta[4] += dt2 * instaVol * (press[4] - targetPressure) / (NkBT*tb2);
165 <  eta[5] += dt2 * instaVol * press[5] / (NkBT*tb2);
243 <  eta[6] += dt2 * instaVol * press[6] / (NkBT*tb2);
244 <  eta[7] += dt2 * instaVol * press[7] / (NkBT*tb2);
245 <  eta[8] += dt2 * instaVol * (press[8] - targetPressure) / (NkBT*tb2);
246 <
247 <  for( i=0; i<nAtoms; i++ ){
248 <    atomIndex = i * 3;
249 <
250 <    // velocity half step
251 <    
252 <    vx = vel[atomIndex];
253 <    vy = vel[atomIndex+1];
254 <    vz = vel[atomIndex+2];
255 <    
256 <    scx = (chi + eta[0])*vx + eta[1]*vy + eta[2]*vz;
257 <    scy = eta[3]*vx + (chi + eta[4])*vy + eta[5]*vz;
258 <    scz = eta[6]*vx + eta[7]*vy + (chi + eta[8])*vz;
259 <    
260 <    vx += dt2 * ((frc[atomIndex]  /atoms[i]->getMass())*eConvert - scx);
261 <    vy += dt2 * ((frc[atomIndex+1]/atoms[i]->getMass())*eConvert - scy);
262 <    vz += dt2 * ((frc[atomIndex+2]/atoms[i]->getMass())*eConvert - scz);
263 <
264 <    vel[atomIndex] = vx;
265 <    vel[atomIndex+1] = vy;
266 <    vel[atomIndex+2] = vz;
267 <    
268 <    if( atoms[i]->isDirectional() ){
160 >  bigScale = 1.0;
161 >  smallScale = 1.0;
162 >  offDiagMax = 0.0;
163 >  
164 >  for(i=0; i<3; i++){
165 >    for(j=0; j<3; j++){
166        
167 <      dAtom = (DirectionalAtom *)atoms[i];
167 >      // Calculate the matrix Product of the eta array (we only need
168 >      // the ij element right now):
169        
170 <      // get and convert the torque to body frame
170 >      eta2ij = 0.0;
171 >      for(k=0; k<3; k++){
172 >        eta2ij += eta[i][k] * eta[k][j];
173 >      }
174        
175 <      Tb[0] = dAtom->getTx();
176 <      Tb[1] = dAtom->getTy();
177 <      Tb[2] = dAtom->getTz();
178 <      
179 <      dAtom->lab2Body( Tb );
180 <      
181 <      // get the angular momentum, and complete the angular momentum
182 <      // half step
183 <      
283 <      ji[0] = dAtom->getJx();
284 <      ji[1] = dAtom->getJy();
285 <      ji[2] = dAtom->getJz();
286 <      
287 <      ji[0] += dt2 * (Tb[0] * eConvert - ji[0]*chi);
288 <      ji[1] += dt2 * (Tb[1] * eConvert - ji[1]*chi);
289 <      ji[2] += dt2 * (Tb[2] * eConvert - ji[2]*chi);
290 <      
291 <      dAtom->setJx( ji[0] );
292 <      dAtom->setJy( ji[1] );
293 <      dAtom->setJz( ji[2] );
175 >      scaleMat[i][j] = 0.0;
176 >      // identity matrix (see above):
177 >      if (i == j) scaleMat[i][j] = 1.0;
178 >      // Taylor expansion for the exponential truncated at second order:
179 >      scaleMat[i][j] += dt*eta[i][j]  + 0.5*dt*dt*eta2ij;
180 >
181 >      if (i != j)
182 >        if (fabs(scaleMat[i][j]) > offDiagMax)
183 >          offDiagMax = fabs(scaleMat[i][j]);
184      }
295  }
296 }
185  
186 < int NPTf::readyCheck() {
187 <
188 <  // First check to see if we have a target temperature.
301 <  // Not having one is fatal.
186 >    if (scaleMat[i][i] > bigScale) bigScale = scaleMat[i][i];
187 >    if (scaleMat[i][i] < smallScale) smallScale = scaleMat[i][i];
188 >  }
189    
190 <  if (!have_target_temp) {
190 >  if ((bigScale > 1.1) || (smallScale < 0.9)) {
191      sprintf( painCave.errMsg,
192 <             "NPTf error: You can't use the NPTf integrator\n"
193 <             "   without a targetTemp!\n"
194 <             );
192 >             "NPTf error: Attempting a Box scaling of more than 10 percent.\n"
193 >             " Check your tauBarostat, as it is probably too small!\n\n"
194 >             " scaleMat = [%lf\t%lf\t%lf]\n"
195 >             "            [%lf\t%lf\t%lf]\n"
196 >             "            [%lf\t%lf\t%lf]\n",
197 >             scaleMat[0][0],scaleMat[0][1],scaleMat[0][2],
198 >             scaleMat[1][0],scaleMat[1][1],scaleMat[1][2],
199 >             scaleMat[2][0],scaleMat[2][1],scaleMat[2][2]);
200      painCave.isFatal = 1;
201      simError();
202 <    return -1;
311 <  }
312 <
313 <  if (!have_target_pressure) {
202 >  } else if (offDiagMax > 0.1) {
203      sprintf( painCave.errMsg,
204 <             "NPTf error: You can't use the NPTf integrator\n"
205 <             "   without a targetPressure!\n"
206 <             );
204 >             "NPTf error: Attempting an off-diagonal Box scaling of more than 10 percent.\n"
205 >             " Check your tauBarostat, as it is probably too small!\n\n"
206 >             " scaleMat = [%lf\t%lf\t%lf]\n"
207 >             "            [%lf\t%lf\t%lf]\n"
208 >             "            [%lf\t%lf\t%lf]\n",
209 >             scaleMat[0][0],scaleMat[0][1],scaleMat[0][2],
210 >             scaleMat[1][0],scaleMat[1][1],scaleMat[1][2],
211 >             scaleMat[2][0],scaleMat[2][1],scaleMat[2][2]);
212      painCave.isFatal = 1;
213      simError();
214 <    return -1;
214 >  } else {
215 >    info->getBoxM(hm);
216 >    info->matMul3(hm, scaleMat, hmnew);
217 >    info->setBoxM(hmnew);
218    }
219 + }
220 +
221 + template<typename T> bool NPTf<T>::etaConverged() {
222 +  int i;
223 +  double diffEta, sumEta;
224 +
225 +  sumEta = 0;
226 +  for(i = 0; i < 3; i++)
227 +    sumEta += pow(prevEta[i][i] - eta[i][i], 2);    
228    
229 <  // We must set tauThermostat.
230 <  
231 <  if (!have_tau_thermostat) {
232 <    sprintf( painCave.errMsg,
327 <             "NPTf error: If you use the NPTf\n"
328 <             "   integrator, you must set tauThermostat.\n");
329 <    painCave.isFatal = 1;
330 <    simError();
331 <    return -1;
332 <  }    
229 >  diffEta = sqrt( sumEta / 3.0 );
230 >  
231 >  return ( diffEta <= etaTolerance );
232 > }
233  
234 <  // We must set tauBarostat.
235 <  
236 <  if (!have_tau_barostat) {
237 <    sprintf( painCave.errMsg,
238 <             "NPTf error: If you use the NPTf\n"
239 <             "   integrator, you must set tauBarostat.\n");
240 <    painCave.isFatal = 1;
241 <    simError();
242 <    return -1;
243 <  }    
234 > template<typename T> double NPTf<T>::getConservedQuantity(void){
235 >  
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 <  // We need NkBT a lot, so just set it here:
245 >  totalEnergy = tStats->getTotalE();
246  
247 <  NkBT = (double)info->ndf * kB * targetTemp;
247 >  thermostat_kinetic = fkBT * tt2 * chi * chi /
248 >    (2.0 * eConvert);
249  
250 <  return 1;
250 >  thermostat_potential = fkBT* integralOfChidt / eConvert;
251 >
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 >  barostat_potential = (targetPressure * tStats->getVolume() / p_convert) /
260 >    eConvert;
261 >
262 >  conservedQuantity = totalEnergy + thermostat_kinetic + thermostat_potential +
263 >    barostat_kinetic + barostat_potential;
264 >  
265 > //   cout.width(8);
266 > //   cout.precision(8);
267 >
268 > //   cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic <<
269 > //       "\t" << thermostat_potential << "\t" << barostat_kinetic <<
270 > //       "\t" << barostat_potential << "\t" << conservedQuantity << endl;
271 >
272 >  return conservedQuantity;
273 >  
274   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines