IMP  2.4.0
The Integrative Modeling Platform
kernel/Particle.h
Go to the documentation of this file.
1 /**
2  * \file IMP/kernel/Particle.h
3  * \brief Classes to handle individual model particles.
4  * (Note that implementation of inline functions in in internal)
5  *
6  * Copyright 2007-2015 IMP Inventors. All rights reserved.
7  *
8  */
9 
10 #ifndef IMPKERNEL_PARTICLE_H
11 #define IMPKERNEL_PARTICLE_H
12 
13 #include <IMP/kernel/kernel_config.h>
14 #include "base_types.h"
15 #include "DerivativeAccumulator.h"
16 #include "Model.h"
17 #include "ModelObject.h"
18 #include "particle_index.h"
19 #include "Key.h"
20 #include "internal/AttributeTable.h"
21 #include <IMP/base/Object.h>
22 #include <IMP/base/utility.h>
23 #include <IMP/base/Pointer.h>
24 #include <IMP/base/check_macros.h>
25 #include <utility>
26 
27 IMPKERNEL_BEGIN_NAMESPACE
28 
29 //class Model;
30 class Changed;
31 class SaveOptimizeds;
32 
33 //! Class to handle individual model particles.
34 /** At this point a Particle should only be considered as a placeholder for the
35  ParticleIndex, accessed through the get_index() method.
36 */
37 class IMPKERNELEXPORT Particle : public ModelObject {
38  private:
39 // doxygen produces funny docs for these things
40 #ifndef IMP_DOXYGEN
41  friend class Model;
42 #endif
43  ParticleIndex id_;
45 
46  public:
47  //! Construct a particle and add it to the Model
48  Particle(kernel::Model *m, std::string name);
49 
50  //! Construct a particle and add it to the Model
52 
53 #ifndef IMP_DOXYGEN
54 
55 #define IMP_KERNEL_PARTICLE_ATTRIBUTE_TYPE_DECL(UCName, lcname, Value) \
56  inline void add_attribute(UCName##Key name, Value initial_value); \
57  inline void remove_attribute(UCName##Key name); \
58  inline bool has_attribute(UCName##Key name) const; \
59  inline Value get_value(UCName##Key name) const; \
60  inline void set_value(UCName##Key name, Value value); \
61  inline void add_cache_attribute(UCName##Key name, Value value); \
62  inline UCName##Keys get_##lcname##_keys() const
63 
64  IMP_KERNEL_PARTICLE_ATTRIBUTE_TYPE_DECL(Float, float, Float);
65  IMP_KERNEL_PARTICLE_ATTRIBUTE_TYPE_DECL(Int, int, Int);
66  IMP_KERNEL_PARTICLE_ATTRIBUTE_TYPE_DECL(String, string, String);
67  IMP_KERNEL_PARTICLE_ATTRIBUTE_TYPE_DECL(Object, object, Object *);
68  IMP_KERNEL_PARTICLE_ATTRIBUTE_TYPE_DECL(WeakObject, weak_object, Object *);
69 
70  /** @name Float Attributes
71  Float attributes can be optimized, meaning the optimizer is
72  allowed to change their value in order to improve the score.
73  As a result, there are a number of extra methods to manipulate
74  them.
75 
76  All distances are assumed to be in angstroms
77  and derivatives in kcal/mol angstrom. This is not enforced.
78  */
79  /*@{*/
80  /** add attribute name to the attributes table of this particle
81 
82  @param name attribute key
83  @param inital_value initial value for the attribute
84  @param optimized whether to flag this attribute as optimized
85  */
86  void add_attribute(FloatKey name, const Float initial_value, bool optimized);
87 
88  /** adds a derivative value to the derivatives table of this particle
89 
90  @param key the attribute key whose derivative is updates
91  @param value the derivative value to be added
92  @param da a derivative accumulator for reweighting derivatives
93  */
94  void add_to_derivative(FloatKey key, Float value,
95  const DerivativeAccumulator &da);
96 
97  void set_is_optimized(FloatKey k, bool tf);
98 
99  //! returns true if key k is marked by model as optimized
100  inline bool get_is_optimized(FloatKey k) const;
101 
102  //! get the particle derivative
103  inline Float get_derivative(FloatKey name) const;
104  /** @} */
105 
106  /** \name Particle attributes
107  @{
108  */
109  void add_attribute(ParticleIndexKey k, Particle *v);
110  bool has_attribute(ParticleIndexKey k);
111  void set_value(ParticleIndexKey k, Particle *v);
112  inline Particle *get_value(ParticleIndexKey k) const;
113  void remove_attribute(ParticleIndexKey k);
114  ParticleIndexKeys get_particle_keys() const;
115  /** @} */
116 
117  //! Print out all the attributes
118  void show(std::ostream &out = std::cout) const;
119 
120  //! Get whether the particle is active.
121  /** Restraints referencing the particle are only evaluated for 'active'
122  particles.
123  \return true it the particle is active.
124  */
125  bool get_is_active() const;
126 #endif
127 
128  //! returns the particle index of this particle in its model
129  ParticleIndex get_index() const;
130 
131 #if !defined(IMP_DOXYGEN)
132  void clear_caches();
133 #endif
134  protected:
136  return ModelObjectsTemp();
137  }
139  return ModelObjectsTemp();
140  }
141 };
142 
143 // for swig
144 class Decorator;
145 
146 /** Take Decorator or Particle. */
147 class IMPKERNELEXPORT ParticleAdaptor : public base::InputAdaptor {
148  Model *m_;
149  ParticleIndex pi_;
150 
151  public:
152  ParticleAdaptor() : m_(nullptr), pi_() {}
153  ParticleAdaptor(Particle *p) : m_(p->get_model()), pi_(p->get_index()) {}
154  ParticleAdaptor(const Decorator &d);
155 #ifndef SWIG
157  : m_(p->get_model()), pi_(p->get_index()) {}
159  : m_(p->get_model()), pi_(p->get_index()) {}
161  : m_(p->get_model()), pi_(p->get_index()) {}
162 #endif
163  Model *get_model() const { return m_; }
164  ParticleIndex get_particle_index() const { return pi_; }
165 };
166 
167 
168 /****************** Inline methods ***************/
169 
170 #ifndef IMP_DOXYGEN
171 
172 bool Particle::get_is_optimized(FloatKey k) const {
173  IMP_USAGE_CHECK(get_is_active(), "Inactive particle used.");
174  return get_model()->get_is_optimized(k, id_);
175 }
176 
177 Float Particle::get_derivative(FloatKey name) const {
178  IMP_USAGE_CHECK(get_is_active(), "Inactive particle used.");
179  return get_model()->get_derivative(name, id_);
180 }
181 
182 Particle *Particle::get_value(ParticleIndexKey k) const {
183  IMP_USAGE_CHECK(get_is_active(), "Inactive particle used.");
184  return get_model()->get_particle(get_model()->get_attribute(k, id_));
185 }
186 
187 #define IMP_PARTICLE_ATTRIBUTE_TYPE_DEF(UCName, lcname, Value) \
188  void Particle::add_attribute(UCName##Key name, Value initial_value) { \
189  IMP_USAGE_CHECK(get_is_active(), "Inactive particle used."); \
190  get_model()->add_attribute(name, id_, initial_value); \
191  } \
192  void Particle::remove_attribute(UCName##Key name) { \
193  IMP_USAGE_CHECK(get_is_active(), "Inactive particle used."); \
194  get_model()->remove_attribute(name, id_); \
195  } \
196  bool Particle::has_attribute(UCName##Key name) const { \
197  IMP_USAGE_CHECK(get_is_active(), "Inactive particle used."); \
198  return get_model()->get_has_attribute(name, id_); \
199  } \
200  Value Particle::get_value(UCName##Key name) const { \
201  IMP_USAGE_CHECK(get_is_active(), "Inactive particle used."); \
202  return get_model()->get_attribute(name, id_); \
203  } \
204  void Particle::set_value(UCName##Key name, Value value) { \
205  IMP_USAGE_CHECK(get_is_active(), "Inactive particle used."); \
206  get_model()->set_attribute(name, id_, value); \
207  } \
208  UCName##Keys Particle::get_##lcname##_keys() const { \
209  IMP_USAGE_CHECK(get_is_active(), "Inactive particle used."); \
210  return get_model()->internal::UCName##AttributeTable::get_attribute_keys( \
211  id_); \
212  } \
213  void Particle::add_cache_attribute(UCName##Key name, Value value) { \
214  IMP_USAGE_CHECK(get_is_active(), "Inactive particle used."); \
215  return get_model()->add_cache_attribute(name, id_, value); \
216  }
217 
218 IMP_PARTICLE_ATTRIBUTE_TYPE_DEF(Float, float, Float);
219 IMP_PARTICLE_ATTRIBUTE_TYPE_DEF(Int, int, Int);
220 IMP_PARTICLE_ATTRIBUTE_TYPE_DEF(String, string, String);
221 IMP_PARTICLE_ATTRIBUTE_TYPE_DEF(Object, object, base::Object *);
222 IMP_PARTICLE_ATTRIBUTE_TYPE_DEF(WeakObject, weak_object, base::Object *);
223 
224 #endif // DOXYGEN
225 
226 IMPKERNEL_END_NAMESPACE
227 #endif /* IMPKERNEL_PARTICLE_H */
Various general useful functions for IMP.
Class for adding derivatives from restraints to the model.
Class for adding derivatives from restraints to the model.
Ints get_index(const kernel::ParticlesTemp &particles, const Subset &subset, const Subsets &excluded)
IMP::base::Vector< IMP::base::WeakPointer< kernel::ModelObject > > ModelObjectsTemp
ParticleIndex get_index() const
returns the particle index of this particle in its model
#define IMP_FINAL
Have the compiler report an error if anything overrides this method.
A smart pointer to a ref-counted Object that is a class member.
Definition: Pointer.h:147
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Basic types used by IMP.
virtual ModelObjectsTemp do_get_outputs() const
A smart pointer to a reference counted object.
Definition: Pointer.h:87
virtual void clear_caches()
Definition: Object.h:227
virtual ModelObjectsTemp do_get_inputs() const
Single variable function.
Key< 3, true > ParticleIndexKey
The type used to identify a particle attribute in the Particles.
Various general useful functions for IMP.
Class to handle individual model particles.
Storage of a model, its restraints, constraints and particles.
A nullptr-initialized pointer to an IMP Object.
Exception definitions and assertions.
A shared base class to help in debugging and things.
void show(Hierarchy h, std::ostream &out=std::cout)
Print out a molecular hierarchy.
IMP::kernel::Particle Particle
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:20
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:170
int Int
Basic integer value.
Definition: types.h:35
Keys to cache lookup of attribute strings.
std::string String
Basic string value.
Definition: types.h:44
#define IMP_OVERRIDE
Cause a compile error if this method does not override a parent method.
Class for storing model, its restraints, constraints, and particles.
Definition: kernel/Model.h:73