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