ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-4/src/integrators/NPT.cpp
Revision: 1927
Committed: Wed Jan 12 17:14:35 2005 UTC (21 years, 4 months ago) by tim
File size: 11168 byte(s)
Log Message:
change license

File Contents

# User Rev Content
1 tim 1927 /*
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. Acknowledgement of the program authors must be made in any
10     * publication of scientific results based in part on use of the
11     * program. An acceptable form of acknowledgement is citation of
12     * the article in which the program was described (Matthew
13     * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14     * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15     * Parallel Simulation Engine for Molecular Dynamics,"
16     * J. Comput. Chem. 26, pp. 252-271 (2005))
17     *
18     * 2. Redistributions of source code must retain the above copyright
19     * notice, this list of conditions and the following disclaimer.
20     *
21     * 3. Redistributions in binary form must reproduce the above copyright
22     * notice, this list of conditions and the following disclaimer in the
23     * documentation and/or other materials provided with the
24     * distribution.
25     *
26     * This software is provided "AS IS," without a warranty of any
27     * kind. All express or implied conditions, representations and
28     * warranties, including any implied warranty of merchantability,
29     * fitness for a particular purpose or non-infringement, are hereby
30     * excluded. The University of Notre Dame and its licensors shall not
31     * be liable for any damages suffered by licensee as a result of
32     * using, modifying or distributing the software or its
33     * derivatives. In no event will the University of Notre Dame or its
34     * licensors be liable for any lost revenue, profit or data, or for
35     * direct, indirect, special, consequential, incidental or punitive
36     * damages, however caused and regardless of the theory of liability,
37     * arising out of the use of or inability to use software, even if the
38     * University of Notre Dame has been advised of the possibility of
39     * such damages.
40     */
41    
42 tim 1774 #include <math.h>
43    
44     #include "brains/SimInfo.hpp"
45     #include "brains/Thermo.hpp"
46     #include "integrators/NPT.hpp"
47 tim 1822 #include "math/SquareMatrix3.hpp"
48     #include "primitives/Molecule.hpp"
49     #include "utils/OOPSEConstant.hpp"
50 tim 1774 #include "utils/simError.h"
51    
52     // Basic isotropic thermostating and barostating via the Melchionna
53     // modification of the Hoover algorithm:
54     //
55     // Melchionna, S., Ciccotti, G., and Holian, B. L., 1993,
56     // Molec. Phys., 78, 533.
57     //
58     // and
59     //
60     // Hoover, W. G., 1986, Phys. Rev. A, 34, 2499.
61    
62     namespace oopse {
63    
64     NPT::NPT(SimInfo* info) :
65 tim 1883 VelocityVerletIntegrator(info), chiTolerance(1e-6), etaTolerance(1e-6), maxIterNum_(4) {
66 tim 1774
67 tim 1841 Globals* simParams = info_->getSimParams();
68 tim 1774
69 tim 1865 if (!simParams->getUseInitXSstate()) {
70 tim 1774 Snapshot* currSnapshot = info_->getSnapshotManager()->getCurrentSnapshot();
71     currSnapshot->setChi(0.0);
72     currSnapshot->setIntegralOfChiDt(0.0);
73 tim 1822 currSnapshot->setEta(Mat3x3d(0.0));
74 tim 1774 }
75    
76 tim 1841 if (!simParams->haveTargetTemp()) {
77 tim 1774 sprintf(painCave.errMsg, "You can't use the NVT integrator without a targetTemp!\n");
78     painCave.isFatal = 1;
79     painCave.severity = OOPSE_ERROR;
80     simError();
81     } else {
82 tim 1841 targetTemp = simParams->getTargetTemp();
83 tim 1774 }
84    
85     // We must set tauThermostat
86 tim 1841 if (!simParams->haveTauThermostat()) {
87 tim 1774 sprintf(painCave.errMsg, "If you use the constant temperature\n"
88     "\tintegrator, you must set tauThermostat_.\n");
89    
90     painCave.severity = OOPSE_ERROR;
91     painCave.isFatal = 1;
92     simError();
93     } else {
94 tim 1841 tauThermostat = simParams->getTauThermostat();
95 tim 1774 }
96    
97 tim 1841 if (!simParams->haveTargetPressure()) {
98 tim 1774 sprintf(painCave.errMsg, "NPT error: You can't use the NPT integrator\n"
99     " without a targetPressure!\n");
100    
101     painCave.isFatal = 1;
102     simError();
103     } else {
104 tim 1841 targetPressure = simParams->getTargetPressure();
105 tim 1774 }
106    
107 tim 1841 if (!simParams->haveTauBarostat()) {
108 tim 1774 sprintf(painCave.errMsg,
109     "If you use the NPT integrator, you must set tauBarostat.\n");
110     painCave.severity = OOPSE_ERROR;
111     painCave.isFatal = 1;
112     simError();
113     } else {
114 tim 1841 tauBarostat = simParams->getTauBarostat();
115 tim 1774 }
116    
117     tt2 = tauThermostat * tauThermostat;
118     tb2 = tauBarostat * tauBarostat;
119    
120     update();
121     }
122    
123     NPT::~NPT() {
124     }
125    
126 tim 1822 void NPT::doUpdate() {
127 tim 1867
128 tim 1774 oldPos.resize(info_->getNIntegrableObjects());
129     oldVel.resize(info_->getNIntegrableObjects());
130     oldJi.resize(info_->getNIntegrableObjects());
131    
132     }
133    
134     void NPT::moveA() {
135 tim 1822 SimInfo::MoleculeIterator i;
136     Molecule::IntegrableObjectIterator j;
137 tim 1774 Molecule* mol;
138     StuntDouble* integrableObject;
139     Vector3d Tb, ji;
140     double mass;
141     Vector3d vel;
142     Vector3d pos;
143     Vector3d frc;
144     Vector3d sc;
145     int index;
146 tim 1822
147 tim 1867 chi= currentSnapshot_->getChi();
148     integralOfChidt = currentSnapshot_->getIntegralOfChiDt();
149 tim 1822 loadEta();
150 tim 1774
151 tim 1822 instaTemp =thermo.getTemperature();
152     press = thermo.getPressureTensor();
153     instaPress = OOPSEConstant::pressureConvert* (press(0, 0) + press(1, 1) + press(2, 2)) / 3.0;
154     instaVol =thermo.getVolume();
155 tim 1774
156 tim 1822 Vector3d COM = info_->getCom();
157 tim 1774
158     //evolve velocity half step
159    
160     calcVelScale();
161    
162     for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
163     for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
164     integrableObject = mol->nextIntegrableObject(j)) {
165    
166     vel = integrableObject->getVel();
167     frc = integrableObject->getFrc();
168    
169     mass = integrableObject->getMass();
170    
171     getVelScaleA(sc, vel);
172    
173     // velocity half step (use chi from previous step here):
174 tim 1822 //vel[j] += dt2 * ((frc[j] / mass) * OOPSEConstant::energyConvert - sc[j]);
175     vel += dt2*OOPSEConstant::energyConvert/mass* frc - dt2*sc;
176 tim 1774 integrableObject->setVel(vel);
177    
178     if (integrableObject->isDirectional()) {
179    
180     // get and convert the torque to body frame
181    
182 tim 1871 Tb = integrableObject->lab2Body(integrableObject->getTrq());
183 tim 1774
184     // get the angular momentum, and propagate a half step
185    
186     ji = integrableObject->getJ();
187    
188 tim 1822 //ji[j] += dt2 * (Tb[j] * OOPSEConstant::energyConvert - ji[j]*chi);
189     ji += dt2*OOPSEConstant::energyConvert * Tb - dt2*chi* ji;
190 tim 1774
191 tim 1822 rotAlgo->rotate(integrableObject, ji, dt);
192 tim 1774
193     integrableObject->setJ(ji);
194     }
195    
196     }
197     }
198     // evolve chi and eta half step
199    
200 tim 1822 chi += dt2 * (instaTemp / targetTemp - 1.0) / tt2;
201    
202 tim 1774 evolveEtaA();
203    
204     //calculate the integral of chidt
205     integralOfChidt += dt2 * chi;
206    
207     index = 0;
208     for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
209     for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
210     integrableObject = mol->nextIntegrableObject(j)) {
211     oldPos[index++] = integrableObject->getPos();
212     }
213     }
214    
215     //the first estimation of r(t+dt) is equal to r(t)
216    
217     for(int k = 0; k < maxIterNum_; k++) {
218     index = 0;
219     for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
220     for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
221     integrableObject = mol->nextIntegrableObject(j)) {
222    
223     vel = integrableObject->getVel();
224     pos = integrableObject->getPos();
225    
226     this->getPosScale(pos, COM, index, sc);
227    
228     pos = oldPos[index] + dt * (vel + sc);
229     integrableObject->setPos(pos);
230    
231     ++index;
232     }
233     }
234    
235 tim 1901 rattle->constraintA();
236 tim 1774 }
237    
238     // Scale the box after all the positions have been moved:
239    
240     this->scaleSimBox();
241 tim 1822
242 tim 1867 currentSnapshot_->setChi(chi);
243     currentSnapshot_->setIntegralOfChiDt(integralOfChidt);
244 tim 1822
245     saveEta();
246 tim 1774 }
247    
248     void NPT::moveB(void) {
249 tim 1822 SimInfo::MoleculeIterator i;
250     Molecule::IntegrableObjectIterator j;
251 tim 1774 Molecule* mol;
252     StuntDouble* integrableObject;
253     int index;
254     Vector3d Tb;
255     Vector3d ji;
256     Vector3d sc;
257     Vector3d vel;
258     Vector3d frc;
259     double mass;
260    
261 tim 1822
262 tim 1867 chi= currentSnapshot_->getChi();
263     integralOfChidt = currentSnapshot_->getIntegralOfChiDt();
264 tim 1822 double oldChi = chi;
265     double prevChi;
266    
267     loadEta();
268    
269 tim 1774 //save velocity and angular momentum
270     index = 0;
271     for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
272     for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
273     integrableObject = mol->nextIntegrableObject(j)) {
274    
275     oldVel[index] = integrableObject->getVel();
276 tim 1822 oldJi[index] = integrableObject->getJ();
277 tim 1774 ++index;
278     }
279     }
280    
281     // do the iteration:
282 tim 1822 instaVol =thermo.getVolume();
283 tim 1774
284     for(int k = 0; k < maxIterNum_; k++) {
285 tim 1822 instaTemp =thermo.getTemperature();
286     instaPress =thermo.getPressure();
287 tim 1774
288     // evolve chi another half step using the temperature at t + dt/2
289 tim 1822 prevChi = chi;
290     chi = oldChi + dt2 * (instaTemp / targetTemp - 1.0) / tt2;
291    
292     //evolve eta
293 tim 1774 this->evolveEtaB();
294     this->calcVelScale();
295    
296     index = 0;
297     for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
298     for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
299     integrableObject = mol->nextIntegrableObject(j)) {
300 tim 1822
301     frc = integrableObject->getFrc();
302 tim 1774 vel = integrableObject->getVel();
303    
304     mass = integrableObject->getMass();
305    
306 tim 1822 getVelScaleB(sc, index);
307 tim 1774
308     // velocity half step
309 tim 1822 //vel[j] = oldVel[3 * i + j] + dt2 *((frc[j] / mass) * OOPSEConstant::energyConvert - sc[j]);
310     vel = oldVel[index] + dt2*OOPSEConstant::energyConvert/mass* frc - dt2*sc;
311 tim 1774 integrableObject->setVel(vel);
312    
313     if (integrableObject->isDirectional()) {
314     // get and convert the torque to body frame
315 tim 1871 Tb = integrableObject->lab2Body(integrableObject->getTrq());
316 tim 1774
317 tim 1822 //ji[j] = oldJi[3*i + j] + dt2 * (Tb[j] * OOPSEConstant::energyConvert - oldJi[3*i+j]*chi);
318     ji = oldJi[index] + dt2*OOPSEConstant::energyConvert*Tb - dt2*chi*oldJi[index];
319 tim 1774 integrableObject->setJ(ji);
320     }
321    
322     ++index;
323     }
324     }
325    
326 tim 1901 rattle->constraintB();
327 tim 1774
328 tim 1822 if ((fabs(prevChi - chi) <= chiTolerance) && this->etaConverged())
329 tim 1774 break;
330     }
331    
332     //calculate integral of chidt
333     integralOfChidt += dt2 * chi;
334    
335 tim 1867 currentSnapshot_->setChi(chi);
336     currentSnapshot_->setIntegralOfChiDt(integralOfChidt);
337 tim 1774
338 tim 1822 saveEta();
339 tim 1774 }
340    
341     }