OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
NVT.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/NVT.hpp"
49
51#include "utils/Constants.hpp"
52#include "utils/simError.h"
53
54namespace OpenMD {
55
56 NVT::NVT(SimInfo* info) :
57 VelocityVerletIntegrator(info), maxIterNum_(4), chiTolerance_(1e-6) {
58 Globals* simParams = info_->getSimParams();
59
60 if (!simParams->getUseIntialExtendedSystemState()) {
61 Snapshot* snap = info_->getSnapshotManager()->getCurrentSnapshot();
62 snap->setThermostat(make_pair(0.0, 0.0));
63 }
64
65 if (!simParams->haveTargetTemp()) {
66 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
67 "You can't use the NVT integrator without a targetTemp_!\n");
68 painCave.isFatal = 1;
69 painCave.severity = OPENMD_ERROR;
70 simError();
71 } else {
72 targetTemp_ = simParams->getTargetTemp();
73 }
74
75 // We must set tauThermostat.
76
77 if (!simParams->haveTauThermostat()) {
78 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
79 "If you use the constant temperature\n"
80 "\tintegrator, you must set tauThermostat.\n");
81
82 painCave.severity = OPENMD_ERROR;
83 painCave.isFatal = 1;
84 simError();
85 } else {
86 tauThermostat_ = simParams->getTauThermostat();
87 }
88
89 updateSizes();
90 }
91
92 void NVT::doUpdateSizes() {
93 oldVel_.resize(info_->getNIntegrableObjects());
94 oldJi_.resize(info_->getNIntegrableObjects());
95 }
96
97 void NVT::moveA() {
98 SimInfo::MoleculeIterator i;
99 Molecule::IntegrableObjectIterator j;
100 Molecule* mol;
101 StuntDouble* sd;
102 Vector3d Tb;
103 Vector3d ji;
104 RealType mass;
105 Vector3d vel;
106 Vector3d pos;
107 Vector3d frc;
108
109 pair<RealType, RealType> thermostat = snap->getThermostat();
110
111 // We need the temperature at time = t for the chi update below:
112
113 RealType instTemp = thermo.getTemperature();
114
115 for (mol = info_->beginMolecule(i); mol != NULL;
116 mol = info_->nextMolecule(i)) {
117 for (sd = mol->beginIntegrableObject(j); sd != NULL;
118 sd = mol->nextIntegrableObject(j)) {
119 vel = sd->getVel();
120 pos = sd->getPos();
121 frc = sd->getFrc();
122
123 mass = sd->getMass();
124
125 // velocity half step (use chi from previous step here):
126 vel += dt2 * Constants::energyConvert / mass * frc -
127 dt2 * thermostat.first * vel;
128
129 // position whole step
130 pos += dt * vel;
131
132 sd->setVel(vel);
133 sd->setPos(pos);
134
135 if (sd->isDirectional()) {
136 // convert the torque to body frame
137 Tb = sd->lab2Body(sd->getTrq());
138
139 // get the angular momentum, and propagate a half step
140
141 ji = sd->getJ();
142
143 ji +=
144 dt2 * Constants::energyConvert * Tb - dt2 * thermostat.first * ji;
145
146 rotAlgo_->rotate(sd, ji, dt);
147
148 sd->setJ(ji);
149 }
150 }
151 }
152
153 flucQ_->moveA();
154 rattle_->constraintA();
155
156 // Finally, evolve chi a half step (just like a velocity) using
157 // temperature at time t, not time t+dt/2
158
159 thermostat.first += dt2 * (instTemp / targetTemp_ - 1.0) /
160 (tauThermostat_ * tauThermostat_);
161 thermostat.second += thermostat.first * dt2;
162
163 snap->setThermostat(thermostat);
164 }
165
166 void NVT::moveB() {
167 SimInfo::MoleculeIterator i;
168 Molecule::IntegrableObjectIterator j;
169 Molecule* mol;
170 StuntDouble* sd;
171
172 Vector3d Tb;
173 Vector3d ji;
174 Vector3d vel;
175 Vector3d frc;
176 RealType mass;
177 RealType instTemp;
178 int index;
179 // Set things up for the iteration:
180
181 pair<RealType, RealType> thermostat = snap->getThermostat();
182 RealType oldChi = thermostat.first;
183 RealType prevChi;
184
185 index = 0;
186 for (mol = info_->beginMolecule(i); mol != NULL;
187 mol = info_->nextMolecule(i)) {
188 for (sd = mol->beginIntegrableObject(j); sd != NULL;
189 sd = mol->nextIntegrableObject(j)) {
190 oldVel_[index] = sd->getVel();
191
192 if (sd->isDirectional()) oldJi_[index] = sd->getJ();
193
194 ++index;
195 }
196 }
197
198 // do the iteration:
199
200 for (int k = 0; k < maxIterNum_; k++) {
201 index = 0;
202 instTemp = thermo.getTemperature();
203
204 // evolve chi another half step using the temperature at t + dt/2
205
206 prevChi = thermostat.first;
207 thermostat.first = oldChi + dt2 * (instTemp / targetTemp_ - 1.0) /
208 (tauThermostat_ * tauThermostat_);
209
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 frc = sd->getFrc();
215 mass = sd->getMass();
216
217 // velocity half step
218
219 vel = oldVel_[index] + dt2 / mass * Constants::energyConvert * frc -
220 dt2 * thermostat.first * oldVel_[index];
221
222 sd->setVel(vel);
223
224 if (sd->isDirectional()) {
225 // get and convert the torque to body frame
226
227 Tb = sd->lab2Body(sd->getTrq());
228
229 ji = oldJi_[index] + dt2 * Constants::energyConvert * Tb -
230 dt2 * thermostat.first * oldJi_[index];
231
232 sd->setJ(ji);
233 }
234
235 ++index;
236 }
237 }
238
239 rattle_->constraintB();
240
241 if (fabs(prevChi - thermostat.first) <= chiTolerance_) break;
242 }
243
244 flucQ_->moveB();
245
246 thermostat.second += dt2 * thermostat.first;
247 snap->setThermostat(thermostat);
248 }
249
250 void NVT::resetIntegrator() { snap->setThermostat(make_pair(0.0, 0.0)); }
251
252 RealType NVT::calcConservedQuantity() {
253 pair<RealType, RealType> thermostat = snap->getThermostat();
254 RealType conservedQuantity;
255 RealType fkBT;
256 RealType Energy;
257 RealType thermostat_kinetic;
258 RealType thermostat_potential;
259
260 fkBT = info_->getNdf() * Constants::kB * targetTemp_;
261
262 Energy = thermo.getTotalEnergy();
263
264 thermostat_kinetic = fkBT * tauThermostat_ * tauThermostat_ *
265 thermostat.first * thermostat.first /
266 (2.0 * Constants::energyConvert);
267
268 thermostat_potential = fkBT * thermostat.second / Constants::energyConvert;
269
270 conservedQuantity = Energy + thermostat_kinetic + thermostat_potential;
271
272 return conservedQuantity;
273 }
274
275} // 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.