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