IMP  2.0.1
The Integrative Modeling Platform
MoverBase.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/MoverBase.h \brief Backwards compatibility.
3  *
4  * Copyright 2007-2013 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPCORE_MOVER_BASE_H
9 #define IMPCORE_MOVER_BASE_H
10 
11 #include <IMP/core/core_config.h>
12 #include "Mover.h"
13 
14 #include <IMP/internal/container_helpers.h>
15 #include <IMP/macros.h>
16 
17 #include <vector>
18 
19 IMPCORE_BEGIN_NAMESPACE
20 
21 
22 #if defined(IMP_DOXYGEN) || IMP_HAS_DEPRECATED
23 
24 //! A class to help implement movers
25 /** This class helps in implementing Movers by allowing changes to be easily
26  rolled back. It maintains a list of particles and a list of attributes.
27  All changes to the product of those two lists will be rolled back
28  when reject_move() is called.
29 
30  See NormalMover for a simple example using this class.
31  */
32 class IMPCOREEXPORT MoverBase: public Mover
33 {
34  base::Vector<Floats > values_;
36  ParticleIndexes particles_;
37  void do_propose_value(unsigned int i,
38  unsigned int j, Float t) {
39  IMP_USAGE_CHECK(j < keys_.size(), "Out of range key");
40  IMP_USAGE_CHECK(i < particles_.size(),
41  "Out of range particle");
42  if (get_model()->get_is_optimized(keys_[j],
43  particles_[i])) {
44  get_model()->set_attribute(keys_[j], particles_[i],
45  t);
46  IMP_USAGE_CHECK_FLOAT_EQUAL(get_model()
47  ->get_attribute(keys_[j],
48  particles_[i]),
49  t,
50  "Tried to set, but it didn't work.");
51  } else {
52  IMP_LOG_TERSE( "Dropping change to unoptimized attribute: "
53  << keys_[j] << " of particle "
54  << get_model()->get_particle(particles_[i])->get_name()
55  << std::endl);
56  }
57  }
58 public:
59  virtual void reset_move();
60 
61  /** This sets everything up and then calls the generate_move method.
62  */
63  virtual ParticlesTemp propose_move(Float f);
64 
65  ParticlesTemp get_output_particles() const {
66  return IMP::internal::get_particle(get_model(), particles_);
67  }
68 
69 protected:
70  unsigned int get_number_of_particles() const {
71  return particles_.size();
72  }
73  unsigned int get_number_of_keys() const {
74  return keys_.size();
75  }
76  std::string get_particle_name(unsigned int i) const {
77  return get_model()->get_particle(particles_[i])->get_name();
78  }
79 
80  //! implement this method to propose a move
81  /** See NormalMover for a simple example.
82  */
83  virtual void do_move (Float f) =0;
84 
85  //! Get the value of a controlled attribute
86  /** \param [in] i The index of the particle.
87  \param [in] j The index of the attribute.
88  */
89  Float get_value (unsigned int i, unsigned int j) const {
90  IMP_USAGE_CHECK(j < keys_.size(), "Out of range key");
91  IMP_USAGE_CHECK(i < particles_.size(), "Out of range particle");
92  return get_model()->get_attribute(keys_[j], particles_[i]);
93  }
94 
95  //! Propose a value
96  /** \param[in] i The index of the particle.
97  \param[in] j The index of the key
98  \param[in] t The value to propose
99  */
100  void propose_value(unsigned int i, unsigned int j, Float t) {
101  do_propose_value(i, j, t);
102  }
103 
104 MoverBase(const ParticlesTemp &ps,
105  const FloatKeys &keys,
106  std::string name):
107  Mover(IMP::internal::get_model(ps), name),
108  keys_(keys),
109  particles_(IMP::internal::get_index(ps)) {}
110 };
111 
112 
113 inline ParticlesTemp MoverBase::propose_move(Float f)
114 {
115  values_.resize(particles_.size(),
116  Floats(keys_.size(), 0));
117  for (unsigned int i=0; i< particles_.size(); ++i) {
118  for (unsigned int j=0; j< keys_.size(); ++j) {
119  values_[i][j]= get_value(i,j);
120  }
121  }
122  do_move(f);
123  return IMP::internal::get_particle(get_model(), particles_);
124 }
125 
126 
127 inline void MoverBase::reset_move()
128 {
129  for (unsigned int i=0; i< particles_.size(); ++i) {
130  for (unsigned int j=0; j< keys_.size(); ++j) {
131  get_model()->set_attribute(keys_[j], particles_[i], values_[i][j]);
132  }
133  }
134 }
135 
137 #endif
138 
139 IMPCORE_END_NAMESPACE
140 
141 #endif /* IMPCORE_MOVER_BASE_H */