IMP logo
IMP Reference Guide  2.22.0
The Integrative Modeling Platform
HarmonicWell.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/HarmonicWell.h \brief Harmonic function.
3  *
4  * Copyright 2007-2022 IMP Inventors. All rights reserved.
5  */
6 
7 #ifndef IMPCORE_HARMONIC_WELL_H
8 #define IMPCORE_HARMONIC_WELL_H
9 
10 #include <IMP/core/core_config.h>
11 #include <IMP/UnaryFunction.h>
12 #include <IMP/utility.h>
13 
14 IMPCORE_BEGIN_NAMESPACE
15 
16 //! A well with harmonic barriers
17 /** The well is defined by a center, a width and a k. The score is
18  0 if the feature is within the width/2.0. Outside of the width
19  the score is a harmonic.
20  \see TruncatedHarmonic
21  \see Harmonic
22  \see HarmonicUpperBound
23  \see HarmonicLowerBound
24  */
25 class HarmonicWell : public UnaryFunction {
26  double get_score(double x) const {
27  if (x < lb_)
28  return .5 * k_ * square(x - lb_);
29  else if (x > ub_)
30  return .5 * k_ * square(x - ub_);
31  else
32  return 0;
33  }
34  double get_derivative(double x) const {
35  if (x < lb_)
36  return k_ * (x - lb_);
37  else if (x > ub_)
38  return k_ * (x - ub_);
39  else
40  return 0;
41  }
42 
43  public:
44  //! Initialize with the lower and upper bounds and the spring constant
45  HarmonicWell(const FloatRange& well, double k)
46  : lb_(well.first), ub_(well.second), k_(k) {
47  IMP_USAGE_CHECK(well.first <= well.second,
48  "The width should be non-negative");
49  IMP_USAGE_CHECK(k >= 0, "The k should be non-negative");
50  }
51 
53  double feature) const override {
54  return DerivativePair(get_score(feature), get_derivative(feature));
55  }
56 
57  virtual double evaluate(double feature) const override {
58  return get_score(feature);
59  }
60 
62 
63  private:
64  double lb_, ub_, k_;
65 };
66 
68 
69 IMPCORE_END_NAMESPACE
70 
71 #endif /* IMPCORE_HARMONIC_WELL_H */
virtual DerivativePair evaluate_with_derivative(double feature) const override
Calculate score and derivative with respect to the given feature.
Definition: HarmonicWell.h:52
virtual double evaluate(double feature) const override
Calculate score with respect to the given feature.
Definition: HarmonicWell.h:57
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Single variable function.
std::pair< Float, Float > FloatRange
A pair representing the allowed range for a Float attribute.
Definition: types.h:30
A more IMP-like version of the std::vector.
Definition: Vector.h:50
A well with harmonic barriers.
Definition: HarmonicWell.h:25
HarmonicWell(const FloatRange &well, double k)
Initialize with the lower and upper bounds and the spring constant.
Definition: HarmonicWell.h:45
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
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing lists of object pointers.
Definition: object_macros.h:44
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
Abstract single variable functor class for score functions.
Definition: UnaryFunction.h:27