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