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 8ec3da8bcSJed Brown #include <ceed/ceed.h> 9ec3da8bcSJed Brown #include <ceed/backend.h> 103d576824SJeremy L Thompson #include <ceed-impl.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 407a982d89SJeremy L. Thompson Computes A = (I - b v v^T) A 417a982d89SJeremy L. Thompson where A is an mxn matrix indexed as A[i*row + j*col] 427a982d89SJeremy L. Thompson 437a982d89SJeremy L. Thompson @param[in,out] A Matrix to apply Householder reflection to, in place 447a982d89SJeremy L. Thompson @param v Householder vector 457a982d89SJeremy L. Thompson @param b Scaling factor 467a982d89SJeremy L. Thompson @param m Number of rows in A 477a982d89SJeremy L. Thompson @param n Number of columns in A 487a982d89SJeremy L. Thompson @param row Row stride 497a982d89SJeremy L. Thompson @param col Col stride 507a982d89SJeremy L. Thompson 517a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 527a982d89SJeremy L. Thompson 537a982d89SJeremy L. Thompson @ref Developer 547a982d89SJeremy L. Thompson **/ 557a982d89SJeremy L. Thompson static int CeedHouseholderReflect(CeedScalar *A, const CeedScalar *v, 567a982d89SJeremy L. Thompson CeedScalar b, CeedInt m, CeedInt n, 577a982d89SJeremy L. Thompson CeedInt row, CeedInt col) { 587a982d89SJeremy L. Thompson for (CeedInt j=0; j<n; j++) { 597a982d89SJeremy L. Thompson CeedScalar w = A[0*row + j*col]; 607a982d89SJeremy L. Thompson for (CeedInt i=1; i<m; i++) 617a982d89SJeremy L. Thompson w += v[i] * A[i*row + j*col]; 627a982d89SJeremy L. Thompson A[0*row + j*col] -= b * w; 637a982d89SJeremy L. Thompson for (CeedInt i=1; i<m; i++) 647a982d89SJeremy L. Thompson A[i*row + j*col] -= b * w * v[i]; 657a982d89SJeremy L. Thompson } 66e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 677a982d89SJeremy L. Thompson } 687a982d89SJeremy L. Thompson 697a982d89SJeremy L. Thompson /** 707a982d89SJeremy L. Thompson @brief Apply Householder Q matrix 717a982d89SJeremy L. Thompson 727a982d89SJeremy L. Thompson Compute A = Q A where Q is mxm and A is mxn. 737a982d89SJeremy L. Thompson 747a982d89SJeremy L. Thompson @param[in,out] A Matrix to apply Householder Q to, in place 757a982d89SJeremy L. Thompson @param Q Householder Q matrix 767a982d89SJeremy L. Thompson @param tau Householder scaling factors 77d1d35e2fSjeremylt @param t_mode Transpose mode for application 787a982d89SJeremy L. Thompson @param m Number of rows in A 797a982d89SJeremy L. Thompson @param n Number of columns in A 807a982d89SJeremy L. Thompson @param k Number of elementary reflectors in Q, k<m 817a982d89SJeremy L. Thompson @param row Row stride in A 827a982d89SJeremy L. Thompson @param col Col stride in A 837a982d89SJeremy L. Thompson 847a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 857a982d89SJeremy L. Thompson 867a982d89SJeremy L. Thompson @ref Developer 877a982d89SJeremy L. Thompson **/ 88d99fa3c5SJeremy L Thompson int CeedHouseholderApplyQ(CeedScalar *A, const CeedScalar *Q, 89d1d35e2fSjeremylt const CeedScalar *tau, CeedTransposeMode t_mode, 907a982d89SJeremy L. Thompson CeedInt m, CeedInt n, CeedInt k, 917a982d89SJeremy L. Thompson CeedInt row, CeedInt col) { 92e15f9bd0SJeremy L Thompson int ierr; 9378464608Sjeremylt CeedScalar *v; 9478464608Sjeremylt ierr = CeedMalloc(m, &v); CeedChk(ierr); 957a982d89SJeremy L. Thompson for (CeedInt ii=0; ii<k; ii++) { 96d1d35e2fSjeremylt CeedInt i = t_mode == CEED_TRANSPOSE ? ii : k-1-ii; 977a982d89SJeremy L. Thompson for (CeedInt j=i+1; j<m; j++) 987a982d89SJeremy L. Thompson v[j] = Q[j*k+i]; 99d1d35e2fSjeremylt // Apply Householder reflector (I - tau v v^T) collo_grad_1d^T 100e15f9bd0SJeremy L Thompson ierr = CeedHouseholderReflect(&A[i*row], &v[i], tau[i], m-i, n, row, col); 101e15f9bd0SJeremy L Thompson CeedChk(ierr); 1027a982d89SJeremy L. Thompson } 10378464608Sjeremylt ierr = CeedFree(&v); CeedChk(ierr); 104e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1057a982d89SJeremy L. Thompson } 1067a982d89SJeremy L. Thompson 1077a982d89SJeremy L. Thompson /** 1087a982d89SJeremy L. Thompson @brief Compute Givens rotation 1097a982d89SJeremy L. Thompson 1107a982d89SJeremy L. Thompson Computes A = G A (or G^T A in transpose mode) 1117a982d89SJeremy L. Thompson where A is an mxn matrix indexed as A[i*n + j*m] 1127a982d89SJeremy L. Thompson 1137a982d89SJeremy L. Thompson @param[in,out] A Row major matrix to apply Givens rotation to, in place 1147a982d89SJeremy L. Thompson @param c Cosine factor 1157a982d89SJeremy L. Thompson @param s Sine factor 116d1d35e2fSjeremylt @param t_mode @ref CEED_NOTRANSPOSE to rotate the basis counter-clockwise, 1174c4400c7SValeria Barra which has the effect of rotating columns of A clockwise; 1184cc79fe7SJed Brown @ref CEED_TRANSPOSE for the opposite rotation 1197a982d89SJeremy L. Thompson @param i First row/column to apply rotation 1207a982d89SJeremy L. Thompson @param k Second row/column to apply rotation 1217a982d89SJeremy L. Thompson @param m Number of rows in A 1227a982d89SJeremy L. Thompson @param n Number of columns in A 1237a982d89SJeremy L. Thompson 1247a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1257a982d89SJeremy L. Thompson 1267a982d89SJeremy L. Thompson @ref Developer 1277a982d89SJeremy L. Thompson **/ 1287a982d89SJeremy L. Thompson static int CeedGivensRotation(CeedScalar *A, CeedScalar c, CeedScalar s, 129d1d35e2fSjeremylt CeedTransposeMode t_mode, CeedInt i, CeedInt k, 1307a982d89SJeremy L. Thompson CeedInt m, CeedInt n) { 131d1d35e2fSjeremylt CeedInt stride_j = 1, stride_ik = m, num_its = n; 132d1d35e2fSjeremylt if (t_mode == CEED_NOTRANSPOSE) { 133d1d35e2fSjeremylt stride_j = n; stride_ik = 1; num_its = m; 1347a982d89SJeremy L. Thompson } 1357a982d89SJeremy L. Thompson 1367a982d89SJeremy L. Thompson // Apply rotation 137d1d35e2fSjeremylt for (CeedInt j=0; j<num_its; j++) { 138d1d35e2fSjeremylt CeedScalar tau1 = A[i*stride_ik+j*stride_j], tau2 = A[k*stride_ik+j*stride_j]; 139d1d35e2fSjeremylt A[i*stride_ik+j*stride_j] = c*tau1 - s*tau2; 140d1d35e2fSjeremylt A[k*stride_ik+j*stride_j] = s*tau1 + c*tau2; 1417a982d89SJeremy L. Thompson } 142e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1437a982d89SJeremy L. Thompson } 1447a982d89SJeremy L. Thompson 1457a982d89SJeremy L. Thompson /** 1467a982d89SJeremy L. Thompson @brief View an array stored in a CeedBasis 1477a982d89SJeremy L. Thompson 1480a0da059Sjeremylt @param[in] name Name of array 149d1d35e2fSjeremylt @param[in] fp_fmt Printing format 1500a0da059Sjeremylt @param[in] m Number of rows in array 1510a0da059Sjeremylt @param[in] n Number of columns in array 1520a0da059Sjeremylt @param[in] a Array to be viewed 1530a0da059Sjeremylt @param[in] stream Stream to view to, e.g., stdout 1547a982d89SJeremy L. Thompson 1557a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1567a982d89SJeremy L. Thompson 1577a982d89SJeremy L. Thompson @ref Developer 1587a982d89SJeremy L. Thompson **/ 159d1d35e2fSjeremylt static int CeedScalarView(const char *name, const char *fp_fmt, CeedInt m, 1607a982d89SJeremy L. Thompson CeedInt n, const CeedScalar *a, FILE *stream) { 16192ae7e47SJeremy L Thompson for (CeedInt i=0; i<m; i++) { 1627a982d89SJeremy L. Thompson if (m > 1) 163*990fdeb6SJeremy L Thompson fprintf(stream, "%12s[%" CeedInt_FMT "]:", name, i); 1647a982d89SJeremy L. Thompson else 1657a982d89SJeremy L. Thompson fprintf(stream, "%12s:", name); 16692ae7e47SJeremy L Thompson for (CeedInt j=0; j<n; j++) 167d1d35e2fSjeremylt fprintf(stream, fp_fmt, fabs(a[i*n+j]) > 1E-14 ? a[i*n+j] : 0); 1687a982d89SJeremy L. Thompson fputs("\n", stream); 1697a982d89SJeremy L. Thompson } 170e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1717a982d89SJeremy L. Thompson } 1727a982d89SJeremy L. Thompson 1737a982d89SJeremy L. Thompson /// @} 1747a982d89SJeremy L. Thompson 1757a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1767a982d89SJeremy L. Thompson /// Ceed Backend API 1777a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1787a982d89SJeremy L. Thompson /// @addtogroup CeedBasisBackend 1797a982d89SJeremy L. Thompson /// @{ 1807a982d89SJeremy L. Thompson 1817a982d89SJeremy L. Thompson /** 1827a982d89SJeremy L. Thompson @brief Return collocated grad matrix 1837a982d89SJeremy L. Thompson 1847a982d89SJeremy L. Thompson @param basis CeedBasis 185d1d35e2fSjeremylt @param[out] collo_grad_1d Row-major (Q_1d * Q_1d) matrix expressing derivatives of 1867a982d89SJeremy L. Thompson basis functions at quadrature points 1877a982d89SJeremy L. Thompson 1887a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1897a982d89SJeremy L. Thompson 1907a982d89SJeremy L. Thompson @ref Backend 1917a982d89SJeremy L. Thompson **/ 192d1d35e2fSjeremylt int CeedBasisGetCollocatedGrad(CeedBasis basis, CeedScalar *collo_grad_1d) { 1937a982d89SJeremy L. Thompson int i, j, k; 1947a982d89SJeremy L. Thompson Ceed ceed; 195d1d35e2fSjeremylt CeedInt ierr, P_1d=(basis)->P_1d, Q_1d=(basis)->Q_1d; 19678464608Sjeremylt CeedScalar *interp_1d, *grad_1d, *tau; 1977a982d89SJeremy L. Thompson 198d1d35e2fSjeremylt ierr = CeedMalloc(Q_1d*P_1d, &interp_1d); CeedChk(ierr); 199d1d35e2fSjeremylt ierr = CeedMalloc(Q_1d*P_1d, &grad_1d); CeedChk(ierr); 20078464608Sjeremylt ierr = CeedMalloc(Q_1d, &tau); CeedChk(ierr); 201d1d35e2fSjeremylt memcpy(interp_1d, (basis)->interp_1d, Q_1d*P_1d*sizeof(basis)->interp_1d[0]); 202d1d35e2fSjeremylt memcpy(grad_1d, (basis)->grad_1d, Q_1d*P_1d*sizeof(basis)->interp_1d[0]); 2037a982d89SJeremy L. Thompson 204d1d35e2fSjeremylt // QR Factorization, interp_1d = Q R 2057a982d89SJeremy L. Thompson ierr = CeedBasisGetCeed(basis, &ceed); CeedChk(ierr); 206d1d35e2fSjeremylt ierr = CeedQRFactorization(ceed, interp_1d, tau, Q_1d, P_1d); CeedChk(ierr); 207e15f9bd0SJeremy L Thompson // Note: This function is for backend use, so all errors are terminal 208e15f9bd0SJeremy L Thompson // and we do not need to clean up memory on failure. 2097a982d89SJeremy L. Thompson 210d1d35e2fSjeremylt // Apply Rinv, collo_grad_1d = grad_1d Rinv 211d1d35e2fSjeremylt for (i=0; i<Q_1d; i++) { // Row i 212d1d35e2fSjeremylt collo_grad_1d[Q_1d*i] = grad_1d[P_1d*i]/interp_1d[0]; 213d1d35e2fSjeremylt for (j=1; j<P_1d; j++) { // Column j 214d1d35e2fSjeremylt collo_grad_1d[j+Q_1d*i] = grad_1d[j+P_1d*i]; 2157a982d89SJeremy L. Thompson for (k=0; k<j; k++) 216d1d35e2fSjeremylt collo_grad_1d[j+Q_1d*i] -= interp_1d[j+P_1d*k]*collo_grad_1d[k+Q_1d*i]; 217d1d35e2fSjeremylt collo_grad_1d[j+Q_1d*i] /= interp_1d[j+P_1d*j]; 2187a982d89SJeremy L. Thompson } 219d1d35e2fSjeremylt for (j=P_1d; j<Q_1d; j++) 220d1d35e2fSjeremylt collo_grad_1d[j+Q_1d*i] = 0; 2217a982d89SJeremy L. Thompson } 2227a982d89SJeremy L. Thompson 223673160d7Sjeremylt // Apply Qtranspose, collo_grad = collo_grad Q_transpose 224d1d35e2fSjeremylt ierr = CeedHouseholderApplyQ(collo_grad_1d, interp_1d, tau, CEED_NOTRANSPOSE, 225d1d35e2fSjeremylt Q_1d, Q_1d, P_1d, 1, Q_1d); CeedChk(ierr); 2267a982d89SJeremy L. Thompson 227d1d35e2fSjeremylt ierr = CeedFree(&interp_1d); CeedChk(ierr); 228d1d35e2fSjeremylt ierr = CeedFree(&grad_1d); CeedChk(ierr); 22978464608Sjeremylt ierr = CeedFree(&tau); CeedChk(ierr); 230e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2317a982d89SJeremy L. Thompson } 2327a982d89SJeremy L. Thompson 2337a982d89SJeremy L. Thompson /** 2347a982d89SJeremy L. Thompson @brief Get tensor status for given CeedBasis 2357a982d89SJeremy L. Thompson 2367a982d89SJeremy L. Thompson @param basis CeedBasis 237d1d35e2fSjeremylt @param[out] is_tensor Variable to store tensor status 2387a982d89SJeremy L. Thompson 2397a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 2407a982d89SJeremy L. Thompson 2417a982d89SJeremy L. Thompson @ref Backend 2427a982d89SJeremy L. Thompson **/ 243d1d35e2fSjeremylt int CeedBasisIsTensor(CeedBasis basis, bool *is_tensor) { 244d1d35e2fSjeremylt *is_tensor = basis->tensor_basis; 245e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2467a982d89SJeremy L. Thompson } 2477a982d89SJeremy L. Thompson 2487a982d89SJeremy L. Thompson /** 2497a982d89SJeremy L. Thompson @brief Get backend data of a CeedBasis 2507a982d89SJeremy L. Thompson 2517a982d89SJeremy L. Thompson @param basis CeedBasis 2527a982d89SJeremy L. Thompson @param[out] data Variable to store data 2537a982d89SJeremy L. Thompson 2547a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 2557a982d89SJeremy L. Thompson 2567a982d89SJeremy L. Thompson @ref Backend 2577a982d89SJeremy L. Thompson **/ 258777ff853SJeremy L Thompson int CeedBasisGetData(CeedBasis basis, void *data) { 259777ff853SJeremy L Thompson *(void **)data = basis->data; 260e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2617a982d89SJeremy L. Thompson } 2627a982d89SJeremy L. Thompson 2637a982d89SJeremy L. Thompson /** 2647a982d89SJeremy L. Thompson @brief Set backend data of a CeedBasis 2657a982d89SJeremy L. Thompson 2667a982d89SJeremy L. Thompson @param[out] basis CeedBasis 2677a982d89SJeremy L. Thompson @param data Data to set 2687a982d89SJeremy L. Thompson 2697a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 2707a982d89SJeremy L. Thompson 2717a982d89SJeremy L. Thompson @ref Backend 2727a982d89SJeremy L. Thompson **/ 273777ff853SJeremy L Thompson int CeedBasisSetData(CeedBasis basis, void *data) { 274777ff853SJeremy L Thompson basis->data = data; 275e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2767a982d89SJeremy L. Thompson } 2777a982d89SJeremy L. Thompson 2787a982d89SJeremy L. Thompson /** 27934359f16Sjeremylt @brief Increment the reference counter for a CeedBasis 28034359f16Sjeremylt 28134359f16Sjeremylt @param basis Basis to increment the reference counter 28234359f16Sjeremylt 28334359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 28434359f16Sjeremylt 28534359f16Sjeremylt @ref Backend 28634359f16Sjeremylt **/ 2879560d06aSjeremylt int CeedBasisReference(CeedBasis basis) { 28834359f16Sjeremylt basis->ref_count++; 28934359f16Sjeremylt return CEED_ERROR_SUCCESS; 29034359f16Sjeremylt } 29134359f16Sjeremylt 29234359f16Sjeremylt /** 2936e15d496SJeremy L Thompson @brief Estimate number of FLOPs required to apply CeedBasis in t_mode and eval_mode 2946e15d496SJeremy L Thompson 2956e15d496SJeremy L Thompson @param basis Basis to estimate FLOPs for 2966e15d496SJeremy L Thompson @param t_mode Apply basis or transpose 2976e15d496SJeremy L Thompson @param eval_mode Basis evaluation mode 2986e15d496SJeremy L Thompson @param flops Address of variable to hold FLOPs estimate 2996e15d496SJeremy L Thompson 3006e15d496SJeremy L Thompson @ref Backend 3016e15d496SJeremy L Thompson **/ 3026e15d496SJeremy L Thompson int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, 3039d36ca50SJeremy L Thompson CeedEvalMode eval_mode, CeedSize *flops) { 3046e15d496SJeremy L Thompson int ierr; 3056e15d496SJeremy L Thompson bool is_tensor; 3066e15d496SJeremy L Thompson 3076e15d496SJeremy L Thompson ierr = CeedBasisIsTensor(basis, &is_tensor); CeedChk(ierr); 3086e15d496SJeremy L Thompson if (is_tensor) { 3096e15d496SJeremy L Thompson CeedInt dim, num_comp, P_1d, Q_1d; 3106e15d496SJeremy L Thompson ierr = CeedBasisGetDimension(basis, &dim); CeedChk(ierr); 3116e15d496SJeremy L Thompson ierr = CeedBasisGetNumComponents(basis, &num_comp); CeedChk(ierr); 3126e15d496SJeremy L Thompson ierr = CeedBasisGetNumNodes1D(basis, &P_1d); CeedChk(ierr); 3136e15d496SJeremy L Thompson ierr = CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d); CeedChk(ierr); 3146e15d496SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 3156e15d496SJeremy L Thompson P_1d = Q_1d; Q_1d = P_1d; 3166e15d496SJeremy L Thompson } 3176e15d496SJeremy L Thompson CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim-1), post = 1; 3186e15d496SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 3196e15d496SJeremy L Thompson tensor_flops += 2 * pre * P_1d * post * Q_1d; 3206e15d496SJeremy L Thompson pre /= P_1d; 3216e15d496SJeremy L Thompson post *= Q_1d; 3226e15d496SJeremy L Thompson } 3236e15d496SJeremy L Thompson switch (eval_mode) { 3246e15d496SJeremy L Thompson case CEED_EVAL_NONE: *flops = 0; break; 3256e15d496SJeremy L Thompson case CEED_EVAL_INTERP: *flops = tensor_flops; break; 3266e15d496SJeremy L Thompson case CEED_EVAL_GRAD: *flops = tensor_flops * 2; break; 3276e15d496SJeremy L Thompson case CEED_EVAL_DIV: 3286e15d496SJeremy L Thompson // LCOV_EXCL_START 3296e15d496SJeremy L Thompson return CeedError(basis->ceed, CEED_ERROR_INCOMPATIBLE, 3306e15d496SJeremy L Thompson "Tensor CEED_EVAL_DIV not supported"); break; 3316e15d496SJeremy L Thompson case CEED_EVAL_CURL: 3326e15d496SJeremy L Thompson return CeedError(basis->ceed, CEED_ERROR_INCOMPATIBLE, 3336e15d496SJeremy L Thompson "Tensor CEED_EVAL_CURL not supported"); break; 3346e15d496SJeremy L Thompson // LCOV_EXCL_STOP 3356e15d496SJeremy L Thompson case CEED_EVAL_WEIGHT: *flops = dim * CeedIntPow(Q_1d, dim); break; 3366e15d496SJeremy L Thompson } 3376e15d496SJeremy L Thompson } else { 3386e15d496SJeremy L Thompson CeedInt dim, num_comp, num_nodes, num_qpts, Q_comp; 3396e15d496SJeremy L Thompson ierr = CeedBasisGetDimension(basis, &dim); CeedChk(ierr); 3406e15d496SJeremy L Thompson ierr = CeedBasisGetNumComponents(basis, &num_comp); CeedChk(ierr); 3416e15d496SJeremy L Thompson ierr = CeedBasisGetNumNodes(basis, &num_nodes); CeedChk(ierr); 3426e15d496SJeremy L Thompson ierr = CeedBasisGetNumQuadraturePoints(basis, &num_qpts); CeedChk(ierr); 3436e15d496SJeremy L Thompson ierr = CeedBasisGetNumQuadratureComponents(basis, &Q_comp); CeedChk(ierr); 3446e15d496SJeremy L Thompson switch (eval_mode) { 3456e15d496SJeremy L Thompson case CEED_EVAL_NONE: *flops = 0; break; 3466e15d496SJeremy L Thompson case CEED_EVAL_INTERP: *flops = num_nodes * num_qpts * num_comp; break; 3476e15d496SJeremy L Thompson case CEED_EVAL_GRAD: *flops = num_nodes * num_qpts * num_comp * dim; break; 3486e15d496SJeremy L Thompson case CEED_EVAL_DIV: *flops = num_nodes * num_qpts; break; 3496e15d496SJeremy L Thompson case CEED_EVAL_CURL: *flops = num_nodes * num_qpts * dim; break; 3506e15d496SJeremy L Thompson case CEED_EVAL_WEIGHT: *flops = 0; break; 3516e15d496SJeremy L Thompson } 3526e15d496SJeremy L Thompson } 3536e15d496SJeremy L Thompson 3546e15d496SJeremy L Thompson return CEED_ERROR_SUCCESS; 3556e15d496SJeremy L Thompson } 3566e15d496SJeremy L Thompson 3576e15d496SJeremy L Thompson /** 3587a982d89SJeremy L. Thompson @brief Get dimension for given CeedElemTopology 3597a982d89SJeremy L. Thompson 3607a982d89SJeremy L. Thompson @param topo CeedElemTopology 3617a982d89SJeremy L. Thompson @param[out] dim Variable to store dimension of topology 3627a982d89SJeremy L. Thompson 3637a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3647a982d89SJeremy L. Thompson 3657a982d89SJeremy L. Thompson @ref Backend 3667a982d89SJeremy L. Thompson **/ 3677a982d89SJeremy L. Thompson int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim) { 3687a982d89SJeremy L. Thompson *dim = (CeedInt) topo >> 16; 369e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3707a982d89SJeremy L. Thompson } 3717a982d89SJeremy L. Thompson 3727a982d89SJeremy L. Thompson /** 3737a982d89SJeremy L. Thompson @brief Get CeedTensorContract of a CeedBasis 3747a982d89SJeremy L. Thompson 3757a982d89SJeremy L. Thompson @param basis CeedBasis 3767a982d89SJeremy L. Thompson @param[out] contract Variable to store CeedTensorContract 3777a982d89SJeremy L. Thompson 3787a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3797a982d89SJeremy L. Thompson 3807a982d89SJeremy L. Thompson @ref Backend 3817a982d89SJeremy L. Thompson **/ 3827a982d89SJeremy L. Thompson int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract) { 3837a982d89SJeremy L. Thompson *contract = basis->contract; 384e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3857a982d89SJeremy L. Thompson } 3867a982d89SJeremy L. Thompson 3877a982d89SJeremy L. Thompson /** 3887a982d89SJeremy L. Thompson @brief Set CeedTensorContract of a CeedBasis 3897a982d89SJeremy L. Thompson 3907a982d89SJeremy L. Thompson @param[out] basis CeedBasis 3917a982d89SJeremy L. Thompson @param contract CeedTensorContract to set 3927a982d89SJeremy L. Thompson 3937a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3947a982d89SJeremy L. Thompson 3957a982d89SJeremy L. Thompson @ref Backend 3967a982d89SJeremy L. Thompson **/ 39734359f16Sjeremylt int CeedBasisSetTensorContract(CeedBasis basis, CeedTensorContract contract) { 39834359f16Sjeremylt int ierr; 39934359f16Sjeremylt basis->contract = contract; 4009560d06aSjeremylt ierr = CeedTensorContractReference(contract); CeedChk(ierr); 401e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4027a982d89SJeremy L. Thompson } 4037a982d89SJeremy L. Thompson 4047a982d89SJeremy L. Thompson /** 4057a982d89SJeremy L. Thompson @brief Return a reference implementation of matrix multiplication C = A B. 4067a982d89SJeremy L. Thompson Note, this is a reference implementation for CPU CeedScalar pointers 4077a982d89SJeremy L. Thompson that is not intended for high performance. 4087a982d89SJeremy L. Thompson 4097a982d89SJeremy L. Thompson @param ceed A Ceed context for error handling 410d1d35e2fSjeremylt @param[in] mat_A Row-major matrix A 411d1d35e2fSjeremylt @param[in] mat_B Row-major matrix B 412d1d35e2fSjeremylt @param[out] mat_C Row-major output matrix C 4137a982d89SJeremy L. Thompson @param m Number of rows of C 4147a982d89SJeremy L. Thompson @param n Number of columns of C 4157a982d89SJeremy L. Thompson @param kk Number of columns of A/rows of B 4167a982d89SJeremy L. Thompson 4177a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4187a982d89SJeremy L. Thompson 4197a982d89SJeremy L. Thompson @ref Utility 4207a982d89SJeremy L. Thompson **/ 421ed9e99e6SJeremy L Thompson int CeedMatrixMatrixMultiply(Ceed ceed, const CeedScalar *mat_A, 422ed9e99e6SJeremy L Thompson const CeedScalar *mat_B, CeedScalar *mat_C, 423ed9e99e6SJeremy L Thompson CeedInt m, CeedInt n, CeedInt kk) { 4247a982d89SJeremy L. Thompson for (CeedInt i=0; i<m; i++) 4257a982d89SJeremy L. Thompson for (CeedInt j=0; j<n; j++) { 4267a982d89SJeremy L. Thompson CeedScalar sum = 0; 4277a982d89SJeremy L. Thompson for (CeedInt k=0; k<kk; k++) 428d1d35e2fSjeremylt sum += mat_A[k+i*kk]*mat_B[j+k*n]; 429d1d35e2fSjeremylt mat_C[j+i*n] = sum; 4307a982d89SJeremy L. Thompson } 431e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4327a982d89SJeremy L. Thompson } 4337a982d89SJeremy L. Thompson 4347a982d89SJeremy L. Thompson /// @} 4357a982d89SJeremy L. Thompson 4367a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 4377a982d89SJeremy L. Thompson /// CeedBasis Public API 4387a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 4397a982d89SJeremy L. Thompson /// @addtogroup CeedBasisUser 440d7b241e6Sjeremylt /// @{ 441d7b241e6Sjeremylt 442b11c1e72Sjeremylt /** 44395bb1877Svaleriabarra @brief Create a tensor-product basis for H^1 discretizations 444b11c1e72Sjeremylt 445b11c1e72Sjeremylt @param ceed A Ceed object where the CeedBasis will be created 446b11c1e72Sjeremylt @param dim Topological dimension 447d1d35e2fSjeremylt @param num_comp Number of field components (1 for scalar fields) 448d1d35e2fSjeremylt @param P_1d Number of nodes in one dimension 449d1d35e2fSjeremylt @param Q_1d Number of quadrature points in one dimension 450d1d35e2fSjeremylt @param interp_1d Row-major (Q_1d * P_1d) matrix expressing the values of nodal 451b11c1e72Sjeremylt basis functions at quadrature points 452d1d35e2fSjeremylt @param grad_1d Row-major (Q_1d * P_1d) matrix expressing derivatives of nodal 453b11c1e72Sjeremylt basis functions at quadrature points 454d1d35e2fSjeremylt @param q_ref_1d Array of length Q_1d holding the locations of quadrature points 455b11c1e72Sjeremylt on the 1D reference element [-1, 1] 456d1d35e2fSjeremylt @param q_weight_1d Array of length Q_1d holding the quadrature weights on the 457b11c1e72Sjeremylt reference element 458b11c1e72Sjeremylt @param[out] basis Address of the variable where the newly created 459b11c1e72Sjeremylt CeedBasis will be stored. 460b11c1e72Sjeremylt 461b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 462dfdf5a53Sjeremylt 4637a982d89SJeremy L. Thompson @ref User 464b11c1e72Sjeremylt **/ 465d1d35e2fSjeremylt int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, 466d1d35e2fSjeremylt CeedInt P_1d, CeedInt Q_1d, 467d1d35e2fSjeremylt const CeedScalar *interp_1d, 468d1d35e2fSjeremylt const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, 469d1d35e2fSjeremylt const CeedScalar *q_weight_1d, CeedBasis *basis) { 470d7b241e6Sjeremylt int ierr; 471d7b241e6Sjeremylt 4725fe0d4faSjeremylt if (!ceed->BasisCreateTensorH1) { 4735fe0d4faSjeremylt Ceed delegate; 474aefd8378Sjeremylt ierr = CeedGetObjectDelegate(ceed, &delegate, "Basis"); CeedChk(ierr); 4755fe0d4faSjeremylt 4765fe0d4faSjeremylt if (!delegate) 477c042f62fSJeremy L Thompson // LCOV_EXCL_START 478e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, 479e15f9bd0SJeremy L Thompson "Backend does not support BasisCreateTensorH1"); 480c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 4815fe0d4faSjeremylt 482d1d35e2fSjeremylt ierr = CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, 483d1d35e2fSjeremylt Q_1d, interp_1d, grad_1d, q_ref_1d, 484d1d35e2fSjeremylt q_weight_1d, basis); CeedChk(ierr); 485e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4865fe0d4faSjeremylt } 487e15f9bd0SJeremy L Thompson 488e15f9bd0SJeremy L Thompson if (dim < 1) 489e15f9bd0SJeremy L Thompson // LCOV_EXCL_START 490e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 491e15f9bd0SJeremy L Thompson "Basis dimension must be a positive value"); 492e15f9bd0SJeremy L Thompson // LCOV_EXCL_STOP 493227444bfSJeremy L Thompson 494227444bfSJeremy L Thompson if (num_comp < 1) 495227444bfSJeremy L Thompson // LCOV_EXCL_START 496227444bfSJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 497227444bfSJeremy L Thompson "Basis must have at least 1 component"); 498227444bfSJeremy L Thompson // LCOV_EXCL_STOP 499227444bfSJeremy L Thompson 500227444bfSJeremy L Thompson if (P_1d < 1) 501227444bfSJeremy L Thompson // LCOV_EXCL_START 502227444bfSJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 503227444bfSJeremy L Thompson "Basis must have at least 1 node"); 504227444bfSJeremy L Thompson // LCOV_EXCL_STOP 505227444bfSJeremy L Thompson 506227444bfSJeremy L Thompson if (Q_1d < 1) 507227444bfSJeremy L Thompson // LCOV_EXCL_START 508227444bfSJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 509227444bfSJeremy L Thompson "Basis must have at least 1 quadrature point"); 510227444bfSJeremy L Thompson // LCOV_EXCL_STOP 511227444bfSJeremy L Thompson 51250c301a5SRezgar Shakeri CeedElemTopology topo = dim == 1 ? CEED_TOPOLOGY_LINE 51350c301a5SRezgar Shakeri : dim == 2 ? CEED_TOPOLOGY_QUAD 51450c301a5SRezgar Shakeri : CEED_TOPOLOGY_HEX; 515e15f9bd0SJeremy L Thompson 516d7b241e6Sjeremylt ierr = CeedCalloc(1, basis); CeedChk(ierr); 517d7b241e6Sjeremylt (*basis)->ceed = ceed; 5189560d06aSjeremylt ierr = CeedReference(ceed); CeedChk(ierr); 519d1d35e2fSjeremylt (*basis)->ref_count = 1; 520d1d35e2fSjeremylt (*basis)->tensor_basis = 1; 521d7b241e6Sjeremylt (*basis)->dim = dim; 522d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 523d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 524d1d35e2fSjeremylt (*basis)->P_1d = P_1d; 525d1d35e2fSjeremylt (*basis)->Q_1d = Q_1d; 526d1d35e2fSjeremylt (*basis)->P = CeedIntPow(P_1d, dim); 527d1d35e2fSjeremylt (*basis)->Q = CeedIntPow(Q_1d, dim); 52850c301a5SRezgar Shakeri (*basis)->Q_comp = 1; 52950c301a5SRezgar Shakeri (*basis)->basis_space = 1; // 1 for H^1 space 530ff3a0f91SJeremy L Thompson ierr = CeedCalloc(Q_1d, &(*basis)->q_ref_1d); CeedChk(ierr); 531ff3a0f91SJeremy L Thompson ierr = CeedCalloc(Q_1d, &(*basis)->q_weight_1d); CeedChk(ierr); 532ff3a0f91SJeremy L Thompson if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d*sizeof(q_ref_1d[0])); 533ff3a0f91SJeremy L Thompson if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, 534ff3a0f91SJeremy L Thompson Q_1d*sizeof(q_weight_1d[0])); 535ff3a0f91SJeremy L Thompson ierr = CeedCalloc(Q_1d*P_1d, &(*basis)->interp_1d); CeedChk(ierr); 536ff3a0f91SJeremy L Thompson ierr = CeedCalloc(Q_1d*P_1d, &(*basis)->grad_1d); CeedChk(ierr); 537ff3a0f91SJeremy L Thompson if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, 538ff3a0f91SJeremy L Thompson Q_1d*P_1d*sizeof(interp_1d[0])); 539ff3a0f91SJeremy L Thompson if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d*P_1d*sizeof(grad_1d[0])); 540d1d35e2fSjeremylt ierr = ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, 541d1d35e2fSjeremylt q_weight_1d, *basis); CeedChk(ierr); 542e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 543d7b241e6Sjeremylt } 544d7b241e6Sjeremylt 545b11c1e72Sjeremylt /** 54695bb1877Svaleriabarra @brief Create a tensor-product Lagrange basis 547b11c1e72Sjeremylt 548b11c1e72Sjeremylt @param ceed A Ceed object where the CeedBasis will be created 549b11c1e72Sjeremylt @param dim Topological dimension of element 550d1d35e2fSjeremylt @param num_comp Number of field components (1 for scalar fields) 551b11c1e72Sjeremylt @param P Number of Gauss-Lobatto nodes in one dimension. The 552b11c1e72Sjeremylt polynomial degree of the resulting Q_k element is k=P-1. 553b11c1e72Sjeremylt @param Q Number of quadrature points in one dimension. 554d1d35e2fSjeremylt @param quad_mode Distribution of the Q quadrature points (affects order of 555b11c1e72Sjeremylt accuracy for the quadrature) 556b11c1e72Sjeremylt @param[out] basis Address of the variable where the newly created 557b11c1e72Sjeremylt CeedBasis will be stored. 558b11c1e72Sjeremylt 559b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 560dfdf5a53Sjeremylt 5617a982d89SJeremy L. Thompson @ref User 562b11c1e72Sjeremylt **/ 563d1d35e2fSjeremylt int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, 564d1d35e2fSjeremylt CeedInt P, CeedInt Q, CeedQuadMode quad_mode, 565692c2638Sjeremylt CeedBasis *basis) { 566d7b241e6Sjeremylt // Allocate 567e15f9bd0SJeremy L Thompson int ierr, ierr2, i, j, k; 568d1d35e2fSjeremylt CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, 569d1d35e2fSjeremylt *q_weight_1d; 5704d537eeaSYohann 5714d537eeaSYohann if (dim < 1) 572c042f62fSJeremy L Thompson // LCOV_EXCL_START 573e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 574e15f9bd0SJeremy L Thompson "Basis dimension must be a positive value"); 575c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 5764d537eeaSYohann 577227444bfSJeremy L Thompson if (num_comp < 1) 578227444bfSJeremy L Thompson // LCOV_EXCL_START 579227444bfSJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 580227444bfSJeremy L Thompson "Basis must have at least 1 component"); 581227444bfSJeremy L Thompson // LCOV_EXCL_STOP 582227444bfSJeremy L Thompson 583227444bfSJeremy L Thompson if (P < 1) 584227444bfSJeremy L Thompson // LCOV_EXCL_START 585227444bfSJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 586227444bfSJeremy L Thompson "Basis must have at least 1 node"); 587227444bfSJeremy L Thompson // LCOV_EXCL_STOP 588227444bfSJeremy L Thompson 589227444bfSJeremy L Thompson if (Q < 1) 590227444bfSJeremy L Thompson // LCOV_EXCL_START 591227444bfSJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 592227444bfSJeremy L Thompson "Basis must have at least 1 quadrature point"); 593227444bfSJeremy L Thompson // LCOV_EXCL_STOP 594227444bfSJeremy L Thompson 595e15f9bd0SJeremy L Thompson // Get Nodes and Weights 596d1d35e2fSjeremylt ierr = CeedCalloc(P*Q, &interp_1d); CeedChk(ierr); 597d1d35e2fSjeremylt ierr = CeedCalloc(P*Q, &grad_1d); CeedChk(ierr); 598d7b241e6Sjeremylt ierr = CeedCalloc(P, &nodes); CeedChk(ierr); 599d1d35e2fSjeremylt ierr = CeedCalloc(Q, &q_ref_1d); CeedChk(ierr); 600d1d35e2fSjeremylt ierr = CeedCalloc(Q, &q_weight_1d); CeedChk(ierr); 601e15f9bd0SJeremy L Thompson ierr = CeedLobattoQuadrature(P, nodes, NULL); 602e15f9bd0SJeremy L Thompson if (ierr) { goto cleanup; } CeedChk(ierr); 603d1d35e2fSjeremylt switch (quad_mode) { 604d7b241e6Sjeremylt case CEED_GAUSS: 605d1d35e2fSjeremylt ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d); 606d7b241e6Sjeremylt break; 607d7b241e6Sjeremylt case CEED_GAUSS_LOBATTO: 608d1d35e2fSjeremylt ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d); 609d7b241e6Sjeremylt break; 610d7b241e6Sjeremylt } 611e15f9bd0SJeremy L Thompson if (ierr) { goto cleanup; } CeedChk(ierr); 612e15f9bd0SJeremy L Thompson 613d7b241e6Sjeremylt // Build B, D matrix 614d7b241e6Sjeremylt // Fornberg, 1998 615d7b241e6Sjeremylt for (i = 0; i < Q; i++) { 616d7b241e6Sjeremylt c1 = 1.0; 617d1d35e2fSjeremylt c3 = nodes[0] - q_ref_1d[i]; 618d1d35e2fSjeremylt interp_1d[i*P+0] = 1.0; 619d7b241e6Sjeremylt for (j = 1; j < P; j++) { 620d7b241e6Sjeremylt c2 = 1.0; 621d7b241e6Sjeremylt c4 = c3; 622d1d35e2fSjeremylt c3 = nodes[j] - q_ref_1d[i]; 623d7b241e6Sjeremylt for (k = 0; k < j; k++) { 624d7b241e6Sjeremylt dx = nodes[j] - nodes[k]; 625d7b241e6Sjeremylt c2 *= dx; 626d7b241e6Sjeremylt if (k == j - 1) { 627d1d35e2fSjeremylt grad_1d[i*P + j] = c1*(interp_1d[i*P + k] - c4*grad_1d[i*P + k]) / c2; 628d1d35e2fSjeremylt interp_1d[i*P + j] = - c1*c4*interp_1d[i*P + k] / c2; 629d7b241e6Sjeremylt } 630d1d35e2fSjeremylt grad_1d[i*P + k] = (c3*grad_1d[i*P + k] - interp_1d[i*P + k]) / dx; 631d1d35e2fSjeremylt interp_1d[i*P + k] = c3*interp_1d[i*P + k] / dx; 632d7b241e6Sjeremylt } 633d7b241e6Sjeremylt c1 = c2; 634d7b241e6Sjeremylt } 635d7b241e6Sjeremylt } 6369ac7b42eSJeremy L Thompson // Pass to CeedBasisCreateTensorH1 637d1d35e2fSjeremylt ierr = CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, 6389ac7b42eSJeremy L Thompson q_ref_1d, q_weight_1d, basis); CeedChk(ierr); 639e15f9bd0SJeremy L Thompson cleanup: 640d1d35e2fSjeremylt ierr2 = CeedFree(&interp_1d); CeedChk(ierr2); 641d1d35e2fSjeremylt ierr2 = CeedFree(&grad_1d); CeedChk(ierr2); 642e15f9bd0SJeremy L Thompson ierr2 = CeedFree(&nodes); CeedChk(ierr2); 643d1d35e2fSjeremylt ierr2 = CeedFree(&q_ref_1d); CeedChk(ierr2); 644d1d35e2fSjeremylt ierr2 = CeedFree(&q_weight_1d); CeedChk(ierr2); 645e15f9bd0SJeremy L Thompson CeedChk(ierr); 646e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 647d7b241e6Sjeremylt } 648d7b241e6Sjeremylt 649b11c1e72Sjeremylt /** 65095bb1877Svaleriabarra @brief Create a non tensor-product basis for H^1 discretizations 651a8de75f0Sjeremylt 652a8de75f0Sjeremylt @param ceed A Ceed object where the CeedBasis will be created 653a8de75f0Sjeremylt @param topo Topology of element, e.g. hypercube, simplex, ect 654d1d35e2fSjeremylt @param num_comp Number of field components (1 for scalar fields) 655d1d35e2fSjeremylt @param num_nodes Total number of nodes 656d1d35e2fSjeremylt @param num_qpts Total number of quadrature points 657d1d35e2fSjeremylt @param interp Row-major (num_qpts * num_nodes) matrix expressing the values of 6588795c945Sjeremylt nodal basis functions at quadrature points 659d1d35e2fSjeremylt @param grad Row-major (num_qpts * dim * num_nodes) matrix expressing 6608795c945Sjeremylt derivatives of nodal basis functions at quadrature points 661d1d35e2fSjeremylt @param q_ref Array of length num_qpts holding the locations of quadrature 66250c301a5SRezgar Shakeri points on the reference element 663d1d35e2fSjeremylt @param q_weight Array of length num_qpts holding the quadrature weights on the 664a8de75f0Sjeremylt reference element 665a8de75f0Sjeremylt @param[out] basis Address of the variable where the newly created 666a8de75f0Sjeremylt CeedBasis will be stored. 667a8de75f0Sjeremylt 668a8de75f0Sjeremylt @return An error code: 0 - success, otherwise - failure 669a8de75f0Sjeremylt 6707a982d89SJeremy L. Thompson @ref User 671a8de75f0Sjeremylt **/ 672d1d35e2fSjeremylt int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, 673d1d35e2fSjeremylt CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 674d1d35e2fSjeremylt const CeedScalar *grad, const CeedScalar *q_ref, 675d1d35e2fSjeremylt const CeedScalar *q_weight, CeedBasis *basis) { 676a8de75f0Sjeremylt int ierr; 677d1d35e2fSjeremylt CeedInt P = num_nodes, Q = num_qpts, dim = 0; 678a8de75f0Sjeremylt 6795fe0d4faSjeremylt if (!ceed->BasisCreateH1) { 6805fe0d4faSjeremylt Ceed delegate; 681aefd8378Sjeremylt ierr = CeedGetObjectDelegate(ceed, &delegate, "Basis"); CeedChk(ierr); 6825fe0d4faSjeremylt 6835fe0d4faSjeremylt if (!delegate) 684c042f62fSJeremy L Thompson // LCOV_EXCL_START 685e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, 686e15f9bd0SJeremy L Thompson "Backend does not support BasisCreateH1"); 687c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 6885fe0d4faSjeremylt 689d1d35e2fSjeremylt ierr = CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, 690d1d35e2fSjeremylt num_qpts, interp, grad, q_ref, 691d1d35e2fSjeremylt q_weight, basis); CeedChk(ierr); 692e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6935fe0d4faSjeremylt } 6945fe0d4faSjeremylt 695227444bfSJeremy L Thompson if (num_comp < 1) 696227444bfSJeremy L Thompson // LCOV_EXCL_START 697227444bfSJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 698227444bfSJeremy L Thompson "Basis must have at least 1 component"); 699227444bfSJeremy L Thompson // LCOV_EXCL_STOP 700227444bfSJeremy L Thompson 701227444bfSJeremy L Thompson if (num_nodes < 1) 702227444bfSJeremy L Thompson // LCOV_EXCL_START 703227444bfSJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 704227444bfSJeremy L Thompson "Basis must have at least 1 node"); 705227444bfSJeremy L Thompson // LCOV_EXCL_STOP 706227444bfSJeremy L Thompson 707227444bfSJeremy L Thompson if (num_qpts < 1) 708227444bfSJeremy L Thompson // LCOV_EXCL_START 709227444bfSJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 710227444bfSJeremy L Thompson "Basis must have at least 1 quadrature point"); 711227444bfSJeremy L Thompson // LCOV_EXCL_STOP 712227444bfSJeremy L Thompson 713a8de75f0Sjeremylt ierr = CeedCalloc(1, basis); CeedChk(ierr); 714a8de75f0Sjeremylt 715a8de75f0Sjeremylt ierr = CeedBasisGetTopologyDimension(topo, &dim); CeedChk(ierr); 716a8de75f0Sjeremylt 717a8de75f0Sjeremylt (*basis)->ceed = ceed; 7189560d06aSjeremylt ierr = CeedReference(ceed); CeedChk(ierr); 719d1d35e2fSjeremylt (*basis)->ref_count = 1; 720d1d35e2fSjeremylt (*basis)->tensor_basis = 0; 721a8de75f0Sjeremylt (*basis)->dim = dim; 722d99fa3c5SJeremy L Thompson (*basis)->topo = topo; 723d1d35e2fSjeremylt (*basis)->num_comp = num_comp; 724a8de75f0Sjeremylt (*basis)->P = P; 725a8de75f0Sjeremylt (*basis)->Q = Q; 72650c301a5SRezgar Shakeri (*basis)->Q_comp = 1; 72750c301a5SRezgar Shakeri (*basis)->basis_space = 1; // 1 for H^1 space 728ff3a0f91SJeremy L Thompson ierr = CeedCalloc(Q*dim, &(*basis)->q_ref_1d); CeedChk(ierr); 729ff3a0f91SJeremy L Thompson ierr = CeedCalloc(Q, &(*basis)->q_weight_1d); CeedChk(ierr); 730ff3a0f91SJeremy L Thompson if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q*dim*sizeof(q_ref[0])); 731ff3a0f91SJeremy L Thompson if(q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q*sizeof(q_weight[0])); 732ff3a0f91SJeremy L Thompson ierr = CeedCalloc(Q*P, &(*basis)->interp); CeedChk(ierr); 733ff3a0f91SJeremy L Thompson ierr = CeedCalloc(dim*Q*P, &(*basis)->grad); CeedChk(ierr); 734ff3a0f91SJeremy L Thompson if(interp) memcpy((*basis)->interp, interp, Q*P*sizeof(interp[0])); 735ff3a0f91SJeremy L Thompson if(grad) memcpy((*basis)->grad, grad, dim*Q*P*sizeof(grad[0])); 736d1d35e2fSjeremylt ierr = ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, 737d1d35e2fSjeremylt q_weight, *basis); CeedChk(ierr); 738e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 739a8de75f0Sjeremylt } 740a8de75f0Sjeremylt 741a8de75f0Sjeremylt /** 74250c301a5SRezgar Shakeri @brief Create a non tensor-product basis for H(div) discretizations 74350c301a5SRezgar Shakeri 74450c301a5SRezgar Shakeri @param ceed A Ceed object where the CeedBasis will be created 74550c301a5SRezgar Shakeri @param topo Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), 74650c301a5SRezgar Shakeri dimension of which is used in some array sizes below 74750c301a5SRezgar Shakeri @param num_comp Number of components (usually 1 for vectors in H(div) bases) 74850c301a5SRezgar Shakeri @param num_nodes Total number of nodes (dofs per element) 74950c301a5SRezgar Shakeri @param num_qpts Total number of quadrature points 75050c301a5SRezgar Shakeri @param interp Row-major (dim*num_qpts * num_nodes) matrix expressing the values of 75150c301a5SRezgar Shakeri nodal basis functions at quadrature points 75250c301a5SRezgar Shakeri @param div Row-major (num_qpts * num_nodes) matrix expressing 75350c301a5SRezgar Shakeri divergence of nodal basis functions at quadrature points 75450c301a5SRezgar Shakeri @param q_ref Array of length num_qpts holding the locations of quadrature 75550c301a5SRezgar Shakeri points on the reference element 75650c301a5SRezgar Shakeri @param q_weight Array of length num_qpts holding the quadrature weights on the 75750c301a5SRezgar Shakeri reference element 75850c301a5SRezgar Shakeri @param[out] basis Address of the variable where the newly created 75950c301a5SRezgar Shakeri CeedBasis will be stored. 76050c301a5SRezgar Shakeri 76150c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 76250c301a5SRezgar Shakeri 76350c301a5SRezgar Shakeri @ref User 76450c301a5SRezgar Shakeri **/ 76550c301a5SRezgar Shakeri int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, 76650c301a5SRezgar Shakeri CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 76750c301a5SRezgar Shakeri const CeedScalar *div, const CeedScalar *q_ref, 76850c301a5SRezgar Shakeri const CeedScalar *q_weight, CeedBasis *basis) { 76950c301a5SRezgar Shakeri int ierr; 77050c301a5SRezgar Shakeri CeedInt Q = num_qpts, P = num_nodes, dim = 0; 77150c301a5SRezgar Shakeri ierr = CeedBasisGetTopologyDimension(topo, &dim); CeedChk(ierr); 77250c301a5SRezgar Shakeri if (!ceed->BasisCreateHdiv) { 77350c301a5SRezgar Shakeri Ceed delegate; 77450c301a5SRezgar Shakeri ierr = CeedGetObjectDelegate(ceed, &delegate, "Basis"); CeedChk(ierr); 77550c301a5SRezgar Shakeri 77650c301a5SRezgar Shakeri if (!delegate) 77750c301a5SRezgar Shakeri // LCOV_EXCL_START 77850c301a5SRezgar Shakeri return CeedError(ceed, CEED_ERROR_UNSUPPORTED, 77950c301a5SRezgar Shakeri "Backend does not implement BasisCreateHdiv"); 78050c301a5SRezgar Shakeri // LCOV_EXCL_STOP 78150c301a5SRezgar Shakeri 78250c301a5SRezgar Shakeri ierr = CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, 78350c301a5SRezgar Shakeri num_qpts, interp, div, q_ref, 78450c301a5SRezgar Shakeri q_weight, basis); CeedChk(ierr); 78550c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 78650c301a5SRezgar Shakeri } 78750c301a5SRezgar Shakeri 788227444bfSJeremy L Thompson if (num_comp < 1) 789227444bfSJeremy L Thompson // LCOV_EXCL_START 790227444bfSJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 791227444bfSJeremy L Thompson "Basis must have at least 1 component"); 792227444bfSJeremy L Thompson // LCOV_EXCL_STOP 793227444bfSJeremy L Thompson 794227444bfSJeremy L Thompson if (num_nodes < 1) 795227444bfSJeremy L Thompson // LCOV_EXCL_START 796227444bfSJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 797227444bfSJeremy L Thompson "Basis must have at least 1 node"); 798227444bfSJeremy L Thompson // LCOV_EXCL_STOP 799227444bfSJeremy L Thompson 800227444bfSJeremy L Thompson if (num_qpts < 1) 801227444bfSJeremy L Thompson // LCOV_EXCL_START 802227444bfSJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 803227444bfSJeremy L Thompson "Basis must have at least 1 quadrature point"); 804227444bfSJeremy L Thompson // LCOV_EXCL_STOP 805227444bfSJeremy L Thompson 80650c301a5SRezgar Shakeri ierr = CeedCalloc(1, basis); CeedChk(ierr); 80750c301a5SRezgar Shakeri 80850c301a5SRezgar Shakeri (*basis)->ceed = ceed; 80950c301a5SRezgar Shakeri ierr = CeedReference(ceed); CeedChk(ierr); 81050c301a5SRezgar Shakeri (*basis)->ref_count = 1; 81150c301a5SRezgar Shakeri (*basis)->tensor_basis = 0; 81250c301a5SRezgar Shakeri (*basis)->dim = dim; 81350c301a5SRezgar Shakeri (*basis)->topo = topo; 81450c301a5SRezgar Shakeri (*basis)->num_comp = num_comp; 81550c301a5SRezgar Shakeri (*basis)->P = P; 81650c301a5SRezgar Shakeri (*basis)->Q = Q; 81750c301a5SRezgar Shakeri (*basis)->Q_comp = dim; 81850c301a5SRezgar Shakeri (*basis)->basis_space = 2; // 2 for H(div) space 81950c301a5SRezgar Shakeri ierr = CeedMalloc(Q*dim, &(*basis)->q_ref_1d); CeedChk(ierr); 82050c301a5SRezgar Shakeri ierr = CeedMalloc(Q, &(*basis)->q_weight_1d); CeedChk(ierr); 82150c301a5SRezgar Shakeri if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q*dim*sizeof(q_ref[0])); 82250c301a5SRezgar Shakeri if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q*sizeof(q_weight[0])); 82350c301a5SRezgar Shakeri ierr = CeedMalloc(dim*Q*P, &(*basis)->interp); CeedChk(ierr); 82450c301a5SRezgar Shakeri ierr = CeedMalloc(Q*P, &(*basis)->div); CeedChk(ierr); 82550c301a5SRezgar Shakeri if (interp) memcpy((*basis)->interp, interp, dim*Q*P*sizeof(interp[0])); 82650c301a5SRezgar Shakeri if (div) memcpy((*basis)->div, div, Q*P*sizeof(div[0])); 82750c301a5SRezgar Shakeri ierr = ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, 82850c301a5SRezgar Shakeri q_weight, *basis); CeedChk(ierr); 82950c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 83050c301a5SRezgar Shakeri } 83150c301a5SRezgar Shakeri 83250c301a5SRezgar Shakeri /** 833f113e5dcSJeremy L Thompson @brief Create a CeedBasis for projection from the nodes of `basis_from` 834f113e5dcSJeremy L Thompson to the nodes of `basis_to`. Only `CEED_EVAL_INTERP` will be 835f113e5dcSJeremy L Thompson valid for the new basis, `basis_project`. This projection is 836f113e5dcSJeremy L Thompson given by `interp_project = interp_to^+ * interp_from`, where 837f113e5dcSJeremy L Thompson the pesudoinverse `interp_to^+` is given by QR factorization. 838f113e5dcSJeremy L Thompson Note: `basis_from` and `basis_to` must have compatible quadrature 839f113e5dcSJeremy L Thompson spaces. 840446e7af4SJeremy L Thompson Note: `basis_project` will have the same number of components as 841446e7af4SJeremy L Thompson `basis_from`, regardless of the number of components that 842446e7af4SJeremy L Thompson `basis_to` has. If `basis_from` has 3 components and `basis_to` 843446e7af4SJeremy L Thompson has 5 components, then `basis_project` will have 3 components. 844f113e5dcSJeremy L Thompson 845f113e5dcSJeremy L Thompson @param[in] basis_from CeedBasis to prolong from 846446e7af4SJeremy L Thompson @param[in] basis_to CeedBasis to prolong to 847f113e5dcSJeremy L Thompson @param[out] basis_project Address of the variable where the newly created 848f113e5dcSJeremy L Thompson CeedBasis will be stored. 849f113e5dcSJeremy L Thompson 850f113e5dcSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 851f113e5dcSJeremy L Thompson 852f113e5dcSJeremy L Thompson @ref User 853f113e5dcSJeremy L Thompson **/ 854446e7af4SJeremy L Thompson int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, 855f113e5dcSJeremy L Thompson CeedBasis *basis_project) { 856f113e5dcSJeremy L Thompson int ierr; 857f113e5dcSJeremy L Thompson Ceed ceed; 858f113e5dcSJeremy L Thompson ierr = CeedBasisGetCeed(basis_to, &ceed); CeedChk(ierr); 859f113e5dcSJeremy L Thompson 860f113e5dcSJeremy L Thompson // Create projectior matrix 861f113e5dcSJeremy L Thompson CeedScalar *interp_project; 862446e7af4SJeremy L Thompson ierr = CeedBasisCreateProjectionMatrix(basis_from, basis_to, 863f113e5dcSJeremy L Thompson &interp_project); CeedChk(ierr); 864f113e5dcSJeremy L Thompson 865f113e5dcSJeremy L Thompson // Build basis 866f113e5dcSJeremy L Thompson bool is_tensor; 867f113e5dcSJeremy L Thompson CeedInt dim, num_comp; 868f113e5dcSJeremy L Thompson CeedScalar *q_ref, *q_weight, *grad; 869f113e5dcSJeremy L Thompson ierr = CeedBasisIsTensor(basis_to, &is_tensor); CeedChk(ierr); 870f113e5dcSJeremy L Thompson ierr = CeedBasisGetDimension(basis_to, &dim); CeedChk(ierr); 871446e7af4SJeremy L Thompson ierr = CeedBasisGetNumComponents(basis_from, &num_comp); CeedChk(ierr); 872f113e5dcSJeremy L Thompson if (is_tensor) { 873f113e5dcSJeremy L Thompson CeedInt P_1d_to, P_1d_from; 874f113e5dcSJeremy L Thompson ierr = CeedBasisGetNumNodes1D(basis_from, &P_1d_from); CeedChk(ierr); 875f113e5dcSJeremy L Thompson ierr = CeedBasisGetNumNodes1D(basis_to, &P_1d_to); CeedChk(ierr); 876f113e5dcSJeremy L Thompson ierr = CeedCalloc(P_1d_to, &q_ref); CeedChk(ierr); 877f113e5dcSJeremy L Thompson ierr = CeedCalloc(P_1d_to, &q_weight); CeedChk(ierr); 878f113e5dcSJeremy L Thompson ierr = CeedCalloc(P_1d_to * P_1d_from * dim, &grad); CeedChk(ierr); 879f113e5dcSJeremy L Thompson ierr = CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, 880f113e5dcSJeremy L Thompson interp_project, grad, q_ref, q_weight, basis_project); 881f113e5dcSJeremy L Thompson CeedChk(ierr); 882f113e5dcSJeremy L Thompson } else { 883f113e5dcSJeremy L Thompson CeedElemTopology topo; 884f113e5dcSJeremy L Thompson ierr = CeedBasisGetTopology(basis_to, &topo); CeedChk(ierr); 885f113e5dcSJeremy L Thompson CeedInt num_nodes_to, num_nodes_from; 886f113e5dcSJeremy L Thompson ierr = CeedBasisGetNumNodes(basis_from, &num_nodes_from); CeedChk(ierr); 887f113e5dcSJeremy L Thompson ierr = CeedBasisGetNumNodes(basis_to, &num_nodes_to); CeedChk(ierr); 888f113e5dcSJeremy L Thompson ierr = CeedCalloc(num_nodes_to * dim, &q_ref); CeedChk(ierr); 889f113e5dcSJeremy L Thompson ierr = CeedCalloc(num_nodes_to, &q_weight); CeedChk(ierr); 890f113e5dcSJeremy L Thompson ierr = CeedCalloc(num_nodes_to * num_nodes_from * dim, &grad); CeedChk(ierr); 891f113e5dcSJeremy L Thompson ierr = CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, 892f113e5dcSJeremy L Thompson interp_project, grad, q_ref, q_weight, basis_project); 893f113e5dcSJeremy L Thompson CeedChk(ierr); 894f113e5dcSJeremy L Thompson } 895f113e5dcSJeremy L Thompson 896f113e5dcSJeremy L Thompson // Cleanup 897f113e5dcSJeremy L Thompson ierr = CeedFree(&interp_project); CeedChk(ierr); 898f113e5dcSJeremy L Thompson ierr = CeedFree(&q_ref); CeedChk(ierr); 899f113e5dcSJeremy L Thompson ierr = CeedFree(&q_weight); CeedChk(ierr); 900f113e5dcSJeremy L Thompson ierr = CeedFree(&grad); CeedChk(ierr); 901f113e5dcSJeremy L Thompson 902f113e5dcSJeremy L Thompson return CEED_ERROR_SUCCESS; 903f113e5dcSJeremy L Thompson } 904f113e5dcSJeremy L Thompson 905f113e5dcSJeremy L Thompson /** 906f113e5dcSJeremy L Thompson @brief Create the interpolation matrix for projection from the nodes of 907f113e5dcSJeremy L Thompson `basis_from` to the nodes of `basis_to`. This projection is 908f113e5dcSJeremy L Thompson given by `interp_project = interp_to^+ * interp_from`, where 909f113e5dcSJeremy L Thompson the pesudoinverse `interp_to^+` is given by QR factorization. 910f113e5dcSJeremy L Thompson Note: `basis_from` and `basis_to` must have compatible quadrature 911f113e5dcSJeremy L Thompson spaces. 912f113e5dcSJeremy L Thompson 913f113e5dcSJeremy L Thompson @param[in] basis_from CeedBasis to project from 914446e7af4SJeremy L Thompson @param[in] basis_to CeedBasis to project to 915f113e5dcSJeremy L Thompson @param[out] interp_project Address of the variable where the newly created 916f113e5dcSJeremy L Thompson projection matrix will be stored. 917f113e5dcSJeremy L Thompson 918f113e5dcSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 919f113e5dcSJeremy L Thompson 920f113e5dcSJeremy L Thompson @ref User 921f113e5dcSJeremy L Thompson **/ 922446e7af4SJeremy L Thompson int CeedBasisCreateProjectionMatrix(CeedBasis basis_from, 923446e7af4SJeremy L Thompson CeedBasis basis_to, 924f113e5dcSJeremy L Thompson CeedScalar **interp_project) { 925f113e5dcSJeremy L Thompson int ierr; 926f113e5dcSJeremy L Thompson Ceed ceed; 927f113e5dcSJeremy L Thompson ierr = CeedBasisGetCeed(basis_to, &ceed); CeedChk(ierr); 928f113e5dcSJeremy L Thompson 929f113e5dcSJeremy L Thompson // Check for compatible quadrature spaces 930f113e5dcSJeremy L Thompson CeedInt Q_to, Q_from; 931f113e5dcSJeremy L Thompson ierr = CeedBasisGetNumQuadraturePoints(basis_to, &Q_to); CeedChk(ierr); 932f113e5dcSJeremy L Thompson ierr = CeedBasisGetNumQuadraturePoints(basis_from, &Q_from); CeedChk(ierr); 933f113e5dcSJeremy L Thompson if (Q_to != Q_from) 934f113e5dcSJeremy L Thompson // LCOV_EXCL_START 935f113e5dcSJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 936f113e5dcSJeremy L Thompson "Bases must have compatible quadrature spaces"); 937f113e5dcSJeremy L Thompson // LCOV_EXCL_STOP 938f113e5dcSJeremy L Thompson 939f113e5dcSJeremy L Thompson // Coarse to fine basis 940f113e5dcSJeremy L Thompson CeedInt P_to, P_from, Q = Q_to; 941f113e5dcSJeremy L Thompson bool is_tensor_to, is_tensor_from; 942f113e5dcSJeremy L Thompson ierr = CeedBasisIsTensor(basis_to, &is_tensor_to); CeedChk(ierr); 943f113e5dcSJeremy L Thompson ierr = CeedBasisIsTensor(basis_from, &is_tensor_from); CeedChk(ierr); 944f113e5dcSJeremy L Thompson CeedScalar *interp_to, *interp_from, *tau; 945f113e5dcSJeremy L Thompson if (is_tensor_to && is_tensor_from) { 946f113e5dcSJeremy L Thompson ierr = CeedBasisGetNumNodes1D(basis_to, &P_to); CeedChk(ierr); 947f113e5dcSJeremy L Thompson ierr = CeedBasisGetNumNodes1D(basis_from, &P_from); CeedChk(ierr); 948f113e5dcSJeremy L Thompson ierr = CeedBasisGetNumQuadraturePoints1D(basis_from, &Q); CeedChk(ierr); 949f113e5dcSJeremy L Thompson } else if (!is_tensor_to && !is_tensor_from) { 950f113e5dcSJeremy L Thompson ierr = CeedBasisGetNumNodes(basis_to, &P_to); CeedChk(ierr); 951f113e5dcSJeremy L Thompson ierr = CeedBasisGetNumNodes(basis_from, &P_from); CeedChk(ierr); 952f113e5dcSJeremy L Thompson } else { 953f113e5dcSJeremy L Thompson // LCOV_EXCL_START 954f113e5dcSJeremy L Thompson return CeedError(ceed, CEED_ERROR_MINOR, 955f113e5dcSJeremy L Thompson "Bases must both be tensor or non-tensor"); 956f113e5dcSJeremy L Thompson // LCOV_EXCL_STOP 957f113e5dcSJeremy L Thompson } 958f113e5dcSJeremy L Thompson 959f113e5dcSJeremy L Thompson ierr = CeedMalloc(Q * P_from, &interp_from); CeedChk(ierr); 960f113e5dcSJeremy L Thompson ierr = CeedMalloc(Q * P_to, &interp_to); CeedChk(ierr); 961f113e5dcSJeremy L Thompson ierr = CeedCalloc(P_to * P_from, interp_project); CeedChk(ierr); 962f113e5dcSJeremy L Thompson ierr = CeedMalloc(Q, &tau); CeedChk(ierr); 963f113e5dcSJeremy L Thompson const CeedScalar *interp_to_source = NULL, *interp_from_source = NULL; 964f113e5dcSJeremy L Thompson if (is_tensor_to) { 965f113e5dcSJeremy L Thompson ierr = CeedBasisGetInterp1D(basis_to, &interp_to_source); CeedChk(ierr); 966f113e5dcSJeremy L Thompson ierr = CeedBasisGetInterp1D(basis_from, &interp_from_source); CeedChk(ierr); 967f113e5dcSJeremy L Thompson } else { 968f113e5dcSJeremy L Thompson ierr = CeedBasisGetInterp(basis_to, &interp_to_source); CeedChk(ierr); 969f113e5dcSJeremy L Thompson ierr = CeedBasisGetInterp(basis_from, &interp_from_source); CeedChk(ierr); 970f113e5dcSJeremy L Thompson } 971f113e5dcSJeremy L Thompson memcpy(interp_to, interp_to_source, Q * P_to * sizeof(interp_to_source[0])); 972f113e5dcSJeremy L Thompson memcpy(interp_from, interp_from_source, 973f113e5dcSJeremy L Thompson Q * P_from * sizeof(interp_from_source[0])); 974f113e5dcSJeremy L Thompson 975f113e5dcSJeremy L Thompson // -- QR Factorization, interp_to = Q R 976f113e5dcSJeremy L Thompson ierr = CeedQRFactorization(ceed, interp_to, tau, Q, P_to); CeedChk(ierr); 977f113e5dcSJeremy L Thompson 978f113e5dcSJeremy L Thompson // -- Apply Qtranspose, interp_to = Qtranspose interp_from 979f113e5dcSJeremy L Thompson ierr = CeedHouseholderApplyQ(interp_from, interp_to, tau, CEED_TRANSPOSE, 980f113e5dcSJeremy L Thompson Q, P_from, P_to, P_from, 1); CeedChk(ierr); 981f113e5dcSJeremy L Thompson 982f113e5dcSJeremy L Thompson // -- Apply Rinv, interp_project = Rinv interp_c 983f113e5dcSJeremy L Thompson for (CeedInt j = 0; j < P_from; j++) { // Column j 984f113e5dcSJeremy L Thompson (*interp_project)[j + P_from * (P_to - 1)] = interp_from[j + P_from * 985f113e5dcSJeremy L Thompson (P_to - 1)] / interp_to[P_to * P_to - 1]; 986f113e5dcSJeremy L Thompson for (CeedInt i = P_to - 2; i >= 0; i--) { // Row i 987f113e5dcSJeremy L Thompson (*interp_project)[j + P_from * i] = interp_from[j + P_from * i]; 988f113e5dcSJeremy L Thompson for (CeedInt k = i+1; k < P_to; k++) { 989f113e5dcSJeremy L Thompson (*interp_project)[j + P_from * i] -= interp_to[k + P_to * i]* 990f113e5dcSJeremy L Thompson (*interp_project)[j + P_from * k]; 991f113e5dcSJeremy L Thompson } 992f113e5dcSJeremy L Thompson (*interp_project)[j + P_from * i] /= interp_to[i + P_to * i]; 993f113e5dcSJeremy L Thompson } 994f113e5dcSJeremy L Thompson } 995f113e5dcSJeremy L Thompson ierr = CeedFree(&tau); CeedChk(ierr); 996f113e5dcSJeremy L Thompson ierr = CeedFree(&interp_to); CeedChk(ierr); 997f113e5dcSJeremy L Thompson ierr = CeedFree(&interp_from); CeedChk(ierr); 998f113e5dcSJeremy L Thompson 999f113e5dcSJeremy L Thompson return CEED_ERROR_SUCCESS; 1000f113e5dcSJeremy L Thompson } 1001f113e5dcSJeremy L Thompson 1002f113e5dcSJeremy L Thompson /** 10039560d06aSjeremylt @brief Copy the pointer to a CeedBasis. Both pointers should 10049560d06aSjeremylt be destroyed with `CeedBasisDestroy()`; 10059560d06aSjeremylt Note: If `*basis_copy` is non-NULL, then it is assumed that 10069560d06aSjeremylt `*basis_copy` is a pointer to a CeedBasis. This CeedBasis 10079560d06aSjeremylt will be destroyed if `*basis_copy` is the only 10089560d06aSjeremylt reference to this CeedBasis. 10099560d06aSjeremylt 10109560d06aSjeremylt @param basis CeedBasis to copy reference to 10119560d06aSjeremylt @param[out] basis_copy Variable to store copied reference 10129560d06aSjeremylt 10139560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 10149560d06aSjeremylt 10159560d06aSjeremylt @ref User 10169560d06aSjeremylt **/ 10179560d06aSjeremylt int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) { 10189560d06aSjeremylt int ierr; 10199560d06aSjeremylt 10209560d06aSjeremylt ierr = CeedBasisReference(basis); CeedChk(ierr); 10219560d06aSjeremylt ierr = CeedBasisDestroy(basis_copy); CeedChk(ierr); 10229560d06aSjeremylt *basis_copy = basis; 10239560d06aSjeremylt return CEED_ERROR_SUCCESS; 10249560d06aSjeremylt } 10259560d06aSjeremylt 10269560d06aSjeremylt /** 10277a982d89SJeremy L. Thompson @brief View a CeedBasis 10287a982d89SJeremy L. Thompson 10297a982d89SJeremy L. Thompson @param basis CeedBasis to view 10307a982d89SJeremy L. Thompson @param stream Stream to view to, e.g., stdout 10317a982d89SJeremy L. Thompson 10327a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 10337a982d89SJeremy L. Thompson 10347a982d89SJeremy L. Thompson @ref User 10357a982d89SJeremy L. Thompson **/ 10367a982d89SJeremy L. Thompson int CeedBasisView(CeedBasis basis, FILE *stream) { 10377a982d89SJeremy L. Thompson int ierr; 103850c301a5SRezgar Shakeri CeedFESpace FE_space = basis->basis_space; 103950c301a5SRezgar Shakeri CeedElemTopology topo = basis->topo; 104050c301a5SRezgar Shakeri // Print FE space and element topology of the basis 1041d1d35e2fSjeremylt if (basis->tensor_basis) { 1042*990fdeb6SJeremy L Thompson fprintf(stream, "CeedBasis (%s on a %s element): dim=%" CeedInt_FMT " P=%" 1043*990fdeb6SJeremy L Thompson CeedInt_FMT " Q=%" CeedInt_FMT "\n", 104450c301a5SRezgar Shakeri CeedFESpaces[FE_space], CeedElemTopologies[topo], 104550c301a5SRezgar Shakeri basis->dim, basis->P_1d, basis->Q_1d); 104650c301a5SRezgar Shakeri } else { 1047*990fdeb6SJeremy L Thompson fprintf(stream, "CeedBasis (%s on a %s element): dim=%" CeedInt_FMT " P=%" 1048*990fdeb6SJeremy L Thompson CeedInt_FMT " Q=%" CeedInt_FMT "\n", 104950c301a5SRezgar Shakeri CeedFESpaces[FE_space], CeedElemTopologies[topo], 105050c301a5SRezgar Shakeri basis->dim, basis->P, basis->Q); 105150c301a5SRezgar Shakeri } 105250c301a5SRezgar Shakeri // Print quadrature data, interpolation/gradient/divergene/curl of the basis 105350c301a5SRezgar Shakeri if (basis->tensor_basis) { // tensor basis 1054d1d35e2fSjeremylt ierr = CeedScalarView("qref1d", "\t% 12.8f", 1, basis->Q_1d, basis->q_ref_1d, 10557a982d89SJeremy L. Thompson stream); CeedChk(ierr); 1056d1d35e2fSjeremylt ierr = CeedScalarView("qweight1d", "\t% 12.8f", 1, basis->Q_1d, 1057d1d35e2fSjeremylt basis->q_weight_1d, stream); CeedChk(ierr); 1058d1d35e2fSjeremylt ierr = CeedScalarView("interp1d", "\t% 12.8f", basis->Q_1d, basis->P_1d, 1059d1d35e2fSjeremylt basis->interp_1d, stream); CeedChk(ierr); 1060d1d35e2fSjeremylt ierr = CeedScalarView("grad1d", "\t% 12.8f", basis->Q_1d, basis->P_1d, 1061d1d35e2fSjeremylt basis->grad_1d, stream); CeedChk(ierr); 106250c301a5SRezgar Shakeri } else { // non-tensor basis 10637a982d89SJeremy L. Thompson ierr = CeedScalarView("qref", "\t% 12.8f", 1, basis->Q*basis->dim, 1064d1d35e2fSjeremylt basis->q_ref_1d, 10657a982d89SJeremy L. Thompson stream); CeedChk(ierr); 1066d1d35e2fSjeremylt ierr = CeedScalarView("qweight", "\t% 12.8f", 1, basis->Q, basis->q_weight_1d, 10677a982d89SJeremy L. Thompson stream); CeedChk(ierr); 106850c301a5SRezgar Shakeri ierr = CeedScalarView("interp", "\t% 12.8f", basis->Q_comp*basis->Q, basis->P, 10697a982d89SJeremy L. Thompson basis->interp, stream); CeedChk(ierr); 107050c301a5SRezgar Shakeri if (basis->grad) { 10717a982d89SJeremy L. Thompson ierr = CeedScalarView("grad", "\t% 12.8f", basis->dim*basis->Q, basis->P, 10727a982d89SJeremy L. Thompson basis->grad, stream); CeedChk(ierr); 10737a982d89SJeremy L. Thompson } 107450c301a5SRezgar Shakeri if (basis->div) { 107550c301a5SRezgar Shakeri ierr = CeedScalarView("div", "\t% 12.8f", basis->Q, basis->P, 107650c301a5SRezgar Shakeri basis->div, stream); CeedChk(ierr); 107750c301a5SRezgar Shakeri } 107850c301a5SRezgar Shakeri } 1079e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10807a982d89SJeremy L. Thompson } 10817a982d89SJeremy L. Thompson 10827a982d89SJeremy L. Thompson /** 10837a982d89SJeremy L. Thompson @brief Apply basis evaluation from nodes to quadrature points or vice versa 10847a982d89SJeremy L. Thompson 10857a982d89SJeremy L. Thompson @param basis CeedBasis to evaluate 1086d1d35e2fSjeremylt @param num_elem The number of elements to apply the basis evaluation to; 10877a982d89SJeremy L. Thompson the backend will specify the ordering in 10884cc79fe7SJed Brown CeedElemRestrictionCreateBlocked() 1089d1d35e2fSjeremylt @param t_mode \ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature 10907a982d89SJeremy L. Thompson points, \ref CEED_TRANSPOSE to apply the transpose, mapping 10917a982d89SJeremy L. Thompson from quadrature points to nodes 1092d1d35e2fSjeremylt @param eval_mode \ref CEED_EVAL_NONE to use values directly, 10937a982d89SJeremy L. Thompson \ref CEED_EVAL_INTERP to use interpolated values, 10947a982d89SJeremy L. Thompson \ref CEED_EVAL_GRAD to use gradients, 10957a982d89SJeremy L. Thompson \ref CEED_EVAL_WEIGHT to use quadrature weights. 10967a982d89SJeremy L. Thompson @param[in] u Input CeedVector 10977a982d89SJeremy L. Thompson @param[out] v Output CeedVector 10987a982d89SJeremy L. Thompson 10997a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 11007a982d89SJeremy L. Thompson 11017a982d89SJeremy L. Thompson @ref User 11027a982d89SJeremy L. Thompson **/ 1103d1d35e2fSjeremylt int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, 1104d1d35e2fSjeremylt CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 11057a982d89SJeremy L. Thompson int ierr; 11061f9221feSJeremy L Thompson CeedSize u_length = 0, v_length; 11071f9221feSJeremy L Thompson CeedInt dim, num_comp, num_nodes, num_qpts; 1108e15f9bd0SJeremy L Thompson ierr = CeedBasisGetDimension(basis, &dim); CeedChk(ierr); 1109d1d35e2fSjeremylt ierr = CeedBasisGetNumComponents(basis, &num_comp); CeedChk(ierr); 1110d1d35e2fSjeremylt ierr = CeedBasisGetNumNodes(basis, &num_nodes); CeedChk(ierr); 1111d1d35e2fSjeremylt ierr = CeedBasisGetNumQuadraturePoints(basis, &num_qpts); CeedChk(ierr); 1112d1d35e2fSjeremylt ierr = CeedVectorGetLength(v, &v_length); CeedChk(ierr); 11137a982d89SJeremy L. Thompson if (u) { 1114d1d35e2fSjeremylt ierr = CeedVectorGetLength(u, &u_length); CeedChk(ierr); 11157a982d89SJeremy L. Thompson } 11167a982d89SJeremy L. Thompson 1117e15f9bd0SJeremy L Thompson if (!basis->Apply) 1118e15f9bd0SJeremy L Thompson // LCOV_EXCL_START 1119e15f9bd0SJeremy L Thompson return CeedError(basis->ceed, CEED_ERROR_UNSUPPORTED, 1120e15f9bd0SJeremy L Thompson "Backend does not support BasisApply"); 1121e15f9bd0SJeremy L Thompson // LCOV_EXCL_STOP 1122e15f9bd0SJeremy L Thompson 1123e15f9bd0SJeremy L Thompson // Check compatibility of topological and geometrical dimensions 1124d1d35e2fSjeremylt if ((t_mode == CEED_TRANSPOSE && (v_length%num_nodes != 0 || 1125d1d35e2fSjeremylt u_length%num_qpts != 0)) || 1126d1d35e2fSjeremylt (t_mode == CEED_NOTRANSPOSE && (u_length%num_nodes != 0 || 1127d1d35e2fSjeremylt v_length%num_qpts != 0))) 11288229195eSjeremylt // LCOV_EXCL_START 1129e15f9bd0SJeremy L Thompson return CeedError(basis->ceed, CEED_ERROR_DIMENSION, 1130e15f9bd0SJeremy L Thompson "Length of input/output vectors " 11317a982d89SJeremy L. Thompson "incompatible with basis dimensions"); 11328229195eSjeremylt // LCOV_EXCL_STOP 11337a982d89SJeremy L. Thompson 1134e15f9bd0SJeremy L Thompson // Check vector lengths to prevent out of bounds issues 1135d1d35e2fSjeremylt bool bad_dims = false; 1136d1d35e2fSjeremylt switch (eval_mode) { 1137e15f9bd0SJeremy L Thompson case CEED_EVAL_NONE: 1138d1d35e2fSjeremylt case CEED_EVAL_INTERP: bad_dims = 1139d1d35e2fSjeremylt ((t_mode == CEED_TRANSPOSE && (u_length < num_elem*num_comp*num_qpts || 1140d1d35e2fSjeremylt v_length < num_elem*num_comp*num_nodes)) || 1141d1d35e2fSjeremylt (t_mode == CEED_NOTRANSPOSE && (v_length < num_elem*num_qpts*num_comp || 1142d1d35e2fSjeremylt u_length < num_elem*num_comp*num_nodes))); 1143e15f9bd0SJeremy L Thompson break; 1144d1d35e2fSjeremylt case CEED_EVAL_GRAD: bad_dims = 1145d1d35e2fSjeremylt ((t_mode == CEED_TRANSPOSE && (u_length < num_elem*num_comp*num_qpts*dim || 1146d1d35e2fSjeremylt v_length < num_elem*num_comp*num_nodes)) || 1147d1d35e2fSjeremylt (t_mode == CEED_NOTRANSPOSE && (v_length < num_elem*num_qpts*num_comp*dim || 1148d1d35e2fSjeremylt u_length < num_elem*num_comp*num_nodes))); 1149e15f9bd0SJeremy L Thompson break; 1150e15f9bd0SJeremy L Thompson case CEED_EVAL_WEIGHT: 1151d1d35e2fSjeremylt bad_dims = v_length < num_elem*num_qpts; 1152e15f9bd0SJeremy L Thompson break; 1153e15f9bd0SJeremy L Thompson // LCOV_EXCL_START 1154d1d35e2fSjeremylt case CEED_EVAL_DIV: bad_dims = 1155d1d35e2fSjeremylt ((t_mode == CEED_TRANSPOSE && (u_length < num_elem*num_comp*num_qpts || 1156d1d35e2fSjeremylt v_length < num_elem*num_comp*num_nodes)) || 1157d1d35e2fSjeremylt (t_mode == CEED_NOTRANSPOSE && (v_length < num_elem*num_qpts*num_comp || 1158d1d35e2fSjeremylt u_length < num_elem*num_comp*num_nodes))); 1159e15f9bd0SJeremy L Thompson break; 1160d1d35e2fSjeremylt case CEED_EVAL_CURL: bad_dims = 1161d1d35e2fSjeremylt ((t_mode == CEED_TRANSPOSE && (u_length < num_elem*num_comp*num_qpts || 1162d1d35e2fSjeremylt v_length < num_elem*num_comp*num_nodes)) || 1163d1d35e2fSjeremylt (t_mode == CEED_NOTRANSPOSE && (v_length < num_elem*num_qpts*num_comp || 1164d1d35e2fSjeremylt u_length < num_elem*num_comp*num_nodes))); 1165e15f9bd0SJeremy L Thompson break; 1166e15f9bd0SJeremy L Thompson // LCOV_EXCL_STOP 1167e15f9bd0SJeremy L Thompson } 1168d1d35e2fSjeremylt if (bad_dims) 1169e15f9bd0SJeremy L Thompson // LCOV_EXCL_START 1170e15f9bd0SJeremy L Thompson return CeedError(basis->ceed, CEED_ERROR_DIMENSION, 1171d1d35e2fSjeremylt "Input/output vectors too short for basis and evaluation mode"); 1172e15f9bd0SJeremy L Thompson // LCOV_EXCL_STOP 1173e15f9bd0SJeremy L Thompson 1174d1d35e2fSjeremylt ierr = basis->Apply(basis, num_elem, t_mode, eval_mode, u, v); CeedChk(ierr); 1175e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11767a982d89SJeremy L. Thompson } 11777a982d89SJeremy L. Thompson 11787a982d89SJeremy L. Thompson /** 1179b7c9bbdaSJeremy L Thompson @brief Get Ceed associated with a CeedBasis 1180b7c9bbdaSJeremy L Thompson 1181b7c9bbdaSJeremy L Thompson @param basis CeedBasis 1182b7c9bbdaSJeremy L Thompson @param[out] ceed Variable to store Ceed 1183b7c9bbdaSJeremy L Thompson 1184b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1185b7c9bbdaSJeremy L Thompson 1186b7c9bbdaSJeremy L Thompson @ref Advanced 1187b7c9bbdaSJeremy L Thompson **/ 1188b7c9bbdaSJeremy L Thompson int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) { 1189b7c9bbdaSJeremy L Thompson *ceed = basis->ceed; 1190b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1191b7c9bbdaSJeremy L Thompson } 1192b7c9bbdaSJeremy L Thompson 1193b7c9bbdaSJeremy L Thompson /** 11949d007619Sjeremylt @brief Get dimension for given CeedBasis 11959d007619Sjeremylt 11969d007619Sjeremylt @param basis CeedBasis 11979d007619Sjeremylt @param[out] dim Variable to store dimension of basis 11989d007619Sjeremylt 11999d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 12009d007619Sjeremylt 1201b7c9bbdaSJeremy L Thompson @ref Advanced 12029d007619Sjeremylt **/ 12039d007619Sjeremylt int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) { 12049d007619Sjeremylt *dim = basis->dim; 1205e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 12069d007619Sjeremylt } 12079d007619Sjeremylt 12089d007619Sjeremylt /** 1209d99fa3c5SJeremy L Thompson @brief Get topology for given CeedBasis 1210d99fa3c5SJeremy L Thompson 1211d99fa3c5SJeremy L Thompson @param basis CeedBasis 1212d99fa3c5SJeremy L Thompson @param[out] topo Variable to store topology of basis 1213d99fa3c5SJeremy L Thompson 1214d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1215d99fa3c5SJeremy L Thompson 1216b7c9bbdaSJeremy L Thompson @ref Advanced 1217d99fa3c5SJeremy L Thompson **/ 1218d99fa3c5SJeremy L Thompson int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) { 1219d99fa3c5SJeremy L Thompson *topo = basis->topo; 1220e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1221d99fa3c5SJeremy L Thompson } 1222d99fa3c5SJeremy L Thompson 1223d99fa3c5SJeremy L Thompson /** 122450c301a5SRezgar Shakeri @brief Get number of Q-vector components for given CeedBasis 122550c301a5SRezgar Shakeri 122650c301a5SRezgar Shakeri @param basis CeedBasis 122750c301a5SRezgar Shakeri @param[out] Q_comp Variable to store number of Q-vector components of basis 122850c301a5SRezgar Shakeri 122950c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 123050c301a5SRezgar Shakeri 123150c301a5SRezgar Shakeri @ref Advanced 123250c301a5SRezgar Shakeri **/ 123350c301a5SRezgar Shakeri int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedInt *Q_comp) { 123450c301a5SRezgar Shakeri *Q_comp = basis->Q_comp; 123550c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 123650c301a5SRezgar Shakeri } 123750c301a5SRezgar Shakeri 123850c301a5SRezgar Shakeri /** 12399d007619Sjeremylt @brief Get number of components for given CeedBasis 12409d007619Sjeremylt 12419d007619Sjeremylt @param basis CeedBasis 1242d1d35e2fSjeremylt @param[out] num_comp Variable to store number of components of basis 12439d007619Sjeremylt 12449d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 12459d007619Sjeremylt 1246b7c9bbdaSJeremy L Thompson @ref Advanced 12479d007619Sjeremylt **/ 1248d1d35e2fSjeremylt int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) { 1249d1d35e2fSjeremylt *num_comp = basis->num_comp; 1250e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 12519d007619Sjeremylt } 12529d007619Sjeremylt 12539d007619Sjeremylt /** 12549d007619Sjeremylt @brief Get total number of nodes (in dim dimensions) of a CeedBasis 12559d007619Sjeremylt 12569d007619Sjeremylt @param basis CeedBasis 12579d007619Sjeremylt @param[out] P Variable to store number of nodes 12589d007619Sjeremylt 12599d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 12609d007619Sjeremylt 12619d007619Sjeremylt @ref Utility 12629d007619Sjeremylt **/ 12639d007619Sjeremylt int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) { 12649d007619Sjeremylt *P = basis->P; 1265e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 12669d007619Sjeremylt } 12679d007619Sjeremylt 12689d007619Sjeremylt /** 12699d007619Sjeremylt @brief Get total number of nodes (in 1 dimension) of a CeedBasis 12709d007619Sjeremylt 12719d007619Sjeremylt @param basis CeedBasis 1272d1d35e2fSjeremylt @param[out] P_1d Variable to store number of nodes 12739d007619Sjeremylt 12749d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 12759d007619Sjeremylt 1276b7c9bbdaSJeremy L Thompson @ref Advanced 12779d007619Sjeremylt **/ 1278d1d35e2fSjeremylt int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) { 1279d1d35e2fSjeremylt if (!basis->tensor_basis) 12809d007619Sjeremylt // LCOV_EXCL_START 1281e15f9bd0SJeremy L Thompson return CeedError(basis->ceed, CEED_ERROR_MINOR, 1282d1d35e2fSjeremylt "Cannot supply P_1d for non-tensor basis"); 12839d007619Sjeremylt // LCOV_EXCL_STOP 12849d007619Sjeremylt 1285d1d35e2fSjeremylt *P_1d = basis->P_1d; 1286e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 12879d007619Sjeremylt } 12889d007619Sjeremylt 12899d007619Sjeremylt /** 12909d007619Sjeremylt @brief Get total number of quadrature points (in dim dimensions) of a CeedBasis 12919d007619Sjeremylt 12929d007619Sjeremylt @param basis CeedBasis 12939d007619Sjeremylt @param[out] Q Variable to store number of quadrature points 12949d007619Sjeremylt 12959d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 12969d007619Sjeremylt 12979d007619Sjeremylt @ref Utility 12989d007619Sjeremylt **/ 12999d007619Sjeremylt int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) { 13009d007619Sjeremylt *Q = basis->Q; 1301e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 13029d007619Sjeremylt } 13039d007619Sjeremylt 13049d007619Sjeremylt /** 13059d007619Sjeremylt @brief Get total number of quadrature points (in 1 dimension) of a CeedBasis 13069d007619Sjeremylt 13079d007619Sjeremylt @param basis CeedBasis 1308d1d35e2fSjeremylt @param[out] Q_1d Variable to store number of quadrature points 13099d007619Sjeremylt 13109d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 13119d007619Sjeremylt 1312b7c9bbdaSJeremy L Thompson @ref Advanced 13139d007619Sjeremylt **/ 1314d1d35e2fSjeremylt int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) { 1315d1d35e2fSjeremylt if (!basis->tensor_basis) 13169d007619Sjeremylt // LCOV_EXCL_START 1317e15f9bd0SJeremy L Thompson return CeedError(basis->ceed, CEED_ERROR_MINOR, 1318d1d35e2fSjeremylt "Cannot supply Q_1d for non-tensor basis"); 13199d007619Sjeremylt // LCOV_EXCL_STOP 13209d007619Sjeremylt 1321d1d35e2fSjeremylt *Q_1d = basis->Q_1d; 1322e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 13239d007619Sjeremylt } 13249d007619Sjeremylt 13259d007619Sjeremylt /** 13269d007619Sjeremylt @brief Get reference coordinates of quadrature points (in dim dimensions) 13279d007619Sjeremylt of a CeedBasis 13289d007619Sjeremylt 13299d007619Sjeremylt @param basis CeedBasis 1330d1d35e2fSjeremylt @param[out] q_ref Variable to store reference coordinates of quadrature points 13319d007619Sjeremylt 13329d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 13339d007619Sjeremylt 1334b7c9bbdaSJeremy L Thompson @ref Advanced 13359d007619Sjeremylt **/ 1336d1d35e2fSjeremylt int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) { 1337d1d35e2fSjeremylt *q_ref = basis->q_ref_1d; 1338e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 13399d007619Sjeremylt } 13409d007619Sjeremylt 13419d007619Sjeremylt /** 13429d007619Sjeremylt @brief Get quadrature weights of quadrature points (in dim dimensions) 13439d007619Sjeremylt of a CeedBasis 13449d007619Sjeremylt 13459d007619Sjeremylt @param basis CeedBasis 1346d1d35e2fSjeremylt @param[out] q_weight Variable to store quadrature weights 13479d007619Sjeremylt 13489d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 13499d007619Sjeremylt 1350b7c9bbdaSJeremy L Thompson @ref Advanced 13519d007619Sjeremylt **/ 1352d1d35e2fSjeremylt int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) { 1353d1d35e2fSjeremylt *q_weight = basis->q_weight_1d; 1354e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 13559d007619Sjeremylt } 13569d007619Sjeremylt 13579d007619Sjeremylt /** 13589d007619Sjeremylt @brief Get interpolation matrix of a CeedBasis 13599d007619Sjeremylt 13609d007619Sjeremylt @param basis CeedBasis 13619d007619Sjeremylt @param[out] interp Variable to store interpolation matrix 13629d007619Sjeremylt 13639d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 13649d007619Sjeremylt 1365b7c9bbdaSJeremy L Thompson @ref Advanced 13669d007619Sjeremylt **/ 13676c58de82SJeremy L Thompson int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) { 1368d1d35e2fSjeremylt if (!basis->interp && basis->tensor_basis) { 13699d007619Sjeremylt // Allocate 13709d007619Sjeremylt int ierr; 13719d007619Sjeremylt ierr = CeedMalloc(basis->Q*basis->P, &basis->interp); CeedChk(ierr); 13729d007619Sjeremylt 13739d007619Sjeremylt // Initialize 13749d007619Sjeremylt for (CeedInt i=0; i<basis->Q*basis->P; i++) 13759d007619Sjeremylt basis->interp[i] = 1.0; 13769d007619Sjeremylt 13779d007619Sjeremylt // Calculate 13789d007619Sjeremylt for (CeedInt d=0; d<basis->dim; d++) 13799d007619Sjeremylt for (CeedInt qpt=0; qpt<basis->Q; qpt++) 13809d007619Sjeremylt for (CeedInt node=0; node<basis->P; node++) { 1381d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 1382d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 1383d1d35e2fSjeremylt basis->interp[qpt*(basis->P)+node] *= basis->interp_1d[q*basis->P_1d+p]; 13849d007619Sjeremylt } 13859d007619Sjeremylt } 13869d007619Sjeremylt *interp = basis->interp; 1387e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 13889d007619Sjeremylt } 13899d007619Sjeremylt 13909d007619Sjeremylt /** 13919d007619Sjeremylt @brief Get 1D interpolation matrix of a tensor product CeedBasis 13929d007619Sjeremylt 13939d007619Sjeremylt @param basis CeedBasis 1394d1d35e2fSjeremylt @param[out] interp_1d Variable to store interpolation matrix 13959d007619Sjeremylt 13969d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 13979d007619Sjeremylt 13989d007619Sjeremylt @ref Backend 13999d007619Sjeremylt **/ 1400d1d35e2fSjeremylt int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) { 1401d1d35e2fSjeremylt if (!basis->tensor_basis) 14029d007619Sjeremylt // LCOV_EXCL_START 1403e15f9bd0SJeremy L Thompson return CeedError(basis->ceed, CEED_ERROR_MINOR, 1404e15f9bd0SJeremy L Thompson "CeedBasis is not a tensor product basis."); 14059d007619Sjeremylt // LCOV_EXCL_STOP 14069d007619Sjeremylt 1407d1d35e2fSjeremylt *interp_1d = basis->interp_1d; 1408e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14099d007619Sjeremylt } 14109d007619Sjeremylt 14119d007619Sjeremylt /** 14129d007619Sjeremylt @brief Get gradient matrix of a CeedBasis 14139d007619Sjeremylt 14149d007619Sjeremylt @param basis CeedBasis 14159d007619Sjeremylt @param[out] grad Variable to store gradient matrix 14169d007619Sjeremylt 14179d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 14189d007619Sjeremylt 1419b7c9bbdaSJeremy L Thompson @ref Advanced 14209d007619Sjeremylt **/ 14216c58de82SJeremy L Thompson int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) { 1422d1d35e2fSjeremylt if (!basis->grad && basis->tensor_basis) { 14239d007619Sjeremylt // Allocate 14249d007619Sjeremylt int ierr; 14259d007619Sjeremylt ierr = CeedMalloc(basis->dim*basis->Q*basis->P, &basis->grad); 14269d007619Sjeremylt CeedChk(ierr); 14279d007619Sjeremylt 14289d007619Sjeremylt // Initialize 14299d007619Sjeremylt for (CeedInt i=0; i<basis->dim*basis->Q*basis->P; i++) 14309d007619Sjeremylt basis->grad[i] = 1.0; 14319d007619Sjeremylt 14329d007619Sjeremylt // Calculate 14339d007619Sjeremylt for (CeedInt d=0; d<basis->dim; d++) 14349d007619Sjeremylt for (CeedInt i=0; i<basis->dim; i++) 14359d007619Sjeremylt for (CeedInt qpt=0; qpt<basis->Q; qpt++) 14369d007619Sjeremylt for (CeedInt node=0; node<basis->P; node++) { 1437d1d35e2fSjeremylt CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 1438d1d35e2fSjeremylt CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 14399d007619Sjeremylt if (i == d) 14409d007619Sjeremylt basis->grad[(i*basis->Q+qpt)*(basis->P)+node] *= 1441d1d35e2fSjeremylt basis->grad_1d[q*basis->P_1d+p]; 14429d007619Sjeremylt else 14439d007619Sjeremylt basis->grad[(i*basis->Q+qpt)*(basis->P)+node] *= 1444d1d35e2fSjeremylt basis->interp_1d[q*basis->P_1d+p]; 14459d007619Sjeremylt } 14469d007619Sjeremylt } 14479d007619Sjeremylt *grad = basis->grad; 1448e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14499d007619Sjeremylt } 14509d007619Sjeremylt 14519d007619Sjeremylt /** 14529d007619Sjeremylt @brief Get 1D gradient matrix of a tensor product CeedBasis 14539d007619Sjeremylt 14549d007619Sjeremylt @param basis CeedBasis 1455d1d35e2fSjeremylt @param[out] grad_1d Variable to store gradient matrix 14569d007619Sjeremylt 14579d007619Sjeremylt @return An error code: 0 - success, otherwise - failure 14589d007619Sjeremylt 1459b7c9bbdaSJeremy L Thompson @ref Advanced 14609d007619Sjeremylt **/ 1461d1d35e2fSjeremylt int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) { 1462d1d35e2fSjeremylt if (!basis->tensor_basis) 14639d007619Sjeremylt // LCOV_EXCL_START 1464e15f9bd0SJeremy L Thompson return CeedError(basis->ceed, CEED_ERROR_MINOR, 1465e15f9bd0SJeremy L Thompson "CeedBasis is not a tensor product basis."); 14669d007619Sjeremylt // LCOV_EXCL_STOP 14679d007619Sjeremylt 1468d1d35e2fSjeremylt *grad_1d = basis->grad_1d; 1469e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14709d007619Sjeremylt } 14719d007619Sjeremylt 14729d007619Sjeremylt /** 147350c301a5SRezgar Shakeri @brief Get divergence matrix of a CeedBasis 147450c301a5SRezgar Shakeri 147550c301a5SRezgar Shakeri @param basis CeedBasis 147650c301a5SRezgar Shakeri @param[out] div Variable to store divergence matrix 147750c301a5SRezgar Shakeri 147850c301a5SRezgar Shakeri @return An error code: 0 - success, otherwise - failure 147950c301a5SRezgar Shakeri 148050c301a5SRezgar Shakeri @ref Advanced 148150c301a5SRezgar Shakeri **/ 148250c301a5SRezgar Shakeri int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) { 148350c301a5SRezgar Shakeri if (!basis->div) 148450c301a5SRezgar Shakeri // LCOV_EXCL_START 148550c301a5SRezgar Shakeri return CeedError(basis->ceed, CEED_ERROR_MINOR, 148650c301a5SRezgar Shakeri "CeedBasis does not have divergence matrix."); 148750c301a5SRezgar Shakeri // LCOV_EXCL_STOP 148850c301a5SRezgar Shakeri 148950c301a5SRezgar Shakeri *div = basis->div; 149050c301a5SRezgar Shakeri return CEED_ERROR_SUCCESS; 149150c301a5SRezgar Shakeri } 149250c301a5SRezgar Shakeri 149350c301a5SRezgar Shakeri /** 14947a982d89SJeremy L. Thompson @brief Destroy a CeedBasis 14957a982d89SJeremy L. Thompson 14967a982d89SJeremy L. Thompson @param basis CeedBasis to destroy 14977a982d89SJeremy L. Thompson 14987a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 14997a982d89SJeremy L. Thompson 15007a982d89SJeremy L. Thompson @ref User 15017a982d89SJeremy L. Thompson **/ 15027a982d89SJeremy L. Thompson int CeedBasisDestroy(CeedBasis *basis) { 15037a982d89SJeremy L. Thompson int ierr; 15047a982d89SJeremy L. Thompson 1505d1d35e2fSjeremylt if (!*basis || --(*basis)->ref_count > 0) return CEED_ERROR_SUCCESS; 15067a982d89SJeremy L. Thompson if ((*basis)->Destroy) { 15077a982d89SJeremy L. Thompson ierr = (*basis)->Destroy(*basis); CeedChk(ierr); 15087a982d89SJeremy L. Thompson } 150934359f16Sjeremylt if ((*basis)->contract) { 151034359f16Sjeremylt ierr = CeedTensorContractDestroy(&(*basis)->contract); CeedChk(ierr); 151134359f16Sjeremylt } 15127a982d89SJeremy L. Thompson ierr = CeedFree(&(*basis)->interp); CeedChk(ierr); 1513d1d35e2fSjeremylt ierr = CeedFree(&(*basis)->interp_1d); CeedChk(ierr); 15147a982d89SJeremy L. Thompson ierr = CeedFree(&(*basis)->grad); CeedChk(ierr); 151550c301a5SRezgar Shakeri ierr = CeedFree(&(*basis)->div); CeedChk(ierr); 1516d1d35e2fSjeremylt ierr = CeedFree(&(*basis)->grad_1d); CeedChk(ierr); 1517d1d35e2fSjeremylt ierr = CeedFree(&(*basis)->q_ref_1d); CeedChk(ierr); 1518d1d35e2fSjeremylt ierr = CeedFree(&(*basis)->q_weight_1d); CeedChk(ierr); 15197a982d89SJeremy L. Thompson ierr = CeedDestroy(&(*basis)->ceed); CeedChk(ierr); 15207a982d89SJeremy L. Thompson ierr = CeedFree(basis); CeedChk(ierr); 1521e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15227a982d89SJeremy L. Thompson } 15237a982d89SJeremy L. Thompson 15247a982d89SJeremy L. Thompson /** 1525b11c1e72Sjeremylt @brief Construct a Gauss-Legendre quadrature 1526b11c1e72Sjeremylt 1527b11c1e72Sjeremylt @param Q Number of quadrature points (integrates polynomials of 1528b11c1e72Sjeremylt degree 2*Q-1 exactly) 1529d1d35e2fSjeremylt @param[out] q_ref_1d Array of length Q to hold the abscissa on [-1, 1] 1530d1d35e2fSjeremylt @param[out] q_weight_1d Array of length Q to hold the weights 1531b11c1e72Sjeremylt 1532b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1533dfdf5a53Sjeremylt 1534dfdf5a53Sjeremylt @ref Utility 1535b11c1e72Sjeremylt **/ 1536d1d35e2fSjeremylt int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, 1537d1d35e2fSjeremylt CeedScalar *q_weight_1d) { 1538d7b241e6Sjeremylt // Allocate 1539d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0*atan(1.0); 1540d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 154192ae7e47SJeremy L Thompson for (CeedInt i = 0; i <= Q/2; i++) { 1542d7b241e6Sjeremylt // Guess 1543d7b241e6Sjeremylt xi = cos(PI*(CeedScalar)(2*i+1)/((CeedScalar)(2*Q))); 1544d7b241e6Sjeremylt // Pn(xi) 1545d7b241e6Sjeremylt P0 = 1.0; 1546d7b241e6Sjeremylt P1 = xi; 1547d7b241e6Sjeremylt P2 = 0.0; 154892ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 1549d7b241e6Sjeremylt P2 = (((CeedScalar)(2*j-1))*xi*P1-((CeedScalar)(j-1))*P0)/((CeedScalar)(j)); 1550d7b241e6Sjeremylt P0 = P1; 1551d7b241e6Sjeremylt P1 = P2; 1552d7b241e6Sjeremylt } 1553d7b241e6Sjeremylt // First Newton Step 1554d7b241e6Sjeremylt dP2 = (xi*P2 - P0)*(CeedScalar)Q/(xi*xi-1.0); 1555d7b241e6Sjeremylt xi = xi-P2/dP2; 1556d7b241e6Sjeremylt // Newton to convergence 155792ae7e47SJeremy L Thompson for (CeedInt k=0; k<100 && fabs(P2)>10*CEED_EPSILON; k++) { 1558d7b241e6Sjeremylt P0 = 1.0; 1559d7b241e6Sjeremylt P1 = xi; 156092ae7e47SJeremy L Thompson for (CeedInt j = 2; j <= Q; j++) { 1561d7b241e6Sjeremylt P2 = (((CeedScalar)(2*j-1))*xi*P1-((CeedScalar)(j-1))*P0)/((CeedScalar)(j)); 1562d7b241e6Sjeremylt P0 = P1; 1563d7b241e6Sjeremylt P1 = P2; 1564d7b241e6Sjeremylt } 1565d7b241e6Sjeremylt dP2 = (xi*P2 - P0)*(CeedScalar)Q/(xi*xi-1.0); 1566d7b241e6Sjeremylt xi = xi-P2/dP2; 1567d7b241e6Sjeremylt } 1568d7b241e6Sjeremylt // Save xi, wi 1569d7b241e6Sjeremylt wi = 2.0/((1.0-xi*xi)*dP2*dP2); 1570d1d35e2fSjeremylt q_weight_1d[i] = wi; 1571d1d35e2fSjeremylt q_weight_1d[Q-1-i] = wi; 1572d1d35e2fSjeremylt q_ref_1d[i] = -xi; 1573d1d35e2fSjeremylt q_ref_1d[Q-1-i]= xi; 1574d7b241e6Sjeremylt } 1575e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1576d7b241e6Sjeremylt } 1577d7b241e6Sjeremylt 1578b11c1e72Sjeremylt /** 1579b11c1e72Sjeremylt @brief Construct a Gauss-Legendre-Lobatto quadrature 1580b11c1e72Sjeremylt 1581b11c1e72Sjeremylt @param Q Number of quadrature points (integrates polynomials of 1582b11c1e72Sjeremylt degree 2*Q-3 exactly) 1583d1d35e2fSjeremylt @param[out] q_ref_1d Array of length Q to hold the abscissa on [-1, 1] 1584d1d35e2fSjeremylt @param[out] q_weight_1d Array of length Q to hold the weights 1585b11c1e72Sjeremylt 1586b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1587dfdf5a53Sjeremylt 1588dfdf5a53Sjeremylt @ref Utility 1589b11c1e72Sjeremylt **/ 1590d1d35e2fSjeremylt int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, 1591d1d35e2fSjeremylt CeedScalar *q_weight_1d) { 1592d7b241e6Sjeremylt // Allocate 1593d7b241e6Sjeremylt CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0*atan(1.0); 1594d1d35e2fSjeremylt // Build q_ref_1d, q_weight_1d 1595d7b241e6Sjeremylt // Set endpoints 159630a100c3SJed Brown if (Q < 2) 1597b0d62198Sjeremylt // LCOV_EXCL_START 1598e15f9bd0SJeremy L Thompson return CeedError(NULL, CEED_ERROR_DIMENSION, 1599*990fdeb6SJeremy L Thompson "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q); 1600b0d62198Sjeremylt // LCOV_EXCL_STOP 1601d7b241e6Sjeremylt wi = 2.0/((CeedScalar)(Q*(Q-1))); 1602d1d35e2fSjeremylt if (q_weight_1d) { 1603d1d35e2fSjeremylt q_weight_1d[0] = wi; 1604d1d35e2fSjeremylt q_weight_1d[Q-1] = wi; 1605d7b241e6Sjeremylt } 1606d1d35e2fSjeremylt q_ref_1d[0] = -1.0; 1607d1d35e2fSjeremylt q_ref_1d[Q-1] = 1.0; 1608d7b241e6Sjeremylt // Interior 160992ae7e47SJeremy L Thompson for (CeedInt i = 1; i <= (Q-1)/2; i++) { 1610d7b241e6Sjeremylt // Guess 1611d7b241e6Sjeremylt xi = cos(PI*(CeedScalar)(i)/(CeedScalar)(Q-1)); 1612d7b241e6Sjeremylt // Pn(xi) 1613d7b241e6Sjeremylt P0 = 1.0; 1614d7b241e6Sjeremylt P1 = xi; 1615d7b241e6Sjeremylt P2 = 0.0; 161692ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 1617d7b241e6Sjeremylt P2 = (((CeedScalar)(2*j-1))*xi*P1-((CeedScalar)(j-1))*P0)/((CeedScalar)(j)); 1618d7b241e6Sjeremylt P0 = P1; 1619d7b241e6Sjeremylt P1 = P2; 1620d7b241e6Sjeremylt } 1621d7b241e6Sjeremylt // First Newton step 1622d7b241e6Sjeremylt dP2 = (xi*P2 - P0)*(CeedScalar)Q/(xi*xi-1.0); 1623d7b241e6Sjeremylt d2P2 = (2*xi*dP2 - (CeedScalar)(Q*(Q-1))*P2)/(1.0-xi*xi); 1624d7b241e6Sjeremylt xi = xi-dP2/d2P2; 1625d7b241e6Sjeremylt // Newton to convergence 162692ae7e47SJeremy L Thompson for (CeedInt k=0; k<100 && fabs(dP2)>10*CEED_EPSILON; k++) { 1627d7b241e6Sjeremylt P0 = 1.0; 1628d7b241e6Sjeremylt P1 = xi; 162992ae7e47SJeremy L Thompson for (CeedInt j = 2; j < Q; j++) { 1630d7b241e6Sjeremylt P2 = (((CeedScalar)(2*j-1))*xi*P1-((CeedScalar)(j-1))*P0)/((CeedScalar)(j)); 1631d7b241e6Sjeremylt P0 = P1; 1632d7b241e6Sjeremylt P1 = P2; 1633d7b241e6Sjeremylt } 1634d7b241e6Sjeremylt dP2 = (xi*P2 - P0)*(CeedScalar)Q/(xi*xi-1.0); 1635d7b241e6Sjeremylt d2P2 = (2*xi*dP2 - (CeedScalar)(Q*(Q-1))*P2)/(1.0-xi*xi); 1636d7b241e6Sjeremylt xi = xi-dP2/d2P2; 1637d7b241e6Sjeremylt } 1638d7b241e6Sjeremylt // Save xi, wi 1639d7b241e6Sjeremylt wi = 2.0/(((CeedScalar)(Q*(Q-1)))*P2*P2); 1640d1d35e2fSjeremylt if (q_weight_1d) { 1641d1d35e2fSjeremylt q_weight_1d[i] = wi; 1642d1d35e2fSjeremylt q_weight_1d[Q-1-i] = wi; 1643d7b241e6Sjeremylt } 1644d1d35e2fSjeremylt q_ref_1d[i] = -xi; 1645d1d35e2fSjeremylt q_ref_1d[Q-1-i]= xi; 1646d7b241e6Sjeremylt } 1647e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1648d7b241e6Sjeremylt } 1649d7b241e6Sjeremylt 1650dfdf5a53Sjeremylt /** 165195bb1877Svaleriabarra @brief Return QR Factorization of a matrix 1652b11c1e72Sjeremylt 165377645d7bSjeremylt @param ceed A Ceed context for error handling 165452bfb9bbSJeremy L Thompson @param[in,out] mat Row-major matrix to be factorized in place 165552bfb9bbSJeremy L Thompson @param[in,out] tau Vector of length m of scaling factors 1656b11c1e72Sjeremylt @param m Number of rows 1657b11c1e72Sjeremylt @param n Number of columns 1658b11c1e72Sjeremylt 1659b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1660dfdf5a53Sjeremylt 1661dfdf5a53Sjeremylt @ref Utility 1662b11c1e72Sjeremylt **/ 1663a7bd39daSjeremylt int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau, 1664d7b241e6Sjeremylt CeedInt m, CeedInt n) { 1665d7b241e6Sjeremylt CeedScalar v[m]; 1666d7b241e6Sjeremylt 1667a7bd39daSjeremylt // Check m >= n 1668a7bd39daSjeremylt if (n > m) 1669c042f62fSJeremy L Thompson // LCOV_EXCL_START 1670e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, 1671e15f9bd0SJeremy L Thompson "Cannot compute QR factorization with n > m"); 1672c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 1673a7bd39daSjeremylt 167452bfb9bbSJeremy L Thompson for (CeedInt i=0; i<n; i++) { 1675bde37e8cSJed Brown if (i >= m-1) { // last row of matrix, no reflection needed 1676bde37e8cSJed Brown tau[i] = 0.; 1677bde37e8cSJed Brown break; 1678bde37e8cSJed Brown } 1679d7b241e6Sjeremylt // Calculate Householder vector, magnitude 1680d7b241e6Sjeremylt CeedScalar sigma = 0.0; 1681d7b241e6Sjeremylt v[i] = mat[i+n*i]; 168252bfb9bbSJeremy L Thompson for (CeedInt j=i+1; j<m; j++) { 1683d7b241e6Sjeremylt v[j] = mat[i+n*j]; 1684d7b241e6Sjeremylt sigma += v[j] * v[j]; 1685d7b241e6Sjeremylt } 1686d7b241e6Sjeremylt CeedScalar norm = sqrt(v[i]*v[i] + sigma); // norm of v[i:m] 1687673160d7Sjeremylt CeedScalar R_ii = -copysign(norm, v[i]); 1688673160d7Sjeremylt v[i] -= R_ii; 1689d7b241e6Sjeremylt // norm of v[i:m] after modification above and scaling below 1690d7b241e6Sjeremylt // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 1691d7b241e6Sjeremylt // tau = 2 / (norm*norm) 1692d7b241e6Sjeremylt tau[i] = 2 * v[i]*v[i] / (v[i]*v[i] + sigma); 16931d102b48SJeremy L Thompson for (CeedInt j=i+1; j<m; j++) 16941d102b48SJeremy L Thompson v[j] /= v[i]; 1695d7b241e6Sjeremylt 1696d7b241e6Sjeremylt // Apply Householder reflector to lower right panel 1697d7b241e6Sjeremylt CeedHouseholderReflect(&mat[i*n+i+1], &v[i], tau[i], m-i, n-i-1, n, 1); 1698d7b241e6Sjeremylt // Save v 1699673160d7Sjeremylt mat[i+n*i] = R_ii; 17001d102b48SJeremy L Thompson for (CeedInt j=i+1; j<m; j++) 1701d7b241e6Sjeremylt mat[i+n*j] = v[j]; 1702d7b241e6Sjeremylt } 1703e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1704d7b241e6Sjeremylt } 1705d7b241e6Sjeremylt 1706b11c1e72Sjeremylt /** 170752bfb9bbSJeremy L Thompson @brief Return symmetric Schur decomposition of the symmetric matrix mat via 170852bfb9bbSJeremy L Thompson symmetric QR factorization 170952bfb9bbSJeremy L Thompson 171077645d7bSjeremylt @param ceed A Ceed context for error handling 171152bfb9bbSJeremy L Thompson @param[in,out] mat Row-major matrix to be factorized in place 1712460bf743SValeria Barra @param[out] lambda Vector of length n of eigenvalues 171352bfb9bbSJeremy L Thompson @param n Number of rows/columns 171452bfb9bbSJeremy L Thompson 171552bfb9bbSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 171652bfb9bbSJeremy L Thompson 171752bfb9bbSJeremy L Thompson @ref Utility 171852bfb9bbSJeremy L Thompson **/ 171903d18186Sjeremylt CeedPragmaOptimizeOff 172052bfb9bbSJeremy L Thompson int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat, 172152bfb9bbSJeremy L Thompson CeedScalar *lambda, CeedInt n) { 172252bfb9bbSJeremy L Thompson // Check bounds for clang-tidy 172352bfb9bbSJeremy L Thompson if (n<2) 1724c042f62fSJeremy L Thompson // LCOV_EXCL_START 1725e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, 1726c042f62fSJeremy L Thompson "Cannot compute symmetric Schur decomposition of scalars"); 1727c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 172852bfb9bbSJeremy L Thompson 1729673160d7Sjeremylt CeedScalar v[n-1], tau[n-1], mat_T[n*n]; 173052bfb9bbSJeremy L Thompson 1731673160d7Sjeremylt // Copy mat to mat_T and set mat to I 1732673160d7Sjeremylt memcpy(mat_T, mat, n*n*sizeof(mat[0])); 173352bfb9bbSJeremy L Thompson for (CeedInt i=0; i<n; i++) 173452bfb9bbSJeremy L Thompson for (CeedInt j=0; j<n; j++) 173552bfb9bbSJeremy L Thompson mat[j+n*i] = (i==j) ? 1 : 0; 173652bfb9bbSJeremy L Thompson 173752bfb9bbSJeremy L Thompson // Reduce to tridiagonal 173852bfb9bbSJeremy L Thompson for (CeedInt i=0; i<n-1; i++) { 173952bfb9bbSJeremy L Thompson // Calculate Householder vector, magnitude 174052bfb9bbSJeremy L Thompson CeedScalar sigma = 0.0; 1741673160d7Sjeremylt v[i] = mat_T[i+n*(i+1)]; 174252bfb9bbSJeremy L Thompson for (CeedInt j=i+1; j<n-1; j++) { 1743673160d7Sjeremylt v[j] = mat_T[i+n*(j+1)]; 174452bfb9bbSJeremy L Thompson sigma += v[j] * v[j]; 174552bfb9bbSJeremy L Thompson } 174652bfb9bbSJeremy L Thompson CeedScalar norm = sqrt(v[i]*v[i] + sigma); // norm of v[i:n-1] 1747673160d7Sjeremylt CeedScalar R_ii = -copysign(norm, v[i]); 1748673160d7Sjeremylt v[i] -= R_ii; 174952bfb9bbSJeremy L Thompson // norm of v[i:m] after modification above and scaling below 175052bfb9bbSJeremy L Thompson // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 175152bfb9bbSJeremy L Thompson // tau = 2 / (norm*norm) 175280a9ef05SNatalie Beams tau[i] = i == n - 2 ? 2 : 2 * v[i]*v[i] / (v[i]*v[i] + sigma); 1753fb551037Sjeremylt for (CeedInt j=i+1; j<n-1; j++) 1754fb551037Sjeremylt v[j] /= v[i]; 175552bfb9bbSJeremy L Thompson 175652bfb9bbSJeremy L Thompson // Update sub and super diagonal 175752bfb9bbSJeremy L Thompson for (CeedInt j=i+2; j<n; j++) { 1758673160d7Sjeremylt mat_T[i+n*j] = 0; mat_T[j+n*i] = 0; 175952bfb9bbSJeremy L Thompson } 176052bfb9bbSJeremy L Thompson // Apply symmetric Householder reflector to lower right panel 1761673160d7Sjeremylt CeedHouseholderReflect(&mat_T[(i+1)+n*(i+1)], &v[i], tau[i], 176252bfb9bbSJeremy L Thompson n-(i+1), n-(i+1), n, 1); 1763673160d7Sjeremylt CeedHouseholderReflect(&mat_T[(i+1)+n*(i+1)], &v[i], tau[i], 176452bfb9bbSJeremy L Thompson n-(i+1), n-(i+1), 1, n); 1765673160d7Sjeremylt 176652bfb9bbSJeremy L Thompson // Save v 1767673160d7Sjeremylt mat_T[i+n*(i+1)] = R_ii; 1768673160d7Sjeremylt mat_T[(i+1)+n*i] = R_ii; 176952bfb9bbSJeremy L Thompson for (CeedInt j=i+1; j<n-1; j++) { 1770673160d7Sjeremylt mat_T[i+n*(j+1)] = v[j]; 177152bfb9bbSJeremy L Thompson } 177252bfb9bbSJeremy L Thompson } 177352bfb9bbSJeremy L Thompson // Backwards accumulation of Q 177452bfb9bbSJeremy L Thompson for (CeedInt i=n-2; i>=0; i--) { 177585cf89eaSjeremylt if (tau[i] > 0.0) { 177652bfb9bbSJeremy L Thompson v[i] = 1; 177752bfb9bbSJeremy L Thompson for (CeedInt j=i+1; j<n-1; j++) { 1778673160d7Sjeremylt v[j] = mat_T[i+n*(j+1)]; 1779673160d7Sjeremylt mat_T[i+n*(j+1)] = 0; 178052bfb9bbSJeremy L Thompson } 178152bfb9bbSJeremy L Thompson CeedHouseholderReflect(&mat[(i+1)+n*(i+1)], &v[i], tau[i], 178252bfb9bbSJeremy L Thompson n-(i+1), n-(i+1), n, 1); 178352bfb9bbSJeremy L Thompson } 178485cf89eaSjeremylt } 178552bfb9bbSJeremy L Thompson 178652bfb9bbSJeremy L Thompson // Reduce sub and super diagonal 1787673160d7Sjeremylt CeedInt p = 0, q = 0, itr = 0, max_itr = n*n*n*n; 1788673160d7Sjeremylt CeedScalar tol = CEED_EPSILON; 178952bfb9bbSJeremy L Thompson 1790673160d7Sjeremylt while (itr < max_itr) { 179152bfb9bbSJeremy L Thompson // Update p, q, size of reduced portions of diagonal 179252bfb9bbSJeremy L Thompson p = 0; q = 0; 179352bfb9bbSJeremy L Thompson for (CeedInt i=n-2; i>=0; i--) { 1794673160d7Sjeremylt if (fabs(mat_T[i+n*(i+1)]) < tol) 179552bfb9bbSJeremy L Thompson q += 1; 179652bfb9bbSJeremy L Thompson else 179752bfb9bbSJeremy L Thompson break; 179852bfb9bbSJeremy L Thompson } 1799673160d7Sjeremylt for (CeedInt i=0; i<n-q-1; i++) { 1800673160d7Sjeremylt if (fabs(mat_T[i+n*(i+1)]) < tol) 180152bfb9bbSJeremy L Thompson p += 1; 180252bfb9bbSJeremy L Thompson else 180352bfb9bbSJeremy L Thompson break; 180452bfb9bbSJeremy L Thompson } 180552bfb9bbSJeremy L Thompson if (q == n-1) break; // Finished reducing 180652bfb9bbSJeremy L Thompson 180752bfb9bbSJeremy L Thompson // Reduce tridiagonal portion 1808673160d7Sjeremylt CeedScalar t_nn = mat_T[(n-1-q)+n*(n-1-q)], 1809673160d7Sjeremylt t_nnm1 = mat_T[(n-2-q)+n*(n-1-q)]; 1810673160d7Sjeremylt CeedScalar d = (mat_T[(n-2-q)+n*(n-2-q)] - t_nn)/2; 1811673160d7Sjeremylt CeedScalar mu = t_nn - t_nnm1*t_nnm1 / 1812673160d7Sjeremylt (d + copysign(sqrt(d*d + t_nnm1*t_nnm1), d)); 1813673160d7Sjeremylt CeedScalar x = mat_T[p+n*p] - mu; 1814673160d7Sjeremylt CeedScalar z = mat_T[p+n*(p+1)]; 1815673160d7Sjeremylt for (CeedInt k=p; k<n-q-1; k++) { 181652bfb9bbSJeremy L Thompson // Compute Givens rotation 181752bfb9bbSJeremy L Thompson CeedScalar c = 1, s = 0; 181852bfb9bbSJeremy L Thompson if (fabs(z) > tol) { 181952bfb9bbSJeremy L Thompson if (fabs(z) > fabs(x)) { 182052bfb9bbSJeremy L Thompson CeedScalar tau = -x/z; 182152bfb9bbSJeremy L Thompson s = 1/sqrt(1+tau*tau), c = s*tau; 182252bfb9bbSJeremy L Thompson } else { 182352bfb9bbSJeremy L Thompson CeedScalar tau = -z/x; 182452bfb9bbSJeremy L Thompson c = 1/sqrt(1+tau*tau), s = c*tau; 182552bfb9bbSJeremy L Thompson } 182652bfb9bbSJeremy L Thompson } 182752bfb9bbSJeremy L Thompson 182852bfb9bbSJeremy L Thompson // Apply Givens rotation to T 1829673160d7Sjeremylt CeedGivensRotation(mat_T, c, s, CEED_NOTRANSPOSE, k, k+1, n, n); 1830673160d7Sjeremylt CeedGivensRotation(mat_T, c, s, CEED_TRANSPOSE, k, k+1, n, n); 183152bfb9bbSJeremy L Thompson 183252bfb9bbSJeremy L Thompson // Apply Givens rotation to Q 183352bfb9bbSJeremy L Thompson CeedGivensRotation(mat, c, s, CEED_NOTRANSPOSE, k, k+1, n, n); 183452bfb9bbSJeremy L Thompson 183552bfb9bbSJeremy L Thompson // Update x, z 183652bfb9bbSJeremy L Thompson if (k < n-q-2) { 1837673160d7Sjeremylt x = mat_T[k+n*(k+1)]; 1838673160d7Sjeremylt z = mat_T[k+n*(k+2)]; 183952bfb9bbSJeremy L Thompson } 184052bfb9bbSJeremy L Thompson } 184152bfb9bbSJeremy L Thompson itr++; 184252bfb9bbSJeremy L Thompson } 1843673160d7Sjeremylt 184452bfb9bbSJeremy L Thompson // Save eigenvalues 184552bfb9bbSJeremy L Thompson for (CeedInt i=0; i<n; i++) 1846673160d7Sjeremylt lambda[i] = mat_T[i+n*i]; 184752bfb9bbSJeremy L Thompson 184852bfb9bbSJeremy L Thompson // Check convergence 1849673160d7Sjeremylt if (itr == max_itr && q < n-1) 1850c042f62fSJeremy L Thompson // LCOV_EXCL_START 1851e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_MINOR, 1852e15f9bd0SJeremy L Thompson "Symmetric QR failed to converge"); 1853c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 1854e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 185552bfb9bbSJeremy L Thompson } 185603d18186Sjeremylt CeedPragmaOptimizeOn 185752bfb9bbSJeremy L Thompson 185852bfb9bbSJeremy L Thompson /** 185952bfb9bbSJeremy L Thompson @brief Return Simultaneous Diagonalization of two matrices. This solves the 186052bfb9bbSJeremy L Thompson generalized eigenvalue problem A x = lambda B x, where A and B 186152bfb9bbSJeremy L Thompson are symmetric and B is positive definite. We generate the matrix X 186252bfb9bbSJeremy L Thompson and vector Lambda such that X^T A X = Lambda and X^T B X = I. This 186352bfb9bbSJeremy L Thompson is equivalent to the LAPACK routine 'sygv' with TYPE = 1. 186452bfb9bbSJeremy L Thompson 186577645d7bSjeremylt @param ceed A Ceed context for error handling 1866d1d35e2fSjeremylt @param[in] mat_A Row-major matrix to be factorized with eigenvalues 1867d1d35e2fSjeremylt @param[in] mat_B Row-major matrix to be factorized to identity 1868d3331725Sjeremylt @param[out] mat_X Row-major orthogonal matrix 1869460bf743SValeria Barra @param[out] lambda Vector of length n of generalized eigenvalues 187052bfb9bbSJeremy L Thompson @param n Number of rows/columns 187152bfb9bbSJeremy L Thompson 187252bfb9bbSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 187352bfb9bbSJeremy L Thompson 187452bfb9bbSJeremy L Thompson @ref Utility 187552bfb9bbSJeremy L Thompson **/ 187603d18186Sjeremylt CeedPragmaOptimizeOff 1877d1d35e2fSjeremylt int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A, 1878d3331725Sjeremylt CeedScalar *mat_B, CeedScalar *mat_X, 187952bfb9bbSJeremy L Thompson CeedScalar *lambda, CeedInt n) { 188052bfb9bbSJeremy L Thompson int ierr; 1881d3331725Sjeremylt CeedScalar *mat_C, *mat_G, *vec_D; 188278464608Sjeremylt ierr = CeedCalloc(n*n, &mat_C); CeedChk(ierr); 188378464608Sjeremylt ierr = CeedCalloc(n*n, &mat_G); CeedChk(ierr); 1884d3331725Sjeremylt ierr = CeedCalloc(n, &vec_D); CeedChk(ierr); 188552bfb9bbSJeremy L Thompson 188652bfb9bbSJeremy L Thompson // Compute B = G D G^T 188778464608Sjeremylt memcpy(mat_G, mat_B, n*n*sizeof(mat_B[0])); 1888d3331725Sjeremylt ierr = CeedSymmetricSchurDecomposition(ceed, mat_G, vec_D, n); CeedChk(ierr); 188952bfb9bbSJeremy L Thompson 189085cf89eaSjeremylt // Sort eigenvalues 189185cf89eaSjeremylt for (CeedInt i=n-1; i>=0; i--) 189285cf89eaSjeremylt for (CeedInt j=0; j<i; j++) { 189385cf89eaSjeremylt if (fabs(vec_D[j]) > fabs(vec_D[j+1])) { 189485cf89eaSjeremylt CeedScalar temp; 189585cf89eaSjeremylt temp = vec_D[j]; vec_D[j] = vec_D[j+1]; vec_D[j+1] = temp; 189685cf89eaSjeremylt for (CeedInt k=0; k<n; k++) { 189785cf89eaSjeremylt temp = mat_G[k*n+j]; mat_G[k*n+j] = mat_G[k*n+j+1]; mat_G[k*n+j+1] = temp; 189885cf89eaSjeremylt } 189985cf89eaSjeremylt } 190085cf89eaSjeremylt } 190185cf89eaSjeremylt 1902fb551037Sjeremylt // Compute C = (G D^1/2)^-1 A (G D^1/2)^-T 1903fb551037Sjeremylt // = D^-1/2 G^T A G D^-1/2 1904d3331725Sjeremylt // -- D = D^-1/2 190552bfb9bbSJeremy L Thompson for (CeedInt i=0; i<n; i++) 1906d3331725Sjeremylt vec_D[i] = 1./sqrt(vec_D[i]); 1907d3331725Sjeremylt // -- G = G D^-1/2 1908d3331725Sjeremylt // -- C = D^-1/2 G^T 1909d3331725Sjeremylt for (CeedInt i=0; i<n; i++) 1910d3331725Sjeremylt for (CeedInt j=0; j<n; j++) { 1911673160d7Sjeremylt mat_G[i*n+j] *= vec_D[j]; 1912673160d7Sjeremylt mat_C[j*n+i] = mat_G[i*n+j]; 1913d3331725Sjeremylt } 1914673160d7Sjeremylt // -- X = (D^-1/2 G^T) A 1915ed9e99e6SJeremy L Thompson ierr = CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_C, 1916d3331725Sjeremylt (const CeedScalar *)mat_A, mat_X, n, n, n); 19179289e5bfSjeremylt CeedChk(ierr); 1918673160d7Sjeremylt // -- C = (D^-1/2 G^T A) (G D^-1/2) 1919ed9e99e6SJeremy L Thompson ierr = CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_X, 192078464608Sjeremylt (const CeedScalar *)mat_G, mat_C, n, n, n); 19219289e5bfSjeremylt CeedChk(ierr); 192252bfb9bbSJeremy L Thompson 192352bfb9bbSJeremy L Thompson // Compute Q^T C Q = lambda 1924d1d35e2fSjeremylt ierr = CeedSymmetricSchurDecomposition(ceed, mat_C, lambda, n); CeedChk(ierr); 192552bfb9bbSJeremy L Thompson 192685cf89eaSjeremylt // Sort eigenvalues 192785cf89eaSjeremylt for (CeedInt i=n-1; i>=0; i--) 192885cf89eaSjeremylt for (CeedInt j=0; j<i; j++) { 192985cf89eaSjeremylt if (fabs(lambda[j]) > fabs(lambda[j+1])) { 193085cf89eaSjeremylt CeedScalar temp; 193185cf89eaSjeremylt temp = lambda[j]; lambda[j] = lambda[j+1]; lambda[j+1] = temp; 193285cf89eaSjeremylt for (CeedInt k=0; k<n; k++) { 193385cf89eaSjeremylt temp = mat_C[k*n+j]; mat_C[k*n+j] = mat_C[k*n+j+1]; mat_C[k*n+j+1] = temp; 193485cf89eaSjeremylt } 193585cf89eaSjeremylt } 193685cf89eaSjeremylt } 193785cf89eaSjeremylt 1938d3331725Sjeremylt // Set X = (G D^1/2)^-T Q 1939fb551037Sjeremylt // = G D^-1/2 Q 1940ed9e99e6SJeremy L Thompson ierr = CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_G, 1941d3331725Sjeremylt (const CeedScalar *)mat_C, mat_X, n, n, n); 19429289e5bfSjeremylt CeedChk(ierr); 194378464608Sjeremylt 194478464608Sjeremylt // Cleanup 194578464608Sjeremylt ierr = CeedFree(&mat_C); CeedChk(ierr); 194678464608Sjeremylt ierr = CeedFree(&mat_G); CeedChk(ierr); 1947d3331725Sjeremylt ierr = CeedFree(&vec_D); CeedChk(ierr); 1948e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 194952bfb9bbSJeremy L Thompson } 195003d18186Sjeremylt CeedPragmaOptimizeOn 195152bfb9bbSJeremy L Thompson 1952d7b241e6Sjeremylt /// @} 1953