IMP logo
IMP Reference Guide  2.17.0
The Integrative Modeling Platform
DerivativeAccumulator.h
Go to the documentation of this file.
1 /**
2  * \file IMP/DerivativeAccumulator.h
3  * \brief Class for adding derivatives from restraints to the model.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPKERNEL_DERIVATIVE_ACCUMULATOR_H
10 #define IMPKERNEL_DERIVATIVE_ACCUMULATOR_H
11 
12 #include <IMP/kernel_config.h>
13 #include <IMP/showable_macros.h>
14 #include <IMP/check_macros.h>
15 #include <IMP/value_macros.h>
16 #include <IMP/math.h>
17 #include <IMP/exception.h>
18 
19 IMPKERNEL_BEGIN_NAMESPACE
20 
21 //! Class for adding derivatives from restraints to the model.
22 /** This class was created so that restraints can be weighted using
23  a RestraintSet and that the derivatives would be scaled appropriately */
24 class IMPKERNELEXPORT DerivativeAccumulator {
25  public:
26  IMP_CXX11_DEFAULT_COPY_CONSTRUCTOR(DerivativeAccumulator);
27  //! the weight is one by default
28  DerivativeAccumulator(double weight = 1.0) : weight_(weight) {}
29 
30  //! The weight is multiplied by the new weight
31  DerivativeAccumulator(const DerivativeAccumulator &copy, double weight)
32  : weight_(copy.weight_ * weight) {}
33 
34  //! Scale a value appropriately.
35  /** \param[in] value Value to add to the float attribute derivative.
36  */
37  double operator()(const double value) const {
38  IMP_INTERNAL_CHECK(!isnan(value), "Can't set derivative to NaN.");
39  return value * weight_;
40  }
41  double get_weight() const { return weight_; }
42  IMP_SHOWABLE_INLINE(DerivativeAccumulator, out << weight_);
43 
44  private:
45  double weight_;
46 };
47 
49 
50 IMPKERNEL_END_NAMESPACE
51 
52 #endif /* IMPKERNEL_DERIVATIVE_ACCUMULATOR_H */
Helper functions to check for NaN or infinity.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
double operator()(const double value) const
Scale a value appropriately.
Exception definitions and assertions.
A more IMP-like version of the std::vector.
Definition: Vector.h:40
DerivativeAccumulator(const DerivativeAccumulator &copy, double weight)
The weight is multiplied by the new weight.
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Definition: check_macros.h:139
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
bool isnan(const T &a)
Return true if a number is NaN.
Definition: math.h:24
Helper macros for throwing and handling exceptions.
DerivativeAccumulator(double weight=1.0)
the weight is one by default
Macros to help in implementing Value objects.
Macros to help with objects that can be printed to a stream.
Class for adding derivatives from restraints to the model.