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 
212 class OPENMM_EXPORT CustomIntegrator : public Integrator {
213 public:
221  ComputeGlobal = 0,
225  ComputePerDof = 1,
229  ComputeSum = 2,
233  ConstrainPositions = 3,
237  ConstrainVelocities = 4,
241  UpdateContextState = 5
242  };
248  CustomIntegrator(double stepSize);
252  int getNumGlobalVariables() const {
253  return globalNames.size();
254  }
258  int getNumPerDofVariables() const {
259  return perDofNames.size();
260  }
264  int getNumComputations() const {
265  return computations.size();
266  }
274  int addGlobalVariable(const std::string& name, double initialValue);
281  const std::string& getGlobalVariableName(int index) const;
290  int addPerDofVariable(const std::string& name, double initialValue);
297  const std::string& getPerDofVariableName(int index) const;
304  double getGlobalVariable(int index) const;
311  void setGlobalVariable(int index, double value);
318  void setGlobalVariableByName(const std::string& name, double value);
326  void getPerDofVariable(int index, std::vector<Vec3>& values) const;
333  void setPerDofVariable(int index, const std::vector<Vec3>& values);
340  void setPerDofVariableByName(const std::string& name, const std::vector<Vec3>& values);
350  int addComputeGlobal(const std::string& variable, const std::string& expression);
361  int addComputePerDof(const std::string& variable, const std::string& expression);
373  int addComputeSum(const std::string& variable, const std::string& expression);
380  int addConstrainPositions();
387  int addConstrainVelocities();
394  int addUpdateContextState();
406  void getComputationStep(int index, ComputationType& type, std::string& variable, std::string& expression) const;
412  const std::string& getKineticEnergyExpression() const;
418  void setKineticEnergyExpression(const std::string& expression);
422  int getRandomNumberSeed() const {
423  return randomNumberSeed;
424  }
433  void setRandomNumberSeed(int seed) {
434  randomNumberSeed = seed;
435  }
441  void step(int steps);
442 protected:
448  void initialize(ContextImpl& context);
453  void cleanup();
457  void stateChanged(State::DataType changed);
461  std::vector<std::string> getKernelNames();
465  double computeKineticEnergy();
466 private:
467  class ComputationInfo;
468  std::vector<std::string> globalNames;
469  std::vector<std::string> perDofNames;
470  mutable std::vector<double> globalValues;
471  std::vector<std::vector<Vec3> > perDofValues;
472  std::vector<ComputationInfo> computations;
473  std::string kineticEnergy;
474  mutable bool globalsAreCurrent;
475  int randomNumberSeed;
476  bool forcesAreValid;
477  Kernel kernel;
478 };
479 
484 class CustomIntegrator::ComputationInfo {
485 public:
486  ComputationType type;
487  std::string variable, expression;
488  ComputationInfo() {
489  }
490  ComputationInfo(ComputationType type, const std::string& variable, const std::string& expression) :
491  type(type), variable(variable), expression(expression) {
492  }
493 };
494 
495 } // namespace OpenMM
496 
497 #endif /*OPENMM_CUSTOMINTEGRATOR_H_*/