IMP logo
IMP Reference Guide  develop.4eee3cf66f,2026/08/02
The Integrative Modeling Platform
atom/MolecularDynamics.h
Go to the documentation of this file.
1 /**
2  * \file IMP/atom/MolecularDynamics.h
3  * \brief Simple molecular dynamics optimizer.
4  *
5  * Copyright 2007-2026 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPATOM_MOLECULAR_DYNAMICS_H
10 #define IMPATOM_MOLECULAR_DYNAMICS_H
11 
12 #include <IMP/atom/atom_config.h>
13 #include "Simulator.h"
14 #include "atom_macros.h"
15 #include <IMP/Particle.h>
16 #include <IMP/Optimizer.h>
17 #include <cereal/access.hpp>
18 #include <cereal/types/base_class.hpp>
19 
20 IMPATOM_BEGIN_NAMESPACE
21 
22 //! A particle with linear (XYZ) velocity
23 /** Typically this is used in combination with the MolecularDynamics optimizer.
24  */
25 class IMPATOMEXPORT LinearVelocity : public Decorator {
26  static void do_setup_particle(Model *m, ParticleIndex pi,
27  const algebra::Vector3D v = algebra::Vector3D(0, 0, 0)) {
28  m->add_attribute(get_velocity_key(), pi, v);
29  }
30 
31 public:
32  static bool get_is_setup(Model *m, ParticleIndex pi) {
33  return m->get_has_attribute(get_velocity_key(), pi);
34  }
35 
36  /* Get the key used to store velocity */
37  static Vector3DKey get_velocity_key();
38 
42 
43  void set_velocity(const algebra::Vector3D &v) {
44  Model *m = get_model();
46  m->set_attribute(get_velocity_key(), pi, v);
47  }
48 
49  algebra::Vector3D get_velocity() const {
50  Model *m = get_model();
52  return m->get_attribute(get_velocity_key(), pi);
53  }
54 };
55 
56 //! A particle with angular velocity
57 /** Typically this is used for RigidBody particles in combination
58  with the MolecularDynamics optimizer. The velocity is stored as
59  a quaternion.
60  */
61 class IMPATOMEXPORT AngularVelocity : public Decorator {
62  static void do_setup_particle(Model *m, ParticleIndex pi,
63  const algebra::Vector4D v = algebra::Vector4D(0, 0, 0, 0)) {
64  m->add_attribute(get_velocities_key(), pi, v.get_coordinates());
65  }
66  static FloatsKey get_velocities_key() {
67  static const FloatsKey key("angvel");
68  return key;
69  }
70 
71 public:
72 
73  static bool get_is_setup(Model *m, ParticleIndex pi) {
74  return m->get_has_attribute(get_velocities_key(), pi);
75  }
76 
80 
81  void set_velocity(const algebra::Vector4D &v) {
82  Model *m = get_model();
84  m->set_attribute(get_velocities_key(), pi, v.get_coordinates());
85  }
86 
87  algebra::Vector4D get_velocity() const {
88  Model *m = get_model();
90  return algebra::Vector4D(m->get_attribute(get_velocities_key(), pi));
91  }
92 };
93 
94 //! Simple molecular dynamics simulator.
95 /**
96  The particles to be optimized must have optimizable x,y,z attributes
97  and a non-optimizable mass attribute; this optimizer assumes the score
98  to be energy in kcal/mol, the xyz coordinates to be in angstroms, and
99  the mass to be in AMU (g/mol).
100 
101  \note RigidBody particles are not handled properly.
102 
103  Particles without optimized x,y,z and nonoptimized mass are skipped.
104  \see VelocityScalingOptimizerState
105  \see LangevinThermostatOptimizerState
106  \see BerendsenThermostatOptimizerState
107  \see RemoveRigidMotionOptimizerState
108  */
109 class IMPATOMEXPORT MolecularDynamics : public Simulator {
110  public:
111  /** Score based on the provided model */
113 
114  MolecularDynamics() {}
115 
116  //! Return the current kinetic energy of the system, in kcal/mol
117  virtual Float get_kinetic_energy() const;
118 
119  //! Return the current kinetic temperature of the system
120  /** \param[in] ekinetic kinetic energy, e.g. from get_kinetic_energy()
121  */
122  Float get_kinetic_temperature(Float ekinetic) const;
123 
124  //! Set maximum velocity in A/fs
125  /** At each dynamics time step, the absolute value of each velocity
126  component is capped at this value. This prevents spurious strong forces
127  (occasionally encountered with frustrated conformations) from causing
128  large oscillations in the system.
129  By default, velocities are not capped.
130 
131  \note The actual velocities that are capped are the half-step velocities
132  in the velocity Verlet algorithm.
133  */
134  void set_velocity_cap(Float velocity_cap) { velocity_cap_ = velocity_cap; }
135 
136  //! Get the current value of the velocity cap
137  /** If velocities are not capped, infinity is returned */
138  Float get_velocity_cap() const { return velocity_cap_; }
139 
140  //! Get number of degrees of freedom in the system
142  setup_degrees_of_freedom(get_simulation_particle_indexes());
143  return degrees_of_freedom_;
144  }
145 
146  //! Assign velocities representative of the given temperature
147  virtual void assign_velocities(Float temperature);
148  virtual void setup(const ParticleIndexes &ps) override;
149  virtual double do_step(const ParticleIndexes &sc,
150  double dt) override;
151  virtual bool get_is_simulation_particle(ParticleIndex p) const
152  override;
153 
155 
156  protected:
157  void initialize();
158 
159  virtual void setup_degrees_of_freedom(const ParticleIndexes &ps);
160 
161  //! First part of velocity Verlet (update coordinates and half-step velocity)
162  virtual void propagate_coordinates(const ParticleIndexes &ps,
163  double step_size);
164 
165  //! Second part of velocity Verlet (update velocity)
166  virtual void propagate_velocities(const ParticleIndexes &ps,
167  double step_size);
168 
169  //! Cap a velocity component to the maximum value.
170  inline void cap_velocity_component(Float &vel) {
171  if (vel >= 0.0) {
172  vel = std::min(vel, velocity_cap_);
173  } else {
174  vel = std::max(vel, -velocity_cap_);
175  }
176  }
177 
178  //! Number of degrees of freedom in the system
180 
181  //! Maximum absolute value of a single velocity component
183 
184  friend class cereal::access;
185  template<class Archive> void serialize(Archive &ar) {
186  ar(cereal::base_class<Simulator>(this), degrees_of_freedom_, velocity_cap_);
187  }
189 };
190 
191 IMPATOM_END_NAMESPACE
192 
193 #endif /* IMPATOM_MOLECULAR_DYNAMICS_H */
A particle with angular velocity.
The base class for simulators.
Definition: Simulator.h:36
ParticleIndex get_particle_index() const
Returns the particle index decorated by this decorator.
Definition: Decorator.h:211
ParticleIndexes get_simulation_particle_indexes() const
#define IMP_DECORATOR_SETUP_1(Name, FirstArgumentType, first_argument_name)
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Model * get_model() const
Returns the Model containing the particle.
Definition: Decorator.h:214
virtual bool get_is_simulation_particle(ParticleIndex p) const =0
Return true if the passed particle is appropriate for the simulation.
VectorD< 4 > Vector4D
Definition: VectorD.h:411
A particle with linear (XYZ) velocity.
Base class for all optimizers.
virtual double do_step(const ParticleIndexes &sc, double dt)=0
Perform a single time step.
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
virtual void setup(const ParticleIndexes &)
Definition: Simulator.h:182
void set_velocity_cap(Float velocity_cap)
Set maximum velocity in A/fs.
void add_attribute(TypeKey attribute_key, ParticleIndex particle, Type value)
add particle attribute with the specified key and initial value
Simple molecular dynamics simulator.
VectorD< 3 > Vector3D
Definition: VectorD.h:407
int degrees_of_freedom_
Number of degrees of freedom in the system.
#define IMP_OBJECT_SERIALIZE_DECL(Name)
Declare methods needed for serialization of Object pointers.
Definition: object_macros.h:95
void set_attribute(TypeKey attribute_key, ParticleIndex particle, Type value)
set the value of particle attribute with the specified key
A base class for Keys.
Definition: Key.h:45
Base class for "simulators", such as molecular dynamics.
#define IMP_DECORATOR_SETUP_0(Name)
Interface to specialized Particle types (e.g. atoms)
Definition: Decorator.h:119
Classes to handle individual model particles. (Note that implementation of inline functions is in int...
void cap_velocity_component(Float &vel)
Cap a velocity component to the maximum value.
Macros for maintaining molecular hierarchies.
Float velocity_cap_
Maximum absolute value of a single velocity component.
int get_degrees_of_freedom()
Get number of degrees of freedom in the system.
#define IMP_DECORATOR_METHODS(Name, Parent)
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:19
bool get_has_attribute(TypeKey attribute_key, ParticleIndex particle) const
return true if particle has attribute with the specified key
Type get_attribute(TypeKey attribute_key, ParticleIndex particle)
get the value of the particle attribute with the specified key
Float get_velocity_cap() const
Get the current value of the velocity cap.