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