xref: /libCEED/examples/petsc/qfunctions/bps/bp3.h (revision 423d854df121c192e1d9e4962fbebdb36fe7ec33)
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 bp3_h
21 #define bp3_h
22 
23 #ifndef __CUDACC__
24 #  include <math.h>
25 #endif
26 
27 // *****************************************************************************
28 // This QFunction sets up the geometric factors required to apply the
29 //   diffusion operator
30 //
31 // We require the product of the inverse of the Jacobian and its transpose to
32 //   properly compute integrals of the form: int( gradv gradu)
33 //
34 // Determinant of Jacobian:
35 //   detJ = J11*A11 + J21*A12 + J31*A13
36 //     Jij = Jacobian entry ij
37 //     Aij = Adjoint ij
38 //
39 // Inverse of Jacobian:
40 //   Bij = Aij / detJ
41 //
42 // Product of Inverse and Transpose:
43 //   BBij = sum( Bik Bkj )
44 //
45 // Stored: w B^T B detJ = w A^T A / detJ
46 //   Note: This matrix is symmetric, so we only store 6 distinct entries
47 //     qd: 0 3 6
48 //         1 4 7
49 //         2 5 8
50 // *****************************************************************************
51 
52 // -----------------------------------------------------------------------------
53 CEED_QFUNCTION(SetupDiffGeo)(void *ctx, CeedInt Q,
54                              const CeedScalar *const *in,
55                              CeedScalar *const *out) {
56   const CeedScalar *J = in[0], *w = in[1];
57   CeedScalar *qd = out[0];
58 
59   // Quadrature Point Loop
60   CeedPragmaSIMD
61   for (CeedInt i=0; i<Q; i++) {
62     const CeedScalar J11 = J[i+Q*0];
63     const CeedScalar J21 = J[i+Q*1];
64     const CeedScalar J31 = J[i+Q*2];
65     const CeedScalar J12 = J[i+Q*3];
66     const CeedScalar J22 = J[i+Q*4];
67     const CeedScalar J32 = J[i+Q*5];
68     const CeedScalar J13 = J[i+Q*6];
69     const CeedScalar J23 = J[i+Q*7];
70     const CeedScalar J33 = J[i+Q*8];
71     const CeedScalar A11 = J22*J33 - J23*J32;
72     const CeedScalar A12 = J13*J32 - J12*J33;
73     const CeedScalar A13 = J12*J23 - J13*J22;
74     const CeedScalar A21 = J23*J31 - J21*J33;
75     const CeedScalar A22 = J11*J33 - J13*J31;
76     const CeedScalar A23 = J13*J21 - J11*J23;
77     const CeedScalar A31 = J21*J32 - J22*J31;
78     const CeedScalar A32 = J12*J31 - J11*J32;
79     const CeedScalar A33 = J11*J22 - J12*J21;
80     const CeedScalar qw = w[i] / (J11*A11 + J21*A12 + J31*A13);
81     qd[i+Q*0] = qw * (A11*A11 + A12*A12 + A13*A13);
82     qd[i+Q*1] = qw * (A11*A21 + A12*A22 + A13*A23);
83     qd[i+Q*2] = qw * (A11*A31 + A12*A32 + A13*A33);
84     qd[i+Q*3] = qw * (A21*A21 + A22*A22 + A23*A23);
85     qd[i+Q*4] = qw * (A21*A31 + A22*A32 + A23*A33);
86     qd[i+Q*5] = qw * (A31*A31 + A32*A32 + A33*A33);
87   } // End of Quadrature Point Loop
88 
89   return 0;
90 }
91 
92 // *****************************************************************************
93 // This QFunction sets up the rhs and true solution for the problem
94 // *****************************************************************************
95 
96 // -----------------------------------------------------------------------------
97 CEED_QFUNCTION(SetupDiffRhs)(void *ctx, CeedInt Q,
98                              const CeedScalar *const *in,
99                              CeedScalar *const *out) {
100 #ifndef M_PI
101 #  define M_PI    3.14159265358979323846
102 #endif
103   const CeedScalar *x = in[0], *J = in[1], *w = in[2];
104   CeedScalar *true_soln = out[0], *rhs = out[1];
105 
106   // Quadrature Point Loop
107   CeedPragmaSIMD
108   for (CeedInt i=0; i<Q; i++) {
109     const CeedScalar J11 = J[i+Q*0];
110     const CeedScalar J21 = J[i+Q*1];
111     const CeedScalar J31 = J[i+Q*2];
112     const CeedScalar J12 = J[i+Q*3];
113     const CeedScalar J22 = J[i+Q*4];
114     const CeedScalar J32 = J[i+Q*5];
115     const CeedScalar J13 = J[i+Q*6];
116     const CeedScalar J23 = J[i+Q*7];
117     const CeedScalar J33 = J[i+Q*8];
118     const CeedScalar A11 = J22*J33 - J23*J32;
119     const CeedScalar A12 = J13*J32 - J12*J33;
120     const CeedScalar A13 = J12*J23 - J13*J22;
121 
122     const CeedScalar c[3] = { 0, 1., 2. };
123     const CeedScalar k[3] = { 1., 2., 3. };
124 
125     true_soln[i] = sin(M_PI*(c[0] + k[0]*x[i+Q*0])) *
126                    sin(M_PI*(c[1] + k[1]*x[i+Q*1])) *
127                    sin(M_PI*(c[2] + k[2]*x[i+Q*2]));
128 
129     const CeedScalar rho = w[i] * (J11*A11 + J21*A12 + J31*A13);
130     rhs[i] = rho * M_PI*M_PI * (k[0]*k[0] + k[1]*k[1] + k[2]*k[2]) *
131              true_soln[i];
132   } // End of Quadrature Point Loop
133 
134   return 0;
135 }
136 
137 // *****************************************************************************
138 // This QFunction applies the diffusion operator for a scalar field.
139 //
140 // Inputs:
141 //   ug     - Input vector gradient at quadrature points
142 //   qdata  - Geometric factors
143 //
144 // Output:
145 //   vg     - Output vector (test functions) gradient at quadrature points
146 //
147 // *****************************************************************************
148 
149 // -----------------------------------------------------------------------------
150 CEED_QFUNCTION(Diff)(void *ctx, CeedInt Q,
151                      const CeedScalar *const *in, CeedScalar *const *out) {
152   const CeedScalar *ug = in[0], *qdata = in[1];
153   CeedScalar *vg = out[0];
154 
155   // Quadrature Point Loop
156   CeedPragmaSIMD
157   for (CeedInt i=0; i<Q; i++) {
158     // Read spatial derivatives of u
159     const CeedScalar du[3]        =  {ug[i+Q*0],
160                                       ug[i+Q*1],
161                                       ug[i+Q*2]
162                                      };
163     // Read qdata (dXdxdXdxT symmetric matrix)
164     const CeedScalar dXdxdXdxT[3][3] = {{qdata[i+0*Q],
165                                          qdata[i+1*Q],
166                                          qdata[i+2*Q]},
167                                         {qdata[i+1*Q],
168                                          qdata[i+3*Q],
169                                          qdata[i+4*Q]},
170                                         {qdata[i+2*Q],
171                                          qdata[i+4*Q],
172                                          qdata[i+5*Q]}
173                                        };
174 
175     for (int j=0; j<3; j++) // j = direction of vg
176       vg[i+j*Q] = (du[0] * dXdxdXdxT[0][j] +
177                    du[1] * dXdxdXdxT[1][j] +
178                    du[2] * dXdxdXdxT[2][j]);
179 
180   } // End of Quadrature Point Loop
181   return 0;
182 }
183 // -----------------------------------------------------------------------------
184 
185 #endif // bp3_h
186