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