48#include "FluctuatingChargeNVT.hpp"
51#include "utils/Constants.hpp"
52#include "utils/simError.h"
56 FluctuatingChargeNVT::FluctuatingChargeNVT(
SimInfo* info) :
58 snap(info->getSnapshotManager()->getCurrentSnapshot()), thermo(info) {}
60 void FluctuatingChargeNVT::initialize() {
61 FluctuatingChargePropagator::initialize();
63 if (info_->getSimParams()->haveDt()) {
64 dt_ = info_->getSimParams()->getDt();
67 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
68 "FluctuatingChargeNVT Error: dt is not set\n");
73 if (!info_->getSimParams()->getUseIntialExtendedSystemState()) {
74 snap->setElectronicThermostat(make_pair(0.0, 0.0));
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");
82 painCave.severity = OPENMD_ERROR;
85 targetTemp_ = fqParams_->getTargetTemp();
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");
95 painCave.severity = OPENMD_ERROR;
99 tauThermostat_ = fqParams_->getTauThermostat();
105 void FluctuatingChargeNVT::moveA() {
106 if (!hasFlucQ_)
return;
108 SimInfo::MoleculeIterator i;
109 Molecule::FluctuatingChargeIterator j;
112 RealType cvel, cpos, cfrc, cmass;
114 pair<RealType, RealType> thermostat = snap->getElectronicThermostat();
115 RealType chi = thermostat.first;
116 RealType integralOfChidt = thermostat.second;
117 RealType instTemp = thermo.getElectronicTemperature();
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();
129 cvel += dt2_ * cfrc / cmass - dt2_ * chi * cvel;
133 atom->setFlucQVel(cvel);
134 atom->setFlucQPos(cpos);
138 chi += dt2_ * (instTemp / targetTemp_ - 1.0) /
139 (tauThermostat_ * tauThermostat_);
141 integralOfChidt += chi * dt2_;
142 snap->setElectronicThermostat(make_pair(chi, integralOfChidt));
145 void FluctuatingChargeNVT::updateSizes() {
146 oldVel_.resize(info_->getNFluctuatingCharges());
149 void FluctuatingChargeNVT::moveB() {
150 if (!hasFlucQ_)
return;
151 SimInfo::MoleculeIterator i;
152 Molecule::FluctuatingChargeIterator j;
156 pair<RealType, RealType> thermostat = snap->getElectronicThermostat();
157 RealType chi = thermostat.first;
158 RealType oldChi = chi;
160 RealType integralOfChidt = thermostat.second;
162 RealType cfrc, cvel, cmass;
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();
176 for (
int k = 0; k < maxIterNum_; k++) {
178 instTemp = thermo.getElectronicTemperature();
181 chi = oldChi + dt2_ * (instTemp / targetTemp_ - 1.0) /
182 (tauThermostat_ * tauThermostat_);
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();
192 cvel = oldVel_[index] + dt2_ * cfrc / cmass -
193 dt2_ * chi * oldVel_[index];
194 atom->setFlucQVel(cvel);
198 if (fabs(prevChi - chi) <= chiTolerance_)
break;
200 integralOfChidt += dt2_ * chi;
201 snap->setElectronicThermostat(make_pair(chi, integralOfChidt));
204 void FluctuatingChargeNVT::resetPropagator() {
205 if (!hasFlucQ_)
return;
206 snap->setElectronicThermostat(make_pair(0.0, 0.0));
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;
215 info_->getNFluctuatingCharges() * Constants::kB * targetTemp_;
217 RealType thermostat_kinetic = fkBT * tauThermostat_ * tauThermostat_ * chi *
218 chi / (2.0 * Constants::energyConvert);
220 RealType thermostat_potential =
221 fkBT * integralOfChidt / Constants::energyConvert;
223 return thermostat_kinetic + thermostat_potential;
abstract class for propagating fluctuating charge variables
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.