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