OpenMD 3.2
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 following paper when you publish your work:
33 *
34 * [1] Drisko et al., J. Open Source Softw. 9, 7004 (2024).
35 *
36 * Good starting points for code and simulation methodology are:
37 *
38 * [2] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
39 * [3] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
40 * [4] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
41 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
42 * [6] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
43 * [7] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
44 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
45 * [9] Drisko & Gezelter, J. Chem. Theory Comput. 20, 4986-4997 (2024).
46 */
47
48#include "integrators/NPT.hpp"
49
50#include <cmath>
51
52#include "brains/SimInfo.hpp"
53#include "brains/Thermo.hpp"
56#include "utils/Constants.hpp"
57#include "utils/simError.h"
58
59// Basic isotropic thermostating and barostating via the Melchionna
60// modification of the Hoover algorithm:
61//
62// Melchionna, S., Ciccotti, G., and Holian, B. L., 1993,
63// Molec. Phys., 78, 533.
64//
65// and
66//
67// Hoover, W. G., 1986, Phys. Rev. A, 34, 2499.
68
69namespace OpenMD {
70
71 NPT::NPT(SimInfo* info) :
72 VelocityVerletIntegrator(info), etaTolerance(1e-6), chiTolerance(1e-6),
73 maxIterNum_(4) {
74 Globals* simParams = info_->getSimParams();
75
76 if (!simParams->getUseIntialExtendedSystemState()) {
77 Snapshot* currSnapshot =
78 info_->getSnapshotManager()->getCurrentSnapshot();
79 currSnapshot->setThermostat(make_pair(0.0, 0.0));
80 currSnapshot->setBarostat(Mat3x3d(0.0));
81 }
82
83 if (!simParams->haveTargetTemp()) {
84 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
85 "You can't use the NVT integrator without a targetTemp!\n");
86 painCave.isFatal = 1;
87 painCave.severity = OPENMD_ERROR;
88 simError();
89 } else {
90 targetTemp = simParams->getTargetTemp();
91 }
92
93 // We must set tauThermostat
94 if (!simParams->haveTauThermostat()) {
95 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
96 "If you use the constant temperature\n"
97 "\tintegrator, you must set tauThermostat.\n");
98
99 painCave.severity = OPENMD_ERROR;
100 painCave.isFatal = 1;
101 simError();
102 } else {
103 tauThermostat = simParams->getTauThermostat();
104 }
105
106 if (!simParams->haveTargetPressure()) {
107 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
108 "NPT error: You can't use the NPT integrator\n"
109 " without a targetPressure!\n");
110
111 painCave.isFatal = 1;
112 simError();
113 } else {
114 targetPressure = simParams->getTargetPressure();
115 }
116
117 if (!simParams->haveTauBarostat()) {
118 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
119 "If you use the NPT integrator, you must set tauBarostat.\n");
120 painCave.severity = OPENMD_ERROR;
121 painCave.isFatal = 1;
122 simError();
123 } else {
124 tauBarostat = simParams->getTauBarostat();
125 }
126
127 tt2 = tauThermostat * tauThermostat;
128 tb2 = tauBarostat * tauBarostat;
129
130 updateSizes();
131 }
132
133 void NPT::doUpdateSizes() {
134 oldPos.resize(info_->getNIntegrableObjects());
135 oldVel.resize(info_->getNIntegrableObjects());
136 oldJi.resize(info_->getNIntegrableObjects());
137 }
138
139 void NPT::moveA() {
140 SimInfo::MoleculeIterator i;
141 Molecule::IntegrableObjectIterator j;
142 Molecule* mol;
143 StuntDouble* sd;
144 Vector3d Tb, ji;
145 RealType mass;
146 Vector3d vel;
147 Vector3d pos;
148 Vector3d frc;
149 Vector3d sc;
150 int index;
151
152 thermostat = snap->getThermostat();
153 loadEta();
154
155 instaTemp = thermo.getTemperature();
156 press = thermo.getPressureTensor();
157 instaPress = Constants::pressureConvert *
158 (press(0, 0) + press(1, 1) + press(2, 2)) / 3.0;
159 instaVol = thermo.getVolume();
160
161 Vector3d COM = thermo.getCom();
162
163 // evolve velocity half step
164
165 calcVelScale();
166
167 for (mol = info_->beginMolecule(i); mol != NULL;
168 mol = info_->nextMolecule(i)) {
169 for (sd = mol->beginIntegrableObject(j); sd != NULL;
170 sd = mol->nextIntegrableObject(j)) {
171 vel = sd->getVel();
172 frc = sd->getFrc();
173
174 mass = sd->getMass();
175
176 getVelScaleA(sc, vel);
177
178 // velocity half step (use chi from previous step here):
179
180 vel += dt2 * Constants::energyConvert / mass * frc - dt2 * sc;
181 sd->setVel(vel);
182
183 if (sd->isDirectional()) {
184 // get and convert the torque to body frame
185
186 Tb = sd->lab2Body(sd->getTrq());
187
188 // get the angular momentum, and propagate a half step
189
190 ji = sd->getJ();
191
192 ji +=
193 dt2 * Constants::energyConvert * Tb - dt2 * thermostat.first * ji;
194
195 rotAlgo_->rotate(sd, ji, dt);
196
197 sd->setJ(ji);
198 }
199 }
200 }
201 // evolve chi and eta half step
202
203 thermostat.first += dt2 * (instaTemp / targetTemp - 1.0) / tt2;
204
205 evolveEtaA();
206
207 // calculate the integral of chidt
208 thermostat.second += dt2 * thermostat.first;
209
210 flucQ_->moveA();
211
212 index = 0;
213 for (mol = info_->beginMolecule(i); mol != NULL;
214 mol = info_->nextMolecule(i)) {
215 for (sd = mol->beginIntegrableObject(j); sd != NULL;
216 sd = mol->nextIntegrableObject(j)) {
217 oldPos[index++] = sd->getPos();
218 }
219 }
220
221 // the first estimation of r(t+dt) is equal to r(t)
222
223 for (int k = 0; k < maxIterNum_; k++) {
224 index = 0;
225 for (mol = info_->beginMolecule(i); mol != NULL;
226 mol = info_->nextMolecule(i)) {
227 for (sd = mol->beginIntegrableObject(j); sd != NULL;
228 sd = mol->nextIntegrableObject(j)) {
229 vel = sd->getVel();
230 pos = sd->getPos();
231
232 this->getPosScale(pos, COM, index, sc);
233
234 pos = oldPos[index] + dt * (vel + sc);
235 sd->setPos(pos);
236
237 ++index;
238 }
239 }
240
241 rattle_->constraintA();
242 }
243
244 // Scale the box after all the positions have been moved:
245
246 this->scaleSimBox();
247
248 snap->setThermostat(thermostat);
249
250 saveEta();
251 }
252
253 void NPT::moveB(void) {
254 SimInfo::MoleculeIterator i;
255 Molecule::IntegrableObjectIterator j;
256 Molecule* mol;
257 StuntDouble* sd;
258 int index;
259 Vector3d Tb;
260 Vector3d ji;
261 Vector3d sc;
262 Vector3d vel;
263 Vector3d frc;
264 RealType mass;
265
266 thermostat = snap->getThermostat();
267 RealType oldChi = thermostat.first;
268 RealType prevChi;
269
270 loadEta();
271
272 // save velocity and angular momentum
273 index = 0;
274 for (mol = info_->beginMolecule(i); mol != NULL;
275 mol = info_->nextMolecule(i)) {
276 for (sd = mol->beginIntegrableObject(j); sd != NULL;
277 sd = mol->nextIntegrableObject(j)) {
278 oldVel[index] = sd->getVel();
279
280 if (sd->isDirectional()) oldJi[index] = sd->getJ();
281
282 ++index;
283 }
284 }
285
286 // do the iteration:
287 instaVol = thermo.getVolume();
288
289 for (int k = 0; k < maxIterNum_; k++) {
290 instaTemp = thermo.getTemperature();
291 instaPress = thermo.getPressure();
292
293 // evolve chi another half step using the temperature at t + dt/2
294 prevChi = thermostat.first;
295 thermostat.first = oldChi + dt2 * (instaTemp / targetTemp - 1.0) / tt2;
296
297 // evolve eta
298 this->evolveEtaB();
299 this->calcVelScale();
300
301 index = 0;
302 for (mol = info_->beginMolecule(i); mol != NULL;
303 mol = info_->nextMolecule(i)) {
304 for (sd = mol->beginIntegrableObject(j); sd != NULL;
305 sd = mol->nextIntegrableObject(j)) {
306 frc = sd->getFrc();
307 mass = sd->getMass();
308
309 getVelScaleB(sc, index);
310
311 // velocity half step
312 vel = oldVel[index] + dt2 * Constants::energyConvert / mass * frc -
313 dt2 * sc;
314
315 sd->setVel(vel);
316
317 if (sd->isDirectional()) {
318 // get and convert the torque to body frame
319 Tb = sd->lab2Body(sd->getTrq());
320
321 ji = oldJi[index] + dt2 * Constants::energyConvert * Tb -
322 dt2 * thermostat.first * oldJi[index];
323
324 sd->setJ(ji);
325 }
326
327 ++index;
328 }
329 }
330
331 rattle_->constraintB();
332
333 if ((fabs(prevChi - thermostat.first) <= chiTolerance) &&
334 this->etaConverged())
335 break;
336 }
337
338 // calculate integral of chidt
339 thermostat.second += dt2 * thermostat.first;
340
341 snap->setThermostat(thermostat);
342
343 flucQ_->moveB();
344 saveEta();
345 }
346
347 void NPT::resetIntegrator() {
348 snap->setThermostat(make_pair(0.0, 0.0));
349 resetEta();
350 }
351
352 void NPT::resetEta() {
353 Mat3x3d etaMat(0.0);
354 snap->setBarostat(etaMat);
355 }
356} // namespace OpenMD
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
Definition SimInfo.hpp:96
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.