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; 198e104ad11SJames Wright bool are_both_tensor; 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)); 2063f08121cSJeremy L Thompson CeedCheck(Q_to == Q_from, ceed, CEED_ERROR_DIMENSION, 2073f08121cSJeremy L Thompson "Bases must have compatible quadrature spaces." 208*23622755SJeremy L Thompson " 'basis_from' has %" CeedInt_FMT " points and 'basis_to' has %" CeedInt_FMT, 2093f08121cSJeremy L Thompson Q_from, Q_to); 2101c66c397SJeremy L Thompson Q = Q_to; 211a76a04e7SJeremy L Thompson 21214556e63SJeremy L Thompson // Check for matching tensor or non-tensor 213e104ad11SJames Wright { 214e104ad11SJames Wright bool is_tensor_to, is_tensor_from; 215e104ad11SJames Wright 2162b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to)); 2172b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from)); 218e104ad11SJames Wright are_both_tensor = is_tensor_to && is_tensor_from; 219e104ad11SJames Wright } 220e104ad11SJames Wright if (are_both_tensor) { 2212b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_to)); 2222b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_from)); 2232b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis_from, &Q)); 2246574a04fSJeremy L Thompson } else { 2252b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_to, &P_to)); 2262b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_from, &P_from)); 227a76a04e7SJeremy L Thompson } 228a76a04e7SJeremy L Thompson 22915ad3917SSebastian Grimberg // Check for matching FE space 23015ad3917SSebastian Grimberg CeedFESpace fe_space_to, fe_space_from; 2313f08121cSJeremy L Thompson 23215ad3917SSebastian Grimberg CeedCall(CeedBasisGetFESpace(basis_to, &fe_space_to)); 23315ad3917SSebastian Grimberg CeedCall(CeedBasisGetFESpace(basis_from, &fe_space_from)); 2343f08121cSJeremy L Thompson CeedCheck(fe_space_to == fe_space_from, ceed, CEED_ERROR_MINOR, 2353f08121cSJeremy L Thompson "Bases must both be the same FE space type." 2363f08121cSJeremy L Thompson " 'basis_from' is a %s and 'basis_to' is a %s", 2373f08121cSJeremy L Thompson CeedFESpaces[fe_space_from], CeedFESpaces[fe_space_to]); 23815ad3917SSebastian Grimberg 23914556e63SJeremy L Thompson // Get source matrices 24015ad3917SSebastian Grimberg CeedInt dim, q_comp = 1; 2412247a93fSRezgar Shakeri CeedScalar *interp_to_inv, *interp_from; 2421c66c397SJeremy L Thompson const CeedScalar *interp_to_source = NULL, *interp_from_source = NULL, *grad_from_source = NULL; 2431c66c397SJeremy L Thompson 244b3ed00e5SJames Wright CeedCall(CeedBasisGetDimension(basis_from, &dim)); 245e104ad11SJames Wright if (are_both_tensor) { 2462b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis_to, &interp_to_source)); 2472b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis_from, &interp_from_source)); 248a76a04e7SJeremy L Thompson } else { 24915ad3917SSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis_from, CEED_EVAL_INTERP, &q_comp)); 2502b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis_to, &interp_to_source)); 2512b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis_from, &interp_from_source)); 25215ad3917SSebastian Grimberg } 25315ad3917SSebastian Grimberg CeedCall(CeedMalloc(Q * P_from * q_comp, &interp_from)); 25415ad3917SSebastian Grimberg CeedCall(CeedCalloc(P_to * P_from, interp_project)); 25515ad3917SSebastian Grimberg 25615ad3917SSebastian Grimberg // `grad_project = interp_to^+ * grad_from` is computed for the H^1 space case so the 257de05fbb2SSebastian Grimberg // projection basis will have a gradient operation (allocated even if not H^1 for the 258de05fbb2SSebastian Grimberg // basis construction later on) 25915ad3917SSebastian Grimberg if (fe_space_to == CEED_FE_SPACE_H1) { 260e104ad11SJames Wright if (are_both_tensor) { 26115ad3917SSebastian Grimberg CeedCall(CeedBasisGetGrad1D(basis_from, &grad_from_source)); 26215ad3917SSebastian Grimberg } else { 2632b730f8bSJeremy L Thompson CeedCall(CeedBasisGetGrad(basis_from, &grad_from_source)); 264a76a04e7SJeremy L Thompson } 265de05fbb2SSebastian Grimberg } 266e104ad11SJames Wright CeedCall(CeedCalloc(P_to * P_from * (are_both_tensor ? 1 : dim), grad_project)); 26715ad3917SSebastian Grimberg 2682247a93fSRezgar Shakeri // Compute interp_to^+, pseudoinverse of interp_to 2692247a93fSRezgar Shakeri CeedCall(CeedCalloc(Q * q_comp * P_to, &interp_to_inv)); 2701203703bSJeremy L Thompson CeedCall(CeedMatrixPseudoinverse(ceed, interp_to_source, Q * q_comp, P_to, interp_to_inv)); 27114556e63SJeremy L Thompson // Build matrices 272e104ad11SJames Wright CeedInt num_matrices = 1 + (fe_space_to == CEED_FE_SPACE_H1) * (are_both_tensor ? 1 : dim); 27314556e63SJeremy L Thompson CeedScalar *input_from[num_matrices], *output_project[num_matrices]; 2741c66c397SJeremy L Thompson 27514556e63SJeremy L Thompson input_from[0] = (CeedScalar *)interp_from_source; 27614556e63SJeremy L Thompson output_project[0] = *interp_project; 27714556e63SJeremy L Thompson for (CeedInt m = 1; m < num_matrices; m++) { 27814556e63SJeremy L Thompson input_from[m] = (CeedScalar *)&grad_from_source[(m - 1) * Q * P_from]; 27902af4036SJeremy L Thompson output_project[m] = &((*grad_project)[(m - 1) * P_to * P_from]); 28014556e63SJeremy L Thompson } 28114556e63SJeremy L Thompson for (CeedInt m = 0; m < num_matrices; m++) { 2822247a93fSRezgar Shakeri // output_project = interp_to^+ * interp_from 28315ad3917SSebastian Grimberg memcpy(interp_from, input_from[m], Q * P_from * q_comp * sizeof(input_from[m][0])); 2842247a93fSRezgar Shakeri CeedCall(CeedMatrixMatrixMultiply(ceed, interp_to_inv, input_from[m], output_project[m], P_to, P_from, Q * q_comp)); 2852247a93fSRezgar Shakeri // Round zero to machine precision 2862247a93fSRezgar Shakeri for (CeedInt i = 0; i < P_to * P_from; i++) { 2872247a93fSRezgar Shakeri if (fabs(output_project[m][i]) < 10 * CEED_EPSILON) output_project[m][i] = 0.0; 288a76a04e7SJeremy L Thompson } 28914556e63SJeremy L Thompson } 29014556e63SJeremy L Thompson 29114556e63SJeremy L Thompson // Cleanup 2922247a93fSRezgar Shakeri CeedCall(CeedFree(&interp_to_inv)); 2932b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_from)); 294a76a04e7SJeremy L Thompson return CEED_ERROR_SUCCESS; 295a76a04e7SJeremy L Thompson } 296a76a04e7SJeremy L Thompson 2977a982d89SJeremy L. Thompson /// @} 2987a982d89SJeremy L. Thompson 2997a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 3007a982d89SJeremy L. Thompson /// Ceed Backend API 3017a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 3027a982d89SJeremy L. Thompson /// @addtogroup CeedBasisBackend 3037a982d89SJeremy L. Thompson /// @{ 3047a982d89SJeremy L. Thompson 3057a982d89SJeremy L. Thompson /** 306ca94c3ddSJeremy L Thompson @brief Return collocated gradient matrix 3077a982d89SJeremy L. Thompson 308ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 309ca94c3ddSJeremy L Thompson @param[out] collo_grad_1d Row-major (`Q_1d * Q_1d`) matrix expressing derivatives of basis functions at quadrature points 3107a982d89SJeremy L. Thompson 3117a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3127a982d89SJeremy L. Thompson 3137a982d89SJeremy L. Thompson @ref Backend 3147a982d89SJeremy L. Thompson **/ 315d1d35e2fSjeremylt int CeedBasisGetCollocatedGrad(CeedBasis basis, CeedScalar *collo_grad_1d) { 3167a982d89SJeremy L. Thompson Ceed ceed; 3172247a93fSRezgar Shakeri CeedInt P_1d, Q_1d; 3182247a93fSRezgar Shakeri CeedScalar *interp_1d_pinv; 3191203703bSJeremy L Thompson const CeedScalar *grad_1d, *interp_1d; 3201203703bSJeremy L Thompson 321ea61e9acSJeremy 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. 3222247a93fSRezgar Shakeri CeedCall(CeedBasisGetCeed(basis, &ceed)); 3232247a93fSRezgar Shakeri CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 3242247a93fSRezgar Shakeri CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 3257a982d89SJeremy L. Thompson 3262247a93fSRezgar Shakeri // Compute interp_1d^+, pseudoinverse of interp_1d 3272247a93fSRezgar Shakeri CeedCall(CeedCalloc(P_1d * Q_1d, &interp_1d_pinv)); 3281203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 3291203703bSJeremy L Thompson CeedCall(CeedMatrixPseudoinverse(ceed, interp_1d, Q_1d, P_1d, interp_1d_pinv)); 3301203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 3311203703bSJeremy L Thompson CeedCall(CeedMatrixMatrixMultiply(ceed, grad_1d, (const CeedScalar *)interp_1d_pinv, collo_grad_1d, Q_1d, Q_1d, P_1d)); 3327a982d89SJeremy L. Thompson 3332247a93fSRezgar Shakeri CeedCall(CeedFree(&interp_1d_pinv)); 334e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3357a982d89SJeremy L. Thompson } 3367a982d89SJeremy L. Thompson 3377a982d89SJeremy L. Thompson /** 338b0cc4569SJeremy L Thompson @brief Return 1D interpolation matrix to Chebyshev polynomial coefficients on quadrature space 339b0cc4569SJeremy L Thompson 340b0cc4569SJeremy L Thompson @param[in] basis `CeedBasis` 341b0cc4569SJeremy L Thompson @param[out] chebyshev_interp_1d Row-major (`P_1d * Q_1d`) matrix interpolating from basis nodes to Chebyshev polynomial coefficients 342b0cc4569SJeremy L Thompson 343b0cc4569SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 344b0cc4569SJeremy L Thompson 345b0cc4569SJeremy L Thompson @ref Backend 346b0cc4569SJeremy L Thompson **/ 347b0cc4569SJeremy L Thompson int CeedBasisGetChebyshevInterp1D(CeedBasis basis, CeedScalar *chebyshev_interp_1d) { 348b0cc4569SJeremy L Thompson CeedInt P_1d, Q_1d; 349b0cc4569SJeremy L Thompson CeedScalar *C, *chebyshev_coeffs_1d_inv; 350b0cc4569SJeremy L Thompson const CeedScalar *interp_1d, *q_ref_1d; 351b0cc4569SJeremy L Thompson Ceed ceed; 352b0cc4569SJeremy L Thompson 353b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 354b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 355b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 356b0cc4569SJeremy L Thompson 357b0cc4569SJeremy L Thompson // Build coefficient matrix 358bd83cbc5SJeremy L Thompson // -- Note: Clang-tidy needs this check 359bd83cbc5SJeremy L Thompson CeedCheck((P_1d > 0) && (Q_1d > 0), ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis dimensions are malformed"); 360b0cc4569SJeremy L Thompson CeedCall(CeedCalloc(Q_1d * Q_1d, &C)); 361b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 362b0cc4569SJeremy L Thompson for (CeedInt i = 0; i < Q_1d; i++) CeedCall(CeedChebyshevPolynomialsAtPoint(q_ref_1d[i], Q_1d, &C[i * Q_1d])); 363b0cc4569SJeremy L Thompson 364b0cc4569SJeremy L Thompson // Compute C^+, pseudoinverse of coefficient matrix 365b0cc4569SJeremy L Thompson CeedCall(CeedCalloc(Q_1d * Q_1d, &chebyshev_coeffs_1d_inv)); 366b0cc4569SJeremy L Thompson CeedCall(CeedMatrixPseudoinverse(ceed, C, Q_1d, Q_1d, chebyshev_coeffs_1d_inv)); 367b0cc4569SJeremy L Thompson 368b0cc4569SJeremy L Thompson // Build mapping from nodes to Chebyshev coefficients 369b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 370b0cc4569SJeremy L Thompson CeedCall(CeedMatrixMatrixMultiply(ceed, chebyshev_coeffs_1d_inv, interp_1d, chebyshev_interp_1d, Q_1d, P_1d, Q_1d)); 371b0cc4569SJeremy L Thompson 372b0cc4569SJeremy L Thompson // Cleanup 373b0cc4569SJeremy L Thompson CeedCall(CeedFree(&C)); 374b0cc4569SJeremy L Thompson CeedCall(CeedFree(&chebyshev_coeffs_1d_inv)); 375b0cc4569SJeremy L Thompson return CEED_ERROR_SUCCESS; 376b0cc4569SJeremy L Thompson } 377b0cc4569SJeremy L Thompson 378b0cc4569SJeremy L Thompson /** 379ca94c3ddSJeremy L Thompson @brief Get tensor status for given `CeedBasis` 3807a982d89SJeremy L. Thompson 381ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 382d1d35e2fSjeremylt @param[out] is_tensor Variable to store tensor status 3837a982d89SJeremy L. Thompson 3847a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3857a982d89SJeremy L. Thompson 3867a982d89SJeremy L. Thompson @ref Backend 3877a982d89SJeremy L. Thompson **/ 388d1d35e2fSjeremylt int CeedBasisIsTensor(CeedBasis basis, bool *is_tensor) { 3896402da51SJeremy L Thompson *is_tensor = basis->is_tensor_basis; 390e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3917a982d89SJeremy L. Thompson } 3927a982d89SJeremy L. Thompson 3937a982d89SJeremy L. Thompson /** 394ca94c3ddSJeremy L Thompson @brief Get backend data of a `CeedBasis` 3957a982d89SJeremy L. Thompson 396ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 3977a982d89SJeremy L. Thompson @param[out] data Variable to store data 3987a982d89SJeremy L. Thompson 3997a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4007a982d89SJeremy L. Thompson 4017a982d89SJeremy L. Thompson @ref Backend 4027a982d89SJeremy L. Thompson **/ 403777ff853SJeremy L Thompson int CeedBasisGetData(CeedBasis basis, void *data) { 404777ff853SJeremy L Thompson *(void **)data = basis->data; 405e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4067a982d89SJeremy L. Thompson } 4077a982d89SJeremy L. Thompson 4087a982d89SJeremy L. Thompson /** 409ca94c3ddSJeremy L Thompson @brief Set backend data of a `CeedBasis` 4107a982d89SJeremy L. Thompson 411ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` 412ea61e9acSJeremy L Thompson @param[in] data Data to set 4137a982d89SJeremy L. Thompson 4147a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4157a982d89SJeremy L. Thompson 4167a982d89SJeremy L. Thompson @ref Backend 4177a982d89SJeremy L. Thompson **/ 418777ff853SJeremy L Thompson int CeedBasisSetData(CeedBasis basis, void *data) { 419777ff853SJeremy L Thompson basis->data = data; 420e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4217a982d89SJeremy L. Thompson } 4227a982d89SJeremy L. Thompson 4237a982d89SJeremy L. Thompson /** 424ca94c3ddSJeremy L Thompson @brief Increment the reference counter for a `CeedBasis` 42534359f16Sjeremylt 426ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` to increment the reference counter 42734359f16Sjeremylt 42834359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 42934359f16Sjeremylt 43034359f16Sjeremylt @ref Backend 43134359f16Sjeremylt **/ 4329560d06aSjeremylt int CeedBasisReference(CeedBasis basis) { 43334359f16Sjeremylt basis->ref_count++; 43434359f16Sjeremylt return CEED_ERROR_SUCCESS; 43534359f16Sjeremylt } 43634359f16Sjeremylt 43734359f16Sjeremylt /** 438ca94c3ddSJeremy L Thompson @brief Get number of Q-vector components for given `CeedBasis` 439c4e3f59bSSebastian Grimberg 440ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 441ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 442ca94c3ddSJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 443ca94c3ddSJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 444ca94c3ddSJeremy L Thompson @ref CEED_EVAL_CURL to use curl 445c4e3f59bSSebastian Grimberg @param[out] q_comp Variable to store number of Q-vector components of basis 446c4e3f59bSSebastian Grimberg 447c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 448c4e3f59bSSebastian Grimberg 449c4e3f59bSSebastian Grimberg @ref Backend 450c4e3f59bSSebastian Grimberg **/ 451c4e3f59bSSebastian Grimberg int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode, CeedInt *q_comp) { 4521203703bSJeremy L Thompson CeedInt dim; 4531203703bSJeremy L Thompson 4541203703bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 455c4e3f59bSSebastian Grimberg switch (eval_mode) { 4561203703bSJeremy L Thompson case CEED_EVAL_INTERP: { 4571203703bSJeremy L Thompson CeedFESpace fe_space; 4581203703bSJeremy L Thompson 4591203703bSJeremy L Thompson CeedCall(CeedBasisGetFESpace(basis, &fe_space)); 4601203703bSJeremy L Thompson *q_comp = (fe_space == CEED_FE_SPACE_H1) ? 1 : dim; 4611203703bSJeremy L Thompson } break; 462c4e3f59bSSebastian Grimberg case CEED_EVAL_GRAD: 4631203703bSJeremy L Thompson *q_comp = dim; 464c4e3f59bSSebastian Grimberg break; 465c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 466c4e3f59bSSebastian Grimberg *q_comp = 1; 467c4e3f59bSSebastian Grimberg break; 468c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 4691203703bSJeremy L Thompson *q_comp = (dim < 3) ? 1 : dim; 470c4e3f59bSSebastian Grimberg break; 471c4e3f59bSSebastian Grimberg case CEED_EVAL_NONE: 472c4e3f59bSSebastian Grimberg case CEED_EVAL_WEIGHT: 473352a5e7cSSebastian Grimberg *q_comp = 1; 474c4e3f59bSSebastian Grimberg break; 475c4e3f59bSSebastian Grimberg } 476c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 477c4e3f59bSSebastian Grimberg } 478c4e3f59bSSebastian Grimberg 479c4e3f59bSSebastian Grimberg /** 480ca94c3ddSJeremy L Thompson @brief Estimate number of FLOPs required to apply `CeedBasis` in `t_mode` and `eval_mode` 4816e15d496SJeremy L Thompson 482ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to estimate FLOPs for 483ea61e9acSJeremy L Thompson @param[in] t_mode Apply basis or transpose 484ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CeedEvalMode 485ea61e9acSJeremy L Thompson @param[out] flops Address of variable to hold FLOPs estimate 4866e15d496SJeremy L Thompson 4876e15d496SJeremy L Thompson @ref Backend 4886e15d496SJeremy L Thompson **/ 4892b730f8bSJeremy L Thompson int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedSize *flops) { 4906e15d496SJeremy L Thompson bool is_tensor; 4916e15d496SJeremy L Thompson 4922b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor)); 4936e15d496SJeremy L Thompson if (is_tensor) { 4946e15d496SJeremy L Thompson CeedInt dim, num_comp, P_1d, Q_1d; 4951c66c397SJeremy L Thompson 4962b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 4972b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 4982b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 4992b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 5006e15d496SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 5012b730f8bSJeremy L Thompson P_1d = Q_1d; 5022b730f8bSJeremy L Thompson Q_1d = P_1d; 5036e15d496SJeremy L Thompson } 5046e15d496SJeremy L Thompson CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim - 1), post = 1; 5056e15d496SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 5066e15d496SJeremy L Thompson tensor_flops += 2 * pre * P_1d * post * Q_1d; 5076e15d496SJeremy L Thompson pre /= P_1d; 5086e15d496SJeremy L Thompson post *= Q_1d; 5096e15d496SJeremy L Thompson } 5106e15d496SJeremy L Thompson switch (eval_mode) { 5112b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 5122b730f8bSJeremy L Thompson *flops = 0; 5132b730f8bSJeremy L Thompson break; 5142b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 5152b730f8bSJeremy L Thompson *flops = tensor_flops; 5162b730f8bSJeremy L Thompson break; 5172b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 5182b730f8bSJeremy L Thompson *flops = tensor_flops * 2; 5192b730f8bSJeremy L Thompson break; 5206e15d496SJeremy L Thompson case CEED_EVAL_DIV: 5211203703bSJeremy L Thompson case CEED_EVAL_CURL: { 5226574a04fSJeremy L Thompson // LCOV_EXCL_START 5236e536b99SJeremy L Thompson return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported", 5246e536b99SJeremy L Thompson CeedEvalModes[eval_mode]); 5252b730f8bSJeremy L Thompson break; 5266e15d496SJeremy L Thompson // LCOV_EXCL_STOP 5271203703bSJeremy L Thompson } 5282b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 5292b730f8bSJeremy L Thompson *flops = dim * CeedIntPow(Q_1d, dim); 5302b730f8bSJeremy L Thompson break; 5316e15d496SJeremy L Thompson } 5326e15d496SJeremy L Thompson } else { 533c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 5341c66c397SJeremy L Thompson 5352b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 5362b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 537c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 5382b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 5392b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 5406e15d496SJeremy L Thompson switch (eval_mode) { 5412b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 5422b730f8bSJeremy L Thompson *flops = 0; 5432b730f8bSJeremy L Thompson break; 5442b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 5452b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 5462b730f8bSJeremy L Thompson case CEED_EVAL_DIV: 5472b730f8bSJeremy L Thompson case CEED_EVAL_CURL: 548c4e3f59bSSebastian Grimberg *flops = num_nodes * num_qpts * num_comp * q_comp; 5492b730f8bSJeremy L Thompson break; 5502b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 5512b730f8bSJeremy L Thompson *flops = 0; 5522b730f8bSJeremy L Thompson break; 5536e15d496SJeremy L Thompson } 5546e15d496SJeremy L Thompson } 5556e15d496SJeremy L Thompson return CEED_ERROR_SUCCESS; 5566e15d496SJeremy L Thompson } 5576e15d496SJeremy L Thompson 5586e15d496SJeremy L Thompson /** 559ca94c3ddSJeremy L Thompson @brief Get `CeedFESpace` for a `CeedBasis` 560c4e3f59bSSebastian Grimberg 561ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 562ca94c3ddSJeremy L Thompson @param[out] fe_space Variable to store `CeedFESpace` 563c4e3f59bSSebastian Grimberg 564c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 565c4e3f59bSSebastian Grimberg 566c4e3f59bSSebastian Grimberg @ref Backend 567c4e3f59bSSebastian Grimberg **/ 568c4e3f59bSSebastian Grimberg int CeedBasisGetFESpace(CeedBasis basis, CeedFESpace *fe_space) { 569c4e3f59bSSebastian Grimberg *fe_space = basis->fe_space; 570c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 571c4e3f59bSSebastian Grimberg } 572c4e3f59bSSebastian Grimberg 573c4e3f59bSSebastian Grimberg /** 574ca94c3ddSJeremy L Thompson @brief Get dimension for given `CeedElemTopology` 5757a982d89SJeremy L. Thompson 576ca94c3ddSJeremy L Thompson @param[in] topo `CeedElemTopology` 5777a982d89SJeremy L. Thompson @param[out] dim Variable to store dimension of topology 5787a982d89SJeremy L. Thompson 5797a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5807a982d89SJeremy L. Thompson 5817a982d89SJeremy L. Thompson @ref Backend 5827a982d89SJeremy L. Thompson **/ 5837a982d89SJeremy L. Thompson int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim) { 5847a982d89SJeremy L. Thompson *dim = (CeedInt)topo >> 16; 585e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5867a982d89SJeremy L. Thompson } 5877a982d89SJeremy L. Thompson 5887a982d89SJeremy L. Thompson /** 589ca94c3ddSJeremy L Thompson @brief Get `CeedTensorContract` of a `CeedBasis` 5907a982d89SJeremy L. Thompson 591ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 592ca94c3ddSJeremy L Thompson @param[out] contract Variable to store `CeedTensorContract` 5937a982d89SJeremy L. Thompson 5947a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5957a982d89SJeremy L. Thompson 5967a982d89SJeremy L. Thompson @ref Backend 5977a982d89SJeremy L. Thompson **/ 5987a982d89SJeremy L. Thompson int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract) { 5997a982d89SJeremy L. Thompson *contract = basis->contract; 600e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6017a982d89SJeremy L. Thompson } 6027a982d89SJeremy L. Thompson 6037a982d89SJeremy L. Thompson /** 604ca94c3ddSJeremy L Thompson @brief Set `CeedTensorContract` of a `CeedBasis` 6057a982d89SJeremy L. Thompson 606ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` 607ca94c3ddSJeremy L Thompson @param[in] contract `CeedTensorContract` to set 6087a982d89SJeremy L. Thompson 6097a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 6107a982d89SJeremy L. Thompson 6117a982d89SJeremy L. Thompson @ref Backend 6127a982d89SJeremy L. Thompson **/ 61334359f16Sjeremylt int CeedBasisSetTensorContract(CeedBasis basis, CeedTensorContract contract) { 61434359f16Sjeremylt basis->contract = contract; 6152b730f8bSJeremy L Thompson CeedCall(CeedTensorContractReference(contract)); 616e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6177a982d89SJeremy L. Thompson } 6187a982d89SJeremy L. Thompson 6197a982d89SJeremy L. Thompson /** 620ca94c3ddSJeremy L Thompson @brief Return a reference implementation of matrix multiplication \f$C = A B\f$. 621ba59ac12SSebastian Grimberg 622ca94c3ddSJeremy L Thompson Note: This is a reference implementation for CPU `CeedScalar` pointers that is not intended for high performance. 6237a982d89SJeremy L. Thompson 624ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 625ca94c3ddSJeremy L Thompson @param[in] mat_A Row-major matrix `A` 626ca94c3ddSJeremy L Thompson @param[in] mat_B Row-major matrix `B` 627ca94c3ddSJeremy L Thompson @param[out] mat_C Row-major output matrix `C` 628ca94c3ddSJeremy L Thompson @param[in] m Number of rows of `C` 629ca94c3ddSJeremy L Thompson @param[in] n Number of columns of `C` 630ca94c3ddSJeremy L Thompson @param[in] kk Number of columns of `A`/rows of `B` 6317a982d89SJeremy L. Thompson 6327a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 6337a982d89SJeremy L. Thompson 6347a982d89SJeremy L. Thompson @ref Utility 6357a982d89SJeremy L. Thompson **/ 6362b730f8bSJeremy L Thompson int CeedMatrixMatrixMultiply(Ceed ceed, const CeedScalar *mat_A, const CeedScalar *mat_B, CeedScalar *mat_C, CeedInt m, CeedInt n, CeedInt kk) { 6372b730f8bSJeremy L Thompson for (CeedInt i = 0; i < m; i++) { 6387a982d89SJeremy L. Thompson for (CeedInt j = 0; j < n; j++) { 6397a982d89SJeremy L. Thompson CeedScalar sum = 0; 6401c66c397SJeremy L Thompson 6412b730f8bSJeremy L Thompson for (CeedInt k = 0; k < kk; k++) sum += mat_A[k + i * kk] * mat_B[j + k * n]; 642d1d35e2fSjeremylt mat_C[j + i * n] = sum; 6437a982d89SJeremy L. Thompson } 6442b730f8bSJeremy L Thompson } 645e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6467a982d89SJeremy L. Thompson } 6477a982d89SJeremy L. Thompson 648ba59ac12SSebastian Grimberg /** 649ba59ac12SSebastian Grimberg @brief Return QR Factorization of a matrix 650ba59ac12SSebastian Grimberg 651ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 652ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 653ca94c3ddSJeremy L Thompson @param[in,out] tau Vector of length `m` of scaling factors 654ba59ac12SSebastian Grimberg @param[in] m Number of rows 655ba59ac12SSebastian Grimberg @param[in] n Number of columns 656ba59ac12SSebastian Grimberg 657ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 658ba59ac12SSebastian Grimberg 659ba59ac12SSebastian Grimberg @ref Utility 660ba59ac12SSebastian Grimberg **/ 661ba59ac12SSebastian Grimberg int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau, CeedInt m, CeedInt n) { 662ba59ac12SSebastian Grimberg CeedScalar v[m]; 663ba59ac12SSebastian Grimberg 664ba59ac12SSebastian Grimberg // Check matrix shape 6656574a04fSJeremy L Thompson CeedCheck(n <= m, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute QR factorization with n > m"); 666ba59ac12SSebastian Grimberg 667ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 6681c66c397SJeremy L Thompson CeedScalar sigma = 0.0; 6691c66c397SJeremy L Thompson 670ba59ac12SSebastian Grimberg if (i >= m - 1) { // last row of matrix, no reflection needed 671ba59ac12SSebastian Grimberg tau[i] = 0.; 672ba59ac12SSebastian Grimberg break; 673ba59ac12SSebastian Grimberg } 674ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 675ba59ac12SSebastian Grimberg v[i] = mat[i + n * i]; 676ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) { 677ba59ac12SSebastian Grimberg v[j] = mat[i + n * j]; 678ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 679ba59ac12SSebastian Grimberg } 6801c66c397SJeremy L Thompson const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:m] 6811c66c397SJeremy L Thompson const CeedScalar R_ii = -copysign(norm, v[i]); 6821c66c397SJeremy L Thompson 683ba59ac12SSebastian Grimberg v[i] -= R_ii; 684ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 685ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 686ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 687ba59ac12SSebastian Grimberg tau[i] = 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 688ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] /= v[i]; 689ba59ac12SSebastian Grimberg 690ba59ac12SSebastian Grimberg // Apply Householder reflector to lower right panel 691ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[i * n + i + 1], &v[i], tau[i], m - i, n - i - 1, n, 1); 692ba59ac12SSebastian Grimberg // Save v 693ba59ac12SSebastian Grimberg mat[i + n * i] = R_ii; 694ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) mat[i + n * j] = v[j]; 695ba59ac12SSebastian Grimberg } 696ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 697ba59ac12SSebastian Grimberg } 698ba59ac12SSebastian Grimberg 699ba59ac12SSebastian Grimberg /** 700ba59ac12SSebastian Grimberg @brief Apply Householder Q matrix 701ba59ac12SSebastian Grimberg 702ca94c3ddSJeremy 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$. 703ba59ac12SSebastian Grimberg 704ba59ac12SSebastian Grimberg @param[in,out] mat_A Matrix to apply Householder Q to, in place 705ba59ac12SSebastian Grimberg @param[in] mat_Q Householder Q matrix 706ba59ac12SSebastian Grimberg @param[in] tau Householder scaling factors 707ba59ac12SSebastian Grimberg @param[in] t_mode Transpose mode for application 708ca94c3ddSJeremy L Thompson @param[in] m Number of rows in `A` 709ca94c3ddSJeremy L Thompson @param[in] n Number of columns in `A` 710ca94c3ddSJeremy L Thompson @param[in] k Number of elementary reflectors in Q, `k < m` 711ca94c3ddSJeremy L Thompson @param[in] row Row stride in `A` 712ca94c3ddSJeremy L Thompson @param[in] col Col stride in `A` 713ba59ac12SSebastian Grimberg 714ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 715ba59ac12SSebastian Grimberg 716c4e3f59bSSebastian Grimberg @ref Utility 717ba59ac12SSebastian Grimberg **/ 718ba59ac12SSebastian Grimberg int CeedHouseholderApplyQ(CeedScalar *mat_A, const CeedScalar *mat_Q, const CeedScalar *tau, CeedTransposeMode t_mode, CeedInt m, CeedInt n, 719ba59ac12SSebastian Grimberg CeedInt k, CeedInt row, CeedInt col) { 720ba59ac12SSebastian Grimberg CeedScalar *v; 7211c66c397SJeremy L Thompson 722ba59ac12SSebastian Grimberg CeedCall(CeedMalloc(m, &v)); 723ba59ac12SSebastian Grimberg for (CeedInt ii = 0; ii < k; ii++) { 724ba59ac12SSebastian Grimberg CeedInt i = t_mode == CEED_TRANSPOSE ? ii : k - 1 - ii; 725ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] = mat_Q[j * k + i]; 726ba59ac12SSebastian Grimberg // Apply Householder reflector (I - tau v v^T) collo_grad_1d^T 727ba59ac12SSebastian Grimberg CeedCall(CeedHouseholderReflect(&mat_A[i * row], &v[i], tau[i], m - i, n, row, col)); 728ba59ac12SSebastian Grimberg } 729ba59ac12SSebastian Grimberg CeedCall(CeedFree(&v)); 730ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 731ba59ac12SSebastian Grimberg } 732ba59ac12SSebastian Grimberg 733ba59ac12SSebastian Grimberg /** 7342247a93fSRezgar Shakeri @brief Return pseudoinverse of a matrix 7352247a93fSRezgar Shakeri 7362247a93fSRezgar Shakeri @param[in] ceed Ceed context for error handling 7372247a93fSRezgar Shakeri @param[in] mat Row-major matrix to compute pseudoinverse of 7382247a93fSRezgar Shakeri @param[in] m Number of rows 7392247a93fSRezgar Shakeri @param[in] n Number of columns 7402247a93fSRezgar Shakeri @param[out] mat_pinv Row-major pseudoinverse matrix 7412247a93fSRezgar Shakeri 7422247a93fSRezgar Shakeri @return An error code: 0 - success, otherwise - failure 7432247a93fSRezgar Shakeri 7442247a93fSRezgar Shakeri @ref Utility 7452247a93fSRezgar Shakeri **/ 7461203703bSJeremy L Thompson int CeedMatrixPseudoinverse(Ceed ceed, const CeedScalar *mat, CeedInt m, CeedInt n, CeedScalar *mat_pinv) { 7472247a93fSRezgar Shakeri CeedScalar *tau, *I, *mat_copy; 7482247a93fSRezgar Shakeri 7492247a93fSRezgar Shakeri CeedCall(CeedCalloc(m, &tau)); 7502247a93fSRezgar Shakeri CeedCall(CeedCalloc(m * m, &I)); 7512247a93fSRezgar Shakeri CeedCall(CeedCalloc(m * n, &mat_copy)); 7522247a93fSRezgar Shakeri memcpy(mat_copy, mat, m * n * sizeof mat[0]); 7532247a93fSRezgar Shakeri 7542247a93fSRezgar Shakeri // QR Factorization, mat = Q R 7552247a93fSRezgar Shakeri CeedCall(CeedQRFactorization(ceed, mat_copy, tau, m, n)); 7562247a93fSRezgar Shakeri 7572247a93fSRezgar Shakeri // -- Apply Q^T, I = Q^T * I 7582247a93fSRezgar Shakeri for (CeedInt i = 0; i < m; i++) I[i * m + i] = 1.0; 7592247a93fSRezgar Shakeri CeedCall(CeedHouseholderApplyQ(I, mat_copy, tau, CEED_TRANSPOSE, m, m, n, m, 1)); 7602247a93fSRezgar Shakeri // -- Apply R_inv, mat_pinv = R_inv * Q^T 7612247a93fSRezgar Shakeri for (CeedInt j = 0; j < m; j++) { // Column j 7622247a93fSRezgar Shakeri mat_pinv[j + m * (n - 1)] = I[j + m * (n - 1)] / mat_copy[n * n - 1]; 7632247a93fSRezgar Shakeri for (CeedInt i = n - 2; i >= 0; i--) { // Row i 7642247a93fSRezgar Shakeri mat_pinv[j + m * i] = I[j + m * i]; 7652247a93fSRezgar Shakeri for (CeedInt k = i + 1; k < n; k++) mat_pinv[j + m * i] -= mat_copy[k + n * i] * mat_pinv[j + m * k]; 7662247a93fSRezgar Shakeri mat_pinv[j + m * i] /= mat_copy[i + n * i]; 7672247a93fSRezgar Shakeri } 7682247a93fSRezgar Shakeri } 7692247a93fSRezgar Shakeri 7702247a93fSRezgar Shakeri // Cleanup 7712247a93fSRezgar Shakeri CeedCall(CeedFree(&I)); 7722247a93fSRezgar Shakeri CeedCall(CeedFree(&tau)); 7732247a93fSRezgar Shakeri CeedCall(CeedFree(&mat_copy)); 7742247a93fSRezgar Shakeri return CEED_ERROR_SUCCESS; 7752247a93fSRezgar Shakeri } 7762247a93fSRezgar Shakeri 7772247a93fSRezgar Shakeri /** 778ba59ac12SSebastian Grimberg @brief Return symmetric Schur decomposition of the symmetric matrix mat via symmetric QR factorization 779ba59ac12SSebastian Grimberg 780ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 781ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 782ba59ac12SSebastian Grimberg @param[out] lambda Vector of length n of eigenvalues 783ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 784ba59ac12SSebastian Grimberg 785ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 786ba59ac12SSebastian Grimberg 787ba59ac12SSebastian Grimberg @ref Utility 788ba59ac12SSebastian Grimberg **/ 7892c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 7902c2ea1dbSJeremy L Thompson int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat, CeedScalar *lambda, CeedInt n) { 791ba59ac12SSebastian Grimberg // Check bounds for clang-tidy 7926574a04fSJeremy L Thompson CeedCheck(n > 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute symmetric Schur decomposition of scalars"); 793ba59ac12SSebastian Grimberg 794ba59ac12SSebastian Grimberg CeedScalar v[n - 1], tau[n - 1], mat_T[n * n]; 795ba59ac12SSebastian Grimberg 796ba59ac12SSebastian Grimberg // Copy mat to mat_T and set mat to I 797ba59ac12SSebastian Grimberg memcpy(mat_T, mat, n * n * sizeof(mat[0])); 798ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 799ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) mat[j + n * i] = (i == j) ? 1 : 0; 800ba59ac12SSebastian Grimberg } 801ba59ac12SSebastian Grimberg 802ba59ac12SSebastian Grimberg // Reduce to tridiagonal 803ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - 1; i++) { 804ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 805ba59ac12SSebastian Grimberg CeedScalar sigma = 0.0; 8061c66c397SJeremy L Thompson 807ba59ac12SSebastian Grimberg v[i] = mat_T[i + n * (i + 1)]; 808ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 809ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 810ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 811ba59ac12SSebastian Grimberg } 8121c66c397SJeremy L Thompson const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:n-1] 8131c66c397SJeremy L Thompson const CeedScalar R_ii = -copysign(norm, v[i]); 8141c66c397SJeremy L Thompson 815ba59ac12SSebastian Grimberg v[i] -= R_ii; 816ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 817ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 818ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 819ba59ac12SSebastian Grimberg tau[i] = i == n - 2 ? 2 : 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 820ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) v[j] /= v[i]; 821ba59ac12SSebastian Grimberg 822ba59ac12SSebastian Grimberg // Update sub and super diagonal 823ba59ac12SSebastian Grimberg for (CeedInt j = i + 2; j < n; j++) { 824ba59ac12SSebastian Grimberg mat_T[i + n * j] = 0; 825ba59ac12SSebastian Grimberg mat_T[j + n * i] = 0; 826ba59ac12SSebastian Grimberg } 827ba59ac12SSebastian Grimberg // Apply symmetric Householder reflector to lower right panel 828ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 829ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), 1, n); 830ba59ac12SSebastian Grimberg 831ba59ac12SSebastian Grimberg // Save v 832ba59ac12SSebastian Grimberg mat_T[i + n * (i + 1)] = R_ii; 833ba59ac12SSebastian Grimberg mat_T[(i + 1) + n * i] = R_ii; 834ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 835ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = v[j]; 836ba59ac12SSebastian Grimberg } 837ba59ac12SSebastian Grimberg } 838ba59ac12SSebastian Grimberg // Backwards accumulation of Q 839ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 840ba59ac12SSebastian Grimberg if (tau[i] > 0.0) { 841ba59ac12SSebastian Grimberg v[i] = 1; 842ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 843ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 844ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = 0; 845ba59ac12SSebastian Grimberg } 846ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 847ba59ac12SSebastian Grimberg } 848ba59ac12SSebastian Grimberg } 849ba59ac12SSebastian Grimberg 850ba59ac12SSebastian Grimberg // Reduce sub and super diagonal 851ba59ac12SSebastian Grimberg CeedInt p = 0, q = 0, itr = 0, max_itr = n * n * n * n; 852ba59ac12SSebastian Grimberg CeedScalar tol = CEED_EPSILON; 853ba59ac12SSebastian Grimberg 854ba59ac12SSebastian Grimberg while (itr < max_itr) { 855ba59ac12SSebastian Grimberg // Update p, q, size of reduced portions of diagonal 856ba59ac12SSebastian Grimberg p = 0; 857ba59ac12SSebastian Grimberg q = 0; 858ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 859ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) q += 1; 860ba59ac12SSebastian Grimberg else break; 861ba59ac12SSebastian Grimberg } 862ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - q - 1; i++) { 863ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) p += 1; 864ba59ac12SSebastian Grimberg else break; 865ba59ac12SSebastian Grimberg } 866ba59ac12SSebastian Grimberg if (q == n - 1) break; // Finished reducing 867ba59ac12SSebastian Grimberg 868ba59ac12SSebastian Grimberg // Reduce tridiagonal portion 869ba59ac12SSebastian Grimberg CeedScalar t_nn = mat_T[(n - 1 - q) + n * (n - 1 - q)], t_nnm1 = mat_T[(n - 2 - q) + n * (n - 1 - q)]; 870ba59ac12SSebastian Grimberg CeedScalar d = (mat_T[(n - 2 - q) + n * (n - 2 - q)] - t_nn) / 2; 871ba59ac12SSebastian Grimberg CeedScalar mu = t_nn - t_nnm1 * t_nnm1 / (d + copysign(sqrt(d * d + t_nnm1 * t_nnm1), d)); 872ba59ac12SSebastian Grimberg CeedScalar x = mat_T[p + n * p] - mu; 873ba59ac12SSebastian Grimberg CeedScalar z = mat_T[p + n * (p + 1)]; 8741c66c397SJeremy L Thompson 875ba59ac12SSebastian Grimberg for (CeedInt k = p; k < n - q - 1; k++) { 876ba59ac12SSebastian Grimberg // Compute Givens rotation 877ba59ac12SSebastian Grimberg CeedScalar c = 1, s = 0; 8781c66c397SJeremy L Thompson 879ba59ac12SSebastian Grimberg if (fabs(z) > tol) { 880ba59ac12SSebastian Grimberg if (fabs(z) > fabs(x)) { 8811c66c397SJeremy L Thompson const CeedScalar tau = -x / z; 8821c66c397SJeremy L Thompson 8831c66c397SJeremy L Thompson s = 1 / sqrt(1 + tau * tau); 8841c66c397SJeremy L Thompson c = s * tau; 885ba59ac12SSebastian Grimberg } else { 8861c66c397SJeremy L Thompson const CeedScalar tau = -z / x; 8871c66c397SJeremy L Thompson 8881c66c397SJeremy L Thompson c = 1 / sqrt(1 + tau * tau); 8891c66c397SJeremy L Thompson s = c * tau; 890ba59ac12SSebastian Grimberg } 891ba59ac12SSebastian Grimberg } 892ba59ac12SSebastian Grimberg 893ba59ac12SSebastian Grimberg // Apply Givens rotation to T 894ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 895ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_TRANSPOSE, k, k + 1, n, n); 896ba59ac12SSebastian Grimberg 897ba59ac12SSebastian Grimberg // Apply Givens rotation to Q 898ba59ac12SSebastian Grimberg CeedGivensRotation(mat, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 899ba59ac12SSebastian Grimberg 900ba59ac12SSebastian Grimberg // Update x, z 901ba59ac12SSebastian Grimberg if (k < n - q - 2) { 902ba59ac12SSebastian Grimberg x = mat_T[k + n * (k + 1)]; 903ba59ac12SSebastian Grimberg z = mat_T[k + n * (k + 2)]; 904ba59ac12SSebastian Grimberg } 905ba59ac12SSebastian Grimberg } 906ba59ac12SSebastian Grimberg itr++; 907ba59ac12SSebastian Grimberg } 908ba59ac12SSebastian Grimberg 909ba59ac12SSebastian Grimberg // Save eigenvalues 910ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) lambda[i] = mat_T[i + n * i]; 911ba59ac12SSebastian Grimberg 912ba59ac12SSebastian Grimberg // Check convergence 9136574a04fSJeremy L Thompson CeedCheck(itr < max_itr || q > n, ceed, CEED_ERROR_MINOR, "Symmetric QR failed to converge"); 914ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 915ba59ac12SSebastian Grimberg } 9162c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 917ba59ac12SSebastian Grimberg 918ba59ac12SSebastian Grimberg /** 919ba59ac12SSebastian Grimberg @brief Return Simultaneous Diagonalization of two matrices. 920ba59ac12SSebastian Grimberg 921ca94c3ddSJeremy L Thompson This solves the generalized eigenvalue problem `A x = lambda B x`, where `A` and `B` are symmetric and `B` is positive definite. 922ca94c3ddSJeremy L Thompson We generate the matrix `X` and vector `Lambda` such that `X^T A X = Lambda` and `X^T B X = I`. 923ca94c3ddSJeremy L Thompson This is equivalent to the LAPACK routine 'sygv' with `TYPE = 1`. 924ba59ac12SSebastian Grimberg 925ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 926ba59ac12SSebastian Grimberg @param[in] mat_A Row-major matrix to be factorized with eigenvalues 927ba59ac12SSebastian Grimberg @param[in] mat_B Row-major matrix to be factorized to identity 928ba59ac12SSebastian Grimberg @param[out] mat_X Row-major orthogonal matrix 929ca94c3ddSJeremy L Thompson @param[out] lambda Vector of length `n` of generalized eigenvalues 930ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 931ba59ac12SSebastian Grimberg 932ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 933ba59ac12SSebastian Grimberg 934ba59ac12SSebastian Grimberg @ref Utility 935ba59ac12SSebastian Grimberg **/ 9362c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 9372c2ea1dbSJeremy L Thompson int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A, CeedScalar *mat_B, CeedScalar *mat_X, CeedScalar *lambda, CeedInt n) { 938ba59ac12SSebastian Grimberg CeedScalar *mat_C, *mat_G, *vec_D; 9391c66c397SJeremy L Thompson 940ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_C)); 941ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_G)); 942ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n, &vec_D)); 943ba59ac12SSebastian Grimberg 944ba59ac12SSebastian Grimberg // Compute B = G D G^T 945ba59ac12SSebastian Grimberg memcpy(mat_G, mat_B, n * n * sizeof(mat_B[0])); 946ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_G, vec_D, n)); 947ba59ac12SSebastian Grimberg 948ba59ac12SSebastian Grimberg // Sort eigenvalues 949ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 950ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 951ba59ac12SSebastian Grimberg if (fabs(vec_D[j]) > fabs(vec_D[j + 1])) { 9521c66c397SJeremy L Thompson CeedScalarSwap(vec_D[j], vec_D[j + 1]); 9531c66c397SJeremy L Thompson for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_G[k * n + j], mat_G[k * n + j + 1]); 954ba59ac12SSebastian Grimberg } 955ba59ac12SSebastian Grimberg } 956ba59ac12SSebastian Grimberg } 957ba59ac12SSebastian Grimberg 958ba59ac12SSebastian Grimberg // Compute C = (G D^1/2)^-1 A (G D^1/2)^-T 959ba59ac12SSebastian Grimberg // = D^-1/2 G^T A G D^-1/2 960ba59ac12SSebastian Grimberg // -- D = D^-1/2 961ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) vec_D[i] = 1. / sqrt(vec_D[i]); 962ba59ac12SSebastian Grimberg // -- G = G D^-1/2 963ba59ac12SSebastian Grimberg // -- C = D^-1/2 G^T 964ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 965ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) { 966ba59ac12SSebastian Grimberg mat_G[i * n + j] *= vec_D[j]; 967ba59ac12SSebastian Grimberg mat_C[j * n + i] = mat_G[i * n + j]; 968ba59ac12SSebastian Grimberg } 969ba59ac12SSebastian Grimberg } 970ba59ac12SSebastian Grimberg // -- X = (D^-1/2 G^T) A 971ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_C, (const CeedScalar *)mat_A, mat_X, n, n, n)); 972ba59ac12SSebastian Grimberg // -- C = (D^-1/2 G^T A) (G D^-1/2) 973ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_X, (const CeedScalar *)mat_G, mat_C, n, n, n)); 974ba59ac12SSebastian Grimberg 975ba59ac12SSebastian Grimberg // Compute Q^T C Q = lambda 976ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_C, lambda, n)); 977ba59ac12SSebastian Grimberg 978ba59ac12SSebastian Grimberg // Sort eigenvalues 979ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 980ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 981ba59ac12SSebastian Grimberg if (fabs(lambda[j]) > fabs(lambda[j + 1])) { 9821c66c397SJeremy L Thompson CeedScalarSwap(lambda[j], lambda[j + 1]); 9831c66c397SJeremy L Thompson for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_C[k * n + j], mat_C[k * n + j + 1]); 984ba59ac12SSebastian Grimberg } 985ba59ac12SSebastian Grimberg } 986ba59ac12SSebastian Grimberg } 987ba59ac12SSebastian Grimberg 988ba59ac12SSebastian Grimberg // Set X = (G D^1/2)^-T Q 989ba59ac12SSebastian Grimberg // = G D^-1/2 Q 990ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_G, (const CeedScalar *)mat_C, mat_X, n, n, n)); 991ba59ac12SSebastian Grimberg 992ba59ac12SSebastian Grimberg // Cleanup 993ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_C)); 994ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_G)); 995ba59ac12SSebastian Grimberg CeedCall(CeedFree(&vec_D)); 996ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 997ba59ac12SSebastian Grimberg } 9982c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 999ba59ac12SSebastian Grimberg 10007a982d89SJeremy L. Thompson /// @} 10017a982d89SJeremy L. Thompson 10027a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 10037a982d89SJeremy L. Thompson /// CeedBasis Public API 10047a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 10057a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser 1006d7b241e6Sjeremylt /// @{ 1007d7b241e6Sjeremylt 1008b11c1e72Sjeremylt /** 1009ca94c3ddSJeremy L Thompson @brief Create a tensor-product basis for \f$H^1\f$ discretizations 1010b11c1e72Sjeremylt 1011ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1012ea61e9acSJeremy L Thompson @param[in] dim Topological dimension 1013ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1014ea61e9acSJeremy L Thompson @param[in] P_1d Number of nodes in one dimension 1015ea61e9acSJeremy L Thompson @param[in] Q_1d Number of quadrature points in one dimension 1016ca94c3ddSJeremy L Thompson @param[in] interp_1d Row-major (`Q_1d * P_1d`) matrix expressing the values of nodal basis functions at quadrature points 1017ca94c3ddSJeremy L Thompson @param[in] grad_1d Row-major (`Q_1d * P_1d`) matrix expressing derivatives of nodal basis functions at quadrature points 1018ca94c3ddSJeremy 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]` 1019ca94c3ddSJeremy L Thompson @param[in] q_weight_1d Array of length `Q_1d` holding the quadrature weights on the reference element 1020ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1021b11c1e72Sjeremylt 1022b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1023dfdf5a53Sjeremylt 10247a982d89SJeremy L. Thompson @ref User 1025b11c1e72Sjeremylt **/ 10262b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, 10272b730f8bSJeremy L Thompson const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis *basis) { 10285fe0d4faSjeremylt if (!ceed->BasisCreateTensorH1) { 10295fe0d4faSjeremylt Ceed delegate; 10306574a04fSJeremy L Thompson 10312b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 10321ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateTensorH1"); 10332b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 1034e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10355fe0d4faSjeremylt } 1036e15f9bd0SJeremy L Thompson 1037ca94c3ddSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value"); 1038ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1039ca94c3ddSJeremy L Thompson CeedCheck(P_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1040ca94c3ddSJeremy L Thompson CeedCheck(Q_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1041227444bfSJeremy L Thompson 10422b730f8bSJeremy L Thompson CeedElemTopology topo = dim == 1 ? CEED_TOPOLOGY_LINE : dim == 2 ? CEED_TOPOLOGY_QUAD : CEED_TOPOLOGY_HEX; 1043e15f9bd0SJeremy L Thompson 10442b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1045db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1046d1d35e2fSjeremylt (*basis)->ref_count = 1; 10476402da51SJeremy L Thompson (*basis)->is_tensor_basis = true; 1048d7b241e6Sjeremylt (*basis)->dim = dim; 1049d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 1050d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 1051d1d35e2fSjeremylt (*basis)->P_1d = P_1d; 1052d1d35e2fSjeremylt (*basis)->Q_1d = Q_1d; 1053d1d35e2fSjeremylt (*basis)->P = CeedIntPow(P_1d, dim); 1054d1d35e2fSjeremylt (*basis)->Q = CeedIntPow(Q_1d, dim); 1055c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 10562b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_ref_1d)); 10572b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_weight_1d)); 1058ff3a0f91SJeremy L Thompson if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d * sizeof(q_ref_1d[0])); 10592b730f8bSJeremy L Thompson if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, Q_1d * sizeof(q_weight_1d[0])); 10602b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->interp_1d)); 10612b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->grad_1d)); 10622b730f8bSJeremy L Thompson if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, Q_1d * P_1d * sizeof(interp_1d[0])); 1063ff3a0f91SJeremy L Thompson if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d * P_1d * sizeof(grad_1d[0])); 10642b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, *basis)); 1065e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1066d7b241e6Sjeremylt } 1067d7b241e6Sjeremylt 1068b11c1e72Sjeremylt /** 1069ca94c3ddSJeremy L Thompson @brief Create a tensor-product \f$H^1\f$ Lagrange basis 1070b11c1e72Sjeremylt 1071ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1072ea61e9acSJeremy L Thompson @param[in] dim Topological dimension of element 1073ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1074ea61e9acSJeremy L Thompson @param[in] P Number of Gauss-Lobatto nodes in one dimension. 1075ca94c3ddSJeremy L Thompson The polynomial degree of the resulting `Q_k` element is `k = P - 1`. 1076ea61e9acSJeremy L Thompson @param[in] Q Number of quadrature points in one dimension. 1077ca94c3ddSJeremy L Thompson @param[in] quad_mode Distribution of the `Q` quadrature points (affects order of accuracy for the quadrature) 1078ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1079b11c1e72Sjeremylt 1080b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1081dfdf5a53Sjeremylt 10827a982d89SJeremy L. Thompson @ref User 1083b11c1e72Sjeremylt **/ 10842b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis) { 1085d7b241e6Sjeremylt // Allocate 1086c8c3fa7dSJeremy L Thompson int ierr = CEED_ERROR_SUCCESS; 10872b730f8bSJeremy L Thompson CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, *q_weight_1d; 10884d537eeaSYohann 1089ca94c3ddSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value"); 1090ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1091ca94c3ddSJeremy L Thompson CeedCheck(P > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1092ca94c3ddSJeremy L Thompson CeedCheck(Q > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1093227444bfSJeremy L Thompson 1094e15f9bd0SJeremy L Thompson // Get Nodes and Weights 10952b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &interp_1d)); 10962b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &grad_1d)); 10972b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P, &nodes)); 10982b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_ref_1d)); 10992b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_weight_1d)); 11002b730f8bSJeremy L Thompson if (CeedLobattoQuadrature(P, nodes, NULL) != CEED_ERROR_SUCCESS) goto cleanup; 1101d1d35e2fSjeremylt switch (quad_mode) { 1102d7b241e6Sjeremylt case CEED_GAUSS: 1103d1d35e2fSjeremylt ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d); 1104d7b241e6Sjeremylt break; 1105d7b241e6Sjeremylt case CEED_GAUSS_LOBATTO: 1106d1d35e2fSjeremylt ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d); 1107d7b241e6Sjeremylt break; 1108d7b241e6Sjeremylt } 11092b730f8bSJeremy L Thompson if (ierr != CEED_ERROR_SUCCESS) goto cleanup; 1110e15f9bd0SJeremy L Thompson 1111d7b241e6Sjeremylt // Build B, D matrix 1112d7b241e6Sjeremylt // Fornberg, 1998 1113c8c3fa7dSJeremy L Thompson for (CeedInt i = 0; i < Q; i++) { 1114d7b241e6Sjeremylt c1 = 1.0; 1115d1d35e2fSjeremylt c3 = nodes[0] - q_ref_1d[i]; 1116d1d35e2fSjeremylt interp_1d[i * P + 0] = 1.0; 1117c8c3fa7dSJeremy L Thompson for (CeedInt j = 1; j < P; j++) { 1118d7b241e6Sjeremylt c2 = 1.0; 1119d7b241e6Sjeremylt c4 = c3; 1120d1d35e2fSjeremylt c3 = nodes[j] - q_ref_1d[i]; 1121c8c3fa7dSJeremy L Thompson for (CeedInt k = 0; k < j; k++) { 1122d7b241e6Sjeremylt dx = nodes[j] - nodes[k]; 1123d7b241e6Sjeremylt c2 *= dx; 1124d7b241e6Sjeremylt if (k == j - 1) { 1125d1d35e2fSjeremylt grad_1d[i * P + j] = c1 * (interp_1d[i * P + k] - c4 * grad_1d[i * P + k]) / c2; 1126d1d35e2fSjeremylt interp_1d[i * P + j] = -c1 * c4 * interp_1d[i * P + k] / c2; 1127d7b241e6Sjeremylt } 1128d1d35e2fSjeremylt grad_1d[i * P + k] = (c3 * grad_1d[i * P + k] - interp_1d[i * P + k]) / dx; 1129d1d35e2fSjeremylt interp_1d[i * P + k] = c3 * interp_1d[i * P + k] / dx; 1130d7b241e6Sjeremylt } 1131d7b241e6Sjeremylt c1 = c2; 1132d7b241e6Sjeremylt } 1133d7b241e6Sjeremylt } 11349ac7b42eSJeremy L Thompson // Pass to CeedBasisCreateTensorH1 11352b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 1136e15f9bd0SJeremy L Thompson cleanup: 11372b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_1d)); 11382b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_1d)); 11392b730f8bSJeremy L Thompson CeedCall(CeedFree(&nodes)); 11402b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref_1d)); 11412b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight_1d)); 1142e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1143d7b241e6Sjeremylt } 1144d7b241e6Sjeremylt 1145b11c1e72Sjeremylt /** 1146ca94c3ddSJeremy L Thompson @brief Create a non tensor-product basis for \f$H^1\f$ discretizations 1147a8de75f0Sjeremylt 1148ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1149e00f3be8SJames Wright @param[in] topo Topology of element, e.g. hypercube, simplex, etc 1150ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1151ea61e9acSJeremy L Thompson @param[in] num_nodes Total number of nodes 1152ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1153ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`num_qpts * num_nodes`) matrix expressing the values of nodal basis functions at quadrature points 1154ca94c3ddSJeremy L Thompson @param[in] grad Row-major (`dim * num_qpts * num_nodes`) matrix expressing derivatives of nodal basis functions at quadrature points 1155ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element 1156ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1157ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1158a8de75f0Sjeremylt 1159a8de75f0Sjeremylt @return An error code: 0 - success, otherwise - failure 1160a8de75f0Sjeremylt 11617a982d89SJeremy L. Thompson @ref User 1162a8de75f0Sjeremylt **/ 11632b730f8bSJeremy L Thompson int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 11642b730f8bSJeremy L Thompson const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1165d1d35e2fSjeremylt CeedInt P = num_nodes, Q = num_qpts, dim = 0; 1166a8de75f0Sjeremylt 11675fe0d4faSjeremylt if (!ceed->BasisCreateH1) { 11685fe0d4faSjeremylt Ceed delegate; 11696574a04fSJeremy L Thompson 11702b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 11711ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateH1"); 11722b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis)); 1173e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11745fe0d4faSjeremylt } 11755fe0d4faSjeremylt 1176ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1177ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1178ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1179227444bfSJeremy L Thompson 11802b730f8bSJeremy L Thompson CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1181a8de75f0Sjeremylt 1182db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1183db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1184d1d35e2fSjeremylt (*basis)->ref_count = 1; 11856402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1186a8de75f0Sjeremylt (*basis)->dim = dim; 1187d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 1188d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 1189a8de75f0Sjeremylt (*basis)->P = P; 1190a8de75f0Sjeremylt (*basis)->Q = Q; 1191c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 11922b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * dim, &(*basis)->q_ref_1d)); 11932b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &(*basis)->q_weight_1d)); 1194ff3a0f91SJeremy L Thompson if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1195ff3a0f91SJeremy L Thompson if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 11962b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * P, &(*basis)->interp)); 11972b730f8bSJeremy L Thompson CeedCall(CeedCalloc(dim * Q * P, &(*basis)->grad)); 1198ff3a0f91SJeremy L Thompson if (interp) memcpy((*basis)->interp, interp, Q * P * sizeof(interp[0])); 1199ff3a0f91SJeremy L Thompson if (grad) memcpy((*basis)->grad, grad, dim * Q * P * sizeof(grad[0])); 12002b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, *basis)); 1201e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1202a8de75f0Sjeremylt } 1203a8de75f0Sjeremylt 1204a8de75f0Sjeremylt /** 1205859c15bbSJames Wright @brief Create a non tensor-product basis for \f$H(\mathrm{div})\f$ discretizations 120650c301a5SRezgar Shakeri 1207ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1208ea61e9acSJeremy 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 1209ea61e9acSJeremy L Thompson @param[in] num_comp Number of components (usually 1 for vectors in H(div) bases) 1210ca94c3ddSJeremy L Thompson @param[in] num_nodes Total number of nodes (DoFs per element) 1211ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1212ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points 1213ca94c3ddSJeremy L Thompson @param[in] div Row-major (`num_qpts * num_nodes`) matrix expressing divergence of basis functions at quadrature points 1214ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element 1215ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1216ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 121750c301a5SRezgar Shakeri 121850c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 121950c301a5SRezgar Shakeri 122050c301a5SRezgar Shakeri @ref User 122150c301a5SRezgar Shakeri **/ 12222b730f8bSJeremy L Thompson int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 12232b730f8bSJeremy L Thompson const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 122450c301a5SRezgar Shakeri CeedInt Q = num_qpts, P = num_nodes, dim = 0; 1225c4e3f59bSSebastian Grimberg 122650c301a5SRezgar Shakeri if (!ceed->BasisCreateHdiv) { 122750c301a5SRezgar Shakeri Ceed delegate; 12286574a04fSJeremy L Thompson 12292b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 12306574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv"); 12312b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis)); 123250c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 123350c301a5SRezgar Shakeri } 123450c301a5SRezgar Shakeri 1235ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1236ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1237ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1238227444bfSJeremy L Thompson 1239c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1240c4e3f59bSSebastian Grimberg 1241db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1242db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 124350c301a5SRezgar Shakeri (*basis)->ref_count = 1; 12446402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 124550c301a5SRezgar Shakeri (*basis)->dim = dim; 124650c301a5SRezgar Shakeri (*basis)->topo = topo; 124750c301a5SRezgar Shakeri (*basis)->num_comp = num_comp; 124850c301a5SRezgar Shakeri (*basis)->P = P; 124950c301a5SRezgar Shakeri (*basis)->Q = Q; 1250c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HDIV; 12512b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 12522b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 125350c301a5SRezgar Shakeri if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 125450c301a5SRezgar Shakeri if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 12552b730f8bSJeremy L Thompson CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 12562b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * P, &(*basis)->div)); 125750c301a5SRezgar Shakeri if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 125850c301a5SRezgar Shakeri if (div) memcpy((*basis)->div, div, Q * P * sizeof(div[0])); 12592b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, q_weight, *basis)); 126050c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 126150c301a5SRezgar Shakeri } 126250c301a5SRezgar Shakeri 126350c301a5SRezgar Shakeri /** 12644385fb7fSSebastian Grimberg @brief Create a non tensor-product basis for \f$H(\mathrm{curl})\f$ discretizations 1265c4e3f59bSSebastian Grimberg 1266ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1267c4e3f59bSSebastian Grimberg @param[in] topo Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below 1268ca94c3ddSJeremy L Thompson @param[in] num_comp Number of components (usually 1 for vectors in \f$H(\mathrm{curl})\f$ bases) 1269ca94c3ddSJeremy L Thompson @param[in] num_nodes Total number of nodes (DoFs per element) 1270c4e3f59bSSebastian Grimberg @param[in] num_qpts Total number of quadrature points 1271ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points 1272ca94c3ddSJeremy 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 1273ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element 1274ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1275ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1276c4e3f59bSSebastian Grimberg 1277c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1278c4e3f59bSSebastian Grimberg 1279c4e3f59bSSebastian Grimberg @ref User 1280c4e3f59bSSebastian Grimberg **/ 1281c4e3f59bSSebastian Grimberg int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 1282c4e3f59bSSebastian Grimberg const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1283c4e3f59bSSebastian Grimberg CeedInt Q = num_qpts, P = num_nodes, dim = 0, curl_comp = 0; 1284c4e3f59bSSebastian Grimberg 1285d075f50bSSebastian Grimberg if (!ceed->BasisCreateHcurl) { 1286c4e3f59bSSebastian Grimberg Ceed delegate; 12876574a04fSJeremy L Thompson 1288c4e3f59bSSebastian Grimberg CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 12896574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl"); 1290c4e3f59bSSebastian Grimberg CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis)); 1291c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1292c4e3f59bSSebastian Grimberg } 1293c4e3f59bSSebastian Grimberg 1294ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1295ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1296ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1297c4e3f59bSSebastian Grimberg 1298c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1299c4e3f59bSSebastian Grimberg curl_comp = (dim < 3) ? 1 : dim; 1300c4e3f59bSSebastian Grimberg 1301db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1302db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1303c4e3f59bSSebastian Grimberg (*basis)->ref_count = 1; 13046402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1305c4e3f59bSSebastian Grimberg (*basis)->dim = dim; 1306c4e3f59bSSebastian Grimberg (*basis)->topo = topo; 1307c4e3f59bSSebastian Grimberg (*basis)->num_comp = num_comp; 1308c4e3f59bSSebastian Grimberg (*basis)->P = P; 1309c4e3f59bSSebastian Grimberg (*basis)->Q = Q; 1310c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HCURL; 1311c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 1312c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 1313c4e3f59bSSebastian Grimberg if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1314c4e3f59bSSebastian Grimberg if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 1315c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 1316c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(curl_comp * Q * P, &(*basis)->curl)); 1317c4e3f59bSSebastian Grimberg if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 1318c4e3f59bSSebastian Grimberg if (curl) memcpy((*basis)->curl, curl, curl_comp * Q * P * sizeof(curl[0])); 1319c4e3f59bSSebastian Grimberg CeedCall(ceed->BasisCreateHcurl(topo, dim, P, Q, interp, curl, q_ref, q_weight, *basis)); 1320c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1321c4e3f59bSSebastian Grimberg } 1322c4e3f59bSSebastian Grimberg 1323c4e3f59bSSebastian Grimberg /** 1324ca94c3ddSJeremy L Thompson @brief Create a `CeedBasis` for projection from the nodes of `basis_from` to the nodes of `basis_to`. 1325ba59ac12SSebastian Grimberg 1326ca94c3ddSJeremy L Thompson Only @ref CEED_EVAL_INTERP will be valid for the new basis, `basis_project`. 1327ca94c3ddSJeremy L Thompson For \f$H^1\f$ spaces, @ref CEED_EVAL_GRAD will also be valid. 1328ca94c3ddSJeremy L Thompson The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization. 1329ca94c3ddSJeremy L Thompson The gradient (for the \f$H^1\f$ case) is given by `grad_project = interp_to^+ * grad_from`. 133015ad3917SSebastian Grimberg 133115ad3917SSebastian Grimberg Note: `basis_from` and `basis_to` must have compatible quadrature spaces. 133215ad3917SSebastian Grimberg 13339fd66db6SSebastian Grimberg Note: `basis_project` will have the same number of components as `basis_from`, regardless of the number of components that `basis_to` has. 13349fd66db6SSebastian Grimberg If `basis_from` has 3 components and `basis_to` has 5 components, then `basis_project` will have 3 components. 1335f113e5dcSJeremy L Thompson 1336e104ad11SJames Wright Note: If either `basis_from` or `basis_to` are non-tensor, then `basis_project` will also be non-tensor 1337e104ad11SJames Wright 1338ca94c3ddSJeremy L Thompson @param[in] basis_from `CeedBasis` to prolong from 1339ca94c3ddSJeremy L Thompson @param[in] basis_to `CeedBasis` to prolong to 1340ca94c3ddSJeremy L Thompson @param[out] basis_project Address of the variable where the newly created `CeedBasis` will be stored 1341f113e5dcSJeremy L Thompson 1342f113e5dcSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1343f113e5dcSJeremy L Thompson 1344f113e5dcSJeremy L Thompson @ref User 1345f113e5dcSJeremy L Thompson **/ 13462b730f8bSJeremy L Thompson int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project) { 1347f113e5dcSJeremy L Thompson Ceed ceed; 1348e104ad11SJames Wright bool create_tensor; 13491c66c397SJeremy L Thompson CeedInt dim, num_comp; 1350097cc795SJames Wright CeedScalar *interp_project, *grad_project; 13511c66c397SJeremy L Thompson 13522b730f8bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis_to, &ceed)); 1353f113e5dcSJeremy L Thompson 1354ecc88aebSJeremy L Thompson // Create projection matrix 13552b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateProjectionMatrices(basis_from, basis_to, &interp_project, &grad_project)); 1356f113e5dcSJeremy L Thompson 1357f113e5dcSJeremy L Thompson // Build basis 1358e104ad11SJames Wright { 1359e104ad11SJames Wright bool is_tensor_to, is_tensor_from; 1360e104ad11SJames Wright 1361e104ad11SJames Wright CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to)); 1362e104ad11SJames Wright CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from)); 1363e104ad11SJames Wright create_tensor = is_tensor_from && is_tensor_to; 1364e104ad11SJames Wright } 13652b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_to, &dim)); 13662b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_from, &num_comp)); 1367e104ad11SJames Wright if (create_tensor) { 1368f113e5dcSJeremy L Thompson CeedInt P_1d_to, P_1d_from; 13691c66c397SJeremy L Thompson 13702b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_1d_from)); 13712b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_1d_to)); 1372097cc795SJames Wright CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, interp_project, grad_project, NULL, NULL, basis_project)); 1373f113e5dcSJeremy L Thompson } else { 1374de05fbb2SSebastian Grimberg // Even if basis_to and basis_from are not H1, the resulting basis is H1 for interpolation to work 1375f113e5dcSJeremy L Thompson CeedInt num_nodes_to, num_nodes_from; 13761c66c397SJeremy L Thompson CeedElemTopology topo; 13771c66c397SJeremy L Thompson 1378e00f3be8SJames Wright CeedCall(CeedBasisGetTopology(basis_from, &topo)); 13792b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_from, &num_nodes_from)); 13802b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_to, &num_nodes_to)); 1381097cc795SJames Wright CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, NULL, NULL, basis_project)); 1382f113e5dcSJeremy L Thompson } 1383f113e5dcSJeremy L Thompson 1384f113e5dcSJeremy L Thompson // Cleanup 13852b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_project)); 13862b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_project)); 1387f113e5dcSJeremy L Thompson return CEED_ERROR_SUCCESS; 1388f113e5dcSJeremy L Thompson } 1389f113e5dcSJeremy L Thompson 1390f113e5dcSJeremy L Thompson /** 1391ca94c3ddSJeremy L Thompson @brief Copy the pointer to a `CeedBasis`. 13929560d06aSjeremylt 1393ca94c3ddSJeremy 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`. 1394ca94c3ddSJeremy L Thompson This `CeedBasis` will be destroyed if `*basis_copy` is the only reference to this `CeedBasis`. 1395ea61e9acSJeremy L Thompson 1396ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to copy reference to 1397ea61e9acSJeremy L Thompson @param[in,out] basis_copy Variable to store copied reference 13989560d06aSjeremylt 13999560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 14009560d06aSjeremylt 14019560d06aSjeremylt @ref User 14029560d06aSjeremylt **/ 14039560d06aSjeremylt int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) { 1404356036faSJeremy L Thompson if (basis != CEED_BASIS_NONE) CeedCall(CeedBasisReference(basis)); 14052b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(basis_copy)); 14069560d06aSjeremylt *basis_copy = basis; 14079560d06aSjeremylt return CEED_ERROR_SUCCESS; 14089560d06aSjeremylt } 14099560d06aSjeremylt 14109560d06aSjeremylt /** 1411ca94c3ddSJeremy L Thompson @brief View a `CeedBasis` 14127a982d89SJeremy L. Thompson 1413ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to view 1414ca94c3ddSJeremy L Thompson @param[in] stream Stream to view to, e.g., `stdout` 14157a982d89SJeremy L. Thompson 14167a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 14177a982d89SJeremy L. Thompson 14187a982d89SJeremy L. Thompson @ref User 14197a982d89SJeremy L. Thompson **/ 14207a982d89SJeremy L. Thompson int CeedBasisView(CeedBasis basis, FILE *stream) { 14211203703bSJeremy L Thompson bool is_tensor_basis; 14221203703bSJeremy L Thompson CeedElemTopology topo; 14231203703bSJeremy L Thompson CeedFESpace fe_space; 14241203703bSJeremy L Thompson 14251203703bSJeremy L Thompson // Basis data 14261203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 14271203703bSJeremy L Thompson CeedCall(CeedBasisGetTopology(basis, &topo)); 14281203703bSJeremy L Thompson CeedCall(CeedBasisGetFESpace(basis, &fe_space)); 14292b730f8bSJeremy L Thompson 143050c301a5SRezgar Shakeri // Print FE space and element topology of the basis 1431edf04919SJeremy L Thompson fprintf(stream, "CeedBasis in a %s on a %s element\n", CeedFESpaces[fe_space], CeedElemTopologies[topo]); 14321203703bSJeremy L Thompson if (is_tensor_basis) { 1433edf04919SJeremy L Thompson fprintf(stream, " P: %" CeedInt_FMT "\n Q: %" CeedInt_FMT "\n", basis->P_1d, basis->Q_1d); 143450c301a5SRezgar Shakeri } else { 1435edf04919SJeremy L Thompson fprintf(stream, " P: %" CeedInt_FMT "\n Q: %" CeedInt_FMT "\n", basis->P, basis->Q); 143650c301a5SRezgar Shakeri } 1437edf04919SJeremy L Thompson fprintf(stream, " dimension: %" CeedInt_FMT "\n field components: %" CeedInt_FMT "\n", basis->dim, basis->num_comp); 1438ea61e9acSJeremy L Thompson // Print quadrature data, interpolation/gradient/divergence/curl of the basis 14391203703bSJeremy L Thompson if (is_tensor_basis) { // tensor basis 14401203703bSJeremy L Thompson CeedInt P_1d, Q_1d; 14411203703bSJeremy L Thompson const CeedScalar *q_ref_1d, *q_weight_1d, *interp_1d, *grad_1d; 14421203703bSJeremy L Thompson 14431203703bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 14441203703bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 14451203703bSJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 14461203703bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d)); 14471203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 14481203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 14491203703bSJeremy L Thompson 14501203703bSJeremy L Thompson CeedCall(CeedScalarView("qref1d", "\t% 12.8f", 1, Q_1d, q_ref_1d, stream)); 14511203703bSJeremy L Thompson CeedCall(CeedScalarView("qweight1d", "\t% 12.8f", 1, Q_1d, q_weight_1d, stream)); 14521203703bSJeremy L Thompson CeedCall(CeedScalarView("interp1d", "\t% 12.8f", Q_1d, P_1d, interp_1d, stream)); 14531203703bSJeremy L Thompson CeedCall(CeedScalarView("grad1d", "\t% 12.8f", Q_1d, P_1d, grad_1d, stream)); 145450c301a5SRezgar Shakeri } else { // non-tensor basis 14551203703bSJeremy L Thompson CeedInt P, Q, dim, q_comp; 14561203703bSJeremy L Thompson const CeedScalar *q_ref, *q_weight, *interp, *grad, *div, *curl; 14571203703bSJeremy L Thompson 14581203703bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &P)); 14591203703bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &Q)); 14601203703bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 14611203703bSJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref)); 14621203703bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight)); 14631203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis, &interp)); 14641203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad(basis, &grad)); 14651203703bSJeremy L Thompson CeedCall(CeedBasisGetDiv(basis, &div)); 14661203703bSJeremy L Thompson CeedCall(CeedBasisGetCurl(basis, &curl)); 14671203703bSJeremy L Thompson 14681203703bSJeremy L Thompson CeedCall(CeedScalarView("qref", "\t% 12.8f", 1, Q * dim, q_ref, stream)); 14691203703bSJeremy L Thompson CeedCall(CeedScalarView("qweight", "\t% 12.8f", 1, Q, q_weight, stream)); 1470c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp)); 14711203703bSJeremy L Thompson CeedCall(CeedScalarView("interp", "\t% 12.8f", q_comp * Q, P, interp, stream)); 14721203703bSJeremy L Thompson if (grad) { 1473c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp)); 14741203703bSJeremy L Thompson CeedCall(CeedScalarView("grad", "\t% 12.8f", q_comp * Q, P, grad, stream)); 14757a982d89SJeremy L. Thompson } 14761203703bSJeremy L Thompson if (div) { 1477c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp)); 14781203703bSJeremy L Thompson CeedCall(CeedScalarView("div", "\t% 12.8f", q_comp * Q, P, div, stream)); 1479c4e3f59bSSebastian Grimberg } 14801203703bSJeremy L Thompson if (curl) { 1481c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp)); 14821203703bSJeremy L Thompson CeedCall(CeedScalarView("curl", "\t% 12.8f", q_comp * Q, P, curl, stream)); 148350c301a5SRezgar Shakeri } 148450c301a5SRezgar Shakeri } 1485e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14867a982d89SJeremy L. Thompson } 14877a982d89SJeremy L. Thompson 14887a982d89SJeremy L. Thompson /** 14897a982d89SJeremy L. Thompson @brief Apply basis evaluation from nodes to quadrature points or vice versa 14907a982d89SJeremy L. Thompson 1491ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1492ea61e9acSJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1493ca94c3ddSJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1494ca94c3ddSJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points; 1495ca94c3ddSJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes 1496ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 1497ca94c3ddSJeremy L Thompson @ref CEED_EVAL_INTERP to use interpolated values, 1498ca94c3ddSJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1499ca94c3ddSJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 1500ca94c3ddSJeremy L Thompson @ref CEED_EVAL_CURL to use curl, 1501ca94c3ddSJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1502ca94c3ddSJeremy L Thompson @param[in] u Input `CeedVector` 1503ca94c3ddSJeremy L Thompson @param[out] v Output `CeedVector` 15047a982d89SJeremy L. Thompson 15057a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 15067a982d89SJeremy L. Thompson 15077a982d89SJeremy L. Thompson @ref User 15087a982d89SJeremy L. Thompson **/ 15092b730f8bSJeremy L Thompson int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 1510c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 15111c66c397SJeremy L Thompson CeedSize u_length = 0, v_length; 15121203703bSJeremy L Thompson Ceed ceed; 15131c66c397SJeremy L Thompson 15141203703bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 15152b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 15162b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 1517c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 15182b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 15192b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 15202b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(v, &v_length)); 1521c8c3fa7dSJeremy L Thompson if (u) CeedCall(CeedVectorGetLength(u, &u_length)); 15227a982d89SJeremy L. Thompson 15231203703bSJeremy L Thompson CeedCheck(basis->Apply, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedBasisApply"); 1524e15f9bd0SJeremy L Thompson 1525e15f9bd0SJeremy L Thompson // Check compatibility of topological and geometrical dimensions 15266574a04fSJeremy L Thompson CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0 && u_length % num_qpts == 0) || 15276574a04fSJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0 && v_length % num_qpts == 0), 15281203703bSJeremy L Thompson ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions"); 15297a982d89SJeremy L. Thompson 1530e15f9bd0SJeremy L Thompson // Check vector lengths to prevent out of bounds issues 153199e754f0SJeremy L Thompson bool has_good_dims = true; 1532d1d35e2fSjeremylt switch (eval_mode) { 1533e15f9bd0SJeremy L Thompson case CEED_EVAL_NONE: 15342b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 15352b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 1536c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 1537c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 153899e754f0SJeremy L Thompson has_good_dims = 15396574a04fSJeremy L Thompson ((t_mode == CEED_TRANSPOSE && u_length >= num_elem * num_comp * num_qpts * q_comp && v_length >= num_elem * num_comp * num_nodes) || 15406574a04fSJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && v_length >= num_elem * num_qpts * num_comp * q_comp && u_length >= num_elem * num_comp * num_nodes)); 1541e15f9bd0SJeremy L Thompson break; 1542e15f9bd0SJeremy L Thompson case CEED_EVAL_WEIGHT: 154399e754f0SJeremy L Thompson has_good_dims = v_length >= num_elem * num_qpts; 1544e15f9bd0SJeremy L Thompson break; 1545e15f9bd0SJeremy L Thompson } 154699e754f0SJeremy L Thompson CeedCheck(has_good_dims, ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 1547e15f9bd0SJeremy L Thompson 15482b730f8bSJeremy L Thompson CeedCall(basis->Apply(basis, num_elem, t_mode, eval_mode, u, v)); 1549e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15507a982d89SJeremy L. Thompson } 15517a982d89SJeremy L. Thompson 15527a982d89SJeremy L. Thompson /** 1553c8c3fa7dSJeremy L Thompson @brief Apply basis evaluation from nodes to arbitrary points 1554c8c3fa7dSJeremy L Thompson 1555ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1556fc0f7cc6SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1557fc0f7cc6SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1558faed4840SJeremy L Thompson @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 1559ca94c3ddSJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 1560ca94c3ddSJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes 1561ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 1562ca94c3ddSJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1563ca94c3ddSJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1564ca94c3ddSJeremy L Thompson @param[in] x_ref `CeedVector` holding reference coordinates of each point 1565ca94c3ddSJeremy L Thompson @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 1566ca94c3ddSJeremy L Thompson @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 1567c8c3fa7dSJeremy L Thompson 1568c8c3fa7dSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1569c8c3fa7dSJeremy L Thompson 1570c8c3fa7dSJeremy L Thompson @ref User 1571c8c3fa7dSJeremy L Thompson **/ 1572fc0f7cc6SJeremy L Thompson int CeedBasisApplyAtPoints(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, 1573fc0f7cc6SJeremy L Thompson CeedVector x_ref, CeedVector u, CeedVector v) { 15741203703bSJeremy L Thompson bool is_tensor_basis; 1575fc0f7cc6SJeremy L Thompson CeedInt dim, num_comp, num_q_comp, num_nodes, P_1d = 1, Q_1d = 1, total_num_points = 0; 15761c66c397SJeremy L Thompson CeedSize x_length = 0, u_length = 0, v_length; 15771203703bSJeremy L Thompson Ceed ceed; 1578c8c3fa7dSJeremy L Thompson 15791203703bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 1580c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 1581c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 1582c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 1583c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 1584c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &num_q_comp)); 1585c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 1586c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetLength(v, &v_length)); 1587953190f4SJeremy L Thompson if (x_ref != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(x_ref, &x_length)); 1588953190f4SJeremy L Thompson if (u != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(u, &u_length)); 1589c8c3fa7dSJeremy L Thompson 1590c8c3fa7dSJeremy L Thompson // Check compatibility of topological and geometrical dimensions 1591fc0f7cc6SJeremy L Thompson for (CeedInt i = 0; i < num_elem; i++) total_num_points += num_points[i]; 1592953190f4SJeremy L Thompson CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0) || (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0) || 1593953190f4SJeremy L Thompson (eval_mode == CEED_EVAL_WEIGHT), 15941203703bSJeremy L Thompson ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions and number of points"); 1595c8c3fa7dSJeremy L Thompson 1596c8c3fa7dSJeremy L Thompson // Check compatibility coordinates vector 1597fc0f7cc6SJeremy L Thompson CeedCheck((x_length >= total_num_points * dim) || (eval_mode == CEED_EVAL_WEIGHT), ceed, CEED_ERROR_DIMENSION, 1598c8c3fa7dSJeremy L Thompson "Length of reference coordinate vector incompatible with basis dimension and number of points"); 1599c8c3fa7dSJeremy L Thompson 1600953190f4SJeremy L Thompson // Check CEED_EVAL_WEIGHT only on CEED_NOTRANSPOSE 16011203703bSJeremy L Thompson CeedCheck(eval_mode != CEED_EVAL_WEIGHT || t_mode == CEED_NOTRANSPOSE, ceed, CEED_ERROR_UNSUPPORTED, 1602953190f4SJeremy L Thompson "CEED_EVAL_WEIGHT only supported with CEED_NOTRANSPOSE"); 1603953190f4SJeremy L Thompson 1604c8c3fa7dSJeremy L Thompson // Check vector lengths to prevent out of bounds issues 160599e754f0SJeremy L Thompson bool has_good_dims = true; 1606c8c3fa7dSJeremy L Thompson switch (eval_mode) { 1607c8c3fa7dSJeremy L Thompson case CEED_EVAL_INTERP: 1608fc0f7cc6SJeremy 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)) || 1609fc0f7cc6SJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && (v_length >= total_num_points * num_q_comp || u_length >= num_elem * num_nodes * num_comp))); 1610c8c3fa7dSJeremy L Thompson break; 1611c8c3fa7dSJeremy L Thompson case CEED_EVAL_GRAD: 1612fc0f7cc6SJeremy L Thompson has_good_dims = 1613fc0f7cc6SJeremy L Thompson ((t_mode == CEED_TRANSPOSE && (u_length >= total_num_points * num_q_comp * dim || v_length >= num_elem * num_nodes * num_comp)) || 1614fc0f7cc6SJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && (v_length >= total_num_points * num_q_comp * dim || u_length >= num_elem * num_nodes * num_comp))); 1615edfbf3a6SJeremy L Thompson break; 1616c8c3fa7dSJeremy L Thompson case CEED_EVAL_WEIGHT: 1617fc0f7cc6SJeremy L Thompson has_good_dims = t_mode == CEED_NOTRANSPOSE && (v_length >= total_num_points); 1618953190f4SJeremy L Thompson break; 161999e754f0SJeremy L Thompson // LCOV_EXCL_START 1620953190f4SJeremy L Thompson case CEED_EVAL_NONE: 1621c8c3fa7dSJeremy L Thompson case CEED_EVAL_DIV: 1622c8c3fa7dSJeremy L Thompson case CEED_EVAL_CURL: 16231203703bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points not supported for %s", CeedEvalModes[eval_mode]); 1624c8c3fa7dSJeremy L Thompson // LCOV_EXCL_STOP 1625c8c3fa7dSJeremy L Thompson } 162699e754f0SJeremy L Thompson CeedCheck(has_good_dims, ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 1627c8c3fa7dSJeremy L Thompson 1628c8c3fa7dSJeremy L Thompson // Backend method 1629c8c3fa7dSJeremy L Thompson if (basis->ApplyAtPoints) { 1630fc0f7cc6SJeremy L Thompson CeedCall(basis->ApplyAtPoints(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 1631c8c3fa7dSJeremy L Thompson return CEED_ERROR_SUCCESS; 1632c8c3fa7dSJeremy L Thompson } 1633c8c3fa7dSJeremy L Thompson 1634c8c3fa7dSJeremy L Thompson // Default implementation 16351203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 16361203703bSJeremy L Thompson CeedCheck(is_tensor_basis, ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points only supported for tensor product bases"); 1637fc0f7cc6SJeremy L Thompson CeedCheck(num_elem == 1, ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points only supported for a single element at a time"); 1638953190f4SJeremy L Thompson if (eval_mode == CEED_EVAL_WEIGHT) { 1639953190f4SJeremy L Thompson CeedCall(CeedVectorSetValue(v, 1.0)); 1640953190f4SJeremy L Thompson return CEED_ERROR_SUCCESS; 1641953190f4SJeremy L Thompson } 1642c8c3fa7dSJeremy L Thompson if (!basis->basis_chebyshev) { 1643c8c3fa7dSJeremy L Thompson // Build basis mapping from nodes to Chebyshev coefficients 1644c8c3fa7dSJeremy L Thompson CeedScalar *chebyshev_interp_1d, *chebyshev_grad_1d, *chebyshev_q_weight_1d; 1645b0cc4569SJeremy L Thompson const CeedScalar *q_ref_1d; 1646c8c3fa7dSJeremy L Thompson 164771a83b88SJeremy L Thompson CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 164871a83b88SJeremy L Thompson CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_grad_1d)); 1649c8c3fa7dSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &chebyshev_q_weight_1d)); 1650b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 1651b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d)); 1652c8c3fa7dSJeremy L Thompson 16531203703bSJeremy L Thompson CeedCall(CeedVectorCreate(ceed, num_comp * CeedIntPow(Q_1d, dim), &basis->vec_chebyshev)); 16541203703bSJeremy 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, 1655c8c3fa7dSJeremy L Thompson &basis->basis_chebyshev)); 1656c8c3fa7dSJeremy L Thompson 1657c8c3fa7dSJeremy L Thompson // Cleanup 1658c8c3fa7dSJeremy L Thompson CeedCall(CeedFree(&chebyshev_interp_1d)); 1659c8c3fa7dSJeremy L Thompson CeedCall(CeedFree(&chebyshev_grad_1d)); 1660c8c3fa7dSJeremy L Thompson CeedCall(CeedFree(&chebyshev_q_weight_1d)); 1661c8c3fa7dSJeremy L Thompson } 1662c8c3fa7dSJeremy L Thompson 1663c8c3fa7dSJeremy L Thompson // Create TensorContract object if needed, such as a basis from the GPU backends 1664c8c3fa7dSJeremy L Thompson if (!basis->contract) { 1665c8c3fa7dSJeremy L Thompson Ceed ceed_ref; 1666585a562dSJeremy L Thompson CeedBasis basis_ref = NULL; 1667c8c3fa7dSJeremy L Thompson 1668c8c3fa7dSJeremy L Thompson CeedCall(CeedInit("/cpu/self", &ceed_ref)); 1669c8c3fa7dSJeremy L Thompson // Only need matching tensor contraction dimensions, any type of basis will work 167071a83b88SJeremy L Thompson CeedCall(CeedBasisCreateTensorH1Lagrange(ceed_ref, dim, num_comp, P_1d, Q_1d, CEED_GAUSS, &basis_ref)); 1671585a562dSJeremy L Thompson // Note - clang-tidy doesn't know basis_ref->contract must be valid here 16721203703bSJeremy L Thompson CeedCheck(basis_ref && basis_ref->contract, ceed, CEED_ERROR_UNSUPPORTED, "Reference CPU ceed failed to create a tensor contraction object"); 1673585a562dSJeremy L Thompson CeedCall(CeedTensorContractReferenceCopy(basis_ref->contract, &basis->contract)); 1674c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisDestroy(&basis_ref)); 1675c8c3fa7dSJeremy L Thompson CeedCall(CeedDestroy(&ceed_ref)); 1676c8c3fa7dSJeremy L Thompson } 1677c8c3fa7dSJeremy L Thompson 1678c8c3fa7dSJeremy L Thompson // Basis evaluation 1679c8c3fa7dSJeremy L Thompson switch (t_mode) { 1680c8c3fa7dSJeremy L Thompson case CEED_NOTRANSPOSE: { 1681c8c3fa7dSJeremy L Thompson // Nodes to arbitrary points 1682c8c3fa7dSJeremy L Thompson CeedScalar *v_array; 1683c8c3fa7dSJeremy L Thompson const CeedScalar *chebyshev_coeffs, *x_array_read; 1684c8c3fa7dSJeremy L Thompson 1685c8c3fa7dSJeremy L Thompson // -- Interpolate to Chebyshev coefficients 1686c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, basis->vec_chebyshev)); 1687c8c3fa7dSJeremy L Thompson 1688c8c3fa7dSJeremy L Thompson // -- Evaluate Chebyshev polynomials at arbitrary points 1689c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs)); 1690c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read)); 1691c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(v, CEED_MEM_HOST, &v_array)); 1692edfbf3a6SJeremy L Thompson switch (eval_mode) { 1693edfbf3a6SJeremy L Thompson case CEED_EVAL_INTERP: { 1694c8c3fa7dSJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 1695c8c3fa7dSJeremy L Thompson 1696c8c3fa7dSJeremy L Thompson // ---- Values at point 1697fc0f7cc6SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 1698c8c3fa7dSJeremy L Thompson CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1; 1699c8c3fa7dSJeremy L Thompson 170053ef2869SZach Atkins for (CeedInt d = 0; d < dim; d++) { 17013778dbaaSJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 1702fc0f7cc6SJeremy L Thompson CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 1703c8c3fa7dSJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false, 17044608bdaaSJeremy L Thompson d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2])); 1705c8c3fa7dSJeremy L Thompson pre /= Q_1d; 1706c8c3fa7dSJeremy L Thompson post *= 1; 1707c8c3fa7dSJeremy L Thompson } 1708fc0f7cc6SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) v_array[c * total_num_points + p] = tmp[dim % 2][c]; 1709c8c3fa7dSJeremy L Thompson } 1710edfbf3a6SJeremy L Thompson break; 1711edfbf3a6SJeremy L Thompson } 1712edfbf3a6SJeremy L Thompson case CEED_EVAL_GRAD: { 1713edfbf3a6SJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 1714edfbf3a6SJeremy L Thompson 1715edfbf3a6SJeremy L Thompson // ---- Values at point 1716fc0f7cc6SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 1717edfbf3a6SJeremy L Thompson // Dim**2 contractions, apply grad when pass == dim 171853ef2869SZach Atkins for (CeedInt pass = 0; pass < dim; pass++) { 1719edfbf3a6SJeremy L Thompson CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1; 1720edfbf3a6SJeremy L Thompson 172153ef2869SZach Atkins for (CeedInt d = 0; d < dim; d++) { 17223778dbaaSJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 1723fc0f7cc6SJeremy L Thompson if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 1724fc0f7cc6SJeremy L Thompson else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 1725edfbf3a6SJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false, 17264608bdaaSJeremy L Thompson d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2])); 1727edfbf3a6SJeremy L Thompson pre /= Q_1d; 1728edfbf3a6SJeremy L Thompson post *= 1; 1729edfbf3a6SJeremy L Thompson } 1730fc0f7cc6SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) v_array[(pass * num_comp + c) * total_num_points + p] = tmp[dim % 2][c]; 1731edfbf3a6SJeremy L Thompson } 1732edfbf3a6SJeremy L Thompson } 1733edfbf3a6SJeremy L Thompson break; 1734edfbf3a6SJeremy L Thompson } 1735edfbf3a6SJeremy L Thompson default: 1736953190f4SJeremy L Thompson // Nothing to do, excluded above 1737edfbf3a6SJeremy L Thompson break; 1738c8c3fa7dSJeremy L Thompson } 1739c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(basis->vec_chebyshev, &chebyshev_coeffs)); 1740c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read)); 1741c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorRestoreArray(v, &v_array)); 1742c8c3fa7dSJeremy L Thompson break; 1743c8c3fa7dSJeremy L Thompson } 17442a94f45fSJeremy L Thompson case CEED_TRANSPOSE: { 17453778dbaaSJeremy L Thompson // Note: No switch on e_mode here because only CEED_EVAL_INTERP is supported at this time 17462a94f45fSJeremy L Thompson // Arbitrary points to nodes 17472a94f45fSJeremy L Thompson CeedScalar *chebyshev_coeffs; 17482a94f45fSJeremy L Thompson const CeedScalar *u_array, *x_array_read; 17492a94f45fSJeremy L Thompson 17501c66c397SJeremy L Thompson // -- Transpose of evaluation of Chebyshev polynomials at arbitrary points 17512a94f45fSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs)); 17522a94f45fSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read)); 17532a94f45fSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array)); 1754038a8942SZach Atkins 1755038a8942SZach Atkins switch (eval_mode) { 1756038a8942SZach Atkins case CEED_EVAL_INTERP: { 17572a94f45fSJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 17582a94f45fSJeremy L Thompson 17592a94f45fSJeremy L Thompson // ---- Values at point 1760fc0f7cc6SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 17612a94f45fSJeremy L Thompson CeedInt pre = num_comp * 1, post = 1; 17622a94f45fSJeremy L Thompson 1763fc0f7cc6SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[c * total_num_points + p]; 176453ef2869SZach Atkins for (CeedInt d = 0; d < dim; d++) { 17653778dbaaSJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 1766fc0f7cc6SJeremy L Thompson CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 17674608bdaaSJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, p > 0 && d == (dim - 1), tmp[d % 2], 17684608bdaaSJeremy L Thompson d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2])); 17692a94f45fSJeremy L Thompson pre /= 1; 17702a94f45fSJeremy L Thompson post *= Q_1d; 17712a94f45fSJeremy L Thompson } 17722a94f45fSJeremy L Thompson } 1773038a8942SZach Atkins break; 1774038a8942SZach Atkins } 1775038a8942SZach Atkins case CEED_EVAL_GRAD: { 1776038a8942SZach Atkins CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 1777038a8942SZach Atkins 1778038a8942SZach Atkins // ---- Values at point 1779fc0f7cc6SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 1780038a8942SZach Atkins // Dim**2 contractions, apply grad when pass == dim 1781038a8942SZach Atkins for (CeedInt pass = 0; pass < dim; pass++) { 1782038a8942SZach Atkins CeedInt pre = num_comp * 1, post = 1; 1783038a8942SZach Atkins 1784fc0f7cc6SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[(pass * num_comp + c) * total_num_points + p]; 1785038a8942SZach Atkins for (CeedInt d = 0; d < dim; d++) { 1786038a8942SZach Atkins // ------ Tensor contract with current Chebyshev polynomial values 1787fc0f7cc6SJeremy L Thompson if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 1788fc0f7cc6SJeremy L Thompson else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 17894608bdaaSJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, 17904608bdaaSJeremy L Thompson (p > 0 || (p == 0 && pass > 0)) && d == (dim - 1), tmp[d % 2], 17914608bdaaSJeremy L Thompson d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2])); 1792038a8942SZach Atkins pre /= 1; 1793038a8942SZach Atkins post *= Q_1d; 1794038a8942SZach Atkins } 1795038a8942SZach Atkins } 1796038a8942SZach Atkins } 1797038a8942SZach Atkins break; 1798038a8942SZach Atkins } 1799038a8942SZach Atkins default: 1800038a8942SZach Atkins // Nothing to do, excluded above 1801038a8942SZach Atkins break; 18022a94f45fSJeremy L Thompson } 18032a94f45fSJeremy L Thompson CeedCall(CeedVectorRestoreArray(basis->vec_chebyshev, &chebyshev_coeffs)); 18042a94f45fSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read)); 18052a94f45fSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(u, &u_array)); 18062a94f45fSJeremy L Thompson 18072a94f45fSJeremy L Thompson // -- Interpolate transpose from Chebyshev coefficients 18082a94f45fSJeremy L Thompson CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v)); 18092a94f45fSJeremy L Thompson break; 18102a94f45fSJeremy L Thompson } 1811c8c3fa7dSJeremy L Thompson } 1812c8c3fa7dSJeremy L Thompson return CEED_ERROR_SUCCESS; 1813c8c3fa7dSJeremy L Thompson } 1814c8c3fa7dSJeremy L Thompson 1815c8c3fa7dSJeremy L Thompson /** 18166e536b99SJeremy L Thompson @brief Get the `Ceed` associated with a `CeedBasis` 1817b7c9bbdaSJeremy L Thompson 1818ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1819ca94c3ddSJeremy L Thompson @param[out] ceed Variable to store `Ceed` 1820b7c9bbdaSJeremy L Thompson 1821b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1822b7c9bbdaSJeremy L Thompson 1823b7c9bbdaSJeremy L Thompson @ref Advanced 1824b7c9bbdaSJeremy L Thompson **/ 1825b7c9bbdaSJeremy L Thompson int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) { 18266e536b99SJeremy L Thompson *ceed = CeedBasisReturnCeed(basis); 1827b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1828b7c9bbdaSJeremy L Thompson } 1829b7c9bbdaSJeremy L Thompson 1830b7c9bbdaSJeremy L Thompson /** 18316e536b99SJeremy L Thompson @brief Return the `Ceed` associated with a `CeedBasis` 18326e536b99SJeremy L Thompson 18336e536b99SJeremy L Thompson @param[in] basis `CeedBasis` 18346e536b99SJeremy L Thompson 18356e536b99SJeremy L Thompson @return `Ceed` associated with the `basis` 18366e536b99SJeremy L Thompson 18376e536b99SJeremy L Thompson @ref Advanced 18386e536b99SJeremy L Thompson **/ 18396e536b99SJeremy L Thompson Ceed CeedBasisReturnCeed(CeedBasis basis) { return basis->ceed; } 18406e536b99SJeremy L Thompson 18416e536b99SJeremy L Thompson /** 1842ca94c3ddSJeremy L Thompson @brief Get dimension for given `CeedBasis` 18439d007619Sjeremylt 1844ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 18459d007619Sjeremylt @param[out] dim Variable to store dimension of basis 18469d007619Sjeremylt 18479d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18489d007619Sjeremylt 1849b7c9bbdaSJeremy L Thompson @ref Advanced 18509d007619Sjeremylt **/ 18519d007619Sjeremylt int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) { 18529d007619Sjeremylt *dim = basis->dim; 1853e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18549d007619Sjeremylt } 18559d007619Sjeremylt 18569d007619Sjeremylt /** 1857ca94c3ddSJeremy L Thompson @brief Get topology for given `CeedBasis` 1858d99fa3c5SJeremy L Thompson 1859ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1860d99fa3c5SJeremy L Thompson @param[out] topo Variable to store topology of basis 1861d99fa3c5SJeremy L Thompson 1862d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1863d99fa3c5SJeremy L Thompson 1864b7c9bbdaSJeremy L Thompson @ref Advanced 1865d99fa3c5SJeremy L Thompson **/ 1866d99fa3c5SJeremy L Thompson int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) { 1867d99fa3c5SJeremy L Thompson *topo = basis->topo; 1868e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1869d99fa3c5SJeremy L Thompson } 1870d99fa3c5SJeremy L Thompson 1871d99fa3c5SJeremy L Thompson /** 1872ca94c3ddSJeremy L Thompson @brief Get number of components for given `CeedBasis` 18739d007619Sjeremylt 1874ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1875ca94c3ddSJeremy L Thompson @param[out] num_comp Variable to store number of components 18769d007619Sjeremylt 18779d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18789d007619Sjeremylt 1879b7c9bbdaSJeremy L Thompson @ref Advanced 18809d007619Sjeremylt **/ 1881d1d35e2fSjeremylt int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) { 1882d1d35e2fSjeremylt *num_comp = basis->num_comp; 1883e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18849d007619Sjeremylt } 18859d007619Sjeremylt 18869d007619Sjeremylt /** 1887ca94c3ddSJeremy L Thompson @brief Get total number of nodes (in `dim` dimensions) of a `CeedBasis` 18889d007619Sjeremylt 1889ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 18909d007619Sjeremylt @param[out] P Variable to store number of nodes 18919d007619Sjeremylt 18929d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18939d007619Sjeremylt 18949d007619Sjeremylt @ref Utility 18959d007619Sjeremylt **/ 18969d007619Sjeremylt int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) { 18979d007619Sjeremylt *P = basis->P; 1898e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18999d007619Sjeremylt } 19009d007619Sjeremylt 19019d007619Sjeremylt /** 1902ca94c3ddSJeremy L Thompson @brief Get total number of nodes (in 1 dimension) of a `CeedBasis` 19039d007619Sjeremylt 1904ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1905d1d35e2fSjeremylt @param[out] P_1d Variable to store number of nodes 19069d007619Sjeremylt 19079d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19089d007619Sjeremylt 1909b7c9bbdaSJeremy L Thompson @ref Advanced 19109d007619Sjeremylt **/ 1911d1d35e2fSjeremylt int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) { 19126e536b99SJeremy L Thompson CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply P_1d for non-tensor CeedBasis"); 1913d1d35e2fSjeremylt *P_1d = basis->P_1d; 1914e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19159d007619Sjeremylt } 19169d007619Sjeremylt 19179d007619Sjeremylt /** 1918ca94c3ddSJeremy L Thompson @brief Get total number of quadrature points (in `dim` dimensions) of a `CeedBasis` 19199d007619Sjeremylt 1920ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 19219d007619Sjeremylt @param[out] Q Variable to store number of quadrature points 19229d007619Sjeremylt 19239d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19249d007619Sjeremylt 19259d007619Sjeremylt @ref Utility 19269d007619Sjeremylt **/ 19279d007619Sjeremylt int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) { 19289d007619Sjeremylt *Q = basis->Q; 1929e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19309d007619Sjeremylt } 19319d007619Sjeremylt 19329d007619Sjeremylt /** 1933ca94c3ddSJeremy L Thompson @brief Get total number of quadrature points (in 1 dimension) of a `CeedBasis` 19349d007619Sjeremylt 1935ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1936d1d35e2fSjeremylt @param[out] Q_1d Variable to store number of quadrature points 19379d007619Sjeremylt 19389d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19399d007619Sjeremylt 1940b7c9bbdaSJeremy L Thompson @ref Advanced 19419d007619Sjeremylt **/ 1942d1d35e2fSjeremylt int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) { 19436e536b99SJeremy L Thompson CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply Q_1d for non-tensor CeedBasis"); 1944d1d35e2fSjeremylt *Q_1d = basis->Q_1d; 1945e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19469d007619Sjeremylt } 19479d007619Sjeremylt 19489d007619Sjeremylt /** 1949ca94c3ddSJeremy L Thompson @brief Get reference coordinates of quadrature points (in `dim` dimensions) of a `CeedBasis` 19509d007619Sjeremylt 1951ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1952d1d35e2fSjeremylt @param[out] q_ref Variable to store reference coordinates of quadrature points 19539d007619Sjeremylt 19549d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19559d007619Sjeremylt 1956b7c9bbdaSJeremy L Thompson @ref Advanced 19579d007619Sjeremylt **/ 1958d1d35e2fSjeremylt int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) { 1959d1d35e2fSjeremylt *q_ref = basis->q_ref_1d; 1960e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19619d007619Sjeremylt } 19629d007619Sjeremylt 19639d007619Sjeremylt /** 1964ca94c3ddSJeremy L Thompson @brief Get quadrature weights of quadrature points (in `dim` dimensions) of a `CeedBasis` 19659d007619Sjeremylt 1966ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1967d1d35e2fSjeremylt @param[out] q_weight Variable to store quadrature weights 19689d007619Sjeremylt 19699d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19709d007619Sjeremylt 1971b7c9bbdaSJeremy L Thompson @ref Advanced 19729d007619Sjeremylt **/ 1973d1d35e2fSjeremylt int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) { 1974d1d35e2fSjeremylt *q_weight = basis->q_weight_1d; 1975e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19769d007619Sjeremylt } 19779d007619Sjeremylt 19789d007619Sjeremylt /** 1979ca94c3ddSJeremy L Thompson @brief Get interpolation matrix of a `CeedBasis` 19809d007619Sjeremylt 1981ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 19829d007619Sjeremylt @param[out] interp Variable to store interpolation matrix 19839d007619Sjeremylt 19849d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19859d007619Sjeremylt 1986b7c9bbdaSJeremy L Thompson @ref Advanced 19879d007619Sjeremylt **/ 19886c58de82SJeremy L Thompson int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) { 19896402da51SJeremy L Thompson if (!basis->interp && basis->is_tensor_basis) { 19909d007619Sjeremylt // Allocate 19912b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->Q * basis->P, &basis->interp)); 19929d007619Sjeremylt 19939d007619Sjeremylt // Initialize 19942b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->Q * basis->P; i++) basis->interp[i] = 1.0; 19959d007619Sjeremylt 19969d007619Sjeremylt // Calculate 19972b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 19982b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 19999d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 2000d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 2001d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 20021c66c397SJeremy L Thompson 2003d1d35e2fSjeremylt basis->interp[qpt * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 20049d007619Sjeremylt } 20059d007619Sjeremylt } 20062b730f8bSJeremy L Thompson } 20072b730f8bSJeremy L Thompson } 20089d007619Sjeremylt *interp = basis->interp; 2009e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20109d007619Sjeremylt } 20119d007619Sjeremylt 20129d007619Sjeremylt /** 2013ca94c3ddSJeremy L Thompson @brief Get 1D interpolation matrix of a tensor product `CeedBasis` 20149d007619Sjeremylt 2015ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2016d1d35e2fSjeremylt @param[out] interp_1d Variable to store interpolation matrix 20179d007619Sjeremylt 20189d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20199d007619Sjeremylt 20209d007619Sjeremylt @ref Backend 20219d007619Sjeremylt **/ 2022d1d35e2fSjeremylt int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) { 20231203703bSJeremy L Thompson bool is_tensor_basis; 20241203703bSJeremy L Thompson 20251203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 20266e536b99SJeremy L Thompson CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis"); 2027d1d35e2fSjeremylt *interp_1d = basis->interp_1d; 2028e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20299d007619Sjeremylt } 20309d007619Sjeremylt 20319d007619Sjeremylt /** 2032ca94c3ddSJeremy L Thompson @brief Get gradient matrix of a `CeedBasis` 20339d007619Sjeremylt 2034ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 20359d007619Sjeremylt @param[out] grad Variable to store gradient matrix 20369d007619Sjeremylt 20379d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20389d007619Sjeremylt 2039b7c9bbdaSJeremy L Thompson @ref Advanced 20409d007619Sjeremylt **/ 20416c58de82SJeremy L Thompson int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) { 20426402da51SJeremy L Thompson if (!basis->grad && basis->is_tensor_basis) { 20439d007619Sjeremylt // Allocate 20442b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->dim * basis->Q * basis->P, &basis->grad)); 20459d007619Sjeremylt 20469d007619Sjeremylt // Initialize 20472b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim * basis->Q * basis->P; i++) basis->grad[i] = 1.0; 20489d007619Sjeremylt 20499d007619Sjeremylt // Calculate 20502b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 20512b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim; i++) { 20522b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 20539d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 2054d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 2055d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 20561c66c397SJeremy L Thompson 20572b730f8bSJeremy L Thompson if (i == d) basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->grad_1d[q * basis->P_1d + p]; 20582b730f8bSJeremy L Thompson else basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 20592b730f8bSJeremy L Thompson } 20602b730f8bSJeremy L Thompson } 20612b730f8bSJeremy L Thompson } 20629d007619Sjeremylt } 20639d007619Sjeremylt } 20649d007619Sjeremylt *grad = basis->grad; 2065e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20669d007619Sjeremylt } 20679d007619Sjeremylt 20689d007619Sjeremylt /** 2069ca94c3ddSJeremy L Thompson @brief Get 1D gradient matrix of a tensor product `CeedBasis` 20709d007619Sjeremylt 2071ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2072d1d35e2fSjeremylt @param[out] grad_1d Variable to store gradient matrix 20739d007619Sjeremylt 20749d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20759d007619Sjeremylt 2076b7c9bbdaSJeremy L Thompson @ref Advanced 20779d007619Sjeremylt **/ 2078d1d35e2fSjeremylt int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) { 20791203703bSJeremy L Thompson bool is_tensor_basis; 20801203703bSJeremy L Thompson 20811203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 20826e536b99SJeremy L Thompson CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis"); 2083d1d35e2fSjeremylt *grad_1d = basis->grad_1d; 2084e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20859d007619Sjeremylt } 20869d007619Sjeremylt 20879d007619Sjeremylt /** 2088ca94c3ddSJeremy L Thompson @brief Get divergence matrix of a `CeedBasis` 208950c301a5SRezgar Shakeri 2090ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 209150c301a5SRezgar Shakeri @param[out] div Variable to store divergence matrix 209250c301a5SRezgar Shakeri 209350c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 209450c301a5SRezgar Shakeri 209550c301a5SRezgar Shakeri @ref Advanced 209650c301a5SRezgar Shakeri **/ 209750c301a5SRezgar Shakeri int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) { 209850c301a5SRezgar Shakeri *div = basis->div; 209950c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 210050c301a5SRezgar Shakeri } 210150c301a5SRezgar Shakeri 210250c301a5SRezgar Shakeri /** 2103ca94c3ddSJeremy L Thompson @brief Get curl matrix of a `CeedBasis` 2104c4e3f59bSSebastian Grimberg 2105ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2106c4e3f59bSSebastian Grimberg @param[out] curl Variable to store curl matrix 2107c4e3f59bSSebastian Grimberg 2108c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 2109c4e3f59bSSebastian Grimberg 2110c4e3f59bSSebastian Grimberg @ref Advanced 2111c4e3f59bSSebastian Grimberg **/ 2112c4e3f59bSSebastian Grimberg int CeedBasisGetCurl(CeedBasis basis, const CeedScalar **curl) { 2113c4e3f59bSSebastian Grimberg *curl = basis->curl; 2114c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 2115c4e3f59bSSebastian Grimberg } 2116c4e3f59bSSebastian Grimberg 2117c4e3f59bSSebastian Grimberg /** 2118ca94c3ddSJeremy L Thompson @brief Destroy a @ref CeedBasis 21197a982d89SJeremy L. Thompson 2120ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` to destroy 21217a982d89SJeremy L. Thompson 21227a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 21237a982d89SJeremy L. Thompson 21247a982d89SJeremy L. Thompson @ref User 21257a982d89SJeremy L. Thompson **/ 21267a982d89SJeremy L. Thompson int CeedBasisDestroy(CeedBasis *basis) { 2127356036faSJeremy L Thompson if (!*basis || *basis == CEED_BASIS_NONE || --(*basis)->ref_count > 0) { 2128ad6481ceSJeremy L Thompson *basis = NULL; 2129ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 2130ad6481ceSJeremy L Thompson } 21312b730f8bSJeremy L Thompson if ((*basis)->Destroy) CeedCall((*basis)->Destroy(*basis)); 21329831d45aSJeremy L Thompson CeedCall(CeedTensorContractDestroy(&(*basis)->contract)); 2133c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_ref_1d)); 2134c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_weight_1d)); 21352b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp)); 21362b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp_1d)); 21372b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad)); 21382b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad_1d)); 2139c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->div)); 2140c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->curl)); 2141c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorDestroy(&(*basis)->vec_chebyshev)); 2142c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisDestroy(&(*basis)->basis_chebyshev)); 21432b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*basis)->ceed)); 21442b730f8bSJeremy L Thompson CeedCall(CeedFree(basis)); 2145e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21467a982d89SJeremy L. Thompson } 21477a982d89SJeremy L. Thompson 21487a982d89SJeremy L. Thompson /** 2149b11c1e72Sjeremylt @brief Construct a Gauss-Legendre quadrature 2150b11c1e72Sjeremylt 2151ca94c3ddSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree `2*Q-1` exactly) 2152ca94c3ddSJeremy L Thompson @param[out] q_ref_1d Array of length `Q` to hold the abscissa on `[-1, 1]` 2153ca94c3ddSJeremy L Thompson @param[out] q_weight_1d Array of length `Q` to hold the weights 2154b11c1e72Sjeremylt 2155b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 2156dfdf5a53Sjeremylt 2157dfdf5a53Sjeremylt @ref Utility 2158b11c1e72Sjeremylt **/ 21592b730f8bSJeremy L Thompson int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2160d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0 * atan(1.0); 21611c66c397SJeremy L Thompson 2162d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 216392ae7e47SJeremy L Thompson for (CeedInt i = 0; i <= Q / 2; i++) { 2164d7b241e6Sjeremylt // Guess 2165d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(2 * i + 1) / ((CeedScalar)(2 * Q))); 2166d7b241e6Sjeremylt // Pn(xi) 2167d7b241e6Sjeremylt P0 = 1.0; 2168d7b241e6Sjeremylt P1 = xi; 2169d7b241e6Sjeremylt P2 = 0.0; 217092ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 2171d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2172d7b241e6Sjeremylt P0 = P1; 2173d7b241e6Sjeremylt P1 = P2; 2174d7b241e6Sjeremylt } 2175d7b241e6Sjeremylt // First Newton Step 2176d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2177d7b241e6Sjeremylt xi = xi - P2 / dP2; 2178d7b241e6Sjeremylt // Newton to convergence 217992ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(P2) > 10 * CEED_EPSILON; k++) { 2180d7b241e6Sjeremylt P0 = 1.0; 2181d7b241e6Sjeremylt P1 = xi; 218292ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 2183d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2184d7b241e6Sjeremylt P0 = P1; 2185d7b241e6Sjeremylt P1 = P2; 2186d7b241e6Sjeremylt } 2187d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2188d7b241e6Sjeremylt xi = xi - P2 / dP2; 2189d7b241e6Sjeremylt } 2190d7b241e6Sjeremylt // Save xi, wi 2191d7b241e6Sjeremylt wi = 2.0 / ((1.0 - xi * xi) * dP2 * dP2); 2192d1d35e2fSjeremylt q_weight_1d[i] = wi; 2193d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 2194d1d35e2fSjeremylt q_ref_1d[i] = -xi; 2195d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 2196d7b241e6Sjeremylt } 2197e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2198d7b241e6Sjeremylt } 2199d7b241e6Sjeremylt 2200b11c1e72Sjeremylt /** 2201b11c1e72Sjeremylt @brief Construct a Gauss-Legendre-Lobatto quadrature 2202b11c1e72Sjeremylt 2203ca94c3ddSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree `2*Q-3` exactly) 2204ca94c3ddSJeremy L Thompson @param[out] q_ref_1d Array of length `Q` to hold the abscissa on `[-1, 1]` 2205ca94c3ddSJeremy L Thompson @param[out] q_weight_1d Array of length `Q` to hold the weights 2206b11c1e72Sjeremylt 2207b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 2208dfdf5a53Sjeremylt 2209dfdf5a53Sjeremylt @ref Utility 2210b11c1e72Sjeremylt **/ 22112b730f8bSJeremy L Thompson int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2212d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0 * atan(1.0); 22131c66c397SJeremy L Thompson 2214d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 2215d7b241e6Sjeremylt // Set endpoints 22166574a04fSJeremy L Thompson CeedCheck(Q > 1, NULL, CEED_ERROR_DIMENSION, "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q); 2217d7b241e6Sjeremylt wi = 2.0 / ((CeedScalar)(Q * (Q - 1))); 2218d1d35e2fSjeremylt if (q_weight_1d) { 2219d1d35e2fSjeremylt q_weight_1d[0] = wi; 2220d1d35e2fSjeremylt q_weight_1d[Q - 1] = wi; 2221d7b241e6Sjeremylt } 2222d1d35e2fSjeremylt q_ref_1d[0] = -1.0; 2223d1d35e2fSjeremylt q_ref_1d[Q - 1] = 1.0; 2224d7b241e6Sjeremylt // Interior 222592ae7e47SJeremy L Thompson for (CeedInt i = 1; i <= (Q - 1) / 2; i++) { 2226d7b241e6Sjeremylt // Guess 2227d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(i) / (CeedScalar)(Q - 1)); 2228d7b241e6Sjeremylt // Pn(xi) 2229d7b241e6Sjeremylt P0 = 1.0; 2230d7b241e6Sjeremylt P1 = xi; 2231d7b241e6Sjeremylt P2 = 0.0; 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 // First Newton step 2238d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2239d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2240d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 2241d7b241e6Sjeremylt // Newton to convergence 224292ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(dP2) > 10 * CEED_EPSILON; k++) { 2243d7b241e6Sjeremylt P0 = 1.0; 2244d7b241e6Sjeremylt P1 = xi; 224592ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 2246d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2247d7b241e6Sjeremylt P0 = P1; 2248d7b241e6Sjeremylt P1 = P2; 2249d7b241e6Sjeremylt } 2250d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2251d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2252d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 2253d7b241e6Sjeremylt } 2254d7b241e6Sjeremylt // Save xi, wi 2255d7b241e6Sjeremylt wi = 2.0 / (((CeedScalar)(Q * (Q - 1))) * P2 * P2); 2256d1d35e2fSjeremylt if (q_weight_1d) { 2257d1d35e2fSjeremylt q_weight_1d[i] = wi; 2258d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 2259d7b241e6Sjeremylt } 2260d1d35e2fSjeremylt q_ref_1d[i] = -xi; 2261d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 2262d7b241e6Sjeremylt } 2263e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2264d7b241e6Sjeremylt } 2265d7b241e6Sjeremylt 2266d7b241e6Sjeremylt /// @} 2267