IMP  2.2.1
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-2014 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 
15 IMPSCOREFUNCTOR_BEGIN_NAMESPACE
16 
17 //! Create efficient distance-based pair scores.
18 /** This class allows one to create efficient distance-based
19  pair scores in C++ by simply writing a pair of small
20  functors (DistanceScore and DistanceDerivative) that do
21  the scoring.
22 
23  \see DistanceScore
24 
25  \note we can add arguments to get the list of input
26  particles and containers later, for now the former
27  is assumed to just be the input and the latter empty.
28 */
29 template <class DistanceScoreT>
30 class DistancePairScore : public PairScore {
31  DistanceScoreT ds_;
32 
33  public:
34  typedef DistanceScoreT DistanceScore;
35  // for backwards compat
36  DistancePairScore(const DistanceScore &t0,
37  std::string name = "FunctorDistancePairScore %1%")
38  : PairScore(name), ds_(t0) {}
39 
40  virtual double evaluate_index(kernel::Model *m,
41  const kernel::ParticleIndexPair &pip,
42  DerivativeAccumulator *da) const IMP_OVERRIDE;
43  virtual kernel::ModelObjectsTemp do_get_inputs(
44  kernel::Model *m, const kernel::ParticleIndexes &pis) const IMP_OVERRIDE;
47 };
48 
49 #ifndef IMP_DOXYGEN
50 template <class DistanceScore>
53  DerivativeAccumulator *da) const {
54  algebra::Vector3D delta =
55  m->get_sphere(p[0]).get_center() - m->get_sphere(p[1]).get_center();
56  double sq = delta.get_squared_magnitude();
57  if (ds_.get_is_trivially_zero(m, p, sq)) {
58  return 0;
59  }
60  double dist = std::sqrt(sq);
61  if (da) {
62  std::pair<double, double> sp = ds_.get_score_and_derivative(m, p, dist);
63  static const double MIN_DISTANCE = .00001;
65  if (dist > MIN_DISTANCE) {
66  uv = delta / dist;
67  } else {
68  uv = algebra::get_zero_vector_d<3>();
69  }
70  m->add_to_coordinate_derivatives(p[0], uv * sp.second, *da);
71  m->add_to_coordinate_derivatives(p[1], -uv * sp.second, *da);
72  return sp.first;
73  } else {
74  return ds_.get_score(m, p, dist);
75  }
76 }
77 template <class DistanceScore>
78 inline kernel::ModelObjectsTemp DistancePairScore<DistanceScore>::do_get_inputs(
79  kernel::Model *m, const kernel::ParticleIndexes &pis) const {
81  ret += ds_.get_inputs(m, pis);
82  return ret;
83 }
84 #endif
85 
86 IMPSCOREFUNCTOR_END_NAMESPACE
87 
88 #endif /* IMPSCORE_FUNCTOR_DISTANCE_PAIR_SCORE_H */
Class for adding derivatives from restraints to the model.
Import IMP/kernel/pair_macros.h in the namespace.
IMP::kernel::PairScore PairScore
IMP::base::Vector< IMP::base::WeakPointer< kernel::ModelObject > > ModelObjectsTemp
#define IMP_PAIR_SCORE_METHODS(Name)
Create efficient distance-based pair scores.
A class to store an fixed array of same-typed values.
Definition: base/Array.h:33
Abstract score function.
IMP::kernel::Model Model
Import IMP/kernel/PairScore.h in the namespace.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
VectorD< 3 > Vector3D
Definition: VectorD.h:395
Class for storing model, its restraints, constraints, and particles.
Definition: kernel/Model.h:72