IMP logo
IMP Reference Guide  develop.02fce3ae61,2026/01/08
The Integrative Modeling Platform
DistanceToSingletonScore.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/DistanceToSingletonScore.h
3  * \brief A Score on the distance to a fixed point.
4  *
5  * Copyright 2007-2025 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPCORE_DISTANCE_TO_SINGLETON_SCORE_H
9 #define IMPCORE_DISTANCE_TO_SINGLETON_SCORE_H
10 
11 #include <IMP/core/core_config.h>
12 #include "XYZ.h"
13 #include "internal/evaluate_distance_pair_score.h"
14 #include <IMP/generic.h>
15 #include <IMP/algebra/Vector3D.h>
16 #include <IMP/SingletonScore.h>
17 #include <IMP/Pointer.h>
18 #include <IMP/singleton_macros.h>
19 #include <IMP/UnaryFunction.h>
20 #include <boost/lambda/lambda.hpp>
21 #include <cereal/access.hpp>
22 #include <cereal/types/base_class.hpp>
23 
24 IMPCORE_BEGIN_NAMESPACE
25 
26 //! Apply a function to the distance to a fixed point.
27 /** A particle is scored based on the distance between it and a constant
28  point as passed to a UnaryFunction. This is useful for anchoring
29  constraining particles within a sphere.
30 
31  To restrain a set of particles stored in SingletonContainer pc in a sphere
32  do the following:
33  \include core/restrain_in_sphere.py
34  */
35 template <class UF>
39  struct StaticD {
41  StaticD(algebra::Vector3D v) : v_(v) {}
42  Float get_coordinate(unsigned int i) { return v_[i]; }
43  void add_to_derivatives(algebra::Vector3D v, DerivativeAccumulator) {
44  // The fixed point is not a real particle, so derivatives aren't used
45  IMP_UNUSED(v);
46  }
47  };
48 
49  friend class cereal::access;
50 
51  template<class Archive> void serialize(Archive &ar) {
52  ar(cereal::base_class<SingletonScore>(this), f_, pt_);
53  }
55 
56  public:
59  virtual double evaluate_index(Model *m, ParticleIndex p,
60  DerivativeAccumulator *da) const override;
62  Model *m, const ParticleIndexes &pis) const override {
63  return IMP::get_particles(m, pis);
64  }
65 
66  UF *get_unary_function() const { return f_; }
67  algebra::Vector3D get_point() const { return pt_; }
68 
70  // We never instantiate the template except with UnaryFunction, so
71  // call this class plain 'DistanceToSingletonScore' to help out
72  // Python get_derived_object() (this is essentially the code that would
73  // be inserted by IMP_OBJECT_METHODS)
74  virtual std::string get_type_name() const override {
75  return "DistanceToSingletonScore";
76  }
77  virtual ::IMP::VersionInfo get_version_info() const override {
78  return ::IMP::VersionInfo(get_module_name(), get_module_version());
79  }
80  virtual ~GenericDistanceToSingletonScore() { IMP::Object::_on_destruction(); }
81 };
82 
83 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
84 
85 template <class UF>
86 GenericDistanceToSingletonScore<UF>::GenericDistanceToSingletonScore(
87  UF *f, const algebra::Vector3D &v)
88  : f_(f), pt_(v) {}
89 template <class UF>
91  Model *m, ParticleIndex pi,
92  DerivativeAccumulator *da) const {
93  double v = internal::evaluate_distance_pair_score(
94  XYZ(m, pi), StaticD(pt_), da, f_.get(), boost::lambda::_1);
95  IMP_LOG_VERBOSE("DistanceTo from " << XYZ(m, pi) << " to " << pt_
96  << " scored " << v << std::endl);
97  return v;
98 }
99 
100 #endif
101 
102 /** Use an IMP::UnaryFunction to score a distance to a point.*/
103 IMP_GENERIC_OBJECT(DistanceToSingletonScore, distance_to_singleton_score,
104  UnaryFunction,
105  (UnaryFunction *f, const algebra::Vector3D &pt), (f, pt));
106 
107 //! Apply a function to the distance to a fixed point.
108 /** A particle is scored based on the distance between it and a constant
109  point as passed to a UnaryFunction. This is useful for anchoring
110  constraining particles within a sphere.
111 
112  To restrain a set of particles stored in SingletonContainer pc in a sphere
113  do the following:
114  \include core/restrain_in_sphere.py
115  */
116 class IMPCOREEXPORT SphereDistanceToSingletonScore : public SingletonScore {
118  algebra::Vector3D pt_;
119  struct StaticD {
121  StaticD(algebra::Vector3D v) : v_(v) {}
122  Float get_coordinate(unsigned int i) { return v_[i]; }
123  void add_to_derivatives(algebra::Vector3D v, DerivativeAccumulator) {
124  // The fixed point is not a real particle, so derivatives aren't used
125  IMP_UNUSED(v);
126  }
127  };
128 
129  public:
131  virtual double evaluate_index(Model *m, ParticleIndex p,
132  DerivativeAccumulator *da) const override;
134  Model *m, const ParticleIndexes &pis) const override {
135  return IMP::get_particles(m, pis);
136  }
139  ;
140 };
141 
142 IMPCORE_END_NAMESPACE
143 
144 #endif /* IMPCORE_DISTANCE_TO_SINGLETON_SCORE_H */
GenericDistanceToSingletonScore< UnaryFunction > DistanceToSingletonScore
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Single variable function.
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pis) const override
Overload this method to specify the inputs.
virtual ::IMP::VersionInfo get_version_info() const override
Get information about the module and version of the object.
Macros for various classes.
Index< ParticleIndexTag > ParticleIndex
Definition: base_types.h:182
ParticlesTemp get_particles(Model *m, const ParticleIndexes &ps)
Get the particles from a list of indexes.
#define IMP_LOG_VERBOSE(expr)
Definition: log_macros.h:83
A more IMP-like version of the std::vector.
Definition: Vector.h:50
Simple XYZ decorator.
#define IMP_GENERIC_OBJECT(Name, lcname, targument, carguments, cparguments)
Typedefs a default instantiation for a generic (templated) object.
Definition: object_macros.h:60
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
#define IMP_UNUSED(variable)
Abstract class for scoring object(s) of type ParticleIndex.
#define IMP_SINGLETON_SCORE_METHODS(Name)
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pis) const override
Overload this method to specify the inputs.
virtual double evaluate_index(Model *m, ParticleIndex p, DerivativeAccumulator *da) const override
Compute the score and the derivative if needed.
#define IMP_OBJECT_SERIALIZE_DECL(Name)
Declare methods needed for serialization of Object pointers.
Definition: object_macros.h:95
Apply a function to the distance to a fixed point.
Define SingletonScore.
A nullptr-initialized pointer to an IMP Object.
VectorD< 3 > Vector3D
Definition: VectorD.h:408
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:19
Simple 3D vector class.
Apply a function to the distance to a fixed point.
Abstract single variable functor class for score functions.
Definition: UnaryFunction.h:27
Class for adding derivatives from restraints to the model.
Compile-time generic restraint and constraint support.