IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
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
20IMPATOM_BEGIN_NAMESPACE
21
22//! A particle with linear (XYZ) velocity
23/** Typically this is used in combination with the MolecularDynamics optimizer.
24 */
25class 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
31public:
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
39 IMP_DECORATOR_METHODS(LinearVelocity, Decorator);
40 IMP_DECORATOR_SETUP_0(LinearVelocity);
41 IMP_DECORATOR_SETUP_1(LinearVelocity, algebra::Vector3D, v);
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 */
61class 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
71public:
72
73 static bool get_is_setup(Model *m, ParticleIndex pi) {
74 return m->get_has_attribute(get_velocities_key(), pi);
75 }
76
77 IMP_DECORATOR_METHODS(AngularVelocity, Decorator);
78 IMP_DECORATOR_SETUP_0(AngularVelocity);
79 IMP_DECORATOR_SETUP_1(AngularVelocity, algebra::Vector4D, v);
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 */
109class IMPATOMEXPORT MolecularDynamics : public Simulator {
110 public:
111 /** Score based on the provided model */
113
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 */
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 */
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;
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)
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
191IMPATOM_END_NAMESPACE
192
193#endif /* IMPATOM_MOLECULAR_DYNAMICS_H */
Base class for all optimizers.
Classes to handle individual model particles. (Note that implementation of inline functions is in int...
Base class for "simulators", such as molecular dynamics.
Macros for maintaining molecular hierarchies.
ParticleIndex get_particle_index() const
Returns the particle index decorated by this decorator.
Definition Decorator.h:211
Model * get_model() const
Returns the Model containing the particle.
Definition Decorator.h:214
Class for storing model, its restraints, constraints, and particles.
Definition Model.h:111
A particle with angular velocity.
A particle with linear (XYZ) velocity.
Simple molecular dynamics simulator.
void set_velocity_cap(Float velocity_cap)
Set maximum velocity in A/fs.
virtual void assign_velocities(Float temperature)
Assign velocities representative of the given temperature.
void cap_velocity_component(Float &vel)
Cap a velocity component to the maximum value.
virtual double do_step(const ParticleIndexes &sc, double dt) override
Perform a single time step.
virtual void propagate_velocities(const ParticleIndexes &ps, double step_size)
Second part of velocity Verlet (update velocity).
virtual Float get_kinetic_energy() const
Return the current kinetic energy of the system, in kcal/mol.
virtual bool get_is_simulation_particle(ParticleIndex p) const override
Return true if the passed particle is appropriate for the simulation.
int degrees_of_freedom_
Number of degrees of freedom in the system.
int get_degrees_of_freedom()
Get number of degrees of freedom in the system.
Float velocity_cap_
Maximum absolute value of a single velocity component.
virtual void propagate_coordinates(const ParticleIndexes &ps, double step_size)
First part of velocity Verlet (update coordinates and half-step velocity).
Float get_velocity_cap() const
Get the current value of the velocity cap.
Float get_kinetic_temperature(Float ekinetic) const
Return the current kinetic temperature of the system.
virtual void setup(const ParticleIndexes &ps) override
ParticleIndexes get_simulation_particle_indexes() const
Simulator(Model *m, std::string name="Simulator %1%", double wave_factor=1.0)
Index< ParticleIndexTag > ParticleIndex
Definition base_types.h:196
double Float
Basic floating-point value (could be float, double...).
Definition types.h:19
Key< 16 > Vector3DKey
The type used to identify 3D vector attributes in the Particles.
Definition base_types.h:102
Vector< ParticleIndex, std::allocator< ParticleIndex > > ParticleIndexes
A list of particles, each identified by its ParticleIndex.
Definition base_types.h:199
VectorD< 3 > Vector3D
Definition VectorD.h:407
VectorD< 4 > Vector4D
Definition VectorD.h:411
Key< 10 > FloatsKey
The type used to identify float vector attributes in the Particles.
Definition base_types.h:60
#define IMP_OBJECT_SERIALIZE_DECL(Name)
Declare methods needed for serialization of Object pointers.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.