9#include <kernel/analytic/function.hpp>
10#include <kernel/assembly/asm_traits.hpp>
11#include <kernel/util/dist.hpp>
12#include <kernel/util/tiny_algebra.hpp>
21 template<
bool h0_,
typename AsmTraits_,
typename AnaTraits_>
24 typedef typename AsmTraits_::DataType DataType;
26 template<
typename FuncEval_,
typename LocalVec_>
29 const typename AsmTraits_::TrafoEvalData&,
30 const typename AsmTraits_::SpaceEvalData&,
39 template<
typename AsmTraits_,
typename AnaTraits_>
40 struct SecHelperH0<true, AsmTraits_, AnaTraits_>
42 typedef typename AsmTraits_::DataType DataType;
44 template<
typename FuncEval_,
typename LocalVec_>
47 const typename AsmTraits_::TrafoEvalData& trafo_data,
48 const typename AsmTraits_::SpaceEvalData& space_data,
49 const LocalVec_& lvad,
50 const int num_loc_dofs
54 typename AnaTraits_::ValueType
value = func_eval.value(trafo_data.img_point);
57 for(
int i(0); i < num_loc_dofs; ++i)
58 value -= lvad[i] * space_data.phi[i].value;
61 return Math::abs(value);
65 template<
bool h1_,
typename AsmTraits_,
typename AnaTraits_,
bool sub_dim_>
68 typedef typename AsmTraits_::DataType DataType;
70 template<
typename FuncEval_,
typename LocalVec_>
73 const typename AsmTraits_::TrafoEvalData&,
74 const typename AsmTraits_::SpaceEvalData&,
84 template<
typename AsmTraits_,
typename AnaTraits_>
85 struct SecHelperH1<true, AsmTraits_, AnaTraits_, false>
87 typedef typename AsmTraits_::DataType DataType;
89 template<
typename FuncEval_,
typename LocalVec_>
92 const typename AsmTraits_::TrafoEvalData& trafo_data,
93 const typename AsmTraits_::SpaceEvalData& space_data,
94 const LocalVec_& lvad,
95 const int num_loc_dofs
99 typename AnaTraits_::GradientType
grad = func_eval.gradient(trafo_data.img_point);
102 for(
int i(0); i < num_loc_dofs; ++i)
103 grad.axpy(-lvad[i], space_data.phi[i].grad);
106 return grad.norm_euclid_sqr();
111 template<
typename AsmTraits_,
typename AnaTraits_>
112 struct SecHelperH1<true, AsmTraits_, AnaTraits_, true>
114 typedef typename AsmTraits_::DataType DataType;
116 template<
typename FuncEval_,
typename LocalVec_>
117 static DataType eval(
118 FuncEval_& func_eval,
119 const typename AsmTraits_::TrafoEvalData& trafo_data,
120 const typename AsmTraits_::SpaceEvalData& space_data,
121 const LocalVec_& lvad,
122 const int num_loc_dofs
126 static constexpr int dom_dim = AsmTraits_::domain_dim;
129 typename AnaTraits_::GradientType
grad = func_eval.gradient(trafo_data.img_point);
132 Tiny::Vector<DataType, dom_dim>
ref_grad;
133 ref_grad.set_vec_mat_mult(grad, trafo_data.jac_mat);
136 for(
int i(0); i < num_loc_dofs; ++i)
137 ref_grad.axpy(-lvad[i], space_data.phi[i].ref_grad);
140 Tiny::Matrix<DataType, dom_dim, dom_dim> gram_mat, gram_inv;
141 gram_mat.set_gram(trafo_data.jac_mat);
142 gram_inv.set_inverse(gram_mat);
145 return gram_inv.scalar_product(ref_grad, ref_grad);
149 template<
bool h2_,
typename AsmTraits_,
typename AnaTraits_>
152 typedef typename AsmTraits_::DataType DataType;
154 template<
typename FuncEval_,
typename LocalVec_>
155 static DataType eval(
157 const typename AsmTraits_::TrafoEvalData&,
158 const typename AsmTraits_::SpaceEvalData&,
167 template<
typename AsmTraits_,
typename AnaTraits_>
168 struct SecHelperH2<true, AsmTraits_, AnaTraits_>
170 typedef typename AsmTraits_::DataType DataType;
172 template<
typename FuncEval_,
typename LocalVec_>
173 static DataType eval(
174 FuncEval_& func_eval,
175 const typename AsmTraits_::TrafoEvalData& trafo_data,
176 const typename AsmTraits_::SpaceEvalData& space_data,
177 const LocalVec_& lvad,
178 const int num_loc_dofs
182 typename AnaTraits_::HessianType
hess = func_eval.hessian(trafo_data.img_point);
185 for(
int i(0); i < num_loc_dofs; ++i)
186 hess.axpy(-lvad[i], space_data.phi[i].hess);
189 return hess.norm_hessian_sqr();
201 template<
typename DataType_>
243 template<
typename DT2_>
259 template<
typename DT2_>
267 norm_h0 = DataType_(other.norm_h0);
268 norm_h1 = DataType_(other.norm_h1);
269 norm_h2 = DataType_(other.norm_h2);
270 norm_l1 = DataType_(other.norm_l1);
374 template<
int max_norm_ = 0,
bool sub_dimensional_ = false>
377 static_assert(max_norm_ >= 0,
"invalid max_norm_ parameter");
389 static constexpr SpaceTags space_config =
423 typename CubatureFactory_>
425 const Vector_& vector,
426 const Function_& function,
428 const CubatureFactory_& cubature_factory)
431 static_assert(Function_::can_value,
"function values are required for H0-Error");
432 static_assert(Function_::can_grad || (max_norm_ < 1),
"function gradients are required for H1-Error");
433 static_assert(Function_::can_hess || (max_norm_ < 2),
"function hessians are required for H2-Error");
436 XASSERTM(vector.size() == space.get_num_dofs(),
"invalid vector size");
439 typedef Vector_ VectorType;
441 typedef Function_ FunctionType;
443 typedef Space_ SpaceType;
447 typedef typename AsmTraits::DataType DataType;
450 typename AsmTraits::CubatureRuleType cubature_rule(Cubature::ctor_factory, cubature_factory);
453 const typename AsmTraits::TrafoType& trafo = space.get_trafo();
459 typename FunctionType::template Evaluator<AnalyticEvalTraits> func_eval(function);
462 typename AsmTraits::TrafoEvaluator trafo_eval(trafo);
465 typename AsmTraits::SpaceEvaluator space_eval(space);
468 typename AsmTraits::DofMapping dof_mapping(space);
471 typename AsmTraits::TrafoEvalData trafo_data;
474 typename AsmTraits::SpaceEvalData space_data;
477 typename AsmTraits::template TLocalVector<DataType> lvad;
480 typename VectorType::GatherAxpy gather_axpy(vector);
484 result.
have_h0 = (max_norm_ >= 0);
485 result.
have_h1 = (max_norm_ >= 1);
486 result.
have_h2 = (max_norm_ >= 2);
487 result.
have_l1 = (max_norm_ >= 0);
491 typedef Intern::SecHelperH0<(max_norm_ >= 0), AsmTraits, AnalyticEvalTraits> SecH0;
492 typedef Intern::SecHelperH1<(max_norm_ >= 1), AsmTraits, AnalyticEvalTraits, sub_dimensional_> SecH1;
493 typedef Intern::SecHelperH2<(max_norm_ >= 2), AsmTraits, AnalyticEvalTraits> SecH2;
496 for(
typename AsmTraits::CellIterator cell(trafo_eval.begin()); cell != trafo_eval.end(); ++cell)
502 dof_mapping.prepare(cell);
505 gather_axpy(lvad, dof_mapping);
508 dof_mapping.finish();
511 trafo_eval.prepare(cell);
514 space_eval.prepare(trafo_eval);
517 int num_loc_dofs = space_eval.get_num_local_dofs();
520 for(
int k(0); k < cubature_rule.get_num_points(); ++k)
523 trafo_eval(trafo_data, cubature_rule.get_point(k));
526 space_eval(space_data, trafo_data);
529 const DataType omega = trafo_data.jac_det * cubature_rule.get_weight(k);
532 const DataType abs_err = SecH0::eval(func_eval, trafo_data, space_data, lvad, num_loc_dofs);
536 result.
norm_l1 += omega * abs_err;
537 result.
norm_h0 += omega * abs_err * abs_err;
538 result.
norm_h1 += omega * SecH1::eval(func_eval, trafo_data, space_data, lvad, num_loc_dofs);
539 result.
norm_h2 += omega * SecH2::eval(func_eval, trafo_data, space_data, lvad, num_loc_dofs);
564 template<
bool h0_,
typename AsmTraits_,
typename AnaTraits_>
567 typedef typename AsmTraits_::DataType DataType;
570 template<
typename FuncEval_,
typename LocalVec_>
571 static ResultType eval(
573 const typename AsmTraits_::TrafoEvalData&,
574 const typename AsmTraits_::SpaceEvalData&,
579 return ResultType::null();
583 template<
typename AsmTraits_,
typename AnaTraits_>
584 struct VecHelperH0<true, AsmTraits_, AnaTraits_>
586 typedef typename AsmTraits_::DataType DataType;
587 typedef Tiny::Vector<DataType, AnaTraits_::image_dim> ResultType;
589 template<
typename FuncEval_,
typename LocalVec_>
590 static ResultType eval(
591 FuncEval_& func_eval,
592 const typename AsmTraits_::TrafoEvalData& trafo_data,
593 const typename AsmTraits_::SpaceEvalData& space_data,
594 const LocalVec_& lvad,
595 const int num_loc_dofs
599 typename AnaTraits_::ValueType
value = func_eval.value(trafo_data.img_point);
602 for(
int i(0); i < num_loc_dofs; ++i)
603 value.axpy(-space_data.phi[i].value, lvad[i]);
607 for(
int k(0); k < AnaTraits_::image_dim; ++k)
608 result[k] = Math::abs(value[k]);
614 template<
bool h1_,
typename AsmTraits_,
typename AnaTraits_>
617 typedef typename AsmTraits_::DataType DataType;
618 typedef Tiny::Vector<DataType, AnaTraits_::image_dim> ResultType;
620 template<
typename FuncEval_,
typename LocalVec_>
621 static ResultType eval(
623 const typename AsmTraits_::TrafoEvalData&,
624 const typename AsmTraits_::SpaceEvalData&,
629 return ResultType::null();
633 template<
typename AsmTraits_,
typename AnaTraits_>
634 struct VecHelperH1<true, AsmTraits_, AnaTraits_>
636 typedef typename AsmTraits_::DataType DataType;
637 typedef Tiny::Vector<DataType, AnaTraits_::image_dim> ResultType;
639 template<
typename FuncEval_,
typename LocalVec_>
640 static ResultType eval(
641 FuncEval_& func_eval,
642 const typename AsmTraits_::TrafoEvalData& trafo_data,
643 const typename AsmTraits_::SpaceEvalData& space_data,
644 const LocalVec_& lvad,
645 const int num_loc_dofs
649 typename AnaTraits_::GradientType
grad = func_eval.gradient(trafo_data.img_point);
652 for(
int i(0); i < num_loc_dofs; ++i)
653 grad.add_outer_product(lvad[i], space_data.phi[i].grad, -DataType(1));
657 for(
int k(0); k < AnaTraits_::image_dim; ++k)
658 result[k] = grad[k].norm_euclid_sqr();
664 template<
bool h2_,
typename AsmTraits_,
typename AnaTraits_>
667 typedef typename AsmTraits_::DataType DataType;
668 typedef Tiny::Vector<DataType, AnaTraits_::image_dim> ResultType;
670 template<
typename FuncEval_,
typename LocalVec_>
671 static ResultType eval(
673 const typename AsmTraits_::TrafoEvalData&,
674 const typename AsmTraits_::SpaceEvalData&,
679 return ResultType::null();
683 template<
typename AsmTraits_,
typename AnaTraits_>
684 struct VecHelperH2<true, AsmTraits_, AnaTraits_>
686 typedef typename AsmTraits_::DataType DataType;
687 typedef Tiny::Vector<DataType, AnaTraits_::image_dim> ResultType;
689 template<
typename FuncEval_,
typename LocalVec_>
690 static ResultType eval(
691 FuncEval_& func_eval,
692 const typename AsmTraits_::TrafoEvalData& trafo_data,
693 const typename AsmTraits_::SpaceEvalData& space_data,
694 const LocalVec_& lvad,
695 const int num_loc_dofs
699 typename AnaTraits_::HessianType
hess = func_eval.hessian(trafo_data.img_point);
702 for(
int i(0); i < num_loc_dofs; ++i)
704 hess.add_vec_mat_outer_product(lvad[i], space_data.phi[i].hess, -DataType(1));
709 for(
int k(0); k < AnaTraits_::image_dim; ++k)
710 result[k] = hess[k].norm_hessian_sqr();
724 template<
typename DataType_,
int dim_>
728 static constexpr int dim = dim_;
809 template<
typename DT2_>
830 template<
typename DT2_>
838 norm_h0 = DataType_(other.norm_h0);
839 norm_h1 = DataType_(other.norm_h1);
840 norm_h2 = DataType_(other.norm_h2);
841 norm_l1 = DataType_(other.norm_l1);
861 DataType_ verr[4+4*dim_] =
868 for(
int i(0); i < dim_; ++i)
881 for(
int i(0); i < dim_; ++i)
887 for(
int i(0); i < dim_; ++i)
893 for(
int i(0); i < dim_; ++i)
899 for(
int i(0); i < dim_; ++i)
905 for(
int i(0); i < dim_; ++i)
909 for(
int i(0); i < dim_; ++i)
935 for(
int i(0); i < dim_; ++i)
942 for(
int i(0); i < dim_; ++i)
949 for(
int i(0); i < dim_; ++i)
963 for(
int i(0); i < dim_; ++i)
970 for(
int i(0); i < dim_; ++i)
977 for(
int i(0); i < dim_; ++i)
1005 template<
int max_norm_ = 0>
1008 static_assert(max_norm_ >= 0,
"invalid max_norm_ parameter");
1020 static constexpr SpaceTags space_config =
1054 typename CubatureFactory_>
1056 const Vector_& vector,
1057 const Function_& function,
1058 const Space_& space,
1059 const CubatureFactory_& cubature_factory)
1062 static_assert(Function_::can_value,
"function values are required for H0-Error");
1063 static_assert(Function_::can_grad || (max_norm_ < 1),
"function gradients are required for H1-Error");
1064 static_assert(Function_::can_hess || (max_norm_ < 2),
"function hessians are required for H2-Error");
1067 XASSERTM(vector.size() == space.get_num_dofs(),
"invalid vector size");
1070 typedef Vector_ VectorType;
1072 typedef Function_ FunctionType;
1074 typedef Space_ SpaceType;
1076 static constexpr int dim = Function_::ImageType::scalar_components;
1080 typename Vector_::DataType,
1086 typedef typename AsmTraits::DataType DataType;
1089 typename AsmTraits::CubatureRuleType cubature_rule(Cubature::ctor_factory, cubature_factory);
1092 const typename AsmTraits::TrafoType& trafo = space.get_trafo();
1098 typename FunctionType::template Evaluator<AnalyticEvalTraits> func_eval(function);
1101 typename AsmTraits::TrafoEvaluator trafo_eval(trafo);
1104 typename AsmTraits::SpaceEvaluator space_eval(space);
1107 typename AsmTraits::DofMapping dof_mapping(space);
1110 typename AsmTraits::TrafoEvalData trafo_data;
1113 typename AsmTraits::SpaceEvalData space_data;
1119 typename AsmTraits::template TLocalVector<VectorValue> lvad;
1122 typename VectorType::GatherAxpy gather_axpy(vector);
1126 info.
have_h0 = (max_norm_ >= 0);
1127 info.
have_h1 = (max_norm_ >= 1);
1128 info.
have_h2 = (max_norm_ >= 2);
1129 info.
have_l1 = (max_norm_ >= 0);
1133 typedef Intern::VecHelperH0<(max_norm_ >= 0), AsmTraits, AnalyticEvalTraits> VecH0;
1134 typedef Intern::VecHelperH1<(max_norm_ >= 1), AsmTraits, AnalyticEvalTraits> VecH1;
1135 typedef Intern::VecHelperH2<(max_norm_ >= 2), AsmTraits, AnalyticEvalTraits> VecH2;
1138 for(
typename AsmTraits::CellIterator cell(trafo_eval.begin()); cell != trafo_eval.end(); ++cell)
1144 dof_mapping.prepare(cell);
1147 gather_axpy(lvad, dof_mapping);
1150 dof_mapping.finish();
1153 trafo_eval.prepare(cell);
1156 space_eval.prepare(trafo_eval);
1159 int num_loc_dofs = space_eval.get_num_local_dofs();
1162 for(
int k(0); k < cubature_rule.get_num_points(); ++k)
1165 trafo_eval(trafo_data, cubature_rule.get_point(k));
1168 space_eval(space_data, trafo_data);
1171 const DataType omega = trafo_data.jac_det * cubature_rule.get_weight(k);
1174 auto abs_err = VecH0::eval(func_eval, trafo_data, space_data, lvad, num_loc_dofs);
1177 for(
int i(0); i < dim; ++i)
1185 info.
norm_h1_comp.
axpy(omega, VecH1::eval(func_eval, trafo_data, space_data, lvad, num_loc_dofs));
1186 info.
norm_h2_comp.
axpy(omega, VecH2::eval(func_eval, trafo_data, space_data, lvad, num_loc_dofs));
1192 space_eval.finish();
1193 trafo_eval.finish();
1199 for(
int i(0); i < dim; ++i)
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Common single-space assembly traits class template.
Scalar H0/H1/H2-error computer class template.
static ScalarErrorInfo< typename Vector_::DataType > compute(const Vector_ &vector, const Function_ &function, const Space_ &space, const CubatureFactory_ &cubature_factory)
Computes the H0/H1/H2-error.
Vector H0/H1/H2-error computer class template.
static VectorErrorInfo< typename Vector_::DataType, Function_::ImageType::scalar_components > compute(const Vector_ &vector, const Function_ &function, const Space_ &space, const CubatureFactory_ &cubature_factory)
Computes the H0/H1/H2-error.
void allreduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Blocking All-Reduce.
String class implementation.
String pad_back(size_type len, char c=' ') const
Pads the back of the string up to a desired length.
Tiny Vector class template.
CUDA_HOST_DEVICE Vector & axpy(DataType alpha, const Vector< T_, n_, snx_ > &x)
Adds another scaled vector onto this vector.
const Operation op_max(MPI_MAX)
Operation wrapper for MPI_MAX.
const Operation op_sum(MPI_SUM)
Operation wrapper for MPI_SUM.
T_ sqrt(T_ x)
Returns the square-root of a value.
T_ sqr(T_ x)
Returns the square of a value.
T_ max(T_ a, T_ b)
Returns the maximum of two values.
String stringify_fp_sci(DataType_ value, int precision=0, int width=0, bool sign=false)
Prints a floating point value to a string in scientific notation.
SpaceTags
Space configuration tags enum.
@ value
specifies whether the space should supply basis function values
@ hess
specifies whether the space should supply basis function hessians
@ ref_grad
specifies whether the space should supply reference basis function gradients
@ grad
specifies whether the space should supply basis function gradients
TrafoTags
Trafo configuration tags enum.
@ img_point
specifies whether the trafo should supply image point coordinates
@ jac_mat
specifies whether the trafo should supply jacobian matrices
@ jac_det
specifies whether the trafo should supply jacobian determinants
Scalar Error information structure.
bool have_h1
Specifies whether the H1-error was computed.
DataType_ norm_h1
The computed H1-error.
ScalarErrorInfo & operator=(const ScalarErrorInfo< DT2_ > &other)
conversion assignment operator
bool have_lmax
Specifies whether the Lmax-error was computed.
bool have_h2
Specifies whether the H2-error was computed.
String format_string(int precision=0, std::size_t pad_size=8u, char pad_char='.') const
Formats the error information as a string.
DataType_ norm_lmax
The computed Lmax-error.
void synchronize(const Dist::Comm &comm)
Synchronizes the error information over a communicator.
ScalarErrorInfo(const ScalarErrorInfo< DT2_ > &other)
conversion constructor
bool have_h0
Specifies whether the H0-error was computed.
friend std::ostream & operator<<(std::ostream &os, const ScalarErrorInfo &sei)
prints the info to an output stream
ScalarErrorInfo()
standard constructor
bool have_l1
Specifies whether the L1-error was computed.
DataType_ norm_h2
The computed H2-error.
DataType_ norm_h0
The computed H0-error.
DataType_ norm_l1
The computed L1-error.
Vector Error information structure.
bool have_lmax
Specifies whether the Lmax-error was computed.
bool have_h1
Specifies whether the H1-error was computed.
DataType_ norm_h1
The computed H1-error.
DataType_ norm_h0
The computed H0-error.
Tiny::Vector< DataType_, dim_ > norm_h0_comp
Vector field components H0-errors.
VectorErrorInfo & operator=(const VectorErrorInfo< DT2_, dim_ > &other)
conversion assignment operator
static constexpr int dim
The dimension of the analysed vector field.
Tiny::Vector< DataType_, dim_ > norm_l1_comp
Vector field components L1-errors.
VectorErrorInfo()
standard constructor
void synchronize(const Dist::Comm &comm)
Synchronizes the error information over a communicator.
Tiny::Vector< DataType_, dim_ > norm_h1_comp
Vector field components H1-errors.
DataType_ norm_l1
The computed L1-error.
VectorErrorInfo(const VectorErrorInfo< DT2_, dim_ > &other)
conversion constructor
Tiny::Vector< DataType_, dim_ > norm_lmax_comp
Vector field components Lmax-errors.
bool have_h0
Specifies whether the H0-error was computed.
String format_string(int precision=0, std::size_t pad_size=8u, char pad_char='.') const
Formats the error information as a string.
friend std::ostream & operator<<(std::ostream &os, const VectorErrorInfo &sei)
prints the info to an output stream
DataType_ norm_lmax
The computed Lmax-error.
Tiny::Vector< DataType_, dim_ > norm_h2_comp
Vector field components H2-errors.
DataType_ norm_h2
The computed H2-error.
bool have_l1
Specifies whether the L1-error was computed.
bool have_h2
Specifies whether the H2-error was computed.