xref: /libCEED/examples/fluids/qfunctions/utils.h (revision dc9b5c4a033c329154210fb4ea036e15fdbd7186)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #ifndef utils_h
9 #define utils_h
10 
11 #include <ceed.h>
12 #include <math.h>
13 
14 #ifndef M_PI
15 #define M_PI 3.14159265358979323846
16 #endif
17 
18 CEED_QFUNCTION_HELPER CeedScalar Max(CeedScalar a, CeedScalar b) { return a < b ? b : a; }
19 CEED_QFUNCTION_HELPER CeedScalar Min(CeedScalar a, CeedScalar b) { return a < b ? a : b; }
20 
21 CEED_QFUNCTION_HELPER void SwapScalar(CeedScalar *a, CeedScalar *b) {
22   CeedScalar temp = *a;
23   *a              = *b;
24   *b              = temp;
25 }
26 
27 CEED_QFUNCTION_HELPER CeedScalar Square(CeedScalar x) { return x * x; }
28 CEED_QFUNCTION_HELPER CeedScalar Cube(CeedScalar x) { return x * x * x; }
29 
30 // @brief Scale vector of length N by scalar alpha
31 CEED_QFUNCTION_HELPER void ScaleN(CeedScalar *u, const CeedScalar alpha, const CeedInt N) {
32   CeedPragmaSIMD for (CeedInt i = 0; i < N; i++) { u[i] *= alpha; }
33 }
34 
35 // @brief Dot product of 3 element vectors
36 CEED_QFUNCTION_HELPER CeedScalar Dot3(const CeedScalar u[3], const CeedScalar v[3]) { return u[0] * v[0] + u[1] * v[1] + u[2] * v[2]; }
37 
38 // @brief Unpack Kelvin-Mandel notation symmetric tensor into full tensor
39 CEED_QFUNCTION_HELPER void KMUnpack(const CeedScalar v[6], CeedScalar A[3][3]) {
40   const CeedScalar weight = 1 / sqrt(2.);
41   A[0][0]                 = v[0];
42   A[1][1]                 = v[1];
43   A[2][2]                 = v[2];
44   A[2][1] = A[1][2] = weight * v[3];
45   A[2][0] = A[0][2] = weight * v[4];
46   A[1][0] = A[0][1] = weight * v[5];
47 }
48 
49 // @brief Linear ramp evaluation
50 CEED_QFUNCTION_HELPER CeedScalar LinearRampCoefficient(CeedScalar amplitude, CeedScalar length, CeedScalar start, CeedScalar x) {
51   if (x < start) {
52     return amplitude;
53   } else if (x < start + length) {
54     return amplitude * ((x - start) * (-1 / length) + 1);
55   } else {
56     return 0;
57   }
58 }
59 
60 #endif  // utils_h
61