xref: /libCEED/examples/petsc/qfunctions/area/areasphere.h (revision d4cc18453651bd0f94c1a2e078b2646a92dafdcc)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, 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 
11c0b5abf0SJeremy L Thompson #include <ceed/types.h>
12c0b5abf0SJeremy L Thompson #ifndef CEED_RUNNING_JIT_PASS
1332d2ee49SValeria Barra #include <math.h>
14c0b5abf0SJeremy L Thompson #endif
1532d2ee49SValeria Barra 
16e83e87a5Sjeremylt // -----------------------------------------------------------------------------
17ea61e9acSJeremy L Thompson // This QFunction sets up the geometric factor required for integration when reference coordinates have a different dimension than the one of physical
18ea61e9acSJeremy L Thompson // coordinates
1932d2ee49SValeria Barra //
2032d2ee49SValeria Barra // Reference (parent) 2D coordinates: X \in [-1, 1]^2
2132d2ee49SValeria Barra //
22ea61e9acSJeremy L Thompson // Global 3D physical coordinates given by the mesh: xx \in [-R, R]^3 with R radius of the sphere
2332d2ee49SValeria Barra //
24ea61e9acSJeremy L Thompson // Local 3D physical coordinates on the 2D manifold: x \in [-l, l]^3 with l half edge of the cube inscribed in the sphere
2532d2ee49SValeria Barra //
2632d2ee49SValeria Barra // Change of coordinates matrix computed by the library:
27ed264d09SValeria Barra //   (physical 3D coords relative to reference 2D coords)
2832d2ee49SValeria Barra //   dxx_j/dX_i (indicial notation) [3 * 2]
2932d2ee49SValeria Barra //
3032d2ee49SValeria Barra // Change of coordinates x (on the 2D manifold) relative to xx (phyisical 3D):
3132d2ee49SValeria Barra //   dx_i/dxx_j (indicial notation) [3 * 3]
3232d2ee49SValeria Barra //
3332d2ee49SValeria Barra // Change of coordinates x (on the 2D manifold) relative to X (reference 2D):
3432d2ee49SValeria Barra //   (by chain rule)
3532d2ee49SValeria Barra //   dx_i/dX_j = dx_i/dxx_k * dxx_k/dX_j [3 * 2]
3632d2ee49SValeria Barra //
379b072555Sjeremylt // mod_J is given by the magnitude of the cross product of the columns of dx_i/dX_j
3832d2ee49SValeria Barra //
399b072555Sjeremylt // The quadrature data is stored in the array q_data.
4032d2ee49SValeria Barra //
41ea61e9acSJeremy L Thompson // We require the determinant of the Jacobian to properly compute integrals of the form: int( u v )
4232d2ee49SValeria Barra //
439b072555Sjeremylt // Qdata: mod_J * w
4432d2ee49SValeria Barra // -----------------------------------------------------------------------------
SetupMassGeoSphere(void * ctx,const CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)452b730f8bSJeremy L Thompson CEED_QFUNCTION(SetupMassGeoSphere)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4632d2ee49SValeria Barra   // Inputs
4732d2ee49SValeria Barra   const CeedScalar *X = in[0], *J = in[1], *w = in[2];
4832d2ee49SValeria Barra   // Outputs
499b072555Sjeremylt   CeedScalar *q_data = out[0];
5032d2ee49SValeria Barra 
5132d2ee49SValeria Barra   // Quadrature Point Loop
522b730f8bSJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
5332d2ee49SValeria Barra     // Read global Cartesian coordinates
542b730f8bSJeremy L Thompson     const CeedScalar xx[3][1] = {{X[i + 0 * Q]}, {X[i + 1 * Q]}, {X[i + 2 * Q]}};
5532d2ee49SValeria Barra 
5632d2ee49SValeria Barra     // Read dxxdX Jacobian entries, stored as
5732d2ee49SValeria Barra     // 0 3
5832d2ee49SValeria Barra     // 1 4
5932d2ee49SValeria Barra     // 2 5
602b730f8bSJeremy L Thompson     const CeedScalar dxxdX[3][2] = {
612b730f8bSJeremy L Thompson         {J[i + Q * 0], J[i + Q * 3]},
622b730f8bSJeremy L Thompson         {J[i + Q * 1], J[i + Q * 4]},
632b730f8bSJeremy L Thompson         {J[i + Q * 2], J[i + Q * 5]}
6432d2ee49SValeria Barra     };
6532d2ee49SValeria Barra 
6632d2ee49SValeria Barra     // Setup
679b072555Sjeremylt     const CeedScalar mod_xx_sq = xx[0][0] * xx[0][0] + xx[1][0] * xx[1][0] + xx[2][0] * xx[2][0];
689b072555Sjeremylt     CeedScalar       xx_sq[3][3];
692b730f8bSJeremy L Thompson     for (int j = 0; j < 3; j++) {
7032d2ee49SValeria Barra       for (int k = 0; k < 3; k++) {
719b072555Sjeremylt         xx_sq[j][k] = 0;
722b730f8bSJeremy 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);
732b730f8bSJeremy L Thompson       }
7432d2ee49SValeria Barra     }
7532d2ee49SValeria Barra 
762b730f8bSJeremy L Thompson     const CeedScalar dxdxx[3][3] = {
772b730f8bSJeremy L Thompson         {1. / sqrt(mod_xx_sq) - xx_sq[0][0], -xx_sq[0][1],                       -xx_sq[0][2]                      },
782b730f8bSJeremy L Thompson         {-xx_sq[1][0],                       1. / sqrt(mod_xx_sq) - xx_sq[1][1], -xx_sq[1][2]                      },
792b730f8bSJeremy L Thompson         {-xx_sq[2][0],                       -xx_sq[2][1],                       1. / sqrt(mod_xx_sq) - xx_sq[2][2]}
8032d2ee49SValeria Barra     };
8132d2ee49SValeria Barra 
8232d2ee49SValeria Barra     CeedScalar dxdX[3][2];
832b730f8bSJeremy L Thompson     for (int j = 0; j < 3; j++) {
8432d2ee49SValeria Barra       for (int k = 0; k < 2; k++) {
8532d2ee49SValeria Barra         dxdX[j][k] = 0;
862b730f8bSJeremy L Thompson         for (int l = 0; l < 3; l++) dxdX[j][k] += dxdxx[j][l] * dxxdX[l][k];
872b730f8bSJeremy L Thompson       }
8832d2ee49SValeria Barra     }
8932d2ee49SValeria Barra 
9032d2ee49SValeria Barra     // J is given by the cross product of the columns of dxdX
9132d2ee49SValeria Barra     const CeedScalar J[3][1] = {{dxdX[1][0] * dxdX[2][1] - dxdX[2][0] * dxdX[1][1]},
9232d2ee49SValeria Barra                                 {dxdX[2][0] * dxdX[0][1] - dxdX[0][0] * dxdX[2][1]},
932b730f8bSJeremy L Thompson                                 {dxdX[0][0] * dxdX[1][1] - dxdX[1][0] * dxdX[0][1]}};
9432d2ee49SValeria Barra     // Use the magnitude of J as our detJ (volume scaling factor)
959b072555Sjeremylt     const CeedScalar mod_J = sqrt(J[0][0] * J[0][0] + J[1][0] * J[1][0] + J[2][0] * J[2][0]);
969b072555Sjeremylt     q_data[i + Q * 0]      = mod_J * w[i];
9732d2ee49SValeria Barra   }  // End of Quadrature Point Loop
9832d2ee49SValeria Barra   return 0;
9932d2ee49SValeria Barra }
10032d2ee49SValeria Barra // -----------------------------------------------------------------------------
101