xref: /libCEED/examples/petsc/qfunctions/bps/bp1sphere.h (revision bf4cb66493dbcc06b8d25c9c91cf89fe1cab7c9b)
1 // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3 // reserved. See files LICENSE and NOTICE for details.
4 //
5 // This file is part of CEED, a collection of benchmarks, miniapps, software
6 // libraries and APIs for efficient high-order finite element and spectral
7 // element discretizations for exascale applications. For more information and
8 // source code availability see http://github.com/ceed.
9 //
10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11 // a collaborative effort of two U.S. Department of Energy organizations (Office
12 // of Science and the National Nuclear Security Administration) responsible for
13 // the planning and preparation of a capable exascale ecosystem, including
14 // software, applications, hardware, advanced system engineering and early
15 // testbed platforms, in support of the nation's exascale computing imperative.
16 
17 /// @file
18 /// libCEED QFunctions for mass operator example for a scalar field on the sphere using PETSc
19 
20 #ifndef bp1sphere_h
21 #define bp1sphere_h
22 
23 #include <math.h>
24 
25 // -----------------------------------------------------------------------------
26 // This QFunction sets up the geometric factors required for integration and
27 //   coordinate transformations when reference coordinates have a different
28 //   dimension than the one of physical coordinates
29 //
30 // Reference (parent) 2D coordinates: X \in [-1, 1]^2
31 //
32 // Global 3D physical coordinates given by the mesh: xx \in [-R, R]^3
33 //   with R radius of the sphere
34 //
35 // Local 3D physical coordinates on the 2D manifold: x \in [-l, l]^3
36 //   with l half edge of the cube inscribed in the sphere
37 //
38 // Change of coordinates matrix computed by the library:
39 //   (physical 3D coords relative to reference 2D coords)
40 //   dxx_j/dX_i (indicial notation) [3 * 2]
41 //
42 // Change of coordinates x (on the 2D manifold) relative to xx (phyisical 3D):
43 //   dx_i/dxx_j (indicial notation) [3 * 3]
44 //
45 // Change of coordinates x (on the 2D manifold) relative to X (reference 2D):
46 //   (by chain rule)
47 //   dx_i/dX_j [3 * 2] = dx_i/dxx_k [3 * 3] * dxx_k/dX_j [3 * 2]
48 //
49 // mod_J is given by the magnitude of the cross product of the columns of dx_i/dX_j
50 //
51 // The quadrature data is stored in the array q_data.
52 //
53 // We require the determinant of the Jacobian to properly compute integrals of
54 //   the form: int( u v )
55 //
56 // Qdata: mod_J * w
57 //
58 // -----------------------------------------------------------------------------
59 CEED_QFUNCTION(SetupMassGeo)(void *ctx, const CeedInt Q,
60                              const CeedScalar *const *in,
61                              CeedScalar *const *out) {
62   // Inputs
63   const CeedScalar *X = in[0], *J = in[1], *w = in[2];
64   // Outputs
65   CeedScalar *q_data = out[0];
66 
67   // Quadrature Point Loop
68   CeedPragmaSIMD
69   for (CeedInt i=0; i<Q; i++) {
70     // Read global Cartesian coordinates
71     const CeedScalar xx[3] = {X[i+0*Q],
72                               X[i+1*Q],
73                               X[i+2*Q]
74                              };
75 
76     // Read dxxdX Jacobian entries, stored as
77     // 0 3
78     // 1 4
79     // 2 5
80     const CeedScalar dxxdX[3][2] = {{J[i+Q*0],
81                                      J[i+Q*3]},
82                                     {J[i+Q*1],
83                                      J[i+Q*4]},
84                                     {J[i+Q*2],
85                                      J[i+Q*5]}
86                                    };
87 
88     // Setup
89     // x = xx (xx^T xx)^{-1/2}
90     // dx/dxx = I (xx^T xx)^{-1/2} - xx xx^T (xx^T xx)^{-3/2}
91     const CeedScalar mod_xx_sq = xx[0]*xx[0]+xx[1]*xx[1]+xx[2]*xx[2];
92     CeedScalar xx_sq[3][3];
93     for (int j=0; j<3; j++)
94       for (int k=0; k<3; k++)
95         xx_sq[j][k] = xx[j]*xx[k] / (sqrt(mod_xx_sq) * mod_xx_sq);
96 
97     const CeedScalar dxdxx[3][3] = {{1./sqrt(mod_xx_sq) - xx_sq[0][0],
98                                      -xx_sq[0][1],
99                                      -xx_sq[0][2]},
100                                     {-xx_sq[1][0],
101                                      1./sqrt(mod_xx_sq) - xx_sq[1][1],
102                                      -xx_sq[1][2]},
103                                     {-xx_sq[2][0],
104                                      -xx_sq[2][1],
105                                      1./sqrt(mod_xx_sq) - xx_sq[2][2]}
106                                    };
107 
108     CeedScalar dxdX[3][2];
109     for (int j=0; j<3; j++)
110       for (int k=0; k<2; k++) {
111         dxdX[j][k] = 0;
112         for (int l=0; l<3; l++)
113           dxdX[j][k] += dxdxx[j][l]*dxxdX[l][k];
114       }
115 
116     // J is given by the cross product of the columns of dxdX
117     const CeedScalar J[3] = {dxdX[1][0]*dxdX[2][1] - dxdX[2][0]*dxdX[1][1],
118                              dxdX[2][0]*dxdX[0][1] - dxdX[0][0]*dxdX[2][1],
119                              dxdX[0][0]*dxdX[1][1] - dxdX[1][0]*dxdX[0][1]
120                             };
121 
122     // Use the magnitude of J as our detJ (volume scaling factor)
123     const CeedScalar mod_J = sqrt(J[0]*J[0]+J[1]*J[1]+J[2]*J[2]);
124 
125     // Interp-to-Interp q_data
126     q_data[i+Q*0] = mod_J * w[i];
127   } // End of Quadrature Point Loop
128 
129   return 0;
130 }
131 
132 // -----------------------------------------------------------------------------
133 // This QFunction sets up the rhs and true solution for the problem
134 // -----------------------------------------------------------------------------
135 CEED_QFUNCTION(SetupMassRhs)(void *ctx, const CeedInt Q,
136                              const CeedScalar *const *in,
137                              CeedScalar *const *out) {
138   // Inputs
139   const CeedScalar *X = in[0], *q_data = in[1];
140   // Outputs
141   CeedScalar *true_soln = out[0], *rhs = out[1];
142 
143   // Context
144   const CeedScalar *context = (const CeedScalar*)ctx;
145   const CeedScalar R        = context[0];
146 
147   // Quadrature Point Loop
148   CeedPragmaSIMD
149   for (CeedInt i=0; i<Q; i++) {
150     // Compute latitude
151     const CeedScalar theta =  asin(X[i+2*Q] / R);
152 
153     // Use absolute value of latitude for true solution
154     true_soln[i] = fabs(theta);
155 
156     rhs[i] = q_data[i] * true_soln[i];
157   } // End of Quadrature Point Loop
158 
159   return 0;
160 }
161 
162 // -----------------------------------------------------------------------------
163 // This QFunction applies the mass operator for a scalar field.
164 //
165 // Inputs:
166 //   u     - Input vector at quadrature points
167 //   q_data - Geometric factors
168 //
169 // Output:
170 //   v     - Output vector (test functions) at quadrature points
171 //
172 // -----------------------------------------------------------------------------
173 CEED_QFUNCTION(Mass)(void *ctx, const CeedInt Q,
174                      const CeedScalar *const *in, CeedScalar *const *out) {
175   // Inputs
176   const CeedScalar *u = in[0], *q_data = in[1];
177   // Outputs
178   CeedScalar *v = out[0];
179 
180   // Quadrature Point Loop
181   CeedPragmaSIMD
182   for (CeedInt i=0; i<Q; i++)
183     v[i] = q_data[i] * u[i];
184 
185   return 0;
186 }
187 // -----------------------------------------------------------------------------
188 
189 #endif // bp1sphere_h
190