/* -------------------------------------------------------------------------- * * 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 > bondIndices; vector > bondParameters; vector > angleIndices; vector > angleParameters; vector > periodicTorsionIndices; vector > periodicTorsionParameters; vector > rbTorsionIndices; vector > rbTorsionParameters; vector > bonded14Indices; vector > exclusions(owner.getNumAtoms()); vector > 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 > bonded14set; findExclusions(bondIndices, exclusions, bonded14set); dynamic_cast(forceKernel.getImpl()).initialize(bondIndices, bondParameters, angleIndices, angleParameters, periodicTorsionIndices, periodicTorsionParameters, rbTorsionIndices, rbTorsionParameters, bonded14Indices, exclusions, nonbondedParameters); dynamic_cast(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(forceKernel.getImpl()).execute(context.getPositions(), forces); } double StandardMMForceFieldImpl::calcEnergy(OpenMMContextImpl& context) { return dynamic_cast(energyKernel.getImpl()).execute(context.getPositions()); } std::vector StandardMMForceFieldImpl::getKernelNames() { std::vector names; names.push_back(CalcStandardMMForcesKernel::Name()); names.push_back(CalcStandardMMEnergyKernel::Name()); return names; } void StandardMMForceFieldImpl::findExclusions(const vector >& bondIndices, vector >& exclusions, set >& bonded14Indices) const { vector > 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 bonded13; addExclusionsToSet(bonded12, bonded13, i, 1); for (set::const_iterator iter = exclusions[i].begin(); iter != exclusions[i].end(); ++iter) if (*iter < i && bonded13.find(*iter) == bonded13.end()) bonded14Indices.insert(pair (*iter, i)); } } void StandardMMForceFieldImpl::addExclusionsToSet(const vector >& bonded12, set& exclusions, int fromAtom, int currentLevel) const { for (set::const_iterator iter = bonded12[fromAtom].begin(); iter != bonded12[fromAtom].end(); ++iter) { exclusions.insert(*iter); if (currentLevel > 0) addExclusionsToSet(bonded12, exclusions, *iter, currentLevel-1); } }