13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3d7b241e6Sjeremylt // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5d7b241e6Sjeremylt // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7d7b241e6Sjeremylt 83d576824SJeremy L Thompson #include <ceed-impl.h> 949aac155SJeremy L Thompson #include <ceed.h> 102b730f8bSJeremy L Thompson #include <ceed/backend.h> 11d7b241e6Sjeremylt #include <math.h> 123d576824SJeremy L Thompson #include <stdbool.h> 13d7b241e6Sjeremylt #include <stdio.h> 14d7b241e6Sjeremylt #include <string.h> 15d7b241e6Sjeremylt 167a982d89SJeremy L. Thompson /// @file 177a982d89SJeremy L. Thompson /// Implementation of CeedBasis interfaces 187a982d89SJeremy L. Thompson 19d7b241e6Sjeremylt /// @cond DOXYGEN_SKIP 20783c99b3SValeria Barra static struct CeedBasis_private ceed_basis_collocated; 21d7b241e6Sjeremylt /// @endcond 22d7b241e6Sjeremylt 237a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser 247a982d89SJeremy L. Thompson /// @{ 257a982d89SJeremy L. Thompson 267a982d89SJeremy L. Thompson /// Indicate that the quadrature points are collocated with the nodes 277a982d89SJeremy L. Thompson const CeedBasis CEED_BASIS_COLLOCATED = &ceed_basis_collocated; 287a982d89SJeremy L. Thompson 297a982d89SJeremy L. Thompson /// @} 307a982d89SJeremy L. Thompson 317a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 327a982d89SJeremy L. Thompson /// CeedBasis Library Internal Functions 337a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 347a982d89SJeremy L. Thompson /// @addtogroup CeedBasisDeveloper 357a982d89SJeremy L. Thompson /// @{ 367a982d89SJeremy L. Thompson 377a982d89SJeremy L. Thompson /** 387a982d89SJeremy L. Thompson @brief Compute Householder reflection 397a982d89SJeremy L. Thompson 40ea61e9acSJeremy L Thompson Computes A = (I - b v v^T) A, where A is an mxn matrix indexed as A[i*row + j*col] 417a982d89SJeremy L. Thompson 427a982d89SJeremy L. Thompson @param[in,out] A Matrix to apply Householder reflection to, in place 43ea61e9acSJeremy L Thompson @param[in] v Householder vector 44ea61e9acSJeremy L Thompson @param[in] b Scaling factor 45ea61e9acSJeremy L Thompson @param[in] m Number of rows in A 46ea61e9acSJeremy L Thompson @param[in] n Number of columns in A 47ea61e9acSJeremy L Thompson @param[in] row Row stride 48ea61e9acSJeremy L Thompson @param[in] col Col stride 497a982d89SJeremy L. Thompson 507a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 517a982d89SJeremy L. Thompson 527a982d89SJeremy L. Thompson @ref Developer 537a982d89SJeremy L. Thompson **/ 542b730f8bSJeremy L Thompson static int CeedHouseholderReflect(CeedScalar *A, const CeedScalar *v, CeedScalar b, CeedInt m, CeedInt n, CeedInt row, CeedInt col) { 557a982d89SJeremy L. Thompson for (CeedInt j = 0; j < n; j++) { 567a982d89SJeremy L. Thompson CeedScalar w = A[0 * row + j * col]; 572b730f8bSJeremy L Thompson for (CeedInt i = 1; i < m; i++) w += v[i] * A[i * row + j * col]; 587a982d89SJeremy L. Thompson A[0 * row + j * col] -= b * w; 592b730f8bSJeremy L Thompson for (CeedInt i = 1; i < m; i++) A[i * row + j * col] -= b * w * v[i]; 607a982d89SJeremy L. Thompson } 61e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 627a982d89SJeremy L. Thompson } 637a982d89SJeremy L. Thompson 647a982d89SJeremy L. Thompson /** 657a982d89SJeremy L. Thompson @brief Compute Givens rotation 667a982d89SJeremy L. Thompson 67ea61e9acSJeremy L Thompson Computes A = G A (or G^T A in transpose mode), where A is an mxn matrix indexed as A[i*n + j*m] 687a982d89SJeremy L. Thompson 697a982d89SJeremy L. Thompson @param[in,out] A Row major matrix to apply Givens rotation to, in place 70ea61e9acSJeremy L Thompson @param[in] c Cosine factor 71ea61e9acSJeremy L Thompson @param[in] s Sine factor 72ea61e9acSJeremy L Thompson @param[in] t_mode @ref CEED_NOTRANSPOSE to rotate the basis counter-clockwise, which has the effect of rotating columns of A clockwise; 734cc79fe7SJed Brown @ref CEED_TRANSPOSE for the opposite rotation 74ea61e9acSJeremy L Thompson @param[in] i First row/column to apply rotation 75ea61e9acSJeremy L Thompson @param[in] k Second row/column to apply rotation 76ea61e9acSJeremy L Thompson @param[in] m Number of rows in A 77ea61e9acSJeremy L Thompson @param[in] n Number of columns in A 787a982d89SJeremy L. Thompson 797a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 807a982d89SJeremy L. Thompson 817a982d89SJeremy L. Thompson @ref Developer 827a982d89SJeremy L. Thompson **/ 832b730f8bSJeremy L Thompson static int CeedGivensRotation(CeedScalar *A, CeedScalar c, CeedScalar s, CeedTransposeMode t_mode, CeedInt i, CeedInt k, CeedInt m, CeedInt n) { 84d1d35e2fSjeremylt CeedInt stride_j = 1, stride_ik = m, num_its = n; 85d1d35e2fSjeremylt if (t_mode == CEED_NOTRANSPOSE) { 862b730f8bSJeremy L Thompson stride_j = n; 872b730f8bSJeremy L Thompson stride_ik = 1; 882b730f8bSJeremy L Thompson num_its = m; 897a982d89SJeremy L. Thompson } 907a982d89SJeremy L. Thompson 917a982d89SJeremy L. Thompson // Apply rotation 92d1d35e2fSjeremylt for (CeedInt j = 0; j < num_its; j++) { 93d1d35e2fSjeremylt CeedScalar tau1 = A[i * stride_ik + j * stride_j], tau2 = A[k * stride_ik + j * stride_j]; 94d1d35e2fSjeremylt A[i * stride_ik + j * stride_j] = c * tau1 - s * tau2; 95d1d35e2fSjeremylt A[k * stride_ik + j * stride_j] = s * tau1 + c * tau2; 967a982d89SJeremy L. Thompson } 97e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 987a982d89SJeremy L. Thompson } 997a982d89SJeremy L. Thompson 1007a982d89SJeremy L. Thompson /** 1017a982d89SJeremy L. Thompson @brief View an array stored in a CeedBasis 1027a982d89SJeremy L. Thompson 1030a0da059Sjeremylt @param[in] name Name of array 104d1d35e2fSjeremylt @param[in] fp_fmt Printing format 1050a0da059Sjeremylt @param[in] m Number of rows in array 1060a0da059Sjeremylt @param[in] n Number of columns in array 1070a0da059Sjeremylt @param[in] a Array to be viewed 1080a0da059Sjeremylt @param[in] stream Stream to view to, e.g., stdout 1097a982d89SJeremy L. Thompson 1107a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1117a982d89SJeremy L. Thompson 1127a982d89SJeremy L. Thompson @ref Developer 1137a982d89SJeremy L. Thompson **/ 1142b730f8bSJeremy L Thompson static int CeedScalarView(const char *name, const char *fp_fmt, CeedInt m, CeedInt n, const CeedScalar *a, FILE *stream) { 11592ae7e47SJeremy L Thompson for (CeedInt i = 0; i < m; i++) { 1162b730f8bSJeremy L Thompson if (m > 1) fprintf(stream, "%12s[%" CeedInt_FMT "]:", name, i); 1172b730f8bSJeremy L Thompson else fprintf(stream, "%12s:", name); 1182b730f8bSJeremy L Thompson for (CeedInt j = 0; j < n; j++) fprintf(stream, fp_fmt, fabs(a[i * n + j]) > 1E-14 ? a[i * n + j] : 0); 1197a982d89SJeremy L. Thompson fputs("\n", stream); 1207a982d89SJeremy L. Thompson } 121e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1227a982d89SJeremy L. Thompson } 1237a982d89SJeremy L. Thompson 124a76a04e7SJeremy L Thompson /** 125ea61e9acSJeremy L Thompson @brief Create the interpolation and gradient matrices for projection from the nodes of `basis_from` to the nodes of `basis_to`. 126ba59ac12SSebastian Grimberg 12715ad3917SSebastian Grimberg The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization. 12815ad3917SSebastian Grimberg The gradient is given by `grad_project = interp_to^+ * grad_from`, and is only computed for H^1 spaces otherwise it should not be used. 12915ad3917SSebastian Grimberg 130ba59ac12SSebastian Grimberg Note: `basis_from` and `basis_to` must have compatible quadrature spaces. 131a76a04e7SJeremy L Thompson 132a76a04e7SJeremy L Thompson @param[in] basis_from CeedBasis to project from 133a76a04e7SJeremy L Thompson @param[in] basis_to CeedBasis to project to 134ea61e9acSJeremy L Thompson @param[out] interp_project Address of the variable where the newly created interpolation matrix will be stored. 135ea61e9acSJeremy L Thompson @param[out] grad_project Address of the variable where the newly created gradient matrix will be stored. 136a76a04e7SJeremy L Thompson 137a76a04e7SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 138a76a04e7SJeremy L Thompson 139a76a04e7SJeremy L Thompson @ref Developer 140a76a04e7SJeremy L Thompson **/ 1412b730f8bSJeremy L Thompson static int CeedBasisCreateProjectionMatrices(CeedBasis basis_from, CeedBasis basis_to, CeedScalar **interp_project, CeedScalar **grad_project) { 142a76a04e7SJeremy L Thompson Ceed ceed; 1432b730f8bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis_to, &ceed)); 144a76a04e7SJeremy L Thompson 145a76a04e7SJeremy L Thompson // Check for compatible quadrature spaces 146a76a04e7SJeremy L Thompson CeedInt Q_to, Q_from; 1472b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_to, &Q_to)); 1482b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_from, &Q_from)); 1496574a04fSJeremy L Thompson CeedCheck(Q_to == Q_from, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces"); 150a76a04e7SJeremy L Thompson 15114556e63SJeremy L Thompson // Check for matching tensor or non-tensor 152a76a04e7SJeremy L Thompson CeedInt P_to, P_from, Q = Q_to; 153a76a04e7SJeremy L Thompson bool is_tensor_to, is_tensor_from; 1542b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to)); 1552b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from)); 1566574a04fSJeremy L Thompson CeedCheck(is_tensor_to == is_tensor_from, ceed, CEED_ERROR_MINOR, "Bases must both be tensor or non-tensor"); 1576574a04fSJeremy L Thompson if (is_tensor_to) { 1582b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_to)); 1592b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_from)); 1602b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis_from, &Q)); 1616574a04fSJeremy L Thompson } else { 1622b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_to, &P_to)); 1632b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_from, &P_from)); 164a76a04e7SJeremy L Thompson } 165a76a04e7SJeremy L Thompson 16615ad3917SSebastian Grimberg // Check for matching FE space 16715ad3917SSebastian Grimberg CeedFESpace fe_space_to, fe_space_from; 16815ad3917SSebastian Grimberg CeedCall(CeedBasisGetFESpace(basis_to, &fe_space_to)); 16915ad3917SSebastian Grimberg CeedCall(CeedBasisGetFESpace(basis_from, &fe_space_from)); 1706574a04fSJeremy L Thompson CeedCheck(fe_space_to == fe_space_from, ceed, CEED_ERROR_MINOR, "Bases must both be the same FE space type"); 17115ad3917SSebastian Grimberg 17214556e63SJeremy L Thompson // Get source matrices 17315ad3917SSebastian Grimberg CeedInt dim, q_comp = 1; 17415ad3917SSebastian Grimberg const CeedScalar *interp_to_source = NULL, *interp_from_source = NULL; 17514556e63SJeremy L Thompson CeedScalar *interp_to, *interp_from, *tau; 1762b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_to, &dim)); 177a76a04e7SJeremy L Thompson if (is_tensor_to) { 1782b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis_to, &interp_to_source)); 1792b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis_from, &interp_from_source)); 180a76a04e7SJeremy L Thompson } else { 18115ad3917SSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis_from, CEED_EVAL_INTERP, &q_comp)); 1822b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis_to, &interp_to_source)); 1832b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp(basis_from, &interp_from_source)); 18415ad3917SSebastian Grimberg } 18515ad3917SSebastian Grimberg CeedCall(CeedMalloc(Q * P_from * q_comp, &interp_from)); 18615ad3917SSebastian Grimberg CeedCall(CeedMalloc(Q * P_to * q_comp, &interp_to)); 18715ad3917SSebastian Grimberg CeedCall(CeedCalloc(P_to * P_from, interp_project)); 18815ad3917SSebastian Grimberg CeedCall(CeedMalloc(Q * q_comp, &tau)); 18915ad3917SSebastian Grimberg 19015ad3917SSebastian Grimberg // `grad_project = interp_to^+ * grad_from` is computed for the H^1 space case so the 191de05fbb2SSebastian Grimberg // projection basis will have a gradient operation (allocated even if not H^1 for the 192de05fbb2SSebastian Grimberg // basis construction later on) 19315ad3917SSebastian Grimberg const CeedScalar *grad_from_source = NULL; 19415ad3917SSebastian Grimberg if (fe_space_to == CEED_FE_SPACE_H1) { 19515ad3917SSebastian Grimberg if (is_tensor_to) { 19615ad3917SSebastian Grimberg CeedCall(CeedBasisGetGrad1D(basis_from, &grad_from_source)); 19715ad3917SSebastian Grimberg } else { 1982b730f8bSJeremy L Thompson CeedCall(CeedBasisGetGrad(basis_from, &grad_from_source)); 199a76a04e7SJeremy L Thompson } 200de05fbb2SSebastian Grimberg } 20115ad3917SSebastian Grimberg CeedCall(CeedCalloc(P_to * P_from * (is_tensor_to ? 1 : dim), grad_project)); 20215ad3917SSebastian Grimberg 20315ad3917SSebastian Grimberg // QR Factorization, interp_to = Q R 20415ad3917SSebastian Grimberg memcpy(interp_to, interp_to_source, Q * P_to * q_comp * sizeof(interp_to_source[0])); 20515ad3917SSebastian Grimberg CeedCall(CeedQRFactorization(ceed, interp_to, tau, Q * q_comp, P_to)); 206a76a04e7SJeremy L Thompson 20714556e63SJeremy L Thompson // Build matrices 20815ad3917SSebastian Grimberg CeedInt num_matrices = 1 + (fe_space_to == CEED_FE_SPACE_H1) * (is_tensor_to ? 1 : dim); 20914556e63SJeremy L Thompson CeedScalar *input_from[num_matrices], *output_project[num_matrices]; 21014556e63SJeremy L Thompson input_from[0] = (CeedScalar *)interp_from_source; 21114556e63SJeremy L Thompson output_project[0] = *interp_project; 21214556e63SJeremy L Thompson for (CeedInt m = 1; m < num_matrices; m++) { 21314556e63SJeremy L Thompson input_from[m] = (CeedScalar *)&grad_from_source[(m - 1) * Q * P_from]; 21402af4036SJeremy L Thompson output_project[m] = &((*grad_project)[(m - 1) * P_to * P_from]); 21514556e63SJeremy L Thompson } 21614556e63SJeremy L Thompson for (CeedInt m = 0; m < num_matrices; m++) { 21715ad3917SSebastian Grimberg // Apply Q^T, interp_from = Q^T interp_from 21815ad3917SSebastian Grimberg memcpy(interp_from, input_from[m], Q * P_from * q_comp * sizeof(input_from[m][0])); 21915ad3917SSebastian Grimberg CeedCall(CeedHouseholderApplyQ(interp_from, interp_to, tau, CEED_TRANSPOSE, Q * q_comp, P_from, P_to, P_from, 1)); 220a76a04e7SJeremy L Thompson 22115ad3917SSebastian Grimberg // Apply Rinv, output_project = Rinv interp_from 222a76a04e7SJeremy L Thompson for (CeedInt j = 0; j < P_from; j++) { // Column j 2232b730f8bSJeremy 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]; 224a76a04e7SJeremy L Thompson for (CeedInt i = P_to - 2; i >= 0; i--) { // Row i 22514556e63SJeremy L Thompson output_project[m][j + P_from * i] = interp_from[j + P_from * i]; 226a76a04e7SJeremy L Thompson for (CeedInt k = i + 1; k < P_to; k++) { 2272b730f8bSJeremy L Thompson output_project[m][j + P_from * i] -= interp_to[k + P_to * i] * output_project[m][j + P_from * k]; 228a76a04e7SJeremy L Thompson } 22914556e63SJeremy L Thompson output_project[m][j + P_from * i] /= interp_to[i + P_to * i]; 230a76a04e7SJeremy L Thompson } 231a76a04e7SJeremy L Thompson } 23214556e63SJeremy L Thompson } 23314556e63SJeremy L Thompson 23414556e63SJeremy L Thompson // Cleanup 2352b730f8bSJeremy L Thompson CeedCall(CeedFree(&tau)); 2362b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_to)); 2372b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_from)); 238a76a04e7SJeremy L Thompson 239a76a04e7SJeremy L Thompson return CEED_ERROR_SUCCESS; 240a76a04e7SJeremy L Thompson } 241a76a04e7SJeremy L Thompson 2427a982d89SJeremy L. Thompson /// @} 2437a982d89SJeremy L. Thompson 2447a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 2457a982d89SJeremy L. Thompson /// Ceed Backend API 2467a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 2477a982d89SJeremy L. Thompson /// @addtogroup CeedBasisBackend 2487a982d89SJeremy L. Thompson /// @{ 2497a982d89SJeremy L. Thompson 2507a982d89SJeremy L. Thompson /** 2517a982d89SJeremy L. Thompson @brief Return collocated grad matrix 2527a982d89SJeremy L. Thompson 253ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 254ea61e9acSJeremy L Thompson @param[out] collo_grad_1d Row-major (Q_1d * Q_1d) matrix expressing derivatives of basis functions at quadrature points 2557a982d89SJeremy L. Thompson 2567a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 2577a982d89SJeremy L. Thompson 2587a982d89SJeremy L. Thompson @ref Backend 2597a982d89SJeremy L. Thompson **/ 260d1d35e2fSjeremylt int CeedBasisGetCollocatedGrad(CeedBasis basis, CeedScalar *collo_grad_1d) { 2617a982d89SJeremy L. Thompson int i, j, k; 2627a982d89SJeremy L. Thompson Ceed ceed; 2632b730f8bSJeremy L Thompson CeedInt P_1d = (basis)->P_1d, Q_1d = (basis)->Q_1d; 26478464608Sjeremylt CeedScalar *interp_1d, *grad_1d, *tau; 2657a982d89SJeremy L. Thompson 2662b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q_1d * P_1d, &interp_1d)); 2672b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q_1d * P_1d, &grad_1d)); 2682b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q_1d, &tau)); 269d1d35e2fSjeremylt memcpy(interp_1d, (basis)->interp_1d, Q_1d * P_1d * sizeof(basis)->interp_1d[0]); 270d1d35e2fSjeremylt memcpy(grad_1d, (basis)->grad_1d, Q_1d * P_1d * sizeof(basis)->interp_1d[0]); 2717a982d89SJeremy L. Thompson 272d1d35e2fSjeremylt // QR Factorization, interp_1d = Q R 2732b730f8bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis, &ceed)); 2742b730f8bSJeremy L Thompson CeedCall(CeedQRFactorization(ceed, interp_1d, tau, Q_1d, P_1d)); 275ea61e9acSJeremy 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. 2767a982d89SJeremy L. Thompson 277d1d35e2fSjeremylt // Apply Rinv, collo_grad_1d = grad_1d Rinv 278d1d35e2fSjeremylt for (i = 0; i < Q_1d; i++) { // Row i 279d1d35e2fSjeremylt collo_grad_1d[Q_1d * i] = grad_1d[P_1d * i] / interp_1d[0]; 280d1d35e2fSjeremylt for (j = 1; j < P_1d; j++) { // Column j 281d1d35e2fSjeremylt collo_grad_1d[j + Q_1d * i] = grad_1d[j + P_1d * i]; 2822b730f8bSJeremy L Thompson for (k = 0; k < j; k++) collo_grad_1d[j + Q_1d * i] -= interp_1d[j + P_1d * k] * collo_grad_1d[k + Q_1d * i]; 283d1d35e2fSjeremylt collo_grad_1d[j + Q_1d * i] /= interp_1d[j + P_1d * j]; 2847a982d89SJeremy L. Thompson } 2852b730f8bSJeremy L Thompson for (j = P_1d; j < Q_1d; j++) collo_grad_1d[j + Q_1d * i] = 0; 2867a982d89SJeremy L. Thompson } 2877a982d89SJeremy L. Thompson 28815ad3917SSebastian Grimberg // Apply Q^T, collo_grad_1d = collo_grad_1d Q^T 2892b730f8bSJeremy L Thompson CeedCall(CeedHouseholderApplyQ(collo_grad_1d, interp_1d, tau, CEED_NOTRANSPOSE, Q_1d, Q_1d, P_1d, 1, Q_1d)); 2907a982d89SJeremy L. Thompson 2912b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_1d)); 2922b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_1d)); 2932b730f8bSJeremy L Thompson CeedCall(CeedFree(&tau)); 294e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2957a982d89SJeremy L. Thompson } 2967a982d89SJeremy L. Thompson 2977a982d89SJeremy L. Thompson /** 2987a982d89SJeremy L. Thompson @brief Get tensor status for given CeedBasis 2997a982d89SJeremy L. Thompson 300ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 301d1d35e2fSjeremylt @param[out] is_tensor Variable to store tensor status 3027a982d89SJeremy L. Thompson 3037a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3047a982d89SJeremy L. Thompson 3057a982d89SJeremy L. Thompson @ref Backend 3067a982d89SJeremy L. Thompson **/ 307d1d35e2fSjeremylt int CeedBasisIsTensor(CeedBasis basis, bool *is_tensor) { 3086402da51SJeremy L Thompson *is_tensor = basis->is_tensor_basis; 309e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3107a982d89SJeremy L. Thompson } 3117a982d89SJeremy L. Thompson 3127a982d89SJeremy L. Thompson /** 3137a982d89SJeremy L. Thompson @brief Get backend data of a CeedBasis 3147a982d89SJeremy L. Thompson 315ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 3167a982d89SJeremy L. Thompson @param[out] data Variable to store data 3177a982d89SJeremy L. Thompson 3187a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3197a982d89SJeremy L. Thompson 3207a982d89SJeremy L. Thompson @ref Backend 3217a982d89SJeremy L. Thompson **/ 322777ff853SJeremy L Thompson int CeedBasisGetData(CeedBasis basis, void *data) { 323777ff853SJeremy L Thompson *(void **)data = basis->data; 324e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3257a982d89SJeremy L. Thompson } 3267a982d89SJeremy L. Thompson 3277a982d89SJeremy L. Thompson /** 3287a982d89SJeremy L. Thompson @brief Set backend data of a CeedBasis 3297a982d89SJeremy L. Thompson 330ea61e9acSJeremy L Thompson @param[in,out] basis CeedBasis 331ea61e9acSJeremy L Thompson @param[in] data Data to set 3327a982d89SJeremy L. Thompson 3337a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3347a982d89SJeremy L. Thompson 3357a982d89SJeremy L. Thompson @ref Backend 3367a982d89SJeremy L. Thompson **/ 337777ff853SJeremy L Thompson int CeedBasisSetData(CeedBasis basis, void *data) { 338777ff853SJeremy L Thompson basis->data = data; 339e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3407a982d89SJeremy L. Thompson } 3417a982d89SJeremy L. Thompson 3427a982d89SJeremy L. Thompson /** 34334359f16Sjeremylt @brief Increment the reference counter for a CeedBasis 34434359f16Sjeremylt 345ea61e9acSJeremy L Thompson @param[in,out] basis Basis to increment the reference counter 34634359f16Sjeremylt 34734359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 34834359f16Sjeremylt 34934359f16Sjeremylt @ref Backend 35034359f16Sjeremylt **/ 3519560d06aSjeremylt int CeedBasisReference(CeedBasis basis) { 35234359f16Sjeremylt basis->ref_count++; 35334359f16Sjeremylt return CEED_ERROR_SUCCESS; 35434359f16Sjeremylt } 35534359f16Sjeremylt 35634359f16Sjeremylt /** 357c4e3f59bSSebastian Grimberg @brief Get number of Q-vector components for given CeedBasis 358c4e3f59bSSebastian Grimberg 359c4e3f59bSSebastian Grimberg @param[in] basis CeedBasis 360c4e3f59bSSebastian Grimberg @param[in] eval_mode \ref CEED_EVAL_INTERP to use interpolated values, 361c4e3f59bSSebastian Grimberg \ref CEED_EVAL_GRAD to use gradients, 362c4e3f59bSSebastian Grimberg \ref CEED_EVAL_DIV to use divergence, 363c4e3f59bSSebastian Grimberg \ref CEED_EVAL_CURL to use curl. 364c4e3f59bSSebastian Grimberg @param[out] q_comp Variable to store number of Q-vector components of basis 365c4e3f59bSSebastian Grimberg 366c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 367c4e3f59bSSebastian Grimberg 368c4e3f59bSSebastian Grimberg @ref Backend 369c4e3f59bSSebastian Grimberg **/ 370c4e3f59bSSebastian Grimberg int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode, CeedInt *q_comp) { 371c4e3f59bSSebastian Grimberg switch (eval_mode) { 372c4e3f59bSSebastian Grimberg case CEED_EVAL_INTERP: 373c4e3f59bSSebastian Grimberg *q_comp = (basis->fe_space == CEED_FE_SPACE_H1) ? 1 : basis->dim; 374c4e3f59bSSebastian Grimberg break; 375c4e3f59bSSebastian Grimberg case CEED_EVAL_GRAD: 376c4e3f59bSSebastian Grimberg *q_comp = basis->dim; 377c4e3f59bSSebastian Grimberg break; 378c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 379c4e3f59bSSebastian Grimberg *q_comp = 1; 380c4e3f59bSSebastian Grimberg break; 381c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 382c4e3f59bSSebastian Grimberg *q_comp = (basis->dim < 3) ? 1 : basis->dim; 383c4e3f59bSSebastian Grimberg break; 384c4e3f59bSSebastian Grimberg case CEED_EVAL_NONE: 385c4e3f59bSSebastian Grimberg case CEED_EVAL_WEIGHT: 386352a5e7cSSebastian Grimberg *q_comp = 1; 387c4e3f59bSSebastian Grimberg break; 388c4e3f59bSSebastian Grimberg } 389c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 390c4e3f59bSSebastian Grimberg } 391c4e3f59bSSebastian Grimberg 392c4e3f59bSSebastian Grimberg /** 3936e15d496SJeremy L Thompson @brief Estimate number of FLOPs required to apply CeedBasis in t_mode and eval_mode 3946e15d496SJeremy L Thompson 395ea61e9acSJeremy L Thompson @param[in] basis Basis to estimate FLOPs for 396ea61e9acSJeremy L Thompson @param[in] t_mode Apply basis or transpose 397ea61e9acSJeremy L Thompson @param[in] eval_mode Basis evaluation mode 398ea61e9acSJeremy L Thompson @param[out] flops Address of variable to hold FLOPs estimate 3996e15d496SJeremy L Thompson 4006e15d496SJeremy L Thompson @ref Backend 4016e15d496SJeremy L Thompson **/ 4022b730f8bSJeremy L Thompson int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedSize *flops) { 4036e15d496SJeremy L Thompson bool is_tensor; 4046e15d496SJeremy L Thompson 4052b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor)); 4066e15d496SJeremy L Thompson if (is_tensor) { 4076e15d496SJeremy L Thompson CeedInt dim, num_comp, P_1d, Q_1d; 4082b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 4092b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 4102b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 4112b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 4126e15d496SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 4132b730f8bSJeremy L Thompson P_1d = Q_1d; 4142b730f8bSJeremy L Thompson Q_1d = P_1d; 4156e15d496SJeremy L Thompson } 4166e15d496SJeremy L Thompson CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim - 1), post = 1; 4176e15d496SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 4186e15d496SJeremy L Thompson tensor_flops += 2 * pre * P_1d * post * Q_1d; 4196e15d496SJeremy L Thompson pre /= P_1d; 4206e15d496SJeremy L Thompson post *= Q_1d; 4216e15d496SJeremy L Thompson } 4226e15d496SJeremy L Thompson switch (eval_mode) { 4232b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 4242b730f8bSJeremy L Thompson *flops = 0; 4252b730f8bSJeremy L Thompson break; 4262b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 4272b730f8bSJeremy L Thompson *flops = tensor_flops; 4282b730f8bSJeremy L Thompson break; 4292b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 4302b730f8bSJeremy L Thompson *flops = tensor_flops * 2; 4312b730f8bSJeremy L Thompson break; 4326e15d496SJeremy L Thompson case CEED_EVAL_DIV: 4336e15d496SJeremy L Thompson case CEED_EVAL_CURL: 4346574a04fSJeremy L Thompson // LCOV_EXCL_START 4356574a04fSJeremy L Thompson return CeedError(basis->ceed, CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported", CeedEvalModes[eval_mode]); 4362b730f8bSJeremy L Thompson break; 4376e15d496SJeremy L Thompson // LCOV_EXCL_STOP 4382b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 4392b730f8bSJeremy L Thompson *flops = dim * CeedIntPow(Q_1d, dim); 4402b730f8bSJeremy L Thompson break; 4416e15d496SJeremy L Thompson } 4426e15d496SJeremy L Thompson } else { 443c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 4442b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 4452b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 446c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 4472b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 4482b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 4496e15d496SJeremy L Thompson switch (eval_mode) { 4502b730f8bSJeremy L Thompson case CEED_EVAL_NONE: 4512b730f8bSJeremy L Thompson *flops = 0; 4522b730f8bSJeremy L Thompson break; 4532b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 4542b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 4552b730f8bSJeremy L Thompson case CEED_EVAL_DIV: 4562b730f8bSJeremy L Thompson case CEED_EVAL_CURL: 457c4e3f59bSSebastian Grimberg *flops = num_nodes * num_qpts * num_comp * q_comp; 4582b730f8bSJeremy L Thompson break; 4592b730f8bSJeremy L Thompson case CEED_EVAL_WEIGHT: 4602b730f8bSJeremy L Thompson *flops = 0; 4612b730f8bSJeremy L Thompson break; 4626e15d496SJeremy L Thompson } 4636e15d496SJeremy L Thompson } 4646e15d496SJeremy L Thompson 4656e15d496SJeremy L Thompson return CEED_ERROR_SUCCESS; 4666e15d496SJeremy L Thompson } 4676e15d496SJeremy L Thompson 4686e15d496SJeremy L Thompson /** 469c4e3f59bSSebastian Grimberg @brief Get CeedFESpace for a CeedBasis 470c4e3f59bSSebastian Grimberg 471c4e3f59bSSebastian Grimberg @param[in] basis CeedBasis 472c4e3f59bSSebastian Grimberg @param[out] fe_space Variable to store CeedFESpace 473c4e3f59bSSebastian Grimberg 474c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 475c4e3f59bSSebastian Grimberg 476c4e3f59bSSebastian Grimberg @ref Backend 477c4e3f59bSSebastian Grimberg **/ 478c4e3f59bSSebastian Grimberg int CeedBasisGetFESpace(CeedBasis basis, CeedFESpace *fe_space) { 479c4e3f59bSSebastian Grimberg *fe_space = basis->fe_space; 480c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 481c4e3f59bSSebastian Grimberg } 482c4e3f59bSSebastian Grimberg 483c4e3f59bSSebastian Grimberg /** 4847a982d89SJeremy L. Thompson @brief Get dimension for given CeedElemTopology 4857a982d89SJeremy L. Thompson 486ea61e9acSJeremy L Thompson @param[in] topo CeedElemTopology 4877a982d89SJeremy L. Thompson @param[out] dim Variable to store dimension of topology 4887a982d89SJeremy L. Thompson 4897a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4907a982d89SJeremy L. Thompson 4917a982d89SJeremy L. Thompson @ref Backend 4927a982d89SJeremy L. Thompson **/ 4937a982d89SJeremy L. Thompson int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim) { 4947a982d89SJeremy L. Thompson *dim = (CeedInt)topo >> 16; 495e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4967a982d89SJeremy L. Thompson } 4977a982d89SJeremy L. Thompson 4987a982d89SJeremy L. Thompson /** 4997a982d89SJeremy L. Thompson @brief Get CeedTensorContract of a CeedBasis 5007a982d89SJeremy L. Thompson 501ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 5027a982d89SJeremy L. Thompson @param[out] contract Variable to store CeedTensorContract 5037a982d89SJeremy L. Thompson 5047a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5057a982d89SJeremy L. Thompson 5067a982d89SJeremy L. Thompson @ref Backend 5077a982d89SJeremy L. Thompson **/ 5087a982d89SJeremy L. Thompson int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract) { 5097a982d89SJeremy L. Thompson *contract = basis->contract; 510e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5117a982d89SJeremy L. Thompson } 5127a982d89SJeremy L. Thompson 5137a982d89SJeremy L. Thompson /** 5147a982d89SJeremy L. Thompson @brief Set CeedTensorContract of a CeedBasis 5157a982d89SJeremy L. Thompson 516ea61e9acSJeremy L Thompson @param[in,out] basis CeedBasis 517ea61e9acSJeremy L Thompson @param[in] contract CeedTensorContract to set 5187a982d89SJeremy L. Thompson 5197a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5207a982d89SJeremy L. Thompson 5217a982d89SJeremy L. Thompson @ref Backend 5227a982d89SJeremy L. Thompson **/ 52334359f16Sjeremylt int CeedBasisSetTensorContract(CeedBasis basis, CeedTensorContract contract) { 52434359f16Sjeremylt basis->contract = contract; 5252b730f8bSJeremy L Thompson CeedCall(CeedTensorContractReference(contract)); 526e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5277a982d89SJeremy L. Thompson } 5287a982d89SJeremy L. Thompson 5297a982d89SJeremy L. Thompson /** 5307a982d89SJeremy L. Thompson @brief Return a reference implementation of matrix multiplication C = A B. 531ba59ac12SSebastian Grimberg 532ba59ac12SSebastian Grimberg Note: This is a reference implementation for CPU CeedScalar pointers that is not intended for high performance. 5337a982d89SJeremy L. Thompson 534ea61e9acSJeremy L Thompson @param[in] ceed Ceed context for error handling 535d1d35e2fSjeremylt @param[in] mat_A Row-major matrix A 536d1d35e2fSjeremylt @param[in] mat_B Row-major matrix B 537d1d35e2fSjeremylt @param[out] mat_C Row-major output matrix C 538ea61e9acSJeremy L Thompson @param[in] m Number of rows of C 539ea61e9acSJeremy L Thompson @param[in] n Number of columns of C 540ea61e9acSJeremy L Thompson @param[in] kk Number of columns of A/rows of B 5417a982d89SJeremy L. Thompson 5427a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5437a982d89SJeremy L. Thompson 5447a982d89SJeremy L. Thompson @ref Utility 5457a982d89SJeremy L. Thompson **/ 5462b730f8bSJeremy L Thompson int CeedMatrixMatrixMultiply(Ceed ceed, const CeedScalar *mat_A, const CeedScalar *mat_B, CeedScalar *mat_C, CeedInt m, CeedInt n, CeedInt kk) { 5472b730f8bSJeremy L Thompson for (CeedInt i = 0; i < m; i++) { 5487a982d89SJeremy L. Thompson for (CeedInt j = 0; j < n; j++) { 5497a982d89SJeremy L. Thompson CeedScalar sum = 0; 5502b730f8bSJeremy L Thompson for (CeedInt k = 0; k < kk; k++) sum += mat_A[k + i * kk] * mat_B[j + k * n]; 551d1d35e2fSjeremylt mat_C[j + i * n] = sum; 5527a982d89SJeremy L. Thompson } 5532b730f8bSJeremy L Thompson } 554e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5557a982d89SJeremy L. Thompson } 5567a982d89SJeremy L. Thompson 557ba59ac12SSebastian Grimberg /** 558ba59ac12SSebastian Grimberg @brief Return QR Factorization of a matrix 559ba59ac12SSebastian Grimberg 560ba59ac12SSebastian Grimberg @param[in] ceed Ceed context for error handling 561ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 562ba59ac12SSebastian Grimberg @param[in,out] tau Vector of length m of scaling factors 563ba59ac12SSebastian Grimberg @param[in] m Number of rows 564ba59ac12SSebastian Grimberg @param[in] n Number of columns 565ba59ac12SSebastian Grimberg 566ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 567ba59ac12SSebastian Grimberg 568ba59ac12SSebastian Grimberg @ref Utility 569ba59ac12SSebastian Grimberg **/ 570ba59ac12SSebastian Grimberg int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau, CeedInt m, CeedInt n) { 571ba59ac12SSebastian Grimberg CeedScalar v[m]; 572ba59ac12SSebastian Grimberg 573ba59ac12SSebastian Grimberg // Check matrix shape 5746574a04fSJeremy L Thompson CeedCheck(n <= m, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute QR factorization with n > m"); 575ba59ac12SSebastian Grimberg 576ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 577ba59ac12SSebastian Grimberg if (i >= m - 1) { // last row of matrix, no reflection needed 578ba59ac12SSebastian Grimberg tau[i] = 0.; 579ba59ac12SSebastian Grimberg break; 580ba59ac12SSebastian Grimberg } 581ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 582ba59ac12SSebastian Grimberg CeedScalar sigma = 0.0; 583ba59ac12SSebastian Grimberg v[i] = mat[i + n * i]; 584ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) { 585ba59ac12SSebastian Grimberg v[j] = mat[i + n * j]; 586ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 587ba59ac12SSebastian Grimberg } 588ba59ac12SSebastian Grimberg CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:m] 589ba59ac12SSebastian Grimberg CeedScalar R_ii = -copysign(norm, v[i]); 590ba59ac12SSebastian Grimberg v[i] -= R_ii; 591ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 592ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 593ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 594ba59ac12SSebastian Grimberg tau[i] = 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 595ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] /= v[i]; 596ba59ac12SSebastian Grimberg 597ba59ac12SSebastian Grimberg // Apply Householder reflector to lower right panel 598ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[i * n + i + 1], &v[i], tau[i], m - i, n - i - 1, n, 1); 599ba59ac12SSebastian Grimberg // Save v 600ba59ac12SSebastian Grimberg mat[i + n * i] = R_ii; 601ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) mat[i + n * j] = v[j]; 602ba59ac12SSebastian Grimberg } 603ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 604ba59ac12SSebastian Grimberg } 605ba59ac12SSebastian Grimberg 606ba59ac12SSebastian Grimberg /** 607ba59ac12SSebastian Grimberg @brief Apply Householder Q matrix 608ba59ac12SSebastian Grimberg 609ba59ac12SSebastian Grimberg Compute mat_A = mat_Q mat_A, where mat_Q is mxm and mat_A is mxn. 610ba59ac12SSebastian Grimberg 611ba59ac12SSebastian Grimberg @param[in,out] mat_A Matrix to apply Householder Q to, in place 612ba59ac12SSebastian Grimberg @param[in] mat_Q Householder Q matrix 613ba59ac12SSebastian Grimberg @param[in] tau Householder scaling factors 614ba59ac12SSebastian Grimberg @param[in] t_mode Transpose mode for application 615ba59ac12SSebastian Grimberg @param[in] m Number of rows in A 616ba59ac12SSebastian Grimberg @param[in] n Number of columns in A 617ba59ac12SSebastian Grimberg @param[in] k Number of elementary reflectors in Q, k<m 618ba59ac12SSebastian Grimberg @param[in] row Row stride in A 619ba59ac12SSebastian Grimberg @param[in] col Col stride in A 620ba59ac12SSebastian Grimberg 621ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 622ba59ac12SSebastian Grimberg 623c4e3f59bSSebastian Grimberg @ref Utility 624ba59ac12SSebastian Grimberg **/ 625ba59ac12SSebastian Grimberg int CeedHouseholderApplyQ(CeedScalar *mat_A, const CeedScalar *mat_Q, const CeedScalar *tau, CeedTransposeMode t_mode, CeedInt m, CeedInt n, 626ba59ac12SSebastian Grimberg CeedInt k, CeedInt row, CeedInt col) { 627ba59ac12SSebastian Grimberg CeedScalar *v; 628ba59ac12SSebastian Grimberg CeedCall(CeedMalloc(m, &v)); 629ba59ac12SSebastian Grimberg for (CeedInt ii = 0; ii < k; ii++) { 630ba59ac12SSebastian Grimberg CeedInt i = t_mode == CEED_TRANSPOSE ? ii : k - 1 - ii; 631ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < m; j++) v[j] = mat_Q[j * k + i]; 632ba59ac12SSebastian Grimberg // Apply Householder reflector (I - tau v v^T) collo_grad_1d^T 633ba59ac12SSebastian Grimberg CeedCall(CeedHouseholderReflect(&mat_A[i * row], &v[i], tau[i], m - i, n, row, col)); 634ba59ac12SSebastian Grimberg } 635ba59ac12SSebastian Grimberg CeedCall(CeedFree(&v)); 636ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 637ba59ac12SSebastian Grimberg } 638ba59ac12SSebastian Grimberg 639ba59ac12SSebastian Grimberg /** 640ba59ac12SSebastian Grimberg @brief Return symmetric Schur decomposition of the symmetric matrix mat via symmetric QR factorization 641ba59ac12SSebastian Grimberg 642ba59ac12SSebastian Grimberg @param[in] ceed Ceed context for error handling 643ba59ac12SSebastian Grimberg @param[in,out] mat Row-major matrix to be factorized in place 644ba59ac12SSebastian Grimberg @param[out] lambda Vector of length n of eigenvalues 645ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 646ba59ac12SSebastian Grimberg 647ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 648ba59ac12SSebastian Grimberg 649ba59ac12SSebastian Grimberg @ref Utility 650ba59ac12SSebastian Grimberg **/ 6512c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 6522c2ea1dbSJeremy L Thompson int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat, CeedScalar *lambda, CeedInt n) { 653ba59ac12SSebastian Grimberg // Check bounds for clang-tidy 6546574a04fSJeremy L Thompson CeedCheck(n > 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute symmetric Schur decomposition of scalars"); 655ba59ac12SSebastian Grimberg 656ba59ac12SSebastian Grimberg CeedScalar v[n - 1], tau[n - 1], mat_T[n * n]; 657ba59ac12SSebastian Grimberg 658ba59ac12SSebastian Grimberg // Copy mat to mat_T and set mat to I 659ba59ac12SSebastian Grimberg memcpy(mat_T, mat, n * n * sizeof(mat[0])); 660ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 661ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) mat[j + n * i] = (i == j) ? 1 : 0; 662ba59ac12SSebastian Grimberg } 663ba59ac12SSebastian Grimberg 664ba59ac12SSebastian Grimberg // Reduce to tridiagonal 665ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - 1; i++) { 666ba59ac12SSebastian Grimberg // Calculate Householder vector, magnitude 667ba59ac12SSebastian Grimberg CeedScalar sigma = 0.0; 668ba59ac12SSebastian Grimberg v[i] = mat_T[i + n * (i + 1)]; 669ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 670ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 671ba59ac12SSebastian Grimberg sigma += v[j] * v[j]; 672ba59ac12SSebastian Grimberg } 673ba59ac12SSebastian Grimberg CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:n-1] 674ba59ac12SSebastian Grimberg CeedScalar R_ii = -copysign(norm, v[i]); 675ba59ac12SSebastian Grimberg v[i] -= R_ii; 676ba59ac12SSebastian Grimberg // norm of v[i:m] after modification above and scaling below 677ba59ac12SSebastian Grimberg // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 678ba59ac12SSebastian Grimberg // tau = 2 / (norm*norm) 679ba59ac12SSebastian Grimberg tau[i] = i == n - 2 ? 2 : 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 680ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) v[j] /= v[i]; 681ba59ac12SSebastian Grimberg 682ba59ac12SSebastian Grimberg // Update sub and super diagonal 683ba59ac12SSebastian Grimberg for (CeedInt j = i + 2; j < n; j++) { 684ba59ac12SSebastian Grimberg mat_T[i + n * j] = 0; 685ba59ac12SSebastian Grimberg mat_T[j + n * i] = 0; 686ba59ac12SSebastian Grimberg } 687ba59ac12SSebastian Grimberg // Apply symmetric Householder reflector to lower right panel 688ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 689ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), 1, n); 690ba59ac12SSebastian Grimberg 691ba59ac12SSebastian Grimberg // Save v 692ba59ac12SSebastian Grimberg mat_T[i + n * (i + 1)] = R_ii; 693ba59ac12SSebastian Grimberg mat_T[(i + 1) + n * i] = R_ii; 694ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 695ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = v[j]; 696ba59ac12SSebastian Grimberg } 697ba59ac12SSebastian Grimberg } 698ba59ac12SSebastian Grimberg // Backwards accumulation of Q 699ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 700ba59ac12SSebastian Grimberg if (tau[i] > 0.0) { 701ba59ac12SSebastian Grimberg v[i] = 1; 702ba59ac12SSebastian Grimberg for (CeedInt j = i + 1; j < n - 1; j++) { 703ba59ac12SSebastian Grimberg v[j] = mat_T[i + n * (j + 1)]; 704ba59ac12SSebastian Grimberg mat_T[i + n * (j + 1)] = 0; 705ba59ac12SSebastian Grimberg } 706ba59ac12SSebastian Grimberg CeedHouseholderReflect(&mat[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 707ba59ac12SSebastian Grimberg } 708ba59ac12SSebastian Grimberg } 709ba59ac12SSebastian Grimberg 710ba59ac12SSebastian Grimberg // Reduce sub and super diagonal 711ba59ac12SSebastian Grimberg CeedInt p = 0, q = 0, itr = 0, max_itr = n * n * n * n; 712ba59ac12SSebastian Grimberg CeedScalar tol = CEED_EPSILON; 713ba59ac12SSebastian Grimberg 714ba59ac12SSebastian Grimberg while (itr < max_itr) { 715ba59ac12SSebastian Grimberg // Update p, q, size of reduced portions of diagonal 716ba59ac12SSebastian Grimberg p = 0; 717ba59ac12SSebastian Grimberg q = 0; 718ba59ac12SSebastian Grimberg for (CeedInt i = n - 2; i >= 0; i--) { 719ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) q += 1; 720ba59ac12SSebastian Grimberg else break; 721ba59ac12SSebastian Grimberg } 722ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n - q - 1; i++) { 723ba59ac12SSebastian Grimberg if (fabs(mat_T[i + n * (i + 1)]) < tol) p += 1; 724ba59ac12SSebastian Grimberg else break; 725ba59ac12SSebastian Grimberg } 726ba59ac12SSebastian Grimberg if (q == n - 1) break; // Finished reducing 727ba59ac12SSebastian Grimberg 728ba59ac12SSebastian Grimberg // Reduce tridiagonal portion 729ba59ac12SSebastian Grimberg CeedScalar t_nn = mat_T[(n - 1 - q) + n * (n - 1 - q)], t_nnm1 = mat_T[(n - 2 - q) + n * (n - 1 - q)]; 730ba59ac12SSebastian Grimberg CeedScalar d = (mat_T[(n - 2 - q) + n * (n - 2 - q)] - t_nn) / 2; 731ba59ac12SSebastian Grimberg CeedScalar mu = t_nn - t_nnm1 * t_nnm1 / (d + copysign(sqrt(d * d + t_nnm1 * t_nnm1), d)); 732ba59ac12SSebastian Grimberg CeedScalar x = mat_T[p + n * p] - mu; 733ba59ac12SSebastian Grimberg CeedScalar z = mat_T[p + n * (p + 1)]; 734ba59ac12SSebastian Grimberg for (CeedInt k = p; k < n - q - 1; k++) { 735ba59ac12SSebastian Grimberg // Compute Givens rotation 736ba59ac12SSebastian Grimberg CeedScalar c = 1, s = 0; 737ba59ac12SSebastian Grimberg if (fabs(z) > tol) { 738ba59ac12SSebastian Grimberg if (fabs(z) > fabs(x)) { 739ba59ac12SSebastian Grimberg CeedScalar tau = -x / z; 740ba59ac12SSebastian Grimberg s = 1 / sqrt(1 + tau * tau), c = s * tau; 741ba59ac12SSebastian Grimberg } else { 742ba59ac12SSebastian Grimberg CeedScalar tau = -z / x; 743ba59ac12SSebastian Grimberg c = 1 / sqrt(1 + tau * tau), s = c * tau; 744ba59ac12SSebastian Grimberg } 745ba59ac12SSebastian Grimberg } 746ba59ac12SSebastian Grimberg 747ba59ac12SSebastian Grimberg // Apply Givens rotation to T 748ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 749ba59ac12SSebastian Grimberg CeedGivensRotation(mat_T, c, s, CEED_TRANSPOSE, k, k + 1, n, n); 750ba59ac12SSebastian Grimberg 751ba59ac12SSebastian Grimberg // Apply Givens rotation to Q 752ba59ac12SSebastian Grimberg CeedGivensRotation(mat, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 753ba59ac12SSebastian Grimberg 754ba59ac12SSebastian Grimberg // Update x, z 755ba59ac12SSebastian Grimberg if (k < n - q - 2) { 756ba59ac12SSebastian Grimberg x = mat_T[k + n * (k + 1)]; 757ba59ac12SSebastian Grimberg z = mat_T[k + n * (k + 2)]; 758ba59ac12SSebastian Grimberg } 759ba59ac12SSebastian Grimberg } 760ba59ac12SSebastian Grimberg itr++; 761ba59ac12SSebastian Grimberg } 762ba59ac12SSebastian Grimberg 763ba59ac12SSebastian Grimberg // Save eigenvalues 764ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) lambda[i] = mat_T[i + n * i]; 765ba59ac12SSebastian Grimberg 766ba59ac12SSebastian Grimberg // Check convergence 7676574a04fSJeremy L Thompson CeedCheck(itr < max_itr || q > n, ceed, CEED_ERROR_MINOR, "Symmetric QR failed to converge"); 768ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 769ba59ac12SSebastian Grimberg } 7702c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 771ba59ac12SSebastian Grimberg 772ba59ac12SSebastian Grimberg /** 773ba59ac12SSebastian Grimberg @brief Return Simultaneous Diagonalization of two matrices. 774ba59ac12SSebastian Grimberg 775ba59ac12SSebastian Grimberg This solves the generalized eigenvalue problem A x = lambda B x, where A and B are symmetric and B is positive definite. 776ba59ac12SSebastian Grimberg We generate the matrix X and vector Lambda such that X^T A X = Lambda and X^T B X = I. 777ba59ac12SSebastian Grimberg This is equivalent to the LAPACK routine 'sygv' with TYPE = 1. 778ba59ac12SSebastian Grimberg 779ba59ac12SSebastian Grimberg @param[in] ceed Ceed context for error handling 780ba59ac12SSebastian Grimberg @param[in] mat_A Row-major matrix to be factorized with eigenvalues 781ba59ac12SSebastian Grimberg @param[in] mat_B Row-major matrix to be factorized to identity 782ba59ac12SSebastian Grimberg @param[out] mat_X Row-major orthogonal matrix 783ba59ac12SSebastian Grimberg @param[out] lambda Vector of length n of generalized eigenvalues 784ba59ac12SSebastian Grimberg @param[in] n Number of rows/columns 785ba59ac12SSebastian Grimberg 786ba59ac12SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 787ba59ac12SSebastian Grimberg 788ba59ac12SSebastian Grimberg @ref Utility 789ba59ac12SSebastian Grimberg **/ 7902c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 7912c2ea1dbSJeremy L Thompson int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A, CeedScalar *mat_B, CeedScalar *mat_X, CeedScalar *lambda, CeedInt n) { 792ba59ac12SSebastian Grimberg CeedScalar *mat_C, *mat_G, *vec_D; 793ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_C)); 794ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n * n, &mat_G)); 795ba59ac12SSebastian Grimberg CeedCall(CeedCalloc(n, &vec_D)); 796ba59ac12SSebastian Grimberg 797ba59ac12SSebastian Grimberg // Compute B = G D G^T 798ba59ac12SSebastian Grimberg memcpy(mat_G, mat_B, n * n * sizeof(mat_B[0])); 799ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_G, vec_D, n)); 800ba59ac12SSebastian Grimberg 801ba59ac12SSebastian Grimberg // Sort eigenvalues 802ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 803ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 804ba59ac12SSebastian Grimberg if (fabs(vec_D[j]) > fabs(vec_D[j + 1])) { 805ba59ac12SSebastian Grimberg CeedScalar temp; 806ba59ac12SSebastian Grimberg temp = vec_D[j]; 807ba59ac12SSebastian Grimberg vec_D[j] = vec_D[j + 1]; 808ba59ac12SSebastian Grimberg vec_D[j + 1] = temp; 809ba59ac12SSebastian Grimberg for (CeedInt k = 0; k < n; k++) { 810ba59ac12SSebastian Grimberg temp = mat_G[k * n + j]; 811ba59ac12SSebastian Grimberg mat_G[k * n + j] = mat_G[k * n + j + 1]; 812ba59ac12SSebastian Grimberg mat_G[k * n + j + 1] = temp; 813ba59ac12SSebastian Grimberg } 814ba59ac12SSebastian Grimberg } 815ba59ac12SSebastian Grimberg } 816ba59ac12SSebastian Grimberg } 817ba59ac12SSebastian Grimberg 818ba59ac12SSebastian Grimberg // Compute C = (G D^1/2)^-1 A (G D^1/2)^-T 819ba59ac12SSebastian Grimberg // = D^-1/2 G^T A G D^-1/2 820ba59ac12SSebastian Grimberg // -- D = D^-1/2 821ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) vec_D[i] = 1. / sqrt(vec_D[i]); 822ba59ac12SSebastian Grimberg // -- G = G D^-1/2 823ba59ac12SSebastian Grimberg // -- C = D^-1/2 G^T 824ba59ac12SSebastian Grimberg for (CeedInt i = 0; i < n; i++) { 825ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < n; j++) { 826ba59ac12SSebastian Grimberg mat_G[i * n + j] *= vec_D[j]; 827ba59ac12SSebastian Grimberg mat_C[j * n + i] = mat_G[i * n + j]; 828ba59ac12SSebastian Grimberg } 829ba59ac12SSebastian Grimberg } 830ba59ac12SSebastian Grimberg // -- X = (D^-1/2 G^T) A 831ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_C, (const CeedScalar *)mat_A, mat_X, n, n, n)); 832ba59ac12SSebastian Grimberg // -- C = (D^-1/2 G^T A) (G D^-1/2) 833ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_X, (const CeedScalar *)mat_G, mat_C, n, n, n)); 834ba59ac12SSebastian Grimberg 835ba59ac12SSebastian Grimberg // Compute Q^T C Q = lambda 836ba59ac12SSebastian Grimberg CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_C, lambda, n)); 837ba59ac12SSebastian Grimberg 838ba59ac12SSebastian Grimberg // Sort eigenvalues 839ba59ac12SSebastian Grimberg for (CeedInt i = n - 1; i >= 0; i--) { 840ba59ac12SSebastian Grimberg for (CeedInt j = 0; j < i; j++) { 841ba59ac12SSebastian Grimberg if (fabs(lambda[j]) > fabs(lambda[j + 1])) { 842ba59ac12SSebastian Grimberg CeedScalar temp; 843ba59ac12SSebastian Grimberg temp = lambda[j]; 844ba59ac12SSebastian Grimberg lambda[j] = lambda[j + 1]; 845ba59ac12SSebastian Grimberg lambda[j + 1] = temp; 846ba59ac12SSebastian Grimberg for (CeedInt k = 0; k < n; k++) { 847ba59ac12SSebastian Grimberg temp = mat_C[k * n + j]; 848ba59ac12SSebastian Grimberg mat_C[k * n + j] = mat_C[k * n + j + 1]; 849ba59ac12SSebastian Grimberg mat_C[k * n + j + 1] = temp; 850ba59ac12SSebastian Grimberg } 851ba59ac12SSebastian Grimberg } 852ba59ac12SSebastian Grimberg } 853ba59ac12SSebastian Grimberg } 854ba59ac12SSebastian Grimberg 855ba59ac12SSebastian Grimberg // Set X = (G D^1/2)^-T Q 856ba59ac12SSebastian Grimberg // = G D^-1/2 Q 857ba59ac12SSebastian Grimberg CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_G, (const CeedScalar *)mat_C, mat_X, n, n, n)); 858ba59ac12SSebastian Grimberg 859ba59ac12SSebastian Grimberg // Cleanup 860ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_C)); 861ba59ac12SSebastian Grimberg CeedCall(CeedFree(&mat_G)); 862ba59ac12SSebastian Grimberg CeedCall(CeedFree(&vec_D)); 863ba59ac12SSebastian Grimberg return CEED_ERROR_SUCCESS; 864ba59ac12SSebastian Grimberg } 8652c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 866ba59ac12SSebastian Grimberg 8677a982d89SJeremy L. Thompson /// @} 8687a982d89SJeremy L. Thompson 8697a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 8707a982d89SJeremy L. Thompson /// CeedBasis Public API 8717a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 8727a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser 873d7b241e6Sjeremylt /// @{ 874d7b241e6Sjeremylt 875b11c1e72Sjeremylt /** 876ba59ac12SSebastian Grimberg @brief Create a tensor-product basis for H^1 discretizations 877b11c1e72Sjeremylt 878ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedBasis will be created 879ea61e9acSJeremy L Thompson @param[in] dim Topological dimension 880ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 881ea61e9acSJeremy L Thompson @param[in] P_1d Number of nodes in one dimension 882ea61e9acSJeremy L Thompson @param[in] Q_1d Number of quadrature points in one dimension 883ea61e9acSJeremy L Thompson @param[in] interp_1d Row-major (Q_1d * P_1d) matrix expressing the values of nodal basis functions at quadrature points 884ea61e9acSJeremy L Thompson @param[in] grad_1d Row-major (Q_1d * P_1d) matrix expressing derivatives of nodal basis functions at quadrature points 885ea61e9acSJeremy 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] 886ea61e9acSJeremy L Thompson @param[in] q_weight_1d Array of length Q_1d holding the quadrature weights on the reference element 887ea61e9acSJeremy L Thompson @param[out] basis Address of the variable where the newly created CeedBasis will be stored. 888b11c1e72Sjeremylt 889b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 890dfdf5a53Sjeremylt 8917a982d89SJeremy L. Thompson @ref User 892b11c1e72Sjeremylt **/ 8932b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, 8942b730f8bSJeremy L Thompson const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis *basis) { 8955fe0d4faSjeremylt if (!ceed->BasisCreateTensorH1) { 8965fe0d4faSjeremylt Ceed delegate; 8976574a04fSJeremy L Thompson 8982b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 8996574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisCreateTensorH1"); 9002b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 901e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9025fe0d4faSjeremylt } 903e15f9bd0SJeremy L Thompson 9046574a04fSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "Basis dimension must be a positive value"); 9056574a04fSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component"); 9066574a04fSJeremy L Thompson CeedCheck(P_1d > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node"); 9076574a04fSJeremy L Thompson CeedCheck(Q_1d > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point"); 908227444bfSJeremy L Thompson 9092b730f8bSJeremy L Thompson CeedElemTopology topo = dim == 1 ? CEED_TOPOLOGY_LINE : dim == 2 ? CEED_TOPOLOGY_QUAD : CEED_TOPOLOGY_HEX; 910e15f9bd0SJeremy L Thompson 9112b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 912*db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 913d1d35e2fSjeremylt (*basis)->ref_count = 1; 9146402da51SJeremy L Thompson (*basis)->is_tensor_basis = true; 915d7b241e6Sjeremylt (*basis)->dim = dim; 916d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 917d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 918d1d35e2fSjeremylt (*basis)->P_1d = P_1d; 919d1d35e2fSjeremylt (*basis)->Q_1d = Q_1d; 920d1d35e2fSjeremylt (*basis)->P = CeedIntPow(P_1d, dim); 921d1d35e2fSjeremylt (*basis)->Q = CeedIntPow(Q_1d, dim); 922c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 9232b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_ref_1d)); 9242b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_weight_1d)); 925ff3a0f91SJeremy L Thompson if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d * sizeof(q_ref_1d[0])); 9262b730f8bSJeremy L Thompson if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, Q_1d * sizeof(q_weight_1d[0])); 9272b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->interp_1d)); 9282b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->grad_1d)); 9292b730f8bSJeremy L Thompson if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, Q_1d * P_1d * sizeof(interp_1d[0])); 930ff3a0f91SJeremy L Thompson if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d * P_1d * sizeof(grad_1d[0])); 9312b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, *basis)); 932e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 933d7b241e6Sjeremylt } 934d7b241e6Sjeremylt 935b11c1e72Sjeremylt /** 93695bb1877Svaleriabarra @brief Create a tensor-product Lagrange basis 937b11c1e72Sjeremylt 938ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedBasis will be created 939ea61e9acSJeremy L Thompson @param[in] dim Topological dimension of element 940ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 941ea61e9acSJeremy L Thompson @param[in] P Number of Gauss-Lobatto nodes in one dimension. 942ea61e9acSJeremy L Thompson The polynomial degree of the resulting Q_k element is k=P-1. 943ea61e9acSJeremy L Thompson @param[in] Q Number of quadrature points in one dimension. 944ea61e9acSJeremy L Thompson @param[in] quad_mode Distribution of the Q quadrature points (affects order of accuracy for the quadrature) 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 CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis) { 952d7b241e6Sjeremylt // Allocate 9532b730f8bSJeremy L Thompson int ierr = CEED_ERROR_SUCCESS, i, j, k; 9542b730f8bSJeremy L Thompson CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, *q_weight_1d; 9554d537eeaSYohann 9566574a04fSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "Basis dimension must be a positive value"); 9576574a04fSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component"); 9586574a04fSJeremy L Thompson CeedCheck(P > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node"); 9596574a04fSJeremy L Thompson CeedCheck(Q > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point"); 960227444bfSJeremy L Thompson 961e15f9bd0SJeremy L Thompson // Get Nodes and Weights 9622b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &interp_1d)); 9632b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &grad_1d)); 9642b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P, &nodes)); 9652b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_ref_1d)); 9662b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_weight_1d)); 9672b730f8bSJeremy L Thompson if (CeedLobattoQuadrature(P, nodes, NULL) != CEED_ERROR_SUCCESS) goto cleanup; 968d1d35e2fSjeremylt switch (quad_mode) { 969d7b241e6Sjeremylt case CEED_GAUSS: 970d1d35e2fSjeremylt ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d); 971d7b241e6Sjeremylt break; 972d7b241e6Sjeremylt case CEED_GAUSS_LOBATTO: 973d1d35e2fSjeremylt ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d); 974d7b241e6Sjeremylt break; 975d7b241e6Sjeremylt } 9762b730f8bSJeremy L Thompson if (ierr != CEED_ERROR_SUCCESS) goto cleanup; 977e15f9bd0SJeremy L Thompson 978d7b241e6Sjeremylt // Build B, D matrix 979d7b241e6Sjeremylt // Fornberg, 1998 980d7b241e6Sjeremylt for (i = 0; i < Q; i++) { 981d7b241e6Sjeremylt c1 = 1.0; 982d1d35e2fSjeremylt c3 = nodes[0] - q_ref_1d[i]; 983d1d35e2fSjeremylt interp_1d[i * P + 0] = 1.0; 984d7b241e6Sjeremylt for (j = 1; j < P; j++) { 985d7b241e6Sjeremylt c2 = 1.0; 986d7b241e6Sjeremylt c4 = c3; 987d1d35e2fSjeremylt c3 = nodes[j] - q_ref_1d[i]; 988d7b241e6Sjeremylt for (k = 0; k < j; k++) { 989d7b241e6Sjeremylt dx = nodes[j] - nodes[k]; 990d7b241e6Sjeremylt c2 *= dx; 991d7b241e6Sjeremylt if (k == j - 1) { 992d1d35e2fSjeremylt grad_1d[i * P + j] = c1 * (interp_1d[i * P + k] - c4 * grad_1d[i * P + k]) / c2; 993d1d35e2fSjeremylt interp_1d[i * P + j] = -c1 * c4 * interp_1d[i * P + k] / c2; 994d7b241e6Sjeremylt } 995d1d35e2fSjeremylt grad_1d[i * P + k] = (c3 * grad_1d[i * P + k] - interp_1d[i * P + k]) / dx; 996d1d35e2fSjeremylt interp_1d[i * P + k] = c3 * interp_1d[i * P + k] / dx; 997d7b241e6Sjeremylt } 998d7b241e6Sjeremylt c1 = c2; 999d7b241e6Sjeremylt } 1000d7b241e6Sjeremylt } 10019ac7b42eSJeremy L Thompson // Pass to CeedBasisCreateTensorH1 10022b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 1003e15f9bd0SJeremy L Thompson cleanup: 10042b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_1d)); 10052b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_1d)); 10062b730f8bSJeremy L Thompson CeedCall(CeedFree(&nodes)); 10072b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref_1d)); 10082b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight_1d)); 1009e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1010d7b241e6Sjeremylt } 1011d7b241e6Sjeremylt 1012b11c1e72Sjeremylt /** 1013ba59ac12SSebastian Grimberg @brief Create a non tensor-product basis for H^1 discretizations 1014a8de75f0Sjeremylt 1015ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedBasis will be created 1016ea61e9acSJeremy L Thompson @param[in] topo Topology of element, e.g. hypercube, simplex, ect 1017ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1018ea61e9acSJeremy L Thompson @param[in] num_nodes Total number of nodes 1019ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1020ea61e9acSJeremy L Thompson @param[in] interp Row-major (num_qpts * num_nodes) matrix expressing the values of nodal basis functions at quadrature points 1021c4e3f59bSSebastian Grimberg @param[in] grad Row-major (dim * num_qpts * num_nodes) matrix expressing derivatives of nodal basis functions at quadrature points 10229fe083eeSJeremy L Thompson @param[in] q_ref Array of length num_qpts * dim holding the locations of quadrature points on the reference element 1023ea61e9acSJeremy L Thompson @param[in] q_weight Array of length num_qpts holding the quadrature weights on the reference element 1024ea61e9acSJeremy L Thompson @param[out] basis Address of the variable where the newly created CeedBasis will be stored. 1025a8de75f0Sjeremylt 1026a8de75f0Sjeremylt @return An error code: 0 - success, otherwise - failure 1027a8de75f0Sjeremylt 10287a982d89SJeremy L. Thompson @ref User 1029a8de75f0Sjeremylt **/ 10302b730f8bSJeremy L Thompson int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 10312b730f8bSJeremy L Thompson const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1032d1d35e2fSjeremylt CeedInt P = num_nodes, Q = num_qpts, dim = 0; 1033a8de75f0Sjeremylt 10345fe0d4faSjeremylt if (!ceed->BasisCreateH1) { 10355fe0d4faSjeremylt Ceed delegate; 10366574a04fSJeremy L Thompson 10372b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 10386574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisCreateH1"); 10392b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis)); 1040e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10415fe0d4faSjeremylt } 10425fe0d4faSjeremylt 10436574a04fSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component"); 10446574a04fSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node"); 10456574a04fSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point"); 1046227444bfSJeremy L Thompson 10472b730f8bSJeremy L Thompson CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1048a8de75f0Sjeremylt 1049*db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1050*db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1051d1d35e2fSjeremylt (*basis)->ref_count = 1; 10526402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1053a8de75f0Sjeremylt (*basis)->dim = dim; 1054d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 1055d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 1056a8de75f0Sjeremylt (*basis)->P = P; 1057a8de75f0Sjeremylt (*basis)->Q = Q; 1058c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 10592b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * dim, &(*basis)->q_ref_1d)); 10602b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &(*basis)->q_weight_1d)); 1061ff3a0f91SJeremy L Thompson if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1062ff3a0f91SJeremy L Thompson if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 10632b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * P, &(*basis)->interp)); 10642b730f8bSJeremy L Thompson CeedCall(CeedCalloc(dim * Q * P, &(*basis)->grad)); 1065ff3a0f91SJeremy L Thompson if (interp) memcpy((*basis)->interp, interp, Q * P * sizeof(interp[0])); 1066ff3a0f91SJeremy L Thompson if (grad) memcpy((*basis)->grad, grad, dim * Q * P * sizeof(grad[0])); 10672b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, *basis)); 1068e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1069a8de75f0Sjeremylt } 1070a8de75f0Sjeremylt 1071a8de75f0Sjeremylt /** 1072859c15bbSJames Wright @brief Create a non tensor-product basis for \f$H(\mathrm{div})\f$ discretizations 107350c301a5SRezgar Shakeri 1074ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedBasis will be created 1075ea61e9acSJeremy 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 1076ea61e9acSJeremy L Thompson @param[in] num_comp Number of components (usually 1 for vectors in H(div) bases) 1077ea61e9acSJeremy L Thompson @param[in] num_nodes Total number of nodes (dofs per element) 1078ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1079c4e3f59bSSebastian Grimberg @param[in] interp Row-major (dim * num_qpts * num_nodes) matrix expressing the values of basis functions at quadrature points 1080c4e3f59bSSebastian Grimberg @param[in] div Row-major (num_qpts * num_nodes) matrix expressing divergence of basis functions at quadrature points 10819fe083eeSJeremy L Thompson @param[in] q_ref Array of length num_qpts * dim holding the locations of quadrature points on the reference element 1082ea61e9acSJeremy L Thompson @param[in] q_weight Array of length num_qpts holding the quadrature weights on the reference element 1083ea61e9acSJeremy L Thompson @param[out] basis Address of the variable where the newly created CeedBasis will be stored. 108450c301a5SRezgar Shakeri 108550c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 108650c301a5SRezgar Shakeri 108750c301a5SRezgar Shakeri @ref User 108850c301a5SRezgar Shakeri **/ 10892b730f8bSJeremy L Thompson int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 10902b730f8bSJeremy L Thompson const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 109150c301a5SRezgar Shakeri CeedInt Q = num_qpts, P = num_nodes, dim = 0; 1092c4e3f59bSSebastian Grimberg 109350c301a5SRezgar Shakeri if (!ceed->BasisCreateHdiv) { 109450c301a5SRezgar Shakeri Ceed delegate; 10956574a04fSJeremy L Thompson 10962b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 10976574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv"); 10982b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis)); 109950c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 110050c301a5SRezgar Shakeri } 110150c301a5SRezgar Shakeri 11026574a04fSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component"); 11036574a04fSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node"); 11046574a04fSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point"); 1105227444bfSJeremy L Thompson 1106c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1107c4e3f59bSSebastian Grimberg 1108*db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1109*db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 111050c301a5SRezgar Shakeri (*basis)->ref_count = 1; 11116402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 111250c301a5SRezgar Shakeri (*basis)->dim = dim; 111350c301a5SRezgar Shakeri (*basis)->topo = topo; 111450c301a5SRezgar Shakeri (*basis)->num_comp = num_comp; 111550c301a5SRezgar Shakeri (*basis)->P = P; 111650c301a5SRezgar Shakeri (*basis)->Q = Q; 1117c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HDIV; 11182b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 11192b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 112050c301a5SRezgar Shakeri if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 112150c301a5SRezgar Shakeri if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 11222b730f8bSJeremy L Thompson CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 11232b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * P, &(*basis)->div)); 112450c301a5SRezgar Shakeri if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 112550c301a5SRezgar Shakeri if (div) memcpy((*basis)->div, div, Q * P * sizeof(div[0])); 11262b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, q_weight, *basis)); 112750c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 112850c301a5SRezgar Shakeri } 112950c301a5SRezgar Shakeri 113050c301a5SRezgar Shakeri /** 11314385fb7fSSebastian Grimberg @brief Create a non tensor-product basis for \f$H(\mathrm{curl})\f$ discretizations 1132c4e3f59bSSebastian Grimberg 1133c4e3f59bSSebastian Grimberg @param[in] ceed Ceed object where the CeedBasis will be created 1134c4e3f59bSSebastian Grimberg @param[in] topo Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below 1135c4e3f59bSSebastian Grimberg @param[in] num_comp Number of components (usually 1 for vectors in H(curl) bases) 1136c4e3f59bSSebastian Grimberg @param[in] num_nodes Total number of nodes (dofs per element) 1137c4e3f59bSSebastian Grimberg @param[in] num_qpts Total number of quadrature points 1138c4e3f59bSSebastian Grimberg @param[in] interp Row-major (dim * num_qpts * num_nodes) matrix expressing the values of basis functions at quadrature points 1139c4e3f59bSSebastian 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 1140c4e3f59bSSebastian Grimberg quadrature points 1141c4e3f59bSSebastian Grimberg @param[in] q_ref Array of length num_qpts * dim holding the locations of quadrature points on the reference element 1142c4e3f59bSSebastian Grimberg @param[in] q_weight Array of length num_qpts holding the quadrature weights on the reference element 1143c4e3f59bSSebastian Grimberg @param[out] basis Address of the variable where the newly created CeedBasis will be stored. 1144c4e3f59bSSebastian Grimberg 1145c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1146c4e3f59bSSebastian Grimberg 1147c4e3f59bSSebastian Grimberg @ref User 1148c4e3f59bSSebastian Grimberg **/ 1149c4e3f59bSSebastian Grimberg int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 1150c4e3f59bSSebastian Grimberg const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1151c4e3f59bSSebastian Grimberg CeedInt Q = num_qpts, P = num_nodes, dim = 0, curl_comp = 0; 1152c4e3f59bSSebastian Grimberg 1153c4e3f59bSSebastian Grimberg if (!ceed->BasisCreateHdiv) { 1154c4e3f59bSSebastian Grimberg Ceed delegate; 11556574a04fSJeremy L Thompson 1156c4e3f59bSSebastian Grimberg CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 11576574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl"); 1158c4e3f59bSSebastian Grimberg CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis)); 1159c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1160c4e3f59bSSebastian Grimberg } 1161c4e3f59bSSebastian Grimberg 11626574a04fSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component"); 11636574a04fSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node"); 11646574a04fSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point"); 1165c4e3f59bSSebastian Grimberg 1166c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1167c4e3f59bSSebastian Grimberg curl_comp = (dim < 3) ? 1 : dim; 1168c4e3f59bSSebastian Grimberg 1169*db002c03SJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1170*db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1171c4e3f59bSSebastian Grimberg (*basis)->ref_count = 1; 11726402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1173c4e3f59bSSebastian Grimberg (*basis)->dim = dim; 1174c4e3f59bSSebastian Grimberg (*basis)->topo = topo; 1175c4e3f59bSSebastian Grimberg (*basis)->num_comp = num_comp; 1176c4e3f59bSSebastian Grimberg (*basis)->P = P; 1177c4e3f59bSSebastian Grimberg (*basis)->Q = Q; 1178c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HCURL; 1179c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 1180c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 1181c4e3f59bSSebastian Grimberg if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1182c4e3f59bSSebastian Grimberg if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 1183c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 1184c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(curl_comp * Q * P, &(*basis)->curl)); 1185c4e3f59bSSebastian Grimberg if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 1186c4e3f59bSSebastian Grimberg if (curl) memcpy((*basis)->curl, curl, curl_comp * Q * P * sizeof(curl[0])); 1187c4e3f59bSSebastian Grimberg CeedCall(ceed->BasisCreateHcurl(topo, dim, P, Q, interp, curl, q_ref, q_weight, *basis)); 1188c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1189c4e3f59bSSebastian Grimberg } 1190c4e3f59bSSebastian Grimberg 1191c4e3f59bSSebastian Grimberg /** 1192ea61e9acSJeremy L Thompson @brief Create a CeedBasis for projection from the nodes of `basis_from` to the nodes of `basis_to`. 1193ba59ac12SSebastian Grimberg 11949fd66db6SSebastian Grimberg Only `CEED_EVAL_INTERP` will be valid for the new basis, `basis_project`. 11959fd66db6SSebastian Grimberg For H^1 spaces, `CEED_EVAL_GRAD` will also be valid. 1196de05fbb2SSebastian Grimberg The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR 11979fd66db6SSebastian Grimberg factorization. 11989fd66db6SSebastian Grimberg The gradient (for the H^1 case) is given by `grad_project = interp_to^+ * grad_from`. 119915ad3917SSebastian Grimberg 120015ad3917SSebastian Grimberg Note: `basis_from` and `basis_to` must have compatible quadrature spaces. 120115ad3917SSebastian Grimberg 12029fd66db6SSebastian Grimberg Note: `basis_project` will have the same number of components as `basis_from`, regardless of the number of components that `basis_to` has. 12039fd66db6SSebastian Grimberg If `basis_from` has 3 components and `basis_to` has 5 components, then `basis_project` will have 3 components. 1204f113e5dcSJeremy L Thompson 1205f113e5dcSJeremy L Thompson @param[in] basis_from CeedBasis to prolong from 1206446e7af4SJeremy L Thompson @param[in] basis_to CeedBasis to prolong to 1207ea61e9acSJeremy L Thompson @param[out] basis_project Address of the variable where the newly created CeedBasis will be stored. 1208f113e5dcSJeremy L Thompson 1209f113e5dcSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1210f113e5dcSJeremy L Thompson 1211f113e5dcSJeremy L Thompson @ref User 1212f113e5dcSJeremy L Thompson **/ 12132b730f8bSJeremy L Thompson int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project) { 1214f113e5dcSJeremy L Thompson Ceed ceed; 12152b730f8bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis_to, &ceed)); 1216f113e5dcSJeremy L Thompson 1217ecc88aebSJeremy L Thompson // Create projection matrix 121814556e63SJeremy L Thompson CeedScalar *interp_project, *grad_project; 12192b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateProjectionMatrices(basis_from, basis_to, &interp_project, &grad_project)); 1220f113e5dcSJeremy L Thompson 1221f113e5dcSJeremy L Thompson // Build basis 1222f113e5dcSJeremy L Thompson bool is_tensor; 1223f113e5dcSJeremy L Thompson CeedInt dim, num_comp; 122414556e63SJeremy L Thompson CeedScalar *q_ref, *q_weight; 12252b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis_to, &is_tensor)); 12262b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_to, &dim)); 12272b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_from, &num_comp)); 1228f113e5dcSJeremy L Thompson if (is_tensor) { 1229f113e5dcSJeremy L Thompson CeedInt P_1d_to, P_1d_from; 12302b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_1d_from)); 12312b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_1d_to)); 12322b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_to, &q_ref)); 12332b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_to, &q_weight)); 12342b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, interp_project, grad_project, q_ref, q_weight, basis_project)); 1235f113e5dcSJeremy L Thompson } else { 1236de05fbb2SSebastian Grimberg // Even if basis_to and basis_from are not H1, the resulting basis is H1 for interpolation to work 1237f113e5dcSJeremy L Thompson CeedElemTopology topo; 12382b730f8bSJeremy L Thompson CeedCall(CeedBasisGetTopology(basis_to, &topo)); 1239f113e5dcSJeremy L Thompson CeedInt num_nodes_to, num_nodes_from; 12402b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_from, &num_nodes_from)); 12412b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_to, &num_nodes_to)); 12422b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_to * dim, &q_ref)); 12432b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_to, &q_weight)); 12442b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, q_ref, q_weight, basis_project)); 1245f113e5dcSJeremy L Thompson } 1246f113e5dcSJeremy L Thompson 1247f113e5dcSJeremy L Thompson // Cleanup 12482b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_project)); 12492b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_project)); 12502b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref)); 12512b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight)); 1252f113e5dcSJeremy L Thompson 1253f113e5dcSJeremy L Thompson return CEED_ERROR_SUCCESS; 1254f113e5dcSJeremy L Thompson } 1255f113e5dcSJeremy L Thompson 1256f113e5dcSJeremy L Thompson /** 1257ea61e9acSJeremy L Thompson @brief Copy the pointer to a CeedBasis. 12589560d06aSjeremylt 1259512bb800SJeremy 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. 1260512bb800SJeremy L Thompson This CeedBasis will be destroyed if `basis_copy` is the only reference to this CeedBasis. 1261ea61e9acSJeremy L Thompson 1262ea61e9acSJeremy L Thompson @param[in] basis CeedBasis to copy reference to 1263ea61e9acSJeremy L Thompson @param[in,out] basis_copy Variable to store copied reference 12649560d06aSjeremylt 12659560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 12669560d06aSjeremylt 12679560d06aSjeremylt @ref User 12689560d06aSjeremylt **/ 12699560d06aSjeremylt int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) { 1270393ac2cdSJeremy L Thompson if (basis != CEED_BASIS_COLLOCATED) CeedCall(CeedBasisReference(basis)); 12712b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(basis_copy)); 12729560d06aSjeremylt *basis_copy = basis; 12739560d06aSjeremylt return CEED_ERROR_SUCCESS; 12749560d06aSjeremylt } 12759560d06aSjeremylt 12769560d06aSjeremylt /** 12777a982d89SJeremy L. Thompson @brief View a CeedBasis 12787a982d89SJeremy L. Thompson 1279ea61e9acSJeremy L Thompson @param[in] basis CeedBasis to view 1280ea61e9acSJeremy L Thompson @param[in] stream Stream to view to, e.g., stdout 12817a982d89SJeremy L. Thompson 12827a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 12837a982d89SJeremy L. Thompson 12847a982d89SJeremy L. Thompson @ref User 12857a982d89SJeremy L. Thompson **/ 12867a982d89SJeremy L. Thompson int CeedBasisView(CeedBasis basis, FILE *stream) { 128750c301a5SRezgar Shakeri CeedElemTopology topo = basis->topo; 1288c4e3f59bSSebastian Grimberg CeedFESpace fe_space = basis->fe_space; 1289c4e3f59bSSebastian Grimberg CeedInt q_comp = 0; 12902b730f8bSJeremy L Thompson 129150c301a5SRezgar Shakeri // Print FE space and element topology of the basis 12926402da51SJeremy L Thompson if (basis->is_tensor_basis) { 1293c4e3f59bSSebastian Grimberg fprintf(stream, "CeedBasis (%s on a %s element): dim=%" CeedInt_FMT " P=%" CeedInt_FMT " Q=%" CeedInt_FMT "\n", CeedFESpaces[fe_space], 12942b730f8bSJeremy L Thompson CeedElemTopologies[topo], basis->dim, basis->P_1d, basis->Q_1d); 129550c301a5SRezgar Shakeri } else { 1296c4e3f59bSSebastian Grimberg fprintf(stream, "CeedBasis (%s on a %s element): dim=%" CeedInt_FMT " P=%" CeedInt_FMT " Q=%" CeedInt_FMT "\n", CeedFESpaces[fe_space], 12972b730f8bSJeremy L Thompson CeedElemTopologies[topo], basis->dim, basis->P, basis->Q); 129850c301a5SRezgar Shakeri } 1299ea61e9acSJeremy L Thompson // Print quadrature data, interpolation/gradient/divergence/curl of the basis 13006402da51SJeremy L Thompson if (basis->is_tensor_basis) { // tensor basis 13012b730f8bSJeremy L Thompson CeedCall(CeedScalarView("qref1d", "\t% 12.8f", 1, basis->Q_1d, basis->q_ref_1d, stream)); 13022b730f8bSJeremy L Thompson CeedCall(CeedScalarView("qweight1d", "\t% 12.8f", 1, basis->Q_1d, basis->q_weight_1d, stream)); 13032b730f8bSJeremy L Thompson CeedCall(CeedScalarView("interp1d", "\t% 12.8f", basis->Q_1d, basis->P_1d, basis->interp_1d, stream)); 13042b730f8bSJeremy L Thompson CeedCall(CeedScalarView("grad1d", "\t% 12.8f", basis->Q_1d, basis->P_1d, basis->grad_1d, stream)); 130550c301a5SRezgar Shakeri } else { // non-tensor basis 13062b730f8bSJeremy L Thompson CeedCall(CeedScalarView("qref", "\t% 12.8f", 1, basis->Q * basis->dim, basis->q_ref_1d, stream)); 13072b730f8bSJeremy L Thompson CeedCall(CeedScalarView("qweight", "\t% 12.8f", 1, basis->Q, basis->q_weight_1d, stream)); 1308c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp)); 1309c4e3f59bSSebastian Grimberg CeedCall(CeedScalarView("interp", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->interp, stream)); 131050c301a5SRezgar Shakeri if (basis->grad) { 1311c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp)); 1312c4e3f59bSSebastian Grimberg CeedCall(CeedScalarView("grad", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->grad, stream)); 13137a982d89SJeremy L. Thompson } 131450c301a5SRezgar Shakeri if (basis->div) { 1315c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp)); 1316c4e3f59bSSebastian Grimberg CeedCall(CeedScalarView("div", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->div, stream)); 1317c4e3f59bSSebastian Grimberg } 1318c4e3f59bSSebastian Grimberg if (basis->curl) { 1319c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp)); 1320c4e3f59bSSebastian Grimberg CeedCall(CeedScalarView("curl", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->curl, stream)); 132150c301a5SRezgar Shakeri } 132250c301a5SRezgar Shakeri } 1323e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 13247a982d89SJeremy L. Thompson } 13257a982d89SJeremy L. Thompson 13267a982d89SJeremy L. Thompson /** 13277a982d89SJeremy L. Thompson @brief Apply basis evaluation from nodes to quadrature points or vice versa 13287a982d89SJeremy L. Thompson 1329ea61e9acSJeremy L Thompson @param[in] basis CeedBasis to evaluate 1330ea61e9acSJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1331ea61e9acSJeremy L Thompson the backend will specify the ordering in CeedElemRestrictionCreateBlocked() 1332ea61e9acSJeremy L Thompson @param[in] t_mode \ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points; 1333ea61e9acSJeremy L Thompson \ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes 1334ea61e9acSJeremy L Thompson @param[in] eval_mode \ref CEED_EVAL_NONE to use values directly, 13357a982d89SJeremy L. Thompson \ref CEED_EVAL_INTERP to use interpolated values, 13367a982d89SJeremy L. Thompson \ref CEED_EVAL_GRAD to use gradients, 1337c4e3f59bSSebastian Grimberg \ref CEED_EVAL_DIV to use divergence, 1338c4e3f59bSSebastian Grimberg \ref CEED_EVAL_CURL to use curl, 13397a982d89SJeremy L. Thompson \ref CEED_EVAL_WEIGHT to use quadrature weights. 13407a982d89SJeremy L. Thompson @param[in] u Input CeedVector 13417a982d89SJeremy L. Thompson @param[out] v Output CeedVector 13427a982d89SJeremy L. Thompson 13437a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 13447a982d89SJeremy L. Thompson 13457a982d89SJeremy L. Thompson @ref User 13467a982d89SJeremy L. Thompson **/ 13472b730f8bSJeremy L Thompson int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 13481f9221feSJeremy L Thompson CeedSize u_length = 0, v_length; 1349c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 13502b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 13512b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 1352c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 13532b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 13542b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 13552b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(v, &v_length)); 13567a982d89SJeremy L. Thompson if (u) { 13572b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(u, &u_length)); 13587a982d89SJeremy L. Thompson } 13597a982d89SJeremy L. Thompson 13606574a04fSJeremy L Thompson CeedCheck(basis->Apply, basis->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisApply"); 1361e15f9bd0SJeremy L Thompson 1362e15f9bd0SJeremy L Thompson // Check compatibility of topological and geometrical dimensions 13636574a04fSJeremy L Thompson CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0 && u_length % num_qpts == 0) || 13646574a04fSJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0 && v_length % num_qpts == 0), 13656574a04fSJeremy L Thompson basis->ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions"); 13667a982d89SJeremy L. Thompson 1367e15f9bd0SJeremy L Thompson // Check vector lengths to prevent out of bounds issues 13686574a04fSJeremy L Thompson bool good_dims = true; 1369d1d35e2fSjeremylt switch (eval_mode) { 1370e15f9bd0SJeremy L Thompson case CEED_EVAL_NONE: 13712b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 13722b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 1373c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 1374c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 13756574a04fSJeremy L Thompson good_dims = 13766574a04fSJeremy L Thompson ((t_mode == CEED_TRANSPOSE && u_length >= num_elem * num_comp * num_qpts * q_comp && v_length >= num_elem * num_comp * num_nodes) || 13776574a04fSJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && v_length >= num_elem * num_qpts * num_comp * q_comp && u_length >= num_elem * num_comp * num_nodes)); 1378e15f9bd0SJeremy L Thompson break; 1379e15f9bd0SJeremy L Thompson case CEED_EVAL_WEIGHT: 13806574a04fSJeremy L Thompson good_dims = v_length >= num_elem * num_qpts; 1381e15f9bd0SJeremy L Thompson break; 1382e15f9bd0SJeremy L Thompson } 13836574a04fSJeremy L Thompson CeedCheck(good_dims, basis->ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 1384e15f9bd0SJeremy L Thompson 13852b730f8bSJeremy L Thompson CeedCall(basis->Apply(basis, num_elem, t_mode, eval_mode, u, v)); 1386e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 13877a982d89SJeremy L. Thompson } 13887a982d89SJeremy L. Thompson 13897a982d89SJeremy L. Thompson /** 1390b7c9bbdaSJeremy L Thompson @brief Get Ceed associated with a CeedBasis 1391b7c9bbdaSJeremy L Thompson 1392ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1393b7c9bbdaSJeremy L Thompson @param[out] ceed Variable to store Ceed 1394b7c9bbdaSJeremy L Thompson 1395b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1396b7c9bbdaSJeremy L Thompson 1397b7c9bbdaSJeremy L Thompson @ref Advanced 1398b7c9bbdaSJeremy L Thompson **/ 1399b7c9bbdaSJeremy L Thompson int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) { 1400b7c9bbdaSJeremy L Thompson *ceed = basis->ceed; 1401b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1402b7c9bbdaSJeremy L Thompson } 1403b7c9bbdaSJeremy L Thompson 1404b7c9bbdaSJeremy L Thompson /** 14059d007619Sjeremylt @brief Get dimension for given CeedBasis 14069d007619Sjeremylt 1407ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 14089d007619Sjeremylt @param[out] dim Variable to store dimension of basis 14099d007619Sjeremylt 14109d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 14119d007619Sjeremylt 1412b7c9bbdaSJeremy L Thompson @ref Advanced 14139d007619Sjeremylt **/ 14149d007619Sjeremylt int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) { 14159d007619Sjeremylt *dim = basis->dim; 1416e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14179d007619Sjeremylt } 14189d007619Sjeremylt 14199d007619Sjeremylt /** 1420d99fa3c5SJeremy L Thompson @brief Get topology for given CeedBasis 1421d99fa3c5SJeremy L Thompson 1422ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1423d99fa3c5SJeremy L Thompson @param[out] topo Variable to store topology of basis 1424d99fa3c5SJeremy L Thompson 1425d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1426d99fa3c5SJeremy L Thompson 1427b7c9bbdaSJeremy L Thompson @ref Advanced 1428d99fa3c5SJeremy L Thompson **/ 1429d99fa3c5SJeremy L Thompson int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) { 1430d99fa3c5SJeremy L Thompson *topo = basis->topo; 1431e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1432d99fa3c5SJeremy L Thompson } 1433d99fa3c5SJeremy L Thompson 1434d99fa3c5SJeremy L Thompson /** 14359d007619Sjeremylt @brief Get number of components for given CeedBasis 14369d007619Sjeremylt 1437ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1438d1d35e2fSjeremylt @param[out] num_comp Variable to store number of components of basis 14399d007619Sjeremylt 14409d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 14419d007619Sjeremylt 1442b7c9bbdaSJeremy L Thompson @ref Advanced 14439d007619Sjeremylt **/ 1444d1d35e2fSjeremylt int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) { 1445d1d35e2fSjeremylt *num_comp = basis->num_comp; 1446e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14479d007619Sjeremylt } 14489d007619Sjeremylt 14499d007619Sjeremylt /** 14509d007619Sjeremylt @brief Get total number of nodes (in dim dimensions) of a CeedBasis 14519d007619Sjeremylt 1452ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 14539d007619Sjeremylt @param[out] P Variable to store number of nodes 14549d007619Sjeremylt 14559d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 14569d007619Sjeremylt 14579d007619Sjeremylt @ref Utility 14589d007619Sjeremylt **/ 14599d007619Sjeremylt int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) { 14609d007619Sjeremylt *P = basis->P; 1461e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14629d007619Sjeremylt } 14639d007619Sjeremylt 14649d007619Sjeremylt /** 14659d007619Sjeremylt @brief Get total number of nodes (in 1 dimension) of a CeedBasis 14669d007619Sjeremylt 1467ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1468d1d35e2fSjeremylt @param[out] P_1d Variable to store number of nodes 14699d007619Sjeremylt 14709d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 14719d007619Sjeremylt 1472b7c9bbdaSJeremy L Thompson @ref Advanced 14739d007619Sjeremylt **/ 1474d1d35e2fSjeremylt int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) { 14756402da51SJeremy L Thompson CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "Cannot supply P_1d for non-tensor basis"); 1476d1d35e2fSjeremylt *P_1d = basis->P_1d; 1477e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14789d007619Sjeremylt } 14799d007619Sjeremylt 14809d007619Sjeremylt /** 14819d007619Sjeremylt @brief Get total number of quadrature points (in dim dimensions) of a CeedBasis 14829d007619Sjeremylt 1483ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 14849d007619Sjeremylt @param[out] Q Variable to store number of quadrature points 14859d007619Sjeremylt 14869d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 14879d007619Sjeremylt 14889d007619Sjeremylt @ref Utility 14899d007619Sjeremylt **/ 14909d007619Sjeremylt int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) { 14919d007619Sjeremylt *Q = basis->Q; 1492e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14939d007619Sjeremylt } 14949d007619Sjeremylt 14959d007619Sjeremylt /** 14969d007619Sjeremylt @brief Get total number of quadrature points (in 1 dimension) of a CeedBasis 14979d007619Sjeremylt 1498ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1499d1d35e2fSjeremylt @param[out] Q_1d Variable to store number of quadrature points 15009d007619Sjeremylt 15019d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 15029d007619Sjeremylt 1503b7c9bbdaSJeremy L Thompson @ref Advanced 15049d007619Sjeremylt **/ 1505d1d35e2fSjeremylt int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) { 15066402da51SJeremy L Thompson CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "Cannot supply Q_1d for non-tensor basis"); 1507d1d35e2fSjeremylt *Q_1d = basis->Q_1d; 1508e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15099d007619Sjeremylt } 15109d007619Sjeremylt 15119d007619Sjeremylt /** 1512ea61e9acSJeremy L Thompson @brief Get reference coordinates of quadrature points (in dim dimensions) of a CeedBasis 15139d007619Sjeremylt 1514ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1515d1d35e2fSjeremylt @param[out] q_ref Variable to store reference coordinates of quadrature points 15169d007619Sjeremylt 15179d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 15189d007619Sjeremylt 1519b7c9bbdaSJeremy L Thompson @ref Advanced 15209d007619Sjeremylt **/ 1521d1d35e2fSjeremylt int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) { 1522d1d35e2fSjeremylt *q_ref = basis->q_ref_1d; 1523e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15249d007619Sjeremylt } 15259d007619Sjeremylt 15269d007619Sjeremylt /** 1527ea61e9acSJeremy L Thompson @brief Get quadrature weights of quadrature points (in dim dimensions) of a CeedBasis 15289d007619Sjeremylt 1529ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1530d1d35e2fSjeremylt @param[out] q_weight Variable to store quadrature weights 15319d007619Sjeremylt 15329d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 15339d007619Sjeremylt 1534b7c9bbdaSJeremy L Thompson @ref Advanced 15359d007619Sjeremylt **/ 1536d1d35e2fSjeremylt int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) { 1537d1d35e2fSjeremylt *q_weight = basis->q_weight_1d; 1538e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15399d007619Sjeremylt } 15409d007619Sjeremylt 15419d007619Sjeremylt /** 15429d007619Sjeremylt @brief Get interpolation matrix of a CeedBasis 15439d007619Sjeremylt 1544ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 15459d007619Sjeremylt @param[out] interp Variable to store interpolation matrix 15469d007619Sjeremylt 15479d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 15489d007619Sjeremylt 1549b7c9bbdaSJeremy L Thompson @ref Advanced 15509d007619Sjeremylt **/ 15516c58de82SJeremy L Thompson int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) { 15526402da51SJeremy L Thompson if (!basis->interp && basis->is_tensor_basis) { 15539d007619Sjeremylt // Allocate 15542b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->Q * basis->P, &basis->interp)); 15559d007619Sjeremylt 15569d007619Sjeremylt // Initialize 15572b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->Q * basis->P; i++) basis->interp[i] = 1.0; 15589d007619Sjeremylt 15599d007619Sjeremylt // Calculate 15602b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 15612b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 15629d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 1563d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 1564d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 1565d1d35e2fSjeremylt basis->interp[qpt * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 15669d007619Sjeremylt } 15679d007619Sjeremylt } 15682b730f8bSJeremy L Thompson } 15692b730f8bSJeremy L Thompson } 15709d007619Sjeremylt *interp = basis->interp; 1571e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15729d007619Sjeremylt } 15739d007619Sjeremylt 15749d007619Sjeremylt /** 15759d007619Sjeremylt @brief Get 1D interpolation matrix of a tensor product CeedBasis 15769d007619Sjeremylt 1577ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1578d1d35e2fSjeremylt @param[out] interp_1d Variable to store interpolation matrix 15799d007619Sjeremylt 15809d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 15819d007619Sjeremylt 15829d007619Sjeremylt @ref Backend 15839d007619Sjeremylt **/ 1584d1d35e2fSjeremylt int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) { 15856402da51SJeremy L Thompson CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "CeedBasis is not a tensor product basis."); 1586d1d35e2fSjeremylt *interp_1d = basis->interp_1d; 1587e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15889d007619Sjeremylt } 15899d007619Sjeremylt 15909d007619Sjeremylt /** 15919d007619Sjeremylt @brief Get gradient matrix of a CeedBasis 15929d007619Sjeremylt 1593ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 15949d007619Sjeremylt @param[out] grad Variable to store gradient matrix 15959d007619Sjeremylt 15969d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 15979d007619Sjeremylt 1598b7c9bbdaSJeremy L Thompson @ref Advanced 15999d007619Sjeremylt **/ 16006c58de82SJeremy L Thompson int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) { 16016402da51SJeremy L Thompson if (!basis->grad && basis->is_tensor_basis) { 16029d007619Sjeremylt // Allocate 16032b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->dim * basis->Q * basis->P, &basis->grad)); 16049d007619Sjeremylt 16059d007619Sjeremylt // Initialize 16062b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim * basis->Q * basis->P; i++) basis->grad[i] = 1.0; 16079d007619Sjeremylt 16089d007619Sjeremylt // Calculate 16092b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 16102b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim; i++) { 16112b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 16129d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 1613d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 1614d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 16152b730f8bSJeremy L Thompson if (i == d) basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->grad_1d[q * basis->P_1d + p]; 16162b730f8bSJeremy L Thompson else basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 16172b730f8bSJeremy L Thompson } 16182b730f8bSJeremy L Thompson } 16192b730f8bSJeremy L Thompson } 16209d007619Sjeremylt } 16219d007619Sjeremylt } 16229d007619Sjeremylt *grad = basis->grad; 1623e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 16249d007619Sjeremylt } 16259d007619Sjeremylt 16269d007619Sjeremylt /** 16279d007619Sjeremylt @brief Get 1D gradient matrix of a tensor product CeedBasis 16289d007619Sjeremylt 1629ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1630d1d35e2fSjeremylt @param[out] grad_1d Variable to store gradient matrix 16319d007619Sjeremylt 16329d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 16339d007619Sjeremylt 1634b7c9bbdaSJeremy L Thompson @ref Advanced 16359d007619Sjeremylt **/ 1636d1d35e2fSjeremylt int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) { 16376402da51SJeremy L Thompson CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "CeedBasis is not a tensor product basis."); 1638d1d35e2fSjeremylt *grad_1d = basis->grad_1d; 1639e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 16409d007619Sjeremylt } 16419d007619Sjeremylt 16429d007619Sjeremylt /** 164350c301a5SRezgar Shakeri @brief Get divergence matrix of a CeedBasis 164450c301a5SRezgar Shakeri 1645ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 164650c301a5SRezgar Shakeri @param[out] div Variable to store divergence matrix 164750c301a5SRezgar Shakeri 164850c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 164950c301a5SRezgar Shakeri 165050c301a5SRezgar Shakeri @ref Advanced 165150c301a5SRezgar Shakeri **/ 165250c301a5SRezgar Shakeri int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) { 16536574a04fSJeremy L Thompson CeedCheck(basis->div, basis->ceed, CEED_ERROR_MINOR, "CeedBasis does not have divergence matrix."); 165450c301a5SRezgar Shakeri *div = basis->div; 165550c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 165650c301a5SRezgar Shakeri } 165750c301a5SRezgar Shakeri 165850c301a5SRezgar Shakeri /** 1659c4e3f59bSSebastian Grimberg @brief Get curl matrix of a CeedBasis 1660c4e3f59bSSebastian Grimberg 1661c4e3f59bSSebastian Grimberg @param[in] basis CeedBasis 1662c4e3f59bSSebastian Grimberg @param[out] curl Variable to store curl matrix 1663c4e3f59bSSebastian Grimberg 1664c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1665c4e3f59bSSebastian Grimberg 1666c4e3f59bSSebastian Grimberg @ref Advanced 1667c4e3f59bSSebastian Grimberg **/ 1668c4e3f59bSSebastian Grimberg int CeedBasisGetCurl(CeedBasis basis, const CeedScalar **curl) { 16696574a04fSJeremy L Thompson CeedCheck(basis->curl, basis->ceed, CEED_ERROR_MINOR, "CeedBasis does not have curl matrix."); 1670c4e3f59bSSebastian Grimberg *curl = basis->curl; 1671c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1672c4e3f59bSSebastian Grimberg } 1673c4e3f59bSSebastian Grimberg 1674c4e3f59bSSebastian Grimberg /** 16757a982d89SJeremy L. Thompson @brief Destroy a CeedBasis 16767a982d89SJeremy L. Thompson 1677ea61e9acSJeremy L Thompson @param[in,out] basis CeedBasis to destroy 16787a982d89SJeremy L. Thompson 16797a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 16807a982d89SJeremy L. Thompson 16817a982d89SJeremy L. Thompson @ref User 16827a982d89SJeremy L. Thompson **/ 16837a982d89SJeremy L. Thompson int CeedBasisDestroy(CeedBasis *basis) { 16847425e127SJeremy L Thompson if (!*basis || *basis == CEED_BASIS_COLLOCATED || --(*basis)->ref_count > 0) { 1685ad6481ceSJeremy L Thompson *basis = NULL; 1686ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 1687ad6481ceSJeremy L Thompson } 16882b730f8bSJeremy L Thompson if ((*basis)->Destroy) CeedCall((*basis)->Destroy(*basis)); 16892b730f8bSJeremy L Thompson if ((*basis)->contract) CeedCall(CeedTensorContractDestroy(&(*basis)->contract)); 1690c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_ref_1d)); 1691c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_weight_1d)); 16922b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp)); 16932b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp_1d)); 16942b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad)); 16952b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad_1d)); 1696c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->div)); 1697c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->curl)); 16982b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*basis)->ceed)); 16992b730f8bSJeremy L Thompson CeedCall(CeedFree(basis)); 1700e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 17017a982d89SJeremy L. Thompson } 17027a982d89SJeremy L. Thompson 17037a982d89SJeremy L. Thompson /** 1704b11c1e72Sjeremylt @brief Construct a Gauss-Legendre quadrature 1705b11c1e72Sjeremylt 1706ea61e9acSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree 2*Q-1 exactly) 1707d1d35e2fSjeremylt @param[out] q_ref_1d Array of length Q to hold the abscissa on [-1, 1] 1708d1d35e2fSjeremylt @param[out] q_weight_1d Array of length Q to hold the weights 1709b11c1e72Sjeremylt 1710b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1711dfdf5a53Sjeremylt 1712dfdf5a53Sjeremylt @ref Utility 1713b11c1e72Sjeremylt **/ 17142b730f8bSJeremy L Thompson int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 1715d7b241e6Sjeremylt // Allocate 1716d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0 * atan(1.0); 1717d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 171892ae7e47SJeremy L Thompson for (CeedInt i = 0; i <= Q / 2; i++) { 1719d7b241e6Sjeremylt // Guess 1720d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(2 * i + 1) / ((CeedScalar)(2 * Q))); 1721d7b241e6Sjeremylt // Pn(xi) 1722d7b241e6Sjeremylt P0 = 1.0; 1723d7b241e6Sjeremylt P1 = xi; 1724d7b241e6Sjeremylt P2 = 0.0; 172592ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 1726d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 1727d7b241e6Sjeremylt P0 = P1; 1728d7b241e6Sjeremylt P1 = P2; 1729d7b241e6Sjeremylt } 1730d7b241e6Sjeremylt // First Newton Step 1731d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 1732d7b241e6Sjeremylt xi = xi - P2 / dP2; 1733d7b241e6Sjeremylt // Newton to convergence 173492ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(P2) > 10 * CEED_EPSILON; k++) { 1735d7b241e6Sjeremylt P0 = 1.0; 1736d7b241e6Sjeremylt P1 = xi; 173792ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 1738d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 1739d7b241e6Sjeremylt P0 = P1; 1740d7b241e6Sjeremylt P1 = P2; 1741d7b241e6Sjeremylt } 1742d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 1743d7b241e6Sjeremylt xi = xi - P2 / dP2; 1744d7b241e6Sjeremylt } 1745d7b241e6Sjeremylt // Save xi, wi 1746d7b241e6Sjeremylt wi = 2.0 / ((1.0 - xi * xi) * dP2 * dP2); 1747d1d35e2fSjeremylt q_weight_1d[i] = wi; 1748d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 1749d1d35e2fSjeremylt q_ref_1d[i] = -xi; 1750d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 1751d7b241e6Sjeremylt } 1752e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1753d7b241e6Sjeremylt } 1754d7b241e6Sjeremylt 1755b11c1e72Sjeremylt /** 1756b11c1e72Sjeremylt @brief Construct a Gauss-Legendre-Lobatto quadrature 1757b11c1e72Sjeremylt 1758ea61e9acSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree 2*Q-3 exactly) 1759d1d35e2fSjeremylt @param[out] q_ref_1d Array of length Q to hold the abscissa on [-1, 1] 1760d1d35e2fSjeremylt @param[out] q_weight_1d Array of length Q to hold the weights 1761b11c1e72Sjeremylt 1762b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1763dfdf5a53Sjeremylt 1764dfdf5a53Sjeremylt @ref Utility 1765b11c1e72Sjeremylt **/ 17662b730f8bSJeremy L Thompson int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 1767d7b241e6Sjeremylt // Allocate 1768d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0 * atan(1.0); 1769d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 1770d7b241e6Sjeremylt // Set endpoints 17716574a04fSJeremy L Thompson CeedCheck(Q > 1, NULL, CEED_ERROR_DIMENSION, "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q); 1772d7b241e6Sjeremylt wi = 2.0 / ((CeedScalar)(Q * (Q - 1))); 1773d1d35e2fSjeremylt if (q_weight_1d) { 1774d1d35e2fSjeremylt q_weight_1d[0] = wi; 1775d1d35e2fSjeremylt q_weight_1d[Q - 1] = wi; 1776d7b241e6Sjeremylt } 1777d1d35e2fSjeremylt q_ref_1d[0] = -1.0; 1778d1d35e2fSjeremylt q_ref_1d[Q - 1] = 1.0; 1779d7b241e6Sjeremylt // Interior 178092ae7e47SJeremy L Thompson for (CeedInt i = 1; i <= (Q - 1) / 2; i++) { 1781d7b241e6Sjeremylt // Guess 1782d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(i) / (CeedScalar)(Q - 1)); 1783d7b241e6Sjeremylt // Pn(xi) 1784d7b241e6Sjeremylt P0 = 1.0; 1785d7b241e6Sjeremylt P1 = xi; 1786d7b241e6Sjeremylt P2 = 0.0; 178792ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 1788d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 1789d7b241e6Sjeremylt P0 = P1; 1790d7b241e6Sjeremylt P1 = P2; 1791d7b241e6Sjeremylt } 1792d7b241e6Sjeremylt // First Newton step 1793d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 1794d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 1795d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 1796d7b241e6Sjeremylt // Newton to convergence 179792ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(dP2) > 10 * CEED_EPSILON; k++) { 1798d7b241e6Sjeremylt P0 = 1.0; 1799d7b241e6Sjeremylt P1 = xi; 180092ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 1801d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 1802d7b241e6Sjeremylt P0 = P1; 1803d7b241e6Sjeremylt P1 = P2; 1804d7b241e6Sjeremylt } 1805d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 1806d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 1807d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 1808d7b241e6Sjeremylt } 1809d7b241e6Sjeremylt // Save xi, wi 1810d7b241e6Sjeremylt wi = 2.0 / (((CeedScalar)(Q * (Q - 1))) * P2 * P2); 1811d1d35e2fSjeremylt if (q_weight_1d) { 1812d1d35e2fSjeremylt q_weight_1d[i] = wi; 1813d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 1814d7b241e6Sjeremylt } 1815d1d35e2fSjeremylt q_ref_1d[i] = -xi; 1816d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 1817d7b241e6Sjeremylt } 1818e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1819d7b241e6Sjeremylt } 1820d7b241e6Sjeremylt 1821d7b241e6Sjeremylt /// @} 1822