OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
LangevinPiston.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/LangevinPiston.hpp"
49
50#include <cmath>
51#include <random>
52
53#ifdef IS_MPI
54#include <mpi.h>
55#endif
56
57#include "brains/SimInfo.hpp"
58#include "brains/Thermo.hpp"
59#include "integrators/IntegratorCreator.hpp"
61#include "utils/Constants.hpp"
62#include "utils/simError.h"
63
64namespace OpenMD {
65
66 LangevinPiston::LangevinPiston(SimInfo* info) : NPT(info) {
67 // NkBT has units of amu Ang^2 fs^-2 :
68 NkBT = info_->getNGlobalIntegrableObjects() * Constants::kB * targetTemp;
69
70 // W_ has units of amu Ang^2
71 // W_ = 3.0 * NkBT * tb2;
72 W_ = NkBT * tb2; // our eta scales all three box directions
73
74 // gamma_ has units of fs^-1
75 if (!simParams->haveLangevinPistonDrag()) {
76 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
77 "To use the LangevinPiston integrator, you must "
78 "set langevinPistonDrag "
79 "(fs^-1).\n");
80 painCave.severity = OPENMD_ERROR;
81 painCave.isFatal = 1;
82 simError();
83 } else {
84 gamma_ = simParams->getLangevinPistonDrag();
85 }
86
87#ifdef IS_MPI
88 if (worldRank == 0) {
89#endif
90 randNumGen_ = info->getRandomNumberGenerator();
91
92 // standard deviation units: amu Angs^2 fs^-2
93 RealType stdDev =
94 std::sqrt(2.0 * W_ * gamma_ * Constants::kB * targetTemp / dt);
95
96 forceDistribution_ = std::normal_distribution<RealType>(0.0, stdDev);
97#ifdef IS_MPI
98 }
99#endif
100
101 // randomForce will have units amu Ang^2 fs^-2:
102 genRandomForce(randomForce_);
103 }
104
105 void LangevinPiston::moveA() {
106 SimInfo::MoleculeIterator i;
107 Molecule::IntegrableObjectIterator j;
108 Molecule* mol;
109 StuntDouble* sd;
110 Vector3d Tb, ji;
111 RealType mass;
112 Vector3d vel;
113 Vector3d pos;
114 Vector3d frc;
115 Vector3d sc;
116 int index;
117
118 loadEta();
119
120 instaTemp = thermo.getTemperature();
121 press = thermo.getPressureTensor();
122 instaPress = Constants::pressureConvert *
123 (press(0, 0) + press(1, 1) + press(2, 2)) / 3.0;
124 instaVol = thermo.getVolume();
125
126 Vector3d COM = thermo.getCom();
127
128 // evolve velocity half step
129
130 calcVelScale();
131
132 for (mol = info_->beginMolecule(i); mol != NULL;
133 mol = info_->nextMolecule(i)) {
134 for (sd = mol->beginIntegrableObject(j); sd != NULL;
135 sd = mol->nextIntegrableObject(j)) {
136 vel = sd->getVel();
137 frc = sd->getFrc();
138
139 mass = sd->getMass();
140
141 getVelScaleA(sc, vel);
142
143 // velocity half step
144
145 vel += dt2 * Constants::energyConvert / mass * frc - dt2 * sc;
146 sd->setVel(vel);
147
148 if (sd->isDirectional()) {
149 // get and convert the torque to body frame
150
151 Tb = sd->lab2Body(sd->getTrq());
152
153 // get the angular momentum, and propagate a half step
154
155 ji = sd->getJ();
156
157 ji += dt2 * Constants::energyConvert * Tb;
158
159 rotAlgo_->rotate(sd, ji, dt);
160
161 sd->setJ(ji);
162 }
163 }
164 }
165 // evolve eta a half step
166
167 evolveEtaA();
168 flucQ_->moveA();
169
170 index = 0;
171 for (mol = info_->beginMolecule(i); mol != NULL;
172 mol = info_->nextMolecule(i)) {
173 for (sd = mol->beginIntegrableObject(j); sd != NULL;
174 sd = mol->nextIntegrableObject(j)) {
175 oldPos[index++] = sd->getPos();
176 }
177 }
178
179 // the first estimation of r(t+dt) is equal to r(t)
180
181 for (int k = 0; k < maxIterNum_; k++) {
182 index = 0;
183 for (mol = info_->beginMolecule(i); mol != NULL;
184 mol = info_->nextMolecule(i)) {
185 for (sd = mol->beginIntegrableObject(j); sd != NULL;
186 sd = mol->nextIntegrableObject(j)) {
187 vel = sd->getVel();
188 pos = sd->getPos();
189
190 this->getPosScale(pos, COM, index, sc);
191
192 pos = oldPos[index] + dt * (vel + sc);
193 sd->setPos(pos);
194
195 ++index;
196 }
197 }
198
199 rattle_->constraintA();
200 }
201
202 // Scale the box after all the positions have been moved:
203
204 this->scaleSimBox();
205
206 saveEta();
207 }
208
209 void LangevinPiston::moveB(void) {
210 SimInfo::MoleculeIterator i;
211 Molecule::IntegrableObjectIterator j;
212 Molecule* mol;
213 StuntDouble* sd;
214 int index;
215 Vector3d Tb;
216 Vector3d ji;
217 Vector3d sc;
218 Vector3d vel;
219 Vector3d frc;
220 RealType mass;
221
222 loadEta();
223
224 // save velocity and angular momentum
225 index = 0;
226 for (mol = info_->beginMolecule(i); mol != NULL;
227 mol = info_->nextMolecule(i)) {
228 for (sd = mol->beginIntegrableObject(j); sd != NULL;
229 sd = mol->nextIntegrableObject(j)) {
230 oldVel[index] = sd->getVel();
231
232 if (sd->isDirectional()) oldJi[index] = sd->getJ();
233
234 ++index;
235 }
236 }
237
238 instaVol = thermo.getVolume();
239
240 for (int k = 0; k < maxIterNum_; k++) {
241 instaTemp = thermo.getTemperature();
242 instaPress = thermo.getPressure();
243
244 // evolve eta
245 this->evolveEtaB();
246 this->calcVelScale();
247
248 index = 0;
249 for (mol = info_->beginMolecule(i); mol != NULL;
250 mol = info_->nextMolecule(i)) {
251 for (sd = mol->beginIntegrableObject(j); sd != NULL;
252 sd = mol->nextIntegrableObject(j)) {
253 frc = sd->getFrc();
254 mass = sd->getMass();
255
256 getVelScaleB(sc, index);
257
258 // velocity half step
259 vel = oldVel[index] + dt2 * Constants::energyConvert / mass * frc -
260 dt2 * sc;
261
262 sd->setVel(vel);
263
264 if (sd->isDirectional()) {
265 // get and convert the torque to body frame
266 Tb = sd->lab2Body(sd->getTrq());
267
268 ji = oldJi[index] + dt2 * Constants::energyConvert * Tb;
269
270 sd->setJ(ji);
271 }
272
273 ++index;
274 }
275 }
276
277 rattle_->constraintB();
278 if (this->etaConverged()) break;
279 }
280
281 flucQ_->moveB();
282 saveEta();
283 }
284
285 void LangevinPiston::evolveEtaA() {
286 // volume is Angs^3
287 // pressures are in atm
288 // pressureConvert takes amu*fs^-2*Ang^-1 -> atm
289 eta += dt2 * (instaVol * (instaPress - targetPressure) /
290 (Constants::pressureConvert * W_) -
291 gamma_ * eta + randomForce_ / W_);
292 oldEta = eta;
293 }
294
295 void LangevinPiston::evolveEtaB() {
296 prevEta = eta;
297
298 genRandomForce(randomForce_);
299
300 eta = oldEta + dt2 * (instaVol * (instaPress - targetPressure) /
301 (Constants::pressureConvert * W_) -
302 gamma_ * eta + randomForce_ / W_);
303 }
304
305 void LangevinPiston::calcVelScale() { vScale = eta; }
306
307 void LangevinPiston::getVelScaleA(Vector3d& sc, const Vector3d& vel) {
308 sc = vScale * vel;
309 }
310
311 void LangevinPiston::getVelScaleB(Vector3d& sc, int index) {
312 sc = vScale * oldVel[index];
313 }
314
315 void LangevinPiston::getPosScale(const Vector3d& pos, const Vector3d& COM,
316 int index, Vector3d& sc) {
317 Vector3d rj = (oldPos[index] + pos) / (RealType)2.0 - COM;
318 sc = eta * rj;
319 }
320
321 void LangevinPiston::scaleSimBox() {
322 RealType scaleFactor;
323
324 // This is from solving the first order equation that defines eta
325 scaleFactor = exp(dt * eta);
326
327 if ((scaleFactor > 1.1) || (scaleFactor < 0.9)) {
328 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
329 "LangevinPiston error: Attempting a Box scaling of more than 10 "
330 "percent\n"
331 " check your tauBarostat, as it is probably too small!\n"
332 " eta = %lf, scaleFactor = %lf\n",
333 eta, scaleFactor);
334 painCave.isFatal = 1;
335 simError();
336 } else {
337 Mat3x3d hmat = snap->getHmat();
338 hmat *= scaleFactor;
339 snap->setHmat(hmat);
340 }
341 }
342
343 bool LangevinPiston::etaConverged() {
344 return (fabs(prevEta - eta) <= etaTolerance);
345 }
346
347 void LangevinPiston::loadEta() {
348 Mat3x3d etaMat = snap->getBarostat();
349 eta = etaMat(0, 0);
350 }
351
352 void LangevinPiston::saveEta() {
353 Mat3x3d etaMat(0.0);
354 etaMat(0, 0) = eta;
355 etaMat(1, 1) = eta;
356 etaMat(2, 2) = eta;
357 snap->setBarostat(etaMat);
358 }
359
360 void LangevinPiston::genRandomForce(RealType& randomForce) {
361#ifdef IS_MPI
362 if (worldRank == 0) {
363#endif
364 randomForce = forceDistribution_(*randNumGen_);
365#ifdef IS_MPI
366 }
367 // push this out to the other processors
368 // Same command on all nodes:
369 MPI_Bcast(&randomForce, 1, MPI_REALTYPE, 0, MPI_COMM_WORLD);
370#endif
371
372 return;
373 }
374} // 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.