xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-basis.c (revision 1203703b5dc87b4acbe66c9a27384ca8ad07798d)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, 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;
1981c66c397SJeremy L Thompson   bool    is_tensor_to, is_tensor_from;
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));
2066574a04fSJeremy L Thompson   CeedCheck(Q_to == Q_from, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2071c66c397SJeremy L Thompson   Q = Q_to;
208a76a04e7SJeremy L Thompson 
20914556e63SJeremy L Thompson   // Check for matching tensor or non-tensor
2102b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to));
2112b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from));
2126574a04fSJeremy L Thompson   CeedCheck(is_tensor_to == is_tensor_from, ceed, CEED_ERROR_MINOR, "Bases must both be tensor or non-tensor");
2136574a04fSJeremy L Thompson   if (is_tensor_to) {
2142b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_to));
2152b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_from));
2162b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints1D(basis_from, &Q));
2176574a04fSJeremy L Thompson   } else {
2182b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_to, &P_to));
2192b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_from, &P_from));
220a76a04e7SJeremy L Thompson   }
221a76a04e7SJeremy L Thompson 
22215ad3917SSebastian Grimberg   // Check for matching FE space
22315ad3917SSebastian Grimberg   CeedFESpace fe_space_to, fe_space_from;
22415ad3917SSebastian Grimberg   CeedCall(CeedBasisGetFESpace(basis_to, &fe_space_to));
22515ad3917SSebastian Grimberg   CeedCall(CeedBasisGetFESpace(basis_from, &fe_space_from));
2266574a04fSJeremy L Thompson   CeedCheck(fe_space_to == fe_space_from, ceed, CEED_ERROR_MINOR, "Bases must both be the same FE space type");
22715ad3917SSebastian Grimberg 
22814556e63SJeremy L Thompson   // Get source matrices
22915ad3917SSebastian Grimberg   CeedInt           dim, q_comp = 1;
2302247a93fSRezgar Shakeri   CeedScalar       *interp_to_inv, *interp_from;
2311c66c397SJeremy L Thompson   const CeedScalar *interp_to_source = NULL, *interp_from_source = NULL, *grad_from_source = NULL;
2321c66c397SJeremy L Thompson 
2332b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis_to, &dim));
234a76a04e7SJeremy L Thompson   if (is_tensor_to) {
2352b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis_to, &interp_to_source));
2362b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis_from, &interp_from_source));
237a76a04e7SJeremy L Thompson   } else {
23815ad3917SSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis_from, CEED_EVAL_INTERP, &q_comp));
2392b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(basis_to, &interp_to_source));
2402b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(basis_from, &interp_from_source));
24115ad3917SSebastian Grimberg   }
24215ad3917SSebastian Grimberg   CeedCall(CeedMalloc(Q * P_from * q_comp, &interp_from));
24315ad3917SSebastian Grimberg   CeedCall(CeedCalloc(P_to * P_from, interp_project));
24415ad3917SSebastian Grimberg 
24515ad3917SSebastian Grimberg   // `grad_project = interp_to^+ * grad_from` is computed for the H^1 space case so the
246de05fbb2SSebastian Grimberg   // projection basis will have a gradient operation (allocated even if not H^1 for the
247de05fbb2SSebastian Grimberg   // basis construction later on)
24815ad3917SSebastian Grimberg   if (fe_space_to == CEED_FE_SPACE_H1) {
24915ad3917SSebastian Grimberg     if (is_tensor_to) {
25015ad3917SSebastian Grimberg       CeedCall(CeedBasisGetGrad1D(basis_from, &grad_from_source));
25115ad3917SSebastian Grimberg     } else {
2522b730f8bSJeremy L Thompson       CeedCall(CeedBasisGetGrad(basis_from, &grad_from_source));
253a76a04e7SJeremy L Thompson     }
254de05fbb2SSebastian Grimberg   }
25515ad3917SSebastian Grimberg   CeedCall(CeedCalloc(P_to * P_from * (is_tensor_to ? 1 : dim), grad_project));
25615ad3917SSebastian Grimberg 
2572247a93fSRezgar Shakeri   // Compute interp_to^+, pseudoinverse of interp_to
2582247a93fSRezgar Shakeri   CeedCall(CeedCalloc(Q * q_comp * P_to, &interp_to_inv));
259*1203703bSJeremy L Thompson   CeedCall(CeedMatrixPseudoinverse(ceed, interp_to_source, Q * q_comp, P_to, interp_to_inv));
26014556e63SJeremy L Thompson   // Build matrices
26115ad3917SSebastian Grimberg   CeedInt     num_matrices = 1 + (fe_space_to == CEED_FE_SPACE_H1) * (is_tensor_to ? 1 : dim);
26214556e63SJeremy L Thompson   CeedScalar *input_from[num_matrices], *output_project[num_matrices];
2631c66c397SJeremy L Thompson 
26414556e63SJeremy L Thompson   input_from[0]     = (CeedScalar *)interp_from_source;
26514556e63SJeremy L Thompson   output_project[0] = *interp_project;
26614556e63SJeremy L Thompson   for (CeedInt m = 1; m < num_matrices; m++) {
26714556e63SJeremy L Thompson     input_from[m]     = (CeedScalar *)&grad_from_source[(m - 1) * Q * P_from];
26802af4036SJeremy L Thompson     output_project[m] = &((*grad_project)[(m - 1) * P_to * P_from]);
26914556e63SJeremy L Thompson   }
27014556e63SJeremy L Thompson   for (CeedInt m = 0; m < num_matrices; m++) {
2712247a93fSRezgar Shakeri     // output_project = interp_to^+ * interp_from
27215ad3917SSebastian Grimberg     memcpy(interp_from, input_from[m], Q * P_from * q_comp * sizeof(input_from[m][0]));
2732247a93fSRezgar Shakeri     CeedCall(CeedMatrixMatrixMultiply(ceed, interp_to_inv, input_from[m], output_project[m], P_to, P_from, Q * q_comp));
2742247a93fSRezgar Shakeri     // Round zero to machine precision
2752247a93fSRezgar Shakeri     for (CeedInt i = 0; i < P_to * P_from; i++) {
2762247a93fSRezgar Shakeri       if (fabs(output_project[m][i]) < 10 * CEED_EPSILON) output_project[m][i] = 0.0;
277a76a04e7SJeremy L Thompson     }
27814556e63SJeremy L Thompson   }
27914556e63SJeremy L Thompson 
28014556e63SJeremy L Thompson   // Cleanup
2812247a93fSRezgar Shakeri   CeedCall(CeedFree(&interp_to_inv));
2822b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_from));
283a76a04e7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
284a76a04e7SJeremy L Thompson }
285a76a04e7SJeremy L Thompson 
2867a982d89SJeremy L. Thompson /// @}
2877a982d89SJeremy L. Thompson 
2887a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
2897a982d89SJeremy L. Thompson /// Ceed Backend API
2907a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
2917a982d89SJeremy L. Thompson /// @addtogroup CeedBasisBackend
2927a982d89SJeremy L. Thompson /// @{
2937a982d89SJeremy L. Thompson 
2947a982d89SJeremy L. Thompson /**
295ca94c3ddSJeremy L Thompson   @brief Return collocated gradient matrix
2967a982d89SJeremy L. Thompson 
297ca94c3ddSJeremy L Thompson   @param[in]  basis         `CeedBasis`
298ca94c3ddSJeremy L Thompson   @param[out] collo_grad_1d Row-major (`Q_1d * Q_1d`) matrix expressing derivatives of basis functions at quadrature points
2997a982d89SJeremy L. Thompson 
3007a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3017a982d89SJeremy L. Thompson 
3027a982d89SJeremy L. Thompson   @ref Backend
3037a982d89SJeremy L. Thompson **/
304d1d35e2fSjeremylt int CeedBasisGetCollocatedGrad(CeedBasis basis, CeedScalar *collo_grad_1d) {
3057a982d89SJeremy L. Thompson   Ceed              ceed;
3062247a93fSRezgar Shakeri   CeedInt           P_1d, Q_1d;
3072247a93fSRezgar Shakeri   CeedScalar       *interp_1d_pinv;
308*1203703bSJeremy L Thompson   const CeedScalar *grad_1d, *interp_1d;
309*1203703bSJeremy L Thompson 
310ea61e9acSJeremy 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.
3112247a93fSRezgar Shakeri   CeedCall(CeedBasisGetCeed(basis, &ceed));
3122247a93fSRezgar Shakeri   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
3132247a93fSRezgar Shakeri   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
3147a982d89SJeremy L. Thompson 
3152247a93fSRezgar Shakeri   // Compute interp_1d^+, pseudoinverse of interp_1d
3162247a93fSRezgar Shakeri   CeedCall(CeedCalloc(P_1d * Q_1d, &interp_1d_pinv));
317*1203703bSJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
318*1203703bSJeremy L Thompson   CeedCall(CeedMatrixPseudoinverse(ceed, interp_1d, Q_1d, P_1d, interp_1d_pinv));
319*1203703bSJeremy L Thompson   CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
320*1203703bSJeremy L Thompson   CeedCall(CeedMatrixMatrixMultiply(ceed, grad_1d, (const CeedScalar *)interp_1d_pinv, collo_grad_1d, Q_1d, Q_1d, P_1d));
3217a982d89SJeremy L. Thompson 
3222247a93fSRezgar Shakeri   CeedCall(CeedFree(&interp_1d_pinv));
323e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3247a982d89SJeremy L. Thompson }
3257a982d89SJeremy L. Thompson 
3267a982d89SJeremy L. Thompson /**
327ca94c3ddSJeremy L Thompson   @brief Get tensor status for given `CeedBasis`
3287a982d89SJeremy L. Thompson 
329ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis`
330d1d35e2fSjeremylt   @param[out] is_tensor Variable to store tensor status
3317a982d89SJeremy L. Thompson 
3327a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3337a982d89SJeremy L. Thompson 
3347a982d89SJeremy L. Thompson   @ref Backend
3357a982d89SJeremy L. Thompson **/
336d1d35e2fSjeremylt int CeedBasisIsTensor(CeedBasis basis, bool *is_tensor) {
3376402da51SJeremy L Thompson   *is_tensor = basis->is_tensor_basis;
338e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3397a982d89SJeremy L. Thompson }
3407a982d89SJeremy L. Thompson 
3417a982d89SJeremy L. Thompson /**
342ca94c3ddSJeremy L Thompson   @brief Get backend data of a `CeedBasis`
3437a982d89SJeremy L. Thompson 
344ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
3457a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
3467a982d89SJeremy L. Thompson 
3477a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3487a982d89SJeremy L. Thompson 
3497a982d89SJeremy L. Thompson   @ref Backend
3507a982d89SJeremy L. Thompson **/
351777ff853SJeremy L Thompson int CeedBasisGetData(CeedBasis basis, void *data) {
352777ff853SJeremy L Thompson   *(void **)data = basis->data;
353e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3547a982d89SJeremy L. Thompson }
3557a982d89SJeremy L. Thompson 
3567a982d89SJeremy L. Thompson /**
357ca94c3ddSJeremy L Thompson   @brief Set backend data of a `CeedBasis`
3587a982d89SJeremy L. Thompson 
359ca94c3ddSJeremy L Thompson   @param[in,out] basis  `CeedBasis`
360ea61e9acSJeremy L Thompson   @param[in]     data   Data to set
3617a982d89SJeremy L. Thompson 
3627a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3637a982d89SJeremy L. Thompson 
3647a982d89SJeremy L. Thompson   @ref Backend
3657a982d89SJeremy L. Thompson **/
366777ff853SJeremy L Thompson int CeedBasisSetData(CeedBasis basis, void *data) {
367777ff853SJeremy L Thompson   basis->data = data;
368e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3697a982d89SJeremy L. Thompson }
3707a982d89SJeremy L. Thompson 
3717a982d89SJeremy L. Thompson /**
372ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedBasis`
37334359f16Sjeremylt 
374ca94c3ddSJeremy L Thompson   @param[in,out] basis `CeedBasis` to increment the reference counter
37534359f16Sjeremylt 
37634359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
37734359f16Sjeremylt 
37834359f16Sjeremylt   @ref Backend
37934359f16Sjeremylt **/
3809560d06aSjeremylt int CeedBasisReference(CeedBasis basis) {
38134359f16Sjeremylt   basis->ref_count++;
38234359f16Sjeremylt   return CEED_ERROR_SUCCESS;
38334359f16Sjeremylt }
38434359f16Sjeremylt 
38534359f16Sjeremylt /**
386ca94c3ddSJeremy L Thompson   @brief Get number of Q-vector components for given `CeedBasis`
387c4e3f59bSSebastian Grimberg 
388ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis`
389ca94c3ddSJeremy L Thompson   @param[in]  eval_mode @ref CEED_EVAL_INTERP to use interpolated values,
390ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_GRAD to use gradients,
391ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_DIV to use divergence,
392ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_CURL to use curl
393c4e3f59bSSebastian Grimberg   @param[out] q_comp    Variable to store number of Q-vector components of basis
394c4e3f59bSSebastian Grimberg 
395c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
396c4e3f59bSSebastian Grimberg 
397c4e3f59bSSebastian Grimberg   @ref Backend
398c4e3f59bSSebastian Grimberg **/
399c4e3f59bSSebastian Grimberg int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode, CeedInt *q_comp) {
400*1203703bSJeremy L Thompson   CeedInt dim;
401*1203703bSJeremy L Thompson 
402*1203703bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
403c4e3f59bSSebastian Grimberg   switch (eval_mode) {
404*1203703bSJeremy L Thompson     case CEED_EVAL_INTERP: {
405*1203703bSJeremy L Thompson       CeedFESpace fe_space;
406*1203703bSJeremy L Thompson 
407*1203703bSJeremy L Thompson       CeedCall(CeedBasisGetFESpace(basis, &fe_space));
408*1203703bSJeremy L Thompson       *q_comp = (fe_space == CEED_FE_SPACE_H1) ? 1 : dim;
409*1203703bSJeremy L Thompson     } break;
410c4e3f59bSSebastian Grimberg     case CEED_EVAL_GRAD:
411*1203703bSJeremy L Thompson       *q_comp = dim;
412c4e3f59bSSebastian Grimberg       break;
413c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
414c4e3f59bSSebastian Grimberg       *q_comp = 1;
415c4e3f59bSSebastian Grimberg       break;
416c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
417*1203703bSJeremy L Thompson       *q_comp = (dim < 3) ? 1 : dim;
418c4e3f59bSSebastian Grimberg       break;
419c4e3f59bSSebastian Grimberg     case CEED_EVAL_NONE:
420c4e3f59bSSebastian Grimberg     case CEED_EVAL_WEIGHT:
421352a5e7cSSebastian Grimberg       *q_comp = 1;
422c4e3f59bSSebastian Grimberg       break;
423c4e3f59bSSebastian Grimberg   }
424c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
425c4e3f59bSSebastian Grimberg }
426c4e3f59bSSebastian Grimberg 
427c4e3f59bSSebastian Grimberg /**
428ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs required to apply `CeedBasis` in `t_mode` and `eval_mode`
4296e15d496SJeremy L Thompson 
430ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis` to estimate FLOPs for
431ea61e9acSJeremy L Thompson   @param[in]  t_mode    Apply basis or transpose
432ca94c3ddSJeremy L Thompson   @param[in]  eval_mode @ref CeedEvalMode
433ea61e9acSJeremy L Thompson   @param[out] flops     Address of variable to hold FLOPs estimate
4346e15d496SJeremy L Thompson 
4356e15d496SJeremy L Thompson   @ref Backend
4366e15d496SJeremy L Thompson **/
4372b730f8bSJeremy L Thompson int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedSize *flops) {
4386e15d496SJeremy L Thompson   bool is_tensor;
4396e15d496SJeremy L Thompson 
4402b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor));
4416e15d496SJeremy L Thompson   if (is_tensor) {
4426e15d496SJeremy L Thompson     CeedInt dim, num_comp, P_1d, Q_1d;
4431c66c397SJeremy L Thompson 
4442b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis, &dim));
4452b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
4462b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
4472b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
4486e15d496SJeremy L Thompson     if (t_mode == CEED_TRANSPOSE) {
4492b730f8bSJeremy L Thompson       P_1d = Q_1d;
4502b730f8bSJeremy L Thompson       Q_1d = P_1d;
4516e15d496SJeremy L Thompson     }
4526e15d496SJeremy L Thompson     CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim - 1), post = 1;
4536e15d496SJeremy L Thompson     for (CeedInt d = 0; d < dim; d++) {
4546e15d496SJeremy L Thompson       tensor_flops += 2 * pre * P_1d * post * Q_1d;
4556e15d496SJeremy L Thompson       pre /= P_1d;
4566e15d496SJeremy L Thompson       post *= Q_1d;
4576e15d496SJeremy L Thompson     }
4586e15d496SJeremy L Thompson     switch (eval_mode) {
4592b730f8bSJeremy L Thompson       case CEED_EVAL_NONE:
4602b730f8bSJeremy L Thompson         *flops = 0;
4612b730f8bSJeremy L Thompson         break;
4622b730f8bSJeremy L Thompson       case CEED_EVAL_INTERP:
4632b730f8bSJeremy L Thompson         *flops = tensor_flops;
4642b730f8bSJeremy L Thompson         break;
4652b730f8bSJeremy L Thompson       case CEED_EVAL_GRAD:
4662b730f8bSJeremy L Thompson         *flops = tensor_flops * 2;
4672b730f8bSJeremy L Thompson         break;
4686e15d496SJeremy L Thompson       case CEED_EVAL_DIV:
469*1203703bSJeremy L Thompson       case CEED_EVAL_CURL: {
4706574a04fSJeremy L Thompson         // LCOV_EXCL_START
471*1203703bSJeremy L Thompson         Ceed ceed;
472*1203703bSJeremy L Thompson 
473*1203703bSJeremy L Thompson         CeedCall(CeedBasisGetCeed(basis, &ceed));
474*1203703bSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported", CeedEvalModes[eval_mode]);
4752b730f8bSJeremy L Thompson         break;
4766e15d496SJeremy L Thompson         // LCOV_EXCL_STOP
477*1203703bSJeremy L Thompson       }
4782b730f8bSJeremy L Thompson       case CEED_EVAL_WEIGHT:
4792b730f8bSJeremy L Thompson         *flops = dim * CeedIntPow(Q_1d, dim);
4802b730f8bSJeremy L Thompson         break;
4816e15d496SJeremy L Thompson     }
4826e15d496SJeremy L Thompson   } else {
483c4e3f59bSSebastian Grimberg     CeedInt dim, num_comp, q_comp, num_nodes, num_qpts;
4841c66c397SJeremy L Thompson 
4852b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis, &dim));
4862b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
487c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
4882b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
4892b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
4906e15d496SJeremy L Thompson     switch (eval_mode) {
4912b730f8bSJeremy L Thompson       case CEED_EVAL_NONE:
4922b730f8bSJeremy L Thompson         *flops = 0;
4932b730f8bSJeremy L Thompson         break;
4942b730f8bSJeremy L Thompson       case CEED_EVAL_INTERP:
4952b730f8bSJeremy L Thompson       case CEED_EVAL_GRAD:
4962b730f8bSJeremy L Thompson       case CEED_EVAL_DIV:
4972b730f8bSJeremy L Thompson       case CEED_EVAL_CURL:
498c4e3f59bSSebastian Grimberg         *flops = num_nodes * num_qpts * num_comp * q_comp;
4992b730f8bSJeremy L Thompson         break;
5002b730f8bSJeremy L Thompson       case CEED_EVAL_WEIGHT:
5012b730f8bSJeremy L Thompson         *flops = 0;
5022b730f8bSJeremy L Thompson         break;
5036e15d496SJeremy L Thompson     }
5046e15d496SJeremy L Thompson   }
5056e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5066e15d496SJeremy L Thompson }
5076e15d496SJeremy L Thompson 
5086e15d496SJeremy L Thompson /**
509ca94c3ddSJeremy L Thompson   @brief Get `CeedFESpace` for a `CeedBasis`
510c4e3f59bSSebastian Grimberg 
511ca94c3ddSJeremy L Thompson   @param[in]  basis    `CeedBasis`
512ca94c3ddSJeremy L Thompson   @param[out] fe_space Variable to store `CeedFESpace`
513c4e3f59bSSebastian Grimberg 
514c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
515c4e3f59bSSebastian Grimberg 
516c4e3f59bSSebastian Grimberg   @ref Backend
517c4e3f59bSSebastian Grimberg **/
518c4e3f59bSSebastian Grimberg int CeedBasisGetFESpace(CeedBasis basis, CeedFESpace *fe_space) {
519c4e3f59bSSebastian Grimberg   *fe_space = basis->fe_space;
520c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
521c4e3f59bSSebastian Grimberg }
522c4e3f59bSSebastian Grimberg 
523c4e3f59bSSebastian Grimberg /**
524ca94c3ddSJeremy L Thompson   @brief Get dimension for given `CeedElemTopology`
5257a982d89SJeremy L. Thompson 
526ca94c3ddSJeremy L Thompson   @param[in]  topo `CeedElemTopology`
5277a982d89SJeremy L. Thompson   @param[out] dim  Variable to store dimension of topology
5287a982d89SJeremy L. Thompson 
5297a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5307a982d89SJeremy L. Thompson 
5317a982d89SJeremy L. Thompson   @ref Backend
5327a982d89SJeremy L. Thompson **/
5337a982d89SJeremy L. Thompson int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim) {
5347a982d89SJeremy L. Thompson   *dim = (CeedInt)topo >> 16;
535e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5367a982d89SJeremy L. Thompson }
5377a982d89SJeremy L. Thompson 
5387a982d89SJeremy L. Thompson /**
539ca94c3ddSJeremy L Thompson   @brief Get `CeedTensorContract` of a `CeedBasis`
5407a982d89SJeremy L. Thompson 
541ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis`
542ca94c3ddSJeremy L Thompson   @param[out] contract  Variable to store `CeedTensorContract`
5437a982d89SJeremy L. Thompson 
5447a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5457a982d89SJeremy L. Thompson 
5467a982d89SJeremy L. Thompson   @ref Backend
5477a982d89SJeremy L. Thompson **/
5487a982d89SJeremy L. Thompson int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract) {
5497a982d89SJeremy L. Thompson   *contract = basis->contract;
550e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5517a982d89SJeremy L. Thompson }
5527a982d89SJeremy L. Thompson 
5537a982d89SJeremy L. Thompson /**
554ca94c3ddSJeremy L Thompson   @brief Set `CeedTensorContract` of a `CeedBasis`
5557a982d89SJeremy L. Thompson 
556ca94c3ddSJeremy L Thompson   @param[in,out] basis    `CeedBasis`
557ca94c3ddSJeremy L Thompson   @param[in]     contract `CeedTensorContract` to set
5587a982d89SJeremy L. Thompson 
5597a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5607a982d89SJeremy L. Thompson 
5617a982d89SJeremy L. Thompson   @ref Backend
5627a982d89SJeremy L. Thompson **/
56334359f16Sjeremylt int CeedBasisSetTensorContract(CeedBasis basis, CeedTensorContract contract) {
56434359f16Sjeremylt   basis->contract = contract;
5652b730f8bSJeremy L Thompson   CeedCall(CeedTensorContractReference(contract));
566e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5677a982d89SJeremy L. Thompson }
5687a982d89SJeremy L. Thompson 
5697a982d89SJeremy L. Thompson /**
570ca94c3ddSJeremy L Thompson   @brief Return a reference implementation of matrix multiplication \f$C = A B\f$.
571ba59ac12SSebastian Grimberg 
572ca94c3ddSJeremy L Thompson   Note: This is a reference implementation for CPU `CeedScalar` pointers that is not intended for high performance.
5737a982d89SJeremy L. Thompson 
574ca94c3ddSJeremy L Thompson   @param[in]  ceed  `Ceed` context for error handling
575ca94c3ddSJeremy L Thompson   @param[in]  mat_A Row-major matrix `A`
576ca94c3ddSJeremy L Thompson   @param[in]  mat_B Row-major matrix `B`
577ca94c3ddSJeremy L Thompson   @param[out] mat_C Row-major output matrix `C`
578ca94c3ddSJeremy L Thompson   @param[in]  m     Number of rows of `C`
579ca94c3ddSJeremy L Thompson   @param[in]  n     Number of columns of `C`
580ca94c3ddSJeremy L Thompson   @param[in]  kk    Number of columns of `A`/rows of `B`
5817a982d89SJeremy L. Thompson 
5827a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5837a982d89SJeremy L. Thompson 
5847a982d89SJeremy L. Thompson   @ref Utility
5857a982d89SJeremy L. Thompson **/
5862b730f8bSJeremy L Thompson int CeedMatrixMatrixMultiply(Ceed ceed, const CeedScalar *mat_A, const CeedScalar *mat_B, CeedScalar *mat_C, CeedInt m, CeedInt n, CeedInt kk) {
5872b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < m; i++) {
5887a982d89SJeremy L. Thompson     for (CeedInt j = 0; j < n; j++) {
5897a982d89SJeremy L. Thompson       CeedScalar sum = 0;
5901c66c397SJeremy L Thompson 
5912b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < kk; k++) sum += mat_A[k + i * kk] * mat_B[j + k * n];
592d1d35e2fSjeremylt       mat_C[j + i * n] = sum;
5937a982d89SJeremy L. Thompson     }
5942b730f8bSJeremy L Thompson   }
595e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5967a982d89SJeremy L. Thompson }
5977a982d89SJeremy L. Thompson 
598ba59ac12SSebastian Grimberg /**
599ba59ac12SSebastian Grimberg   @brief Return QR Factorization of a matrix
600ba59ac12SSebastian Grimberg 
601ca94c3ddSJeremy L Thompson   @param[in]     ceed `Ceed` context for error handling
602ba59ac12SSebastian Grimberg   @param[in,out] mat  Row-major matrix to be factorized in place
603ca94c3ddSJeremy L Thompson   @param[in,out] tau  Vector of length `m` of scaling factors
604ba59ac12SSebastian Grimberg   @param[in]     m    Number of rows
605ba59ac12SSebastian Grimberg   @param[in]     n    Number of columns
606ba59ac12SSebastian Grimberg 
607ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
608ba59ac12SSebastian Grimberg 
609ba59ac12SSebastian Grimberg   @ref Utility
610ba59ac12SSebastian Grimberg **/
611ba59ac12SSebastian Grimberg int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau, CeedInt m, CeedInt n) {
612ba59ac12SSebastian Grimberg   CeedScalar v[m];
613ba59ac12SSebastian Grimberg 
614ba59ac12SSebastian Grimberg   // Check matrix shape
6156574a04fSJeremy L Thompson   CeedCheck(n <= m, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute QR factorization with n > m");
616ba59ac12SSebastian Grimberg 
617ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
6181c66c397SJeremy L Thompson     CeedScalar sigma = 0.0;
6191c66c397SJeremy L Thompson 
620ba59ac12SSebastian Grimberg     if (i >= m - 1) {  // last row of matrix, no reflection needed
621ba59ac12SSebastian Grimberg       tau[i] = 0.;
622ba59ac12SSebastian Grimberg       break;
623ba59ac12SSebastian Grimberg     }
624ba59ac12SSebastian Grimberg     // Calculate Householder vector, magnitude
625ba59ac12SSebastian Grimberg     v[i] = mat[i + n * i];
626ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) {
627ba59ac12SSebastian Grimberg       v[j] = mat[i + n * j];
628ba59ac12SSebastian Grimberg       sigma += v[j] * v[j];
629ba59ac12SSebastian Grimberg     }
6301c66c397SJeremy L Thompson     const CeedScalar norm = sqrt(v[i] * v[i] + sigma);  // norm of v[i:m]
6311c66c397SJeremy L Thompson     const CeedScalar R_ii = -copysign(norm, v[i]);
6321c66c397SJeremy L Thompson 
633ba59ac12SSebastian Grimberg     v[i] -= R_ii;
634ba59ac12SSebastian Grimberg     // norm of v[i:m] after modification above and scaling below
635ba59ac12SSebastian Grimberg     //   norm = sqrt(v[i]*v[i] + sigma) / v[i];
636ba59ac12SSebastian Grimberg     //   tau = 2 / (norm*norm)
637ba59ac12SSebastian Grimberg     tau[i] = 2 * v[i] * v[i] / (v[i] * v[i] + sigma);
638ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) v[j] /= v[i];
639ba59ac12SSebastian Grimberg 
640ba59ac12SSebastian Grimberg     // Apply Householder reflector to lower right panel
641ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat[i * n + i + 1], &v[i], tau[i], m - i, n - i - 1, n, 1);
642ba59ac12SSebastian Grimberg     // Save v
643ba59ac12SSebastian Grimberg     mat[i + n * i] = R_ii;
644ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) mat[i + n * j] = v[j];
645ba59ac12SSebastian Grimberg   }
646ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
647ba59ac12SSebastian Grimberg }
648ba59ac12SSebastian Grimberg 
649ba59ac12SSebastian Grimberg /**
650ba59ac12SSebastian Grimberg   @brief Apply Householder Q matrix
651ba59ac12SSebastian Grimberg 
652ca94c3ddSJeremy 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$.
653ba59ac12SSebastian Grimberg 
654ba59ac12SSebastian Grimberg   @param[in,out] mat_A  Matrix to apply Householder Q to, in place
655ba59ac12SSebastian Grimberg   @param[in]     mat_Q  Householder Q matrix
656ba59ac12SSebastian Grimberg   @param[in]     tau    Householder scaling factors
657ba59ac12SSebastian Grimberg   @param[in]     t_mode Transpose mode for application
658ca94c3ddSJeremy L Thompson   @param[in]     m      Number of rows in `A`
659ca94c3ddSJeremy L Thompson   @param[in]     n      Number of columns in `A`
660ca94c3ddSJeremy L Thompson   @param[in]     k      Number of elementary reflectors in Q, `k < m`
661ca94c3ddSJeremy L Thompson   @param[in]     row    Row stride in `A`
662ca94c3ddSJeremy L Thompson   @param[in]     col    Col stride in `A`
663ba59ac12SSebastian Grimberg 
664ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
665ba59ac12SSebastian Grimberg 
666c4e3f59bSSebastian Grimberg   @ref Utility
667ba59ac12SSebastian Grimberg **/
668ba59ac12SSebastian Grimberg int CeedHouseholderApplyQ(CeedScalar *mat_A, const CeedScalar *mat_Q, const CeedScalar *tau, CeedTransposeMode t_mode, CeedInt m, CeedInt n,
669ba59ac12SSebastian Grimberg                           CeedInt k, CeedInt row, CeedInt col) {
670ba59ac12SSebastian Grimberg   CeedScalar *v;
6711c66c397SJeremy L Thompson 
672ba59ac12SSebastian Grimberg   CeedCall(CeedMalloc(m, &v));
673ba59ac12SSebastian Grimberg   for (CeedInt ii = 0; ii < k; ii++) {
674ba59ac12SSebastian Grimberg     CeedInt i = t_mode == CEED_TRANSPOSE ? ii : k - 1 - ii;
675ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) v[j] = mat_Q[j * k + i];
676ba59ac12SSebastian Grimberg     // Apply Householder reflector (I - tau v v^T) collo_grad_1d^T
677ba59ac12SSebastian Grimberg     CeedCall(CeedHouseholderReflect(&mat_A[i * row], &v[i], tau[i], m - i, n, row, col));
678ba59ac12SSebastian Grimberg   }
679ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&v));
680ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
681ba59ac12SSebastian Grimberg }
682ba59ac12SSebastian Grimberg 
683ba59ac12SSebastian Grimberg /**
6842247a93fSRezgar Shakeri   @brief Return pseudoinverse of a matrix
6852247a93fSRezgar Shakeri 
6862247a93fSRezgar Shakeri   @param[in]     ceed      Ceed context for error handling
6872247a93fSRezgar Shakeri   @param[in]     mat       Row-major matrix to compute pseudoinverse of
6882247a93fSRezgar Shakeri   @param[in]     m         Number of rows
6892247a93fSRezgar Shakeri   @param[in]     n         Number of columns
6902247a93fSRezgar Shakeri   @param[out]    mat_pinv  Row-major pseudoinverse matrix
6912247a93fSRezgar Shakeri 
6922247a93fSRezgar Shakeri   @return An error code: 0 - success, otherwise - failure
6932247a93fSRezgar Shakeri 
6942247a93fSRezgar Shakeri   @ref Utility
6952247a93fSRezgar Shakeri **/
696*1203703bSJeremy L Thompson int CeedMatrixPseudoinverse(Ceed ceed, const CeedScalar *mat, CeedInt m, CeedInt n, CeedScalar *mat_pinv) {
6972247a93fSRezgar Shakeri   CeedScalar *tau, *I, *mat_copy;
6982247a93fSRezgar Shakeri 
6992247a93fSRezgar Shakeri   CeedCall(CeedCalloc(m, &tau));
7002247a93fSRezgar Shakeri   CeedCall(CeedCalloc(m * m, &I));
7012247a93fSRezgar Shakeri   CeedCall(CeedCalloc(m * n, &mat_copy));
7022247a93fSRezgar Shakeri   memcpy(mat_copy, mat, m * n * sizeof mat[0]);
7032247a93fSRezgar Shakeri 
7042247a93fSRezgar Shakeri   // QR Factorization, mat = Q R
7052247a93fSRezgar Shakeri   CeedCall(CeedQRFactorization(ceed, mat_copy, tau, m, n));
7062247a93fSRezgar Shakeri 
7072247a93fSRezgar Shakeri   // -- Apply Q^T, I = Q^T * I
7082247a93fSRezgar Shakeri   for (CeedInt i = 0; i < m; i++) I[i * m + i] = 1.0;
7092247a93fSRezgar Shakeri   CeedCall(CeedHouseholderApplyQ(I, mat_copy, tau, CEED_TRANSPOSE, m, m, n, m, 1));
7102247a93fSRezgar Shakeri   // -- Apply R_inv, mat_pinv = R_inv * Q^T
7112247a93fSRezgar Shakeri   for (CeedInt j = 0; j < m; j++) {  // Column j
7122247a93fSRezgar Shakeri     mat_pinv[j + m * (n - 1)] = I[j + m * (n - 1)] / mat_copy[n * n - 1];
7132247a93fSRezgar Shakeri     for (CeedInt i = n - 2; i >= 0; i--) {  // Row i
7142247a93fSRezgar Shakeri       mat_pinv[j + m * i] = I[j + m * i];
7152247a93fSRezgar Shakeri       for (CeedInt k = i + 1; k < n; k++) mat_pinv[j + m * i] -= mat_copy[k + n * i] * mat_pinv[j + m * k];
7162247a93fSRezgar Shakeri       mat_pinv[j + m * i] /= mat_copy[i + n * i];
7172247a93fSRezgar Shakeri     }
7182247a93fSRezgar Shakeri   }
7192247a93fSRezgar Shakeri 
7202247a93fSRezgar Shakeri   // Cleanup
7212247a93fSRezgar Shakeri   CeedCall(CeedFree(&I));
7222247a93fSRezgar Shakeri   CeedCall(CeedFree(&tau));
7232247a93fSRezgar Shakeri   CeedCall(CeedFree(&mat_copy));
7242247a93fSRezgar Shakeri   return CEED_ERROR_SUCCESS;
7252247a93fSRezgar Shakeri }
7262247a93fSRezgar Shakeri 
7272247a93fSRezgar Shakeri /**
728ba59ac12SSebastian Grimberg   @brief Return symmetric Schur decomposition of the symmetric matrix mat via symmetric QR factorization
729ba59ac12SSebastian Grimberg 
730ca94c3ddSJeremy L Thompson   @param[in]     ceed   `Ceed` context for error handling
731ba59ac12SSebastian Grimberg   @param[in,out] mat    Row-major matrix to be factorized in place
732ba59ac12SSebastian Grimberg   @param[out]    lambda Vector of length n of eigenvalues
733ba59ac12SSebastian Grimberg   @param[in]     n      Number of rows/columns
734ba59ac12SSebastian Grimberg 
735ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
736ba59ac12SSebastian Grimberg 
737ba59ac12SSebastian Grimberg   @ref Utility
738ba59ac12SSebastian Grimberg **/
7392c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
7402c2ea1dbSJeremy L Thompson int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat, CeedScalar *lambda, CeedInt n) {
741ba59ac12SSebastian Grimberg   // Check bounds for clang-tidy
7426574a04fSJeremy L Thompson   CeedCheck(n > 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute symmetric Schur decomposition of scalars");
743ba59ac12SSebastian Grimberg 
744ba59ac12SSebastian Grimberg   CeedScalar v[n - 1], tau[n - 1], mat_T[n * n];
745ba59ac12SSebastian Grimberg 
746ba59ac12SSebastian Grimberg   // Copy mat to mat_T and set mat to I
747ba59ac12SSebastian Grimberg   memcpy(mat_T, mat, n * n * sizeof(mat[0]));
748ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
749ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < n; j++) mat[j + n * i] = (i == j) ? 1 : 0;
750ba59ac12SSebastian Grimberg   }
751ba59ac12SSebastian Grimberg 
752ba59ac12SSebastian Grimberg   // Reduce to tridiagonal
753ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n - 1; i++) {
754ba59ac12SSebastian Grimberg     // Calculate Householder vector, magnitude
755ba59ac12SSebastian Grimberg     CeedScalar sigma = 0.0;
7561c66c397SJeremy L Thompson 
757ba59ac12SSebastian Grimberg     v[i] = mat_T[i + n * (i + 1)];
758ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) {
759ba59ac12SSebastian Grimberg       v[j] = mat_T[i + n * (j + 1)];
760ba59ac12SSebastian Grimberg       sigma += v[j] * v[j];
761ba59ac12SSebastian Grimberg     }
7621c66c397SJeremy L Thompson     const CeedScalar norm = sqrt(v[i] * v[i] + sigma);  // norm of v[i:n-1]
7631c66c397SJeremy L Thompson     const CeedScalar R_ii = -copysign(norm, v[i]);
7641c66c397SJeremy L Thompson 
765ba59ac12SSebastian Grimberg     v[i] -= R_ii;
766ba59ac12SSebastian Grimberg     // norm of v[i:m] after modification above and scaling below
767ba59ac12SSebastian Grimberg     //   norm = sqrt(v[i]*v[i] + sigma) / v[i];
768ba59ac12SSebastian Grimberg     //   tau = 2 / (norm*norm)
769ba59ac12SSebastian Grimberg     tau[i] = i == n - 2 ? 2 : 2 * v[i] * v[i] / (v[i] * v[i] + sigma);
770ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) v[j] /= v[i];
771ba59ac12SSebastian Grimberg 
772ba59ac12SSebastian Grimberg     // Update sub and super diagonal
773ba59ac12SSebastian Grimberg     for (CeedInt j = i + 2; j < n; j++) {
774ba59ac12SSebastian Grimberg       mat_T[i + n * j] = 0;
775ba59ac12SSebastian Grimberg       mat_T[j + n * i] = 0;
776ba59ac12SSebastian Grimberg     }
777ba59ac12SSebastian Grimberg     // Apply symmetric Householder reflector to lower right panel
778ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1);
779ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), 1, n);
780ba59ac12SSebastian Grimberg 
781ba59ac12SSebastian Grimberg     // Save v
782ba59ac12SSebastian Grimberg     mat_T[i + n * (i + 1)] = R_ii;
783ba59ac12SSebastian Grimberg     mat_T[(i + 1) + n * i] = R_ii;
784ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) {
785ba59ac12SSebastian Grimberg       mat_T[i + n * (j + 1)] = v[j];
786ba59ac12SSebastian Grimberg     }
787ba59ac12SSebastian Grimberg   }
788ba59ac12SSebastian Grimberg   // Backwards accumulation of Q
789ba59ac12SSebastian Grimberg   for (CeedInt i = n - 2; i >= 0; i--) {
790ba59ac12SSebastian Grimberg     if (tau[i] > 0.0) {
791ba59ac12SSebastian Grimberg       v[i] = 1;
792ba59ac12SSebastian Grimberg       for (CeedInt j = i + 1; j < n - 1; j++) {
793ba59ac12SSebastian Grimberg         v[j]                   = mat_T[i + n * (j + 1)];
794ba59ac12SSebastian Grimberg         mat_T[i + n * (j + 1)] = 0;
795ba59ac12SSebastian Grimberg       }
796ba59ac12SSebastian Grimberg       CeedHouseholderReflect(&mat[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1);
797ba59ac12SSebastian Grimberg     }
798ba59ac12SSebastian Grimberg   }
799ba59ac12SSebastian Grimberg 
800ba59ac12SSebastian Grimberg   // Reduce sub and super diagonal
801ba59ac12SSebastian Grimberg   CeedInt    p = 0, q = 0, itr = 0, max_itr = n * n * n * n;
802ba59ac12SSebastian Grimberg   CeedScalar tol = CEED_EPSILON;
803ba59ac12SSebastian Grimberg 
804ba59ac12SSebastian Grimberg   while (itr < max_itr) {
805ba59ac12SSebastian Grimberg     // Update p, q, size of reduced portions of diagonal
806ba59ac12SSebastian Grimberg     p = 0;
807ba59ac12SSebastian Grimberg     q = 0;
808ba59ac12SSebastian Grimberg     for (CeedInt i = n - 2; i >= 0; i--) {
809ba59ac12SSebastian Grimberg       if (fabs(mat_T[i + n * (i + 1)]) < tol) q += 1;
810ba59ac12SSebastian Grimberg       else break;
811ba59ac12SSebastian Grimberg     }
812ba59ac12SSebastian Grimberg     for (CeedInt i = 0; i < n - q - 1; i++) {
813ba59ac12SSebastian Grimberg       if (fabs(mat_T[i + n * (i + 1)]) < tol) p += 1;
814ba59ac12SSebastian Grimberg       else break;
815ba59ac12SSebastian Grimberg     }
816ba59ac12SSebastian Grimberg     if (q == n - 1) break;  // Finished reducing
817ba59ac12SSebastian Grimberg 
818ba59ac12SSebastian Grimberg     // Reduce tridiagonal portion
819ba59ac12SSebastian Grimberg     CeedScalar t_nn = mat_T[(n - 1 - q) + n * (n - 1 - q)], t_nnm1 = mat_T[(n - 2 - q) + n * (n - 1 - q)];
820ba59ac12SSebastian Grimberg     CeedScalar d  = (mat_T[(n - 2 - q) + n * (n - 2 - q)] - t_nn) / 2;
821ba59ac12SSebastian Grimberg     CeedScalar mu = t_nn - t_nnm1 * t_nnm1 / (d + copysign(sqrt(d * d + t_nnm1 * t_nnm1), d));
822ba59ac12SSebastian Grimberg     CeedScalar x  = mat_T[p + n * p] - mu;
823ba59ac12SSebastian Grimberg     CeedScalar z  = mat_T[p + n * (p + 1)];
8241c66c397SJeremy L Thompson 
825ba59ac12SSebastian Grimberg     for (CeedInt k = p; k < n - q - 1; k++) {
826ba59ac12SSebastian Grimberg       // Compute Givens rotation
827ba59ac12SSebastian Grimberg       CeedScalar c = 1, s = 0;
8281c66c397SJeremy L Thompson 
829ba59ac12SSebastian Grimberg       if (fabs(z) > tol) {
830ba59ac12SSebastian Grimberg         if (fabs(z) > fabs(x)) {
8311c66c397SJeremy L Thompson           const CeedScalar tau = -x / z;
8321c66c397SJeremy L Thompson 
8331c66c397SJeremy L Thompson           s = 1 / sqrt(1 + tau * tau);
8341c66c397SJeremy L Thompson           c = s * tau;
835ba59ac12SSebastian Grimberg         } else {
8361c66c397SJeremy L Thompson           const CeedScalar tau = -z / x;
8371c66c397SJeremy L Thompson 
8381c66c397SJeremy L Thompson           c = 1 / sqrt(1 + tau * tau);
8391c66c397SJeremy L Thompson           s = c * tau;
840ba59ac12SSebastian Grimberg         }
841ba59ac12SSebastian Grimberg       }
842ba59ac12SSebastian Grimberg 
843ba59ac12SSebastian Grimberg       // Apply Givens rotation to T
844ba59ac12SSebastian Grimberg       CeedGivensRotation(mat_T, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n);
845ba59ac12SSebastian Grimberg       CeedGivensRotation(mat_T, c, s, CEED_TRANSPOSE, k, k + 1, n, n);
846ba59ac12SSebastian Grimberg 
847ba59ac12SSebastian Grimberg       // Apply Givens rotation to Q
848ba59ac12SSebastian Grimberg       CeedGivensRotation(mat, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n);
849ba59ac12SSebastian Grimberg 
850ba59ac12SSebastian Grimberg       // Update x, z
851ba59ac12SSebastian Grimberg       if (k < n - q - 2) {
852ba59ac12SSebastian Grimberg         x = mat_T[k + n * (k + 1)];
853ba59ac12SSebastian Grimberg         z = mat_T[k + n * (k + 2)];
854ba59ac12SSebastian Grimberg       }
855ba59ac12SSebastian Grimberg     }
856ba59ac12SSebastian Grimberg     itr++;
857ba59ac12SSebastian Grimberg   }
858ba59ac12SSebastian Grimberg 
859ba59ac12SSebastian Grimberg   // Save eigenvalues
860ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) lambda[i] = mat_T[i + n * i];
861ba59ac12SSebastian Grimberg 
862ba59ac12SSebastian Grimberg   // Check convergence
8636574a04fSJeremy L Thompson   CeedCheck(itr < max_itr || q > n, ceed, CEED_ERROR_MINOR, "Symmetric QR failed to converge");
864ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
865ba59ac12SSebastian Grimberg }
8662c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
867ba59ac12SSebastian Grimberg 
868ba59ac12SSebastian Grimberg /**
869ba59ac12SSebastian Grimberg   @brief Return Simultaneous Diagonalization of two matrices.
870ba59ac12SSebastian Grimberg 
871ca94c3ddSJeremy L Thompson   This solves the generalized eigenvalue problem `A x = lambda B x`, where `A` and `B` are symmetric and `B` is positive definite.
872ca94c3ddSJeremy L Thompson   We generate the matrix `X` and vector `Lambda` such that `X^T A X = Lambda` and `X^T B X = I`.
873ca94c3ddSJeremy L Thompson   This is equivalent to the LAPACK routine 'sygv' with `TYPE = 1`.
874ba59ac12SSebastian Grimberg 
875ca94c3ddSJeremy L Thompson   @param[in]  ceed   `Ceed` context for error handling
876ba59ac12SSebastian Grimberg   @param[in]  mat_A  Row-major matrix to be factorized with eigenvalues
877ba59ac12SSebastian Grimberg   @param[in]  mat_B  Row-major matrix to be factorized to identity
878ba59ac12SSebastian Grimberg   @param[out] mat_X  Row-major orthogonal matrix
879ca94c3ddSJeremy L Thompson   @param[out] lambda Vector of length `n` of generalized eigenvalues
880ba59ac12SSebastian Grimberg   @param[in]  n      Number of rows/columns
881ba59ac12SSebastian Grimberg 
882ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
883ba59ac12SSebastian Grimberg 
884ba59ac12SSebastian Grimberg   @ref Utility
885ba59ac12SSebastian Grimberg **/
8862c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
8872c2ea1dbSJeremy L Thompson int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A, CeedScalar *mat_B, CeedScalar *mat_X, CeedScalar *lambda, CeedInt n) {
888ba59ac12SSebastian Grimberg   CeedScalar *mat_C, *mat_G, *vec_D;
8891c66c397SJeremy L Thompson 
890ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n * n, &mat_C));
891ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n * n, &mat_G));
892ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n, &vec_D));
893ba59ac12SSebastian Grimberg 
894ba59ac12SSebastian Grimberg   // Compute B = G D G^T
895ba59ac12SSebastian Grimberg   memcpy(mat_G, mat_B, n * n * sizeof(mat_B[0]));
896ba59ac12SSebastian Grimberg   CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_G, vec_D, n));
897ba59ac12SSebastian Grimberg 
898ba59ac12SSebastian Grimberg   // Sort eigenvalues
899ba59ac12SSebastian Grimberg   for (CeedInt i = n - 1; i >= 0; i--) {
900ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < i; j++) {
901ba59ac12SSebastian Grimberg       if (fabs(vec_D[j]) > fabs(vec_D[j + 1])) {
9021c66c397SJeremy L Thompson         CeedScalarSwap(vec_D[j], vec_D[j + 1]);
9031c66c397SJeremy L Thompson         for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_G[k * n + j], mat_G[k * n + j + 1]);
904ba59ac12SSebastian Grimberg       }
905ba59ac12SSebastian Grimberg     }
906ba59ac12SSebastian Grimberg   }
907ba59ac12SSebastian Grimberg 
908ba59ac12SSebastian Grimberg   // Compute C = (G D^1/2)^-1 A (G D^1/2)^-T
909ba59ac12SSebastian Grimberg   //           = D^-1/2 G^T A G D^-1/2
910ba59ac12SSebastian Grimberg   // -- D = D^-1/2
911ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) vec_D[i] = 1. / sqrt(vec_D[i]);
912ba59ac12SSebastian Grimberg   // -- G = G D^-1/2
913ba59ac12SSebastian Grimberg   // -- C = D^-1/2 G^T
914ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
915ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < n; j++) {
916ba59ac12SSebastian Grimberg       mat_G[i * n + j] *= vec_D[j];
917ba59ac12SSebastian Grimberg       mat_C[j * n + i] = mat_G[i * n + j];
918ba59ac12SSebastian Grimberg     }
919ba59ac12SSebastian Grimberg   }
920ba59ac12SSebastian Grimberg   // -- X = (D^-1/2 G^T) A
921ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_C, (const CeedScalar *)mat_A, mat_X, n, n, n));
922ba59ac12SSebastian Grimberg   // -- C = (D^-1/2 G^T A) (G D^-1/2)
923ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_X, (const CeedScalar *)mat_G, mat_C, n, n, n));
924ba59ac12SSebastian Grimberg 
925ba59ac12SSebastian Grimberg   // Compute Q^T C Q = lambda
926ba59ac12SSebastian Grimberg   CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_C, lambda, n));
927ba59ac12SSebastian Grimberg 
928ba59ac12SSebastian Grimberg   // Sort eigenvalues
929ba59ac12SSebastian Grimberg   for (CeedInt i = n - 1; i >= 0; i--) {
930ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < i; j++) {
931ba59ac12SSebastian Grimberg       if (fabs(lambda[j]) > fabs(lambda[j + 1])) {
9321c66c397SJeremy L Thompson         CeedScalarSwap(lambda[j], lambda[j + 1]);
9331c66c397SJeremy L Thompson         for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_C[k * n + j], mat_C[k * n + j + 1]);
934ba59ac12SSebastian Grimberg       }
935ba59ac12SSebastian Grimberg     }
936ba59ac12SSebastian Grimberg   }
937ba59ac12SSebastian Grimberg 
938ba59ac12SSebastian Grimberg   // Set X = (G D^1/2)^-T Q
939ba59ac12SSebastian Grimberg   //       = G D^-1/2 Q
940ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_G, (const CeedScalar *)mat_C, mat_X, n, n, n));
941ba59ac12SSebastian Grimberg 
942ba59ac12SSebastian Grimberg   // Cleanup
943ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&mat_C));
944ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&mat_G));
945ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&vec_D));
946ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
947ba59ac12SSebastian Grimberg }
9482c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
949ba59ac12SSebastian Grimberg 
9507a982d89SJeremy L. Thompson /// @}
9517a982d89SJeremy L. Thompson 
9527a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
9537a982d89SJeremy L. Thompson /// CeedBasis Public API
9547a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
9557a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser
956d7b241e6Sjeremylt /// @{
957d7b241e6Sjeremylt 
958b11c1e72Sjeremylt /**
959ca94c3ddSJeremy L Thompson   @brief Create a tensor-product basis for \f$H^1\f$ discretizations
960b11c1e72Sjeremylt 
961ca94c3ddSJeremy L Thompson   @param[in]  ceed        `Ceed` object used to create the `CeedBasis`
962ea61e9acSJeremy L Thompson   @param[in]  dim         Topological dimension
963ea61e9acSJeremy L Thompson   @param[in]  num_comp    Number of field components (1 for scalar fields)
964ea61e9acSJeremy L Thompson   @param[in]  P_1d        Number of nodes in one dimension
965ea61e9acSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
966ca94c3ddSJeremy L Thompson   @param[in]  interp_1d   Row-major (`Q_1d * P_1d`) matrix expressing the values of nodal basis functions at quadrature points
967ca94c3ddSJeremy L Thompson   @param[in]  grad_1d     Row-major (`Q_1d * P_1d`) matrix expressing derivatives of nodal basis functions at quadrature points
968ca94c3ddSJeremy 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]`
969ca94c3ddSJeremy L Thompson   @param[in]  q_weight_1d Array of length `Q_1d` holding the quadrature weights on the reference element
970ca94c3ddSJeremy L Thompson   @param[out] basis       Address of the variable where the newly created `CeedBasis` will be stored
971b11c1e72Sjeremylt 
972b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
973dfdf5a53Sjeremylt 
9747a982d89SJeremy L. Thompson   @ref User
975b11c1e72Sjeremylt **/
9762b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d,
9772b730f8bSJeremy L Thompson                             const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis *basis) {
9785fe0d4faSjeremylt   if (!ceed->BasisCreateTensorH1) {
9795fe0d4faSjeremylt     Ceed delegate;
9806574a04fSJeremy L Thompson 
9812b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
9826574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisCreateTensorH1");
9832b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis));
984e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
9855fe0d4faSjeremylt   }
986e15f9bd0SJeremy L Thompson 
987ca94c3ddSJeremy L Thompson   CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value");
988ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
989ca94c3ddSJeremy L Thompson   CeedCheck(P_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
990ca94c3ddSJeremy L Thompson   CeedCheck(Q_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
991227444bfSJeremy L Thompson 
9922b730f8bSJeremy L Thompson   CeedElemTopology topo = dim == 1 ? CEED_TOPOLOGY_LINE : dim == 2 ? CEED_TOPOLOGY_QUAD : CEED_TOPOLOGY_HEX;
993e15f9bd0SJeremy L Thompson 
9942b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
995db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed));
996d1d35e2fSjeremylt   (*basis)->ref_count       = 1;
9976402da51SJeremy L Thompson   (*basis)->is_tensor_basis = true;
998d7b241e6Sjeremylt   (*basis)->dim             = dim;
999d99fa3c5SJeremy L Thompson   (*basis)->topo            = topo;
1000d1d35e2fSjeremylt   (*basis)->num_comp        = num_comp;
1001d1d35e2fSjeremylt   (*basis)->P_1d            = P_1d;
1002d1d35e2fSjeremylt   (*basis)->Q_1d            = Q_1d;
1003d1d35e2fSjeremylt   (*basis)->P               = CeedIntPow(P_1d, dim);
1004d1d35e2fSjeremylt   (*basis)->Q               = CeedIntPow(Q_1d, dim);
1005c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_H1;
10062b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d, &(*basis)->q_ref_1d));
10072b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d, &(*basis)->q_weight_1d));
1008ff3a0f91SJeremy L Thompson   if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d * sizeof(q_ref_1d[0]));
10092b730f8bSJeremy L Thompson   if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, Q_1d * sizeof(q_weight_1d[0]));
10102b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->interp_1d));
10112b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->grad_1d));
10122b730f8bSJeremy L Thompson   if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, Q_1d * P_1d * sizeof(interp_1d[0]));
1013ff3a0f91SJeremy L Thompson   if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d * P_1d * sizeof(grad_1d[0]));
10142b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, *basis));
1015e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1016d7b241e6Sjeremylt }
1017d7b241e6Sjeremylt 
1018b11c1e72Sjeremylt /**
1019ca94c3ddSJeremy L Thompson   @brief Create a tensor-product \f$H^1\f$ Lagrange basis
1020b11c1e72Sjeremylt 
1021ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` object used to create the `CeedBasis`
1022ea61e9acSJeremy L Thompson   @param[in]  dim       Topological dimension of element
1023ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of field components (1 for scalar fields)
1024ea61e9acSJeremy L Thompson   @param[in]  P         Number of Gauss-Lobatto nodes in one dimension.
1025ca94c3ddSJeremy L Thompson                           The polynomial degree of the resulting `Q_k` element is `k = P - 1`.
1026ea61e9acSJeremy L Thompson   @param[in]  Q         Number of quadrature points in one dimension.
1027ca94c3ddSJeremy L Thompson   @param[in]  quad_mode Distribution of the `Q` quadrature points (affects order of accuracy for the quadrature)
1028ca94c3ddSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created `CeedBasis` will be stored
1029b11c1e72Sjeremylt 
1030b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1031dfdf5a53Sjeremylt 
10327a982d89SJeremy L. Thompson   @ref User
1033b11c1e72Sjeremylt **/
10342b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis) {
1035d7b241e6Sjeremylt   // Allocate
1036c8c3fa7dSJeremy L Thompson   int        ierr = CEED_ERROR_SUCCESS;
10372b730f8bSJeremy L Thompson   CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, *q_weight_1d;
10384d537eeaSYohann 
1039ca94c3ddSJeremy L Thompson   CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value");
1040ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1041ca94c3ddSJeremy L Thompson   CeedCheck(P > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1042ca94c3ddSJeremy L Thompson   CeedCheck(Q > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1043227444bfSJeremy L Thompson 
1044e15f9bd0SJeremy L Thompson   // Get Nodes and Weights
10452b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P * Q, &interp_1d));
10462b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P * Q, &grad_1d));
10472b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P, &nodes));
10482b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &q_ref_1d));
10492b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &q_weight_1d));
10502b730f8bSJeremy L Thompson   if (CeedLobattoQuadrature(P, nodes, NULL) != CEED_ERROR_SUCCESS) goto cleanup;
1051d1d35e2fSjeremylt   switch (quad_mode) {
1052d7b241e6Sjeremylt     case CEED_GAUSS:
1053d1d35e2fSjeremylt       ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d);
1054d7b241e6Sjeremylt       break;
1055d7b241e6Sjeremylt     case CEED_GAUSS_LOBATTO:
1056d1d35e2fSjeremylt       ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d);
1057d7b241e6Sjeremylt       break;
1058d7b241e6Sjeremylt   }
10592b730f8bSJeremy L Thompson   if (ierr != CEED_ERROR_SUCCESS) goto cleanup;
1060e15f9bd0SJeremy L Thompson 
1061d7b241e6Sjeremylt   // Build B, D matrix
1062d7b241e6Sjeremylt   // Fornberg, 1998
1063c8c3fa7dSJeremy L Thompson   for (CeedInt i = 0; i < Q; i++) {
1064d7b241e6Sjeremylt     c1                   = 1.0;
1065d1d35e2fSjeremylt     c3                   = nodes[0] - q_ref_1d[i];
1066d1d35e2fSjeremylt     interp_1d[i * P + 0] = 1.0;
1067c8c3fa7dSJeremy L Thompson     for (CeedInt j = 1; j < P; j++) {
1068d7b241e6Sjeremylt       c2 = 1.0;
1069d7b241e6Sjeremylt       c4 = c3;
1070d1d35e2fSjeremylt       c3 = nodes[j] - q_ref_1d[i];
1071c8c3fa7dSJeremy L Thompson       for (CeedInt k = 0; k < j; k++) {
1072d7b241e6Sjeremylt         dx = nodes[j] - nodes[k];
1073d7b241e6Sjeremylt         c2 *= dx;
1074d7b241e6Sjeremylt         if (k == j - 1) {
1075d1d35e2fSjeremylt           grad_1d[i * P + j]   = c1 * (interp_1d[i * P + k] - c4 * grad_1d[i * P + k]) / c2;
1076d1d35e2fSjeremylt           interp_1d[i * P + j] = -c1 * c4 * interp_1d[i * P + k] / c2;
1077d7b241e6Sjeremylt         }
1078d1d35e2fSjeremylt         grad_1d[i * P + k]   = (c3 * grad_1d[i * P + k] - interp_1d[i * P + k]) / dx;
1079d1d35e2fSjeremylt         interp_1d[i * P + k] = c3 * interp_1d[i * P + k] / dx;
1080d7b241e6Sjeremylt       }
1081d7b241e6Sjeremylt       c1 = c2;
1082d7b241e6Sjeremylt     }
1083d7b241e6Sjeremylt   }
10849ac7b42eSJeremy L Thompson   // Pass to CeedBasisCreateTensorH1
10852b730f8bSJeremy L Thompson   CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis));
1086e15f9bd0SJeremy L Thompson cleanup:
10872b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_1d));
10882b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_1d));
10892b730f8bSJeremy L Thompson   CeedCall(CeedFree(&nodes));
10902b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref_1d));
10912b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight_1d));
1092e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1093d7b241e6Sjeremylt }
1094d7b241e6Sjeremylt 
1095b11c1e72Sjeremylt /**
1096ca94c3ddSJeremy L Thompson   @brief Create a non tensor-product basis for \f$H^1\f$ discretizations
1097a8de75f0Sjeremylt 
1098ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` object used to create the `CeedBasis`
1099ea61e9acSJeremy L Thompson   @param[in]  topo      Topology of element, e.g. hypercube, simplex, ect
1100ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of field components (1 for scalar fields)
1101ea61e9acSJeremy L Thompson   @param[in]  num_nodes Total number of nodes
1102ea61e9acSJeremy L Thompson   @param[in]  num_qpts  Total number of quadrature points
1103ca94c3ddSJeremy L Thompson   @param[in]  interp    Row-major (`num_qpts * num_nodes`) matrix expressing the values of nodal basis functions at quadrature points
1104ca94c3ddSJeremy L Thompson   @param[in]  grad      Row-major (`dim * num_qpts * num_nodes`) matrix expressing derivatives of nodal basis functions at quadrature points
1105ca94c3ddSJeremy L Thompson   @param[in]  q_ref     Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element
1106ca94c3ddSJeremy L Thompson   @param[in]  q_weight  Array of length `num_qpts` holding the quadrature weights on the reference element
1107ca94c3ddSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created `CeedBasis` will be stored
1108a8de75f0Sjeremylt 
1109a8de75f0Sjeremylt   @return An error code: 0 - success, otherwise - failure
1110a8de75f0Sjeremylt 
11117a982d89SJeremy L. Thompson   @ref User
1112a8de75f0Sjeremylt **/
11132b730f8bSJeremy L Thompson int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
11142b730f8bSJeremy L Thompson                       const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
1115d1d35e2fSjeremylt   CeedInt P = num_nodes, Q = num_qpts, dim = 0;
1116a8de75f0Sjeremylt 
11175fe0d4faSjeremylt   if (!ceed->BasisCreateH1) {
11185fe0d4faSjeremylt     Ceed delegate;
11196574a04fSJeremy L Thompson 
11202b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
11216574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisCreateH1");
11222b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis));
1123e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
11245fe0d4faSjeremylt   }
11255fe0d4faSjeremylt 
1126ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1127ca94c3ddSJeremy L Thompson   CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1128ca94c3ddSJeremy L Thompson   CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1129227444bfSJeremy L Thompson 
11302b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1131a8de75f0Sjeremylt 
1132db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1133db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed));
1134d1d35e2fSjeremylt   (*basis)->ref_count       = 1;
11356402da51SJeremy L Thompson   (*basis)->is_tensor_basis = false;
1136a8de75f0Sjeremylt   (*basis)->dim             = dim;
1137d99fa3c5SJeremy L Thompson   (*basis)->topo            = topo;
1138d1d35e2fSjeremylt   (*basis)->num_comp        = num_comp;
1139a8de75f0Sjeremylt   (*basis)->P               = P;
1140a8de75f0Sjeremylt   (*basis)->Q               = Q;
1141c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_H1;
11422b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q * dim, &(*basis)->q_ref_1d));
11432b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &(*basis)->q_weight_1d));
1144ff3a0f91SJeremy L Thompson   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
1145ff3a0f91SJeremy L Thompson   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
11462b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q * P, &(*basis)->interp));
11472b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(dim * Q * P, &(*basis)->grad));
1148ff3a0f91SJeremy L Thompson   if (interp) memcpy((*basis)->interp, interp, Q * P * sizeof(interp[0]));
1149ff3a0f91SJeremy L Thompson   if (grad) memcpy((*basis)->grad, grad, dim * Q * P * sizeof(grad[0]));
11502b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, *basis));
1151e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1152a8de75f0Sjeremylt }
1153a8de75f0Sjeremylt 
1154a8de75f0Sjeremylt /**
1155859c15bbSJames Wright   @brief Create a non tensor-product basis for \f$H(\mathrm{div})\f$ discretizations
115650c301a5SRezgar Shakeri 
1157ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` object used to create the `CeedBasis`
1158ea61e9acSJeremy 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
1159ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of components (usually 1 for vectors in H(div) bases)
1160ca94c3ddSJeremy L Thompson   @param[in]  num_nodes Total number of nodes (DoFs per element)
1161ea61e9acSJeremy L Thompson   @param[in]  num_qpts  Total number of quadrature points
1162ca94c3ddSJeremy L Thompson   @param[in]  interp    Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points
1163ca94c3ddSJeremy L Thompson   @param[in]  div       Row-major (`num_qpts * num_nodes`) matrix expressing divergence of basis functions at quadrature points
1164ca94c3ddSJeremy L Thompson   @param[in]  q_ref     Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element
1165ca94c3ddSJeremy L Thompson   @param[in]  q_weight  Array of length `num_qpts` holding the quadrature weights on the reference element
1166ca94c3ddSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created `CeedBasis` will be stored
116750c301a5SRezgar Shakeri 
116850c301a5SRezgar Shakeri   @return An error code: 0 - success, otherwise - failure
116950c301a5SRezgar Shakeri 
117050c301a5SRezgar Shakeri   @ref User
117150c301a5SRezgar Shakeri **/
11722b730f8bSJeremy L Thompson int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
11732b730f8bSJeremy L Thompson                         const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
117450c301a5SRezgar Shakeri   CeedInt Q = num_qpts, P = num_nodes, dim = 0;
1175c4e3f59bSSebastian Grimberg 
117650c301a5SRezgar Shakeri   if (!ceed->BasisCreateHdiv) {
117750c301a5SRezgar Shakeri     Ceed delegate;
11786574a04fSJeremy L Thompson 
11792b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
11806574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv");
11812b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis));
118250c301a5SRezgar Shakeri     return CEED_ERROR_SUCCESS;
118350c301a5SRezgar Shakeri   }
118450c301a5SRezgar Shakeri 
1185ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1186ca94c3ddSJeremy L Thompson   CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1187ca94c3ddSJeremy L Thompson   CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1188227444bfSJeremy L Thompson 
1189c4e3f59bSSebastian Grimberg   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1190c4e3f59bSSebastian Grimberg 
1191db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1192db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed));
119350c301a5SRezgar Shakeri   (*basis)->ref_count       = 1;
11946402da51SJeremy L Thompson   (*basis)->is_tensor_basis = false;
119550c301a5SRezgar Shakeri   (*basis)->dim             = dim;
119650c301a5SRezgar Shakeri   (*basis)->topo            = topo;
119750c301a5SRezgar Shakeri   (*basis)->num_comp        = num_comp;
119850c301a5SRezgar Shakeri   (*basis)->P               = P;
119950c301a5SRezgar Shakeri   (*basis)->Q               = Q;
1200c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_HDIV;
12012b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d));
12022b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d));
120350c301a5SRezgar Shakeri   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
120450c301a5SRezgar Shakeri   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
12052b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp));
12062b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q * P, &(*basis)->div));
120750c301a5SRezgar Shakeri   if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0]));
120850c301a5SRezgar Shakeri   if (div) memcpy((*basis)->div, div, Q * P * sizeof(div[0]));
12092b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, q_weight, *basis));
121050c301a5SRezgar Shakeri   return CEED_ERROR_SUCCESS;
121150c301a5SRezgar Shakeri }
121250c301a5SRezgar Shakeri 
121350c301a5SRezgar Shakeri /**
12144385fb7fSSebastian Grimberg   @brief Create a non tensor-product basis for \f$H(\mathrm{curl})\f$ discretizations
1215c4e3f59bSSebastian Grimberg 
1216ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` object used to create the `CeedBasis`
1217c4e3f59bSSebastian Grimberg   @param[in]  topo      Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below
1218ca94c3ddSJeremy L Thompson   @param[in]  num_comp  Number of components (usually 1 for vectors in \f$H(\mathrm{curl})\f$ bases)
1219ca94c3ddSJeremy L Thompson   @param[in]  num_nodes Total number of nodes (DoFs per element)
1220c4e3f59bSSebastian Grimberg   @param[in]  num_qpts  Total number of quadrature points
1221ca94c3ddSJeremy L Thompson   @param[in]  interp    Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points
1222ca94c3ddSJeremy 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
1223ca94c3ddSJeremy L Thompson   @param[in]  q_ref     Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element
1224ca94c3ddSJeremy L Thompson   @param[in]  q_weight  Array of length `num_qpts` holding the quadrature weights on the reference element
1225ca94c3ddSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created `CeedBasis` will be stored
1226c4e3f59bSSebastian Grimberg 
1227c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1228c4e3f59bSSebastian Grimberg 
1229c4e3f59bSSebastian Grimberg   @ref User
1230c4e3f59bSSebastian Grimberg **/
1231c4e3f59bSSebastian Grimberg int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
1232c4e3f59bSSebastian Grimberg                          const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
1233c4e3f59bSSebastian Grimberg   CeedInt Q = num_qpts, P = num_nodes, dim = 0, curl_comp = 0;
1234c4e3f59bSSebastian Grimberg 
1235d075f50bSSebastian Grimberg   if (!ceed->BasisCreateHcurl) {
1236c4e3f59bSSebastian Grimberg     Ceed delegate;
12376574a04fSJeremy L Thompson 
1238c4e3f59bSSebastian Grimberg     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
12396574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl");
1240c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis));
1241c4e3f59bSSebastian Grimberg     return CEED_ERROR_SUCCESS;
1242c4e3f59bSSebastian Grimberg   }
1243c4e3f59bSSebastian Grimberg 
1244ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1245ca94c3ddSJeremy L Thompson   CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1246ca94c3ddSJeremy L Thompson   CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1247c4e3f59bSSebastian Grimberg 
1248c4e3f59bSSebastian Grimberg   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1249c4e3f59bSSebastian Grimberg   curl_comp = (dim < 3) ? 1 : dim;
1250c4e3f59bSSebastian Grimberg 
1251db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1252db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed));
1253c4e3f59bSSebastian Grimberg   (*basis)->ref_count       = 1;
12546402da51SJeremy L Thompson   (*basis)->is_tensor_basis = false;
1255c4e3f59bSSebastian Grimberg   (*basis)->dim             = dim;
1256c4e3f59bSSebastian Grimberg   (*basis)->topo            = topo;
1257c4e3f59bSSebastian Grimberg   (*basis)->num_comp        = num_comp;
1258c4e3f59bSSebastian Grimberg   (*basis)->P               = P;
1259c4e3f59bSSebastian Grimberg   (*basis)->Q               = Q;
1260c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_HCURL;
1261c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d));
1262c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d));
1263c4e3f59bSSebastian Grimberg   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
1264c4e3f59bSSebastian Grimberg   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
1265c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp));
1266c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(curl_comp * Q * P, &(*basis)->curl));
1267c4e3f59bSSebastian Grimberg   if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0]));
1268c4e3f59bSSebastian Grimberg   if (curl) memcpy((*basis)->curl, curl, curl_comp * Q * P * sizeof(curl[0]));
1269c4e3f59bSSebastian Grimberg   CeedCall(ceed->BasisCreateHcurl(topo, dim, P, Q, interp, curl, q_ref, q_weight, *basis));
1270c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1271c4e3f59bSSebastian Grimberg }
1272c4e3f59bSSebastian Grimberg 
1273c4e3f59bSSebastian Grimberg /**
1274ca94c3ddSJeremy L Thompson   @brief Create a `CeedBasis` for projection from the nodes of `basis_from` to the nodes of `basis_to`.
1275ba59ac12SSebastian Grimberg 
1276ca94c3ddSJeremy L Thompson   Only @ref CEED_EVAL_INTERP will be valid for the new basis, `basis_project`.
1277ca94c3ddSJeremy L Thompson   For \f$H^1\f$ spaces, @ref CEED_EVAL_GRAD will also be valid.
1278ca94c3ddSJeremy L Thompson   The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization.
1279ca94c3ddSJeremy L Thompson   The gradient (for the \f$H^1\f$ case) is given by `grad_project = interp_to^+ * grad_from`.
128015ad3917SSebastian Grimberg 
128115ad3917SSebastian Grimberg   Note: `basis_from` and `basis_to` must have compatible quadrature spaces.
128215ad3917SSebastian Grimberg 
12839fd66db6SSebastian Grimberg   Note: `basis_project` will have the same number of components as `basis_from`, regardless of the number of components that `basis_to` has.
12849fd66db6SSebastian Grimberg         If `basis_from` has 3 components and `basis_to` has 5 components, then `basis_project` will have 3 components.
1285f113e5dcSJeremy L Thompson 
1286ca94c3ddSJeremy L Thompson   @param[in]  basis_from    `CeedBasis` to prolong from
1287ca94c3ddSJeremy L Thompson   @param[in]  basis_to      `CeedBasis` to prolong to
1288ca94c3ddSJeremy L Thompson   @param[out] basis_project Address of the variable where the newly created `CeedBasis` will be stored
1289f113e5dcSJeremy L Thompson 
1290f113e5dcSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1291f113e5dcSJeremy L Thompson 
1292f113e5dcSJeremy L Thompson   @ref User
1293f113e5dcSJeremy L Thompson **/
12942b730f8bSJeremy L Thompson int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project) {
1295f113e5dcSJeremy L Thompson   Ceed        ceed;
12961c66c397SJeremy L Thompson   bool        is_tensor;
12971c66c397SJeremy L Thompson   CeedInt     dim, num_comp;
12981c66c397SJeremy L Thompson   CeedScalar *q_ref, *q_weight, *interp_project, *grad_project;
12991c66c397SJeremy L Thompson 
13002b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis_to, &ceed));
1301f113e5dcSJeremy L Thompson 
1302ecc88aebSJeremy L Thompson   // Create projection matrix
13032b730f8bSJeremy L Thompson   CeedCall(CeedBasisCreateProjectionMatrices(basis_from, basis_to, &interp_project, &grad_project));
1304f113e5dcSJeremy L Thompson 
1305f113e5dcSJeremy L Thompson   // Build basis
13062b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis_to, &is_tensor));
13072b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis_to, &dim));
13082b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_from, &num_comp));
1309f113e5dcSJeremy L Thompson   if (is_tensor) {
1310f113e5dcSJeremy L Thompson     CeedInt P_1d_to, P_1d_from;
13111c66c397SJeremy L Thompson 
13122b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_1d_from));
13132b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_1d_to));
13142b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_to, &q_ref));
13152b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_to, &q_weight));
13162b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, interp_project, grad_project, q_ref, q_weight, basis_project));
1317f113e5dcSJeremy L Thompson   } else {
1318de05fbb2SSebastian Grimberg     // Even if basis_to and basis_from are not H1, the resulting basis is H1 for interpolation to work
1319f113e5dcSJeremy L Thompson     CeedInt          num_nodes_to, num_nodes_from;
13201c66c397SJeremy L Thompson     CeedElemTopology topo;
13211c66c397SJeremy L Thompson 
13221c66c397SJeremy L Thompson     CeedCall(CeedBasisGetTopology(basis_to, &topo));
13232b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_from, &num_nodes_from));
13242b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_to, &num_nodes_to));
13252b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_to * dim, &q_ref));
13262b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_to, &q_weight));
13272b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, q_ref, q_weight, basis_project));
1328f113e5dcSJeremy L Thompson   }
1329f113e5dcSJeremy L Thompson 
1330f113e5dcSJeremy L Thompson   // Cleanup
13312b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_project));
13322b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_project));
13332b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref));
13342b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight));
1335f113e5dcSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1336f113e5dcSJeremy L Thompson }
1337f113e5dcSJeremy L Thompson 
1338f113e5dcSJeremy L Thompson /**
1339ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedBasis`.
13409560d06aSjeremylt 
1341ca94c3ddSJeremy 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`.
1342ca94c3ddSJeremy L Thompson         This `CeedBasis` will be destroyed if `*basis_copy` is the only reference to this `CeedBasis`.
1343ea61e9acSJeremy L Thompson 
1344ca94c3ddSJeremy L Thompson   @param[in]     basis      `CeedBasis` to copy reference to
1345ea61e9acSJeremy L Thompson   @param[in,out] basis_copy Variable to store copied reference
13469560d06aSjeremylt 
13479560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
13489560d06aSjeremylt 
13499560d06aSjeremylt   @ref User
13509560d06aSjeremylt **/
13519560d06aSjeremylt int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) {
1352356036faSJeremy L Thompson   if (basis != CEED_BASIS_NONE) CeedCall(CeedBasisReference(basis));
13532b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(basis_copy));
13549560d06aSjeremylt   *basis_copy = basis;
13559560d06aSjeremylt   return CEED_ERROR_SUCCESS;
13569560d06aSjeremylt }
13579560d06aSjeremylt 
13589560d06aSjeremylt /**
1359ca94c3ddSJeremy L Thompson   @brief View a `CeedBasis`
13607a982d89SJeremy L. Thompson 
1361ca94c3ddSJeremy L Thompson   @param[in] basis  `CeedBasis` to view
1362ca94c3ddSJeremy L Thompson   @param[in] stream Stream to view to, e.g., `stdout`
13637a982d89SJeremy L. Thompson 
13647a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
13657a982d89SJeremy L. Thompson 
13667a982d89SJeremy L. Thompson   @ref User
13677a982d89SJeremy L. Thompson **/
13687a982d89SJeremy L. Thompson int CeedBasisView(CeedBasis basis, FILE *stream) {
1369*1203703bSJeremy L Thompson   bool             is_tensor_basis;
1370*1203703bSJeremy L Thompson   CeedElemTopology topo;
1371*1203703bSJeremy L Thompson   CeedFESpace      fe_space;
1372*1203703bSJeremy L Thompson 
1373*1203703bSJeremy L Thompson   // Basis data
1374*1203703bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
1375*1203703bSJeremy L Thompson   CeedCall(CeedBasisGetTopology(basis, &topo));
1376*1203703bSJeremy L Thompson   CeedCall(CeedBasisGetFESpace(basis, &fe_space));
13772b730f8bSJeremy L Thompson 
137850c301a5SRezgar Shakeri   // Print FE space and element topology of the basis
1379edf04919SJeremy L Thompson   fprintf(stream, "CeedBasis in a %s on a %s element\n", CeedFESpaces[fe_space], CeedElemTopologies[topo]);
1380*1203703bSJeremy L Thompson   if (is_tensor_basis) {
1381edf04919SJeremy L Thompson     fprintf(stream, "  P: %" CeedInt_FMT "\n  Q: %" CeedInt_FMT "\n", basis->P_1d, basis->Q_1d);
138250c301a5SRezgar Shakeri   } else {
1383edf04919SJeremy L Thompson     fprintf(stream, "  P: %" CeedInt_FMT "\n  Q: %" CeedInt_FMT "\n", basis->P, basis->Q);
138450c301a5SRezgar Shakeri   }
1385edf04919SJeremy L Thompson   fprintf(stream, "  dimension: %" CeedInt_FMT "\n  field components: %" CeedInt_FMT "\n", basis->dim, basis->num_comp);
1386ea61e9acSJeremy L Thompson   // Print quadrature data, interpolation/gradient/divergence/curl of the basis
1387*1203703bSJeremy L Thompson   if (is_tensor_basis) {  // tensor basis
1388*1203703bSJeremy L Thompson     CeedInt           P_1d, Q_1d;
1389*1203703bSJeremy L Thompson     const CeedScalar *q_ref_1d, *q_weight_1d, *interp_1d, *grad_1d;
1390*1203703bSJeremy L Thompson 
1391*1203703bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
1392*1203703bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
1393*1203703bSJeremy L Thompson     CeedCall(CeedBasisGetQRef(basis, &q_ref_1d));
1394*1203703bSJeremy L Thompson     CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d));
1395*1203703bSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
1396*1203703bSJeremy L Thompson     CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
1397*1203703bSJeremy L Thompson 
1398*1203703bSJeremy L Thompson     CeedCall(CeedScalarView("qref1d", "\t% 12.8f", 1, Q_1d, q_ref_1d, stream));
1399*1203703bSJeremy L Thompson     CeedCall(CeedScalarView("qweight1d", "\t% 12.8f", 1, Q_1d, q_weight_1d, stream));
1400*1203703bSJeremy L Thompson     CeedCall(CeedScalarView("interp1d", "\t% 12.8f", Q_1d, P_1d, interp_1d, stream));
1401*1203703bSJeremy L Thompson     CeedCall(CeedScalarView("grad1d", "\t% 12.8f", Q_1d, P_1d, grad_1d, stream));
140250c301a5SRezgar Shakeri   } else {  // non-tensor basis
1403*1203703bSJeremy L Thompson     CeedInt           P, Q, dim, q_comp;
1404*1203703bSJeremy L Thompson     const CeedScalar *q_ref, *q_weight, *interp, *grad, *div, *curl;
1405*1203703bSJeremy L Thompson 
1406*1203703bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis, &P));
1407*1203703bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(basis, &Q));
1408*1203703bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis, &dim));
1409*1203703bSJeremy L Thompson     CeedCall(CeedBasisGetQRef(basis, &q_ref));
1410*1203703bSJeremy L Thompson     CeedCall(CeedBasisGetQWeights(basis, &q_weight));
1411*1203703bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(basis, &interp));
1412*1203703bSJeremy L Thompson     CeedCall(CeedBasisGetGrad(basis, &grad));
1413*1203703bSJeremy L Thompson     CeedCall(CeedBasisGetDiv(basis, &div));
1414*1203703bSJeremy L Thompson     CeedCall(CeedBasisGetCurl(basis, &curl));
1415*1203703bSJeremy L Thompson 
1416*1203703bSJeremy L Thompson     CeedCall(CeedScalarView("qref", "\t% 12.8f", 1, Q * dim, q_ref, stream));
1417*1203703bSJeremy L Thompson     CeedCall(CeedScalarView("qweight", "\t% 12.8f", 1, Q, q_weight, stream));
1418c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp));
1419*1203703bSJeremy L Thompson     CeedCall(CeedScalarView("interp", "\t% 12.8f", q_comp * Q, P, interp, stream));
1420*1203703bSJeremy L Thompson     if (grad) {
1421c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp));
1422*1203703bSJeremy L Thompson       CeedCall(CeedScalarView("grad", "\t% 12.8f", q_comp * Q, P, grad, stream));
14237a982d89SJeremy L. Thompson     }
1424*1203703bSJeremy L Thompson     if (div) {
1425c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp));
1426*1203703bSJeremy L Thompson       CeedCall(CeedScalarView("div", "\t% 12.8f", q_comp * Q, P, div, stream));
1427c4e3f59bSSebastian Grimberg     }
1428*1203703bSJeremy L Thompson     if (curl) {
1429c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp));
1430*1203703bSJeremy L Thompson       CeedCall(CeedScalarView("curl", "\t% 12.8f", q_comp * Q, P, curl, stream));
143150c301a5SRezgar Shakeri     }
143250c301a5SRezgar Shakeri   }
1433e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
14347a982d89SJeremy L. Thompson }
14357a982d89SJeremy L. Thompson 
14367a982d89SJeremy L. Thompson /**
14377a982d89SJeremy L. Thompson   @brief Apply basis evaluation from nodes to quadrature points or vice versa
14387a982d89SJeremy L. Thompson 
1439ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis` to evaluate
1440ea61e9acSJeremy L Thompson   @param[in]  num_elem  The number of elements to apply the basis evaluation to;
1441ca94c3ddSJeremy L Thompson                           the backend will specify the ordering in @ref CeedElemRestrictionCreate()
1442ca94c3ddSJeremy L Thompson   @param[in]  t_mode    @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points;
1443ca94c3ddSJeremy L Thompson                           @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes
1444ca94c3ddSJeremy L Thompson   @param[in]  eval_mode @ref CEED_EVAL_NONE to use values directly,
1445ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_INTERP to use interpolated values,
1446ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_GRAD to use gradients,
1447ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_DIV to use divergence,
1448ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_CURL to use curl,
1449ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_WEIGHT to use quadrature weights
1450ca94c3ddSJeremy L Thompson   @param[in]  u         Input `CeedVector`
1451ca94c3ddSJeremy L Thompson   @param[out] v         Output `CeedVector`
14527a982d89SJeremy L. Thompson 
14537a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
14547a982d89SJeremy L. Thompson 
14557a982d89SJeremy L. Thompson   @ref User
14567a982d89SJeremy L. Thompson **/
14572b730f8bSJeremy L Thompson int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) {
1458c4e3f59bSSebastian Grimberg   CeedInt  dim, num_comp, q_comp, num_nodes, num_qpts;
14591c66c397SJeremy L Thompson   CeedSize u_length = 0, v_length;
1460*1203703bSJeremy L Thompson   Ceed     ceed;
14611c66c397SJeremy L Thompson 
1462*1203703bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis, &ceed));
14632b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
14642b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
1465c4e3f59bSSebastian Grimberg   CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
14662b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
14672b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
14682b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(v, &v_length));
1469c8c3fa7dSJeremy L Thompson   if (u) CeedCall(CeedVectorGetLength(u, &u_length));
14707a982d89SJeremy L. Thompson 
1471*1203703bSJeremy L Thompson   CeedCheck(basis->Apply, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedBasisApply");
1472e15f9bd0SJeremy L Thompson 
1473e15f9bd0SJeremy L Thompson   // Check compatibility of topological and geometrical dimensions
14746574a04fSJeremy L Thompson   CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0 && u_length % num_qpts == 0) ||
14756574a04fSJeremy L Thompson                 (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0 && v_length % num_qpts == 0),
1476*1203703bSJeremy L Thompson             ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions");
14777a982d89SJeremy L. Thompson 
1478e15f9bd0SJeremy L Thompson   // Check vector lengths to prevent out of bounds issues
14796574a04fSJeremy L Thompson   bool good_dims = true;
1480d1d35e2fSjeremylt   switch (eval_mode) {
1481e15f9bd0SJeremy L Thompson     case CEED_EVAL_NONE:
14822b730f8bSJeremy L Thompson     case CEED_EVAL_INTERP:
14832b730f8bSJeremy L Thompson     case CEED_EVAL_GRAD:
1484c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
1485c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
14866574a04fSJeremy L Thompson       good_dims =
14876574a04fSJeremy L Thompson           ((t_mode == CEED_TRANSPOSE && u_length >= num_elem * num_comp * num_qpts * q_comp && v_length >= num_elem * num_comp * num_nodes) ||
14886574a04fSJeremy L Thompson            (t_mode == CEED_NOTRANSPOSE && v_length >= num_elem * num_qpts * num_comp * q_comp && u_length >= num_elem * num_comp * num_nodes));
1489e15f9bd0SJeremy L Thompson       break;
1490e15f9bd0SJeremy L Thompson     case CEED_EVAL_WEIGHT:
14916574a04fSJeremy L Thompson       good_dims = v_length >= num_elem * num_qpts;
1492e15f9bd0SJeremy L Thompson       break;
1493e15f9bd0SJeremy L Thompson   }
1494*1203703bSJeremy L Thompson   CeedCheck(good_dims, ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode");
1495e15f9bd0SJeremy L Thompson 
14962b730f8bSJeremy L Thompson   CeedCall(basis->Apply(basis, num_elem, t_mode, eval_mode, u, v));
1497e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
14987a982d89SJeremy L. Thompson }
14997a982d89SJeremy L. Thompson 
15007a982d89SJeremy L. Thompson /**
1501c8c3fa7dSJeremy L Thompson   @brief Apply basis evaluation from nodes to arbitrary points
1502c8c3fa7dSJeremy L Thompson 
1503ca94c3ddSJeremy L Thompson   @param[in]  basis      `CeedBasis` to evaluate
1504c8c3fa7dSJeremy L Thompson   @param[in]  num_points The number of points to apply the basis evaluation to
1505ca94c3ddSJeremy L Thompson   @param[in]  t_mode     @ref CEED_NOTRANSPOSE to evaluate from nodes to points;
1506ca94c3ddSJeremy L Thompson                            @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes
1507ca94c3ddSJeremy L Thompson   @param[in]  eval_mode  @ref CEED_EVAL_INTERP to use interpolated values,
1508ca94c3ddSJeremy L Thompson                            @ref CEED_EVAL_GRAD to use gradients,
1509ca94c3ddSJeremy L Thompson                            @ref CEED_EVAL_WEIGHT to use quadrature weights
1510ca94c3ddSJeremy L Thompson   @param[in]  x_ref      `CeedVector` holding reference coordinates of each point
1511ca94c3ddSJeremy L Thompson   @param[in]  u          Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE
1512ca94c3ddSJeremy L Thompson   @param[out] v          Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP
1513c8c3fa7dSJeremy L Thompson 
1514c8c3fa7dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1515c8c3fa7dSJeremy L Thompson 
1516c8c3fa7dSJeremy L Thompson   @ref User
1517c8c3fa7dSJeremy L Thompson **/
1518c8c3fa7dSJeremy L Thompson int CeedBasisApplyAtPoints(CeedBasis basis, CeedInt num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u,
1519c8c3fa7dSJeremy L Thompson                            CeedVector v) {
1520*1203703bSJeremy L Thompson   bool     is_tensor_basis;
1521c8c3fa7dSJeremy L Thompson   CeedInt  dim, num_comp, num_q_comp, num_nodes, P_1d = 1, Q_1d = 1;
15221c66c397SJeremy L Thompson   CeedSize x_length = 0, u_length = 0, v_length;
1523*1203703bSJeremy L Thompson   Ceed     ceed;
1524c8c3fa7dSJeremy L Thompson 
1525*1203703bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis, &ceed));
1526c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
1527c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
1528c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
1529c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
1530c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &num_q_comp));
1531c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
1532c8c3fa7dSJeremy L Thompson   CeedCall(CeedVectorGetLength(v, &v_length));
1533953190f4SJeremy L Thompson   if (x_ref != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(x_ref, &x_length));
1534953190f4SJeremy L Thompson   if (u != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(u, &u_length));
1535c8c3fa7dSJeremy L Thompson 
1536c8c3fa7dSJeremy L Thompson   // Check compatibility of topological and geometrical dimensions
1537953190f4SJeremy L Thompson   CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0) || (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0) ||
1538953190f4SJeremy L Thompson                 (eval_mode == CEED_EVAL_WEIGHT),
1539*1203703bSJeremy L Thompson             ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions and number of points");
1540c8c3fa7dSJeremy L Thompson 
1541c8c3fa7dSJeremy L Thompson   // Check compatibility coordinates vector
1542*1203703bSJeremy L Thompson   CeedCheck((x_length >= num_points * dim) || (eval_mode == CEED_EVAL_WEIGHT), ceed, CEED_ERROR_DIMENSION,
1543c8c3fa7dSJeremy L Thompson             "Length of reference coordinate vector incompatible with basis dimension and number of points");
1544c8c3fa7dSJeremy L Thompson 
1545953190f4SJeremy L Thompson   // Check CEED_EVAL_WEIGHT only on CEED_NOTRANSPOSE
1546*1203703bSJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT || t_mode == CEED_NOTRANSPOSE, ceed, CEED_ERROR_UNSUPPORTED,
1547953190f4SJeremy L Thompson             "CEED_EVAL_WEIGHT only supported with CEED_NOTRANSPOSE");
1548953190f4SJeremy L Thompson 
1549c8c3fa7dSJeremy L Thompson   // Check vector lengths to prevent out of bounds issues
1550c8c3fa7dSJeremy L Thompson   bool good_dims = false;
1551c8c3fa7dSJeremy L Thompson   switch (eval_mode) {
1552c8c3fa7dSJeremy L Thompson     case CEED_EVAL_INTERP:
1553c8c3fa7dSJeremy L Thompson       good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= num_points * num_q_comp || v_length >= num_nodes * num_comp)) ||
1554c8c3fa7dSJeremy L Thompson                    (t_mode == CEED_NOTRANSPOSE && (v_length >= num_points * num_q_comp || u_length >= num_nodes * num_comp)));
1555c8c3fa7dSJeremy L Thompson       break;
1556c8c3fa7dSJeremy L Thompson     case CEED_EVAL_GRAD:
1557edfbf3a6SJeremy L Thompson       good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= num_points * num_q_comp * dim || v_length >= num_nodes * num_comp)) ||
1558edfbf3a6SJeremy L Thompson                    (t_mode == CEED_NOTRANSPOSE && (v_length >= num_points * num_q_comp * dim || u_length >= num_nodes * num_comp)));
1559edfbf3a6SJeremy L Thompson       break;
1560c8c3fa7dSJeremy L Thompson     case CEED_EVAL_WEIGHT:
1561953190f4SJeremy L Thompson       good_dims = t_mode == CEED_NOTRANSPOSE && (v_length >= num_points);
1562953190f4SJeremy L Thompson       break;
1563953190f4SJeremy L Thompson     case CEED_EVAL_NONE:
1564c8c3fa7dSJeremy L Thompson     case CEED_EVAL_DIV:
1565c8c3fa7dSJeremy L Thompson     case CEED_EVAL_CURL:
1566c8c3fa7dSJeremy L Thompson       // LCOV_EXCL_START
1567*1203703bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points not supported for %s", CeedEvalModes[eval_mode]);
1568c8c3fa7dSJeremy L Thompson       // LCOV_EXCL_STOP
1569c8c3fa7dSJeremy L Thompson   }
1570*1203703bSJeremy L Thompson   CeedCheck(good_dims, ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode");
1571c8c3fa7dSJeremy L Thompson 
1572c8c3fa7dSJeremy L Thompson   // Backend method
1573c8c3fa7dSJeremy L Thompson   if (basis->ApplyAtPoints) {
1574c8c3fa7dSJeremy L Thompson     CeedCall(basis->ApplyAtPoints(basis, num_points, t_mode, eval_mode, x_ref, u, v));
1575c8c3fa7dSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1576c8c3fa7dSJeremy L Thompson   }
1577c8c3fa7dSJeremy L Thompson 
1578c8c3fa7dSJeremy L Thompson   // Default implementation
1579*1203703bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
1580*1203703bSJeremy L Thompson   CeedCheck(is_tensor_basis, ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points only supported for tensor product bases");
1581953190f4SJeremy L Thompson   if (eval_mode == CEED_EVAL_WEIGHT) {
1582953190f4SJeremy L Thompson     CeedCall(CeedVectorSetValue(v, 1.0));
1583953190f4SJeremy L Thompson     return CEED_ERROR_SUCCESS;
1584953190f4SJeremy L Thompson   }
1585c8c3fa7dSJeremy L Thompson   if (!basis->basis_chebyshev) {
1586c8c3fa7dSJeremy L Thompson     // Build matrix mapping from quadrature point values to Chebyshev coefficients
15872247a93fSRezgar Shakeri     CeedScalar       *C, *chebyshev_coeffs_1d_inv;
1588c8c3fa7dSJeremy L Thompson     const CeedScalar *q_ref_1d;
1589c8c3fa7dSJeremy L Thompson 
1590c8c3fa7dSJeremy L Thompson     // Build coefficient matrix
1591c8c3fa7dSJeremy L Thompson     // -- Note: Clang-tidy needs this check because it does not understand the is_tensor_basis check above
1592*1203703bSJeremy L Thompson     CeedCheck(P_1d > 0 && Q_1d > 0, ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis dimensions are malformed");
1593c8c3fa7dSJeremy L Thompson     CeedCall(CeedCalloc(Q_1d * Q_1d, &C));
1594c8c3fa7dSJeremy L Thompson     CeedCall(CeedBasisGetQRef(basis, &q_ref_1d));
15953778dbaaSJeremy L Thompson     for (CeedInt i = 0; i < Q_1d; i++) CeedCall(CeedChebyshevPolynomialsAtPoint(q_ref_1d[i], Q_1d, &C[i * Q_1d]));
1596c8c3fa7dSJeremy L Thompson 
15972247a93fSRezgar Shakeri     // Compute C^+, pseudoinverse of coefficient matrix
15982247a93fSRezgar Shakeri     CeedCall(CeedCalloc(Q_1d * Q_1d, &chebyshev_coeffs_1d_inv));
1599*1203703bSJeremy L Thompson     CeedCall(CeedMatrixPseudoinverse(ceed, C, Q_1d, Q_1d, chebyshev_coeffs_1d_inv));
1600c8c3fa7dSJeremy L Thompson 
1601c8c3fa7dSJeremy L Thompson     // Build basis mapping from nodes to Chebyshev coefficients
1602c8c3fa7dSJeremy L Thompson     CeedScalar       *chebyshev_interp_1d, *chebyshev_grad_1d, *chebyshev_q_weight_1d;
1603c8c3fa7dSJeremy L Thompson     const CeedScalar *interp_1d;
1604c8c3fa7dSJeremy L Thompson 
160571a83b88SJeremy L Thompson     CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
160671a83b88SJeremy L Thompson     CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_grad_1d));
1607c8c3fa7dSJeremy L Thompson     CeedCall(CeedCalloc(Q_1d, &chebyshev_q_weight_1d));
1608c8c3fa7dSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
1609*1203703bSJeremy L Thompson     CeedCall(CeedMatrixMatrixMultiply(ceed, chebyshev_coeffs_1d_inv, interp_1d, chebyshev_interp_1d, Q_1d, P_1d, Q_1d));
1610c8c3fa7dSJeremy L Thompson 
1611*1203703bSJeremy L Thompson     CeedCall(CeedVectorCreate(ceed, num_comp * CeedIntPow(Q_1d, dim), &basis->vec_chebyshev));
1612*1203703bSJeremy 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,
1613c8c3fa7dSJeremy L Thompson                                      &basis->basis_chebyshev));
1614c8c3fa7dSJeremy L Thompson 
1615c8c3fa7dSJeremy L Thompson     // Cleanup
1616c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&C));
16172247a93fSRezgar Shakeri     CeedCall(CeedFree(&chebyshev_coeffs_1d_inv));
1618c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&chebyshev_interp_1d));
1619c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&chebyshev_grad_1d));
1620c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&chebyshev_q_weight_1d));
1621c8c3fa7dSJeremy L Thompson   }
1622c8c3fa7dSJeremy L Thompson 
1623c8c3fa7dSJeremy L Thompson   // Create TensorContract object if needed, such as a basis from the GPU backends
1624c8c3fa7dSJeremy L Thompson   if (!basis->contract) {
1625c8c3fa7dSJeremy L Thompson     Ceed      ceed_ref;
1626585a562dSJeremy L Thompson     CeedBasis basis_ref = NULL;
1627c8c3fa7dSJeremy L Thompson 
1628c8c3fa7dSJeremy L Thompson     CeedCall(CeedInit("/cpu/self", &ceed_ref));
1629c8c3fa7dSJeremy L Thompson     // Only need matching tensor contraction dimensions, any type of basis will work
163071a83b88SJeremy L Thompson     CeedCall(CeedBasisCreateTensorH1Lagrange(ceed_ref, dim, num_comp, P_1d, Q_1d, CEED_GAUSS, &basis_ref));
1631585a562dSJeremy L Thompson     // Note - clang-tidy doesn't know basis_ref->contract must be valid here
1632*1203703bSJeremy L Thompson     CeedCheck(basis_ref && basis_ref->contract, ceed, CEED_ERROR_UNSUPPORTED, "Reference CPU ceed failed to create a tensor contraction object");
1633585a562dSJeremy L Thompson     CeedCall(CeedTensorContractReferenceCopy(basis_ref->contract, &basis->contract));
1634c8c3fa7dSJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis_ref));
1635c8c3fa7dSJeremy L Thompson     CeedCall(CeedDestroy(&ceed_ref));
1636c8c3fa7dSJeremy L Thompson   }
1637c8c3fa7dSJeremy L Thompson 
1638c8c3fa7dSJeremy L Thompson   // Basis evaluation
1639c8c3fa7dSJeremy L Thompson   switch (t_mode) {
1640c8c3fa7dSJeremy L Thompson     case CEED_NOTRANSPOSE: {
1641c8c3fa7dSJeremy L Thompson       // Nodes to arbitrary points
1642c8c3fa7dSJeremy L Thompson       CeedScalar       *v_array;
1643c8c3fa7dSJeremy L Thompson       const CeedScalar *chebyshev_coeffs, *x_array_read;
1644c8c3fa7dSJeremy L Thompson 
1645c8c3fa7dSJeremy L Thompson       // -- Interpolate to Chebyshev coefficients
1646c8c3fa7dSJeremy L Thompson       CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, basis->vec_chebyshev));
1647c8c3fa7dSJeremy L Thompson 
1648c8c3fa7dSJeremy L Thompson       // -- Evaluate Chebyshev polynomials at arbitrary points
1649c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs));
1650c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read));
1651c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorGetArrayWrite(v, CEED_MEM_HOST, &v_array));
1652edfbf3a6SJeremy L Thompson       switch (eval_mode) {
1653edfbf3a6SJeremy L Thompson         case CEED_EVAL_INTERP: {
1654c8c3fa7dSJeremy L Thompson           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
1655c8c3fa7dSJeremy L Thompson 
1656c8c3fa7dSJeremy L Thompson           // ---- Values at point
1657c8c3fa7dSJeremy L Thompson           for (CeedInt p = 0; p < num_points; p++) {
1658c8c3fa7dSJeremy L Thompson             CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1;
1659c8c3fa7dSJeremy L Thompson 
166053ef2869SZach Atkins             for (CeedInt d = 0; d < dim; d++) {
16613778dbaaSJeremy L Thompson               // ------ Tensor contract with current Chebyshev polynomial values
16629c34f28eSJeremy L Thompson               CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x));
1663c8c3fa7dSJeremy L Thompson               CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false,
16644608bdaaSJeremy L Thompson                                                d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2]));
1665c8c3fa7dSJeremy L Thompson               pre /= Q_1d;
1666c8c3fa7dSJeremy L Thompson               post *= 1;
1667c8c3fa7dSJeremy L Thompson             }
16684608bdaaSJeremy L Thompson             for (CeedInt c = 0; c < num_comp; c++) v_array[c * num_points + p] = tmp[dim % 2][c];
1669c8c3fa7dSJeremy L Thompson           }
1670edfbf3a6SJeremy L Thompson           break;
1671edfbf3a6SJeremy L Thompson         }
1672edfbf3a6SJeremy L Thompson         case CEED_EVAL_GRAD: {
1673edfbf3a6SJeremy L Thompson           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
1674edfbf3a6SJeremy L Thompson 
1675edfbf3a6SJeremy L Thompson           // ---- Values at point
1676edfbf3a6SJeremy L Thompson           for (CeedInt p = 0; p < num_points; p++) {
1677edfbf3a6SJeremy L Thompson             // Dim**2 contractions, apply grad when pass == dim
167853ef2869SZach Atkins             for (CeedInt pass = 0; pass < dim; pass++) {
1679edfbf3a6SJeremy L Thompson               CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1;
1680edfbf3a6SJeremy L Thompson 
168153ef2869SZach Atkins               for (CeedInt d = 0; d < dim; d++) {
16823778dbaaSJeremy L Thompson                 // ------ Tensor contract with current Chebyshev polynomial values
16839c34f28eSJeremy L Thompson                 if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x));
16849c34f28eSJeremy L Thompson                 else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x));
1685edfbf3a6SJeremy L Thompson                 CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false,
16864608bdaaSJeremy L Thompson                                                  d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2]));
1687edfbf3a6SJeremy L Thompson                 pre /= Q_1d;
1688edfbf3a6SJeremy L Thompson                 post *= 1;
1689edfbf3a6SJeremy L Thompson               }
16904608bdaaSJeremy L Thompson               for (CeedInt c = 0; c < num_comp; c++) v_array[(pass * num_comp + c) * num_points + p] = tmp[dim % 2][c];
1691edfbf3a6SJeremy L Thompson             }
1692edfbf3a6SJeremy L Thompson           }
1693edfbf3a6SJeremy L Thompson           break;
1694edfbf3a6SJeremy L Thompson         }
1695edfbf3a6SJeremy L Thompson         default:
1696953190f4SJeremy L Thompson           // Nothing to do, excluded above
1697edfbf3a6SJeremy L Thompson           break;
1698c8c3fa7dSJeremy L Thompson       }
1699c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(basis->vec_chebyshev, &chebyshev_coeffs));
1700c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read));
1701c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorRestoreArray(v, &v_array));
1702c8c3fa7dSJeremy L Thompson       break;
1703c8c3fa7dSJeremy L Thompson     }
17042a94f45fSJeremy L Thompson     case CEED_TRANSPOSE: {
17053778dbaaSJeremy L Thompson       // Note: No switch on e_mode here because only CEED_EVAL_INTERP is supported at this time
17062a94f45fSJeremy L Thompson       // Arbitrary points to nodes
17072a94f45fSJeremy L Thompson       CeedScalar       *chebyshev_coeffs;
17082a94f45fSJeremy L Thompson       const CeedScalar *u_array, *x_array_read;
17092a94f45fSJeremy L Thompson 
17101c66c397SJeremy L Thompson       // -- Transpose of evaluation of Chebyshev polynomials at arbitrary points
17112a94f45fSJeremy L Thompson       CeedCall(CeedVectorGetArrayWrite(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs));
17122a94f45fSJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read));
17132a94f45fSJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array));
1714038a8942SZach Atkins 
1715038a8942SZach Atkins       switch (eval_mode) {
1716038a8942SZach Atkins         case CEED_EVAL_INTERP: {
17172a94f45fSJeremy L Thompson           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
17182a94f45fSJeremy L Thompson 
17192a94f45fSJeremy L Thompson           // ---- Values at point
17202a94f45fSJeremy L Thompson           for (CeedInt p = 0; p < num_points; p++) {
17212a94f45fSJeremy L Thompson             CeedInt pre = num_comp * 1, post = 1;
17222a94f45fSJeremy L Thompson 
17234608bdaaSJeremy L Thompson             for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[c * num_points + p];
172453ef2869SZach Atkins             for (CeedInt d = 0; d < dim; d++) {
17253778dbaaSJeremy L Thompson               // ------ Tensor contract with current Chebyshev polynomial values
17269c34f28eSJeremy L Thompson               CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x));
17274608bdaaSJeremy L Thompson               CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, p > 0 && d == (dim - 1), tmp[d % 2],
17284608bdaaSJeremy L Thompson                                                d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2]));
17292a94f45fSJeremy L Thompson               pre /= 1;
17302a94f45fSJeremy L Thompson               post *= Q_1d;
17312a94f45fSJeremy L Thompson             }
17322a94f45fSJeremy L Thompson           }
1733038a8942SZach Atkins           break;
1734038a8942SZach Atkins         }
1735038a8942SZach Atkins         case CEED_EVAL_GRAD: {
1736038a8942SZach Atkins           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
1737038a8942SZach Atkins 
1738038a8942SZach Atkins           // ---- Values at point
1739038a8942SZach Atkins           for (CeedInt p = 0; p < num_points; p++) {
1740038a8942SZach Atkins             // Dim**2 contractions, apply grad when pass == dim
1741038a8942SZach Atkins             for (CeedInt pass = 0; pass < dim; pass++) {
1742038a8942SZach Atkins               CeedInt pre = num_comp * 1, post = 1;
1743038a8942SZach Atkins 
17444608bdaaSJeremy L Thompson               for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[(pass * num_comp + c) * num_points + p];
1745038a8942SZach Atkins               for (CeedInt d = 0; d < dim; d++) {
1746038a8942SZach Atkins                 // ------ Tensor contract with current Chebyshev polynomial values
17479c34f28eSJeremy L Thompson                 if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x));
17489c34f28eSJeremy L Thompson                 else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x));
17494608bdaaSJeremy L Thompson                 CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode,
17504608bdaaSJeremy L Thompson                                                  (p > 0 || (p == 0 && pass > 0)) && d == (dim - 1), tmp[d % 2],
17514608bdaaSJeremy L Thompson                                                  d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2]));
1752038a8942SZach Atkins                 pre /= 1;
1753038a8942SZach Atkins                 post *= Q_1d;
1754038a8942SZach Atkins               }
1755038a8942SZach Atkins             }
1756038a8942SZach Atkins           }
1757038a8942SZach Atkins           break;
1758038a8942SZach Atkins         }
1759038a8942SZach Atkins         default:
1760038a8942SZach Atkins           // Nothing to do, excluded above
1761038a8942SZach Atkins           break;
17622a94f45fSJeremy L Thompson       }
17632a94f45fSJeremy L Thompson       CeedCall(CeedVectorRestoreArray(basis->vec_chebyshev, &chebyshev_coeffs));
17642a94f45fSJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read));
17652a94f45fSJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(u, &u_array));
17662a94f45fSJeremy L Thompson 
17672a94f45fSJeremy L Thompson       // -- Interpolate transpose from Chebyshev coefficients
17682a94f45fSJeremy L Thompson       CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v));
17692a94f45fSJeremy L Thompson       break;
17702a94f45fSJeremy L Thompson     }
1771c8c3fa7dSJeremy L Thompson   }
1772c8c3fa7dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1773c8c3fa7dSJeremy L Thompson }
1774c8c3fa7dSJeremy L Thompson 
1775c8c3fa7dSJeremy L Thompson /**
1776ca94c3ddSJeremy L Thompson   @brief Get `Ceed` associated with a `CeedBasis`
1777b7c9bbdaSJeremy L Thompson 
1778ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
1779ca94c3ddSJeremy L Thompson   @param[out] ceed  Variable to store `Ceed`
1780b7c9bbdaSJeremy L Thompson 
1781b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1782b7c9bbdaSJeremy L Thompson 
1783b7c9bbdaSJeremy L Thompson   @ref Advanced
1784b7c9bbdaSJeremy L Thompson **/
1785b7c9bbdaSJeremy L Thompson int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) {
1786b7c9bbdaSJeremy L Thompson   *ceed = basis->ceed;
1787b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1788b7c9bbdaSJeremy L Thompson }
1789b7c9bbdaSJeremy L Thompson 
1790b7c9bbdaSJeremy L Thompson /**
1791ca94c3ddSJeremy L Thompson   @brief Get dimension for given `CeedBasis`
17929d007619Sjeremylt 
1793ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
17949d007619Sjeremylt   @param[out] dim   Variable to store dimension of basis
17959d007619Sjeremylt 
17969d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
17979d007619Sjeremylt 
1798b7c9bbdaSJeremy L Thompson   @ref Advanced
17999d007619Sjeremylt **/
18009d007619Sjeremylt int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) {
18019d007619Sjeremylt   *dim = basis->dim;
1802e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18039d007619Sjeremylt }
18049d007619Sjeremylt 
18059d007619Sjeremylt /**
1806ca94c3ddSJeremy L Thompson   @brief Get topology for given `CeedBasis`
1807d99fa3c5SJeremy L Thompson 
1808ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
1809d99fa3c5SJeremy L Thompson   @param[out] topo  Variable to store topology of basis
1810d99fa3c5SJeremy L Thompson 
1811d99fa3c5SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1812d99fa3c5SJeremy L Thompson 
1813b7c9bbdaSJeremy L Thompson   @ref Advanced
1814d99fa3c5SJeremy L Thompson **/
1815d99fa3c5SJeremy L Thompson int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) {
1816d99fa3c5SJeremy L Thompson   *topo = basis->topo;
1817e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1818d99fa3c5SJeremy L Thompson }
1819d99fa3c5SJeremy L Thompson 
1820d99fa3c5SJeremy L Thompson /**
1821ca94c3ddSJeremy L Thompson   @brief Get number of components for given `CeedBasis`
18229d007619Sjeremylt 
1823ca94c3ddSJeremy L Thompson   @param[in]  basis    `CeedBasis`
1824ca94c3ddSJeremy L Thompson   @param[out] num_comp Variable to store number of components
18259d007619Sjeremylt 
18269d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18279d007619Sjeremylt 
1828b7c9bbdaSJeremy L Thompson   @ref Advanced
18299d007619Sjeremylt **/
1830d1d35e2fSjeremylt int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) {
1831d1d35e2fSjeremylt   *num_comp = basis->num_comp;
1832e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18339d007619Sjeremylt }
18349d007619Sjeremylt 
18359d007619Sjeremylt /**
1836ca94c3ddSJeremy L Thompson   @brief Get total number of nodes (in `dim` dimensions) of a `CeedBasis`
18379d007619Sjeremylt 
1838ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
18399d007619Sjeremylt   @param[out] P     Variable to store number of nodes
18409d007619Sjeremylt 
18419d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18429d007619Sjeremylt 
18439d007619Sjeremylt   @ref Utility
18449d007619Sjeremylt **/
18459d007619Sjeremylt int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) {
18469d007619Sjeremylt   *P = basis->P;
1847e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18489d007619Sjeremylt }
18499d007619Sjeremylt 
18509d007619Sjeremylt /**
1851ca94c3ddSJeremy L Thompson   @brief Get total number of nodes (in 1 dimension) of a `CeedBasis`
18529d007619Sjeremylt 
1853ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
1854d1d35e2fSjeremylt   @param[out] P_1d  Variable to store number of nodes
18559d007619Sjeremylt 
18569d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18579d007619Sjeremylt 
1858b7c9bbdaSJeremy L Thompson   @ref Advanced
18599d007619Sjeremylt **/
1860d1d35e2fSjeremylt int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) {
1861*1203703bSJeremy L Thompson   Ceed ceed;
1862*1203703bSJeremy L Thompson 
1863*1203703bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis, &ceed));
1864*1203703bSJeremy L Thompson   CeedCheck(basis->is_tensor_basis, ceed, CEED_ERROR_MINOR, "Cannot supply P_1d for non-tensor CeedBasis");
1865d1d35e2fSjeremylt   *P_1d = basis->P_1d;
1866e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18679d007619Sjeremylt }
18689d007619Sjeremylt 
18699d007619Sjeremylt /**
1870ca94c3ddSJeremy L Thompson   @brief Get total number of quadrature points (in `dim` dimensions) of a `CeedBasis`
18719d007619Sjeremylt 
1872ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
18739d007619Sjeremylt   @param[out] Q     Variable to store number of quadrature points
18749d007619Sjeremylt 
18759d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18769d007619Sjeremylt 
18779d007619Sjeremylt   @ref Utility
18789d007619Sjeremylt **/
18799d007619Sjeremylt int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) {
18809d007619Sjeremylt   *Q = basis->Q;
1881e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18829d007619Sjeremylt }
18839d007619Sjeremylt 
18849d007619Sjeremylt /**
1885ca94c3ddSJeremy L Thompson   @brief Get total number of quadrature points (in 1 dimension) of a `CeedBasis`
18869d007619Sjeremylt 
1887ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
1888d1d35e2fSjeremylt   @param[out] Q_1d  Variable to store number of quadrature points
18899d007619Sjeremylt 
18909d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18919d007619Sjeremylt 
1892b7c9bbdaSJeremy L Thompson   @ref Advanced
18939d007619Sjeremylt **/
1894d1d35e2fSjeremylt int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) {
1895*1203703bSJeremy L Thompson   Ceed ceed;
1896*1203703bSJeremy L Thompson 
1897*1203703bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis, &ceed));
1898*1203703bSJeremy L Thompson   CeedCheck(basis->is_tensor_basis, ceed, CEED_ERROR_MINOR, "Cannot supply Q_1d for non-tensor CeedBasis");
1899d1d35e2fSjeremylt   *Q_1d = basis->Q_1d;
1900e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
19019d007619Sjeremylt }
19029d007619Sjeremylt 
19039d007619Sjeremylt /**
1904ca94c3ddSJeremy L Thompson   @brief Get reference coordinates of quadrature points (in `dim` dimensions) of a `CeedBasis`
19059d007619Sjeremylt 
1906ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
1907d1d35e2fSjeremylt   @param[out] q_ref Variable to store reference coordinates of quadrature points
19089d007619Sjeremylt 
19099d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
19109d007619Sjeremylt 
1911b7c9bbdaSJeremy L Thompson   @ref Advanced
19129d007619Sjeremylt **/
1913d1d35e2fSjeremylt int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) {
1914d1d35e2fSjeremylt   *q_ref = basis->q_ref_1d;
1915e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
19169d007619Sjeremylt }
19179d007619Sjeremylt 
19189d007619Sjeremylt /**
1919ca94c3ddSJeremy L Thompson   @brief Get quadrature weights of quadrature points (in `dim` dimensions) of a `CeedBasis`
19209d007619Sjeremylt 
1921ca94c3ddSJeremy L Thompson   @param[in]  basis    `CeedBasis`
1922d1d35e2fSjeremylt   @param[out] q_weight Variable to store quadrature weights
19239d007619Sjeremylt 
19249d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
19259d007619Sjeremylt 
1926b7c9bbdaSJeremy L Thompson   @ref Advanced
19279d007619Sjeremylt **/
1928d1d35e2fSjeremylt int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) {
1929d1d35e2fSjeremylt   *q_weight = basis->q_weight_1d;
1930e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
19319d007619Sjeremylt }
19329d007619Sjeremylt 
19339d007619Sjeremylt /**
1934ca94c3ddSJeremy L Thompson   @brief Get interpolation matrix of a `CeedBasis`
19359d007619Sjeremylt 
1936ca94c3ddSJeremy L Thompson   @param[in]  basis  `CeedBasis`
19379d007619Sjeremylt   @param[out] interp Variable to store interpolation matrix
19389d007619Sjeremylt 
19399d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
19409d007619Sjeremylt 
1941b7c9bbdaSJeremy L Thompson   @ref Advanced
19429d007619Sjeremylt **/
19436c58de82SJeremy L Thompson int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) {
19446402da51SJeremy L Thompson   if (!basis->interp && basis->is_tensor_basis) {
19459d007619Sjeremylt     // Allocate
19462b730f8bSJeremy L Thompson     CeedCall(CeedMalloc(basis->Q * basis->P, &basis->interp));
19479d007619Sjeremylt 
19489d007619Sjeremylt     // Initialize
19492b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < basis->Q * basis->P; i++) basis->interp[i] = 1.0;
19509d007619Sjeremylt 
19519d007619Sjeremylt     // Calculate
19522b730f8bSJeremy L Thompson     for (CeedInt d = 0; d < basis->dim; d++) {
19532b730f8bSJeremy L Thompson       for (CeedInt qpt = 0; qpt < basis->Q; qpt++) {
19549d007619Sjeremylt         for (CeedInt node = 0; node < basis->P; node++) {
1955d1d35e2fSjeremylt           CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d;
1956d1d35e2fSjeremylt           CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d;
19571c66c397SJeremy L Thompson 
1958d1d35e2fSjeremylt           basis->interp[qpt * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p];
19599d007619Sjeremylt         }
19609d007619Sjeremylt       }
19612b730f8bSJeremy L Thompson     }
19622b730f8bSJeremy L Thompson   }
19639d007619Sjeremylt   *interp = basis->interp;
1964e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
19659d007619Sjeremylt }
19669d007619Sjeremylt 
19679d007619Sjeremylt /**
1968ca94c3ddSJeremy L Thompson   @brief Get 1D interpolation matrix of a tensor product `CeedBasis`
19699d007619Sjeremylt 
1970ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis`
1971d1d35e2fSjeremylt   @param[out] interp_1d Variable to store interpolation matrix
19729d007619Sjeremylt 
19739d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
19749d007619Sjeremylt 
19759d007619Sjeremylt   @ref Backend
19769d007619Sjeremylt **/
1977d1d35e2fSjeremylt int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) {
1978*1203703bSJeremy L Thompson   bool is_tensor_basis;
1979*1203703bSJeremy L Thompson   Ceed ceed;
1980*1203703bSJeremy L Thompson 
1981*1203703bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis, &ceed));
1982*1203703bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
1983*1203703bSJeremy L Thompson   CeedCheck(is_tensor_basis, ceed, CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis");
1984d1d35e2fSjeremylt   *interp_1d = basis->interp_1d;
1985e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
19869d007619Sjeremylt }
19879d007619Sjeremylt 
19889d007619Sjeremylt /**
1989ca94c3ddSJeremy L Thompson   @brief Get gradient matrix of a `CeedBasis`
19909d007619Sjeremylt 
1991ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
19929d007619Sjeremylt   @param[out] grad  Variable to store gradient matrix
19939d007619Sjeremylt 
19949d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
19959d007619Sjeremylt 
1996b7c9bbdaSJeremy L Thompson   @ref Advanced
19979d007619Sjeremylt **/
19986c58de82SJeremy L Thompson int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) {
19996402da51SJeremy L Thompson   if (!basis->grad && basis->is_tensor_basis) {
20009d007619Sjeremylt     // Allocate
20012b730f8bSJeremy L Thompson     CeedCall(CeedMalloc(basis->dim * basis->Q * basis->P, &basis->grad));
20029d007619Sjeremylt 
20039d007619Sjeremylt     // Initialize
20042b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < basis->dim * basis->Q * basis->P; i++) basis->grad[i] = 1.0;
20059d007619Sjeremylt 
20069d007619Sjeremylt     // Calculate
20072b730f8bSJeremy L Thompson     for (CeedInt d = 0; d < basis->dim; d++) {
20082b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < basis->dim; i++) {
20092b730f8bSJeremy L Thompson         for (CeedInt qpt = 0; qpt < basis->Q; qpt++) {
20109d007619Sjeremylt           for (CeedInt node = 0; node < basis->P; node++) {
2011d1d35e2fSjeremylt             CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d;
2012d1d35e2fSjeremylt             CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d;
20131c66c397SJeremy L Thompson 
20142b730f8bSJeremy L Thompson             if (i == d) basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->grad_1d[q * basis->P_1d + p];
20152b730f8bSJeremy L Thompson             else basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p];
20162b730f8bSJeremy L Thompson           }
20172b730f8bSJeremy L Thompson         }
20182b730f8bSJeremy L Thompson       }
20199d007619Sjeremylt     }
20209d007619Sjeremylt   }
20219d007619Sjeremylt   *grad = basis->grad;
2022e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
20239d007619Sjeremylt }
20249d007619Sjeremylt 
20259d007619Sjeremylt /**
2026ca94c3ddSJeremy L Thompson   @brief Get 1D gradient matrix of a tensor product `CeedBasis`
20279d007619Sjeremylt 
2028ca94c3ddSJeremy L Thompson   @param[in]  basis   `CeedBasis`
2029d1d35e2fSjeremylt   @param[out] grad_1d Variable to store gradient matrix
20309d007619Sjeremylt 
20319d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
20329d007619Sjeremylt 
2033b7c9bbdaSJeremy L Thompson   @ref Advanced
20349d007619Sjeremylt **/
2035d1d35e2fSjeremylt int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) {
2036*1203703bSJeremy L Thompson   bool is_tensor_basis;
2037*1203703bSJeremy L Thompson   Ceed ceed;
2038*1203703bSJeremy L Thompson 
2039*1203703bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis, &ceed));
2040*1203703bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
2041*1203703bSJeremy L Thompson   CeedCheck(is_tensor_basis, ceed, CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis");
2042d1d35e2fSjeremylt   *grad_1d = basis->grad_1d;
2043e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
20449d007619Sjeremylt }
20459d007619Sjeremylt 
20469d007619Sjeremylt /**
2047ca94c3ddSJeremy L Thompson   @brief Get divergence matrix of a `CeedBasis`
204850c301a5SRezgar Shakeri 
2049ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
205050c301a5SRezgar Shakeri   @param[out] div   Variable to store divergence matrix
205150c301a5SRezgar Shakeri 
205250c301a5SRezgar Shakeri   @return An error code: 0 - success, otherwise - failure
205350c301a5SRezgar Shakeri 
205450c301a5SRezgar Shakeri   @ref Advanced
205550c301a5SRezgar Shakeri **/
205650c301a5SRezgar Shakeri int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) {
205750c301a5SRezgar Shakeri   *div = basis->div;
205850c301a5SRezgar Shakeri   return CEED_ERROR_SUCCESS;
205950c301a5SRezgar Shakeri }
206050c301a5SRezgar Shakeri 
206150c301a5SRezgar Shakeri /**
2062ca94c3ddSJeremy L Thompson   @brief Get curl matrix of a `CeedBasis`
2063c4e3f59bSSebastian Grimberg 
2064ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
2065c4e3f59bSSebastian Grimberg   @param[out] curl  Variable to store curl matrix
2066c4e3f59bSSebastian Grimberg 
2067c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
2068c4e3f59bSSebastian Grimberg 
2069c4e3f59bSSebastian Grimberg   @ref Advanced
2070c4e3f59bSSebastian Grimberg **/
2071c4e3f59bSSebastian Grimberg int CeedBasisGetCurl(CeedBasis basis, const CeedScalar **curl) {
2072c4e3f59bSSebastian Grimberg   *curl = basis->curl;
2073c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
2074c4e3f59bSSebastian Grimberg }
2075c4e3f59bSSebastian Grimberg 
2076c4e3f59bSSebastian Grimberg /**
2077ca94c3ddSJeremy L Thompson   @brief Destroy a @ref  CeedBasis
20787a982d89SJeremy L. Thompson 
2079ca94c3ddSJeremy L Thompson   @param[in,out] basis `CeedBasis` to destroy
20807a982d89SJeremy L. Thompson 
20817a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
20827a982d89SJeremy L. Thompson 
20837a982d89SJeremy L. Thompson   @ref User
20847a982d89SJeremy L. Thompson **/
20857a982d89SJeremy L. Thompson int CeedBasisDestroy(CeedBasis *basis) {
2086356036faSJeremy L Thompson   if (!*basis || *basis == CEED_BASIS_NONE || --(*basis)->ref_count > 0) {
2087ad6481ceSJeremy L Thompson     *basis = NULL;
2088ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2089ad6481ceSJeremy L Thompson   }
20902b730f8bSJeremy L Thompson   if ((*basis)->Destroy) CeedCall((*basis)->Destroy(*basis));
20919831d45aSJeremy L Thompson   CeedCall(CeedTensorContractDestroy(&(*basis)->contract));
2092c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->q_ref_1d));
2093c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->q_weight_1d));
20942b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->interp));
20952b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->interp_1d));
20962b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->grad));
20972b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->grad_1d));
2098c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->div));
2099c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->curl));
2100c8c3fa7dSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*basis)->vec_chebyshev));
2101c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisDestroy(&(*basis)->basis_chebyshev));
21022b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*basis)->ceed));
21032b730f8bSJeremy L Thompson   CeedCall(CeedFree(basis));
2104e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
21057a982d89SJeremy L. Thompson }
21067a982d89SJeremy L. Thompson 
21077a982d89SJeremy L. Thompson /**
2108b11c1e72Sjeremylt   @brief Construct a Gauss-Legendre quadrature
2109b11c1e72Sjeremylt 
2110ca94c3ddSJeremy L Thompson   @param[in]  Q           Number of quadrature points (integrates polynomials of degree `2*Q-1` exactly)
2111ca94c3ddSJeremy L Thompson   @param[out] q_ref_1d    Array of length `Q` to hold the abscissa on `[-1, 1]`
2112ca94c3ddSJeremy L Thompson   @param[out] q_weight_1d Array of length `Q` to hold the weights
2113b11c1e72Sjeremylt 
2114b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2115dfdf5a53Sjeremylt 
2116dfdf5a53Sjeremylt   @ref Utility
2117b11c1e72Sjeremylt **/
21182b730f8bSJeremy L Thompson int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) {
2119d7b241e6Sjeremylt   CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0 * atan(1.0);
21201c66c397SJeremy L Thompson 
2121d1d35e2fSjeremylt   // Build q_ref_1d, q_weight_1d
212292ae7e47SJeremy L Thompson   for (CeedInt i = 0; i <= Q / 2; i++) {
2123d7b241e6Sjeremylt     // Guess
2124d7b241e6Sjeremylt     xi = cos(PI * (CeedScalar)(2 * i + 1) / ((CeedScalar)(2 * Q)));
2125d7b241e6Sjeremylt     // Pn(xi)
2126d7b241e6Sjeremylt     P0 = 1.0;
2127d7b241e6Sjeremylt     P1 = xi;
2128d7b241e6Sjeremylt     P2 = 0.0;
212992ae7e47SJeremy L Thompson     for (CeedInt j = 2; j <= Q; j++) {
2130d7b241e6Sjeremylt       P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2131d7b241e6Sjeremylt       P0 = P1;
2132d7b241e6Sjeremylt       P1 = P2;
2133d7b241e6Sjeremylt     }
2134d7b241e6Sjeremylt     // First Newton Step
2135d7b241e6Sjeremylt     dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2136d7b241e6Sjeremylt     xi  = xi - P2 / dP2;
2137d7b241e6Sjeremylt     // Newton to convergence
213892ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < 100 && fabs(P2) > 10 * CEED_EPSILON; k++) {
2139d7b241e6Sjeremylt       P0 = 1.0;
2140d7b241e6Sjeremylt       P1 = xi;
214192ae7e47SJeremy L Thompson       for (CeedInt j = 2; j <= Q; j++) {
2142d7b241e6Sjeremylt         P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2143d7b241e6Sjeremylt         P0 = P1;
2144d7b241e6Sjeremylt         P1 = P2;
2145d7b241e6Sjeremylt       }
2146d7b241e6Sjeremylt       dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2147d7b241e6Sjeremylt       xi  = xi - P2 / dP2;
2148d7b241e6Sjeremylt     }
2149d7b241e6Sjeremylt     // Save xi, wi
2150d7b241e6Sjeremylt     wi                     = 2.0 / ((1.0 - xi * xi) * dP2 * dP2);
2151d1d35e2fSjeremylt     q_weight_1d[i]         = wi;
2152d1d35e2fSjeremylt     q_weight_1d[Q - 1 - i] = wi;
2153d1d35e2fSjeremylt     q_ref_1d[i]            = -xi;
2154d1d35e2fSjeremylt     q_ref_1d[Q - 1 - i]    = xi;
2155d7b241e6Sjeremylt   }
2156e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2157d7b241e6Sjeremylt }
2158d7b241e6Sjeremylt 
2159b11c1e72Sjeremylt /**
2160b11c1e72Sjeremylt   @brief Construct a Gauss-Legendre-Lobatto quadrature
2161b11c1e72Sjeremylt 
2162ca94c3ddSJeremy L Thompson   @param[in]  Q           Number of quadrature points (integrates polynomials of degree `2*Q-3` exactly)
2163ca94c3ddSJeremy L Thompson   @param[out] q_ref_1d    Array of length `Q` to hold the abscissa on `[-1, 1]`
2164ca94c3ddSJeremy L Thompson   @param[out] q_weight_1d Array of length `Q` to hold the weights
2165b11c1e72Sjeremylt 
2166b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2167dfdf5a53Sjeremylt 
2168dfdf5a53Sjeremylt   @ref Utility
2169b11c1e72Sjeremylt **/
21702b730f8bSJeremy L Thompson int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) {
2171d7b241e6Sjeremylt   CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0 * atan(1.0);
21721c66c397SJeremy L Thompson 
2173d1d35e2fSjeremylt   // Build q_ref_1d, q_weight_1d
2174d7b241e6Sjeremylt   // Set endpoints
21756574a04fSJeremy L Thompson   CeedCheck(Q > 1, NULL, CEED_ERROR_DIMENSION, "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q);
2176d7b241e6Sjeremylt   wi = 2.0 / ((CeedScalar)(Q * (Q - 1)));
2177d1d35e2fSjeremylt   if (q_weight_1d) {
2178d1d35e2fSjeremylt     q_weight_1d[0]     = wi;
2179d1d35e2fSjeremylt     q_weight_1d[Q - 1] = wi;
2180d7b241e6Sjeremylt   }
2181d1d35e2fSjeremylt   q_ref_1d[0]     = -1.0;
2182d1d35e2fSjeremylt   q_ref_1d[Q - 1] = 1.0;
2183d7b241e6Sjeremylt   // Interior
218492ae7e47SJeremy L Thompson   for (CeedInt i = 1; i <= (Q - 1) / 2; i++) {
2185d7b241e6Sjeremylt     // Guess
2186d7b241e6Sjeremylt     xi = cos(PI * (CeedScalar)(i) / (CeedScalar)(Q - 1));
2187d7b241e6Sjeremylt     // Pn(xi)
2188d7b241e6Sjeremylt     P0 = 1.0;
2189d7b241e6Sjeremylt     P1 = xi;
2190d7b241e6Sjeremylt     P2 = 0.0;
219192ae7e47SJeremy L Thompson     for (CeedInt j = 2; j < Q; j++) {
2192d7b241e6Sjeremylt       P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2193d7b241e6Sjeremylt       P0 = P1;
2194d7b241e6Sjeremylt       P1 = P2;
2195d7b241e6Sjeremylt     }
2196d7b241e6Sjeremylt     // First Newton step
2197d7b241e6Sjeremylt     dP2  = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2198d7b241e6Sjeremylt     d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi);
2199d7b241e6Sjeremylt     xi   = xi - dP2 / d2P2;
2200d7b241e6Sjeremylt     // Newton to convergence
220192ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < 100 && fabs(dP2) > 10 * CEED_EPSILON; k++) {
2202d7b241e6Sjeremylt       P0 = 1.0;
2203d7b241e6Sjeremylt       P1 = xi;
220492ae7e47SJeremy L Thompson       for (CeedInt j = 2; j < Q; j++) {
2205d7b241e6Sjeremylt         P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2206d7b241e6Sjeremylt         P0 = P1;
2207d7b241e6Sjeremylt         P1 = P2;
2208d7b241e6Sjeremylt       }
2209d7b241e6Sjeremylt       dP2  = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2210d7b241e6Sjeremylt       d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi);
2211d7b241e6Sjeremylt       xi   = xi - dP2 / d2P2;
2212d7b241e6Sjeremylt     }
2213d7b241e6Sjeremylt     // Save xi, wi
2214d7b241e6Sjeremylt     wi = 2.0 / (((CeedScalar)(Q * (Q - 1))) * P2 * P2);
2215d1d35e2fSjeremylt     if (q_weight_1d) {
2216d1d35e2fSjeremylt       q_weight_1d[i]         = wi;
2217d1d35e2fSjeremylt       q_weight_1d[Q - 1 - i] = wi;
2218d7b241e6Sjeremylt     }
2219d1d35e2fSjeremylt     q_ref_1d[i]         = -xi;
2220d1d35e2fSjeremylt     q_ref_1d[Q - 1 - i] = xi;
2221d7b241e6Sjeremylt   }
2222e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2223d7b241e6Sjeremylt }
2224d7b241e6Sjeremylt 
2225d7b241e6Sjeremylt /// @}
2226