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