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

Comparing:
trunk/OOPSE/libmdtools/NPTi.cpp (file contents), Revision 746 by mmeineke, Thu Sep 4 21:48:35 2003 UTC vs.
branches/new-templateless/OOPSE/libmdtools/NPTi.cpp (file contents), Revision 850 by mmeineke, Mon Nov 3 22:07:17 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 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> NPTi<T>::NPTi ( SimInfo *theInfo, ForceFields* the_ff):
27 <  T( theInfo, the_ff )
26 > NPTi::NPTi ( SimInfo *theInfo, ForceFields* the_ff):
27 >  NPT( theInfo, the_ff )
28   {
29 <  chi = 0.0;
29 >  GenericData* data;
30 >  double *etaArray;
31 >
32    eta = 0.0;
33 <  have_tau_thermostat = 0;
34 <  have_tau_barostat = 0;
35 <  have_target_temp = 0;
36 <  have_target_pressure = 0;
33 >  oldEta = 0.0;
34 >
35 >  // retrieve eta array from simInfo if it exists
36 >  data = info->getProperty(ETAVALUE_ID);
37 >  if(data != NULL){
38 >    
39 >    int test = data->getDarray(etaArray);
40 >    
41 >    if( test == 9 ){
42 >      
43 >      eta = etaArray[0];
44 >      }    
45 >      delete[] etaArray;
46 >    }
47 >    else
48 >      std::cerr << "NPTi error: etaArray is not length 9 (actual = " << test
49 >                << ").\n"
50 >                << "            Simulation wil proceed with eta = 0;\n";
51 >  }
52   }
53  
54 < template<typename T> void NPTi<T>::moveA() {
55 <  
56 <  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];
54 > NPTi::~NPTi() {
55 >  //nothing for now
56 > }
57  
58 <  double rj[3];
59 <  double instaTemp, instaPress, instaVol;
60 <  double tt2, tb2, scaleFactor;
58 > void NPTi::resetIntegrator() {
59 >  eta = 0.0;
60 >  NPT::resetIntegrator();
61 > }
62  
63 <  tt2 = tauThermostat * tauThermostat;
64 <  tb2 = tauBarostat * tauBarostat;
49 <
50 <  instaTemp = tStats->getTemperature();
51 <  instaPress = tStats->getPressure();
52 <  instaVol = tStats->getVolume();
53 <  
54 <   // first evolve chi a half step
55 <  
56 <  chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2;
57 <  eta += dt2 * ( instaVol * (instaPress - targetPressure) /
63 > void NPTi::evolveEtaA() {
64 >  eta += dt2 * ( instaVol * (instaPress - targetPressure) /
65                   (p_convert*NkBT*tb2));
66 +  oldEta = eta;
67 + }
68  
69 <  for( i=0; i<nAtoms; i++ ){
61 <    atoms[i]->getVel( vel );
62 <    atoms[i]->getPos( pos );
63 <    atoms[i]->getFrc( frc );
69 > void NPTi::evolveEtaB() {
70  
71 <    mass = atoms[i]->getMass();
71 >  prevEta = eta;
72 >  eta = oldEta + dt2 * ( instaVol * (instaPress - targetPressure) /
73 >                 (p_convert*NkBT*tb2));
74 > }
75  
76 <    for (j=0; j < 3; j++) {
77 <      vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*(chi+eta));
69 <      rj[j] = pos[j];
70 <    }
76 > void NPTi::getVelScaleA(double sc[3], double vel[3]) {
77 >  int i;
78  
79 <    atoms[i]->setVel( vel );
79 >  for(i=0; i<3; i++) sc[i] = vel[i] * ( chi + eta );
80 > }
81  
82 <    info->wrapVector(rj);
82 > void NPTi::getVelScaleB(double sc[3], int index ){
83 >  int i;
84  
85 <    for (j = 0; j < 3; j++)
86 <      pos[j] += dt * (vel[j] + eta*rj[j]);
85 >  for(i=0; i<3; i++) sc[i] = oldVel[index*3 + i] * ( chi + eta );
86 > }
87  
79    atoms[i]->setPos( pos );
88  
89 <    if( atoms[i]->isDirectional() ){
89 > void NPTi::getPosScale(double pos[3], double COM[3],
90 >                                               int index, double sc[3]){
91 >  int j;
92  
93 <      dAtom = (DirectionalAtom *)atoms[i];
94 <          
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
93 >  for(j=0; j<3; j++)
94 >    sc[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j];
95  
96 <      dAtom->getJ( ji );
96 >  for(j=0; j<3; j++)
97 >    sc[j] *= eta;
98 > }
99  
100 <      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
100 > void NPTi::scaleSimBox( void ){
101  
102 <      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 );
102 >  double scaleFactor;
103  
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  
104    scaleFactor = exp(dt*eta);
105  
106    if ((scaleFactor > 1.1) || (scaleFactor < 0.9)) {
# Line 138 | Line 111 | template<typename T> void NPTi<T>::moveA() {
111               );
112      painCave.isFatal = 1;
113      simError();
114 <  } else {        
115 <    info->scaleBox(exp(dt*eta));      
114 >  } else {
115 >    info->scaleBox(scaleFactor);
116    }
117  
118   }
119  
120 < template<typename T> void NPTi<T>::moveB( void ){
120 > bool NPTi::etaConverged() {
121  
122 <  int i, j;
123 <  DirectionalAtom* dAtom;
151 <  double Tb[3], ji[3];
152 <  double vel[3], frc[3];
153 <  double mass;
122 >  return ( fabs(prevEta - eta) <= etaTolerance );
123 > }
124  
125 <  double instaTemp, instaPress, instaVol;
156 <  double tt2, tb2;
157 <  
158 <  tt2 = tauThermostat * tauThermostat;
159 <  tb2 = tauBarostat * tauBarostat;
125 > double NPTi::getConservedQuantity(void){
126  
127 <  instaTemp = tStats->getTemperature();
128 <  instaPress = tStats->getPressure();
129 <  instaVol = tStats->getVolume();
127 >  double conservedQuantity;
128 >  double Energy;
129 >  double thermostat_kinetic;
130 >  double thermostat_potential;
131 >  double barostat_kinetic;
132 >  double barostat_potential;
133  
134 <  chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2;
166 <  eta += dt2 * ( instaVol * (instaPress - targetPressure) /
167 <                 (p_convert*NkBT*tb2));
168 <  
169 <  for( i=0; i<nAtoms; i++ ){
134 >  Energy = tStats->getTotalE();
135  
136 <    atoms[i]->getVel( vel );
137 <    atoms[i]->getFrc( frc );
136 >  thermostat_kinetic = fkBT* tt2 * chi * chi /
137 >    (2.0 * eConvert);
138  
139 <    mass = atoms[i]->getMass();
139 >  thermostat_potential = fkBT* integralOfChidt / eConvert;
140  
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 );
141  
142 <    if( atoms[i]->isDirectional() ){
142 >  barostat_kinetic = 3.0 * NkBT * tb2 * eta * eta /
143 >    (2.0 * eConvert);
144  
145 <      dAtom = (DirectionalAtom *)atoms[i];
145 >  barostat_potential = (targetPressure * tStats->getVolume() / p_convert) /
146 >    eConvert;
147  
148 <      // get and convert the torque to body frame      
148 >  conservedQuantity = Energy + thermostat_kinetic + thermostat_potential +
149 >    barostat_kinetic + barostat_potential;
150  
151 <      dAtom->getTrq( Tb );
152 <      dAtom->lab2Body( Tb );
151 > //   cout.width(8);
152 > //   cout.precision(8);
153  
154 <      // get the angular momentum, and propagate a half step
155 <
156 <      dAtom->getJ( ji );
157 <
195 <      for (j=0; j < 3; j++)
196 <        ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);    
197 <
198 <      dAtom->setJ( ji );
199 <    }
200 <  }
154 > //   cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic <<
155 > //       "\t" << thermostat_potential << "\t" << barostat_kinetic <<
156 > //       "\t" << barostat_potential << "\t" << conservedQuantity << endl;
157 >  return conservedQuantity;
158   }
159  
160 < template<typename T> void NPTi<T>::resetIntegrator() {
161 <  chi = 0.0;
162 <  eta = 0.0;
163 < }
160 > string NPTi::getAdditionalParameters(void){
161 >  string parameters;
162 >  const int BUFFERSIZE = 2000; // size of the read buffer
163 >  char buffer[BUFFERSIZE];
164  
165 < template<typename T> int NPTi<T>::readyCheck() {
165 >  sprintf(buffer,"\t%lf\t%lf;", chi, integralOfChidt);
166 >  parameters += buffer;
167  
168 <  //check parent's readyCheck() first
169 <  if (T::readyCheck() == -1)
212 <    return -1;
213 <
214 <  // First check to see if we have a target temperature.
215 <  // Not having one is fatal.
216 <  
217 <  if (!have_target_temp) {
218 <    sprintf( painCave.errMsg,
219 <             "NPTi error: You can't use the NPTi integrator\n"
220 <             "   without a targetTemp!\n"
221 <             );
222 <    painCave.isFatal = 1;
223 <    simError();
224 <    return -1;
225 <  }
168 >  sprintf(buffer,"\t%lf\t0\t0;", eta);
169 >  parameters += buffer;
170  
171 <  if (!have_target_pressure) {
172 <    sprintf( painCave.errMsg,
229 <             "NPTi error: You can't use the NPTi integrator\n"
230 <             "   without a targetPressure!\n"
231 <             );
232 <    painCave.isFatal = 1;
233 <    simError();
234 <    return -1;
235 <  }
236 <  
237 <  // We must set tauThermostat.
238 <  
239 <  if (!have_tau_thermostat) {
240 <    sprintf( painCave.errMsg,
241 <             "NPTi error: If you use the NPTi\n"
242 <             "   integrator, you must set tauThermostat.\n");
243 <    painCave.isFatal = 1;
244 <    simError();
245 <    return -1;
246 <  }    
171 >  sprintf(buffer,"\t0\t%lf\t0;", eta);
172 >  parameters += buffer;
173  
174 <  // We must set tauBarostat.
175 <  
250 <  if (!have_tau_barostat) {
251 <    sprintf( painCave.errMsg,
252 <             "NPTi error: If you use the NPTi\n"
253 <             "   integrator, you must set tauBarostat.\n");
254 <    painCave.isFatal = 1;
255 <    simError();
256 <    return -1;
257 <  }    
174 >  sprintf(buffer,"\t0\t0\t%lf;", eta);
175 >  parameters += buffer;
176  
177 <  // We need NkBT a lot, so just set it here:
177 >  return parameters;
178  
261  NkBT = (double)info->ndf * kB * targetTemp;
262
263  return 1;
179   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines