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