OpenMM
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends
CustomIntegrator.h
1 #ifndef OPENMM_CUSTOMINTEGRATOR_H_
2 #define OPENMM_CUSTOMINTEGRATOR_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * OpenMM *
6  * -------------------------------------------------------------------------- *
7  * This is part of the OpenMM molecular simulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org. *
11  * *
12  * Portions copyright (c) 2011-2012 Stanford University and the Authors. *
13  * Authors: Peter Eastman *
14  * Contributors: *
15  * *
16  * Permission is hereby granted, free of charge, to any person obtaining a *
17  * copy of this software and associated documentation files (the "Software"), *
18  * to deal in the Software without restriction, including without limitation *
19  * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
20  * and/or sell copies of the Software, and to permit persons to whom the *
21  * Software is furnished to do so, subject to the following conditions: *
22  * *
23  * The above copyright notice and this permission notice shall be included in *
24  * all copies or substantial portions of the Software. *
25  * *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
27  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
28  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
29  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
30  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
31  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
32  * USE OR OTHER DEALINGS IN THE SOFTWARE. *
33  * -------------------------------------------------------------------------- */
34 
35 #include "Integrator.h"
36 #include "Vec3.h"
37 #include "openmm/Kernel.h"
38 #include "internal/windowsExport.h"
39 #include <string>
40 #include <vector>
41 
42 namespace OpenMM {
43 
211 class OPENMM_EXPORT CustomIntegrator : public Integrator {
212 public:
220  ComputeGlobal = 0,
224  ComputePerDof = 1,
228  ComputeSum = 2,
232  ConstrainPositions = 3,
236  ConstrainVelocities = 4,
240  UpdateContextState = 5
241  };
247  CustomIntegrator(double stepSize);
251  int getNumGlobalVariables() const {
252  return globalNames.size();
253  }
257  int getNumPerDofVariables() const {
258  return perDofNames.size();
259  }
263  int getNumComputations() const {
264  return computations.size();
265  }
273  int addGlobalVariable(const std::string& name, double initialValue);
280  const std::string& getGlobalVariableName(int index) const;
289  int addPerDofVariable(const std::string& name, double initialValue);
296  const std::string& getPerDofVariableName(int index) const;
303  double getGlobalVariable(int index) const;
310  void setGlobalVariable(int index, double value);
317  void setGlobalVariableByName(const std::string& name, double value);
325  void getPerDofVariable(int index, std::vector<Vec3>& values) const;
332  void setPerDofVariable(int index, const std::vector<Vec3>& values);
339  void setPerDofVariableByName(const std::string& name, const std::vector<Vec3>& values);
349  int addComputeGlobal(const std::string& variable, const std::string& expression);
360  int addComputePerDof(const std::string& variable, const std::string& expression);
372  int addComputeSum(const std::string& variable, const std::string& expression);
379  int addConstrainPositions();
386  int addConstrainVelocities();
393  int addUpdateContextState();
405  void getComputationStep(int index, ComputationType& type, std::string& variable, std::string& expression) const;
411  const std::string& getKineticEnergyExpression() const;
417  void setKineticEnergyExpression(const std::string& expression);
421  int getRandomNumberSeed() const {
422  return randomNumberSeed;
423  }
432  void setRandomNumberSeed(int seed) {
433  randomNumberSeed = seed;
434  }
440  void step(int steps);
441 protected:
447  void initialize(ContextImpl& context);
452  void cleanup();
456  void stateChanged(State::DataType changed);
460  std::vector<std::string> getKernelNames();
464  double computeKineticEnergy();
465 private:
466  class ComputationInfo;
467  std::vector<std::string> globalNames;
468  std::vector<std::string> perDofNames;
469  mutable std::vector<double> globalValues;
470  std::vector<std::vector<Vec3> > perDofValues;
471  std::vector<ComputationInfo> computations;
472  std::string kineticEnergy;
473  mutable bool globalsAreCurrent;
474  int randomNumberSeed;
475  bool forcesAreValid;
476  Kernel kernel;
477 };
478 
483 class CustomIntegrator::ComputationInfo {
484 public:
485  ComputationType type;
486  std::string variable, expression;
487  ComputationInfo() {
488  }
489  ComputationInfo(ComputationType type, const std::string& variable, const std::string& expression) :
490  type(type), variable(variable), expression(expression) {
491  }
492 };
493 
494 } // namespace OpenMM
495 
496 #endif /*OPENMM_CUSTOMINTEGRATOR_H_*/