xref: /libCEED/examples/fluids/qfunctions/setupgeo.h (revision caee03026e6576cbf7a399c2fc51bb918c77f451)
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 /// Geometric factors (3D) for Navier-Stokes example using PETSc
10 
11 #ifndef setup_geo_h
12 #define setup_geo_h
13 
14 #include <ceed.h>
15 #include <math.h>
16 
17 // *****************************************************************************
18 // This QFunction sets up the geometric factors required for integration and
19 //   coordinate transformations
20 //
21 // Reference (parent) coordinates: X
22 // Physical (current) coordinates: x
23 // Change of coordinate matrix: dxdX_{i,j} = x_{i,j} (indicial notation)
24 // Inverse of change of coordinate matrix: dXdx_{i,j} = (detJ^-1) * X_{i,j}
25 //
26 // All quadrature data is stored in 10 field vector of quadrature data.
27 //
28 // We require the determinant of the Jacobian to properly compute integrals of
29 //   the form: int( v u )
30 //
31 // Determinant of Jacobian:
32 //   detJ = J11*A11 + J21*A12 + J31*A13
33 //     Jij = Jacobian entry ij
34 //     Aij = Adjoint ij
35 //
36 // Stored: w detJ
37 //   in q_data[0]
38 //
39 // We require the transpose of the inverse of the Jacobian to properly compute
40 //   integrals of the form: int( gradv u )
41 //
42 // Inverse of Jacobian:
43 //   dXdx_i,j = Aij / detJ
44 //
45 // Stored: Aij / detJ
46 //   in q_data[1:9] as
47 //   (detJ^-1) * [A11 A12 A13]
48 //               [A21 A22 A23]
49 //               [A31 A32 A33]
50 //
51 // *****************************************************************************
52 CEED_QFUNCTION(Setup)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
53   // Inputs
54   const CeedScalar(*J)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[0];
55   const CeedScalar(*w)                = in[1];
56 
57   // Outputs
58   CeedScalar(*q_data)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
59 
60   CeedPragmaSIMD
61       // Quadrature Point Loop
62       for (CeedInt i = 0; i < Q; i++) {
63     // Setup
64     const CeedScalar J11  = J[0][0][i];
65     const CeedScalar J21  = J[0][1][i];
66     const CeedScalar J31  = J[0][2][i];
67     const CeedScalar J12  = J[1][0][i];
68     const CeedScalar J22  = J[1][1][i];
69     const CeedScalar J32  = J[1][2][i];
70     const CeedScalar J13  = J[2][0][i];
71     const CeedScalar J23  = J[2][1][i];
72     const CeedScalar J33  = J[2][2][i];
73     const CeedScalar A11  = J22 * J33 - J23 * J32;
74     const CeedScalar A12  = J13 * J32 - J12 * J33;
75     const CeedScalar A13  = J12 * J23 - J13 * J22;
76     const CeedScalar A21  = J23 * J31 - J21 * J33;
77     const CeedScalar A22  = J11 * J33 - J13 * J31;
78     const CeedScalar A23  = J13 * J21 - J11 * J23;
79     const CeedScalar A31  = J21 * J32 - J22 * J31;
80     const CeedScalar A32  = J12 * J31 - J11 * J32;
81     const CeedScalar A33  = J11 * J22 - J12 * J21;
82     const CeedScalar detJ = J11 * A11 + J21 * A12 + J31 * A13;
83 
84     // Qdata
85     // -- Interp-to-Interp q_data
86     q_data[0][i] = w[i] * detJ;
87     // -- Interp-to-Grad q_data
88     // Inverse of change of coordinate matrix: X_i,j
89     q_data[1][i] = A11 / detJ;
90     q_data[2][i] = A12 / detJ;
91     q_data[3][i] = A13 / detJ;
92     q_data[4][i] = A21 / detJ;
93     q_data[5][i] = A22 / detJ;
94     q_data[6][i] = A23 / detJ;
95     q_data[7][i] = A31 / detJ;
96     q_data[8][i] = A32 / detJ;
97     q_data[9][i] = A33 / detJ;
98 
99   }  // End of Quadrature Point Loop
100 
101   // Return
102   return 0;
103 }
104 
105 // *****************************************************************************
106 // This QFunction sets up the geometric factor required for integration when
107 //   reference coordinates are in 2D and the physical coordinates are in 3D
108 //
109 // Reference (parent) 2D coordinates: X
110 // Physical (current) 3D coordinates: x
111 // Change of coordinate matrix:
112 //   dxdX_{i,j} = dx_i/dX_j (indicial notation) [3 * 2]
113 // Inverse change of coordinate matrix:
114 //   dXdx_{i,j} = dX_i/dx_j (indicial notation) [2 * 3]
115 //
116 // (J1,J2,J3) is given by the cross product of the columns of dxdX_{i,j}
117 //
118 // detJb is the magnitude of (J1,J2,J3)
119 //
120 // dXdx is calculated via Moore–Penrose inverse:
121 //
122 //   dX_i/dx_j = (dxdX^T dxdX)^(-1) dxdX
123 //             = (dx_l/dX_i * dx_l/dX_k)^(-1) dx_j/dX_k
124 //
125 // All quadrature data is stored in 10 field vector of quadrature data.
126 //
127 // We require the determinant of the Jacobian to properly compute integrals of
128 //   the form: int( u v )
129 //
130 // Stored: w detJb
131 //   in q_data_sur[0]
132 //
133 // Normal vector = (J1,J2,J3) / detJb
134 //
135 //   - TODO Could possibly remove normal vector, as it could be calculated in the Qfunction from dXdx
136 // Stored: (J1,J2,J3) / detJb
137 //   in q_data_sur[1:3] as
138 //   (detJb^-1) * [ J1 ]
139 //                [ J2 ]
140 //                [ J3 ]
141 //
142 // Stored: dXdx_{i,j}
143 //   in q_data_sur[4:9] as
144 //    [dXdx_11 dXdx_12 dXdx_13]
145 //    [dXdx_21 dXdx_22 dXdx_23]
146 //
147 // *****************************************************************************
148 CEED_QFUNCTION(SetupBoundary)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
149   // Inputs
150   const CeedScalar(*J)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[0];
151   const CeedScalar(*w)                = in[1];
152 
153   // Outputs
154   CeedScalar(*q_data_sur)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
155 
156   CeedPragmaSIMD
157       // Quadrature Point Loop
158       for (CeedInt i = 0; i < Q; i++) {
159     // Setup
160     const CeedScalar dxdX[3][2] = {
161         {J[0][0][i], J[1][0][i]},
162         {J[0][1][i], J[1][1][i]},
163         {J[0][2][i], J[1][2][i]}
164     };
165     // J1, J2, and J3 are given by the cross product of the columns of dxdX
166     const CeedScalar J1 = dxdX[1][0] * dxdX[2][1] - dxdX[2][0] * dxdX[1][1];
167     const CeedScalar J2 = dxdX[2][0] * dxdX[0][1] - dxdX[0][0] * dxdX[2][1];
168     const CeedScalar J3 = dxdX[0][0] * dxdX[1][1] - dxdX[1][0] * dxdX[0][1];
169 
170     const CeedScalar detJb = sqrt(J1 * J1 + J2 * J2 + J3 * J3);
171 
172     // q_data_sur
173     // -- Interp-to-Interp q_data_sur
174     q_data_sur[0][i] = w[i] * detJb;
175     q_data_sur[1][i] = J1 / detJb;
176     q_data_sur[2][i] = J2 / detJb;
177     q_data_sur[3][i] = J3 / detJb;
178 
179     // dxdX_k,j * dxdX_j,k
180     CeedScalar dxdXTdxdX[2][2] = {{0.}};
181     for (CeedInt j = 0; j < 2; j++) {
182       for (CeedInt k = 0; k < 2; k++) {
183         for (CeedInt l = 0; l < 3; l++) dxdXTdxdX[j][k] += dxdX[l][j] * dxdX[l][k];
184       }
185     }
186 
187     const CeedScalar detdxdXTdxdX = dxdXTdxdX[0][0] * dxdXTdxdX[1][1] - dxdXTdxdX[1][0] * dxdXTdxdX[0][1];
188 
189     // Compute inverse of dxdXTdxdX
190     CeedScalar dxdXTdxdX_inv[2][2];
191     dxdXTdxdX_inv[0][0] = dxdXTdxdX[1][1] / detdxdXTdxdX;
192     dxdXTdxdX_inv[0][1] = -dxdXTdxdX[0][1] / detdxdXTdxdX;
193     dxdXTdxdX_inv[1][0] = -dxdXTdxdX[1][0] / detdxdXTdxdX;
194     dxdXTdxdX_inv[1][1] = dxdXTdxdX[0][0] / detdxdXTdxdX;
195 
196     // Compute dXdx from dxdXTdxdX^-1 and dxdX
197     CeedScalar dXdx[2][3] = {{0.}};
198     for (CeedInt j = 0; j < 2; j++) {
199       for (CeedInt k = 0; k < 3; k++) {
200         for (CeedInt l = 0; l < 2; l++) dXdx[j][k] += dxdXTdxdX_inv[l][j] * dxdX[k][l];
201       }
202     }
203 
204     q_data_sur[4][i] = dXdx[0][0];
205     q_data_sur[5][i] = dXdx[0][1];
206     q_data_sur[6][i] = dXdx[0][2];
207     q_data_sur[7][i] = dXdx[1][0];
208     q_data_sur[8][i] = dXdx[1][1];
209     q_data_sur[9][i] = dXdx[1][2];
210 
211   }  // End of Quadrature Point Loop
212 
213   // Return
214   return 0;
215 }
216 
217 // *****************************************************************************
218 
219 #endif  // setup_geo_h
220