OpenMD 3.0
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
NPT.cpp
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45#include "integrators/NPT.hpp"
46
47#include <cmath>
48
49#include "brains/SimInfo.hpp"
50#include "brains/Thermo.hpp"
53#include "utils/Constants.hpp"
54#include "utils/simError.h"
55
56// Basic isotropic thermostating and barostating via the Melchionna
57// modification of the Hoover algorithm:
58//
59// Melchionna, S., Ciccotti, G., and Holian, B. L., 1993,
60// Molec. Phys., 78, 533.
61//
62// and
63//
64// Hoover, W. G., 1986, Phys. Rev. A, 34, 2499.
65
66namespace OpenMD {
67
68 NPT::NPT(SimInfo* info) :
69 VelocityVerletIntegrator(info), etaTolerance(1e-6), chiTolerance(1e-6),
70 maxIterNum_(4) {
71 Globals* simParams = info_->getSimParams();
72
73 if (!simParams->getUseIntialExtendedSystemState()) {
74 Snapshot* currSnapshot =
75 info_->getSnapshotManager()->getCurrentSnapshot();
76 currSnapshot->setThermostat(make_pair(0.0, 0.0));
77 currSnapshot->setBarostat(Mat3x3d(0.0));
78 }
79
80 if (!simParams->haveTargetTemp()) {
81 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
82 "You can't use the NVT integrator without a targetTemp!\n");
83 painCave.isFatal = 1;
84 painCave.severity = OPENMD_ERROR;
85 simError();
86 } else {
87 targetTemp = simParams->getTargetTemp();
88 }
89
90 // We must set tauThermostat
91 if (!simParams->haveTauThermostat()) {
92 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
93 "If you use the constant temperature\n"
94 "\tintegrator, you must set tauThermostat.\n");
95
96 painCave.severity = OPENMD_ERROR;
97 painCave.isFatal = 1;
98 simError();
99 } else {
100 tauThermostat = simParams->getTauThermostat();
101 }
102
103 if (!simParams->haveTargetPressure()) {
104 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
105 "NPT error: You can't use the NPT integrator\n"
106 " without a targetPressure!\n");
107
108 painCave.isFatal = 1;
109 simError();
110 } else {
111 targetPressure = simParams->getTargetPressure();
112 }
113
114 if (!simParams->haveTauBarostat()) {
115 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
116 "If you use the NPT integrator, you must set tauBarostat.\n");
117 painCave.severity = OPENMD_ERROR;
118 painCave.isFatal = 1;
119 simError();
120 } else {
121 tauBarostat = simParams->getTauBarostat();
122 }
123
124 tt2 = tauThermostat * tauThermostat;
125 tb2 = tauBarostat * tauBarostat;
126
127 updateSizes();
128 }
129
130 void NPT::doUpdateSizes() {
131 oldPos.resize(info_->getNIntegrableObjects());
132 oldVel.resize(info_->getNIntegrableObjects());
133 oldJi.resize(info_->getNIntegrableObjects());
134 }
135
136 void NPT::moveA() {
137 SimInfo::MoleculeIterator i;
138 Molecule::IntegrableObjectIterator j;
139 Molecule* mol;
140 StuntDouble* sd;
141 Vector3d Tb, ji;
142 RealType mass;
143 Vector3d vel;
144 Vector3d pos;
145 Vector3d frc;
146 Vector3d sc;
147 int index;
148
149 thermostat = snap->getThermostat();
150 loadEta();
151
152 instaTemp = thermo.getTemperature();
153 press = thermo.getPressureTensor();
154 instaPress = Constants::pressureConvert *
155 (press(0, 0) + press(1, 1) + press(2, 2)) / 3.0;
156 instaVol = thermo.getVolume();
157
158 Vector3d COM = thermo.getCom();
159
160 // evolve velocity half step
161
162 calcVelScale();
163
164 for (mol = info_->beginMolecule(i); mol != NULL;
165 mol = info_->nextMolecule(i)) {
166 for (sd = mol->beginIntegrableObject(j); sd != NULL;
167 sd = mol->nextIntegrableObject(j)) {
168 vel = sd->getVel();
169 frc = sd->getFrc();
170
171 mass = sd->getMass();
172
173 getVelScaleA(sc, vel);
174
175 // velocity half step (use chi from previous step here):
176
177 vel += dt2 * Constants::energyConvert / mass * frc - dt2 * sc;
178 sd->setVel(vel);
179
180 if (sd->isDirectional()) {
181 // get and convert the torque to body frame
182
183 Tb = sd->lab2Body(sd->getTrq());
184
185 // get the angular momentum, and propagate a half step
186
187 ji = sd->getJ();
188
189 ji +=
190 dt2 * Constants::energyConvert * Tb - dt2 * thermostat.first * ji;
191
192 rotAlgo_->rotate(sd, ji, dt);
193
194 sd->setJ(ji);
195 }
196 }
197 }
198 // evolve chi and eta half step
199
200 thermostat.first += dt2 * (instaTemp / targetTemp - 1.0) / tt2;
201
202 evolveEtaA();
203
204 // calculate the integral of chidt
205 thermostat.second += dt2 * thermostat.first;
206
207 flucQ_->moveA();
208
209 index = 0;
210 for (mol = info_->beginMolecule(i); mol != NULL;
211 mol = info_->nextMolecule(i)) {
212 for (sd = mol->beginIntegrableObject(j); sd != NULL;
213 sd = mol->nextIntegrableObject(j)) {
214 oldPos[index++] = sd->getPos();
215 }
216 }
217
218 // the first estimation of r(t+dt) is equal to r(t)
219
220 for (int k = 0; k < maxIterNum_; k++) {
221 index = 0;
222 for (mol = info_->beginMolecule(i); mol != NULL;
223 mol = info_->nextMolecule(i)) {
224 for (sd = mol->beginIntegrableObject(j); sd != NULL;
225 sd = mol->nextIntegrableObject(j)) {
226 vel = sd->getVel();
227 pos = sd->getPos();
228
229 this->getPosScale(pos, COM, index, sc);
230
231 pos = oldPos[index] + dt * (vel + sc);
232 sd->setPos(pos);
233
234 ++index;
235 }
236 }
237
238 rattle_->constraintA();
239 }
240
241 // Scale the box after all the positions have been moved:
242
243 this->scaleSimBox();
244
245 snap->setThermostat(thermostat);
246
247 saveEta();
248 }
249
250 void NPT::moveB(void) {
251 SimInfo::MoleculeIterator i;
252 Molecule::IntegrableObjectIterator j;
253 Molecule* mol;
254 StuntDouble* sd;
255 int index;
256 Vector3d Tb;
257 Vector3d ji;
258 Vector3d sc;
259 Vector3d vel;
260 Vector3d frc;
261 RealType mass;
262
263 thermostat = snap->getThermostat();
264 RealType oldChi = thermostat.first;
265 RealType prevChi;
266
267 loadEta();
268
269 // save velocity and angular momentum
270 index = 0;
271 for (mol = info_->beginMolecule(i); mol != NULL;
272 mol = info_->nextMolecule(i)) {
273 for (sd = mol->beginIntegrableObject(j); sd != NULL;
274 sd = mol->nextIntegrableObject(j)) {
275 oldVel[index] = sd->getVel();
276
277 if (sd->isDirectional()) oldJi[index] = sd->getJ();
278
279 ++index;
280 }
281 }
282
283 // do the iteration:
284 instaVol = thermo.getVolume();
285
286 for (int k = 0; k < maxIterNum_; k++) {
287 instaTemp = thermo.getTemperature();
288 instaPress = thermo.getPressure();
289
290 // evolve chi another half step using the temperature at t + dt/2
291 prevChi = thermostat.first;
292 thermostat.first = oldChi + dt2 * (instaTemp / targetTemp - 1.0) / tt2;
293
294 // evolve eta
295 this->evolveEtaB();
296 this->calcVelScale();
297
298 index = 0;
299 for (mol = info_->beginMolecule(i); mol != NULL;
300 mol = info_->nextMolecule(i)) {
301 for (sd = mol->beginIntegrableObject(j); sd != NULL;
302 sd = mol->nextIntegrableObject(j)) {
303 frc = sd->getFrc();
304 mass = sd->getMass();
305
306 getVelScaleB(sc, index);
307
308 // velocity half step
309 vel = oldVel[index] + dt2 * Constants::energyConvert / mass * frc -
310 dt2 * sc;
311
312 sd->setVel(vel);
313
314 if (sd->isDirectional()) {
315 // get and convert the torque to body frame
316 Tb = sd->lab2Body(sd->getTrq());
317
318 ji = oldJi[index] + dt2 * Constants::energyConvert * Tb -
319 dt2 * thermostat.first * oldJi[index];
320
321 sd->setJ(ji);
322 }
323
324 ++index;
325 }
326 }
327
328 rattle_->constraintB();
329
330 if ((fabs(prevChi - thermostat.first) <= chiTolerance) &&
331 this->etaConverged())
332 break;
333 }
334
335 // calculate integral of chidt
336 thermostat.second += dt2 * thermostat.first;
337
338 snap->setThermostat(thermostat);
339
340 flucQ_->moveB();
341 saveEta();
342 }
343
344 void NPT::resetIntegrator() {
345 snap->setThermostat(make_pair(0.0, 0.0));
346 resetEta();
347 }
348
349 void NPT::resetEta() {
350 Mat3x3d etaMat(0.0);
351 snap->setBarostat(etaMat);
352 }
353} // namespace OpenMD
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
Definition SimInfo.hpp:93
Molecule * beginMolecule(MoleculeIterator &i)
Returns the first molecule in this SimInfo and intialize the iterator.
Definition SimInfo.cpp:240
unsigned int getNIntegrableObjects()
Returns the number of local integrable objects.
Definition SimInfo.hpp:192
Molecule * nextMolecule(MoleculeIterator &i)
Returns the next avaliable Molecule based on the iterator.
Definition SimInfo.cpp:245
The Snapshot class is a repository storing dynamic data during a Simulation.
Definition Snapshot.hpp:147
"Don't move, or you're dead! Stand up! Captain, we've got them!"
Vector3d getTrq()
Returns the current torque of this stuntDouble.
Vector3d lab2Body(const Vector3d &v)
Converts a lab fixed vector to a body fixed vector.
Vector3d getVel()
Returns the current velocity of this stuntDouble.
RealType getMass()
Returns the mass of this stuntDouble.
Vector3d getPos()
Returns the current position of this stuntDouble.
void setPos(const Vector3d &pos)
Sets the current position of this stuntDouble.
void setVel(const Vector3d &vel)
Sets the current velocity of this stuntDouble.
Vector3d getJ()
Returns the current angular momentum of this stuntDouble (body -fixed).
bool isDirectional()
Tests if this stuntDouble is a directional one.
Vector3d getFrc()
Returns the current force of this stuntDouble.
void setJ(const Vector3d &angMom)
Sets the current angular momentum of this stuntDouble (body-fixed).
Mat3x3d getPressureTensor()
gives the pressure tensor in amu*fs^-2*Ang^-1
Definition Thermo.cpp:443
Vector3d getCom()
Returns the center of the mass of the whole system.
Definition Thermo.cpp:805
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.