#ifndef OPENMM_CPUKERNELS_H_ #define OPENMM_CPUKERNELS_H_ /* -------------------------------------------------------------------------- * * OpenMM * * -------------------------------------------------------------------------- * * This is part of the OpenMM molecular simulation toolkit. * * See https://openmm.org/development. * * * * Portions copyright (c) 2013-2025 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 "CpuBondForce.h" #include "CpuConstantPotentialForce.h" #include "CpuCustomGBForce.h" #include "CpuCustomManyParticleForce.h" #include "CpuCustomNonbondedForce.h" #include "CpuGayBerneForce.h" #include "CpuGBSAOBCForce.h" #include "CpuLangevinMiddleDynamics.h" #include "CpuLCPOForce.h" #include "CpuNeighborList.h" #include "CpuNonbondedForce.h" #include "CpuPlatform.h" #include "ReferenceKernels.h" #include "openmm/kernels.h" #include "openmm/System.h" #include "openmm/internal/CustomNonbondedForceImpl.h" #include #include namespace OpenMM { /** * This kernel is invoked at the beginning and end of force and energy computations. It gives the * Platform a chance to clear buffers and do other initialization at the beginning, and to do any * necessary work at the end to determine the final results. */ class CpuCalcForcesAndEnergyKernel : public CalcForcesAndEnergyKernel { public: CpuCalcForcesAndEnergyKernel(std::string name, const Platform& platform, CpuPlatform::PlatformData& data, ContextImpl& context); /** * Initialize the kernel. * * @param system the System this kernel will be applied to */ void initialize(const System& system); /** * This is called at the beginning of each force/energy computation, before calcForcesAndEnergy() has been called on * any ForceImpl. * * @param context the context in which to execute this kernel * @param includeForce true if forces should be computed * @param includeEnergy true if potential energy should be computed * @param groups a set of bit flags for which force groups to include */ void beginComputation(ContextImpl& context, bool includeForce, bool includeEnergy, int groups); /** * This is called at the end of each force/energy computation, after calcForcesAndEnergy() has been called on * every ForceImpl. * * @param context the context in which to execute this kernel * @param includeForce true if forces should be computed * @param includeEnergy true if potential energy should be computed * @param groups a set of bit flags for which force groups to include * @param valid the method may set this to false to indicate the results are invalid and the force/energy * calculation should be repeated * @return the potential energy of the system. This value is added to all values returned by ForceImpls' * calcForcesAndEnergy() methods. That is, each force kernel may either return its contribution to the * energy directly, or add it to an internal buffer so that it will be included here. */ double finishComputation(ContextImpl& context, bool includeForce, bool includeEnergy, int groups, bool& valid); private: CpuPlatform::PlatformData& data; Kernel referenceKernel; std::vector lastPositions; }; /** * This kernel provides methods for setting and retrieving various state data: time, positions, * velocities, and forces. */ class CpuUpdateStateDataKernel : public ReferenceUpdateStateDataKernel { public: CpuUpdateStateDataKernel(std::string name, const Platform& platform, CpuPlatform::PlatformData& data, ReferencePlatform::PlatformData& refdata) : ReferenceUpdateStateDataKernel(name, platform, refdata), data(data) { } /** * Create a checkpoint recording the current state of the Context. * * @param stream an output stream the checkpoint data should be written to */ void createCheckpoint(ContextImpl& context, std::ostream& stream); /** * Load a checkpoint that was written by createCheckpoint(). * * @param stream an input stream the checkpoint data should be read from */ void loadCheckpoint(ContextImpl& context, std::istream& stream); private: CpuPlatform::PlatformData& data; }; /** * This kernel is invoked by HarmonicAngleForce to calculate the forces acting on the system and the energy of the system. */ class CpuCalcHarmonicAngleForceKernel : public CalcHarmonicAngleForceKernel { public: CpuCalcHarmonicAngleForceKernel(std::string name, const Platform& platform, CpuPlatform::PlatformData& data) : CalcHarmonicAngleForceKernel(name, platform), data(data), usePeriodic(false) { } /** * Initialize the kernel. * * @param system the System this kernel will be applied to * @param force the HarmonicAngleForce this kernel will be used for */ void initialize(const System& system, const HarmonicAngleForce& force); /** * Execute the kernel to calculate the forces and/or energy. * * @param context the context in which to execute this kernel * @param includeForces true if forces should be calculated * @param includeEnergy true if the energy should be calculated * @return the potential energy due to the force */ double execute(ContextImpl& context, bool includeForces, bool includeEnergy); /** * Copy changed parameters over to a context. * * @param firstAngle the index of the first bond whose parameters might have changed * @param lastAngle the index of the last bond whose parameters might have changed */ void copyParametersToContext(ContextImpl& context, const HarmonicAngleForce& force, int firstAngle, int lastAngle); private: CpuPlatform::PlatformData& data; int numAngles; std::vector > angleIndexArray; std::vector > angleParamArray; CpuBondForce bondForce; bool usePeriodic; }; /** * This kernel is invoked by PeriodicTorsionForce to calculate the forces acting on the system and the energy of the system. */ class CpuCalcPeriodicTorsionForceKernel : public CalcPeriodicTorsionForceKernel { public: CpuCalcPeriodicTorsionForceKernel(std::string name, const Platform& platform, CpuPlatform::PlatformData& data) : CalcPeriodicTorsionForceKernel(name, platform), data(data), usePeriodic(false) { } /** * Initialize the kernel. * * @param system the System this kernel will be applied to * @param force the PeriodicTorsionForce this kernel will be used for */ void initialize(const System& system, const PeriodicTorsionForce& force); /** * Execute the kernel to calculate the forces and/or energy. * * @param context the context in which to execute this kernel * @param includeForces true if forces should be calculated * @param includeEnergy true if the energy should be calculated * @return the potential energy due to the force */ double execute(ContextImpl& context, bool includeForces, bool includeEnergy); /** * Copy changed parameters over to a context. * * @param context the context to copy parameters to * @param force the PeriodicTorsionForce to copy the parameters from * @param firstTorsion the index of the first torsion whose parameters might have changed * @param lastTorsion the index of the last torsion whose parameters might have changed */ void copyParametersToContext(ContextImpl& context, const PeriodicTorsionForce& force, int firstTorsion, int lastTorsion); private: CpuPlatform::PlatformData& data; int numTorsions; std::vector > torsionIndexArray; std::vector > torsionParamArray; CpuBondForce bondForce; bool usePeriodic; }; /** * This kernel is invoked by RBTorsionForce to calculate the forces acting on the system and the energy of the system. */ class CpuCalcRBTorsionForceKernel : public CalcRBTorsionForceKernel { public: CpuCalcRBTorsionForceKernel(std::string name, const Platform& platform, CpuPlatform::PlatformData& data) : CalcRBTorsionForceKernel(name, platform), data(data), usePeriodic(false) { } /** * Initialize the kernel. * * @param system the System this kernel will be applied to * @param force the RBTorsionForce this kernel will be used for */ void initialize(const System& system, const RBTorsionForce& force); /** * Execute the kernel to calculate the forces and/or energy. * * @param context the context in which to execute this kernel * @param includeForces true if forces should be calculated * @param includeEnergy true if the energy should be calculated * @return the potential energy due to the force */ double execute(ContextImpl& context, bool includeForces, bool includeEnergy); /** * Copy changed parameters over to a context. * * @param context the context to copy parameters to * @param force the RBTorsionForce to copy the parameters from */ void copyParametersToContext(ContextImpl& context, const RBTorsionForce& force); private: CpuPlatform::PlatformData& data; int numTorsions; std::vector > torsionIndexArray; std::vector > torsionParamArray; CpuBondForce bondForce; bool usePeriodic; }; /** * This kernel is invoked by NonbondedForce to calculate the forces acting on the system. */ class CpuCalcNonbondedForceKernel : public CalcNonbondedForceKernel { public: CpuCalcNonbondedForceKernel(std::string name, const Platform& platform, CpuPlatform::PlatformData& data); ~CpuCalcNonbondedForceKernel(); /** * Initialize the kernel. * * @param system the System this kernel will be applied to * @param force the NonbondedForce this kernel will be used for */ void initialize(const System& system, const NonbondedForce& force); /** * Execute the kernel to calculate the forces and/or energy. * * @param context the context in which to execute this kernel * @param includeForces true if forces should be calculated * @param includeEnergy true if the energy should be calculated * @param includeDirect true if direct space interactions should be included * @param includeReciprocal true if reciprocal space interactions should be included * @return the potential energy due to the force */ double execute(ContextImpl& context, bool includeForces, bool includeEnergy, bool includeDirect, bool includeReciprocal); /** * Copy changed parameters over to a context. * * @param context the context to copy parameters to * @param force the NonbondedForce to copy the parameters from * @param firstParticle the index of the first particle whose parameters might have changed * @param lastParticle the index of the last particle whose parameters might have changed * @param firstException the index of the first exception whose parameters might have changed * @param lastException the index of the last exception whose parameters might have changed */ void copyParametersToContext(ContextImpl& context, const NonbondedForce& force, int firstParticle, int lastParticle, int firstException, int lastException); /** * Get the parameters being used for PME. * * @param alpha the separation parameter * @param nx the number of grid points along the X axis * @param ny the number of grid points along the Y axis * @param nz the number of grid points along the Z axis */ void getPMEParameters(double& alpha, int& nx, int& ny, int& nz) const; /** * Get the parameters being used for the dispersion term in LJPME. * * @param alpha the separation parameter * @param nx the number of grid points along the X axis * @param ny the number of grid points along the Y axis * @param nz the number of grid points along the Z axis */ void getLJPMEParameters(double& alpha, int& nx, int& ny, int& nz) const; private: class PmeIO; void computeParameters(ContextImpl& context, bool offsetsOnly); CpuPlatform::PlatformData& data; int numParticles, num14, chargePosqIndex, ljPosqIndex; std::vector > bonded14IndexArray; std::vector > bonded14ParamArray; std::map nb14Index; double nonbondedCutoff, switchingDistance, rfDielectric, ewaldAlpha, ewaldDispersionAlpha, ewaldSelfEnergy, dispersionCoefficient, totalCharge; int kmax[3], gridSize[3], dispersionGridSize[3]; bool useSwitchingFunction, exceptionsArePeriodic, useOptimizedPme, hasInitializedPme, hasInitializedDispersionPme, hasParticleOffsets, hasExceptionOffsets; std::vector > exclusions; std::vector > particleParams; std::vector C6params; std::vector charges; std::vector > baseParticleParams, baseExceptionParams; std::vector > > particleParamOffsets, exceptionParamOffsets; std::vector paramNames; std::vector paramValues; NonbondedMethod nonbondedMethod; CpuNonbondedForce* nonbonded; Kernel optimizedPme, optimizedDispersionPme; CpuBondForce bondForce; }; /** * This kernel is invoked by ConstantPotentialForce to calculate the forces acting on the system. */ class CpuCalcConstantPotentialForceKernel : public CalcConstantPotentialForceKernel { public: CpuCalcConstantPotentialForceKernel(std::string name, const Platform& platform, CpuPlatform::PlatformData& data); ~CpuCalcConstantPotentialForceKernel(); /** * Initialize the kernel. * * @param system the System this kernel will be applied to * @param force the ConstantPotentialForce this kernel will be used for */ void initialize(const System& system, const ConstantPotentialForce& force); /** * Execute the kernel to calculate the forces and/or energy. * * @param context the context in which to execute this kernel * @param includeForces true if forces should be calculated * @param includeEnergy true if the energy should be calculated * @return the potential energy due to the force */ double execute(ContextImpl& context, bool includeForces, bool includeEnergy); /** * Copy changed parameters over to a context. * * @param context the context to copy parameters to * @param force the ConstantPotentialForce to copy the parameters from * @param firstParticle the index of the first particle whose parameters might have changed * @param lastParticle the index of the last particle whose parameters might have changed * @param firstException the index of the first exception whose parameters might have changed * @param lastException the index of the last exception whose parameters might have changed * @param firstElectrode the index of the first electrode whose parameters might have changed * @param lastElectrode the index of the last electrode whose parameters might have changed */ void copyParametersToContext(ContextImpl& context, const ConstantPotentialForce& force, int firstParticle, int lastParticle, int firstException, int lastException, int firstElectrode, int lastElectrode); /** * Get the parameters being used for PME. * * @param alpha the separation parameter * @param nx the number of grid points along the X axis * @param ny the number of grid points along the Y axis * @param nz the number of grid points along the Z axis */ void getPMEParameters(double& alpha, int& nx, int& ny, int& nz) const; /** * Get the charges on all particles. * * @param context the context to copy parameters to * @param[out] charges a vector to populate with particle charges */ void getCharges(ContextImpl& context, std::vector& charges); private: void checkBoxSize(const Vec3* boxVectors); void ensurePmeInitialized(ContextImpl& context); private: CpuPlatform::PlatformData& data; int numParticles, num14, numElectrodeParticles, chargePosqIndex; std::vector setCharges; std::vector charges; std::vector > bonded14ParamArray; std::vector > bonded14IndexArray; std::map nb14Index; std::vector > exclusions; std::vector sysToElec, elecToSys, sysElec, elecElec; std::vector > electrodeParams; double nonbondedCutoff, ewaldAlpha, cgErrorTol, chargeTarget; int gridSize[3]; bool exceptionsArePeriodic, hasInitializedPme, useChargeConstraint; Vec3 externalField; CpuConstantPotentialForce* constantPotential; CpuConstantPotentialSolver* solver; CpuBondForce bondForce; Kernel pmeKernel; }; /** * This kernel is invoked by CustomNonbondedForce to calculate the forces acting on the system. */ class CpuCalcCustomNonbondedForceKernel : public CalcCustomNonbondedForceKernel { public: CpuCalcCustomNonbondedForceKernel(std::string name, const Platform& platform, CpuPlatform::PlatformData& data); ~CpuCalcCustomNonbondedForceKernel(); /** * Initialize the kernel. * * @param system the System this kernel will be applied to * @param force the CustomNonbondedForce this kernel will be used for */ void initialize(const System& system, const CustomNonbondedForce& force); /** * Execute the kernel to calculate the forces and/or energy. * * @param context the context in which to execute this kernel * @param includeForces true if forces should be calculated * @param includeEnergy true if the energy should be calculated * @return the potential energy due to the force */ double execute(ContextImpl& context, bool includeForces, bool includeEnergy); /** * Copy changed parameters over to a context. * * @param context the context to copy parameters to * @param force the CustomNonbondedForce to copy the parameters from * @param firstParticle the index of the first particle whose parameters might have changed * @param lastParticle the index of the last particle whose parameters might have changed */ void copyParametersToContext(ContextImpl& context, const CustomNonbondedForce& force, int firstParticle, int lastParticle); private: void createInteraction(const CustomNonbondedForce& force); CpuPlatform::PlatformData& data; int numParticles; std::vector > particleParamArray; double nonbondedCutoff, switchingDistance, periodicBoxSize[3], longRangeCoefficient; bool useSwitchingFunction, hasInitializedLongRangeCorrection; CustomNonbondedForce* forceCopy; CustomNonbondedForceImpl::LongRangeCorrectionData longRangeCorrectionData; std::map globalParamValues; std::vector > exclusions; std::vector parameterNames, globalParameterNames, computedValueNames, energyParamDerivNames; std::vector, std::set > > interactionGroups; std::vector longRangeCoefficientDerivs; std::map tabulatedFunctionUpdateCount; NonbondedMethod nonbondedMethod; CpuCustomNonbondedForce* nonbonded; }; /** * This kernel is invoked by GBSAOBCForce to calculate the forces acting on the system. */ class CpuCalcGBSAOBCForceKernel : public CalcGBSAOBCForceKernel { public: CpuCalcGBSAOBCForceKernel(std::string name, const Platform& platform, CpuPlatform::PlatformData& data) : CalcGBSAOBCForceKernel(name, platform), data(data) { } ~CpuCalcGBSAOBCForceKernel(); /** * Initialize the kernel. * * @param system the System this kernel will be applied to * @param force the GBSAOBCForce this kernel will be used for */ void initialize(const System& system, const GBSAOBCForce& force); /** * Execute the kernel to calculate the forces and/or energy. * * @param context the context in which to execute this kernel * @param includeForces true if forces should be calculated * @param includeEnergy true if the energy should be calculated * @return the potential energy due to the force */ double execute(ContextImpl& context, bool includeForces, bool includeEnergy); /** * Copy changed parameters over to a context. * * @param context the context to copy parameters to * @param force the GBSAOBCForce to copy the parameters from */ void copyParametersToContext(ContextImpl& context, const GBSAOBCForce& force); private: CpuPlatform::PlatformData& data; int posqIndex; std::vector > particleParams; std::vector charges; CpuGBSAOBCForce obc; }; /** * This kernel is invoked by CustomGBForce to calculate the forces acting on the system. */ class CpuCalcCustomGBForceKernel : public CalcCustomGBForceKernel { public: CpuCalcCustomGBForceKernel(std::string name, const Platform& platform, CpuPlatform::PlatformData& data) : CalcCustomGBForceKernel(name, platform), data(data), ixn(NULL), neighborList(NULL) { } ~CpuCalcCustomGBForceKernel(); /** * Initialize the kernel. * * @param system the System this kernel will be applied to * @param force the CustomGBForce this kernel will be used for */ void initialize(const System& system, const CustomGBForce& force); /** * Execute the kernel to calculate the forces and/or energy. * * @param context the context in which to execute this kernel * @param includeForces true if forces should be calculated * @param includeEnergy true if the energy should be calculated * @return the potential energy due to the force */ double execute(ContextImpl& context, bool includeForces, bool includeEnergy); /** * Copy changed parameters over to a context. * * @param context the context to copy parameters to * @param force the CustomGBForce to copy the parameters from */ void copyParametersToContext(ContextImpl& context, const CustomGBForce& force); private: void createInteraction(const CustomGBForce& force); CpuPlatform::PlatformData& data; int numParticles; bool isPeriodic; std::vector > particleParamArray; double nonbondedCutoff; CpuCustomGBForce* ixn; CpuNeighborList* neighborList; std::vector > exclusions; std::vector particleParameterNames, globalParameterNames, energyParamDerivNames, valueNames; std::vector valueTypes; std::vector energyTypes; std::map tabulatedFunctionUpdateCount; NonbondedMethod nonbondedMethod; }; /** * This kernel is invoked by CustomManyParticleForce to calculate the forces acting on the system and the energy of the system. */ class CpuCalcCustomManyParticleForceKernel : public CalcCustomManyParticleForceKernel { public: CpuCalcCustomManyParticleForceKernel(std::string name, const Platform& platform, CpuPlatform::PlatformData& data) : CalcCustomManyParticleForceKernel(name, platform), data(data), ixn(NULL) { } ~CpuCalcCustomManyParticleForceKernel(); /** * Initialize the kernel. * * @param system the System this kernel will be applied to * @param force the CustomManyParticleForce this kernel will be used for */ void initialize(const System& system, const CustomManyParticleForce& force); /** * Execute the kernel to calculate the forces and/or energy. * * @param context the context in which to execute this kernel * @param includeForces true if forces should be calculated * @param includeEnergy true if the energy should be calculated * @return the potential energy due to the force */ double execute(ContextImpl& context, bool includeForces, bool includeEnergy); /** * Copy changed parameters over to a context. * * @param context the context to copy parameters to * @param force the CustomManyParticleForce to copy the parameters from */ void copyParametersToContext(ContextImpl& context, const CustomManyParticleForce& force); private: CpuPlatform::PlatformData& data; int numParticles; double cutoffDistance; std::vector > particleParamArray; CpuCustomManyParticleForce* ixn; std::vector globalParameterNames; std::map tabulatedFunctionUpdateCount; NonbondedMethod nonbondedMethod; }; /** * This kernel is invoked by GayBerneForce to calculate the forces acting on the system. */ class CpuCalcGayBerneForceKernel : public CalcGayBerneForceKernel { public: CpuCalcGayBerneForceKernel(std::string name, const Platform& platform, CpuPlatform::PlatformData& data) : CalcGayBerneForceKernel(name, platform), data(data), ixn(NULL) { } ~CpuCalcGayBerneForceKernel(); /** * Initialize the kernel. * * @param system the System this kernel will be applied to * @param force the GayBerneForce this kernel will be used for */ void initialize(const System& system, const GayBerneForce& force); /** * Execute the kernel to calculate the forces and/or energy. * * @param context the context in which to execute this kernel * @param includeForces true if forces should be calculated * @return the potential energy due to the force */ double execute(ContextImpl& context, bool includeForces, bool includeEnergy); /** * Copy changed parameters over to a context. * * @param context the context to copy parameters to * @param force the GayBerneForce to copy the parameters from */ void copyParametersToContext(ContextImpl& context, const GayBerneForce& force); private: CpuPlatform::PlatformData& data; CpuGayBerneForce* ixn; }; /** * This kernel is invoked by LCPOForce to calculate the forces acting on the system and the energy of the system. */ class CpuCalcLCPOForceKernel : public CalcLCPOForceKernel { public: CpuCalcLCPOForceKernel(std::string name, const Platform& platform, CpuPlatform::PlatformData& data) : CalcLCPOForceKernel(name, platform), data(data), ixn(NULL) { } ~CpuCalcLCPOForceKernel(); /** * Initialize the kernel. * * @param system the System this kernel will be applied to * @param force the LCPOForce this kernel will be used for */ void initialize(const System& system, const LCPOForce& force); /** * Execute the kernel to calculate the forces and/or energy. * * @param context the context in which to execute this kernel * @param includeForces true if forces should be calculated * @param includeEnergy true if the energy should be calculated * @return the potential energy due to the force */ double execute(ContextImpl& context, bool includeForces, bool includeEnergy); /** * Copy changed parameters over to a context. * * @param context the context to copy parameters to * @param force the LCPOForce to copy the parameters from */ void copyParametersToContext(ContextImpl& context, const LCPOForce& force); private: CpuPlatform::PlatformData& data; CpuLCPOForce* ixn; bool doInteraction; double oneBodyEnergy; std::vector activeParticles; std::vector parameters; }; /** * This kernel is invoked by LangevinMiddleIntegrator to take one time step. */ class CpuIntegrateLangevinMiddleStepKernel : public IntegrateLangevinMiddleStepKernel { public: CpuIntegrateLangevinMiddleStepKernel(std::string name, const Platform& platform, CpuPlatform::PlatformData& data) : IntegrateLangevinMiddleStepKernel(name, platform), data(data), dynamics(0) { } ~CpuIntegrateLangevinMiddleStepKernel(); /** * Initialize the kernel, setting up the particle masses. * * @param system the System this kernel will be applied to * @param integrator the LangevinMiddleIntegrator this kernel will be used for */ void initialize(const System& system, const LangevinMiddleIntegrator& integrator); /** * Execute the kernel. * * @param context the context in which to execute this kernel * @param integrator the LangevinMiddleIntegrator this kernel is being used for */ void execute(ContextImpl& context, const LangevinMiddleIntegrator& integrator); /** * Compute the kinetic energy. * * @param context the context in which to execute this kernel * @param integrator the LangevinMiddleIntegrator this kernel is being used for */ double computeKineticEnergy(ContextImpl& context, const LangevinMiddleIntegrator& integrator); private: CpuPlatform::PlatformData& data; CpuLangevinMiddleDynamics* dynamics; std::vector masses; double prevTemp, prevFriction, prevStepSize; }; } // namespace OpenMM #endif /*OPENMM_CPUKERNELS_H_*/