ABLATE Source Documentation  0.12.33
mathUtilities.hpp
1 #ifndef ABLATELIBRARY_MATHUTILITIES_HPP
2 #define ABLATELIBRARY_MATHUTILITIES_HPP
3 #include <petsc.h>
4 
5 namespace ablate::utilities {
6 class MathUtilities {
7  public:
8  template <class I, class T>
9  static inline void ScaleVector(I dim, T* vec, T alpha) {
10  for (I d = 0; d < dim; d++) {
11  vec[d] *= alpha;
12  }
13  }
14 
15  template <class I, class T>
16  static inline void NormVector(I dim, T* vec) {
17  auto mag = MagVector(dim, vec);
18  for (I d = 0; d < dim; d++) {
19  vec[d] = vec[d] / mag;
20  }
21  }
22 
23  template <class I, class T>
24  static inline bool VectorEquals(I dim, const T* test, const T* equal, T tolerance = 1.0E-8) {
25  for (I d = 0; d < dim; d++) {
26  if (test[d] < equal[d] - tolerance || test[d] > equal[d] + tolerance) {
27  return false;
28  }
29  }
30  return true;
31  }
32 
33  template <class R, class T>
34  static inline bool Equals(R test, T equal, T tolerance = 1.0E-8) {
35  return test > (equal - tolerance) && test < (equal + tolerance);
36  }
37 
38  template <class I, class T>
39  static inline void NormVector(I dim, const T* in, T* out) {
40  T mag = 0.0;
41  for (I d = 0; d < dim; d++) {
42  mag += in[d] * in[d];
43  }
44  mag = PetscSqrtReal(mag);
45  for (I d = 0; d < dim; d++) {
46  out[d] = in[d] / mag;
47  }
48  }
49 
50  template <class I, class T>
51  static inline T MagVector(I dim, const T* in) {
52  T mag = 0.0;
53  for (I d = 0; d < dim; d++) {
54  mag += in[d] * in[d];
55  }
56  return PetscSqrtReal(mag);
57  }
58 
59  template <class I, class T>
60  static inline T DotVector(I dim, const T* a, const T* b) {
61  T dot = 0.0;
62  for (I d = 0; d < dim; d++) {
63  dot += a[d] * b[d];
64  }
65  return dot;
66  }
67 
68  template <int dim, class T>
69  static inline T DotVector(const T* a, const T* b) {
70  if constexpr (dim == 3) {
71  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
72  }
73  if constexpr (dim == 2) {
74  return a[0] * b[0] + a[1] * b[1];
75  }
76  if constexpr (dim == 1) {
77  return a[0] * b[0];
78  }
79  static_assert(dim == 1 || dim == 2 || dim == 3, "ablate::utilities::MathUtilities::DotVector only support dimensions of 1, 2, or 3");
80  }
81 
93  template <class I, class T>
94  static inline T DiffDotVector(I dim, const T* aE, const T* aS, const T* b) {
95  switch (dim) { // Take the difference of the first two points and dot it with the third vector
96  case 3: {
97  return (((aE[0] - aS[0]) * b[0]) + ((aE[1] - aS[1]) * b[1]) + ((aE[2] - aS[2]) * b[2]));
98  }
99  case 2: {
100  return (((aE[0] - aS[0]) * b[0]) + ((aE[1] - aS[1]) * b[1]));
101  }
102  default: {
103  return (((aE[0] - aS[0]) * b[0]));
104  }
105  }
106  }
107 
119  template <int dim, class T>
120  static inline T DiffDotVector(const T* aE, const T* aS, const T* b) {
121  if constexpr (dim == 3) {
122  return (((aE[0] - aS[0]) * b[0]) + ((aE[1] - aS[1]) * b[1]) + ((aE[2] - aS[2]) * b[2]));
123  }
124  if constexpr (dim == 2) {
125  return (((aE[0] - aS[0]) * b[0]) + ((aE[1] - aS[1]) * b[1]));
126  }
127  if constexpr (dim == 1) {
128  return (((aE[0] - aS[0]) * b[0]));
129  }
130  static_assert(dim == 1 || dim == 2 || dim == 3, "ablate::utilities::MathUtilities::DiffDotVector only support dimensions of 1, 2, or 3");
131  }
132 
142  template <class I, class T>
143  static inline void CrossVector(I dim, const T* a, const T* b, T* c) {
144  switch (dim) {
145  case 3: {
146  c[0] = (a[1] * b[2] - b[1] * a[2]);
147  c[1] = (b[0] * a[2] - a[0] * b[2]);
148  c[2] = (a[0] * b[1] - b[0] * a[1]);
149  break;
150  }
151  case 2: {
152  c[0] = (a[0] * b[1] - b[0] * a[1]);
153  break;
154  }
155  default: {
156  c[0] = 0.;
157  break;
158  }
159  }
160  }
161 
171  template <int dim, class T>
172  static inline void CrossVector([[maybe_unused]] const T* a, [[maybe_unused]] const T* b, T* c) {
173  if constexpr (dim == 3) {
174  c[0] = (a[1] * b[2] - b[1] * a[2]);
175  c[1] = (b[0] * a[2] - a[0] * b[2]);
176  c[2] = (a[0] * b[1] - b[0] * a[1]);
177  }
178  if constexpr (dim == 2) {
179  c[0] = (a[0] * b[1] - b[0] * a[1]);
180  }
181  if constexpr (dim == 1) {
182  c[0] = 0.;
183  }
184  static_assert(dim == 1 || dim == 2 || dim == 3, "ablate::utilities::MathUtilities::CrossVector only support dimensions of 1, 2, or 3");
185  }
186 
195  template <class I, class T>
196  static inline void Subtract(I dim, const T* a, const T* b, T* c) {
197  for (I i = 0; i < dim; i++) {
198  c[i] = a[i] - b[i];
199  }
200  }
201 
210  template <class I, class T>
211  static inline void Plus(I dim, const T* a, T* b) {
212  for (I i = 0; i < dim; i++) {
213  b[i] += a[i];
214  }
215  }
216 
224  template <class I, class T>
225  static inline void Multiply(I dim, const T a[3][3], const T* in, T* out) {
226  for (I i = 0; i < dim; i++) {
227  out[i] = 0.0;
228  for (I j = 0; j < dim; j++) {
229  out[i] += a[i][j] * in[j];
230  }
231  }
232  }
233 
241  template <class I, class T>
242  static inline void MultiplyTranspose(I dim, const T a[3][3], const T* in, T* out) {
243  for (I i = 0; i < dim; i++) {
244  out[i] = 0.0;
245  for (I j = 0; j < dim; j++) {
246  out[i] += a[j][i] * in[j];
247  }
248  }
249  }
250 
258  static void ComputeTransformationMatrix(PetscInt dim, const PetscScalar normal[3], PetscScalar transformationMatrix[3][3]);
259 
267  static PetscReal ComputeDeterminant(PetscInt dim, PetscScalar transformationMatrix[3][3]);
268 
272  enum class Norm { L1, L1_NORM, L2, LINF, L2_NORM };
273 
282  static PetscErrorCode ComputeNorm(Norm normType, Vec x, Vec y, PetscReal norm[]);
283 
284  MathUtilities() = delete;
285 };
286 
293 std::ostream& operator<<(std::ostream& os, const MathUtilities::Norm& v);
300 std::istream& operator>>(std::istream& is, MathUtilities::Norm& v);
301 
302 } // namespace ablate::utilities
303 
304 #endif // ABLATELIBRARY_MATHUTILITIES_HPP
Definition: mathUtilities.hpp:6
static void Subtract(I dim, const T *a, const T *b, T *c)
Definition: mathUtilities.hpp:196
Norm
Definition: mathUtilities.hpp:272
static PetscReal ComputeDeterminant(PetscInt dim, PetscScalar transformationMatrix[3][3])
Definition: mathUtilities.cpp:35
static void CrossVector([[maybe_unused]] const T *a, [[maybe_unused]] const T *b, T *c)
Definition: mathUtilities.hpp:172
static void Plus(I dim, const T *a, T *b)
Definition: mathUtilities.hpp:211
static void ComputeTransformationMatrix(PetscInt dim, const PetscScalar normal[3], PetscScalar transformationMatrix[3][3])
Definition: mathUtilities.cpp:4
static PetscErrorCode ComputeNorm(Norm normType, Vec x, Vec y, PetscReal norm[])
Definition: mathUtilities.cpp:48
static T DiffDotVector(I dim, const T *aE, const T *aS, const T *b)
Definition: mathUtilities.hpp:94
static T DiffDotVector(const T *aE, const T *aS, const T *b)
Definition: mathUtilities.hpp:120
static void MultiplyTranspose(I dim, const T a[3][3], const T *in, T *out)
Definition: mathUtilities.hpp:242
static void Multiply(I dim, const T a[3][3], const T *in, T *out)
Definition: mathUtilities.hpp:225
static void CrossVector(I dim, const T *a, const T *b, T *c)
Definition: mathUtilities.hpp:143