15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3d7b241e6Sjeremylt // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5d7b241e6Sjeremylt // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7d7b241e6Sjeremylt 83d576824SJeremy L Thompson #include <ceed-impl.h> 949aac155SJeremy L Thompson #include <ceed.h> 102b730f8bSJeremy L Thompson #include <ceed/backend.h> 11d7b241e6Sjeremylt #include <math.h> 123d576824SJeremy L Thompson #include <stdbool.h> 13d7b241e6Sjeremylt #include <stdio.h> 14d7b241e6Sjeremylt #include <string.h> 15d7b241e6Sjeremylt 167a982d89SJeremy L. Thompson /// @file 177a982d89SJeremy L. Thompson /// Implementation of CeedBasis interfaces 187a982d89SJeremy L. Thompson 19d7b241e6Sjeremylt /// @cond DOXYGEN_SKIP 20356036faSJeremy L Thompson static struct CeedBasis_private ceed_basis_none; 21d7b241e6Sjeremylt /// @endcond 22d7b241e6Sjeremylt 237a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser 247a982d89SJeremy L. Thompson /// @{ 257a982d89SJeremy L. Thompson 26ca94c3ddSJeremy L Thompson /// Argument for @ref CeedOperatorSetField() indicating that the field does not require a `CeedBasis` 27356036faSJeremy L Thompson const CeedBasis CEED_BASIS_NONE = &ceed_basis_none; 28356036faSJeremy L Thompson 297a982d89SJeremy L. Thompson /// @} 307a982d89SJeremy L. Thompson 317a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 327a982d89SJeremy L. Thompson /// CeedBasis Library Internal Functions 337a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 347a982d89SJeremy L. Thompson /// @addtogroup CeedBasisDeveloper 357a982d89SJeremy L. Thompson /// @{ 367a982d89SJeremy L. Thompson 377a982d89SJeremy L. Thompson /** 383778dbaaSJeremy L Thompson @brief Compute Chebyshev polynomial values at a point 393778dbaaSJeremy L Thompson 403778dbaaSJeremy L Thompson @param[in] x Coordinate to evaluate Chebyshev polynomials at 41ca94c3ddSJeremy L Thompson @param[in] n Number of Chebyshev polynomials to evaluate, `n >= 2` 423778dbaaSJeremy L Thompson @param[out] chebyshev_x Array of Chebyshev polynomial values 433778dbaaSJeremy L Thompson 443778dbaaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 453778dbaaSJeremy L Thompson 463778dbaaSJeremy L Thompson @ref Developer 473778dbaaSJeremy L Thompson **/ 483778dbaaSJeremy L Thompson static int CeedChebyshevPolynomialsAtPoint(CeedScalar x, CeedInt n, CeedScalar *chebyshev_x) { 493778dbaaSJeremy L Thompson chebyshev_x[0] = 1.0; 503778dbaaSJeremy L Thompson chebyshev_x[1] = 2 * x; 513778dbaaSJeremy L Thompson for (CeedInt i = 2; i < n; i++) chebyshev_x[i] = 2 * x * chebyshev_x[i - 1] - chebyshev_x[i - 2]; 523778dbaaSJeremy L Thompson return CEED_ERROR_SUCCESS; 533778dbaaSJeremy L Thompson } 543778dbaaSJeremy L Thompson 553778dbaaSJeremy L Thompson /** 563778dbaaSJeremy L Thompson @brief Compute values of the derivative of Chebyshev polynomials at a point 573778dbaaSJeremy L Thompson 583778dbaaSJeremy L Thompson @param[in] x Coordinate to evaluate derivative of Chebyshev polynomials at 59ca94c3ddSJeremy L Thompson @param[in] n Number of Chebyshev polynomials to evaluate, `n >= 2` 606cec60aaSJed Brown @param[out] chebyshev_dx Array of Chebyshev polynomial derivative values 613778dbaaSJeremy L Thompson 623778dbaaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 633778dbaaSJeremy L Thompson 643778dbaaSJeremy L Thompson @ref Developer 653778dbaaSJeremy L Thompson **/ 663778dbaaSJeremy L Thompson static int CeedChebyshevDerivativeAtPoint(CeedScalar x, CeedInt n, CeedScalar *chebyshev_dx) { 673778dbaaSJeremy L Thompson CeedScalar chebyshev_x[3]; 683778dbaaSJeremy L Thompson 693778dbaaSJeremy L Thompson chebyshev_x[1] = 1.0; 703778dbaaSJeremy L Thompson chebyshev_x[2] = 2 * x; 713778dbaaSJeremy L Thompson chebyshev_dx[0] = 0.0; 723778dbaaSJeremy L Thompson chebyshev_dx[1] = 2.0; 733778dbaaSJeremy L Thompson for (CeedInt i = 2; i < n; i++) { 743778dbaaSJeremy L Thompson chebyshev_x[0] = chebyshev_x[1]; 753778dbaaSJeremy L Thompson chebyshev_x[1] = chebyshev_x[2]; 763778dbaaSJeremy L Thompson chebyshev_x[2] = 2 * x * chebyshev_x[1] - chebyshev_x[0]; 773778dbaaSJeremy L Thompson chebyshev_dx[i] = 2 * x * chebyshev_dx[i - 1] + 2 * chebyshev_x[1] - chebyshev_dx[i - 2]; 783778dbaaSJeremy L Thompson } 793778dbaaSJeremy L Thompson return CEED_ERROR_SUCCESS; 803778dbaaSJeremy L Thompson } 813778dbaaSJeremy L Thompson 823778dbaaSJeremy L Thompson /** 83ca94c3ddSJeremy L Thompson @brief Compute Householder reflection. 847a982d89SJeremy L. Thompson 85ca94c3ddSJeremy L Thompson Computes \f$A = (I - b v v^T) A\f$, where \f$A\f$ is an \f$m \times n\f$ matrix indexed as `A[i*row + j*col]`. 867a982d89SJeremy L. Thompson 877a982d89SJeremy L. Thompson @param[in,out] A Matrix to apply Householder reflection to, in place 88ea61e9acSJeremy L Thompson @param[in] v Householder vector 89ea61e9acSJeremy L Thompson @param[in] b Scaling factor 90ca94c3ddSJeremy L Thompson @param[in] m Number of rows in `A` 91ca94c3ddSJeremy L Thompson @param[in] n Number of columns in `A` 92ea61e9acSJeremy L Thompson @param[in] row Row stride 93ea61e9acSJeremy L Thompson @param[in] col Col stride 947a982d89SJeremy L. Thompson 957a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 967a982d89SJeremy L. Thompson 977a982d89SJeremy L. Thompson @ref Developer 987a982d89SJeremy L. Thompson **/ 992b730f8bSJeremy L Thompson static int CeedHouseholderReflect(CeedScalar *A, const CeedScalar *v, CeedScalar b, CeedInt m, CeedInt n, CeedInt row, CeedInt col) { 1007a982d89SJeremy L. Thompson for (CeedInt j = 0; j < n; j++) { 1017a982d89SJeremy L. Thompson CeedScalar w = A[0 * row + j * col]; 1021c66c397SJeremy L Thompson 1032b730f8bSJeremy L Thompson for (CeedInt i = 1; i < m; i++) w += v[i] * A[i * row + j * col]; 1047a982d89SJeremy L. Thompson A[0 * row + j * col] -= b * w; 1052b730f8bSJeremy L Thompson for (CeedInt i = 1; i < m; i++) A[i * row + j * col] -= b * w * v[i]; 1067a982d89SJeremy L. Thompson } 107e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1087a982d89SJeremy L. Thompson } 1097a982d89SJeremy L. Thompson 1107a982d89SJeremy L. Thompson /** 1117a982d89SJeremy L. Thompson @brief Compute Givens rotation 1127a982d89SJeremy L. Thompson 113ca94c3ddSJeremy L Thompson Computes \f$A = G A\f$ (or \f$G^T A\f$ in transpose mode), where \f$A\f$ is an \f$m \times n\f$ matrix indexed as `A[i*n + j*m]`. 1147a982d89SJeremy L. Thompson 1157a982d89SJeremy L. Thompson @param[in,out] A Row major matrix to apply Givens rotation to, in place 116ea61e9acSJeremy L Thompson @param[in] c Cosine factor 117ea61e9acSJeremy L Thompson @param[in] s Sine factor 118ca94c3ddSJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to rotate the basis counter-clockwise, which has the effect of rotating columns of `A` clockwise; 1194cc79fe7SJed Brown @ref CEED_TRANSPOSE for the opposite rotation 120ea61e9acSJeremy L Thompson @param[in] i First row/column to apply rotation 121ea61e9acSJeremy L Thompson @param[in] k Second row/column to apply rotation 122ca94c3ddSJeremy L Thompson @param[in] m Number of rows in `A` 123ca94c3ddSJeremy L Thompson @param[in] n Number of columns in `A` 1247a982d89SJeremy L. Thompson 1257a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1267a982d89SJeremy L. Thompson 1277a982d89SJeremy L. Thompson @ref Developer 1287a982d89SJeremy L. Thompson **/ 1292b730f8bSJeremy L Thompson static int CeedGivensRotation(CeedScalar *A, CeedScalar c, CeedScalar s, CeedTransposeMode t_mode, CeedInt i, CeedInt k, CeedInt m, CeedInt n) { 130d1d35e2fSjeremylt CeedInt stride_j = 1, stride_ik = m, num_its = n; 1311c66c397SJeremy L Thompson 132d1d35e2fSjeremylt if (t_mode == CEED_NOTRANSPOSE) { 1332b730f8bSJeremy L Thompson stride_j = n; 1342b730f8bSJeremy L Thompson stride_ik = 1; 1352b730f8bSJeremy L Thompson num_its = m; 1367a982d89SJeremy L. Thompson } 1377a982d89SJeremy L. Thompson 1387a982d89SJeremy L. Thompson // Apply rotation 139d1d35e2fSjeremylt for (CeedInt j = 0; j < num_its; j++) { 140d1d35e2fSjeremylt CeedScalar tau1 = A[i * stride_ik + j * stride_j], tau2 = A[k * stride_ik + j * stride_j]; 1411c66c397SJeremy L Thompson 142d1d35e2fSjeremylt A[i * stride_ik + j * stride_j] = c * tau1 - s * tau2; 143d1d35e2fSjeremylt A[k * stride_ik + j * stride_j] = s * tau1 + c * tau2; 1447a982d89SJeremy L. Thompson } 145e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1467a982d89SJeremy L. Thompson } 1477a982d89SJeremy L. Thompson 1487a982d89SJeremy L. Thompson /** 149ca94c3ddSJeremy L Thompson @brief View an array stored in a `CeedBasis` 1507a982d89SJeremy L. Thompson 1510a0da059Sjeremylt @param[in] name Name of array 152d1d35e2fSjeremylt @param[in] fp_fmt Printing format 1530a0da059Sjeremylt @param[in] m Number of rows in array 1540a0da059Sjeremylt @param[in] n Number of columns in array 1550a0da059Sjeremylt @param[in] a Array to be viewed 156ca94c3ddSJeremy L Thompson @param[in] stream Stream to view to, e.g., `stdout` 1577a982d89SJeremy L. Thompson 1587a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1597a982d89SJeremy L. Thompson 1607a982d89SJeremy L. Thompson @ref Developer 1617a982d89SJeremy L. Thompson **/ 1622b730f8bSJeremy L Thompson static int CeedScalarView(const char *name, const char *fp_fmt, CeedInt m, CeedInt n, const CeedScalar *a, FILE *stream) { 163edf04919SJeremy L Thompson if (m > 1) { 164edf04919SJeremy L Thompson fprintf(stream, " %s:\n", name); 165edf04919SJeremy L Thompson } else { 166edf04919SJeremy L Thompson char padded_name[12]; 167edf04919SJeremy L Thompson 168edf04919SJeremy L Thompson snprintf(padded_name, 11, "%s:", name); 169edf04919SJeremy L Thompson fprintf(stream, " %-10s", padded_name); 170edf04919SJeremy L Thompson } 17192ae7e47SJeremy L Thompson for (CeedInt i = 0; i < m; i++) { 172edf04919SJeremy L Thompson if (m > 1) fprintf(stream, " [%" CeedInt_FMT "]", i); 1732b730f8bSJeremy L Thompson for (CeedInt j = 0; j < n; j++) fprintf(stream, fp_fmt, fabs(a[i * n + j]) > 1E-14 ? a[i * n + j] : 0); 1747a982d89SJeremy L. Thompson fputs("\n", stream); 1757a982d89SJeremy L. Thompson } 176e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1777a982d89SJeremy L. Thompson } 1787a982d89SJeremy L. Thompson 179a76a04e7SJeremy L Thompson /** 180ea61e9acSJeremy L Thompson @brief Create the interpolation and gradient matrices for projection from the nodes of `basis_from` to the nodes of `basis_to`. 181ba59ac12SSebastian Grimberg 18215ad3917SSebastian Grimberg The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization. 183ca94c3ddSJeremy L Thompson The gradient is given by `grad_project = interp_to^+ * grad_from`, and is only computed for \f$H^1\f$ spaces otherwise it should not be used. 18415ad3917SSebastian Grimberg 185ba59ac12SSebastian Grimberg Note: `basis_from` and `basis_to` must have compatible quadrature spaces. 186a76a04e7SJeremy L Thompson 187ca94c3ddSJeremy L Thompson @param[in] basis_from `CeedBasis` to project from 188ca94c3ddSJeremy L Thompson @param[in] basis_to `CeedBasis` to project to 189ca94c3ddSJeremy L Thompson @param[out] interp_project Address of the variable where the newly created interpolation matrix will be stored 190ca94c3ddSJeremy L Thompson @param[out] grad_project Address of the variable where the newly created gradient matrix will be stored 191a76a04e7SJeremy L Thompson 192a76a04e7SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 193a76a04e7SJeremy L Thompson 194a76a04e7SJeremy L Thompson @ref Developer 195a76a04e7SJeremy L Thompson **/ 1962b730f8bSJeremy L Thompson static int CeedBasisCreateProjectionMatrices(CeedBasis basis_from, CeedBasis basis_to, CeedScalar **interp_project, CeedScalar **grad_project) { 197e104ad11SJames Wright bool are_both_tensor; 1981c66c397SJeremy L Thompson CeedInt Q, Q_to, Q_from, P_to, P_from; 1991c66c397SJeremy L Thompson 200a76a04e7SJeremy L Thompson // Check for compatible quadrature spaces 2012b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_to, &Q_to)); 2022b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_from, &Q_from)); 2039bc66399SJeremy L Thompson CeedCheck(Q_to == Q_from, CeedBasisReturnCeed(basis_to), CEED_ERROR_DIMENSION, 2043f08121cSJeremy L Thompson "Bases must have compatible quadrature spaces." 20523622755SJeremy L Thompson " 'basis_from' has %" CeedInt_FMT " points and 'basis_to' has %" CeedInt_FMT, 2063f08121cSJeremy L Thompson Q_from, Q_to); 2071c66c397SJeremy L Thompson Q = Q_to; 208a76a04e7SJeremy L Thompson 20914556e63SJeremy L Thompson // Check for matching tensor or non-tensor 210e104ad11SJames Wright { 211e104ad11SJames Wright bool is_tensor_to, is_tensor_from; 212e104ad11SJames Wright 2132b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to)); 2142b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from)); 215e104ad11SJames Wright are_both_tensor = is_tensor_to && is_tensor_from; 216e104ad11SJames Wright } 217e104ad11SJames Wright if (are_both_tensor) { 2182b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_to)); 2192b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_from)); 2202b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis_from, &Q)); 2216574a04fSJeremy L Thompson } else { 2222b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_to, &P_to)); 2232b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_from, &P_from)); 224a76a04e7SJeremy L Thompson } 225a76a04e7SJeremy L Thompson 22615ad3917SSebastian Grimberg // Check for matching FE space 22715ad3917SSebastian Grimberg CeedFESpace fe_space_to, fe_space_from; 2283f08121cSJeremy L Thompson 22915ad3917SSebastian Grimberg CeedCall(CeedBasisGetFESpace(basis_to, &fe_space_to)); 23015ad3917SSebastian Grimberg CeedCall(CeedBasisGetFESpace(basis_from, &fe_space_from)); 2319bc66399SJeremy L Thompson CeedCheck(fe_space_to == fe_space_from, CeedBasisReturnCeed(basis_to), CEED_ERROR_MINOR, 2323f08121cSJeremy L Thompson "Bases must both be the same FE space type." 2333f08121cSJeremy L Thompson " 'basis_from' is a %s and 'basis_to' is a %s", 2343f08121cSJeremy L Thompson CeedFESpaces[fe_space_from], CeedFESpaces[fe_space_to]); 23515ad3917SSebastian Grimberg 23614556e63SJeremy L Thompson // Get source matrices 23715ad3917SSebastian Grimberg CeedInt dim, q_comp = 1; 2382247a93fSRezgar Shakeri CeedScalar *interp_to_inv, *interp_from; 2391c66c397SJeremy L Thompson const CeedScalar *interp_to_source = NULL, *interp_from_source = NULL, *grad_from_source = NULL; 2401c66c397SJeremy L Thompson 241b3ed00e5SJames Wright CeedCall(CeedBasisGetDimension(basis_from, &dim)); 242e104ad11SJames Wright if (are_both_tensor) { 2432b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis_to, &interp_to_source)); 2442b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis_from, &interp_from_source)); 245a76a04e7SJeremy L Thompson } else { 24615ad3917SSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis_from, CEED_EVAL_INTERP, &q_comp)); 2472b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis_to, &interp_to_source)); 2482b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis_from, &interp_from_source)); 24915ad3917SSebastian Grimberg } 25015ad3917SSebastian Grimberg CeedCall(CeedMalloc(Q * P_from * q_comp, &interp_from)); 25115ad3917SSebastian Grimberg CeedCall(CeedCalloc(P_to * P_from, interp_project)); 25215ad3917SSebastian Grimberg 25315ad3917SSebastian Grimberg // `grad_project = interp_to^+ * grad_from` is computed for the H^1 space case so the 254de05fbb2SSebastian Grimberg // projection basis will have a gradient operation (allocated even if not H^1 for the 255de05fbb2SSebastian Grimberg // basis construction later on) 25615ad3917SSebastian Grimberg if (fe_space_to == CEED_FE_SPACE_H1) { 257e104ad11SJames Wright if (are_both_tensor) { 25815ad3917SSebastian Grimberg CeedCall(CeedBasisGetGrad1D(basis_from, &grad_from_source)); 25915ad3917SSebastian Grimberg } else { 2602b730f8bSJeremy L Thompson CeedCall(CeedBasisGetGrad(basis_from, &grad_from_source)); 261a76a04e7SJeremy L Thompson } 262de05fbb2SSebastian Grimberg } 263e104ad11SJames Wright CeedCall(CeedCalloc(P_to * P_from * (are_both_tensor ? 1 : dim), grad_project)); 26415ad3917SSebastian Grimberg 2652247a93fSRezgar Shakeri // Compute interp_to^+, pseudoinverse of interp_to 2662247a93fSRezgar Shakeri CeedCall(CeedCalloc(Q * q_comp * P_to, &interp_to_inv)); 2679bc66399SJeremy L Thompson CeedCall(CeedMatrixPseudoinverse(CeedBasisReturnCeed(basis_to), interp_to_source, Q * q_comp, P_to, interp_to_inv)); 26814556e63SJeremy L Thompson // Build matrices 269e104ad11SJames Wright CeedInt num_matrices = 1 + (fe_space_to == CEED_FE_SPACE_H1) * (are_both_tensor ? 1 : dim); 27014556e63SJeremy L Thompson CeedScalar *input_from[num_matrices], *output_project[num_matrices]; 2711c66c397SJeremy L Thompson 27214556e63SJeremy L Thompson input_from[0] = (CeedScalar *)interp_from_source; 27314556e63SJeremy L Thompson output_project[0] = *interp_project; 27414556e63SJeremy L Thompson for (CeedInt m = 1; m < num_matrices; m++) { 27514556e63SJeremy L Thompson input_from[m] = (CeedScalar *)&grad_from_source[(m - 1) * Q * P_from]; 27602af4036SJeremy L Thompson output_project[m] = &((*grad_project)[(m - 1) * P_to * P_from]); 27714556e63SJeremy L Thompson } 27814556e63SJeremy L Thompson for (CeedInt m = 0; m < num_matrices; m++) { 2792247a93fSRezgar Shakeri // output_project = interp_to^+ * interp_from 28015ad3917SSebastian Grimberg memcpy(interp_from, input_from[m], Q * P_from * q_comp * sizeof(input_from[m][0])); 2819bc66399SJeremy L Thompson CeedCall(CeedMatrixMatrixMultiply(CeedBasisReturnCeed(basis_to), interp_to_inv, input_from[m], output_project[m], P_to, P_from, Q * q_comp)); 2822247a93fSRezgar Shakeri // Round zero to machine precision 2832247a93fSRezgar Shakeri for (CeedInt i = 0; i < P_to * P_from; i++) { 2842247a93fSRezgar Shakeri if (fabs(output_project[m][i]) < 10 * CEED_EPSILON) output_project[m][i] = 0.0; 285a76a04e7SJeremy L Thompson } 28614556e63SJeremy L Thompson } 28714556e63SJeremy L Thompson 28814556e63SJeremy L Thompson // Cleanup 2892247a93fSRezgar Shakeri CeedCall(CeedFree(&interp_to_inv)); 2902b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_from)); 291a76a04e7SJeremy L Thompson return CEED_ERROR_SUCCESS; 292a76a04e7SJeremy L Thompson } 293a76a04e7SJeremy L Thompson 2940b31fde2SJeremy L Thompson /** 2950b31fde2SJeremy L Thompson @brief Check input vector dimensions for CeedBasisApply[Add]AtPoints 2960b31fde2SJeremy L Thompson 2970b31fde2SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 2980b31fde2SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 2990b31fde2SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 3000b31fde2SJeremy L Thompson @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 3010b31fde2SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 3020b31fde2SJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes 3030b31fde2SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 3040b31fde2SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 3050b31fde2SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 3060b31fde2SJeremy L Thompson @param[in] x_ref `CeedVector` holding reference coordinates of each point 3070b31fde2SJeremy L Thompson @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 3080b31fde2SJeremy L Thompson @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 3090b31fde2SJeremy L Thompson 3100b31fde2SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3110b31fde2SJeremy L Thompson 3120b31fde2SJeremy L Thompson @ref Developer 3130b31fde2SJeremy L Thompson **/ 3140b31fde2SJeremy L Thompson static int CeedBasisApplyAtPointsCheckDims(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, 3150b31fde2SJeremy L Thompson CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) { 3160b31fde2SJeremy L Thompson CeedInt dim, num_comp, num_q_comp, num_nodes, P_1d = 1, Q_1d = 1, total_num_points = 0; 3170b31fde2SJeremy L Thompson CeedSize x_length = 0, u_length = 0, v_length; 3180b31fde2SJeremy L Thompson 3190b31fde2SJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 3200b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 3210b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 3220b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 3230b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &num_q_comp)); 3240b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 3250b31fde2SJeremy L Thompson CeedCall(CeedVectorGetLength(v, &v_length)); 3260b31fde2SJeremy L Thompson if (x_ref != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(x_ref, &x_length)); 3270b31fde2SJeremy L Thompson if (u != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(u, &u_length)); 3280b31fde2SJeremy L Thompson 3290b31fde2SJeremy L Thompson // Check compatibility coordinates vector 3300b31fde2SJeremy L Thompson for (CeedInt i = 0; i < num_elem; i++) total_num_points += num_points[i]; 3319bc66399SJeremy L Thompson CeedCheck((x_length >= (CeedSize)total_num_points * (CeedSize)dim) || (eval_mode == CEED_EVAL_WEIGHT), CeedBasisReturnCeed(basis), 3329bc66399SJeremy L Thompson CEED_ERROR_DIMENSION, 3330b31fde2SJeremy L Thompson "Length of reference coordinate vector incompatible with basis dimension and number of points." 3340b31fde2SJeremy L Thompson " Found reference coordinate vector of length %" CeedSize_FMT ", not of length %" CeedSize_FMT ".", 33519a04db8SJeremy L Thompson x_length, (CeedSize)total_num_points * (CeedSize)dim); 3360b31fde2SJeremy L Thompson 3370b31fde2SJeremy L Thompson // Check CEED_EVAL_WEIGHT only on CEED_NOTRANSPOSE 3389bc66399SJeremy L Thompson CeedCheck(eval_mode != CEED_EVAL_WEIGHT || t_mode == CEED_NOTRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, 3390b31fde2SJeremy L Thompson "CEED_EVAL_WEIGHT only supported with CEED_NOTRANSPOSE"); 3400b31fde2SJeremy L Thompson 3410b31fde2SJeremy L Thompson // Check vector lengths to prevent out of bounds issues 3420b31fde2SJeremy L Thompson bool has_good_dims = true; 3430b31fde2SJeremy L Thompson switch (eval_mode) { 3440b31fde2SJeremy L Thompson case CEED_EVAL_INTERP: 34519a04db8SJeremy L Thompson has_good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= (CeedSize)total_num_points * (CeedSize)num_q_comp || 34619a04db8SJeremy L Thompson v_length >= (CeedSize)num_elem * (CeedSize)num_nodes * (CeedSize)num_comp)) || 34719a04db8SJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && (v_length >= (CeedSize)total_num_points * (CeedSize)num_q_comp || 34819a04db8SJeremy L Thompson u_length >= (CeedSize)num_elem * (CeedSize)num_nodes * (CeedSize)num_comp))); 3490b31fde2SJeremy L Thompson break; 3500b31fde2SJeremy L Thompson case CEED_EVAL_GRAD: 35119a04db8SJeremy L Thompson has_good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= (CeedSize)total_num_points * (CeedSize)num_q_comp * (CeedSize)dim || 35219a04db8SJeremy L Thompson v_length >= (CeedSize)num_elem * (CeedSize)num_nodes * (CeedSize)num_comp)) || 35319a04db8SJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && (v_length >= (CeedSize)total_num_points * (CeedSize)num_q_comp * (CeedSize)dim || 35419a04db8SJeremy L Thompson u_length >= (CeedSize)num_elem * (CeedSize)num_nodes * (CeedSize)num_comp))); 3550b31fde2SJeremy L Thompson break; 3560b31fde2SJeremy L Thompson case CEED_EVAL_WEIGHT: 3570b31fde2SJeremy L Thompson has_good_dims = t_mode == CEED_NOTRANSPOSE && (v_length >= total_num_points); 3580b31fde2SJeremy L Thompson break; 3590b31fde2SJeremy L Thompson // LCOV_EXCL_START 3600b31fde2SJeremy L Thompson case CEED_EVAL_NONE: 3610b31fde2SJeremy L Thompson case CEED_EVAL_DIV: 3620b31fde2SJeremy L Thompson case CEED_EVAL_CURL: 3639bc66399SJeremy L Thompson return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points not supported for %s", 3649bc66399SJeremy L Thompson CeedEvalModes[eval_mode]); 3650b31fde2SJeremy L Thompson // LCOV_EXCL_STOP 3660b31fde2SJeremy L Thompson } 3679bc66399SJeremy L Thompson CeedCheck(has_good_dims, CeedBasisReturnCeed(basis), CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 3680b31fde2SJeremy L Thompson return CEED_ERROR_SUCCESS; 3690b31fde2SJeremy L Thompson } 3700b31fde2SJeremy L Thompson 3710b31fde2SJeremy L Thompson /** 3720b31fde2SJeremy L Thompson @brief Default implimentation to apply basis evaluation from nodes to arbitrary points 3730b31fde2SJeremy L Thompson 3740b31fde2SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 3750b31fde2SJeremy L Thompson @param[in] apply_add Sum result into target vector or overwrite 3760b31fde2SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 3770b31fde2SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 3780b31fde2SJeremy L Thompson @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 3790b31fde2SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 3800b31fde2SJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes 3810b31fde2SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 3820b31fde2SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 3830b31fde2SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 3840b31fde2SJeremy L Thompson @param[in] x_ref `CeedVector` holding reference coordinates of each point 3850b31fde2SJeremy L Thompson @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 3860b31fde2SJeremy L Thompson @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 3870b31fde2SJeremy L Thompson 3880b31fde2SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3890b31fde2SJeremy L Thompson 3900b31fde2SJeremy L Thompson @ref Developer 3910b31fde2SJeremy L Thompson **/ 3920b31fde2SJeremy L Thompson static int CeedBasisApplyAtPoints_Core(CeedBasis basis, bool apply_add, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, 3930b31fde2SJeremy L Thompson CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) { 3940b31fde2SJeremy L Thompson CeedInt dim, num_comp, P_1d = 1, Q_1d = 1, total_num_points = num_points[0]; 3950b31fde2SJeremy L Thompson 3960b31fde2SJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 3970b31fde2SJeremy L Thompson // Inserting check because clang-tidy doesn't understand this cannot occur 3989bc66399SJeremy L Thompson CeedCheck(dim > 0, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Malformed CeedBasis, dim > 0 is required"); 3990b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 4000b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 4010b31fde2SJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 4020b31fde2SJeremy L Thompson 4030b31fde2SJeremy L Thompson // Default implementation 4040b31fde2SJeremy L Thompson { 4050b31fde2SJeremy L Thompson bool is_tensor_basis; 4060b31fde2SJeremy L Thompson 4070b31fde2SJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 4089bc66399SJeremy L Thompson CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, 4099bc66399SJeremy L Thompson "Evaluation at arbitrary points only supported for tensor product bases"); 4100b31fde2SJeremy L Thompson } 4119bc66399SJeremy L Thompson CeedCheck(num_elem == 1, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, 4129bc66399SJeremy L Thompson "Evaluation at arbitrary points only supported for a single element at a time"); 4130b31fde2SJeremy L Thompson if (eval_mode == CEED_EVAL_WEIGHT) { 4140b31fde2SJeremy L Thompson CeedCall(CeedVectorSetValue(v, 1.0)); 4150b31fde2SJeremy L Thompson return CEED_ERROR_SUCCESS; 4160b31fde2SJeremy L Thompson } 4170b31fde2SJeremy L Thompson if (!basis->basis_chebyshev) { 4180b31fde2SJeremy L Thompson // Build basis mapping from nodes to Chebyshev coefficients 4190b31fde2SJeremy L Thompson CeedScalar *chebyshev_interp_1d, *chebyshev_grad_1d, *chebyshev_q_weight_1d; 4200b31fde2SJeremy L Thompson const CeedScalar *q_ref_1d; 4219bc66399SJeremy L Thompson Ceed ceed; 4220b31fde2SJeremy L Thompson 4230b31fde2SJeremy L Thompson CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 4240b31fde2SJeremy L Thompson CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_grad_1d)); 4250b31fde2SJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &chebyshev_q_weight_1d)); 4260b31fde2SJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 4270b31fde2SJeremy L Thompson CeedCall(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d)); 4280b31fde2SJeremy L Thompson 4299bc66399SJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 4300b31fde2SJeremy L Thompson CeedCall(CeedVectorCreate(ceed, num_comp * CeedIntPow(Q_1d, dim), &basis->vec_chebyshev)); 4310b31fde2SJeremy 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, 4320b31fde2SJeremy L Thompson &basis->basis_chebyshev)); 4330b31fde2SJeremy L Thompson 4340b31fde2SJeremy L Thompson // Cleanup 4350b31fde2SJeremy L Thompson CeedCall(CeedFree(&chebyshev_interp_1d)); 4360b31fde2SJeremy L Thompson CeedCall(CeedFree(&chebyshev_grad_1d)); 4370b31fde2SJeremy L Thompson CeedCall(CeedFree(&chebyshev_q_weight_1d)); 4389bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 4390b31fde2SJeremy L Thompson } 4400b31fde2SJeremy L Thompson 4410b31fde2SJeremy L Thompson // Create TensorContract object if needed, such as a basis from the GPU backends 4420b31fde2SJeremy L Thompson if (!basis->contract) { 4430b31fde2SJeremy L Thompson Ceed ceed_ref; 4440b31fde2SJeremy L Thompson CeedBasis basis_ref = NULL; 4450b31fde2SJeremy L Thompson 4460b31fde2SJeremy L Thompson CeedCall(CeedInit("/cpu/self", &ceed_ref)); 4470b31fde2SJeremy L Thompson // Only need matching tensor contraction dimensions, any type of basis will work 4480b31fde2SJeremy L Thompson CeedCall(CeedBasisCreateTensorH1Lagrange(ceed_ref, dim, num_comp, P_1d, Q_1d, CEED_GAUSS, &basis_ref)); 4490b31fde2SJeremy L Thompson // Note - clang-tidy doesn't know basis_ref->contract must be valid here 4509bc66399SJeremy L Thompson CeedCheck(basis_ref && basis_ref->contract, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, 4519bc66399SJeremy L Thompson "Reference CPU ceed failed to create a tensor contraction object"); 4520b31fde2SJeremy L Thompson CeedCall(CeedTensorContractReferenceCopy(basis_ref->contract, &basis->contract)); 4530b31fde2SJeremy L Thompson CeedCall(CeedBasisDestroy(&basis_ref)); 4540b31fde2SJeremy L Thompson CeedCall(CeedDestroy(&ceed_ref)); 4550b31fde2SJeremy L Thompson } 4560b31fde2SJeremy L Thompson 4570b31fde2SJeremy L Thompson // Basis evaluation 4580b31fde2SJeremy L Thompson switch (t_mode) { 4590b31fde2SJeremy L Thompson case CEED_NOTRANSPOSE: { 4600b31fde2SJeremy L Thompson // Nodes to arbitrary points 4610b31fde2SJeremy L Thompson CeedScalar *v_array; 4620b31fde2SJeremy L Thompson const CeedScalar *chebyshev_coeffs, *x_array_read; 4630b31fde2SJeremy L Thompson 4640b31fde2SJeremy L Thompson // -- Interpolate to Chebyshev coefficients 4650b31fde2SJeremy L Thompson CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, basis->vec_chebyshev)); 4660b31fde2SJeremy L Thompson 4670b31fde2SJeremy L Thompson // -- Evaluate Chebyshev polynomials at arbitrary points 4680b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs)); 4690b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read)); 4700b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(v, CEED_MEM_HOST, &v_array)); 4710b31fde2SJeremy L Thompson switch (eval_mode) { 4720b31fde2SJeremy L Thompson case CEED_EVAL_INTERP: { 4730b31fde2SJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 4740b31fde2SJeremy L Thompson 4750b31fde2SJeremy L Thompson // ---- Values at point 4760b31fde2SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 4770b31fde2SJeremy L Thompson CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1; 4780b31fde2SJeremy L Thompson 4790b31fde2SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 4800b31fde2SJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 4810b31fde2SJeremy L Thompson CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 4820b31fde2SJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false, 4830b31fde2SJeremy L Thompson d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2])); 4840b31fde2SJeremy L Thompson pre /= Q_1d; 4850b31fde2SJeremy L Thompson post *= 1; 4860b31fde2SJeremy L Thompson } 4870b31fde2SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) v_array[c * total_num_points + p] = tmp[dim % 2][c]; 4880b31fde2SJeremy L Thompson } 4890b31fde2SJeremy L Thompson break; 4900b31fde2SJeremy L Thompson } 4910b31fde2SJeremy L Thompson case CEED_EVAL_GRAD: { 4920b31fde2SJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 4930b31fde2SJeremy L Thompson 4940b31fde2SJeremy L Thompson // ---- Values at point 4950b31fde2SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 4960b31fde2SJeremy L Thompson // Dim**2 contractions, apply grad when pass == dim 4970b31fde2SJeremy L Thompson for (CeedInt pass = 0; pass < dim; pass++) { 4980b31fde2SJeremy L Thompson CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1; 4990b31fde2SJeremy L Thompson 5000b31fde2SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 5010b31fde2SJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 5020b31fde2SJeremy L Thompson if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 5030b31fde2SJeremy L Thompson else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 5040b31fde2SJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false, 5050b31fde2SJeremy L Thompson d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2])); 5060b31fde2SJeremy L Thompson pre /= Q_1d; 5070b31fde2SJeremy L Thompson post *= 1; 5080b31fde2SJeremy L Thompson } 5090b31fde2SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) v_array[(pass * num_comp + c) * total_num_points + p] = tmp[dim % 2][c]; 5100b31fde2SJeremy L Thompson } 5110b31fde2SJeremy L Thompson } 5120b31fde2SJeremy L Thompson break; 5130b31fde2SJeremy L Thompson } 5140b31fde2SJeremy L Thompson default: 5150b31fde2SJeremy L Thompson // Nothing to do, excluded above 5160b31fde2SJeremy L Thompson break; 5170b31fde2SJeremy L Thompson } 5180b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(basis->vec_chebyshev, &chebyshev_coeffs)); 5190b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read)); 5200b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArray(v, &v_array)); 5210b31fde2SJeremy L Thompson break; 5220b31fde2SJeremy L Thompson } 5230b31fde2SJeremy L Thompson case CEED_TRANSPOSE: { 5240b31fde2SJeremy L Thompson // Note: No switch on e_mode here because only CEED_EVAL_INTERP is supported at this time 5250b31fde2SJeremy L Thompson // Arbitrary points to nodes 5260b31fde2SJeremy L Thompson CeedScalar *chebyshev_coeffs; 5270b31fde2SJeremy L Thompson const CeedScalar *u_array, *x_array_read; 5280b31fde2SJeremy L Thompson 5290b31fde2SJeremy L Thompson // -- Transpose of evaluation of Chebyshev polynomials at arbitrary points 5300b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs)); 5310b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read)); 5320b31fde2SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array)); 5330b31fde2SJeremy L Thompson 5340b31fde2SJeremy L Thompson switch (eval_mode) { 5350b31fde2SJeremy L Thompson case CEED_EVAL_INTERP: { 5360b31fde2SJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 5370b31fde2SJeremy L Thompson 5380b31fde2SJeremy L Thompson // ---- Values at point 5390b31fde2SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 5400b31fde2SJeremy L Thompson CeedInt pre = num_comp * 1, post = 1; 5410b31fde2SJeremy L Thompson 5420b31fde2SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[c * total_num_points + p]; 5430b31fde2SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 5440b31fde2SJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 5450b31fde2SJeremy L Thompson CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 5460b31fde2SJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, p > 0 && d == (dim - 1), tmp[d % 2], 5470b31fde2SJeremy L Thompson d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2])); 5480b31fde2SJeremy L Thompson pre /= 1; 5490b31fde2SJeremy L Thompson post *= Q_1d; 5500b31fde2SJeremy L Thompson } 5510b31fde2SJeremy L Thompson } 5520b31fde2SJeremy L Thompson break; 5530b31fde2SJeremy L Thompson } 5540b31fde2SJeremy L Thompson case CEED_EVAL_GRAD: { 5550b31fde2SJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 5560b31fde2SJeremy L Thompson 5570b31fde2SJeremy L Thompson // ---- Values at point 5580b31fde2SJeremy L Thompson for (CeedInt p = 0; p < total_num_points; p++) { 5590b31fde2SJeremy L Thompson // Dim**2 contractions, apply grad when pass == dim 5600b31fde2SJeremy L Thompson for (CeedInt pass = 0; pass < dim; pass++) { 5610b31fde2SJeremy L Thompson CeedInt pre = num_comp * 1, post = 1; 5620b31fde2SJeremy L Thompson 5630b31fde2SJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[(pass * num_comp + c) * total_num_points + p]; 5640b31fde2SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 5650b31fde2SJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 5660b31fde2SJeremy L Thompson if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 5670b31fde2SJeremy L Thompson else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 5680b31fde2SJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, 5690b31fde2SJeremy L Thompson (p > 0 || (p == 0 && pass > 0)) && d == (dim - 1), tmp[d % 2], 5700b31fde2SJeremy L Thompson d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2])); 5710b31fde2SJeremy L Thompson pre /= 1; 5720b31fde2SJeremy L Thompson post *= Q_1d; 5730b31fde2SJeremy L Thompson } 5740b31fde2SJeremy L Thompson } 5750b31fde2SJeremy L Thompson } 5760b31fde2SJeremy L Thompson break; 5770b31fde2SJeremy L Thompson } 5780b31fde2SJeremy L Thompson default: 5790b31fde2SJeremy L Thompson // Nothing to do, excluded above 5800b31fde2SJeremy L Thompson break; 5810b31fde2SJeremy L Thompson } 5820b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArray(basis->vec_chebyshev, &chebyshev_coeffs)); 5830b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read)); 5840b31fde2SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(u, &u_array)); 5850b31fde2SJeremy L Thompson 5860b31fde2SJeremy L Thompson // -- Interpolate transpose from Chebyshev coefficients 5870b31fde2SJeremy L Thompson if (apply_add) CeedCall(CeedBasisApplyAdd(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v)); 5880b31fde2SJeremy L Thompson else CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v)); 5890b31fde2SJeremy L Thompson break; 5900b31fde2SJeremy L Thompson } 5910b31fde2SJeremy L Thompson } 5920b31fde2SJeremy L Thompson return CEED_ERROR_SUCCESS; 5930b31fde2SJeremy L Thompson } 5940b31fde2SJeremy L Thompson 5957a982d89SJeremy L. Thompson /// @} 5967a982d89SJeremy L. Thompson 5977a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 5987a982d89SJeremy L. Thompson /// Ceed Backend API 5997a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 6007a982d89SJeremy L. Thompson /// @addtogroup CeedBasisBackend 6017a982d89SJeremy L. Thompson /// @{ 6027a982d89SJeremy L. Thompson 6037a982d89SJeremy L. Thompson /** 604*fda26546SJeremy L Thompson @brief Fallback to a reference implementation for a non tensor-product basis for \f$H^1\f$ discretizations. 605*fda26546SJeremy L Thompson This function may only be called inside of a backend `BasisCreateH1` function. 606*fda26546SJeremy L Thompson This is used by a backend when the specific parameters for a `CeedBasis` exceed the backend's support, such as 607*fda26546SJeremy L Thompson when a `interp` and `grad` matrices require too many bytes to fit into shared memory on a GPU. 608*fda26546SJeremy L Thompson 609*fda26546SJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 610*fda26546SJeremy L Thompson @param[in] topo Topology of element, e.g. hypercube, simplex, etc 611*fda26546SJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 612*fda26546SJeremy L Thompson @param[in] num_nodes Total number of nodes 613*fda26546SJeremy L Thompson @param[in] num_qpts Total number of quadrature points 614*fda26546SJeremy L Thompson @param[in] interp Row-major (`num_qpts * num_nodes`) matrix expressing the values of nodal basis functions at quadrature points 615*fda26546SJeremy L Thompson @param[in] grad Row-major (`dim * num_qpts * num_nodes`) matrix expressing derivatives of nodal basis functions at quadrature points 616*fda26546SJeremy L Thompson @param[in] q_ref Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element 617*fda26546SJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 618*fda26546SJeremy L Thompson @param[out] basis Newly created `CeedBasis` 619*fda26546SJeremy L Thompson 620*fda26546SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 621*fda26546SJeremy L Thompson 622*fda26546SJeremy L Thompson @ref User 623*fda26546SJeremy L Thompson **/ 624*fda26546SJeremy L Thompson int CeedBasisCreateH1Fallback(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 625*fda26546SJeremy L Thompson const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) { 626*fda26546SJeremy L Thompson CeedInt P = num_nodes, Q = num_qpts, dim = 0; 627*fda26546SJeremy L Thompson Ceed delegate; 628*fda26546SJeremy L Thompson 629*fda26546SJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 630*fda26546SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateH1"); 631*fda26546SJeremy L Thompson 632*fda26546SJeremy L Thompson CeedCall(CeedReferenceCopy(delegate, &(basis)->ceed)); 633*fda26546SJeremy L Thompson CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 634*fda26546SJeremy L Thompson CeedCall(delegate->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, basis)); 635*fda26546SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 636*fda26546SJeremy L Thompson return CEED_ERROR_SUCCESS; 637*fda26546SJeremy L Thompson } 638*fda26546SJeremy L Thompson 639*fda26546SJeremy L Thompson /** 640ca94c3ddSJeremy L Thompson @brief Return collocated gradient matrix 6417a982d89SJeremy L. Thompson 642ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 643ca94c3ddSJeremy L Thompson @param[out] collo_grad_1d Row-major (`Q_1d * Q_1d`) matrix expressing derivatives of basis functions at quadrature points 6447a982d89SJeremy L. Thompson 6457a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 6467a982d89SJeremy L. Thompson 6477a982d89SJeremy L. Thompson @ref Backend 6487a982d89SJeremy L. Thompson **/ 649d1d35e2fSjeremylt int CeedBasisGetCollocatedGrad(CeedBasis basis, CeedScalar *collo_grad_1d) { 6507a982d89SJeremy L. Thompson Ceed ceed; 6512247a93fSRezgar Shakeri CeedInt P_1d, Q_1d; 6522247a93fSRezgar Shakeri CeedScalar *interp_1d_pinv; 6531203703bSJeremy L Thompson const CeedScalar *grad_1d, *interp_1d; 6541203703bSJeremy L Thompson 655ea61e9acSJeremy 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. 6562247a93fSRezgar Shakeri CeedCall(CeedBasisGetCeed(basis, &ceed)); 6572247a93fSRezgar Shakeri CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 6582247a93fSRezgar Shakeri CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 6597a982d89SJeremy L. Thompson 6602247a93fSRezgar Shakeri // Compute interp_1d^+, pseudoinverse of interp_1d 6612247a93fSRezgar Shakeri CeedCall(CeedCalloc(P_1d * Q_1d, &interp_1d_pinv)); 6621203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 6631203703bSJeremy L Thompson CeedCall(CeedMatrixPseudoinverse(ceed, interp_1d, Q_1d, P_1d, interp_1d_pinv)); 6641203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 6651203703bSJeremy L Thompson CeedCall(CeedMatrixMatrixMultiply(ceed, grad_1d, (const CeedScalar *)interp_1d_pinv, collo_grad_1d, Q_1d, Q_1d, P_1d)); 6667a982d89SJeremy L. Thompson 6672247a93fSRezgar Shakeri CeedCall(CeedFree(&interp_1d_pinv)); 6689bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 669e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6707a982d89SJeremy L. Thompson } 6717a982d89SJeremy L. Thompson 6727a982d89SJeremy L. Thompson /** 673b0cc4569SJeremy L Thompson @brief Return 1D interpolation matrix to Chebyshev polynomial coefficients on quadrature space 674b0cc4569SJeremy L Thompson 675b0cc4569SJeremy L Thompson @param[in] basis `CeedBasis` 676b0cc4569SJeremy L Thompson @param[out] chebyshev_interp_1d Row-major (`P_1d * Q_1d`) matrix interpolating from basis nodes to Chebyshev polynomial coefficients 677b0cc4569SJeremy L Thompson 678b0cc4569SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 679b0cc4569SJeremy L Thompson 680b0cc4569SJeremy L Thompson @ref Backend 681b0cc4569SJeremy L Thompson **/ 682b0cc4569SJeremy L Thompson int CeedBasisGetChebyshevInterp1D(CeedBasis basis, CeedScalar *chebyshev_interp_1d) { 683b0cc4569SJeremy L Thompson CeedInt P_1d, Q_1d; 684b0cc4569SJeremy L Thompson CeedScalar *C, *chebyshev_coeffs_1d_inv; 685b0cc4569SJeremy L Thompson const CeedScalar *interp_1d, *q_ref_1d; 686b0cc4569SJeremy L Thompson Ceed ceed; 687b0cc4569SJeremy L Thompson 688b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 689b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 690b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 691b0cc4569SJeremy L Thompson 692b0cc4569SJeremy L Thompson // Build coefficient matrix 693bd83cbc5SJeremy L Thompson // -- Note: Clang-tidy needs this check 694bd83cbc5SJeremy L Thompson CeedCheck((P_1d > 0) && (Q_1d > 0), ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis dimensions are malformed"); 695b0cc4569SJeremy L Thompson CeedCall(CeedCalloc(Q_1d * Q_1d, &C)); 696b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 697b0cc4569SJeremy L Thompson for (CeedInt i = 0; i < Q_1d; i++) CeedCall(CeedChebyshevPolynomialsAtPoint(q_ref_1d[i], Q_1d, &C[i * Q_1d])); 698b0cc4569SJeremy L Thompson 699b0cc4569SJeremy L Thompson // Compute C^+, pseudoinverse of coefficient matrix 700b0cc4569SJeremy L Thompson CeedCall(CeedCalloc(Q_1d * Q_1d, &chebyshev_coeffs_1d_inv)); 701b0cc4569SJeremy L Thompson CeedCall(CeedMatrixPseudoinverse(ceed, C, Q_1d, Q_1d, chebyshev_coeffs_1d_inv)); 702b0cc4569SJeremy L Thompson 703b0cc4569SJeremy L Thompson // Build mapping from nodes to Chebyshev coefficients 704b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 705b0cc4569SJeremy L Thompson CeedCall(CeedMatrixMatrixMultiply(ceed, chebyshev_coeffs_1d_inv, interp_1d, chebyshev_interp_1d, Q_1d, P_1d, Q_1d)); 706b0cc4569SJeremy L Thompson 707b0cc4569SJeremy L Thompson // Cleanup 708b0cc4569SJeremy L Thompson CeedCall(CeedFree(&C)); 709b0cc4569SJeremy L Thompson CeedCall(CeedFree(&chebyshev_coeffs_1d_inv)); 7109bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 711b0cc4569SJeremy L Thompson return CEED_ERROR_SUCCESS; 712b0cc4569SJeremy L Thompson } 713b0cc4569SJeremy L Thompson 714b0cc4569SJeremy L Thompson /** 715ca94c3ddSJeremy L Thompson @brief Get tensor status for given `CeedBasis` 7167a982d89SJeremy L. Thompson 717ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 718d1d35e2fSjeremylt @param[out] is_tensor Variable to store tensor status 7197a982d89SJeremy L. Thompson 7207a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 7217a982d89SJeremy L. Thompson 7227a982d89SJeremy L. Thompson @ref Backend 7237a982d89SJeremy L. Thompson **/ 724d1d35e2fSjeremylt int CeedBasisIsTensor(CeedBasis basis, bool *is_tensor) { 7256402da51SJeremy L Thompson *is_tensor = basis->is_tensor_basis; 726e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7277a982d89SJeremy L. Thompson } 7287a982d89SJeremy L. Thompson 7297a982d89SJeremy L. Thompson /** 730ca94c3ddSJeremy L Thompson @brief Get backend data of a `CeedBasis` 7317a982d89SJeremy L. Thompson 732ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 7337a982d89SJeremy L. Thompson @param[out] data Variable to store data 7347a982d89SJeremy L. Thompson 7357a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 7367a982d89SJeremy L. Thompson 7377a982d89SJeremy L. Thompson @ref Backend 7387a982d89SJeremy L. Thompson **/ 739777ff853SJeremy L Thompson int CeedBasisGetData(CeedBasis basis, void *data) { 740777ff853SJeremy L Thompson *(void **)data = basis->data; 741e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7427a982d89SJeremy L. Thompson } 7437a982d89SJeremy L. Thompson 7447a982d89SJeremy L. Thompson /** 745ca94c3ddSJeremy L Thompson @brief Set backend data of a `CeedBasis` 7467a982d89SJeremy L. Thompson 747ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` 748ea61e9acSJeremy L Thompson @param[in] data Data to set 7497a982d89SJeremy L. Thompson 7507a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 7517a982d89SJeremy L. Thompson 7527a982d89SJeremy L. Thompson @ref Backend 7537a982d89SJeremy L. Thompson **/ 754777ff853SJeremy L Thompson int CeedBasisSetData(CeedBasis basis, void *data) { 755777ff853SJeremy L Thompson basis->data = data; 756e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7577a982d89SJeremy L. Thompson } 7587a982d89SJeremy L. Thompson 7597a982d89SJeremy L. Thompson /** 760ca94c3ddSJeremy L Thompson @brief Increment the reference counter for a `CeedBasis` 76134359f16Sjeremylt 762ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` to increment the reference counter 76334359f16Sjeremylt 76434359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 76534359f16Sjeremylt 76634359f16Sjeremylt @ref Backend 76734359f16Sjeremylt **/ 7689560d06aSjeremylt int CeedBasisReference(CeedBasis basis) { 76934359f16Sjeremylt basis->ref_count++; 77034359f16Sjeremylt return CEED_ERROR_SUCCESS; 77134359f16Sjeremylt } 77234359f16Sjeremylt 77334359f16Sjeremylt /** 774ca94c3ddSJeremy L Thompson @brief Get number of Q-vector components for given `CeedBasis` 775c4e3f59bSSebastian Grimberg 776ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 777ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 778ca94c3ddSJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 779ca94c3ddSJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 780ca94c3ddSJeremy L Thompson @ref CEED_EVAL_CURL to use curl 781c4e3f59bSSebastian Grimberg @param[out] q_comp Variable to store number of Q-vector components of basis 782c4e3f59bSSebastian Grimberg 783c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 784c4e3f59bSSebastian Grimberg 785c4e3f59bSSebastian Grimberg @ref Backend 786c4e3f59bSSebastian Grimberg **/ 787c4e3f59bSSebastian Grimberg int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode, CeedInt *q_comp) { 7881203703bSJeremy L Thompson CeedInt dim; 7891203703bSJeremy L Thompson 7901203703bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 791c4e3f59bSSebastian Grimberg switch (eval_mode) { 7921203703bSJeremy L Thompson case CEED_EVAL_INTERP: { 7931203703bSJeremy L Thompson CeedFESpace fe_space; 7941203703bSJeremy L Thompson 7951203703bSJeremy L Thompson CeedCall(CeedBasisGetFESpace(basis, &fe_space)); 7961203703bSJeremy L Thompson *q_comp = (fe_space == CEED_FE_SPACE_H1) ? 1 : dim; 7971203703bSJeremy L Thompson } break; 798c4e3f59bSSebastian Grimberg case CEED_EVAL_GRAD: 7991203703bSJeremy L Thompson *q_comp = dim; 800c4e3f59bSSebastian Grimberg break; 801c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 802c4e3f59bSSebastian Grimberg *q_comp = 1; 803c4e3f59bSSebastian Grimberg break; 804c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 8051203703bSJeremy L Thompson *q_comp = (dim < 3) ? 1 : dim; 806c4e3f59bSSebastian Grimberg break; 807c4e3f59bSSebastian Grimberg case CEED_EVAL_NONE: 808c4e3f59bSSebastian Grimberg case CEED_EVAL_WEIGHT: 809352a5e7cSSebastian Grimberg *q_comp = 1; 810c4e3f59bSSebastian Grimberg break; 811c4e3f59bSSebastian Grimberg } 812c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 813c4e3f59bSSebastian Grimberg } 814c4e3f59bSSebastian Grimberg 815c4e3f59bSSebastian Grimberg /** 816ca94c3ddSJeremy L Thompson @brief Estimate number of FLOPs required to apply `CeedBasis` in `t_mode` and `eval_mode` 8176e15d496SJeremy L Thompson 818ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to estimate FLOPs for 819ea61e9acSJeremy L Thompson @param[in] t_mode Apply basis or transpose 820ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CeedEvalMode 8213f919cbcSJeremy L Thompson @param[in] is_at_points Evaluate the basis at points or quadrature points 8223f919cbcSJeremy L Thompson @param[in] num_points Number of points basis is evaluated at 823ea61e9acSJeremy L Thompson @param[out] flops Address of variable to hold FLOPs estimate 8246e15d496SJeremy L Thompson 8256e15d496SJeremy L Thompson @ref Backend 8266e15d496SJeremy L Thompson **/ 8273f919cbcSJeremy L Thompson int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, bool is_at_points, CeedInt num_points, 8283f919cbcSJeremy L Thompson CeedSize *flops) { 8296e15d496SJeremy L Thompson bool is_tensor; 8306e15d496SJeremy L Thompson 8312b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor)); 8323f919cbcSJeremy L Thompson CeedCheck(!is_at_points || is_tensor, CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Can only evaluate tensor-product bases at points"); 8336e15d496SJeremy L Thompson if (is_tensor) { 8346e15d496SJeremy L Thompson CeedInt dim, num_comp, P_1d, Q_1d; 8351c66c397SJeremy L Thompson 8362b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 8372b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 8382b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 8392b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 8406e15d496SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 8412b730f8bSJeremy L Thompson P_1d = Q_1d; 8422b730f8bSJeremy L Thompson Q_1d = P_1d; 8436e15d496SJeremy L Thompson } 8446e15d496SJeremy L Thompson CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim - 1), post = 1; 8453f919cbcSJeremy L Thompson 8466e15d496SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 8476e15d496SJeremy L Thompson tensor_flops += 2 * pre * P_1d * post * Q_1d; 8486e15d496SJeremy L Thompson pre /= P_1d; 8496e15d496SJeremy L Thompson post *= Q_1d; 8506e15d496SJeremy L Thompson } 8513f919cbcSJeremy L Thompson if (is_at_points) { 8523f919cbcSJeremy L Thompson CeedInt chebyshev_flops = (Q_1d - 2) * 3 + 1, d_chebyshev_flops = (Q_1d - 2) * 8 + 1; 8533f919cbcSJeremy L Thompson CeedInt point_tensor_flops = 0, pre = CeedIntPow(Q_1d, dim - 1), post = 1; 8543f919cbcSJeremy L Thompson 8553f919cbcSJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 8563f919cbcSJeremy L Thompson point_tensor_flops += 2 * pre * Q_1d * post * 1; 8573f919cbcSJeremy L Thompson pre /= P_1d; 8583f919cbcSJeremy L Thompson post *= Q_1d; 8593f919cbcSJeremy L Thompson } 8603f919cbcSJeremy L Thompson 8613f919cbcSJeremy L Thompson switch (eval_mode) { 8623f919cbcSJeremy L Thompson case CEED_EVAL_NONE: 8633f919cbcSJeremy L Thompson *flops = 0; 8643f919cbcSJeremy L Thompson break; 8653f919cbcSJeremy L Thompson case CEED_EVAL_INTERP: 8663f919cbcSJeremy L Thompson *flops = tensor_flops + num_points * (dim * chebyshev_flops + point_tensor_flops + (t_mode == CEED_TRANSPOSE ? CeedIntPow(Q_1d, dim) : 0)); 8673f919cbcSJeremy L Thompson break; 8683f919cbcSJeremy L Thompson case CEED_EVAL_GRAD: 8693f919cbcSJeremy L Thompson *flops = tensor_flops + num_points * (dim * (d_chebyshev_flops + (dim - 1) * chebyshev_flops + point_tensor_flops + 8703f919cbcSJeremy L Thompson (t_mode == CEED_TRANSPOSE ? CeedIntPow(Q_1d, dim) : 0))); 8713f919cbcSJeremy L Thompson break; 8723f919cbcSJeremy L Thompson case CEED_EVAL_DIV: 8733f919cbcSJeremy L Thompson case CEED_EVAL_CURL: { 8743f919cbcSJeremy L Thompson // LCOV_EXCL_START 8753f919cbcSJeremy L Thompson return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported", 8763f919cbcSJeremy L Thompson CeedEvalModes[eval_mode]); 8773f919cbcSJeremy L Thompson break; 8783f919cbcSJeremy L Thompson // LCOV_EXCL_STOP 8793f919cbcSJeremy L Thompson } 8803f919cbcSJeremy L Thompson case CEED_EVAL_WEIGHT: 8813f919cbcSJeremy L Thompson *flops = num_points; 8823f919cbcSJeremy L Thompson break; 8833f919cbcSJeremy L Thompson } 8843f919cbcSJeremy L Thompson } else { 8856e15d496SJeremy L Thompson switch (eval_mode) { 8862b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 8872b730f8bSJeremy L Thompson *flops = 0; 8882b730f8bSJeremy L Thompson break; 8892b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 8902b730f8bSJeremy L Thompson *flops = tensor_flops; 8912b730f8bSJeremy L Thompson break; 8922b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 8932b730f8bSJeremy L Thompson *flops = tensor_flops * 2; 8942b730f8bSJeremy L Thompson break; 8956e15d496SJeremy L Thompson case CEED_EVAL_DIV: 8961203703bSJeremy L Thompson case CEED_EVAL_CURL: { 8976574a04fSJeremy L Thompson // LCOV_EXCL_START 8986e536b99SJeremy L Thompson return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported", 8996e536b99SJeremy L Thompson CeedEvalModes[eval_mode]); 9002b730f8bSJeremy L Thompson break; 9016e15d496SJeremy L Thompson // LCOV_EXCL_STOP 9021203703bSJeremy L Thompson } 9032b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 9042b730f8bSJeremy L Thompson *flops = dim * CeedIntPow(Q_1d, dim); 9052b730f8bSJeremy L Thompson break; 9066e15d496SJeremy L Thompson } 9073f919cbcSJeremy L Thompson } 9086e15d496SJeremy L Thompson } else { 909c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 9101c66c397SJeremy L Thompson 9112b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 9122b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 913c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 9142b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 9152b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 9166e15d496SJeremy L Thompson switch (eval_mode) { 9172b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 9182b730f8bSJeremy L Thompson *flops = 0; 9192b730f8bSJeremy L Thompson break; 9202b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 9212b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 9222b730f8bSJeremy L Thompson case CEED_EVAL_DIV: 9232b730f8bSJeremy L Thompson case CEED_EVAL_CURL: 924c4e3f59bSSebastian Grimberg *flops = num_nodes * num_qpts * num_comp * q_comp; 9252b730f8bSJeremy L Thompson break; 9262b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 9272b730f8bSJeremy L Thompson *flops = 0; 9282b730f8bSJeremy L Thompson break; 9296e15d496SJeremy L Thompson } 9306e15d496SJeremy L Thompson } 9316e15d496SJeremy L Thompson return CEED_ERROR_SUCCESS; 9326e15d496SJeremy L Thompson } 9336e15d496SJeremy L Thompson 9346e15d496SJeremy L Thompson /** 935ca94c3ddSJeremy L Thompson @brief Get `CeedFESpace` for a `CeedBasis` 936c4e3f59bSSebastian Grimberg 937ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 938ca94c3ddSJeremy L Thompson @param[out] fe_space Variable to store `CeedFESpace` 939c4e3f59bSSebastian Grimberg 940c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 941c4e3f59bSSebastian Grimberg 942c4e3f59bSSebastian Grimberg @ref Backend 943c4e3f59bSSebastian Grimberg **/ 944c4e3f59bSSebastian Grimberg int CeedBasisGetFESpace(CeedBasis basis, CeedFESpace *fe_space) { 945c4e3f59bSSebastian Grimberg *fe_space = basis->fe_space; 946c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 947c4e3f59bSSebastian Grimberg } 948c4e3f59bSSebastian Grimberg 949c4e3f59bSSebastian Grimberg /** 950ca94c3ddSJeremy L Thompson @brief Get dimension for given `CeedElemTopology` 9517a982d89SJeremy L. Thompson 952ca94c3ddSJeremy L Thompson @param[in] topo `CeedElemTopology` 9537a982d89SJeremy L. Thompson @param[out] dim Variable to store dimension of topology 9547a982d89SJeremy L. Thompson 9557a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 9567a982d89SJeremy L. Thompson 9577a982d89SJeremy L. Thompson @ref Backend 9587a982d89SJeremy L. Thompson **/ 9597a982d89SJeremy L. Thompson int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim) { 9607a982d89SJeremy L. Thompson *dim = (CeedInt)topo >> 16; 961e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9627a982d89SJeremy L. Thompson } 9637a982d89SJeremy L. Thompson 9647a982d89SJeremy L. Thompson /** 965ca94c3ddSJeremy L Thompson @brief Get `CeedTensorContract` of a `CeedBasis` 9667a982d89SJeremy L. Thompson 967ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 968ca94c3ddSJeremy L Thompson @param[out] contract Variable to store `CeedTensorContract` 9697a982d89SJeremy L. Thompson 9707a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 9717a982d89SJeremy L. Thompson 9727a982d89SJeremy L. Thompson @ref Backend 9737a982d89SJeremy L. Thompson **/ 9747a982d89SJeremy L. Thompson int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract) { 9757a982d89SJeremy L. Thompson *contract = basis->contract; 976e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9777a982d89SJeremy L. Thompson } 9787a982d89SJeremy L. Thompson 9797a982d89SJeremy L. Thompson /** 980ca94c3ddSJeremy L Thompson @brief Set `CeedTensorContract` of a `CeedBasis` 9817a982d89SJeremy L. Thompson 982ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` 983ca94c3ddSJeremy L Thompson @param[in] contract `CeedTensorContract` to set 9847a982d89SJeremy L. Thompson 9857a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 9867a982d89SJeremy L. Thompson 9877a982d89SJeremy L. Thompson @ref Backend 9887a982d89SJeremy L. Thompson **/ 98934359f16Sjeremylt int CeedBasisSetTensorContract(CeedBasis basis, CeedTensorContract contract) { 99034359f16Sjeremylt basis->contract = contract; 9912b730f8bSJeremy L Thompson CeedCall(CeedTensorContractReference(contract)); 992e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9937a982d89SJeremy L. Thompson } 9947a982d89SJeremy L. Thompson 9957a982d89SJeremy L. Thompson /** 996ca94c3ddSJeremy L Thompson @brief Return a reference implementation of matrix multiplication \f$C = A B\f$. 997ba59ac12SSebastian Grimberg 998ca94c3ddSJeremy L Thompson Note: This is a reference implementation for CPU `CeedScalar` pointers that is not intended for high performance. 9997a982d89SJeremy L. Thompson 1000ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 1001ca94c3ddSJeremy L Thompson @param[in] mat_A Row-major matrix `A` 1002ca94c3ddSJeremy L Thompson @param[in] mat_B Row-major matrix `B` 1003ca94c3ddSJeremy L Thompson @param[out] mat_C Row-major output matrix `C` 1004ca94c3ddSJeremy L Thompson @param[in] m Number of rows of `C` 1005ca94c3ddSJeremy L Thompson @param[in] n Number of columns of `C` 1006ca94c3ddSJeremy L Thompson @param[in] kk Number of columns of `A`/rows of `B` 10077a982d89SJeremy L. Thompson 10087a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 10097a982d89SJeremy L. Thompson 10107a982d89SJeremy L. Thompson @ref Utility 10117a982d89SJeremy L. Thompson **/ 10122b730f8bSJeremy L Thompson int CeedMatrixMatrixMultiply(Ceed ceed, const CeedScalar *mat_A, const CeedScalar *mat_B, CeedScalar *mat_C, CeedInt m, CeedInt n, CeedInt kk) { 10132b730f8bSJeremy L Thompson for (CeedInt i = 0; i < m; i++) { 10147a982d89SJeremy L. Thompson for (CeedInt j = 0; j < n; j++) { 10157a982d89SJeremy L. Thompson CeedScalar sum = 0; 10161c66c397SJeremy L Thompson 10172b730f8bSJeremy L Thompson for (CeedInt k = 0; k < kk; k++) sum += mat_A[k + i * kk] * mat_B[j + k * n]; 1018d1d35e2fSjeremylt mat_C[j + i * n] = sum; 10197a982d89SJeremy L. Thompson } 10202b730f8bSJeremy L Thompson } 1021e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10227a982d89SJeremy L. Thompson } 10237a982d89SJeremy L. Thompson 1024ba59ac12SSebastian Grimberg /** 1025ba59ac12SSebastian Grimberg @brief Return QR Factorization of a matrix 1026ba59ac12SSebastian Grimberg 1027ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 1028ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 1029ca94c3ddSJeremy L Thompson @param[in,out] tau Vector of length `m` of scaling factors 1030ba59ac12SSebastian Grimberg @param[in] m Number of rows 1031ba59ac12SSebastian Grimberg @param[in] n Number of columns 1032ba59ac12SSebastian Grimberg 1033ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1034ba59ac12SSebastian Grimberg 1035ba59ac12SSebastian Grimberg @ref Utility 1036ba59ac12SSebastian Grimberg **/ 1037ba59ac12SSebastian Grimberg int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau, CeedInt m, CeedInt n) { 1038ba59ac12SSebastian Grimberg CeedScalar v[m]; 1039ba59ac12SSebastian Grimberg 1040ba59ac12SSebastian Grimberg // Check matrix shape 10416574a04fSJeremy L Thompson CeedCheck(n <= m, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute QR factorization with n > m"); 1042ba59ac12SSebastian Grimberg 1043ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 10441c66c397SJeremy L Thompson CeedScalar sigma = 0.0; 10451c66c397SJeremy L Thompson 1046ba59ac12SSebastian Grimberg if (i >= m - 1) { // last row of matrix, no reflection needed 1047ba59ac12SSebastian Grimberg tau[i] = 0.; 1048ba59ac12SSebastian Grimberg break; 1049ba59ac12SSebastian Grimberg } 1050ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 1051ba59ac12SSebastian Grimberg v[i] = mat[i + n * i]; 1052ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) { 1053ba59ac12SSebastian Grimberg v[j] = mat[i + n * j]; 1054ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 1055ba59ac12SSebastian Grimberg } 10561c66c397SJeremy L Thompson const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:m] 10571c66c397SJeremy L Thompson const CeedScalar R_ii = -copysign(norm, v[i]); 10581c66c397SJeremy L Thompson 1059ba59ac12SSebastian Grimberg v[i] -= R_ii; 1060ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 1061ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 1062ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 1063ba59ac12SSebastian Grimberg tau[i] = 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 1064ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] /= v[i]; 1065ba59ac12SSebastian Grimberg 1066ba59ac12SSebastian Grimberg // Apply Householder reflector to lower right panel 1067ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[i * n + i + 1], &v[i], tau[i], m - i, n - i - 1, n, 1); 1068ba59ac12SSebastian Grimberg // Save v 1069ba59ac12SSebastian Grimberg mat[i + n * i] = R_ii; 1070ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) mat[i + n * j] = v[j]; 1071ba59ac12SSebastian Grimberg } 1072ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 1073ba59ac12SSebastian Grimberg } 1074ba59ac12SSebastian Grimberg 1075ba59ac12SSebastian Grimberg /** 1076ba59ac12SSebastian Grimberg @brief Apply Householder Q matrix 1077ba59ac12SSebastian Grimberg 1078ca94c3ddSJeremy 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$. 1079ba59ac12SSebastian Grimberg 1080ba59ac12SSebastian Grimberg @param[in,out] mat_A Matrix to apply Householder Q to, in place 1081ba59ac12SSebastian Grimberg @param[in] mat_Q Householder Q matrix 1082ba59ac12SSebastian Grimberg @param[in] tau Householder scaling factors 1083ba59ac12SSebastian Grimberg @param[in] t_mode Transpose mode for application 1084ca94c3ddSJeremy L Thompson @param[in] m Number of rows in `A` 1085ca94c3ddSJeremy L Thompson @param[in] n Number of columns in `A` 1086ca94c3ddSJeremy L Thompson @param[in] k Number of elementary reflectors in Q, `k < m` 1087ca94c3ddSJeremy L Thompson @param[in] row Row stride in `A` 1088ca94c3ddSJeremy L Thompson @param[in] col Col stride in `A` 1089ba59ac12SSebastian Grimberg 1090ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1091ba59ac12SSebastian Grimberg 1092c4e3f59bSSebastian Grimberg @ref Utility 1093ba59ac12SSebastian Grimberg **/ 1094ba59ac12SSebastian Grimberg int CeedHouseholderApplyQ(CeedScalar *mat_A, const CeedScalar *mat_Q, const CeedScalar *tau, CeedTransposeMode t_mode, CeedInt m, CeedInt n, 1095ba59ac12SSebastian Grimberg CeedInt k, CeedInt row, CeedInt col) { 1096ba59ac12SSebastian Grimberg CeedScalar *v; 10971c66c397SJeremy L Thompson 1098ba59ac12SSebastian Grimberg CeedCall(CeedMalloc(m, &v)); 1099ba59ac12SSebastian Grimberg for (CeedInt ii = 0; ii < k; ii++) { 1100ba59ac12SSebastian Grimberg CeedInt i = t_mode == CEED_TRANSPOSE ? ii : k - 1 - ii; 1101ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] = mat_Q[j * k + i]; 1102ba59ac12SSebastian Grimberg // Apply Householder reflector (I - tau v v^T) collo_grad_1d^T 1103ba59ac12SSebastian Grimberg CeedCall(CeedHouseholderReflect(&mat_A[i * row], &v[i], tau[i], m - i, n, row, col)); 1104ba59ac12SSebastian Grimberg } 1105ba59ac12SSebastian Grimberg CeedCall(CeedFree(&v)); 1106ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 1107ba59ac12SSebastian Grimberg } 1108ba59ac12SSebastian Grimberg 1109ba59ac12SSebastian Grimberg /** 11102247a93fSRezgar Shakeri @brief Return pseudoinverse of a matrix 11112247a93fSRezgar Shakeri 11122247a93fSRezgar Shakeri @param[in] ceed Ceed context for error handling 11132247a93fSRezgar Shakeri @param[in] mat Row-major matrix to compute pseudoinverse of 11142247a93fSRezgar Shakeri @param[in] m Number of rows 11152247a93fSRezgar Shakeri @param[in] n Number of columns 11162247a93fSRezgar Shakeri @param[out] mat_pinv Row-major pseudoinverse matrix 11172247a93fSRezgar Shakeri 11182247a93fSRezgar Shakeri @return An error code: 0 - success, otherwise - failure 11192247a93fSRezgar Shakeri 11202247a93fSRezgar Shakeri @ref Utility 11212247a93fSRezgar Shakeri **/ 11221203703bSJeremy L Thompson int CeedMatrixPseudoinverse(Ceed ceed, const CeedScalar *mat, CeedInt m, CeedInt n, CeedScalar *mat_pinv) { 11232247a93fSRezgar Shakeri CeedScalar *tau, *I, *mat_copy; 11242247a93fSRezgar Shakeri 11252247a93fSRezgar Shakeri CeedCall(CeedCalloc(m, &tau)); 11262247a93fSRezgar Shakeri CeedCall(CeedCalloc(m * m, &I)); 11272247a93fSRezgar Shakeri CeedCall(CeedCalloc(m * n, &mat_copy)); 11282247a93fSRezgar Shakeri memcpy(mat_copy, mat, m * n * sizeof mat[0]); 11292247a93fSRezgar Shakeri 11302247a93fSRezgar Shakeri // QR Factorization, mat = Q R 11312247a93fSRezgar Shakeri CeedCall(CeedQRFactorization(ceed, mat_copy, tau, m, n)); 11322247a93fSRezgar Shakeri 11332247a93fSRezgar Shakeri // -- Apply Q^T, I = Q^T * I 11342247a93fSRezgar Shakeri for (CeedInt i = 0; i < m; i++) I[i * m + i] = 1.0; 11352247a93fSRezgar Shakeri CeedCall(CeedHouseholderApplyQ(I, mat_copy, tau, CEED_TRANSPOSE, m, m, n, m, 1)); 11362247a93fSRezgar Shakeri // -- Apply R_inv, mat_pinv = R_inv * Q^T 11372247a93fSRezgar Shakeri for (CeedInt j = 0; j < m; j++) { // Column j 11382247a93fSRezgar Shakeri mat_pinv[j + m * (n - 1)] = I[j + m * (n - 1)] / mat_copy[n * n - 1]; 11392247a93fSRezgar Shakeri for (CeedInt i = n - 2; i >= 0; i--) { // Row i 11402247a93fSRezgar Shakeri mat_pinv[j + m * i] = I[j + m * i]; 11412247a93fSRezgar Shakeri for (CeedInt k = i + 1; k < n; k++) mat_pinv[j + m * i] -= mat_copy[k + n * i] * mat_pinv[j + m * k]; 11422247a93fSRezgar Shakeri mat_pinv[j + m * i] /= mat_copy[i + n * i]; 11432247a93fSRezgar Shakeri } 11442247a93fSRezgar Shakeri } 11452247a93fSRezgar Shakeri 11462247a93fSRezgar Shakeri // Cleanup 11472247a93fSRezgar Shakeri CeedCall(CeedFree(&I)); 11482247a93fSRezgar Shakeri CeedCall(CeedFree(&tau)); 11492247a93fSRezgar Shakeri CeedCall(CeedFree(&mat_copy)); 11502247a93fSRezgar Shakeri return CEED_ERROR_SUCCESS; 11512247a93fSRezgar Shakeri } 11522247a93fSRezgar Shakeri 11532247a93fSRezgar Shakeri /** 1154ba59ac12SSebastian Grimberg @brief Return symmetric Schur decomposition of the symmetric matrix mat via symmetric QR factorization 1155ba59ac12SSebastian Grimberg 1156ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 1157ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 1158ba59ac12SSebastian Grimberg @param[out] lambda Vector of length n of eigenvalues 1159ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 1160ba59ac12SSebastian Grimberg 1161ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1162ba59ac12SSebastian Grimberg 1163ba59ac12SSebastian Grimberg @ref Utility 1164ba59ac12SSebastian Grimberg **/ 11652c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 11662c2ea1dbSJeremy L Thompson int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat, CeedScalar *lambda, CeedInt n) { 1167ba59ac12SSebastian Grimberg // Check bounds for clang-tidy 11686574a04fSJeremy L Thompson CeedCheck(n > 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute symmetric Schur decomposition of scalars"); 1169ba59ac12SSebastian Grimberg 1170ba59ac12SSebastian Grimberg CeedScalar v[n - 1], tau[n - 1], mat_T[n * n]; 1171ba59ac12SSebastian Grimberg 1172ba59ac12SSebastian Grimberg // Copy mat to mat_T and set mat to I 1173ba59ac12SSebastian Grimberg memcpy(mat_T, mat, n * n * sizeof(mat[0])); 1174ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 1175ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) mat[j + n * i] = (i == j) ? 1 : 0; 1176ba59ac12SSebastian Grimberg } 1177ba59ac12SSebastian Grimberg 1178ba59ac12SSebastian Grimberg // Reduce to tridiagonal 1179ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - 1; i++) { 1180ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 1181ba59ac12SSebastian Grimberg CeedScalar sigma = 0.0; 11821c66c397SJeremy L Thompson 1183ba59ac12SSebastian Grimberg v[i] = mat_T[i + n * (i + 1)]; 1184ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 1185ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 1186ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 1187ba59ac12SSebastian Grimberg } 11881c66c397SJeremy L Thompson const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:n-1] 11891c66c397SJeremy L Thompson const CeedScalar R_ii = -copysign(norm, v[i]); 11901c66c397SJeremy L Thompson 1191ba59ac12SSebastian Grimberg v[i] -= R_ii; 1192ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 1193ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 1194ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 1195ba59ac12SSebastian Grimberg tau[i] = i == n - 2 ? 2 : 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 1196ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) v[j] /= v[i]; 1197ba59ac12SSebastian Grimberg 1198ba59ac12SSebastian Grimberg // Update sub and super diagonal 1199ba59ac12SSebastian Grimberg for (CeedInt j = i + 2; j < n; j++) { 1200ba59ac12SSebastian Grimberg mat_T[i + n * j] = 0; 1201ba59ac12SSebastian Grimberg mat_T[j + n * i] = 0; 1202ba59ac12SSebastian Grimberg } 1203ba59ac12SSebastian Grimberg // Apply symmetric Householder reflector to lower right panel 1204ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 1205ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), 1, n); 1206ba59ac12SSebastian Grimberg 1207ba59ac12SSebastian Grimberg // Save v 1208ba59ac12SSebastian Grimberg mat_T[i + n * (i + 1)] = R_ii; 1209ba59ac12SSebastian Grimberg mat_T[(i + 1) + n * i] = R_ii; 1210ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 1211ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = v[j]; 1212ba59ac12SSebastian Grimberg } 1213ba59ac12SSebastian Grimberg } 1214ba59ac12SSebastian Grimberg // Backwards accumulation of Q 1215ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 1216ba59ac12SSebastian Grimberg if (tau[i] > 0.0) { 1217ba59ac12SSebastian Grimberg v[i] = 1; 1218ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 1219ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 1220ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = 0; 1221ba59ac12SSebastian Grimberg } 1222ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 1223ba59ac12SSebastian Grimberg } 1224ba59ac12SSebastian Grimberg } 1225ba59ac12SSebastian Grimberg 1226ba59ac12SSebastian Grimberg // Reduce sub and super diagonal 1227ba59ac12SSebastian Grimberg CeedInt p = 0, q = 0, itr = 0, max_itr = n * n * n * n; 1228ba59ac12SSebastian Grimberg CeedScalar tol = CEED_EPSILON; 1229ba59ac12SSebastian Grimberg 1230ba59ac12SSebastian Grimberg while (itr < max_itr) { 1231ba59ac12SSebastian Grimberg // Update p, q, size of reduced portions of diagonal 1232ba59ac12SSebastian Grimberg p = 0; 1233ba59ac12SSebastian Grimberg q = 0; 1234ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 1235ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) q += 1; 1236ba59ac12SSebastian Grimberg else break; 1237ba59ac12SSebastian Grimberg } 1238ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - q - 1; i++) { 1239ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) p += 1; 1240ba59ac12SSebastian Grimberg else break; 1241ba59ac12SSebastian Grimberg } 1242ba59ac12SSebastian Grimberg if (q == n - 1) break; // Finished reducing 1243ba59ac12SSebastian Grimberg 1244ba59ac12SSebastian Grimberg // Reduce tridiagonal portion 1245ba59ac12SSebastian Grimberg CeedScalar t_nn = mat_T[(n - 1 - q) + n * (n - 1 - q)], t_nnm1 = mat_T[(n - 2 - q) + n * (n - 1 - q)]; 1246ba59ac12SSebastian Grimberg CeedScalar d = (mat_T[(n - 2 - q) + n * (n - 2 - q)] - t_nn) / 2; 1247ba59ac12SSebastian Grimberg CeedScalar mu = t_nn - t_nnm1 * t_nnm1 / (d + copysign(sqrt(d * d + t_nnm1 * t_nnm1), d)); 1248ba59ac12SSebastian Grimberg CeedScalar x = mat_T[p + n * p] - mu; 1249ba59ac12SSebastian Grimberg CeedScalar z = mat_T[p + n * (p + 1)]; 12501c66c397SJeremy L Thompson 1251ba59ac12SSebastian Grimberg for (CeedInt k = p; k < n - q - 1; k++) { 1252ba59ac12SSebastian Grimberg // Compute Givens rotation 1253ba59ac12SSebastian Grimberg CeedScalar c = 1, s = 0; 12541c66c397SJeremy L Thompson 1255ba59ac12SSebastian Grimberg if (fabs(z) > tol) { 1256ba59ac12SSebastian Grimberg if (fabs(z) > fabs(x)) { 12571c66c397SJeremy L Thompson const CeedScalar tau = -x / z; 12581c66c397SJeremy L Thompson 12591c66c397SJeremy L Thompson s = 1 / sqrt(1 + tau * tau); 12601c66c397SJeremy L Thompson c = s * tau; 1261ba59ac12SSebastian Grimberg } else { 12621c66c397SJeremy L Thompson const CeedScalar tau = -z / x; 12631c66c397SJeremy L Thompson 12641c66c397SJeremy L Thompson c = 1 / sqrt(1 + tau * tau); 12651c66c397SJeremy L Thompson s = c * tau; 1266ba59ac12SSebastian Grimberg } 1267ba59ac12SSebastian Grimberg } 1268ba59ac12SSebastian Grimberg 1269ba59ac12SSebastian Grimberg // Apply Givens rotation to T 1270ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 1271ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_TRANSPOSE, k, k + 1, n, n); 1272ba59ac12SSebastian Grimberg 1273ba59ac12SSebastian Grimberg // Apply Givens rotation to Q 1274ba59ac12SSebastian Grimberg CeedGivensRotation(mat, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 1275ba59ac12SSebastian Grimberg 1276ba59ac12SSebastian Grimberg // Update x, z 1277ba59ac12SSebastian Grimberg if (k < n - q - 2) { 1278ba59ac12SSebastian Grimberg x = mat_T[k + n * (k + 1)]; 1279ba59ac12SSebastian Grimberg z = mat_T[k + n * (k + 2)]; 1280ba59ac12SSebastian Grimberg } 1281ba59ac12SSebastian Grimberg } 1282ba59ac12SSebastian Grimberg itr++; 1283ba59ac12SSebastian Grimberg } 1284ba59ac12SSebastian Grimberg 1285ba59ac12SSebastian Grimberg // Save eigenvalues 1286ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) lambda[i] = mat_T[i + n * i]; 1287ba59ac12SSebastian Grimberg 1288ba59ac12SSebastian Grimberg // Check convergence 12896574a04fSJeremy L Thompson CeedCheck(itr < max_itr || q > n, ceed, CEED_ERROR_MINOR, "Symmetric QR failed to converge"); 1290ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 1291ba59ac12SSebastian Grimberg } 12922c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 1293ba59ac12SSebastian Grimberg 1294ba59ac12SSebastian Grimberg /** 1295ba59ac12SSebastian Grimberg @brief Return Simultaneous Diagonalization of two matrices. 1296ba59ac12SSebastian Grimberg 1297ca94c3ddSJeremy L Thompson This solves the generalized eigenvalue problem `A x = lambda B x`, where `A` and `B` are symmetric and `B` is positive definite. 1298ca94c3ddSJeremy L Thompson We generate the matrix `X` and vector `Lambda` such that `X^T A X = Lambda` and `X^T B X = I`. 1299ca94c3ddSJeremy L Thompson This is equivalent to the LAPACK routine 'sygv' with `TYPE = 1`. 1300ba59ac12SSebastian Grimberg 1301ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 1302ba59ac12SSebastian Grimberg @param[in] mat_A Row-major matrix to be factorized with eigenvalues 1303ba59ac12SSebastian Grimberg @param[in] mat_B Row-major matrix to be factorized to identity 1304ba59ac12SSebastian Grimberg @param[out] mat_X Row-major orthogonal matrix 1305ca94c3ddSJeremy L Thompson @param[out] lambda Vector of length `n` of generalized eigenvalues 1306ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 1307ba59ac12SSebastian Grimberg 1308ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1309ba59ac12SSebastian Grimberg 1310ba59ac12SSebastian Grimberg @ref Utility 1311ba59ac12SSebastian Grimberg **/ 13122c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 13132c2ea1dbSJeremy L Thompson int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A, CeedScalar *mat_B, CeedScalar *mat_X, CeedScalar *lambda, CeedInt n) { 1314ba59ac12SSebastian Grimberg CeedScalar *mat_C, *mat_G, *vec_D; 13151c66c397SJeremy L Thompson 1316ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_C)); 1317ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_G)); 1318ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n, &vec_D)); 1319ba59ac12SSebastian Grimberg 1320ba59ac12SSebastian Grimberg // Compute B = G D G^T 1321ba59ac12SSebastian Grimberg memcpy(mat_G, mat_B, n * n * sizeof(mat_B[0])); 1322ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_G, vec_D, n)); 1323ba59ac12SSebastian Grimberg 1324ba59ac12SSebastian Grimberg // Sort eigenvalues 1325ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 1326ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 1327ba59ac12SSebastian Grimberg if (fabs(vec_D[j]) > fabs(vec_D[j + 1])) { 13281c66c397SJeremy L Thompson CeedScalarSwap(vec_D[j], vec_D[j + 1]); 13291c66c397SJeremy L Thompson for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_G[k * n + j], mat_G[k * n + j + 1]); 1330ba59ac12SSebastian Grimberg } 1331ba59ac12SSebastian Grimberg } 1332ba59ac12SSebastian Grimberg } 1333ba59ac12SSebastian Grimberg 1334ba59ac12SSebastian Grimberg // Compute C = (G D^1/2)^-1 A (G D^1/2)^-T 1335ba59ac12SSebastian Grimberg // = D^-1/2 G^T A G D^-1/2 1336ba59ac12SSebastian Grimberg // -- D = D^-1/2 1337ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) vec_D[i] = 1. / sqrt(vec_D[i]); 1338ba59ac12SSebastian Grimberg // -- G = G D^-1/2 1339ba59ac12SSebastian Grimberg // -- C = D^-1/2 G^T 1340ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 1341ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) { 1342ba59ac12SSebastian Grimberg mat_G[i * n + j] *= vec_D[j]; 1343ba59ac12SSebastian Grimberg mat_C[j * n + i] = mat_G[i * n + j]; 1344ba59ac12SSebastian Grimberg } 1345ba59ac12SSebastian Grimberg } 1346ba59ac12SSebastian Grimberg // -- X = (D^-1/2 G^T) A 1347ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_C, (const CeedScalar *)mat_A, mat_X, n, n, n)); 1348ba59ac12SSebastian Grimberg // -- C = (D^-1/2 G^T A) (G D^-1/2) 1349ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_X, (const CeedScalar *)mat_G, mat_C, n, n, n)); 1350ba59ac12SSebastian Grimberg 1351ba59ac12SSebastian Grimberg // Compute Q^T C Q = lambda 1352ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_C, lambda, n)); 1353ba59ac12SSebastian Grimberg 1354ba59ac12SSebastian Grimberg // Sort eigenvalues 1355ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 1356ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 1357ba59ac12SSebastian Grimberg if (fabs(lambda[j]) > fabs(lambda[j + 1])) { 13581c66c397SJeremy L Thompson CeedScalarSwap(lambda[j], lambda[j + 1]); 13591c66c397SJeremy L Thompson for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_C[k * n + j], mat_C[k * n + j + 1]); 1360ba59ac12SSebastian Grimberg } 1361ba59ac12SSebastian Grimberg } 1362ba59ac12SSebastian Grimberg } 1363ba59ac12SSebastian Grimberg 1364ba59ac12SSebastian Grimberg // Set X = (G D^1/2)^-T Q 1365ba59ac12SSebastian Grimberg // = G D^-1/2 Q 1366ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_G, (const CeedScalar *)mat_C, mat_X, n, n, n)); 1367ba59ac12SSebastian Grimberg 1368ba59ac12SSebastian Grimberg // Cleanup 1369ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_C)); 1370ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_G)); 1371ba59ac12SSebastian Grimberg CeedCall(CeedFree(&vec_D)); 1372ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 1373ba59ac12SSebastian Grimberg } 13742c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 1375ba59ac12SSebastian Grimberg 13767a982d89SJeremy L. Thompson /// @} 13777a982d89SJeremy L. Thompson 13787a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 13797a982d89SJeremy L. Thompson /// CeedBasis Public API 13807a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 13817a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser 1382d7b241e6Sjeremylt /// @{ 1383d7b241e6Sjeremylt 1384b11c1e72Sjeremylt /** 1385ca94c3ddSJeremy L Thompson @brief Create a tensor-product basis for \f$H^1\f$ discretizations 1386b11c1e72Sjeremylt 1387ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1388ea61e9acSJeremy L Thompson @param[in] dim Topological dimension 1389ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1390ea61e9acSJeremy L Thompson @param[in] P_1d Number of nodes in one dimension 1391ea61e9acSJeremy L Thompson @param[in] Q_1d Number of quadrature points in one dimension 1392ca94c3ddSJeremy L Thompson @param[in] interp_1d Row-major (`Q_1d * P_1d`) matrix expressing the values of nodal basis functions at quadrature points 1393ca94c3ddSJeremy L Thompson @param[in] grad_1d Row-major (`Q_1d * P_1d`) matrix expressing derivatives of nodal basis functions at quadrature points 1394ca94c3ddSJeremy 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]` 1395ca94c3ddSJeremy L Thompson @param[in] q_weight_1d Array of length `Q_1d` holding the quadrature weights on the reference element 1396ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1397b11c1e72Sjeremylt 1398b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1399dfdf5a53Sjeremylt 14007a982d89SJeremy L. Thompson @ref User 1401b11c1e72Sjeremylt **/ 14022b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, 14032b730f8bSJeremy L Thompson const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis *basis) { 14045fe0d4faSjeremylt if (!ceed->BasisCreateTensorH1) { 14055fe0d4faSjeremylt Ceed delegate; 14066574a04fSJeremy L Thompson 14072b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 14081ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateTensorH1"); 14092b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 14109bc66399SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 1411e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14125fe0d4faSjeremylt } 1413e15f9bd0SJeremy L Thompson 1414ca94c3ddSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value"); 1415ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1416ca94c3ddSJeremy L Thompson CeedCheck(P_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1417ca94c3ddSJeremy L Thompson CeedCheck(Q_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1418227444bfSJeremy L Thompson 14192b730f8bSJeremy L Thompson CeedElemTopology topo = dim == 1 ? CEED_TOPOLOGY_LINE : dim == 2 ? CEED_TOPOLOGY_QUAD : CEED_TOPOLOGY_HEX; 1420e15f9bd0SJeremy L Thompson 14212b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1422db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1423d1d35e2fSjeremylt (*basis)->ref_count = 1; 14246402da51SJeremy L Thompson (*basis)->is_tensor_basis = true; 1425d7b241e6Sjeremylt (*basis)->dim = dim; 1426d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 1427d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 1428d1d35e2fSjeremylt (*basis)->P_1d = P_1d; 1429d1d35e2fSjeremylt (*basis)->Q_1d = Q_1d; 1430d1d35e2fSjeremylt (*basis)->P = CeedIntPow(P_1d, dim); 1431d1d35e2fSjeremylt (*basis)->Q = CeedIntPow(Q_1d, dim); 1432c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 14332b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_ref_1d)); 14342b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_weight_1d)); 1435ff3a0f91SJeremy L Thompson if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d * sizeof(q_ref_1d[0])); 14362b730f8bSJeremy L Thompson if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, Q_1d * sizeof(q_weight_1d[0])); 14372b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->interp_1d)); 14382b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->grad_1d)); 14392b730f8bSJeremy L Thompson if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, Q_1d * P_1d * sizeof(interp_1d[0])); 1440ff3a0f91SJeremy L Thompson if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d * P_1d * sizeof(grad_1d[0])); 14412b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, *basis)); 1442e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1443d7b241e6Sjeremylt } 1444d7b241e6Sjeremylt 1445b11c1e72Sjeremylt /** 1446ca94c3ddSJeremy L Thompson @brief Create a tensor-product \f$H^1\f$ Lagrange basis 1447b11c1e72Sjeremylt 1448ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1449ea61e9acSJeremy L Thompson @param[in] dim Topological dimension of element 1450ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1451ea61e9acSJeremy L Thompson @param[in] P Number of Gauss-Lobatto nodes in one dimension. 1452ca94c3ddSJeremy L Thompson The polynomial degree of the resulting `Q_k` element is `k = P - 1`. 1453ea61e9acSJeremy L Thompson @param[in] Q Number of quadrature points in one dimension. 1454ca94c3ddSJeremy L Thompson @param[in] quad_mode Distribution of the `Q` quadrature points (affects order of accuracy for the quadrature) 1455ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1456b11c1e72Sjeremylt 1457b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1458dfdf5a53Sjeremylt 14597a982d89SJeremy L. Thompson @ref User 1460b11c1e72Sjeremylt **/ 14612b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis) { 1462d7b241e6Sjeremylt // Allocate 1463c8c3fa7dSJeremy L Thompson int ierr = CEED_ERROR_SUCCESS; 14642b730f8bSJeremy L Thompson CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, *q_weight_1d; 14654d537eeaSYohann 1466ca94c3ddSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value"); 1467ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1468ca94c3ddSJeremy L Thompson CeedCheck(P > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1469ca94c3ddSJeremy L Thompson CeedCheck(Q > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1470227444bfSJeremy L Thompson 1471e15f9bd0SJeremy L Thompson // Get Nodes and Weights 14722b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &interp_1d)); 14732b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &grad_1d)); 14742b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P, &nodes)); 14752b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_ref_1d)); 14762b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_weight_1d)); 14772b730f8bSJeremy L Thompson if (CeedLobattoQuadrature(P, nodes, NULL) != CEED_ERROR_SUCCESS) goto cleanup; 1478d1d35e2fSjeremylt switch (quad_mode) { 1479d7b241e6Sjeremylt case CEED_GAUSS: 1480d1d35e2fSjeremylt ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d); 1481d7b241e6Sjeremylt break; 1482d7b241e6Sjeremylt case CEED_GAUSS_LOBATTO: 1483d1d35e2fSjeremylt ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d); 1484d7b241e6Sjeremylt break; 1485d7b241e6Sjeremylt } 14862b730f8bSJeremy L Thompson if (ierr != CEED_ERROR_SUCCESS) goto cleanup; 1487e15f9bd0SJeremy L Thompson 1488d7b241e6Sjeremylt // Build B, D matrix 1489d7b241e6Sjeremylt // Fornberg, 1998 1490c8c3fa7dSJeremy L Thompson for (CeedInt i = 0; i < Q; i++) { 1491d7b241e6Sjeremylt c1 = 1.0; 1492d1d35e2fSjeremylt c3 = nodes[0] - q_ref_1d[i]; 1493d1d35e2fSjeremylt interp_1d[i * P + 0] = 1.0; 1494c8c3fa7dSJeremy L Thompson for (CeedInt j = 1; j < P; j++) { 1495d7b241e6Sjeremylt c2 = 1.0; 1496d7b241e6Sjeremylt c4 = c3; 1497d1d35e2fSjeremylt c3 = nodes[j] - q_ref_1d[i]; 1498c8c3fa7dSJeremy L Thompson for (CeedInt k = 0; k < j; k++) { 1499d7b241e6Sjeremylt dx = nodes[j] - nodes[k]; 1500d7b241e6Sjeremylt c2 *= dx; 1501d7b241e6Sjeremylt if (k == j - 1) { 1502d1d35e2fSjeremylt grad_1d[i * P + j] = c1 * (interp_1d[i * P + k] - c4 * grad_1d[i * P + k]) / c2; 1503d1d35e2fSjeremylt interp_1d[i * P + j] = -c1 * c4 * interp_1d[i * P + k] / c2; 1504d7b241e6Sjeremylt } 1505d1d35e2fSjeremylt grad_1d[i * P + k] = (c3 * grad_1d[i * P + k] - interp_1d[i * P + k]) / dx; 1506d1d35e2fSjeremylt interp_1d[i * P + k] = c3 * interp_1d[i * P + k] / dx; 1507d7b241e6Sjeremylt } 1508d7b241e6Sjeremylt c1 = c2; 1509d7b241e6Sjeremylt } 1510d7b241e6Sjeremylt } 15119ac7b42eSJeremy L Thompson // Pass to CeedBasisCreateTensorH1 15122b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 1513e15f9bd0SJeremy L Thompson cleanup: 15142b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_1d)); 15152b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_1d)); 15162b730f8bSJeremy L Thompson CeedCall(CeedFree(&nodes)); 15172b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref_1d)); 15182b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight_1d)); 1519e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1520d7b241e6Sjeremylt } 1521d7b241e6Sjeremylt 1522b11c1e72Sjeremylt /** 1523ca94c3ddSJeremy L Thompson @brief Create a non tensor-product basis for \f$H^1\f$ discretizations 1524a8de75f0Sjeremylt 1525ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1526e00f3be8SJames Wright @param[in] topo Topology of element, e.g. hypercube, simplex, etc 1527ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1528ea61e9acSJeremy L Thompson @param[in] num_nodes Total number of nodes 1529ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1530ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`num_qpts * num_nodes`) matrix expressing the values of nodal basis functions at quadrature points 1531ca94c3ddSJeremy L Thompson @param[in] grad Row-major (`dim * num_qpts * num_nodes`) matrix expressing derivatives of nodal basis functions at quadrature points 1532*fda26546SJeremy L Thompson @param[in] q_ref Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element 1533ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1534ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1535a8de75f0Sjeremylt 1536a8de75f0Sjeremylt @return An error code: 0 - success, otherwise - failure 1537a8de75f0Sjeremylt 15387a982d89SJeremy L. Thompson @ref User 1539a8de75f0Sjeremylt **/ 15402b730f8bSJeremy L Thompson int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 15412b730f8bSJeremy L Thompson const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1542d1d35e2fSjeremylt CeedInt P = num_nodes, Q = num_qpts, dim = 0; 1543a8de75f0Sjeremylt 15445fe0d4faSjeremylt if (!ceed->BasisCreateH1) { 15455fe0d4faSjeremylt Ceed delegate; 15466574a04fSJeremy L Thompson 15472b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 15481ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateH1"); 15492b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis)); 15509bc66399SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 1551e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15525fe0d4faSjeremylt } 15535fe0d4faSjeremylt 1554ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1555ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1556ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1557227444bfSJeremy L Thompson 15582b730f8bSJeremy L Thompson CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1559a8de75f0Sjeremylt 1560db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1561db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1562d1d35e2fSjeremylt (*basis)->ref_count = 1; 15636402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1564a8de75f0Sjeremylt (*basis)->dim = dim; 1565d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 1566d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 1567a8de75f0Sjeremylt (*basis)->P = P; 1568a8de75f0Sjeremylt (*basis)->Q = Q; 1569c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 15702b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * dim, &(*basis)->q_ref_1d)); 15712b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &(*basis)->q_weight_1d)); 1572ff3a0f91SJeremy L Thompson if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1573ff3a0f91SJeremy L Thompson if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 15742b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * P, &(*basis)->interp)); 15752b730f8bSJeremy L Thompson CeedCall(CeedCalloc(dim * Q * P, &(*basis)->grad)); 1576ff3a0f91SJeremy L Thompson if (interp) memcpy((*basis)->interp, interp, Q * P * sizeof(interp[0])); 1577ff3a0f91SJeremy L Thompson if (grad) memcpy((*basis)->grad, grad, dim * Q * P * sizeof(grad[0])); 15782b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, *basis)); 1579e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1580a8de75f0Sjeremylt } 1581a8de75f0Sjeremylt 1582a8de75f0Sjeremylt /** 1583859c15bbSJames Wright @brief Create a non tensor-product basis for \f$H(\mathrm{div})\f$ discretizations 158450c301a5SRezgar Shakeri 1585ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1586ea61e9acSJeremy 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 1587ea61e9acSJeremy L Thompson @param[in] num_comp Number of components (usually 1 for vectors in H(div) bases) 1588ca94c3ddSJeremy L Thompson @param[in] num_nodes Total number of nodes (DoFs per element) 1589ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1590ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points 1591ca94c3ddSJeremy L Thompson @param[in] div Row-major (`num_qpts * num_nodes`) matrix expressing divergence of basis functions at quadrature points 1592ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element 1593ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1594ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 159550c301a5SRezgar Shakeri 159650c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 159750c301a5SRezgar Shakeri 159850c301a5SRezgar Shakeri @ref User 159950c301a5SRezgar Shakeri **/ 16002b730f8bSJeremy L Thompson int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 16012b730f8bSJeremy L Thompson const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 160250c301a5SRezgar Shakeri CeedInt Q = num_qpts, P = num_nodes, dim = 0; 1603c4e3f59bSSebastian Grimberg 160450c301a5SRezgar Shakeri if (!ceed->BasisCreateHdiv) { 160550c301a5SRezgar Shakeri Ceed delegate; 16066574a04fSJeremy L Thompson 16072b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 16086574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv"); 16092b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis)); 16109bc66399SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 161150c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 161250c301a5SRezgar Shakeri } 161350c301a5SRezgar Shakeri 1614ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1615ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1616ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1617227444bfSJeremy L Thompson 1618c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1619c4e3f59bSSebastian Grimberg 1620db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1621db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 162250c301a5SRezgar Shakeri (*basis)->ref_count = 1; 16236402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 162450c301a5SRezgar Shakeri (*basis)->dim = dim; 162550c301a5SRezgar Shakeri (*basis)->topo = topo; 162650c301a5SRezgar Shakeri (*basis)->num_comp = num_comp; 162750c301a5SRezgar Shakeri (*basis)->P = P; 162850c301a5SRezgar Shakeri (*basis)->Q = Q; 1629c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HDIV; 16302b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 16312b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 163250c301a5SRezgar Shakeri if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 163350c301a5SRezgar Shakeri if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 16342b730f8bSJeremy L Thompson CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 16352b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * P, &(*basis)->div)); 163650c301a5SRezgar Shakeri if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 163750c301a5SRezgar Shakeri if (div) memcpy((*basis)->div, div, Q * P * sizeof(div[0])); 16382b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, q_weight, *basis)); 163950c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 164050c301a5SRezgar Shakeri } 164150c301a5SRezgar Shakeri 164250c301a5SRezgar Shakeri /** 16434385fb7fSSebastian Grimberg @brief Create a non tensor-product basis for \f$H(\mathrm{curl})\f$ discretizations 1644c4e3f59bSSebastian Grimberg 1645ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1646c4e3f59bSSebastian Grimberg @param[in] topo Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below 1647ca94c3ddSJeremy L Thompson @param[in] num_comp Number of components (usually 1 for vectors in \f$H(\mathrm{curl})\f$ bases) 1648ca94c3ddSJeremy L Thompson @param[in] num_nodes Total number of nodes (DoFs per element) 1649c4e3f59bSSebastian Grimberg @param[in] num_qpts Total number of quadrature points 1650ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points 1651ca94c3ddSJeremy 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 1652ca94c3ddSJeremy 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 1655c4e3f59bSSebastian Grimberg 1656c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1657c4e3f59bSSebastian Grimberg 1658c4e3f59bSSebastian Grimberg @ref User 1659c4e3f59bSSebastian Grimberg **/ 1660c4e3f59bSSebastian Grimberg int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 1661c4e3f59bSSebastian Grimberg const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1662c4e3f59bSSebastian Grimberg CeedInt Q = num_qpts, P = num_nodes, dim = 0, curl_comp = 0; 1663c4e3f59bSSebastian Grimberg 1664d075f50bSSebastian Grimberg if (!ceed->BasisCreateHcurl) { 1665c4e3f59bSSebastian Grimberg Ceed delegate; 16666574a04fSJeremy L Thompson 1667c4e3f59bSSebastian Grimberg CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 16686574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl"); 1669c4e3f59bSSebastian Grimberg CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis)); 16709bc66399SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 1671c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1672c4e3f59bSSebastian Grimberg } 1673c4e3f59bSSebastian Grimberg 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"); 1677c4e3f59bSSebastian Grimberg 1678c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1679c4e3f59bSSebastian Grimberg curl_comp = (dim < 3) ? 1 : dim; 1680c4e3f59bSSebastian Grimberg 1681db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1682db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1683c4e3f59bSSebastian Grimberg (*basis)->ref_count = 1; 16846402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1685c4e3f59bSSebastian Grimberg (*basis)->dim = dim; 1686c4e3f59bSSebastian Grimberg (*basis)->topo = topo; 1687c4e3f59bSSebastian Grimberg (*basis)->num_comp = num_comp; 1688c4e3f59bSSebastian Grimberg (*basis)->P = P; 1689c4e3f59bSSebastian Grimberg (*basis)->Q = Q; 1690c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HCURL; 1691c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 1692c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 1693c4e3f59bSSebastian Grimberg if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1694c4e3f59bSSebastian Grimberg if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 1695c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 1696c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(curl_comp * Q * P, &(*basis)->curl)); 1697c4e3f59bSSebastian Grimberg if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 1698c4e3f59bSSebastian Grimberg if (curl) memcpy((*basis)->curl, curl, curl_comp * Q * P * sizeof(curl[0])); 1699c4e3f59bSSebastian Grimberg CeedCall(ceed->BasisCreateHcurl(topo, dim, P, Q, interp, curl, q_ref, q_weight, *basis)); 1700c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1701c4e3f59bSSebastian Grimberg } 1702c4e3f59bSSebastian Grimberg 1703c4e3f59bSSebastian Grimberg /** 1704ca94c3ddSJeremy L Thompson @brief Create a `CeedBasis` for projection from the nodes of `basis_from` to the nodes of `basis_to`. 1705ba59ac12SSebastian Grimberg 1706ca94c3ddSJeremy L Thompson Only @ref CEED_EVAL_INTERP will be valid for the new basis, `basis_project`. 1707ca94c3ddSJeremy L Thompson For \f$H^1\f$ spaces, @ref CEED_EVAL_GRAD will also be valid. 1708ca94c3ddSJeremy L Thompson The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization. 1709ca94c3ddSJeremy L Thompson The gradient (for the \f$H^1\f$ case) is given by `grad_project = interp_to^+ * grad_from`. 171015ad3917SSebastian Grimberg 171115ad3917SSebastian Grimberg Note: `basis_from` and `basis_to` must have compatible quadrature spaces. 171215ad3917SSebastian Grimberg 17139fd66db6SSebastian Grimberg Note: `basis_project` will have the same number of components as `basis_from`, regardless of the number of components that `basis_to` has. 17149fd66db6SSebastian Grimberg If `basis_from` has 3 components and `basis_to` has 5 components, then `basis_project` will have 3 components. 1715f113e5dcSJeremy L Thompson 1716e104ad11SJames Wright Note: If either `basis_from` or `basis_to` are non-tensor, then `basis_project` will also be non-tensor 1717e104ad11SJames Wright 1718ca94c3ddSJeremy L Thompson @param[in] basis_from `CeedBasis` to prolong from 1719ca94c3ddSJeremy L Thompson @param[in] basis_to `CeedBasis` to prolong to 1720ca94c3ddSJeremy L Thompson @param[out] basis_project Address of the variable where the newly created `CeedBasis` will be stored 1721f113e5dcSJeremy L Thompson 1722f113e5dcSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1723f113e5dcSJeremy L Thompson 1724f113e5dcSJeremy L Thompson @ref User 1725f113e5dcSJeremy L Thompson **/ 17262b730f8bSJeremy L Thompson int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project) { 1727f113e5dcSJeremy L Thompson Ceed ceed; 1728e104ad11SJames Wright bool create_tensor; 17291c66c397SJeremy L Thompson CeedInt dim, num_comp; 1730097cc795SJames Wright CeedScalar *interp_project, *grad_project; 17311c66c397SJeremy L Thompson 17322b730f8bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis_to, &ceed)); 1733f113e5dcSJeremy L Thompson 1734ecc88aebSJeremy L Thompson // Create projection matrix 17352b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateProjectionMatrices(basis_from, basis_to, &interp_project, &grad_project)); 1736f113e5dcSJeremy L Thompson 1737f113e5dcSJeremy L Thompson // Build basis 1738e104ad11SJames Wright { 1739e104ad11SJames Wright bool is_tensor_to, is_tensor_from; 1740e104ad11SJames Wright 1741e104ad11SJames Wright CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to)); 1742e104ad11SJames Wright CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from)); 1743e104ad11SJames Wright create_tensor = is_tensor_from && is_tensor_to; 1744e104ad11SJames Wright } 17452b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_to, &dim)); 17462b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_from, &num_comp)); 1747e104ad11SJames Wright if (create_tensor) { 1748f113e5dcSJeremy L Thompson CeedInt P_1d_to, P_1d_from; 17491c66c397SJeremy L Thompson 17502b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_1d_from)); 17512b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_1d_to)); 1752097cc795SJames Wright CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, interp_project, grad_project, NULL, NULL, basis_project)); 1753f113e5dcSJeremy L Thompson } else { 1754de05fbb2SSebastian Grimberg // Even if basis_to and basis_from are not H1, the resulting basis is H1 for interpolation to work 1755f113e5dcSJeremy L Thompson CeedInt num_nodes_to, num_nodes_from; 17561c66c397SJeremy L Thompson CeedElemTopology topo; 17571c66c397SJeremy L Thompson 1758e00f3be8SJames Wright CeedCall(CeedBasisGetTopology(basis_from, &topo)); 17592b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_from, &num_nodes_from)); 17602b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_to, &num_nodes_to)); 1761097cc795SJames Wright CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, NULL, NULL, basis_project)); 1762f113e5dcSJeremy L Thompson } 1763f113e5dcSJeremy L Thompson 1764f113e5dcSJeremy L Thompson // Cleanup 17652b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_project)); 17662b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_project)); 17679bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 1768f113e5dcSJeremy L Thompson return CEED_ERROR_SUCCESS; 1769f113e5dcSJeremy L Thompson } 1770f113e5dcSJeremy L Thompson 1771f113e5dcSJeremy L Thompson /** 1772ca94c3ddSJeremy L Thompson @brief Copy the pointer to a `CeedBasis`. 17739560d06aSjeremylt 1774ca94c3ddSJeremy 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`. 1775ca94c3ddSJeremy L Thompson This `CeedBasis` will be destroyed if `*basis_copy` is the only reference to this `CeedBasis`. 1776ea61e9acSJeremy L Thompson 1777ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to copy reference to 1778ea61e9acSJeremy L Thompson @param[in,out] basis_copy Variable to store copied reference 17799560d06aSjeremylt 17809560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 17819560d06aSjeremylt 17829560d06aSjeremylt @ref User 17839560d06aSjeremylt **/ 17849560d06aSjeremylt int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) { 1785356036faSJeremy L Thompson if (basis != CEED_BASIS_NONE) CeedCall(CeedBasisReference(basis)); 17862b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(basis_copy)); 17879560d06aSjeremylt *basis_copy = basis; 17889560d06aSjeremylt return CEED_ERROR_SUCCESS; 17899560d06aSjeremylt } 17909560d06aSjeremylt 17919560d06aSjeremylt /** 1792ca94c3ddSJeremy L Thompson @brief View a `CeedBasis` 17937a982d89SJeremy L. Thompson 1794ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to view 1795ca94c3ddSJeremy L Thompson @param[in] stream Stream to view to, e.g., `stdout` 17967a982d89SJeremy L. Thompson 17977a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 17987a982d89SJeremy L. Thompson 17997a982d89SJeremy L. Thompson @ref User 18007a982d89SJeremy L. Thompson **/ 18017a982d89SJeremy L. Thompson int CeedBasisView(CeedBasis basis, FILE *stream) { 18021203703bSJeremy L Thompson bool is_tensor_basis; 18031203703bSJeremy L Thompson CeedElemTopology topo; 18041203703bSJeremy L Thompson CeedFESpace fe_space; 18051203703bSJeremy L Thompson 18061203703bSJeremy L Thompson // Basis data 18071203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 18081203703bSJeremy L Thompson CeedCall(CeedBasisGetTopology(basis, &topo)); 18091203703bSJeremy L Thompson CeedCall(CeedBasisGetFESpace(basis, &fe_space)); 18102b730f8bSJeremy L Thompson 181150c301a5SRezgar Shakeri // Print FE space and element topology of the basis 1812edf04919SJeremy L Thompson fprintf(stream, "CeedBasis in a %s on a %s element\n", CeedFESpaces[fe_space], CeedElemTopologies[topo]); 18131203703bSJeremy L Thompson if (is_tensor_basis) { 1814edf04919SJeremy L Thompson fprintf(stream, " P: %" CeedInt_FMT "\n Q: %" CeedInt_FMT "\n", basis->P_1d, basis->Q_1d); 181550c301a5SRezgar Shakeri } else { 1816edf04919SJeremy L Thompson fprintf(stream, " P: %" CeedInt_FMT "\n Q: %" CeedInt_FMT "\n", basis->P, basis->Q); 181750c301a5SRezgar Shakeri } 1818edf04919SJeremy L Thompson fprintf(stream, " dimension: %" CeedInt_FMT "\n field components: %" CeedInt_FMT "\n", basis->dim, basis->num_comp); 1819ea61e9acSJeremy L Thompson // Print quadrature data, interpolation/gradient/divergence/curl of the basis 18201203703bSJeremy L Thompson if (is_tensor_basis) { // tensor basis 18211203703bSJeremy L Thompson CeedInt P_1d, Q_1d; 18221203703bSJeremy L Thompson const CeedScalar *q_ref_1d, *q_weight_1d, *interp_1d, *grad_1d; 18231203703bSJeremy L Thompson 18241203703bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 18251203703bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 18261203703bSJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 18271203703bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d)); 18281203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 18291203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 18301203703bSJeremy L Thompson 18311203703bSJeremy L Thompson CeedCall(CeedScalarView("qref1d", "\t% 12.8f", 1, Q_1d, q_ref_1d, stream)); 18321203703bSJeremy L Thompson CeedCall(CeedScalarView("qweight1d", "\t% 12.8f", 1, Q_1d, q_weight_1d, stream)); 18331203703bSJeremy L Thompson CeedCall(CeedScalarView("interp1d", "\t% 12.8f", Q_1d, P_1d, interp_1d, stream)); 18341203703bSJeremy L Thompson CeedCall(CeedScalarView("grad1d", "\t% 12.8f", Q_1d, P_1d, grad_1d, stream)); 183550c301a5SRezgar Shakeri } else { // non-tensor basis 18361203703bSJeremy L Thompson CeedInt P, Q, dim, q_comp; 18371203703bSJeremy L Thompson const CeedScalar *q_ref, *q_weight, *interp, *grad, *div, *curl; 18381203703bSJeremy L Thompson 18391203703bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &P)); 18401203703bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &Q)); 18411203703bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 18421203703bSJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref)); 18431203703bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight)); 18441203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis, &interp)); 18451203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad(basis, &grad)); 18461203703bSJeremy L Thompson CeedCall(CeedBasisGetDiv(basis, &div)); 18471203703bSJeremy L Thompson CeedCall(CeedBasisGetCurl(basis, &curl)); 18481203703bSJeremy L Thompson 18491203703bSJeremy L Thompson CeedCall(CeedScalarView("qref", "\t% 12.8f", 1, Q * dim, q_ref, stream)); 18501203703bSJeremy L Thompson CeedCall(CeedScalarView("qweight", "\t% 12.8f", 1, Q, q_weight, stream)); 1851c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp)); 18521203703bSJeremy L Thompson CeedCall(CeedScalarView("interp", "\t% 12.8f", q_comp * Q, P, interp, stream)); 18531203703bSJeremy L Thompson if (grad) { 1854c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp)); 18551203703bSJeremy L Thompson CeedCall(CeedScalarView("grad", "\t% 12.8f", q_comp * Q, P, grad, stream)); 18567a982d89SJeremy L. Thompson } 18571203703bSJeremy L Thompson if (div) { 1858c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp)); 18591203703bSJeremy L Thompson CeedCall(CeedScalarView("div", "\t% 12.8f", q_comp * Q, P, div, stream)); 1860c4e3f59bSSebastian Grimberg } 18611203703bSJeremy L Thompson if (curl) { 1862c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp)); 18631203703bSJeremy L Thompson CeedCall(CeedScalarView("curl", "\t% 12.8f", q_comp * Q, P, curl, stream)); 186450c301a5SRezgar Shakeri } 186550c301a5SRezgar Shakeri } 1866e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18677a982d89SJeremy L. Thompson } 18687a982d89SJeremy L. Thompson 18697a982d89SJeremy L. Thompson /** 1870db2becc9SJeremy L Thompson @brief Check input vector dimensions for CeedBasisApply[Add] 18717a982d89SJeremy L. Thompson 1872ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1873ea61e9acSJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1874ca94c3ddSJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1875ca94c3ddSJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points; 1876ca94c3ddSJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes 1877ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 1878ca94c3ddSJeremy L Thompson @ref CEED_EVAL_INTERP to use interpolated values, 1879ca94c3ddSJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1880ca94c3ddSJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 1881ca94c3ddSJeremy L Thompson @ref CEED_EVAL_CURL to use curl, 1882ca94c3ddSJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1883ca94c3ddSJeremy L Thompson @param[in] u Input `CeedVector` 1884ca94c3ddSJeremy L Thompson @param[out] v Output `CeedVector` 18857a982d89SJeremy L. Thompson 18867a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 18877a982d89SJeremy L. Thompson 1888db2becc9SJeremy L Thompson @ref Developer 18897a982d89SJeremy L. Thompson **/ 1890db2becc9SJeremy L Thompson static int CeedBasisApplyCheckDims(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 1891c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 18921c66c397SJeremy L Thompson CeedSize u_length = 0, v_length; 18931c66c397SJeremy L Thompson 18942b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 18952b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 1896c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 18972b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 18982b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 18992b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(v, &v_length)); 1900c8c3fa7dSJeremy L Thompson if (u) CeedCall(CeedVectorGetLength(u, &u_length)); 19017a982d89SJeremy L. Thompson 1902e15f9bd0SJeremy L Thompson // Check vector lengths to prevent out of bounds issues 190399e754f0SJeremy L Thompson bool has_good_dims = true; 1904d1d35e2fSjeremylt switch (eval_mode) { 1905e15f9bd0SJeremy L Thompson case CEED_EVAL_NONE: 19062b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 19072b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 1908c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 1909c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 191019a04db8SJeremy L Thompson has_good_dims = ((t_mode == CEED_TRANSPOSE && u_length >= (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_qpts * (CeedSize)q_comp && 191119a04db8SJeremy L Thompson v_length >= (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_nodes) || 191219a04db8SJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && v_length >= (CeedSize)num_elem * (CeedSize)num_qpts * (CeedSize)num_comp * (CeedSize)q_comp && 191319a04db8SJeremy L Thompson u_length >= (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_nodes)); 1914e15f9bd0SJeremy L Thompson break; 1915e15f9bd0SJeremy L Thompson case CEED_EVAL_WEIGHT: 191619a04db8SJeremy L Thompson has_good_dims = v_length >= (CeedSize)num_elem * (CeedSize)num_qpts; 1917e15f9bd0SJeremy L Thompson break; 1918e15f9bd0SJeremy L Thompson } 19199bc66399SJeremy L Thompson CeedCheck(has_good_dims, CeedBasisReturnCeed(basis), CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 1920db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 1921db2becc9SJeremy L Thompson } 1922e15f9bd0SJeremy L Thompson 1923db2becc9SJeremy L Thompson /** 1924db2becc9SJeremy L Thompson @brief Apply basis evaluation from nodes to quadrature points or vice versa 1925db2becc9SJeremy L Thompson 1926db2becc9SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1927db2becc9SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1928db2becc9SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1929db2becc9SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points; 1930db2becc9SJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes 1931db2becc9SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 1932db2becc9SJeremy L Thompson @ref CEED_EVAL_INTERP to use interpolated values, 1933db2becc9SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1934db2becc9SJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 1935db2becc9SJeremy L Thompson @ref CEED_EVAL_CURL to use curl, 1936db2becc9SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1937db2becc9SJeremy L Thompson @param[in] u Input `CeedVector` 1938db2becc9SJeremy L Thompson @param[out] v Output `CeedVector` 1939db2becc9SJeremy L Thompson 1940db2becc9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1941db2becc9SJeremy L Thompson 1942db2becc9SJeremy L Thompson @ref User 1943db2becc9SJeremy L Thompson **/ 1944db2becc9SJeremy L Thompson int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 1945db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyCheckDims(basis, num_elem, t_mode, eval_mode, u, v)); 1946db2becc9SJeremy L Thompson CeedCheck(basis->Apply, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedBasisApply"); 19472b730f8bSJeremy L Thompson CeedCall(basis->Apply(basis, num_elem, t_mode, eval_mode, u, v)); 1948e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19497a982d89SJeremy L. Thompson } 19507a982d89SJeremy L. Thompson 19517a982d89SJeremy L. Thompson /** 1952db2becc9SJeremy L Thompson @brief Apply basis evaluation from quadrature points to nodes and sum into target vector 1953db2becc9SJeremy L Thompson 1954db2becc9SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1955db2becc9SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1956db2becc9SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1957db2becc9SJeremy L Thompson @param[in] t_mode @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes; 1958db2becc9SJeremy L Thompson @ref CEED_NOTRANSPOSE is not valid for `CeedBasisApplyAdd()` 1959db2becc9SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 1960db2becc9SJeremy L Thompson @ref CEED_EVAL_INTERP to use interpolated values, 1961db2becc9SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1962db2becc9SJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 1963db2becc9SJeremy L Thompson @ref CEED_EVAL_CURL to use curl, 1964db2becc9SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1965db2becc9SJeremy L Thompson @param[in] u Input `CeedVector` 1966db2becc9SJeremy L Thompson @param[out] v Output `CeedVector` to sum into 1967db2becc9SJeremy L Thompson 1968db2becc9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1969db2becc9SJeremy L Thompson 1970db2becc9SJeremy L Thompson @ref User 1971db2becc9SJeremy L Thompson **/ 1972db2becc9SJeremy L Thompson int CeedBasisApplyAdd(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 1973db2becc9SJeremy L Thompson CeedCheck(t_mode == CEED_TRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "CeedBasisApplyAdd only supports CEED_TRANSPOSE"); 1974db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyCheckDims(basis, num_elem, t_mode, eval_mode, u, v)); 1975db2becc9SJeremy L Thompson CeedCheck(basis->ApplyAdd, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedBasisApplyAdd"); 1976db2becc9SJeremy L Thompson CeedCall(basis->ApplyAdd(basis, num_elem, t_mode, eval_mode, u, v)); 1977db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 1978db2becc9SJeremy L Thompson } 1979db2becc9SJeremy L Thompson 1980db2becc9SJeremy L Thompson /** 1981db2becc9SJeremy L Thompson @brief Apply basis evaluation from nodes to arbitrary points 1982db2becc9SJeremy L Thompson 1983db2becc9SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1984db2becc9SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1985db2becc9SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1986db2becc9SJeremy L Thompson @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 1987db2becc9SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 1988db2becc9SJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes 1989db2becc9SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 1990db2becc9SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1991db2becc9SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1992db2becc9SJeremy L Thompson @param[in] x_ref `CeedVector` holding reference coordinates of each point 1993db2becc9SJeremy L Thompson @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 1994db2becc9SJeremy L Thompson @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 1995db2becc9SJeremy L Thompson 1996db2becc9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1997db2becc9SJeremy L Thompson 1998db2becc9SJeremy L Thompson @ref User 1999db2becc9SJeremy L Thompson **/ 2000db2becc9SJeremy L Thompson int CeedBasisApplyAtPoints(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, 2001db2becc9SJeremy L Thompson CeedVector x_ref, CeedVector u, CeedVector v) { 2002db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyAtPointsCheckDims(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2003db2becc9SJeremy L Thompson if (basis->ApplyAtPoints) { 2004db2becc9SJeremy L Thompson CeedCall(basis->ApplyAtPoints(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2005db2becc9SJeremy L Thompson } else { 2006db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyAtPoints_Core(basis, false, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2007db2becc9SJeremy L Thompson } 2008db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 2009db2becc9SJeremy L Thompson } 2010db2becc9SJeremy L Thompson 2011db2becc9SJeremy L Thompson /** 2012db2becc9SJeremy L Thompson @brief Apply basis evaluation from nodes to arbitrary points and sum into target vector 2013db2becc9SJeremy L Thompson 2014db2becc9SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 2015db2becc9SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 2016db2becc9SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 2017db2becc9SJeremy L Thompson @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 2018db2becc9SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 2019db2becc9SJeremy L Thompson @ref CEED_NOTRANSPOSE is not valid for `CeedBasisApplyAddAtPoints()` 2020db2becc9SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 2021db2becc9SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 2022db2becc9SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 2023db2becc9SJeremy L Thompson @param[in] x_ref `CeedVector` holding reference coordinates of each point 2024db2becc9SJeremy L Thompson @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 2025db2becc9SJeremy L Thompson @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 2026db2becc9SJeremy L Thompson 2027db2becc9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2028db2becc9SJeremy L Thompson 2029db2becc9SJeremy L Thompson @ref User 2030db2becc9SJeremy L Thompson **/ 2031db2becc9SJeremy L Thompson int CeedBasisApplyAddAtPoints(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, 2032db2becc9SJeremy L Thompson CeedVector x_ref, CeedVector u, CeedVector v) { 2033db2becc9SJeremy L Thompson CeedCheck(t_mode == CEED_TRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "CeedBasisApplyAddAtPoints only supports CEED_TRANSPOSE"); 2034db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyAtPointsCheckDims(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2035db2becc9SJeremy L Thompson if (basis->ApplyAddAtPoints) { 2036db2becc9SJeremy L Thompson CeedCall(basis->ApplyAddAtPoints(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2037db2becc9SJeremy L Thompson } else { 2038db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyAtPoints_Core(basis, true, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2039db2becc9SJeremy L Thompson } 2040db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 2041db2becc9SJeremy L Thompson } 2042db2becc9SJeremy L Thompson 2043db2becc9SJeremy L Thompson /** 20446e536b99SJeremy L Thompson @brief Get the `Ceed` associated with a `CeedBasis` 2045b7c9bbdaSJeremy L Thompson 2046ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2047ca94c3ddSJeremy L Thompson @param[out] ceed Variable to store `Ceed` 2048b7c9bbdaSJeremy L Thompson 2049b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2050b7c9bbdaSJeremy L Thompson 2051b7c9bbdaSJeremy L Thompson @ref Advanced 2052b7c9bbdaSJeremy L Thompson **/ 2053b7c9bbdaSJeremy L Thompson int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) { 20549bc66399SJeremy L Thompson *ceed = NULL; 20559bc66399SJeremy L Thompson CeedCall(CeedReferenceCopy(CeedBasisReturnCeed(basis), ceed)); 2056b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 2057b7c9bbdaSJeremy L Thompson } 2058b7c9bbdaSJeremy L Thompson 2059b7c9bbdaSJeremy L Thompson /** 20606e536b99SJeremy L Thompson @brief Return the `Ceed` associated with a `CeedBasis` 20616e536b99SJeremy L Thompson 20626e536b99SJeremy L Thompson @param[in] basis `CeedBasis` 20636e536b99SJeremy L Thompson 20646e536b99SJeremy L Thompson @return `Ceed` associated with the `basis` 20656e536b99SJeremy L Thompson 20666e536b99SJeremy L Thompson @ref Advanced 20676e536b99SJeremy L Thompson **/ 20686e536b99SJeremy L Thompson Ceed CeedBasisReturnCeed(CeedBasis basis) { return basis->ceed; } 20696e536b99SJeremy L Thompson 20706e536b99SJeremy L Thompson /** 2071ca94c3ddSJeremy L Thompson @brief Get dimension for given `CeedBasis` 20729d007619Sjeremylt 2073ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 20749d007619Sjeremylt @param[out] dim Variable to store dimension of basis 20759d007619Sjeremylt 20769d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20779d007619Sjeremylt 2078b7c9bbdaSJeremy L Thompson @ref Advanced 20799d007619Sjeremylt **/ 20809d007619Sjeremylt int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) { 20819d007619Sjeremylt *dim = basis->dim; 2082e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20839d007619Sjeremylt } 20849d007619Sjeremylt 20859d007619Sjeremylt /** 2086ca94c3ddSJeremy L Thompson @brief Get topology for given `CeedBasis` 2087d99fa3c5SJeremy L Thompson 2088ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2089d99fa3c5SJeremy L Thompson @param[out] topo Variable to store topology of basis 2090d99fa3c5SJeremy L Thompson 2091d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2092d99fa3c5SJeremy L Thompson 2093b7c9bbdaSJeremy L Thompson @ref Advanced 2094d99fa3c5SJeremy L Thompson **/ 2095d99fa3c5SJeremy L Thompson int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) { 2096d99fa3c5SJeremy L Thompson *topo = basis->topo; 2097e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2098d99fa3c5SJeremy L Thompson } 2099d99fa3c5SJeremy L Thompson 2100d99fa3c5SJeremy L Thompson /** 2101ca94c3ddSJeremy L Thompson @brief Get number of components for given `CeedBasis` 21029d007619Sjeremylt 2103ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2104ca94c3ddSJeremy L Thompson @param[out] num_comp Variable to store number of components 21059d007619Sjeremylt 21069d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21079d007619Sjeremylt 2108b7c9bbdaSJeremy L Thompson @ref Advanced 21099d007619Sjeremylt **/ 2110d1d35e2fSjeremylt int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) { 2111d1d35e2fSjeremylt *num_comp = basis->num_comp; 2112e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21139d007619Sjeremylt } 21149d007619Sjeremylt 21159d007619Sjeremylt /** 2116ca94c3ddSJeremy L Thompson @brief Get total number of nodes (in `dim` dimensions) of a `CeedBasis` 21179d007619Sjeremylt 2118ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 21199d007619Sjeremylt @param[out] P Variable to store number of nodes 21209d007619Sjeremylt 21219d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21229d007619Sjeremylt 21239d007619Sjeremylt @ref Utility 21249d007619Sjeremylt **/ 21259d007619Sjeremylt int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) { 21269d007619Sjeremylt *P = basis->P; 2127e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21289d007619Sjeremylt } 21299d007619Sjeremylt 21309d007619Sjeremylt /** 2131ca94c3ddSJeremy L Thompson @brief Get total number of nodes (in 1 dimension) of a `CeedBasis` 21329d007619Sjeremylt 2133ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2134d1d35e2fSjeremylt @param[out] P_1d Variable to store number of nodes 21359d007619Sjeremylt 21369d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21379d007619Sjeremylt 2138b7c9bbdaSJeremy L Thompson @ref Advanced 21399d007619Sjeremylt **/ 2140d1d35e2fSjeremylt int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) { 21416e536b99SJeremy L Thompson CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply P_1d for non-tensor CeedBasis"); 2142d1d35e2fSjeremylt *P_1d = basis->P_1d; 2143e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21449d007619Sjeremylt } 21459d007619Sjeremylt 21469d007619Sjeremylt /** 2147ca94c3ddSJeremy L Thompson @brief Get total number of quadrature points (in `dim` dimensions) of a `CeedBasis` 21489d007619Sjeremylt 2149ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 21509d007619Sjeremylt @param[out] Q Variable to store number of quadrature points 21519d007619Sjeremylt 21529d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21539d007619Sjeremylt 21549d007619Sjeremylt @ref Utility 21559d007619Sjeremylt **/ 21569d007619Sjeremylt int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) { 21579d007619Sjeremylt *Q = basis->Q; 2158e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21599d007619Sjeremylt } 21609d007619Sjeremylt 21619d007619Sjeremylt /** 2162ca94c3ddSJeremy L Thompson @brief Get total number of quadrature points (in 1 dimension) of a `CeedBasis` 21639d007619Sjeremylt 2164ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2165d1d35e2fSjeremylt @param[out] Q_1d Variable to store number of quadrature points 21669d007619Sjeremylt 21679d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21689d007619Sjeremylt 2169b7c9bbdaSJeremy L Thompson @ref Advanced 21709d007619Sjeremylt **/ 2171d1d35e2fSjeremylt int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) { 21726e536b99SJeremy L Thompson CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply Q_1d for non-tensor CeedBasis"); 2173d1d35e2fSjeremylt *Q_1d = basis->Q_1d; 2174e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21759d007619Sjeremylt } 21769d007619Sjeremylt 21779d007619Sjeremylt /** 2178ca94c3ddSJeremy L Thompson @brief Get reference coordinates of quadrature points (in `dim` dimensions) of a `CeedBasis` 21799d007619Sjeremylt 2180ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2181d1d35e2fSjeremylt @param[out] q_ref Variable to store reference coordinates of quadrature points 21829d007619Sjeremylt 21839d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21849d007619Sjeremylt 2185b7c9bbdaSJeremy L Thompson @ref Advanced 21869d007619Sjeremylt **/ 2187d1d35e2fSjeremylt int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) { 2188d1d35e2fSjeremylt *q_ref = basis->q_ref_1d; 2189e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21909d007619Sjeremylt } 21919d007619Sjeremylt 21929d007619Sjeremylt /** 2193ca94c3ddSJeremy L Thompson @brief Get quadrature weights of quadrature points (in `dim` dimensions) of a `CeedBasis` 21949d007619Sjeremylt 2195ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2196d1d35e2fSjeremylt @param[out] q_weight Variable to store quadrature weights 21979d007619Sjeremylt 21989d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21999d007619Sjeremylt 2200b7c9bbdaSJeremy L Thompson @ref Advanced 22019d007619Sjeremylt **/ 2202d1d35e2fSjeremylt int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) { 2203d1d35e2fSjeremylt *q_weight = basis->q_weight_1d; 2204e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 22059d007619Sjeremylt } 22069d007619Sjeremylt 22079d007619Sjeremylt /** 2208ca94c3ddSJeremy L Thompson @brief Get interpolation matrix of a `CeedBasis` 22099d007619Sjeremylt 2210ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 22119d007619Sjeremylt @param[out] interp Variable to store interpolation matrix 22129d007619Sjeremylt 22139d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 22149d007619Sjeremylt 2215b7c9bbdaSJeremy L Thompson @ref Advanced 22169d007619Sjeremylt **/ 22176c58de82SJeremy L Thompson int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) { 22186402da51SJeremy L Thompson if (!basis->interp && basis->is_tensor_basis) { 22199d007619Sjeremylt // Allocate 22202b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->Q * basis->P, &basis->interp)); 22219d007619Sjeremylt 22229d007619Sjeremylt // Initialize 22232b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->Q * basis->P; i++) basis->interp[i] = 1.0; 22249d007619Sjeremylt 22259d007619Sjeremylt // Calculate 22262b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 22272b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 22289d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 2229d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 2230d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 22311c66c397SJeremy L Thompson 2232d1d35e2fSjeremylt basis->interp[qpt * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 22339d007619Sjeremylt } 22349d007619Sjeremylt } 22352b730f8bSJeremy L Thompson } 22362b730f8bSJeremy L Thompson } 22379d007619Sjeremylt *interp = basis->interp; 2238e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 22399d007619Sjeremylt } 22409d007619Sjeremylt 22419d007619Sjeremylt /** 2242ca94c3ddSJeremy L Thompson @brief Get 1D interpolation matrix of a tensor product `CeedBasis` 22439d007619Sjeremylt 2244ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2245d1d35e2fSjeremylt @param[out] interp_1d Variable to store interpolation matrix 22469d007619Sjeremylt 22479d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 22489d007619Sjeremylt 22499d007619Sjeremylt @ref Backend 22509d007619Sjeremylt **/ 2251d1d35e2fSjeremylt int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) { 22521203703bSJeremy L Thompson bool is_tensor_basis; 22531203703bSJeremy L Thompson 22541203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 22556e536b99SJeremy L Thompson CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis"); 2256d1d35e2fSjeremylt *interp_1d = basis->interp_1d; 2257e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 22589d007619Sjeremylt } 22599d007619Sjeremylt 22609d007619Sjeremylt /** 2261ca94c3ddSJeremy L Thompson @brief Get gradient matrix of a `CeedBasis` 22629d007619Sjeremylt 2263ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 22649d007619Sjeremylt @param[out] grad Variable to store gradient matrix 22659d007619Sjeremylt 22669d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 22679d007619Sjeremylt 2268b7c9bbdaSJeremy L Thompson @ref Advanced 22699d007619Sjeremylt **/ 22706c58de82SJeremy L Thompson int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) { 22716402da51SJeremy L Thompson if (!basis->grad && basis->is_tensor_basis) { 22729d007619Sjeremylt // Allocate 22732b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->dim * basis->Q * basis->P, &basis->grad)); 22749d007619Sjeremylt 22759d007619Sjeremylt // Initialize 22762b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim * basis->Q * basis->P; i++) basis->grad[i] = 1.0; 22779d007619Sjeremylt 22789d007619Sjeremylt // Calculate 22792b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 22802b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim; i++) { 22812b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 22829d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 2283d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 2284d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 22851c66c397SJeremy L Thompson 22862b730f8bSJeremy L Thompson if (i == d) basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->grad_1d[q * basis->P_1d + p]; 22872b730f8bSJeremy L Thompson else basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 22882b730f8bSJeremy L Thompson } 22892b730f8bSJeremy L Thompson } 22902b730f8bSJeremy L Thompson } 22919d007619Sjeremylt } 22929d007619Sjeremylt } 22939d007619Sjeremylt *grad = basis->grad; 2294e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 22959d007619Sjeremylt } 22969d007619Sjeremylt 22979d007619Sjeremylt /** 2298ca94c3ddSJeremy L Thompson @brief Get 1D gradient matrix of a tensor product `CeedBasis` 22999d007619Sjeremylt 2300ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2301d1d35e2fSjeremylt @param[out] grad_1d Variable to store gradient matrix 23029d007619Sjeremylt 23039d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 23049d007619Sjeremylt 2305b7c9bbdaSJeremy L Thompson @ref Advanced 23069d007619Sjeremylt **/ 2307d1d35e2fSjeremylt int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) { 23081203703bSJeremy L Thompson bool is_tensor_basis; 23091203703bSJeremy L Thompson 23101203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 23116e536b99SJeremy L Thompson CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis"); 2312d1d35e2fSjeremylt *grad_1d = basis->grad_1d; 2313e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 23149d007619Sjeremylt } 23159d007619Sjeremylt 23169d007619Sjeremylt /** 2317ca94c3ddSJeremy L Thompson @brief Get divergence matrix of a `CeedBasis` 231850c301a5SRezgar Shakeri 2319ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 232050c301a5SRezgar Shakeri @param[out] div Variable to store divergence matrix 232150c301a5SRezgar Shakeri 232250c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 232350c301a5SRezgar Shakeri 232450c301a5SRezgar Shakeri @ref Advanced 232550c301a5SRezgar Shakeri **/ 232650c301a5SRezgar Shakeri int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) { 232750c301a5SRezgar Shakeri *div = basis->div; 232850c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 232950c301a5SRezgar Shakeri } 233050c301a5SRezgar Shakeri 233150c301a5SRezgar Shakeri /** 2332ca94c3ddSJeremy L Thompson @brief Get curl matrix of a `CeedBasis` 2333c4e3f59bSSebastian Grimberg 2334ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2335c4e3f59bSSebastian Grimberg @param[out] curl Variable to store curl matrix 2336c4e3f59bSSebastian Grimberg 2337c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 2338c4e3f59bSSebastian Grimberg 2339c4e3f59bSSebastian Grimberg @ref Advanced 2340c4e3f59bSSebastian Grimberg **/ 2341c4e3f59bSSebastian Grimberg int CeedBasisGetCurl(CeedBasis basis, const CeedScalar **curl) { 2342c4e3f59bSSebastian Grimberg *curl = basis->curl; 2343c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 2344c4e3f59bSSebastian Grimberg } 2345c4e3f59bSSebastian Grimberg 2346c4e3f59bSSebastian Grimberg /** 2347ca94c3ddSJeremy L Thompson @brief Destroy a @ref CeedBasis 23487a982d89SJeremy L. Thompson 2349ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` to destroy 23507a982d89SJeremy L. Thompson 23517a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 23527a982d89SJeremy L. Thompson 23537a982d89SJeremy L. Thompson @ref User 23547a982d89SJeremy L. Thompson **/ 23557a982d89SJeremy L. Thompson int CeedBasisDestroy(CeedBasis *basis) { 2356356036faSJeremy L Thompson if (!*basis || *basis == CEED_BASIS_NONE || --(*basis)->ref_count > 0) { 2357ad6481ceSJeremy L Thompson *basis = NULL; 2358ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 2359ad6481ceSJeremy L Thompson } 23602b730f8bSJeremy L Thompson if ((*basis)->Destroy) CeedCall((*basis)->Destroy(*basis)); 23619831d45aSJeremy L Thompson CeedCall(CeedTensorContractDestroy(&(*basis)->contract)); 2362c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_ref_1d)); 2363c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_weight_1d)); 23642b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp)); 23652b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp_1d)); 23662b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad)); 23672b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad_1d)); 2368c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->div)); 2369c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->curl)); 2370c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorDestroy(&(*basis)->vec_chebyshev)); 2371c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisDestroy(&(*basis)->basis_chebyshev)); 23722b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*basis)->ceed)); 23732b730f8bSJeremy L Thompson CeedCall(CeedFree(basis)); 2374e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 23757a982d89SJeremy L. Thompson } 23767a982d89SJeremy L. Thompson 23777a982d89SJeremy L. Thompson /** 2378b11c1e72Sjeremylt @brief Construct a Gauss-Legendre quadrature 2379b11c1e72Sjeremylt 2380ca94c3ddSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree `2*Q-1` exactly) 2381ca94c3ddSJeremy L Thompson @param[out] q_ref_1d Array of length `Q` to hold the abscissa on `[-1, 1]` 2382ca94c3ddSJeremy L Thompson @param[out] q_weight_1d Array of length `Q` to hold the weights 2383b11c1e72Sjeremylt 2384b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 2385dfdf5a53Sjeremylt 2386dfdf5a53Sjeremylt @ref Utility 2387b11c1e72Sjeremylt **/ 23882b730f8bSJeremy L Thompson int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2389d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0 * atan(1.0); 23901c66c397SJeremy L Thompson 2391d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 239292ae7e47SJeremy L Thompson for (CeedInt i = 0; i <= Q / 2; i++) { 2393d7b241e6Sjeremylt // Guess 2394d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(2 * i + 1) / ((CeedScalar)(2 * Q))); 2395d7b241e6Sjeremylt // Pn(xi) 2396d7b241e6Sjeremylt P0 = 1.0; 2397d7b241e6Sjeremylt P1 = xi; 2398d7b241e6Sjeremylt P2 = 0.0; 239992ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 2400d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2401d7b241e6Sjeremylt P0 = P1; 2402d7b241e6Sjeremylt P1 = P2; 2403d7b241e6Sjeremylt } 2404d7b241e6Sjeremylt // First Newton Step 2405d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2406d7b241e6Sjeremylt xi = xi - P2 / dP2; 2407d7b241e6Sjeremylt // Newton to convergence 240892ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(P2) > 10 * CEED_EPSILON; k++) { 2409d7b241e6Sjeremylt P0 = 1.0; 2410d7b241e6Sjeremylt P1 = xi; 241192ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 2412d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2413d7b241e6Sjeremylt P0 = P1; 2414d7b241e6Sjeremylt P1 = P2; 2415d7b241e6Sjeremylt } 2416d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2417d7b241e6Sjeremylt xi = xi - P2 / dP2; 2418d7b241e6Sjeremylt } 2419d7b241e6Sjeremylt // Save xi, wi 2420d7b241e6Sjeremylt wi = 2.0 / ((1.0 - xi * xi) * dP2 * dP2); 2421d1d35e2fSjeremylt q_weight_1d[i] = wi; 2422d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 2423d1d35e2fSjeremylt q_ref_1d[i] = -xi; 2424d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 2425d7b241e6Sjeremylt } 2426e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2427d7b241e6Sjeremylt } 2428d7b241e6Sjeremylt 2429b11c1e72Sjeremylt /** 2430b11c1e72Sjeremylt @brief Construct a Gauss-Legendre-Lobatto quadrature 2431b11c1e72Sjeremylt 2432ca94c3ddSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree `2*Q-3` exactly) 2433ca94c3ddSJeremy L Thompson @param[out] q_ref_1d Array of length `Q` to hold the abscissa on `[-1, 1]` 2434ca94c3ddSJeremy L Thompson @param[out] q_weight_1d Array of length `Q` to hold the weights 2435b11c1e72Sjeremylt 2436b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 2437dfdf5a53Sjeremylt 2438dfdf5a53Sjeremylt @ref Utility 2439b11c1e72Sjeremylt **/ 24402b730f8bSJeremy L Thompson int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2441d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0 * atan(1.0); 24421c66c397SJeremy L Thompson 2443d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 2444d7b241e6Sjeremylt // Set endpoints 24456574a04fSJeremy L Thompson CeedCheck(Q > 1, NULL, CEED_ERROR_DIMENSION, "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q); 2446d7b241e6Sjeremylt wi = 2.0 / ((CeedScalar)(Q * (Q - 1))); 2447d1d35e2fSjeremylt if (q_weight_1d) { 2448d1d35e2fSjeremylt q_weight_1d[0] = wi; 2449d1d35e2fSjeremylt q_weight_1d[Q - 1] = wi; 2450d7b241e6Sjeremylt } 2451d1d35e2fSjeremylt q_ref_1d[0] = -1.0; 2452d1d35e2fSjeremylt q_ref_1d[Q - 1] = 1.0; 2453d7b241e6Sjeremylt // Interior 245492ae7e47SJeremy L Thompson for (CeedInt i = 1; i <= (Q - 1) / 2; i++) { 2455d7b241e6Sjeremylt // Guess 2456d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(i) / (CeedScalar)(Q - 1)); 2457d7b241e6Sjeremylt // Pn(xi) 2458d7b241e6Sjeremylt P0 = 1.0; 2459d7b241e6Sjeremylt P1 = xi; 2460d7b241e6Sjeremylt P2 = 0.0; 246192ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 2462d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2463d7b241e6Sjeremylt P0 = P1; 2464d7b241e6Sjeremylt P1 = P2; 2465d7b241e6Sjeremylt } 2466d7b241e6Sjeremylt // First Newton step 2467d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2468d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2469d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 2470d7b241e6Sjeremylt // Newton to convergence 247192ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(dP2) > 10 * CEED_EPSILON; k++) { 2472d7b241e6Sjeremylt P0 = 1.0; 2473d7b241e6Sjeremylt P1 = xi; 247492ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 2475d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2476d7b241e6Sjeremylt P0 = P1; 2477d7b241e6Sjeremylt P1 = P2; 2478d7b241e6Sjeremylt } 2479d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2480d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2481d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 2482d7b241e6Sjeremylt } 2483d7b241e6Sjeremylt // Save xi, wi 2484d7b241e6Sjeremylt wi = 2.0 / (((CeedScalar)(Q * (Q - 1))) * P2 * P2); 2485d1d35e2fSjeremylt if (q_weight_1d) { 2486d1d35e2fSjeremylt q_weight_1d[i] = wi; 2487d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 2488d7b241e6Sjeremylt } 2489d1d35e2fSjeremylt q_ref_1d[i] = -xi; 2490d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 2491d7b241e6Sjeremylt } 2492e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2493d7b241e6Sjeremylt } 2494d7b241e6Sjeremylt 2495d7b241e6Sjeremylt /// @} 2496