IMP logo
IMP Reference Guide  develop.5651aa123e,2024/07/20
The Integrative Modeling Platform
core/Harmonic.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/Harmonic.h \brief Harmonic function.
3  *
4  * Copyright 2007-2022 IMP Inventors. All rights reserved.
5  */
6 
7 #ifndef IMPCORE_HARMONIC_H
8 #define IMPCORE_HARMONIC_H
9 
10 #include <IMP/core/core_config.h>
11 #include <IMP/UnaryFunction.h>
12 #include <IMP/utility.h>
13 #include <cereal/access.hpp>
14 #include <cereal/types/base_class.hpp>
15 #include <cereal/types/polymorphic.hpp>
16 
17 IMPCORE_BEGIN_NAMESPACE
18 
19 //! %Harmonic function (symmetric about the mean)
20 /** This is a simple score modeling an harmonic oscillator. The score is
21  0.5 * k * x * x, where k is the 'force constant' and x is the distance
22  from the mean.
23  \see TruncatedHarmonic
24  \see HarmonicUpperBound
25  \see HarmonicLowerBound
26  */
27 class Harmonic : public UnaryFunction {
28  public:
29  /** Create with the given mean and the spring constant k */
30  Harmonic(Float mean, Float k) : mean_(mean), k_(k) {}
31  Harmonic() {}
32 
34  double feature) const override {
35  return DerivativePair(0.5 * k_ * square(feature - mean_),
36  k_ * (feature - mean_));
37  }
38 
39  virtual double evaluate(double feature) const override {
40  return 0.5 * k_ * square(feature - mean_);
41  }
42 
44 
45  //! Return the mean of this function
46  Float get_mean() const { return mean_; }
47 
48  //! Return the spring constant
49  Float get_k() const { return k_; }
50 
51  //! Set the mean of this function
52  void set_mean(Float mean) { mean_ = mean; }
53 
54  //! Set the spring constant
55  void set_k(Float k) { k_ = k; }
56 
57  //! Return the k to use for a given %Gaussian standard deviation.
58  /** Given the standard deviation of a %Gaussian distribution, get
59  the force constant of the harmonic score function that yields that
60  same distribution. For temperature in Kelvin, this assumes the score
61  function is energy in kcal/mol, and thus returns a force constant in
62  kcal/mol/A/A.
63  \param[in] sd %Gaussian standard deviation, in angstroms
64  \param[in] t System temperature, in Kelvin
65  \return Force constant
66  */
68  // Gas constant in kcal/mol K
69  const static Float R = 8.31441 / 4186.8;
70  return R * t / square(sd);
71  }
72 
73  private:
74  Float mean_;
75  Float k_;
76 
77  friend class cereal::access;
78 
79  template<class Archive> void serialize(Archive &ar) {
80  ar(cereal::base_class<UnaryFunction>(this), mean_, k_);
81  }
82  IMP_OBJECT_SERIALIZE_DECL(Harmonic);
83 };
84 
85 IMPCORE_END_NAMESPACE
86 
87 #endif /* IMPCORE_HARMONIC_H */
Float get_k() const
Return the spring constant.
Definition: core/Harmonic.h:49
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Single variable function.
virtual DerivativePair evaluate_with_derivative(double feature) const override
Calculate score and derivative with respect to the given feature.
Definition: core/Harmonic.h:33
void set_k(Float k)
Set the spring constant.
Definition: core/Harmonic.h:55
void set_mean(Float mean)
Set the mean of this function.
Definition: core/Harmonic.h:52
static Float get_k_from_standard_deviation(Float sd, Float t=297.15)
Return the k to use for a given Gaussian standard deviation.
Definition: core/Harmonic.h:67
virtual double evaluate(double feature) const override
Calculate score with respect to the given feature.
Definition: core/Harmonic.h:39
#define IMP_OBJECT_SERIALIZE_DECL(Name)
Declare methods needed for serialization of Object pointers.
Definition: object_macros.h:95
Various general useful functions for IMP.
std::pair< double, double > DerivativePair
A pair representing a function value with its first derivative.
Definition: types.h:22
Float get_mean() const
Return the mean of this function.
Definition: core/Harmonic.h:46
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:19
Harmonic(Float mean, Float k)
Definition: core/Harmonic.h:30
Abstract single variable functor class for score functions.
Definition: UnaryFunction.h:27
Harmonic function (symmetric about the mean)
Definition: core/Harmonic.h:27