IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
linear_distance_pair_scores.h
Go to the documentation of this file.
1/**
2 * \file linear_distance_pair_scores.h
3 * \brief A Score on the distance between a pair of particles.
4 *
5 * Copyright 2007-2022 IMP Inventors. All rights reserved.
6 */
7
8#ifndef IMPNPCTRANSPORT_LINEAR_DISTANCE_PAIR_SCORES_H
9#define IMPNPCTRANSPORT_LINEAR_DISTANCE_PAIR_SCORES_H
10
11#include "npctransport_config.h"
12#include <IMP/PairScore.h>
13#include <IMP/pair_macros.h>
14#include <IMP/core/XYZR.h>
15#include <IMP/algebra/utility.h>
16#include "internal/sites.h"
17
18#include <boost/array.hpp>
19
20IMPNPCTRANSPORT_BEGIN_NAMESPACE
21
22#ifndef SWIG
23/**
24 Evaluates a linear pair potential, such that score = k * (delta_length - x0)
25 is returned.
26 Also, updates the derivative vectors of the particles if da is not null.
27
28 @param[out] d_xyzr0 pointer to derivative of particle 0
29 @param[out] d_xyzr1 pointer to derivative of particle 1
30 @param[in,out] da accumulator for score derivatives to be updated
31 @param[in] delta a vector that represents the displacement between the
32 two particles
33 @param delta_length the cached length of delta, assumed correct, and required
34 for faster calculation
35 @param x0 resting distance (where score = 0)
36 @param k score linear coefficient. Note that the score is attractive
37 for a positive k, and repulsive for a negative k
38 (assuming the lower the score the better).
39 @return the score
40 */
41inline double do_evaluate_index(algebra::Sphere3D& d_xyzr0,
42 algebra::Sphere3D& d_xyzr1,
44 const algebra::Vector3D &delta,
45 double delta_length, double x0, double k) {
46 double shifted_length = delta_length - x0;
47 double score = k * shifted_length;
48 static const double MIN_DISTANCE = .00001;
49 if (da && delta_length > MIN_DISTANCE) {
50 algebra::Vector3D deriv = k * delta / delta_length; // deriv magnitude is k
51 Model::add_to_coordinate_derivatives(d_xyzr0, deriv, *da);
52 Model::add_to_coordinate_derivatives(d_xyzr1, -deriv, *da);
53 IMP_LOG(TERSE, "Distance: " << shifted_length << "\nscore: " << score
54 << "\nderiv: " << deriv << std::endl);
55 }
56 IMP_CHECK_CODE ( else {
57 IMP_LOG(TERSE, "Distance: " << shifted_length << "\nscore: " << score
58 << std::endl);
59 } );
60 return score;
61}
62#endif
63
64/**
65 A soft linear repulsive score between two spheres.
66 If the spheres penetrate each other, the score is
67 ( k_ * [penetration-magnitude] kcal/mol), otherwise the score is 0.
68 */
69class IMPNPCTRANSPORTEXPORT LinearSoftSpherePairScore : public PairScore {
70 private:
71 // score potential coefficient (larger = stronger repulsion)
72 double k_;
73
74 protected:
75 //! evaluates the soft linear repulsive score, for spheres s0 and s1,
76 //! outputting derivatives to ds0 and ds1 scaled by da, if da is not null
77 inline double evaluate_index
78 (algebra::Sphere3D const& s0,
79 algebra::Sphere3D const& s1,
82 DerivativeAccumulator *da) const;
83
84 public:
85
86 LinearSoftSpherePairScore(double k,
87 std::string name = "LinearSSPairScore%1%");
88
89 virtual double evaluate_index(Model *m, const ParticleIndexPair &p,
90 DerivativeAccumulator *da) const override;
91
92 virtual double evaluate_indexes(Model *m,
93 const ParticleIndexPairs &pips,
95 unsigned int lower_bound,
96 unsigned int upper_bound,
97 bool all_indexes_checked=false) const override;
98
100 ( Model *m,
101 const ParticleIndexPairs &p,
103 double max, unsigned int lower_bound,
104 unsigned int upper_bound, bool all_indexes_checked=false) const override
105 {
106 IMP_UNUSED(all_indexes_checked);
107 double ret = 0;
108 for (unsigned int i = lower_bound; i < upper_bound; ++i) {
109 ret += evaluate_if_good_index(m, p[i], da, max - ret);
110 if (ret > max) return std::numeric_limits<double>::max();
111 }
112 return ret;
113 }
114
115
117 const ParticleIndexes &pis) const override;
118
119 //! returns the k for sphere-sphere repulsion
120 double get_k() const { return k_; }
121
122 IMP_OBJECT_METHODS(LinearSoftSpherePairScore);
123 ;
124};
125
126#ifndef IMP_DOXYGEN
127
128//! evaluates the soft linear repulsive score, for spheres s0 and s1,
129//! outputting derivatives to ds0 and ds1 scaled by da, if da is not null
130inline double
132(algebra::Sphere3D const& s0,
133 algebra::Sphere3D const& s1,
136 DerivativeAccumulator *da) const
137{
139 algebra::Vector3D delta =
140 s0.get_center() - s1.get_center();
141 double delta_length_2 =
142 delta.get_squared_magnitude(); // (work with sqr for efficiency)
143 double x0 =
144 s0.get_radius() + s1.get_radius();
145 double x0_2 = x0 * x0;
146 bool not_penetrating = delta_length_2 > x0_2;
147 if (not_penetrating) return 0;
148 // penetrating spheres ; -k_ in order to get a repulsive score
149 double delta_length = std::sqrt(delta_length_2);
150 return do_evaluate_index(ds0, ds1, da,
151 delta, delta_length, x0, -k_);
152}
153
154// evaluates the soft linear repulsive score, using the internal Model
155// indexing to access particles data (see Model/include/internal/)
156inline double
157LinearSoftSpherePairScore::evaluate_index
158( Model *m,
159 const ParticleIndexPair &pp,
160 DerivativeAccumulator *da) const
161{
163 algebra::Sphere3D const& s0(m->get_sphere(pp[0]));
164 algebra::Sphere3D const& s1(m->get_sphere(pp[1]));
165 algebra::Sphere3D* d_xyzrs=
166 m->access_sphere_derivatives_data();
167 algebra::Sphere3D& ds0( d_xyzrs[pp[0].get_index()] );
168 algebra::Sphere3D& ds1( d_xyzrs[pp[1].get_index()] );
169 return evaluate_index(s0, s1, ds0, ds1, da);
170}
171
172//! evaluate all index pairs between pis[lower_bound] and pis[upper_bound]
173//! and update their coordinate derivatives scaled by da, if da is not null
174inline double
175LinearSoftSpherePairScore::evaluate_indexes
176( Model *m,
177 const ParticleIndexPairs &pips,
178 DerivativeAccumulator *da,
179 unsigned int lower_bound,
180 unsigned int upper_bound, bool) const
181{
183 algebra::Sphere3D const* xyzrs=
184 m->access_spheres_data();
185 algebra::Sphere3D* d_xyzrs=
186 m->access_sphere_derivatives_data();
187 double ret = 0;
188 for (unsigned int i = lower_bound; i < upper_bound; ++i) {
189 int i0(pips[i][0].get_index());
190 int i1(pips[i][1].get_index());
191 ret += evaluate_index( xyzrs[i0], xyzrs[i1],
192 d_xyzrs[i0], d_xyzrs[i1],
193 da);
194 }
195 return ret;
196}
197
198#endif
199
200/**
201 A soft linear attractive / repulsive score between two spheres.
202 The score is 0 if the spheres are beyond the attractive range.
203 Within the attractive range, the score decreases linearly (= attraction)
204 with slope k_attr_ until the spheres touch. Once the spheres begin to
205 penetrate each other, the score rises linearly with slope k_rep_
206 (= repulsion), though it may be negative for small penetration.
207*/
208class IMPNPCTRANSPORTEXPORT LinearInteractionPairScore : public PairScore {
209#ifndef SWIG
210 public:
211 /** cache for reusable intermediate calculation */
213 // the squared-distance between particle centers:
214 double particles_delta_squared;
215 // the sum of particle radii:
216 double sum_particles_radii;
217 };
218#endif
219
220 private:
221 double range_attr_, // range of attraction between particles
222 k_rep_, // repulsion coeficcient
223 k_attr_; // attraction coefficient
224
225 // cache for reusable intermediate calculations of evaluate_infex
226 mutable EvaluationCache cache_;
227
228 protected:
229 //! evaluates the linear interaction score, for spheres s0 and s1,
230 //! outputting derivatives to ds0 and ds1 scaled by da, if da is not null
231 inline double evaluate_index
232 (algebra::Sphere3D const& s0,
233 algebra::Sphere3D const& s1,
236 DerivativeAccumulator *da) const;
237
238 public:
239 /**
240 The score is 0 if the spheres are beyond the attractive range.
241 Within the attractive range, the score decreases linearly (= attraction)
242 with slope k_attr_ until the spheres touch. Once the spheres begin to
243 penetrate each other, the score rises linearly with slope k_rep_
244 (= repulsion), though it may be negative for small penetration.
245
246 The energy potential difference between the unbound state and when the
247 beads touch is:
248 DELTA-U=0.5*k_attr*range_attr [kcal/mol]
249
250 @param k_rep repulsion force constant in kcal/mol/A units
251 @param range_attr attraction range between sphere surfaces
252 @param k_attr attraction force constant in kcal/mol/A unit
253 @param name score object names
254 */
255 LinearInteractionPairScore(double k_rep, double range_attr, double k_attr,
256 std::string name = "LinearIDPairScore%1%");
257
258#ifndef SWIG
259 // returns cached intermediate computations from last call to
260 // this->evaluate_index()
261 EvaluationCache const &get_evaluation_cache() const { return cache_; }
262#endif
263
264 IMP_IMPLEMENT(double evaluate_index(Model *m, const ParticleIndexPair &p,
265 DerivativeAccumulator *da) const
266 override);
267 IMP_IMPLEMENT_INLINE(double evaluate_if_good_index
268 ( Model *m,
269 const ParticleIndexPair &p,
271 double max ) const,
272 {
273 IMP_UNUSED(max);
274 return evaluate_index(m, p, da);
275 }
276 );
277
278 //! evaluate all index pairs between pips[lower_bound] and pis[upper_bound]
279 //! and update their coordinate derivatives scaled by da, if da is not null
280 virtual double evaluate_indexes(Model *m,
281 const ParticleIndexPairs &pips,
283 unsigned int lower_bound,
284 unsigned int upper_bound,
285 bool all_indexes_checked=false) const override;
286
287 double evaluate_if_good_index(Model *m, const ParticleIndexPairs &p,
288 DerivativeAccumulator *da, double max,
289 unsigned int lower_bound,
290 unsigned int upper_bound) const {
291 double ret = 0;
292 for (unsigned int i = lower_bound; i < upper_bound; ++i) {
293 ret += evaluate_if_good_index(m, p[i], da, max - ret);
294 if (ret > max) return std::numeric_limits<double>::max();
295 }
296 return ret;
297 }
298
300 override;
301
302 //! returns the range for sphere-sphere attraction in A
303 double get_range_attraction() const { return range_attr_; }
304
305 //! returns the k for sphere-sphere attraction in kcal/mol/A units
306 double get_k_attraction() const { return k_attr_; }
307
308 //! set k for sphere-sphere attraction in kcal/mol/A units
309 void set_k_attraction(double k_attr) {
310 k_attr_= k_attr;
311 }
312
313 //! returns the k for sphere-sphere repulsion in kcal/mol/A units
314 double get_k_repulsion() const { return k_rep_; }
315
316 //! set k for sphere-sphere repulsion in kcal/mol/A units
317 void set_k_repulsion(double k_rep) {
318 k_rep_ = k_rep;
319 }
320
322};
323
324#ifndef IMP_DOXYGEN
325
326//! evaluates the linear interaction score, for spheres s0 and s1,
327//! outputting derivatives to ds0 and ds1 scaled by da, if da is not null
328inline double
330(algebra::Sphere3D const& s0,
331 algebra::Sphere3D const& s1,
334 DerivativeAccumulator *da) const
335{
336 // Associate intermediate variables with cache_, for further reuse:
337 double &delta_length_2 = cache_.particles_delta_squared;
338 double &x0 = cache_.sum_particles_radii;
339 algebra::Vector3D delta =
340 s0.get_center() - s1.get_center();
341 delta_length_2 = delta.get_squared_magnitude();
342 IMP_LOG(PROGRESS,
343 "LinearInteractionPairScore cached delta2 "
344 << cache_.particles_delta_squared << std::endl);
345 x0 = s0.get_radius() + s1.get_radius();
346 // Terminate immediately if very far, work with squares for speed
347 // equivalent to [delta_length > x0 + attr_range]:
348 if (delta_length_2 > std::pow(x0 + range_attr_, 2)) return 0;
349 double offset = -range_attr_ * k_attr_;
350 double delta_length = std::sqrt(delta_length_2);
351 if (delta_length > x0) { // attractive regime
352 return // decreases with slope k_attr_ as spheres get closer
353 (do_evaluate_index(ds0, ds1, da,
354 delta, delta_length, x0, k_attr_) +
355 offset);
356 } else { // repulsive regime, may be negative for small penetration
357 return (do_evaluate_index(ds0, ds1, da,
358 delta, delta_length, x0, -k_rep_) +
359 offset);
360 }
361}
362
363inline double
364LinearInteractionPairScore::evaluate_index
365( Model *m,
366 const ParticleIndexPair &pp,
367 DerivativeAccumulator *da) const
368{
370 algebra::Sphere3D const& s0(m->get_sphere(pp[0]));
371 algebra::Sphere3D const& s1(m->get_sphere(pp[1]));
372 algebra::Sphere3D* d_xyzrs=
373 m->access_sphere_derivatives_data();
374 algebra::Sphere3D& ds0( d_xyzrs[pp[0].get_index()] );
375 algebra::Sphere3D& ds1( d_xyzrs[pp[1].get_index()] );
376 return evaluate_index(s0, s1, ds0, ds1, da);
377 }
378
379inline double
380LinearInteractionPairScore::evaluate_indexes
381( Model *m,
382 const ParticleIndexPairs &pips,
383 DerivativeAccumulator *da,
384 unsigned int lower_bound,
385 unsigned int upper_bound, bool) const
386{
388 algebra::Sphere3D const* xyzrs=
389 m->access_spheres_data();
390 algebra::Sphere3D* d_xyzrs=
391 m->access_sphere_derivatives_data();
392 double ret = 0;
393 for (unsigned int i = lower_bound; i < upper_bound; ++i) {
394 int i0(pips[i][0].get_index());
395 int i1(pips[i][1].get_index());
396 ret += evaluate_index( xyzrs[i0], xyzrs[i1],
397 d_xyzrs[i0], d_xyzrs[i1],
398 da);
399 }
400 return ret;
401}
402
403
404#endif
405
406/**
407 A soft linear attractive / repulsive score between two spheres
408 for the backbone of a chain.
409*/
410class IMPNPCTRANSPORTEXPORT LinearWellPairScore : public PairScore {
411 private:
412 double rest_length_factor_, k_;
413
414 public:
415 /**
416 a linear well pair potential that keeps two particles around
417 a resting distance relative to their radii
418
419 @param rest_length_factor the resting distance between particles
420 relative to their sum of radii
421 @param k the force constant (attractive if beyond or repulsive
422 if below rest length) in units kcal/mol/A
423 @param name the name of the score
424 */
425 LinearWellPairScore(double rest_length_factor, double k,
426 std::string name = "LinearIDPairScore%1%");
427
428 void set_rest_length_factor(double rest_length_factor)
429 { rest_length_factor_ = rest_length_factor; }
430 double get_rest_length_factor() const { return rest_length_factor_; }
431 void set_k(double k)
432 { k_ = k; }
433 double get_k() { return k_; }
435 DerivativeAccumulator *da) const override;
437 const ParticleIndexes &pis) const override;
440 ;
441};
442
443#ifndef IMP_DOXYGEN
444inline double
446( Model *m,
447 const ParticleIndexPair &pp,
448 DerivativeAccumulator *da) const
449{
451 algebra::Sphere3D const& s0 = m->get_sphere(pp[0]);
452 algebra::Sphere3D const& s1 = m->get_sphere(pp[1]);
453 algebra::Sphere3D* d_xyzrs=
454 m->access_sphere_derivatives_data();
455 algebra::Sphere3D& ds0( d_xyzrs[pp[0].get_index()] );
456 algebra::Sphere3D& ds1( d_xyzrs[pp[1].get_index()] );
457 double x0 = (s0.get_radius() + s1.get_radius()) * rest_length_factor_;
458 algebra::Vector3D delta = s0.get_center() - s1.get_center();
459 double delta_length_2 = delta.get_squared_magnitude();
460 double delta_length = std::sqrt(delta_length_2);
461 if (delta_length > x0) { // attractive regime
462 return // k_ > 0 = get spheres closer
463 do_evaluate_index(ds0, ds1, da,
464 delta, delta_length, x0, k_);
465 } else {
466 return// -k_ < 0 = keep spheres apart
467 do_evaluate_index(ds0, ds1, da,
468 delta, delta_length, x0, -k_);
469 }
470}
471#endif
472
474
475
476IMPNPCTRANSPORT_END_NAMESPACE
477
478#endif /* IMPNPCTRANSPORT_LINEAR_DISTANCE_PAIR_SCORES_H */
Define PairScore.
Decorator for a sphere-like particle.
Functions to deal with very common math operations.
#define IMP_CHECK_CODE(expr)
Only compile the code if checks are enabled.
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_if_good_index(Model *m, const ParticleIndexPair &vt, DerivativeAccumulator *da, double max) const
Compute the score and the derivative if needed, only if "good".
LinearInteractionPairScore(double k_rep, double range_attr, double k_attr, std::string name="LinearIDPairScore%1%")
double evaluate_index(algebra::Sphere3D const &s0, algebra::Sphere3D const &s1, algebra::Sphere3D &ds0, algebra::Sphere3D &ds1, DerivativeAccumulator *da) const
double get_range_attraction() const
returns the range for sphere-sphere attraction in A
double get_k_attraction() const
returns the k for sphere-sphere attraction in kcal/mol/A units
double get_k_repulsion() const
returns the k for sphere-sphere repulsion in kcal/mol/A units
virtual double evaluate_indexes(Model *m, const ParticleIndexPairs &pips, DerivativeAccumulator *da, unsigned int lower_bound, unsigned int upper_bound, bool all_indexes_checked=false) const override
void set_k_repulsion(double k_rep)
set k for sphere-sphere repulsion in kcal/mol/A units
ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pis) const override
Overload this method to specify the inputs.
void set_k_attraction(double k_attr)
set k for sphere-sphere attraction in kcal/mol/A units
virtual double evaluate_indexes(Model *m, const ParticleIndexPairs &pips, DerivativeAccumulator *da, unsigned int lower_bound, unsigned int upper_bound, bool all_indexes_checked=false) const override
Compute the score and the derivative if needed over a set.
double get_k() const
returns the k for sphere-sphere repulsion
double evaluate_if_good_indexes(Model *m, const ParticleIndexPairs &p, DerivativeAccumulator *da, double max, unsigned int lower_bound, unsigned int upper_bound, bool all_indexes_checked=false) const override
double evaluate_index(algebra::Sphere3D const &s0, algebra::Sphere3D const &s1, algebra::Sphere3D &ds0, algebra::Sphere3D &ds1, DerivativeAccumulator *da) const
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, const ParticleIndexPair &p, DerivativeAccumulator *da) const override
Compute the score and the derivative if needed.
double evaluate_index(Model *m, const ParticleIndexPair &p, DerivativeAccumulator *da) const override
Compute the score and the derivative if needed.
LinearWellPairScore(double rest_length_factor, double k, std::string name="LinearIDPairScore%1%")
ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pis) const override
Overload this method to specify the inputs.
#define IMP_IMPLEMENT(signature)
#define IMP_IMPLEMENT_INLINE(signature, body)
#define IMP_OBJECT_LOG
Set the log level to the object's log level.
Definition log_macros.h:284
SphereD< 3 > Sphere3D
Typedef for Python.
Definition SphereD.h:103
Ints get_index(const ParticlesTemp &particles, const Subset &subset, const Subsets &excluded)
IMP::Vector< IMP::Pointer< LinearWellPairScore > > LinearWellPairScores
double do_evaluate_index(algebra::Sphere3D &d_xyzr0, algebra::Sphere3D &d_xyzr1, DerivativeAccumulator *da, const algebra::Vector3D &delta, double delta_length, double x0, double k)
IMP::Vector< ParticleIndexPair, std::allocator< ParticleIndexPair > > ParticleIndexPairs
Definition base_types.h:208
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
@ TERSE
Output a line or two per evaluation call.
Definition enums.h:31
@ PROGRESS
Definition enums.h:29
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing lists of object pointers.
#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
#define IMP_UNUSED(variable)