xref: /honee/qfunctions/utils.h (revision d8667e38623468ed8757e29a58df3cbc3502b3ab)
1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3 #pragma once
4 
5 #include <ceed/types.h>
6 #ifndef CEED_RUNNING_JIT_PASS
7 #include <math.h>
8 #endif
9 
10 #ifndef M_PI
11 #define M_PI 3.14159265358979323846
12 #endif
13 
14 CEED_QFUNCTION_HELPER CeedScalar Max(CeedScalar a, CeedScalar b) { return a < b ? b : a; }
15 CEED_QFUNCTION_HELPER CeedScalar Min(CeedScalar a, CeedScalar b) { return a < b ? a : b; }
16 
17 CEED_QFUNCTION_HELPER void SwapScalar(CeedScalar *a, CeedScalar *b) {
18   CeedScalar temp = *a;
19   *a              = *b;
20   *b              = temp;
21 }
22 
23 CEED_QFUNCTION_HELPER CeedScalar Square(CeedScalar x) { return x * x; }
24 CEED_QFUNCTION_HELPER CeedScalar Cube(CeedScalar x) { return x * x * x; }
25 
26 // @brief Scale vector of length N by scalar alpha
27 CEED_QFUNCTION_HELPER void ScaleN(CeedScalar *u, const CeedScalar alpha, const CeedInt N) {
28   CeedPragmaSIMD for (CeedInt i = 0; i < N; i++) u[i] *= alpha;
29 }
30 
31 // @brief Set vector of length N to a value alpha
32 CEED_QFUNCTION_HELPER void SetValueN(CeedScalar *u, const CeedScalar alpha, const CeedInt N) {
33   CeedPragmaSIMD for (CeedInt i = 0; i < N; i++) u[i] = alpha;
34 }
35 
36 // @brief Copy N elements from x to y
37 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]; }
38 
39 // @brief Copy 3x3 matrix from A to B
40 CEED_QFUNCTION_HELPER void CopyMat3(const CeedScalar A[3][3], CeedScalar B[3][3]) { CopyN((const CeedScalar *)A, (CeedScalar *)B, 9); }
41 
42 // @brief Dot product of vectors with N elements
43 CEED_QFUNCTION_HELPER CeedScalar DotN(const CeedScalar *u, const CeedScalar *v, const CeedInt N) {
44   CeedScalar output = 0;
45   CeedPragmaSIMD for (CeedInt i = 0; i < N; i++) output += u[i] * v[i];
46   return output;
47 }
48 
49 // @brief Dot product of 3 element vectors
50 CEED_QFUNCTION_HELPER CeedScalar Dot3(const CeedScalar *u, const CeedScalar *v) { return u[0] * v[0] + u[1] * v[1] + u[2] * v[2]; }
51 
52 // @brief \ell^2 norm of 3 element vectors
53 CEED_QFUNCTION_HELPER CeedScalar Norm3(const CeedScalar *u) { return sqrt(u[0] * u[0] + u[1] * u[1] + u[2] * u[2]); }
54 
55 // @brief \ell^2 norm of 2 element vectors
56 CEED_QFUNCTION_HELPER CeedScalar Norm2(const CeedScalar *u) { return sqrt(u[0] * u[0] + u[1] * u[1]); }
57 
58 // @brief Cross product of vectors with 3 elements
59 CEED_QFUNCTION_HELPER void Cross3(const CeedScalar u[3], const CeedScalar v[3], CeedScalar w[3]) {
60   w[0] = (u[1] * v[2]) - (u[2] * v[1]);
61   w[1] = (u[2] * v[0]) - (u[0] * v[2]);
62   w[2] = (u[0] * v[1]) - (u[1] * v[0]);
63 }
64 
65 // @brief Curl of vector given its gradient
66 CEED_QFUNCTION_HELPER void Curl3(const CeedScalar gradient[3][3], CeedScalar v[3]) {
67   v[0] = gradient[2][1] - gradient[1][2];
68   v[1] = gradient[0][2] - gradient[2][0];
69   v[2] = gradient[1][0] - gradient[0][1];
70 }
71 
72 // @brief Matrix vector product, b = Ax + b. A is NxM, x is M, b is N
73 CEED_QFUNCTION_HELPER void MatVecNM(const CeedScalar *A, const CeedScalar *x, const CeedInt N, const CeedInt M, const CeedTransposeMode transpose_A,
74                                     CeedScalar *b) {
75   switch (transpose_A) {
76     case CEED_NOTRANSPOSE:
77       CeedPragmaSIMD for (CeedInt i = 0; i < N; i++) b[i] += DotN(&A[i * M], x, M);
78       break;
79     case CEED_TRANSPOSE:
80       CeedPragmaSIMD for (CeedInt i = 0; i < M; i++) { CeedPragmaSIMD for (CeedInt j = 0; j < N; j++) b[i] += A[j * M + i] * x[j]; }
81       break;
82   }
83 }
84 
85 // @brief 3x3 Matrix vector product  b = Ax + b.
86 CEED_QFUNCTION_HELPER void MatVec3(const CeedScalar A[3][3], const CeedScalar x[3], const CeedTransposeMode transpose_A, CeedScalar b[3]) {
87   MatVecNM((const CeedScalar *)A, (const CeedScalar *)x, 3, 3, transpose_A, (CeedScalar *)b);
88 }
89 
90 // @brief Matrix-Matrix product, B = DA + B, where D is diagonal.
91 // @details A is NxM, D is diagonal NxN, represented by a vector of length N, and B is NxM. Optionally, A may be transposed.
92 CEED_QFUNCTION_HELPER void MatDiagNM(const CeedScalar *A, const CeedScalar *D, const CeedInt N, const CeedInt M, const CeedTransposeMode transpose_A,
93                                      CeedScalar *B) {
94   switch (transpose_A) {
95     case CEED_NOTRANSPOSE:
96       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]; }
97       break;
98     case CEED_TRANSPOSE:
99       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]; }
100       break;
101   }
102 }
103 
104 // @brief 3x3 Matrix-Matrix product, B = DA + B, where D is diagonal.
105 // @details Optionally, A may be transposed.
106 CEED_QFUNCTION_HELPER void MatDiag3(const CeedScalar A[3][3], const CeedScalar D[3], const CeedTransposeMode transpose_A, CeedScalar B[3][3]) {
107   MatDiagNM((const CeedScalar *)A, (const CeedScalar *)D, 3, 3, transpose_A, (CeedScalar *)B);
108 }
109 // @brief NxN Matrix-Matrix product, C = AB + C
110 CEED_QFUNCTION_HELPER void MatMatN(const CeedScalar *A, const CeedScalar *B, const CeedInt N, const CeedTransposeMode transpose_A,
111                                    const CeedTransposeMode transpose_B, CeedScalar *C) {
112   switch (transpose_A) {
113     case CEED_NOTRANSPOSE:
114       switch (transpose_B) {
115         case CEED_NOTRANSPOSE:
116           CeedPragmaSIMD for (CeedInt i = 0; i < N; i++) {
117             CeedPragmaSIMD for (CeedInt j = 0; j < N; j++) {
118               CeedPragmaSIMD for (CeedInt k = 0; k < N; k++) C[i * N + j] += A[i * N + k] * B[k * N + j];
119             }
120           }
121           break;
122         case CEED_TRANSPOSE:
123           CeedPragmaSIMD for (CeedInt i = 0; i < N; i++) {
124             CeedPragmaSIMD for (CeedInt j = 0; j < N; j++) {
125               CeedPragmaSIMD for (CeedInt k = 0; k < N; k++) C[i * N + j] += A[i * N + k] * B[j * N + k];
126             }
127           }
128           break;
129       }
130       break;
131     case CEED_TRANSPOSE:
132       switch (transpose_B) {
133         case CEED_NOTRANSPOSE:
134           CeedPragmaSIMD for (CeedInt i = 0; i < N; i++) {
135             CeedPragmaSIMD for (CeedInt j = 0; j < N; j++) {
136               CeedPragmaSIMD for (CeedInt k = 0; k < N; k++) C[i * N + j] += A[k * N + i] * B[k * N + j];
137             }
138           }
139           break;
140         case CEED_TRANSPOSE:
141           CeedPragmaSIMD for (CeedInt i = 0; i < N; i++) {
142             CeedPragmaSIMD for (CeedInt j = 0; j < N; j++) {
143               CeedPragmaSIMD for (CeedInt k = 0; k < N; k++) C[i * N + j] += A[k * N + i] * B[j * N + k];
144             }
145           }
146           break;
147       }
148       break;
149   }
150 }
151 
152 // @brief 3x3 Matrix-Matrix product, C = AB + C
153 CEED_QFUNCTION_HELPER void MatMat3(const CeedScalar A[3][3], const CeedScalar B[3][3], const CeedTransposeMode transpose_A,
154                                    const CeedTransposeMode transpose_B, CeedScalar C[3][3]) {
155   MatMatN((const CeedScalar *)A, (const CeedScalar *)B, 3, transpose_A, transpose_B, (CeedScalar *)C);
156 }
157 
158 /**
159  * @brief Calculate inverse of 2x2 matrix
160  *
161  * @param[in]  A        Input matrix
162  * @param[out] detJ_ptr Determinate of A, may be NULL is not desired
163  * @param[out] A_inv    Output matrix inverse
164  */
165 CEED_QFUNCTION_HELPER void MatInv2(const CeedScalar A[2][2], CeedScalar A_inv[2][2], CeedScalar *detJ_ptr) {
166   const CeedScalar detJ = A[0][0] * A[1][1] - A[1][0] * A[0][1];
167 
168   A_inv[0][0] = A[1][1] / detJ;
169   A_inv[0][1] = -A[0][1] / detJ;
170   A_inv[1][0] = -A[1][0] / detJ;
171   A_inv[1][1] = A[0][0] / detJ;
172   if (detJ_ptr) *detJ_ptr = detJ;
173 }
174 
175 /**
176  * @brief Calculate inverse of 3x3 matrix
177  *
178  * @param[in]  A        Input matrix
179  * @param[out] detJ_ptr Determinate of A, may be NULL is not desired
180  * @param[out] A_inv    Output matrix inverse
181  */
182 CEED_QFUNCTION_HELPER void MatInv3(const CeedScalar A[3][3], CeedScalar A_inv[3][3], CeedScalar *detJ_ptr) {
183   // Compute Adjugate of dxdX
184   A_inv[0][0] = A[1][1] * A[2][2] - A[1][2] * A[2][1];
185   A_inv[0][1] = A[0][2] * A[2][1] - A[0][1] * A[2][2];
186   A_inv[0][2] = A[0][1] * A[1][2] - A[0][2] * A[1][1];
187   A_inv[1][0] = A[1][2] * A[2][0] - A[1][0] * A[2][2];
188   A_inv[1][1] = A[0][0] * A[2][2] - A[0][2] * A[2][0];
189   A_inv[1][2] = A[0][2] * A[1][0] - A[0][0] * A[1][2];
190   A_inv[2][0] = A[1][0] * A[2][1] - A[1][1] * A[2][0];
191   A_inv[2][1] = A[0][1] * A[2][0] - A[0][0] * A[2][1];
192   A_inv[2][2] = A[0][0] * A[1][1] - A[0][1] * A[1][0];
193 
194   const CeedScalar detJ = A[0][0] * A_inv[0][0] + A[1][0] * A_inv[0][1] + A[2][0] * A_inv[0][2];
195   ScaleN((CeedScalar *)A_inv, 1 / detJ, 9);
196   if (detJ_ptr) *detJ_ptr = detJ;
197 }
198 
199 /**
200   @brief MxN Matrix-Matrix product, C = AB + C
201 
202   C is NxM, A is NxP, B is PxM
203 
204   @param[in]  mat_A Row-major matrix `A`
205   @param[in]  mat_B Row-major matrix `B`
206   @param[out] mat_C Row-major output matrix `C`
207   @param[in]  N     Number of rows of `C`
208   @param[in]  M     Number of columns of `C`
209   @param[in]  P     Number of columns of `A`/rows of `B`
210 **/
211 CEED_QFUNCTION_HELPER void MatMatNM(const CeedScalar *mat_A, const CeedScalar *mat_B, CeedScalar *mat_C, CeedInt N, CeedInt M, CeedInt P) {
212   for (CeedInt i = 0; i < N; i++) {
213     for (CeedInt j = 0; j < M; j++) {
214       for (CeedInt k = 0; k < P; k++) mat_C[i * M + j] += mat_A[i * P + k] * mat_B[k * M + j];
215     }
216   }
217 }
218 
219 // @brief Unpack Kelvin-Mandel notation symmetric tensor into full tensor
220 CEED_QFUNCTION_HELPER void KMUnpack(const CeedScalar v[6], CeedScalar A[3][3]) {
221   const CeedScalar weight = 1 / sqrt(2.);
222   A[0][0]                 = v[0];
223   A[1][1]                 = v[1];
224   A[2][2]                 = v[2];
225   A[2][1] = A[1][2] = weight * v[3];
226   A[2][0] = A[0][2] = weight * v[4];
227   A[1][0] = A[0][1] = weight * v[5];
228 }
229 
230 // @brief Pack full tensor into Kelvin-Mandel notation symmetric tensor
231 CEED_QFUNCTION_HELPER void KMPack(const CeedScalar A[3][3], CeedScalar v[6]) {
232   const CeedScalar weight = sqrt(2.);
233   v[0]                    = A[0][0];
234   v[1]                    = A[1][1];
235   v[2]                    = A[2][2];
236   v[3]                    = A[2][1] * weight;
237   v[4]                    = A[2][0] * weight;
238   v[5]                    = A[1][0] * weight;
239 }
240 
241 // @brief Calculate metric tensor from mapping, g_{ij} = xi_{k,i} xi_{k,j} = dXdx^T dXdx
242 CEED_QFUNCTION_HELPER void KMMetricTensor(const CeedScalar dXdx[3][3], CeedScalar km_g_ij[6]) {
243   CeedScalar g_ij[3][3] = {{0.}};
244   MatMat3(dXdx, dXdx, CEED_TRANSPOSE, CEED_NOTRANSPOSE, g_ij);
245   KMPack(g_ij, km_g_ij);
246 }
247 
248 // @brief Linear ramp evaluation
249 CEED_QFUNCTION_HELPER CeedScalar LinearRampCoefficient(CeedScalar amplitude, CeedScalar length, CeedScalar start, CeedScalar x) {
250   if (x < start) {
251     return amplitude;
252   } else if (x < start + length) {
253     return amplitude * ((x - start) * (-1 / length) + 1);
254   } else {
255     return 0;
256   }
257 }
258 
259 /**
260   @brief Pack stored values at quadrature point
261 
262   @param[in]   Q              Number of quadrature points
263   @param[in]   i              Current quadrature point
264   @param[in]   start          Starting index to store components
265   @param[in]   num_comp       Number of components to store
266   @param[in]   values_at_qpnt Local values for quadrature point i
267   @param[out]  stored         Stored values
268 
269   @return An error code: 0 - success, otherwise - failure
270 **/
271 CEED_QFUNCTION_HELPER int StoredValuesPack(CeedInt Q, CeedInt i, CeedInt start, CeedInt num_comp, const CeedScalar *values_at_qpnt,
272                                            CeedScalar *stored) {
273   for (CeedInt j = 0; j < num_comp; j++) stored[(start + j) * Q + i] = values_at_qpnt[j];
274 
275   return CEED_ERROR_SUCCESS;
276 }
277 
278 /**
279   @brief Unpack stored values at quadrature point
280 
281   @param[in]   Q              Number of quadrature points
282   @param[in]   i              Current quadrature point
283   @param[in]   start          Starting index to store components
284   @param[in]   num_comp       Number of components to store
285   @param[in]   stored         Stored values
286   @param[out]  values_at_qpnt Local values for quadrature point i
287 
288   @return An error code: 0 - success, otherwise - failure
289 **/
290 CEED_QFUNCTION_HELPER int StoredValuesUnpack(CeedInt Q, CeedInt i, CeedInt start, CeedInt num_comp, const CeedScalar *stored,
291                                              CeedScalar *values_at_qpnt) {
292   for (CeedInt j = 0; j < num_comp; j++) values_at_qpnt[j] = stored[(start + j) * Q + i];
293 
294   return CEED_ERROR_SUCCESS;
295 }
296 
297 /**
298   @brief Unpack N-D element q_data at quadrature point
299 
300   @param[in]   dim       Dimension of the element
301   @param[in]   Q         Number of quadrature points
302   @param[in]   i         Current quadrature point
303   @param[in]   q_data    Pointer to q_data (generated by `setupgeo.h:Setup`)
304   @param[out]  wdetJ     Quadrature weight times determinant of the mapping Jacobian, or `NULL`
305   @param[out]  dXdx      Inverse of the mapping Jacobian (shape [dim][dim]), or `NULL`
306 
307   @return An error code: 0 - success, otherwise - failure
308 **/
309 CEED_QFUNCTION_HELPER int QdataUnpack_ND(CeedInt dim, CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar *dXdx) {
310   switch (dim) {
311     case 2:
312       if (wdetJ) StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
313       if (dXdx) StoredValuesUnpack(Q, i, 1, 4, q_data, dXdx);
314       break;
315     case 3:
316       if (wdetJ) StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
317       if (dXdx) StoredValuesUnpack(Q, i, 1, 9, q_data, dXdx);
318       break;
319   }
320   return CEED_ERROR_SUCCESS;
321 }
322 
323 /**
324   @brief Unpack boundary element q_data for N-D problem at quadrature point
325 
326   @param[in]   dim       Dimension of the element
327   @param[in]   Q         Number of quadrature points
328   @param[in]   i         Current quadrature point
329   @param[in]   q_data    Pointer to q_data (generated by `setupgeo.h:SetupBoundary`)
330   @param[out]  wdetJ     Quadrature weight times determinant of the mapping Jacobian, or `NULL`
331   @param[out]  dXdx      Inverse of the mapping Jacobian (shape [dim - 1][dim]), or `NULL`
332   @param[out]  normal    Components of the normal vector (shape [dim]), or `NULL`
333 
334   @return An error code: 0 - success, otherwise - failure
335 **/
336 CEED_QFUNCTION_HELPER int QdataBoundaryUnpack_ND(CeedInt dim, CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar *dXdx,
337                                                  CeedScalar *normal) {
338   switch (dim) {
339     case 2:
340       if (wdetJ) StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
341       if (normal) StoredValuesUnpack(Q, i, 1, 2, q_data, normal);
342       break;
343     case 3:
344       if (wdetJ) StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
345       if (normal) StoredValuesUnpack(Q, i, 1, 3, q_data, normal);
346       if (dXdx) StoredValuesUnpack(Q, i, 4, 6, q_data, (CeedScalar *)dXdx);
347       break;
348   }
349   return CEED_ERROR_SUCCESS;
350 }
351 
352 /**
353   @brief Unpack boundary element q_data for N-D problem at quadrature point
354 
355   @param[in]   dim       Dimension of the element
356   @param[in]   Q         Number of quadrature points
357   @param[in]   i         Current quadrature point
358   @param[in]   q_data    Pointer to q_data (generated by `setupgeo.h:SetupBoundaryGradient`)
359   @param[out]  wdetJ     Quadrature weight times determinant of the mapping Jacobian, or `NULL`
360   @param[out]  dXdx      Inverse of the mapping Jacobian (shape [dim][dim]), or `NULL`
361   @param[out]  normal    Components of the normal vector (shape [dim]), or `NULL`
362 
363   @return An error code: 0 - success, otherwise - failure
364 **/
365 CEED_QFUNCTION_HELPER int QdataBoundaryGradientUnpack_ND(CeedInt dim, CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ,
366                                                          CeedScalar *dXdx, CeedScalar *normal) {
367   switch (dim) {
368     case 2:
369       if (wdetJ) StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
370       if (dXdx) StoredValuesUnpack(Q, i, 1, 4, q_data, dXdx);
371       if (normal) StoredValuesUnpack(Q, i, 5, 2, q_data, normal);
372       break;
373     case 3:
374       if (wdetJ) StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
375       if (dXdx) StoredValuesUnpack(Q, i, 1, 9, q_data, dXdx);
376       if (normal) StoredValuesUnpack(Q, i, 10, 3, q_data, normal);
377       break;
378   }
379   return CEED_ERROR_SUCCESS;
380 }
381 
382 /**
383   @brief Unpack 3D element q_data at quadrature point
384 
385   @param[in]   Q         Number of quadrature points
386   @param[in]   i         Current quadrature point
387   @param[in]   q_data    Pointer to q_data (generated by `setupgeo.h:Setup`)
388   @param[out]  wdetJ     Quadrature weight times determinant of the mapping Jacobian
389   @param[out]  dXdx      Inverse of the mapping Jacobian (shape [3][3])
390 
391   @return An error code: 0 - success, otherwise - failure
392 **/
393 CEED_QFUNCTION_HELPER int QdataUnpack_3D(CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar dXdx[3][3]) {
394   return QdataUnpack_ND(3, Q, i, q_data, wdetJ, (CeedScalar *)dXdx);
395 }
396 
397 /**
398   @brief Unpack boundary element q_data for 3D problem at quadrature point
399 
400   @param[in]   Q         Number of quadrature points
401   @param[in]   i         Current quadrature point
402   @param[in]   q_data    Pointer to q_data (generated by `setupgeo.h:SetupBoundary`)
403   @param[out]  wdetJ     Quadrature weight times determinant of the mapping Jacobian, or `NULL`
404   @param[out]  dXdx      Inverse of the mapping Jacobian (shape [2][3]), or `NULL`
405   @param[out]  normal    Components of the normal vector (shape [3]), or `NULL`
406 
407   @return An error code: 0 - success, otherwise - failure
408 **/
409 CEED_QFUNCTION_HELPER int QdataBoundaryUnpack_3D(CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar dXdx[2][3],
410                                                  CeedScalar normal[3]) {
411   return QdataBoundaryUnpack_ND(3, Q, i, q_data, wdetJ, (CeedScalar *)dXdx, normal);
412 }
413 
414 /**
415   @brief Unpack boundary element q_data for 3D problem at quadrature point
416 
417   @param[in]   Q         Number of quadrature points
418   @param[in]   i         Current quadrature point
419   @param[in]   q_data    Pointer to q_data (generated by `setupgeo.h:SetupBoundary`)
420   @param[out]  wdetJ     Quadrature weight times determinant of the mapping Jacobian, or `NULL`
421   @param[out]  dXdx      Inverse of the mapping Jacobian (shape [3][3]), or `NULL`
422   @param[out]  normal    Components of the normal vector (shape [3]), or `NULL`
423 
424   @return An error code: 0 - success, otherwise - failure
425 **/
426 CEED_QFUNCTION_HELPER int QdataBoundaryGradientUnpack_3D(CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar dXdx[3][3],
427                                                          CeedScalar normal[3]) {
428   return QdataBoundaryGradientUnpack_ND(3, Q, i, q_data, wdetJ, (CeedScalar *)dXdx, normal);
429 }
430 
431 /**
432   @brief Unpack 2D element q_data at quadrature point
433 
434   @param[in]   Q         Number of quadrature points
435   @param[in]   i         Current quadrature point
436   @param[in]   q_data    Pointer to q_data (generated by `setupgeo.h:Setup`)
437   @param[out]  wdetJ     Quadrature weight times determinant of the mapping Jacobian
438   @param[out]  dXdx      Inverse of the mapping Jacobian (shape [2][2])
439 
440   @return An error code: 0 - success, otherwise - failure
441 **/
442 CEED_QFUNCTION_HELPER int QdataUnpack_2D(CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar dXdx[2][2]) {
443   QdataUnpack_ND(2, Q, i, q_data, wdetJ, (CeedScalar *)dXdx);
444   return CEED_ERROR_SUCCESS;
445 }
446 
447 /**
448   @brief Unpack boundary element q_data for 2D problem at quadrature point
449 
450   @param[in]   Q         Number of quadrature points
451   @param[in]   i         Current quadrature point
452   @param[in]   q_data    Pointer to q_data (generated by `setupgeo.h:SetupBoundary2d`)
453   @param[out]  wdetJ     Quadrature weight times determinant of the mapping Jacobian, or `NULL`
454   @param[out]  normal    Components of the normal vector (shape [2]), or `NULL`
455 
456   @return An error code: 0 - success, otherwise - failure
457 **/
458 CEED_QFUNCTION_HELPER int QdataBoundaryUnpack_2D(CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar normal[2]) {
459   QdataBoundaryUnpack_ND(3, Q, i, q_data, wdetJ, NULL, normal);
460   return CEED_ERROR_SUCCESS;
461 }
462 
463 /**
464   @brief Unpack boundary element q_data for 2D problem at quadrature point
465 
466   @param[in]   Q         Number of quadrature points
467   @param[in]   i         Current quadrature point
468   @param[in]   q_data    Pointer to q_data (generated by `setupgeo.h:SetupBoundary`)
469   @param[out]  wdetJ     Quadrature weight times determinant of the mapping Jacobian, or `NULL`
470   @param[out]  dXdx      Inverse of the mapping Jacobian (shape [2][2]), or `NULL`
471   @param[out]  normal    Components of the normal vector (shape [2]), or `NULL`
472 
473   @return An error code: 0 - success, otherwise - failure
474 **/
475 CEED_QFUNCTION_HELPER int QdataBoundaryGradientUnpack_2D(CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar dXdx[2][2],
476                                                          CeedScalar normal[2]) {
477   return QdataBoundaryGradientUnpack_ND(2, Q, i, q_data, wdetJ, (CeedScalar *)dXdx, normal);
478 }
479 
480 /**
481   @brief Unpack `CEED_EVAL_GRAD` QF input into quadrature-point local array
482 
483   @param[in]  Q          Number of quadrature points
484   @param[in]  i          Current quadrature point
485   @param[in]  num_comp   Number of components of the input
486   @param[in]  dim        Topological dimension of the element (ie. number of derivative terms per component)
487   @param[in]  grad       QF gradient input, shape `[dim][num_comp][Q]`
488   @param[out] grad_local Gradient array at quadrature point Q, shape `[num_comp][dim]`
489 **/
490 CEED_QFUNCTION_HELPER void GradUnpackN(CeedInt Q, CeedInt i, CeedInt num_comp, CeedInt dim, const CeedScalar *grad, CeedScalar *grad_local) {
491   for (CeedInt d = 0; d < dim; d++) {
492     for (CeedInt c = 0; c < num_comp; c++) {
493       grad_local[dim * c + d] = grad[(Q * num_comp) * d + Q * c + i];
494     }
495   }
496 }
497 
498 /**
499   @brief Unpack `CEED_EVAL_GRAD` QF input into quadrature-point local array for 3D elements
500 
501   @param[in]  Q          Number of quadrature points
502   @param[in]  i          Current quadrature point
503   @param[in]  num_comp   Number of components of the input
504   @param[in]  grad       QF gradient input, shape `[3][num_comp][Q]`
505   @param[out] grad_local Gradient array at quadrature point Q, shape `[num_comp][3]`
506 **/
507 CEED_QFUNCTION_HELPER void GradUnpack3(CeedInt Q, CeedInt i, CeedInt num_comp, const CeedScalar *grad, CeedScalar (*grad_local)[3]) {
508   GradUnpackN(Q, i, num_comp, 3, grad, (CeedScalar *)grad_local);
509 }
510 
511 /**
512   @brief Unpack `CEED_EVAL_GRAD` QF input into quadrature-point local array for 2D elements
513 
514   @param[in]  Q          Number of quadrature points
515   @param[in]  i          Current quadrature point
516   @param[in]  num_comp   Number of components of the input
517   @param[in]  grad       QF gradient input, shape `[2][num_comp][Q]`
518   @param[out] grad_local Gradient array at quadrature point Q, shape `[num_comp][2]`
519 **/
520 CEED_QFUNCTION_HELPER void GradUnpack2(CeedInt Q, CeedInt i, CeedInt num_comp, const CeedScalar *grad, CeedScalar (*grad_local)[2]) {
521   GradUnpackN(Q, i, num_comp, 2, grad, (CeedScalar *)grad_local);
522 }
523 
524 /**
525   @brief Calculate divergence from reference gradient
526 
527   Given gradient array G_{ij} and inverse element mapping X_{ij}, then the divergence is
528 
529   G_{ij} X{ji}
530 
531   @param[in]  grad_qn    Gradient array, orientation [vector component][gradient direction]
532   @param[in]  dXdx       Inverse of the mapping Jacobian (shape [dim][dim])
533   @param[in]  dim        Dimension of the problem
534   @param[out] divergence The divergence
535 **/
536 CEED_QFUNCTION_HELPER void DivergenceND(const CeedScalar *grad_qn, const CeedScalar *dXdx, const CeedInt dim, CeedScalar *divergence) {
537   for (CeedInt i = 0; i < dim; i++) {
538     for (CeedInt j = 0; j < dim; j++) {
539       *divergence += grad_qn[i * dim + j] * dXdx[j * dim + i];
540     }
541   }
542 }
543 
544 /**
545   @brief Calculate divergence from reference gradient for 3D problem
546 
547   Given gradient array G_{ij} and inverse element mapping X_{ij}, then the divergence is
548 
549   G_{ij} X{ji}
550 
551   @param[in]  grad_qn    Gradient array, orientation [vector component][gradient direction]
552   @param[in]  dXdx       Inverse of the mapping Jacobian (shape [3][3])
553   @param[out] divergence The divergence
554 **/
555 CEED_QFUNCTION_HELPER void Divergence3D(const CeedScalar grad_qn[3][3], const CeedScalar dXdx[3][3], CeedScalar *divergence) {
556   DivergenceND((const CeedScalar *)grad_qn, (const CeedScalar *)dXdx, 3, divergence);
557 }
558