xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-basis.c (revision 2c2ea1dbee80fceecd2c97f30b09f8c87820a53e)
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 /**
387a982d89SJeremy L. Thompson   @brief Compute Householder reflection
397a982d89SJeremy L. Thompson 
40ea61e9acSJeremy L Thompson   Computes A = (I - b v v^T) A, where A is an mxn matrix indexed as A[i*row + j*col]
417a982d89SJeremy L. Thompson 
427a982d89SJeremy L. Thompson   @param[in,out] A   Matrix to apply Householder reflection to, in place
43ea61e9acSJeremy L Thompson   @param[in]     v   Householder vector
44ea61e9acSJeremy L Thompson   @param[in]     b   Scaling factor
45ea61e9acSJeremy L Thompson   @param[in]     m   Number of rows in A
46ea61e9acSJeremy L Thompson   @param[in]     n   Number of columns in A
47ea61e9acSJeremy L Thompson   @param[in]     row Row stride
48ea61e9acSJeremy L Thompson   @param[in]     col Col stride
497a982d89SJeremy L. Thompson 
507a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
517a982d89SJeremy L. Thompson 
527a982d89SJeremy L. Thompson   @ref Developer
537a982d89SJeremy L. Thompson **/
542b730f8bSJeremy L Thompson static int CeedHouseholderReflect(CeedScalar *A, const CeedScalar *v, CeedScalar b, CeedInt m, CeedInt n, CeedInt row, CeedInt col) {
557a982d89SJeremy L. Thompson   for (CeedInt j = 0; j < n; j++) {
567a982d89SJeremy L. Thompson     CeedScalar w = A[0 * row + j * col];
572b730f8bSJeremy L Thompson     for (CeedInt i = 1; i < m; i++) w += v[i] * A[i * row + j * col];
587a982d89SJeremy L. Thompson     A[0 * row + j * col] -= b * w;
592b730f8bSJeremy L Thompson     for (CeedInt i = 1; i < m; i++) A[i * row + j * col] -= b * w * v[i];
607a982d89SJeremy L. Thompson   }
61e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
627a982d89SJeremy L. Thompson }
637a982d89SJeremy L. Thompson 
647a982d89SJeremy L. Thompson /**
657a982d89SJeremy L. Thompson   @brief Compute Givens rotation
667a982d89SJeremy L. Thompson 
67ea61e9acSJeremy 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]
687a982d89SJeremy L. Thompson 
697a982d89SJeremy L. Thompson   @param[in,out] A      Row major matrix to apply Givens rotation to, in place
70ea61e9acSJeremy L Thompson   @param[in]     c      Cosine factor
71ea61e9acSJeremy L Thompson   @param[in]     s      Sine factor
72ea61e9acSJeremy 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;
734cc79fe7SJed Brown                           @ref CEED_TRANSPOSE for the opposite rotation
74ea61e9acSJeremy L Thompson   @param[in]     i      First row/column to apply rotation
75ea61e9acSJeremy L Thompson   @param[in]     k      Second row/column to apply rotation
76ea61e9acSJeremy L Thompson   @param[in]     m      Number of rows in A
77ea61e9acSJeremy L Thompson   @param[in]     n      Number of columns in A
787a982d89SJeremy L. Thompson 
797a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
807a982d89SJeremy L. Thompson 
817a982d89SJeremy L. Thompson   @ref Developer
827a982d89SJeremy L. Thompson **/
832b730f8bSJeremy L Thompson static int CeedGivensRotation(CeedScalar *A, CeedScalar c, CeedScalar s, CeedTransposeMode t_mode, CeedInt i, CeedInt k, CeedInt m, CeedInt n) {
84d1d35e2fSjeremylt   CeedInt stride_j = 1, stride_ik = m, num_its = n;
85d1d35e2fSjeremylt   if (t_mode == CEED_NOTRANSPOSE) {
862b730f8bSJeremy L Thompson     stride_j  = n;
872b730f8bSJeremy L Thompson     stride_ik = 1;
882b730f8bSJeremy L Thompson     num_its   = m;
897a982d89SJeremy L. Thompson   }
907a982d89SJeremy L. Thompson 
917a982d89SJeremy L. Thompson   // Apply rotation
92d1d35e2fSjeremylt   for (CeedInt j = 0; j < num_its; j++) {
93d1d35e2fSjeremylt     CeedScalar tau1 = A[i * stride_ik + j * stride_j], tau2 = A[k * stride_ik + j * stride_j];
94d1d35e2fSjeremylt     A[i * stride_ik + j * stride_j] = c * tau1 - s * tau2;
95d1d35e2fSjeremylt     A[k * stride_ik + j * stride_j] = s * tau1 + c * tau2;
967a982d89SJeremy L. Thompson   }
97e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
987a982d89SJeremy L. Thompson }
997a982d89SJeremy L. Thompson 
1007a982d89SJeremy L. Thompson /**
1017a982d89SJeremy L. Thompson   @brief View an array stored in a CeedBasis
1027a982d89SJeremy L. Thompson 
1030a0da059Sjeremylt   @param[in] name   Name of array
104d1d35e2fSjeremylt   @param[in] fp_fmt Printing format
1050a0da059Sjeremylt   @param[in] m      Number of rows in array
1060a0da059Sjeremylt   @param[in] n      Number of columns in array
1070a0da059Sjeremylt   @param[in] a      Array to be viewed
1080a0da059Sjeremylt   @param[in] stream Stream to view to, e.g., stdout
1097a982d89SJeremy L. Thompson 
1107a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1117a982d89SJeremy L. Thompson 
1127a982d89SJeremy L. Thompson   @ref Developer
1137a982d89SJeremy L. Thompson **/
1142b730f8bSJeremy L Thompson static int CeedScalarView(const char *name, const char *fp_fmt, CeedInt m, CeedInt n, const CeedScalar *a, FILE *stream) {
11592ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < m; i++) {
1162b730f8bSJeremy L Thompson     if (m > 1) fprintf(stream, "%12s[%" CeedInt_FMT "]:", name, i);
1172b730f8bSJeremy L Thompson     else fprintf(stream, "%12s:", name);
1182b730f8bSJeremy 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);
1197a982d89SJeremy L. Thompson     fputs("\n", stream);
1207a982d89SJeremy L. Thompson   }
121e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1227a982d89SJeremy L. Thompson }
1237a982d89SJeremy L. Thompson 
124a76a04e7SJeremy L Thompson /**
125ea61e9acSJeremy L Thompson   @brief Create the interpolation and gradient matrices for projection from the nodes of `basis_from` to the nodes of `basis_to`.
126ba59ac12SSebastian Grimberg 
12715ad3917SSebastian Grimberg   The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization.
12815ad3917SSebastian 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.
12915ad3917SSebastian Grimberg 
130ba59ac12SSebastian Grimberg   Note: `basis_from` and `basis_to` must have compatible quadrature spaces.
131a76a04e7SJeremy L Thompson 
132a76a04e7SJeremy L Thompson   @param[in]  basis_from     CeedBasis to project from
133a76a04e7SJeremy L Thompson   @param[in]  basis_to       CeedBasis to project to
134ea61e9acSJeremy L Thompson   @param[out] interp_project Address of the variable where the newly created interpolation matrix will be stored.
135ea61e9acSJeremy L Thompson   @param[out] grad_project   Address of the variable where the newly created gradient matrix will be stored.
136a76a04e7SJeremy L Thompson 
137a76a04e7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
138a76a04e7SJeremy L Thompson 
139a76a04e7SJeremy L Thompson   @ref Developer
140a76a04e7SJeremy L Thompson **/
1412b730f8bSJeremy L Thompson static int CeedBasisCreateProjectionMatrices(CeedBasis basis_from, CeedBasis basis_to, CeedScalar **interp_project, CeedScalar **grad_project) {
142a76a04e7SJeremy L Thompson   Ceed ceed;
1432b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis_to, &ceed));
144a76a04e7SJeremy L Thompson 
145a76a04e7SJeremy L Thompson   // Check for compatible quadrature spaces
146a76a04e7SJeremy L Thompson   CeedInt Q_to, Q_from;
1472b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_to, &Q_to));
1482b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_from, &Q_from));
1492b730f8bSJeremy L Thompson   if (Q_to != Q_from) {
150a76a04e7SJeremy L Thompson     // LCOV_EXCL_START
1512b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
152a76a04e7SJeremy L Thompson     // LCOV_EXCL_STOP
1532b730f8bSJeremy L Thompson   }
154a76a04e7SJeremy L Thompson 
15514556e63SJeremy L Thompson   // Check for matching tensor or non-tensor
156a76a04e7SJeremy L Thompson   CeedInt P_to, P_from, Q = Q_to;
157a76a04e7SJeremy L Thompson   bool    is_tensor_to, is_tensor_from;
1582b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to));
1592b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from));
160a76a04e7SJeremy L Thompson   if (is_tensor_to && is_tensor_from) {
1612b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_to));
1622b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_from));
1632b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints1D(basis_from, &Q));
164a76a04e7SJeremy L Thompson   } else if (!is_tensor_to && !is_tensor_from) {
1652b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_to, &P_to));
1662b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_from, &P_from));
167a76a04e7SJeremy L Thompson   } else {
168a76a04e7SJeremy L Thompson     // LCOV_EXCL_START
1692b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_MINOR, "Bases must both be tensor or non-tensor");
170a76a04e7SJeremy L Thompson     // LCOV_EXCL_STOP
171a76a04e7SJeremy L Thompson   }
172a76a04e7SJeremy L Thompson 
17315ad3917SSebastian Grimberg   // Check for matching FE space
17415ad3917SSebastian Grimberg   CeedFESpace fe_space_to, fe_space_from;
17515ad3917SSebastian Grimberg   CeedCall(CeedBasisGetFESpace(basis_to, &fe_space_to));
17615ad3917SSebastian Grimberg   CeedCall(CeedBasisGetFESpace(basis_from, &fe_space_from));
17715ad3917SSebastian Grimberg   if (fe_space_to != fe_space_from) {
17815ad3917SSebastian Grimberg     // LCOV_EXCL_START
17915ad3917SSebastian Grimberg     return CeedError(ceed, CEED_ERROR_MINOR, "Bases must both be the same FE space type");
18015ad3917SSebastian Grimberg     // LCOV_EXCL_STOP
18115ad3917SSebastian Grimberg   }
18215ad3917SSebastian Grimberg 
18314556e63SJeremy L Thompson   // Get source matrices
18415ad3917SSebastian Grimberg   CeedInt           dim, q_comp = 1;
18515ad3917SSebastian Grimberg   const CeedScalar *interp_to_source = NULL, *interp_from_source = NULL;
18614556e63SJeremy L Thompson   CeedScalar       *interp_to, *interp_from, *tau;
1872b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis_to, &dim));
188a76a04e7SJeremy L Thompson   if (is_tensor_to) {
1892b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis_to, &interp_to_source));
1902b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp1D(basis_from, &interp_from_source));
191a76a04e7SJeremy L Thompson   } else {
19215ad3917SSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis_from, CEED_EVAL_INTERP, &q_comp));
1932b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(basis_to, &interp_to_source));
1942b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(basis_from, &interp_from_source));
19515ad3917SSebastian Grimberg   }
19615ad3917SSebastian Grimberg   CeedCall(CeedMalloc(Q * P_from * q_comp, &interp_from));
19715ad3917SSebastian Grimberg   CeedCall(CeedMalloc(Q * P_to * q_comp, &interp_to));
19815ad3917SSebastian Grimberg   CeedCall(CeedCalloc(P_to * P_from, interp_project));
19915ad3917SSebastian Grimberg   CeedCall(CeedMalloc(Q * q_comp, &tau));
20015ad3917SSebastian Grimberg 
20115ad3917SSebastian Grimberg   // `grad_project = interp_to^+ * grad_from` is computed for the H^1 space case so the
20215ad3917SSebastian Grimberg   // projection basis will have a gradient operation
20315ad3917SSebastian Grimberg   const CeedScalar *grad_from_source = NULL;
20415ad3917SSebastian Grimberg   if (fe_space_to == CEED_FE_SPACE_H1) {
20515ad3917SSebastian Grimberg     if (is_tensor_to) {
20615ad3917SSebastian Grimberg       CeedCall(CeedBasisGetGrad1D(basis_from, &grad_from_source));
20715ad3917SSebastian Grimberg     } else {
2082b730f8bSJeremy L Thompson       CeedCall(CeedBasisGetGrad(basis_from, &grad_from_source));
209a76a04e7SJeremy L Thompson     }
21015ad3917SSebastian Grimberg     CeedCall(CeedCalloc(P_to * P_from * (is_tensor_to ? 1 : dim), grad_project));
21115ad3917SSebastian Grimberg   } else {
21215ad3917SSebastian Grimberg     // Projected derivate operator is only calculated for H^1 but we allocate it for the
21315ad3917SSebastian Grimberg     // basis construction later
21415ad3917SSebastian Grimberg     CeedInt q_comp_deriv = 1;
21515ad3917SSebastian Grimberg     if (fe_space_to == CEED_FE_SPACE_HDIV) {
21615ad3917SSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_from, CEED_EVAL_DIV, &q_comp_deriv));
21715ad3917SSebastian Grimberg     } else if (fe_space_to == CEED_FE_SPACE_HCURL) {
21815ad3917SSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_from, CEED_EVAL_CURL, &q_comp_deriv));
21915ad3917SSebastian Grimberg     }
22015ad3917SSebastian Grimberg     CeedCall(CeedCalloc(P_to * P_from * q_comp_deriv, grad_project));
22115ad3917SSebastian Grimberg   }
22215ad3917SSebastian Grimberg 
22315ad3917SSebastian Grimberg   // QR Factorization, interp_to = Q R
22415ad3917SSebastian Grimberg   memcpy(interp_to, interp_to_source, Q * P_to * q_comp * sizeof(interp_to_source[0]));
22515ad3917SSebastian Grimberg   CeedCall(CeedQRFactorization(ceed, interp_to, tau, Q * q_comp, P_to));
226a76a04e7SJeremy L Thompson 
22714556e63SJeremy L Thompson   // Build matrices
22815ad3917SSebastian Grimberg   CeedInt     num_matrices = 1 + (fe_space_to == CEED_FE_SPACE_H1) * (is_tensor_to ? 1 : dim);
22914556e63SJeremy L Thompson   CeedScalar *input_from[num_matrices], *output_project[num_matrices];
23014556e63SJeremy L Thompson   input_from[0]     = (CeedScalar *)interp_from_source;
23114556e63SJeremy L Thompson   output_project[0] = *interp_project;
23214556e63SJeremy L Thompson   for (CeedInt m = 1; m < num_matrices; m++) {
23314556e63SJeremy L Thompson     input_from[m]     = (CeedScalar *)&grad_from_source[(m - 1) * Q * P_from];
23402af4036SJeremy L Thompson     output_project[m] = &((*grad_project)[(m - 1) * P_to * P_from]);
23514556e63SJeremy L Thompson   }
23614556e63SJeremy L Thompson   for (CeedInt m = 0; m < num_matrices; m++) {
23715ad3917SSebastian Grimberg     // Apply Q^T, interp_from = Q^T interp_from
23815ad3917SSebastian Grimberg     memcpy(interp_from, input_from[m], Q * P_from * q_comp * sizeof(input_from[m][0]));
23915ad3917SSebastian Grimberg     CeedCall(CeedHouseholderApplyQ(interp_from, interp_to, tau, CEED_TRANSPOSE, Q * q_comp, P_from, P_to, P_from, 1));
240a76a04e7SJeremy L Thompson 
24115ad3917SSebastian Grimberg     // Apply Rinv, output_project = Rinv interp_from
242a76a04e7SJeremy L Thompson     for (CeedInt j = 0; j < P_from; j++) {  // Column j
2432b730f8bSJeremy 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];
244a76a04e7SJeremy L Thompson       for (CeedInt i = P_to - 2; i >= 0; i--) {  // Row i
24514556e63SJeremy L Thompson         output_project[m][j + P_from * i] = interp_from[j + P_from * i];
246a76a04e7SJeremy L Thompson         for (CeedInt k = i + 1; k < P_to; k++) {
2472b730f8bSJeremy L Thompson           output_project[m][j + P_from * i] -= interp_to[k + P_to * i] * output_project[m][j + P_from * k];
248a76a04e7SJeremy L Thompson         }
24914556e63SJeremy L Thompson         output_project[m][j + P_from * i] /= interp_to[i + P_to * i];
250a76a04e7SJeremy L Thompson       }
251a76a04e7SJeremy L Thompson     }
25214556e63SJeremy L Thompson   }
25314556e63SJeremy L Thompson 
25414556e63SJeremy L Thompson   // Cleanup
2552b730f8bSJeremy L Thompson   CeedCall(CeedFree(&tau));
2562b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_to));
2572b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_from));
258a76a04e7SJeremy L Thompson 
259a76a04e7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
260a76a04e7SJeremy L Thompson }
261a76a04e7SJeremy L Thompson 
2627a982d89SJeremy L. Thompson /// @}
2637a982d89SJeremy L. Thompson 
2647a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
2657a982d89SJeremy L. Thompson /// Ceed Backend API
2667a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
2677a982d89SJeremy L. Thompson /// @addtogroup CeedBasisBackend
2687a982d89SJeremy L. Thompson /// @{
2697a982d89SJeremy L. Thompson 
2707a982d89SJeremy L. Thompson /**
2717a982d89SJeremy L. Thompson   @brief Return collocated grad matrix
2727a982d89SJeremy L. Thompson 
273ea61e9acSJeremy L Thompson   @param[in]  basis         CeedBasis
274ea61e9acSJeremy L Thompson   @param[out] collo_grad_1d Row-major (Q_1d * Q_1d) matrix expressing derivatives of basis functions at quadrature points
2757a982d89SJeremy L. Thompson 
2767a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2777a982d89SJeremy L. Thompson 
2787a982d89SJeremy L. Thompson   @ref Backend
2797a982d89SJeremy L. Thompson **/
280d1d35e2fSjeremylt int CeedBasisGetCollocatedGrad(CeedBasis basis, CeedScalar *collo_grad_1d) {
2817a982d89SJeremy L. Thompson   int         i, j, k;
2827a982d89SJeremy L. Thompson   Ceed        ceed;
2832b730f8bSJeremy L Thompson   CeedInt     P_1d = (basis)->P_1d, Q_1d = (basis)->Q_1d;
28478464608Sjeremylt   CeedScalar *interp_1d, *grad_1d, *tau;
2857a982d89SJeremy L. Thompson 
2862b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q_1d * P_1d, &interp_1d));
2872b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q_1d * P_1d, &grad_1d));
2882b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q_1d, &tau));
289d1d35e2fSjeremylt   memcpy(interp_1d, (basis)->interp_1d, Q_1d * P_1d * sizeof(basis)->interp_1d[0]);
290d1d35e2fSjeremylt   memcpy(grad_1d, (basis)->grad_1d, Q_1d * P_1d * sizeof(basis)->interp_1d[0]);
2917a982d89SJeremy L. Thompson 
292d1d35e2fSjeremylt   // QR Factorization, interp_1d = Q R
2932b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis, &ceed));
2942b730f8bSJeremy L Thompson   CeedCall(CeedQRFactorization(ceed, interp_1d, tau, Q_1d, P_1d));
295ea61e9acSJeremy 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.
2967a982d89SJeremy L. Thompson 
297d1d35e2fSjeremylt   // Apply Rinv, collo_grad_1d = grad_1d Rinv
298d1d35e2fSjeremylt   for (i = 0; i < Q_1d; i++) {  // Row i
299d1d35e2fSjeremylt     collo_grad_1d[Q_1d * i] = grad_1d[P_1d * i] / interp_1d[0];
300d1d35e2fSjeremylt     for (j = 1; j < P_1d; j++) {  // Column j
301d1d35e2fSjeremylt       collo_grad_1d[j + Q_1d * i] = grad_1d[j + P_1d * i];
3022b730f8bSJeremy L Thompson       for (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];
303d1d35e2fSjeremylt       collo_grad_1d[j + Q_1d * i] /= interp_1d[j + P_1d * j];
3047a982d89SJeremy L. Thompson     }
3052b730f8bSJeremy L Thompson     for (j = P_1d; j < Q_1d; j++) collo_grad_1d[j + Q_1d * i] = 0;
3067a982d89SJeremy L. Thompson   }
3077a982d89SJeremy L. Thompson 
30815ad3917SSebastian Grimberg   // Apply Q^T, collo_grad_1d = collo_grad_1d Q^T
3092b730f8bSJeremy L Thompson   CeedCall(CeedHouseholderApplyQ(collo_grad_1d, interp_1d, tau, CEED_NOTRANSPOSE, Q_1d, Q_1d, P_1d, 1, Q_1d));
3107a982d89SJeremy L. Thompson 
3112b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_1d));
3122b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_1d));
3132b730f8bSJeremy L Thompson   CeedCall(CeedFree(&tau));
314e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3157a982d89SJeremy L. Thompson }
3167a982d89SJeremy L. Thompson 
3177a982d89SJeremy L. Thompson /**
3187a982d89SJeremy L. Thompson   @brief Get tensor status for given CeedBasis
3197a982d89SJeremy L. Thompson 
320ea61e9acSJeremy L Thompson   @param[in]  basis     CeedBasis
321d1d35e2fSjeremylt   @param[out] is_tensor Variable to store tensor status
3227a982d89SJeremy L. Thompson 
3237a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3247a982d89SJeremy L. Thompson 
3257a982d89SJeremy L. Thompson   @ref Backend
3267a982d89SJeremy L. Thompson **/
327d1d35e2fSjeremylt int CeedBasisIsTensor(CeedBasis basis, bool *is_tensor) {
328d1d35e2fSjeremylt   *is_tensor = basis->tensor_basis;
329e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3307a982d89SJeremy L. Thompson }
3317a982d89SJeremy L. Thompson 
3327a982d89SJeremy L. Thompson /**
3337a982d89SJeremy L. Thompson   @brief Get backend data of a CeedBasis
3347a982d89SJeremy L. Thompson 
335ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
3367a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
3377a982d89SJeremy L. Thompson 
3387a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3397a982d89SJeremy L. Thompson 
3407a982d89SJeremy L. Thompson   @ref Backend
3417a982d89SJeremy L. Thompson **/
342777ff853SJeremy L Thompson int CeedBasisGetData(CeedBasis basis, void *data) {
343777ff853SJeremy L Thompson   *(void **)data = basis->data;
344e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3457a982d89SJeremy L. Thompson }
3467a982d89SJeremy L. Thompson 
3477a982d89SJeremy L. Thompson /**
3487a982d89SJeremy L. Thompson   @brief Set backend data of a CeedBasis
3497a982d89SJeremy L. Thompson 
350ea61e9acSJeremy L Thompson   @param[in,out] basis  CeedBasis
351ea61e9acSJeremy L Thompson   @param[in]     data   Data to set
3527a982d89SJeremy L. Thompson 
3537a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3547a982d89SJeremy L. Thompson 
3557a982d89SJeremy L. Thompson   @ref Backend
3567a982d89SJeremy L. Thompson **/
357777ff853SJeremy L Thompson int CeedBasisSetData(CeedBasis basis, void *data) {
358777ff853SJeremy L Thompson   basis->data = data;
359e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3607a982d89SJeremy L. Thompson }
3617a982d89SJeremy L. Thompson 
3627a982d89SJeremy L. Thompson /**
36334359f16Sjeremylt   @brief Increment the reference counter for a CeedBasis
36434359f16Sjeremylt 
365ea61e9acSJeremy L Thompson   @param[in,out] basis Basis to increment the reference counter
36634359f16Sjeremylt 
36734359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
36834359f16Sjeremylt 
36934359f16Sjeremylt   @ref Backend
37034359f16Sjeremylt **/
3719560d06aSjeremylt int CeedBasisReference(CeedBasis basis) {
37234359f16Sjeremylt   basis->ref_count++;
37334359f16Sjeremylt   return CEED_ERROR_SUCCESS;
37434359f16Sjeremylt }
37534359f16Sjeremylt 
37634359f16Sjeremylt /**
377c4e3f59bSSebastian Grimberg   @brief Get number of Q-vector components for given CeedBasis
378c4e3f59bSSebastian Grimberg 
379c4e3f59bSSebastian Grimberg   @param[in]  basis  CeedBasis
380c4e3f59bSSebastian Grimberg   @param[in]  eval_mode \ref CEED_EVAL_INTERP to use interpolated values,
381c4e3f59bSSebastian Grimberg                           \ref CEED_EVAL_GRAD to use gradients,
382c4e3f59bSSebastian Grimberg                           \ref CEED_EVAL_DIV to use divergence,
383c4e3f59bSSebastian Grimberg                           \ref CEED_EVAL_CURL to use curl.
384c4e3f59bSSebastian Grimberg   @param[out] q_comp Variable to store number of Q-vector components of basis
385c4e3f59bSSebastian Grimberg 
386c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
387c4e3f59bSSebastian Grimberg 
388c4e3f59bSSebastian Grimberg   @ref Backend
389c4e3f59bSSebastian Grimberg **/
390c4e3f59bSSebastian Grimberg int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode, CeedInt *q_comp) {
391c4e3f59bSSebastian Grimberg   switch (eval_mode) {
392c4e3f59bSSebastian Grimberg     case CEED_EVAL_INTERP:
393c4e3f59bSSebastian Grimberg       *q_comp = (basis->fe_space == CEED_FE_SPACE_H1) ? 1 : basis->dim;
394c4e3f59bSSebastian Grimberg       break;
395c4e3f59bSSebastian Grimberg     case CEED_EVAL_GRAD:
396c4e3f59bSSebastian Grimberg       *q_comp = basis->dim;
397c4e3f59bSSebastian Grimberg       break;
398c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
399c4e3f59bSSebastian Grimberg       *q_comp = 1;
400c4e3f59bSSebastian Grimberg       break;
401c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
402c4e3f59bSSebastian Grimberg       *q_comp = (basis->dim < 3) ? 1 : basis->dim;
403c4e3f59bSSebastian Grimberg       break;
404c4e3f59bSSebastian Grimberg     case CEED_EVAL_NONE:
405c4e3f59bSSebastian Grimberg     case CEED_EVAL_WEIGHT:
406352a5e7cSSebastian Grimberg       *q_comp = 1;
407c4e3f59bSSebastian Grimberg       break;
408c4e3f59bSSebastian Grimberg   }
409c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
410c4e3f59bSSebastian Grimberg }
411c4e3f59bSSebastian Grimberg 
412c4e3f59bSSebastian Grimberg /**
4136e15d496SJeremy L Thompson   @brief Estimate number of FLOPs required to apply CeedBasis in t_mode and eval_mode
4146e15d496SJeremy L Thompson 
415ea61e9acSJeremy L Thompson   @param[in]  basis     Basis to estimate FLOPs for
416ea61e9acSJeremy L Thompson   @param[in]  t_mode    Apply basis or transpose
417ea61e9acSJeremy L Thompson   @param[in]  eval_mode Basis evaluation mode
418ea61e9acSJeremy L Thompson   @param[out] flops     Address of variable to hold FLOPs estimate
4196e15d496SJeremy L Thompson 
4206e15d496SJeremy L Thompson   @ref Backend
4216e15d496SJeremy L Thompson **/
4222b730f8bSJeremy L Thompson int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedSize *flops) {
4236e15d496SJeremy L Thompson   bool is_tensor;
4246e15d496SJeremy L Thompson 
4252b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor));
4266e15d496SJeremy L Thompson   if (is_tensor) {
4276e15d496SJeremy L Thompson     CeedInt dim, num_comp, P_1d, Q_1d;
4282b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis, &dim));
4292b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
4302b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
4312b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
4326e15d496SJeremy L Thompson     if (t_mode == CEED_TRANSPOSE) {
4332b730f8bSJeremy L Thompson       P_1d = Q_1d;
4342b730f8bSJeremy L Thompson       Q_1d = P_1d;
4356e15d496SJeremy L Thompson     }
4366e15d496SJeremy L Thompson     CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim - 1), post = 1;
4376e15d496SJeremy L Thompson     for (CeedInt d = 0; d < dim; d++) {
4386e15d496SJeremy L Thompson       tensor_flops += 2 * pre * P_1d * post * Q_1d;
4396e15d496SJeremy L Thompson       pre /= P_1d;
4406e15d496SJeremy L Thompson       post *= Q_1d;
4416e15d496SJeremy L Thompson     }
4426e15d496SJeremy L Thompson     switch (eval_mode) {
4432b730f8bSJeremy L Thompson       case CEED_EVAL_NONE:
4442b730f8bSJeremy L Thompson         *flops = 0;
4452b730f8bSJeremy L Thompson         break;
4462b730f8bSJeremy L Thompson       case CEED_EVAL_INTERP:
4472b730f8bSJeremy L Thompson         *flops = tensor_flops;
4482b730f8bSJeremy L Thompson         break;
4492b730f8bSJeremy L Thompson       case CEED_EVAL_GRAD:
4502b730f8bSJeremy L Thompson         *flops = tensor_flops * 2;
4512b730f8bSJeremy L Thompson         break;
4526e15d496SJeremy L Thompson       case CEED_EVAL_DIV:
4536e15d496SJeremy L Thompson         // LCOV_EXCL_START
4542b730f8bSJeremy L Thompson         return CeedError(basis->ceed, CEED_ERROR_INCOMPATIBLE, "Tensor CEED_EVAL_DIV not supported");
4552b730f8bSJeremy L Thompson         break;
4566e15d496SJeremy L Thompson       case CEED_EVAL_CURL:
4572b730f8bSJeremy L Thompson         return CeedError(basis->ceed, CEED_ERROR_INCOMPATIBLE, "Tensor CEED_EVAL_CURL not supported");
4582b730f8bSJeremy L Thompson         break;
4596e15d496SJeremy L Thompson       // LCOV_EXCL_STOP
4602b730f8bSJeremy L Thompson       case CEED_EVAL_WEIGHT:
4612b730f8bSJeremy L Thompson         *flops = dim * CeedIntPow(Q_1d, dim);
4622b730f8bSJeremy L Thompson         break;
4636e15d496SJeremy L Thompson     }
4646e15d496SJeremy L Thompson   } else {
465c4e3f59bSSebastian Grimberg     CeedInt dim, num_comp, q_comp, num_nodes, num_qpts;
4662b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis, &dim));
4672b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
468c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
4692b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
4702b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
4716e15d496SJeremy L Thompson     switch (eval_mode) {
4722b730f8bSJeremy L Thompson       case CEED_EVAL_NONE:
4732b730f8bSJeremy L Thompson         *flops = 0;
4742b730f8bSJeremy L Thompson         break;
4752b730f8bSJeremy L Thompson       case CEED_EVAL_INTERP:
4762b730f8bSJeremy L Thompson       case CEED_EVAL_GRAD:
4772b730f8bSJeremy L Thompson       case CEED_EVAL_DIV:
4782b730f8bSJeremy L Thompson       case CEED_EVAL_CURL:
479c4e3f59bSSebastian Grimberg         *flops = num_nodes * num_qpts * num_comp * q_comp;
4802b730f8bSJeremy L Thompson         break;
4812b730f8bSJeremy L Thompson       case CEED_EVAL_WEIGHT:
4822b730f8bSJeremy L Thompson         *flops = 0;
4832b730f8bSJeremy L Thompson         break;
4846e15d496SJeremy L Thompson     }
4856e15d496SJeremy L Thompson   }
4866e15d496SJeremy L Thompson 
4876e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4886e15d496SJeremy L Thompson }
4896e15d496SJeremy L Thompson 
4906e15d496SJeremy L Thompson /**
491c4e3f59bSSebastian Grimberg   @brief Get CeedFESpace for a CeedBasis
492c4e3f59bSSebastian Grimberg 
493c4e3f59bSSebastian Grimberg   @param[in]  basis     CeedBasis
494c4e3f59bSSebastian Grimberg   @param[out] fe_space  Variable to store CeedFESpace
495c4e3f59bSSebastian Grimberg 
496c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
497c4e3f59bSSebastian Grimberg 
498c4e3f59bSSebastian Grimberg   @ref Backend
499c4e3f59bSSebastian Grimberg **/
500c4e3f59bSSebastian Grimberg int CeedBasisGetFESpace(CeedBasis basis, CeedFESpace *fe_space) {
501c4e3f59bSSebastian Grimberg   *fe_space = basis->fe_space;
502c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
503c4e3f59bSSebastian Grimberg }
504c4e3f59bSSebastian Grimberg 
505c4e3f59bSSebastian Grimberg /**
5067a982d89SJeremy L. Thompson   @brief Get dimension for given CeedElemTopology
5077a982d89SJeremy L. Thompson 
508ea61e9acSJeremy L Thompson   @param[in]  topo CeedElemTopology
5097a982d89SJeremy L. Thompson   @param[out] dim  Variable to store dimension of topology
5107a982d89SJeremy L. Thompson 
5117a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5127a982d89SJeremy L. Thompson 
5137a982d89SJeremy L. Thompson   @ref Backend
5147a982d89SJeremy L. Thompson **/
5157a982d89SJeremy L. Thompson int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim) {
5167a982d89SJeremy L. Thompson   *dim = (CeedInt)topo >> 16;
517e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5187a982d89SJeremy L. Thompson }
5197a982d89SJeremy L. Thompson 
5207a982d89SJeremy L. Thompson /**
5217a982d89SJeremy L. Thompson   @brief Get CeedTensorContract of a CeedBasis
5227a982d89SJeremy L. Thompson 
523ea61e9acSJeremy L Thompson   @param[in]  basis     CeedBasis
5247a982d89SJeremy L. Thompson   @param[out] contract  Variable to store CeedTensorContract
5257a982d89SJeremy L. Thompson 
5267a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5277a982d89SJeremy L. Thompson 
5287a982d89SJeremy L. Thompson   @ref Backend
5297a982d89SJeremy L. Thompson **/
5307a982d89SJeremy L. Thompson int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract) {
5317a982d89SJeremy L. Thompson   *contract = basis->contract;
532e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5337a982d89SJeremy L. Thompson }
5347a982d89SJeremy L. Thompson 
5357a982d89SJeremy L. Thompson /**
5367a982d89SJeremy L. Thompson   @brief Set CeedTensorContract of a CeedBasis
5377a982d89SJeremy L. Thompson 
538ea61e9acSJeremy L Thompson   @param[in,out] basis    CeedBasis
539ea61e9acSJeremy L Thompson   @param[in]     contract CeedTensorContract to set
5407a982d89SJeremy L. Thompson 
5417a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5427a982d89SJeremy L. Thompson 
5437a982d89SJeremy L. Thompson   @ref Backend
5447a982d89SJeremy L. Thompson **/
54534359f16Sjeremylt int CeedBasisSetTensorContract(CeedBasis basis, CeedTensorContract contract) {
54634359f16Sjeremylt   basis->contract = contract;
5472b730f8bSJeremy L Thompson   CeedCall(CeedTensorContractReference(contract));
548e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5497a982d89SJeremy L. Thompson }
5507a982d89SJeremy L. Thompson 
5517a982d89SJeremy L. Thompson /**
5527a982d89SJeremy L. Thompson   @brief Return a reference implementation of matrix multiplication C = A B.
553ba59ac12SSebastian Grimberg 
554ba59ac12SSebastian Grimberg   Note: This is a reference implementation for CPU CeedScalar pointers that is not intended for high performance.
5557a982d89SJeremy L. Thompson 
556ea61e9acSJeremy L Thompson   @param[in]  ceed  Ceed context for error handling
557d1d35e2fSjeremylt   @param[in]  mat_A Row-major matrix A
558d1d35e2fSjeremylt   @param[in]  mat_B Row-major matrix B
559d1d35e2fSjeremylt   @param[out] mat_C Row-major output matrix C
560ea61e9acSJeremy L Thompson   @param[in]  m     Number of rows of C
561ea61e9acSJeremy L Thompson   @param[in]  n     Number of columns of C
562ea61e9acSJeremy L Thompson   @param[in]  kk    Number of columns of A/rows of B
5637a982d89SJeremy L. Thompson 
5647a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5657a982d89SJeremy L. Thompson 
5667a982d89SJeremy L. Thompson   @ref Utility
5677a982d89SJeremy L. Thompson **/
5682b730f8bSJeremy L Thompson int CeedMatrixMatrixMultiply(Ceed ceed, const CeedScalar *mat_A, const CeedScalar *mat_B, CeedScalar *mat_C, CeedInt m, CeedInt n, CeedInt kk) {
5692b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < m; i++) {
5707a982d89SJeremy L. Thompson     for (CeedInt j = 0; j < n; j++) {
5717a982d89SJeremy L. Thompson       CeedScalar sum = 0;
5722b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < kk; k++) sum += mat_A[k + i * kk] * mat_B[j + k * n];
573d1d35e2fSjeremylt       mat_C[j + i * n] = sum;
5747a982d89SJeremy L. Thompson     }
5752b730f8bSJeremy L Thompson   }
576e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5777a982d89SJeremy L. Thompson }
5787a982d89SJeremy L. Thompson 
579ba59ac12SSebastian Grimberg /**
580ba59ac12SSebastian Grimberg   @brief Return QR Factorization of a matrix
581ba59ac12SSebastian Grimberg 
582ba59ac12SSebastian Grimberg   @param[in]     ceed Ceed context for error handling
583ba59ac12SSebastian Grimberg   @param[in,out] mat  Row-major matrix to be factorized in place
584ba59ac12SSebastian Grimberg   @param[in,out] tau  Vector of length m of scaling factors
585ba59ac12SSebastian Grimberg   @param[in]     m    Number of rows
586ba59ac12SSebastian Grimberg   @param[in]     n    Number of columns
587ba59ac12SSebastian Grimberg 
588ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
589ba59ac12SSebastian Grimberg 
590ba59ac12SSebastian Grimberg   @ref Utility
591ba59ac12SSebastian Grimberg **/
592ba59ac12SSebastian Grimberg int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau, CeedInt m, CeedInt n) {
593ba59ac12SSebastian Grimberg   CeedScalar v[m];
594ba59ac12SSebastian Grimberg 
595ba59ac12SSebastian Grimberg   // Check matrix shape
596ba59ac12SSebastian Grimberg   if (n > m) {
597ba59ac12SSebastian Grimberg     // LCOV_EXCL_START
598ba59ac12SSebastian Grimberg     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute QR factorization with n > m");
599ba59ac12SSebastian Grimberg     // LCOV_EXCL_STOP
600ba59ac12SSebastian Grimberg   }
601ba59ac12SSebastian Grimberg 
602ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
603ba59ac12SSebastian Grimberg     if (i >= m - 1) {  // last row of matrix, no reflection needed
604ba59ac12SSebastian Grimberg       tau[i] = 0.;
605ba59ac12SSebastian Grimberg       break;
606ba59ac12SSebastian Grimberg     }
607ba59ac12SSebastian Grimberg     // Calculate Householder vector, magnitude
608ba59ac12SSebastian Grimberg     CeedScalar sigma = 0.0;
609ba59ac12SSebastian Grimberg     v[i]             = mat[i + n * i];
610ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) {
611ba59ac12SSebastian Grimberg       v[j] = mat[i + n * j];
612ba59ac12SSebastian Grimberg       sigma += v[j] * v[j];
613ba59ac12SSebastian Grimberg     }
614ba59ac12SSebastian Grimberg     CeedScalar norm = sqrt(v[i] * v[i] + sigma);  // norm of v[i:m]
615ba59ac12SSebastian Grimberg     CeedScalar R_ii = -copysign(norm, v[i]);
616ba59ac12SSebastian Grimberg     v[i] -= R_ii;
617ba59ac12SSebastian Grimberg     // norm of v[i:m] after modification above and scaling below
618ba59ac12SSebastian Grimberg     //   norm = sqrt(v[i]*v[i] + sigma) / v[i];
619ba59ac12SSebastian Grimberg     //   tau = 2 / (norm*norm)
620ba59ac12SSebastian Grimberg     tau[i] = 2 * v[i] * v[i] / (v[i] * v[i] + sigma);
621ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) v[j] /= v[i];
622ba59ac12SSebastian Grimberg 
623ba59ac12SSebastian Grimberg     // Apply Householder reflector to lower right panel
624ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat[i * n + i + 1], &v[i], tau[i], m - i, n - i - 1, n, 1);
625ba59ac12SSebastian Grimberg     // Save v
626ba59ac12SSebastian Grimberg     mat[i + n * i] = R_ii;
627ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) mat[i + n * j] = v[j];
628ba59ac12SSebastian Grimberg   }
629ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
630ba59ac12SSebastian Grimberg }
631ba59ac12SSebastian Grimberg 
632ba59ac12SSebastian Grimberg /**
633ba59ac12SSebastian Grimberg   @brief Apply Householder Q matrix
634ba59ac12SSebastian Grimberg 
635ba59ac12SSebastian Grimberg   Compute mat_A = mat_Q mat_A, where mat_Q is mxm and mat_A is mxn.
636ba59ac12SSebastian Grimberg 
637ba59ac12SSebastian Grimberg   @param[in,out] mat_A  Matrix to apply Householder Q to, in place
638ba59ac12SSebastian Grimberg   @param[in]     mat_Q  Householder Q matrix
639ba59ac12SSebastian Grimberg   @param[in]     tau    Householder scaling factors
640ba59ac12SSebastian Grimberg   @param[in]     t_mode Transpose mode for application
641ba59ac12SSebastian Grimberg   @param[in]     m      Number of rows in A
642ba59ac12SSebastian Grimberg   @param[in]     n      Number of columns in A
643ba59ac12SSebastian Grimberg   @param[in]     k      Number of elementary reflectors in Q, k<m
644ba59ac12SSebastian Grimberg   @param[in]     row    Row stride in A
645ba59ac12SSebastian Grimberg   @param[in]     col    Col stride in A
646ba59ac12SSebastian Grimberg 
647ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
648ba59ac12SSebastian Grimberg 
649c4e3f59bSSebastian Grimberg   @ref Utility
650ba59ac12SSebastian Grimberg **/
651ba59ac12SSebastian Grimberg int CeedHouseholderApplyQ(CeedScalar *mat_A, const CeedScalar *mat_Q, const CeedScalar *tau, CeedTransposeMode t_mode, CeedInt m, CeedInt n,
652ba59ac12SSebastian Grimberg                           CeedInt k, CeedInt row, CeedInt col) {
653ba59ac12SSebastian Grimberg   CeedScalar *v;
654ba59ac12SSebastian Grimberg   CeedCall(CeedMalloc(m, &v));
655ba59ac12SSebastian Grimberg   for (CeedInt ii = 0; ii < k; ii++) {
656ba59ac12SSebastian Grimberg     CeedInt i = t_mode == CEED_TRANSPOSE ? ii : k - 1 - ii;
657ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < m; j++) v[j] = mat_Q[j * k + i];
658ba59ac12SSebastian Grimberg     // Apply Householder reflector (I - tau v v^T) collo_grad_1d^T
659ba59ac12SSebastian Grimberg     CeedCall(CeedHouseholderReflect(&mat_A[i * row], &v[i], tau[i], m - i, n, row, col));
660ba59ac12SSebastian Grimberg   }
661ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&v));
662ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
663ba59ac12SSebastian Grimberg }
664ba59ac12SSebastian Grimberg 
665ba59ac12SSebastian Grimberg /**
666ba59ac12SSebastian Grimberg   @brief Return symmetric Schur decomposition of the symmetric matrix mat via symmetric QR factorization
667ba59ac12SSebastian Grimberg 
668ba59ac12SSebastian Grimberg   @param[in]     ceed   Ceed context for error handling
669ba59ac12SSebastian Grimberg   @param[in,out] mat    Row-major matrix to be factorized in place
670ba59ac12SSebastian Grimberg   @param[out]    lambda Vector of length n of eigenvalues
671ba59ac12SSebastian Grimberg   @param[in]     n      Number of rows/columns
672ba59ac12SSebastian Grimberg 
673ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
674ba59ac12SSebastian Grimberg 
675ba59ac12SSebastian Grimberg   @ref Utility
676ba59ac12SSebastian Grimberg **/
677*2c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
678*2c2ea1dbSJeremy L Thompson int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat, CeedScalar *lambda, CeedInt n) {
679ba59ac12SSebastian Grimberg   // Check bounds for clang-tidy
680ba59ac12SSebastian Grimberg   if (n < 2) {
681ba59ac12SSebastian Grimberg     // LCOV_EXCL_START
682ba59ac12SSebastian Grimberg     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute symmetric Schur decomposition of scalars");
683ba59ac12SSebastian Grimberg     // LCOV_EXCL_STOP
684ba59ac12SSebastian Grimberg   }
685ba59ac12SSebastian Grimberg 
686ba59ac12SSebastian Grimberg   CeedScalar v[n - 1], tau[n - 1], mat_T[n * n];
687ba59ac12SSebastian Grimberg 
688ba59ac12SSebastian Grimberg   // Copy mat to mat_T and set mat to I
689ba59ac12SSebastian Grimberg   memcpy(mat_T, mat, n * n * sizeof(mat[0]));
690ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
691ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < n; j++) mat[j + n * i] = (i == j) ? 1 : 0;
692ba59ac12SSebastian Grimberg   }
693ba59ac12SSebastian Grimberg 
694ba59ac12SSebastian Grimberg   // Reduce to tridiagonal
695ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n - 1; i++) {
696ba59ac12SSebastian Grimberg     // Calculate Householder vector, magnitude
697ba59ac12SSebastian Grimberg     CeedScalar sigma = 0.0;
698ba59ac12SSebastian Grimberg     v[i]             = mat_T[i + n * (i + 1)];
699ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) {
700ba59ac12SSebastian Grimberg       v[j] = mat_T[i + n * (j + 1)];
701ba59ac12SSebastian Grimberg       sigma += v[j] * v[j];
702ba59ac12SSebastian Grimberg     }
703ba59ac12SSebastian Grimberg     CeedScalar norm = sqrt(v[i] * v[i] + sigma);  // norm of v[i:n-1]
704ba59ac12SSebastian Grimberg     CeedScalar R_ii = -copysign(norm, v[i]);
705ba59ac12SSebastian Grimberg     v[i] -= R_ii;
706ba59ac12SSebastian Grimberg     // norm of v[i:m] after modification above and scaling below
707ba59ac12SSebastian Grimberg     //   norm = sqrt(v[i]*v[i] + sigma) / v[i];
708ba59ac12SSebastian Grimberg     //   tau = 2 / (norm*norm)
709ba59ac12SSebastian Grimberg     tau[i] = i == n - 2 ? 2 : 2 * v[i] * v[i] / (v[i] * v[i] + sigma);
710ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) v[j] /= v[i];
711ba59ac12SSebastian Grimberg 
712ba59ac12SSebastian Grimberg     // Update sub and super diagonal
713ba59ac12SSebastian Grimberg     for (CeedInt j = i + 2; j < n; j++) {
714ba59ac12SSebastian Grimberg       mat_T[i + n * j] = 0;
715ba59ac12SSebastian Grimberg       mat_T[j + n * i] = 0;
716ba59ac12SSebastian Grimberg     }
717ba59ac12SSebastian Grimberg     // Apply symmetric Householder reflector to lower right panel
718ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1);
719ba59ac12SSebastian Grimberg     CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), 1, n);
720ba59ac12SSebastian Grimberg 
721ba59ac12SSebastian Grimberg     // Save v
722ba59ac12SSebastian Grimberg     mat_T[i + n * (i + 1)] = R_ii;
723ba59ac12SSebastian Grimberg     mat_T[(i + 1) + n * i] = R_ii;
724ba59ac12SSebastian Grimberg     for (CeedInt j = i + 1; j < n - 1; j++) {
725ba59ac12SSebastian Grimberg       mat_T[i + n * (j + 1)] = v[j];
726ba59ac12SSebastian Grimberg     }
727ba59ac12SSebastian Grimberg   }
728ba59ac12SSebastian Grimberg   // Backwards accumulation of Q
729ba59ac12SSebastian Grimberg   for (CeedInt i = n - 2; i >= 0; i--) {
730ba59ac12SSebastian Grimberg     if (tau[i] > 0.0) {
731ba59ac12SSebastian Grimberg       v[i] = 1;
732ba59ac12SSebastian Grimberg       for (CeedInt j = i + 1; j < n - 1; j++) {
733ba59ac12SSebastian Grimberg         v[j]                   = mat_T[i + n * (j + 1)];
734ba59ac12SSebastian Grimberg         mat_T[i + n * (j + 1)] = 0;
735ba59ac12SSebastian Grimberg       }
736ba59ac12SSebastian Grimberg       CeedHouseholderReflect(&mat[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1);
737ba59ac12SSebastian Grimberg     }
738ba59ac12SSebastian Grimberg   }
739ba59ac12SSebastian Grimberg 
740ba59ac12SSebastian Grimberg   // Reduce sub and super diagonal
741ba59ac12SSebastian Grimberg   CeedInt    p = 0, q = 0, itr = 0, max_itr = n * n * n * n;
742ba59ac12SSebastian Grimberg   CeedScalar tol = CEED_EPSILON;
743ba59ac12SSebastian Grimberg 
744ba59ac12SSebastian Grimberg   while (itr < max_itr) {
745ba59ac12SSebastian Grimberg     // Update p, q, size of reduced portions of diagonal
746ba59ac12SSebastian Grimberg     p = 0;
747ba59ac12SSebastian Grimberg     q = 0;
748ba59ac12SSebastian Grimberg     for (CeedInt i = n - 2; i >= 0; i--) {
749ba59ac12SSebastian Grimberg       if (fabs(mat_T[i + n * (i + 1)]) < tol) q += 1;
750ba59ac12SSebastian Grimberg       else break;
751ba59ac12SSebastian Grimberg     }
752ba59ac12SSebastian Grimberg     for (CeedInt i = 0; i < n - q - 1; i++) {
753ba59ac12SSebastian Grimberg       if (fabs(mat_T[i + n * (i + 1)]) < tol) p += 1;
754ba59ac12SSebastian Grimberg       else break;
755ba59ac12SSebastian Grimberg     }
756ba59ac12SSebastian Grimberg     if (q == n - 1) break;  // Finished reducing
757ba59ac12SSebastian Grimberg 
758ba59ac12SSebastian Grimberg     // Reduce tridiagonal portion
759ba59ac12SSebastian Grimberg     CeedScalar t_nn = mat_T[(n - 1 - q) + n * (n - 1 - q)], t_nnm1 = mat_T[(n - 2 - q) + n * (n - 1 - q)];
760ba59ac12SSebastian Grimberg     CeedScalar d  = (mat_T[(n - 2 - q) + n * (n - 2 - q)] - t_nn) / 2;
761ba59ac12SSebastian Grimberg     CeedScalar mu = t_nn - t_nnm1 * t_nnm1 / (d + copysign(sqrt(d * d + t_nnm1 * t_nnm1), d));
762ba59ac12SSebastian Grimberg     CeedScalar x  = mat_T[p + n * p] - mu;
763ba59ac12SSebastian Grimberg     CeedScalar z  = mat_T[p + n * (p + 1)];
764ba59ac12SSebastian Grimberg     for (CeedInt k = p; k < n - q - 1; k++) {
765ba59ac12SSebastian Grimberg       // Compute Givens rotation
766ba59ac12SSebastian Grimberg       CeedScalar c = 1, s = 0;
767ba59ac12SSebastian Grimberg       if (fabs(z) > tol) {
768ba59ac12SSebastian Grimberg         if (fabs(z) > fabs(x)) {
769ba59ac12SSebastian Grimberg           CeedScalar tau = -x / z;
770ba59ac12SSebastian Grimberg           s = 1 / sqrt(1 + tau * tau), c = s * tau;
771ba59ac12SSebastian Grimberg         } else {
772ba59ac12SSebastian Grimberg           CeedScalar tau = -z / x;
773ba59ac12SSebastian Grimberg           c = 1 / sqrt(1 + tau * tau), s = c * tau;
774ba59ac12SSebastian Grimberg         }
775ba59ac12SSebastian Grimberg       }
776ba59ac12SSebastian Grimberg 
777ba59ac12SSebastian Grimberg       // Apply Givens rotation to T
778ba59ac12SSebastian Grimberg       CeedGivensRotation(mat_T, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n);
779ba59ac12SSebastian Grimberg       CeedGivensRotation(mat_T, c, s, CEED_TRANSPOSE, k, k + 1, n, n);
780ba59ac12SSebastian Grimberg 
781ba59ac12SSebastian Grimberg       // Apply Givens rotation to Q
782ba59ac12SSebastian Grimberg       CeedGivensRotation(mat, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n);
783ba59ac12SSebastian Grimberg 
784ba59ac12SSebastian Grimberg       // Update x, z
785ba59ac12SSebastian Grimberg       if (k < n - q - 2) {
786ba59ac12SSebastian Grimberg         x = mat_T[k + n * (k + 1)];
787ba59ac12SSebastian Grimberg         z = mat_T[k + n * (k + 2)];
788ba59ac12SSebastian Grimberg       }
789ba59ac12SSebastian Grimberg     }
790ba59ac12SSebastian Grimberg     itr++;
791ba59ac12SSebastian Grimberg   }
792ba59ac12SSebastian Grimberg 
793ba59ac12SSebastian Grimberg   // Save eigenvalues
794ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) lambda[i] = mat_T[i + n * i];
795ba59ac12SSebastian Grimberg 
796ba59ac12SSebastian Grimberg   // Check convergence
797ba59ac12SSebastian Grimberg   if (itr == max_itr && q < n - 1) {
798ba59ac12SSebastian Grimberg     // LCOV_EXCL_START
799ba59ac12SSebastian Grimberg     return CeedError(ceed, CEED_ERROR_MINOR, "Symmetric QR failed to converge");
800ba59ac12SSebastian Grimberg     // LCOV_EXCL_STOP
801ba59ac12SSebastian Grimberg   }
802ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
803ba59ac12SSebastian Grimberg }
804*2c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
805ba59ac12SSebastian Grimberg 
806ba59ac12SSebastian Grimberg /**
807ba59ac12SSebastian Grimberg   @brief Return Simultaneous Diagonalization of two matrices.
808ba59ac12SSebastian Grimberg 
809ba59ac12SSebastian Grimberg   This solves the generalized eigenvalue problem A x = lambda B x, where A and B are symmetric and B is positive definite.
810ba59ac12SSebastian Grimberg   We generate the matrix X and vector Lambda such that X^T A X = Lambda and X^T B X = I.
811ba59ac12SSebastian Grimberg   This is equivalent to the LAPACK routine 'sygv' with TYPE = 1.
812ba59ac12SSebastian Grimberg 
813ba59ac12SSebastian Grimberg   @param[in]  ceed   Ceed context for error handling
814ba59ac12SSebastian Grimberg   @param[in]  mat_A  Row-major matrix to be factorized with eigenvalues
815ba59ac12SSebastian Grimberg   @param[in]  mat_B  Row-major matrix to be factorized to identity
816ba59ac12SSebastian Grimberg   @param[out] mat_X  Row-major orthogonal matrix
817ba59ac12SSebastian Grimberg   @param[out] lambda Vector of length n of generalized eigenvalues
818ba59ac12SSebastian Grimberg   @param[in]  n      Number of rows/columns
819ba59ac12SSebastian Grimberg 
820ba59ac12SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
821ba59ac12SSebastian Grimberg 
822ba59ac12SSebastian Grimberg   @ref Utility
823ba59ac12SSebastian Grimberg **/
824*2c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
825*2c2ea1dbSJeremy L Thompson int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A, CeedScalar *mat_B, CeedScalar *mat_X, CeedScalar *lambda, CeedInt n) {
826ba59ac12SSebastian Grimberg   CeedScalar *mat_C, *mat_G, *vec_D;
827ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n * n, &mat_C));
828ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n * n, &mat_G));
829ba59ac12SSebastian Grimberg   CeedCall(CeedCalloc(n, &vec_D));
830ba59ac12SSebastian Grimberg 
831ba59ac12SSebastian Grimberg   // Compute B = G D G^T
832ba59ac12SSebastian Grimberg   memcpy(mat_G, mat_B, n * n * sizeof(mat_B[0]));
833ba59ac12SSebastian Grimberg   CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_G, vec_D, n));
834ba59ac12SSebastian Grimberg 
835ba59ac12SSebastian Grimberg   // Sort eigenvalues
836ba59ac12SSebastian Grimberg   for (CeedInt i = n - 1; i >= 0; i--) {
837ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < i; j++) {
838ba59ac12SSebastian Grimberg       if (fabs(vec_D[j]) > fabs(vec_D[j + 1])) {
839ba59ac12SSebastian Grimberg         CeedScalar temp;
840ba59ac12SSebastian Grimberg         temp         = vec_D[j];
841ba59ac12SSebastian Grimberg         vec_D[j]     = vec_D[j + 1];
842ba59ac12SSebastian Grimberg         vec_D[j + 1] = temp;
843ba59ac12SSebastian Grimberg         for (CeedInt k = 0; k < n; k++) {
844ba59ac12SSebastian Grimberg           temp                 = mat_G[k * n + j];
845ba59ac12SSebastian Grimberg           mat_G[k * n + j]     = mat_G[k * n + j + 1];
846ba59ac12SSebastian Grimberg           mat_G[k * n + j + 1] = temp;
847ba59ac12SSebastian Grimberg         }
848ba59ac12SSebastian Grimberg       }
849ba59ac12SSebastian Grimberg     }
850ba59ac12SSebastian Grimberg   }
851ba59ac12SSebastian Grimberg 
852ba59ac12SSebastian Grimberg   // Compute C = (G D^1/2)^-1 A (G D^1/2)^-T
853ba59ac12SSebastian Grimberg   //           = D^-1/2 G^T A G D^-1/2
854ba59ac12SSebastian Grimberg   // -- D = D^-1/2
855ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) vec_D[i] = 1. / sqrt(vec_D[i]);
856ba59ac12SSebastian Grimberg   // -- G = G D^-1/2
857ba59ac12SSebastian Grimberg   // -- C = D^-1/2 G^T
858ba59ac12SSebastian Grimberg   for (CeedInt i = 0; i < n; i++) {
859ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < n; j++) {
860ba59ac12SSebastian Grimberg       mat_G[i * n + j] *= vec_D[j];
861ba59ac12SSebastian Grimberg       mat_C[j * n + i] = mat_G[i * n + j];
862ba59ac12SSebastian Grimberg     }
863ba59ac12SSebastian Grimberg   }
864ba59ac12SSebastian Grimberg   // -- X = (D^-1/2 G^T) A
865ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_C, (const CeedScalar *)mat_A, mat_X, n, n, n));
866ba59ac12SSebastian Grimberg   // -- C = (D^-1/2 G^T A) (G D^-1/2)
867ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_X, (const CeedScalar *)mat_G, mat_C, n, n, n));
868ba59ac12SSebastian Grimberg 
869ba59ac12SSebastian Grimberg   // Compute Q^T C Q = lambda
870ba59ac12SSebastian Grimberg   CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_C, lambda, n));
871ba59ac12SSebastian Grimberg 
872ba59ac12SSebastian Grimberg   // Sort eigenvalues
873ba59ac12SSebastian Grimberg   for (CeedInt i = n - 1; i >= 0; i--) {
874ba59ac12SSebastian Grimberg     for (CeedInt j = 0; j < i; j++) {
875ba59ac12SSebastian Grimberg       if (fabs(lambda[j]) > fabs(lambda[j + 1])) {
876ba59ac12SSebastian Grimberg         CeedScalar temp;
877ba59ac12SSebastian Grimberg         temp          = lambda[j];
878ba59ac12SSebastian Grimberg         lambda[j]     = lambda[j + 1];
879ba59ac12SSebastian Grimberg         lambda[j + 1] = temp;
880ba59ac12SSebastian Grimberg         for (CeedInt k = 0; k < n; k++) {
881ba59ac12SSebastian Grimberg           temp                 = mat_C[k * n + j];
882ba59ac12SSebastian Grimberg           mat_C[k * n + j]     = mat_C[k * n + j + 1];
883ba59ac12SSebastian Grimberg           mat_C[k * n + j + 1] = temp;
884ba59ac12SSebastian Grimberg         }
885ba59ac12SSebastian Grimberg       }
886ba59ac12SSebastian Grimberg     }
887ba59ac12SSebastian Grimberg   }
888ba59ac12SSebastian Grimberg 
889ba59ac12SSebastian Grimberg   // Set X = (G D^1/2)^-T Q
890ba59ac12SSebastian Grimberg   //       = G D^-1/2 Q
891ba59ac12SSebastian Grimberg   CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_G, (const CeedScalar *)mat_C, mat_X, n, n, n));
892ba59ac12SSebastian Grimberg 
893ba59ac12SSebastian Grimberg   // Cleanup
894ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&mat_C));
895ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&mat_G));
896ba59ac12SSebastian Grimberg   CeedCall(CeedFree(&vec_D));
897ba59ac12SSebastian Grimberg   return CEED_ERROR_SUCCESS;
898ba59ac12SSebastian Grimberg }
899*2c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
900ba59ac12SSebastian Grimberg 
9017a982d89SJeremy L. Thompson /// @}
9027a982d89SJeremy L. Thompson 
9037a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
9047a982d89SJeremy L. Thompson /// CeedBasis Public API
9057a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
9067a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser
907d7b241e6Sjeremylt /// @{
908d7b241e6Sjeremylt 
909b11c1e72Sjeremylt /**
910ba59ac12SSebastian Grimberg   @brief Create a tensor-product basis for H^1 discretizations
911b11c1e72Sjeremylt 
912ea61e9acSJeremy L Thompson   @param[in]  ceed        Ceed object where the CeedBasis will be created
913ea61e9acSJeremy L Thompson   @param[in]  dim         Topological dimension
914ea61e9acSJeremy L Thompson   @param[in]  num_comp    Number of field components (1 for scalar fields)
915ea61e9acSJeremy L Thompson   @param[in]  P_1d        Number of nodes in one dimension
916ea61e9acSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
917ea61e9acSJeremy L Thompson   @param[in]  interp_1d   Row-major (Q_1d * P_1d) matrix expressing the values of nodal basis functions at quadrature points
918ea61e9acSJeremy L Thompson   @param[in]  grad_1d     Row-major (Q_1d * P_1d) matrix expressing derivatives of nodal basis functions at quadrature points
919ea61e9acSJeremy 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]
920ea61e9acSJeremy L Thompson   @param[in]  q_weight_1d Array of length Q_1d holding the quadrature weights on the reference element
921ea61e9acSJeremy L Thompson   @param[out] basis       Address of the variable where the newly created CeedBasis will be stored.
922b11c1e72Sjeremylt 
923b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
924dfdf5a53Sjeremylt 
9257a982d89SJeremy L. Thompson   @ref User
926b11c1e72Sjeremylt **/
9272b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d,
9282b730f8bSJeremy L Thompson                             const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis *basis) {
9295fe0d4faSjeremylt   if (!ceed->BasisCreateTensorH1) {
9305fe0d4faSjeremylt     Ceed delegate;
9312b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
9325fe0d4faSjeremylt 
9332b730f8bSJeremy L Thompson     if (!delegate) {
934c042f62fSJeremy L Thompson       // LCOV_EXCL_START
9352b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisCreateTensorH1");
936c042f62fSJeremy L Thompson       // LCOV_EXCL_STOP
9372b730f8bSJeremy L Thompson     }
9385fe0d4faSjeremylt 
9392b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis));
940e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
9415fe0d4faSjeremylt   }
942e15f9bd0SJeremy L Thompson 
9432b730f8bSJeremy L Thompson   if (dim < 1) {
944e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
9452b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Basis dimension must be a positive value");
946e15f9bd0SJeremy L Thompson     // LCOV_EXCL_STOP
9472b730f8bSJeremy L Thompson   }
948227444bfSJeremy L Thompson 
9492b730f8bSJeremy L Thompson   if (num_comp < 1) {
950227444bfSJeremy L Thompson     // LCOV_EXCL_START
9512b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component");
952227444bfSJeremy L Thompson     // LCOV_EXCL_STOP
9532b730f8bSJeremy L Thompson   }
954227444bfSJeremy L Thompson 
9552b730f8bSJeremy L Thompson   if (P_1d < 1) {
956227444bfSJeremy L Thompson     // LCOV_EXCL_START
9572b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node");
958227444bfSJeremy L Thompson     // LCOV_EXCL_STOP
9592b730f8bSJeremy L Thompson   }
960227444bfSJeremy L Thompson 
9612b730f8bSJeremy L Thompson   if (Q_1d < 1) {
962227444bfSJeremy L Thompson     // LCOV_EXCL_START
9632b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point");
964227444bfSJeremy L Thompson     // LCOV_EXCL_STOP
9652b730f8bSJeremy L Thompson   }
966227444bfSJeremy L Thompson 
9672b730f8bSJeremy L Thompson   CeedElemTopology topo = dim == 1 ? CEED_TOPOLOGY_LINE : dim == 2 ? CEED_TOPOLOGY_QUAD : CEED_TOPOLOGY_HEX;
968e15f9bd0SJeremy L Thompson 
9692b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
970d7b241e6Sjeremylt   (*basis)->ceed = ceed;
9712b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
972d1d35e2fSjeremylt   (*basis)->ref_count    = 1;
973d1d35e2fSjeremylt   (*basis)->tensor_basis = 1;
974d7b241e6Sjeremylt   (*basis)->dim          = dim;
975d99fa3c5SJeremy L Thompson   (*basis)->topo         = topo;
976d1d35e2fSjeremylt   (*basis)->num_comp     = num_comp;
977d1d35e2fSjeremylt   (*basis)->P_1d         = P_1d;
978d1d35e2fSjeremylt   (*basis)->Q_1d         = Q_1d;
979d1d35e2fSjeremylt   (*basis)->P            = CeedIntPow(P_1d, dim);
980d1d35e2fSjeremylt   (*basis)->Q            = CeedIntPow(Q_1d, dim);
981c4e3f59bSSebastian Grimberg   (*basis)->fe_space     = CEED_FE_SPACE_H1;
9822b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d, &(*basis)->q_ref_1d));
9832b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d, &(*basis)->q_weight_1d));
984ff3a0f91SJeremy L Thompson   if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d * sizeof(q_ref_1d[0]));
9852b730f8bSJeremy L Thompson   if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, Q_1d * sizeof(q_weight_1d[0]));
9862b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->interp_1d));
9872b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->grad_1d));
9882b730f8bSJeremy L Thompson   if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, Q_1d * P_1d * sizeof(interp_1d[0]));
989ff3a0f91SJeremy L Thompson   if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d * P_1d * sizeof(grad_1d[0]));
9902b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, *basis));
991e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
992d7b241e6Sjeremylt }
993d7b241e6Sjeremylt 
994b11c1e72Sjeremylt /**
99595bb1877Svaleriabarra   @brief Create a tensor-product Lagrange basis
996b11c1e72Sjeremylt 
997ea61e9acSJeremy L Thompson   @param[in]  ceed      Ceed object where the CeedBasis will be created
998ea61e9acSJeremy L Thompson   @param[in]  dim       Topological dimension of element
999ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of field components (1 for scalar fields)
1000ea61e9acSJeremy L Thompson   @param[in]  P         Number of Gauss-Lobatto nodes in one dimension.
1001ea61e9acSJeremy L Thompson                           The polynomial degree of the resulting Q_k element is k=P-1.
1002ea61e9acSJeremy L Thompson   @param[in]  Q         Number of quadrature points in one dimension.
1003ea61e9acSJeremy L Thompson   @param[in]  quad_mode Distribution of the Q quadrature points (affects order of accuracy for the quadrature)
1004ea61e9acSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created CeedBasis will be stored.
1005b11c1e72Sjeremylt 
1006b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1007dfdf5a53Sjeremylt 
10087a982d89SJeremy L. Thompson   @ref User
1009b11c1e72Sjeremylt **/
10102b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis) {
1011d7b241e6Sjeremylt   // Allocate
10122b730f8bSJeremy L Thompson   int        ierr = CEED_ERROR_SUCCESS, i, j, k;
10132b730f8bSJeremy L Thompson   CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, *q_weight_1d;
10144d537eeaSYohann 
10152b730f8bSJeremy L Thompson   if (dim < 1) {
1016c042f62fSJeremy L Thompson     // LCOV_EXCL_START
10172b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Basis dimension must be a positive value");
1018c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
10192b730f8bSJeremy L Thompson   }
10204d537eeaSYohann 
10212b730f8bSJeremy L Thompson   if (num_comp < 1) {
1022227444bfSJeremy L Thompson     // LCOV_EXCL_START
10232b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component");
1024227444bfSJeremy L Thompson     // LCOV_EXCL_STOP
10252b730f8bSJeremy L Thompson   }
1026227444bfSJeremy L Thompson 
10272b730f8bSJeremy L Thompson   if (P < 1) {
1028227444bfSJeremy L Thompson     // LCOV_EXCL_START
10292b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node");
1030227444bfSJeremy L Thompson     // LCOV_EXCL_STOP
10312b730f8bSJeremy L Thompson   }
1032227444bfSJeremy L Thompson 
10332b730f8bSJeremy L Thompson   if (Q < 1) {
1034227444bfSJeremy L Thompson     // LCOV_EXCL_START
10352b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point");
1036227444bfSJeremy L Thompson     // LCOV_EXCL_STOP
10372b730f8bSJeremy L Thompson   }
1038227444bfSJeremy L Thompson 
1039e15f9bd0SJeremy L Thompson   // Get Nodes and Weights
10402b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P * Q, &interp_1d));
10412b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P * Q, &grad_1d));
10422b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P, &nodes));
10432b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &q_ref_1d));
10442b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &q_weight_1d));
10452b730f8bSJeremy L Thompson   if (CeedLobattoQuadrature(P, nodes, NULL) != CEED_ERROR_SUCCESS) goto cleanup;
1046d1d35e2fSjeremylt   switch (quad_mode) {
1047d7b241e6Sjeremylt     case CEED_GAUSS:
1048d1d35e2fSjeremylt       ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d);
1049d7b241e6Sjeremylt       break;
1050d7b241e6Sjeremylt     case CEED_GAUSS_LOBATTO:
1051d1d35e2fSjeremylt       ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d);
1052d7b241e6Sjeremylt       break;
1053d7b241e6Sjeremylt   }
10542b730f8bSJeremy L Thompson   if (ierr != CEED_ERROR_SUCCESS) goto cleanup;
1055e15f9bd0SJeremy L Thompson 
1056d7b241e6Sjeremylt   // Build B, D matrix
1057d7b241e6Sjeremylt   // Fornberg, 1998
1058d7b241e6Sjeremylt   for (i = 0; i < Q; i++) {
1059d7b241e6Sjeremylt     c1                   = 1.0;
1060d1d35e2fSjeremylt     c3                   = nodes[0] - q_ref_1d[i];
1061d1d35e2fSjeremylt     interp_1d[i * P + 0] = 1.0;
1062d7b241e6Sjeremylt     for (j = 1; j < P; j++) {
1063d7b241e6Sjeremylt       c2 = 1.0;
1064d7b241e6Sjeremylt       c4 = c3;
1065d1d35e2fSjeremylt       c3 = nodes[j] - q_ref_1d[i];
1066d7b241e6Sjeremylt       for (k = 0; k < j; k++) {
1067d7b241e6Sjeremylt         dx = nodes[j] - nodes[k];
1068d7b241e6Sjeremylt         c2 *= dx;
1069d7b241e6Sjeremylt         if (k == j - 1) {
1070d1d35e2fSjeremylt           grad_1d[i * P + j]   = c1 * (interp_1d[i * P + k] - c4 * grad_1d[i * P + k]) / c2;
1071d1d35e2fSjeremylt           interp_1d[i * P + j] = -c1 * c4 * interp_1d[i * P + k] / c2;
1072d7b241e6Sjeremylt         }
1073d1d35e2fSjeremylt         grad_1d[i * P + k]   = (c3 * grad_1d[i * P + k] - interp_1d[i * P + k]) / dx;
1074d1d35e2fSjeremylt         interp_1d[i * P + k] = c3 * interp_1d[i * P + k] / dx;
1075d7b241e6Sjeremylt       }
1076d7b241e6Sjeremylt       c1 = c2;
1077d7b241e6Sjeremylt     }
1078d7b241e6Sjeremylt   }
10799ac7b42eSJeremy L Thompson   // Pass to CeedBasisCreateTensorH1
10802b730f8bSJeremy L Thompson   CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis));
1081e15f9bd0SJeremy L Thompson cleanup:
10822b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_1d));
10832b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_1d));
10842b730f8bSJeremy L Thompson   CeedCall(CeedFree(&nodes));
10852b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref_1d));
10862b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight_1d));
1087e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1088d7b241e6Sjeremylt }
1089d7b241e6Sjeremylt 
1090b11c1e72Sjeremylt /**
1091ba59ac12SSebastian Grimberg   @brief Create a non tensor-product basis for H^1 discretizations
1092a8de75f0Sjeremylt 
1093ea61e9acSJeremy L Thompson   @param[in]  ceed      Ceed object where the CeedBasis will be created
1094ea61e9acSJeremy L Thompson   @param[in]  topo      Topology of element, e.g. hypercube, simplex, ect
1095ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of field components (1 for scalar fields)
1096ea61e9acSJeremy L Thompson   @param[in]  num_nodes Total number of nodes
1097ea61e9acSJeremy L Thompson   @param[in]  num_qpts  Total number of quadrature points
1098ea61e9acSJeremy L Thompson   @param[in]  interp    Row-major (num_qpts * num_nodes) matrix expressing the values of nodal basis functions at quadrature points
1099c4e3f59bSSebastian Grimberg   @param[in]  grad      Row-major (dim * num_qpts * num_nodes) matrix expressing derivatives of nodal basis functions at quadrature points
11009fe083eeSJeremy L Thompson   @param[in]  q_ref     Array of length num_qpts * dim holding the locations of quadrature points on the reference element
1101ea61e9acSJeremy L Thompson   @param[in]  q_weight  Array of length num_qpts holding the quadrature weights on the reference element
1102ea61e9acSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created CeedBasis will be stored.
1103a8de75f0Sjeremylt 
1104a8de75f0Sjeremylt   @return An error code: 0 - success, otherwise - failure
1105a8de75f0Sjeremylt 
11067a982d89SJeremy L. Thompson   @ref User
1107a8de75f0Sjeremylt **/
11082b730f8bSJeremy L Thompson int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
11092b730f8bSJeremy L Thompson                       const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
1110d1d35e2fSjeremylt   CeedInt P = num_nodes, Q = num_qpts, dim = 0;
1111a8de75f0Sjeremylt 
11125fe0d4faSjeremylt   if (!ceed->BasisCreateH1) {
11135fe0d4faSjeremylt     Ceed delegate;
11142b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
11155fe0d4faSjeremylt 
11162b730f8bSJeremy L Thompson     if (!delegate) {
1117c042f62fSJeremy L Thompson       // LCOV_EXCL_START
11182b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisCreateH1");
1119c042f62fSJeremy L Thompson       // LCOV_EXCL_STOP
11202b730f8bSJeremy L Thompson     }
11215fe0d4faSjeremylt 
11222b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis));
1123e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
11245fe0d4faSjeremylt   }
11255fe0d4faSjeremylt 
11262b730f8bSJeremy L Thompson   if (num_comp < 1) {
1127227444bfSJeremy L Thompson     // LCOV_EXCL_START
11282b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component");
1129227444bfSJeremy L Thompson     // LCOV_EXCL_STOP
11302b730f8bSJeremy L Thompson   }
1131227444bfSJeremy L Thompson 
11322b730f8bSJeremy L Thompson   if (num_nodes < 1) {
1133227444bfSJeremy L Thompson     // LCOV_EXCL_START
11342b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node");
1135227444bfSJeremy L Thompson     // LCOV_EXCL_STOP
11362b730f8bSJeremy L Thompson   }
1137227444bfSJeremy L Thompson 
11382b730f8bSJeremy L Thompson   if (num_qpts < 1) {
1139227444bfSJeremy L Thompson     // LCOV_EXCL_START
11402b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point");
1141227444bfSJeremy L Thompson     // LCOV_EXCL_STOP
11422b730f8bSJeremy L Thompson   }
1143227444bfSJeremy L Thompson 
11442b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
1145a8de75f0Sjeremylt 
11462b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1147a8de75f0Sjeremylt 
1148a8de75f0Sjeremylt   (*basis)->ceed = ceed;
11492b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1150d1d35e2fSjeremylt   (*basis)->ref_count    = 1;
1151d1d35e2fSjeremylt   (*basis)->tensor_basis = 0;
1152a8de75f0Sjeremylt   (*basis)->dim          = dim;
1153d99fa3c5SJeremy L Thompson   (*basis)->topo         = topo;
1154d1d35e2fSjeremylt   (*basis)->num_comp     = num_comp;
1155a8de75f0Sjeremylt   (*basis)->P            = P;
1156a8de75f0Sjeremylt   (*basis)->Q            = Q;
1157c4e3f59bSSebastian Grimberg   (*basis)->fe_space     = CEED_FE_SPACE_H1;
11582b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q * dim, &(*basis)->q_ref_1d));
11592b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q, &(*basis)->q_weight_1d));
1160ff3a0f91SJeremy L Thompson   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
1161ff3a0f91SJeremy L Thompson   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
11622b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(Q * P, &(*basis)->interp));
11632b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(dim * Q * P, &(*basis)->grad));
1164ff3a0f91SJeremy L Thompson   if (interp) memcpy((*basis)->interp, interp, Q * P * sizeof(interp[0]));
1165ff3a0f91SJeremy L Thompson   if (grad) memcpy((*basis)->grad, grad, dim * Q * P * sizeof(grad[0]));
11662b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, *basis));
1167e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1168a8de75f0Sjeremylt }
1169a8de75f0Sjeremylt 
1170a8de75f0Sjeremylt /**
1171859c15bbSJames Wright   @brief Create a non tensor-product basis for \f$H(\mathrm{div})\f$ discretizations
117250c301a5SRezgar Shakeri 
1173ea61e9acSJeremy L Thompson   @param[in]  ceed      Ceed object where the CeedBasis will be created
1174ea61e9acSJeremy 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
1175ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of components (usually 1 for vectors in H(div) bases)
1176ea61e9acSJeremy L Thompson   @param[in]  num_nodes Total number of nodes (dofs per element)
1177ea61e9acSJeremy L Thompson   @param[in]  num_qpts  Total number of quadrature points
1178c4e3f59bSSebastian Grimberg   @param[in]  interp    Row-major (dim * num_qpts * num_nodes) matrix expressing the values of basis functions at quadrature points
1179c4e3f59bSSebastian Grimberg   @param[in]  div       Row-major (num_qpts * num_nodes) matrix expressing divergence of basis functions at quadrature points
11809fe083eeSJeremy L Thompson   @param[in]  q_ref     Array of length num_qpts * dim holding the locations of quadrature points on the reference element
1181ea61e9acSJeremy L Thompson   @param[in]  q_weight  Array of length num_qpts holding the quadrature weights on the reference element
1182ea61e9acSJeremy L Thompson   @param[out] basis     Address of the variable where the newly created CeedBasis will be stored.
118350c301a5SRezgar Shakeri 
118450c301a5SRezgar Shakeri   @return An error code: 0 - success, otherwise - failure
118550c301a5SRezgar Shakeri 
118650c301a5SRezgar Shakeri   @ref User
118750c301a5SRezgar Shakeri **/
11882b730f8bSJeremy L Thompson int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
11892b730f8bSJeremy L Thompson                         const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
119050c301a5SRezgar Shakeri   CeedInt Q = num_qpts, P = num_nodes, dim = 0;
1191c4e3f59bSSebastian Grimberg 
119250c301a5SRezgar Shakeri   if (!ceed->BasisCreateHdiv) {
119350c301a5SRezgar Shakeri     Ceed delegate;
11942b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
119550c301a5SRezgar Shakeri 
11962b730f8bSJeremy L Thompson     if (!delegate) {
119750c301a5SRezgar Shakeri       // LCOV_EXCL_START
11982b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv");
119950c301a5SRezgar Shakeri       // LCOV_EXCL_STOP
12002b730f8bSJeremy L Thompson     }
120150c301a5SRezgar Shakeri 
12022b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis));
120350c301a5SRezgar Shakeri     return CEED_ERROR_SUCCESS;
120450c301a5SRezgar Shakeri   }
120550c301a5SRezgar Shakeri 
12062b730f8bSJeremy L Thompson   if (num_comp < 1) {
1207227444bfSJeremy L Thompson     // LCOV_EXCL_START
12082b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component");
1209227444bfSJeremy L Thompson     // LCOV_EXCL_STOP
12102b730f8bSJeremy L Thompson   }
1211227444bfSJeremy L Thompson 
12122b730f8bSJeremy L Thompson   if (num_nodes < 1) {
1213227444bfSJeremy L Thompson     // LCOV_EXCL_START
12142b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node");
1215227444bfSJeremy L Thompson     // LCOV_EXCL_STOP
12162b730f8bSJeremy L Thompson   }
1217227444bfSJeremy L Thompson 
12182b730f8bSJeremy L Thompson   if (num_qpts < 1) {
1219227444bfSJeremy L Thompson     // LCOV_EXCL_START
12202b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point");
1221227444bfSJeremy L Thompson     // LCOV_EXCL_STOP
12222b730f8bSJeremy L Thompson   }
1223227444bfSJeremy L Thompson 
12242b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, basis));
122550c301a5SRezgar Shakeri 
1226c4e3f59bSSebastian Grimberg   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1227c4e3f59bSSebastian Grimberg 
122850c301a5SRezgar Shakeri   (*basis)->ceed = ceed;
12292b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
123050c301a5SRezgar Shakeri   (*basis)->ref_count    = 1;
123150c301a5SRezgar Shakeri   (*basis)->tensor_basis = 0;
123250c301a5SRezgar Shakeri   (*basis)->dim          = dim;
123350c301a5SRezgar Shakeri   (*basis)->topo         = topo;
123450c301a5SRezgar Shakeri   (*basis)->num_comp     = num_comp;
123550c301a5SRezgar Shakeri   (*basis)->P            = P;
123650c301a5SRezgar Shakeri   (*basis)->Q            = Q;
1237c4e3f59bSSebastian Grimberg   (*basis)->fe_space     = CEED_FE_SPACE_HDIV;
12382b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d));
12392b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d));
124050c301a5SRezgar Shakeri   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
124150c301a5SRezgar Shakeri   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
12422b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp));
12432b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(Q * P, &(*basis)->div));
124450c301a5SRezgar Shakeri   if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0]));
124550c301a5SRezgar Shakeri   if (div) memcpy((*basis)->div, div, Q * P * sizeof(div[0]));
12462b730f8bSJeremy L Thompson   CeedCall(ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, q_weight, *basis));
124750c301a5SRezgar Shakeri   return CEED_ERROR_SUCCESS;
124850c301a5SRezgar Shakeri }
124950c301a5SRezgar Shakeri 
125050c301a5SRezgar Shakeri /**
1251c4e3f59bSSebastian Grimberg   @brief Create a non tensor-product basis for H(curl) discretizations
1252c4e3f59bSSebastian Grimberg 
1253c4e3f59bSSebastian Grimberg   @param[in]  ceed      Ceed object where the CeedBasis will be created
1254c4e3f59bSSebastian Grimberg   @param[in]  topo      Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below
1255c4e3f59bSSebastian Grimberg   @param[in]  num_comp  Number of components (usually 1 for vectors in H(curl) bases)
1256c4e3f59bSSebastian Grimberg   @param[in]  num_nodes Total number of nodes (dofs per element)
1257c4e3f59bSSebastian Grimberg   @param[in]  num_qpts  Total number of quadrature points
1258c4e3f59bSSebastian Grimberg   @param[in]  interp    Row-major (dim * num_qpts * num_nodes) matrix expressing the values of basis functions at quadrature points
1259c4e3f59bSSebastian 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
1260c4e3f59bSSebastian Grimberg quadrature points
1261c4e3f59bSSebastian Grimberg   @param[in]  q_ref     Array of length num_qpts * dim holding the locations of quadrature points on the reference element
1262c4e3f59bSSebastian Grimberg   @param[in]  q_weight  Array of length num_qpts holding the quadrature weights on the reference element
1263c4e3f59bSSebastian Grimberg   @param[out] basis     Address of the variable where the newly created CeedBasis will be stored.
1264c4e3f59bSSebastian Grimberg 
1265c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1266c4e3f59bSSebastian Grimberg 
1267c4e3f59bSSebastian Grimberg   @ref User
1268c4e3f59bSSebastian Grimberg **/
1269c4e3f59bSSebastian Grimberg int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
1270c4e3f59bSSebastian Grimberg                          const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) {
1271c4e3f59bSSebastian Grimberg   CeedInt Q = num_qpts, P = num_nodes, dim = 0, curl_comp = 0;
1272c4e3f59bSSebastian Grimberg 
1273c4e3f59bSSebastian Grimberg   if (!ceed->BasisCreateHdiv) {
1274c4e3f59bSSebastian Grimberg     Ceed delegate;
1275c4e3f59bSSebastian Grimberg     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis"));
1276c4e3f59bSSebastian Grimberg 
1277c4e3f59bSSebastian Grimberg     if (!delegate) {
1278c4e3f59bSSebastian Grimberg       // LCOV_EXCL_START
1279c4e3f59bSSebastian Grimberg       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl");
1280c4e3f59bSSebastian Grimberg       // LCOV_EXCL_STOP
1281c4e3f59bSSebastian Grimberg     }
1282c4e3f59bSSebastian Grimberg 
1283c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis));
1284c4e3f59bSSebastian Grimberg     return CEED_ERROR_SUCCESS;
1285c4e3f59bSSebastian Grimberg   }
1286c4e3f59bSSebastian Grimberg 
1287c4e3f59bSSebastian Grimberg   if (num_comp < 1) {
1288c4e3f59bSSebastian Grimberg     // LCOV_EXCL_START
1289c4e3f59bSSebastian Grimberg     return CeedError(ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component");
1290c4e3f59bSSebastian Grimberg     // LCOV_EXCL_STOP
1291c4e3f59bSSebastian Grimberg   }
1292c4e3f59bSSebastian Grimberg 
1293c4e3f59bSSebastian Grimberg   if (num_nodes < 1) {
1294c4e3f59bSSebastian Grimberg     // LCOV_EXCL_START
1295c4e3f59bSSebastian Grimberg     return CeedError(ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node");
1296c4e3f59bSSebastian Grimberg     // LCOV_EXCL_STOP
1297c4e3f59bSSebastian Grimberg   }
1298c4e3f59bSSebastian Grimberg 
1299c4e3f59bSSebastian Grimberg   if (num_qpts < 1) {
1300c4e3f59bSSebastian Grimberg     // LCOV_EXCL_START
1301c4e3f59bSSebastian Grimberg     return CeedError(ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point");
1302c4e3f59bSSebastian Grimberg     // LCOV_EXCL_STOP
1303c4e3f59bSSebastian Grimberg   }
1304c4e3f59bSSebastian Grimberg 
1305c4e3f59bSSebastian Grimberg   CeedCall(CeedCalloc(1, basis));
1306c4e3f59bSSebastian Grimberg 
1307c4e3f59bSSebastian Grimberg   CeedCall(CeedBasisGetTopologyDimension(topo, &dim));
1308c4e3f59bSSebastian Grimberg   curl_comp = (dim < 3) ? 1 : dim;
1309c4e3f59bSSebastian Grimberg 
1310c4e3f59bSSebastian Grimberg   (*basis)->ceed = ceed;
1311c4e3f59bSSebastian Grimberg   CeedCall(CeedReference(ceed));
1312c4e3f59bSSebastian Grimberg   (*basis)->ref_count    = 1;
1313c4e3f59bSSebastian Grimberg   (*basis)->tensor_basis = 0;
1314c4e3f59bSSebastian Grimberg   (*basis)->dim          = dim;
1315c4e3f59bSSebastian Grimberg   (*basis)->topo         = topo;
1316c4e3f59bSSebastian Grimberg   (*basis)->num_comp     = num_comp;
1317c4e3f59bSSebastian Grimberg   (*basis)->P            = P;
1318c4e3f59bSSebastian Grimberg   (*basis)->Q            = Q;
1319c4e3f59bSSebastian Grimberg   (*basis)->fe_space     = CEED_FE_SPACE_HCURL;
1320c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d));
1321c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d));
1322c4e3f59bSSebastian Grimberg   if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0]));
1323c4e3f59bSSebastian Grimberg   if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0]));
1324c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp));
1325c4e3f59bSSebastian Grimberg   CeedCall(CeedMalloc(curl_comp * Q * P, &(*basis)->curl));
1326c4e3f59bSSebastian Grimberg   if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0]));
1327c4e3f59bSSebastian Grimberg   if (curl) memcpy((*basis)->curl, curl, curl_comp * Q * P * sizeof(curl[0]));
1328c4e3f59bSSebastian Grimberg   CeedCall(ceed->BasisCreateHcurl(topo, dim, P, Q, interp, curl, q_ref, q_weight, *basis));
1329c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1330c4e3f59bSSebastian Grimberg }
1331c4e3f59bSSebastian Grimberg 
1332c4e3f59bSSebastian Grimberg /**
1333ea61e9acSJeremy L Thompson   @brief Create a CeedBasis for projection from the nodes of `basis_from` to the nodes of `basis_to`.
1334ba59ac12SSebastian Grimberg 
133515ad3917SSebastian Grimberg   Only `CEED_EVAL_INTERP` will be valid for the new basis, `basis_project`. For H^1 spaces, `CEED_EVAL_GRAD` will also be valid.
1336ea61e9acSJeremy L Thompson   The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pesudoinverse `interp_to^+` is given by QR
133715ad3917SSebastian Grimberg factorization.  The gradient (for the H^1 case) is given by `grad_project = interp_to^+ * grad_from`.
133815ad3917SSebastian Grimberg 
133915ad3917SSebastian Grimberg   Note: `basis_from` and `basis_to` must have compatible quadrature spaces.
134015ad3917SSebastian Grimberg 
1341ba59ac12SSebastian Grimberg   Note: `basis_project` will have the same number of components as `basis_from`, regardless of the number of components that `basis_to` has. If
1342ea61e9acSJeremy L Thompson `basis_from` has 3 components and `basis_to` has 5 components, then `basis_project` will have 3 components.
1343f113e5dcSJeremy L Thompson 
1344f113e5dcSJeremy L Thompson   @param[in]  basis_from    CeedBasis to prolong from
1345446e7af4SJeremy L Thompson   @param[in]  basis_to      CeedBasis to prolong to
1346ea61e9acSJeremy L Thompson   @param[out] basis_project Address of the variable where the newly created CeedBasis will be stored.
1347f113e5dcSJeremy L Thompson 
1348f113e5dcSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1349f113e5dcSJeremy L Thompson 
1350f113e5dcSJeremy L Thompson   @ref User
1351f113e5dcSJeremy L Thompson **/
13522b730f8bSJeremy L Thompson int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project) {
1353f113e5dcSJeremy L Thompson   Ceed ceed;
13542b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetCeed(basis_to, &ceed));
1355f113e5dcSJeremy L Thompson 
1356ecc88aebSJeremy L Thompson   // Create projection matrix
135714556e63SJeremy L Thompson   CeedScalar *interp_project, *grad_project;
13582b730f8bSJeremy L Thompson   CeedCall(CeedBasisCreateProjectionMatrices(basis_from, basis_to, &interp_project, &grad_project));
1359f113e5dcSJeremy L Thompson 
1360f113e5dcSJeremy L Thompson   // Build basis
1361f113e5dcSJeremy L Thompson   bool        is_tensor;
136215ad3917SSebastian Grimberg   CeedFESpace fe_space;
1363f113e5dcSJeremy L Thompson   CeedInt     dim, num_comp;
136414556e63SJeremy L Thompson   CeedScalar *q_ref, *q_weight;
13652b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis_to, &is_tensor));
136615ad3917SSebastian Grimberg   CeedCall(CeedBasisGetFESpace(basis_to, &fe_space));
13672b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis_to, &dim));
13682b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_from, &num_comp));
1369f113e5dcSJeremy L Thompson   if (is_tensor) {
1370f113e5dcSJeremy L Thompson     CeedInt P_1d_to, P_1d_from;
13712b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_1d_from));
13722b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_1d_to));
13732b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_to, &q_ref));
13742b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_to, &q_weight));
13752b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, interp_project, grad_project, q_ref, q_weight, basis_project));
1376f113e5dcSJeremy L Thompson   } else {
1377f113e5dcSJeremy L Thompson     CeedElemTopology topo;
13782b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetTopology(basis_to, &topo));
1379f113e5dcSJeremy L Thompson     CeedInt num_nodes_to, num_nodes_from;
13802b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_from, &num_nodes_from));
13812b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_to, &num_nodes_to));
13822b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_to * dim, &q_ref));
13832b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_to, &q_weight));
138415ad3917SSebastian Grimberg     switch (fe_space) {
138515ad3917SSebastian Grimberg       case CEED_FE_SPACE_H1:
13862b730f8bSJeremy L Thompson         CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, q_ref, q_weight, basis_project));
138715ad3917SSebastian Grimberg         break;
138815ad3917SSebastian Grimberg       case CEED_FE_SPACE_HDIV:
138915ad3917SSebastian Grimberg         CeedCall(
139015ad3917SSebastian Grimberg             CeedBasisCreateHdiv(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, q_ref, q_weight, basis_project));
139115ad3917SSebastian Grimberg         break;
139215ad3917SSebastian Grimberg       case CEED_FE_SPACE_HCURL:
139315ad3917SSebastian Grimberg         CeedCall(
139415ad3917SSebastian Grimberg             CeedBasisCreateHcurl(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, q_ref, q_weight, basis_project));
139515ad3917SSebastian Grimberg         break;
139615ad3917SSebastian Grimberg     }
1397f113e5dcSJeremy L Thompson   }
1398f113e5dcSJeremy L Thompson 
1399f113e5dcSJeremy L Thompson   // Cleanup
14002b730f8bSJeremy L Thompson   CeedCall(CeedFree(&interp_project));
14012b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_project));
14022b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref));
14032b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight));
1404f113e5dcSJeremy L Thompson 
1405f113e5dcSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1406f113e5dcSJeremy L Thompson }
1407f113e5dcSJeremy L Thompson 
1408f113e5dcSJeremy L Thompson /**
1409ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedBasis.
14109560d06aSjeremylt 
1411512bb800SJeremy 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.
1412512bb800SJeremy L Thompson   This CeedBasis will be destroyed if `basis_copy` is the only reference to this CeedBasis.
1413ea61e9acSJeremy L Thompson 
1414ea61e9acSJeremy L Thompson   @param[in]     basis      CeedBasis to copy reference to
1415ea61e9acSJeremy L Thompson   @param[in,out] basis_copy Variable to store copied reference
14169560d06aSjeremylt 
14179560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
14189560d06aSjeremylt 
14199560d06aSjeremylt   @ref User
14209560d06aSjeremylt **/
14219560d06aSjeremylt int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) {
1422393ac2cdSJeremy L Thompson   if (basis != CEED_BASIS_COLLOCATED) CeedCall(CeedBasisReference(basis));
14232b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(basis_copy));
14249560d06aSjeremylt   *basis_copy = basis;
14259560d06aSjeremylt   return CEED_ERROR_SUCCESS;
14269560d06aSjeremylt }
14279560d06aSjeremylt 
14289560d06aSjeremylt /**
14297a982d89SJeremy L. Thompson   @brief View a CeedBasis
14307a982d89SJeremy L. Thompson 
1431ea61e9acSJeremy L Thompson   @param[in] basis  CeedBasis to view
1432ea61e9acSJeremy L Thompson   @param[in] stream Stream to view to, e.g., stdout
14337a982d89SJeremy L. Thompson 
14347a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
14357a982d89SJeremy L. Thompson 
14367a982d89SJeremy L. Thompson   @ref User
14377a982d89SJeremy L. Thompson **/
14387a982d89SJeremy L. Thompson int CeedBasisView(CeedBasis basis, FILE *stream) {
143950c301a5SRezgar Shakeri   CeedElemTopology topo     = basis->topo;
1440c4e3f59bSSebastian Grimberg   CeedFESpace      fe_space = basis->fe_space;
1441c4e3f59bSSebastian Grimberg   CeedInt          q_comp   = 0;
14422b730f8bSJeremy L Thompson 
144350c301a5SRezgar Shakeri   // Print FE space and element topology of the basis
1444d1d35e2fSjeremylt   if (basis->tensor_basis) {
1445c4e3f59bSSebastian Grimberg     fprintf(stream, "CeedBasis (%s on a %s element): dim=%" CeedInt_FMT " P=%" CeedInt_FMT " Q=%" CeedInt_FMT "\n", CeedFESpaces[fe_space],
14462b730f8bSJeremy L Thompson             CeedElemTopologies[topo], basis->dim, basis->P_1d, basis->Q_1d);
144750c301a5SRezgar Shakeri   } else {
1448c4e3f59bSSebastian Grimberg     fprintf(stream, "CeedBasis (%s on a %s element): dim=%" CeedInt_FMT " P=%" CeedInt_FMT " Q=%" CeedInt_FMT "\n", CeedFESpaces[fe_space],
14492b730f8bSJeremy L Thompson             CeedElemTopologies[topo], basis->dim, basis->P, basis->Q);
145050c301a5SRezgar Shakeri   }
1451ea61e9acSJeremy L Thompson   // Print quadrature data, interpolation/gradient/divergence/curl of the basis
145250c301a5SRezgar Shakeri   if (basis->tensor_basis) {  // tensor basis
14532b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("qref1d", "\t% 12.8f", 1, basis->Q_1d, basis->q_ref_1d, stream));
14542b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("qweight1d", "\t% 12.8f", 1, basis->Q_1d, basis->q_weight_1d, stream));
14552b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("interp1d", "\t% 12.8f", basis->Q_1d, basis->P_1d, basis->interp_1d, stream));
14562b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("grad1d", "\t% 12.8f", basis->Q_1d, basis->P_1d, basis->grad_1d, stream));
145750c301a5SRezgar Shakeri   } else {  // non-tensor basis
14582b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("qref", "\t% 12.8f", 1, basis->Q * basis->dim, basis->q_ref_1d, stream));
14592b730f8bSJeremy L Thompson     CeedCall(CeedScalarView("qweight", "\t% 12.8f", 1, basis->Q, basis->q_weight_1d, stream));
1460c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp));
1461c4e3f59bSSebastian Grimberg     CeedCall(CeedScalarView("interp", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->interp, stream));
146250c301a5SRezgar Shakeri     if (basis->grad) {
1463c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp));
1464c4e3f59bSSebastian Grimberg       CeedCall(CeedScalarView("grad", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->grad, stream));
14657a982d89SJeremy L. Thompson     }
146650c301a5SRezgar Shakeri     if (basis->div) {
1467c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp));
1468c4e3f59bSSebastian Grimberg       CeedCall(CeedScalarView("div", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->div, stream));
1469c4e3f59bSSebastian Grimberg     }
1470c4e3f59bSSebastian Grimberg     if (basis->curl) {
1471c4e3f59bSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp));
1472c4e3f59bSSebastian Grimberg       CeedCall(CeedScalarView("curl", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->curl, stream));
147350c301a5SRezgar Shakeri     }
147450c301a5SRezgar Shakeri   }
1475e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
14767a982d89SJeremy L. Thompson }
14777a982d89SJeremy L. Thompson 
14787a982d89SJeremy L. Thompson /**
14797a982d89SJeremy L. Thompson   @brief Apply basis evaluation from nodes to quadrature points or vice versa
14807a982d89SJeremy L. Thompson 
1481ea61e9acSJeremy L Thompson   @param[in]  basis      CeedBasis to evaluate
1482ea61e9acSJeremy L Thompson   @param[in]  num_elem   The number of elements to apply the basis evaluation to;
1483ea61e9acSJeremy L Thompson                            the backend will specify the ordering in CeedElemRestrictionCreateBlocked()
1484ea61e9acSJeremy L Thompson   @param[in]  t_mode    \ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points;
1485ea61e9acSJeremy L Thompson                           \ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes
1486ea61e9acSJeremy L Thompson   @param[in]  eval_mode \ref CEED_EVAL_NONE to use values directly,
14877a982d89SJeremy L. Thompson                           \ref CEED_EVAL_INTERP to use interpolated values,
14887a982d89SJeremy L. Thompson                           \ref CEED_EVAL_GRAD to use gradients,
1489c4e3f59bSSebastian Grimberg                           \ref CEED_EVAL_DIV to use divergence,
1490c4e3f59bSSebastian Grimberg                           \ref CEED_EVAL_CURL to use curl,
14917a982d89SJeremy L. Thompson                           \ref CEED_EVAL_WEIGHT to use quadrature weights.
14927a982d89SJeremy L. Thompson   @param[in]  u        Input CeedVector
14937a982d89SJeremy L. Thompson   @param[out] v        Output CeedVector
14947a982d89SJeremy L. Thompson 
14957a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
14967a982d89SJeremy L. Thompson 
14977a982d89SJeremy L. Thompson   @ref User
14987a982d89SJeremy L. Thompson **/
14992b730f8bSJeremy L Thompson int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) {
15001f9221feSJeremy L Thompson   CeedSize u_length = 0, v_length;
1501c4e3f59bSSebastian Grimberg   CeedInt  dim, num_comp, q_comp, num_nodes, num_qpts;
15022b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
15032b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
1504c4e3f59bSSebastian Grimberg   CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
15052b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
15062b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
15072b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(v, &v_length));
15087a982d89SJeremy L. Thompson   if (u) {
15092b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetLength(u, &u_length));
15107a982d89SJeremy L. Thompson   }
15117a982d89SJeremy L. Thompson 
15122b730f8bSJeremy L Thompson   if (!basis->Apply) {
1513e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
15142b730f8bSJeremy L Thompson     return CeedError(basis->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisApply");
1515e15f9bd0SJeremy L Thompson     // LCOV_EXCL_STOP
15162b730f8bSJeremy L Thompson   }
1517e15f9bd0SJeremy L Thompson 
1518e15f9bd0SJeremy L Thompson   // Check compatibility of topological and geometrical dimensions
15192b730f8bSJeremy L Thompson   if ((t_mode == CEED_TRANSPOSE && (v_length % num_nodes != 0 || u_length % num_qpts != 0)) ||
15202b730f8bSJeremy L Thompson       (t_mode == CEED_NOTRANSPOSE && (u_length % num_nodes != 0 || v_length % num_qpts != 0))) {
15218229195eSjeremylt     // LCOV_EXCL_START
15222b730f8bSJeremy L Thompson     return CeedError(basis->ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions");
15238229195eSjeremylt     // LCOV_EXCL_STOP
15242b730f8bSJeremy L Thompson   }
15257a982d89SJeremy L. Thompson 
1526e15f9bd0SJeremy L Thompson   // Check vector lengths to prevent out of bounds issues
1527d1d35e2fSjeremylt   bool bad_dims = false;
1528d1d35e2fSjeremylt   switch (eval_mode) {
1529e15f9bd0SJeremy L Thompson     case CEED_EVAL_NONE:
15302b730f8bSJeremy L Thompson     case CEED_EVAL_INTERP:
15312b730f8bSJeremy L Thompson     case CEED_EVAL_GRAD:
1532c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
1533c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
1534c4e3f59bSSebastian Grimberg       bad_dims = ((t_mode == CEED_TRANSPOSE && (u_length < num_elem * num_comp * num_qpts * q_comp || v_length < num_elem * num_comp * num_nodes)) ||
1535c4e3f59bSSebastian Grimberg                   (t_mode == CEED_NOTRANSPOSE && (v_length < num_elem * num_qpts * num_comp * q_comp || u_length < num_elem * num_comp * num_nodes)));
1536e15f9bd0SJeremy L Thompson       break;
1537e15f9bd0SJeremy L Thompson     case CEED_EVAL_WEIGHT:
1538d1d35e2fSjeremylt       bad_dims = v_length < num_elem * num_qpts;
1539e15f9bd0SJeremy L Thompson       break;
1540e15f9bd0SJeremy L Thompson   }
15412b730f8bSJeremy L Thompson   if (bad_dims) {
1542e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
15432b730f8bSJeremy L Thompson     return CeedError(basis->ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode");
1544e15f9bd0SJeremy L Thompson     // LCOV_EXCL_STOP
15452b730f8bSJeremy L Thompson   }
1546e15f9bd0SJeremy L Thompson 
15472b730f8bSJeremy L Thompson   CeedCall(basis->Apply(basis, num_elem, t_mode, eval_mode, u, v));
1548e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
15497a982d89SJeremy L. Thompson }
15507a982d89SJeremy L. Thompson 
15517a982d89SJeremy L. Thompson /**
1552b7c9bbdaSJeremy L Thompson   @brief Get Ceed associated with a CeedBasis
1553b7c9bbdaSJeremy L Thompson 
1554ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
1555b7c9bbdaSJeremy L Thompson   @param[out] ceed  Variable to store Ceed
1556b7c9bbdaSJeremy L Thompson 
1557b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1558b7c9bbdaSJeremy L Thompson 
1559b7c9bbdaSJeremy L Thompson   @ref Advanced
1560b7c9bbdaSJeremy L Thompson **/
1561b7c9bbdaSJeremy L Thompson int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) {
1562b7c9bbdaSJeremy L Thompson   *ceed = basis->ceed;
1563b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1564b7c9bbdaSJeremy L Thompson }
1565b7c9bbdaSJeremy L Thompson 
1566b7c9bbdaSJeremy L Thompson /**
15679d007619Sjeremylt   @brief Get dimension for given CeedBasis
15689d007619Sjeremylt 
1569ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
15709d007619Sjeremylt   @param[out] dim   Variable to store dimension of basis
15719d007619Sjeremylt 
15729d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
15739d007619Sjeremylt 
1574b7c9bbdaSJeremy L Thompson   @ref Advanced
15759d007619Sjeremylt **/
15769d007619Sjeremylt int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) {
15779d007619Sjeremylt   *dim = basis->dim;
1578e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
15799d007619Sjeremylt }
15809d007619Sjeremylt 
15819d007619Sjeremylt /**
1582d99fa3c5SJeremy L Thompson   @brief Get topology for given CeedBasis
1583d99fa3c5SJeremy L Thompson 
1584ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
1585d99fa3c5SJeremy L Thompson   @param[out] topo  Variable to store topology of basis
1586d99fa3c5SJeremy L Thompson 
1587d99fa3c5SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1588d99fa3c5SJeremy L Thompson 
1589b7c9bbdaSJeremy L Thompson   @ref Advanced
1590d99fa3c5SJeremy L Thompson **/
1591d99fa3c5SJeremy L Thompson int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) {
1592d99fa3c5SJeremy L Thompson   *topo = basis->topo;
1593e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1594d99fa3c5SJeremy L Thompson }
1595d99fa3c5SJeremy L Thompson 
1596d99fa3c5SJeremy L Thompson /**
15979d007619Sjeremylt   @brief Get number of components for given CeedBasis
15989d007619Sjeremylt 
1599ea61e9acSJeremy L Thompson   @param[in]  basis    CeedBasis
1600d1d35e2fSjeremylt   @param[out] num_comp Variable to store number of components of basis
16019d007619Sjeremylt 
16029d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
16039d007619Sjeremylt 
1604b7c9bbdaSJeremy L Thompson   @ref Advanced
16059d007619Sjeremylt **/
1606d1d35e2fSjeremylt int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) {
1607d1d35e2fSjeremylt   *num_comp = basis->num_comp;
1608e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
16099d007619Sjeremylt }
16109d007619Sjeremylt 
16119d007619Sjeremylt /**
16129d007619Sjeremylt   @brief Get total number of nodes (in dim dimensions) of a CeedBasis
16139d007619Sjeremylt 
1614ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
16159d007619Sjeremylt   @param[out] P     Variable to store number of nodes
16169d007619Sjeremylt 
16179d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
16189d007619Sjeremylt 
16199d007619Sjeremylt   @ref Utility
16209d007619Sjeremylt **/
16219d007619Sjeremylt int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) {
16229d007619Sjeremylt   *P = basis->P;
1623e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
16249d007619Sjeremylt }
16259d007619Sjeremylt 
16269d007619Sjeremylt /**
16279d007619Sjeremylt   @brief Get total number of nodes (in 1 dimension) of a CeedBasis
16289d007619Sjeremylt 
1629ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
1630d1d35e2fSjeremylt   @param[out] P_1d  Variable to store number of nodes
16319d007619Sjeremylt 
16329d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
16339d007619Sjeremylt 
1634b7c9bbdaSJeremy L Thompson   @ref Advanced
16359d007619Sjeremylt **/
1636d1d35e2fSjeremylt int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) {
16372b730f8bSJeremy L Thompson   if (!basis->tensor_basis) {
16389d007619Sjeremylt     // LCOV_EXCL_START
16392b730f8bSJeremy L Thompson     return CeedError(basis->ceed, CEED_ERROR_MINOR, "Cannot supply P_1d for non-tensor basis");
16409d007619Sjeremylt     // LCOV_EXCL_STOP
16412b730f8bSJeremy L Thompson   }
16429d007619Sjeremylt 
1643d1d35e2fSjeremylt   *P_1d = basis->P_1d;
1644e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
16459d007619Sjeremylt }
16469d007619Sjeremylt 
16479d007619Sjeremylt /**
16489d007619Sjeremylt   @brief Get total number of quadrature points (in dim dimensions) of a CeedBasis
16499d007619Sjeremylt 
1650ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
16519d007619Sjeremylt   @param[out] Q     Variable to store number of quadrature points
16529d007619Sjeremylt 
16539d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
16549d007619Sjeremylt 
16559d007619Sjeremylt   @ref Utility
16569d007619Sjeremylt **/
16579d007619Sjeremylt int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) {
16589d007619Sjeremylt   *Q = basis->Q;
1659e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
16609d007619Sjeremylt }
16619d007619Sjeremylt 
16629d007619Sjeremylt /**
16639d007619Sjeremylt   @brief Get total number of quadrature points (in 1 dimension) of a CeedBasis
16649d007619Sjeremylt 
1665ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
1666d1d35e2fSjeremylt   @param[out] Q_1d  Variable to store number of quadrature points
16679d007619Sjeremylt 
16689d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
16699d007619Sjeremylt 
1670b7c9bbdaSJeremy L Thompson   @ref Advanced
16719d007619Sjeremylt **/
1672d1d35e2fSjeremylt int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) {
16732b730f8bSJeremy L Thompson   if (!basis->tensor_basis) {
16749d007619Sjeremylt     // LCOV_EXCL_START
16752b730f8bSJeremy L Thompson     return CeedError(basis->ceed, CEED_ERROR_MINOR, "Cannot supply Q_1d for non-tensor basis");
16769d007619Sjeremylt     // LCOV_EXCL_STOP
16772b730f8bSJeremy L Thompson   }
16789d007619Sjeremylt 
1679d1d35e2fSjeremylt   *Q_1d = basis->Q_1d;
1680e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
16819d007619Sjeremylt }
16829d007619Sjeremylt 
16839d007619Sjeremylt /**
1684ea61e9acSJeremy L Thompson   @brief Get reference coordinates of quadrature points (in dim dimensions) of a CeedBasis
16859d007619Sjeremylt 
1686ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
1687d1d35e2fSjeremylt   @param[out] q_ref Variable to store reference coordinates of quadrature points
16889d007619Sjeremylt 
16899d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
16909d007619Sjeremylt 
1691b7c9bbdaSJeremy L Thompson   @ref Advanced
16929d007619Sjeremylt **/
1693d1d35e2fSjeremylt int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) {
1694d1d35e2fSjeremylt   *q_ref = basis->q_ref_1d;
1695e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
16969d007619Sjeremylt }
16979d007619Sjeremylt 
16989d007619Sjeremylt /**
1699ea61e9acSJeremy L Thompson   @brief Get quadrature weights of quadrature points (in dim dimensions) of a CeedBasis
17009d007619Sjeremylt 
1701ea61e9acSJeremy L Thompson   @param[in]  basis    CeedBasis
1702d1d35e2fSjeremylt   @param[out] q_weight Variable to store quadrature weights
17039d007619Sjeremylt 
17049d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
17059d007619Sjeremylt 
1706b7c9bbdaSJeremy L Thompson   @ref Advanced
17079d007619Sjeremylt **/
1708d1d35e2fSjeremylt int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) {
1709d1d35e2fSjeremylt   *q_weight = basis->q_weight_1d;
1710e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17119d007619Sjeremylt }
17129d007619Sjeremylt 
17139d007619Sjeremylt /**
17149d007619Sjeremylt   @brief Get interpolation matrix of a CeedBasis
17159d007619Sjeremylt 
1716ea61e9acSJeremy L Thompson   @param[in]  basis  CeedBasis
17179d007619Sjeremylt   @param[out] interp Variable to store interpolation matrix
17189d007619Sjeremylt 
17199d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
17209d007619Sjeremylt 
1721b7c9bbdaSJeremy L Thompson   @ref Advanced
17229d007619Sjeremylt **/
17236c58de82SJeremy L Thompson int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) {
1724d1d35e2fSjeremylt   if (!basis->interp && basis->tensor_basis) {
17259d007619Sjeremylt     // Allocate
17262b730f8bSJeremy L Thompson     CeedCall(CeedMalloc(basis->Q * basis->P, &basis->interp));
17279d007619Sjeremylt 
17289d007619Sjeremylt     // Initialize
17292b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < basis->Q * basis->P; i++) basis->interp[i] = 1.0;
17309d007619Sjeremylt 
17319d007619Sjeremylt     // Calculate
17322b730f8bSJeremy L Thompson     for (CeedInt d = 0; d < basis->dim; d++) {
17332b730f8bSJeremy L Thompson       for (CeedInt qpt = 0; qpt < basis->Q; qpt++) {
17349d007619Sjeremylt         for (CeedInt node = 0; node < basis->P; node++) {
1735d1d35e2fSjeremylt           CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d;
1736d1d35e2fSjeremylt           CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d;
1737d1d35e2fSjeremylt           basis->interp[qpt * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p];
17389d007619Sjeremylt         }
17399d007619Sjeremylt       }
17402b730f8bSJeremy L Thompson     }
17412b730f8bSJeremy L Thompson   }
17429d007619Sjeremylt   *interp = basis->interp;
1743e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17449d007619Sjeremylt }
17459d007619Sjeremylt 
17469d007619Sjeremylt /**
17479d007619Sjeremylt   @brief Get 1D interpolation matrix of a tensor product CeedBasis
17489d007619Sjeremylt 
1749ea61e9acSJeremy L Thompson   @param[in]  basis     CeedBasis
1750d1d35e2fSjeremylt   @param[out] interp_1d Variable to store interpolation matrix
17519d007619Sjeremylt 
17529d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
17539d007619Sjeremylt 
17549d007619Sjeremylt   @ref Backend
17559d007619Sjeremylt **/
1756d1d35e2fSjeremylt int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) {
17572b730f8bSJeremy L Thompson   if (!basis->tensor_basis) {
17589d007619Sjeremylt     // LCOV_EXCL_START
17592b730f8bSJeremy L Thompson     return CeedError(basis->ceed, CEED_ERROR_MINOR, "CeedBasis is not a tensor product basis.");
17609d007619Sjeremylt     // LCOV_EXCL_STOP
17612b730f8bSJeremy L Thompson   }
17629d007619Sjeremylt 
1763d1d35e2fSjeremylt   *interp_1d = basis->interp_1d;
1764e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17659d007619Sjeremylt }
17669d007619Sjeremylt 
17679d007619Sjeremylt /**
17689d007619Sjeremylt   @brief Get gradient matrix of a CeedBasis
17699d007619Sjeremylt 
1770ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
17719d007619Sjeremylt   @param[out] grad  Variable to store gradient matrix
17729d007619Sjeremylt 
17739d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
17749d007619Sjeremylt 
1775b7c9bbdaSJeremy L Thompson   @ref Advanced
17769d007619Sjeremylt **/
17776c58de82SJeremy L Thompson int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) {
1778d1d35e2fSjeremylt   if (!basis->grad && basis->tensor_basis) {
17799d007619Sjeremylt     // Allocate
17802b730f8bSJeremy L Thompson     CeedCall(CeedMalloc(basis->dim * basis->Q * basis->P, &basis->grad));
17819d007619Sjeremylt 
17829d007619Sjeremylt     // Initialize
17832b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < basis->dim * basis->Q * basis->P; i++) basis->grad[i] = 1.0;
17849d007619Sjeremylt 
17859d007619Sjeremylt     // Calculate
17862b730f8bSJeremy L Thompson     for (CeedInt d = 0; d < basis->dim; d++) {
17872b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < basis->dim; i++) {
17882b730f8bSJeremy L Thompson         for (CeedInt qpt = 0; qpt < basis->Q; qpt++) {
17899d007619Sjeremylt           for (CeedInt node = 0; node < basis->P; node++) {
1790d1d35e2fSjeremylt             CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d;
1791d1d35e2fSjeremylt             CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d;
17922b730f8bSJeremy L Thompson             if (i == d) basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->grad_1d[q * basis->P_1d + p];
17932b730f8bSJeremy L Thompson             else basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p];
17942b730f8bSJeremy L Thompson           }
17952b730f8bSJeremy L Thompson         }
17962b730f8bSJeremy L Thompson       }
17979d007619Sjeremylt     }
17989d007619Sjeremylt   }
17999d007619Sjeremylt   *grad = basis->grad;
1800e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18019d007619Sjeremylt }
18029d007619Sjeremylt 
18039d007619Sjeremylt /**
18049d007619Sjeremylt   @brief Get 1D gradient matrix of a tensor product CeedBasis
18059d007619Sjeremylt 
1806ea61e9acSJeremy L Thompson   @param[in]  basis   CeedBasis
1807d1d35e2fSjeremylt   @param[out] grad_1d Variable to store gradient matrix
18089d007619Sjeremylt 
18099d007619Sjeremylt   @return An error code: 0 - success, otherwise - failure
18109d007619Sjeremylt 
1811b7c9bbdaSJeremy L Thompson   @ref Advanced
18129d007619Sjeremylt **/
1813d1d35e2fSjeremylt int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) {
18142b730f8bSJeremy L Thompson   if (!basis->tensor_basis) {
18159d007619Sjeremylt     // LCOV_EXCL_START
18162b730f8bSJeremy L Thompson     return CeedError(basis->ceed, CEED_ERROR_MINOR, "CeedBasis is not a tensor product basis.");
18179d007619Sjeremylt     // LCOV_EXCL_STOP
18182b730f8bSJeremy L Thompson   }
18199d007619Sjeremylt 
1820d1d35e2fSjeremylt   *grad_1d = basis->grad_1d;
1821e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18229d007619Sjeremylt }
18239d007619Sjeremylt 
18249d007619Sjeremylt /**
182550c301a5SRezgar Shakeri   @brief Get divergence matrix of a CeedBasis
182650c301a5SRezgar Shakeri 
1827ea61e9acSJeremy L Thompson   @param[in]  basis CeedBasis
182850c301a5SRezgar Shakeri   @param[out] div   Variable to store divergence matrix
182950c301a5SRezgar Shakeri 
183050c301a5SRezgar Shakeri   @return An error code: 0 - success, otherwise - failure
183150c301a5SRezgar Shakeri 
183250c301a5SRezgar Shakeri   @ref Advanced
183350c301a5SRezgar Shakeri **/
183450c301a5SRezgar Shakeri int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) {
18352b730f8bSJeremy L Thompson   if (!basis->div) {
183650c301a5SRezgar Shakeri     // LCOV_EXCL_START
18372b730f8bSJeremy L Thompson     return CeedError(basis->ceed, CEED_ERROR_MINOR, "CeedBasis does not have divergence matrix.");
183850c301a5SRezgar Shakeri     // LCOV_EXCL_STOP
18392b730f8bSJeremy L Thompson   }
184050c301a5SRezgar Shakeri 
184150c301a5SRezgar Shakeri   *div = basis->div;
184250c301a5SRezgar Shakeri   return CEED_ERROR_SUCCESS;
184350c301a5SRezgar Shakeri }
184450c301a5SRezgar Shakeri 
184550c301a5SRezgar Shakeri /**
1846c4e3f59bSSebastian Grimberg   @brief Get curl matrix of a CeedBasis
1847c4e3f59bSSebastian Grimberg 
1848c4e3f59bSSebastian Grimberg   @param[in]  basis CeedBasis
1849c4e3f59bSSebastian Grimberg   @param[out] curl  Variable to store curl matrix
1850c4e3f59bSSebastian Grimberg 
1851c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1852c4e3f59bSSebastian Grimberg 
1853c4e3f59bSSebastian Grimberg   @ref Advanced
1854c4e3f59bSSebastian Grimberg **/
1855c4e3f59bSSebastian Grimberg int CeedBasisGetCurl(CeedBasis basis, const CeedScalar **curl) {
1856c4e3f59bSSebastian Grimberg   if (!basis->curl) {
1857c4e3f59bSSebastian Grimberg     // LCOV_EXCL_START
1858c4e3f59bSSebastian Grimberg     return CeedError(basis->ceed, CEED_ERROR_MINOR, "CeedBasis does not have curl matrix.");
1859c4e3f59bSSebastian Grimberg     // LCOV_EXCL_STOP
1860c4e3f59bSSebastian Grimberg   }
1861c4e3f59bSSebastian Grimberg 
1862c4e3f59bSSebastian Grimberg   *curl = basis->curl;
1863c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1864c4e3f59bSSebastian Grimberg }
1865c4e3f59bSSebastian Grimberg 
1866c4e3f59bSSebastian Grimberg /**
18677a982d89SJeremy L. Thompson   @brief Destroy a CeedBasis
18687a982d89SJeremy L. Thompson 
1869ea61e9acSJeremy L Thompson   @param[in,out] basis CeedBasis to destroy
18707a982d89SJeremy L. Thompson 
18717a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
18727a982d89SJeremy L. Thompson 
18737a982d89SJeremy L. Thompson   @ref User
18747a982d89SJeremy L. Thompson **/
18757a982d89SJeremy L. Thompson int CeedBasisDestroy(CeedBasis *basis) {
18767425e127SJeremy L Thompson   if (!*basis || *basis == CEED_BASIS_COLLOCATED || --(*basis)->ref_count > 0) {
1877ad6481ceSJeremy L Thompson     *basis = NULL;
1878ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1879ad6481ceSJeremy L Thompson   }
18802b730f8bSJeremy L Thompson   if ((*basis)->Destroy) CeedCall((*basis)->Destroy(*basis));
18812b730f8bSJeremy L Thompson   if ((*basis)->contract) CeedCall(CeedTensorContractDestroy(&(*basis)->contract));
1882c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->q_ref_1d));
1883c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->q_weight_1d));
18842b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->interp));
18852b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->interp_1d));
18862b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->grad));
18872b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*basis)->grad_1d));
1888c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->div));
1889c4e3f59bSSebastian Grimberg   CeedCall(CeedFree(&(*basis)->curl));
18902b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*basis)->ceed));
18912b730f8bSJeremy L Thompson   CeedCall(CeedFree(basis));
1892e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18937a982d89SJeremy L. Thompson }
18947a982d89SJeremy L. Thompson 
18957a982d89SJeremy L. Thompson /**
1896b11c1e72Sjeremylt   @brief Construct a Gauss-Legendre quadrature
1897b11c1e72Sjeremylt 
1898ea61e9acSJeremy L Thompson   @param[in]  Q           Number of quadrature points (integrates polynomials of degree 2*Q-1 exactly)
1899d1d35e2fSjeremylt   @param[out] q_ref_1d    Array of length Q to hold the abscissa on [-1, 1]
1900d1d35e2fSjeremylt   @param[out] q_weight_1d Array of length Q to hold the weights
1901b11c1e72Sjeremylt 
1902b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1903dfdf5a53Sjeremylt 
1904dfdf5a53Sjeremylt   @ref Utility
1905b11c1e72Sjeremylt **/
19062b730f8bSJeremy L Thompson int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) {
1907d7b241e6Sjeremylt   // Allocate
1908d7b241e6Sjeremylt   CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0 * atan(1.0);
1909d1d35e2fSjeremylt   // Build q_ref_1d, q_weight_1d
191092ae7e47SJeremy L Thompson   for (CeedInt i = 0; i <= Q / 2; i++) {
1911d7b241e6Sjeremylt     // Guess
1912d7b241e6Sjeremylt     xi = cos(PI * (CeedScalar)(2 * i + 1) / ((CeedScalar)(2 * Q)));
1913d7b241e6Sjeremylt     // Pn(xi)
1914d7b241e6Sjeremylt     P0 = 1.0;
1915d7b241e6Sjeremylt     P1 = xi;
1916d7b241e6Sjeremylt     P2 = 0.0;
191792ae7e47SJeremy L Thompson     for (CeedInt j = 2; j <= Q; j++) {
1918d7b241e6Sjeremylt       P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
1919d7b241e6Sjeremylt       P0 = P1;
1920d7b241e6Sjeremylt       P1 = P2;
1921d7b241e6Sjeremylt     }
1922d7b241e6Sjeremylt     // First Newton Step
1923d7b241e6Sjeremylt     dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
1924d7b241e6Sjeremylt     xi  = xi - P2 / dP2;
1925d7b241e6Sjeremylt     // Newton to convergence
192692ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < 100 && fabs(P2) > 10 * CEED_EPSILON; k++) {
1927d7b241e6Sjeremylt       P0 = 1.0;
1928d7b241e6Sjeremylt       P1 = xi;
192992ae7e47SJeremy L Thompson       for (CeedInt j = 2; j <= Q; j++) {
1930d7b241e6Sjeremylt         P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
1931d7b241e6Sjeremylt         P0 = P1;
1932d7b241e6Sjeremylt         P1 = P2;
1933d7b241e6Sjeremylt       }
1934d7b241e6Sjeremylt       dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
1935d7b241e6Sjeremylt       xi  = xi - P2 / dP2;
1936d7b241e6Sjeremylt     }
1937d7b241e6Sjeremylt     // Save xi, wi
1938d7b241e6Sjeremylt     wi                     = 2.0 / ((1.0 - xi * xi) * dP2 * dP2);
1939d1d35e2fSjeremylt     q_weight_1d[i]         = wi;
1940d1d35e2fSjeremylt     q_weight_1d[Q - 1 - i] = wi;
1941d1d35e2fSjeremylt     q_ref_1d[i]            = -xi;
1942d1d35e2fSjeremylt     q_ref_1d[Q - 1 - i]    = xi;
1943d7b241e6Sjeremylt   }
1944e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1945d7b241e6Sjeremylt }
1946d7b241e6Sjeremylt 
1947b11c1e72Sjeremylt /**
1948b11c1e72Sjeremylt   @brief Construct a Gauss-Legendre-Lobatto quadrature
1949b11c1e72Sjeremylt 
1950ea61e9acSJeremy L Thompson   @param[in]  Q           Number of quadrature points (integrates polynomials of degree 2*Q-3 exactly)
1951d1d35e2fSjeremylt   @param[out] q_ref_1d    Array of length Q to hold the abscissa on [-1, 1]
1952d1d35e2fSjeremylt   @param[out] q_weight_1d Array of length Q to hold the weights
1953b11c1e72Sjeremylt 
1954b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1955dfdf5a53Sjeremylt 
1956dfdf5a53Sjeremylt   @ref Utility
1957b11c1e72Sjeremylt **/
19582b730f8bSJeremy L Thompson int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) {
1959d7b241e6Sjeremylt   // Allocate
1960d7b241e6Sjeremylt   CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0 * atan(1.0);
1961d1d35e2fSjeremylt   // Build q_ref_1d, q_weight_1d
1962d7b241e6Sjeremylt   // Set endpoints
19632b730f8bSJeremy L Thompson   if (Q < 2) {
1964b0d62198Sjeremylt     // LCOV_EXCL_START
19652b730f8bSJeremy L Thompson     return CeedError(NULL, CEED_ERROR_DIMENSION, "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q);
1966b0d62198Sjeremylt     // LCOV_EXCL_STOP
19672b730f8bSJeremy L Thompson   }
1968d7b241e6Sjeremylt   wi = 2.0 / ((CeedScalar)(Q * (Q - 1)));
1969d1d35e2fSjeremylt   if (q_weight_1d) {
1970d1d35e2fSjeremylt     q_weight_1d[0]     = wi;
1971d1d35e2fSjeremylt     q_weight_1d[Q - 1] = wi;
1972d7b241e6Sjeremylt   }
1973d1d35e2fSjeremylt   q_ref_1d[0]     = -1.0;
1974d1d35e2fSjeremylt   q_ref_1d[Q - 1] = 1.0;
1975d7b241e6Sjeremylt   // Interior
197692ae7e47SJeremy L Thompson   for (CeedInt i = 1; i <= (Q - 1) / 2; i++) {
1977d7b241e6Sjeremylt     // Guess
1978d7b241e6Sjeremylt     xi = cos(PI * (CeedScalar)(i) / (CeedScalar)(Q - 1));
1979d7b241e6Sjeremylt     // Pn(xi)
1980d7b241e6Sjeremylt     P0 = 1.0;
1981d7b241e6Sjeremylt     P1 = xi;
1982d7b241e6Sjeremylt     P2 = 0.0;
198392ae7e47SJeremy L Thompson     for (CeedInt j = 2; j < Q; j++) {
1984d7b241e6Sjeremylt       P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
1985d7b241e6Sjeremylt       P0 = P1;
1986d7b241e6Sjeremylt       P1 = P2;
1987d7b241e6Sjeremylt     }
1988d7b241e6Sjeremylt     // First Newton step
1989d7b241e6Sjeremylt     dP2  = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
1990d7b241e6Sjeremylt     d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi);
1991d7b241e6Sjeremylt     xi   = xi - dP2 / d2P2;
1992d7b241e6Sjeremylt     // Newton to convergence
199392ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < 100 && fabs(dP2) > 10 * CEED_EPSILON; k++) {
1994d7b241e6Sjeremylt       P0 = 1.0;
1995d7b241e6Sjeremylt       P1 = xi;
199692ae7e47SJeremy L Thompson       for (CeedInt j = 2; j < Q; j++) {
1997d7b241e6Sjeremylt         P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j));
1998d7b241e6Sjeremylt         P0 = P1;
1999d7b241e6Sjeremylt         P1 = P2;
2000d7b241e6Sjeremylt       }
2001d7b241e6Sjeremylt       dP2  = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0);
2002d7b241e6Sjeremylt       d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi);
2003d7b241e6Sjeremylt       xi   = xi - dP2 / d2P2;
2004d7b241e6Sjeremylt     }
2005d7b241e6Sjeremylt     // Save xi, wi
2006d7b241e6Sjeremylt     wi = 2.0 / (((CeedScalar)(Q * (Q - 1))) * P2 * P2);
2007d1d35e2fSjeremylt     if (q_weight_1d) {
2008d1d35e2fSjeremylt       q_weight_1d[i]         = wi;
2009d1d35e2fSjeremylt       q_weight_1d[Q - 1 - i] = wi;
2010d7b241e6Sjeremylt     }
2011d1d35e2fSjeremylt     q_ref_1d[i]         = -xi;
2012d1d35e2fSjeremylt     q_ref_1d[Q - 1 - i] = xi;
2013d7b241e6Sjeremylt   }
2014e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2015d7b241e6Sjeremylt }
2016d7b241e6Sjeremylt 
2017d7b241e6Sjeremylt /// @}
2018