Gyselalib++
 
Loading...
Searching...
No Matches
math_tools.hpp
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5
6#include <ddc/ddc.hpp>
7
8#include <Kokkos_Core.hpp>
9
10template <typename T>
11inline T sum(T* array, int size)
12{
13 T val(0.0);
14 for (int i(0); i < size; ++i) {
15 val += array[i];
16 }
17 return val;
18}
19
20template <class ElementType, class LayoutPolicy, class AccessorPolicy, std::size_t Ext>
21inline ElementType sum(Kokkos::mdspan<
22 ElementType,
23 Kokkos::extents<std::size_t, Ext>,
24 LayoutPolicy,
25 AccessorPolicy> const& array)
26{
27 ElementType val(0.0);
28 for (std::size_t i(0); i < array.extent(0); ++i) {
29 val += array[i];
30 }
31 return val;
32}
33
34template <class ElementType, class LayoutPolicy, class AccessorPolicy, std::size_t Ext>
35inline ElementType sum(
36 Kokkos::mdspan<
37 ElementType,
38 Kokkos::extents<std::size_t, Ext>,
39 LayoutPolicy,
40 AccessorPolicy> const& array,
41 int start,
42 int end)
43{
44 ElementType val(0.0);
45 for (int i(start); i < end; ++i) {
46 val += array[i];
47 }
48 return val;
49}
50
51template <typename T>
52inline T modulo(T x, T y)
53{
54 return x - y * std::floor(double(x) / y);
55}
56
57constexpr inline double ipow(double a, std::size_t i)
58{
59 double r(1.0);
60 for (std::size_t j(0); j < i; ++j) {
61 r *= a;
62 }
63 return r;
64}
65
66inline double ipow(double a, int i)
67{
68 double r(1.0);
69 if (i > 0) {
70 for (int j(0); j < i; ++j) {
71 r *= a;
72 }
73 } else if (i < 0) {
74 for (int j(0); j < -i; ++j) {
75 r *= a;
76 }
77 r = 1.0 / r;
78 }
79 return r;
80}
81
82inline std::size_t factorial(std::size_t f)
83{
84 std::size_t r = 1;
85 for (std::size_t i(2); i < f + 1; ++i) {
86 r *= i;
87 }
88 return r;
89}
90
91template <class T, std::size_t D>
92inline T dot_product(std::array<T, D> const& a, std::array<T, D> const& b)
93{
94 T result = 0;
95 for (std::size_t i(0); i < D; ++i) {
96 result += a[i] * b[i];
97 }
98 return result;
99}
100
101template <typename T>
102inline T min(T x, T y)
103{
104 return x < y ? x : y;
105}
106
107template <typename T>
108inline T max(T x, T y)
109{
110 return x > y ? x : y;
111}
112
113template <std::size_t N, std::size_t M, std::size_t P>
114KOKKOS_INLINE_FUNCTION std::array<std::array<double, N>, P> mat_mul(
115 std::array<std::array<double, N>, M> const& a,
116 std::array<std::array<double, M>, P> const& b)
117{
118 std::array<std::array<double, N>, P> result;
119 for (std::size_t i(0); i < N; ++i) {
120 for (std::size_t j(0); j < P; ++j) {
121 result[i][j] = 0.0;
122 for (std::size_t k(0); k < M; ++k) {
123 result[i][j] += a[i][k] * b[k][j];
124 }
125 }
126 }
127 return result;
128}
129
130template <std::size_t N, std::size_t M>
131KOKKOS_INLINE_FUNCTION std::array<double, N> mat_vec_mul(
132 std::array<std::array<double, N>, M> const& a,
133 std::array<double, M> const& b)
134{
135 std::array<double, N> result;
136 for (std::size_t i(0); i < N; ++i) {
137 result[i] = 0.0;
138 for (std::size_t k(0); k < M; ++k) {
139 result[i] += a[i][k] * b[k];
140 }
141 }
142 return result;
143}
144
145KOKKOS_INLINE_FUNCTION double determinant(std::array<std::array<double, 2>, 2> arr)
146{
147 return arr[0][0] * arr[1][1] - arr[0][1] * arr[1][0];
148}
149
150KOKKOS_INLINE_FUNCTION std::array<std::array<double, 2>, 2> inverse(
151 std::array<std::array<double, 2>, 2> arr)
152{
153 std::array<std::array<double, 2>, 2> inv;
154 double det = determinant(arr);
155 inv[0][0] = arr[1][1] / det;
156 inv[1][0] = -arr[1][0] / det;
157 inv[0][1] = -arr[0][1] / det;
158 inv[1][1] = arr[0][0] / det;
159 return inv;
160}
A class which describes the real space in the temporal direction.
Definition geometry.hpp:44