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