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