xref: /libCEED/examples/petsc/qfunctions/bps/bp4sphere.h (revision 3d8e882215d238700cdceb37404f76ca7fa24eaa)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 /// @file
9 /// libCEED QFunctions for mass operator example for a vector field on the sphere using PETSc
10 
11 #ifndef bp4sphere_h
12 #define bp4sphere_h
13 
14 #include <math.h>
15 
16 // -----------------------------------------------------------------------------
17 // This QFunction sets up the rhs and true solution for the problem
18 // -----------------------------------------------------------------------------
19 CEED_QFUNCTION(SetupDiffRhs3)(void *ctx, const CeedInt Q,
20                              const CeedScalar *const *in,
21                              CeedScalar *const *out) {
22   // Inputs
23   const CeedScalar *X = in[0], *q_data = in[1];
24   // Outputs
25   CeedScalar *true_soln = out[0], *rhs = out[1];
26 
27   // Context
28   const CeedScalar *context = (const CeedScalar*)ctx;
29   const CeedScalar R        = context[0];
30 
31   // Quadrature Point Loop
32   CeedPragmaSIMD
33   for (CeedInt i=0; i<Q; i++) {
34     // Read global Cartesian coordinates
35     CeedScalar x = X[i+Q*0], y = X[i+Q*1], z = X[i+Q*2];
36     // Normalize quadrature point coordinates to sphere
37     CeedScalar rad = sqrt(x*x + y*y + z*z);
38     x *= R / rad;
39     y *= R / rad;
40     z *= R / rad;
41     // Compute latitude and longitude
42     const CeedScalar theta  = asin(z / R); // latitude
43     const CeedScalar lambda = atan2(y, x); // longitude
44 
45     // Use absolute value of latitude for true solution
46     // Component 1
47     true_soln[i+0*Q] = sin(lambda) * cos(theta);
48     // Component 2
49     true_soln[i+1*Q] = 2 * true_soln[i+0*Q];
50     // Component 3
51     true_soln[i+2*Q] = 3 * true_soln[i+0*Q];
52 
53     // Component 1
54     rhs[i+0*Q] = q_data[i+Q*0] * 2 * sin(lambda)*cos(theta) / (R*R);
55     // Component 2
56     rhs[i+1*Q] = 2 * rhs[i+0*Q];
57     // Component 3
58     rhs[i+2*Q] = 3 * rhs[i+0*Q];
59   } // End of Quadrature Point Loop
60 
61   return 0;
62 }
63 
64 // -----------------------------------------------------------------------------
65 // This QFunction applies the diffusion operator for a vector field of 3 components.
66 //
67 // Inputs:
68 //   ug     - Input vector Jacobian at quadrature points
69 //   q_data  - Geometric factors
70 //
71 // Output:
72 //   vJ     - Output vector (test functions) Jacobian at quadrature points
73 //
74 // -----------------------------------------------------------------------------
75 CEED_QFUNCTION(Diff3)(void *ctx, const CeedInt Q,
76                       const CeedScalar *const *in, CeedScalar *const *out) {
77   const CeedScalar *ug = in[0], *q_data = in[1];
78   CeedScalar *vJ = out[0];
79 
80   // Quadrature Point Loop
81   CeedPragmaSIMD
82   for (CeedInt i=0; i<Q; i++) {
83     // Read spatial derivatives of u
84     const CeedScalar uJ[3][2]         = {{ug[i+(0+0*3)*Q],
85                                           ug[i+(0+1*3)*Q]},
86                                          {ug[i+(1+0*3)*Q],
87                                           ug[i+(1+1*3)*Q]},
88                                          {ug[i+(2+0*3)*Q],
89                                           ug[i+(2+1*3)*Q]}
90                                         };
91     // Read q_data
92     const CeedScalar w_det_J          =   q_data[i+Q*0];
93     // -- Grad-to-Grad q_data
94     // ---- dXdx_j,k * dXdx_k,j
95     const CeedScalar dXdxdXdx_T[2][2] = {{q_data[i+Q*1],
96                                           q_data[i+Q*3]},
97                                          {q_data[i+Q*3],
98                                           q_data[i+Q*2]}
99                                         };
100 
101     for (int k=0; k<3; k++) // k = component
102       for (int j=0; j<2; j++) // j = direction of vg
103         vJ[i+(k+j*3)*Q] = w_det_J * (uJ[k][0] * dXdxdXdx_T[0][j] +
104                                    uJ[k][1] * dXdxdXdx_T[1][j]);
105 
106   } // End of Quadrature Point Loop
107 
108   return 0;
109 }
110 // -----------------------------------------------------------------------------
111 
112 #endif // bp4sphere_h
113