1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3cb32e2e7SValeria Barra //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5cb32e2e7SValeria Barra //
63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed
7cb32e2e7SValeria Barra
8cb32e2e7SValeria Barra /// @file
9cb32e2e7SValeria Barra /// libCEED QFunctions for diffusion operator example using PETSc
10cb32e2e7SValeria Barra
11c0b5abf0SJeremy L Thompson #include <ceed/types.h>
12c0b5abf0SJeremy L Thompson #ifndef CEED_RUNNING_JIT_PASS
1313921685Svaleriabarra #include <math.h>
14c0b5abf0SJeremy L Thompson #endif
1513921685Svaleriabarra
16e83e87a5Sjeremylt // -----------------------------------------------------------------------------
17ea61e9acSJeremy L Thompson // This QFunction sets up the geometric factors required to apply the diffusion operator
18ed264d09SValeria Barra //
19ea61e9acSJeremy L Thompson // We require the product of the inverse of the Jacobian and its transpose to properly compute integrals of the form: int( gradv gradu)
20ed264d09SValeria Barra //
21ed264d09SValeria Barra // Determinant of Jacobian:
22ed264d09SValeria Barra // detJ = J11*A11 + J21*A12 + J31*A13
23ed264d09SValeria Barra // Jij = Jacobian entry ij
24ed264d09SValeria Barra // Aij = Adjoint ij
25ed264d09SValeria Barra //
26ed264d09SValeria Barra // Inverse of Jacobian:
27ed264d09SValeria Barra // Bij = Aij / detJ
28ed264d09SValeria Barra //
29ed264d09SValeria Barra // Product of Inverse and Transpose:
30ed264d09SValeria Barra // BBij = sum( Bik Bkj )
31ed264d09SValeria Barra //
32ed264d09SValeria Barra // Stored: w B^T B detJ = w A^T A / detJ
33ed264d09SValeria Barra // Note: This matrix is symmetric, so we only store 6 distinct entries
340a8fc04aSrezgarshakeri // qd: 1 4 7
35ed264d09SValeria Barra // 2 5 8
360a8fc04aSrezgarshakeri // 3 6 9
37cb32e2e7SValeria Barra // -----------------------------------------------------------------------------
SetupDiffGeo(void * ctx,CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)382b730f8bSJeremy L Thompson CEED_QFUNCTION(SetupDiffGeo)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
39d4d45553Srezgarshakeri // Inputs
40d4d45553Srezgarshakeri const CeedScalar(*J)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[1];
41d4d45553Srezgarshakeri const CeedScalar(*w) = in[2]; // Note: *X = in[0]
42d4d45553Srezgarshakeri // Outputs
43d4d45553Srezgarshakeri CeedScalar(*qd) = out[0];
44cb32e2e7SValeria Barra
45d4d45553Srezgarshakeri const CeedInt dim = 3;
46cb32e2e7SValeria Barra // Quadrature Point Loop
472b730f8bSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
48d4d45553Srezgarshakeri // Setup
49d4d45553Srezgarshakeri CeedScalar A[3][3];
50d4d45553Srezgarshakeri for (CeedInt j = 0; j < dim; j++) {
51d4d45553Srezgarshakeri for (CeedInt k = 0; k < dim; k++) {
52d4d45553Srezgarshakeri // Equivalent code with no mod operations:
53d4d45553Srezgarshakeri // A[k][j] = J[k+1][j+1]*J[k+2][j+2] - J[k+1][j+2]*J[k+2][j+1]
54d4d45553Srezgarshakeri A[k][j] = J[(k + 1) % dim][(j + 1) % dim][i] * J[(k + 2) % dim][(j + 2) % dim][i] -
55d4d45553Srezgarshakeri J[(k + 1) % dim][(j + 2) % dim][i] * J[(k + 2) % dim][(j + 1) % dim][i];
56d4d45553Srezgarshakeri }
57d4d45553Srezgarshakeri }
58d4d45553Srezgarshakeri const CeedScalar detJ = J[0][0][i] * A[0][0] + J[0][1][i] * A[0][1] + J[0][2][i] * A[0][2];
59d4d45553Srezgarshakeri
60d4d45553Srezgarshakeri const CeedScalar qw = w[i] / detJ;
610a8fc04aSrezgarshakeri qd[i + Q * 0] = w[i] * detJ;
620a8fc04aSrezgarshakeri qd[i + Q * 1] = qw * (A[0][0] * A[0][0] + A[0][1] * A[0][1] + A[0][2] * A[0][2]);
630a8fc04aSrezgarshakeri qd[i + Q * 2] = qw * (A[0][0] * A[1][0] + A[0][1] * A[1][1] + A[0][2] * A[1][2]);
640a8fc04aSrezgarshakeri qd[i + Q * 3] = qw * (A[0][0] * A[2][0] + A[0][1] * A[2][1] + A[0][2] * A[2][2]);
650a8fc04aSrezgarshakeri qd[i + Q * 4] = qw * (A[1][0] * A[1][0] + A[1][1] * A[1][1] + A[1][2] * A[1][2]);
660a8fc04aSrezgarshakeri qd[i + Q * 5] = qw * (A[1][0] * A[2][0] + A[1][1] * A[2][1] + A[1][2] * A[2][2]);
670a8fc04aSrezgarshakeri qd[i + Q * 6] = qw * (A[2][0] * A[2][0] + A[2][1] * A[2][1] + A[2][2] * A[2][2]);
68cb32e2e7SValeria Barra } // End of Quadrature Point Loop
69cb32e2e7SValeria Barra
70cb32e2e7SValeria Barra return 0;
71cb32e2e7SValeria Barra }
72cb32e2e7SValeria Barra
73e83e87a5Sjeremylt // -----------------------------------------------------------------------------
74ed264d09SValeria Barra // This QFunction sets up the rhs and true solution for the problem
75cb32e2e7SValeria Barra // -----------------------------------------------------------------------------
SetupDiffRhs(void * ctx,CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)762b730f8bSJeremy L Thompson CEED_QFUNCTION(SetupDiffRhs)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
77cb32e2e7SValeria Barra #ifndef M_PI
78cb32e2e7SValeria Barra #define M_PI 3.14159265358979323846
79cb32e2e7SValeria Barra #endif
80e83e87a5Sjeremylt const CeedScalar *x = in[0], *w = in[1];
81cb32e2e7SValeria Barra CeedScalar *true_soln = out[0], *rhs = out[1];
82cb32e2e7SValeria Barra
83cb32e2e7SValeria Barra // Quadrature Point Loop
842b730f8bSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
85cb32e2e7SValeria Barra const CeedScalar c[3] = {0, 1., 2.};
86cb32e2e7SValeria Barra const CeedScalar k[3] = {1., 2., 3.};
87cb32e2e7SValeria Barra
882b730f8bSJeremy L Thompson true_soln[i] = sin(M_PI * (c[0] + k[0] * x[i + Q * 0])) * sin(M_PI * (c[1] + k[1] * x[i + Q * 1])) * sin(M_PI * (c[2] + k[2] * x[i + Q * 2]));
89cb32e2e7SValeria Barra
902b730f8bSJeremy L Thompson rhs[i] = w[i + Q * 0] * M_PI * M_PI * (k[0] * k[0] + k[1] * k[1] + k[2] * k[2]) * true_soln[i];
91cb32e2e7SValeria Barra } // End of Quadrature Point Loop
92cb32e2e7SValeria Barra return 0;
93cb32e2e7SValeria Barra }
94cb32e2e7SValeria Barra
95e83e87a5Sjeremylt // -----------------------------------------------------------------------------
96ed264d09SValeria Barra // This QFunction applies the diffusion operator for a scalar field.
97ed264d09SValeria Barra //
98ed264d09SValeria Barra // Inputs:
99ed264d09SValeria Barra // ug - Input vector gradient at quadrature points
1009b072555Sjeremylt // q_data - Geometric factors
101ed264d09SValeria Barra //
102ed264d09SValeria Barra // Output:
103ed264d09SValeria Barra // vg - Output vector (test functions) gradient at quadrature points
104cb32e2e7SValeria Barra // -----------------------------------------------------------------------------
Diff(void * ctx,CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)1052b730f8bSJeremy L Thompson CEED_QFUNCTION(Diff)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1069b072555Sjeremylt const CeedScalar *ug = in[0], *q_data = in[1];
107cb32e2e7SValeria Barra CeedScalar *vg = out[0];
108cb32e2e7SValeria Barra
109cb32e2e7SValeria Barra // Quadrature Point Loop
1102b730f8bSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
111cb32e2e7SValeria Barra // Read spatial derivatives of u
1122b730f8bSJeremy L Thompson const CeedScalar du[3] = {ug[i + Q * 0], ug[i + Q * 1], ug[i + Q * 2]};
1139b072555Sjeremylt // Read q_data (dXdxdXdx_T symmetric matrix)
1142b730f8bSJeremy L Thompson const CeedScalar dXdxdXdx_T[3][3] = {
1152b730f8bSJeremy L Thompson {q_data[i + 1 * Q], q_data[i + 2 * Q], q_data[i + 3 * Q]},
1162b730f8bSJeremy L Thompson {q_data[i + 2 * Q], q_data[i + 4 * Q], q_data[i + 5 * Q]},
1172b730f8bSJeremy L Thompson {q_data[i + 3 * Q], q_data[i + 5 * Q], q_data[i + 6 * Q]}
118cb32e2e7SValeria Barra };
119cb32e2e7SValeria Barra
1202b730f8bSJeremy L Thompson for (int j = 0; j < 3; j++) { // j = direction of vg
1212b730f8bSJeremy L Thompson vg[i + j * Q] = (du[0] * dXdxdXdx_T[0][j] + du[1] * dXdxdXdx_T[1][j] + du[2] * dXdxdXdx_T[2][j]);
1222b730f8bSJeremy L Thompson }
123cb32e2e7SValeria Barra } // End of Quadrature Point Loop
124cb32e2e7SValeria Barra return 0;
125cb32e2e7SValeria Barra }
126cb32e2e7SValeria Barra // -----------------------------------------------------------------------------
127