xref: /libCEED/examples/petsc/qfunctions/bps/bp3.h (revision b761d2cab99a31c30bb32e57e285fc6533e68118)
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 CEED_QFUNCTION(SetupDiffGeo)(void *ctx, CeedInt Q,
52                              const CeedScalar *const *in,
53                              CeedScalar *const *out) {
54   const CeedScalar *J = in[1], *w = in[2]; // Note: *X = in[0]
55   CeedScalar *qd = out[0];
56 
57   // Quadrature Point Loop
58   CeedPragmaSIMD
59   for (CeedInt i=0; i<Q; i++) {
60     const CeedScalar J11 = J[i+Q*0];
61     const CeedScalar J21 = J[i+Q*1];
62     const CeedScalar J31 = J[i+Q*2];
63     const CeedScalar J12 = J[i+Q*3];
64     const CeedScalar J22 = J[i+Q*4];
65     const CeedScalar J32 = J[i+Q*5];
66     const CeedScalar J13 = J[i+Q*6];
67     const CeedScalar J23 = J[i+Q*7];
68     const CeedScalar J33 = J[i+Q*8];
69     const CeedScalar A11 = J22*J33 - J23*J32;
70     const CeedScalar A12 = J13*J32 - J12*J33;
71     const CeedScalar A13 = J12*J23 - J13*J22;
72     const CeedScalar A21 = J23*J31 - J21*J33;
73     const CeedScalar A22 = J11*J33 - J13*J31;
74     const CeedScalar A23 = J13*J21 - J11*J23;
75     const CeedScalar A31 = J21*J32 - J22*J31;
76     const CeedScalar A32 = J12*J31 - J11*J32;
77     const CeedScalar A33 = J11*J22 - J12*J21;
78     const CeedScalar qw = w[i] / (J11*A11 + J21*A12 + J31*A13);
79     qd[i+Q*0] = qw * (A11*A11 + A12*A12 + A13*A13);
80     qd[i+Q*1] = qw * (A11*A21 + A12*A22 + A13*A23);
81     qd[i+Q*2] = qw * (A11*A31 + A12*A32 + A13*A33);
82     qd[i+Q*3] = qw * (A21*A21 + A22*A22 + A23*A23);
83     qd[i+Q*4] = qw * (A21*A31 + A22*A32 + A23*A33);
84     qd[i+Q*5] = qw * (A31*A31 + A32*A32 + A33*A33);
85     qd[i+Q*6] = w[i] * (J11*A11 + J21*A12 + J31*A13);
86   } // End of Quadrature Point Loop
87 
88   return 0;
89 }
90 
91 // -----------------------------------------------------------------------------
92 // This QFunction sets up the rhs and true solution for the problem
93 // -----------------------------------------------------------------------------
94 CEED_QFUNCTION(SetupDiffRhs)(void *ctx, CeedInt Q,
95                              const CeedScalar *const *in,
96                              CeedScalar *const *out) {
97 #ifndef M_PI
98 #  define M_PI    3.14159265358979323846
99 #endif
100   const CeedScalar *x = in[0], *w = in[1];
101   CeedScalar *true_soln = out[0], *rhs = out[1];
102 
103   // Quadrature Point Loop
104   CeedPragmaSIMD
105   for (CeedInt i=0; i<Q; i++) {
106     const CeedScalar c[3] = { 0, 1., 2. };
107     const CeedScalar k[3] = { 1., 2., 3. };
108 
109     true_soln[i] = sin(M_PI*(c[0] + k[0]*x[i+Q*0])) *
110                    sin(M_PI*(c[1] + k[1]*x[i+Q*1])) *
111                    sin(M_PI*(c[2] + k[2]*x[i+Q*2]));
112 
113     rhs[i] = w[i+Q*6] * M_PI*M_PI * (k[0]*k[0] + k[1]*k[1] + k[2]*k[2]) *
114              true_soln[i];
115   } // End of Quadrature Point Loop
116 
117   return 0;
118 }
119 
120 // -----------------------------------------------------------------------------
121 // This QFunction applies the diffusion operator for a scalar field.
122 //
123 // Inputs:
124 //   ug     - Input vector gradient at quadrature points
125 //   q_data  - Geometric factors
126 //
127 // Output:
128 //   vg     - Output vector (test functions) gradient at quadrature points
129 //
130 // -----------------------------------------------------------------------------
131 CEED_QFUNCTION(Diff)(void *ctx, CeedInt Q,
132                      const CeedScalar *const *in, CeedScalar *const *out) {
133   const CeedScalar *ug = in[0], *q_data = in[1];
134   CeedScalar *vg = out[0];
135 
136   // Quadrature Point Loop
137   CeedPragmaSIMD
138   for (CeedInt i=0; i<Q; i++) {
139     // Read spatial derivatives of u
140     const CeedScalar du[3]            =  {ug[i+Q*0],
141                                           ug[i+Q*1],
142                                           ug[i+Q*2]
143                                          };
144     // Read q_data (dXdxdXdx_T symmetric matrix)
145     const CeedScalar dXdxdXdx_T[3][3] = {{q_data[i+0*Q],
146                                           q_data[i+1*Q],
147                                           q_data[i+2*Q]},
148                                          {q_data[i+1*Q],
149                                           q_data[i+3*Q],
150                                           q_data[i+4*Q]},
151                                          {q_data[i+2*Q],
152                                           q_data[i+4*Q],
153                                           q_data[i+5*Q]}
154                                         };
155 
156     for (int j=0; j<3; j++) // j = direction of vg
157       vg[i+j*Q] = (du[0] * dXdxdXdx_T[0][j] +
158                    du[1] * dXdxdXdx_T[1][j] +
159                    du[2] * dXdxdXdx_T[2][j]);
160 
161   } // End of Quadrature Point Loop
162   return 0;
163 }
164 // -----------------------------------------------------------------------------
165 
166 #endif // bp3_h
167