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