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)); 203*9bc66399SJeremy 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)); 231*9bc66399SJeremy 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)); 267*9bc66399SJeremy 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])); 281*9bc66399SJeremy 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]; 331*9bc66399SJeremy L Thompson CeedCheck((x_length >= (CeedSize)total_num_points * (CeedSize)dim) || (eval_mode == CEED_EVAL_WEIGHT), CeedBasisReturnCeed(basis), 332*9bc66399SJeremy 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 338*9bc66399SJeremy 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: 363*9bc66399SJeremy L Thompson return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points not supported for %s", 364*9bc66399SJeremy L Thompson CeedEvalModes[eval_mode]); 3650b31fde2SJeremy L Thompson // LCOV_EXCL_STOP 3660b31fde2SJeremy L Thompson } 367*9bc66399SJeremy 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 398*9bc66399SJeremy 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)); 408*9bc66399SJeremy L Thompson CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, 409*9bc66399SJeremy L Thompson "Evaluation at arbitrary points only supported for tensor product bases"); 4100b31fde2SJeremy L Thompson } 411*9bc66399SJeremy L Thompson CeedCheck(num_elem == 1, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, 412*9bc66399SJeremy 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; 421*9bc66399SJeremy 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 429*9bc66399SJeremy 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)); 438*9bc66399SJeremy 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 450*9bc66399SJeremy L Thompson CeedCheck(basis_ref && basis_ref->contract, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, 451*9bc66399SJeremy 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 /** 604ca94c3ddSJeremy L Thompson @brief Return collocated gradient matrix 6057a982d89SJeremy L. Thompson 606ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 607ca94c3ddSJeremy L Thompson @param[out] collo_grad_1d Row-major (`Q_1d * Q_1d`) matrix expressing derivatives of basis functions at quadrature points 6087a982d89SJeremy L. Thompson 6097a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 6107a982d89SJeremy L. Thompson 6117a982d89SJeremy L. Thompson @ref Backend 6127a982d89SJeremy L. Thompson **/ 613d1d35e2fSjeremylt int CeedBasisGetCollocatedGrad(CeedBasis basis, CeedScalar *collo_grad_1d) { 6147a982d89SJeremy L. Thompson Ceed ceed; 6152247a93fSRezgar Shakeri CeedInt P_1d, Q_1d; 6162247a93fSRezgar Shakeri CeedScalar *interp_1d_pinv; 6171203703bSJeremy L Thompson const CeedScalar *grad_1d, *interp_1d; 6181203703bSJeremy L Thompson 619ea61e9acSJeremy 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. 6202247a93fSRezgar Shakeri CeedCall(CeedBasisGetCeed(basis, &ceed)); 6212247a93fSRezgar Shakeri CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 6222247a93fSRezgar Shakeri CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 6237a982d89SJeremy L. Thompson 6242247a93fSRezgar Shakeri // Compute interp_1d^+, pseudoinverse of interp_1d 6252247a93fSRezgar Shakeri CeedCall(CeedCalloc(P_1d * Q_1d, &interp_1d_pinv)); 6261203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 6271203703bSJeremy L Thompson CeedCall(CeedMatrixPseudoinverse(ceed, interp_1d, Q_1d, P_1d, interp_1d_pinv)); 6281203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 6291203703bSJeremy L Thompson CeedCall(CeedMatrixMatrixMultiply(ceed, grad_1d, (const CeedScalar *)interp_1d_pinv, collo_grad_1d, Q_1d, Q_1d, P_1d)); 6307a982d89SJeremy L. Thompson 6312247a93fSRezgar Shakeri CeedCall(CeedFree(&interp_1d_pinv)); 632*9bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 633e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6347a982d89SJeremy L. Thompson } 6357a982d89SJeremy L. Thompson 6367a982d89SJeremy L. Thompson /** 637b0cc4569SJeremy L Thompson @brief Return 1D interpolation matrix to Chebyshev polynomial coefficients on quadrature space 638b0cc4569SJeremy L Thompson 639b0cc4569SJeremy L Thompson @param[in] basis `CeedBasis` 640b0cc4569SJeremy L Thompson @param[out] chebyshev_interp_1d Row-major (`P_1d * Q_1d`) matrix interpolating from basis nodes to Chebyshev polynomial coefficients 641b0cc4569SJeremy L Thompson 642b0cc4569SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 643b0cc4569SJeremy L Thompson 644b0cc4569SJeremy L Thompson @ref Backend 645b0cc4569SJeremy L Thompson **/ 646b0cc4569SJeremy L Thompson int CeedBasisGetChebyshevInterp1D(CeedBasis basis, CeedScalar *chebyshev_interp_1d) { 647b0cc4569SJeremy L Thompson CeedInt P_1d, Q_1d; 648b0cc4569SJeremy L Thompson CeedScalar *C, *chebyshev_coeffs_1d_inv; 649b0cc4569SJeremy L Thompson const CeedScalar *interp_1d, *q_ref_1d; 650b0cc4569SJeremy L Thompson Ceed ceed; 651b0cc4569SJeremy L Thompson 652b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 653b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 654b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 655b0cc4569SJeremy L Thompson 656b0cc4569SJeremy L Thompson // Build coefficient matrix 657bd83cbc5SJeremy L Thompson // -- Note: Clang-tidy needs this check 658bd83cbc5SJeremy L Thompson CeedCheck((P_1d > 0) && (Q_1d > 0), ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis dimensions are malformed"); 659b0cc4569SJeremy L Thompson CeedCall(CeedCalloc(Q_1d * Q_1d, &C)); 660b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 661b0cc4569SJeremy L Thompson for (CeedInt i = 0; i < Q_1d; i++) CeedCall(CeedChebyshevPolynomialsAtPoint(q_ref_1d[i], Q_1d, &C[i * Q_1d])); 662b0cc4569SJeremy L Thompson 663b0cc4569SJeremy L Thompson // Compute C^+, pseudoinverse of coefficient matrix 664b0cc4569SJeremy L Thompson CeedCall(CeedCalloc(Q_1d * Q_1d, &chebyshev_coeffs_1d_inv)); 665b0cc4569SJeremy L Thompson CeedCall(CeedMatrixPseudoinverse(ceed, C, Q_1d, Q_1d, chebyshev_coeffs_1d_inv)); 666b0cc4569SJeremy L Thompson 667b0cc4569SJeremy L Thompson // Build mapping from nodes to Chebyshev coefficients 668b0cc4569SJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 669b0cc4569SJeremy L Thompson CeedCall(CeedMatrixMatrixMultiply(ceed, chebyshev_coeffs_1d_inv, interp_1d, chebyshev_interp_1d, Q_1d, P_1d, Q_1d)); 670b0cc4569SJeremy L Thompson 671b0cc4569SJeremy L Thompson // Cleanup 672b0cc4569SJeremy L Thompson CeedCall(CeedFree(&C)); 673b0cc4569SJeremy L Thompson CeedCall(CeedFree(&chebyshev_coeffs_1d_inv)); 674*9bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 675b0cc4569SJeremy L Thompson return CEED_ERROR_SUCCESS; 676b0cc4569SJeremy L Thompson } 677b0cc4569SJeremy L Thompson 678b0cc4569SJeremy L Thompson /** 679ca94c3ddSJeremy L Thompson @brief Get tensor status for given `CeedBasis` 6807a982d89SJeremy L. Thompson 681ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 682d1d35e2fSjeremylt @param[out] is_tensor Variable to store tensor status 6837a982d89SJeremy L. Thompson 6847a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 6857a982d89SJeremy L. Thompson 6867a982d89SJeremy L. Thompson @ref Backend 6877a982d89SJeremy L. Thompson **/ 688d1d35e2fSjeremylt int CeedBasisIsTensor(CeedBasis basis, bool *is_tensor) { 6896402da51SJeremy L Thompson *is_tensor = basis->is_tensor_basis; 690e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6917a982d89SJeremy L. Thompson } 6927a982d89SJeremy L. Thompson 6937a982d89SJeremy L. Thompson /** 694ca94c3ddSJeremy L Thompson @brief Get backend data of a `CeedBasis` 6957a982d89SJeremy L. Thompson 696ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 6977a982d89SJeremy L. Thompson @param[out] data Variable to store data 6987a982d89SJeremy L. Thompson 6997a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 7007a982d89SJeremy L. Thompson 7017a982d89SJeremy L. Thompson @ref Backend 7027a982d89SJeremy L. Thompson **/ 703777ff853SJeremy L Thompson int CeedBasisGetData(CeedBasis basis, void *data) { 704777ff853SJeremy L Thompson *(void **)data = basis->data; 705e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7067a982d89SJeremy L. Thompson } 7077a982d89SJeremy L. Thompson 7087a982d89SJeremy L. Thompson /** 709ca94c3ddSJeremy L Thompson @brief Set backend data of a `CeedBasis` 7107a982d89SJeremy L. Thompson 711ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` 712ea61e9acSJeremy L Thompson @param[in] data Data to set 7137a982d89SJeremy L. Thompson 7147a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 7157a982d89SJeremy L. Thompson 7167a982d89SJeremy L. Thompson @ref Backend 7177a982d89SJeremy L. Thompson **/ 718777ff853SJeremy L Thompson int CeedBasisSetData(CeedBasis basis, void *data) { 719777ff853SJeremy L Thompson basis->data = data; 720e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7217a982d89SJeremy L. Thompson } 7227a982d89SJeremy L. Thompson 7237a982d89SJeremy L. Thompson /** 724ca94c3ddSJeremy L Thompson @brief Increment the reference counter for a `CeedBasis` 72534359f16Sjeremylt 726ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` to increment the reference counter 72734359f16Sjeremylt 72834359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 72934359f16Sjeremylt 73034359f16Sjeremylt @ref Backend 73134359f16Sjeremylt **/ 7329560d06aSjeremylt int CeedBasisReference(CeedBasis basis) { 73334359f16Sjeremylt basis->ref_count++; 73434359f16Sjeremylt return CEED_ERROR_SUCCESS; 73534359f16Sjeremylt } 73634359f16Sjeremylt 73734359f16Sjeremylt /** 738ca94c3ddSJeremy L Thompson @brief Get number of Q-vector components for given `CeedBasis` 739c4e3f59bSSebastian Grimberg 740ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 741ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 742ca94c3ddSJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 743ca94c3ddSJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 744ca94c3ddSJeremy L Thompson @ref CEED_EVAL_CURL to use curl 745c4e3f59bSSebastian Grimberg @param[out] q_comp Variable to store number of Q-vector components of basis 746c4e3f59bSSebastian Grimberg 747c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 748c4e3f59bSSebastian Grimberg 749c4e3f59bSSebastian Grimberg @ref Backend 750c4e3f59bSSebastian Grimberg **/ 751c4e3f59bSSebastian Grimberg int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode, CeedInt *q_comp) { 7521203703bSJeremy L Thompson CeedInt dim; 7531203703bSJeremy L Thompson 7541203703bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 755c4e3f59bSSebastian Grimberg switch (eval_mode) { 7561203703bSJeremy L Thompson case CEED_EVAL_INTERP: { 7571203703bSJeremy L Thompson CeedFESpace fe_space; 7581203703bSJeremy L Thompson 7591203703bSJeremy L Thompson CeedCall(CeedBasisGetFESpace(basis, &fe_space)); 7601203703bSJeremy L Thompson *q_comp = (fe_space == CEED_FE_SPACE_H1) ? 1 : dim; 7611203703bSJeremy L Thompson } break; 762c4e3f59bSSebastian Grimberg case CEED_EVAL_GRAD: 7631203703bSJeremy L Thompson *q_comp = dim; 764c4e3f59bSSebastian Grimberg break; 765c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 766c4e3f59bSSebastian Grimberg *q_comp = 1; 767c4e3f59bSSebastian Grimberg break; 768c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 7691203703bSJeremy L Thompson *q_comp = (dim < 3) ? 1 : dim; 770c4e3f59bSSebastian Grimberg break; 771c4e3f59bSSebastian Grimberg case CEED_EVAL_NONE: 772c4e3f59bSSebastian Grimberg case CEED_EVAL_WEIGHT: 773352a5e7cSSebastian Grimberg *q_comp = 1; 774c4e3f59bSSebastian Grimberg break; 775c4e3f59bSSebastian Grimberg } 776c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 777c4e3f59bSSebastian Grimberg } 778c4e3f59bSSebastian Grimberg 779c4e3f59bSSebastian Grimberg /** 780ca94c3ddSJeremy L Thompson @brief Estimate number of FLOPs required to apply `CeedBasis` in `t_mode` and `eval_mode` 7816e15d496SJeremy L Thompson 782ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to estimate FLOPs for 783ea61e9acSJeremy L Thompson @param[in] t_mode Apply basis or transpose 784ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CeedEvalMode 785ea61e9acSJeremy L Thompson @param[out] flops Address of variable to hold FLOPs estimate 7866e15d496SJeremy L Thompson 7876e15d496SJeremy L Thompson @ref Backend 7886e15d496SJeremy L Thompson **/ 7892b730f8bSJeremy L Thompson int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedSize *flops) { 7906e15d496SJeremy L Thompson bool is_tensor; 7916e15d496SJeremy L Thompson 7922b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor)); 7936e15d496SJeremy L Thompson if (is_tensor) { 7946e15d496SJeremy L Thompson CeedInt dim, num_comp, P_1d, Q_1d; 7951c66c397SJeremy L Thompson 7962b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 7972b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 7982b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 7992b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 8006e15d496SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 8012b730f8bSJeremy L Thompson P_1d = Q_1d; 8022b730f8bSJeremy L Thompson Q_1d = P_1d; 8036e15d496SJeremy L Thompson } 8046e15d496SJeremy L Thompson CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim - 1), post = 1; 8056e15d496SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 8066e15d496SJeremy L Thompson tensor_flops += 2 * pre * P_1d * post * Q_1d; 8076e15d496SJeremy L Thompson pre /= P_1d; 8086e15d496SJeremy L Thompson post *= Q_1d; 8096e15d496SJeremy L Thompson } 8106e15d496SJeremy L Thompson switch (eval_mode) { 8112b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 8122b730f8bSJeremy L Thompson *flops = 0; 8132b730f8bSJeremy L Thompson break; 8142b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 8152b730f8bSJeremy L Thompson *flops = tensor_flops; 8162b730f8bSJeremy L Thompson break; 8172b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 8182b730f8bSJeremy L Thompson *flops = tensor_flops * 2; 8192b730f8bSJeremy L Thompson break; 8206e15d496SJeremy L Thompson case CEED_EVAL_DIV: 8211203703bSJeremy L Thompson case CEED_EVAL_CURL: { 8226574a04fSJeremy L Thompson // LCOV_EXCL_START 8236e536b99SJeremy L Thompson return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported", 8246e536b99SJeremy L Thompson CeedEvalModes[eval_mode]); 8252b730f8bSJeremy L Thompson break; 8266e15d496SJeremy L Thompson // LCOV_EXCL_STOP 8271203703bSJeremy L Thompson } 8282b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 8292b730f8bSJeremy L Thompson *flops = dim * CeedIntPow(Q_1d, dim); 8302b730f8bSJeremy L Thompson break; 8316e15d496SJeremy L Thompson } 8326e15d496SJeremy L Thompson } else { 833c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 8341c66c397SJeremy L Thompson 8352b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 8362b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 837c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 8382b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 8392b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 8406e15d496SJeremy L Thompson switch (eval_mode) { 8412b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 8422b730f8bSJeremy L Thompson *flops = 0; 8432b730f8bSJeremy L Thompson break; 8442b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 8452b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 8462b730f8bSJeremy L Thompson case CEED_EVAL_DIV: 8472b730f8bSJeremy L Thompson case CEED_EVAL_CURL: 848c4e3f59bSSebastian Grimberg *flops = num_nodes * num_qpts * num_comp * q_comp; 8492b730f8bSJeremy L Thompson break; 8502b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 8512b730f8bSJeremy L Thompson *flops = 0; 8522b730f8bSJeremy L Thompson break; 8536e15d496SJeremy L Thompson } 8546e15d496SJeremy L Thompson } 8556e15d496SJeremy L Thompson return CEED_ERROR_SUCCESS; 8566e15d496SJeremy L Thompson } 8576e15d496SJeremy L Thompson 8586e15d496SJeremy L Thompson /** 859ca94c3ddSJeremy L Thompson @brief Get `CeedFESpace` for a `CeedBasis` 860c4e3f59bSSebastian Grimberg 861ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 862ca94c3ddSJeremy L Thompson @param[out] fe_space Variable to store `CeedFESpace` 863c4e3f59bSSebastian Grimberg 864c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 865c4e3f59bSSebastian Grimberg 866c4e3f59bSSebastian Grimberg @ref Backend 867c4e3f59bSSebastian Grimberg **/ 868c4e3f59bSSebastian Grimberg int CeedBasisGetFESpace(CeedBasis basis, CeedFESpace *fe_space) { 869c4e3f59bSSebastian Grimberg *fe_space = basis->fe_space; 870c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 871c4e3f59bSSebastian Grimberg } 872c4e3f59bSSebastian Grimberg 873c4e3f59bSSebastian Grimberg /** 874ca94c3ddSJeremy L Thompson @brief Get dimension for given `CeedElemTopology` 8757a982d89SJeremy L. Thompson 876ca94c3ddSJeremy L Thompson @param[in] topo `CeedElemTopology` 8777a982d89SJeremy L. Thompson @param[out] dim Variable to store dimension of topology 8787a982d89SJeremy L. Thompson 8797a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 8807a982d89SJeremy L. Thompson 8817a982d89SJeremy L. Thompson @ref Backend 8827a982d89SJeremy L. Thompson **/ 8837a982d89SJeremy L. Thompson int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim) { 8847a982d89SJeremy L. Thompson *dim = (CeedInt)topo >> 16; 885e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 8867a982d89SJeremy L. Thompson } 8877a982d89SJeremy L. Thompson 8887a982d89SJeremy L. Thompson /** 889ca94c3ddSJeremy L Thompson @brief Get `CeedTensorContract` of a `CeedBasis` 8907a982d89SJeremy L. Thompson 891ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 892ca94c3ddSJeremy L Thompson @param[out] contract Variable to store `CeedTensorContract` 8937a982d89SJeremy L. Thompson 8947a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 8957a982d89SJeremy L. Thompson 8967a982d89SJeremy L. Thompson @ref Backend 8977a982d89SJeremy L. Thompson **/ 8987a982d89SJeremy L. Thompson int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract) { 8997a982d89SJeremy L. Thompson *contract = basis->contract; 900e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9017a982d89SJeremy L. Thompson } 9027a982d89SJeremy L. Thompson 9037a982d89SJeremy L. Thompson /** 904ca94c3ddSJeremy L Thompson @brief Set `CeedTensorContract` of a `CeedBasis` 9057a982d89SJeremy L. Thompson 906ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` 907ca94c3ddSJeremy L Thompson @param[in] contract `CeedTensorContract` to set 9087a982d89SJeremy L. Thompson 9097a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 9107a982d89SJeremy L. Thompson 9117a982d89SJeremy L. Thompson @ref Backend 9127a982d89SJeremy L. Thompson **/ 91334359f16Sjeremylt int CeedBasisSetTensorContract(CeedBasis basis, CeedTensorContract contract) { 91434359f16Sjeremylt basis->contract = contract; 9152b730f8bSJeremy L Thompson CeedCall(CeedTensorContractReference(contract)); 916e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9177a982d89SJeremy L. Thompson } 9187a982d89SJeremy L. Thompson 9197a982d89SJeremy L. Thompson /** 920ca94c3ddSJeremy L Thompson @brief Return a reference implementation of matrix multiplication \f$C = A B\f$. 921ba59ac12SSebastian Grimberg 922ca94c3ddSJeremy L Thompson Note: This is a reference implementation for CPU `CeedScalar` pointers that is not intended for high performance. 9237a982d89SJeremy L. Thompson 924ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 925ca94c3ddSJeremy L Thompson @param[in] mat_A Row-major matrix `A` 926ca94c3ddSJeremy L Thompson @param[in] mat_B Row-major matrix `B` 927ca94c3ddSJeremy L Thompson @param[out] mat_C Row-major output matrix `C` 928ca94c3ddSJeremy L Thompson @param[in] m Number of rows of `C` 929ca94c3ddSJeremy L Thompson @param[in] n Number of columns of `C` 930ca94c3ddSJeremy L Thompson @param[in] kk Number of columns of `A`/rows of `B` 9317a982d89SJeremy L. Thompson 9327a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 9337a982d89SJeremy L. Thompson 9347a982d89SJeremy L. Thompson @ref Utility 9357a982d89SJeremy L. Thompson **/ 9362b730f8bSJeremy L Thompson int CeedMatrixMatrixMultiply(Ceed ceed, const CeedScalar *mat_A, const CeedScalar *mat_B, CeedScalar *mat_C, CeedInt m, CeedInt n, CeedInt kk) { 9372b730f8bSJeremy L Thompson for (CeedInt i = 0; i < m; i++) { 9387a982d89SJeremy L. Thompson for (CeedInt j = 0; j < n; j++) { 9397a982d89SJeremy L. Thompson CeedScalar sum = 0; 9401c66c397SJeremy L Thompson 9412b730f8bSJeremy L Thompson for (CeedInt k = 0; k < kk; k++) sum += mat_A[k + i * kk] * mat_B[j + k * n]; 942d1d35e2fSjeremylt mat_C[j + i * n] = sum; 9437a982d89SJeremy L. Thompson } 9442b730f8bSJeremy L Thompson } 945e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9467a982d89SJeremy L. Thompson } 9477a982d89SJeremy L. Thompson 948ba59ac12SSebastian Grimberg /** 949ba59ac12SSebastian Grimberg @brief Return QR Factorization of a matrix 950ba59ac12SSebastian Grimberg 951ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 952ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 953ca94c3ddSJeremy L Thompson @param[in,out] tau Vector of length `m` of scaling factors 954ba59ac12SSebastian Grimberg @param[in] m Number of rows 955ba59ac12SSebastian Grimberg @param[in] n Number of columns 956ba59ac12SSebastian Grimberg 957ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 958ba59ac12SSebastian Grimberg 959ba59ac12SSebastian Grimberg @ref Utility 960ba59ac12SSebastian Grimberg **/ 961ba59ac12SSebastian Grimberg int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau, CeedInt m, CeedInt n) { 962ba59ac12SSebastian Grimberg CeedScalar v[m]; 963ba59ac12SSebastian Grimberg 964ba59ac12SSebastian Grimberg // Check matrix shape 9656574a04fSJeremy L Thompson CeedCheck(n <= m, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute QR factorization with n > m"); 966ba59ac12SSebastian Grimberg 967ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 9681c66c397SJeremy L Thompson CeedScalar sigma = 0.0; 9691c66c397SJeremy L Thompson 970ba59ac12SSebastian Grimberg if (i >= m - 1) { // last row of matrix, no reflection needed 971ba59ac12SSebastian Grimberg tau[i] = 0.; 972ba59ac12SSebastian Grimberg break; 973ba59ac12SSebastian Grimberg } 974ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 975ba59ac12SSebastian Grimberg v[i] = mat[i + n * i]; 976ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) { 977ba59ac12SSebastian Grimberg v[j] = mat[i + n * j]; 978ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 979ba59ac12SSebastian Grimberg } 9801c66c397SJeremy L Thompson const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:m] 9811c66c397SJeremy L Thompson const CeedScalar R_ii = -copysign(norm, v[i]); 9821c66c397SJeremy L Thompson 983ba59ac12SSebastian Grimberg v[i] -= R_ii; 984ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 985ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 986ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 987ba59ac12SSebastian Grimberg tau[i] = 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 988ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] /= v[i]; 989ba59ac12SSebastian Grimberg 990ba59ac12SSebastian Grimberg // Apply Householder reflector to lower right panel 991ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[i * n + i + 1], &v[i], tau[i], m - i, n - i - 1, n, 1); 992ba59ac12SSebastian Grimberg // Save v 993ba59ac12SSebastian Grimberg mat[i + n * i] = R_ii; 994ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) mat[i + n * j] = v[j]; 995ba59ac12SSebastian Grimberg } 996ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 997ba59ac12SSebastian Grimberg } 998ba59ac12SSebastian Grimberg 999ba59ac12SSebastian Grimberg /** 1000ba59ac12SSebastian Grimberg @brief Apply Householder Q matrix 1001ba59ac12SSebastian Grimberg 1002ca94c3ddSJeremy 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$. 1003ba59ac12SSebastian Grimberg 1004ba59ac12SSebastian Grimberg @param[in,out] mat_A Matrix to apply Householder Q to, in place 1005ba59ac12SSebastian Grimberg @param[in] mat_Q Householder Q matrix 1006ba59ac12SSebastian Grimberg @param[in] tau Householder scaling factors 1007ba59ac12SSebastian Grimberg @param[in] t_mode Transpose mode for application 1008ca94c3ddSJeremy L Thompson @param[in] m Number of rows in `A` 1009ca94c3ddSJeremy L Thompson @param[in] n Number of columns in `A` 1010ca94c3ddSJeremy L Thompson @param[in] k Number of elementary reflectors in Q, `k < m` 1011ca94c3ddSJeremy L Thompson @param[in] row Row stride in `A` 1012ca94c3ddSJeremy L Thompson @param[in] col Col stride in `A` 1013ba59ac12SSebastian Grimberg 1014ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1015ba59ac12SSebastian Grimberg 1016c4e3f59bSSebastian Grimberg @ref Utility 1017ba59ac12SSebastian Grimberg **/ 1018ba59ac12SSebastian Grimberg int CeedHouseholderApplyQ(CeedScalar *mat_A, const CeedScalar *mat_Q, const CeedScalar *tau, CeedTransposeMode t_mode, CeedInt m, CeedInt n, 1019ba59ac12SSebastian Grimberg CeedInt k, CeedInt row, CeedInt col) { 1020ba59ac12SSebastian Grimberg CeedScalar *v; 10211c66c397SJeremy L Thompson 1022ba59ac12SSebastian Grimberg CeedCall(CeedMalloc(m, &v)); 1023ba59ac12SSebastian Grimberg for (CeedInt ii = 0; ii < k; ii++) { 1024ba59ac12SSebastian Grimberg CeedInt i = t_mode == CEED_TRANSPOSE ? ii : k - 1 - ii; 1025ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] = mat_Q[j * k + i]; 1026ba59ac12SSebastian Grimberg // Apply Householder reflector (I - tau v v^T) collo_grad_1d^T 1027ba59ac12SSebastian Grimberg CeedCall(CeedHouseholderReflect(&mat_A[i * row], &v[i], tau[i], m - i, n, row, col)); 1028ba59ac12SSebastian Grimberg } 1029ba59ac12SSebastian Grimberg CeedCall(CeedFree(&v)); 1030ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 1031ba59ac12SSebastian Grimberg } 1032ba59ac12SSebastian Grimberg 1033ba59ac12SSebastian Grimberg /** 10342247a93fSRezgar Shakeri @brief Return pseudoinverse of a matrix 10352247a93fSRezgar Shakeri 10362247a93fSRezgar Shakeri @param[in] ceed Ceed context for error handling 10372247a93fSRezgar Shakeri @param[in] mat Row-major matrix to compute pseudoinverse of 10382247a93fSRezgar Shakeri @param[in] m Number of rows 10392247a93fSRezgar Shakeri @param[in] n Number of columns 10402247a93fSRezgar Shakeri @param[out] mat_pinv Row-major pseudoinverse matrix 10412247a93fSRezgar Shakeri 10422247a93fSRezgar Shakeri @return An error code: 0 - success, otherwise - failure 10432247a93fSRezgar Shakeri 10442247a93fSRezgar Shakeri @ref Utility 10452247a93fSRezgar Shakeri **/ 10461203703bSJeremy L Thompson int CeedMatrixPseudoinverse(Ceed ceed, const CeedScalar *mat, CeedInt m, CeedInt n, CeedScalar *mat_pinv) { 10472247a93fSRezgar Shakeri CeedScalar *tau, *I, *mat_copy; 10482247a93fSRezgar Shakeri 10492247a93fSRezgar Shakeri CeedCall(CeedCalloc(m, &tau)); 10502247a93fSRezgar Shakeri CeedCall(CeedCalloc(m * m, &I)); 10512247a93fSRezgar Shakeri CeedCall(CeedCalloc(m * n, &mat_copy)); 10522247a93fSRezgar Shakeri memcpy(mat_copy, mat, m * n * sizeof mat[0]); 10532247a93fSRezgar Shakeri 10542247a93fSRezgar Shakeri // QR Factorization, mat = Q R 10552247a93fSRezgar Shakeri CeedCall(CeedQRFactorization(ceed, mat_copy, tau, m, n)); 10562247a93fSRezgar Shakeri 10572247a93fSRezgar Shakeri // -- Apply Q^T, I = Q^T * I 10582247a93fSRezgar Shakeri for (CeedInt i = 0; i < m; i++) I[i * m + i] = 1.0; 10592247a93fSRezgar Shakeri CeedCall(CeedHouseholderApplyQ(I, mat_copy, tau, CEED_TRANSPOSE, m, m, n, m, 1)); 10602247a93fSRezgar Shakeri // -- Apply R_inv, mat_pinv = R_inv * Q^T 10612247a93fSRezgar Shakeri for (CeedInt j = 0; j < m; j++) { // Column j 10622247a93fSRezgar Shakeri mat_pinv[j + m * (n - 1)] = I[j + m * (n - 1)] / mat_copy[n * n - 1]; 10632247a93fSRezgar Shakeri for (CeedInt i = n - 2; i >= 0; i--) { // Row i 10642247a93fSRezgar Shakeri mat_pinv[j + m * i] = I[j + m * i]; 10652247a93fSRezgar Shakeri for (CeedInt k = i + 1; k < n; k++) mat_pinv[j + m * i] -= mat_copy[k + n * i] * mat_pinv[j + m * k]; 10662247a93fSRezgar Shakeri mat_pinv[j + m * i] /= mat_copy[i + n * i]; 10672247a93fSRezgar Shakeri } 10682247a93fSRezgar Shakeri } 10692247a93fSRezgar Shakeri 10702247a93fSRezgar Shakeri // Cleanup 10712247a93fSRezgar Shakeri CeedCall(CeedFree(&I)); 10722247a93fSRezgar Shakeri CeedCall(CeedFree(&tau)); 10732247a93fSRezgar Shakeri CeedCall(CeedFree(&mat_copy)); 10742247a93fSRezgar Shakeri return CEED_ERROR_SUCCESS; 10752247a93fSRezgar Shakeri } 10762247a93fSRezgar Shakeri 10772247a93fSRezgar Shakeri /** 1078ba59ac12SSebastian Grimberg @brief Return symmetric Schur decomposition of the symmetric matrix mat via symmetric QR factorization 1079ba59ac12SSebastian Grimberg 1080ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 1081ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 1082ba59ac12SSebastian Grimberg @param[out] lambda Vector of length n of eigenvalues 1083ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 1084ba59ac12SSebastian Grimberg 1085ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1086ba59ac12SSebastian Grimberg 1087ba59ac12SSebastian Grimberg @ref Utility 1088ba59ac12SSebastian Grimberg **/ 10892c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 10902c2ea1dbSJeremy L Thompson int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat, CeedScalar *lambda, CeedInt n) { 1091ba59ac12SSebastian Grimberg // Check bounds for clang-tidy 10926574a04fSJeremy L Thompson CeedCheck(n > 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute symmetric Schur decomposition of scalars"); 1093ba59ac12SSebastian Grimberg 1094ba59ac12SSebastian Grimberg CeedScalar v[n - 1], tau[n - 1], mat_T[n * n]; 1095ba59ac12SSebastian Grimberg 1096ba59ac12SSebastian Grimberg // Copy mat to mat_T and set mat to I 1097ba59ac12SSebastian Grimberg memcpy(mat_T, mat, n * n * sizeof(mat[0])); 1098ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 1099ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) mat[j + n * i] = (i == j) ? 1 : 0; 1100ba59ac12SSebastian Grimberg } 1101ba59ac12SSebastian Grimberg 1102ba59ac12SSebastian Grimberg // Reduce to tridiagonal 1103ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - 1; i++) { 1104ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 1105ba59ac12SSebastian Grimberg CeedScalar sigma = 0.0; 11061c66c397SJeremy L Thompson 1107ba59ac12SSebastian Grimberg v[i] = mat_T[i + n * (i + 1)]; 1108ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 1109ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 1110ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 1111ba59ac12SSebastian Grimberg } 11121c66c397SJeremy L Thompson const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:n-1] 11131c66c397SJeremy L Thompson const CeedScalar R_ii = -copysign(norm, v[i]); 11141c66c397SJeremy L Thompson 1115ba59ac12SSebastian Grimberg v[i] -= R_ii; 1116ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 1117ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 1118ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 1119ba59ac12SSebastian Grimberg tau[i] = i == n - 2 ? 2 : 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 1120ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) v[j] /= v[i]; 1121ba59ac12SSebastian Grimberg 1122ba59ac12SSebastian Grimberg // Update sub and super diagonal 1123ba59ac12SSebastian Grimberg for (CeedInt j = i + 2; j < n; j++) { 1124ba59ac12SSebastian Grimberg mat_T[i + n * j] = 0; 1125ba59ac12SSebastian Grimberg mat_T[j + n * i] = 0; 1126ba59ac12SSebastian Grimberg } 1127ba59ac12SSebastian Grimberg // Apply symmetric Householder reflector to lower right panel 1128ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 1129ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), 1, n); 1130ba59ac12SSebastian Grimberg 1131ba59ac12SSebastian Grimberg // Save v 1132ba59ac12SSebastian Grimberg mat_T[i + n * (i + 1)] = R_ii; 1133ba59ac12SSebastian Grimberg mat_T[(i + 1) + n * i] = R_ii; 1134ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 1135ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = v[j]; 1136ba59ac12SSebastian Grimberg } 1137ba59ac12SSebastian Grimberg } 1138ba59ac12SSebastian Grimberg // Backwards accumulation of Q 1139ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 1140ba59ac12SSebastian Grimberg if (tau[i] > 0.0) { 1141ba59ac12SSebastian Grimberg v[i] = 1; 1142ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 1143ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 1144ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = 0; 1145ba59ac12SSebastian Grimberg } 1146ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 1147ba59ac12SSebastian Grimberg } 1148ba59ac12SSebastian Grimberg } 1149ba59ac12SSebastian Grimberg 1150ba59ac12SSebastian Grimberg // Reduce sub and super diagonal 1151ba59ac12SSebastian Grimberg CeedInt p = 0, q = 0, itr = 0, max_itr = n * n * n * n; 1152ba59ac12SSebastian Grimberg CeedScalar tol = CEED_EPSILON; 1153ba59ac12SSebastian Grimberg 1154ba59ac12SSebastian Grimberg while (itr < max_itr) { 1155ba59ac12SSebastian Grimberg // Update p, q, size of reduced portions of diagonal 1156ba59ac12SSebastian Grimberg p = 0; 1157ba59ac12SSebastian Grimberg q = 0; 1158ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 1159ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) q += 1; 1160ba59ac12SSebastian Grimberg else break; 1161ba59ac12SSebastian Grimberg } 1162ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - q - 1; i++) { 1163ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) p += 1; 1164ba59ac12SSebastian Grimberg else break; 1165ba59ac12SSebastian Grimberg } 1166ba59ac12SSebastian Grimberg if (q == n - 1) break; // Finished reducing 1167ba59ac12SSebastian Grimberg 1168ba59ac12SSebastian Grimberg // Reduce tridiagonal portion 1169ba59ac12SSebastian Grimberg CeedScalar t_nn = mat_T[(n - 1 - q) + n * (n - 1 - q)], t_nnm1 = mat_T[(n - 2 - q) + n * (n - 1 - q)]; 1170ba59ac12SSebastian Grimberg CeedScalar d = (mat_T[(n - 2 - q) + n * (n - 2 - q)] - t_nn) / 2; 1171ba59ac12SSebastian Grimberg CeedScalar mu = t_nn - t_nnm1 * t_nnm1 / (d + copysign(sqrt(d * d + t_nnm1 * t_nnm1), d)); 1172ba59ac12SSebastian Grimberg CeedScalar x = mat_T[p + n * p] - mu; 1173ba59ac12SSebastian Grimberg CeedScalar z = mat_T[p + n * (p + 1)]; 11741c66c397SJeremy L Thompson 1175ba59ac12SSebastian Grimberg for (CeedInt k = p; k < n - q - 1; k++) { 1176ba59ac12SSebastian Grimberg // Compute Givens rotation 1177ba59ac12SSebastian Grimberg CeedScalar c = 1, s = 0; 11781c66c397SJeremy L Thompson 1179ba59ac12SSebastian Grimberg if (fabs(z) > tol) { 1180ba59ac12SSebastian Grimberg if (fabs(z) > fabs(x)) { 11811c66c397SJeremy L Thompson const CeedScalar tau = -x / z; 11821c66c397SJeremy L Thompson 11831c66c397SJeremy L Thompson s = 1 / sqrt(1 + tau * tau); 11841c66c397SJeremy L Thompson c = s * tau; 1185ba59ac12SSebastian Grimberg } else { 11861c66c397SJeremy L Thompson const CeedScalar tau = -z / x; 11871c66c397SJeremy L Thompson 11881c66c397SJeremy L Thompson c = 1 / sqrt(1 + tau * tau); 11891c66c397SJeremy L Thompson s = c * tau; 1190ba59ac12SSebastian Grimberg } 1191ba59ac12SSebastian Grimberg } 1192ba59ac12SSebastian Grimberg 1193ba59ac12SSebastian Grimberg // Apply Givens rotation to T 1194ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 1195ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_TRANSPOSE, k, k + 1, n, n); 1196ba59ac12SSebastian Grimberg 1197ba59ac12SSebastian Grimberg // Apply Givens rotation to Q 1198ba59ac12SSebastian Grimberg CeedGivensRotation(mat, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 1199ba59ac12SSebastian Grimberg 1200ba59ac12SSebastian Grimberg // Update x, z 1201ba59ac12SSebastian Grimberg if (k < n - q - 2) { 1202ba59ac12SSebastian Grimberg x = mat_T[k + n * (k + 1)]; 1203ba59ac12SSebastian Grimberg z = mat_T[k + n * (k + 2)]; 1204ba59ac12SSebastian Grimberg } 1205ba59ac12SSebastian Grimberg } 1206ba59ac12SSebastian Grimberg itr++; 1207ba59ac12SSebastian Grimberg } 1208ba59ac12SSebastian Grimberg 1209ba59ac12SSebastian Grimberg // Save eigenvalues 1210ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) lambda[i] = mat_T[i + n * i]; 1211ba59ac12SSebastian Grimberg 1212ba59ac12SSebastian Grimberg // Check convergence 12136574a04fSJeremy L Thompson CeedCheck(itr < max_itr || q > n, ceed, CEED_ERROR_MINOR, "Symmetric QR failed to converge"); 1214ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 1215ba59ac12SSebastian Grimberg } 12162c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 1217ba59ac12SSebastian Grimberg 1218ba59ac12SSebastian Grimberg /** 1219ba59ac12SSebastian Grimberg @brief Return Simultaneous Diagonalization of two matrices. 1220ba59ac12SSebastian Grimberg 1221ca94c3ddSJeremy L Thompson This solves the generalized eigenvalue problem `A x = lambda B x`, where `A` and `B` are symmetric and `B` is positive definite. 1222ca94c3ddSJeremy L Thompson We generate the matrix `X` and vector `Lambda` such that `X^T A X = Lambda` and `X^T B X = I`. 1223ca94c3ddSJeremy L Thompson This is equivalent to the LAPACK routine 'sygv' with `TYPE = 1`. 1224ba59ac12SSebastian Grimberg 1225ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` context for error handling 1226ba59ac12SSebastian Grimberg @param[in] mat_A Row-major matrix to be factorized with eigenvalues 1227ba59ac12SSebastian Grimberg @param[in] mat_B Row-major matrix to be factorized to identity 1228ba59ac12SSebastian Grimberg @param[out] mat_X Row-major orthogonal matrix 1229ca94c3ddSJeremy L Thompson @param[out] lambda Vector of length `n` of generalized eigenvalues 1230ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 1231ba59ac12SSebastian Grimberg 1232ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1233ba59ac12SSebastian Grimberg 1234ba59ac12SSebastian Grimberg @ref Utility 1235ba59ac12SSebastian Grimberg **/ 12362c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 12372c2ea1dbSJeremy L Thompson int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A, CeedScalar *mat_B, CeedScalar *mat_X, CeedScalar *lambda, CeedInt n) { 1238ba59ac12SSebastian Grimberg CeedScalar *mat_C, *mat_G, *vec_D; 12391c66c397SJeremy L Thompson 1240ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_C)); 1241ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_G)); 1242ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n, &vec_D)); 1243ba59ac12SSebastian Grimberg 1244ba59ac12SSebastian Grimberg // Compute B = G D G^T 1245ba59ac12SSebastian Grimberg memcpy(mat_G, mat_B, n * n * sizeof(mat_B[0])); 1246ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_G, vec_D, n)); 1247ba59ac12SSebastian Grimberg 1248ba59ac12SSebastian Grimberg // Sort eigenvalues 1249ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 1250ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 1251ba59ac12SSebastian Grimberg if (fabs(vec_D[j]) > fabs(vec_D[j + 1])) { 12521c66c397SJeremy L Thompson CeedScalarSwap(vec_D[j], vec_D[j + 1]); 12531c66c397SJeremy L Thompson for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_G[k * n + j], mat_G[k * n + j + 1]); 1254ba59ac12SSebastian Grimberg } 1255ba59ac12SSebastian Grimberg } 1256ba59ac12SSebastian Grimberg } 1257ba59ac12SSebastian Grimberg 1258ba59ac12SSebastian Grimberg // Compute C = (G D^1/2)^-1 A (G D^1/2)^-T 1259ba59ac12SSebastian Grimberg // = D^-1/2 G^T A G D^-1/2 1260ba59ac12SSebastian Grimberg // -- D = D^-1/2 1261ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) vec_D[i] = 1. / sqrt(vec_D[i]); 1262ba59ac12SSebastian Grimberg // -- G = G D^-1/2 1263ba59ac12SSebastian Grimberg // -- C = D^-1/2 G^T 1264ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 1265ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) { 1266ba59ac12SSebastian Grimberg mat_G[i * n + j] *= vec_D[j]; 1267ba59ac12SSebastian Grimberg mat_C[j * n + i] = mat_G[i * n + j]; 1268ba59ac12SSebastian Grimberg } 1269ba59ac12SSebastian Grimberg } 1270ba59ac12SSebastian Grimberg // -- X = (D^-1/2 G^T) A 1271ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_C, (const CeedScalar *)mat_A, mat_X, n, n, n)); 1272ba59ac12SSebastian Grimberg // -- C = (D^-1/2 G^T A) (G D^-1/2) 1273ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_X, (const CeedScalar *)mat_G, mat_C, n, n, n)); 1274ba59ac12SSebastian Grimberg 1275ba59ac12SSebastian Grimberg // Compute Q^T C Q = lambda 1276ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_C, lambda, n)); 1277ba59ac12SSebastian Grimberg 1278ba59ac12SSebastian Grimberg // Sort eigenvalues 1279ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 1280ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 1281ba59ac12SSebastian Grimberg if (fabs(lambda[j]) > fabs(lambda[j + 1])) { 12821c66c397SJeremy L Thompson CeedScalarSwap(lambda[j], lambda[j + 1]); 12831c66c397SJeremy L Thompson for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_C[k * n + j], mat_C[k * n + j + 1]); 1284ba59ac12SSebastian Grimberg } 1285ba59ac12SSebastian Grimberg } 1286ba59ac12SSebastian Grimberg } 1287ba59ac12SSebastian Grimberg 1288ba59ac12SSebastian Grimberg // Set X = (G D^1/2)^-T Q 1289ba59ac12SSebastian Grimberg // = G D^-1/2 Q 1290ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_G, (const CeedScalar *)mat_C, mat_X, n, n, n)); 1291ba59ac12SSebastian Grimberg 1292ba59ac12SSebastian Grimberg // Cleanup 1293ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_C)); 1294ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_G)); 1295ba59ac12SSebastian Grimberg CeedCall(CeedFree(&vec_D)); 1296ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 1297ba59ac12SSebastian Grimberg } 12982c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 1299ba59ac12SSebastian Grimberg 13007a982d89SJeremy L. Thompson /// @} 13017a982d89SJeremy L. Thompson 13027a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 13037a982d89SJeremy L. Thompson /// CeedBasis Public API 13047a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 13057a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser 1306d7b241e6Sjeremylt /// @{ 1307d7b241e6Sjeremylt 1308b11c1e72Sjeremylt /** 1309ca94c3ddSJeremy L Thompson @brief Create a tensor-product basis for \f$H^1\f$ discretizations 1310b11c1e72Sjeremylt 1311ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1312ea61e9acSJeremy L Thompson @param[in] dim Topological dimension 1313ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1314ea61e9acSJeremy L Thompson @param[in] P_1d Number of nodes in one dimension 1315ea61e9acSJeremy L Thompson @param[in] Q_1d Number of quadrature points in one dimension 1316ca94c3ddSJeremy L Thompson @param[in] interp_1d Row-major (`Q_1d * P_1d`) matrix expressing the values of nodal basis functions at quadrature points 1317ca94c3ddSJeremy L Thompson @param[in] grad_1d Row-major (`Q_1d * P_1d`) matrix expressing derivatives of nodal basis functions at quadrature points 1318ca94c3ddSJeremy 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]` 1319ca94c3ddSJeremy L Thompson @param[in] q_weight_1d Array of length `Q_1d` holding the quadrature weights on the reference element 1320ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1321b11c1e72Sjeremylt 1322b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1323dfdf5a53Sjeremylt 13247a982d89SJeremy L. Thompson @ref User 1325b11c1e72Sjeremylt **/ 13262b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, 13272b730f8bSJeremy L Thompson const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis *basis) { 13285fe0d4faSjeremylt if (!ceed->BasisCreateTensorH1) { 13295fe0d4faSjeremylt Ceed delegate; 13306574a04fSJeremy L Thompson 13312b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 13321ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateTensorH1"); 13332b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 1334*9bc66399SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 1335e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 13365fe0d4faSjeremylt } 1337e15f9bd0SJeremy L Thompson 1338ca94c3ddSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value"); 1339ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1340ca94c3ddSJeremy L Thompson CeedCheck(P_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1341ca94c3ddSJeremy L Thompson CeedCheck(Q_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1342227444bfSJeremy L Thompson 13432b730f8bSJeremy L Thompson CeedElemTopology topo = dim == 1 ? CEED_TOPOLOGY_LINE : dim == 2 ? CEED_TOPOLOGY_QUAD : CEED_TOPOLOGY_HEX; 1344e15f9bd0SJeremy L Thompson 13452b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1346db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1347d1d35e2fSjeremylt (*basis)->ref_count = 1; 13486402da51SJeremy L Thompson (*basis)->is_tensor_basis = true; 1349d7b241e6Sjeremylt (*basis)->dim = dim; 1350d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 1351d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 1352d1d35e2fSjeremylt (*basis)->P_1d = P_1d; 1353d1d35e2fSjeremylt (*basis)->Q_1d = Q_1d; 1354d1d35e2fSjeremylt (*basis)->P = CeedIntPow(P_1d, dim); 1355d1d35e2fSjeremylt (*basis)->Q = CeedIntPow(Q_1d, dim); 1356c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 13572b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_ref_1d)); 13582b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_weight_1d)); 1359ff3a0f91SJeremy L Thompson if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d * sizeof(q_ref_1d[0])); 13602b730f8bSJeremy L Thompson if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, Q_1d * sizeof(q_weight_1d[0])); 13612b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->interp_1d)); 13622b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->grad_1d)); 13632b730f8bSJeremy L Thompson if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, Q_1d * P_1d * sizeof(interp_1d[0])); 1364ff3a0f91SJeremy L Thompson if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d * P_1d * sizeof(grad_1d[0])); 13652b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, *basis)); 1366e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1367d7b241e6Sjeremylt } 1368d7b241e6Sjeremylt 1369b11c1e72Sjeremylt /** 1370ca94c3ddSJeremy L Thompson @brief Create a tensor-product \f$H^1\f$ Lagrange basis 1371b11c1e72Sjeremylt 1372ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1373ea61e9acSJeremy L Thompson @param[in] dim Topological dimension of element 1374ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1375ea61e9acSJeremy L Thompson @param[in] P Number of Gauss-Lobatto nodes in one dimension. 1376ca94c3ddSJeremy L Thompson The polynomial degree of the resulting `Q_k` element is `k = P - 1`. 1377ea61e9acSJeremy L Thompson @param[in] Q Number of quadrature points in one dimension. 1378ca94c3ddSJeremy L Thompson @param[in] quad_mode Distribution of the `Q` quadrature points (affects order of accuracy for the quadrature) 1379ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1380b11c1e72Sjeremylt 1381b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1382dfdf5a53Sjeremylt 13837a982d89SJeremy L. Thompson @ref User 1384b11c1e72Sjeremylt **/ 13852b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis) { 1386d7b241e6Sjeremylt // Allocate 1387c8c3fa7dSJeremy L Thompson int ierr = CEED_ERROR_SUCCESS; 13882b730f8bSJeremy L Thompson CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, *q_weight_1d; 13894d537eeaSYohann 1390ca94c3ddSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value"); 1391ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1392ca94c3ddSJeremy L Thompson CeedCheck(P > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1393ca94c3ddSJeremy L Thompson CeedCheck(Q > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1394227444bfSJeremy L Thompson 1395e15f9bd0SJeremy L Thompson // Get Nodes and Weights 13962b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &interp_1d)); 13972b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &grad_1d)); 13982b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P, &nodes)); 13992b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_ref_1d)); 14002b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_weight_1d)); 14012b730f8bSJeremy L Thompson if (CeedLobattoQuadrature(P, nodes, NULL) != CEED_ERROR_SUCCESS) goto cleanup; 1402d1d35e2fSjeremylt switch (quad_mode) { 1403d7b241e6Sjeremylt case CEED_GAUSS: 1404d1d35e2fSjeremylt ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d); 1405d7b241e6Sjeremylt break; 1406d7b241e6Sjeremylt case CEED_GAUSS_LOBATTO: 1407d1d35e2fSjeremylt ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d); 1408d7b241e6Sjeremylt break; 1409d7b241e6Sjeremylt } 14102b730f8bSJeremy L Thompson if (ierr != CEED_ERROR_SUCCESS) goto cleanup; 1411e15f9bd0SJeremy L Thompson 1412d7b241e6Sjeremylt // Build B, D matrix 1413d7b241e6Sjeremylt // Fornberg, 1998 1414c8c3fa7dSJeremy L Thompson for (CeedInt i = 0; i < Q; i++) { 1415d7b241e6Sjeremylt c1 = 1.0; 1416d1d35e2fSjeremylt c3 = nodes[0] - q_ref_1d[i]; 1417d1d35e2fSjeremylt interp_1d[i * P + 0] = 1.0; 1418c8c3fa7dSJeremy L Thompson for (CeedInt j = 1; j < P; j++) { 1419d7b241e6Sjeremylt c2 = 1.0; 1420d7b241e6Sjeremylt c4 = c3; 1421d1d35e2fSjeremylt c3 = nodes[j] - q_ref_1d[i]; 1422c8c3fa7dSJeremy L Thompson for (CeedInt k = 0; k < j; k++) { 1423d7b241e6Sjeremylt dx = nodes[j] - nodes[k]; 1424d7b241e6Sjeremylt c2 *= dx; 1425d7b241e6Sjeremylt if (k == j - 1) { 1426d1d35e2fSjeremylt grad_1d[i * P + j] = c1 * (interp_1d[i * P + k] - c4 * grad_1d[i * P + k]) / c2; 1427d1d35e2fSjeremylt interp_1d[i * P + j] = -c1 * c4 * interp_1d[i * P + k] / c2; 1428d7b241e6Sjeremylt } 1429d1d35e2fSjeremylt grad_1d[i * P + k] = (c3 * grad_1d[i * P + k] - interp_1d[i * P + k]) / dx; 1430d1d35e2fSjeremylt interp_1d[i * P + k] = c3 * interp_1d[i * P + k] / dx; 1431d7b241e6Sjeremylt } 1432d7b241e6Sjeremylt c1 = c2; 1433d7b241e6Sjeremylt } 1434d7b241e6Sjeremylt } 14359ac7b42eSJeremy L Thompson // Pass to CeedBasisCreateTensorH1 14362b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 1437e15f9bd0SJeremy L Thompson cleanup: 14382b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_1d)); 14392b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_1d)); 14402b730f8bSJeremy L Thompson CeedCall(CeedFree(&nodes)); 14412b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref_1d)); 14422b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight_1d)); 1443e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1444d7b241e6Sjeremylt } 1445d7b241e6Sjeremylt 1446b11c1e72Sjeremylt /** 1447ca94c3ddSJeremy L Thompson @brief Create a non tensor-product basis for \f$H^1\f$ discretizations 1448a8de75f0Sjeremylt 1449ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1450e00f3be8SJames Wright @param[in] topo Topology of element, e.g. hypercube, simplex, etc 1451ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1452ea61e9acSJeremy L Thompson @param[in] num_nodes Total number of nodes 1453ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1454ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`num_qpts * num_nodes`) matrix expressing the values of nodal basis functions at quadrature points 1455ca94c3ddSJeremy L Thompson @param[in] grad Row-major (`dim * num_qpts * num_nodes`) matrix expressing derivatives of nodal basis functions at quadrature points 1456ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element 1457ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1458ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1459a8de75f0Sjeremylt 1460a8de75f0Sjeremylt @return An error code: 0 - success, otherwise - failure 1461a8de75f0Sjeremylt 14627a982d89SJeremy L. Thompson @ref User 1463a8de75f0Sjeremylt **/ 14642b730f8bSJeremy L Thompson int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 14652b730f8bSJeremy L Thompson const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1466d1d35e2fSjeremylt CeedInt P = num_nodes, Q = num_qpts, dim = 0; 1467a8de75f0Sjeremylt 14685fe0d4faSjeremylt if (!ceed->BasisCreateH1) { 14695fe0d4faSjeremylt Ceed delegate; 14706574a04fSJeremy L Thompson 14712b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 14721ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateH1"); 14732b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis)); 1474*9bc66399SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 1475e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14765fe0d4faSjeremylt } 14775fe0d4faSjeremylt 1478ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1479ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1480ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1481227444bfSJeremy L Thompson 14822b730f8bSJeremy L Thompson CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1483a8de75f0Sjeremylt 1484db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1485db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1486d1d35e2fSjeremylt (*basis)->ref_count = 1; 14876402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1488a8de75f0Sjeremylt (*basis)->dim = dim; 1489d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 1490d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 1491a8de75f0Sjeremylt (*basis)->P = P; 1492a8de75f0Sjeremylt (*basis)->Q = Q; 1493c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 14942b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * dim, &(*basis)->q_ref_1d)); 14952b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &(*basis)->q_weight_1d)); 1496ff3a0f91SJeremy L Thompson if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1497ff3a0f91SJeremy L Thompson if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 14982b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * P, &(*basis)->interp)); 14992b730f8bSJeremy L Thompson CeedCall(CeedCalloc(dim * Q * P, &(*basis)->grad)); 1500ff3a0f91SJeremy L Thompson if (interp) memcpy((*basis)->interp, interp, Q * P * sizeof(interp[0])); 1501ff3a0f91SJeremy L Thompson if (grad) memcpy((*basis)->grad, grad, dim * Q * P * sizeof(grad[0])); 15022b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, *basis)); 1503e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1504a8de75f0Sjeremylt } 1505a8de75f0Sjeremylt 1506a8de75f0Sjeremylt /** 1507859c15bbSJames Wright @brief Create a non tensor-product basis for \f$H(\mathrm{div})\f$ discretizations 150850c301a5SRezgar Shakeri 1509ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1510ea61e9acSJeremy 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 1511ea61e9acSJeremy L Thompson @param[in] num_comp Number of components (usually 1 for vectors in H(div) bases) 1512ca94c3ddSJeremy L Thompson @param[in] num_nodes Total number of nodes (DoFs per element) 1513ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1514ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points 1515ca94c3ddSJeremy L Thompson @param[in] div Row-major (`num_qpts * num_nodes`) matrix expressing divergence of basis functions at quadrature points 1516ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element 1517ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1518ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 151950c301a5SRezgar Shakeri 152050c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 152150c301a5SRezgar Shakeri 152250c301a5SRezgar Shakeri @ref User 152350c301a5SRezgar Shakeri **/ 15242b730f8bSJeremy L Thompson int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 15252b730f8bSJeremy L Thompson const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 152650c301a5SRezgar Shakeri CeedInt Q = num_qpts, P = num_nodes, dim = 0; 1527c4e3f59bSSebastian Grimberg 152850c301a5SRezgar Shakeri if (!ceed->BasisCreateHdiv) { 152950c301a5SRezgar Shakeri Ceed delegate; 15306574a04fSJeremy L Thompson 15312b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 15326574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv"); 15332b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis)); 1534*9bc66399SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 153550c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 153650c301a5SRezgar Shakeri } 153750c301a5SRezgar Shakeri 1538ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1539ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1540ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1541227444bfSJeremy L Thompson 1542c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1543c4e3f59bSSebastian Grimberg 1544db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1545db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 154650c301a5SRezgar Shakeri (*basis)->ref_count = 1; 15476402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 154850c301a5SRezgar Shakeri (*basis)->dim = dim; 154950c301a5SRezgar Shakeri (*basis)->topo = topo; 155050c301a5SRezgar Shakeri (*basis)->num_comp = num_comp; 155150c301a5SRezgar Shakeri (*basis)->P = P; 155250c301a5SRezgar Shakeri (*basis)->Q = Q; 1553c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HDIV; 15542b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 15552b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 155650c301a5SRezgar Shakeri if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 155750c301a5SRezgar Shakeri if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 15582b730f8bSJeremy L Thompson CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 15592b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * P, &(*basis)->div)); 156050c301a5SRezgar Shakeri if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 156150c301a5SRezgar Shakeri if (div) memcpy((*basis)->div, div, Q * P * sizeof(div[0])); 15622b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, q_weight, *basis)); 156350c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 156450c301a5SRezgar Shakeri } 156550c301a5SRezgar Shakeri 156650c301a5SRezgar Shakeri /** 15674385fb7fSSebastian Grimberg @brief Create a non tensor-product basis for \f$H(\mathrm{curl})\f$ discretizations 1568c4e3f59bSSebastian Grimberg 1569ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedBasis` 1570c4e3f59bSSebastian Grimberg @param[in] topo Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below 1571ca94c3ddSJeremy L Thompson @param[in] num_comp Number of components (usually 1 for vectors in \f$H(\mathrm{curl})\f$ bases) 1572ca94c3ddSJeremy L Thompson @param[in] num_nodes Total number of nodes (DoFs per element) 1573c4e3f59bSSebastian Grimberg @param[in] num_qpts Total number of quadrature points 1574ca94c3ddSJeremy L Thompson @param[in] interp Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points 1575ca94c3ddSJeremy 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 1576ca94c3ddSJeremy L Thompson @param[in] q_ref Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element 1577ca94c3ddSJeremy L Thompson @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1578ca94c3ddSJeremy L Thompson @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1579c4e3f59bSSebastian Grimberg 1580c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1581c4e3f59bSSebastian Grimberg 1582c4e3f59bSSebastian Grimberg @ref User 1583c4e3f59bSSebastian Grimberg **/ 1584c4e3f59bSSebastian Grimberg int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 1585c4e3f59bSSebastian Grimberg const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1586c4e3f59bSSebastian Grimberg CeedInt Q = num_qpts, P = num_nodes, dim = 0, curl_comp = 0; 1587c4e3f59bSSebastian Grimberg 1588d075f50bSSebastian Grimberg if (!ceed->BasisCreateHcurl) { 1589c4e3f59bSSebastian Grimberg Ceed delegate; 15906574a04fSJeremy L Thompson 1591c4e3f59bSSebastian Grimberg CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 15926574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl"); 1593c4e3f59bSSebastian Grimberg CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis)); 1594*9bc66399SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 1595c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1596c4e3f59bSSebastian Grimberg } 1597c4e3f59bSSebastian Grimberg 1598ca94c3ddSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1599ca94c3ddSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1600ca94c3ddSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1601c4e3f59bSSebastian Grimberg 1602c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1603c4e3f59bSSebastian Grimberg curl_comp = (dim < 3) ? 1 : dim; 1604c4e3f59bSSebastian Grimberg 1605db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1606db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1607c4e3f59bSSebastian Grimberg (*basis)->ref_count = 1; 16086402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1609c4e3f59bSSebastian Grimberg (*basis)->dim = dim; 1610c4e3f59bSSebastian Grimberg (*basis)->topo = topo; 1611c4e3f59bSSebastian Grimberg (*basis)->num_comp = num_comp; 1612c4e3f59bSSebastian Grimberg (*basis)->P = P; 1613c4e3f59bSSebastian Grimberg (*basis)->Q = Q; 1614c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HCURL; 1615c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 1616c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 1617c4e3f59bSSebastian Grimberg if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1618c4e3f59bSSebastian Grimberg if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 1619c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 1620c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(curl_comp * Q * P, &(*basis)->curl)); 1621c4e3f59bSSebastian Grimberg if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 1622c4e3f59bSSebastian Grimberg if (curl) memcpy((*basis)->curl, curl, curl_comp * Q * P * sizeof(curl[0])); 1623c4e3f59bSSebastian Grimberg CeedCall(ceed->BasisCreateHcurl(topo, dim, P, Q, interp, curl, q_ref, q_weight, *basis)); 1624c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1625c4e3f59bSSebastian Grimberg } 1626c4e3f59bSSebastian Grimberg 1627c4e3f59bSSebastian Grimberg /** 1628ca94c3ddSJeremy L Thompson @brief Create a `CeedBasis` for projection from the nodes of `basis_from` to the nodes of `basis_to`. 1629ba59ac12SSebastian Grimberg 1630ca94c3ddSJeremy L Thompson Only @ref CEED_EVAL_INTERP will be valid for the new basis, `basis_project`. 1631ca94c3ddSJeremy L Thompson For \f$H^1\f$ spaces, @ref CEED_EVAL_GRAD will also be valid. 1632ca94c3ddSJeremy L Thompson The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization. 1633ca94c3ddSJeremy L Thompson The gradient (for the \f$H^1\f$ case) is given by `grad_project = interp_to^+ * grad_from`. 163415ad3917SSebastian Grimberg 163515ad3917SSebastian Grimberg Note: `basis_from` and `basis_to` must have compatible quadrature spaces. 163615ad3917SSebastian Grimberg 16379fd66db6SSebastian Grimberg Note: `basis_project` will have the same number of components as `basis_from`, regardless of the number of components that `basis_to` has. 16389fd66db6SSebastian Grimberg If `basis_from` has 3 components and `basis_to` has 5 components, then `basis_project` will have 3 components. 1639f113e5dcSJeremy L Thompson 1640e104ad11SJames Wright Note: If either `basis_from` or `basis_to` are non-tensor, then `basis_project` will also be non-tensor 1641e104ad11SJames Wright 1642ca94c3ddSJeremy L Thompson @param[in] basis_from `CeedBasis` to prolong from 1643ca94c3ddSJeremy L Thompson @param[in] basis_to `CeedBasis` to prolong to 1644ca94c3ddSJeremy L Thompson @param[out] basis_project Address of the variable where the newly created `CeedBasis` will be stored 1645f113e5dcSJeremy L Thompson 1646f113e5dcSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1647f113e5dcSJeremy L Thompson 1648f113e5dcSJeremy L Thompson @ref User 1649f113e5dcSJeremy L Thompson **/ 16502b730f8bSJeremy L Thompson int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project) { 1651f113e5dcSJeremy L Thompson Ceed ceed; 1652e104ad11SJames Wright bool create_tensor; 16531c66c397SJeremy L Thompson CeedInt dim, num_comp; 1654097cc795SJames Wright CeedScalar *interp_project, *grad_project; 16551c66c397SJeremy L Thompson 16562b730f8bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis_to, &ceed)); 1657f113e5dcSJeremy L Thompson 1658ecc88aebSJeremy L Thompson // Create projection matrix 16592b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateProjectionMatrices(basis_from, basis_to, &interp_project, &grad_project)); 1660f113e5dcSJeremy L Thompson 1661f113e5dcSJeremy L Thompson // Build basis 1662e104ad11SJames Wright { 1663e104ad11SJames Wright bool is_tensor_to, is_tensor_from; 1664e104ad11SJames Wright 1665e104ad11SJames Wright CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to)); 1666e104ad11SJames Wright CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from)); 1667e104ad11SJames Wright create_tensor = is_tensor_from && is_tensor_to; 1668e104ad11SJames Wright } 16692b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_to, &dim)); 16702b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_from, &num_comp)); 1671e104ad11SJames Wright if (create_tensor) { 1672f113e5dcSJeremy L Thompson CeedInt P_1d_to, P_1d_from; 16731c66c397SJeremy L Thompson 16742b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_1d_from)); 16752b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_1d_to)); 1676097cc795SJames Wright CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, interp_project, grad_project, NULL, NULL, basis_project)); 1677f113e5dcSJeremy L Thompson } else { 1678de05fbb2SSebastian Grimberg // Even if basis_to and basis_from are not H1, the resulting basis is H1 for interpolation to work 1679f113e5dcSJeremy L Thompson CeedInt num_nodes_to, num_nodes_from; 16801c66c397SJeremy L Thompson CeedElemTopology topo; 16811c66c397SJeremy L Thompson 1682e00f3be8SJames Wright CeedCall(CeedBasisGetTopology(basis_from, &topo)); 16832b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_from, &num_nodes_from)); 16842b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_to, &num_nodes_to)); 1685097cc795SJames Wright CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, NULL, NULL, basis_project)); 1686f113e5dcSJeremy L Thompson } 1687f113e5dcSJeremy L Thompson 1688f113e5dcSJeremy L Thompson // Cleanup 16892b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_project)); 16902b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_project)); 1691*9bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 1692f113e5dcSJeremy L Thompson return CEED_ERROR_SUCCESS; 1693f113e5dcSJeremy L Thompson } 1694f113e5dcSJeremy L Thompson 1695f113e5dcSJeremy L Thompson /** 1696ca94c3ddSJeremy L Thompson @brief Copy the pointer to a `CeedBasis`. 16979560d06aSjeremylt 1698ca94c3ddSJeremy 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`. 1699ca94c3ddSJeremy L Thompson This `CeedBasis` will be destroyed if `*basis_copy` is the only reference to this `CeedBasis`. 1700ea61e9acSJeremy L Thompson 1701ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to copy reference to 1702ea61e9acSJeremy L Thompson @param[in,out] basis_copy Variable to store copied reference 17039560d06aSjeremylt 17049560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 17059560d06aSjeremylt 17069560d06aSjeremylt @ref User 17079560d06aSjeremylt **/ 17089560d06aSjeremylt int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) { 1709356036faSJeremy L Thompson if (basis != CEED_BASIS_NONE) CeedCall(CeedBasisReference(basis)); 17102b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(basis_copy)); 17119560d06aSjeremylt *basis_copy = basis; 17129560d06aSjeremylt return CEED_ERROR_SUCCESS; 17139560d06aSjeremylt } 17149560d06aSjeremylt 17159560d06aSjeremylt /** 1716ca94c3ddSJeremy L Thompson @brief View a `CeedBasis` 17177a982d89SJeremy L. Thompson 1718ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to view 1719ca94c3ddSJeremy L Thompson @param[in] stream Stream to view to, e.g., `stdout` 17207a982d89SJeremy L. Thompson 17217a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 17227a982d89SJeremy L. Thompson 17237a982d89SJeremy L. Thompson @ref User 17247a982d89SJeremy L. Thompson **/ 17257a982d89SJeremy L. Thompson int CeedBasisView(CeedBasis basis, FILE *stream) { 17261203703bSJeremy L Thompson bool is_tensor_basis; 17271203703bSJeremy L Thompson CeedElemTopology topo; 17281203703bSJeremy L Thompson CeedFESpace fe_space; 17291203703bSJeremy L Thompson 17301203703bSJeremy L Thompson // Basis data 17311203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 17321203703bSJeremy L Thompson CeedCall(CeedBasisGetTopology(basis, &topo)); 17331203703bSJeremy L Thompson CeedCall(CeedBasisGetFESpace(basis, &fe_space)); 17342b730f8bSJeremy L Thompson 173550c301a5SRezgar Shakeri // Print FE space and element topology of the basis 1736edf04919SJeremy L Thompson fprintf(stream, "CeedBasis in a %s on a %s element\n", CeedFESpaces[fe_space], CeedElemTopologies[topo]); 17371203703bSJeremy L Thompson if (is_tensor_basis) { 1738edf04919SJeremy L Thompson fprintf(stream, " P: %" CeedInt_FMT "\n Q: %" CeedInt_FMT "\n", basis->P_1d, basis->Q_1d); 173950c301a5SRezgar Shakeri } else { 1740edf04919SJeremy L Thompson fprintf(stream, " P: %" CeedInt_FMT "\n Q: %" CeedInt_FMT "\n", basis->P, basis->Q); 174150c301a5SRezgar Shakeri } 1742edf04919SJeremy L Thompson fprintf(stream, " dimension: %" CeedInt_FMT "\n field components: %" CeedInt_FMT "\n", basis->dim, basis->num_comp); 1743ea61e9acSJeremy L Thompson // Print quadrature data, interpolation/gradient/divergence/curl of the basis 17441203703bSJeremy L Thompson if (is_tensor_basis) { // tensor basis 17451203703bSJeremy L Thompson CeedInt P_1d, Q_1d; 17461203703bSJeremy L Thompson const CeedScalar *q_ref_1d, *q_weight_1d, *interp_1d, *grad_1d; 17471203703bSJeremy L Thompson 17481203703bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 17491203703bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 17501203703bSJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 17511203703bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d)); 17521203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 17531203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 17541203703bSJeremy L Thompson 17551203703bSJeremy L Thompson CeedCall(CeedScalarView("qref1d", "\t% 12.8f", 1, Q_1d, q_ref_1d, stream)); 17561203703bSJeremy L Thompson CeedCall(CeedScalarView("qweight1d", "\t% 12.8f", 1, Q_1d, q_weight_1d, stream)); 17571203703bSJeremy L Thompson CeedCall(CeedScalarView("interp1d", "\t% 12.8f", Q_1d, P_1d, interp_1d, stream)); 17581203703bSJeremy L Thompson CeedCall(CeedScalarView("grad1d", "\t% 12.8f", Q_1d, P_1d, grad_1d, stream)); 175950c301a5SRezgar Shakeri } else { // non-tensor basis 17601203703bSJeremy L Thompson CeedInt P, Q, dim, q_comp; 17611203703bSJeremy L Thompson const CeedScalar *q_ref, *q_weight, *interp, *grad, *div, *curl; 17621203703bSJeremy L Thompson 17631203703bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &P)); 17641203703bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &Q)); 17651203703bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 17661203703bSJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref)); 17671203703bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight)); 17681203703bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis, &interp)); 17691203703bSJeremy L Thompson CeedCall(CeedBasisGetGrad(basis, &grad)); 17701203703bSJeremy L Thompson CeedCall(CeedBasisGetDiv(basis, &div)); 17711203703bSJeremy L Thompson CeedCall(CeedBasisGetCurl(basis, &curl)); 17721203703bSJeremy L Thompson 17731203703bSJeremy L Thompson CeedCall(CeedScalarView("qref", "\t% 12.8f", 1, Q * dim, q_ref, stream)); 17741203703bSJeremy L Thompson CeedCall(CeedScalarView("qweight", "\t% 12.8f", 1, Q, q_weight, stream)); 1775c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp)); 17761203703bSJeremy L Thompson CeedCall(CeedScalarView("interp", "\t% 12.8f", q_comp * Q, P, interp, stream)); 17771203703bSJeremy L Thompson if (grad) { 1778c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp)); 17791203703bSJeremy L Thompson CeedCall(CeedScalarView("grad", "\t% 12.8f", q_comp * Q, P, grad, stream)); 17807a982d89SJeremy L. Thompson } 17811203703bSJeremy L Thompson if (div) { 1782c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp)); 17831203703bSJeremy L Thompson CeedCall(CeedScalarView("div", "\t% 12.8f", q_comp * Q, P, div, stream)); 1784c4e3f59bSSebastian Grimberg } 17851203703bSJeremy L Thompson if (curl) { 1786c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp)); 17871203703bSJeremy L Thompson CeedCall(CeedScalarView("curl", "\t% 12.8f", q_comp * Q, P, curl, stream)); 178850c301a5SRezgar Shakeri } 178950c301a5SRezgar Shakeri } 1790e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 17917a982d89SJeremy L. Thompson } 17927a982d89SJeremy L. Thompson 17937a982d89SJeremy L. Thompson /** 1794db2becc9SJeremy L Thompson @brief Check input vector dimensions for CeedBasisApply[Add] 17957a982d89SJeremy L. Thompson 1796ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1797ea61e9acSJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1798ca94c3ddSJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1799ca94c3ddSJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points; 1800ca94c3ddSJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes 1801ca94c3ddSJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 1802ca94c3ddSJeremy L Thompson @ref CEED_EVAL_INTERP to use interpolated values, 1803ca94c3ddSJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1804ca94c3ddSJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 1805ca94c3ddSJeremy L Thompson @ref CEED_EVAL_CURL to use curl, 1806ca94c3ddSJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1807ca94c3ddSJeremy L Thompson @param[in] u Input `CeedVector` 1808ca94c3ddSJeremy L Thompson @param[out] v Output `CeedVector` 18097a982d89SJeremy L. Thompson 18107a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 18117a982d89SJeremy L. Thompson 1812db2becc9SJeremy L Thompson @ref Developer 18137a982d89SJeremy L. Thompson **/ 1814db2becc9SJeremy L Thompson static int CeedBasisApplyCheckDims(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 1815c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 18161c66c397SJeremy L Thompson CeedSize u_length = 0, v_length; 18171c66c397SJeremy L Thompson 18182b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 18192b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 1820c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 18212b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 18222b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 18232b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(v, &v_length)); 1824c8c3fa7dSJeremy L Thompson if (u) CeedCall(CeedVectorGetLength(u, &u_length)); 18257a982d89SJeremy L. Thompson 1826e15f9bd0SJeremy L Thompson // Check vector lengths to prevent out of bounds issues 182799e754f0SJeremy L Thompson bool has_good_dims = true; 1828d1d35e2fSjeremylt switch (eval_mode) { 1829e15f9bd0SJeremy L Thompson case CEED_EVAL_NONE: 18302b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 18312b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 1832c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 1833c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 183419a04db8SJeremy L Thompson has_good_dims = ((t_mode == CEED_TRANSPOSE && u_length >= (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_qpts * (CeedSize)q_comp && 183519a04db8SJeremy L Thompson v_length >= (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_nodes) || 183619a04db8SJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && v_length >= (CeedSize)num_elem * (CeedSize)num_qpts * (CeedSize)num_comp * (CeedSize)q_comp && 183719a04db8SJeremy L Thompson u_length >= (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_nodes)); 1838e15f9bd0SJeremy L Thompson break; 1839e15f9bd0SJeremy L Thompson case CEED_EVAL_WEIGHT: 184019a04db8SJeremy L Thompson has_good_dims = v_length >= (CeedSize)num_elem * (CeedSize)num_qpts; 1841e15f9bd0SJeremy L Thompson break; 1842e15f9bd0SJeremy L Thompson } 1843*9bc66399SJeremy L Thompson CeedCheck(has_good_dims, CeedBasisReturnCeed(basis), CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 1844db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 1845db2becc9SJeremy L Thompson } 1846e15f9bd0SJeremy L Thompson 1847db2becc9SJeremy L Thompson /** 1848db2becc9SJeremy L Thompson @brief Apply basis evaluation from nodes to quadrature points or vice versa 1849db2becc9SJeremy L Thompson 1850db2becc9SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1851db2becc9SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1852db2becc9SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1853db2becc9SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points; 1854db2becc9SJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes 1855db2becc9SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 1856db2becc9SJeremy L Thompson @ref CEED_EVAL_INTERP to use interpolated values, 1857db2becc9SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1858db2becc9SJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 1859db2becc9SJeremy L Thompson @ref CEED_EVAL_CURL to use curl, 1860db2becc9SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1861db2becc9SJeremy L Thompson @param[in] u Input `CeedVector` 1862db2becc9SJeremy L Thompson @param[out] v Output `CeedVector` 1863db2becc9SJeremy L Thompson 1864db2becc9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1865db2becc9SJeremy L Thompson 1866db2becc9SJeremy L Thompson @ref User 1867db2becc9SJeremy L Thompson **/ 1868db2becc9SJeremy L Thompson int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 1869db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyCheckDims(basis, num_elem, t_mode, eval_mode, u, v)); 1870db2becc9SJeremy L Thompson CeedCheck(basis->Apply, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedBasisApply"); 18712b730f8bSJeremy L Thompson CeedCall(basis->Apply(basis, num_elem, t_mode, eval_mode, u, v)); 1872e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18737a982d89SJeremy L. Thompson } 18747a982d89SJeremy L. Thompson 18757a982d89SJeremy L. Thompson /** 1876db2becc9SJeremy L Thompson @brief Apply basis evaluation from quadrature points to nodes and sum into target vector 1877db2becc9SJeremy L Thompson 1878db2becc9SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1879db2becc9SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1880db2becc9SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1881db2becc9SJeremy L Thompson @param[in] t_mode @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes; 1882db2becc9SJeremy L Thompson @ref CEED_NOTRANSPOSE is not valid for `CeedBasisApplyAdd()` 1883db2becc9SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 1884db2becc9SJeremy L Thompson @ref CEED_EVAL_INTERP to use interpolated values, 1885db2becc9SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1886db2becc9SJeremy L Thompson @ref CEED_EVAL_DIV to use divergence, 1887db2becc9SJeremy L Thompson @ref CEED_EVAL_CURL to use curl, 1888db2becc9SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1889db2becc9SJeremy L Thompson @param[in] u Input `CeedVector` 1890db2becc9SJeremy L Thompson @param[out] v Output `CeedVector` to sum into 1891db2becc9SJeremy L Thompson 1892db2becc9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1893db2becc9SJeremy L Thompson 1894db2becc9SJeremy L Thompson @ref User 1895db2becc9SJeremy L Thompson **/ 1896db2becc9SJeremy L Thompson int CeedBasisApplyAdd(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 1897db2becc9SJeremy L Thompson CeedCheck(t_mode == CEED_TRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "CeedBasisApplyAdd only supports CEED_TRANSPOSE"); 1898db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyCheckDims(basis, num_elem, t_mode, eval_mode, u, v)); 1899db2becc9SJeremy L Thompson CeedCheck(basis->ApplyAdd, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedBasisApplyAdd"); 1900db2becc9SJeremy L Thompson CeedCall(basis->ApplyAdd(basis, num_elem, t_mode, eval_mode, u, v)); 1901db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 1902db2becc9SJeremy L Thompson } 1903db2becc9SJeremy L Thompson 1904db2becc9SJeremy L Thompson /** 1905db2becc9SJeremy L Thompson @brief Apply basis evaluation from nodes to arbitrary points 1906db2becc9SJeremy L Thompson 1907db2becc9SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1908db2becc9SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1909db2becc9SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1910db2becc9SJeremy L Thompson @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 1911db2becc9SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 1912db2becc9SJeremy L Thompson @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes 1913db2becc9SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 1914db2becc9SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1915db2becc9SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1916db2becc9SJeremy L Thompson @param[in] x_ref `CeedVector` holding reference coordinates of each point 1917db2becc9SJeremy L Thompson @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 1918db2becc9SJeremy L Thompson @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 1919db2becc9SJeremy L Thompson 1920db2becc9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1921db2becc9SJeremy L Thompson 1922db2becc9SJeremy L Thompson @ref User 1923db2becc9SJeremy L Thompson **/ 1924db2becc9SJeremy L Thompson int CeedBasisApplyAtPoints(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, 1925db2becc9SJeremy L Thompson CeedVector x_ref, CeedVector u, CeedVector v) { 1926db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyAtPointsCheckDims(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 1927db2becc9SJeremy L Thompson if (basis->ApplyAtPoints) { 1928db2becc9SJeremy L Thompson CeedCall(basis->ApplyAtPoints(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 1929db2becc9SJeremy L Thompson } else { 1930db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyAtPoints_Core(basis, false, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 1931db2becc9SJeremy L Thompson } 1932db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 1933db2becc9SJeremy L Thompson } 1934db2becc9SJeremy L Thompson 1935db2becc9SJeremy L Thompson /** 1936db2becc9SJeremy L Thompson @brief Apply basis evaluation from nodes to arbitrary points and sum into target vector 1937db2becc9SJeremy L Thompson 1938db2becc9SJeremy L Thompson @param[in] basis `CeedBasis` to evaluate 1939db2becc9SJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1940db2becc9SJeremy L Thompson the backend will specify the ordering in @ref CeedElemRestrictionCreate() 1941db2becc9SJeremy L Thompson @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 1942db2becc9SJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 1943db2becc9SJeremy L Thompson @ref CEED_NOTRANSPOSE is not valid for `CeedBasisApplyAddAtPoints()` 1944db2becc9SJeremy L Thompson @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 1945db2becc9SJeremy L Thompson @ref CEED_EVAL_GRAD to use gradients, 1946db2becc9SJeremy L Thompson @ref CEED_EVAL_WEIGHT to use quadrature weights 1947db2becc9SJeremy L Thompson @param[in] x_ref `CeedVector` holding reference coordinates of each point 1948db2becc9SJeremy L Thompson @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 1949db2becc9SJeremy L Thompson @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 1950db2becc9SJeremy L Thompson 1951db2becc9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1952db2becc9SJeremy L Thompson 1953db2becc9SJeremy L Thompson @ref User 1954db2becc9SJeremy L Thompson **/ 1955db2becc9SJeremy L Thompson int CeedBasisApplyAddAtPoints(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, 1956db2becc9SJeremy L Thompson CeedVector x_ref, CeedVector u, CeedVector v) { 1957db2becc9SJeremy L Thompson CeedCheck(t_mode == CEED_TRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "CeedBasisApplyAddAtPoints only supports CEED_TRANSPOSE"); 1958db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyAtPointsCheckDims(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 1959db2becc9SJeremy L Thompson if (basis->ApplyAddAtPoints) { 1960db2becc9SJeremy L Thompson CeedCall(basis->ApplyAddAtPoints(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 1961db2becc9SJeremy L Thompson } else { 1962db2becc9SJeremy L Thompson CeedCall(CeedBasisApplyAtPoints_Core(basis, true, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 1963db2becc9SJeremy L Thompson } 1964db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 1965db2becc9SJeremy L Thompson } 1966db2becc9SJeremy L Thompson 1967db2becc9SJeremy L Thompson /** 19686e536b99SJeremy L Thompson @brief Get the `Ceed` associated with a `CeedBasis` 1969b7c9bbdaSJeremy L Thompson 1970ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 1971ca94c3ddSJeremy L Thompson @param[out] ceed Variable to store `Ceed` 1972b7c9bbdaSJeremy L Thompson 1973b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1974b7c9bbdaSJeremy L Thompson 1975b7c9bbdaSJeremy L Thompson @ref Advanced 1976b7c9bbdaSJeremy L Thompson **/ 1977b7c9bbdaSJeremy L Thompson int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) { 1978*9bc66399SJeremy L Thompson *ceed = NULL; 1979*9bc66399SJeremy L Thompson CeedCall(CeedReferenceCopy(CeedBasisReturnCeed(basis), ceed)); 1980b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1981b7c9bbdaSJeremy L Thompson } 1982b7c9bbdaSJeremy L Thompson 1983b7c9bbdaSJeremy L Thompson /** 19846e536b99SJeremy L Thompson @brief Return the `Ceed` associated with a `CeedBasis` 19856e536b99SJeremy L Thompson 19866e536b99SJeremy L Thompson @param[in] basis `CeedBasis` 19876e536b99SJeremy L Thompson 19886e536b99SJeremy L Thompson @return `Ceed` associated with the `basis` 19896e536b99SJeremy L Thompson 19906e536b99SJeremy L Thompson @ref Advanced 19916e536b99SJeremy L Thompson **/ 19926e536b99SJeremy L Thompson Ceed CeedBasisReturnCeed(CeedBasis basis) { return basis->ceed; } 19936e536b99SJeremy L Thompson 19946e536b99SJeremy L Thompson /** 1995ca94c3ddSJeremy L Thompson @brief Get dimension for given `CeedBasis` 19969d007619Sjeremylt 1997ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 19989d007619Sjeremylt @param[out] dim Variable to store dimension of basis 19999d007619Sjeremylt 20009d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20019d007619Sjeremylt 2002b7c9bbdaSJeremy L Thompson @ref Advanced 20039d007619Sjeremylt **/ 20049d007619Sjeremylt int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) { 20059d007619Sjeremylt *dim = basis->dim; 2006e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20079d007619Sjeremylt } 20089d007619Sjeremylt 20099d007619Sjeremylt /** 2010ca94c3ddSJeremy L Thompson @brief Get topology for given `CeedBasis` 2011d99fa3c5SJeremy L Thompson 2012ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2013d99fa3c5SJeremy L Thompson @param[out] topo Variable to store topology of basis 2014d99fa3c5SJeremy L Thompson 2015d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2016d99fa3c5SJeremy L Thompson 2017b7c9bbdaSJeremy L Thompson @ref Advanced 2018d99fa3c5SJeremy L Thompson **/ 2019d99fa3c5SJeremy L Thompson int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) { 2020d99fa3c5SJeremy L Thompson *topo = basis->topo; 2021e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2022d99fa3c5SJeremy L Thompson } 2023d99fa3c5SJeremy L Thompson 2024d99fa3c5SJeremy L Thompson /** 2025ca94c3ddSJeremy L Thompson @brief Get number of components for given `CeedBasis` 20269d007619Sjeremylt 2027ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2028ca94c3ddSJeremy L Thompson @param[out] num_comp Variable to store number of components 20299d007619Sjeremylt 20309d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20319d007619Sjeremylt 2032b7c9bbdaSJeremy L Thompson @ref Advanced 20339d007619Sjeremylt **/ 2034d1d35e2fSjeremylt int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) { 2035d1d35e2fSjeremylt *num_comp = basis->num_comp; 2036e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20379d007619Sjeremylt } 20389d007619Sjeremylt 20399d007619Sjeremylt /** 2040ca94c3ddSJeremy L Thompson @brief Get total number of nodes (in `dim` dimensions) of a `CeedBasis` 20419d007619Sjeremylt 2042ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 20439d007619Sjeremylt @param[out] P Variable to store number of nodes 20449d007619Sjeremylt 20459d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20469d007619Sjeremylt 20479d007619Sjeremylt @ref Utility 20489d007619Sjeremylt **/ 20499d007619Sjeremylt int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) { 20509d007619Sjeremylt *P = basis->P; 2051e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20529d007619Sjeremylt } 20539d007619Sjeremylt 20549d007619Sjeremylt /** 2055ca94c3ddSJeremy L Thompson @brief Get total number of nodes (in 1 dimension) of a `CeedBasis` 20569d007619Sjeremylt 2057ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2058d1d35e2fSjeremylt @param[out] P_1d Variable to store number of nodes 20599d007619Sjeremylt 20609d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20619d007619Sjeremylt 2062b7c9bbdaSJeremy L Thompson @ref Advanced 20639d007619Sjeremylt **/ 2064d1d35e2fSjeremylt int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) { 20656e536b99SJeremy L Thompson CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply P_1d for non-tensor CeedBasis"); 2066d1d35e2fSjeremylt *P_1d = basis->P_1d; 2067e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20689d007619Sjeremylt } 20699d007619Sjeremylt 20709d007619Sjeremylt /** 2071ca94c3ddSJeremy L Thompson @brief Get total number of quadrature points (in `dim` dimensions) of a `CeedBasis` 20729d007619Sjeremylt 2073ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 20749d007619Sjeremylt @param[out] Q Variable to store number of quadrature points 20759d007619Sjeremylt 20769d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20779d007619Sjeremylt 20789d007619Sjeremylt @ref Utility 20799d007619Sjeremylt **/ 20809d007619Sjeremylt int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) { 20819d007619Sjeremylt *Q = basis->Q; 2082e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20839d007619Sjeremylt } 20849d007619Sjeremylt 20859d007619Sjeremylt /** 2086ca94c3ddSJeremy L Thompson @brief Get total number of quadrature points (in 1 dimension) of a `CeedBasis` 20879d007619Sjeremylt 2088ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2089d1d35e2fSjeremylt @param[out] Q_1d Variable to store number of quadrature points 20909d007619Sjeremylt 20919d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 20929d007619Sjeremylt 2093b7c9bbdaSJeremy L Thompson @ref Advanced 20949d007619Sjeremylt **/ 2095d1d35e2fSjeremylt int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) { 20966e536b99SJeremy L Thompson CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply Q_1d for non-tensor CeedBasis"); 2097d1d35e2fSjeremylt *Q_1d = basis->Q_1d; 2098e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20999d007619Sjeremylt } 21009d007619Sjeremylt 21019d007619Sjeremylt /** 2102ca94c3ddSJeremy L Thompson @brief Get reference coordinates of quadrature points (in `dim` dimensions) of a `CeedBasis` 21039d007619Sjeremylt 2104ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2105d1d35e2fSjeremylt @param[out] q_ref Variable to store reference coordinates of quadrature points 21069d007619Sjeremylt 21079d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21089d007619Sjeremylt 2109b7c9bbdaSJeremy L Thompson @ref Advanced 21109d007619Sjeremylt **/ 2111d1d35e2fSjeremylt int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) { 2112d1d35e2fSjeremylt *q_ref = basis->q_ref_1d; 2113e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21149d007619Sjeremylt } 21159d007619Sjeremylt 21169d007619Sjeremylt /** 2117ca94c3ddSJeremy L Thompson @brief Get quadrature weights of quadrature points (in `dim` dimensions) of a `CeedBasis` 21189d007619Sjeremylt 2119ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2120d1d35e2fSjeremylt @param[out] q_weight Variable to store quadrature weights 21219d007619Sjeremylt 21229d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21239d007619Sjeremylt 2124b7c9bbdaSJeremy L Thompson @ref Advanced 21259d007619Sjeremylt **/ 2126d1d35e2fSjeremylt int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) { 2127d1d35e2fSjeremylt *q_weight = basis->q_weight_1d; 2128e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21299d007619Sjeremylt } 21309d007619Sjeremylt 21319d007619Sjeremylt /** 2132ca94c3ddSJeremy L Thompson @brief Get interpolation matrix of a `CeedBasis` 21339d007619Sjeremylt 2134ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 21359d007619Sjeremylt @param[out] interp Variable to store interpolation matrix 21369d007619Sjeremylt 21379d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21389d007619Sjeremylt 2139b7c9bbdaSJeremy L Thompson @ref Advanced 21409d007619Sjeremylt **/ 21416c58de82SJeremy L Thompson int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) { 21426402da51SJeremy L Thompson if (!basis->interp && basis->is_tensor_basis) { 21439d007619Sjeremylt // Allocate 21442b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->Q * basis->P, &basis->interp)); 21459d007619Sjeremylt 21469d007619Sjeremylt // Initialize 21472b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->Q * basis->P; i++) basis->interp[i] = 1.0; 21489d007619Sjeremylt 21499d007619Sjeremylt // Calculate 21502b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 21512b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 21529d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 2153d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 2154d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 21551c66c397SJeremy L Thompson 2156d1d35e2fSjeremylt basis->interp[qpt * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 21579d007619Sjeremylt } 21589d007619Sjeremylt } 21592b730f8bSJeremy L Thompson } 21602b730f8bSJeremy L Thompson } 21619d007619Sjeremylt *interp = basis->interp; 2162e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21639d007619Sjeremylt } 21649d007619Sjeremylt 21659d007619Sjeremylt /** 2166ca94c3ddSJeremy L Thompson @brief Get 1D interpolation matrix of a tensor product `CeedBasis` 21679d007619Sjeremylt 2168ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2169d1d35e2fSjeremylt @param[out] interp_1d Variable to store interpolation matrix 21709d007619Sjeremylt 21719d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21729d007619Sjeremylt 21739d007619Sjeremylt @ref Backend 21749d007619Sjeremylt **/ 2175d1d35e2fSjeremylt int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) { 21761203703bSJeremy L Thompson bool is_tensor_basis; 21771203703bSJeremy L Thompson 21781203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 21796e536b99SJeremy L Thompson CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis"); 2180d1d35e2fSjeremylt *interp_1d = basis->interp_1d; 2181e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21829d007619Sjeremylt } 21839d007619Sjeremylt 21849d007619Sjeremylt /** 2185ca94c3ddSJeremy L Thompson @brief Get gradient matrix of a `CeedBasis` 21869d007619Sjeremylt 2187ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 21889d007619Sjeremylt @param[out] grad Variable to store gradient matrix 21899d007619Sjeremylt 21909d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 21919d007619Sjeremylt 2192b7c9bbdaSJeremy L Thompson @ref Advanced 21939d007619Sjeremylt **/ 21946c58de82SJeremy L Thompson int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) { 21956402da51SJeremy L Thompson if (!basis->grad && basis->is_tensor_basis) { 21969d007619Sjeremylt // Allocate 21972b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->dim * basis->Q * basis->P, &basis->grad)); 21989d007619Sjeremylt 21999d007619Sjeremylt // Initialize 22002b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim * basis->Q * basis->P; i++) basis->grad[i] = 1.0; 22019d007619Sjeremylt 22029d007619Sjeremylt // Calculate 22032b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 22042b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim; i++) { 22052b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 22069d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 2207d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 2208d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 22091c66c397SJeremy L Thompson 22102b730f8bSJeremy L Thompson if (i == d) basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->grad_1d[q * basis->P_1d + p]; 22112b730f8bSJeremy L Thompson else basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 22122b730f8bSJeremy L Thompson } 22132b730f8bSJeremy L Thompson } 22142b730f8bSJeremy L Thompson } 22159d007619Sjeremylt } 22169d007619Sjeremylt } 22179d007619Sjeremylt *grad = basis->grad; 2218e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 22199d007619Sjeremylt } 22209d007619Sjeremylt 22219d007619Sjeremylt /** 2222ca94c3ddSJeremy L Thompson @brief Get 1D gradient matrix of a tensor product `CeedBasis` 22239d007619Sjeremylt 2224ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2225d1d35e2fSjeremylt @param[out] grad_1d Variable to store gradient matrix 22269d007619Sjeremylt 22279d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 22289d007619Sjeremylt 2229b7c9bbdaSJeremy L Thompson @ref Advanced 22309d007619Sjeremylt **/ 2231d1d35e2fSjeremylt int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) { 22321203703bSJeremy L Thompson bool is_tensor_basis; 22331203703bSJeremy L Thompson 22341203703bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 22356e536b99SJeremy L Thompson CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis"); 2236d1d35e2fSjeremylt *grad_1d = basis->grad_1d; 2237e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 22389d007619Sjeremylt } 22399d007619Sjeremylt 22409d007619Sjeremylt /** 2241ca94c3ddSJeremy L Thompson @brief Get divergence matrix of a `CeedBasis` 224250c301a5SRezgar Shakeri 2243ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 224450c301a5SRezgar Shakeri @param[out] div Variable to store divergence matrix 224550c301a5SRezgar Shakeri 224650c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 224750c301a5SRezgar Shakeri 224850c301a5SRezgar Shakeri @ref Advanced 224950c301a5SRezgar Shakeri **/ 225050c301a5SRezgar Shakeri int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) { 225150c301a5SRezgar Shakeri *div = basis->div; 225250c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 225350c301a5SRezgar Shakeri } 225450c301a5SRezgar Shakeri 225550c301a5SRezgar Shakeri /** 2256ca94c3ddSJeremy L Thompson @brief Get curl matrix of a `CeedBasis` 2257c4e3f59bSSebastian Grimberg 2258ca94c3ddSJeremy L Thompson @param[in] basis `CeedBasis` 2259c4e3f59bSSebastian Grimberg @param[out] curl Variable to store curl matrix 2260c4e3f59bSSebastian Grimberg 2261c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 2262c4e3f59bSSebastian Grimberg 2263c4e3f59bSSebastian Grimberg @ref Advanced 2264c4e3f59bSSebastian Grimberg **/ 2265c4e3f59bSSebastian Grimberg int CeedBasisGetCurl(CeedBasis basis, const CeedScalar **curl) { 2266c4e3f59bSSebastian Grimberg *curl = basis->curl; 2267c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 2268c4e3f59bSSebastian Grimberg } 2269c4e3f59bSSebastian Grimberg 2270c4e3f59bSSebastian Grimberg /** 2271ca94c3ddSJeremy L Thompson @brief Destroy a @ref CeedBasis 22727a982d89SJeremy L. Thompson 2273ca94c3ddSJeremy L Thompson @param[in,out] basis `CeedBasis` to destroy 22747a982d89SJeremy L. Thompson 22757a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 22767a982d89SJeremy L. Thompson 22777a982d89SJeremy L. Thompson @ref User 22787a982d89SJeremy L. Thompson **/ 22797a982d89SJeremy L. Thompson int CeedBasisDestroy(CeedBasis *basis) { 2280356036faSJeremy L Thompson if (!*basis || *basis == CEED_BASIS_NONE || --(*basis)->ref_count > 0) { 2281ad6481ceSJeremy L Thompson *basis = NULL; 2282ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 2283ad6481ceSJeremy L Thompson } 22842b730f8bSJeremy L Thompson if ((*basis)->Destroy) CeedCall((*basis)->Destroy(*basis)); 22859831d45aSJeremy L Thompson CeedCall(CeedTensorContractDestroy(&(*basis)->contract)); 2286c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_ref_1d)); 2287c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_weight_1d)); 22882b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp)); 22892b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp_1d)); 22902b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad)); 22912b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad_1d)); 2292c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->div)); 2293c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->curl)); 2294c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorDestroy(&(*basis)->vec_chebyshev)); 2295c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisDestroy(&(*basis)->basis_chebyshev)); 22962b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*basis)->ceed)); 22972b730f8bSJeremy L Thompson CeedCall(CeedFree(basis)); 2298e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 22997a982d89SJeremy L. Thompson } 23007a982d89SJeremy L. Thompson 23017a982d89SJeremy L. Thompson /** 2302b11c1e72Sjeremylt @brief Construct a Gauss-Legendre quadrature 2303b11c1e72Sjeremylt 2304ca94c3ddSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree `2*Q-1` exactly) 2305ca94c3ddSJeremy L Thompson @param[out] q_ref_1d Array of length `Q` to hold the abscissa on `[-1, 1]` 2306ca94c3ddSJeremy L Thompson @param[out] q_weight_1d Array of length `Q` to hold the weights 2307b11c1e72Sjeremylt 2308b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 2309dfdf5a53Sjeremylt 2310dfdf5a53Sjeremylt @ref Utility 2311b11c1e72Sjeremylt **/ 23122b730f8bSJeremy L Thompson int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2313d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0 * atan(1.0); 23141c66c397SJeremy L Thompson 2315d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 231692ae7e47SJeremy L Thompson for (CeedInt i = 0; i <= Q / 2; i++) { 2317d7b241e6Sjeremylt // Guess 2318d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(2 * i + 1) / ((CeedScalar)(2 * Q))); 2319d7b241e6Sjeremylt // Pn(xi) 2320d7b241e6Sjeremylt P0 = 1.0; 2321d7b241e6Sjeremylt P1 = xi; 2322d7b241e6Sjeremylt P2 = 0.0; 232392ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 2324d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2325d7b241e6Sjeremylt P0 = P1; 2326d7b241e6Sjeremylt P1 = P2; 2327d7b241e6Sjeremylt } 2328d7b241e6Sjeremylt // First Newton Step 2329d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2330d7b241e6Sjeremylt xi = xi - P2 / dP2; 2331d7b241e6Sjeremylt // Newton to convergence 233292ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(P2) > 10 * CEED_EPSILON; k++) { 2333d7b241e6Sjeremylt P0 = 1.0; 2334d7b241e6Sjeremylt P1 = xi; 233592ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 2336d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2337d7b241e6Sjeremylt P0 = P1; 2338d7b241e6Sjeremylt P1 = P2; 2339d7b241e6Sjeremylt } 2340d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2341d7b241e6Sjeremylt xi = xi - P2 / dP2; 2342d7b241e6Sjeremylt } 2343d7b241e6Sjeremylt // Save xi, wi 2344d7b241e6Sjeremylt wi = 2.0 / ((1.0 - xi * xi) * dP2 * dP2); 2345d1d35e2fSjeremylt q_weight_1d[i] = wi; 2346d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 2347d1d35e2fSjeremylt q_ref_1d[i] = -xi; 2348d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 2349d7b241e6Sjeremylt } 2350e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2351d7b241e6Sjeremylt } 2352d7b241e6Sjeremylt 2353b11c1e72Sjeremylt /** 2354b11c1e72Sjeremylt @brief Construct a Gauss-Legendre-Lobatto quadrature 2355b11c1e72Sjeremylt 2356ca94c3ddSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree `2*Q-3` exactly) 2357ca94c3ddSJeremy L Thompson @param[out] q_ref_1d Array of length `Q` to hold the abscissa on `[-1, 1]` 2358ca94c3ddSJeremy L Thompson @param[out] q_weight_1d Array of length `Q` to hold the weights 2359b11c1e72Sjeremylt 2360b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 2361dfdf5a53Sjeremylt 2362dfdf5a53Sjeremylt @ref Utility 2363b11c1e72Sjeremylt **/ 23642b730f8bSJeremy L Thompson int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2365d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0 * atan(1.0); 23661c66c397SJeremy L Thompson 2367d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 2368d7b241e6Sjeremylt // Set endpoints 23696574a04fSJeremy L Thompson CeedCheck(Q > 1, NULL, CEED_ERROR_DIMENSION, "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q); 2370d7b241e6Sjeremylt wi = 2.0 / ((CeedScalar)(Q * (Q - 1))); 2371d1d35e2fSjeremylt if (q_weight_1d) { 2372d1d35e2fSjeremylt q_weight_1d[0] = wi; 2373d1d35e2fSjeremylt q_weight_1d[Q - 1] = wi; 2374d7b241e6Sjeremylt } 2375d1d35e2fSjeremylt q_ref_1d[0] = -1.0; 2376d1d35e2fSjeremylt q_ref_1d[Q - 1] = 1.0; 2377d7b241e6Sjeremylt // Interior 237892ae7e47SJeremy L Thompson for (CeedInt i = 1; i <= (Q - 1) / 2; i++) { 2379d7b241e6Sjeremylt // Guess 2380d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(i) / (CeedScalar)(Q - 1)); 2381d7b241e6Sjeremylt // Pn(xi) 2382d7b241e6Sjeremylt P0 = 1.0; 2383d7b241e6Sjeremylt P1 = xi; 2384d7b241e6Sjeremylt P2 = 0.0; 238592ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 2386d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2387d7b241e6Sjeremylt P0 = P1; 2388d7b241e6Sjeremylt P1 = P2; 2389d7b241e6Sjeremylt } 2390d7b241e6Sjeremylt // First Newton step 2391d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2392d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2393d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 2394d7b241e6Sjeremylt // Newton to convergence 239592ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(dP2) > 10 * CEED_EPSILON; k++) { 2396d7b241e6Sjeremylt P0 = 1.0; 2397d7b241e6Sjeremylt P1 = xi; 239892ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 2399d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2400d7b241e6Sjeremylt P0 = P1; 2401d7b241e6Sjeremylt P1 = P2; 2402d7b241e6Sjeremylt } 2403d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2404d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2405d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 2406d7b241e6Sjeremylt } 2407d7b241e6Sjeremylt // Save xi, wi 2408d7b241e6Sjeremylt wi = 2.0 / (((CeedScalar)(Q * (Q - 1))) * P2 * P2); 2409d1d35e2fSjeremylt if (q_weight_1d) { 2410d1d35e2fSjeremylt q_weight_1d[i] = wi; 2411d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 2412d7b241e6Sjeremylt } 2413d1d35e2fSjeremylt q_ref_1d[i] = -xi; 2414d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 2415d7b241e6Sjeremylt } 2416e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2417d7b241e6Sjeremylt } 2418d7b241e6Sjeremylt 2419d7b241e6Sjeremylt /// @} 2420