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." 20823622755SJeremy 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 297*0b31fde2SJeremy L Thompson /** 298*0b31fde2SJeremy L Thompson @brief Check input vector dimensions for CeedBasisApply[Add]AtPoints 299*0b31fde2SJeremy L Thompson 300*0b31fde2SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 301*0b31fde2SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 302*0b31fde2SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 303*0b31fde2SJeremy L Thompson @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 304*0b31fde2SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 305*0b31fde2SJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes 306*0b31fde2SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 307*0b31fde2SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 308*0b31fde2SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 309*0b31fde2SJeremy L Thompson @param[in] x_ref `CeedVector` holding reference coordinates of each point 310*0b31fde2SJeremy L Thompson @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 311*0b31fde2SJeremy L Thompson @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 312*0b31fde2SJeremy L Thompson 313*0b31fde2SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 314*0b31fde2SJeremy L Thompson 315*0b31fde2SJeremy L Thompson @ref Developer 316*0b31fde2SJeremy L Thompson **/ 317*0b31fde2SJeremy L Thompson static int CeedBasisApplyAtPointsCheckDims(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, 318*0b31fde2SJeremy L Thompson CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) { 319*0b31fde2SJeremy L Thompson CeedInt dim, num_comp, num_q_comp, num_nodes, P_1d = 1, Q_1d = 1, total_num_points = 0; 320*0b31fde2SJeremy L Thompson CeedSize x_length = 0, u_length = 0, v_length; 321*0b31fde2SJeremy L Thompson Ceed ceed; 322*0b31fde2SJeremy L Thompson 323*0b31fde2SJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 324*0b31fde2SJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 325*0b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 326*0b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 327*0b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 328*0b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &num_q_comp)); 329*0b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 330*0b31fde2SJeremy L Thompson CeedCall(CeedVectorGetLength(v, &v_length)); 331*0b31fde2SJeremy L Thompson if (x_ref != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(x_ref, &x_length)); 332*0b31fde2SJeremy L Thompson if (u != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(u, &u_length)); 333*0b31fde2SJeremy L Thompson 334*0b31fde2SJeremy L Thompson // Check compatibility of topological and geometrical dimensions 335*0b31fde2SJeremy L Thompson CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0) || (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0) || 336*0b31fde2SJeremy L Thompson (eval_mode == CEED_EVAL_WEIGHT), 337*0b31fde2SJeremy L Thompson ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions and number of points"); 338*0b31fde2SJeremy L Thompson 339*0b31fde2SJeremy L Thompson // Check compatibility coordinates vector 340*0b31fde2SJeremy L Thompson for (CeedInt i = 0; i < num_elem; i++) total_num_points += num_points[i]; 341*0b31fde2SJeremy L Thompson CeedCheck((x_length >= total_num_points * dim) || (eval_mode == CEED_EVAL_WEIGHT), ceed, CEED_ERROR_DIMENSION, 342*0b31fde2SJeremy L Thompson "Length of reference coordinate vector incompatible with basis dimension and number of points." 343*0b31fde2SJeremy L Thompson " Found reference coordinate vector of length %" CeedSize_FMT ", not of length %" CeedSize_FMT ".", 344*0b31fde2SJeremy L Thompson x_length, total_num_points * dim); 345*0b31fde2SJeremy L Thompson 346*0b31fde2SJeremy L Thompson // Check CEED_EVAL_WEIGHT only on CEED_NOTRANSPOSE 347*0b31fde2SJeremy L Thompson CeedCheck(eval_mode != CEED_EVAL_WEIGHT || t_mode == CEED_NOTRANSPOSE, ceed, CEED_ERROR_UNSUPPORTED, 348*0b31fde2SJeremy L Thompson "CEED_EVAL_WEIGHT only supported with CEED_NOTRANSPOSE"); 349*0b31fde2SJeremy L Thompson 350*0b31fde2SJeremy L Thompson // Check vector lengths to prevent out of bounds issues 351*0b31fde2SJeremy L Thompson bool has_good_dims = true; 352*0b31fde2SJeremy L Thompson switch (eval_mode) { 353*0b31fde2SJeremy L Thompson case CEED_EVAL_INTERP: 354*0b31fde2SJeremy 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)) || 355*0b31fde2SJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && (v_length >= total_num_points * num_q_comp || u_length >= num_elem * num_nodes * num_comp))); 356*0b31fde2SJeremy L Thompson break; 357*0b31fde2SJeremy L Thompson case CEED_EVAL_GRAD: 358*0b31fde2SJeremy L Thompson has_good_dims = 359*0b31fde2SJeremy L Thompson ((t_mode == CEED_TRANSPOSE && (u_length >= total_num_points * num_q_comp * dim || v_length >= num_elem * num_nodes * num_comp)) || 360*0b31fde2SJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && (v_length >= total_num_points * num_q_comp * dim || u_length >= num_elem * num_nodes * num_comp))); 361*0b31fde2SJeremy L Thompson break; 362*0b31fde2SJeremy L Thompson case CEED_EVAL_WEIGHT: 363*0b31fde2SJeremy L Thompson has_good_dims = t_mode == CEED_NOTRANSPOSE && (v_length >= total_num_points); 364*0b31fde2SJeremy L Thompson break; 365*0b31fde2SJeremy L Thompson // LCOV_EXCL_START 366*0b31fde2SJeremy L Thompson case CEED_EVAL_NONE: 367*0b31fde2SJeremy L Thompson case CEED_EVAL_DIV: 368*0b31fde2SJeremy L Thompson case CEED_EVAL_CURL: 369*0b31fde2SJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points not supported for %s", CeedEvalModes[eval_mode]); 370*0b31fde2SJeremy L Thompson // LCOV_EXCL_STOP 371*0b31fde2SJeremy L Thompson } 372*0b31fde2SJeremy L Thompson CeedCheck(has_good_dims, ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 373*0b31fde2SJeremy L Thompson return CEED_ERROR_SUCCESS; 374*0b31fde2SJeremy L Thompson } 375*0b31fde2SJeremy L Thompson 376*0b31fde2SJeremy L Thompson /** 377*0b31fde2SJeremy L Thompson @brief Default implimentation to apply basis evaluation from nodes to arbitrary points 378*0b31fde2SJeremy L Thompson 379*0b31fde2SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 380*0b31fde2SJeremy L Thompson @param[in] apply_add Sum result into target vector or overwrite 381*0b31fde2SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 382*0b31fde2SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 383*0b31fde2SJeremy L Thompson @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 384*0b31fde2SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 385*0b31fde2SJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes 386*0b31fde2SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 387*0b31fde2SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 388*0b31fde2SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 389*0b31fde2SJeremy L Thompson @param[in] x_ref `CeedVector` holding reference coordinates of each point 390*0b31fde2SJeremy L Thompson @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 391*0b31fde2SJeremy L Thompson @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 392*0b31fde2SJeremy L Thompson 393*0b31fde2SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 394*0b31fde2SJeremy L Thompson 395*0b31fde2SJeremy L Thompson @ref Developer 396*0b31fde2SJeremy L Thompson **/ 397*0b31fde2SJeremy L Thompson static int CeedBasisApplyAtPoints_Core(CeedBasis basis, bool apply_add, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, 398*0b31fde2SJeremy L Thompson CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) { 399*0b31fde2SJeremy L Thompson CeedInt dim, num_comp, P_1d = 1, Q_1d = 1, total_num_points = num_points[0]; 400*0b31fde2SJeremy L Thompson Ceed ceed; 401*0b31fde2SJeremy L Thompson 402*0b31fde2SJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 403*0b31fde2SJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 404*0b31fde2SJeremy L Thompson // Inserting check because clang-tidy doesn't understand this cannot occur 405*0b31fde2SJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_UNSUPPORTED, "Malformed CeedBasis, dim > 0 is required"); 406*0b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 407*0b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 408*0b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 409*0b31fde2SJeremy L Thompson 410*0b31fde2SJeremy L Thompson // Default implementation 411*0b31fde2SJeremy L Thompson { 412*0b31fde2SJeremy L Thompson bool is_tensor_basis; 413*0b31fde2SJeremy L Thompson 414*0b31fde2SJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 415*0b31fde2SJeremy L Thompson CeedCheck(is_tensor_basis, ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points only supported for tensor product bases"); 416*0b31fde2SJeremy L Thompson } 417*0b31fde2SJeremy L Thompson CeedCheck(num_elem == 1, ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points only supported for a single element at a time"); 418*0b31fde2SJeremy L Thompson if (eval_mode == CEED_EVAL_WEIGHT) { 419*0b31fde2SJeremy L Thompson CeedCall(CeedVectorSetValue(v, 1.0)); 420*0b31fde2SJeremy L Thompson return CEED_ERROR_SUCCESS; 421*0b31fde2SJeremy L Thompson } 422*0b31fde2SJeremy L Thompson if (!basis->basis_chebyshev) { 423*0b31fde2SJeremy L Thompson // Build basis mapping from nodes to Chebyshev coefficients 424*0b31fde2SJeremy L Thompson CeedScalar *chebyshev_interp_1d, *chebyshev_grad_1d, *chebyshev_q_weight_1d; 425*0b31fde2SJeremy L Thompson const CeedScalar *q_ref_1d; 426*0b31fde2SJeremy L Thompson 427*0b31fde2SJeremy L Thompson CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 428*0b31fde2SJeremy L Thompson CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_grad_1d)); 429*0b31fde2SJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &chebyshev_q_weight_1d)); 430*0b31fde2SJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 431*0b31fde2SJeremy L Thompson CeedCall(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d)); 432*0b31fde2SJeremy L Thompson 433*0b31fde2SJeremy L Thompson CeedCall(CeedVectorCreate(ceed, num_comp * CeedIntPow(Q_1d, dim), &basis->vec_chebyshev)); 434*0b31fde2SJeremy 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, 435*0b31fde2SJeremy L Thompson &basis->basis_chebyshev)); 436*0b31fde2SJeremy L Thompson 437*0b31fde2SJeremy L Thompson // Cleanup 438*0b31fde2SJeremy L Thompson CeedCall(CeedFree(&chebyshev_interp_1d)); 439*0b31fde2SJeremy L Thompson CeedCall(CeedFree(&chebyshev_grad_1d)); 440*0b31fde2SJeremy L Thompson CeedCall(CeedFree(&chebyshev_q_weight_1d)); 441*0b31fde2SJeremy L Thompson } 442*0b31fde2SJeremy L Thompson 443*0b31fde2SJeremy L Thompson // Create TensorContract object if needed, such as a basis from the GPU backends 444*0b31fde2SJeremy L Thompson if (!basis->contract) { 445*0b31fde2SJeremy L Thompson Ceed ceed_ref; 446*0b31fde2SJeremy L Thompson CeedBasis basis_ref = NULL; 447*0b31fde2SJeremy L Thompson 448*0b31fde2SJeremy L Thompson CeedCall(CeedInit("/cpu/self", &ceed_ref)); 449*0b31fde2SJeremy L Thompson // Only need matching tensor contraction dimensions, any type of basis will work 450*0b31fde2SJeremy L Thompson CeedCall(CeedBasisCreateTensorH1Lagrange(ceed_ref, dim, num_comp, P_1d, Q_1d, CEED_GAUSS, &basis_ref)); 451*0b31fde2SJeremy L Thompson // Note - clang-tidy doesn't know basis_ref->contract must be valid here 452*0b31fde2SJeremy L Thompson CeedCheck(basis_ref && basis_ref->contract, ceed, CEED_ERROR_UNSUPPORTED, "Reference CPU ceed failed to create a tensor contraction object"); 453*0b31fde2SJeremy L Thompson CeedCall(CeedTensorContractReferenceCopy(basis_ref->contract, &basis->contract)); 454*0b31fde2SJeremy L Thompson CeedCall(CeedBasisDestroy(&basis_ref)); 455*0b31fde2SJeremy L Thompson CeedCall(CeedDestroy(&ceed_ref)); 456*0b31fde2SJeremy L Thompson } 457*0b31fde2SJeremy L Thompson 458*0b31fde2SJeremy L Thompson // Basis evaluation 459*0b31fde2SJeremy L Thompson switch (t_mode) { 460*0b31fde2SJeremy L Thompson case CEED_NOTRANSPOSE: { 461*0b31fde2SJeremy L Thompson // Nodes to arbitrary points 462*0b31fde2SJeremy L Thompson CeedScalar *v_array; 463*0b31fde2SJeremy L Thompson const CeedScalar *chebyshev_coeffs, *x_array_read; 464*0b31fde2SJeremy L Thompson 465*0b31fde2SJeremy L Thompson // -- Interpolate to Chebyshev coefficients 466*0b31fde2SJeremy L Thompson CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, basis->vec_chebyshev)); 467*0b31fde2SJeremy L Thompson 468*0b31fde2SJeremy L Thompson // -- Evaluate Chebyshev polynomials at arbitrary points 469*0b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs)); 470*0b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read)); 471*0b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(v, CEED_MEM_HOST, &v_array)); 472*0b31fde2SJeremy L Thompson switch (eval_mode) { 473*0b31fde2SJeremy L Thompson case CEED_EVAL_INTERP: { 474*0b31fde2SJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 475*0b31fde2SJeremy L Thompson 476*0b31fde2SJeremy L Thompson // ---- Values at point 477*0b31fde2SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 478*0b31fde2SJeremy L Thompson CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1; 479*0b31fde2SJeremy L Thompson 480*0b31fde2SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 481*0b31fde2SJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 482*0b31fde2SJeremy L Thompson CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 483*0b31fde2SJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false, 484*0b31fde2SJeremy L Thompson d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2])); 485*0b31fde2SJeremy L Thompson pre /= Q_1d; 486*0b31fde2SJeremy L Thompson post *= 1; 487*0b31fde2SJeremy L Thompson } 488*0b31fde2SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) v_array[c * total_num_points + p] = tmp[dim % 2][c]; 489*0b31fde2SJeremy L Thompson } 490*0b31fde2SJeremy L Thompson break; 491*0b31fde2SJeremy L Thompson } 492*0b31fde2SJeremy L Thompson case CEED_EVAL_GRAD: { 493*0b31fde2SJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 494*0b31fde2SJeremy L Thompson 495*0b31fde2SJeremy L Thompson // ---- Values at point 496*0b31fde2SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 497*0b31fde2SJeremy L Thompson // Dim**2 contractions, apply grad when pass == dim 498*0b31fde2SJeremy L Thompson for (CeedInt pass = 0; pass < dim; pass++) { 499*0b31fde2SJeremy L Thompson CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1; 500*0b31fde2SJeremy L Thompson 501*0b31fde2SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 502*0b31fde2SJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 503*0b31fde2SJeremy L Thompson if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 504*0b31fde2SJeremy L Thompson else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 505*0b31fde2SJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false, 506*0b31fde2SJeremy L Thompson d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2])); 507*0b31fde2SJeremy L Thompson pre /= Q_1d; 508*0b31fde2SJeremy L Thompson post *= 1; 509*0b31fde2SJeremy L Thompson } 510*0b31fde2SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) v_array[(pass * num_comp + c) * total_num_points + p] = tmp[dim % 2][c]; 511*0b31fde2SJeremy L Thompson } 512*0b31fde2SJeremy L Thompson } 513*0b31fde2SJeremy L Thompson break; 514*0b31fde2SJeremy L Thompson } 515*0b31fde2SJeremy L Thompson default: 516*0b31fde2SJeremy L Thompson // Nothing to do, excluded above 517*0b31fde2SJeremy L Thompson break; 518*0b31fde2SJeremy L Thompson } 519*0b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(basis->vec_chebyshev, &chebyshev_coeffs)); 520*0b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read)); 521*0b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArray(v, &v_array)); 522*0b31fde2SJeremy L Thompson break; 523*0b31fde2SJeremy L Thompson } 524*0b31fde2SJeremy L Thompson case CEED_TRANSPOSE: { 525*0b31fde2SJeremy L Thompson // Note: No switch on e_mode here because only CEED_EVAL_INTERP is supported at this time 526*0b31fde2SJeremy L Thompson // Arbitrary points to nodes 527*0b31fde2SJeremy L Thompson CeedScalar *chebyshev_coeffs; 528*0b31fde2SJeremy L Thompson const CeedScalar *u_array, *x_array_read; 529*0b31fde2SJeremy L Thompson 530*0b31fde2SJeremy L Thompson // -- Transpose of evaluation of Chebyshev polynomials at arbitrary points 531*0b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs)); 532*0b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read)); 533*0b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array)); 534*0b31fde2SJeremy L Thompson 535*0b31fde2SJeremy L Thompson switch (eval_mode) { 536*0b31fde2SJeremy L Thompson case CEED_EVAL_INTERP: { 537*0b31fde2SJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 538*0b31fde2SJeremy L Thompson 539*0b31fde2SJeremy L Thompson // ---- Values at point 540*0b31fde2SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 541*0b31fde2SJeremy L Thompson CeedInt pre = num_comp * 1, post = 1; 542*0b31fde2SJeremy L Thompson 543*0b31fde2SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[c * total_num_points + p]; 544*0b31fde2SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 545*0b31fde2SJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 546*0b31fde2SJeremy L Thompson CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 547*0b31fde2SJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, p > 0 && d == (dim - 1), tmp[d % 2], 548*0b31fde2SJeremy L Thompson d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2])); 549*0b31fde2SJeremy L Thompson pre /= 1; 550*0b31fde2SJeremy L Thompson post *= Q_1d; 551*0b31fde2SJeremy L Thompson } 552*0b31fde2SJeremy L Thompson } 553*0b31fde2SJeremy L Thompson break; 554*0b31fde2SJeremy L Thompson } 555*0b31fde2SJeremy L Thompson case CEED_EVAL_GRAD: { 556*0b31fde2SJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 557*0b31fde2SJeremy L Thompson 558*0b31fde2SJeremy L Thompson // ---- Values at point 559*0b31fde2SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 560*0b31fde2SJeremy L Thompson // Dim**2 contractions, apply grad when pass == dim 561*0b31fde2SJeremy L Thompson for (CeedInt pass = 0; pass < dim; pass++) { 562*0b31fde2SJeremy L Thompson CeedInt pre = num_comp * 1, post = 1; 563*0b31fde2SJeremy L Thompson 564*0b31fde2SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[(pass * num_comp + c) * total_num_points + p]; 565*0b31fde2SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 566*0b31fde2SJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 567*0b31fde2SJeremy L Thompson if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 568*0b31fde2SJeremy L Thompson else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 569*0b31fde2SJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, 570*0b31fde2SJeremy L Thompson (p > 0 || (p == 0 && pass > 0)) && d == (dim - 1), tmp[d % 2], 571*0b31fde2SJeremy L Thompson d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2])); 572*0b31fde2SJeremy L Thompson pre /= 1; 573*0b31fde2SJeremy L Thompson post *= Q_1d; 574*0b31fde2SJeremy L Thompson } 575*0b31fde2SJeremy L Thompson } 576*0b31fde2SJeremy L Thompson } 577*0b31fde2SJeremy L Thompson break; 578*0b31fde2SJeremy L Thompson } 579*0b31fde2SJeremy L Thompson default: 580*0b31fde2SJeremy L Thompson // Nothing to do, excluded above 581*0b31fde2SJeremy L Thompson break; 582*0b31fde2SJeremy L Thompson } 583*0b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArray(basis->vec_chebyshev, &chebyshev_coeffs)); 584*0b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read)); 585*0b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(u, &u_array)); 586*0b31fde2SJeremy L Thompson 587*0b31fde2SJeremy L Thompson // -- Interpolate transpose from Chebyshev coefficients 588*0b31fde2SJeremy L Thompson if (apply_add) CeedCall(CeedBasisApplyAdd(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v)); 589*0b31fde2SJeremy L Thompson else CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v)); 590*0b31fde2SJeremy L Thompson break; 591*0b31fde2SJeremy L Thompson } 592*0b31fde2SJeremy L Thompson } 593*0b31fde2SJeremy L Thompson return CEED_ERROR_SUCCESS; 594*0b31fde2SJeremy L Thompson } 595*0b31fde2SJeremy L Thompson 5967a982d89SJeremy L. Thompson /// @} 5977a982d89SJeremy L. Thompson 5987a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 5997a982d89SJeremy L. Thompson /// Ceed Backend API 6007a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 6017a982d89SJeremy L. Thompson /// @addtogroup CeedBasisBackend 6027a982d89SJeremy L. Thompson /// @{ 6037a982d89SJeremy L. Thompson 6047a982d89SJeremy L. Thompson /** 605ca94c3ddSJeremy L Thompson @brief Return collocated gradient matrix 6067a982d89SJeremy L. Thompson 607ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 608ca94c3ddSJeremy L Thompson @param[out] collo_grad_1d Row-major (`Q_1d * Q_1d`) matrix expressing derivatives of basis functions at quadrature points 6097a982d89SJeremy L. Thompson 6107a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 6117a982d89SJeremy L. Thompson 6127a982d89SJeremy L. Thompson @ref Backend 6137a982d89SJeremy L. Thompson **/ 614d1d35e2fSjeremylt int CeedBasisGetCollocatedGrad(CeedBasis basis, CeedScalar *collo_grad_1d) { 6157a982d89SJeremy L. Thompson Ceed ceed; 6162247a93fSRezgar Shakeri CeedInt P_1d, Q_1d; 6172247a93fSRezgar Shakeri CeedScalar *interp_1d_pinv; 6181203703bSJeremy L Thompson const CeedScalar *grad_1d, *interp_1d; 6191203703bSJeremy L Thompson 620ea61e9acSJeremy 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. 6212247a93fSRezgar Shakeri CeedCall(CeedBasisGetCeed(basis, &ceed)); 6222247a93fSRezgar Shakeri CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 6232247a93fSRezgar Shakeri CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 6247a982d89SJeremy L. Thompson 6252247a93fSRezgar Shakeri // Compute interp_1d^+, pseudoinverse of interp_1d 6262247a93fSRezgar Shakeri CeedCall(CeedCalloc(P_1d * Q_1d, &interp_1d_pinv)); 6271203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 6281203703bSJeremy L Thompson CeedCall(CeedMatrixPseudoinverse(ceed, interp_1d, Q_1d, P_1d, interp_1d_pinv)); 6291203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 6301203703bSJeremy L Thompson CeedCall(CeedMatrixMatrixMultiply(ceed, grad_1d, (const CeedScalar *)interp_1d_pinv, collo_grad_1d, Q_1d, Q_1d, P_1d)); 6317a982d89SJeremy L. Thompson 6322247a93fSRezgar Shakeri CeedCall(CeedFree(&interp_1d_pinv)); 633e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6347a982d89SJeremy L. Thompson } 6357a982d89SJeremy L. Thompson 6367a982d89SJeremy L. Thompson /** 637b0cc4569SJeremy L Thompson @brief Return 1D interpolation matrix to Chebyshev polynomial coefficients on quadrature space 638b0cc4569SJeremy L Thompson 639b0cc4569SJeremy L Thompson @param[in] basis `CeedBasis` 640b0cc4569SJeremy L Thompson @param[out] chebyshev_interp_1d Row-major (`P_1d * Q_1d`) matrix interpolating from basis nodes to Chebyshev polynomial coefficients 641b0cc4569SJeremy L Thompson 642b0cc4569SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 643b0cc4569SJeremy L Thompson 644b0cc4569SJeremy L Thompson @ref Backend 645b0cc4569SJeremy L Thompson **/ 646b0cc4569SJeremy L Thompson int CeedBasisGetChebyshevInterp1D(CeedBasis basis, CeedScalar *chebyshev_interp_1d) { 647b0cc4569SJeremy L Thompson CeedInt P_1d, Q_1d; 648b0cc4569SJeremy L Thompson CeedScalar *C, *chebyshev_coeffs_1d_inv; 649b0cc4569SJeremy L Thompson const CeedScalar *interp_1d, *q_ref_1d; 650b0cc4569SJeremy L Thompson Ceed ceed; 651b0cc4569SJeremy L Thompson 652b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 653b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 654b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 655b0cc4569SJeremy L Thompson 656b0cc4569SJeremy L Thompson // Build coefficient matrix 657bd83cbc5SJeremy L Thompson // -- Note: Clang-tidy needs this check 658bd83cbc5SJeremy L Thompson CeedCheck((P_1d > 0) && (Q_1d > 0), ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis dimensions are malformed"); 659b0cc4569SJeremy L Thompson CeedCall(CeedCalloc(Q_1d * Q_1d, &C)); 660b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 661b0cc4569SJeremy L Thompson for (CeedInt i = 0; i < Q_1d; i++) CeedCall(CeedChebyshevPolynomialsAtPoint(q_ref_1d[i], Q_1d, &C[i * Q_1d])); 662b0cc4569SJeremy L Thompson 663b0cc4569SJeremy L Thompson // Compute C^+, pseudoinverse of coefficient matrix 664b0cc4569SJeremy L Thompson CeedCall(CeedCalloc(Q_1d * Q_1d, &chebyshev_coeffs_1d_inv)); 665b0cc4569SJeremy L Thompson CeedCall(CeedMatrixPseudoinverse(ceed, C, Q_1d, Q_1d, chebyshev_coeffs_1d_inv)); 666b0cc4569SJeremy L Thompson 667b0cc4569SJeremy L Thompson // Build mapping from nodes to Chebyshev coefficients 668b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 669b0cc4569SJeremy L Thompson CeedCall(CeedMatrixMatrixMultiply(ceed, chebyshev_coeffs_1d_inv, interp_1d, chebyshev_interp_1d, Q_1d, P_1d, Q_1d)); 670b0cc4569SJeremy L Thompson 671b0cc4569SJeremy L Thompson // Cleanup 672b0cc4569SJeremy L Thompson CeedCall(CeedFree(&C)); 673b0cc4569SJeremy L Thompson CeedCall(CeedFree(&chebyshev_coeffs_1d_inv)); 674b0cc4569SJeremy L Thompson return CEED_ERROR_SUCCESS; 675b0cc4569SJeremy L Thompson } 676b0cc4569SJeremy L Thompson 677b0cc4569SJeremy L Thompson /** 678ca94c3ddSJeremy L Thompson @brief Get tensor status for given `CeedBasis` 6797a982d89SJeremy L. Thompson 680ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 681d1d35e2fSjeremylt @param[out] is_tensor Variable to store tensor status 6827a982d89SJeremy L. Thompson 6837a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 6847a982d89SJeremy L. Thompson 6857a982d89SJeremy L. Thompson @ref Backend 6867a982d89SJeremy L. Thompson **/ 687d1d35e2fSjeremylt int CeedBasisIsTensor(CeedBasis basis, bool *is_tensor) { 6886402da51SJeremy L Thompson *is_tensor = basis->is_tensor_basis; 689e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6907a982d89SJeremy L. Thompson } 6917a982d89SJeremy L. Thompson 6927a982d89SJeremy L. Thompson /** 693ca94c3ddSJeremy L Thompson @brief Get backend data of a `CeedBasis` 6947a982d89SJeremy L. Thompson 695ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 6967a982d89SJeremy L. Thompson @param[out] data Variable to store data 6977a982d89SJeremy L. Thompson 6987a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 6997a982d89SJeremy L. Thompson 7007a982d89SJeremy L. Thompson @ref Backend 7017a982d89SJeremy L. Thompson **/ 702777ff853SJeremy L Thompson int CeedBasisGetData(CeedBasis basis, void *data) { 703777ff853SJeremy L Thompson *(void **)data = basis->data; 704e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7057a982d89SJeremy L. Thompson } 7067a982d89SJeremy L. Thompson 7077a982d89SJeremy L. Thompson /** 708ca94c3ddSJeremy L Thompson @brief Set backend data of a `CeedBasis` 7097a982d89SJeremy L. Thompson 710ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` 711ea61e9acSJeremy L Thompson @param[in] data Data to set 7127a982d89SJeremy L. Thompson 7137a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 7147a982d89SJeremy L. Thompson 7157a982d89SJeremy L. Thompson @ref Backend 7167a982d89SJeremy L. Thompson **/ 717777ff853SJeremy L Thompson int CeedBasisSetData(CeedBasis basis, void *data) { 718777ff853SJeremy L Thompson basis->data = data; 719e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7207a982d89SJeremy L. Thompson } 7217a982d89SJeremy L. Thompson 7227a982d89SJeremy L. Thompson /** 723ca94c3ddSJeremy L Thompson @brief Increment the reference counter for a `CeedBasis` 72434359f16Sjeremylt 725ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` to increment the reference counter 72634359f16Sjeremylt 72734359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 72834359f16Sjeremylt 72934359f16Sjeremylt @ref Backend 73034359f16Sjeremylt **/ 7319560d06aSjeremylt int CeedBasisReference(CeedBasis basis) { 73234359f16Sjeremylt basis->ref_count++; 73334359f16Sjeremylt return CEED_ERROR_SUCCESS; 73434359f16Sjeremylt } 73534359f16Sjeremylt 73634359f16Sjeremylt /** 737ca94c3ddSJeremy L Thompson @brief Get number of Q-vector components for given `CeedBasis` 738c4e3f59bSSebastian Grimberg 739ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 740ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 741ca94c3ddSJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 742ca94c3ddSJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 743ca94c3ddSJeremy L Thompson @ref CEED_EVAL_CURL to use curl 744c4e3f59bSSebastian Grimberg @param[out] q_comp Variable to store number of Q-vector components of basis 745c4e3f59bSSebastian Grimberg 746c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 747c4e3f59bSSebastian Grimberg 748c4e3f59bSSebastian Grimberg @ref Backend 749c4e3f59bSSebastian Grimberg **/ 750c4e3f59bSSebastian Grimberg int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode, CeedInt *q_comp) { 7511203703bSJeremy L Thompson CeedInt dim; 7521203703bSJeremy L Thompson 7531203703bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 754c4e3f59bSSebastian Grimberg switch (eval_mode) { 7551203703bSJeremy L Thompson case CEED_EVAL_INTERP: { 7561203703bSJeremy L Thompson CeedFESpace fe_space; 7571203703bSJeremy L Thompson 7581203703bSJeremy L Thompson CeedCall(CeedBasisGetFESpace(basis, &fe_space)); 7591203703bSJeremy L Thompson *q_comp = (fe_space == CEED_FE_SPACE_H1) ? 1 : dim; 7601203703bSJeremy L Thompson } break; 761c4e3f59bSSebastian Grimberg case CEED_EVAL_GRAD: 7621203703bSJeremy L Thompson *q_comp = dim; 763c4e3f59bSSebastian Grimberg break; 764c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 765c4e3f59bSSebastian Grimberg *q_comp = 1; 766c4e3f59bSSebastian Grimberg break; 767c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 7681203703bSJeremy L Thompson *q_comp = (dim < 3) ? 1 : dim; 769c4e3f59bSSebastian Grimberg break; 770c4e3f59bSSebastian Grimberg case CEED_EVAL_NONE: 771c4e3f59bSSebastian Grimberg case CEED_EVAL_WEIGHT: 772352a5e7cSSebastian Grimberg *q_comp = 1; 773c4e3f59bSSebastian Grimberg break; 774c4e3f59bSSebastian Grimberg } 775c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 776c4e3f59bSSebastian Grimberg } 777c4e3f59bSSebastian Grimberg 778c4e3f59bSSebastian Grimberg /** 779ca94c3ddSJeremy L Thompson @brief Estimate number of FLOPs required to apply `CeedBasis` in `t_mode` and `eval_mode` 7806e15d496SJeremy L Thompson 781ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to estimate FLOPs for 782ea61e9acSJeremy L Thompson @param[in] t_mode Apply basis or transpose 783ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CeedEvalMode 784ea61e9acSJeremy L Thompson @param[out] flops Address of variable to hold FLOPs estimate 7856e15d496SJeremy L Thompson 7866e15d496SJeremy L Thompson @ref Backend 7876e15d496SJeremy L Thompson **/ 7882b730f8bSJeremy L Thompson int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedSize *flops) { 7896e15d496SJeremy L Thompson bool is_tensor; 7906e15d496SJeremy L Thompson 7912b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor)); 7926e15d496SJeremy L Thompson if (is_tensor) { 7936e15d496SJeremy L Thompson CeedInt dim, num_comp, P_1d, Q_1d; 7941c66c397SJeremy L Thompson 7952b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 7962b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 7972b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 7982b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 7996e15d496SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 8002b730f8bSJeremy L Thompson P_1d = Q_1d; 8012b730f8bSJeremy L Thompson Q_1d = P_1d; 8026e15d496SJeremy L Thompson } 8036e15d496SJeremy L Thompson CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim - 1), post = 1; 8046e15d496SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 8056e15d496SJeremy L Thompson tensor_flops += 2 * pre * P_1d * post * Q_1d; 8066e15d496SJeremy L Thompson pre /= P_1d; 8076e15d496SJeremy L Thompson post *= Q_1d; 8086e15d496SJeremy L Thompson } 8096e15d496SJeremy L Thompson switch (eval_mode) { 8102b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 8112b730f8bSJeremy L Thompson *flops = 0; 8122b730f8bSJeremy L Thompson break; 8132b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 8142b730f8bSJeremy L Thompson *flops = tensor_flops; 8152b730f8bSJeremy L Thompson break; 8162b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 8172b730f8bSJeremy L Thompson *flops = tensor_flops * 2; 8182b730f8bSJeremy L Thompson break; 8196e15d496SJeremy L Thompson case CEED_EVAL_DIV: 8201203703bSJeremy L Thompson case CEED_EVAL_CURL: { 8216574a04fSJeremy L Thompson // LCOV_EXCL_START 8226e536b99SJeremy L Thompson return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported", 8236e536b99SJeremy L Thompson CeedEvalModes[eval_mode]); 8242b730f8bSJeremy L Thompson break; 8256e15d496SJeremy L Thompson // LCOV_EXCL_STOP 8261203703bSJeremy L Thompson } 8272b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 8282b730f8bSJeremy L Thompson *flops = dim * CeedIntPow(Q_1d, dim); 8292b730f8bSJeremy L Thompson break; 8306e15d496SJeremy L Thompson } 8316e15d496SJeremy L Thompson } else { 832c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 8331c66c397SJeremy L Thompson 8342b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 8352b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 836c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 8372b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 8382b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 8396e15d496SJeremy L Thompson switch (eval_mode) { 8402b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 8412b730f8bSJeremy L Thompson *flops = 0; 8422b730f8bSJeremy L Thompson break; 8432b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 8442b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 8452b730f8bSJeremy L Thompson case CEED_EVAL_DIV: 8462b730f8bSJeremy L Thompson case CEED_EVAL_CURL: 847c4e3f59bSSebastian Grimberg *flops = num_nodes * num_qpts * num_comp * q_comp; 8482b730f8bSJeremy L Thompson break; 8492b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 8502b730f8bSJeremy L Thompson *flops = 0; 8512b730f8bSJeremy L Thompson break; 8526e15d496SJeremy L Thompson } 8536e15d496SJeremy L Thompson } 8546e15d496SJeremy L Thompson return CEED_ERROR_SUCCESS; 8556e15d496SJeremy L Thompson } 8566e15d496SJeremy L Thompson 8576e15d496SJeremy L Thompson /** 858ca94c3ddSJeremy L Thompson @brief Get `CeedFESpace` for a `CeedBasis` 859c4e3f59bSSebastian Grimberg 860ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 861ca94c3ddSJeremy L Thompson @param[out] fe_space Variable to store `CeedFESpace` 862c4e3f59bSSebastian Grimberg 863c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 864c4e3f59bSSebastian Grimberg 865c4e3f59bSSebastian Grimberg @ref Backend 866c4e3f59bSSebastian Grimberg **/ 867c4e3f59bSSebastian Grimberg int CeedBasisGetFESpace(CeedBasis basis, CeedFESpace *fe_space) { 868c4e3f59bSSebastian Grimberg *fe_space = basis->fe_space; 869c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 870c4e3f59bSSebastian Grimberg } 871c4e3f59bSSebastian Grimberg 872c4e3f59bSSebastian Grimberg /** 873ca94c3ddSJeremy L Thompson @brief Get dimension for given `CeedElemTopology` 8747a982d89SJeremy L. Thompson 875ca94c3ddSJeremy L Thompson @param[in] topo `CeedElemTopology` 8767a982d89SJeremy L. Thompson @param[out] dim Variable to store dimension of topology 8777a982d89SJeremy L. Thompson 8787a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 8797a982d89SJeremy L. Thompson 8807a982d89SJeremy L. Thompson @ref Backend 8817a982d89SJeremy L. Thompson **/ 8827a982d89SJeremy L. Thompson int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim) { 8837a982d89SJeremy L. Thompson *dim = (CeedInt)topo >> 16; 884e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 8857a982d89SJeremy L. Thompson } 8867a982d89SJeremy L. Thompson 8877a982d89SJeremy L. Thompson /** 888ca94c3ddSJeremy L Thompson @brief Get `CeedTensorContract` of a `CeedBasis` 8897a982d89SJeremy L. Thompson 890ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 891ca94c3ddSJeremy L Thompson @param[out] contract Variable to store `CeedTensorContract` 8927a982d89SJeremy L. Thompson 8937a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 8947a982d89SJeremy L. Thompson 8957a982d89SJeremy L. Thompson @ref Backend 8967a982d89SJeremy L. Thompson **/ 8977a982d89SJeremy L. Thompson int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract) { 8987a982d89SJeremy L. Thompson *contract = basis->contract; 899e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9007a982d89SJeremy L. Thompson } 9017a982d89SJeremy L. Thompson 9027a982d89SJeremy L. Thompson /** 903ca94c3ddSJeremy L Thompson @brief Set `CeedTensorContract` of a `CeedBasis` 9047a982d89SJeremy L. Thompson 905ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` 906ca94c3ddSJeremy L Thompson @param[in] contract `CeedTensorContract` to set 9077a982d89SJeremy L. Thompson 9087a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 9097a982d89SJeremy L. Thompson 9107a982d89SJeremy L. Thompson @ref Backend 9117a982d89SJeremy L. Thompson **/ 91234359f16Sjeremylt int CeedBasisSetTensorContract(CeedBasis basis, CeedTensorContract contract) { 91334359f16Sjeremylt basis->contract = contract; 9142b730f8bSJeremy L Thompson CeedCall(CeedTensorContractReference(contract)); 915e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9167a982d89SJeremy L. Thompson } 9177a982d89SJeremy L. Thompson 9187a982d89SJeremy L. Thompson /** 919ca94c3ddSJeremy L Thompson @brief Return a reference implementation of matrix multiplication \f$C = A B\f$. 920ba59ac12SSebastian Grimberg 921ca94c3ddSJeremy L Thompson Note: This is a reference implementation for CPU `CeedScalar` pointers that is not intended for high performance. 9227a982d89SJeremy L. Thompson 923ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 924ca94c3ddSJeremy L Thompson @param[in] mat_A Row-major matrix `A` 925ca94c3ddSJeremy L Thompson @param[in] mat_B Row-major matrix `B` 926ca94c3ddSJeremy L Thompson @param[out] mat_C Row-major output matrix `C` 927ca94c3ddSJeremy L Thompson @param[in] m Number of rows of `C` 928ca94c3ddSJeremy L Thompson @param[in] n Number of columns of `C` 929ca94c3ddSJeremy L Thompson @param[in] kk Number of columns of `A`/rows of `B` 9307a982d89SJeremy L. Thompson 9317a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 9327a982d89SJeremy L. Thompson 9337a982d89SJeremy L. Thompson @ref Utility 9347a982d89SJeremy L. Thompson **/ 9352b730f8bSJeremy L Thompson int CeedMatrixMatrixMultiply(Ceed ceed, const CeedScalar *mat_A, const CeedScalar *mat_B, CeedScalar *mat_C, CeedInt m, CeedInt n, CeedInt kk) { 9362b730f8bSJeremy L Thompson for (CeedInt i = 0; i < m; i++) { 9377a982d89SJeremy L. Thompson for (CeedInt j = 0; j < n; j++) { 9387a982d89SJeremy L. Thompson CeedScalar sum = 0; 9391c66c397SJeremy L Thompson 9402b730f8bSJeremy L Thompson for (CeedInt k = 0; k < kk; k++) sum += mat_A[k + i * kk] * mat_B[j + k * n]; 941d1d35e2fSjeremylt mat_C[j + i * n] = sum; 9427a982d89SJeremy L. Thompson } 9432b730f8bSJeremy L Thompson } 944e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9457a982d89SJeremy L. Thompson } 9467a982d89SJeremy L. Thompson 947ba59ac12SSebastian Grimberg /** 948ba59ac12SSebastian Grimberg @brief Return QR Factorization of a matrix 949ba59ac12SSebastian Grimberg 950ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 951ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 952ca94c3ddSJeremy L Thompson @param[in,out] tau Vector of length `m` of scaling factors 953ba59ac12SSebastian Grimberg @param[in] m Number of rows 954ba59ac12SSebastian Grimberg @param[in] n Number of columns 955ba59ac12SSebastian Grimberg 956ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 957ba59ac12SSebastian Grimberg 958ba59ac12SSebastian Grimberg @ref Utility 959ba59ac12SSebastian Grimberg **/ 960ba59ac12SSebastian Grimberg int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau, CeedInt m, CeedInt n) { 961ba59ac12SSebastian Grimberg CeedScalar v[m]; 962ba59ac12SSebastian Grimberg 963ba59ac12SSebastian Grimberg // Check matrix shape 9646574a04fSJeremy L Thompson CeedCheck(n <= m, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute QR factorization with n > m"); 965ba59ac12SSebastian Grimberg 966ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 9671c66c397SJeremy L Thompson CeedScalar sigma = 0.0; 9681c66c397SJeremy L Thompson 969ba59ac12SSebastian Grimberg if (i >= m - 1) { // last row of matrix, no reflection needed 970ba59ac12SSebastian Grimberg tau[i] = 0.; 971ba59ac12SSebastian Grimberg break; 972ba59ac12SSebastian Grimberg } 973ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 974ba59ac12SSebastian Grimberg v[i] = mat[i + n * i]; 975ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) { 976ba59ac12SSebastian Grimberg v[j] = mat[i + n * j]; 977ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 978ba59ac12SSebastian Grimberg } 9791c66c397SJeremy L Thompson const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:m] 9801c66c397SJeremy L Thompson const CeedScalar R_ii = -copysign(norm, v[i]); 9811c66c397SJeremy L Thompson 982ba59ac12SSebastian Grimberg v[i] -= R_ii; 983ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 984ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 985ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 986ba59ac12SSebastian Grimberg tau[i] = 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 987ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] /= v[i]; 988ba59ac12SSebastian Grimberg 989ba59ac12SSebastian Grimberg // Apply Householder reflector to lower right panel 990ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[i * n + i + 1], &v[i], tau[i], m - i, n - i - 1, n, 1); 991ba59ac12SSebastian Grimberg // Save v 992ba59ac12SSebastian Grimberg mat[i + n * i] = R_ii; 993ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) mat[i + n * j] = v[j]; 994ba59ac12SSebastian Grimberg } 995ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 996ba59ac12SSebastian Grimberg } 997ba59ac12SSebastian Grimberg 998ba59ac12SSebastian Grimberg /** 999ba59ac12SSebastian Grimberg @brief Apply Householder Q matrix 1000ba59ac12SSebastian Grimberg 1001ca94c3ddSJeremy 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$. 1002ba59ac12SSebastian Grimberg 1003ba59ac12SSebastian Grimberg @param[in,out] mat_A Matrix to apply Householder Q to, in place 1004ba59ac12SSebastian Grimberg @param[in] mat_Q Householder Q matrix 1005ba59ac12SSebastian Grimberg @param[in] tau Householder scaling factors 1006ba59ac12SSebastian Grimberg @param[in] t_mode Transpose mode for application 1007ca94c3ddSJeremy L Thompson @param[in] m Number of rows in `A` 1008ca94c3ddSJeremy L Thompson @param[in] n Number of columns in `A` 1009ca94c3ddSJeremy L Thompson @param[in] k Number of elementary reflectors in Q, `k < m` 1010ca94c3ddSJeremy L Thompson @param[in] row Row stride in `A` 1011ca94c3ddSJeremy L Thompson @param[in] col Col stride in `A` 1012ba59ac12SSebastian Grimberg 1013ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1014ba59ac12SSebastian Grimberg 1015c4e3f59bSSebastian Grimberg @ref Utility 1016ba59ac12SSebastian Grimberg **/ 1017ba59ac12SSebastian Grimberg int CeedHouseholderApplyQ(CeedScalar *mat_A, const CeedScalar *mat_Q, const CeedScalar *tau, CeedTransposeMode t_mode, CeedInt m, CeedInt n, 1018ba59ac12SSebastian Grimberg CeedInt k, CeedInt row, CeedInt col) { 1019ba59ac12SSebastian Grimberg CeedScalar *v; 10201c66c397SJeremy L Thompson 1021ba59ac12SSebastian Grimberg CeedCall(CeedMalloc(m, &v)); 1022ba59ac12SSebastian Grimberg for (CeedInt ii = 0; ii < k; ii++) { 1023ba59ac12SSebastian Grimberg CeedInt i = t_mode == CEED_TRANSPOSE ? ii : k - 1 - ii; 1024ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] = mat_Q[j * k + i]; 1025ba59ac12SSebastian Grimberg // Apply Householder reflector (I - tau v v^T) collo_grad_1d^T 1026ba59ac12SSebastian Grimberg CeedCall(CeedHouseholderReflect(&mat_A[i * row], &v[i], tau[i], m - i, n, row, col)); 1027ba59ac12SSebastian Grimberg } 1028ba59ac12SSebastian Grimberg CeedCall(CeedFree(&v)); 1029ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 1030ba59ac12SSebastian Grimberg } 1031ba59ac12SSebastian Grimberg 1032ba59ac12SSebastian Grimberg /** 10332247a93fSRezgar Shakeri @brief Return pseudoinverse of a matrix 10342247a93fSRezgar Shakeri 10352247a93fSRezgar Shakeri @param[in] ceed Ceed context for error handling 10362247a93fSRezgar Shakeri @param[in] mat Row-major matrix to compute pseudoinverse of 10372247a93fSRezgar Shakeri @param[in] m Number of rows 10382247a93fSRezgar Shakeri @param[in] n Number of columns 10392247a93fSRezgar Shakeri @param[out] mat_pinv Row-major pseudoinverse matrix 10402247a93fSRezgar Shakeri 10412247a93fSRezgar Shakeri @return An error code: 0 - success, otherwise - failure 10422247a93fSRezgar Shakeri 10432247a93fSRezgar Shakeri @ref Utility 10442247a93fSRezgar Shakeri **/ 10451203703bSJeremy L Thompson int CeedMatrixPseudoinverse(Ceed ceed, const CeedScalar *mat, CeedInt m, CeedInt n, CeedScalar *mat_pinv) { 10462247a93fSRezgar Shakeri CeedScalar *tau, *I, *mat_copy; 10472247a93fSRezgar Shakeri 10482247a93fSRezgar Shakeri CeedCall(CeedCalloc(m, &tau)); 10492247a93fSRezgar Shakeri CeedCall(CeedCalloc(m * m, &I)); 10502247a93fSRezgar Shakeri CeedCall(CeedCalloc(m * n, &mat_copy)); 10512247a93fSRezgar Shakeri memcpy(mat_copy, mat, m * n * sizeof mat[0]); 10522247a93fSRezgar Shakeri 10532247a93fSRezgar Shakeri // QR Factorization, mat = Q R 10542247a93fSRezgar Shakeri CeedCall(CeedQRFactorization(ceed, mat_copy, tau, m, n)); 10552247a93fSRezgar Shakeri 10562247a93fSRezgar Shakeri // -- Apply Q^T, I = Q^T * I 10572247a93fSRezgar Shakeri for (CeedInt i = 0; i < m; i++) I[i * m + i] = 1.0; 10582247a93fSRezgar Shakeri CeedCall(CeedHouseholderApplyQ(I, mat_copy, tau, CEED_TRANSPOSE, m, m, n, m, 1)); 10592247a93fSRezgar Shakeri // -- Apply R_inv, mat_pinv = R_inv * Q^T 10602247a93fSRezgar Shakeri for (CeedInt j = 0; j < m; j++) { // Column j 10612247a93fSRezgar Shakeri mat_pinv[j + m * (n - 1)] = I[j + m * (n - 1)] / mat_copy[n * n - 1]; 10622247a93fSRezgar Shakeri for (CeedInt i = n - 2; i >= 0; i--) { // Row i 10632247a93fSRezgar Shakeri mat_pinv[j + m * i] = I[j + m * i]; 10642247a93fSRezgar Shakeri for (CeedInt k = i + 1; k < n; k++) mat_pinv[j + m * i] -= mat_copy[k + n * i] * mat_pinv[j + m * k]; 10652247a93fSRezgar Shakeri mat_pinv[j + m * i] /= mat_copy[i + n * i]; 10662247a93fSRezgar Shakeri } 10672247a93fSRezgar Shakeri } 10682247a93fSRezgar Shakeri 10692247a93fSRezgar Shakeri // Cleanup 10702247a93fSRezgar Shakeri CeedCall(CeedFree(&I)); 10712247a93fSRezgar Shakeri CeedCall(CeedFree(&tau)); 10722247a93fSRezgar Shakeri CeedCall(CeedFree(&mat_copy)); 10732247a93fSRezgar Shakeri return CEED_ERROR_SUCCESS; 10742247a93fSRezgar Shakeri } 10752247a93fSRezgar Shakeri 10762247a93fSRezgar Shakeri /** 1077ba59ac12SSebastian Grimberg @brief Return symmetric Schur decomposition of the symmetric matrix mat via symmetric QR factorization 1078ba59ac12SSebastian Grimberg 1079ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 1080ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 1081ba59ac12SSebastian Grimberg @param[out] lambda Vector of length n of eigenvalues 1082ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 1083ba59ac12SSebastian Grimberg 1084ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1085ba59ac12SSebastian Grimberg 1086ba59ac12SSebastian Grimberg @ref Utility 1087ba59ac12SSebastian Grimberg **/ 10882c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 10892c2ea1dbSJeremy L Thompson int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat, CeedScalar *lambda, CeedInt n) { 1090ba59ac12SSebastian Grimberg // Check bounds for clang-tidy 10916574a04fSJeremy L Thompson CeedCheck(n > 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute symmetric Schur decomposition of scalars"); 1092ba59ac12SSebastian Grimberg 1093ba59ac12SSebastian Grimberg CeedScalar v[n - 1], tau[n - 1], mat_T[n * n]; 1094ba59ac12SSebastian Grimberg 1095ba59ac12SSebastian Grimberg // Copy mat to mat_T and set mat to I 1096ba59ac12SSebastian Grimberg memcpy(mat_T, mat, n * n * sizeof(mat[0])); 1097ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 1098ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) mat[j + n * i] = (i == j) ? 1 : 0; 1099ba59ac12SSebastian Grimberg } 1100ba59ac12SSebastian Grimberg 1101ba59ac12SSebastian Grimberg // Reduce to tridiagonal 1102ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - 1; i++) { 1103ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 1104ba59ac12SSebastian Grimberg CeedScalar sigma = 0.0; 11051c66c397SJeremy L Thompson 1106ba59ac12SSebastian Grimberg v[i] = mat_T[i + n * (i + 1)]; 1107ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 1108ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 1109ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 1110ba59ac12SSebastian Grimberg } 11111c66c397SJeremy L Thompson const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:n-1] 11121c66c397SJeremy L Thompson const CeedScalar R_ii = -copysign(norm, v[i]); 11131c66c397SJeremy L Thompson 1114ba59ac12SSebastian Grimberg v[i] -= R_ii; 1115ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 1116ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 1117ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 1118ba59ac12SSebastian Grimberg tau[i] = i == n - 2 ? 2 : 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 1119ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) v[j] /= v[i]; 1120ba59ac12SSebastian Grimberg 1121ba59ac12SSebastian Grimberg // Update sub and super diagonal 1122ba59ac12SSebastian Grimberg for (CeedInt j = i + 2; j < n; j++) { 1123ba59ac12SSebastian Grimberg mat_T[i + n * j] = 0; 1124ba59ac12SSebastian Grimberg mat_T[j + n * i] = 0; 1125ba59ac12SSebastian Grimberg } 1126ba59ac12SSebastian Grimberg // Apply symmetric Householder reflector to lower right panel 1127ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 1128ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), 1, n); 1129ba59ac12SSebastian Grimberg 1130ba59ac12SSebastian Grimberg // Save v 1131ba59ac12SSebastian Grimberg mat_T[i + n * (i + 1)] = R_ii; 1132ba59ac12SSebastian Grimberg mat_T[(i + 1) + n * i] = R_ii; 1133ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 1134ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = v[j]; 1135ba59ac12SSebastian Grimberg } 1136ba59ac12SSebastian Grimberg } 1137ba59ac12SSebastian Grimberg // Backwards accumulation of Q 1138ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 1139ba59ac12SSebastian Grimberg if (tau[i] > 0.0) { 1140ba59ac12SSebastian Grimberg v[i] = 1; 1141ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 1142ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 1143ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = 0; 1144ba59ac12SSebastian Grimberg } 1145ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 1146ba59ac12SSebastian Grimberg } 1147ba59ac12SSebastian Grimberg } 1148ba59ac12SSebastian Grimberg 1149ba59ac12SSebastian Grimberg // Reduce sub and super diagonal 1150ba59ac12SSebastian Grimberg CeedInt p = 0, q = 0, itr = 0, max_itr = n * n * n * n; 1151ba59ac12SSebastian Grimberg CeedScalar tol = CEED_EPSILON; 1152ba59ac12SSebastian Grimberg 1153ba59ac12SSebastian Grimberg while (itr < max_itr) { 1154ba59ac12SSebastian Grimberg // Update p, q, size of reduced portions of diagonal 1155ba59ac12SSebastian Grimberg p = 0; 1156ba59ac12SSebastian Grimberg q = 0; 1157ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 1158ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) q += 1; 1159ba59ac12SSebastian Grimberg else break; 1160ba59ac12SSebastian Grimberg } 1161ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - q - 1; i++) { 1162ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) p += 1; 1163ba59ac12SSebastian Grimberg else break; 1164ba59ac12SSebastian Grimberg } 1165ba59ac12SSebastian Grimberg if (q == n - 1) break; // Finished reducing 1166ba59ac12SSebastian Grimberg 1167ba59ac12SSebastian Grimberg // Reduce tridiagonal portion 1168ba59ac12SSebastian Grimberg CeedScalar t_nn = mat_T[(n - 1 - q) + n * (n - 1 - q)], t_nnm1 = mat_T[(n - 2 - q) + n * (n - 1 - q)]; 1169ba59ac12SSebastian Grimberg CeedScalar d = (mat_T[(n - 2 - q) + n * (n - 2 - q)] - t_nn) / 2; 1170ba59ac12SSebastian Grimberg CeedScalar mu = t_nn - t_nnm1 * t_nnm1 / (d + copysign(sqrt(d * d + t_nnm1 * t_nnm1), d)); 1171ba59ac12SSebastian Grimberg CeedScalar x = mat_T[p + n * p] - mu; 1172ba59ac12SSebastian Grimberg CeedScalar z = mat_T[p + n * (p + 1)]; 11731c66c397SJeremy L Thompson 1174ba59ac12SSebastian Grimberg for (CeedInt k = p; k < n - q - 1; k++) { 1175ba59ac12SSebastian Grimberg // Compute Givens rotation 1176ba59ac12SSebastian Grimberg CeedScalar c = 1, s = 0; 11771c66c397SJeremy L Thompson 1178ba59ac12SSebastian Grimberg if (fabs(z) > tol) { 1179ba59ac12SSebastian Grimberg if (fabs(z) > fabs(x)) { 11801c66c397SJeremy L Thompson const CeedScalar tau = -x / z; 11811c66c397SJeremy L Thompson 11821c66c397SJeremy L Thompson s = 1 / sqrt(1 + tau * tau); 11831c66c397SJeremy L Thompson c = s * tau; 1184ba59ac12SSebastian Grimberg } else { 11851c66c397SJeremy L Thompson const CeedScalar tau = -z / x; 11861c66c397SJeremy L Thompson 11871c66c397SJeremy L Thompson c = 1 / sqrt(1 + tau * tau); 11881c66c397SJeremy L Thompson s = c * tau; 1189ba59ac12SSebastian Grimberg } 1190ba59ac12SSebastian Grimberg } 1191ba59ac12SSebastian Grimberg 1192ba59ac12SSebastian Grimberg // Apply Givens rotation to T 1193ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 1194ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_TRANSPOSE, k, k + 1, n, n); 1195ba59ac12SSebastian Grimberg 1196ba59ac12SSebastian Grimberg // Apply Givens rotation to Q 1197ba59ac12SSebastian Grimberg CeedGivensRotation(mat, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 1198ba59ac12SSebastian Grimberg 1199ba59ac12SSebastian Grimberg // Update x, z 1200ba59ac12SSebastian Grimberg if (k < n - q - 2) { 1201ba59ac12SSebastian Grimberg x = mat_T[k + n * (k + 1)]; 1202ba59ac12SSebastian Grimberg z = mat_T[k + n * (k + 2)]; 1203ba59ac12SSebastian Grimberg } 1204ba59ac12SSebastian Grimberg } 1205ba59ac12SSebastian Grimberg itr++; 1206ba59ac12SSebastian Grimberg } 1207ba59ac12SSebastian Grimberg 1208ba59ac12SSebastian Grimberg // Save eigenvalues 1209ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) lambda[i] = mat_T[i + n * i]; 1210ba59ac12SSebastian Grimberg 1211ba59ac12SSebastian Grimberg // Check convergence 12126574a04fSJeremy L Thompson CeedCheck(itr < max_itr || q > n, ceed, CEED_ERROR_MINOR, "Symmetric QR failed to converge"); 1213ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 1214ba59ac12SSebastian Grimberg } 12152c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 1216ba59ac12SSebastian Grimberg 1217ba59ac12SSebastian Grimberg /** 1218ba59ac12SSebastian Grimberg @brief Return Simultaneous Diagonalization of two matrices. 1219ba59ac12SSebastian Grimberg 1220ca94c3ddSJeremy L Thompson This solves the generalized eigenvalue problem `A x = lambda B x`, where `A` and `B` are symmetric and `B` is positive definite. 1221ca94c3ddSJeremy L Thompson We generate the matrix `X` and vector `Lambda` such that `X^T A X = Lambda` and `X^T B X = I`. 1222ca94c3ddSJeremy L Thompson This is equivalent to the LAPACK routine 'sygv' with `TYPE = 1`. 1223ba59ac12SSebastian Grimberg 1224ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 1225ba59ac12SSebastian Grimberg @param[in] mat_A Row-major matrix to be factorized with eigenvalues 1226ba59ac12SSebastian Grimberg @param[in] mat_B Row-major matrix to be factorized to identity 1227ba59ac12SSebastian Grimberg @param[out] mat_X Row-major orthogonal matrix 1228ca94c3ddSJeremy L Thompson @param[out] lambda Vector of length `n` of generalized eigenvalues 1229ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 1230ba59ac12SSebastian Grimberg 1231ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1232ba59ac12SSebastian Grimberg 1233ba59ac12SSebastian Grimberg @ref Utility 1234ba59ac12SSebastian Grimberg **/ 12352c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 12362c2ea1dbSJeremy L Thompson int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A, CeedScalar *mat_B, CeedScalar *mat_X, CeedScalar *lambda, CeedInt n) { 1237ba59ac12SSebastian Grimberg CeedScalar *mat_C, *mat_G, *vec_D; 12381c66c397SJeremy L Thompson 1239ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_C)); 1240ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_G)); 1241ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n, &vec_D)); 1242ba59ac12SSebastian Grimberg 1243ba59ac12SSebastian Grimberg // Compute B = G D G^T 1244ba59ac12SSebastian Grimberg memcpy(mat_G, mat_B, n * n * sizeof(mat_B[0])); 1245ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_G, vec_D, n)); 1246ba59ac12SSebastian Grimberg 1247ba59ac12SSebastian Grimberg // Sort eigenvalues 1248ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 1249ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 1250ba59ac12SSebastian Grimberg if (fabs(vec_D[j]) > fabs(vec_D[j + 1])) { 12511c66c397SJeremy L Thompson CeedScalarSwap(vec_D[j], vec_D[j + 1]); 12521c66c397SJeremy L Thompson for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_G[k * n + j], mat_G[k * n + j + 1]); 1253ba59ac12SSebastian Grimberg } 1254ba59ac12SSebastian Grimberg } 1255ba59ac12SSebastian Grimberg } 1256ba59ac12SSebastian Grimberg 1257ba59ac12SSebastian Grimberg // Compute C = (G D^1/2)^-1 A (G D^1/2)^-T 1258ba59ac12SSebastian Grimberg // = D^-1/2 G^T A G D^-1/2 1259ba59ac12SSebastian Grimberg // -- D = D^-1/2 1260ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) vec_D[i] = 1. / sqrt(vec_D[i]); 1261ba59ac12SSebastian Grimberg // -- G = G D^-1/2 1262ba59ac12SSebastian Grimberg // -- C = D^-1/2 G^T 1263ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 1264ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) { 1265ba59ac12SSebastian Grimberg mat_G[i * n + j] *= vec_D[j]; 1266ba59ac12SSebastian Grimberg mat_C[j * n + i] = mat_G[i * n + j]; 1267ba59ac12SSebastian Grimberg } 1268ba59ac12SSebastian Grimberg } 1269ba59ac12SSebastian Grimberg // -- X = (D^-1/2 G^T) A 1270ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_C, (const CeedScalar *)mat_A, mat_X, n, n, n)); 1271ba59ac12SSebastian Grimberg // -- C = (D^-1/2 G^T A) (G D^-1/2) 1272ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_X, (const CeedScalar *)mat_G, mat_C, n, n, n)); 1273ba59ac12SSebastian Grimberg 1274ba59ac12SSebastian Grimberg // Compute Q^T C Q = lambda 1275ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_C, lambda, n)); 1276ba59ac12SSebastian Grimberg 1277ba59ac12SSebastian Grimberg // Sort eigenvalues 1278ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 1279ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 1280ba59ac12SSebastian Grimberg if (fabs(lambda[j]) > fabs(lambda[j + 1])) { 12811c66c397SJeremy L Thompson CeedScalarSwap(lambda[j], lambda[j + 1]); 12821c66c397SJeremy L Thompson for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_C[k * n + j], mat_C[k * n + j + 1]); 1283ba59ac12SSebastian Grimberg } 1284ba59ac12SSebastian Grimberg } 1285ba59ac12SSebastian Grimberg } 1286ba59ac12SSebastian Grimberg 1287ba59ac12SSebastian Grimberg // Set X = (G D^1/2)^-T Q 1288ba59ac12SSebastian Grimberg // = G D^-1/2 Q 1289ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_G, (const CeedScalar *)mat_C, mat_X, n, n, n)); 1290ba59ac12SSebastian Grimberg 1291ba59ac12SSebastian Grimberg // Cleanup 1292ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_C)); 1293ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_G)); 1294ba59ac12SSebastian Grimberg CeedCall(CeedFree(&vec_D)); 1295ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 1296ba59ac12SSebastian Grimberg } 12972c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 1298ba59ac12SSebastian Grimberg 12997a982d89SJeremy L. Thompson /// @} 13007a982d89SJeremy L. Thompson 13017a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 13027a982d89SJeremy L. Thompson /// CeedBasis Public API 13037a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 13047a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser 1305d7b241e6Sjeremylt /// @{ 1306d7b241e6Sjeremylt 1307b11c1e72Sjeremylt /** 1308ca94c3ddSJeremy L Thompson @brief Create a tensor-product basis for \f$H^1\f$ discretizations 1309b11c1e72Sjeremylt 1310ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1311ea61e9acSJeremy L Thompson @param[in] dim Topological dimension 1312ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1313ea61e9acSJeremy L Thompson @param[in] P_1d Number of nodes in one dimension 1314ea61e9acSJeremy L Thompson @param[in] Q_1d Number of quadrature points in one dimension 1315ca94c3ddSJeremy L Thompson @param[in] interp_1d Row-major (`Q_1d * P_1d`) matrix expressing the values of nodal basis functions at quadrature points 1316ca94c3ddSJeremy L Thompson @param[in] grad_1d Row-major (`Q_1d * P_1d`) matrix expressing derivatives of nodal basis functions at quadrature points 1317ca94c3ddSJeremy 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]` 1318ca94c3ddSJeremy L Thompson @param[in] q_weight_1d Array of length `Q_1d` holding the quadrature weights on the reference element 1319ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1320b11c1e72Sjeremylt 1321b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1322dfdf5a53Sjeremylt 13237a982d89SJeremy L. Thompson @ref User 1324b11c1e72Sjeremylt **/ 13252b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, 13262b730f8bSJeremy L Thompson const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis *basis) { 13275fe0d4faSjeremylt if (!ceed->BasisCreateTensorH1) { 13285fe0d4faSjeremylt Ceed delegate; 13296574a04fSJeremy L Thompson 13302b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 13311ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateTensorH1"); 13322b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 1333e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 13345fe0d4faSjeremylt } 1335e15f9bd0SJeremy L Thompson 1336ca94c3ddSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value"); 1337ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1338ca94c3ddSJeremy L Thompson CeedCheck(P_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1339ca94c3ddSJeremy L Thompson CeedCheck(Q_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1340227444bfSJeremy L Thompson 13412b730f8bSJeremy L Thompson CeedElemTopology topo = dim == 1 ? CEED_TOPOLOGY_LINE : dim == 2 ? CEED_TOPOLOGY_QUAD : CEED_TOPOLOGY_HEX; 1342e15f9bd0SJeremy L Thompson 13432b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1344db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1345d1d35e2fSjeremylt (*basis)->ref_count = 1; 13466402da51SJeremy L Thompson (*basis)->is_tensor_basis = true; 1347d7b241e6Sjeremylt (*basis)->dim = dim; 1348d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 1349d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 1350d1d35e2fSjeremylt (*basis)->P_1d = P_1d; 1351d1d35e2fSjeremylt (*basis)->Q_1d = Q_1d; 1352d1d35e2fSjeremylt (*basis)->P = CeedIntPow(P_1d, dim); 1353d1d35e2fSjeremylt (*basis)->Q = CeedIntPow(Q_1d, dim); 1354c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 13552b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_ref_1d)); 13562b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_weight_1d)); 1357ff3a0f91SJeremy L Thompson if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d * sizeof(q_ref_1d[0])); 13582b730f8bSJeremy L Thompson if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, Q_1d * sizeof(q_weight_1d[0])); 13592b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->interp_1d)); 13602b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->grad_1d)); 13612b730f8bSJeremy L Thompson if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, Q_1d * P_1d * sizeof(interp_1d[0])); 1362ff3a0f91SJeremy L Thompson if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d * P_1d * sizeof(grad_1d[0])); 13632b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, *basis)); 1364e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1365d7b241e6Sjeremylt } 1366d7b241e6Sjeremylt 1367b11c1e72Sjeremylt /** 1368ca94c3ddSJeremy L Thompson @brief Create a tensor-product \f$H^1\f$ Lagrange basis 1369b11c1e72Sjeremylt 1370ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1371ea61e9acSJeremy L Thompson @param[in] dim Topological dimension of element 1372ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1373ea61e9acSJeremy L Thompson @param[in] P Number of Gauss-Lobatto nodes in one dimension. 1374ca94c3ddSJeremy L Thompson The polynomial degree of the resulting `Q_k` element is `k = P - 1`. 1375ea61e9acSJeremy L Thompson @param[in] Q Number of quadrature points in one dimension. 1376ca94c3ddSJeremy L Thompson @param[in] quad_mode Distribution of the `Q` quadrature points (affects order of accuracy for the quadrature) 1377ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1378b11c1e72Sjeremylt 1379b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1380dfdf5a53Sjeremylt 13817a982d89SJeremy L. Thompson @ref User 1382b11c1e72Sjeremylt **/ 13832b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis) { 1384d7b241e6Sjeremylt // Allocate 1385c8c3fa7dSJeremy L Thompson int ierr = CEED_ERROR_SUCCESS; 13862b730f8bSJeremy L Thompson CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, *q_weight_1d; 13874d537eeaSYohann 1388ca94c3ddSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value"); 1389ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1390ca94c3ddSJeremy L Thompson CeedCheck(P > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1391ca94c3ddSJeremy L Thompson CeedCheck(Q > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1392227444bfSJeremy L Thompson 1393e15f9bd0SJeremy L Thompson // Get Nodes and Weights 13942b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &interp_1d)); 13952b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &grad_1d)); 13962b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P, &nodes)); 13972b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_ref_1d)); 13982b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_weight_1d)); 13992b730f8bSJeremy L Thompson if (CeedLobattoQuadrature(P, nodes, NULL) != CEED_ERROR_SUCCESS) goto cleanup; 1400d1d35e2fSjeremylt switch (quad_mode) { 1401d7b241e6Sjeremylt case CEED_GAUSS: 1402d1d35e2fSjeremylt ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d); 1403d7b241e6Sjeremylt break; 1404d7b241e6Sjeremylt case CEED_GAUSS_LOBATTO: 1405d1d35e2fSjeremylt ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d); 1406d7b241e6Sjeremylt break; 1407d7b241e6Sjeremylt } 14082b730f8bSJeremy L Thompson if (ierr != CEED_ERROR_SUCCESS) goto cleanup; 1409e15f9bd0SJeremy L Thompson 1410d7b241e6Sjeremylt // Build B, D matrix 1411d7b241e6Sjeremylt // Fornberg, 1998 1412c8c3fa7dSJeremy L Thompson for (CeedInt i = 0; i < Q; i++) { 1413d7b241e6Sjeremylt c1 = 1.0; 1414d1d35e2fSjeremylt c3 = nodes[0] - q_ref_1d[i]; 1415d1d35e2fSjeremylt interp_1d[i * P + 0] = 1.0; 1416c8c3fa7dSJeremy L Thompson for (CeedInt j = 1; j < P; j++) { 1417d7b241e6Sjeremylt c2 = 1.0; 1418d7b241e6Sjeremylt c4 = c3; 1419d1d35e2fSjeremylt c3 = nodes[j] - q_ref_1d[i]; 1420c8c3fa7dSJeremy L Thompson for (CeedInt k = 0; k < j; k++) { 1421d7b241e6Sjeremylt dx = nodes[j] - nodes[k]; 1422d7b241e6Sjeremylt c2 *= dx; 1423d7b241e6Sjeremylt if (k == j - 1) { 1424d1d35e2fSjeremylt grad_1d[i * P + j] = c1 * (interp_1d[i * P + k] - c4 * grad_1d[i * P + k]) / c2; 1425d1d35e2fSjeremylt interp_1d[i * P + j] = -c1 * c4 * interp_1d[i * P + k] / c2; 1426d7b241e6Sjeremylt } 1427d1d35e2fSjeremylt grad_1d[i * P + k] = (c3 * grad_1d[i * P + k] - interp_1d[i * P + k]) / dx; 1428d1d35e2fSjeremylt interp_1d[i * P + k] = c3 * interp_1d[i * P + k] / dx; 1429d7b241e6Sjeremylt } 1430d7b241e6Sjeremylt c1 = c2; 1431d7b241e6Sjeremylt } 1432d7b241e6Sjeremylt } 14339ac7b42eSJeremy L Thompson // Pass to CeedBasisCreateTensorH1 14342b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 1435e15f9bd0SJeremy L Thompson cleanup: 14362b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_1d)); 14372b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_1d)); 14382b730f8bSJeremy L Thompson CeedCall(CeedFree(&nodes)); 14392b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref_1d)); 14402b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight_1d)); 1441e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1442d7b241e6Sjeremylt } 1443d7b241e6Sjeremylt 1444b11c1e72Sjeremylt /** 1445ca94c3ddSJeremy L Thompson @brief Create a non tensor-product basis for \f$H^1\f$ discretizations 1446a8de75f0Sjeremylt 1447ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1448e00f3be8SJames Wright @param[in] topo Topology of element, e.g. hypercube, simplex, etc 1449ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1450ea61e9acSJeremy L Thompson @param[in] num_nodes Total number of nodes 1451ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1452ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`num_qpts * num_nodes`) matrix expressing the values of nodal basis functions at quadrature points 1453ca94c3ddSJeremy L Thompson @param[in] grad Row-major (`dim * num_qpts * num_nodes`) matrix expressing derivatives of nodal basis functions at quadrature points 1454ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element 1455ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1456ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1457a8de75f0Sjeremylt 1458a8de75f0Sjeremylt @return An error code: 0 - success, otherwise - failure 1459a8de75f0Sjeremylt 14607a982d89SJeremy L. Thompson @ref User 1461a8de75f0Sjeremylt **/ 14622b730f8bSJeremy L Thompson int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 14632b730f8bSJeremy L Thompson const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1464d1d35e2fSjeremylt CeedInt P = num_nodes, Q = num_qpts, dim = 0; 1465a8de75f0Sjeremylt 14665fe0d4faSjeremylt if (!ceed->BasisCreateH1) { 14675fe0d4faSjeremylt Ceed delegate; 14686574a04fSJeremy L Thompson 14692b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 14701ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateH1"); 14712b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis)); 1472e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14735fe0d4faSjeremylt } 14745fe0d4faSjeremylt 1475ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1476ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1477ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1478227444bfSJeremy L Thompson 14792b730f8bSJeremy L Thompson CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1480a8de75f0Sjeremylt 1481db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1482db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1483d1d35e2fSjeremylt (*basis)->ref_count = 1; 14846402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1485a8de75f0Sjeremylt (*basis)->dim = dim; 1486d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 1487d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 1488a8de75f0Sjeremylt (*basis)->P = P; 1489a8de75f0Sjeremylt (*basis)->Q = Q; 1490c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 14912b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * dim, &(*basis)->q_ref_1d)); 14922b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &(*basis)->q_weight_1d)); 1493ff3a0f91SJeremy L Thompson if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1494ff3a0f91SJeremy L Thompson if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 14952b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * P, &(*basis)->interp)); 14962b730f8bSJeremy L Thompson CeedCall(CeedCalloc(dim * Q * P, &(*basis)->grad)); 1497ff3a0f91SJeremy L Thompson if (interp) memcpy((*basis)->interp, interp, Q * P * sizeof(interp[0])); 1498ff3a0f91SJeremy L Thompson if (grad) memcpy((*basis)->grad, grad, dim * Q * P * sizeof(grad[0])); 14992b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, *basis)); 1500e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1501a8de75f0Sjeremylt } 1502a8de75f0Sjeremylt 1503a8de75f0Sjeremylt /** 1504859c15bbSJames Wright @brief Create a non tensor-product basis for \f$H(\mathrm{div})\f$ discretizations 150550c301a5SRezgar Shakeri 1506ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1507ea61e9acSJeremy 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 1508ea61e9acSJeremy L Thompson @param[in] num_comp Number of components (usually 1 for vectors in H(div) bases) 1509ca94c3ddSJeremy L Thompson @param[in] num_nodes Total number of nodes (DoFs per element) 1510ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1511ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points 1512ca94c3ddSJeremy L Thompson @param[in] div Row-major (`num_qpts * num_nodes`) matrix expressing divergence of basis functions at quadrature points 1513ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element 1514ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1515ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 151650c301a5SRezgar Shakeri 151750c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 151850c301a5SRezgar Shakeri 151950c301a5SRezgar Shakeri @ref User 152050c301a5SRezgar Shakeri **/ 15212b730f8bSJeremy L Thompson int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 15222b730f8bSJeremy L Thompson const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 152350c301a5SRezgar Shakeri CeedInt Q = num_qpts, P = num_nodes, dim = 0; 1524c4e3f59bSSebastian Grimberg 152550c301a5SRezgar Shakeri if (!ceed->BasisCreateHdiv) { 152650c301a5SRezgar Shakeri Ceed delegate; 15276574a04fSJeremy L Thompson 15282b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 15296574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv"); 15302b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis)); 153150c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 153250c301a5SRezgar Shakeri } 153350c301a5SRezgar Shakeri 1534ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1535ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1536ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1537227444bfSJeremy L Thompson 1538c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1539c4e3f59bSSebastian Grimberg 1540db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1541db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 154250c301a5SRezgar Shakeri (*basis)->ref_count = 1; 15436402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 154450c301a5SRezgar Shakeri (*basis)->dim = dim; 154550c301a5SRezgar Shakeri (*basis)->topo = topo; 154650c301a5SRezgar Shakeri (*basis)->num_comp = num_comp; 154750c301a5SRezgar Shakeri (*basis)->P = P; 154850c301a5SRezgar Shakeri (*basis)->Q = Q; 1549c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HDIV; 15502b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 15512b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 155250c301a5SRezgar Shakeri if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 155350c301a5SRezgar Shakeri if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 15542b730f8bSJeremy L Thompson CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 15552b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * P, &(*basis)->div)); 155650c301a5SRezgar Shakeri if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 155750c301a5SRezgar Shakeri if (div) memcpy((*basis)->div, div, Q * P * sizeof(div[0])); 15582b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, q_weight, *basis)); 155950c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 156050c301a5SRezgar Shakeri } 156150c301a5SRezgar Shakeri 156250c301a5SRezgar Shakeri /** 15634385fb7fSSebastian Grimberg @brief Create a non tensor-product basis for \f$H(\mathrm{curl})\f$ discretizations 1564c4e3f59bSSebastian Grimberg 1565ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1566c4e3f59bSSebastian Grimberg @param[in] topo Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below 1567ca94c3ddSJeremy L Thompson @param[in] num_comp Number of components (usually 1 for vectors in \f$H(\mathrm{curl})\f$ bases) 1568ca94c3ddSJeremy L Thompson @param[in] num_nodes Total number of nodes (DoFs per element) 1569c4e3f59bSSebastian Grimberg @param[in] num_qpts Total number of quadrature points 1570ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points 1571ca94c3ddSJeremy 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 1572ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element 1573ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1574ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1575c4e3f59bSSebastian Grimberg 1576c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1577c4e3f59bSSebastian Grimberg 1578c4e3f59bSSebastian Grimberg @ref User 1579c4e3f59bSSebastian Grimberg **/ 1580c4e3f59bSSebastian Grimberg int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 1581c4e3f59bSSebastian Grimberg const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1582c4e3f59bSSebastian Grimberg CeedInt Q = num_qpts, P = num_nodes, dim = 0, curl_comp = 0; 1583c4e3f59bSSebastian Grimberg 1584d075f50bSSebastian Grimberg if (!ceed->BasisCreateHcurl) { 1585c4e3f59bSSebastian Grimberg Ceed delegate; 15866574a04fSJeremy L Thompson 1587c4e3f59bSSebastian Grimberg CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 15886574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl"); 1589c4e3f59bSSebastian Grimberg CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis)); 1590c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1591c4e3f59bSSebastian Grimberg } 1592c4e3f59bSSebastian Grimberg 1593ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1594ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1595ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1596c4e3f59bSSebastian Grimberg 1597c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1598c4e3f59bSSebastian Grimberg curl_comp = (dim < 3) ? 1 : dim; 1599c4e3f59bSSebastian Grimberg 1600db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1601db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1602c4e3f59bSSebastian Grimberg (*basis)->ref_count = 1; 16036402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1604c4e3f59bSSebastian Grimberg (*basis)->dim = dim; 1605c4e3f59bSSebastian Grimberg (*basis)->topo = topo; 1606c4e3f59bSSebastian Grimberg (*basis)->num_comp = num_comp; 1607c4e3f59bSSebastian Grimberg (*basis)->P = P; 1608c4e3f59bSSebastian Grimberg (*basis)->Q = Q; 1609c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HCURL; 1610c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 1611c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 1612c4e3f59bSSebastian Grimberg if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1613c4e3f59bSSebastian Grimberg if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 1614c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 1615c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(curl_comp * Q * P, &(*basis)->curl)); 1616c4e3f59bSSebastian Grimberg if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 1617c4e3f59bSSebastian Grimberg if (curl) memcpy((*basis)->curl, curl, curl_comp * Q * P * sizeof(curl[0])); 1618c4e3f59bSSebastian Grimberg CeedCall(ceed->BasisCreateHcurl(topo, dim, P, Q, interp, curl, q_ref, q_weight, *basis)); 1619c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1620c4e3f59bSSebastian Grimberg } 1621c4e3f59bSSebastian Grimberg 1622c4e3f59bSSebastian Grimberg /** 1623ca94c3ddSJeremy L Thompson @brief Create a `CeedBasis` for projection from the nodes of `basis_from` to the nodes of `basis_to`. 1624ba59ac12SSebastian Grimberg 1625ca94c3ddSJeremy L Thompson Only @ref CEED_EVAL_INTERP will be valid for the new basis, `basis_project`. 1626ca94c3ddSJeremy L Thompson For \f$H^1\f$ spaces, @ref CEED_EVAL_GRAD will also be valid. 1627ca94c3ddSJeremy L Thompson The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization. 1628ca94c3ddSJeremy L Thompson The gradient (for the \f$H^1\f$ case) is given by `grad_project = interp_to^+ * grad_from`. 162915ad3917SSebastian Grimberg 163015ad3917SSebastian Grimberg Note: `basis_from` and `basis_to` must have compatible quadrature spaces. 163115ad3917SSebastian Grimberg 16329fd66db6SSebastian Grimberg Note: `basis_project` will have the same number of components as `basis_from`, regardless of the number of components that `basis_to` has. 16339fd66db6SSebastian Grimberg If `basis_from` has 3 components and `basis_to` has 5 components, then `basis_project` will have 3 components. 1634f113e5dcSJeremy L Thompson 1635e104ad11SJames Wright Note: If either `basis_from` or `basis_to` are non-tensor, then `basis_project` will also be non-tensor 1636e104ad11SJames Wright 1637ca94c3ddSJeremy L Thompson @param[in] basis_from `CeedBasis` to prolong from 1638ca94c3ddSJeremy L Thompson @param[in] basis_to `CeedBasis` to prolong to 1639ca94c3ddSJeremy L Thompson @param[out] basis_project Address of the variable where the newly created `CeedBasis` will be stored 1640f113e5dcSJeremy L Thompson 1641f113e5dcSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1642f113e5dcSJeremy L Thompson 1643f113e5dcSJeremy L Thompson @ref User 1644f113e5dcSJeremy L Thompson **/ 16452b730f8bSJeremy L Thompson int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project) { 1646f113e5dcSJeremy L Thompson Ceed ceed; 1647e104ad11SJames Wright bool create_tensor; 16481c66c397SJeremy L Thompson CeedInt dim, num_comp; 1649097cc795SJames Wright CeedScalar *interp_project, *grad_project; 16501c66c397SJeremy L Thompson 16512b730f8bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis_to, &ceed)); 1652f113e5dcSJeremy L Thompson 1653ecc88aebSJeremy L Thompson // Create projection matrix 16542b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateProjectionMatrices(basis_from, basis_to, &interp_project, &grad_project)); 1655f113e5dcSJeremy L Thompson 1656f113e5dcSJeremy L Thompson // Build basis 1657e104ad11SJames Wright { 1658e104ad11SJames Wright bool is_tensor_to, is_tensor_from; 1659e104ad11SJames Wright 1660e104ad11SJames Wright CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to)); 1661e104ad11SJames Wright CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from)); 1662e104ad11SJames Wright create_tensor = is_tensor_from && is_tensor_to; 1663e104ad11SJames Wright } 16642b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_to, &dim)); 16652b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_from, &num_comp)); 1666e104ad11SJames Wright if (create_tensor) { 1667f113e5dcSJeremy L Thompson CeedInt P_1d_to, P_1d_from; 16681c66c397SJeremy L Thompson 16692b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_1d_from)); 16702b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_1d_to)); 1671097cc795SJames Wright CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, interp_project, grad_project, NULL, NULL, basis_project)); 1672f113e5dcSJeremy L Thompson } else { 1673de05fbb2SSebastian Grimberg // Even if basis_to and basis_from are not H1, the resulting basis is H1 for interpolation to work 1674f113e5dcSJeremy L Thompson CeedInt num_nodes_to, num_nodes_from; 16751c66c397SJeremy L Thompson CeedElemTopology topo; 16761c66c397SJeremy L Thompson 1677e00f3be8SJames Wright CeedCall(CeedBasisGetTopology(basis_from, &topo)); 16782b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_from, &num_nodes_from)); 16792b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_to, &num_nodes_to)); 1680097cc795SJames Wright CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, NULL, NULL, basis_project)); 1681f113e5dcSJeremy L Thompson } 1682f113e5dcSJeremy L Thompson 1683f113e5dcSJeremy L Thompson // Cleanup 16842b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_project)); 16852b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_project)); 1686f113e5dcSJeremy L Thompson return CEED_ERROR_SUCCESS; 1687f113e5dcSJeremy L Thompson } 1688f113e5dcSJeremy L Thompson 1689f113e5dcSJeremy L Thompson /** 1690ca94c3ddSJeremy L Thompson @brief Copy the pointer to a `CeedBasis`. 16919560d06aSjeremylt 1692ca94c3ddSJeremy 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`. 1693ca94c3ddSJeremy L Thompson This `CeedBasis` will be destroyed if `*basis_copy` is the only reference to this `CeedBasis`. 1694ea61e9acSJeremy L Thompson 1695ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to copy reference to 1696ea61e9acSJeremy L Thompson @param[in,out] basis_copy Variable to store copied reference 16979560d06aSjeremylt 16989560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 16999560d06aSjeremylt 17009560d06aSjeremylt @ref User 17019560d06aSjeremylt **/ 17029560d06aSjeremylt int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) { 1703356036faSJeremy L Thompson if (basis != CEED_BASIS_NONE) CeedCall(CeedBasisReference(basis)); 17042b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(basis_copy)); 17059560d06aSjeremylt *basis_copy = basis; 17069560d06aSjeremylt return CEED_ERROR_SUCCESS; 17079560d06aSjeremylt } 17089560d06aSjeremylt 17099560d06aSjeremylt /** 1710ca94c3ddSJeremy L Thompson @brief View a `CeedBasis` 17117a982d89SJeremy L. Thompson 1712ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to view 1713ca94c3ddSJeremy L Thompson @param[in] stream Stream to view to, e.g., `stdout` 17147a982d89SJeremy L. Thompson 17157a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 17167a982d89SJeremy L. Thompson 17177a982d89SJeremy L. Thompson @ref User 17187a982d89SJeremy L. Thompson **/ 17197a982d89SJeremy L. Thompson int CeedBasisView(CeedBasis basis, FILE *stream) { 17201203703bSJeremy L Thompson bool is_tensor_basis; 17211203703bSJeremy L Thompson CeedElemTopology topo; 17221203703bSJeremy L Thompson CeedFESpace fe_space; 17231203703bSJeremy L Thompson 17241203703bSJeremy L Thompson // Basis data 17251203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 17261203703bSJeremy L Thompson CeedCall(CeedBasisGetTopology(basis, &topo)); 17271203703bSJeremy L Thompson CeedCall(CeedBasisGetFESpace(basis, &fe_space)); 17282b730f8bSJeremy L Thompson 172950c301a5SRezgar Shakeri // Print FE space and element topology of the basis 1730edf04919SJeremy L Thompson fprintf(stream, "CeedBasis in a %s on a %s element\n", CeedFESpaces[fe_space], CeedElemTopologies[topo]); 17311203703bSJeremy L Thompson if (is_tensor_basis) { 1732edf04919SJeremy L Thompson fprintf(stream, " P: %" CeedInt_FMT "\n Q: %" CeedInt_FMT "\n", basis->P_1d, basis->Q_1d); 173350c301a5SRezgar Shakeri } else { 1734edf04919SJeremy L Thompson fprintf(stream, " P: %" CeedInt_FMT "\n Q: %" CeedInt_FMT "\n", basis->P, basis->Q); 173550c301a5SRezgar Shakeri } 1736edf04919SJeremy L Thompson fprintf(stream, " dimension: %" CeedInt_FMT "\n field components: %" CeedInt_FMT "\n", basis->dim, basis->num_comp); 1737ea61e9acSJeremy L Thompson // Print quadrature data, interpolation/gradient/divergence/curl of the basis 17381203703bSJeremy L Thompson if (is_tensor_basis) { // tensor basis 17391203703bSJeremy L Thompson CeedInt P_1d, Q_1d; 17401203703bSJeremy L Thompson const CeedScalar *q_ref_1d, *q_weight_1d, *interp_1d, *grad_1d; 17411203703bSJeremy L Thompson 17421203703bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 17431203703bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 17441203703bSJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 17451203703bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d)); 17461203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 17471203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 17481203703bSJeremy L Thompson 17491203703bSJeremy L Thompson CeedCall(CeedScalarView("qref1d", "\t% 12.8f", 1, Q_1d, q_ref_1d, stream)); 17501203703bSJeremy L Thompson CeedCall(CeedScalarView("qweight1d", "\t% 12.8f", 1, Q_1d, q_weight_1d, stream)); 17511203703bSJeremy L Thompson CeedCall(CeedScalarView("interp1d", "\t% 12.8f", Q_1d, P_1d, interp_1d, stream)); 17521203703bSJeremy L Thompson CeedCall(CeedScalarView("grad1d", "\t% 12.8f", Q_1d, P_1d, grad_1d, stream)); 175350c301a5SRezgar Shakeri } else { // non-tensor basis 17541203703bSJeremy L Thompson CeedInt P, Q, dim, q_comp; 17551203703bSJeremy L Thompson const CeedScalar *q_ref, *q_weight, *interp, *grad, *div, *curl; 17561203703bSJeremy L Thompson 17571203703bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &P)); 17581203703bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &Q)); 17591203703bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 17601203703bSJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref)); 17611203703bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight)); 17621203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis, &interp)); 17631203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad(basis, &grad)); 17641203703bSJeremy L Thompson CeedCall(CeedBasisGetDiv(basis, &div)); 17651203703bSJeremy L Thompson CeedCall(CeedBasisGetCurl(basis, &curl)); 17661203703bSJeremy L Thompson 17671203703bSJeremy L Thompson CeedCall(CeedScalarView("qref", "\t% 12.8f", 1, Q * dim, q_ref, stream)); 17681203703bSJeremy L Thompson CeedCall(CeedScalarView("qweight", "\t% 12.8f", 1, Q, q_weight, stream)); 1769c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp)); 17701203703bSJeremy L Thompson CeedCall(CeedScalarView("interp", "\t% 12.8f", q_comp * Q, P, interp, stream)); 17711203703bSJeremy L Thompson if (grad) { 1772c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp)); 17731203703bSJeremy L Thompson CeedCall(CeedScalarView("grad", "\t% 12.8f", q_comp * Q, P, grad, stream)); 17747a982d89SJeremy L. Thompson } 17751203703bSJeremy L Thompson if (div) { 1776c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp)); 17771203703bSJeremy L Thompson CeedCall(CeedScalarView("div", "\t% 12.8f", q_comp * Q, P, div, stream)); 1778c4e3f59bSSebastian Grimberg } 17791203703bSJeremy L Thompson if (curl) { 1780c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp)); 17811203703bSJeremy L Thompson CeedCall(CeedScalarView("curl", "\t% 12.8f", q_comp * Q, P, curl, stream)); 178250c301a5SRezgar Shakeri } 178350c301a5SRezgar Shakeri } 1784e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 17857a982d89SJeremy L. Thompson } 17867a982d89SJeremy L. Thompson 17877a982d89SJeremy L. Thompson /** 1788db2becc9SJeremy L Thompson @brief Check input vector dimensions for CeedBasisApply[Add] 17897a982d89SJeremy L. Thompson 1790ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1791ea61e9acSJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1792ca94c3ddSJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1793ca94c3ddSJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points; 1794ca94c3ddSJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes 1795ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 1796ca94c3ddSJeremy L Thompson @ref CEED_EVAL_INTERP to use interpolated values, 1797ca94c3ddSJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1798ca94c3ddSJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 1799ca94c3ddSJeremy L Thompson @ref CEED_EVAL_CURL to use curl, 1800ca94c3ddSJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1801ca94c3ddSJeremy L Thompson @param[in] u Input `CeedVector` 1802ca94c3ddSJeremy L Thompson @param[out] v Output `CeedVector` 18037a982d89SJeremy L. Thompson 18047a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 18057a982d89SJeremy L. Thompson 1806db2becc9SJeremy L Thompson @ref Developer 18077a982d89SJeremy L. Thompson **/ 1808db2becc9SJeremy L Thompson static int CeedBasisApplyCheckDims(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 1809c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 18101c66c397SJeremy L Thompson CeedSize u_length = 0, v_length; 18111203703bSJeremy L Thompson Ceed ceed; 18121c66c397SJeremy L Thompson 18131203703bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 18142b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 18152b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 1816c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 18172b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 18182b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 18192b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(v, &v_length)); 1820c8c3fa7dSJeremy L Thompson if (u) CeedCall(CeedVectorGetLength(u, &u_length)); 18217a982d89SJeremy L. Thompson 1822e15f9bd0SJeremy L Thompson // Check compatibility of topological and geometrical dimensions 18236574a04fSJeremy L Thompson CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0 && u_length % num_qpts == 0) || 18246574a04fSJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0 && v_length % num_qpts == 0), 18251203703bSJeremy L Thompson ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions"); 18267a982d89SJeremy L. Thompson 1827e15f9bd0SJeremy L Thompson // Check vector lengths to prevent out of bounds issues 182899e754f0SJeremy L Thompson bool has_good_dims = true; 1829d1d35e2fSjeremylt switch (eval_mode) { 1830e15f9bd0SJeremy L Thompson case CEED_EVAL_NONE: 18312b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 18322b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 1833c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 1834c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 183599e754f0SJeremy L Thompson has_good_dims = 18366574a04fSJeremy L Thompson ((t_mode == CEED_TRANSPOSE && u_length >= num_elem * num_comp * num_qpts * q_comp && v_length >= num_elem * num_comp * num_nodes) || 18376574a04fSJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && v_length >= num_elem * num_qpts * num_comp * q_comp && u_length >= num_elem * num_comp * num_nodes)); 1838e15f9bd0SJeremy L Thompson break; 1839e15f9bd0SJeremy L Thompson case CEED_EVAL_WEIGHT: 184099e754f0SJeremy L Thompson has_good_dims = v_length >= num_elem * num_qpts; 1841e15f9bd0SJeremy L Thompson break; 1842e15f9bd0SJeremy L Thompson } 184399e754f0SJeremy L Thompson CeedCheck(has_good_dims, ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 1844db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 1845db2becc9SJeremy L Thompson } 1846e15f9bd0SJeremy L Thompson 1847db2becc9SJeremy L Thompson /** 1848db2becc9SJeremy L Thompson @brief Apply basis evaluation from nodes to quadrature points or vice versa 1849db2becc9SJeremy L Thompson 1850db2becc9SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1851db2becc9SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1852db2becc9SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1853db2becc9SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points; 1854db2becc9SJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes 1855db2becc9SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 1856db2becc9SJeremy L Thompson @ref CEED_EVAL_INTERP to use interpolated values, 1857db2becc9SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1858db2becc9SJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 1859db2becc9SJeremy L Thompson @ref CEED_EVAL_CURL to use curl, 1860db2becc9SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1861db2becc9SJeremy L Thompson @param[in] u Input `CeedVector` 1862db2becc9SJeremy L Thompson @param[out] v Output `CeedVector` 1863db2becc9SJeremy L Thompson 1864db2becc9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1865db2becc9SJeremy L Thompson 1866db2becc9SJeremy L Thompson @ref User 1867db2becc9SJeremy L Thompson **/ 1868db2becc9SJeremy L Thompson int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 1869db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyCheckDims(basis, num_elem, t_mode, eval_mode, u, v)); 1870db2becc9SJeremy L Thompson CeedCheck(basis->Apply, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedBasisApply"); 18712b730f8bSJeremy L Thompson CeedCall(basis->Apply(basis, num_elem, t_mode, eval_mode, u, v)); 1872e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18737a982d89SJeremy L. Thompson } 18747a982d89SJeremy L. Thompson 18757a982d89SJeremy L. Thompson /** 1876db2becc9SJeremy L Thompson @brief Apply basis evaluation from quadrature points to nodes and sum into target vector 1877db2becc9SJeremy L Thompson 1878db2becc9SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1879db2becc9SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1880db2becc9SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1881db2becc9SJeremy L Thompson @param[in] t_mode @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes; 1882db2becc9SJeremy L Thompson @ref CEED_NOTRANSPOSE is not valid for `CeedBasisApplyAdd()` 1883db2becc9SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 1884db2becc9SJeremy L Thompson @ref CEED_EVAL_INTERP to use interpolated values, 1885db2becc9SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1886db2becc9SJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 1887db2becc9SJeremy L Thompson @ref CEED_EVAL_CURL to use curl, 1888db2becc9SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1889db2becc9SJeremy L Thompson @param[in] u Input `CeedVector` 1890db2becc9SJeremy L Thompson @param[out] v Output `CeedVector` to sum into 1891db2becc9SJeremy L Thompson 1892db2becc9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1893db2becc9SJeremy L Thompson 1894db2becc9SJeremy L Thompson @ref User 1895db2becc9SJeremy L Thompson **/ 1896db2becc9SJeremy L Thompson int CeedBasisApplyAdd(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 1897db2becc9SJeremy L Thompson CeedCheck(t_mode == CEED_TRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "CeedBasisApplyAdd only supports CEED_TRANSPOSE"); 1898db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyCheckDims(basis, num_elem, t_mode, eval_mode, u, v)); 1899db2becc9SJeremy L Thompson CeedCheck(basis->ApplyAdd, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedBasisApplyAdd"); 1900db2becc9SJeremy L Thompson CeedCall(basis->ApplyAdd(basis, num_elem, t_mode, eval_mode, u, v)); 1901db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 1902db2becc9SJeremy L Thompson } 1903db2becc9SJeremy L Thompson 1904db2becc9SJeremy L Thompson /** 1905db2becc9SJeremy L Thompson @brief Apply basis evaluation from nodes to arbitrary points 1906db2becc9SJeremy L Thompson 1907db2becc9SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1908db2becc9SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1909db2becc9SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1910db2becc9SJeremy L Thompson @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 1911db2becc9SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 1912db2becc9SJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes 1913db2becc9SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 1914db2becc9SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1915db2becc9SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1916db2becc9SJeremy L Thompson @param[in] x_ref `CeedVector` holding reference coordinates of each point 1917db2becc9SJeremy L Thompson @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 1918db2becc9SJeremy L Thompson @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 1919db2becc9SJeremy L Thompson 1920db2becc9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1921db2becc9SJeremy L Thompson 1922db2becc9SJeremy L Thompson @ref User 1923db2becc9SJeremy L Thompson **/ 1924db2becc9SJeremy L Thompson int CeedBasisApplyAtPoints(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, 1925db2becc9SJeremy L Thompson CeedVector x_ref, CeedVector u, CeedVector v) { 1926db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyAtPointsCheckDims(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 1927db2becc9SJeremy L Thompson if (basis->ApplyAtPoints) { 1928db2becc9SJeremy L Thompson CeedCall(basis->ApplyAtPoints(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 1929db2becc9SJeremy L Thompson } else { 1930db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyAtPoints_Core(basis, false, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 1931db2becc9SJeremy L Thompson } 1932db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 1933db2becc9SJeremy L Thompson } 1934db2becc9SJeremy L Thompson 1935db2becc9SJeremy L Thompson /** 1936db2becc9SJeremy L Thompson @brief Apply basis evaluation from nodes to arbitrary points and sum into target vector 1937db2becc9SJeremy L Thompson 1938db2becc9SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1939db2becc9SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1940db2becc9SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1941db2becc9SJeremy L Thompson @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 1942db2becc9SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 1943db2becc9SJeremy L Thompson @ref CEED_NOTRANSPOSE is not valid for `CeedBasisApplyAddAtPoints()` 1944db2becc9SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 1945db2becc9SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1946db2becc9SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1947db2becc9SJeremy L Thompson @param[in] x_ref `CeedVector` holding reference coordinates of each point 1948db2becc9SJeremy L Thompson @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 1949db2becc9SJeremy L Thompson @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 1950db2becc9SJeremy L Thompson 1951db2becc9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1952db2becc9SJeremy L Thompson 1953db2becc9SJeremy L Thompson @ref User 1954db2becc9SJeremy L Thompson **/ 1955db2becc9SJeremy L Thompson int CeedBasisApplyAddAtPoints(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, 1956db2becc9SJeremy L Thompson CeedVector x_ref, CeedVector u, CeedVector v) { 1957db2becc9SJeremy L Thompson CeedCheck(t_mode == CEED_TRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "CeedBasisApplyAddAtPoints only supports CEED_TRANSPOSE"); 1958db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyAtPointsCheckDims(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 1959db2becc9SJeremy L Thompson if (basis->ApplyAddAtPoints) { 1960db2becc9SJeremy L Thompson CeedCall(basis->ApplyAddAtPoints(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 1961db2becc9SJeremy L Thompson } else { 1962db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyAtPoints_Core(basis, true, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 1963db2becc9SJeremy L Thompson } 1964db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 1965db2becc9SJeremy L Thompson } 1966db2becc9SJeremy L Thompson 1967db2becc9SJeremy L Thompson /** 19686e536b99SJeremy L Thompson @brief Get the `Ceed` associated with a `CeedBasis` 1969b7c9bbdaSJeremy L Thompson 1970ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1971ca94c3ddSJeremy L Thompson @param[out] ceed Variable to store `Ceed` 1972b7c9bbdaSJeremy L Thompson 1973b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1974b7c9bbdaSJeremy L Thompson 1975b7c9bbdaSJeremy L Thompson @ref Advanced 1976b7c9bbdaSJeremy L Thompson **/ 1977b7c9bbdaSJeremy L Thompson int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) { 19786e536b99SJeremy L Thompson *ceed = CeedBasisReturnCeed(basis); 1979b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1980b7c9bbdaSJeremy L Thompson } 1981b7c9bbdaSJeremy L Thompson 1982b7c9bbdaSJeremy L Thompson /** 19836e536b99SJeremy L Thompson @brief Return the `Ceed` associated with a `CeedBasis` 19846e536b99SJeremy L Thompson 19856e536b99SJeremy L Thompson @param[in] basis `CeedBasis` 19866e536b99SJeremy L Thompson 19876e536b99SJeremy L Thompson @return `Ceed` associated with the `basis` 19886e536b99SJeremy L Thompson 19896e536b99SJeremy L Thompson @ref Advanced 19906e536b99SJeremy L Thompson **/ 19916e536b99SJeremy L Thompson Ceed CeedBasisReturnCeed(CeedBasis basis) { return basis->ceed; } 19926e536b99SJeremy L Thompson 19936e536b99SJeremy L Thompson /** 1994ca94c3ddSJeremy L Thompson @brief Get dimension for given `CeedBasis` 19959d007619Sjeremylt 1996ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 19979d007619Sjeremylt @param[out] dim Variable to store dimension of basis 19989d007619Sjeremylt 19999d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20009d007619Sjeremylt 2001b7c9bbdaSJeremy L Thompson @ref Advanced 20029d007619Sjeremylt **/ 20039d007619Sjeremylt int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) { 20049d007619Sjeremylt *dim = basis->dim; 2005e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20069d007619Sjeremylt } 20079d007619Sjeremylt 20089d007619Sjeremylt /** 2009ca94c3ddSJeremy L Thompson @brief Get topology for given `CeedBasis` 2010d99fa3c5SJeremy L Thompson 2011ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2012d99fa3c5SJeremy L Thompson @param[out] topo Variable to store topology of basis 2013d99fa3c5SJeremy L Thompson 2014d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2015d99fa3c5SJeremy L Thompson 2016b7c9bbdaSJeremy L Thompson @ref Advanced 2017d99fa3c5SJeremy L Thompson **/ 2018d99fa3c5SJeremy L Thompson int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) { 2019d99fa3c5SJeremy L Thompson *topo = basis->topo; 2020e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2021d99fa3c5SJeremy L Thompson } 2022d99fa3c5SJeremy L Thompson 2023d99fa3c5SJeremy L Thompson /** 2024ca94c3ddSJeremy L Thompson @brief Get number of components for given `CeedBasis` 20259d007619Sjeremylt 2026ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2027ca94c3ddSJeremy L Thompson @param[out] num_comp Variable to store number of components 20289d007619Sjeremylt 20299d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20309d007619Sjeremylt 2031b7c9bbdaSJeremy L Thompson @ref Advanced 20329d007619Sjeremylt **/ 2033d1d35e2fSjeremylt int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) { 2034d1d35e2fSjeremylt *num_comp = basis->num_comp; 2035e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20369d007619Sjeremylt } 20379d007619Sjeremylt 20389d007619Sjeremylt /** 2039ca94c3ddSJeremy L Thompson @brief Get total number of nodes (in `dim` dimensions) of a `CeedBasis` 20409d007619Sjeremylt 2041ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 20429d007619Sjeremylt @param[out] P Variable to store number of nodes 20439d007619Sjeremylt 20449d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20459d007619Sjeremylt 20469d007619Sjeremylt @ref Utility 20479d007619Sjeremylt **/ 20489d007619Sjeremylt int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) { 20499d007619Sjeremylt *P = basis->P; 2050e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20519d007619Sjeremylt } 20529d007619Sjeremylt 20539d007619Sjeremylt /** 2054ca94c3ddSJeremy L Thompson @brief Get total number of nodes (in 1 dimension) of a `CeedBasis` 20559d007619Sjeremylt 2056ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2057d1d35e2fSjeremylt @param[out] P_1d Variable to store number of nodes 20589d007619Sjeremylt 20599d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20609d007619Sjeremylt 2061b7c9bbdaSJeremy L Thompson @ref Advanced 20629d007619Sjeremylt **/ 2063d1d35e2fSjeremylt int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) { 20646e536b99SJeremy L Thompson CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply P_1d for non-tensor CeedBasis"); 2065d1d35e2fSjeremylt *P_1d = basis->P_1d; 2066e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20679d007619Sjeremylt } 20689d007619Sjeremylt 20699d007619Sjeremylt /** 2070ca94c3ddSJeremy L Thompson @brief Get total number of quadrature points (in `dim` dimensions) of a `CeedBasis` 20719d007619Sjeremylt 2072ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 20739d007619Sjeremylt @param[out] Q Variable to store number of quadrature points 20749d007619Sjeremylt 20759d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20769d007619Sjeremylt 20779d007619Sjeremylt @ref Utility 20789d007619Sjeremylt **/ 20799d007619Sjeremylt int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) { 20809d007619Sjeremylt *Q = basis->Q; 2081e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20829d007619Sjeremylt } 20839d007619Sjeremylt 20849d007619Sjeremylt /** 2085ca94c3ddSJeremy L Thompson @brief Get total number of quadrature points (in 1 dimension) of a `CeedBasis` 20869d007619Sjeremylt 2087ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2088d1d35e2fSjeremylt @param[out] Q_1d Variable to store number of quadrature points 20899d007619Sjeremylt 20909d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20919d007619Sjeremylt 2092b7c9bbdaSJeremy L Thompson @ref Advanced 20939d007619Sjeremylt **/ 2094d1d35e2fSjeremylt int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) { 20956e536b99SJeremy L Thompson CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply Q_1d for non-tensor CeedBasis"); 2096d1d35e2fSjeremylt *Q_1d = basis->Q_1d; 2097e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20989d007619Sjeremylt } 20999d007619Sjeremylt 21009d007619Sjeremylt /** 2101ca94c3ddSJeremy L Thompson @brief Get reference coordinates of quadrature points (in `dim` dimensions) of a `CeedBasis` 21029d007619Sjeremylt 2103ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2104d1d35e2fSjeremylt @param[out] q_ref Variable to store reference coordinates of quadrature points 21059d007619Sjeremylt 21069d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21079d007619Sjeremylt 2108b7c9bbdaSJeremy L Thompson @ref Advanced 21099d007619Sjeremylt **/ 2110d1d35e2fSjeremylt int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) { 2111d1d35e2fSjeremylt *q_ref = basis->q_ref_1d; 2112e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21139d007619Sjeremylt } 21149d007619Sjeremylt 21159d007619Sjeremylt /** 2116ca94c3ddSJeremy L Thompson @brief Get quadrature weights of quadrature points (in `dim` dimensions) of a `CeedBasis` 21179d007619Sjeremylt 2118ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2119d1d35e2fSjeremylt @param[out] q_weight Variable to store quadrature weights 21209d007619Sjeremylt 21219d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21229d007619Sjeremylt 2123b7c9bbdaSJeremy L Thompson @ref Advanced 21249d007619Sjeremylt **/ 2125d1d35e2fSjeremylt int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) { 2126d1d35e2fSjeremylt *q_weight = basis->q_weight_1d; 2127e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21289d007619Sjeremylt } 21299d007619Sjeremylt 21309d007619Sjeremylt /** 2131ca94c3ddSJeremy L Thompson @brief Get interpolation matrix of a `CeedBasis` 21329d007619Sjeremylt 2133ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 21349d007619Sjeremylt @param[out] interp Variable to store interpolation matrix 21359d007619Sjeremylt 21369d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21379d007619Sjeremylt 2138b7c9bbdaSJeremy L Thompson @ref Advanced 21399d007619Sjeremylt **/ 21406c58de82SJeremy L Thompson int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) { 21416402da51SJeremy L Thompson if (!basis->interp && basis->is_tensor_basis) { 21429d007619Sjeremylt // Allocate 21432b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->Q * basis->P, &basis->interp)); 21449d007619Sjeremylt 21459d007619Sjeremylt // Initialize 21462b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->Q * basis->P; i++) basis->interp[i] = 1.0; 21479d007619Sjeremylt 21489d007619Sjeremylt // Calculate 21492b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 21502b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 21519d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 2152d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 2153d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 21541c66c397SJeremy L Thompson 2155d1d35e2fSjeremylt basis->interp[qpt * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 21569d007619Sjeremylt } 21579d007619Sjeremylt } 21582b730f8bSJeremy L Thompson } 21592b730f8bSJeremy L Thompson } 21609d007619Sjeremylt *interp = basis->interp; 2161e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21629d007619Sjeremylt } 21639d007619Sjeremylt 21649d007619Sjeremylt /** 2165ca94c3ddSJeremy L Thompson @brief Get 1D interpolation matrix of a tensor product `CeedBasis` 21669d007619Sjeremylt 2167ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2168d1d35e2fSjeremylt @param[out] interp_1d Variable to store interpolation matrix 21699d007619Sjeremylt 21709d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21719d007619Sjeremylt 21729d007619Sjeremylt @ref Backend 21739d007619Sjeremylt **/ 2174d1d35e2fSjeremylt int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) { 21751203703bSJeremy L Thompson bool is_tensor_basis; 21761203703bSJeremy L Thompson 21771203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 21786e536b99SJeremy L Thompson CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis"); 2179d1d35e2fSjeremylt *interp_1d = basis->interp_1d; 2180e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21819d007619Sjeremylt } 21829d007619Sjeremylt 21839d007619Sjeremylt /** 2184ca94c3ddSJeremy L Thompson @brief Get gradient matrix of a `CeedBasis` 21859d007619Sjeremylt 2186ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 21879d007619Sjeremylt @param[out] grad Variable to store gradient matrix 21889d007619Sjeremylt 21899d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21909d007619Sjeremylt 2191b7c9bbdaSJeremy L Thompson @ref Advanced 21929d007619Sjeremylt **/ 21936c58de82SJeremy L Thompson int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) { 21946402da51SJeremy L Thompson if (!basis->grad && basis->is_tensor_basis) { 21959d007619Sjeremylt // Allocate 21962b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->dim * basis->Q * basis->P, &basis->grad)); 21979d007619Sjeremylt 21989d007619Sjeremylt // Initialize 21992b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim * basis->Q * basis->P; i++) basis->grad[i] = 1.0; 22009d007619Sjeremylt 22019d007619Sjeremylt // Calculate 22022b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 22032b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim; i++) { 22042b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 22059d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 2206d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 2207d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 22081c66c397SJeremy L Thompson 22092b730f8bSJeremy L Thompson if (i == d) basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->grad_1d[q * basis->P_1d + p]; 22102b730f8bSJeremy L Thompson else basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 22112b730f8bSJeremy L Thompson } 22122b730f8bSJeremy L Thompson } 22132b730f8bSJeremy L Thompson } 22149d007619Sjeremylt } 22159d007619Sjeremylt } 22169d007619Sjeremylt *grad = basis->grad; 2217e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 22189d007619Sjeremylt } 22199d007619Sjeremylt 22209d007619Sjeremylt /** 2221ca94c3ddSJeremy L Thompson @brief Get 1D gradient matrix of a tensor product `CeedBasis` 22229d007619Sjeremylt 2223ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2224d1d35e2fSjeremylt @param[out] grad_1d Variable to store gradient matrix 22259d007619Sjeremylt 22269d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 22279d007619Sjeremylt 2228b7c9bbdaSJeremy L Thompson @ref Advanced 22299d007619Sjeremylt **/ 2230d1d35e2fSjeremylt int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) { 22311203703bSJeremy L Thompson bool is_tensor_basis; 22321203703bSJeremy L Thompson 22331203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 22346e536b99SJeremy L Thompson CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis"); 2235d1d35e2fSjeremylt *grad_1d = basis->grad_1d; 2236e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 22379d007619Sjeremylt } 22389d007619Sjeremylt 22399d007619Sjeremylt /** 2240ca94c3ddSJeremy L Thompson @brief Get divergence matrix of a `CeedBasis` 224150c301a5SRezgar Shakeri 2242ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 224350c301a5SRezgar Shakeri @param[out] div Variable to store divergence matrix 224450c301a5SRezgar Shakeri 224550c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 224650c301a5SRezgar Shakeri 224750c301a5SRezgar Shakeri @ref Advanced 224850c301a5SRezgar Shakeri **/ 224950c301a5SRezgar Shakeri int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) { 225050c301a5SRezgar Shakeri *div = basis->div; 225150c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 225250c301a5SRezgar Shakeri } 225350c301a5SRezgar Shakeri 225450c301a5SRezgar Shakeri /** 2255ca94c3ddSJeremy L Thompson @brief Get curl matrix of a `CeedBasis` 2256c4e3f59bSSebastian Grimberg 2257ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2258c4e3f59bSSebastian Grimberg @param[out] curl Variable to store curl matrix 2259c4e3f59bSSebastian Grimberg 2260c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 2261c4e3f59bSSebastian Grimberg 2262c4e3f59bSSebastian Grimberg @ref Advanced 2263c4e3f59bSSebastian Grimberg **/ 2264c4e3f59bSSebastian Grimberg int CeedBasisGetCurl(CeedBasis basis, const CeedScalar **curl) { 2265c4e3f59bSSebastian Grimberg *curl = basis->curl; 2266c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 2267c4e3f59bSSebastian Grimberg } 2268c4e3f59bSSebastian Grimberg 2269c4e3f59bSSebastian Grimberg /** 2270ca94c3ddSJeremy L Thompson @brief Destroy a @ref CeedBasis 22717a982d89SJeremy L. Thompson 2272ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` to destroy 22737a982d89SJeremy L. Thompson 22747a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 22757a982d89SJeremy L. Thompson 22767a982d89SJeremy L. Thompson @ref User 22777a982d89SJeremy L. Thompson **/ 22787a982d89SJeremy L. Thompson int CeedBasisDestroy(CeedBasis *basis) { 2279356036faSJeremy L Thompson if (!*basis || *basis == CEED_BASIS_NONE || --(*basis)->ref_count > 0) { 2280ad6481ceSJeremy L Thompson *basis = NULL; 2281ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 2282ad6481ceSJeremy L Thompson } 22832b730f8bSJeremy L Thompson if ((*basis)->Destroy) CeedCall((*basis)->Destroy(*basis)); 22849831d45aSJeremy L Thompson CeedCall(CeedTensorContractDestroy(&(*basis)->contract)); 2285c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_ref_1d)); 2286c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_weight_1d)); 22872b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp)); 22882b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp_1d)); 22892b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad)); 22902b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad_1d)); 2291c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->div)); 2292c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->curl)); 2293c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorDestroy(&(*basis)->vec_chebyshev)); 2294c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisDestroy(&(*basis)->basis_chebyshev)); 22952b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*basis)->ceed)); 22962b730f8bSJeremy L Thompson CeedCall(CeedFree(basis)); 2297e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 22987a982d89SJeremy L. Thompson } 22997a982d89SJeremy L. Thompson 23007a982d89SJeremy L. Thompson /** 2301b11c1e72Sjeremylt @brief Construct a Gauss-Legendre quadrature 2302b11c1e72Sjeremylt 2303ca94c3ddSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree `2*Q-1` exactly) 2304ca94c3ddSJeremy L Thompson @param[out] q_ref_1d Array of length `Q` to hold the abscissa on `[-1, 1]` 2305ca94c3ddSJeremy L Thompson @param[out] q_weight_1d Array of length `Q` to hold the weights 2306b11c1e72Sjeremylt 2307b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 2308dfdf5a53Sjeremylt 2309dfdf5a53Sjeremylt @ref Utility 2310b11c1e72Sjeremylt **/ 23112b730f8bSJeremy L Thompson int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2312d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0 * atan(1.0); 23131c66c397SJeremy L Thompson 2314d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 231592ae7e47SJeremy L Thompson for (CeedInt i = 0; i <= Q / 2; i++) { 2316d7b241e6Sjeremylt // Guess 2317d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(2 * i + 1) / ((CeedScalar)(2 * Q))); 2318d7b241e6Sjeremylt // Pn(xi) 2319d7b241e6Sjeremylt P0 = 1.0; 2320d7b241e6Sjeremylt P1 = xi; 2321d7b241e6Sjeremylt P2 = 0.0; 232292ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 2323d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2324d7b241e6Sjeremylt P0 = P1; 2325d7b241e6Sjeremylt P1 = P2; 2326d7b241e6Sjeremylt } 2327d7b241e6Sjeremylt // First Newton Step 2328d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2329d7b241e6Sjeremylt xi = xi - P2 / dP2; 2330d7b241e6Sjeremylt // Newton to convergence 233192ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(P2) > 10 * CEED_EPSILON; k++) { 2332d7b241e6Sjeremylt P0 = 1.0; 2333d7b241e6Sjeremylt P1 = xi; 233492ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 2335d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2336d7b241e6Sjeremylt P0 = P1; 2337d7b241e6Sjeremylt P1 = P2; 2338d7b241e6Sjeremylt } 2339d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2340d7b241e6Sjeremylt xi = xi - P2 / dP2; 2341d7b241e6Sjeremylt } 2342d7b241e6Sjeremylt // Save xi, wi 2343d7b241e6Sjeremylt wi = 2.0 / ((1.0 - xi * xi) * dP2 * dP2); 2344d1d35e2fSjeremylt q_weight_1d[i] = wi; 2345d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 2346d1d35e2fSjeremylt q_ref_1d[i] = -xi; 2347d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 2348d7b241e6Sjeremylt } 2349e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2350d7b241e6Sjeremylt } 2351d7b241e6Sjeremylt 2352b11c1e72Sjeremylt /** 2353b11c1e72Sjeremylt @brief Construct a Gauss-Legendre-Lobatto quadrature 2354b11c1e72Sjeremylt 2355ca94c3ddSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree `2*Q-3` exactly) 2356ca94c3ddSJeremy L Thompson @param[out] q_ref_1d Array of length `Q` to hold the abscissa on `[-1, 1]` 2357ca94c3ddSJeremy L Thompson @param[out] q_weight_1d Array of length `Q` to hold the weights 2358b11c1e72Sjeremylt 2359b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 2360dfdf5a53Sjeremylt 2361dfdf5a53Sjeremylt @ref Utility 2362b11c1e72Sjeremylt **/ 23632b730f8bSJeremy L Thompson int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2364d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0 * atan(1.0); 23651c66c397SJeremy L Thompson 2366d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 2367d7b241e6Sjeremylt // Set endpoints 23686574a04fSJeremy L Thompson CeedCheck(Q > 1, NULL, CEED_ERROR_DIMENSION, "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q); 2369d7b241e6Sjeremylt wi = 2.0 / ((CeedScalar)(Q * (Q - 1))); 2370d1d35e2fSjeremylt if (q_weight_1d) { 2371d1d35e2fSjeremylt q_weight_1d[0] = wi; 2372d1d35e2fSjeremylt q_weight_1d[Q - 1] = wi; 2373d7b241e6Sjeremylt } 2374d1d35e2fSjeremylt q_ref_1d[0] = -1.0; 2375d1d35e2fSjeremylt q_ref_1d[Q - 1] = 1.0; 2376d7b241e6Sjeremylt // Interior 237792ae7e47SJeremy L Thompson for (CeedInt i = 1; i <= (Q - 1) / 2; i++) { 2378d7b241e6Sjeremylt // Guess 2379d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(i) / (CeedScalar)(Q - 1)); 2380d7b241e6Sjeremylt // Pn(xi) 2381d7b241e6Sjeremylt P0 = 1.0; 2382d7b241e6Sjeremylt P1 = xi; 2383d7b241e6Sjeremylt P2 = 0.0; 238492ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 2385d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2386d7b241e6Sjeremylt P0 = P1; 2387d7b241e6Sjeremylt P1 = P2; 2388d7b241e6Sjeremylt } 2389d7b241e6Sjeremylt // First Newton step 2390d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2391d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2392d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 2393d7b241e6Sjeremylt // Newton to convergence 239492ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(dP2) > 10 * CEED_EPSILON; k++) { 2395d7b241e6Sjeremylt P0 = 1.0; 2396d7b241e6Sjeremylt P1 = xi; 239792ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 2398d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2399d7b241e6Sjeremylt P0 = P1; 2400d7b241e6Sjeremylt P1 = P2; 2401d7b241e6Sjeremylt } 2402d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2403d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2404d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 2405d7b241e6Sjeremylt } 2406d7b241e6Sjeremylt // Save xi, wi 2407d7b241e6Sjeremylt wi = 2.0 / (((CeedScalar)(Q * (Q - 1))) * P2 * P2); 2408d1d35e2fSjeremylt if (q_weight_1d) { 2409d1d35e2fSjeremylt q_weight_1d[i] = wi; 2410d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 2411d7b241e6Sjeremylt } 2412d1d35e2fSjeremylt q_ref_1d[i] = -xi; 2413d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 2414d7b241e6Sjeremylt } 2415e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2416d7b241e6Sjeremylt } 2417d7b241e6Sjeremylt 2418d7b241e6Sjeremylt /// @} 2419