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