xref: /honee/qfunctions/setupgeo_helpers.h (revision 59583c89b45b6b157f78238b346d74da78c93906)
1ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
31a74fa30SJames Wright 
41a74fa30SJames Wright /// @file
5ea615d4cSJames Wright /// Geometric factors (3D) for HONEE
6c7ece6efSJeremy L Thompson #pragma once
71a74fa30SJames Wright 
83e17a7a1SJames Wright #include <ceed/types.h>
91a74fa30SJames Wright #include "utils.h"
101a74fa30SJames Wright 
111a74fa30SJames Wright /**
121a74fa30SJames Wright  * @brief Calculate dXdx from dxdX for 3D elements
131a74fa30SJames Wright  *
141a74fa30SJames Wright  * Reference (parent) coordinates: X
151a74fa30SJames Wright  * Physical (current) coordinates: x
161a74fa30SJames Wright  * Change of coordinate matrix: dxdX_{i,j} = x_{i,j} (indicial notation)
171a74fa30SJames Wright  * Inverse of change of coordinate matrix: dXdx_{i,j} = (detJ^-1) * X_{i,j}
181a74fa30SJames Wright  *
191a74fa30SJames Wright  * @param[in]  Q        Number of quadrature points
201a74fa30SJames Wright  * @param[in]  i        Current quadrature point
211a74fa30SJames Wright  * @param[in]  dxdX_q   Mapping Jacobian (gradient of the coordinate space)
221a74fa30SJames Wright  * @param[out] dXdx     Inverse of mapping Jacobian at quadrature point i
231a74fa30SJames Wright  * @param[out] detJ_ptr Determinate of the Jacobian, may be NULL is not desired
241a74fa30SJames Wright  */
InvertMappingJacobian_3D(CeedInt Q,CeedInt i,const CeedScalar (* dxdX_q)[3][CEED_Q_VLA],CeedScalar dXdx[3][3],CeedScalar * detJ_ptr)251a74fa30SJames Wright CEED_QFUNCTION_HELPER void InvertMappingJacobian_3D(CeedInt Q, CeedInt i, const CeedScalar (*dxdX_q)[3][CEED_Q_VLA], CeedScalar dXdx[3][3],
261a74fa30SJames Wright                                                     CeedScalar *detJ_ptr) {
2783c0b726SJames Wright   CeedScalar dxdX[3][3];
281a74fa30SJames Wright 
29*22440147SJames Wright   GradUnpack3D(Q, i, 3, (CeedScalar *)dxdX_q, dxdX);
30d8667e38SJames Wright   MatInv3(dxdX, dXdx, detJ_ptr);
311a74fa30SJames Wright }
321a74fa30SJames Wright 
331a74fa30SJames Wright /**
3483c0b726SJames Wright  * @brief Calculate dXdx from dxdX for 2D elements
35baadde1fSJames Wright  *
36baadde1fSJames Wright  * Reference (parent) coordinates: X
37baadde1fSJames Wright  * Physical (current) coordinates: x
38baadde1fSJames Wright  * Change of coordinate matrix: dxdX_{i,j} = x_{i,j} (indicial notation)
39baadde1fSJames Wright  * Inverse of change of coordinate matrix: dXdx_{i,j} = (detJ^-1) * X_{i,j}
40baadde1fSJames Wright  *
41baadde1fSJames Wright  * @param[in]  Q        Number of quadrature points
42baadde1fSJames Wright  * @param[in]  i        Current quadrature point
43baadde1fSJames Wright  * @param[in]  dxdX_q   Mapping Jacobian (gradient of the coordinate space)
44baadde1fSJames Wright  * @param[out] dXdx     Inverse of mapping Jacobian at quadrature point i
45baadde1fSJames Wright  * @param[out] detJ_ptr Determinate of the Jacobian, may be NULL is not desired
46baadde1fSJames Wright  */
InvertMappingJacobian_2D(CeedInt Q,CeedInt i,const CeedScalar (* dxdX_q)[2][CEED_Q_VLA],CeedScalar dXdx[2][2],CeedScalar * detJ_ptr)47baadde1fSJames Wright CEED_QFUNCTION_HELPER void InvertMappingJacobian_2D(CeedInt Q, CeedInt i, const CeedScalar (*dxdX_q)[2][CEED_Q_VLA], CeedScalar dXdx[2][2],
48baadde1fSJames Wright                                                     CeedScalar *detJ_ptr) {
4983c0b726SJames Wright   CeedScalar dxdX[2][2];
50baadde1fSJames Wright 
51*22440147SJames Wright   GradUnpack2D(Q, i, 2, (CeedScalar *)dxdX_q, dxdX);
52d8667e38SJames Wright   MatInv2(dxdX, dXdx, detJ_ptr);
53baadde1fSJames Wright }
54baadde1fSJames Wright 
55baadde1fSJames Wright /**
561a74fa30SJames Wright  * @brief Calculate face element's normal vector from dxdX
571a74fa30SJames Wright  *
581a74fa30SJames Wright  * Reference (parent) 2D coordinates: X
591a74fa30SJames Wright  * Physical (current) 3D coordinates: x
601a74fa30SJames Wright  * Change of coordinate matrix:
611a74fa30SJames Wright  *   dxdX_{i,j} = dx_i/dX_j (indicial notation) [3 * 2]
621a74fa30SJames Wright  * Inverse change of coordinate matrix:
631a74fa30SJames Wright  *   dXdx_{i,j} = dX_i/dx_j (indicial notation) [2 * 3]
641a74fa30SJames Wright  *
6583c0b726SJames Wright  * (N1,N2,N3) is given by the cross product of the columns of dxdX_{i,j}
661a74fa30SJames Wright  *
6783c0b726SJames Wright  * detJb is the magnitude of (N1,N2,N3)
681a74fa30SJames Wright  *
6983c0b726SJames Wright  * Normal vector = (N1,N2,N3) / detJb
701a74fa30SJames Wright  *
711a74fa30SJames Wright  * @param[in]  Q        Number of quadrature points
721a74fa30SJames Wright  * @param[in]  i        Current quadrature point
731a74fa30SJames Wright  * @param[in]  dxdX_q   Mapping Jacobian (gradient of the coordinate space)
741a74fa30SJames Wright  * @param[out] normal   Inverse of mapping Jacobian at quadrature point i
751a74fa30SJames Wright  * @param[out] detJ_ptr Determinate of the Jacobian, may be NULL is not desired
761a74fa30SJames Wright  */
NormalVectorFromdxdX_3D(CeedInt Q,CeedInt i,const CeedScalar (* dxdX_q)[3][CEED_Q_VLA],CeedScalar normal[3],CeedScalar * detJ_ptr)77ff20bea9SJames Wright CEED_QFUNCTION_HELPER void NormalVectorFromdxdX_3D(CeedInt Q, CeedInt i, const CeedScalar (*dxdX_q)[3][CEED_Q_VLA], CeedScalar normal[3],
781a74fa30SJames Wright                                                    CeedScalar *detJ_ptr) {
7983c0b726SJames Wright   CeedScalar dxdX[3][2];
801a74fa30SJames Wright 
81*22440147SJames Wright   GradUnpack2D(Q, i, 3, (CeedScalar *)dxdX_q, dxdX);
8283c0b726SJames Wright   // N1, N2, and N3 are given by the cross product of the columns of dxdX
8383c0b726SJames Wright   normal[0] = dxdX[1][0] * dxdX[2][1] - dxdX[2][0] * dxdX[1][1];
8483c0b726SJames Wright   normal[1] = dxdX[2][0] * dxdX[0][1] - dxdX[0][0] * dxdX[2][1];
8583c0b726SJames Wright   normal[2] = dxdX[0][0] * dxdX[1][1] - dxdX[1][0] * dxdX[0][1];
861a74fa30SJames Wright 
8783c0b726SJames Wright   const CeedScalar detJ = Norm3(normal);
8883c0b726SJames Wright   ScaleN(normal, 1 / detJ, 3);
891a74fa30SJames Wright   if (detJ_ptr) *detJ_ptr = detJ;
901a74fa30SJames Wright }
911a74fa30SJames Wright 
921a74fa30SJames Wright /**
932c512a7bSJames Wright  * This QFunction sets up the geometric factor required for integration when reference coordinates are in 1D and the physical coordinates are in 2D
942c512a7bSJames Wright  *
952c512a7bSJames Wright  * Reference (parent) 1D coordinates: X
962c512a7bSJames Wright  * Physical (current) 2D coordinates: x
972c512a7bSJames Wright  * Change of coordinate vector:
9883c0b726SJames Wright  *           N1 = dx_1/dX
9983c0b726SJames Wright  *           N2 = dx_2/dX
1002c512a7bSJames Wright  *
10183c0b726SJames Wright  * detJb is the magnitude of (N1,N2)
1022c512a7bSJames Wright  *
1032c512a7bSJames Wright  * We require the determinant of the Jacobian to properly compute integrals of the form: int( u v )
1042c512a7bSJames Wright  *
10583c0b726SJames Wright  * Normal vector is given by the cross product of (N1,N2)/detJ and ẑ
1062c512a7bSJames Wright  *
1072c512a7bSJames Wright  * @param[in]  Q        Number of quadrature points
1082c512a7bSJames Wright  * @param[in]  i        Current quadrature point
1092c512a7bSJames Wright  * @param[in]  dxdX_q   Mapping Jacobian (gradient of the coordinate space)
1102c512a7bSJames Wright  * @param[out] normal   Inverse of mapping Jacobian at quadrature point i
1112c512a7bSJames Wright  * @param[out] detJ_ptr Determinate of the Jacobian, may be NULL is not desired
1122c512a7bSJames Wright  */
NormalVectorFromdxdX_2D(CeedInt Q,CeedInt i,const CeedScalar (* dxdX_q)[CEED_Q_VLA],CeedScalar normal[2],CeedScalar * detJ_ptr)1132c512a7bSJames Wright CEED_QFUNCTION_HELPER void NormalVectorFromdxdX_2D(CeedInt Q, CeedInt i, const CeedScalar (*dxdX_q)[CEED_Q_VLA], CeedScalar normal[2],
1142c512a7bSJames Wright                                                    CeedScalar *detJ_ptr) {
11583c0b726SJames Wright   normal[0]              = dxdX_q[1][i];
11683c0b726SJames Wright   normal[1]              = -dxdX_q[0][i];
11783c0b726SJames Wright   const CeedScalar detJb = Norm2(normal);
11883c0b726SJames Wright   ScaleN(normal, 1 / detJb, 2);
1192c512a7bSJames Wright   if (detJ_ptr) *detJ_ptr = detJb;
1202c512a7bSJames Wright }
1212c512a7bSJames Wright 
1222c512a7bSJames Wright /**
1231a74fa30SJames Wright  * @brief Calculate inverse of mapping Jacobian, (dxdX)^-1
1241a74fa30SJames Wright  *
1251a74fa30SJames Wright  * Reference (parent) 2D coordinates: X
1261a74fa30SJames Wright  * Physical (current) 3D coordinates: x
1271a74fa30SJames Wright  * Change of coordinate matrix:
1281a74fa30SJames Wright  *   dxdX_{i,j} = dx_i/dX_j (indicial notation) [3 * 2]
1291a74fa30SJames Wright  * Inverse change of coordinate matrix:
1301a74fa30SJames Wright  *   dXdx_{i,j} = dX_i/dx_j (indicial notation) [2 * 3]
1311a74fa30SJames Wright  *
1321a74fa30SJames Wright  * dXdx is calculated via Moore–Penrose inverse:
1331a74fa30SJames Wright  *
1341a74fa30SJames Wright  *   dX_i/dx_j = (dxdX^T dxdX)^(-1) dxdX
1351a74fa30SJames Wright  *             = (dx_l/dX_i * dx_l/dX_k)^(-1) dx_j/dX_k
1361a74fa30SJames Wright  *
1371a74fa30SJames Wright  * @param[in]  Q      Number of quadrature points
1381a74fa30SJames Wright  * @param[in]  i      Current quadrature point
1391a74fa30SJames Wright  * @param[in]  dxdX_q Mapping Jacobian (gradient of the coordinate space)
1401a74fa30SJames Wright  * @param[out] dXdx   Inverse of mapping Jacobian at quadrature point i
1411a74fa30SJames Wright  */
InvertBoundaryMappingJacobian_3D(CeedInt Q,CeedInt i,const CeedScalar (* dxdX_q)[3][CEED_Q_VLA],CeedScalar dXdx[2][3])1421a74fa30SJames Wright CEED_QFUNCTION_HELPER void InvertBoundaryMappingJacobian_3D(CeedInt Q, CeedInt i, const CeedScalar (*dxdX_q)[3][CEED_Q_VLA], CeedScalar dXdx[2][3]) {
14383c0b726SJames Wright   CeedScalar dxdX[3][2];
144*22440147SJames Wright   GradUnpack2D(Q, i, 3, (CeedScalar *)dxdX_q, dxdX);
1451a74fa30SJames Wright 
1461a74fa30SJames Wright   // dxdX_k,j * dxdX_j,k
1471a74fa30SJames Wright   CeedScalar dxdXTdxdX[2][2] = {{0.}};
1481a74fa30SJames Wright   for (CeedInt j = 0; j < 2; j++) {
1491a74fa30SJames Wright     for (CeedInt k = 0; k < 2; k++) {
1501a74fa30SJames Wright       for (CeedInt l = 0; l < 3; l++) dxdXTdxdX[j][k] += dxdX[l][j] * dxdX[l][k];
1511a74fa30SJames Wright     }
1521a74fa30SJames Wright   }
1531a74fa30SJames Wright 
1541a74fa30SJames Wright   const CeedScalar detdxdXTdxdX = dxdXTdxdX[0][0] * dxdXTdxdX[1][1] - dxdXTdxdX[1][0] * dxdXTdxdX[0][1];
1551a74fa30SJames Wright 
1561a74fa30SJames Wright   // Compute inverse of dxdXTdxdX
1571a74fa30SJames Wright   CeedScalar dxdXTdxdX_inv[2][2];
1581a74fa30SJames Wright   dxdXTdxdX_inv[0][0] = dxdXTdxdX[1][1] / detdxdXTdxdX;
1591a74fa30SJames Wright   dxdXTdxdX_inv[0][1] = -dxdXTdxdX[0][1] / detdxdXTdxdX;
1601a74fa30SJames Wright   dxdXTdxdX_inv[1][0] = -dxdXTdxdX[1][0] / detdxdXTdxdX;
1611a74fa30SJames Wright   dxdXTdxdX_inv[1][1] = dxdXTdxdX[0][0] / detdxdXTdxdX;
1621a74fa30SJames Wright 
1631a74fa30SJames Wright   // Compute dXdx from dxdXTdxdX^-1 and dxdX
1641a74fa30SJames Wright   for (CeedInt j = 0; j < 2; j++) {
1651a74fa30SJames Wright     for (CeedInt k = 0; k < 3; k++) {
1661a74fa30SJames Wright       dXdx[j][k] = 0;
1671a74fa30SJames Wright       for (CeedInt l = 0; l < 2; l++) dXdx[j][k] += dxdXTdxdX_inv[l][j] * dxdX[k][l];
1681a74fa30SJames Wright     }
1691a74fa30SJames Wright   }
1701a74fa30SJames Wright }
171