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