15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3d7b241e6Sjeremylt // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5d7b241e6Sjeremylt // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7d7b241e6Sjeremylt 83d576824SJeremy L Thompson #include <ceed-impl.h> 949aac155SJeremy L Thompson #include <ceed.h> 102b730f8bSJeremy L Thompson #include <ceed/backend.h> 11d7b241e6Sjeremylt #include <math.h> 123d576824SJeremy L Thompson #include <stdbool.h> 13d7b241e6Sjeremylt #include <stdio.h> 14d7b241e6Sjeremylt #include <string.h> 15d7b241e6Sjeremylt 167a982d89SJeremy L. Thompson /// @file 177a982d89SJeremy L. Thompson /// Implementation of CeedBasis interfaces 187a982d89SJeremy L. Thompson 19d7b241e6Sjeremylt /// @cond DOXYGEN_SKIP 20356036faSJeremy L Thompson static struct CeedBasis_private ceed_basis_none; 21d7b241e6Sjeremylt /// @endcond 22d7b241e6Sjeremylt 237a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser 247a982d89SJeremy L. Thompson /// @{ 257a982d89SJeremy L. Thompson 26ca94c3ddSJeremy L Thompson /// Argument for @ref CeedOperatorSetField() indicating that the field does not require a `CeedBasis` 27356036faSJeremy L Thompson const CeedBasis CEED_BASIS_NONE = &ceed_basis_none; 28356036faSJeremy L Thompson 297a982d89SJeremy L. Thompson /// @} 307a982d89SJeremy L. Thompson 317a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 327a982d89SJeremy L. Thompson /// CeedBasis Library Internal Functions 337a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 347a982d89SJeremy L. Thompson /// @addtogroup CeedBasisDeveloper 357a982d89SJeremy L. Thompson /// @{ 367a982d89SJeremy L. Thompson 377a982d89SJeremy L. Thompson /** 383778dbaaSJeremy L Thompson @brief Compute Chebyshev polynomial values at a point 393778dbaaSJeremy L Thompson 403778dbaaSJeremy L Thompson @param[in] x Coordinate to evaluate Chebyshev polynomials at 41ca94c3ddSJeremy L Thompson @param[in] n Number of Chebyshev polynomials to evaluate, `n >= 2` 423778dbaaSJeremy L Thompson @param[out] chebyshev_x Array of Chebyshev polynomial values 433778dbaaSJeremy L Thompson 443778dbaaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 453778dbaaSJeremy L Thompson 463778dbaaSJeremy L Thompson @ref Developer 473778dbaaSJeremy L Thompson **/ 483778dbaaSJeremy L Thompson static int CeedChebyshevPolynomialsAtPoint(CeedScalar x, CeedInt n, CeedScalar *chebyshev_x) { 493778dbaaSJeremy L Thompson chebyshev_x[0] = 1.0; 503778dbaaSJeremy L Thompson chebyshev_x[1] = 2 * x; 513778dbaaSJeremy L Thompson for (CeedInt i = 2; i < n; i++) chebyshev_x[i] = 2 * x * chebyshev_x[i - 1] - chebyshev_x[i - 2]; 523778dbaaSJeremy L Thompson return CEED_ERROR_SUCCESS; 533778dbaaSJeremy L Thompson } 543778dbaaSJeremy L Thompson 553778dbaaSJeremy L Thompson /** 563778dbaaSJeremy L Thompson @brief Compute values of the derivative of Chebyshev polynomials at a point 573778dbaaSJeremy L Thompson 583778dbaaSJeremy L Thompson @param[in] x Coordinate to evaluate derivative of Chebyshev polynomials at 59ca94c3ddSJeremy L Thompson @param[in] n Number of Chebyshev polynomials to evaluate, `n >= 2` 606cec60aaSJed Brown @param[out] chebyshev_dx Array of Chebyshev polynomial derivative values 613778dbaaSJeremy L Thompson 623778dbaaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 633778dbaaSJeremy L Thompson 643778dbaaSJeremy L Thompson @ref Developer 653778dbaaSJeremy L Thompson **/ 663778dbaaSJeremy L Thompson static int CeedChebyshevDerivativeAtPoint(CeedScalar x, CeedInt n, CeedScalar *chebyshev_dx) { 673778dbaaSJeremy L Thompson CeedScalar chebyshev_x[3]; 683778dbaaSJeremy L Thompson 693778dbaaSJeremy L Thompson chebyshev_x[1] = 1.0; 703778dbaaSJeremy L Thompson chebyshev_x[2] = 2 * x; 713778dbaaSJeremy L Thompson chebyshev_dx[0] = 0.0; 723778dbaaSJeremy L Thompson chebyshev_dx[1] = 2.0; 733778dbaaSJeremy L Thompson for (CeedInt i = 2; i < n; i++) { 743778dbaaSJeremy L Thompson chebyshev_x[0] = chebyshev_x[1]; 753778dbaaSJeremy L Thompson chebyshev_x[1] = chebyshev_x[2]; 763778dbaaSJeremy L Thompson chebyshev_x[2] = 2 * x * chebyshev_x[1] - chebyshev_x[0]; 773778dbaaSJeremy L Thompson chebyshev_dx[i] = 2 * x * chebyshev_dx[i - 1] + 2 * chebyshev_x[1] - chebyshev_dx[i - 2]; 783778dbaaSJeremy L Thompson } 793778dbaaSJeremy L Thompson return CEED_ERROR_SUCCESS; 803778dbaaSJeremy L Thompson } 813778dbaaSJeremy L Thompson 823778dbaaSJeremy L Thompson /** 83ca94c3ddSJeremy L Thompson @brief Compute Householder reflection. 847a982d89SJeremy L. Thompson 85ca94c3ddSJeremy L Thompson Computes \f$A = (I - b v v^T) A\f$, where \f$A\f$ is an \f$m \times n\f$ matrix indexed as `A[i*row + j*col]`. 867a982d89SJeremy L. Thompson 877a982d89SJeremy L. Thompson @param[in,out] A Matrix to apply Householder reflection to, in place 88ea61e9acSJeremy L Thompson @param[in] v Householder vector 89ea61e9acSJeremy L Thompson @param[in] b Scaling factor 90ca94c3ddSJeremy L Thompson @param[in] m Number of rows in `A` 91ca94c3ddSJeremy L Thompson @param[in] n Number of columns in `A` 92ea61e9acSJeremy L Thompson @param[in] row Row stride 93ea61e9acSJeremy L Thompson @param[in] col Col stride 947a982d89SJeremy L. Thompson 957a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 967a982d89SJeremy L. Thompson 977a982d89SJeremy L. Thompson @ref Developer 987a982d89SJeremy L. Thompson **/ 992b730f8bSJeremy L Thompson static int CeedHouseholderReflect(CeedScalar *A, const CeedScalar *v, CeedScalar b, CeedInt m, CeedInt n, CeedInt row, CeedInt col) { 1007a982d89SJeremy L. Thompson for (CeedInt j = 0; j < n; j++) { 1017a982d89SJeremy L. Thompson CeedScalar w = A[0 * row + j * col]; 1021c66c397SJeremy L Thompson 1032b730f8bSJeremy L Thompson for (CeedInt i = 1; i < m; i++) w += v[i] * A[i * row + j * col]; 1047a982d89SJeremy L. Thompson A[0 * row + j * col] -= b * w; 1052b730f8bSJeremy L Thompson for (CeedInt i = 1; i < m; i++) A[i * row + j * col] -= b * w * v[i]; 1067a982d89SJeremy L. Thompson } 107e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1087a982d89SJeremy L. Thompson } 1097a982d89SJeremy L. Thompson 1107a982d89SJeremy L. Thompson /** 1117a982d89SJeremy L. Thompson @brief Compute Givens rotation 1127a982d89SJeremy L. Thompson 113ca94c3ddSJeremy L Thompson Computes \f$A = G A\f$ (or \f$G^T A\f$ in transpose mode), where \f$A\f$ is an \f$m \times n\f$ matrix indexed as `A[i*n + j*m]`. 1147a982d89SJeremy L. Thompson 1157a982d89SJeremy L. Thompson @param[in,out] A Row major matrix to apply Givens rotation to, in place 116ea61e9acSJeremy L Thompson @param[in] c Cosine factor 117ea61e9acSJeremy L Thompson @param[in] s Sine factor 118ca94c3ddSJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to rotate the basis counter-clockwise, which has the effect of rotating columns of `A` clockwise; 1194cc79fe7SJed Brown @ref CEED_TRANSPOSE for the opposite rotation 120ea61e9acSJeremy L Thompson @param[in] i First row/column to apply rotation 121ea61e9acSJeremy L Thompson @param[in] k Second row/column to apply rotation 122ca94c3ddSJeremy L Thompson @param[in] m Number of rows in `A` 123ca94c3ddSJeremy L Thompson @param[in] n Number of columns in `A` 1247a982d89SJeremy L. Thompson 1257a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1267a982d89SJeremy L. Thompson 1277a982d89SJeremy L. Thompson @ref Developer 1287a982d89SJeremy L. Thompson **/ 1292b730f8bSJeremy L Thompson static int CeedGivensRotation(CeedScalar *A, CeedScalar c, CeedScalar s, CeedTransposeMode t_mode, CeedInt i, CeedInt k, CeedInt m, CeedInt n) { 130d1d35e2fSjeremylt CeedInt stride_j = 1, stride_ik = m, num_its = n; 1311c66c397SJeremy L Thompson 132d1d35e2fSjeremylt if (t_mode == CEED_NOTRANSPOSE) { 1332b730f8bSJeremy L Thompson stride_j = n; 1342b730f8bSJeremy L Thompson stride_ik = 1; 1352b730f8bSJeremy L Thompson num_its = m; 1367a982d89SJeremy L. Thompson } 1377a982d89SJeremy L. Thompson 1387a982d89SJeremy L. Thompson // Apply rotation 139d1d35e2fSjeremylt for (CeedInt j = 0; j < num_its; j++) { 140d1d35e2fSjeremylt CeedScalar tau1 = A[i * stride_ik + j * stride_j], tau2 = A[k * stride_ik + j * stride_j]; 1411c66c397SJeremy L Thompson 142d1d35e2fSjeremylt A[i * stride_ik + j * stride_j] = c * tau1 - s * tau2; 143d1d35e2fSjeremylt A[k * stride_ik + j * stride_j] = s * tau1 + c * tau2; 1447a982d89SJeremy L. Thompson } 145e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1467a982d89SJeremy L. Thompson } 1477a982d89SJeremy L. Thompson 1487a982d89SJeremy L. Thompson /** 149ca94c3ddSJeremy L Thompson @brief View an array stored in a `CeedBasis` 1507a982d89SJeremy L. Thompson 1510a0da059Sjeremylt @param[in] name Name of array 152d1d35e2fSjeremylt @param[in] fp_fmt Printing format 1530a0da059Sjeremylt @param[in] m Number of rows in array 1540a0da059Sjeremylt @param[in] n Number of columns in array 1550a0da059Sjeremylt @param[in] a Array to be viewed 156ca94c3ddSJeremy L Thompson @param[in] stream Stream to view to, e.g., `stdout` 1577a982d89SJeremy L. Thompson 1587a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1597a982d89SJeremy L. Thompson 1607a982d89SJeremy L. Thompson @ref Developer 1617a982d89SJeremy L. Thompson **/ 1622b730f8bSJeremy L Thompson static int CeedScalarView(const char *name, const char *fp_fmt, CeedInt m, CeedInt n, const CeedScalar *a, FILE *stream) { 163edf04919SJeremy L Thompson if (m > 1) { 164edf04919SJeremy L Thompson fprintf(stream, " %s:\n", name); 165edf04919SJeremy L Thompson } else { 166edf04919SJeremy L Thompson char padded_name[12]; 167edf04919SJeremy L Thompson 168edf04919SJeremy L Thompson snprintf(padded_name, 11, "%s:", name); 169edf04919SJeremy L Thompson fprintf(stream, " %-10s", padded_name); 170edf04919SJeremy L Thompson } 17192ae7e47SJeremy L Thompson for (CeedInt i = 0; i < m; i++) { 172edf04919SJeremy L Thompson if (m > 1) fprintf(stream, " [%" CeedInt_FMT "]", i); 1732b730f8bSJeremy L Thompson for (CeedInt j = 0; j < n; j++) fprintf(stream, fp_fmt, fabs(a[i * n + j]) > 1E-14 ? a[i * n + j] : 0); 1747a982d89SJeremy L. Thompson fputs("\n", stream); 1757a982d89SJeremy L. Thompson } 176e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1777a982d89SJeremy L. Thompson } 1787a982d89SJeremy L. Thompson 179a76a04e7SJeremy L Thompson /** 180ea61e9acSJeremy L Thompson @brief Create the interpolation and gradient matrices for projection from the nodes of `basis_from` to the nodes of `basis_to`. 181ba59ac12SSebastian Grimberg 18215ad3917SSebastian Grimberg The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization. 183ca94c3ddSJeremy L Thompson The gradient is given by `grad_project = interp_to^+ * grad_from`, and is only computed for \f$H^1\f$ spaces otherwise it should not be used. 18415ad3917SSebastian Grimberg 185ba59ac12SSebastian Grimberg Note: `basis_from` and `basis_to` must have compatible quadrature spaces. 186a76a04e7SJeremy L Thompson 187ca94c3ddSJeremy L Thompson @param[in] basis_from `CeedBasis` to project from 188ca94c3ddSJeremy L Thompson @param[in] basis_to `CeedBasis` to project to 189ca94c3ddSJeremy L Thompson @param[out] interp_project Address of the variable where the newly created interpolation matrix will be stored 190ca94c3ddSJeremy L Thompson @param[out] grad_project Address of the variable where the newly created gradient matrix will be stored 191a76a04e7SJeremy L Thompson 192a76a04e7SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 193a76a04e7SJeremy L Thompson 194a76a04e7SJeremy L Thompson @ref Developer 195a76a04e7SJeremy L Thompson **/ 1962b730f8bSJeremy L Thompson static int CeedBasisCreateProjectionMatrices(CeedBasis basis_from, CeedBasis basis_to, CeedScalar **interp_project, CeedScalar **grad_project) { 197a76a04e7SJeremy L Thompson Ceed ceed; 1981c66c397SJeremy L Thompson bool is_tensor_to, is_tensor_from; 1991c66c397SJeremy L Thompson CeedInt Q, Q_to, Q_from, P_to, P_from; 2001c66c397SJeremy L Thompson 2012b730f8bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis_to, &ceed)); 202a76a04e7SJeremy L Thompson 203a76a04e7SJeremy L Thompson // Check for compatible quadrature spaces 2042b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_to, &Q_to)); 2052b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_from, &Q_from)); 2066574a04fSJeremy L Thompson CeedCheck(Q_to == Q_from, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces"); 2071c66c397SJeremy L Thompson Q = Q_to; 208a76a04e7SJeremy L Thompson 20914556e63SJeremy L Thompson // Check for matching tensor or non-tensor 2102b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to)); 2112b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from)); 2126574a04fSJeremy L Thompson CeedCheck(is_tensor_to == is_tensor_from, ceed, CEED_ERROR_MINOR, "Bases must both be tensor or non-tensor"); 2136574a04fSJeremy L Thompson if (is_tensor_to) { 2142b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_to)); 2152b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_from)); 2162b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis_from, &Q)); 2176574a04fSJeremy L Thompson } else { 2182b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_to, &P_to)); 2192b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_from, &P_from)); 220a76a04e7SJeremy L Thompson } 221a76a04e7SJeremy L Thompson 22215ad3917SSebastian Grimberg // Check for matching FE space 22315ad3917SSebastian Grimberg CeedFESpace fe_space_to, fe_space_from; 22415ad3917SSebastian Grimberg CeedCall(CeedBasisGetFESpace(basis_to, &fe_space_to)); 22515ad3917SSebastian Grimberg CeedCall(CeedBasisGetFESpace(basis_from, &fe_space_from)); 2266574a04fSJeremy L Thompson CeedCheck(fe_space_to == fe_space_from, ceed, CEED_ERROR_MINOR, "Bases must both be the same FE space type"); 22715ad3917SSebastian Grimberg 22814556e63SJeremy L Thompson // Get source matrices 22915ad3917SSebastian Grimberg CeedInt dim, q_comp = 1; 2302247a93fSRezgar Shakeri CeedScalar *interp_to_inv, *interp_from; 2311c66c397SJeremy L Thompson const CeedScalar *interp_to_source = NULL, *interp_from_source = NULL, *grad_from_source = NULL; 2321c66c397SJeremy L Thompson 2332b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_to, &dim)); 234a76a04e7SJeremy L Thompson if (is_tensor_to) { 2352b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis_to, &interp_to_source)); 2362b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis_from, &interp_from_source)); 237a76a04e7SJeremy L Thompson } else { 23815ad3917SSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis_from, CEED_EVAL_INTERP, &q_comp)); 2392b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis_to, &interp_to_source)); 2402b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis_from, &interp_from_source)); 24115ad3917SSebastian Grimberg } 24215ad3917SSebastian Grimberg CeedCall(CeedMalloc(Q * P_from * q_comp, &interp_from)); 24315ad3917SSebastian Grimberg CeedCall(CeedCalloc(P_to * P_from, interp_project)); 24415ad3917SSebastian Grimberg 24515ad3917SSebastian Grimberg // `grad_project = interp_to^+ * grad_from` is computed for the H^1 space case so the 246de05fbb2SSebastian Grimberg // projection basis will have a gradient operation (allocated even if not H^1 for the 247de05fbb2SSebastian Grimberg // basis construction later on) 24815ad3917SSebastian Grimberg if (fe_space_to == CEED_FE_SPACE_H1) { 24915ad3917SSebastian Grimberg if (is_tensor_to) { 25015ad3917SSebastian Grimberg CeedCall(CeedBasisGetGrad1D(basis_from, &grad_from_source)); 25115ad3917SSebastian Grimberg } else { 2522b730f8bSJeremy L Thompson CeedCall(CeedBasisGetGrad(basis_from, &grad_from_source)); 253a76a04e7SJeremy L Thompson } 254de05fbb2SSebastian Grimberg } 25515ad3917SSebastian Grimberg CeedCall(CeedCalloc(P_to * P_from * (is_tensor_to ? 1 : dim), grad_project)); 25615ad3917SSebastian Grimberg 2572247a93fSRezgar Shakeri // Compute interp_to^+, pseudoinverse of interp_to 2582247a93fSRezgar Shakeri CeedCall(CeedCalloc(Q * q_comp * P_to, &interp_to_inv)); 2591203703bSJeremy L Thompson CeedCall(CeedMatrixPseudoinverse(ceed, interp_to_source, Q * q_comp, P_to, interp_to_inv)); 26014556e63SJeremy L Thompson // Build matrices 26115ad3917SSebastian Grimberg CeedInt num_matrices = 1 + (fe_space_to == CEED_FE_SPACE_H1) * (is_tensor_to ? 1 : dim); 26214556e63SJeremy L Thompson CeedScalar *input_from[num_matrices], *output_project[num_matrices]; 2631c66c397SJeremy L Thompson 26414556e63SJeremy L Thompson input_from[0] = (CeedScalar *)interp_from_source; 26514556e63SJeremy L Thompson output_project[0] = *interp_project; 26614556e63SJeremy L Thompson for (CeedInt m = 1; m < num_matrices; m++) { 26714556e63SJeremy L Thompson input_from[m] = (CeedScalar *)&grad_from_source[(m - 1) * Q * P_from]; 26802af4036SJeremy L Thompson output_project[m] = &((*grad_project)[(m - 1) * P_to * P_from]); 26914556e63SJeremy L Thompson } 27014556e63SJeremy L Thompson for (CeedInt m = 0; m < num_matrices; m++) { 2712247a93fSRezgar Shakeri // output_project = interp_to^+ * interp_from 27215ad3917SSebastian Grimberg memcpy(interp_from, input_from[m], Q * P_from * q_comp * sizeof(input_from[m][0])); 2732247a93fSRezgar Shakeri CeedCall(CeedMatrixMatrixMultiply(ceed, interp_to_inv, input_from[m], output_project[m], P_to, P_from, Q * q_comp)); 2742247a93fSRezgar Shakeri // Round zero to machine precision 2752247a93fSRezgar Shakeri for (CeedInt i = 0; i < P_to * P_from; i++) { 2762247a93fSRezgar Shakeri if (fabs(output_project[m][i]) < 10 * CEED_EPSILON) output_project[m][i] = 0.0; 277a76a04e7SJeremy L Thompson } 27814556e63SJeremy L Thompson } 27914556e63SJeremy L Thompson 28014556e63SJeremy L Thompson // Cleanup 2812247a93fSRezgar Shakeri CeedCall(CeedFree(&interp_to_inv)); 2822b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_from)); 283a76a04e7SJeremy L Thompson return CEED_ERROR_SUCCESS; 284a76a04e7SJeremy L Thompson } 285a76a04e7SJeremy L Thompson 2867a982d89SJeremy L. Thompson /// @} 2877a982d89SJeremy L. Thompson 2887a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 2897a982d89SJeremy L. Thompson /// Ceed Backend API 2907a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 2917a982d89SJeremy L. Thompson /// @addtogroup CeedBasisBackend 2927a982d89SJeremy L. Thompson /// @{ 2937a982d89SJeremy L. Thompson 2947a982d89SJeremy L. Thompson /** 295ca94c3ddSJeremy L Thompson @brief Return collocated gradient matrix 2967a982d89SJeremy L. Thompson 297ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 298ca94c3ddSJeremy L Thompson @param[out] collo_grad_1d Row-major (`Q_1d * Q_1d`) matrix expressing derivatives of basis functions at quadrature points 2997a982d89SJeremy L. Thompson 3007a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3017a982d89SJeremy L. Thompson 3027a982d89SJeremy L. Thompson @ref Backend 3037a982d89SJeremy L. Thompson **/ 304d1d35e2fSjeremylt int CeedBasisGetCollocatedGrad(CeedBasis basis, CeedScalar *collo_grad_1d) { 3057a982d89SJeremy L. Thompson Ceed ceed; 3062247a93fSRezgar Shakeri CeedInt P_1d, Q_1d; 3072247a93fSRezgar Shakeri CeedScalar *interp_1d_pinv; 3081203703bSJeremy L Thompson const CeedScalar *grad_1d, *interp_1d; 3091203703bSJeremy L Thompson 310ea61e9acSJeremy L Thompson // Note: This function is for backend use, so all errors are terminal and we do not need to clean up memory on failure. 3112247a93fSRezgar Shakeri CeedCall(CeedBasisGetCeed(basis, &ceed)); 3122247a93fSRezgar Shakeri CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 3132247a93fSRezgar Shakeri CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 3147a982d89SJeremy L. Thompson 3152247a93fSRezgar Shakeri // Compute interp_1d^+, pseudoinverse of interp_1d 3162247a93fSRezgar Shakeri CeedCall(CeedCalloc(P_1d * Q_1d, &interp_1d_pinv)); 3171203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 3181203703bSJeremy L Thompson CeedCall(CeedMatrixPseudoinverse(ceed, interp_1d, Q_1d, P_1d, interp_1d_pinv)); 3191203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 3201203703bSJeremy L Thompson CeedCall(CeedMatrixMatrixMultiply(ceed, grad_1d, (const CeedScalar *)interp_1d_pinv, collo_grad_1d, Q_1d, Q_1d, P_1d)); 3217a982d89SJeremy L. Thompson 3222247a93fSRezgar Shakeri CeedCall(CeedFree(&interp_1d_pinv)); 323e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3247a982d89SJeremy L. Thompson } 3257a982d89SJeremy L. Thompson 3267a982d89SJeremy L. Thompson /** 327ca94c3ddSJeremy L Thompson @brief Get tensor status for given `CeedBasis` 3287a982d89SJeremy L. Thompson 329ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 330d1d35e2fSjeremylt @param[out] is_tensor Variable to store tensor status 3317a982d89SJeremy L. Thompson 3327a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3337a982d89SJeremy L. Thompson 3347a982d89SJeremy L. Thompson @ref Backend 3357a982d89SJeremy L. Thompson **/ 336d1d35e2fSjeremylt int CeedBasisIsTensor(CeedBasis basis, bool *is_tensor) { 3376402da51SJeremy L Thompson *is_tensor = basis->is_tensor_basis; 338e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3397a982d89SJeremy L. Thompson } 3407a982d89SJeremy L. Thompson 3417a982d89SJeremy L. Thompson /** 342ca94c3ddSJeremy L Thompson @brief Get backend data of a `CeedBasis` 3437a982d89SJeremy L. Thompson 344ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 3457a982d89SJeremy L. Thompson @param[out] data Variable to store data 3467a982d89SJeremy L. Thompson 3477a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3487a982d89SJeremy L. Thompson 3497a982d89SJeremy L. Thompson @ref Backend 3507a982d89SJeremy L. Thompson **/ 351777ff853SJeremy L Thompson int CeedBasisGetData(CeedBasis basis, void *data) { 352777ff853SJeremy L Thompson *(void **)data = basis->data; 353e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3547a982d89SJeremy L. Thompson } 3557a982d89SJeremy L. Thompson 3567a982d89SJeremy L. Thompson /** 357ca94c3ddSJeremy L Thompson @brief Set backend data of a `CeedBasis` 3587a982d89SJeremy L. Thompson 359ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` 360ea61e9acSJeremy L Thompson @param[in] data Data to set 3617a982d89SJeremy L. Thompson 3627a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3637a982d89SJeremy L. Thompson 3647a982d89SJeremy L. Thompson @ref Backend 3657a982d89SJeremy L. Thompson **/ 366777ff853SJeremy L Thompson int CeedBasisSetData(CeedBasis basis, void *data) { 367777ff853SJeremy L Thompson basis->data = data; 368e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3697a982d89SJeremy L. Thompson } 3707a982d89SJeremy L. Thompson 3717a982d89SJeremy L. Thompson /** 372ca94c3ddSJeremy L Thompson @brief Increment the reference counter for a `CeedBasis` 37334359f16Sjeremylt 374ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` to increment the reference counter 37534359f16Sjeremylt 37634359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 37734359f16Sjeremylt 37834359f16Sjeremylt @ref Backend 37934359f16Sjeremylt **/ 3809560d06aSjeremylt int CeedBasisReference(CeedBasis basis) { 38134359f16Sjeremylt basis->ref_count++; 38234359f16Sjeremylt return CEED_ERROR_SUCCESS; 38334359f16Sjeremylt } 38434359f16Sjeremylt 38534359f16Sjeremylt /** 386ca94c3ddSJeremy L Thompson @brief Get number of Q-vector components for given `CeedBasis` 387c4e3f59bSSebastian Grimberg 388ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 389ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 390ca94c3ddSJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 391ca94c3ddSJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 392ca94c3ddSJeremy L Thompson @ref CEED_EVAL_CURL to use curl 393c4e3f59bSSebastian Grimberg @param[out] q_comp Variable to store number of Q-vector components of basis 394c4e3f59bSSebastian Grimberg 395c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 396c4e3f59bSSebastian Grimberg 397c4e3f59bSSebastian Grimberg @ref Backend 398c4e3f59bSSebastian Grimberg **/ 399c4e3f59bSSebastian Grimberg int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode, CeedInt *q_comp) { 4001203703bSJeremy L Thompson CeedInt dim; 4011203703bSJeremy L Thompson 4021203703bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 403c4e3f59bSSebastian Grimberg switch (eval_mode) { 4041203703bSJeremy L Thompson case CEED_EVAL_INTERP: { 4051203703bSJeremy L Thompson CeedFESpace fe_space; 4061203703bSJeremy L Thompson 4071203703bSJeremy L Thompson CeedCall(CeedBasisGetFESpace(basis, &fe_space)); 4081203703bSJeremy L Thompson *q_comp = (fe_space == CEED_FE_SPACE_H1) ? 1 : dim; 4091203703bSJeremy L Thompson } break; 410c4e3f59bSSebastian Grimberg case CEED_EVAL_GRAD: 4111203703bSJeremy L Thompson *q_comp = dim; 412c4e3f59bSSebastian Grimberg break; 413c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 414c4e3f59bSSebastian Grimberg *q_comp = 1; 415c4e3f59bSSebastian Grimberg break; 416c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 4171203703bSJeremy L Thompson *q_comp = (dim < 3) ? 1 : dim; 418c4e3f59bSSebastian Grimberg break; 419c4e3f59bSSebastian Grimberg case CEED_EVAL_NONE: 420c4e3f59bSSebastian Grimberg case CEED_EVAL_WEIGHT: 421352a5e7cSSebastian Grimberg *q_comp = 1; 422c4e3f59bSSebastian Grimberg break; 423c4e3f59bSSebastian Grimberg } 424c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 425c4e3f59bSSebastian Grimberg } 426c4e3f59bSSebastian Grimberg 427c4e3f59bSSebastian Grimberg /** 428ca94c3ddSJeremy L Thompson @brief Estimate number of FLOPs required to apply `CeedBasis` in `t_mode` and `eval_mode` 4296e15d496SJeremy L Thompson 430ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to estimate FLOPs for 431ea61e9acSJeremy L Thompson @param[in] t_mode Apply basis or transpose 432ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CeedEvalMode 433ea61e9acSJeremy L Thompson @param[out] flops Address of variable to hold FLOPs estimate 4346e15d496SJeremy L Thompson 4356e15d496SJeremy L Thompson @ref Backend 4366e15d496SJeremy L Thompson **/ 4372b730f8bSJeremy L Thompson int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedSize *flops) { 4386e15d496SJeremy L Thompson bool is_tensor; 4396e15d496SJeremy L Thompson 4402b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor)); 4416e15d496SJeremy L Thompson if (is_tensor) { 4426e15d496SJeremy L Thompson CeedInt dim, num_comp, P_1d, Q_1d; 4431c66c397SJeremy L Thompson 4442b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 4452b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 4462b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 4472b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 4486e15d496SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 4492b730f8bSJeremy L Thompson P_1d = Q_1d; 4502b730f8bSJeremy L Thompson Q_1d = P_1d; 4516e15d496SJeremy L Thompson } 4526e15d496SJeremy L Thompson CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim - 1), post = 1; 4536e15d496SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 4546e15d496SJeremy L Thompson tensor_flops += 2 * pre * P_1d * post * Q_1d; 4556e15d496SJeremy L Thompson pre /= P_1d; 4566e15d496SJeremy L Thompson post *= Q_1d; 4576e15d496SJeremy L Thompson } 4586e15d496SJeremy L Thompson switch (eval_mode) { 4592b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 4602b730f8bSJeremy L Thompson *flops = 0; 4612b730f8bSJeremy L Thompson break; 4622b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 4632b730f8bSJeremy L Thompson *flops = tensor_flops; 4642b730f8bSJeremy L Thompson break; 4652b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 4662b730f8bSJeremy L Thompson *flops = tensor_flops * 2; 4672b730f8bSJeremy L Thompson break; 4686e15d496SJeremy L Thompson case CEED_EVAL_DIV: 4691203703bSJeremy L Thompson case CEED_EVAL_CURL: { 4706574a04fSJeremy L Thompson // LCOV_EXCL_START 4716e536b99SJeremy L Thompson return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported", 4726e536b99SJeremy L Thompson CeedEvalModes[eval_mode]); 4732b730f8bSJeremy L Thompson break; 4746e15d496SJeremy L Thompson // LCOV_EXCL_STOP 4751203703bSJeremy L Thompson } 4762b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 4772b730f8bSJeremy L Thompson *flops = dim * CeedIntPow(Q_1d, dim); 4782b730f8bSJeremy L Thompson break; 4796e15d496SJeremy L Thompson } 4806e15d496SJeremy L Thompson } else { 481c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 4821c66c397SJeremy L Thompson 4832b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 4842b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 485c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 4862b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 4872b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 4886e15d496SJeremy L Thompson switch (eval_mode) { 4892b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 4902b730f8bSJeremy L Thompson *flops = 0; 4912b730f8bSJeremy L Thompson break; 4922b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 4932b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 4942b730f8bSJeremy L Thompson case CEED_EVAL_DIV: 4952b730f8bSJeremy L Thompson case CEED_EVAL_CURL: 496c4e3f59bSSebastian Grimberg *flops = num_nodes * num_qpts * num_comp * q_comp; 4972b730f8bSJeremy L Thompson break; 4982b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 4992b730f8bSJeremy L Thompson *flops = 0; 5002b730f8bSJeremy L Thompson break; 5016e15d496SJeremy L Thompson } 5026e15d496SJeremy L Thompson } 5036e15d496SJeremy L Thompson return CEED_ERROR_SUCCESS; 5046e15d496SJeremy L Thompson } 5056e15d496SJeremy L Thompson 5066e15d496SJeremy L Thompson /** 507ca94c3ddSJeremy L Thompson @brief Get `CeedFESpace` for a `CeedBasis` 508c4e3f59bSSebastian Grimberg 509ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 510ca94c3ddSJeremy L Thompson @param[out] fe_space Variable to store `CeedFESpace` 511c4e3f59bSSebastian Grimberg 512c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 513c4e3f59bSSebastian Grimberg 514c4e3f59bSSebastian Grimberg @ref Backend 515c4e3f59bSSebastian Grimberg **/ 516c4e3f59bSSebastian Grimberg int CeedBasisGetFESpace(CeedBasis basis, CeedFESpace *fe_space) { 517c4e3f59bSSebastian Grimberg *fe_space = basis->fe_space; 518c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 519c4e3f59bSSebastian Grimberg } 520c4e3f59bSSebastian Grimberg 521c4e3f59bSSebastian Grimberg /** 522ca94c3ddSJeremy L Thompson @brief Get dimension for given `CeedElemTopology` 5237a982d89SJeremy L. Thompson 524ca94c3ddSJeremy L Thompson @param[in] topo `CeedElemTopology` 5257a982d89SJeremy L. Thompson @param[out] dim Variable to store dimension of topology 5267a982d89SJeremy L. Thompson 5277a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5287a982d89SJeremy L. Thompson 5297a982d89SJeremy L. Thompson @ref Backend 5307a982d89SJeremy L. Thompson **/ 5317a982d89SJeremy L. Thompson int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim) { 5327a982d89SJeremy L. Thompson *dim = (CeedInt)topo >> 16; 533e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5347a982d89SJeremy L. Thompson } 5357a982d89SJeremy L. Thompson 5367a982d89SJeremy L. Thompson /** 537ca94c3ddSJeremy L Thompson @brief Get `CeedTensorContract` of a `CeedBasis` 5387a982d89SJeremy L. Thompson 539ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 540ca94c3ddSJeremy L Thompson @param[out] contract Variable to store `CeedTensorContract` 5417a982d89SJeremy L. Thompson 5427a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5437a982d89SJeremy L. Thompson 5447a982d89SJeremy L. Thompson @ref Backend 5457a982d89SJeremy L. Thompson **/ 5467a982d89SJeremy L. Thompson int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract) { 5477a982d89SJeremy L. Thompson *contract = basis->contract; 548e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5497a982d89SJeremy L. Thompson } 5507a982d89SJeremy L. Thompson 5517a982d89SJeremy L. Thompson /** 552ca94c3ddSJeremy L Thompson @brief Set `CeedTensorContract` of a `CeedBasis` 5537a982d89SJeremy L. Thompson 554ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` 555ca94c3ddSJeremy L Thompson @param[in] contract `CeedTensorContract` to set 5567a982d89SJeremy L. Thompson 5577a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5587a982d89SJeremy L. Thompson 5597a982d89SJeremy L. Thompson @ref Backend 5607a982d89SJeremy L. Thompson **/ 56134359f16Sjeremylt int CeedBasisSetTensorContract(CeedBasis basis, CeedTensorContract contract) { 56234359f16Sjeremylt basis->contract = contract; 5632b730f8bSJeremy L Thompson CeedCall(CeedTensorContractReference(contract)); 564e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5657a982d89SJeremy L. Thompson } 5667a982d89SJeremy L. Thompson 5677a982d89SJeremy L. Thompson /** 568ca94c3ddSJeremy L Thompson @brief Return a reference implementation of matrix multiplication \f$C = A B\f$. 569ba59ac12SSebastian Grimberg 570ca94c3ddSJeremy L Thompson Note: This is a reference implementation for CPU `CeedScalar` pointers that is not intended for high performance. 5717a982d89SJeremy L. Thompson 572ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 573ca94c3ddSJeremy L Thompson @param[in] mat_A Row-major matrix `A` 574ca94c3ddSJeremy L Thompson @param[in] mat_B Row-major matrix `B` 575ca94c3ddSJeremy L Thompson @param[out] mat_C Row-major output matrix `C` 576ca94c3ddSJeremy L Thompson @param[in] m Number of rows of `C` 577ca94c3ddSJeremy L Thompson @param[in] n Number of columns of `C` 578ca94c3ddSJeremy L Thompson @param[in] kk Number of columns of `A`/rows of `B` 5797a982d89SJeremy L. Thompson 5807a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5817a982d89SJeremy L. Thompson 5827a982d89SJeremy L. Thompson @ref Utility 5837a982d89SJeremy L. Thompson **/ 5842b730f8bSJeremy L Thompson int CeedMatrixMatrixMultiply(Ceed ceed, const CeedScalar *mat_A, const CeedScalar *mat_B, CeedScalar *mat_C, CeedInt m, CeedInt n, CeedInt kk) { 5852b730f8bSJeremy L Thompson for (CeedInt i = 0; i < m; i++) { 5867a982d89SJeremy L. Thompson for (CeedInt j = 0; j < n; j++) { 5877a982d89SJeremy L. Thompson CeedScalar sum = 0; 5881c66c397SJeremy L Thompson 5892b730f8bSJeremy L Thompson for (CeedInt k = 0; k < kk; k++) sum += mat_A[k + i * kk] * mat_B[j + k * n]; 590d1d35e2fSjeremylt mat_C[j + i * n] = sum; 5917a982d89SJeremy L. Thompson } 5922b730f8bSJeremy L Thompson } 593e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5947a982d89SJeremy L. Thompson } 5957a982d89SJeremy L. Thompson 596ba59ac12SSebastian Grimberg /** 597ba59ac12SSebastian Grimberg @brief Return QR Factorization of a matrix 598ba59ac12SSebastian Grimberg 599ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 600ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 601ca94c3ddSJeremy L Thompson @param[in,out] tau Vector of length `m` of scaling factors 602ba59ac12SSebastian Grimberg @param[in] m Number of rows 603ba59ac12SSebastian Grimberg @param[in] n Number of columns 604ba59ac12SSebastian Grimberg 605ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 606ba59ac12SSebastian Grimberg 607ba59ac12SSebastian Grimberg @ref Utility 608ba59ac12SSebastian Grimberg **/ 609ba59ac12SSebastian Grimberg int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau, CeedInt m, CeedInt n) { 610ba59ac12SSebastian Grimberg CeedScalar v[m]; 611ba59ac12SSebastian Grimberg 612ba59ac12SSebastian Grimberg // Check matrix shape 6136574a04fSJeremy L Thompson CeedCheck(n <= m, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute QR factorization with n > m"); 614ba59ac12SSebastian Grimberg 615ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 6161c66c397SJeremy L Thompson CeedScalar sigma = 0.0; 6171c66c397SJeremy L Thompson 618ba59ac12SSebastian Grimberg if (i >= m - 1) { // last row of matrix, no reflection needed 619ba59ac12SSebastian Grimberg tau[i] = 0.; 620ba59ac12SSebastian Grimberg break; 621ba59ac12SSebastian Grimberg } 622ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 623ba59ac12SSebastian Grimberg v[i] = mat[i + n * i]; 624ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) { 625ba59ac12SSebastian Grimberg v[j] = mat[i + n * j]; 626ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 627ba59ac12SSebastian Grimberg } 6281c66c397SJeremy L Thompson const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:m] 6291c66c397SJeremy L Thompson const CeedScalar R_ii = -copysign(norm, v[i]); 6301c66c397SJeremy L Thompson 631ba59ac12SSebastian Grimberg v[i] -= R_ii; 632ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 633ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 634ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 635ba59ac12SSebastian Grimberg tau[i] = 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 636ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] /= v[i]; 637ba59ac12SSebastian Grimberg 638ba59ac12SSebastian Grimberg // Apply Householder reflector to lower right panel 639ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[i * n + i + 1], &v[i], tau[i], m - i, n - i - 1, n, 1); 640ba59ac12SSebastian Grimberg // Save v 641ba59ac12SSebastian Grimberg mat[i + n * i] = R_ii; 642ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) mat[i + n * j] = v[j]; 643ba59ac12SSebastian Grimberg } 644ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 645ba59ac12SSebastian Grimberg } 646ba59ac12SSebastian Grimberg 647ba59ac12SSebastian Grimberg /** 648ba59ac12SSebastian Grimberg @brief Apply Householder Q matrix 649ba59ac12SSebastian Grimberg 650ca94c3ddSJeremy L Thompson Compute `mat_A = mat_Q mat_A`, where `mat_Q` is \f$m \times m\f$ and `mat_A` is \f$m \times n\f$. 651ba59ac12SSebastian Grimberg 652ba59ac12SSebastian Grimberg @param[in,out] mat_A Matrix to apply Householder Q to, in place 653ba59ac12SSebastian Grimberg @param[in] mat_Q Householder Q matrix 654ba59ac12SSebastian Grimberg @param[in] tau Householder scaling factors 655ba59ac12SSebastian Grimberg @param[in] t_mode Transpose mode for application 656ca94c3ddSJeremy L Thompson @param[in] m Number of rows in `A` 657ca94c3ddSJeremy L Thompson @param[in] n Number of columns in `A` 658ca94c3ddSJeremy L Thompson @param[in] k Number of elementary reflectors in Q, `k < m` 659ca94c3ddSJeremy L Thompson @param[in] row Row stride in `A` 660ca94c3ddSJeremy L Thompson @param[in] col Col stride in `A` 661ba59ac12SSebastian Grimberg 662ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 663ba59ac12SSebastian Grimberg 664c4e3f59bSSebastian Grimberg @ref Utility 665ba59ac12SSebastian Grimberg **/ 666ba59ac12SSebastian Grimberg int CeedHouseholderApplyQ(CeedScalar *mat_A, const CeedScalar *mat_Q, const CeedScalar *tau, CeedTransposeMode t_mode, CeedInt m, CeedInt n, 667ba59ac12SSebastian Grimberg CeedInt k, CeedInt row, CeedInt col) { 668ba59ac12SSebastian Grimberg CeedScalar *v; 6691c66c397SJeremy L Thompson 670ba59ac12SSebastian Grimberg CeedCall(CeedMalloc(m, &v)); 671ba59ac12SSebastian Grimberg for (CeedInt ii = 0; ii < k; ii++) { 672ba59ac12SSebastian Grimberg CeedInt i = t_mode == CEED_TRANSPOSE ? ii : k - 1 - ii; 673ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] = mat_Q[j * k + i]; 674ba59ac12SSebastian Grimberg // Apply Householder reflector (I - tau v v^T) collo_grad_1d^T 675ba59ac12SSebastian Grimberg CeedCall(CeedHouseholderReflect(&mat_A[i * row], &v[i], tau[i], m - i, n, row, col)); 676ba59ac12SSebastian Grimberg } 677ba59ac12SSebastian Grimberg CeedCall(CeedFree(&v)); 678ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 679ba59ac12SSebastian Grimberg } 680ba59ac12SSebastian Grimberg 681ba59ac12SSebastian Grimberg /** 6822247a93fSRezgar Shakeri @brief Return pseudoinverse of a matrix 6832247a93fSRezgar Shakeri 6842247a93fSRezgar Shakeri @param[in] ceed Ceed context for error handling 6852247a93fSRezgar Shakeri @param[in] mat Row-major matrix to compute pseudoinverse of 6862247a93fSRezgar Shakeri @param[in] m Number of rows 6872247a93fSRezgar Shakeri @param[in] n Number of columns 6882247a93fSRezgar Shakeri @param[out] mat_pinv Row-major pseudoinverse matrix 6892247a93fSRezgar Shakeri 6902247a93fSRezgar Shakeri @return An error code: 0 - success, otherwise - failure 6912247a93fSRezgar Shakeri 6922247a93fSRezgar Shakeri @ref Utility 6932247a93fSRezgar Shakeri **/ 6941203703bSJeremy L Thompson int CeedMatrixPseudoinverse(Ceed ceed, const CeedScalar *mat, CeedInt m, CeedInt n, CeedScalar *mat_pinv) { 6952247a93fSRezgar Shakeri CeedScalar *tau, *I, *mat_copy; 6962247a93fSRezgar Shakeri 6972247a93fSRezgar Shakeri CeedCall(CeedCalloc(m, &tau)); 6982247a93fSRezgar Shakeri CeedCall(CeedCalloc(m * m, &I)); 6992247a93fSRezgar Shakeri CeedCall(CeedCalloc(m * n, &mat_copy)); 7002247a93fSRezgar Shakeri memcpy(mat_copy, mat, m * n * sizeof mat[0]); 7012247a93fSRezgar Shakeri 7022247a93fSRezgar Shakeri // QR Factorization, mat = Q R 7032247a93fSRezgar Shakeri CeedCall(CeedQRFactorization(ceed, mat_copy, tau, m, n)); 7042247a93fSRezgar Shakeri 7052247a93fSRezgar Shakeri // -- Apply Q^T, I = Q^T * I 7062247a93fSRezgar Shakeri for (CeedInt i = 0; i < m; i++) I[i * m + i] = 1.0; 7072247a93fSRezgar Shakeri CeedCall(CeedHouseholderApplyQ(I, mat_copy, tau, CEED_TRANSPOSE, m, m, n, m, 1)); 7082247a93fSRezgar Shakeri // -- Apply R_inv, mat_pinv = R_inv * Q^T 7092247a93fSRezgar Shakeri for (CeedInt j = 0; j < m; j++) { // Column j 7102247a93fSRezgar Shakeri mat_pinv[j + m * (n - 1)] = I[j + m * (n - 1)] / mat_copy[n * n - 1]; 7112247a93fSRezgar Shakeri for (CeedInt i = n - 2; i >= 0; i--) { // Row i 7122247a93fSRezgar Shakeri mat_pinv[j + m * i] = I[j + m * i]; 7132247a93fSRezgar Shakeri for (CeedInt k = i + 1; k < n; k++) mat_pinv[j + m * i] -= mat_copy[k + n * i] * mat_pinv[j + m * k]; 7142247a93fSRezgar Shakeri mat_pinv[j + m * i] /= mat_copy[i + n * i]; 7152247a93fSRezgar Shakeri } 7162247a93fSRezgar Shakeri } 7172247a93fSRezgar Shakeri 7182247a93fSRezgar Shakeri // Cleanup 7192247a93fSRezgar Shakeri CeedCall(CeedFree(&I)); 7202247a93fSRezgar Shakeri CeedCall(CeedFree(&tau)); 7212247a93fSRezgar Shakeri CeedCall(CeedFree(&mat_copy)); 7222247a93fSRezgar Shakeri return CEED_ERROR_SUCCESS; 7232247a93fSRezgar Shakeri } 7242247a93fSRezgar Shakeri 7252247a93fSRezgar Shakeri /** 726ba59ac12SSebastian Grimberg @brief Return symmetric Schur decomposition of the symmetric matrix mat via symmetric QR factorization 727ba59ac12SSebastian Grimberg 728ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 729ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 730ba59ac12SSebastian Grimberg @param[out] lambda Vector of length n of eigenvalues 731ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 732ba59ac12SSebastian Grimberg 733ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 734ba59ac12SSebastian Grimberg 735ba59ac12SSebastian Grimberg @ref Utility 736ba59ac12SSebastian Grimberg **/ 7372c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 7382c2ea1dbSJeremy L Thompson int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat, CeedScalar *lambda, CeedInt n) { 739ba59ac12SSebastian Grimberg // Check bounds for clang-tidy 7406574a04fSJeremy L Thompson CeedCheck(n > 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute symmetric Schur decomposition of scalars"); 741ba59ac12SSebastian Grimberg 742ba59ac12SSebastian Grimberg CeedScalar v[n - 1], tau[n - 1], mat_T[n * n]; 743ba59ac12SSebastian Grimberg 744ba59ac12SSebastian Grimberg // Copy mat to mat_T and set mat to I 745ba59ac12SSebastian Grimberg memcpy(mat_T, mat, n * n * sizeof(mat[0])); 746ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 747ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) mat[j + n * i] = (i == j) ? 1 : 0; 748ba59ac12SSebastian Grimberg } 749ba59ac12SSebastian Grimberg 750ba59ac12SSebastian Grimberg // Reduce to tridiagonal 751ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - 1; i++) { 752ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 753ba59ac12SSebastian Grimberg CeedScalar sigma = 0.0; 7541c66c397SJeremy L Thompson 755ba59ac12SSebastian Grimberg v[i] = mat_T[i + n * (i + 1)]; 756ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 757ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 758ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 759ba59ac12SSebastian Grimberg } 7601c66c397SJeremy L Thompson const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:n-1] 7611c66c397SJeremy L Thompson const CeedScalar R_ii = -copysign(norm, v[i]); 7621c66c397SJeremy L Thompson 763ba59ac12SSebastian Grimberg v[i] -= R_ii; 764ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 765ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 766ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 767ba59ac12SSebastian Grimberg tau[i] = i == n - 2 ? 2 : 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 768ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) v[j] /= v[i]; 769ba59ac12SSebastian Grimberg 770ba59ac12SSebastian Grimberg // Update sub and super diagonal 771ba59ac12SSebastian Grimberg for (CeedInt j = i + 2; j < n; j++) { 772ba59ac12SSebastian Grimberg mat_T[i + n * j] = 0; 773ba59ac12SSebastian Grimberg mat_T[j + n * i] = 0; 774ba59ac12SSebastian Grimberg } 775ba59ac12SSebastian Grimberg // Apply symmetric Householder reflector to lower right panel 776ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 777ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), 1, n); 778ba59ac12SSebastian Grimberg 779ba59ac12SSebastian Grimberg // Save v 780ba59ac12SSebastian Grimberg mat_T[i + n * (i + 1)] = R_ii; 781ba59ac12SSebastian Grimberg mat_T[(i + 1) + n * i] = R_ii; 782ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 783ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = v[j]; 784ba59ac12SSebastian Grimberg } 785ba59ac12SSebastian Grimberg } 786ba59ac12SSebastian Grimberg // Backwards accumulation of Q 787ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 788ba59ac12SSebastian Grimberg if (tau[i] > 0.0) { 789ba59ac12SSebastian Grimberg v[i] = 1; 790ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 791ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 792ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = 0; 793ba59ac12SSebastian Grimberg } 794ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 795ba59ac12SSebastian Grimberg } 796ba59ac12SSebastian Grimberg } 797ba59ac12SSebastian Grimberg 798ba59ac12SSebastian Grimberg // Reduce sub and super diagonal 799ba59ac12SSebastian Grimberg CeedInt p = 0, q = 0, itr = 0, max_itr = n * n * n * n; 800ba59ac12SSebastian Grimberg CeedScalar tol = CEED_EPSILON; 801ba59ac12SSebastian Grimberg 802ba59ac12SSebastian Grimberg while (itr < max_itr) { 803ba59ac12SSebastian Grimberg // Update p, q, size of reduced portions of diagonal 804ba59ac12SSebastian Grimberg p = 0; 805ba59ac12SSebastian Grimberg q = 0; 806ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 807ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) q += 1; 808ba59ac12SSebastian Grimberg else break; 809ba59ac12SSebastian Grimberg } 810ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - q - 1; i++) { 811ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) p += 1; 812ba59ac12SSebastian Grimberg else break; 813ba59ac12SSebastian Grimberg } 814ba59ac12SSebastian Grimberg if (q == n - 1) break; // Finished reducing 815ba59ac12SSebastian Grimberg 816ba59ac12SSebastian Grimberg // Reduce tridiagonal portion 817ba59ac12SSebastian Grimberg CeedScalar t_nn = mat_T[(n - 1 - q) + n * (n - 1 - q)], t_nnm1 = mat_T[(n - 2 - q) + n * (n - 1 - q)]; 818ba59ac12SSebastian Grimberg CeedScalar d = (mat_T[(n - 2 - q) + n * (n - 2 - q)] - t_nn) / 2; 819ba59ac12SSebastian Grimberg CeedScalar mu = t_nn - t_nnm1 * t_nnm1 / (d + copysign(sqrt(d * d + t_nnm1 * t_nnm1), d)); 820ba59ac12SSebastian Grimberg CeedScalar x = mat_T[p + n * p] - mu; 821ba59ac12SSebastian Grimberg CeedScalar z = mat_T[p + n * (p + 1)]; 8221c66c397SJeremy L Thompson 823ba59ac12SSebastian Grimberg for (CeedInt k = p; k < n - q - 1; k++) { 824ba59ac12SSebastian Grimberg // Compute Givens rotation 825ba59ac12SSebastian Grimberg CeedScalar c = 1, s = 0; 8261c66c397SJeremy L Thompson 827ba59ac12SSebastian Grimberg if (fabs(z) > tol) { 828ba59ac12SSebastian Grimberg if (fabs(z) > fabs(x)) { 8291c66c397SJeremy L Thompson const CeedScalar tau = -x / z; 8301c66c397SJeremy L Thompson 8311c66c397SJeremy L Thompson s = 1 / sqrt(1 + tau * tau); 8321c66c397SJeremy L Thompson c = s * tau; 833ba59ac12SSebastian Grimberg } else { 8341c66c397SJeremy L Thompson const CeedScalar tau = -z / x; 8351c66c397SJeremy L Thompson 8361c66c397SJeremy L Thompson c = 1 / sqrt(1 + tau * tau); 8371c66c397SJeremy L Thompson s = c * tau; 838ba59ac12SSebastian Grimberg } 839ba59ac12SSebastian Grimberg } 840ba59ac12SSebastian Grimberg 841ba59ac12SSebastian Grimberg // Apply Givens rotation to T 842ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 843ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_TRANSPOSE, k, k + 1, n, n); 844ba59ac12SSebastian Grimberg 845ba59ac12SSebastian Grimberg // Apply Givens rotation to Q 846ba59ac12SSebastian Grimberg CeedGivensRotation(mat, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 847ba59ac12SSebastian Grimberg 848ba59ac12SSebastian Grimberg // Update x, z 849ba59ac12SSebastian Grimberg if (k < n - q - 2) { 850ba59ac12SSebastian Grimberg x = mat_T[k + n * (k + 1)]; 851ba59ac12SSebastian Grimberg z = mat_T[k + n * (k + 2)]; 852ba59ac12SSebastian Grimberg } 853ba59ac12SSebastian Grimberg } 854ba59ac12SSebastian Grimberg itr++; 855ba59ac12SSebastian Grimberg } 856ba59ac12SSebastian Grimberg 857ba59ac12SSebastian Grimberg // Save eigenvalues 858ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) lambda[i] = mat_T[i + n * i]; 859ba59ac12SSebastian Grimberg 860ba59ac12SSebastian Grimberg // Check convergence 8616574a04fSJeremy L Thompson CeedCheck(itr < max_itr || q > n, ceed, CEED_ERROR_MINOR, "Symmetric QR failed to converge"); 862ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 863ba59ac12SSebastian Grimberg } 8642c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 865ba59ac12SSebastian Grimberg 866ba59ac12SSebastian Grimberg /** 867ba59ac12SSebastian Grimberg @brief Return Simultaneous Diagonalization of two matrices. 868ba59ac12SSebastian Grimberg 869ca94c3ddSJeremy L Thompson This solves the generalized eigenvalue problem `A x = lambda B x`, where `A` and `B` are symmetric and `B` is positive definite. 870ca94c3ddSJeremy L Thompson We generate the matrix `X` and vector `Lambda` such that `X^T A X = Lambda` and `X^T B X = I`. 871ca94c3ddSJeremy L Thompson This is equivalent to the LAPACK routine 'sygv' with `TYPE = 1`. 872ba59ac12SSebastian Grimberg 873ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 874ba59ac12SSebastian Grimberg @param[in] mat_A Row-major matrix to be factorized with eigenvalues 875ba59ac12SSebastian Grimberg @param[in] mat_B Row-major matrix to be factorized to identity 876ba59ac12SSebastian Grimberg @param[out] mat_X Row-major orthogonal matrix 877ca94c3ddSJeremy L Thompson @param[out] lambda Vector of length `n` of generalized eigenvalues 878ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 879ba59ac12SSebastian Grimberg 880ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 881ba59ac12SSebastian Grimberg 882ba59ac12SSebastian Grimberg @ref Utility 883ba59ac12SSebastian Grimberg **/ 8842c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 8852c2ea1dbSJeremy L Thompson int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A, CeedScalar *mat_B, CeedScalar *mat_X, CeedScalar *lambda, CeedInt n) { 886ba59ac12SSebastian Grimberg CeedScalar *mat_C, *mat_G, *vec_D; 8871c66c397SJeremy L Thompson 888ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_C)); 889ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_G)); 890ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n, &vec_D)); 891ba59ac12SSebastian Grimberg 892ba59ac12SSebastian Grimberg // Compute B = G D G^T 893ba59ac12SSebastian Grimberg memcpy(mat_G, mat_B, n * n * sizeof(mat_B[0])); 894ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_G, vec_D, n)); 895ba59ac12SSebastian Grimberg 896ba59ac12SSebastian Grimberg // Sort eigenvalues 897ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 898ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 899ba59ac12SSebastian Grimberg if (fabs(vec_D[j]) > fabs(vec_D[j + 1])) { 9001c66c397SJeremy L Thompson CeedScalarSwap(vec_D[j], vec_D[j + 1]); 9011c66c397SJeremy L Thompson for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_G[k * n + j], mat_G[k * n + j + 1]); 902ba59ac12SSebastian Grimberg } 903ba59ac12SSebastian Grimberg } 904ba59ac12SSebastian Grimberg } 905ba59ac12SSebastian Grimberg 906ba59ac12SSebastian Grimberg // Compute C = (G D^1/2)^-1 A (G D^1/2)^-T 907ba59ac12SSebastian Grimberg // = D^-1/2 G^T A G D^-1/2 908ba59ac12SSebastian Grimberg // -- D = D^-1/2 909ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) vec_D[i] = 1. / sqrt(vec_D[i]); 910ba59ac12SSebastian Grimberg // -- G = G D^-1/2 911ba59ac12SSebastian Grimberg // -- C = D^-1/2 G^T 912ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 913ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) { 914ba59ac12SSebastian Grimberg mat_G[i * n + j] *= vec_D[j]; 915ba59ac12SSebastian Grimberg mat_C[j * n + i] = mat_G[i * n + j]; 916ba59ac12SSebastian Grimberg } 917ba59ac12SSebastian Grimberg } 918ba59ac12SSebastian Grimberg // -- X = (D^-1/2 G^T) A 919ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_C, (const CeedScalar *)mat_A, mat_X, n, n, n)); 920ba59ac12SSebastian Grimberg // -- C = (D^-1/2 G^T A) (G D^-1/2) 921ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_X, (const CeedScalar *)mat_G, mat_C, n, n, n)); 922ba59ac12SSebastian Grimberg 923ba59ac12SSebastian Grimberg // Compute Q^T C Q = lambda 924ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_C, lambda, n)); 925ba59ac12SSebastian Grimberg 926ba59ac12SSebastian Grimberg // Sort eigenvalues 927ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 928ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 929ba59ac12SSebastian Grimberg if (fabs(lambda[j]) > fabs(lambda[j + 1])) { 9301c66c397SJeremy L Thompson CeedScalarSwap(lambda[j], lambda[j + 1]); 9311c66c397SJeremy L Thompson for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_C[k * n + j], mat_C[k * n + j + 1]); 932ba59ac12SSebastian Grimberg } 933ba59ac12SSebastian Grimberg } 934ba59ac12SSebastian Grimberg } 935ba59ac12SSebastian Grimberg 936ba59ac12SSebastian Grimberg // Set X = (G D^1/2)^-T Q 937ba59ac12SSebastian Grimberg // = G D^-1/2 Q 938ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_G, (const CeedScalar *)mat_C, mat_X, n, n, n)); 939ba59ac12SSebastian Grimberg 940ba59ac12SSebastian Grimberg // Cleanup 941ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_C)); 942ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_G)); 943ba59ac12SSebastian Grimberg CeedCall(CeedFree(&vec_D)); 944ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 945ba59ac12SSebastian Grimberg } 9462c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 947ba59ac12SSebastian Grimberg 9487a982d89SJeremy L. Thompson /// @} 9497a982d89SJeremy L. Thompson 9507a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 9517a982d89SJeremy L. Thompson /// CeedBasis Public API 9527a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 9537a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser 954d7b241e6Sjeremylt /// @{ 955d7b241e6Sjeremylt 956b11c1e72Sjeremylt /** 957ca94c3ddSJeremy L Thompson @brief Create a tensor-product basis for \f$H^1\f$ discretizations 958b11c1e72Sjeremylt 959ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 960ea61e9acSJeremy L Thompson @param[in] dim Topological dimension 961ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 962ea61e9acSJeremy L Thompson @param[in] P_1d Number of nodes in one dimension 963ea61e9acSJeremy L Thompson @param[in] Q_1d Number of quadrature points in one dimension 964ca94c3ddSJeremy L Thompson @param[in] interp_1d Row-major (`Q_1d * P_1d`) matrix expressing the values of nodal basis functions at quadrature points 965ca94c3ddSJeremy L Thompson @param[in] grad_1d Row-major (`Q_1d * P_1d`) matrix expressing derivatives of nodal basis functions at quadrature points 966ca94c3ddSJeremy L Thompson @param[in] q_ref_1d Array of length `Q_1d` holding the locations of quadrature points on the 1D reference element `[-1, 1]` 967ca94c3ddSJeremy L Thompson @param[in] q_weight_1d Array of length `Q_1d` holding the quadrature weights on the reference element 968ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 969b11c1e72Sjeremylt 970b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 971dfdf5a53Sjeremylt 9727a982d89SJeremy L. Thompson @ref User 973b11c1e72Sjeremylt **/ 9742b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, 9752b730f8bSJeremy L Thompson const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis *basis) { 9765fe0d4faSjeremylt if (!ceed->BasisCreateTensorH1) { 9775fe0d4faSjeremylt Ceed delegate; 9786574a04fSJeremy L Thompson 9792b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 980*1ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateTensorH1"); 9812b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 982e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9835fe0d4faSjeremylt } 984e15f9bd0SJeremy L Thompson 985ca94c3ddSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value"); 986ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 987ca94c3ddSJeremy L Thompson CeedCheck(P_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 988ca94c3ddSJeremy L Thompson CeedCheck(Q_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 989227444bfSJeremy L Thompson 9902b730f8bSJeremy L Thompson CeedElemTopology topo = dim == 1 ? CEED_TOPOLOGY_LINE : dim == 2 ? CEED_TOPOLOGY_QUAD : CEED_TOPOLOGY_HEX; 991e15f9bd0SJeremy L Thompson 9922b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 993db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 994d1d35e2fSjeremylt (*basis)->ref_count = 1; 9956402da51SJeremy L Thompson (*basis)->is_tensor_basis = true; 996d7b241e6Sjeremylt (*basis)->dim = dim; 997d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 998d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 999d1d35e2fSjeremylt (*basis)->P_1d = P_1d; 1000d1d35e2fSjeremylt (*basis)->Q_1d = Q_1d; 1001d1d35e2fSjeremylt (*basis)->P = CeedIntPow(P_1d, dim); 1002d1d35e2fSjeremylt (*basis)->Q = CeedIntPow(Q_1d, dim); 1003c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 10042b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_ref_1d)); 10052b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_weight_1d)); 1006ff3a0f91SJeremy L Thompson if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d * sizeof(q_ref_1d[0])); 10072b730f8bSJeremy L Thompson if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, Q_1d * sizeof(q_weight_1d[0])); 10082b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->interp_1d)); 10092b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->grad_1d)); 10102b730f8bSJeremy L Thompson if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, Q_1d * P_1d * sizeof(interp_1d[0])); 1011ff3a0f91SJeremy L Thompson if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d * P_1d * sizeof(grad_1d[0])); 10122b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, *basis)); 1013e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1014d7b241e6Sjeremylt } 1015d7b241e6Sjeremylt 1016b11c1e72Sjeremylt /** 1017ca94c3ddSJeremy L Thompson @brief Create a tensor-product \f$H^1\f$ Lagrange basis 1018b11c1e72Sjeremylt 1019ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1020ea61e9acSJeremy L Thompson @param[in] dim Topological dimension of element 1021ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1022ea61e9acSJeremy L Thompson @param[in] P Number of Gauss-Lobatto nodes in one dimension. 1023ca94c3ddSJeremy L Thompson The polynomial degree of the resulting `Q_k` element is `k = P - 1`. 1024ea61e9acSJeremy L Thompson @param[in] Q Number of quadrature points in one dimension. 1025ca94c3ddSJeremy L Thompson @param[in] quad_mode Distribution of the `Q` quadrature points (affects order of accuracy for the quadrature) 1026ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1027b11c1e72Sjeremylt 1028b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1029dfdf5a53Sjeremylt 10307a982d89SJeremy L. Thompson @ref User 1031b11c1e72Sjeremylt **/ 10322b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis) { 1033d7b241e6Sjeremylt // Allocate 1034c8c3fa7dSJeremy L Thompson int ierr = CEED_ERROR_SUCCESS; 10352b730f8bSJeremy L Thompson CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, *q_weight_1d; 10364d537eeaSYohann 1037ca94c3ddSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value"); 1038ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1039ca94c3ddSJeremy L Thompson CeedCheck(P > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1040ca94c3ddSJeremy L Thompson CeedCheck(Q > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1041227444bfSJeremy L Thompson 1042e15f9bd0SJeremy L Thompson // Get Nodes and Weights 10432b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &interp_1d)); 10442b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &grad_1d)); 10452b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P, &nodes)); 10462b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_ref_1d)); 10472b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_weight_1d)); 10482b730f8bSJeremy L Thompson if (CeedLobattoQuadrature(P, nodes, NULL) != CEED_ERROR_SUCCESS) goto cleanup; 1049d1d35e2fSjeremylt switch (quad_mode) { 1050d7b241e6Sjeremylt case CEED_GAUSS: 1051d1d35e2fSjeremylt ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d); 1052d7b241e6Sjeremylt break; 1053d7b241e6Sjeremylt case CEED_GAUSS_LOBATTO: 1054d1d35e2fSjeremylt ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d); 1055d7b241e6Sjeremylt break; 1056d7b241e6Sjeremylt } 10572b730f8bSJeremy L Thompson if (ierr != CEED_ERROR_SUCCESS) goto cleanup; 1058e15f9bd0SJeremy L Thompson 1059d7b241e6Sjeremylt // Build B, D matrix 1060d7b241e6Sjeremylt // Fornberg, 1998 1061c8c3fa7dSJeremy L Thompson for (CeedInt i = 0; i < Q; i++) { 1062d7b241e6Sjeremylt c1 = 1.0; 1063d1d35e2fSjeremylt c3 = nodes[0] - q_ref_1d[i]; 1064d1d35e2fSjeremylt interp_1d[i * P + 0] = 1.0; 1065c8c3fa7dSJeremy L Thompson for (CeedInt j = 1; j < P; j++) { 1066d7b241e6Sjeremylt c2 = 1.0; 1067d7b241e6Sjeremylt c4 = c3; 1068d1d35e2fSjeremylt c3 = nodes[j] - q_ref_1d[i]; 1069c8c3fa7dSJeremy L Thompson for (CeedInt k = 0; k < j; k++) { 1070d7b241e6Sjeremylt dx = nodes[j] - nodes[k]; 1071d7b241e6Sjeremylt c2 *= dx; 1072d7b241e6Sjeremylt if (k == j - 1) { 1073d1d35e2fSjeremylt grad_1d[i * P + j] = c1 * (interp_1d[i * P + k] - c4 * grad_1d[i * P + k]) / c2; 1074d1d35e2fSjeremylt interp_1d[i * P + j] = -c1 * c4 * interp_1d[i * P + k] / c2; 1075d7b241e6Sjeremylt } 1076d1d35e2fSjeremylt grad_1d[i * P + k] = (c3 * grad_1d[i * P + k] - interp_1d[i * P + k]) / dx; 1077d1d35e2fSjeremylt interp_1d[i * P + k] = c3 * interp_1d[i * P + k] / dx; 1078d7b241e6Sjeremylt } 1079d7b241e6Sjeremylt c1 = c2; 1080d7b241e6Sjeremylt } 1081d7b241e6Sjeremylt } 10829ac7b42eSJeremy L Thompson // Pass to CeedBasisCreateTensorH1 10832b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 1084e15f9bd0SJeremy L Thompson cleanup: 10852b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_1d)); 10862b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_1d)); 10872b730f8bSJeremy L Thompson CeedCall(CeedFree(&nodes)); 10882b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref_1d)); 10892b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight_1d)); 1090e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1091d7b241e6Sjeremylt } 1092d7b241e6Sjeremylt 1093b11c1e72Sjeremylt /** 1094ca94c3ddSJeremy L Thompson @brief Create a non tensor-product basis for \f$H^1\f$ discretizations 1095a8de75f0Sjeremylt 1096ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1097ea61e9acSJeremy L Thompson @param[in] topo Topology of element, e.g. hypercube, simplex, ect 1098ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1099ea61e9acSJeremy L Thompson @param[in] num_nodes Total number of nodes 1100ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1101ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`num_qpts * num_nodes`) matrix expressing the values of nodal basis functions at quadrature points 1102ca94c3ddSJeremy L Thompson @param[in] grad Row-major (`dim * num_qpts * num_nodes`) matrix expressing derivatives of nodal basis functions at quadrature points 1103ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element 1104ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1105ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1106a8de75f0Sjeremylt 1107a8de75f0Sjeremylt @return An error code: 0 - success, otherwise - failure 1108a8de75f0Sjeremylt 11097a982d89SJeremy L. Thompson @ref User 1110a8de75f0Sjeremylt **/ 11112b730f8bSJeremy L Thompson int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 11122b730f8bSJeremy L Thompson const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1113d1d35e2fSjeremylt CeedInt P = num_nodes, Q = num_qpts, dim = 0; 1114a8de75f0Sjeremylt 11155fe0d4faSjeremylt if (!ceed->BasisCreateH1) { 11165fe0d4faSjeremylt Ceed delegate; 11176574a04fSJeremy L Thompson 11182b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 1119*1ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateH1"); 11202b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis)); 1121e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11225fe0d4faSjeremylt } 11235fe0d4faSjeremylt 1124ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1125ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1126ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1127227444bfSJeremy L Thompson 11282b730f8bSJeremy L Thompson CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1129a8de75f0Sjeremylt 1130db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1131db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1132d1d35e2fSjeremylt (*basis)->ref_count = 1; 11336402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1134a8de75f0Sjeremylt (*basis)->dim = dim; 1135d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 1136d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 1137a8de75f0Sjeremylt (*basis)->P = P; 1138a8de75f0Sjeremylt (*basis)->Q = Q; 1139c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 11402b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * dim, &(*basis)->q_ref_1d)); 11412b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &(*basis)->q_weight_1d)); 1142ff3a0f91SJeremy L Thompson if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1143ff3a0f91SJeremy L Thompson if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 11442b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * P, &(*basis)->interp)); 11452b730f8bSJeremy L Thompson CeedCall(CeedCalloc(dim * Q * P, &(*basis)->grad)); 1146ff3a0f91SJeremy L Thompson if (interp) memcpy((*basis)->interp, interp, Q * P * sizeof(interp[0])); 1147ff3a0f91SJeremy L Thompson if (grad) memcpy((*basis)->grad, grad, dim * Q * P * sizeof(grad[0])); 11482b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, *basis)); 1149e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1150a8de75f0Sjeremylt } 1151a8de75f0Sjeremylt 1152a8de75f0Sjeremylt /** 1153859c15bbSJames Wright @brief Create a non tensor-product basis for \f$H(\mathrm{div})\f$ discretizations 115450c301a5SRezgar Shakeri 1155ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1156ea61e9acSJeremy L Thompson @param[in] topo Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below 1157ea61e9acSJeremy L Thompson @param[in] num_comp Number of components (usually 1 for vectors in H(div) bases) 1158ca94c3ddSJeremy L Thompson @param[in] num_nodes Total number of nodes (DoFs per element) 1159ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1160ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points 1161ca94c3ddSJeremy L Thompson @param[in] div Row-major (`num_qpts * num_nodes`) matrix expressing divergence of basis functions at quadrature points 1162ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element 1163ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1164ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 116550c301a5SRezgar Shakeri 116650c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 116750c301a5SRezgar Shakeri 116850c301a5SRezgar Shakeri @ref User 116950c301a5SRezgar Shakeri **/ 11702b730f8bSJeremy L Thompson int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 11712b730f8bSJeremy L Thompson const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 117250c301a5SRezgar Shakeri CeedInt Q = num_qpts, P = num_nodes, dim = 0; 1173c4e3f59bSSebastian Grimberg 117450c301a5SRezgar Shakeri if (!ceed->BasisCreateHdiv) { 117550c301a5SRezgar Shakeri Ceed delegate; 11766574a04fSJeremy L Thompson 11772b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 11786574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv"); 11792b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis)); 118050c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 118150c301a5SRezgar Shakeri } 118250c301a5SRezgar Shakeri 1183ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1184ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1185ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1186227444bfSJeremy L Thompson 1187c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1188c4e3f59bSSebastian Grimberg 1189db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1190db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 119150c301a5SRezgar Shakeri (*basis)->ref_count = 1; 11926402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 119350c301a5SRezgar Shakeri (*basis)->dim = dim; 119450c301a5SRezgar Shakeri (*basis)->topo = topo; 119550c301a5SRezgar Shakeri (*basis)->num_comp = num_comp; 119650c301a5SRezgar Shakeri (*basis)->P = P; 119750c301a5SRezgar Shakeri (*basis)->Q = Q; 1198c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HDIV; 11992b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 12002b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 120150c301a5SRezgar Shakeri if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 120250c301a5SRezgar Shakeri if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 12032b730f8bSJeremy L Thompson CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 12042b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * P, &(*basis)->div)); 120550c301a5SRezgar Shakeri if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 120650c301a5SRezgar Shakeri if (div) memcpy((*basis)->div, div, Q * P * sizeof(div[0])); 12072b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, q_weight, *basis)); 120850c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 120950c301a5SRezgar Shakeri } 121050c301a5SRezgar Shakeri 121150c301a5SRezgar Shakeri /** 12124385fb7fSSebastian Grimberg @brief Create a non tensor-product basis for \f$H(\mathrm{curl})\f$ discretizations 1213c4e3f59bSSebastian Grimberg 1214ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1215c4e3f59bSSebastian Grimberg @param[in] topo Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below 1216ca94c3ddSJeremy L Thompson @param[in] num_comp Number of components (usually 1 for vectors in \f$H(\mathrm{curl})\f$ bases) 1217ca94c3ddSJeremy L Thompson @param[in] num_nodes Total number of nodes (DoFs per element) 1218c4e3f59bSSebastian Grimberg @param[in] num_qpts Total number of quadrature points 1219ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points 1220ca94c3ddSJeremy L Thompson @param[in] curl Row-major (`curl_comp * num_qpts * num_nodes`, `curl_comp = 1` if `dim < 3` otherwise `curl_comp = dim`) matrix expressing curl of basis functions at quadrature points 1221ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element 1222ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1223ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1224c4e3f59bSSebastian Grimberg 1225c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1226c4e3f59bSSebastian Grimberg 1227c4e3f59bSSebastian Grimberg @ref User 1228c4e3f59bSSebastian Grimberg **/ 1229c4e3f59bSSebastian Grimberg int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 1230c4e3f59bSSebastian Grimberg const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1231c4e3f59bSSebastian Grimberg CeedInt Q = num_qpts, P = num_nodes, dim = 0, curl_comp = 0; 1232c4e3f59bSSebastian Grimberg 1233d075f50bSSebastian Grimberg if (!ceed->BasisCreateHcurl) { 1234c4e3f59bSSebastian Grimberg Ceed delegate; 12356574a04fSJeremy L Thompson 1236c4e3f59bSSebastian Grimberg CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 12376574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl"); 1238c4e3f59bSSebastian Grimberg CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis)); 1239c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1240c4e3f59bSSebastian Grimberg } 1241c4e3f59bSSebastian Grimberg 1242ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1243ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1244ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1245c4e3f59bSSebastian Grimberg 1246c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1247c4e3f59bSSebastian Grimberg curl_comp = (dim < 3) ? 1 : dim; 1248c4e3f59bSSebastian Grimberg 1249db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1250db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1251c4e3f59bSSebastian Grimberg (*basis)->ref_count = 1; 12526402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1253c4e3f59bSSebastian Grimberg (*basis)->dim = dim; 1254c4e3f59bSSebastian Grimberg (*basis)->topo = topo; 1255c4e3f59bSSebastian Grimberg (*basis)->num_comp = num_comp; 1256c4e3f59bSSebastian Grimberg (*basis)->P = P; 1257c4e3f59bSSebastian Grimberg (*basis)->Q = Q; 1258c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HCURL; 1259c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 1260c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 1261c4e3f59bSSebastian Grimberg if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1262c4e3f59bSSebastian Grimberg if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 1263c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 1264c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(curl_comp * Q * P, &(*basis)->curl)); 1265c4e3f59bSSebastian Grimberg if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 1266c4e3f59bSSebastian Grimberg if (curl) memcpy((*basis)->curl, curl, curl_comp * Q * P * sizeof(curl[0])); 1267c4e3f59bSSebastian Grimberg CeedCall(ceed->BasisCreateHcurl(topo, dim, P, Q, interp, curl, q_ref, q_weight, *basis)); 1268c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1269c4e3f59bSSebastian Grimberg } 1270c4e3f59bSSebastian Grimberg 1271c4e3f59bSSebastian Grimberg /** 1272ca94c3ddSJeremy L Thompson @brief Create a `CeedBasis` for projection from the nodes of `basis_from` to the nodes of `basis_to`. 1273ba59ac12SSebastian Grimberg 1274ca94c3ddSJeremy L Thompson Only @ref CEED_EVAL_INTERP will be valid for the new basis, `basis_project`. 1275ca94c3ddSJeremy L Thompson For \f$H^1\f$ spaces, @ref CEED_EVAL_GRAD will also be valid. 1276ca94c3ddSJeremy L Thompson The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization. 1277ca94c3ddSJeremy L Thompson The gradient (for the \f$H^1\f$ case) is given by `grad_project = interp_to^+ * grad_from`. 127815ad3917SSebastian Grimberg 127915ad3917SSebastian Grimberg Note: `basis_from` and `basis_to` must have compatible quadrature spaces. 128015ad3917SSebastian Grimberg 12819fd66db6SSebastian Grimberg Note: `basis_project` will have the same number of components as `basis_from`, regardless of the number of components that `basis_to` has. 12829fd66db6SSebastian Grimberg If `basis_from` has 3 components and `basis_to` has 5 components, then `basis_project` will have 3 components. 1283f113e5dcSJeremy L Thompson 1284ca94c3ddSJeremy L Thompson @param[in] basis_from `CeedBasis` to prolong from 1285ca94c3ddSJeremy L Thompson @param[in] basis_to `CeedBasis` to prolong to 1286ca94c3ddSJeremy L Thompson @param[out] basis_project Address of the variable where the newly created `CeedBasis` will be stored 1287f113e5dcSJeremy L Thompson 1288f113e5dcSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1289f113e5dcSJeremy L Thompson 1290f113e5dcSJeremy L Thompson @ref User 1291f113e5dcSJeremy L Thompson **/ 12922b730f8bSJeremy L Thompson int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project) { 1293f113e5dcSJeremy L Thompson Ceed ceed; 12941c66c397SJeremy L Thompson bool is_tensor; 12951c66c397SJeremy L Thompson CeedInt dim, num_comp; 12961c66c397SJeremy L Thompson CeedScalar *q_ref, *q_weight, *interp_project, *grad_project; 12971c66c397SJeremy L Thompson 12982b730f8bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis_to, &ceed)); 1299f113e5dcSJeremy L Thompson 1300ecc88aebSJeremy L Thompson // Create projection matrix 13012b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateProjectionMatrices(basis_from, basis_to, &interp_project, &grad_project)); 1302f113e5dcSJeremy L Thompson 1303f113e5dcSJeremy L Thompson // Build basis 13042b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis_to, &is_tensor)); 13052b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_to, &dim)); 13062b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_from, &num_comp)); 1307f113e5dcSJeremy L Thompson if (is_tensor) { 1308f113e5dcSJeremy L Thompson CeedInt P_1d_to, P_1d_from; 13091c66c397SJeremy L Thompson 13102b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_1d_from)); 13112b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_1d_to)); 13122b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_to, &q_ref)); 13132b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_to, &q_weight)); 13142b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, interp_project, grad_project, q_ref, q_weight, basis_project)); 1315f113e5dcSJeremy L Thompson } else { 1316de05fbb2SSebastian Grimberg // Even if basis_to and basis_from are not H1, the resulting basis is H1 for interpolation to work 1317f113e5dcSJeremy L Thompson CeedInt num_nodes_to, num_nodes_from; 13181c66c397SJeremy L Thompson CeedElemTopology topo; 13191c66c397SJeremy L Thompson 13201c66c397SJeremy L Thompson CeedCall(CeedBasisGetTopology(basis_to, &topo)); 13212b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_from, &num_nodes_from)); 13222b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_to, &num_nodes_to)); 13232b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_to * dim, &q_ref)); 13242b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_to, &q_weight)); 13252b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, q_ref, q_weight, basis_project)); 1326f113e5dcSJeremy L Thompson } 1327f113e5dcSJeremy L Thompson 1328f113e5dcSJeremy L Thompson // Cleanup 13292b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_project)); 13302b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_project)); 13312b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref)); 13322b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight)); 1333f113e5dcSJeremy L Thompson return CEED_ERROR_SUCCESS; 1334f113e5dcSJeremy L Thompson } 1335f113e5dcSJeremy L Thompson 1336f113e5dcSJeremy L Thompson /** 1337ca94c3ddSJeremy L Thompson @brief Copy the pointer to a `CeedBasis`. 13389560d06aSjeremylt 1339ca94c3ddSJeremy L Thompson Note: If the value of `*basis_copy` passed into this function is non-`NULL`, then it is assumed that `*basis_copy` is a pointer to a `CeedBasis`. 1340ca94c3ddSJeremy L Thompson This `CeedBasis` will be destroyed if `*basis_copy` is the only reference to this `CeedBasis`. 1341ea61e9acSJeremy L Thompson 1342ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to copy reference to 1343ea61e9acSJeremy L Thompson @param[in,out] basis_copy Variable to store copied reference 13449560d06aSjeremylt 13459560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 13469560d06aSjeremylt 13479560d06aSjeremylt @ref User 13489560d06aSjeremylt **/ 13499560d06aSjeremylt int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) { 1350356036faSJeremy L Thompson if (basis != CEED_BASIS_NONE) CeedCall(CeedBasisReference(basis)); 13512b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(basis_copy)); 13529560d06aSjeremylt *basis_copy = basis; 13539560d06aSjeremylt return CEED_ERROR_SUCCESS; 13549560d06aSjeremylt } 13559560d06aSjeremylt 13569560d06aSjeremylt /** 1357ca94c3ddSJeremy L Thompson @brief View a `CeedBasis` 13587a982d89SJeremy L. Thompson 1359ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to view 1360ca94c3ddSJeremy L Thompson @param[in] stream Stream to view to, e.g., `stdout` 13617a982d89SJeremy L. Thompson 13627a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 13637a982d89SJeremy L. Thompson 13647a982d89SJeremy L. Thompson @ref User 13657a982d89SJeremy L. Thompson **/ 13667a982d89SJeremy L. Thompson int CeedBasisView(CeedBasis basis, FILE *stream) { 13671203703bSJeremy L Thompson bool is_tensor_basis; 13681203703bSJeremy L Thompson CeedElemTopology topo; 13691203703bSJeremy L Thompson CeedFESpace fe_space; 13701203703bSJeremy L Thompson 13711203703bSJeremy L Thompson // Basis data 13721203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 13731203703bSJeremy L Thompson CeedCall(CeedBasisGetTopology(basis, &topo)); 13741203703bSJeremy L Thompson CeedCall(CeedBasisGetFESpace(basis, &fe_space)); 13752b730f8bSJeremy L Thompson 137650c301a5SRezgar Shakeri // Print FE space and element topology of the basis 1377edf04919SJeremy L Thompson fprintf(stream, "CeedBasis in a %s on a %s element\n", CeedFESpaces[fe_space], CeedElemTopologies[topo]); 13781203703bSJeremy L Thompson if (is_tensor_basis) { 1379edf04919SJeremy L Thompson fprintf(stream, " P: %" CeedInt_FMT "\n Q: %" CeedInt_FMT "\n", basis->P_1d, basis->Q_1d); 138050c301a5SRezgar Shakeri } else { 1381edf04919SJeremy L Thompson fprintf(stream, " P: %" CeedInt_FMT "\n Q: %" CeedInt_FMT "\n", basis->P, basis->Q); 138250c301a5SRezgar Shakeri } 1383edf04919SJeremy L Thompson fprintf(stream, " dimension: %" CeedInt_FMT "\n field components: %" CeedInt_FMT "\n", basis->dim, basis->num_comp); 1384ea61e9acSJeremy L Thompson // Print quadrature data, interpolation/gradient/divergence/curl of the basis 13851203703bSJeremy L Thompson if (is_tensor_basis) { // tensor basis 13861203703bSJeremy L Thompson CeedInt P_1d, Q_1d; 13871203703bSJeremy L Thompson const CeedScalar *q_ref_1d, *q_weight_1d, *interp_1d, *grad_1d; 13881203703bSJeremy L Thompson 13891203703bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 13901203703bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 13911203703bSJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 13921203703bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d)); 13931203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 13941203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 13951203703bSJeremy L Thompson 13961203703bSJeremy L Thompson CeedCall(CeedScalarView("qref1d", "\t% 12.8f", 1, Q_1d, q_ref_1d, stream)); 13971203703bSJeremy L Thompson CeedCall(CeedScalarView("qweight1d", "\t% 12.8f", 1, Q_1d, q_weight_1d, stream)); 13981203703bSJeremy L Thompson CeedCall(CeedScalarView("interp1d", "\t% 12.8f", Q_1d, P_1d, interp_1d, stream)); 13991203703bSJeremy L Thompson CeedCall(CeedScalarView("grad1d", "\t% 12.8f", Q_1d, P_1d, grad_1d, stream)); 140050c301a5SRezgar Shakeri } else { // non-tensor basis 14011203703bSJeremy L Thompson CeedInt P, Q, dim, q_comp; 14021203703bSJeremy L Thompson const CeedScalar *q_ref, *q_weight, *interp, *grad, *div, *curl; 14031203703bSJeremy L Thompson 14041203703bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &P)); 14051203703bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &Q)); 14061203703bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 14071203703bSJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref)); 14081203703bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight)); 14091203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis, &interp)); 14101203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad(basis, &grad)); 14111203703bSJeremy L Thompson CeedCall(CeedBasisGetDiv(basis, &div)); 14121203703bSJeremy L Thompson CeedCall(CeedBasisGetCurl(basis, &curl)); 14131203703bSJeremy L Thompson 14141203703bSJeremy L Thompson CeedCall(CeedScalarView("qref", "\t% 12.8f", 1, Q * dim, q_ref, stream)); 14151203703bSJeremy L Thompson CeedCall(CeedScalarView("qweight", "\t% 12.8f", 1, Q, q_weight, stream)); 1416c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp)); 14171203703bSJeremy L Thompson CeedCall(CeedScalarView("interp", "\t% 12.8f", q_comp * Q, P, interp, stream)); 14181203703bSJeremy L Thompson if (grad) { 1419c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp)); 14201203703bSJeremy L Thompson CeedCall(CeedScalarView("grad", "\t% 12.8f", q_comp * Q, P, grad, stream)); 14217a982d89SJeremy L. Thompson } 14221203703bSJeremy L Thompson if (div) { 1423c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp)); 14241203703bSJeremy L Thompson CeedCall(CeedScalarView("div", "\t% 12.8f", q_comp * Q, P, div, stream)); 1425c4e3f59bSSebastian Grimberg } 14261203703bSJeremy L Thompson if (curl) { 1427c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp)); 14281203703bSJeremy L Thompson CeedCall(CeedScalarView("curl", "\t% 12.8f", q_comp * Q, P, curl, stream)); 142950c301a5SRezgar Shakeri } 143050c301a5SRezgar Shakeri } 1431e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14327a982d89SJeremy L. Thompson } 14337a982d89SJeremy L. Thompson 14347a982d89SJeremy L. Thompson /** 14357a982d89SJeremy L. Thompson @brief Apply basis evaluation from nodes to quadrature points or vice versa 14367a982d89SJeremy L. Thompson 1437ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1438ea61e9acSJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1439ca94c3ddSJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1440ca94c3ddSJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points; 1441ca94c3ddSJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes 1442ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 1443ca94c3ddSJeremy L Thompson @ref CEED_EVAL_INTERP to use interpolated values, 1444ca94c3ddSJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1445ca94c3ddSJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 1446ca94c3ddSJeremy L Thompson @ref CEED_EVAL_CURL to use curl, 1447ca94c3ddSJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1448ca94c3ddSJeremy L Thompson @param[in] u Input `CeedVector` 1449ca94c3ddSJeremy L Thompson @param[out] v Output `CeedVector` 14507a982d89SJeremy L. Thompson 14517a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 14527a982d89SJeremy L. Thompson 14537a982d89SJeremy L. Thompson @ref User 14547a982d89SJeremy L. Thompson **/ 14552b730f8bSJeremy L Thompson int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 1456c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 14571c66c397SJeremy L Thompson CeedSize u_length = 0, v_length; 14581203703bSJeremy L Thompson Ceed ceed; 14591c66c397SJeremy L Thompson 14601203703bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 14612b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 14622b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 1463c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 14642b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 14652b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 14662b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(v, &v_length)); 1467c8c3fa7dSJeremy L Thompson if (u) CeedCall(CeedVectorGetLength(u, &u_length)); 14687a982d89SJeremy L. Thompson 14691203703bSJeremy L Thompson CeedCheck(basis->Apply, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedBasisApply"); 1470e15f9bd0SJeremy L Thompson 1471e15f9bd0SJeremy L Thompson // Check compatibility of topological and geometrical dimensions 14726574a04fSJeremy L Thompson CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0 && u_length % num_qpts == 0) || 14736574a04fSJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0 && v_length % num_qpts == 0), 14741203703bSJeremy L Thompson ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions"); 14757a982d89SJeremy L. Thompson 1476e15f9bd0SJeremy L Thompson // Check vector lengths to prevent out of bounds issues 147799e754f0SJeremy L Thompson bool has_good_dims = true; 1478d1d35e2fSjeremylt switch (eval_mode) { 1479e15f9bd0SJeremy L Thompson case CEED_EVAL_NONE: 14802b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 14812b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 1482c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 1483c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 148499e754f0SJeremy L Thompson has_good_dims = 14856574a04fSJeremy L Thompson ((t_mode == CEED_TRANSPOSE && u_length >= num_elem * num_comp * num_qpts * q_comp && v_length >= num_elem * num_comp * num_nodes) || 14866574a04fSJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && v_length >= num_elem * num_qpts * num_comp * q_comp && u_length >= num_elem * num_comp * num_nodes)); 1487e15f9bd0SJeremy L Thompson break; 1488e15f9bd0SJeremy L Thompson case CEED_EVAL_WEIGHT: 148999e754f0SJeremy L Thompson has_good_dims = v_length >= num_elem * num_qpts; 1490e15f9bd0SJeremy L Thompson break; 1491e15f9bd0SJeremy L Thompson } 149299e754f0SJeremy L Thompson CeedCheck(has_good_dims, ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 1493e15f9bd0SJeremy L Thompson 14942b730f8bSJeremy L Thompson CeedCall(basis->Apply(basis, num_elem, t_mode, eval_mode, u, v)); 1495e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14967a982d89SJeremy L. Thompson } 14977a982d89SJeremy L. Thompson 14987a982d89SJeremy L. Thompson /** 1499c8c3fa7dSJeremy L Thompson @brief Apply basis evaluation from nodes to arbitrary points 1500c8c3fa7dSJeremy L Thompson 1501ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1502c8c3fa7dSJeremy L Thompson @param[in] num_points The number of points to apply the basis evaluation to 1503ca94c3ddSJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 1504ca94c3ddSJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes 1505ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 1506ca94c3ddSJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1507ca94c3ddSJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1508ca94c3ddSJeremy L Thompson @param[in] x_ref `CeedVector` holding reference coordinates of each point 1509ca94c3ddSJeremy L Thompson @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 1510ca94c3ddSJeremy L Thompson @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 1511c8c3fa7dSJeremy L Thompson 1512c8c3fa7dSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1513c8c3fa7dSJeremy L Thompson 1514c8c3fa7dSJeremy L Thompson @ref User 1515c8c3fa7dSJeremy L Thompson **/ 1516c8c3fa7dSJeremy L Thompson int CeedBasisApplyAtPoints(CeedBasis basis, CeedInt num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, 1517c8c3fa7dSJeremy L Thompson CeedVector v) { 15181203703bSJeremy L Thompson bool is_tensor_basis; 1519c8c3fa7dSJeremy L Thompson CeedInt dim, num_comp, num_q_comp, num_nodes, P_1d = 1, Q_1d = 1; 15201c66c397SJeremy L Thompson CeedSize x_length = 0, u_length = 0, v_length; 15211203703bSJeremy L Thompson Ceed ceed; 1522c8c3fa7dSJeremy L Thompson 15231203703bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 1524c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 1525c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 1526c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 1527c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 1528c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &num_q_comp)); 1529c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 1530c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetLength(v, &v_length)); 1531953190f4SJeremy L Thompson if (x_ref != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(x_ref, &x_length)); 1532953190f4SJeremy L Thompson if (u != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(u, &u_length)); 1533c8c3fa7dSJeremy L Thompson 1534c8c3fa7dSJeremy L Thompson // Check compatibility of topological and geometrical dimensions 1535953190f4SJeremy L Thompson CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0) || (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0) || 1536953190f4SJeremy L Thompson (eval_mode == CEED_EVAL_WEIGHT), 15371203703bSJeremy L Thompson ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions and number of points"); 1538c8c3fa7dSJeremy L Thompson 1539c8c3fa7dSJeremy L Thompson // Check compatibility coordinates vector 15401203703bSJeremy L Thompson CeedCheck((x_length >= num_points * dim) || (eval_mode == CEED_EVAL_WEIGHT), ceed, CEED_ERROR_DIMENSION, 1541c8c3fa7dSJeremy L Thompson "Length of reference coordinate vector incompatible with basis dimension and number of points"); 1542c8c3fa7dSJeremy L Thompson 1543953190f4SJeremy L Thompson // Check CEED_EVAL_WEIGHT only on CEED_NOTRANSPOSE 15441203703bSJeremy L Thompson CeedCheck(eval_mode != CEED_EVAL_WEIGHT || t_mode == CEED_NOTRANSPOSE, ceed, CEED_ERROR_UNSUPPORTED, 1545953190f4SJeremy L Thompson "CEED_EVAL_WEIGHT only supported with CEED_NOTRANSPOSE"); 1546953190f4SJeremy L Thompson 1547c8c3fa7dSJeremy L Thompson // Check vector lengths to prevent out of bounds issues 154899e754f0SJeremy L Thompson bool has_good_dims = true; 1549c8c3fa7dSJeremy L Thompson switch (eval_mode) { 1550c8c3fa7dSJeremy L Thompson case CEED_EVAL_INTERP: 155199e754f0SJeremy L Thompson has_good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= num_points * num_q_comp || v_length >= num_nodes * num_comp)) || 1552c8c3fa7dSJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && (v_length >= num_points * num_q_comp || u_length >= num_nodes * num_comp))); 1553c8c3fa7dSJeremy L Thompson break; 1554c8c3fa7dSJeremy L Thompson case CEED_EVAL_GRAD: 155599e754f0SJeremy L Thompson has_good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= num_points * num_q_comp * dim || v_length >= num_nodes * num_comp)) || 1556edfbf3a6SJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && (v_length >= num_points * num_q_comp * dim || u_length >= num_nodes * num_comp))); 1557edfbf3a6SJeremy L Thompson break; 1558c8c3fa7dSJeremy L Thompson case CEED_EVAL_WEIGHT: 155999e754f0SJeremy L Thompson has_good_dims = t_mode == CEED_NOTRANSPOSE && (v_length >= num_points); 1560953190f4SJeremy L Thompson break; 156199e754f0SJeremy L Thompson // LCOV_EXCL_START 1562953190f4SJeremy L Thompson case CEED_EVAL_NONE: 1563c8c3fa7dSJeremy L Thompson case CEED_EVAL_DIV: 1564c8c3fa7dSJeremy L Thompson case CEED_EVAL_CURL: 15651203703bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points not supported for %s", CeedEvalModes[eval_mode]); 1566c8c3fa7dSJeremy L Thompson // LCOV_EXCL_STOP 1567c8c3fa7dSJeremy L Thompson } 156899e754f0SJeremy L Thompson CeedCheck(has_good_dims, ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 1569c8c3fa7dSJeremy L Thompson 1570c8c3fa7dSJeremy L Thompson // Backend method 1571c8c3fa7dSJeremy L Thompson if (basis->ApplyAtPoints) { 1572c8c3fa7dSJeremy L Thompson CeedCall(basis->ApplyAtPoints(basis, num_points, t_mode, eval_mode, x_ref, u, v)); 1573c8c3fa7dSJeremy L Thompson return CEED_ERROR_SUCCESS; 1574c8c3fa7dSJeremy L Thompson } 1575c8c3fa7dSJeremy L Thompson 1576c8c3fa7dSJeremy L Thompson // Default implementation 15771203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 15781203703bSJeremy L Thompson CeedCheck(is_tensor_basis, ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points only supported for tensor product bases"); 1579953190f4SJeremy L Thompson if (eval_mode == CEED_EVAL_WEIGHT) { 1580953190f4SJeremy L Thompson CeedCall(CeedVectorSetValue(v, 1.0)); 1581953190f4SJeremy L Thompson return CEED_ERROR_SUCCESS; 1582953190f4SJeremy L Thompson } 1583c8c3fa7dSJeremy L Thompson if (!basis->basis_chebyshev) { 1584c8c3fa7dSJeremy L Thompson // Build matrix mapping from quadrature point values to Chebyshev coefficients 15852247a93fSRezgar Shakeri CeedScalar *C, *chebyshev_coeffs_1d_inv; 1586c8c3fa7dSJeremy L Thompson const CeedScalar *q_ref_1d; 1587c8c3fa7dSJeremy L Thompson 1588c8c3fa7dSJeremy L Thompson // Build coefficient matrix 1589c8c3fa7dSJeremy L Thompson // -- Note: Clang-tidy needs this check because it does not understand the is_tensor_basis check above 15901203703bSJeremy L Thompson CeedCheck(P_1d > 0 && Q_1d > 0, ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis dimensions are malformed"); 1591c8c3fa7dSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * Q_1d, &C)); 1592c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 15933778dbaaSJeremy L Thompson for (CeedInt i = 0; i < Q_1d; i++) CeedCall(CeedChebyshevPolynomialsAtPoint(q_ref_1d[i], Q_1d, &C[i * Q_1d])); 1594c8c3fa7dSJeremy L Thompson 15952247a93fSRezgar Shakeri // Compute C^+, pseudoinverse of coefficient matrix 15962247a93fSRezgar Shakeri CeedCall(CeedCalloc(Q_1d * Q_1d, &chebyshev_coeffs_1d_inv)); 15971203703bSJeremy L Thompson CeedCall(CeedMatrixPseudoinverse(ceed, C, Q_1d, Q_1d, chebyshev_coeffs_1d_inv)); 1598c8c3fa7dSJeremy L Thompson 1599c8c3fa7dSJeremy L Thompson // Build basis mapping from nodes to Chebyshev coefficients 1600c8c3fa7dSJeremy L Thompson CeedScalar *chebyshev_interp_1d, *chebyshev_grad_1d, *chebyshev_q_weight_1d; 1601c8c3fa7dSJeremy L Thompson const CeedScalar *interp_1d; 1602c8c3fa7dSJeremy L Thompson 160371a83b88SJeremy L Thompson CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 160471a83b88SJeremy L Thompson CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_grad_1d)); 1605c8c3fa7dSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &chebyshev_q_weight_1d)); 1606c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 16071203703bSJeremy L Thompson CeedCall(CeedMatrixMatrixMultiply(ceed, chebyshev_coeffs_1d_inv, interp_1d, chebyshev_interp_1d, Q_1d, P_1d, Q_1d)); 1608c8c3fa7dSJeremy L Thompson 16091203703bSJeremy L Thompson CeedCall(CeedVectorCreate(ceed, num_comp * CeedIntPow(Q_1d, dim), &basis->vec_chebyshev)); 16101203703bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d, Q_1d, chebyshev_interp_1d, chebyshev_grad_1d, q_ref_1d, chebyshev_q_weight_1d, 1611c8c3fa7dSJeremy L Thompson &basis->basis_chebyshev)); 1612c8c3fa7dSJeremy L Thompson 1613c8c3fa7dSJeremy L Thompson // Cleanup 1614c8c3fa7dSJeremy L Thompson CeedCall(CeedFree(&C)); 16152247a93fSRezgar Shakeri CeedCall(CeedFree(&chebyshev_coeffs_1d_inv)); 1616c8c3fa7dSJeremy L Thompson CeedCall(CeedFree(&chebyshev_interp_1d)); 1617c8c3fa7dSJeremy L Thompson CeedCall(CeedFree(&chebyshev_grad_1d)); 1618c8c3fa7dSJeremy L Thompson CeedCall(CeedFree(&chebyshev_q_weight_1d)); 1619c8c3fa7dSJeremy L Thompson } 1620c8c3fa7dSJeremy L Thompson 1621c8c3fa7dSJeremy L Thompson // Create TensorContract object if needed, such as a basis from the GPU backends 1622c8c3fa7dSJeremy L Thompson if (!basis->contract) { 1623c8c3fa7dSJeremy L Thompson Ceed ceed_ref; 1624585a562dSJeremy L Thompson CeedBasis basis_ref = NULL; 1625c8c3fa7dSJeremy L Thompson 1626c8c3fa7dSJeremy L Thompson CeedCall(CeedInit("/cpu/self", &ceed_ref)); 1627c8c3fa7dSJeremy L Thompson // Only need matching tensor contraction dimensions, any type of basis will work 162871a83b88SJeremy L Thompson CeedCall(CeedBasisCreateTensorH1Lagrange(ceed_ref, dim, num_comp, P_1d, Q_1d, CEED_GAUSS, &basis_ref)); 1629585a562dSJeremy L Thompson // Note - clang-tidy doesn't know basis_ref->contract must be valid here 16301203703bSJeremy L Thompson CeedCheck(basis_ref && basis_ref->contract, ceed, CEED_ERROR_UNSUPPORTED, "Reference CPU ceed failed to create a tensor contraction object"); 1631585a562dSJeremy L Thompson CeedCall(CeedTensorContractReferenceCopy(basis_ref->contract, &basis->contract)); 1632c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisDestroy(&basis_ref)); 1633c8c3fa7dSJeremy L Thompson CeedCall(CeedDestroy(&ceed_ref)); 1634c8c3fa7dSJeremy L Thompson } 1635c8c3fa7dSJeremy L Thompson 1636c8c3fa7dSJeremy L Thompson // Basis evaluation 1637c8c3fa7dSJeremy L Thompson switch (t_mode) { 1638c8c3fa7dSJeremy L Thompson case CEED_NOTRANSPOSE: { 1639c8c3fa7dSJeremy L Thompson // Nodes to arbitrary points 1640c8c3fa7dSJeremy L Thompson CeedScalar *v_array; 1641c8c3fa7dSJeremy L Thompson const CeedScalar *chebyshev_coeffs, *x_array_read; 1642c8c3fa7dSJeremy L Thompson 1643c8c3fa7dSJeremy L Thompson // -- Interpolate to Chebyshev coefficients 1644c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, basis->vec_chebyshev)); 1645c8c3fa7dSJeremy L Thompson 1646c8c3fa7dSJeremy L Thompson // -- Evaluate Chebyshev polynomials at arbitrary points 1647c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs)); 1648c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read)); 1649c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(v, CEED_MEM_HOST, &v_array)); 1650edfbf3a6SJeremy L Thompson switch (eval_mode) { 1651edfbf3a6SJeremy L Thompson case CEED_EVAL_INTERP: { 1652c8c3fa7dSJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 1653c8c3fa7dSJeremy L Thompson 1654c8c3fa7dSJeremy L Thompson // ---- Values at point 1655c8c3fa7dSJeremy L Thompson for (CeedInt p = 0; p < num_points; p++) { 1656c8c3fa7dSJeremy L Thompson CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1; 1657c8c3fa7dSJeremy L Thompson 165853ef2869SZach Atkins for (CeedInt d = 0; d < dim; d++) { 16593778dbaaSJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 16609c34f28eSJeremy L Thompson CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x)); 1661c8c3fa7dSJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false, 16624608bdaaSJeremy L Thompson d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2])); 1663c8c3fa7dSJeremy L Thompson pre /= Q_1d; 1664c8c3fa7dSJeremy L Thompson post *= 1; 1665c8c3fa7dSJeremy L Thompson } 16664608bdaaSJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) v_array[c * num_points + p] = tmp[dim % 2][c]; 1667c8c3fa7dSJeremy L Thompson } 1668edfbf3a6SJeremy L Thompson break; 1669edfbf3a6SJeremy L Thompson } 1670edfbf3a6SJeremy L Thompson case CEED_EVAL_GRAD: { 1671edfbf3a6SJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 1672edfbf3a6SJeremy L Thompson 1673edfbf3a6SJeremy L Thompson // ---- Values at point 1674edfbf3a6SJeremy L Thompson for (CeedInt p = 0; p < num_points; p++) { 1675edfbf3a6SJeremy L Thompson // Dim**2 contractions, apply grad when pass == dim 167653ef2869SZach Atkins for (CeedInt pass = 0; pass < dim; pass++) { 1677edfbf3a6SJeremy L Thompson CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1; 1678edfbf3a6SJeremy L Thompson 167953ef2869SZach Atkins for (CeedInt d = 0; d < dim; d++) { 16803778dbaaSJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 16819c34f28eSJeremy L Thompson if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x)); 16829c34f28eSJeremy L Thompson else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x)); 1683edfbf3a6SJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false, 16844608bdaaSJeremy L Thompson d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2])); 1685edfbf3a6SJeremy L Thompson pre /= Q_1d; 1686edfbf3a6SJeremy L Thompson post *= 1; 1687edfbf3a6SJeremy L Thompson } 16884608bdaaSJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) v_array[(pass * num_comp + c) * num_points + p] = tmp[dim % 2][c]; 1689edfbf3a6SJeremy L Thompson } 1690edfbf3a6SJeremy L Thompson } 1691edfbf3a6SJeremy L Thompson break; 1692edfbf3a6SJeremy L Thompson } 1693edfbf3a6SJeremy L Thompson default: 1694953190f4SJeremy L Thompson // Nothing to do, excluded above 1695edfbf3a6SJeremy L Thompson break; 1696c8c3fa7dSJeremy L Thompson } 1697c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(basis->vec_chebyshev, &chebyshev_coeffs)); 1698c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read)); 1699c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorRestoreArray(v, &v_array)); 1700c8c3fa7dSJeremy L Thompson break; 1701c8c3fa7dSJeremy L Thompson } 17022a94f45fSJeremy L Thompson case CEED_TRANSPOSE: { 17033778dbaaSJeremy L Thompson // Note: No switch on e_mode here because only CEED_EVAL_INTERP is supported at this time 17042a94f45fSJeremy L Thompson // Arbitrary points to nodes 17052a94f45fSJeremy L Thompson CeedScalar *chebyshev_coeffs; 17062a94f45fSJeremy L Thompson const CeedScalar *u_array, *x_array_read; 17072a94f45fSJeremy L Thompson 17081c66c397SJeremy L Thompson // -- Transpose of evaluation of Chebyshev polynomials at arbitrary points 17092a94f45fSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs)); 17102a94f45fSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read)); 17112a94f45fSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array)); 1712038a8942SZach Atkins 1713038a8942SZach Atkins switch (eval_mode) { 1714038a8942SZach Atkins case CEED_EVAL_INTERP: { 17152a94f45fSJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 17162a94f45fSJeremy L Thompson 17172a94f45fSJeremy L Thompson // ---- Values at point 17182a94f45fSJeremy L Thompson for (CeedInt p = 0; p < num_points; p++) { 17192a94f45fSJeremy L Thompson CeedInt pre = num_comp * 1, post = 1; 17202a94f45fSJeremy L Thompson 17214608bdaaSJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[c * num_points + p]; 172253ef2869SZach Atkins for (CeedInt d = 0; d < dim; d++) { 17233778dbaaSJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 17249c34f28eSJeremy L Thompson CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x)); 17254608bdaaSJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, p > 0 && d == (dim - 1), tmp[d % 2], 17264608bdaaSJeremy L Thompson d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2])); 17272a94f45fSJeremy L Thompson pre /= 1; 17282a94f45fSJeremy L Thompson post *= Q_1d; 17292a94f45fSJeremy L Thompson } 17302a94f45fSJeremy L Thompson } 1731038a8942SZach Atkins break; 1732038a8942SZach Atkins } 1733038a8942SZach Atkins case CEED_EVAL_GRAD: { 1734038a8942SZach Atkins CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 1735038a8942SZach Atkins 1736038a8942SZach Atkins // ---- Values at point 1737038a8942SZach Atkins for (CeedInt p = 0; p < num_points; p++) { 1738038a8942SZach Atkins // Dim**2 contractions, apply grad when pass == dim 1739038a8942SZach Atkins for (CeedInt pass = 0; pass < dim; pass++) { 1740038a8942SZach Atkins CeedInt pre = num_comp * 1, post = 1; 1741038a8942SZach Atkins 17424608bdaaSJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[(pass * num_comp + c) * num_points + p]; 1743038a8942SZach Atkins for (CeedInt d = 0; d < dim; d++) { 1744038a8942SZach Atkins // ------ Tensor contract with current Chebyshev polynomial values 17459c34f28eSJeremy L Thompson if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x)); 17469c34f28eSJeremy L Thompson else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x)); 17474608bdaaSJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, 17484608bdaaSJeremy L Thompson (p > 0 || (p == 0 && pass > 0)) && d == (dim - 1), tmp[d % 2], 17494608bdaaSJeremy L Thompson d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2])); 1750038a8942SZach Atkins pre /= 1; 1751038a8942SZach Atkins post *= Q_1d; 1752038a8942SZach Atkins } 1753038a8942SZach Atkins } 1754038a8942SZach Atkins } 1755038a8942SZach Atkins break; 1756038a8942SZach Atkins } 1757038a8942SZach Atkins default: 1758038a8942SZach Atkins // Nothing to do, excluded above 1759038a8942SZach Atkins break; 17602a94f45fSJeremy L Thompson } 17612a94f45fSJeremy L Thompson CeedCall(CeedVectorRestoreArray(basis->vec_chebyshev, &chebyshev_coeffs)); 17622a94f45fSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read)); 17632a94f45fSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(u, &u_array)); 17642a94f45fSJeremy L Thompson 17652a94f45fSJeremy L Thompson // -- Interpolate transpose from Chebyshev coefficients 17662a94f45fSJeremy L Thompson CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v)); 17672a94f45fSJeremy L Thompson break; 17682a94f45fSJeremy L Thompson } 1769c8c3fa7dSJeremy L Thompson } 1770c8c3fa7dSJeremy L Thompson return CEED_ERROR_SUCCESS; 1771c8c3fa7dSJeremy L Thompson } 1772c8c3fa7dSJeremy L Thompson 1773c8c3fa7dSJeremy L Thompson /** 17746e536b99SJeremy L Thompson @brief Get the `Ceed` associated with a `CeedBasis` 1775b7c9bbdaSJeremy L Thompson 1776ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1777ca94c3ddSJeremy L Thompson @param[out] ceed Variable to store `Ceed` 1778b7c9bbdaSJeremy L Thompson 1779b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1780b7c9bbdaSJeremy L Thompson 1781b7c9bbdaSJeremy L Thompson @ref Advanced 1782b7c9bbdaSJeremy L Thompson **/ 1783b7c9bbdaSJeremy L Thompson int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) { 17846e536b99SJeremy L Thompson *ceed = CeedBasisReturnCeed(basis); 1785b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1786b7c9bbdaSJeremy L Thompson } 1787b7c9bbdaSJeremy L Thompson 1788b7c9bbdaSJeremy L Thompson /** 17896e536b99SJeremy L Thompson @brief Return the `Ceed` associated with a `CeedBasis` 17906e536b99SJeremy L Thompson 17916e536b99SJeremy L Thompson @param[in] basis `CeedBasis` 17926e536b99SJeremy L Thompson 17936e536b99SJeremy L Thompson @return `Ceed` associated with the `basis` 17946e536b99SJeremy L Thompson 17956e536b99SJeremy L Thompson @ref Advanced 17966e536b99SJeremy L Thompson **/ 17976e536b99SJeremy L Thompson Ceed CeedBasisReturnCeed(CeedBasis basis) { return basis->ceed; } 17986e536b99SJeremy L Thompson 17996e536b99SJeremy L Thompson /** 1800ca94c3ddSJeremy L Thompson @brief Get dimension for given `CeedBasis` 18019d007619Sjeremylt 1802ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 18039d007619Sjeremylt @param[out] dim Variable to store dimension of basis 18049d007619Sjeremylt 18059d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18069d007619Sjeremylt 1807b7c9bbdaSJeremy L Thompson @ref Advanced 18089d007619Sjeremylt **/ 18099d007619Sjeremylt int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) { 18109d007619Sjeremylt *dim = basis->dim; 1811e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18129d007619Sjeremylt } 18139d007619Sjeremylt 18149d007619Sjeremylt /** 1815ca94c3ddSJeremy L Thompson @brief Get topology for given `CeedBasis` 1816d99fa3c5SJeremy L Thompson 1817ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1818d99fa3c5SJeremy L Thompson @param[out] topo Variable to store topology of basis 1819d99fa3c5SJeremy L Thompson 1820d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1821d99fa3c5SJeremy L Thompson 1822b7c9bbdaSJeremy L Thompson @ref Advanced 1823d99fa3c5SJeremy L Thompson **/ 1824d99fa3c5SJeremy L Thompson int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) { 1825d99fa3c5SJeremy L Thompson *topo = basis->topo; 1826e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1827d99fa3c5SJeremy L Thompson } 1828d99fa3c5SJeremy L Thompson 1829d99fa3c5SJeremy L Thompson /** 1830ca94c3ddSJeremy L Thompson @brief Get number of components for given `CeedBasis` 18319d007619Sjeremylt 1832ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1833ca94c3ddSJeremy L Thompson @param[out] num_comp Variable to store number of components 18349d007619Sjeremylt 18359d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18369d007619Sjeremylt 1837b7c9bbdaSJeremy L Thompson @ref Advanced 18389d007619Sjeremylt **/ 1839d1d35e2fSjeremylt int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) { 1840d1d35e2fSjeremylt *num_comp = basis->num_comp; 1841e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18429d007619Sjeremylt } 18439d007619Sjeremylt 18449d007619Sjeremylt /** 1845ca94c3ddSJeremy L Thompson @brief Get total number of nodes (in `dim` dimensions) of a `CeedBasis` 18469d007619Sjeremylt 1847ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 18489d007619Sjeremylt @param[out] P Variable to store number of nodes 18499d007619Sjeremylt 18509d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18519d007619Sjeremylt 18529d007619Sjeremylt @ref Utility 18539d007619Sjeremylt **/ 18549d007619Sjeremylt int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) { 18559d007619Sjeremylt *P = basis->P; 1856e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18579d007619Sjeremylt } 18589d007619Sjeremylt 18599d007619Sjeremylt /** 1860ca94c3ddSJeremy L Thompson @brief Get total number of nodes (in 1 dimension) of a `CeedBasis` 18619d007619Sjeremylt 1862ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1863d1d35e2fSjeremylt @param[out] P_1d Variable to store number of nodes 18649d007619Sjeremylt 18659d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18669d007619Sjeremylt 1867b7c9bbdaSJeremy L Thompson @ref Advanced 18689d007619Sjeremylt **/ 1869d1d35e2fSjeremylt int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) { 18706e536b99SJeremy L Thompson CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply P_1d for non-tensor CeedBasis"); 1871d1d35e2fSjeremylt *P_1d = basis->P_1d; 1872e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18739d007619Sjeremylt } 18749d007619Sjeremylt 18759d007619Sjeremylt /** 1876ca94c3ddSJeremy L Thompson @brief Get total number of quadrature points (in `dim` dimensions) of a `CeedBasis` 18779d007619Sjeremylt 1878ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 18799d007619Sjeremylt @param[out] Q Variable to store number of quadrature points 18809d007619Sjeremylt 18819d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18829d007619Sjeremylt 18839d007619Sjeremylt @ref Utility 18849d007619Sjeremylt **/ 18859d007619Sjeremylt int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) { 18869d007619Sjeremylt *Q = basis->Q; 1887e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18889d007619Sjeremylt } 18899d007619Sjeremylt 18909d007619Sjeremylt /** 1891ca94c3ddSJeremy L Thompson @brief Get total number of quadrature points (in 1 dimension) of a `CeedBasis` 18929d007619Sjeremylt 1893ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1894d1d35e2fSjeremylt @param[out] Q_1d Variable to store number of quadrature points 18959d007619Sjeremylt 18969d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18979d007619Sjeremylt 1898b7c9bbdaSJeremy L Thompson @ref Advanced 18999d007619Sjeremylt **/ 1900d1d35e2fSjeremylt int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) { 19016e536b99SJeremy L Thompson CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply Q_1d for non-tensor CeedBasis"); 1902d1d35e2fSjeremylt *Q_1d = basis->Q_1d; 1903e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19049d007619Sjeremylt } 19059d007619Sjeremylt 19069d007619Sjeremylt /** 1907ca94c3ddSJeremy L Thompson @brief Get reference coordinates of quadrature points (in `dim` dimensions) of a `CeedBasis` 19089d007619Sjeremylt 1909ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1910d1d35e2fSjeremylt @param[out] q_ref Variable to store reference coordinates of quadrature points 19119d007619Sjeremylt 19129d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19139d007619Sjeremylt 1914b7c9bbdaSJeremy L Thompson @ref Advanced 19159d007619Sjeremylt **/ 1916d1d35e2fSjeremylt int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) { 1917d1d35e2fSjeremylt *q_ref = basis->q_ref_1d; 1918e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19199d007619Sjeremylt } 19209d007619Sjeremylt 19219d007619Sjeremylt /** 1922ca94c3ddSJeremy L Thompson @brief Get quadrature weights of quadrature points (in `dim` dimensions) of a `CeedBasis` 19239d007619Sjeremylt 1924ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1925d1d35e2fSjeremylt @param[out] q_weight Variable to store quadrature weights 19269d007619Sjeremylt 19279d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19289d007619Sjeremylt 1929b7c9bbdaSJeremy L Thompson @ref Advanced 19309d007619Sjeremylt **/ 1931d1d35e2fSjeremylt int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) { 1932d1d35e2fSjeremylt *q_weight = basis->q_weight_1d; 1933e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19349d007619Sjeremylt } 19359d007619Sjeremylt 19369d007619Sjeremylt /** 1937ca94c3ddSJeremy L Thompson @brief Get interpolation matrix of a `CeedBasis` 19389d007619Sjeremylt 1939ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 19409d007619Sjeremylt @param[out] interp Variable to store interpolation matrix 19419d007619Sjeremylt 19429d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19439d007619Sjeremylt 1944b7c9bbdaSJeremy L Thompson @ref Advanced 19459d007619Sjeremylt **/ 19466c58de82SJeremy L Thompson int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) { 19476402da51SJeremy L Thompson if (!basis->interp && basis->is_tensor_basis) { 19489d007619Sjeremylt // Allocate 19492b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->Q * basis->P, &basis->interp)); 19509d007619Sjeremylt 19519d007619Sjeremylt // Initialize 19522b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->Q * basis->P; i++) basis->interp[i] = 1.0; 19539d007619Sjeremylt 19549d007619Sjeremylt // Calculate 19552b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 19562b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 19579d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 1958d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 1959d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 19601c66c397SJeremy L Thompson 1961d1d35e2fSjeremylt basis->interp[qpt * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 19629d007619Sjeremylt } 19639d007619Sjeremylt } 19642b730f8bSJeremy L Thompson } 19652b730f8bSJeremy L Thompson } 19669d007619Sjeremylt *interp = basis->interp; 1967e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19689d007619Sjeremylt } 19699d007619Sjeremylt 19709d007619Sjeremylt /** 1971ca94c3ddSJeremy L Thompson @brief Get 1D interpolation matrix of a tensor product `CeedBasis` 19729d007619Sjeremylt 1973ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1974d1d35e2fSjeremylt @param[out] interp_1d Variable to store interpolation matrix 19759d007619Sjeremylt 19769d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19779d007619Sjeremylt 19789d007619Sjeremylt @ref Backend 19799d007619Sjeremylt **/ 1980d1d35e2fSjeremylt int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) { 19811203703bSJeremy L Thompson bool is_tensor_basis; 19821203703bSJeremy L Thompson 19831203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 19846e536b99SJeremy L Thompson CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis"); 1985d1d35e2fSjeremylt *interp_1d = basis->interp_1d; 1986e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19879d007619Sjeremylt } 19889d007619Sjeremylt 19899d007619Sjeremylt /** 1990ca94c3ddSJeremy L Thompson @brief Get gradient matrix of a `CeedBasis` 19919d007619Sjeremylt 1992ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 19939d007619Sjeremylt @param[out] grad Variable to store gradient matrix 19949d007619Sjeremylt 19959d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19969d007619Sjeremylt 1997b7c9bbdaSJeremy L Thompson @ref Advanced 19989d007619Sjeremylt **/ 19996c58de82SJeremy L Thompson int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) { 20006402da51SJeremy L Thompson if (!basis->grad && basis->is_tensor_basis) { 20019d007619Sjeremylt // Allocate 20022b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->dim * basis->Q * basis->P, &basis->grad)); 20039d007619Sjeremylt 20049d007619Sjeremylt // Initialize 20052b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim * basis->Q * basis->P; i++) basis->grad[i] = 1.0; 20069d007619Sjeremylt 20079d007619Sjeremylt // Calculate 20082b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 20092b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim; i++) { 20102b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 20119d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 2012d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 2013d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 20141c66c397SJeremy L Thompson 20152b730f8bSJeremy L Thompson if (i == d) basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->grad_1d[q * basis->P_1d + p]; 20162b730f8bSJeremy L Thompson else basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 20172b730f8bSJeremy L Thompson } 20182b730f8bSJeremy L Thompson } 20192b730f8bSJeremy L Thompson } 20209d007619Sjeremylt } 20219d007619Sjeremylt } 20229d007619Sjeremylt *grad = basis->grad; 2023e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20249d007619Sjeremylt } 20259d007619Sjeremylt 20269d007619Sjeremylt /** 2027ca94c3ddSJeremy L Thompson @brief Get 1D gradient matrix of a tensor product `CeedBasis` 20289d007619Sjeremylt 2029ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2030d1d35e2fSjeremylt @param[out] grad_1d Variable to store gradient matrix 20319d007619Sjeremylt 20329d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20339d007619Sjeremylt 2034b7c9bbdaSJeremy L Thompson @ref Advanced 20359d007619Sjeremylt **/ 2036d1d35e2fSjeremylt int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) { 20371203703bSJeremy L Thompson bool is_tensor_basis; 20381203703bSJeremy L Thompson 20391203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 20406e536b99SJeremy L Thompson CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis"); 2041d1d35e2fSjeremylt *grad_1d = basis->grad_1d; 2042e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20439d007619Sjeremylt } 20449d007619Sjeremylt 20459d007619Sjeremylt /** 2046ca94c3ddSJeremy L Thompson @brief Get divergence matrix of a `CeedBasis` 204750c301a5SRezgar Shakeri 2048ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 204950c301a5SRezgar Shakeri @param[out] div Variable to store divergence matrix 205050c301a5SRezgar Shakeri 205150c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 205250c301a5SRezgar Shakeri 205350c301a5SRezgar Shakeri @ref Advanced 205450c301a5SRezgar Shakeri **/ 205550c301a5SRezgar Shakeri int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) { 205650c301a5SRezgar Shakeri *div = basis->div; 205750c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 205850c301a5SRezgar Shakeri } 205950c301a5SRezgar Shakeri 206050c301a5SRezgar Shakeri /** 2061ca94c3ddSJeremy L Thompson @brief Get curl matrix of a `CeedBasis` 2062c4e3f59bSSebastian Grimberg 2063ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2064c4e3f59bSSebastian Grimberg @param[out] curl Variable to store curl matrix 2065c4e3f59bSSebastian Grimberg 2066c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 2067c4e3f59bSSebastian Grimberg 2068c4e3f59bSSebastian Grimberg @ref Advanced 2069c4e3f59bSSebastian Grimberg **/ 2070c4e3f59bSSebastian Grimberg int CeedBasisGetCurl(CeedBasis basis, const CeedScalar **curl) { 2071c4e3f59bSSebastian Grimberg *curl = basis->curl; 2072c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 2073c4e3f59bSSebastian Grimberg } 2074c4e3f59bSSebastian Grimberg 2075c4e3f59bSSebastian Grimberg /** 2076ca94c3ddSJeremy L Thompson @brief Destroy a @ref CeedBasis 20777a982d89SJeremy L. Thompson 2078ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` to destroy 20797a982d89SJeremy L. Thompson 20807a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 20817a982d89SJeremy L. Thompson 20827a982d89SJeremy L. Thompson @ref User 20837a982d89SJeremy L. Thompson **/ 20847a982d89SJeremy L. Thompson int CeedBasisDestroy(CeedBasis *basis) { 2085356036faSJeremy L Thompson if (!*basis || *basis == CEED_BASIS_NONE || --(*basis)->ref_count > 0) { 2086ad6481ceSJeremy L Thompson *basis = NULL; 2087ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 2088ad6481ceSJeremy L Thompson } 20892b730f8bSJeremy L Thompson if ((*basis)->Destroy) CeedCall((*basis)->Destroy(*basis)); 20909831d45aSJeremy L Thompson CeedCall(CeedTensorContractDestroy(&(*basis)->contract)); 2091c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_ref_1d)); 2092c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_weight_1d)); 20932b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp)); 20942b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp_1d)); 20952b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad)); 20962b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad_1d)); 2097c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->div)); 2098c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->curl)); 2099c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorDestroy(&(*basis)->vec_chebyshev)); 2100c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisDestroy(&(*basis)->basis_chebyshev)); 21012b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*basis)->ceed)); 21022b730f8bSJeremy L Thompson CeedCall(CeedFree(basis)); 2103e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21047a982d89SJeremy L. Thompson } 21057a982d89SJeremy L. Thompson 21067a982d89SJeremy L. Thompson /** 2107b11c1e72Sjeremylt @brief Construct a Gauss-Legendre quadrature 2108b11c1e72Sjeremylt 2109ca94c3ddSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree `2*Q-1` exactly) 2110ca94c3ddSJeremy L Thompson @param[out] q_ref_1d Array of length `Q` to hold the abscissa on `[-1, 1]` 2111ca94c3ddSJeremy L Thompson @param[out] q_weight_1d Array of length `Q` to hold the weights 2112b11c1e72Sjeremylt 2113b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 2114dfdf5a53Sjeremylt 2115dfdf5a53Sjeremylt @ref Utility 2116b11c1e72Sjeremylt **/ 21172b730f8bSJeremy L Thompson int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2118d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0 * atan(1.0); 21191c66c397SJeremy L Thompson 2120d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 212192ae7e47SJeremy L Thompson for (CeedInt i = 0; i <= Q / 2; i++) { 2122d7b241e6Sjeremylt // Guess 2123d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(2 * i + 1) / ((CeedScalar)(2 * Q))); 2124d7b241e6Sjeremylt // Pn(xi) 2125d7b241e6Sjeremylt P0 = 1.0; 2126d7b241e6Sjeremylt P1 = xi; 2127d7b241e6Sjeremylt P2 = 0.0; 212892ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 2129d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2130d7b241e6Sjeremylt P0 = P1; 2131d7b241e6Sjeremylt P1 = P2; 2132d7b241e6Sjeremylt } 2133d7b241e6Sjeremylt // First Newton Step 2134d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2135d7b241e6Sjeremylt xi = xi - P2 / dP2; 2136d7b241e6Sjeremylt // Newton to convergence 213792ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(P2) > 10 * CEED_EPSILON; k++) { 2138d7b241e6Sjeremylt P0 = 1.0; 2139d7b241e6Sjeremylt P1 = xi; 214092ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 2141d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2142d7b241e6Sjeremylt P0 = P1; 2143d7b241e6Sjeremylt P1 = P2; 2144d7b241e6Sjeremylt } 2145d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2146d7b241e6Sjeremylt xi = xi - P2 / dP2; 2147d7b241e6Sjeremylt } 2148d7b241e6Sjeremylt // Save xi, wi 2149d7b241e6Sjeremylt wi = 2.0 / ((1.0 - xi * xi) * dP2 * dP2); 2150d1d35e2fSjeremylt q_weight_1d[i] = wi; 2151d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 2152d1d35e2fSjeremylt q_ref_1d[i] = -xi; 2153d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 2154d7b241e6Sjeremylt } 2155e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2156d7b241e6Sjeremylt } 2157d7b241e6Sjeremylt 2158b11c1e72Sjeremylt /** 2159b11c1e72Sjeremylt @brief Construct a Gauss-Legendre-Lobatto quadrature 2160b11c1e72Sjeremylt 2161ca94c3ddSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree `2*Q-3` exactly) 2162ca94c3ddSJeremy L Thompson @param[out] q_ref_1d Array of length `Q` to hold the abscissa on `[-1, 1]` 2163ca94c3ddSJeremy L Thompson @param[out] q_weight_1d Array of length `Q` to hold the weights 2164b11c1e72Sjeremylt 2165b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 2166dfdf5a53Sjeremylt 2167dfdf5a53Sjeremylt @ref Utility 2168b11c1e72Sjeremylt **/ 21692b730f8bSJeremy L Thompson int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2170d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0 * atan(1.0); 21711c66c397SJeremy L Thompson 2172d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 2173d7b241e6Sjeremylt // Set endpoints 21746574a04fSJeremy L Thompson CeedCheck(Q > 1, NULL, CEED_ERROR_DIMENSION, "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q); 2175d7b241e6Sjeremylt wi = 2.0 / ((CeedScalar)(Q * (Q - 1))); 2176d1d35e2fSjeremylt if (q_weight_1d) { 2177d1d35e2fSjeremylt q_weight_1d[0] = wi; 2178d1d35e2fSjeremylt q_weight_1d[Q - 1] = wi; 2179d7b241e6Sjeremylt } 2180d1d35e2fSjeremylt q_ref_1d[0] = -1.0; 2181d1d35e2fSjeremylt q_ref_1d[Q - 1] = 1.0; 2182d7b241e6Sjeremylt // Interior 218392ae7e47SJeremy L Thompson for (CeedInt i = 1; i <= (Q - 1) / 2; i++) { 2184d7b241e6Sjeremylt // Guess 2185d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(i) / (CeedScalar)(Q - 1)); 2186d7b241e6Sjeremylt // Pn(xi) 2187d7b241e6Sjeremylt P0 = 1.0; 2188d7b241e6Sjeremylt P1 = xi; 2189d7b241e6Sjeremylt P2 = 0.0; 219092ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 2191d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2192d7b241e6Sjeremylt P0 = P1; 2193d7b241e6Sjeremylt P1 = P2; 2194d7b241e6Sjeremylt } 2195d7b241e6Sjeremylt // First Newton step 2196d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2197d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2198d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 2199d7b241e6Sjeremylt // Newton to convergence 220092ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(dP2) > 10 * CEED_EPSILON; k++) { 2201d7b241e6Sjeremylt P0 = 1.0; 2202d7b241e6Sjeremylt P1 = xi; 220392ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 2204d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2205d7b241e6Sjeremylt P0 = P1; 2206d7b241e6Sjeremylt P1 = P2; 2207d7b241e6Sjeremylt } 2208d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2209d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2210d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 2211d7b241e6Sjeremylt } 2212d7b241e6Sjeremylt // Save xi, wi 2213d7b241e6Sjeremylt wi = 2.0 / (((CeedScalar)(Q * (Q - 1))) * P2 * P2); 2214d1d35e2fSjeremylt if (q_weight_1d) { 2215d1d35e2fSjeremylt q_weight_1d[i] = wi; 2216d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 2217d7b241e6Sjeremylt } 2218d1d35e2fSjeremylt q_ref_1d[i] = -xi; 2219d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 2220d7b241e6Sjeremylt } 2221e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2222d7b241e6Sjeremylt } 2223d7b241e6Sjeremylt 2224d7b241e6Sjeremylt /// @} 2225