IMP logo
IMP Reference Guide  2.16.0
The Integrative Modeling Platform
DistancePairScoreWithCache.h
Go to the documentation of this file.
1 /**
2  * \file IMP/score_functor/DistancePairScoreWithCache.h
3  * \brief A Score on the distance between a pair of particles, using cache.
4  *
5  * Copyright 2007-2021 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPSCORE_FUNCTOR_DISTANCE_PAIR_SCORE_WITH_CACHE_H
9 #define IMPSCORE_FUNCTOR_DISTANCE_PAIR_SCORE_WITH_CACHE_H
10 
11 #include <IMP/score_functor/score_functor_config.h>
12 #include <IMP/PairScore.h>
13 #include <IMP/pair_macros.h>
14 
15 IMPSCOREFUNCTOR_BEGIN_NAMESPACE
16 
17 //! Create efficient distance-based pair scores, with cache.
18 /** This class allows one to create efficient distance-based
19  pair scores in C++ by simply writing a functor (ScoreWithCache) that
20  does the scoring. This is like DistancePairScore but it allows the
21  functor to do once-per-model-evaluation cache updates.
22 
23  \see ScoreWithCache, DistancePairScore
24 */
25 template <class DistanceScoreT>
27  DistanceScoreT ds_;
28 
29  double evaluate_index_with_cache(Model *m,
30  const ParticleIndexPair &pip,
31  DerivativeAccumulator *da) const;
32 
33  public:
34  typedef DistanceScoreT DistanceScore;
35 
36  // for backwards compat
37  DistancePairScoreWithCache(const DistanceScore &t0,
38  std::string name = "FunctorDistancePairScoreWithCache %1%")
39  : PairScore(name), ds_(t0) {}
40 
41  virtual double evaluate_index(Model *m,
42  const ParticleIndexPair &pip,
44 
45  virtual ModelObjectsTemp do_get_inputs(
46  Model *m, const ParticleIndexes &pis) const IMP_OVERRIDE;
47 
48  /**
49  return a reference to the functor that is applied on a pair of particles
50  in order to compute their distances
51  */
52  DistanceScoreT& get_score_functor()
53  {return ds_; }
54 
55  virtual double evaluate_indexes(
56  Model *m, const ParticleIndexPairs &p,
57  DerivativeAccumulator *da, unsigned int lower_bound,
58  unsigned int upper_bound) const IMP_OVERRIDE;
59 
60  virtual double evaluate_indexes_scores(
62  unsigned int lower_bound, unsigned int upper_bound,
63  std::vector<double> &score) const IMP_OVERRIDE;
64 
65  virtual double evaluate_indexes_delta(
67  const std::vector<unsigned> &indexes,
68  std::vector<double> &score) const IMP_OVERRIDE;
69 
71 };
72 
73 #ifndef IMP_DOXYGEN
74 template <class DistanceScore>
76  Model *m, const ParticleIndexPairs &p,
77  DerivativeAccumulator *da, unsigned int lower_bound,
78  unsigned int upper_bound) const {
79  double ret = 0;
80  ds_.check_cache_valid(m);
81  for (unsigned int i = lower_bound; i < upper_bound; ++i) {
82  ret += evaluate_index_with_cache(m, p[i], da);
83  }
84  return ret;
85 }
86 
87 template <class DistanceScore>
88 inline double
89 DistancePairScoreWithCache<DistanceScore>::evaluate_indexes_scores(
90  Model *m, const ParticleIndexPairs &p, DerivativeAccumulator *da,
91  unsigned int lower_bound, unsigned int upper_bound,
92  std::vector<double> &score) const {
93  double ret = 0;
94  ds_.check_cache_valid(m);
95  for (unsigned int i = lower_bound; i < upper_bound; ++i) {
96  double s = evaluate_index_with_cache(m, p[i], da);
97  score[i] = s;
98  ret += s;
99  }
100  return ret;
101 }
102 
103 template <class DistanceScore>
104 inline double
105 DistancePairScoreWithCache<DistanceScore>::evaluate_indexes_delta(
106  Model *m, const ParticleIndexPairs &p, DerivativeAccumulator *da,
107  const std::vector<unsigned> &indexes, std::vector<double> &score) const {
108  double ret = 0;
109  ds_.check_cache_valid(m);
110  for (std::vector<unsigned>::const_iterator it = indexes.begin();
111  it != indexes.end(); ++it) {
112  double s = evaluate_index_with_cache(m, p[*it], da);
113  ret = ret - score[*it] + s;
114  score[*it] = s;
115  }
116  return ret;
117 }
118 
119 template <class DistanceScore>
120 inline double DistancePairScoreWithCache<DistanceScore>::evaluate_index(
121  Model *m, const ParticleIndexPair &p,
122  DerivativeAccumulator *da) const {
123  ds_.check_cache_valid(m);
124  return evaluate_index_with_cache(m, p, da);
125 }
126 
127 template <class DistanceScore>
128 inline double
129 DistancePairScoreWithCache<DistanceScore>::evaluate_index_with_cache(
130  Model *m, const ParticleIndexPair &p,
131  DerivativeAccumulator *da) const {
132  algebra::Vector3D delta =
133  m->get_sphere(p[0]).get_center() - m->get_sphere(p[1]).get_center();
134  double sq = delta.get_squared_magnitude();
135  if (ds_.get_is_trivially_zero_with_cache(m, p, sq)) {
136  return 0;
137  }
138  double dist = std::sqrt(sq);
139  if (da) {
140  std::pair<double, double> sp = ds_.get_score_and_derivative_with_cache(
141  m, p, dist);
142  static const double MIN_DISTANCE = .00001;
144  if (dist > MIN_DISTANCE) {
145  uv = delta / dist;
146  } else {
147  uv = algebra::get_zero_vector_d<3>();
148  }
149  m->add_to_coordinate_derivatives(p[0], uv * sp.second, *da);
150  m->add_to_coordinate_derivatives(p[1], -uv * sp.second, *da);
151  return sp.first;
152  } else {
153  return ds_.get_score_with_cache(m, p, dist);
154  }
155 }
156 
157 template <class DistanceScore>
158 inline ModelObjectsTemp
159 DistancePairScoreWithCache<DistanceScore>::do_get_inputs(
160  Model *m, const ParticleIndexes &pis) const {
161  ModelObjectsTemp ret;
162  ret += ds_.get_inputs(m, pis);
163  return ret;
164 }
165 #endif
166 
167 IMPSCOREFUNCTOR_END_NAMESPACE
168 
169 #endif /* IMPSCORE_FUNCTOR_DISTANCE_PAIR_SCORE_WITH_CACHE_H */
Abstract class for scoring object(s) of type ParticleIndexPair.
Definition: PairScore.h:42
Macros for various classes.
IMP::Vector< ParticleIndexPair > ParticleIndexPairs
Definition: base_types.h:168
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Create efficient distance-based pair scores, with cache.
A more IMP-like version of the std::vector.
Definition: Vector.h:40
IMP::Vector< IMP::WeakPointer< ModelObject > > ModelObjectsTemp
Definition: base_types.h:89
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:73
Define PairScore.
VectorD< 3 > Vector3D
Definition: VectorD.h:421
#define IMP_OVERRIDE
Cause a compile error if this method does not override a parent method.
Class for adding derivatives from restraints to the model.