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