9 #ifndef IMPKERNEL_MODEL_H
10 #define IMPKERNEL_MODEL_H
12 #include <IMP/kernel_config.h>
22 #include "internal/AttributeTable.h"
23 #include "internal/attribute_tables.h"
24 #include "internal/moved_particles_cache.h"
25 #include "internal/KeyVector.h"
28 #include <IMP/internal/IDGenerator.h>
29 #include <boost/unordered_map.hpp>
30 #include <boost/unordered_set.hpp>
32 #include <boost/iterator/transform_iterator.hpp>
33 #include <boost/iterator/filter_iterator.hpp>
34 #include <cereal/access.hpp>
35 #include <cereal/types/polymorphic.hpp>
39 IMPKERNEL_BEGIN_NAMESPACE
45 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
52 COMPUTING_DEPENDENCIES
59 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
61 inline std::ostream &operator<<(
62 std::ostream &out,
const std::set<ModelObject *> &) {
63 out <<
"(set of ModelObject)";
87 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
89 public internal::Masks,
92 public internal::FloatAttributeTable,
93 public internal::StringAttributeTable,
94 public internal::IntAttributeTable,
95 public internal::ObjectAttributeTable,
96 public internal::WeakObjectAttributeTable,
97 public internal::IntsAttributeTable,
98 public internal::FloatsAttributeTable,
99 public internal::ObjectsAttributeTable,
100 public internal::ParticleAttributeTable,
101 public internal::ParticlesAttributeTable
104 typedef std::set<ModelObject *> Edges;
107 IMP_NAMED_TUPLE_5(NodeInfo,
NodeInfos, Edges, inputs, Edges, input_outputs,
108 Edges, outputs, Edges, readers, Edges, writers, );
109 typedef boost::unordered_map<const ModelObject *, NodeInfo>
DependencyGraph;
110 DependencyGraph dependency_graph_;
111 boost::unordered_set<const ModelObject *> no_dependencies_;
112 boost::unordered_map<const ModelObject *, ScoreStatesTemp>
113 required_score_states_;
116 boost::unordered_map<FloatKey, FloatRange> ranges_;
122 internal::KeyVector<ModelKey, PointerMember<Object> > model_data_;
124 #if !defined(IMP_DOXYGEN)
127 std::map<uint32_t, Model*> map_;
128 internal::IDGenerator id_gen_;
131 uint32_t add_new_model(
Model *m);
132 void add_model_with_id(
Model *m, uint32_t
id);
133 void remove_model(
Model *m);
134 Model *
get(uint32_t id)
const;
137 static ModelMap model_map_;
142 void do_clear_required_score_states(
ModelObject *mo);
143 void do_check_required_score_states(
const ModelObject *mo)
const;
144 void do_check_update_order(
const ScoreState *ss)
const;
145 void do_check_inputs_and_outputs(
const ModelObject *mo)
const;
146 void do_check_readers_and_writers(
const ModelObject *mo)
const;
147 void do_check_not_in_readers_and_writers(
const ModelObject *mo)
const;
151 unsigned age_counter_;
155 unsigned dependencies_age_;
158 bool dependencies_saved_;
159 unsigned saved_dependencies_age_;
162 std::vector<ModelObject *> mos_added_since_save_, mos_removed_since_save_;
166 internal::MovedParticlesRestraintCache moved_particles_restraint_cache_;
168 internal::MovedParticlesParticleCache moved_particles_particle_cache_;
170 unsigned moved_particles_cache_age_;
172 void register_unique_id();
174 friend class cereal::access;
176 template<
class Archive>
void serialize(Archive &ar) {
177 ar(cereal::base_class<Object>(
this));
181 if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
182 register_unique_id();
184 ar(cereal::base_class<internal::FloatAttributeTable>(
this),
185 cereal::base_class<internal::StringAttributeTable>(
this),
186 cereal::base_class<internal::IntAttributeTable>(
this),
187 cereal::base_class<internal::ObjectAttributeTable>(
this),
188 cereal::base_class<internal::WeakObjectAttributeTable>(
this),
189 cereal::base_class<internal::IntsAttributeTable>(
this),
190 cereal::base_class<internal::FloatsAttributeTable>(
this),
191 cereal::base_class<internal::ObjectsAttributeTable>(
this),
192 cereal::base_class<internal::ParticleAttributeTable>(
this),
193 cereal::base_class<internal::ParticlesAttributeTable>(
this),
194 free_particles_, model_data_, mutable_access_score_states());
196 if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
198 free_particles_.clear();
200 particle_index_.clear();
208 for (
auto pi : to_free) {
212 size_t count = particle_index_.size();
214 for (
size_t i = 0; i < count; ++i) {
224 if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
227 trigger_age_.clear();
228 dependencies_age_ = 0;
229 saved_dependencies_age_ = 0;
230 dependencies_saved_ =
false;
231 moved_particles_cache_age_ = 0;
236 void increase_age() {
238 if (age_counter_ == 0) {
243 template <
class MOType,
class MOVector>
244 void do_get_dependent(
ModelObject *mo, MOVector &ret) {
245 const auto &node = dependency_graph_.find(mo);
247 "Object " << mo->get_name()
248 <<
" does not have dependencies.");
250 "Node not in dependency_graph.");
251 MOType *r =
dynamic_cast<MOType *
>(mo);
255 for (
ModelObject *cur : node->second.get_outputs()) {
256 do_get_dependent<MOType, MOVector>(cur, ret);
258 for (
ModelObject *cur : node->second.get_readers()) {
259 do_get_dependent<MOType, MOVector>(cur, ret);
263 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
270 internal::Stage cur_stage_;
273 const std::set<Restraint *> &get_dependent_restraints(
ParticleIndex pi) {
274 return moved_particles_restraint_cache_.get_dependent_restraints(pi);
279 return moved_particles_particle_cache_.get_dependent_particles(pi);
284 bool do_get_has_dependencies(
const ModelObject *mo)
const {
285 return no_dependencies_.find(mo) == no_dependencies_.end();
287 void do_set_has_dependencies(
const ModelObject *mo,
bool tf);
288 void do_set_has_all_dependencies(
bool tf);
290 void validate_computed_derivatives()
const {}
291 void set_has_all_dependencies(
bool tf);
292 bool get_has_all_dependencies()
const;
293 void check_dependency_invariants()
const;
294 void check_dependency_invariants(
const ModelObject *mo)
const;
301 internal::Stage get_stage()
const {
return cur_stage_; }
303 static void do_remove_score_state(
ScoreState *obj);
306 bool do_get_has_required_score_states(
const ModelObject *mo)
const;
307 void do_set_has_required_score_states(
ModelObject *mo,
bool tf);
311 "Doesn't have score states");
312 return required_score_states_.find(mo)->second;
319 Model(std::string name =
"Model %1%");
322 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
323 IMP_MODEL_IMPORT(internal::FloatAttributeTable);
324 IMP_MODEL_IMPORT(internal::StringAttributeTable);
325 IMP_MODEL_IMPORT(internal::IntAttributeTable);
326 IMP_MODEL_IMPORT(internal::ObjectAttributeTable);
327 IMP_MODEL_IMPORT(internal::WeakObjectAttributeTable);
328 IMP_MODEL_IMPORT(internal::IntsAttributeTable);
329 IMP_MODEL_IMPORT(internal::FloatsAttributeTable);
330 IMP_MODEL_IMPORT(internal::ObjectsAttributeTable);
331 IMP_MODEL_IMPORT(internal::ParticleAttributeTable);
332 IMP_MODEL_IMPORT(internal::ParticlesAttributeTable);
346 #if !defined(IMP_DOXYGEN)
368 do_remove_score_state(obj));
393 void add_attribute(TypeKey attribute_key,
ParticleIndex particle, Type value);
397 void remove_attribute(TypeKey attribute_key,
ParticleIndex particle);
400 bool get_has_attribute(TypeKey attribute_key,
ParticleIndex particle)
const;
404 void set_attribute(TypeKey attribute_key,
ParticleIndex particle, Type value);
408 Type get_attribute(TypeKey attribute_key,
ParticleIndex particle);
421 void add_cache_attribute(TypeKey attribute_key,
ParticleIndex particle,
426 void set_is_optimized(TypeKey attribute_key,
ParticleIndex particle,
432 #define IMP_MODEL_ATTRIBUTE_METHODS(Type, Value) \
433 void add_attribute(Type##Key attribute_key, ParticleIndex particle, \
435 void remove_attribute(Type##Key attribute_key, ParticleIndex particle); \
436 bool get_has_attribute(Type##Key attribute_key, \
437 ParticleIndex particle) const; \
438 void set_attribute(Type##Key attribute_key, ParticleIndex particle, \
440 Value get_attribute(Type##Key attribute_key, ParticleIndex particle); \
441 void add_cache_attribute(Type##Key attribute_key, ParticleIndex particle, \
445 IMP_MODEL_ATTRIBUTE_METHODS(
Int,
Int);
447 IMP_MODEL_ATTRIBUTE_METHODS(
Ints,
Ints);
452 IMP_MODEL_ATTRIBUTE_METHODS(WeakObject,
Object *);
461 return particle_index_[p];
466 if (particle_index_.size() <= get_as_unsigned_int(p))
return false;
467 return particle_index_[p];
494 bool get_has_data(
ModelKey mk)
const;
532 if (trigger_age_.size() > tk.get_index()) {
533 return trigger_age_[tk.get_index()];
541 if (tk.get_index() >= trigger_age_.size()) {
542 trigger_age_.resize(tk.get_index() + 1, 0);
544 trigger_age_[tk.get_index()] = age_counter_;
557 dependencies_saved_ =
true;
558 saved_dependencies_age_ = dependencies_age_;
560 mos_added_since_save_.clear();
561 mos_removed_since_save_.clear();
580 if (dependencies_saved_) {
581 dependencies_saved_ =
false;
582 dependencies_age_ = saved_dependencies_age_;
585 std::sort(mos_added_since_save_.begin(), mos_added_since_save_.end());
586 std::sort(mos_removed_since_save_.begin(),
587 mos_removed_since_save_.end());
589 "ModelObjects added do not match those removed");
610 return model_map_.get(
id);
616 #if !defined(IMP_DOXYGEN)
621 IMPKERNEL_END_NAMESPACE
623 CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES(
624 IMP::Model, cereal::specialization::member_serialize);
Particle * get_particle(ParticleIndex p) const
Get the particle from an index.
#define IMP_IF_CHECK(level)
Execute the code block if a certain level checks are on.
Used to hold a set of related restraints.
boost::graph DependencyGraph
Directed graph on the interactions between the various objects in the model.
The base class for undecorators.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
void restore_dependencies()
Restore ModelObject dependencies to previous restore point.
Index< ParticleIndexTag > ParticleIndex
void add_particle(RMF::FileHandle fh, Particle *hs)
Macros to help in defining tuple classes.
virtual void clear_caches()
unsigned get_dependencies_updated()
Get the model age when ModelObject dependencies were last changed, or 0.
A more IMP-like version of the std::vector.
unsigned get_particles_size() const
Get an upper bound on the number of particles in the Model.
unsigned get_age()
Get the current 'model time'.
bool get_has_particle(ParticleIndex p) const
Check whether a given particle index exists.
Macros to define containers of objects.
unsigned get_trigger_last_updated(TriggerKey tk)
Get the time when the given trigger was last updated, or 0.
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Class for storing model, its restraints, constraints, and particles.
Base class for objects in a Model that depend on other objects.
virtual void do_destroy()
Common base class for heavy weight IMP objects.
ParticleIndexes get_particle_indexes(ParticlesTemp const &particles)
ScoreStates maintain invariants in the Model.
static Model * get_by_unique_id(uint32_t id)
Return the Model with the given unique ID.
uint32_t get_unique_id() const
Get the unique ID of this Model.
Implements a vector tied to a particular index of type Index<Tag>.
Base class for objects in a Model that depend on other objects.
Classes to handle individual model particles. (Note that implementation of inline functions is in int...
A nullptr-initialized pointer to an IMP Object.
void save_dependencies()
Mark a 'restore point' for ModelObject dependencies.
A shared base class to help in debugging and things.
Represents a scoring function on the model.
double Float
Basic floating-point value (could be float, double...)
Class to handle individual particles of a Model object.
bool get_has_dependencies() const
Return whether this object has dependencies computed.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Abstract base class for all restraints.
int Int
Basic integer value.
void set_trigger_updated(TriggerKey tk)
Update the given trigger.
std::string String
Basic string value.
Class for adding derivatives from restraints to the model.