xref: /libCEED/examples/petsc/qfunctions/bps/bp13.h (revision ed094490f53e580908aa80e9fe815a6fd76d7526)
1 // Copyright (c) 2017-2025, 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(SetupMassDiffRhs)(void *ctx, const 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     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]));
32 
33     rhs[i] = w[i + Q * 0] * (M_PI * M_PI * (k[0] * k[0] + k[1] * k[1] + k[2] * k[2]) + 1.0) * true_soln[i];
34   }  // End of Quadrature Point Loop
35   return 0;
36 }
37 
38 // -----------------------------------------------------------------------------
39 // This QFunction applies the mass + diffusion operator for a scalar field.
40 //
41 // Inputs:
42 //   u       - Input vector at quadrature points
43 //   ug      - Input vector gradient at quadrature points
44 //   q_data  - Geometric factors
45 //
46 // Output:
47 //   v      - Output vector (test functions) at quadrature points
48 //   vg     - Output vector (test functions) gradient at quadrature points
49 // -----------------------------------------------------------------------------
50 CEED_QFUNCTION(MassDiff)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
51   const CeedScalar *u = in[0], *ug = in[1], *q_data = in[2];
52   CeedScalar       *v = out[0], *vg = out[1];
53 
54   // Quadrature Point Loop
55   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
56     // Read spatial derivatives of u
57     const CeedScalar du[3] = {ug[i + Q * 0], ug[i + Q * 1], ug[i + Q * 2]};
58     // Read q_data (dXdxdXdx_T symmetric matrix)
59     const CeedScalar dXdxdXdx_T[3][3] = {
60         {q_data[i + 1 * Q], q_data[i + 2 * Q], q_data[i + 3 * Q]},
61         {q_data[i + 2 * Q], q_data[i + 4 * Q], q_data[i + 5 * Q]},
62         {q_data[i + 3 * Q], q_data[i + 5 * Q], q_data[i + 6 * Q]}
63     };
64 
65     // Mass
66     v[i] = q_data[i + 0 * Q] * u[i];
67     // Diff
68     for (int j = 0; j < 3; j++) {  // j = direction of vg
69       vg[i + j * Q] = (du[0] * dXdxdXdx_T[0][j] + du[1] * dXdxdXdx_T[1][j] + du[2] * dXdxdXdx_T[2][j]);
70     }
71   }  // End of Quadrature Point Loop
72   return 0;
73 }
74 // -----------------------------------------------------------------------------
75