xref: /libCEED/examples/petsc/qfunctions/bps/bp4.h (revision 9cf88b28e874a91fbe464b60f41b3973b01eeda4)
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 diffusion operator example using PETSc
19 
20 #ifndef bp4_h
21 #define bp4_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 
31 // -----------------------------------------------------------------------------
32 CEED_QFUNCTION(SetupDiffRhs3)(void *ctx, CeedInt Q,
33                               const CeedScalar *const *in,
34                               CeedScalar *const *out) {
35 #ifndef M_PI
36 #  define M_PI    3.14159265358979323846
37 #endif
38   const CeedScalar *x = in[0], *J = in[1], *w = in[2];
39   CeedScalar *true_soln = out[0], *rhs = out[1];
40 
41   // Quadrature Point Loop
42   CeedPragmaSIMD
43   for (CeedInt i=0; i<Q; i++) {
44     const CeedScalar J11 = J[i+Q*0];
45     const CeedScalar J21 = J[i+Q*1];
46     const CeedScalar J31 = J[i+Q*2];
47     const CeedScalar J12 = J[i+Q*3];
48     const CeedScalar J22 = J[i+Q*4];
49     const CeedScalar J32 = J[i+Q*5];
50     const CeedScalar J13 = J[i+Q*6];
51     const CeedScalar J23 = J[i+Q*7];
52     const CeedScalar J33 = J[i+Q*8];
53     const CeedScalar A11 = J22*J33 - J23*J32;
54     const CeedScalar A12 = J13*J32 - J12*J33;
55     const CeedScalar A13 = J12*J23 - J13*J22;
56 
57     const CeedScalar c[3] = { 0, 1., 2. };
58     const CeedScalar k[3] = { 1., 2., 3. };
59 
60     // Component 1
61     true_soln[i+0*Q] = sin(M_PI*(c[0] + k[0]*x[i+Q*0])) *
62                        sin(M_PI*(c[1] + k[1]*x[i+Q*1])) *
63                        sin(M_PI*(c[2] + k[2]*x[i+Q*2]));
64     // Component 2
65     true_soln[i+1*Q] = 2 * true_soln[i+0*Q];
66     // Component 3
67     true_soln[i+2*Q] = 3 * true_soln[i+0*Q];
68 
69     const CeedScalar rho = w[i] * (J11*A11 + J21*A12 + J31*A13);
70     // Component 1
71     rhs[i+0*Q] = rho * M_PI*M_PI * (k[0]*k[0] + k[1]*k[1] + k[2]*k[2]) *
72                  true_soln[i+0*Q];
73     // Component 2
74     rhs[i+1*Q] = 2 * rhs[i+0*Q];
75     // Component 3
76     rhs[i+2*Q] = 3 * rhs[i+0*Q];
77   } // End of Quadrature Point Loop
78 
79   return 0;
80 }
81 
82 // *****************************************************************************
83 // This QFunction applies the diffusion operator for a vector field of 3 components.
84 //
85 // Inputs:
86 //   ug     - Input vector Jacobian at quadrature points
87 //   qdata  - Geometric factors
88 //
89 // Output:
90 //   vJ     - Output vector (test functions) Jacobian at quadrature points
91 //
92 // *****************************************************************************
93 
94 // -----------------------------------------------------------------------------
95 CEED_QFUNCTION(Diff3)(void *ctx, CeedInt Q,
96                      const CeedScalar *const *in, CeedScalar *const *out) {
97   const CeedScalar *ug = in[0], *qd = in[1];
98   CeedScalar *vg = out[0];
99 
100   // Quadrature Point Loop
101   CeedPragmaSIMD
102   for (CeedInt i=0; i<Q; i++) {
103     // Read spatial derivatives of u components
104     const CeedScalar uJ[3][3]        = {{ug[i+(0+0*3)*Q],
105                                          ug[i+(0+1*3)*Q],
106                                          ug[i+(0+2*3)*Q]},
107                                         {ug[i+(1+0*3)*Q],
108                                          ug[i+(1+1*3)*Q],
109                                          ug[i+(1+2*3)*Q]},
110                                         {ug[i+(2+0*3)*Q],
111                                          ug[i+(2+1*3)*Q],
112                                          ug[i+(2+2*3)*Q]}
113                                        };
114     // Read qdata (dXdxdXdxT symmetric matrix)
115     const CeedScalar dXdxdXdxT[3][3] = {{qd[i+0*Q],
116                                          qd[i+1*Q],
117                                          qd[i+2*Q]},
118                                         {qd[i+1*Q],
119                                          qd[i+3*Q],
120                                          qd[i+4*Q]},
121                                         {qd[i+2*Q],
122                                          qd[i+4*Q],
123                                          qd[i+5*Q]}
124                                        };
125 
126     for (int k=0; k<3; k++) // k = component
127       for (int j=0; j<3; j++) // j = direction of vg
128         vg[i+(k+j*3)*Q] = (uJ[k][0] * dXdxdXdxT[0][j] +
129                            uJ[k][1] * dXdxdXdxT[1][j] +
130                            uJ[k][2] * dXdxdXdxT[2][j]);
131   } // End of Quadrature Point Loop
132 
133   return 0;
134 }
135 // -----------------------------------------------------------------------------
136 
137 #endif // bp4_h
138