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