FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
function.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
7
8// includes, FEAT
10#include <kernel/util/tiny_algebra.hpp>
11
12namespace FEAT
13{
17 namespace Analytic
18 {
20 namespace Image
21 {
27 struct Scalar
28 {
30 static constexpr bool is_scalar = true;
32 static constexpr bool is_vector = false;
34 static constexpr int scalar_components = 1;
35 };
36
45 template<int m_>
46 struct Vector
47 {
48 static_assert(m_ > 0, "invalid vector field dimension");
49
51 static constexpr bool is_scalar = false;
53 static constexpr bool is_vector = true;
55 static constexpr int scalar_components = m_;
56
58 static constexpr int image_dim = m_;
59 };
60 } // namespace Image
61
63 template<typename DataType_, int domain_dim_, typename ImageType_>
65
67 template<typename DataType_, int domain_dim_>
68 struct EvalTraitsBase<DataType_, domain_dim_, Image::Scalar>
69 {
70 typedef DataType_ DataType;
71 static constexpr int domain_dim = domain_dim_;
72 static constexpr int image_dim = 1;
73
75
76 typedef DataType_ ValueType;
79 };
80
82 template<typename DataType_, int domain_dim_, int image_dim_>
83 struct EvalTraitsBase<DataType_, domain_dim_, Image::Vector<image_dim_>>
84 {
85 typedef DataType_ DataType;
86 static constexpr int domain_dim = domain_dim_;
87 static constexpr int image_dim = image_dim_;
88
90
94 };
95
96 template<typename DataType_, typename Function_>
97 struct EvalTraits :
98 public EvalTraitsBase<DataType_, Function_::domain_dim, typename Function_::ImageType>
99 {
100 };
101
112 {
113 public:
114 // Note:
115 // The following block is only visible for doxygen. Its contents must be specified
116 // by the derived classes.
117#ifdef DOXYGEN
123 static constexpr int domain_dim = ...;
124
133 typedef ... ImageType;
134
136 static constexpr bool can_value = true or false;
138 static constexpr bool can_grad = true or false;
140 static constexpr bool can_hess = true or false;
141#else
142 static constexpr bool can_value = false;
143 static constexpr bool can_grad = false;
144 static constexpr bool can_hess = false;
145#endif // DOXYGEN
146
155 template<typename Traits_>
157 {
158 public:
165 typedef typename Traits_::DataType DataType;
166
173 typedef typename Traits_::PointType PointType;
174
183 typedef typename Traits_::ValueType ValueType;
184
192 typedef typename Traits_::GradientType GradientType;
193
201 typedef typename Traits_::HessianType HessianType;
202
203 public:
204#ifdef DOXYGEN
211 explicit Evaluator(const Function& function)
212 {
213 }
214
225 {
226 }
227
238 {
239 }
240
251 {
252 }
253#endif // DOXYGEN
254 }; // class Function::Evaluator<...>
255 }; // class Function
256
273 template<typename Function_, typename DT_, int dim_, int s_>
275 eval_value(const Function_& function, const Tiny::Vector<DT_, dim_, s_>& point)
276 {
278 static_assert(Function_::can_value, "function does not support evaluation of values");
279 static_assert(dim_ == Traits::domain_dim, "invalid point dimension");
280 typename Function_::template Evaluator<Traits> evaluator(function);
281 return evaluator.value(point);
282 }
283
284 // \see eval_value()
285 template<typename Function_, typename DT_>
287 eval_value_x(const Function_& function, const DT_ x)
288 {
290 p.v[0] = x;
291 return eval_value(function, p);
292 }
293
294 // \see eval_value()
295 template<typename Function_, typename DT_>
296 typename Analytic::EvalTraits<DT_, Function_>::ValueType
297 eval_value_x(const Function_& function, const DT_ x, const DT_ y)
298 {
299 Tiny::Vector<DT_, 2> p;
300 p.v[0] = x;
301 p.v[1] = y;
302 return eval_value(function, p);
303 }
304
305 // \see eval_value()
306 template<typename Function_, typename DT_>
307 typename Analytic::EvalTraits<DT_, Function_>::ValueType
308 eval_value_x(const Function_& function, const DT_ x, const DT_ y, const DT_ z)
309 {
310 Tiny::Vector<DT_, 3> p;
311 p.v[0] = x;
312 p.v[1] = y;
313 p.v[2] = z;
314 return eval_value(function, p);
315 }
316
333 template<typename Function_, typename DT_, int dim_, int s_>
334 typename Analytic::EvalTraits<DT_, Function_>::GradientType
335 eval_gradient(const Function_& function, const Tiny::Vector<DT_, dim_, s_>& point)
336 {
338 static_assert(Function_::can_grad, "function does not support evaluation of gradients");
339 static_assert(dim_ == Traits::domain_dim, "invalid point dimension");
340 typename Function_::template Evaluator<Traits> evaluator(function);
341 return evaluator.gradient(point);
342 }
343
344 // \see eval_gradient()
345 template<typename Function_, typename DT_>
347 eval_gradient_x(const Function_& function, const DT_ x)
348 {
350 p.v[0] = x;
351 return eval_gradient(function, p);
352 }
353
354 // \see eval_gradient()
355 template<typename Function_, typename DT_>
356 typename Analytic::EvalTraits<DT_, Function_>::GradientType
357 eval_gradient_x(const Function_& function, const DT_ x, const DT_ y)
358 {
359 Tiny::Vector<DT_, 2> p;
360 p.v[0] = x;
361 p.v[1] = y;
362 return eval_gradient(function, p);
363 }
364
365 // \see eval_gradient()
366 template<typename Function_, typename DT_>
367 typename Analytic::EvalTraits<DT_, Function_>::GradientType
368 eval_gradient_x(const Function_& function, const DT_ x, const DT_ y, const DT_ z)
369 {
370 Tiny::Vector<DT_, 3> p;
371 p.v[0] = x;
372 p.v[1] = y;
373 p.v[2] = z;
374 return eval_gradient(function, p);
375 }
376
393 template<typename Function_, typename DT_, int dim_, int s_>
394 typename Analytic::EvalTraits<DT_, Function_>::HessianType
395 eval_hessian(const Function_& function, const Tiny::Vector<DT_, dim_, s_>& point)
396 {
398 static_assert(Function_::can_hess, "function does not support evaluation of hessians");
399 static_assert(dim_ == Traits::domain_dim, "invalid point dimension");
400 typename Function_::template Evaluator<Traits> evaluator(function);
401 return evaluator.hessian(point);
402 }
403
404 // \see eval_hessian()
405 template<typename Function_, typename DT_>
407 eval_hessian_x(const Function_& function, const DT_ x)
408 {
410 p.v[0] = x;
411 return eval_hessian(function, p);
412 }
413
414 // \see eval_hessian()
415 template<typename Function_, typename DT_>
416 typename Analytic::EvalTraits<DT_, Function_>::HessianType
417 eval_hessian_x(const Function_& function, const DT_ x, const DT_ y)
418 {
419 Tiny::Vector<DT_, 2> p;
420 p.v[0] = x;
421 p.v[1] = y;
422 return eval_hessian(function, p);
423 }
424
425 // \see eval_hessian()
426 template<typename Function_, typename DT_>
427 typename Analytic::EvalTraits<DT_, Function_>::HessianType
428 eval_hessian_x(const Function_& function, const DT_ x, const DT_ y, const DT_ z)
429 {
430 Tiny::Vector<DT_, 3> p;
431 p.v[0] = x;
432 p.v[1] = y;
433 p.v[2] = z;
434 return eval_hessian(function, p);
435 }
436
437 } // namespace Analytic
438} // namespace FEAT
FEAT Kernel base header.
Analytic Function Evaluator base-class template.
Definition: function.hpp:157
ValueType value(const PointType &point)
Computes the function value in a given point.
Definition: function.hpp:224
GradientType gradient(const PointType &point)
Computes the function gradient in a given point.
Definition: function.hpp:237
Traits_::PointType PointType
The type of the domain evaluation point.
Definition: function.hpp:173
Traits_::HessianType HessianType
The type of the function hessian.
Definition: function.hpp:201
Traits_::GradientType GradientType
The type of the function gradient.
Definition: function.hpp:192
Traits_::DataType DataType
The underlying floating point data type.
Definition: function.hpp:165
Traits_::ValueType ValueType
The type of the function value.
Definition: function.hpp:183
Evaluator(const Function &function)
Mandatory constructor.
Definition: function.hpp:211
HessianType hessian(const PointType &point)
Computes the function hessian in a given point.
Definition: function.hpp:250
Analytic Function interface.
Definition: function.hpp:112
static constexpr bool can_grad
Specifies whether the function's evaluator can compute function gradients.
Definition: function.hpp:138
static constexpr bool can_hess
Specifies whether the function's evaluator can compute function hessians.
Definition: function.hpp:140
static constexpr bool can_value
Specifies whether the function's evaluator can compute function values.
Definition: function.hpp:136
typedef ImageType
Specifies the image type of the function.
Definition: function.hpp:133
static constexpr int domain_dim
Specifies the domain dimension of the function.
Definition: function.hpp:123
Tiny Matrix class template.
Tiny Tensor3 class template.
Tiny Vector class template.
Analytic::EvalTraits< DT_, Function_ >::GradientType eval_gradient(const Function_ &function, const Tiny::Vector< DT_, dim_, s_ > &point)
Helper function to quickly evaluate a function gradient in a given point.
Definition: function.hpp:335
Analytic::EvalTraits< DT_, Function_ >::HessianType eval_hessian(const Function_ &function, const Tiny::Vector< DT_, dim_, s_ > &point)
Helper function to quickly evaluate a function hessian in a given point.
Definition: function.hpp:395
Analytic::EvalTraits< DT_, Function_ >::ValueType eval_value(const Function_ &function, const Tiny::Vector< DT_, dim_, s_ > &point)
Helper function to quickly evaluate a function value in a given point.
Definition: function.hpp:275
FEAT namespace.
Definition: adjactor.hpp:12
analytic evaluation traits base-class
Definition: function.hpp:64
Scalar Function Image tag class.
Definition: function.hpp:28
static constexpr bool is_scalar
this is a scalar function
Definition: function.hpp:30
static constexpr bool is_vector
this is not a vector field
Definition: function.hpp:32
static constexpr int scalar_components
this function has 1 scalar component
Definition: function.hpp:34
Vector Field Image tag class.
Definition: function.hpp:47
static constexpr bool is_vector
this is a vector field
Definition: function.hpp:53
static constexpr int scalar_components
this function has m_ scalar components
Definition: function.hpp:55
static constexpr int image_dim
this is the image dimension of the vector field
Definition: function.hpp:58
static constexpr bool is_scalar
this is not a scalar function
Definition: function.hpp:51