/* -------------------------------------------------------------------------- * * SimTK Core: SimTK Simbody(tm) * * -------------------------------------------------------------------------- * * This is part of the SimTK Core biosimulation toolkit originating from * * Simbios, the NIH National Center for Physics-Based Simulation of * * Biological Structures at Stanford, funded under the NIH Roadmap for * * Medical Research, grant U54 GM072970. See https://simtk.org. * * * * Portions copyright (c) 2007 Stanford University and the Authors. * * Authors: Peter Eastman * * Contributors: * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the "Software"), * * to deal in the Software without restriction, including without limitation * * the rights to use, copy, modify, merge, publish, distribute, sublicense, * * and/or sell copies of the Software, and to permit persons to whom the * * Software is furnished to do so, subject to the following conditions: * * * * The above copyright notice and this permission notice shall be included in * * all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * * USE OR OTHER DEALINGS IN THE SOFTWARE. * * -------------------------------------------------------------------------- */ #include "SimTKcommon.h" #include "molmodel/internal/VelocityRescalingThermostat.h" #include "molmodel/internal/md_units.h" #include using namespace SimTK; using namespace SimTK::units::md; /** * This class is the internal implementation for VelocityRescalingThermostat. */ class VelocityRescalingThermostat::VelocityRescalingThermostatImpl { public: VelocityRescalingThermostatImpl(const MultibodySystem& system, MDTemperature temperature) : system(system), temperature(temperature) { } MDTemperature getTemperature() { return temperature; } void setTemperature(MDTemperature temp) { temperature = temp; } void handleEvent(State& state, Real accuracy, const Vector& yWeights, const Vector& ooConstraintTols, Stage& lowestModified, bool& shouldTerminate) const { const MDEnergy ke = system.calcKineticEnergy(state) * kilojoules_per_mole; if (ke == 0*kilojoules_per_mole) return; // This is the number of dofs. TODO: we're ignoring constraint redundancy // but we shouldn't be! That could result in negative dofs, so we'll // make sure that doesn't happen. But don't expect meaningful results // in that case. Note that it is the acceleration-level constraints that // matter; they remove dofs regardless of whether there is a corresponding // velocity constraint. const int dof = std::max(0, state.getNU() - state.getNUDotErr()); const MDInverseTemperature ooCurrentTemp = (dof*units::md::BoltzmannConstant)/(2*ke); const Real scale = std::sqrt(temperature*ooCurrentTemp); // sqrt(desiredT/currentT) state.updU() *= scale; lowestModified = Stage::Velocity; } private: const MultibodySystem& system; MDTemperature temperature; }; VelocityRescalingThermostat::VelocityRescalingThermostat( const MultibodySystem& system, MDTemperature temperature, MDTime rescalingInterval) : PeriodicEventHandler(rescalingInterval/picoseconds) { impl = new VelocityRescalingThermostatImpl(system, temperature); } VelocityRescalingThermostat::~VelocityRescalingThermostat() { delete impl; } MDTemperature VelocityRescalingThermostat::getTemperature() { return impl->getTemperature(); } void VelocityRescalingThermostat::setTemperature(MDTemperature temp) { impl->setTemperature(temp); } void VelocityRescalingThermostat::handleEvent(State& state, Real accuracy, const Vector& yWeights, const Vector& ooConstraintTols, Stage& lowestModified, bool& shouldTerminate) const { impl->handleEvent(state, accuracy, yWeights, ooConstraintTols, lowestModified, shouldTerminate); }