Flow123d  jenkins-Flow123d-linux-release-multijob-198
functors.hh
Go to the documentation of this file.
1 #ifndef FUNCTORBASE_H
2 #define FUNCTORBASE_H
3 
4 //include thirdparty/FADBAD++
5 #include "fadbad.h"
6 #include "badiff.h"
7 #include "tadiff.h"
8 
9 using namespace fadbad;
10 
11 ///Class provides common functionality for functors.
12 /** This class provides methods for reading and setting functors
13  * parameters.
14  *
15  * User is adviced to create Enumeration to access parameters.
16  * For example usage see class \ref FunctorBase.
17  *
18  * One can also use \ref set_param_from_func
19  * to copy all parameters from another functor.
20  */
21 template<class Type>
23 {
24 public:
25  ///Constructor.
26  FunctorCommon();
27 
28  ///Destructor.
29  virtual ~FunctorCommon();
30 
31  ///Sets a functor's parameter.
32  /**There is a vector for the functors parameters.
33  * The vector of parameters is addressed by integer, but one can
34  * use enum inside one's own functor to make it more convenient.
35  * e.g. \code `set_param(MyFunctor::my_parameter, value)` \endcode.
36  * @param param_name is integer value (or enum value) to access member of the parameter vector
37  * @param param_value is the new value of selected parameter
38  */
39  void set_param(unsigned int param_name, double param_value);
40 
41  ///Sets a functor's parameters from another functor.
42  /** Copies all the parameters defined in the given functor.
43  * @param func is pointer to a functor we want to copy from
44  */
45  template<class TType>
46  void set_param_from_func(FunctorCommon<TType>* func);
47 
48  ///Returns parameter.
49  /** @param param_name is integer value (or enum value) to access member of the parameter vector
50  */
51  double param(unsigned int param_name);
52 
53 protected:
55 
56  ///Setting these friend classes to be able to access param_ vector.
57  template<class U> friend class FunctorCommon;
58 };
59 
60 
61 ///Abstract templated explicit functor class.
62 /** This class represents an explicit functor. Actual non-abstract functor
63  * is obtained by deriving from this class and overriding the operator().
64  * Parameters can be set and used by the methods of ancestor class FunctorCommon.
65  *
66  * The template is used to enable computing derivates by FADBAD++ library.
67  * The virtual operator() needs to operate both with type double and FADBAD++ types,
68  * such as `B<double>` and `T<double>`.
69  *
70  * Simple usage example for function \f$ x^3p_1+\frac{p_2}{p_3} \f$ with three parameters: \code
71 template<class Type=double>
72 class Cubic : public FunctorBase<Type>
73 {
74 public:
75  typedef enum{ p1, p2, p3
76  } Parameters;
77 
78  Cubic(double pp1, double pp2, double pp3)
79  {
80  this->set_param(p1, pp1);
81  this->set_param(p2, pp2);
82  this->set_param(p3, pp3);
83  }
84 
85  Type operator()(Type x)
86  {
87  return x*x*x * this->param(p1) + this->param(p2)/this->param(p3);
88  }
89 };
90  * \endcode
91  */
92 template<class Type>
93 class FunctorBase : public FunctorCommon<Type>
94 {
95 public:
96  ///Constructor.
97  FunctorBase();
98 
99  ///Destructor.
100  virtual ~FunctorBase();
101 
102  ///Virtual operator () with type @p Type.
103  virtual Type operator()(Type x) = 0;
104 };
105 
106 
107 
108 ///Abstract templated implicit functor class.
109 /**This class represents an implicit functor.
110  * It is used the same way as the class \ref FunctorBase,
111  * only the operator() has two arguments.
112  */
113 template<class Type>
114 class IFunctorBase : public FunctorCommon<Type>
115 {
116 public:
117  ///Constructor.
118  IFunctorBase();
119 
120  ///Destructor.
121  virtual ~IFunctorBase();
122 
123  ///Virtual operator () with type @p Type.
124  virtual Type operator()(Type x, Type y) = 0;
125 };
126 
127 #endif //FUNCTORBASE_H
std::vector< double > param_
Definition: functors.hh:54
Class provides common functionality for functors.
Definition: functors.hh:22
Abstract templated implicit functor class.
Definition: functors.hh:114
Abstract templated explicit functor class.
Definition: functors.hh:93