48#include "integrators/LangevinPiston.hpp"
58#include "brains/Thermo.hpp"
59#include "integrators/IntegratorCreator.hpp"
61#include "utils/Constants.hpp"
62#include "utils/simError.h"
66 LangevinPiston::LangevinPiston(
SimInfo* info) :
NPT(info) {
68 NkBT = info_->getNGlobalIntegrableObjects() * Constants::kB * targetTemp;
75 if (!simParams->haveLangevinPistonDrag()) {
76 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
77 "To use the LangevinPiston integrator, you must "
78 "set langevinPistonDrag "
80 painCave.severity = OPENMD_ERROR;
84 gamma_ = simParams->getLangevinPistonDrag();
90 randNumGen_ = info->getRandomNumberGenerator();
94 std::sqrt(2.0 * W_ * gamma_ * Constants::kB * targetTemp / dt);
96 forceDistribution_ = std::normal_distribution<RealType>(0.0, stdDev);
102 genRandomForce(randomForce_);
105 void LangevinPiston::moveA() {
106 SimInfo::MoleculeIterator i;
107 Molecule::IntegrableObjectIterator j;
120 instaTemp = thermo.getTemperature();
121 press = thermo.getPressureTensor();
122 instaPress = Constants::pressureConvert *
123 (press(0, 0) + press(1, 1) + press(2, 2)) / 3.0;
124 instaVol = thermo.getVolume();
126 Vector3d COM = thermo.getCom();
132 for (mol = info_->beginMolecule(i); mol != NULL;
133 mol = info_->nextMolecule(i)) {
134 for (sd = mol->beginIntegrableObject(j); sd != NULL;
135 sd = mol->nextIntegrableObject(j)) {
139 mass = sd->getMass();
141 getVelScaleA(sc, vel);
145 vel += dt2 * Constants::energyConvert / mass * frc - dt2 * sc;
148 if (sd->isDirectional()) {
151 Tb = sd->lab2Body(sd->getTrq());
157 ji += dt2 * Constants::energyConvert * Tb;
159 rotAlgo_->rotate(sd, ji, dt);
171 for (mol = info_->beginMolecule(i); mol != NULL;
172 mol = info_->nextMolecule(i)) {
173 for (sd = mol->beginIntegrableObject(j); sd != NULL;
174 sd = mol->nextIntegrableObject(j)) {
175 oldPos[index++] = sd->getPos();
181 for (
int k = 0; k < maxIterNum_; k++) {
183 for (mol = info_->beginMolecule(i); mol != NULL;
184 mol = info_->nextMolecule(i)) {
185 for (sd = mol->beginIntegrableObject(j); sd != NULL;
186 sd = mol->nextIntegrableObject(j)) {
190 this->getPosScale(pos, COM, index, sc);
192 pos = oldPos[index] + dt * (vel + sc);
199 rattle_->constraintA();
209 void LangevinPiston::moveB(
void) {
210 SimInfo::MoleculeIterator i;
211 Molecule::IntegrableObjectIterator j;
226 for (mol = info_->beginMolecule(i); mol != NULL;
227 mol = info_->nextMolecule(i)) {
228 for (sd = mol->beginIntegrableObject(j); sd != NULL;
229 sd = mol->nextIntegrableObject(j)) {
230 oldVel[index] = sd->getVel();
232 if (sd->isDirectional()) oldJi[index] = sd->getJ();
238 instaVol = thermo.getVolume();
240 for (
int k = 0; k < maxIterNum_; k++) {
241 instaTemp = thermo.getTemperature();
242 instaPress = thermo.getPressure();
246 this->calcVelScale();
249 for (mol = info_->beginMolecule(i); mol != NULL;
250 mol = info_->nextMolecule(i)) {
251 for (sd = mol->beginIntegrableObject(j); sd != NULL;
252 sd = mol->nextIntegrableObject(j)) {
254 mass = sd->getMass();
256 getVelScaleB(sc, index);
259 vel = oldVel[index] + dt2 * Constants::energyConvert / mass * frc -
264 if (sd->isDirectional()) {
266 Tb = sd->lab2Body(sd->getTrq());
268 ji = oldJi[index] + dt2 * Constants::energyConvert * Tb;
277 rattle_->constraintB();
278 if (this->etaConverged())
break;
285 void LangevinPiston::evolveEtaA() {
289 eta += dt2 * (instaVol * (instaPress - targetPressure) /
290 (Constants::pressureConvert * W_) -
291 gamma_ * eta + randomForce_ / W_);
295 void LangevinPiston::evolveEtaB() {
298 genRandomForce(randomForce_);
300 eta = oldEta + dt2 * (instaVol * (instaPress - targetPressure) /
301 (Constants::pressureConvert * W_) -
302 gamma_ * eta + randomForce_ / W_);
305 void LangevinPiston::calcVelScale() { vScale = eta; }
307 void LangevinPiston::getVelScaleA(Vector3d& sc,
const Vector3d& vel) {
311 void LangevinPiston::getVelScaleB(Vector3d& sc,
int index) {
312 sc = vScale * oldVel[index];
315 void LangevinPiston::getPosScale(
const Vector3d& pos,
const Vector3d& COM,
316 int index, Vector3d& sc) {
317 Vector3d rj = (oldPos[index] + pos) / (RealType)2.0 - COM;
321 void LangevinPiston::scaleSimBox() {
322 RealType scaleFactor;
325 scaleFactor = exp(dt * eta);
327 if ((scaleFactor > 1.1) || (scaleFactor < 0.9)) {
328 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
329 "LangevinPiston error: Attempting a Box scaling of more than 10 "
331 " check your tauBarostat, as it is probably too small!\n"
332 " eta = %lf, scaleFactor = %lf\n",
334 painCave.isFatal = 1;
337 Mat3x3d hmat = snap->getHmat();
343 bool LangevinPiston::etaConverged() {
344 return (fabs(prevEta - eta) <= etaTolerance);
347 void LangevinPiston::loadEta() {
348 Mat3x3d etaMat = snap->getBarostat();
352 void LangevinPiston::saveEta() {
357 snap->setBarostat(etaMat);
360 void LangevinPiston::genRandomForce(RealType& randomForce) {
362 if (worldRank == 0) {
364 randomForce = forceDistribution_(*randNumGen_);
369 MPI_Bcast(&randomForce, 1, MPI_REALTYPE, 0, MPI_COMM_WORLD);
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.