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