xref: /libCEED/examples/petsc/qfunctions/area/areasphere.h (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
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 areasphere_h
12 #define areasphere_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 3D physical coordinates given by the mesh: xx \in [-R, R]^3
25 //   with R radius of the sphere
26 //
27 // Local 3D physical coordinates on the 2D manifold: x \in [-l, l]^3
28 //   with l half edge of the cube inscribed in the sphere
29 //
30 // Change of coordinates matrix computed by the library:
31 //   (physical 3D coords relative to reference 2D coords)
32 //   dxx_j/dX_i (indicial notation) [3 * 2]
33 //
34 // Change of coordinates x (on the 2D manifold) relative to xx (phyisical 3D):
35 //   dx_i/dxx_j (indicial notation) [3 * 3]
36 //
37 // Change of coordinates x (on the 2D manifold) relative to X (reference 2D):
38 //   (by chain rule)
39 //   dx_i/dX_j = dx_i/dxx_k * dxx_k/dX_j [3 * 2]
40 //
41 // mod_J is given by the magnitude of the cross product of the columns of dx_i/dX_j
42 //
43 // The quadrature data is stored in the array q_data.
44 //
45 // We require the determinant of the Jacobian to properly compute integrals of
46 //   the form: int( u v )
47 //
48 // Qdata: mod_J * w
49 //
50 // -----------------------------------------------------------------------------
51 CEED_QFUNCTION(SetupMassGeoSphere)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
52   // Inputs
53   const CeedScalar *X = in[0], *J = in[1], *w = in[2];
54   // Outputs
55   CeedScalar *q_data = out[0];
56 
57   // Quadrature Point Loop
58   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
59     // Read global Cartesian coordinates
60     const CeedScalar xx[3][1] = {{X[i + 0 * Q]}, {X[i + 1 * Q]}, {X[i + 2 * Q]}};
61 
62     // Read dxxdX Jacobian entries, stored as
63     // 0 3
64     // 1 4
65     // 2 5
66     const CeedScalar dxxdX[3][2] = {
67         {J[i + Q * 0], J[i + Q * 3]},
68         {J[i + Q * 1], J[i + Q * 4]},
69         {J[i + Q * 2], J[i + Q * 5]}
70     };
71 
72     // Setup
73     const CeedScalar mod_xx_sq = xx[0][0] * xx[0][0] + xx[1][0] * xx[1][0] + xx[2][0] * xx[2][0];
74     CeedScalar       xx_sq[3][3];
75     for (int j = 0; j < 3; j++) {
76       for (int k = 0; k < 3; k++) {
77         xx_sq[j][k] = 0;
78         for (int l = 0; l < 1; l++) xx_sq[j][k] += xx[j][l] * xx[k][l] / (sqrt(mod_xx_sq) * mod_xx_sq);
79       }
80     }
81 
82     const CeedScalar dxdxx[3][3] = {
83         {1. / sqrt(mod_xx_sq) - xx_sq[0][0], -xx_sq[0][1],                       -xx_sq[0][2]                      },
84         {-xx_sq[1][0],                       1. / sqrt(mod_xx_sq) - xx_sq[1][1], -xx_sq[1][2]                      },
85         {-xx_sq[2][0],                       -xx_sq[2][1],                       1. / sqrt(mod_xx_sq) - xx_sq[2][2]}
86     };
87 
88     CeedScalar dxdX[3][2];
89     for (int j = 0; j < 3; j++) {
90       for (int k = 0; k < 2; k++) {
91         dxdX[j][k] = 0;
92         for (int l = 0; l < 3; l++) dxdX[j][k] += dxdxx[j][l] * dxxdX[l][k];
93       }
94     }
95 
96     // J is given by the cross product of the columns of dxdX
97     const CeedScalar J[3][1] = {{dxdX[1][0] * dxdX[2][1] - dxdX[2][0] * dxdX[1][1]},
98                                 {dxdX[2][0] * dxdX[0][1] - dxdX[0][0] * dxdX[2][1]},
99                                 {dxdX[0][0] * dxdX[1][1] - dxdX[1][0] * dxdX[0][1]}};
100     // Use the magnitude of J as our detJ (volume scaling factor)
101     const CeedScalar mod_J = sqrt(J[0][0] * J[0][0] + J[1][0] * J[1][0] + J[2][0] * J[2][0]);
102     q_data[i + Q * 0]      = mod_J * w[i];
103   }  // End of Quadrature Point Loop
104   return 0;
105 }
106 // -----------------------------------------------------------------------------
107 
108 #endif  // areasphere_h
109