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