IMP  2.1.0
The Integrative Modeling Platform
kernel/declare_Model.h
Go to the documentation of this file.
1 /**
2  * \file IMP/kernel/declare_Model.h
3  * \brief Storage of a model, its restraints,
4  * constraints and particles.
5  *
6  * Copyright 2007-2013 IMP Inventors. All rights reserved.
7  *
8  */
9 
10 #ifndef IMPKERNEL_DECLARE_MODEL_H
11 #define IMPKERNEL_DECLARE_MODEL_H
12 
13 #include <IMP/kernel/kernel_config.h>
14 #include "ModelObject.h"
16 #include "declare_Restraint.h"
17 #include "declare_RestraintSet.h"
18 #include "ScoreState.h"
19 #include "container_macros.h"
20 #include "base_types.h"
21 #include "declare_Particle.h"
22 #include "Undecorator.h"
23 #include "internal/AttributeTable.h"
24 #include "internal/attribute_tables.h"
25 #include <IMP/base/Object.h>
26 #include <IMP/base/Pointer.h>
27 #include <IMP/base/map.h>
28 #include <IMP/base/set.h>
29 #include <IMP/base/tuple_macros.h>
30 #include <boost/iterator/transform_iterator.hpp>
31 #include <boost/iterator/filter_iterator.hpp>
32 
33 #include <limits>
34 
35 IMPKERNEL_BEGIN_NAMESPACE
36 
37 class ModelObject;
38 class Undecorator;
39 
40 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
41 namespace internal {
42 enum Stage {
43  NOT_EVALUATING,
44  BEFORE_EVALUATING,
45  EVALUATING,
46  AFTER_EVALUATING,
47  COMPUTING_DEPENDENCIES
48 };
49 }
50 #endif
51 
52 class Model;
53 
54 //! Class for storing model, its restraints, constraints, and particles.
55 /** The Model maintains a standard \imp container for each of Particle,
56  ScoreState and Restraint object types.
57 
58  Each Float attribute has an associated range which reflects the
59  range of values that it is expected to take on during optimization.
60  The optimizer can use these ranges to make the optimization process
61  more efficient. By default, the range estimates are simply the
62  range of values for that attribute in the various particles, but
63  it can be set to another value. For example, an attribute storing
64  an angle could have the range set to (0,PI).
65 
66  The ranges are not enforced; they are just guidelines. In order to
67  enforce ranges, see, for example,
68  IMP::example::ExampleSingletonModifier.
69 
70  \headerfile Model.h "IMP/Model.h"
71  */
72 class IMPKERNELEXPORT Model : public base::Object
73 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
74  ,
75  public internal::Masks,
76  // The attribute tables provide fast access to
77  // e.g. particle attributes, etc.
78  public internal::FloatAttributeTable,
79  public internal::StringAttributeTable,
80  public internal::IntAttributeTable,
81  public internal::ObjectAttributeTable,
82  public internal::WeakObjectAttributeTable,
83  public internal::IntsAttributeTable,
84  public internal::ObjectsAttributeTable,
85  public internal::ParticleAttributeTable,
86  public internal::ParticlesAttributeTable
87 #endif
88  {
90  // must be up top
91  // we don't want any liveness checks
92  IMP_NAMED_TUPLE_5(NodeInfo, NodeInfos, Edges, inputs, Edges, input_outputs,
93  Edges, outputs, Edges, readers, Edges, writers, );
95  DependencyGraph dependency_graph_;
96  base::set<const ModelObject *> no_dependencies_;
98 
99  // basic representation
101 
102  ParticleIndexes free_particles_;
105 
107  ////////////// DEPRECATED
108  // for old code that uses the model for the scoring function
110 
111  void do_add_dependencies(const ModelObject *mo);
112  void do_clear_required_score_states(kernel::ModelObject *mo);
113  void do_check_required_score_states(const ModelObject *mo) const;
114  void do_check_update_order(const ScoreState *ss) const;
115  void do_check_inputs_and_outputs(const ModelObject *mo) const;
116  void do_check_readers_and_writers(const ModelObject *mo) const;
117  void do_check_not_in_readers_and_writers(const ModelObject *mo) const;
118  void do_clear_dependencies(const ModelObject *mo);
119 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
120  // things the evaluate template functions need, can't be bothered with friends
121  public:
122 #endif
123  // check more things on the first call
124  bool first_call_;
125  // the stage of evaluation
126  internal::Stage cur_stage_;
127 
128  ModelObjectsTemp get_dependency_graph_inputs(const ModelObject *mo) const;
129  ModelObjectsTemp get_dependency_graph_outputs(const ModelObject *mo) const;
130  bool do_get_has_dependencies(const ModelObject *mo) const {
131  return no_dependencies_.find(mo) == no_dependencies_.end();
132  }
133  void do_set_has_dependencies(const ModelObject *mo, bool tf);
134  void do_set_has_all_dependencies(bool tf);
135 
136  void validate_computed_derivatives() const {}
137  void set_has_all_dependencies(bool tf);
138  bool get_has_all_dependencies() const;
139  void check_dependency_invariants() const;
140  void check_dependency_invariants(const ModelObject *mo) const;
141  ScoreStatesTemp get_ancestor_score_states(const ModelObject *mo) const;
142  ScoreStatesTemp get_descendent_score_states(const ModelObject *mo) const;
143 
144  void before_evaluate(const ScoreStatesTemp &states);
145  void after_evaluate(const ScoreStatesTemp &states, bool calc_derivs);
146 
147  internal::Stage get_stage() const { return cur_stage_; }
148  ParticleIndex add_particle_internal(Particle *p);
149  static void do_remove_score_state(ScoreState *obj);
150  void do_add_score_state(ScoreState *obj);
151  void do_remove_particle(ParticleIndex pi);
152  bool do_get_has_required_score_states(const ModelObject *mo) const;
153  void do_set_has_required_score_states(kernel::ModelObject *mo, bool tf);
154  const ScoreStatesTemp &do_get_required_score_states(
155  const ModelObject *mo) const {
156  IMP_USAGE_CHECK(do_get_has_required_score_states(mo),
157  "Doesn't have score states");
158  return required_score_states_.find(mo)->second;
159  }
160  void do_add_model_object(kernel::ModelObject *mo);
161  void do_remove_model_object(kernel::ModelObject *mo);
162 
163  public:
164  /** Construct an empty model */
165  Model(std::string name = "Model %1%");
166 
167  public:
168 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
169  IMP_MODEL_IMPORT(internal::FloatAttributeTable);
170  IMP_MODEL_IMPORT(internal::StringAttributeTable);
171  IMP_MODEL_IMPORT(internal::IntAttributeTable);
172  IMP_MODEL_IMPORT(internal::ObjectAttributeTable);
173  IMP_MODEL_IMPORT(internal::WeakObjectAttributeTable);
174  IMP_MODEL_IMPORT(internal::IntsAttributeTable);
175  IMP_MODEL_IMPORT(internal::ObjectsAttributeTable);
176  IMP_MODEL_IMPORT(internal::ParticleAttributeTable);
177  IMP_MODEL_IMPORT(internal::ParticlesAttributeTable);
178 #endif
179  /** Clear all the cache attributes of a given particle.*/
180  void clear_particle_caches(ParticleIndex pi);
181 
182  //! Add particle to the model
183  ParticleIndex add_particle(std::string name);
184 
185  //! Get the name of a particle
187  return get_particle(pi)->get_name();
188  }
189 
190  /** Add the passed Undecorator to the particle.*/
191  void add_undecorator(ParticleIndex pi, Undecorator *d);
192 
193  /** @name States
194 
195  ScoreStates can be added to the Model in order to keep them
196  alive as long as the model is alive. Being added does affect
197  their ability to perform their required action. See ScoreState
198  for more information.
199 
200  \advancedmethod
201  */
202  /**@{*/
203  IMP_LIST_ACTION(public, ScoreState, ScoreStates, score_state, score_states,
204  ScoreState *, ScoreStates, do_add_score_state(obj), {},
205  do_remove_score_state(obj));
206  /**@}*/
207 
208  public:
209 #ifndef SWIG
210  using Object::clear_caches;
211 #endif
212 
213  //! Sometimes it is useful to be able to make sure the model is up to date
214  /** This method updates all the state but does not necessarily compute the
215  score. Use this to make sure that your containers and rigid bodies are
216  up to date.
217  */
218  void update();
219 
220 #ifdef IMP_DOXYGEN
221  /** \name Accessing attributes
222  \anchor model_attributes
223  All the attribute data associated with each Particle is stored in the
224  Model. For each type of attribute, there are the methods detailed below
225  (where, eg, TypeKey is FloatKey or StringKey)
226  @{
227  */
228  /** \pre get_has_attribute(attribute_key, particle) is false*/
229  void add_attribute(TypeKey attribute_key, ParticleIndex particle, Type value);
230 
231  /** \pre get_has_attribute(attribute_key, particle) is true*/
232  void remove_attribute(TypeKey attribute_key, ParticleIndex particle);
233 
234  bool get_has_attribute(TypeKey attribute_key, ParticleIndex particle) const;
235 
236  /** \pre get_has_attribute(attribute_key, particle) is true*/
237  void set_attribute(TypeKey attribute_key, ParticleIndex particle, Type value);
238 
239  /** \pre get_has_attribute(attribute_key, particle) is true*/
240  Type get_attribute(TypeKey attribute_key, ParticleIndex particle);
241 
242  /** Cache attributes, unklike normal attributes, can be added during
243  evaluation. They are also cleared by the clear_cache_attributes() method.
244  Cache attributes should be used when one is adding data to a particle
245  to aid scoring (eg cache the rigid body collision acceleration structure).
246 
247  When some pertinent aspect of the particle changes, the clear method
248  should
249  be called (yes, this is a bit vague). Examples where it should be cleared
250  include changing the set of members of a core::RigidBody or their
251  coordinates, changing the members of an atom::Hierarchy.
252  */
253  void add_cache_attribute(TypeKey attribute_key, ParticleIndex particle,
254  Type value);
255 
256  //! Optimized attributes are the parameters of the model
257  /** They will be modified by the samplers and optimizers.
258  */
259  void set_is_optimized(TypeKey attribute_key, ParticleIndex particle,
260  bool true_or_false);
261 /** @} */
262 #endif
263 
264 #ifdef SWIG
265 #define IMP_MODEL_ATTRIBUTE_METHODS(Type, Value) \
266  void add_attribute(Type##Key attribute_key, ParticleIndex particle, \
267  Value value); \
268  void remove_attribute(Type##Key attribute_key, ParticleIndex particle); \
269  bool get_has_attribute(Type##Key attribute_key, \
270  ParticleIndex particle) const; \
271  void set_attribute(Type##Key attribute_key, ParticleIndex particle, \
272  Value value); \
273  Value get_attribute(Type##Key attribute_key, ParticleIndex particle); \
274  void add_cache_attribute(Type##Key attribute_key, ParticleIndex particle, \
275  Value value)
276 
277  IMP_MODEL_ATTRIBUTE_METHODS(Float, Float);
278  IMP_MODEL_ATTRIBUTE_METHODS(Int, Int);
279  IMP_MODEL_ATTRIBUTE_METHODS(Ints, Ints);
280  IMP_MODEL_ATTRIBUTE_METHODS(String, String);
281  IMP_MODEL_ATTRIBUTE_METHODS(ParticleIndexes, ParticleIndexes);
282  IMP_MODEL_ATTRIBUTE_METHODS(ParticleIndex, ParticleIndex);
283  IMP_MODEL_ATTRIBUTE_METHODS(Object, Object *);
284  IMP_MODEL_ATTRIBUTE_METHODS(WeakObject, Object *);
285  void set_is_optimized(FloatKey, ParticleIndex, bool);
286 #endif
287 
288  /** Get the particle from an index. */
289  Particle *get_particle(ParticleIndex p) const;
290 
291  /** Get the particle from an index. */
292  bool get_has_particle(ParticleIndex p) const;
293 
294  /** Get all particle indexes */
295  ParticleIndexes get_particle_indexes();
296 
297  /** Get all the ModelObjects associated with this Model.
298  */
299  ModelObjectsTemp get_model_objects() const;
300 
301  /** Remove a particle from the Model. The particle will then be inactive and
302  cannot be used for anything and all data stored in the particle is lost.
303  */
304  void remove_particle(ParticleIndex pi);
305 
306  /** \name Storing data in the model
307 
308  One can store data associated with the model. This is used, for example,
309  to keep a central ScoreState to normalize rigid body rotational variables.
310  @{ */
311  /** Store a piece of data in the model referenced by the key. */
312  void add_data(kernel::ModelKey mk, base::Object *o);
313  /** Get back some data stored in the model. */
314  base::Object *get_data(kernel::ModelKey mk) const;
315  /** Remove data stored in the model. */
316  void remove_data(kernel::ModelKey mk);
317  /** Check if the model has a certain piece of data attached. */
318  bool get_has_data(kernel::ModelKey mk) const;
319  /** @} */
320 
322 
323 // deprecated
324 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
325  struct NotNull {
326  bool operator()(const base::Pointer<Particle> &p) { return p; }
327  };
328  typedef boost::filter_iterator<
329  NotNull, base::Vector<base::Pointer<Particle> >::const_iterator>
330  ParticleIterator;
331 #endif
332 
333 // all deprecated but too used to add warnings about now
334 #if !defined(IMP_DOXYGEN)
335  ScoringFunction *create_model_scoring_function();
336  void add_restraint(Restraint *r);
337  void remove_restraint(Restraint *r);
340  virtual void do_destroy() IMP_OVERRIDE;
341 #endif
342 
343  /** \deprecated_at{2.1} Use a RestraintSet instead.*/
344  IMPKERNEL_DEPRECATED_METHOD_DECL(2.1)
345  unsigned int get_number_of_restraints() const;
346  /** \deprecated_at{2.1} Use a RestraintSet instead.*/
347  IMPKERNEL_DEPRECATED_METHOD_DECL(2.1)
348  Restraint *get_restraint(unsigned int i) const;
349  /** \deprecated_at{2.1} Use a ScoringFunction instead.*/
350  IMPKERNEL_DEPRECATED_METHOD_DECL(2.1)
351  double evaluate(bool tf, bool warn = true);
352  /** \deprecated_at{2.1} Use the ParticleIndex version.*/
353  IMPKERNEL_DEPRECATED_METHOD_DECL(2.1)
354  void remove_particle(Particle *p);
355  /** \deprecated_at{2.1} Use get_particle_indexes(). */
356  IMPKERNEL_DEPRECATED_METHOD_DECL(2.1)
357  unsigned int get_number_of_particles() const;
358  /** \deprecated_at{2.1} Use get_particle_indexes(). */
359  IMPKERNEL_DEPRECATED_METHOD_DECL(2.1)
360  ParticlesTemp get_particles() const;
361  /** \deprecated_at{2.1} Do not use. */
362  IMPKERNEL_DEPRECATED_METHOD_DECL(2.1)
363  ModelObjectsTemp get_optimized_particles() const;
364  /** \deprecated_at{2.1} Using a ScoringFunction instead. */
365  IMPKERNEL_DEPRECATED_METHOD_DECL(2.1)
366  RestraintSet *get_root_restraint_set();
367  /** \deprecated_at{2.1} Get the maximum directly from the restraint.*/
368  IMPKERNEL_DEPRECATED_METHOD_DECL(2.1)
369  double get_maximum_score(Restraint *r) const;
370  /** \deprecated_at{2.1} Set get the maximum directly on the restraint.*/
371  IMPKERNEL_DEPRECATED_METHOD_DECL(2.1)
372  void set_maximum_score(Restraint *r, double s);
373  /** \deprecated_at{2.1} You should use a ScoringFunction or a RestraintSet.*/
374  IMPKERNEL_DEPRECATED_METHOD_DECL(2.1)
375  void set_maximum_score(double s);
376  /** \deprecated_at{2.1} You should use a ScoringFunction or a RestraintSet.*/
377  IMPKERNEL_DEPRECATED_METHOD_DECL(2.1)
378  double get_maximum_score() const;
379 #ifndef SWIG
380  /** \deprecated_at{2.1} Use get_particle_indexes(). */
381  IMPKERNEL_DEPRECATED_METHOD_DECL(2.1)
382  ParticleIterator particles_begin() const;
383  /** \deprecated_at{2.1} Use get_particle_indexes(). */
384  IMPKERNEL_DEPRECATED_METHOD_DECL(2.1)
385  ParticleIterator particles_end() const;
386  /** \deprecated_at{2.1} Use a ScoringFunction instead. */
387  IMPKERNEL_DEPRECATED_METHOD_DECL(2.1)
388  operator Restraint *() const { return restraints_.get(); }
389 #endif
390 };
391 
392 IMPKERNEL_END_NAMESPACE
393 
394 #endif /* IMPKERNEL_DECLARE_MODEL_H */
Abstract base class for all restraints.
IMP::kernel::ModelObject ModelObject
The base class for decorators.
IMP::base::Vector< IMP::base::WeakPointer< kernel::ModelObject > > ModelObjectsTemp
Used to hold a set of related restraints.
Classes to handle individual model particles.
A nullptr-initialized pointer to an IMP Object.
A smart pointer to a ref-counted Object that is a class memeber.
Definition: base/Pointer.h:146
ParticlesTemp get_particles(kernel::Model *m, const ParticleIndexes &ps)
Basic types used by IMP.
IMP::base::Vector< IMP::base::WeakPointer< Restraint > > RestraintsTemp
Object used to hold a set of restraints.
A smart pointer to a reference counted object.
Definition: base/Pointer.h:87
kernel::RestraintsTemp get_restraints(const Subset &s, const ParticleStatesTable *pst, const DependencyGraph &dg, kernel::RestraintSet *rs)
ScoreStates maintian invariants in the Model.
ScoringFunction * create_scoring_function(RestraintType *rs, double weight=1.0, double max=NO_MAX, std::string name=std::string())
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
virtual void clear_caches()
Various general useful macros for IMP.
Single variable function.
IMP::kernel::Model Model
A restraint is a term in an IMP ScoringFunction.
Storage of a model, its restraints, constraints and particles.
Shared score state.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Class to handle individual model particles.
IMP::kernel::DependencyGraph DependencyGraph
Common base class for heavy weight IMP objects.
double Float
Basic floating-point value (could be float, double...)
Definition: base/types.h:20
virtual void do_destroy()
int Int
Basic integer value.
Definition: base/types.h:35
Macros to define containers of objects.
A shared base class to help in debugging and things.
void add_particle(RMF::FileHandle fh, kernel::Particle *hs)
void add_restraint(RMF::FileHandle fh, kernel::Restraint *hs)
std::string String
Basic string value.
Definition: base/types.h:44
Declare an efficient stl-compatible set.
Declare an efficient stl-compatible map.
Class for storing model, its restraints, constraints, and particles.
std::string get_particle_name(ParticleIndex pi)
Get the name of a particle.