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::Vector3DAttributeTable,
100 public internal::ObjectsAttributeTable,
101 public internal::ParticleAttributeTable,
102 public internal::ParticlesAttributeTable,
103 public internal::SparseStringAttributeTable,
104 public internal::SparseIntAttributeTable,
105 public internal::SparseFloatAttributeTable,
106 public internal::SparseParticleAttributeTable
109 typedef std::set<ModelObject *> Edges;
112 IMP_NAMED_TUPLE_5(NodeInfo,
NodeInfos, Edges, inputs, Edges, input_outputs,
113 Edges, outputs, Edges, readers, Edges, writers, );
114 typedef boost::unordered_map<const ModelObject *, NodeInfo>
DependencyGraph;
115 DependencyGraph dependency_graph_;
116 boost::unordered_set<const ModelObject *> no_dependencies_;
117 boost::unordered_map<const ModelObject *, ScoreStatesTemp>
118 required_score_states_;
121 boost::unordered_map<FloatKey, FloatRange> ranges_;
127 internal::KeyVector<ModelKey, PointerMember<Object> > model_data_;
129 #if !defined(IMP_DOXYGEN)
132 std::map<uint32_t, Model*> map_;
133 internal::IDGenerator id_gen_;
136 uint32_t add_new_model(
Model *m);
137 void add_model_with_id(
Model *m, uint32_t
id);
138 void remove_model(
Model *m);
139 Model *
get(uint32_t id)
const;
142 static ModelMap model_map_;
147 void do_clear_required_score_states(
ModelObject *mo);
148 void do_check_required_score_states(
const ModelObject *mo)
const;
149 void do_check_update_order(
const ScoreState *ss)
const;
150 void do_check_inputs_and_outputs(
const ModelObject *mo)
const;
151 void do_check_readers_and_writers(
const ModelObject *mo)
const;
152 void do_check_not_in_readers_and_writers(
const ModelObject *mo)
const;
156 unsigned age_counter_;
160 unsigned dependencies_age_;
162 unsigned removed_particles_attributes_age_;
165 bool dependencies_saved_;
166 unsigned saved_dependencies_age_;
169 std::vector<ModelObject *> mos_added_since_save_, mos_removed_since_save_;
173 internal::MovedParticlesRestraintCache moved_particles_restraint_cache_;
175 internal::MovedParticlesParticleCache moved_particles_particle_cache_;
177 unsigned moved_particles_cache_age_;
179 void register_unique_id();
181 friend class cereal::access;
183 template<
class Archive>
void serialize(Archive &ar,
184 std::uint32_t
const version) {
185 ar(cereal::base_class<Object>(
this));
189 if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
190 register_unique_id();
192 ar(cereal::base_class<internal::FloatAttributeTable>(
this),
193 cereal::base_class<internal::StringAttributeTable>(
this),
194 cereal::base_class<internal::IntAttributeTable>(
this),
195 cereal::base_class<internal::IntsAttributeTable>(
this),
196 cereal::base_class<internal::FloatsAttributeTable>(
this),
197 cereal::base_class<internal::Vector3DAttributeTable>(
this),
198 cereal::base_class<internal::ParticleAttributeTable>(
this),
199 cereal::base_class<internal::ParticlesAttributeTable>(
this),
200 cereal::base_class<internal::SparseStringAttributeTable>(
this),
201 cereal::base_class<internal::SparseIntAttributeTable>(
this),
202 cereal::base_class<internal::SparseFloatAttributeTable>(
this),
203 cereal::base_class<internal::SparseParticleAttributeTable>(
this));
205 if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
207 free_particles_.clear();
209 particle_index_.clear();
217 for (
auto pi : to_free) {
221 size_t count = particle_index_.size();
223 for (
size_t i = 0; i < count; ++i) {
235 ar(cereal::base_class<internal::ObjectAttributeTable>(
this),
236 cereal::base_class<internal::ObjectsAttributeTable>(
this),
237 cereal::base_class<internal::WeakObjectAttributeTable>(
this),
238 model_data_, mutable_access_score_states());
240 if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
243 trigger_age_.clear();
244 dependencies_age_ = 0;
245 removed_particles_attributes_age_ = 0;
246 saved_dependencies_age_ = 0;
247 dependencies_saved_ =
false;
248 moved_particles_cache_age_ = 0;
253 void increase_age() {
255 if (age_counter_ == 0) {
260 template <
class MOType,
class MOVector>
261 void do_get_dependent(
ModelObject *mo, MOVector &ret) {
262 const auto &node = dependency_graph_.find(mo);
264 "Object " << mo->get_name()
265 <<
" does not have dependencies.");
267 "Node not in dependency_graph.");
268 MOType *r =
dynamic_cast<MOType *
>(mo);
272 for (
ModelObject *cur : node->second.get_outputs()) {
273 do_get_dependent<MOType, MOVector>(cur, ret);
275 for (
ModelObject *cur : node->second.get_readers()) {
276 do_get_dependent<MOType, MOVector>(cur, ret);
280 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
287 internal::Stage cur_stage_;
290 const std::set<Restraint *> &get_dependent_restraints(
ParticleIndex pi) {
291 return moved_particles_restraint_cache_.get_dependent_restraints(pi);
296 return moved_particles_particle_cache_.get_dependent_particles(pi);
301 bool do_get_has_dependencies(
const ModelObject *mo)
const {
302 return no_dependencies_.find(mo) == no_dependencies_.end();
304 void do_set_has_dependencies(
const ModelObject *mo,
bool tf);
305 void do_set_has_all_dependencies(
bool tf);
307 void validate_computed_derivatives()
const {}
308 void set_has_all_dependencies(
bool tf);
309 bool get_has_all_dependencies()
const;
310 void check_dependency_invariants()
const;
311 void check_dependency_invariants(
const ModelObject *mo)
const;
318 internal::Stage get_stage()
const {
return cur_stage_; }
320 static void do_remove_score_state(
ScoreState *obj);
323 bool do_get_has_required_score_states(
const ModelObject *mo)
const;
324 void do_set_has_required_score_states(
ModelObject *mo,
bool tf);
328 "Doesn't have score states");
329 return required_score_states_.find(mo)->second;
336 Model(std::string name =
"Model %1%");
339 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
340 IMP_MODEL_IMPORT(internal::FloatAttributeTable);
341 IMP_MODEL_IMPORT(internal::StringAttributeTable);
342 IMP_MODEL_IMPORT(internal::IntAttributeTable);
343 IMP_MODEL_IMPORT(internal::ObjectAttributeTable);
344 IMP_MODEL_IMPORT(internal::WeakObjectAttributeTable);
345 IMP_MODEL_IMPORT(internal::IntsAttributeTable);
346 IMP_MODEL_IMPORT(internal::FloatsAttributeTable);
347 IMP_MODEL_IMPORT(internal::Vector3DAttributeTable);
348 IMP_MODEL_IMPORT(internal::ObjectsAttributeTable);
349 IMP_MODEL_IMPORT(internal::ParticleAttributeTable);
350 IMP_MODEL_IMPORT(internal::ParticlesAttributeTable);
351 IMP_MODEL_SPARSE_IMPORT(internal::SparseStringAttributeTable);
352 IMP_MODEL_SPARSE_IMPORT(internal::SparseIntAttributeTable);
353 IMP_MODEL_SPARSE_IMPORT(internal::SparseFloatAttributeTable);
354 IMP_MODEL_SPARSE_IMPORT(internal::SparseParticleAttributeTable);
368 #if !defined(IMP_DOXYGEN)
390 do_remove_score_state(obj));
415 void add_attribute(TypeKey attribute_key,
ParticleIndex particle, Type value);
419 void remove_attribute(TypeKey attribute_key,
ParticleIndex particle);
422 bool get_has_attribute(TypeKey attribute_key,
ParticleIndex particle)
const;
426 void set_attribute(TypeKey attribute_key,
ParticleIndex particle, Type value);
430 Type get_attribute(TypeKey attribute_key,
ParticleIndex particle);
443 void add_cache_attribute(TypeKey attribute_key,
ParticleIndex particle,
448 void set_is_optimized(TypeKey attribute_key,
ParticleIndex particle,
454 #define IMP_MODEL_ATTRIBUTE_METHODS(Type, Value) \
455 void add_attribute(Type##Key attribute_key, ParticleIndex particle, \
457 void remove_attribute(Type##Key attribute_key, ParticleIndex particle); \
458 bool get_has_attribute(Type##Key attribute_key, \
459 ParticleIndex particle) const; \
460 void set_attribute(Type##Key attribute_key, ParticleIndex particle, \
462 Value get_attribute(Type##Key attribute_key, ParticleIndex particle); \
463 void add_cache_attribute(Type##Key attribute_key, ParticleIndex particle, \
466 #define IMP_MODEL_SPARSE_ATTRIBUTE_METHODS(Type, Value) \
467 void add_attribute(Type##Key attribute_key, ParticleIndex particle, \
469 void remove_attribute(Type##Key attribute_key, ParticleIndex particle); \
470 bool get_has_attribute(Type##Key attribute_key, \
471 ParticleIndex particle) const; \
472 void set_attribute(Type##Key attribute_key, ParticleIndex particle, \
474 Value get_attribute(Type##Key attribute_key, ParticleIndex particle)
477 IMP_MODEL_ATTRIBUTE_METHODS(
Int,
Int);
480 IMP_MODEL_ATTRIBUTE_METHODS(
Ints,
Ints);
485 IMP_MODEL_ATTRIBUTE_METHODS(WeakObject,
Object *);
486 IMP_MODEL_SPARSE_ATTRIBUTE_METHODS(SparseString,
String);
487 IMP_MODEL_SPARSE_ATTRIBUTE_METHODS(SparseInt,
Int);
488 IMP_MODEL_SPARSE_ATTRIBUTE_METHODS(SparseFloat,
Float);
489 IMP_MODEL_SPARSE_ATTRIBUTE_METHODS(SparseParticleIndex,
ParticleIndex);
498 return particle_index_[p];
503 if (particle_index_.size() <= get_as_unsigned_int(p))
return false;
504 return particle_index_[p];
531 bool get_has_data(
ModelKey mk)
const;
569 if (trigger_age_.size() > tk.get_index()) {
570 return trigger_age_[tk.get_index()];
578 if (tk.get_index() >= trigger_age_.size()) {
579 trigger_age_.resize(tk.get_index() + 1, 0);
581 trigger_age_[tk.get_index()] = age_counter_;
596 return removed_particles_attributes_age_;
602 dependencies_saved_ =
true;
603 saved_dependencies_age_ = dependencies_age_;
605 mos_added_since_save_.clear();
606 mos_removed_since_save_.clear();
625 if (dependencies_saved_) {
626 dependencies_saved_ =
false;
627 dependencies_age_ = saved_dependencies_age_;
630 std::sort(mos_added_since_save_.begin(), mos_added_since_save_.end());
631 std::sort(mos_removed_since_save_.begin(),
632 mos_removed_since_save_.end());
634 "ModelObjects added do not match those removed");
655 return model_map_.get(
id);
661 #if !defined(IMP_DOXYGEN)
666 IMPKERNEL_END_NAMESPACE
668 CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES(
669 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>.
unsigned get_removed_particles_attributes_age()
Get the model age when particles or attributes were last removed, or 0.
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.