xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-basis.c (revision 71a83b8886acabf7fcbee1aa6fceb0f5afc97717)
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
20783c99b3SValeria Barra static struct CeedBasis_private ceed_basis_collocated;
21d7b241e6Sjeremylt /// @endcond
22d7b241e6Sjeremylt 
237a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser
247a982d89SJeremy L. Thompson /// @{
257a982d89SJeremy L. Thompson 
267a982d89SJeremy L. Thompson /// Indicate that the quadrature points are collocated with the nodes
277a982d89SJeremy L. Thompson const CeedBasis CEED_BASIS_COLLOCATED = &ceed_basis_collocated;
287a982d89SJeremy L. Thompson 
297a982d89SJeremy L. Thompson /// @}
307a982d89SJeremy L. Thompson 
317a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
327a982d89SJeremy L. Thompson /// CeedBasis Library Internal Functions
337a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
347a982d89SJeremy L. Thompson /// @addtogroup CeedBasisDeveloper
357a982d89SJeremy L. Thompson /// @{
367a982d89SJeremy L. Thompson 
377a982d89SJeremy L. Thompson /**
383778dbaaSJeremy L Thompson   @brief Compute Chebyshev polynomial values at a point
393778dbaaSJeremy L Thompson 
403778dbaaSJeremy L Thompson   @param[in]  x           Coordinate to evaluate Chebyshev polynomials at
413778dbaaSJeremy L Thompson   @param[in]  n           Number of Chebyshev polynomials to evaluate, n >= 2
423778dbaaSJeremy L Thompson   @param[out] chebyshev_x Array of Chebyshev polynomial values
433778dbaaSJeremy L Thompson 
443778dbaaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
453778dbaaSJeremy L Thompson 
463778dbaaSJeremy L Thompson   @ref Developer
473778dbaaSJeremy L Thompson **/
483778dbaaSJeremy L Thompson static int CeedChebyshevPolynomialsAtPoint(CeedScalar x, CeedInt n, CeedScalar *chebyshev_x) {
493778dbaaSJeremy L Thompson   chebyshev_x[0] = 1.0;
503778dbaaSJeremy L Thompson   chebyshev_x[1] = 2 * x;
513778dbaaSJeremy L Thompson   for (CeedInt i = 2; i < n; i++) chebyshev_x[i] = 2 * x * chebyshev_x[i - 1] - chebyshev_x[i - 2];
523778dbaaSJeremy L Thompson 
533778dbaaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
543778dbaaSJeremy L Thompson }
553778dbaaSJeremy L Thompson 
563778dbaaSJeremy L Thompson /**
573778dbaaSJeremy L Thompson   @brief Compute values of the derivative of Chebyshev polynomials at a point
583778dbaaSJeremy L Thompson 
593778dbaaSJeremy L Thompson   @param[in]  x           Coordinate to evaluate derivative of Chebyshev polynomials at
603778dbaaSJeremy L Thompson   @param[in]  n           Number of Chebyshev polynomials to evaluate, n >= 2
613778dbaaSJeremy L Thompson   @param[out] chebyshev_x Array of Chebyshev polynomial derivative values
623778dbaaSJeremy L Thompson 
633778dbaaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
643778dbaaSJeremy L Thompson 
653778dbaaSJeremy L Thompson   @ref Developer
663778dbaaSJeremy L Thompson **/
673778dbaaSJeremy L Thompson static int CeedChebyshevDerivativeAtPoint(CeedScalar x, CeedInt n, CeedScalar *chebyshev_dx) {
683778dbaaSJeremy L Thompson   CeedScalar chebyshev_x[3];
693778dbaaSJeremy L Thompson 
703778dbaaSJeremy L Thompson   chebyshev_x[1]  = 1.0;
713778dbaaSJeremy L Thompson   chebyshev_x[2]  = 2 * x;
723778dbaaSJeremy L Thompson   chebyshev_dx[0] = 0.0;
733778dbaaSJeremy L Thompson   chebyshev_dx[1] = 2.0;
743778dbaaSJeremy L Thompson   for (CeedInt i = 2; i < n; i++) {
753778dbaaSJeremy L Thompson     chebyshev_x[0]  = chebyshev_x[1];
763778dbaaSJeremy L Thompson     chebyshev_x[1]  = chebyshev_x[2];
773778dbaaSJeremy L Thompson     chebyshev_x[2]  = 2 * x * chebyshev_x[1] - chebyshev_x[0];
783778dbaaSJeremy L Thompson     chebyshev_dx[i] = 2 * x * chebyshev_dx[i - 1] + 2 * chebyshev_x[1] - chebyshev_dx[i - 2];
793778dbaaSJeremy L Thompson   }
803778dbaaSJeremy L Thompson 
813778dbaaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
823778dbaaSJeremy L Thompson }
833778dbaaSJeremy L Thompson 
843778dbaaSJeremy L Thompson /**
857a982d89SJeremy L. Thompson   @brief Compute Householder reflection
867a982d89SJeremy L. Thompson 
87ea61e9acSJeremy L Thompson   Computes A = (I - b v v^T) A, where A is an mxn matrix indexed as A[i*row + j*col]
887a982d89SJeremy L. Thompson 
897a982d89SJeremy L. Thompson   @param[in,out] A   Matrix to apply Householder reflection to, in place
90ea61e9acSJeremy L Thompson   @param[in]     v   Householder vector
91ea61e9acSJeremy L Thompson   @param[in]     b   Scaling factor
92ea61e9acSJeremy L Thompson   @param[in]     m   Number of rows in A
93ea61e9acSJeremy L Thompson   @param[in]     n   Number of columns in A
94ea61e9acSJeremy L Thompson   @param[in]     row Row stride
95ea61e9acSJeremy L Thompson   @param[in]     col Col stride
967a982d89SJeremy L. Thompson 
977a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
987a982d89SJeremy L. Thompson 
997a982d89SJeremy L. Thompson   @ref Developer
1007a982d89SJeremy L. Thompson **/
1012b730f8bSJeremy L Thompson static int CeedHouseholderReflect(CeedScalar *A, const CeedScalar *v, CeedScalar b, CeedInt m, CeedInt n, CeedInt row, CeedInt col) {
1027a982d89SJeremy L. Thompson   for (CeedInt j = 0; j < n; j++) {
1037a982d89SJeremy L. Thompson     CeedScalar w = A[0 * row + j * col];
1042b730f8bSJeremy L Thompson     for (CeedInt i = 1; i < m; i++) w += v[i] * A[i * row + j * col];
1057a982d89SJeremy L. Thompson     A[0 * row + j * col] -= b * w;
1062b730f8bSJeremy L Thompson     for (CeedInt i = 1; i < m; i++) A[i * row + j * col] -= b * w * v[i];
1077a982d89SJeremy L. Thompson   }
108e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1097a982d89SJeremy L. Thompson }
1107a982d89SJeremy L. Thompson 
1117a982d89SJeremy L. Thompson /**
1127a982d89SJeremy L. Thompson   @brief Compute Givens rotation
1137a982d89SJeremy L. Thompson 
114ea61e9acSJeremy L Thompson   Computes A = G A (or G^T A in transpose mode), where A is an mxn matrix indexed as A[i*n + j*m]
1157a982d89SJeremy L. Thompson 
1167a982d89SJeremy L. Thompson   @param[in,out] A      Row major matrix to apply Givens rotation to, in place
117ea61e9acSJeremy L Thompson   @param[in]     c      Cosine factor
118ea61e9acSJeremy L Thompson   @param[in]     s      Sine factor
119ea61e9acSJeremy 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;
1204cc79fe7SJed Brown                           @ref CEED_TRANSPOSE for the opposite rotation
121ea61e9acSJeremy L Thompson   @param[in]     i      First row/column to apply rotation
122ea61e9acSJeremy L Thompson   @param[in]     k      Second row/column to apply rotation
123ea61e9acSJeremy L Thompson   @param[in]     m      Number of rows in A
124ea61e9acSJeremy L Thompson   @param[in]     n      Number of columns in A
1257a982d89SJeremy L. Thompson 
1267a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1277a982d89SJeremy L. Thompson 
1287a982d89SJeremy L. Thompson   @ref Developer
1297a982d89SJeremy L. Thompson **/
1302b730f8bSJeremy L Thompson static int CeedGivensRotation(CeedScalar *A, CeedScalar c, CeedScalar s, CeedTransposeMode t_mode, CeedInt i, CeedInt k, CeedInt m, CeedInt n) {
131d1d35e2fSjeremylt   CeedInt stride_j = 1, stride_ik = m, num_its = n;
132d1d35e2fSjeremylt   if (t_mode == CEED_NOTRANSPOSE) {
1332b730f8bSJeremy L Thompson     stride_j  = n;
1342b730f8bSJeremy L Thompson     stride_ik = 1;
1352b730f8bSJeremy L Thompson     num_its   = m;
1367a982d89SJeremy L. Thompson   }
1377a982d89SJeremy L. Thompson 
1387a982d89SJeremy L. Thompson   // Apply rotation
139d1d35e2fSjeremylt   for (CeedInt j = 0; j < num_its; j++) {
140d1d35e2fSjeremylt     CeedScalar tau1 = A[i * stride_ik + j * stride_j], tau2 = A[k * stride_ik + j * stride_j];
141d1d35e2fSjeremylt     A[i * stride_ik + j * stride_j] = c * tau1 - s * tau2;
142d1d35e2fSjeremylt     A[k * stride_ik + j * stride_j] = s * tau1 + c * tau2;
1437a982d89SJeremy L. Thompson   }
144e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1457a982d89SJeremy L. Thompson }
1467a982d89SJeremy L. Thompson 
1477a982d89SJeremy L. Thompson /**
1487a982d89SJeremy L. Thompson   @brief View an array stored in a CeedBasis
1497a982d89SJeremy L. Thompson 
1500a0da059Sjeremylt   @param[in] name   Name of array
151d1d35e2fSjeremylt   @param[in] fp_fmt Printing format
1520a0da059Sjeremylt   @param[in] m      Number of rows in array
1530a0da059Sjeremylt   @param[in] n      Number of columns in array
1540a0da059Sjeremylt   @param[in] a      Array to be viewed
1550a0da059Sjeremylt   @param[in] stream Stream to view to, e.g., stdout
1567a982d89SJeremy L. Thompson 
1577a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1587a982d89SJeremy L. Thompson 
1597a982d89SJeremy L. Thompson   @ref Developer
1607a982d89SJeremy L. Thompson **/
1612b730f8bSJeremy L Thompson static int CeedScalarView(const char *name, const char *fp_fmt, CeedInt m, CeedInt n, const CeedScalar *a, FILE *stream) {
162edf04919SJeremy L Thompson   if (m > 1) {
163edf04919SJeremy L Thompson     fprintf(stream, "  %s:\n", name);
164edf04919SJeremy L Thompson   } else {
165edf04919SJeremy L Thompson     char padded_name[12];
166edf04919SJeremy L Thompson 
167edf04919SJeremy L Thompson     snprintf(padded_name, 11, "%s:", name);
168edf04919SJeremy L Thompson     fprintf(stream, "  %-10s", padded_name);
169edf04919SJeremy L Thompson   }
17092ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < m; i++) {
171edf04919SJeremy L Thompson     if (m > 1) fprintf(stream, "    [%" CeedInt_FMT "]", i);
1722b730f8bSJeremy 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);
1737a982d89SJeremy L. Thompson     fputs("\n", stream);
1747a982d89SJeremy L. Thompson   }
175e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1767a982d89SJeremy L. Thompson }
1777a982d89SJeremy L. Thompson 
178a76a04e7SJeremy L Thompson /**
179ea61e9acSJeremy L Thompson   @brief Create the interpolation and gradient matrices for projection from the nodes of `basis_from` to the nodes of `basis_to`.
180ba59ac12SSebastian Grimberg 
18115ad3917SSebastian Grimberg   The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization.
18215ad3917SSebastian Grimberg   The gradient is given by `grad_project = interp_to^+ * grad_from`, and is only computed for H^1 spaces otherwise it should not be used.
18315ad3917SSebastian Grimberg 
184ba59ac12SSebastian Grimberg   Note: `basis_from` and `basis_to` must have compatible quadrature spaces.
185a76a04e7SJeremy L Thompson 
186a76a04e7SJeremy L Thompson   @param[in]  basis_from     CeedBasis to project from
187a76a04e7SJeremy L Thompson   @param[in]  basis_to       CeedBasis to project to
188ea61e9acSJeremy L Thompson   @param[out] interp_project Address of the variable where the newly created interpolation matrix will be stored.
189ea61e9acSJeremy L Thompson   @param[out] grad_project   Address of the variable where the newly created gradient matrix will be stored.
190a76a04e7SJeremy L Thompson 
191a76a04e7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
192a76a04e7SJeremy L Thompson 
193a76a04e7SJeremy L Thompson   @ref Developer
194a76a04e7SJeremy L Thompson **/
1952b730f8bSJeremy L Thompson static int CeedBasisCreateProjectionMatrices(CeedBasis basis_from, CeedBasis basis_to, CeedScalar **interp_project, CeedScalar **grad_project) {
196a76a04e7SJeremy L Thompson   Ceed ceed;
1972b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis_to, &ceed));
198a76a04e7SJeremy L Thompson 
199a76a04e7SJeremy L Thompson   // Check for compatible quadrature spaces
200a76a04e7SJeremy L Thompson   CeedInt Q_to, Q_from;
2012b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_to, &Q_to));
2022b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_from, &Q_from));
2036574a04fSJeremy L Thompson   CeedCheck(Q_to == Q_from, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
204a76a04e7SJeremy L Thompson 
20514556e63SJeremy L Thompson   // Check for matching tensor or non-tensor
206a76a04e7SJeremy L Thompson   CeedInt P_to, P_from, Q = Q_to;
207a76a04e7SJeremy L Thompson   bool    is_tensor_to, is_tensor_from;
2082b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to));
2092b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from));
2106574a04fSJeremy L Thompson   CeedCheck(is_tensor_to == is_tensor_from, ceed, CEED_ERROR_MINOR, "Bases must both be tensor or non-tensor");
2116574a04fSJeremy L Thompson   if (is_tensor_to) {
2122b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_to));
2132b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_from));
2142b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints1D(basis_from, &Q));
2156574a04fSJeremy L Thompson   } else {
2162b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_to, &P_to));
2172b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_from, &P_from));
218a76a04e7SJeremy L Thompson   }
219a76a04e7SJeremy L Thompson 
22015ad3917SSebastian Grimberg   // Check for matching FE space
22115ad3917SSebastian Grimberg   CeedFESpace fe_space_to, fe_space_from;
22215ad3917SSebastian Grimberg   CeedCall(CeedBasisGetFESpace(basis_to, &fe_space_to));
22315ad3917SSebastian Grimberg   CeedCall(CeedBasisGetFESpace(basis_from, &fe_space_from));
2246574a04fSJeremy L Thompson   CeedCheck(fe_space_to == fe_space_from, ceed, CEED_ERROR_MINOR, "Bases must both be the same FE space type");
22515ad3917SSebastian Grimberg 
22614556e63SJeremy L Thompson   // Get source matrices
22715ad3917SSebastian Grimberg   CeedInt           dim, q_comp = 1;
22815ad3917SSebastian Grimberg   const CeedScalar *interp_to_source = NULL, *interp_from_source = NULL;
22914556e63SJeremy L Thompson   CeedScalar       *interp_to, *interp_from, *tau;
2302b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis_to, &dim));
231a76a04e7SJeremy L Thompson   if (is_tensor_to) {
2322b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis_to, &interp_to_source));
2332b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis_from, &interp_from_source));
234a76a04e7SJeremy L Thompson   } else {
23515ad3917SSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis_from, CEED_EVAL_INTERP, &q_comp));
2362b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(basis_to, &interp_to_source));
2372b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(basis_from, &interp_from_source));
23815ad3917SSebastian Grimberg   }
23915ad3917SSebastian Grimberg   CeedCall(CeedMalloc(Q * P_from * q_comp, &interp_from));
24015ad3917SSebastian Grimberg   CeedCall(CeedMalloc(Q * P_to * q_comp, &interp_to));
24115ad3917SSebastian Grimberg   CeedCall(CeedCalloc(P_to * P_from, interp_project));
24215ad3917SSebastian Grimberg   CeedCall(CeedMalloc(Q * q_comp, &tau));
24315ad3917SSebastian Grimberg 
24415ad3917SSebastian Grimberg   // `grad_project = interp_to^+ * grad_from` is computed for the H^1 space case so the
245de05fbb2SSebastian Grimberg   // projection basis will have a gradient operation (allocated even if not H^1 for the
246de05fbb2SSebastian Grimberg   // basis construction later on)
24715ad3917SSebastian Grimberg   const CeedScalar *grad_from_source = NULL;
24815ad3917SSebastian Grimberg   if (fe_space_to == CEED_FE_SPACE_H1) {
24915ad3917SSebastian Grimberg     if (is_tensor_to) {
25015ad3917SSebastian Grimberg       CeedCall(CeedBasisGetGrad1D(basis_from, &grad_from_source));
25115ad3917SSebastian Grimberg     } else {
2522b730f8bSJeremy L Thompson       CeedCall(CeedBasisGetGrad(basis_from, &grad_from_source));
253a76a04e7SJeremy L Thompson     }
254de05fbb2SSebastian Grimberg   }
25515ad3917SSebastian Grimberg   CeedCall(CeedCalloc(P_to * P_from * (is_tensor_to ? 1 : dim), grad_project));
25615ad3917SSebastian Grimberg 
25715ad3917SSebastian Grimberg   // QR Factorization, interp_to = Q R
25815ad3917SSebastian Grimberg   memcpy(interp_to, interp_to_source, Q * P_to * q_comp * sizeof(interp_to_source[0]));
25915ad3917SSebastian Grimberg   CeedCall(CeedQRFactorization(ceed, interp_to, tau, Q * q_comp, P_to));
260a76a04e7SJeremy L Thompson 
26114556e63SJeremy L Thompson   // Build matrices
26215ad3917SSebastian Grimberg   CeedInt     num_matrices = 1 + (fe_space_to == CEED_FE_SPACE_H1) * (is_tensor_to ? 1 : dim);
26314556e63SJeremy L Thompson   CeedScalar *input_from[num_matrices], *output_project[num_matrices];
26414556e63SJeremy L Thompson   input_from[0]     = (CeedScalar *)interp_from_source;
26514556e63SJeremy L Thompson   output_project[0] = *interp_project;
26614556e63SJeremy L Thompson   for (CeedInt m = 1; m < num_matrices; m++) {
26714556e63SJeremy L Thompson     input_from[m]     = (CeedScalar *)&grad_from_source[(m - 1) * Q * P_from];
26802af4036SJeremy L Thompson     output_project[m] = &((*grad_project)[(m - 1) * P_to * P_from]);
26914556e63SJeremy L Thompson   }
27014556e63SJeremy L Thompson   for (CeedInt m = 0; m < num_matrices; m++) {
27115ad3917SSebastian Grimberg     // Apply Q^T, interp_from = Q^T interp_from
27215ad3917SSebastian Grimberg     memcpy(interp_from, input_from[m], Q * P_from * q_comp * sizeof(input_from[m][0]));
27315ad3917SSebastian Grimberg     CeedCall(CeedHouseholderApplyQ(interp_from, interp_to, tau, CEED_TRANSPOSE, Q * q_comp, P_from, P_to, P_from, 1));
274a76a04e7SJeremy L Thompson 
27515ad3917SSebastian Grimberg     // Apply Rinv, output_project = Rinv interp_from
276a76a04e7SJeremy L Thompson     for (CeedInt j = 0; j < P_from; j++) {  // Column j
2772b730f8bSJeremy 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];
278a76a04e7SJeremy L Thompson       for (CeedInt i = P_to - 2; i >= 0; i--) {  // Row i
27914556e63SJeremy L Thompson         output_project[m][j + P_from * i] = interp_from[j + P_from * i];
280a76a04e7SJeremy L Thompson         for (CeedInt k = i + 1; k < P_to; k++) {
2812b730f8bSJeremy L Thompson           output_project[m][j + P_from * i] -= interp_to[k + P_to * i] * output_project[m][j + P_from * k];
282a76a04e7SJeremy L Thompson         }
28314556e63SJeremy L Thompson         output_project[m][j + P_from * i] /= interp_to[i + P_to * i];
284a76a04e7SJeremy L Thompson       }
285a76a04e7SJeremy L Thompson     }
28614556e63SJeremy L Thompson   }
28714556e63SJeremy L Thompson 
28814556e63SJeremy L Thompson   // Cleanup
2892b730f8bSJeremy L Thompson   CeedCall(CeedFree(&tau));
2902b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_to));
2912b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_from));
292a76a04e7SJeremy L Thompson 
293a76a04e7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
294a76a04e7SJeremy L Thompson }
295a76a04e7SJeremy L Thompson 
2967a982d89SJeremy L. Thompson /// @}
2977a982d89SJeremy L. Thompson 
2987a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
2997a982d89SJeremy L. Thompson /// Ceed Backend API
3007a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3017a982d89SJeremy L. Thompson /// @addtogroup CeedBasisBackend
3027a982d89SJeremy L. Thompson /// @{
3037a982d89SJeremy L. Thompson 
3047a982d89SJeremy L. Thompson /**
3057a982d89SJeremy L. Thompson   @brief Return collocated grad matrix
3067a982d89SJeremy L. Thompson 
307ea61e9acSJeremy L Thompson   @param[in]  basis         CeedBasis
308ea61e9acSJeremy L Thompson   @param[out] collo_grad_1d Row-major (Q_1d * Q_1d) matrix expressing derivatives of basis functions at quadrature points
3097a982d89SJeremy L. Thompson 
3107a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3117a982d89SJeremy L. Thompson 
3127a982d89SJeremy L. Thompson   @ref Backend
3137a982d89SJeremy L. Thompson **/
314d1d35e2fSjeremylt int CeedBasisGetCollocatedGrad(CeedBasis basis, CeedScalar *collo_grad_1d) {
3157a982d89SJeremy L. Thompson   Ceed        ceed;
3162b730f8bSJeremy L Thompson   CeedInt     P_1d = (basis)->P_1d, Q_1d = (basis)->Q_1d;
31778464608Sjeremylt   CeedScalar *interp_1d, *grad_1d, *tau;
3187a982d89SJeremy L. Thompson 
3192b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q_1d * P_1d, &interp_1d));
3202b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q_1d * P_1d, &grad_1d));
3212b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q_1d, &tau));
322d1d35e2fSjeremylt   memcpy(interp_1d, (basis)->interp_1d, Q_1d * P_1d * sizeof(basis)->interp_1d[0]);
323d1d35e2fSjeremylt   memcpy(grad_1d, (basis)->grad_1d, Q_1d * P_1d * sizeof(basis)->interp_1d[0]);
3247a982d89SJeremy L. Thompson 
325d1d35e2fSjeremylt   // QR Factorization, interp_1d = Q R
3262b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis, &ceed));
3272b730f8bSJeremy L Thompson   CeedCall(CeedQRFactorization(ceed, interp_1d, tau, Q_1d, P_1d));
328ea61e9acSJeremy 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.
3297a982d89SJeremy L. Thompson 
330c8c3fa7dSJeremy L Thompson   // Apply R_inv, collo_grad_1d = grad_1d R_inv
331c8c3fa7dSJeremy L Thompson   for (CeedInt i = 0; i < Q_1d; i++) {  // Row i
332d1d35e2fSjeremylt     collo_grad_1d[Q_1d * i] = grad_1d[P_1d * i] / interp_1d[0];
333c8c3fa7dSJeremy L Thompson     for (CeedInt j = 1; j < P_1d; j++) {  // Column j
334d1d35e2fSjeremylt       collo_grad_1d[j + Q_1d * i] = grad_1d[j + P_1d * i];
335c8c3fa7dSJeremy 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];
336d1d35e2fSjeremylt       collo_grad_1d[j + Q_1d * i] /= interp_1d[j + P_1d * j];
3377a982d89SJeremy L. Thompson     }
338c8c3fa7dSJeremy L Thompson     for (CeedInt j = P_1d; j < Q_1d; j++) collo_grad_1d[j + Q_1d * i] = 0;
3397a982d89SJeremy L. Thompson   }
3407a982d89SJeremy L. Thompson 
34115ad3917SSebastian Grimberg   // Apply Q^T, collo_grad_1d = collo_grad_1d Q^T
3422b730f8bSJeremy L Thompson   CeedCall(CeedHouseholderApplyQ(collo_grad_1d, interp_1d, tau, CEED_NOTRANSPOSE, Q_1d, Q_1d, P_1d, 1, Q_1d));
3437a982d89SJeremy L. Thompson 
3442b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_1d));
3452b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_1d));
3462b730f8bSJeremy L Thompson   CeedCall(CeedFree(&tau));
347e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3487a982d89SJeremy L. Thompson }
3497a982d89SJeremy L. Thompson 
3507a982d89SJeremy L. Thompson /**
3517a982d89SJeremy L. Thompson   @brief Get tensor status for given CeedBasis
3527a982d89SJeremy L. Thompson 
353ea61e9acSJeremy L Thompson   @param[in]  basis     CeedBasis
354d1d35e2fSjeremylt   @param[out] is_tensor Variable to store tensor status
3557a982d89SJeremy L. Thompson 
3567a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3577a982d89SJeremy L. Thompson 
3587a982d89SJeremy L. Thompson   @ref Backend
3597a982d89SJeremy L. Thompson **/
360d1d35e2fSjeremylt int CeedBasisIsTensor(CeedBasis basis, bool *is_tensor) {
3616402da51SJeremy L Thompson   *is_tensor = basis->is_tensor_basis;
362e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3637a982d89SJeremy L. Thompson }
3647a982d89SJeremy L. Thompson 
3657a982d89SJeremy L. Thompson /**
3667a982d89SJeremy L. Thompson   @brief Get backend data of a CeedBasis
3677a982d89SJeremy L. Thompson 
368ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
3697a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
3707a982d89SJeremy L. Thompson 
3717a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3727a982d89SJeremy L. Thompson 
3737a982d89SJeremy L. Thompson   @ref Backend
3747a982d89SJeremy L. Thompson **/
375777ff853SJeremy L Thompson int CeedBasisGetData(CeedBasis basis, void *data) {
376777ff853SJeremy L Thompson   *(void **)data = basis->data;
377e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3787a982d89SJeremy L. Thompson }
3797a982d89SJeremy L. Thompson 
3807a982d89SJeremy L. Thompson /**
3817a982d89SJeremy L. Thompson   @brief Set backend data of a CeedBasis
3827a982d89SJeremy L. Thompson 
383ea61e9acSJeremy L Thompson   @param[in,out] basis  CeedBasis
384ea61e9acSJeremy L Thompson   @param[in]     data   Data to set
3857a982d89SJeremy L. Thompson 
3867a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3877a982d89SJeremy L. Thompson 
3887a982d89SJeremy L. Thompson   @ref Backend
3897a982d89SJeremy L. Thompson **/
390777ff853SJeremy L Thompson int CeedBasisSetData(CeedBasis basis, void *data) {
391777ff853SJeremy L Thompson   basis->data = data;
392e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3937a982d89SJeremy L. Thompson }
3947a982d89SJeremy L. Thompson 
3957a982d89SJeremy L. Thompson /**
39634359f16Sjeremylt   @brief Increment the reference counter for a CeedBasis
39734359f16Sjeremylt 
398ea61e9acSJeremy L Thompson   @param[in,out] basis Basis to increment the reference counter
39934359f16Sjeremylt 
40034359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
40134359f16Sjeremylt 
40234359f16Sjeremylt   @ref Backend
40334359f16Sjeremylt **/
4049560d06aSjeremylt int CeedBasisReference(CeedBasis basis) {
40534359f16Sjeremylt   basis->ref_count++;
40634359f16Sjeremylt   return CEED_ERROR_SUCCESS;
40734359f16Sjeremylt }
40834359f16Sjeremylt 
40934359f16Sjeremylt /**
410c4e3f59bSSebastian Grimberg   @brief Get number of Q-vector components for given CeedBasis
411c4e3f59bSSebastian Grimberg 
412c4e3f59bSSebastian Grimberg   @param[in]  basis  CeedBasis
413c4e3f59bSSebastian Grimberg   @param[in]  eval_mode \ref CEED_EVAL_INTERP to use interpolated values,
414c4e3f59bSSebastian Grimberg                           \ref CEED_EVAL_GRAD to use gradients,
415c4e3f59bSSebastian Grimberg                           \ref CEED_EVAL_DIV to use divergence,
416c4e3f59bSSebastian Grimberg                           \ref CEED_EVAL_CURL to use curl.
417c4e3f59bSSebastian Grimberg   @param[out] q_comp Variable to store number of Q-vector components of basis
418c4e3f59bSSebastian Grimberg 
419c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
420c4e3f59bSSebastian Grimberg 
421c4e3f59bSSebastian Grimberg   @ref Backend
422c4e3f59bSSebastian Grimberg **/
423c4e3f59bSSebastian Grimberg int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode, CeedInt *q_comp) {
424c4e3f59bSSebastian Grimberg   switch (eval_mode) {
425c4e3f59bSSebastian Grimberg     case CEED_EVAL_INTERP:
426c4e3f59bSSebastian Grimberg       *q_comp = (basis->fe_space == CEED_FE_SPACE_H1) ? 1 : basis->dim;
427c4e3f59bSSebastian Grimberg       break;
428c4e3f59bSSebastian Grimberg     case CEED_EVAL_GRAD:
429c4e3f59bSSebastian Grimberg       *q_comp = basis->dim;
430c4e3f59bSSebastian Grimberg       break;
431c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
432c4e3f59bSSebastian Grimberg       *q_comp = 1;
433c4e3f59bSSebastian Grimberg       break;
434c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
435c4e3f59bSSebastian Grimberg       *q_comp = (basis->dim < 3) ? 1 : basis->dim;
436c4e3f59bSSebastian Grimberg       break;
437c4e3f59bSSebastian Grimberg     case CEED_EVAL_NONE:
438c4e3f59bSSebastian Grimberg     case CEED_EVAL_WEIGHT:
439352a5e7cSSebastian Grimberg       *q_comp = 1;
440c4e3f59bSSebastian Grimberg       break;
441c4e3f59bSSebastian Grimberg   }
442c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
443c4e3f59bSSebastian Grimberg }
444c4e3f59bSSebastian Grimberg 
445c4e3f59bSSebastian Grimberg /**
4466e15d496SJeremy L Thompson   @brief Estimate number of FLOPs required to apply CeedBasis in t_mode and eval_mode
4476e15d496SJeremy L Thompson 
448ea61e9acSJeremy L Thompson   @param[in]  basis     Basis to estimate FLOPs for
449ea61e9acSJeremy L Thompson   @param[in]  t_mode    Apply basis or transpose
450ea61e9acSJeremy L Thompson   @param[in]  eval_mode Basis evaluation mode
451ea61e9acSJeremy L Thompson   @param[out] flops     Address of variable to hold FLOPs estimate
4526e15d496SJeremy L Thompson 
4536e15d496SJeremy L Thompson   @ref Backend
4546e15d496SJeremy L Thompson **/
4552b730f8bSJeremy L Thompson int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedSize *flops) {
4566e15d496SJeremy L Thompson   bool is_tensor;
4576e15d496SJeremy L Thompson 
4582b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor));
4596e15d496SJeremy L Thompson   if (is_tensor) {
4606e15d496SJeremy L Thompson     CeedInt dim, num_comp, P_1d, Q_1d;
4612b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis, &dim));
4622b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
4632b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
4642b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
4656e15d496SJeremy L Thompson     if (t_mode == CEED_TRANSPOSE) {
4662b730f8bSJeremy L Thompson       P_1d = Q_1d;
4672b730f8bSJeremy L Thompson       Q_1d = P_1d;
4686e15d496SJeremy L Thompson     }
4696e15d496SJeremy L Thompson     CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim - 1), post = 1;
4706e15d496SJeremy L Thompson     for (CeedInt d = 0; d < dim; d++) {
4716e15d496SJeremy L Thompson       tensor_flops += 2 * pre * P_1d * post * Q_1d;
4726e15d496SJeremy L Thompson       pre /= P_1d;
4736e15d496SJeremy L Thompson       post *= Q_1d;
4746e15d496SJeremy L Thompson     }
4756e15d496SJeremy L Thompson     switch (eval_mode) {
4762b730f8bSJeremy L Thompson       case CEED_EVAL_NONE:
4772b730f8bSJeremy L Thompson         *flops = 0;
4782b730f8bSJeremy L Thompson         break;
4792b730f8bSJeremy L Thompson       case CEED_EVAL_INTERP:
4802b730f8bSJeremy L Thompson         *flops = tensor_flops;
4812b730f8bSJeremy L Thompson         break;
4822b730f8bSJeremy L Thompson       case CEED_EVAL_GRAD:
4832b730f8bSJeremy L Thompson         *flops = tensor_flops * 2;
4842b730f8bSJeremy L Thompson         break;
4856e15d496SJeremy L Thompson       case CEED_EVAL_DIV:
4866e15d496SJeremy L Thompson       case CEED_EVAL_CURL:
4876574a04fSJeremy L Thompson         // LCOV_EXCL_START
4886574a04fSJeremy L Thompson         return CeedError(basis->ceed, CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported", CeedEvalModes[eval_mode]);
4892b730f8bSJeremy L Thompson         break;
4906e15d496SJeremy L Thompson       // LCOV_EXCL_STOP
4912b730f8bSJeremy L Thompson       case CEED_EVAL_WEIGHT:
4922b730f8bSJeremy L Thompson         *flops = dim * CeedIntPow(Q_1d, dim);
4932b730f8bSJeremy L Thompson         break;
4946e15d496SJeremy L Thompson     }
4956e15d496SJeremy L Thompson   } else {
496c4e3f59bSSebastian Grimberg     CeedInt dim, num_comp, q_comp, num_nodes, num_qpts;
4972b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis, &dim));
4982b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
499c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
5002b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
5012b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
5026e15d496SJeremy L Thompson     switch (eval_mode) {
5032b730f8bSJeremy L Thompson       case CEED_EVAL_NONE:
5042b730f8bSJeremy L Thompson         *flops = 0;
5052b730f8bSJeremy L Thompson         break;
5062b730f8bSJeremy L Thompson       case CEED_EVAL_INTERP:
5072b730f8bSJeremy L Thompson       case CEED_EVAL_GRAD:
5082b730f8bSJeremy L Thompson       case CEED_EVAL_DIV:
5092b730f8bSJeremy L Thompson       case CEED_EVAL_CURL:
510c4e3f59bSSebastian Grimberg         *flops = num_nodes * num_qpts * num_comp * q_comp;
5112b730f8bSJeremy L Thompson         break;
5122b730f8bSJeremy L Thompson       case CEED_EVAL_WEIGHT:
5132b730f8bSJeremy L Thompson         *flops = 0;
5142b730f8bSJeremy L Thompson         break;
5156e15d496SJeremy L Thompson     }
5166e15d496SJeremy L Thompson   }
5176e15d496SJeremy L Thompson 
5186e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5196e15d496SJeremy L Thompson }
5206e15d496SJeremy L Thompson 
5216e15d496SJeremy L Thompson /**
522c4e3f59bSSebastian Grimberg   @brief Get CeedFESpace for a CeedBasis
523c4e3f59bSSebastian Grimberg 
524c4e3f59bSSebastian Grimberg   @param[in]  basis     CeedBasis
525c4e3f59bSSebastian Grimberg   @param[out] fe_space  Variable to store CeedFESpace
526c4e3f59bSSebastian Grimberg 
527c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
528c4e3f59bSSebastian Grimberg 
529c4e3f59bSSebastian Grimberg   @ref Backend
530c4e3f59bSSebastian Grimberg **/
531c4e3f59bSSebastian Grimberg int CeedBasisGetFESpace(CeedBasis basis, CeedFESpace *fe_space) {
532c4e3f59bSSebastian Grimberg   *fe_space = basis->fe_space;
533c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
534c4e3f59bSSebastian Grimberg }
535c4e3f59bSSebastian Grimberg 
536c4e3f59bSSebastian Grimberg /**
5377a982d89SJeremy L. Thompson   @brief Get dimension for given CeedElemTopology
5387a982d89SJeremy L. Thompson 
539ea61e9acSJeremy L Thompson   @param[in]  topo CeedElemTopology
5407a982d89SJeremy L. Thompson   @param[out] dim  Variable to store dimension of topology
5417a982d89SJeremy L. Thompson 
5427a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5437a982d89SJeremy L. Thompson 
5447a982d89SJeremy L. Thompson   @ref Backend
5457a982d89SJeremy L. Thompson **/
5467a982d89SJeremy L. Thompson int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim) {
5477a982d89SJeremy L. Thompson   *dim = (CeedInt)topo >> 16;
548e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5497a982d89SJeremy L. Thompson }
5507a982d89SJeremy L. Thompson 
5517a982d89SJeremy L. Thompson /**
5527a982d89SJeremy L. Thompson   @brief Get CeedTensorContract of a CeedBasis
5537a982d89SJeremy L. Thompson 
554ea61e9acSJeremy L Thompson   @param[in]  basis     CeedBasis
5557a982d89SJeremy L. Thompson   @param[out] contract  Variable to store CeedTensorContract
5567a982d89SJeremy L. Thompson 
5577a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5587a982d89SJeremy L. Thompson 
5597a982d89SJeremy L. Thompson   @ref Backend
5607a982d89SJeremy L. Thompson **/
5617a982d89SJeremy L. Thompson int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract) {
5627a982d89SJeremy L. Thompson   *contract = basis->contract;
563e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5647a982d89SJeremy L. Thompson }
5657a982d89SJeremy L. Thompson 
5667a982d89SJeremy L. Thompson /**
5677a982d89SJeremy L. Thompson   @brief Set CeedTensorContract of a CeedBasis
5687a982d89SJeremy L. Thompson 
569ea61e9acSJeremy L Thompson   @param[in,out] basis    CeedBasis
570ea61e9acSJeremy L Thompson   @param[in]     contract CeedTensorContract to set
5717a982d89SJeremy L. Thompson 
5727a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5737a982d89SJeremy L. Thompson 
5747a982d89SJeremy L. Thompson   @ref Backend
5757a982d89SJeremy L. Thompson **/
57634359f16Sjeremylt int CeedBasisSetTensorContract(CeedBasis basis, CeedTensorContract contract) {
57734359f16Sjeremylt   basis->contract = contract;
5782b730f8bSJeremy L Thompson   CeedCall(CeedTensorContractReference(contract));
579e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5807a982d89SJeremy L. Thompson }
5817a982d89SJeremy L. Thompson 
5827a982d89SJeremy L. Thompson /**
5837a982d89SJeremy L. Thompson   @brief Return a reference implementation of matrix multiplication C = A B.
584ba59ac12SSebastian Grimberg 
585ba59ac12SSebastian Grimberg   Note: This is a reference implementation for CPU CeedScalar pointers that is not intended for high performance.
5867a982d89SJeremy L. Thompson 
587ea61e9acSJeremy L Thompson   @param[in]  ceed  Ceed context for error handling
588d1d35e2fSjeremylt   @param[in]  mat_A Row-major matrix A
589d1d35e2fSjeremylt   @param[in]  mat_B Row-major matrix B
590d1d35e2fSjeremylt   @param[out] mat_C Row-major output matrix C
591ea61e9acSJeremy L Thompson   @param[in]  m     Number of rows of C
592ea61e9acSJeremy L Thompson   @param[in]  n     Number of columns of C
593ea61e9acSJeremy L Thompson   @param[in]  kk    Number of columns of A/rows of B
5947a982d89SJeremy L. Thompson 
5957a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5967a982d89SJeremy L. Thompson 
5977a982d89SJeremy L. Thompson   @ref Utility
5987a982d89SJeremy L. Thompson **/
5992b730f8bSJeremy L Thompson int CeedMatrixMatrixMultiply(Ceed ceed, const CeedScalar *mat_A, const CeedScalar *mat_B, CeedScalar *mat_C, CeedInt m, CeedInt n, CeedInt kk) {
6002b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < m; i++) {
6017a982d89SJeremy L. Thompson     for (CeedInt j = 0; j < n; j++) {
6027a982d89SJeremy L. Thompson       CeedScalar sum = 0;
6032b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < kk; k++) sum += mat_A[k + i * kk] * mat_B[j + k * n];
604d1d35e2fSjeremylt       mat_C[j + i * n] = sum;
6057a982d89SJeremy L. Thompson     }
6062b730f8bSJeremy L Thompson   }
607e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6087a982d89SJeremy L. Thompson }
6097a982d89SJeremy L. Thompson 
610ba59ac12SSebastian Grimberg /**
611ba59ac12SSebastian Grimberg   @brief Return QR Factorization of a matrix
612ba59ac12SSebastian Grimberg 
613ba59ac12SSebastian Grimberg   @param[in]     ceed Ceed context for error handling
614ba59ac12SSebastian Grimberg   @param[in,out] mat  Row-major matrix to be factorized in place
615ba59ac12SSebastian Grimberg   @param[in,out] tau  Vector of length m of scaling factors
616ba59ac12SSebastian Grimberg   @param[in]     m    Number of rows
617ba59ac12SSebastian Grimberg   @param[in]     n    Number of columns
618ba59ac12SSebastian Grimberg 
619ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
620ba59ac12SSebastian Grimberg 
621ba59ac12SSebastian Grimberg   @ref Utility
622ba59ac12SSebastian Grimberg **/
623ba59ac12SSebastian Grimberg int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau, CeedInt m, CeedInt n) {
624ba59ac12SSebastian Grimberg   CeedScalar v[m];
625ba59ac12SSebastian Grimberg 
626ba59ac12SSebastian Grimberg   // Check matrix shape
6276574a04fSJeremy L Thompson   CeedCheck(n <= m, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute QR factorization with n > m");
628ba59ac12SSebastian Grimberg 
629ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
630ba59ac12SSebastian Grimberg     if (i >= m - 1) {  // last row of matrix, no reflection needed
631ba59ac12SSebastian Grimberg       tau[i] = 0.;
632ba59ac12SSebastian Grimberg       break;
633ba59ac12SSebastian Grimberg     }
634ba59ac12SSebastian Grimberg     // Calculate Householder vector, magnitude
635ba59ac12SSebastian Grimberg     CeedScalar sigma = 0.0;
636ba59ac12SSebastian Grimberg     v[i]             = mat[i + n * i];
637ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) {
638ba59ac12SSebastian Grimberg       v[j] = mat[i + n * j];
639ba59ac12SSebastian Grimberg       sigma += v[j] * v[j];
640ba59ac12SSebastian Grimberg     }
641ba59ac12SSebastian Grimberg     CeedScalar norm = sqrt(v[i] * v[i] + sigma);  // norm of v[i:m]
642ba59ac12SSebastian Grimberg     CeedScalar R_ii = -copysign(norm, v[i]);
643ba59ac12SSebastian Grimberg     v[i] -= R_ii;
644ba59ac12SSebastian Grimberg     // norm of v[i:m] after modification above and scaling below
645ba59ac12SSebastian Grimberg     //   norm = sqrt(v[i]*v[i] + sigma) / v[i];
646ba59ac12SSebastian Grimberg     //   tau = 2 / (norm*norm)
647ba59ac12SSebastian Grimberg     tau[i] = 2 * v[i] * v[i] / (v[i] * v[i] + sigma);
648ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) v[j] /= v[i];
649ba59ac12SSebastian Grimberg 
650ba59ac12SSebastian Grimberg     // Apply Householder reflector to lower right panel
651ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat[i * n + i + 1], &v[i], tau[i], m - i, n - i - 1, n, 1);
652ba59ac12SSebastian Grimberg     // Save v
653ba59ac12SSebastian Grimberg     mat[i + n * i] = R_ii;
654ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) mat[i + n * j] = v[j];
655ba59ac12SSebastian Grimberg   }
656ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
657ba59ac12SSebastian Grimberg }
658ba59ac12SSebastian Grimberg 
659ba59ac12SSebastian Grimberg /**
660ba59ac12SSebastian Grimberg   @brief Apply Householder Q matrix
661ba59ac12SSebastian Grimberg 
662ba59ac12SSebastian Grimberg   Compute mat_A = mat_Q mat_A, where mat_Q is mxm and mat_A is mxn.
663ba59ac12SSebastian Grimberg 
664ba59ac12SSebastian Grimberg   @param[in,out] mat_A  Matrix to apply Householder Q to, in place
665ba59ac12SSebastian Grimberg   @param[in]     mat_Q  Householder Q matrix
666ba59ac12SSebastian Grimberg   @param[in]     tau    Householder scaling factors
667ba59ac12SSebastian Grimberg   @param[in]     t_mode Transpose mode for application
668ba59ac12SSebastian Grimberg   @param[in]     m      Number of rows in A
669ba59ac12SSebastian Grimberg   @param[in]     n      Number of columns in A
670ba59ac12SSebastian Grimberg   @param[in]     k      Number of elementary reflectors in Q, k<m
671ba59ac12SSebastian Grimberg   @param[in]     row    Row stride in A
672ba59ac12SSebastian Grimberg   @param[in]     col    Col stride in A
673ba59ac12SSebastian Grimberg 
674ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
675ba59ac12SSebastian Grimberg 
676c4e3f59bSSebastian Grimberg   @ref Utility
677ba59ac12SSebastian Grimberg **/
678ba59ac12SSebastian Grimberg int CeedHouseholderApplyQ(CeedScalar *mat_A, const CeedScalar *mat_Q, const CeedScalar *tau, CeedTransposeMode t_mode, CeedInt m, CeedInt n,
679ba59ac12SSebastian Grimberg                           CeedInt k, CeedInt row, CeedInt col) {
680ba59ac12SSebastian Grimberg   CeedScalar *v;
681ba59ac12SSebastian Grimberg   CeedCall(CeedMalloc(m, &v));
682ba59ac12SSebastian Grimberg   for (CeedInt ii = 0; ii < k; ii++) {
683ba59ac12SSebastian Grimberg     CeedInt i = t_mode == CEED_TRANSPOSE ? ii : k - 1 - ii;
684ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) v[j] = mat_Q[j * k + i];
685ba59ac12SSebastian Grimberg     // Apply Householder reflector (I - tau v v^T) collo_grad_1d^T
686ba59ac12SSebastian Grimberg     CeedCall(CeedHouseholderReflect(&mat_A[i * row], &v[i], tau[i], m - i, n, row, col));
687ba59ac12SSebastian Grimberg   }
688ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&v));
689ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
690ba59ac12SSebastian Grimberg }
691ba59ac12SSebastian Grimberg 
692ba59ac12SSebastian Grimberg /**
693ba59ac12SSebastian Grimberg   @brief Return symmetric Schur decomposition of the symmetric matrix mat via symmetric QR factorization
694ba59ac12SSebastian Grimberg 
695ba59ac12SSebastian Grimberg   @param[in]     ceed   Ceed context for error handling
696ba59ac12SSebastian Grimberg   @param[in,out] mat    Row-major matrix to be factorized in place
697ba59ac12SSebastian Grimberg   @param[out]    lambda Vector of length n of eigenvalues
698ba59ac12SSebastian Grimberg   @param[in]     n      Number of rows/columns
699ba59ac12SSebastian Grimberg 
700ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
701ba59ac12SSebastian Grimberg 
702ba59ac12SSebastian Grimberg   @ref Utility
703ba59ac12SSebastian Grimberg **/
7042c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
7052c2ea1dbSJeremy L Thompson int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat, CeedScalar *lambda, CeedInt n) {
706ba59ac12SSebastian Grimberg   // Check bounds for clang-tidy
7076574a04fSJeremy L Thompson   CeedCheck(n > 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute symmetric Schur decomposition of scalars");
708ba59ac12SSebastian Grimberg 
709ba59ac12SSebastian Grimberg   CeedScalar v[n - 1], tau[n - 1], mat_T[n * n];
710ba59ac12SSebastian Grimberg 
711ba59ac12SSebastian Grimberg   // Copy mat to mat_T and set mat to I
712ba59ac12SSebastian Grimberg   memcpy(mat_T, mat, n * n * sizeof(mat[0]));
713ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
714ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < n; j++) mat[j + n * i] = (i == j) ? 1 : 0;
715ba59ac12SSebastian Grimberg   }
716ba59ac12SSebastian Grimberg 
717ba59ac12SSebastian Grimberg   // Reduce to tridiagonal
718ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n - 1; i++) {
719ba59ac12SSebastian Grimberg     // Calculate Householder vector, magnitude
720ba59ac12SSebastian Grimberg     CeedScalar sigma = 0.0;
721ba59ac12SSebastian Grimberg     v[i]             = mat_T[i + n * (i + 1)];
722ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) {
723ba59ac12SSebastian Grimberg       v[j] = mat_T[i + n * (j + 1)];
724ba59ac12SSebastian Grimberg       sigma += v[j] * v[j];
725ba59ac12SSebastian Grimberg     }
726ba59ac12SSebastian Grimberg     CeedScalar norm = sqrt(v[i] * v[i] + sigma);  // norm of v[i:n-1]
727ba59ac12SSebastian Grimberg     CeedScalar R_ii = -copysign(norm, v[i]);
728ba59ac12SSebastian Grimberg     v[i] -= R_ii;
729ba59ac12SSebastian Grimberg     // norm of v[i:m] after modification above and scaling below
730ba59ac12SSebastian Grimberg     //   norm = sqrt(v[i]*v[i] + sigma) / v[i];
731ba59ac12SSebastian Grimberg     //   tau = 2 / (norm*norm)
732ba59ac12SSebastian Grimberg     tau[i] = i == n - 2 ? 2 : 2 * v[i] * v[i] / (v[i] * v[i] + sigma);
733ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) v[j] /= v[i];
734ba59ac12SSebastian Grimberg 
735ba59ac12SSebastian Grimberg     // Update sub and super diagonal
736ba59ac12SSebastian Grimberg     for (CeedInt j = i + 2; j < n; j++) {
737ba59ac12SSebastian Grimberg       mat_T[i + n * j] = 0;
738ba59ac12SSebastian Grimberg       mat_T[j + n * i] = 0;
739ba59ac12SSebastian Grimberg     }
740ba59ac12SSebastian Grimberg     // Apply symmetric Householder reflector to lower right panel
741ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1);
742ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), 1, n);
743ba59ac12SSebastian Grimberg 
744ba59ac12SSebastian Grimberg     // Save v
745ba59ac12SSebastian Grimberg     mat_T[i + n * (i + 1)] = R_ii;
746ba59ac12SSebastian Grimberg     mat_T[(i + 1) + n * i] = R_ii;
747ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) {
748ba59ac12SSebastian Grimberg       mat_T[i + n * (j + 1)] = v[j];
749ba59ac12SSebastian Grimberg     }
750ba59ac12SSebastian Grimberg   }
751ba59ac12SSebastian Grimberg   // Backwards accumulation of Q
752ba59ac12SSebastian Grimberg   for (CeedInt i = n - 2; i >= 0; i--) {
753ba59ac12SSebastian Grimberg     if (tau[i] > 0.0) {
754ba59ac12SSebastian Grimberg       v[i] = 1;
755ba59ac12SSebastian Grimberg       for (CeedInt j = i + 1; j < n - 1; j++) {
756ba59ac12SSebastian Grimberg         v[j]                   = mat_T[i + n * (j + 1)];
757ba59ac12SSebastian Grimberg         mat_T[i + n * (j + 1)] = 0;
758ba59ac12SSebastian Grimberg       }
759ba59ac12SSebastian Grimberg       CeedHouseholderReflect(&mat[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1);
760ba59ac12SSebastian Grimberg     }
761ba59ac12SSebastian Grimberg   }
762ba59ac12SSebastian Grimberg 
763ba59ac12SSebastian Grimberg   // Reduce sub and super diagonal
764ba59ac12SSebastian Grimberg   CeedInt    p = 0, q = 0, itr = 0, max_itr = n * n * n * n;
765ba59ac12SSebastian Grimberg   CeedScalar tol = CEED_EPSILON;
766ba59ac12SSebastian Grimberg 
767ba59ac12SSebastian Grimberg   while (itr < max_itr) {
768ba59ac12SSebastian Grimberg     // Update p, q, size of reduced portions of diagonal
769ba59ac12SSebastian Grimberg     p = 0;
770ba59ac12SSebastian Grimberg     q = 0;
771ba59ac12SSebastian Grimberg     for (CeedInt i = n - 2; i >= 0; i--) {
772ba59ac12SSebastian Grimberg       if (fabs(mat_T[i + n * (i + 1)]) < tol) q += 1;
773ba59ac12SSebastian Grimberg       else break;
774ba59ac12SSebastian Grimberg     }
775ba59ac12SSebastian Grimberg     for (CeedInt i = 0; i < n - q - 1; i++) {
776ba59ac12SSebastian Grimberg       if (fabs(mat_T[i + n * (i + 1)]) < tol) p += 1;
777ba59ac12SSebastian Grimberg       else break;
778ba59ac12SSebastian Grimberg     }
779ba59ac12SSebastian Grimberg     if (q == n - 1) break;  // Finished reducing
780ba59ac12SSebastian Grimberg 
781ba59ac12SSebastian Grimberg     // Reduce tridiagonal portion
782ba59ac12SSebastian Grimberg     CeedScalar t_nn = mat_T[(n - 1 - q) + n * (n - 1 - q)], t_nnm1 = mat_T[(n - 2 - q) + n * (n - 1 - q)];
783ba59ac12SSebastian Grimberg     CeedScalar d  = (mat_T[(n - 2 - q) + n * (n - 2 - q)] - t_nn) / 2;
784ba59ac12SSebastian Grimberg     CeedScalar mu = t_nn - t_nnm1 * t_nnm1 / (d + copysign(sqrt(d * d + t_nnm1 * t_nnm1), d));
785ba59ac12SSebastian Grimberg     CeedScalar x  = mat_T[p + n * p] - mu;
786ba59ac12SSebastian Grimberg     CeedScalar z  = mat_T[p + n * (p + 1)];
787ba59ac12SSebastian Grimberg     for (CeedInt k = p; k < n - q - 1; k++) {
788ba59ac12SSebastian Grimberg       // Compute Givens rotation
789ba59ac12SSebastian Grimberg       CeedScalar c = 1, s = 0;
790ba59ac12SSebastian Grimberg       if (fabs(z) > tol) {
791ba59ac12SSebastian Grimberg         if (fabs(z) > fabs(x)) {
792ba59ac12SSebastian Grimberg           CeedScalar tau = -x / z;
793ba59ac12SSebastian Grimberg           s = 1 / sqrt(1 + tau * tau), c = s * tau;
794ba59ac12SSebastian Grimberg         } else {
795ba59ac12SSebastian Grimberg           CeedScalar tau = -z / x;
796ba59ac12SSebastian Grimberg           c = 1 / sqrt(1 + tau * tau), s = c * tau;
797ba59ac12SSebastian Grimberg         }
798ba59ac12SSebastian Grimberg       }
799ba59ac12SSebastian Grimberg 
800ba59ac12SSebastian Grimberg       // Apply Givens rotation to T
801ba59ac12SSebastian Grimberg       CeedGivensRotation(mat_T, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n);
802ba59ac12SSebastian Grimberg       CeedGivensRotation(mat_T, c, s, CEED_TRANSPOSE, k, k + 1, n, n);
803ba59ac12SSebastian Grimberg 
804ba59ac12SSebastian Grimberg       // Apply Givens rotation to Q
805ba59ac12SSebastian Grimberg       CeedGivensRotation(mat, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n);
806ba59ac12SSebastian Grimberg 
807ba59ac12SSebastian Grimberg       // Update x, z
808ba59ac12SSebastian Grimberg       if (k < n - q - 2) {
809ba59ac12SSebastian Grimberg         x = mat_T[k + n * (k + 1)];
810ba59ac12SSebastian Grimberg         z = mat_T[k + n * (k + 2)];
811ba59ac12SSebastian Grimberg       }
812ba59ac12SSebastian Grimberg     }
813ba59ac12SSebastian Grimberg     itr++;
814ba59ac12SSebastian Grimberg   }
815ba59ac12SSebastian Grimberg 
816ba59ac12SSebastian Grimberg   // Save eigenvalues
817ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) lambda[i] = mat_T[i + n * i];
818ba59ac12SSebastian Grimberg 
819ba59ac12SSebastian Grimberg   // Check convergence
8206574a04fSJeremy L Thompson   CeedCheck(itr < max_itr || q > n, ceed, CEED_ERROR_MINOR, "Symmetric QR failed to converge");
821ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
822ba59ac12SSebastian Grimberg }
8232c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
824ba59ac12SSebastian Grimberg 
825ba59ac12SSebastian Grimberg /**
826ba59ac12SSebastian Grimberg   @brief Return Simultaneous Diagonalization of two matrices.
827ba59ac12SSebastian Grimberg 
828ba59ac12SSebastian Grimberg   This solves the generalized eigenvalue problem A x = lambda B x, where A and B are symmetric and B is positive definite.
829ba59ac12SSebastian Grimberg   We generate the matrix X and vector Lambda such that X^T A X = Lambda and X^T B X = I.
830ba59ac12SSebastian Grimberg   This is equivalent to the LAPACK routine 'sygv' with TYPE = 1.
831ba59ac12SSebastian Grimberg 
832ba59ac12SSebastian Grimberg   @param[in]  ceed   Ceed context for error handling
833ba59ac12SSebastian Grimberg   @param[in]  mat_A  Row-major matrix to be factorized with eigenvalues
834ba59ac12SSebastian Grimberg   @param[in]  mat_B  Row-major matrix to be factorized to identity
835ba59ac12SSebastian Grimberg   @param[out] mat_X  Row-major orthogonal matrix
836ba59ac12SSebastian Grimberg   @param[out] lambda Vector of length n of generalized eigenvalues
837ba59ac12SSebastian Grimberg   @param[in]  n      Number of rows/columns
838ba59ac12SSebastian Grimberg 
839ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
840ba59ac12SSebastian Grimberg 
841ba59ac12SSebastian Grimberg   @ref Utility
842ba59ac12SSebastian Grimberg **/
8432c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
8442c2ea1dbSJeremy L Thompson int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A, CeedScalar *mat_B, CeedScalar *mat_X, CeedScalar *lambda, CeedInt n) {
845ba59ac12SSebastian Grimberg   CeedScalar *mat_C, *mat_G, *vec_D;
846ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n * n, &mat_C));
847ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n * n, &mat_G));
848ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n, &vec_D));
849ba59ac12SSebastian Grimberg 
850ba59ac12SSebastian Grimberg   // Compute B = G D G^T
851ba59ac12SSebastian Grimberg   memcpy(mat_G, mat_B, n * n * sizeof(mat_B[0]));
852ba59ac12SSebastian Grimberg   CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_G, vec_D, n));
853ba59ac12SSebastian Grimberg 
854ba59ac12SSebastian Grimberg   // Sort eigenvalues
855ba59ac12SSebastian Grimberg   for (CeedInt i = n - 1; i >= 0; i--) {
856ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < i; j++) {
857ba59ac12SSebastian Grimberg       if (fabs(vec_D[j]) > fabs(vec_D[j + 1])) {
858ba59ac12SSebastian Grimberg         CeedScalar temp;
859ba59ac12SSebastian Grimberg         temp         = vec_D[j];
860ba59ac12SSebastian Grimberg         vec_D[j]     = vec_D[j + 1];
861ba59ac12SSebastian Grimberg         vec_D[j + 1] = temp;
862ba59ac12SSebastian Grimberg         for (CeedInt k = 0; k < n; k++) {
863ba59ac12SSebastian Grimberg           temp                 = mat_G[k * n + j];
864ba59ac12SSebastian Grimberg           mat_G[k * n + j]     = mat_G[k * n + j + 1];
865ba59ac12SSebastian Grimberg           mat_G[k * n + j + 1] = temp;
866ba59ac12SSebastian Grimberg         }
867ba59ac12SSebastian Grimberg       }
868ba59ac12SSebastian Grimberg     }
869ba59ac12SSebastian Grimberg   }
870ba59ac12SSebastian Grimberg 
871ba59ac12SSebastian Grimberg   // Compute C = (G D^1/2)^-1 A (G D^1/2)^-T
872ba59ac12SSebastian Grimberg   //           = D^-1/2 G^T A G D^-1/2
873ba59ac12SSebastian Grimberg   // -- D = D^-1/2
874ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) vec_D[i] = 1. / sqrt(vec_D[i]);
875ba59ac12SSebastian Grimberg   // -- G = G D^-1/2
876ba59ac12SSebastian Grimberg   // -- C = D^-1/2 G^T
877ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
878ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < n; j++) {
879ba59ac12SSebastian Grimberg       mat_G[i * n + j] *= vec_D[j];
880ba59ac12SSebastian Grimberg       mat_C[j * n + i] = mat_G[i * n + j];
881ba59ac12SSebastian Grimberg     }
882ba59ac12SSebastian Grimberg   }
883ba59ac12SSebastian Grimberg   // -- X = (D^-1/2 G^T) A
884ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_C, (const CeedScalar *)mat_A, mat_X, n, n, n));
885ba59ac12SSebastian Grimberg   // -- C = (D^-1/2 G^T A) (G D^-1/2)
886ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_X, (const CeedScalar *)mat_G, mat_C, n, n, n));
887ba59ac12SSebastian Grimberg 
888ba59ac12SSebastian Grimberg   // Compute Q^T C Q = lambda
889ba59ac12SSebastian Grimberg   CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_C, lambda, n));
890ba59ac12SSebastian Grimberg 
891ba59ac12SSebastian Grimberg   // Sort eigenvalues
892ba59ac12SSebastian Grimberg   for (CeedInt i = n - 1; i >= 0; i--) {
893ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < i; j++) {
894ba59ac12SSebastian Grimberg       if (fabs(lambda[j]) > fabs(lambda[j + 1])) {
895ba59ac12SSebastian Grimberg         CeedScalar temp;
896ba59ac12SSebastian Grimberg         temp          = lambda[j];
897ba59ac12SSebastian Grimberg         lambda[j]     = lambda[j + 1];
898ba59ac12SSebastian Grimberg         lambda[j + 1] = temp;
899ba59ac12SSebastian Grimberg         for (CeedInt k = 0; k < n; k++) {
900ba59ac12SSebastian Grimberg           temp                 = mat_C[k * n + j];
901ba59ac12SSebastian Grimberg           mat_C[k * n + j]     = mat_C[k * n + j + 1];
902ba59ac12SSebastian Grimberg           mat_C[k * n + j + 1] = temp;
903ba59ac12SSebastian Grimberg         }
904ba59ac12SSebastian Grimberg       }
905ba59ac12SSebastian Grimberg     }
906ba59ac12SSebastian Grimberg   }
907ba59ac12SSebastian Grimberg 
908ba59ac12SSebastian Grimberg   // Set X = (G D^1/2)^-T Q
909ba59ac12SSebastian Grimberg   //       = G D^-1/2 Q
910ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_G, (const CeedScalar *)mat_C, mat_X, n, n, n));
911ba59ac12SSebastian Grimberg 
912ba59ac12SSebastian Grimberg   // Cleanup
913ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&mat_C));
914ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&mat_G));
915ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&vec_D));
916ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
917ba59ac12SSebastian Grimberg }
9182c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
919ba59ac12SSebastian Grimberg 
9207a982d89SJeremy L. Thompson /// @}
9217a982d89SJeremy L. Thompson 
9227a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
9237a982d89SJeremy L. Thompson /// CeedBasis Public API
9247a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
9257a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser
926d7b241e6Sjeremylt /// @{
927d7b241e6Sjeremylt 
928b11c1e72Sjeremylt /**
929ba59ac12SSebastian Grimberg   @brief Create a tensor-product basis for H^1 discretizations
930b11c1e72Sjeremylt 
931ea61e9acSJeremy L Thompson   @param[in]  ceed        Ceed object where the CeedBasis will be created
932ea61e9acSJeremy L Thompson   @param[in]  dim         Topological dimension
933ea61e9acSJeremy L Thompson   @param[in]  num_comp    Number of field components (1 for scalar fields)
934ea61e9acSJeremy L Thompson   @param[in]  P_1d        Number of nodes in one dimension
935ea61e9acSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
936ea61e9acSJeremy L Thompson   @param[in]  interp_1d   Row-major (Q_1d * P_1d) matrix expressing the values of nodal basis functions at quadrature points
937ea61e9acSJeremy L Thompson   @param[in]  grad_1d     Row-major (Q_1d * P_1d) matrix expressing derivatives of nodal basis functions at quadrature points
938ea61e9acSJeremy 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]
939ea61e9acSJeremy L Thompson   @param[in]  q_weight_1d Array of length Q_1d holding the quadrature weights on the reference element
940ea61e9acSJeremy L Thompson   @param[out] basis       Address of the variable where the newly created CeedBasis will be stored.
941b11c1e72Sjeremylt 
942b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
943dfdf5a53Sjeremylt 
9447a982d89SJeremy L. Thompson   @ref User
945b11c1e72Sjeremylt **/
9462b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d,
9472b730f8bSJeremy L Thompson                             const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis *basis) {
9485fe0d4faSjeremylt   if (!ceed->BasisCreateTensorH1) {
9495fe0d4faSjeremylt     Ceed delegate;
9506574a04fSJeremy L Thompson 
9512b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
9526574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisCreateTensorH1");
9532b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis));
954e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
9555fe0d4faSjeremylt   }
956e15f9bd0SJeremy L Thompson 
9576574a04fSJeremy L Thompson   CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "Basis dimension must be a positive value");
9586574a04fSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component");
9596574a04fSJeremy L Thompson   CeedCheck(P_1d > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node");
9606574a04fSJeremy L Thompson   CeedCheck(Q_1d > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point");
961227444bfSJeremy L Thompson 
9622b730f8bSJeremy L Thompson   CeedElemTopology topo = dim == 1 ? CEED_TOPOLOGY_LINE : dim == 2 ? CEED_TOPOLOGY_QUAD : CEED_TOPOLOGY_HEX;
963e15f9bd0SJeremy L Thompson 
9642b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
965db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed));
966d1d35e2fSjeremylt   (*basis)->ref_count       = 1;
9676402da51SJeremy L Thompson   (*basis)->is_tensor_basis = true;
968d7b241e6Sjeremylt   (*basis)->dim             = dim;
969d99fa3c5SJeremy L Thompson   (*basis)->topo            = topo;
970d1d35e2fSjeremylt   (*basis)->num_comp        = num_comp;
971d1d35e2fSjeremylt   (*basis)->P_1d            = P_1d;
972d1d35e2fSjeremylt   (*basis)->Q_1d            = Q_1d;
973d1d35e2fSjeremylt   (*basis)->P               = CeedIntPow(P_1d, dim);
974d1d35e2fSjeremylt   (*basis)->Q               = CeedIntPow(Q_1d, dim);
975c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_H1;
9762b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d, &(*basis)->q_ref_1d));
9772b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d, &(*basis)->q_weight_1d));
978ff3a0f91SJeremy L Thompson   if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d * sizeof(q_ref_1d[0]));
9792b730f8bSJeremy L Thompson   if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, Q_1d * sizeof(q_weight_1d[0]));
9802b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->interp_1d));
9812b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->grad_1d));
9822b730f8bSJeremy L Thompson   if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, Q_1d * P_1d * sizeof(interp_1d[0]));
983ff3a0f91SJeremy L Thompson   if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d * P_1d * sizeof(grad_1d[0]));
9842b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, *basis));
985e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
986d7b241e6Sjeremylt }
987d7b241e6Sjeremylt 
988b11c1e72Sjeremylt /**
98995bb1877Svaleriabarra   @brief Create a tensor-product Lagrange basis
990b11c1e72Sjeremylt 
991ea61e9acSJeremy L Thompson   @param[in]  ceed      Ceed object where the CeedBasis will be created
992ea61e9acSJeremy L Thompson   @param[in]  dim       Topological dimension of element
993ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of field components (1 for scalar fields)
994ea61e9acSJeremy L Thompson   @param[in]  P         Number of Gauss-Lobatto nodes in one dimension.
995ea61e9acSJeremy L Thompson                           The polynomial degree of the resulting Q_k element is k=P-1.
996ea61e9acSJeremy L Thompson   @param[in]  Q         Number of quadrature points in one dimension.
997ea61e9acSJeremy L Thompson   @param[in]  quad_mode Distribution of the Q quadrature points (affects order of accuracy for the quadrature)
998ea61e9acSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created CeedBasis will be stored.
999b11c1e72Sjeremylt 
1000b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1001dfdf5a53Sjeremylt 
10027a982d89SJeremy L. Thompson   @ref User
1003b11c1e72Sjeremylt **/
10042b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis) {
1005d7b241e6Sjeremylt   // Allocate
1006c8c3fa7dSJeremy L Thompson   int        ierr = CEED_ERROR_SUCCESS;
10072b730f8bSJeremy L Thompson   CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, *q_weight_1d;
10084d537eeaSYohann 
10096574a04fSJeremy L Thompson   CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "Basis dimension must be a positive value");
10106574a04fSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component");
10116574a04fSJeremy L Thompson   CeedCheck(P > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node");
10126574a04fSJeremy L Thompson   CeedCheck(Q > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point");
1013227444bfSJeremy L Thompson 
1014e15f9bd0SJeremy L Thompson   // Get Nodes and Weights
10152b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P * Q, &interp_1d));
10162b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P * Q, &grad_1d));
10172b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P, &nodes));
10182b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &q_ref_1d));
10192b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &q_weight_1d));
10202b730f8bSJeremy L Thompson   if (CeedLobattoQuadrature(P, nodes, NULL) != CEED_ERROR_SUCCESS) goto cleanup;
1021d1d35e2fSjeremylt   switch (quad_mode) {
1022d7b241e6Sjeremylt     case CEED_GAUSS:
1023d1d35e2fSjeremylt       ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d);
1024d7b241e6Sjeremylt       break;
1025d7b241e6Sjeremylt     case CEED_GAUSS_LOBATTO:
1026d1d35e2fSjeremylt       ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d);
1027d7b241e6Sjeremylt       break;
1028d7b241e6Sjeremylt   }
10292b730f8bSJeremy L Thompson   if (ierr != CEED_ERROR_SUCCESS) goto cleanup;
1030e15f9bd0SJeremy L Thompson 
1031d7b241e6Sjeremylt   // Build B, D matrix
1032d7b241e6Sjeremylt   // Fornberg, 1998
1033c8c3fa7dSJeremy L Thompson   for (CeedInt i = 0; i < Q; i++) {
1034d7b241e6Sjeremylt     c1                   = 1.0;
1035d1d35e2fSjeremylt     c3                   = nodes[0] - q_ref_1d[i];
1036d1d35e2fSjeremylt     interp_1d[i * P + 0] = 1.0;
1037c8c3fa7dSJeremy L Thompson     for (CeedInt j = 1; j < P; j++) {
1038d7b241e6Sjeremylt       c2 = 1.0;
1039d7b241e6Sjeremylt       c4 = c3;
1040d1d35e2fSjeremylt       c3 = nodes[j] - q_ref_1d[i];
1041c8c3fa7dSJeremy L Thompson       for (CeedInt k = 0; k < j; k++) {
1042d7b241e6Sjeremylt         dx = nodes[j] - nodes[k];
1043d7b241e6Sjeremylt         c2 *= dx;
1044d7b241e6Sjeremylt         if (k == j - 1) {
1045d1d35e2fSjeremylt           grad_1d[i * P + j]   = c1 * (interp_1d[i * P + k] - c4 * grad_1d[i * P + k]) / c2;
1046d1d35e2fSjeremylt           interp_1d[i * P + j] = -c1 * c4 * interp_1d[i * P + k] / c2;
1047d7b241e6Sjeremylt         }
1048d1d35e2fSjeremylt         grad_1d[i * P + k]   = (c3 * grad_1d[i * P + k] - interp_1d[i * P + k]) / dx;
1049d1d35e2fSjeremylt         interp_1d[i * P + k] = c3 * interp_1d[i * P + k] / dx;
1050d7b241e6Sjeremylt       }
1051d7b241e6Sjeremylt       c1 = c2;
1052d7b241e6Sjeremylt     }
1053d7b241e6Sjeremylt   }
10549ac7b42eSJeremy L Thompson   // Pass to CeedBasisCreateTensorH1
10552b730f8bSJeremy L Thompson   CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis));
1056e15f9bd0SJeremy L Thompson cleanup:
10572b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_1d));
10582b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_1d));
10592b730f8bSJeremy L Thompson   CeedCall(CeedFree(&nodes));
10602b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref_1d));
10612b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight_1d));
1062e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1063d7b241e6Sjeremylt }
1064d7b241e6Sjeremylt 
1065b11c1e72Sjeremylt /**
1066ba59ac12SSebastian Grimberg   @brief Create a non tensor-product basis for H^1 discretizations
1067a8de75f0Sjeremylt 
1068ea61e9acSJeremy L Thompson   @param[in]  ceed      Ceed object where the CeedBasis will be created
1069ea61e9acSJeremy L Thompson   @param[in]  topo      Topology of element, e.g. hypercube, simplex, ect
1070ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of field components (1 for scalar fields)
1071ea61e9acSJeremy L Thompson   @param[in]  num_nodes Total number of nodes
1072ea61e9acSJeremy L Thompson   @param[in]  num_qpts  Total number of quadrature points
1073ea61e9acSJeremy L Thompson   @param[in]  interp    Row-major (num_qpts * num_nodes) matrix expressing the values of nodal basis functions at quadrature points
1074c4e3f59bSSebastian Grimberg   @param[in]  grad      Row-major (dim * num_qpts * num_nodes) matrix expressing derivatives of nodal basis functions at quadrature points
10759fe083eeSJeremy L Thompson   @param[in]  q_ref     Array of length num_qpts * dim holding the locations of quadrature points on the reference element
1076ea61e9acSJeremy L Thompson   @param[in]  q_weight  Array of length num_qpts holding the quadrature weights on the reference element
1077ea61e9acSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created CeedBasis will be stored.
1078a8de75f0Sjeremylt 
1079a8de75f0Sjeremylt   @return An error code: 0 - success, otherwise - failure
1080a8de75f0Sjeremylt 
10817a982d89SJeremy L. Thompson   @ref User
1082a8de75f0Sjeremylt **/
10832b730f8bSJeremy L Thompson int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
10842b730f8bSJeremy L Thompson                       const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
1085d1d35e2fSjeremylt   CeedInt P = num_nodes, Q = num_qpts, dim = 0;
1086a8de75f0Sjeremylt 
10875fe0d4faSjeremylt   if (!ceed->BasisCreateH1) {
10885fe0d4faSjeremylt     Ceed delegate;
10896574a04fSJeremy L Thompson 
10902b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
10916574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisCreateH1");
10922b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis));
1093e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
10945fe0d4faSjeremylt   }
10955fe0d4faSjeremylt 
10966574a04fSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component");
10976574a04fSJeremy L Thompson   CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node");
10986574a04fSJeremy L Thompson   CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point");
1099227444bfSJeremy L Thompson 
11002b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1101a8de75f0Sjeremylt 
1102db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1103db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed));
1104d1d35e2fSjeremylt   (*basis)->ref_count       = 1;
11056402da51SJeremy L Thompson   (*basis)->is_tensor_basis = false;
1106a8de75f0Sjeremylt   (*basis)->dim             = dim;
1107d99fa3c5SJeremy L Thompson   (*basis)->topo            = topo;
1108d1d35e2fSjeremylt   (*basis)->num_comp        = num_comp;
1109a8de75f0Sjeremylt   (*basis)->P               = P;
1110a8de75f0Sjeremylt   (*basis)->Q               = Q;
1111c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_H1;
11122b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q * dim, &(*basis)->q_ref_1d));
11132b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &(*basis)->q_weight_1d));
1114ff3a0f91SJeremy L Thompson   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
1115ff3a0f91SJeremy L Thompson   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
11162b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q * P, &(*basis)->interp));
11172b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(dim * Q * P, &(*basis)->grad));
1118ff3a0f91SJeremy L Thompson   if (interp) memcpy((*basis)->interp, interp, Q * P * sizeof(interp[0]));
1119ff3a0f91SJeremy L Thompson   if (grad) memcpy((*basis)->grad, grad, dim * Q * P * sizeof(grad[0]));
11202b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, *basis));
1121e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1122a8de75f0Sjeremylt }
1123a8de75f0Sjeremylt 
1124a8de75f0Sjeremylt /**
1125859c15bbSJames Wright   @brief Create a non tensor-product basis for \f$H(\mathrm{div})\f$ discretizations
112650c301a5SRezgar Shakeri 
1127ea61e9acSJeremy L Thompson   @param[in]  ceed      Ceed object where the CeedBasis will be created
1128ea61e9acSJeremy 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
1129ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of components (usually 1 for vectors in H(div) bases)
1130ea61e9acSJeremy L Thompson   @param[in]  num_nodes Total number of nodes (dofs per element)
1131ea61e9acSJeremy L Thompson   @param[in]  num_qpts  Total number of quadrature points
1132c4e3f59bSSebastian Grimberg   @param[in]  interp    Row-major (dim * num_qpts * num_nodes) matrix expressing the values of basis functions at quadrature points
1133c4e3f59bSSebastian Grimberg   @param[in]  div       Row-major (num_qpts * num_nodes) matrix expressing divergence of basis functions at quadrature points
11349fe083eeSJeremy L Thompson   @param[in]  q_ref     Array of length num_qpts * dim holding the locations of quadrature points on the reference element
1135ea61e9acSJeremy L Thompson   @param[in]  q_weight  Array of length num_qpts holding the quadrature weights on the reference element
1136ea61e9acSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created CeedBasis will be stored.
113750c301a5SRezgar Shakeri 
113850c301a5SRezgar Shakeri   @return An error code: 0 - success, otherwise - failure
113950c301a5SRezgar Shakeri 
114050c301a5SRezgar Shakeri   @ref User
114150c301a5SRezgar Shakeri **/
11422b730f8bSJeremy L Thompson int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
11432b730f8bSJeremy L Thompson                         const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
114450c301a5SRezgar Shakeri   CeedInt Q = num_qpts, P = num_nodes, dim = 0;
1145c4e3f59bSSebastian Grimberg 
114650c301a5SRezgar Shakeri   if (!ceed->BasisCreateHdiv) {
114750c301a5SRezgar Shakeri     Ceed delegate;
11486574a04fSJeremy L Thompson 
11492b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
11506574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv");
11512b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis));
115250c301a5SRezgar Shakeri     return CEED_ERROR_SUCCESS;
115350c301a5SRezgar Shakeri   }
115450c301a5SRezgar Shakeri 
11556574a04fSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component");
11566574a04fSJeremy L Thompson   CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node");
11576574a04fSJeremy L Thompson   CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point");
1158227444bfSJeremy L Thompson 
1159c4e3f59bSSebastian Grimberg   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1160c4e3f59bSSebastian Grimberg 
1161db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1162db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed));
116350c301a5SRezgar Shakeri   (*basis)->ref_count       = 1;
11646402da51SJeremy L Thompson   (*basis)->is_tensor_basis = false;
116550c301a5SRezgar Shakeri   (*basis)->dim             = dim;
116650c301a5SRezgar Shakeri   (*basis)->topo            = topo;
116750c301a5SRezgar Shakeri   (*basis)->num_comp        = num_comp;
116850c301a5SRezgar Shakeri   (*basis)->P               = P;
116950c301a5SRezgar Shakeri   (*basis)->Q               = Q;
1170c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_HDIV;
11712b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d));
11722b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d));
117350c301a5SRezgar Shakeri   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
117450c301a5SRezgar Shakeri   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
11752b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp));
11762b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q * P, &(*basis)->div));
117750c301a5SRezgar Shakeri   if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0]));
117850c301a5SRezgar Shakeri   if (div) memcpy((*basis)->div, div, Q * P * sizeof(div[0]));
11792b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, q_weight, *basis));
118050c301a5SRezgar Shakeri   return CEED_ERROR_SUCCESS;
118150c301a5SRezgar Shakeri }
118250c301a5SRezgar Shakeri 
118350c301a5SRezgar Shakeri /**
11844385fb7fSSebastian Grimberg   @brief Create a non tensor-product basis for \f$H(\mathrm{curl})\f$ discretizations
1185c4e3f59bSSebastian Grimberg 
1186c4e3f59bSSebastian Grimberg   @param[in]  ceed      Ceed object where the CeedBasis will be created
1187c4e3f59bSSebastian Grimberg   @param[in]  topo      Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below
1188c4e3f59bSSebastian Grimberg   @param[in]  num_comp  Number of components (usually 1 for vectors in H(curl) bases)
1189c4e3f59bSSebastian Grimberg   @param[in]  num_nodes Total number of nodes (dofs per element)
1190c4e3f59bSSebastian Grimberg   @param[in]  num_qpts  Total number of quadrature points
1191c4e3f59bSSebastian Grimberg   @param[in]  interp    Row-major (dim * num_qpts * num_nodes) matrix expressing the values of basis functions at quadrature points
1192c4e3f59bSSebastian Grimberg   @param[in]  curl      Row-major (curl_comp * num_qpts * num_nodes, curl_comp = 1 if dim < 3 else dim) matrix expressing curl of basis functions at
1193c4e3f59bSSebastian Grimberg quadrature points
1194c4e3f59bSSebastian Grimberg   @param[in]  q_ref     Array of length num_qpts * dim holding the locations of quadrature points on the reference element
1195c4e3f59bSSebastian Grimberg   @param[in]  q_weight  Array of length num_qpts holding the quadrature weights on the reference element
1196c4e3f59bSSebastian Grimberg   @param[out] basis     Address of the variable where the newly created CeedBasis will be stored.
1197c4e3f59bSSebastian Grimberg 
1198c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1199c4e3f59bSSebastian Grimberg 
1200c4e3f59bSSebastian Grimberg   @ref User
1201c4e3f59bSSebastian Grimberg **/
1202c4e3f59bSSebastian Grimberg int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
1203c4e3f59bSSebastian Grimberg                          const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
1204c4e3f59bSSebastian Grimberg   CeedInt Q = num_qpts, P = num_nodes, dim = 0, curl_comp = 0;
1205c4e3f59bSSebastian Grimberg 
1206c4e3f59bSSebastian Grimberg   if (!ceed->BasisCreateHdiv) {
1207c4e3f59bSSebastian Grimberg     Ceed delegate;
12086574a04fSJeremy L Thompson 
1209c4e3f59bSSebastian Grimberg     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
12106574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl");
1211c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis));
1212c4e3f59bSSebastian Grimberg     return CEED_ERROR_SUCCESS;
1213c4e3f59bSSebastian Grimberg   }
1214c4e3f59bSSebastian Grimberg 
12156574a04fSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component");
12166574a04fSJeremy L Thompson   CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node");
12176574a04fSJeremy L Thompson   CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point");
1218c4e3f59bSSebastian Grimberg 
1219c4e3f59bSSebastian Grimberg   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1220c4e3f59bSSebastian Grimberg   curl_comp = (dim < 3) ? 1 : dim;
1221c4e3f59bSSebastian Grimberg 
1222db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1223db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed));
1224c4e3f59bSSebastian Grimberg   (*basis)->ref_count       = 1;
12256402da51SJeremy L Thompson   (*basis)->is_tensor_basis = false;
1226c4e3f59bSSebastian Grimberg   (*basis)->dim             = dim;
1227c4e3f59bSSebastian Grimberg   (*basis)->topo            = topo;
1228c4e3f59bSSebastian Grimberg   (*basis)->num_comp        = num_comp;
1229c4e3f59bSSebastian Grimberg   (*basis)->P               = P;
1230c4e3f59bSSebastian Grimberg   (*basis)->Q               = Q;
1231c4e3f59bSSebastian Grimberg   (*basis)->fe_space        = CEED_FE_SPACE_HCURL;
1232c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d));
1233c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d));
1234c4e3f59bSSebastian Grimberg   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
1235c4e3f59bSSebastian Grimberg   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
1236c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp));
1237c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(curl_comp * Q * P, &(*basis)->curl));
1238c4e3f59bSSebastian Grimberg   if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0]));
1239c4e3f59bSSebastian Grimberg   if (curl) memcpy((*basis)->curl, curl, curl_comp * Q * P * sizeof(curl[0]));
1240c4e3f59bSSebastian Grimberg   CeedCall(ceed->BasisCreateHcurl(topo, dim, P, Q, interp, curl, q_ref, q_weight, *basis));
1241c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1242c4e3f59bSSebastian Grimberg }
1243c4e3f59bSSebastian Grimberg 
1244c4e3f59bSSebastian Grimberg /**
1245ea61e9acSJeremy L Thompson   @brief Create a CeedBasis for projection from the nodes of `basis_from` to the nodes of `basis_to`.
1246ba59ac12SSebastian Grimberg 
12479fd66db6SSebastian Grimberg   Only `CEED_EVAL_INTERP` will be valid for the new basis, `basis_project`.
12489fd66db6SSebastian Grimberg   For H^1 spaces, `CEED_EVAL_GRAD` will also be valid.
1249de05fbb2SSebastian Grimberg   The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR
12509fd66db6SSebastian Grimberg factorization.
12519fd66db6SSebastian Grimberg   The gradient (for the H^1 case) is given by `grad_project = interp_to^+ * grad_from`.
125215ad3917SSebastian Grimberg 
125315ad3917SSebastian Grimberg   Note: `basis_from` and `basis_to` must have compatible quadrature spaces.
125415ad3917SSebastian Grimberg 
12559fd66db6SSebastian Grimberg   Note: `basis_project` will have the same number of components as `basis_from`, regardless of the number of components that `basis_to` has.
12569fd66db6SSebastian Grimberg         If `basis_from` has 3 components and `basis_to` has 5 components, then `basis_project` will have 3 components.
1257f113e5dcSJeremy L Thompson 
1258f113e5dcSJeremy L Thompson   @param[in]  basis_from    CeedBasis to prolong from
1259446e7af4SJeremy L Thompson   @param[in]  basis_to      CeedBasis to prolong to
1260ea61e9acSJeremy L Thompson   @param[out] basis_project Address of the variable where the newly created CeedBasis will be stored.
1261f113e5dcSJeremy L Thompson 
1262f113e5dcSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1263f113e5dcSJeremy L Thompson 
1264f113e5dcSJeremy L Thompson   @ref User
1265f113e5dcSJeremy L Thompson **/
12662b730f8bSJeremy L Thompson int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project) {
1267f113e5dcSJeremy L Thompson   Ceed ceed;
12682b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis_to, &ceed));
1269f113e5dcSJeremy L Thompson 
1270ecc88aebSJeremy L Thompson   // Create projection matrix
127114556e63SJeremy L Thompson   CeedScalar *interp_project, *grad_project;
12722b730f8bSJeremy L Thompson   CeedCall(CeedBasisCreateProjectionMatrices(basis_from, basis_to, &interp_project, &grad_project));
1273f113e5dcSJeremy L Thompson 
1274f113e5dcSJeremy L Thompson   // Build basis
1275f113e5dcSJeremy L Thompson   bool        is_tensor;
1276f113e5dcSJeremy L Thompson   CeedInt     dim, num_comp;
127714556e63SJeremy L Thompson   CeedScalar *q_ref, *q_weight;
12782b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis_to, &is_tensor));
12792b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis_to, &dim));
12802b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_from, &num_comp));
1281f113e5dcSJeremy L Thompson   if (is_tensor) {
1282f113e5dcSJeremy L Thompson     CeedInt P_1d_to, P_1d_from;
12832b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_1d_from));
12842b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_1d_to));
12852b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_to, &q_ref));
12862b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_to, &q_weight));
12872b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, interp_project, grad_project, q_ref, q_weight, basis_project));
1288f113e5dcSJeremy L Thompson   } else {
1289de05fbb2SSebastian Grimberg     // Even if basis_to and basis_from are not H1, the resulting basis is H1 for interpolation to work
1290f113e5dcSJeremy L Thompson     CeedElemTopology topo;
12912b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetTopology(basis_to, &topo));
1292f113e5dcSJeremy L Thompson     CeedInt num_nodes_to, num_nodes_from;
12932b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_from, &num_nodes_from));
12942b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_to, &num_nodes_to));
12952b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_to * dim, &q_ref));
12962b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_to, &q_weight));
12972b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, q_ref, q_weight, basis_project));
1298f113e5dcSJeremy L Thompson   }
1299f113e5dcSJeremy L Thompson 
1300f113e5dcSJeremy L Thompson   // Cleanup
13012b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_project));
13022b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_project));
13032b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref));
13042b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight));
1305f113e5dcSJeremy L Thompson 
1306f113e5dcSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1307f113e5dcSJeremy L Thompson }
1308f113e5dcSJeremy L Thompson 
1309f113e5dcSJeremy L Thompson /**
1310ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedBasis.
13119560d06aSjeremylt 
1312512bb800SJeremy 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.
1313512bb800SJeremy L Thompson         This CeedBasis will be destroyed if `basis_copy` is the only reference to this CeedBasis.
1314ea61e9acSJeremy L Thompson 
1315ea61e9acSJeremy L Thompson   @param[in]     basis      CeedBasis to copy reference to
1316ea61e9acSJeremy L Thompson   @param[in,out] basis_copy Variable to store copied reference
13179560d06aSjeremylt 
13189560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
13199560d06aSjeremylt 
13209560d06aSjeremylt   @ref User
13219560d06aSjeremylt **/
13229560d06aSjeremylt int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) {
1323393ac2cdSJeremy L Thompson   if (basis != CEED_BASIS_COLLOCATED) CeedCall(CeedBasisReference(basis));
13242b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(basis_copy));
13259560d06aSjeremylt   *basis_copy = basis;
13269560d06aSjeremylt   return CEED_ERROR_SUCCESS;
13279560d06aSjeremylt }
13289560d06aSjeremylt 
13299560d06aSjeremylt /**
13307a982d89SJeremy L. Thompson   @brief View a CeedBasis
13317a982d89SJeremy L. Thompson 
1332ea61e9acSJeremy L Thompson   @param[in] basis  CeedBasis to view
1333ea61e9acSJeremy L Thompson   @param[in] stream Stream to view to, e.g., stdout
13347a982d89SJeremy L. Thompson 
13357a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
13367a982d89SJeremy L. Thompson 
13377a982d89SJeremy L. Thompson   @ref User
13387a982d89SJeremy L. Thompson **/
13397a982d89SJeremy L. Thompson int CeedBasisView(CeedBasis basis, FILE *stream) {
134050c301a5SRezgar Shakeri   CeedElemTopology topo     = basis->topo;
1341c4e3f59bSSebastian Grimberg   CeedFESpace      fe_space = basis->fe_space;
1342c4e3f59bSSebastian Grimberg   CeedInt          q_comp   = 0;
13432b730f8bSJeremy L Thompson 
134450c301a5SRezgar Shakeri   // Print FE space and element topology of the basis
1345edf04919SJeremy L Thompson   fprintf(stream, "CeedBasis in a %s on a %s element\n", CeedFESpaces[fe_space], CeedElemTopologies[topo]);
13466402da51SJeremy L Thompson   if (basis->is_tensor_basis) {
1347edf04919SJeremy L Thompson     fprintf(stream, "  P: %" CeedInt_FMT "\n  Q: %" CeedInt_FMT "\n", basis->P_1d, basis->Q_1d);
134850c301a5SRezgar Shakeri   } else {
1349edf04919SJeremy L Thompson     fprintf(stream, "  P: %" CeedInt_FMT "\n  Q: %" CeedInt_FMT "\n", basis->P, basis->Q);
135050c301a5SRezgar Shakeri   }
1351edf04919SJeremy L Thompson   fprintf(stream, "  dimension: %" CeedInt_FMT "\n  field components: %" CeedInt_FMT "\n", basis->dim, basis->num_comp);
1352ea61e9acSJeremy L Thompson   // Print quadrature data, interpolation/gradient/divergence/curl of the basis
13536402da51SJeremy L Thompson   if (basis->is_tensor_basis) {  // tensor basis
13542b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("qref1d", "\t% 12.8f", 1, basis->Q_1d, basis->q_ref_1d, stream));
13552b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("qweight1d", "\t% 12.8f", 1, basis->Q_1d, basis->q_weight_1d, stream));
13562b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("interp1d", "\t% 12.8f", basis->Q_1d, basis->P_1d, basis->interp_1d, stream));
13572b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("grad1d", "\t% 12.8f", basis->Q_1d, basis->P_1d, basis->grad_1d, stream));
135850c301a5SRezgar Shakeri   } else {  // non-tensor basis
13592b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("qref", "\t% 12.8f", 1, basis->Q * basis->dim, basis->q_ref_1d, stream));
13602b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("qweight", "\t% 12.8f", 1, basis->Q, basis->q_weight_1d, stream));
1361c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp));
1362c4e3f59bSSebastian Grimberg     CeedCall(CeedScalarView("interp", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->interp, stream));
136350c301a5SRezgar Shakeri     if (basis->grad) {
1364c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp));
1365c4e3f59bSSebastian Grimberg       CeedCall(CeedScalarView("grad", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->grad, stream));
13667a982d89SJeremy L. Thompson     }
136750c301a5SRezgar Shakeri     if (basis->div) {
1368c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp));
1369c4e3f59bSSebastian Grimberg       CeedCall(CeedScalarView("div", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->div, stream));
1370c4e3f59bSSebastian Grimberg     }
1371c4e3f59bSSebastian Grimberg     if (basis->curl) {
1372c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp));
1373c4e3f59bSSebastian Grimberg       CeedCall(CeedScalarView("curl", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->curl, stream));
137450c301a5SRezgar Shakeri     }
137550c301a5SRezgar Shakeri   }
1376e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13777a982d89SJeremy L. Thompson }
13787a982d89SJeremy L. Thompson 
13797a982d89SJeremy L. Thompson /**
13807a982d89SJeremy L. Thompson   @brief Apply basis evaluation from nodes to quadrature points or vice versa
13817a982d89SJeremy L. Thompson 
1382ea61e9acSJeremy L Thompson   @param[in]  basis      CeedBasis to evaluate
1383ea61e9acSJeremy L Thompson   @param[in]  num_elem   The number of elements to apply the basis evaluation to;
1384ea61e9acSJeremy L Thompson                            the backend will specify the ordering in CeedElemRestrictionCreateBlocked()
1385ea61e9acSJeremy L Thompson   @param[in]  t_mode    \ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points;
1386ea61e9acSJeremy L Thompson                           \ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes
1387ea61e9acSJeremy L Thompson   @param[in]  eval_mode \ref CEED_EVAL_NONE to use values directly,
13887a982d89SJeremy L. Thompson                           \ref CEED_EVAL_INTERP to use interpolated values,
13897a982d89SJeremy L. Thompson                           \ref CEED_EVAL_GRAD to use gradients,
1390c4e3f59bSSebastian Grimberg                           \ref CEED_EVAL_DIV to use divergence,
1391c4e3f59bSSebastian Grimberg                           \ref CEED_EVAL_CURL to use curl,
13927a982d89SJeremy L. Thompson                           \ref CEED_EVAL_WEIGHT to use quadrature weights.
13937a982d89SJeremy L. Thompson   @param[in]  u        Input CeedVector
13947a982d89SJeremy L. Thompson   @param[out] v        Output CeedVector
13957a982d89SJeremy L. Thompson 
13967a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
13977a982d89SJeremy L. Thompson 
13987a982d89SJeremy L. Thompson   @ref User
13997a982d89SJeremy L. Thompson **/
14002b730f8bSJeremy L Thompson int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) {
14011f9221feSJeremy L Thompson   CeedSize u_length = 0, v_length;
1402c4e3f59bSSebastian Grimberg   CeedInt  dim, num_comp, q_comp, num_nodes, num_qpts;
14032b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
14042b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
1405c4e3f59bSSebastian Grimberg   CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
14062b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
14072b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
14082b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(v, &v_length));
1409c8c3fa7dSJeremy L Thompson   if (u) CeedCall(CeedVectorGetLength(u, &u_length));
14107a982d89SJeremy L. Thompson 
14116574a04fSJeremy L Thompson   CeedCheck(basis->Apply, basis->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisApply");
1412e15f9bd0SJeremy L Thompson 
1413e15f9bd0SJeremy L Thompson   // Check compatibility of topological and geometrical dimensions
14146574a04fSJeremy L Thompson   CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0 && u_length % num_qpts == 0) ||
14156574a04fSJeremy L Thompson                 (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0 && v_length % num_qpts == 0),
14166574a04fSJeremy L Thompson             basis->ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions");
14177a982d89SJeremy L. Thompson 
1418e15f9bd0SJeremy L Thompson   // Check vector lengths to prevent out of bounds issues
14196574a04fSJeremy L Thompson   bool good_dims = true;
1420d1d35e2fSjeremylt   switch (eval_mode) {
1421e15f9bd0SJeremy L Thompson     case CEED_EVAL_NONE:
14222b730f8bSJeremy L Thompson     case CEED_EVAL_INTERP:
14232b730f8bSJeremy L Thompson     case CEED_EVAL_GRAD:
1424c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
1425c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
14266574a04fSJeremy L Thompson       good_dims =
14276574a04fSJeremy L Thompson           ((t_mode == CEED_TRANSPOSE && u_length >= num_elem * num_comp * num_qpts * q_comp && v_length >= num_elem * num_comp * num_nodes) ||
14286574a04fSJeremy L Thompson            (t_mode == CEED_NOTRANSPOSE && v_length >= num_elem * num_qpts * num_comp * q_comp && u_length >= num_elem * num_comp * num_nodes));
1429e15f9bd0SJeremy L Thompson       break;
1430e15f9bd0SJeremy L Thompson     case CEED_EVAL_WEIGHT:
14316574a04fSJeremy L Thompson       good_dims = v_length >= num_elem * num_qpts;
1432e15f9bd0SJeremy L Thompson       break;
1433e15f9bd0SJeremy L Thompson   }
14346574a04fSJeremy L Thompson   CeedCheck(good_dims, basis->ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode");
1435e15f9bd0SJeremy L Thompson 
14362b730f8bSJeremy L Thompson   CeedCall(basis->Apply(basis, num_elem, t_mode, eval_mode, u, v));
1437e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
14387a982d89SJeremy L. Thompson }
14397a982d89SJeremy L. Thompson 
14407a982d89SJeremy L. Thompson /**
1441c8c3fa7dSJeremy L Thompson   @brief Apply basis evaluation from nodes to arbitrary points
1442c8c3fa7dSJeremy L Thompson 
1443c8c3fa7dSJeremy L Thompson   @param[in]  basis      CeedBasis to evaluate
1444c8c3fa7dSJeremy L Thompson   @param[in]  num_points The number of points to apply the basis evaluation to
1445c8c3fa7dSJeremy L Thompson   @param[in]  t_mode    \ref CEED_NOTRANSPOSE to evaluate from nodes to points;
1446c8c3fa7dSJeremy L Thompson                           \ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes
1447c8c3fa7dSJeremy L Thompson   @param[in]  eval_mode \ref CEED_EVAL_INTERP to use interpolated values,
1448c8c3fa7dSJeremy L Thompson                           \ref CEED_EVAL_GRAD to use gradients
1449c8c3fa7dSJeremy L Thompson   @param[in]  x_ref    CeedVector holding reference coordinates of each point
1450c8c3fa7dSJeremy L Thompson   @param[in]  u        Input CeedVector, of length `num_nodes * num_comp` for `CEED_NOTRANSPOSE`
1451c8c3fa7dSJeremy L Thompson   @param[out] v        Output CeedVector, of length `num_points * num_q_comp` for `CEED_NOTRANSPOSE` with `CEED_EVAL_INTERP`
1452c8c3fa7dSJeremy L Thompson 
1453c8c3fa7dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1454c8c3fa7dSJeremy L Thompson 
1455c8c3fa7dSJeremy L Thompson   @ref User
1456c8c3fa7dSJeremy L Thompson **/
1457c8c3fa7dSJeremy L Thompson int CeedBasisApplyAtPoints(CeedBasis basis, CeedInt num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u,
1458c8c3fa7dSJeremy L Thompson                            CeedVector v) {
1459c8c3fa7dSJeremy L Thompson   CeedSize x_length = 0, u_length = 0, v_length;
1460c8c3fa7dSJeremy L Thompson   CeedInt  dim, num_comp, num_q_comp, num_nodes, P_1d = 1, Q_1d = 1;
1461c8c3fa7dSJeremy L Thompson 
1462c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
1463c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
1464c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
1465c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
1466c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &num_q_comp));
1467c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
1468c8c3fa7dSJeremy L Thompson   CeedCall(CeedVectorGetLength(x_ref, &x_length));
1469c8c3fa7dSJeremy L Thompson   CeedCall(CeedVectorGetLength(v, &v_length));
1470c8c3fa7dSJeremy L Thompson   CeedCall(CeedVectorGetLength(u, &u_length));
1471c8c3fa7dSJeremy L Thompson 
1472c8c3fa7dSJeremy L Thompson   // Check compatibility of topological and geometrical dimensions
1473c8c3fa7dSJeremy L Thompson   CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0) || (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0), basis->ceed,
1474c8c3fa7dSJeremy L Thompson             CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions and number of points");
1475c8c3fa7dSJeremy L Thompson 
1476c8c3fa7dSJeremy L Thompson   // Check compatibility coordinates vector
1477c8c3fa7dSJeremy L Thompson   CeedCheck(x_length >= num_points * dim, basis->ceed, CEED_ERROR_DIMENSION,
1478c8c3fa7dSJeremy L Thompson             "Length of reference coordinate vector incompatible with basis dimension and number of points");
1479c8c3fa7dSJeremy L Thompson 
1480c8c3fa7dSJeremy L Thompson   // Check vector lengths to prevent out of bounds issues
1481c8c3fa7dSJeremy L Thompson   bool good_dims = false;
1482c8c3fa7dSJeremy L Thompson   switch (eval_mode) {
1483c8c3fa7dSJeremy L Thompson     case CEED_EVAL_INTERP:
1484c8c3fa7dSJeremy L Thompson       good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= num_points * num_q_comp || v_length >= num_nodes * num_comp)) ||
1485c8c3fa7dSJeremy L Thompson                    (t_mode == CEED_NOTRANSPOSE && (v_length >= num_points * num_q_comp || u_length >= num_nodes * num_comp)));
1486c8c3fa7dSJeremy L Thompson       break;
1487c8c3fa7dSJeremy L Thompson     case CEED_EVAL_GRAD:
1488edfbf3a6SJeremy L Thompson       good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= num_points * num_q_comp * dim || v_length >= num_nodes * num_comp)) ||
1489edfbf3a6SJeremy L Thompson                    (t_mode == CEED_NOTRANSPOSE && (v_length >= num_points * num_q_comp * dim || u_length >= num_nodes * num_comp)));
1490edfbf3a6SJeremy L Thompson       break;
1491c8c3fa7dSJeremy L Thompson     case CEED_EVAL_NONE:
1492c8c3fa7dSJeremy L Thompson     case CEED_EVAL_WEIGHT:
1493c8c3fa7dSJeremy L Thompson     case CEED_EVAL_DIV:
1494c8c3fa7dSJeremy L Thompson     case CEED_EVAL_CURL:
1495c8c3fa7dSJeremy L Thompson       // LCOV_EXCL_START
1496c8c3fa7dSJeremy L Thompson       return CeedError(basis->ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points not supported for %s", CeedEvalModes[eval_mode]);
1497c8c3fa7dSJeremy L Thompson       // LCOV_EXCL_STOP
1498c8c3fa7dSJeremy L Thompson   }
1499c8c3fa7dSJeremy L Thompson   CeedCheck(good_dims, basis->ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode");
1500c8c3fa7dSJeremy L Thompson 
1501c8c3fa7dSJeremy L Thompson   // Backend method
1502c8c3fa7dSJeremy L Thompson   if (basis->ApplyAtPoints) {
1503c8c3fa7dSJeremy L Thompson     CeedCall(basis->ApplyAtPoints(basis, num_points, t_mode, eval_mode, x_ref, u, v));
1504c8c3fa7dSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1505c8c3fa7dSJeremy L Thompson   }
1506c8c3fa7dSJeremy L Thompson 
1507c8c3fa7dSJeremy L Thompson   // Default implementation
1508c8c3fa7dSJeremy L Thompson   CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points only supported for tensor product bases");
1509edfbf3a6SJeremy L Thompson   CeedCheck(eval_mode == CEED_EVAL_INTERP || t_mode == CEED_NOTRANSPOSE, basis->ceed, CEED_ERROR_UNSUPPORTED, "%s evaluation only supported for %s",
1510edfbf3a6SJeremy L Thompson             CeedEvalModes[eval_mode], CeedTransposeModes[CEED_NOTRANSPOSE]);
1511c8c3fa7dSJeremy L Thompson   if (!basis->basis_chebyshev) {
1512c8c3fa7dSJeremy L Thompson     // Build matrix mapping from quadrature point values to Chebyshev coefficients
1513c8c3fa7dSJeremy L Thompson     CeedScalar       *tau, *C, *I, *chebyshev_coeffs_1d;
1514c8c3fa7dSJeremy L Thompson     const CeedScalar *q_ref_1d;
1515c8c3fa7dSJeremy L Thompson 
1516c8c3fa7dSJeremy L Thompson     // Build coefficient matrix
1517c8c3fa7dSJeremy L Thompson     // -- Note: Clang-tidy needs this check because it does not understand the is_tensor_basis check above
1518c8c3fa7dSJeremy L Thompson     CeedCheck(P_1d > 0 && Q_1d > 0, basis->ceed, CEED_ERROR_INCOMPATIBLE, "Basis dimensions are malformed");
1519c8c3fa7dSJeremy L Thompson     CeedCall(CeedCalloc(Q_1d * Q_1d, &C));
1520c8c3fa7dSJeremy L Thompson     CeedCall(CeedBasisGetQRef(basis, &q_ref_1d));
15213778dbaaSJeremy L Thompson     for (CeedInt i = 0; i < Q_1d; i++) CeedCall(CeedChebyshevPolynomialsAtPoint(q_ref_1d[i], Q_1d, &C[i * Q_1d]));
1522c8c3fa7dSJeremy L Thompson 
1523c8c3fa7dSJeremy L Thompson     // Inverse of coefficient matrix
1524c8c3fa7dSJeremy L Thompson     CeedCall(CeedCalloc(Q_1d * Q_1d, &chebyshev_coeffs_1d));
1525c8c3fa7dSJeremy L Thompson     CeedCall(CeedCalloc(Q_1d * Q_1d, &I));
1526c8c3fa7dSJeremy L Thompson     CeedCall(CeedCalloc(Q_1d, &tau));
1527c8c3fa7dSJeremy L Thompson     // -- QR Factorization, C = Q R
1528c8c3fa7dSJeremy L Thompson     CeedCall(CeedQRFactorization(basis->ceed, C, tau, Q_1d, Q_1d));
1529c8c3fa7dSJeremy L Thompson     // -- chebyshev_coeffs_1d = R_inv Q^T
1530c8c3fa7dSJeremy L Thompson     for (CeedInt i = 0; i < Q_1d; i++) I[i * Q_1d + i] = 1.0;
1531c8c3fa7dSJeremy L Thompson     // ---- Apply R_inv, chebyshev_coeffs_1d = I R_inv
1532c8c3fa7dSJeremy L Thompson     for (CeedInt i = 0; i < Q_1d; i++) {  // Row i
1533c8c3fa7dSJeremy L Thompson       chebyshev_coeffs_1d[Q_1d * i] = I[Q_1d * i] / C[0];
1534c8c3fa7dSJeremy L Thompson       for (CeedInt j = 1; j < Q_1d; j++) {  // Column j
1535c8c3fa7dSJeremy L Thompson         chebyshev_coeffs_1d[j + Q_1d * i] = I[j + Q_1d * i];
1536c8c3fa7dSJeremy 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];
1537c8c3fa7dSJeremy L Thompson         chebyshev_coeffs_1d[j + Q_1d * i] /= C[j + Q_1d * j];
1538c8c3fa7dSJeremy L Thompson       }
1539c8c3fa7dSJeremy L Thompson     }
1540c8c3fa7dSJeremy L Thompson     // ---- Apply Q^T, chebyshev_coeffs_1d = R_inv Q^T
1541c8c3fa7dSJeremy L Thompson     CeedCall(CeedHouseholderApplyQ(chebyshev_coeffs_1d, C, tau, CEED_NOTRANSPOSE, Q_1d, Q_1d, Q_1d, 1, Q_1d));
1542c8c3fa7dSJeremy L Thompson 
1543c8c3fa7dSJeremy L Thompson     // Build basis mapping from nodes to Chebyshev coefficients
1544c8c3fa7dSJeremy L Thompson     CeedScalar       *chebyshev_interp_1d, *chebyshev_grad_1d, *chebyshev_q_weight_1d;
1545c8c3fa7dSJeremy L Thompson     const CeedScalar *interp_1d;
1546c8c3fa7dSJeremy L Thompson 
1547*71a83b88SJeremy L Thompson     CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
1548*71a83b88SJeremy L Thompson     CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_grad_1d));
1549c8c3fa7dSJeremy L Thompson     CeedCall(CeedCalloc(Q_1d, &chebyshev_q_weight_1d));
1550c8c3fa7dSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
1551c8c3fa7dSJeremy L Thompson     CeedCall(CeedMatrixMatrixMultiply(basis->ceed, chebyshev_coeffs_1d, interp_1d, chebyshev_interp_1d, Q_1d, P_1d, Q_1d));
1552c8c3fa7dSJeremy L Thompson 
1553c8c3fa7dSJeremy L Thompson     CeedCall(CeedVectorCreate(basis->ceed, num_comp * CeedIntPow(Q_1d, dim), &basis->vec_chebyshev));
1554*71a83b88SJeremy 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,
1555c8c3fa7dSJeremy L Thompson                                      &basis->basis_chebyshev));
1556c8c3fa7dSJeremy L Thompson 
1557c8c3fa7dSJeremy L Thompson     // Cleanup
1558c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&C));
1559c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&chebyshev_coeffs_1d));
1560c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&I));
1561c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&tau));
1562c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&chebyshev_interp_1d));
1563c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&chebyshev_grad_1d));
1564c8c3fa7dSJeremy L Thompson     CeedCall(CeedFree(&chebyshev_q_weight_1d));
1565c8c3fa7dSJeremy L Thompson   }
1566c8c3fa7dSJeremy L Thompson 
1567c8c3fa7dSJeremy L Thompson   // Create TensorContract object if needed, such as a basis from the GPU backends
1568c8c3fa7dSJeremy L Thompson   if (!basis->contract) {
1569c8c3fa7dSJeremy L Thompson     Ceed      ceed_ref;
1570585a562dSJeremy L Thompson     CeedBasis basis_ref = NULL;
1571c8c3fa7dSJeremy L Thompson 
1572c8c3fa7dSJeremy L Thompson     CeedCall(CeedInit("/cpu/self", &ceed_ref));
1573c8c3fa7dSJeremy L Thompson     // Only need matching tensor contraction dimensions, any type of basis will work
1574*71a83b88SJeremy L Thompson     CeedCall(CeedBasisCreateTensorH1Lagrange(ceed_ref, dim, num_comp, P_1d, Q_1d, CEED_GAUSS, &basis_ref));
1575585a562dSJeremy L Thompson     // Note - clang-tidy doesn't know basis_ref->contract must be valid here
1576585a562dSJeremy L Thompson     CeedCheck(basis_ref && basis_ref->contract, basis->ceed, CEED_ERROR_UNSUPPORTED,
1577585a562dSJeremy L Thompson               "Refrence CPU ceed failed to create a tensor contraction object");
1578585a562dSJeremy L Thompson     CeedCall(CeedTensorContractReferenceCopy(basis_ref->contract, &basis->contract));
1579c8c3fa7dSJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis_ref));
1580c8c3fa7dSJeremy L Thompson     CeedCall(CeedDestroy(&ceed_ref));
1581c8c3fa7dSJeremy L Thompson   }
1582c8c3fa7dSJeremy L Thompson 
1583c8c3fa7dSJeremy L Thompson   // Basis evaluation
1584c8c3fa7dSJeremy L Thompson   switch (t_mode) {
1585c8c3fa7dSJeremy L Thompson     case CEED_NOTRANSPOSE: {
1586c8c3fa7dSJeremy L Thompson       // Nodes to arbitrary points
1587c8c3fa7dSJeremy L Thompson       CeedScalar       *v_array;
1588c8c3fa7dSJeremy L Thompson       const CeedScalar *chebyshev_coeffs, *x_array_read;
1589c8c3fa7dSJeremy L Thompson 
1590c8c3fa7dSJeremy L Thompson       // -- Interpolate to Chebyshev coefficients
1591c8c3fa7dSJeremy L Thompson       CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, basis->vec_chebyshev));
1592c8c3fa7dSJeremy L Thompson 
1593c8c3fa7dSJeremy L Thompson       // -- Evaluate Chebyshev polynomials at arbitrary points
1594c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs));
1595c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read));
1596c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorGetArrayWrite(v, CEED_MEM_HOST, &v_array));
1597edfbf3a6SJeremy L Thompson       switch (eval_mode) {
1598edfbf3a6SJeremy L Thompson         case CEED_EVAL_INTERP: {
1599c8c3fa7dSJeremy L Thompson           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
1600c8c3fa7dSJeremy L Thompson 
1601c8c3fa7dSJeremy L Thompson           // ---- Values at point
1602c8c3fa7dSJeremy L Thompson           for (CeedInt p = 0; p < num_points; p++) {
1603c8c3fa7dSJeremy L Thompson             CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1;
1604c8c3fa7dSJeremy L Thompson 
16055ea233a5SJeremy L Thompson             // Note: stepping "backwards" through the tensor contractions to agree with the ordering of the Chebyshev coefficients
1606c8c3fa7dSJeremy L Thompson             for (CeedInt d = dim - 1; d >= 0; d--) {
16073778dbaaSJeremy L Thompson               // ------ Tensor contract with current Chebyshev polynomial values
16083778dbaaSJeremy L Thompson               CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[p * dim + d], Q_1d, chebyshev_x));
1609c8c3fa7dSJeremy L Thompson               CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false,
1610c8c3fa7dSJeremy L Thompson                                                d == (dim - 1) ? chebyshev_coeffs : tmp[d % 2], d == 0 ? &v_array[p * num_comp] : tmp[(d + 1) % 2]));
1611c8c3fa7dSJeremy L Thompson               pre /= Q_1d;
1612c8c3fa7dSJeremy L Thompson               post *= 1;
1613c8c3fa7dSJeremy L Thompson             }
1614c8c3fa7dSJeremy L Thompson           }
1615edfbf3a6SJeremy L Thompson           break;
1616edfbf3a6SJeremy L Thompson         }
1617edfbf3a6SJeremy L Thompson         case CEED_EVAL_GRAD: {
1618edfbf3a6SJeremy L Thompson           CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
1619edfbf3a6SJeremy L Thompson 
1620edfbf3a6SJeremy L Thompson           // ---- Values at point
1621edfbf3a6SJeremy L Thompson           for (CeedInt p = 0; p < num_points; p++) {
16225ea233a5SJeremy L Thompson             // Note: stepping "backwards" through the tensor contractions to agree with the ordering of the Chebyshev coefficients
1623edfbf3a6SJeremy L Thompson             // Dim**2 contractions, apply grad when pass == dim
1624edfbf3a6SJeremy L Thompson             for (CeedInt pass = dim - 1; pass >= 0; pass--) {
1625edfbf3a6SJeremy L Thompson               CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1;
1626edfbf3a6SJeremy L Thompson 
1627edfbf3a6SJeremy L Thompson               for (CeedInt d = dim - 1; d >= 0; d--) {
16283778dbaaSJeremy L Thompson                 // ------ Tensor contract with current Chebyshev polynomial values
16293778dbaaSJeremy L Thompson                 if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[p * dim + d], Q_1d, chebyshev_x));
16303778dbaaSJeremy L Thompson                 else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[p * dim + d], Q_1d, chebyshev_x));
1631edfbf3a6SJeremy L Thompson                 CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false,
1632edfbf3a6SJeremy L Thompson                                                  d == (dim - 1) ? chebyshev_coeffs : tmp[d % 2],
1633edfbf3a6SJeremy L Thompson                                                  d == 0 ? &v_array[p * num_comp * dim + pass] : tmp[(d + 1) % 2]));
1634edfbf3a6SJeremy L Thompson                 pre /= Q_1d;
1635edfbf3a6SJeremy L Thompson                 post *= 1;
1636edfbf3a6SJeremy L Thompson               }
1637edfbf3a6SJeremy L Thompson             }
1638edfbf3a6SJeremy L Thompson           }
1639edfbf3a6SJeremy L Thompson           break;
1640edfbf3a6SJeremy L Thompson         }
1641edfbf3a6SJeremy L Thompson         default:
1642edfbf3a6SJeremy L Thompson           // Nothing to do, this won't occur
1643edfbf3a6SJeremy L Thompson           break;
1644c8c3fa7dSJeremy L Thompson       }
1645c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(basis->vec_chebyshev, &chebyshev_coeffs));
1646c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read));
1647c8c3fa7dSJeremy L Thompson       CeedCall(CeedVectorRestoreArray(v, &v_array));
1648c8c3fa7dSJeremy L Thompson       break;
1649c8c3fa7dSJeremy L Thompson     }
16502a94f45fSJeremy L Thompson     case CEED_TRANSPOSE: {
16513778dbaaSJeremy L Thompson       // Note: No switch on e_mode here because only CEED_EVAL_INTERP is supported at this time
16522a94f45fSJeremy L Thompson       // Arbitrary points to nodes
16532a94f45fSJeremy L Thompson       CeedScalar       *chebyshev_coeffs;
16542a94f45fSJeremy L Thompson       const CeedScalar *u_array, *x_array_read;
16552a94f45fSJeremy L Thompson 
16562a94f45fSJeremy L Thompson       // -- Transpose of evaluaton of Chebyshev polynomials at arbitrary points
16572a94f45fSJeremy L Thompson       CeedCall(CeedVectorGetArrayWrite(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs));
16582a94f45fSJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read));
16592a94f45fSJeremy L Thompson       CeedCall(CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array));
16602a94f45fSJeremy L Thompson       {
16612a94f45fSJeremy L Thompson         CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d];
16622a94f45fSJeremy L Thompson 
16632a94f45fSJeremy L Thompson         // ---- Values at point
16642a94f45fSJeremy L Thompson         for (CeedInt p = 0; p < num_points; p++) {
16652a94f45fSJeremy L Thompson           CeedInt pre = num_comp * 1, post = 1;
16662a94f45fSJeremy L Thompson 
16675ea233a5SJeremy L Thompson           // Note: stepping "backwards" through the tensor contractions to agree with the ordering of the Chebyshev coefficients
16682a94f45fSJeremy L Thompson           for (CeedInt d = dim - 1; d >= 0; d--) {
16693778dbaaSJeremy L Thompson             // ------ Tensor contract with current Chebyshev polynomial values
16703778dbaaSJeremy L Thompson             CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[p * dim + d], Q_1d, chebyshev_x));
16712a94f45fSJeremy L Thompson             CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, p > 0 && d == 0,
16722a94f45fSJeremy L Thompson                                              d == (dim - 1) ? &u_array[p * num_comp] : tmp[d % 2], d == 0 ? chebyshev_coeffs : tmp[(d + 1) % 2]));
16732a94f45fSJeremy L Thompson             pre /= 1;
16742a94f45fSJeremy L Thompson             post *= Q_1d;
16752a94f45fSJeremy L Thompson           }
16762a94f45fSJeremy L Thompson         }
16772a94f45fSJeremy L Thompson       }
16782a94f45fSJeremy L Thompson       CeedCall(CeedVectorRestoreArray(basis->vec_chebyshev, &chebyshev_coeffs));
16792a94f45fSJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read));
16802a94f45fSJeremy L Thompson       CeedCall(CeedVectorRestoreArrayRead(u, &u_array));
16812a94f45fSJeremy L Thompson 
16822a94f45fSJeremy L Thompson       // -- Interpolate transpose from Chebyshev coefficients
16832a94f45fSJeremy L Thompson       CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v));
16842a94f45fSJeremy L Thompson       break;
16852a94f45fSJeremy L Thompson     }
1686c8c3fa7dSJeremy L Thompson   }
1687c8c3fa7dSJeremy L Thompson 
1688c8c3fa7dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1689c8c3fa7dSJeremy L Thompson }
1690c8c3fa7dSJeremy L Thompson 
1691c8c3fa7dSJeremy L Thompson /**
1692b7c9bbdaSJeremy L Thompson   @brief Get Ceed associated with a CeedBasis
1693b7c9bbdaSJeremy L Thompson 
1694ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
1695b7c9bbdaSJeremy L Thompson   @param[out] ceed  Variable to store Ceed
1696b7c9bbdaSJeremy L Thompson 
1697b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1698b7c9bbdaSJeremy L Thompson 
1699b7c9bbdaSJeremy L Thompson   @ref Advanced
1700b7c9bbdaSJeremy L Thompson **/
1701b7c9bbdaSJeremy L Thompson int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) {
1702b7c9bbdaSJeremy L Thompson   *ceed = basis->ceed;
1703b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1704b7c9bbdaSJeremy L Thompson }
1705b7c9bbdaSJeremy L Thompson 
1706b7c9bbdaSJeremy L Thompson /**
17079d007619Sjeremylt   @brief Get dimension for given CeedBasis
17089d007619Sjeremylt 
1709ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
17109d007619Sjeremylt   @param[out] dim   Variable to store dimension of basis
17119d007619Sjeremylt 
17129d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
17139d007619Sjeremylt 
1714b7c9bbdaSJeremy L Thompson   @ref Advanced
17159d007619Sjeremylt **/
17169d007619Sjeremylt int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) {
17179d007619Sjeremylt   *dim = basis->dim;
1718e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17199d007619Sjeremylt }
17209d007619Sjeremylt 
17219d007619Sjeremylt /**
1722d99fa3c5SJeremy L Thompson   @brief Get topology for given CeedBasis
1723d99fa3c5SJeremy L Thompson 
1724ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
1725d99fa3c5SJeremy L Thompson   @param[out] topo  Variable to store topology of basis
1726d99fa3c5SJeremy L Thompson 
1727d99fa3c5SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1728d99fa3c5SJeremy L Thompson 
1729b7c9bbdaSJeremy L Thompson   @ref Advanced
1730d99fa3c5SJeremy L Thompson **/
1731d99fa3c5SJeremy L Thompson int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) {
1732d99fa3c5SJeremy L Thompson   *topo = basis->topo;
1733e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1734d99fa3c5SJeremy L Thompson }
1735d99fa3c5SJeremy L Thompson 
1736d99fa3c5SJeremy L Thompson /**
17379d007619Sjeremylt   @brief Get number of components for given CeedBasis
17389d007619Sjeremylt 
1739ea61e9acSJeremy L Thompson   @param[in]  basis    CeedBasis
1740d1d35e2fSjeremylt   @param[out] num_comp Variable to store number of components of basis
17419d007619Sjeremylt 
17429d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
17439d007619Sjeremylt 
1744b7c9bbdaSJeremy L Thompson   @ref Advanced
17459d007619Sjeremylt **/
1746d1d35e2fSjeremylt int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) {
1747d1d35e2fSjeremylt   *num_comp = basis->num_comp;
1748e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17499d007619Sjeremylt }
17509d007619Sjeremylt 
17519d007619Sjeremylt /**
17529d007619Sjeremylt   @brief Get total number of nodes (in dim dimensions) of a CeedBasis
17539d007619Sjeremylt 
1754ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
17559d007619Sjeremylt   @param[out] P     Variable to store number of nodes
17569d007619Sjeremylt 
17579d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
17589d007619Sjeremylt 
17599d007619Sjeremylt   @ref Utility
17609d007619Sjeremylt **/
17619d007619Sjeremylt int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) {
17629d007619Sjeremylt   *P = basis->P;
1763e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17649d007619Sjeremylt }
17659d007619Sjeremylt 
17669d007619Sjeremylt /**
17679d007619Sjeremylt   @brief Get total number of nodes (in 1 dimension) of a CeedBasis
17689d007619Sjeremylt 
1769ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
1770d1d35e2fSjeremylt   @param[out] P_1d  Variable to store number of nodes
17719d007619Sjeremylt 
17729d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
17739d007619Sjeremylt 
1774b7c9bbdaSJeremy L Thompson   @ref Advanced
17759d007619Sjeremylt **/
1776d1d35e2fSjeremylt int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) {
17776402da51SJeremy L Thompson   CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "Cannot supply P_1d for non-tensor basis");
1778d1d35e2fSjeremylt   *P_1d = basis->P_1d;
1779e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17809d007619Sjeremylt }
17819d007619Sjeremylt 
17829d007619Sjeremylt /**
17839d007619Sjeremylt   @brief Get total number of quadrature points (in dim dimensions) of a CeedBasis
17849d007619Sjeremylt 
1785ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
17869d007619Sjeremylt   @param[out] Q     Variable to store number of quadrature points
17879d007619Sjeremylt 
17889d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
17899d007619Sjeremylt 
17909d007619Sjeremylt   @ref Utility
17919d007619Sjeremylt **/
17929d007619Sjeremylt int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) {
17939d007619Sjeremylt   *Q = basis->Q;
1794e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17959d007619Sjeremylt }
17969d007619Sjeremylt 
17979d007619Sjeremylt /**
17989d007619Sjeremylt   @brief Get total number of quadrature points (in 1 dimension) of a CeedBasis
17999d007619Sjeremylt 
1800ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
1801d1d35e2fSjeremylt   @param[out] Q_1d  Variable to store number of quadrature points
18029d007619Sjeremylt 
18039d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18049d007619Sjeremylt 
1805b7c9bbdaSJeremy L Thompson   @ref Advanced
18069d007619Sjeremylt **/
1807d1d35e2fSjeremylt int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) {
18086402da51SJeremy L Thompson   CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "Cannot supply Q_1d for non-tensor basis");
1809d1d35e2fSjeremylt   *Q_1d = basis->Q_1d;
1810e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18119d007619Sjeremylt }
18129d007619Sjeremylt 
18139d007619Sjeremylt /**
1814ea61e9acSJeremy L Thompson   @brief Get reference coordinates of quadrature points (in dim dimensions) of a CeedBasis
18159d007619Sjeremylt 
1816ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
1817d1d35e2fSjeremylt   @param[out] q_ref Variable to store reference coordinates of quadrature points
18189d007619Sjeremylt 
18199d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18209d007619Sjeremylt 
1821b7c9bbdaSJeremy L Thompson   @ref Advanced
18229d007619Sjeremylt **/
1823d1d35e2fSjeremylt int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) {
1824d1d35e2fSjeremylt   *q_ref = basis->q_ref_1d;
1825e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18269d007619Sjeremylt }
18279d007619Sjeremylt 
18289d007619Sjeremylt /**
1829ea61e9acSJeremy L Thompson   @brief Get quadrature weights of quadrature points (in dim dimensions) of a CeedBasis
18309d007619Sjeremylt 
1831ea61e9acSJeremy L Thompson   @param[in]  basis    CeedBasis
1832d1d35e2fSjeremylt   @param[out] q_weight Variable to store quadrature weights
18339d007619Sjeremylt 
18349d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18359d007619Sjeremylt 
1836b7c9bbdaSJeremy L Thompson   @ref Advanced
18379d007619Sjeremylt **/
1838d1d35e2fSjeremylt int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) {
1839d1d35e2fSjeremylt   *q_weight = basis->q_weight_1d;
1840e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18419d007619Sjeremylt }
18429d007619Sjeremylt 
18439d007619Sjeremylt /**
18449d007619Sjeremylt   @brief Get interpolation matrix of a CeedBasis
18459d007619Sjeremylt 
1846ea61e9acSJeremy L Thompson   @param[in]  basis  CeedBasis
18479d007619Sjeremylt   @param[out] interp Variable to store interpolation matrix
18489d007619Sjeremylt 
18499d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18509d007619Sjeremylt 
1851b7c9bbdaSJeremy L Thompson   @ref Advanced
18529d007619Sjeremylt **/
18536c58de82SJeremy L Thompson int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) {
18546402da51SJeremy L Thompson   if (!basis->interp && basis->is_tensor_basis) {
18559d007619Sjeremylt     // Allocate
18562b730f8bSJeremy L Thompson     CeedCall(CeedMalloc(basis->Q * basis->P, &basis->interp));
18579d007619Sjeremylt 
18589d007619Sjeremylt     // Initialize
18592b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < basis->Q * basis->P; i++) basis->interp[i] = 1.0;
18609d007619Sjeremylt 
18619d007619Sjeremylt     // Calculate
18622b730f8bSJeremy L Thompson     for (CeedInt d = 0; d < basis->dim; d++) {
18632b730f8bSJeremy L Thompson       for (CeedInt qpt = 0; qpt < basis->Q; qpt++) {
18649d007619Sjeremylt         for (CeedInt node = 0; node < basis->P; node++) {
1865d1d35e2fSjeremylt           CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d;
1866d1d35e2fSjeremylt           CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d;
1867d1d35e2fSjeremylt           basis->interp[qpt * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p];
18689d007619Sjeremylt         }
18699d007619Sjeremylt       }
18702b730f8bSJeremy L Thompson     }
18712b730f8bSJeremy L Thompson   }
18729d007619Sjeremylt   *interp = basis->interp;
1873e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18749d007619Sjeremylt }
18759d007619Sjeremylt 
18769d007619Sjeremylt /**
18779d007619Sjeremylt   @brief Get 1D interpolation matrix of a tensor product CeedBasis
18789d007619Sjeremylt 
1879ea61e9acSJeremy L Thompson   @param[in]  basis     CeedBasis
1880d1d35e2fSjeremylt   @param[out] interp_1d Variable to store interpolation matrix
18819d007619Sjeremylt 
18829d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18839d007619Sjeremylt 
18849d007619Sjeremylt   @ref Backend
18859d007619Sjeremylt **/
1886d1d35e2fSjeremylt int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) {
18876402da51SJeremy L Thompson   CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "CeedBasis is not a tensor product basis.");
1888d1d35e2fSjeremylt   *interp_1d = basis->interp_1d;
1889e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18909d007619Sjeremylt }
18919d007619Sjeremylt 
18929d007619Sjeremylt /**
18939d007619Sjeremylt   @brief Get gradient matrix of a CeedBasis
18949d007619Sjeremylt 
1895ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
18969d007619Sjeremylt   @param[out] grad  Variable to store gradient matrix
18979d007619Sjeremylt 
18989d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18999d007619Sjeremylt 
1900b7c9bbdaSJeremy L Thompson   @ref Advanced
19019d007619Sjeremylt **/
19026c58de82SJeremy L Thompson int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) {
19036402da51SJeremy L Thompson   if (!basis->grad && basis->is_tensor_basis) {
19049d007619Sjeremylt     // Allocate
19052b730f8bSJeremy L Thompson     CeedCall(CeedMalloc(basis->dim * basis->Q * basis->P, &basis->grad));
19069d007619Sjeremylt 
19079d007619Sjeremylt     // Initialize
19082b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < basis->dim * basis->Q * basis->P; i++) basis->grad[i] = 1.0;
19099d007619Sjeremylt 
19109d007619Sjeremylt     // Calculate
19112b730f8bSJeremy L Thompson     for (CeedInt d = 0; d < basis->dim; d++) {
19122b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < basis->dim; i++) {
19132b730f8bSJeremy L Thompson         for (CeedInt qpt = 0; qpt < basis->Q; qpt++) {
19149d007619Sjeremylt           for (CeedInt node = 0; node < basis->P; node++) {
1915d1d35e2fSjeremylt             CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d;
1916d1d35e2fSjeremylt             CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d;
19172b730f8bSJeremy L Thompson             if (i == d) basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->grad_1d[q * basis->P_1d + p];
19182b730f8bSJeremy L Thompson             else basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p];
19192b730f8bSJeremy L Thompson           }
19202b730f8bSJeremy L Thompson         }
19212b730f8bSJeremy L Thompson       }
19229d007619Sjeremylt     }
19239d007619Sjeremylt   }
19249d007619Sjeremylt   *grad = basis->grad;
1925e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
19269d007619Sjeremylt }
19279d007619Sjeremylt 
19289d007619Sjeremylt /**
19299d007619Sjeremylt   @brief Get 1D gradient matrix of a tensor product CeedBasis
19309d007619Sjeremylt 
1931ea61e9acSJeremy L Thompson   @param[in]  basis   CeedBasis
1932d1d35e2fSjeremylt   @param[out] grad_1d Variable to store gradient matrix
19339d007619Sjeremylt 
19349d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
19359d007619Sjeremylt 
1936b7c9bbdaSJeremy L Thompson   @ref Advanced
19379d007619Sjeremylt **/
1938d1d35e2fSjeremylt int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) {
19396402da51SJeremy L Thompson   CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "CeedBasis is not a tensor product basis.");
1940d1d35e2fSjeremylt   *grad_1d = basis->grad_1d;
1941e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
19429d007619Sjeremylt }
19439d007619Sjeremylt 
19449d007619Sjeremylt /**
194550c301a5SRezgar Shakeri   @brief Get divergence matrix of a CeedBasis
194650c301a5SRezgar Shakeri 
1947ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
194850c301a5SRezgar Shakeri   @param[out] div   Variable to store divergence matrix
194950c301a5SRezgar Shakeri 
195050c301a5SRezgar Shakeri   @return An error code: 0 - success, otherwise - failure
195150c301a5SRezgar Shakeri 
195250c301a5SRezgar Shakeri   @ref Advanced
195350c301a5SRezgar Shakeri **/
195450c301a5SRezgar Shakeri int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) {
19556574a04fSJeremy L Thompson   CeedCheck(basis->div, basis->ceed, CEED_ERROR_MINOR, "CeedBasis does not have divergence matrix.");
195650c301a5SRezgar Shakeri   *div = basis->div;
195750c301a5SRezgar Shakeri   return CEED_ERROR_SUCCESS;
195850c301a5SRezgar Shakeri }
195950c301a5SRezgar Shakeri 
196050c301a5SRezgar Shakeri /**
1961c4e3f59bSSebastian Grimberg   @brief Get curl matrix of a CeedBasis
1962c4e3f59bSSebastian Grimberg 
1963c4e3f59bSSebastian Grimberg   @param[in]  basis CeedBasis
1964c4e3f59bSSebastian Grimberg   @param[out] curl  Variable to store curl matrix
1965c4e3f59bSSebastian Grimberg 
1966c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1967c4e3f59bSSebastian Grimberg 
1968c4e3f59bSSebastian Grimberg   @ref Advanced
1969c4e3f59bSSebastian Grimberg **/
1970c4e3f59bSSebastian Grimberg int CeedBasisGetCurl(CeedBasis basis, const CeedScalar **curl) {
19716574a04fSJeremy L Thompson   CeedCheck(basis->curl, basis->ceed, CEED_ERROR_MINOR, "CeedBasis does not have curl matrix.");
1972c4e3f59bSSebastian Grimberg   *curl = basis->curl;
1973c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1974c4e3f59bSSebastian Grimberg }
1975c4e3f59bSSebastian Grimberg 
1976c4e3f59bSSebastian Grimberg /**
19777a982d89SJeremy L. Thompson   @brief Destroy a CeedBasis
19787a982d89SJeremy L. Thompson 
1979ea61e9acSJeremy L Thompson   @param[in,out] basis CeedBasis to destroy
19807a982d89SJeremy L. Thompson 
19817a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
19827a982d89SJeremy L. Thompson 
19837a982d89SJeremy L. Thompson   @ref User
19847a982d89SJeremy L. Thompson **/
19857a982d89SJeremy L. Thompson int CeedBasisDestroy(CeedBasis *basis) {
19867425e127SJeremy L Thompson   if (!*basis || *basis == CEED_BASIS_COLLOCATED || --(*basis)->ref_count > 0) {
1987ad6481ceSJeremy L Thompson     *basis = NULL;
1988ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1989ad6481ceSJeremy L Thompson   }
19902b730f8bSJeremy L Thompson   if ((*basis)->Destroy) CeedCall((*basis)->Destroy(*basis));
19919831d45aSJeremy L Thompson   CeedCall(CeedTensorContractDestroy(&(*basis)->contract));
1992c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->q_ref_1d));
1993c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->q_weight_1d));
19942b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->interp));
19952b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->interp_1d));
19962b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->grad));
19972b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->grad_1d));
1998c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->div));
1999c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->curl));
2000c8c3fa7dSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*basis)->vec_chebyshev));
2001c8c3fa7dSJeremy L Thompson   CeedCall(CeedBasisDestroy(&(*basis)->basis_chebyshev));
20022b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*basis)->ceed));
20032b730f8bSJeremy L Thompson   CeedCall(CeedFree(basis));
2004e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
20057a982d89SJeremy L. Thompson }
20067a982d89SJeremy L. Thompson 
20077a982d89SJeremy L. Thompson /**
2008b11c1e72Sjeremylt   @brief Construct a Gauss-Legendre quadrature
2009b11c1e72Sjeremylt 
2010ea61e9acSJeremy L Thompson   @param[in]  Q           Number of quadrature points (integrates polynomials of degree 2*Q-1 exactly)
2011d1d35e2fSjeremylt   @param[out] q_ref_1d    Array of length Q to hold the abscissa on [-1, 1]
2012d1d35e2fSjeremylt   @param[out] q_weight_1d Array of length Q to hold the weights
2013b11c1e72Sjeremylt 
2014b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2015dfdf5a53Sjeremylt 
2016dfdf5a53Sjeremylt   @ref Utility
2017b11c1e72Sjeremylt **/
20182b730f8bSJeremy L Thompson int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) {
2019d7b241e6Sjeremylt   // Allocate
2020d7b241e6Sjeremylt   CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0 * atan(1.0);
2021d1d35e2fSjeremylt   // Build q_ref_1d, q_weight_1d
202292ae7e47SJeremy L Thompson   for (CeedInt i = 0; i <= Q / 2; i++) {
2023d7b241e6Sjeremylt     // Guess
2024d7b241e6Sjeremylt     xi = cos(PI * (CeedScalar)(2 * i + 1) / ((CeedScalar)(2 * Q)));
2025d7b241e6Sjeremylt     // Pn(xi)
2026d7b241e6Sjeremylt     P0 = 1.0;
2027d7b241e6Sjeremylt     P1 = xi;
2028d7b241e6Sjeremylt     P2 = 0.0;
202992ae7e47SJeremy L Thompson     for (CeedInt j = 2; j <= Q; j++) {
2030d7b241e6Sjeremylt       P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2031d7b241e6Sjeremylt       P0 = P1;
2032d7b241e6Sjeremylt       P1 = P2;
2033d7b241e6Sjeremylt     }
2034d7b241e6Sjeremylt     // First Newton Step
2035d7b241e6Sjeremylt     dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2036d7b241e6Sjeremylt     xi  = xi - P2 / dP2;
2037d7b241e6Sjeremylt     // Newton to convergence
203892ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < 100 && fabs(P2) > 10 * CEED_EPSILON; k++) {
2039d7b241e6Sjeremylt       P0 = 1.0;
2040d7b241e6Sjeremylt       P1 = xi;
204192ae7e47SJeremy L Thompson       for (CeedInt j = 2; j <= Q; j++) {
2042d7b241e6Sjeremylt         P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2043d7b241e6Sjeremylt         P0 = P1;
2044d7b241e6Sjeremylt         P1 = P2;
2045d7b241e6Sjeremylt       }
2046d7b241e6Sjeremylt       dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2047d7b241e6Sjeremylt       xi  = xi - P2 / dP2;
2048d7b241e6Sjeremylt     }
2049d7b241e6Sjeremylt     // Save xi, wi
2050d7b241e6Sjeremylt     wi                     = 2.0 / ((1.0 - xi * xi) * dP2 * dP2);
2051d1d35e2fSjeremylt     q_weight_1d[i]         = wi;
2052d1d35e2fSjeremylt     q_weight_1d[Q - 1 - i] = wi;
2053d1d35e2fSjeremylt     q_ref_1d[i]            = -xi;
2054d1d35e2fSjeremylt     q_ref_1d[Q - 1 - i]    = xi;
2055d7b241e6Sjeremylt   }
2056e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2057d7b241e6Sjeremylt }
2058d7b241e6Sjeremylt 
2059b11c1e72Sjeremylt /**
2060b11c1e72Sjeremylt   @brief Construct a Gauss-Legendre-Lobatto quadrature
2061b11c1e72Sjeremylt 
2062ea61e9acSJeremy L Thompson   @param[in]  Q           Number of quadrature points (integrates polynomials of degree 2*Q-3 exactly)
2063d1d35e2fSjeremylt   @param[out] q_ref_1d    Array of length Q to hold the abscissa on [-1, 1]
2064d1d35e2fSjeremylt   @param[out] q_weight_1d Array of length Q to hold the weights
2065b11c1e72Sjeremylt 
2066b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2067dfdf5a53Sjeremylt 
2068dfdf5a53Sjeremylt   @ref Utility
2069b11c1e72Sjeremylt **/
20702b730f8bSJeremy L Thompson int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) {
2071d7b241e6Sjeremylt   // Allocate
2072d7b241e6Sjeremylt   CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0 * atan(1.0);
2073d1d35e2fSjeremylt   // Build q_ref_1d, q_weight_1d
2074d7b241e6Sjeremylt   // Set endpoints
20756574a04fSJeremy L Thompson   CeedCheck(Q > 1, NULL, CEED_ERROR_DIMENSION, "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q);
2076d7b241e6Sjeremylt   wi = 2.0 / ((CeedScalar)(Q * (Q - 1)));
2077d1d35e2fSjeremylt   if (q_weight_1d) {
2078d1d35e2fSjeremylt     q_weight_1d[0]     = wi;
2079d1d35e2fSjeremylt     q_weight_1d[Q - 1] = wi;
2080d7b241e6Sjeremylt   }
2081d1d35e2fSjeremylt   q_ref_1d[0]     = -1.0;
2082d1d35e2fSjeremylt   q_ref_1d[Q - 1] = 1.0;
2083d7b241e6Sjeremylt   // Interior
208492ae7e47SJeremy L Thompson   for (CeedInt i = 1; i <= (Q - 1) / 2; i++) {
2085d7b241e6Sjeremylt     // Guess
2086d7b241e6Sjeremylt     xi = cos(PI * (CeedScalar)(i) / (CeedScalar)(Q - 1));
2087d7b241e6Sjeremylt     // Pn(xi)
2088d7b241e6Sjeremylt     P0 = 1.0;
2089d7b241e6Sjeremylt     P1 = xi;
2090d7b241e6Sjeremylt     P2 = 0.0;
209192ae7e47SJeremy L Thompson     for (CeedInt j = 2; j < Q; j++) {
2092d7b241e6Sjeremylt       P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2093d7b241e6Sjeremylt       P0 = P1;
2094d7b241e6Sjeremylt       P1 = P2;
2095d7b241e6Sjeremylt     }
2096d7b241e6Sjeremylt     // First Newton step
2097d7b241e6Sjeremylt     dP2  = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2098d7b241e6Sjeremylt     d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi);
2099d7b241e6Sjeremylt     xi   = xi - dP2 / d2P2;
2100d7b241e6Sjeremylt     // Newton to convergence
210192ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < 100 && fabs(dP2) > 10 * CEED_EPSILON; k++) {
2102d7b241e6Sjeremylt       P0 = 1.0;
2103d7b241e6Sjeremylt       P1 = xi;
210492ae7e47SJeremy L Thompson       for (CeedInt j = 2; j < Q; j++) {
2105d7b241e6Sjeremylt         P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
2106d7b241e6Sjeremylt         P0 = P1;
2107d7b241e6Sjeremylt         P1 = P2;
2108d7b241e6Sjeremylt       }
2109d7b241e6Sjeremylt       dP2  = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2110d7b241e6Sjeremylt       d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi);
2111d7b241e6Sjeremylt       xi   = xi - dP2 / d2P2;
2112d7b241e6Sjeremylt     }
2113d7b241e6Sjeremylt     // Save xi, wi
2114d7b241e6Sjeremylt     wi = 2.0 / (((CeedScalar)(Q * (Q - 1))) * P2 * P2);
2115d1d35e2fSjeremylt     if (q_weight_1d) {
2116d1d35e2fSjeremylt       q_weight_1d[i]         = wi;
2117d1d35e2fSjeremylt       q_weight_1d[Q - 1 - i] = wi;
2118d7b241e6Sjeremylt     }
2119d1d35e2fSjeremylt     q_ref_1d[i]         = -xi;
2120d1d35e2fSjeremylt     q_ref_1d[Q - 1 - i] = xi;
2121d7b241e6Sjeremylt   }
2122e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2123d7b241e6Sjeremylt }
2124d7b241e6Sjeremylt 
2125d7b241e6Sjeremylt /// @}
2126