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 std::uint32_t
const version) {
178 ar(cereal::base_class<Object>(
this));
182 if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
183 register_unique_id();
185 ar(cereal::base_class<internal::FloatAttributeTable>(
this),
186 cereal::base_class<internal::StringAttributeTable>(
this),
187 cereal::base_class<internal::IntAttributeTable>(
this),
188 cereal::base_class<internal::IntsAttributeTable>(
this),
189 cereal::base_class<internal::FloatsAttributeTable>(
this),
190 cereal::base_class<internal::ParticleAttributeTable>(
this),
191 cereal::base_class<internal::ParticlesAttributeTable>(
this));
193 if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
195 free_particles_.clear();
197 particle_index_.clear();
205 for (
auto pi : to_free) {
209 size_t count = particle_index_.size();
211 for (
size_t i = 0; i < count; ++i) {
223 ar(cereal::base_class<internal::ObjectAttributeTable>(
this),
224 cereal::base_class<internal::ObjectsAttributeTable>(
this),
225 cereal::base_class<internal::WeakObjectAttributeTable>(
this),
226 model_data_, mutable_access_score_states());
228 if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
231 trigger_age_.clear();
232 dependencies_age_ = 0;
233 saved_dependencies_age_ = 0;
234 dependencies_saved_ =
false;
235 moved_particles_cache_age_ = 0;
240 void increase_age() {
242 if (age_counter_ == 0) {
247 template <
class MOType,
class MOVector>
248 void do_get_dependent(
ModelObject *mo, MOVector &ret) {
249 const auto &node = dependency_graph_.find(mo);
251 "Object " << mo->get_name()
252 <<
" does not have dependencies.");
254 "Node not in dependency_graph.");
255 MOType *r =
dynamic_cast<MOType *
>(mo);
259 for (
ModelObject *cur : node->second.get_outputs()) {
260 do_get_dependent<MOType, MOVector>(cur, ret);
262 for (
ModelObject *cur : node->second.get_readers()) {
263 do_get_dependent<MOType, MOVector>(cur, ret);
267 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
274 internal::Stage cur_stage_;
277 const std::set<Restraint *> &get_dependent_restraints(
ParticleIndex pi) {
278 return moved_particles_restraint_cache_.get_dependent_restraints(pi);
283 return moved_particles_particle_cache_.get_dependent_particles(pi);
288 bool do_get_has_dependencies(
const ModelObject *mo)
const {
289 return no_dependencies_.find(mo) == no_dependencies_.end();
291 void do_set_has_dependencies(
const ModelObject *mo,
bool tf);
292 void do_set_has_all_dependencies(
bool tf);
294 void validate_computed_derivatives()
const {}
295 void set_has_all_dependencies(
bool tf);
296 bool get_has_all_dependencies()
const;
297 void check_dependency_invariants()
const;
298 void check_dependency_invariants(
const ModelObject *mo)
const;
305 internal::Stage get_stage()
const {
return cur_stage_; }
307 static void do_remove_score_state(
ScoreState *obj);
310 bool do_get_has_required_score_states(
const ModelObject *mo)
const;
311 void do_set_has_required_score_states(
ModelObject *mo,
bool tf);
315 "Doesn't have score states");
316 return required_score_states_.find(mo)->second;
323 Model(std::string name =
"Model %1%");
326 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
327 IMP_MODEL_IMPORT(internal::FloatAttributeTable);
328 IMP_MODEL_IMPORT(internal::StringAttributeTable);
329 IMP_MODEL_IMPORT(internal::IntAttributeTable);
330 IMP_MODEL_IMPORT(internal::ObjectAttributeTable);
331 IMP_MODEL_IMPORT(internal::WeakObjectAttributeTable);
332 IMP_MODEL_IMPORT(internal::IntsAttributeTable);
333 IMP_MODEL_IMPORT(internal::FloatsAttributeTable);
334 IMP_MODEL_IMPORT(internal::ObjectsAttributeTable);
335 IMP_MODEL_IMPORT(internal::ParticleAttributeTable);
336 IMP_MODEL_IMPORT(internal::ParticlesAttributeTable);
350 #if !defined(IMP_DOXYGEN)
372 do_remove_score_state(obj));
397 void add_attribute(TypeKey attribute_key,
ParticleIndex particle, Type value);
401 void remove_attribute(TypeKey attribute_key,
ParticleIndex particle);
404 bool get_has_attribute(TypeKey attribute_key,
ParticleIndex particle)
const;
408 void set_attribute(TypeKey attribute_key,
ParticleIndex particle, Type value);
412 Type get_attribute(TypeKey attribute_key,
ParticleIndex particle);
425 void add_cache_attribute(TypeKey attribute_key,
ParticleIndex particle,
430 void set_is_optimized(TypeKey attribute_key,
ParticleIndex particle,
436 #define IMP_MODEL_ATTRIBUTE_METHODS(Type, Value) \
437 void add_attribute(Type##Key attribute_key, ParticleIndex particle, \
439 void remove_attribute(Type##Key attribute_key, ParticleIndex particle); \
440 bool get_has_attribute(Type##Key attribute_key, \
441 ParticleIndex particle) const; \
442 void set_attribute(Type##Key attribute_key, ParticleIndex particle, \
444 Value get_attribute(Type##Key attribute_key, ParticleIndex particle); \
445 void add_cache_attribute(Type##Key attribute_key, ParticleIndex particle, \
449 IMP_MODEL_ATTRIBUTE_METHODS(
Int,
Int);
451 IMP_MODEL_ATTRIBUTE_METHODS(
Ints,
Ints);
456 IMP_MODEL_ATTRIBUTE_METHODS(WeakObject,
Object *);
465 return particle_index_[p];
470 if (particle_index_.size() <= get_as_unsigned_int(p))
return false;
471 return particle_index_[p];
498 bool get_has_data(
ModelKey mk)
const;
536 if (trigger_age_.size() > tk.get_index()) {
537 return trigger_age_[tk.get_index()];
545 if (tk.get_index() >= trigger_age_.size()) {
546 trigger_age_.resize(tk.get_index() + 1, 0);
548 trigger_age_[tk.get_index()] = age_counter_;
561 dependencies_saved_ =
true;
562 saved_dependencies_age_ = dependencies_age_;
564 mos_added_since_save_.clear();
565 mos_removed_since_save_.clear();
584 if (dependencies_saved_) {
585 dependencies_saved_ =
false;
586 dependencies_age_ = saved_dependencies_age_;
589 std::sort(mos_added_since_save_.begin(), mos_added_since_save_.end());
590 std::sort(mos_removed_since_save_.begin(),
591 mos_removed_since_save_.end());
593 "ModelObjects added do not match those removed");
614 return model_map_.get(
id);
620 #if !defined(IMP_DOXYGEN)
625 IMPKERNEL_END_NAMESPACE
627 CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES(
628 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.