1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors. 2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause 3 4 /// @file 5 /// Geometric factors (3D) for Navier-Stokes example using PETSc 6 #pragma once 7 8 #include <ceed/types.h> 9 #include "utils.h" 10 11 /** 12 * @brief Calculate dXdx from dxdX for 3D elements 13 * 14 * Reference (parent) coordinates: X 15 * Physical (current) coordinates: x 16 * Change of coordinate matrix: dxdX_{i,j} = x_{i,j} (indicial notation) 17 * Inverse of change of coordinate matrix: dXdx_{i,j} = (detJ^-1) * X_{i,j} 18 * 19 * Determinant of Jacobian: 20 * detJ = J11*A11 + J21*A12 + J31*A13 21 * Jij = Jacobian entry ij 22 * Aij = Adjugate ij 23 * 24 * Inverse of Jacobian: 25 * dXdx_i,j = Aij / detJ 26 * 27 * @param[in] Q Number of quadrature points 28 * @param[in] i Current quadrature point 29 * @param[in] dxdX_q Mapping Jacobian (gradient of the coordinate space) 30 * @param[out] dXdx Inverse of mapping Jacobian at quadrature point i 31 * @param[out] detJ_ptr Determinate of the Jacobian, may be NULL is not desired 32 */ 33 CEED_QFUNCTION_HELPER void InvertMappingJacobian_3D(CeedInt Q, CeedInt i, const CeedScalar (*dxdX_q)[3][CEED_Q_VLA], CeedScalar dXdx[3][3], 34 CeedScalar *detJ_ptr) { 35 CeedScalar dxdX[3][3]; 36 37 GradUnpack3(Q, i, 3, (CeedScalar *)dxdX_q, dxdX); 38 // Compute Adjugate of dxdX 39 dXdx[0][0] = dxdX[1][1] * dxdX[2][2] - dxdX[1][2] * dxdX[2][1]; 40 dXdx[0][1] = dxdX[0][2] * dxdX[2][1] - dxdX[0][1] * dxdX[2][2]; 41 dXdx[0][2] = dxdX[0][1] * dxdX[1][2] - dxdX[0][2] * dxdX[1][1]; 42 dXdx[1][0] = dxdX[1][2] * dxdX[2][0] - dxdX[1][0] * dxdX[2][2]; 43 dXdx[1][1] = dxdX[0][0] * dxdX[2][2] - dxdX[0][2] * dxdX[2][0]; 44 dXdx[1][2] = dxdX[0][2] * dxdX[1][0] - dxdX[0][0] * dxdX[1][2]; 45 dXdx[2][0] = dxdX[1][0] * dxdX[2][1] - dxdX[1][1] * dxdX[2][0]; 46 dXdx[2][1] = dxdX[0][1] * dxdX[2][0] - dxdX[0][0] * dxdX[2][1]; 47 dXdx[2][2] = dxdX[0][0] * dxdX[1][1] - dxdX[0][1] * dxdX[1][0]; 48 49 const CeedScalar detJ = dxdX[0][0] * dXdx[0][0] + dxdX[1][0] * dXdx[0][1] + dxdX[2][0] * dXdx[0][2]; 50 ScaleN((CeedScalar *)dXdx, 1 / detJ, 9); 51 if (detJ_ptr) *detJ_ptr = detJ; 52 } 53 54 /** 55 * @brief Calculate dXdx from dxdX for 2D elements 56 * 57 * Reference (parent) coordinates: X 58 * Physical (current) coordinates: x 59 * Change of coordinate matrix: dxdX_{i,j} = x_{i,j} (indicial notation) 60 * Inverse of change of coordinate matrix: dXdx_{i,j} = (detJ^-1) * X_{i,j} 61 * 62 * Determinant of Jacobian: 63 * detJ = J11*J22 - J21*J12 64 * Jij = Jacobian entry ij 65 * Aij = Adjugate ij 66 * 67 * Inverse of Jacobian: 68 * dXdx_i,j = Aij / detJ 69 * 70 * @param[in] Q Number of quadrature points 71 * @param[in] i Current quadrature point 72 * @param[in] dxdX_q Mapping Jacobian (gradient of the coordinate space) 73 * @param[out] dXdx Inverse of mapping Jacobian at quadrature point i 74 * @param[out] detJ_ptr Determinate of the Jacobian, may be NULL is not desired 75 */ 76 CEED_QFUNCTION_HELPER void InvertMappingJacobian_2D(CeedInt Q, CeedInt i, const CeedScalar (*dxdX_q)[2][CEED_Q_VLA], CeedScalar dXdx[2][2], 77 CeedScalar *detJ_ptr) { 78 CeedScalar dxdX[2][2]; 79 80 GradUnpack2(Q, i, 2, (CeedScalar *)dxdX_q, dxdX); 81 const CeedScalar detJ = dxdX[0][0] * dxdX[1][1] - dxdX[1][0] * dxdX[0][1]; 82 83 dXdx[0][0] = dxdX[1][1] / detJ; 84 dXdx[0][1] = -dxdX[0][1] / detJ; 85 dXdx[1][0] = -dxdX[1][0] / detJ; 86 dXdx[1][1] = dxdX[0][0] / detJ; 87 if (detJ_ptr) *detJ_ptr = detJ; 88 } 89 90 /** 91 * @brief Calculate face element's normal vector from dxdX 92 * 93 * Reference (parent) 2D coordinates: X 94 * Physical (current) 3D coordinates: x 95 * Change of coordinate matrix: 96 * dxdX_{i,j} = dx_i/dX_j (indicial notation) [3 * 2] 97 * Inverse change of coordinate matrix: 98 * dXdx_{i,j} = dX_i/dx_j (indicial notation) [2 * 3] 99 * 100 * (N1,N2,N3) is given by the cross product of the columns of dxdX_{i,j} 101 * 102 * detJb is the magnitude of (N1,N2,N3) 103 * 104 * Normal vector = (N1,N2,N3) / detJb 105 * 106 * @param[in] Q Number of quadrature points 107 * @param[in] i Current quadrature point 108 * @param[in] dxdX_q Mapping Jacobian (gradient of the coordinate space) 109 * @param[out] normal Inverse of mapping Jacobian at quadrature point i 110 * @param[out] detJ_ptr Determinate of the Jacobian, may be NULL is not desired 111 */ 112 CEED_QFUNCTION_HELPER void NormalVectorFromdxdX_3D(CeedInt Q, CeedInt i, const CeedScalar (*dxdX_q)[3][CEED_Q_VLA], CeedScalar normal[3], 113 CeedScalar *detJ_ptr) { 114 CeedScalar dxdX[3][2]; 115 116 GradUnpack2(Q, i, 3, (CeedScalar *)dxdX_q, dxdX); 117 // N1, N2, and N3 are given by the cross product of the columns of dxdX 118 normal[0] = dxdX[1][0] * dxdX[2][1] - dxdX[2][0] * dxdX[1][1]; 119 normal[1] = dxdX[2][0] * dxdX[0][1] - dxdX[0][0] * dxdX[2][1]; 120 normal[2] = dxdX[0][0] * dxdX[1][1] - dxdX[1][0] * dxdX[0][1]; 121 122 const CeedScalar detJ = Norm3(normal); 123 ScaleN(normal, 1 / detJ, 3); 124 if (detJ_ptr) *detJ_ptr = detJ; 125 } 126 127 /** 128 * This QFunction sets up the geometric factor required for integration when reference coordinates are in 1D and the physical coordinates are in 2D 129 * 130 * Reference (parent) 1D coordinates: X 131 * Physical (current) 2D coordinates: x 132 * Change of coordinate vector: 133 * N1 = dx_1/dX 134 * N2 = dx_2/dX 135 * 136 * detJb is the magnitude of (N1,N2) 137 * 138 * We require the determinant of the Jacobian to properly compute integrals of the form: int( u v ) 139 * 140 * Normal vector is given by the cross product of (N1,N2)/detJ and ẑ 141 * 142 * @param[in] Q Number of quadrature points 143 * @param[in] i Current quadrature point 144 * @param[in] dxdX_q Mapping Jacobian (gradient of the coordinate space) 145 * @param[out] normal Inverse of mapping Jacobian at quadrature point i 146 * @param[out] detJ_ptr Determinate of the Jacobian, may be NULL is not desired 147 */ 148 CEED_QFUNCTION_HELPER void NormalVectorFromdxdX_2D(CeedInt Q, CeedInt i, const CeedScalar (*dxdX_q)[CEED_Q_VLA], CeedScalar normal[2], 149 CeedScalar *detJ_ptr) { 150 normal[0] = dxdX_q[1][i]; 151 normal[1] = -dxdX_q[0][i]; 152 const CeedScalar detJb = Norm2(normal); 153 ScaleN(normal, 1 / detJb, 2); 154 if (detJ_ptr) *detJ_ptr = detJb; 155 } 156 157 /** 158 * @brief Calculate inverse of mapping Jacobian, (dxdX)^-1 159 * 160 * Reference (parent) 2D coordinates: X 161 * Physical (current) 3D coordinates: x 162 * Change of coordinate matrix: 163 * dxdX_{i,j} = dx_i/dX_j (indicial notation) [3 * 2] 164 * Inverse change of coordinate matrix: 165 * dXdx_{i,j} = dX_i/dx_j (indicial notation) [2 * 3] 166 * 167 * dXdx is calculated via Moore–Penrose inverse: 168 * 169 * dX_i/dx_j = (dxdX^T dxdX)^(-1) dxdX 170 * = (dx_l/dX_i * dx_l/dX_k)^(-1) dx_j/dX_k 171 * 172 * @param[in] Q Number of quadrature points 173 * @param[in] i Current quadrature point 174 * @param[in] dxdX_q Mapping Jacobian (gradient of the coordinate space) 175 * @param[out] dXdx Inverse of mapping Jacobian at quadrature point i 176 */ 177 CEED_QFUNCTION_HELPER void InvertBoundaryMappingJacobian_3D(CeedInt Q, CeedInt i, const CeedScalar (*dxdX_q)[3][CEED_Q_VLA], CeedScalar dXdx[2][3]) { 178 CeedScalar dxdX[3][2]; 179 GradUnpack2(Q, i, 3, (CeedScalar *)dxdX_q, dxdX); 180 181 // dxdX_k,j * dxdX_j,k 182 CeedScalar dxdXTdxdX[2][2] = {{0.}}; 183 for (CeedInt j = 0; j < 2; j++) { 184 for (CeedInt k = 0; k < 2; k++) { 185 for (CeedInt l = 0; l < 3; l++) dxdXTdxdX[j][k] += dxdX[l][j] * dxdX[l][k]; 186 } 187 } 188 189 const CeedScalar detdxdXTdxdX = dxdXTdxdX[0][0] * dxdXTdxdX[1][1] - dxdXTdxdX[1][0] * dxdXTdxdX[0][1]; 190 191 // Compute inverse of dxdXTdxdX 192 CeedScalar dxdXTdxdX_inv[2][2]; 193 dxdXTdxdX_inv[0][0] = dxdXTdxdX[1][1] / detdxdXTdxdX; 194 dxdXTdxdX_inv[0][1] = -dxdXTdxdX[0][1] / detdxdXTdxdX; 195 dxdXTdxdX_inv[1][0] = -dxdXTdxdX[1][0] / detdxdXTdxdX; 196 dxdXTdxdX_inv[1][1] = dxdXTdxdX[0][0] / detdxdXTdxdX; 197 198 // Compute dXdx from dxdXTdxdX^-1 and dxdX 199 for (CeedInt j = 0; j < 2; j++) { 200 for (CeedInt k = 0; k < 3; k++) { 201 dXdx[j][k] = 0; 202 for (CeedInt l = 0; l < 2; l++) dXdx[j][k] += dxdXTdxdX_inv[l][j] * dxdX[k][l]; 203 } 204 } 205 } 206