FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
analytic_function_operator.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
8#include <kernel/analytic/function.hpp>
9
10#include <deque>
11
12namespace FEAT
13{
14 namespace Solver
15 {
36 template<typename DT_, typename IT_, typename Function_>
38 {
39 public:
41 static_assert(std::is_same<typename Function_::ImageType, Analytic::Image::Scalar>::value,
42 "AnalyticFunctionOperator is implemented for scalar functions only");
43
45 typedef DT_ DataType;
47 typedef IT_ IndexType;
48
50 typedef Function_ FunctionType;
51
53 static constexpr bool can_hess = FunctionType::can_hess;
55 static constexpr bool can_diag_hess = can_hess;
56
60 typedef typename Function_::template Evaluator<FuncEvalTraits> EvalType;
62 typedef typename EvalType::PointType PointType;
63
65 static constexpr int dim = PointType::n;
66
74 typedef typename EvalType::HessianType HessianType;
75
76 private:
78 Function_& _function;
81
85 typename EvalType::GradientType _my_grad;
87 typename EvalType::HessianType _my_hess;
88
95
96 public:
104 explicit AnalyticFunctionOperator(Function_& func) :
105 _function(func),
106 _func_eval(func),
107 _my_state(DT_(0)),
108 _my_grad(DT_(0)),
109 _my_hess(DT_(0)),
113 {
114 }
115
116 static String name()
117 {
118 return "AnalyticFunctionOperator";
119 }
120
127 {
128 return VectorTypeL(IT_(1));
129 }
130
137 {
138 return VectorTypeR(IT_(1));
139 }
140
149 {
150 return Index(dim);
151 }
152
161 {
162 return Index(dim);
163 }
164
169 {
170 return _num_func_evals;
171 }
172
177 {
178 return _num_grad_evals;
179 }
180
185 {
186 return _num_hess_evals;
187 }
188
193 {
197 }
198
209 template<typename FilterType_>
210 void prepare(const VectorTypeR& vec_state, FilterType_ DOXY(filter))
211 {
212 _my_state = vec_state.elements_view_r()(0);
213 }
214
224 void eval_fval_grad(typename EvalType::ValueType& fval, GradientType& grad)
225 {
228 fval = _func_eval.value(_my_state);
229 _my_grad = _func_eval.gradient(_my_state);
230 grad.elements_view_w()[0] = _my_grad;
231 }
232
242 void apply_hess(VectorTypeL& vec_out, const VectorTypeR& vec_in)
243 {
245 _my_hess = _func_eval.hessian(_my_state);
246 vec_out.elements_view_w()[0] = _my_hess*vec_in.elements_view_r()(0);
247 }
248
256 {
258 mat_out = _func_eval.hessian(_my_state);
259 }
260
268 {
270 mat_out = _func_eval.hessian(_my_state);
271 for(int i(1); i < mat_out.m; ++i)
272 {
273 for(int j(0); j < i; ++j)
274 {
275 mat_out(i,j) = DT_(0);
276 mat_out(j,i) = DT_(0);
277 }
278 }
279 }
280 };
281 } // namespace Solver
282} // namespace FEAT
FEAT Kernel base header.
Blocked Dense data vector class template.
Wrapper class defining an operator based on a scalar AnalyticFunction.
PointType _my_state
The current point, the function and its gradient/hessian are evaluated here.
Function_ & _function
The Analytic::Function.
EvalType::HessianType _my_hess
Temporary variable to save the current hessian or other matrices.
Index _num_func_evals
Counter for number of function evaluations.
void reset_num_evals()
Resets all evaluation counts.
void apply_hess(VectorTypeL &vec_out, const VectorTypeR &vec_in)
Applies the Hessian of the operator at the current state to a vector.
LAFEM::DenseVectorBlocked< DT_, IT_, dim > VectorTypeL
Output vector for the operator's gradient.
EvalType::HessianType HessianType
Output matrix for the operator's hessian.
Analytic::EvalTraits< DT_, Function_ > FuncEvalTraits
Class that knows what Function_ wants as input parameters for its evaluator etc.
AnalyticFunctionOperator(Function_ &func)
The one and only sensible constructor.
EvalType::GradientType _my_grad
Temporary variable to save the current gradient or other vectors.
Index _num_grad_evals
Counter for number of gradient evaluations.
void eval_fval_grad(typename EvalType::ValueType &fval, GradientType &grad)
Evaluates the gradient of the operator at the current state.
Index num_cols()
The number of input variables for the operator and its gradient.
static constexpr int dim
Dimension each variable is from.
static constexpr bool can_hess
Can the operator compute its full Hessian?
EvalType::PointType PointType
Input type for Function_.
void compute_hess(HessianType &mat_out)
Evaluates the Hessian of the operator at the current state.
LAFEM::DenseVectorBlocked< DT_, IT_, dim > VectorTypeR
Input vector for the operator and its gradient.
Index num_rows()
The number of output variables of the operator's gradient.
void prepare(const VectorTypeR &vec_state, FilterType_ filter)
Prepares the operator for evaluation by setting the current state.
Function_::template Evaluator< FuncEvalTraits > EvalType
Type of Function_'s evaluator.
void compute_approx_hess(HessianType &mat_out)
Evaluates the Hessian of the operator at the current state.
static constexpr bool can_diag_hess
Can the operator compute the diagonal of its Hessian?
VectorTypeR create_vector_r() const
Creates an empty R-vector of appropriate size.
Function_ FunctionType
The type of the underlying analytic function.
VectorTypeL create_vector_l() const
Creates an empty L-vector of appropriate size.
VectorTypeR GradientType
Output vector for the operator's gradient.
Index _num_hess_evals
Counter for number of hessian evaluations.
String class implementation.
Definition: string.hpp:47
FEAT namespace.
Definition: adjactor.hpp:12
@ grad
specifies whether the space should supply basis function gradients
std::uint64_t Index
Index data type.