xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-basis.c (revision b0f67a9c1aeeb4d82b4724afaae1227ff4e81f15)
19ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, 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
1564c789ea2SJeremy L Thompson   @param[in] tabs   Tabs to append before each new line
157ca94c3ddSJeremy L Thompson   @param[in] stream Stream to view to, e.g., `stdout`
1587a982d89SJeremy L. Thompson 
1597a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1607a982d89SJeremy L. Thompson 
1617a982d89SJeremy L. Thompson   @ref Developer
1627a982d89SJeremy L. Thompson **/
1634c789ea2SJeremy L Thompson static int CeedScalarView(const char *name, const char *fp_fmt, CeedInt m, CeedInt n, const CeedScalar *a, const char *tabs, FILE *stream) {
164edf04919SJeremy L Thompson   if (m > 1) {
1654c789ea2SJeremy L Thompson     fprintf(stream, "%s  %s:\n", tabs, name);
166edf04919SJeremy L Thompson   } else {
167edf04919SJeremy L Thompson     char padded_name[12];
168edf04919SJeremy L Thompson 
169edf04919SJeremy L Thompson     snprintf(padded_name, 11, "%s:", name);
1704c789ea2SJeremy L Thompson     fprintf(stream, "%s  %-10s", tabs, padded_name);
171edf04919SJeremy L Thompson   }
17292ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < m; i++) {
1734c789ea2SJeremy L Thompson     if (m > 1) fprintf(stream, "%s    [%" CeedInt_FMT "]", tabs, i);
1742b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < n; j++) fprintf(stream, fp_fmt, fabs(a[i * n + j]) > 1E-14 ? a[i * n + j] : 0);
1757a982d89SJeremy L. Thompson     fputs("\n", stream);
1767a982d89SJeremy L. Thompson   }
177e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1787a982d89SJeremy L. Thompson }
1797a982d89SJeremy L. Thompson 
180a76a04e7SJeremy L Thompson /**
181*b0f67a9cSJeremy L Thompson   @brief View a `CeedBasis` passed as a `CeedObject`
182*b0f67a9cSJeremy L Thompson 
183*b0f67a9cSJeremy L Thompson   @param[in] basis  `CeedBasis` to view
184*b0f67a9cSJeremy L Thompson   @param[in] stream Filestream to write to
185*b0f67a9cSJeremy L Thompson 
186*b0f67a9cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
187*b0f67a9cSJeremy L Thompson 
188*b0f67a9cSJeremy L Thompson   @ref Developer
189*b0f67a9cSJeremy L Thompson **/
190*b0f67a9cSJeremy L Thompson static int CeedBasisView_Object(CeedObject basis, FILE *stream) {
191*b0f67a9cSJeremy L Thompson   CeedCall(CeedBasisView((CeedBasis)basis, stream));
192*b0f67a9cSJeremy L Thompson   return CEED_ERROR_SUCCESS;
193*b0f67a9cSJeremy L Thompson }
194*b0f67a9cSJeremy L Thompson 
195*b0f67a9cSJeremy L Thompson /**
196ea61e9acSJeremy L Thompson   @brief Create the interpolation and gradient matrices for projection from the nodes of `basis_from` to the nodes of `basis_to`.
197ba59ac12SSebastian Grimberg 
19815ad3917SSebastian Grimberg   The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization.
199ca94c3ddSJeremy 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.
20015ad3917SSebastian Grimberg 
201ba59ac12SSebastian Grimberg   Note: `basis_from` and `basis_to` must have compatible quadrature spaces.
202a76a04e7SJeremy L Thompson 
203ca94c3ddSJeremy L Thompson   @param[in]  basis_from     `CeedBasis` to project from
204ca94c3ddSJeremy L Thompson   @param[in]  basis_to       `CeedBasis` to project to
205ca94c3ddSJeremy L Thompson   @param[out] interp_project Address of the variable where the newly created interpolation matrix will be stored
206ca94c3ddSJeremy L Thompson   @param[out] grad_project   Address of the variable where the newly created gradient matrix will be stored
207a76a04e7SJeremy L Thompson 
208a76a04e7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
209a76a04e7SJeremy L Thompson 
210a76a04e7SJeremy L Thompson   @ref Developer
211a76a04e7SJeremy L Thompson **/
2122b730f8bSJeremy L Thompson static int CeedBasisCreateProjectionMatrices(CeedBasis basis_from, CeedBasis basis_to, CeedScalar **interp_project, CeedScalar **grad_project) {
213e104ad11SJames Wright   bool    are_both_tensor;
2141c66c397SJeremy L Thompson   CeedInt Q, Q_to, Q_from, P_to, P_from;
2151c66c397SJeremy L Thompson 
216a76a04e7SJeremy L Thompson   // Check for compatible quadrature spaces
2172b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_to, &Q_to));
2182b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_from, &Q_from));
2199bc66399SJeremy L Thompson   CeedCheck(Q_to == Q_from, CeedBasisReturnCeed(basis_to), CEED_ERROR_DIMENSION,
2203f08121cSJeremy L Thompson             "Bases must have compatible quadrature spaces."
22123622755SJeremy L Thompson             " 'basis_from' has %" CeedInt_FMT " points and 'basis_to' has %" CeedInt_FMT,
2223f08121cSJeremy L Thompson             Q_from, Q_to);
2231c66c397SJeremy L Thompson   Q = Q_to;
224a76a04e7SJeremy L Thompson 
22514556e63SJeremy L Thompson   // Check for matching tensor or non-tensor
226e104ad11SJames Wright   {
227e104ad11SJames Wright     bool is_tensor_to, is_tensor_from;
228e104ad11SJames Wright 
2292b730f8bSJeremy L Thompson     CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to));
2302b730f8bSJeremy L Thompson     CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from));
231e104ad11SJames Wright     are_both_tensor = is_tensor_to && is_tensor_from;
232e104ad11SJames Wright   }
233e104ad11SJames Wright   if (are_both_tensor) {
2342b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_to));
2352b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_from));
2362b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints1D(basis_from, &Q));
2376574a04fSJeremy L Thompson   } else {
2382b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_to, &P_to));
2392b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_from, &P_from));
240a76a04e7SJeremy L Thompson   }
241a76a04e7SJeremy L Thompson 
24215ad3917SSebastian Grimberg   // Check for matching FE space
24315ad3917SSebastian Grimberg   CeedFESpace fe_space_to, fe_space_from;
2443f08121cSJeremy L Thompson 
24515ad3917SSebastian Grimberg   CeedCall(CeedBasisGetFESpace(basis_to, &fe_space_to));
24615ad3917SSebastian Grimberg   CeedCall(CeedBasisGetFESpace(basis_from, &fe_space_from));
2479bc66399SJeremy L Thompson   CeedCheck(fe_space_to == fe_space_from, CeedBasisReturnCeed(basis_to), CEED_ERROR_MINOR,
2483f08121cSJeremy L Thompson             "Bases must both be the same FE space type."
2493f08121cSJeremy L Thompson             " 'basis_from' is a %s and 'basis_to' is a %s",
2503f08121cSJeremy L Thompson             CeedFESpaces[fe_space_from], CeedFESpaces[fe_space_to]);
25115ad3917SSebastian Grimberg 
25214556e63SJeremy L Thompson   // Get source matrices
25315ad3917SSebastian Grimberg   CeedInt           dim, q_comp = 1;
2542247a93fSRezgar Shakeri   CeedScalar       *interp_to_inv, *interp_from;
2551c66c397SJeremy L Thompson   const CeedScalar *interp_to_source = NULL, *interp_from_source = NULL, *grad_from_source = NULL;
2561c66c397SJeremy L Thompson 
257b3ed00e5SJames Wright   CeedCall(CeedBasisGetDimension(basis_from, &dim));
258e104ad11SJames Wright   if (are_both_tensor) {
2592b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis_to, &interp_to_source));
2602b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis_from, &interp_from_source));
261a76a04e7SJeremy L Thompson   } else {
26215ad3917SSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis_from, CEED_EVAL_INTERP, &q_comp));
2632b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(basis_to, &interp_to_source));
2642b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(basis_from, &interp_from_source));
26515ad3917SSebastian Grimberg   }
26615ad3917SSebastian Grimberg   CeedCall(CeedMalloc(Q * P_from * q_comp, &interp_from));
26715ad3917SSebastian Grimberg   CeedCall(CeedCalloc(P_to * P_from, interp_project));
26815ad3917SSebastian Grimberg 
26915ad3917SSebastian Grimberg   // `grad_project = interp_to^+ * grad_from` is computed for the H^1 space case so the
270de05fbb2SSebastian Grimberg   // projection basis will have a gradient operation (allocated even if not H^1 for the
271de05fbb2SSebastian Grimberg   // basis construction later on)
27215ad3917SSebastian Grimberg   if (fe_space_to == CEED_FE_SPACE_H1) {
273e104ad11SJames Wright     if (are_both_tensor) {
27415ad3917SSebastian Grimberg       CeedCall(CeedBasisGetGrad1D(basis_from, &grad_from_source));
27515ad3917SSebastian Grimberg     } else {
2762b730f8bSJeremy L Thompson       CeedCall(CeedBasisGetGrad(basis_from, &grad_from_source));
277a76a04e7SJeremy L Thompson     }
278de05fbb2SSebastian Grimberg   }
279e104ad11SJames Wright   CeedCall(CeedCalloc(P_to * P_from * (are_both_tensor ? 1 : dim), grad_project));
28015ad3917SSebastian Grimberg 
2812247a93fSRezgar Shakeri   // Compute interp_to^+, pseudoinverse of interp_to
2822247a93fSRezgar Shakeri   CeedCall(CeedCalloc(Q * q_comp * P_to, &interp_to_inv));
2839bc66399SJeremy L Thompson   CeedCall(CeedMatrixPseudoinverse(CeedBasisReturnCeed(basis_to), interp_to_source, Q * q_comp, P_to, interp_to_inv));
28414556e63SJeremy L Thompson   // Build matrices
285e104ad11SJames Wright   CeedInt     num_matrices = 1 + (fe_space_to == CEED_FE_SPACE_H1) * (are_both_tensor ? 1 : dim);
28614556e63SJeremy L Thompson   CeedScalar *input_from[num_matrices], *output_project[num_matrices];
2871c66c397SJeremy L Thompson 
28814556e63SJeremy L Thompson   input_from[0]     = (CeedScalar *)interp_from_source;
28914556e63SJeremy L Thompson   output_project[0] = *interp_project;
29014556e63SJeremy L Thompson   for (CeedInt m = 1; m < num_matrices; m++) {
29114556e63SJeremy L Thompson     input_from[m]     = (CeedScalar *)&grad_from_source[(m - 1) * Q * P_from];
29202af4036SJeremy L Thompson     output_project[m] = &((*grad_project)[(m - 1) * P_to * P_from]);
29314556e63SJeremy L Thompson   }
29414556e63SJeremy L Thompson   for (CeedInt m = 0; m < num_matrices; m++) {
2952247a93fSRezgar Shakeri     // output_project = interp_to^+ * interp_from
29615ad3917SSebastian Grimberg     memcpy(interp_from, input_from[m], Q * P_from * q_comp * sizeof(input_from[m][0]));
2979bc66399SJeremy L Thompson     CeedCall(CeedMatrixMatrixMultiply(CeedBasisReturnCeed(basis_to), interp_to_inv, input_from[m], output_project[m], P_to, P_from, Q * q_comp));
2982247a93fSRezgar Shakeri     // Round zero to machine precision
2992247a93fSRezgar Shakeri     for (CeedInt i = 0; i < P_to * P_from; i++) {
3002247a93fSRezgar Shakeri       if (fabs(output_project[m][i]) < 10 * CEED_EPSILON) output_project[m][i] = 0.0;
301a76a04e7SJeremy L Thompson     }
30214556e63SJeremy L Thompson   }
30314556e63SJeremy L Thompson 
30414556e63SJeremy L Thompson   // Cleanup
3052247a93fSRezgar Shakeri   CeedCall(CeedFree(&interp_to_inv));
3062b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_from));
307a76a04e7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
308a76a04e7SJeremy L Thompson }
309a76a04e7SJeremy L Thompson 
3100b31fde2SJeremy L Thompson /**
3116ab8e59fSJames Wright   @brief Check input vector dimensions for CeedBasisApply[Add]
3126ab8e59fSJames Wright 
3136ab8e59fSJames Wright   @param[in]  basis     `CeedBasis` to evaluate
3146ab8e59fSJames Wright   @param[in]  num_elem  The number of elements to apply the basis evaluation to;
3156ab8e59fSJames Wright                           the backend will specify the ordering in @ref CeedElemRestrictionCreate()
3166ab8e59fSJames Wright   @param[in]  t_mode    @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points;
3176ab8e59fSJames Wright                           @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes
3186ab8e59fSJames Wright   @param[in]  eval_mode @ref CEED_EVAL_NONE to use values directly,
3196ab8e59fSJames Wright                           @ref CEED_EVAL_INTERP to use interpolated values,
3206ab8e59fSJames Wright                           @ref CEED_EVAL_GRAD to use gradients,
3216ab8e59fSJames Wright                           @ref CEED_EVAL_DIV to use divergence,
3226ab8e59fSJames Wright                           @ref CEED_EVAL_CURL to use curl,
3236ab8e59fSJames Wright                           @ref CEED_EVAL_WEIGHT to use quadrature weights
3246ab8e59fSJames Wright   @param[in]  u         Input `CeedVector`
3256ab8e59fSJames Wright   @param[out] v         Output `CeedVector`
3266ab8e59fSJames Wright 
3276ab8e59fSJames Wright   @return An error code: 0 - success, otherwise - failure
3286ab8e59fSJames Wright 
3296ab8e59fSJames Wright   @ref Developer
3306ab8e59fSJames Wright **/
3316ab8e59fSJames Wright static int CeedBasisApplyCheckDims(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) {
3326ab8e59fSJames Wright   CeedInt  dim, num_comp, q_comp, num_nodes, num_qpts;
3336ab8e59fSJames Wright   CeedSize u_length = 0, v_length;
3346ab8e59fSJames Wright 
3356ab8e59fSJames Wright   CeedCall(CeedBasisGetDimension(basis, &dim));
3366ab8e59fSJames Wright   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
3376ab8e59fSJames Wright   CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
3386ab8e59fSJames Wright   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
3396ab8e59fSJames Wright   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
3406ab8e59fSJames Wright   CeedCall(CeedVectorGetLength(v, &v_length));
3416ab8e59fSJames Wright   if (u) CeedCall(CeedVectorGetLength(u, &u_length));
3426ab8e59fSJames Wright 
3436ab8e59fSJames Wright   // Check vector lengths to prevent out of bounds issues
3446ab8e59fSJames Wright   bool has_good_dims = true;
3456ab8e59fSJames Wright   switch (eval_mode) {
3466ab8e59fSJames Wright     case CEED_EVAL_NONE:
3476ab8e59fSJames Wright     case CEED_EVAL_INTERP:
3486ab8e59fSJames Wright     case CEED_EVAL_GRAD:
3496ab8e59fSJames Wright     case CEED_EVAL_DIV:
3506ab8e59fSJames Wright     case CEED_EVAL_CURL:
3516ab8e59fSJames Wright       has_good_dims = ((t_mode == CEED_TRANSPOSE && u_length >= (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_qpts * (CeedSize)q_comp &&
3526ab8e59fSJames Wright                         v_length >= (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_nodes) ||
3536ab8e59fSJames Wright                        (t_mode == CEED_NOTRANSPOSE && v_length >= (CeedSize)num_elem * (CeedSize)num_qpts * (CeedSize)num_comp * (CeedSize)q_comp &&
3546ab8e59fSJames Wright                         u_length >= (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_nodes));
3556ab8e59fSJames Wright       break;
3566ab8e59fSJames Wright     case CEED_EVAL_WEIGHT:
3576ab8e59fSJames Wright       has_good_dims = v_length >= (CeedSize)num_elem * (CeedSize)num_qpts;
3586ab8e59fSJames Wright       break;
3596ab8e59fSJames Wright   }
3606ab8e59fSJames Wright   CeedCheck(has_good_dims, CeedBasisReturnCeed(basis), CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode");
3616ab8e59fSJames Wright   return CEED_ERROR_SUCCESS;
3626ab8e59fSJames Wright }
3636ab8e59fSJames Wright 
3646ab8e59fSJames Wright /**
3650b31fde2SJeremy L Thompson   @brief Check input vector dimensions for CeedBasisApply[Add]AtPoints
3660b31fde2SJeremy L Thompson 
3670b31fde2SJeremy L Thompson   @param[in]  basis      `CeedBasis` to evaluate
3680b31fde2SJeremy L Thompson   @param[in]  num_elem   The number of elements to apply the basis evaluation to;
3690b31fde2SJeremy L Thompson                           the backend will specify the ordering in @ref CeedElemRestrictionCreate()
3700b31fde2SJeremy L Thompson   @param[in]  num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem`
3710b31fde2SJeremy L Thompson   @param[in]  t_mode     @ref CEED_NOTRANSPOSE to evaluate from nodes to points;
3720b31fde2SJeremy L Thompson                            @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes
3730b31fde2SJeremy L Thompson   @param[in]  eval_mode  @ref CEED_EVAL_INTERP to use interpolated values,
3740b31fde2SJeremy L Thompson                            @ref CEED_EVAL_GRAD to use gradients,
3750b31fde2SJeremy L Thompson                            @ref CEED_EVAL_WEIGHT to use quadrature weights
3760b31fde2SJeremy L Thompson   @param[in]  x_ref      `CeedVector` holding reference coordinates of each point
3770b31fde2SJeremy L Thompson   @param[in]  u          Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE
3780b31fde2SJeremy L Thompson   @param[out] v          Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP
3790b31fde2SJeremy L Thompson 
3800b31fde2SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3810b31fde2SJeremy L Thompson 
3820b31fde2SJeremy L Thompson   @ref Developer
3830b31fde2SJeremy L Thompson **/
3840b31fde2SJeremy L Thompson static int CeedBasisApplyAtPointsCheckDims(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode,
3850b31fde2SJeremy L Thompson                                            CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) {
3860b31fde2SJeremy L Thompson   CeedInt  dim, num_comp, num_q_comp, num_nodes, P_1d = 1, Q_1d = 1, total_num_points = 0;
3870b31fde2SJeremy L Thompson   CeedSize x_length = 0, u_length = 0, v_length;
3880b31fde2SJeremy L Thompson 
3890b31fde2SJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
3900b31fde2SJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
3910b31fde2SJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
3920b31fde2SJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
3930b31fde2SJeremy L Thompson   CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &num_q_comp));
3940b31fde2SJeremy L Thompson   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
3950b31fde2SJeremy L Thompson   CeedCall(CeedVectorGetLength(v, &v_length));
3960b31fde2SJeremy L Thompson   if (x_ref != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(x_ref, &x_length));
3970b31fde2SJeremy L Thompson   if (u != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(u, &u_length));
3980b31fde2SJeremy L Thompson 
3990b31fde2SJeremy L Thompson   // Check compatibility coordinates vector
4000b31fde2SJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) total_num_points += num_points[i];
4019bc66399SJeremy L Thompson   CeedCheck((x_length >= (CeedSize)total_num_points * (CeedSize)dim) || (eval_mode == CEED_EVAL_WEIGHT), CeedBasisReturnCeed(basis),
4029bc66399SJeremy L Thompson             CEED_ERROR_DIMENSION,
4030b31fde2SJeremy L Thompson             "Length of reference coordinate vector incompatible with basis dimension and number of points."
4040b31fde2SJeremy L Thompson             " Found reference coordinate vector of length %" CeedSize_FMT ", not of length %" CeedSize_FMT ".",
40519a04db8SJeremy L Thompson             x_length, (CeedSize)total_num_points * (CeedSize)dim);
4060b31fde2SJeremy L Thompson 
4070b31fde2SJeremy L Thompson   // Check CEED_EVAL_WEIGHT only on CEED_NOTRANSPOSE
4089bc66399SJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT || t_mode == CEED_NOTRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED,
4090b31fde2SJeremy L Thompson             "CEED_EVAL_WEIGHT only supported with CEED_NOTRANSPOSE");
4100b31fde2SJeremy L Thompson 
4110b31fde2SJeremy L Thompson   // Check vector lengths to prevent out of bounds issues
4120b31fde2SJeremy L Thompson   bool has_good_dims = true;
4130b31fde2SJeremy L Thompson   switch (eval_mode) {
4140b31fde2SJeremy L Thompson     case CEED_EVAL_INTERP:
41519a04db8SJeremy L Thompson       has_good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= (CeedSize)total_num_points * (CeedSize)num_q_comp ||
41619a04db8SJeremy L Thompson                                                      v_length >= (CeedSize)num_elem * (CeedSize)num_nodes * (CeedSize)num_comp)) ||
41719a04db8SJeremy L Thompson                        (t_mode == CEED_NOTRANSPOSE && (v_length >= (CeedSize)total_num_points * (CeedSize)num_q_comp ||
41819a04db8SJeremy L Thompson                                                        u_length >= (CeedSize)num_elem * (CeedSize)num_nodes * (CeedSize)num_comp)));
4190b31fde2SJeremy L Thompson       break;
4200b31fde2SJeremy L Thompson     case CEED_EVAL_GRAD:
42119a04db8SJeremy L Thompson       has_good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= (CeedSize)total_num_points * (CeedSize)num_q_comp * (CeedSize)dim ||
42219a04db8SJeremy L Thompson                                                      v_length >= (CeedSize)num_elem * (CeedSize)num_nodes * (CeedSize)num_comp)) ||
42319a04db8SJeremy L Thompson                        (t_mode == CEED_NOTRANSPOSE && (v_length >= (CeedSize)total_num_points * (CeedSize)num_q_comp * (CeedSize)dim ||
42419a04db8SJeremy L Thompson                                                        u_length >= (CeedSize)num_elem * (CeedSize)num_nodes * (CeedSize)num_comp)));
4250b31fde2SJeremy L Thompson       break;
4260b31fde2SJeremy L Thompson     case CEED_EVAL_WEIGHT:
4270b31fde2SJeremy L Thompson       has_good_dims = t_mode == CEED_NOTRANSPOSE && (v_length >= total_num_points);
4280b31fde2SJeremy L Thompson       break;
4290b31fde2SJeremy L Thompson       // LCOV_EXCL_START
4300b31fde2SJeremy L Thompson     case CEED_EVAL_NONE:
4310b31fde2SJeremy L Thompson     case CEED_EVAL_DIV:
4320b31fde2SJeremy L Thompson     case CEED_EVAL_CURL:
4339bc66399SJeremy L Thompson       return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points not supported for %s",
4349bc66399SJeremy L Thompson                        CeedEvalModes[eval_mode]);
4350b31fde2SJeremy L Thompson       // LCOV_EXCL_STOP
4360b31fde2SJeremy L Thompson   }
4379bc66399SJeremy L Thompson   CeedCheck(has_good_dims, CeedBasisReturnCeed(basis), CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode");
4380b31fde2SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4390b31fde2SJeremy L Thompson }
4400b31fde2SJeremy L Thompson 
4410b31fde2SJeremy L Thompson /**
4420b31fde2SJeremy L Thompson   @brief Default implimentation to apply basis evaluation from nodes to arbitrary points
4430b31fde2SJeremy L Thompson 
4440b31fde2SJeremy L Thompson   @param[in]  basis      `CeedBasis` to evaluate
4450b31fde2SJeremy L Thompson   @param[in]  apply_add  Sum result into target vector or overwrite
4460b31fde2SJeremy L Thompson   @param[in]  num_elem   The number of elements to apply the basis evaluation to;
4470b31fde2SJeremy L Thompson                           the backend will specify the ordering in @ref CeedElemRestrictionCreate()
4480b31fde2SJeremy L Thompson   @param[in]  num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem`
4490b31fde2SJeremy L Thompson   @param[in]  t_mode     @ref CEED_NOTRANSPOSE to evaluate from nodes to points;
4500b31fde2SJeremy L Thompson                            @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes
4510b31fde2SJeremy L Thompson   @param[in]  eval_mode  @ref CEED_EVAL_INTERP to use interpolated values,
4520b31fde2SJeremy L Thompson                            @ref CEED_EVAL_GRAD to use gradients,
4530b31fde2SJeremy L Thompson                            @ref CEED_EVAL_WEIGHT to use quadrature weights
4540b31fde2SJeremy L Thompson   @param[in]  x_ref      `CeedVector` holding reference coordinates of each point
4550b31fde2SJeremy L Thompson   @param[in]  u          Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE
4560b31fde2SJeremy L Thompson   @param[out] v          Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP
4570b31fde2SJeremy L Thompson 
4580b31fde2SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4590b31fde2SJeremy L Thompson 
4600b31fde2SJeremy L Thompson   @ref Developer
4610b31fde2SJeremy L Thompson **/
4620b31fde2SJeremy L Thompson static int CeedBasisApplyAtPoints_Core(CeedBasis basis, bool apply_add, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode,
4630b31fde2SJeremy L Thompson                                        CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) {
4640b31fde2SJeremy L Thompson   CeedInt dim, num_comp, P_1d = 1, Q_1d = 1, total_num_points = num_points[0];
4650b31fde2SJeremy L Thompson 
4660b31fde2SJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
4670b31fde2SJeremy L Thompson   // Inserting check because clang-tidy doesn't understand this cannot occur
4689bc66399SJeremy L Thompson   CeedCheck(dim > 0, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Malformed CeedBasis, dim > 0 is required");
4690b31fde2SJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
4700b31fde2SJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
4710b31fde2SJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
4720b31fde2SJeremy L Thompson 
4730b31fde2SJeremy L Thompson   // Default implementation
4740b31fde2SJeremy L Thompson   {
4750b31fde2SJeremy L Thompson     bool is_tensor_basis;
4760b31fde2SJeremy L Thompson 
4770b31fde2SJeremy L Thompson     CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
4789bc66399SJeremy L Thompson     CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED,
4799bc66399SJeremy L Thompson               "Evaluation at arbitrary points only supported for tensor product bases");
4800b31fde2SJeremy L Thompson   }
4819bc66399SJeremy L Thompson   CeedCheck(num_elem == 1, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED,
4829bc66399SJeremy L Thompson             "Evaluation at arbitrary  points only supported for a single element at a time");
4830b31fde2SJeremy L Thompson   if (eval_mode == CEED_EVAL_WEIGHT) {
4840b31fde2SJeremy L Thompson     CeedCall(CeedVectorSetValue(v, 1.0));
4850b31fde2SJeremy L Thompson     return CEED_ERROR_SUCCESS;
4860b31fde2SJeremy L Thompson   }
4870b31fde2SJeremy L Thompson   if (!basis->basis_chebyshev) {
4880b31fde2SJeremy L Thompson     // Build basis mapping from nodes to Chebyshev coefficients
4890b31fde2SJeremy L Thompson     CeedScalar       *chebyshev_interp_1d, *chebyshev_grad_1d, *chebyshev_q_weight_1d;
4900b31fde2SJeremy L Thompson     const CeedScalar *q_ref_1d;
4919bc66399SJeremy L Thompson     Ceed              ceed;
4920b31fde2SJeremy L Thompson 
4930b31fde2SJeremy L Thompson     CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
4940b31fde2SJeremy L Thompson     CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_grad_1d));
4950b31fde2SJeremy L Thompson     CeedCall(CeedCalloc(Q_1d, &chebyshev_q_weight_1d));
4960b31fde2SJeremy L Thompson     CeedCall(CeedBasisGetQRef(basis, &q_ref_1d));
4970b31fde2SJeremy L Thompson     CeedCall(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
4980b31fde2SJeremy L Thompson 
4999bc66399SJeremy L Thompson     CeedCall(CeedBasisGetCeed(basis, &ceed));
5000b31fde2SJeremy L Thompson     CeedCall(CeedVectorCreate(ceed, num_comp * CeedIntPow(Q_1d, dim), &basis->vec_chebyshev));
5010b31fde2SJeremy 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,
5020b31fde2SJeremy L Thompson                                      &basis->basis_chebyshev));
5030b31fde2SJeremy L Thompson 
5040b31fde2SJeremy L Thompson     // Cleanup
5050b31fde2SJeremy L Thompson     CeedCall(CeedFree(&chebyshev_interp_1d));
5060b31fde2SJeremy L Thompson     CeedCall(CeedFree(&chebyshev_grad_1d));
5070b31fde2SJeremy L Thompson     CeedCall(CeedFree(&chebyshev_q_weight_1d));
5089bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&ceed));
5090b31fde2SJeremy L Thompson   }
5100b31fde2SJeremy L Thompson 
5110b31fde2SJeremy L Thompson   // Create TensorContract object if needed, such as a basis from the GPU backends
5120b31fde2SJeremy L Thompson   if (!basis->contract) {
5130b31fde2SJeremy L Thompson     Ceed      ceed_ref;
5140b31fde2SJeremy L Thompson     CeedBasis basis_ref = NULL;
5150b31fde2SJeremy L Thompson 
5160b31fde2SJeremy L Thompson     CeedCall(CeedInit("/cpu/self", &ceed_ref));
5170b31fde2SJeremy L Thompson     // Only need matching tensor contraction dimensions, any type of basis will work
5180b31fde2SJeremy L Thompson     CeedCall(CeedBasisCreateTensorH1Lagrange(ceed_ref, dim, num_comp, P_1d, Q_1d, CEED_GAUSS, &basis_ref));
5190b31fde2SJeremy L Thompson     // Note - clang-tidy doesn't know basis_ref->contract must be valid here
5209bc66399SJeremy L Thompson     CeedCheck(basis_ref && basis_ref->contract, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED,
5219bc66399SJeremy L Thompson               "Reference CPU ceed failed to create a tensor contraction object");
5220b31fde2SJeremy L Thompson     CeedCall(CeedTensorContractReferenceCopy(basis_ref->contract, &basis->contract));
5230b31fde2SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis_ref));
5240b31fde2SJeremy L Thompson     CeedCall(CeedDestroy(&ceed_ref));
5250b31fde2SJeremy L Thompson   }
5260b31fde2SJeremy L Thompson 
5270b31fde2SJeremy L Thompson   // Basis evaluation
5280b31fde2SJeremy L Thompson   switch (t_mode) {
5290b31fde2SJeremy L Thompson     case CEED_NOTRANSPOSE: {
5300b31fde2SJeremy L Thompson       // Nodes to arbitrary points
5310b31fde2SJeremy L Thompson       CeedScalar       *v_array;
5320b31fde2SJeremy L Thompson       const CeedScalar *chebyshev_coeffs, *x_array_read;
5330b31fde2SJeremy L Thompson 
5340b31fde2SJeremy L Thompson       // -- Interpolate to Chebyshev coefficients
5350b31fde2SJeremy L Thompson       CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, basis->vec_chebyshev));
5360b31fde2SJeremy L Thompson 
5370b31fde2SJeremy L Thompson       // -- Evaluate Chebyshev polynomials at arbitrary points
5380b31fde2SJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs));
5390b31fde2SJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read));
5400b31fde2SJeremy L Thompson       CeedCall(CeedVectorGetArrayWrite(v, CEED_MEM_HOST, &v_array));
5410b31fde2SJeremy L Thompson       switch (eval_mode) {
5420b31fde2SJeremy L Thompson         case CEED_EVAL_INTERP: {
5430b31fde2SJeremy L Thompson           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
5440b31fde2SJeremy L Thompson 
5450b31fde2SJeremy L Thompson           // ---- Values at point
5460b31fde2SJeremy L Thompson           for (CeedInt p = 0; p < total_num_points; p++) {
5470b31fde2SJeremy L Thompson             CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1;
5480b31fde2SJeremy L Thompson 
5490b31fde2SJeremy L Thompson             for (CeedInt d = 0; d < dim; d++) {
5500b31fde2SJeremy L Thompson               // ------ Tensor contract with current Chebyshev polynomial values
5510b31fde2SJeremy L Thompson               CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x));
5520b31fde2SJeremy L Thompson               CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false,
5530b31fde2SJeremy L Thompson                                                d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2]));
5540b31fde2SJeremy L Thompson               pre /= Q_1d;
5550b31fde2SJeremy L Thompson               post *= 1;
5560b31fde2SJeremy L Thompson             }
5570b31fde2SJeremy L Thompson             for (CeedInt c = 0; c < num_comp; c++) v_array[c * total_num_points + p] = tmp[dim % 2][c];
5580b31fde2SJeremy L Thompson           }
5590b31fde2SJeremy L Thompson           break;
5600b31fde2SJeremy L Thompson         }
5610b31fde2SJeremy L Thompson         case CEED_EVAL_GRAD: {
5620b31fde2SJeremy L Thompson           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
5630b31fde2SJeremy L Thompson 
5640b31fde2SJeremy L Thompson           // ---- Values at point
5650b31fde2SJeremy L Thompson           for (CeedInt p = 0; p < total_num_points; p++) {
5660b31fde2SJeremy L Thompson             // Dim**2 contractions, apply grad when pass == dim
5670b31fde2SJeremy L Thompson             for (CeedInt pass = 0; pass < dim; pass++) {
5680b31fde2SJeremy L Thompson               CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1;
5690b31fde2SJeremy L Thompson 
5700b31fde2SJeremy L Thompson               for (CeedInt d = 0; d < dim; d++) {
5710b31fde2SJeremy L Thompson                 // ------ Tensor contract with current Chebyshev polynomial values
5720b31fde2SJeremy L Thompson                 if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x));
5730b31fde2SJeremy L Thompson                 else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x));
5740b31fde2SJeremy L Thompson                 CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false,
5750b31fde2SJeremy L Thompson                                                  d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2]));
5760b31fde2SJeremy L Thompson                 pre /= Q_1d;
5770b31fde2SJeremy L Thompson                 post *= 1;
5780b31fde2SJeremy L Thompson               }
5790b31fde2SJeremy L Thompson               for (CeedInt c = 0; c < num_comp; c++) v_array[(pass * num_comp + c) * total_num_points + p] = tmp[dim % 2][c];
5800b31fde2SJeremy L Thompson             }
5810b31fde2SJeremy L Thompson           }
5820b31fde2SJeremy L Thompson           break;
5830b31fde2SJeremy L Thompson         }
5840b31fde2SJeremy L Thompson         default:
5850b31fde2SJeremy L Thompson           // Nothing to do, excluded above
5860b31fde2SJeremy L Thompson           break;
5870b31fde2SJeremy L Thompson       }
5880b31fde2SJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(basis->vec_chebyshev, &chebyshev_coeffs));
5890b31fde2SJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read));
5900b31fde2SJeremy L Thompson       CeedCall(CeedVectorRestoreArray(v, &v_array));
5910b31fde2SJeremy L Thompson       break;
5920b31fde2SJeremy L Thompson     }
5930b31fde2SJeremy L Thompson     case CEED_TRANSPOSE: {
5940b31fde2SJeremy L Thompson       // Note: No switch on e_mode here because only CEED_EVAL_INTERP is supported at this time
5950b31fde2SJeremy L Thompson       // Arbitrary points to nodes
5960b31fde2SJeremy L Thompson       CeedScalar       *chebyshev_coeffs;
5970b31fde2SJeremy L Thompson       const CeedScalar *u_array, *x_array_read;
5980b31fde2SJeremy L Thompson 
5990b31fde2SJeremy L Thompson       // -- Transpose of evaluation of Chebyshev polynomials at arbitrary points
6000b31fde2SJeremy L Thompson       CeedCall(CeedVectorGetArrayWrite(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs));
6010b31fde2SJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read));
6020b31fde2SJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array));
6030b31fde2SJeremy L Thompson 
6040b31fde2SJeremy L Thompson       switch (eval_mode) {
6050b31fde2SJeremy L Thompson         case CEED_EVAL_INTERP: {
6060b31fde2SJeremy L Thompson           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
6070b31fde2SJeremy L Thompson 
6080b31fde2SJeremy L Thompson           // ---- Values at point
6090b31fde2SJeremy L Thompson           for (CeedInt p = 0; p < total_num_points; p++) {
6100b31fde2SJeremy L Thompson             CeedInt pre = num_comp * 1, post = 1;
6110b31fde2SJeremy L Thompson 
6120b31fde2SJeremy L Thompson             for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[c * total_num_points + p];
6130b31fde2SJeremy L Thompson             for (CeedInt d = 0; d < dim; d++) {
6140b31fde2SJeremy L Thompson               // ------ Tensor contract with current Chebyshev polynomial values
6150b31fde2SJeremy L Thompson               CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x));
6160b31fde2SJeremy L Thompson               CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, p > 0 && d == (dim - 1), tmp[d % 2],
6170b31fde2SJeremy L Thompson                                                d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2]));
6180b31fde2SJeremy L Thompson               pre /= 1;
6190b31fde2SJeremy L Thompson               post *= Q_1d;
6200b31fde2SJeremy L Thompson             }
6210b31fde2SJeremy L Thompson           }
6220b31fde2SJeremy L Thompson           break;
6230b31fde2SJeremy L Thompson         }
6240b31fde2SJeremy L Thompson         case CEED_EVAL_GRAD: {
6250b31fde2SJeremy L Thompson           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
6260b31fde2SJeremy L Thompson 
6270b31fde2SJeremy L Thompson           // ---- Values at point
6280b31fde2SJeremy L Thompson           for (CeedInt p = 0; p < total_num_points; p++) {
6290b31fde2SJeremy L Thompson             // Dim**2 contractions, apply grad when pass == dim
6300b31fde2SJeremy L Thompson             for (CeedInt pass = 0; pass < dim; pass++) {
6310b31fde2SJeremy L Thompson               CeedInt pre = num_comp * 1, post = 1;
6320b31fde2SJeremy L Thompson 
6330b31fde2SJeremy L Thompson               for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[(pass * num_comp + c) * total_num_points + p];
6340b31fde2SJeremy L Thompson               for (CeedInt d = 0; d < dim; d++) {
6350b31fde2SJeremy L Thompson                 // ------ Tensor contract with current Chebyshev polynomial values
6360b31fde2SJeremy L Thompson                 if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x));
6370b31fde2SJeremy L Thompson                 else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x));
6380b31fde2SJeremy L Thompson                 CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode,
6390b31fde2SJeremy L Thompson                                                  (p > 0 || (p == 0 && pass > 0)) && d == (dim - 1), tmp[d % 2],
6400b31fde2SJeremy L Thompson                                                  d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2]));
6410b31fde2SJeremy L Thompson                 pre /= 1;
6420b31fde2SJeremy L Thompson                 post *= Q_1d;
6430b31fde2SJeremy L Thompson               }
6440b31fde2SJeremy L Thompson             }
6450b31fde2SJeremy L Thompson           }
6460b31fde2SJeremy L Thompson           break;
6470b31fde2SJeremy L Thompson         }
6480b31fde2SJeremy L Thompson         default:
6490b31fde2SJeremy L Thompson           // Nothing to do, excluded above
6500b31fde2SJeremy L Thompson           break;
6510b31fde2SJeremy L Thompson       }
6520b31fde2SJeremy L Thompson       CeedCall(CeedVectorRestoreArray(basis->vec_chebyshev, &chebyshev_coeffs));
6530b31fde2SJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read));
6540b31fde2SJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(u, &u_array));
6550b31fde2SJeremy L Thompson 
6560b31fde2SJeremy L Thompson       // -- Interpolate transpose from Chebyshev coefficients
6570b31fde2SJeremy L Thompson       if (apply_add) CeedCall(CeedBasisApplyAdd(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v));
6580b31fde2SJeremy L Thompson       else CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v));
6590b31fde2SJeremy L Thompson       break;
6600b31fde2SJeremy L Thompson     }
6610b31fde2SJeremy L Thompson   }
6620b31fde2SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6630b31fde2SJeremy L Thompson }
6640b31fde2SJeremy L Thompson 
6657a982d89SJeremy L. Thompson /// @}
6667a982d89SJeremy L. Thompson 
6677a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6687a982d89SJeremy L. Thompson /// Ceed Backend API
6697a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6707a982d89SJeremy L. Thompson /// @addtogroup CeedBasisBackend
6717a982d89SJeremy L. Thompson /// @{
6727a982d89SJeremy L. Thompson 
6737a982d89SJeremy L. Thompson /**
674fda26546SJeremy L Thompson   @brief Fallback to a reference implementation for a non tensor-product basis for \f$H^1\f$ discretizations.
675fda26546SJeremy L Thompson     This function may only be called inside of a backend `BasisCreateH1` function.
676fda26546SJeremy L Thompson     This is used by a backend when the specific parameters for a `CeedBasis` exceed the backend's support, such as
677fda26546SJeremy L Thompson     when a `interp` and `grad` matrices require too many bytes to fit into shared memory on a GPU.
678fda26546SJeremy L Thompson 
679fda26546SJeremy L Thompson   @param[in]  ceed      `Ceed` object used to create the `CeedBasis`
680fda26546SJeremy L Thompson   @param[in]  topo      Topology of element, e.g. hypercube, simplex, etc
681fda26546SJeremy L Thompson   @param[in]  num_comp  Number of field components (1 for scalar fields)
682fda26546SJeremy L Thompson   @param[in]  num_nodes Total number of nodes
683fda26546SJeremy L Thompson   @param[in]  num_qpts  Total number of quadrature points
684fda26546SJeremy L Thompson   @param[in]  interp    Row-major (`num_qpts * num_nodes`) matrix expressing the values of nodal basis functions at quadrature points
685fda26546SJeremy L Thompson   @param[in]  grad      Row-major (`dim * num_qpts * num_nodes`) matrix expressing derivatives of nodal basis functions at quadrature points
686fda26546SJeremy L Thompson   @param[in]  q_ref     Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element
687fda26546SJeremy L Thompson   @param[in]  q_weight  Array of length `num_qpts` holding the quadrature weights on the reference element
688fda26546SJeremy L Thompson   @param[out] basis     Newly created `CeedBasis`
689fda26546SJeremy L Thompson 
690fda26546SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
691fda26546SJeremy L Thompson 
692fda26546SJeremy L Thompson   @ref User
693fda26546SJeremy L Thompson **/
694fda26546SJeremy L Thompson int CeedBasisCreateH1Fallback(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
695fda26546SJeremy L Thompson                               const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
696fda26546SJeremy L Thompson   CeedInt P = num_nodes, Q = num_qpts, dim = 0;
697fda26546SJeremy L Thompson   Ceed    delegate;
698fda26546SJeremy L Thompson 
699fda26546SJeremy L Thompson   CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
700fda26546SJeremy L Thompson   CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateH1");
701fda26546SJeremy L Thompson 
702*b0f67a9cSJeremy L Thompson   CeedCall(CeedReferenceCopy(delegate, &(basis)->obj.ceed));
703fda26546SJeremy L Thompson   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
704fda26546SJeremy L Thompson   CeedCall(delegate->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, basis));
705fda26546SJeremy L Thompson   CeedCall(CeedDestroy(&delegate));
706fda26546SJeremy L Thompson   return CEED_ERROR_SUCCESS;
707fda26546SJeremy L Thompson }
708fda26546SJeremy L Thompson 
709fda26546SJeremy L Thompson /**
710ca94c3ddSJeremy L Thompson   @brief Return collocated gradient matrix
7117a982d89SJeremy L. Thompson 
712ca94c3ddSJeremy L Thompson   @param[in]  basis         `CeedBasis`
713ca94c3ddSJeremy L Thompson   @param[out] collo_grad_1d Row-major (`Q_1d * Q_1d`) matrix expressing derivatives of basis functions at quadrature points
7147a982d89SJeremy L. Thompson 
7157a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
7167a982d89SJeremy L. Thompson 
7177a982d89SJeremy L. Thompson   @ref Backend
7187a982d89SJeremy L. Thompson **/
719d1d35e2fSjeremylt int CeedBasisGetCollocatedGrad(CeedBasis basis, CeedScalar *collo_grad_1d) {
7207a982d89SJeremy L. Thompson   Ceed              ceed;
7212247a93fSRezgar Shakeri   CeedInt           P_1d, Q_1d;
7222247a93fSRezgar Shakeri   CeedScalar       *interp_1d_pinv;
7231203703bSJeremy L Thompson   const CeedScalar *grad_1d, *interp_1d;
7241203703bSJeremy L Thompson 
725ea61e9acSJeremy 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.
7262247a93fSRezgar Shakeri   CeedCall(CeedBasisGetCeed(basis, &ceed));
7272247a93fSRezgar Shakeri   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
7282247a93fSRezgar Shakeri   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
7297a982d89SJeremy L. Thompson 
7302247a93fSRezgar Shakeri   // Compute interp_1d^+, pseudoinverse of interp_1d
7312247a93fSRezgar Shakeri   CeedCall(CeedCalloc(P_1d * Q_1d, &interp_1d_pinv));
7321203703bSJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
7331203703bSJeremy L Thompson   CeedCall(CeedMatrixPseudoinverse(ceed, interp_1d, Q_1d, P_1d, interp_1d_pinv));
7341203703bSJeremy L Thompson   CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
7351203703bSJeremy L Thompson   CeedCall(CeedMatrixMatrixMultiply(ceed, grad_1d, (const CeedScalar *)interp_1d_pinv, collo_grad_1d, Q_1d, Q_1d, P_1d));
7367a982d89SJeremy L. Thompson 
7372247a93fSRezgar Shakeri   CeedCall(CeedFree(&interp_1d_pinv));
7389bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
739e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7407a982d89SJeremy L. Thompson }
7417a982d89SJeremy L. Thompson 
7427a982d89SJeremy L. Thompson /**
743b0cc4569SJeremy L Thompson   @brief Return 1D interpolation matrix to Chebyshev polynomial coefficients on quadrature space
744b0cc4569SJeremy L Thompson 
745b0cc4569SJeremy L Thompson   @param[in]  basis               `CeedBasis`
746b0cc4569SJeremy L Thompson   @param[out] chebyshev_interp_1d Row-major (`P_1d * Q_1d`) matrix interpolating from basis nodes to Chebyshev polynomial coefficients
747b0cc4569SJeremy L Thompson 
748b0cc4569SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
749b0cc4569SJeremy L Thompson 
750b0cc4569SJeremy L Thompson   @ref Backend
751b0cc4569SJeremy L Thompson **/
752b0cc4569SJeremy L Thompson int CeedBasisGetChebyshevInterp1D(CeedBasis basis, CeedScalar *chebyshev_interp_1d) {
753b0cc4569SJeremy L Thompson   CeedInt           P_1d, Q_1d;
754b0cc4569SJeremy L Thompson   CeedScalar       *C, *chebyshev_coeffs_1d_inv;
755b0cc4569SJeremy L Thompson   const CeedScalar *interp_1d, *q_ref_1d;
756b0cc4569SJeremy L Thompson   Ceed              ceed;
757b0cc4569SJeremy L Thompson 
758b0cc4569SJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis, &ceed));
759b0cc4569SJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
760b0cc4569SJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
761b0cc4569SJeremy L Thompson 
762b0cc4569SJeremy L Thompson   // Build coefficient matrix
763bd83cbc5SJeremy L Thompson   // -- Note: Clang-tidy needs this check
764bd83cbc5SJeremy L Thompson   CeedCheck((P_1d > 0) && (Q_1d > 0), ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis dimensions are malformed");
765b0cc4569SJeremy L Thompson   CeedCall(CeedCalloc(Q_1d * Q_1d, &C));
766b0cc4569SJeremy L Thompson   CeedCall(CeedBasisGetQRef(basis, &q_ref_1d));
767b0cc4569SJeremy L Thompson   for (CeedInt i = 0; i < Q_1d; i++) CeedCall(CeedChebyshevPolynomialsAtPoint(q_ref_1d[i], Q_1d, &C[i * Q_1d]));
768b0cc4569SJeremy L Thompson 
769b0cc4569SJeremy L Thompson   // Compute C^+, pseudoinverse of coefficient matrix
770b0cc4569SJeremy L Thompson   CeedCall(CeedCalloc(Q_1d * Q_1d, &chebyshev_coeffs_1d_inv));
771b0cc4569SJeremy L Thompson   CeedCall(CeedMatrixPseudoinverse(ceed, C, Q_1d, Q_1d, chebyshev_coeffs_1d_inv));
772b0cc4569SJeremy L Thompson 
773b0cc4569SJeremy L Thompson   // Build mapping from nodes to Chebyshev coefficients
774b0cc4569SJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
775b0cc4569SJeremy L Thompson   CeedCall(CeedMatrixMatrixMultiply(ceed, chebyshev_coeffs_1d_inv, interp_1d, chebyshev_interp_1d, Q_1d, P_1d, Q_1d));
776b0cc4569SJeremy L Thompson 
777b0cc4569SJeremy L Thompson   // Cleanup
778b0cc4569SJeremy L Thompson   CeedCall(CeedFree(&C));
779b0cc4569SJeremy L Thompson   CeedCall(CeedFree(&chebyshev_coeffs_1d_inv));
7809bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
781b0cc4569SJeremy L Thompson   return CEED_ERROR_SUCCESS;
782b0cc4569SJeremy L Thompson }
783b0cc4569SJeremy L Thompson 
784b0cc4569SJeremy L Thompson /**
785ca94c3ddSJeremy L Thompson   @brief Get tensor status for given `CeedBasis`
7867a982d89SJeremy L. Thompson 
787ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis`
788d1d35e2fSjeremylt   @param[out] is_tensor Variable to store tensor status
7897a982d89SJeremy L. Thompson 
7907a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
7917a982d89SJeremy L. Thompson 
7927a982d89SJeremy L. Thompson   @ref Backend
7937a982d89SJeremy L. Thompson **/
794d1d35e2fSjeremylt int CeedBasisIsTensor(CeedBasis basis, bool *is_tensor) {
7956402da51SJeremy L Thompson   *is_tensor = basis->is_tensor_basis;
796e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7977a982d89SJeremy L. Thompson }
7987a982d89SJeremy L. Thompson 
7997a982d89SJeremy L. Thompson /**
800ca62d558SJeremy L Thompson   @brief Determine if given `CeedBasis` has nodes collocated with quadrature points
801ca62d558SJeremy L Thompson 
802ca62d558SJeremy L Thompson   @param[in]  basis         `CeedBasis`
803aa4b4a9fSJeremy L Thompson   @param[out] is_collocated Variable to store collocated status
804ca62d558SJeremy L Thompson 
805ca62d558SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
806ca62d558SJeremy L Thompson 
807ca62d558SJeremy L Thompson   @ref Backend
808ca62d558SJeremy L Thompson **/
809ca62d558SJeremy L Thompson int CeedBasisIsCollocated(CeedBasis basis, bool *is_collocated) {
810ca62d558SJeremy L Thompson   if (basis->is_tensor_basis && (basis->Q_1d == basis->P_1d)) {
811ca62d558SJeremy L Thompson     *is_collocated = true;
812ca62d558SJeremy L Thompson 
813ca62d558SJeremy L Thompson     for (CeedInt i = 0; i < basis->P_1d; i++) {
814ca62d558SJeremy L Thompson       *is_collocated = *is_collocated && (fabs(basis->interp_1d[i + basis->P_1d * i] - 1.0) < 10 * CEED_EPSILON);
815ca62d558SJeremy L Thompson       for (CeedInt j = 0; j < basis->Q_1d; j++) {
816ca62d558SJeremy L Thompson         if (j != i) *is_collocated = *is_collocated && (fabs(basis->interp_1d[j + basis->P_1d * i]) < 10 * CEED_EPSILON);
817ca62d558SJeremy L Thompson       }
818ca62d558SJeremy L Thompson     }
819ca62d558SJeremy L Thompson   } else {
820ca62d558SJeremy L Thompson     *is_collocated = false;
821ca62d558SJeremy L Thompson   }
822ca62d558SJeremy L Thompson   return CEED_ERROR_SUCCESS;
823ca62d558SJeremy L Thompson }
824ca62d558SJeremy L Thompson 
825ca62d558SJeremy L Thompson /**
826ca94c3ddSJeremy L Thompson   @brief Get backend data of a `CeedBasis`
8277a982d89SJeremy L. Thompson 
828ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
8297a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
8307a982d89SJeremy L. Thompson 
8317a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
8327a982d89SJeremy L. Thompson 
8337a982d89SJeremy L. Thompson   @ref Backend
8347a982d89SJeremy L. Thompson **/
835777ff853SJeremy L Thompson int CeedBasisGetData(CeedBasis basis, void *data) {
836777ff853SJeremy L Thompson   *(void **)data = basis->data;
837e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8387a982d89SJeremy L. Thompson }
8397a982d89SJeremy L. Thompson 
8407a982d89SJeremy L. Thompson /**
841ca94c3ddSJeremy L Thompson   @brief Set backend data of a `CeedBasis`
8427a982d89SJeremy L. Thompson 
843ca94c3ddSJeremy L Thompson   @param[in,out] basis  `CeedBasis`
844ea61e9acSJeremy L Thompson   @param[in]     data   Data to set
8457a982d89SJeremy L. Thompson 
8467a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
8477a982d89SJeremy L. Thompson 
8487a982d89SJeremy L. Thompson   @ref Backend
8497a982d89SJeremy L. Thompson **/
850777ff853SJeremy L Thompson int CeedBasisSetData(CeedBasis basis, void *data) {
851777ff853SJeremy L Thompson   basis->data = data;
852e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8537a982d89SJeremy L. Thompson }
8547a982d89SJeremy L. Thompson 
8557a982d89SJeremy L. Thompson /**
856ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedBasis`
85734359f16Sjeremylt 
858ca94c3ddSJeremy L Thompson   @param[in,out] basis `CeedBasis` to increment the reference counter
85934359f16Sjeremylt 
86034359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
86134359f16Sjeremylt 
86234359f16Sjeremylt   @ref Backend
86334359f16Sjeremylt **/
8649560d06aSjeremylt int CeedBasisReference(CeedBasis basis) {
865*b0f67a9cSJeremy L Thompson   CeedCall(CeedObjectReference((CeedObject)basis));
86634359f16Sjeremylt   return CEED_ERROR_SUCCESS;
86734359f16Sjeremylt }
86834359f16Sjeremylt 
86934359f16Sjeremylt /**
870ca94c3ddSJeremy L Thompson   @brief Get number of Q-vector components for given `CeedBasis`
871c4e3f59bSSebastian Grimberg 
872ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis`
873ca94c3ddSJeremy L Thompson   @param[in]  eval_mode @ref CEED_EVAL_INTERP to use interpolated values,
874ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_GRAD to use gradients,
875ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_DIV to use divergence,
876ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_CURL to use curl
877c4e3f59bSSebastian Grimberg   @param[out] q_comp    Variable to store number of Q-vector components of basis
878c4e3f59bSSebastian Grimberg 
879c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
880c4e3f59bSSebastian Grimberg 
881c4e3f59bSSebastian Grimberg   @ref Backend
882c4e3f59bSSebastian Grimberg **/
883c4e3f59bSSebastian Grimberg int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode, CeedInt *q_comp) {
8841203703bSJeremy L Thompson   CeedInt dim;
8851203703bSJeremy L Thompson 
8861203703bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
887c4e3f59bSSebastian Grimberg   switch (eval_mode) {
8881203703bSJeremy L Thompson     case CEED_EVAL_INTERP: {
8891203703bSJeremy L Thompson       CeedFESpace fe_space;
8901203703bSJeremy L Thompson 
8911203703bSJeremy L Thompson       CeedCall(CeedBasisGetFESpace(basis, &fe_space));
8921203703bSJeremy L Thompson       *q_comp = (fe_space == CEED_FE_SPACE_H1) ? 1 : dim;
8931203703bSJeremy L Thompson     } break;
894c4e3f59bSSebastian Grimberg     case CEED_EVAL_GRAD:
8951203703bSJeremy L Thompson       *q_comp = dim;
896c4e3f59bSSebastian Grimberg       break;
897c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
898c4e3f59bSSebastian Grimberg       *q_comp = 1;
899c4e3f59bSSebastian Grimberg       break;
900c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
9011203703bSJeremy L Thompson       *q_comp = (dim < 3) ? 1 : dim;
902c4e3f59bSSebastian Grimberg       break;
903c4e3f59bSSebastian Grimberg     case CEED_EVAL_NONE:
904c4e3f59bSSebastian Grimberg     case CEED_EVAL_WEIGHT:
905352a5e7cSSebastian Grimberg       *q_comp = 1;
906c4e3f59bSSebastian Grimberg       break;
907c4e3f59bSSebastian Grimberg   }
908c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
909c4e3f59bSSebastian Grimberg }
910c4e3f59bSSebastian Grimberg 
911c4e3f59bSSebastian Grimberg /**
912ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs required to apply `CeedBasis` in `t_mode` and `eval_mode`
9136e15d496SJeremy L Thompson 
914ca94c3ddSJeremy L Thompson   @param[in]  basis        `CeedBasis` to estimate FLOPs for
915ea61e9acSJeremy L Thompson   @param[in]  t_mode       Apply basis or transpose
916ca94c3ddSJeremy L Thompson   @param[in]  eval_mode    @ref CeedEvalMode
9173f919cbcSJeremy L Thompson   @param[in]  is_at_points Evaluate the basis at points or quadrature points
9183f919cbcSJeremy L Thompson   @param[in]  num_points   Number of points basis is evaluated at
919ea61e9acSJeremy L Thompson   @param[out] flops        Address of variable to hold FLOPs estimate
9206e15d496SJeremy L Thompson 
9216e15d496SJeremy L Thompson   @ref Backend
9226e15d496SJeremy L Thompson **/
9233f919cbcSJeremy L Thompson int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, bool is_at_points, CeedInt num_points,
9243f919cbcSJeremy L Thompson                               CeedSize *flops) {
9256e15d496SJeremy L Thompson   bool is_tensor;
9266e15d496SJeremy L Thompson 
9272b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor));
9283f919cbcSJeremy L Thompson   CeedCheck(!is_at_points || is_tensor, CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Can only evaluate tensor-product bases at points");
9296e15d496SJeremy L Thompson   if (is_tensor) {
9306e15d496SJeremy L Thompson     CeedInt dim, num_comp, P_1d, Q_1d;
9311c66c397SJeremy L Thompson 
9322b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis, &dim));
9332b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
9342b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
9352b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
9366e15d496SJeremy L Thompson     if (t_mode == CEED_TRANSPOSE) {
9372b730f8bSJeremy L Thompson       P_1d = Q_1d;
9382b730f8bSJeremy L Thompson       Q_1d = P_1d;
9396e15d496SJeremy L Thompson     }
9406e15d496SJeremy L Thompson     CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim - 1), post = 1;
9413f919cbcSJeremy L Thompson 
9426e15d496SJeremy L Thompson     for (CeedInt d = 0; d < dim; d++) {
9436e15d496SJeremy L Thompson       tensor_flops += 2 * pre * P_1d * post * Q_1d;
9446e15d496SJeremy L Thompson       pre /= P_1d;
9456e15d496SJeremy L Thompson       post *= Q_1d;
9466e15d496SJeremy L Thompson     }
9473f919cbcSJeremy L Thompson     if (is_at_points) {
94852780386SJeremy L Thompson       bool is_gpu = false;
94952780386SJeremy L Thompson 
95052780386SJeremy L Thompson       {
95152780386SJeremy L Thompson         CeedMemType mem_type;
95252780386SJeremy L Thompson 
95352780386SJeremy L Thompson         CeedCall(CeedGetPreferredMemType(CeedBasisReturnCeed(basis), &mem_type));
95452780386SJeremy L Thompson         is_gpu = mem_type == CEED_MEM_DEVICE;
95552780386SJeremy L Thompson       }
95652780386SJeremy L Thompson 
9573f919cbcSJeremy L Thompson       CeedInt chebyshev_flops = (Q_1d - 2) * 3 + 1, d_chebyshev_flops = (Q_1d - 2) * 8 + 1;
9583f919cbcSJeremy L Thompson       CeedInt point_tensor_flops = 0, pre = CeedIntPow(Q_1d, dim - 1), post = 1;
9593f919cbcSJeremy L Thompson 
9603f919cbcSJeremy L Thompson       for (CeedInt d = 0; d < dim; d++) {
9613f919cbcSJeremy L Thompson         point_tensor_flops += 2 * pre * Q_1d * post * 1;
9623f919cbcSJeremy L Thompson         pre /= P_1d;
9633f919cbcSJeremy L Thompson         post *= Q_1d;
9643f919cbcSJeremy L Thompson       }
9653f919cbcSJeremy L Thompson 
9663f919cbcSJeremy L Thompson       switch (eval_mode) {
9673f919cbcSJeremy L Thompson         case CEED_EVAL_NONE:
9683f919cbcSJeremy L Thompson           *flops = 0;
9693f919cbcSJeremy L Thompson           break;
970a82cd097SZach Atkins         case CEED_EVAL_INTERP: {
971a82cd097SZach Atkins           *flops = tensor_flops + num_points * num_comp * (point_tensor_flops + (t_mode == CEED_TRANSPOSE ? CeedIntPow(Q_1d, dim) : 0));
972a82cd097SZach Atkins           if (dim == 3 && is_gpu) {
973802d760aSJeremy L Thompson             *flops += num_points * Q_1d *
974802d760aSJeremy L Thompson                       (chebyshev_flops + num_comp * (2 * chebyshev_flops + 2 * Q_1d * Q_1d + (t_mode == CEED_TRANSPOSE ? 2 * Q_1d + 1 : 3 * Q_1d)));
975a82cd097SZach Atkins           } else {
976a82cd097SZach Atkins             *flops += num_points * (is_gpu ? num_comp : 1) * dim * chebyshev_flops;
977a82cd097SZach Atkins           }
9783f919cbcSJeremy L Thompson           break;
979a82cd097SZach Atkins         }
980a82cd097SZach Atkins         case CEED_EVAL_GRAD: {
981a82cd097SZach Atkins           *flops = tensor_flops + num_points * num_comp * (point_tensor_flops + (t_mode == CEED_TRANSPOSE ? CeedIntPow(Q_1d, dim) : 0));
982a82cd097SZach Atkins           if (dim == 3 && is_gpu) {
983802d760aSJeremy L Thompson             CeedInt inner_flops =
984dc7b9553SJeremy L Thompson                 dim * (2 * Q_1d * Q_1d + (t_mode == CEED_TRANSPOSE ? 2 : 3) * Q_1d) + (dim - 1) * (2 * chebyshev_flops + d_chebyshev_flops);
985802d760aSJeremy L Thompson 
98627a8a650SZach Atkins             *flops += num_points * Q_1d * (chebyshev_flops + d_chebyshev_flops + num_comp * (inner_flops + (t_mode == CEED_TRANSPOSE ? 1 : 0)));
987a82cd097SZach Atkins           } else {
988a82cd097SZach Atkins             *flops += num_points * (is_gpu ? num_comp : 1) * dim * (d_chebyshev_flops + (dim - 1) * chebyshev_flops);
989a82cd097SZach Atkins           }
9903f919cbcSJeremy L Thompson           break;
991a82cd097SZach Atkins         }
9923f919cbcSJeremy L Thompson         case CEED_EVAL_DIV:
9933f919cbcSJeremy L Thompson         case CEED_EVAL_CURL: {
9943f919cbcSJeremy L Thompson           // LCOV_EXCL_START
99552780386SJeremy L Thompson           return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported at points",
9963f919cbcSJeremy L Thompson                            CeedEvalModes[eval_mode]);
9973f919cbcSJeremy L Thompson           break;
9983f919cbcSJeremy L Thompson           // LCOV_EXCL_STOP
9993f919cbcSJeremy L Thompson         }
10003f919cbcSJeremy L Thompson         case CEED_EVAL_WEIGHT:
10013f919cbcSJeremy L Thompson           *flops = num_points;
10023f919cbcSJeremy L Thompson           break;
10033f919cbcSJeremy L Thompson       }
10043f919cbcSJeremy L Thompson     } else {
10056e15d496SJeremy L Thompson       switch (eval_mode) {
10062b730f8bSJeremy L Thompson         case CEED_EVAL_NONE:
10072b730f8bSJeremy L Thompson           *flops = 0;
10082b730f8bSJeremy L Thompson           break;
10092b730f8bSJeremy L Thompson         case CEED_EVAL_INTERP:
10102b730f8bSJeremy L Thompson           *flops = tensor_flops;
10112b730f8bSJeremy L Thompson           break;
10122b730f8bSJeremy L Thompson         case CEED_EVAL_GRAD:
10132b730f8bSJeremy L Thompson           *flops = tensor_flops * 2;
10142b730f8bSJeremy L Thompson           break;
10156e15d496SJeremy L Thompson         case CEED_EVAL_DIV:
10161203703bSJeremy L Thompson         case CEED_EVAL_CURL: {
10176574a04fSJeremy L Thompson           // LCOV_EXCL_START
10186e536b99SJeremy L Thompson           return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported",
10196e536b99SJeremy L Thompson                            CeedEvalModes[eval_mode]);
10202b730f8bSJeremy L Thompson           break;
10216e15d496SJeremy L Thompson           // LCOV_EXCL_STOP
10221203703bSJeremy L Thompson         }
10232b730f8bSJeremy L Thompson         case CEED_EVAL_WEIGHT:
10242b730f8bSJeremy L Thompson           *flops = dim * CeedIntPow(Q_1d, dim);
10252b730f8bSJeremy L Thompson           break;
10266e15d496SJeremy L Thompson       }
10273f919cbcSJeremy L Thompson     }
10286e15d496SJeremy L Thompson   } else {
1029c4e3f59bSSebastian Grimberg     CeedInt dim, num_comp, q_comp, num_nodes, num_qpts;
10301c66c397SJeremy L Thompson 
10312b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis, &dim));
10322b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
1033c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
10342b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
10352b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
10366e15d496SJeremy L Thompson     switch (eval_mode) {
10372b730f8bSJeremy L Thompson       case CEED_EVAL_NONE:
10382b730f8bSJeremy L Thompson         *flops = 0;
10392b730f8bSJeremy L Thompson         break;
10402b730f8bSJeremy L Thompson       case CEED_EVAL_INTERP:
10412b730f8bSJeremy L Thompson       case CEED_EVAL_GRAD:
10422b730f8bSJeremy L Thompson       case CEED_EVAL_DIV:
10432b730f8bSJeremy L Thompson       case CEED_EVAL_CURL:
1044c4e3f59bSSebastian Grimberg         *flops = num_nodes * num_qpts * num_comp * q_comp;
10452b730f8bSJeremy L Thompson         break;
10462b730f8bSJeremy L Thompson       case CEED_EVAL_WEIGHT:
10472b730f8bSJeremy L Thompson         *flops = 0;
10482b730f8bSJeremy L Thompson         break;
10496e15d496SJeremy L Thompson     }
10506e15d496SJeremy L Thompson   }
10516e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10526e15d496SJeremy L Thompson }
10536e15d496SJeremy L Thompson 
10546e15d496SJeremy L Thompson /**
1055ca94c3ddSJeremy L Thompson   @brief Get `CeedFESpace` for a `CeedBasis`
1056c4e3f59bSSebastian Grimberg 
1057ca94c3ddSJeremy L Thompson   @param[in]  basis    `CeedBasis`
1058ca94c3ddSJeremy L Thompson   @param[out] fe_space Variable to store `CeedFESpace`
1059c4e3f59bSSebastian Grimberg 
1060c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1061c4e3f59bSSebastian Grimberg 
1062c4e3f59bSSebastian Grimberg   @ref Backend
1063c4e3f59bSSebastian Grimberg **/
1064c4e3f59bSSebastian Grimberg int CeedBasisGetFESpace(CeedBasis basis, CeedFESpace *fe_space) {
1065c4e3f59bSSebastian Grimberg   *fe_space = basis->fe_space;
1066c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1067c4e3f59bSSebastian Grimberg }
1068c4e3f59bSSebastian Grimberg 
1069c4e3f59bSSebastian Grimberg /**
1070ca94c3ddSJeremy L Thompson   @brief Get dimension for given `CeedElemTopology`
10717a982d89SJeremy L. Thompson 
1072ca94c3ddSJeremy L Thompson   @param[in]  topo `CeedElemTopology`
10737a982d89SJeremy L. Thompson   @param[out] dim  Variable to store dimension of topology
10747a982d89SJeremy L. Thompson 
10757a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
10767a982d89SJeremy L. Thompson 
10777a982d89SJeremy L. Thompson   @ref Backend
10787a982d89SJeremy L. Thompson **/
10797a982d89SJeremy L. Thompson int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim) {
10807a982d89SJeremy L. Thompson   *dim = (CeedInt)topo >> 16;
1081e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10827a982d89SJeremy L. Thompson }
10837a982d89SJeremy L. Thompson 
10847a982d89SJeremy L. Thompson /**
1085ca94c3ddSJeremy L Thompson   @brief Get `CeedTensorContract` of a `CeedBasis`
10867a982d89SJeremy L. Thompson 
1087ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis`
1088ca94c3ddSJeremy L Thompson   @param[out] contract  Variable to store `CeedTensorContract`
10897a982d89SJeremy L. Thompson 
10907a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
10917a982d89SJeremy L. Thompson 
10927a982d89SJeremy L. Thompson   @ref Backend
10937a982d89SJeremy L. Thompson **/
10947a982d89SJeremy L. Thompson int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract) {
10957a982d89SJeremy L. Thompson   *contract = basis->contract;
1096e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10977a982d89SJeremy L. Thompson }
10987a982d89SJeremy L. Thompson 
10997a982d89SJeremy L. Thompson /**
1100ca94c3ddSJeremy L Thompson   @brief Set `CeedTensorContract` of a `CeedBasis`
11017a982d89SJeremy L. Thompson 
1102ca94c3ddSJeremy L Thompson   @param[in,out] basis    `CeedBasis`
1103ca94c3ddSJeremy L Thompson   @param[in]     contract `CeedTensorContract` to set
11047a982d89SJeremy L. Thompson 
11057a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
11067a982d89SJeremy L. Thompson 
11077a982d89SJeremy L. Thompson   @ref Backend
11087a982d89SJeremy L. Thompson **/
110934359f16Sjeremylt int CeedBasisSetTensorContract(CeedBasis basis, CeedTensorContract contract) {
111034359f16Sjeremylt   basis->contract = contract;
11112b730f8bSJeremy L Thompson   CeedCall(CeedTensorContractReference(contract));
1112e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11137a982d89SJeremy L. Thompson }
11147a982d89SJeremy L. Thompson 
11157a982d89SJeremy L. Thompson /**
1116ca94c3ddSJeremy L Thompson   @brief Return a reference implementation of matrix multiplication \f$C = A B\f$.
1117ba59ac12SSebastian Grimberg 
1118ca94c3ddSJeremy L Thompson   Note: This is a reference implementation for CPU `CeedScalar` pointers that is not intended for high performance.
11197a982d89SJeremy L. Thompson 
1120ca94c3ddSJeremy L Thompson   @param[in]  ceed  `Ceed` context for error handling
1121ca94c3ddSJeremy L Thompson   @param[in]  mat_A Row-major matrix `A`
1122ca94c3ddSJeremy L Thompson   @param[in]  mat_B Row-major matrix `B`
1123ca94c3ddSJeremy L Thompson   @param[out] mat_C Row-major output matrix `C`
1124ca94c3ddSJeremy L Thompson   @param[in]  m     Number of rows of `C`
1125ca94c3ddSJeremy L Thompson   @param[in]  n     Number of columns of `C`
1126ca94c3ddSJeremy L Thompson   @param[in]  kk    Number of columns of `A`/rows of `B`
11277a982d89SJeremy L. Thompson 
11287a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
11297a982d89SJeremy L. Thompson 
11307a982d89SJeremy L. Thompson   @ref Utility
11317a982d89SJeremy L. Thompson **/
11322b730f8bSJeremy L Thompson int CeedMatrixMatrixMultiply(Ceed ceed, const CeedScalar *mat_A, const CeedScalar *mat_B, CeedScalar *mat_C, CeedInt m, CeedInt n, CeedInt kk) {
11332b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < m; i++) {
11347a982d89SJeremy L. Thompson     for (CeedInt j = 0; j < n; j++) {
11357a982d89SJeremy L. Thompson       CeedScalar sum = 0;
11361c66c397SJeremy L Thompson 
11372b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < kk; k++) sum += mat_A[k + i * kk] * mat_B[j + k * n];
1138d1d35e2fSjeremylt       mat_C[j + i * n] = sum;
11397a982d89SJeremy L. Thompson     }
11402b730f8bSJeremy L Thompson   }
1141e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11427a982d89SJeremy L. Thompson }
11437a982d89SJeremy L. Thompson 
1144ba59ac12SSebastian Grimberg /**
1145ba59ac12SSebastian Grimberg   @brief Return QR Factorization of a matrix
1146ba59ac12SSebastian Grimberg 
1147ca94c3ddSJeremy L Thompson   @param[in]     ceed `Ceed` context for error handling
1148ba59ac12SSebastian Grimberg   @param[in,out] mat  Row-major matrix to be factorized in place
1149ca94c3ddSJeremy L Thompson   @param[in,out] tau  Vector of length `m` of scaling factors
1150ba59ac12SSebastian Grimberg   @param[in]     m    Number of rows
1151ba59ac12SSebastian Grimberg   @param[in]     n    Number of columns
1152ba59ac12SSebastian Grimberg 
1153ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1154ba59ac12SSebastian Grimberg 
1155ba59ac12SSebastian Grimberg   @ref Utility
1156ba59ac12SSebastian Grimberg **/
1157ba59ac12SSebastian Grimberg int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau, CeedInt m, CeedInt n) {
1158ba59ac12SSebastian Grimberg   CeedScalar v[m];
1159ba59ac12SSebastian Grimberg 
1160ba59ac12SSebastian Grimberg   // Check matrix shape
11616574a04fSJeremy L Thompson   CeedCheck(n <= m, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute QR factorization with n > m");
1162ba59ac12SSebastian Grimberg 
1163ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
11641c66c397SJeremy L Thompson     CeedScalar sigma = 0.0;
11651c66c397SJeremy L Thompson 
1166ba59ac12SSebastian Grimberg     if (i >= m - 1) {  // last row of matrix, no reflection needed
1167ba59ac12SSebastian Grimberg       tau[i] = 0.;
1168ba59ac12SSebastian Grimberg       break;
1169ba59ac12SSebastian Grimberg     }
1170ba59ac12SSebastian Grimberg     // Calculate Householder vector, magnitude
1171ba59ac12SSebastian Grimberg     v[i] = mat[i + n * i];
1172ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) {
1173ba59ac12SSebastian Grimberg       v[j] = mat[i + n * j];
1174ba59ac12SSebastian Grimberg       sigma += v[j] * v[j];
1175ba59ac12SSebastian Grimberg     }
11761c66c397SJeremy L Thompson     const CeedScalar norm = sqrt(v[i] * v[i] + sigma);  // norm of v[i:m]
11771c66c397SJeremy L Thompson     const CeedScalar R_ii = -copysign(norm, v[i]);
11781c66c397SJeremy L Thompson 
1179ba59ac12SSebastian Grimberg     v[i] -= R_ii;
1180ba59ac12SSebastian Grimberg     // norm of v[i:m] after modification above and scaling below
1181ba59ac12SSebastian Grimberg     //   norm = sqrt(v[i]*v[i] + sigma) / v[i];
1182ba59ac12SSebastian Grimberg     //   tau = 2 / (norm*norm)
1183ba59ac12SSebastian Grimberg     tau[i] = 2 * v[i] * v[i] / (v[i] * v[i] + sigma);
1184ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) v[j] /= v[i];
1185ba59ac12SSebastian Grimberg 
1186ba59ac12SSebastian Grimberg     // Apply Householder reflector to lower right panel
1187ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat[i * n + i + 1], &v[i], tau[i], m - i, n - i - 1, n, 1);
1188ba59ac12SSebastian Grimberg     // Save v
1189ba59ac12SSebastian Grimberg     mat[i + n * i] = R_ii;
1190ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) mat[i + n * j] = v[j];
1191ba59ac12SSebastian Grimberg   }
1192ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
1193ba59ac12SSebastian Grimberg }
1194ba59ac12SSebastian Grimberg 
1195ba59ac12SSebastian Grimberg /**
1196ba59ac12SSebastian Grimberg   @brief Apply Householder Q matrix
1197ba59ac12SSebastian Grimberg 
1198ca94c3ddSJeremy L Thompson   Compute `mat_A = mat_Q mat_A`, where `mat_Q` is \f$m \times m\f$ and `mat_A` is \f$m \times n\f$.
1199ba59ac12SSebastian Grimberg 
1200ba59ac12SSebastian Grimberg   @param[in,out] mat_A  Matrix to apply Householder Q to, in place
1201ba59ac12SSebastian Grimberg   @param[in]     mat_Q  Householder Q matrix
1202ba59ac12SSebastian Grimberg   @param[in]     tau    Householder scaling factors
1203ba59ac12SSebastian Grimberg   @param[in]     t_mode Transpose mode for application
1204ca94c3ddSJeremy L Thompson   @param[in]     m      Number of rows in `A`
1205ca94c3ddSJeremy L Thompson   @param[in]     n      Number of columns in `A`
1206ca94c3ddSJeremy L Thompson   @param[in]     k      Number of elementary reflectors in Q, `k < m`
1207ca94c3ddSJeremy L Thompson   @param[in]     row    Row stride in `A`
1208ca94c3ddSJeremy L Thompson   @param[in]     col    Col stride in `A`
1209ba59ac12SSebastian Grimberg 
1210ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1211ba59ac12SSebastian Grimberg 
1212c4e3f59bSSebastian Grimberg   @ref Utility
1213ba59ac12SSebastian Grimberg **/
1214ba59ac12SSebastian Grimberg int CeedHouseholderApplyQ(CeedScalar *mat_A, const CeedScalar *mat_Q, const CeedScalar *tau, CeedTransposeMode t_mode, CeedInt m, CeedInt n,
1215ba59ac12SSebastian Grimberg                           CeedInt k, CeedInt row, CeedInt col) {
1216ba59ac12SSebastian Grimberg   CeedScalar *v;
12171c66c397SJeremy L Thompson 
1218ba59ac12SSebastian Grimberg   CeedCall(CeedMalloc(m, &v));
1219ba59ac12SSebastian Grimberg   for (CeedInt ii = 0; ii < k; ii++) {
1220ba59ac12SSebastian Grimberg     CeedInt i = t_mode == CEED_TRANSPOSE ? ii : k - 1 - ii;
1221ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) v[j] = mat_Q[j * k + i];
1222ba59ac12SSebastian Grimberg     // Apply Householder reflector (I - tau v v^T) collo_grad_1d^T
1223ba59ac12SSebastian Grimberg     CeedCall(CeedHouseholderReflect(&mat_A[i * row], &v[i], tau[i], m - i, n, row, col));
1224ba59ac12SSebastian Grimberg   }
1225ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&v));
1226ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
1227ba59ac12SSebastian Grimberg }
1228ba59ac12SSebastian Grimberg 
1229ba59ac12SSebastian Grimberg /**
12302247a93fSRezgar Shakeri   @brief Return pseudoinverse of a matrix
12312247a93fSRezgar Shakeri 
12322247a93fSRezgar Shakeri   @param[in]     ceed      Ceed context for error handling
12332247a93fSRezgar Shakeri   @param[in]     mat       Row-major matrix to compute pseudoinverse of
12342247a93fSRezgar Shakeri   @param[in]     m         Number of rows
12352247a93fSRezgar Shakeri   @param[in]     n         Number of columns
12362247a93fSRezgar Shakeri   @param[out]    mat_pinv  Row-major pseudoinverse matrix
12372247a93fSRezgar Shakeri 
12382247a93fSRezgar Shakeri   @return An error code: 0 - success, otherwise - failure
12392247a93fSRezgar Shakeri 
12402247a93fSRezgar Shakeri   @ref Utility
12412247a93fSRezgar Shakeri **/
12421203703bSJeremy L Thompson int CeedMatrixPseudoinverse(Ceed ceed, const CeedScalar *mat, CeedInt m, CeedInt n, CeedScalar *mat_pinv) {
12432247a93fSRezgar Shakeri   CeedScalar *tau, *I, *mat_copy;
12442247a93fSRezgar Shakeri 
12452247a93fSRezgar Shakeri   CeedCall(CeedCalloc(m, &tau));
12462247a93fSRezgar Shakeri   CeedCall(CeedCalloc(m * m, &I));
12472247a93fSRezgar Shakeri   CeedCall(CeedCalloc(m * n, &mat_copy));
12482247a93fSRezgar Shakeri   memcpy(mat_copy, mat, m * n * sizeof mat[0]);
12492247a93fSRezgar Shakeri 
12502247a93fSRezgar Shakeri   // QR Factorization, mat = Q R
12512247a93fSRezgar Shakeri   CeedCall(CeedQRFactorization(ceed, mat_copy, tau, m, n));
12522247a93fSRezgar Shakeri 
12532247a93fSRezgar Shakeri   // -- Apply Q^T, I = Q^T * I
12542247a93fSRezgar Shakeri   for (CeedInt i = 0; i < m; i++) I[i * m + i] = 1.0;
12552247a93fSRezgar Shakeri   CeedCall(CeedHouseholderApplyQ(I, mat_copy, tau, CEED_TRANSPOSE, m, m, n, m, 1));
12562247a93fSRezgar Shakeri   // -- Apply R_inv, mat_pinv = R_inv * Q^T
12572247a93fSRezgar Shakeri   for (CeedInt j = 0; j < m; j++) {  // Column j
12582247a93fSRezgar Shakeri     mat_pinv[j + m * (n - 1)] = I[j + m * (n - 1)] / mat_copy[n * n - 1];
12592247a93fSRezgar Shakeri     for (CeedInt i = n - 2; i >= 0; i--) {  // Row i
12602247a93fSRezgar Shakeri       mat_pinv[j + m * i] = I[j + m * i];
12612247a93fSRezgar Shakeri       for (CeedInt k = i + 1; k < n; k++) mat_pinv[j + m * i] -= mat_copy[k + n * i] * mat_pinv[j + m * k];
12622247a93fSRezgar Shakeri       mat_pinv[j + m * i] /= mat_copy[i + n * i];
12632247a93fSRezgar Shakeri     }
12642247a93fSRezgar Shakeri   }
12652247a93fSRezgar Shakeri 
12662247a93fSRezgar Shakeri   // Cleanup
12672247a93fSRezgar Shakeri   CeedCall(CeedFree(&I));
12682247a93fSRezgar Shakeri   CeedCall(CeedFree(&tau));
12692247a93fSRezgar Shakeri   CeedCall(CeedFree(&mat_copy));
12702247a93fSRezgar Shakeri   return CEED_ERROR_SUCCESS;
12712247a93fSRezgar Shakeri }
12722247a93fSRezgar Shakeri 
12732247a93fSRezgar Shakeri /**
1274ba59ac12SSebastian Grimberg   @brief Return symmetric Schur decomposition of the symmetric matrix mat via symmetric QR factorization
1275ba59ac12SSebastian Grimberg 
1276ca94c3ddSJeremy L Thompson   @param[in]     ceed   `Ceed` context for error handling
1277ba59ac12SSebastian Grimberg   @param[in,out] mat    Row-major matrix to be factorized in place
1278ba59ac12SSebastian Grimberg   @param[out]    lambda Vector of length n of eigenvalues
1279ba59ac12SSebastian Grimberg   @param[in]     n      Number of rows/columns
1280ba59ac12SSebastian Grimberg 
1281ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1282ba59ac12SSebastian Grimberg 
1283ba59ac12SSebastian Grimberg   @ref Utility
1284ba59ac12SSebastian Grimberg **/
12852c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
12862c2ea1dbSJeremy L Thompson int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat, CeedScalar *lambda, CeedInt n) {
1287ba59ac12SSebastian Grimberg   // Check bounds for clang-tidy
12886574a04fSJeremy L Thompson   CeedCheck(n > 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute symmetric Schur decomposition of scalars");
1289ba59ac12SSebastian Grimberg 
1290ba59ac12SSebastian Grimberg   CeedScalar v[n - 1], tau[n - 1], mat_T[n * n];
1291ba59ac12SSebastian Grimberg 
1292ba59ac12SSebastian Grimberg   // Copy mat to mat_T and set mat to I
1293ba59ac12SSebastian Grimberg   memcpy(mat_T, mat, n * n * sizeof(mat[0]));
1294ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
1295ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < n; j++) mat[j + n * i] = (i == j) ? 1 : 0;
1296ba59ac12SSebastian Grimberg   }
1297ba59ac12SSebastian Grimberg 
1298ba59ac12SSebastian Grimberg   // Reduce to tridiagonal
1299ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n - 1; i++) {
1300ba59ac12SSebastian Grimberg     // Calculate Householder vector, magnitude
1301ba59ac12SSebastian Grimberg     CeedScalar sigma = 0.0;
13021c66c397SJeremy L Thompson 
1303ba59ac12SSebastian Grimberg     v[i] = mat_T[i + n * (i + 1)];
1304ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) {
1305ba59ac12SSebastian Grimberg       v[j] = mat_T[i + n * (j + 1)];
1306ba59ac12SSebastian Grimberg       sigma += v[j] * v[j];
1307ba59ac12SSebastian Grimberg     }
13081c66c397SJeremy L Thompson     const CeedScalar norm = sqrt(v[i] * v[i] + sigma);  // norm of v[i:n-1]
13091c66c397SJeremy L Thompson     const CeedScalar R_ii = -copysign(norm, v[i]);
13101c66c397SJeremy L Thompson 
1311ba59ac12SSebastian Grimberg     v[i] -= R_ii;
1312ba59ac12SSebastian Grimberg     // norm of v[i:m] after modification above and scaling below
1313ba59ac12SSebastian Grimberg     //   norm = sqrt(v[i]*v[i] + sigma) / v[i];
1314ba59ac12SSebastian Grimberg     //   tau = 2 / (norm*norm)
1315ba59ac12SSebastian Grimberg     tau[i] = i == n - 2 ? 2 : 2 * v[i] * v[i] / (v[i] * v[i] + sigma);
1316ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) v[j] /= v[i];
1317ba59ac12SSebastian Grimberg 
1318ba59ac12SSebastian Grimberg     // Update sub and super diagonal
1319ba59ac12SSebastian Grimberg     for (CeedInt j = i + 2; j < n; j++) {
1320ba59ac12SSebastian Grimberg       mat_T[i + n * j] = 0;
1321ba59ac12SSebastian Grimberg       mat_T[j + n * i] = 0;
1322ba59ac12SSebastian Grimberg     }
1323ba59ac12SSebastian Grimberg     // Apply symmetric Householder reflector to lower right panel
1324ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1);
1325ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), 1, n);
1326ba59ac12SSebastian Grimberg 
1327ba59ac12SSebastian Grimberg     // Save v
1328ba59ac12SSebastian Grimberg     mat_T[i + n * (i + 1)] = R_ii;
1329ba59ac12SSebastian Grimberg     mat_T[(i + 1) + n * i] = R_ii;
1330ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) {
1331ba59ac12SSebastian Grimberg       mat_T[i + n * (j + 1)] = v[j];
1332ba59ac12SSebastian Grimberg     }
1333ba59ac12SSebastian Grimberg   }
1334ba59ac12SSebastian Grimberg   // Backwards accumulation of Q
1335ba59ac12SSebastian Grimberg   for (CeedInt i = n - 2; i >= 0; i--) {
1336ba59ac12SSebastian Grimberg     if (tau[i] > 0.0) {
1337ba59ac12SSebastian Grimberg       v[i] = 1;
1338ba59ac12SSebastian Grimberg       for (CeedInt j = i + 1; j < n - 1; j++) {
1339ba59ac12SSebastian Grimberg         v[j]                   = mat_T[i + n * (j + 1)];
1340ba59ac12SSebastian Grimberg         mat_T[i + n * (j + 1)] = 0;
1341ba59ac12SSebastian Grimberg       }
1342ba59ac12SSebastian Grimberg       CeedHouseholderReflect(&mat[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1);
1343ba59ac12SSebastian Grimberg     }
1344ba59ac12SSebastian Grimberg   }
1345ba59ac12SSebastian Grimberg 
1346ba59ac12SSebastian Grimberg   // Reduce sub and super diagonal
1347ba59ac12SSebastian Grimberg   CeedInt    p = 0, q = 0, itr = 0, max_itr = n * n * n * n;
1348ba59ac12SSebastian Grimberg   CeedScalar tol = CEED_EPSILON;
1349ba59ac12SSebastian Grimberg 
1350ba59ac12SSebastian Grimberg   while (itr < max_itr) {
1351ba59ac12SSebastian Grimberg     // Update p, q, size of reduced portions of diagonal
1352ba59ac12SSebastian Grimberg     p = 0;
1353ba59ac12SSebastian Grimberg     q = 0;
1354ba59ac12SSebastian Grimberg     for (CeedInt i = n - 2; i >= 0; i--) {
1355ba59ac12SSebastian Grimberg       if (fabs(mat_T[i + n * (i + 1)]) < tol) q += 1;
1356ba59ac12SSebastian Grimberg       else break;
1357ba59ac12SSebastian Grimberg     }
1358ba59ac12SSebastian Grimberg     for (CeedInt i = 0; i < n - q - 1; i++) {
1359ba59ac12SSebastian Grimberg       if (fabs(mat_T[i + n * (i + 1)]) < tol) p += 1;
1360ba59ac12SSebastian Grimberg       else break;
1361ba59ac12SSebastian Grimberg     }
1362ba59ac12SSebastian Grimberg     if (q == n - 1) break;  // Finished reducing
1363ba59ac12SSebastian Grimberg 
1364ba59ac12SSebastian Grimberg     // Reduce tridiagonal portion
1365ba59ac12SSebastian Grimberg     CeedScalar t_nn = mat_T[(n - 1 - q) + n * (n - 1 - q)], t_nnm1 = mat_T[(n - 2 - q) + n * (n - 1 - q)];
1366ba59ac12SSebastian Grimberg     CeedScalar d  = (mat_T[(n - 2 - q) + n * (n - 2 - q)] - t_nn) / 2;
1367ba59ac12SSebastian Grimberg     CeedScalar mu = t_nn - t_nnm1 * t_nnm1 / (d + copysign(sqrt(d * d + t_nnm1 * t_nnm1), d));
1368ba59ac12SSebastian Grimberg     CeedScalar x  = mat_T[p + n * p] - mu;
1369ba59ac12SSebastian Grimberg     CeedScalar z  = mat_T[p + n * (p + 1)];
13701c66c397SJeremy L Thompson 
1371ba59ac12SSebastian Grimberg     for (CeedInt k = p; k < n - q - 1; k++) {
1372ba59ac12SSebastian Grimberg       // Compute Givens rotation
1373ba59ac12SSebastian Grimberg       CeedScalar c = 1, s = 0;
13741c66c397SJeremy L Thompson 
1375ba59ac12SSebastian Grimberg       if (fabs(z) > tol) {
1376ba59ac12SSebastian Grimberg         if (fabs(z) > fabs(x)) {
13771c66c397SJeremy L Thompson           const CeedScalar tau = -x / z;
13781c66c397SJeremy L Thompson 
13791c66c397SJeremy L Thompson           s = 1 / sqrt(1 + tau * tau);
13801c66c397SJeremy L Thompson           c = s * tau;
1381ba59ac12SSebastian Grimberg         } else {
13821c66c397SJeremy L Thompson           const CeedScalar tau = -z / x;
13831c66c397SJeremy L Thompson 
13841c66c397SJeremy L Thompson           c = 1 / sqrt(1 + tau * tau);
13851c66c397SJeremy L Thompson           s = c * tau;
1386ba59ac12SSebastian Grimberg         }
1387ba59ac12SSebastian Grimberg       }
1388ba59ac12SSebastian Grimberg 
1389ba59ac12SSebastian Grimberg       // Apply Givens rotation to T
1390ba59ac12SSebastian Grimberg       CeedGivensRotation(mat_T, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n);
1391ba59ac12SSebastian Grimberg       CeedGivensRotation(mat_T, c, s, CEED_TRANSPOSE, k, k + 1, n, n);
1392ba59ac12SSebastian Grimberg 
1393ba59ac12SSebastian Grimberg       // Apply Givens rotation to Q
1394ba59ac12SSebastian Grimberg       CeedGivensRotation(mat, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n);
1395ba59ac12SSebastian Grimberg 
1396ba59ac12SSebastian Grimberg       // Update x, z
1397ba59ac12SSebastian Grimberg       if (k < n - q - 2) {
1398ba59ac12SSebastian Grimberg         x = mat_T[k + n * (k + 1)];
1399ba59ac12SSebastian Grimberg         z = mat_T[k + n * (k + 2)];
1400ba59ac12SSebastian Grimberg       }
1401ba59ac12SSebastian Grimberg     }
1402ba59ac12SSebastian Grimberg     itr++;
1403ba59ac12SSebastian Grimberg   }
1404ba59ac12SSebastian Grimberg 
1405ba59ac12SSebastian Grimberg   // Save eigenvalues
1406ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) lambda[i] = mat_T[i + n * i];
1407ba59ac12SSebastian Grimberg 
1408ba59ac12SSebastian Grimberg   // Check convergence
14096574a04fSJeremy L Thompson   CeedCheck(itr < max_itr || q > n, ceed, CEED_ERROR_MINOR, "Symmetric QR failed to converge");
1410ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
1411ba59ac12SSebastian Grimberg }
14122c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
1413ba59ac12SSebastian Grimberg 
1414ba59ac12SSebastian Grimberg /**
1415ba59ac12SSebastian Grimberg   @brief Return Simultaneous Diagonalization of two matrices.
1416ba59ac12SSebastian Grimberg 
1417ca94c3ddSJeremy L Thompson   This solves the generalized eigenvalue problem `A x = lambda B x`, where `A` and `B` are symmetric and `B` is positive definite.
1418ca94c3ddSJeremy L Thompson   We generate the matrix `X` and vector `Lambda` such that `X^T A X = Lambda` and `X^T B X = I`.
1419ca94c3ddSJeremy L Thompson   This is equivalent to the LAPACK routine 'sygv' with `TYPE = 1`.
1420ba59ac12SSebastian Grimberg 
1421ca94c3ddSJeremy L Thompson   @param[in]  ceed   `Ceed` context for error handling
1422ba59ac12SSebastian Grimberg   @param[in]  mat_A  Row-major matrix to be factorized with eigenvalues
1423ba59ac12SSebastian Grimberg   @param[in]  mat_B  Row-major matrix to be factorized to identity
1424ba59ac12SSebastian Grimberg   @param[out] mat_X  Row-major orthogonal matrix
1425ca94c3ddSJeremy L Thompson   @param[out] lambda Vector of length `n` of generalized eigenvalues
1426ba59ac12SSebastian Grimberg   @param[in]  n      Number of rows/columns
1427ba59ac12SSebastian Grimberg 
1428ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1429ba59ac12SSebastian Grimberg 
1430ba59ac12SSebastian Grimberg   @ref Utility
1431ba59ac12SSebastian Grimberg **/
14322c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
14332c2ea1dbSJeremy L Thompson int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A, CeedScalar *mat_B, CeedScalar *mat_X, CeedScalar *lambda, CeedInt n) {
1434ba59ac12SSebastian Grimberg   CeedScalar *mat_C, *mat_G, *vec_D;
14351c66c397SJeremy L Thompson 
1436ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n * n, &mat_C));
1437ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n * n, &mat_G));
1438ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n, &vec_D));
1439ba59ac12SSebastian Grimberg 
1440ba59ac12SSebastian Grimberg   // Compute B = G D G^T
1441ba59ac12SSebastian Grimberg   memcpy(mat_G, mat_B, n * n * sizeof(mat_B[0]));
1442ba59ac12SSebastian Grimberg   CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_G, vec_D, n));
1443ba59ac12SSebastian Grimberg 
1444ba59ac12SSebastian Grimberg   // Sort eigenvalues
1445ba59ac12SSebastian Grimberg   for (CeedInt i = n - 1; i >= 0; i--) {
1446ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < i; j++) {
1447ba59ac12SSebastian Grimberg       if (fabs(vec_D[j]) > fabs(vec_D[j + 1])) {
14481c66c397SJeremy L Thompson         CeedScalarSwap(vec_D[j], vec_D[j + 1]);
14491c66c397SJeremy L Thompson         for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_G[k * n + j], mat_G[k * n + j + 1]);
1450ba59ac12SSebastian Grimberg       }
1451ba59ac12SSebastian Grimberg     }
1452ba59ac12SSebastian Grimberg   }
1453ba59ac12SSebastian Grimberg 
1454ba59ac12SSebastian Grimberg   // Compute C = (G D^1/2)^-1 A (G D^1/2)^-T
1455ba59ac12SSebastian Grimberg   //           = D^-1/2 G^T A G D^-1/2
1456ba59ac12SSebastian Grimberg   // -- D = D^-1/2
1457ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) vec_D[i] = 1. / sqrt(vec_D[i]);
1458ba59ac12SSebastian Grimberg   // -- G = G D^-1/2
1459ba59ac12SSebastian Grimberg   // -- C = D^-1/2 G^T
1460ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
1461ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < n; j++) {
1462ba59ac12SSebastian Grimberg       mat_G[i * n + j] *= vec_D[j];
1463ba59ac12SSebastian Grimberg       mat_C[j * n + i] = mat_G[i * n + j];
1464ba59ac12SSebastian Grimberg     }
1465ba59ac12SSebastian Grimberg   }
1466ba59ac12SSebastian Grimberg   // -- X = (D^-1/2 G^T) A
1467ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_C, (const CeedScalar *)mat_A, mat_X, n, n, n));
1468ba59ac12SSebastian Grimberg   // -- C = (D^-1/2 G^T A) (G D^-1/2)
1469ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_X, (const CeedScalar *)mat_G, mat_C, n, n, n));
1470ba59ac12SSebastian Grimberg 
1471ba59ac12SSebastian Grimberg   // Compute Q^T C Q = lambda
1472ba59ac12SSebastian Grimberg   CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_C, lambda, n));
1473ba59ac12SSebastian Grimberg 
1474ba59ac12SSebastian Grimberg   // Sort eigenvalues
1475ba59ac12SSebastian Grimberg   for (CeedInt i = n - 1; i >= 0; i--) {
1476ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < i; j++) {
1477ba59ac12SSebastian Grimberg       if (fabs(lambda[j]) > fabs(lambda[j + 1])) {
14781c66c397SJeremy L Thompson         CeedScalarSwap(lambda[j], lambda[j + 1]);
14791c66c397SJeremy L Thompson         for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_C[k * n + j], mat_C[k * n + j + 1]);
1480ba59ac12SSebastian Grimberg       }
1481ba59ac12SSebastian Grimberg     }
1482ba59ac12SSebastian Grimberg   }
1483ba59ac12SSebastian Grimberg 
1484ba59ac12SSebastian Grimberg   // Set X = (G D^1/2)^-T Q
1485ba59ac12SSebastian Grimberg   //       = G D^-1/2 Q
1486ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_G, (const CeedScalar *)mat_C, mat_X, n, n, n));
1487ba59ac12SSebastian Grimberg 
1488ba59ac12SSebastian Grimberg   // Cleanup
1489ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&mat_C));
1490ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&mat_G));
1491ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&vec_D));
1492ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
1493ba59ac12SSebastian Grimberg }
14942c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
1495ba59ac12SSebastian Grimberg 
14967a982d89SJeremy L. Thompson /// @}
14977a982d89SJeremy L. Thompson 
14987a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
14997a982d89SJeremy L. Thompson /// CeedBasis Public API
15007a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
15017a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser
1502d7b241e6Sjeremylt /// @{
1503d7b241e6Sjeremylt 
1504b11c1e72Sjeremylt /**
1505ca94c3ddSJeremy L Thompson   @brief Create a tensor-product basis for \f$H^1\f$ discretizations
1506b11c1e72Sjeremylt 
1507ca94c3ddSJeremy L Thompson   @param[in]  ceed        `Ceed` object used to create the `CeedBasis`
1508ea61e9acSJeremy L Thompson   @param[in]  dim         Topological dimension
1509ea61e9acSJeremy L Thompson   @param[in]  num_comp    Number of field components (1 for scalar fields)
1510ea61e9acSJeremy L Thompson   @param[in]  P_1d        Number of nodes in one dimension
1511ea61e9acSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
1512ca94c3ddSJeremy L Thompson   @param[in]  interp_1d   Row-major (`Q_1d * P_1d`) matrix expressing the values of nodal basis functions at quadrature points
1513ca94c3ddSJeremy L Thompson   @param[in]  grad_1d     Row-major (`Q_1d * P_1d`) matrix expressing derivatives of nodal basis functions at quadrature points
1514ca94c3ddSJeremy L Thompson   @param[in]  q_ref_1d    Array of length `Q_1d` holding the locations of quadrature points on the 1D reference element `[-1, 1]`
1515ca94c3ddSJeremy L Thompson   @param[in]  q_weight_1d Array of length `Q_1d` holding the quadrature weights on the reference element
1516ca94c3ddSJeremy L Thompson   @param[out] basis       Address of the variable where the newly created `CeedBasis` will be stored
1517b11c1e72Sjeremylt 
1518b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1519dfdf5a53Sjeremylt 
15207a982d89SJeremy L. Thompson   @ref User
1521b11c1e72Sjeremylt **/
15222b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d,
15232b730f8bSJeremy L Thompson                             const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis *basis) {
15245fe0d4faSjeremylt   if (!ceed->BasisCreateTensorH1) {
15255fe0d4faSjeremylt     Ceed delegate;
15266574a04fSJeremy L Thompson 
15272b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
15281ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateTensorH1");
15292b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis));
15309bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
1531e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
15325fe0d4faSjeremylt   }
1533e15f9bd0SJeremy L Thompson 
1534ca94c3ddSJeremy L Thompson   CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value");
1535ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1536ca94c3ddSJeremy L Thompson   CeedCheck(P_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1537ca94c3ddSJeremy L Thompson   CeedCheck(Q_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1538227444bfSJeremy L Thompson 
15392b730f8bSJeremy L Thompson   CeedElemTopology topo = dim == 1 ? CEED_TOPOLOGY_LINE : dim == 2 ? CEED_TOPOLOGY_QUAD : CEED_TOPOLOGY_HEX;
1540e15f9bd0SJeremy L Thompson 
15412b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1542*b0f67a9cSJeremy L Thompson   CeedCall(CeedObjectCreate(ceed, CeedBasisView_Object, &(*basis)->obj));
15436402da51SJeremy L Thompson   (*basis)->is_tensor_basis = true;
1544d7b241e6Sjeremylt   (*basis)->dim             = dim;
1545d99fa3c5SJeremy L Thompson   (*basis)->topo            = topo;
1546d1d35e2fSjeremylt   (*basis)->num_comp        = num_comp;
1547d1d35e2fSjeremylt   (*basis)->P_1d            = P_1d;
1548d1d35e2fSjeremylt   (*basis)->Q_1d            = Q_1d;
1549d1d35e2fSjeremylt   (*basis)->P               = CeedIntPow(P_1d, dim);
1550d1d35e2fSjeremylt   (*basis)->Q               = CeedIntPow(Q_1d, dim);
1551c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_H1;
15522b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d, &(*basis)->q_ref_1d));
15532b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d, &(*basis)->q_weight_1d));
1554ff3a0f91SJeremy L Thompson   if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d * sizeof(q_ref_1d[0]));
15552b730f8bSJeremy L Thompson   if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, Q_1d * sizeof(q_weight_1d[0]));
15562b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->interp_1d));
15572b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->grad_1d));
15582b730f8bSJeremy L Thompson   if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, Q_1d * P_1d * sizeof(interp_1d[0]));
1559ff3a0f91SJeremy L Thompson   if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d * P_1d * sizeof(grad_1d[0]));
15602b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, *basis));
1561e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1562d7b241e6Sjeremylt }
1563d7b241e6Sjeremylt 
1564b11c1e72Sjeremylt /**
1565ca94c3ddSJeremy L Thompson   @brief Create a tensor-product \f$H^1\f$ Lagrange basis
1566b11c1e72Sjeremylt 
1567ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` object used to create the `CeedBasis`
1568ea61e9acSJeremy L Thompson   @param[in]  dim       Topological dimension of element
1569ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of field components (1 for scalar fields)
1570ea61e9acSJeremy L Thompson   @param[in]  P         Number of Gauss-Lobatto nodes in one dimension.
1571ca94c3ddSJeremy L Thompson                           The polynomial degree of the resulting `Q_k` element is `k = P - 1`.
1572ea61e9acSJeremy L Thompson   @param[in]  Q         Number of quadrature points in one dimension.
1573ca94c3ddSJeremy L Thompson   @param[in]  quad_mode Distribution of the `Q` quadrature points (affects order of accuracy for the quadrature)
1574ca94c3ddSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created `CeedBasis` will be stored
1575b11c1e72Sjeremylt 
1576b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1577dfdf5a53Sjeremylt 
15787a982d89SJeremy L. Thompson   @ref User
1579b11c1e72Sjeremylt **/
15802b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis) {
1581d7b241e6Sjeremylt   // Allocate
1582c8c3fa7dSJeremy L Thompson   int        ierr = CEED_ERROR_SUCCESS;
15832b730f8bSJeremy L Thompson   CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, *q_weight_1d;
15844d537eeaSYohann 
1585ca94c3ddSJeremy L Thompson   CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value");
1586ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1587ca94c3ddSJeremy L Thompson   CeedCheck(P > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1588ca94c3ddSJeremy L Thompson   CeedCheck(Q > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1589227444bfSJeremy L Thompson 
1590e15f9bd0SJeremy L Thompson   // Get Nodes and Weights
15912b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P * Q, &interp_1d));
15922b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P * Q, &grad_1d));
15932b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P, &nodes));
15942b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &q_ref_1d));
15952b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &q_weight_1d));
15962b730f8bSJeremy L Thompson   if (CeedLobattoQuadrature(P, nodes, NULL) != CEED_ERROR_SUCCESS) goto cleanup;
1597d1d35e2fSjeremylt   switch (quad_mode) {
1598d7b241e6Sjeremylt     case CEED_GAUSS:
1599d1d35e2fSjeremylt       ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d);
1600d7b241e6Sjeremylt       break;
1601d7b241e6Sjeremylt     case CEED_GAUSS_LOBATTO:
1602d1d35e2fSjeremylt       ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d);
1603d7b241e6Sjeremylt       break;
1604d7b241e6Sjeremylt   }
16052b730f8bSJeremy L Thompson   if (ierr != CEED_ERROR_SUCCESS) goto cleanup;
1606e15f9bd0SJeremy L Thompson 
1607d7b241e6Sjeremylt   // Build B, D matrix
1608d7b241e6Sjeremylt   // Fornberg, 1998
1609c8c3fa7dSJeremy L Thompson   for (CeedInt i = 0; i < Q; i++) {
1610d7b241e6Sjeremylt     c1                   = 1.0;
1611d1d35e2fSjeremylt     c3                   = nodes[0] - q_ref_1d[i];
1612d1d35e2fSjeremylt     interp_1d[i * P + 0] = 1.0;
1613c8c3fa7dSJeremy L Thompson     for (CeedInt j = 1; j < P; j++) {
1614d7b241e6Sjeremylt       c2 = 1.0;
1615d7b241e6Sjeremylt       c4 = c3;
1616d1d35e2fSjeremylt       c3 = nodes[j] - q_ref_1d[i];
1617c8c3fa7dSJeremy L Thompson       for (CeedInt k = 0; k < j; k++) {
1618d7b241e6Sjeremylt         dx = nodes[j] - nodes[k];
1619d7b241e6Sjeremylt         c2 *= dx;
1620d7b241e6Sjeremylt         if (k == j - 1) {
1621d1d35e2fSjeremylt           grad_1d[i * P + j]   = c1 * (interp_1d[i * P + k] - c4 * grad_1d[i * P + k]) / c2;
1622d1d35e2fSjeremylt           interp_1d[i * P + j] = -c1 * c4 * interp_1d[i * P + k] / c2;
1623d7b241e6Sjeremylt         }
1624d1d35e2fSjeremylt         grad_1d[i * P + k]   = (c3 * grad_1d[i * P + k] - interp_1d[i * P + k]) / dx;
1625d1d35e2fSjeremylt         interp_1d[i * P + k] = c3 * interp_1d[i * P + k] / dx;
1626d7b241e6Sjeremylt       }
1627d7b241e6Sjeremylt       c1 = c2;
1628d7b241e6Sjeremylt     }
1629d7b241e6Sjeremylt   }
16309ac7b42eSJeremy L Thompson   // Pass to CeedBasisCreateTensorH1
16312b730f8bSJeremy L Thompson   CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis));
1632e15f9bd0SJeremy L Thompson cleanup:
16332b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_1d));
16342b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_1d));
16352b730f8bSJeremy L Thompson   CeedCall(CeedFree(&nodes));
16362b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref_1d));
16372b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight_1d));
1638e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1639d7b241e6Sjeremylt }
1640d7b241e6Sjeremylt 
1641b11c1e72Sjeremylt /**
1642ca94c3ddSJeremy L Thompson   @brief Create a non tensor-product basis for \f$H^1\f$ discretizations
1643a8de75f0Sjeremylt 
1644ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` object used to create the `CeedBasis`
1645e00f3be8SJames Wright   @param[in]  topo      Topology of element, e.g. hypercube, simplex, etc
1646ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of field components (1 for scalar fields)
1647ea61e9acSJeremy L Thompson   @param[in]  num_nodes Total number of nodes
1648ea61e9acSJeremy L Thompson   @param[in]  num_qpts  Total number of quadrature points
1649ca94c3ddSJeremy L Thompson   @param[in]  interp    Row-major (`num_qpts * num_nodes`) matrix expressing the values of nodal basis functions at quadrature points
1650ca94c3ddSJeremy L Thompson   @param[in]  grad      Row-major (`dim * num_qpts * num_nodes`) matrix expressing derivatives of nodal basis functions at quadrature points
1651fda26546SJeremy L Thompson   @param[in]  q_ref     Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element
1652ca94c3ddSJeremy L Thompson   @param[in]  q_weight  Array of length `num_qpts` holding the quadrature weights on the reference element
1653ca94c3ddSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created `CeedBasis` will be stored
1654a8de75f0Sjeremylt 
1655a8de75f0Sjeremylt   @return An error code: 0 - success, otherwise - failure
1656a8de75f0Sjeremylt 
16577a982d89SJeremy L. Thompson   @ref User
1658a8de75f0Sjeremylt **/
16592b730f8bSJeremy L Thompson int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
16602b730f8bSJeremy L Thompson                       const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
1661d1d35e2fSjeremylt   CeedInt P = num_nodes, Q = num_qpts, dim = 0;
1662a8de75f0Sjeremylt 
16635fe0d4faSjeremylt   if (!ceed->BasisCreateH1) {
16645fe0d4faSjeremylt     Ceed delegate;
16656574a04fSJeremy L Thompson 
16662b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
16671ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateH1");
16682b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis));
16699bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
1670e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
16715fe0d4faSjeremylt   }
16725fe0d4faSjeremylt 
1673ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1674ca94c3ddSJeremy L Thompson   CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1675ca94c3ddSJeremy L Thompson   CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1676227444bfSJeremy L Thompson 
16772b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1678a8de75f0Sjeremylt 
1679db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1680*b0f67a9cSJeremy L Thompson   CeedCall(CeedObjectCreate(ceed, CeedBasisView_Object, &(*basis)->obj));
16816402da51SJeremy L Thompson   (*basis)->is_tensor_basis = false;
1682a8de75f0Sjeremylt   (*basis)->dim             = dim;
1683d99fa3c5SJeremy L Thompson   (*basis)->topo            = topo;
1684d1d35e2fSjeremylt   (*basis)->num_comp        = num_comp;
1685a8de75f0Sjeremylt   (*basis)->P               = P;
1686a8de75f0Sjeremylt   (*basis)->Q               = Q;
1687c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_H1;
16882b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q * dim, &(*basis)->q_ref_1d));
16892b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &(*basis)->q_weight_1d));
1690ff3a0f91SJeremy L Thompson   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
1691ff3a0f91SJeremy L Thompson   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
16922b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q * P, &(*basis)->interp));
16932b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(dim * Q * P, &(*basis)->grad));
1694ff3a0f91SJeremy L Thompson   if (interp) memcpy((*basis)->interp, interp, Q * P * sizeof(interp[0]));
1695ff3a0f91SJeremy L Thompson   if (grad) memcpy((*basis)->grad, grad, dim * Q * P * sizeof(grad[0]));
16962b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, *basis));
1697e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1698a8de75f0Sjeremylt }
1699a8de75f0Sjeremylt 
1700a8de75f0Sjeremylt /**
1701859c15bbSJames Wright   @brief Create a non tensor-product basis for \f$H(\mathrm{div})\f$ discretizations
170250c301a5SRezgar Shakeri 
1703ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` object used to create the `CeedBasis`
1704ea61e9acSJeremy 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
1705ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of components (usually 1 for vectors in H(div) bases)
1706ca94c3ddSJeremy L Thompson   @param[in]  num_nodes Total number of nodes (DoFs per element)
1707ea61e9acSJeremy L Thompson   @param[in]  num_qpts  Total number of quadrature points
1708ca94c3ddSJeremy L Thompson   @param[in]  interp    Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points
1709ca94c3ddSJeremy L Thompson   @param[in]  div       Row-major (`num_qpts * num_nodes`) matrix expressing divergence of basis functions at quadrature points
1710ca94c3ddSJeremy L Thompson   @param[in]  q_ref     Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element
1711ca94c3ddSJeremy L Thompson   @param[in]  q_weight  Array of length `num_qpts` holding the quadrature weights on the reference element
1712ca94c3ddSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created `CeedBasis` will be stored
171350c301a5SRezgar Shakeri 
171450c301a5SRezgar Shakeri   @return An error code: 0 - success, otherwise - failure
171550c301a5SRezgar Shakeri 
171650c301a5SRezgar Shakeri   @ref User
171750c301a5SRezgar Shakeri **/
17182b730f8bSJeremy L Thompson int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
17192b730f8bSJeremy L Thompson                         const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
172050c301a5SRezgar Shakeri   CeedInt Q = num_qpts, P = num_nodes, dim = 0;
1721c4e3f59bSSebastian Grimberg 
172250c301a5SRezgar Shakeri   if (!ceed->BasisCreateHdiv) {
172350c301a5SRezgar Shakeri     Ceed delegate;
17246574a04fSJeremy L Thompson 
17252b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
17266574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv");
17272b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis));
17289bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
172950c301a5SRezgar Shakeri     return CEED_ERROR_SUCCESS;
173050c301a5SRezgar Shakeri   }
173150c301a5SRezgar Shakeri 
1732ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1733ca94c3ddSJeremy L Thompson   CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1734ca94c3ddSJeremy L Thompson   CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1735227444bfSJeremy L Thompson 
1736c4e3f59bSSebastian Grimberg   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1737c4e3f59bSSebastian Grimberg 
1738db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1739*b0f67a9cSJeremy L Thompson   CeedCall(CeedObjectCreate(ceed, CeedBasisView_Object, &(*basis)->obj));
17406402da51SJeremy L Thompson   (*basis)->is_tensor_basis = false;
174150c301a5SRezgar Shakeri   (*basis)->dim             = dim;
174250c301a5SRezgar Shakeri   (*basis)->topo            = topo;
174350c301a5SRezgar Shakeri   (*basis)->num_comp        = num_comp;
174450c301a5SRezgar Shakeri   (*basis)->P               = P;
174550c301a5SRezgar Shakeri   (*basis)->Q               = Q;
1746c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_HDIV;
17472b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d));
17482b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d));
174950c301a5SRezgar Shakeri   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
175050c301a5SRezgar Shakeri   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
17512b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp));
17522b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q * P, &(*basis)->div));
175350c301a5SRezgar Shakeri   if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0]));
175450c301a5SRezgar Shakeri   if (div) memcpy((*basis)->div, div, Q * P * sizeof(div[0]));
17552b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, q_weight, *basis));
175650c301a5SRezgar Shakeri   return CEED_ERROR_SUCCESS;
175750c301a5SRezgar Shakeri }
175850c301a5SRezgar Shakeri 
175950c301a5SRezgar Shakeri /**
17604385fb7fSSebastian Grimberg   @brief Create a non tensor-product basis for \f$H(\mathrm{curl})\f$ discretizations
1761c4e3f59bSSebastian Grimberg 
1762ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` object used to create the `CeedBasis`
1763c4e3f59bSSebastian Grimberg   @param[in]  topo      Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below
1764ca94c3ddSJeremy L Thompson   @param[in]  num_comp  Number of components (usually 1 for vectors in \f$H(\mathrm{curl})\f$ bases)
1765ca94c3ddSJeremy L Thompson   @param[in]  num_nodes Total number of nodes (DoFs per element)
1766c4e3f59bSSebastian Grimberg   @param[in]  num_qpts  Total number of quadrature points
1767ca94c3ddSJeremy L Thompson   @param[in]  interp    Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points
1768ca94c3ddSJeremy 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
1769ca94c3ddSJeremy L Thompson   @param[in]  q_ref     Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element
1770ca94c3ddSJeremy L Thompson   @param[in]  q_weight  Array of length `num_qpts` holding the quadrature weights on the reference element
1771ca94c3ddSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created `CeedBasis` will be stored
1772c4e3f59bSSebastian Grimberg 
1773c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1774c4e3f59bSSebastian Grimberg 
1775c4e3f59bSSebastian Grimberg   @ref User
1776c4e3f59bSSebastian Grimberg **/
1777c4e3f59bSSebastian Grimberg int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
1778c4e3f59bSSebastian Grimberg                          const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
1779c4e3f59bSSebastian Grimberg   CeedInt Q = num_qpts, P = num_nodes, dim = 0, curl_comp = 0;
1780c4e3f59bSSebastian Grimberg 
1781d075f50bSSebastian Grimberg   if (!ceed->BasisCreateHcurl) {
1782c4e3f59bSSebastian Grimberg     Ceed delegate;
17836574a04fSJeremy L Thompson 
1784c4e3f59bSSebastian Grimberg     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
17856574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl");
1786c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis));
17879bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
1788c4e3f59bSSebastian Grimberg     return CEED_ERROR_SUCCESS;
1789c4e3f59bSSebastian Grimberg   }
1790c4e3f59bSSebastian Grimberg 
1791ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1792ca94c3ddSJeremy L Thompson   CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1793ca94c3ddSJeremy L Thompson   CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1794c4e3f59bSSebastian Grimberg 
1795c4e3f59bSSebastian Grimberg   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1796c4e3f59bSSebastian Grimberg   curl_comp = (dim < 3) ? 1 : dim;
1797c4e3f59bSSebastian Grimberg 
1798db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1799*b0f67a9cSJeremy L Thompson   CeedCall(CeedObjectCreate(ceed, CeedBasisView_Object, &(*basis)->obj));
18006402da51SJeremy L Thompson   (*basis)->is_tensor_basis = false;
1801c4e3f59bSSebastian Grimberg   (*basis)->dim             = dim;
1802c4e3f59bSSebastian Grimberg   (*basis)->topo            = topo;
1803c4e3f59bSSebastian Grimberg   (*basis)->num_comp        = num_comp;
1804c4e3f59bSSebastian Grimberg   (*basis)->P               = P;
1805c4e3f59bSSebastian Grimberg   (*basis)->Q               = Q;
1806c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_HCURL;
1807c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d));
1808c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d));
1809c4e3f59bSSebastian Grimberg   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
1810c4e3f59bSSebastian Grimberg   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
1811c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp));
1812c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(curl_comp * Q * P, &(*basis)->curl));
1813c4e3f59bSSebastian Grimberg   if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0]));
1814c4e3f59bSSebastian Grimberg   if (curl) memcpy((*basis)->curl, curl, curl_comp * Q * P * sizeof(curl[0]));
1815c4e3f59bSSebastian Grimberg   CeedCall(ceed->BasisCreateHcurl(topo, dim, P, Q, interp, curl, q_ref, q_weight, *basis));
1816c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1817c4e3f59bSSebastian Grimberg }
1818c4e3f59bSSebastian Grimberg 
1819c4e3f59bSSebastian Grimberg /**
1820ca94c3ddSJeremy L Thompson   @brief Create a `CeedBasis` for projection from the nodes of `basis_from` to the nodes of `basis_to`.
1821ba59ac12SSebastian Grimberg 
1822ca94c3ddSJeremy L Thompson   Only @ref CEED_EVAL_INTERP will be valid for the new basis, `basis_project`.
1823ca94c3ddSJeremy L Thompson   For \f$H^1\f$ spaces, @ref CEED_EVAL_GRAD will also be valid.
1824ca94c3ddSJeremy L Thompson   The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization.
1825ca94c3ddSJeremy L Thompson   The gradient (for the \f$H^1\f$ case) is given by `grad_project = interp_to^+ * grad_from`.
182615ad3917SSebastian Grimberg 
182715ad3917SSebastian Grimberg   Note: `basis_from` and `basis_to` must have compatible quadrature spaces.
182815ad3917SSebastian Grimberg 
18299fd66db6SSebastian Grimberg   Note: `basis_project` will have the same number of components as `basis_from`, regardless of the number of components that `basis_to` has.
18309fd66db6SSebastian Grimberg         If `basis_from` has 3 components and `basis_to` has 5 components, then `basis_project` will have 3 components.
1831f113e5dcSJeremy L Thompson 
1832e104ad11SJames Wright   Note: If either `basis_from` or `basis_to` are non-tensor, then `basis_project` will also be non-tensor
1833e104ad11SJames Wright 
1834ca94c3ddSJeremy L Thompson   @param[in]  basis_from    `CeedBasis` to prolong from
1835ca94c3ddSJeremy L Thompson   @param[in]  basis_to      `CeedBasis` to prolong to
1836ca94c3ddSJeremy L Thompson   @param[out] basis_project Address of the variable where the newly created `CeedBasis` will be stored
1837f113e5dcSJeremy L Thompson 
1838f113e5dcSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1839f113e5dcSJeremy L Thompson 
1840f113e5dcSJeremy L Thompson   @ref User
1841f113e5dcSJeremy L Thompson **/
18422b730f8bSJeremy L Thompson int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project) {
1843f113e5dcSJeremy L Thompson   Ceed        ceed;
1844e104ad11SJames Wright   bool        create_tensor;
18451c66c397SJeremy L Thompson   CeedInt     dim, num_comp;
1846097cc795SJames Wright   CeedScalar *interp_project, *grad_project;
18471c66c397SJeremy L Thompson 
18482b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis_to, &ceed));
1849f113e5dcSJeremy L Thompson 
1850ecc88aebSJeremy L Thompson   // Create projection matrix
18512b730f8bSJeremy L Thompson   CeedCall(CeedBasisCreateProjectionMatrices(basis_from, basis_to, &interp_project, &grad_project));
1852f113e5dcSJeremy L Thompson 
1853f113e5dcSJeremy L Thompson   // Build basis
1854e104ad11SJames Wright   {
1855e104ad11SJames Wright     bool is_tensor_to, is_tensor_from;
1856e104ad11SJames Wright 
1857e104ad11SJames Wright     CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to));
1858e104ad11SJames Wright     CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from));
1859e104ad11SJames Wright     create_tensor = is_tensor_from && is_tensor_to;
1860e104ad11SJames Wright   }
18612b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis_to, &dim));
18622b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_from, &num_comp));
1863e104ad11SJames Wright   if (create_tensor) {
1864f113e5dcSJeremy L Thompson     CeedInt P_1d_to, P_1d_from;
18651c66c397SJeremy L Thompson 
18662b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_1d_from));
18672b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_1d_to));
1868097cc795SJames Wright     CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, interp_project, grad_project, NULL, NULL, basis_project));
1869f113e5dcSJeremy L Thompson   } else {
1870de05fbb2SSebastian Grimberg     // Even if basis_to and basis_from are not H1, the resulting basis is H1 for interpolation to work
1871f113e5dcSJeremy L Thompson     CeedInt          num_nodes_to, num_nodes_from;
18721c66c397SJeremy L Thompson     CeedElemTopology topo;
18731c66c397SJeremy L Thompson 
1874e00f3be8SJames Wright     CeedCall(CeedBasisGetTopology(basis_from, &topo));
18752b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_from, &num_nodes_from));
18762b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_to, &num_nodes_to));
1877097cc795SJames Wright     CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, NULL, NULL, basis_project));
1878f113e5dcSJeremy L Thompson   }
1879f113e5dcSJeremy L Thompson 
1880f113e5dcSJeremy L Thompson   // Cleanup
18812b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_project));
18822b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_project));
18839bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
1884f113e5dcSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1885f113e5dcSJeremy L Thompson }
1886f113e5dcSJeremy L Thompson 
1887f113e5dcSJeremy L Thompson /**
1888ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedBasis`.
18899560d06aSjeremylt 
1890ca94c3ddSJeremy 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`.
1891ca94c3ddSJeremy L Thompson         This `CeedBasis` will be destroyed if `*basis_copy` is the only reference to this `CeedBasis`.
1892ea61e9acSJeremy L Thompson 
1893ca94c3ddSJeremy L Thompson   @param[in]     basis      `CeedBasis` to copy reference to
1894ea61e9acSJeremy L Thompson   @param[in,out] basis_copy Variable to store copied reference
18959560d06aSjeremylt 
18969560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
18979560d06aSjeremylt 
18989560d06aSjeremylt   @ref User
18999560d06aSjeremylt **/
19009560d06aSjeremylt int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) {
1901356036faSJeremy L Thompson   if (basis != CEED_BASIS_NONE) CeedCall(CeedBasisReference(basis));
19022b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(basis_copy));
19039560d06aSjeremylt   *basis_copy = basis;
19049560d06aSjeremylt   return CEED_ERROR_SUCCESS;
19059560d06aSjeremylt }
19069560d06aSjeremylt 
19079560d06aSjeremylt /**
19084c789ea2SJeremy L Thompson   @brief Set the number of tabs to indent for @ref CeedBasisView() output
19094c789ea2SJeremy L Thompson 
19104c789ea2SJeremy L Thompson   @param[in] basis    `CeedBasis` to set the number of view tabs
19114c789ea2SJeremy L Thompson   @param[in] num_tabs Number of view tabs to set
19124c789ea2SJeremy L Thompson 
19134c789ea2SJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
19144c789ea2SJeremy L Thompson 
19154c789ea2SJeremy L Thompson   @ref User
19164c789ea2SJeremy L Thompson **/
19174c789ea2SJeremy L Thompson int CeedBasisSetNumViewTabs(CeedBasis basis, CeedInt num_tabs) {
19184c789ea2SJeremy L Thompson   CeedCheck(num_tabs >= 0, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Number of view tabs must be non-negative");
19194c789ea2SJeremy L Thompson   basis->num_tabs = num_tabs;
19204c789ea2SJeremy L Thompson   return CEED_ERROR_SUCCESS;
19214c789ea2SJeremy L Thompson }
19224c789ea2SJeremy L Thompson 
19234c789ea2SJeremy L Thompson /**
1924690992b2SZach Atkins   @brief Get the number of tabs to indent for @ref CeedBasisView() output
1925690992b2SZach Atkins 
1926690992b2SZach Atkins   @param[in]  basis    `CeedBasis` to get the number of view tabs
1927690992b2SZach Atkins   @param[out] num_tabs Number of view tabs
1928690992b2SZach Atkins 
1929690992b2SZach Atkins   @return Error code: 0 - success, otherwise - failure
1930690992b2SZach Atkins 
1931690992b2SZach Atkins   @ref User
1932690992b2SZach Atkins **/
1933690992b2SZach Atkins int CeedBasisGetNumViewTabs(CeedBasis basis, CeedInt *num_tabs) {
1934690992b2SZach Atkins   *num_tabs = basis->num_tabs;
1935690992b2SZach Atkins   return CEED_ERROR_SUCCESS;
1936690992b2SZach Atkins }
1937690992b2SZach Atkins 
1938690992b2SZach Atkins /**
1939ca94c3ddSJeremy L Thompson   @brief View a `CeedBasis`
19407a982d89SJeremy L. Thompson 
1941ca94c3ddSJeremy L Thompson   @param[in] basis  `CeedBasis` to view
1942ca94c3ddSJeremy L Thompson   @param[in] stream Stream to view to, e.g., `stdout`
19437a982d89SJeremy L. Thompson 
19447a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
19457a982d89SJeremy L. Thompson 
19467a982d89SJeremy L. Thompson   @ref User
19477a982d89SJeremy L. Thompson **/
19487a982d89SJeremy L. Thompson int CeedBasisView(CeedBasis basis, FILE *stream) {
19491203703bSJeremy L Thompson   bool             is_tensor_basis;
19504c789ea2SJeremy L Thompson   char            *tabs = NULL;
19511203703bSJeremy L Thompson   CeedElemTopology topo;
19521203703bSJeremy L Thompson   CeedFESpace      fe_space;
19531203703bSJeremy L Thompson 
19541203703bSJeremy L Thompson   // Basis data
19551203703bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
19561203703bSJeremy L Thompson   CeedCall(CeedBasisGetTopology(basis, &topo));
19571203703bSJeremy L Thompson   CeedCall(CeedBasisGetFESpace(basis, &fe_space));
19582b730f8bSJeremy L Thompson 
19594c789ea2SJeremy L Thompson   {
19604c789ea2SJeremy L Thompson     CeedInt num_tabs = 0;
19614c789ea2SJeremy L Thompson 
19624c789ea2SJeremy L Thompson     CeedCall(CeedBasisGetNumViewTabs(basis, &num_tabs));
19634c789ea2SJeremy L Thompson     CeedCall(CeedCalloc(CEED_TAB_WIDTH * num_tabs + 1, &tabs));
19644c789ea2SJeremy L Thompson     for (CeedInt i = 0; i < CEED_TAB_WIDTH * num_tabs; i++) tabs[i] = ' ';
196550c301a5SRezgar Shakeri   }
19664c789ea2SJeremy L Thompson 
19674c789ea2SJeremy L Thompson   // Print FE space and element topology of the basis
19684c789ea2SJeremy L Thompson   fprintf(stream, "%sCeedBasis in a %s on a %s element\n", tabs, CeedFESpaces[fe_space], CeedElemTopologies[topo]);
19694c789ea2SJeremy L Thompson   if (is_tensor_basis) {
19704c789ea2SJeremy L Thompson     fprintf(stream, "%s  P: %" CeedInt_FMT "\n%s  Q: %" CeedInt_FMT "\n", tabs, basis->P_1d, tabs, basis->Q_1d);
19714c789ea2SJeremy L Thompson   } else {
19724c789ea2SJeremy L Thompson     fprintf(stream, "%s  P: %" CeedInt_FMT "\n%s  Q: %" CeedInt_FMT "\n", tabs, basis->P, tabs, basis->Q);
19734c789ea2SJeremy L Thompson   }
19744c789ea2SJeremy L Thompson   fprintf(stream, "%s  dimension: %" CeedInt_FMT "\n%s  field components: %" CeedInt_FMT "\n", tabs, basis->dim, tabs, basis->num_comp);
1975ea61e9acSJeremy L Thompson   // Print quadrature data, interpolation/gradient/divergence/curl of the basis
19761203703bSJeremy L Thompson   if (is_tensor_basis) {  // tensor basis
19771203703bSJeremy L Thompson     CeedInt           P_1d, Q_1d;
19781203703bSJeremy L Thompson     const CeedScalar *q_ref_1d, *q_weight_1d, *interp_1d, *grad_1d;
19791203703bSJeremy L Thompson 
19801203703bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
19811203703bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
19821203703bSJeremy L Thompson     CeedCall(CeedBasisGetQRef(basis, &q_ref_1d));
19831203703bSJeremy L Thompson     CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d));
19841203703bSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
19851203703bSJeremy L Thompson     CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
19861203703bSJeremy L Thompson 
19874c789ea2SJeremy L Thompson     CeedCall(CeedScalarView("qref1d", "\t% 12.8f", 1, Q_1d, q_ref_1d, tabs, stream));
19884c789ea2SJeremy L Thompson     CeedCall(CeedScalarView("qweight1d", "\t% 12.8f", 1, Q_1d, q_weight_1d, tabs, stream));
19894c789ea2SJeremy L Thompson     CeedCall(CeedScalarView("interp1d", "\t% 12.8f", Q_1d, P_1d, interp_1d, tabs, stream));
19904c789ea2SJeremy L Thompson     CeedCall(CeedScalarView("grad1d", "\t% 12.8f", Q_1d, P_1d, grad_1d, tabs, stream));
199150c301a5SRezgar Shakeri   } else {  // non-tensor basis
19921203703bSJeremy L Thompson     CeedInt           P, Q, dim, q_comp;
19931203703bSJeremy L Thompson     const CeedScalar *q_ref, *q_weight, *interp, *grad, *div, *curl;
19941203703bSJeremy L Thompson 
19951203703bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis, &P));
19961203703bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(basis, &Q));
19971203703bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis, &dim));
19981203703bSJeremy L Thompson     CeedCall(CeedBasisGetQRef(basis, &q_ref));
19991203703bSJeremy L Thompson     CeedCall(CeedBasisGetQWeights(basis, &q_weight));
20001203703bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(basis, &interp));
20011203703bSJeremy L Thompson     CeedCall(CeedBasisGetGrad(basis, &grad));
20021203703bSJeremy L Thompson     CeedCall(CeedBasisGetDiv(basis, &div));
20031203703bSJeremy L Thompson     CeedCall(CeedBasisGetCurl(basis, &curl));
20041203703bSJeremy L Thompson 
20054c789ea2SJeremy L Thompson     CeedCall(CeedScalarView("qref", "\t% 12.8f", 1, Q * dim, q_ref, tabs, stream));
20064c789ea2SJeremy L Thompson     CeedCall(CeedScalarView("qweight", "\t% 12.8f", 1, Q, q_weight, tabs, stream));
2007c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp));
20084c789ea2SJeremy L Thompson     CeedCall(CeedScalarView("interp", "\t% 12.8f", q_comp * Q, P, interp, tabs, stream));
20091203703bSJeremy L Thompson     if (grad) {
2010c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp));
20114c789ea2SJeremy L Thompson       CeedCall(CeedScalarView("grad", "\t% 12.8f", q_comp * Q, P, grad, tabs, stream));
20127a982d89SJeremy L. Thompson     }
20131203703bSJeremy L Thompson     if (div) {
2014c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp));
20154c789ea2SJeremy L Thompson       CeedCall(CeedScalarView("div", "\t% 12.8f", q_comp * Q, P, div, tabs, stream));
2016c4e3f59bSSebastian Grimberg     }
20171203703bSJeremy L Thompson     if (curl) {
2018c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp));
20194c789ea2SJeremy L Thompson       CeedCall(CeedScalarView("curl", "\t% 12.8f", q_comp * Q, P, curl, tabs, stream));
202050c301a5SRezgar Shakeri     }
202150c301a5SRezgar Shakeri   }
20224c789ea2SJeremy L Thompson   CeedCall(CeedFree(&tabs));
2023e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
20247a982d89SJeremy L. Thompson }
20257a982d89SJeremy L. Thompson 
20267a982d89SJeremy L. Thompson /**
2027db2becc9SJeremy L Thompson   @brief Apply basis evaluation from nodes to quadrature points or vice versa
2028db2becc9SJeremy L Thompson 
2029db2becc9SJeremy L Thompson   @param[in]  basis     `CeedBasis` to evaluate
2030db2becc9SJeremy L Thompson   @param[in]  num_elem  The number of elements to apply the basis evaluation to;
2031db2becc9SJeremy L Thompson                           the backend will specify the ordering in @ref CeedElemRestrictionCreate()
2032db2becc9SJeremy L Thompson   @param[in]  t_mode    @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points;
2033db2becc9SJeremy L Thompson                           @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes
2034db2becc9SJeremy L Thompson   @param[in]  eval_mode @ref CEED_EVAL_NONE to use values directly,
2035db2becc9SJeremy L Thompson                           @ref CEED_EVAL_INTERP to use interpolated values,
2036db2becc9SJeremy L Thompson                           @ref CEED_EVAL_GRAD to use gradients,
2037db2becc9SJeremy L Thompson                           @ref CEED_EVAL_DIV to use divergence,
2038db2becc9SJeremy L Thompson                           @ref CEED_EVAL_CURL to use curl,
2039db2becc9SJeremy L Thompson                           @ref CEED_EVAL_WEIGHT to use quadrature weights
2040db2becc9SJeremy L Thompson   @param[in]  u         Input `CeedVector`
2041db2becc9SJeremy L Thompson   @param[out] v         Output `CeedVector`
2042db2becc9SJeremy L Thompson 
2043db2becc9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2044db2becc9SJeremy L Thompson 
2045db2becc9SJeremy L Thompson   @ref User
2046db2becc9SJeremy L Thompson **/
2047db2becc9SJeremy L Thompson int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) {
2048db2becc9SJeremy L Thompson   CeedCall(CeedBasisApplyCheckDims(basis, num_elem, t_mode, eval_mode, u, v));
2049db2becc9SJeremy L Thompson   CeedCheck(basis->Apply, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedBasisApply");
20502b730f8bSJeremy L Thompson   CeedCall(basis->Apply(basis, num_elem, t_mode, eval_mode, u, v));
2051e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
20527a982d89SJeremy L. Thompson }
20537a982d89SJeremy L. Thompson 
20547a982d89SJeremy L. Thompson /**
2055db2becc9SJeremy L Thompson   @brief Apply basis evaluation from quadrature points to nodes and sum into target vector
2056db2becc9SJeremy L Thompson 
2057db2becc9SJeremy L Thompson   @param[in]  basis     `CeedBasis` to evaluate
2058db2becc9SJeremy L Thompson   @param[in]  num_elem  The number of elements to apply the basis evaluation to;
2059db2becc9SJeremy L Thompson                           the backend will specify the ordering in @ref CeedElemRestrictionCreate()
2060db2becc9SJeremy L Thompson   @param[in]  t_mode    @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes;
2061db2becc9SJeremy L Thompson                            @ref CEED_NOTRANSPOSE is not valid for `CeedBasisApplyAdd()`
2062db2becc9SJeremy L Thompson   @param[in]  eval_mode @ref CEED_EVAL_NONE to use values directly,
2063db2becc9SJeremy L Thompson                           @ref CEED_EVAL_INTERP to use interpolated values,
2064db2becc9SJeremy L Thompson                           @ref CEED_EVAL_GRAD to use gradients,
2065db2becc9SJeremy L Thompson                           @ref CEED_EVAL_DIV to use divergence,
2066db2becc9SJeremy L Thompson                           @ref CEED_EVAL_CURL to use curl,
2067db2becc9SJeremy L Thompson                           @ref CEED_EVAL_WEIGHT to use quadrature weights
2068db2becc9SJeremy L Thompson   @param[in]  u         Input `CeedVector`
2069db2becc9SJeremy L Thompson   @param[out] v         Output `CeedVector` to sum into
2070db2becc9SJeremy L Thompson 
2071db2becc9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2072db2becc9SJeremy L Thompson 
2073db2becc9SJeremy L Thompson   @ref User
2074db2becc9SJeremy L Thompson **/
2075db2becc9SJeremy L Thompson int CeedBasisApplyAdd(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) {
2076db2becc9SJeremy L Thompson   CeedCheck(t_mode == CEED_TRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "CeedBasisApplyAdd only supports CEED_TRANSPOSE");
2077db2becc9SJeremy L Thompson   CeedCall(CeedBasisApplyCheckDims(basis, num_elem, t_mode, eval_mode, u, v));
2078db2becc9SJeremy L Thompson   CeedCheck(basis->ApplyAdd, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedBasisApplyAdd");
2079db2becc9SJeremy L Thompson   CeedCall(basis->ApplyAdd(basis, num_elem, t_mode, eval_mode, u, v));
2080db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2081db2becc9SJeremy L Thompson }
2082db2becc9SJeremy L Thompson 
2083db2becc9SJeremy L Thompson /**
2084db2becc9SJeremy L Thompson   @brief Apply basis evaluation from nodes to arbitrary points
2085db2becc9SJeremy L Thompson 
2086db2becc9SJeremy L Thompson   @param[in]  basis      `CeedBasis` to evaluate
2087db2becc9SJeremy L Thompson   @param[in]  num_elem   The number of elements to apply the basis evaluation to;
2088db2becc9SJeremy L Thompson                           the backend will specify the ordering in @ref CeedElemRestrictionCreate()
2089db2becc9SJeremy L Thompson   @param[in]  num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem`
2090db2becc9SJeremy L Thompson   @param[in]  t_mode     @ref CEED_NOTRANSPOSE to evaluate from nodes to points;
2091db2becc9SJeremy L Thompson                            @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes
2092db2becc9SJeremy L Thompson   @param[in]  eval_mode  @ref CEED_EVAL_INTERP to use interpolated values,
2093db2becc9SJeremy L Thompson                            @ref CEED_EVAL_GRAD to use gradients,
2094db2becc9SJeremy L Thompson                            @ref CEED_EVAL_WEIGHT to use quadrature weights
2095db2becc9SJeremy L Thompson   @param[in]  x_ref      `CeedVector` holding reference coordinates of each point
2096db2becc9SJeremy L Thompson   @param[in]  u          Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE
2097db2becc9SJeremy L Thompson   @param[out] v          Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP
2098db2becc9SJeremy L Thompson 
2099db2becc9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2100db2becc9SJeremy L Thompson 
2101db2becc9SJeremy L Thompson   @ref User
2102db2becc9SJeremy L Thompson **/
2103db2becc9SJeremy L Thompson int CeedBasisApplyAtPoints(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode,
2104db2becc9SJeremy L Thompson                            CeedVector x_ref, CeedVector u, CeedVector v) {
2105db2becc9SJeremy L Thompson   CeedCall(CeedBasisApplyAtPointsCheckDims(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
2106db2becc9SJeremy L Thompson   if (basis->ApplyAtPoints) {
2107db2becc9SJeremy L Thompson     CeedCall(basis->ApplyAtPoints(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
2108db2becc9SJeremy L Thompson   } else {
2109db2becc9SJeremy L Thompson     CeedCall(CeedBasisApplyAtPoints_Core(basis, false, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
2110db2becc9SJeremy L Thompson   }
2111db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2112db2becc9SJeremy L Thompson }
2113db2becc9SJeremy L Thompson 
2114db2becc9SJeremy L Thompson /**
2115db2becc9SJeremy L Thompson   @brief Apply basis evaluation from nodes to arbitrary points and sum into target vector
2116db2becc9SJeremy L Thompson 
2117db2becc9SJeremy L Thompson   @param[in]  basis      `CeedBasis` to evaluate
2118db2becc9SJeremy L Thompson   @param[in]  num_elem   The number of elements to apply the basis evaluation to;
2119db2becc9SJeremy L Thompson                           the backend will specify the ordering in @ref CeedElemRestrictionCreate()
2120db2becc9SJeremy L Thompson   @param[in]  num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem`
2121db2becc9SJeremy L Thompson   @param[in]  t_mode     @ref CEED_NOTRANSPOSE to evaluate from nodes to points;
2122db2becc9SJeremy L Thompson                            @ref CEED_NOTRANSPOSE is not valid for `CeedBasisApplyAddAtPoints()`
2123db2becc9SJeremy L Thompson   @param[in]  eval_mode  @ref CEED_EVAL_INTERP to use interpolated values,
2124db2becc9SJeremy L Thompson                            @ref CEED_EVAL_GRAD to use gradients,
2125db2becc9SJeremy L Thompson                            @ref CEED_EVAL_WEIGHT to use quadrature weights
2126db2becc9SJeremy L Thompson   @param[in]  x_ref      `CeedVector` holding reference coordinates of each point
2127db2becc9SJeremy L Thompson   @param[in]  u          Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE
2128db2becc9SJeremy L Thompson   @param[out] v          Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP
2129db2becc9SJeremy L Thompson 
2130db2becc9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2131db2becc9SJeremy L Thompson 
2132db2becc9SJeremy L Thompson   @ref User
2133db2becc9SJeremy L Thompson **/
2134db2becc9SJeremy L Thompson int CeedBasisApplyAddAtPoints(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode,
2135db2becc9SJeremy L Thompson                               CeedVector x_ref, CeedVector u, CeedVector v) {
2136db2becc9SJeremy L Thompson   CeedCheck(t_mode == CEED_TRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "CeedBasisApplyAddAtPoints only supports CEED_TRANSPOSE");
2137db2becc9SJeremy L Thompson   CeedCall(CeedBasisApplyAtPointsCheckDims(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
2138db2becc9SJeremy L Thompson   if (basis->ApplyAddAtPoints) {
2139db2becc9SJeremy L Thompson     CeedCall(basis->ApplyAddAtPoints(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
2140db2becc9SJeremy L Thompson   } else {
2141db2becc9SJeremy L Thompson     CeedCall(CeedBasisApplyAtPoints_Core(basis, true, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
2142db2becc9SJeremy L Thompson   }
2143db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2144db2becc9SJeremy L Thompson }
2145db2becc9SJeremy L Thompson 
2146db2becc9SJeremy L Thompson /**
21476e536b99SJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedBasis`
2148b7c9bbdaSJeremy L Thompson 
2149ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
2150ca94c3ddSJeremy L Thompson   @param[out] ceed  Variable to store `Ceed`
2151b7c9bbdaSJeremy L Thompson 
2152b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2153b7c9bbdaSJeremy L Thompson 
2154b7c9bbdaSJeremy L Thompson   @ref Advanced
2155b7c9bbdaSJeremy L Thompson **/
2156b7c9bbdaSJeremy L Thompson int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) {
2157*b0f67a9cSJeremy L Thompson   CeedCall(CeedObjectGetCeed((CeedObject)basis, ceed));
2158b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2159b7c9bbdaSJeremy L Thompson }
2160b7c9bbdaSJeremy L Thompson 
2161b7c9bbdaSJeremy L Thompson /**
21626e536b99SJeremy L Thompson   @brief Return the `Ceed` associated with a `CeedBasis`
21636e536b99SJeremy L Thompson 
21646e536b99SJeremy L Thompson   @param[in] basis `CeedBasis`
21656e536b99SJeremy L Thompson 
21666e536b99SJeremy L Thompson   @return `Ceed` associated with the `basis`
21676e536b99SJeremy L Thompson 
21686e536b99SJeremy L Thompson   @ref Advanced
21696e536b99SJeremy L Thompson **/
2170*b0f67a9cSJeremy L Thompson Ceed CeedBasisReturnCeed(CeedBasis basis) { return CeedObjectReturnCeed((CeedObject)basis); }
21716e536b99SJeremy L Thompson 
21726e536b99SJeremy L Thompson /**
2173ca94c3ddSJeremy L Thompson   @brief Get dimension for given `CeedBasis`
21749d007619Sjeremylt 
2175ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
21769d007619Sjeremylt   @param[out] dim   Variable to store dimension of basis
21779d007619Sjeremylt 
21789d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
21799d007619Sjeremylt 
2180b7c9bbdaSJeremy L Thompson   @ref Advanced
21819d007619Sjeremylt **/
21829d007619Sjeremylt int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) {
21839d007619Sjeremylt   *dim = basis->dim;
2184e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
21859d007619Sjeremylt }
21869d007619Sjeremylt 
21879d007619Sjeremylt /**
2188ca94c3ddSJeremy L Thompson   @brief Get topology for given `CeedBasis`
2189d99fa3c5SJeremy L Thompson 
2190ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
2191d99fa3c5SJeremy L Thompson   @param[out] topo  Variable to store topology of basis
2192d99fa3c5SJeremy L Thompson 
2193d99fa3c5SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2194d99fa3c5SJeremy L Thompson 
2195b7c9bbdaSJeremy L Thompson   @ref Advanced
2196d99fa3c5SJeremy L Thompson **/
2197d99fa3c5SJeremy L Thompson int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) {
2198d99fa3c5SJeremy L Thompson   *topo = basis->topo;
2199e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2200d99fa3c5SJeremy L Thompson }
2201d99fa3c5SJeremy L Thompson 
2202d99fa3c5SJeremy L Thompson /**
2203ca94c3ddSJeremy L Thompson   @brief Get number of components for given `CeedBasis`
22049d007619Sjeremylt 
2205ca94c3ddSJeremy L Thompson   @param[in]  basis    `CeedBasis`
2206ca94c3ddSJeremy L Thompson   @param[out] num_comp Variable to store number of components
22079d007619Sjeremylt 
22089d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
22099d007619Sjeremylt 
2210b7c9bbdaSJeremy L Thompson   @ref Advanced
22119d007619Sjeremylt **/
2212d1d35e2fSjeremylt int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) {
2213d1d35e2fSjeremylt   *num_comp = basis->num_comp;
2214e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
22159d007619Sjeremylt }
22169d007619Sjeremylt 
22179d007619Sjeremylt /**
2218ca94c3ddSJeremy L Thompson   @brief Get total number of nodes (in `dim` dimensions) of a `CeedBasis`
22199d007619Sjeremylt 
2220ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
22219d007619Sjeremylt   @param[out] P     Variable to store number of nodes
22229d007619Sjeremylt 
22239d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
22249d007619Sjeremylt 
22259d007619Sjeremylt   @ref Utility
22269d007619Sjeremylt **/
22279d007619Sjeremylt int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) {
22289d007619Sjeremylt   *P = basis->P;
2229e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
22309d007619Sjeremylt }
22319d007619Sjeremylt 
22329d007619Sjeremylt /**
2233ca94c3ddSJeremy L Thompson   @brief Get total number of nodes (in 1 dimension) of a `CeedBasis`
22349d007619Sjeremylt 
2235ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
2236d1d35e2fSjeremylt   @param[out] P_1d  Variable to store number of nodes
22379d007619Sjeremylt 
22389d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
22399d007619Sjeremylt 
2240b7c9bbdaSJeremy L Thompson   @ref Advanced
22419d007619Sjeremylt **/
2242d1d35e2fSjeremylt int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) {
22436e536b99SJeremy L Thompson   CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply P_1d for non-tensor CeedBasis");
2244d1d35e2fSjeremylt   *P_1d = basis->P_1d;
2245e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
22469d007619Sjeremylt }
22479d007619Sjeremylt 
22489d007619Sjeremylt /**
2249ca94c3ddSJeremy L Thompson   @brief Get total number of quadrature points (in `dim` dimensions) of a `CeedBasis`
22509d007619Sjeremylt 
2251ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
22529d007619Sjeremylt   @param[out] Q     Variable to store number of quadrature points
22539d007619Sjeremylt 
22549d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
22559d007619Sjeremylt 
22569d007619Sjeremylt   @ref Utility
22579d007619Sjeremylt **/
22589d007619Sjeremylt int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) {
22599d007619Sjeremylt   *Q = basis->Q;
2260e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
22619d007619Sjeremylt }
22629d007619Sjeremylt 
22639d007619Sjeremylt /**
2264ca94c3ddSJeremy L Thompson   @brief Get total number of quadrature points (in 1 dimension) of a `CeedBasis`
22659d007619Sjeremylt 
2266ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
2267d1d35e2fSjeremylt   @param[out] Q_1d  Variable to store number of quadrature points
22689d007619Sjeremylt 
22699d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
22709d007619Sjeremylt 
2271b7c9bbdaSJeremy L Thompson   @ref Advanced
22729d007619Sjeremylt **/
2273d1d35e2fSjeremylt int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) {
22746e536b99SJeremy L Thompson   CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply Q_1d for non-tensor CeedBasis");
2275d1d35e2fSjeremylt   *Q_1d = basis->Q_1d;
2276e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
22779d007619Sjeremylt }
22789d007619Sjeremylt 
22799d007619Sjeremylt /**
2280ca94c3ddSJeremy L Thompson   @brief Get reference coordinates of quadrature points (in `dim` dimensions) of a `CeedBasis`
22819d007619Sjeremylt 
2282ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
2283d1d35e2fSjeremylt   @param[out] q_ref Variable to store reference coordinates of quadrature points
22849d007619Sjeremylt 
22859d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
22869d007619Sjeremylt 
2287b7c9bbdaSJeremy L Thompson   @ref Advanced
22889d007619Sjeremylt **/
2289d1d35e2fSjeremylt int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) {
2290d1d35e2fSjeremylt   *q_ref = basis->q_ref_1d;
2291e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
22929d007619Sjeremylt }
22939d007619Sjeremylt 
22949d007619Sjeremylt /**
2295ca94c3ddSJeremy L Thompson   @brief Get quadrature weights of quadrature points (in `dim` dimensions) of a `CeedBasis`
22969d007619Sjeremylt 
2297ca94c3ddSJeremy L Thompson   @param[in]  basis    `CeedBasis`
2298d1d35e2fSjeremylt   @param[out] q_weight Variable to store quadrature weights
22999d007619Sjeremylt 
23009d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
23019d007619Sjeremylt 
2302b7c9bbdaSJeremy L Thompson   @ref Advanced
23039d007619Sjeremylt **/
2304d1d35e2fSjeremylt int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) {
2305d1d35e2fSjeremylt   *q_weight = basis->q_weight_1d;
2306e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
23079d007619Sjeremylt }
23089d007619Sjeremylt 
23099d007619Sjeremylt /**
2310ca94c3ddSJeremy L Thompson   @brief Get interpolation matrix of a `CeedBasis`
23119d007619Sjeremylt 
2312ca94c3ddSJeremy L Thompson   @param[in]  basis  `CeedBasis`
23139d007619Sjeremylt   @param[out] interp Variable to store interpolation matrix
23149d007619Sjeremylt 
23159d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
23169d007619Sjeremylt 
2317b7c9bbdaSJeremy L Thompson   @ref Advanced
23189d007619Sjeremylt **/
23196c58de82SJeremy L Thompson int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) {
23206402da51SJeremy L Thompson   if (!basis->interp && basis->is_tensor_basis) {
23219d007619Sjeremylt     // Allocate
23222b730f8bSJeremy L Thompson     CeedCall(CeedMalloc(basis->Q * basis->P, &basis->interp));
23239d007619Sjeremylt 
23249d007619Sjeremylt     // Initialize
23252b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < basis->Q * basis->P; i++) basis->interp[i] = 1.0;
23269d007619Sjeremylt 
23279d007619Sjeremylt     // Calculate
23282b730f8bSJeremy L Thompson     for (CeedInt d = 0; d < basis->dim; d++) {
23292b730f8bSJeremy L Thompson       for (CeedInt qpt = 0; qpt < basis->Q; qpt++) {
23309d007619Sjeremylt         for (CeedInt node = 0; node < basis->P; node++) {
2331d1d35e2fSjeremylt           CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d;
2332d1d35e2fSjeremylt           CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d;
23331c66c397SJeremy L Thompson 
2334d1d35e2fSjeremylt           basis->interp[qpt * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p];
23359d007619Sjeremylt         }
23369d007619Sjeremylt       }
23372b730f8bSJeremy L Thompson     }
23382b730f8bSJeremy L Thompson   }
23399d007619Sjeremylt   *interp = basis->interp;
2340e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
23419d007619Sjeremylt }
23429d007619Sjeremylt 
23439d007619Sjeremylt /**
2344ca94c3ddSJeremy L Thompson   @brief Get 1D interpolation matrix of a tensor product `CeedBasis`
23459d007619Sjeremylt 
2346ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis`
2347d1d35e2fSjeremylt   @param[out] interp_1d Variable to store interpolation matrix
23489d007619Sjeremylt 
23499d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
23509d007619Sjeremylt 
23519d007619Sjeremylt   @ref Backend
23529d007619Sjeremylt **/
2353d1d35e2fSjeremylt int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) {
23541203703bSJeremy L Thompson   bool is_tensor_basis;
23551203703bSJeremy L Thompson 
23561203703bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
23576e536b99SJeremy L Thompson   CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis");
2358d1d35e2fSjeremylt   *interp_1d = basis->interp_1d;
2359e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
23609d007619Sjeremylt }
23619d007619Sjeremylt 
23629d007619Sjeremylt /**
2363ca94c3ddSJeremy L Thompson   @brief Get gradient matrix of a `CeedBasis`
23649d007619Sjeremylt 
2365ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
23669d007619Sjeremylt   @param[out] grad  Variable to store gradient matrix
23679d007619Sjeremylt 
23689d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
23699d007619Sjeremylt 
2370b7c9bbdaSJeremy L Thompson   @ref Advanced
23719d007619Sjeremylt **/
23726c58de82SJeremy L Thompson int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) {
23736402da51SJeremy L Thompson   if (!basis->grad && basis->is_tensor_basis) {
23749d007619Sjeremylt     // Allocate
23752b730f8bSJeremy L Thompson     CeedCall(CeedMalloc(basis->dim * basis->Q * basis->P, &basis->grad));
23769d007619Sjeremylt 
23779d007619Sjeremylt     // Initialize
23782b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < basis->dim * basis->Q * basis->P; i++) basis->grad[i] = 1.0;
23799d007619Sjeremylt 
23809d007619Sjeremylt     // Calculate
23812b730f8bSJeremy L Thompson     for (CeedInt d = 0; d < basis->dim; d++) {
23822b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < basis->dim; i++) {
23832b730f8bSJeremy L Thompson         for (CeedInt qpt = 0; qpt < basis->Q; qpt++) {
23849d007619Sjeremylt           for (CeedInt node = 0; node < basis->P; node++) {
2385d1d35e2fSjeremylt             CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d;
2386d1d35e2fSjeremylt             CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d;
23871c66c397SJeremy L Thompson 
23882b730f8bSJeremy L Thompson             if (i == d) basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->grad_1d[q * basis->P_1d + p];
23892b730f8bSJeremy L Thompson             else basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p];
23902b730f8bSJeremy L Thompson           }
23912b730f8bSJeremy L Thompson         }
23922b730f8bSJeremy L Thompson       }
23939d007619Sjeremylt     }
23949d007619Sjeremylt   }
23959d007619Sjeremylt   *grad = basis->grad;
2396e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
23979d007619Sjeremylt }
23989d007619Sjeremylt 
23999d007619Sjeremylt /**
2400ca94c3ddSJeremy L Thompson   @brief Get 1D gradient matrix of a tensor product `CeedBasis`
24019d007619Sjeremylt 
2402ca94c3ddSJeremy L Thompson   @param[in]  basis   `CeedBasis`
2403d1d35e2fSjeremylt   @param[out] grad_1d Variable to store gradient matrix
24049d007619Sjeremylt 
24059d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
24069d007619Sjeremylt 
2407b7c9bbdaSJeremy L Thompson   @ref Advanced
24089d007619Sjeremylt **/
2409d1d35e2fSjeremylt int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) {
24101203703bSJeremy L Thompson   bool is_tensor_basis;
24111203703bSJeremy L Thompson 
24121203703bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
24136e536b99SJeremy L Thompson   CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis");
2414d1d35e2fSjeremylt   *grad_1d = basis->grad_1d;
2415e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
24169d007619Sjeremylt }
24179d007619Sjeremylt 
24189d007619Sjeremylt /**
2419ca94c3ddSJeremy L Thompson   @brief Get divergence matrix of a `CeedBasis`
242050c301a5SRezgar Shakeri 
2421ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
242250c301a5SRezgar Shakeri   @param[out] div   Variable to store divergence matrix
242350c301a5SRezgar Shakeri 
242450c301a5SRezgar Shakeri   @return An error code: 0 - success, otherwise - failure
242550c301a5SRezgar Shakeri 
242650c301a5SRezgar Shakeri   @ref Advanced
242750c301a5SRezgar Shakeri **/
242850c301a5SRezgar Shakeri int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) {
242950c301a5SRezgar Shakeri   *div = basis->div;
243050c301a5SRezgar Shakeri   return CEED_ERROR_SUCCESS;
243150c301a5SRezgar Shakeri }
243250c301a5SRezgar Shakeri 
243350c301a5SRezgar Shakeri /**
2434ca94c3ddSJeremy L Thompson   @brief Get curl matrix of a `CeedBasis`
2435c4e3f59bSSebastian Grimberg 
2436ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
2437c4e3f59bSSebastian Grimberg   @param[out] curl  Variable to store curl matrix
2438c4e3f59bSSebastian Grimberg 
2439c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
2440c4e3f59bSSebastian Grimberg 
2441c4e3f59bSSebastian Grimberg   @ref Advanced
2442c4e3f59bSSebastian Grimberg **/
2443c4e3f59bSSebastian Grimberg int CeedBasisGetCurl(CeedBasis basis, const CeedScalar **curl) {
2444c4e3f59bSSebastian Grimberg   *curl = basis->curl;
2445c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
2446c4e3f59bSSebastian Grimberg }
2447c4e3f59bSSebastian Grimberg 
2448c4e3f59bSSebastian Grimberg /**
2449ca94c3ddSJeremy L Thompson   @brief Destroy a @ref CeedBasis
24507a982d89SJeremy L. Thompson 
2451ca94c3ddSJeremy L Thompson   @param[in,out] basis `CeedBasis` to destroy
24527a982d89SJeremy L. Thompson 
24537a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
24547a982d89SJeremy L. Thompson 
24557a982d89SJeremy L. Thompson   @ref User
24567a982d89SJeremy L. Thompson **/
24577a982d89SJeremy L. Thompson int CeedBasisDestroy(CeedBasis *basis) {
2458*b0f67a9cSJeremy L Thompson   if (!*basis || *basis == CEED_BASIS_NONE || CeedObjectDereference((CeedObject)*basis) > 0) {
2459ad6481ceSJeremy L Thompson     *basis = NULL;
2460ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2461ad6481ceSJeremy L Thompson   }
24622b730f8bSJeremy L Thompson   if ((*basis)->Destroy) CeedCall((*basis)->Destroy(*basis));
24639831d45aSJeremy L Thompson   CeedCall(CeedTensorContractDestroy(&(*basis)->contract));
2464c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->q_ref_1d));
2465c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->q_weight_1d));
24662b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->interp));
24672b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->interp_1d));
24682b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->grad));
24692b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->grad_1d));
2470c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->div));
2471c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->curl));
2472c8c3fa7dSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*basis)->vec_chebyshev));
2473c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisDestroy(&(*basis)->basis_chebyshev));
2474*b0f67a9cSJeremy L Thompson   CeedCall(CeedObjectDestroy(&(*basis)->obj));
24752b730f8bSJeremy L Thompson   CeedCall(CeedFree(basis));
2476e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
24777a982d89SJeremy L. Thompson }
24787a982d89SJeremy L. Thompson 
24797a982d89SJeremy L. Thompson /**
2480b11c1e72Sjeremylt   @brief Construct a Gauss-Legendre quadrature
2481b11c1e72Sjeremylt 
2482ca94c3ddSJeremy L Thompson   @param[in]  Q           Number of quadrature points (integrates polynomials of degree `2*Q-1` exactly)
2483ca94c3ddSJeremy L Thompson   @param[out] q_ref_1d    Array of length `Q` to hold the abscissa on `[-1, 1]`
2484ca94c3ddSJeremy L Thompson   @param[out] q_weight_1d Array of length `Q` to hold the weights
2485b11c1e72Sjeremylt 
2486b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2487dfdf5a53Sjeremylt 
2488dfdf5a53Sjeremylt   @ref Utility
2489b11c1e72Sjeremylt **/
24902b730f8bSJeremy L Thompson int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) {
2491d7b241e6Sjeremylt   CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0 * atan(1.0);
24921c66c397SJeremy L Thompson 
2493d1d35e2fSjeremylt   // Build q_ref_1d, q_weight_1d
249492ae7e47SJeremy L Thompson   for (CeedInt i = 0; i <= Q / 2; i++) {
2495d7b241e6Sjeremylt     // Guess
2496d7b241e6Sjeremylt     xi = cos(PI * (CeedScalar)(2 * i + 1) / ((CeedScalar)(2 * Q)));
2497d7b241e6Sjeremylt     // Pn(xi)
2498d7b241e6Sjeremylt     P0 = 1.0;
2499d7b241e6Sjeremylt     P1 = xi;
2500d7b241e6Sjeremylt     P2 = 0.0;
250192ae7e47SJeremy L Thompson     for (CeedInt j = 2; j <= Q; j++) {
2502d7b241e6Sjeremylt       P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2503d7b241e6Sjeremylt       P0 = P1;
2504d7b241e6Sjeremylt       P1 = P2;
2505d7b241e6Sjeremylt     }
2506d7b241e6Sjeremylt     // First Newton Step
2507d7b241e6Sjeremylt     dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2508d7b241e6Sjeremylt     xi  = xi - P2 / dP2;
2509d7b241e6Sjeremylt     // Newton to convergence
251092ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < 100 && fabs(P2) > 10 * CEED_EPSILON; k++) {
2511d7b241e6Sjeremylt       P0 = 1.0;
2512d7b241e6Sjeremylt       P1 = xi;
251392ae7e47SJeremy L Thompson       for (CeedInt j = 2; j <= Q; j++) {
2514d7b241e6Sjeremylt         P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2515d7b241e6Sjeremylt         P0 = P1;
2516d7b241e6Sjeremylt         P1 = P2;
2517d7b241e6Sjeremylt       }
2518d7b241e6Sjeremylt       dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2519d7b241e6Sjeremylt       xi  = xi - P2 / dP2;
2520d7b241e6Sjeremylt     }
2521d7b241e6Sjeremylt     // Save xi, wi
2522d7b241e6Sjeremylt     wi                     = 2.0 / ((1.0 - xi * xi) * dP2 * dP2);
2523d1d35e2fSjeremylt     q_weight_1d[i]         = wi;
2524d1d35e2fSjeremylt     q_weight_1d[Q - 1 - i] = wi;
2525d1d35e2fSjeremylt     q_ref_1d[i]            = -xi;
2526d1d35e2fSjeremylt     q_ref_1d[Q - 1 - i]    = xi;
2527d7b241e6Sjeremylt   }
2528e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2529d7b241e6Sjeremylt }
2530d7b241e6Sjeremylt 
2531b11c1e72Sjeremylt /**
2532b11c1e72Sjeremylt   @brief Construct a Gauss-Legendre-Lobatto quadrature
2533b11c1e72Sjeremylt 
2534ca94c3ddSJeremy L Thompson   @param[in]  Q           Number of quadrature points (integrates polynomials of degree `2*Q-3` exactly)
2535ca94c3ddSJeremy L Thompson   @param[out] q_ref_1d    Array of length `Q` to hold the abscissa on `[-1, 1]`
2536ca94c3ddSJeremy L Thompson   @param[out] q_weight_1d Array of length `Q` to hold the weights
2537b11c1e72Sjeremylt 
2538b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2539dfdf5a53Sjeremylt 
2540dfdf5a53Sjeremylt   @ref Utility
2541b11c1e72Sjeremylt **/
25422b730f8bSJeremy L Thompson int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) {
2543d7b241e6Sjeremylt   CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0 * atan(1.0);
25441c66c397SJeremy L Thompson 
2545d1d35e2fSjeremylt   // Build q_ref_1d, q_weight_1d
2546d7b241e6Sjeremylt   // Set endpoints
25476574a04fSJeremy L Thompson   CeedCheck(Q > 1, NULL, CEED_ERROR_DIMENSION, "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q);
2548d7b241e6Sjeremylt   wi = 2.0 / ((CeedScalar)(Q * (Q - 1)));
2549d1d35e2fSjeremylt   if (q_weight_1d) {
2550d1d35e2fSjeremylt     q_weight_1d[0]     = wi;
2551d1d35e2fSjeremylt     q_weight_1d[Q - 1] = wi;
2552d7b241e6Sjeremylt   }
2553d1d35e2fSjeremylt   q_ref_1d[0]     = -1.0;
2554d1d35e2fSjeremylt   q_ref_1d[Q - 1] = 1.0;
2555d7b241e6Sjeremylt   // Interior
255692ae7e47SJeremy L Thompson   for (CeedInt i = 1; i <= (Q - 1) / 2; i++) {
2557d7b241e6Sjeremylt     // Guess
2558d7b241e6Sjeremylt     xi = cos(PI * (CeedScalar)(i) / (CeedScalar)(Q - 1));
2559d7b241e6Sjeremylt     // Pn(xi)
2560d7b241e6Sjeremylt     P0 = 1.0;
2561d7b241e6Sjeremylt     P1 = xi;
2562d7b241e6Sjeremylt     P2 = 0.0;
256392ae7e47SJeremy L Thompson     for (CeedInt j = 2; j < Q; j++) {
2564d7b241e6Sjeremylt       P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2565d7b241e6Sjeremylt       P0 = P1;
2566d7b241e6Sjeremylt       P1 = P2;
2567d7b241e6Sjeremylt     }
2568d7b241e6Sjeremylt     // First Newton step
2569d7b241e6Sjeremylt     dP2  = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2570d7b241e6Sjeremylt     d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi);
2571d7b241e6Sjeremylt     xi   = xi - dP2 / d2P2;
2572d7b241e6Sjeremylt     // Newton to convergence
257392ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < 100 && fabs(dP2) > 10 * CEED_EPSILON; k++) {
2574d7b241e6Sjeremylt       P0 = 1.0;
2575d7b241e6Sjeremylt       P1 = xi;
257692ae7e47SJeremy L Thompson       for (CeedInt j = 2; j < Q; j++) {
2577d7b241e6Sjeremylt         P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2578d7b241e6Sjeremylt         P0 = P1;
2579d7b241e6Sjeremylt         P1 = P2;
2580d7b241e6Sjeremylt       }
2581d7b241e6Sjeremylt       dP2  = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2582d7b241e6Sjeremylt       d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi);
2583d7b241e6Sjeremylt       xi   = xi - dP2 / d2P2;
2584d7b241e6Sjeremylt     }
2585d7b241e6Sjeremylt     // Save xi, wi
2586d7b241e6Sjeremylt     wi = 2.0 / (((CeedScalar)(Q * (Q - 1))) * P2 * P2);
2587d1d35e2fSjeremylt     if (q_weight_1d) {
2588d1d35e2fSjeremylt       q_weight_1d[i]         = wi;
2589d1d35e2fSjeremylt       q_weight_1d[Q - 1 - i] = wi;
2590d7b241e6Sjeremylt     }
2591d1d35e2fSjeremylt     q_ref_1d[i]         = -xi;
2592d1d35e2fSjeremylt     q_ref_1d[Q - 1 - i] = xi;
2593d7b241e6Sjeremylt   }
2594e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2595d7b241e6Sjeremylt }
2596d7b241e6Sjeremylt 
2597d7b241e6Sjeremylt /// @}
2598