xref: /libCEED/examples/petsc/qfunctions/bps/bp4.h (revision d416dc2b8eb8ab8cb4fa3546f1e63962299dc06a)
1 // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 /// @file
9 /// libCEED QFunctions for diffusion operator example using PETSc
10 
11 #include <ceed/types.h>
12 #ifndef CEED_RUNNING_JIT_PASS
13 #include <math.h>
14 #endif
15 
16 // -----------------------------------------------------------------------------
17 // This QFunction sets up the rhs and true solution for the problem
18 // -----------------------------------------------------------------------------
19 CEED_QFUNCTION(SetupDiffRhs3)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
20 #ifndef M_PI
21 #define M_PI 3.14159265358979323846
22 #endif
23   const CeedScalar *x = in[0], *w = in[1];
24   CeedScalar       *true_soln = out[0], *rhs = out[1];
25 
26   // Quadrature Point Loop
27   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
28     const CeedScalar c[3] = {0, 1., 2.};
29     const CeedScalar k[3] = {1., 2., 3.};
30 
31     // Component 1
32     true_soln[i + 0 * Q] =
33         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]));
34     // Component 2
35     true_soln[i + 1 * Q] = 2 * true_soln[i + 0 * Q];
36     // Component 3
37     true_soln[i + 2 * Q] = 3 * true_soln[i + 0 * Q];
38 
39     // Component 1
40     rhs[i + 0 * Q] = w[i + Q * 0] * M_PI * M_PI * (k[0] * k[0] + k[1] * k[1] + k[2] * k[2]) * true_soln[i + 0 * Q];
41     // Component 2
42     rhs[i + 1 * Q] = 2 * rhs[i + 0 * Q];
43     // Component 3
44     rhs[i + 2 * Q] = 3 * rhs[i + 0 * Q];
45   }  // End of Quadrature Point Loop
46   return 0;
47 }
48 
49 // -----------------------------------------------------------------------------
50 // This QFunction applies the diffusion operator for a vector field of 3 components.
51 //
52 // Inputs:
53 //   ug      - Input vector Jacobian at quadrature points
54 //   q_data  - Geometric factors
55 //
56 // Output:
57 //   vJ     - Output vector (test functions) Jacobian at quadrature points
58 // -----------------------------------------------------------------------------
59 CEED_QFUNCTION(Diff3)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
60   const CeedScalar *ug = in[0], *q_data = in[1];
61   CeedScalar       *vg = out[0];
62 
63   // Quadrature Point Loop
64   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
65     // Read spatial derivatives of u components
66     const CeedScalar uJ[3][3] = {
67         {ug[i + (0 + 0 * 3) * Q], ug[i + (0 + 1 * 3) * Q], ug[i + (0 + 2 * 3) * Q]},
68         {ug[i + (1 + 0 * 3) * Q], ug[i + (1 + 1 * 3) * Q], ug[i + (1 + 2 * 3) * Q]},
69         {ug[i + (2 + 0 * 3) * Q], ug[i + (2 + 1 * 3) * Q], ug[i + (2 + 2 * 3) * Q]}
70     };
71     // Read q_data (dXdxdXdx_T symmetric matrix)
72     const CeedScalar dXdxdXdx_T[3][3] = {
73         {q_data[i + 1 * Q], q_data[i + 2 * Q], q_data[i + 3 * Q]},
74         {q_data[i + 2 * Q], q_data[i + 4 * Q], q_data[i + 5 * Q]},
75         {q_data[i + 3 * Q], q_data[i + 5 * Q], q_data[i + 6 * Q]}
76     };
77 
78     for (int k = 0; k < 3; k++) {    // k = component
79       for (int j = 0; j < 3; j++) {  // j = direction of vg
80         vg[i + (k + j * 3) * Q] = (uJ[k][0] * dXdxdXdx_T[0][j] + uJ[k][1] * dXdxdXdx_T[1][j] + uJ[k][2] * dXdxdXdx_T[2][j]);
81       }
82     }
83   }  // End of Quadrature Point Loop
84   return 0;
85 }
86 // -----------------------------------------------------------------------------
87