IMP logo
IMP Reference Guide  2.20.0
The Integrative Modeling Platform
harmonic_distance_pair_scores.h
Go to the documentation of this file.
1 /**
2  * \file harmonic_distance_pair_scores.h
3  * \brief Harmonic scores on the distance between a pair of particles.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPNPCTRANSPORT_HARMONIC_DISTANCE_PAIR_SCORES_H
9 #define IMPNPCTRANSPORT_HARMONIC_DISTANCE_PAIR_SCORES_H
10 
11 #include "npctransport_config.h"
12 #include <IMP/compiler_macros.h>
13 #include <IMP/PairScore.h>
14 #include <IMP/pair_macros.h>
15 #include <IMP/core/XYZR.h>
16 #include <IMP/algebra/utility.h>
17 
18 #include <boost/array.hpp>
19 
20 IMPNPCTRANSPORT_BEGIN_NAMESPACE
21 
22 /**
23  Evaluates a linear pair potential, such that force = k * (delta_length - x0)
24  is returned.
25  Also, updates the derivative vectors of the particles in the model m.
26 
27  @param[out] m a model for the particles, in which particle derivatives are
28  updated
29  @param[in] pp a pair of particle indices for fast access through internal
30  model methods
31  @param[in,out] da accumulator for score derivatives to be updated
32  @param[in] delta a vector from pp[1] to pp[0]
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  */
43  const algebra::Vector3D &delta,
44  double delta_length, double x0, double k) {
45  double shifted_length = delta_length - x0;
46  double score = 0.5 * k * shifted_length * shifted_length;
47  static const double MIN_DISTANCE = .00001;
48  if (IMP_LIKELY( da && delta_length > MIN_DISTANCE )) { // Profiling note on use of likely(): in BD simulations, the simulation bottleneck is when da is true, and the spring is likely out of equilibrium
49  algebra::Vector3D deriv = (k * shifted_length / delta_length) * delta; // deriv magnitude is k*shifted_length
50  m->add_to_coordinate_derivatives(pp[0], deriv, *da);
51  m->add_to_coordinate_derivatives(pp[1], -deriv, *da);
52  IMP_LOG(TERSE, "Distance: " << shifted_length << "\nscore: " << score
53  << "\nderiv: " << deriv << std::endl);
54  }
55  IMP_CHECK_CODE ( else {
56  IMP_LOG(TERSE, "Distance: " << shifted_length << "\nscore: " << score
57  << std::endl);
58  } );
59  return score;
60 }
61 
62 /**
63  A harmonic score between two bonded spheres with a certain
64  rest length and spring constant
65 */
66 class IMPNPCTRANSPORTEXPORT HarmonicWellPairScore : public PairScore {
67  private:
68  double rest_length_factor_, k_;
69 
70  public:
71  /**
72  a linear well pair potential that keeps two particles around
73  a resting distance relative to their radii
74 
75  @param rest_length_factor the resting distance between particles
76  relative to their sum of radii
77  @param k the force constant (attractive if beyond or repulsive
78  if below rest length) in units kcal/mol/A^2
79  @param name the name of the score
80  */
81  HarmonicWellPairScore(double rest_length_factor, double k,
82  std::string name = "HarmonicIDPairScore%1%");
83 
84  void set_rest_length_factor(double rest_length_factor)
85  { rest_length_factor_ = rest_length_factor; }
86  double get_rest_length_factor() const { return rest_length_factor_; }
87  void set_k(double k)
88  { k_ = k; }
89  double get_k() { return k_; }
90  double evaluate_index(Model *m, const ParticleIndexPair &p,
91  DerivativeAccumulator *da) const override;
93  const ParticleIndexes &pis) const override;
96  ;
97 };
98 
99 #ifndef IMP_DOXYGEN
100 inline double HarmonicWellPairScore::evaluate_index(
101  Model *m, const ParticleIndexPair &pp, DerivativeAccumulator *da) const {
103 
104  algebra::Sphere3D s0 = m->get_sphere(pp[0]);
105  algebra::Sphere3D s1 = m->get_sphere(pp[1]);
106  double x0 = (s0.get_radius() + s1.get_radius()) * rest_length_factor_;
107  algebra::Vector3D delta = s0.get_center() - s1.get_center();
108  double delta_length_2 = delta.get_squared_magnitude();
109  double delta_length = std::sqrt(delta_length_2);
110  return // k_ > 0 = get spheres closer
111  do_evaluate_index_harmonic(m, pp, da, delta, delta_length, x0, k_);
112 }
113 #endif
114 
116 
117 
118 IMPNPCTRANSPORT_END_NAMESPACE
119 
120 #endif /* IMPNPCTRANSPORT_HARMONIC_DISTANCE_PAIR_SCORES_H */
Abstract class for scoring object(s) of type ParticleIndexPair.
Definition: PairScore.h:44
Macros for various classes.
#define IMP_PAIR_SCORE_METHODS(Name)
Definition: pair_macros.h:25
#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
double do_evaluate_index_harmonic(Model *m, const ParticleIndexPair &pp, DerivativeAccumulator *da, const algebra::Vector3D &delta, double delta_length, double x0, double k)
A more IMP-like version of the std::vector.
Definition: Vector.h:42
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
Functions to deal with very common math operations.
Define PairScore.
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pis) const =0
Overload this method to specify the inputs.
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing lists of object pointers.
Definition: object_macros.h:44
#define IMP_CHECK_CODE(expr)
Only compile the code if checks are enabled.
Definition: check_macros.h:117
VectorD< 3 > Vector3D
Definition: VectorD.h:425
Various compiler workarounds.
Output a line or two per evaluation call.
Definition: enums.h:31
Decorator for a sphere-like particle.
virtual double evaluate_index(Model *m, const ParticleIndexPair &vt, DerivativeAccumulator *da) const =0
Compute the score and the derivative if needed.
Class for adding derivatives from restraints to the model.