1d275d636SJeremy L Thompson // Copyright (c) 2017-2025, 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 156*4c789ea2SJeremy L Thompson @param[in] tabs Tabs to append before each new line 157ca94c3ddSJeremy L Thompson @param[in] stream Stream to view to, e.g., `stdout` 1587a982d89SJeremy L. Thompson 1597a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1607a982d89SJeremy L. Thompson 1617a982d89SJeremy L. Thompson @ref Developer 1627a982d89SJeremy L. Thompson **/ 163*4c789ea2SJeremy L Thompson static int CeedScalarView(const char *name, const char *fp_fmt, CeedInt m, CeedInt n, const CeedScalar *a, const char *tabs, FILE *stream) { 164edf04919SJeremy L Thompson if (m > 1) { 165*4c789ea2SJeremy L Thompson fprintf(stream, "%s %s:\n", tabs, name); 166edf04919SJeremy L Thompson } else { 167edf04919SJeremy L Thompson char padded_name[12]; 168edf04919SJeremy L Thompson 169edf04919SJeremy L Thompson snprintf(padded_name, 11, "%s:", name); 170*4c789ea2SJeremy L Thompson fprintf(stream, "%s %-10s", tabs, padded_name); 171edf04919SJeremy L Thompson } 17292ae7e47SJeremy L Thompson for (CeedInt i = 0; i < m; i++) { 173*4c789ea2SJeremy L Thompson if (m > 1) fprintf(stream, "%s [%" CeedInt_FMT "]", tabs, i); 1742b730f8bSJeremy 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); 1757a982d89SJeremy L. Thompson fputs("\n", stream); 1767a982d89SJeremy L. Thompson } 177e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1787a982d89SJeremy L. Thompson } 1797a982d89SJeremy L. Thompson 180a76a04e7SJeremy L Thompson /** 181ea61e9acSJeremy L Thompson @brief Create the interpolation and gradient matrices for projection from the nodes of `basis_from` to the nodes of `basis_to`. 182ba59ac12SSebastian Grimberg 18315ad3917SSebastian Grimberg The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization. 184ca94c3ddSJeremy 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. 18515ad3917SSebastian Grimberg 186ba59ac12SSebastian Grimberg Note: `basis_from` and `basis_to` must have compatible quadrature spaces. 187a76a04e7SJeremy L Thompson 188ca94c3ddSJeremy L Thompson @param[in] basis_from `CeedBasis` to project from 189ca94c3ddSJeremy L Thompson @param[in] basis_to `CeedBasis` to project to 190ca94c3ddSJeremy L Thompson @param[out] interp_project Address of the variable where the newly created interpolation matrix will be stored 191ca94c3ddSJeremy L Thompson @param[out] grad_project Address of the variable where the newly created gradient matrix will be stored 192a76a04e7SJeremy L Thompson 193a76a04e7SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 194a76a04e7SJeremy L Thompson 195a76a04e7SJeremy L Thompson @ref Developer 196a76a04e7SJeremy L Thompson **/ 1972b730f8bSJeremy L Thompson static int CeedBasisCreateProjectionMatrices(CeedBasis basis_from, CeedBasis basis_to, CeedScalar **interp_project, CeedScalar **grad_project) { 198e104ad11SJames Wright bool are_both_tensor; 1991c66c397SJeremy L Thompson CeedInt Q, Q_to, Q_from, P_to, P_from; 2001c66c397SJeremy L Thompson 201a76a04e7SJeremy L Thompson // Check for compatible quadrature spaces 2022b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_to, &Q_to)); 2032b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_from, &Q_from)); 2049bc66399SJeremy L Thompson CeedCheck(Q_to == Q_from, CeedBasisReturnCeed(basis_to), CEED_ERROR_DIMENSION, 2053f08121cSJeremy L Thompson "Bases must have compatible quadrature spaces." 20623622755SJeremy L Thompson " 'basis_from' has %" CeedInt_FMT " points and 'basis_to' has %" CeedInt_FMT, 2073f08121cSJeremy L Thompson Q_from, Q_to); 2081c66c397SJeremy L Thompson Q = Q_to; 209a76a04e7SJeremy L Thompson 21014556e63SJeremy L Thompson // Check for matching tensor or non-tensor 211e104ad11SJames Wright { 212e104ad11SJames Wright bool is_tensor_to, is_tensor_from; 213e104ad11SJames Wright 2142b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to)); 2152b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from)); 216e104ad11SJames Wright are_both_tensor = is_tensor_to && is_tensor_from; 217e104ad11SJames Wright } 218e104ad11SJames Wright if (are_both_tensor) { 2192b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_to)); 2202b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_from)); 2212b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis_from, &Q)); 2226574a04fSJeremy L Thompson } else { 2232b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_to, &P_to)); 2242b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_from, &P_from)); 225a76a04e7SJeremy L Thompson } 226a76a04e7SJeremy L Thompson 22715ad3917SSebastian Grimberg // Check for matching FE space 22815ad3917SSebastian Grimberg CeedFESpace fe_space_to, fe_space_from; 2293f08121cSJeremy L Thompson 23015ad3917SSebastian Grimberg CeedCall(CeedBasisGetFESpace(basis_to, &fe_space_to)); 23115ad3917SSebastian Grimberg CeedCall(CeedBasisGetFESpace(basis_from, &fe_space_from)); 2329bc66399SJeremy L Thompson CeedCheck(fe_space_to == fe_space_from, CeedBasisReturnCeed(basis_to), CEED_ERROR_MINOR, 2333f08121cSJeremy L Thompson "Bases must both be the same FE space type." 2343f08121cSJeremy L Thompson " 'basis_from' is a %s and 'basis_to' is a %s", 2353f08121cSJeremy L Thompson CeedFESpaces[fe_space_from], CeedFESpaces[fe_space_to]); 23615ad3917SSebastian Grimberg 23714556e63SJeremy L Thompson // Get source matrices 23815ad3917SSebastian Grimberg CeedInt dim, q_comp = 1; 2392247a93fSRezgar Shakeri CeedScalar *interp_to_inv, *interp_from; 2401c66c397SJeremy L Thompson const CeedScalar *interp_to_source = NULL, *interp_from_source = NULL, *grad_from_source = NULL; 2411c66c397SJeremy L Thompson 242b3ed00e5SJames Wright CeedCall(CeedBasisGetDimension(basis_from, &dim)); 243e104ad11SJames Wright if (are_both_tensor) { 2442b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis_to, &interp_to_source)); 2452b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis_from, &interp_from_source)); 246a76a04e7SJeremy L Thompson } else { 24715ad3917SSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis_from, CEED_EVAL_INTERP, &q_comp)); 2482b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis_to, &interp_to_source)); 2492b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis_from, &interp_from_source)); 25015ad3917SSebastian Grimberg } 25115ad3917SSebastian Grimberg CeedCall(CeedMalloc(Q * P_from * q_comp, &interp_from)); 25215ad3917SSebastian Grimberg CeedCall(CeedCalloc(P_to * P_from, interp_project)); 25315ad3917SSebastian Grimberg 25415ad3917SSebastian Grimberg // `grad_project = interp_to^+ * grad_from` is computed for the H^1 space case so the 255de05fbb2SSebastian Grimberg // projection basis will have a gradient operation (allocated even if not H^1 for the 256de05fbb2SSebastian Grimberg // basis construction later on) 25715ad3917SSebastian Grimberg if (fe_space_to == CEED_FE_SPACE_H1) { 258e104ad11SJames Wright if (are_both_tensor) { 25915ad3917SSebastian Grimberg CeedCall(CeedBasisGetGrad1D(basis_from, &grad_from_source)); 26015ad3917SSebastian Grimberg } else { 2612b730f8bSJeremy L Thompson CeedCall(CeedBasisGetGrad(basis_from, &grad_from_source)); 262a76a04e7SJeremy L Thompson } 263de05fbb2SSebastian Grimberg } 264e104ad11SJames Wright CeedCall(CeedCalloc(P_to * P_from * (are_both_tensor ? 1 : dim), grad_project)); 26515ad3917SSebastian Grimberg 2662247a93fSRezgar Shakeri // Compute interp_to^+, pseudoinverse of interp_to 2672247a93fSRezgar Shakeri CeedCall(CeedCalloc(Q * q_comp * P_to, &interp_to_inv)); 2689bc66399SJeremy L Thompson CeedCall(CeedMatrixPseudoinverse(CeedBasisReturnCeed(basis_to), interp_to_source, Q * q_comp, P_to, interp_to_inv)); 26914556e63SJeremy L Thompson // Build matrices 270e104ad11SJames Wright CeedInt num_matrices = 1 + (fe_space_to == CEED_FE_SPACE_H1) * (are_both_tensor ? 1 : dim); 27114556e63SJeremy L Thompson CeedScalar *input_from[num_matrices], *output_project[num_matrices]; 2721c66c397SJeremy L Thompson 27314556e63SJeremy L Thompson input_from[0] = (CeedScalar *)interp_from_source; 27414556e63SJeremy L Thompson output_project[0] = *interp_project; 27514556e63SJeremy L Thompson for (CeedInt m = 1; m < num_matrices; m++) { 27614556e63SJeremy L Thompson input_from[m] = (CeedScalar *)&grad_from_source[(m - 1) * Q * P_from]; 27702af4036SJeremy L Thompson output_project[m] = &((*grad_project)[(m - 1) * P_to * P_from]); 27814556e63SJeremy L Thompson } 27914556e63SJeremy L Thompson for (CeedInt m = 0; m < num_matrices; m++) { 2802247a93fSRezgar Shakeri // output_project = interp_to^+ * interp_from 28115ad3917SSebastian Grimberg memcpy(interp_from, input_from[m], Q * P_from * q_comp * sizeof(input_from[m][0])); 2829bc66399SJeremy L Thompson CeedCall(CeedMatrixMatrixMultiply(CeedBasisReturnCeed(basis_to), interp_to_inv, input_from[m], output_project[m], P_to, P_from, Q * q_comp)); 2832247a93fSRezgar Shakeri // Round zero to machine precision 2842247a93fSRezgar Shakeri for (CeedInt i = 0; i < P_to * P_from; i++) { 2852247a93fSRezgar Shakeri if (fabs(output_project[m][i]) < 10 * CEED_EPSILON) output_project[m][i] = 0.0; 286a76a04e7SJeremy L Thompson } 28714556e63SJeremy L Thompson } 28814556e63SJeremy L Thompson 28914556e63SJeremy L Thompson // Cleanup 2902247a93fSRezgar Shakeri CeedCall(CeedFree(&interp_to_inv)); 2912b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_from)); 292a76a04e7SJeremy L Thompson return CEED_ERROR_SUCCESS; 293a76a04e7SJeremy L Thompson } 294a76a04e7SJeremy L Thompson 2950b31fde2SJeremy L Thompson /** 2966ab8e59fSJames Wright @brief Check input vector dimensions for CeedBasisApply[Add] 2976ab8e59fSJames Wright 2986ab8e59fSJames Wright @param[in] basis `CeedBasis` to evaluate 2996ab8e59fSJames Wright @param[in] num_elem The number of elements to apply the basis evaluation to; 3006ab8e59fSJames Wright the backend will specify the ordering in @ref CeedElemRestrictionCreate() 3016ab8e59fSJames Wright @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points; 3026ab8e59fSJames Wright @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes 3036ab8e59fSJames Wright @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 3046ab8e59fSJames Wright @ref CEED_EVAL_INTERP to use interpolated values, 3056ab8e59fSJames Wright @ref CEED_EVAL_GRAD to use gradients, 3066ab8e59fSJames Wright @ref CEED_EVAL_DIV to use divergence, 3076ab8e59fSJames Wright @ref CEED_EVAL_CURL to use curl, 3086ab8e59fSJames Wright @ref CEED_EVAL_WEIGHT to use quadrature weights 3096ab8e59fSJames Wright @param[in] u Input `CeedVector` 3106ab8e59fSJames Wright @param[out] v Output `CeedVector` 3116ab8e59fSJames Wright 3126ab8e59fSJames Wright @return An error code: 0 - success, otherwise - failure 3136ab8e59fSJames Wright 3146ab8e59fSJames Wright @ref Developer 3156ab8e59fSJames Wright **/ 3166ab8e59fSJames Wright static int CeedBasisApplyCheckDims(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 3176ab8e59fSJames Wright CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 3186ab8e59fSJames Wright CeedSize u_length = 0, v_length; 3196ab8e59fSJames Wright 3206ab8e59fSJames Wright CeedCall(CeedBasisGetDimension(basis, &dim)); 3216ab8e59fSJames Wright CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 3226ab8e59fSJames Wright CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 3236ab8e59fSJames Wright CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 3246ab8e59fSJames Wright CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 3256ab8e59fSJames Wright CeedCall(CeedVectorGetLength(v, &v_length)); 3266ab8e59fSJames Wright if (u) CeedCall(CeedVectorGetLength(u, &u_length)); 3276ab8e59fSJames Wright 3286ab8e59fSJames Wright // Check vector lengths to prevent out of bounds issues 3296ab8e59fSJames Wright bool has_good_dims = true; 3306ab8e59fSJames Wright switch (eval_mode) { 3316ab8e59fSJames Wright case CEED_EVAL_NONE: 3326ab8e59fSJames Wright case CEED_EVAL_INTERP: 3336ab8e59fSJames Wright case CEED_EVAL_GRAD: 3346ab8e59fSJames Wright case CEED_EVAL_DIV: 3356ab8e59fSJames Wright case CEED_EVAL_CURL: 3366ab8e59fSJames Wright has_good_dims = ((t_mode == CEED_TRANSPOSE && u_length >= (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_qpts * (CeedSize)q_comp && 3376ab8e59fSJames Wright v_length >= (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_nodes) || 3386ab8e59fSJames Wright (t_mode == CEED_NOTRANSPOSE && v_length >= (CeedSize)num_elem * (CeedSize)num_qpts * (CeedSize)num_comp * (CeedSize)q_comp && 3396ab8e59fSJames Wright u_length >= (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_nodes)); 3406ab8e59fSJames Wright break; 3416ab8e59fSJames Wright case CEED_EVAL_WEIGHT: 3426ab8e59fSJames Wright has_good_dims = v_length >= (CeedSize)num_elem * (CeedSize)num_qpts; 3436ab8e59fSJames Wright break; 3446ab8e59fSJames Wright } 3456ab8e59fSJames Wright CeedCheck(has_good_dims, CeedBasisReturnCeed(basis), CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 3466ab8e59fSJames Wright return CEED_ERROR_SUCCESS; 3476ab8e59fSJames Wright } 3486ab8e59fSJames Wright 3496ab8e59fSJames Wright /** 3500b31fde2SJeremy L Thompson @brief Check input vector dimensions for CeedBasisApply[Add]AtPoints 3510b31fde2SJeremy L Thompson 3520b31fde2SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 3530b31fde2SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 3540b31fde2SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 3550b31fde2SJeremy L Thompson @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 3560b31fde2SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 3570b31fde2SJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes 3580b31fde2SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 3590b31fde2SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 3600b31fde2SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 3610b31fde2SJeremy L Thompson @param[in] x_ref `CeedVector` holding reference coordinates of each point 3620b31fde2SJeremy L Thompson @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 3630b31fde2SJeremy L Thompson @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 3640b31fde2SJeremy L Thompson 3650b31fde2SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3660b31fde2SJeremy L Thompson 3670b31fde2SJeremy L Thompson @ref Developer 3680b31fde2SJeremy L Thompson **/ 3690b31fde2SJeremy L Thompson static int CeedBasisApplyAtPointsCheckDims(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, 3700b31fde2SJeremy L Thompson CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) { 3710b31fde2SJeremy L Thompson CeedInt dim, num_comp, num_q_comp, num_nodes, P_1d = 1, Q_1d = 1, total_num_points = 0; 3720b31fde2SJeremy L Thompson CeedSize x_length = 0, u_length = 0, v_length; 3730b31fde2SJeremy L Thompson 3740b31fde2SJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 3750b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 3760b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 3770b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 3780b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &num_q_comp)); 3790b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 3800b31fde2SJeremy L Thompson CeedCall(CeedVectorGetLength(v, &v_length)); 3810b31fde2SJeremy L Thompson if (x_ref != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(x_ref, &x_length)); 3820b31fde2SJeremy L Thompson if (u != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(u, &u_length)); 3830b31fde2SJeremy L Thompson 3840b31fde2SJeremy L Thompson // Check compatibility coordinates vector 3850b31fde2SJeremy L Thompson for (CeedInt i = 0; i < num_elem; i++) total_num_points += num_points[i]; 3869bc66399SJeremy L Thompson CeedCheck((x_length >= (CeedSize)total_num_points * (CeedSize)dim) || (eval_mode == CEED_EVAL_WEIGHT), CeedBasisReturnCeed(basis), 3879bc66399SJeremy L Thompson CEED_ERROR_DIMENSION, 3880b31fde2SJeremy L Thompson "Length of reference coordinate vector incompatible with basis dimension and number of points." 3890b31fde2SJeremy L Thompson " Found reference coordinate vector of length %" CeedSize_FMT ", not of length %" CeedSize_FMT ".", 39019a04db8SJeremy L Thompson x_length, (CeedSize)total_num_points * (CeedSize)dim); 3910b31fde2SJeremy L Thompson 3920b31fde2SJeremy L Thompson // Check CEED_EVAL_WEIGHT only on CEED_NOTRANSPOSE 3939bc66399SJeremy L Thompson CeedCheck(eval_mode != CEED_EVAL_WEIGHT || t_mode == CEED_NOTRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, 3940b31fde2SJeremy L Thompson "CEED_EVAL_WEIGHT only supported with CEED_NOTRANSPOSE"); 3950b31fde2SJeremy L Thompson 3960b31fde2SJeremy L Thompson // Check vector lengths to prevent out of bounds issues 3970b31fde2SJeremy L Thompson bool has_good_dims = true; 3980b31fde2SJeremy L Thompson switch (eval_mode) { 3990b31fde2SJeremy L Thompson case CEED_EVAL_INTERP: 40019a04db8SJeremy L Thompson has_good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= (CeedSize)total_num_points * (CeedSize)num_q_comp || 40119a04db8SJeremy L Thompson v_length >= (CeedSize)num_elem * (CeedSize)num_nodes * (CeedSize)num_comp)) || 40219a04db8SJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && (v_length >= (CeedSize)total_num_points * (CeedSize)num_q_comp || 40319a04db8SJeremy L Thompson u_length >= (CeedSize)num_elem * (CeedSize)num_nodes * (CeedSize)num_comp))); 4040b31fde2SJeremy L Thompson break; 4050b31fde2SJeremy L Thompson case CEED_EVAL_GRAD: 40619a04db8SJeremy L Thompson has_good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= (CeedSize)total_num_points * (CeedSize)num_q_comp * (CeedSize)dim || 40719a04db8SJeremy L Thompson v_length >= (CeedSize)num_elem * (CeedSize)num_nodes * (CeedSize)num_comp)) || 40819a04db8SJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && (v_length >= (CeedSize)total_num_points * (CeedSize)num_q_comp * (CeedSize)dim || 40919a04db8SJeremy L Thompson u_length >= (CeedSize)num_elem * (CeedSize)num_nodes * (CeedSize)num_comp))); 4100b31fde2SJeremy L Thompson break; 4110b31fde2SJeremy L Thompson case CEED_EVAL_WEIGHT: 4120b31fde2SJeremy L Thompson has_good_dims = t_mode == CEED_NOTRANSPOSE && (v_length >= total_num_points); 4130b31fde2SJeremy L Thompson break; 4140b31fde2SJeremy L Thompson // LCOV_EXCL_START 4150b31fde2SJeremy L Thompson case CEED_EVAL_NONE: 4160b31fde2SJeremy L Thompson case CEED_EVAL_DIV: 4170b31fde2SJeremy L Thompson case CEED_EVAL_CURL: 4189bc66399SJeremy L Thompson return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points not supported for %s", 4199bc66399SJeremy L Thompson CeedEvalModes[eval_mode]); 4200b31fde2SJeremy L Thompson // LCOV_EXCL_STOP 4210b31fde2SJeremy L Thompson } 4229bc66399SJeremy L Thompson CeedCheck(has_good_dims, CeedBasisReturnCeed(basis), CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 4230b31fde2SJeremy L Thompson return CEED_ERROR_SUCCESS; 4240b31fde2SJeremy L Thompson } 4250b31fde2SJeremy L Thompson 4260b31fde2SJeremy L Thompson /** 4270b31fde2SJeremy L Thompson @brief Default implimentation to apply basis evaluation from nodes to arbitrary points 4280b31fde2SJeremy L Thompson 4290b31fde2SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 4300b31fde2SJeremy L Thompson @param[in] apply_add Sum result into target vector or overwrite 4310b31fde2SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 4320b31fde2SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 4330b31fde2SJeremy L Thompson @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 4340b31fde2SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 4350b31fde2SJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes 4360b31fde2SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 4370b31fde2SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 4380b31fde2SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 4390b31fde2SJeremy L Thompson @param[in] x_ref `CeedVector` holding reference coordinates of each point 4400b31fde2SJeremy L Thompson @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 4410b31fde2SJeremy L Thompson @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 4420b31fde2SJeremy L Thompson 4430b31fde2SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4440b31fde2SJeremy L Thompson 4450b31fde2SJeremy L Thompson @ref Developer 4460b31fde2SJeremy L Thompson **/ 4470b31fde2SJeremy L Thompson static int CeedBasisApplyAtPoints_Core(CeedBasis basis, bool apply_add, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, 4480b31fde2SJeremy L Thompson CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) { 4490b31fde2SJeremy L Thompson CeedInt dim, num_comp, P_1d = 1, Q_1d = 1, total_num_points = num_points[0]; 4500b31fde2SJeremy L Thompson 4510b31fde2SJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 4520b31fde2SJeremy L Thompson // Inserting check because clang-tidy doesn't understand this cannot occur 4539bc66399SJeremy L Thompson CeedCheck(dim > 0, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Malformed CeedBasis, dim > 0 is required"); 4540b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 4550b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 4560b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 4570b31fde2SJeremy L Thompson 4580b31fde2SJeremy L Thompson // Default implementation 4590b31fde2SJeremy L Thompson { 4600b31fde2SJeremy L Thompson bool is_tensor_basis; 4610b31fde2SJeremy L Thompson 4620b31fde2SJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 4639bc66399SJeremy L Thompson CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, 4649bc66399SJeremy L Thompson "Evaluation at arbitrary points only supported for tensor product bases"); 4650b31fde2SJeremy L Thompson } 4669bc66399SJeremy L Thompson CeedCheck(num_elem == 1, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, 4679bc66399SJeremy L Thompson "Evaluation at arbitrary points only supported for a single element at a time"); 4680b31fde2SJeremy L Thompson if (eval_mode == CEED_EVAL_WEIGHT) { 4690b31fde2SJeremy L Thompson CeedCall(CeedVectorSetValue(v, 1.0)); 4700b31fde2SJeremy L Thompson return CEED_ERROR_SUCCESS; 4710b31fde2SJeremy L Thompson } 4720b31fde2SJeremy L Thompson if (!basis->basis_chebyshev) { 4730b31fde2SJeremy L Thompson // Build basis mapping from nodes to Chebyshev coefficients 4740b31fde2SJeremy L Thompson CeedScalar *chebyshev_interp_1d, *chebyshev_grad_1d, *chebyshev_q_weight_1d; 4750b31fde2SJeremy L Thompson const CeedScalar *q_ref_1d; 4769bc66399SJeremy L Thompson Ceed ceed; 4770b31fde2SJeremy L Thompson 4780b31fde2SJeremy L Thompson CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 4790b31fde2SJeremy L Thompson CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_grad_1d)); 4800b31fde2SJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &chebyshev_q_weight_1d)); 4810b31fde2SJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 4820b31fde2SJeremy L Thompson CeedCall(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d)); 4830b31fde2SJeremy L Thompson 4849bc66399SJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 4850b31fde2SJeremy L Thompson CeedCall(CeedVectorCreate(ceed, num_comp * CeedIntPow(Q_1d, dim), &basis->vec_chebyshev)); 4860b31fde2SJeremy 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, 4870b31fde2SJeremy L Thompson &basis->basis_chebyshev)); 4880b31fde2SJeremy L Thompson 4890b31fde2SJeremy L Thompson // Cleanup 4900b31fde2SJeremy L Thompson CeedCall(CeedFree(&chebyshev_interp_1d)); 4910b31fde2SJeremy L Thompson CeedCall(CeedFree(&chebyshev_grad_1d)); 4920b31fde2SJeremy L Thompson CeedCall(CeedFree(&chebyshev_q_weight_1d)); 4939bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 4940b31fde2SJeremy L Thompson } 4950b31fde2SJeremy L Thompson 4960b31fde2SJeremy L Thompson // Create TensorContract object if needed, such as a basis from the GPU backends 4970b31fde2SJeremy L Thompson if (!basis->contract) { 4980b31fde2SJeremy L Thompson Ceed ceed_ref; 4990b31fde2SJeremy L Thompson CeedBasis basis_ref = NULL; 5000b31fde2SJeremy L Thompson 5010b31fde2SJeremy L Thompson CeedCall(CeedInit("/cpu/self", &ceed_ref)); 5020b31fde2SJeremy L Thompson // Only need matching tensor contraction dimensions, any type of basis will work 5030b31fde2SJeremy L Thompson CeedCall(CeedBasisCreateTensorH1Lagrange(ceed_ref, dim, num_comp, P_1d, Q_1d, CEED_GAUSS, &basis_ref)); 5040b31fde2SJeremy L Thompson // Note - clang-tidy doesn't know basis_ref->contract must be valid here 5059bc66399SJeremy L Thompson CeedCheck(basis_ref && basis_ref->contract, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, 5069bc66399SJeremy L Thompson "Reference CPU ceed failed to create a tensor contraction object"); 5070b31fde2SJeremy L Thompson CeedCall(CeedTensorContractReferenceCopy(basis_ref->contract, &basis->contract)); 5080b31fde2SJeremy L Thompson CeedCall(CeedBasisDestroy(&basis_ref)); 5090b31fde2SJeremy L Thompson CeedCall(CeedDestroy(&ceed_ref)); 5100b31fde2SJeremy L Thompson } 5110b31fde2SJeremy L Thompson 5120b31fde2SJeremy L Thompson // Basis evaluation 5130b31fde2SJeremy L Thompson switch (t_mode) { 5140b31fde2SJeremy L Thompson case CEED_NOTRANSPOSE: { 5150b31fde2SJeremy L Thompson // Nodes to arbitrary points 5160b31fde2SJeremy L Thompson CeedScalar *v_array; 5170b31fde2SJeremy L Thompson const CeedScalar *chebyshev_coeffs, *x_array_read; 5180b31fde2SJeremy L Thompson 5190b31fde2SJeremy L Thompson // -- Interpolate to Chebyshev coefficients 5200b31fde2SJeremy L Thompson CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, basis->vec_chebyshev)); 5210b31fde2SJeremy L Thompson 5220b31fde2SJeremy L Thompson // -- Evaluate Chebyshev polynomials at arbitrary points 5230b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs)); 5240b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read)); 5250b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(v, CEED_MEM_HOST, &v_array)); 5260b31fde2SJeremy L Thompson switch (eval_mode) { 5270b31fde2SJeremy L Thompson case CEED_EVAL_INTERP: { 5280b31fde2SJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 5290b31fde2SJeremy L Thompson 5300b31fde2SJeremy L Thompson // ---- Values at point 5310b31fde2SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 5320b31fde2SJeremy L Thompson CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1; 5330b31fde2SJeremy L Thompson 5340b31fde2SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 5350b31fde2SJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 5360b31fde2SJeremy L Thompson CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 5370b31fde2SJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false, 5380b31fde2SJeremy L Thompson d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2])); 5390b31fde2SJeremy L Thompson pre /= Q_1d; 5400b31fde2SJeremy L Thompson post *= 1; 5410b31fde2SJeremy L Thompson } 5420b31fde2SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) v_array[c * total_num_points + p] = tmp[dim % 2][c]; 5430b31fde2SJeremy L Thompson } 5440b31fde2SJeremy L Thompson break; 5450b31fde2SJeremy L Thompson } 5460b31fde2SJeremy L Thompson case CEED_EVAL_GRAD: { 5470b31fde2SJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 5480b31fde2SJeremy L Thompson 5490b31fde2SJeremy L Thompson // ---- Values at point 5500b31fde2SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 5510b31fde2SJeremy L Thompson // Dim**2 contractions, apply grad when pass == dim 5520b31fde2SJeremy L Thompson for (CeedInt pass = 0; pass < dim; pass++) { 5530b31fde2SJeremy L Thompson CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1; 5540b31fde2SJeremy L Thompson 5550b31fde2SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 5560b31fde2SJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 5570b31fde2SJeremy L Thompson if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 5580b31fde2SJeremy L Thompson else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 5590b31fde2SJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false, 5600b31fde2SJeremy L Thompson d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2])); 5610b31fde2SJeremy L Thompson pre /= Q_1d; 5620b31fde2SJeremy L Thompson post *= 1; 5630b31fde2SJeremy L Thompson } 5640b31fde2SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) v_array[(pass * num_comp + c) * total_num_points + p] = tmp[dim % 2][c]; 5650b31fde2SJeremy L Thompson } 5660b31fde2SJeremy L Thompson } 5670b31fde2SJeremy L Thompson break; 5680b31fde2SJeremy L Thompson } 5690b31fde2SJeremy L Thompson default: 5700b31fde2SJeremy L Thompson // Nothing to do, excluded above 5710b31fde2SJeremy L Thompson break; 5720b31fde2SJeremy L Thompson } 5730b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(basis->vec_chebyshev, &chebyshev_coeffs)); 5740b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read)); 5750b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArray(v, &v_array)); 5760b31fde2SJeremy L Thompson break; 5770b31fde2SJeremy L Thompson } 5780b31fde2SJeremy L Thompson case CEED_TRANSPOSE: { 5790b31fde2SJeremy L Thompson // Note: No switch on e_mode here because only CEED_EVAL_INTERP is supported at this time 5800b31fde2SJeremy L Thompson // Arbitrary points to nodes 5810b31fde2SJeremy L Thompson CeedScalar *chebyshev_coeffs; 5820b31fde2SJeremy L Thompson const CeedScalar *u_array, *x_array_read; 5830b31fde2SJeremy L Thompson 5840b31fde2SJeremy L Thompson // -- Transpose of evaluation of Chebyshev polynomials at arbitrary points 5850b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs)); 5860b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read)); 5870b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array)); 5880b31fde2SJeremy L Thompson 5890b31fde2SJeremy L Thompson switch (eval_mode) { 5900b31fde2SJeremy L Thompson case CEED_EVAL_INTERP: { 5910b31fde2SJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 5920b31fde2SJeremy L Thompson 5930b31fde2SJeremy L Thompson // ---- Values at point 5940b31fde2SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 5950b31fde2SJeremy L Thompson CeedInt pre = num_comp * 1, post = 1; 5960b31fde2SJeremy L Thompson 5970b31fde2SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[c * total_num_points + p]; 5980b31fde2SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 5990b31fde2SJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 6000b31fde2SJeremy L Thompson CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 6010b31fde2SJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, p > 0 && d == (dim - 1), tmp[d % 2], 6020b31fde2SJeremy L Thompson d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2])); 6030b31fde2SJeremy L Thompson pre /= 1; 6040b31fde2SJeremy L Thompson post *= Q_1d; 6050b31fde2SJeremy L Thompson } 6060b31fde2SJeremy L Thompson } 6070b31fde2SJeremy L Thompson break; 6080b31fde2SJeremy L Thompson } 6090b31fde2SJeremy L Thompson case CEED_EVAL_GRAD: { 6100b31fde2SJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 6110b31fde2SJeremy L Thompson 6120b31fde2SJeremy L Thompson // ---- Values at point 6130b31fde2SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 6140b31fde2SJeremy L Thompson // Dim**2 contractions, apply grad when pass == dim 6150b31fde2SJeremy L Thompson for (CeedInt pass = 0; pass < dim; pass++) { 6160b31fde2SJeremy L Thompson CeedInt pre = num_comp * 1, post = 1; 6170b31fde2SJeremy L Thompson 6180b31fde2SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[(pass * num_comp + c) * total_num_points + p]; 6190b31fde2SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 6200b31fde2SJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 6210b31fde2SJeremy L Thompson if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 6220b31fde2SJeremy L Thompson else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 6230b31fde2SJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, 6240b31fde2SJeremy L Thompson (p > 0 || (p == 0 && pass > 0)) && d == (dim - 1), tmp[d % 2], 6250b31fde2SJeremy L Thompson d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2])); 6260b31fde2SJeremy L Thompson pre /= 1; 6270b31fde2SJeremy L Thompson post *= Q_1d; 6280b31fde2SJeremy L Thompson } 6290b31fde2SJeremy L Thompson } 6300b31fde2SJeremy L Thompson } 6310b31fde2SJeremy L Thompson break; 6320b31fde2SJeremy L Thompson } 6330b31fde2SJeremy L Thompson default: 6340b31fde2SJeremy L Thompson // Nothing to do, excluded above 6350b31fde2SJeremy L Thompson break; 6360b31fde2SJeremy L Thompson } 6370b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArray(basis->vec_chebyshev, &chebyshev_coeffs)); 6380b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read)); 6390b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(u, &u_array)); 6400b31fde2SJeremy L Thompson 6410b31fde2SJeremy L Thompson // -- Interpolate transpose from Chebyshev coefficients 6420b31fde2SJeremy L Thompson if (apply_add) CeedCall(CeedBasisApplyAdd(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v)); 6430b31fde2SJeremy L Thompson else CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v)); 6440b31fde2SJeremy L Thompson break; 6450b31fde2SJeremy L Thompson } 6460b31fde2SJeremy L Thompson } 6470b31fde2SJeremy L Thompson return CEED_ERROR_SUCCESS; 6480b31fde2SJeremy L Thompson } 6490b31fde2SJeremy L Thompson 6507a982d89SJeremy L. Thompson /// @} 6517a982d89SJeremy L. Thompson 6527a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 6537a982d89SJeremy L. Thompson /// Ceed Backend API 6547a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 6557a982d89SJeremy L. Thompson /// @addtogroup CeedBasisBackend 6567a982d89SJeremy L. Thompson /// @{ 6577a982d89SJeremy L. Thompson 6587a982d89SJeremy L. Thompson /** 659fda26546SJeremy L Thompson @brief Fallback to a reference implementation for a non tensor-product basis for \f$H^1\f$ discretizations. 660fda26546SJeremy L Thompson This function may only be called inside of a backend `BasisCreateH1` function. 661fda26546SJeremy L Thompson This is used by a backend when the specific parameters for a `CeedBasis` exceed the backend's support, such as 662fda26546SJeremy L Thompson when a `interp` and `grad` matrices require too many bytes to fit into shared memory on a GPU. 663fda26546SJeremy L Thompson 664fda26546SJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 665fda26546SJeremy L Thompson @param[in] topo Topology of element, e.g. hypercube, simplex, etc 666fda26546SJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 667fda26546SJeremy L Thompson @param[in] num_nodes Total number of nodes 668fda26546SJeremy L Thompson @param[in] num_qpts Total number of quadrature points 669fda26546SJeremy L Thompson @param[in] interp Row-major (`num_qpts * num_nodes`) matrix expressing the values of nodal basis functions at quadrature points 670fda26546SJeremy L Thompson @param[in] grad Row-major (`dim * num_qpts * num_nodes`) matrix expressing derivatives of nodal basis functions at quadrature points 671fda26546SJeremy L Thompson @param[in] q_ref Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element 672fda26546SJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 673fda26546SJeremy L Thompson @param[out] basis Newly created `CeedBasis` 674fda26546SJeremy L Thompson 675fda26546SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 676fda26546SJeremy L Thompson 677fda26546SJeremy L Thompson @ref User 678fda26546SJeremy L Thompson **/ 679fda26546SJeremy L Thompson int CeedBasisCreateH1Fallback(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 680fda26546SJeremy L Thompson const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) { 681fda26546SJeremy L Thompson CeedInt P = num_nodes, Q = num_qpts, dim = 0; 682fda26546SJeremy L Thompson Ceed delegate; 683fda26546SJeremy L Thompson 684fda26546SJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 685fda26546SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateH1"); 686fda26546SJeremy L Thompson 687fda26546SJeremy L Thompson CeedCall(CeedReferenceCopy(delegate, &(basis)->ceed)); 688fda26546SJeremy L Thompson CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 689fda26546SJeremy L Thompson CeedCall(delegate->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, basis)); 690fda26546SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 691fda26546SJeremy L Thompson return CEED_ERROR_SUCCESS; 692fda26546SJeremy L Thompson } 693fda26546SJeremy L Thompson 694fda26546SJeremy L Thompson /** 695ca94c3ddSJeremy L Thompson @brief Return collocated gradient matrix 6967a982d89SJeremy L. Thompson 697ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 698ca94c3ddSJeremy L Thompson @param[out] collo_grad_1d Row-major (`Q_1d * Q_1d`) matrix expressing derivatives of basis functions at quadrature points 6997a982d89SJeremy L. Thompson 7007a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 7017a982d89SJeremy L. Thompson 7027a982d89SJeremy L. Thompson @ref Backend 7037a982d89SJeremy L. Thompson **/ 704d1d35e2fSjeremylt int CeedBasisGetCollocatedGrad(CeedBasis basis, CeedScalar *collo_grad_1d) { 7057a982d89SJeremy L. Thompson Ceed ceed; 7062247a93fSRezgar Shakeri CeedInt P_1d, Q_1d; 7072247a93fSRezgar Shakeri CeedScalar *interp_1d_pinv; 7081203703bSJeremy L Thompson const CeedScalar *grad_1d, *interp_1d; 7091203703bSJeremy L Thompson 710ea61e9acSJeremy 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. 7112247a93fSRezgar Shakeri CeedCall(CeedBasisGetCeed(basis, &ceed)); 7122247a93fSRezgar Shakeri CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 7132247a93fSRezgar Shakeri CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 7147a982d89SJeremy L. Thompson 7152247a93fSRezgar Shakeri // Compute interp_1d^+, pseudoinverse of interp_1d 7162247a93fSRezgar Shakeri CeedCall(CeedCalloc(P_1d * Q_1d, &interp_1d_pinv)); 7171203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 7181203703bSJeremy L Thompson CeedCall(CeedMatrixPseudoinverse(ceed, interp_1d, Q_1d, P_1d, interp_1d_pinv)); 7191203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 7201203703bSJeremy L Thompson CeedCall(CeedMatrixMatrixMultiply(ceed, grad_1d, (const CeedScalar *)interp_1d_pinv, collo_grad_1d, Q_1d, Q_1d, P_1d)); 7217a982d89SJeremy L. Thompson 7222247a93fSRezgar Shakeri CeedCall(CeedFree(&interp_1d_pinv)); 7239bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 724e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7257a982d89SJeremy L. Thompson } 7267a982d89SJeremy L. Thompson 7277a982d89SJeremy L. Thompson /** 728*4c789ea2SJeremy L Thompson @brief Get the number of tabs to indent for @ref CeedBasisView() output 729*4c789ea2SJeremy L Thompson 730*4c789ea2SJeremy L Thompson @param[in] basis `CeedBasis` to get the number of view tabs 731*4c789ea2SJeremy L Thompson @param[out] num_tabs Number of view tabs 732*4c789ea2SJeremy L Thompson 733*4c789ea2SJeremy L Thompson @return Error code: 0 - success, otherwise - failure 734*4c789ea2SJeremy L Thompson 735*4c789ea2SJeremy L Thompson @ref Backend 736*4c789ea2SJeremy L Thompson **/ 737*4c789ea2SJeremy L Thompson int CeedBasisGetNumViewTabs(CeedBasis basis, CeedInt *num_tabs) { 738*4c789ea2SJeremy L Thompson *num_tabs = basis->num_tabs; 739*4c789ea2SJeremy L Thompson return CEED_ERROR_SUCCESS; 740*4c789ea2SJeremy L Thompson } 741*4c789ea2SJeremy L Thompson 742*4c789ea2SJeremy L Thompson /** 743b0cc4569SJeremy L Thompson @brief Return 1D interpolation matrix to Chebyshev polynomial coefficients on quadrature space 744b0cc4569SJeremy L Thompson 745b0cc4569SJeremy L Thompson @param[in] basis `CeedBasis` 746b0cc4569SJeremy L Thompson @param[out] chebyshev_interp_1d Row-major (`P_1d * Q_1d`) matrix interpolating from basis nodes to Chebyshev polynomial coefficients 747b0cc4569SJeremy L Thompson 748b0cc4569SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 749b0cc4569SJeremy L Thompson 750b0cc4569SJeremy L Thompson @ref Backend 751b0cc4569SJeremy L Thompson **/ 752b0cc4569SJeremy L Thompson int CeedBasisGetChebyshevInterp1D(CeedBasis basis, CeedScalar *chebyshev_interp_1d) { 753b0cc4569SJeremy L Thompson CeedInt P_1d, Q_1d; 754b0cc4569SJeremy L Thompson CeedScalar *C, *chebyshev_coeffs_1d_inv; 755b0cc4569SJeremy L Thompson const CeedScalar *interp_1d, *q_ref_1d; 756b0cc4569SJeremy L Thompson Ceed ceed; 757b0cc4569SJeremy L Thompson 758b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 759b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 760b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 761b0cc4569SJeremy L Thompson 762b0cc4569SJeremy L Thompson // Build coefficient matrix 763bd83cbc5SJeremy L Thompson // -- Note: Clang-tidy needs this check 764bd83cbc5SJeremy L Thompson CeedCheck((P_1d > 0) && (Q_1d > 0), ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis dimensions are malformed"); 765b0cc4569SJeremy L Thompson CeedCall(CeedCalloc(Q_1d * Q_1d, &C)); 766b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 767b0cc4569SJeremy L Thompson for (CeedInt i = 0; i < Q_1d; i++) CeedCall(CeedChebyshevPolynomialsAtPoint(q_ref_1d[i], Q_1d, &C[i * Q_1d])); 768b0cc4569SJeremy L Thompson 769b0cc4569SJeremy L Thompson // Compute C^+, pseudoinverse of coefficient matrix 770b0cc4569SJeremy L Thompson CeedCall(CeedCalloc(Q_1d * Q_1d, &chebyshev_coeffs_1d_inv)); 771b0cc4569SJeremy L Thompson CeedCall(CeedMatrixPseudoinverse(ceed, C, Q_1d, Q_1d, chebyshev_coeffs_1d_inv)); 772b0cc4569SJeremy L Thompson 773b0cc4569SJeremy L Thompson // Build mapping from nodes to Chebyshev coefficients 774b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 775b0cc4569SJeremy L Thompson CeedCall(CeedMatrixMatrixMultiply(ceed, chebyshev_coeffs_1d_inv, interp_1d, chebyshev_interp_1d, Q_1d, P_1d, Q_1d)); 776b0cc4569SJeremy L Thompson 777b0cc4569SJeremy L Thompson // Cleanup 778b0cc4569SJeremy L Thompson CeedCall(CeedFree(&C)); 779b0cc4569SJeremy L Thompson CeedCall(CeedFree(&chebyshev_coeffs_1d_inv)); 7809bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 781b0cc4569SJeremy L Thompson return CEED_ERROR_SUCCESS; 782b0cc4569SJeremy L Thompson } 783b0cc4569SJeremy L Thompson 784b0cc4569SJeremy L Thompson /** 785ca94c3ddSJeremy L Thompson @brief Get tensor status for given `CeedBasis` 7867a982d89SJeremy L. Thompson 787ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 788d1d35e2fSjeremylt @param[out] is_tensor Variable to store tensor status 7897a982d89SJeremy L. Thompson 7907a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 7917a982d89SJeremy L. Thompson 7927a982d89SJeremy L. Thompson @ref Backend 7937a982d89SJeremy L. Thompson **/ 794d1d35e2fSjeremylt int CeedBasisIsTensor(CeedBasis basis, bool *is_tensor) { 7956402da51SJeremy L Thompson *is_tensor = basis->is_tensor_basis; 796e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7977a982d89SJeremy L. Thompson } 7987a982d89SJeremy L. Thompson 7997a982d89SJeremy L. Thompson /** 800ca62d558SJeremy L Thompson @brief Determine if given `CeedBasis` has nodes collocated with quadrature points 801ca62d558SJeremy L Thompson 802ca62d558SJeremy L Thompson @param[in] basis `CeedBasis` 803aa4b4a9fSJeremy L Thompson @param[out] is_collocated Variable to store collocated status 804ca62d558SJeremy L Thompson 805ca62d558SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 806ca62d558SJeremy L Thompson 807ca62d558SJeremy L Thompson @ref Backend 808ca62d558SJeremy L Thompson **/ 809ca62d558SJeremy L Thompson int CeedBasisIsCollocated(CeedBasis basis, bool *is_collocated) { 810ca62d558SJeremy L Thompson if (basis->is_tensor_basis && (basis->Q_1d == basis->P_1d)) { 811ca62d558SJeremy L Thompson *is_collocated = true; 812ca62d558SJeremy L Thompson 813ca62d558SJeremy L Thompson for (CeedInt i = 0; i < basis->P_1d; i++) { 814ca62d558SJeremy L Thompson *is_collocated = *is_collocated && (fabs(basis->interp_1d[i + basis->P_1d * i] - 1.0) < 10 * CEED_EPSILON); 815ca62d558SJeremy L Thompson for (CeedInt j = 0; j < basis->Q_1d; j++) { 816ca62d558SJeremy L Thompson if (j != i) *is_collocated = *is_collocated && (fabs(basis->interp_1d[j + basis->P_1d * i]) < 10 * CEED_EPSILON); 817ca62d558SJeremy L Thompson } 818ca62d558SJeremy L Thompson } 819ca62d558SJeremy L Thompson } else { 820ca62d558SJeremy L Thompson *is_collocated = false; 821ca62d558SJeremy L Thompson } 822ca62d558SJeremy L Thompson return CEED_ERROR_SUCCESS; 823ca62d558SJeremy L Thompson } 824ca62d558SJeremy L Thompson 825ca62d558SJeremy L Thompson /** 826ca94c3ddSJeremy L Thompson @brief Get backend data of a `CeedBasis` 8277a982d89SJeremy L. Thompson 828ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 8297a982d89SJeremy L. Thompson @param[out] data Variable to store data 8307a982d89SJeremy L. Thompson 8317a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 8327a982d89SJeremy L. Thompson 8337a982d89SJeremy L. Thompson @ref Backend 8347a982d89SJeremy L. Thompson **/ 835777ff853SJeremy L Thompson int CeedBasisGetData(CeedBasis basis, void *data) { 836777ff853SJeremy L Thompson *(void **)data = basis->data; 837e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 8387a982d89SJeremy L. Thompson } 8397a982d89SJeremy L. Thompson 8407a982d89SJeremy L. Thompson /** 841ca94c3ddSJeremy L Thompson @brief Set backend data of a `CeedBasis` 8427a982d89SJeremy L. Thompson 843ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` 844ea61e9acSJeremy L Thompson @param[in] data Data to set 8457a982d89SJeremy L. Thompson 8467a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 8477a982d89SJeremy L. Thompson 8487a982d89SJeremy L. Thompson @ref Backend 8497a982d89SJeremy L. Thompson **/ 850777ff853SJeremy L Thompson int CeedBasisSetData(CeedBasis basis, void *data) { 851777ff853SJeremy L Thompson basis->data = data; 852e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 8537a982d89SJeremy L. Thompson } 8547a982d89SJeremy L. Thompson 8557a982d89SJeremy L. Thompson /** 856ca94c3ddSJeremy L Thompson @brief Increment the reference counter for a `CeedBasis` 85734359f16Sjeremylt 858ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` to increment the reference counter 85934359f16Sjeremylt 86034359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 86134359f16Sjeremylt 86234359f16Sjeremylt @ref Backend 86334359f16Sjeremylt **/ 8649560d06aSjeremylt int CeedBasisReference(CeedBasis basis) { 86534359f16Sjeremylt basis->ref_count++; 86634359f16Sjeremylt return CEED_ERROR_SUCCESS; 86734359f16Sjeremylt } 86834359f16Sjeremylt 86934359f16Sjeremylt /** 870ca94c3ddSJeremy L Thompson @brief Get number of Q-vector components for given `CeedBasis` 871c4e3f59bSSebastian Grimberg 872ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 873ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 874ca94c3ddSJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 875ca94c3ddSJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 876ca94c3ddSJeremy L Thompson @ref CEED_EVAL_CURL to use curl 877c4e3f59bSSebastian Grimberg @param[out] q_comp Variable to store number of Q-vector components of basis 878c4e3f59bSSebastian Grimberg 879c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 880c4e3f59bSSebastian Grimberg 881c4e3f59bSSebastian Grimberg @ref Backend 882c4e3f59bSSebastian Grimberg **/ 883c4e3f59bSSebastian Grimberg int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode, CeedInt *q_comp) { 8841203703bSJeremy L Thompson CeedInt dim; 8851203703bSJeremy L Thompson 8861203703bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 887c4e3f59bSSebastian Grimberg switch (eval_mode) { 8881203703bSJeremy L Thompson case CEED_EVAL_INTERP: { 8891203703bSJeremy L Thompson CeedFESpace fe_space; 8901203703bSJeremy L Thompson 8911203703bSJeremy L Thompson CeedCall(CeedBasisGetFESpace(basis, &fe_space)); 8921203703bSJeremy L Thompson *q_comp = (fe_space == CEED_FE_SPACE_H1) ? 1 : dim; 8931203703bSJeremy L Thompson } break; 894c4e3f59bSSebastian Grimberg case CEED_EVAL_GRAD: 8951203703bSJeremy L Thompson *q_comp = dim; 896c4e3f59bSSebastian Grimberg break; 897c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 898c4e3f59bSSebastian Grimberg *q_comp = 1; 899c4e3f59bSSebastian Grimberg break; 900c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 9011203703bSJeremy L Thompson *q_comp = (dim < 3) ? 1 : dim; 902c4e3f59bSSebastian Grimberg break; 903c4e3f59bSSebastian Grimberg case CEED_EVAL_NONE: 904c4e3f59bSSebastian Grimberg case CEED_EVAL_WEIGHT: 905352a5e7cSSebastian Grimberg *q_comp = 1; 906c4e3f59bSSebastian Grimberg break; 907c4e3f59bSSebastian Grimberg } 908c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 909c4e3f59bSSebastian Grimberg } 910c4e3f59bSSebastian Grimberg 911c4e3f59bSSebastian Grimberg /** 912ca94c3ddSJeremy L Thompson @brief Estimate number of FLOPs required to apply `CeedBasis` in `t_mode` and `eval_mode` 9136e15d496SJeremy L Thompson 914ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to estimate FLOPs for 915ea61e9acSJeremy L Thompson @param[in] t_mode Apply basis or transpose 916ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CeedEvalMode 9173f919cbcSJeremy L Thompson @param[in] is_at_points Evaluate the basis at points or quadrature points 9183f919cbcSJeremy L Thompson @param[in] num_points Number of points basis is evaluated at 919ea61e9acSJeremy L Thompson @param[out] flops Address of variable to hold FLOPs estimate 9206e15d496SJeremy L Thompson 9216e15d496SJeremy L Thompson @ref Backend 9226e15d496SJeremy L Thompson **/ 9233f919cbcSJeremy L Thompson int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, bool is_at_points, CeedInt num_points, 9243f919cbcSJeremy L Thompson CeedSize *flops) { 9256e15d496SJeremy L Thompson bool is_tensor; 9266e15d496SJeremy L Thompson 9272b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor)); 9283f919cbcSJeremy L Thompson CeedCheck(!is_at_points || is_tensor, CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Can only evaluate tensor-product bases at points"); 9296e15d496SJeremy L Thompson if (is_tensor) { 9306e15d496SJeremy L Thompson CeedInt dim, num_comp, P_1d, Q_1d; 9311c66c397SJeremy L Thompson 9322b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 9332b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 9342b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 9352b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 9366e15d496SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 9372b730f8bSJeremy L Thompson P_1d = Q_1d; 9382b730f8bSJeremy L Thompson Q_1d = P_1d; 9396e15d496SJeremy L Thompson } 9406e15d496SJeremy L Thompson CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim - 1), post = 1; 9413f919cbcSJeremy L Thompson 9426e15d496SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 9436e15d496SJeremy L Thompson tensor_flops += 2 * pre * P_1d * post * Q_1d; 9446e15d496SJeremy L Thompson pre /= P_1d; 9456e15d496SJeremy L Thompson post *= Q_1d; 9466e15d496SJeremy L Thompson } 9473f919cbcSJeremy L Thompson if (is_at_points) { 94852780386SJeremy L Thompson bool is_gpu = false; 94952780386SJeremy L Thompson 95052780386SJeremy L Thompson { 95152780386SJeremy L Thompson CeedMemType mem_type; 95252780386SJeremy L Thompson 95352780386SJeremy L Thompson CeedCall(CeedGetPreferredMemType(CeedBasisReturnCeed(basis), &mem_type)); 95452780386SJeremy L Thompson is_gpu = mem_type == CEED_MEM_DEVICE; 95552780386SJeremy L Thompson } 95652780386SJeremy L Thompson 9573f919cbcSJeremy L Thompson CeedInt chebyshev_flops = (Q_1d - 2) * 3 + 1, d_chebyshev_flops = (Q_1d - 2) * 8 + 1; 9583f919cbcSJeremy L Thompson CeedInt point_tensor_flops = 0, pre = CeedIntPow(Q_1d, dim - 1), post = 1; 9593f919cbcSJeremy L Thompson 9603f919cbcSJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 9613f919cbcSJeremy L Thompson point_tensor_flops += 2 * pre * Q_1d * post * 1; 9623f919cbcSJeremy L Thompson pre /= P_1d; 9633f919cbcSJeremy L Thompson post *= Q_1d; 9643f919cbcSJeremy L Thompson } 9653f919cbcSJeremy L Thompson 9663f919cbcSJeremy L Thompson switch (eval_mode) { 9673f919cbcSJeremy L Thompson case CEED_EVAL_NONE: 9683f919cbcSJeremy L Thompson *flops = 0; 9693f919cbcSJeremy L Thompson break; 970a82cd097SZach Atkins case CEED_EVAL_INTERP: { 971a82cd097SZach Atkins *flops = tensor_flops + num_points * num_comp * (point_tensor_flops + (t_mode == CEED_TRANSPOSE ? CeedIntPow(Q_1d, dim) : 0)); 972a82cd097SZach Atkins if (dim == 3 && is_gpu) { 973802d760aSJeremy L Thompson *flops += num_points * Q_1d * 974802d760aSJeremy L Thompson (chebyshev_flops + num_comp * (2 * chebyshev_flops + 2 * Q_1d * Q_1d + (t_mode == CEED_TRANSPOSE ? 2 * Q_1d + 1 : 3 * Q_1d))); 975a82cd097SZach Atkins } else { 976a82cd097SZach Atkins *flops += num_points * (is_gpu ? num_comp : 1) * dim * chebyshev_flops; 977a82cd097SZach Atkins } 9783f919cbcSJeremy L Thompson break; 979a82cd097SZach Atkins } 980a82cd097SZach Atkins case CEED_EVAL_GRAD: { 981a82cd097SZach Atkins *flops = tensor_flops + num_points * num_comp * (point_tensor_flops + (t_mode == CEED_TRANSPOSE ? CeedIntPow(Q_1d, dim) : 0)); 982a82cd097SZach Atkins if (dim == 3 && is_gpu) { 983802d760aSJeremy L Thompson CeedInt inner_flops = 984dc7b9553SJeremy L Thompson dim * (2 * Q_1d * Q_1d + (t_mode == CEED_TRANSPOSE ? 2 : 3) * Q_1d) + (dim - 1) * (2 * chebyshev_flops + d_chebyshev_flops); 985802d760aSJeremy L Thompson 98627a8a650SZach Atkins *flops += num_points * Q_1d * (chebyshev_flops + d_chebyshev_flops + num_comp * (inner_flops + (t_mode == CEED_TRANSPOSE ? 1 : 0))); 987a82cd097SZach Atkins } else { 988a82cd097SZach Atkins *flops += num_points * (is_gpu ? num_comp : 1) * dim * (d_chebyshev_flops + (dim - 1) * chebyshev_flops); 989a82cd097SZach Atkins } 9903f919cbcSJeremy L Thompson break; 991a82cd097SZach Atkins } 9923f919cbcSJeremy L Thompson case CEED_EVAL_DIV: 9933f919cbcSJeremy L Thompson case CEED_EVAL_CURL: { 9943f919cbcSJeremy L Thompson // LCOV_EXCL_START 99552780386SJeremy L Thompson return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported at points", 9963f919cbcSJeremy L Thompson CeedEvalModes[eval_mode]); 9973f919cbcSJeremy L Thompson break; 9983f919cbcSJeremy L Thompson // LCOV_EXCL_STOP 9993f919cbcSJeremy L Thompson } 10003f919cbcSJeremy L Thompson case CEED_EVAL_WEIGHT: 10013f919cbcSJeremy L Thompson *flops = num_points; 10023f919cbcSJeremy L Thompson break; 10033f919cbcSJeremy L Thompson } 10043f919cbcSJeremy L Thompson } else { 10056e15d496SJeremy L Thompson switch (eval_mode) { 10062b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 10072b730f8bSJeremy L Thompson *flops = 0; 10082b730f8bSJeremy L Thompson break; 10092b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 10102b730f8bSJeremy L Thompson *flops = tensor_flops; 10112b730f8bSJeremy L Thompson break; 10122b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 10132b730f8bSJeremy L Thompson *flops = tensor_flops * 2; 10142b730f8bSJeremy L Thompson break; 10156e15d496SJeremy L Thompson case CEED_EVAL_DIV: 10161203703bSJeremy L Thompson case CEED_EVAL_CURL: { 10176574a04fSJeremy L Thompson // LCOV_EXCL_START 10186e536b99SJeremy L Thompson return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported", 10196e536b99SJeremy L Thompson CeedEvalModes[eval_mode]); 10202b730f8bSJeremy L Thompson break; 10216e15d496SJeremy L Thompson // LCOV_EXCL_STOP 10221203703bSJeremy L Thompson } 10232b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 10242b730f8bSJeremy L Thompson *flops = dim * CeedIntPow(Q_1d, dim); 10252b730f8bSJeremy L Thompson break; 10266e15d496SJeremy L Thompson } 10273f919cbcSJeremy L Thompson } 10286e15d496SJeremy L Thompson } else { 1029c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 10301c66c397SJeremy L Thompson 10312b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 10322b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 1033c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 10342b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 10352b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 10366e15d496SJeremy L Thompson switch (eval_mode) { 10372b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 10382b730f8bSJeremy L Thompson *flops = 0; 10392b730f8bSJeremy L Thompson break; 10402b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 10412b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 10422b730f8bSJeremy L Thompson case CEED_EVAL_DIV: 10432b730f8bSJeremy L Thompson case CEED_EVAL_CURL: 1044c4e3f59bSSebastian Grimberg *flops = num_nodes * num_qpts * num_comp * q_comp; 10452b730f8bSJeremy L Thompson break; 10462b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 10472b730f8bSJeremy L Thompson *flops = 0; 10482b730f8bSJeremy L Thompson break; 10496e15d496SJeremy L Thompson } 10506e15d496SJeremy L Thompson } 10516e15d496SJeremy L Thompson return CEED_ERROR_SUCCESS; 10526e15d496SJeremy L Thompson } 10536e15d496SJeremy L Thompson 10546e15d496SJeremy L Thompson /** 1055ca94c3ddSJeremy L Thompson @brief Get `CeedFESpace` for a `CeedBasis` 1056c4e3f59bSSebastian Grimberg 1057ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1058ca94c3ddSJeremy L Thompson @param[out] fe_space Variable to store `CeedFESpace` 1059c4e3f59bSSebastian Grimberg 1060c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1061c4e3f59bSSebastian Grimberg 1062c4e3f59bSSebastian Grimberg @ref Backend 1063c4e3f59bSSebastian Grimberg **/ 1064c4e3f59bSSebastian Grimberg int CeedBasisGetFESpace(CeedBasis basis, CeedFESpace *fe_space) { 1065c4e3f59bSSebastian Grimberg *fe_space = basis->fe_space; 1066c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1067c4e3f59bSSebastian Grimberg } 1068c4e3f59bSSebastian Grimberg 1069c4e3f59bSSebastian Grimberg /** 1070ca94c3ddSJeremy L Thompson @brief Get dimension for given `CeedElemTopology` 10717a982d89SJeremy L. Thompson 1072ca94c3ddSJeremy L Thompson @param[in] topo `CeedElemTopology` 10737a982d89SJeremy L. Thompson @param[out] dim Variable to store dimension of topology 10747a982d89SJeremy L. Thompson 10757a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 10767a982d89SJeremy L. Thompson 10777a982d89SJeremy L. Thompson @ref Backend 10787a982d89SJeremy L. Thompson **/ 10797a982d89SJeremy L. Thompson int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim) { 10807a982d89SJeremy L. Thompson *dim = (CeedInt)topo >> 16; 1081e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10827a982d89SJeremy L. Thompson } 10837a982d89SJeremy L. Thompson 10847a982d89SJeremy L. Thompson /** 1085ca94c3ddSJeremy L Thompson @brief Get `CeedTensorContract` of a `CeedBasis` 10867a982d89SJeremy L. Thompson 1087ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1088ca94c3ddSJeremy L Thompson @param[out] contract Variable to store `CeedTensorContract` 10897a982d89SJeremy L. Thompson 10907a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 10917a982d89SJeremy L. Thompson 10927a982d89SJeremy L. Thompson @ref Backend 10937a982d89SJeremy L. Thompson **/ 10947a982d89SJeremy L. Thompson int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract) { 10957a982d89SJeremy L. Thompson *contract = basis->contract; 1096e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10977a982d89SJeremy L. Thompson } 10987a982d89SJeremy L. Thompson 10997a982d89SJeremy L. Thompson /** 1100ca94c3ddSJeremy L Thompson @brief Set `CeedTensorContract` of a `CeedBasis` 11017a982d89SJeremy L. Thompson 1102ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` 1103ca94c3ddSJeremy L Thompson @param[in] contract `CeedTensorContract` to set 11047a982d89SJeremy L. Thompson 11057a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 11067a982d89SJeremy L. Thompson 11077a982d89SJeremy L. Thompson @ref Backend 11087a982d89SJeremy L. Thompson **/ 110934359f16Sjeremylt int CeedBasisSetTensorContract(CeedBasis basis, CeedTensorContract contract) { 111034359f16Sjeremylt basis->contract = contract; 11112b730f8bSJeremy L Thompson CeedCall(CeedTensorContractReference(contract)); 1112e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11137a982d89SJeremy L. Thompson } 11147a982d89SJeremy L. Thompson 11157a982d89SJeremy L. Thompson /** 1116ca94c3ddSJeremy L Thompson @brief Return a reference implementation of matrix multiplication \f$C = A B\f$. 1117ba59ac12SSebastian Grimberg 1118ca94c3ddSJeremy L Thompson Note: This is a reference implementation for CPU `CeedScalar` pointers that is not intended for high performance. 11197a982d89SJeremy L. Thompson 1120ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 1121ca94c3ddSJeremy L Thompson @param[in] mat_A Row-major matrix `A` 1122ca94c3ddSJeremy L Thompson @param[in] mat_B Row-major matrix `B` 1123ca94c3ddSJeremy L Thompson @param[out] mat_C Row-major output matrix `C` 1124ca94c3ddSJeremy L Thompson @param[in] m Number of rows of `C` 1125ca94c3ddSJeremy L Thompson @param[in] n Number of columns of `C` 1126ca94c3ddSJeremy L Thompson @param[in] kk Number of columns of `A`/rows of `B` 11277a982d89SJeremy L. Thompson 11287a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 11297a982d89SJeremy L. Thompson 11307a982d89SJeremy L. Thompson @ref Utility 11317a982d89SJeremy L. Thompson **/ 11322b730f8bSJeremy L Thompson int CeedMatrixMatrixMultiply(Ceed ceed, const CeedScalar *mat_A, const CeedScalar *mat_B, CeedScalar *mat_C, CeedInt m, CeedInt n, CeedInt kk) { 11332b730f8bSJeremy L Thompson for (CeedInt i = 0; i < m; i++) { 11347a982d89SJeremy L. Thompson for (CeedInt j = 0; j < n; j++) { 11357a982d89SJeremy L. Thompson CeedScalar sum = 0; 11361c66c397SJeremy L Thompson 11372b730f8bSJeremy L Thompson for (CeedInt k = 0; k < kk; k++) sum += mat_A[k + i * kk] * mat_B[j + k * n]; 1138d1d35e2fSjeremylt mat_C[j + i * n] = sum; 11397a982d89SJeremy L. Thompson } 11402b730f8bSJeremy L Thompson } 1141e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11427a982d89SJeremy L. Thompson } 11437a982d89SJeremy L. Thompson 1144ba59ac12SSebastian Grimberg /** 1145ba59ac12SSebastian Grimberg @brief Return QR Factorization of a matrix 1146ba59ac12SSebastian Grimberg 1147ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 1148ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 1149ca94c3ddSJeremy L Thompson @param[in,out] tau Vector of length `m` of scaling factors 1150ba59ac12SSebastian Grimberg @param[in] m Number of rows 1151ba59ac12SSebastian Grimberg @param[in] n Number of columns 1152ba59ac12SSebastian Grimberg 1153ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1154ba59ac12SSebastian Grimberg 1155ba59ac12SSebastian Grimberg @ref Utility 1156ba59ac12SSebastian Grimberg **/ 1157ba59ac12SSebastian Grimberg int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau, CeedInt m, CeedInt n) { 1158ba59ac12SSebastian Grimberg CeedScalar v[m]; 1159ba59ac12SSebastian Grimberg 1160ba59ac12SSebastian Grimberg // Check matrix shape 11616574a04fSJeremy L Thompson CeedCheck(n <= m, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute QR factorization with n > m"); 1162ba59ac12SSebastian Grimberg 1163ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 11641c66c397SJeremy L Thompson CeedScalar sigma = 0.0; 11651c66c397SJeremy L Thompson 1166ba59ac12SSebastian Grimberg if (i >= m - 1) { // last row of matrix, no reflection needed 1167ba59ac12SSebastian Grimberg tau[i] = 0.; 1168ba59ac12SSebastian Grimberg break; 1169ba59ac12SSebastian Grimberg } 1170ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 1171ba59ac12SSebastian Grimberg v[i] = mat[i + n * i]; 1172ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) { 1173ba59ac12SSebastian Grimberg v[j] = mat[i + n * j]; 1174ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 1175ba59ac12SSebastian Grimberg } 11761c66c397SJeremy L Thompson const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:m] 11771c66c397SJeremy L Thompson const CeedScalar R_ii = -copysign(norm, v[i]); 11781c66c397SJeremy L Thompson 1179ba59ac12SSebastian Grimberg v[i] -= R_ii; 1180ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 1181ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 1182ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 1183ba59ac12SSebastian Grimberg tau[i] = 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 1184ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] /= v[i]; 1185ba59ac12SSebastian Grimberg 1186ba59ac12SSebastian Grimberg // Apply Householder reflector to lower right panel 1187ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[i * n + i + 1], &v[i], tau[i], m - i, n - i - 1, n, 1); 1188ba59ac12SSebastian Grimberg // Save v 1189ba59ac12SSebastian Grimberg mat[i + n * i] = R_ii; 1190ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) mat[i + n * j] = v[j]; 1191ba59ac12SSebastian Grimberg } 1192ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 1193ba59ac12SSebastian Grimberg } 1194ba59ac12SSebastian Grimberg 1195ba59ac12SSebastian Grimberg /** 1196ba59ac12SSebastian Grimberg @brief Apply Householder Q matrix 1197ba59ac12SSebastian Grimberg 1198ca94c3ddSJeremy 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$. 1199ba59ac12SSebastian Grimberg 1200ba59ac12SSebastian Grimberg @param[in,out] mat_A Matrix to apply Householder Q to, in place 1201ba59ac12SSebastian Grimberg @param[in] mat_Q Householder Q matrix 1202ba59ac12SSebastian Grimberg @param[in] tau Householder scaling factors 1203ba59ac12SSebastian Grimberg @param[in] t_mode Transpose mode for application 1204ca94c3ddSJeremy L Thompson @param[in] m Number of rows in `A` 1205ca94c3ddSJeremy L Thompson @param[in] n Number of columns in `A` 1206ca94c3ddSJeremy L Thompson @param[in] k Number of elementary reflectors in Q, `k < m` 1207ca94c3ddSJeremy L Thompson @param[in] row Row stride in `A` 1208ca94c3ddSJeremy L Thompson @param[in] col Col stride in `A` 1209ba59ac12SSebastian Grimberg 1210ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1211ba59ac12SSebastian Grimberg 1212c4e3f59bSSebastian Grimberg @ref Utility 1213ba59ac12SSebastian Grimberg **/ 1214ba59ac12SSebastian Grimberg int CeedHouseholderApplyQ(CeedScalar *mat_A, const CeedScalar *mat_Q, const CeedScalar *tau, CeedTransposeMode t_mode, CeedInt m, CeedInt n, 1215ba59ac12SSebastian Grimberg CeedInt k, CeedInt row, CeedInt col) { 1216ba59ac12SSebastian Grimberg CeedScalar *v; 12171c66c397SJeremy L Thompson 1218ba59ac12SSebastian Grimberg CeedCall(CeedMalloc(m, &v)); 1219ba59ac12SSebastian Grimberg for (CeedInt ii = 0; ii < k; ii++) { 1220ba59ac12SSebastian Grimberg CeedInt i = t_mode == CEED_TRANSPOSE ? ii : k - 1 - ii; 1221ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] = mat_Q[j * k + i]; 1222ba59ac12SSebastian Grimberg // Apply Householder reflector (I - tau v v^T) collo_grad_1d^T 1223ba59ac12SSebastian Grimberg CeedCall(CeedHouseholderReflect(&mat_A[i * row], &v[i], tau[i], m - i, n, row, col)); 1224ba59ac12SSebastian Grimberg } 1225ba59ac12SSebastian Grimberg CeedCall(CeedFree(&v)); 1226ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 1227ba59ac12SSebastian Grimberg } 1228ba59ac12SSebastian Grimberg 1229ba59ac12SSebastian Grimberg /** 12302247a93fSRezgar Shakeri @brief Return pseudoinverse of a matrix 12312247a93fSRezgar Shakeri 12322247a93fSRezgar Shakeri @param[in] ceed Ceed context for error handling 12332247a93fSRezgar Shakeri @param[in] mat Row-major matrix to compute pseudoinverse of 12342247a93fSRezgar Shakeri @param[in] m Number of rows 12352247a93fSRezgar Shakeri @param[in] n Number of columns 12362247a93fSRezgar Shakeri @param[out] mat_pinv Row-major pseudoinverse matrix 12372247a93fSRezgar Shakeri 12382247a93fSRezgar Shakeri @return An error code: 0 - success, otherwise - failure 12392247a93fSRezgar Shakeri 12402247a93fSRezgar Shakeri @ref Utility 12412247a93fSRezgar Shakeri **/ 12421203703bSJeremy L Thompson int CeedMatrixPseudoinverse(Ceed ceed, const CeedScalar *mat, CeedInt m, CeedInt n, CeedScalar *mat_pinv) { 12432247a93fSRezgar Shakeri CeedScalar *tau, *I, *mat_copy; 12442247a93fSRezgar Shakeri 12452247a93fSRezgar Shakeri CeedCall(CeedCalloc(m, &tau)); 12462247a93fSRezgar Shakeri CeedCall(CeedCalloc(m * m, &I)); 12472247a93fSRezgar Shakeri CeedCall(CeedCalloc(m * n, &mat_copy)); 12482247a93fSRezgar Shakeri memcpy(mat_copy, mat, m * n * sizeof mat[0]); 12492247a93fSRezgar Shakeri 12502247a93fSRezgar Shakeri // QR Factorization, mat = Q R 12512247a93fSRezgar Shakeri CeedCall(CeedQRFactorization(ceed, mat_copy, tau, m, n)); 12522247a93fSRezgar Shakeri 12532247a93fSRezgar Shakeri // -- Apply Q^T, I = Q^T * I 12542247a93fSRezgar Shakeri for (CeedInt i = 0; i < m; i++) I[i * m + i] = 1.0; 12552247a93fSRezgar Shakeri CeedCall(CeedHouseholderApplyQ(I, mat_copy, tau, CEED_TRANSPOSE, m, m, n, m, 1)); 12562247a93fSRezgar Shakeri // -- Apply R_inv, mat_pinv = R_inv * Q^T 12572247a93fSRezgar Shakeri for (CeedInt j = 0; j < m; j++) { // Column j 12582247a93fSRezgar Shakeri mat_pinv[j + m * (n - 1)] = I[j + m * (n - 1)] / mat_copy[n * n - 1]; 12592247a93fSRezgar Shakeri for (CeedInt i = n - 2; i >= 0; i--) { // Row i 12602247a93fSRezgar Shakeri mat_pinv[j + m * i] = I[j + m * i]; 12612247a93fSRezgar Shakeri for (CeedInt k = i + 1; k < n; k++) mat_pinv[j + m * i] -= mat_copy[k + n * i] * mat_pinv[j + m * k]; 12622247a93fSRezgar Shakeri mat_pinv[j + m * i] /= mat_copy[i + n * i]; 12632247a93fSRezgar Shakeri } 12642247a93fSRezgar Shakeri } 12652247a93fSRezgar Shakeri 12662247a93fSRezgar Shakeri // Cleanup 12672247a93fSRezgar Shakeri CeedCall(CeedFree(&I)); 12682247a93fSRezgar Shakeri CeedCall(CeedFree(&tau)); 12692247a93fSRezgar Shakeri CeedCall(CeedFree(&mat_copy)); 12702247a93fSRezgar Shakeri return CEED_ERROR_SUCCESS; 12712247a93fSRezgar Shakeri } 12722247a93fSRezgar Shakeri 12732247a93fSRezgar Shakeri /** 1274ba59ac12SSebastian Grimberg @brief Return symmetric Schur decomposition of the symmetric matrix mat via symmetric QR factorization 1275ba59ac12SSebastian Grimberg 1276ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 1277ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 1278ba59ac12SSebastian Grimberg @param[out] lambda Vector of length n of eigenvalues 1279ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 1280ba59ac12SSebastian Grimberg 1281ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1282ba59ac12SSebastian Grimberg 1283ba59ac12SSebastian Grimberg @ref Utility 1284ba59ac12SSebastian Grimberg **/ 12852c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 12862c2ea1dbSJeremy L Thompson int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat, CeedScalar *lambda, CeedInt n) { 1287ba59ac12SSebastian Grimberg // Check bounds for clang-tidy 12886574a04fSJeremy L Thompson CeedCheck(n > 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute symmetric Schur decomposition of scalars"); 1289ba59ac12SSebastian Grimberg 1290ba59ac12SSebastian Grimberg CeedScalar v[n - 1], tau[n - 1], mat_T[n * n]; 1291ba59ac12SSebastian Grimberg 1292ba59ac12SSebastian Grimberg // Copy mat to mat_T and set mat to I 1293ba59ac12SSebastian Grimberg memcpy(mat_T, mat, n * n * sizeof(mat[0])); 1294ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 1295ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) mat[j + n * i] = (i == j) ? 1 : 0; 1296ba59ac12SSebastian Grimberg } 1297ba59ac12SSebastian Grimberg 1298ba59ac12SSebastian Grimberg // Reduce to tridiagonal 1299ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - 1; i++) { 1300ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 1301ba59ac12SSebastian Grimberg CeedScalar sigma = 0.0; 13021c66c397SJeremy L Thompson 1303ba59ac12SSebastian Grimberg v[i] = mat_T[i + n * (i + 1)]; 1304ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 1305ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 1306ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 1307ba59ac12SSebastian Grimberg } 13081c66c397SJeremy L Thompson const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:n-1] 13091c66c397SJeremy L Thompson const CeedScalar R_ii = -copysign(norm, v[i]); 13101c66c397SJeremy L Thompson 1311ba59ac12SSebastian Grimberg v[i] -= R_ii; 1312ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 1313ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 1314ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 1315ba59ac12SSebastian Grimberg tau[i] = i == n - 2 ? 2 : 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 1316ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) v[j] /= v[i]; 1317ba59ac12SSebastian Grimberg 1318ba59ac12SSebastian Grimberg // Update sub and super diagonal 1319ba59ac12SSebastian Grimberg for (CeedInt j = i + 2; j < n; j++) { 1320ba59ac12SSebastian Grimberg mat_T[i + n * j] = 0; 1321ba59ac12SSebastian Grimberg mat_T[j + n * i] = 0; 1322ba59ac12SSebastian Grimberg } 1323ba59ac12SSebastian Grimberg // Apply symmetric Householder reflector to lower right panel 1324ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 1325ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), 1, n); 1326ba59ac12SSebastian Grimberg 1327ba59ac12SSebastian Grimberg // Save v 1328ba59ac12SSebastian Grimberg mat_T[i + n * (i + 1)] = R_ii; 1329ba59ac12SSebastian Grimberg mat_T[(i + 1) + n * i] = R_ii; 1330ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 1331ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = v[j]; 1332ba59ac12SSebastian Grimberg } 1333ba59ac12SSebastian Grimberg } 1334ba59ac12SSebastian Grimberg // Backwards accumulation of Q 1335ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 1336ba59ac12SSebastian Grimberg if (tau[i] > 0.0) { 1337ba59ac12SSebastian Grimberg v[i] = 1; 1338ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 1339ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 1340ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = 0; 1341ba59ac12SSebastian Grimberg } 1342ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 1343ba59ac12SSebastian Grimberg } 1344ba59ac12SSebastian Grimberg } 1345ba59ac12SSebastian Grimberg 1346ba59ac12SSebastian Grimberg // Reduce sub and super diagonal 1347ba59ac12SSebastian Grimberg CeedInt p = 0, q = 0, itr = 0, max_itr = n * n * n * n; 1348ba59ac12SSebastian Grimberg CeedScalar tol = CEED_EPSILON; 1349ba59ac12SSebastian Grimberg 1350ba59ac12SSebastian Grimberg while (itr < max_itr) { 1351ba59ac12SSebastian Grimberg // Update p, q, size of reduced portions of diagonal 1352ba59ac12SSebastian Grimberg p = 0; 1353ba59ac12SSebastian Grimberg q = 0; 1354ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 1355ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) q += 1; 1356ba59ac12SSebastian Grimberg else break; 1357ba59ac12SSebastian Grimberg } 1358ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - q - 1; i++) { 1359ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) p += 1; 1360ba59ac12SSebastian Grimberg else break; 1361ba59ac12SSebastian Grimberg } 1362ba59ac12SSebastian Grimberg if (q == n - 1) break; // Finished reducing 1363ba59ac12SSebastian Grimberg 1364ba59ac12SSebastian Grimberg // Reduce tridiagonal portion 1365ba59ac12SSebastian Grimberg CeedScalar t_nn = mat_T[(n - 1 - q) + n * (n - 1 - q)], t_nnm1 = mat_T[(n - 2 - q) + n * (n - 1 - q)]; 1366ba59ac12SSebastian Grimberg CeedScalar d = (mat_T[(n - 2 - q) + n * (n - 2 - q)] - t_nn) / 2; 1367ba59ac12SSebastian Grimberg CeedScalar mu = t_nn - t_nnm1 * t_nnm1 / (d + copysign(sqrt(d * d + t_nnm1 * t_nnm1), d)); 1368ba59ac12SSebastian Grimberg CeedScalar x = mat_T[p + n * p] - mu; 1369ba59ac12SSebastian Grimberg CeedScalar z = mat_T[p + n * (p + 1)]; 13701c66c397SJeremy L Thompson 1371ba59ac12SSebastian Grimberg for (CeedInt k = p; k < n - q - 1; k++) { 1372ba59ac12SSebastian Grimberg // Compute Givens rotation 1373ba59ac12SSebastian Grimberg CeedScalar c = 1, s = 0; 13741c66c397SJeremy L Thompson 1375ba59ac12SSebastian Grimberg if (fabs(z) > tol) { 1376ba59ac12SSebastian Grimberg if (fabs(z) > fabs(x)) { 13771c66c397SJeremy L Thompson const CeedScalar tau = -x / z; 13781c66c397SJeremy L Thompson 13791c66c397SJeremy L Thompson s = 1 / sqrt(1 + tau * tau); 13801c66c397SJeremy L Thompson c = s * tau; 1381ba59ac12SSebastian Grimberg } else { 13821c66c397SJeremy L Thompson const CeedScalar tau = -z / x; 13831c66c397SJeremy L Thompson 13841c66c397SJeremy L Thompson c = 1 / sqrt(1 + tau * tau); 13851c66c397SJeremy L Thompson s = c * tau; 1386ba59ac12SSebastian Grimberg } 1387ba59ac12SSebastian Grimberg } 1388ba59ac12SSebastian Grimberg 1389ba59ac12SSebastian Grimberg // Apply Givens rotation to T 1390ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 1391ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_TRANSPOSE, k, k + 1, n, n); 1392ba59ac12SSebastian Grimberg 1393ba59ac12SSebastian Grimberg // Apply Givens rotation to Q 1394ba59ac12SSebastian Grimberg CeedGivensRotation(mat, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 1395ba59ac12SSebastian Grimberg 1396ba59ac12SSebastian Grimberg // Update x, z 1397ba59ac12SSebastian Grimberg if (k < n - q - 2) { 1398ba59ac12SSebastian Grimberg x = mat_T[k + n * (k + 1)]; 1399ba59ac12SSebastian Grimberg z = mat_T[k + n * (k + 2)]; 1400ba59ac12SSebastian Grimberg } 1401ba59ac12SSebastian Grimberg } 1402ba59ac12SSebastian Grimberg itr++; 1403ba59ac12SSebastian Grimberg } 1404ba59ac12SSebastian Grimberg 1405ba59ac12SSebastian Grimberg // Save eigenvalues 1406ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) lambda[i] = mat_T[i + n * i]; 1407ba59ac12SSebastian Grimberg 1408ba59ac12SSebastian Grimberg // Check convergence 14096574a04fSJeremy L Thompson CeedCheck(itr < max_itr || q > n, ceed, CEED_ERROR_MINOR, "Symmetric QR failed to converge"); 1410ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 1411ba59ac12SSebastian Grimberg } 14122c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 1413ba59ac12SSebastian Grimberg 1414ba59ac12SSebastian Grimberg /** 1415ba59ac12SSebastian Grimberg @brief Return Simultaneous Diagonalization of two matrices. 1416ba59ac12SSebastian Grimberg 1417ca94c3ddSJeremy L Thompson This solves the generalized eigenvalue problem `A x = lambda B x`, where `A` and `B` are symmetric and `B` is positive definite. 1418ca94c3ddSJeremy L Thompson We generate the matrix `X` and vector `Lambda` such that `X^T A X = Lambda` and `X^T B X = I`. 1419ca94c3ddSJeremy L Thompson This is equivalent to the LAPACK routine 'sygv' with `TYPE = 1`. 1420ba59ac12SSebastian Grimberg 1421ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 1422ba59ac12SSebastian Grimberg @param[in] mat_A Row-major matrix to be factorized with eigenvalues 1423ba59ac12SSebastian Grimberg @param[in] mat_B Row-major matrix to be factorized to identity 1424ba59ac12SSebastian Grimberg @param[out] mat_X Row-major orthogonal matrix 1425ca94c3ddSJeremy L Thompson @param[out] lambda Vector of length `n` of generalized eigenvalues 1426ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 1427ba59ac12SSebastian Grimberg 1428ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1429ba59ac12SSebastian Grimberg 1430ba59ac12SSebastian Grimberg @ref Utility 1431ba59ac12SSebastian Grimberg **/ 14322c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 14332c2ea1dbSJeremy L Thompson int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A, CeedScalar *mat_B, CeedScalar *mat_X, CeedScalar *lambda, CeedInt n) { 1434ba59ac12SSebastian Grimberg CeedScalar *mat_C, *mat_G, *vec_D; 14351c66c397SJeremy L Thompson 1436ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_C)); 1437ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_G)); 1438ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n, &vec_D)); 1439ba59ac12SSebastian Grimberg 1440ba59ac12SSebastian Grimberg // Compute B = G D G^T 1441ba59ac12SSebastian Grimberg memcpy(mat_G, mat_B, n * n * sizeof(mat_B[0])); 1442ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_G, vec_D, n)); 1443ba59ac12SSebastian Grimberg 1444ba59ac12SSebastian Grimberg // Sort eigenvalues 1445ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 1446ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 1447ba59ac12SSebastian Grimberg if (fabs(vec_D[j]) > fabs(vec_D[j + 1])) { 14481c66c397SJeremy L Thompson CeedScalarSwap(vec_D[j], vec_D[j + 1]); 14491c66c397SJeremy L Thompson for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_G[k * n + j], mat_G[k * n + j + 1]); 1450ba59ac12SSebastian Grimberg } 1451ba59ac12SSebastian Grimberg } 1452ba59ac12SSebastian Grimberg } 1453ba59ac12SSebastian Grimberg 1454ba59ac12SSebastian Grimberg // Compute C = (G D^1/2)^-1 A (G D^1/2)^-T 1455ba59ac12SSebastian Grimberg // = D^-1/2 G^T A G D^-1/2 1456ba59ac12SSebastian Grimberg // -- D = D^-1/2 1457ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) vec_D[i] = 1. / sqrt(vec_D[i]); 1458ba59ac12SSebastian Grimberg // -- G = G D^-1/2 1459ba59ac12SSebastian Grimberg // -- C = D^-1/2 G^T 1460ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 1461ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) { 1462ba59ac12SSebastian Grimberg mat_G[i * n + j] *= vec_D[j]; 1463ba59ac12SSebastian Grimberg mat_C[j * n + i] = mat_G[i * n + j]; 1464ba59ac12SSebastian Grimberg } 1465ba59ac12SSebastian Grimberg } 1466ba59ac12SSebastian Grimberg // -- X = (D^-1/2 G^T) A 1467ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_C, (const CeedScalar *)mat_A, mat_X, n, n, n)); 1468ba59ac12SSebastian Grimberg // -- C = (D^-1/2 G^T A) (G D^-1/2) 1469ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_X, (const CeedScalar *)mat_G, mat_C, n, n, n)); 1470ba59ac12SSebastian Grimberg 1471ba59ac12SSebastian Grimberg // Compute Q^T C Q = lambda 1472ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_C, lambda, n)); 1473ba59ac12SSebastian Grimberg 1474ba59ac12SSebastian Grimberg // Sort eigenvalues 1475ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 1476ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 1477ba59ac12SSebastian Grimberg if (fabs(lambda[j]) > fabs(lambda[j + 1])) { 14781c66c397SJeremy L Thompson CeedScalarSwap(lambda[j], lambda[j + 1]); 14791c66c397SJeremy L Thompson for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_C[k * n + j], mat_C[k * n + j + 1]); 1480ba59ac12SSebastian Grimberg } 1481ba59ac12SSebastian Grimberg } 1482ba59ac12SSebastian Grimberg } 1483ba59ac12SSebastian Grimberg 1484ba59ac12SSebastian Grimberg // Set X = (G D^1/2)^-T Q 1485ba59ac12SSebastian Grimberg // = G D^-1/2 Q 1486ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_G, (const CeedScalar *)mat_C, mat_X, n, n, n)); 1487ba59ac12SSebastian Grimberg 1488ba59ac12SSebastian Grimberg // Cleanup 1489ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_C)); 1490ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_G)); 1491ba59ac12SSebastian Grimberg CeedCall(CeedFree(&vec_D)); 1492ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 1493ba59ac12SSebastian Grimberg } 14942c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 1495ba59ac12SSebastian Grimberg 14967a982d89SJeremy L. Thompson /// @} 14977a982d89SJeremy L. Thompson 14987a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 14997a982d89SJeremy L. Thompson /// CeedBasis Public API 15007a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 15017a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser 1502d7b241e6Sjeremylt /// @{ 1503d7b241e6Sjeremylt 1504b11c1e72Sjeremylt /** 1505ca94c3ddSJeremy L Thompson @brief Create a tensor-product basis for \f$H^1\f$ discretizations 1506b11c1e72Sjeremylt 1507ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1508ea61e9acSJeremy L Thompson @param[in] dim Topological dimension 1509ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1510ea61e9acSJeremy L Thompson @param[in] P_1d Number of nodes in one dimension 1511ea61e9acSJeremy L Thompson @param[in] Q_1d Number of quadrature points in one dimension 1512ca94c3ddSJeremy L Thompson @param[in] interp_1d Row-major (`Q_1d * P_1d`) matrix expressing the values of nodal basis functions at quadrature points 1513ca94c3ddSJeremy L Thompson @param[in] grad_1d Row-major (`Q_1d * P_1d`) matrix expressing derivatives of nodal basis functions at quadrature points 1514ca94c3ddSJeremy 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]` 1515ca94c3ddSJeremy L Thompson @param[in] q_weight_1d Array of length `Q_1d` holding the quadrature weights on the reference element 1516ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1517b11c1e72Sjeremylt 1518b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1519dfdf5a53Sjeremylt 15207a982d89SJeremy L. Thompson @ref User 1521b11c1e72Sjeremylt **/ 15222b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, 15232b730f8bSJeremy L Thompson const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis *basis) { 15245fe0d4faSjeremylt if (!ceed->BasisCreateTensorH1) { 15255fe0d4faSjeremylt Ceed delegate; 15266574a04fSJeremy L Thompson 15272b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 15281ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateTensorH1"); 15292b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 15309bc66399SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 1531e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15325fe0d4faSjeremylt } 1533e15f9bd0SJeremy L Thompson 1534ca94c3ddSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value"); 1535ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1536ca94c3ddSJeremy L Thompson CeedCheck(P_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1537ca94c3ddSJeremy L Thompson CeedCheck(Q_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1538227444bfSJeremy L Thompson 15392b730f8bSJeremy L Thompson CeedElemTopology topo = dim == 1 ? CEED_TOPOLOGY_LINE : dim == 2 ? CEED_TOPOLOGY_QUAD : CEED_TOPOLOGY_HEX; 1540e15f9bd0SJeremy L Thompson 15412b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1542db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1543d1d35e2fSjeremylt (*basis)->ref_count = 1; 15446402da51SJeremy L Thompson (*basis)->is_tensor_basis = true; 1545d7b241e6Sjeremylt (*basis)->dim = dim; 1546d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 1547d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 1548d1d35e2fSjeremylt (*basis)->P_1d = P_1d; 1549d1d35e2fSjeremylt (*basis)->Q_1d = Q_1d; 1550d1d35e2fSjeremylt (*basis)->P = CeedIntPow(P_1d, dim); 1551d1d35e2fSjeremylt (*basis)->Q = CeedIntPow(Q_1d, dim); 1552c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 15532b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_ref_1d)); 15542b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_weight_1d)); 1555ff3a0f91SJeremy L Thompson if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d * sizeof(q_ref_1d[0])); 15562b730f8bSJeremy L Thompson if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, Q_1d * sizeof(q_weight_1d[0])); 15572b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->interp_1d)); 15582b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->grad_1d)); 15592b730f8bSJeremy L Thompson if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, Q_1d * P_1d * sizeof(interp_1d[0])); 1560ff3a0f91SJeremy L Thompson if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d * P_1d * sizeof(grad_1d[0])); 15612b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, *basis)); 1562e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1563d7b241e6Sjeremylt } 1564d7b241e6Sjeremylt 1565b11c1e72Sjeremylt /** 1566ca94c3ddSJeremy L Thompson @brief Create a tensor-product \f$H^1\f$ Lagrange basis 1567b11c1e72Sjeremylt 1568ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1569ea61e9acSJeremy L Thompson @param[in] dim Topological dimension of element 1570ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1571ea61e9acSJeremy L Thompson @param[in] P Number of Gauss-Lobatto nodes in one dimension. 1572ca94c3ddSJeremy L Thompson The polynomial degree of the resulting `Q_k` element is `k = P - 1`. 1573ea61e9acSJeremy L Thompson @param[in] Q Number of quadrature points in one dimension. 1574ca94c3ddSJeremy L Thompson @param[in] quad_mode Distribution of the `Q` quadrature points (affects order of accuracy for the quadrature) 1575ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1576b11c1e72Sjeremylt 1577b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1578dfdf5a53Sjeremylt 15797a982d89SJeremy L. Thompson @ref User 1580b11c1e72Sjeremylt **/ 15812b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis) { 1582d7b241e6Sjeremylt // Allocate 1583c8c3fa7dSJeremy L Thompson int ierr = CEED_ERROR_SUCCESS; 15842b730f8bSJeremy L Thompson CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, *q_weight_1d; 15854d537eeaSYohann 1586ca94c3ddSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value"); 1587ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1588ca94c3ddSJeremy L Thompson CeedCheck(P > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1589ca94c3ddSJeremy L Thompson CeedCheck(Q > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1590227444bfSJeremy L Thompson 1591e15f9bd0SJeremy L Thompson // Get Nodes and Weights 15922b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &interp_1d)); 15932b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &grad_1d)); 15942b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P, &nodes)); 15952b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_ref_1d)); 15962b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_weight_1d)); 15972b730f8bSJeremy L Thompson if (CeedLobattoQuadrature(P, nodes, NULL) != CEED_ERROR_SUCCESS) goto cleanup; 1598d1d35e2fSjeremylt switch (quad_mode) { 1599d7b241e6Sjeremylt case CEED_GAUSS: 1600d1d35e2fSjeremylt ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d); 1601d7b241e6Sjeremylt break; 1602d7b241e6Sjeremylt case CEED_GAUSS_LOBATTO: 1603d1d35e2fSjeremylt ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d); 1604d7b241e6Sjeremylt break; 1605d7b241e6Sjeremylt } 16062b730f8bSJeremy L Thompson if (ierr != CEED_ERROR_SUCCESS) goto cleanup; 1607e15f9bd0SJeremy L Thompson 1608d7b241e6Sjeremylt // Build B, D matrix 1609d7b241e6Sjeremylt // Fornberg, 1998 1610c8c3fa7dSJeremy L Thompson for (CeedInt i = 0; i < Q; i++) { 1611d7b241e6Sjeremylt c1 = 1.0; 1612d1d35e2fSjeremylt c3 = nodes[0] - q_ref_1d[i]; 1613d1d35e2fSjeremylt interp_1d[i * P + 0] = 1.0; 1614c8c3fa7dSJeremy L Thompson for (CeedInt j = 1; j < P; j++) { 1615d7b241e6Sjeremylt c2 = 1.0; 1616d7b241e6Sjeremylt c4 = c3; 1617d1d35e2fSjeremylt c3 = nodes[j] - q_ref_1d[i]; 1618c8c3fa7dSJeremy L Thompson for (CeedInt k = 0; k < j; k++) { 1619d7b241e6Sjeremylt dx = nodes[j] - nodes[k]; 1620d7b241e6Sjeremylt c2 *= dx; 1621d7b241e6Sjeremylt if (k == j - 1) { 1622d1d35e2fSjeremylt grad_1d[i * P + j] = c1 * (interp_1d[i * P + k] - c4 * grad_1d[i * P + k]) / c2; 1623d1d35e2fSjeremylt interp_1d[i * P + j] = -c1 * c4 * interp_1d[i * P + k] / c2; 1624d7b241e6Sjeremylt } 1625d1d35e2fSjeremylt grad_1d[i * P + k] = (c3 * grad_1d[i * P + k] - interp_1d[i * P + k]) / dx; 1626d1d35e2fSjeremylt interp_1d[i * P + k] = c3 * interp_1d[i * P + k] / dx; 1627d7b241e6Sjeremylt } 1628d7b241e6Sjeremylt c1 = c2; 1629d7b241e6Sjeremylt } 1630d7b241e6Sjeremylt } 16319ac7b42eSJeremy L Thompson // Pass to CeedBasisCreateTensorH1 16322b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 1633e15f9bd0SJeremy L Thompson cleanup: 16342b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_1d)); 16352b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_1d)); 16362b730f8bSJeremy L Thompson CeedCall(CeedFree(&nodes)); 16372b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref_1d)); 16382b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight_1d)); 1639e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1640d7b241e6Sjeremylt } 1641d7b241e6Sjeremylt 1642b11c1e72Sjeremylt /** 1643ca94c3ddSJeremy L Thompson @brief Create a non tensor-product basis for \f$H^1\f$ discretizations 1644a8de75f0Sjeremylt 1645ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1646e00f3be8SJames Wright @param[in] topo Topology of element, e.g. hypercube, simplex, etc 1647ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1648ea61e9acSJeremy L Thompson @param[in] num_nodes Total number of nodes 1649ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1650ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`num_qpts * num_nodes`) matrix expressing the values of nodal basis functions at quadrature points 1651ca94c3ddSJeremy L Thompson @param[in] grad Row-major (`dim * num_qpts * num_nodes`) matrix expressing derivatives of nodal basis functions at quadrature points 1652fda26546SJeremy L Thompson @param[in] q_ref Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element 1653ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1654ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1655a8de75f0Sjeremylt 1656a8de75f0Sjeremylt @return An error code: 0 - success, otherwise - failure 1657a8de75f0Sjeremylt 16587a982d89SJeremy L. Thompson @ref User 1659a8de75f0Sjeremylt **/ 16602b730f8bSJeremy L Thompson int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 16612b730f8bSJeremy L Thompson const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1662d1d35e2fSjeremylt CeedInt P = num_nodes, Q = num_qpts, dim = 0; 1663a8de75f0Sjeremylt 16645fe0d4faSjeremylt if (!ceed->BasisCreateH1) { 16655fe0d4faSjeremylt Ceed delegate; 16666574a04fSJeremy L Thompson 16672b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 16681ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateH1"); 16692b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis)); 16709bc66399SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 1671e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 16725fe0d4faSjeremylt } 16735fe0d4faSjeremylt 1674ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1675ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1676ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1677227444bfSJeremy L Thompson 16782b730f8bSJeremy L Thompson CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1679a8de75f0Sjeremylt 1680db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1681db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1682d1d35e2fSjeremylt (*basis)->ref_count = 1; 16836402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1684a8de75f0Sjeremylt (*basis)->dim = dim; 1685d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 1686d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 1687a8de75f0Sjeremylt (*basis)->P = P; 1688a8de75f0Sjeremylt (*basis)->Q = Q; 1689c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 16902b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * dim, &(*basis)->q_ref_1d)); 16912b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &(*basis)->q_weight_1d)); 1692ff3a0f91SJeremy L Thompson if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1693ff3a0f91SJeremy L Thompson if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 16942b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * P, &(*basis)->interp)); 16952b730f8bSJeremy L Thompson CeedCall(CeedCalloc(dim * Q * P, &(*basis)->grad)); 1696ff3a0f91SJeremy L Thompson if (interp) memcpy((*basis)->interp, interp, Q * P * sizeof(interp[0])); 1697ff3a0f91SJeremy L Thompson if (grad) memcpy((*basis)->grad, grad, dim * Q * P * sizeof(grad[0])); 16982b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, *basis)); 1699e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1700a8de75f0Sjeremylt } 1701a8de75f0Sjeremylt 1702a8de75f0Sjeremylt /** 1703859c15bbSJames Wright @brief Create a non tensor-product basis for \f$H(\mathrm{div})\f$ discretizations 170450c301a5SRezgar Shakeri 1705ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1706ea61e9acSJeremy 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 1707ea61e9acSJeremy L Thompson @param[in] num_comp Number of components (usually 1 for vectors in H(div) bases) 1708ca94c3ddSJeremy L Thompson @param[in] num_nodes Total number of nodes (DoFs per element) 1709ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1710ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points 1711ca94c3ddSJeremy L Thompson @param[in] div Row-major (`num_qpts * num_nodes`) matrix expressing divergence of basis functions at quadrature points 1712ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element 1713ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1714ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 171550c301a5SRezgar Shakeri 171650c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 171750c301a5SRezgar Shakeri 171850c301a5SRezgar Shakeri @ref User 171950c301a5SRezgar Shakeri **/ 17202b730f8bSJeremy L Thompson int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 17212b730f8bSJeremy L Thompson const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 172250c301a5SRezgar Shakeri CeedInt Q = num_qpts, P = num_nodes, dim = 0; 1723c4e3f59bSSebastian Grimberg 172450c301a5SRezgar Shakeri if (!ceed->BasisCreateHdiv) { 172550c301a5SRezgar Shakeri Ceed delegate; 17266574a04fSJeremy L Thompson 17272b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 17286574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv"); 17292b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis)); 17309bc66399SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 173150c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 173250c301a5SRezgar Shakeri } 173350c301a5SRezgar Shakeri 1734ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1735ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1736ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1737227444bfSJeremy L Thompson 1738c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1739c4e3f59bSSebastian Grimberg 1740db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1741db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 174250c301a5SRezgar Shakeri (*basis)->ref_count = 1; 17436402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 174450c301a5SRezgar Shakeri (*basis)->dim = dim; 174550c301a5SRezgar Shakeri (*basis)->topo = topo; 174650c301a5SRezgar Shakeri (*basis)->num_comp = num_comp; 174750c301a5SRezgar Shakeri (*basis)->P = P; 174850c301a5SRezgar Shakeri (*basis)->Q = Q; 1749c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HDIV; 17502b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 17512b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 175250c301a5SRezgar Shakeri if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 175350c301a5SRezgar Shakeri if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 17542b730f8bSJeremy L Thompson CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 17552b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * P, &(*basis)->div)); 175650c301a5SRezgar Shakeri if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 175750c301a5SRezgar Shakeri if (div) memcpy((*basis)->div, div, Q * P * sizeof(div[0])); 17582b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, q_weight, *basis)); 175950c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 176050c301a5SRezgar Shakeri } 176150c301a5SRezgar Shakeri 176250c301a5SRezgar Shakeri /** 17634385fb7fSSebastian Grimberg @brief Create a non tensor-product basis for \f$H(\mathrm{curl})\f$ discretizations 1764c4e3f59bSSebastian Grimberg 1765ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1766c4e3f59bSSebastian Grimberg @param[in] topo Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below 1767ca94c3ddSJeremy L Thompson @param[in] num_comp Number of components (usually 1 for vectors in \f$H(\mathrm{curl})\f$ bases) 1768ca94c3ddSJeremy L Thompson @param[in] num_nodes Total number of nodes (DoFs per element) 1769c4e3f59bSSebastian Grimberg @param[in] num_qpts Total number of quadrature points 1770ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points 1771ca94c3ddSJeremy 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 1772ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element 1773ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1774ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1775c4e3f59bSSebastian Grimberg 1776c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1777c4e3f59bSSebastian Grimberg 1778c4e3f59bSSebastian Grimberg @ref User 1779c4e3f59bSSebastian Grimberg **/ 1780c4e3f59bSSebastian Grimberg int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 1781c4e3f59bSSebastian Grimberg const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1782c4e3f59bSSebastian Grimberg CeedInt Q = num_qpts, P = num_nodes, dim = 0, curl_comp = 0; 1783c4e3f59bSSebastian Grimberg 1784d075f50bSSebastian Grimberg if (!ceed->BasisCreateHcurl) { 1785c4e3f59bSSebastian Grimberg Ceed delegate; 17866574a04fSJeremy L Thompson 1787c4e3f59bSSebastian Grimberg CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 17886574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl"); 1789c4e3f59bSSebastian Grimberg CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis)); 17909bc66399SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 1791c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1792c4e3f59bSSebastian Grimberg } 1793c4e3f59bSSebastian Grimberg 1794ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1795ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1796ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1797c4e3f59bSSebastian Grimberg 1798c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1799c4e3f59bSSebastian Grimberg curl_comp = (dim < 3) ? 1 : dim; 1800c4e3f59bSSebastian Grimberg 1801db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1802db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1803c4e3f59bSSebastian Grimberg (*basis)->ref_count = 1; 18046402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1805c4e3f59bSSebastian Grimberg (*basis)->dim = dim; 1806c4e3f59bSSebastian Grimberg (*basis)->topo = topo; 1807c4e3f59bSSebastian Grimberg (*basis)->num_comp = num_comp; 1808c4e3f59bSSebastian Grimberg (*basis)->P = P; 1809c4e3f59bSSebastian Grimberg (*basis)->Q = Q; 1810c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HCURL; 1811c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 1812c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 1813c4e3f59bSSebastian Grimberg if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1814c4e3f59bSSebastian Grimberg if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 1815c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 1816c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(curl_comp * Q * P, &(*basis)->curl)); 1817c4e3f59bSSebastian Grimberg if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 1818c4e3f59bSSebastian Grimberg if (curl) memcpy((*basis)->curl, curl, curl_comp * Q * P * sizeof(curl[0])); 1819c4e3f59bSSebastian Grimberg CeedCall(ceed->BasisCreateHcurl(topo, dim, P, Q, interp, curl, q_ref, q_weight, *basis)); 1820c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1821c4e3f59bSSebastian Grimberg } 1822c4e3f59bSSebastian Grimberg 1823c4e3f59bSSebastian Grimberg /** 1824ca94c3ddSJeremy L Thompson @brief Create a `CeedBasis` for projection from the nodes of `basis_from` to the nodes of `basis_to`. 1825ba59ac12SSebastian Grimberg 1826ca94c3ddSJeremy L Thompson Only @ref CEED_EVAL_INTERP will be valid for the new basis, `basis_project`. 1827ca94c3ddSJeremy L Thompson For \f$H^1\f$ spaces, @ref CEED_EVAL_GRAD will also be valid. 1828ca94c3ddSJeremy L Thompson The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization. 1829ca94c3ddSJeremy L Thompson The gradient (for the \f$H^1\f$ case) is given by `grad_project = interp_to^+ * grad_from`. 183015ad3917SSebastian Grimberg 183115ad3917SSebastian Grimberg Note: `basis_from` and `basis_to` must have compatible quadrature spaces. 183215ad3917SSebastian Grimberg 18339fd66db6SSebastian Grimberg Note: `basis_project` will have the same number of components as `basis_from`, regardless of the number of components that `basis_to` has. 18349fd66db6SSebastian Grimberg If `basis_from` has 3 components and `basis_to` has 5 components, then `basis_project` will have 3 components. 1835f113e5dcSJeremy L Thompson 1836e104ad11SJames Wright Note: If either `basis_from` or `basis_to` are non-tensor, then `basis_project` will also be non-tensor 1837e104ad11SJames Wright 1838ca94c3ddSJeremy L Thompson @param[in] basis_from `CeedBasis` to prolong from 1839ca94c3ddSJeremy L Thompson @param[in] basis_to `CeedBasis` to prolong to 1840ca94c3ddSJeremy L Thompson @param[out] basis_project Address of the variable where the newly created `CeedBasis` will be stored 1841f113e5dcSJeremy L Thompson 1842f113e5dcSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1843f113e5dcSJeremy L Thompson 1844f113e5dcSJeremy L Thompson @ref User 1845f113e5dcSJeremy L Thompson **/ 18462b730f8bSJeremy L Thompson int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project) { 1847f113e5dcSJeremy L Thompson Ceed ceed; 1848e104ad11SJames Wright bool create_tensor; 18491c66c397SJeremy L Thompson CeedInt dim, num_comp; 1850097cc795SJames Wright CeedScalar *interp_project, *grad_project; 18511c66c397SJeremy L Thompson 18522b730f8bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis_to, &ceed)); 1853f113e5dcSJeremy L Thompson 1854ecc88aebSJeremy L Thompson // Create projection matrix 18552b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateProjectionMatrices(basis_from, basis_to, &interp_project, &grad_project)); 1856f113e5dcSJeremy L Thompson 1857f113e5dcSJeremy L Thompson // Build basis 1858e104ad11SJames Wright { 1859e104ad11SJames Wright bool is_tensor_to, is_tensor_from; 1860e104ad11SJames Wright 1861e104ad11SJames Wright CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to)); 1862e104ad11SJames Wright CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from)); 1863e104ad11SJames Wright create_tensor = is_tensor_from && is_tensor_to; 1864e104ad11SJames Wright } 18652b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_to, &dim)); 18662b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_from, &num_comp)); 1867e104ad11SJames Wright if (create_tensor) { 1868f113e5dcSJeremy L Thompson CeedInt P_1d_to, P_1d_from; 18691c66c397SJeremy L Thompson 18702b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_1d_from)); 18712b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_1d_to)); 1872097cc795SJames Wright CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, interp_project, grad_project, NULL, NULL, basis_project)); 1873f113e5dcSJeremy L Thompson } else { 1874de05fbb2SSebastian Grimberg // Even if basis_to and basis_from are not H1, the resulting basis is H1 for interpolation to work 1875f113e5dcSJeremy L Thompson CeedInt num_nodes_to, num_nodes_from; 18761c66c397SJeremy L Thompson CeedElemTopology topo; 18771c66c397SJeremy L Thompson 1878e00f3be8SJames Wright CeedCall(CeedBasisGetTopology(basis_from, &topo)); 18792b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_from, &num_nodes_from)); 18802b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_to, &num_nodes_to)); 1881097cc795SJames Wright CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, NULL, NULL, basis_project)); 1882f113e5dcSJeremy L Thompson } 1883f113e5dcSJeremy L Thompson 1884f113e5dcSJeremy L Thompson // Cleanup 18852b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_project)); 18862b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_project)); 18879bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 1888f113e5dcSJeremy L Thompson return CEED_ERROR_SUCCESS; 1889f113e5dcSJeremy L Thompson } 1890f113e5dcSJeremy L Thompson 1891f113e5dcSJeremy L Thompson /** 1892ca94c3ddSJeremy L Thompson @brief Copy the pointer to a `CeedBasis`. 18939560d06aSjeremylt 1894ca94c3ddSJeremy 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`. 1895ca94c3ddSJeremy L Thompson This `CeedBasis` will be destroyed if `*basis_copy` is the only reference to this `CeedBasis`. 1896ea61e9acSJeremy L Thompson 1897ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to copy reference to 1898ea61e9acSJeremy L Thompson @param[in,out] basis_copy Variable to store copied reference 18999560d06aSjeremylt 19009560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 19019560d06aSjeremylt 19029560d06aSjeremylt @ref User 19039560d06aSjeremylt **/ 19049560d06aSjeremylt int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) { 1905356036faSJeremy L Thompson if (basis != CEED_BASIS_NONE) CeedCall(CeedBasisReference(basis)); 19062b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(basis_copy)); 19079560d06aSjeremylt *basis_copy = basis; 19089560d06aSjeremylt return CEED_ERROR_SUCCESS; 19099560d06aSjeremylt } 19109560d06aSjeremylt 19119560d06aSjeremylt /** 1912*4c789ea2SJeremy L Thompson @brief Set the number of tabs to indent for @ref CeedBasisView() output 1913*4c789ea2SJeremy L Thompson 1914*4c789ea2SJeremy L Thompson @param[in] basis `CeedBasis` to set the number of view tabs 1915*4c789ea2SJeremy L Thompson @param[in] num_tabs Number of view tabs to set 1916*4c789ea2SJeremy L Thompson 1917*4c789ea2SJeremy L Thompson @return Error code: 0 - success, otherwise - failure 1918*4c789ea2SJeremy L Thompson 1919*4c789ea2SJeremy L Thompson @ref User 1920*4c789ea2SJeremy L Thompson **/ 1921*4c789ea2SJeremy L Thompson int CeedBasisSetNumViewTabs(CeedBasis basis, CeedInt num_tabs) { 1922*4c789ea2SJeremy L Thompson CeedCheck(num_tabs >= 0, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Number of view tabs must be non-negative"); 1923*4c789ea2SJeremy L Thompson basis->num_tabs = num_tabs; 1924*4c789ea2SJeremy L Thompson return CEED_ERROR_SUCCESS; 1925*4c789ea2SJeremy L Thompson } 1926*4c789ea2SJeremy L Thompson 1927*4c789ea2SJeremy L Thompson /** 1928ca94c3ddSJeremy L Thompson @brief View a `CeedBasis` 19297a982d89SJeremy L. Thompson 1930ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to view 1931ca94c3ddSJeremy L Thompson @param[in] stream Stream to view to, e.g., `stdout` 19327a982d89SJeremy L. Thompson 19337a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 19347a982d89SJeremy L. Thompson 19357a982d89SJeremy L. Thompson @ref User 19367a982d89SJeremy L. Thompson **/ 19377a982d89SJeremy L. Thompson int CeedBasisView(CeedBasis basis, FILE *stream) { 19381203703bSJeremy L Thompson bool is_tensor_basis; 1939*4c789ea2SJeremy L Thompson char *tabs = NULL; 19401203703bSJeremy L Thompson CeedElemTopology topo; 19411203703bSJeremy L Thompson CeedFESpace fe_space; 19421203703bSJeremy L Thompson 19431203703bSJeremy L Thompson // Basis data 19441203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 19451203703bSJeremy L Thompson CeedCall(CeedBasisGetTopology(basis, &topo)); 19461203703bSJeremy L Thompson CeedCall(CeedBasisGetFESpace(basis, &fe_space)); 19472b730f8bSJeremy L Thompson 1948*4c789ea2SJeremy L Thompson { 1949*4c789ea2SJeremy L Thompson CeedInt num_tabs = 0; 1950*4c789ea2SJeremy L Thompson 1951*4c789ea2SJeremy L Thompson CeedCall(CeedBasisGetNumViewTabs(basis, &num_tabs)); 1952*4c789ea2SJeremy L Thompson CeedCall(CeedCalloc(CEED_TAB_WIDTH * num_tabs + 1, &tabs)); 1953*4c789ea2SJeremy L Thompson for (CeedInt i = 0; i < CEED_TAB_WIDTH * num_tabs; i++) tabs[i] = ' '; 195450c301a5SRezgar Shakeri } 1955*4c789ea2SJeremy L Thompson 1956*4c789ea2SJeremy L Thompson // Print FE space and element topology of the basis 1957*4c789ea2SJeremy L Thompson fprintf(stream, "%sCeedBasis in a %s on a %s element\n", tabs, CeedFESpaces[fe_space], CeedElemTopologies[topo]); 1958*4c789ea2SJeremy L Thompson if (is_tensor_basis) { 1959*4c789ea2SJeremy L Thompson fprintf(stream, "%s P: %" CeedInt_FMT "\n%s Q: %" CeedInt_FMT "\n", tabs, basis->P_1d, tabs, basis->Q_1d); 1960*4c789ea2SJeremy L Thompson } else { 1961*4c789ea2SJeremy L Thompson fprintf(stream, "%s P: %" CeedInt_FMT "\n%s Q: %" CeedInt_FMT "\n", tabs, basis->P, tabs, basis->Q); 1962*4c789ea2SJeremy L Thompson } 1963*4c789ea2SJeremy L Thompson fprintf(stream, "%s dimension: %" CeedInt_FMT "\n%s field components: %" CeedInt_FMT "\n", tabs, basis->dim, tabs, basis->num_comp); 1964ea61e9acSJeremy L Thompson // Print quadrature data, interpolation/gradient/divergence/curl of the basis 19651203703bSJeremy L Thompson if (is_tensor_basis) { // tensor basis 19661203703bSJeremy L Thompson CeedInt P_1d, Q_1d; 19671203703bSJeremy L Thompson const CeedScalar *q_ref_1d, *q_weight_1d, *interp_1d, *grad_1d; 19681203703bSJeremy L Thompson 19691203703bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 19701203703bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 19711203703bSJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 19721203703bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d)); 19731203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 19741203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 19751203703bSJeremy L Thompson 1976*4c789ea2SJeremy L Thompson CeedCall(CeedScalarView("qref1d", "\t% 12.8f", 1, Q_1d, q_ref_1d, tabs, stream)); 1977*4c789ea2SJeremy L Thompson CeedCall(CeedScalarView("qweight1d", "\t% 12.8f", 1, Q_1d, q_weight_1d, tabs, stream)); 1978*4c789ea2SJeremy L Thompson CeedCall(CeedScalarView("interp1d", "\t% 12.8f", Q_1d, P_1d, interp_1d, tabs, stream)); 1979*4c789ea2SJeremy L Thompson CeedCall(CeedScalarView("grad1d", "\t% 12.8f", Q_1d, P_1d, grad_1d, tabs, stream)); 198050c301a5SRezgar Shakeri } else { // non-tensor basis 19811203703bSJeremy L Thompson CeedInt P, Q, dim, q_comp; 19821203703bSJeremy L Thompson const CeedScalar *q_ref, *q_weight, *interp, *grad, *div, *curl; 19831203703bSJeremy L Thompson 19841203703bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &P)); 19851203703bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &Q)); 19861203703bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 19871203703bSJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref)); 19881203703bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight)); 19891203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis, &interp)); 19901203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad(basis, &grad)); 19911203703bSJeremy L Thompson CeedCall(CeedBasisGetDiv(basis, &div)); 19921203703bSJeremy L Thompson CeedCall(CeedBasisGetCurl(basis, &curl)); 19931203703bSJeremy L Thompson 1994*4c789ea2SJeremy L Thompson CeedCall(CeedScalarView("qref", "\t% 12.8f", 1, Q * dim, q_ref, tabs, stream)); 1995*4c789ea2SJeremy L Thompson CeedCall(CeedScalarView("qweight", "\t% 12.8f", 1, Q, q_weight, tabs, stream)); 1996c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp)); 1997*4c789ea2SJeremy L Thompson CeedCall(CeedScalarView("interp", "\t% 12.8f", q_comp * Q, P, interp, tabs, stream)); 19981203703bSJeremy L Thompson if (grad) { 1999c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp)); 2000*4c789ea2SJeremy L Thompson CeedCall(CeedScalarView("grad", "\t% 12.8f", q_comp * Q, P, grad, tabs, stream)); 20017a982d89SJeremy L. Thompson } 20021203703bSJeremy L Thompson if (div) { 2003c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp)); 2004*4c789ea2SJeremy L Thompson CeedCall(CeedScalarView("div", "\t% 12.8f", q_comp * Q, P, div, tabs, stream)); 2005c4e3f59bSSebastian Grimberg } 20061203703bSJeremy L Thompson if (curl) { 2007c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp)); 2008*4c789ea2SJeremy L Thompson CeedCall(CeedScalarView("curl", "\t% 12.8f", q_comp * Q, P, curl, tabs, stream)); 200950c301a5SRezgar Shakeri } 201050c301a5SRezgar Shakeri } 2011*4c789ea2SJeremy L Thompson CeedCall(CeedFree(&tabs)); 2012e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20137a982d89SJeremy L. Thompson } 20147a982d89SJeremy L. Thompson 20157a982d89SJeremy L. Thompson /** 2016db2becc9SJeremy L Thompson @brief Apply basis evaluation from nodes to quadrature points or vice versa 2017db2becc9SJeremy L Thompson 2018db2becc9SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 2019db2becc9SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 2020db2becc9SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 2021db2becc9SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points; 2022db2becc9SJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes 2023db2becc9SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 2024db2becc9SJeremy L Thompson @ref CEED_EVAL_INTERP to use interpolated values, 2025db2becc9SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 2026db2becc9SJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 2027db2becc9SJeremy L Thompson @ref CEED_EVAL_CURL to use curl, 2028db2becc9SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 2029db2becc9SJeremy L Thompson @param[in] u Input `CeedVector` 2030db2becc9SJeremy L Thompson @param[out] v Output `CeedVector` 2031db2becc9SJeremy L Thompson 2032db2becc9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2033db2becc9SJeremy L Thompson 2034db2becc9SJeremy L Thompson @ref User 2035db2becc9SJeremy L Thompson **/ 2036db2becc9SJeremy L Thompson int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 2037db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyCheckDims(basis, num_elem, t_mode, eval_mode, u, v)); 2038db2becc9SJeremy L Thompson CeedCheck(basis->Apply, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedBasisApply"); 20392b730f8bSJeremy L Thompson CeedCall(basis->Apply(basis, num_elem, t_mode, eval_mode, u, v)); 2040e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20417a982d89SJeremy L. Thompson } 20427a982d89SJeremy L. Thompson 20437a982d89SJeremy L. Thompson /** 2044db2becc9SJeremy L Thompson @brief Apply basis evaluation from quadrature points to nodes and sum into target vector 2045db2becc9SJeremy L Thompson 2046db2becc9SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 2047db2becc9SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 2048db2becc9SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 2049db2becc9SJeremy L Thompson @param[in] t_mode @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes; 2050db2becc9SJeremy L Thompson @ref CEED_NOTRANSPOSE is not valid for `CeedBasisApplyAdd()` 2051db2becc9SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 2052db2becc9SJeremy L Thompson @ref CEED_EVAL_INTERP to use interpolated values, 2053db2becc9SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 2054db2becc9SJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 2055db2becc9SJeremy L Thompson @ref CEED_EVAL_CURL to use curl, 2056db2becc9SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 2057db2becc9SJeremy L Thompson @param[in] u Input `CeedVector` 2058db2becc9SJeremy L Thompson @param[out] v Output `CeedVector` to sum into 2059db2becc9SJeremy L Thompson 2060db2becc9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2061db2becc9SJeremy L Thompson 2062db2becc9SJeremy L Thompson @ref User 2063db2becc9SJeremy L Thompson **/ 2064db2becc9SJeremy L Thompson int CeedBasisApplyAdd(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 2065db2becc9SJeremy L Thompson CeedCheck(t_mode == CEED_TRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "CeedBasisApplyAdd only supports CEED_TRANSPOSE"); 2066db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyCheckDims(basis, num_elem, t_mode, eval_mode, u, v)); 2067db2becc9SJeremy L Thompson CeedCheck(basis->ApplyAdd, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedBasisApplyAdd"); 2068db2becc9SJeremy L Thompson CeedCall(basis->ApplyAdd(basis, num_elem, t_mode, eval_mode, u, v)); 2069db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 2070db2becc9SJeremy L Thompson } 2071db2becc9SJeremy L Thompson 2072db2becc9SJeremy L Thompson /** 2073db2becc9SJeremy L Thompson @brief Apply basis evaluation from nodes to arbitrary points 2074db2becc9SJeremy L Thompson 2075db2becc9SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 2076db2becc9SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 2077db2becc9SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 2078db2becc9SJeremy L Thompson @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 2079db2becc9SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 2080db2becc9SJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes 2081db2becc9SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 2082db2becc9SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 2083db2becc9SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 2084db2becc9SJeremy L Thompson @param[in] x_ref `CeedVector` holding reference coordinates of each point 2085db2becc9SJeremy L Thompson @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 2086db2becc9SJeremy L Thompson @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 2087db2becc9SJeremy L Thompson 2088db2becc9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2089db2becc9SJeremy L Thompson 2090db2becc9SJeremy L Thompson @ref User 2091db2becc9SJeremy L Thompson **/ 2092db2becc9SJeremy L Thompson int CeedBasisApplyAtPoints(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, 2093db2becc9SJeremy L Thompson CeedVector x_ref, CeedVector u, CeedVector v) { 2094db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyAtPointsCheckDims(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2095db2becc9SJeremy L Thompson if (basis->ApplyAtPoints) { 2096db2becc9SJeremy L Thompson CeedCall(basis->ApplyAtPoints(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2097db2becc9SJeremy L Thompson } else { 2098db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyAtPoints_Core(basis, false, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2099db2becc9SJeremy L Thompson } 2100db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 2101db2becc9SJeremy L Thompson } 2102db2becc9SJeremy L Thompson 2103db2becc9SJeremy L Thompson /** 2104db2becc9SJeremy L Thompson @brief Apply basis evaluation from nodes to arbitrary points and sum into target vector 2105db2becc9SJeremy L Thompson 2106db2becc9SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 2107db2becc9SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 2108db2becc9SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 2109db2becc9SJeremy L Thompson @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 2110db2becc9SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 2111db2becc9SJeremy L Thompson @ref CEED_NOTRANSPOSE is not valid for `CeedBasisApplyAddAtPoints()` 2112db2becc9SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 2113db2becc9SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 2114db2becc9SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 2115db2becc9SJeremy L Thompson @param[in] x_ref `CeedVector` holding reference coordinates of each point 2116db2becc9SJeremy L Thompson @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 2117db2becc9SJeremy L Thompson @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 2118db2becc9SJeremy L Thompson 2119db2becc9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2120db2becc9SJeremy L Thompson 2121db2becc9SJeremy L Thompson @ref User 2122db2becc9SJeremy L Thompson **/ 2123db2becc9SJeremy L Thompson int CeedBasisApplyAddAtPoints(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, 2124db2becc9SJeremy L Thompson CeedVector x_ref, CeedVector u, CeedVector v) { 2125db2becc9SJeremy L Thompson CeedCheck(t_mode == CEED_TRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "CeedBasisApplyAddAtPoints only supports CEED_TRANSPOSE"); 2126db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyAtPointsCheckDims(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2127db2becc9SJeremy L Thompson if (basis->ApplyAddAtPoints) { 2128db2becc9SJeremy L Thompson CeedCall(basis->ApplyAddAtPoints(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2129db2becc9SJeremy L Thompson } else { 2130db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyAtPoints_Core(basis, true, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2131db2becc9SJeremy L Thompson } 2132db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 2133db2becc9SJeremy L Thompson } 2134db2becc9SJeremy L Thompson 2135db2becc9SJeremy L Thompson /** 21366e536b99SJeremy L Thompson @brief Get the `Ceed` associated with a `CeedBasis` 2137b7c9bbdaSJeremy L Thompson 2138ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2139ca94c3ddSJeremy L Thompson @param[out] ceed Variable to store `Ceed` 2140b7c9bbdaSJeremy L Thompson 2141b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2142b7c9bbdaSJeremy L Thompson 2143b7c9bbdaSJeremy L Thompson @ref Advanced 2144b7c9bbdaSJeremy L Thompson **/ 2145b7c9bbdaSJeremy L Thompson int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) { 21469bc66399SJeremy L Thompson *ceed = NULL; 21479bc66399SJeremy L Thompson CeedCall(CeedReferenceCopy(CeedBasisReturnCeed(basis), ceed)); 2148b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 2149b7c9bbdaSJeremy L Thompson } 2150b7c9bbdaSJeremy L Thompson 2151b7c9bbdaSJeremy L Thompson /** 21526e536b99SJeremy L Thompson @brief Return the `Ceed` associated with a `CeedBasis` 21536e536b99SJeremy L Thompson 21546e536b99SJeremy L Thompson @param[in] basis `CeedBasis` 21556e536b99SJeremy L Thompson 21566e536b99SJeremy L Thompson @return `Ceed` associated with the `basis` 21576e536b99SJeremy L Thompson 21586e536b99SJeremy L Thompson @ref Advanced 21596e536b99SJeremy L Thompson **/ 21606e536b99SJeremy L Thompson Ceed CeedBasisReturnCeed(CeedBasis basis) { return basis->ceed; } 21616e536b99SJeremy L Thompson 21626e536b99SJeremy L Thompson /** 2163ca94c3ddSJeremy L Thompson @brief Get dimension for given `CeedBasis` 21649d007619Sjeremylt 2165ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 21669d007619Sjeremylt @param[out] dim Variable to store dimension of basis 21679d007619Sjeremylt 21689d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21699d007619Sjeremylt 2170b7c9bbdaSJeremy L Thompson @ref Advanced 21719d007619Sjeremylt **/ 21729d007619Sjeremylt int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) { 21739d007619Sjeremylt *dim = basis->dim; 2174e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21759d007619Sjeremylt } 21769d007619Sjeremylt 21779d007619Sjeremylt /** 2178ca94c3ddSJeremy L Thompson @brief Get topology for given `CeedBasis` 2179d99fa3c5SJeremy L Thompson 2180ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2181d99fa3c5SJeremy L Thompson @param[out] topo Variable to store topology of basis 2182d99fa3c5SJeremy L Thompson 2183d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2184d99fa3c5SJeremy L Thompson 2185b7c9bbdaSJeremy L Thompson @ref Advanced 2186d99fa3c5SJeremy L Thompson **/ 2187d99fa3c5SJeremy L Thompson int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) { 2188d99fa3c5SJeremy L Thompson *topo = basis->topo; 2189e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2190d99fa3c5SJeremy L Thompson } 2191d99fa3c5SJeremy L Thompson 2192d99fa3c5SJeremy L Thompson /** 2193ca94c3ddSJeremy L Thompson @brief Get number of components for given `CeedBasis` 21949d007619Sjeremylt 2195ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2196ca94c3ddSJeremy L Thompson @param[out] num_comp Variable to store number of components 21979d007619Sjeremylt 21989d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21999d007619Sjeremylt 2200b7c9bbdaSJeremy L Thompson @ref Advanced 22019d007619Sjeremylt **/ 2202d1d35e2fSjeremylt int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) { 2203d1d35e2fSjeremylt *num_comp = basis->num_comp; 2204e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 22059d007619Sjeremylt } 22069d007619Sjeremylt 22079d007619Sjeremylt /** 2208ca94c3ddSJeremy L Thompson @brief Get total number of nodes (in `dim` dimensions) of a `CeedBasis` 22099d007619Sjeremylt 2210ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 22119d007619Sjeremylt @param[out] P Variable to store number of nodes 22129d007619Sjeremylt 22139d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 22149d007619Sjeremylt 22159d007619Sjeremylt @ref Utility 22169d007619Sjeremylt **/ 22179d007619Sjeremylt int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) { 22189d007619Sjeremylt *P = basis->P; 2219e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 22209d007619Sjeremylt } 22219d007619Sjeremylt 22229d007619Sjeremylt /** 2223ca94c3ddSJeremy L Thompson @brief Get total number of nodes (in 1 dimension) of a `CeedBasis` 22249d007619Sjeremylt 2225ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2226d1d35e2fSjeremylt @param[out] P_1d Variable to store number of nodes 22279d007619Sjeremylt 22289d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 22299d007619Sjeremylt 2230b7c9bbdaSJeremy L Thompson @ref Advanced 22319d007619Sjeremylt **/ 2232d1d35e2fSjeremylt int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) { 22336e536b99SJeremy L Thompson CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply P_1d for non-tensor CeedBasis"); 2234d1d35e2fSjeremylt *P_1d = basis->P_1d; 2235e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 22369d007619Sjeremylt } 22379d007619Sjeremylt 22389d007619Sjeremylt /** 2239ca94c3ddSJeremy L Thompson @brief Get total number of quadrature points (in `dim` dimensions) of a `CeedBasis` 22409d007619Sjeremylt 2241ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 22429d007619Sjeremylt @param[out] Q Variable to store number of quadrature points 22439d007619Sjeremylt 22449d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 22459d007619Sjeremylt 22469d007619Sjeremylt @ref Utility 22479d007619Sjeremylt **/ 22489d007619Sjeremylt int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) { 22499d007619Sjeremylt *Q = basis->Q; 2250e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 22519d007619Sjeremylt } 22529d007619Sjeremylt 22539d007619Sjeremylt /** 2254ca94c3ddSJeremy L Thompson @brief Get total number of quadrature points (in 1 dimension) of a `CeedBasis` 22559d007619Sjeremylt 2256ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2257d1d35e2fSjeremylt @param[out] Q_1d Variable to store number of quadrature points 22589d007619Sjeremylt 22599d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 22609d007619Sjeremylt 2261b7c9bbdaSJeremy L Thompson @ref Advanced 22629d007619Sjeremylt **/ 2263d1d35e2fSjeremylt int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) { 22646e536b99SJeremy L Thompson CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply Q_1d for non-tensor CeedBasis"); 2265d1d35e2fSjeremylt *Q_1d = basis->Q_1d; 2266e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 22679d007619Sjeremylt } 22689d007619Sjeremylt 22699d007619Sjeremylt /** 2270ca94c3ddSJeremy L Thompson @brief Get reference coordinates of quadrature points (in `dim` dimensions) of a `CeedBasis` 22719d007619Sjeremylt 2272ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2273d1d35e2fSjeremylt @param[out] q_ref Variable to store reference coordinates of quadrature points 22749d007619Sjeremylt 22759d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 22769d007619Sjeremylt 2277b7c9bbdaSJeremy L Thompson @ref Advanced 22789d007619Sjeremylt **/ 2279d1d35e2fSjeremylt int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) { 2280d1d35e2fSjeremylt *q_ref = basis->q_ref_1d; 2281e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 22829d007619Sjeremylt } 22839d007619Sjeremylt 22849d007619Sjeremylt /** 2285ca94c3ddSJeremy L Thompson @brief Get quadrature weights of quadrature points (in `dim` dimensions) of a `CeedBasis` 22869d007619Sjeremylt 2287ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2288d1d35e2fSjeremylt @param[out] q_weight Variable to store quadrature weights 22899d007619Sjeremylt 22909d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 22919d007619Sjeremylt 2292b7c9bbdaSJeremy L Thompson @ref Advanced 22939d007619Sjeremylt **/ 2294d1d35e2fSjeremylt int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) { 2295d1d35e2fSjeremylt *q_weight = basis->q_weight_1d; 2296e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 22979d007619Sjeremylt } 22989d007619Sjeremylt 22999d007619Sjeremylt /** 2300ca94c3ddSJeremy L Thompson @brief Get interpolation matrix of a `CeedBasis` 23019d007619Sjeremylt 2302ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 23039d007619Sjeremylt @param[out] interp Variable to store interpolation matrix 23049d007619Sjeremylt 23059d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 23069d007619Sjeremylt 2307b7c9bbdaSJeremy L Thompson @ref Advanced 23089d007619Sjeremylt **/ 23096c58de82SJeremy L Thompson int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) { 23106402da51SJeremy L Thompson if (!basis->interp && basis->is_tensor_basis) { 23119d007619Sjeremylt // Allocate 23122b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->Q * basis->P, &basis->interp)); 23139d007619Sjeremylt 23149d007619Sjeremylt // Initialize 23152b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->Q * basis->P; i++) basis->interp[i] = 1.0; 23169d007619Sjeremylt 23179d007619Sjeremylt // Calculate 23182b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 23192b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 23209d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 2321d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 2322d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 23231c66c397SJeremy L Thompson 2324d1d35e2fSjeremylt basis->interp[qpt * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 23259d007619Sjeremylt } 23269d007619Sjeremylt } 23272b730f8bSJeremy L Thompson } 23282b730f8bSJeremy L Thompson } 23299d007619Sjeremylt *interp = basis->interp; 2330e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 23319d007619Sjeremylt } 23329d007619Sjeremylt 23339d007619Sjeremylt /** 2334ca94c3ddSJeremy L Thompson @brief Get 1D interpolation matrix of a tensor product `CeedBasis` 23359d007619Sjeremylt 2336ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2337d1d35e2fSjeremylt @param[out] interp_1d Variable to store interpolation matrix 23389d007619Sjeremylt 23399d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 23409d007619Sjeremylt 23419d007619Sjeremylt @ref Backend 23429d007619Sjeremylt **/ 2343d1d35e2fSjeremylt int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) { 23441203703bSJeremy L Thompson bool is_tensor_basis; 23451203703bSJeremy L Thompson 23461203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 23476e536b99SJeremy L Thompson CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis"); 2348d1d35e2fSjeremylt *interp_1d = basis->interp_1d; 2349e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 23509d007619Sjeremylt } 23519d007619Sjeremylt 23529d007619Sjeremylt /** 2353ca94c3ddSJeremy L Thompson @brief Get gradient matrix of a `CeedBasis` 23549d007619Sjeremylt 2355ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 23569d007619Sjeremylt @param[out] grad Variable to store gradient matrix 23579d007619Sjeremylt 23589d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 23599d007619Sjeremylt 2360b7c9bbdaSJeremy L Thompson @ref Advanced 23619d007619Sjeremylt **/ 23626c58de82SJeremy L Thompson int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) { 23636402da51SJeremy L Thompson if (!basis->grad && basis->is_tensor_basis) { 23649d007619Sjeremylt // Allocate 23652b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->dim * basis->Q * basis->P, &basis->grad)); 23669d007619Sjeremylt 23679d007619Sjeremylt // Initialize 23682b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim * basis->Q * basis->P; i++) basis->grad[i] = 1.0; 23699d007619Sjeremylt 23709d007619Sjeremylt // Calculate 23712b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 23722b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim; i++) { 23732b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 23749d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 2375d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 2376d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 23771c66c397SJeremy L Thompson 23782b730f8bSJeremy L Thompson if (i == d) basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->grad_1d[q * basis->P_1d + p]; 23792b730f8bSJeremy L Thompson else basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 23802b730f8bSJeremy L Thompson } 23812b730f8bSJeremy L Thompson } 23822b730f8bSJeremy L Thompson } 23839d007619Sjeremylt } 23849d007619Sjeremylt } 23859d007619Sjeremylt *grad = basis->grad; 2386e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 23879d007619Sjeremylt } 23889d007619Sjeremylt 23899d007619Sjeremylt /** 2390ca94c3ddSJeremy L Thompson @brief Get 1D gradient matrix of a tensor product `CeedBasis` 23919d007619Sjeremylt 2392ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2393d1d35e2fSjeremylt @param[out] grad_1d Variable to store gradient matrix 23949d007619Sjeremylt 23959d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 23969d007619Sjeremylt 2397b7c9bbdaSJeremy L Thompson @ref Advanced 23989d007619Sjeremylt **/ 2399d1d35e2fSjeremylt int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) { 24001203703bSJeremy L Thompson bool is_tensor_basis; 24011203703bSJeremy L Thompson 24021203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 24036e536b99SJeremy L Thompson CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis"); 2404d1d35e2fSjeremylt *grad_1d = basis->grad_1d; 2405e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 24069d007619Sjeremylt } 24079d007619Sjeremylt 24089d007619Sjeremylt /** 2409ca94c3ddSJeremy L Thompson @brief Get divergence matrix of a `CeedBasis` 241050c301a5SRezgar Shakeri 2411ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 241250c301a5SRezgar Shakeri @param[out] div Variable to store divergence matrix 241350c301a5SRezgar Shakeri 241450c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 241550c301a5SRezgar Shakeri 241650c301a5SRezgar Shakeri @ref Advanced 241750c301a5SRezgar Shakeri **/ 241850c301a5SRezgar Shakeri int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) { 241950c301a5SRezgar Shakeri *div = basis->div; 242050c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 242150c301a5SRezgar Shakeri } 242250c301a5SRezgar Shakeri 242350c301a5SRezgar Shakeri /** 2424ca94c3ddSJeremy L Thompson @brief Get curl matrix of a `CeedBasis` 2425c4e3f59bSSebastian Grimberg 2426ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2427c4e3f59bSSebastian Grimberg @param[out] curl Variable to store curl matrix 2428c4e3f59bSSebastian Grimberg 2429c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 2430c4e3f59bSSebastian Grimberg 2431c4e3f59bSSebastian Grimberg @ref Advanced 2432c4e3f59bSSebastian Grimberg **/ 2433c4e3f59bSSebastian Grimberg int CeedBasisGetCurl(CeedBasis basis, const CeedScalar **curl) { 2434c4e3f59bSSebastian Grimberg *curl = basis->curl; 2435c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 2436c4e3f59bSSebastian Grimberg } 2437c4e3f59bSSebastian Grimberg 2438c4e3f59bSSebastian Grimberg /** 2439ca94c3ddSJeremy L Thompson @brief Destroy a @ref CeedBasis 24407a982d89SJeremy L. Thompson 2441ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` to destroy 24427a982d89SJeremy L. Thompson 24437a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 24447a982d89SJeremy L. Thompson 24457a982d89SJeremy L. Thompson @ref User 24467a982d89SJeremy L. Thompson **/ 24477a982d89SJeremy L. Thompson int CeedBasisDestroy(CeedBasis *basis) { 2448356036faSJeremy L Thompson if (!*basis || *basis == CEED_BASIS_NONE || --(*basis)->ref_count > 0) { 2449ad6481ceSJeremy L Thompson *basis = NULL; 2450ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 2451ad6481ceSJeremy L Thompson } 24522b730f8bSJeremy L Thompson if ((*basis)->Destroy) CeedCall((*basis)->Destroy(*basis)); 24539831d45aSJeremy L Thompson CeedCall(CeedTensorContractDestroy(&(*basis)->contract)); 2454c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_ref_1d)); 2455c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_weight_1d)); 24562b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp)); 24572b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp_1d)); 24582b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad)); 24592b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad_1d)); 2460c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->div)); 2461c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->curl)); 2462c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorDestroy(&(*basis)->vec_chebyshev)); 2463c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisDestroy(&(*basis)->basis_chebyshev)); 24642b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*basis)->ceed)); 24652b730f8bSJeremy L Thompson CeedCall(CeedFree(basis)); 2466e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 24677a982d89SJeremy L. Thompson } 24687a982d89SJeremy L. Thompson 24697a982d89SJeremy L. Thompson /** 2470b11c1e72Sjeremylt @brief Construct a Gauss-Legendre quadrature 2471b11c1e72Sjeremylt 2472ca94c3ddSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree `2*Q-1` exactly) 2473ca94c3ddSJeremy L Thompson @param[out] q_ref_1d Array of length `Q` to hold the abscissa on `[-1, 1]` 2474ca94c3ddSJeremy L Thompson @param[out] q_weight_1d Array of length `Q` to hold the weights 2475b11c1e72Sjeremylt 2476b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 2477dfdf5a53Sjeremylt 2478dfdf5a53Sjeremylt @ref Utility 2479b11c1e72Sjeremylt **/ 24802b730f8bSJeremy L Thompson int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2481d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0 * atan(1.0); 24821c66c397SJeremy L Thompson 2483d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 248492ae7e47SJeremy L Thompson for (CeedInt i = 0; i <= Q / 2; i++) { 2485d7b241e6Sjeremylt // Guess 2486d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(2 * i + 1) / ((CeedScalar)(2 * Q))); 2487d7b241e6Sjeremylt // Pn(xi) 2488d7b241e6Sjeremylt P0 = 1.0; 2489d7b241e6Sjeremylt P1 = xi; 2490d7b241e6Sjeremylt P2 = 0.0; 249192ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 2492d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2493d7b241e6Sjeremylt P0 = P1; 2494d7b241e6Sjeremylt P1 = P2; 2495d7b241e6Sjeremylt } 2496d7b241e6Sjeremylt // First Newton Step 2497d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2498d7b241e6Sjeremylt xi = xi - P2 / dP2; 2499d7b241e6Sjeremylt // Newton to convergence 250092ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(P2) > 10 * CEED_EPSILON; k++) { 2501d7b241e6Sjeremylt P0 = 1.0; 2502d7b241e6Sjeremylt P1 = xi; 250392ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 2504d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2505d7b241e6Sjeremylt P0 = P1; 2506d7b241e6Sjeremylt P1 = P2; 2507d7b241e6Sjeremylt } 2508d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2509d7b241e6Sjeremylt xi = xi - P2 / dP2; 2510d7b241e6Sjeremylt } 2511d7b241e6Sjeremylt // Save xi, wi 2512d7b241e6Sjeremylt wi = 2.0 / ((1.0 - xi * xi) * dP2 * dP2); 2513d1d35e2fSjeremylt q_weight_1d[i] = wi; 2514d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 2515d1d35e2fSjeremylt q_ref_1d[i] = -xi; 2516d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 2517d7b241e6Sjeremylt } 2518e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2519d7b241e6Sjeremylt } 2520d7b241e6Sjeremylt 2521b11c1e72Sjeremylt /** 2522b11c1e72Sjeremylt @brief Construct a Gauss-Legendre-Lobatto quadrature 2523b11c1e72Sjeremylt 2524ca94c3ddSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree `2*Q-3` exactly) 2525ca94c3ddSJeremy L Thompson @param[out] q_ref_1d Array of length `Q` to hold the abscissa on `[-1, 1]` 2526ca94c3ddSJeremy L Thompson @param[out] q_weight_1d Array of length `Q` to hold the weights 2527b11c1e72Sjeremylt 2528b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 2529dfdf5a53Sjeremylt 2530dfdf5a53Sjeremylt @ref Utility 2531b11c1e72Sjeremylt **/ 25322b730f8bSJeremy L Thompson int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2533d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0 * atan(1.0); 25341c66c397SJeremy L Thompson 2535d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 2536d7b241e6Sjeremylt // Set endpoints 25376574a04fSJeremy L Thompson CeedCheck(Q > 1, NULL, CEED_ERROR_DIMENSION, "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q); 2538d7b241e6Sjeremylt wi = 2.0 / ((CeedScalar)(Q * (Q - 1))); 2539d1d35e2fSjeremylt if (q_weight_1d) { 2540d1d35e2fSjeremylt q_weight_1d[0] = wi; 2541d1d35e2fSjeremylt q_weight_1d[Q - 1] = wi; 2542d7b241e6Sjeremylt } 2543d1d35e2fSjeremylt q_ref_1d[0] = -1.0; 2544d1d35e2fSjeremylt q_ref_1d[Q - 1] = 1.0; 2545d7b241e6Sjeremylt // Interior 254692ae7e47SJeremy L Thompson for (CeedInt i = 1; i <= (Q - 1) / 2; i++) { 2547d7b241e6Sjeremylt // Guess 2548d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(i) / (CeedScalar)(Q - 1)); 2549d7b241e6Sjeremylt // Pn(xi) 2550d7b241e6Sjeremylt P0 = 1.0; 2551d7b241e6Sjeremylt P1 = xi; 2552d7b241e6Sjeremylt P2 = 0.0; 255392ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 2554d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2555d7b241e6Sjeremylt P0 = P1; 2556d7b241e6Sjeremylt P1 = P2; 2557d7b241e6Sjeremylt } 2558d7b241e6Sjeremylt // First Newton step 2559d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2560d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2561d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 2562d7b241e6Sjeremylt // Newton to convergence 256392ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(dP2) > 10 * CEED_EPSILON; k++) { 2564d7b241e6Sjeremylt P0 = 1.0; 2565d7b241e6Sjeremylt P1 = xi; 256692ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 2567d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2568d7b241e6Sjeremylt P0 = P1; 2569d7b241e6Sjeremylt P1 = P2; 2570d7b241e6Sjeremylt } 2571d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2572d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2573d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 2574d7b241e6Sjeremylt } 2575d7b241e6Sjeremylt // Save xi, wi 2576d7b241e6Sjeremylt wi = 2.0 / (((CeedScalar)(Q * (Q - 1))) * P2 * P2); 2577d1d35e2fSjeremylt if (q_weight_1d) { 2578d1d35e2fSjeremylt q_weight_1d[i] = wi; 2579d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 2580d7b241e6Sjeremylt } 2581d1d35e2fSjeremylt q_ref_1d[i] = -xi; 2582d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 2583d7b241e6Sjeremylt } 2584e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2585d7b241e6Sjeremylt } 2586d7b241e6Sjeremylt 2587d7b241e6Sjeremylt /// @} 2588