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,
102 public internal::SparseStringAttributeTable,
103 public internal::SparseIntAttributeTable,
104 public internal::SparseFloatAttributeTable,
105 public internal::SparseParticleAttributeTable
108 typedef std::set<ModelObject *> Edges;
111 IMP_NAMED_TUPLE_5(NodeInfo,
NodeInfos, Edges, inputs, Edges, input_outputs,
112 Edges, outputs, Edges, readers, Edges, writers, );
113 typedef boost::unordered_map<const ModelObject *, NodeInfo>
DependencyGraph;
114 DependencyGraph dependency_graph_;
115 boost::unordered_set<const ModelObject *> no_dependencies_;
116 boost::unordered_map<const ModelObject *, ScoreStatesTemp>
117 required_score_states_;
120 boost::unordered_map<FloatKey, FloatRange> ranges_;
126 internal::KeyVector<ModelKey, PointerMember<Object> > model_data_;
128 #if !defined(IMP_DOXYGEN)
131 std::map<uint32_t, Model*> map_;
132 internal::IDGenerator id_gen_;
135 uint32_t add_new_model(
Model *m);
136 void add_model_with_id(
Model *m, uint32_t
id);
137 void remove_model(
Model *m);
138 Model *
get(uint32_t id)
const;
141 static ModelMap model_map_;
146 void do_clear_required_score_states(
ModelObject *mo);
147 void do_check_required_score_states(
const ModelObject *mo)
const;
148 void do_check_update_order(
const ScoreState *ss)
const;
149 void do_check_inputs_and_outputs(
const ModelObject *mo)
const;
150 void do_check_readers_and_writers(
const ModelObject *mo)
const;
151 void do_check_not_in_readers_and_writers(
const ModelObject *mo)
const;
155 unsigned age_counter_;
159 unsigned dependencies_age_;
162 bool dependencies_saved_;
163 unsigned saved_dependencies_age_;
166 std::vector<ModelObject *> mos_added_since_save_, mos_removed_since_save_;
170 internal::MovedParticlesRestraintCache moved_particles_restraint_cache_;
172 internal::MovedParticlesParticleCache moved_particles_particle_cache_;
174 unsigned moved_particles_cache_age_;
176 void register_unique_id();
178 friend class cereal::access;
180 template<
class Archive>
void serialize(Archive &ar,
181 std::uint32_t
const version) {
182 ar(cereal::base_class<Object>(
this));
186 if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
187 register_unique_id();
189 ar(cereal::base_class<internal::FloatAttributeTable>(
this),
190 cereal::base_class<internal::StringAttributeTable>(
this),
191 cereal::base_class<internal::IntAttributeTable>(
this),
192 cereal::base_class<internal::IntsAttributeTable>(
this),
193 cereal::base_class<internal::FloatsAttributeTable>(
this),
194 cereal::base_class<internal::ParticleAttributeTable>(
this),
195 cereal::base_class<internal::ParticlesAttributeTable>(
this),
196 cereal::base_class<internal::SparseStringAttributeTable>(
this),
197 cereal::base_class<internal::SparseIntAttributeTable>(
this),
198 cereal::base_class<internal::SparseFloatAttributeTable>(
this),
199 cereal::base_class<internal::SparseParticleAttributeTable>(
this));
201 if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
203 free_particles_.clear();
205 particle_index_.clear();
213 for (
auto pi : to_free) {
217 size_t count = particle_index_.size();
219 for (
size_t i = 0; i < count; ++i) {
231 ar(cereal::base_class<internal::ObjectAttributeTable>(
this),
232 cereal::base_class<internal::ObjectsAttributeTable>(
this),
233 cereal::base_class<internal::WeakObjectAttributeTable>(
this),
234 model_data_, mutable_access_score_states());
236 if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
239 trigger_age_.clear();
240 dependencies_age_ = 0;
241 saved_dependencies_age_ = 0;
242 dependencies_saved_ =
false;
243 moved_particles_cache_age_ = 0;
248 void increase_age() {
250 if (age_counter_ == 0) {
255 template <
class MOType,
class MOVector>
256 void do_get_dependent(
ModelObject *mo, MOVector &ret) {
257 const auto &node = dependency_graph_.find(mo);
259 "Object " << mo->get_name()
260 <<
" does not have dependencies.");
262 "Node not in dependency_graph.");
263 MOType *r =
dynamic_cast<MOType *
>(mo);
267 for (
ModelObject *cur : node->second.get_outputs()) {
268 do_get_dependent<MOType, MOVector>(cur, ret);
270 for (
ModelObject *cur : node->second.get_readers()) {
271 do_get_dependent<MOType, MOVector>(cur, ret);
275 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
282 internal::Stage cur_stage_;
285 const std::set<Restraint *> &get_dependent_restraints(
ParticleIndex pi) {
286 return moved_particles_restraint_cache_.get_dependent_restraints(pi);
291 return moved_particles_particle_cache_.get_dependent_particles(pi);
296 bool do_get_has_dependencies(
const ModelObject *mo)
const {
297 return no_dependencies_.find(mo) == no_dependencies_.end();
299 void do_set_has_dependencies(
const ModelObject *mo,
bool tf);
300 void do_set_has_all_dependencies(
bool tf);
302 void validate_computed_derivatives()
const {}
303 void set_has_all_dependencies(
bool tf);
304 bool get_has_all_dependencies()
const;
305 void check_dependency_invariants()
const;
306 void check_dependency_invariants(
const ModelObject *mo)
const;
313 internal::Stage get_stage()
const {
return cur_stage_; }
315 static void do_remove_score_state(
ScoreState *obj);
318 bool do_get_has_required_score_states(
const ModelObject *mo)
const;
319 void do_set_has_required_score_states(
ModelObject *mo,
bool tf);
323 "Doesn't have score states");
324 return required_score_states_.find(mo)->second;
331 Model(std::string name =
"Model %1%");
334 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
335 IMP_MODEL_IMPORT(internal::FloatAttributeTable);
336 IMP_MODEL_IMPORT(internal::StringAttributeTable);
337 IMP_MODEL_IMPORT(internal::IntAttributeTable);
338 IMP_MODEL_IMPORT(internal::ObjectAttributeTable);
339 IMP_MODEL_IMPORT(internal::WeakObjectAttributeTable);
340 IMP_MODEL_IMPORT(internal::IntsAttributeTable);
341 IMP_MODEL_IMPORT(internal::FloatsAttributeTable);
342 IMP_MODEL_IMPORT(internal::ObjectsAttributeTable);
343 IMP_MODEL_IMPORT(internal::ParticleAttributeTable);
344 IMP_MODEL_IMPORT(internal::ParticlesAttributeTable);
345 IMP_MODEL_SPARSE_IMPORT(internal::SparseStringAttributeTable);
346 IMP_MODEL_SPARSE_IMPORT(internal::SparseIntAttributeTable);
347 IMP_MODEL_SPARSE_IMPORT(internal::SparseFloatAttributeTable);
348 IMP_MODEL_SPARSE_IMPORT(internal::SparseParticleAttributeTable);
362 #if !defined(IMP_DOXYGEN)
384 do_remove_score_state(obj));
409 void add_attribute(TypeKey attribute_key,
ParticleIndex particle, Type value);
413 void remove_attribute(TypeKey attribute_key,
ParticleIndex particle);
416 bool get_has_attribute(TypeKey attribute_key,
ParticleIndex particle)
const;
420 void set_attribute(TypeKey attribute_key,
ParticleIndex particle, Type value);
424 Type get_attribute(TypeKey attribute_key,
ParticleIndex particle);
437 void add_cache_attribute(TypeKey attribute_key,
ParticleIndex particle,
442 void set_is_optimized(TypeKey attribute_key,
ParticleIndex particle,
448 #define IMP_MODEL_ATTRIBUTE_METHODS(Type, Value) \
449 void add_attribute(Type##Key attribute_key, ParticleIndex particle, \
451 void remove_attribute(Type##Key attribute_key, ParticleIndex particle); \
452 bool get_has_attribute(Type##Key attribute_key, \
453 ParticleIndex particle) const; \
454 void set_attribute(Type##Key attribute_key, ParticleIndex particle, \
456 Value get_attribute(Type##Key attribute_key, ParticleIndex particle); \
457 void add_cache_attribute(Type##Key attribute_key, ParticleIndex particle, \
460 #define IMP_MODEL_SPARSE_ATTRIBUTE_METHODS(Type, Value) \
461 void add_attribute(Type##Key attribute_key, ParticleIndex particle, \
463 void remove_attribute(Type##Key attribute_key, ParticleIndex particle); \
464 bool get_has_attribute(Type##Key attribute_key, \
465 ParticleIndex particle) const; \
466 void set_attribute(Type##Key attribute_key, ParticleIndex particle, \
468 Value get_attribute(Type##Key attribute_key, ParticleIndex particle)
471 IMP_MODEL_ATTRIBUTE_METHODS(
Int,
Int);
473 IMP_MODEL_ATTRIBUTE_METHODS(
Ints,
Ints);
478 IMP_MODEL_ATTRIBUTE_METHODS(WeakObject,
Object *);
479 IMP_MODEL_SPARSE_ATTRIBUTE_METHODS(SparseString,
String);
480 IMP_MODEL_SPARSE_ATTRIBUTE_METHODS(SparseInt,
Int);
481 IMP_MODEL_SPARSE_ATTRIBUTE_METHODS(SparseFloat,
Float);
482 IMP_MODEL_SPARSE_ATTRIBUTE_METHODS(SparseParticleIndex,
ParticleIndex);
491 return particle_index_[p];
496 if (particle_index_.size() <= get_as_unsigned_int(p))
return false;
497 return particle_index_[p];
524 bool get_has_data(
ModelKey mk)
const;
562 if (trigger_age_.size() > tk.get_index()) {
563 return trigger_age_[tk.get_index()];
571 if (tk.get_index() >= trigger_age_.size()) {
572 trigger_age_.resize(tk.get_index() + 1, 0);
574 trigger_age_[tk.get_index()] = age_counter_;
587 dependencies_saved_ =
true;
588 saved_dependencies_age_ = dependencies_age_;
590 mos_added_since_save_.clear();
591 mos_removed_since_save_.clear();
610 if (dependencies_saved_) {
611 dependencies_saved_ =
false;
612 dependencies_age_ = saved_dependencies_age_;
615 std::sort(mos_added_since_save_.begin(), mos_added_since_save_.end());
616 std::sort(mos_removed_since_save_.begin(),
617 mos_removed_since_save_.end());
619 "ModelObjects added do not match those removed");
640 return model_map_.get(
id);
646 #if !defined(IMP_DOXYGEN)
651 IMPKERNEL_END_NAMESPACE
653 CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES(
654 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.