IMP logo
IMP Reference Guide  develop.907651fe7c,2026/01/28
The Integrative Modeling Platform
BoundingBox3DSingletonScore.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/BoundingBox3DSingletonScore.h
3  * \brief Score particles based on a bounding box
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPCORE_BOUNDING_BOX_3DSINGLETON_SCORE_H
9 #define IMPCORE_BOUNDING_BOX_3DSINGLETON_SCORE_H
10 
11 #include <IMP/core/core_config.h>
12 #include <IMP/generic.h>
13 #include <IMP/SingletonScore.h>
14 #include <IMP/singleton_macros.h>
15 #include <IMP/UnaryFunction.h>
17 #include "XYZ.h"
18 #include "internal/evaluate_distance_pair_score.h"
19 #include <boost/lambda/lambda.hpp>
20 
21 IMPCORE_BEGIN_NAMESPACE
22 
23 //! A generic C++ template for scoring particles based on how far outside a
24 //! box they are using unary function UF.
25 /**
26  The bounding box score is evaluated by applying a function f of
27  class UF to a particle based on the distance of its center from
28  the bounding box walls. The radius of the particle is ignored. If
29  the particle is within the bounding box, the score is always
30  0. Logically, it is expected that f(0) = 0 and f(x) > 0 if x>0.
31 
32  In Python, use the templated \ref BoundingBox3DSingletonScore class
33  as in the following example. In the example, 10 particles of
34  radius 1.0 are generated in a 10 A x 10 A x 10 A bounding box. The
35  particles are restrained in the bounding box by applying an
36  upper-bounded harmonic potential. It is upper bounded because it
37  is active only outside the bounding box.
38 
39  \code{.py}
40  k_bb = 10.0 # bounding box force coefficient in kcal/mol/A^2
41  bb_side = 10.0 # in A
42  radius = 1.0 # in A
43  number
44  m = IMP.Model()
45  bb = IMP.algebra.BoundingBox3D(IMP.algebra.Vector3D(0, 0, 0),
46  IMP.algebra.Vector3D(bb_side, bb_side, bb_side))
47  particles = []
48  for i in range(0,10):
49  p = IMP.Particle(m)
50  s = IMP.algebra.Sphere3D(
51  IMP.algebra.get_random_vector_in(bb), radius)
52  d = IMP.core.XYZR.setup_particle(p, s)
53  d.set_coordinates_are_optimized(True)
54  particles.append(p)
55  hpb = IMP.core.HarmonicUpperBound(0, k)
56  bbss = IMP.core.BoundingBox3DSingletonScore(hpb, bb)
57  bbr = IMP.container.SingletonsRestraint(bbss, particles)
58  \endcode
59 
60  \see BoundingBox3DSingletonScore
61  \see create_bounding_box_3d_singleton_score
62  */
63 template <class UF>
67 
68  public:
69  /**
70  @param f an unary function applied to particles that are outside the box.
71  f is passed the distance of the particle center from the bounding
72  box walls, ignoring the particle radius. Logically, it is expected
73  to satisfy f(0) = 0 and f(x) > 0 if x>0.
74  @param bb the bounding box to which f is applied
75  */
77 
78  UF *get_unary_function() const { return f_; }
79  algebra::BoundingBoxD<3> get_bounding_box() const { return bb_; }
80 
81  /**
82  return 0 if the p is within the bounding box or f(d) if it is
83  outside the bounding box, where f is the unary function provided
84  during construction and d is the distance of the center of p from
85  the bounding box walls.
86  Update derivatives as needed, weighted using da.
87  */
88  virtual double evaluate_index(Model *m, ParticleIndex p,
89  DerivativeAccumulator *da) const override;
91  Model *m, const ParticleIndexes &pis) const override {
92  return IMP::get_particles(m, pis);
93  }
96  ;
97 };
98 
99 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
100 template <class UF>
102  UF *f, const algebra::BoundingBox3D &bb)
103  : f_(f), bb_(bb) {
104  IMP_USAGE_CHECK(std::abs(f_->evaluate(0)) < .1,
105  "The unary function should return "
106  " 0 when passed a value of 0. Not "
107  << f_->evaluate(0));
108 }
109 template <class UF>
111  Model *m, ParticleIndex pi,
112  DerivativeAccumulator *da) const {
114  core::XYZ d(m, pi);
116  bool outside = false;
117  for (unsigned int i = 0; i < 3; ++i) {
118  if (bb_.get_corner(0)[i] > d.get_coordinate(i)) {
119  cp[i] = bb_.get_corner(0)[i];
120  outside = true;
121  } else if (bb_.get_corner(1)[i] < d.get_coordinate(i)) {
122  cp[i] = bb_.get_corner(1)[i];
123  outside = true;
124  } else {
125  cp[i] = d.get_coordinate(i);
126  }
127  }
128  if (outside) {
129  IMP_LOG_VERBOSE("Particle " << Showable(pi) << " is outside box: " << d
130  << " of " << bb_ << std::endl);
131  algebra::Vector3D deriv;
132  double v = internal::compute_distance_pair_score(
133  d.get_coordinates() - cp, f_.get(), &deriv, boost::lambda::_1);
134  if (da) {
135  d.add_to_derivatives(deriv, *da);
136  }
137  return v;
138  } else {
139  return 0;
140  }
141 }
142 
143 #endif
144 
145 //! Score particles based on how far outside a box they are by
146 //! applying f to the distance.
147 //! \see GenericBoundingBox3DSingletonScore
148 /**
149  Example usage is provided in \ref GenericBoundingBox3DSingletonScore
150 */
151 IMP_GENERIC_OBJECT(BoundingBox3DSingletonScore, bounding_box_3d_singleton_score,
152  UnaryFunction,
153  (UnaryFunction *f, const algebra::BoundingBoxD<3> &bb),
154  (f, bb));
155 
156 IMPCORE_END_NAMESPACE
157 
158 #endif /* IMPCORE_BOUNDING_BOX_3DSINGLETON_SCORE_H */
159 
virtual double evaluate_index(Model *m, ParticleIndex p, DerivativeAccumulator *da) const override
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
#define IMP_OBJECT_LOG
Set the log level to the object's log level.
Definition: log_macros.h:284
Single variable function.
Macros for various classes.
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
Abstract class for scoring object(s) of type ParticleIndex.
#define IMP_SINGLETON_SCORE_METHODS(Name)
A bounding box in D dimensions.
A decorator for a particle with x,y,z coordinates.
Definition: XYZ.h:30
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pis) const override
Overload this method to specify the inputs.
Define SingletonScore.
VectorD< 3 > Vector3D
Definition: VectorD.h:408
GenericBoundingBox3DSingletonScore< UnaryFunction > BoundingBox3DSingletonScore
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
Class for adding derivatives from restraints to the model.
Compile-time generic restraint and constraint support.