xref: /libCEED/examples/petsc/qfunctions/bps/bp4sphere.h (revision b761d2cab99a31c30bb32e57e285fc6533e68118)
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 vector field on the sphere using PETSc
19 
20 #ifndef bp4sphere_h
21 #define bp4sphere_h
22 
23 #ifndef __CUDACC__
24 #  include <math.h>
25 #endif
26 
27 // -----------------------------------------------------------------------------
28 // This QFunction sets up the rhs and true solution for the problem
29 // -----------------------------------------------------------------------------
30 CEED_QFUNCTION(SetupDiffRhs3)(void *ctx, const CeedInt Q,
31                              const CeedScalar *const *in,
32                              CeedScalar *const *out) {
33   // Inputs
34   const CeedScalar *X = in[0], *q_data = in[1];
35   // Outputs
36   CeedScalar *true_soln = out[0], *rhs = out[1];
37 
38   // Context
39   const CeedScalar *context = (const CeedScalar*)ctx;
40   const CeedScalar R        = context[0];
41 
42   // Quadrature Point Loop
43   CeedPragmaSIMD
44   for (CeedInt i=0; i<Q; i++) {
45     // Read global Cartesian coordinates
46     CeedScalar x = X[i+Q*0], y = X[i+Q*1], z = X[i+Q*2];
47     // Normalize quadrature point coordinates to sphere
48     CeedScalar rad = sqrt(x*x + y*y + z*z);
49     x *= R / rad;
50     y *= R / rad;
51     z *= R / rad;
52     // Compute latitude and longitude
53     const CeedScalar theta  = asin(z / R); // latitude
54     const CeedScalar lambda = atan2(y, x); // longitude
55 
56     // Use absolute value of latitude for true solution
57     // Component 1
58     true_soln[i+0*Q] = sin(lambda) * cos(theta);
59     // Component 2
60     true_soln[i+1*Q] = 2 * true_soln[i+0*Q];
61     // Component 3
62     true_soln[i+2*Q] = 3 * true_soln[i+0*Q];
63 
64     // Component 1
65     rhs[i+0*Q] = q_data[i+Q*0] * 2 * sin(lambda)*cos(theta) / (R*R);
66     // Component 2
67     rhs[i+1*Q] = 2 * rhs[i+0*Q];
68     // Component 3
69     rhs[i+2*Q] = 3 * rhs[i+0*Q];
70   } // End of Quadrature Point Loop
71 
72   return 0;
73 }
74 
75 // -----------------------------------------------------------------------------
76 // This QFunction applies the diffusion operator for a vector field of 3 components.
77 //
78 // Inputs:
79 //   ug     - Input vector Jacobian at quadrature points
80 //   q_data  - Geometric factors
81 //
82 // Output:
83 //   vJ     - Output vector (test functions) Jacobian at quadrature points
84 //
85 // -----------------------------------------------------------------------------
86 CEED_QFUNCTION(Diff3)(void *ctx, const CeedInt Q,
87                       const CeedScalar *const *in, CeedScalar *const *out) {
88   const CeedScalar *ug = in[0], *q_data = in[1];
89   CeedScalar *vJ = out[0];
90 
91   // Quadrature Point Loop
92   CeedPragmaSIMD
93   for (CeedInt i=0; i<Q; i++) {
94     // Read spatial derivatives of u
95     const CeedScalar uJ[3][2]         = {{ug[i+(0+0*3)*Q],
96                                           ug[i+(0+1*3)*Q]},
97                                          {ug[i+(1+0*3)*Q],
98                                           ug[i+(1+1*3)*Q]},
99                                          {ug[i+(2+0*3)*Q],
100                                           ug[i+(2+1*3)*Q]}
101                                         };
102     // Read q_data
103     const CeedScalar w_det_J          =   q_data[i+Q*0];
104     // -- Grad-to-Grad q_data
105     // ---- dXdx_j,k * dXdx_k,j
106     const CeedScalar dXdxdXdx_T[2][2] = {{q_data[i+Q*1],
107                                           q_data[i+Q*3]},
108                                          {q_data[i+Q*3],
109                                           q_data[i+Q*2]}
110                                         };
111 
112     for (int k=0; k<3; k++) // k = component
113       for (int j=0; j<2; j++) // j = direction of vg
114         vJ[i+(k+j*3)*Q] = w_det_J * (uJ[k][0] * dXdxdXdx_T[0][j] +
115                                    uJ[k][1] * dXdxdXdx_T[1][j]);
116 
117   } // End of Quadrature Point Loop
118 
119   return 0;
120 }
121 // -----------------------------------------------------------------------------
122 
123 #endif // bp4sphere_h
124