ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/integrators/NVT.cpp
(Generate patch)

Comparing:
trunk/src/integrators/NVT.cpp (file contents), Revision 2 by gezelter, Fri Sep 24 04:16:43 2004 UTC vs.
branches/development/src/integrators/NVT.cpp (file contents), Revision 1665 by gezelter, Tue Nov 22 20:38:56 2011 UTC

# Line 1 | Line 1
1 < #include <math.h>
1 > /*
2 > * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3 > *
4 > * The University of Notre Dame grants you ("Licensee") a
5 > * non-exclusive, royalty free, license to use, modify and
6 > * redistribute this software in source and binary code form, provided
7 > * that the following conditions are met:
8 > *
9 > * 1. Redistributions of source code must retain the above copyright
10 > *    notice, this list of conditions and the following disclaimer.
11 > *
12 > * 2. Redistributions in binary form must reproduce the above copyright
13 > *    notice, this list of conditions and the following disclaimer in the
14 > *    documentation and/or other materials provided with the
15 > *    distribution.
16 > *
17 > * This software is provided "AS IS," without a warranty of any
18 > * kind. All express or implied conditions, representations and
19 > * warranties, including any implied warranty of merchantability,
20 > * fitness for a particular purpose or non-infringement, are hereby
21 > * excluded.  The University of Notre Dame and its licensors shall not
22 > * be liable for any damages suffered by licensee as a result of
23 > * using, modifying or distributing the software or its
24 > * derivatives. In no event will the University of Notre Dame or its
25 > * licensors be liable for any lost revenue, profit or data, or for
26 > * direct, indirect, special, consequential, incidental or punitive
27 > * damages, however caused and regardless of the theory of liability,
28 > * arising out of the use of or inability to use software, even if the
29 > * University of Notre Dame has been advised of the possibility of
30 > * such damages.
31 > *
32 > * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your
33 > * research, please cite the appropriate papers when you publish your
34 > * work.  Good starting points are:
35 > *                                                                      
36 > * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).            
37 > * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).          
38 > * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008).          
39 > * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010).
40 > * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41 > */
42 >
43 > #include "integrators/NVT.hpp"
44 > #include "primitives/Molecule.hpp"
45 > #include "utils/simError.h"
46 > #include "utils/PhysicalConstants.hpp"
47  
48 < #include "Atom.hpp"
4 < #include "SRI.hpp"
5 < #include "AbstractClasses.hpp"
6 < #include "SimInfo.hpp"
7 < #include "ForceFields.hpp"
8 < #include "Thermo.hpp"
9 < #include "ReadWrite.hpp"
10 < #include "Integrator.hpp"
11 < #include "simError.h"
48 > namespace OpenMD {
49  
50 +  NVT::NVT(SimInfo* info) : VelocityVerletIntegrator(info), chiTolerance_ (1e-6), maxIterNum_(4) {
51  
52 < // Basic thermostating via Hoover, Phys.Rev.A, 1985, Vol. 31 (5) 1695-1697
52 >    Globals* simParams = info_->getSimParams();
53  
54 < template<typename T> NVT<T>::NVT ( SimInfo *theInfo, ForceFields* the_ff):
55 <  T( theInfo, the_ff )
56 < {
57 <  GenericData* data;
20 <  DoubleData * chiValue;
21 <  DoubleData * integralOfChidtValue;
22 <
23 <  chiValue = NULL;
24 <  integralOfChidtValue = NULL;
25 <
26 <  chi = 0.0;
27 <  have_tau_thermostat = 0;
28 <  have_target_temp = 0;
29 <  have_chi_tolerance = 0;
30 <  integralOfChidt = 0.0;
31 <
32 <
33 <  if( theInfo->useInitXSstate ){
34 <
35 <    // retrieve chi and integralOfChidt from simInfo
36 <    data = info->getProperty(CHIVALUE_ID);
37 <    if(data){
38 <      chiValue = dynamic_cast<DoubleData*>(data);
54 >    if (!simParams->getUseIntialExtendedSystemState()) {
55 >      Snapshot* currSnapshot = info_->getSnapshotManager()->getCurrentSnapshot();
56 >      currSnapshot->setChi(0.0);
57 >      currSnapshot->setIntegralOfChiDt(0.0);
58      }
59      
60 <    data = info->getProperty(INTEGRALOFCHIDT_ID);
61 <    if(data){
62 <      integralOfChidtValue = dynamic_cast<DoubleData*>(data);
60 >    if (!simParams->haveTargetTemp()) {
61 >      sprintf(painCave.errMsg, "You can't use the NVT integrator without a targetTemp_!\n");
62 >      painCave.isFatal = 1;
63 >      painCave.severity = OPENMD_ERROR;
64 >      simError();
65 >    } else {
66 >      targetTemp_ = simParams->getTargetTemp();
67      }
45    
46    // chi and integralOfChidt should appear by pair
47    if(chiValue && integralOfChidtValue){
48      chi = chiValue->getData();
49      integralOfChidt = integralOfChidtValue->getData();
50    }
51  }
68  
69 <  oldVel = new double[3*integrableObjects.size()];
54 <  oldJi = new double[3*integrableObjects.size()];
55 < }
69 >    // We must set tauThermostat.
70  
71 < template<typename T> NVT<T>::~NVT() {
72 <  delete[] oldVel;
73 <  delete[] oldJi;
60 < }
71 >    if (!simParams->haveTauThermostat()) {
72 >      sprintf(painCave.errMsg, "If you use the constant temperature\n"
73 >              "\tintegrator, you must set tauThermostat.\n");
74  
75 < template<typename T> void NVT<T>::moveA() {
75 >      painCave.severity = OPENMD_ERROR;
76 >      painCave.isFatal = 1;
77 >      simError();
78 >    } else {
79 >      tauThermostat_ = simParams->getTauThermostat();
80 >    }
81  
82 <  int i, j;
83 <  DirectionalAtom* dAtom;
66 <  double Tb[3], ji[3];
67 <  double mass;
68 <  double vel[3], pos[3], frc[3];
82 >    update();
83 >  }
84  
85 <  double instTemp;
85 >  void NVT::doUpdate() {
86 >    oldVel_.resize(info_->getNIntegrableObjects());
87 >    oldJi_.resize(info_->getNIntegrableObjects());    
88 >  }
89 >  void NVT::moveA() {
90 >    SimInfo::MoleculeIterator i;
91 >    Molecule::IntegrableObjectIterator  j;
92 >    Molecule* mol;
93 >    StuntDouble* integrableObject;
94 >    Vector3d Tb;
95 >    Vector3d ji;
96 >    RealType mass;
97 >    Vector3d vel;
98 >    Vector3d pos;
99 >    Vector3d frc;
100  
101 <  // We need the temperature at time = t for the chi update below:
101 >    RealType chi = currentSnapshot_->getChi();
102 >    RealType integralOfChidt = currentSnapshot_->getIntegralOfChiDt();
103 >    
104 >    // We need the temperature at time = t for the chi update below:
105  
106 <  instTemp = tStats->getTemperature();
106 >    RealType instTemp = thermo.getTemperature();
107  
108 <  for( i=0; i < integrableObjects.size(); i++ ){
108 >    for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
109 >      for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
110 >           integrableObject = mol->nextIntegrableObject(j)) {
111  
112 <    integrableObjects[i]->getVel( vel );
113 <    integrableObjects[i]->getPos( pos );
114 <    integrableObjects[i]->getFrc( frc );
112 >        vel = integrableObject->getVel();
113 >        pos = integrableObject->getPos();
114 >        frc = integrableObject->getFrc();
115  
116 <    mass = integrableObjects[i]->getMass();
116 >        mass = integrableObject->getMass();
117  
118 <    for (j=0; j < 3; j++) {
119 <      // velocity half step  (use chi from previous step here):
120 <      vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*chi);
121 <      // position whole step
122 <      pos[j] += dt * vel[j];
123 <    }
118 >        // velocity half step  (use chi from previous step here):
119 >        //vel[j] += dt2 * ((frc[j] / mass ) * PhysicalConstants::energyConvert - vel[j]*chi);
120 >        vel += dt2 *PhysicalConstants::energyConvert/mass*frc - dt2*chi*vel;
121 >        
122 >        // position whole step
123 >        //pos[j] += dt * vel[j];
124 >        pos += dt * vel;
125  
126 <    integrableObjects[i]->setVel( vel );
127 <    integrableObjects[i]->setPos( pos );
126 >        integrableObject->setVel(vel);
127 >        integrableObject->setPos(pos);
128  
129 <    if( integrableObjects[i]->isDirectional() ){
129 >        if (integrableObject->isDirectional()) {
130  
131 <      // get and convert the torque to body frame
131 >          //convert the torque to body frame
132 >          Tb = integrableObject->lab2Body(integrableObject->getTrq());
133  
134 <      integrableObjects[i]->getTrq( Tb );
99 <      integrableObjects[i]->lab2Body( Tb );
134 >          // get the angular momentum, and propagate a half step
135  
136 <      // get the angular momentum, and propagate a half step
136 >          ji = integrableObject->getJ();
137  
138 <      integrableObjects[i]->getJ( ji );
138 >          //ji[j] += dt2 * (Tb[j] * PhysicalConstants::energyConvert - ji[j]*chi);
139 >          ji += dt2*PhysicalConstants::energyConvert*Tb - dt2*chi *ji;
140 >          rotAlgo->rotate(integrableObject, ji, dt);
141  
142 <      for (j=0; j < 3; j++)
143 <        ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);
142 >          integrableObject->setJ(ji);
143 >        }
144 >      }
145  
108      this->rotationPropagation( integrableObjects[i], ji );
109
110      integrableObjects[i]->setJ( ji );
146      }
147 <  }
148 <  
114 <  if(nConstrained)
115 <    constrainA();
147 >    
148 >    rattle->constraintA();
149  
150 <  // Finally, evolve chi a half step (just like a velocity) using
151 <  // temperature at time t, not time t+dt/2
150 >    // Finally, evolve chi a half step (just like a velocity) using
151 >    // temperature at time t, not time t+dt/2
152  
153 <  //std::cerr << "targetTemp = " << targetTemp << " instTemp = " << instTemp << " tauThermostat = " << tauThermostat << " integral of Chi = " << integralOfChidt << "\n";
154 <  
155 <  chi += dt2 * ( instTemp / targetTemp - 1.0) / (tauThermostat*tauThermostat);
123 <  integralOfChidt += chi*dt2;
153 >    
154 >    chi += dt2 * (instTemp / targetTemp_ - 1.0) / (tauThermostat_ * tauThermostat_);
155 >    integralOfChidt += chi * dt2;
156  
157 < }
158 <
127 < template<typename T> void NVT<T>::moveB( void ){
128 <  int i, j, k;
129 <  double Tb[3], ji[3];
130 <  double vel[3], frc[3];
131 <  double mass;
132 <  double instTemp;
133 <  double oldChi, prevChi;
134 <
135 <  // Set things up for the iteration:
136 <
137 <  oldChi = chi;
138 <
139 <  for( i=0; i < integrableObjects.size(); i++ ){
140 <
141 <    integrableObjects[i]->getVel( vel );
142 <
143 <    for (j=0; j < 3; j++)
144 <      oldVel[3*i + j]  = vel[j];
145 <
146 <    if( integrableObjects[i]->isDirectional() ){
147 <
148 <      integrableObjects[i]->getJ( ji );
149 <
150 <      for (j=0; j < 3; j++)
151 <        oldJi[3*i + j] = ji[j];
152 <
153 <    }
157 >    currentSnapshot_->setChi(chi);
158 >    currentSnapshot_->setIntegralOfChiDt(integralOfChidt);
159    }
160  
161 <  // do the iteration:
161 >  void NVT::moveB() {
162 >    SimInfo::MoleculeIterator i;
163 >    Molecule::IntegrableObjectIterator  j;
164 >    Molecule* mol;
165 >    StuntDouble* integrableObject;
166 >    
167 >    Vector3d Tb;
168 >    Vector3d ji;    
169 >    Vector3d vel;
170 >    Vector3d frc;
171 >    RealType mass;
172 >    RealType instTemp;
173 >    int index;
174 >    // Set things up for the iteration:
175  
176 <  for (k=0; k < 4; k++) {
176 >    RealType chi = currentSnapshot_->getChi();
177 >    RealType oldChi = chi;
178 >    RealType  prevChi;
179 >    RealType integralOfChidt = currentSnapshot_->getIntegralOfChiDt();
180  
181 <    instTemp = tStats->getTemperature();
181 >    index = 0;
182 >    for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
183 >      for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
184 >           integrableObject = mol->nextIntegrableObject(j)) {
185 >        oldVel_[index] = integrableObject->getVel();
186 >        oldJi_[index] = integrableObject->getJ();                
187  
188 <    // evolve chi another half step using the temperature at t + dt/2
188 >        ++index;    
189 >      }
190 >          
191 >    }
192  
193 <    prevChi = chi;
165 <    chi = oldChi + dt2 * ( instTemp / targetTemp - 1.0) /
166 <      (tauThermostat*tauThermostat);
193 >    // do the iteration:
194  
195 <    for( i=0; i < integrableObjects.size(); i++ ){
195 >    for(int k = 0; k < maxIterNum_; k++) {
196 >      index = 0;
197 >      instTemp = thermo.getTemperature();
198  
199 <      integrableObjects[i]->getFrc( frc );
171 <      integrableObjects[i]->getVel(vel);
199 >      // evolve chi another half step using the temperature at t + dt/2
200  
201 <      mass = integrableObjects[i]->getMass();
201 >      prevChi = chi;
202 >      chi = oldChi + dt2 * (instTemp / targetTemp_ - 1.0) / (tauThermostat_ * tauThermostat_);
203  
204 <      // velocity half step
205 <      for (j=0; j < 3; j++)
206 <        vel[j] = oldVel[3*i+j] + dt2 * ((frc[j] / mass ) * eConvert - oldVel[3*i + j]*chi);
204 >      for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
205 >        for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
206 >             integrableObject = mol->nextIntegrableObject(j)) {
207  
208 <      integrableObjects[i]->setVel( vel );
208 >          frc = integrableObject->getFrc();
209 >          vel = integrableObject->getVel();
210  
211 <      if( integrableObjects[i]->isDirectional() ){
211 >          mass = integrableObject->getMass();
212  
213 <        // get and convert the torque to body frame
213 >          // velocity half step
214 >          //for(j = 0; j < 3; j++)
215 >          //    vel[j] = oldVel_[3*i+j] + dt2 * ((frc[j] / mass ) * PhysicalConstants::energyConvert - oldVel_[3*i + j]*chi);
216 >          vel = oldVel_[index] + dt2/mass*PhysicalConstants::energyConvert * frc - dt2*chi*oldVel_[index];
217 >            
218 >          integrableObject->setVel(vel);
219  
220 <        integrableObjects[i]->getTrq( Tb );
186 <        integrableObjects[i]->lab2Body( Tb );
220 >          if (integrableObject->isDirectional()) {
221  
222 <        for (j=0; j < 3; j++)
189 <          ji[j] = oldJi[3*i + j] + dt2 * (Tb[j] * eConvert - oldJi[3*i+j]*chi);
222 >            // get and convert the torque to body frame
223  
224 <        integrableObjects[i]->setJ( ji );
192 <      }
193 <    }
194 <    
195 <    if(nConstrained)
196 <      constrainB();
224 >            Tb =  integrableObject->lab2Body(integrableObject->getTrq());
225  
226 <    if (fabs(prevChi - chi) <= chiTolerance) break;
227 <  }
226 >            //for(j = 0; j < 3; j++)
227 >            //    ji[j] = oldJi_[3*i + j] + dt2 * (Tb[j] * PhysicalConstants::energyConvert - oldJi_[3*i+j]*chi);
228 >            ji = oldJi_[index] + dt2*PhysicalConstants::energyConvert*Tb - dt2*chi *oldJi_[index];
229  
230 <  integralOfChidt += dt2*chi;
231 < }
230 >            integrableObject->setJ(ji);
231 >          }
232  
204 template<typename T> void NVT<T>::resetIntegrator( void ){
233  
234 <  chi = 0.0;
235 <  integralOfChidt = 0.0;
236 < }
234 >          ++index;
235 >        }
236 >      }
237 >    
238  
239 < template<typename T> int NVT<T>::readyCheck() {
239 >      rattle->constraintB();
240  
241 <  //check parent's readyCheck() first
242 <  if (T::readyCheck() == -1)
214 <    return -1;
241 >      if (fabs(prevChi - chi) <= chiTolerance_)
242 >        break;
243  
244 <  // First check to see if we have a target temperature.
217 <  // Not having one is fatal.
244 >    }
245  
246 <  if (!have_target_temp) {
220 <    sprintf( painCave.errMsg,
221 <             "You can't use the NVT integrator without a targetTemp!\n"
222 <             );
223 <    painCave.isFatal = 1;
224 <    painCave.severity = OOPSE_ERROR;
225 <    simError();
226 <    return -1;
227 <  }
246 >    integralOfChidt += dt2 * chi;
247  
248 <  // We must set tauThermostat.
249 <
231 <  if (!have_tau_thermostat) {
232 <    sprintf( painCave.errMsg,
233 <             "If you use the constant temperature\n"
234 <             "\tintegrator, you must set tauThermostat.\n");
235 <    painCave.severity = OOPSE_ERROR;
236 <    painCave.isFatal = 1;
237 <    simError();
238 <    return -1;
248 >    currentSnapshot_->setChi(chi);
249 >    currentSnapshot_->setIntegralOfChiDt(integralOfChidt);
250    }
251  
252 <  if (!have_chi_tolerance) {
253 <    sprintf( painCave.errMsg,
254 <             "In NVT integrator: setting chi tolerance to 1e-6\n");
244 <    chiTolerance = 1e-6;
245 <    have_chi_tolerance = 1;
246 <    painCave.severity = OOPSE_INFO;
247 <    painCave.isFatal = 0;
248 <    simError();
252 >  void NVT::resetIntegrator() {
253 >      currentSnapshot_->setChi(0.0);
254 >      currentSnapshot_->setIntegralOfChiDt(0.0);
255    }
256 +  
257 +  RealType NVT::calcConservedQuantity() {
258  
259 <  return 1;
259 >    RealType chi = currentSnapshot_->getChi();
260 >    RealType integralOfChidt = currentSnapshot_->getIntegralOfChiDt();
261 >    RealType conservedQuantity;
262 >    RealType fkBT;
263 >    RealType Energy;
264 >    RealType thermostat_kinetic;
265 >    RealType thermostat_potential;
266 >    
267 >    fkBT = info_->getNdf() *PhysicalConstants::kB *targetTemp_;
268  
269 < }
269 >    Energy = thermo.getTotalE();
270  
271 < template<typename T> double NVT<T>::getConservedQuantity(void){
271 >    thermostat_kinetic = fkBT * tauThermostat_ * tauThermostat_ * chi * chi / (2.0 * PhysicalConstants::energyConvert);
272  
273 <  double conservedQuantity;
258 <  double fkBT;
259 <  double Energy;
260 <  double thermostat_kinetic;
261 <  double thermostat_potential;
273 >    thermostat_potential = fkBT * integralOfChidt / PhysicalConstants::energyConvert;
274  
275 <  fkBT = (double)(info->ndf) * kB * targetTemp;
275 >    conservedQuantity = Energy + thermostat_kinetic + thermostat_potential;
276  
277 <  Energy = tStats->getTotalE();
277 >    return conservedQuantity;
278 >  }
279  
267  thermostat_kinetic = fkBT* tauThermostat * tauThermostat * chi * chi /
268    (2.0 * eConvert);
280  
281 <  thermostat_potential = fkBT * integralOfChidt / eConvert;
271 <
272 <  conservedQuantity = Energy + thermostat_kinetic + thermostat_potential;
273 <
274 <  return conservedQuantity;
275 < }
276 <
277 < template<typename T> string NVT<T>::getAdditionalParameters(void){
278 <  string parameters;
279 <  const int BUFFERSIZE = 2000; // size of the read buffer
280 <  char buffer[BUFFERSIZE];
281 <
282 <  sprintf(buffer,"\t%G\t%G;", chi, integralOfChidt);
283 <  parameters += buffer;
284 <
285 <  return parameters;
286 < }
281 > }//end namespace OpenMD

Comparing:
trunk/src/integrators/NVT.cpp (property svn:keywords), Revision 2 by gezelter, Fri Sep 24 04:16:43 2004 UTC vs.
branches/development/src/integrators/NVT.cpp (property svn:keywords), Revision 1665 by gezelter, Tue Nov 22 20:38:56 2011 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines