xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-basis.c (revision db2becc9f302fe8eb3a32ace50ce3f3a5d42e6c4)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3d7b241e6Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5d7b241e6Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7d7b241e6Sjeremylt 
83d576824SJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
11d7b241e6Sjeremylt #include <math.h>
123d576824SJeremy L Thompson #include <stdbool.h>
13d7b241e6Sjeremylt #include <stdio.h>
14d7b241e6Sjeremylt #include <string.h>
15d7b241e6Sjeremylt 
167a982d89SJeremy L. Thompson /// @file
177a982d89SJeremy L. Thompson /// Implementation of CeedBasis interfaces
187a982d89SJeremy L. Thompson 
19d7b241e6Sjeremylt /// @cond DOXYGEN_SKIP
20356036faSJeremy L Thompson static struct CeedBasis_private ceed_basis_none;
21d7b241e6Sjeremylt /// @endcond
22d7b241e6Sjeremylt 
237a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser
247a982d89SJeremy L. Thompson /// @{
257a982d89SJeremy L. Thompson 
26ca94c3ddSJeremy L Thompson /// Argument for @ref CeedOperatorSetField() indicating that the field does not require a `CeedBasis`
27356036faSJeremy L Thompson const CeedBasis CEED_BASIS_NONE = &ceed_basis_none;
28356036faSJeremy L Thompson 
297a982d89SJeremy L. Thompson /// @}
307a982d89SJeremy L. Thompson 
317a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
327a982d89SJeremy L. Thompson /// CeedBasis Library Internal Functions
337a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
347a982d89SJeremy L. Thompson /// @addtogroup CeedBasisDeveloper
357a982d89SJeremy L. Thompson /// @{
367a982d89SJeremy L. Thompson 
377a982d89SJeremy L. Thompson /**
383778dbaaSJeremy L Thompson   @brief Compute Chebyshev polynomial values at a point
393778dbaaSJeremy L Thompson 
403778dbaaSJeremy L Thompson   @param[in]  x           Coordinate to evaluate Chebyshev polynomials at
41ca94c3ddSJeremy L Thompson   @param[in]  n           Number of Chebyshev polynomials to evaluate, `n >= 2`
423778dbaaSJeremy L Thompson   @param[out] chebyshev_x Array of Chebyshev polynomial values
433778dbaaSJeremy L Thompson 
443778dbaaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
453778dbaaSJeremy L Thompson 
463778dbaaSJeremy L Thompson   @ref Developer
473778dbaaSJeremy L Thompson **/
483778dbaaSJeremy L Thompson static int CeedChebyshevPolynomialsAtPoint(CeedScalar x, CeedInt n, CeedScalar *chebyshev_x) {
493778dbaaSJeremy L Thompson   chebyshev_x[0] = 1.0;
503778dbaaSJeremy L Thompson   chebyshev_x[1] = 2 * x;
513778dbaaSJeremy L Thompson   for (CeedInt i = 2; i < n; i++) chebyshev_x[i] = 2 * x * chebyshev_x[i - 1] - chebyshev_x[i - 2];
523778dbaaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
533778dbaaSJeremy L Thompson }
543778dbaaSJeremy L Thompson 
553778dbaaSJeremy L Thompson /**
563778dbaaSJeremy L Thompson   @brief Compute values of the derivative of Chebyshev polynomials at a point
573778dbaaSJeremy L Thompson 
583778dbaaSJeremy L Thompson   @param[in]  x            Coordinate to evaluate derivative of Chebyshev polynomials at
59ca94c3ddSJeremy L Thompson   @param[in]  n            Number of Chebyshev polynomials to evaluate, `n >= 2`
606cec60aaSJed Brown   @param[out] chebyshev_dx Array of Chebyshev polynomial derivative values
613778dbaaSJeremy L Thompson 
623778dbaaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
633778dbaaSJeremy L Thompson 
643778dbaaSJeremy L Thompson   @ref Developer
653778dbaaSJeremy L Thompson **/
663778dbaaSJeremy L Thompson static int CeedChebyshevDerivativeAtPoint(CeedScalar x, CeedInt n, CeedScalar *chebyshev_dx) {
673778dbaaSJeremy L Thompson   CeedScalar chebyshev_x[3];
683778dbaaSJeremy L Thompson 
693778dbaaSJeremy L Thompson   chebyshev_x[1]  = 1.0;
703778dbaaSJeremy L Thompson   chebyshev_x[2]  = 2 * x;
713778dbaaSJeremy L Thompson   chebyshev_dx[0] = 0.0;
723778dbaaSJeremy L Thompson   chebyshev_dx[1] = 2.0;
733778dbaaSJeremy L Thompson   for (CeedInt i = 2; i < n; i++) {
743778dbaaSJeremy L Thompson     chebyshev_x[0]  = chebyshev_x[1];
753778dbaaSJeremy L Thompson     chebyshev_x[1]  = chebyshev_x[2];
763778dbaaSJeremy L Thompson     chebyshev_x[2]  = 2 * x * chebyshev_x[1] - chebyshev_x[0];
773778dbaaSJeremy L Thompson     chebyshev_dx[i] = 2 * x * chebyshev_dx[i - 1] + 2 * chebyshev_x[1] - chebyshev_dx[i - 2];
783778dbaaSJeremy L Thompson   }
793778dbaaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
803778dbaaSJeremy L Thompson }
813778dbaaSJeremy L Thompson 
823778dbaaSJeremy L Thompson /**
83ca94c3ddSJeremy L Thompson   @brief Compute Householder reflection.
847a982d89SJeremy L. Thompson 
85ca94c3ddSJeremy L Thompson   Computes \f$A = (I - b v v^T) A\f$, where \f$A\f$ is an \f$m \times n\f$ matrix indexed as `A[i*row + j*col]`.
867a982d89SJeremy L. Thompson 
877a982d89SJeremy L. Thompson   @param[in,out] A   Matrix to apply Householder reflection to, in place
88ea61e9acSJeremy L Thompson   @param[in]     v   Householder vector
89ea61e9acSJeremy L Thompson   @param[in]     b   Scaling factor
90ca94c3ddSJeremy L Thompson   @param[in]     m   Number of rows in `A`
91ca94c3ddSJeremy L Thompson   @param[in]     n   Number of columns in `A`
92ea61e9acSJeremy L Thompson   @param[in]     row Row stride
93ea61e9acSJeremy L Thompson   @param[in]     col Col stride
947a982d89SJeremy L. Thompson 
957a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
967a982d89SJeremy L. Thompson 
977a982d89SJeremy L. Thompson   @ref Developer
987a982d89SJeremy L. Thompson **/
992b730f8bSJeremy L Thompson static int CeedHouseholderReflect(CeedScalar *A, const CeedScalar *v, CeedScalar b, CeedInt m, CeedInt n, CeedInt row, CeedInt col) {
1007a982d89SJeremy L. Thompson   for (CeedInt j = 0; j < n; j++) {
1017a982d89SJeremy L. Thompson     CeedScalar w = A[0 * row + j * col];
1021c66c397SJeremy L Thompson 
1032b730f8bSJeremy L Thompson     for (CeedInt i = 1; i < m; i++) w += v[i] * A[i * row + j * col];
1047a982d89SJeremy L. Thompson     A[0 * row + j * col] -= b * w;
1052b730f8bSJeremy L Thompson     for (CeedInt i = 1; i < m; i++) A[i * row + j * col] -= b * w * v[i];
1067a982d89SJeremy L. Thompson   }
107e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1087a982d89SJeremy L. Thompson }
1097a982d89SJeremy L. Thompson 
1107a982d89SJeremy L. Thompson /**
1117a982d89SJeremy L. Thompson   @brief Compute Givens rotation
1127a982d89SJeremy L. Thompson 
113ca94c3ddSJeremy L Thompson   Computes \f$A = G A\f$ (or \f$G^T A\f$ in transpose mode), where \f$A\f$ is an \f$m \times n\f$ matrix indexed as `A[i*n + j*m]`.
1147a982d89SJeremy L. Thompson 
1157a982d89SJeremy L. Thompson   @param[in,out] A      Row major matrix to apply Givens rotation to, in place
116ea61e9acSJeremy L Thompson   @param[in]     c      Cosine factor
117ea61e9acSJeremy L Thompson   @param[in]     s      Sine factor
118ca94c3ddSJeremy L Thompson   @param[in]     t_mode @ref CEED_NOTRANSPOSE to rotate the basis counter-clockwise, which has the effect of rotating columns of `A` clockwise;
1194cc79fe7SJed Brown                           @ref CEED_TRANSPOSE for the opposite rotation
120ea61e9acSJeremy L Thompson   @param[in]     i      First row/column to apply rotation
121ea61e9acSJeremy L Thompson   @param[in]     k      Second row/column to apply rotation
122ca94c3ddSJeremy L Thompson   @param[in]     m      Number of rows in `A`
123ca94c3ddSJeremy L Thompson   @param[in]     n      Number of columns in `A`
1247a982d89SJeremy L. Thompson 
1257a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1267a982d89SJeremy L. Thompson 
1277a982d89SJeremy L. Thompson   @ref Developer
1287a982d89SJeremy L. Thompson **/
1292b730f8bSJeremy L Thompson static int CeedGivensRotation(CeedScalar *A, CeedScalar c, CeedScalar s, CeedTransposeMode t_mode, CeedInt i, CeedInt k, CeedInt m, CeedInt n) {
130d1d35e2fSjeremylt   CeedInt stride_j = 1, stride_ik = m, num_its = n;
1311c66c397SJeremy L Thompson 
132d1d35e2fSjeremylt   if (t_mode == CEED_NOTRANSPOSE) {
1332b730f8bSJeremy L Thompson     stride_j  = n;
1342b730f8bSJeremy L Thompson     stride_ik = 1;
1352b730f8bSJeremy L Thompson     num_its   = m;
1367a982d89SJeremy L. Thompson   }
1377a982d89SJeremy L. Thompson 
1387a982d89SJeremy L. Thompson   // Apply rotation
139d1d35e2fSjeremylt   for (CeedInt j = 0; j < num_its; j++) {
140d1d35e2fSjeremylt     CeedScalar tau1 = A[i * stride_ik + j * stride_j], tau2 = A[k * stride_ik + j * stride_j];
1411c66c397SJeremy L Thompson 
142d1d35e2fSjeremylt     A[i * stride_ik + j * stride_j] = c * tau1 - s * tau2;
143d1d35e2fSjeremylt     A[k * stride_ik + j * stride_j] = s * tau1 + c * tau2;
1447a982d89SJeremy L. Thompson   }
145e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1467a982d89SJeremy L. Thompson }
1477a982d89SJeremy L. Thompson 
1487a982d89SJeremy L. Thompson /**
149ca94c3ddSJeremy L Thompson   @brief View an array stored in a `CeedBasis`
1507a982d89SJeremy L. Thompson 
1510a0da059Sjeremylt   @param[in] name   Name of array
152d1d35e2fSjeremylt   @param[in] fp_fmt Printing format
1530a0da059Sjeremylt   @param[in] m      Number of rows in array
1540a0da059Sjeremylt   @param[in] n      Number of columns in array
1550a0da059Sjeremylt   @param[in] a      Array to be viewed
156ca94c3ddSJeremy L Thompson   @param[in] stream Stream to view to, e.g., `stdout`
1577a982d89SJeremy L. Thompson 
1587a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1597a982d89SJeremy L. Thompson 
1607a982d89SJeremy L. Thompson   @ref Developer
1617a982d89SJeremy L. Thompson **/
1622b730f8bSJeremy L Thompson static int CeedScalarView(const char *name, const char *fp_fmt, CeedInt m, CeedInt n, const CeedScalar *a, FILE *stream) {
163edf04919SJeremy L Thompson   if (m > 1) {
164edf04919SJeremy L Thompson     fprintf(stream, "  %s:\n", name);
165edf04919SJeremy L Thompson   } else {
166edf04919SJeremy L Thompson     char padded_name[12];
167edf04919SJeremy L Thompson 
168edf04919SJeremy L Thompson     snprintf(padded_name, 11, "%s:", name);
169edf04919SJeremy L Thompson     fprintf(stream, "  %-10s", padded_name);
170edf04919SJeremy L Thompson   }
17192ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < m; i++) {
172edf04919SJeremy L Thompson     if (m > 1) fprintf(stream, "    [%" CeedInt_FMT "]", i);
1732b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < n; j++) fprintf(stream, fp_fmt, fabs(a[i * n + j]) > 1E-14 ? a[i * n + j] : 0);
1747a982d89SJeremy L. Thompson     fputs("\n", stream);
1757a982d89SJeremy L. Thompson   }
176e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1777a982d89SJeremy L. Thompson }
1787a982d89SJeremy L. Thompson 
179a76a04e7SJeremy L Thompson /**
180ea61e9acSJeremy L Thompson   @brief Create the interpolation and gradient matrices for projection from the nodes of `basis_from` to the nodes of `basis_to`.
181ba59ac12SSebastian Grimberg 
18215ad3917SSebastian Grimberg   The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization.
183ca94c3ddSJeremy L Thompson   The gradient is given by `grad_project = interp_to^+ * grad_from`, and is only computed for \f$H^1\f$ spaces otherwise it should not be used.
18415ad3917SSebastian Grimberg 
185ba59ac12SSebastian Grimberg   Note: `basis_from` and `basis_to` must have compatible quadrature spaces.
186a76a04e7SJeremy L Thompson 
187ca94c3ddSJeremy L Thompson   @param[in]  basis_from     `CeedBasis` to project from
188ca94c3ddSJeremy L Thompson   @param[in]  basis_to       `CeedBasis` to project to
189ca94c3ddSJeremy L Thompson   @param[out] interp_project Address of the variable where the newly created interpolation matrix will be stored
190ca94c3ddSJeremy L Thompson   @param[out] grad_project   Address of the variable where the newly created gradient matrix will be stored
191a76a04e7SJeremy L Thompson 
192a76a04e7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
193a76a04e7SJeremy L Thompson 
194a76a04e7SJeremy L Thompson   @ref Developer
195a76a04e7SJeremy L Thompson **/
1962b730f8bSJeremy L Thompson static int CeedBasisCreateProjectionMatrices(CeedBasis basis_from, CeedBasis basis_to, CeedScalar **interp_project, CeedScalar **grad_project) {
197a76a04e7SJeremy L Thompson   Ceed    ceed;
198e104ad11SJames Wright   bool    are_both_tensor;
1991c66c397SJeremy L Thompson   CeedInt Q, Q_to, Q_from, P_to, P_from;
2001c66c397SJeremy L Thompson 
2012b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis_to, &ceed));
202a76a04e7SJeremy L Thompson 
203a76a04e7SJeremy L Thompson   // Check for compatible quadrature spaces
2042b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_to, &Q_to));
2052b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_from, &Q_from));
2063f08121cSJeremy L Thompson   CeedCheck(Q_to == Q_from, ceed, CEED_ERROR_DIMENSION,
2073f08121cSJeremy L Thompson             "Bases must have compatible quadrature spaces."
20823622755SJeremy L Thompson             " 'basis_from' has %" CeedInt_FMT " points and 'basis_to' has %" CeedInt_FMT,
2093f08121cSJeremy L Thompson             Q_from, Q_to);
2101c66c397SJeremy L Thompson   Q = Q_to;
211a76a04e7SJeremy L Thompson 
21214556e63SJeremy L Thompson   // Check for matching tensor or non-tensor
213e104ad11SJames Wright   {
214e104ad11SJames Wright     bool is_tensor_to, is_tensor_from;
215e104ad11SJames Wright 
2162b730f8bSJeremy L Thompson     CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to));
2172b730f8bSJeremy L Thompson     CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from));
218e104ad11SJames Wright     are_both_tensor = is_tensor_to && is_tensor_from;
219e104ad11SJames Wright   }
220e104ad11SJames Wright   if (are_both_tensor) {
2212b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_to));
2222b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_from));
2232b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints1D(basis_from, &Q));
2246574a04fSJeremy L Thompson   } else {
2252b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_to, &P_to));
2262b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_from, &P_from));
227a76a04e7SJeremy L Thompson   }
228a76a04e7SJeremy L Thompson 
22915ad3917SSebastian Grimberg   // Check for matching FE space
23015ad3917SSebastian Grimberg   CeedFESpace fe_space_to, fe_space_from;
2313f08121cSJeremy L Thompson 
23215ad3917SSebastian Grimberg   CeedCall(CeedBasisGetFESpace(basis_to, &fe_space_to));
23315ad3917SSebastian Grimberg   CeedCall(CeedBasisGetFESpace(basis_from, &fe_space_from));
2343f08121cSJeremy L Thompson   CeedCheck(fe_space_to == fe_space_from, ceed, CEED_ERROR_MINOR,
2353f08121cSJeremy L Thompson             "Bases must both be the same FE space type."
2363f08121cSJeremy L Thompson             " 'basis_from' is a %s and 'basis_to' is a %s",
2373f08121cSJeremy L Thompson             CeedFESpaces[fe_space_from], CeedFESpaces[fe_space_to]);
23815ad3917SSebastian Grimberg 
23914556e63SJeremy L Thompson   // Get source matrices
24015ad3917SSebastian Grimberg   CeedInt           dim, q_comp = 1;
2412247a93fSRezgar Shakeri   CeedScalar       *interp_to_inv, *interp_from;
2421c66c397SJeremy L Thompson   const CeedScalar *interp_to_source = NULL, *interp_from_source = NULL, *grad_from_source = NULL;
2431c66c397SJeremy L Thompson 
244b3ed00e5SJames Wright   CeedCall(CeedBasisGetDimension(basis_from, &dim));
245e104ad11SJames Wright   if (are_both_tensor) {
2462b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis_to, &interp_to_source));
2472b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis_from, &interp_from_source));
248a76a04e7SJeremy L Thompson   } else {
24915ad3917SSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis_from, CEED_EVAL_INTERP, &q_comp));
2502b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(basis_to, &interp_to_source));
2512b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(basis_from, &interp_from_source));
25215ad3917SSebastian Grimberg   }
25315ad3917SSebastian Grimberg   CeedCall(CeedMalloc(Q * P_from * q_comp, &interp_from));
25415ad3917SSebastian Grimberg   CeedCall(CeedCalloc(P_to * P_from, interp_project));
25515ad3917SSebastian Grimberg 
25615ad3917SSebastian Grimberg   // `grad_project = interp_to^+ * grad_from` is computed for the H^1 space case so the
257de05fbb2SSebastian Grimberg   // projection basis will have a gradient operation (allocated even if not H^1 for the
258de05fbb2SSebastian Grimberg   // basis construction later on)
25915ad3917SSebastian Grimberg   if (fe_space_to == CEED_FE_SPACE_H1) {
260e104ad11SJames Wright     if (are_both_tensor) {
26115ad3917SSebastian Grimberg       CeedCall(CeedBasisGetGrad1D(basis_from, &grad_from_source));
26215ad3917SSebastian Grimberg     } else {
2632b730f8bSJeremy L Thompson       CeedCall(CeedBasisGetGrad(basis_from, &grad_from_source));
264a76a04e7SJeremy L Thompson     }
265de05fbb2SSebastian Grimberg   }
266e104ad11SJames Wright   CeedCall(CeedCalloc(P_to * P_from * (are_both_tensor ? 1 : dim), grad_project));
26715ad3917SSebastian Grimberg 
2682247a93fSRezgar Shakeri   // Compute interp_to^+, pseudoinverse of interp_to
2692247a93fSRezgar Shakeri   CeedCall(CeedCalloc(Q * q_comp * P_to, &interp_to_inv));
2701203703bSJeremy L Thompson   CeedCall(CeedMatrixPseudoinverse(ceed, interp_to_source, Q * q_comp, P_to, interp_to_inv));
27114556e63SJeremy L Thompson   // Build matrices
272e104ad11SJames Wright   CeedInt     num_matrices = 1 + (fe_space_to == CEED_FE_SPACE_H1) * (are_both_tensor ? 1 : dim);
27314556e63SJeremy L Thompson   CeedScalar *input_from[num_matrices], *output_project[num_matrices];
2741c66c397SJeremy L Thompson 
27514556e63SJeremy L Thompson   input_from[0]     = (CeedScalar *)interp_from_source;
27614556e63SJeremy L Thompson   output_project[0] = *interp_project;
27714556e63SJeremy L Thompson   for (CeedInt m = 1; m < num_matrices; m++) {
27814556e63SJeremy L Thompson     input_from[m]     = (CeedScalar *)&grad_from_source[(m - 1) * Q * P_from];
27902af4036SJeremy L Thompson     output_project[m] = &((*grad_project)[(m - 1) * P_to * P_from]);
28014556e63SJeremy L Thompson   }
28114556e63SJeremy L Thompson   for (CeedInt m = 0; m < num_matrices; m++) {
2822247a93fSRezgar Shakeri     // output_project = interp_to^+ * interp_from
28315ad3917SSebastian Grimberg     memcpy(interp_from, input_from[m], Q * P_from * q_comp * sizeof(input_from[m][0]));
2842247a93fSRezgar Shakeri     CeedCall(CeedMatrixMatrixMultiply(ceed, interp_to_inv, input_from[m], output_project[m], P_to, P_from, Q * q_comp));
2852247a93fSRezgar Shakeri     // Round zero to machine precision
2862247a93fSRezgar Shakeri     for (CeedInt i = 0; i < P_to * P_from; i++) {
2872247a93fSRezgar Shakeri       if (fabs(output_project[m][i]) < 10 * CEED_EPSILON) output_project[m][i] = 0.0;
288a76a04e7SJeremy L Thompson     }
28914556e63SJeremy L Thompson   }
29014556e63SJeremy L Thompson 
29114556e63SJeremy L Thompson   // Cleanup
2922247a93fSRezgar Shakeri   CeedCall(CeedFree(&interp_to_inv));
2932b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_from));
294a76a04e7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
295a76a04e7SJeremy L Thompson }
296a76a04e7SJeremy L Thompson 
2977a982d89SJeremy L. Thompson /// @}
2987a982d89SJeremy L. Thompson 
2997a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3007a982d89SJeremy L. Thompson /// Ceed Backend API
3017a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3027a982d89SJeremy L. Thompson /// @addtogroup CeedBasisBackend
3037a982d89SJeremy L. Thompson /// @{
3047a982d89SJeremy L. Thompson 
3057a982d89SJeremy L. Thompson /**
306ca94c3ddSJeremy L Thompson   @brief Return collocated gradient matrix
3077a982d89SJeremy L. Thompson 
308ca94c3ddSJeremy L Thompson   @param[in]  basis         `CeedBasis`
309ca94c3ddSJeremy L Thompson   @param[out] collo_grad_1d Row-major (`Q_1d * Q_1d`) matrix expressing derivatives of basis functions at quadrature points
3107a982d89SJeremy L. Thompson 
3117a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3127a982d89SJeremy L. Thompson 
3137a982d89SJeremy L. Thompson   @ref Backend
3147a982d89SJeremy L. Thompson **/
315d1d35e2fSjeremylt int CeedBasisGetCollocatedGrad(CeedBasis basis, CeedScalar *collo_grad_1d) {
3167a982d89SJeremy L. Thompson   Ceed              ceed;
3172247a93fSRezgar Shakeri   CeedInt           P_1d, Q_1d;
3182247a93fSRezgar Shakeri   CeedScalar       *interp_1d_pinv;
3191203703bSJeremy L Thompson   const CeedScalar *grad_1d, *interp_1d;
3201203703bSJeremy L Thompson 
321ea61e9acSJeremy L Thompson   // Note: This function is for backend use, so all errors are terminal and we do not need to clean up memory on failure.
3222247a93fSRezgar Shakeri   CeedCall(CeedBasisGetCeed(basis, &ceed));
3232247a93fSRezgar Shakeri   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
3242247a93fSRezgar Shakeri   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
3257a982d89SJeremy L. Thompson 
3262247a93fSRezgar Shakeri   // Compute interp_1d^+, pseudoinverse of interp_1d
3272247a93fSRezgar Shakeri   CeedCall(CeedCalloc(P_1d * Q_1d, &interp_1d_pinv));
3281203703bSJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
3291203703bSJeremy L Thompson   CeedCall(CeedMatrixPseudoinverse(ceed, interp_1d, Q_1d, P_1d, interp_1d_pinv));
3301203703bSJeremy L Thompson   CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
3311203703bSJeremy L Thompson   CeedCall(CeedMatrixMatrixMultiply(ceed, grad_1d, (const CeedScalar *)interp_1d_pinv, collo_grad_1d, Q_1d, Q_1d, P_1d));
3327a982d89SJeremy L. Thompson 
3332247a93fSRezgar Shakeri   CeedCall(CeedFree(&interp_1d_pinv));
334e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3357a982d89SJeremy L. Thompson }
3367a982d89SJeremy L. Thompson 
3377a982d89SJeremy L. Thompson /**
338b0cc4569SJeremy L Thompson   @brief Return 1D interpolation matrix to Chebyshev polynomial coefficients on quadrature space
339b0cc4569SJeremy L Thompson 
340b0cc4569SJeremy L Thompson   @param[in]  basis               `CeedBasis`
341b0cc4569SJeremy L Thompson   @param[out] chebyshev_interp_1d Row-major (`P_1d * Q_1d`) matrix interpolating from basis nodes to Chebyshev polynomial coefficients
342b0cc4569SJeremy L Thompson 
343b0cc4569SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
344b0cc4569SJeremy L Thompson 
345b0cc4569SJeremy L Thompson   @ref Backend
346b0cc4569SJeremy L Thompson **/
347b0cc4569SJeremy L Thompson int CeedBasisGetChebyshevInterp1D(CeedBasis basis, CeedScalar *chebyshev_interp_1d) {
348b0cc4569SJeremy L Thompson   CeedInt           P_1d, Q_1d;
349b0cc4569SJeremy L Thompson   CeedScalar       *C, *chebyshev_coeffs_1d_inv;
350b0cc4569SJeremy L Thompson   const CeedScalar *interp_1d, *q_ref_1d;
351b0cc4569SJeremy L Thompson   Ceed              ceed;
352b0cc4569SJeremy L Thompson 
353b0cc4569SJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis, &ceed));
354b0cc4569SJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
355b0cc4569SJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
356b0cc4569SJeremy L Thompson 
357b0cc4569SJeremy L Thompson   // Build coefficient matrix
358bd83cbc5SJeremy L Thompson   // -- Note: Clang-tidy needs this check
359bd83cbc5SJeremy L Thompson   CeedCheck((P_1d > 0) && (Q_1d > 0), ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis dimensions are malformed");
360b0cc4569SJeremy L Thompson   CeedCall(CeedCalloc(Q_1d * Q_1d, &C));
361b0cc4569SJeremy L Thompson   CeedCall(CeedBasisGetQRef(basis, &q_ref_1d));
362b0cc4569SJeremy L Thompson   for (CeedInt i = 0; i < Q_1d; i++) CeedCall(CeedChebyshevPolynomialsAtPoint(q_ref_1d[i], Q_1d, &C[i * Q_1d]));
363b0cc4569SJeremy L Thompson 
364b0cc4569SJeremy L Thompson   // Compute C^+, pseudoinverse of coefficient matrix
365b0cc4569SJeremy L Thompson   CeedCall(CeedCalloc(Q_1d * Q_1d, &chebyshev_coeffs_1d_inv));
366b0cc4569SJeremy L Thompson   CeedCall(CeedMatrixPseudoinverse(ceed, C, Q_1d, Q_1d, chebyshev_coeffs_1d_inv));
367b0cc4569SJeremy L Thompson 
368b0cc4569SJeremy L Thompson   // Build mapping from nodes to Chebyshev coefficients
369b0cc4569SJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
370b0cc4569SJeremy L Thompson   CeedCall(CeedMatrixMatrixMultiply(ceed, chebyshev_coeffs_1d_inv, interp_1d, chebyshev_interp_1d, Q_1d, P_1d, Q_1d));
371b0cc4569SJeremy L Thompson 
372b0cc4569SJeremy L Thompson   // Cleanup
373b0cc4569SJeremy L Thompson   CeedCall(CeedFree(&C));
374b0cc4569SJeremy L Thompson   CeedCall(CeedFree(&chebyshev_coeffs_1d_inv));
375b0cc4569SJeremy L Thompson   return CEED_ERROR_SUCCESS;
376b0cc4569SJeremy L Thompson }
377b0cc4569SJeremy L Thompson 
378b0cc4569SJeremy L Thompson /**
379ca94c3ddSJeremy L Thompson   @brief Get tensor status for given `CeedBasis`
3807a982d89SJeremy L. Thompson 
381ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis`
382d1d35e2fSjeremylt   @param[out] is_tensor Variable to store tensor status
3837a982d89SJeremy L. Thompson 
3847a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3857a982d89SJeremy L. Thompson 
3867a982d89SJeremy L. Thompson   @ref Backend
3877a982d89SJeremy L. Thompson **/
388d1d35e2fSjeremylt int CeedBasisIsTensor(CeedBasis basis, bool *is_tensor) {
3896402da51SJeremy L Thompson   *is_tensor = basis->is_tensor_basis;
390e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3917a982d89SJeremy L. Thompson }
3927a982d89SJeremy L. Thompson 
3937a982d89SJeremy L. Thompson /**
394ca94c3ddSJeremy L Thompson   @brief Get backend data of a `CeedBasis`
3957a982d89SJeremy L. Thompson 
396ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
3977a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
3987a982d89SJeremy L. Thompson 
3997a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4007a982d89SJeremy L. Thompson 
4017a982d89SJeremy L. Thompson   @ref Backend
4027a982d89SJeremy L. Thompson **/
403777ff853SJeremy L Thompson int CeedBasisGetData(CeedBasis basis, void *data) {
404777ff853SJeremy L Thompson   *(void **)data = basis->data;
405e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4067a982d89SJeremy L. Thompson }
4077a982d89SJeremy L. Thompson 
4087a982d89SJeremy L. Thompson /**
409ca94c3ddSJeremy L Thompson   @brief Set backend data of a `CeedBasis`
4107a982d89SJeremy L. Thompson 
411ca94c3ddSJeremy L Thompson   @param[in,out] basis  `CeedBasis`
412ea61e9acSJeremy L Thompson   @param[in]     data   Data to set
4137a982d89SJeremy L. Thompson 
4147a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4157a982d89SJeremy L. Thompson 
4167a982d89SJeremy L. Thompson   @ref Backend
4177a982d89SJeremy L. Thompson **/
418777ff853SJeremy L Thompson int CeedBasisSetData(CeedBasis basis, void *data) {
419777ff853SJeremy L Thompson   basis->data = data;
420e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4217a982d89SJeremy L. Thompson }
4227a982d89SJeremy L. Thompson 
4237a982d89SJeremy L. Thompson /**
424ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedBasis`
42534359f16Sjeremylt 
426ca94c3ddSJeremy L Thompson   @param[in,out] basis `CeedBasis` to increment the reference counter
42734359f16Sjeremylt 
42834359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
42934359f16Sjeremylt 
43034359f16Sjeremylt   @ref Backend
43134359f16Sjeremylt **/
4329560d06aSjeremylt int CeedBasisReference(CeedBasis basis) {
43334359f16Sjeremylt   basis->ref_count++;
43434359f16Sjeremylt   return CEED_ERROR_SUCCESS;
43534359f16Sjeremylt }
43634359f16Sjeremylt 
43734359f16Sjeremylt /**
438ca94c3ddSJeremy L Thompson   @brief Get number of Q-vector components for given `CeedBasis`
439c4e3f59bSSebastian Grimberg 
440ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis`
441ca94c3ddSJeremy L Thompson   @param[in]  eval_mode @ref CEED_EVAL_INTERP to use interpolated values,
442ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_GRAD to use gradients,
443ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_DIV to use divergence,
444ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_CURL to use curl
445c4e3f59bSSebastian Grimberg   @param[out] q_comp    Variable to store number of Q-vector components of basis
446c4e3f59bSSebastian Grimberg 
447c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
448c4e3f59bSSebastian Grimberg 
449c4e3f59bSSebastian Grimberg   @ref Backend
450c4e3f59bSSebastian Grimberg **/
451c4e3f59bSSebastian Grimberg int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode, CeedInt *q_comp) {
4521203703bSJeremy L Thompson   CeedInt dim;
4531203703bSJeremy L Thompson 
4541203703bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
455c4e3f59bSSebastian Grimberg   switch (eval_mode) {
4561203703bSJeremy L Thompson     case CEED_EVAL_INTERP: {
4571203703bSJeremy L Thompson       CeedFESpace fe_space;
4581203703bSJeremy L Thompson 
4591203703bSJeremy L Thompson       CeedCall(CeedBasisGetFESpace(basis, &fe_space));
4601203703bSJeremy L Thompson       *q_comp = (fe_space == CEED_FE_SPACE_H1) ? 1 : dim;
4611203703bSJeremy L Thompson     } break;
462c4e3f59bSSebastian Grimberg     case CEED_EVAL_GRAD:
4631203703bSJeremy L Thompson       *q_comp = dim;
464c4e3f59bSSebastian Grimberg       break;
465c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
466c4e3f59bSSebastian Grimberg       *q_comp = 1;
467c4e3f59bSSebastian Grimberg       break;
468c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
4691203703bSJeremy L Thompson       *q_comp = (dim < 3) ? 1 : dim;
470c4e3f59bSSebastian Grimberg       break;
471c4e3f59bSSebastian Grimberg     case CEED_EVAL_NONE:
472c4e3f59bSSebastian Grimberg     case CEED_EVAL_WEIGHT:
473352a5e7cSSebastian Grimberg       *q_comp = 1;
474c4e3f59bSSebastian Grimberg       break;
475c4e3f59bSSebastian Grimberg   }
476c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
477c4e3f59bSSebastian Grimberg }
478c4e3f59bSSebastian Grimberg 
479c4e3f59bSSebastian Grimberg /**
480ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs required to apply `CeedBasis` in `t_mode` and `eval_mode`
4816e15d496SJeremy L Thompson 
482ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis` to estimate FLOPs for
483ea61e9acSJeremy L Thompson   @param[in]  t_mode    Apply basis or transpose
484ca94c3ddSJeremy L Thompson   @param[in]  eval_mode @ref CeedEvalMode
485ea61e9acSJeremy L Thompson   @param[out] flops     Address of variable to hold FLOPs estimate
4866e15d496SJeremy L Thompson 
4876e15d496SJeremy L Thompson   @ref Backend
4886e15d496SJeremy L Thompson **/
4892b730f8bSJeremy L Thompson int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedSize *flops) {
4906e15d496SJeremy L Thompson   bool is_tensor;
4916e15d496SJeremy L Thompson 
4922b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor));
4936e15d496SJeremy L Thompson   if (is_tensor) {
4946e15d496SJeremy L Thompson     CeedInt dim, num_comp, P_1d, Q_1d;
4951c66c397SJeremy L Thompson 
4962b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis, &dim));
4972b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
4982b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
4992b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
5006e15d496SJeremy L Thompson     if (t_mode == CEED_TRANSPOSE) {
5012b730f8bSJeremy L Thompson       P_1d = Q_1d;
5022b730f8bSJeremy L Thompson       Q_1d = P_1d;
5036e15d496SJeremy L Thompson     }
5046e15d496SJeremy L Thompson     CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim - 1), post = 1;
5056e15d496SJeremy L Thompson     for (CeedInt d = 0; d < dim; d++) {
5066e15d496SJeremy L Thompson       tensor_flops += 2 * pre * P_1d * post * Q_1d;
5076e15d496SJeremy L Thompson       pre /= P_1d;
5086e15d496SJeremy L Thompson       post *= Q_1d;
5096e15d496SJeremy L Thompson     }
5106e15d496SJeremy L Thompson     switch (eval_mode) {
5112b730f8bSJeremy L Thompson       case CEED_EVAL_NONE:
5122b730f8bSJeremy L Thompson         *flops = 0;
5132b730f8bSJeremy L Thompson         break;
5142b730f8bSJeremy L Thompson       case CEED_EVAL_INTERP:
5152b730f8bSJeremy L Thompson         *flops = tensor_flops;
5162b730f8bSJeremy L Thompson         break;
5172b730f8bSJeremy L Thompson       case CEED_EVAL_GRAD:
5182b730f8bSJeremy L Thompson         *flops = tensor_flops * 2;
5192b730f8bSJeremy L Thompson         break;
5206e15d496SJeremy L Thompson       case CEED_EVAL_DIV:
5211203703bSJeremy L Thompson       case CEED_EVAL_CURL: {
5226574a04fSJeremy L Thompson         // LCOV_EXCL_START
5236e536b99SJeremy L Thompson         return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported",
5246e536b99SJeremy L Thompson                          CeedEvalModes[eval_mode]);
5252b730f8bSJeremy L Thompson         break;
5266e15d496SJeremy L Thompson         // LCOV_EXCL_STOP
5271203703bSJeremy L Thompson       }
5282b730f8bSJeremy L Thompson       case CEED_EVAL_WEIGHT:
5292b730f8bSJeremy L Thompson         *flops = dim * CeedIntPow(Q_1d, dim);
5302b730f8bSJeremy L Thompson         break;
5316e15d496SJeremy L Thompson     }
5326e15d496SJeremy L Thompson   } else {
533c4e3f59bSSebastian Grimberg     CeedInt dim, num_comp, q_comp, num_nodes, num_qpts;
5341c66c397SJeremy L Thompson 
5352b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis, &dim));
5362b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
537c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
5382b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
5392b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
5406e15d496SJeremy L Thompson     switch (eval_mode) {
5412b730f8bSJeremy L Thompson       case CEED_EVAL_NONE:
5422b730f8bSJeremy L Thompson         *flops = 0;
5432b730f8bSJeremy L Thompson         break;
5442b730f8bSJeremy L Thompson       case CEED_EVAL_INTERP:
5452b730f8bSJeremy L Thompson       case CEED_EVAL_GRAD:
5462b730f8bSJeremy L Thompson       case CEED_EVAL_DIV:
5472b730f8bSJeremy L Thompson       case CEED_EVAL_CURL:
548c4e3f59bSSebastian Grimberg         *flops = num_nodes * num_qpts * num_comp * q_comp;
5492b730f8bSJeremy L Thompson         break;
5502b730f8bSJeremy L Thompson       case CEED_EVAL_WEIGHT:
5512b730f8bSJeremy L Thompson         *flops = 0;
5522b730f8bSJeremy L Thompson         break;
5536e15d496SJeremy L Thompson     }
5546e15d496SJeremy L Thompson   }
5556e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5566e15d496SJeremy L Thompson }
5576e15d496SJeremy L Thompson 
5586e15d496SJeremy L Thompson /**
559ca94c3ddSJeremy L Thompson   @brief Get `CeedFESpace` for a `CeedBasis`
560c4e3f59bSSebastian Grimberg 
561ca94c3ddSJeremy L Thompson   @param[in]  basis    `CeedBasis`
562ca94c3ddSJeremy L Thompson   @param[out] fe_space Variable to store `CeedFESpace`
563c4e3f59bSSebastian Grimberg 
564c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
565c4e3f59bSSebastian Grimberg 
566c4e3f59bSSebastian Grimberg   @ref Backend
567c4e3f59bSSebastian Grimberg **/
568c4e3f59bSSebastian Grimberg int CeedBasisGetFESpace(CeedBasis basis, CeedFESpace *fe_space) {
569c4e3f59bSSebastian Grimberg   *fe_space = basis->fe_space;
570c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
571c4e3f59bSSebastian Grimberg }
572c4e3f59bSSebastian Grimberg 
573c4e3f59bSSebastian Grimberg /**
574ca94c3ddSJeremy L Thompson   @brief Get dimension for given `CeedElemTopology`
5757a982d89SJeremy L. Thompson 
576ca94c3ddSJeremy L Thompson   @param[in]  topo `CeedElemTopology`
5777a982d89SJeremy L. Thompson   @param[out] dim  Variable to store dimension of topology
5787a982d89SJeremy L. Thompson 
5797a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5807a982d89SJeremy L. Thompson 
5817a982d89SJeremy L. Thompson   @ref Backend
5827a982d89SJeremy L. Thompson **/
5837a982d89SJeremy L. Thompson int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim) {
5847a982d89SJeremy L. Thompson   *dim = (CeedInt)topo >> 16;
585e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5867a982d89SJeremy L. Thompson }
5877a982d89SJeremy L. Thompson 
5887a982d89SJeremy L. Thompson /**
589ca94c3ddSJeremy L Thompson   @brief Get `CeedTensorContract` of a `CeedBasis`
5907a982d89SJeremy L. Thompson 
591ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis`
592ca94c3ddSJeremy L Thompson   @param[out] contract  Variable to store `CeedTensorContract`
5937a982d89SJeremy L. Thompson 
5947a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5957a982d89SJeremy L. Thompson 
5967a982d89SJeremy L. Thompson   @ref Backend
5977a982d89SJeremy L. Thompson **/
5987a982d89SJeremy L. Thompson int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract) {
5997a982d89SJeremy L. Thompson   *contract = basis->contract;
600e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6017a982d89SJeremy L. Thompson }
6027a982d89SJeremy L. Thompson 
6037a982d89SJeremy L. Thompson /**
604ca94c3ddSJeremy L Thompson   @brief Set `CeedTensorContract` of a `CeedBasis`
6057a982d89SJeremy L. Thompson 
606ca94c3ddSJeremy L Thompson   @param[in,out] basis    `CeedBasis`
607ca94c3ddSJeremy L Thompson   @param[in]     contract `CeedTensorContract` to set
6087a982d89SJeremy L. Thompson 
6097a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6107a982d89SJeremy L. Thompson 
6117a982d89SJeremy L. Thompson   @ref Backend
6127a982d89SJeremy L. Thompson **/
61334359f16Sjeremylt int CeedBasisSetTensorContract(CeedBasis basis, CeedTensorContract contract) {
61434359f16Sjeremylt   basis->contract = contract;
6152b730f8bSJeremy L Thompson   CeedCall(CeedTensorContractReference(contract));
616e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6177a982d89SJeremy L. Thompson }
6187a982d89SJeremy L. Thompson 
6197a982d89SJeremy L. Thompson /**
620ca94c3ddSJeremy L Thompson   @brief Return a reference implementation of matrix multiplication \f$C = A B\f$.
621ba59ac12SSebastian Grimberg 
622ca94c3ddSJeremy L Thompson   Note: This is a reference implementation for CPU `CeedScalar` pointers that is not intended for high performance.
6237a982d89SJeremy L. Thompson 
624ca94c3ddSJeremy L Thompson   @param[in]  ceed  `Ceed` context for error handling
625ca94c3ddSJeremy L Thompson   @param[in]  mat_A Row-major matrix `A`
626ca94c3ddSJeremy L Thompson   @param[in]  mat_B Row-major matrix `B`
627ca94c3ddSJeremy L Thompson   @param[out] mat_C Row-major output matrix `C`
628ca94c3ddSJeremy L Thompson   @param[in]  m     Number of rows of `C`
629ca94c3ddSJeremy L Thompson   @param[in]  n     Number of columns of `C`
630ca94c3ddSJeremy L Thompson   @param[in]  kk    Number of columns of `A`/rows of `B`
6317a982d89SJeremy L. Thompson 
6327a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6337a982d89SJeremy L. Thompson 
6347a982d89SJeremy L. Thompson   @ref Utility
6357a982d89SJeremy L. Thompson **/
6362b730f8bSJeremy L Thompson int CeedMatrixMatrixMultiply(Ceed ceed, const CeedScalar *mat_A, const CeedScalar *mat_B, CeedScalar *mat_C, CeedInt m, CeedInt n, CeedInt kk) {
6372b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < m; i++) {
6387a982d89SJeremy L. Thompson     for (CeedInt j = 0; j < n; j++) {
6397a982d89SJeremy L. Thompson       CeedScalar sum = 0;
6401c66c397SJeremy L Thompson 
6412b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < kk; k++) sum += mat_A[k + i * kk] * mat_B[j + k * n];
642d1d35e2fSjeremylt       mat_C[j + i * n] = sum;
6437a982d89SJeremy L. Thompson     }
6442b730f8bSJeremy L Thompson   }
645e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6467a982d89SJeremy L. Thompson }
6477a982d89SJeremy L. Thompson 
648ba59ac12SSebastian Grimberg /**
649ba59ac12SSebastian Grimberg   @brief Return QR Factorization of a matrix
650ba59ac12SSebastian Grimberg 
651ca94c3ddSJeremy L Thompson   @param[in]     ceed `Ceed` context for error handling
652ba59ac12SSebastian Grimberg   @param[in,out] mat  Row-major matrix to be factorized in place
653ca94c3ddSJeremy L Thompson   @param[in,out] tau  Vector of length `m` of scaling factors
654ba59ac12SSebastian Grimberg   @param[in]     m    Number of rows
655ba59ac12SSebastian Grimberg   @param[in]     n    Number of columns
656ba59ac12SSebastian Grimberg 
657ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
658ba59ac12SSebastian Grimberg 
659ba59ac12SSebastian Grimberg   @ref Utility
660ba59ac12SSebastian Grimberg **/
661ba59ac12SSebastian Grimberg int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau, CeedInt m, CeedInt n) {
662ba59ac12SSebastian Grimberg   CeedScalar v[m];
663ba59ac12SSebastian Grimberg 
664ba59ac12SSebastian Grimberg   // Check matrix shape
6656574a04fSJeremy L Thompson   CeedCheck(n <= m, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute QR factorization with n > m");
666ba59ac12SSebastian Grimberg 
667ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
6681c66c397SJeremy L Thompson     CeedScalar sigma = 0.0;
6691c66c397SJeremy L Thompson 
670ba59ac12SSebastian Grimberg     if (i >= m - 1) {  // last row of matrix, no reflection needed
671ba59ac12SSebastian Grimberg       tau[i] = 0.;
672ba59ac12SSebastian Grimberg       break;
673ba59ac12SSebastian Grimberg     }
674ba59ac12SSebastian Grimberg     // Calculate Householder vector, magnitude
675ba59ac12SSebastian Grimberg     v[i] = mat[i + n * i];
676ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) {
677ba59ac12SSebastian Grimberg       v[j] = mat[i + n * j];
678ba59ac12SSebastian Grimberg       sigma += v[j] * v[j];
679ba59ac12SSebastian Grimberg     }
6801c66c397SJeremy L Thompson     const CeedScalar norm = sqrt(v[i] * v[i] + sigma);  // norm of v[i:m]
6811c66c397SJeremy L Thompson     const CeedScalar R_ii = -copysign(norm, v[i]);
6821c66c397SJeremy L Thompson 
683ba59ac12SSebastian Grimberg     v[i] -= R_ii;
684ba59ac12SSebastian Grimberg     // norm of v[i:m] after modification above and scaling below
685ba59ac12SSebastian Grimberg     //   norm = sqrt(v[i]*v[i] + sigma) / v[i];
686ba59ac12SSebastian Grimberg     //   tau = 2 / (norm*norm)
687ba59ac12SSebastian Grimberg     tau[i] = 2 * v[i] * v[i] / (v[i] * v[i] + sigma);
688ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) v[j] /= v[i];
689ba59ac12SSebastian Grimberg 
690ba59ac12SSebastian Grimberg     // Apply Householder reflector to lower right panel
691ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat[i * n + i + 1], &v[i], tau[i], m - i, n - i - 1, n, 1);
692ba59ac12SSebastian Grimberg     // Save v
693ba59ac12SSebastian Grimberg     mat[i + n * i] = R_ii;
694ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) mat[i + n * j] = v[j];
695ba59ac12SSebastian Grimberg   }
696ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
697ba59ac12SSebastian Grimberg }
698ba59ac12SSebastian Grimberg 
699ba59ac12SSebastian Grimberg /**
700ba59ac12SSebastian Grimberg   @brief Apply Householder Q matrix
701ba59ac12SSebastian Grimberg 
702ca94c3ddSJeremy L Thompson   Compute `mat_A = mat_Q mat_A`, where `mat_Q` is \f$m \times m\f$ and `mat_A` is \f$m \times n\f$.
703ba59ac12SSebastian Grimberg 
704ba59ac12SSebastian Grimberg   @param[in,out] mat_A  Matrix to apply Householder Q to, in place
705ba59ac12SSebastian Grimberg   @param[in]     mat_Q  Householder Q matrix
706ba59ac12SSebastian Grimberg   @param[in]     tau    Householder scaling factors
707ba59ac12SSebastian Grimberg   @param[in]     t_mode Transpose mode for application
708ca94c3ddSJeremy L Thompson   @param[in]     m      Number of rows in `A`
709ca94c3ddSJeremy L Thompson   @param[in]     n      Number of columns in `A`
710ca94c3ddSJeremy L Thompson   @param[in]     k      Number of elementary reflectors in Q, `k < m`
711ca94c3ddSJeremy L Thompson   @param[in]     row    Row stride in `A`
712ca94c3ddSJeremy L Thompson   @param[in]     col    Col stride in `A`
713ba59ac12SSebastian Grimberg 
714ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
715ba59ac12SSebastian Grimberg 
716c4e3f59bSSebastian Grimberg   @ref Utility
717ba59ac12SSebastian Grimberg **/
718ba59ac12SSebastian Grimberg int CeedHouseholderApplyQ(CeedScalar *mat_A, const CeedScalar *mat_Q, const CeedScalar *tau, CeedTransposeMode t_mode, CeedInt m, CeedInt n,
719ba59ac12SSebastian Grimberg                           CeedInt k, CeedInt row, CeedInt col) {
720ba59ac12SSebastian Grimberg   CeedScalar *v;
7211c66c397SJeremy L Thompson 
722ba59ac12SSebastian Grimberg   CeedCall(CeedMalloc(m, &v));
723ba59ac12SSebastian Grimberg   for (CeedInt ii = 0; ii < k; ii++) {
724ba59ac12SSebastian Grimberg     CeedInt i = t_mode == CEED_TRANSPOSE ? ii : k - 1 - ii;
725ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) v[j] = mat_Q[j * k + i];
726ba59ac12SSebastian Grimberg     // Apply Householder reflector (I - tau v v^T) collo_grad_1d^T
727ba59ac12SSebastian Grimberg     CeedCall(CeedHouseholderReflect(&mat_A[i * row], &v[i], tau[i], m - i, n, row, col));
728ba59ac12SSebastian Grimberg   }
729ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&v));
730ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
731ba59ac12SSebastian Grimberg }
732ba59ac12SSebastian Grimberg 
733ba59ac12SSebastian Grimberg /**
7342247a93fSRezgar Shakeri   @brief Return pseudoinverse of a matrix
7352247a93fSRezgar Shakeri 
7362247a93fSRezgar Shakeri   @param[in]     ceed      Ceed context for error handling
7372247a93fSRezgar Shakeri   @param[in]     mat       Row-major matrix to compute pseudoinverse of
7382247a93fSRezgar Shakeri   @param[in]     m         Number of rows
7392247a93fSRezgar Shakeri   @param[in]     n         Number of columns
7402247a93fSRezgar Shakeri   @param[out]    mat_pinv  Row-major pseudoinverse matrix
7412247a93fSRezgar Shakeri 
7422247a93fSRezgar Shakeri   @return An error code: 0 - success, otherwise - failure
7432247a93fSRezgar Shakeri 
7442247a93fSRezgar Shakeri   @ref Utility
7452247a93fSRezgar Shakeri **/
7461203703bSJeremy L Thompson int CeedMatrixPseudoinverse(Ceed ceed, const CeedScalar *mat, CeedInt m, CeedInt n, CeedScalar *mat_pinv) {
7472247a93fSRezgar Shakeri   CeedScalar *tau, *I, *mat_copy;
7482247a93fSRezgar Shakeri 
7492247a93fSRezgar Shakeri   CeedCall(CeedCalloc(m, &tau));
7502247a93fSRezgar Shakeri   CeedCall(CeedCalloc(m * m, &I));
7512247a93fSRezgar Shakeri   CeedCall(CeedCalloc(m * n, &mat_copy));
7522247a93fSRezgar Shakeri   memcpy(mat_copy, mat, m * n * sizeof mat[0]);
7532247a93fSRezgar Shakeri 
7542247a93fSRezgar Shakeri   // QR Factorization, mat = Q R
7552247a93fSRezgar Shakeri   CeedCall(CeedQRFactorization(ceed, mat_copy, tau, m, n));
7562247a93fSRezgar Shakeri 
7572247a93fSRezgar Shakeri   // -- Apply Q^T, I = Q^T * I
7582247a93fSRezgar Shakeri   for (CeedInt i = 0; i < m; i++) I[i * m + i] = 1.0;
7592247a93fSRezgar Shakeri   CeedCall(CeedHouseholderApplyQ(I, mat_copy, tau, CEED_TRANSPOSE, m, m, n, m, 1));
7602247a93fSRezgar Shakeri   // -- Apply R_inv, mat_pinv = R_inv * Q^T
7612247a93fSRezgar Shakeri   for (CeedInt j = 0; j < m; j++) {  // Column j
7622247a93fSRezgar Shakeri     mat_pinv[j + m * (n - 1)] = I[j + m * (n - 1)] / mat_copy[n * n - 1];
7632247a93fSRezgar Shakeri     for (CeedInt i = n - 2; i >= 0; i--) {  // Row i
7642247a93fSRezgar Shakeri       mat_pinv[j + m * i] = I[j + m * i];
7652247a93fSRezgar Shakeri       for (CeedInt k = i + 1; k < n; k++) mat_pinv[j + m * i] -= mat_copy[k + n * i] * mat_pinv[j + m * k];
7662247a93fSRezgar Shakeri       mat_pinv[j + m * i] /= mat_copy[i + n * i];
7672247a93fSRezgar Shakeri     }
7682247a93fSRezgar Shakeri   }
7692247a93fSRezgar Shakeri 
7702247a93fSRezgar Shakeri   // Cleanup
7712247a93fSRezgar Shakeri   CeedCall(CeedFree(&I));
7722247a93fSRezgar Shakeri   CeedCall(CeedFree(&tau));
7732247a93fSRezgar Shakeri   CeedCall(CeedFree(&mat_copy));
7742247a93fSRezgar Shakeri   return CEED_ERROR_SUCCESS;
7752247a93fSRezgar Shakeri }
7762247a93fSRezgar Shakeri 
7772247a93fSRezgar Shakeri /**
778ba59ac12SSebastian Grimberg   @brief Return symmetric Schur decomposition of the symmetric matrix mat via symmetric QR factorization
779ba59ac12SSebastian Grimberg 
780ca94c3ddSJeremy L Thompson   @param[in]     ceed   `Ceed` context for error handling
781ba59ac12SSebastian Grimberg   @param[in,out] mat    Row-major matrix to be factorized in place
782ba59ac12SSebastian Grimberg   @param[out]    lambda Vector of length n of eigenvalues
783ba59ac12SSebastian Grimberg   @param[in]     n      Number of rows/columns
784ba59ac12SSebastian Grimberg 
785ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
786ba59ac12SSebastian Grimberg 
787ba59ac12SSebastian Grimberg   @ref Utility
788ba59ac12SSebastian Grimberg **/
7892c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
7902c2ea1dbSJeremy L Thompson int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat, CeedScalar *lambda, CeedInt n) {
791ba59ac12SSebastian Grimberg   // Check bounds for clang-tidy
7926574a04fSJeremy L Thompson   CeedCheck(n > 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute symmetric Schur decomposition of scalars");
793ba59ac12SSebastian Grimberg 
794ba59ac12SSebastian Grimberg   CeedScalar v[n - 1], tau[n - 1], mat_T[n * n];
795ba59ac12SSebastian Grimberg 
796ba59ac12SSebastian Grimberg   // Copy mat to mat_T and set mat to I
797ba59ac12SSebastian Grimberg   memcpy(mat_T, mat, n * n * sizeof(mat[0]));
798ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
799ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < n; j++) mat[j + n * i] = (i == j) ? 1 : 0;
800ba59ac12SSebastian Grimberg   }
801ba59ac12SSebastian Grimberg 
802ba59ac12SSebastian Grimberg   // Reduce to tridiagonal
803ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n - 1; i++) {
804ba59ac12SSebastian Grimberg     // Calculate Householder vector, magnitude
805ba59ac12SSebastian Grimberg     CeedScalar sigma = 0.0;
8061c66c397SJeremy L Thompson 
807ba59ac12SSebastian Grimberg     v[i] = mat_T[i + n * (i + 1)];
808ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) {
809ba59ac12SSebastian Grimberg       v[j] = mat_T[i + n * (j + 1)];
810ba59ac12SSebastian Grimberg       sigma += v[j] * v[j];
811ba59ac12SSebastian Grimberg     }
8121c66c397SJeremy L Thompson     const CeedScalar norm = sqrt(v[i] * v[i] + sigma);  // norm of v[i:n-1]
8131c66c397SJeremy L Thompson     const CeedScalar R_ii = -copysign(norm, v[i]);
8141c66c397SJeremy L Thompson 
815ba59ac12SSebastian Grimberg     v[i] -= R_ii;
816ba59ac12SSebastian Grimberg     // norm of v[i:m] after modification above and scaling below
817ba59ac12SSebastian Grimberg     //   norm = sqrt(v[i]*v[i] + sigma) / v[i];
818ba59ac12SSebastian Grimberg     //   tau = 2 / (norm*norm)
819ba59ac12SSebastian Grimberg     tau[i] = i == n - 2 ? 2 : 2 * v[i] * v[i] / (v[i] * v[i] + sigma);
820ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) v[j] /= v[i];
821ba59ac12SSebastian Grimberg 
822ba59ac12SSebastian Grimberg     // Update sub and super diagonal
823ba59ac12SSebastian Grimberg     for (CeedInt j = i + 2; j < n; j++) {
824ba59ac12SSebastian Grimberg       mat_T[i + n * j] = 0;
825ba59ac12SSebastian Grimberg       mat_T[j + n * i] = 0;
826ba59ac12SSebastian Grimberg     }
827ba59ac12SSebastian Grimberg     // Apply symmetric Householder reflector to lower right panel
828ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1);
829ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), 1, n);
830ba59ac12SSebastian Grimberg 
831ba59ac12SSebastian Grimberg     // Save v
832ba59ac12SSebastian Grimberg     mat_T[i + n * (i + 1)] = R_ii;
833ba59ac12SSebastian Grimberg     mat_T[(i + 1) + n * i] = R_ii;
834ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) {
835ba59ac12SSebastian Grimberg       mat_T[i + n * (j + 1)] = v[j];
836ba59ac12SSebastian Grimberg     }
837ba59ac12SSebastian Grimberg   }
838ba59ac12SSebastian Grimberg   // Backwards accumulation of Q
839ba59ac12SSebastian Grimberg   for (CeedInt i = n - 2; i >= 0; i--) {
840ba59ac12SSebastian Grimberg     if (tau[i] > 0.0) {
841ba59ac12SSebastian Grimberg       v[i] = 1;
842ba59ac12SSebastian Grimberg       for (CeedInt j = i + 1; j < n - 1; j++) {
843ba59ac12SSebastian Grimberg         v[j]                   = mat_T[i + n * (j + 1)];
844ba59ac12SSebastian Grimberg         mat_T[i + n * (j + 1)] = 0;
845ba59ac12SSebastian Grimberg       }
846ba59ac12SSebastian Grimberg       CeedHouseholderReflect(&mat[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1);
847ba59ac12SSebastian Grimberg     }
848ba59ac12SSebastian Grimberg   }
849ba59ac12SSebastian Grimberg 
850ba59ac12SSebastian Grimberg   // Reduce sub and super diagonal
851ba59ac12SSebastian Grimberg   CeedInt    p = 0, q = 0, itr = 0, max_itr = n * n * n * n;
852ba59ac12SSebastian Grimberg   CeedScalar tol = CEED_EPSILON;
853ba59ac12SSebastian Grimberg 
854ba59ac12SSebastian Grimberg   while (itr < max_itr) {
855ba59ac12SSebastian Grimberg     // Update p, q, size of reduced portions of diagonal
856ba59ac12SSebastian Grimberg     p = 0;
857ba59ac12SSebastian Grimberg     q = 0;
858ba59ac12SSebastian Grimberg     for (CeedInt i = n - 2; i >= 0; i--) {
859ba59ac12SSebastian Grimberg       if (fabs(mat_T[i + n * (i + 1)]) < tol) q += 1;
860ba59ac12SSebastian Grimberg       else break;
861ba59ac12SSebastian Grimberg     }
862ba59ac12SSebastian Grimberg     for (CeedInt i = 0; i < n - q - 1; i++) {
863ba59ac12SSebastian Grimberg       if (fabs(mat_T[i + n * (i + 1)]) < tol) p += 1;
864ba59ac12SSebastian Grimberg       else break;
865ba59ac12SSebastian Grimberg     }
866ba59ac12SSebastian Grimberg     if (q == n - 1) break;  // Finished reducing
867ba59ac12SSebastian Grimberg 
868ba59ac12SSebastian Grimberg     // Reduce tridiagonal portion
869ba59ac12SSebastian Grimberg     CeedScalar t_nn = mat_T[(n - 1 - q) + n * (n - 1 - q)], t_nnm1 = mat_T[(n - 2 - q) + n * (n - 1 - q)];
870ba59ac12SSebastian Grimberg     CeedScalar d  = (mat_T[(n - 2 - q) + n * (n - 2 - q)] - t_nn) / 2;
871ba59ac12SSebastian Grimberg     CeedScalar mu = t_nn - t_nnm1 * t_nnm1 / (d + copysign(sqrt(d * d + t_nnm1 * t_nnm1), d));
872ba59ac12SSebastian Grimberg     CeedScalar x  = mat_T[p + n * p] - mu;
873ba59ac12SSebastian Grimberg     CeedScalar z  = mat_T[p + n * (p + 1)];
8741c66c397SJeremy L Thompson 
875ba59ac12SSebastian Grimberg     for (CeedInt k = p; k < n - q - 1; k++) {
876ba59ac12SSebastian Grimberg       // Compute Givens rotation
877ba59ac12SSebastian Grimberg       CeedScalar c = 1, s = 0;
8781c66c397SJeremy L Thompson 
879ba59ac12SSebastian Grimberg       if (fabs(z) > tol) {
880ba59ac12SSebastian Grimberg         if (fabs(z) > fabs(x)) {
8811c66c397SJeremy L Thompson           const CeedScalar tau = -x / z;
8821c66c397SJeremy L Thompson 
8831c66c397SJeremy L Thompson           s = 1 / sqrt(1 + tau * tau);
8841c66c397SJeremy L Thompson           c = s * tau;
885ba59ac12SSebastian Grimberg         } else {
8861c66c397SJeremy L Thompson           const CeedScalar tau = -z / x;
8871c66c397SJeremy L Thompson 
8881c66c397SJeremy L Thompson           c = 1 / sqrt(1 + tau * tau);
8891c66c397SJeremy L Thompson           s = c * tau;
890ba59ac12SSebastian Grimberg         }
891ba59ac12SSebastian Grimberg       }
892ba59ac12SSebastian Grimberg 
893ba59ac12SSebastian Grimberg       // Apply Givens rotation to T
894ba59ac12SSebastian Grimberg       CeedGivensRotation(mat_T, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n);
895ba59ac12SSebastian Grimberg       CeedGivensRotation(mat_T, c, s, CEED_TRANSPOSE, k, k + 1, n, n);
896ba59ac12SSebastian Grimberg 
897ba59ac12SSebastian Grimberg       // Apply Givens rotation to Q
898ba59ac12SSebastian Grimberg       CeedGivensRotation(mat, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n);
899ba59ac12SSebastian Grimberg 
900ba59ac12SSebastian Grimberg       // Update x, z
901ba59ac12SSebastian Grimberg       if (k < n - q - 2) {
902ba59ac12SSebastian Grimberg         x = mat_T[k + n * (k + 1)];
903ba59ac12SSebastian Grimberg         z = mat_T[k + n * (k + 2)];
904ba59ac12SSebastian Grimberg       }
905ba59ac12SSebastian Grimberg     }
906ba59ac12SSebastian Grimberg     itr++;
907ba59ac12SSebastian Grimberg   }
908ba59ac12SSebastian Grimberg 
909ba59ac12SSebastian Grimberg   // Save eigenvalues
910ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) lambda[i] = mat_T[i + n * i];
911ba59ac12SSebastian Grimberg 
912ba59ac12SSebastian Grimberg   // Check convergence
9136574a04fSJeremy L Thompson   CeedCheck(itr < max_itr || q > n, ceed, CEED_ERROR_MINOR, "Symmetric QR failed to converge");
914ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
915ba59ac12SSebastian Grimberg }
9162c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
917ba59ac12SSebastian Grimberg 
918ba59ac12SSebastian Grimberg /**
919ba59ac12SSebastian Grimberg   @brief Return Simultaneous Diagonalization of two matrices.
920ba59ac12SSebastian Grimberg 
921ca94c3ddSJeremy L Thompson   This solves the generalized eigenvalue problem `A x = lambda B x`, where `A` and `B` are symmetric and `B` is positive definite.
922ca94c3ddSJeremy L Thompson   We generate the matrix `X` and vector `Lambda` such that `X^T A X = Lambda` and `X^T B X = I`.
923ca94c3ddSJeremy L Thompson   This is equivalent to the LAPACK routine 'sygv' with `TYPE = 1`.
924ba59ac12SSebastian Grimberg 
925ca94c3ddSJeremy L Thompson   @param[in]  ceed   `Ceed` context for error handling
926ba59ac12SSebastian Grimberg   @param[in]  mat_A  Row-major matrix to be factorized with eigenvalues
927ba59ac12SSebastian Grimberg   @param[in]  mat_B  Row-major matrix to be factorized to identity
928ba59ac12SSebastian Grimberg   @param[out] mat_X  Row-major orthogonal matrix
929ca94c3ddSJeremy L Thompson   @param[out] lambda Vector of length `n` of generalized eigenvalues
930ba59ac12SSebastian Grimberg   @param[in]  n      Number of rows/columns
931ba59ac12SSebastian Grimberg 
932ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
933ba59ac12SSebastian Grimberg 
934ba59ac12SSebastian Grimberg   @ref Utility
935ba59ac12SSebastian Grimberg **/
9362c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
9372c2ea1dbSJeremy L Thompson int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A, CeedScalar *mat_B, CeedScalar *mat_X, CeedScalar *lambda, CeedInt n) {
938ba59ac12SSebastian Grimberg   CeedScalar *mat_C, *mat_G, *vec_D;
9391c66c397SJeremy L Thompson 
940ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n * n, &mat_C));
941ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n * n, &mat_G));
942ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n, &vec_D));
943ba59ac12SSebastian Grimberg 
944ba59ac12SSebastian Grimberg   // Compute B = G D G^T
945ba59ac12SSebastian Grimberg   memcpy(mat_G, mat_B, n * n * sizeof(mat_B[0]));
946ba59ac12SSebastian Grimberg   CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_G, vec_D, n));
947ba59ac12SSebastian Grimberg 
948ba59ac12SSebastian Grimberg   // Sort eigenvalues
949ba59ac12SSebastian Grimberg   for (CeedInt i = n - 1; i >= 0; i--) {
950ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < i; j++) {
951ba59ac12SSebastian Grimberg       if (fabs(vec_D[j]) > fabs(vec_D[j + 1])) {
9521c66c397SJeremy L Thompson         CeedScalarSwap(vec_D[j], vec_D[j + 1]);
9531c66c397SJeremy L Thompson         for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_G[k * n + j], mat_G[k * n + j + 1]);
954ba59ac12SSebastian Grimberg       }
955ba59ac12SSebastian Grimberg     }
956ba59ac12SSebastian Grimberg   }
957ba59ac12SSebastian Grimberg 
958ba59ac12SSebastian Grimberg   // Compute C = (G D^1/2)^-1 A (G D^1/2)^-T
959ba59ac12SSebastian Grimberg   //           = D^-1/2 G^T A G D^-1/2
960ba59ac12SSebastian Grimberg   // -- D = D^-1/2
961ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) vec_D[i] = 1. / sqrt(vec_D[i]);
962ba59ac12SSebastian Grimberg   // -- G = G D^-1/2
963ba59ac12SSebastian Grimberg   // -- C = D^-1/2 G^T
964ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
965ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < n; j++) {
966ba59ac12SSebastian Grimberg       mat_G[i * n + j] *= vec_D[j];
967ba59ac12SSebastian Grimberg       mat_C[j * n + i] = mat_G[i * n + j];
968ba59ac12SSebastian Grimberg     }
969ba59ac12SSebastian Grimberg   }
970ba59ac12SSebastian Grimberg   // -- X = (D^-1/2 G^T) A
971ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_C, (const CeedScalar *)mat_A, mat_X, n, n, n));
972ba59ac12SSebastian Grimberg   // -- C = (D^-1/2 G^T A) (G D^-1/2)
973ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_X, (const CeedScalar *)mat_G, mat_C, n, n, n));
974ba59ac12SSebastian Grimberg 
975ba59ac12SSebastian Grimberg   // Compute Q^T C Q = lambda
976ba59ac12SSebastian Grimberg   CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_C, lambda, n));
977ba59ac12SSebastian Grimberg 
978ba59ac12SSebastian Grimberg   // Sort eigenvalues
979ba59ac12SSebastian Grimberg   for (CeedInt i = n - 1; i >= 0; i--) {
980ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < i; j++) {
981ba59ac12SSebastian Grimberg       if (fabs(lambda[j]) > fabs(lambda[j + 1])) {
9821c66c397SJeremy L Thompson         CeedScalarSwap(lambda[j], lambda[j + 1]);
9831c66c397SJeremy L Thompson         for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_C[k * n + j], mat_C[k * n + j + 1]);
984ba59ac12SSebastian Grimberg       }
985ba59ac12SSebastian Grimberg     }
986ba59ac12SSebastian Grimberg   }
987ba59ac12SSebastian Grimberg 
988ba59ac12SSebastian Grimberg   // Set X = (G D^1/2)^-T Q
989ba59ac12SSebastian Grimberg   //       = G D^-1/2 Q
990ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_G, (const CeedScalar *)mat_C, mat_X, n, n, n));
991ba59ac12SSebastian Grimberg 
992ba59ac12SSebastian Grimberg   // Cleanup
993ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&mat_C));
994ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&mat_G));
995ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&vec_D));
996ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
997ba59ac12SSebastian Grimberg }
9982c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
999ba59ac12SSebastian Grimberg 
10007a982d89SJeremy L. Thompson /// @}
10017a982d89SJeremy L. Thompson 
10027a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
10037a982d89SJeremy L. Thompson /// CeedBasis Public API
10047a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
10057a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser
1006d7b241e6Sjeremylt /// @{
1007d7b241e6Sjeremylt 
1008b11c1e72Sjeremylt /**
1009ca94c3ddSJeremy L Thompson   @brief Create a tensor-product basis for \f$H^1\f$ discretizations
1010b11c1e72Sjeremylt 
1011ca94c3ddSJeremy L Thompson   @param[in]  ceed        `Ceed` object used to create the `CeedBasis`
1012ea61e9acSJeremy L Thompson   @param[in]  dim         Topological dimension
1013ea61e9acSJeremy L Thompson   @param[in]  num_comp    Number of field components (1 for scalar fields)
1014ea61e9acSJeremy L Thompson   @param[in]  P_1d        Number of nodes in one dimension
1015ea61e9acSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
1016ca94c3ddSJeremy L Thompson   @param[in]  interp_1d   Row-major (`Q_1d * P_1d`) matrix expressing the values of nodal basis functions at quadrature points
1017ca94c3ddSJeremy L Thompson   @param[in]  grad_1d     Row-major (`Q_1d * P_1d`) matrix expressing derivatives of nodal basis functions at quadrature points
1018ca94c3ddSJeremy L Thompson   @param[in]  q_ref_1d    Array of length `Q_1d` holding the locations of quadrature points on the 1D reference element `[-1, 1]`
1019ca94c3ddSJeremy L Thompson   @param[in]  q_weight_1d Array of length `Q_1d` holding the quadrature weights on the reference element
1020ca94c3ddSJeremy L Thompson   @param[out] basis       Address of the variable where the newly created `CeedBasis` will be stored
1021b11c1e72Sjeremylt 
1022b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1023dfdf5a53Sjeremylt 
10247a982d89SJeremy L. Thompson   @ref User
1025b11c1e72Sjeremylt **/
10262b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d,
10272b730f8bSJeremy L Thompson                             const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis *basis) {
10285fe0d4faSjeremylt   if (!ceed->BasisCreateTensorH1) {
10295fe0d4faSjeremylt     Ceed delegate;
10306574a04fSJeremy L Thompson 
10312b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
10321ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateTensorH1");
10332b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis));
1034e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
10355fe0d4faSjeremylt   }
1036e15f9bd0SJeremy L Thompson 
1037ca94c3ddSJeremy L Thompson   CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value");
1038ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1039ca94c3ddSJeremy L Thompson   CeedCheck(P_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1040ca94c3ddSJeremy L Thompson   CeedCheck(Q_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1041227444bfSJeremy L Thompson 
10422b730f8bSJeremy L Thompson   CeedElemTopology topo = dim == 1 ? CEED_TOPOLOGY_LINE : dim == 2 ? CEED_TOPOLOGY_QUAD : CEED_TOPOLOGY_HEX;
1043e15f9bd0SJeremy L Thompson 
10442b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1045db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed));
1046d1d35e2fSjeremylt   (*basis)->ref_count       = 1;
10476402da51SJeremy L Thompson   (*basis)->is_tensor_basis = true;
1048d7b241e6Sjeremylt   (*basis)->dim             = dim;
1049d99fa3c5SJeremy L Thompson   (*basis)->topo            = topo;
1050d1d35e2fSjeremylt   (*basis)->num_comp        = num_comp;
1051d1d35e2fSjeremylt   (*basis)->P_1d            = P_1d;
1052d1d35e2fSjeremylt   (*basis)->Q_1d            = Q_1d;
1053d1d35e2fSjeremylt   (*basis)->P               = CeedIntPow(P_1d, dim);
1054d1d35e2fSjeremylt   (*basis)->Q               = CeedIntPow(Q_1d, dim);
1055c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_H1;
10562b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d, &(*basis)->q_ref_1d));
10572b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d, &(*basis)->q_weight_1d));
1058ff3a0f91SJeremy L Thompson   if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d * sizeof(q_ref_1d[0]));
10592b730f8bSJeremy L Thompson   if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, Q_1d * sizeof(q_weight_1d[0]));
10602b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->interp_1d));
10612b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->grad_1d));
10622b730f8bSJeremy L Thompson   if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, Q_1d * P_1d * sizeof(interp_1d[0]));
1063ff3a0f91SJeremy L Thompson   if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d * P_1d * sizeof(grad_1d[0]));
10642b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, *basis));
1065e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1066d7b241e6Sjeremylt }
1067d7b241e6Sjeremylt 
1068b11c1e72Sjeremylt /**
1069ca94c3ddSJeremy L Thompson   @brief Create a tensor-product \f$H^1\f$ Lagrange basis
1070b11c1e72Sjeremylt 
1071ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` object used to create the `CeedBasis`
1072ea61e9acSJeremy L Thompson   @param[in]  dim       Topological dimension of element
1073ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of field components (1 for scalar fields)
1074ea61e9acSJeremy L Thompson   @param[in]  P         Number of Gauss-Lobatto nodes in one dimension.
1075ca94c3ddSJeremy L Thompson                           The polynomial degree of the resulting `Q_k` element is `k = P - 1`.
1076ea61e9acSJeremy L Thompson   @param[in]  Q         Number of quadrature points in one dimension.
1077ca94c3ddSJeremy L Thompson   @param[in]  quad_mode Distribution of the `Q` quadrature points (affects order of accuracy for the quadrature)
1078ca94c3ddSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created `CeedBasis` will be stored
1079b11c1e72Sjeremylt 
1080b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1081dfdf5a53Sjeremylt 
10827a982d89SJeremy L. Thompson   @ref User
1083b11c1e72Sjeremylt **/
10842b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis) {
1085d7b241e6Sjeremylt   // Allocate
1086c8c3fa7dSJeremy L Thompson   int        ierr = CEED_ERROR_SUCCESS;
10872b730f8bSJeremy L Thompson   CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, *q_weight_1d;
10884d537eeaSYohann 
1089ca94c3ddSJeremy L Thompson   CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value");
1090ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1091ca94c3ddSJeremy L Thompson   CeedCheck(P > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1092ca94c3ddSJeremy L Thompson   CeedCheck(Q > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1093227444bfSJeremy L Thompson 
1094e15f9bd0SJeremy L Thompson   // Get Nodes and Weights
10952b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P * Q, &interp_1d));
10962b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P * Q, &grad_1d));
10972b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P, &nodes));
10982b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &q_ref_1d));
10992b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &q_weight_1d));
11002b730f8bSJeremy L Thompson   if (CeedLobattoQuadrature(P, nodes, NULL) != CEED_ERROR_SUCCESS) goto cleanup;
1101d1d35e2fSjeremylt   switch (quad_mode) {
1102d7b241e6Sjeremylt     case CEED_GAUSS:
1103d1d35e2fSjeremylt       ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d);
1104d7b241e6Sjeremylt       break;
1105d7b241e6Sjeremylt     case CEED_GAUSS_LOBATTO:
1106d1d35e2fSjeremylt       ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d);
1107d7b241e6Sjeremylt       break;
1108d7b241e6Sjeremylt   }
11092b730f8bSJeremy L Thompson   if (ierr != CEED_ERROR_SUCCESS) goto cleanup;
1110e15f9bd0SJeremy L Thompson 
1111d7b241e6Sjeremylt   // Build B, D matrix
1112d7b241e6Sjeremylt   // Fornberg, 1998
1113c8c3fa7dSJeremy L Thompson   for (CeedInt i = 0; i < Q; i++) {
1114d7b241e6Sjeremylt     c1                   = 1.0;
1115d1d35e2fSjeremylt     c3                   = nodes[0] - q_ref_1d[i];
1116d1d35e2fSjeremylt     interp_1d[i * P + 0] = 1.0;
1117c8c3fa7dSJeremy L Thompson     for (CeedInt j = 1; j < P; j++) {
1118d7b241e6Sjeremylt       c2 = 1.0;
1119d7b241e6Sjeremylt       c4 = c3;
1120d1d35e2fSjeremylt       c3 = nodes[j] - q_ref_1d[i];
1121c8c3fa7dSJeremy L Thompson       for (CeedInt k = 0; k < j; k++) {
1122d7b241e6Sjeremylt         dx = nodes[j] - nodes[k];
1123d7b241e6Sjeremylt         c2 *= dx;
1124d7b241e6Sjeremylt         if (k == j - 1) {
1125d1d35e2fSjeremylt           grad_1d[i * P + j]   = c1 * (interp_1d[i * P + k] - c4 * grad_1d[i * P + k]) / c2;
1126d1d35e2fSjeremylt           interp_1d[i * P + j] = -c1 * c4 * interp_1d[i * P + k] / c2;
1127d7b241e6Sjeremylt         }
1128d1d35e2fSjeremylt         grad_1d[i * P + k]   = (c3 * grad_1d[i * P + k] - interp_1d[i * P + k]) / dx;
1129d1d35e2fSjeremylt         interp_1d[i * P + k] = c3 * interp_1d[i * P + k] / dx;
1130d7b241e6Sjeremylt       }
1131d7b241e6Sjeremylt       c1 = c2;
1132d7b241e6Sjeremylt     }
1133d7b241e6Sjeremylt   }
11349ac7b42eSJeremy L Thompson   // Pass to CeedBasisCreateTensorH1
11352b730f8bSJeremy L Thompson   CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis));
1136e15f9bd0SJeremy L Thompson cleanup:
11372b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_1d));
11382b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_1d));
11392b730f8bSJeremy L Thompson   CeedCall(CeedFree(&nodes));
11402b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref_1d));
11412b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight_1d));
1142e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1143d7b241e6Sjeremylt }
1144d7b241e6Sjeremylt 
1145b11c1e72Sjeremylt /**
1146ca94c3ddSJeremy L Thompson   @brief Create a non tensor-product basis for \f$H^1\f$ discretizations
1147a8de75f0Sjeremylt 
1148ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` object used to create the `CeedBasis`
1149e00f3be8SJames Wright   @param[in]  topo      Topology of element, e.g. hypercube, simplex, etc
1150ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of field components (1 for scalar fields)
1151ea61e9acSJeremy L Thompson   @param[in]  num_nodes Total number of nodes
1152ea61e9acSJeremy L Thompson   @param[in]  num_qpts  Total number of quadrature points
1153ca94c3ddSJeremy L Thompson   @param[in]  interp    Row-major (`num_qpts * num_nodes`) matrix expressing the values of nodal basis functions at quadrature points
1154ca94c3ddSJeremy L Thompson   @param[in]  grad      Row-major (`dim * num_qpts * num_nodes`) matrix expressing derivatives of nodal basis functions at quadrature points
1155ca94c3ddSJeremy L Thompson   @param[in]  q_ref     Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element
1156ca94c3ddSJeremy L Thompson   @param[in]  q_weight  Array of length `num_qpts` holding the quadrature weights on the reference element
1157ca94c3ddSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created `CeedBasis` will be stored
1158a8de75f0Sjeremylt 
1159a8de75f0Sjeremylt   @return An error code: 0 - success, otherwise - failure
1160a8de75f0Sjeremylt 
11617a982d89SJeremy L. Thompson   @ref User
1162a8de75f0Sjeremylt **/
11632b730f8bSJeremy L Thompson int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
11642b730f8bSJeremy L Thompson                       const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
1165d1d35e2fSjeremylt   CeedInt P = num_nodes, Q = num_qpts, dim = 0;
1166a8de75f0Sjeremylt 
11675fe0d4faSjeremylt   if (!ceed->BasisCreateH1) {
11685fe0d4faSjeremylt     Ceed delegate;
11696574a04fSJeremy L Thompson 
11702b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
11711ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateH1");
11722b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis));
1173e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
11745fe0d4faSjeremylt   }
11755fe0d4faSjeremylt 
1176ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1177ca94c3ddSJeremy L Thompson   CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1178ca94c3ddSJeremy L Thompson   CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1179227444bfSJeremy L Thompson 
11802b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1181a8de75f0Sjeremylt 
1182db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1183db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed));
1184d1d35e2fSjeremylt   (*basis)->ref_count       = 1;
11856402da51SJeremy L Thompson   (*basis)->is_tensor_basis = false;
1186a8de75f0Sjeremylt   (*basis)->dim             = dim;
1187d99fa3c5SJeremy L Thompson   (*basis)->topo            = topo;
1188d1d35e2fSjeremylt   (*basis)->num_comp        = num_comp;
1189a8de75f0Sjeremylt   (*basis)->P               = P;
1190a8de75f0Sjeremylt   (*basis)->Q               = Q;
1191c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_H1;
11922b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q * dim, &(*basis)->q_ref_1d));
11932b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &(*basis)->q_weight_1d));
1194ff3a0f91SJeremy L Thompson   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
1195ff3a0f91SJeremy L Thompson   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
11962b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q * P, &(*basis)->interp));
11972b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(dim * Q * P, &(*basis)->grad));
1198ff3a0f91SJeremy L Thompson   if (interp) memcpy((*basis)->interp, interp, Q * P * sizeof(interp[0]));
1199ff3a0f91SJeremy L Thompson   if (grad) memcpy((*basis)->grad, grad, dim * Q * P * sizeof(grad[0]));
12002b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, *basis));
1201e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1202a8de75f0Sjeremylt }
1203a8de75f0Sjeremylt 
1204a8de75f0Sjeremylt /**
1205859c15bbSJames Wright   @brief Create a non tensor-product basis for \f$H(\mathrm{div})\f$ discretizations
120650c301a5SRezgar Shakeri 
1207ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` object used to create the `CeedBasis`
1208ea61e9acSJeremy L Thompson   @param[in]  topo      Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below
1209ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of components (usually 1 for vectors in H(div) bases)
1210ca94c3ddSJeremy L Thompson   @param[in]  num_nodes Total number of nodes (DoFs per element)
1211ea61e9acSJeremy L Thompson   @param[in]  num_qpts  Total number of quadrature points
1212ca94c3ddSJeremy L Thompson   @param[in]  interp    Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points
1213ca94c3ddSJeremy L Thompson   @param[in]  div       Row-major (`num_qpts * num_nodes`) matrix expressing divergence of basis functions at quadrature points
1214ca94c3ddSJeremy L Thompson   @param[in]  q_ref     Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element
1215ca94c3ddSJeremy L Thompson   @param[in]  q_weight  Array of length `num_qpts` holding the quadrature weights on the reference element
1216ca94c3ddSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created `CeedBasis` will be stored
121750c301a5SRezgar Shakeri 
121850c301a5SRezgar Shakeri   @return An error code: 0 - success, otherwise - failure
121950c301a5SRezgar Shakeri 
122050c301a5SRezgar Shakeri   @ref User
122150c301a5SRezgar Shakeri **/
12222b730f8bSJeremy L Thompson int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
12232b730f8bSJeremy L Thompson                         const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
122450c301a5SRezgar Shakeri   CeedInt Q = num_qpts, P = num_nodes, dim = 0;
1225c4e3f59bSSebastian Grimberg 
122650c301a5SRezgar Shakeri   if (!ceed->BasisCreateHdiv) {
122750c301a5SRezgar Shakeri     Ceed delegate;
12286574a04fSJeremy L Thompson 
12292b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
12306574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv");
12312b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis));
123250c301a5SRezgar Shakeri     return CEED_ERROR_SUCCESS;
123350c301a5SRezgar Shakeri   }
123450c301a5SRezgar Shakeri 
1235ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1236ca94c3ddSJeremy L Thompson   CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1237ca94c3ddSJeremy L Thompson   CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1238227444bfSJeremy L Thompson 
1239c4e3f59bSSebastian Grimberg   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1240c4e3f59bSSebastian Grimberg 
1241db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1242db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed));
124350c301a5SRezgar Shakeri   (*basis)->ref_count       = 1;
12446402da51SJeremy L Thompson   (*basis)->is_tensor_basis = false;
124550c301a5SRezgar Shakeri   (*basis)->dim             = dim;
124650c301a5SRezgar Shakeri   (*basis)->topo            = topo;
124750c301a5SRezgar Shakeri   (*basis)->num_comp        = num_comp;
124850c301a5SRezgar Shakeri   (*basis)->P               = P;
124950c301a5SRezgar Shakeri   (*basis)->Q               = Q;
1250c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_HDIV;
12512b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d));
12522b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d));
125350c301a5SRezgar Shakeri   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
125450c301a5SRezgar Shakeri   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
12552b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp));
12562b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q * P, &(*basis)->div));
125750c301a5SRezgar Shakeri   if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0]));
125850c301a5SRezgar Shakeri   if (div) memcpy((*basis)->div, div, Q * P * sizeof(div[0]));
12592b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, q_weight, *basis));
126050c301a5SRezgar Shakeri   return CEED_ERROR_SUCCESS;
126150c301a5SRezgar Shakeri }
126250c301a5SRezgar Shakeri 
126350c301a5SRezgar Shakeri /**
12644385fb7fSSebastian Grimberg   @brief Create a non tensor-product basis for \f$H(\mathrm{curl})\f$ discretizations
1265c4e3f59bSSebastian Grimberg 
1266ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` object used to create the `CeedBasis`
1267c4e3f59bSSebastian Grimberg   @param[in]  topo      Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below
1268ca94c3ddSJeremy L Thompson   @param[in]  num_comp  Number of components (usually 1 for vectors in \f$H(\mathrm{curl})\f$ bases)
1269ca94c3ddSJeremy L Thompson   @param[in]  num_nodes Total number of nodes (DoFs per element)
1270c4e3f59bSSebastian Grimberg   @param[in]  num_qpts  Total number of quadrature points
1271ca94c3ddSJeremy L Thompson   @param[in]  interp    Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points
1272ca94c3ddSJeremy L Thompson   @param[in]  curl      Row-major (`curl_comp * num_qpts * num_nodes`, `curl_comp = 1` if `dim < 3` otherwise `curl_comp = dim`) matrix expressing curl of basis functions at quadrature points
1273ca94c3ddSJeremy L Thompson   @param[in]  q_ref     Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element
1274ca94c3ddSJeremy L Thompson   @param[in]  q_weight  Array of length `num_qpts` holding the quadrature weights on the reference element
1275ca94c3ddSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created `CeedBasis` will be stored
1276c4e3f59bSSebastian Grimberg 
1277c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1278c4e3f59bSSebastian Grimberg 
1279c4e3f59bSSebastian Grimberg   @ref User
1280c4e3f59bSSebastian Grimberg **/
1281c4e3f59bSSebastian Grimberg int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
1282c4e3f59bSSebastian Grimberg                          const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
1283c4e3f59bSSebastian Grimberg   CeedInt Q = num_qpts, P = num_nodes, dim = 0, curl_comp = 0;
1284c4e3f59bSSebastian Grimberg 
1285d075f50bSSebastian Grimberg   if (!ceed->BasisCreateHcurl) {
1286c4e3f59bSSebastian Grimberg     Ceed delegate;
12876574a04fSJeremy L Thompson 
1288c4e3f59bSSebastian Grimberg     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
12896574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl");
1290c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis));
1291c4e3f59bSSebastian Grimberg     return CEED_ERROR_SUCCESS;
1292c4e3f59bSSebastian Grimberg   }
1293c4e3f59bSSebastian Grimberg 
1294ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1295ca94c3ddSJeremy L Thompson   CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1296ca94c3ddSJeremy L Thompson   CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1297c4e3f59bSSebastian Grimberg 
1298c4e3f59bSSebastian Grimberg   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1299c4e3f59bSSebastian Grimberg   curl_comp = (dim < 3) ? 1 : dim;
1300c4e3f59bSSebastian Grimberg 
1301db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1302db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed));
1303c4e3f59bSSebastian Grimberg   (*basis)->ref_count       = 1;
13046402da51SJeremy L Thompson   (*basis)->is_tensor_basis = false;
1305c4e3f59bSSebastian Grimberg   (*basis)->dim             = dim;
1306c4e3f59bSSebastian Grimberg   (*basis)->topo            = topo;
1307c4e3f59bSSebastian Grimberg   (*basis)->num_comp        = num_comp;
1308c4e3f59bSSebastian Grimberg   (*basis)->P               = P;
1309c4e3f59bSSebastian Grimberg   (*basis)->Q               = Q;
1310c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_HCURL;
1311c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d));
1312c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d));
1313c4e3f59bSSebastian Grimberg   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
1314c4e3f59bSSebastian Grimberg   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
1315c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp));
1316c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(curl_comp * Q * P, &(*basis)->curl));
1317c4e3f59bSSebastian Grimberg   if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0]));
1318c4e3f59bSSebastian Grimberg   if (curl) memcpy((*basis)->curl, curl, curl_comp * Q * P * sizeof(curl[0]));
1319c4e3f59bSSebastian Grimberg   CeedCall(ceed->BasisCreateHcurl(topo, dim, P, Q, interp, curl, q_ref, q_weight, *basis));
1320c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1321c4e3f59bSSebastian Grimberg }
1322c4e3f59bSSebastian Grimberg 
1323c4e3f59bSSebastian Grimberg /**
1324ca94c3ddSJeremy L Thompson   @brief Create a `CeedBasis` for projection from the nodes of `basis_from` to the nodes of `basis_to`.
1325ba59ac12SSebastian Grimberg 
1326ca94c3ddSJeremy L Thompson   Only @ref CEED_EVAL_INTERP will be valid for the new basis, `basis_project`.
1327ca94c3ddSJeremy L Thompson   For \f$H^1\f$ spaces, @ref CEED_EVAL_GRAD will also be valid.
1328ca94c3ddSJeremy L Thompson   The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization.
1329ca94c3ddSJeremy L Thompson   The gradient (for the \f$H^1\f$ case) is given by `grad_project = interp_to^+ * grad_from`.
133015ad3917SSebastian Grimberg 
133115ad3917SSebastian Grimberg   Note: `basis_from` and `basis_to` must have compatible quadrature spaces.
133215ad3917SSebastian Grimberg 
13339fd66db6SSebastian Grimberg   Note: `basis_project` will have the same number of components as `basis_from`, regardless of the number of components that `basis_to` has.
13349fd66db6SSebastian Grimberg         If `basis_from` has 3 components and `basis_to` has 5 components, then `basis_project` will have 3 components.
1335f113e5dcSJeremy L Thompson 
1336e104ad11SJames Wright   Note: If either `basis_from` or `basis_to` are non-tensor, then `basis_project` will also be non-tensor
1337e104ad11SJames Wright 
1338ca94c3ddSJeremy L Thompson   @param[in]  basis_from    `CeedBasis` to prolong from
1339ca94c3ddSJeremy L Thompson   @param[in]  basis_to      `CeedBasis` to prolong to
1340ca94c3ddSJeremy L Thompson   @param[out] basis_project Address of the variable where the newly created `CeedBasis` will be stored
1341f113e5dcSJeremy L Thompson 
1342f113e5dcSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1343f113e5dcSJeremy L Thompson 
1344f113e5dcSJeremy L Thompson   @ref User
1345f113e5dcSJeremy L Thompson **/
13462b730f8bSJeremy L Thompson int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project) {
1347f113e5dcSJeremy L Thompson   Ceed        ceed;
1348e104ad11SJames Wright   bool        create_tensor;
13491c66c397SJeremy L Thompson   CeedInt     dim, num_comp;
1350097cc795SJames Wright   CeedScalar *interp_project, *grad_project;
13511c66c397SJeremy L Thompson 
13522b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis_to, &ceed));
1353f113e5dcSJeremy L Thompson 
1354ecc88aebSJeremy L Thompson   // Create projection matrix
13552b730f8bSJeremy L Thompson   CeedCall(CeedBasisCreateProjectionMatrices(basis_from, basis_to, &interp_project, &grad_project));
1356f113e5dcSJeremy L Thompson 
1357f113e5dcSJeremy L Thompson   // Build basis
1358e104ad11SJames Wright   {
1359e104ad11SJames Wright     bool is_tensor_to, is_tensor_from;
1360e104ad11SJames Wright 
1361e104ad11SJames Wright     CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to));
1362e104ad11SJames Wright     CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from));
1363e104ad11SJames Wright     create_tensor = is_tensor_from && is_tensor_to;
1364e104ad11SJames Wright   }
13652b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis_to, &dim));
13662b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_from, &num_comp));
1367e104ad11SJames Wright   if (create_tensor) {
1368f113e5dcSJeremy L Thompson     CeedInt P_1d_to, P_1d_from;
13691c66c397SJeremy L Thompson 
13702b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_1d_from));
13712b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_1d_to));
1372097cc795SJames Wright     CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, interp_project, grad_project, NULL, NULL, basis_project));
1373f113e5dcSJeremy L Thompson   } else {
1374de05fbb2SSebastian Grimberg     // Even if basis_to and basis_from are not H1, the resulting basis is H1 for interpolation to work
1375f113e5dcSJeremy L Thompson     CeedInt          num_nodes_to, num_nodes_from;
13761c66c397SJeremy L Thompson     CeedElemTopology topo;
13771c66c397SJeremy L Thompson 
1378e00f3be8SJames Wright     CeedCall(CeedBasisGetTopology(basis_from, &topo));
13792b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_from, &num_nodes_from));
13802b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_to, &num_nodes_to));
1381097cc795SJames Wright     CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, NULL, NULL, basis_project));
1382f113e5dcSJeremy L Thompson   }
1383f113e5dcSJeremy L Thompson 
1384f113e5dcSJeremy L Thompson   // Cleanup
13852b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_project));
13862b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_project));
1387f113e5dcSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1388f113e5dcSJeremy L Thompson }
1389f113e5dcSJeremy L Thompson 
1390f113e5dcSJeremy L Thompson /**
1391ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedBasis`.
13929560d06aSjeremylt 
1393ca94c3ddSJeremy L Thompson   Note: If the value of `*basis_copy` passed into this function is non-`NULL`, then it is assumed that `*basis_copy` is a pointer to a `CeedBasis`.
1394ca94c3ddSJeremy L Thompson         This `CeedBasis` will be destroyed if `*basis_copy` is the only reference to this `CeedBasis`.
1395ea61e9acSJeremy L Thompson 
1396ca94c3ddSJeremy L Thompson   @param[in]     basis      `CeedBasis` to copy reference to
1397ea61e9acSJeremy L Thompson   @param[in,out] basis_copy Variable to store copied reference
13989560d06aSjeremylt 
13999560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
14009560d06aSjeremylt 
14019560d06aSjeremylt   @ref User
14029560d06aSjeremylt **/
14039560d06aSjeremylt int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) {
1404356036faSJeremy L Thompson   if (basis != CEED_BASIS_NONE) CeedCall(CeedBasisReference(basis));
14052b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(basis_copy));
14069560d06aSjeremylt   *basis_copy = basis;
14079560d06aSjeremylt   return CEED_ERROR_SUCCESS;
14089560d06aSjeremylt }
14099560d06aSjeremylt 
14109560d06aSjeremylt /**
1411ca94c3ddSJeremy L Thompson   @brief View a `CeedBasis`
14127a982d89SJeremy L. Thompson 
1413ca94c3ddSJeremy L Thompson   @param[in] basis  `CeedBasis` to view
1414ca94c3ddSJeremy L Thompson   @param[in] stream Stream to view to, e.g., `stdout`
14157a982d89SJeremy L. Thompson 
14167a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
14177a982d89SJeremy L. Thompson 
14187a982d89SJeremy L. Thompson   @ref User
14197a982d89SJeremy L. Thompson **/
14207a982d89SJeremy L. Thompson int CeedBasisView(CeedBasis basis, FILE *stream) {
14211203703bSJeremy L Thompson   bool             is_tensor_basis;
14221203703bSJeremy L Thompson   CeedElemTopology topo;
14231203703bSJeremy L Thompson   CeedFESpace      fe_space;
14241203703bSJeremy L Thompson 
14251203703bSJeremy L Thompson   // Basis data
14261203703bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
14271203703bSJeremy L Thompson   CeedCall(CeedBasisGetTopology(basis, &topo));
14281203703bSJeremy L Thompson   CeedCall(CeedBasisGetFESpace(basis, &fe_space));
14292b730f8bSJeremy L Thompson 
143050c301a5SRezgar Shakeri   // Print FE space and element topology of the basis
1431edf04919SJeremy L Thompson   fprintf(stream, "CeedBasis in a %s on a %s element\n", CeedFESpaces[fe_space], CeedElemTopologies[topo]);
14321203703bSJeremy L Thompson   if (is_tensor_basis) {
1433edf04919SJeremy L Thompson     fprintf(stream, "  P: %" CeedInt_FMT "\n  Q: %" CeedInt_FMT "\n", basis->P_1d, basis->Q_1d);
143450c301a5SRezgar Shakeri   } else {
1435edf04919SJeremy L Thompson     fprintf(stream, "  P: %" CeedInt_FMT "\n  Q: %" CeedInt_FMT "\n", basis->P, basis->Q);
143650c301a5SRezgar Shakeri   }
1437edf04919SJeremy L Thompson   fprintf(stream, "  dimension: %" CeedInt_FMT "\n  field components: %" CeedInt_FMT "\n", basis->dim, basis->num_comp);
1438ea61e9acSJeremy L Thompson   // Print quadrature data, interpolation/gradient/divergence/curl of the basis
14391203703bSJeremy L Thompson   if (is_tensor_basis) {  // tensor basis
14401203703bSJeremy L Thompson     CeedInt           P_1d, Q_1d;
14411203703bSJeremy L Thompson     const CeedScalar *q_ref_1d, *q_weight_1d, *interp_1d, *grad_1d;
14421203703bSJeremy L Thompson 
14431203703bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
14441203703bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
14451203703bSJeremy L Thompson     CeedCall(CeedBasisGetQRef(basis, &q_ref_1d));
14461203703bSJeremy L Thompson     CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d));
14471203703bSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
14481203703bSJeremy L Thompson     CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
14491203703bSJeremy L Thompson 
14501203703bSJeremy L Thompson     CeedCall(CeedScalarView("qref1d", "\t% 12.8f", 1, Q_1d, q_ref_1d, stream));
14511203703bSJeremy L Thompson     CeedCall(CeedScalarView("qweight1d", "\t% 12.8f", 1, Q_1d, q_weight_1d, stream));
14521203703bSJeremy L Thompson     CeedCall(CeedScalarView("interp1d", "\t% 12.8f", Q_1d, P_1d, interp_1d, stream));
14531203703bSJeremy L Thompson     CeedCall(CeedScalarView("grad1d", "\t% 12.8f", Q_1d, P_1d, grad_1d, stream));
145450c301a5SRezgar Shakeri   } else {  // non-tensor basis
14551203703bSJeremy L Thompson     CeedInt           P, Q, dim, q_comp;
14561203703bSJeremy L Thompson     const CeedScalar *q_ref, *q_weight, *interp, *grad, *div, *curl;
14571203703bSJeremy L Thompson 
14581203703bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis, &P));
14591203703bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(basis, &Q));
14601203703bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis, &dim));
14611203703bSJeremy L Thompson     CeedCall(CeedBasisGetQRef(basis, &q_ref));
14621203703bSJeremy L Thompson     CeedCall(CeedBasisGetQWeights(basis, &q_weight));
14631203703bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(basis, &interp));
14641203703bSJeremy L Thompson     CeedCall(CeedBasisGetGrad(basis, &grad));
14651203703bSJeremy L Thompson     CeedCall(CeedBasisGetDiv(basis, &div));
14661203703bSJeremy L Thompson     CeedCall(CeedBasisGetCurl(basis, &curl));
14671203703bSJeremy L Thompson 
14681203703bSJeremy L Thompson     CeedCall(CeedScalarView("qref", "\t% 12.8f", 1, Q * dim, q_ref, stream));
14691203703bSJeremy L Thompson     CeedCall(CeedScalarView("qweight", "\t% 12.8f", 1, Q, q_weight, stream));
1470c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp));
14711203703bSJeremy L Thompson     CeedCall(CeedScalarView("interp", "\t% 12.8f", q_comp * Q, P, interp, stream));
14721203703bSJeremy L Thompson     if (grad) {
1473c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp));
14741203703bSJeremy L Thompson       CeedCall(CeedScalarView("grad", "\t% 12.8f", q_comp * Q, P, grad, stream));
14757a982d89SJeremy L. Thompson     }
14761203703bSJeremy L Thompson     if (div) {
1477c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp));
14781203703bSJeremy L Thompson       CeedCall(CeedScalarView("div", "\t% 12.8f", q_comp * Q, P, div, stream));
1479c4e3f59bSSebastian Grimberg     }
14801203703bSJeremy L Thompson     if (curl) {
1481c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp));
14821203703bSJeremy L Thompson       CeedCall(CeedScalarView("curl", "\t% 12.8f", q_comp * Q, P, curl, stream));
148350c301a5SRezgar Shakeri     }
148450c301a5SRezgar Shakeri   }
1485e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
14867a982d89SJeremy L. Thompson }
14877a982d89SJeremy L. Thompson 
14887a982d89SJeremy L. Thompson /**
1489*db2becc9SJeremy L Thompson   @brief Check input vector dimensions for CeedBasisApply[Add]
14907a982d89SJeremy L. Thompson 
1491ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis` to evaluate
1492ea61e9acSJeremy L Thompson   @param[in]  num_elem  The number of elements to apply the basis evaluation to;
1493ca94c3ddSJeremy L Thompson                           the backend will specify the ordering in @ref CeedElemRestrictionCreate()
1494ca94c3ddSJeremy L Thompson   @param[in]  t_mode    @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points;
1495ca94c3ddSJeremy L Thompson                           @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes
1496ca94c3ddSJeremy L Thompson   @param[in]  eval_mode @ref CEED_EVAL_NONE to use values directly,
1497ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_INTERP to use interpolated values,
1498ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_GRAD to use gradients,
1499ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_DIV to use divergence,
1500ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_CURL to use curl,
1501ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_WEIGHT to use quadrature weights
1502ca94c3ddSJeremy L Thompson   @param[in]  u         Input `CeedVector`
1503ca94c3ddSJeremy L Thompson   @param[out] v         Output `CeedVector`
15047a982d89SJeremy L. Thompson 
15057a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
15067a982d89SJeremy L. Thompson 
1507*db2becc9SJeremy L Thompson   @ref Developer
15087a982d89SJeremy L. Thompson **/
1509*db2becc9SJeremy L Thompson static int CeedBasisApplyCheckDims(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) {
1510c4e3f59bSSebastian Grimberg   CeedInt  dim, num_comp, q_comp, num_nodes, num_qpts;
15111c66c397SJeremy L Thompson   CeedSize u_length = 0, v_length;
15121203703bSJeremy L Thompson   Ceed     ceed;
15131c66c397SJeremy L Thompson 
15141203703bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis, &ceed));
15152b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
15162b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
1517c4e3f59bSSebastian Grimberg   CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
15182b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
15192b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
15202b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(v, &v_length));
1521c8c3fa7dSJeremy L Thompson   if (u) CeedCall(CeedVectorGetLength(u, &u_length));
15227a982d89SJeremy L. Thompson 
1523e15f9bd0SJeremy L Thompson   // Check compatibility of topological and geometrical dimensions
15246574a04fSJeremy L Thompson   CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0 && u_length % num_qpts == 0) ||
15256574a04fSJeremy L Thompson                 (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0 && v_length % num_qpts == 0),
15261203703bSJeremy L Thompson             ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions");
15277a982d89SJeremy L. Thompson 
1528e15f9bd0SJeremy L Thompson   // Check vector lengths to prevent out of bounds issues
152999e754f0SJeremy L Thompson   bool has_good_dims = true;
1530d1d35e2fSjeremylt   switch (eval_mode) {
1531e15f9bd0SJeremy L Thompson     case CEED_EVAL_NONE:
15322b730f8bSJeremy L Thompson     case CEED_EVAL_INTERP:
15332b730f8bSJeremy L Thompson     case CEED_EVAL_GRAD:
1534c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
1535c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
153699e754f0SJeremy L Thompson       has_good_dims =
15376574a04fSJeremy L Thompson           ((t_mode == CEED_TRANSPOSE && u_length >= num_elem * num_comp * num_qpts * q_comp && v_length >= num_elem * num_comp * num_nodes) ||
15386574a04fSJeremy L Thompson            (t_mode == CEED_NOTRANSPOSE && v_length >= num_elem * num_qpts * num_comp * q_comp && u_length >= num_elem * num_comp * num_nodes));
1539e15f9bd0SJeremy L Thompson       break;
1540e15f9bd0SJeremy L Thompson     case CEED_EVAL_WEIGHT:
154199e754f0SJeremy L Thompson       has_good_dims = v_length >= num_elem * num_qpts;
1542e15f9bd0SJeremy L Thompson       break;
1543e15f9bd0SJeremy L Thompson   }
154499e754f0SJeremy L Thompson   CeedCheck(has_good_dims, ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode");
1545*db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1546*db2becc9SJeremy L Thompson }
1547e15f9bd0SJeremy L Thompson 
1548*db2becc9SJeremy L Thompson /**
1549*db2becc9SJeremy L Thompson   @brief Apply basis evaluation from nodes to quadrature points or vice versa
1550*db2becc9SJeremy L Thompson 
1551*db2becc9SJeremy L Thompson   @param[in]  basis     `CeedBasis` to evaluate
1552*db2becc9SJeremy L Thompson   @param[in]  num_elem  The number of elements to apply the basis evaluation to;
1553*db2becc9SJeremy L Thompson                           the backend will specify the ordering in @ref CeedElemRestrictionCreate()
1554*db2becc9SJeremy L Thompson   @param[in]  t_mode    @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points;
1555*db2becc9SJeremy L Thompson                           @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes
1556*db2becc9SJeremy L Thompson   @param[in]  eval_mode @ref CEED_EVAL_NONE to use values directly,
1557*db2becc9SJeremy L Thompson                           @ref CEED_EVAL_INTERP to use interpolated values,
1558*db2becc9SJeremy L Thompson                           @ref CEED_EVAL_GRAD to use gradients,
1559*db2becc9SJeremy L Thompson                           @ref CEED_EVAL_DIV to use divergence,
1560*db2becc9SJeremy L Thompson                           @ref CEED_EVAL_CURL to use curl,
1561*db2becc9SJeremy L Thompson                           @ref CEED_EVAL_WEIGHT to use quadrature weights
1562*db2becc9SJeremy L Thompson   @param[in]  u         Input `CeedVector`
1563*db2becc9SJeremy L Thompson   @param[out] v         Output `CeedVector`
1564*db2becc9SJeremy L Thompson 
1565*db2becc9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1566*db2becc9SJeremy L Thompson 
1567*db2becc9SJeremy L Thompson   @ref User
1568*db2becc9SJeremy L Thompson **/
1569*db2becc9SJeremy L Thompson int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) {
1570*db2becc9SJeremy L Thompson   CeedCall(CeedBasisApplyCheckDims(basis, num_elem, t_mode, eval_mode, u, v));
1571*db2becc9SJeremy L Thompson   CeedCheck(basis->Apply, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedBasisApply");
15722b730f8bSJeremy L Thompson   CeedCall(basis->Apply(basis, num_elem, t_mode, eval_mode, u, v));
1573e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
15747a982d89SJeremy L. Thompson }
15757a982d89SJeremy L. Thompson 
15767a982d89SJeremy L. Thompson /**
1577*db2becc9SJeremy L Thompson   @brief Apply basis evaluation from quadrature points to nodes and sum into target vector
1578*db2becc9SJeremy L Thompson 
1579*db2becc9SJeremy L Thompson   @param[in]  basis     `CeedBasis` to evaluate
1580*db2becc9SJeremy L Thompson   @param[in]  num_elem  The number of elements to apply the basis evaluation to;
1581*db2becc9SJeremy L Thompson                           the backend will specify the ordering in @ref CeedElemRestrictionCreate()
1582*db2becc9SJeremy L Thompson   @param[in]  t_mode    @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes;
1583*db2becc9SJeremy L Thompson                            @ref CEED_NOTRANSPOSE is not valid for `CeedBasisApplyAdd()`
1584*db2becc9SJeremy L Thompson   @param[in]  eval_mode @ref CEED_EVAL_NONE to use values directly,
1585*db2becc9SJeremy L Thompson                           @ref CEED_EVAL_INTERP to use interpolated values,
1586*db2becc9SJeremy L Thompson                           @ref CEED_EVAL_GRAD to use gradients,
1587*db2becc9SJeremy L Thompson                           @ref CEED_EVAL_DIV to use divergence,
1588*db2becc9SJeremy L Thompson                           @ref CEED_EVAL_CURL to use curl,
1589*db2becc9SJeremy L Thompson                           @ref CEED_EVAL_WEIGHT to use quadrature weights
1590*db2becc9SJeremy L Thompson   @param[in]  u         Input `CeedVector`
1591*db2becc9SJeremy L Thompson   @param[out] v         Output `CeedVector` to sum into
1592*db2becc9SJeremy L Thompson 
1593*db2becc9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1594*db2becc9SJeremy L Thompson 
1595*db2becc9SJeremy L Thompson   @ref User
1596*db2becc9SJeremy L Thompson **/
1597*db2becc9SJeremy L Thompson int CeedBasisApplyAdd(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) {
1598*db2becc9SJeremy L Thompson   CeedCheck(t_mode == CEED_TRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "CeedBasisApplyAdd only supports CEED_TRANSPOSE");
1599*db2becc9SJeremy L Thompson   CeedCall(CeedBasisApplyCheckDims(basis, num_elem, t_mode, eval_mode, u, v));
1600*db2becc9SJeremy L Thompson   CeedCheck(basis->ApplyAdd, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedBasisApplyAdd");
1601*db2becc9SJeremy L Thompson   CeedCall(basis->ApplyAdd(basis, num_elem, t_mode, eval_mode, u, v));
1602*db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1603*db2becc9SJeremy L Thompson }
1604*db2becc9SJeremy L Thompson 
1605*db2becc9SJeremy L Thompson /**
1606*db2becc9SJeremy L Thompson   @brief Check input vector dimensions for CeedBasisApply[Add]AtPoints
1607c8c3fa7dSJeremy L Thompson 
1608ca94c3ddSJeremy L Thompson   @param[in]  basis      `CeedBasis` to evaluate
1609fc0f7cc6SJeremy L Thompson   @param[in]  num_elem   The number of elements to apply the basis evaluation to;
1610fc0f7cc6SJeremy L Thompson                           the backend will specify the ordering in @ref CeedElemRestrictionCreate()
1611faed4840SJeremy L Thompson   @param[in]  num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem`
1612ca94c3ddSJeremy L Thompson   @param[in]  t_mode     @ref CEED_NOTRANSPOSE to evaluate from nodes to points;
1613ca94c3ddSJeremy L Thompson                            @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes
1614ca94c3ddSJeremy L Thompson   @param[in]  eval_mode  @ref CEED_EVAL_INTERP to use interpolated values,
1615ca94c3ddSJeremy L Thompson                            @ref CEED_EVAL_GRAD to use gradients,
1616ca94c3ddSJeremy L Thompson                            @ref CEED_EVAL_WEIGHT to use quadrature weights
1617ca94c3ddSJeremy L Thompson   @param[in]  x_ref      `CeedVector` holding reference coordinates of each point
1618ca94c3ddSJeremy L Thompson   @param[in]  u          Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE
1619ca94c3ddSJeremy L Thompson   @param[out] v          Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP
1620c8c3fa7dSJeremy L Thompson 
1621c8c3fa7dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1622c8c3fa7dSJeremy L Thompson 
1623*db2becc9SJeremy L Thompson   @ref Developer
1624c8c3fa7dSJeremy L Thompson **/
1625*db2becc9SJeremy L Thompson static int CeedBasisApplyAtPointsCheckDims(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode,
1626*db2becc9SJeremy L Thompson                                            CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) {
1627fc0f7cc6SJeremy L Thompson   CeedInt  dim, num_comp, num_q_comp, num_nodes, P_1d = 1, Q_1d = 1, total_num_points = 0;
16281c66c397SJeremy L Thompson   CeedSize x_length = 0, u_length = 0, v_length;
16291203703bSJeremy L Thompson   Ceed     ceed;
1630c8c3fa7dSJeremy L Thompson 
16311203703bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis, &ceed));
1632c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
1633c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
1634c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
1635c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
1636c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &num_q_comp));
1637c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
1638c8c3fa7dSJeremy L Thompson   CeedCall(CeedVectorGetLength(v, &v_length));
1639953190f4SJeremy L Thompson   if (x_ref != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(x_ref, &x_length));
1640953190f4SJeremy L Thompson   if (u != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(u, &u_length));
1641c8c3fa7dSJeremy L Thompson 
1642c8c3fa7dSJeremy L Thompson   // Check compatibility of topological and geometrical dimensions
1643fc0f7cc6SJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) total_num_points += num_points[i];
1644953190f4SJeremy L Thompson   CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0) || (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0) ||
1645953190f4SJeremy L Thompson                 (eval_mode == CEED_EVAL_WEIGHT),
16461203703bSJeremy L Thompson             ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions and number of points");
1647c8c3fa7dSJeremy L Thompson 
1648c8c3fa7dSJeremy L Thompson   // Check compatibility coordinates vector
1649fc0f7cc6SJeremy L Thompson   CeedCheck((x_length >= total_num_points * dim) || (eval_mode == CEED_EVAL_WEIGHT), ceed, CEED_ERROR_DIMENSION,
1650c8c3fa7dSJeremy L Thompson             "Length of reference coordinate vector incompatible with basis dimension and number of points");
1651c8c3fa7dSJeremy L Thompson 
1652953190f4SJeremy L Thompson   // Check CEED_EVAL_WEIGHT only on CEED_NOTRANSPOSE
16531203703bSJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT || t_mode == CEED_NOTRANSPOSE, ceed, CEED_ERROR_UNSUPPORTED,
1654953190f4SJeremy L Thompson             "CEED_EVAL_WEIGHT only supported with CEED_NOTRANSPOSE");
1655953190f4SJeremy L Thompson 
1656c8c3fa7dSJeremy L Thompson   // Check vector lengths to prevent out of bounds issues
165799e754f0SJeremy L Thompson   bool has_good_dims = true;
1658c8c3fa7dSJeremy L Thompson   switch (eval_mode) {
1659c8c3fa7dSJeremy L Thompson     case CEED_EVAL_INTERP:
1660fc0f7cc6SJeremy L Thompson       has_good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= total_num_points * num_q_comp || v_length >= num_elem * num_nodes * num_comp)) ||
1661fc0f7cc6SJeremy L Thompson                        (t_mode == CEED_NOTRANSPOSE && (v_length >= total_num_points * num_q_comp || u_length >= num_elem * num_nodes * num_comp)));
1662c8c3fa7dSJeremy L Thompson       break;
1663c8c3fa7dSJeremy L Thompson     case CEED_EVAL_GRAD:
1664fc0f7cc6SJeremy L Thompson       has_good_dims =
1665fc0f7cc6SJeremy L Thompson           ((t_mode == CEED_TRANSPOSE && (u_length >= total_num_points * num_q_comp * dim || v_length >= num_elem * num_nodes * num_comp)) ||
1666fc0f7cc6SJeremy L Thompson            (t_mode == CEED_NOTRANSPOSE && (v_length >= total_num_points * num_q_comp * dim || u_length >= num_elem * num_nodes * num_comp)));
1667edfbf3a6SJeremy L Thompson       break;
1668c8c3fa7dSJeremy L Thompson     case CEED_EVAL_WEIGHT:
1669fc0f7cc6SJeremy L Thompson       has_good_dims = t_mode == CEED_NOTRANSPOSE && (v_length >= total_num_points);
1670953190f4SJeremy L Thompson       break;
167199e754f0SJeremy L Thompson       // LCOV_EXCL_START
1672953190f4SJeremy L Thompson     case CEED_EVAL_NONE:
1673c8c3fa7dSJeremy L Thompson     case CEED_EVAL_DIV:
1674c8c3fa7dSJeremy L Thompson     case CEED_EVAL_CURL:
16751203703bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points not supported for %s", CeedEvalModes[eval_mode]);
1676c8c3fa7dSJeremy L Thompson       // LCOV_EXCL_STOP
1677c8c3fa7dSJeremy L Thompson   }
167899e754f0SJeremy L Thompson   CeedCheck(has_good_dims, ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode");
1679c8c3fa7dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1680c8c3fa7dSJeremy L Thompson }
1681c8c3fa7dSJeremy L Thompson 
1682*db2becc9SJeremy L Thompson /**
1683*db2becc9SJeremy L Thompson   @brief Default implimentation to apply basis evaluation from nodes to arbitrary points
1684*db2becc9SJeremy L Thompson 
1685*db2becc9SJeremy L Thompson   @param[in]  basis      `CeedBasis` to evaluate
1686*db2becc9SJeremy L Thompson   @param[in]  apply_add  Sum result into target vector or overwrite
1687*db2becc9SJeremy L Thompson   @param[in]  num_elem   The number of elements to apply the basis evaluation to;
1688*db2becc9SJeremy L Thompson                           the backend will specify the ordering in @ref CeedElemRestrictionCreate()
1689*db2becc9SJeremy L Thompson   @param[in]  num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem`
1690*db2becc9SJeremy L Thompson   @param[in]  t_mode     @ref CEED_NOTRANSPOSE to evaluate from nodes to points;
1691*db2becc9SJeremy L Thompson                            @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes
1692*db2becc9SJeremy L Thompson   @param[in]  eval_mode  @ref CEED_EVAL_INTERP to use interpolated values,
1693*db2becc9SJeremy L Thompson                            @ref CEED_EVAL_GRAD to use gradients,
1694*db2becc9SJeremy L Thompson                            @ref CEED_EVAL_WEIGHT to use quadrature weights
1695*db2becc9SJeremy L Thompson   @param[in]  x_ref      `CeedVector` holding reference coordinates of each point
1696*db2becc9SJeremy L Thompson   @param[in]  u          Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE
1697*db2becc9SJeremy L Thompson   @param[out] v          Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP
1698*db2becc9SJeremy L Thompson 
1699*db2becc9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1700*db2becc9SJeremy L Thompson 
1701*db2becc9SJeremy L Thompson   @ref Developer
1702*db2becc9SJeremy L Thompson **/
1703*db2becc9SJeremy L Thompson static int CeedBasisApplyAtPoints_Core(CeedBasis basis, bool apply_add, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode,
1704*db2becc9SJeremy L Thompson                                        CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) {
1705*db2becc9SJeremy L Thompson   CeedInt dim, num_comp, P_1d = 1, Q_1d = 1, total_num_points = num_points[0];
1706*db2becc9SJeremy L Thompson   Ceed    ceed;
1707*db2becc9SJeremy L Thompson 
1708*db2becc9SJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis, &ceed));
1709*db2becc9SJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
1710*db2becc9SJeremy L Thompson   // Inserting check because clang-tidy doesn't understand this cannot occur
1711*db2becc9SJeremy L Thompson   CeedCheck(dim > 0, ceed, CEED_ERROR_UNSUPPORTED, "Malformed CeedBasis, dim > 0 is required");
1712*db2becc9SJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
1713*db2becc9SJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
1714*db2becc9SJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
1715*db2becc9SJeremy L Thompson 
1716c8c3fa7dSJeremy L Thompson   // Default implementation
1717*db2becc9SJeremy L Thompson   {
1718*db2becc9SJeremy L Thompson     bool is_tensor_basis;
1719*db2becc9SJeremy L Thompson 
17201203703bSJeremy L Thompson     CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
17211203703bSJeremy L Thompson     CeedCheck(is_tensor_basis, ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points only supported for tensor product bases");
1722*db2becc9SJeremy L Thompson   }
1723fc0f7cc6SJeremy L Thompson   CeedCheck(num_elem == 1, ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary  points only supported for a single element at a time");
1724953190f4SJeremy L Thompson   if (eval_mode == CEED_EVAL_WEIGHT) {
1725953190f4SJeremy L Thompson     CeedCall(CeedVectorSetValue(v, 1.0));
1726953190f4SJeremy L Thompson     return CEED_ERROR_SUCCESS;
1727953190f4SJeremy L Thompson   }
1728c8c3fa7dSJeremy L Thompson   if (!basis->basis_chebyshev) {
1729c8c3fa7dSJeremy L Thompson     // Build basis mapping from nodes to Chebyshev coefficients
1730c8c3fa7dSJeremy L Thompson     CeedScalar       *chebyshev_interp_1d, *chebyshev_grad_1d, *chebyshev_q_weight_1d;
1731b0cc4569SJeremy L Thompson     const CeedScalar *q_ref_1d;
1732c8c3fa7dSJeremy L Thompson 
173371a83b88SJeremy L Thompson     CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
173471a83b88SJeremy L Thompson     CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_grad_1d));
1735c8c3fa7dSJeremy L Thompson     CeedCall(CeedCalloc(Q_1d, &chebyshev_q_weight_1d));
1736b0cc4569SJeremy L Thompson     CeedCall(CeedBasisGetQRef(basis, &q_ref_1d));
1737b0cc4569SJeremy L Thompson     CeedCall(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
1738c8c3fa7dSJeremy L Thompson 
17391203703bSJeremy L Thompson     CeedCall(CeedVectorCreate(ceed, num_comp * CeedIntPow(Q_1d, dim), &basis->vec_chebyshev));
17401203703bSJeremy 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,
1741c8c3fa7dSJeremy L Thompson                                      &basis->basis_chebyshev));
1742c8c3fa7dSJeremy L Thompson 
1743c8c3fa7dSJeremy L Thompson     // Cleanup
1744c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&chebyshev_interp_1d));
1745c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&chebyshev_grad_1d));
1746c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&chebyshev_q_weight_1d));
1747c8c3fa7dSJeremy L Thompson   }
1748c8c3fa7dSJeremy L Thompson 
1749c8c3fa7dSJeremy L Thompson   // Create TensorContract object if needed, such as a basis from the GPU backends
1750c8c3fa7dSJeremy L Thompson   if (!basis->contract) {
1751c8c3fa7dSJeremy L Thompson     Ceed      ceed_ref;
1752585a562dSJeremy L Thompson     CeedBasis basis_ref = NULL;
1753c8c3fa7dSJeremy L Thompson 
1754c8c3fa7dSJeremy L Thompson     CeedCall(CeedInit("/cpu/self", &ceed_ref));
1755c8c3fa7dSJeremy L Thompson     // Only need matching tensor contraction dimensions, any type of basis will work
175671a83b88SJeremy L Thompson     CeedCall(CeedBasisCreateTensorH1Lagrange(ceed_ref, dim, num_comp, P_1d, Q_1d, CEED_GAUSS, &basis_ref));
1757585a562dSJeremy L Thompson     // Note - clang-tidy doesn't know basis_ref->contract must be valid here
17581203703bSJeremy L Thompson     CeedCheck(basis_ref && basis_ref->contract, ceed, CEED_ERROR_UNSUPPORTED, "Reference CPU ceed failed to create a tensor contraction object");
1759585a562dSJeremy L Thompson     CeedCall(CeedTensorContractReferenceCopy(basis_ref->contract, &basis->contract));
1760c8c3fa7dSJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis_ref));
1761c8c3fa7dSJeremy L Thompson     CeedCall(CeedDestroy(&ceed_ref));
1762c8c3fa7dSJeremy L Thompson   }
1763c8c3fa7dSJeremy L Thompson 
1764c8c3fa7dSJeremy L Thompson   // Basis evaluation
1765c8c3fa7dSJeremy L Thompson   switch (t_mode) {
1766c8c3fa7dSJeremy L Thompson     case CEED_NOTRANSPOSE: {
1767c8c3fa7dSJeremy L Thompson       // Nodes to arbitrary points
1768c8c3fa7dSJeremy L Thompson       CeedScalar       *v_array;
1769c8c3fa7dSJeremy L Thompson       const CeedScalar *chebyshev_coeffs, *x_array_read;
1770c8c3fa7dSJeremy L Thompson 
1771c8c3fa7dSJeremy L Thompson       // -- Interpolate to Chebyshev coefficients
1772c8c3fa7dSJeremy L Thompson       CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, basis->vec_chebyshev));
1773c8c3fa7dSJeremy L Thompson 
1774c8c3fa7dSJeremy L Thompson       // -- Evaluate Chebyshev polynomials at arbitrary points
1775c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs));
1776c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read));
1777c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorGetArrayWrite(v, CEED_MEM_HOST, &v_array));
1778edfbf3a6SJeremy L Thompson       switch (eval_mode) {
1779edfbf3a6SJeremy L Thompson         case CEED_EVAL_INTERP: {
1780c8c3fa7dSJeremy L Thompson           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
1781c8c3fa7dSJeremy L Thompson 
1782c8c3fa7dSJeremy L Thompson           // ---- Values at point
1783fc0f7cc6SJeremy L Thompson           for (CeedInt p = 0; p < total_num_points; p++) {
1784c8c3fa7dSJeremy L Thompson             CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1;
1785c8c3fa7dSJeremy L Thompson 
178653ef2869SZach Atkins             for (CeedInt d = 0; d < dim; d++) {
17873778dbaaSJeremy L Thompson               // ------ Tensor contract with current Chebyshev polynomial values
1788fc0f7cc6SJeremy L Thompson               CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x));
1789c8c3fa7dSJeremy L Thompson               CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false,
17904608bdaaSJeremy L Thompson                                                d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2]));
1791c8c3fa7dSJeremy L Thompson               pre /= Q_1d;
1792c8c3fa7dSJeremy L Thompson               post *= 1;
1793c8c3fa7dSJeremy L Thompson             }
1794fc0f7cc6SJeremy L Thompson             for (CeedInt c = 0; c < num_comp; c++) v_array[c * total_num_points + p] = tmp[dim % 2][c];
1795c8c3fa7dSJeremy L Thompson           }
1796edfbf3a6SJeremy L Thompson           break;
1797edfbf3a6SJeremy L Thompson         }
1798edfbf3a6SJeremy L Thompson         case CEED_EVAL_GRAD: {
1799edfbf3a6SJeremy L Thompson           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
1800edfbf3a6SJeremy L Thompson 
1801edfbf3a6SJeremy L Thompson           // ---- Values at point
1802fc0f7cc6SJeremy L Thompson           for (CeedInt p = 0; p < total_num_points; p++) {
1803edfbf3a6SJeremy L Thompson             // Dim**2 contractions, apply grad when pass == dim
180453ef2869SZach Atkins             for (CeedInt pass = 0; pass < dim; pass++) {
1805edfbf3a6SJeremy L Thompson               CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1;
1806edfbf3a6SJeremy L Thompson 
180753ef2869SZach Atkins               for (CeedInt d = 0; d < dim; d++) {
18083778dbaaSJeremy L Thompson                 // ------ Tensor contract with current Chebyshev polynomial values
1809fc0f7cc6SJeremy L Thompson                 if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x));
1810fc0f7cc6SJeremy L Thompson                 else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x));
1811edfbf3a6SJeremy L Thompson                 CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false,
18124608bdaaSJeremy L Thompson                                                  d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2]));
1813edfbf3a6SJeremy L Thompson                 pre /= Q_1d;
1814edfbf3a6SJeremy L Thompson                 post *= 1;
1815edfbf3a6SJeremy L Thompson               }
1816fc0f7cc6SJeremy L Thompson               for (CeedInt c = 0; c < num_comp; c++) v_array[(pass * num_comp + c) * total_num_points + p] = tmp[dim % 2][c];
1817edfbf3a6SJeremy L Thompson             }
1818edfbf3a6SJeremy L Thompson           }
1819edfbf3a6SJeremy L Thompson           break;
1820edfbf3a6SJeremy L Thompson         }
1821edfbf3a6SJeremy L Thompson         default:
1822953190f4SJeremy L Thompson           // Nothing to do, excluded above
1823edfbf3a6SJeremy L Thompson           break;
1824c8c3fa7dSJeremy L Thompson       }
1825c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(basis->vec_chebyshev, &chebyshev_coeffs));
1826c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read));
1827c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorRestoreArray(v, &v_array));
1828c8c3fa7dSJeremy L Thompson       break;
1829c8c3fa7dSJeremy L Thompson     }
18302a94f45fSJeremy L Thompson     case CEED_TRANSPOSE: {
18313778dbaaSJeremy L Thompson       // Note: No switch on e_mode here because only CEED_EVAL_INTERP is supported at this time
18322a94f45fSJeremy L Thompson       // Arbitrary points to nodes
18332a94f45fSJeremy L Thompson       CeedScalar       *chebyshev_coeffs;
18342a94f45fSJeremy L Thompson       const CeedScalar *u_array, *x_array_read;
18352a94f45fSJeremy L Thompson 
18361c66c397SJeremy L Thompson       // -- Transpose of evaluation of Chebyshev polynomials at arbitrary points
18372a94f45fSJeremy L Thompson       CeedCall(CeedVectorGetArrayWrite(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs));
18382a94f45fSJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read));
18392a94f45fSJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array));
1840038a8942SZach Atkins 
1841038a8942SZach Atkins       switch (eval_mode) {
1842038a8942SZach Atkins         case CEED_EVAL_INTERP: {
18432a94f45fSJeremy L Thompson           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
18442a94f45fSJeremy L Thompson 
18452a94f45fSJeremy L Thompson           // ---- Values at point
1846fc0f7cc6SJeremy L Thompson           for (CeedInt p = 0; p < total_num_points; p++) {
18472a94f45fSJeremy L Thompson             CeedInt pre = num_comp * 1, post = 1;
18482a94f45fSJeremy L Thompson 
1849fc0f7cc6SJeremy L Thompson             for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[c * total_num_points + p];
185053ef2869SZach Atkins             for (CeedInt d = 0; d < dim; d++) {
18513778dbaaSJeremy L Thompson               // ------ Tensor contract with current Chebyshev polynomial values
1852fc0f7cc6SJeremy L Thompson               CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x));
18534608bdaaSJeremy L Thompson               CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, p > 0 && d == (dim - 1), tmp[d % 2],
18544608bdaaSJeremy L Thompson                                                d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2]));
18552a94f45fSJeremy L Thompson               pre /= 1;
18562a94f45fSJeremy L Thompson               post *= Q_1d;
18572a94f45fSJeremy L Thompson             }
18582a94f45fSJeremy L Thompson           }
1859038a8942SZach Atkins           break;
1860038a8942SZach Atkins         }
1861038a8942SZach Atkins         case CEED_EVAL_GRAD: {
1862038a8942SZach Atkins           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
1863038a8942SZach Atkins 
1864038a8942SZach Atkins           // ---- Values at point
1865fc0f7cc6SJeremy L Thompson           for (CeedInt p = 0; p < total_num_points; p++) {
1866038a8942SZach Atkins             // Dim**2 contractions, apply grad when pass == dim
1867038a8942SZach Atkins             for (CeedInt pass = 0; pass < dim; pass++) {
1868038a8942SZach Atkins               CeedInt pre = num_comp * 1, post = 1;
1869038a8942SZach Atkins 
1870fc0f7cc6SJeremy L Thompson               for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[(pass * num_comp + c) * total_num_points + p];
1871038a8942SZach Atkins               for (CeedInt d = 0; d < dim; d++) {
1872038a8942SZach Atkins                 // ------ Tensor contract with current Chebyshev polynomial values
1873fc0f7cc6SJeremy L Thompson                 if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x));
1874fc0f7cc6SJeremy L Thompson                 else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x));
18754608bdaaSJeremy L Thompson                 CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode,
18764608bdaaSJeremy L Thompson                                                  (p > 0 || (p == 0 && pass > 0)) && d == (dim - 1), tmp[d % 2],
18774608bdaaSJeremy L Thompson                                                  d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2]));
1878038a8942SZach Atkins                 pre /= 1;
1879038a8942SZach Atkins                 post *= Q_1d;
1880038a8942SZach Atkins               }
1881038a8942SZach Atkins             }
1882038a8942SZach Atkins           }
1883038a8942SZach Atkins           break;
1884038a8942SZach Atkins         }
1885038a8942SZach Atkins         default:
1886038a8942SZach Atkins           // Nothing to do, excluded above
1887038a8942SZach Atkins           break;
18882a94f45fSJeremy L Thompson       }
18892a94f45fSJeremy L Thompson       CeedCall(CeedVectorRestoreArray(basis->vec_chebyshev, &chebyshev_coeffs));
18902a94f45fSJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read));
18912a94f45fSJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(u, &u_array));
18922a94f45fSJeremy L Thompson 
18932a94f45fSJeremy L Thompson       // -- Interpolate transpose from Chebyshev coefficients
1894*db2becc9SJeremy L Thompson       if (apply_add) CeedCall(CeedBasisApplyAdd(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v));
1895*db2becc9SJeremy L Thompson       else CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v));
18962a94f45fSJeremy L Thompson       break;
18972a94f45fSJeremy L Thompson     }
1898c8c3fa7dSJeremy L Thompson   }
1899c8c3fa7dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1900c8c3fa7dSJeremy L Thompson }
1901c8c3fa7dSJeremy L Thompson 
1902c8c3fa7dSJeremy L Thompson /**
1903*db2becc9SJeremy L Thompson   @brief Apply basis evaluation from nodes to arbitrary points
1904*db2becc9SJeremy L Thompson 
1905*db2becc9SJeremy L Thompson   @param[in]  basis      `CeedBasis` to evaluate
1906*db2becc9SJeremy L Thompson   @param[in]  num_elem   The number of elements to apply the basis evaluation to;
1907*db2becc9SJeremy L Thompson                           the backend will specify the ordering in @ref CeedElemRestrictionCreate()
1908*db2becc9SJeremy L Thompson   @param[in]  num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem`
1909*db2becc9SJeremy L Thompson   @param[in]  t_mode     @ref CEED_NOTRANSPOSE to evaluate from nodes to points;
1910*db2becc9SJeremy L Thompson                            @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes
1911*db2becc9SJeremy L Thompson   @param[in]  eval_mode  @ref CEED_EVAL_INTERP to use interpolated values,
1912*db2becc9SJeremy L Thompson                            @ref CEED_EVAL_GRAD to use gradients,
1913*db2becc9SJeremy L Thompson                            @ref CEED_EVAL_WEIGHT to use quadrature weights
1914*db2becc9SJeremy L Thompson   @param[in]  x_ref      `CeedVector` holding reference coordinates of each point
1915*db2becc9SJeremy L Thompson   @param[in]  u          Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE
1916*db2becc9SJeremy L Thompson   @param[out] v          Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP
1917*db2becc9SJeremy L Thompson 
1918*db2becc9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1919*db2becc9SJeremy L Thompson 
1920*db2becc9SJeremy L Thompson   @ref User
1921*db2becc9SJeremy L Thompson **/
1922*db2becc9SJeremy L Thompson int CeedBasisApplyAtPoints(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode,
1923*db2becc9SJeremy L Thompson                            CeedVector x_ref, CeedVector u, CeedVector v) {
1924*db2becc9SJeremy L Thompson   CeedCall(CeedBasisApplyAtPointsCheckDims(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
1925*db2becc9SJeremy L Thompson   if (basis->ApplyAtPoints) {
1926*db2becc9SJeremy L Thompson     CeedCall(basis->ApplyAtPoints(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
1927*db2becc9SJeremy L Thompson   } else {
1928*db2becc9SJeremy L Thompson     CeedCall(CeedBasisApplyAtPoints_Core(basis, false, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
1929*db2becc9SJeremy L Thompson   }
1930*db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1931*db2becc9SJeremy L Thompson }
1932*db2becc9SJeremy L Thompson 
1933*db2becc9SJeremy L Thompson /**
1934*db2becc9SJeremy L Thompson   @brief Apply basis evaluation from nodes to arbitrary points and sum into target vector
1935*db2becc9SJeremy L Thompson 
1936*db2becc9SJeremy L Thompson   @param[in]  basis      `CeedBasis` to evaluate
1937*db2becc9SJeremy L Thompson   @param[in]  num_elem   The number of elements to apply the basis evaluation to;
1938*db2becc9SJeremy L Thompson                           the backend will specify the ordering in @ref CeedElemRestrictionCreate()
1939*db2becc9SJeremy L Thompson   @param[in]  num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem`
1940*db2becc9SJeremy L Thompson   @param[in]  t_mode     @ref CEED_NOTRANSPOSE to evaluate from nodes to points;
1941*db2becc9SJeremy L Thompson                            @ref CEED_NOTRANSPOSE is not valid for `CeedBasisApplyAddAtPoints()`
1942*db2becc9SJeremy L Thompson   @param[in]  eval_mode  @ref CEED_EVAL_INTERP to use interpolated values,
1943*db2becc9SJeremy L Thompson                            @ref CEED_EVAL_GRAD to use gradients,
1944*db2becc9SJeremy L Thompson                            @ref CEED_EVAL_WEIGHT to use quadrature weights
1945*db2becc9SJeremy L Thompson   @param[in]  x_ref      `CeedVector` holding reference coordinates of each point
1946*db2becc9SJeremy L Thompson   @param[in]  u          Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE
1947*db2becc9SJeremy L Thompson   @param[out] v          Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP
1948*db2becc9SJeremy L Thompson 
1949*db2becc9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1950*db2becc9SJeremy L Thompson 
1951*db2becc9SJeremy L Thompson   @ref User
1952*db2becc9SJeremy L Thompson **/
1953*db2becc9SJeremy L Thompson int CeedBasisApplyAddAtPoints(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode,
1954*db2becc9SJeremy L Thompson                               CeedVector x_ref, CeedVector u, CeedVector v) {
1955*db2becc9SJeremy L Thompson   CeedCheck(t_mode == CEED_TRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "CeedBasisApplyAddAtPoints only supports CEED_TRANSPOSE");
1956*db2becc9SJeremy L Thompson   CeedCall(CeedBasisApplyAtPointsCheckDims(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
1957*db2becc9SJeremy L Thompson   if (basis->ApplyAddAtPoints) {
1958*db2becc9SJeremy L Thompson     CeedCall(basis->ApplyAddAtPoints(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
1959*db2becc9SJeremy L Thompson   } else {
1960*db2becc9SJeremy L Thompson     CeedCall(CeedBasisApplyAtPoints_Core(basis, true, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
1961*db2becc9SJeremy L Thompson   }
1962*db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1963*db2becc9SJeremy L Thompson }
1964*db2becc9SJeremy L Thompson 
1965*db2becc9SJeremy L Thompson /**
19666e536b99SJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedBasis`
1967b7c9bbdaSJeremy L Thompson 
1968ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
1969ca94c3ddSJeremy L Thompson   @param[out] ceed  Variable to store `Ceed`
1970b7c9bbdaSJeremy L Thompson 
1971b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1972b7c9bbdaSJeremy L Thompson 
1973b7c9bbdaSJeremy L Thompson   @ref Advanced
1974b7c9bbdaSJeremy L Thompson **/
1975b7c9bbdaSJeremy L Thompson int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) {
19766e536b99SJeremy L Thompson   *ceed = CeedBasisReturnCeed(basis);
1977b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1978b7c9bbdaSJeremy L Thompson }
1979b7c9bbdaSJeremy L Thompson 
1980b7c9bbdaSJeremy L Thompson /**
19816e536b99SJeremy L Thompson   @brief Return the `Ceed` associated with a `CeedBasis`
19826e536b99SJeremy L Thompson 
19836e536b99SJeremy L Thompson   @param[in]  basis `CeedBasis`
19846e536b99SJeremy L Thompson 
19856e536b99SJeremy L Thompson   @return `Ceed` associated with the `basis`
19866e536b99SJeremy L Thompson 
19876e536b99SJeremy L Thompson   @ref Advanced
19886e536b99SJeremy L Thompson **/
19896e536b99SJeremy L Thompson Ceed CeedBasisReturnCeed(CeedBasis basis) { return basis->ceed; }
19906e536b99SJeremy L Thompson 
19916e536b99SJeremy L Thompson /**
1992ca94c3ddSJeremy L Thompson   @brief Get dimension for given `CeedBasis`
19939d007619Sjeremylt 
1994ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
19959d007619Sjeremylt   @param[out] dim   Variable to store dimension of basis
19969d007619Sjeremylt 
19979d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
19989d007619Sjeremylt 
1999b7c9bbdaSJeremy L Thompson   @ref Advanced
20009d007619Sjeremylt **/
20019d007619Sjeremylt int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) {
20029d007619Sjeremylt   *dim = basis->dim;
2003e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
20049d007619Sjeremylt }
20059d007619Sjeremylt 
20069d007619Sjeremylt /**
2007ca94c3ddSJeremy L Thompson   @brief Get topology for given `CeedBasis`
2008d99fa3c5SJeremy L Thompson 
2009ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
2010d99fa3c5SJeremy L Thompson   @param[out] topo  Variable to store topology of basis
2011d99fa3c5SJeremy L Thompson 
2012d99fa3c5SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2013d99fa3c5SJeremy L Thompson 
2014b7c9bbdaSJeremy L Thompson   @ref Advanced
2015d99fa3c5SJeremy L Thompson **/
2016d99fa3c5SJeremy L Thompson int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) {
2017d99fa3c5SJeremy L Thompson   *topo = basis->topo;
2018e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2019d99fa3c5SJeremy L Thompson }
2020d99fa3c5SJeremy L Thompson 
2021d99fa3c5SJeremy L Thompson /**
2022ca94c3ddSJeremy L Thompson   @brief Get number of components for given `CeedBasis`
20239d007619Sjeremylt 
2024ca94c3ddSJeremy L Thompson   @param[in]  basis    `CeedBasis`
2025ca94c3ddSJeremy L Thompson   @param[out] num_comp Variable to store number of components
20269d007619Sjeremylt 
20279d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
20289d007619Sjeremylt 
2029b7c9bbdaSJeremy L Thompson   @ref Advanced
20309d007619Sjeremylt **/
2031d1d35e2fSjeremylt int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) {
2032d1d35e2fSjeremylt   *num_comp = basis->num_comp;
2033e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
20349d007619Sjeremylt }
20359d007619Sjeremylt 
20369d007619Sjeremylt /**
2037ca94c3ddSJeremy L Thompson   @brief Get total number of nodes (in `dim` dimensions) of a `CeedBasis`
20389d007619Sjeremylt 
2039ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
20409d007619Sjeremylt   @param[out] P     Variable to store number of nodes
20419d007619Sjeremylt 
20429d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
20439d007619Sjeremylt 
20449d007619Sjeremylt   @ref Utility
20459d007619Sjeremylt **/
20469d007619Sjeremylt int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) {
20479d007619Sjeremylt   *P = basis->P;
2048e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
20499d007619Sjeremylt }
20509d007619Sjeremylt 
20519d007619Sjeremylt /**
2052ca94c3ddSJeremy L Thompson   @brief Get total number of nodes (in 1 dimension) of a `CeedBasis`
20539d007619Sjeremylt 
2054ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
2055d1d35e2fSjeremylt   @param[out] P_1d  Variable to store number of nodes
20569d007619Sjeremylt 
20579d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
20589d007619Sjeremylt 
2059b7c9bbdaSJeremy L Thompson   @ref Advanced
20609d007619Sjeremylt **/
2061d1d35e2fSjeremylt int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) {
20626e536b99SJeremy L Thompson   CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply P_1d for non-tensor CeedBasis");
2063d1d35e2fSjeremylt   *P_1d = basis->P_1d;
2064e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
20659d007619Sjeremylt }
20669d007619Sjeremylt 
20679d007619Sjeremylt /**
2068ca94c3ddSJeremy L Thompson   @brief Get total number of quadrature points (in `dim` dimensions) of a `CeedBasis`
20699d007619Sjeremylt 
2070ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
20719d007619Sjeremylt   @param[out] Q     Variable to store number of quadrature points
20729d007619Sjeremylt 
20739d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
20749d007619Sjeremylt 
20759d007619Sjeremylt   @ref Utility
20769d007619Sjeremylt **/
20779d007619Sjeremylt int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) {
20789d007619Sjeremylt   *Q = basis->Q;
2079e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
20809d007619Sjeremylt }
20819d007619Sjeremylt 
20829d007619Sjeremylt /**
2083ca94c3ddSJeremy L Thompson   @brief Get total number of quadrature points (in 1 dimension) of a `CeedBasis`
20849d007619Sjeremylt 
2085ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
2086d1d35e2fSjeremylt   @param[out] Q_1d  Variable to store number of quadrature points
20879d007619Sjeremylt 
20889d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
20899d007619Sjeremylt 
2090b7c9bbdaSJeremy L Thompson   @ref Advanced
20919d007619Sjeremylt **/
2092d1d35e2fSjeremylt int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) {
20936e536b99SJeremy L Thompson   CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply Q_1d for non-tensor CeedBasis");
2094d1d35e2fSjeremylt   *Q_1d = basis->Q_1d;
2095e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
20969d007619Sjeremylt }
20979d007619Sjeremylt 
20989d007619Sjeremylt /**
2099ca94c3ddSJeremy L Thompson   @brief Get reference coordinates of quadrature points (in `dim` dimensions) of a `CeedBasis`
21009d007619Sjeremylt 
2101ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
2102d1d35e2fSjeremylt   @param[out] q_ref Variable to store reference coordinates of quadrature points
21039d007619Sjeremylt 
21049d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
21059d007619Sjeremylt 
2106b7c9bbdaSJeremy L Thompson   @ref Advanced
21079d007619Sjeremylt **/
2108d1d35e2fSjeremylt int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) {
2109d1d35e2fSjeremylt   *q_ref = basis->q_ref_1d;
2110e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
21119d007619Sjeremylt }
21129d007619Sjeremylt 
21139d007619Sjeremylt /**
2114ca94c3ddSJeremy L Thompson   @brief Get quadrature weights of quadrature points (in `dim` dimensions) of a `CeedBasis`
21159d007619Sjeremylt 
2116ca94c3ddSJeremy L Thompson   @param[in]  basis    `CeedBasis`
2117d1d35e2fSjeremylt   @param[out] q_weight Variable to store quadrature weights
21189d007619Sjeremylt 
21199d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
21209d007619Sjeremylt 
2121b7c9bbdaSJeremy L Thompson   @ref Advanced
21229d007619Sjeremylt **/
2123d1d35e2fSjeremylt int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) {
2124d1d35e2fSjeremylt   *q_weight = basis->q_weight_1d;
2125e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
21269d007619Sjeremylt }
21279d007619Sjeremylt 
21289d007619Sjeremylt /**
2129ca94c3ddSJeremy L Thompson   @brief Get interpolation matrix of a `CeedBasis`
21309d007619Sjeremylt 
2131ca94c3ddSJeremy L Thompson   @param[in]  basis  `CeedBasis`
21329d007619Sjeremylt   @param[out] interp Variable to store interpolation matrix
21339d007619Sjeremylt 
21349d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
21359d007619Sjeremylt 
2136b7c9bbdaSJeremy L Thompson   @ref Advanced
21379d007619Sjeremylt **/
21386c58de82SJeremy L Thompson int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) {
21396402da51SJeremy L Thompson   if (!basis->interp && basis->is_tensor_basis) {
21409d007619Sjeremylt     // Allocate
21412b730f8bSJeremy L Thompson     CeedCall(CeedMalloc(basis->Q * basis->P, &basis->interp));
21429d007619Sjeremylt 
21439d007619Sjeremylt     // Initialize
21442b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < basis->Q * basis->P; i++) basis->interp[i] = 1.0;
21459d007619Sjeremylt 
21469d007619Sjeremylt     // Calculate
21472b730f8bSJeremy L Thompson     for (CeedInt d = 0; d < basis->dim; d++) {
21482b730f8bSJeremy L Thompson       for (CeedInt qpt = 0; qpt < basis->Q; qpt++) {
21499d007619Sjeremylt         for (CeedInt node = 0; node < basis->P; node++) {
2150d1d35e2fSjeremylt           CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d;
2151d1d35e2fSjeremylt           CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d;
21521c66c397SJeremy L Thompson 
2153d1d35e2fSjeremylt           basis->interp[qpt * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p];
21549d007619Sjeremylt         }
21559d007619Sjeremylt       }
21562b730f8bSJeremy L Thompson     }
21572b730f8bSJeremy L Thompson   }
21589d007619Sjeremylt   *interp = basis->interp;
2159e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
21609d007619Sjeremylt }
21619d007619Sjeremylt 
21629d007619Sjeremylt /**
2163ca94c3ddSJeremy L Thompson   @brief Get 1D interpolation matrix of a tensor product `CeedBasis`
21649d007619Sjeremylt 
2165ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis`
2166d1d35e2fSjeremylt   @param[out] interp_1d Variable to store interpolation matrix
21679d007619Sjeremylt 
21689d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
21699d007619Sjeremylt 
21709d007619Sjeremylt   @ref Backend
21719d007619Sjeremylt **/
2172d1d35e2fSjeremylt int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) {
21731203703bSJeremy L Thompson   bool is_tensor_basis;
21741203703bSJeremy L Thompson 
21751203703bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
21766e536b99SJeremy L Thompson   CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis");
2177d1d35e2fSjeremylt   *interp_1d = basis->interp_1d;
2178e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
21799d007619Sjeremylt }
21809d007619Sjeremylt 
21819d007619Sjeremylt /**
2182ca94c3ddSJeremy L Thompson   @brief Get gradient matrix of a `CeedBasis`
21839d007619Sjeremylt 
2184ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
21859d007619Sjeremylt   @param[out] grad  Variable to store gradient matrix
21869d007619Sjeremylt 
21879d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
21889d007619Sjeremylt 
2189b7c9bbdaSJeremy L Thompson   @ref Advanced
21909d007619Sjeremylt **/
21916c58de82SJeremy L Thompson int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) {
21926402da51SJeremy L Thompson   if (!basis->grad && basis->is_tensor_basis) {
21939d007619Sjeremylt     // Allocate
21942b730f8bSJeremy L Thompson     CeedCall(CeedMalloc(basis->dim * basis->Q * basis->P, &basis->grad));
21959d007619Sjeremylt 
21969d007619Sjeremylt     // Initialize
21972b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < basis->dim * basis->Q * basis->P; i++) basis->grad[i] = 1.0;
21989d007619Sjeremylt 
21999d007619Sjeremylt     // Calculate
22002b730f8bSJeremy L Thompson     for (CeedInt d = 0; d < basis->dim; d++) {
22012b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < basis->dim; i++) {
22022b730f8bSJeremy L Thompson         for (CeedInt qpt = 0; qpt < basis->Q; qpt++) {
22039d007619Sjeremylt           for (CeedInt node = 0; node < basis->P; node++) {
2204d1d35e2fSjeremylt             CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d;
2205d1d35e2fSjeremylt             CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d;
22061c66c397SJeremy L Thompson 
22072b730f8bSJeremy L Thompson             if (i == d) basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->grad_1d[q * basis->P_1d + p];
22082b730f8bSJeremy L Thompson             else basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p];
22092b730f8bSJeremy L Thompson           }
22102b730f8bSJeremy L Thompson         }
22112b730f8bSJeremy L Thompson       }
22129d007619Sjeremylt     }
22139d007619Sjeremylt   }
22149d007619Sjeremylt   *grad = basis->grad;
2215e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
22169d007619Sjeremylt }
22179d007619Sjeremylt 
22189d007619Sjeremylt /**
2219ca94c3ddSJeremy L Thompson   @brief Get 1D gradient matrix of a tensor product `CeedBasis`
22209d007619Sjeremylt 
2221ca94c3ddSJeremy L Thompson   @param[in]  basis   `CeedBasis`
2222d1d35e2fSjeremylt   @param[out] grad_1d Variable to store gradient matrix
22239d007619Sjeremylt 
22249d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
22259d007619Sjeremylt 
2226b7c9bbdaSJeremy L Thompson   @ref Advanced
22279d007619Sjeremylt **/
2228d1d35e2fSjeremylt int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) {
22291203703bSJeremy L Thompson   bool is_tensor_basis;
22301203703bSJeremy L Thompson 
22311203703bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
22326e536b99SJeremy L Thompson   CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis");
2233d1d35e2fSjeremylt   *grad_1d = basis->grad_1d;
2234e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
22359d007619Sjeremylt }
22369d007619Sjeremylt 
22379d007619Sjeremylt /**
2238ca94c3ddSJeremy L Thompson   @brief Get divergence matrix of a `CeedBasis`
223950c301a5SRezgar Shakeri 
2240ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
224150c301a5SRezgar Shakeri   @param[out] div   Variable to store divergence matrix
224250c301a5SRezgar Shakeri 
224350c301a5SRezgar Shakeri   @return An error code: 0 - success, otherwise - failure
224450c301a5SRezgar Shakeri 
224550c301a5SRezgar Shakeri   @ref Advanced
224650c301a5SRezgar Shakeri **/
224750c301a5SRezgar Shakeri int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) {
224850c301a5SRezgar Shakeri   *div = basis->div;
224950c301a5SRezgar Shakeri   return CEED_ERROR_SUCCESS;
225050c301a5SRezgar Shakeri }
225150c301a5SRezgar Shakeri 
225250c301a5SRezgar Shakeri /**
2253ca94c3ddSJeremy L Thompson   @brief Get curl matrix of a `CeedBasis`
2254c4e3f59bSSebastian Grimberg 
2255ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
2256c4e3f59bSSebastian Grimberg   @param[out] curl  Variable to store curl matrix
2257c4e3f59bSSebastian Grimberg 
2258c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
2259c4e3f59bSSebastian Grimberg 
2260c4e3f59bSSebastian Grimberg   @ref Advanced
2261c4e3f59bSSebastian Grimberg **/
2262c4e3f59bSSebastian Grimberg int CeedBasisGetCurl(CeedBasis basis, const CeedScalar **curl) {
2263c4e3f59bSSebastian Grimberg   *curl = basis->curl;
2264c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
2265c4e3f59bSSebastian Grimberg }
2266c4e3f59bSSebastian Grimberg 
2267c4e3f59bSSebastian Grimberg /**
2268ca94c3ddSJeremy L Thompson   @brief Destroy a @ref  CeedBasis
22697a982d89SJeremy L. Thompson 
2270ca94c3ddSJeremy L Thompson   @param[in,out] basis `CeedBasis` to destroy
22717a982d89SJeremy L. Thompson 
22727a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
22737a982d89SJeremy L. Thompson 
22747a982d89SJeremy L. Thompson   @ref User
22757a982d89SJeremy L. Thompson **/
22767a982d89SJeremy L. Thompson int CeedBasisDestroy(CeedBasis *basis) {
2277356036faSJeremy L Thompson   if (!*basis || *basis == CEED_BASIS_NONE || --(*basis)->ref_count > 0) {
2278ad6481ceSJeremy L Thompson     *basis = NULL;
2279ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2280ad6481ceSJeremy L Thompson   }
22812b730f8bSJeremy L Thompson   if ((*basis)->Destroy) CeedCall((*basis)->Destroy(*basis));
22829831d45aSJeremy L Thompson   CeedCall(CeedTensorContractDestroy(&(*basis)->contract));
2283c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->q_ref_1d));
2284c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->q_weight_1d));
22852b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->interp));
22862b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->interp_1d));
22872b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->grad));
22882b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->grad_1d));
2289c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->div));
2290c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->curl));
2291c8c3fa7dSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*basis)->vec_chebyshev));
2292c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisDestroy(&(*basis)->basis_chebyshev));
22932b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*basis)->ceed));
22942b730f8bSJeremy L Thompson   CeedCall(CeedFree(basis));
2295e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
22967a982d89SJeremy L. Thompson }
22977a982d89SJeremy L. Thompson 
22987a982d89SJeremy L. Thompson /**
2299b11c1e72Sjeremylt   @brief Construct a Gauss-Legendre quadrature
2300b11c1e72Sjeremylt 
2301ca94c3ddSJeremy L Thompson   @param[in]  Q           Number of quadrature points (integrates polynomials of degree `2*Q-1` exactly)
2302ca94c3ddSJeremy L Thompson   @param[out] q_ref_1d    Array of length `Q` to hold the abscissa on `[-1, 1]`
2303ca94c3ddSJeremy L Thompson   @param[out] q_weight_1d Array of length `Q` to hold the weights
2304b11c1e72Sjeremylt 
2305b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2306dfdf5a53Sjeremylt 
2307dfdf5a53Sjeremylt   @ref Utility
2308b11c1e72Sjeremylt **/
23092b730f8bSJeremy L Thompson int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) {
2310d7b241e6Sjeremylt   CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0 * atan(1.0);
23111c66c397SJeremy L Thompson 
2312d1d35e2fSjeremylt   // Build q_ref_1d, q_weight_1d
231392ae7e47SJeremy L Thompson   for (CeedInt i = 0; i <= Q / 2; i++) {
2314d7b241e6Sjeremylt     // Guess
2315d7b241e6Sjeremylt     xi = cos(PI * (CeedScalar)(2 * i + 1) / ((CeedScalar)(2 * Q)));
2316d7b241e6Sjeremylt     // Pn(xi)
2317d7b241e6Sjeremylt     P0 = 1.0;
2318d7b241e6Sjeremylt     P1 = xi;
2319d7b241e6Sjeremylt     P2 = 0.0;
232092ae7e47SJeremy L Thompson     for (CeedInt j = 2; j <= Q; j++) {
2321d7b241e6Sjeremylt       P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2322d7b241e6Sjeremylt       P0 = P1;
2323d7b241e6Sjeremylt       P1 = P2;
2324d7b241e6Sjeremylt     }
2325d7b241e6Sjeremylt     // First Newton Step
2326d7b241e6Sjeremylt     dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2327d7b241e6Sjeremylt     xi  = xi - P2 / dP2;
2328d7b241e6Sjeremylt     // Newton to convergence
232992ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < 100 && fabs(P2) > 10 * CEED_EPSILON; k++) {
2330d7b241e6Sjeremylt       P0 = 1.0;
2331d7b241e6Sjeremylt       P1 = xi;
233292ae7e47SJeremy L Thompson       for (CeedInt j = 2; j <= Q; j++) {
2333d7b241e6Sjeremylt         P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2334d7b241e6Sjeremylt         P0 = P1;
2335d7b241e6Sjeremylt         P1 = P2;
2336d7b241e6Sjeremylt       }
2337d7b241e6Sjeremylt       dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2338d7b241e6Sjeremylt       xi  = xi - P2 / dP2;
2339d7b241e6Sjeremylt     }
2340d7b241e6Sjeremylt     // Save xi, wi
2341d7b241e6Sjeremylt     wi                     = 2.0 / ((1.0 - xi * xi) * dP2 * dP2);
2342d1d35e2fSjeremylt     q_weight_1d[i]         = wi;
2343d1d35e2fSjeremylt     q_weight_1d[Q - 1 - i] = wi;
2344d1d35e2fSjeremylt     q_ref_1d[i]            = -xi;
2345d1d35e2fSjeremylt     q_ref_1d[Q - 1 - i]    = xi;
2346d7b241e6Sjeremylt   }
2347e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2348d7b241e6Sjeremylt }
2349d7b241e6Sjeremylt 
2350b11c1e72Sjeremylt /**
2351b11c1e72Sjeremylt   @brief Construct a Gauss-Legendre-Lobatto quadrature
2352b11c1e72Sjeremylt 
2353ca94c3ddSJeremy L Thompson   @param[in]  Q           Number of quadrature points (integrates polynomials of degree `2*Q-3` exactly)
2354ca94c3ddSJeremy L Thompson   @param[out] q_ref_1d    Array of length `Q` to hold the abscissa on `[-1, 1]`
2355ca94c3ddSJeremy L Thompson   @param[out] q_weight_1d Array of length `Q` to hold the weights
2356b11c1e72Sjeremylt 
2357b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2358dfdf5a53Sjeremylt 
2359dfdf5a53Sjeremylt   @ref Utility
2360b11c1e72Sjeremylt **/
23612b730f8bSJeremy L Thompson int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) {
2362d7b241e6Sjeremylt   CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0 * atan(1.0);
23631c66c397SJeremy L Thompson 
2364d1d35e2fSjeremylt   // Build q_ref_1d, q_weight_1d
2365d7b241e6Sjeremylt   // Set endpoints
23666574a04fSJeremy L Thompson   CeedCheck(Q > 1, NULL, CEED_ERROR_DIMENSION, "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q);
2367d7b241e6Sjeremylt   wi = 2.0 / ((CeedScalar)(Q * (Q - 1)));
2368d1d35e2fSjeremylt   if (q_weight_1d) {
2369d1d35e2fSjeremylt     q_weight_1d[0]     = wi;
2370d1d35e2fSjeremylt     q_weight_1d[Q - 1] = wi;
2371d7b241e6Sjeremylt   }
2372d1d35e2fSjeremylt   q_ref_1d[0]     = -1.0;
2373d1d35e2fSjeremylt   q_ref_1d[Q - 1] = 1.0;
2374d7b241e6Sjeremylt   // Interior
237592ae7e47SJeremy L Thompson   for (CeedInt i = 1; i <= (Q - 1) / 2; i++) {
2376d7b241e6Sjeremylt     // Guess
2377d7b241e6Sjeremylt     xi = cos(PI * (CeedScalar)(i) / (CeedScalar)(Q - 1));
2378d7b241e6Sjeremylt     // Pn(xi)
2379d7b241e6Sjeremylt     P0 = 1.0;
2380d7b241e6Sjeremylt     P1 = xi;
2381d7b241e6Sjeremylt     P2 = 0.0;
238292ae7e47SJeremy L Thompson     for (CeedInt j = 2; j < Q; j++) {
2383d7b241e6Sjeremylt       P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2384d7b241e6Sjeremylt       P0 = P1;
2385d7b241e6Sjeremylt       P1 = P2;
2386d7b241e6Sjeremylt     }
2387d7b241e6Sjeremylt     // First Newton step
2388d7b241e6Sjeremylt     dP2  = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2389d7b241e6Sjeremylt     d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi);
2390d7b241e6Sjeremylt     xi   = xi - dP2 / d2P2;
2391d7b241e6Sjeremylt     // Newton to convergence
239292ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < 100 && fabs(dP2) > 10 * CEED_EPSILON; k++) {
2393d7b241e6Sjeremylt       P0 = 1.0;
2394d7b241e6Sjeremylt       P1 = xi;
239592ae7e47SJeremy L Thompson       for (CeedInt j = 2; j < Q; j++) {
2396d7b241e6Sjeremylt         P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2397d7b241e6Sjeremylt         P0 = P1;
2398d7b241e6Sjeremylt         P1 = P2;
2399d7b241e6Sjeremylt       }
2400d7b241e6Sjeremylt       dP2  = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2401d7b241e6Sjeremylt       d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi);
2402d7b241e6Sjeremylt       xi   = xi - dP2 / d2P2;
2403d7b241e6Sjeremylt     }
2404d7b241e6Sjeremylt     // Save xi, wi
2405d7b241e6Sjeremylt     wi = 2.0 / (((CeedScalar)(Q * (Q - 1))) * P2 * P2);
2406d1d35e2fSjeremylt     if (q_weight_1d) {
2407d1d35e2fSjeremylt       q_weight_1d[i]         = wi;
2408d1d35e2fSjeremylt       q_weight_1d[Q - 1 - i] = wi;
2409d7b241e6Sjeremylt     }
2410d1d35e2fSjeremylt     q_ref_1d[i]         = -xi;
2411d1d35e2fSjeremylt     q_ref_1d[Q - 1 - i] = xi;
2412d7b241e6Sjeremylt   }
2413e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2414d7b241e6Sjeremylt }
2415d7b241e6Sjeremylt 
2416d7b241e6Sjeremylt /// @}
2417