xref: /libCEED/examples/petsc/qfunctions/area/areacube.h (revision 5dfaedb85d2aa5da89951bb5d8f41d61be09bbf6)
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 for a scalar field on the sphere using PETSc
10 
11 #ifndef areacube_h
12 #define areacube_h
13 
14 #include <ceed.h>
15 #include <math.h>
16 
17 // -----------------------------------------------------------------------------
18 // This QFunction sets up the geometric factor required for integration when
19 //   reference coordinates have a different dimension than the one of
20 //   physical coordinates
21 //
22 // Reference (parent) 2D coordinates: X \in [-1, 1]^2
23 //
24 // Global physical coordinates given by the mesh (3D): xx \in [-l, l]^3
25 //
26 // Local physical coordinates on the manifold (2D): x \in [-l, l]^2
27 //
28 // Change of coordinates matrix computed by the library:
29 //   (physical 3D coords relative to reference 2D coords)
30 //   dxx_j/dX_i (indicial notation) [3 * 2]
31 //
32 // Change of coordinates x (physical 2D) relative to xx (phyisical 3D):
33 //   dx_i/dxx_j (indicial notation) [2 * 3]
34 //
35 // Change of coordinates x (physical 2D) relative to X (reference 2D):
36 //   (by chain rule)
37 //   dx_i/dX_j = dx_i/dxx_k * dxx_k/dX_j
38 //
39 // The quadrature data is stored in the array q_data.
40 //
41 // We require the determinant of the Jacobian to properly compute integrals of
42 //   the form: int( u v )
43 //
44 // Qdata: w * det(dx_i/dX_j)
45 //
46 // -----------------------------------------------------------------------------
47 CEED_QFUNCTION(SetupMassGeoCube)(void *ctx, const CeedInt Q,
48                              const CeedScalar *const *in,
49                              CeedScalar *const *out) {
50   // Inputs
51   const CeedScalar *J = in[1], *w = in[2];
52   // Outputs
53   CeedScalar *q_data = out[0];
54 
55   // Quadrature Point Loop
56   CeedPragmaSIMD
57   for (CeedInt i=0; i<Q; i++) {
58     // Read dxxdX Jacobian entries, stored as
59     // 0 3
60     // 1 4
61     // 2 5
62     const CeedScalar dxxdX[3][2] = {{J[i+Q*0],
63                                      J[i+Q*3]},
64                                     {J[i+Q*1],
65                                      J[i+Q*4]},
66                                     {J[i+Q*2],
67                                      J[i+Q*5]}
68                                    };
69 
70     // Modulus of dxxdX column vectors
71     const CeedScalar mod_g_1 = sqrt(dxxdX[0][0]*dxxdX[0][0] +
72                                     dxxdX[1][0]*dxxdX[1][0] +
73                                     dxxdX[2][0]*dxxdX[2][0]);
74     const CeedScalar mod_g_2 = sqrt(dxxdX[0][1]*dxxdX[0][1] +
75                                     dxxdX[1][1]*dxxdX[1][1] +
76                                     dxxdX[2][1]*dxxdX[2][1]);
77 
78     // Use normalized column vectors of dxxdX as rows of dxdxx
79     const CeedScalar dxdxx[2][3] = {{dxxdX[0][0] / mod_g_1,
80                                      dxxdX[1][0] / mod_g_1,
81                                      dxxdX[2][0] / mod_g_1},
82                                     {dxxdX[0][1] / mod_g_2,
83                                      dxxdX[1][1] / mod_g_2,
84                                      dxxdX[2][1] / mod_g_2}
85                                    };
86 
87     CeedScalar dxdX[2][2];
88     for (int j=0; j<2; j++)
89       for (int k=0; k<2; k++) {
90         dxdX[j][k] = 0;
91         for (int l=0; l<3; l++)
92           dxdX[j][k] += dxdxx[j][l]*dxxdX[l][k];
93       }
94 
95     q_data[i+Q*0] = (dxdX[0][0]*dxdX[1][1] - dxdX[1][0]*dxdX[0][1]) * w[i];
96 
97   } // End of Quadrature Point Loop
98   return 0;
99 }
100 
101 // -----------------------------------------------------------------------------
102 // This QFunction applies the mass operator for a scalar field.
103 //
104 // Inputs:
105 //   u     - Input vector at quadrature points
106 //   q_data - Geometric factors
107 //
108 // Output:
109 //   v     - Output vector (test function) at quadrature points
110 //
111 // -----------------------------------------------------------------------------
112 CEED_QFUNCTION(Mass)(void *ctx, const CeedInt Q,
113                      const CeedScalar *const *in, CeedScalar *const *out) {
114   // Inputs
115   const CeedScalar *u = in[0], *q_data = in[1];
116   // Outputs
117   CeedScalar *v = out[0];
118 
119   // Quadrature Point Loop
120   CeedPragmaSIMD
121   for (CeedInt i=0; i<Q; i++)
122     v[i] = q_data[i] * u[i];
123 
124   return 0;
125 }
126 // -----------------------------------------------------------------------------
127 
128 #endif // areacube_h
129