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 586 by mmeineke, Wed Jul 9 22:14:06 2003 UTC vs.
branches/new-templateless/OOPSE/libmdtools/NPTi.cpp (file contents), Revision 851 by mmeineke, Wed Nov 5 19:18:17 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines