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