IMP logo
IMP Reference Guide  2.20.0
The Integrative Modeling Platform
score_functor/DistancePairScore.h
Go to the documentation of this file.
1 /**
2  * \file IMP/score_functor/DistancePairScore.h
3  * \brief A Score on the distance between a pair of particles.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPSCORE_FUNCTOR_DISTANCE_PAIR_SCORE_H
9 #define IMPSCORE_FUNCTOR_DISTANCE_PAIR_SCORE_H
10 
11 #include <IMP/score_functor/score_functor_config.h>
12 #include <IMP/PairScore.h>
13 #include <IMP/pair_macros.h>
14 #include <cereal/access.hpp>
15 #include <cereal/types/base_class.hpp>
16 
17 IMPSCOREFUNCTOR_BEGIN_NAMESPACE
18 
19 //! Create efficient distance-based pair scores.
20 /** This class allows one to create efficient distance-based
21  pair scores in C++ by simply writing a functor (Score) that
22  does the scoring.
23 
24  \see Score
25 
26  \note we can add arguments to get the list of input
27  particles and containers later, for now the former
28  is assumed to just be the input and the latter empty.
29 */
30 template <class DistanceScoreT>
31 class DistancePairScore : public PairScore {
32  DistanceScoreT ds_;
33 
34  friend class cereal::access;
35 
36  template<class Archive> void serialize(Archive &ar) {
37  ar(cereal::base_class<PairScore>(this), ds_);
38  }
39 
40  public:
41  typedef DistanceScoreT DistanceScore;
42 
43  // for backwards compat
44  DistancePairScore(const DistanceScore &t0,
45  std::string name = "FunctorDistancePairScore %1%")
46  : PairScore(name), ds_(t0) {}
48 
49  virtual double evaluate_index(Model *m,
50  const ParticleIndexPair &pip,
51  DerivativeAccumulator *da) const override;
52 
53  virtual ModelObjectsTemp do_get_inputs(
54  Model *m, const ParticleIndexes &pis) const override;
55 
56  /**
57  return a reference to the functor that is applied on a pair of particles
58  in order to compute their distances
59  */
60  DistanceScoreT& get_score_functor()
61  {return ds_; }
62 
65 };
66 
67 #ifndef IMP_DOXYGEN
68 template <class DistanceScore>
70  Model *m, const ParticleIndexPair &p,
71  DerivativeAccumulator *da) const {
72  algebra::Vector3D delta =
73  m->get_sphere(p[0]).get_center() - m->get_sphere(p[1]).get_center();
74  double sq = delta.get_squared_magnitude();
75  if (ds_.get_is_trivially_zero(m, p, sq)) {
76  return 0;
77  }
78  double dist = std::sqrt(sq);
79  if (da) {
80  std::pair<double, double> sp = ds_.get_score_and_derivative(m, p, dist);
81  static const double MIN_DISTANCE = .00001;
83  if (dist > MIN_DISTANCE) {
84  uv = delta / dist;
85  } else {
86  uv = algebra::get_zero_vector_d<3>();
87  }
88  m->add_to_coordinate_derivatives(p[0], uv * sp.second, *da);
89  m->add_to_coordinate_derivatives(p[1], -uv * sp.second, *da);
90  return sp.first;
91  } else {
92  return ds_.get_score(m, p, dist);
93  }
94 }
95 template <class DistanceScore>
96 inline ModelObjectsTemp DistancePairScore<DistanceScore>::do_get_inputs(
97  Model *m, const ParticleIndexes &pis) const {
98  ModelObjectsTemp ret;
99  ret += ds_.get_inputs(m, pis);
100  return ret;
101 }
102 #endif
103 
104 IMPSCOREFUNCTOR_END_NAMESPACE
105 
106 #endif /* IMPSCORE_FUNCTOR_DISTANCE_PAIR_SCORE_H */
Abstract class for scoring object(s) of type ParticleIndexPair.
Definition: PairScore.h:44
Macros for various classes.
#define IMP_PAIR_SCORE_METHODS(Name)
Definition: pair_macros.h:25
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Create efficient distance-based pair scores.
A more IMP-like version of the std::vector.
Definition: Vector.h:42
IMP::Vector< IMP::WeakPointer< ModelObject > > ModelObjectsTemp
Definition: base_types.h:106
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
Define PairScore.
VectorD< 3 > Vector3D
Definition: VectorD.h:425
Class for adding derivatives from restraints to the model.