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 658 by tim, Thu Jul 31 15:35:07 2003 UTC vs.
Revision 847 by mmeineke, Fri Oct 31 18:28:52 2003 UTC

# Line 1 | Line 1
1 < #include <cmath>
1 > #include <math.h>
2   #include "Atom.hpp"
3   #include "SRI.hpp"
4   #include "AbstractClasses.hpp"
# Line 7 | Line 7
7   #include "Thermo.hpp"
8   #include "ReadWrite.hpp"
9   #include "Integrator.hpp"
10 < #include "simError.h"
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:
18   //
19   //    Melchionna, S., Ciccotti, G., and Holian, B. L., 1993,
20 < //       Molec. Phys., 78, 533.
20 > //       Molec. Phys., 78, 533.
21   //
22   //           and
23 < //
23 > //
24   //    Hoover, W. G., 1986, Phys. Rev. A, 34, 2499.
25  
26   template<typename T> NPTf<T>::NPTf ( SimInfo *theInfo, ForceFields* the_ff):
27    T( theInfo, the_ff )
28   {
29 +  GenericData* data;
30 +  DoubleArrayData * etaValue;
31 +  vector<double> etaArray;
32 +  int i,j;
33 +
34 +  for(i = 0; i < 3; i++){
35 +    for (j = 0; j < 3; j++){
36 +
37 +      eta[i][j] = 0.0;
38 +      oldEta[i][j] = 0.0;
39 +    }
40 +  }
41 +
42 +    // retrieve eta array from simInfo if it exists
43 +    data = info->getProperty(ETAVALUE_ID);
44 +    if(data){
45 +      etaValue = dynamic_cast<DoubleArrayData*>(data);
46 +
47 +      if(etaValue){
48 +        etaArray = etaValue->getData();
49 +
50 +        for(i = 0; i < 3; i++){
51 +          for (j = 0; j < 3; j++){
52 +            eta[i][j] = etaArray[3*i+j];
53 +            oldEta[i][j] = eta[i][j];
54 +          }
55 +        }
56 +
57 +      }
58 +    }
59 +
60 + }
61 +
62 + template<typename T> NPTf<T>::~NPTf() {
63 +
64 +  // empty for now
65 + }
66 +
67 + template<typename T> void NPTf<T>::resetIntegrator() {
68 +
69    int i, j;
27  chi = 0.0;
70  
71 <  for(i = 0; i < 3; i++)
72 <    for (j = 0; j < 3; j++)
71 >  for(i = 0; i < 3; i++)
72 >    for (j = 0; j < 3; j++)
73        eta[i][j] = 0.0;
74  
75 <  have_tau_thermostat = 0;
34 <  have_tau_barostat = 0;
35 <  have_target_temp = 0;
36 <  have_target_pressure = 0;
75 >  T::resetIntegrator();
76   }
77  
78 < template<typename T> void NPTf<T>::moveA() {
40 <  
41 <  int i, j, k;
42 <  DirectionalAtom* dAtom;
43 <  double Tb[3], ji[3];
44 <  double A[3][3], I[3][3];
45 <  double angle, mass;
46 <  double vel[3], pos[3], frc[3];
78 > template<typename T> void NPTf<T>::evolveEtaA() {
79  
80 <  double rj[3];
49 <  double instaTemp, instaPress, instaVol;
50 <  double tt2, tb2;
51 <  double sc[3];
52 <  double eta2ij;
53 <  double press[3][3], vScale[3][3], hm[3][3], hmnew[3][3], scaleMat[3][3];
54 <  double bigScale, smallScale, offDiagMax;
80 >  int i, j;
81  
82 <  tt2 = tauThermostat * tauThermostat;
83 <  tb2 = tauBarostat * tauBarostat;
82 >  for(i = 0; i < 3; i ++){
83 >    for(j = 0; j < 3; j++){
84 >      if( i == j)
85 >        eta[i][j] += dt2 *  instaVol *
86 >          (press[i][j] - targetPressure/p_convert) / (NkBT*tb2);
87 >      else
88 >        eta[i][j] += dt2 * instaVol * press[i][j] / (NkBT*tb2);
89 >    }
90 >  }
91  
92 <  instaTemp = tStats->getTemperature();
93 <  tStats->getPressureTensor(press);
94 <  instaVol = tStats->getVolume();
95 <  
63 <  // first evolve chi a half step
64 <  
65 <  chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2;
92 >  for(i = 0; i < 3; i++)
93 >    for (j = 0; j < 3; j++)
94 >      oldEta[i][j] = eta[i][j];
95 > }
96  
97 + template<typename T> void NPTf<T>::evolveEtaB() {
98 +
99 +  int i,j;
100 +
101 +  for(i = 0; i < 3; i++)
102 +    for (j = 0; j < 3; j++)
103 +      prevEta[i][j] = eta[i][j];
104 +
105 +  for(i = 0; i < 3; i ++){
106 +    for(j = 0; j < 3; j++){
107 +      if( i == j) {
108 +        eta[i][j] = oldEta[i][j] + dt2 *  instaVol *
109 +          (press[i][j] - targetPressure/p_convert) / (NkBT*tb2);
110 +      } else {
111 +        eta[i][j] = oldEta[i][j] + dt2 * instaVol * press[i][j] / (NkBT*tb2);
112 +      }
113 +    }
114 +  }
115 + }
116 +
117 + template<typename T> void NPTf<T>::getVelScaleA(double sc[3], double vel[3]) {
118 +  int i,j;
119 +  double vScale[3][3];
120 +
121    for (i = 0; i < 3; i++ ) {
122      for (j = 0; j < 3; j++ ) {
123 <      if (i == j) {
70 <        
71 <        eta[i][j] += dt2 * instaVol *
72 <          (press[i][j] - targetPressure/p_convert) / (NkBT*tb2);
73 <        
74 <        vScale[i][j] = eta[i][j] + chi;
75 <        
76 <      } else {
77 <        
78 <        eta[i][j] += dt2 * instaVol * press[i][j] / (NkBT*tb2);
123 >      vScale[i][j] = eta[i][j];
124  
125 <        vScale[i][j] = eta[i][j];
126 <        
125 >      if (i == j) {
126 >        vScale[i][j] += chi;
127        }
128      }
129    }
130  
131 <  for( i=0; i<nAtoms; i++ ){
131 >  info->matVecMul3( vScale, vel, sc );
132 > }
133  
134 <    atoms[i]->getVel( vel );
135 <    atoms[i]->getPos( pos );
136 <    atoms[i]->getFrc( frc );
134 > template<typename T> void NPTf<T>::getVelScaleB(double sc[3], int index ){
135 >  int i,j;
136 >  double myVel[3];
137 >  double vScale[3][3];
138  
139 <    mass = atoms[i]->getMass();
140 <    
141 <    // velocity half step
142 <        
143 <    info->matVecMul3( vScale, vel, sc );
144 <    
145 <    for (j = 0; j < 3; j++) {
99 <      vel[j] += dt2 * ((frc[j]  / mass) * eConvert - sc[j]);
100 <      rj[j] = pos[j];
139 >  for (i = 0; i < 3; i++ ) {
140 >    for (j = 0; j < 3; j++ ) {
141 >      vScale[i][j] = eta[i][j];
142 >
143 >      if (i == j) {
144 >        vScale[i][j] += chi;
145 >      }
146      }
147 +  }
148  
149 <    atoms[i]->setVel( vel );
149 >  for (j = 0; j < 3; j++)
150 >    myVel[j] = oldVel[3*index + j];
151  
152 <    // position whole step    
152 >  info->matVecMul3( vScale, myVel, sc );
153 > }
154  
155 <    info->wrapVector(rj);
155 > template<typename T> void NPTf<T>::getPosScale(double pos[3], double COM[3],
156 >                                               int index, double sc[3]){
157 >  int j;
158 >  double rj[3];
159  
160 <    info->matVecMul3( eta, rj, sc );
160 >  for(j=0; j<3; j++)
161 >    rj[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j];
162  
163 <    for (j = 0; j < 3; j++ )
164 <      pos[j] += dt * (vel[j] + sc[j]);
163 >  info->matVecMul3( eta, rj, sc );
164 > }
165  
166 <    atoms[i]->setPos( pos );
115 <  
116 <    if( atoms[i]->isDirectional() ){
166 > template<typename T> void NPTf<T>::scaleSimBox( void ){
167  
168 <      dAtom = (DirectionalAtom *)atoms[i];
169 <          
170 <      // get and convert the torque to body frame
171 <      
172 <      dAtom->getTrq( Tb );
123 <      dAtom->lab2Body( Tb );
124 <      
125 <      // get the angular momentum, and propagate a half step
168 >  int i,j,k;
169 >  double scaleMat[3][3];
170 >  double eta2ij;
171 >  double bigScale, smallScale, offDiagMax;
172 >  double hm[3][3], hmnew[3][3];
173  
127      dAtom->getJ( ji );
174  
129      for (j=0; j < 3; j++)
130        ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);
131      
132      // use the angular velocities to propagate the rotation matrix a
133      // full time step
175  
176 <      dAtom->getA(A);
136 <      dAtom->getI(I);
137 <    
138 <      // rotate about the x-axis      
139 <      angle = dt2 * ji[0] / I[0][0];
140 <      this->rotate( 1, 2, angle, ji, A );
176 >  // Scale the box after all the positions have been moved:
177  
142      // rotate about the y-axis
143      angle = dt2 * ji[1] / I[1][1];
144      this->rotate( 2, 0, angle, ji, A );
145      
146      // rotate about the z-axis
147      angle = dt * ji[2] / I[2][2];
148      this->rotate( 0, 1, angle, ji, A);
149      
150      // rotate about the y-axis
151      angle = dt2 * ji[1] / I[1][1];
152      this->rotate( 2, 0, angle, ji, A );
153      
154       // rotate about the x-axis
155      angle = dt2 * ji[0] / I[0][0];
156      this->rotate( 1, 2, angle, ji, A );
157      
158      dAtom->setJ( ji );
159      dAtom->setA( A  );    
160    }                    
161  }
162  
163  // Scale the box after all the positions have been moved:
164  
178    // Use a taylor expansion for eta products:  Hmat = Hmat . exp(dt * etaMat)
179    //  Hmat = Hmat . ( Ident + dt * etaMat  + dt^2 * etaMat*etaMat / 2)
180 <  
180 >
181    bigScale = 1.0;
182    smallScale = 1.0;
183    offDiagMax = 0.0;
184 <  
184 >
185    for(i=0; i<3; i++){
186      for(j=0; j<3; j++){
187 <      
187 >
188        // Calculate the matrix Product of the eta array (we only need
189        // the ij element right now):
190 <      
190 >
191        eta2ij = 0.0;
192        for(k=0; k<3; k++){
193          eta2ij += eta[i][k] * eta[k][j];
194        }
195 <      
195 >
196        scaleMat[i][j] = 0.0;
197        // identity matrix (see above):
198        if (i == j) scaleMat[i][j] = 1.0;
# Line 187 | Line 200 | template<typename T> void NPTf<T>::moveA() {
200        scaleMat[i][j] += dt*eta[i][j]  + 0.5*dt*dt*eta2ij;
201  
202        if (i != j)
203 <        if (fabs(scaleMat[i][j]) > offDiagMax)
203 >        if (fabs(scaleMat[i][j]) > offDiagMax)
204            offDiagMax = fabs(scaleMat[i][j]);
192      
205      }
206  
207      if (scaleMat[i][i] > bigScale) bigScale = scaleMat[i][i];
208      if (scaleMat[i][i] < smallScale) smallScale = scaleMat[i][i];
209    }
210 <  
210 >
211    if ((bigScale > 1.1) || (smallScale < 0.9)) {
212      sprintf( painCave.errMsg,
213               "NPTf error: Attempting a Box scaling of more than 10 percent.\n"
# Line 225 | Line 237 | template<typename T> void NPTf<T>::moveA() {
237      info->matMul3(hm, scaleMat, hmnew);
238      info->setBoxM(hmnew);
239    }
228  
240   }
241  
242 < template<typename T> void NPTf<T>::moveB( void ){
242 > template<typename T> bool NPTf<T>::etaConverged() {
243 >  int i;
244 >  double diffEta, sumEta;
245  
246 <  int i, j;
247 <  DirectionalAtom* dAtom;
248 <  double Tb[3], ji[3];
236 <  double vel[3], frc[3];
237 <  double mass;
246 >  sumEta = 0;
247 >  for(i = 0; i < 3; i++)
248 >    sumEta += pow(prevEta[i][i] - eta[i][i], 2);
249  
250 <  double instaTemp, instaPress, instaVol;
240 <  double tt2, tb2;
241 <  double sc[3];
242 <  double press[3][3], vScale[3][3];
243 <  
244 <  tt2 = tauThermostat * tauThermostat;
245 <  tb2 = tauBarostat * tauBarostat;
250 >  diffEta = sqrt( sumEta / 3.0 );
251  
252 <  instaTemp = tStats->getTemperature();
253 <  tStats->getPressureTensor(press);
249 <  instaVol = tStats->getVolume();
250 <  
251 <  // first evolve chi a half step
252 <  
253 <  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) {
252 >  return ( diffEta <= etaTolerance );
253 > }
254  
255 <        eta[i][j] += dt2 * instaVol *
260 <          (press[i][j] - targetPressure/p_convert) / (NkBT*tb2);
255 > template<typename T> double NPTf<T>::getConservedQuantity(void){
256  
257 <        vScale[i][j] = eta[i][j] + chi;
258 <        
259 <      } else {
260 <        
261 <        eta[i][j] += dt2 * instaVol * press[i][j] / (NkBT*tb2);
257 >  double conservedQuantity;
258 >  double totalEnergy;
259 >  double thermostat_kinetic;
260 >  double thermostat_potential;
261 >  double barostat_kinetic;
262 >  double barostat_potential;
263 >  double trEta;
264 >  double a[3][3], b[3][3];
265  
266 <        vScale[i][j] = eta[i][j];
269 <        
270 <      }
271 <    }
272 <  }
266 >  totalEnergy = tStats->getTotalE();
267  
268 <  for( i=0; i<nAtoms; i++ ){
268 >  thermostat_kinetic = fkBT * tt2 * chi * chi /
269 >    (2.0 * eConvert);
270  
271 <    atoms[i]->getVel( vel );
277 <    atoms[i]->getFrc( frc );
271 >  thermostat_potential = fkBT* integralOfChidt / eConvert;
272  
273 <    mass = atoms[i]->getMass();
274 <    
275 <    // 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 <    }
273 >  info->transposeMat3(eta, a);
274 >  info->matMul3(a, eta, b);
275 >  trEta = info->matTrace3(b);
276  
277 <    atoms[i]->setVel( vel );
278 <    
291 <    if( atoms[i]->isDirectional() ){
277 >  barostat_kinetic = NkBT * tb2 * trEta /
278 >    (2.0 * eConvert);
279  
280 <      dAtom = (DirectionalAtom *)atoms[i];
281 <          
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 );
280 >  barostat_potential = (targetPressure * tStats->getVolume() / p_convert) /
281 >    eConvert;
282  
283 <    }                    
284 <  }
311 < }
283 >  conservedQuantity = totalEnergy + thermostat_kinetic + thermostat_potential +
284 >    barostat_kinetic + barostat_potential;
285  
286 < template<typename T> int NPTf<T>::readyCheck() {
286 > //   cout.width(8);
287 > //   cout.precision(8);
288  
289 <  //check parent's readyCheck() first
290 <  if (T::readyCheck() == -1)
291 <    return -1;
318 <
319 <  // First check to see if we have a target temperature.
320 <  // Not having one is fatal.
321 <  
322 <  if (!have_target_temp) {
323 <    sprintf( painCave.errMsg,
324 <             "NPTf error: You can't use the NPTf integrator\n"
325 <             "   without a targetTemp!\n"
326 <             );
327 <    painCave.isFatal = 1;
328 <    simError();
329 <    return -1;
330 <  }
289 > //   cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic <<
290 > //       "\t" << thermostat_potential << "\t" << barostat_kinetic <<
291 > //       "\t" << barostat_potential << "\t" << conservedQuantity << endl;
292  
293 <  if (!have_target_pressure) {
333 <    sprintf( painCave.errMsg,
334 <             "NPTf error: You can't use the NPTf integrator\n"
335 <             "   without a targetPressure!\n"
336 <             );
337 <    painCave.isFatal = 1;
338 <    simError();
339 <    return -1;
340 <  }
341 <  
342 <  // We must set tauThermostat.
343 <  
344 <  if (!have_tau_thermostat) {
345 <    sprintf( painCave.errMsg,
346 <             "NPTf error: If you use the NPTf\n"
347 <             "   integrator, you must set tauThermostat.\n");
348 <    painCave.isFatal = 1;
349 <    simError();
350 <    return -1;
351 <  }    
293 >  return conservedQuantity;
294  
295 <  // We must set tauBarostat.
354 <  
355 <  if (!have_tau_barostat) {
356 <    sprintf( painCave.errMsg,
357 <             "NPTf error: If you use the NPTf\n"
358 <             "   integrator, you must set tauBarostat.\n");
359 <    painCave.isFatal = 1;
360 <    simError();
361 <    return -1;
362 <  }    
295 > }
296  
297 <  // We need NkBT a lot, so just set it here:
297 > template<typename T> string NPTf<T>::getAdditionalParameters(void){
298 >  string parameters;
299 >  const int BUFFERSIZE = 2000; // size of the read buffer
300 >  char buffer[BUFFERSIZE];
301  
302 <  NkBT = (double)info->ndf * kB * targetTemp;
302 >  sprintf(buffer,"\t%lf\t%lf;", chi, integralOfChidt);
303 >  parameters += buffer;
304  
305 <  return 1;
305 >  for(int i = 0; i < 3; i++){
306 >    sprintf(buffer,"\t%lf\t%lf\t%lf;", eta[3*i], eta[3*i+1], eta[3*i+2]);
307 >    parameters += buffer;
308 >  }
309 >
310 >  return parameters;
311 >
312   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines