xref: /libCEED/examples/petsc/qfunctions/bps/bp1sphere.h (revision 23d6ba15ce2709c4ef8d39cdb3938232a70f8a28)
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
19 //   coordinate transformations when reference coordinates have a different
20 //   dimension than the one of 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 [3 * 2] = dx_i/dxx_k [3 * 3] * 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(SetupMassGeo)(void *ctx, const CeedInt Q,
52                              const CeedScalar *const *in,
53                              CeedScalar *const *out) {
54   // Inputs
55   const CeedScalar *X = in[0], *J = in[1], *w = in[2];
56   // Outputs
57   CeedScalar *q_data = out[0];
58 
59   // Quadrature Point Loop
60   CeedPragmaSIMD
61   for (CeedInt i=0; i<Q; i++) {
62     // Read global Cartesian coordinates
63     const CeedScalar xx[3] = {X[i+0*Q],
64                               X[i+1*Q],
65                               X[i+2*Q]
66                              };
67 
68     // Read dxxdX Jacobian entries, stored as
69     // 0 3
70     // 1 4
71     // 2 5
72     const CeedScalar dxxdX[3][2] = {{J[i+Q*0],
73                                      J[i+Q*3]},
74                                     {J[i+Q*1],
75                                      J[i+Q*4]},
76                                     {J[i+Q*2],
77                                      J[i+Q*5]}
78                                    };
79 
80     // Setup
81     // x = xx (xx^T xx)^{-1/2}
82     // dx/dxx = I (xx^T xx)^{-1/2} - xx xx^T (xx^T xx)^{-3/2}
83     const CeedScalar mod_xx_sq = xx[0]*xx[0]+xx[1]*xx[1]+xx[2]*xx[2];
84     CeedScalar xx_sq[3][3];
85     for (int j=0; j<3; j++)
86       for (int k=0; k<3; k++)
87         xx_sq[j][k] = xx[j]*xx[k] / (sqrt(mod_xx_sq) * mod_xx_sq);
88 
89     const CeedScalar dxdxx[3][3] = {{1./sqrt(mod_xx_sq) - xx_sq[0][0],
90                                      -xx_sq[0][1],
91                                      -xx_sq[0][2]},
92                                     {-xx_sq[1][0],
93                                      1./sqrt(mod_xx_sq) - xx_sq[1][1],
94                                      -xx_sq[1][2]},
95                                     {-xx_sq[2][0],
96                                      -xx_sq[2][1],
97                                      1./sqrt(mod_xx_sq) - xx_sq[2][2]}
98                                    };
99 
100     CeedScalar dxdX[3][2];
101     for (int j=0; j<3; j++)
102       for (int k=0; k<2; k++) {
103         dxdX[j][k] = 0;
104         for (int l=0; l<3; l++)
105           dxdX[j][k] += dxdxx[j][l]*dxxdX[l][k];
106       }
107 
108     // J is given by the cross product of the columns of dxdX
109     const CeedScalar J[3] = {dxdX[1][0]*dxdX[2][1] - dxdX[2][0]*dxdX[1][1],
110                              dxdX[2][0]*dxdX[0][1] - dxdX[0][0]*dxdX[2][1],
111                              dxdX[0][0]*dxdX[1][1] - dxdX[1][0]*dxdX[0][1]
112                             };
113 
114     // Use the magnitude of J as our detJ (volume scaling factor)
115     const CeedScalar mod_J = sqrt(J[0]*J[0]+J[1]*J[1]+J[2]*J[2]);
116 
117     // Interp-to-Interp q_data
118     q_data[i+Q*0] = mod_J * w[i];
119   } // End of Quadrature Point Loop
120 
121   return 0;
122 }
123 
124 // -----------------------------------------------------------------------------
125 // This QFunction sets up the rhs and true solution for the problem
126 // -----------------------------------------------------------------------------
127 CEED_QFUNCTION(SetupMassRhs)(void *ctx, const CeedInt Q,
128                              const CeedScalar *const *in,
129                              CeedScalar *const *out) {
130   // Inputs
131   const CeedScalar *X = in[0], *q_data = in[1];
132   // Outputs
133   CeedScalar *true_soln = out[0], *rhs = out[1];
134 
135   // Context
136   const CeedScalar *context = (const CeedScalar*)ctx;
137   const CeedScalar R        = context[0];
138 
139   // Quadrature Point Loop
140   CeedPragmaSIMD
141   for (CeedInt i=0; i<Q; i++) {
142     // Compute latitude
143     const CeedScalar theta =  asin(X[i+2*Q] / R);
144 
145     // Use absolute value of latitude for true solution
146     true_soln[i] = fabs(theta);
147 
148     rhs[i] = q_data[i] * true_soln[i];
149   } // End of Quadrature Point Loop
150 
151   return 0;
152 }
153 
154 // -----------------------------------------------------------------------------
155 // This QFunction applies the mass operator for a scalar field.
156 //
157 // Inputs:
158 //   u     - Input vector at quadrature points
159 //   q_data - Geometric factors
160 //
161 // Output:
162 //   v     - Output vector (test functions) at quadrature points
163 //
164 // -----------------------------------------------------------------------------
165 CEED_QFUNCTION(Mass)(void *ctx, const CeedInt Q,
166                      const CeedScalar *const *in, CeedScalar *const *out) {
167   // Inputs
168   const CeedScalar *u = in[0], *q_data = in[1];
169   // Outputs
170   CeedScalar *v = out[0];
171 
172   // Quadrature Point Loop
173   CeedPragmaSIMD
174   for (CeedInt i=0; i<Q; i++)
175     v[i] = q_data[i] * u[i];
176 
177   return 0;
178 }
179 // -----------------------------------------------------------------------------
180 
181 #endif // bp1sphere_h
182