// SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
// SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause

/// @file
/// Geometric factors (2D) for HONEE
#include <ceed/types.h>
#include "setupgeo_helpers.h"
#include "utils.h"

// *****************************************************************************
// This QFunction sets up the geometric factors required for integration and coordinate transformations
//
// Reference (parent) coordinates: X
// Physical (current) coordinates: x
// Change of coordinate matrix: dxdX_{i,j} = x_{i,j} (indicial notation)
// Inverse of change of coordinate matrix: dXdx_{i,j} = (detJ^-1) * X_{i,j}
//
// All quadrature data is stored in 10 field vector of quadrature data.
//
// We require the determinant of the Jacobian to properly compute integrals of the form: int( v u )
//
// Determinant of Jacobian:
//   detJ = J11*J22 - J21*J12
//     Jij = Jacobian entry ij
//
// Stored: w detJ
//   in q_data[0]
//
// We require the transpose of the inverse of the Jacobian to properly compute integrals of the form: int( gradv u )
//
// Inverse of Jacobian:
//   dXdx_i,j = Aij / detJ
//   Aij = Adjugate ij
//
// Stored: Aij / detJ
//   in q_data[1:4] as
//   (detJ^-1) * [A11 A12]
//               [A21 A22]
// *****************************************************************************
CEED_QFUNCTION(Setup2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
  const CeedScalar(*J)[2][CEED_Q_VLA] = (const CeedScalar(*)[2][CEED_Q_VLA])in[0];
  const CeedScalar(*w)                = in[1];
  CeedScalar(*q_data)                 = out[0];

  CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
    CeedScalar dXdx[2][2], detJ;
    InvertMappingJacobian_2D(Q, i, J, dXdx, &detJ);
    const CeedScalar wdetJ = w[i] * detJ;

    StoredValuesPack(Q, i, 0, 1, &wdetJ, q_data);
    StoredValuesPack(Q, i, 1, 4, (const CeedScalar *)dXdx, q_data);
  }
  return 0;
}

// *****************************************************************************
// This QFunction sets up the geometric factor required for integration when reference coordinates are in 1D and the physical coordinates are in 2D
//
// Reference (parent) 1D coordinates: X
// Physical (current) 2D coordinates: x
// Change of coordinate vector:
//           J1 = dx_1/dX
//           J2 = dx_2/dX
//
// detJb is the magnitude of (J1,J2)
//
// All quadrature data is stored in 3 field vector of quadrature data.
//
// We require the determinant of the Jacobian to properly compute integrals of the form: int( u v )
//
// Stored: w detJb
//   in q_data_sur[0]
//
// Normal vector is given by the cross product of (J1,J2)/detJ and ẑ
//
// Stored: (J1,J2,0) x (0,0,1) / detJb
//   in q_data_sur[1:2] as
//   (detJb^-1) * [ J2 ]
//                [-J1 ]
// *****************************************************************************
CEED_QFUNCTION(SetupBoundary2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
  const CeedScalar(*J)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
  const CeedScalar(*w)             = in[1];
  CeedScalar(*q_data_sur)          = out[0];

  CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
    CeedScalar normal[2], detJb;
    NormalVectorFromdxdX_2D(Q, i, J, normal, &detJb);
    const CeedScalar wdetJ = w[i] * detJb;

    StoredValuesPack(Q, i, 0, 1, &wdetJ, q_data_sur);
    StoredValuesPack(Q, i, 1, 2, normal, q_data_sur);
  }
  return 0;
}

// *****************************************************************************
// This QFunction sets up the geometric factor required for integration when reference coordinates are 2D and the physical coordinates are in 3D
//
// In otherwords, when a 2D topology element is embedded in a 3D physical space.
//
// Reference (parent) 2D coordinates: X
// Physical (current) 3D coordinates: x
// Change of coordinate matrix:
//   dxdX_{i,j} = dx_i/dX_j (indicial notation) [3 * 2]
// Inverse change of coordinate matrix:
//   dXdx_{i,j} = dX_i/dx_j (indicial notation) [2 * 3]
//
// (J1,J2,J3) is given by the cross product of the columns of dxdX_{i,j}
//
// detJb is the magnitude of (J1,J2,J3)
//
// dXdx is calculated via Moore–Penrose inverse:
//
//   dX_i/dx_j = (dxdX^T dxdX)^(-1) dxdX
//             = (dx_l/dX_i * dx_l/dX_k)^(-1) dx_j/dX_k
//
// All quadrature data is stored in 10 field vector of quadrature data.
//
// We require the determinant of the Jacobian to properly compute integrals of
//   the form: int( u v )
//
// Stored: w detJb
//   in q_data_sur[0]
//
// Normal vector = (J1,J2,J3) / detJb
//
// Stored: (J1,J2,J3) / detJb
//
// Stored: dXdx_{i,j}
//   in q_data_sur[1:6] as
//    [dXdx_11 dXdx_12 dXdx_13]
//    [dXdx_21 dXdx_22 dXdx_23]
// *****************************************************************************
CEED_QFUNCTION(Setup2D_3Dcoords)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
  const CeedScalar(*J)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[0];
  const CeedScalar(*w)                = in[1];
  CeedScalar(*q_data_sur)             = out[0];

  CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
    CeedScalar detJb, normal[3], dXdx[2][3];

    NormalVectorFromdxdX_3D(Q, i, J, normal, &detJb);
    InvertBoundaryMappingJacobian_3D(Q, i, J, dXdx);
    const CeedScalar wdetJ = w[i] * detJb;

    StoredValuesPack(Q, i, 0, 1, &wdetJ, q_data_sur);
    StoredValuesPack(Q, i, 1, 6, (const CeedScalar *)dXdx, q_data_sur);
  }
  return 0;
}

/**
  @brief Compute geometric factors for integration, gradient transformations, and coordinate transformations on element faces.

  Reference (parent) 1D coordinates are given by `X` and physical (current) 2D coordinates are given by `x`.
  The change of coordinate matrix is given by`dxdX_{i,j} = dx_i/dX_j (indicial notation) [2 * 1]`.

  @param[in]   ctx  QFunction context, unused
  @param[in]   Q    Number of quadrature points
  @param[in]   in   Input arrays
                      - 0 - Jacobian of cell coordinates
                      - 1 - Jacobian of face coordinates
                      - 2 - quadrature weights
  @param[out]  out  Output array
                      - 0 - qdata, `w detNb`, `dXdx`, and `N`

  @return An error code: 0 - success, otherwise - failure
**/
CEED_QFUNCTION(Setup2DBoundaryGradient)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
  const CeedScalar(*J_cell)[2][CEED_Q_VLA] = (const CeedScalar(*)[2][CEED_Q_VLA])in[0];
  const CeedScalar(*J_face)[CEED_Q_VLA]    = (const CeedScalar(*)[CEED_Q_VLA])in[1];
  const CeedScalar(*w)                     = in[2];
  CeedScalar(*q_data_sur)                  = out[0];

  CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
    CeedScalar detJ_face, normal[2], dXdx[2][2];

    NormalVectorFromdxdX_2D(Q, i, J_face, normal, &detJ_face);
    const CeedScalar wdetJ = w[i] * detJ_face;
    InvertMappingJacobian_2D(Q, i, J_cell, dXdx, NULL);

    StoredValuesPack(Q, i, 0, 1, &wdetJ, q_data_sur);
    StoredValuesPack(Q, i, 1, 4, (CeedScalar *)dXdx, q_data_sur);
    StoredValuesPack(Q, i, 5, 2, normal, q_data_sur);
  }
  return CEED_ERROR_SUCCESS;
}
