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