xref: /libCEED/examples/fluids/qfunctions/utils.h (revision 4e5897fc3f7a5f948072c1b4dffe9e884e6f2e55)
113fa47b2SJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
213fa47b2SJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
313fa47b2SJames Wright //
413fa47b2SJames Wright // SPDX-License-Identifier: BSD-2-Clause
513fa47b2SJames Wright //
613fa47b2SJames Wright // This file is part of CEED:  http://github.com/ceed
713fa47b2SJames Wright 
813fa47b2SJames Wright #ifndef utils_h
913fa47b2SJames Wright #define utils_h
1013fa47b2SJames Wright 
1113fa47b2SJames Wright #include <ceed.h>
12c9c2c079SJeremy L Thompson #include <math.h>
1313fa47b2SJames Wright 
1413fa47b2SJames Wright #ifndef M_PI
1513fa47b2SJames Wright #define M_PI 3.14159265358979323846
1613fa47b2SJames Wright #endif
1713fa47b2SJames Wright 
1813fa47b2SJames Wright CEED_QFUNCTION_HELPER CeedScalar Max(CeedScalar a, CeedScalar b) { return a < b ? b : a; }
1913fa47b2SJames Wright CEED_QFUNCTION_HELPER CeedScalar Min(CeedScalar a, CeedScalar b) { return a < b ? a : b; }
2013fa47b2SJames Wright 
21dc9b5c4aSJames Wright CEED_QFUNCTION_HELPER void SwapScalar(CeedScalar *a, CeedScalar *b) {
22dc9b5c4aSJames Wright   CeedScalar temp = *a;
23dc9b5c4aSJames Wright   *a              = *b;
24dc9b5c4aSJames Wright   *b              = temp;
25dc9b5c4aSJames Wright }
26dc9b5c4aSJames Wright 
2713fa47b2SJames Wright CEED_QFUNCTION_HELPER CeedScalar Square(CeedScalar x) { return x * x; }
2813fa47b2SJames Wright CEED_QFUNCTION_HELPER CeedScalar Cube(CeedScalar x) { return x * x * x; }
2913fa47b2SJames Wright 
30530ad8c4SKenneth E. Jansen // @brief Scale vector of length N by scalar alpha
31530ad8c4SKenneth E. Jansen CEED_QFUNCTION_HELPER void ScaleN(CeedScalar *u, const CeedScalar alpha, const CeedInt N) {
32a455f92dSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < N; i++) u[i] *= alpha;
33a455f92dSJames Wright }
34a455f92dSJames Wright 
35a455f92dSJames Wright // @brief Set vector of length N to a value alpha
36a455f92dSJames Wright CEED_QFUNCTION_HELPER void SetValueN(CeedScalar *u, const CeedScalar alpha, const CeedInt N) {
37a455f92dSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < N; i++) u[i] = alpha;
38a455f92dSJames Wright }
39a455f92dSJames Wright 
40a455f92dSJames Wright // @brief Copy N elements from x to y
41a455f92dSJames Wright CEED_QFUNCTION_HELPER void CopyN(const CeedScalar *x, CeedScalar *y, const CeedInt N) { CeedPragmaSIMD for (CeedInt i = 0; i < N; i++) y[i] = x[i]; }
42a455f92dSJames Wright 
43a455f92dSJames Wright // @brief Copy 3x3 matrix from A to B
44a455f92dSJames Wright CEED_QFUNCTION_HELPER void CopyMat3(const CeedScalar A[3][3], CeedScalar B[3][3]) { CopyN((const CeedScalar *)A, (CeedScalar *)B, 9); }
45a455f92dSJames Wright 
46a455f92dSJames Wright // @brief Dot product of vectors with N elements
47a455f92dSJames Wright CEED_QFUNCTION_HELPER CeedScalar DotN(const CeedScalar *u, const CeedScalar *v, const CeedInt N) {
48a455f92dSJames Wright   CeedScalar output = 0;
49a455f92dSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < N; i++) output += u[i] * v[i];
50a455f92dSJames Wright   return output;
51530ad8c4SKenneth E. Jansen }
52530ad8c4SKenneth E. Jansen 
5313fa47b2SJames Wright // @brief Dot product of 3 element vectors
54be91e165SJames Wright CEED_QFUNCTION_HELPER CeedScalar Dot3(const CeedScalar *u, const CeedScalar *v) { return u[0] * v[0] + u[1] * v[1] + u[2] * v[2]; }
5513fa47b2SJames Wright 
56a455f92dSJames Wright // @brief Cross product of vectors with 3 elements
57a455f92dSJames Wright CEED_QFUNCTION_HELPER void Cross3(const CeedScalar u[3], const CeedScalar v[3], CeedScalar w[3]) {
58a455f92dSJames Wright   w[0] = (u[1] * v[2]) - (u[2] * v[1]);
59a455f92dSJames Wright   w[1] = (u[2] * v[0]) - (u[0] * v[2]);
60a455f92dSJames Wright   w[2] = (u[0] * v[1]) - (u[1] * v[0]);
61a455f92dSJames Wright }
62a455f92dSJames Wright 
63a455f92dSJames Wright // @brief Curl of vector given its gradient
64a455f92dSJames Wright CEED_QFUNCTION_HELPER void Curl3(const CeedScalar gradient[3][3], CeedScalar v[3]) {
65a455f92dSJames Wright   v[0] = gradient[2][1] - gradient[1][2];
66a455f92dSJames Wright   v[1] = gradient[0][2] - gradient[2][0];
67a455f92dSJames Wright   v[2] = gradient[1][0] - gradient[0][1];
68a455f92dSJames Wright }
69a455f92dSJames Wright 
70a455f92dSJames Wright // @brief Matrix vector product, b = Ax + b. A is NxM, x is M, b is N
71a455f92dSJames Wright CEED_QFUNCTION_HELPER void MatVecNM(const CeedScalar *A, const CeedScalar *x, const CeedInt N, const CeedInt M, const CeedTransposeMode transpose_A,
72a455f92dSJames Wright                                     CeedScalar *b) {
73a455f92dSJames Wright   switch (transpose_A) {
74a455f92dSJames Wright     case CEED_NOTRANSPOSE:
75a455f92dSJames Wright       CeedPragmaSIMD for (CeedInt i = 0; i < N; i++) b[i] += DotN(&A[i * M], x, M);
76a455f92dSJames Wright       break;
77a455f92dSJames Wright     case CEED_TRANSPOSE:
78a455f92dSJames Wright       CeedPragmaSIMD for (CeedInt i = 0; i < M; i++) { CeedPragmaSIMD for (CeedInt j = 0; j < N; j++) b[i] += A[j * M + i] * x[j]; }
79a455f92dSJames Wright       break;
80a455f92dSJames Wright   }
81a455f92dSJames Wright }
82a455f92dSJames Wright 
83a455f92dSJames Wright // @brief 3x3 Matrix vector product  b = Ax + b.
84a455f92dSJames Wright CEED_QFUNCTION_HELPER void MatVec3(const CeedScalar A[3][3], const CeedScalar x[3], const CeedTransposeMode transpose_A, CeedScalar b[3]) {
85a455f92dSJames Wright   MatVecNM((const CeedScalar *)A, (const CeedScalar *)x, 3, 3, transpose_A, (CeedScalar *)b);
86a455f92dSJames Wright }
87a455f92dSJames Wright 
88a455f92dSJames Wright // @brief Matrix-Matrix product, B = DA + B, where D is diagonal.
89a455f92dSJames Wright // @details A is NxM, D is diagonal NxN, represented by a vector of length N, and B is NxM. Optionally, A may be transposed.
90a455f92dSJames Wright CEED_QFUNCTION_HELPER void MatDiagNM(const CeedScalar *A, const CeedScalar *D, const CeedInt N, const CeedInt M, const CeedTransposeMode transpose_A,
91a455f92dSJames Wright                                      CeedScalar *B) {
92a455f92dSJames Wright   switch (transpose_A) {
93a455f92dSJames Wright     case CEED_NOTRANSPOSE:
94a455f92dSJames Wright       CeedPragmaSIMD for (CeedInt i = 0; i < N; i++) { CeedPragmaSIMD for (CeedInt j = 0; j < M; j++) B[i * M + j] += D[i] * A[i * M + j]; }
95a455f92dSJames Wright       break;
96a455f92dSJames Wright     case CEED_TRANSPOSE:
97a455f92dSJames Wright       CeedPragmaSIMD for (CeedInt i = 0; i < M; i++) { CeedPragmaSIMD for (CeedInt j = 0; j < N; j++) B[i * N + j] += D[i] * A[j * M + i]; }
98a455f92dSJames Wright       break;
99a455f92dSJames Wright   }
100a455f92dSJames Wright }
101a455f92dSJames Wright 
102a455f92dSJames Wright // @brief 3x3 Matrix-Matrix product, B = DA + B, where D is diagonal.
103a455f92dSJames Wright // @details Optionally, A may be transposed.
104a455f92dSJames Wright CEED_QFUNCTION_HELPER void MatDiag3(const CeedScalar A[3][3], const CeedScalar D[3], const CeedTransposeMode transpose_A, CeedScalar B[3][3]) {
105a455f92dSJames Wright   MatDiagNM((const CeedScalar *)A, (const CeedScalar *)D, 3, 3, transpose_A, (CeedScalar *)B);
106a455f92dSJames Wright }
107a455f92dSJames Wright 
108a455f92dSJames Wright // @brief 3x3 Matrix-Matrix product, C = AB + C
109a455f92dSJames Wright CEED_QFUNCTION_HELPER void MatMat3(const CeedScalar A[3][3], const CeedScalar B[3][3], const CeedTransposeMode transpose_A,
110a455f92dSJames Wright                                    const CeedTransposeMode transpose_B, CeedScalar C[3][3]) {
111a455f92dSJames Wright   switch (transpose_A) {
112a455f92dSJames Wright     case CEED_NOTRANSPOSE:
113a455f92dSJames Wright       switch (transpose_B) {
114a455f92dSJames Wright         case CEED_NOTRANSPOSE:
115a455f92dSJames Wright           CeedPragmaSIMD for (CeedInt i = 0; i < 3; i++) {
116a455f92dSJames Wright             CeedPragmaSIMD for (CeedInt j = 0; j < 3; j++) { CeedPragmaSIMD for (CeedInt k = 0; k < 3; k++) C[i][j] += A[i][k] * B[k][j]; }
117a455f92dSJames Wright           }
118a455f92dSJames Wright           break;
119a455f92dSJames Wright         case CEED_TRANSPOSE:
120a455f92dSJames Wright           CeedPragmaSIMD for (CeedInt i = 0; i < 3; i++) {
121a455f92dSJames Wright             CeedPragmaSIMD for (CeedInt j = 0; j < 3; j++) { CeedPragmaSIMD for (CeedInt k = 0; k < 3; k++) C[i][j] += A[i][k] * B[j][k]; }
122a455f92dSJames Wright           }
123a455f92dSJames Wright           break;
124a455f92dSJames Wright       }
125a455f92dSJames Wright       break;
126a455f92dSJames Wright     case CEED_TRANSPOSE:
127a455f92dSJames Wright       switch (transpose_B) {
128a455f92dSJames Wright         case CEED_NOTRANSPOSE:
129a455f92dSJames Wright           CeedPragmaSIMD for (CeedInt i = 0; i < 3; i++) {
130a455f92dSJames Wright             CeedPragmaSIMD for (CeedInt j = 0; j < 3; j++) { CeedPragmaSIMD for (CeedInt k = 0; k < 3; k++) C[i][j] += A[k][i] * B[k][j]; }
131a455f92dSJames Wright           }
132a455f92dSJames Wright           break;
133a455f92dSJames Wright         case CEED_TRANSPOSE:
134a455f92dSJames Wright           CeedPragmaSIMD for (CeedInt i = 0; i < 3; i++) {
135a455f92dSJames Wright             CeedPragmaSIMD for (CeedInt j = 0; j < 3; j++) { CeedPragmaSIMD for (CeedInt k = 0; k < 3; k++) C[i][j] += A[k][i] * B[j][k]; }
136a455f92dSJames Wright           }
137a455f92dSJames Wright           break;
138a455f92dSJames Wright       }
139a455f92dSJames Wright       break;
140a455f92dSJames Wright   }
141a455f92dSJames Wright }
142a455f92dSJames Wright 
14313fa47b2SJames Wright // @brief Unpack Kelvin-Mandel notation symmetric tensor into full tensor
14413fa47b2SJames Wright CEED_QFUNCTION_HELPER void KMUnpack(const CeedScalar v[6], CeedScalar A[3][3]) {
14513fa47b2SJames Wright   const CeedScalar weight = 1 / sqrt(2.);
14613fa47b2SJames Wright   A[0][0]                 = v[0];
14713fa47b2SJames Wright   A[1][1]                 = v[1];
14813fa47b2SJames Wright   A[2][2]                 = v[2];
14913fa47b2SJames Wright   A[2][1] = A[1][2] = weight * v[3];
15013fa47b2SJames Wright   A[2][0] = A[0][2] = weight * v[4];
15113fa47b2SJames Wright   A[1][0] = A[0][1] = weight * v[5];
15213fa47b2SJames Wright }
15313fa47b2SJames Wright 
154a455f92dSJames Wright // @brief Pack full tensor into Kelvin-Mandel notation symmetric tensor
155a455f92dSJames Wright CEED_QFUNCTION_HELPER void KMPack(const CeedScalar A[3][3], CeedScalar v[6]) {
156a455f92dSJames Wright   const CeedScalar weight = sqrt(2.);
157a455f92dSJames Wright   v[0]                    = A[0][0];
158a455f92dSJames Wright   v[1]                    = A[1][1];
159a455f92dSJames Wright   v[2]                    = A[2][2];
160a455f92dSJames Wright   v[3]                    = A[2][1] * weight;
161a455f92dSJames Wright   v[4]                    = A[2][0] * weight;
162a455f92dSJames Wright   v[5]                    = A[1][0] * weight;
163a455f92dSJames Wright }
164a455f92dSJames Wright 
165a455f92dSJames Wright // @brief Calculate metric tensor from mapping, g_{ij} = xi_{k,i} xi_{k,j} = dXdx^T dXdx
166a455f92dSJames Wright CEED_QFUNCTION_HELPER void KMMetricTensor(const CeedScalar dXdx[3][3], CeedScalar km_g_ij[6]) {
167a455f92dSJames Wright   CeedScalar g_ij[3][3] = {{0.}};
168a455f92dSJames Wright   MatMat3(dXdx, dXdx, CEED_TRANSPOSE, CEED_NOTRANSPOSE, g_ij);
169a455f92dSJames Wright   KMPack(g_ij, km_g_ij);
170a455f92dSJames Wright }
171a455f92dSJames Wright 
172530ad8c4SKenneth E. Jansen // @brief Linear ramp evaluation
173530ad8c4SKenneth E. Jansen CEED_QFUNCTION_HELPER CeedScalar LinearRampCoefficient(CeedScalar amplitude, CeedScalar length, CeedScalar start, CeedScalar x) {
174530ad8c4SKenneth E. Jansen   if (x < start) {
175530ad8c4SKenneth E. Jansen     return amplitude;
176530ad8c4SKenneth E. Jansen   } else if (x < start + length) {
177530ad8c4SKenneth E. Jansen     return amplitude * ((x - start) * (-1 / length) + 1);
178530ad8c4SKenneth E. Jansen   } else {
179530ad8c4SKenneth E. Jansen     return 0;
180530ad8c4SKenneth E. Jansen   }
181530ad8c4SKenneth E. Jansen }
182530ad8c4SKenneth E. Jansen 
183f3e15844SJames Wright /**
184f3e15844SJames Wright   @brief Pack stored values at quadrature point
185f3e15844SJames Wright 
186f3e15844SJames Wright   @param[in]   Q              Number of quadrature points
187f3e15844SJames Wright   @param[in]   i              Current quadrature point
188f3e15844SJames Wright   @param[in]   start          Starting index to store components
189f3e15844SJames Wright   @param[in]   num_comp       Number of components to store
190*4e5897fcSJames Wright   @param[in]   values_at_qpnt Local values for quadrature point i
191f3e15844SJames Wright   @param[out]  stored         Stored values
192f3e15844SJames Wright 
193f3e15844SJames Wright   @return An error code: 0 - success, otherwise - failure
194f3e15844SJames Wright **/
195*4e5897fcSJames Wright CEED_QFUNCTION_HELPER int StoredValuesPack(CeedInt Q, CeedInt i, CeedInt start, CeedInt num_comp, const CeedScalar *values_at_qpnt,
196*4e5897fcSJames Wright                                            CeedScalar *stored) {
197*4e5897fcSJames Wright   for (CeedInt j = 0; j < num_comp; j++) stored[(start + j) * Q + i] = values_at_qpnt[j];
198f3e15844SJames Wright 
199f3e15844SJames Wright   return CEED_ERROR_SUCCESS;
200f3e15844SJames Wright }
201f3e15844SJames Wright 
202f3e15844SJames Wright /**
203f3e15844SJames Wright   @brief Unpack stored values at quadrature point
204f3e15844SJames Wright 
205f3e15844SJames Wright   @param[in]   Q              Number of quadrature points
206f3e15844SJames Wright   @param[in]   i              Current quadrature point
207f3e15844SJames Wright   @param[in]   start          Starting index to store components
208f3e15844SJames Wright   @param[in]   num_comp       Number of components to store
209f3e15844SJames Wright   @param[in]   stored         Stored values
210*4e5897fcSJames Wright   @param[out]  values_at_qpnt Local values for quadrature point i
211f3e15844SJames Wright 
212f3e15844SJames Wright   @return An error code: 0 - success, otherwise - failure
213f3e15844SJames Wright **/
214*4e5897fcSJames Wright CEED_QFUNCTION_HELPER int StoredValuesUnpack(CeedInt Q, CeedInt i, CeedInt start, CeedInt num_comp, const CeedScalar *stored,
215*4e5897fcSJames Wright                                              CeedScalar *values_at_qpnt) {
216*4e5897fcSJames Wright   for (CeedInt j = 0; j < num_comp; j++) values_at_qpnt[j] = stored[(start + j) * Q + i];
217f3e15844SJames Wright 
218f3e15844SJames Wright   return CEED_ERROR_SUCCESS;
219f3e15844SJames Wright }
220f3e15844SJames Wright 
221f3e15844SJames Wright /**
222f3e15844SJames Wright   @brief Unpack 3D element q_data at quadrature point
223f3e15844SJames Wright 
224f3e15844SJames Wright   @param[in]   Q         Number of quadrature points
225f3e15844SJames Wright   @param[in]   i         Current quadrature point
226f3e15844SJames Wright   @param[in]   q_data    Pointer to q_data (generated by `setupgeo.h:Setup`)
227f3e15844SJames Wright   @param[out]  wdetJ     Quadrature weight times determinant of the mapping Jacobian
228f3e15844SJames Wright   @param[out]  dXdx      Inverse of the mapping Jacobian (shape [3][3])
229f3e15844SJames Wright 
230f3e15844SJames Wright   @return An error code: 0 - success, otherwise - failure
231f3e15844SJames Wright **/
232f3e15844SJames Wright CEED_QFUNCTION_HELPER int QdataUnpack_3D(CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar dXdx[3][3]) {
233f3e15844SJames Wright   StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
234f3e15844SJames Wright   StoredValuesUnpack(Q, i, 1, 9, q_data, (CeedScalar *)dXdx);
235f3e15844SJames Wright   return CEED_ERROR_SUCCESS;
236f3e15844SJames Wright }
237f3e15844SJames Wright 
238f3e15844SJames Wright /**
239f3e15844SJames Wright   @brief Unpack boundary element q_data for 3D problem at quadrature point
240f3e15844SJames Wright 
241f3e15844SJames Wright   @param[in]   Q         Number of quadrature points
242f3e15844SJames Wright   @param[in]   i         Current quadrature point
243f3e15844SJames Wright   @param[in]   q_data    Pointer to q_data (generated by `setupgeo.h:Setup`)
244f3e15844SJames Wright   @param[out]  wdetJ     Quadrature weight times determinant of the mapping Jacobian, or `NULL`
245f3e15844SJames Wright   @param[out]  dXdx      Inverse of the mapping Jacobian (shape [2][3]), or `NULL`
246f3e15844SJames Wright   @param[out]  normal    Components of the normal vector (shape [3]), or `NULL`
247f3e15844SJames Wright 
248f3e15844SJames Wright   @return An error code: 0 - success, otherwise - failure
249f3e15844SJames Wright **/
250f3e15844SJames Wright CEED_QFUNCTION_HELPER int QdataBoundaryUnpack_3D(CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar dXdx[2][3],
251f3e15844SJames Wright                                                  CeedScalar normal[3]) {
252f3e15844SJames Wright   if (wdetJ) StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
253f3e15844SJames Wright   if (normal) StoredValuesUnpack(Q, i, 1, 3, q_data, normal);
254f3e15844SJames Wright   if (dXdx) StoredValuesUnpack(Q, i, 4, 6, q_data, (CeedScalar *)dXdx);
255f3e15844SJames Wright   return CEED_ERROR_SUCCESS;
256f3e15844SJames Wright }
257f3e15844SJames Wright 
25813fa47b2SJames Wright #endif  // utils_h
259