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