IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
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
17IMPSCOREFUNCTOR_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*/
30template <class DistanceScoreT>
31class 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) {}
47 DistancePairScore() {}
48
49 virtual double evaluate_index(Model *m,
50 const ParticleIndexPair &pip,
51 DerivativeAccumulator *da) const override;
52
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
63 const DistanceScoreT& get_score_functor() const { return ds_; }
64
65 IMP_PAIR_SCORE_METHODS(DistancePairScore);
66 IMP_OBJECT_METHODS(DistancePairScore);
67};
68
69#ifndef IMP_DOXYGEN
70template <class DistanceScore>
72 Model *m, const ParticleIndexPair &p,
73 DerivativeAccumulator *da) const {
74 algebra::Vector3D delta =
75 m->get_sphere(std::get<0>(p)).get_center()
76 - m->get_sphere(std::get<1>(p)).get_center();
77 double sq = delta.get_squared_magnitude();
78 if (ds_.get_is_trivially_zero(m, p, sq)) {
79 return 0;
80 }
81 double dist = std::sqrt(sq);
82 if (da) {
83 std::pair<double, double> sp = ds_.get_score_and_derivative(m, p, dist);
84 static const double MIN_DISTANCE = .00001;
86 if (dist > MIN_DISTANCE) {
87 uv = delta / dist;
88 } else {
89 uv = algebra::get_zero_vector_d<3>();
90 }
91 m->add_to_coordinate_derivatives(std::get<0>(p), uv * sp.second, *da);
92 m->add_to_coordinate_derivatives(std::get<1>(p), -uv * sp.second, *da);
93 return sp.first;
94 } else {
95 return ds_.get_score(m, p, dist);
96 }
97}
98template <class DistanceScore>
99inline ModelObjectsTemp DistancePairScore<DistanceScore>::do_get_inputs(
100 Model *m, const ParticleIndexes &pis) const {
102 ret += ds_.get_inputs(m, pis);
103 return ret;
104}
105#endif
106
107IMPSCOREFUNCTOR_END_NAMESPACE
108
109#endif /* IMPSCORE_FUNCTOR_DISTANCE_PAIR_SCORE_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 ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pis) const override
Overload this method to specify the inputs.
Vector< ParticleIndex, std::allocator< ParticleIndex > > ParticleIndexes
A list of particles, each identified by its ParticleIndex.
Definition base_types.h:199
VectorD< 3 > Vector3D
Definition VectorD.h:407
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.
#define IMP_PAIR_SCORE_METHODS(Name)
Definition pair_macros.h:25