Gyselalib++
math_tools.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: MIT
7 #pragma once
8 
9 #include <ddc/ddc.hpp>
10 
11 #include "quadrature.hpp"
12 
24 template <class... Tags>
25 KOKKOS_FUNCTION double norm_inf(ddc::Coordinate<Tags...> coord)
26 {
27  double result = 0.0;
28  ((result = Kokkos::max(result, Kokkos::fabs(coord.template get<Tags>()))), ...);
29  return result;
30 }
31 
32 
44 KOKKOS_INLINE_FUNCTION double norm_inf(double const coord)
45 {
46  return coord;
47 }
48 
49 namespace detail {
50 
51 // General implementation of the infinity norm. This function is in a namespace to avoid code duplication
52 // without creating a function so general that it also captures multipatch types.
53 template <class ExecSpace, class FuncType>
54 double norm_inf(ExecSpace exec_space, FuncType function)
55 {
56  static_assert(
57  Kokkos::SpaceAccessibility<ExecSpace, typename FuncType::memory_space>::accessible);
58  using IdxRangeFunc = typename FuncType::discrete_domain_type;
59  using IdxFunc = typename IdxRangeFunc::discrete_element_type;
60  IdxRangeFunc idx_range = get_idx_range(function);
61  return ddc::parallel_transform_reduce(
62  exec_space,
63  idx_range,
64  0.,
65  ddc::reducer::max<double>(),
66  KOKKOS_LAMBDA(IdxFunc const idx) { return ::norm_inf(function(idx)); });
67 }
68 
69 // General implementation of the infinity norm of an error. This function is in a namespace to avoid code duplication
70 // without creating a function so general that it also captures multipatch types.
71 template <class ExecSpace, class FuncType>
72 double error_norm_inf(ExecSpace exec_space, FuncType function, FuncType exact_function)
73 {
74  static_assert(
75  Kokkos::SpaceAccessibility<ExecSpace, typename FuncType::memory_space>::accessible);
76  using IdxRangeFunc = typename FuncType::discrete_domain_type;
77  using IdxFunc = typename IdxRangeFunc::discrete_element_type;
78  IdxRangeFunc idx_range = get_idx_range(function);
79  return ddc::parallel_transform_reduce(
80  exec_space,
81  idx_range,
82  0.,
83  ddc::reducer::max<double>(),
84  KOKKOS_LAMBDA(IdxFunc const idx) {
85  return ::norm_inf(function(idx) - exact_function(idx));
86  });
87 }
88 
89 }; // namespace detail
90 
97 template <class ExecSpace, class ElementType, class IdxRange>
98 inline double norm_inf(
99  ExecSpace exec_space,
100  ConstField<ElementType, IdxRange, typename ExecSpace::memory_space> function)
101 {
102  return detail::norm_inf(exec_space, function);
103 }
104 
111 template <class ExecSpace, class ElementType, class IdxRange, class NDTag>
112 inline double norm_inf(
113  ExecSpace exec_space,
115 {
116  return detail::norm_inf(exec_space, function);
117 }
118 
126 template <class ExecSpace, class ElementType, class IdxRange>
127 inline double error_norm_inf(
128  ExecSpace exec_space,
129  ConstField<ElementType, IdxRange, typename ExecSpace::memory_space> function,
130  ConstField<ElementType, IdxRange, typename ExecSpace::memory_space> exact_function)
131 {
132  return detail::error_norm_inf(exec_space, function, exact_function);
133 }
134 
142 template <class ExecSpace, class ElementType, class IdxRange, class NDTag>
143 inline double error_norm_inf(
144  ExecSpace exec_space,
147  exact_function)
148 {
149  return detail::error_norm_inf(exec_space, function, exact_function);
150 }
151 
166 template <class IdxRangeQuad, class ExecSpace>
167 double norm_L1(
168  ExecSpace exec_space,
170  DField<IdxRangeQuad, typename ExecSpace::memory_space> function)
171 {
172  using IdxQuad = typename IdxRangeQuad::discrete_element_type;
173  return quadrature(
174  exec_space,
175  KOKKOS_LAMBDA(IdxQuad const idx) { return Kokkos::fabs(function(idx)); });
176 }
177 
186 template <class IdxRangeQuad, class ExecSpace>
188  ExecSpace exec_space,
190  DField<IdxRangeQuad, typename ExecSpace::memory_space> function,
191  DField<IdxRangeQuad, typename ExecSpace::memory_space> exact_function)
192 {
193  using IdxQuad = typename IdxRangeQuad::discrete_element_type;
194  return quadrature(
195  exec_space,
196  KOKKOS_LAMBDA(IdxQuad const idx) {
197  return Kokkos::fabs(function(idx) - exact_function(idx));
198  });
199 }
200 
201 
216 template <class IdxRangeQuad, class ExecSpace>
217 double norm_L2(
218  ExecSpace exec_space,
220  DField<IdxRangeQuad, typename ExecSpace::memory_space> function)
221 {
222  using IdxQuad = typename IdxRangeQuad::discrete_element_type;
223  return std::sqrt(quadrature(
224  exec_space,
225  KOKKOS_LAMBDA(IdxQuad const idx) { return function(idx) * function(idx); }));
226 }
227 
236 template <class IdxRangeQuad, class ExecSpace>
238  ExecSpace exec_space,
240  DField<IdxRangeQuad, typename ExecSpace::memory_space> function,
241  DField<IdxRangeQuad, typename ExecSpace::memory_space> exact_function)
242 {
243  using IdxQuad = typename IdxRangeQuad::discrete_element_type;
244  return std::sqrt(quadrature(
245  exec_space,
246  KOKKOS_LAMBDA(IdxQuad const idx) {
247  double err = function(idx) - exact_function(idx);
248  return err * err;
249  }));
250 }
A class providing an operator for integrating functions defined on a discrete index range.
Definition: quadrature.hpp:29
A class which holds multiple (scalar) fields in order to represent a vector field.
Definition: vector_field.hpp:64
double norm_L2(ExecSpace exec_space, Quadrature< IdxRangeQuad, IdxRangeQuad, typename ExecSpace::memory_space > quadrature, DField< IdxRangeQuad, typename ExecSpace::memory_space > function)
Compute L2 norm of a function with a given quadrature.
Definition: math_tools.hpp:217
double error_norm_L1(ExecSpace exec_space, Quadrature< IdxRangeQuad, IdxRangeQuad, typename ExecSpace::memory_space > quadrature, DField< IdxRangeQuad, typename ExecSpace::memory_space > function, DField< IdxRangeQuad, typename ExecSpace::memory_space > exact_function)
Compute the L1 norm of the error between 2 Fields.
Definition: math_tools.hpp:187
double error_norm_L2(ExecSpace exec_space, Quadrature< IdxRangeQuad, IdxRangeQuad, typename ExecSpace::memory_space > quadrature, DField< IdxRangeQuad, typename ExecSpace::memory_space > function, DField< IdxRangeQuad, typename ExecSpace::memory_space > exact_function)
Compute the L2 norm of the error between 2 Fields.
Definition: math_tools.hpp:237
double norm_L1(ExecSpace exec_space, Quadrature< IdxRangeQuad, IdxRangeQuad, typename ExecSpace::memory_space > quadrature, DField< IdxRangeQuad, typename ExecSpace::memory_space > function)
Compute L1 norm of a function with a given quadrature.
Definition: math_tools.hpp:167
KOKKOS_FUNCTION double norm_inf(ddc::Coordinate< Tags... > coord)
Compute the infinity norm.
Definition: math_tools.hpp:25
double error_norm_inf(ExecSpace exec_space, ConstField< ElementType, IdxRange, typename ExecSpace::memory_space > function, ConstField< ElementType, IdxRange, typename ExecSpace::memory_space > exact_function)
Compute the infinity norm of the error between 2 Fields.
Definition: math_tools.hpp:127