IMP  2.0.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-2013 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>
31 {
32  DistanceScoreT ds_;
33 public:
34  typedef DistanceScoreT DistanceScore;
35  // for backwards compat
36  DistancePairScore(const DistanceScore &t0,
37  std::string name="FunctorDistancePairScore %1%"):
38  PairScore(name),
39  ds_(t0) {}
40 
42 };
43 
44 #ifndef IMP_DOXYGEN
45 template <class DistanceScore>
47 evaluate_index(Model *m, const ParticleIndexPair& p,
48  DerivativeAccumulator *da) const {
49  algebra::Vector3D delta=m->get_sphere(p[0]).get_center()-
50  m->get_sphere(p[1]).get_center();
51  double sq= delta.get_squared_magnitude();
52  if (ds_.get_is_trivially_zero(m, p, sq)) {return 0;}
53  double dist= std::sqrt(sq);
54  if (da) {
55  std::pair<double, double> sp=
56  ds_.get_score_and_derivative(m, p, dist);
57  static const double MIN_DISTANCE = .00001;
59  if (dist > MIN_DISTANCE) {
60  uv= delta/dist;
61  } else {
62  uv= algebra::get_zero_vector_d<3>();
63  }
64  m->add_to_coordinate_derivatives(p[0], uv*sp.second, *da);
65  m->add_to_coordinate_derivatives(p[1], -uv*sp.second, *da);
66  return sp.first;
67  } else {
68  return ds_.get_score(m, p, dist);
69  }
70 }
71 template <class DistanceScore>
72 inline ModelObjectsTemp DistancePairScore<DistanceScore>::
73 do_get_inputs(Model *m,
74  const ParticleIndexes &pis) const {
75  ModelObjectsTemp ret;
76  ret+= ds_.get_inputs(m, pis);
77  return ret;
78 }
79 template <class DistanceScore>
80 inline void DistancePairScore<DistanceScore>::
81 do_show(std::ostream &) const {
82 }
83 #endif
84 
85 
86 
87 IMPSCOREFUNCTOR_END_NAMESPACE
88 
89 #endif /* IMPSCORE_FUNCTOR_DISTANCE_PAIR_SCORE_H */