OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
FluctuatingChargeLangevin.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 "FluctuatingChargeLangevin.hpp"
49
50#include <cmath>
51
53#include "utils/Constants.hpp"
54#include "utils/simError.h"
55
56namespace OpenMD {
57
58 FluctuatingChargeLangevin::FluctuatingChargeLangevin(SimInfo* info) :
59 FluctuatingChargePropagator(info), maxIterNum_(4), forceTolerance_(1e-6),
60 snap_(info->getSnapshotManager()->getCurrentSnapshot()),
61 randNumGen_(info->getRandomNumberGenerator()) {}
62
63 void FluctuatingChargeLangevin::initialize() {
64 FluctuatingChargePropagator::initialize();
65 if (hasFlucQ_) {
66 if (info_->getSimParams()->haveDt()) {
67 dt_ = info_->getSimParams()->getDt();
68 dt2_ = dt_ * 0.5;
69 } else {
70 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
71 "FluctuatingChargeLangevin Error: dt is not set\n");
72 painCave.isFatal = 1;
73 simError();
74 }
75
76 if (!fqParams_->haveTargetTemp()) {
77 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
78 "You can't use the FluctuatingChargeLangevin "
79 "propagator without a flucQ.targetTemp!\n");
80 painCave.isFatal = 1;
81 painCave.severity = OPENMD_ERROR;
82 simError();
83 } else {
84 targetTemp_ = fqParams_->getTargetTemp();
85 }
86
87 // We must set tauThermostat.
88
89 if (!fqParams_->haveDragCoefficient()) {
90 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
91 "If you use the FluctuatingChargeLangevin\n"
92 "\tpropagator, you must set flucQ.dragCoefficient .\n");
93
94 painCave.severity = OPENMD_ERROR;
95 painCave.isFatal = 1;
96 simError();
97 } else {
98 drag_ = fqParams_->getDragCoefficient();
99 }
100 }
101
102 RealType stdDev =
103 std::sqrt(2.0 * Constants::kb * targetTemp_ * drag_ / dt_);
104
105 forceDistribution_ = std::normal_distribution<RealType>(0.0, stdDev);
106 }
107
108 void FluctuatingChargeLangevin::moveA() {
109 if (!hasFlucQ_) return;
110
111 SimInfo::MoleculeIterator i;
112 Molecule::FluctuatingChargeIterator j;
113 Molecule* mol;
114 Atom* atom;
115 RealType cvel, cpos, cfrc, cmass;
116
117 for (mol = info_->beginMolecule(i); mol != NULL;
118 mol = info_->nextMolecule(i)) {
119 for (atom = mol->beginFluctuatingCharge(j); atom != NULL;
120 atom = mol->nextFluctuatingCharge(j)) {
121 cvel = atom->getFlucQVel();
122 cpos = atom->getFlucQPos();
123 cfrc = atom->getFlucQFrc();
124 cmass = atom->getChargeMass();
125
126 // velocity half step
127 cvel += dt2_ * cfrc / cmass;
128 // position whole step
129 cpos += dt_ * cvel;
130
131 atom->setFlucQVel(cvel);
132 atom->setFlucQPos(cpos);
133 }
134 }
135 }
136
137 void FluctuatingChargeLangevin::applyConstraints() {
138 if (!hasFlucQ_) return;
139
140 SimInfo::MoleculeIterator i;
141 Molecule::FluctuatingChargeIterator j;
142 Molecule* mol;
143 Atom* atom;
144 RealType cvel, cfrc, cmass, randomForce, frictionForce;
145 RealType velStep, oldFF; // used to test for convergence
146
147 for (mol = info_->beginMolecule(i); mol != NULL;
148 mol = info_->nextMolecule(i)) {
149 for (atom = mol->beginFluctuatingCharge(j); atom != NULL;
150 atom = mol->nextFluctuatingCharge(j)) {
151 randomForce = forceDistribution_(*randNumGen_);
152 atom->addFlucQFrc(randomForce);
153
154 // What remains contains velocity explicitly, but the velocity
155 // required is at the full step: v(t + h), while we have
156 // initially the velocity at the half step: v(t + h/2). We
157 // need to iterate to converge the friction force vector.
158
159 // this is the velocity at the half-step:
160
161 cvel = atom->getFlucQVel();
162
163 // estimate velocity at full-step using everything but
164 // friction forces:
165
166 cfrc = atom->getFlucQFrc();
167 cmass = atom->getChargeMass();
168 velStep = cvel + dt2_ * cfrc / cmass;
169
170 frictionForce = 0.0;
171
172 // iteration starts here:
173
174 for (int k = 0; k < maxIterNum_; k++) {
175 oldFF = frictionForce;
176 frictionForce = -drag_ * velStep;
177 // re-estimate velocities at full-step using friction forces:
178
179 velStep = cvel + dt2_ * (cfrc + frictionForce) / cmass;
180
181 // check for convergence
182
183 if (fabs(frictionForce - oldFF) <= forceTolerance_)
184 break; // iteration ends here
185 }
186 atom->addFlucQFrc(frictionForce);
187 }
188 }
189 fqConstraints_->applyConstraints();
190 }
191
192 void FluctuatingChargeLangevin::moveB() {
193 if (!hasFlucQ_) return;
194 SimInfo::MoleculeIterator i;
195 Molecule::FluctuatingChargeIterator j;
196 Molecule* mol;
197 Atom* atom;
198 RealType cfrc, cvel, cmass;
199
200 for (mol = info_->beginMolecule(i); mol != NULL;
201 mol = info_->nextMolecule(i)) {
202 for (atom = mol->beginFluctuatingCharge(j); atom != NULL;
203 atom = mol->nextFluctuatingCharge(j)) {
204 cvel = atom->getFlucQVel();
205 cfrc = atom->getFlucQFrc();
206 cmass = atom->getChargeMass();
207
208 // velocity half step
209 cvel += (dt2_ * cfrc) / cmass;
210
211 atom->setFlucQVel(cvel);
212 }
213 }
214 }
215} // namespace OpenMD
abstract class for propagating fluctuating charge variables
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.