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