Commit 6ddebdb2 authored by Peter Eastman's avatar Peter Eastman
Browse files

Reorganization of the OpenMM source code

parent 0e879806
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* This is part of the OpenMM molecular simulation 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) 2008 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 "BrownianIntegrator.h"
#include "OpenMMContext.h"
#include "internal/OpenMMContextImpl.h"
#include "kernels.h"
#include <string>
using namespace OpenMM;
using std::string;
using std::vector;
BrownianIntegrator::BrownianIntegrator(double temperature, double frictionCoeff, double stepSize) {
setTemperature(temperature);
setFriction(frictionCoeff);
setStepSize(stepSize);
}
void BrownianIntegrator::initialize(OpenMMContextImpl& contextRef) {
context = &contextRef;
kernel = context->getPlatform().createKernel(IntegrateBrownianStepKernel::Name());
const System& system = context->getSystem();
vector<double> masses(system.getNumAtoms());
vector<std::vector<int> > constraintIndices(system.getNumConstraints());
vector<double> constraintLengths(system.getNumConstraints());
for (int i = 0; i < system.getNumAtoms(); ++i)
masses[i] = system.getAtomMass(i);
for (int i = 0; i < system.getNumConstraints(); ++i) {
int atom1, atom2;
double distance;
system.getConstraintParameters(i, atom1, atom2, distance);
constraintIndices[i].push_back(atom1);
constraintIndices[i].push_back(atom2);
constraintLengths[i] = distance;
}
dynamic_cast<IntegrateBrownianStepKernel&>(kernel.getImpl()).initialize(masses, constraintIndices, constraintLengths);
}
vector<string> BrownianIntegrator::getKernelNames() {
std::vector<std::string> names;
names.push_back(IntegrateBrownianStepKernel::Name());
return names;
}
void BrownianIntegrator::step(int steps) {
for (int i = 0; i < steps; ++i) {
context->updateContextState();
context->calcForces();
dynamic_cast<IntegrateBrownianStepKernel&>(kernel.getImpl()).execute(context->getPositions(), context->getVelocities(), context->getForces(), temperature,
friction, getStepSize());
context->setTime(context->getTime()+getStepSize());
}
}
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* This is part of the OpenMM molecular simulation 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) 2008 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 "Force.h"
#include "OpenMMException.h"
#include "GBSAOBCForceField.h"
#include "internal/GBSAOBCForceFieldImpl.h"
using namespace OpenMM;
GBSAOBCForceField::GBSAOBCForceField(int numAtoms) : atoms(numAtoms) {
}
void GBSAOBCForceField::getAtomParameters(int index, double& charge, double& radius, double& scalingFactor) const {
charge = atoms[index].charge;
radius = atoms[index].radius;
scalingFactor = atoms[index].scalingFactor;
}
void GBSAOBCForceField::setAtomParameters(int index, double charge, double radius, double scalingFactor) {
atoms[index].charge = charge;
atoms[index].radius = radius;
atoms[index].scalingFactor = scalingFactor;
}
ForceImpl* GBSAOBCForceField::createImpl(OpenMMContextImpl& context) {
return new GBSAOBCForceFieldImpl(*this, context);
}
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* This is part of the OpenMM molecular simulation 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) 2008 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 "internal/GBSAOBCForceFieldImpl.h"
#include "internal/OpenMMContextImpl.h"
#include "kernels.h"
#include <vector>
using namespace OpenMM;
using std::vector;
GBSAOBCForceFieldImpl::GBSAOBCForceFieldImpl(GBSAOBCForceField& owner, OpenMMContextImpl& context) : owner(owner), hasInitialized(false) {
}
void GBSAOBCForceFieldImpl::initialize(OpenMMContextImpl& context) {
hasInitialized = true;
forceKernel = context.getPlatform().createKernel(CalcStandardMMForcesKernel::Name());
energyKernel = context.getPlatform().createKernel(CalcStandardMMEnergyKernel::Name());
std::vector<double> bornRadii;
// TODO calculate the initial Born radii.
vector<vector<double> > atomParameters;
for (int i = 0; i < owner.getNumAtoms(); ++i) {
double charge, radius, scalingFactor;
owner.getAtomParameters(i, charge, radius, scalingFactor);
atomParameters[i].push_back(charge);
atomParameters[i].push_back(radius);
atomParameters[i].push_back(scalingFactor);
}
dynamic_cast<CalcGBSAOBCForcesKernel&>(forceKernel.getImpl()).initialize(bornRadii, atomParameters,
owner.getSolventDielectric(), owner.getSoluteDielectric());
dynamic_cast<CalcGBSAOBCEnergyKernel&>(forceKernel.getImpl()).initialize(bornRadii, atomParameters,
owner.getSolventDielectric(), owner.getSoluteDielectric());
}
void GBSAOBCForceFieldImpl::calcForces(OpenMMContextImpl& context, Stream& forces) {
if (!hasInitialized)
initialize(context);
dynamic_cast<CalcGBSAOBCForcesKernel&>(forceKernel.getImpl()).execute(context.getPositions(), forces);
}
double GBSAOBCForceFieldImpl::calcEnergy(OpenMMContextImpl& context) {
if (!hasInitialized)
initialize(context);
return dynamic_cast<CalcGBSAOBCEnergyKernel&>(energyKernel.getImpl()).execute(context.getPositions());
}
std::vector<std::string> GBSAOBCForceFieldImpl::getKernelNames() {
std::vector<std::string> names;
names.push_back(CalcGBSAOBCForcesKernel::Name());
names.push_back(CalcGBSAOBCEnergyKernel::Name());
return names;
}
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* This is part of the OpenMM molecular simulation 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) 2008 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 "LangevinIntegrator.h"
#include "OpenMMContext.h"
#include "internal/OpenMMContextImpl.h"
#include "kernels.h"
#include <string>
using namespace OpenMM;
using std::string;
using std::vector;
LangevinIntegrator::LangevinIntegrator(double temperature, double frictionCoeff, double stepSize) {
setTemperature(temperature);
setFriction(frictionCoeff);
setStepSize(stepSize);
}
void LangevinIntegrator::initialize(OpenMMContextImpl& contextRef) {
context = &contextRef;
kernel = context->getPlatform().createKernel(IntegrateLangevinStepKernel::Name());
const System& system = context->getSystem();
vector<double> masses(system.getNumAtoms());
vector<std::vector<int> > constraintIndices(system.getNumConstraints());
vector<double> constraintLengths(system.getNumConstraints());
for (int i = 0; i < system.getNumAtoms(); ++i)
masses[i] = system.getAtomMass(i);
for (int i = 0; i < system.getNumConstraints(); ++i) {
int atom1, atom2;
double distance;
system.getConstraintParameters(i, atom1, atom2, distance);
constraintIndices[i].push_back(atom1);
constraintIndices[i].push_back(atom2);
constraintLengths[i] = distance;
}
dynamic_cast<IntegrateLangevinStepKernel&>(kernel.getImpl()).initialize(masses, constraintIndices, constraintLengths);
}
vector<string> LangevinIntegrator::getKernelNames() {
std::vector<std::string> names;
names.push_back(IntegrateLangevinStepKernel::Name());
return names;
}
void LangevinIntegrator::step(int steps) {
for (int i = 0; i < steps; ++i) {
context->updateContextState();
context->calcForces();
dynamic_cast<IntegrateLangevinStepKernel&>(kernel.getImpl()).execute(context->getPositions(), context->getVelocities(), context->getForces(), temperature,
friction, getStepSize());
context->setTime(context->getTime()+getStepSize());
}
}
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* This is part of the OpenMM molecular simulation 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) 2008 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 "OpenMMContext.h"
#include "internal/OpenMMContextImpl.h"
#include "OpenMMException.h"
using namespace OpenMM;
using namespace std;
OpenMMContext::OpenMMContext(System& system, Integrator& integrator) : impl(new OpenMMContextImpl(*this, system, integrator)) {
}
const System& OpenMMContext::getSystem() const {
return impl->getSystem();
}
System& OpenMMContext::getSystem() {
return impl->getSystem();
}
const Integrator& OpenMMContext::getIntegrator() const {
return impl->getIntegrator();
}
Integrator& OpenMMContext::getIntegrator() {
return impl->getIntegrator();
}
State OpenMMContext::getState(State::DataType types) const {
State state(impl->getTime(), impl->getSystem().getNumAtoms(), types);
if (types&State::Energy)
state.setEnergy(impl->calcKineticEnergy(), impl->calcPotentialEnergy());
if (types&State::Forces)
impl->getForces().saveToArray(&state.updForces()[0]);
if (types&State::Parameters) {
for (map<string, double>::const_iterator iter = impl->parameters.begin(); iter != impl->parameters.end(); iter++)
state.updParameters()[iter->first] = iter->second;
}
if (types&State::Positions)
impl->getPositions().saveToArray(&state.updPositions()[0]);
if (types&State::Velocities)
impl->getVelocities().saveToArray(&state.updVelocities()[0]);
return state;
}
double OpenMMContext::getTime() {
return impl->getTime();
}
void OpenMMContext::setTime(double time) {
impl->setTime(time);
}
void OpenMMContext::setPositions(const vector<Vec3>& positions) {
if ((int) positions.size() != impl->getSystem().getNumAtoms())
throw OpenMMException("Called setPositions() on an OpenMMContext with the wrong number of positions");
impl->getPositions().loadFromArray(&positions[0]);
}
void OpenMMContext::setVelocities(const vector<Vec3>& velocities) {
if ((int) velocities.size() != impl->getSystem().getNumAtoms())
throw OpenMMException("Called setVelocities() on an OpenMMContext with the wrong number of velocities");
impl->getVelocities().loadFromArray(&velocities[0]);
}
double OpenMMContext::getParameter(string name) {
return impl->getParameter(name);
}
void OpenMMContext::setParameter(string name, double value) {
impl->setParameter(name, value);
}
void OpenMMContext::reinitialize() {
impl->reinitialize();
}
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* This is part of the OpenMM molecular simulation 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) 2008 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 "Force.h"
#include "Integrator.h"
#include "OpenMMException.h"
#include "System.h"
#include "kernels.h"
#include "internal/ForceImpl.h"
#include "internal/OpenMMContextImpl.h"
#include <vector>
using namespace OpenMM;
using std::vector;
using std::string;
OpenMMContextImpl::OpenMMContextImpl(OpenMMContext& owner, System& system, Integrator& integrator) : owner(owner), system(system), integrator(integrator) {
vector<string> kernelNames;
kernelNames.push_back(CalcKineticEnergyKernel::Name());
for (int i = 0; i < system.getNumForces(); ++i) {
forceImpls.push_back(system.getForce(i).createImpl(*this));
vector<string> forceKernels = forceImpls[forceImpls.size()-1]->getKernelNames();
kernelNames.insert(kernelNames.begin(), forceKernels.begin(), forceKernels.end());
}
vector<string> integratorKernels = integrator.getKernelNames();
kernelNames.insert(kernelNames.begin(), integratorKernels.begin(), integratorKernels.end());
platform = &Platform::findPlatform(kernelNames);
positions = platform->createStream("atomPositions", system.getNumAtoms(), Stream::Double3);
velocities = platform->createStream("atomVelocities", system.getNumAtoms(), Stream::Double3);
forces = platform->createStream("atomForces", system.getNumAtoms(), Stream::Double3);
kineticEnergyKernel = platform->createKernel(CalcKineticEnergyKernel::Name());
integrator.initialize(*this);
}
OpenMMContextImpl::~OpenMMContextImpl() {
for (int i = 0; i < (int) forceImpls.size(); ++i)
delete forceImpls[i];
}
double OpenMMContextImpl::getParameter(std::string name) {
if (parameters.find(name) == parameters.end())
throw OpenMMException("Called getParameter() with invalid parameter name");
return parameters[name];
}
void OpenMMContextImpl::setParameter(std::string name, double value) {
if (parameters.find(name) == parameters.end())
throw OpenMMException("Called setParameter() with invalid parameter name");
parameters[name] = value;
}
void OpenMMContextImpl::calcForces() {
double zero[] = {0.0, 0.0, 0.0};
forces.fillWithValue(zero);
for (int i = 0; i < (int) forceImpls.size(); ++i)
forceImpls[i]->calcForces(*this, forces);
}
double OpenMMContextImpl::calcKineticEnergy() {
return dynamic_cast<CalcKineticEnergyKernel&>(kineticEnergyKernel.getImpl()).execute(velocities);
}
double OpenMMContextImpl::calcPotentialEnergy() {
double energy = 0.0;
for (int i = 0; i < (int) forceImpls.size(); ++i)
energy += forceImpls[i]->calcEnergy(*this);
return energy;
}
void OpenMMContextImpl::updateContextState() {
for (int i = 0; i < (int) forceImpls.size(); ++i)
forceImpls[i]->updateContextState(*this);
}
void OpenMMContextImpl::reinitialize() {
for (int i = 0; i < (int) forceImpls.size(); ++i)
delete forceImpls[i];
forceImpls.resize(0);
for (int i = 0; i < system.getNumForces(); ++i)
forceImpls.push_back(system.getForce(i).createImpl(*this));
integrator.initialize(*this);
}
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* This is part of the OpenMM molecular simulation 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) 2008 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 "Force.h"
#include "OpenMMException.h"
#include "StandardMMForceField.h"
#include "internal/StandardMMForceFieldImpl.h"
using namespace OpenMM;
StandardMMForceField::StandardMMForceField(int numAtoms, int numBonds, int numAngles, int numPeriodicTorsions, int numRBTorsions) :
atoms(numAtoms), bonds(numBonds), angles(numAngles), periodicTorsions(numPeriodicTorsions), rbTorsions(numRBTorsions) {
}
void StandardMMForceField::getAtomParameters(int index, double& charge, double& radius, double& depth) const {
charge = atoms[index].charge;
radius = atoms[index].radius;
depth = atoms[index].depth;
}
void StandardMMForceField::setAtomParameters(int index, double charge, double radius, double depth) {
atoms[index].charge = charge;
atoms[index].radius = radius;
atoms[index].depth = depth;
}
void StandardMMForceField::getBondParameters(int index, int& atom1, int& atom2, double& length, double& k) const {
atom1 = bonds[index].atom1;
atom2 = bonds[index].atom2;
length = bonds[index].length;
k = bonds[index].k;
}
void StandardMMForceField::setBondParameters(int index, int atom1, int atom2, double length, double k) {
bonds[index].atom1 = atom1;
bonds[index].atom2 = atom2;
bonds[index].length = length;
bonds[index].k = k;
}
void StandardMMForceField::getAngleParameters(int index, int& atom1, int& atom2, int& atom3, double& angle, double& k) const {
atom1 = angles[index].atom1;
atom2 = angles[index].atom2;
atom3 = angles[index].atom3;
angle = angles[index].angle;
k = angles[index].k;
}
void StandardMMForceField::setAngleParameters(int index, int atom1, int atom2, int atom3, double angle, double k) {
angles[index].atom1 = atom1;
angles[index].atom2 = atom2;
angles[index].atom3 = atom3;
angles[index].angle = angle;
angles[index].k = k;
}
void StandardMMForceField::getPeriodicTorsionParameters(int index, int& atom1, int& atom2, int& atom3, int& atom4, int& periodicity, double& phase, double& k) const {
atom1 = periodicTorsions[index].atom1;
atom2 = periodicTorsions[index].atom2;
atom3 = periodicTorsions[index].atom3;
atom4 = periodicTorsions[index].atom4;
periodicity = periodicTorsions[index].periodicity;
phase = periodicTorsions[index].phase;
k = periodicTorsions[index].k;
}
void StandardMMForceField::setPeriodicTorsionParameters(int index, int atom1, int atom2, int atom3, int atom4, int periodicity, double phase, double k) {
periodicTorsions[index].atom1 = atom1;
periodicTorsions[index].atom2 = atom2;
periodicTorsions[index].atom3 = atom3;
periodicTorsions[index].atom4 = atom4;
periodicTorsions[index].periodicity = periodicity;
periodicTorsions[index].phase = phase;
periodicTorsions[index].k = k;
}
void StandardMMForceField::getRBTorsionParameters(int index, int& atom1, int& atom2, int& atom3, int& atom4, double& c0, double& c1, double& c2, double& c3, double& c4, double& c5) const {
atom1 = rbTorsions[index].atom1;
atom2 = rbTorsions[index].atom2;
atom3 = rbTorsions[index].atom3;
atom4 = rbTorsions[index].atom4;
c0 = rbTorsions[index].c[0];
c1 = rbTorsions[index].c[1];
c2 = rbTorsions[index].c[2];
c3 = rbTorsions[index].c[3];
c4 = rbTorsions[index].c[4];
c5 = rbTorsions[index].c[5];
}
void StandardMMForceField::setRBTorsionParameters(int index, int atom1, int atom2, int atom3, int atom4, double c0, double c1, double c2, double c3, double c4, double c5) {
rbTorsions[index].atom1 = atom1;
rbTorsions[index].atom2 = atom2;
rbTorsions[index].atom3 = atom3;
rbTorsions[index].atom4 = atom4;
rbTorsions[index].c[0] = c0;
rbTorsions[index].c[1] = c1;
rbTorsions[index].c[2] = c2;
rbTorsions[index].c[3] = c3;
rbTorsions[index].c[4] = c4;
rbTorsions[index].c[5] = c5;
}
ForceImpl* StandardMMForceField::createImpl(OpenMMContextImpl& context) {
return new StandardMMForceFieldImpl(*this, context);
}
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* This is part of the OpenMM molecular simulation 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) 2008 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 "internal/OpenMMContextImpl.h"
#include "internal/StandardMMForceFieldImpl.h"
#include "kernels.h"
using namespace OpenMM;
using std::pair;
using std::vector;
using std::set;
StandardMMForceFieldImpl::StandardMMForceFieldImpl(StandardMMForceField& owner, OpenMMContextImpl& context) : owner(owner) {
forceKernel = context.getPlatform().createKernel(CalcStandardMMForcesKernel::Name());
energyKernel = context.getPlatform().createKernel(CalcStandardMMEnergyKernel::Name());
vector<vector<int> > bondIndices;
vector<vector<double> > bondParameters;
vector<vector<int> > angleIndices;
vector<vector<double> > angleParameters;
vector<vector<int> > periodicTorsionIndices;
vector<vector<double> > periodicTorsionParameters;
vector<vector<int> > rbTorsionIndices;
vector<vector<double> > rbTorsionParameters;
vector<vector<int> > bonded14Indices;
vector<set<int> > exclusions(owner.getNumAtoms());
vector<vector<double> > nonbondedParameters(owner.getNumAtoms());
for (int i = 0; i < owner.getNumBonds(); ++i) {
int atom1, atom2;
double length, k;
owner.getBondParameters(i, atom1, atom2, length, k);
bondIndices[i].push_back(atom1);
bondIndices[i].push_back(atom2);
bondParameters[i].push_back(length);
bondParameters[i].push_back(k);
}
for (int i = 0; i < owner.getNumAngles(); ++i) {
int atom1, atom2, atom3;
double angle, k;
owner.getAngleParameters(i, atom1, atom2, atom3, angle, k);
angleIndices[i].push_back(atom1);
angleIndices[i].push_back(atom2);
angleIndices[i].push_back(atom3);
angleParameters[i].push_back(angle);
angleParameters[i].push_back(k);
}
for (int i = 0; i < owner.getNumPeriodicTorsions(); ++i) {
int atom1, atom2, atom3, atom4, periodicity;
double phase, k;
owner.getPeriodicTorsionParameters(i, atom1, atom2, atom3, atom4, periodicity, phase, k);
periodicTorsionIndices[i].push_back(atom1);
periodicTorsionIndices[i].push_back(atom2);
periodicTorsionIndices[i].push_back(atom3);
periodicTorsionIndices[i].push_back(atom4);
periodicTorsionParameters[i].push_back(periodicity);
periodicTorsionParameters[i].push_back(phase);
periodicTorsionParameters[i].push_back(k);
}
for (int i = 0; i < owner.getNumRBTorsions(); ++i) {
int atom1, atom2, atom3, atom4;
double c0, c1, c2, c3, c4, c5;
owner.getRBTorsionParameters(i, atom1, atom2, atom3, atom4, c0, c1, c2, c3, c4, c5);
rbTorsionIndices[i].push_back(atom1);
rbTorsionIndices[i].push_back(atom2);
rbTorsionIndices[i].push_back(atom3);
rbTorsionIndices[i].push_back(atom4);
rbTorsionParameters[i].push_back(c0);
rbTorsionParameters[i].push_back(c1);
rbTorsionParameters[i].push_back(c2);
rbTorsionParameters[i].push_back(c3);
rbTorsionParameters[i].push_back(c4);
rbTorsionParameters[i].push_back(c5);
}
for (int i = 0; i < owner.getNumAtoms(); ++i) {
double charge, radius, depth;
owner.getAtomParameters(i, charge, radius, depth);
nonbondedParameters[i].push_back(charge);
nonbondedParameters[i].push_back(radius);
nonbondedParameters[i].push_back(depth);
}
set<pair<int, int> > bonded14set;
findExclusions(bondIndices, exclusions, bonded14set);
dynamic_cast<CalcStandardMMForcesKernel&>(forceKernel.getImpl()).initialize(bondIndices, bondParameters, angleIndices, angleParameters,
periodicTorsionIndices, periodicTorsionParameters, rbTorsionIndices, rbTorsionParameters, bonded14Indices, exclusions, nonbondedParameters);
dynamic_cast<CalcStandardMMEnergyKernel&>(energyKernel.getImpl()).initialize(bondIndices, bondParameters, angleIndices, angleParameters,
periodicTorsionIndices, periodicTorsionParameters, rbTorsionIndices, rbTorsionParameters, bonded14Indices, exclusions, nonbondedParameters);
}
StandardMMForceFieldImpl::~StandardMMForceFieldImpl() {
}
void StandardMMForceFieldImpl::calcForces(OpenMMContextImpl& context, Stream& forces) {
dynamic_cast<CalcStandardMMForcesKernel&>(forceKernel.getImpl()).execute(context.getPositions(), forces);
}
double StandardMMForceFieldImpl::calcEnergy(OpenMMContextImpl& context) {
return dynamic_cast<CalcStandardMMEnergyKernel&>(energyKernel.getImpl()).execute(context.getPositions());
}
std::vector<std::string> StandardMMForceFieldImpl::getKernelNames() {
std::vector<std::string> names;
names.push_back(CalcStandardMMForcesKernel::Name());
names.push_back(CalcStandardMMEnergyKernel::Name());
return names;
}
void StandardMMForceFieldImpl::findExclusions(const vector<vector<int> >& bondIndices, vector<set<int> >& exclusions, set<pair<int, int> >& bonded14Indices) const {
vector<set<int> > bonded12(exclusions.size());
for (int i = 0; i < (int) bondIndices.size(); ++i) {
bonded12[bondIndices[i][0]].insert(bondIndices[i][1]);
bonded12[bondIndices[i][1]].insert(bondIndices[i][2]);
}
for (int i = 0; i < (int) exclusions.size(); ++i)
addExclusionsToSet(bonded12, exclusions[i], i, 2);
for (int i = 0; i < (int) exclusions.size(); ++i) {
set<int> bonded13;
addExclusionsToSet(bonded12, bonded13, i, 1);
for (set<int>::const_iterator iter = exclusions[i].begin(); iter != exclusions[i].end(); ++iter)
if (*iter < i && bonded13.find(*iter) == bonded13.end())
bonded14Indices.insert(pair<int, int> (*iter, i));
}
}
void StandardMMForceFieldImpl::addExclusionsToSet(const vector<set<int> >& bonded12, set<int>& exclusions, int fromAtom, int currentLevel) const {
for (set<int>::const_iterator iter = bonded12[fromAtom].begin(); iter != bonded12[fromAtom].end(); ++iter) {
exclusions.insert(*iter);
if (currentLevel > 0)
addExclusionsToSet(bonded12, exclusions, *iter, currentLevel-1);
}
}
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* This is part of the OpenMM molecular simulation 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) 2008 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 "OpenMMException.h"
#include "State.h"
using namespace OpenMM;
using namespace std;
double State::getTime() const {
return time;
}
const vector<Vec3>& State::getPositions() const {
if (types&Positions == 0)
throw OpenMMException("Invoked getPositions() on a State which does not contain positions.");
return positions;
}
const vector<Vec3>& State::getVelocities() const {
if (types&Velocities == 0)
throw OpenMMException("Invoked getVelocities() on a State which does not contain velocities.");
return velocities;
}
const vector<Vec3>& State::getForces() const {
if (types&Forces== 0)
throw OpenMMException("Invoked getForces() on a State which does not contain forces.");
return forces;
}
double State::getKineticEnergy() const {
if (types&Energy== 0)
throw OpenMMException("Invoked getKineticEnergy() on a State which does not contain energies.");
return ke;
}
double State::getPotentialEnergy() const {
if (types&Energy== 0)
throw OpenMMException("Invoked getPotentialEnergy() on a State which does not contain energies.");
return pe;
}
const map<string, double>& State::getParameters() const {
if (types&Parameters== 0)
throw OpenMMException("Invoked getParameters() on a State which does not contain parameters.");
return parameters;
}
State::State(double time, int numAtoms, DataType types) : types(types), time(time), ke(0), pe(0),
positions(types&Positions == 0 ? 0 : numAtoms), velocities(types&Velocities== 0 ? 0 : numAtoms),
forces(types&Forces== 0 ? 0 : numAtoms) {
}
vector<Vec3>& State::updPositions() {
return positions;
}
vector<Vec3>& State::updVelocities() {
return velocities;
}
vector<Vec3>& State::updForces() {
return forces;
}
map<string, double>& State::updParameters() {
return parameters;
}
void State::setEnergy(double kinetic, double potential) {
ke = kinetic;
pe = potential;
}
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* This is part of the OpenMM molecular simulation 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) 2008 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 "Force.h"
#include "System.h"
using namespace OpenMM;
System::System(int numAtoms, int numConstraints) : masses(numAtoms), constraints(numConstraints), forces(0) {
for (int i = 0; i < numAtoms; ++i)
masses[i] = 0.0;
}
System::~System() {
for (int i = 0; i < (int) forces.size(); ++i)
delete forces[i];
}
void System::getConstraintParameters(int index, int& atom1, int& atom2, double& distance) const {
atom1 = constraints[index].atom1;
atom2 = constraints[index].atom2;
distance = constraints[index].distance;
}
void System::setConstraintParameters(int index, int atom1, int atom2, double distance) {
constraints[index].atom1 = atom1;
constraints[index].atom2 = atom2;
constraints[index].distance = distance;
}
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* This is part of the OpenMM molecular simulation 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) 2008 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 "VerletIntegrator.h"
#include "OpenMMContext.h"
#include "internal/OpenMMContextImpl.h"
#include "kernels.h"
#include <string>
using namespace OpenMM;
using std::string;
using std::vector;
VerletIntegrator::VerletIntegrator(double stepSize) {
setStepSize(stepSize);
}
void VerletIntegrator::initialize(OpenMMContextImpl& contextRef) {
context = &contextRef;
kernel = context->getPlatform().createKernel(IntegrateVerletStepKernel::Name());
const System& system = context->getSystem();
vector<double> masses(system.getNumAtoms());
vector<std::vector<int> > constraintIndices(system.getNumConstraints());
vector<double> constraintLengths(system.getNumConstraints());
for (int i = 0; i < system.getNumAtoms(); ++i)
masses[i] = system.getAtomMass(i);
for (int i = 0; i < system.getNumConstraints(); ++i) {
int atom1, atom2;
double distance;
system.getConstraintParameters(i, atom1, atom2, distance);
constraintIndices[i].push_back(atom1);
constraintIndices[i].push_back(atom2);
constraintLengths[i] = distance;
}
dynamic_cast<IntegrateVerletStepKernel&>(kernel.getImpl()).initialize(masses, constraintIndices, constraintLengths);
}
vector<string> VerletIntegrator::getKernelNames() {
std::vector<std::string> names;
names.push_back(IntegrateVerletStepKernel::Name());
return names;
}
void VerletIntegrator::step(int steps) {
for (int i = 0; i < steps; ++i) {
context->updateContextState();
context->calcForces();
dynamic_cast<IntegrateVerletStepKernel&>(kernel.getImpl()).execute(context->getPositions(), context->getVelocities(), context->getForces(), getStepSize());
context->setTime(context->getTime()+getStepSize());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment