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