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 577 by gezelter, Wed Jul 9 01:41:11 2003 UTC vs.
Revision 857 by mmeineke, Fri Nov 7 17:09:48 2003 UTC

# Line 1 | Line 1
1 + #include <math.h>
2   #include "Atom.hpp"
3   #include "SRI.hpp"
4   #include "AbstractClasses.hpp"
# Line 6 | 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 < NPTi::NPTi ( SimInfo *theInfo, ForceFields* the_ff):
27 <  Integrator( theInfo, the_ff )
26 > template<typename T> NPTi<T>::NPTi ( SimInfo *theInfo, ForceFields* the_ff):
27 >  T( theInfo, the_ff )
28   {
29 <  chi = 0.0;
29 >  GenericData* data;
30 >  DoubleArrayData * etaValue;
31 >  vector<double> etaArray;
32 >
33    eta = 0.0;
34 <  have_tau_thermostat = 0;
35 <  have_tau_barostat = 0;
36 <  have_target_temp = 0;
37 <  have_target_pressure = 0;
34 >  oldEta = 0.0;
35 >
36 >  if( theInfo->useInitXSstate ){
37 >    // retrieve eta from simInfo if
38 >    data = info->getProperty(ETAVALUE_ID);
39 >    if(data){
40 >      etaValue = dynamic_cast<DoubleArrayData*>(data);
41 >      
42 >      if(etaValue){
43 >        etaArray = etaValue->getData();
44 >        eta = etaArray[0];
45 >        oldEta = eta;
46 >      }
47 >    }
48 >  }
49   }
50  
51 < void NPTi::moveA() {
52 <  
53 <  int i,j,k;
36 <  int atomIndex, aMatIndex;
37 <  DirectionalAtom* dAtom;
38 <  double Tb[3];
39 <  double ji[3];
40 <  double rj[3];
41 <  double instaTemp, instaPress, instaVol;
42 <  double tt2, tb2;
43 <  double angle;
51 > template<typename T> NPTi<T>::~NPTi() {
52 >  //nothing for now
53 > }
54  
55 <  tt2 = tauThermostat * tauThermostat;
56 <  tb2 = tauBarostat * tauBarostat;
55 > template<typename T> void NPTi<T>::resetIntegrator() {
56 >  eta = 0.0;
57 >  T::resetIntegrator();
58 > }
59  
60 <  instaTemp = tStats->getTemperature();
61 <  instaPress = tStats->getPressure();
62 <  instaVol = tStats->getVolume();
63 <  
64 <  // first evolve chi a half step
53 <  
54 <  chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2;
55 <  eta += dt2 * ( instaVol * (instaPress - targetPressure) / (NkBT*tb2));
60 > template<typename T> void NPTi<T>::evolveEtaA() {
61 >  eta += dt2 * ( instaVol * (instaPress - targetPressure) /
62 >                 (p_convert*NkBT*tb2));
63 >  oldEta = eta;
64 > }
65  
66 <  for( i=0; i<nAtoms; i++ ){
58 <    atomIndex = i * 3;
59 <    aMatIndex = i * 9;
60 <    
61 <    // velocity half step
62 <    for( j=atomIndex; j<(atomIndex+3); j++ )
63 <      vel[j] += dt2 * ((frc[j]/atoms[i]->getMass())*eConvert
64 <                       - vel[j]*(chi+eta));
66 > template<typename T> void NPTi<T>::evolveEtaB() {
67  
68 <    // position whole step    
68 >  prevEta = eta;
69 >  eta = oldEta + dt2 * ( instaVol * (instaPress - targetPressure) /
70 >                 (p_convert*NkBT*tb2));
71 > }
72  
73 <    rj[0] = pos[atomIndex];
74 <    rj[1] = pos[atomIndex+1];
75 <    rj[2] = pos[atomIndex+2];
71 <    
72 <    info->wrapVector(rj);
73 > template<typename T> void NPTi<T>::calcVelScale(void) {
74 >  vScale = chi + eta;
75 > }
76  
77 <    pos[atomIndex] += dt * (vel[atomIndex] + eta*rj[0]);
78 <    pos[atomIndex+1] += dt * (vel[atomIndex+1] + eta*rj[1]);
76 <    pos[atomIndex+2] += dt * (vel[atomIndex+2] + eta*rj[2]);
77 <  
78 <    if( atoms[i]->isDirectional() ){
77 > template<typename T> void NPTi<T>::getVelScaleA(double sc[3], double vel[3]) {
78 >  int i;
79  
80 <      dAtom = (DirectionalAtom *)atoms[i];
81 <          
82 <      // get and convert the torque to body frame
83 <      
84 <      Tb[0] = dAtom->getTx();
85 <      Tb[1] = dAtom->getTy();
86 <      Tb[2] = dAtom->getTz();
87 <      
88 <      dAtom->lab2Body( Tb );
89 <      
90 <      // get the angular momentum, and propagate a half step
80 >  for(i=0; i<3; i++) sc[i] = vel[i] * vScale;
81 > }
82  
83 <      ji[0] = dAtom->getJx();
84 <      ji[1] = dAtom->getJy();
94 <      ji[2] = dAtom->getJz();
95 <      
96 <      ji[0] += dt2 * (Tb[0] * eConvert - ji[0]*chi);
97 <      ji[1] += dt2 * (Tb[1] * eConvert - ji[1]*chi);
98 <      ji[2] += dt2 * (Tb[2] * eConvert - ji[2]*chi);
99 <      
100 <      // use the angular velocities to propagate the rotation matrix a
101 <      // full time step
102 <      
103 <      // rotate about the x-axis      
104 <      angle = dt2 * ji[0] / dAtom->getIxx();
105 <      this->rotate( 1, 2, angle, ji, &Amat[aMatIndex] );
106 <      
107 <      // rotate about the y-axis
108 <      angle = dt2 * ji[1] / dAtom->getIyy();
109 <      this->rotate( 2, 0, angle, ji, &Amat[aMatIndex] );
110 <      
111 <      // rotate about the z-axis
112 <      angle = dt * ji[2] / dAtom->getIzz();
113 <      this->rotate( 0, 1, angle, ji, &Amat[aMatIndex] );
114 <      
115 <      // rotate about the y-axis
116 <      angle = dt2 * ji[1] / dAtom->getIyy();
117 <      this->rotate( 2, 0, angle, ji, &Amat[aMatIndex] );
118 <      
119 <       // rotate about the x-axis
120 <      angle = dt2 * ji[0] / dAtom->getIxx();
121 <      this->rotate( 1, 2, angle, ji, &Amat[aMatIndex] );
122 <      
123 <      dAtom->setJx( ji[0] );
124 <      dAtom->setJy( ji[1] );
125 <      dAtom->setJz( ji[2] );
126 <    }
127 <    
128 <  }
129 <  // Scale the box after all the positions have been moved:
83 > template<typename T> void NPTi<T>::getVelScaleB(double sc[3], int index ){
84 >  int i;
85  
86 <  info->scaleBox(exp(dt*eta));
132 <
86 >  for(i=0; i<3; i++) sc[i] = oldVel[index*3 + i] * vScale;
87   }
88  
135 void NPTi::moveB( void ){
136  int i,j,k;
137  int atomIndex;
138  DirectionalAtom* dAtom;
139  double Tb[3];
140  double ji[3];
141  double instaTemp, instaPress, instaVol;
142  double tt2, tb2;
143  
144  tt2 = tauThermostat * tauThermostat;
145  tb2 = tauBarostat * tauBarostat;
89  
90 <  instaTemp = tStats->getTemperature();
91 <  instaPress = tStats->getPressure();
92 <  instaVol = tStats->getVolume();
90 > template<typename T> void NPTi<T>::getPosScale(double pos[3], double COM[3],
91 >                                               int index, double sc[3]){
92 >  int j;
93  
94 <  chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2;
95 <  eta += dt2 * ( instaVol * (instaPress - targetPressure) / (NkBT*tb2));
96 <  
97 <  for( i=0; i<nAtoms; i++ ){
98 <    atomIndex = i * 3;
156 <    
157 <    // velocity half step
158 <    for( j=atomIndex; j<(atomIndex+3); j++ )
159 <    for( j=atomIndex; j<(atomIndex+3); j++ )
160 <      vel[j] += dt2 * ((frc[j]/atoms[i]->getMass())*eConvert
161 <                       - vel[j]*(chi+eta));
162 <    
163 <    if( atoms[i]->isDirectional() ){
164 <      
165 <      dAtom = (DirectionalAtom *)atoms[i];
166 <      
167 <      // get and convert the torque to body frame
168 <      
169 <      Tb[0] = dAtom->getTx();
170 <      Tb[1] = dAtom->getTy();
171 <      Tb[2] = dAtom->getTz();
172 <      
173 <      dAtom->lab2Body( Tb );
174 <      
175 <      // get the angular momentum, and complete the angular momentum
176 <      // half step
177 <      
178 <      ji[0] = dAtom->getJx();
179 <      ji[1] = dAtom->getJy();
180 <      ji[2] = dAtom->getJz();
181 <      
182 <      ji[0] += dt2 * (Tb[0] * eConvert - ji[0]*chi);
183 <      ji[1] += dt2 * (Tb[1] * eConvert - ji[1]*chi);
184 <      ji[2] += dt2 * (Tb[2] * eConvert - ji[2]*chi);
185 <      
186 <      dAtom->setJx( ji[0] );
187 <      dAtom->setJy( ji[1] );
188 <      dAtom->setJz( ji[2] );
189 <    }
190 <  }
94 >  for(j=0; j<3; j++)
95 >    sc[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j];
96 >
97 >  for(j=0; j<3; j++)
98 >    sc[j] *= eta;
99   }
100  
101 < int NPTi::readyCheck() {
194 <
195 <  // First check to see if we have a target temperature.
196 <  // Not having one is fatal.
197 <  
198 <  if (!have_target_temp) {
199 <    sprintf( painCave.errMsg,
200 <             "NPTi error: You can't use the NPTi integrator\n"
201 <             "   without a targetTemp!\n"
202 <             );
203 <    painCave.isFatal = 1;
204 <    simError();
205 <    return -1;
206 <  }
101 > template<typename T> void NPTi<T>::scaleSimBox( void ){
102  
103 <  if (!have_target_pressure) {
103 >  double scaleFactor;
104 >
105 >  scaleFactor = exp(dt*eta);
106 >
107 >  if ((scaleFactor > 1.1) || (scaleFactor < 0.9)) {
108      sprintf( painCave.errMsg,
109 <             "NPTi error: You can't use the NPTi integrator\n"
110 <             "   without a targetPressure!\n"
109 >             "NPTi error: Attempting a Box scaling of more than 10 percent"
110 >             " check your tauBarostat, as it is probably too small!\n"
111 >             " eta = %lf, scaleFactor = %lf\n", eta, scaleFactor
112               );
113      painCave.isFatal = 1;
114      simError();
115 <    return -1;
115 >  } else {
116 >    info->scaleBox(scaleFactor);
117    }
217  
218  // We must set tauThermostat.
219  
220  if (!have_tau_thermostat) {
221    sprintf( painCave.errMsg,
222             "NPTi error: If you use the NPTi\n"
223             "   integrator, you must set tauThermostat.\n");
224    painCave.isFatal = 1;
225    simError();
226    return -1;
227  }    
118  
119 <  // We must set tauBarostat.
230 <  
231 <  if (!have_tau_barostat) {
232 <    sprintf( painCave.errMsg,
233 <             "NPTi error: If you use the NPTi\n"
234 <             "   integrator, you must set tauBarostat.\n");
235 <    painCave.isFatal = 1;
236 <    simError();
237 <    return -1;
238 <  }    
119 > }
120  
121 <  // We need NkBT a lot, so just set it here:
121 > template<typename T> bool NPTi<T>::etaConverged() {
122  
123 <  NkBT = (double)info->ndf * kB * targetTemp;
123 >  return ( fabs(prevEta - eta) <= etaTolerance );
124 > }
125  
126 <  return 1;
126 > template<typename T> double NPTi<T>::getConservedQuantity(void){
127 >
128 >  double conservedQuantity;
129 >  double Energy;
130 >  double thermostat_kinetic;
131 >  double thermostat_potential;
132 >  double barostat_kinetic;
133 >  double barostat_potential;
134 >
135 >  Energy = tStats->getTotalE();
136 >
137 >  thermostat_kinetic = fkBT* tt2 * chi * chi /
138 >    (2.0 * eConvert);
139 >
140 >  thermostat_potential = fkBT* integralOfChidt / eConvert;
141 >
142 >
143 >  barostat_kinetic = 3.0 * NkBT * tb2 * eta * eta /
144 >    (2.0 * eConvert);
145 >
146 >  barostat_potential = (targetPressure * tStats->getVolume() / p_convert) /
147 >    eConvert;
148 >
149 >  conservedQuantity = Energy + thermostat_kinetic + thermostat_potential +
150 >    barostat_kinetic + barostat_potential;
151 >
152 > //   cout.width(8);
153 > //   cout.precision(8);
154 >
155 > //   cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic <<
156 > //       "\t" << thermostat_potential << "\t" << barostat_kinetic <<
157 > //       "\t" << barostat_potential << "\t" << conservedQuantity << endl;
158 >  return conservedQuantity;
159   }
160 +
161 + template<typename T> string NPTi<T>::getAdditionalParameters(void){
162 +  string parameters;
163 +  const int BUFFERSIZE = 2000; // size of the read buffer
164 +  char buffer[BUFFERSIZE];
165 +
166 +  sprintf(buffer,"\t%G\t%G;", chi, integralOfChidt);
167 +  parameters += buffer;
168 +
169 +  sprintf(buffer,"\t%G\t0\t0;", eta);
170 +  parameters += buffer;
171 +
172 +  sprintf(buffer,"\t0\t%G\t0;", eta);
173 +  parameters += buffer;
174 +
175 +  sprintf(buffer,"\t0\t0\t%G;", eta);
176 +  parameters += buffer;
177 +
178 +  return parameters;
179 +
180 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines