xref: /libCEED/examples/petsc/qfunctions/bps/bp1sphere.h (revision 356036fa84f714fa73ef64c9a80ce2028dde816f)
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 bp1sphere_h
12 #define bp1sphere_h
13 
14 #include <ceed.h>
15 #include <math.h>
16 
17 // -----------------------------------------------------------------------------
18 // This QFunction sets up the geometric factors required for integration and coordinate transformations when reference coordinates have a different
19 // dimension than the one of physical coordinates
20 //
21 // Reference (parent) 2D coordinates: X \in [-1, 1]^2
22 //
23 // Global 3D physical coordinates given by the mesh: xx \in [-R, R]^3 with R radius of the sphere
24 //
25 // Local 3D physical coordinates on the 2D manifold: x \in [-l, l]^3 with l half edge of the cube inscribed in the sphere
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 (on the 2D manifold) relative to xx (phyisical 3D):
32 //   dx_i/dxx_j (indicial notation) [3 * 3]
33 //
34 // Change of coordinates x (on the 2D manifold) relative to X (reference 2D):
35 //   (by chain rule)
36 //   dx_i/dX_j [3 * 2] = dx_i/dxx_k [3 * 3] * dxx_k/dX_j [3 * 2]
37 //
38 // mod_J is given by the magnitude of the cross product of the columns of dx_i/dX_j
39 //
40 // The quadrature data is stored in the array q_data.
41 //
42 // We require the determinant of the Jacobian to properly compute integrals of the form: int( u v )
43 //
44 // Qdata: mod_J * w
45 // -----------------------------------------------------------------------------
46 CEED_QFUNCTION(SetupMassGeo)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
47   // Inputs
48   const CeedScalar *X = in[0], *J = in[1], *w = in[2];
49   // Outputs
50   CeedScalar *q_data = out[0];
51 
52   // Quadrature Point Loop
53   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
54     // Read global Cartesian coordinates
55     const CeedScalar xx[3] = {X[i + 0 * Q], X[i + 1 * Q], X[i + 2 * Q]};
56 
57     // Read dxxdX Jacobian entries, stored as
58     // 0 3
59     // 1 4
60     // 2 5
61     const CeedScalar dxxdX[3][2] = {
62         {J[i + Q * 0], J[i + Q * 3]},
63         {J[i + Q * 1], J[i + Q * 4]},
64         {J[i + Q * 2], J[i + Q * 5]}
65     };
66 
67     // Setup
68     // x = xx (xx^T xx)^{-1/2}
69     // dx/dxx = I (xx^T xx)^{-1/2} - xx xx^T (xx^T xx)^{-3/2}
70     const CeedScalar mod_xx_sq = xx[0] * xx[0] + xx[1] * xx[1] + xx[2] * xx[2];
71     CeedScalar       xx_sq[3][3];
72     for (int j = 0; j < 3; j++) {
73       for (int k = 0; k < 3; k++) xx_sq[j][k] = xx[j] * xx[k] / (sqrt(mod_xx_sq) * mod_xx_sq);
74     }
75 
76     const CeedScalar dxdxx[3][3] = {
77         {1. / sqrt(mod_xx_sq) - xx_sq[0][0], -xx_sq[0][1],                       -xx_sq[0][2]                      },
78         {-xx_sq[1][0],                       1. / sqrt(mod_xx_sq) - xx_sq[1][1], -xx_sq[1][2]                      },
79         {-xx_sq[2][0],                       -xx_sq[2][1],                       1. / sqrt(mod_xx_sq) - xx_sq[2][2]}
80     };
81 
82     CeedScalar dxdX[3][2];
83     for (int j = 0; j < 3; j++) {
84       for (int k = 0; k < 2; k++) {
85         dxdX[j][k] = 0;
86         for (int l = 0; l < 3; l++) dxdX[j][k] += dxdxx[j][l] * dxxdX[l][k];
87       }
88     }
89 
90     // J is given by the cross product of the columns of dxdX
91     const CeedScalar J[3] = {dxdX[1][0] * dxdX[2][1] - dxdX[2][0] * dxdX[1][1], dxdX[2][0] * dxdX[0][1] - dxdX[0][0] * dxdX[2][1],
92                              dxdX[0][0] * dxdX[1][1] - dxdX[1][0] * dxdX[0][1]};
93 
94     // Use the magnitude of J as our detJ (volume scaling factor)
95     const CeedScalar mod_J = sqrt(J[0] * J[0] + J[1] * J[1] + J[2] * J[2]);
96 
97     // Interp-to-Interp q_data
98     q_data[i + Q * 0] = mod_J * w[i];
99   }  // End of Quadrature Point Loop
100 
101   return 0;
102 }
103 
104 // -----------------------------------------------------------------------------
105 // This QFunction sets up the rhs and true solution for the problem
106 // -----------------------------------------------------------------------------
107 CEED_QFUNCTION(SetupMassRhs)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
108   // Inputs
109   const CeedScalar *X = in[0], *q_data = in[1];
110   // Outputs
111   CeedScalar *true_soln = out[0], *rhs = out[1];
112 
113   // Context
114   const CeedScalar *context = (const CeedScalar *)ctx;
115   const CeedScalar  R       = context[0];
116 
117   // Quadrature Point Loop
118   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
119     // Compute latitude
120     const CeedScalar theta = asin(X[i + 2 * Q] / R);
121 
122     // Use absolute value of latitude for true solution
123     true_soln[i] = fabs(theta);
124 
125     rhs[i] = q_data[i] * true_soln[i];
126   }  // End of Quadrature Point Loop
127 
128   return 0;
129 }
130 
131 // -----------------------------------------------------------------------------
132 // This QFunction applies the mass operator for a scalar field.
133 //
134 // Inputs:
135 //   u      - Input vector at quadrature points
136 //   q_data - Geometric factors
137 //
138 // Output:
139 //   v     - Output vector (test functions) at quadrature points
140 // -----------------------------------------------------------------------------
141 CEED_QFUNCTION(Mass)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
142   // Inputs
143   const CeedScalar *u = in[0], *q_data = in[1];
144   // Outputs
145   CeedScalar *v = out[0];
146 
147   // Quadrature Point Loop
148   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) v[i] = q_data[i] * u[i];
149 
150   return 0;
151 }
152 // -----------------------------------------------------------------------------
153 
154 #endif  // bp1sphere_h
155