IMP logo
IMP Reference Guide  develop.d97d4ead1f,2024/11/21
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-2024 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(std::get<0>(p)).get_center()
74  - m->get_sphere(std::get<1>(p)).get_center();
75  double sq = delta.get_squared_magnitude();
76  if (ds_.get_is_trivially_zero(m, p, sq)) {
77  return 0;
78  }
79  double dist = std::sqrt(sq);
80  if (da) {
81  std::pair<double, double> sp = ds_.get_score_and_derivative(m, p, dist);
82  static const double MIN_DISTANCE = .00001;
84  if (dist > MIN_DISTANCE) {
85  uv = delta / dist;
86  } else {
87  uv = algebra::get_zero_vector_d<3>();
88  }
89  m->add_to_coordinate_derivatives(std::get<0>(p), uv * sp.second, *da);
90  m->add_to_coordinate_derivatives(std::get<1>(p), -uv * sp.second, *da);
91  return sp.first;
92  } else {
93  return ds_.get_score(m, p, dist);
94  }
95 }
96 template <class DistanceScore>
97 inline ModelObjectsTemp DistancePairScore<DistanceScore>::do_get_inputs(
98  Model *m, const ParticleIndexes &pis) const {
99  ModelObjectsTemp ret;
100  ret += ds_.get_inputs(m, pis);
101  return ret;
102 }
103 #endif
104 
105 IMPSCOREFUNCTOR_END_NAMESPACE
106 
107 #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:50
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:408
Class for adding derivatives from restraints to the model.