IMP logo
IMP Reference Guide  develop.385bf31a7a,2026/08/05
The Integrative Modeling Platform
Simulator.h
Go to the documentation of this file.
1 /**
2  * \file IMP/atom/Simulator.h
3  * \brief Base class for "simulators", such as molecular dynamics.
4  *
5  * Copyright 2007-2026 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPATOM_SIMULATOR_H
10 #define IMPATOM_SIMULATOR_H
11 
12 #include <IMP/atom/atom_config.h>
13 
14 #include <IMP/Particle.h>
15 #include <IMP/Optimizer.h>
16 #include <IMP/internal/units.h>
17 #include <IMP/algebra/Vector3D.h>
18 #include <cereal/access.hpp>
19 #include <cereal/types/base_class.hpp>
20 
21 IMPATOM_BEGIN_NAMESPACE
22 
23 // for swig
24 class SimulationParameters;
25 
26 //! The base class for simulators.
27 /**
28  A simulator is an optimizer with dynamic tracking of time,
29  such that each frame is associated with a (possibly variable size)
30  time step.
31 
32  The simulation can be invoked directly by calling simulate(fs) for
33  a given time in femtoseconds, or by calling Optimizer::optimize(nf)
34  for a given number of frames.
35  */
36 class IMPATOMEXPORT Simulator : public Optimizer {
37  public:
38  /**
39  @param m model associated with simulator
40  @param name simulator name where %1% is a joker
41  @param wave_factor if >=1.001, use wave time step size with larger maximal
42  time step, using simulate_wave() when calling optimize()
43 
44  @note wave_factor is an advanced feature - if you're not sure, just use its
45  default, see also simulate_wave()
46  @note wave_factor is experimental and liable to change at any time.
47 
48  \see simulate()
49  */
50  Simulator(Model *m, std::string name = "Simulator %1%",
51  double wave_factor = 1.0);
52 
53  Simulator() {}
54 
55  //! Simulate for a given time in fs
56  /**
57  simulate for at least the passed time, by calling do_simulate()
58  with optimizing states turned on
59 
60  @param time_in_fs time in femtoseconds
61  */
62  double simulate(double time_in_fs);
63 
64  //! Simulate for a given time in fs using a wave step function
65  //! with maximal time step increased by up to max_time_step_factor
66  /**
67  simulate for at least the passed time, by calling do_simulate_wave()
68  with optimizing states turned on
69 
70  @param time_in_fs time_in_fs in femtoseconds
71  @param max_time_step_factor the maximal factor by which the
72  maximum time step is exceeded
73  @param base base by which time step increases or decreases
74  during the wave
75 
76  @note This function is experimental and liable to change at any
77  time.
78  */
79  double simulate_wave(double time_in_fs, double max_time_step_factor = 10.0,
80  double base = 1.5);
81 
82  //! Return the simulator temperature in kelvin
83  double get_temperature() const { return temperature_; }
84 
85  //! Set the temperature of the simulator to d in kelvin units
86  /**
87  Sets the temperature of the simulator. Note that some simulators
88  (e.g. BrownianDynamics) may rely on other temperature-dependent
89  constants (e.g. diffusion coefficients via Diffusion decorator)
90  that will need to be updated independently to reflect the new
91  temperature.
92 
93  @param d temperature in K
94  */
95  void set_temperature(double d) { temperature_ = d; }
96 
97  /** \name Time steps
98  The simulator has a maximum allowed time step. It can take
99  shorter ones if needed due to stability concerns.
100 
101  As with all times in \imp, the time step is in fs.
102  @{
103  */
104  void set_maximum_time_step(double ts) { max_time_step_ = ts; }
105 
106  //! Get the maximum allowed time step in fs
107  double get_maximum_time_step() const { return max_time_step_; }
108 
109  //! Get the time of the last simulated time step (or the maximal time step
110  //! if the simulation has not started), in units of fs
111  double get_last_time_step() const {
112  if (last_time_step_ < 0)
113  return get_maximum_time_step();
114  else
115  return last_time_step_;
116  }
117 /** @} */
118 #ifndef IMP_DOXYGEN
119  void set_time_step(double ts) { set_maximum_time_step(ts); }
120 #endif
121 
122  // returns kt in units of kcal/mol for temperature get_temperature() [K]
123  double get_kt() const;
124 
125  /**
126  returns the simulation time in femtoseconds that was performed
127  by this simulator since it was constructed
128  @note this time can be tweaked using set_current_time() )
129  */
130  double get_current_time() const { return current_time_; }
131 
132  //! Sets the current simulation time in femtoseconds to ct.
133  void set_current_time(double ct) { current_time_ = ct; }
134 
135  //! Returns the set of particles used in the simulation.
136  /** If a non-empty
137  set of particles was provided explicitly by earlier calls to the
138  particles list accessor methods, eg, add_particles(), this set
139  it returned. Otherwise, the associated Model object is
140  searched for appropriate particles that have a mass and XYZ
141  decorators.
142 
143  \see add_particle()
144  \see add_particles()
145  \see remove_particle()
146  \see clear_particles()
147  \see set_particles()
148  \see set_particles_order()
149  */
150  ParticlesTemp get_simulation_particles() const;
151 
152  /**
153  Same as get_simulation_particles(), but returns particle
154  model indexes.
155 
156  \see get_simulation_particles()
157  */
158  ParticleIndexes get_simulation_particle_indexes() const;
159 
160  /** \name Explicitly accessing the particles list
161 
162  One can explicitly specify which particles should be used for
163  the simulation, or retrieve information about the list of particles.
164  Each particle must be a Mass and core::XYZ particle. If none are
165  specified, the model is searched for appropriate particles, based
166  on the get_simulation_particles() method, which can be overridden
167  by child classes.
168  @{
169  */
170  IMP_LIST(public, Particle, particle, Particle *, Particles);
171 
172  protected:
173  /** @} */
174  virtual Float do_optimize(unsigned int max_steps) override IMP_SWIG_FINAL;
175 
176  /** Perform any setup operations needed before running a series
177  of simulation steps
178 
179  @note Called by do_simulate() or do_simulate_wave() before iterative
180  calls to do_step()
181 */
182  virtual void setup(const ParticleIndexes &) {};
183 
184  //! Perform a single time step
185  /** \param[in] sc the particles that should be moved
186  \param[in] dt maximum time step value
187  \return the amount that time should be advanced.
188  */
189  virtual double do_step(const ParticleIndexes &sc, double dt) = 0;
190 
191  //! Return true if the passed particle is appropriate for the simulation.
192  virtual bool get_is_simulation_particle(ParticleIndex p) const = 0;
193 
194  /** called by simulate() -
195  calls setup() and then calls do_step() iteratively
196  till given simulation time is completed
197 
198  @param time time to simulate
199 
200  @return score at end of simulation period
201  */
202  virtual double do_simulate(double time);
203 
204  /** Calls the protected method setup() and then calls
205  method do_step() iteratively, and using a self adjusting time
206  step that can grow up to max_time_step_factor times than
207  the default time step returned by get_maximum_time_step()
208 
209  \see simulate_wave()
210  */
211  virtual double do_simulate_wave(double time_in_fs, double max_time_step_factor = 10.0,
212  double base = 1.5);
213 
214  private:
215  double temperature_;
216  double max_time_step_;
217  double current_time_;
218  double last_time_step_;
219  double wave_factor_; // if >1.0, use simulate_wave() from do_optimize()
220 
221  friend class cereal::access;
222  template<class Archive> void serialize(Archive &ar) {
223  ar(cereal::base_class<Optimizer>(this), temperature_, max_time_step_,
224  current_time_, last_time_step_, wave_factor_,
225  mutable_access_particles());
226  }
227 };
228 
230 
231 
232 IMPATOM_END_NAMESPACE
233 
234 #endif /* IMPATOM_SIMULATOR_H */
double get_kt(double T)
Return kT for a given temperature in units of [kcal/mol].
The base class for simulators.
Definition: Simulator.h:36
double get_current_time() const
Definition: Simulator.h:130
void set_temperature(double d)
Set the temperature of the simulator to d in kelvin units.
Definition: Simulator.h:95
double get_maximum_time_step() const
Get the maximum allowed time step in fs.
Definition: Simulator.h:107
Base class for all optimizers.
#define IMP_LIST(protection, Ucname, lcname, Data, PluralData)
A macro to provide a uniform interface for storing lists of objects.
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
virtual void setup(const ParticleIndexes &)
Definition: Simulator.h:182
virtual double do_optimize(unsigned int ns)=0
override this function to do actual optimization
void set_current_time(double ct)
Sets the current simulation time in femtoseconds to ct.
Definition: Simulator.h:133
Base class for all optimizers.
Definition: Optimizer.h:48
Simple 3D vector class.
double get_last_time_step() const
Definition: Simulator.h:111
Classes to handle individual model particles. (Note that implementation of inline functions is in int...
double get_temperature() const
Return the simulator temperature in kelvin.
Definition: Simulator.h:83
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing lists of object pointers.
Definition: object_macros.h:44
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:19
Class to handle individual particles of a Model object.
Definition: Particle.h:45
#define IMP_SWIG_FINAL
Have the compiler report an error if anything overrides this method.