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