--- trunk/src/integrators/NVT.cpp 2005/01/17 16:58:32 271 +++ trunk/src/integrators/NVT.cpp 2015/03/07 21:41:51 2071 @@ -1,4 +1,4 @@ - /* +/* * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. * * The University of Notre Dame grants you ("Licensee") a @@ -6,19 +6,10 @@ * redistribute this software in source and binary code form, provided * that the following conditions are met: * - * 1. Acknowledgement of the program authors must be made in any - * publication of scientific results based in part on use of the - * program. An acceptable form of acknowledgement is citation of - * the article in which the program was described (Matthew - * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher - * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented - * Parallel Simulation Engine for Molecular Dynamics," - * J. Comput. Chem. 26, pp. 252-271 (2005)) - * - * 2. Redistributions of source code must retain the above copyright + * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - * 3. Redistributions in binary form must reproduce the above copyright + * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the * distribution. @@ -37,240 +28,257 @@ * arising out of the use of or inability to use software, even if the * University of Notre Dame has been advised of the possibility of * such damages. + * + * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your + * research, please cite the appropriate papers when you publish your + * work. Good starting points are: + * + * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). + * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). + * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). + * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). + * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). */ #include "integrators/NVT.hpp" #include "primitives/Molecule.hpp" #include "utils/simError.h" -#include "utils/OOPSEConstant.hpp" +#include "utils/PhysicalConstants.hpp" -namespace oopse { +namespace OpenMD { -NVT::NVT(SimInfo* info) : VelocityVerletIntegrator(info), chiTolerance_ (1e-6), maxIterNum_(4) { + NVT::NVT(SimInfo* info) : VelocityVerletIntegrator(info), maxIterNum_(4), + chiTolerance_(1e-6) { Globals* simParams = info_->getSimParams(); - if (!simParams->getUseInitXSstate()) { - Snapshot* currSnapshot = info_->getSnapshotManager()->getCurrentSnapshot(); - currSnapshot->setChi(0.0); - currSnapshot->setIntegralOfChiDt(0.0); + if (!simParams->getUseIntialExtendedSystemState()) { + Snapshot* snap = info_->getSnapshotManager()->getCurrentSnapshot(); + snap->setThermostat(make_pair(0.0, 0.0)); } if (!simParams->haveTargetTemp()) { - sprintf(painCave.errMsg, "You can't use the NVT integrator without a targetTemp_!\n"); - painCave.isFatal = 1; - painCave.severity = OOPSE_ERROR; - simError(); + sprintf(painCave.errMsg, "You can't use the NVT integrator without a targetTemp_!\n"); + painCave.isFatal = 1; + painCave.severity = OPENMD_ERROR; + simError(); } else { - targetTemp_ = simParams->getTargetTemp(); + targetTemp_ = simParams->getTargetTemp(); } - // We must set tauThermostat_. + // We must set tauThermostat. if (!simParams->haveTauThermostat()) { - sprintf(painCave.errMsg, "If you use the constant temperature\n" - "\tintegrator, you must set tauThermostat_.\n"); + sprintf(painCave.errMsg, "If you use the constant temperature\n" + "\tintegrator, you must set tauThermostat.\n"); - painCave.severity = OOPSE_ERROR; - painCave.isFatal = 1; - simError(); + painCave.severity = OPENMD_ERROR; + painCave.isFatal = 1; + simError(); } else { - tauThermostat_ = simParams->getTauThermostat(); + tauThermostat_ = simParams->getTauThermostat(); } - update(); -} + updateSizes(); + } -void NVT::doUpdate() { + void NVT::doUpdateSizes() { oldVel_.resize(info_->getNIntegrableObjects()); - oldJi_.resize(info_->getNIntegrableObjects()); -} -void NVT::moveA() { + oldJi_.resize(info_->getNIntegrableObjects()); + } + + void NVT::moveA() { SimInfo::MoleculeIterator i; Molecule::IntegrableObjectIterator j; Molecule* mol; - StuntDouble* integrableObject; + StuntDouble* sd; Vector3d Tb; Vector3d ji; - double mass; + RealType mass; Vector3d vel; Vector3d pos; Vector3d frc; - double chi = currentSnapshot_->getChi(); - double integralOfChidt = currentSnapshot_->getIntegralOfChiDt(); - + pair thermostat = snap->getThermostat(); + // We need the temperature at time = t for the chi update below: - double instTemp = thermo.getTemperature(); + RealType instTemp = thermo.getTemperature(); - for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) { - for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL; - integrableObject = mol->nextIntegrableObject(j)) { + for (mol = info_->beginMolecule(i); mol != NULL; + mol = info_->nextMolecule(i)) { - vel = integrableObject->getVel(); - pos = integrableObject->getPos(); - frc = integrableObject->getFrc(); + for (sd = mol->beginIntegrableObject(j); sd != NULL; + sd = mol->nextIntegrableObject(j)) { - mass = integrableObject->getMass(); + vel = sd->getVel(); + pos = sd->getPos(); + frc = sd->getFrc(); - // velocity half step (use chi from previous step here): - //vel[j] += dt2 * ((frc[j] / mass ) * OOPSEConstant::energyConvert - vel[j]*chi); - vel += dt2 *OOPSEConstant::energyConvert/mass*frc - dt2*chi*vel; + mass = sd->getMass(); + + // velocity half step (use chi from previous step here): + vel += dt2 *PhysicalConstants::energyConvert/mass*frc + - dt2*thermostat.first*vel; // position whole step - //pos[j] += dt * vel[j]; pos += dt * vel; - integrableObject->setVel(vel); - integrableObject->setPos(pos); + sd->setVel(vel); + sd->setPos(pos); - if (integrableObject->isDirectional()) { + if (sd->isDirectional()) { - //convert the torque to body frame - Tb = integrableObject->lab2Body(integrableObject->getTrq()); + //convert the torque to body frame + Tb = sd->lab2Body(sd->getTrq()); - // get the angular momentum, and propagate a half step + // get the angular momentum, and propagate a half step - ji = integrableObject->getJ(); + ji = sd->getJ(); - //ji[j] += dt2 * (Tb[j] * OOPSEConstant::energyConvert - ji[j]*chi); - ji += dt2*OOPSEConstant::energyConvert*Tb - dt2*chi *ji; - rotAlgo->rotate(integrableObject, ji, dt); + ji += dt2*PhysicalConstants::energyConvert*Tb + - dt2*thermostat.first *ji; - integrableObject->setJ(ji); + rotAlgo_->rotate(sd, ji, dt); + + sd->setJ(ji); } - } + } } - rattle->constraintA(); + flucQ_->moveA(); + rattle_->constraintA(); // Finally, evolve chi a half step (just like a velocity) using // temperature at time t, not time t+dt/2 - - chi += dt2 * (instTemp / targetTemp_ - 1.0) / (tauThermostat_ * tauThermostat_); - integralOfChidt += chi * dt2; + thermostat.first += dt2 * (instTemp / targetTemp_ - 1.0) + / (tauThermostat_ * tauThermostat_); + thermostat.second += thermostat.first * dt2; - currentSnapshot_->setChi(chi); - currentSnapshot_->setIntegralOfChiDt(integralOfChidt); -} + snap->setThermostat(thermostat); + } -void NVT::moveB() { + void NVT::moveB() { SimInfo::MoleculeIterator i; Molecule::IntegrableObjectIterator j; Molecule* mol; - StuntDouble* integrableObject; + StuntDouble* sd; Vector3d Tb; Vector3d ji; Vector3d vel; Vector3d frc; - double mass; - double instTemp; + RealType mass; + RealType instTemp; int index; // Set things up for the iteration: - double chi = currentSnapshot_->getChi(); - double oldChi = chi; - double prevChi; - double integralOfChidt = currentSnapshot_->getIntegralOfChiDt(); + pair thermostat = snap->getThermostat(); + RealType oldChi = thermostat.first; + RealType prevChi; index = 0; - for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) { - for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL; - integrableObject = mol->nextIntegrableObject(j)) { - oldVel_[index] = integrableObject->getVel(); - oldJi_[index] = integrableObject->getJ(); + for (mol = info_->beginMolecule(i); mol != NULL; + mol = info_->nextMolecule(i)) { - ++index; - } - + for (sd = mol->beginIntegrableObject(j); sd != NULL; + sd = mol->nextIntegrableObject(j)) { + + oldVel_[index] = sd->getVel(); + + if (sd->isDirectional()) + oldJi_[index] = sd->getJ(); + + ++index; + } } // do the iteration: for(int k = 0; k < maxIterNum_; k++) { - index = 0; - instTemp = thermo.getTemperature(); + index = 0; + instTemp = thermo.getTemperature(); - // evolve chi another half step using the temperature at t + dt/2 + // evolve chi another half step using the temperature at t + dt/2 - prevChi = chi; - chi = oldChi + dt2 * (instTemp / targetTemp_ - 1.0) / (tauThermostat_ * tauThermostat_); + prevChi = thermostat.first; + thermostat.first = oldChi + dt2 * (instTemp / targetTemp_ - 1.0) + / (tauThermostat_ * tauThermostat_); - for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) { - for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL; - integrableObject = mol->nextIntegrableObject(j)) { + for (mol = info_->beginMolecule(i); mol != NULL; + mol = info_->nextMolecule(i)) { + + for (sd = mol->beginIntegrableObject(j); sd != NULL; + sd = mol->nextIntegrableObject(j)) { - frc = integrableObject->getFrc(); - vel = integrableObject->getVel(); + frc = sd->getFrc(); + mass = sd->getMass(); - mass = integrableObject->getMass(); + // velocity half step - // velocity half step - //for(j = 0; j < 3; j++) - // vel[j] = oldVel_[3*i+j] + dt2 * ((frc[j] / mass ) * OOPSEConstant::energyConvert - oldVel_[3*i + j]*chi); - vel = oldVel_[index] + dt2/mass*OOPSEConstant::energyConvert * frc - dt2*chi*oldVel_[index]; + vel = oldVel_[index] + + dt2/mass*PhysicalConstants::energyConvert * frc + - dt2*thermostat.first*oldVel_[index]; - integrableObject->setVel(vel); + sd->setVel(vel); - if (integrableObject->isDirectional()) { + if (sd->isDirectional()) { - // get and convert the torque to body frame + // get and convert the torque to body frame - Tb = integrableObject->lab2Body(integrableObject->getTrq()); + Tb = sd->lab2Body(sd->getTrq()); - //for(j = 0; j < 3; j++) - // ji[j] = oldJi_[3*i + j] + dt2 * (Tb[j] * OOPSEConstant::energyConvert - oldJi_[3*i+j]*chi); - ji = oldJi_[index] + dt2*OOPSEConstant::energyConvert*Tb - dt2*chi *oldJi_[index]; + ji = oldJi_[index] + dt2*PhysicalConstants::energyConvert*Tb + - dt2*thermostat.first *oldJi_[index]; - integrableObject->setJ(ji); - } + sd->setJ(ji); + } - ++index; - } - } + ++index; + } + } + rattle_->constraintB(); - rattle->constraintB(); + if (fabs(prevChi - thermostat.first) <= chiTolerance_) + break; - if (fabs(prevChi - chi) <= chiTolerance_) - break; - } - integralOfChidt += dt2 * chi; + flucQ_->moveB(); - currentSnapshot_->setChi(chi); - currentSnapshot_->setIntegralOfChiDt(integralOfChidt); -} + thermostat.second += dt2 * thermostat.first; + snap->setThermostat(thermostat); + } + void NVT::resetIntegrator() { + snap->setThermostat(make_pair(0.0, 0.0)); + } + + RealType NVT::calcConservedQuantity() { -double NVT::calcConservedQuantity() { - - double chi = currentSnapshot_->getChi(); - double integralOfChidt = currentSnapshot_->getIntegralOfChiDt(); - double conservedQuantity; - double fkBT; - double Energy; - double thermostat_kinetic; - double thermostat_potential; + pair thermostat = snap->getThermostat(); + RealType conservedQuantity; + RealType fkBT; + RealType Energy; + RealType thermostat_kinetic; + RealType thermostat_potential; - fkBT = info_->getNdf() *OOPSEConstant::kB *targetTemp_; + fkBT = info_->getNdf() *PhysicalConstants::kB *targetTemp_; - Energy = thermo.getTotalE(); + Energy = thermo.getTotalEnergy(); - thermostat_kinetic = fkBT * tauThermostat_ * tauThermostat_ * chi * chi / (2.0 * OOPSEConstant::energyConvert); + thermostat_kinetic = fkBT * tauThermostat_ * tauThermostat_ * thermostat.first * thermostat.first / (2.0 * PhysicalConstants::energyConvert); - thermostat_potential = fkBT * integralOfChidt / OOPSEConstant::energyConvert; + thermostat_potential = fkBT * thermostat.second / PhysicalConstants::energyConvert; conservedQuantity = Energy + thermostat_kinetic + thermostat_potential; return conservedQuantity; -} + } -}//end namespace oopse +}//end namespace OpenMD