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 191*de05fbb2SSebastian Grimberg // projection basis will have a gradient operation (allocated even if not H^1 for the 192*de05fbb2SSebastian 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 } 200*de05fbb2SSebastian 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)); 912d7b241e6Sjeremylt (*basis)->ceed = ceed; 9132b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 914d1d35e2fSjeremylt (*basis)->ref_count = 1; 9156402da51SJeremy L Thompson (*basis)->is_tensor_basis = true; 916d7b241e6Sjeremylt (*basis)->dim = dim; 917d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 918d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 919d1d35e2fSjeremylt (*basis)->P_1d = P_1d; 920d1d35e2fSjeremylt (*basis)->Q_1d = Q_1d; 921d1d35e2fSjeremylt (*basis)->P = CeedIntPow(P_1d, dim); 922d1d35e2fSjeremylt (*basis)->Q = CeedIntPow(Q_1d, dim); 923c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 9242b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_ref_1d)); 9252b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d, &(*basis)->q_weight_1d)); 926ff3a0f91SJeremy L Thompson if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d * sizeof(q_ref_1d[0])); 9272b730f8bSJeremy L Thompson if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, Q_1d * sizeof(q_weight_1d[0])); 9282b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->interp_1d)); 9292b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->grad_1d)); 9302b730f8bSJeremy L Thompson if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, Q_1d * P_1d * sizeof(interp_1d[0])); 931ff3a0f91SJeremy L Thompson if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d * P_1d * sizeof(grad_1d[0])); 9322b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, *basis)); 933e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 934d7b241e6Sjeremylt } 935d7b241e6Sjeremylt 936b11c1e72Sjeremylt /** 93795bb1877Svaleriabarra @brief Create a tensor-product Lagrange basis 938b11c1e72Sjeremylt 939ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedBasis will be created 940ea61e9acSJeremy L Thompson @param[in] dim Topological dimension of element 941ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 942ea61e9acSJeremy L Thompson @param[in] P Number of Gauss-Lobatto nodes in one dimension. 943ea61e9acSJeremy L Thompson The polynomial degree of the resulting Q_k element is k=P-1. 944ea61e9acSJeremy L Thompson @param[in] Q Number of quadrature points in one dimension. 945ea61e9acSJeremy L Thompson @param[in] quad_mode Distribution of the Q quadrature points (affects order of accuracy for the quadrature) 946ea61e9acSJeremy L Thompson @param[out] basis Address of the variable where the newly created CeedBasis will be stored. 947b11c1e72Sjeremylt 948b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 949dfdf5a53Sjeremylt 9507a982d89SJeremy L. Thompson @ref User 951b11c1e72Sjeremylt **/ 9522b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis) { 953d7b241e6Sjeremylt // Allocate 9542b730f8bSJeremy L Thompson int ierr = CEED_ERROR_SUCCESS, i, j, k; 9552b730f8bSJeremy L Thompson CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, *q_weight_1d; 9564d537eeaSYohann 9576574a04fSJeremy L Thompson CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "Basis dimension must be a positive value"); 9586574a04fSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component"); 9596574a04fSJeremy L Thompson CeedCheck(P > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node"); 9606574a04fSJeremy L Thompson CeedCheck(Q > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point"); 961227444bfSJeremy L Thompson 962e15f9bd0SJeremy L Thompson // Get Nodes and Weights 9632b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &interp_1d)); 9642b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P * Q, &grad_1d)); 9652b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P, &nodes)); 9662b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_ref_1d)); 9672b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &q_weight_1d)); 9682b730f8bSJeremy L Thompson if (CeedLobattoQuadrature(P, nodes, NULL) != CEED_ERROR_SUCCESS) goto cleanup; 969d1d35e2fSjeremylt switch (quad_mode) { 970d7b241e6Sjeremylt case CEED_GAUSS: 971d1d35e2fSjeremylt ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d); 972d7b241e6Sjeremylt break; 973d7b241e6Sjeremylt case CEED_GAUSS_LOBATTO: 974d1d35e2fSjeremylt ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d); 975d7b241e6Sjeremylt break; 976d7b241e6Sjeremylt } 9772b730f8bSJeremy L Thompson if (ierr != CEED_ERROR_SUCCESS) goto cleanup; 978e15f9bd0SJeremy L Thompson 979d7b241e6Sjeremylt // Build B, D matrix 980d7b241e6Sjeremylt // Fornberg, 1998 981d7b241e6Sjeremylt for (i = 0; i < Q; i++) { 982d7b241e6Sjeremylt c1 = 1.0; 983d1d35e2fSjeremylt c3 = nodes[0] - q_ref_1d[i]; 984d1d35e2fSjeremylt interp_1d[i * P + 0] = 1.0; 985d7b241e6Sjeremylt for (j = 1; j < P; j++) { 986d7b241e6Sjeremylt c2 = 1.0; 987d7b241e6Sjeremylt c4 = c3; 988d1d35e2fSjeremylt c3 = nodes[j] - q_ref_1d[i]; 989d7b241e6Sjeremylt for (k = 0; k < j; k++) { 990d7b241e6Sjeremylt dx = nodes[j] - nodes[k]; 991d7b241e6Sjeremylt c2 *= dx; 992d7b241e6Sjeremylt if (k == j - 1) { 993d1d35e2fSjeremylt grad_1d[i * P + j] = c1 * (interp_1d[i * P + k] - c4 * grad_1d[i * P + k]) / c2; 994d1d35e2fSjeremylt interp_1d[i * P + j] = -c1 * c4 * interp_1d[i * P + k] / c2; 995d7b241e6Sjeremylt } 996d1d35e2fSjeremylt grad_1d[i * P + k] = (c3 * grad_1d[i * P + k] - interp_1d[i * P + k]) / dx; 997d1d35e2fSjeremylt interp_1d[i * P + k] = c3 * interp_1d[i * P + k] / dx; 998d7b241e6Sjeremylt } 999d7b241e6Sjeremylt c1 = c2; 1000d7b241e6Sjeremylt } 1001d7b241e6Sjeremylt } 10029ac7b42eSJeremy L Thompson // Pass to CeedBasisCreateTensorH1 10032b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 1004e15f9bd0SJeremy L Thompson cleanup: 10052b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_1d)); 10062b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_1d)); 10072b730f8bSJeremy L Thompson CeedCall(CeedFree(&nodes)); 10082b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref_1d)); 10092b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight_1d)); 1010e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1011d7b241e6Sjeremylt } 1012d7b241e6Sjeremylt 1013b11c1e72Sjeremylt /** 1014ba59ac12SSebastian Grimberg @brief Create a non tensor-product basis for H^1 discretizations 1015a8de75f0Sjeremylt 1016ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedBasis will be created 1017ea61e9acSJeremy L Thompson @param[in] topo Topology of element, e.g. hypercube, simplex, ect 1018ea61e9acSJeremy L Thompson @param[in] num_comp Number of field components (1 for scalar fields) 1019ea61e9acSJeremy L Thompson @param[in] num_nodes Total number of nodes 1020ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1021ea61e9acSJeremy L Thompson @param[in] interp Row-major (num_qpts * num_nodes) matrix expressing the values of nodal basis functions at quadrature points 1022c4e3f59bSSebastian Grimberg @param[in] grad Row-major (dim * num_qpts * num_nodes) matrix expressing derivatives of nodal basis functions at quadrature points 10239fe083eeSJeremy L Thompson @param[in] q_ref Array of length num_qpts * dim holding the locations of quadrature points on the reference element 1024ea61e9acSJeremy L Thompson @param[in] q_weight Array of length num_qpts holding the quadrature weights on the reference element 1025ea61e9acSJeremy L Thompson @param[out] basis Address of the variable where the newly created CeedBasis will be stored. 1026a8de75f0Sjeremylt 1027a8de75f0Sjeremylt @return An error code: 0 - success, otherwise - failure 1028a8de75f0Sjeremylt 10297a982d89SJeremy L. Thompson @ref User 1030a8de75f0Sjeremylt **/ 10312b730f8bSJeremy L Thompson int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 10322b730f8bSJeremy L Thompson const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1033d1d35e2fSjeremylt CeedInt P = num_nodes, Q = num_qpts, dim = 0; 1034a8de75f0Sjeremylt 10355fe0d4faSjeremylt if (!ceed->BasisCreateH1) { 10365fe0d4faSjeremylt Ceed delegate; 10376574a04fSJeremy L Thompson 10382b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 10396574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisCreateH1"); 10402b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis)); 1041e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10425fe0d4faSjeremylt } 10435fe0d4faSjeremylt 10446574a04fSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component"); 10456574a04fSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node"); 10466574a04fSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point"); 1047227444bfSJeremy L Thompson 10482b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 1049a8de75f0Sjeremylt 10502b730f8bSJeremy L Thompson CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1051a8de75f0Sjeremylt 1052a8de75f0Sjeremylt (*basis)->ceed = ceed; 10532b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 1054d1d35e2fSjeremylt (*basis)->ref_count = 1; 10556402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1056a8de75f0Sjeremylt (*basis)->dim = dim; 1057d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 1058d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 1059a8de75f0Sjeremylt (*basis)->P = P; 1060a8de75f0Sjeremylt (*basis)->Q = Q; 1061c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_H1; 10622b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * dim, &(*basis)->q_ref_1d)); 10632b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q, &(*basis)->q_weight_1d)); 1064ff3a0f91SJeremy L Thompson if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1065ff3a0f91SJeremy L Thompson if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 10662b730f8bSJeremy L Thompson CeedCall(CeedCalloc(Q * P, &(*basis)->interp)); 10672b730f8bSJeremy L Thompson CeedCall(CeedCalloc(dim * Q * P, &(*basis)->grad)); 1068ff3a0f91SJeremy L Thompson if (interp) memcpy((*basis)->interp, interp, Q * P * sizeof(interp[0])); 1069ff3a0f91SJeremy L Thompson if (grad) memcpy((*basis)->grad, grad, dim * Q * P * sizeof(grad[0])); 10702b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, *basis)); 1071e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1072a8de75f0Sjeremylt } 1073a8de75f0Sjeremylt 1074a8de75f0Sjeremylt /** 1075859c15bbSJames Wright @brief Create a non tensor-product basis for \f$H(\mathrm{div})\f$ discretizations 107650c301a5SRezgar Shakeri 1077ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedBasis will be created 1078ea61e9acSJeremy 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 1079ea61e9acSJeremy L Thompson @param[in] num_comp Number of components (usually 1 for vectors in H(div) bases) 1080ea61e9acSJeremy L Thompson @param[in] num_nodes Total number of nodes (dofs per element) 1081ea61e9acSJeremy L Thompson @param[in] num_qpts Total number of quadrature points 1082c4e3f59bSSebastian Grimberg @param[in] interp Row-major (dim * num_qpts * num_nodes) matrix expressing the values of basis functions at quadrature points 1083c4e3f59bSSebastian Grimberg @param[in] div Row-major (num_qpts * num_nodes) matrix expressing divergence of basis functions at quadrature points 10849fe083eeSJeremy L Thompson @param[in] q_ref Array of length num_qpts * dim holding the locations of quadrature points on the reference element 1085ea61e9acSJeremy L Thompson @param[in] q_weight Array of length num_qpts holding the quadrature weights on the reference element 1086ea61e9acSJeremy L Thompson @param[out] basis Address of the variable where the newly created CeedBasis will be stored. 108750c301a5SRezgar Shakeri 108850c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 108950c301a5SRezgar Shakeri 109050c301a5SRezgar Shakeri @ref User 109150c301a5SRezgar Shakeri **/ 10922b730f8bSJeremy L Thompson int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 10932b730f8bSJeremy L Thompson const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 109450c301a5SRezgar Shakeri CeedInt Q = num_qpts, P = num_nodes, dim = 0; 1095c4e3f59bSSebastian Grimberg 109650c301a5SRezgar Shakeri if (!ceed->BasisCreateHdiv) { 109750c301a5SRezgar Shakeri Ceed delegate; 10986574a04fSJeremy L Thompson 10992b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 11006574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv"); 11012b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis)); 110250c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 110350c301a5SRezgar Shakeri } 110450c301a5SRezgar Shakeri 11056574a04fSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component"); 11066574a04fSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node"); 11076574a04fSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point"); 1108227444bfSJeremy L Thompson 11092b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, basis)); 111050c301a5SRezgar Shakeri 1111c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1112c4e3f59bSSebastian Grimberg 111350c301a5SRezgar Shakeri (*basis)->ceed = ceed; 11142b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 111550c301a5SRezgar Shakeri (*basis)->ref_count = 1; 11166402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 111750c301a5SRezgar Shakeri (*basis)->dim = dim; 111850c301a5SRezgar Shakeri (*basis)->topo = topo; 111950c301a5SRezgar Shakeri (*basis)->num_comp = num_comp; 112050c301a5SRezgar Shakeri (*basis)->P = P; 112150c301a5SRezgar Shakeri (*basis)->Q = Q; 1122c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HDIV; 11232b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 11242b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 112550c301a5SRezgar Shakeri if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 112650c301a5SRezgar Shakeri if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 11272b730f8bSJeremy L Thompson CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 11282b730f8bSJeremy L Thompson CeedCall(CeedMalloc(Q * P, &(*basis)->div)); 112950c301a5SRezgar Shakeri if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 113050c301a5SRezgar Shakeri if (div) memcpy((*basis)->div, div, Q * P * sizeof(div[0])); 11312b730f8bSJeremy L Thompson CeedCall(ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, q_weight, *basis)); 113250c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 113350c301a5SRezgar Shakeri } 113450c301a5SRezgar Shakeri 113550c301a5SRezgar Shakeri /** 11364385fb7fSSebastian Grimberg @brief Create a non tensor-product basis for \f$H(\mathrm{curl})\f$ discretizations 1137c4e3f59bSSebastian Grimberg 1138c4e3f59bSSebastian Grimberg @param[in] ceed Ceed object where the CeedBasis will be created 1139c4e3f59bSSebastian Grimberg @param[in] topo Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below 1140c4e3f59bSSebastian Grimberg @param[in] num_comp Number of components (usually 1 for vectors in H(curl) bases) 1141c4e3f59bSSebastian Grimberg @param[in] num_nodes Total number of nodes (dofs per element) 1142c4e3f59bSSebastian Grimberg @param[in] num_qpts Total number of quadrature points 1143c4e3f59bSSebastian Grimberg @param[in] interp Row-major (dim * num_qpts * num_nodes) matrix expressing the values of basis functions at quadrature points 1144c4e3f59bSSebastian 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 1145c4e3f59bSSebastian Grimberg quadrature points 1146c4e3f59bSSebastian Grimberg @param[in] q_ref Array of length num_qpts * dim holding the locations of quadrature points on the reference element 1147c4e3f59bSSebastian Grimberg @param[in] q_weight Array of length num_qpts holding the quadrature weights on the reference element 1148c4e3f59bSSebastian Grimberg @param[out] basis Address of the variable where the newly created CeedBasis will be stored. 1149c4e3f59bSSebastian Grimberg 1150c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1151c4e3f59bSSebastian Grimberg 1152c4e3f59bSSebastian Grimberg @ref User 1153c4e3f59bSSebastian Grimberg **/ 1154c4e3f59bSSebastian Grimberg int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 1155c4e3f59bSSebastian Grimberg const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1156c4e3f59bSSebastian Grimberg CeedInt Q = num_qpts, P = num_nodes, dim = 0, curl_comp = 0; 1157c4e3f59bSSebastian Grimberg 1158c4e3f59bSSebastian Grimberg if (!ceed->BasisCreateHdiv) { 1159c4e3f59bSSebastian Grimberg Ceed delegate; 11606574a04fSJeremy L Thompson 1161c4e3f59bSSebastian Grimberg CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 11626574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl"); 1163c4e3f59bSSebastian Grimberg CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis)); 1164c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1165c4e3f59bSSebastian Grimberg } 1166c4e3f59bSSebastian Grimberg 11676574a04fSJeremy L Thompson CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 component"); 11686574a04fSJeremy L Thompson CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 node"); 11696574a04fSJeremy L Thompson CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "Basis must have at least 1 quadrature point"); 1170c4e3f59bSSebastian Grimberg 1171c4e3f59bSSebastian Grimberg CeedCall(CeedCalloc(1, basis)); 1172c4e3f59bSSebastian Grimberg 1173c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1174c4e3f59bSSebastian Grimberg curl_comp = (dim < 3) ? 1 : dim; 1175c4e3f59bSSebastian Grimberg 1176c4e3f59bSSebastian Grimberg (*basis)->ceed = ceed; 1177c4e3f59bSSebastian Grimberg CeedCall(CeedReference(ceed)); 1178c4e3f59bSSebastian Grimberg (*basis)->ref_count = 1; 11796402da51SJeremy L Thompson (*basis)->is_tensor_basis = false; 1180c4e3f59bSSebastian Grimberg (*basis)->dim = dim; 1181c4e3f59bSSebastian Grimberg (*basis)->topo = topo; 1182c4e3f59bSSebastian Grimberg (*basis)->num_comp = num_comp; 1183c4e3f59bSSebastian Grimberg (*basis)->P = P; 1184c4e3f59bSSebastian Grimberg (*basis)->Q = Q; 1185c4e3f59bSSebastian Grimberg (*basis)->fe_space = CEED_FE_SPACE_HCURL; 1186c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 1187c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 1188c4e3f59bSSebastian Grimberg if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1189c4e3f59bSSebastian Grimberg if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 1190c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 1191c4e3f59bSSebastian Grimberg CeedCall(CeedMalloc(curl_comp * Q * P, &(*basis)->curl)); 1192c4e3f59bSSebastian Grimberg if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 1193c4e3f59bSSebastian Grimberg if (curl) memcpy((*basis)->curl, curl, curl_comp * Q * P * sizeof(curl[0])); 1194c4e3f59bSSebastian Grimberg CeedCall(ceed->BasisCreateHcurl(topo, dim, P, Q, interp, curl, q_ref, q_weight, *basis)); 1195c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1196c4e3f59bSSebastian Grimberg } 1197c4e3f59bSSebastian Grimberg 1198c4e3f59bSSebastian Grimberg /** 1199ea61e9acSJeremy L Thompson @brief Create a CeedBasis for projection from the nodes of `basis_from` to the nodes of `basis_to`. 1200ba59ac12SSebastian Grimberg 12019fd66db6SSebastian Grimberg Only `CEED_EVAL_INTERP` will be valid for the new basis, `basis_project`. 12029fd66db6SSebastian Grimberg For H^1 spaces, `CEED_EVAL_GRAD` will also be valid. 1203*de05fbb2SSebastian Grimberg The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR 12049fd66db6SSebastian Grimberg factorization. 12059fd66db6SSebastian Grimberg The gradient (for the H^1 case) is given by `grad_project = interp_to^+ * grad_from`. 120615ad3917SSebastian Grimberg 120715ad3917SSebastian Grimberg Note: `basis_from` and `basis_to` must have compatible quadrature spaces. 120815ad3917SSebastian Grimberg 12099fd66db6SSebastian Grimberg Note: `basis_project` will have the same number of components as `basis_from`, regardless of the number of components that `basis_to` has. 12109fd66db6SSebastian Grimberg If `basis_from` has 3 components and `basis_to` has 5 components, then `basis_project` will have 3 components. 1211f113e5dcSJeremy L Thompson 1212f113e5dcSJeremy L Thompson @param[in] basis_from CeedBasis to prolong from 1213446e7af4SJeremy L Thompson @param[in] basis_to CeedBasis to prolong to 1214ea61e9acSJeremy L Thompson @param[out] basis_project Address of the variable where the newly created CeedBasis will be stored. 1215f113e5dcSJeremy L Thompson 1216f113e5dcSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1217f113e5dcSJeremy L Thompson 1218f113e5dcSJeremy L Thompson @ref User 1219f113e5dcSJeremy L Thompson **/ 12202b730f8bSJeremy L Thompson int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project) { 1221f113e5dcSJeremy L Thompson Ceed ceed; 12222b730f8bSJeremy L Thompson CeedCall(CeedBasisGetCeed(basis_to, &ceed)); 1223f113e5dcSJeremy L Thompson 1224ecc88aebSJeremy L Thompson // Create projection matrix 122514556e63SJeremy L Thompson CeedScalar *interp_project, *grad_project; 12262b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateProjectionMatrices(basis_from, basis_to, &interp_project, &grad_project)); 1227f113e5dcSJeremy L Thompson 1228f113e5dcSJeremy L Thompson // Build basis 1229f113e5dcSJeremy L Thompson bool is_tensor; 1230f113e5dcSJeremy L Thompson CeedInt dim, num_comp; 123114556e63SJeremy L Thompson CeedScalar *q_ref, *q_weight; 12322b730f8bSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis_to, &is_tensor)); 12332b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_to, &dim)); 12342b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_from, &num_comp)); 1235f113e5dcSJeremy L Thompson if (is_tensor) { 1236f113e5dcSJeremy L Thompson CeedInt P_1d_to, P_1d_from; 12372b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_1d_from)); 12382b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_1d_to)); 12392b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_to, &q_ref)); 12402b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_to, &q_weight)); 12412b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, interp_project, grad_project, q_ref, q_weight, basis_project)); 1242f113e5dcSJeremy L Thompson } else { 1243*de05fbb2SSebastian Grimberg // Even if basis_to and basis_from are not H1, the resulting basis is H1 for interpolation to work 1244f113e5dcSJeremy L Thompson CeedElemTopology topo; 12452b730f8bSJeremy L Thompson CeedCall(CeedBasisGetTopology(basis_to, &topo)); 1246f113e5dcSJeremy L Thompson CeedInt num_nodes_to, num_nodes_from; 12472b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_from, &num_nodes_from)); 12482b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_to, &num_nodes_to)); 12492b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_to * dim, &q_ref)); 12502b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_to, &q_weight)); 12512b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, q_ref, q_weight, basis_project)); 1252f113e5dcSJeremy L Thompson } 1253f113e5dcSJeremy L Thompson 1254f113e5dcSJeremy L Thompson // Cleanup 12552b730f8bSJeremy L Thompson CeedCall(CeedFree(&interp_project)); 12562b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_project)); 12572b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref)); 12582b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight)); 1259f113e5dcSJeremy L Thompson 1260f113e5dcSJeremy L Thompson return CEED_ERROR_SUCCESS; 1261f113e5dcSJeremy L Thompson } 1262f113e5dcSJeremy L Thompson 1263f113e5dcSJeremy L Thompson /** 1264ea61e9acSJeremy L Thompson @brief Copy the pointer to a CeedBasis. 12659560d06aSjeremylt 1266512bb800SJeremy 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. 1267512bb800SJeremy L Thompson This CeedBasis will be destroyed if `basis_copy` is the only reference to this CeedBasis. 1268ea61e9acSJeremy L Thompson 1269ea61e9acSJeremy L Thompson @param[in] basis CeedBasis to copy reference to 1270ea61e9acSJeremy L Thompson @param[in,out] basis_copy Variable to store copied reference 12719560d06aSjeremylt 12729560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 12739560d06aSjeremylt 12749560d06aSjeremylt @ref User 12759560d06aSjeremylt **/ 12769560d06aSjeremylt int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) { 1277393ac2cdSJeremy L Thompson if (basis != CEED_BASIS_COLLOCATED) CeedCall(CeedBasisReference(basis)); 12782b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(basis_copy)); 12799560d06aSjeremylt *basis_copy = basis; 12809560d06aSjeremylt return CEED_ERROR_SUCCESS; 12819560d06aSjeremylt } 12829560d06aSjeremylt 12839560d06aSjeremylt /** 12847a982d89SJeremy L. Thompson @brief View a CeedBasis 12857a982d89SJeremy L. Thompson 1286ea61e9acSJeremy L Thompson @param[in] basis CeedBasis to view 1287ea61e9acSJeremy L Thompson @param[in] stream Stream to view to, e.g., stdout 12887a982d89SJeremy L. Thompson 12897a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 12907a982d89SJeremy L. Thompson 12917a982d89SJeremy L. Thompson @ref User 12927a982d89SJeremy L. Thompson **/ 12937a982d89SJeremy L. Thompson int CeedBasisView(CeedBasis basis, FILE *stream) { 129450c301a5SRezgar Shakeri CeedElemTopology topo = basis->topo; 1295c4e3f59bSSebastian Grimberg CeedFESpace fe_space = basis->fe_space; 1296c4e3f59bSSebastian Grimberg CeedInt q_comp = 0; 12972b730f8bSJeremy L Thompson 129850c301a5SRezgar Shakeri // Print FE space and element topology of the basis 12996402da51SJeremy L Thompson if (basis->is_tensor_basis) { 1300c4e3f59bSSebastian Grimberg fprintf(stream, "CeedBasis (%s on a %s element): dim=%" CeedInt_FMT " P=%" CeedInt_FMT " Q=%" CeedInt_FMT "\n", CeedFESpaces[fe_space], 13012b730f8bSJeremy L Thompson CeedElemTopologies[topo], basis->dim, basis->P_1d, basis->Q_1d); 130250c301a5SRezgar Shakeri } else { 1303c4e3f59bSSebastian Grimberg fprintf(stream, "CeedBasis (%s on a %s element): dim=%" CeedInt_FMT " P=%" CeedInt_FMT " Q=%" CeedInt_FMT "\n", CeedFESpaces[fe_space], 13042b730f8bSJeremy L Thompson CeedElemTopologies[topo], basis->dim, basis->P, basis->Q); 130550c301a5SRezgar Shakeri } 1306ea61e9acSJeremy L Thompson // Print quadrature data, interpolation/gradient/divergence/curl of the basis 13076402da51SJeremy L Thompson if (basis->is_tensor_basis) { // tensor basis 13082b730f8bSJeremy L Thompson CeedCall(CeedScalarView("qref1d", "\t% 12.8f", 1, basis->Q_1d, basis->q_ref_1d, stream)); 13092b730f8bSJeremy L Thompson CeedCall(CeedScalarView("qweight1d", "\t% 12.8f", 1, basis->Q_1d, basis->q_weight_1d, stream)); 13102b730f8bSJeremy L Thompson CeedCall(CeedScalarView("interp1d", "\t% 12.8f", basis->Q_1d, basis->P_1d, basis->interp_1d, stream)); 13112b730f8bSJeremy L Thompson CeedCall(CeedScalarView("grad1d", "\t% 12.8f", basis->Q_1d, basis->P_1d, basis->grad_1d, stream)); 131250c301a5SRezgar Shakeri } else { // non-tensor basis 13132b730f8bSJeremy L Thompson CeedCall(CeedScalarView("qref", "\t% 12.8f", 1, basis->Q * basis->dim, basis->q_ref_1d, stream)); 13142b730f8bSJeremy L Thompson CeedCall(CeedScalarView("qweight", "\t% 12.8f", 1, basis->Q, basis->q_weight_1d, stream)); 1315c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp)); 1316c4e3f59bSSebastian Grimberg CeedCall(CeedScalarView("interp", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->interp, stream)); 131750c301a5SRezgar Shakeri if (basis->grad) { 1318c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp)); 1319c4e3f59bSSebastian Grimberg CeedCall(CeedScalarView("grad", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->grad, stream)); 13207a982d89SJeremy L. Thompson } 132150c301a5SRezgar Shakeri if (basis->div) { 1322c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp)); 1323c4e3f59bSSebastian Grimberg CeedCall(CeedScalarView("div", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->div, stream)); 1324c4e3f59bSSebastian Grimberg } 1325c4e3f59bSSebastian Grimberg if (basis->curl) { 1326c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp)); 1327c4e3f59bSSebastian Grimberg CeedCall(CeedScalarView("curl", "\t% 12.8f", q_comp * basis->Q, basis->P, basis->curl, stream)); 132850c301a5SRezgar Shakeri } 132950c301a5SRezgar Shakeri } 1330e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 13317a982d89SJeremy L. Thompson } 13327a982d89SJeremy L. Thompson 13337a982d89SJeremy L. Thompson /** 13347a982d89SJeremy L. Thompson @brief Apply basis evaluation from nodes to quadrature points or vice versa 13357a982d89SJeremy L. Thompson 1336ea61e9acSJeremy L Thompson @param[in] basis CeedBasis to evaluate 1337ea61e9acSJeremy L Thompson @param[in] num_elem The number of elements to apply the basis evaluation to; 1338ea61e9acSJeremy L Thompson the backend will specify the ordering in CeedElemRestrictionCreateBlocked() 1339ea61e9acSJeremy L Thompson @param[in] t_mode \ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points; 1340ea61e9acSJeremy L Thompson \ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes 1341ea61e9acSJeremy L Thompson @param[in] eval_mode \ref CEED_EVAL_NONE to use values directly, 13427a982d89SJeremy L. Thompson \ref CEED_EVAL_INTERP to use interpolated values, 13437a982d89SJeremy L. Thompson \ref CEED_EVAL_GRAD to use gradients, 1344c4e3f59bSSebastian Grimberg \ref CEED_EVAL_DIV to use divergence, 1345c4e3f59bSSebastian Grimberg \ref CEED_EVAL_CURL to use curl, 13467a982d89SJeremy L. Thompson \ref CEED_EVAL_WEIGHT to use quadrature weights. 13477a982d89SJeremy L. Thompson @param[in] u Input CeedVector 13487a982d89SJeremy L. Thompson @param[out] v Output CeedVector 13497a982d89SJeremy L. Thompson 13507a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 13517a982d89SJeremy L. Thompson 13527a982d89SJeremy L. Thompson @ref User 13537a982d89SJeremy L. Thompson **/ 13542b730f8bSJeremy L Thompson int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 13551f9221feSJeremy L Thompson CeedSize u_length = 0, v_length; 1356c4e3f59bSSebastian Grimberg CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 13572b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 13582b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 1359c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 13602b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 13612b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 13622b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(v, &v_length)); 13637a982d89SJeremy L. Thompson if (u) { 13642b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(u, &u_length)); 13657a982d89SJeremy L. Thompson } 13667a982d89SJeremy L. Thompson 13676574a04fSJeremy L Thompson CeedCheck(basis->Apply, basis->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support BasisApply"); 1368e15f9bd0SJeremy L Thompson 1369e15f9bd0SJeremy L Thompson // Check compatibility of topological and geometrical dimensions 13706574a04fSJeremy L Thompson CeedCheck((t_mode == CEED_TRANSPOSE && v_length % num_nodes == 0 && u_length % num_qpts == 0) || 13716574a04fSJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && u_length % num_nodes == 0 && v_length % num_qpts == 0), 13726574a04fSJeremy L Thompson basis->ceed, CEED_ERROR_DIMENSION, "Length of input/output vectors incompatible with basis dimensions"); 13737a982d89SJeremy L. Thompson 1374e15f9bd0SJeremy L Thompson // Check vector lengths to prevent out of bounds issues 13756574a04fSJeremy L Thompson bool good_dims = true; 1376d1d35e2fSjeremylt switch (eval_mode) { 1377e15f9bd0SJeremy L Thompson case CEED_EVAL_NONE: 13782b730f8bSJeremy L Thompson case CEED_EVAL_INTERP: 13792b730f8bSJeremy L Thompson case CEED_EVAL_GRAD: 1380c4e3f59bSSebastian Grimberg case CEED_EVAL_DIV: 1381c4e3f59bSSebastian Grimberg case CEED_EVAL_CURL: 13826574a04fSJeremy L Thompson good_dims = 13836574a04fSJeremy L Thompson ((t_mode == CEED_TRANSPOSE && u_length >= num_elem * num_comp * num_qpts * q_comp && v_length >= num_elem * num_comp * num_nodes) || 13846574a04fSJeremy L Thompson (t_mode == CEED_NOTRANSPOSE && v_length >= num_elem * num_qpts * num_comp * q_comp && u_length >= num_elem * num_comp * num_nodes)); 1385e15f9bd0SJeremy L Thompson break; 1386e15f9bd0SJeremy L Thompson case CEED_EVAL_WEIGHT: 13876574a04fSJeremy L Thompson good_dims = v_length >= num_elem * num_qpts; 1388e15f9bd0SJeremy L Thompson break; 1389e15f9bd0SJeremy L Thompson } 13906574a04fSJeremy L Thompson CeedCheck(good_dims, basis->ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 1391e15f9bd0SJeremy L Thompson 13922b730f8bSJeremy L Thompson CeedCall(basis->Apply(basis, num_elem, t_mode, eval_mode, u, v)); 1393e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 13947a982d89SJeremy L. Thompson } 13957a982d89SJeremy L. Thompson 13967a982d89SJeremy L. Thompson /** 1397b7c9bbdaSJeremy L Thompson @brief Get Ceed associated with a CeedBasis 1398b7c9bbdaSJeremy L Thompson 1399ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1400b7c9bbdaSJeremy L Thompson @param[out] ceed Variable to store Ceed 1401b7c9bbdaSJeremy L Thompson 1402b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1403b7c9bbdaSJeremy L Thompson 1404b7c9bbdaSJeremy L Thompson @ref Advanced 1405b7c9bbdaSJeremy L Thompson **/ 1406b7c9bbdaSJeremy L Thompson int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) { 1407b7c9bbdaSJeremy L Thompson *ceed = basis->ceed; 1408b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1409b7c9bbdaSJeremy L Thompson } 1410b7c9bbdaSJeremy L Thompson 1411b7c9bbdaSJeremy L Thompson /** 14129d007619Sjeremylt @brief Get dimension for given CeedBasis 14139d007619Sjeremylt 1414ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 14159d007619Sjeremylt @param[out] dim Variable to store dimension of basis 14169d007619Sjeremylt 14179d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 14189d007619Sjeremylt 1419b7c9bbdaSJeremy L Thompson @ref Advanced 14209d007619Sjeremylt **/ 14219d007619Sjeremylt int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) { 14229d007619Sjeremylt *dim = basis->dim; 1423e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14249d007619Sjeremylt } 14259d007619Sjeremylt 14269d007619Sjeremylt /** 1427d99fa3c5SJeremy L Thompson @brief Get topology for given CeedBasis 1428d99fa3c5SJeremy L Thompson 1429ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1430d99fa3c5SJeremy L Thompson @param[out] topo Variable to store topology of basis 1431d99fa3c5SJeremy L Thompson 1432d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1433d99fa3c5SJeremy L Thompson 1434b7c9bbdaSJeremy L Thompson @ref Advanced 1435d99fa3c5SJeremy L Thompson **/ 1436d99fa3c5SJeremy L Thompson int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) { 1437d99fa3c5SJeremy L Thompson *topo = basis->topo; 1438e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1439d99fa3c5SJeremy L Thompson } 1440d99fa3c5SJeremy L Thompson 1441d99fa3c5SJeremy L Thompson /** 14429d007619Sjeremylt @brief Get number of components for given CeedBasis 14439d007619Sjeremylt 1444ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1445d1d35e2fSjeremylt @param[out] num_comp Variable to store number of components of basis 14469d007619Sjeremylt 14479d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 14489d007619Sjeremylt 1449b7c9bbdaSJeremy L Thompson @ref Advanced 14509d007619Sjeremylt **/ 1451d1d35e2fSjeremylt int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) { 1452d1d35e2fSjeremylt *num_comp = basis->num_comp; 1453e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14549d007619Sjeremylt } 14559d007619Sjeremylt 14569d007619Sjeremylt /** 14579d007619Sjeremylt @brief Get total number of nodes (in dim dimensions) of a CeedBasis 14589d007619Sjeremylt 1459ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 14609d007619Sjeremylt @param[out] P Variable to store number of nodes 14619d007619Sjeremylt 14629d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 14639d007619Sjeremylt 14649d007619Sjeremylt @ref Utility 14659d007619Sjeremylt **/ 14669d007619Sjeremylt int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) { 14679d007619Sjeremylt *P = basis->P; 1468e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14699d007619Sjeremylt } 14709d007619Sjeremylt 14719d007619Sjeremylt /** 14729d007619Sjeremylt @brief Get total number of nodes (in 1 dimension) of a CeedBasis 14739d007619Sjeremylt 1474ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1475d1d35e2fSjeremylt @param[out] P_1d Variable to store number of nodes 14769d007619Sjeremylt 14779d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 14789d007619Sjeremylt 1479b7c9bbdaSJeremy L Thompson @ref Advanced 14809d007619Sjeremylt **/ 1481d1d35e2fSjeremylt int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) { 14826402da51SJeremy L Thompson CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "Cannot supply P_1d for non-tensor basis"); 1483d1d35e2fSjeremylt *P_1d = basis->P_1d; 1484e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14859d007619Sjeremylt } 14869d007619Sjeremylt 14879d007619Sjeremylt /** 14889d007619Sjeremylt @brief Get total number of quadrature points (in dim dimensions) of a CeedBasis 14899d007619Sjeremylt 1490ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 14919d007619Sjeremylt @param[out] Q Variable to store number of quadrature points 14929d007619Sjeremylt 14939d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 14949d007619Sjeremylt 14959d007619Sjeremylt @ref Utility 14969d007619Sjeremylt **/ 14979d007619Sjeremylt int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) { 14989d007619Sjeremylt *Q = basis->Q; 1499e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15009d007619Sjeremylt } 15019d007619Sjeremylt 15029d007619Sjeremylt /** 15039d007619Sjeremylt @brief Get total number of quadrature points (in 1 dimension) of a CeedBasis 15049d007619Sjeremylt 1505ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1506d1d35e2fSjeremylt @param[out] Q_1d Variable to store number of quadrature points 15079d007619Sjeremylt 15089d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 15099d007619Sjeremylt 1510b7c9bbdaSJeremy L Thompson @ref Advanced 15119d007619Sjeremylt **/ 1512d1d35e2fSjeremylt int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) { 15136402da51SJeremy L Thompson CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "Cannot supply Q_1d for non-tensor basis"); 1514d1d35e2fSjeremylt *Q_1d = basis->Q_1d; 1515e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15169d007619Sjeremylt } 15179d007619Sjeremylt 15189d007619Sjeremylt /** 1519ea61e9acSJeremy L Thompson @brief Get reference coordinates of quadrature points (in dim dimensions) of a CeedBasis 15209d007619Sjeremylt 1521ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1522d1d35e2fSjeremylt @param[out] q_ref Variable to store reference coordinates of quadrature points 15239d007619Sjeremylt 15249d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 15259d007619Sjeremylt 1526b7c9bbdaSJeremy L Thompson @ref Advanced 15279d007619Sjeremylt **/ 1528d1d35e2fSjeremylt int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) { 1529d1d35e2fSjeremylt *q_ref = basis->q_ref_1d; 1530e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15319d007619Sjeremylt } 15329d007619Sjeremylt 15339d007619Sjeremylt /** 1534ea61e9acSJeremy L Thompson @brief Get quadrature weights of quadrature points (in dim dimensions) of a CeedBasis 15359d007619Sjeremylt 1536ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1537d1d35e2fSjeremylt @param[out] q_weight Variable to store quadrature weights 15389d007619Sjeremylt 15399d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 15409d007619Sjeremylt 1541b7c9bbdaSJeremy L Thompson @ref Advanced 15429d007619Sjeremylt **/ 1543d1d35e2fSjeremylt int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) { 1544d1d35e2fSjeremylt *q_weight = basis->q_weight_1d; 1545e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15469d007619Sjeremylt } 15479d007619Sjeremylt 15489d007619Sjeremylt /** 15499d007619Sjeremylt @brief Get interpolation matrix of a CeedBasis 15509d007619Sjeremylt 1551ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 15529d007619Sjeremylt @param[out] interp Variable to store interpolation matrix 15539d007619Sjeremylt 15549d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 15559d007619Sjeremylt 1556b7c9bbdaSJeremy L Thompson @ref Advanced 15579d007619Sjeremylt **/ 15586c58de82SJeremy L Thompson int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) { 15596402da51SJeremy L Thompson if (!basis->interp && basis->is_tensor_basis) { 15609d007619Sjeremylt // Allocate 15612b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->Q * basis->P, &basis->interp)); 15629d007619Sjeremylt 15639d007619Sjeremylt // Initialize 15642b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->Q * basis->P; i++) basis->interp[i] = 1.0; 15659d007619Sjeremylt 15669d007619Sjeremylt // Calculate 15672b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 15682b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 15699d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 1570d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 1571d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 1572d1d35e2fSjeremylt basis->interp[qpt * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 15739d007619Sjeremylt } 15749d007619Sjeremylt } 15752b730f8bSJeremy L Thompson } 15762b730f8bSJeremy L Thompson } 15779d007619Sjeremylt *interp = basis->interp; 1578e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15799d007619Sjeremylt } 15809d007619Sjeremylt 15819d007619Sjeremylt /** 15829d007619Sjeremylt @brief Get 1D interpolation matrix of a tensor product CeedBasis 15839d007619Sjeremylt 1584ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1585d1d35e2fSjeremylt @param[out] interp_1d Variable to store interpolation matrix 15869d007619Sjeremylt 15879d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 15889d007619Sjeremylt 15899d007619Sjeremylt @ref Backend 15909d007619Sjeremylt **/ 1591d1d35e2fSjeremylt int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) { 15926402da51SJeremy L Thompson CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "CeedBasis is not a tensor product basis."); 1593d1d35e2fSjeremylt *interp_1d = basis->interp_1d; 1594e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15959d007619Sjeremylt } 15969d007619Sjeremylt 15979d007619Sjeremylt /** 15989d007619Sjeremylt @brief Get gradient matrix of a CeedBasis 15999d007619Sjeremylt 1600ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 16019d007619Sjeremylt @param[out] grad Variable to store gradient matrix 16029d007619Sjeremylt 16039d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 16049d007619Sjeremylt 1605b7c9bbdaSJeremy L Thompson @ref Advanced 16069d007619Sjeremylt **/ 16076c58de82SJeremy L Thompson int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) { 16086402da51SJeremy L Thompson if (!basis->grad && basis->is_tensor_basis) { 16099d007619Sjeremylt // Allocate 16102b730f8bSJeremy L Thompson CeedCall(CeedMalloc(basis->dim * basis->Q * basis->P, &basis->grad)); 16119d007619Sjeremylt 16129d007619Sjeremylt // Initialize 16132b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim * basis->Q * basis->P; i++) basis->grad[i] = 1.0; 16149d007619Sjeremylt 16159d007619Sjeremylt // Calculate 16162b730f8bSJeremy L Thompson for (CeedInt d = 0; d < basis->dim; d++) { 16172b730f8bSJeremy L Thompson for (CeedInt i = 0; i < basis->dim; i++) { 16182b730f8bSJeremy L Thompson for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 16199d007619Sjeremylt for (CeedInt node = 0; node < basis->P; node++) { 1620d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 1621d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 16222b730f8bSJeremy L Thompson if (i == d) basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->grad_1d[q * basis->P_1d + p]; 16232b730f8bSJeremy L Thompson else basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 16242b730f8bSJeremy L Thompson } 16252b730f8bSJeremy L Thompson } 16262b730f8bSJeremy L Thompson } 16279d007619Sjeremylt } 16289d007619Sjeremylt } 16299d007619Sjeremylt *grad = basis->grad; 1630e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 16319d007619Sjeremylt } 16329d007619Sjeremylt 16339d007619Sjeremylt /** 16349d007619Sjeremylt @brief Get 1D gradient matrix of a tensor product CeedBasis 16359d007619Sjeremylt 1636ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 1637d1d35e2fSjeremylt @param[out] grad_1d Variable to store gradient matrix 16389d007619Sjeremylt 16399d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 16409d007619Sjeremylt 1641b7c9bbdaSJeremy L Thompson @ref Advanced 16429d007619Sjeremylt **/ 1643d1d35e2fSjeremylt int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) { 16446402da51SJeremy L Thompson CeedCheck(basis->is_tensor_basis, basis->ceed, CEED_ERROR_MINOR, "CeedBasis is not a tensor product basis."); 1645d1d35e2fSjeremylt *grad_1d = basis->grad_1d; 1646e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 16479d007619Sjeremylt } 16489d007619Sjeremylt 16499d007619Sjeremylt /** 165050c301a5SRezgar Shakeri @brief Get divergence matrix of a CeedBasis 165150c301a5SRezgar Shakeri 1652ea61e9acSJeremy L Thompson @param[in] basis CeedBasis 165350c301a5SRezgar Shakeri @param[out] div Variable to store divergence matrix 165450c301a5SRezgar Shakeri 165550c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 165650c301a5SRezgar Shakeri 165750c301a5SRezgar Shakeri @ref Advanced 165850c301a5SRezgar Shakeri **/ 165950c301a5SRezgar Shakeri int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) { 16606574a04fSJeremy L Thompson CeedCheck(basis->div, basis->ceed, CEED_ERROR_MINOR, "CeedBasis does not have divergence matrix."); 166150c301a5SRezgar Shakeri *div = basis->div; 166250c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 166350c301a5SRezgar Shakeri } 166450c301a5SRezgar Shakeri 166550c301a5SRezgar Shakeri /** 1666c4e3f59bSSebastian Grimberg @brief Get curl matrix of a CeedBasis 1667c4e3f59bSSebastian Grimberg 1668c4e3f59bSSebastian Grimberg @param[in] basis CeedBasis 1669c4e3f59bSSebastian Grimberg @param[out] curl Variable to store curl matrix 1670c4e3f59bSSebastian Grimberg 1671c4e3f59bSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1672c4e3f59bSSebastian Grimberg 1673c4e3f59bSSebastian Grimberg @ref Advanced 1674c4e3f59bSSebastian Grimberg **/ 1675c4e3f59bSSebastian Grimberg int CeedBasisGetCurl(CeedBasis basis, const CeedScalar **curl) { 16766574a04fSJeremy L Thompson CeedCheck(basis->curl, basis->ceed, CEED_ERROR_MINOR, "CeedBasis does not have curl matrix."); 1677c4e3f59bSSebastian Grimberg *curl = basis->curl; 1678c4e3f59bSSebastian Grimberg return CEED_ERROR_SUCCESS; 1679c4e3f59bSSebastian Grimberg } 1680c4e3f59bSSebastian Grimberg 1681c4e3f59bSSebastian Grimberg /** 16827a982d89SJeremy L. Thompson @brief Destroy a CeedBasis 16837a982d89SJeremy L. Thompson 1684ea61e9acSJeremy L Thompson @param[in,out] basis CeedBasis to destroy 16857a982d89SJeremy L. Thompson 16867a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 16877a982d89SJeremy L. Thompson 16887a982d89SJeremy L. Thompson @ref User 16897a982d89SJeremy L. Thompson **/ 16907a982d89SJeremy L. Thompson int CeedBasisDestroy(CeedBasis *basis) { 16917425e127SJeremy L Thompson if (!*basis || *basis == CEED_BASIS_COLLOCATED || --(*basis)->ref_count > 0) { 1692ad6481ceSJeremy L Thompson *basis = NULL; 1693ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 1694ad6481ceSJeremy L Thompson } 16952b730f8bSJeremy L Thompson if ((*basis)->Destroy) CeedCall((*basis)->Destroy(*basis)); 16962b730f8bSJeremy L Thompson if ((*basis)->contract) CeedCall(CeedTensorContractDestroy(&(*basis)->contract)); 1697c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_ref_1d)); 1698c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->q_weight_1d)); 16992b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp)); 17002b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->interp_1d)); 17012b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad)); 17022b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*basis)->grad_1d)); 1703c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->div)); 1704c4e3f59bSSebastian Grimberg CeedCall(CeedFree(&(*basis)->curl)); 17052b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*basis)->ceed)); 17062b730f8bSJeremy L Thompson CeedCall(CeedFree(basis)); 1707e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 17087a982d89SJeremy L. Thompson } 17097a982d89SJeremy L. Thompson 17107a982d89SJeremy L. Thompson /** 1711b11c1e72Sjeremylt @brief Construct a Gauss-Legendre quadrature 1712b11c1e72Sjeremylt 1713ea61e9acSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree 2*Q-1 exactly) 1714d1d35e2fSjeremylt @param[out] q_ref_1d Array of length Q to hold the abscissa on [-1, 1] 1715d1d35e2fSjeremylt @param[out] q_weight_1d Array of length Q to hold the weights 1716b11c1e72Sjeremylt 1717b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1718dfdf5a53Sjeremylt 1719dfdf5a53Sjeremylt @ref Utility 1720b11c1e72Sjeremylt **/ 17212b730f8bSJeremy L Thompson int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 1722d7b241e6Sjeremylt // Allocate 1723d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0 * atan(1.0); 1724d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 172592ae7e47SJeremy L Thompson for (CeedInt i = 0; i <= Q / 2; i++) { 1726d7b241e6Sjeremylt // Guess 1727d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(2 * i + 1) / ((CeedScalar)(2 * Q))); 1728d7b241e6Sjeremylt // Pn(xi) 1729d7b241e6Sjeremylt P0 = 1.0; 1730d7b241e6Sjeremylt P1 = xi; 1731d7b241e6Sjeremylt P2 = 0.0; 173292ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 1733d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 1734d7b241e6Sjeremylt P0 = P1; 1735d7b241e6Sjeremylt P1 = P2; 1736d7b241e6Sjeremylt } 1737d7b241e6Sjeremylt // First Newton Step 1738d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 1739d7b241e6Sjeremylt xi = xi - P2 / dP2; 1740d7b241e6Sjeremylt // Newton to convergence 174192ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(P2) > 10 * CEED_EPSILON; k++) { 1742d7b241e6Sjeremylt P0 = 1.0; 1743d7b241e6Sjeremylt P1 = xi; 174492ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 1745d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 1746d7b241e6Sjeremylt P0 = P1; 1747d7b241e6Sjeremylt P1 = P2; 1748d7b241e6Sjeremylt } 1749d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 1750d7b241e6Sjeremylt xi = xi - P2 / dP2; 1751d7b241e6Sjeremylt } 1752d7b241e6Sjeremylt // Save xi, wi 1753d7b241e6Sjeremylt wi = 2.0 / ((1.0 - xi * xi) * dP2 * dP2); 1754d1d35e2fSjeremylt q_weight_1d[i] = wi; 1755d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 1756d1d35e2fSjeremylt q_ref_1d[i] = -xi; 1757d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 1758d7b241e6Sjeremylt } 1759e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1760d7b241e6Sjeremylt } 1761d7b241e6Sjeremylt 1762b11c1e72Sjeremylt /** 1763b11c1e72Sjeremylt @brief Construct a Gauss-Legendre-Lobatto quadrature 1764b11c1e72Sjeremylt 1765ea61e9acSJeremy L Thompson @param[in] Q Number of quadrature points (integrates polynomials of degree 2*Q-3 exactly) 1766d1d35e2fSjeremylt @param[out] q_ref_1d Array of length Q to hold the abscissa on [-1, 1] 1767d1d35e2fSjeremylt @param[out] q_weight_1d Array of length Q to hold the weights 1768b11c1e72Sjeremylt 1769b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1770dfdf5a53Sjeremylt 1771dfdf5a53Sjeremylt @ref Utility 1772b11c1e72Sjeremylt **/ 17732b730f8bSJeremy L Thompson int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 1774d7b241e6Sjeremylt // Allocate 1775d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0 * atan(1.0); 1776d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 1777d7b241e6Sjeremylt // Set endpoints 17786574a04fSJeremy L Thompson CeedCheck(Q > 1, NULL, CEED_ERROR_DIMENSION, "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q); 1779d7b241e6Sjeremylt wi = 2.0 / ((CeedScalar)(Q * (Q - 1))); 1780d1d35e2fSjeremylt if (q_weight_1d) { 1781d1d35e2fSjeremylt q_weight_1d[0] = wi; 1782d1d35e2fSjeremylt q_weight_1d[Q - 1] = wi; 1783d7b241e6Sjeremylt } 1784d1d35e2fSjeremylt q_ref_1d[0] = -1.0; 1785d1d35e2fSjeremylt q_ref_1d[Q - 1] = 1.0; 1786d7b241e6Sjeremylt // Interior 178792ae7e47SJeremy L Thompson for (CeedInt i = 1; i <= (Q - 1) / 2; i++) { 1788d7b241e6Sjeremylt // Guess 1789d7b241e6Sjeremylt xi = cos(PI * (CeedScalar)(i) / (CeedScalar)(Q - 1)); 1790d7b241e6Sjeremylt // Pn(xi) 1791d7b241e6Sjeremylt P0 = 1.0; 1792d7b241e6Sjeremylt P1 = xi; 1793d7b241e6Sjeremylt P2 = 0.0; 179492ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 1795d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 1796d7b241e6Sjeremylt P0 = P1; 1797d7b241e6Sjeremylt P1 = P2; 1798d7b241e6Sjeremylt } 1799d7b241e6Sjeremylt // First Newton step 1800d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 1801d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 1802d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 1803d7b241e6Sjeremylt // Newton to convergence 180492ae7e47SJeremy L Thompson for (CeedInt k = 0; k < 100 && fabs(dP2) > 10 * CEED_EPSILON; k++) { 1805d7b241e6Sjeremylt P0 = 1.0; 1806d7b241e6Sjeremylt P1 = xi; 180792ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 1808d7b241e6Sjeremylt P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 1809d7b241e6Sjeremylt P0 = P1; 1810d7b241e6Sjeremylt P1 = P2; 1811d7b241e6Sjeremylt } 1812d7b241e6Sjeremylt dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 1813d7b241e6Sjeremylt d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 1814d7b241e6Sjeremylt xi = xi - dP2 / d2P2; 1815d7b241e6Sjeremylt } 1816d7b241e6Sjeremylt // Save xi, wi 1817d7b241e6Sjeremylt wi = 2.0 / (((CeedScalar)(Q * (Q - 1))) * P2 * P2); 1818d1d35e2fSjeremylt if (q_weight_1d) { 1819d1d35e2fSjeremylt q_weight_1d[i] = wi; 1820d1d35e2fSjeremylt q_weight_1d[Q - 1 - i] = wi; 1821d7b241e6Sjeremylt } 1822d1d35e2fSjeremylt q_ref_1d[i] = -xi; 1823d1d35e2fSjeremylt q_ref_1d[Q - 1 - i] = xi; 1824d7b241e6Sjeremylt } 1825e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1826d7b241e6Sjeremylt } 1827d7b241e6Sjeremylt 1828d7b241e6Sjeremylt /// @} 1829