OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
FluctuatingChargeNVT.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 "FluctuatingChargeNVT.hpp"
49
51#include "utils/Constants.hpp"
52#include "utils/simError.h"
53
54namespace OpenMD {
55
56 FluctuatingChargeNVT::FluctuatingChargeNVT(SimInfo* info) :
57 FluctuatingChargePropagator(info), maxIterNum_(4), chiTolerance_(1e-6),
58 snap(info->getSnapshotManager()->getCurrentSnapshot()), thermo(info) {}
59
60 void FluctuatingChargeNVT::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 "FluctuatingChargeNVT Error: dt is not set\n");
69 painCave.isFatal = 1;
70 simError();
71 }
72
73 if (!info_->getSimParams()->getUseIntialExtendedSystemState()) {
74 snap->setElectronicThermostat(make_pair(0.0, 0.0));
75 }
76
77 if (!fqParams_->haveTargetTemp()) {
78 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
79 "You can't use the FluctuatingChargeNVT "
80 "propagator without a flucQ.targetTemp!\n");
81 painCave.isFatal = 1;
82 painCave.severity = OPENMD_ERROR;
83 simError();
84 } else {
85 targetTemp_ = fqParams_->getTargetTemp();
86 }
87
88 // We must set tauThermostat.
89
90 if (!fqParams_->haveTauThermostat()) {
91 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
92 "If you use the FluctuatingChargeNVT\n"
93 "\tpropagator, you must set flucQ.tauThermostat .\n");
94
95 painCave.severity = OPENMD_ERROR;
96 painCave.isFatal = 1;
97 simError();
98 } else {
99 tauThermostat_ = fqParams_->getTauThermostat();
100 }
101 updateSizes();
102 }
103 }
104
105 void FluctuatingChargeNVT::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 pair<RealType, RealType> thermostat = snap->getElectronicThermostat();
115 RealType chi = thermostat.first;
116 RealType integralOfChidt = thermostat.second;
117 RealType instTemp = thermo.getElectronicTemperature();
118
119 for (mol = info_->beginMolecule(i); mol != NULL;
120 mol = info_->nextMolecule(i)) {
121 for (atom = mol->beginFluctuatingCharge(j); atom != NULL;
122 atom = mol->nextFluctuatingCharge(j)) {
123 cvel = atom->getFlucQVel();
124 cpos = atom->getFlucQPos();
125 cfrc = atom->getFlucQFrc();
126 cmass = atom->getChargeMass();
127
128 // velocity half step
129 cvel += dt2_ * cfrc / cmass - dt2_ * chi * cvel;
130 // position whole step
131 cpos += dt_ * cvel;
132
133 atom->setFlucQVel(cvel);
134 atom->setFlucQPos(cpos);
135 }
136 }
137
138 chi += dt2_ * (instTemp / targetTemp_ - 1.0) /
139 (tauThermostat_ * tauThermostat_);
140
141 integralOfChidt += chi * dt2_;
142 snap->setElectronicThermostat(make_pair(chi, integralOfChidt));
143 }
144
145 void FluctuatingChargeNVT::updateSizes() {
146 oldVel_.resize(info_->getNFluctuatingCharges());
147 }
148
149 void FluctuatingChargeNVT::moveB() {
150 if (!hasFlucQ_) return;
151 SimInfo::MoleculeIterator i;
152 Molecule::FluctuatingChargeIterator j;
153 Molecule* mol;
154 Atom* atom;
155 RealType instTemp;
156 pair<RealType, RealType> thermostat = snap->getElectronicThermostat();
157 RealType chi = thermostat.first;
158 RealType oldChi = chi;
159 RealType prevChi;
160 RealType integralOfChidt = thermostat.second;
161 int index;
162 RealType cfrc, cvel, cmass;
163
164 index = 0;
165 for (mol = info_->beginMolecule(i); mol != NULL;
166 mol = info_->nextMolecule(i)) {
167 for (atom = mol->beginFluctuatingCharge(j); atom != NULL;
168 atom = mol->nextFluctuatingCharge(j)) {
169 oldVel_[index] = atom->getFlucQVel();
170 ++index;
171 }
172 }
173
174 // do the iteration:
175
176 for (int k = 0; k < maxIterNum_; k++) {
177 index = 0;
178 instTemp = thermo.getElectronicTemperature();
179 // evolve chi another half step using the temperature at t + dt/2
180 prevChi = chi;
181 chi = oldChi + dt2_ * (instTemp / targetTemp_ - 1.0) /
182 (tauThermostat_ * tauThermostat_);
183
184 for (mol = info_->beginMolecule(i); mol != NULL;
185 mol = info_->nextMolecule(i)) {
186 for (atom = mol->beginFluctuatingCharge(j); atom != NULL;
187 atom = mol->nextFluctuatingCharge(j)) {
188 cfrc = atom->getFlucQFrc();
189 cmass = atom->getChargeMass();
190
191 // velocity half step
192 cvel = oldVel_[index] + dt2_ * cfrc / cmass -
193 dt2_ * chi * oldVel_[index];
194 atom->setFlucQVel(cvel);
195 ++index;
196 }
197 }
198 if (fabs(prevChi - chi) <= chiTolerance_) break;
199 }
200 integralOfChidt += dt2_ * chi;
201 snap->setElectronicThermostat(make_pair(chi, integralOfChidt));
202 }
203
204 void FluctuatingChargeNVT::resetPropagator() {
205 if (!hasFlucQ_) return;
206 snap->setElectronicThermostat(make_pair(0.0, 0.0));
207 }
208
209 RealType FluctuatingChargeNVT::calcConservedQuantity() {
210 if (!hasFlucQ_) return 0.0;
211 pair<RealType, RealType> thermostat = snap->getElectronicThermostat();
212 RealType chi = thermostat.first;
213 RealType integralOfChidt = thermostat.second;
214 RealType fkBT =
215 info_->getNFluctuatingCharges() * Constants::kB * targetTemp_;
216
217 RealType thermostat_kinetic = fkBT * tauThermostat_ * tauThermostat_ * chi *
218 chi / (2.0 * Constants::energyConvert);
219
220 RealType thermostat_potential =
221 fkBT * integralOfChidt / Constants::energyConvert;
222
223 return thermostat_kinetic + thermostat_potential;
224 }
225} // 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.