13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3d7b241e6Sjeremylt // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5d7b241e6Sjeremylt // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7d7b241e6Sjeremylt 83d576824SJeremy L Thompson #include <ceed-impl.h> 949aac155SJeremy L Thompson #include <ceed.h> 102b730f8bSJeremy L Thompson #include <ceed/backend.h> 11d7b241e6Sjeremylt #include <math.h> 123d576824SJeremy L Thompson #include <stdbool.h> 13d7b241e6Sjeremylt #include <stdio.h> 14d7b241e6Sjeremylt #include <string.h> 15d7b241e6Sjeremylt 167a982d89SJeremy L. Thompson /// @file 177a982d89SJeremy L. Thompson /// Implementation of CeedBasis interfaces 187a982d89SJeremy L. Thompson 19d7b241e6Sjeremylt /// @cond DOXYGEN_SKIP 20356036faSJeremy L Thompson static struct CeedBasis_private ceed_basis_none; 21d7b241e6Sjeremylt /// @endcond 22d7b241e6Sjeremylt 237a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser 247a982d89SJeremy L. Thompson /// @{ 257a982d89SJeremy L. Thompson 26ac5aa7bcSJeremy L Thompson /// Argument for CeedOperatorSetField indicating that the field does not require a CeedBasis 27356036faSJeremy L Thompson const CeedBasis CEED_BASIS_NONE = &ceed_basis_none; 28356036faSJeremy L Thompson 29356036faSJeremy L Thompson /// This feature will be removed. Use CEED_BASIS_NONE. 30356036faSJeremy L Thompson const CeedBasis CEED_BASIS_COLLOCATED = &ceed_basis_none; 317a982d89SJeremy L. Thompson 327a982d89SJeremy L. Thompson /// @} 337a982d89SJeremy L. Thompson 347a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 357a982d89SJeremy L. Thompson /// CeedBasis Library Internal Functions 367a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 377a982d89SJeremy L. Thompson /// @addtogroup CeedBasisDeveloper 387a982d89SJeremy L. Thompson /// @{ 397a982d89SJeremy L. Thompson 407a982d89SJeremy L. Thompson /** 413778dbaaSJeremy L Thompson @brief Compute Chebyshev polynomial values at a point 423778dbaaSJeremy L Thompson 433778dbaaSJeremy L Thompson @param[in] x Coordinate to evaluate Chebyshev polynomials at 443778dbaaSJeremy L Thompson @param[in] n Number of Chebyshev polynomials to evaluate, n >= 2 453778dbaaSJeremy L Thompson @param[out] chebyshev_x Array of Chebyshev polynomial values 463778dbaaSJeremy L Thompson 473778dbaaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 483778dbaaSJeremy L Thompson 493778dbaaSJeremy L Thompson @ref Developer 503778dbaaSJeremy L Thompson **/ 513778dbaaSJeremy L Thompson static int CeedChebyshevPolynomialsAtPoint(CeedScalar x, CeedInt n, CeedScalar *chebyshev_x) { 523778dbaaSJeremy L Thompson chebyshev_x[0] = 1.0; 533778dbaaSJeremy L Thompson chebyshev_x[1] = 2 * x; 543778dbaaSJeremy L Thompson for (CeedInt i = 2; i < n; i++) chebyshev_x[i] = 2 * x * chebyshev_x[i - 1] - chebyshev_x[i - 2]; 553778dbaaSJeremy L Thompson return CEED_ERROR_SUCCESS; 563778dbaaSJeremy L Thompson } 573778dbaaSJeremy L Thompson 583778dbaaSJeremy L Thompson /** 593778dbaaSJeremy L Thompson @brief Compute values of the derivative of Chebyshev polynomials at a point 603778dbaaSJeremy L Thompson 613778dbaaSJeremy L Thompson @param[in] x Coordinate to evaluate derivative of Chebyshev polynomials at 623778dbaaSJeremy L Thompson @param[in] n Number of Chebyshev polynomials to evaluate, n >= 2 636cec60aaSJed Brown @param[out] chebyshev_dx Array of Chebyshev polynomial derivative values 643778dbaaSJeremy L Thompson 653778dbaaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 663778dbaaSJeremy L Thompson 673778dbaaSJeremy L Thompson @ref Developer 683778dbaaSJeremy L Thompson **/ 693778dbaaSJeremy L Thompson static int CeedChebyshevDerivativeAtPoint(CeedScalar x, CeedInt n, CeedScalar *chebyshev_dx) { 703778dbaaSJeremy L Thompson CeedScalar chebyshev_x[3]; 713778dbaaSJeremy L Thompson 723778dbaaSJeremy L Thompson chebyshev_x[1] = 1.0; 733778dbaaSJeremy L Thompson chebyshev_x[2] = 2 * x; 743778dbaaSJeremy L Thompson chebyshev_dx[0] = 0.0; 753778dbaaSJeremy L Thompson chebyshev_dx[1] = 2.0; 763778dbaaSJeremy L Thompson for (CeedInt i = 2; i < n; i++) { 773778dbaaSJeremy L Thompson chebyshev_x[0] = chebyshev_x[1]; 783778dbaaSJeremy L Thompson chebyshev_x[1] = chebyshev_x[2]; 793778dbaaSJeremy L Thompson chebyshev_x[2] = 2 * x * chebyshev_x[1] - chebyshev_x[0]; 803778dbaaSJeremy L Thompson chebyshev_dx[i] = 2 * x * chebyshev_dx[i - 1] + 2 * chebyshev_x[1] - chebyshev_dx[i - 2]; 813778dbaaSJeremy L Thompson } 823778dbaaSJeremy L Thompson return CEED_ERROR_SUCCESS; 833778dbaaSJeremy L Thompson } 843778dbaaSJeremy L Thompson 853778dbaaSJeremy L Thompson /** 867a982d89SJeremy L. Thompson @brief Compute Householder reflection 877a982d89SJeremy L. Thompson 88ea61e9acSJeremy L Thompson Computes A = (I - b v v^T) A, where A is an mxn matrix indexed as A[i*row + j*col] 897a982d89SJeremy L. Thompson 907a982d89SJeremy L. Thompson @param[in,out] A Matrix to apply Householder reflection to, in place 91ea61e9acSJeremy L Thompson @param[in] v Householder vector 92ea61e9acSJeremy L Thompson @param[in] b Scaling factor 93ea61e9acSJeremy L Thompson @param[in] m Number of rows in A 94ea61e9acSJeremy L Thompson @param[in] n Number of columns in A 95ea61e9acSJeremy L Thompson @param[in] row Row stride 96ea61e9acSJeremy L Thompson @param[in] col Col stride 977a982d89SJeremy L. Thompson 987a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 997a982d89SJeremy L. Thompson 1007a982d89SJeremy L. Thompson @ref Developer 1017a982d89SJeremy L. Thompson **/ 1022b730f8bSJeremy L Thompson static int CeedHouseholderReflect(CeedScalar *A, const CeedScalar *v, CeedScalar b, CeedInt m, CeedInt n, CeedInt row, CeedInt col) { 1037a982d89SJeremy L. Thompson for (CeedInt j = 0; j < n; j++) { 1047a982d89SJeremy L. Thompson CeedScalar w = A[0 * row + j * col]; 1051c66c397SJeremy L Thompson 1062b730f8bSJeremy L Thompson for (CeedInt i = 1; i < m; i++) w += v[i] * A[i * row + j * col]; 1077a982d89SJeremy L. Thompson A[0 * row + j * col] -= b * w; 1082b730f8bSJeremy L Thompson for (CeedInt i = 1; i < m; i++) A[i * row + j * col] -= b * w * v[i]; 1097a982d89SJeremy L. Thompson } 110e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1117a982d89SJeremy L. Thompson } 1127a982d89SJeremy L. Thompson 1137a982d89SJeremy L. Thompson /** 1147a982d89SJeremy L. Thompson @brief Compute Givens rotation 1157a982d89SJeremy L. Thompson 116ea61e9acSJeremy 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] 1177a982d89SJeremy L. Thompson 1187a982d89SJeremy L. Thompson @param[in,out] A Row major matrix to apply Givens rotation to, in place 119ea61e9acSJeremy L Thompson @param[in] c Cosine factor 120ea61e9acSJeremy L Thompson @param[in] s Sine factor 121ea61e9acSJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to rotate the basis counter-clockwise, which has the effect of rotating columns of A clockwise; 1224cc79fe7SJed Brown @ref CEED_TRANSPOSE for the opposite rotation 123ea61e9acSJeremy L Thompson @param[in] i First row/column to apply rotation 124ea61e9acSJeremy L Thompson @param[in] k Second row/column to apply rotation 125ea61e9acSJeremy L Thompson @param[in] m Number of rows in A 126ea61e9acSJeremy L Thompson @param[in] n Number of columns in A 1277a982d89SJeremy L. Thompson 1287a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1297a982d89SJeremy L. Thompson 1307a982d89SJeremy L. Thompson @ref Developer 1317a982d89SJeremy L. Thompson **/ 1322b730f8bSJeremy L Thompson static int CeedGivensRotation(CeedScalar *A, CeedScalar c, CeedScalar s, CeedTransposeMode t_mode, CeedInt i, CeedInt k, CeedInt m, CeedInt n) { 133d1d35e2fSjeremylt CeedInt stride_j = 1, stride_ik = m, num_its = n; 1341c66c397SJeremy L Thompson 135d1d35e2fSjeremylt if (t_mode == CEED_NOTRANSPOSE) { 1362b730f8bSJeremy L Thompson stride_j = n; 1372b730f8bSJeremy L Thompson stride_ik = 1; 1382b730f8bSJeremy L Thompson num_its = m; 1397a982d89SJeremy L. Thompson } 1407a982d89SJeremy L. Thompson 1417a982d89SJeremy L. Thompson // Apply rotation 142d1d35e2fSjeremylt for (CeedInt j = 0; j < num_its; j++) { 143d1d35e2fSjeremylt CeedScalar tau1 = A[i * stride_ik + j * stride_j], tau2 = A[k * stride_ik + j * stride_j]; 1441c66c397SJeremy L Thompson 145d1d35e2fSjeremylt A[i * stride_ik + j * stride_j] = c * tau1 - s * tau2; 146d1d35e2fSjeremylt A[k * stride_ik + j * stride_j] = s * tau1 + c * tau2; 1477a982d89SJeremy L. Thompson } 148e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1497a982d89SJeremy L. Thompson } 1507a982d89SJeremy L. Thompson 1517a982d89SJeremy L. Thompson /** 1527a982d89SJeremy L. Thompson @brief View an array stored in a CeedBasis 1537a982d89SJeremy L. Thompson 1540a0da059Sjeremylt @param[in] name Name of array 155d1d35e2fSjeremylt @param[in] fp_fmt Printing format 1560a0da059Sjeremylt @param[in] m Number of rows in array 1570a0da059Sjeremylt @param[in] n Number of columns in array 1580a0da059Sjeremylt @param[in] a Array to be viewed 1590a0da059Sjeremylt @param[in] stream Stream to view to, e.g., stdout 1607a982d89SJeremy L. Thompson 1617a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1627a982d89SJeremy L. Thompson 1637a982d89SJeremy L. Thompson @ref Developer 1647a982d89SJeremy L. Thompson **/ 1652b730f8bSJeremy L Thompson static int CeedScalarView(const char *name, const char *fp_fmt, CeedInt m, CeedInt n, const CeedScalar *a, FILE *stream) { 166edf04919SJeremy L Thompson if (m > 1) { 167edf04919SJeremy L Thompson fprintf(stream, " %s:\n", name); 168edf04919SJeremy L Thompson } else { 169edf04919SJeremy L Thompson char padded_name[12]; 170edf04919SJeremy L Thompson 171edf04919SJeremy L Thompson snprintf(padded_name, 11, "%s:", name); 172edf04919SJeremy L Thompson fprintf(stream, " %-10s", padded_name); 173edf04919SJeremy L Thompson } 17492ae7e47SJeremy L Thompson for (CeedInt i = 0; i < m; i++) { 175edf04919SJeremy L Thompson if (m > 1) fprintf(stream, " [%" CeedInt_FMT "]", i); 1762b730f8bSJeremy L Thompson for (CeedInt j = 0; j < n; j++) fprintf(stream, fp_fmt, fabs(a[i * n + j]) > 1E-14 ? a[i * n + j] : 0); 1777a982d89SJeremy L. Thompson fputs("\n", stream); 1787a982d89SJeremy L. Thompson } 179e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1807a982d89SJeremy L. Thompson } 1817a982d89SJeremy L. Thompson 182a76a04e7SJeremy L Thompson /** 183ea61e9acSJeremy L Thompson @brief Create the interpolation and gradient matrices for projection from the nodes of `basis_from` to the nodes of `basis_to`. 184ba59ac12SSebastian Grimberg 18515ad3917SSebastian Grimberg The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization. 18615ad3917SSebastian 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. 18715ad3917SSebastian Grimberg 188ba59ac12SSebastian Grimberg Note: `basis_from` and `basis_to` must have compatible quadrature spaces. 189a76a04e7SJeremy L Thompson 190a76a04e7SJeremy L Thompson @param[in] basis_from CeedBasis to project from 191a76a04e7SJeremy L Thompson @param[in] basis_to CeedBasis to project to 192ea61e9acSJeremy L Thompson @param[out] interp_project Address of the variable where the newly created interpolation matrix will be stored. 193ea61e9acSJeremy L Thompson @param[out] grad_project Address of the variable where the newly created gradient matrix will be stored. 194a76a04e7SJeremy L Thompson 195a76a04e7SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 196a76a04e7SJeremy L Thompson 197a76a04e7SJeremy L Thompson @ref Developer 198a76a04e7SJeremy L Thompson **/ 1992b730f8bSJeremy L Thompson static int CeedBasisCreateProjectionMatrices(CeedBasis basis_from, CeedBasis basis_to, CeedScalar **interp_project, CeedScalar **grad_project) { 200a76a04e7SJeremy L Thompson Ceed ceed; 2011c66c397SJeremy L Thompson bool is_tensor_to, is_tensor_from; 2021c66c397SJeremy L Thompson CeedInt Q, Q_to, Q_from, P_to, P_from; 2031c66c397SJeremy L Thompson 2042b730f8bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis_to, &ceed)); 205a76a04e7SJeremy L Thompson 206a76a04e7SJeremy L Thompson // Check for compatible quadrature spaces 2072b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_to, &Q_to)); 2082b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_from, &Q_from)); 2096574a04fSJeremy L Thompson CeedCheck(Q_to == Q_from, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces"); 2101c66c397SJeremy L Thompson Q = Q_to; 211a76a04e7SJeremy L Thompson 21214556e63SJeremy L Thompson // Check for matching tensor or non-tensor 2132b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to)); 2142b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from)); 2156574a04fSJeremy L Thompson CeedCheck(is_tensor_to == is_tensor_from, ceed, CEED_ERROR_MINOR, "Bases must both be tensor or non-tensor"); 2166574a04fSJeremy L Thompson if (is_tensor_to) { 2172b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_to)); 2182b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_from)); 2192b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis_from, &Q)); 2206574a04fSJeremy L Thompson } else { 2212b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_to, &P_to)); 2222b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_from, &P_from)); 223a76a04e7SJeremy L Thompson } 224a76a04e7SJeremy L Thompson 22515ad3917SSebastian Grimberg // Check for matching FE space 22615ad3917SSebastian Grimberg CeedFESpace fe_space_to, fe_space_from; 22715ad3917SSebastian Grimberg CeedCall(CeedBasisGetFESpace(basis_to, &fe_space_to)); 22815ad3917SSebastian Grimberg CeedCall(CeedBasisGetFESpace(basis_from, &fe_space_from)); 2296574a04fSJeremy L Thompson CeedCheck(fe_space_to == fe_space_from, ceed, CEED_ERROR_MINOR, "Bases must both be the same FE space type"); 23015ad3917SSebastian Grimberg 23114556e63SJeremy L Thompson // Get source matrices 23215ad3917SSebastian Grimberg CeedInt dim, q_comp = 1; 23314556e63SJeremy L Thompson CeedScalar *interp_to, *interp_from, *tau; 2341c66c397SJeremy L Thompson const CeedScalar *interp_to_source = NULL, *interp_from_source = NULL, *grad_from_source = NULL; 2351c66c397SJeremy L Thompson 2362b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_to, &dim)); 237a76a04e7SJeremy L Thompson if (is_tensor_to) { 2382b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis_to, &interp_to_source)); 2392b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis_from, &interp_from_source)); 240a76a04e7SJeremy L Thompson } else { 24115ad3917SSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis_from, CEED_EVAL_INTERP, &q_comp)); 2422b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis_to, &interp_to_source)); 2432b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis_from, &interp_from_source)); 24415ad3917SSebastian Grimberg } 24515ad3917SSebastian Grimberg CeedCall(CeedMalloc(Q * P_from * q_comp, &interp_from)); 24615ad3917SSebastian Grimberg CeedCall(CeedMalloc(Q * P_to * q_comp, &interp_to)); 24715ad3917SSebastian Grimberg CeedCall(CeedCalloc(P_to * P_from, interp_project)); 24815ad3917SSebastian Grimberg CeedCall(CeedMalloc(Q * q_comp, &tau)); 24915ad3917SSebastian Grimberg 25015ad3917SSebastian Grimberg // `grad_project = interp_to^+ * grad_from` is computed for the H^1 space case so the 251de05fbb2SSebastian Grimberg // projection basis will have a gradient operation (allocated even if not H^1 for the 252de05fbb2SSebastian Grimberg // basis construction later on) 25315ad3917SSebastian Grimberg if (fe_space_to == CEED_FE_SPACE_H1) { 25415ad3917SSebastian Grimberg if (is_tensor_to) { 25515ad3917SSebastian Grimberg CeedCall(CeedBasisGetGrad1D(basis_from, &grad_from_source)); 25615ad3917SSebastian Grimberg } else { 2572b730f8bSJeremy L Thompson CeedCall(CeedBasisGetGrad(basis_from, &grad_from_source)); 258a76a04e7SJeremy L Thompson } 259de05fbb2SSebastian Grimberg } 26015ad3917SSebastian Grimberg CeedCall(CeedCalloc(P_to * P_from * (is_tensor_to ? 1 : dim), grad_project)); 26115ad3917SSebastian Grimberg 26215ad3917SSebastian Grimberg // QR Factorization, interp_to = Q R 26315ad3917SSebastian Grimberg memcpy(interp_to, interp_to_source, Q * P_to * q_comp * sizeof(interp_to_source[0])); 26415ad3917SSebastian Grimberg CeedCall(CeedQRFactorization(ceed, interp_to, tau, Q * q_comp, P_to)); 265a76a04e7SJeremy L Thompson 26614556e63SJeremy L Thompson // Build matrices 26715ad3917SSebastian Grimberg CeedInt num_matrices = 1 + (fe_space_to == CEED_FE_SPACE_H1) * (is_tensor_to ? 1 : dim); 26814556e63SJeremy L Thompson CeedScalar *input_from[num_matrices], *output_project[num_matrices]; 2691c66c397SJeremy L Thompson 27014556e63SJeremy L Thompson input_from[0] = (CeedScalar *)interp_from_source; 27114556e63SJeremy L Thompson output_project[0] = *interp_project; 27214556e63SJeremy L Thompson for (CeedInt m = 1; m < num_matrices; m++) { 27314556e63SJeremy L Thompson input_from[m] = (CeedScalar *)&grad_from_source[(m - 1) * Q * P_from]; 27402af4036SJeremy L Thompson output_project[m] = &((*grad_project)[(m - 1) * P_to * P_from]); 27514556e63SJeremy L Thompson } 27614556e63SJeremy L Thompson for (CeedInt m = 0; m < num_matrices; m++) { 27715ad3917SSebastian Grimberg // Apply Q^T, interp_from = Q^T interp_from 27815ad3917SSebastian Grimberg memcpy(interp_from, input_from[m], Q * P_from * q_comp * sizeof(input_from[m][0])); 27915ad3917SSebastian Grimberg CeedCall(CeedHouseholderApplyQ(interp_from, interp_to, tau, CEED_TRANSPOSE, Q * q_comp, P_from, P_to, P_from, 1)); 280a76a04e7SJeremy L Thompson 28115ad3917SSebastian Grimberg // Apply Rinv, output_project = Rinv interp_from 282a76a04e7SJeremy L Thompson for (CeedInt j = 0; j < P_from; j++) { // Column j 2832b730f8bSJeremy L Thompson output_project[m][j + P_from * (P_to - 1)] = interp_from[j + P_from * (P_to - 1)] / interp_to[P_to * P_to - 1]; 284a76a04e7SJeremy L Thompson for (CeedInt i = P_to - 2; i >= 0; i--) { // Row i 28514556e63SJeremy L Thompson output_project[m][j + P_from * i] = interp_from[j + P_from * i]; 286a76a04e7SJeremy L Thompson for (CeedInt k = i + 1; k < P_to; k++) { 2872b730f8bSJeremy L Thompson output_project[m][j + P_from * i] -= interp_to[k + P_to * i] * output_project[m][j + P_from * k]; 288a76a04e7SJeremy L Thompson } 28914556e63SJeremy L Thompson output_project[m][j + P_from * i] /= interp_to[i + P_to * i]; 290a76a04e7SJeremy L Thompson } 291a76a04e7SJeremy L Thompson } 29214556e63SJeremy L Thompson } 29314556e63SJeremy L Thompson 29414556e63SJeremy L Thompson // Cleanup 2952b730f8bSJeremy L Thompson CeedCall(CeedFree(&tau)); 2962b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_to)); 2972b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_from)); 298a76a04e7SJeremy L Thompson return CEED_ERROR_SUCCESS; 299a76a04e7SJeremy L Thompson } 300a76a04e7SJeremy L Thompson 3017a982d89SJeremy L. Thompson /// @} 3027a982d89SJeremy L. Thompson 3037a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 3047a982d89SJeremy L. Thompson /// Ceed Backend API 3057a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 3067a982d89SJeremy L. Thompson /// @addtogroup CeedBasisBackend 3077a982d89SJeremy L. Thompson /// @{ 3087a982d89SJeremy L. Thompson 3097a982d89SJeremy L. Thompson /** 3107a982d89SJeremy L. Thompson @brief Return collocated grad matrix 3117a982d89SJeremy L. Thompson 312ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 313ea61e9acSJeremy L Thompson @param[out] collo_grad_1d Row-major (Q_1d * Q_1d) matrix expressing derivatives of basis functions at quadrature points 3147a982d89SJeremy L. Thompson 3157a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3167a982d89SJeremy L. Thompson 3177a982d89SJeremy L. Thompson @ref Backend 3187a982d89SJeremy L. Thompson **/ 319d1d35e2fSjeremylt int CeedBasisGetCollocatedGrad(CeedBasis basis, CeedScalar *collo_grad_1d) { 3207a982d89SJeremy L. Thompson Ceed ceed; 3212b730f8bSJeremy L Thompson CeedInt P_1d = (basis)->P_1d, Q_1d = (basis)->Q_1d; 32278464608Sjeremylt CeedScalar *interp_1d, *grad_1d, *tau; 3237a982d89SJeremy L. Thompson 3242b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q_1d * P_1d, &interp_1d)); 3252b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q_1d * P_1d, &grad_1d)); 3262b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q_1d, &tau)); 327d1d35e2fSjeremylt memcpy(interp_1d, (basis)->interp_1d, Q_1d * P_1d * sizeof(basis)->interp_1d[0]); 328d1d35e2fSjeremylt memcpy(grad_1d, (basis)->grad_1d, Q_1d * P_1d * sizeof(basis)->interp_1d[0]); 3297a982d89SJeremy L. Thompson 330d1d35e2fSjeremylt // QR Factorization, interp_1d = Q R 3312b730f8bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 3322b730f8bSJeremy L Thompson CeedCall(CeedQRFactorization(ceed, interp_1d, tau, Q_1d, P_1d)); 333ea61e9acSJeremy L Thompson // Note: This function is for backend use, so all errors are terminal and we do not need to clean up memory on failure. 3347a982d89SJeremy L. Thompson 335c8c3fa7dSJeremy L Thompson // Apply R_inv, collo_grad_1d = grad_1d R_inv 336c8c3fa7dSJeremy L Thompson for (CeedInt i = 0; i < Q_1d; i++) { // Row i 337d1d35e2fSjeremylt collo_grad_1d[Q_1d * i] = grad_1d[P_1d * i] / interp_1d[0]; 338c8c3fa7dSJeremy L Thompson for (CeedInt j = 1; j < P_1d; j++) { // Column j 339d1d35e2fSjeremylt collo_grad_1d[j + Q_1d * i] = grad_1d[j + P_1d * i]; 340c8c3fa7dSJeremy L Thompson for (CeedInt k = 0; k < j; k++) collo_grad_1d[j + Q_1d * i] -= interp_1d[j + P_1d * k] * collo_grad_1d[k + Q_1d * i]; 341d1d35e2fSjeremylt collo_grad_1d[j + Q_1d * i] /= interp_1d[j + P_1d * j]; 3427a982d89SJeremy L. Thompson } 343c8c3fa7dSJeremy L Thompson for (CeedInt j = P_1d; j < Q_1d; j++) collo_grad_1d[j + Q_1d * i] = 0; 3447a982d89SJeremy L. Thompson } 3457a982d89SJeremy L. Thompson 34615ad3917SSebastian Grimberg // Apply Q^T, collo_grad_1d = collo_grad_1d Q^T 3472b730f8bSJeremy L Thompson CeedCall(CeedHouseholderApplyQ(collo_grad_1d, interp_1d, tau, CEED_NOTRANSPOSE, Q_1d, Q_1d, P_1d, 1, Q_1d)); 3487a982d89SJeremy L. Thompson 3492b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_1d)); 3502b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_1d)); 3512b730f8bSJeremy L Thompson CeedCall(CeedFree(&tau)); 352e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3537a982d89SJeremy L. Thompson } 3547a982d89SJeremy L. Thompson 3557a982d89SJeremy L. Thompson /** 3567a982d89SJeremy L. Thompson @brief Get tensor status for given CeedBasis 3577a982d89SJeremy L. Thompson 358ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 359d1d35e2fSjeremylt @param[out] is_tensor Variable to store tensor status 3607a982d89SJeremy L. Thompson 3617a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3627a982d89SJeremy L. Thompson 3637a982d89SJeremy L. Thompson @ref Backend 3647a982d89SJeremy L. Thompson **/ 365d1d35e2fSjeremylt int CeedBasisIsTensor(CeedBasis basis, bool *is_tensor) { 3666402da51SJeremy L Thompson *is_tensor = basis->is_tensor_basis; 367e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3687a982d89SJeremy L. Thompson } 3697a982d89SJeremy L. Thompson 3707a982d89SJeremy L. Thompson /** 3717a982d89SJeremy L. Thompson @brief Get backend data of a CeedBasis 3727a982d89SJeremy L. Thompson 373ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 3747a982d89SJeremy L. Thompson @param[out] data Variable to store data 3757a982d89SJeremy L. Thompson 3767a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3777a982d89SJeremy L. Thompson 3787a982d89SJeremy L. Thompson @ref Backend 3797a982d89SJeremy L. Thompson **/ 380777ff853SJeremy L Thompson int CeedBasisGetData(CeedBasis basis, void *data) { 381777ff853SJeremy L Thompson *(void **)data = basis->data; 382e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3837a982d89SJeremy L. Thompson } 3847a982d89SJeremy L. Thompson 3857a982d89SJeremy L. Thompson /** 3867a982d89SJeremy L. Thompson @brief Set backend data of a CeedBasis 3877a982d89SJeremy L. Thompson 388ea61e9acSJeremy L Thompson @param[in,out] basis CeedBasis 389ea61e9acSJeremy L Thompson @param[in] data Data to set 3907a982d89SJeremy L. Thompson 3917a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3927a982d89SJeremy L. Thompson 3937a982d89SJeremy L. Thompson @ref Backend 3947a982d89SJeremy L. Thompson **/ 395777ff853SJeremy L Thompson int CeedBasisSetData(CeedBasis basis, void *data) { 396777ff853SJeremy L Thompson basis->data = data; 397e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3987a982d89SJeremy L. Thompson } 3997a982d89SJeremy L. Thompson 4007a982d89SJeremy L. Thompson /** 40134359f16Sjeremylt @brief Increment the reference counter for a CeedBasis 40234359f16Sjeremylt 403ea61e9acSJeremy L Thompson @param[in,out] basis Basis to increment the reference counter 40434359f16Sjeremylt 40534359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 40634359f16Sjeremylt 40734359f16Sjeremylt @ref Backend 40834359f16Sjeremylt **/ 4099560d06aSjeremylt int CeedBasisReference(CeedBasis basis) { 41034359f16Sjeremylt basis->ref_count++; 41134359f16Sjeremylt return CEED_ERROR_SUCCESS; 41234359f16Sjeremylt } 41334359f16Sjeremylt 41434359f16Sjeremylt /** 415c4e3f59bSSebastian Grimberg @brief Get number of Q-vector components for given CeedBasis 416c4e3f59bSSebastian Grimberg 417c4e3f59bSSebastian Grimberg @param[in] basis CeedBasis 418c4e3f59bSSebastian Grimberg @param[in] eval_mode \ref CEED_EVAL_INTERP to use interpolated values, 419c4e3f59bSSebastian Grimberg \ref CEED_EVAL_GRAD to use gradients, 420c4e3f59bSSebastian Grimberg \ref CEED_EVAL_DIV to use divergence, 421c4e3f59bSSebastian Grimberg \ref CEED_EVAL_CURL to use curl. 422c4e3f59bSSebastian Grimberg @param[out] q_comp Variable to store number of Q-vector components of basis 423c4e3f59bSSebastian Grimberg 424c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 425c4e3f59bSSebastian Grimberg 426c4e3f59bSSebastian Grimberg @ref Backend 427c4e3f59bSSebastian Grimberg **/ 428c4e3f59bSSebastian Grimberg int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode, CeedInt *q_comp) { 429c4e3f59bSSebastian Grimberg switch (eval_mode) { 430c4e3f59bSSebastian Grimberg case CEED_EVAL_INTERP: 431c4e3f59bSSebastian Grimberg *q_comp = (basis->fe_space == CEED_FE_SPACE_H1) ? 1 : basis->dim; 432c4e3f59bSSebastian Grimberg break; 433c4e3f59bSSebastian Grimberg case CEED_EVAL_GRAD: 434c4e3f59bSSebastian Grimberg *q_comp = basis->dim; 435c4e3f59bSSebastian Grimberg break; 436c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 437c4e3f59bSSebastian Grimberg *q_comp = 1; 438c4e3f59bSSebastian Grimberg break; 439c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 440c4e3f59bSSebastian Grimberg *q_comp = (basis->dim < 3) ? 1 : basis->dim; 441c4e3f59bSSebastian Grimberg break; 442c4e3f59bSSebastian Grimberg case CEED_EVAL_NONE: 443c4e3f59bSSebastian Grimberg case CEED_EVAL_WEIGHT: 444352a5e7cSSebastian Grimberg *q_comp = 1; 445c4e3f59bSSebastian Grimberg break; 446c4e3f59bSSebastian Grimberg } 447c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 448c4e3f59bSSebastian Grimberg } 449c4e3f59bSSebastian Grimberg 450c4e3f59bSSebastian Grimberg /** 4516e15d496SJeremy L Thompson @brief Estimate number of FLOPs required to apply CeedBasis in t_mode and eval_mode 4526e15d496SJeremy L Thompson 453ea61e9acSJeremy L Thompson @param[in] basis Basis to estimate FLOPs for 454ea61e9acSJeremy L Thompson @param[in] t_mode Apply basis or transpose 455ea61e9acSJeremy L Thompson @param[in] eval_mode Basis evaluation mode 456ea61e9acSJeremy L Thompson @param[out] flops Address of variable to hold FLOPs estimate 4576e15d496SJeremy L Thompson 4586e15d496SJeremy L Thompson @ref Backend 4596e15d496SJeremy L Thompson **/ 4602b730f8bSJeremy L Thompson int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedSize *flops) { 4616e15d496SJeremy L Thompson bool is_tensor; 4626e15d496SJeremy L Thompson 4632b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor)); 4646e15d496SJeremy L Thompson if (is_tensor) { 4656e15d496SJeremy L Thompson CeedInt dim, num_comp, P_1d, Q_1d; 4661c66c397SJeremy L Thompson 4672b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 4682b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 4692b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 4702b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 4716e15d496SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 4722b730f8bSJeremy L Thompson P_1d = Q_1d; 4732b730f8bSJeremy L Thompson Q_1d = P_1d; 4746e15d496SJeremy L Thompson } 4756e15d496SJeremy L Thompson CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim - 1), post = 1; 4766e15d496SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 4776e15d496SJeremy L Thompson tensor_flops += 2 * pre * P_1d * post * Q_1d; 4786e15d496SJeremy L Thompson pre /= P_1d; 4796e15d496SJeremy L Thompson post *= Q_1d; 4806e15d496SJeremy L Thompson } 4816e15d496SJeremy L Thompson switch (eval_mode) { 4822b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 4832b730f8bSJeremy L Thompson *flops = 0; 4842b730f8bSJeremy L Thompson break; 4852b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 4862b730f8bSJeremy L Thompson *flops = tensor_flops; 4872b730f8bSJeremy L Thompson break; 4882b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 4892b730f8bSJeremy L Thompson *flops = tensor_flops * 2; 4902b730f8bSJeremy L Thompson break; 4916e15d496SJeremy L Thompson case CEED_EVAL_DIV: 4926e15d496SJeremy L Thompson case CEED_EVAL_CURL: 4936574a04fSJeremy L Thompson // LCOV_EXCL_START 4946574a04fSJeremy L Thompson return CeedError(basis->ceed, CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported", CeedEvalModes[eval_mode]); 4952b730f8bSJeremy L Thompson break; 4966e15d496SJeremy L Thompson // LCOV_EXCL_STOP 4972b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 4982b730f8bSJeremy L Thompson *flops = dim * CeedIntPow(Q_1d, dim); 4992b730f8bSJeremy L Thompson break; 5006e15d496SJeremy L Thompson } 5016e15d496SJeremy L Thompson } else { 502c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 5031c66c397SJeremy L Thompson 5042b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 5052b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 506c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 5072b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 5082b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 5096e15d496SJeremy L Thompson switch (eval_mode) { 5102b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 5112b730f8bSJeremy L Thompson *flops = 0; 5122b730f8bSJeremy L Thompson break; 5132b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 5142b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 5152b730f8bSJeremy L Thompson case CEED_EVAL_DIV: 5162b730f8bSJeremy L Thompson case CEED_EVAL_CURL: 517c4e3f59bSSebastian Grimberg *flops = num_nodes * num_qpts * num_comp * q_comp; 5182b730f8bSJeremy L Thompson break; 5192b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 5202b730f8bSJeremy L Thompson *flops = 0; 5212b730f8bSJeremy L Thompson break; 5226e15d496SJeremy L Thompson } 5236e15d496SJeremy L Thompson } 5246e15d496SJeremy L Thompson return CEED_ERROR_SUCCESS; 5256e15d496SJeremy L Thompson } 5266e15d496SJeremy L Thompson 5276e15d496SJeremy L Thompson /** 528c4e3f59bSSebastian Grimberg @brief Get CeedFESpace for a CeedBasis 529c4e3f59bSSebastian Grimberg 530c4e3f59bSSebastian Grimberg @param[in] basis CeedBasis 531c4e3f59bSSebastian Grimberg @param[out] fe_space Variable to store CeedFESpace 532c4e3f59bSSebastian Grimberg 533c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 534c4e3f59bSSebastian Grimberg 535c4e3f59bSSebastian Grimberg @ref Backend 536c4e3f59bSSebastian Grimberg **/ 537c4e3f59bSSebastian Grimberg int CeedBasisGetFESpace(CeedBasis basis, CeedFESpace *fe_space) { 538c4e3f59bSSebastian Grimberg *fe_space = basis->fe_space; 539c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 540c4e3f59bSSebastian Grimberg } 541c4e3f59bSSebastian Grimberg 542c4e3f59bSSebastian Grimberg /** 5437a982d89SJeremy L. Thompson @brief Get dimension for given CeedElemTopology 5447a982d89SJeremy L. Thompson 545ea61e9acSJeremy L Thompson @param[in] topo CeedElemTopology 5467a982d89SJeremy L. Thompson @param[out] dim Variable to store dimension of topology 5477a982d89SJeremy L. Thompson 5487a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5497a982d89SJeremy L. Thompson 5507a982d89SJeremy L. Thompson @ref Backend 5517a982d89SJeremy L. Thompson **/ 5527a982d89SJeremy L. Thompson int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim) { 5537a982d89SJeremy L. Thompson *dim = (CeedInt)topo >> 16; 554e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5557a982d89SJeremy L. Thompson } 5567a982d89SJeremy L. Thompson 5577a982d89SJeremy L. Thompson /** 5587a982d89SJeremy L. Thompson @brief Get CeedTensorContract of a CeedBasis 5597a982d89SJeremy L. Thompson 560ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 5617a982d89SJeremy L. Thompson @param[out] contract Variable to store CeedTensorContract 5627a982d89SJeremy L. Thompson 5637a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5647a982d89SJeremy L. Thompson 5657a982d89SJeremy L. Thompson @ref Backend 5667a982d89SJeremy L. Thompson **/ 5677a982d89SJeremy L. Thompson int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract) { 5687a982d89SJeremy L. Thompson *contract = basis->contract; 569e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5707a982d89SJeremy L. Thompson } 5717a982d89SJeremy L. Thompson 5727a982d89SJeremy L. Thompson /** 5737a982d89SJeremy L. Thompson @brief Set CeedTensorContract of a CeedBasis 5747a982d89SJeremy L. Thompson 575ea61e9acSJeremy L Thompson @param[in,out] basis CeedBasis 576ea61e9acSJeremy L Thompson @param[in] contract CeedTensorContract to set 5777a982d89SJeremy L. Thompson 5787a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5797a982d89SJeremy L. Thompson 5807a982d89SJeremy L. Thompson @ref Backend 5817a982d89SJeremy L. Thompson **/ 58234359f16Sjeremylt int CeedBasisSetTensorContract(CeedBasis basis, CeedTensorContract contract) { 58334359f16Sjeremylt basis->contract = contract; 5842b730f8bSJeremy L Thompson CeedCall(CeedTensorContractReference(contract)); 585e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5867a982d89SJeremy L. Thompson } 5877a982d89SJeremy L. Thompson 5887a982d89SJeremy L. Thompson /** 5897a982d89SJeremy L. Thompson @brief Return a reference implementation of matrix multiplication C = A B. 590ba59ac12SSebastian Grimberg 591ba59ac12SSebastian Grimberg Note: This is a reference implementation for CPU CeedScalar pointers that is not intended for high performance. 5927a982d89SJeremy L. Thompson 593ea61e9acSJeremy L Thompson @param[in] ceed Ceed context for error handling 594d1d35e2fSjeremylt @param[in] mat_A Row-major matrix A 595d1d35e2fSjeremylt @param[in] mat_B Row-major matrix B 596d1d35e2fSjeremylt @param[out] mat_C Row-major output matrix C 597ea61e9acSJeremy L Thompson @param[in] m Number of rows of C 598ea61e9acSJeremy L Thompson @param[in] n Number of columns of C 599ea61e9acSJeremy L Thompson @param[in] kk Number of columns of A/rows of B 6007a982d89SJeremy L. Thompson 6017a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 6027a982d89SJeremy L. Thompson 6037a982d89SJeremy L. Thompson @ref Utility 6047a982d89SJeremy L. Thompson **/ 6052b730f8bSJeremy L Thompson int CeedMatrixMatrixMultiply(Ceed ceed, const CeedScalar *mat_A, const CeedScalar *mat_B, CeedScalar *mat_C, CeedInt m, CeedInt n, CeedInt kk) { 6062b730f8bSJeremy L Thompson for (CeedInt i = 0; i < m; i++) { 6077a982d89SJeremy L. Thompson for (CeedInt j = 0; j < n; j++) { 6087a982d89SJeremy L. Thompson CeedScalar sum = 0; 6091c66c397SJeremy L Thompson 6102b730f8bSJeremy L Thompson for (CeedInt k = 0; k < kk; k++) sum += mat_A[k + i * kk] * mat_B[j + k * n]; 611d1d35e2fSjeremylt mat_C[j + i * n] = sum; 6127a982d89SJeremy L. Thompson } 6132b730f8bSJeremy L Thompson } 614e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6157a982d89SJeremy L. Thompson } 6167a982d89SJeremy L. Thompson 617ba59ac12SSebastian Grimberg /** 618ba59ac12SSebastian Grimberg @brief Return QR Factorization of a matrix 619ba59ac12SSebastian Grimberg 620ba59ac12SSebastian Grimberg @param[in] ceed Ceed context for error handling 621ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 622ba59ac12SSebastian Grimberg @param[in,out] tau Vector of length m of scaling factors 623ba59ac12SSebastian Grimberg @param[in] m Number of rows 624ba59ac12SSebastian Grimberg @param[in] n Number of columns 625ba59ac12SSebastian Grimberg 626ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 627ba59ac12SSebastian Grimberg 628ba59ac12SSebastian Grimberg @ref Utility 629ba59ac12SSebastian Grimberg **/ 630ba59ac12SSebastian Grimberg int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau, CeedInt m, CeedInt n) { 631ba59ac12SSebastian Grimberg CeedScalar v[m]; 632ba59ac12SSebastian Grimberg 633ba59ac12SSebastian Grimberg // Check matrix shape 6346574a04fSJeremy L Thompson CeedCheck(n <= m, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute QR factorization with n > m"); 635ba59ac12SSebastian Grimberg 636ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 6371c66c397SJeremy L Thompson CeedScalar sigma = 0.0; 6381c66c397SJeremy L Thompson 639ba59ac12SSebastian Grimberg if (i >= m - 1) { // last row of matrix, no reflection needed 640ba59ac12SSebastian Grimberg tau[i] = 0.; 641ba59ac12SSebastian Grimberg break; 642ba59ac12SSebastian Grimberg } 643ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 644ba59ac12SSebastian Grimberg v[i] = mat[i + n * i]; 645ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) { 646ba59ac12SSebastian Grimberg v[j] = mat[i + n * j]; 647ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 648ba59ac12SSebastian Grimberg } 6491c66c397SJeremy L Thompson const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:m] 6501c66c397SJeremy L Thompson const CeedScalar R_ii = -copysign(norm, v[i]); 6511c66c397SJeremy L Thompson 652ba59ac12SSebastian Grimberg v[i] -= R_ii; 653ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 654ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 655ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 656ba59ac12SSebastian Grimberg tau[i] = 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 657ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] /= v[i]; 658ba59ac12SSebastian Grimberg 659ba59ac12SSebastian Grimberg // Apply Householder reflector to lower right panel 660ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[i * n + i + 1], &v[i], tau[i], m - i, n - i - 1, n, 1); 661ba59ac12SSebastian Grimberg // Save v 662ba59ac12SSebastian Grimberg mat[i + n * i] = R_ii; 663ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) mat[i + n * j] = v[j]; 664ba59ac12SSebastian Grimberg } 665ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 666ba59ac12SSebastian Grimberg } 667ba59ac12SSebastian Grimberg 668ba59ac12SSebastian Grimberg /** 669ba59ac12SSebastian Grimberg @brief Apply Householder Q matrix 670ba59ac12SSebastian Grimberg 671ba59ac12SSebastian Grimberg Compute mat_A = mat_Q mat_A, where mat_Q is mxm and mat_A is mxn. 672ba59ac12SSebastian Grimberg 673ba59ac12SSebastian Grimberg @param[in,out] mat_A Matrix to apply Householder Q to, in place 674ba59ac12SSebastian Grimberg @param[in] mat_Q Householder Q matrix 675ba59ac12SSebastian Grimberg @param[in] tau Householder scaling factors 676ba59ac12SSebastian Grimberg @param[in] t_mode Transpose mode for application 677ba59ac12SSebastian Grimberg @param[in] m Number of rows in A 678ba59ac12SSebastian Grimberg @param[in] n Number of columns in A 679ba59ac12SSebastian Grimberg @param[in] k Number of elementary reflectors in Q, k<m 680ba59ac12SSebastian Grimberg @param[in] row Row stride in A 681ba59ac12SSebastian Grimberg @param[in] col Col stride in A 682ba59ac12SSebastian Grimberg 683ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 684ba59ac12SSebastian Grimberg 685c4e3f59bSSebastian Grimberg @ref Utility 686ba59ac12SSebastian Grimberg **/ 687ba59ac12SSebastian Grimberg int CeedHouseholderApplyQ(CeedScalar *mat_A, const CeedScalar *mat_Q, const CeedScalar *tau, CeedTransposeMode t_mode, CeedInt m, CeedInt n, 688ba59ac12SSebastian Grimberg CeedInt k, CeedInt row, CeedInt col) { 689ba59ac12SSebastian Grimberg CeedScalar *v; 6901c66c397SJeremy L Thompson 691ba59ac12SSebastian Grimberg CeedCall(CeedMalloc(m, &v)); 692ba59ac12SSebastian Grimberg for (CeedInt ii = 0; ii < k; ii++) { 693ba59ac12SSebastian Grimberg CeedInt i = t_mode == CEED_TRANSPOSE ? ii : k - 1 - ii; 694ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] = mat_Q[j * k + i]; 695ba59ac12SSebastian Grimberg // Apply Householder reflector (I - tau v v^T) collo_grad_1d^T 696ba59ac12SSebastian Grimberg CeedCall(CeedHouseholderReflect(&mat_A[i * row], &v[i], tau[i], m - i, n, row, col)); 697ba59ac12SSebastian Grimberg } 698ba59ac12SSebastian Grimberg CeedCall(CeedFree(&v)); 699ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 700ba59ac12SSebastian Grimberg } 701ba59ac12SSebastian Grimberg 702ba59ac12SSebastian Grimberg /** 703ba59ac12SSebastian Grimberg @brief Return symmetric Schur decomposition of the symmetric matrix mat via symmetric QR factorization 704ba59ac12SSebastian Grimberg 705ba59ac12SSebastian Grimberg @param[in] ceed Ceed context for error handling 706ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 707ba59ac12SSebastian Grimberg @param[out] lambda Vector of length n of eigenvalues 708ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 709ba59ac12SSebastian Grimberg 710ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 711ba59ac12SSebastian Grimberg 712ba59ac12SSebastian Grimberg @ref Utility 713ba59ac12SSebastian Grimberg **/ 7142c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 7152c2ea1dbSJeremy L Thompson int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat, CeedScalar *lambda, CeedInt n) { 716ba59ac12SSebastian Grimberg // Check bounds for clang-tidy 7176574a04fSJeremy L Thompson CeedCheck(n > 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute symmetric Schur decomposition of scalars"); 718ba59ac12SSebastian Grimberg 719ba59ac12SSebastian Grimberg CeedScalar v[n - 1], tau[n - 1], mat_T[n * n]; 720ba59ac12SSebastian Grimberg 721ba59ac12SSebastian Grimberg // Copy mat to mat_T and set mat to I 722ba59ac12SSebastian Grimberg memcpy(mat_T, mat, n * n * sizeof(mat[0])); 723ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 724ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) mat[j + n * i] = (i == j) ? 1 : 0; 725ba59ac12SSebastian Grimberg } 726ba59ac12SSebastian Grimberg 727ba59ac12SSebastian Grimberg // Reduce to tridiagonal 728ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - 1; i++) { 729ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 730ba59ac12SSebastian Grimberg CeedScalar sigma = 0.0; 7311c66c397SJeremy L Thompson 732ba59ac12SSebastian Grimberg v[i] = mat_T[i + n * (i + 1)]; 733ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 734ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 735ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 736ba59ac12SSebastian Grimberg } 7371c66c397SJeremy L Thompson const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:n-1] 7381c66c397SJeremy L Thompson const CeedScalar R_ii = -copysign(norm, v[i]); 7391c66c397SJeremy L Thompson 740ba59ac12SSebastian Grimberg v[i] -= R_ii; 741ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 742ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 743ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 744ba59ac12SSebastian Grimberg tau[i] = i == n - 2 ? 2 : 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 745ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) v[j] /= v[i]; 746ba59ac12SSebastian Grimberg 747ba59ac12SSebastian Grimberg // Update sub and super diagonal 748ba59ac12SSebastian Grimberg for (CeedInt j = i + 2; j < n; j++) { 749ba59ac12SSebastian Grimberg mat_T[i + n * j] = 0; 750ba59ac12SSebastian Grimberg mat_T[j + n * i] = 0; 751ba59ac12SSebastian Grimberg } 752ba59ac12SSebastian Grimberg // Apply symmetric Householder reflector to lower right panel 753ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 754ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), 1, n); 755ba59ac12SSebastian Grimberg 756ba59ac12SSebastian Grimberg // Save v 757ba59ac12SSebastian Grimberg mat_T[i + n * (i + 1)] = R_ii; 758ba59ac12SSebastian Grimberg mat_T[(i + 1) + n * i] = R_ii; 759ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 760ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = v[j]; 761ba59ac12SSebastian Grimberg } 762ba59ac12SSebastian Grimberg } 763ba59ac12SSebastian Grimberg // Backwards accumulation of Q 764ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 765ba59ac12SSebastian Grimberg if (tau[i] > 0.0) { 766ba59ac12SSebastian Grimberg v[i] = 1; 767ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 768ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 769ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = 0; 770ba59ac12SSebastian Grimberg } 771ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 772ba59ac12SSebastian Grimberg } 773ba59ac12SSebastian Grimberg } 774ba59ac12SSebastian Grimberg 775ba59ac12SSebastian Grimberg // Reduce sub and super diagonal 776ba59ac12SSebastian Grimberg CeedInt p = 0, q = 0, itr = 0, max_itr = n * n * n * n; 777ba59ac12SSebastian Grimberg CeedScalar tol = CEED_EPSILON; 778ba59ac12SSebastian Grimberg 779ba59ac12SSebastian Grimberg while (itr < max_itr) { 780ba59ac12SSebastian Grimberg // Update p, q, size of reduced portions of diagonal 781ba59ac12SSebastian Grimberg p = 0; 782ba59ac12SSebastian Grimberg q = 0; 783ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 784ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) q += 1; 785ba59ac12SSebastian Grimberg else break; 786ba59ac12SSebastian Grimberg } 787ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - q - 1; i++) { 788ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) p += 1; 789ba59ac12SSebastian Grimberg else break; 790ba59ac12SSebastian Grimberg } 791ba59ac12SSebastian Grimberg if (q == n - 1) break; // Finished reducing 792ba59ac12SSebastian Grimberg 793ba59ac12SSebastian Grimberg // Reduce tridiagonal portion 794ba59ac12SSebastian Grimberg CeedScalar t_nn = mat_T[(n - 1 - q) + n * (n - 1 - q)], t_nnm1 = mat_T[(n - 2 - q) + n * (n - 1 - q)]; 795ba59ac12SSebastian Grimberg CeedScalar d = (mat_T[(n - 2 - q) + n * (n - 2 - q)] - t_nn) / 2; 796ba59ac12SSebastian Grimberg CeedScalar mu = t_nn - t_nnm1 * t_nnm1 / (d + copysign(sqrt(d * d + t_nnm1 * t_nnm1), d)); 797ba59ac12SSebastian Grimberg CeedScalar x = mat_T[p + n * p] - mu; 798ba59ac12SSebastian Grimberg CeedScalar z = mat_T[p + n * (p + 1)]; 7991c66c397SJeremy L Thompson 800ba59ac12SSebastian Grimberg for (CeedInt k = p; k < n - q - 1; k++) { 801ba59ac12SSebastian Grimberg // Compute Givens rotation 802ba59ac12SSebastian Grimberg CeedScalar c = 1, s = 0; 8031c66c397SJeremy L Thompson 804ba59ac12SSebastian Grimberg if (fabs(z) > tol) { 805ba59ac12SSebastian Grimberg if (fabs(z) > fabs(x)) { 8061c66c397SJeremy L Thompson const CeedScalar tau = -x / z; 8071c66c397SJeremy L Thompson 8081c66c397SJeremy L Thompson s = 1 / sqrt(1 + tau * tau); 8091c66c397SJeremy L Thompson c = s * tau; 810ba59ac12SSebastian Grimberg } else { 8111c66c397SJeremy L Thompson const CeedScalar tau = -z / x; 8121c66c397SJeremy L Thompson 8131c66c397SJeremy L Thompson c = 1 / sqrt(1 + tau * tau); 8141c66c397SJeremy L Thompson s = c * tau; 815ba59ac12SSebastian Grimberg } 816ba59ac12SSebastian Grimberg } 817ba59ac12SSebastian Grimberg 818ba59ac12SSebastian Grimberg // Apply Givens rotation to T 819ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 820ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_TRANSPOSE, k, k + 1, n, n); 821ba59ac12SSebastian Grimberg 822ba59ac12SSebastian Grimberg // Apply Givens rotation to Q 823ba59ac12SSebastian Grimberg CeedGivensRotation(mat, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 824ba59ac12SSebastian Grimberg 825ba59ac12SSebastian Grimberg // Update x, z 826ba59ac12SSebastian Grimberg if (k < n - q - 2) { 827ba59ac12SSebastian Grimberg x = mat_T[k + n * (k + 1)]; 828ba59ac12SSebastian Grimberg z = mat_T[k + n * (k + 2)]; 829ba59ac12SSebastian Grimberg } 830ba59ac12SSebastian Grimberg } 831ba59ac12SSebastian Grimberg itr++; 832ba59ac12SSebastian Grimberg } 833ba59ac12SSebastian Grimberg 834ba59ac12SSebastian Grimberg // Save eigenvalues 835ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) lambda[i] = mat_T[i + n * i]; 836ba59ac12SSebastian Grimberg 837ba59ac12SSebastian Grimberg // Check convergence 8386574a04fSJeremy L Thompson CeedCheck(itr < max_itr || q > n, ceed, CEED_ERROR_MINOR, "Symmetric QR failed to converge"); 839ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 840ba59ac12SSebastian Grimberg } 8412c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 842ba59ac12SSebastian Grimberg 843ba59ac12SSebastian Grimberg /** 844ba59ac12SSebastian Grimberg @brief Return Simultaneous Diagonalization of two matrices. 845ba59ac12SSebastian Grimberg 846ba59ac12SSebastian Grimberg This solves the generalized eigenvalue problem A x = lambda B x, where A and B are symmetric and B is positive definite. 847ba59ac12SSebastian Grimberg We generate the matrix X and vector Lambda such that X^T A X = Lambda and X^T B X = I. 848ba59ac12SSebastian Grimberg This is equivalent to the LAPACK routine 'sygv' with TYPE = 1. 849ba59ac12SSebastian Grimberg 850ba59ac12SSebastian Grimberg @param[in] ceed Ceed context for error handling 851ba59ac12SSebastian Grimberg @param[in] mat_A Row-major matrix to be factorized with eigenvalues 852ba59ac12SSebastian Grimberg @param[in] mat_B Row-major matrix to be factorized to identity 853ba59ac12SSebastian Grimberg @param[out] mat_X Row-major orthogonal matrix 854ba59ac12SSebastian Grimberg @param[out] lambda Vector of length n of generalized eigenvalues 855ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 856ba59ac12SSebastian Grimberg 857ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 858ba59ac12SSebastian Grimberg 859ba59ac12SSebastian Grimberg @ref Utility 860ba59ac12SSebastian Grimberg **/ 8612c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 8622c2ea1dbSJeremy L Thompson int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A, CeedScalar *mat_B, CeedScalar *mat_X, CeedScalar *lambda, CeedInt n) { 863ba59ac12SSebastian Grimberg CeedScalar *mat_C, *mat_G, *vec_D; 8641c66c397SJeremy L Thompson 865ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_C)); 866ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_G)); 867ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n, &vec_D)); 868ba59ac12SSebastian Grimberg 869ba59ac12SSebastian Grimberg // Compute B = G D G^T 870ba59ac12SSebastian Grimberg memcpy(mat_G, mat_B, n * n * sizeof(mat_B[0])); 871ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_G, vec_D, n)); 872ba59ac12SSebastian Grimberg 873ba59ac12SSebastian Grimberg // Sort eigenvalues 874ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 875ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 876ba59ac12SSebastian Grimberg if (fabs(vec_D[j]) > fabs(vec_D[j + 1])) { 8771c66c397SJeremy L Thompson CeedScalarSwap(vec_D[j], vec_D[j + 1]); 8781c66c397SJeremy L Thompson for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_G[k * n + j], mat_G[k * n + j + 1]); 879ba59ac12SSebastian Grimberg } 880ba59ac12SSebastian Grimberg } 881ba59ac12SSebastian Grimberg } 882ba59ac12SSebastian Grimberg 883ba59ac12SSebastian Grimberg // Compute C = (G D^1/2)^-1 A (G D^1/2)^-T 884ba59ac12SSebastian Grimberg // = D^-1/2 G^T A G D^-1/2 885ba59ac12SSebastian Grimberg // -- D = D^-1/2 886ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) vec_D[i] = 1. / sqrt(vec_D[i]); 887ba59ac12SSebastian Grimberg // -- G = G D^-1/2 888ba59ac12SSebastian Grimberg // -- C = D^-1/2 G^T 889ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 890ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) { 891ba59ac12SSebastian Grimberg mat_G[i * n + j] *= vec_D[j]; 892ba59ac12SSebastian Grimberg mat_C[j * n + i] = mat_G[i * n + j]; 893ba59ac12SSebastian Grimberg } 894ba59ac12SSebastian Grimberg } 895ba59ac12SSebastian Grimberg // -- X = (D^-1/2 G^T) A 896ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_C, (const CeedScalar *)mat_A, mat_X, n, n, n)); 897ba59ac12SSebastian Grimberg // -- C = (D^-1/2 G^T A) (G D^-1/2) 898ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_X, (const CeedScalar *)mat_G, mat_C, n, n, n)); 899ba59ac12SSebastian Grimberg 900ba59ac12SSebastian Grimberg // Compute Q^T C Q = lambda 901ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_C, lambda, n)); 902ba59ac12SSebastian Grimberg 903ba59ac12SSebastian Grimberg // Sort eigenvalues 904ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 905ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 906ba59ac12SSebastian Grimberg if (fabs(lambda[j]) > fabs(lambda[j + 1])) { 9071c66c397SJeremy L Thompson CeedScalarSwap(lambda[j], lambda[j + 1]); 9081c66c397SJeremy L Thompson for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_C[k * n + j], mat_C[k * n + j + 1]); 909ba59ac12SSebastian Grimberg } 910ba59ac12SSebastian Grimberg } 911ba59ac12SSebastian Grimberg } 912ba59ac12SSebastian Grimberg 913ba59ac12SSebastian Grimberg // Set X = (G D^1/2)^-T Q 914ba59ac12SSebastian Grimberg // = G D^-1/2 Q 915ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_G, (const CeedScalar *)mat_C, mat_X, n, n, n)); 916ba59ac12SSebastian Grimberg 917ba59ac12SSebastian Grimberg // Cleanup 918ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_C)); 919ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_G)); 920ba59ac12SSebastian Grimberg CeedCall(CeedFree(&vec_D)); 921ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 922ba59ac12SSebastian Grimberg } 9232c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 924ba59ac12SSebastian Grimberg 9257a982d89SJeremy L. Thompson /// @} 9267a982d89SJeremy L. Thompson 9277a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 9287a982d89SJeremy L. Thompson /// CeedBasis Public API 9297a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 9307a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser 931d7b241e6Sjeremylt /// @{ 932d7b241e6Sjeremylt 933b11c1e72Sjeremylt /** 934ba59ac12SSebastian Grimberg @brief Create a tensor-product basis for H^1 discretizations 935b11c1e72Sjeremylt 936ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedBasis will be created 937ea61e9acSJeremy L Thompson @param[in] dim Topological dimension 938ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 939ea61e9acSJeremy L Thompson @param[in] P_1d Number of nodes in one dimension 940ea61e9acSJeremy L Thompson @param[in] Q_1d Number of quadrature points in one dimension 941ea61e9acSJeremy L Thompson @param[in] interp_1d Row-major (Q_1d * P_1d) matrix expressing the values of nodal basis functions at quadrature points 942ea61e9acSJeremy L Thompson @param[in] grad_1d Row-major (Q_1d * P_1d) matrix expressing derivatives of nodal basis functions at quadrature points 943ea61e9acSJeremy 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] 944ea61e9acSJeremy L Thompson @param[in] q_weight_1d Array of length Q_1d holding the quadrature weights on the reference element 945ea61e9acSJeremy L Thompson @param[out] basis Address of the variable where the newly created CeedBasis will be stored. 946b11c1e72Sjeremylt 947b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 948dfdf5a53Sjeremylt 9497a982d89SJeremy L. Thompson @ref User 950b11c1e72Sjeremylt **/ 9512b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, 9522b730f8bSJeremy L Thompson const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis *basis) { 9535fe0d4faSjeremylt if (!ceed->BasisCreateTensorH1) { 9545fe0d4faSjeremylt Ceed delegate; 9556574a04fSJeremy L Thompson 9562b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 9576574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisCreateTensorH1"); 9582b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 959e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9605fe0d4faSjeremylt } 961e15f9bd0SJeremy L Thompson 9626574a04fSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "Basis dimension must be a positive value"); 9636574a04fSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component"); 9646574a04fSJeremy L Thompson CeedCheck(P_1d > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node"); 9656574a04fSJeremy L Thompson CeedCheck(Q_1d > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point"); 966227444bfSJeremy L Thompson 9672b730f8bSJeremy L Thompson CeedElemTopology topo = dim == 1 ? CEED_TOPOLOGY_LINE : dim == 2 ? CEED_TOPOLOGY_QUAD : CEED_TOPOLOGY_HEX; 968e15f9bd0SJeremy L Thompson 9692b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 970db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 971d1d35e2fSjeremylt (*basis)->ref_count = 1; 9726402da51SJeremy L Thompson (*basis)->is_tensor_basis = true; 973d7b241e6Sjeremylt (*basis)->dim = dim; 974d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 975d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 976d1d35e2fSjeremylt (*basis)->P_1d = P_1d; 977d1d35e2fSjeremylt (*basis)->Q_1d = Q_1d; 978d1d35e2fSjeremylt (*basis)->P = CeedIntPow(P_1d, dim); 979d1d35e2fSjeremylt (*basis)->Q = CeedIntPow(Q_1d, dim); 980c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 9812b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_ref_1d)); 9822b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_weight_1d)); 983ff3a0f91SJeremy L Thompson if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d * sizeof(q_ref_1d[0])); 9842b730f8bSJeremy L Thompson if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, Q_1d * sizeof(q_weight_1d[0])); 9852b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->interp_1d)); 9862b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->grad_1d)); 9872b730f8bSJeremy L Thompson if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, Q_1d * P_1d * sizeof(interp_1d[0])); 988ff3a0f91SJeremy L Thompson if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d * P_1d * sizeof(grad_1d[0])); 9892b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, *basis)); 990e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 991d7b241e6Sjeremylt } 992d7b241e6Sjeremylt 993b11c1e72Sjeremylt /** 99495bb1877Svaleriabarra @brief Create a tensor-product Lagrange basis 995b11c1e72Sjeremylt 996ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedBasis will be created 997ea61e9acSJeremy L Thompson @param[in] dim Topological dimension of element 998ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 999ea61e9acSJeremy L Thompson @param[in] P Number of Gauss-Lobatto nodes in one dimension. 1000ea61e9acSJeremy L Thompson The polynomial degree of the resulting Q_k element is k=P-1. 1001ea61e9acSJeremy L Thompson @param[in] Q Number of quadrature points in one dimension. 1002ea61e9acSJeremy L Thompson @param[in] quad_mode Distribution of the Q quadrature points (affects order of accuracy for the quadrature) 1003ea61e9acSJeremy L Thompson @param[out] basis Address of the variable where the newly created CeedBasis will be stored. 1004b11c1e72Sjeremylt 1005b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1006dfdf5a53Sjeremylt 10077a982d89SJeremy L. Thompson @ref User 1008b11c1e72Sjeremylt **/ 10092b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis) { 1010d7b241e6Sjeremylt // Allocate 1011c8c3fa7dSJeremy L Thompson int ierr = CEED_ERROR_SUCCESS; 10122b730f8bSJeremy L Thompson CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, *q_weight_1d; 10134d537eeaSYohann 10146574a04fSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "Basis dimension must be a positive value"); 10156574a04fSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component"); 10166574a04fSJeremy L Thompson CeedCheck(P > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node"); 10176574a04fSJeremy L Thompson CeedCheck(Q > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point"); 1018227444bfSJeremy L Thompson 1019e15f9bd0SJeremy L Thompson // Get Nodes and Weights 10202b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &interp_1d)); 10212b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &grad_1d)); 10222b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P, &nodes)); 10232b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_ref_1d)); 10242b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_weight_1d)); 10252b730f8bSJeremy L Thompson if (CeedLobattoQuadrature(P, nodes, NULL) != CEED_ERROR_SUCCESS) goto cleanup; 1026d1d35e2fSjeremylt switch (quad_mode) { 1027d7b241e6Sjeremylt case CEED_GAUSS: 1028d1d35e2fSjeremylt ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d); 1029d7b241e6Sjeremylt break; 1030d7b241e6Sjeremylt case CEED_GAUSS_LOBATTO: 1031d1d35e2fSjeremylt ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d); 1032d7b241e6Sjeremylt break; 1033d7b241e6Sjeremylt } 10342b730f8bSJeremy L Thompson if (ierr != CEED_ERROR_SUCCESS) goto cleanup; 1035e15f9bd0SJeremy L Thompson 1036d7b241e6Sjeremylt // Build B, D matrix 1037d7b241e6Sjeremylt // Fornberg, 1998 1038c8c3fa7dSJeremy L Thompson for (CeedInt i = 0; i < Q; i++) { 1039d7b241e6Sjeremylt c1 = 1.0; 1040d1d35e2fSjeremylt c3 = nodes[0] - q_ref_1d[i]; 1041d1d35e2fSjeremylt interp_1d[i * P + 0] = 1.0; 1042c8c3fa7dSJeremy L Thompson for (CeedInt j = 1; j < P; j++) { 1043d7b241e6Sjeremylt c2 = 1.0; 1044d7b241e6Sjeremylt c4 = c3; 1045d1d35e2fSjeremylt c3 = nodes[j] - q_ref_1d[i]; 1046c8c3fa7dSJeremy L Thompson for (CeedInt k = 0; k < j; k++) { 1047d7b241e6Sjeremylt dx = nodes[j] - nodes[k]; 1048d7b241e6Sjeremylt c2 *= dx; 1049d7b241e6Sjeremylt if (k == j - 1) { 1050d1d35e2fSjeremylt grad_1d[i * P + j] = c1 * (interp_1d[i * P + k] - c4 * grad_1d[i * P + k]) / c2; 1051d1d35e2fSjeremylt interp_1d[i * P + j] = -c1 * c4 * interp_1d[i * P + k] / c2; 1052d7b241e6Sjeremylt } 1053d1d35e2fSjeremylt grad_1d[i * P + k] = (c3 * grad_1d[i * P + k] - interp_1d[i * P + k]) / dx; 1054d1d35e2fSjeremylt interp_1d[i * P + k] = c3 * interp_1d[i * P + k] / dx; 1055d7b241e6Sjeremylt } 1056d7b241e6Sjeremylt c1 = c2; 1057d7b241e6Sjeremylt } 1058d7b241e6Sjeremylt } 10599ac7b42eSJeremy L Thompson // Pass to CeedBasisCreateTensorH1 10602b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 1061e15f9bd0SJeremy L Thompson cleanup: 10622b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_1d)); 10632b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_1d)); 10642b730f8bSJeremy L Thompson CeedCall(CeedFree(&nodes)); 10652b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref_1d)); 10662b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight_1d)); 1067e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1068d7b241e6Sjeremylt } 1069d7b241e6Sjeremylt 1070b11c1e72Sjeremylt /** 1071ba59ac12SSebastian Grimberg @brief Create a non tensor-product basis for H^1 discretizations 1072a8de75f0Sjeremylt 1073ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedBasis will be created 1074ea61e9acSJeremy L Thompson @param[in] topo Topology of element, e.g. hypercube, simplex, ect 1075ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1076ea61e9acSJeremy L Thompson @param[in] num_nodes Total number of nodes 1077ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1078ea61e9acSJeremy L Thompson @param[in] interp Row-major (num_qpts * num_nodes) matrix expressing the values of nodal basis functions at quadrature points 1079c4e3f59bSSebastian Grimberg @param[in] grad Row-major (dim * num_qpts * num_nodes) matrix expressing derivatives of nodal basis functions at quadrature points 10809fe083eeSJeremy L Thompson @param[in] q_ref Array of length num_qpts * dim holding the locations of quadrature points on the reference element 1081ea61e9acSJeremy L Thompson @param[in] q_weight Array of length num_qpts holding the quadrature weights on the reference element 1082ea61e9acSJeremy L Thompson @param[out] basis Address of the variable where the newly created CeedBasis will be stored. 1083a8de75f0Sjeremylt 1084a8de75f0Sjeremylt @return An error code: 0 - success, otherwise - failure 1085a8de75f0Sjeremylt 10867a982d89SJeremy L. Thompson @ref User 1087a8de75f0Sjeremylt **/ 10882b730f8bSJeremy L Thompson int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 10892b730f8bSJeremy L Thompson const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1090d1d35e2fSjeremylt CeedInt P = num_nodes, Q = num_qpts, dim = 0; 1091a8de75f0Sjeremylt 10925fe0d4faSjeremylt if (!ceed->BasisCreateH1) { 10935fe0d4faSjeremylt Ceed delegate; 10946574a04fSJeremy L Thompson 10952b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 10966574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisCreateH1"); 10972b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis)); 1098e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10995fe0d4faSjeremylt } 11005fe0d4faSjeremylt 11016574a04fSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component"); 11026574a04fSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node"); 11036574a04fSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point"); 1104227444bfSJeremy L Thompson 11052b730f8bSJeremy L Thompson CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1106a8de75f0Sjeremylt 1107db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1108db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1109d1d35e2fSjeremylt (*basis)->ref_count = 1; 11106402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1111a8de75f0Sjeremylt (*basis)->dim = dim; 1112d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 1113d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 1114a8de75f0Sjeremylt (*basis)->P = P; 1115a8de75f0Sjeremylt (*basis)->Q = Q; 1116c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 11172b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * dim, &(*basis)->q_ref_1d)); 11182b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &(*basis)->q_weight_1d)); 1119ff3a0f91SJeremy L Thompson if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1120ff3a0f91SJeremy L Thompson if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 11212b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * P, &(*basis)->interp)); 11222b730f8bSJeremy L Thompson CeedCall(CeedCalloc(dim * Q * P, &(*basis)->grad)); 1123ff3a0f91SJeremy L Thompson if (interp) memcpy((*basis)->interp, interp, Q * P * sizeof(interp[0])); 1124ff3a0f91SJeremy L Thompson if (grad) memcpy((*basis)->grad, grad, dim * Q * P * sizeof(grad[0])); 11252b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, *basis)); 1126e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1127a8de75f0Sjeremylt } 1128a8de75f0Sjeremylt 1129a8de75f0Sjeremylt /** 1130859c15bbSJames Wright @brief Create a non tensor-product basis for \f$H(\mathrm{div})\f$ discretizations 113150c301a5SRezgar Shakeri 1132ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedBasis will be created 1133ea61e9acSJeremy L Thompson @param[in] topo Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below 1134ea61e9acSJeremy L Thompson @param[in] num_comp Number of components (usually 1 for vectors in H(div) bases) 1135ea61e9acSJeremy L Thompson @param[in] num_nodes Total number of nodes (dofs per element) 1136ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1137c4e3f59bSSebastian Grimberg @param[in] interp Row-major (dim * num_qpts * num_nodes) matrix expressing the values of basis functions at quadrature points 1138c4e3f59bSSebastian Grimberg @param[in] div Row-major (num_qpts * num_nodes) matrix expressing divergence of basis functions at quadrature points 11399fe083eeSJeremy L Thompson @param[in] q_ref Array of length num_qpts * dim holding the locations of quadrature points on the reference element 1140ea61e9acSJeremy L Thompson @param[in] q_weight Array of length num_qpts holding the quadrature weights on the reference element 1141ea61e9acSJeremy L Thompson @param[out] basis Address of the variable where the newly created CeedBasis will be stored. 114250c301a5SRezgar Shakeri 114350c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 114450c301a5SRezgar Shakeri 114550c301a5SRezgar Shakeri @ref User 114650c301a5SRezgar Shakeri **/ 11472b730f8bSJeremy L Thompson int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 11482b730f8bSJeremy L Thompson const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 114950c301a5SRezgar Shakeri CeedInt Q = num_qpts, P = num_nodes, dim = 0; 1150c4e3f59bSSebastian Grimberg 115150c301a5SRezgar Shakeri if (!ceed->BasisCreateHdiv) { 115250c301a5SRezgar Shakeri Ceed delegate; 11536574a04fSJeremy L Thompson 11542b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 11556574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv"); 11562b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis)); 115750c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 115850c301a5SRezgar Shakeri } 115950c301a5SRezgar Shakeri 11606574a04fSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component"); 11616574a04fSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node"); 11626574a04fSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point"); 1163227444bfSJeremy L Thompson 1164c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1165c4e3f59bSSebastian Grimberg 1166db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1167db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 116850c301a5SRezgar Shakeri (*basis)->ref_count = 1; 11696402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 117050c301a5SRezgar Shakeri (*basis)->dim = dim; 117150c301a5SRezgar Shakeri (*basis)->topo = topo; 117250c301a5SRezgar Shakeri (*basis)->num_comp = num_comp; 117350c301a5SRezgar Shakeri (*basis)->P = P; 117450c301a5SRezgar Shakeri (*basis)->Q = Q; 1175c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HDIV; 11762b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 11772b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 117850c301a5SRezgar Shakeri if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 117950c301a5SRezgar Shakeri if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 11802b730f8bSJeremy L Thompson CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 11812b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * P, &(*basis)->div)); 118250c301a5SRezgar Shakeri if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 118350c301a5SRezgar Shakeri if (div) memcpy((*basis)->div, div, Q * P * sizeof(div[0])); 11842b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, q_weight, *basis)); 118550c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 118650c301a5SRezgar Shakeri } 118750c301a5SRezgar Shakeri 118850c301a5SRezgar Shakeri /** 11894385fb7fSSebastian Grimberg @brief Create a non tensor-product basis for \f$H(\mathrm{curl})\f$ discretizations 1190c4e3f59bSSebastian Grimberg 1191c4e3f59bSSebastian Grimberg @param[in] ceed Ceed object where the CeedBasis will be created 1192c4e3f59bSSebastian Grimberg @param[in] topo Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below 1193c4e3f59bSSebastian Grimberg @param[in] num_comp Number of components (usually 1 for vectors in H(curl) bases) 1194c4e3f59bSSebastian Grimberg @param[in] num_nodes Total number of nodes (dofs per element) 1195c4e3f59bSSebastian Grimberg @param[in] num_qpts Total number of quadrature points 1196c4e3f59bSSebastian Grimberg @param[in] interp Row-major (dim * num_qpts * num_nodes) matrix expressing the values of basis functions at quadrature points 1197c4e3f59bSSebastian 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 1198c4e3f59bSSebastian Grimberg quadrature points 1199c4e3f59bSSebastian Grimberg @param[in] q_ref Array of length num_qpts * dim holding the locations of quadrature points on the reference element 1200c4e3f59bSSebastian Grimberg @param[in] q_weight Array of length num_qpts holding the quadrature weights on the reference element 1201c4e3f59bSSebastian Grimberg @param[out] basis Address of the variable where the newly created CeedBasis will be stored. 1202c4e3f59bSSebastian Grimberg 1203c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1204c4e3f59bSSebastian Grimberg 1205c4e3f59bSSebastian Grimberg @ref User 1206c4e3f59bSSebastian Grimberg **/ 1207c4e3f59bSSebastian Grimberg int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 1208c4e3f59bSSebastian Grimberg const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1209c4e3f59bSSebastian Grimberg CeedInt Q = num_qpts, P = num_nodes, dim = 0, curl_comp = 0; 1210c4e3f59bSSebastian Grimberg 1211*d075f50bSSebastian Grimberg if (!ceed->BasisCreateHcurl) { 1212c4e3f59bSSebastian Grimberg Ceed delegate; 12136574a04fSJeremy L Thompson 1214c4e3f59bSSebastian Grimberg CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 12156574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl"); 1216c4e3f59bSSebastian Grimberg CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis)); 1217c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1218c4e3f59bSSebastian Grimberg } 1219c4e3f59bSSebastian Grimberg 12206574a04fSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component"); 12216574a04fSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node"); 12226574a04fSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point"); 1223c4e3f59bSSebastian Grimberg 1224c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1225c4e3f59bSSebastian Grimberg curl_comp = (dim < 3) ? 1 : dim; 1226c4e3f59bSSebastian Grimberg 1227db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1228db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1229c4e3f59bSSebastian Grimberg (*basis)->ref_count = 1; 12306402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1231c4e3f59bSSebastian Grimberg (*basis)->dim = dim; 1232c4e3f59bSSebastian Grimberg (*basis)->topo = topo; 1233c4e3f59bSSebastian Grimberg (*basis)->num_comp = num_comp; 1234c4e3f59bSSebastian Grimberg (*basis)->P = P; 1235c4e3f59bSSebastian Grimberg (*basis)->Q = Q; 1236c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HCURL; 1237c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 1238c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 1239c4e3f59bSSebastian Grimberg if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1240c4e3f59bSSebastian Grimberg if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 1241c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 1242c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(curl_comp * Q * P, &(*basis)->curl)); 1243c4e3f59bSSebastian Grimberg if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 1244c4e3f59bSSebastian Grimberg if (curl) memcpy((*basis)->curl, curl, curl_comp * Q * P * sizeof(curl[0])); 1245c4e3f59bSSebastian Grimberg CeedCall(ceed->BasisCreateHcurl(topo, dim, P, Q, interp, curl, q_ref, q_weight, *basis)); 1246c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1247c4e3f59bSSebastian Grimberg } 1248c4e3f59bSSebastian Grimberg 1249c4e3f59bSSebastian Grimberg /** 1250ea61e9acSJeremy L Thompson @brief Create a CeedBasis for projection from the nodes of `basis_from` to the nodes of `basis_to`. 1251ba59ac12SSebastian Grimberg 12529fd66db6SSebastian Grimberg Only `CEED_EVAL_INTERP` will be valid for the new basis, `basis_project`. 12539fd66db6SSebastian Grimberg For H^1 spaces, `CEED_EVAL_GRAD` will also be valid. 1254de05fbb2SSebastian Grimberg The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR 12559fd66db6SSebastian Grimberg factorization. 12569fd66db6SSebastian Grimberg The gradient (for the H^1 case) is given by `grad_project = interp_to^+ * grad_from`. 125715ad3917SSebastian Grimberg 125815ad3917SSebastian Grimberg Note: `basis_from` and `basis_to` must have compatible quadrature spaces. 125915ad3917SSebastian Grimberg 12609fd66db6SSebastian Grimberg Note: `basis_project` will have the same number of components as `basis_from`, regardless of the number of components that `basis_to` has. 12619fd66db6SSebastian Grimberg If `basis_from` has 3 components and `basis_to` has 5 components, then `basis_project` will have 3 components. 1262f113e5dcSJeremy L Thompson 1263f113e5dcSJeremy L Thompson @param[in] basis_from CeedBasis to prolong from 1264446e7af4SJeremy L Thompson @param[in] basis_to CeedBasis to prolong to 1265ea61e9acSJeremy L Thompson @param[out] basis_project Address of the variable where the newly created CeedBasis will be stored. 1266f113e5dcSJeremy L Thompson 1267f113e5dcSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1268f113e5dcSJeremy L Thompson 1269f113e5dcSJeremy L Thompson @ref User 1270f113e5dcSJeremy L Thompson **/ 12712b730f8bSJeremy L Thompson int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project) { 1272f113e5dcSJeremy L Thompson Ceed ceed; 12731c66c397SJeremy L Thompson bool is_tensor; 12741c66c397SJeremy L Thompson CeedInt dim, num_comp; 12751c66c397SJeremy L Thompson CeedScalar *q_ref, *q_weight, *interp_project, *grad_project; 12761c66c397SJeremy L Thompson 12772b730f8bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis_to, &ceed)); 1278f113e5dcSJeremy L Thompson 1279ecc88aebSJeremy L Thompson // Create projection matrix 12802b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateProjectionMatrices(basis_from, basis_to, &interp_project, &grad_project)); 1281f113e5dcSJeremy L Thompson 1282f113e5dcSJeremy L Thompson // Build basis 12832b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis_to, &is_tensor)); 12842b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_to, &dim)); 12852b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_from, &num_comp)); 1286f113e5dcSJeremy L Thompson if (is_tensor) { 1287f113e5dcSJeremy L Thompson CeedInt P_1d_to, P_1d_from; 12881c66c397SJeremy L Thompson 12892b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_1d_from)); 12902b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_1d_to)); 12912b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_to, &q_ref)); 12922b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_to, &q_weight)); 12932b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, interp_project, grad_project, q_ref, q_weight, basis_project)); 1294f113e5dcSJeremy L Thompson } else { 1295de05fbb2SSebastian Grimberg // Even if basis_to and basis_from are not H1, the resulting basis is H1 for interpolation to work 1296f113e5dcSJeremy L Thompson CeedInt num_nodes_to, num_nodes_from; 12971c66c397SJeremy L Thompson CeedElemTopology topo; 12981c66c397SJeremy L Thompson 12991c66c397SJeremy L Thompson CeedCall(CeedBasisGetTopology(basis_to, &topo)); 13002b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_from, &num_nodes_from)); 13012b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_to, &num_nodes_to)); 13022b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_to * dim, &q_ref)); 13032b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_to, &q_weight)); 13042b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, q_ref, q_weight, basis_project)); 1305f113e5dcSJeremy L Thompson } 1306f113e5dcSJeremy L Thompson 1307f113e5dcSJeremy L Thompson // Cleanup 13082b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_project)); 13092b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_project)); 13102b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref)); 13112b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight)); 1312f113e5dcSJeremy L Thompson return CEED_ERROR_SUCCESS; 1313f113e5dcSJeremy L Thompson } 1314f113e5dcSJeremy L Thompson 1315f113e5dcSJeremy L Thompson /** 1316ea61e9acSJeremy L Thompson @brief Copy the pointer to a CeedBasis. 13179560d06aSjeremylt 1318512bb800SJeremy 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. 1319512bb800SJeremy L Thompson This CeedBasis will be destroyed if `basis_copy` is the only reference to this CeedBasis. 1320ea61e9acSJeremy L Thompson 1321ea61e9acSJeremy L Thompson @param[in] basis CeedBasis to copy reference to 1322ea61e9acSJeremy L Thompson @param[in,out] basis_copy Variable to store copied reference 13239560d06aSjeremylt 13249560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 13259560d06aSjeremylt 13269560d06aSjeremylt @ref User 13279560d06aSjeremylt **/ 13289560d06aSjeremylt int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) { 1329356036faSJeremy L Thompson if (basis != CEED_BASIS_NONE) CeedCall(CeedBasisReference(basis)); 13302b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(basis_copy)); 13319560d06aSjeremylt *basis_copy = basis; 13329560d06aSjeremylt return CEED_ERROR_SUCCESS; 13339560d06aSjeremylt } 13349560d06aSjeremylt 13359560d06aSjeremylt /** 13367a982d89SJeremy L. Thompson @brief View a CeedBasis 13377a982d89SJeremy L. Thompson 1338ea61e9acSJeremy L Thompson @param[in] basis CeedBasis to view 1339ea61e9acSJeremy L Thompson @param[in] stream Stream to view to, e.g., stdout 13407a982d89SJeremy L. Thompson 13417a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 13427a982d89SJeremy L. Thompson 13437a982d89SJeremy L. Thompson @ref User 13447a982d89SJeremy L. Thompson **/ 13457a982d89SJeremy L. Thompson int CeedBasisView(CeedBasis basis, FILE *stream) { 13461c66c397SJeremy L Thompson CeedInt q_comp = 0; 134750c301a5SRezgar Shakeri CeedElemTopology topo = basis->topo; 1348c4e3f59bSSebastian Grimberg CeedFESpace fe_space = basis->fe_space; 13492b730f8bSJeremy L Thompson 135050c301a5SRezgar Shakeri // Print FE space and element topology of the basis 1351edf04919SJeremy L Thompson fprintf(stream, "CeedBasis in a %s on a %s element\n", CeedFESpaces[fe_space], CeedElemTopologies[topo]); 13526402da51SJeremy L Thompson if (basis->is_tensor_basis) { 1353edf04919SJeremy L Thompson fprintf(stream, " P: %" CeedInt_FMT "\n Q: %" CeedInt_FMT "\n", basis->P_1d, basis->Q_1d); 135450c301a5SRezgar Shakeri } else { 1355edf04919SJeremy L Thompson fprintf(stream, " P: %" CeedInt_FMT "\n Q: %" CeedInt_FMT "\n", basis->P, basis->Q); 135650c301a5SRezgar Shakeri } 1357edf04919SJeremy L Thompson fprintf(stream, " dimension: %" CeedInt_FMT "\n field components: %" CeedInt_FMT "\n", basis->dim, basis->num_comp); 1358ea61e9acSJeremy L Thompson // Print quadrature data, interpolation/gradient/divergence/curl of the basis 13596402da51SJeremy L Thompson if (basis->is_tensor_basis) { // tensor basis 13602b730f8bSJeremy L Thompson CeedCall(CeedScalarView("qref1d", "\t% 12.8f", 1, basis->Q_1d, basis->q_ref_1d, stream)); 13612b730f8bSJeremy L Thompson CeedCall(CeedScalarView("qweight1d", "\t% 12.8f", 1, basis->Q_1d, basis->q_weight_1d, stream)); 13622b730f8bSJeremy L Thompson CeedCall(CeedScalarView("interp1d", "\t% 12.8f", basis->Q_1d, basis->P_1d, basis->interp_1d, stream)); 13632b730f8bSJeremy L Thompson CeedCall(CeedScalarView("grad1d", "\t% 12.8f", basis->Q_1d, basis->P_1d, basis->grad_1d, stream)); 136450c301a5SRezgar Shakeri } else { // non-tensor basis 13652b730f8bSJeremy L Thompson CeedCall(CeedScalarView("qref", "\t% 12.8f", 1, basis->Q * basis->dim, basis->q_ref_1d, stream)); 13662b730f8bSJeremy L Thompson CeedCall(CeedScalarView("qweight", "\t% 12.8f", 1, basis->Q, basis->q_weight_1d, stream)); 1367c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp)); 1368c4e3f59bSSebastian Grimberg CeedCall(CeedScalarView("interp", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->interp, stream)); 136950c301a5SRezgar Shakeri if (basis->grad) { 1370c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp)); 1371c4e3f59bSSebastian Grimberg CeedCall(CeedScalarView("grad", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->grad, stream)); 13727a982d89SJeremy L. Thompson } 137350c301a5SRezgar Shakeri if (basis->div) { 1374c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp)); 1375c4e3f59bSSebastian Grimberg CeedCall(CeedScalarView("div", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->div, stream)); 1376c4e3f59bSSebastian Grimberg } 1377c4e3f59bSSebastian Grimberg if (basis->curl) { 1378c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp)); 1379c4e3f59bSSebastian Grimberg CeedCall(CeedScalarView("curl", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->curl, stream)); 138050c301a5SRezgar Shakeri } 138150c301a5SRezgar Shakeri } 1382e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 13837a982d89SJeremy L. Thompson } 13847a982d89SJeremy L. Thompson 13857a982d89SJeremy L. Thompson /** 13867a982d89SJeremy L. Thompson @brief Apply basis evaluation from nodes to quadrature points or vice versa 13877a982d89SJeremy L. Thompson 1388ea61e9acSJeremy L Thompson @param[in] basis CeedBasis to evaluate 1389ea61e9acSJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1390ea61e9acSJeremy L Thompson the backend will specify the ordering in CeedElemRestrictionCreateBlocked() 1391ea61e9acSJeremy L Thompson @param[in] t_mode \ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points; 1392ea61e9acSJeremy L Thompson \ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes 1393ea61e9acSJeremy L Thompson @param[in] eval_mode \ref CEED_EVAL_NONE to use values directly, 13947a982d89SJeremy L. Thompson \ref CEED_EVAL_INTERP to use interpolated values, 13957a982d89SJeremy L. Thompson \ref CEED_EVAL_GRAD to use gradients, 1396c4e3f59bSSebastian Grimberg \ref CEED_EVAL_DIV to use divergence, 1397c4e3f59bSSebastian Grimberg \ref CEED_EVAL_CURL to use curl, 13987a982d89SJeremy L. Thompson \ref CEED_EVAL_WEIGHT to use quadrature weights. 13997a982d89SJeremy L. Thompson @param[in] u Input CeedVector 14007a982d89SJeremy L. Thompson @param[out] v Output CeedVector 14017a982d89SJeremy L. Thompson 14027a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 14037a982d89SJeremy L. Thompson 14047a982d89SJeremy L. Thompson @ref User 14057a982d89SJeremy L. Thompson **/ 14062b730f8bSJeremy L Thompson int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 1407c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 14081c66c397SJeremy L Thompson CeedSize u_length = 0, v_length; 14091c66c397SJeremy L Thompson 14102b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 14112b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 1412c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 14132b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 14142b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 14152b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(v, &v_length)); 1416c8c3fa7dSJeremy L Thompson if (u) CeedCall(CeedVectorGetLength(u, &u_length)); 14177a982d89SJeremy L. Thompson 14186574a04fSJeremy L Thompson CeedCheck(basis->Apply, basis->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisApply"); 1419e15f9bd0SJeremy L Thompson 1420e15f9bd0SJeremy L Thompson // Check compatibility of topological and geometrical dimensions 14216574a04fSJeremy L Thompson CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0 && u_length % num_qpts == 0) || 14226574a04fSJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0 && v_length % num_qpts == 0), 14236574a04fSJeremy L Thompson basis->ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions"); 14247a982d89SJeremy L. Thompson 1425e15f9bd0SJeremy L Thompson // Check vector lengths to prevent out of bounds issues 14266574a04fSJeremy L Thompson bool good_dims = true; 1427d1d35e2fSjeremylt switch (eval_mode) { 1428e15f9bd0SJeremy L Thompson case CEED_EVAL_NONE: 14292b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 14302b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 1431c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 1432c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 14336574a04fSJeremy L Thompson good_dims = 14346574a04fSJeremy L Thompson ((t_mode == CEED_TRANSPOSE && u_length >= num_elem * num_comp * num_qpts * q_comp && v_length >= num_elem * num_comp * num_nodes) || 14356574a04fSJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && v_length >= num_elem * num_qpts * num_comp * q_comp && u_length >= num_elem * num_comp * num_nodes)); 1436e15f9bd0SJeremy L Thompson break; 1437e15f9bd0SJeremy L Thompson case CEED_EVAL_WEIGHT: 14386574a04fSJeremy L Thompson good_dims = v_length >= num_elem * num_qpts; 1439e15f9bd0SJeremy L Thompson break; 1440e15f9bd0SJeremy L Thompson } 14416574a04fSJeremy L Thompson CeedCheck(good_dims, basis->ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 1442e15f9bd0SJeremy L Thompson 14432b730f8bSJeremy L Thompson CeedCall(basis->Apply(basis, num_elem, t_mode, eval_mode, u, v)); 1444e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14457a982d89SJeremy L. Thompson } 14467a982d89SJeremy L. Thompson 14477a982d89SJeremy L. Thompson /** 1448c8c3fa7dSJeremy L Thompson @brief Apply basis evaluation from nodes to arbitrary points 1449c8c3fa7dSJeremy L Thompson 1450c8c3fa7dSJeremy L Thompson @param[in] basis CeedBasis to evaluate 1451c8c3fa7dSJeremy L Thompson @param[in] num_points The number of points to apply the basis evaluation to 1452c8c3fa7dSJeremy L Thompson @param[in] t_mode \ref CEED_NOTRANSPOSE to evaluate from nodes to points; 1453c8c3fa7dSJeremy L Thompson \ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes 1454c8c3fa7dSJeremy L Thompson @param[in] eval_mode \ref CEED_EVAL_INTERP to use interpolated values, 1455c8c3fa7dSJeremy L Thompson \ref CEED_EVAL_GRAD to use gradients 1456c8c3fa7dSJeremy L Thompson @param[in] x_ref CeedVector holding reference coordinates of each point 1457c8c3fa7dSJeremy L Thompson @param[in] u Input CeedVector, of length `num_nodes * num_comp` for `CEED_NOTRANSPOSE` 1458c8c3fa7dSJeremy L Thompson @param[out] v Output CeedVector, of length `num_points * num_q_comp` for `CEED_NOTRANSPOSE` with `CEED_EVAL_INTERP` 1459c8c3fa7dSJeremy L Thompson 1460c8c3fa7dSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1461c8c3fa7dSJeremy L Thompson 1462c8c3fa7dSJeremy L Thompson @ref User 1463c8c3fa7dSJeremy L Thompson **/ 1464c8c3fa7dSJeremy L Thompson int CeedBasisApplyAtPoints(CeedBasis basis, CeedInt num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, 1465c8c3fa7dSJeremy L Thompson CeedVector v) { 1466c8c3fa7dSJeremy L Thompson CeedInt dim, num_comp, num_q_comp, num_nodes, P_1d = 1, Q_1d = 1; 14671c66c397SJeremy L Thompson CeedSize x_length = 0, u_length = 0, v_length; 1468c8c3fa7dSJeremy L Thompson 1469c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 1470c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 1471c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 1472c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 1473c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &num_q_comp)); 1474c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 1475c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetLength(x_ref, &x_length)); 1476c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetLength(v, &v_length)); 1477c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetLength(u, &u_length)); 1478c8c3fa7dSJeremy L Thompson 1479c8c3fa7dSJeremy L Thompson // Check compatibility of topological and geometrical dimensions 1480c8c3fa7dSJeremy L Thompson CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0) || (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0), basis->ceed, 1481c8c3fa7dSJeremy L Thompson CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions and number of points"); 1482c8c3fa7dSJeremy L Thompson 1483c8c3fa7dSJeremy L Thompson // Check compatibility coordinates vector 1484c8c3fa7dSJeremy L Thompson CeedCheck(x_length >= num_points * dim, basis->ceed, CEED_ERROR_DIMENSION, 1485c8c3fa7dSJeremy L Thompson "Length of reference coordinate vector incompatible with basis dimension and number of points"); 1486c8c3fa7dSJeremy L Thompson 1487c8c3fa7dSJeremy L Thompson // Check vector lengths to prevent out of bounds issues 1488c8c3fa7dSJeremy L Thompson bool good_dims = false; 1489c8c3fa7dSJeremy L Thompson switch (eval_mode) { 1490c8c3fa7dSJeremy L Thompson case CEED_EVAL_INTERP: 1491c8c3fa7dSJeremy L Thompson good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= num_points * num_q_comp || v_length >= num_nodes * num_comp)) || 1492c8c3fa7dSJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && (v_length >= num_points * num_q_comp || u_length >= num_nodes * num_comp))); 1493c8c3fa7dSJeremy L Thompson break; 1494c8c3fa7dSJeremy L Thompson case CEED_EVAL_GRAD: 1495edfbf3a6SJeremy L Thompson good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= num_points * num_q_comp * dim || v_length >= num_nodes * num_comp)) || 1496edfbf3a6SJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && (v_length >= num_points * num_q_comp * dim || u_length >= num_nodes * num_comp))); 1497edfbf3a6SJeremy L Thompson break; 1498c8c3fa7dSJeremy L Thompson case CEED_EVAL_NONE: 1499c8c3fa7dSJeremy L Thompson case CEED_EVAL_WEIGHT: 1500c8c3fa7dSJeremy L Thompson case CEED_EVAL_DIV: 1501c8c3fa7dSJeremy L Thompson case CEED_EVAL_CURL: 1502c8c3fa7dSJeremy L Thompson // LCOV_EXCL_START 1503c8c3fa7dSJeremy L Thompson return CeedError(basis->ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points not supported for %s", CeedEvalModes[eval_mode]); 1504c8c3fa7dSJeremy L Thompson // LCOV_EXCL_STOP 1505c8c3fa7dSJeremy L Thompson } 1506c8c3fa7dSJeremy L Thompson CeedCheck(good_dims, basis->ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 1507c8c3fa7dSJeremy L Thompson 1508c8c3fa7dSJeremy L Thompson // Backend method 1509c8c3fa7dSJeremy L Thompson if (basis->ApplyAtPoints) { 1510c8c3fa7dSJeremy L Thompson CeedCall(basis->ApplyAtPoints(basis, num_points, t_mode, eval_mode, x_ref, u, v)); 1511c8c3fa7dSJeremy L Thompson return CEED_ERROR_SUCCESS; 1512c8c3fa7dSJeremy L Thompson } 1513c8c3fa7dSJeremy L Thompson 1514c8c3fa7dSJeremy L Thompson // Default implementation 1515c8c3fa7dSJeremy L Thompson CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points only supported for tensor product bases"); 1516c8c3fa7dSJeremy L Thompson if (!basis->basis_chebyshev) { 1517c8c3fa7dSJeremy L Thompson // Build matrix mapping from quadrature point values to Chebyshev coefficients 1518c8c3fa7dSJeremy L Thompson CeedScalar *tau, *C, *I, *chebyshev_coeffs_1d; 1519c8c3fa7dSJeremy L Thompson const CeedScalar *q_ref_1d; 1520c8c3fa7dSJeremy L Thompson 1521c8c3fa7dSJeremy L Thompson // Build coefficient matrix 1522c8c3fa7dSJeremy L Thompson // -- Note: Clang-tidy needs this check because it does not understand the is_tensor_basis check above 1523c8c3fa7dSJeremy L Thompson CeedCheck(P_1d > 0 && Q_1d > 0, basis->ceed, CEED_ERROR_INCOMPATIBLE, "Basis dimensions are malformed"); 1524c8c3fa7dSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * Q_1d, &C)); 1525c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 15263778dbaaSJeremy L Thompson for (CeedInt i = 0; i < Q_1d; i++) CeedCall(CeedChebyshevPolynomialsAtPoint(q_ref_1d[i], Q_1d, &C[i * Q_1d])); 1527c8c3fa7dSJeremy L Thompson 1528c8c3fa7dSJeremy L Thompson // Inverse of coefficient matrix 1529c8c3fa7dSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * Q_1d, &chebyshev_coeffs_1d)); 1530c8c3fa7dSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * Q_1d, &I)); 1531c8c3fa7dSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &tau)); 1532c8c3fa7dSJeremy L Thompson // -- QR Factorization, C = Q R 1533c8c3fa7dSJeremy L Thompson CeedCall(CeedQRFactorization(basis->ceed, C, tau, Q_1d, Q_1d)); 1534c8c3fa7dSJeremy L Thompson // -- chebyshev_coeffs_1d = R_inv Q^T 1535c8c3fa7dSJeremy L Thompson for (CeedInt i = 0; i < Q_1d; i++) I[i * Q_1d + i] = 1.0; 1536c8c3fa7dSJeremy L Thompson // ---- Apply R_inv, chebyshev_coeffs_1d = I R_inv 1537c8c3fa7dSJeremy L Thompson for (CeedInt i = 0; i < Q_1d; i++) { // Row i 1538c8c3fa7dSJeremy L Thompson chebyshev_coeffs_1d[Q_1d * i] = I[Q_1d * i] / C[0]; 1539c8c3fa7dSJeremy L Thompson for (CeedInt j = 1; j < Q_1d; j++) { // Column j 1540c8c3fa7dSJeremy L Thompson chebyshev_coeffs_1d[j + Q_1d * i] = I[j + Q_1d * i]; 1541c8c3fa7dSJeremy L Thompson for (CeedInt k = 0; k < j; k++) chebyshev_coeffs_1d[j + Q_1d * i] -= C[j + Q_1d * k] * chebyshev_coeffs_1d[k + Q_1d * i]; 1542c8c3fa7dSJeremy L Thompson chebyshev_coeffs_1d[j + Q_1d * i] /= C[j + Q_1d * j]; 1543c8c3fa7dSJeremy L Thompson } 1544c8c3fa7dSJeremy L Thompson } 1545c8c3fa7dSJeremy L Thompson // ---- Apply Q^T, chebyshev_coeffs_1d = R_inv Q^T 1546c8c3fa7dSJeremy L Thompson CeedCall(CeedHouseholderApplyQ(chebyshev_coeffs_1d, C, tau, CEED_NOTRANSPOSE, Q_1d, Q_1d, Q_1d, 1, Q_1d)); 1547c8c3fa7dSJeremy L Thompson 1548c8c3fa7dSJeremy L Thompson // Build basis mapping from nodes to Chebyshev coefficients 1549c8c3fa7dSJeremy L Thompson CeedScalar *chebyshev_interp_1d, *chebyshev_grad_1d, *chebyshev_q_weight_1d; 1550c8c3fa7dSJeremy L Thompson const CeedScalar *interp_1d; 1551c8c3fa7dSJeremy L Thompson 155271a83b88SJeremy L Thompson CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 155371a83b88SJeremy L Thompson CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_grad_1d)); 1554c8c3fa7dSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &chebyshev_q_weight_1d)); 1555c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 1556c8c3fa7dSJeremy L Thompson CeedCall(CeedMatrixMatrixMultiply(basis->ceed, chebyshev_coeffs_1d, interp_1d, chebyshev_interp_1d, Q_1d, P_1d, Q_1d)); 1557c8c3fa7dSJeremy L Thompson 1558c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorCreate(basis->ceed, num_comp * CeedIntPow(Q_1d, dim), &basis->vec_chebyshev)); 155971a83b88SJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(basis->ceed, dim, num_comp, P_1d, Q_1d, chebyshev_interp_1d, chebyshev_grad_1d, q_ref_1d, chebyshev_q_weight_1d, 1560c8c3fa7dSJeremy L Thompson &basis->basis_chebyshev)); 1561c8c3fa7dSJeremy L Thompson 1562c8c3fa7dSJeremy L Thompson // Cleanup 1563c8c3fa7dSJeremy L Thompson CeedCall(CeedFree(&C)); 1564c8c3fa7dSJeremy L Thompson CeedCall(CeedFree(&chebyshev_coeffs_1d)); 1565c8c3fa7dSJeremy L Thompson CeedCall(CeedFree(&I)); 1566c8c3fa7dSJeremy L Thompson CeedCall(CeedFree(&tau)); 1567c8c3fa7dSJeremy L Thompson CeedCall(CeedFree(&chebyshev_interp_1d)); 1568c8c3fa7dSJeremy L Thompson CeedCall(CeedFree(&chebyshev_grad_1d)); 1569c8c3fa7dSJeremy L Thompson CeedCall(CeedFree(&chebyshev_q_weight_1d)); 1570c8c3fa7dSJeremy L Thompson } 1571c8c3fa7dSJeremy L Thompson 1572c8c3fa7dSJeremy L Thompson // Create TensorContract object if needed, such as a basis from the GPU backends 1573c8c3fa7dSJeremy L Thompson if (!basis->contract) { 1574c8c3fa7dSJeremy L Thompson Ceed ceed_ref; 1575585a562dSJeremy L Thompson CeedBasis basis_ref = NULL; 1576c8c3fa7dSJeremy L Thompson 1577c8c3fa7dSJeremy L Thompson CeedCall(CeedInit("/cpu/self", &ceed_ref)); 1578c8c3fa7dSJeremy L Thompson // Only need matching tensor contraction dimensions, any type of basis will work 157971a83b88SJeremy L Thompson CeedCall(CeedBasisCreateTensorH1Lagrange(ceed_ref, dim, num_comp, P_1d, Q_1d, CEED_GAUSS, &basis_ref)); 1580585a562dSJeremy L Thompson // Note - clang-tidy doesn't know basis_ref->contract must be valid here 1581585a562dSJeremy L Thompson CeedCheck(basis_ref && basis_ref->contract, basis->ceed, CEED_ERROR_UNSUPPORTED, 15821c66c397SJeremy L Thompson "Reference CPU ceed failed to create a tensor contraction object"); 1583585a562dSJeremy L Thompson CeedCall(CeedTensorContractReferenceCopy(basis_ref->contract, &basis->contract)); 1584c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisDestroy(&basis_ref)); 1585c8c3fa7dSJeremy L Thompson CeedCall(CeedDestroy(&ceed_ref)); 1586c8c3fa7dSJeremy L Thompson } 1587c8c3fa7dSJeremy L Thompson 1588c8c3fa7dSJeremy L Thompson // Basis evaluation 1589c8c3fa7dSJeremy L Thompson switch (t_mode) { 1590c8c3fa7dSJeremy L Thompson case CEED_NOTRANSPOSE: { 1591c8c3fa7dSJeremy L Thompson // Nodes to arbitrary points 1592c8c3fa7dSJeremy L Thompson CeedScalar *v_array; 1593c8c3fa7dSJeremy L Thompson const CeedScalar *chebyshev_coeffs, *x_array_read; 1594c8c3fa7dSJeremy L Thompson 1595c8c3fa7dSJeremy L Thompson // -- Interpolate to Chebyshev coefficients 1596c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, basis->vec_chebyshev)); 1597c8c3fa7dSJeremy L Thompson 1598c8c3fa7dSJeremy L Thompson // -- Evaluate Chebyshev polynomials at arbitrary points 1599c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs)); 1600c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read)); 1601c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(v, CEED_MEM_HOST, &v_array)); 1602edfbf3a6SJeremy L Thompson switch (eval_mode) { 1603edfbf3a6SJeremy L Thompson case CEED_EVAL_INTERP: { 1604c8c3fa7dSJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 1605c8c3fa7dSJeremy L Thompson 1606c8c3fa7dSJeremy L Thompson // ---- Values at point 1607c8c3fa7dSJeremy L Thompson for (CeedInt p = 0; p < num_points; p++) { 1608c8c3fa7dSJeremy L Thompson CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1; 1609c8c3fa7dSJeremy L Thompson 161053ef2869SZach Atkins for (CeedInt d = 0; d < dim; d++) { 16113778dbaaSJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 16123778dbaaSJeremy L Thompson CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[p * dim + d], Q_1d, chebyshev_x)); 1613c8c3fa7dSJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false, 161453ef2869SZach Atkins d == 0 ? chebyshev_coeffs : tmp[d % 2], d == (dim - 1) ? &v_array[p * num_comp] : tmp[(d + 1) % 2])); 1615c8c3fa7dSJeremy L Thompson pre /= Q_1d; 1616c8c3fa7dSJeremy L Thompson post *= 1; 1617c8c3fa7dSJeremy L Thompson } 1618c8c3fa7dSJeremy L Thompson } 1619edfbf3a6SJeremy L Thompson break; 1620edfbf3a6SJeremy L Thompson } 1621edfbf3a6SJeremy L Thompson case CEED_EVAL_GRAD: { 1622edfbf3a6SJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 1623edfbf3a6SJeremy L Thompson 1624edfbf3a6SJeremy L Thompson // ---- Values at point 1625edfbf3a6SJeremy L Thompson for (CeedInt p = 0; p < num_points; p++) { 1626edfbf3a6SJeremy L Thompson // Dim**2 contractions, apply grad when pass == dim 162753ef2869SZach Atkins for (CeedInt pass = 0; pass < dim; pass++) { 1628edfbf3a6SJeremy L Thompson CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1; 1629edfbf3a6SJeremy L Thompson 163053ef2869SZach Atkins for (CeedInt d = 0; d < dim; d++) { 16313778dbaaSJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 16323778dbaaSJeremy L Thompson if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[p * dim + d], Q_1d, chebyshev_x)); 16333778dbaaSJeremy L Thompson else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[p * dim + d], Q_1d, chebyshev_x)); 1634edfbf3a6SJeremy L Thompson CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false, 163553ef2869SZach Atkins d == 0 ? chebyshev_coeffs : tmp[d % 2], 163653ef2869SZach Atkins d == (dim - 1) ? &v_array[p * num_comp * dim + pass] : tmp[(d + 1) % 2])); 1637edfbf3a6SJeremy L Thompson pre /= Q_1d; 1638edfbf3a6SJeremy L Thompson post *= 1; 1639edfbf3a6SJeremy L Thompson } 1640edfbf3a6SJeremy L Thompson } 1641edfbf3a6SJeremy L Thompson } 1642edfbf3a6SJeremy L Thompson break; 1643edfbf3a6SJeremy L Thompson } 1644edfbf3a6SJeremy L Thompson default: 1645edfbf3a6SJeremy L Thompson // Nothing to do, this won't occur 1646edfbf3a6SJeremy L Thompson break; 1647c8c3fa7dSJeremy L Thompson } 1648c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(basis->vec_chebyshev, &chebyshev_coeffs)); 1649c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read)); 1650c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorRestoreArray(v, &v_array)); 1651c8c3fa7dSJeremy L Thompson break; 1652c8c3fa7dSJeremy L Thompson } 16532a94f45fSJeremy L Thompson case CEED_TRANSPOSE: { 16543778dbaaSJeremy L Thompson // Note: No switch on e_mode here because only CEED_EVAL_INTERP is supported at this time 16552a94f45fSJeremy L Thompson // Arbitrary points to nodes 16562a94f45fSJeremy L Thompson CeedScalar *chebyshev_coeffs; 16572a94f45fSJeremy L Thompson const CeedScalar *u_array, *x_array_read; 16582a94f45fSJeremy L Thompson 16591c66c397SJeremy L Thompson // -- Transpose of evaluation of Chebyshev polynomials at arbitrary points 16602a94f45fSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs)); 16612a94f45fSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read)); 16622a94f45fSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array)); 1663038a8942SZach Atkins 1664038a8942SZach Atkins switch (eval_mode) { 1665038a8942SZach Atkins case CEED_EVAL_INTERP: { 16662a94f45fSJeremy L Thompson CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 16672a94f45fSJeremy L Thompson 16682a94f45fSJeremy L Thompson // ---- Values at point 16692a94f45fSJeremy L Thompson for (CeedInt p = 0; p < num_points; p++) { 16702a94f45fSJeremy L Thompson CeedInt pre = num_comp * 1, post = 1; 16712a94f45fSJeremy L Thompson 167253ef2869SZach Atkins for (CeedInt d = 0; d < dim; d++) { 16733778dbaaSJeremy L Thompson // ------ Tensor contract with current Chebyshev polynomial values 16743778dbaaSJeremy L Thompson CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[p * dim + d], Q_1d, chebyshev_x)); 167553ef2869SZach Atkins CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, p > 0 && d == (dim - 1), 167653ef2869SZach Atkins d == 0 ? &u_array[p * num_comp] : tmp[d % 2], d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2])); 16772a94f45fSJeremy L Thompson pre /= 1; 16782a94f45fSJeremy L Thompson post *= Q_1d; 16792a94f45fSJeremy L Thompson } 16802a94f45fSJeremy L Thompson } 1681038a8942SZach Atkins break; 1682038a8942SZach Atkins } 1683038a8942SZach Atkins case CEED_EVAL_GRAD: { 1684038a8942SZach Atkins CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 1685038a8942SZach Atkins 1686038a8942SZach Atkins // ---- Values at point 1687038a8942SZach Atkins for (CeedInt p = 0; p < num_points; p++) { 1688038a8942SZach Atkins // Dim**2 contractions, apply grad when pass == dim 1689038a8942SZach Atkins for (CeedInt pass = 0; pass < dim; pass++) { 1690038a8942SZach Atkins CeedInt pre = num_comp * 1, post = 1; 1691038a8942SZach Atkins 1692038a8942SZach Atkins for (CeedInt d = 0; d < dim; d++) { 1693038a8942SZach Atkins // ------ Tensor contract with current Chebyshev polynomial values 1694038a8942SZach Atkins if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[p * dim + d], Q_1d, chebyshev_x)); 1695038a8942SZach Atkins else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[p * dim + d], Q_1d, chebyshev_x)); 1696038a8942SZach Atkins CeedCall(CeedTensorContractApply( 1697038a8942SZach Atkins basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, (p > 0 || (p == 0 && pass > 0)) && d == (dim - 1), 1698038a8942SZach Atkins d == 0 ? &u_array[p * num_comp * dim + pass] : tmp[d % 2], d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2])); 1699038a8942SZach Atkins pre /= 1; 1700038a8942SZach Atkins post *= Q_1d; 1701038a8942SZach Atkins } 1702038a8942SZach Atkins } 1703038a8942SZach Atkins } 1704038a8942SZach Atkins break; 1705038a8942SZach Atkins } 1706038a8942SZach Atkins default: 1707038a8942SZach Atkins // Nothing to do, excluded above 1708038a8942SZach Atkins break; 17092a94f45fSJeremy L Thompson } 17102a94f45fSJeremy L Thompson CeedCall(CeedVectorRestoreArray(basis->vec_chebyshev, &chebyshev_coeffs)); 17112a94f45fSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read)); 17122a94f45fSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(u, &u_array)); 17132a94f45fSJeremy L Thompson 17142a94f45fSJeremy L Thompson // -- Interpolate transpose from Chebyshev coefficients 17152a94f45fSJeremy L Thompson CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v)); 17162a94f45fSJeremy L Thompson break; 17172a94f45fSJeremy L Thompson } 1718c8c3fa7dSJeremy L Thompson } 1719c8c3fa7dSJeremy L Thompson return CEED_ERROR_SUCCESS; 1720c8c3fa7dSJeremy L Thompson } 1721c8c3fa7dSJeremy L Thompson 1722c8c3fa7dSJeremy L Thompson /** 1723b7c9bbdaSJeremy L Thompson @brief Get Ceed associated with a CeedBasis 1724b7c9bbdaSJeremy L Thompson 1725ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1726b7c9bbdaSJeremy L Thompson @param[out] ceed Variable to store Ceed 1727b7c9bbdaSJeremy L Thompson 1728b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1729b7c9bbdaSJeremy L Thompson 1730b7c9bbdaSJeremy L Thompson @ref Advanced 1731b7c9bbdaSJeremy L Thompson **/ 1732b7c9bbdaSJeremy L Thompson int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) { 1733b7c9bbdaSJeremy L Thompson *ceed = basis->ceed; 1734b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1735b7c9bbdaSJeremy L Thompson } 1736b7c9bbdaSJeremy L Thompson 1737b7c9bbdaSJeremy L Thompson /** 17389d007619Sjeremylt @brief Get dimension for given CeedBasis 17399d007619Sjeremylt 1740ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 17419d007619Sjeremylt @param[out] dim Variable to store dimension of basis 17429d007619Sjeremylt 17439d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 17449d007619Sjeremylt 1745b7c9bbdaSJeremy L Thompson @ref Advanced 17469d007619Sjeremylt **/ 17479d007619Sjeremylt int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) { 17489d007619Sjeremylt *dim = basis->dim; 1749e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 17509d007619Sjeremylt } 17519d007619Sjeremylt 17529d007619Sjeremylt /** 1753d99fa3c5SJeremy L Thompson @brief Get topology for given CeedBasis 1754d99fa3c5SJeremy L Thompson 1755ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1756d99fa3c5SJeremy L Thompson @param[out] topo Variable to store topology of basis 1757d99fa3c5SJeremy L Thompson 1758d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1759d99fa3c5SJeremy L Thompson 1760b7c9bbdaSJeremy L Thompson @ref Advanced 1761d99fa3c5SJeremy L Thompson **/ 1762d99fa3c5SJeremy L Thompson int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) { 1763d99fa3c5SJeremy L Thompson *topo = basis->topo; 1764e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1765d99fa3c5SJeremy L Thompson } 1766d99fa3c5SJeremy L Thompson 1767d99fa3c5SJeremy L Thompson /** 17689d007619Sjeremylt @brief Get number of components for given CeedBasis 17699d007619Sjeremylt 1770ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1771d1d35e2fSjeremylt @param[out] num_comp Variable to store number of components of basis 17729d007619Sjeremylt 17739d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 17749d007619Sjeremylt 1775b7c9bbdaSJeremy L Thompson @ref Advanced 17769d007619Sjeremylt **/ 1777d1d35e2fSjeremylt int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) { 1778d1d35e2fSjeremylt *num_comp = basis->num_comp; 1779e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 17809d007619Sjeremylt } 17819d007619Sjeremylt 17829d007619Sjeremylt /** 17839d007619Sjeremylt @brief Get total number of nodes (in dim dimensions) of a CeedBasis 17849d007619Sjeremylt 1785ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 17869d007619Sjeremylt @param[out] P Variable to store number of nodes 17879d007619Sjeremylt 17889d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 17899d007619Sjeremylt 17909d007619Sjeremylt @ref Utility 17919d007619Sjeremylt **/ 17929d007619Sjeremylt int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) { 17939d007619Sjeremylt *P = basis->P; 1794e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 17959d007619Sjeremylt } 17969d007619Sjeremylt 17979d007619Sjeremylt /** 17989d007619Sjeremylt @brief Get total number of nodes (in 1 dimension) of a CeedBasis 17999d007619Sjeremylt 1800ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1801d1d35e2fSjeremylt @param[out] P_1d Variable to store number of nodes 18029d007619Sjeremylt 18039d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18049d007619Sjeremylt 1805b7c9bbdaSJeremy L Thompson @ref Advanced 18069d007619Sjeremylt **/ 1807d1d35e2fSjeremylt int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) { 18086402da51SJeremy L Thompson CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "Cannot supply P_1d for non-tensor basis"); 1809d1d35e2fSjeremylt *P_1d = basis->P_1d; 1810e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18119d007619Sjeremylt } 18129d007619Sjeremylt 18139d007619Sjeremylt /** 18149d007619Sjeremylt @brief Get total number of quadrature points (in dim dimensions) of a CeedBasis 18159d007619Sjeremylt 1816ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 18179d007619Sjeremylt @param[out] Q Variable to store number of quadrature points 18189d007619Sjeremylt 18199d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18209d007619Sjeremylt 18219d007619Sjeremylt @ref Utility 18229d007619Sjeremylt **/ 18239d007619Sjeremylt int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) { 18249d007619Sjeremylt *Q = basis->Q; 1825e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18269d007619Sjeremylt } 18279d007619Sjeremylt 18289d007619Sjeremylt /** 18299d007619Sjeremylt @brief Get total number of quadrature points (in 1 dimension) of a CeedBasis 18309d007619Sjeremylt 1831ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1832d1d35e2fSjeremylt @param[out] Q_1d Variable to store number of quadrature points 18339d007619Sjeremylt 18349d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18359d007619Sjeremylt 1836b7c9bbdaSJeremy L Thompson @ref Advanced 18379d007619Sjeremylt **/ 1838d1d35e2fSjeremylt int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) { 18396402da51SJeremy L Thompson CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "Cannot supply Q_1d for non-tensor basis"); 1840d1d35e2fSjeremylt *Q_1d = basis->Q_1d; 1841e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18429d007619Sjeremylt } 18439d007619Sjeremylt 18449d007619Sjeremylt /** 1845ea61e9acSJeremy L Thompson @brief Get reference coordinates of quadrature points (in dim dimensions) of a CeedBasis 18469d007619Sjeremylt 1847ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1848d1d35e2fSjeremylt @param[out] q_ref Variable to store reference coordinates of quadrature points 18499d007619Sjeremylt 18509d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18519d007619Sjeremylt 1852b7c9bbdaSJeremy L Thompson @ref Advanced 18539d007619Sjeremylt **/ 1854d1d35e2fSjeremylt int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) { 1855d1d35e2fSjeremylt *q_ref = basis->q_ref_1d; 1856e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18579d007619Sjeremylt } 18589d007619Sjeremylt 18599d007619Sjeremylt /** 1860ea61e9acSJeremy L Thompson @brief Get quadrature weights of quadrature points (in dim dimensions) of a CeedBasis 18619d007619Sjeremylt 1862ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1863d1d35e2fSjeremylt @param[out] q_weight Variable to store quadrature weights 18649d007619Sjeremylt 18659d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18669d007619Sjeremylt 1867b7c9bbdaSJeremy L Thompson @ref Advanced 18689d007619Sjeremylt **/ 1869d1d35e2fSjeremylt int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) { 1870d1d35e2fSjeremylt *q_weight = basis->q_weight_1d; 1871e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18729d007619Sjeremylt } 18739d007619Sjeremylt 18749d007619Sjeremylt /** 18759d007619Sjeremylt @brief Get interpolation matrix of a CeedBasis 18769d007619Sjeremylt 1877ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 18789d007619Sjeremylt @param[out] interp Variable to store interpolation matrix 18799d007619Sjeremylt 18809d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 18819d007619Sjeremylt 1882b7c9bbdaSJeremy L Thompson @ref Advanced 18839d007619Sjeremylt **/ 18846c58de82SJeremy L Thompson int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) { 18856402da51SJeremy L Thompson if (!basis->interp && basis->is_tensor_basis) { 18869d007619Sjeremylt // Allocate 18872b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->Q * basis->P, &basis->interp)); 18889d007619Sjeremylt 18899d007619Sjeremylt // Initialize 18902b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->Q * basis->P; i++) basis->interp[i] = 1.0; 18919d007619Sjeremylt 18929d007619Sjeremylt // Calculate 18932b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 18942b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 18959d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 1896d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 1897d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 18981c66c397SJeremy L Thompson 1899d1d35e2fSjeremylt basis->interp[qpt * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 19009d007619Sjeremylt } 19019d007619Sjeremylt } 19022b730f8bSJeremy L Thompson } 19032b730f8bSJeremy L Thompson } 19049d007619Sjeremylt *interp = basis->interp; 1905e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19069d007619Sjeremylt } 19079d007619Sjeremylt 19089d007619Sjeremylt /** 19099d007619Sjeremylt @brief Get 1D interpolation matrix of a tensor product CeedBasis 19109d007619Sjeremylt 1911ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1912d1d35e2fSjeremylt @param[out] interp_1d Variable to store interpolation matrix 19139d007619Sjeremylt 19149d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19159d007619Sjeremylt 19169d007619Sjeremylt @ref Backend 19179d007619Sjeremylt **/ 1918d1d35e2fSjeremylt int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) { 19196402da51SJeremy L Thompson CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "CeedBasis is not a tensor product basis."); 1920d1d35e2fSjeremylt *interp_1d = basis->interp_1d; 1921e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19229d007619Sjeremylt } 19239d007619Sjeremylt 19249d007619Sjeremylt /** 19259d007619Sjeremylt @brief Get gradient matrix of a CeedBasis 19269d007619Sjeremylt 1927ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 19289d007619Sjeremylt @param[out] grad Variable to store gradient matrix 19299d007619Sjeremylt 19309d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19319d007619Sjeremylt 1932b7c9bbdaSJeremy L Thompson @ref Advanced 19339d007619Sjeremylt **/ 19346c58de82SJeremy L Thompson int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) { 19356402da51SJeremy L Thompson if (!basis->grad && basis->is_tensor_basis) { 19369d007619Sjeremylt // Allocate 19372b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->dim * basis->Q * basis->P, &basis->grad)); 19389d007619Sjeremylt 19399d007619Sjeremylt // Initialize 19402b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim * basis->Q * basis->P; i++) basis->grad[i] = 1.0; 19419d007619Sjeremylt 19429d007619Sjeremylt // Calculate 19432b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 19442b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim; i++) { 19452b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 19469d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 1947d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 1948d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 19491c66c397SJeremy L Thompson 19502b730f8bSJeremy L Thompson if (i == d) basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->grad_1d[q * basis->P_1d + p]; 19512b730f8bSJeremy L Thompson else basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 19522b730f8bSJeremy L Thompson } 19532b730f8bSJeremy L Thompson } 19542b730f8bSJeremy L Thompson } 19559d007619Sjeremylt } 19569d007619Sjeremylt } 19579d007619Sjeremylt *grad = basis->grad; 1958e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19599d007619Sjeremylt } 19609d007619Sjeremylt 19619d007619Sjeremylt /** 19629d007619Sjeremylt @brief Get 1D gradient matrix of a tensor product CeedBasis 19639d007619Sjeremylt 1964ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1965d1d35e2fSjeremylt @param[out] grad_1d Variable to store gradient matrix 19669d007619Sjeremylt 19679d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 19689d007619Sjeremylt 1969b7c9bbdaSJeremy L Thompson @ref Advanced 19709d007619Sjeremylt **/ 1971d1d35e2fSjeremylt int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) { 19726402da51SJeremy L Thompson CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "CeedBasis is not a tensor product basis."); 1973d1d35e2fSjeremylt *grad_1d = basis->grad_1d; 1974e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 19759d007619Sjeremylt } 19769d007619Sjeremylt 19779d007619Sjeremylt /** 197850c301a5SRezgar Shakeri @brief Get divergence matrix of a CeedBasis 197950c301a5SRezgar Shakeri 1980ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 198150c301a5SRezgar Shakeri @param[out] div Variable to store divergence matrix 198250c301a5SRezgar Shakeri 198350c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 198450c301a5SRezgar Shakeri 198550c301a5SRezgar Shakeri @ref Advanced 198650c301a5SRezgar Shakeri **/ 198750c301a5SRezgar Shakeri int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) { 19886574a04fSJeremy L Thompson CeedCheck(basis->div, basis->ceed, CEED_ERROR_MINOR, "CeedBasis does not have divergence matrix."); 198950c301a5SRezgar Shakeri *div = basis->div; 199050c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 199150c301a5SRezgar Shakeri } 199250c301a5SRezgar Shakeri 199350c301a5SRezgar Shakeri /** 1994c4e3f59bSSebastian Grimberg @brief Get curl matrix of a CeedBasis 1995c4e3f59bSSebastian Grimberg 1996c4e3f59bSSebastian Grimberg @param[in] basis CeedBasis 1997c4e3f59bSSebastian Grimberg @param[out] curl Variable to store curl matrix 1998c4e3f59bSSebastian Grimberg 1999c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 2000c4e3f59bSSebastian Grimberg 2001c4e3f59bSSebastian Grimberg @ref Advanced 2002c4e3f59bSSebastian Grimberg **/ 2003c4e3f59bSSebastian Grimberg int CeedBasisGetCurl(CeedBasis basis, const CeedScalar **curl) { 20046574a04fSJeremy L Thompson CeedCheck(basis->curl, basis->ceed, CEED_ERROR_MINOR, "CeedBasis does not have curl matrix."); 2005c4e3f59bSSebastian Grimberg *curl = basis->curl; 2006c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 2007c4e3f59bSSebastian Grimberg } 2008c4e3f59bSSebastian Grimberg 2009c4e3f59bSSebastian Grimberg /** 20107a982d89SJeremy L. Thompson @brief Destroy a CeedBasis 20117a982d89SJeremy L. Thompson 2012ea61e9acSJeremy L Thompson @param[in,out] basis CeedBasis to destroy 20137a982d89SJeremy L. Thompson 20147a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 20157a982d89SJeremy L. Thompson 20167a982d89SJeremy L. Thompson @ref User 20177a982d89SJeremy L. Thompson **/ 20187a982d89SJeremy L. Thompson int CeedBasisDestroy(CeedBasis *basis) { 2019356036faSJeremy L Thompson if (!*basis || *basis == CEED_BASIS_NONE || --(*basis)->ref_count > 0) { 2020ad6481ceSJeremy L Thompson *basis = NULL; 2021ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 2022ad6481ceSJeremy L Thompson } 20232b730f8bSJeremy L Thompson if ((*basis)->Destroy) CeedCall((*basis)->Destroy(*basis)); 20249831d45aSJeremy L Thompson CeedCall(CeedTensorContractDestroy(&(*basis)->contract)); 2025c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_ref_1d)); 2026c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_weight_1d)); 20272b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp)); 20282b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp_1d)); 20292b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad)); 20302b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad_1d)); 2031c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->div)); 2032c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->curl)); 2033c8c3fa7dSJeremy L Thompson CeedCall(CeedVectorDestroy(&(*basis)->vec_chebyshev)); 2034c8c3fa7dSJeremy L Thompson CeedCall(CeedBasisDestroy(&(*basis)->basis_chebyshev)); 20352b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*basis)->ceed)); 20362b730f8bSJeremy L Thompson CeedCall(CeedFree(basis)); 2037e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 20387a982d89SJeremy L. Thompson } 20397a982d89SJeremy L. Thompson 20407a982d89SJeremy L. Thompson /** 2041b11c1e72Sjeremylt @brief Construct a Gauss-Legendre quadrature 2042b11c1e72Sjeremylt 2043ea61e9acSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree 2*Q-1 exactly) 2044d1d35e2fSjeremylt @param[out] q_ref_1d Array of length Q to hold the abscissa on [-1, 1] 2045d1d35e2fSjeremylt @param[out] q_weight_1d Array of length Q to hold the weights 2046b11c1e72Sjeremylt 2047b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 2048dfdf5a53Sjeremylt 2049dfdf5a53Sjeremylt @ref Utility 2050b11c1e72Sjeremylt **/ 20512b730f8bSJeremy L Thompson int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2052d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0 * atan(1.0); 20531c66c397SJeremy L Thompson 2054d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 205592ae7e47SJeremy L Thompson for (CeedInt i = 0; i <= Q / 2; i++) { 2056d7b241e6Sjeremylt // Guess 2057d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(2 * i + 1) / ((CeedScalar)(2 * Q))); 2058d7b241e6Sjeremylt // Pn(xi) 2059d7b241e6Sjeremylt P0 = 1.0; 2060d7b241e6Sjeremylt P1 = xi; 2061d7b241e6Sjeremylt P2 = 0.0; 206292ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 2063d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2064d7b241e6Sjeremylt P0 = P1; 2065d7b241e6Sjeremylt P1 = P2; 2066d7b241e6Sjeremylt } 2067d7b241e6Sjeremylt // First Newton Step 2068d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2069d7b241e6Sjeremylt xi = xi - P2 / dP2; 2070d7b241e6Sjeremylt // Newton to convergence 207192ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(P2) > 10 * CEED_EPSILON; k++) { 2072d7b241e6Sjeremylt P0 = 1.0; 2073d7b241e6Sjeremylt P1 = xi; 207492ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 2075d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2076d7b241e6Sjeremylt P0 = P1; 2077d7b241e6Sjeremylt P1 = P2; 2078d7b241e6Sjeremylt } 2079d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2080d7b241e6Sjeremylt xi = xi - P2 / dP2; 2081d7b241e6Sjeremylt } 2082d7b241e6Sjeremylt // Save xi, wi 2083d7b241e6Sjeremylt wi = 2.0 / ((1.0 - xi * xi) * dP2 * dP2); 2084d1d35e2fSjeremylt q_weight_1d[i] = wi; 2085d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 2086d1d35e2fSjeremylt q_ref_1d[i] = -xi; 2087d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 2088d7b241e6Sjeremylt } 2089e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2090d7b241e6Sjeremylt } 2091d7b241e6Sjeremylt 2092b11c1e72Sjeremylt /** 2093b11c1e72Sjeremylt @brief Construct a Gauss-Legendre-Lobatto quadrature 2094b11c1e72Sjeremylt 2095ea61e9acSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree 2*Q-3 exactly) 2096d1d35e2fSjeremylt @param[out] q_ref_1d Array of length Q to hold the abscissa on [-1, 1] 2097d1d35e2fSjeremylt @param[out] q_weight_1d Array of length Q to hold the weights 2098b11c1e72Sjeremylt 2099b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 2100dfdf5a53Sjeremylt 2101dfdf5a53Sjeremylt @ref Utility 2102b11c1e72Sjeremylt **/ 21032b730f8bSJeremy L Thompson int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2104d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0 * atan(1.0); 21051c66c397SJeremy L Thompson 2106d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 2107d7b241e6Sjeremylt // Set endpoints 21086574a04fSJeremy L Thompson CeedCheck(Q > 1, NULL, CEED_ERROR_DIMENSION, "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q); 2109d7b241e6Sjeremylt wi = 2.0 / ((CeedScalar)(Q * (Q - 1))); 2110d1d35e2fSjeremylt if (q_weight_1d) { 2111d1d35e2fSjeremylt q_weight_1d[0] = wi; 2112d1d35e2fSjeremylt q_weight_1d[Q - 1] = wi; 2113d7b241e6Sjeremylt } 2114d1d35e2fSjeremylt q_ref_1d[0] = -1.0; 2115d1d35e2fSjeremylt q_ref_1d[Q - 1] = 1.0; 2116d7b241e6Sjeremylt // Interior 211792ae7e47SJeremy L Thompson for (CeedInt i = 1; i <= (Q - 1) / 2; i++) { 2118d7b241e6Sjeremylt // Guess 2119d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(i) / (CeedScalar)(Q - 1)); 2120d7b241e6Sjeremylt // Pn(xi) 2121d7b241e6Sjeremylt P0 = 1.0; 2122d7b241e6Sjeremylt P1 = xi; 2123d7b241e6Sjeremylt P2 = 0.0; 212492ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 2125d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2126d7b241e6Sjeremylt P0 = P1; 2127d7b241e6Sjeremylt P1 = P2; 2128d7b241e6Sjeremylt } 2129d7b241e6Sjeremylt // First Newton step 2130d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2131d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2132d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 2133d7b241e6Sjeremylt // Newton to convergence 213492ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(dP2) > 10 * CEED_EPSILON; k++) { 2135d7b241e6Sjeremylt P0 = 1.0; 2136d7b241e6Sjeremylt P1 = xi; 213792ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 2138d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2139d7b241e6Sjeremylt P0 = P1; 2140d7b241e6Sjeremylt P1 = P2; 2141d7b241e6Sjeremylt } 2142d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2143d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2144d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 2145d7b241e6Sjeremylt } 2146d7b241e6Sjeremylt // Save xi, wi 2147d7b241e6Sjeremylt wi = 2.0 / (((CeedScalar)(Q * (Q - 1))) * P2 * P2); 2148d1d35e2fSjeremylt if (q_weight_1d) { 2149d1d35e2fSjeremylt q_weight_1d[i] = wi; 2150d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 2151d7b241e6Sjeremylt } 2152d1d35e2fSjeremylt q_ref_1d[i] = -xi; 2153d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 2154d7b241e6Sjeremylt } 2155e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2156d7b241e6Sjeremylt } 2157d7b241e6Sjeremylt 2158d7b241e6Sjeremylt /// @} 2159