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 /** 327b0cc4569SJeremy L Thompson @brief Return 1D interpolation matrix to Chebyshev polynomial coefficients on quadrature space 328b0cc4569SJeremy L Thompson 329b0cc4569SJeremy L Thompson @param[in] basis `CeedBasis` 330b0cc4569SJeremy L Thompson @param[out] chebyshev_interp_1d Row-major (`P_1d * Q_1d`) matrix interpolating from basis nodes to Chebyshev polynomial coefficients 331b0cc4569SJeremy L Thompson 332b0cc4569SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 333b0cc4569SJeremy L Thompson 334b0cc4569SJeremy L Thompson @ref Backend 335b0cc4569SJeremy L Thompson **/ 336b0cc4569SJeremy L Thompson int CeedBasisGetChebyshevInterp1D(CeedBasis basis, CeedScalar *chebyshev_interp_1d) { 337b0cc4569SJeremy L Thompson CeedInt P_1d, Q_1d; 338b0cc4569SJeremy L Thompson CeedScalar *C, *chebyshev_coeffs_1d_inv; 339b0cc4569SJeremy L Thompson const CeedScalar *interp_1d, *q_ref_1d; 340b0cc4569SJeremy L Thompson Ceed ceed; 341b0cc4569SJeremy L Thompson 342b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 343b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 344b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 345b0cc4569SJeremy L Thompson 346b0cc4569SJeremy L Thompson // Build coefficient matrix 347*bd83cbc5SJeremy L Thompson // -- Note: Clang-tidy needs this check 348*bd83cbc5SJeremy L Thompson CeedCheck((P_1d > 0) && (Q_1d > 0), ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis dimensions are malformed"); 349b0cc4569SJeremy L Thompson CeedCall(CeedCalloc(Q_1d * Q_1d, &C)); 350b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 351b0cc4569SJeremy L Thompson for (CeedInt i = 0; i < Q_1d; i++) CeedCall(CeedChebyshevPolynomialsAtPoint(q_ref_1d[i], Q_1d, &C[i * Q_1d])); 352b0cc4569SJeremy L Thompson 353b0cc4569SJeremy L Thompson // Compute C^+, pseudoinverse of coefficient matrix 354b0cc4569SJeremy L Thompson CeedCall(CeedCalloc(Q_1d * Q_1d, &chebyshev_coeffs_1d_inv)); 355b0cc4569SJeremy L Thompson CeedCall(CeedMatrixPseudoinverse(ceed, C, Q_1d, Q_1d, chebyshev_coeffs_1d_inv)); 356b0cc4569SJeremy L Thompson 357b0cc4569SJeremy L Thompson // Build mapping from nodes to Chebyshev coefficients 358b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 359b0cc4569SJeremy L Thompson CeedCall(CeedMatrixMatrixMultiply(ceed, chebyshev_coeffs_1d_inv, interp_1d, chebyshev_interp_1d, Q_1d, P_1d, Q_1d)); 360b0cc4569SJeremy L Thompson 361b0cc4569SJeremy L Thompson // Cleanup 362b0cc4569SJeremy L Thompson CeedCall(CeedFree(&C)); 363b0cc4569SJeremy L Thompson CeedCall(CeedFree(&chebyshev_coeffs_1d_inv)); 364b0cc4569SJeremy L Thompson return CEED_ERROR_SUCCESS; 365b0cc4569SJeremy L Thompson } 366b0cc4569SJeremy L Thompson 367b0cc4569SJeremy L Thompson /** 368ca94c3ddSJeremy L Thompson @brief Get tensor status for given `CeedBasis` 3697a982d89SJeremy L. Thompson 370ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 371d1d35e2fSjeremylt @param[out] is_tensor Variable to store tensor status 3727a982d89SJeremy L. Thompson 3737a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3747a982d89SJeremy L. Thompson 3757a982d89SJeremy L. Thompson @ref Backend 3767a982d89SJeremy L. Thompson **/ 377d1d35e2fSjeremylt int CeedBasisIsTensor(CeedBasis basis, bool *is_tensor) { 3786402da51SJeremy L Thompson *is_tensor = basis->is_tensor_basis; 379e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3807a982d89SJeremy L. Thompson } 3817a982d89SJeremy L. Thompson 3827a982d89SJeremy L. Thompson /** 383ca94c3ddSJeremy L Thompson @brief Get backend data of a `CeedBasis` 3847a982d89SJeremy L. Thompson 385ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 3867a982d89SJeremy L. Thompson @param[out] data Variable to store data 3877a982d89SJeremy L. Thompson 3887a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3897a982d89SJeremy L. Thompson 3907a982d89SJeremy L. Thompson @ref Backend 3917a982d89SJeremy L. Thompson **/ 392777ff853SJeremy L Thompson int CeedBasisGetData(CeedBasis basis, void *data) { 393777ff853SJeremy L Thompson *(void **)data = basis->data; 394e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3957a982d89SJeremy L. Thompson } 3967a982d89SJeremy L. Thompson 3977a982d89SJeremy L. Thompson /** 398ca94c3ddSJeremy L Thompson @brief Set backend data of a `CeedBasis` 3997a982d89SJeremy L. Thompson 400ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` 401ea61e9acSJeremy L Thompson @param[in] data Data to set 4027a982d89SJeremy L. Thompson 4037a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4047a982d89SJeremy L. Thompson 4057a982d89SJeremy L. Thompson @ref Backend 4067a982d89SJeremy L. Thompson **/ 407777ff853SJeremy L Thompson int CeedBasisSetData(CeedBasis basis, void *data) { 408777ff853SJeremy L Thompson basis->data = data; 409e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4107a982d89SJeremy L. Thompson } 4117a982d89SJeremy L. Thompson 4127a982d89SJeremy L. Thompson /** 413ca94c3ddSJeremy L Thompson @brief Increment the reference counter for a `CeedBasis` 41434359f16Sjeremylt 415ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` to increment the reference counter 41634359f16Sjeremylt 41734359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 41834359f16Sjeremylt 41934359f16Sjeremylt @ref Backend 42034359f16Sjeremylt **/ 4219560d06aSjeremylt int CeedBasisReference(CeedBasis basis) { 42234359f16Sjeremylt basis->ref_count++; 42334359f16Sjeremylt return CEED_ERROR_SUCCESS; 42434359f16Sjeremylt } 42534359f16Sjeremylt 42634359f16Sjeremylt /** 427ca94c3ddSJeremy L Thompson @brief Get number of Q-vector components for given `CeedBasis` 428c4e3f59bSSebastian Grimberg 429ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 430ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 431ca94c3ddSJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 432ca94c3ddSJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 433ca94c3ddSJeremy L Thompson @ref CEED_EVAL_CURL to use curl 434c4e3f59bSSebastian Grimberg @param[out] q_comp Variable to store number of Q-vector components of basis 435c4e3f59bSSebastian Grimberg 436c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 437c4e3f59bSSebastian Grimberg 438c4e3f59bSSebastian Grimberg @ref Backend 439c4e3f59bSSebastian Grimberg **/ 440c4e3f59bSSebastian Grimberg int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode, CeedInt *q_comp) { 4411203703bSJeremy L Thompson CeedInt dim; 4421203703bSJeremy L Thompson 4431203703bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 444c4e3f59bSSebastian Grimberg switch (eval_mode) { 4451203703bSJeremy L Thompson case CEED_EVAL_INTERP: { 4461203703bSJeremy L Thompson CeedFESpace fe_space; 4471203703bSJeremy L Thompson 4481203703bSJeremy L Thompson CeedCall(CeedBasisGetFESpace(basis, &fe_space)); 4491203703bSJeremy L Thompson *q_comp = (fe_space == CEED_FE_SPACE_H1) ? 1 : dim; 4501203703bSJeremy L Thompson } break; 451c4e3f59bSSebastian Grimberg case CEED_EVAL_GRAD: 4521203703bSJeremy L Thompson *q_comp = dim; 453c4e3f59bSSebastian Grimberg break; 454c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 455c4e3f59bSSebastian Grimberg *q_comp = 1; 456c4e3f59bSSebastian Grimberg break; 457c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 4581203703bSJeremy L Thompson *q_comp = (dim < 3) ? 1 : dim; 459c4e3f59bSSebastian Grimberg break; 460c4e3f59bSSebastian Grimberg case CEED_EVAL_NONE: 461c4e3f59bSSebastian Grimberg case CEED_EVAL_WEIGHT: 462352a5e7cSSebastian Grimberg *q_comp = 1; 463c4e3f59bSSebastian Grimberg break; 464c4e3f59bSSebastian Grimberg } 465c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 466c4e3f59bSSebastian Grimberg } 467c4e3f59bSSebastian Grimberg 468c4e3f59bSSebastian Grimberg /** 469ca94c3ddSJeremy L Thompson @brief Estimate number of FLOPs required to apply `CeedBasis` in `t_mode` and `eval_mode` 4706e15d496SJeremy L Thompson 471ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to estimate FLOPs for 472ea61e9acSJeremy L Thompson @param[in] t_mode Apply basis or transpose 473ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CeedEvalMode 474ea61e9acSJeremy L Thompson @param[out] flops Address of variable to hold FLOPs estimate 4756e15d496SJeremy L Thompson 4766e15d496SJeremy L Thompson @ref Backend 4776e15d496SJeremy L Thompson **/ 4782b730f8bSJeremy L Thompson int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedSize *flops) { 4796e15d496SJeremy L Thompson bool is_tensor; 4806e15d496SJeremy L Thompson 4812b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor)); 4826e15d496SJeremy L Thompson if (is_tensor) { 4836e15d496SJeremy L Thompson CeedInt dim, num_comp, P_1d, Q_1d; 4841c66c397SJeremy L Thompson 4852b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 4862b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 4872b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 4882b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 4896e15d496SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 4902b730f8bSJeremy L Thompson P_1d = Q_1d; 4912b730f8bSJeremy L Thompson Q_1d = P_1d; 4926e15d496SJeremy L Thompson } 4936e15d496SJeremy L Thompson CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim - 1), post = 1; 4946e15d496SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 4956e15d496SJeremy L Thompson tensor_flops += 2 * pre * P_1d * post * Q_1d; 4966e15d496SJeremy L Thompson pre /= P_1d; 4976e15d496SJeremy L Thompson post *= Q_1d; 4986e15d496SJeremy L Thompson } 4996e15d496SJeremy L Thompson switch (eval_mode) { 5002b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 5012b730f8bSJeremy L Thompson *flops = 0; 5022b730f8bSJeremy L Thompson break; 5032b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 5042b730f8bSJeremy L Thompson *flops = tensor_flops; 5052b730f8bSJeremy L Thompson break; 5062b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 5072b730f8bSJeremy L Thompson *flops = tensor_flops * 2; 5082b730f8bSJeremy L Thompson break; 5096e15d496SJeremy L Thompson case CEED_EVAL_DIV: 5101203703bSJeremy L Thompson case CEED_EVAL_CURL: { 5116574a04fSJeremy L Thompson // LCOV_EXCL_START 5126e536b99SJeremy L Thompson return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported", 5136e536b99SJeremy L Thompson CeedEvalModes[eval_mode]); 5142b730f8bSJeremy L Thompson break; 5156e15d496SJeremy L Thompson // LCOV_EXCL_STOP 5161203703bSJeremy L Thompson } 5172b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 5182b730f8bSJeremy L Thompson *flops = dim * CeedIntPow(Q_1d, dim); 5192b730f8bSJeremy L Thompson break; 5206e15d496SJeremy L Thompson } 5216e15d496SJeremy L Thompson } else { 522c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 5231c66c397SJeremy L Thompson 5242b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 5252b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 526c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 5272b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 5282b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 5296e15d496SJeremy L Thompson switch (eval_mode) { 5302b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 5312b730f8bSJeremy L Thompson *flops = 0; 5322b730f8bSJeremy L Thompson break; 5332b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 5342b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 5352b730f8bSJeremy L Thompson case CEED_EVAL_DIV: 5362b730f8bSJeremy L Thompson case CEED_EVAL_CURL: 537c4e3f59bSSebastian Grimberg *flops = num_nodes * num_qpts * num_comp * q_comp; 5382b730f8bSJeremy L Thompson break; 5392b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 5402b730f8bSJeremy L Thompson *flops = 0; 5412b730f8bSJeremy L Thompson break; 5426e15d496SJeremy L Thompson } 5436e15d496SJeremy L Thompson } 5446e15d496SJeremy L Thompson return CEED_ERROR_SUCCESS; 5456e15d496SJeremy L Thompson } 5466e15d496SJeremy L Thompson 5476e15d496SJeremy L Thompson /** 548ca94c3ddSJeremy L Thompson @brief Get `CeedFESpace` for a `CeedBasis` 549c4e3f59bSSebastian Grimberg 550ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 551ca94c3ddSJeremy L Thompson @param[out] fe_space Variable to store `CeedFESpace` 552c4e3f59bSSebastian Grimberg 553c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 554c4e3f59bSSebastian Grimberg 555c4e3f59bSSebastian Grimberg @ref Backend 556c4e3f59bSSebastian Grimberg **/ 557c4e3f59bSSebastian Grimberg int CeedBasisGetFESpace(CeedBasis basis, CeedFESpace *fe_space) { 558c4e3f59bSSebastian Grimberg *fe_space = basis->fe_space; 559c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 560c4e3f59bSSebastian Grimberg } 561c4e3f59bSSebastian Grimberg 562c4e3f59bSSebastian Grimberg /** 563ca94c3ddSJeremy L Thompson @brief Get dimension for given `CeedElemTopology` 5647a982d89SJeremy L. Thompson 565ca94c3ddSJeremy L Thompson @param[in] topo `CeedElemTopology` 5667a982d89SJeremy L. Thompson @param[out] dim Variable to store dimension of topology 5677a982d89SJeremy L. Thompson 5687a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5697a982d89SJeremy L. Thompson 5707a982d89SJeremy L. Thompson @ref Backend 5717a982d89SJeremy L. Thompson **/ 5727a982d89SJeremy L. Thompson int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim) { 5737a982d89SJeremy L. Thompson *dim = (CeedInt)topo >> 16; 574e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5757a982d89SJeremy L. Thompson } 5767a982d89SJeremy L. Thompson 5777a982d89SJeremy L. Thompson /** 578ca94c3ddSJeremy L Thompson @brief Get `CeedTensorContract` of a `CeedBasis` 5797a982d89SJeremy L. Thompson 580ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 581ca94c3ddSJeremy L Thompson @param[out] contract Variable to store `CeedTensorContract` 5827a982d89SJeremy L. Thompson 5837a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5847a982d89SJeremy L. Thompson 5857a982d89SJeremy L. Thompson @ref Backend 5867a982d89SJeremy L. Thompson **/ 5877a982d89SJeremy L. Thompson int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract) { 5887a982d89SJeremy L. Thompson *contract = basis->contract; 589e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5907a982d89SJeremy L. Thompson } 5917a982d89SJeremy L. Thompson 5927a982d89SJeremy L. Thompson /** 593ca94c3ddSJeremy L Thompson @brief Set `CeedTensorContract` of a `CeedBasis` 5947a982d89SJeremy L. Thompson 595ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` 596ca94c3ddSJeremy L Thompson @param[in] contract `CeedTensorContract` to set 5977a982d89SJeremy L. Thompson 5987a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5997a982d89SJeremy L. Thompson 6007a982d89SJeremy L. Thompson @ref Backend 6017a982d89SJeremy L. Thompson **/ 60234359f16Sjeremylt int CeedBasisSetTensorContract(CeedBasis basis, CeedTensorContract contract) { 60334359f16Sjeremylt basis->contract = contract; 6042b730f8bSJeremy L Thompson CeedCall(CeedTensorContractReference(contract)); 605e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6067a982d89SJeremy L. Thompson } 6077a982d89SJeremy L. Thompson 6087a982d89SJeremy L. Thompson /** 609ca94c3ddSJeremy L Thompson @brief Return a reference implementation of matrix multiplication \f$C = A B\f$. 610ba59ac12SSebastian Grimberg 611ca94c3ddSJeremy L Thompson Note: This is a reference implementation for CPU `CeedScalar` pointers that is not intended for high performance. 6127a982d89SJeremy L. Thompson 613ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 614ca94c3ddSJeremy L Thompson @param[in] mat_A Row-major matrix `A` 615ca94c3ddSJeremy L Thompson @param[in] mat_B Row-major matrix `B` 616ca94c3ddSJeremy L Thompson @param[out] mat_C Row-major output matrix `C` 617ca94c3ddSJeremy L Thompson @param[in] m Number of rows of `C` 618ca94c3ddSJeremy L Thompson @param[in] n Number of columns of `C` 619ca94c3ddSJeremy L Thompson @param[in] kk Number of columns of `A`/rows of `B` 6207a982d89SJeremy L. Thompson 6217a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 6227a982d89SJeremy L. Thompson 6237a982d89SJeremy L. Thompson @ref Utility 6247a982d89SJeremy L. Thompson **/ 6252b730f8bSJeremy L Thompson int CeedMatrixMatrixMultiply(Ceed ceed, const CeedScalar *mat_A, const CeedScalar *mat_B, CeedScalar *mat_C, CeedInt m, CeedInt n, CeedInt kk) { 6262b730f8bSJeremy L Thompson for (CeedInt i = 0; i < m; i++) { 6277a982d89SJeremy L. Thompson for (CeedInt j = 0; j < n; j++) { 6287a982d89SJeremy L. Thompson CeedScalar sum = 0; 6291c66c397SJeremy L Thompson 6302b730f8bSJeremy L Thompson for (CeedInt k = 0; k < kk; k++) sum += mat_A[k + i * kk] * mat_B[j + k * n]; 631d1d35e2fSjeremylt mat_C[j + i * n] = sum; 6327a982d89SJeremy L. Thompson } 6332b730f8bSJeremy L Thompson } 634e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6357a982d89SJeremy L. Thompson } 6367a982d89SJeremy L. Thompson 637ba59ac12SSebastian Grimberg /** 638ba59ac12SSebastian Grimberg @brief Return QR Factorization of a matrix 639ba59ac12SSebastian Grimberg 640ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 641ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 642ca94c3ddSJeremy L Thompson @param[in,out] tau Vector of length `m` of scaling factors 643ba59ac12SSebastian Grimberg @param[in] m Number of rows 644ba59ac12SSebastian Grimberg @param[in] n Number of columns 645ba59ac12SSebastian Grimberg 646ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 647ba59ac12SSebastian Grimberg 648ba59ac12SSebastian Grimberg @ref Utility 649ba59ac12SSebastian Grimberg **/ 650ba59ac12SSebastian Grimberg int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau, CeedInt m, CeedInt n) { 651ba59ac12SSebastian Grimberg CeedScalar v[m]; 652ba59ac12SSebastian Grimberg 653ba59ac12SSebastian Grimberg // Check matrix shape 6546574a04fSJeremy L Thompson CeedCheck(n <= m, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute QR factorization with n > m"); 655ba59ac12SSebastian Grimberg 656ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 6571c66c397SJeremy L Thompson CeedScalar sigma = 0.0; 6581c66c397SJeremy L Thompson 659ba59ac12SSebastian Grimberg if (i >= m - 1) { // last row of matrix, no reflection needed 660ba59ac12SSebastian Grimberg tau[i] = 0.; 661ba59ac12SSebastian Grimberg break; 662ba59ac12SSebastian Grimberg } 663ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 664ba59ac12SSebastian Grimberg v[i] = mat[i + n * i]; 665ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) { 666ba59ac12SSebastian Grimberg v[j] = mat[i + n * j]; 667ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 668ba59ac12SSebastian Grimberg } 6691c66c397SJeremy L Thompson const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:m] 6701c66c397SJeremy L Thompson const CeedScalar R_ii = -copysign(norm, v[i]); 6711c66c397SJeremy L Thompson 672ba59ac12SSebastian Grimberg v[i] -= R_ii; 673ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 674ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 675ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 676ba59ac12SSebastian Grimberg tau[i] = 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 677ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] /= v[i]; 678ba59ac12SSebastian Grimberg 679ba59ac12SSebastian Grimberg // Apply Householder reflector to lower right panel 680ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[i * n + i + 1], &v[i], tau[i], m - i, n - i - 1, n, 1); 681ba59ac12SSebastian Grimberg // Save v 682ba59ac12SSebastian Grimberg mat[i + n * i] = R_ii; 683ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) mat[i + n * j] = v[j]; 684ba59ac12SSebastian Grimberg } 685ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 686ba59ac12SSebastian Grimberg } 687ba59ac12SSebastian Grimberg 688ba59ac12SSebastian Grimberg /** 689ba59ac12SSebastian Grimberg @brief Apply Householder Q matrix 690ba59ac12SSebastian Grimberg 691ca94c3ddSJeremy 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$. 692ba59ac12SSebastian Grimberg 693ba59ac12SSebastian Grimberg @param[in,out] mat_A Matrix to apply Householder Q to, in place 694ba59ac12SSebastian Grimberg @param[in] mat_Q Householder Q matrix 695ba59ac12SSebastian Grimberg @param[in] tau Householder scaling factors 696ba59ac12SSebastian Grimberg @param[in] t_mode Transpose mode for application 697ca94c3ddSJeremy L Thompson @param[in] m Number of rows in `A` 698ca94c3ddSJeremy L Thompson @param[in] n Number of columns in `A` 699ca94c3ddSJeremy L Thompson @param[in] k Number of elementary reflectors in Q, `k < m` 700ca94c3ddSJeremy L Thompson @param[in] row Row stride in `A` 701ca94c3ddSJeremy L Thompson @param[in] col Col stride in `A` 702ba59ac12SSebastian Grimberg 703ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 704ba59ac12SSebastian Grimberg 705c4e3f59bSSebastian Grimberg @ref Utility 706ba59ac12SSebastian Grimberg **/ 707ba59ac12SSebastian Grimberg int CeedHouseholderApplyQ(CeedScalar *mat_A, const CeedScalar *mat_Q, const CeedScalar *tau, CeedTransposeMode t_mode, CeedInt m, CeedInt n, 708ba59ac12SSebastian Grimberg CeedInt k, CeedInt row, CeedInt col) { 709ba59ac12SSebastian Grimberg CeedScalar *v; 7101c66c397SJeremy L Thompson 711ba59ac12SSebastian Grimberg CeedCall(CeedMalloc(m, &v)); 712ba59ac12SSebastian Grimberg for (CeedInt ii = 0; ii < k; ii++) { 713ba59ac12SSebastian Grimberg CeedInt i = t_mode == CEED_TRANSPOSE ? ii : k - 1 - ii; 714ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] = mat_Q[j * k + i]; 715ba59ac12SSebastian Grimberg // Apply Householder reflector (I - tau v v^T) collo_grad_1d^T 716ba59ac12SSebastian Grimberg CeedCall(CeedHouseholderReflect(&mat_A[i * row], &v[i], tau[i], m - i, n, row, col)); 717ba59ac12SSebastian Grimberg } 718ba59ac12SSebastian Grimberg CeedCall(CeedFree(&v)); 719ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 720ba59ac12SSebastian Grimberg } 721ba59ac12SSebastian Grimberg 722ba59ac12SSebastian Grimberg /** 7232247a93fSRezgar Shakeri @brief Return pseudoinverse of a matrix 7242247a93fSRezgar Shakeri 7252247a93fSRezgar Shakeri @param[in] ceed Ceed context for error handling 7262247a93fSRezgar Shakeri @param[in] mat Row-major matrix to compute pseudoinverse of 7272247a93fSRezgar Shakeri @param[in] m Number of rows 7282247a93fSRezgar Shakeri @param[in] n Number of columns 7292247a93fSRezgar Shakeri @param[out] mat_pinv Row-major pseudoinverse matrix 7302247a93fSRezgar Shakeri 7312247a93fSRezgar Shakeri @return An error code: 0 - success, otherwise - failure 7322247a93fSRezgar Shakeri 7332247a93fSRezgar Shakeri @ref Utility 7342247a93fSRezgar Shakeri **/ 7351203703bSJeremy L Thompson int CeedMatrixPseudoinverse(Ceed ceed, const CeedScalar *mat, CeedInt m, CeedInt n, CeedScalar *mat_pinv) { 7362247a93fSRezgar Shakeri CeedScalar *tau, *I, *mat_copy; 7372247a93fSRezgar Shakeri 7382247a93fSRezgar Shakeri CeedCall(CeedCalloc(m, &tau)); 7392247a93fSRezgar Shakeri CeedCall(CeedCalloc(m * m, &I)); 7402247a93fSRezgar Shakeri CeedCall(CeedCalloc(m * n, &mat_copy)); 7412247a93fSRezgar Shakeri memcpy(mat_copy, mat, m * n * sizeof mat[0]); 7422247a93fSRezgar Shakeri 7432247a93fSRezgar Shakeri // QR Factorization, mat = Q R 7442247a93fSRezgar Shakeri CeedCall(CeedQRFactorization(ceed, mat_copy, tau, m, n)); 7452247a93fSRezgar Shakeri 7462247a93fSRezgar Shakeri // -- Apply Q^T, I = Q^T * I 7472247a93fSRezgar Shakeri for (CeedInt i = 0; i < m; i++) I[i * m + i] = 1.0; 7482247a93fSRezgar Shakeri CeedCall(CeedHouseholderApplyQ(I, mat_copy, tau, CEED_TRANSPOSE, m, m, n, m, 1)); 7492247a93fSRezgar Shakeri // -- Apply R_inv, mat_pinv = R_inv * Q^T 7502247a93fSRezgar Shakeri for (CeedInt j = 0; j < m; j++) { // Column j 7512247a93fSRezgar Shakeri mat_pinv[j + m * (n - 1)] = I[j + m * (n - 1)] / mat_copy[n * n - 1]; 7522247a93fSRezgar Shakeri for (CeedInt i = n - 2; i >= 0; i--) { // Row i 7532247a93fSRezgar Shakeri mat_pinv[j + m * i] = I[j + m * i]; 7542247a93fSRezgar Shakeri for (CeedInt k = i + 1; k < n; k++) mat_pinv[j + m * i] -= mat_copy[k + n * i] * mat_pinv[j + m * k]; 7552247a93fSRezgar Shakeri mat_pinv[j + m * i] /= mat_copy[i + n * i]; 7562247a93fSRezgar Shakeri } 7572247a93fSRezgar Shakeri } 7582247a93fSRezgar Shakeri 7592247a93fSRezgar Shakeri // Cleanup 7602247a93fSRezgar Shakeri CeedCall(CeedFree(&I)); 7612247a93fSRezgar Shakeri CeedCall(CeedFree(&tau)); 7622247a93fSRezgar Shakeri CeedCall(CeedFree(&mat_copy)); 7632247a93fSRezgar Shakeri return CEED_ERROR_SUCCESS; 7642247a93fSRezgar Shakeri } 7652247a93fSRezgar Shakeri 7662247a93fSRezgar Shakeri /** 767ba59ac12SSebastian Grimberg @brief Return symmetric Schur decomposition of the symmetric matrix mat via symmetric QR factorization 768ba59ac12SSebastian Grimberg 769ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 770ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 771ba59ac12SSebastian Grimberg @param[out] lambda Vector of length n of eigenvalues 772ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 773ba59ac12SSebastian Grimberg 774ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 775ba59ac12SSebastian Grimberg 776ba59ac12SSebastian Grimberg @ref Utility 777ba59ac12SSebastian Grimberg **/ 7782c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 7792c2ea1dbSJeremy L Thompson int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat, CeedScalar *lambda, CeedInt n) { 780ba59ac12SSebastian Grimberg // Check bounds for clang-tidy 7816574a04fSJeremy L Thompson CeedCheck(n > 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute symmetric Schur decomposition of scalars"); 782ba59ac12SSebastian Grimberg 783ba59ac12SSebastian Grimberg CeedScalar v[n - 1], tau[n - 1], mat_T[n * n]; 784ba59ac12SSebastian Grimberg 785ba59ac12SSebastian Grimberg // Copy mat to mat_T and set mat to I 786ba59ac12SSebastian Grimberg memcpy(mat_T, mat, n * n * sizeof(mat[0])); 787ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 788ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) mat[j + n * i] = (i == j) ? 1 : 0; 789ba59ac12SSebastian Grimberg } 790ba59ac12SSebastian Grimberg 791ba59ac12SSebastian Grimberg // Reduce to tridiagonal 792ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - 1; i++) { 793ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 794ba59ac12SSebastian Grimberg CeedScalar sigma = 0.0; 7951c66c397SJeremy L Thompson 796ba59ac12SSebastian Grimberg v[i] = mat_T[i + n * (i + 1)]; 797ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 798ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 799ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 800ba59ac12SSebastian Grimberg } 8011c66c397SJeremy L Thompson const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:n-1] 8021c66c397SJeremy L Thompson const CeedScalar R_ii = -copysign(norm, v[i]); 8031c66c397SJeremy L Thompson 804ba59ac12SSebastian Grimberg v[i] -= R_ii; 805ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 806ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 807ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 808ba59ac12SSebastian Grimberg tau[i] = i == n - 2 ? 2 : 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 809ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) v[j] /= v[i]; 810ba59ac12SSebastian Grimberg 811ba59ac12SSebastian Grimberg // Update sub and super diagonal 812ba59ac12SSebastian Grimberg for (CeedInt j = i + 2; j < n; j++) { 813ba59ac12SSebastian Grimberg mat_T[i + n * j] = 0; 814ba59ac12SSebastian Grimberg mat_T[j + n * i] = 0; 815ba59ac12SSebastian Grimberg } 816ba59ac12SSebastian Grimberg // Apply symmetric Householder reflector to lower right panel 817ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 818ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), 1, n); 819ba59ac12SSebastian Grimberg 820ba59ac12SSebastian Grimberg // Save v 821ba59ac12SSebastian Grimberg mat_T[i + n * (i + 1)] = R_ii; 822ba59ac12SSebastian Grimberg mat_T[(i + 1) + n * i] = R_ii; 823ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 824ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = v[j]; 825ba59ac12SSebastian Grimberg } 826ba59ac12SSebastian Grimberg } 827ba59ac12SSebastian Grimberg // Backwards accumulation of Q 828ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 829ba59ac12SSebastian Grimberg if (tau[i] > 0.0) { 830ba59ac12SSebastian Grimberg v[i] = 1; 831ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 832ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 833ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = 0; 834ba59ac12SSebastian Grimberg } 835ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 836ba59ac12SSebastian Grimberg } 837ba59ac12SSebastian Grimberg } 838ba59ac12SSebastian Grimberg 839ba59ac12SSebastian Grimberg // Reduce sub and super diagonal 840ba59ac12SSebastian Grimberg CeedInt p = 0, q = 0, itr = 0, max_itr = n * n * n * n; 841ba59ac12SSebastian Grimberg CeedScalar tol = CEED_EPSILON; 842ba59ac12SSebastian Grimberg 843ba59ac12SSebastian Grimberg while (itr < max_itr) { 844ba59ac12SSebastian Grimberg // Update p, q, size of reduced portions of diagonal 845ba59ac12SSebastian Grimberg p = 0; 846ba59ac12SSebastian Grimberg q = 0; 847ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 848ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) q += 1; 849ba59ac12SSebastian Grimberg else break; 850ba59ac12SSebastian Grimberg } 851ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - q - 1; i++) { 852ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) p += 1; 853ba59ac12SSebastian Grimberg else break; 854ba59ac12SSebastian Grimberg } 855ba59ac12SSebastian Grimberg if (q == n - 1) break; // Finished reducing 856ba59ac12SSebastian Grimberg 857ba59ac12SSebastian Grimberg // Reduce tridiagonal portion 858ba59ac12SSebastian Grimberg CeedScalar t_nn = mat_T[(n - 1 - q) + n * (n - 1 - q)], t_nnm1 = mat_T[(n - 2 - q) + n * (n - 1 - q)]; 859ba59ac12SSebastian Grimberg CeedScalar d = (mat_T[(n - 2 - q) + n * (n - 2 - q)] - t_nn) / 2; 860ba59ac12SSebastian Grimberg CeedScalar mu = t_nn - t_nnm1 * t_nnm1 / (d + copysign(sqrt(d * d + t_nnm1 * t_nnm1), d)); 861ba59ac12SSebastian Grimberg CeedScalar x = mat_T[p + n * p] - mu; 862ba59ac12SSebastian Grimberg CeedScalar z = mat_T[p + n * (p + 1)]; 8631c66c397SJeremy L Thompson 864ba59ac12SSebastian Grimberg for (CeedInt k = p; k < n - q - 1; k++) { 865ba59ac12SSebastian Grimberg // Compute Givens rotation 866ba59ac12SSebastian Grimberg CeedScalar c = 1, s = 0; 8671c66c397SJeremy L Thompson 868ba59ac12SSebastian Grimberg if (fabs(z) > tol) { 869ba59ac12SSebastian Grimberg if (fabs(z) > fabs(x)) { 8701c66c397SJeremy L Thompson const CeedScalar tau = -x / z; 8711c66c397SJeremy L Thompson 8721c66c397SJeremy L Thompson s = 1 / sqrt(1 + tau * tau); 8731c66c397SJeremy L Thompson c = s * tau; 874ba59ac12SSebastian Grimberg } else { 8751c66c397SJeremy L Thompson const CeedScalar tau = -z / x; 8761c66c397SJeremy L Thompson 8771c66c397SJeremy L Thompson c = 1 / sqrt(1 + tau * tau); 8781c66c397SJeremy L Thompson s = c * tau; 879ba59ac12SSebastian Grimberg } 880ba59ac12SSebastian Grimberg } 881ba59ac12SSebastian Grimberg 882ba59ac12SSebastian Grimberg // Apply Givens rotation to T 883ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 884ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_TRANSPOSE, k, k + 1, n, n); 885ba59ac12SSebastian Grimberg 886ba59ac12SSebastian Grimberg // Apply Givens rotation to Q 887ba59ac12SSebastian Grimberg CeedGivensRotation(mat, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 888ba59ac12SSebastian Grimberg 889ba59ac12SSebastian Grimberg // Update x, z 890ba59ac12SSebastian Grimberg if (k < n - q - 2) { 891ba59ac12SSebastian Grimberg x = mat_T[k + n * (k + 1)]; 892ba59ac12SSebastian Grimberg z = mat_T[k + n * (k + 2)]; 893ba59ac12SSebastian Grimberg } 894ba59ac12SSebastian Grimberg } 895ba59ac12SSebastian Grimberg itr++; 896ba59ac12SSebastian Grimberg } 897ba59ac12SSebastian Grimberg 898ba59ac12SSebastian Grimberg // Save eigenvalues 899ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) lambda[i] = mat_T[i + n * i]; 900ba59ac12SSebastian Grimberg 901ba59ac12SSebastian Grimberg // Check convergence 9026574a04fSJeremy L Thompson CeedCheck(itr < max_itr || q > n, ceed, CEED_ERROR_MINOR, "Symmetric QR failed to converge"); 903ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 904ba59ac12SSebastian Grimberg } 9052c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 906ba59ac12SSebastian Grimberg 907ba59ac12SSebastian Grimberg /** 908ba59ac12SSebastian Grimberg @brief Return Simultaneous Diagonalization of two matrices. 909ba59ac12SSebastian Grimberg 910ca94c3ddSJeremy L Thompson This solves the generalized eigenvalue problem `A x = lambda B x`, where `A` and `B` are symmetric and `B` is positive definite. 911ca94c3ddSJeremy L Thompson We generate the matrix `X` and vector `Lambda` such that `X^T A X = Lambda` and `X^T B X = I`. 912ca94c3ddSJeremy L Thompson This is equivalent to the LAPACK routine 'sygv' with `TYPE = 1`. 913ba59ac12SSebastian Grimberg 914ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 915ba59ac12SSebastian Grimberg @param[in] mat_A Row-major matrix to be factorized with eigenvalues 916ba59ac12SSebastian Grimberg @param[in] mat_B Row-major matrix to be factorized to identity 917ba59ac12SSebastian Grimberg @param[out] mat_X Row-major orthogonal matrix 918ca94c3ddSJeremy L Thompson @param[out] lambda Vector of length `n` of generalized eigenvalues 919ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 920ba59ac12SSebastian Grimberg 921ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 922ba59ac12SSebastian Grimberg 923ba59ac12SSebastian Grimberg @ref Utility 924ba59ac12SSebastian Grimberg **/ 9252c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 9262c2ea1dbSJeremy L Thompson int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A, CeedScalar *mat_B, CeedScalar *mat_X, CeedScalar *lambda, CeedInt n) { 927ba59ac12SSebastian Grimberg CeedScalar *mat_C, *mat_G, *vec_D; 9281c66c397SJeremy L Thompson 929ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_C)); 930ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_G)); 931ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n, &vec_D)); 932ba59ac12SSebastian Grimberg 933ba59ac12SSebastian Grimberg // Compute B = G D G^T 934ba59ac12SSebastian Grimberg memcpy(mat_G, mat_B, n * n * sizeof(mat_B[0])); 935ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_G, vec_D, n)); 936ba59ac12SSebastian Grimberg 937ba59ac12SSebastian Grimberg // Sort eigenvalues 938ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 939ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 940ba59ac12SSebastian Grimberg if (fabs(vec_D[j]) > fabs(vec_D[j + 1])) { 9411c66c397SJeremy L Thompson CeedScalarSwap(vec_D[j], vec_D[j + 1]); 9421c66c397SJeremy L Thompson for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_G[k * n + j], mat_G[k * n + j + 1]); 943ba59ac12SSebastian Grimberg } 944ba59ac12SSebastian Grimberg } 945ba59ac12SSebastian Grimberg } 946ba59ac12SSebastian Grimberg 947ba59ac12SSebastian Grimberg // Compute C = (G D^1/2)^-1 A (G D^1/2)^-T 948ba59ac12SSebastian Grimberg // = D^-1/2 G^T A G D^-1/2 949ba59ac12SSebastian Grimberg // -- D = D^-1/2 950ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) vec_D[i] = 1. / sqrt(vec_D[i]); 951ba59ac12SSebastian Grimberg // -- G = G D^-1/2 952ba59ac12SSebastian Grimberg // -- C = D^-1/2 G^T 953ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 954ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) { 955ba59ac12SSebastian Grimberg mat_G[i * n + j] *= vec_D[j]; 956ba59ac12SSebastian Grimberg mat_C[j * n + i] = mat_G[i * n + j]; 957ba59ac12SSebastian Grimberg } 958ba59ac12SSebastian Grimberg } 959ba59ac12SSebastian Grimberg // -- X = (D^-1/2 G^T) A 960ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_C, (const CeedScalar *)mat_A, mat_X, n, n, n)); 961ba59ac12SSebastian Grimberg // -- C = (D^-1/2 G^T A) (G D^-1/2) 962ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_X, (const CeedScalar *)mat_G, mat_C, n, n, n)); 963ba59ac12SSebastian Grimberg 964ba59ac12SSebastian Grimberg // Compute Q^T C Q = lambda 965ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_C, lambda, n)); 966ba59ac12SSebastian Grimberg 967ba59ac12SSebastian Grimberg // Sort eigenvalues 968ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 969ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 970ba59ac12SSebastian Grimberg if (fabs(lambda[j]) > fabs(lambda[j + 1])) { 9711c66c397SJeremy L Thompson CeedScalarSwap(lambda[j], lambda[j + 1]); 9721c66c397SJeremy L Thompson for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_C[k * n + j], mat_C[k * n + j + 1]); 973ba59ac12SSebastian Grimberg } 974ba59ac12SSebastian Grimberg } 975ba59ac12SSebastian Grimberg } 976ba59ac12SSebastian Grimberg 977ba59ac12SSebastian Grimberg // Set X = (G D^1/2)^-T Q 978ba59ac12SSebastian Grimberg // = G D^-1/2 Q 979ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_G, (const CeedScalar *)mat_C, mat_X, n, n, n)); 980ba59ac12SSebastian Grimberg 981ba59ac12SSebastian Grimberg // Cleanup 982ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_C)); 983ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_G)); 984ba59ac12SSebastian Grimberg CeedCall(CeedFree(&vec_D)); 985ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 986ba59ac12SSebastian Grimberg } 9872c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 988ba59ac12SSebastian Grimberg 9897a982d89SJeremy L. Thompson /// @} 9907a982d89SJeremy L. Thompson 9917a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 9927a982d89SJeremy L. Thompson /// CeedBasis Public API 9937a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 9947a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser 995d7b241e6Sjeremylt /// @{ 996d7b241e6Sjeremylt 997b11c1e72Sjeremylt /** 998ca94c3ddSJeremy L Thompson @brief Create a tensor-product basis for \f$H^1\f$ discretizations 999b11c1e72Sjeremylt 1000ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1001ea61e9acSJeremy L Thompson @param[in] dim Topological dimension 1002ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1003ea61e9acSJeremy L Thompson @param[in] P_1d Number of nodes in one dimension 1004ea61e9acSJeremy L Thompson @param[in] Q_1d Number of quadrature points in one dimension 1005ca94c3ddSJeremy L Thompson @param[in] interp_1d Row-major (`Q_1d * P_1d`) matrix expressing the values of nodal basis functions at quadrature points 1006ca94c3ddSJeremy L Thompson @param[in] grad_1d Row-major (`Q_1d * P_1d`) matrix expressing derivatives of nodal basis functions at quadrature points 1007ca94c3ddSJeremy 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]` 1008ca94c3ddSJeremy L Thompson @param[in] q_weight_1d Array of length `Q_1d` holding the quadrature weights on the reference element 1009ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1010b11c1e72Sjeremylt 1011b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1012dfdf5a53Sjeremylt 10137a982d89SJeremy L. Thompson @ref User 1014b11c1e72Sjeremylt **/ 10152b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, 10162b730f8bSJeremy L Thompson const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis *basis) { 10175fe0d4faSjeremylt if (!ceed->BasisCreateTensorH1) { 10185fe0d4faSjeremylt Ceed delegate; 10196574a04fSJeremy L Thompson 10202b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 10211ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateTensorH1"); 10222b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 1023e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10245fe0d4faSjeremylt } 1025e15f9bd0SJeremy L Thompson 1026ca94c3ddSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value"); 1027ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1028ca94c3ddSJeremy L Thompson CeedCheck(P_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1029ca94c3ddSJeremy L Thompson CeedCheck(Q_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1030227444bfSJeremy L Thompson 10312b730f8bSJeremy L Thompson CeedElemTopology topo = dim == 1 ? CEED_TOPOLOGY_LINE : dim == 2 ? CEED_TOPOLOGY_QUAD : CEED_TOPOLOGY_HEX; 1032e15f9bd0SJeremy L Thompson 10332b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1034db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1035d1d35e2fSjeremylt (*basis)->ref_count = 1; 10366402da51SJeremy L Thompson (*basis)->is_tensor_basis = true; 1037d7b241e6Sjeremylt (*basis)->dim = dim; 1038d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 1039d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 1040d1d35e2fSjeremylt (*basis)->P_1d = P_1d; 1041d1d35e2fSjeremylt (*basis)->Q_1d = Q_1d; 1042d1d35e2fSjeremylt (*basis)->P = CeedIntPow(P_1d, dim); 1043d1d35e2fSjeremylt (*basis)->Q = CeedIntPow(Q_1d, dim); 1044c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 10452b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_ref_1d)); 10462b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_weight_1d)); 1047ff3a0f91SJeremy L Thompson if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d * sizeof(q_ref_1d[0])); 10482b730f8bSJeremy L Thompson if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, Q_1d * sizeof(q_weight_1d[0])); 10492b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->interp_1d)); 10502b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->grad_1d)); 10512b730f8bSJeremy L Thompson if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, Q_1d * P_1d * sizeof(interp_1d[0])); 1052ff3a0f91SJeremy L Thompson if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d * P_1d * sizeof(grad_1d[0])); 10532b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, *basis)); 1054e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1055d7b241e6Sjeremylt } 1056d7b241e6Sjeremylt 1057b11c1e72Sjeremylt /** 1058ca94c3ddSJeremy L Thompson @brief Create a tensor-product \f$H^1\f$ Lagrange basis 1059b11c1e72Sjeremylt 1060ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1061ea61e9acSJeremy L Thompson @param[in] dim Topological dimension of element 1062ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1063ea61e9acSJeremy L Thompson @param[in] P Number of Gauss-Lobatto nodes in one dimension. 1064ca94c3ddSJeremy L Thompson The polynomial degree of the resulting `Q_k` element is `k = P - 1`. 1065ea61e9acSJeremy L Thompson @param[in] Q Number of quadrature points in one dimension. 1066ca94c3ddSJeremy L Thompson @param[in] quad_mode Distribution of the `Q` quadrature points (affects order of accuracy for the quadrature) 1067ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1068b11c1e72Sjeremylt 1069b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1070dfdf5a53Sjeremylt 10717a982d89SJeremy L. Thompson @ref User 1072b11c1e72Sjeremylt **/ 10732b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis) { 1074d7b241e6Sjeremylt // Allocate 1075c8c3fa7dSJeremy L Thompson int ierr = CEED_ERROR_SUCCESS; 10762b730f8bSJeremy L Thompson CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, *q_weight_1d; 10774d537eeaSYohann 1078ca94c3ddSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value"); 1079ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1080ca94c3ddSJeremy L Thompson CeedCheck(P > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1081ca94c3ddSJeremy L Thompson CeedCheck(Q > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1082227444bfSJeremy L Thompson 1083e15f9bd0SJeremy L Thompson // Get Nodes and Weights 10842b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &interp_1d)); 10852b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &grad_1d)); 10862b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P, &nodes)); 10872b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_ref_1d)); 10882b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_weight_1d)); 10892b730f8bSJeremy L Thompson if (CeedLobattoQuadrature(P, nodes, NULL) != CEED_ERROR_SUCCESS) goto cleanup; 1090d1d35e2fSjeremylt switch (quad_mode) { 1091d7b241e6Sjeremylt case CEED_GAUSS: 1092d1d35e2fSjeremylt ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d); 1093d7b241e6Sjeremylt break; 1094d7b241e6Sjeremylt case CEED_GAUSS_LOBATTO: 1095d1d35e2fSjeremylt ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d); 1096d7b241e6Sjeremylt break; 1097d7b241e6Sjeremylt } 10982b730f8bSJeremy L Thompson if (ierr != CEED_ERROR_SUCCESS) goto cleanup; 1099e15f9bd0SJeremy L Thompson 1100d7b241e6Sjeremylt // Build B, D matrix 1101d7b241e6Sjeremylt // Fornberg, 1998 1102c8c3fa7dSJeremy L Thompson for (CeedInt i = 0; i < Q; i++) { 1103d7b241e6Sjeremylt c1 = 1.0; 1104d1d35e2fSjeremylt c3 = nodes[0] - q_ref_1d[i]; 1105d1d35e2fSjeremylt interp_1d[i * P + 0] = 1.0; 1106c8c3fa7dSJeremy L Thompson for (CeedInt j = 1; j < P; j++) { 1107d7b241e6Sjeremylt c2 = 1.0; 1108d7b241e6Sjeremylt c4 = c3; 1109d1d35e2fSjeremylt c3 = nodes[j] - q_ref_1d[i]; 1110c8c3fa7dSJeremy L Thompson for (CeedInt k = 0; k < j; k++) { 1111d7b241e6Sjeremylt dx = nodes[j] - nodes[k]; 1112d7b241e6Sjeremylt c2 *= dx; 1113d7b241e6Sjeremylt if (k == j - 1) { 1114d1d35e2fSjeremylt grad_1d[i * P + j] = c1 * (interp_1d[i * P + k] - c4 * grad_1d[i * P + k]) / c2; 1115d1d35e2fSjeremylt interp_1d[i * P + j] = -c1 * c4 * interp_1d[i * P + k] / c2; 1116d7b241e6Sjeremylt } 1117d1d35e2fSjeremylt grad_1d[i * P + k] = (c3 * grad_1d[i * P + k] - interp_1d[i * P + k]) / dx; 1118d1d35e2fSjeremylt interp_1d[i * P + k] = c3 * interp_1d[i * P + k] / dx; 1119d7b241e6Sjeremylt } 1120d7b241e6Sjeremylt c1 = c2; 1121d7b241e6Sjeremylt } 1122d7b241e6Sjeremylt } 11239ac7b42eSJeremy L Thompson // Pass to CeedBasisCreateTensorH1 11242b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 1125e15f9bd0SJeremy L Thompson cleanup: 11262b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_1d)); 11272b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_1d)); 11282b730f8bSJeremy L Thompson CeedCall(CeedFree(&nodes)); 11292b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref_1d)); 11302b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight_1d)); 1131e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1132d7b241e6Sjeremylt } 1133d7b241e6Sjeremylt 1134b11c1e72Sjeremylt /** 1135ca94c3ddSJeremy L Thompson @brief Create a non tensor-product basis for \f$H^1\f$ discretizations 1136a8de75f0Sjeremylt 1137ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1138ea61e9acSJeremy L Thompson @param[in] topo Topology of element, e.g. hypercube, simplex, ect 1139ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1140ea61e9acSJeremy L Thompson @param[in] num_nodes Total number of nodes 1141ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1142ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`num_qpts * num_nodes`) matrix expressing the values of nodal basis functions at quadrature points 1143ca94c3ddSJeremy L Thompson @param[in] grad Row-major (`dim * num_qpts * num_nodes`) matrix expressing derivatives of nodal basis functions at quadrature points 1144ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element 1145ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1146ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1147a8de75f0Sjeremylt 1148a8de75f0Sjeremylt @return An error code: 0 - success, otherwise - failure 1149a8de75f0Sjeremylt 11507a982d89SJeremy L. Thompson @ref User 1151a8de75f0Sjeremylt **/ 11522b730f8bSJeremy L Thompson int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 11532b730f8bSJeremy L Thompson const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1154d1d35e2fSjeremylt CeedInt P = num_nodes, Q = num_qpts, dim = 0; 1155a8de75f0Sjeremylt 11565fe0d4faSjeremylt if (!ceed->BasisCreateH1) { 11575fe0d4faSjeremylt Ceed delegate; 11586574a04fSJeremy L Thompson 11592b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 11601ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateH1"); 11612b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis)); 1162e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11635fe0d4faSjeremylt } 11645fe0d4faSjeremylt 1165ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1166ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1167ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1168227444bfSJeremy L Thompson 11692b730f8bSJeremy L Thompson CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1170a8de75f0Sjeremylt 1171db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1172db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1173d1d35e2fSjeremylt (*basis)->ref_count = 1; 11746402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1175a8de75f0Sjeremylt (*basis)->dim = dim; 1176d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 1177d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 1178a8de75f0Sjeremylt (*basis)->P = P; 1179a8de75f0Sjeremylt (*basis)->Q = Q; 1180c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 11812b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * dim, &(*basis)->q_ref_1d)); 11822b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &(*basis)->q_weight_1d)); 1183ff3a0f91SJeremy L Thompson if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1184ff3a0f91SJeremy L Thompson if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 11852b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * P, &(*basis)->interp)); 11862b730f8bSJeremy L Thompson CeedCall(CeedCalloc(dim * Q * P, &(*basis)->grad)); 1187ff3a0f91SJeremy L Thompson if (interp) memcpy((*basis)->interp, interp, Q * P * sizeof(interp[0])); 1188ff3a0f91SJeremy L Thompson if (grad) memcpy((*basis)->grad, grad, dim * Q * P * sizeof(grad[0])); 11892b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, *basis)); 1190e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1191a8de75f0Sjeremylt } 1192a8de75f0Sjeremylt 1193a8de75f0Sjeremylt /** 1194859c15bbSJames Wright @brief Create a non tensor-product basis for \f$H(\mathrm{div})\f$ discretizations 119550c301a5SRezgar Shakeri 1196ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1197ea61e9acSJeremy 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 1198ea61e9acSJeremy L Thompson @param[in] num_comp Number of components (usually 1 for vectors in H(div) bases) 1199ca94c3ddSJeremy L Thompson @param[in] num_nodes Total number of nodes (DoFs per element) 1200ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1201ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points 1202ca94c3ddSJeremy L Thompson @param[in] div Row-major (`num_qpts * num_nodes`) matrix expressing divergence of basis functions at quadrature points 1203ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element 1204ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1205ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 120650c301a5SRezgar Shakeri 120750c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 120850c301a5SRezgar Shakeri 120950c301a5SRezgar Shakeri @ref User 121050c301a5SRezgar Shakeri **/ 12112b730f8bSJeremy L Thompson int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 12122b730f8bSJeremy L Thompson const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 121350c301a5SRezgar Shakeri CeedInt Q = num_qpts, P = num_nodes, dim = 0; 1214c4e3f59bSSebastian Grimberg 121550c301a5SRezgar Shakeri if (!ceed->BasisCreateHdiv) { 121650c301a5SRezgar Shakeri Ceed delegate; 12176574a04fSJeremy L Thompson 12182b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 12196574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv"); 12202b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis)); 122150c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 122250c301a5SRezgar Shakeri } 122350c301a5SRezgar Shakeri 1224ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1225ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1226ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1227227444bfSJeremy L Thompson 1228c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1229c4e3f59bSSebastian Grimberg 1230db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1231db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 123250c301a5SRezgar Shakeri (*basis)->ref_count = 1; 12336402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 123450c301a5SRezgar Shakeri (*basis)->dim = dim; 123550c301a5SRezgar Shakeri (*basis)->topo = topo; 123650c301a5SRezgar Shakeri (*basis)->num_comp = num_comp; 123750c301a5SRezgar Shakeri (*basis)->P = P; 123850c301a5SRezgar Shakeri (*basis)->Q = Q; 1239c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HDIV; 12402b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 12412b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 124250c301a5SRezgar Shakeri if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 124350c301a5SRezgar Shakeri if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 12442b730f8bSJeremy L Thompson CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 12452b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * P, &(*basis)->div)); 124650c301a5SRezgar Shakeri if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 124750c301a5SRezgar Shakeri if (div) memcpy((*basis)->div, div, Q * P * sizeof(div[0])); 12482b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, q_weight, *basis)); 124950c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 125050c301a5SRezgar Shakeri } 125150c301a5SRezgar Shakeri 125250c301a5SRezgar Shakeri /** 12534385fb7fSSebastian Grimberg @brief Create a non tensor-product basis for \f$H(\mathrm{curl})\f$ discretizations 1254c4e3f59bSSebastian Grimberg 1255ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1256c4e3f59bSSebastian Grimberg @param[in] topo Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below 1257ca94c3ddSJeremy L Thompson @param[in] num_comp Number of components (usually 1 for vectors in \f$H(\mathrm{curl})\f$ bases) 1258ca94c3ddSJeremy L Thompson @param[in] num_nodes Total number of nodes (DoFs per element) 1259c4e3f59bSSebastian Grimberg @param[in] num_qpts Total number of quadrature points 1260ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points 1261ca94c3ddSJeremy 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 1262ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element 1263ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1264ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1265c4e3f59bSSebastian Grimberg 1266c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1267c4e3f59bSSebastian Grimberg 1268c4e3f59bSSebastian Grimberg @ref User 1269c4e3f59bSSebastian Grimberg **/ 1270c4e3f59bSSebastian Grimberg int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 1271c4e3f59bSSebastian Grimberg const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1272c4e3f59bSSebastian Grimberg CeedInt Q = num_qpts, P = num_nodes, dim = 0, curl_comp = 0; 1273c4e3f59bSSebastian Grimberg 1274d075f50bSSebastian Grimberg if (!ceed->BasisCreateHcurl) { 1275c4e3f59bSSebastian Grimberg Ceed delegate; 12766574a04fSJeremy L Thompson 1277c4e3f59bSSebastian Grimberg CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 12786574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl"); 1279c4e3f59bSSebastian Grimberg CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis)); 1280c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1281c4e3f59bSSebastian Grimberg } 1282c4e3f59bSSebastian Grimberg 1283ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1284ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1285ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1286c4e3f59bSSebastian Grimberg 1287c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1288c4e3f59bSSebastian Grimberg curl_comp = (dim < 3) ? 1 : dim; 1289c4e3f59bSSebastian Grimberg 1290db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1291db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1292c4e3f59bSSebastian Grimberg (*basis)->ref_count = 1; 12936402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1294c4e3f59bSSebastian Grimberg (*basis)->dim = dim; 1295c4e3f59bSSebastian Grimberg (*basis)->topo = topo; 1296c4e3f59bSSebastian Grimberg (*basis)->num_comp = num_comp; 1297c4e3f59bSSebastian Grimberg (*basis)->P = P; 1298c4e3f59bSSebastian Grimberg (*basis)->Q = Q; 1299c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HCURL; 1300c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 1301c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 1302c4e3f59bSSebastian Grimberg if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1303c4e3f59bSSebastian Grimberg if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 1304c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 1305c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(curl_comp * Q * P, &(*basis)->curl)); 1306c4e3f59bSSebastian Grimberg if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 1307c4e3f59bSSebastian Grimberg if (curl) memcpy((*basis)->curl, curl, curl_comp * Q * P * sizeof(curl[0])); 1308c4e3f59bSSebastian Grimberg CeedCall(ceed->BasisCreateHcurl(topo, dim, P, Q, interp, curl, q_ref, q_weight, *basis)); 1309c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1310c4e3f59bSSebastian Grimberg } 1311c4e3f59bSSebastian Grimberg 1312c4e3f59bSSebastian Grimberg /** 1313ca94c3ddSJeremy L Thompson @brief Create a `CeedBasis` for projection from the nodes of `basis_from` to the nodes of `basis_to`. 1314ba59ac12SSebastian Grimberg 1315ca94c3ddSJeremy L Thompson Only @ref CEED_EVAL_INTERP will be valid for the new basis, `basis_project`. 1316ca94c3ddSJeremy L Thompson For \f$H^1\f$ spaces, @ref CEED_EVAL_GRAD will also be valid. 1317ca94c3ddSJeremy L Thompson The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization. 1318ca94c3ddSJeremy L Thompson The gradient (for the \f$H^1\f$ case) is given by `grad_project = interp_to^+ * grad_from`. 131915ad3917SSebastian Grimberg 132015ad3917SSebastian Grimberg Note: `basis_from` and `basis_to` must have compatible quadrature spaces. 132115ad3917SSebastian Grimberg 13229fd66db6SSebastian Grimberg Note: `basis_project` will have the same number of components as `basis_from`, regardless of the number of components that `basis_to` has. 13239fd66db6SSebastian Grimberg If `basis_from` has 3 components and `basis_to` has 5 components, then `basis_project` will have 3 components. 1324f113e5dcSJeremy L Thompson 1325ca94c3ddSJeremy L Thompson @param[in] basis_from `CeedBasis` to prolong from 1326ca94c3ddSJeremy L Thompson @param[in] basis_to `CeedBasis` to prolong to 1327ca94c3ddSJeremy L Thompson @param[out] basis_project Address of the variable where the newly created `CeedBasis` will be stored 1328f113e5dcSJeremy L Thompson 1329f113e5dcSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1330f113e5dcSJeremy L Thompson 1331f113e5dcSJeremy L Thompson @ref User 1332f113e5dcSJeremy L Thompson **/ 13332b730f8bSJeremy L Thompson int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project) { 1334f113e5dcSJeremy L Thompson Ceed ceed; 13351c66c397SJeremy L Thompson bool is_tensor; 13361c66c397SJeremy L Thompson CeedInt dim, num_comp; 13371c66c397SJeremy L Thompson CeedScalar *q_ref, *q_weight, *interp_project, *grad_project; 13381c66c397SJeremy L Thompson 13392b730f8bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis_to, &ceed)); 1340f113e5dcSJeremy L Thompson 1341ecc88aebSJeremy L Thompson // Create projection matrix 13422b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateProjectionMatrices(basis_from, basis_to, &interp_project, &grad_project)); 1343f113e5dcSJeremy L Thompson 1344f113e5dcSJeremy L Thompson // Build basis 13452b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis_to, &is_tensor)); 13462b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_to, &dim)); 13472b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_from, &num_comp)); 1348f113e5dcSJeremy L Thompson if (is_tensor) { 1349f113e5dcSJeremy L Thompson CeedInt P_1d_to, P_1d_from; 13501c66c397SJeremy L Thompson 13512b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_1d_from)); 13522b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_1d_to)); 13532b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_to, &q_ref)); 13542b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_to, &q_weight)); 13552b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, interp_project, grad_project, q_ref, q_weight, basis_project)); 1356f113e5dcSJeremy L Thompson } else { 1357de05fbb2SSebastian Grimberg // Even if basis_to and basis_from are not H1, the resulting basis is H1 for interpolation to work 1358f113e5dcSJeremy L Thompson CeedInt num_nodes_to, num_nodes_from; 13591c66c397SJeremy L Thompson CeedElemTopology topo; 13601c66c397SJeremy L Thompson 13611c66c397SJeremy L Thompson CeedCall(CeedBasisGetTopology(basis_to, &topo)); 13622b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_from, &num_nodes_from)); 13632b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_to, &num_nodes_to)); 13642b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_to * dim, &q_ref)); 13652b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_to, &q_weight)); 13662b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, q_ref, q_weight, basis_project)); 1367f113e5dcSJeremy L Thompson } 1368f113e5dcSJeremy L Thompson 1369f113e5dcSJeremy L Thompson // Cleanup 13702b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_project)); 13712b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_project)); 13722b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref)); 13732b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight)); 1374f113e5dcSJeremy L Thompson return CEED_ERROR_SUCCESS; 1375f113e5dcSJeremy L Thompson } 1376f113e5dcSJeremy L Thompson 1377f113e5dcSJeremy L Thompson /** 1378ca94c3ddSJeremy L Thompson @brief Copy the pointer to a `CeedBasis`. 13799560d06aSjeremylt 1380ca94c3ddSJeremy 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`. 1381ca94c3ddSJeremy L Thompson This `CeedBasis` will be destroyed if `*basis_copy` is the only reference to this `CeedBasis`. 1382ea61e9acSJeremy L Thompson 1383ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to copy reference to 1384ea61e9acSJeremy L Thompson @param[in,out] basis_copy Variable to store copied reference 13859560d06aSjeremylt 13869560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 13879560d06aSjeremylt 13889560d06aSjeremylt @ref User 13899560d06aSjeremylt **/ 13909560d06aSjeremylt int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) { 1391356036faSJeremy L Thompson if (basis != CEED_BASIS_NONE) CeedCall(CeedBasisReference(basis)); 13922b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(basis_copy)); 13939560d06aSjeremylt *basis_copy = basis; 13949560d06aSjeremylt return CEED_ERROR_SUCCESS; 13959560d06aSjeremylt } 13969560d06aSjeremylt 13979560d06aSjeremylt /** 1398ca94c3ddSJeremy L Thompson @brief View a `CeedBasis` 13997a982d89SJeremy L. Thompson 1400ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to view 1401ca94c3ddSJeremy L Thompson @param[in] stream Stream to view to, e.g., `stdout` 14027a982d89SJeremy L. Thompson 14037a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 14047a982d89SJeremy L. Thompson 14057a982d89SJeremy L. Thompson @ref User 14067a982d89SJeremy L. Thompson **/ 14077a982d89SJeremy L. Thompson int CeedBasisView(CeedBasis basis, FILE *stream) { 14081203703bSJeremy L Thompson bool is_tensor_basis; 14091203703bSJeremy L Thompson CeedElemTopology topo; 14101203703bSJeremy L Thompson CeedFESpace fe_space; 14111203703bSJeremy L Thompson 14121203703bSJeremy L Thompson // Basis data 14131203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 14141203703bSJeremy L Thompson CeedCall(CeedBasisGetTopology(basis, &topo)); 14151203703bSJeremy L Thompson CeedCall(CeedBasisGetFESpace(basis, &fe_space)); 14162b730f8bSJeremy L Thompson 141750c301a5SRezgar Shakeri // Print FE space and element topology of the basis 1418edf04919SJeremy L Thompson fprintf(stream, "CeedBasis in a %s on a %s element\n", CeedFESpaces[fe_space], CeedElemTopologies[topo]); 14191203703bSJeremy L Thompson if (is_tensor_basis) { 1420edf04919SJeremy L Thompson fprintf(stream, " P: %" CeedInt_FMT "\n Q: %" CeedInt_FMT "\n", basis->P_1d, basis->Q_1d); 142150c301a5SRezgar Shakeri } else { 1422edf04919SJeremy L Thompson fprintf(stream, " P: %" CeedInt_FMT "\n Q: %" CeedInt_FMT "\n", basis->P, basis->Q); 142350c301a5SRezgar Shakeri } 1424edf04919SJeremy L Thompson fprintf(stream, " dimension: %" CeedInt_FMT "\n field components: %" CeedInt_FMT "\n", basis->dim, basis->num_comp); 1425ea61e9acSJeremy L Thompson // Print quadrature data, interpolation/gradient/divergence/curl of the basis 14261203703bSJeremy L Thompson if (is_tensor_basis) { // tensor basis 14271203703bSJeremy L Thompson CeedInt P_1d, Q_1d; 14281203703bSJeremy L Thompson const CeedScalar *q_ref_1d, *q_weight_1d, *interp_1d, *grad_1d; 14291203703bSJeremy L Thompson 14301203703bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 14311203703bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 14321203703bSJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 14331203703bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d)); 14341203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 14351203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 14361203703bSJeremy L Thompson 14371203703bSJeremy L Thompson CeedCall(CeedScalarView("qref1d", "\t% 12.8f", 1, Q_1d, q_ref_1d, stream)); 14381203703bSJeremy L Thompson CeedCall(CeedScalarView("qweight1d", "\t% 12.8f", 1, Q_1d, q_weight_1d, stream)); 14391203703bSJeremy L Thompson CeedCall(CeedScalarView("interp1d", "\t% 12.8f", Q_1d, P_1d, interp_1d, stream)); 14401203703bSJeremy L Thompson CeedCall(CeedScalarView("grad1d", "\t% 12.8f", Q_1d, P_1d, grad_1d, stream)); 144150c301a5SRezgar Shakeri } else { // non-tensor basis 14421203703bSJeremy L Thompson CeedInt P, Q, dim, q_comp; 14431203703bSJeremy L Thompson const CeedScalar *q_ref, *q_weight, *interp, *grad, *div, *curl; 14441203703bSJeremy L Thompson 14451203703bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &P)); 14461203703bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &Q)); 14471203703bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 14481203703bSJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref)); 14491203703bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight)); 14501203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis, &interp)); 14511203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad(basis, &grad)); 14521203703bSJeremy L Thompson CeedCall(CeedBasisGetDiv(basis, &div)); 14531203703bSJeremy L Thompson CeedCall(CeedBasisGetCurl(basis, &curl)); 14541203703bSJeremy L Thompson 14551203703bSJeremy L Thompson CeedCall(CeedScalarView("qref", "\t% 12.8f", 1, Q * dim, q_ref, stream)); 14561203703bSJeremy L Thompson CeedCall(CeedScalarView("qweight", "\t% 12.8f", 1, Q, q_weight, stream)); 1457c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp)); 14581203703bSJeremy L Thompson CeedCall(CeedScalarView("interp", "\t% 12.8f", q_comp * Q, P, interp, stream)); 14591203703bSJeremy L Thompson if (grad) { 1460c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp)); 14611203703bSJeremy L Thompson CeedCall(CeedScalarView("grad", "\t% 12.8f", q_comp * Q, P, grad, stream)); 14627a982d89SJeremy L. Thompson } 14631203703bSJeremy L Thompson if (div) { 1464c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp)); 14651203703bSJeremy L Thompson CeedCall(CeedScalarView("div", "\t% 12.8f", q_comp * Q, P, div, stream)); 1466c4e3f59bSSebastian Grimberg } 14671203703bSJeremy L Thompson if (curl) { 1468c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp)); 14691203703bSJeremy L Thompson CeedCall(CeedScalarView("curl", "\t% 12.8f", q_comp * Q, P, curl, stream)); 147050c301a5SRezgar Shakeri } 147150c301a5SRezgar Shakeri } 1472e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14737a982d89SJeremy L. Thompson } 14747a982d89SJeremy L. Thompson 14757a982d89SJeremy L. Thompson /** 14767a982d89SJeremy L. Thompson @brief Apply basis evaluation from nodes to quadrature points or vice versa 14777a982d89SJeremy L. Thompson 1478ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1479ea61e9acSJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1480ca94c3ddSJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1481ca94c3ddSJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points; 1482ca94c3ddSJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes 1483ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 1484ca94c3ddSJeremy L Thompson @ref CEED_EVAL_INTERP to use interpolated values, 1485ca94c3ddSJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1486ca94c3ddSJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 1487ca94c3ddSJeremy L Thompson @ref CEED_EVAL_CURL to use curl, 1488ca94c3ddSJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1489ca94c3ddSJeremy L Thompson @param[in] u Input `CeedVector` 1490ca94c3ddSJeremy L Thompson @param[out] v Output `CeedVector` 14917a982d89SJeremy L. Thompson 14927a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 14937a982d89SJeremy L. Thompson 14947a982d89SJeremy L. Thompson @ref User 14957a982d89SJeremy L. Thompson **/ 14962b730f8bSJeremy L Thompson int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 1497c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 14981c66c397SJeremy L Thompson CeedSize u_length = 0, v_length; 14991203703bSJeremy L Thompson Ceed ceed; 15001c66c397SJeremy L Thompson 15011203703bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 15022b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 15032b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 1504c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 15052b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 15062b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 15072b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(v, &v_length)); 1508c8c3fa7dSJeremy L Thompson if (u) CeedCall(CeedVectorGetLength(u, &u_length)); 15097a982d89SJeremy L. Thompson 15101203703bSJeremy L Thompson CeedCheck(basis->Apply, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedBasisApply"); 1511e15f9bd0SJeremy L Thompson 1512e15f9bd0SJeremy L Thompson // Check compatibility of topological and geometrical dimensions 15136574a04fSJeremy L Thompson CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0 && u_length % num_qpts == 0) || 15146574a04fSJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0 && v_length % num_qpts == 0), 15151203703bSJeremy L Thompson ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions"); 15167a982d89SJeremy L. Thompson 1517e15f9bd0SJeremy L Thompson // Check vector lengths to prevent out of bounds issues 151899e754f0SJeremy L Thompson bool has_good_dims = true; 1519d1d35e2fSjeremylt switch (eval_mode) { 1520e15f9bd0SJeremy L Thompson case CEED_EVAL_NONE: 15212b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 15222b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 1523c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 1524c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 152599e754f0SJeremy L Thompson has_good_dims = 15266574a04fSJeremy L Thompson ((t_mode == CEED_TRANSPOSE && u_length >= num_elem * num_comp * num_qpts * q_comp && v_length >= num_elem * num_comp * num_nodes) || 15276574a04fSJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && v_length >= num_elem * num_qpts * num_comp * q_comp && u_length >= num_elem * num_comp * num_nodes)); 1528e15f9bd0SJeremy L Thompson break; 1529e15f9bd0SJeremy L Thompson case CEED_EVAL_WEIGHT: 153099e754f0SJeremy L Thompson has_good_dims = v_length >= num_elem * num_qpts; 1531e15f9bd0SJeremy L Thompson break; 1532e15f9bd0SJeremy L Thompson } 153399e754f0SJeremy L Thompson CeedCheck(has_good_dims, ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 1534e15f9bd0SJeremy L Thompson 15352b730f8bSJeremy L Thompson CeedCall(basis->Apply(basis, num_elem, t_mode, eval_mode, u, v)); 1536e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15377a982d89SJeremy L. Thompson } 15387a982d89SJeremy L. Thompson 15397a982d89SJeremy L. Thompson /** 1540c8c3fa7dSJeremy L Thompson @brief Apply basis evaluation from nodes to arbitrary points 1541c8c3fa7dSJeremy L Thompson 1542ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1543fc0f7cc6SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1544fc0f7cc6SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1545faed4840SJeremy L Thompson @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 1546ca94c3ddSJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 1547ca94c3ddSJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes 1548ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 1549ca94c3ddSJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1550ca94c3ddSJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1551ca94c3ddSJeremy L Thompson @param[in] x_ref `CeedVector` holding reference coordinates of each point 1552ca94c3ddSJeremy L Thompson @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 1553ca94c3ddSJeremy L Thompson @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 1554c8c3fa7dSJeremy L Thompson 1555c8c3fa7dSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1556c8c3fa7dSJeremy L Thompson 1557c8c3fa7dSJeremy L Thompson @ref User 1558c8c3fa7dSJeremy L Thompson **/ 1559fc0f7cc6SJeremy L Thompson int CeedBasisApplyAtPoints(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, 1560fc0f7cc6SJeremy L Thompson CeedVector x_ref, CeedVector u, CeedVector v) { 15611203703bSJeremy L Thompson bool is_tensor_basis; 1562fc0f7cc6SJeremy L Thompson CeedInt dim, num_comp, num_q_comp, num_nodes, P_1d = 1, Q_1d = 1, total_num_points = 0; 15631c66c397SJeremy L Thompson CeedSize x_length = 0, u_length = 0, v_length; 15641203703bSJeremy L Thompson Ceed ceed; 1565c8c3fa7dSJeremy L Thompson 15661203703bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 1567c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 1568c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 1569c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 1570c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 1571c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &num_q_comp)); 1572c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 1573c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetLength(v, &v_length)); 1574953190f4SJeremy L Thompson if (x_ref != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(x_ref, &x_length)); 1575953190f4SJeremy L Thompson if (u != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(u, &u_length)); 1576c8c3fa7dSJeremy L Thompson 1577c8c3fa7dSJeremy L Thompson // Check compatibility of topological and geometrical dimensions 1578fc0f7cc6SJeremy L Thompson for (CeedInt i = 0; i < num_elem; i++) total_num_points += num_points[i]; 1579953190f4SJeremy L Thompson CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0) || (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0) || 1580953190f4SJeremy L Thompson (eval_mode == CEED_EVAL_WEIGHT), 15811203703bSJeremy L Thompson ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions and number of points"); 1582c8c3fa7dSJeremy L Thompson 1583c8c3fa7dSJeremy L Thompson // Check compatibility coordinates vector 1584fc0f7cc6SJeremy L Thompson CeedCheck((x_length >= total_num_points * dim) || (eval_mode == CEED_EVAL_WEIGHT), ceed, CEED_ERROR_DIMENSION, 1585c8c3fa7dSJeremy L Thompson "Length of reference coordinate vector incompatible with basis dimension and number of points"); 1586c8c3fa7dSJeremy L Thompson 1587953190f4SJeremy L Thompson // Check CEED_EVAL_WEIGHT only on CEED_NOTRANSPOSE 15881203703bSJeremy L Thompson CeedCheck(eval_mode != CEED_EVAL_WEIGHT || t_mode == CEED_NOTRANSPOSE, ceed, CEED_ERROR_UNSUPPORTED, 1589953190f4SJeremy L Thompson "CEED_EVAL_WEIGHT only supported with CEED_NOTRANSPOSE"); 1590953190f4SJeremy L Thompson 1591c8c3fa7dSJeremy L Thompson // Check vector lengths to prevent out of bounds issues 159299e754f0SJeremy L Thompson bool has_good_dims = true; 1593c8c3fa7dSJeremy L Thompson switch (eval_mode) { 1594c8c3fa7dSJeremy L Thompson case CEED_EVAL_INTERP: 1595fc0f7cc6SJeremy L Thompson has_good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= total_num_points * num_q_comp || v_length >= num_elem * num_nodes * num_comp)) || 1596fc0f7cc6SJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && (v_length >= total_num_points * num_q_comp || u_length >= num_elem * num_nodes * num_comp))); 1597c8c3fa7dSJeremy L Thompson break; 1598c8c3fa7dSJeremy L Thompson case CEED_EVAL_GRAD: 1599fc0f7cc6SJeremy L Thompson has_good_dims = 1600fc0f7cc6SJeremy L Thompson ((t_mode == CEED_TRANSPOSE && (u_length >= total_num_points * num_q_comp * dim || v_length >= num_elem * num_nodes * num_comp)) || 1601fc0f7cc6SJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && (v_length >= total_num_points * num_q_comp * dim || u_length >= num_elem * num_nodes * num_comp))); 1602edfbf3a6SJeremy L Thompson break; 1603c8c3fa7dSJeremy L Thompson case CEED_EVAL_WEIGHT: 1604fc0f7cc6SJeremy L Thompson has_good_dims = t_mode == CEED_NOTRANSPOSE && (v_length >= total_num_points); 1605953190f4SJeremy L Thompson break; 160699e754f0SJeremy L Thompson // LCOV_EXCL_START 1607953190f4SJeremy L Thompson case CEED_EVAL_NONE: 1608c8c3fa7dSJeremy L Thompson case CEED_EVAL_DIV: 1609c8c3fa7dSJeremy L Thompson case CEED_EVAL_CURL: 16101203703bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points not supported for %s", CeedEvalModes[eval_mode]); 1611c8c3fa7dSJeremy L Thompson // LCOV_EXCL_STOP 1612c8c3fa7dSJeremy L Thompson } 161399e754f0SJeremy L Thompson CeedCheck(has_good_dims, ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 1614c8c3fa7dSJeremy L Thompson 1615c8c3fa7dSJeremy L Thompson // Backend method 1616c8c3fa7dSJeremy L Thompson if (basis->ApplyAtPoints) { 1617fc0f7cc6SJeremy L Thompson CeedCall(basis->ApplyAtPoints(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 1618c8c3fa7dSJeremy L Thompson return CEED_ERROR_SUCCESS; 1619c8c3fa7dSJeremy L Thompson } 1620c8c3fa7dSJeremy L Thompson 1621c8c3fa7dSJeremy L Thompson // Default implementation 16221203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 16231203703bSJeremy L Thompson CeedCheck(is_tensor_basis, ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points only supported for tensor product bases"); 1624fc0f7cc6SJeremy L Thompson CeedCheck(num_elem == 1, ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points only supported for a single element at a time"); 1625953190f4SJeremy L Thompson if (eval_mode == CEED_EVAL_WEIGHT) { 1626953190f4SJeremy L Thompson CeedCall(CeedVectorSetValue(v, 1.0)); 1627953190f4SJeremy L Thompson return CEED_ERROR_SUCCESS; 1628953190f4SJeremy L Thompson } 1629c8c3fa7dSJeremy L Thompson if (!basis->basis_chebyshev) { 1630c8c3fa7dSJeremy L Thompson // Build basis mapping from nodes to Chebyshev coefficients 1631c8c3fa7dSJeremy L Thompson CeedScalar *chebyshev_interp_1d, *chebyshev_grad_1d, *chebyshev_q_weight_1d; 1632b0cc4569SJeremy L Thompson const CeedScalar *q_ref_1d; 1633c8c3fa7dSJeremy L Thompson 163471a83b88SJeremy L Thompson CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 163571a83b88SJeremy L Thompson CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_grad_1d)); 1636c8c3fa7dSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &chebyshev_q_weight_1d)); 1637b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 1638b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d)); 1639c8c3fa7dSJeremy L Thompson 16401203703bSJeremy L Thompson CeedCall(CeedVectorCreate(ceed, num_comp * CeedIntPow(Q_1d, dim), &basis->vec_chebyshev)); 16411203703bSJeremy 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, 1642c8c3fa7dSJeremy L Thompson &basis->basis_chebyshev)); 1643c8c3fa7dSJeremy L Thompson 1644c8c3fa7dSJeremy L Thompson // Cleanup 1645c8c3fa7dSJeremy L Thompson CeedCall(CeedFree(&chebyshev_interp_1d)); 1646c8c3fa7dSJeremy L Thompson CeedCall(CeedFree(&chebyshev_grad_1d)); 1647c8c3fa7dSJeremy L Thompson CeedCall(CeedFree(&chebyshev_q_weight_1d)); 1648c8c3fa7dSJeremy L Thompson } 1649c8c3fa7dSJeremy L Thompson 1650c8c3fa7dSJeremy L Thompson // Create TensorContract object if needed, such as a basis from the GPU backends 1651c8c3fa7dSJeremy L Thompson if (!basis->contract) { 1652c8c3fa7dSJeremy L Thompson Ceed ceed_ref; 1653585a562dSJeremy L Thompson CeedBasis basis_ref = NULL; 1654c8c3fa7dSJeremy L Thompson 1655c8c3fa7dSJeremy L Thompson CeedCall(CeedInit("/cpu/self", &ceed_ref)); 1656c8c3fa7dSJeremy L Thompson // Only need matching tensor contraction dimensions, any type of basis will work 165771a83b88SJeremy L Thompson CeedCall(CeedBasisCreateTensorH1Lagrange(ceed_ref, dim, num_comp, P_1d, Q_1d, CEED_GAUSS, &basis_ref)); 1658585a562dSJeremy L Thompson // Note - clang-tidy doesn't know basis_ref->contract must be valid here 16591203703bSJeremy L Thompson CeedCheck(basis_ref && basis_ref->contract, ceed, CEED_ERROR_UNSUPPORTED, "Reference CPU ceed failed to create a tensor contraction object"); 1660585a562dSJeremy L Thompson CeedCall(CeedTensorContractReferenceCopy(basis_ref->contract, &basis->contract)); 1661c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisDestroy(&basis_ref)); 1662c8c3fa7dSJeremy L Thompson CeedCall(CeedDestroy(&ceed_ref)); 1663c8c3fa7dSJeremy L Thompson } 1664c8c3fa7dSJeremy L Thompson 1665c8c3fa7dSJeremy L Thompson // Basis evaluation 1666c8c3fa7dSJeremy L Thompson switch (t_mode) { 1667c8c3fa7dSJeremy L Thompson case CEED_NOTRANSPOSE: { 1668c8c3fa7dSJeremy L Thompson // Nodes to arbitrary points 1669c8c3fa7dSJeremy L Thompson CeedScalar *v_array; 1670c8c3fa7dSJeremy L Thompson const CeedScalar *chebyshev_coeffs, *x_array_read; 1671c8c3fa7dSJeremy L Thompson 1672c8c3fa7dSJeremy L Thompson // -- Interpolate to Chebyshev coefficients 1673c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, basis->vec_chebyshev)); 1674c8c3fa7dSJeremy L Thompson 1675c8c3fa7dSJeremy L Thompson // -- Evaluate Chebyshev polynomials at arbitrary points 1676c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs)); 1677c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read)); 1678c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(v, CEED_MEM_HOST, &v_array)); 1679edfbf3a6SJeremy L Thompson switch (eval_mode) { 1680edfbf3a6SJeremy L Thompson case CEED_EVAL_INTERP: { 1681c8c3fa7dSJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 1682c8c3fa7dSJeremy L Thompson 1683c8c3fa7dSJeremy L Thompson // ---- Values at point 1684fc0f7cc6SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 1685c8c3fa7dSJeremy L Thompson CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1; 1686c8c3fa7dSJeremy L Thompson 168753ef2869SZach Atkins for (CeedInt d = 0; d < dim; d++) { 16883778dbaaSJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 1689fc0f7cc6SJeremy L Thompson CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 1690c8c3fa7dSJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false, 16914608bdaaSJeremy L Thompson d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2])); 1692c8c3fa7dSJeremy L Thompson pre /= Q_1d; 1693c8c3fa7dSJeremy L Thompson post *= 1; 1694c8c3fa7dSJeremy L Thompson } 1695fc0f7cc6SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) v_array[c * total_num_points + p] = tmp[dim % 2][c]; 1696c8c3fa7dSJeremy L Thompson } 1697edfbf3a6SJeremy L Thompson break; 1698edfbf3a6SJeremy L Thompson } 1699edfbf3a6SJeremy L Thompson case CEED_EVAL_GRAD: { 1700edfbf3a6SJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 1701edfbf3a6SJeremy L Thompson 1702edfbf3a6SJeremy L Thompson // ---- Values at point 1703fc0f7cc6SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 1704edfbf3a6SJeremy L Thompson // Dim**2 contractions, apply grad when pass == dim 170553ef2869SZach Atkins for (CeedInt pass = 0; pass < dim; pass++) { 1706edfbf3a6SJeremy L Thompson CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1; 1707edfbf3a6SJeremy L Thompson 170853ef2869SZach Atkins for (CeedInt d = 0; d < dim; d++) { 17093778dbaaSJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 1710fc0f7cc6SJeremy L Thompson if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 1711fc0f7cc6SJeremy L Thompson else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 1712edfbf3a6SJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false, 17134608bdaaSJeremy L Thompson d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2])); 1714edfbf3a6SJeremy L Thompson pre /= Q_1d; 1715edfbf3a6SJeremy L Thompson post *= 1; 1716edfbf3a6SJeremy L Thompson } 1717fc0f7cc6SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) v_array[(pass * num_comp + c) * total_num_points + p] = tmp[dim % 2][c]; 1718edfbf3a6SJeremy L Thompson } 1719edfbf3a6SJeremy L Thompson } 1720edfbf3a6SJeremy L Thompson break; 1721edfbf3a6SJeremy L Thompson } 1722edfbf3a6SJeremy L Thompson default: 1723953190f4SJeremy L Thompson // Nothing to do, excluded above 1724edfbf3a6SJeremy L Thompson break; 1725c8c3fa7dSJeremy L Thompson } 1726c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(basis->vec_chebyshev, &chebyshev_coeffs)); 1727c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read)); 1728c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorRestoreArray(v, &v_array)); 1729c8c3fa7dSJeremy L Thompson break; 1730c8c3fa7dSJeremy L Thompson } 17312a94f45fSJeremy L Thompson case CEED_TRANSPOSE: { 17323778dbaaSJeremy L Thompson // Note: No switch on e_mode here because only CEED_EVAL_INTERP is supported at this time 17332a94f45fSJeremy L Thompson // Arbitrary points to nodes 17342a94f45fSJeremy L Thompson CeedScalar *chebyshev_coeffs; 17352a94f45fSJeremy L Thompson const CeedScalar *u_array, *x_array_read; 17362a94f45fSJeremy L Thompson 17371c66c397SJeremy L Thompson // -- Transpose of evaluation of Chebyshev polynomials at arbitrary points 17382a94f45fSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs)); 17392a94f45fSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read)); 17402a94f45fSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array)); 1741038a8942SZach Atkins 1742038a8942SZach Atkins switch (eval_mode) { 1743038a8942SZach Atkins case CEED_EVAL_INTERP: { 17442a94f45fSJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 17452a94f45fSJeremy L Thompson 17462a94f45fSJeremy L Thompson // ---- Values at point 1747fc0f7cc6SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 17482a94f45fSJeremy L Thompson CeedInt pre = num_comp * 1, post = 1; 17492a94f45fSJeremy L Thompson 1750fc0f7cc6SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[c * total_num_points + p]; 175153ef2869SZach Atkins for (CeedInt d = 0; d < dim; d++) { 17523778dbaaSJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 1753fc0f7cc6SJeremy L Thompson CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 17544608bdaaSJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, p > 0 && d == (dim - 1), tmp[d % 2], 17554608bdaaSJeremy L Thompson d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2])); 17562a94f45fSJeremy L Thompson pre /= 1; 17572a94f45fSJeremy L Thompson post *= Q_1d; 17582a94f45fSJeremy L Thompson } 17592a94f45fSJeremy L Thompson } 1760038a8942SZach Atkins break; 1761038a8942SZach Atkins } 1762038a8942SZach Atkins case CEED_EVAL_GRAD: { 1763038a8942SZach Atkins CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 1764038a8942SZach Atkins 1765038a8942SZach Atkins // ---- Values at point 1766fc0f7cc6SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 1767038a8942SZach Atkins // Dim**2 contractions, apply grad when pass == dim 1768038a8942SZach Atkins for (CeedInt pass = 0; pass < dim; pass++) { 1769038a8942SZach Atkins CeedInt pre = num_comp * 1, post = 1; 1770038a8942SZach Atkins 1771fc0f7cc6SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[(pass * num_comp + c) * total_num_points + p]; 1772038a8942SZach Atkins for (CeedInt d = 0; d < dim; d++) { 1773038a8942SZach Atkins // ------ Tensor contract with current Chebyshev polynomial values 1774fc0f7cc6SJeremy L Thompson if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 1775fc0f7cc6SJeremy L Thompson else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 17764608bdaaSJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, 17774608bdaaSJeremy L Thompson (p > 0 || (p == 0 && pass > 0)) && d == (dim - 1), tmp[d % 2], 17784608bdaaSJeremy L Thompson d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2])); 1779038a8942SZach Atkins pre /= 1; 1780038a8942SZach Atkins post *= Q_1d; 1781038a8942SZach Atkins } 1782038a8942SZach Atkins } 1783038a8942SZach Atkins } 1784038a8942SZach Atkins break; 1785038a8942SZach Atkins } 1786038a8942SZach Atkins default: 1787038a8942SZach Atkins // Nothing to do, excluded above 1788038a8942SZach Atkins break; 17892a94f45fSJeremy L Thompson } 17902a94f45fSJeremy L Thompson CeedCall(CeedVectorRestoreArray(basis->vec_chebyshev, &chebyshev_coeffs)); 17912a94f45fSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read)); 17922a94f45fSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(u, &u_array)); 17932a94f45fSJeremy L Thompson 17942a94f45fSJeremy L Thompson // -- Interpolate transpose from Chebyshev coefficients 17952a94f45fSJeremy L Thompson CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v)); 17962a94f45fSJeremy L Thompson break; 17972a94f45fSJeremy L Thompson } 1798c8c3fa7dSJeremy L Thompson } 1799c8c3fa7dSJeremy L Thompson return CEED_ERROR_SUCCESS; 1800c8c3fa7dSJeremy L Thompson } 1801c8c3fa7dSJeremy L Thompson 1802c8c3fa7dSJeremy L Thompson /** 18036e536b99SJeremy L Thompson @brief Get the `Ceed` associated with a `CeedBasis` 1804b7c9bbdaSJeremy L Thompson 1805ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1806ca94c3ddSJeremy L Thompson @param[out] ceed Variable to store `Ceed` 1807b7c9bbdaSJeremy L Thompson 1808b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1809b7c9bbdaSJeremy L Thompson 1810b7c9bbdaSJeremy L Thompson @ref Advanced 1811b7c9bbdaSJeremy L Thompson **/ 1812b7c9bbdaSJeremy L Thompson int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) { 18136e536b99SJeremy L Thompson *ceed = CeedBasisReturnCeed(basis); 1814b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1815b7c9bbdaSJeremy L Thompson } 1816b7c9bbdaSJeremy L Thompson 1817b7c9bbdaSJeremy L Thompson /** 18186e536b99SJeremy L Thompson @brief Return the `Ceed` associated with a `CeedBasis` 18196e536b99SJeremy L Thompson 18206e536b99SJeremy L Thompson @param[in] basis `CeedBasis` 18216e536b99SJeremy L Thompson 18226e536b99SJeremy L Thompson @return `Ceed` associated with the `basis` 18236e536b99SJeremy L Thompson 18246e536b99SJeremy L Thompson @ref Advanced 18256e536b99SJeremy L Thompson **/ 18266e536b99SJeremy L Thompson Ceed CeedBasisReturnCeed(CeedBasis basis) { return basis->ceed; } 18276e536b99SJeremy L Thompson 18286e536b99SJeremy L Thompson /** 1829ca94c3ddSJeremy L Thompson @brief Get dimension for given `CeedBasis` 18309d007619Sjeremylt 1831ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 18329d007619Sjeremylt @param[out] dim Variable to store dimension of basis 18339d007619Sjeremylt 18349d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18359d007619Sjeremylt 1836b7c9bbdaSJeremy L Thompson @ref Advanced 18379d007619Sjeremylt **/ 18389d007619Sjeremylt int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) { 18399d007619Sjeremylt *dim = basis->dim; 1840e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18419d007619Sjeremylt } 18429d007619Sjeremylt 18439d007619Sjeremylt /** 1844ca94c3ddSJeremy L Thompson @brief Get topology for given `CeedBasis` 1845d99fa3c5SJeremy L Thompson 1846ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1847d99fa3c5SJeremy L Thompson @param[out] topo Variable to store topology of basis 1848d99fa3c5SJeremy L Thompson 1849d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1850d99fa3c5SJeremy L Thompson 1851b7c9bbdaSJeremy L Thompson @ref Advanced 1852d99fa3c5SJeremy L Thompson **/ 1853d99fa3c5SJeremy L Thompson int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) { 1854d99fa3c5SJeremy L Thompson *topo = basis->topo; 1855e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1856d99fa3c5SJeremy L Thompson } 1857d99fa3c5SJeremy L Thompson 1858d99fa3c5SJeremy L Thompson /** 1859ca94c3ddSJeremy L Thompson @brief Get number of components for given `CeedBasis` 18609d007619Sjeremylt 1861ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1862ca94c3ddSJeremy L Thompson @param[out] num_comp Variable to store number of components 18639d007619Sjeremylt 18649d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18659d007619Sjeremylt 1866b7c9bbdaSJeremy L Thompson @ref Advanced 18679d007619Sjeremylt **/ 1868d1d35e2fSjeremylt int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) { 1869d1d35e2fSjeremylt *num_comp = basis->num_comp; 1870e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18719d007619Sjeremylt } 18729d007619Sjeremylt 18739d007619Sjeremylt /** 1874ca94c3ddSJeremy L Thompson @brief Get total number of nodes (in `dim` dimensions) of a `CeedBasis` 18759d007619Sjeremylt 1876ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 18779d007619Sjeremylt @param[out] P Variable to store number of nodes 18789d007619Sjeremylt 18799d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18809d007619Sjeremylt 18819d007619Sjeremylt @ref Utility 18829d007619Sjeremylt **/ 18839d007619Sjeremylt int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) { 18849d007619Sjeremylt *P = basis->P; 1885e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18869d007619Sjeremylt } 18879d007619Sjeremylt 18889d007619Sjeremylt /** 1889ca94c3ddSJeremy L Thompson @brief Get total number of nodes (in 1 dimension) of a `CeedBasis` 18909d007619Sjeremylt 1891ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1892d1d35e2fSjeremylt @param[out] P_1d Variable to store number of nodes 18939d007619Sjeremylt 18949d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18959d007619Sjeremylt 1896b7c9bbdaSJeremy L Thompson @ref Advanced 18979d007619Sjeremylt **/ 1898d1d35e2fSjeremylt int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) { 18996e536b99SJeremy L Thompson CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply P_1d for non-tensor CeedBasis"); 1900d1d35e2fSjeremylt *P_1d = basis->P_1d; 1901e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19029d007619Sjeremylt } 19039d007619Sjeremylt 19049d007619Sjeremylt /** 1905ca94c3ddSJeremy L Thompson @brief Get total number of quadrature points (in `dim` dimensions) of a `CeedBasis` 19069d007619Sjeremylt 1907ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 19089d007619Sjeremylt @param[out] Q Variable to store number of quadrature points 19099d007619Sjeremylt 19109d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19119d007619Sjeremylt 19129d007619Sjeremylt @ref Utility 19139d007619Sjeremylt **/ 19149d007619Sjeremylt int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) { 19159d007619Sjeremylt *Q = basis->Q; 1916e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19179d007619Sjeremylt } 19189d007619Sjeremylt 19199d007619Sjeremylt /** 1920ca94c3ddSJeremy L Thompson @brief Get total number of quadrature points (in 1 dimension) of a `CeedBasis` 19219d007619Sjeremylt 1922ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1923d1d35e2fSjeremylt @param[out] Q_1d Variable to store number of quadrature points 19249d007619Sjeremylt 19259d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19269d007619Sjeremylt 1927b7c9bbdaSJeremy L Thompson @ref Advanced 19289d007619Sjeremylt **/ 1929d1d35e2fSjeremylt int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) { 19306e536b99SJeremy L Thompson CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply Q_1d for non-tensor CeedBasis"); 1931d1d35e2fSjeremylt *Q_1d = basis->Q_1d; 1932e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19339d007619Sjeremylt } 19349d007619Sjeremylt 19359d007619Sjeremylt /** 1936ca94c3ddSJeremy L Thompson @brief Get reference coordinates of quadrature points (in `dim` dimensions) of a `CeedBasis` 19379d007619Sjeremylt 1938ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1939d1d35e2fSjeremylt @param[out] q_ref Variable to store reference coordinates of quadrature points 19409d007619Sjeremylt 19419d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19429d007619Sjeremylt 1943b7c9bbdaSJeremy L Thompson @ref Advanced 19449d007619Sjeremylt **/ 1945d1d35e2fSjeremylt int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) { 1946d1d35e2fSjeremylt *q_ref = basis->q_ref_1d; 1947e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19489d007619Sjeremylt } 19499d007619Sjeremylt 19509d007619Sjeremylt /** 1951ca94c3ddSJeremy L Thompson @brief Get quadrature weights of quadrature points (in `dim` dimensions) of a `CeedBasis` 19529d007619Sjeremylt 1953ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1954d1d35e2fSjeremylt @param[out] q_weight Variable to store quadrature weights 19559d007619Sjeremylt 19569d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19579d007619Sjeremylt 1958b7c9bbdaSJeremy L Thompson @ref Advanced 19599d007619Sjeremylt **/ 1960d1d35e2fSjeremylt int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) { 1961d1d35e2fSjeremylt *q_weight = basis->q_weight_1d; 1962e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19639d007619Sjeremylt } 19649d007619Sjeremylt 19659d007619Sjeremylt /** 1966ca94c3ddSJeremy L Thompson @brief Get interpolation matrix of a `CeedBasis` 19679d007619Sjeremylt 1968ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 19699d007619Sjeremylt @param[out] interp Variable to store interpolation matrix 19709d007619Sjeremylt 19719d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19729d007619Sjeremylt 1973b7c9bbdaSJeremy L Thompson @ref Advanced 19749d007619Sjeremylt **/ 19756c58de82SJeremy L Thompson int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) { 19766402da51SJeremy L Thompson if (!basis->interp && basis->is_tensor_basis) { 19779d007619Sjeremylt // Allocate 19782b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->Q * basis->P, &basis->interp)); 19799d007619Sjeremylt 19809d007619Sjeremylt // Initialize 19812b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->Q * basis->P; i++) basis->interp[i] = 1.0; 19829d007619Sjeremylt 19839d007619Sjeremylt // Calculate 19842b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 19852b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 19869d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 1987d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 1988d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 19891c66c397SJeremy L Thompson 1990d1d35e2fSjeremylt basis->interp[qpt * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 19919d007619Sjeremylt } 19929d007619Sjeremylt } 19932b730f8bSJeremy L Thompson } 19942b730f8bSJeremy L Thompson } 19959d007619Sjeremylt *interp = basis->interp; 1996e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19979d007619Sjeremylt } 19989d007619Sjeremylt 19999d007619Sjeremylt /** 2000ca94c3ddSJeremy L Thompson @brief Get 1D interpolation matrix of a tensor product `CeedBasis` 20019d007619Sjeremylt 2002ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2003d1d35e2fSjeremylt @param[out] interp_1d Variable to store interpolation matrix 20049d007619Sjeremylt 20059d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20069d007619Sjeremylt 20079d007619Sjeremylt @ref Backend 20089d007619Sjeremylt **/ 2009d1d35e2fSjeremylt int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) { 20101203703bSJeremy L Thompson bool is_tensor_basis; 20111203703bSJeremy L Thompson 20121203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 20136e536b99SJeremy L Thompson CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis"); 2014d1d35e2fSjeremylt *interp_1d = basis->interp_1d; 2015e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20169d007619Sjeremylt } 20179d007619Sjeremylt 20189d007619Sjeremylt /** 2019ca94c3ddSJeremy L Thompson @brief Get gradient matrix of a `CeedBasis` 20209d007619Sjeremylt 2021ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 20229d007619Sjeremylt @param[out] grad Variable to store gradient matrix 20239d007619Sjeremylt 20249d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20259d007619Sjeremylt 2026b7c9bbdaSJeremy L Thompson @ref Advanced 20279d007619Sjeremylt **/ 20286c58de82SJeremy L Thompson int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) { 20296402da51SJeremy L Thompson if (!basis->grad && basis->is_tensor_basis) { 20309d007619Sjeremylt // Allocate 20312b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->dim * basis->Q * basis->P, &basis->grad)); 20329d007619Sjeremylt 20339d007619Sjeremylt // Initialize 20342b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim * basis->Q * basis->P; i++) basis->grad[i] = 1.0; 20359d007619Sjeremylt 20369d007619Sjeremylt // Calculate 20372b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 20382b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim; i++) { 20392b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 20409d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 2041d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 2042d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 20431c66c397SJeremy L Thompson 20442b730f8bSJeremy L Thompson if (i == d) basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->grad_1d[q * basis->P_1d + p]; 20452b730f8bSJeremy L Thompson else basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 20462b730f8bSJeremy L Thompson } 20472b730f8bSJeremy L Thompson } 20482b730f8bSJeremy L Thompson } 20499d007619Sjeremylt } 20509d007619Sjeremylt } 20519d007619Sjeremylt *grad = basis->grad; 2052e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20539d007619Sjeremylt } 20549d007619Sjeremylt 20559d007619Sjeremylt /** 2056ca94c3ddSJeremy L Thompson @brief Get 1D gradient matrix of a tensor product `CeedBasis` 20579d007619Sjeremylt 2058ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2059d1d35e2fSjeremylt @param[out] grad_1d Variable to store gradient matrix 20609d007619Sjeremylt 20619d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20629d007619Sjeremylt 2063b7c9bbdaSJeremy L Thompson @ref Advanced 20649d007619Sjeremylt **/ 2065d1d35e2fSjeremylt int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) { 20661203703bSJeremy L Thompson bool is_tensor_basis; 20671203703bSJeremy L Thompson 20681203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 20696e536b99SJeremy L Thompson CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis"); 2070d1d35e2fSjeremylt *grad_1d = basis->grad_1d; 2071e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20729d007619Sjeremylt } 20739d007619Sjeremylt 20749d007619Sjeremylt /** 2075ca94c3ddSJeremy L Thompson @brief Get divergence matrix of a `CeedBasis` 207650c301a5SRezgar Shakeri 2077ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 207850c301a5SRezgar Shakeri @param[out] div Variable to store divergence matrix 207950c301a5SRezgar Shakeri 208050c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 208150c301a5SRezgar Shakeri 208250c301a5SRezgar Shakeri @ref Advanced 208350c301a5SRezgar Shakeri **/ 208450c301a5SRezgar Shakeri int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) { 208550c301a5SRezgar Shakeri *div = basis->div; 208650c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 208750c301a5SRezgar Shakeri } 208850c301a5SRezgar Shakeri 208950c301a5SRezgar Shakeri /** 2090ca94c3ddSJeremy L Thompson @brief Get curl matrix of a `CeedBasis` 2091c4e3f59bSSebastian Grimberg 2092ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2093c4e3f59bSSebastian Grimberg @param[out] curl Variable to store curl matrix 2094c4e3f59bSSebastian Grimberg 2095c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 2096c4e3f59bSSebastian Grimberg 2097c4e3f59bSSebastian Grimberg @ref Advanced 2098c4e3f59bSSebastian Grimberg **/ 2099c4e3f59bSSebastian Grimberg int CeedBasisGetCurl(CeedBasis basis, const CeedScalar **curl) { 2100c4e3f59bSSebastian Grimberg *curl = basis->curl; 2101c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 2102c4e3f59bSSebastian Grimberg } 2103c4e3f59bSSebastian Grimberg 2104c4e3f59bSSebastian Grimberg /** 2105ca94c3ddSJeremy L Thompson @brief Destroy a @ref CeedBasis 21067a982d89SJeremy L. Thompson 2107ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` to destroy 21087a982d89SJeremy L. Thompson 21097a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 21107a982d89SJeremy L. Thompson 21117a982d89SJeremy L. Thompson @ref User 21127a982d89SJeremy L. Thompson **/ 21137a982d89SJeremy L. Thompson int CeedBasisDestroy(CeedBasis *basis) { 2114356036faSJeremy L Thompson if (!*basis || *basis == CEED_BASIS_NONE || --(*basis)->ref_count > 0) { 2115ad6481ceSJeremy L Thompson *basis = NULL; 2116ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 2117ad6481ceSJeremy L Thompson } 21182b730f8bSJeremy L Thompson if ((*basis)->Destroy) CeedCall((*basis)->Destroy(*basis)); 21199831d45aSJeremy L Thompson CeedCall(CeedTensorContractDestroy(&(*basis)->contract)); 2120c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_ref_1d)); 2121c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_weight_1d)); 21222b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp)); 21232b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp_1d)); 21242b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad)); 21252b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad_1d)); 2126c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->div)); 2127c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->curl)); 2128c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorDestroy(&(*basis)->vec_chebyshev)); 2129c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisDestroy(&(*basis)->basis_chebyshev)); 21302b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*basis)->ceed)); 21312b730f8bSJeremy L Thompson CeedCall(CeedFree(basis)); 2132e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21337a982d89SJeremy L. Thompson } 21347a982d89SJeremy L. Thompson 21357a982d89SJeremy L. Thompson /** 2136b11c1e72Sjeremylt @brief Construct a Gauss-Legendre quadrature 2137b11c1e72Sjeremylt 2138ca94c3ddSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree `2*Q-1` exactly) 2139ca94c3ddSJeremy L Thompson @param[out] q_ref_1d Array of length `Q` to hold the abscissa on `[-1, 1]` 2140ca94c3ddSJeremy L Thompson @param[out] q_weight_1d Array of length `Q` to hold the weights 2141b11c1e72Sjeremylt 2142b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 2143dfdf5a53Sjeremylt 2144dfdf5a53Sjeremylt @ref Utility 2145b11c1e72Sjeremylt **/ 21462b730f8bSJeremy L Thompson int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2147d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0 * atan(1.0); 21481c66c397SJeremy L Thompson 2149d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 215092ae7e47SJeremy L Thompson for (CeedInt i = 0; i <= Q / 2; i++) { 2151d7b241e6Sjeremylt // Guess 2152d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(2 * i + 1) / ((CeedScalar)(2 * Q))); 2153d7b241e6Sjeremylt // Pn(xi) 2154d7b241e6Sjeremylt P0 = 1.0; 2155d7b241e6Sjeremylt P1 = xi; 2156d7b241e6Sjeremylt P2 = 0.0; 215792ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 2158d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2159d7b241e6Sjeremylt P0 = P1; 2160d7b241e6Sjeremylt P1 = P2; 2161d7b241e6Sjeremylt } 2162d7b241e6Sjeremylt // First Newton Step 2163d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2164d7b241e6Sjeremylt xi = xi - P2 / dP2; 2165d7b241e6Sjeremylt // Newton to convergence 216692ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(P2) > 10 * CEED_EPSILON; k++) { 2167d7b241e6Sjeremylt P0 = 1.0; 2168d7b241e6Sjeremylt P1 = xi; 216992ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 2170d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2171d7b241e6Sjeremylt P0 = P1; 2172d7b241e6Sjeremylt P1 = P2; 2173d7b241e6Sjeremylt } 2174d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2175d7b241e6Sjeremylt xi = xi - P2 / dP2; 2176d7b241e6Sjeremylt } 2177d7b241e6Sjeremylt // Save xi, wi 2178d7b241e6Sjeremylt wi = 2.0 / ((1.0 - xi * xi) * dP2 * dP2); 2179d1d35e2fSjeremylt q_weight_1d[i] = wi; 2180d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 2181d1d35e2fSjeremylt q_ref_1d[i] = -xi; 2182d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 2183d7b241e6Sjeremylt } 2184e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2185d7b241e6Sjeremylt } 2186d7b241e6Sjeremylt 2187b11c1e72Sjeremylt /** 2188b11c1e72Sjeremylt @brief Construct a Gauss-Legendre-Lobatto quadrature 2189b11c1e72Sjeremylt 2190ca94c3ddSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree `2*Q-3` exactly) 2191ca94c3ddSJeremy L Thompson @param[out] q_ref_1d Array of length `Q` to hold the abscissa on `[-1, 1]` 2192ca94c3ddSJeremy L Thompson @param[out] q_weight_1d Array of length `Q` to hold the weights 2193b11c1e72Sjeremylt 2194b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 2195dfdf5a53Sjeremylt 2196dfdf5a53Sjeremylt @ref Utility 2197b11c1e72Sjeremylt **/ 21982b730f8bSJeremy L Thompson int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2199d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0 * atan(1.0); 22001c66c397SJeremy L Thompson 2201d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 2202d7b241e6Sjeremylt // Set endpoints 22036574a04fSJeremy L Thompson CeedCheck(Q > 1, NULL, CEED_ERROR_DIMENSION, "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q); 2204d7b241e6Sjeremylt wi = 2.0 / ((CeedScalar)(Q * (Q - 1))); 2205d1d35e2fSjeremylt if (q_weight_1d) { 2206d1d35e2fSjeremylt q_weight_1d[0] = wi; 2207d1d35e2fSjeremylt q_weight_1d[Q - 1] = wi; 2208d7b241e6Sjeremylt } 2209d1d35e2fSjeremylt q_ref_1d[0] = -1.0; 2210d1d35e2fSjeremylt q_ref_1d[Q - 1] = 1.0; 2211d7b241e6Sjeremylt // Interior 221292ae7e47SJeremy L Thompson for (CeedInt i = 1; i <= (Q - 1) / 2; i++) { 2213d7b241e6Sjeremylt // Guess 2214d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(i) / (CeedScalar)(Q - 1)); 2215d7b241e6Sjeremylt // Pn(xi) 2216d7b241e6Sjeremylt P0 = 1.0; 2217d7b241e6Sjeremylt P1 = xi; 2218d7b241e6Sjeremylt P2 = 0.0; 221992ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 2220d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2221d7b241e6Sjeremylt P0 = P1; 2222d7b241e6Sjeremylt P1 = P2; 2223d7b241e6Sjeremylt } 2224d7b241e6Sjeremylt // First Newton step 2225d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2226d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2227d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 2228d7b241e6Sjeremylt // Newton to convergence 222992ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(dP2) > 10 * CEED_EPSILON; k++) { 2230d7b241e6Sjeremylt P0 = 1.0; 2231d7b241e6Sjeremylt P1 = xi; 223292ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 2233d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2234d7b241e6Sjeremylt P0 = P1; 2235d7b241e6Sjeremylt P1 = P2; 2236d7b241e6Sjeremylt } 2237d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2238d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2239d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 2240d7b241e6Sjeremylt } 2241d7b241e6Sjeremylt // Save xi, wi 2242d7b241e6Sjeremylt wi = 2.0 / (((CeedScalar)(Q * (Q - 1))) * P2 * P2); 2243d1d35e2fSjeremylt if (q_weight_1d) { 2244d1d35e2fSjeremylt q_weight_1d[i] = wi; 2245d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 2246d7b241e6Sjeremylt } 2247d1d35e2fSjeremylt q_ref_1d[i] = -xi; 2248d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 2249d7b241e6Sjeremylt } 2250e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2251d7b241e6Sjeremylt } 2252d7b241e6Sjeremylt 2253d7b241e6Sjeremylt /// @} 2254