xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-basis.c (revision ca94c3ddc8f82b7d93a79f9e4812e99b8be840ff)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3d7b241e6Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5d7b241e6Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7d7b241e6Sjeremylt 
83d576824SJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
11d7b241e6Sjeremylt #include <math.h>
123d576824SJeremy L Thompson #include <stdbool.h>
13d7b241e6Sjeremylt #include <stdio.h>
14d7b241e6Sjeremylt #include <string.h>
15d7b241e6Sjeremylt 
167a982d89SJeremy L. Thompson /// @file
177a982d89SJeremy L. Thompson /// Implementation of CeedBasis interfaces
187a982d89SJeremy L. Thompson 
19d7b241e6Sjeremylt /// @cond DOXYGEN_SKIP
20356036faSJeremy L Thompson static struct CeedBasis_private ceed_basis_none;
21d7b241e6Sjeremylt /// @endcond
22d7b241e6Sjeremylt 
237a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser
247a982d89SJeremy L. Thompson /// @{
257a982d89SJeremy L. Thompson 
26*ca94c3ddSJeremy 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 
29*ca94c3ddSJeremy L Thompson /// This feature will be removed. Use @ref CEED_BASIS_NONE.
30356036faSJeremy L Thompson const CeedBasis CEED_BASIS_COLLOCATED = &ceed_basis_none;
317a982d89SJeremy L. Thompson 
327a982d89SJeremy L. Thompson /// @}
337a982d89SJeremy L. Thompson 
347a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
357a982d89SJeremy L. Thompson /// CeedBasis Library Internal Functions
367a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
377a982d89SJeremy L. Thompson /// @addtogroup CeedBasisDeveloper
387a982d89SJeremy L. Thompson /// @{
397a982d89SJeremy L. Thompson 
407a982d89SJeremy L. Thompson /**
413778dbaaSJeremy L Thompson   @brief Compute Chebyshev polynomial values at a point
423778dbaaSJeremy L Thompson 
433778dbaaSJeremy L Thompson   @param[in]  x           Coordinate to evaluate Chebyshev polynomials at
44*ca94c3ddSJeremy L Thompson   @param[in]  n           Number of Chebyshev polynomials to evaluate, `n >= 2`
453778dbaaSJeremy L Thompson   @param[out] chebyshev_x Array of Chebyshev polynomial values
463778dbaaSJeremy L Thompson 
473778dbaaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
483778dbaaSJeremy L Thompson 
493778dbaaSJeremy L Thompson   @ref Developer
503778dbaaSJeremy L Thompson **/
513778dbaaSJeremy L Thompson static int CeedChebyshevPolynomialsAtPoint(CeedScalar x, CeedInt n, CeedScalar *chebyshev_x) {
523778dbaaSJeremy L Thompson   chebyshev_x[0] = 1.0;
533778dbaaSJeremy L Thompson   chebyshev_x[1] = 2 * x;
543778dbaaSJeremy L Thompson   for (CeedInt i = 2; i < n; i++) chebyshev_x[i] = 2 * x * chebyshev_x[i - 1] - chebyshev_x[i - 2];
553778dbaaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
563778dbaaSJeremy L Thompson }
573778dbaaSJeremy L Thompson 
583778dbaaSJeremy L Thompson /**
593778dbaaSJeremy L Thompson   @brief Compute values of the derivative of Chebyshev polynomials at a point
603778dbaaSJeremy L Thompson 
613778dbaaSJeremy L Thompson   @param[in]  x            Coordinate to evaluate derivative of Chebyshev polynomials at
62*ca94c3ddSJeremy L Thompson   @param[in]  n            Number of Chebyshev polynomials to evaluate, `n >= 2`
636cec60aaSJed Brown   @param[out] chebyshev_dx Array of Chebyshev polynomial derivative values
643778dbaaSJeremy L Thompson 
653778dbaaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
663778dbaaSJeremy L Thompson 
673778dbaaSJeremy L Thompson   @ref Developer
683778dbaaSJeremy L Thompson **/
693778dbaaSJeremy L Thompson static int CeedChebyshevDerivativeAtPoint(CeedScalar x, CeedInt n, CeedScalar *chebyshev_dx) {
703778dbaaSJeremy L Thompson   CeedScalar chebyshev_x[3];
713778dbaaSJeremy L Thompson 
723778dbaaSJeremy L Thompson   chebyshev_x[1]  = 1.0;
733778dbaaSJeremy L Thompson   chebyshev_x[2]  = 2 * x;
743778dbaaSJeremy L Thompson   chebyshev_dx[0] = 0.0;
753778dbaaSJeremy L Thompson   chebyshev_dx[1] = 2.0;
763778dbaaSJeremy L Thompson   for (CeedInt i = 2; i < n; i++) {
773778dbaaSJeremy L Thompson     chebyshev_x[0]  = chebyshev_x[1];
783778dbaaSJeremy L Thompson     chebyshev_x[1]  = chebyshev_x[2];
793778dbaaSJeremy L Thompson     chebyshev_x[2]  = 2 * x * chebyshev_x[1] - chebyshev_x[0];
803778dbaaSJeremy L Thompson     chebyshev_dx[i] = 2 * x * chebyshev_dx[i - 1] + 2 * chebyshev_x[1] - chebyshev_dx[i - 2];
813778dbaaSJeremy L Thompson   }
823778dbaaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
833778dbaaSJeremy L Thompson }
843778dbaaSJeremy L Thompson 
853778dbaaSJeremy L Thompson /**
86*ca94c3ddSJeremy L Thompson   @brief Compute Householder reflection.
877a982d89SJeremy L. Thompson 
88*ca94c3ddSJeremy 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]`.
897a982d89SJeremy L. Thompson 
907a982d89SJeremy L. Thompson   @param[in,out] A   Matrix to apply Householder reflection to, in place
91ea61e9acSJeremy L Thompson   @param[in]     v   Householder vector
92ea61e9acSJeremy L Thompson   @param[in]     b   Scaling factor
93*ca94c3ddSJeremy L Thompson   @param[in]     m   Number of rows in `A`
94*ca94c3ddSJeremy L Thompson   @param[in]     n   Number of columns in `A`
95ea61e9acSJeremy L Thompson   @param[in]     row Row stride
96ea61e9acSJeremy L Thompson   @param[in]     col Col stride
977a982d89SJeremy L. Thompson 
987a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
997a982d89SJeremy L. Thompson 
1007a982d89SJeremy L. Thompson   @ref Developer
1017a982d89SJeremy L. Thompson **/
1022b730f8bSJeremy L Thompson static int CeedHouseholderReflect(CeedScalar *A, const CeedScalar *v, CeedScalar b, CeedInt m, CeedInt n, CeedInt row, CeedInt col) {
1037a982d89SJeremy L. Thompson   for (CeedInt j = 0; j < n; j++) {
1047a982d89SJeremy L. Thompson     CeedScalar w = A[0 * row + j * col];
1051c66c397SJeremy L Thompson 
1062b730f8bSJeremy L Thompson     for (CeedInt i = 1; i < m; i++) w += v[i] * A[i * row + j * col];
1077a982d89SJeremy L. Thompson     A[0 * row + j * col] -= b * w;
1082b730f8bSJeremy L Thompson     for (CeedInt i = 1; i < m; i++) A[i * row + j * col] -= b * w * v[i];
1097a982d89SJeremy L. Thompson   }
110e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1117a982d89SJeremy L. Thompson }
1127a982d89SJeremy L. Thompson 
1137a982d89SJeremy L. Thompson /**
1147a982d89SJeremy L. Thompson   @brief Compute Givens rotation
1157a982d89SJeremy L. Thompson 
116*ca94c3ddSJeremy 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]`.
1177a982d89SJeremy L. Thompson 
1187a982d89SJeremy L. Thompson   @param[in,out] A      Row major matrix to apply Givens rotation to, in place
119ea61e9acSJeremy L Thompson   @param[in]     c      Cosine factor
120ea61e9acSJeremy L Thompson   @param[in]     s      Sine factor
121*ca94c3ddSJeremy 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;
1224cc79fe7SJed Brown                           @ref CEED_TRANSPOSE for the opposite rotation
123ea61e9acSJeremy L Thompson   @param[in]     i      First row/column to apply rotation
124ea61e9acSJeremy L Thompson   @param[in]     k      Second row/column to apply rotation
125*ca94c3ddSJeremy L Thompson   @param[in]     m      Number of rows in `A`
126*ca94c3ddSJeremy L Thompson   @param[in]     n      Number of columns in `A`
1277a982d89SJeremy L. Thompson 
1287a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1297a982d89SJeremy L. Thompson 
1307a982d89SJeremy L. Thompson   @ref Developer
1317a982d89SJeremy L. Thompson **/
1322b730f8bSJeremy L Thompson static int CeedGivensRotation(CeedScalar *A, CeedScalar c, CeedScalar s, CeedTransposeMode t_mode, CeedInt i, CeedInt k, CeedInt m, CeedInt n) {
133d1d35e2fSjeremylt   CeedInt stride_j = 1, stride_ik = m, num_its = n;
1341c66c397SJeremy L Thompson 
135d1d35e2fSjeremylt   if (t_mode == CEED_NOTRANSPOSE) {
1362b730f8bSJeremy L Thompson     stride_j  = n;
1372b730f8bSJeremy L Thompson     stride_ik = 1;
1382b730f8bSJeremy L Thompson     num_its   = m;
1397a982d89SJeremy L. Thompson   }
1407a982d89SJeremy L. Thompson 
1417a982d89SJeremy L. Thompson   // Apply rotation
142d1d35e2fSjeremylt   for (CeedInt j = 0; j < num_its; j++) {
143d1d35e2fSjeremylt     CeedScalar tau1 = A[i * stride_ik + j * stride_j], tau2 = A[k * stride_ik + j * stride_j];
1441c66c397SJeremy L Thompson 
145d1d35e2fSjeremylt     A[i * stride_ik + j * stride_j] = c * tau1 - s * tau2;
146d1d35e2fSjeremylt     A[k * stride_ik + j * stride_j] = s * tau1 + c * tau2;
1477a982d89SJeremy L. Thompson   }
148e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1497a982d89SJeremy L. Thompson }
1507a982d89SJeremy L. Thompson 
1517a982d89SJeremy L. Thompson /**
152*ca94c3ddSJeremy L Thompson   @brief View an array stored in a `CeedBasis`
1537a982d89SJeremy L. Thompson 
1540a0da059Sjeremylt   @param[in] name   Name of array
155d1d35e2fSjeremylt   @param[in] fp_fmt Printing format
1560a0da059Sjeremylt   @param[in] m      Number of rows in array
1570a0da059Sjeremylt   @param[in] n      Number of columns in array
1580a0da059Sjeremylt   @param[in] a      Array to be viewed
159*ca94c3ddSJeremy L Thompson   @param[in] stream Stream to view to, e.g., `stdout`
1607a982d89SJeremy L. Thompson 
1617a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1627a982d89SJeremy L. Thompson 
1637a982d89SJeremy L. Thompson   @ref Developer
1647a982d89SJeremy L. Thompson **/
1652b730f8bSJeremy L Thompson static int CeedScalarView(const char *name, const char *fp_fmt, CeedInt m, CeedInt n, const CeedScalar *a, FILE *stream) {
166edf04919SJeremy L Thompson   if (m > 1) {
167edf04919SJeremy L Thompson     fprintf(stream, "  %s:\n", name);
168edf04919SJeremy L Thompson   } else {
169edf04919SJeremy L Thompson     char padded_name[12];
170edf04919SJeremy L Thompson 
171edf04919SJeremy L Thompson     snprintf(padded_name, 11, "%s:", name);
172edf04919SJeremy L Thompson     fprintf(stream, "  %-10s", padded_name);
173edf04919SJeremy L Thompson   }
17492ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < m; i++) {
175edf04919SJeremy L Thompson     if (m > 1) fprintf(stream, "    [%" CeedInt_FMT "]", i);
1762b730f8bSJeremy 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);
1777a982d89SJeremy L. Thompson     fputs("\n", stream);
1787a982d89SJeremy L. Thompson   }
179e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1807a982d89SJeremy L. Thompson }
1817a982d89SJeremy L. Thompson 
182a76a04e7SJeremy L Thompson /**
183ea61e9acSJeremy L Thompson   @brief Create the interpolation and gradient matrices for projection from the nodes of `basis_from` to the nodes of `basis_to`.
184ba59ac12SSebastian Grimberg 
18515ad3917SSebastian Grimberg   The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization.
186*ca94c3ddSJeremy 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.
18715ad3917SSebastian Grimberg 
188ba59ac12SSebastian Grimberg   Note: `basis_from` and `basis_to` must have compatible quadrature spaces.
189a76a04e7SJeremy L Thompson 
190*ca94c3ddSJeremy L Thompson   @param[in]  basis_from     `CeedBasis` to project from
191*ca94c3ddSJeremy L Thompson   @param[in]  basis_to       `CeedBasis` to project to
192*ca94c3ddSJeremy L Thompson   @param[out] interp_project Address of the variable where the newly created interpolation matrix will be stored
193*ca94c3ddSJeremy L Thompson   @param[out] grad_project   Address of the variable where the newly created gradient matrix will be stored
194a76a04e7SJeremy L Thompson 
195a76a04e7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
196a76a04e7SJeremy L Thompson 
197a76a04e7SJeremy L Thompson   @ref Developer
198a76a04e7SJeremy L Thompson **/
1992b730f8bSJeremy L Thompson static int CeedBasisCreateProjectionMatrices(CeedBasis basis_from, CeedBasis basis_to, CeedScalar **interp_project, CeedScalar **grad_project) {
200a76a04e7SJeremy L Thompson   Ceed    ceed;
2011c66c397SJeremy L Thompson   bool    is_tensor_to, is_tensor_from;
2021c66c397SJeremy L Thompson   CeedInt Q, Q_to, Q_from, P_to, P_from;
2031c66c397SJeremy L Thompson 
2042b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis_to, &ceed));
205a76a04e7SJeremy L Thompson 
206a76a04e7SJeremy L Thompson   // Check for compatible quadrature spaces
2072b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_to, &Q_to));
2082b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_from, &Q_from));
2096574a04fSJeremy L Thompson   CeedCheck(Q_to == Q_from, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2101c66c397SJeremy L Thompson   Q = Q_to;
211a76a04e7SJeremy L Thompson 
21214556e63SJeremy L Thompson   // Check for matching tensor or non-tensor
2132b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to));
2142b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from));
2156574a04fSJeremy L Thompson   CeedCheck(is_tensor_to == is_tensor_from, ceed, CEED_ERROR_MINOR, "Bases must both be tensor or non-tensor");
2166574a04fSJeremy L Thompson   if (is_tensor_to) {
2172b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_to));
2182b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_from));
2192b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints1D(basis_from, &Q));
2206574a04fSJeremy L Thompson   } else {
2212b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_to, &P_to));
2222b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_from, &P_from));
223a76a04e7SJeremy L Thompson   }
224a76a04e7SJeremy L Thompson 
22515ad3917SSebastian Grimberg   // Check for matching FE space
22615ad3917SSebastian Grimberg   CeedFESpace fe_space_to, fe_space_from;
22715ad3917SSebastian Grimberg   CeedCall(CeedBasisGetFESpace(basis_to, &fe_space_to));
22815ad3917SSebastian Grimberg   CeedCall(CeedBasisGetFESpace(basis_from, &fe_space_from));
2296574a04fSJeremy L Thompson   CeedCheck(fe_space_to == fe_space_from, ceed, CEED_ERROR_MINOR, "Bases must both be the same FE space type");
23015ad3917SSebastian Grimberg 
23114556e63SJeremy L Thompson   // Get source matrices
23215ad3917SSebastian Grimberg   CeedInt           dim, q_comp = 1;
23314556e63SJeremy L Thompson   CeedScalar       *interp_to, *interp_from, *tau;
2341c66c397SJeremy L Thompson   const CeedScalar *interp_to_source = NULL, *interp_from_source = NULL, *grad_from_source = NULL;
2351c66c397SJeremy L Thompson 
2362b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis_to, &dim));
237a76a04e7SJeremy L Thompson   if (is_tensor_to) {
2382b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis_to, &interp_to_source));
2392b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis_from, &interp_from_source));
240a76a04e7SJeremy L Thompson   } else {
24115ad3917SSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis_from, CEED_EVAL_INTERP, &q_comp));
2422b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(basis_to, &interp_to_source));
2432b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(basis_from, &interp_from_source));
24415ad3917SSebastian Grimberg   }
24515ad3917SSebastian Grimberg   CeedCall(CeedMalloc(Q * P_from * q_comp, &interp_from));
24615ad3917SSebastian Grimberg   CeedCall(CeedMalloc(Q * P_to * q_comp, &interp_to));
24715ad3917SSebastian Grimberg   CeedCall(CeedCalloc(P_to * P_from, interp_project));
24815ad3917SSebastian Grimberg   CeedCall(CeedMalloc(Q * q_comp, &tau));
24915ad3917SSebastian Grimberg 
25015ad3917SSebastian Grimberg   // `grad_project = interp_to^+ * grad_from` is computed for the H^1 space case so the
251de05fbb2SSebastian Grimberg   // projection basis will have a gradient operation (allocated even if not H^1 for the
252de05fbb2SSebastian Grimberg   // basis construction later on)
25315ad3917SSebastian Grimberg   if (fe_space_to == CEED_FE_SPACE_H1) {
25415ad3917SSebastian Grimberg     if (is_tensor_to) {
25515ad3917SSebastian Grimberg       CeedCall(CeedBasisGetGrad1D(basis_from, &grad_from_source));
25615ad3917SSebastian Grimberg     } else {
2572b730f8bSJeremy L Thompson       CeedCall(CeedBasisGetGrad(basis_from, &grad_from_source));
258a76a04e7SJeremy L Thompson     }
259de05fbb2SSebastian Grimberg   }
26015ad3917SSebastian Grimberg   CeedCall(CeedCalloc(P_to * P_from * (is_tensor_to ? 1 : dim), grad_project));
26115ad3917SSebastian Grimberg 
26215ad3917SSebastian Grimberg   // QR Factorization, interp_to = Q R
26315ad3917SSebastian Grimberg   memcpy(interp_to, interp_to_source, Q * P_to * q_comp * sizeof(interp_to_source[0]));
26415ad3917SSebastian Grimberg   CeedCall(CeedQRFactorization(ceed, interp_to, tau, Q * q_comp, P_to));
265a76a04e7SJeremy L Thompson 
26614556e63SJeremy L Thompson   // Build matrices
26715ad3917SSebastian Grimberg   CeedInt     num_matrices = 1 + (fe_space_to == CEED_FE_SPACE_H1) * (is_tensor_to ? 1 : dim);
26814556e63SJeremy L Thompson   CeedScalar *input_from[num_matrices], *output_project[num_matrices];
2691c66c397SJeremy L Thompson 
27014556e63SJeremy L Thompson   input_from[0]     = (CeedScalar *)interp_from_source;
27114556e63SJeremy L Thompson   output_project[0] = *interp_project;
27214556e63SJeremy L Thompson   for (CeedInt m = 1; m < num_matrices; m++) {
27314556e63SJeremy L Thompson     input_from[m]     = (CeedScalar *)&grad_from_source[(m - 1) * Q * P_from];
27402af4036SJeremy L Thompson     output_project[m] = &((*grad_project)[(m - 1) * P_to * P_from]);
27514556e63SJeremy L Thompson   }
27614556e63SJeremy L Thompson   for (CeedInt m = 0; m < num_matrices; m++) {
27715ad3917SSebastian Grimberg     // Apply Q^T, interp_from = Q^T interp_from
27815ad3917SSebastian Grimberg     memcpy(interp_from, input_from[m], Q * P_from * q_comp * sizeof(input_from[m][0]));
27915ad3917SSebastian Grimberg     CeedCall(CeedHouseholderApplyQ(interp_from, interp_to, tau, CEED_TRANSPOSE, Q * q_comp, P_from, P_to, P_from, 1));
280a76a04e7SJeremy L Thompson 
28115ad3917SSebastian Grimberg     // Apply Rinv, output_project = Rinv interp_from
282a76a04e7SJeremy L Thompson     for (CeedInt j = 0; j < P_from; j++) {  // Column j
2832b730f8bSJeremy L Thompson       output_project[m][j + P_from * (P_to - 1)] = interp_from[j + P_from * (P_to - 1)] / interp_to[P_to * P_to - 1];
284a76a04e7SJeremy L Thompson       for (CeedInt i = P_to - 2; i >= 0; i--) {  // Row i
28514556e63SJeremy L Thompson         output_project[m][j + P_from * i] = interp_from[j + P_from * i];
286a76a04e7SJeremy L Thompson         for (CeedInt k = i + 1; k < P_to; k++) {
2872b730f8bSJeremy L Thompson           output_project[m][j + P_from * i] -= interp_to[k + P_to * i] * output_project[m][j + P_from * k];
288a76a04e7SJeremy L Thompson         }
28914556e63SJeremy L Thompson         output_project[m][j + P_from * i] /= interp_to[i + P_to * i];
290a76a04e7SJeremy L Thompson       }
291a76a04e7SJeremy L Thompson     }
29214556e63SJeremy L Thompson   }
29314556e63SJeremy L Thompson 
29414556e63SJeremy L Thompson   // Cleanup
2952b730f8bSJeremy L Thompson   CeedCall(CeedFree(&tau));
2962b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_to));
2972b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_from));
298a76a04e7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
299a76a04e7SJeremy L Thompson }
300a76a04e7SJeremy L Thompson 
3017a982d89SJeremy L. Thompson /// @}
3027a982d89SJeremy L. Thompson 
3037a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3047a982d89SJeremy L. Thompson /// Ceed Backend API
3057a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3067a982d89SJeremy L. Thompson /// @addtogroup CeedBasisBackend
3077a982d89SJeremy L. Thompson /// @{
3087a982d89SJeremy L. Thompson 
3097a982d89SJeremy L. Thompson /**
310*ca94c3ddSJeremy L Thompson   @brief Return collocated gradient matrix
3117a982d89SJeremy L. Thompson 
312*ca94c3ddSJeremy L Thompson   @param[in]  basis         `CeedBasis`
313*ca94c3ddSJeremy L Thompson   @param[out] collo_grad_1d Row-major (`Q_1d * Q_1d`) matrix expressing derivatives of basis functions at quadrature points
3147a982d89SJeremy L. Thompson 
3157a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3167a982d89SJeremy L. Thompson 
3177a982d89SJeremy L. Thompson   @ref Backend
3187a982d89SJeremy L. Thompson **/
319d1d35e2fSjeremylt int CeedBasisGetCollocatedGrad(CeedBasis basis, CeedScalar *collo_grad_1d) {
3207a982d89SJeremy L. Thompson   Ceed        ceed;
3212b730f8bSJeremy L Thompson   CeedInt     P_1d = (basis)->P_1d, Q_1d = (basis)->Q_1d;
32278464608Sjeremylt   CeedScalar *interp_1d, *grad_1d, *tau;
3237a982d89SJeremy L. Thompson 
3242b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q_1d * P_1d, &interp_1d));
3252b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q_1d * P_1d, &grad_1d));
3262b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q_1d, &tau));
327d1d35e2fSjeremylt   memcpy(interp_1d, (basis)->interp_1d, Q_1d * P_1d * sizeof(basis)->interp_1d[0]);
328d1d35e2fSjeremylt   memcpy(grad_1d, (basis)->grad_1d, Q_1d * P_1d * sizeof(basis)->interp_1d[0]);
3297a982d89SJeremy L. Thompson 
330d1d35e2fSjeremylt   // QR Factorization, interp_1d = Q R
3312b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis, &ceed));
3322b730f8bSJeremy L Thompson   CeedCall(CeedQRFactorization(ceed, interp_1d, tau, Q_1d, P_1d));
333ea61e9acSJeremy 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.
3347a982d89SJeremy L. Thompson 
335c8c3fa7dSJeremy L Thompson   // Apply R_inv, collo_grad_1d = grad_1d R_inv
336c8c3fa7dSJeremy L Thompson   for (CeedInt i = 0; i < Q_1d; i++) {  // Row i
337d1d35e2fSjeremylt     collo_grad_1d[Q_1d * i] = grad_1d[P_1d * i] / interp_1d[0];
338c8c3fa7dSJeremy L Thompson     for (CeedInt j = 1; j < P_1d; j++) {  // Column j
339d1d35e2fSjeremylt       collo_grad_1d[j + Q_1d * i] = grad_1d[j + P_1d * i];
340c8c3fa7dSJeremy L Thompson       for (CeedInt k = 0; k < j; k++) collo_grad_1d[j + Q_1d * i] -= interp_1d[j + P_1d * k] * collo_grad_1d[k + Q_1d * i];
341d1d35e2fSjeremylt       collo_grad_1d[j + Q_1d * i] /= interp_1d[j + P_1d * j];
3427a982d89SJeremy L. Thompson     }
343c8c3fa7dSJeremy L Thompson     for (CeedInt j = P_1d; j < Q_1d; j++) collo_grad_1d[j + Q_1d * i] = 0;
3447a982d89SJeremy L. Thompson   }
3457a982d89SJeremy L. Thompson 
34615ad3917SSebastian Grimberg   // Apply Q^T, collo_grad_1d = collo_grad_1d Q^T
3472b730f8bSJeremy L Thompson   CeedCall(CeedHouseholderApplyQ(collo_grad_1d, interp_1d, tau, CEED_NOTRANSPOSE, Q_1d, Q_1d, P_1d, 1, Q_1d));
3487a982d89SJeremy L. Thompson 
3492b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_1d));
3502b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_1d));
3512b730f8bSJeremy L Thompson   CeedCall(CeedFree(&tau));
352e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3537a982d89SJeremy L. Thompson }
3547a982d89SJeremy L. Thompson 
3557a982d89SJeremy L. Thompson /**
356*ca94c3ddSJeremy L Thompson   @brief Get tensor status for given `CeedBasis`
3577a982d89SJeremy L. Thompson 
358*ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis`
359d1d35e2fSjeremylt   @param[out] is_tensor Variable to store tensor status
3607a982d89SJeremy L. Thompson 
3617a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3627a982d89SJeremy L. Thompson 
3637a982d89SJeremy L. Thompson   @ref Backend
3647a982d89SJeremy L. Thompson **/
365d1d35e2fSjeremylt int CeedBasisIsTensor(CeedBasis basis, bool *is_tensor) {
3666402da51SJeremy L Thompson   *is_tensor = basis->is_tensor_basis;
367e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3687a982d89SJeremy L. Thompson }
3697a982d89SJeremy L. Thompson 
3707a982d89SJeremy L. Thompson /**
371*ca94c3ddSJeremy L Thompson   @brief Get backend data of a `CeedBasis`
3727a982d89SJeremy L. Thompson 
373*ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
3747a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
3757a982d89SJeremy L. Thompson 
3767a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3777a982d89SJeremy L. Thompson 
3787a982d89SJeremy L. Thompson   @ref Backend
3797a982d89SJeremy L. Thompson **/
380777ff853SJeremy L Thompson int CeedBasisGetData(CeedBasis basis, void *data) {
381777ff853SJeremy L Thompson   *(void **)data = basis->data;
382e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3837a982d89SJeremy L. Thompson }
3847a982d89SJeremy L. Thompson 
3857a982d89SJeremy L. Thompson /**
386*ca94c3ddSJeremy L Thompson   @brief Set backend data of a `CeedBasis`
3877a982d89SJeremy L. Thompson 
388*ca94c3ddSJeremy L Thompson   @param[in,out] basis  `CeedBasis`
389ea61e9acSJeremy L Thompson   @param[in]     data   Data to set
3907a982d89SJeremy L. Thompson 
3917a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3927a982d89SJeremy L. Thompson 
3937a982d89SJeremy L. Thompson   @ref Backend
3947a982d89SJeremy L. Thompson **/
395777ff853SJeremy L Thompson int CeedBasisSetData(CeedBasis basis, void *data) {
396777ff853SJeremy L Thompson   basis->data = data;
397e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3987a982d89SJeremy L. Thompson }
3997a982d89SJeremy L. Thompson 
4007a982d89SJeremy L. Thompson /**
401*ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedBasis`
40234359f16Sjeremylt 
403*ca94c3ddSJeremy L Thompson   @param[in,out] basis `CeedBasis` to increment the reference counter
40434359f16Sjeremylt 
40534359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
40634359f16Sjeremylt 
40734359f16Sjeremylt   @ref Backend
40834359f16Sjeremylt **/
4099560d06aSjeremylt int CeedBasisReference(CeedBasis basis) {
41034359f16Sjeremylt   basis->ref_count++;
41134359f16Sjeremylt   return CEED_ERROR_SUCCESS;
41234359f16Sjeremylt }
41334359f16Sjeremylt 
41434359f16Sjeremylt /**
415*ca94c3ddSJeremy L Thompson   @brief Get number of Q-vector components for given `CeedBasis`
416c4e3f59bSSebastian Grimberg 
417*ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis`
418*ca94c3ddSJeremy L Thompson   @param[in]  eval_mode @ref CEED_EVAL_INTERP to use interpolated values,
419*ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_GRAD to use gradients,
420*ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_DIV to use divergence,
421*ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_CURL to use curl
422c4e3f59bSSebastian Grimberg   @param[out] q_comp    Variable to store number of Q-vector components of basis
423c4e3f59bSSebastian Grimberg 
424c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
425c4e3f59bSSebastian Grimberg 
426c4e3f59bSSebastian Grimberg   @ref Backend
427c4e3f59bSSebastian Grimberg **/
428c4e3f59bSSebastian Grimberg int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode, CeedInt *q_comp) {
429c4e3f59bSSebastian Grimberg   switch (eval_mode) {
430c4e3f59bSSebastian Grimberg     case CEED_EVAL_INTERP:
431c4e3f59bSSebastian Grimberg       *q_comp = (basis->fe_space == CEED_FE_SPACE_H1) ? 1 : basis->dim;
432c4e3f59bSSebastian Grimberg       break;
433c4e3f59bSSebastian Grimberg     case CEED_EVAL_GRAD:
434c4e3f59bSSebastian Grimberg       *q_comp = basis->dim;
435c4e3f59bSSebastian Grimberg       break;
436c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
437c4e3f59bSSebastian Grimberg       *q_comp = 1;
438c4e3f59bSSebastian Grimberg       break;
439c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
440c4e3f59bSSebastian Grimberg       *q_comp = (basis->dim < 3) ? 1 : basis->dim;
441c4e3f59bSSebastian Grimberg       break;
442c4e3f59bSSebastian Grimberg     case CEED_EVAL_NONE:
443c4e3f59bSSebastian Grimberg     case CEED_EVAL_WEIGHT:
444352a5e7cSSebastian Grimberg       *q_comp = 1;
445c4e3f59bSSebastian Grimberg       break;
446c4e3f59bSSebastian Grimberg   }
447c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
448c4e3f59bSSebastian Grimberg }
449c4e3f59bSSebastian Grimberg 
450c4e3f59bSSebastian Grimberg /**
451*ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs required to apply `CeedBasis` in `t_mode` and `eval_mode`
4526e15d496SJeremy L Thompson 
453*ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis` to estimate FLOPs for
454ea61e9acSJeremy L Thompson   @param[in]  t_mode    Apply basis or transpose
455*ca94c3ddSJeremy L Thompson   @param[in]  eval_mode @ref CeedEvalMode
456ea61e9acSJeremy L Thompson   @param[out] flops     Address of variable to hold FLOPs estimate
4576e15d496SJeremy L Thompson 
4586e15d496SJeremy L Thompson   @ref Backend
4596e15d496SJeremy L Thompson **/
4602b730f8bSJeremy L Thompson int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedSize *flops) {
4616e15d496SJeremy L Thompson   bool is_tensor;
4626e15d496SJeremy L Thompson 
4632b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor));
4646e15d496SJeremy L Thompson   if (is_tensor) {
4656e15d496SJeremy L Thompson     CeedInt dim, num_comp, P_1d, Q_1d;
4661c66c397SJeremy L Thompson 
4672b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis, &dim));
4682b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
4692b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
4702b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
4716e15d496SJeremy L Thompson     if (t_mode == CEED_TRANSPOSE) {
4722b730f8bSJeremy L Thompson       P_1d = Q_1d;
4732b730f8bSJeremy L Thompson       Q_1d = P_1d;
4746e15d496SJeremy L Thompson     }
4756e15d496SJeremy L Thompson     CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim - 1), post = 1;
4766e15d496SJeremy L Thompson     for (CeedInt d = 0; d < dim; d++) {
4776e15d496SJeremy L Thompson       tensor_flops += 2 * pre * P_1d * post * Q_1d;
4786e15d496SJeremy L Thompson       pre /= P_1d;
4796e15d496SJeremy L Thompson       post *= Q_1d;
4806e15d496SJeremy L Thompson     }
4816e15d496SJeremy L Thompson     switch (eval_mode) {
4822b730f8bSJeremy L Thompson       case CEED_EVAL_NONE:
4832b730f8bSJeremy L Thompson         *flops = 0;
4842b730f8bSJeremy L Thompson         break;
4852b730f8bSJeremy L Thompson       case CEED_EVAL_INTERP:
4862b730f8bSJeremy L Thompson         *flops = tensor_flops;
4872b730f8bSJeremy L Thompson         break;
4882b730f8bSJeremy L Thompson       case CEED_EVAL_GRAD:
4892b730f8bSJeremy L Thompson         *flops = tensor_flops * 2;
4902b730f8bSJeremy L Thompson         break;
4916e15d496SJeremy L Thompson       case CEED_EVAL_DIV:
4926e15d496SJeremy L Thompson       case CEED_EVAL_CURL:
4936574a04fSJeremy L Thompson         // LCOV_EXCL_START
4946574a04fSJeremy L Thompson         return CeedError(basis->ceed, CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported", CeedEvalModes[eval_mode]);
4952b730f8bSJeremy L Thompson         break;
4966e15d496SJeremy L Thompson       // LCOV_EXCL_STOP
4972b730f8bSJeremy L Thompson       case CEED_EVAL_WEIGHT:
4982b730f8bSJeremy L Thompson         *flops = dim * CeedIntPow(Q_1d, dim);
4992b730f8bSJeremy L Thompson         break;
5006e15d496SJeremy L Thompson     }
5016e15d496SJeremy L Thompson   } else {
502c4e3f59bSSebastian Grimberg     CeedInt dim, num_comp, q_comp, num_nodes, num_qpts;
5031c66c397SJeremy L Thompson 
5042b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis, &dim));
5052b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
506c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
5072b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
5082b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
5096e15d496SJeremy L Thompson     switch (eval_mode) {
5102b730f8bSJeremy L Thompson       case CEED_EVAL_NONE:
5112b730f8bSJeremy L Thompson         *flops = 0;
5122b730f8bSJeremy L Thompson         break;
5132b730f8bSJeremy L Thompson       case CEED_EVAL_INTERP:
5142b730f8bSJeremy L Thompson       case CEED_EVAL_GRAD:
5152b730f8bSJeremy L Thompson       case CEED_EVAL_DIV:
5162b730f8bSJeremy L Thompson       case CEED_EVAL_CURL:
517c4e3f59bSSebastian Grimberg         *flops = num_nodes * num_qpts * num_comp * q_comp;
5182b730f8bSJeremy L Thompson         break;
5192b730f8bSJeremy L Thompson       case CEED_EVAL_WEIGHT:
5202b730f8bSJeremy L Thompson         *flops = 0;
5212b730f8bSJeremy L Thompson         break;
5226e15d496SJeremy L Thompson     }
5236e15d496SJeremy L Thompson   }
5246e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5256e15d496SJeremy L Thompson }
5266e15d496SJeremy L Thompson 
5276e15d496SJeremy L Thompson /**
528*ca94c3ddSJeremy L Thompson   @brief Get `CeedFESpace` for a `CeedBasis`
529c4e3f59bSSebastian Grimberg 
530*ca94c3ddSJeremy L Thompson   @param[in]  basis    `CeedBasis`
531*ca94c3ddSJeremy L Thompson   @param[out] fe_space Variable to store `CeedFESpace`
532c4e3f59bSSebastian Grimberg 
533c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
534c4e3f59bSSebastian Grimberg 
535c4e3f59bSSebastian Grimberg   @ref Backend
536c4e3f59bSSebastian Grimberg **/
537c4e3f59bSSebastian Grimberg int CeedBasisGetFESpace(CeedBasis basis, CeedFESpace *fe_space) {
538c4e3f59bSSebastian Grimberg   *fe_space = basis->fe_space;
539c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
540c4e3f59bSSebastian Grimberg }
541c4e3f59bSSebastian Grimberg 
542c4e3f59bSSebastian Grimberg /**
543*ca94c3ddSJeremy L Thompson   @brief Get dimension for given `CeedElemTopology`
5447a982d89SJeremy L. Thompson 
545*ca94c3ddSJeremy L Thompson   @param[in]  topo `CeedElemTopology`
5467a982d89SJeremy L. Thompson   @param[out] dim  Variable to store dimension of topology
5477a982d89SJeremy L. Thompson 
5487a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5497a982d89SJeremy L. Thompson 
5507a982d89SJeremy L. Thompson   @ref Backend
5517a982d89SJeremy L. Thompson **/
5527a982d89SJeremy L. Thompson int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim) {
5537a982d89SJeremy L. Thompson   *dim = (CeedInt)topo >> 16;
554e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5557a982d89SJeremy L. Thompson }
5567a982d89SJeremy L. Thompson 
5577a982d89SJeremy L. Thompson /**
558*ca94c3ddSJeremy L Thompson   @brief Get `CeedTensorContract` of a `CeedBasis`
5597a982d89SJeremy L. Thompson 
560*ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis`
561*ca94c3ddSJeremy L Thompson   @param[out] contract  Variable to store `CeedTensorContract`
5627a982d89SJeremy L. Thompson 
5637a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5647a982d89SJeremy L. Thompson 
5657a982d89SJeremy L. Thompson   @ref Backend
5667a982d89SJeremy L. Thompson **/
5677a982d89SJeremy L. Thompson int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract) {
5687a982d89SJeremy L. Thompson   *contract = basis->contract;
569e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5707a982d89SJeremy L. Thompson }
5717a982d89SJeremy L. Thompson 
5727a982d89SJeremy L. Thompson /**
573*ca94c3ddSJeremy L Thompson   @brief Set `CeedTensorContract` of a `CeedBasis`
5747a982d89SJeremy L. Thompson 
575*ca94c3ddSJeremy L Thompson   @param[in,out] basis    `CeedBasis`
576*ca94c3ddSJeremy L Thompson   @param[in]     contract `CeedTensorContract` to set
5777a982d89SJeremy L. Thompson 
5787a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5797a982d89SJeremy L. Thompson 
5807a982d89SJeremy L. Thompson   @ref Backend
5817a982d89SJeremy L. Thompson **/
58234359f16Sjeremylt int CeedBasisSetTensorContract(CeedBasis basis, CeedTensorContract contract) {
58334359f16Sjeremylt   basis->contract = contract;
5842b730f8bSJeremy L Thompson   CeedCall(CeedTensorContractReference(contract));
585e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5867a982d89SJeremy L. Thompson }
5877a982d89SJeremy L. Thompson 
5887a982d89SJeremy L. Thompson /**
589*ca94c3ddSJeremy L Thompson   @brief Return a reference implementation of matrix multiplication \f$C = A B\f$.
590ba59ac12SSebastian Grimberg 
591*ca94c3ddSJeremy L Thompson   Note: This is a reference implementation for CPU `CeedScalar` pointers that is not intended for high performance.
5927a982d89SJeremy L. Thompson 
593*ca94c3ddSJeremy L Thompson   @param[in]  ceed  `Ceed` context for error handling
594*ca94c3ddSJeremy L Thompson   @param[in]  mat_A Row-major matrix `A`
595*ca94c3ddSJeremy L Thompson   @param[in]  mat_B Row-major matrix `B`
596*ca94c3ddSJeremy L Thompson   @param[out] mat_C Row-major output matrix `C`
597*ca94c3ddSJeremy L Thompson   @param[in]  m     Number of rows of `C`
598*ca94c3ddSJeremy L Thompson   @param[in]  n     Number of columns of `C`
599*ca94c3ddSJeremy L Thompson   @param[in]  kk    Number of columns of `A`/rows of `B`
6007a982d89SJeremy L. Thompson 
6017a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6027a982d89SJeremy L. Thompson 
6037a982d89SJeremy L. Thompson   @ref Utility
6047a982d89SJeremy L. Thompson **/
6052b730f8bSJeremy L Thompson int CeedMatrixMatrixMultiply(Ceed ceed, const CeedScalar *mat_A, const CeedScalar *mat_B, CeedScalar *mat_C, CeedInt m, CeedInt n, CeedInt kk) {
6062b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < m; i++) {
6077a982d89SJeremy L. Thompson     for (CeedInt j = 0; j < n; j++) {
6087a982d89SJeremy L. Thompson       CeedScalar sum = 0;
6091c66c397SJeremy L Thompson 
6102b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < kk; k++) sum += mat_A[k + i * kk] * mat_B[j + k * n];
611d1d35e2fSjeremylt       mat_C[j + i * n] = sum;
6127a982d89SJeremy L. Thompson     }
6132b730f8bSJeremy L Thompson   }
614e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6157a982d89SJeremy L. Thompson }
6167a982d89SJeremy L. Thompson 
617ba59ac12SSebastian Grimberg /**
618ba59ac12SSebastian Grimberg   @brief Return QR Factorization of a matrix
619ba59ac12SSebastian Grimberg 
620*ca94c3ddSJeremy L Thompson   @param[in]     ceed `Ceed` context for error handling
621ba59ac12SSebastian Grimberg   @param[in,out] mat  Row-major matrix to be factorized in place
622*ca94c3ddSJeremy L Thompson   @param[in,out] tau  Vector of length `m` of scaling factors
623ba59ac12SSebastian Grimberg   @param[in]     m    Number of rows
624ba59ac12SSebastian Grimberg   @param[in]     n    Number of columns
625ba59ac12SSebastian Grimberg 
626ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
627ba59ac12SSebastian Grimberg 
628ba59ac12SSebastian Grimberg   @ref Utility
629ba59ac12SSebastian Grimberg **/
630ba59ac12SSebastian Grimberg int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau, CeedInt m, CeedInt n) {
631ba59ac12SSebastian Grimberg   CeedScalar v[m];
632ba59ac12SSebastian Grimberg 
633ba59ac12SSebastian Grimberg   // Check matrix shape
6346574a04fSJeremy L Thompson   CeedCheck(n <= m, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute QR factorization with n > m");
635ba59ac12SSebastian Grimberg 
636ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
6371c66c397SJeremy L Thompson     CeedScalar sigma = 0.0;
6381c66c397SJeremy L Thompson 
639ba59ac12SSebastian Grimberg     if (i >= m - 1) {  // last row of matrix, no reflection needed
640ba59ac12SSebastian Grimberg       tau[i] = 0.;
641ba59ac12SSebastian Grimberg       break;
642ba59ac12SSebastian Grimberg     }
643ba59ac12SSebastian Grimberg     // Calculate Householder vector, magnitude
644ba59ac12SSebastian Grimberg     v[i] = mat[i + n * i];
645ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) {
646ba59ac12SSebastian Grimberg       v[j] = mat[i + n * j];
647ba59ac12SSebastian Grimberg       sigma += v[j] * v[j];
648ba59ac12SSebastian Grimberg     }
6491c66c397SJeremy L Thompson     const CeedScalar norm = sqrt(v[i] * v[i] + sigma);  // norm of v[i:m]
6501c66c397SJeremy L Thompson     const CeedScalar R_ii = -copysign(norm, v[i]);
6511c66c397SJeremy L Thompson 
652ba59ac12SSebastian Grimberg     v[i] -= R_ii;
653ba59ac12SSebastian Grimberg     // norm of v[i:m] after modification above and scaling below
654ba59ac12SSebastian Grimberg     //   norm = sqrt(v[i]*v[i] + sigma) / v[i];
655ba59ac12SSebastian Grimberg     //   tau = 2 / (norm*norm)
656ba59ac12SSebastian Grimberg     tau[i] = 2 * v[i] * v[i] / (v[i] * v[i] + sigma);
657ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) v[j] /= v[i];
658ba59ac12SSebastian Grimberg 
659ba59ac12SSebastian Grimberg     // Apply Householder reflector to lower right panel
660ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat[i * n + i + 1], &v[i], tau[i], m - i, n - i - 1, n, 1);
661ba59ac12SSebastian Grimberg     // Save v
662ba59ac12SSebastian Grimberg     mat[i + n * i] = R_ii;
663ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) mat[i + n * j] = v[j];
664ba59ac12SSebastian Grimberg   }
665ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
666ba59ac12SSebastian Grimberg }
667ba59ac12SSebastian Grimberg 
668ba59ac12SSebastian Grimberg /**
669ba59ac12SSebastian Grimberg   @brief Apply Householder Q matrix
670ba59ac12SSebastian Grimberg 
671*ca94c3ddSJeremy 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$.
672ba59ac12SSebastian Grimberg 
673ba59ac12SSebastian Grimberg   @param[in,out] mat_A  Matrix to apply Householder Q to, in place
674ba59ac12SSebastian Grimberg   @param[in]     mat_Q  Householder Q matrix
675ba59ac12SSebastian Grimberg   @param[in]     tau    Householder scaling factors
676ba59ac12SSebastian Grimberg   @param[in]     t_mode Transpose mode for application
677*ca94c3ddSJeremy L Thompson   @param[in]     m      Number of rows in `A`
678*ca94c3ddSJeremy L Thompson   @param[in]     n      Number of columns in `A`
679*ca94c3ddSJeremy L Thompson   @param[in]     k      Number of elementary reflectors in Q, `k < m`
680*ca94c3ddSJeremy L Thompson   @param[in]     row    Row stride in `A`
681*ca94c3ddSJeremy L Thompson   @param[in]     col    Col stride in `A`
682ba59ac12SSebastian Grimberg 
683ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
684ba59ac12SSebastian Grimberg 
685c4e3f59bSSebastian Grimberg   @ref Utility
686ba59ac12SSebastian Grimberg **/
687ba59ac12SSebastian Grimberg int CeedHouseholderApplyQ(CeedScalar *mat_A, const CeedScalar *mat_Q, const CeedScalar *tau, CeedTransposeMode t_mode, CeedInt m, CeedInt n,
688ba59ac12SSebastian Grimberg                           CeedInt k, CeedInt row, CeedInt col) {
689ba59ac12SSebastian Grimberg   CeedScalar *v;
6901c66c397SJeremy L Thompson 
691ba59ac12SSebastian Grimberg   CeedCall(CeedMalloc(m, &v));
692ba59ac12SSebastian Grimberg   for (CeedInt ii = 0; ii < k; ii++) {
693ba59ac12SSebastian Grimberg     CeedInt i = t_mode == CEED_TRANSPOSE ? ii : k - 1 - ii;
694ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) v[j] = mat_Q[j * k + i];
695ba59ac12SSebastian Grimberg     // Apply Householder reflector (I - tau v v^T) collo_grad_1d^T
696ba59ac12SSebastian Grimberg     CeedCall(CeedHouseholderReflect(&mat_A[i * row], &v[i], tau[i], m - i, n, row, col));
697ba59ac12SSebastian Grimberg   }
698ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&v));
699ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
700ba59ac12SSebastian Grimberg }
701ba59ac12SSebastian Grimberg 
702ba59ac12SSebastian Grimberg /**
703ba59ac12SSebastian Grimberg   @brief Return symmetric Schur decomposition of the symmetric matrix mat via symmetric QR factorization
704ba59ac12SSebastian Grimberg 
705*ca94c3ddSJeremy L Thompson   @param[in]     ceed   `Ceed` context for error handling
706ba59ac12SSebastian Grimberg   @param[in,out] mat    Row-major matrix to be factorized in place
707ba59ac12SSebastian Grimberg   @param[out]    lambda Vector of length n of eigenvalues
708ba59ac12SSebastian Grimberg   @param[in]     n      Number of rows/columns
709ba59ac12SSebastian Grimberg 
710ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
711ba59ac12SSebastian Grimberg 
712ba59ac12SSebastian Grimberg   @ref Utility
713ba59ac12SSebastian Grimberg **/
7142c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
7152c2ea1dbSJeremy L Thompson int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat, CeedScalar *lambda, CeedInt n) {
716ba59ac12SSebastian Grimberg   // Check bounds for clang-tidy
7176574a04fSJeremy L Thompson   CeedCheck(n > 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute symmetric Schur decomposition of scalars");
718ba59ac12SSebastian Grimberg 
719ba59ac12SSebastian Grimberg   CeedScalar v[n - 1], tau[n - 1], mat_T[n * n];
720ba59ac12SSebastian Grimberg 
721ba59ac12SSebastian Grimberg   // Copy mat to mat_T and set mat to I
722ba59ac12SSebastian Grimberg   memcpy(mat_T, mat, n * n * sizeof(mat[0]));
723ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
724ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < n; j++) mat[j + n * i] = (i == j) ? 1 : 0;
725ba59ac12SSebastian Grimberg   }
726ba59ac12SSebastian Grimberg 
727ba59ac12SSebastian Grimberg   // Reduce to tridiagonal
728ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n - 1; i++) {
729ba59ac12SSebastian Grimberg     // Calculate Householder vector, magnitude
730ba59ac12SSebastian Grimberg     CeedScalar sigma = 0.0;
7311c66c397SJeremy L Thompson 
732ba59ac12SSebastian Grimberg     v[i] = mat_T[i + n * (i + 1)];
733ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) {
734ba59ac12SSebastian Grimberg       v[j] = mat_T[i + n * (j + 1)];
735ba59ac12SSebastian Grimberg       sigma += v[j] * v[j];
736ba59ac12SSebastian Grimberg     }
7371c66c397SJeremy L Thompson     const CeedScalar norm = sqrt(v[i] * v[i] + sigma);  // norm of v[i:n-1]
7381c66c397SJeremy L Thompson     const CeedScalar R_ii = -copysign(norm, v[i]);
7391c66c397SJeremy L Thompson 
740ba59ac12SSebastian Grimberg     v[i] -= R_ii;
741ba59ac12SSebastian Grimberg     // norm of v[i:m] after modification above and scaling below
742ba59ac12SSebastian Grimberg     //   norm = sqrt(v[i]*v[i] + sigma) / v[i];
743ba59ac12SSebastian Grimberg     //   tau = 2 / (norm*norm)
744ba59ac12SSebastian Grimberg     tau[i] = i == n - 2 ? 2 : 2 * v[i] * v[i] / (v[i] * v[i] + sigma);
745ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) v[j] /= v[i];
746ba59ac12SSebastian Grimberg 
747ba59ac12SSebastian Grimberg     // Update sub and super diagonal
748ba59ac12SSebastian Grimberg     for (CeedInt j = i + 2; j < n; j++) {
749ba59ac12SSebastian Grimberg       mat_T[i + n * j] = 0;
750ba59ac12SSebastian Grimberg       mat_T[j + n * i] = 0;
751ba59ac12SSebastian Grimberg     }
752ba59ac12SSebastian Grimberg     // Apply symmetric Householder reflector to lower right panel
753ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1);
754ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), 1, n);
755ba59ac12SSebastian Grimberg 
756ba59ac12SSebastian Grimberg     // Save v
757ba59ac12SSebastian Grimberg     mat_T[i + n * (i + 1)] = R_ii;
758ba59ac12SSebastian Grimberg     mat_T[(i + 1) + n * i] = R_ii;
759ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) {
760ba59ac12SSebastian Grimberg       mat_T[i + n * (j + 1)] = v[j];
761ba59ac12SSebastian Grimberg     }
762ba59ac12SSebastian Grimberg   }
763ba59ac12SSebastian Grimberg   // Backwards accumulation of Q
764ba59ac12SSebastian Grimberg   for (CeedInt i = n - 2; i >= 0; i--) {
765ba59ac12SSebastian Grimberg     if (tau[i] > 0.0) {
766ba59ac12SSebastian Grimberg       v[i] = 1;
767ba59ac12SSebastian Grimberg       for (CeedInt j = i + 1; j < n - 1; j++) {
768ba59ac12SSebastian Grimberg         v[j]                   = mat_T[i + n * (j + 1)];
769ba59ac12SSebastian Grimberg         mat_T[i + n * (j + 1)] = 0;
770ba59ac12SSebastian Grimberg       }
771ba59ac12SSebastian Grimberg       CeedHouseholderReflect(&mat[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1);
772ba59ac12SSebastian Grimberg     }
773ba59ac12SSebastian Grimberg   }
774ba59ac12SSebastian Grimberg 
775ba59ac12SSebastian Grimberg   // Reduce sub and super diagonal
776ba59ac12SSebastian Grimberg   CeedInt    p = 0, q = 0, itr = 0, max_itr = n * n * n * n;
777ba59ac12SSebastian Grimberg   CeedScalar tol = CEED_EPSILON;
778ba59ac12SSebastian Grimberg 
779ba59ac12SSebastian Grimberg   while (itr < max_itr) {
780ba59ac12SSebastian Grimberg     // Update p, q, size of reduced portions of diagonal
781ba59ac12SSebastian Grimberg     p = 0;
782ba59ac12SSebastian Grimberg     q = 0;
783ba59ac12SSebastian Grimberg     for (CeedInt i = n - 2; i >= 0; i--) {
784ba59ac12SSebastian Grimberg       if (fabs(mat_T[i + n * (i + 1)]) < tol) q += 1;
785ba59ac12SSebastian Grimberg       else break;
786ba59ac12SSebastian Grimberg     }
787ba59ac12SSebastian Grimberg     for (CeedInt i = 0; i < n - q - 1; i++) {
788ba59ac12SSebastian Grimberg       if (fabs(mat_T[i + n * (i + 1)]) < tol) p += 1;
789ba59ac12SSebastian Grimberg       else break;
790ba59ac12SSebastian Grimberg     }
791ba59ac12SSebastian Grimberg     if (q == n - 1) break;  // Finished reducing
792ba59ac12SSebastian Grimberg 
793ba59ac12SSebastian Grimberg     // Reduce tridiagonal portion
794ba59ac12SSebastian Grimberg     CeedScalar t_nn = mat_T[(n - 1 - q) + n * (n - 1 - q)], t_nnm1 = mat_T[(n - 2 - q) + n * (n - 1 - q)];
795ba59ac12SSebastian Grimberg     CeedScalar d  = (mat_T[(n - 2 - q) + n * (n - 2 - q)] - t_nn) / 2;
796ba59ac12SSebastian Grimberg     CeedScalar mu = t_nn - t_nnm1 * t_nnm1 / (d + copysign(sqrt(d * d + t_nnm1 * t_nnm1), d));
797ba59ac12SSebastian Grimberg     CeedScalar x  = mat_T[p + n * p] - mu;
798ba59ac12SSebastian Grimberg     CeedScalar z  = mat_T[p + n * (p + 1)];
7991c66c397SJeremy L Thompson 
800ba59ac12SSebastian Grimberg     for (CeedInt k = p; k < n - q - 1; k++) {
801ba59ac12SSebastian Grimberg       // Compute Givens rotation
802ba59ac12SSebastian Grimberg       CeedScalar c = 1, s = 0;
8031c66c397SJeremy L Thompson 
804ba59ac12SSebastian Grimberg       if (fabs(z) > tol) {
805ba59ac12SSebastian Grimberg         if (fabs(z) > fabs(x)) {
8061c66c397SJeremy L Thompson           const CeedScalar tau = -x / z;
8071c66c397SJeremy L Thompson 
8081c66c397SJeremy L Thompson           s = 1 / sqrt(1 + tau * tau);
8091c66c397SJeremy L Thompson           c = s * tau;
810ba59ac12SSebastian Grimberg         } else {
8111c66c397SJeremy L Thompson           const CeedScalar tau = -z / x;
8121c66c397SJeremy L Thompson 
8131c66c397SJeremy L Thompson           c = 1 / sqrt(1 + tau * tau);
8141c66c397SJeremy L Thompson           s = c * tau;
815ba59ac12SSebastian Grimberg         }
816ba59ac12SSebastian Grimberg       }
817ba59ac12SSebastian Grimberg 
818ba59ac12SSebastian Grimberg       // Apply Givens rotation to T
819ba59ac12SSebastian Grimberg       CeedGivensRotation(mat_T, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n);
820ba59ac12SSebastian Grimberg       CeedGivensRotation(mat_T, c, s, CEED_TRANSPOSE, k, k + 1, n, n);
821ba59ac12SSebastian Grimberg 
822ba59ac12SSebastian Grimberg       // Apply Givens rotation to Q
823ba59ac12SSebastian Grimberg       CeedGivensRotation(mat, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n);
824ba59ac12SSebastian Grimberg 
825ba59ac12SSebastian Grimberg       // Update x, z
826ba59ac12SSebastian Grimberg       if (k < n - q - 2) {
827ba59ac12SSebastian Grimberg         x = mat_T[k + n * (k + 1)];
828ba59ac12SSebastian Grimberg         z = mat_T[k + n * (k + 2)];
829ba59ac12SSebastian Grimberg       }
830ba59ac12SSebastian Grimberg     }
831ba59ac12SSebastian Grimberg     itr++;
832ba59ac12SSebastian Grimberg   }
833ba59ac12SSebastian Grimberg 
834ba59ac12SSebastian Grimberg   // Save eigenvalues
835ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) lambda[i] = mat_T[i + n * i];
836ba59ac12SSebastian Grimberg 
837ba59ac12SSebastian Grimberg   // Check convergence
8386574a04fSJeremy L Thompson   CeedCheck(itr < max_itr || q > n, ceed, CEED_ERROR_MINOR, "Symmetric QR failed to converge");
839ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
840ba59ac12SSebastian Grimberg }
8412c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
842ba59ac12SSebastian Grimberg 
843ba59ac12SSebastian Grimberg /**
844ba59ac12SSebastian Grimberg   @brief Return Simultaneous Diagonalization of two matrices.
845ba59ac12SSebastian Grimberg 
846*ca94c3ddSJeremy L Thompson   This solves the generalized eigenvalue problem `A x = lambda B x`, where `A` and `B` are symmetric and `B` is positive definite.
847*ca94c3ddSJeremy L Thompson   We generate the matrix `X` and vector `Lambda` such that `X^T A X = Lambda` and `X^T B X = I`.
848*ca94c3ddSJeremy L Thompson   This is equivalent to the LAPACK routine 'sygv' with `TYPE = 1`.
849ba59ac12SSebastian Grimberg 
850*ca94c3ddSJeremy L Thompson   @param[in]  ceed   `Ceed` context for error handling
851ba59ac12SSebastian Grimberg   @param[in]  mat_A  Row-major matrix to be factorized with eigenvalues
852ba59ac12SSebastian Grimberg   @param[in]  mat_B  Row-major matrix to be factorized to identity
853ba59ac12SSebastian Grimberg   @param[out] mat_X  Row-major orthogonal matrix
854*ca94c3ddSJeremy L Thompson   @param[out] lambda Vector of length `n` of generalized eigenvalues
855ba59ac12SSebastian Grimberg   @param[in]  n      Number of rows/columns
856ba59ac12SSebastian Grimberg 
857ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
858ba59ac12SSebastian Grimberg 
859ba59ac12SSebastian Grimberg   @ref Utility
860ba59ac12SSebastian Grimberg **/
8612c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
8622c2ea1dbSJeremy L Thompson int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A, CeedScalar *mat_B, CeedScalar *mat_X, CeedScalar *lambda, CeedInt n) {
863ba59ac12SSebastian Grimberg   CeedScalar *mat_C, *mat_G, *vec_D;
8641c66c397SJeremy L Thompson 
865ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n * n, &mat_C));
866ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n * n, &mat_G));
867ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n, &vec_D));
868ba59ac12SSebastian Grimberg 
869ba59ac12SSebastian Grimberg   // Compute B = G D G^T
870ba59ac12SSebastian Grimberg   memcpy(mat_G, mat_B, n * n * sizeof(mat_B[0]));
871ba59ac12SSebastian Grimberg   CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_G, vec_D, n));
872ba59ac12SSebastian Grimberg 
873ba59ac12SSebastian Grimberg   // Sort eigenvalues
874ba59ac12SSebastian Grimberg   for (CeedInt i = n - 1; i >= 0; i--) {
875ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < i; j++) {
876ba59ac12SSebastian Grimberg       if (fabs(vec_D[j]) > fabs(vec_D[j + 1])) {
8771c66c397SJeremy L Thompson         CeedScalarSwap(vec_D[j], vec_D[j + 1]);
8781c66c397SJeremy L Thompson         for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_G[k * n + j], mat_G[k * n + j + 1]);
879ba59ac12SSebastian Grimberg       }
880ba59ac12SSebastian Grimberg     }
881ba59ac12SSebastian Grimberg   }
882ba59ac12SSebastian Grimberg 
883ba59ac12SSebastian Grimberg   // Compute C = (G D^1/2)^-1 A (G D^1/2)^-T
884ba59ac12SSebastian Grimberg   //           = D^-1/2 G^T A G D^-1/2
885ba59ac12SSebastian Grimberg   // -- D = D^-1/2
886ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) vec_D[i] = 1. / sqrt(vec_D[i]);
887ba59ac12SSebastian Grimberg   // -- G = G D^-1/2
888ba59ac12SSebastian Grimberg   // -- C = D^-1/2 G^T
889ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
890ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < n; j++) {
891ba59ac12SSebastian Grimberg       mat_G[i * n + j] *= vec_D[j];
892ba59ac12SSebastian Grimberg       mat_C[j * n + i] = mat_G[i * n + j];
893ba59ac12SSebastian Grimberg     }
894ba59ac12SSebastian Grimberg   }
895ba59ac12SSebastian Grimberg   // -- X = (D^-1/2 G^T) A
896ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_C, (const CeedScalar *)mat_A, mat_X, n, n, n));
897ba59ac12SSebastian Grimberg   // -- C = (D^-1/2 G^T A) (G D^-1/2)
898ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_X, (const CeedScalar *)mat_G, mat_C, n, n, n));
899ba59ac12SSebastian Grimberg 
900ba59ac12SSebastian Grimberg   // Compute Q^T C Q = lambda
901ba59ac12SSebastian Grimberg   CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_C, lambda, n));
902ba59ac12SSebastian Grimberg 
903ba59ac12SSebastian Grimberg   // Sort eigenvalues
904ba59ac12SSebastian Grimberg   for (CeedInt i = n - 1; i >= 0; i--) {
905ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < i; j++) {
906ba59ac12SSebastian Grimberg       if (fabs(lambda[j]) > fabs(lambda[j + 1])) {
9071c66c397SJeremy L Thompson         CeedScalarSwap(lambda[j], lambda[j + 1]);
9081c66c397SJeremy L Thompson         for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_C[k * n + j], mat_C[k * n + j + 1]);
909ba59ac12SSebastian Grimberg       }
910ba59ac12SSebastian Grimberg     }
911ba59ac12SSebastian Grimberg   }
912ba59ac12SSebastian Grimberg 
913ba59ac12SSebastian Grimberg   // Set X = (G D^1/2)^-T Q
914ba59ac12SSebastian Grimberg   //       = G D^-1/2 Q
915ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_G, (const CeedScalar *)mat_C, mat_X, n, n, n));
916ba59ac12SSebastian Grimberg 
917ba59ac12SSebastian Grimberg   // Cleanup
918ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&mat_C));
919ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&mat_G));
920ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&vec_D));
921ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
922ba59ac12SSebastian Grimberg }
9232c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
924ba59ac12SSebastian Grimberg 
9257a982d89SJeremy L. Thompson /// @}
9267a982d89SJeremy L. Thompson 
9277a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
9287a982d89SJeremy L. Thompson /// CeedBasis Public API
9297a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
9307a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser
931d7b241e6Sjeremylt /// @{
932d7b241e6Sjeremylt 
933b11c1e72Sjeremylt /**
934*ca94c3ddSJeremy L Thompson   @brief Create a tensor-product basis for \f$H^1\f$ discretizations
935b11c1e72Sjeremylt 
936*ca94c3ddSJeremy L Thompson   @param[in]  ceed        `Ceed` object used to create the `CeedBasis`
937ea61e9acSJeremy L Thompson   @param[in]  dim         Topological dimension
938ea61e9acSJeremy L Thompson   @param[in]  num_comp    Number of field components (1 for scalar fields)
939ea61e9acSJeremy L Thompson   @param[in]  P_1d        Number of nodes in one dimension
940ea61e9acSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
941*ca94c3ddSJeremy L Thompson   @param[in]  interp_1d   Row-major (`Q_1d * P_1d`) matrix expressing the values of nodal basis functions at quadrature points
942*ca94c3ddSJeremy L Thompson   @param[in]  grad_1d     Row-major (`Q_1d * P_1d`) matrix expressing derivatives of nodal basis functions at quadrature points
943*ca94c3ddSJeremy 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]`
944*ca94c3ddSJeremy L Thompson   @param[in]  q_weight_1d Array of length `Q_1d` holding the quadrature weights on the reference element
945*ca94c3ddSJeremy L Thompson   @param[out] basis       Address of the variable where the newly created `CeedBasis` will be stored
946b11c1e72Sjeremylt 
947b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
948dfdf5a53Sjeremylt 
9497a982d89SJeremy L. Thompson   @ref User
950b11c1e72Sjeremylt **/
9512b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d,
9522b730f8bSJeremy L Thompson                             const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis *basis) {
9535fe0d4faSjeremylt   if (!ceed->BasisCreateTensorH1) {
9545fe0d4faSjeremylt     Ceed delegate;
9556574a04fSJeremy L Thompson 
9562b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
9576574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisCreateTensorH1");
9582b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis));
959e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
9605fe0d4faSjeremylt   }
961e15f9bd0SJeremy L Thompson 
962*ca94c3ddSJeremy L Thompson   CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value");
963*ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
964*ca94c3ddSJeremy L Thompson   CeedCheck(P_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
965*ca94c3ddSJeremy L Thompson   CeedCheck(Q_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
966227444bfSJeremy L Thompson 
9672b730f8bSJeremy L Thompson   CeedElemTopology topo = dim == 1 ? CEED_TOPOLOGY_LINE : dim == 2 ? CEED_TOPOLOGY_QUAD : CEED_TOPOLOGY_HEX;
968e15f9bd0SJeremy L Thompson 
9692b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
970db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed));
971d1d35e2fSjeremylt   (*basis)->ref_count       = 1;
9726402da51SJeremy L Thompson   (*basis)->is_tensor_basis = true;
973d7b241e6Sjeremylt   (*basis)->dim             = dim;
974d99fa3c5SJeremy L Thompson   (*basis)->topo            = topo;
975d1d35e2fSjeremylt   (*basis)->num_comp        = num_comp;
976d1d35e2fSjeremylt   (*basis)->P_1d            = P_1d;
977d1d35e2fSjeremylt   (*basis)->Q_1d            = Q_1d;
978d1d35e2fSjeremylt   (*basis)->P               = CeedIntPow(P_1d, dim);
979d1d35e2fSjeremylt   (*basis)->Q               = CeedIntPow(Q_1d, dim);
980c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_H1;
9812b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d, &(*basis)->q_ref_1d));
9822b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d, &(*basis)->q_weight_1d));
983ff3a0f91SJeremy L Thompson   if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d * sizeof(q_ref_1d[0]));
9842b730f8bSJeremy L Thompson   if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, Q_1d * sizeof(q_weight_1d[0]));
9852b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->interp_1d));
9862b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->grad_1d));
9872b730f8bSJeremy L Thompson   if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, Q_1d * P_1d * sizeof(interp_1d[0]));
988ff3a0f91SJeremy L Thompson   if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d * P_1d * sizeof(grad_1d[0]));
9892b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, *basis));
990e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
991d7b241e6Sjeremylt }
992d7b241e6Sjeremylt 
993b11c1e72Sjeremylt /**
994*ca94c3ddSJeremy L Thompson   @brief Create a tensor-product \f$H^1\f$ Lagrange basis
995b11c1e72Sjeremylt 
996*ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` object used to create the `CeedBasis`
997ea61e9acSJeremy L Thompson   @param[in]  dim       Topological dimension of element
998ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of field components (1 for scalar fields)
999ea61e9acSJeremy L Thompson   @param[in]  P         Number of Gauss-Lobatto nodes in one dimension.
1000*ca94c3ddSJeremy L Thompson                           The polynomial degree of the resulting `Q_k` element is `k = P - 1`.
1001ea61e9acSJeremy L Thompson   @param[in]  Q         Number of quadrature points in one dimension.
1002*ca94c3ddSJeremy L Thompson   @param[in]  quad_mode Distribution of the `Q` quadrature points (affects order of accuracy for the quadrature)
1003*ca94c3ddSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created `CeedBasis` will be stored
1004b11c1e72Sjeremylt 
1005b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1006dfdf5a53Sjeremylt 
10077a982d89SJeremy L. Thompson   @ref User
1008b11c1e72Sjeremylt **/
10092b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis) {
1010d7b241e6Sjeremylt   // Allocate
1011c8c3fa7dSJeremy L Thompson   int        ierr = CEED_ERROR_SUCCESS;
10122b730f8bSJeremy L Thompson   CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, *q_weight_1d;
10134d537eeaSYohann 
1014*ca94c3ddSJeremy L Thompson   CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value");
1015*ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1016*ca94c3ddSJeremy L Thompson   CeedCheck(P > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1017*ca94c3ddSJeremy L Thompson   CeedCheck(Q > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1018227444bfSJeremy L Thompson 
1019e15f9bd0SJeremy L Thompson   // Get Nodes and Weights
10202b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P * Q, &interp_1d));
10212b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P * Q, &grad_1d));
10222b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P, &nodes));
10232b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &q_ref_1d));
10242b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &q_weight_1d));
10252b730f8bSJeremy L Thompson   if (CeedLobattoQuadrature(P, nodes, NULL) != CEED_ERROR_SUCCESS) goto cleanup;
1026d1d35e2fSjeremylt   switch (quad_mode) {
1027d7b241e6Sjeremylt     case CEED_GAUSS:
1028d1d35e2fSjeremylt       ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d);
1029d7b241e6Sjeremylt       break;
1030d7b241e6Sjeremylt     case CEED_GAUSS_LOBATTO:
1031d1d35e2fSjeremylt       ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d);
1032d7b241e6Sjeremylt       break;
1033d7b241e6Sjeremylt   }
10342b730f8bSJeremy L Thompson   if (ierr != CEED_ERROR_SUCCESS) goto cleanup;
1035e15f9bd0SJeremy L Thompson 
1036d7b241e6Sjeremylt   // Build B, D matrix
1037d7b241e6Sjeremylt   // Fornberg, 1998
1038c8c3fa7dSJeremy L Thompson   for (CeedInt i = 0; i < Q; i++) {
1039d7b241e6Sjeremylt     c1                   = 1.0;
1040d1d35e2fSjeremylt     c3                   = nodes[0] - q_ref_1d[i];
1041d1d35e2fSjeremylt     interp_1d[i * P + 0] = 1.0;
1042c8c3fa7dSJeremy L Thompson     for (CeedInt j = 1; j < P; j++) {
1043d7b241e6Sjeremylt       c2 = 1.0;
1044d7b241e6Sjeremylt       c4 = c3;
1045d1d35e2fSjeremylt       c3 = nodes[j] - q_ref_1d[i];
1046c8c3fa7dSJeremy L Thompson       for (CeedInt k = 0; k < j; k++) {
1047d7b241e6Sjeremylt         dx = nodes[j] - nodes[k];
1048d7b241e6Sjeremylt         c2 *= dx;
1049d7b241e6Sjeremylt         if (k == j - 1) {
1050d1d35e2fSjeremylt           grad_1d[i * P + j]   = c1 * (interp_1d[i * P + k] - c4 * grad_1d[i * P + k]) / c2;
1051d1d35e2fSjeremylt           interp_1d[i * P + j] = -c1 * c4 * interp_1d[i * P + k] / c2;
1052d7b241e6Sjeremylt         }
1053d1d35e2fSjeremylt         grad_1d[i * P + k]   = (c3 * grad_1d[i * P + k] - interp_1d[i * P + k]) / dx;
1054d1d35e2fSjeremylt         interp_1d[i * P + k] = c3 * interp_1d[i * P + k] / dx;
1055d7b241e6Sjeremylt       }
1056d7b241e6Sjeremylt       c1 = c2;
1057d7b241e6Sjeremylt     }
1058d7b241e6Sjeremylt   }
10599ac7b42eSJeremy L Thompson   // Pass to CeedBasisCreateTensorH1
10602b730f8bSJeremy L Thompson   CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis));
1061e15f9bd0SJeremy L Thompson cleanup:
10622b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_1d));
10632b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_1d));
10642b730f8bSJeremy L Thompson   CeedCall(CeedFree(&nodes));
10652b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref_1d));
10662b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight_1d));
1067e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1068d7b241e6Sjeremylt }
1069d7b241e6Sjeremylt 
1070b11c1e72Sjeremylt /**
1071*ca94c3ddSJeremy L Thompson   @brief Create a non tensor-product basis for \f$H^1\f$ discretizations
1072a8de75f0Sjeremylt 
1073*ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` object used to create the `CeedBasis`
1074ea61e9acSJeremy L Thompson   @param[in]  topo      Topology of element, e.g. hypercube, simplex, ect
1075ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of field components (1 for scalar fields)
1076ea61e9acSJeremy L Thompson   @param[in]  num_nodes Total number of nodes
1077ea61e9acSJeremy L Thompson   @param[in]  num_qpts  Total number of quadrature points
1078*ca94c3ddSJeremy L Thompson   @param[in]  interp    Row-major (`num_qpts * num_nodes`) matrix expressing the values of nodal basis functions at quadrature points
1079*ca94c3ddSJeremy L Thompson   @param[in]  grad      Row-major (`dim * num_qpts * num_nodes`) matrix expressing derivatives of nodal basis functions at quadrature points
1080*ca94c3ddSJeremy L Thompson   @param[in]  q_ref     Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element
1081*ca94c3ddSJeremy L Thompson   @param[in]  q_weight  Array of length `num_qpts` holding the quadrature weights on the reference element
1082*ca94c3ddSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created `CeedBasis` will be stored
1083a8de75f0Sjeremylt 
1084a8de75f0Sjeremylt   @return An error code: 0 - success, otherwise - failure
1085a8de75f0Sjeremylt 
10867a982d89SJeremy L. Thompson   @ref User
1087a8de75f0Sjeremylt **/
10882b730f8bSJeremy L Thompson int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
10892b730f8bSJeremy L Thompson                       const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
1090d1d35e2fSjeremylt   CeedInt P = num_nodes, Q = num_qpts, dim = 0;
1091a8de75f0Sjeremylt 
10925fe0d4faSjeremylt   if (!ceed->BasisCreateH1) {
10935fe0d4faSjeremylt     Ceed delegate;
10946574a04fSJeremy L Thompson 
10952b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
10966574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisCreateH1");
10972b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis));
1098e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
10995fe0d4faSjeremylt   }
11005fe0d4faSjeremylt 
1101*ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1102*ca94c3ddSJeremy L Thompson   CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1103*ca94c3ddSJeremy L Thompson   CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1104227444bfSJeremy L Thompson 
11052b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1106a8de75f0Sjeremylt 
1107db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1108db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed));
1109d1d35e2fSjeremylt   (*basis)->ref_count       = 1;
11106402da51SJeremy L Thompson   (*basis)->is_tensor_basis = false;
1111a8de75f0Sjeremylt   (*basis)->dim             = dim;
1112d99fa3c5SJeremy L Thompson   (*basis)->topo            = topo;
1113d1d35e2fSjeremylt   (*basis)->num_comp        = num_comp;
1114a8de75f0Sjeremylt   (*basis)->P               = P;
1115a8de75f0Sjeremylt   (*basis)->Q               = Q;
1116c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_H1;
11172b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q * dim, &(*basis)->q_ref_1d));
11182b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &(*basis)->q_weight_1d));
1119ff3a0f91SJeremy L Thompson   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
1120ff3a0f91SJeremy L Thompson   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
11212b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q * P, &(*basis)->interp));
11222b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(dim * Q * P, &(*basis)->grad));
1123ff3a0f91SJeremy L Thompson   if (interp) memcpy((*basis)->interp, interp, Q * P * sizeof(interp[0]));
1124ff3a0f91SJeremy L Thompson   if (grad) memcpy((*basis)->grad, grad, dim * Q * P * sizeof(grad[0]));
11252b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, *basis));
1126e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1127a8de75f0Sjeremylt }
1128a8de75f0Sjeremylt 
1129a8de75f0Sjeremylt /**
1130859c15bbSJames Wright   @brief Create a non tensor-product basis for \f$H(\mathrm{div})\f$ discretizations
113150c301a5SRezgar Shakeri 
1132*ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` object used to create the `CeedBasis`
1133ea61e9acSJeremy 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
1134ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of components (usually 1 for vectors in H(div) bases)
1135*ca94c3ddSJeremy L Thompson   @param[in]  num_nodes Total number of nodes (DoFs per element)
1136ea61e9acSJeremy L Thompson   @param[in]  num_qpts  Total number of quadrature points
1137*ca94c3ddSJeremy L Thompson   @param[in]  interp    Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points
1138*ca94c3ddSJeremy L Thompson   @param[in]  div       Row-major (`num_qpts * num_nodes`) matrix expressing divergence of basis functions at quadrature points
1139*ca94c3ddSJeremy L Thompson   @param[in]  q_ref     Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element
1140*ca94c3ddSJeremy L Thompson   @param[in]  q_weight  Array of length `num_qpts` holding the quadrature weights on the reference element
1141*ca94c3ddSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created `CeedBasis` will be stored
114250c301a5SRezgar Shakeri 
114350c301a5SRezgar Shakeri   @return An error code: 0 - success, otherwise - failure
114450c301a5SRezgar Shakeri 
114550c301a5SRezgar Shakeri   @ref User
114650c301a5SRezgar Shakeri **/
11472b730f8bSJeremy L Thompson int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
11482b730f8bSJeremy L Thompson                         const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
114950c301a5SRezgar Shakeri   CeedInt Q = num_qpts, P = num_nodes, dim = 0;
1150c4e3f59bSSebastian Grimberg 
115150c301a5SRezgar Shakeri   if (!ceed->BasisCreateHdiv) {
115250c301a5SRezgar Shakeri     Ceed delegate;
11536574a04fSJeremy L Thompson 
11542b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
11556574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv");
11562b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis));
115750c301a5SRezgar Shakeri     return CEED_ERROR_SUCCESS;
115850c301a5SRezgar Shakeri   }
115950c301a5SRezgar Shakeri 
1160*ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1161*ca94c3ddSJeremy L Thompson   CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1162*ca94c3ddSJeremy L Thompson   CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1163227444bfSJeremy L Thompson 
1164c4e3f59bSSebastian Grimberg   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1165c4e3f59bSSebastian Grimberg 
1166db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1167db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed));
116850c301a5SRezgar Shakeri   (*basis)->ref_count       = 1;
11696402da51SJeremy L Thompson   (*basis)->is_tensor_basis = false;
117050c301a5SRezgar Shakeri   (*basis)->dim             = dim;
117150c301a5SRezgar Shakeri   (*basis)->topo            = topo;
117250c301a5SRezgar Shakeri   (*basis)->num_comp        = num_comp;
117350c301a5SRezgar Shakeri   (*basis)->P               = P;
117450c301a5SRezgar Shakeri   (*basis)->Q               = Q;
1175c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_HDIV;
11762b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d));
11772b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d));
117850c301a5SRezgar Shakeri   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
117950c301a5SRezgar Shakeri   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
11802b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp));
11812b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q * P, &(*basis)->div));
118250c301a5SRezgar Shakeri   if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0]));
118350c301a5SRezgar Shakeri   if (div) memcpy((*basis)->div, div, Q * P * sizeof(div[0]));
11842b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, q_weight, *basis));
118550c301a5SRezgar Shakeri   return CEED_ERROR_SUCCESS;
118650c301a5SRezgar Shakeri }
118750c301a5SRezgar Shakeri 
118850c301a5SRezgar Shakeri /**
11894385fb7fSSebastian Grimberg   @brief Create a non tensor-product basis for \f$H(\mathrm{curl})\f$ discretizations
1190c4e3f59bSSebastian Grimberg 
1191*ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` object used to create the `CeedBasis`
1192c4e3f59bSSebastian Grimberg   @param[in]  topo      Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below
1193*ca94c3ddSJeremy L Thompson   @param[in]  num_comp  Number of components (usually 1 for vectors in \f$H(\mathrm{curl})\f$ bases)
1194*ca94c3ddSJeremy L Thompson   @param[in]  num_nodes Total number of nodes (DoFs per element)
1195c4e3f59bSSebastian Grimberg   @param[in]  num_qpts  Total number of quadrature points
1196*ca94c3ddSJeremy L Thompson   @param[in]  interp    Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points
1197*ca94c3ddSJeremy 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
1198*ca94c3ddSJeremy L Thompson   @param[in]  q_ref     Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element
1199*ca94c3ddSJeremy L Thompson   @param[in]  q_weight  Array of length `num_qpts` holding the quadrature weights on the reference element
1200*ca94c3ddSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created `CeedBasis` will be stored
1201c4e3f59bSSebastian Grimberg 
1202c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1203c4e3f59bSSebastian Grimberg 
1204c4e3f59bSSebastian Grimberg   @ref User
1205c4e3f59bSSebastian Grimberg **/
1206c4e3f59bSSebastian Grimberg int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
1207c4e3f59bSSebastian Grimberg                          const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
1208c4e3f59bSSebastian Grimberg   CeedInt Q = num_qpts, P = num_nodes, dim = 0, curl_comp = 0;
1209c4e3f59bSSebastian Grimberg 
1210d075f50bSSebastian Grimberg   if (!ceed->BasisCreateHcurl) {
1211c4e3f59bSSebastian Grimberg     Ceed delegate;
12126574a04fSJeremy L Thompson 
1213c4e3f59bSSebastian Grimberg     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
12146574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl");
1215c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis));
1216c4e3f59bSSebastian Grimberg     return CEED_ERROR_SUCCESS;
1217c4e3f59bSSebastian Grimberg   }
1218c4e3f59bSSebastian Grimberg 
1219*ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component");
1220*ca94c3ddSJeremy L Thompson   CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node");
1221*ca94c3ddSJeremy L Thompson   CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point");
1222c4e3f59bSSebastian Grimberg 
1223c4e3f59bSSebastian Grimberg   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1224c4e3f59bSSebastian Grimberg   curl_comp = (dim < 3) ? 1 : dim;
1225c4e3f59bSSebastian Grimberg 
1226db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1227db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed));
1228c4e3f59bSSebastian Grimberg   (*basis)->ref_count       = 1;
12296402da51SJeremy L Thompson   (*basis)->is_tensor_basis = false;
1230c4e3f59bSSebastian Grimberg   (*basis)->dim             = dim;
1231c4e3f59bSSebastian Grimberg   (*basis)->topo            = topo;
1232c4e3f59bSSebastian Grimberg   (*basis)->num_comp        = num_comp;
1233c4e3f59bSSebastian Grimberg   (*basis)->P               = P;
1234c4e3f59bSSebastian Grimberg   (*basis)->Q               = Q;
1235c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_HCURL;
1236c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d));
1237c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d));
1238c4e3f59bSSebastian Grimberg   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
1239c4e3f59bSSebastian Grimberg   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
1240c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp));
1241c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(curl_comp * Q * P, &(*basis)->curl));
1242c4e3f59bSSebastian Grimberg   if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0]));
1243c4e3f59bSSebastian Grimberg   if (curl) memcpy((*basis)->curl, curl, curl_comp * Q * P * sizeof(curl[0]));
1244c4e3f59bSSebastian Grimberg   CeedCall(ceed->BasisCreateHcurl(topo, dim, P, Q, interp, curl, q_ref, q_weight, *basis));
1245c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1246c4e3f59bSSebastian Grimberg }
1247c4e3f59bSSebastian Grimberg 
1248c4e3f59bSSebastian Grimberg /**
1249*ca94c3ddSJeremy L Thompson   @brief Create a `CeedBasis` for projection from the nodes of `basis_from` to the nodes of `basis_to`.
1250ba59ac12SSebastian Grimberg 
1251*ca94c3ddSJeremy L Thompson   Only @ref CEED_EVAL_INTERP will be valid for the new basis, `basis_project`.
1252*ca94c3ddSJeremy L Thompson   For \f$H^1\f$ spaces, @ref CEED_EVAL_GRAD will also be valid.
1253*ca94c3ddSJeremy L Thompson   The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization.
1254*ca94c3ddSJeremy L Thompson   The gradient (for the \f$H^1\f$ case) is given by `grad_project = interp_to^+ * grad_from`.
125515ad3917SSebastian Grimberg 
125615ad3917SSebastian Grimberg   Note: `basis_from` and `basis_to` must have compatible quadrature spaces.
125715ad3917SSebastian Grimberg 
12589fd66db6SSebastian Grimberg   Note: `basis_project` will have the same number of components as `basis_from`, regardless of the number of components that `basis_to` has.
12599fd66db6SSebastian Grimberg         If `basis_from` has 3 components and `basis_to` has 5 components, then `basis_project` will have 3 components.
1260f113e5dcSJeremy L Thompson 
1261*ca94c3ddSJeremy L Thompson   @param[in]  basis_from    `CeedBasis` to prolong from
1262*ca94c3ddSJeremy L Thompson   @param[in]  basis_to      `CeedBasis` to prolong to
1263*ca94c3ddSJeremy L Thompson   @param[out] basis_project Address of the variable where the newly created `CeedBasis` will be stored
1264f113e5dcSJeremy L Thompson 
1265f113e5dcSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1266f113e5dcSJeremy L Thompson 
1267f113e5dcSJeremy L Thompson   @ref User
1268f113e5dcSJeremy L Thompson **/
12692b730f8bSJeremy L Thompson int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project) {
1270f113e5dcSJeremy L Thompson   Ceed        ceed;
12711c66c397SJeremy L Thompson   bool        is_tensor;
12721c66c397SJeremy L Thompson   CeedInt     dim, num_comp;
12731c66c397SJeremy L Thompson   CeedScalar *q_ref, *q_weight, *interp_project, *grad_project;
12741c66c397SJeremy L Thompson 
12752b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis_to, &ceed));
1276f113e5dcSJeremy L Thompson 
1277ecc88aebSJeremy L Thompson   // Create projection matrix
12782b730f8bSJeremy L Thompson   CeedCall(CeedBasisCreateProjectionMatrices(basis_from, basis_to, &interp_project, &grad_project));
1279f113e5dcSJeremy L Thompson 
1280f113e5dcSJeremy L Thompson   // Build basis
12812b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis_to, &is_tensor));
12822b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis_to, &dim));
12832b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_from, &num_comp));
1284f113e5dcSJeremy L Thompson   if (is_tensor) {
1285f113e5dcSJeremy L Thompson     CeedInt P_1d_to, P_1d_from;
12861c66c397SJeremy L Thompson 
12872b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_1d_from));
12882b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_1d_to));
12892b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_to, &q_ref));
12902b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_to, &q_weight));
12912b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, interp_project, grad_project, q_ref, q_weight, basis_project));
1292f113e5dcSJeremy L Thompson   } else {
1293de05fbb2SSebastian Grimberg     // Even if basis_to and basis_from are not H1, the resulting basis is H1 for interpolation to work
1294f113e5dcSJeremy L Thompson     CeedInt          num_nodes_to, num_nodes_from;
12951c66c397SJeremy L Thompson     CeedElemTopology topo;
12961c66c397SJeremy L Thompson 
12971c66c397SJeremy L Thompson     CeedCall(CeedBasisGetTopology(basis_to, &topo));
12982b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_from, &num_nodes_from));
12992b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_to, &num_nodes_to));
13002b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_to * dim, &q_ref));
13012b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_to, &q_weight));
13022b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, q_ref, q_weight, basis_project));
1303f113e5dcSJeremy L Thompson   }
1304f113e5dcSJeremy L Thompson 
1305f113e5dcSJeremy L Thompson   // Cleanup
13062b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_project));
13072b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_project));
13082b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref));
13092b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight));
1310f113e5dcSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1311f113e5dcSJeremy L Thompson }
1312f113e5dcSJeremy L Thompson 
1313f113e5dcSJeremy L Thompson /**
1314*ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedBasis`.
13159560d06aSjeremylt 
1316*ca94c3ddSJeremy 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`.
1317*ca94c3ddSJeremy L Thompson         This `CeedBasis` will be destroyed if `*basis_copy` is the only reference to this `CeedBasis`.
1318ea61e9acSJeremy L Thompson 
1319*ca94c3ddSJeremy L Thompson   @param[in]     basis      `CeedBasis` to copy reference to
1320ea61e9acSJeremy L Thompson   @param[in,out] basis_copy Variable to store copied reference
13219560d06aSjeremylt 
13229560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
13239560d06aSjeremylt 
13249560d06aSjeremylt   @ref User
13259560d06aSjeremylt **/
13269560d06aSjeremylt int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) {
1327356036faSJeremy L Thompson   if (basis != CEED_BASIS_NONE) CeedCall(CeedBasisReference(basis));
13282b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(basis_copy));
13299560d06aSjeremylt   *basis_copy = basis;
13309560d06aSjeremylt   return CEED_ERROR_SUCCESS;
13319560d06aSjeremylt }
13329560d06aSjeremylt 
13339560d06aSjeremylt /**
1334*ca94c3ddSJeremy L Thompson   @brief View a `CeedBasis`
13357a982d89SJeremy L. Thompson 
1336*ca94c3ddSJeremy L Thompson   @param[in] basis  `CeedBasis` to view
1337*ca94c3ddSJeremy L Thompson   @param[in] stream Stream to view to, e.g., `stdout`
13387a982d89SJeremy L. Thompson 
13397a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
13407a982d89SJeremy L. Thompson 
13417a982d89SJeremy L. Thompson   @ref User
13427a982d89SJeremy L. Thompson **/
13437a982d89SJeremy L. Thompson int CeedBasisView(CeedBasis basis, FILE *stream) {
13441c66c397SJeremy L Thompson   CeedInt          q_comp   = 0;
134550c301a5SRezgar Shakeri   CeedElemTopology topo     = basis->topo;
1346c4e3f59bSSebastian Grimberg   CeedFESpace      fe_space = basis->fe_space;
13472b730f8bSJeremy L Thompson 
134850c301a5SRezgar Shakeri   // Print FE space and element topology of the basis
1349edf04919SJeremy L Thompson   fprintf(stream, "CeedBasis in a %s on a %s element\n", CeedFESpaces[fe_space], CeedElemTopologies[topo]);
13506402da51SJeremy L Thompson   if (basis->is_tensor_basis) {
1351edf04919SJeremy L Thompson     fprintf(stream, "  P: %" CeedInt_FMT "\n  Q: %" CeedInt_FMT "\n", basis->P_1d, basis->Q_1d);
135250c301a5SRezgar Shakeri   } else {
1353edf04919SJeremy L Thompson     fprintf(stream, "  P: %" CeedInt_FMT "\n  Q: %" CeedInt_FMT "\n", basis->P, basis->Q);
135450c301a5SRezgar Shakeri   }
1355edf04919SJeremy L Thompson   fprintf(stream, "  dimension: %" CeedInt_FMT "\n  field components: %" CeedInt_FMT "\n", basis->dim, basis->num_comp);
1356ea61e9acSJeremy L Thompson   // Print quadrature data, interpolation/gradient/divergence/curl of the basis
13576402da51SJeremy L Thompson   if (basis->is_tensor_basis) {  // tensor basis
13582b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("qref1d", "\t% 12.8f", 1, basis->Q_1d, basis->q_ref_1d, stream));
13592b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("qweight1d", "\t% 12.8f", 1, basis->Q_1d, basis->q_weight_1d, stream));
13602b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("interp1d", "\t% 12.8f", basis->Q_1d, basis->P_1d, basis->interp_1d, stream));
13612b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("grad1d", "\t% 12.8f", basis->Q_1d, basis->P_1d, basis->grad_1d, stream));
136250c301a5SRezgar Shakeri   } else {  // non-tensor basis
13632b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("qref", "\t% 12.8f", 1, basis->Q * basis->dim, basis->q_ref_1d, stream));
13642b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("qweight", "\t% 12.8f", 1, basis->Q, basis->q_weight_1d, stream));
1365c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp));
1366c4e3f59bSSebastian Grimberg     CeedCall(CeedScalarView("interp", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->interp, stream));
136750c301a5SRezgar Shakeri     if (basis->grad) {
1368c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp));
1369c4e3f59bSSebastian Grimberg       CeedCall(CeedScalarView("grad", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->grad, stream));
13707a982d89SJeremy L. Thompson     }
137150c301a5SRezgar Shakeri     if (basis->div) {
1372c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp));
1373c4e3f59bSSebastian Grimberg       CeedCall(CeedScalarView("div", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->div, stream));
1374c4e3f59bSSebastian Grimberg     }
1375c4e3f59bSSebastian Grimberg     if (basis->curl) {
1376c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp));
1377c4e3f59bSSebastian Grimberg       CeedCall(CeedScalarView("curl", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->curl, stream));
137850c301a5SRezgar Shakeri     }
137950c301a5SRezgar Shakeri   }
1380e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13817a982d89SJeremy L. Thompson }
13827a982d89SJeremy L. Thompson 
13837a982d89SJeremy L. Thompson /**
13847a982d89SJeremy L. Thompson   @brief Apply basis evaluation from nodes to quadrature points or vice versa
13857a982d89SJeremy L. Thompson 
1386*ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis` to evaluate
1387ea61e9acSJeremy L Thompson   @param[in]  num_elem  The number of elements to apply the basis evaluation to;
1388*ca94c3ddSJeremy L Thompson                           the backend will specify the ordering in @ref CeedElemRestrictionCreate()
1389*ca94c3ddSJeremy L Thompson   @param[in]  t_mode    @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points;
1390*ca94c3ddSJeremy L Thompson                           @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes
1391*ca94c3ddSJeremy L Thompson   @param[in]  eval_mode @ref CEED_EVAL_NONE to use values directly,
1392*ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_INTERP to use interpolated values,
1393*ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_GRAD to use gradients,
1394*ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_DIV to use divergence,
1395*ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_CURL to use curl,
1396*ca94c3ddSJeremy L Thompson                           @ref CEED_EVAL_WEIGHT to use quadrature weights
1397*ca94c3ddSJeremy L Thompson   @param[in]  u         Input `CeedVector`
1398*ca94c3ddSJeremy L Thompson   @param[out] v         Output `CeedVector`
13997a982d89SJeremy L. Thompson 
14007a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
14017a982d89SJeremy L. Thompson 
14027a982d89SJeremy L. Thompson   @ref User
14037a982d89SJeremy L. Thompson **/
14042b730f8bSJeremy L Thompson int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) {
1405c4e3f59bSSebastian Grimberg   CeedInt  dim, num_comp, q_comp, num_nodes, num_qpts;
14061c66c397SJeremy L Thompson   CeedSize u_length = 0, v_length;
14071c66c397SJeremy L Thompson 
14082b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
14092b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
1410c4e3f59bSSebastian Grimberg   CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
14112b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
14122b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
14132b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(v, &v_length));
1414c8c3fa7dSJeremy L Thompson   if (u) CeedCall(CeedVectorGetLength(u, &u_length));
14157a982d89SJeremy L. Thompson 
1416*ca94c3ddSJeremy L Thompson   CeedCheck(basis->Apply, basis->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedBasisApply");
1417e15f9bd0SJeremy L Thompson 
1418e15f9bd0SJeremy L Thompson   // Check compatibility of topological and geometrical dimensions
14196574a04fSJeremy L Thompson   CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0 && u_length % num_qpts == 0) ||
14206574a04fSJeremy L Thompson                 (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0 && v_length % num_qpts == 0),
14216574a04fSJeremy L Thompson             basis->ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions");
14227a982d89SJeremy L. Thompson 
1423e15f9bd0SJeremy L Thompson   // Check vector lengths to prevent out of bounds issues
14246574a04fSJeremy L Thompson   bool good_dims = true;
1425d1d35e2fSjeremylt   switch (eval_mode) {
1426e15f9bd0SJeremy L Thompson     case CEED_EVAL_NONE:
14272b730f8bSJeremy L Thompson     case CEED_EVAL_INTERP:
14282b730f8bSJeremy L Thompson     case CEED_EVAL_GRAD:
1429c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
1430c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
14316574a04fSJeremy L Thompson       good_dims =
14326574a04fSJeremy L Thompson           ((t_mode == CEED_TRANSPOSE && u_length >= num_elem * num_comp * num_qpts * q_comp && v_length >= num_elem * num_comp * num_nodes) ||
14336574a04fSJeremy L Thompson            (t_mode == CEED_NOTRANSPOSE && v_length >= num_elem * num_qpts * num_comp * q_comp && u_length >= num_elem * num_comp * num_nodes));
1434e15f9bd0SJeremy L Thompson       break;
1435e15f9bd0SJeremy L Thompson     case CEED_EVAL_WEIGHT:
14366574a04fSJeremy L Thompson       good_dims = v_length >= num_elem * num_qpts;
1437e15f9bd0SJeremy L Thompson       break;
1438e15f9bd0SJeremy L Thompson   }
14396574a04fSJeremy L Thompson   CeedCheck(good_dims, basis->ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode");
1440e15f9bd0SJeremy L Thompson 
14412b730f8bSJeremy L Thompson   CeedCall(basis->Apply(basis, num_elem, t_mode, eval_mode, u, v));
1442e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
14437a982d89SJeremy L. Thompson }
14447a982d89SJeremy L. Thompson 
14457a982d89SJeremy L. Thompson /**
1446c8c3fa7dSJeremy L Thompson   @brief Apply basis evaluation from nodes to arbitrary points
1447c8c3fa7dSJeremy L Thompson 
1448*ca94c3ddSJeremy L Thompson   @param[in]  basis      `CeedBasis` to evaluate
1449c8c3fa7dSJeremy L Thompson   @param[in]  num_points The number of points to apply the basis evaluation to
1450*ca94c3ddSJeremy L Thompson   @param[in]  t_mode     @ref CEED_NOTRANSPOSE to evaluate from nodes to points;
1451*ca94c3ddSJeremy L Thompson                            @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes
1452*ca94c3ddSJeremy L Thompson   @param[in]  eval_mode  @ref CEED_EVAL_INTERP to use interpolated values,
1453*ca94c3ddSJeremy L Thompson                            @ref CEED_EVAL_GRAD to use gradients,
1454*ca94c3ddSJeremy L Thompson                            @ref CEED_EVAL_WEIGHT to use quadrature weights
1455*ca94c3ddSJeremy L Thompson   @param[in]  x_ref      `CeedVector` holding reference coordinates of each point
1456*ca94c3ddSJeremy L Thompson   @param[in]  u          Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE
1457*ca94c3ddSJeremy L Thompson   @param[out] v          Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP
1458c8c3fa7dSJeremy L Thompson 
1459c8c3fa7dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1460c8c3fa7dSJeremy L Thompson 
1461c8c3fa7dSJeremy L Thompson   @ref User
1462c8c3fa7dSJeremy L Thompson **/
1463c8c3fa7dSJeremy L Thompson int CeedBasisApplyAtPoints(CeedBasis basis, CeedInt num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u,
1464c8c3fa7dSJeremy L Thompson                            CeedVector v) {
1465c8c3fa7dSJeremy L Thompson   CeedInt  dim, num_comp, num_q_comp, num_nodes, P_1d = 1, Q_1d = 1;
14661c66c397SJeremy L Thompson   CeedSize x_length = 0, u_length = 0, v_length;
1467c8c3fa7dSJeremy L Thompson 
1468c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
1469c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
1470c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
1471c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
1472c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &num_q_comp));
1473c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
1474c8c3fa7dSJeremy L Thompson   CeedCall(CeedVectorGetLength(v, &v_length));
1475953190f4SJeremy L Thompson   if (x_ref != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(x_ref, &x_length));
1476953190f4SJeremy L Thompson   if (u != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(u, &u_length));
1477c8c3fa7dSJeremy L Thompson 
1478c8c3fa7dSJeremy L Thompson   // Check compatibility of topological and geometrical dimensions
1479953190f4SJeremy L Thompson   CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0) || (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0) ||
1480953190f4SJeremy L Thompson                 (eval_mode == CEED_EVAL_WEIGHT),
1481953190f4SJeremy L Thompson             basis->ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions and number of points");
1482c8c3fa7dSJeremy L Thompson 
1483c8c3fa7dSJeremy L Thompson   // Check compatibility coordinates vector
1484953190f4SJeremy L Thompson   CeedCheck((x_length >= num_points * dim) || (eval_mode == CEED_EVAL_WEIGHT), basis->ceed, CEED_ERROR_DIMENSION,
1485c8c3fa7dSJeremy L Thompson             "Length of reference coordinate vector incompatible with basis dimension and number of points");
1486c8c3fa7dSJeremy L Thompson 
1487953190f4SJeremy L Thompson   // Check CEED_EVAL_WEIGHT only on CEED_NOTRANSPOSE
1488953190f4SJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT || t_mode == CEED_NOTRANSPOSE, basis->ceed, CEED_ERROR_UNSUPPORTED,
1489953190f4SJeremy L Thompson             "CEED_EVAL_WEIGHT only supported with CEED_NOTRANSPOSE");
1490953190f4SJeremy L Thompson 
1491c8c3fa7dSJeremy L Thompson   // Check vector lengths to prevent out of bounds issues
1492c8c3fa7dSJeremy L Thompson   bool good_dims = false;
1493c8c3fa7dSJeremy L Thompson   switch (eval_mode) {
1494c8c3fa7dSJeremy L Thompson     case CEED_EVAL_INTERP:
1495c8c3fa7dSJeremy L Thompson       good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= num_points * num_q_comp || v_length >= num_nodes * num_comp)) ||
1496c8c3fa7dSJeremy L Thompson                    (t_mode == CEED_NOTRANSPOSE && (v_length >= num_points * num_q_comp || u_length >= num_nodes * num_comp)));
1497c8c3fa7dSJeremy L Thompson       break;
1498c8c3fa7dSJeremy L Thompson     case CEED_EVAL_GRAD:
1499edfbf3a6SJeremy L Thompson       good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= num_points * num_q_comp * dim || v_length >= num_nodes * num_comp)) ||
1500edfbf3a6SJeremy L Thompson                    (t_mode == CEED_NOTRANSPOSE && (v_length >= num_points * num_q_comp * dim || u_length >= num_nodes * num_comp)));
1501edfbf3a6SJeremy L Thompson       break;
1502c8c3fa7dSJeremy L Thompson     case CEED_EVAL_WEIGHT:
1503953190f4SJeremy L Thompson       good_dims = t_mode == CEED_NOTRANSPOSE && (v_length >= num_points);
1504953190f4SJeremy L Thompson       break;
1505953190f4SJeremy L Thompson     case CEED_EVAL_NONE:
1506c8c3fa7dSJeremy L Thompson     case CEED_EVAL_DIV:
1507c8c3fa7dSJeremy L Thompson     case CEED_EVAL_CURL:
1508c8c3fa7dSJeremy L Thompson       // LCOV_EXCL_START
1509c8c3fa7dSJeremy L Thompson       return CeedError(basis->ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points not supported for %s", CeedEvalModes[eval_mode]);
1510c8c3fa7dSJeremy L Thompson       // LCOV_EXCL_STOP
1511c8c3fa7dSJeremy L Thompson   }
1512c8c3fa7dSJeremy L Thompson   CeedCheck(good_dims, basis->ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode");
1513c8c3fa7dSJeremy L Thompson 
1514c8c3fa7dSJeremy L Thompson   // Backend method
1515c8c3fa7dSJeremy L Thompson   if (basis->ApplyAtPoints) {
1516c8c3fa7dSJeremy L Thompson     CeedCall(basis->ApplyAtPoints(basis, num_points, t_mode, eval_mode, x_ref, u, v));
1517c8c3fa7dSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1518c8c3fa7dSJeremy L Thompson   }
1519c8c3fa7dSJeremy L Thompson 
1520c8c3fa7dSJeremy L Thompson   // Default implementation
1521c8c3fa7dSJeremy L Thompson   CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points only supported for tensor product bases");
1522953190f4SJeremy L Thompson   if (eval_mode == CEED_EVAL_WEIGHT) {
1523953190f4SJeremy L Thompson     CeedCall(CeedVectorSetValue(v, 1.0));
1524953190f4SJeremy L Thompson     return CEED_ERROR_SUCCESS;
1525953190f4SJeremy L Thompson   }
1526c8c3fa7dSJeremy L Thompson   if (!basis->basis_chebyshev) {
1527c8c3fa7dSJeremy L Thompson     // Build matrix mapping from quadrature point values to Chebyshev coefficients
1528c8c3fa7dSJeremy L Thompson     CeedScalar       *tau, *C, *I, *chebyshev_coeffs_1d;
1529c8c3fa7dSJeremy L Thompson     const CeedScalar *q_ref_1d;
1530c8c3fa7dSJeremy L Thompson 
1531c8c3fa7dSJeremy L Thompson     // Build coefficient matrix
1532c8c3fa7dSJeremy L Thompson     // -- Note: Clang-tidy needs this check because it does not understand the is_tensor_basis check above
1533*ca94c3ddSJeremy L Thompson     CeedCheck(P_1d > 0 && Q_1d > 0, basis->ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis dimensions are malformed");
1534c8c3fa7dSJeremy L Thompson     CeedCall(CeedCalloc(Q_1d * Q_1d, &C));
1535c8c3fa7dSJeremy L Thompson     CeedCall(CeedBasisGetQRef(basis, &q_ref_1d));
15363778dbaaSJeremy L Thompson     for (CeedInt i = 0; i < Q_1d; i++) CeedCall(CeedChebyshevPolynomialsAtPoint(q_ref_1d[i], Q_1d, &C[i * Q_1d]));
1537c8c3fa7dSJeremy L Thompson 
1538c8c3fa7dSJeremy L Thompson     // Inverse of coefficient matrix
1539c8c3fa7dSJeremy L Thompson     CeedCall(CeedCalloc(Q_1d * Q_1d, &chebyshev_coeffs_1d));
1540c8c3fa7dSJeremy L Thompson     CeedCall(CeedCalloc(Q_1d * Q_1d, &I));
1541c8c3fa7dSJeremy L Thompson     CeedCall(CeedCalloc(Q_1d, &tau));
1542c8c3fa7dSJeremy L Thompson     // -- QR Factorization, C = Q R
1543c8c3fa7dSJeremy L Thompson     CeedCall(CeedQRFactorization(basis->ceed, C, tau, Q_1d, Q_1d));
1544c8c3fa7dSJeremy L Thompson     // -- chebyshev_coeffs_1d = R_inv Q^T
1545c8c3fa7dSJeremy L Thompson     for (CeedInt i = 0; i < Q_1d; i++) I[i * Q_1d + i] = 1.0;
1546c8c3fa7dSJeremy L Thompson     // ---- Apply R_inv, chebyshev_coeffs_1d = I R_inv
1547c8c3fa7dSJeremy L Thompson     for (CeedInt i = 0; i < Q_1d; i++) {  // Row i
1548c8c3fa7dSJeremy L Thompson       chebyshev_coeffs_1d[Q_1d * i] = I[Q_1d * i] / C[0];
1549c8c3fa7dSJeremy L Thompson       for (CeedInt j = 1; j < Q_1d; j++) {  // Column j
1550c8c3fa7dSJeremy L Thompson         chebyshev_coeffs_1d[j + Q_1d * i] = I[j + Q_1d * i];
1551c8c3fa7dSJeremy L Thompson         for (CeedInt k = 0; k < j; k++) chebyshev_coeffs_1d[j + Q_1d * i] -= C[j + Q_1d * k] * chebyshev_coeffs_1d[k + Q_1d * i];
1552c8c3fa7dSJeremy L Thompson         chebyshev_coeffs_1d[j + Q_1d * i] /= C[j + Q_1d * j];
1553c8c3fa7dSJeremy L Thompson       }
1554c8c3fa7dSJeremy L Thompson     }
1555c8c3fa7dSJeremy L Thompson     // ---- Apply Q^T, chebyshev_coeffs_1d = R_inv Q^T
1556c8c3fa7dSJeremy L Thompson     CeedCall(CeedHouseholderApplyQ(chebyshev_coeffs_1d, C, tau, CEED_NOTRANSPOSE, Q_1d, Q_1d, Q_1d, 1, Q_1d));
1557c8c3fa7dSJeremy L Thompson 
1558c8c3fa7dSJeremy L Thompson     // Build basis mapping from nodes to Chebyshev coefficients
1559c8c3fa7dSJeremy L Thompson     CeedScalar       *chebyshev_interp_1d, *chebyshev_grad_1d, *chebyshev_q_weight_1d;
1560c8c3fa7dSJeremy L Thompson     const CeedScalar *interp_1d;
1561c8c3fa7dSJeremy L Thompson 
156271a83b88SJeremy L Thompson     CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
156371a83b88SJeremy L Thompson     CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_grad_1d));
1564c8c3fa7dSJeremy L Thompson     CeedCall(CeedCalloc(Q_1d, &chebyshev_q_weight_1d));
1565c8c3fa7dSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
1566c8c3fa7dSJeremy L Thompson     CeedCall(CeedMatrixMatrixMultiply(basis->ceed, chebyshev_coeffs_1d, interp_1d, chebyshev_interp_1d, Q_1d, P_1d, Q_1d));
1567c8c3fa7dSJeremy L Thompson 
1568c8c3fa7dSJeremy L Thompson     CeedCall(CeedVectorCreate(basis->ceed, num_comp * CeedIntPow(Q_1d, dim), &basis->vec_chebyshev));
156971a83b88SJeremy L Thompson     CeedCall(CeedBasisCreateTensorH1(basis->ceed, dim, num_comp, P_1d, Q_1d, chebyshev_interp_1d, chebyshev_grad_1d, q_ref_1d, chebyshev_q_weight_1d,
1570c8c3fa7dSJeremy L Thompson                                      &basis->basis_chebyshev));
1571c8c3fa7dSJeremy L Thompson 
1572c8c3fa7dSJeremy L Thompson     // Cleanup
1573c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&C));
1574c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&chebyshev_coeffs_1d));
1575c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&I));
1576c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&tau));
1577c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&chebyshev_interp_1d));
1578c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&chebyshev_grad_1d));
1579c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&chebyshev_q_weight_1d));
1580c8c3fa7dSJeremy L Thompson   }
1581c8c3fa7dSJeremy L Thompson 
1582c8c3fa7dSJeremy L Thompson   // Create TensorContract object if needed, such as a basis from the GPU backends
1583c8c3fa7dSJeremy L Thompson   if (!basis->contract) {
1584c8c3fa7dSJeremy L Thompson     Ceed      ceed_ref;
1585585a562dSJeremy L Thompson     CeedBasis basis_ref = NULL;
1586c8c3fa7dSJeremy L Thompson 
1587c8c3fa7dSJeremy L Thompson     CeedCall(CeedInit("/cpu/self", &ceed_ref));
1588c8c3fa7dSJeremy L Thompson     // Only need matching tensor contraction dimensions, any type of basis will work
158971a83b88SJeremy L Thompson     CeedCall(CeedBasisCreateTensorH1Lagrange(ceed_ref, dim, num_comp, P_1d, Q_1d, CEED_GAUSS, &basis_ref));
1590585a562dSJeremy L Thompson     // Note - clang-tidy doesn't know basis_ref->contract must be valid here
1591585a562dSJeremy L Thompson     CeedCheck(basis_ref && basis_ref->contract, basis->ceed, CEED_ERROR_UNSUPPORTED,
15921c66c397SJeremy L Thompson               "Reference CPU ceed failed to create a tensor contraction object");
1593585a562dSJeremy L Thompson     CeedCall(CeedTensorContractReferenceCopy(basis_ref->contract, &basis->contract));
1594c8c3fa7dSJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis_ref));
1595c8c3fa7dSJeremy L Thompson     CeedCall(CeedDestroy(&ceed_ref));
1596c8c3fa7dSJeremy L Thompson   }
1597c8c3fa7dSJeremy L Thompson 
1598c8c3fa7dSJeremy L Thompson   // Basis evaluation
1599c8c3fa7dSJeremy L Thompson   switch (t_mode) {
1600c8c3fa7dSJeremy L Thompson     case CEED_NOTRANSPOSE: {
1601c8c3fa7dSJeremy L Thompson       // Nodes to arbitrary points
1602c8c3fa7dSJeremy L Thompson       CeedScalar       *v_array;
1603c8c3fa7dSJeremy L Thompson       const CeedScalar *chebyshev_coeffs, *x_array_read;
1604c8c3fa7dSJeremy L Thompson 
1605c8c3fa7dSJeremy L Thompson       // -- Interpolate to Chebyshev coefficients
1606c8c3fa7dSJeremy L Thompson       CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, basis->vec_chebyshev));
1607c8c3fa7dSJeremy L Thompson 
1608c8c3fa7dSJeremy L Thompson       // -- Evaluate Chebyshev polynomials at arbitrary points
1609c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs));
1610c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read));
1611c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorGetArrayWrite(v, CEED_MEM_HOST, &v_array));
1612edfbf3a6SJeremy L Thompson       switch (eval_mode) {
1613edfbf3a6SJeremy L Thompson         case CEED_EVAL_INTERP: {
1614c8c3fa7dSJeremy L Thompson           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
1615c8c3fa7dSJeremy L Thompson 
1616c8c3fa7dSJeremy L Thompson           // ---- Values at point
1617c8c3fa7dSJeremy L Thompson           for (CeedInt p = 0; p < num_points; p++) {
1618c8c3fa7dSJeremy L Thompson             CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1;
1619c8c3fa7dSJeremy L Thompson 
162053ef2869SZach Atkins             for (CeedInt d = 0; d < dim; d++) {
16213778dbaaSJeremy L Thompson               // ------ Tensor contract with current Chebyshev polynomial values
16229c34f28eSJeremy L Thompson               CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x));
1623c8c3fa7dSJeremy L Thompson               CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false,
16244608bdaaSJeremy L Thompson                                                d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2]));
1625c8c3fa7dSJeremy L Thompson               pre /= Q_1d;
1626c8c3fa7dSJeremy L Thompson               post *= 1;
1627c8c3fa7dSJeremy L Thompson             }
16284608bdaaSJeremy L Thompson             for (CeedInt c = 0; c < num_comp; c++) v_array[c * num_points + p] = tmp[dim % 2][c];
1629c8c3fa7dSJeremy L Thompson           }
1630edfbf3a6SJeremy L Thompson           break;
1631edfbf3a6SJeremy L Thompson         }
1632edfbf3a6SJeremy L Thompson         case CEED_EVAL_GRAD: {
1633edfbf3a6SJeremy L Thompson           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
1634edfbf3a6SJeremy L Thompson 
1635edfbf3a6SJeremy L Thompson           // ---- Values at point
1636edfbf3a6SJeremy L Thompson           for (CeedInt p = 0; p < num_points; p++) {
1637edfbf3a6SJeremy L Thompson             // Dim**2 contractions, apply grad when pass == dim
163853ef2869SZach Atkins             for (CeedInt pass = 0; pass < dim; pass++) {
1639edfbf3a6SJeremy L Thompson               CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1;
1640edfbf3a6SJeremy L Thompson 
164153ef2869SZach Atkins               for (CeedInt d = 0; d < dim; d++) {
16423778dbaaSJeremy L Thompson                 // ------ Tensor contract with current Chebyshev polynomial values
16439c34f28eSJeremy L Thompson                 if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x));
16449c34f28eSJeremy L Thompson                 else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x));
1645edfbf3a6SJeremy L Thompson                 CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false,
16464608bdaaSJeremy L Thompson                                                  d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2]));
1647edfbf3a6SJeremy L Thompson                 pre /= Q_1d;
1648edfbf3a6SJeremy L Thompson                 post *= 1;
1649edfbf3a6SJeremy L Thompson               }
16504608bdaaSJeremy L Thompson               for (CeedInt c = 0; c < num_comp; c++) v_array[(pass * num_comp + c) * num_points + p] = tmp[dim % 2][c];
1651edfbf3a6SJeremy L Thompson             }
1652edfbf3a6SJeremy L Thompson           }
1653edfbf3a6SJeremy L Thompson           break;
1654edfbf3a6SJeremy L Thompson         }
1655edfbf3a6SJeremy L Thompson         default:
1656953190f4SJeremy L Thompson           // Nothing to do, excluded above
1657edfbf3a6SJeremy L Thompson           break;
1658c8c3fa7dSJeremy L Thompson       }
1659c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(basis->vec_chebyshev, &chebyshev_coeffs));
1660c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read));
1661c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorRestoreArray(v, &v_array));
1662c8c3fa7dSJeremy L Thompson       break;
1663c8c3fa7dSJeremy L Thompson     }
16642a94f45fSJeremy L Thompson     case CEED_TRANSPOSE: {
16653778dbaaSJeremy L Thompson       // Note: No switch on e_mode here because only CEED_EVAL_INTERP is supported at this time
16662a94f45fSJeremy L Thompson       // Arbitrary points to nodes
16672a94f45fSJeremy L Thompson       CeedScalar       *chebyshev_coeffs;
16682a94f45fSJeremy L Thompson       const CeedScalar *u_array, *x_array_read;
16692a94f45fSJeremy L Thompson 
16701c66c397SJeremy L Thompson       // -- Transpose of evaluation of Chebyshev polynomials at arbitrary points
16712a94f45fSJeremy L Thompson       CeedCall(CeedVectorGetArrayWrite(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs));
16722a94f45fSJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read));
16732a94f45fSJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array));
1674038a8942SZach Atkins 
1675038a8942SZach Atkins       switch (eval_mode) {
1676038a8942SZach Atkins         case CEED_EVAL_INTERP: {
16772a94f45fSJeremy L Thompson           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
16782a94f45fSJeremy L Thompson 
16792a94f45fSJeremy L Thompson           // ---- Values at point
16802a94f45fSJeremy L Thompson           for (CeedInt p = 0; p < num_points; p++) {
16812a94f45fSJeremy L Thompson             CeedInt pre = num_comp * 1, post = 1;
16822a94f45fSJeremy L Thompson 
16834608bdaaSJeremy L Thompson             for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[c * num_points + p];
168453ef2869SZach Atkins             for (CeedInt d = 0; d < dim; d++) {
16853778dbaaSJeremy L Thompson               // ------ Tensor contract with current Chebyshev polynomial values
16869c34f28eSJeremy L Thompson               CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x));
16874608bdaaSJeremy L Thompson               CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, p > 0 && d == (dim - 1), tmp[d % 2],
16884608bdaaSJeremy L Thompson                                                d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2]));
16892a94f45fSJeremy L Thompson               pre /= 1;
16902a94f45fSJeremy L Thompson               post *= Q_1d;
16912a94f45fSJeremy L Thompson             }
16922a94f45fSJeremy L Thompson           }
1693038a8942SZach Atkins           break;
1694038a8942SZach Atkins         }
1695038a8942SZach Atkins         case CEED_EVAL_GRAD: {
1696038a8942SZach Atkins           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
1697038a8942SZach Atkins 
1698038a8942SZach Atkins           // ---- Values at point
1699038a8942SZach Atkins           for (CeedInt p = 0; p < num_points; p++) {
1700038a8942SZach Atkins             // Dim**2 contractions, apply grad when pass == dim
1701038a8942SZach Atkins             for (CeedInt pass = 0; pass < dim; pass++) {
1702038a8942SZach Atkins               CeedInt pre = num_comp * 1, post = 1;
1703038a8942SZach Atkins 
17044608bdaaSJeremy L Thompson               for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[(pass * num_comp + c) * num_points + p];
1705038a8942SZach Atkins               for (CeedInt d = 0; d < dim; d++) {
1706038a8942SZach Atkins                 // ------ Tensor contract with current Chebyshev polynomial values
17079c34f28eSJeremy L Thompson                 if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x));
17089c34f28eSJeremy L Thompson                 else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * num_points + p], Q_1d, chebyshev_x));
17094608bdaaSJeremy L Thompson                 CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode,
17104608bdaaSJeremy L Thompson                                                  (p > 0 || (p == 0 && pass > 0)) && d == (dim - 1), tmp[d % 2],
17114608bdaaSJeremy L Thompson                                                  d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2]));
1712038a8942SZach Atkins                 pre /= 1;
1713038a8942SZach Atkins                 post *= Q_1d;
1714038a8942SZach Atkins               }
1715038a8942SZach Atkins             }
1716038a8942SZach Atkins           }
1717038a8942SZach Atkins           break;
1718038a8942SZach Atkins         }
1719038a8942SZach Atkins         default:
1720038a8942SZach Atkins           // Nothing to do, excluded above
1721038a8942SZach Atkins           break;
17222a94f45fSJeremy L Thompson       }
17232a94f45fSJeremy L Thompson       CeedCall(CeedVectorRestoreArray(basis->vec_chebyshev, &chebyshev_coeffs));
17242a94f45fSJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read));
17252a94f45fSJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(u, &u_array));
17262a94f45fSJeremy L Thompson 
17272a94f45fSJeremy L Thompson       // -- Interpolate transpose from Chebyshev coefficients
17282a94f45fSJeremy L Thompson       CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v));
17292a94f45fSJeremy L Thompson       break;
17302a94f45fSJeremy L Thompson     }
1731c8c3fa7dSJeremy L Thompson   }
1732c8c3fa7dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1733c8c3fa7dSJeremy L Thompson }
1734c8c3fa7dSJeremy L Thompson 
1735c8c3fa7dSJeremy L Thompson /**
1736*ca94c3ddSJeremy L Thompson   @brief Get `Ceed` associated with a `CeedBasis`
1737b7c9bbdaSJeremy L Thompson 
1738*ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
1739*ca94c3ddSJeremy L Thompson   @param[out] ceed  Variable to store `Ceed`
1740b7c9bbdaSJeremy L Thompson 
1741b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1742b7c9bbdaSJeremy L Thompson 
1743b7c9bbdaSJeremy L Thompson   @ref Advanced
1744b7c9bbdaSJeremy L Thompson **/
1745b7c9bbdaSJeremy L Thompson int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) {
1746b7c9bbdaSJeremy L Thompson   *ceed = basis->ceed;
1747b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1748b7c9bbdaSJeremy L Thompson }
1749b7c9bbdaSJeremy L Thompson 
1750b7c9bbdaSJeremy L Thompson /**
1751*ca94c3ddSJeremy L Thompson   @brief Get dimension for given `CeedBasis`
17529d007619Sjeremylt 
1753*ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
17549d007619Sjeremylt   @param[out] dim   Variable to store dimension of basis
17559d007619Sjeremylt 
17569d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
17579d007619Sjeremylt 
1758b7c9bbdaSJeremy L Thompson   @ref Advanced
17599d007619Sjeremylt **/
17609d007619Sjeremylt int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) {
17619d007619Sjeremylt   *dim = basis->dim;
1762e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17639d007619Sjeremylt }
17649d007619Sjeremylt 
17659d007619Sjeremylt /**
1766*ca94c3ddSJeremy L Thompson   @brief Get topology for given `CeedBasis`
1767d99fa3c5SJeremy L Thompson 
1768*ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
1769d99fa3c5SJeremy L Thompson   @param[out] topo  Variable to store topology of basis
1770d99fa3c5SJeremy L Thompson 
1771d99fa3c5SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1772d99fa3c5SJeremy L Thompson 
1773b7c9bbdaSJeremy L Thompson   @ref Advanced
1774d99fa3c5SJeremy L Thompson **/
1775d99fa3c5SJeremy L Thompson int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) {
1776d99fa3c5SJeremy L Thompson   *topo = basis->topo;
1777e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1778d99fa3c5SJeremy L Thompson }
1779d99fa3c5SJeremy L Thompson 
1780d99fa3c5SJeremy L Thompson /**
1781*ca94c3ddSJeremy L Thompson   @brief Get number of components for given `CeedBasis`
17829d007619Sjeremylt 
1783*ca94c3ddSJeremy L Thompson   @param[in]  basis    `CeedBasis`
1784*ca94c3ddSJeremy L Thompson   @param[out] num_comp Variable to store number of components
17859d007619Sjeremylt 
17869d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
17879d007619Sjeremylt 
1788b7c9bbdaSJeremy L Thompson   @ref Advanced
17899d007619Sjeremylt **/
1790d1d35e2fSjeremylt int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) {
1791d1d35e2fSjeremylt   *num_comp = basis->num_comp;
1792e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17939d007619Sjeremylt }
17949d007619Sjeremylt 
17959d007619Sjeremylt /**
1796*ca94c3ddSJeremy L Thompson   @brief Get total number of nodes (in `dim` dimensions) of a `CeedBasis`
17979d007619Sjeremylt 
1798*ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
17999d007619Sjeremylt   @param[out] P     Variable to store number of nodes
18009d007619Sjeremylt 
18019d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18029d007619Sjeremylt 
18039d007619Sjeremylt   @ref Utility
18049d007619Sjeremylt **/
18059d007619Sjeremylt int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) {
18069d007619Sjeremylt   *P = basis->P;
1807e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18089d007619Sjeremylt }
18099d007619Sjeremylt 
18109d007619Sjeremylt /**
1811*ca94c3ddSJeremy L Thompson   @brief Get total number of nodes (in 1 dimension) of a `CeedBasis`
18129d007619Sjeremylt 
1813*ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
1814d1d35e2fSjeremylt   @param[out] P_1d  Variable to store number of nodes
18159d007619Sjeremylt 
18169d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18179d007619Sjeremylt 
1818b7c9bbdaSJeremy L Thompson   @ref Advanced
18199d007619Sjeremylt **/
1820d1d35e2fSjeremylt int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) {
1821*ca94c3ddSJeremy L Thompson   CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "Cannot supply P_1d for non-tensor CeedBasis");
1822d1d35e2fSjeremylt   *P_1d = basis->P_1d;
1823e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18249d007619Sjeremylt }
18259d007619Sjeremylt 
18269d007619Sjeremylt /**
1827*ca94c3ddSJeremy L Thompson   @brief Get total number of quadrature points (in `dim` dimensions) of a `CeedBasis`
18289d007619Sjeremylt 
1829*ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
18309d007619Sjeremylt   @param[out] Q     Variable to store number of quadrature points
18319d007619Sjeremylt 
18329d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18339d007619Sjeremylt 
18349d007619Sjeremylt   @ref Utility
18359d007619Sjeremylt **/
18369d007619Sjeremylt int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) {
18379d007619Sjeremylt   *Q = basis->Q;
1838e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18399d007619Sjeremylt }
18409d007619Sjeremylt 
18419d007619Sjeremylt /**
1842*ca94c3ddSJeremy L Thompson   @brief Get total number of quadrature points (in 1 dimension) of a `CeedBasis`
18439d007619Sjeremylt 
1844*ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
1845d1d35e2fSjeremylt   @param[out] Q_1d  Variable to store number of quadrature points
18469d007619Sjeremylt 
18479d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18489d007619Sjeremylt 
1849b7c9bbdaSJeremy L Thompson   @ref Advanced
18509d007619Sjeremylt **/
1851d1d35e2fSjeremylt int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) {
1852*ca94c3ddSJeremy L Thompson   CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "Cannot supply Q_1d for non-tensor CeedBasis");
1853d1d35e2fSjeremylt   *Q_1d = basis->Q_1d;
1854e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18559d007619Sjeremylt }
18569d007619Sjeremylt 
18579d007619Sjeremylt /**
1858*ca94c3ddSJeremy L Thompson   @brief Get reference coordinates of quadrature points (in `dim` dimensions) of a `CeedBasis`
18599d007619Sjeremylt 
1860*ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
1861d1d35e2fSjeremylt   @param[out] q_ref Variable to store reference coordinates of quadrature points
18629d007619Sjeremylt 
18639d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18649d007619Sjeremylt 
1865b7c9bbdaSJeremy L Thompson   @ref Advanced
18669d007619Sjeremylt **/
1867d1d35e2fSjeremylt int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) {
1868d1d35e2fSjeremylt   *q_ref = basis->q_ref_1d;
1869e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18709d007619Sjeremylt }
18719d007619Sjeremylt 
18729d007619Sjeremylt /**
1873*ca94c3ddSJeremy L Thompson   @brief Get quadrature weights of quadrature points (in `dim` dimensions) of a `CeedBasis`
18749d007619Sjeremylt 
1875*ca94c3ddSJeremy L Thompson   @param[in]  basis    `CeedBasis`
1876d1d35e2fSjeremylt   @param[out] q_weight Variable to store quadrature weights
18779d007619Sjeremylt 
18789d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18799d007619Sjeremylt 
1880b7c9bbdaSJeremy L Thompson   @ref Advanced
18819d007619Sjeremylt **/
1882d1d35e2fSjeremylt int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) {
1883d1d35e2fSjeremylt   *q_weight = basis->q_weight_1d;
1884e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18859d007619Sjeremylt }
18869d007619Sjeremylt 
18879d007619Sjeremylt /**
1888*ca94c3ddSJeremy L Thompson   @brief Get interpolation matrix of a `CeedBasis`
18899d007619Sjeremylt 
1890*ca94c3ddSJeremy L Thompson   @param[in]  basis  `CeedBasis`
18919d007619Sjeremylt   @param[out] interp Variable to store interpolation matrix
18929d007619Sjeremylt 
18939d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18949d007619Sjeremylt 
1895b7c9bbdaSJeremy L Thompson   @ref Advanced
18969d007619Sjeremylt **/
18976c58de82SJeremy L Thompson int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) {
18986402da51SJeremy L Thompson   if (!basis->interp && basis->is_tensor_basis) {
18999d007619Sjeremylt     // Allocate
19002b730f8bSJeremy L Thompson     CeedCall(CeedMalloc(basis->Q * basis->P, &basis->interp));
19019d007619Sjeremylt 
19029d007619Sjeremylt     // Initialize
19032b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < basis->Q * basis->P; i++) basis->interp[i] = 1.0;
19049d007619Sjeremylt 
19059d007619Sjeremylt     // Calculate
19062b730f8bSJeremy L Thompson     for (CeedInt d = 0; d < basis->dim; d++) {
19072b730f8bSJeremy L Thompson       for (CeedInt qpt = 0; qpt < basis->Q; qpt++) {
19089d007619Sjeremylt         for (CeedInt node = 0; node < basis->P; node++) {
1909d1d35e2fSjeremylt           CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d;
1910d1d35e2fSjeremylt           CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d;
19111c66c397SJeremy L Thompson 
1912d1d35e2fSjeremylt           basis->interp[qpt * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p];
19139d007619Sjeremylt         }
19149d007619Sjeremylt       }
19152b730f8bSJeremy L Thompson     }
19162b730f8bSJeremy L Thompson   }
19179d007619Sjeremylt   *interp = basis->interp;
1918e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
19199d007619Sjeremylt }
19209d007619Sjeremylt 
19219d007619Sjeremylt /**
1922*ca94c3ddSJeremy L Thompson   @brief Get 1D interpolation matrix of a tensor product `CeedBasis`
19239d007619Sjeremylt 
1924*ca94c3ddSJeremy L Thompson   @param[in]  basis     `CeedBasis`
1925d1d35e2fSjeremylt   @param[out] interp_1d Variable to store interpolation matrix
19269d007619Sjeremylt 
19279d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
19289d007619Sjeremylt 
19299d007619Sjeremylt   @ref Backend
19309d007619Sjeremylt **/
1931d1d35e2fSjeremylt int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) {
1932*ca94c3ddSJeremy L Thompson   CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis");
1933d1d35e2fSjeremylt   *interp_1d = basis->interp_1d;
1934e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
19359d007619Sjeremylt }
19369d007619Sjeremylt 
19379d007619Sjeremylt /**
1938*ca94c3ddSJeremy L Thompson   @brief Get gradient matrix of a `CeedBasis`
19399d007619Sjeremylt 
1940*ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
19419d007619Sjeremylt   @param[out] grad  Variable to store gradient matrix
19429d007619Sjeremylt 
19439d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
19449d007619Sjeremylt 
1945b7c9bbdaSJeremy L Thompson   @ref Advanced
19469d007619Sjeremylt **/
19476c58de82SJeremy L Thompson int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) {
19486402da51SJeremy L Thompson   if (!basis->grad && basis->is_tensor_basis) {
19499d007619Sjeremylt     // Allocate
19502b730f8bSJeremy L Thompson     CeedCall(CeedMalloc(basis->dim * basis->Q * basis->P, &basis->grad));
19519d007619Sjeremylt 
19529d007619Sjeremylt     // Initialize
19532b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < basis->dim * basis->Q * basis->P; i++) basis->grad[i] = 1.0;
19549d007619Sjeremylt 
19559d007619Sjeremylt     // Calculate
19562b730f8bSJeremy L Thompson     for (CeedInt d = 0; d < basis->dim; d++) {
19572b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < basis->dim; i++) {
19582b730f8bSJeremy L Thompson         for (CeedInt qpt = 0; qpt < basis->Q; qpt++) {
19599d007619Sjeremylt           for (CeedInt node = 0; node < basis->P; node++) {
1960d1d35e2fSjeremylt             CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d;
1961d1d35e2fSjeremylt             CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d;
19621c66c397SJeremy L Thompson 
19632b730f8bSJeremy L Thompson             if (i == d) basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->grad_1d[q * basis->P_1d + p];
19642b730f8bSJeremy L Thompson             else basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p];
19652b730f8bSJeremy L Thompson           }
19662b730f8bSJeremy L Thompson         }
19672b730f8bSJeremy L Thompson       }
19689d007619Sjeremylt     }
19699d007619Sjeremylt   }
19709d007619Sjeremylt   *grad = basis->grad;
1971e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
19729d007619Sjeremylt }
19739d007619Sjeremylt 
19749d007619Sjeremylt /**
1975*ca94c3ddSJeremy L Thompson   @brief Get 1D gradient matrix of a tensor product `CeedBasis`
19769d007619Sjeremylt 
1977*ca94c3ddSJeremy L Thompson   @param[in]  basis   `CeedBasis`
1978d1d35e2fSjeremylt   @param[out] grad_1d Variable to store gradient matrix
19799d007619Sjeremylt 
19809d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
19819d007619Sjeremylt 
1982b7c9bbdaSJeremy L Thompson   @ref Advanced
19839d007619Sjeremylt **/
1984d1d35e2fSjeremylt int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) {
1985*ca94c3ddSJeremy L Thompson   CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis");
1986d1d35e2fSjeremylt   *grad_1d = basis->grad_1d;
1987e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
19889d007619Sjeremylt }
19899d007619Sjeremylt 
19909d007619Sjeremylt /**
1991*ca94c3ddSJeremy L Thompson   @brief Get divergence matrix of a `CeedBasis`
199250c301a5SRezgar Shakeri 
1993*ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
199450c301a5SRezgar Shakeri   @param[out] div   Variable to store divergence matrix
199550c301a5SRezgar Shakeri 
199650c301a5SRezgar Shakeri   @return An error code: 0 - success, otherwise - failure
199750c301a5SRezgar Shakeri 
199850c301a5SRezgar Shakeri   @ref Advanced
199950c301a5SRezgar Shakeri **/
200050c301a5SRezgar Shakeri int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) {
2001*ca94c3ddSJeremy L Thompson   CeedCheck(basis->div, basis->ceed, CEED_ERROR_MINOR, "CeedBasis does not have divergence matrix");
200250c301a5SRezgar Shakeri   *div = basis->div;
200350c301a5SRezgar Shakeri   return CEED_ERROR_SUCCESS;
200450c301a5SRezgar Shakeri }
200550c301a5SRezgar Shakeri 
200650c301a5SRezgar Shakeri /**
2007*ca94c3ddSJeremy L Thompson   @brief Get curl matrix of a `CeedBasis`
2008c4e3f59bSSebastian Grimberg 
2009*ca94c3ddSJeremy L Thompson   @param[in]  basis `CeedBasis`
2010c4e3f59bSSebastian Grimberg   @param[out] curl  Variable to store curl matrix
2011c4e3f59bSSebastian Grimberg 
2012c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
2013c4e3f59bSSebastian Grimberg 
2014c4e3f59bSSebastian Grimberg   @ref Advanced
2015c4e3f59bSSebastian Grimberg **/
2016c4e3f59bSSebastian Grimberg int CeedBasisGetCurl(CeedBasis basis, const CeedScalar **curl) {
2017*ca94c3ddSJeremy L Thompson   CeedCheck(basis->curl, basis->ceed, CEED_ERROR_MINOR, "CeedBasis does not have curl matrix");
2018c4e3f59bSSebastian Grimberg   *curl = basis->curl;
2019c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
2020c4e3f59bSSebastian Grimberg }
2021c4e3f59bSSebastian Grimberg 
2022c4e3f59bSSebastian Grimberg /**
2023*ca94c3ddSJeremy L Thompson   @brief Destroy a @ref  CeedBasis
20247a982d89SJeremy L. Thompson 
2025*ca94c3ddSJeremy L Thompson   @param[in,out] basis `CeedBasis` to destroy
20267a982d89SJeremy L. Thompson 
20277a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
20287a982d89SJeremy L. Thompson 
20297a982d89SJeremy L. Thompson   @ref User
20307a982d89SJeremy L. Thompson **/
20317a982d89SJeremy L. Thompson int CeedBasisDestroy(CeedBasis *basis) {
2032356036faSJeremy L Thompson   if (!*basis || *basis == CEED_BASIS_NONE || --(*basis)->ref_count > 0) {
2033ad6481ceSJeremy L Thompson     *basis = NULL;
2034ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2035ad6481ceSJeremy L Thompson   }
20362b730f8bSJeremy L Thompson   if ((*basis)->Destroy) CeedCall((*basis)->Destroy(*basis));
20379831d45aSJeremy L Thompson   CeedCall(CeedTensorContractDestroy(&(*basis)->contract));
2038c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->q_ref_1d));
2039c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->q_weight_1d));
20402b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->interp));
20412b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->interp_1d));
20422b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->grad));
20432b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->grad_1d));
2044c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->div));
2045c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->curl));
2046c8c3fa7dSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*basis)->vec_chebyshev));
2047c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisDestroy(&(*basis)->basis_chebyshev));
20482b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*basis)->ceed));
20492b730f8bSJeremy L Thompson   CeedCall(CeedFree(basis));
2050e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
20517a982d89SJeremy L. Thompson }
20527a982d89SJeremy L. Thompson 
20537a982d89SJeremy L. Thompson /**
2054b11c1e72Sjeremylt   @brief Construct a Gauss-Legendre quadrature
2055b11c1e72Sjeremylt 
2056*ca94c3ddSJeremy L Thompson   @param[in]  Q           Number of quadrature points (integrates polynomials of degree `2*Q-1` exactly)
2057*ca94c3ddSJeremy L Thompson   @param[out] q_ref_1d    Array of length `Q` to hold the abscissa on `[-1, 1]`
2058*ca94c3ddSJeremy L Thompson   @param[out] q_weight_1d Array of length `Q` to hold the weights
2059b11c1e72Sjeremylt 
2060b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2061dfdf5a53Sjeremylt 
2062dfdf5a53Sjeremylt   @ref Utility
2063b11c1e72Sjeremylt **/
20642b730f8bSJeremy L Thompson int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) {
2065d7b241e6Sjeremylt   CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0 * atan(1.0);
20661c66c397SJeremy L Thompson 
2067d1d35e2fSjeremylt   // Build q_ref_1d, q_weight_1d
206892ae7e47SJeremy L Thompson   for (CeedInt i = 0; i <= Q / 2; i++) {
2069d7b241e6Sjeremylt     // Guess
2070d7b241e6Sjeremylt     xi = cos(PI * (CeedScalar)(2 * i + 1) / ((CeedScalar)(2 * Q)));
2071d7b241e6Sjeremylt     // Pn(xi)
2072d7b241e6Sjeremylt     P0 = 1.0;
2073d7b241e6Sjeremylt     P1 = xi;
2074d7b241e6Sjeremylt     P2 = 0.0;
207592ae7e47SJeremy L Thompson     for (CeedInt j = 2; j <= Q; j++) {
2076d7b241e6Sjeremylt       P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2077d7b241e6Sjeremylt       P0 = P1;
2078d7b241e6Sjeremylt       P1 = P2;
2079d7b241e6Sjeremylt     }
2080d7b241e6Sjeremylt     // First Newton Step
2081d7b241e6Sjeremylt     dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2082d7b241e6Sjeremylt     xi  = xi - P2 / dP2;
2083d7b241e6Sjeremylt     // Newton to convergence
208492ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < 100 && fabs(P2) > 10 * CEED_EPSILON; k++) {
2085d7b241e6Sjeremylt       P0 = 1.0;
2086d7b241e6Sjeremylt       P1 = xi;
208792ae7e47SJeremy L Thompson       for (CeedInt j = 2; j <= Q; j++) {
2088d7b241e6Sjeremylt         P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2089d7b241e6Sjeremylt         P0 = P1;
2090d7b241e6Sjeremylt         P1 = P2;
2091d7b241e6Sjeremylt       }
2092d7b241e6Sjeremylt       dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2093d7b241e6Sjeremylt       xi  = xi - P2 / dP2;
2094d7b241e6Sjeremylt     }
2095d7b241e6Sjeremylt     // Save xi, wi
2096d7b241e6Sjeremylt     wi                     = 2.0 / ((1.0 - xi * xi) * dP2 * dP2);
2097d1d35e2fSjeremylt     q_weight_1d[i]         = wi;
2098d1d35e2fSjeremylt     q_weight_1d[Q - 1 - i] = wi;
2099d1d35e2fSjeremylt     q_ref_1d[i]            = -xi;
2100d1d35e2fSjeremylt     q_ref_1d[Q - 1 - i]    = xi;
2101d7b241e6Sjeremylt   }
2102e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2103d7b241e6Sjeremylt }
2104d7b241e6Sjeremylt 
2105b11c1e72Sjeremylt /**
2106b11c1e72Sjeremylt   @brief Construct a Gauss-Legendre-Lobatto quadrature
2107b11c1e72Sjeremylt 
2108*ca94c3ddSJeremy L Thompson   @param[in]  Q           Number of quadrature points (integrates polynomials of degree `2*Q-3` exactly)
2109*ca94c3ddSJeremy L Thompson   @param[out] q_ref_1d    Array of length `Q` to hold the abscissa on `[-1, 1]`
2110*ca94c3ddSJeremy L Thompson   @param[out] q_weight_1d Array of length `Q` to hold the weights
2111b11c1e72Sjeremylt 
2112b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2113dfdf5a53Sjeremylt 
2114dfdf5a53Sjeremylt   @ref Utility
2115b11c1e72Sjeremylt **/
21162b730f8bSJeremy L Thompson int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) {
2117d7b241e6Sjeremylt   CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0 * atan(1.0);
21181c66c397SJeremy L Thompson 
2119d1d35e2fSjeremylt   // Build q_ref_1d, q_weight_1d
2120d7b241e6Sjeremylt   // Set endpoints
21216574a04fSJeremy L Thompson   CeedCheck(Q > 1, NULL, CEED_ERROR_DIMENSION, "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q);
2122d7b241e6Sjeremylt   wi = 2.0 / ((CeedScalar)(Q * (Q - 1)));
2123d1d35e2fSjeremylt   if (q_weight_1d) {
2124d1d35e2fSjeremylt     q_weight_1d[0]     = wi;
2125d1d35e2fSjeremylt     q_weight_1d[Q - 1] = wi;
2126d7b241e6Sjeremylt   }
2127d1d35e2fSjeremylt   q_ref_1d[0]     = -1.0;
2128d1d35e2fSjeremylt   q_ref_1d[Q - 1] = 1.0;
2129d7b241e6Sjeremylt   // Interior
213092ae7e47SJeremy L Thompson   for (CeedInt i = 1; i <= (Q - 1) / 2; i++) {
2131d7b241e6Sjeremylt     // Guess
2132d7b241e6Sjeremylt     xi = cos(PI * (CeedScalar)(i) / (CeedScalar)(Q - 1));
2133d7b241e6Sjeremylt     // Pn(xi)
2134d7b241e6Sjeremylt     P0 = 1.0;
2135d7b241e6Sjeremylt     P1 = xi;
2136d7b241e6Sjeremylt     P2 = 0.0;
213792ae7e47SJeremy L Thompson     for (CeedInt j = 2; j < Q; j++) {
2138d7b241e6Sjeremylt       P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2139d7b241e6Sjeremylt       P0 = P1;
2140d7b241e6Sjeremylt       P1 = P2;
2141d7b241e6Sjeremylt     }
2142d7b241e6Sjeremylt     // First Newton step
2143d7b241e6Sjeremylt     dP2  = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2144d7b241e6Sjeremylt     d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi);
2145d7b241e6Sjeremylt     xi   = xi - dP2 / d2P2;
2146d7b241e6Sjeremylt     // Newton to convergence
214792ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < 100 && fabs(dP2) > 10 * CEED_EPSILON; k++) {
2148d7b241e6Sjeremylt       P0 = 1.0;
2149d7b241e6Sjeremylt       P1 = xi;
215092ae7e47SJeremy L Thompson       for (CeedInt j = 2; j < Q; j++) {
2151d7b241e6Sjeremylt         P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2152d7b241e6Sjeremylt         P0 = P1;
2153d7b241e6Sjeremylt         P1 = P2;
2154d7b241e6Sjeremylt       }
2155d7b241e6Sjeremylt       dP2  = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2156d7b241e6Sjeremylt       d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi);
2157d7b241e6Sjeremylt       xi   = xi - dP2 / d2P2;
2158d7b241e6Sjeremylt     }
2159d7b241e6Sjeremylt     // Save xi, wi
2160d7b241e6Sjeremylt     wi = 2.0 / (((CeedScalar)(Q * (Q - 1))) * P2 * P2);
2161d1d35e2fSjeremylt     if (q_weight_1d) {
2162d1d35e2fSjeremylt       q_weight_1d[i]         = wi;
2163d1d35e2fSjeremylt       q_weight_1d[Q - 1 - i] = wi;
2164d7b241e6Sjeremylt     }
2165d1d35e2fSjeremylt     q_ref_1d[i]         = -xi;
2166d1d35e2fSjeremylt     q_ref_1d[Q - 1 - i] = xi;
2167d7b241e6Sjeremylt   }
2168e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2169d7b241e6Sjeremylt }
2170d7b241e6Sjeremylt 
2171d7b241e6Sjeremylt /// @}
2172