xref: /libCEED/examples/petsc/qfunctions/bps/bp1.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 mass 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 geometric factors required to apply the mass operator
18 //
19 // The quadrature data is stored in the array q_data.
20 //
21 // We require the determinant of the Jacobian to properly compute integrals of the form: int( u v )
22 //
23 // Qdata: det_J * w
24 //
25 // -----------------------------------------------------------------------------
26 CEED_QFUNCTION(SetupMassGeo)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
27   // Inputs
28   const CeedScalar(*J)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[1];
29   const CeedScalar(*w)                = in[2];  // Note: *X = in[0]
30   // Outputs
31   CeedScalar *q_data = out[0];
32 
33   const CeedInt dim = 3;
34   // Quadrature Point Loop
35   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
36     // Setup
37     CeedScalar A[3][3];
38     for (CeedInt j = 0; j < dim; j++) {
39       for (CeedInt k = 0; k < dim; k++) {
40         // Equivalent code with no mod operations:
41         // A[k][j] = J[k+1][j+1]*J[k+2][j+2] - J[k+1][j+2]*J[k+2][j+1]
42         A[k][j] = J[(k + 1) % dim][(j + 1) % dim][i] * J[(k + 2) % dim][(j + 2) % dim][i] -
43                   J[(k + 1) % dim][(j + 2) % dim][i] * J[(k + 2) % dim][(j + 1) % dim][i];
44       }
45     }
46     const CeedScalar detJ = J[0][0][i] * A[0][0] + J[0][1][i] * A[0][1] + J[0][2][i] * A[0][2];
47     q_data[i]             = detJ * w[i];
48   }  // End of Quadrature Point Loop
49   return 0;
50 }
51 
52 // -----------------------------------------------------------------------------
53 // This QFunction sets up the rhs and true solution for the problem
54 // -----------------------------------------------------------------------------
55 CEED_QFUNCTION(SetupMassRhs)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
56   const CeedScalar *x = in[0], *w = in[1];
57   CeedScalar       *true_soln = out[0], *rhs = out[1];
58 
59   // Quadrature Point Loop
60   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
61     true_soln[i] = sqrt(x[i] * x[i] + x[i + Q] * x[i + Q] + x[i + 2 * Q] * x[i + 2 * Q]);
62     rhs[i]       = w[i] * true_soln[i];
63   }  // End of Quadrature Point Loop
64   return 0;
65 }
66 
67 // -----------------------------------------------------------------------------
68 // This QFunction applies the mass operator for a scalar field.
69 //
70 // Inputs:
71 //   u      - Input vector at quadrature points
72 //   q_data - Geometric factors
73 //
74 // Output:
75 //   v     - Output vector (test functions) at quadrature points
76 // -----------------------------------------------------------------------------
77 CEED_QFUNCTION(Mass)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
78   const CeedScalar *u = in[0], *q_data = in[1];
79   CeedScalar       *v = out[0];
80 
81   // Quadrature Point Loop
82   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) v[i] = q_data[i] * u[i];
83 
84   return 0;
85 }
86 // -----------------------------------------------------------------------------
87