IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
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-2022 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
15IMPSCOREFUNCTOR_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*/
25template <class DistanceScoreT>
26class DistancePairScoreWithCache : public PairScore {
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,
43 DerivativeAccumulator *da) const override;
44
46 Model *m, const ParticleIndexes &pis) const 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, bool all_indexes_checked=false) const override;
59
62 unsigned int lower_bound, unsigned int upper_bound,
63 std::vector<double> &score,
64 bool all_indexes_checked=false) const override;
65
66 virtual double evaluate_indexes_delta(
68 const std::vector<unsigned> &indexes,
69 std::vector<double> &score,
70 bool all_indexes_checked=false) const override;
71
72 IMP_OBJECT_METHODS(DistancePairScoreWithCache);
73};
74
75#ifndef IMP_DOXYGEN
76template <class DistanceScore>
78 Model *m, const ParticleIndexPairs &p,
79 DerivativeAccumulator *da, unsigned int lower_bound,
80 unsigned int upper_bound, bool) const {
81 double ret = 0;
82 ds_.check_cache_valid(m);
83 for (unsigned int i = lower_bound; i < upper_bound; ++i) {
84 ret += evaluate_index_with_cache(m, p[i], da);
85 }
86 return ret;
87}
88
89template <class DistanceScore>
90inline double
91DistancePairScoreWithCache<DistanceScore>::evaluate_indexes_scores(
92 Model *m, const ParticleIndexPairs &p, DerivativeAccumulator *da,
93 unsigned int lower_bound, unsigned int upper_bound,
94 std::vector<double> &score, bool) const {
95 double ret = 0;
96 ds_.check_cache_valid(m);
97 for (unsigned int i = lower_bound; i < upper_bound; ++i) {
98 double s = evaluate_index_with_cache(m, p[i], da);
99 score[i] = s;
100 ret += s;
101 }
102 return ret;
103}
104
105template <class DistanceScore>
106inline double
107DistancePairScoreWithCache<DistanceScore>::evaluate_indexes_delta(
108 Model *m, const ParticleIndexPairs &p, DerivativeAccumulator *da,
109 const std::vector<unsigned> &indexes, std::vector<double> &score,
110 bool) const {
111 double ret = 0;
112 ds_.check_cache_valid(m);
113 for (unsigned it : indexes) {
114 double s = evaluate_index_with_cache(m, p[it], da);
115 ret = ret - score[it] + s;
116 score[it] = s;
117 }
118 return ret;
119}
120
121template <class DistanceScore>
122inline double DistancePairScoreWithCache<DistanceScore>::evaluate_index(
123 Model *m, const ParticleIndexPair &p,
124 DerivativeAccumulator *da) const {
125 ds_.check_cache_valid(m);
126 return evaluate_index_with_cache(m, p, da);
127}
128
129template <class DistanceScore>
130inline double
131DistancePairScoreWithCache<DistanceScore>::evaluate_index_with_cache(
132 Model *m, const ParticleIndexPair &p,
133 DerivativeAccumulator *da) const {
134 algebra::Vector3D delta =
135 m->get_sphere(std::get<0>(p)).get_center()
136 - m->get_sphere(std::get<1>(p)).get_center();
137 double sq = delta.get_squared_magnitude();
138 if (ds_.get_is_trivially_zero_with_cache(m, p, sq)) {
139 return 0;
140 }
141 double dist = std::sqrt(sq);
142 if (da) {
143 std::pair<double, double> sp = ds_.get_score_and_derivative_with_cache(
144 m, p, dist);
145 static const double MIN_DISTANCE = .00001;
146 algebra::Vector3D uv;
147 if (dist > MIN_DISTANCE) {
148 uv = delta / dist;
149 } else {
150 uv = algebra::get_zero_vector_d<3>();
151 }
152 m->add_to_coordinate_derivatives(std::get<0>(p), uv * sp.second, *da);
153 m->add_to_coordinate_derivatives(std::get<1>(p), -uv * sp.second, *da);
154 return sp.first;
155 } else {
156 return ds_.get_score_with_cache(m, p, dist);
157 }
158}
159
160template <class DistanceScore>
161inline ModelObjectsTemp
162DistancePairScoreWithCache<DistanceScore>::do_get_inputs(
163 Model *m, const ParticleIndexes &pis) const {
165 ret += ds_.get_inputs(m, pis);
166 return ret;
167}
168#endif
169
170IMPSCOREFUNCTOR_END_NAMESPACE
171
172#endif /* IMPSCORE_FUNCTOR_DISTANCE_PAIR_SCORE_WITH_CACHE_H */
Define PairScore.
Class for adding derivatives from restraints to the model.
Class for storing model, its restraints, constraints, and particles.
Definition Model.h:111
virtual double evaluate_index(Model *m, const ParticleIndexPair &pip, DerivativeAccumulator *da) const override
Compute the score and the derivative if needed.
virtual double evaluate_indexes(Model *m, const ParticleIndexPairs &p, DerivativeAccumulator *da, unsigned int lower_bound, unsigned int upper_bound, bool all_indexes_checked=false) const override
Compute the score and the derivative if needed over a set.
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pis) const override
Overload this method to specify the inputs.
virtual double evaluate_indexes_scores(Model *m, const ParticleIndexPairs &p, DerivativeAccumulator *da, unsigned int lower_bound, unsigned int upper_bound, std::vector< double > &score, bool all_indexes_checked=false) const override
Compute the score and the derivative if needed over a set.
virtual double evaluate_indexes_delta(Model *m, const ParticleIndexPairs &p, DerivativeAccumulator *da, const std::vector< unsigned > &indexes, std::vector< double > &score, bool all_indexes_checked=false) const override
Compute the change in score and the derivative if needed over a set.
IMP::Vector< ParticleIndexPair, std::allocator< ParticleIndexPair > > ParticleIndexPairs
Definition base_types.h:208
Vector< ParticleIndex, std::allocator< ParticleIndex > > ParticleIndexes
A list of particles, each identified by its ParticleIndex.
Definition base_types.h:199
Array< 2, ParticleIndex > ParticleIndexPair
An ordered pair of particles, each identified by its ParticleIndex.
Definition base_types.h:202
IMP::Vector< IMP::WeakPointer< ModelObject > > ModelObjectsTemp
Definition base_types.h:122
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Macros for various classes.