xref: /libCEED/examples/petsc/qfunctions/area/areasphere.h (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
332d2ee49SValeria Barra //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
532d2ee49SValeria Barra //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
732d2ee49SValeria Barra 
832d2ee49SValeria Barra /// @file
932d2ee49SValeria Barra /// libCEED QFunctions for mass operator example for a scalar field on the sphere using PETSc
1032d2ee49SValeria Barra 
11f6b55d2cSvaleriabarra #ifndef areasphere_h
12f6b55d2cSvaleriabarra #define areasphere_h
13f6b55d2cSvaleriabarra 
14c9c2c079SJeremy L Thompson #include <ceed.h>
1532d2ee49SValeria Barra #include <math.h>
1632d2ee49SValeria Barra 
17e83e87a5Sjeremylt // -----------------------------------------------------------------------------
1832d2ee49SValeria Barra // This QFunction sets up the geometric factor required for integration when
1932d2ee49SValeria Barra //   reference coordinates have a different dimension than the one of
20ed264d09SValeria Barra //   physical coordinates
2132d2ee49SValeria Barra //
2232d2ee49SValeria Barra // Reference (parent) 2D coordinates: X \in [-1, 1]^2
2332d2ee49SValeria Barra //
2432d2ee49SValeria Barra // Global 3D physical coordinates given by the mesh: xx \in [-R, R]^3
2532d2ee49SValeria Barra //   with R radius of the sphere
2632d2ee49SValeria Barra //
2732d2ee49SValeria Barra // Local 3D physical coordinates on the 2D manifold: x \in [-l, l]^3
2832d2ee49SValeria Barra //   with l half edge of the cube inscribed in the sphere
2932d2ee49SValeria Barra //
3032d2ee49SValeria Barra // Change of coordinates matrix computed by the library:
31ed264d09SValeria Barra //   (physical 3D coords relative to reference 2D coords)
3232d2ee49SValeria Barra //   dxx_j/dX_i (indicial notation) [3 * 2]
3332d2ee49SValeria Barra //
3432d2ee49SValeria Barra // Change of coordinates x (on the 2D manifold) relative to xx (phyisical 3D):
3532d2ee49SValeria Barra //   dx_i/dxx_j (indicial notation) [3 * 3]
3632d2ee49SValeria Barra //
3732d2ee49SValeria Barra // Change of coordinates x (on the 2D manifold) relative to X (reference 2D):
3832d2ee49SValeria Barra //   (by chain rule)
3932d2ee49SValeria Barra //   dx_i/dX_j = dx_i/dxx_k * dxx_k/dX_j [3 * 2]
4032d2ee49SValeria Barra //
419b072555Sjeremylt // mod_J is given by the magnitude of the cross product of the columns of dx_i/dX_j
4232d2ee49SValeria Barra //
439b072555Sjeremylt // The quadrature data is stored in the array q_data.
4432d2ee49SValeria Barra //
4532d2ee49SValeria Barra // We require the determinant of the Jacobian to properly compute integrals of
4632d2ee49SValeria Barra //   the form: int( u v )
4732d2ee49SValeria Barra //
489b072555Sjeremylt // Qdata: mod_J * w
4932d2ee49SValeria Barra //
5032d2ee49SValeria Barra // -----------------------------------------------------------------------------
51*2b730f8bSJeremy L Thompson CEED_QFUNCTION(SetupMassGeoSphere)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5232d2ee49SValeria Barra   // Inputs
5332d2ee49SValeria Barra   const CeedScalar *X = in[0], *J = in[1], *w = in[2];
5432d2ee49SValeria Barra   // Outputs
559b072555Sjeremylt   CeedScalar *q_data = out[0];
5632d2ee49SValeria Barra 
5732d2ee49SValeria Barra   // Quadrature Point Loop
58*2b730f8bSJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
5932d2ee49SValeria Barra     // Read global Cartesian coordinates
60*2b730f8bSJeremy L Thompson     const CeedScalar xx[3][1] = {{X[i + 0 * Q]}, {X[i + 1 * Q]}, {X[i + 2 * Q]}};
6132d2ee49SValeria Barra 
6232d2ee49SValeria Barra     // Read dxxdX Jacobian entries, stored as
6332d2ee49SValeria Barra     // 0 3
6432d2ee49SValeria Barra     // 1 4
6532d2ee49SValeria Barra     // 2 5
66*2b730f8bSJeremy L Thompson     const CeedScalar dxxdX[3][2] = {
67*2b730f8bSJeremy L Thompson         {J[i + Q * 0], J[i + Q * 3]},
68*2b730f8bSJeremy L Thompson         {J[i + Q * 1], J[i + Q * 4]},
69*2b730f8bSJeremy L Thompson         {J[i + Q * 2], J[i + Q * 5]}
7032d2ee49SValeria Barra     };
7132d2ee49SValeria Barra 
7232d2ee49SValeria Barra     // Setup
739b072555Sjeremylt     const CeedScalar mod_xx_sq = xx[0][0] * xx[0][0] + xx[1][0] * xx[1][0] + xx[2][0] * xx[2][0];
749b072555Sjeremylt     CeedScalar       xx_sq[3][3];
75*2b730f8bSJeremy L Thompson     for (int j = 0; j < 3; j++) {
7632d2ee49SValeria Barra       for (int k = 0; k < 3; k++) {
779b072555Sjeremylt         xx_sq[j][k] = 0;
78*2b730f8bSJeremy L Thompson         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*2b730f8bSJeremy L Thompson       }
8032d2ee49SValeria Barra     }
8132d2ee49SValeria Barra 
82*2b730f8bSJeremy L Thompson     const CeedScalar dxdxx[3][3] = {
83*2b730f8bSJeremy L Thompson         {1. / sqrt(mod_xx_sq) - xx_sq[0][0], -xx_sq[0][1],                       -xx_sq[0][2]                      },
84*2b730f8bSJeremy L Thompson         {-xx_sq[1][0],                       1. / sqrt(mod_xx_sq) - xx_sq[1][1], -xx_sq[1][2]                      },
85*2b730f8bSJeremy L Thompson         {-xx_sq[2][0],                       -xx_sq[2][1],                       1. / sqrt(mod_xx_sq) - xx_sq[2][2]}
8632d2ee49SValeria Barra     };
8732d2ee49SValeria Barra 
8832d2ee49SValeria Barra     CeedScalar dxdX[3][2];
89*2b730f8bSJeremy L Thompson     for (int j = 0; j < 3; j++) {
9032d2ee49SValeria Barra       for (int k = 0; k < 2; k++) {
9132d2ee49SValeria Barra         dxdX[j][k] = 0;
92*2b730f8bSJeremy L Thompson         for (int l = 0; l < 3; l++) dxdX[j][k] += dxdxx[j][l] * dxxdX[l][k];
93*2b730f8bSJeremy L Thompson       }
9432d2ee49SValeria Barra     }
9532d2ee49SValeria Barra 
9632d2ee49SValeria Barra     // J is given by the cross product of the columns of dxdX
9732d2ee49SValeria Barra     const CeedScalar J[3][1] = {{dxdX[1][0] * dxdX[2][1] - dxdX[2][0] * dxdX[1][1]},
9832d2ee49SValeria Barra                                 {dxdX[2][0] * dxdX[0][1] - dxdX[0][0] * dxdX[2][1]},
99*2b730f8bSJeremy L Thompson                                 {dxdX[0][0] * dxdX[1][1] - dxdX[1][0] * dxdX[0][1]}};
10032d2ee49SValeria Barra     // Use the magnitude of J as our detJ (volume scaling factor)
1019b072555Sjeremylt     const CeedScalar mod_J = sqrt(J[0][0] * J[0][0] + J[1][0] * J[1][0] + J[2][0] * J[2][0]);
1029b072555Sjeremylt     q_data[i + Q * 0]      = mod_J * w[i];
10332d2ee49SValeria Barra   }  // End of Quadrature Point Loop
10432d2ee49SValeria Barra   return 0;
10532d2ee49SValeria Barra }
10632d2ee49SValeria Barra // -----------------------------------------------------------------------------
107f6b55d2cSvaleriabarra 
108f6b55d2cSvaleriabarra #endif  // areasphere_h
109