1 // Copyright (c) 2017-2025, Lawrence Livermore National Security, LLC and other CEED contributors. 2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3 // 4 // SPDX-License-Identifier: BSD-2-Clause 5 // 6 // This file is part of CEED: http://github.com/ceed 7 8 #include <ceed-impl.h> 9 #include <ceed.h> 10 #include <ceed/backend.h> 11 #include <math.h> 12 #include <stdbool.h> 13 #include <stdio.h> 14 #include <string.h> 15 16 /// @file 17 /// Implementation of CeedBasis interfaces 18 19 /// @cond DOXYGEN_SKIP 20 static struct CeedBasis_private ceed_basis_none; 21 /// @endcond 22 23 /// @addtogroup CeedBasisUser 24 /// @{ 25 26 /// Argument for @ref CeedOperatorSetField() indicating that the field does not require a `CeedBasis` 27 const CeedBasis CEED_BASIS_NONE = &ceed_basis_none; 28 29 /// @} 30 31 /// ---------------------------------------------------------------------------- 32 /// CeedBasis Library Internal Functions 33 /// ---------------------------------------------------------------------------- 34 /// @addtogroup CeedBasisDeveloper 35 /// @{ 36 37 /** 38 @brief Compute Chebyshev polynomial values at a point 39 40 @param[in] x Coordinate to evaluate Chebyshev polynomials at 41 @param[in] n Number of Chebyshev polynomials to evaluate, `n >= 2` 42 @param[out] chebyshev_x Array of Chebyshev polynomial values 43 44 @return An error code: 0 - success, otherwise - failure 45 46 @ref Developer 47 **/ 48 static int CeedChebyshevPolynomialsAtPoint(CeedScalar x, CeedInt n, CeedScalar *chebyshev_x) { 49 chebyshev_x[0] = 1.0; 50 chebyshev_x[1] = 2 * x; 51 for (CeedInt i = 2; i < n; i++) chebyshev_x[i] = 2 * x * chebyshev_x[i - 1] - chebyshev_x[i - 2]; 52 return CEED_ERROR_SUCCESS; 53 } 54 55 /** 56 @brief Compute values of the derivative of Chebyshev polynomials at a point 57 58 @param[in] x Coordinate to evaluate derivative of Chebyshev polynomials at 59 @param[in] n Number of Chebyshev polynomials to evaluate, `n >= 2` 60 @param[out] chebyshev_dx Array of Chebyshev polynomial derivative values 61 62 @return An error code: 0 - success, otherwise - failure 63 64 @ref Developer 65 **/ 66 static int CeedChebyshevDerivativeAtPoint(CeedScalar x, CeedInt n, CeedScalar *chebyshev_dx) { 67 CeedScalar chebyshev_x[3]; 68 69 chebyshev_x[1] = 1.0; 70 chebyshev_x[2] = 2 * x; 71 chebyshev_dx[0] = 0.0; 72 chebyshev_dx[1] = 2.0; 73 for (CeedInt i = 2; i < n; i++) { 74 chebyshev_x[0] = chebyshev_x[1]; 75 chebyshev_x[1] = chebyshev_x[2]; 76 chebyshev_x[2] = 2 * x * chebyshev_x[1] - chebyshev_x[0]; 77 chebyshev_dx[i] = 2 * x * chebyshev_dx[i - 1] + 2 * chebyshev_x[1] - chebyshev_dx[i - 2]; 78 } 79 return CEED_ERROR_SUCCESS; 80 } 81 82 /** 83 @brief Compute Householder reflection. 84 85 Computes \f$A = (I - b v v^T) A\f$, where \f$A\f$ is an \f$m \times n\f$ matrix indexed as `A[i*row + j*col]`. 86 87 @param[in,out] A Matrix to apply Householder reflection to, in place 88 @param[in] v Householder vector 89 @param[in] b Scaling factor 90 @param[in] m Number of rows in `A` 91 @param[in] n Number of columns in `A` 92 @param[in] row Row stride 93 @param[in] col Col stride 94 95 @return An error code: 0 - success, otherwise - failure 96 97 @ref Developer 98 **/ 99 static int CeedHouseholderReflect(CeedScalar *A, const CeedScalar *v, CeedScalar b, CeedInt m, CeedInt n, CeedInt row, CeedInt col) { 100 for (CeedInt j = 0; j < n; j++) { 101 CeedScalar w = A[0 * row + j * col]; 102 103 for (CeedInt i = 1; i < m; i++) w += v[i] * A[i * row + j * col]; 104 A[0 * row + j * col] -= b * w; 105 for (CeedInt i = 1; i < m; i++) A[i * row + j * col] -= b * w * v[i]; 106 } 107 return CEED_ERROR_SUCCESS; 108 } 109 110 /** 111 @brief Compute Givens rotation 112 113 Computes \f$A = G A\f$ (or \f$G^T A\f$ in transpose mode), where \f$A\f$ is an \f$m \times n\f$ matrix indexed as `A[i*n + j*m]`. 114 115 @param[in,out] A Row major matrix to apply Givens rotation to, in place 116 @param[in] c Cosine factor 117 @param[in] s Sine factor 118 @param[in] t_mode @ref CEED_NOTRANSPOSE to rotate the basis counter-clockwise, which has the effect of rotating columns of `A` clockwise; 119 @ref CEED_TRANSPOSE for the opposite rotation 120 @param[in] i First row/column to apply rotation 121 @param[in] k Second row/column to apply rotation 122 @param[in] m Number of rows in `A` 123 @param[in] n Number of columns in `A` 124 125 @return An error code: 0 - success, otherwise - failure 126 127 @ref Developer 128 **/ 129 static int CeedGivensRotation(CeedScalar *A, CeedScalar c, CeedScalar s, CeedTransposeMode t_mode, CeedInt i, CeedInt k, CeedInt m, CeedInt n) { 130 CeedInt stride_j = 1, stride_ik = m, num_its = n; 131 132 if (t_mode == CEED_NOTRANSPOSE) { 133 stride_j = n; 134 stride_ik = 1; 135 num_its = m; 136 } 137 138 // Apply rotation 139 for (CeedInt j = 0; j < num_its; j++) { 140 CeedScalar tau1 = A[i * stride_ik + j * stride_j], tau2 = A[k * stride_ik + j * stride_j]; 141 142 A[i * stride_ik + j * stride_j] = c * tau1 - s * tau2; 143 A[k * stride_ik + j * stride_j] = s * tau1 + c * tau2; 144 } 145 return CEED_ERROR_SUCCESS; 146 } 147 148 /** 149 @brief View an array stored in a `CeedBasis` 150 151 @param[in] name Name of array 152 @param[in] fp_fmt Printing format 153 @param[in] m Number of rows in array 154 @param[in] n Number of columns in array 155 @param[in] a Array to be viewed 156 @param[in] tabs Tabs to append before each new line 157 @param[in] stream Stream to view to, e.g., `stdout` 158 159 @return An error code: 0 - success, otherwise - failure 160 161 @ref Developer 162 **/ 163 static int CeedScalarView(const char *name, const char *fp_fmt, CeedInt m, CeedInt n, const CeedScalar *a, const char *tabs, FILE *stream) { 164 if (m > 1) { 165 fprintf(stream, "%s %s:\n", tabs, name); 166 } else { 167 char padded_name[12]; 168 169 snprintf(padded_name, 11, "%s:", name); 170 fprintf(stream, "%s %-10s", tabs, padded_name); 171 } 172 for (CeedInt i = 0; i < m; i++) { 173 if (m > 1) fprintf(stream, "%s [%" CeedInt_FMT "]", tabs, i); 174 for (CeedInt j = 0; j < n; j++) fprintf(stream, fp_fmt, fabs(a[i * n + j]) > 1E-14 ? a[i * n + j] : 0); 175 fputs("\n", stream); 176 } 177 return CEED_ERROR_SUCCESS; 178 } 179 180 /** 181 @brief Create the interpolation and gradient matrices for projection from the nodes of `basis_from` to the nodes of `basis_to`. 182 183 The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization. 184 The gradient is given by `grad_project = interp_to^+ * grad_from`, and is only computed for \f$H^1\f$ spaces otherwise it should not be used. 185 186 Note: `basis_from` and `basis_to` must have compatible quadrature spaces. 187 188 @param[in] basis_from `CeedBasis` to project from 189 @param[in] basis_to `CeedBasis` to project to 190 @param[out] interp_project Address of the variable where the newly created interpolation matrix will be stored 191 @param[out] grad_project Address of the variable where the newly created gradient matrix will be stored 192 193 @return An error code: 0 - success, otherwise - failure 194 195 @ref Developer 196 **/ 197 static int CeedBasisCreateProjectionMatrices(CeedBasis basis_from, CeedBasis basis_to, CeedScalar **interp_project, CeedScalar **grad_project) { 198 bool are_both_tensor; 199 CeedInt Q, Q_to, Q_from, P_to, P_from; 200 201 // Check for compatible quadrature spaces 202 CeedCall(CeedBasisGetNumQuadraturePoints(basis_to, &Q_to)); 203 CeedCall(CeedBasisGetNumQuadraturePoints(basis_from, &Q_from)); 204 CeedCheck(Q_to == Q_from, CeedBasisReturnCeed(basis_to), CEED_ERROR_DIMENSION, 205 "Bases must have compatible quadrature spaces." 206 " 'basis_from' has %" CeedInt_FMT " points and 'basis_to' has %" CeedInt_FMT, 207 Q_from, Q_to); 208 Q = Q_to; 209 210 // Check for matching tensor or non-tensor 211 { 212 bool is_tensor_to, is_tensor_from; 213 214 CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to)); 215 CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from)); 216 are_both_tensor = is_tensor_to && is_tensor_from; 217 } 218 if (are_both_tensor) { 219 CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_to)); 220 CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_from)); 221 CeedCall(CeedBasisGetNumQuadraturePoints1D(basis_from, &Q)); 222 } else { 223 CeedCall(CeedBasisGetNumNodes(basis_to, &P_to)); 224 CeedCall(CeedBasisGetNumNodes(basis_from, &P_from)); 225 } 226 227 // Check for matching FE space 228 CeedFESpace fe_space_to, fe_space_from; 229 230 CeedCall(CeedBasisGetFESpace(basis_to, &fe_space_to)); 231 CeedCall(CeedBasisGetFESpace(basis_from, &fe_space_from)); 232 CeedCheck(fe_space_to == fe_space_from, CeedBasisReturnCeed(basis_to), CEED_ERROR_MINOR, 233 "Bases must both be the same FE space type." 234 " 'basis_from' is a %s and 'basis_to' is a %s", 235 CeedFESpaces[fe_space_from], CeedFESpaces[fe_space_to]); 236 237 // Get source matrices 238 CeedInt dim, q_comp = 1; 239 CeedScalar *interp_to_inv, *interp_from; 240 const CeedScalar *interp_to_source = NULL, *interp_from_source = NULL, *grad_from_source = NULL; 241 242 CeedCall(CeedBasisGetDimension(basis_from, &dim)); 243 if (are_both_tensor) { 244 CeedCall(CeedBasisGetInterp1D(basis_to, &interp_to_source)); 245 CeedCall(CeedBasisGetInterp1D(basis_from, &interp_from_source)); 246 } else { 247 CeedCall(CeedBasisGetNumQuadratureComponents(basis_from, CEED_EVAL_INTERP, &q_comp)); 248 CeedCall(CeedBasisGetInterp(basis_to, &interp_to_source)); 249 CeedCall(CeedBasisGetInterp(basis_from, &interp_from_source)); 250 } 251 CeedCall(CeedMalloc(Q * P_from * q_comp, &interp_from)); 252 CeedCall(CeedCalloc(P_to * P_from, interp_project)); 253 254 // `grad_project = interp_to^+ * grad_from` is computed for the H^1 space case so the 255 // projection basis will have a gradient operation (allocated even if not H^1 for the 256 // basis construction later on) 257 if (fe_space_to == CEED_FE_SPACE_H1) { 258 if (are_both_tensor) { 259 CeedCall(CeedBasisGetGrad1D(basis_from, &grad_from_source)); 260 } else { 261 CeedCall(CeedBasisGetGrad(basis_from, &grad_from_source)); 262 } 263 } 264 CeedCall(CeedCalloc(P_to * P_from * (are_both_tensor ? 1 : dim), grad_project)); 265 266 // Compute interp_to^+, pseudoinverse of interp_to 267 CeedCall(CeedCalloc(Q * q_comp * P_to, &interp_to_inv)); 268 CeedCall(CeedMatrixPseudoinverse(CeedBasisReturnCeed(basis_to), interp_to_source, Q * q_comp, P_to, interp_to_inv)); 269 // Build matrices 270 CeedInt num_matrices = 1 + (fe_space_to == CEED_FE_SPACE_H1) * (are_both_tensor ? 1 : dim); 271 CeedScalar *input_from[num_matrices], *output_project[num_matrices]; 272 273 input_from[0] = (CeedScalar *)interp_from_source; 274 output_project[0] = *interp_project; 275 for (CeedInt m = 1; m < num_matrices; m++) { 276 input_from[m] = (CeedScalar *)&grad_from_source[(m - 1) * Q * P_from]; 277 output_project[m] = &((*grad_project)[(m - 1) * P_to * P_from]); 278 } 279 for (CeedInt m = 0; m < num_matrices; m++) { 280 // output_project = interp_to^+ * interp_from 281 memcpy(interp_from, input_from[m], Q * P_from * q_comp * sizeof(input_from[m][0])); 282 CeedCall(CeedMatrixMatrixMultiply(CeedBasisReturnCeed(basis_to), interp_to_inv, input_from[m], output_project[m], P_to, P_from, Q * q_comp)); 283 // Round zero to machine precision 284 for (CeedInt i = 0; i < P_to * P_from; i++) { 285 if (fabs(output_project[m][i]) < 10 * CEED_EPSILON) output_project[m][i] = 0.0; 286 } 287 } 288 289 // Cleanup 290 CeedCall(CeedFree(&interp_to_inv)); 291 CeedCall(CeedFree(&interp_from)); 292 return CEED_ERROR_SUCCESS; 293 } 294 295 /** 296 @brief Check input vector dimensions for CeedBasisApply[Add] 297 298 @param[in] basis `CeedBasis` to evaluate 299 @param[in] num_elem The number of elements to apply the basis evaluation to; 300 the backend will specify the ordering in @ref CeedElemRestrictionCreate() 301 @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points; 302 @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes 303 @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 304 @ref CEED_EVAL_INTERP to use interpolated values, 305 @ref CEED_EVAL_GRAD to use gradients, 306 @ref CEED_EVAL_DIV to use divergence, 307 @ref CEED_EVAL_CURL to use curl, 308 @ref CEED_EVAL_WEIGHT to use quadrature weights 309 @param[in] u Input `CeedVector` 310 @param[out] v Output `CeedVector` 311 312 @return An error code: 0 - success, otherwise - failure 313 314 @ref Developer 315 **/ 316 static int CeedBasisApplyCheckDims(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 317 CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 318 CeedSize u_length = 0, v_length; 319 320 CeedCall(CeedBasisGetDimension(basis, &dim)); 321 CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 322 CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 323 CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 324 CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 325 CeedCall(CeedVectorGetLength(v, &v_length)); 326 if (u) CeedCall(CeedVectorGetLength(u, &u_length)); 327 328 // Check vector lengths to prevent out of bounds issues 329 bool has_good_dims = true; 330 switch (eval_mode) { 331 case CEED_EVAL_NONE: 332 case CEED_EVAL_INTERP: 333 case CEED_EVAL_GRAD: 334 case CEED_EVAL_DIV: 335 case CEED_EVAL_CURL: 336 has_good_dims = ((t_mode == CEED_TRANSPOSE && u_length >= (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_qpts * (CeedSize)q_comp && 337 v_length >= (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_nodes) || 338 (t_mode == CEED_NOTRANSPOSE && v_length >= (CeedSize)num_elem * (CeedSize)num_qpts * (CeedSize)num_comp * (CeedSize)q_comp && 339 u_length >= (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_nodes)); 340 break; 341 case CEED_EVAL_WEIGHT: 342 has_good_dims = v_length >= (CeedSize)num_elem * (CeedSize)num_qpts; 343 break; 344 } 345 CeedCheck(has_good_dims, CeedBasisReturnCeed(basis), CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 346 return CEED_ERROR_SUCCESS; 347 } 348 349 /** 350 @brief Check input vector dimensions for CeedBasisApply[Add]AtPoints 351 352 @param[in] basis `CeedBasis` to evaluate 353 @param[in] num_elem The number of elements to apply the basis evaluation to; 354 the backend will specify the ordering in @ref CeedElemRestrictionCreate() 355 @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 356 @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 357 @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes 358 @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 359 @ref CEED_EVAL_GRAD to use gradients, 360 @ref CEED_EVAL_WEIGHT to use quadrature weights 361 @param[in] x_ref `CeedVector` holding reference coordinates of each point 362 @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 363 @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 364 365 @return An error code: 0 - success, otherwise - failure 366 367 @ref Developer 368 **/ 369 static int CeedBasisApplyAtPointsCheckDims(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, 370 CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) { 371 CeedInt dim, num_comp, num_q_comp, num_nodes, P_1d = 1, Q_1d = 1, total_num_points = 0; 372 CeedSize x_length = 0, u_length = 0, v_length; 373 374 CeedCall(CeedBasisGetDimension(basis, &dim)); 375 CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 376 CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 377 CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 378 CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &num_q_comp)); 379 CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 380 CeedCall(CeedVectorGetLength(v, &v_length)); 381 if (x_ref != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(x_ref, &x_length)); 382 if (u != CEED_VECTOR_NONE) CeedCall(CeedVectorGetLength(u, &u_length)); 383 384 // Check compatibility coordinates vector 385 for (CeedInt i = 0; i < num_elem; i++) total_num_points += num_points[i]; 386 CeedCheck((x_length >= (CeedSize)total_num_points * (CeedSize)dim) || (eval_mode == CEED_EVAL_WEIGHT), CeedBasisReturnCeed(basis), 387 CEED_ERROR_DIMENSION, 388 "Length of reference coordinate vector incompatible with basis dimension and number of points." 389 " Found reference coordinate vector of length %" CeedSize_FMT ", not of length %" CeedSize_FMT ".", 390 x_length, (CeedSize)total_num_points * (CeedSize)dim); 391 392 // Check CEED_EVAL_WEIGHT only on CEED_NOTRANSPOSE 393 CeedCheck(eval_mode != CEED_EVAL_WEIGHT || t_mode == CEED_NOTRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, 394 "CEED_EVAL_WEIGHT only supported with CEED_NOTRANSPOSE"); 395 396 // Check vector lengths to prevent out of bounds issues 397 bool has_good_dims = true; 398 switch (eval_mode) { 399 case CEED_EVAL_INTERP: 400 has_good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= (CeedSize)total_num_points * (CeedSize)num_q_comp || 401 v_length >= (CeedSize)num_elem * (CeedSize)num_nodes * (CeedSize)num_comp)) || 402 (t_mode == CEED_NOTRANSPOSE && (v_length >= (CeedSize)total_num_points * (CeedSize)num_q_comp || 403 u_length >= (CeedSize)num_elem * (CeedSize)num_nodes * (CeedSize)num_comp))); 404 break; 405 case CEED_EVAL_GRAD: 406 has_good_dims = ((t_mode == CEED_TRANSPOSE && (u_length >= (CeedSize)total_num_points * (CeedSize)num_q_comp * (CeedSize)dim || 407 v_length >= (CeedSize)num_elem * (CeedSize)num_nodes * (CeedSize)num_comp)) || 408 (t_mode == CEED_NOTRANSPOSE && (v_length >= (CeedSize)total_num_points * (CeedSize)num_q_comp * (CeedSize)dim || 409 u_length >= (CeedSize)num_elem * (CeedSize)num_nodes * (CeedSize)num_comp))); 410 break; 411 case CEED_EVAL_WEIGHT: 412 has_good_dims = t_mode == CEED_NOTRANSPOSE && (v_length >= total_num_points); 413 break; 414 // LCOV_EXCL_START 415 case CEED_EVAL_NONE: 416 case CEED_EVAL_DIV: 417 case CEED_EVAL_CURL: 418 return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points not supported for %s", 419 CeedEvalModes[eval_mode]); 420 // LCOV_EXCL_STOP 421 } 422 CeedCheck(has_good_dims, CeedBasisReturnCeed(basis), CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); 423 return CEED_ERROR_SUCCESS; 424 } 425 426 /** 427 @brief Default implimentation to apply basis evaluation from nodes to arbitrary points 428 429 @param[in] basis `CeedBasis` to evaluate 430 @param[in] apply_add Sum result into target vector or overwrite 431 @param[in] num_elem The number of elements to apply the basis evaluation to; 432 the backend will specify the ordering in @ref CeedElemRestrictionCreate() 433 @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 434 @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 435 @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes 436 @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 437 @ref CEED_EVAL_GRAD to use gradients, 438 @ref CEED_EVAL_WEIGHT to use quadrature weights 439 @param[in] x_ref `CeedVector` holding reference coordinates of each point 440 @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 441 @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 442 443 @return An error code: 0 - success, otherwise - failure 444 445 @ref Developer 446 **/ 447 static int CeedBasisApplyAtPoints_Core(CeedBasis basis, bool apply_add, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, 448 CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) { 449 CeedInt dim, num_comp, P_1d = 1, Q_1d = 1, total_num_points = num_points[0]; 450 451 CeedCall(CeedBasisGetDimension(basis, &dim)); 452 // Inserting check because clang-tidy doesn't understand this cannot occur 453 CeedCheck(dim > 0, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Malformed CeedBasis, dim > 0 is required"); 454 CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 455 CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 456 CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 457 458 // Default implementation 459 { 460 bool is_tensor_basis; 461 462 CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 463 CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, 464 "Evaluation at arbitrary points only supported for tensor product bases"); 465 } 466 CeedCheck(num_elem == 1, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, 467 "Evaluation at arbitrary points only supported for a single element at a time"); 468 if (eval_mode == CEED_EVAL_WEIGHT) { 469 CeedCall(CeedVectorSetValue(v, 1.0)); 470 return CEED_ERROR_SUCCESS; 471 } 472 if (!basis->basis_chebyshev) { 473 // Build basis mapping from nodes to Chebyshev coefficients 474 CeedScalar *chebyshev_interp_1d, *chebyshev_grad_1d, *chebyshev_q_weight_1d; 475 const CeedScalar *q_ref_1d; 476 Ceed ceed; 477 478 CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 479 CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_grad_1d)); 480 CeedCall(CeedCalloc(Q_1d, &chebyshev_q_weight_1d)); 481 CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 482 CeedCall(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d)); 483 484 CeedCall(CeedBasisGetCeed(basis, &ceed)); 485 CeedCall(CeedVectorCreate(ceed, num_comp * CeedIntPow(Q_1d, dim), &basis->vec_chebyshev)); 486 CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d, Q_1d, chebyshev_interp_1d, chebyshev_grad_1d, q_ref_1d, chebyshev_q_weight_1d, 487 &basis->basis_chebyshev)); 488 489 // Cleanup 490 CeedCall(CeedFree(&chebyshev_interp_1d)); 491 CeedCall(CeedFree(&chebyshev_grad_1d)); 492 CeedCall(CeedFree(&chebyshev_q_weight_1d)); 493 CeedCall(CeedDestroy(&ceed)); 494 } 495 496 // Create TensorContract object if needed, such as a basis from the GPU backends 497 if (!basis->contract) { 498 Ceed ceed_ref; 499 CeedBasis basis_ref = NULL; 500 501 CeedCall(CeedInit("/cpu/self", &ceed_ref)); 502 // Only need matching tensor contraction dimensions, any type of basis will work 503 CeedCall(CeedBasisCreateTensorH1Lagrange(ceed_ref, dim, num_comp, P_1d, Q_1d, CEED_GAUSS, &basis_ref)); 504 // Note - clang-tidy doesn't know basis_ref->contract must be valid here 505 CeedCheck(basis_ref && basis_ref->contract, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, 506 "Reference CPU ceed failed to create a tensor contraction object"); 507 CeedCall(CeedTensorContractReferenceCopy(basis_ref->contract, &basis->contract)); 508 CeedCall(CeedBasisDestroy(&basis_ref)); 509 CeedCall(CeedDestroy(&ceed_ref)); 510 } 511 512 // Basis evaluation 513 switch (t_mode) { 514 case CEED_NOTRANSPOSE: { 515 // Nodes to arbitrary points 516 CeedScalar *v_array; 517 const CeedScalar *chebyshev_coeffs, *x_array_read; 518 519 // -- Interpolate to Chebyshev coefficients 520 CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, basis->vec_chebyshev)); 521 522 // -- Evaluate Chebyshev polynomials at arbitrary points 523 CeedCall(CeedVectorGetArrayRead(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs)); 524 CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read)); 525 CeedCall(CeedVectorGetArrayWrite(v, CEED_MEM_HOST, &v_array)); 526 switch (eval_mode) { 527 case CEED_EVAL_INTERP: { 528 CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 529 530 // ---- Values at point 531 for (CeedInt p = 0; p < total_num_points; p++) { 532 CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1; 533 534 for (CeedInt d = 0; d < dim; d++) { 535 // ------ Tensor contract with current Chebyshev polynomial values 536 CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 537 CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false, 538 d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2])); 539 pre /= Q_1d; 540 post *= 1; 541 } 542 for (CeedInt c = 0; c < num_comp; c++) v_array[c * total_num_points + p] = tmp[dim % 2][c]; 543 } 544 break; 545 } 546 case CEED_EVAL_GRAD: { 547 CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 548 549 // ---- Values at point 550 for (CeedInt p = 0; p < total_num_points; p++) { 551 // Dim**2 contractions, apply grad when pass == dim 552 for (CeedInt pass = 0; pass < dim; pass++) { 553 CeedInt pre = num_comp * CeedIntPow(Q_1d, dim - 1), post = 1; 554 555 for (CeedInt d = 0; d < dim; d++) { 556 // ------ Tensor contract with current Chebyshev polynomial values 557 if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 558 else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 559 CeedCall(CeedTensorContractApply(basis->contract, pre, Q_1d, post, 1, chebyshev_x, t_mode, false, 560 d == 0 ? chebyshev_coeffs : tmp[d % 2], tmp[(d + 1) % 2])); 561 pre /= Q_1d; 562 post *= 1; 563 } 564 for (CeedInt c = 0; c < num_comp; c++) v_array[(pass * num_comp + c) * total_num_points + p] = tmp[dim % 2][c]; 565 } 566 } 567 break; 568 } 569 default: 570 // Nothing to do, excluded above 571 break; 572 } 573 CeedCall(CeedVectorRestoreArrayRead(basis->vec_chebyshev, &chebyshev_coeffs)); 574 CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read)); 575 CeedCall(CeedVectorRestoreArray(v, &v_array)); 576 break; 577 } 578 case CEED_TRANSPOSE: { 579 // Note: No switch on e_mode here because only CEED_EVAL_INTERP is supported at this time 580 // Arbitrary points to nodes 581 CeedScalar *chebyshev_coeffs; 582 const CeedScalar *u_array, *x_array_read; 583 584 // -- Transpose of evaluation of Chebyshev polynomials at arbitrary points 585 CeedCall(CeedVectorGetArrayWrite(basis->vec_chebyshev, CEED_MEM_HOST, &chebyshev_coeffs)); 586 CeedCall(CeedVectorGetArrayRead(x_ref, CEED_MEM_HOST, &x_array_read)); 587 CeedCall(CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array)); 588 589 switch (eval_mode) { 590 case CEED_EVAL_INTERP: { 591 CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 592 593 // ---- Values at point 594 for (CeedInt p = 0; p < total_num_points; p++) { 595 CeedInt pre = num_comp * 1, post = 1; 596 597 for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[c * total_num_points + p]; 598 for (CeedInt d = 0; d < dim; d++) { 599 // ------ Tensor contract with current Chebyshev polynomial values 600 CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 601 CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, p > 0 && d == (dim - 1), tmp[d % 2], 602 d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2])); 603 pre /= 1; 604 post *= Q_1d; 605 } 606 } 607 break; 608 } 609 case CEED_EVAL_GRAD: { 610 CeedScalar tmp[2][num_comp * CeedIntPow(Q_1d, dim)], chebyshev_x[Q_1d]; 611 612 // ---- Values at point 613 for (CeedInt p = 0; p < total_num_points; p++) { 614 // Dim**2 contractions, apply grad when pass == dim 615 for (CeedInt pass = 0; pass < dim; pass++) { 616 CeedInt pre = num_comp * 1, post = 1; 617 618 for (CeedInt c = 0; c < num_comp; c++) tmp[0][c] = u_array[(pass * num_comp + c) * total_num_points + p]; 619 for (CeedInt d = 0; d < dim; d++) { 620 // ------ Tensor contract with current Chebyshev polynomial values 621 if (pass == d) CeedCall(CeedChebyshevDerivativeAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 622 else CeedCall(CeedChebyshevPolynomialsAtPoint(x_array_read[d * total_num_points + p], Q_1d, chebyshev_x)); 623 CeedCall(CeedTensorContractApply(basis->contract, pre, 1, post, Q_1d, chebyshev_x, t_mode, 624 (p > 0 || (p == 0 && pass > 0)) && d == (dim - 1), tmp[d % 2], 625 d == (dim - 1) ? chebyshev_coeffs : tmp[(d + 1) % 2])); 626 pre /= 1; 627 post *= Q_1d; 628 } 629 } 630 } 631 break; 632 } 633 default: 634 // Nothing to do, excluded above 635 break; 636 } 637 CeedCall(CeedVectorRestoreArray(basis->vec_chebyshev, &chebyshev_coeffs)); 638 CeedCall(CeedVectorRestoreArrayRead(x_ref, &x_array_read)); 639 CeedCall(CeedVectorRestoreArrayRead(u, &u_array)); 640 641 // -- Interpolate transpose from Chebyshev coefficients 642 if (apply_add) CeedCall(CeedBasisApplyAdd(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v)); 643 else CeedCall(CeedBasisApply(basis->basis_chebyshev, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, basis->vec_chebyshev, v)); 644 break; 645 } 646 } 647 return CEED_ERROR_SUCCESS; 648 } 649 650 /// @} 651 652 /// ---------------------------------------------------------------------------- 653 /// Ceed Backend API 654 /// ---------------------------------------------------------------------------- 655 /// @addtogroup CeedBasisBackend 656 /// @{ 657 658 /** 659 @brief Fallback to a reference implementation for a non tensor-product basis for \f$H^1\f$ discretizations. 660 This function may only be called inside of a backend `BasisCreateH1` function. 661 This is used by a backend when the specific parameters for a `CeedBasis` exceed the backend's support, such as 662 when a `interp` and `grad` matrices require too many bytes to fit into shared memory on a GPU. 663 664 @param[in] ceed `Ceed` object used to create the `CeedBasis` 665 @param[in] topo Topology of element, e.g. hypercube, simplex, etc 666 @param[in] num_comp Number of field components (1 for scalar fields) 667 @param[in] num_nodes Total number of nodes 668 @param[in] num_qpts Total number of quadrature points 669 @param[in] interp Row-major (`num_qpts * num_nodes`) matrix expressing the values of nodal basis functions at quadrature points 670 @param[in] grad Row-major (`dim * num_qpts * num_nodes`) matrix expressing derivatives of nodal basis functions at quadrature points 671 @param[in] q_ref Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element 672 @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 673 @param[out] basis Newly created `CeedBasis` 674 675 @return An error code: 0 - success, otherwise - failure 676 677 @ref User 678 **/ 679 int CeedBasisCreateH1Fallback(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 680 const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) { 681 CeedInt P = num_nodes, Q = num_qpts, dim = 0; 682 Ceed delegate; 683 684 CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 685 CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateH1"); 686 687 CeedCall(CeedReferenceCopy(delegate, &(basis)->ceed)); 688 CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 689 CeedCall(delegate->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, basis)); 690 CeedCall(CeedDestroy(&delegate)); 691 return CEED_ERROR_SUCCESS; 692 } 693 694 /** 695 @brief Return collocated gradient matrix 696 697 @param[in] basis `CeedBasis` 698 @param[out] collo_grad_1d Row-major (`Q_1d * Q_1d`) matrix expressing derivatives of basis functions at quadrature points 699 700 @return An error code: 0 - success, otherwise - failure 701 702 @ref Backend 703 **/ 704 int CeedBasisGetCollocatedGrad(CeedBasis basis, CeedScalar *collo_grad_1d) { 705 Ceed ceed; 706 CeedInt P_1d, Q_1d; 707 CeedScalar *interp_1d_pinv; 708 const CeedScalar *grad_1d, *interp_1d; 709 710 // Note: This function is for backend use, so all errors are terminal and we do not need to clean up memory on failure. 711 CeedCall(CeedBasisGetCeed(basis, &ceed)); 712 CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 713 CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 714 715 // Compute interp_1d^+, pseudoinverse of interp_1d 716 CeedCall(CeedCalloc(P_1d * Q_1d, &interp_1d_pinv)); 717 CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 718 CeedCall(CeedMatrixPseudoinverse(ceed, interp_1d, Q_1d, P_1d, interp_1d_pinv)); 719 CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 720 CeedCall(CeedMatrixMatrixMultiply(ceed, grad_1d, (const CeedScalar *)interp_1d_pinv, collo_grad_1d, Q_1d, Q_1d, P_1d)); 721 722 CeedCall(CeedFree(&interp_1d_pinv)); 723 CeedCall(CeedDestroy(&ceed)); 724 return CEED_ERROR_SUCCESS; 725 } 726 727 /** 728 @brief Get the number of tabs to indent for @ref CeedBasisView() output 729 730 @param[in] basis `CeedBasis` to get the number of view tabs 731 @param[out] num_tabs Number of view tabs 732 733 @return Error code: 0 - success, otherwise - failure 734 735 @ref Backend 736 **/ 737 int CeedBasisGetNumViewTabs(CeedBasis basis, CeedInt *num_tabs) { 738 *num_tabs = basis->num_tabs; 739 return CEED_ERROR_SUCCESS; 740 } 741 742 /** 743 @brief Return 1D interpolation matrix to Chebyshev polynomial coefficients on quadrature space 744 745 @param[in] basis `CeedBasis` 746 @param[out] chebyshev_interp_1d Row-major (`P_1d * Q_1d`) matrix interpolating from basis nodes to Chebyshev polynomial coefficients 747 748 @return An error code: 0 - success, otherwise - failure 749 750 @ref Backend 751 **/ 752 int CeedBasisGetChebyshevInterp1D(CeedBasis basis, CeedScalar *chebyshev_interp_1d) { 753 CeedInt P_1d, Q_1d; 754 CeedScalar *C, *chebyshev_coeffs_1d_inv; 755 const CeedScalar *interp_1d, *q_ref_1d; 756 Ceed ceed; 757 758 CeedCall(CeedBasisGetCeed(basis, &ceed)); 759 CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 760 CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 761 762 // Build coefficient matrix 763 // -- Note: Clang-tidy needs this check 764 CeedCheck((P_1d > 0) && (Q_1d > 0), ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis dimensions are malformed"); 765 CeedCall(CeedCalloc(Q_1d * Q_1d, &C)); 766 CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 767 for (CeedInt i = 0; i < Q_1d; i++) CeedCall(CeedChebyshevPolynomialsAtPoint(q_ref_1d[i], Q_1d, &C[i * Q_1d])); 768 769 // Compute C^+, pseudoinverse of coefficient matrix 770 CeedCall(CeedCalloc(Q_1d * Q_1d, &chebyshev_coeffs_1d_inv)); 771 CeedCall(CeedMatrixPseudoinverse(ceed, C, Q_1d, Q_1d, chebyshev_coeffs_1d_inv)); 772 773 // Build mapping from nodes to Chebyshev coefficients 774 CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 775 CeedCall(CeedMatrixMatrixMultiply(ceed, chebyshev_coeffs_1d_inv, interp_1d, chebyshev_interp_1d, Q_1d, P_1d, Q_1d)); 776 777 // Cleanup 778 CeedCall(CeedFree(&C)); 779 CeedCall(CeedFree(&chebyshev_coeffs_1d_inv)); 780 CeedCall(CeedDestroy(&ceed)); 781 return CEED_ERROR_SUCCESS; 782 } 783 784 /** 785 @brief Get tensor status for given `CeedBasis` 786 787 @param[in] basis `CeedBasis` 788 @param[out] is_tensor Variable to store tensor status 789 790 @return An error code: 0 - success, otherwise - failure 791 792 @ref Backend 793 **/ 794 int CeedBasisIsTensor(CeedBasis basis, bool *is_tensor) { 795 *is_tensor = basis->is_tensor_basis; 796 return CEED_ERROR_SUCCESS; 797 } 798 799 /** 800 @brief Determine if given `CeedBasis` has nodes collocated with quadrature points 801 802 @param[in] basis `CeedBasis` 803 @param[out] is_collocated Variable to store collocated status 804 805 @return An error code: 0 - success, otherwise - failure 806 807 @ref Backend 808 **/ 809 int CeedBasisIsCollocated(CeedBasis basis, bool *is_collocated) { 810 if (basis->is_tensor_basis && (basis->Q_1d == basis->P_1d)) { 811 *is_collocated = true; 812 813 for (CeedInt i = 0; i < basis->P_1d; i++) { 814 *is_collocated = *is_collocated && (fabs(basis->interp_1d[i + basis->P_1d * i] - 1.0) < 10 * CEED_EPSILON); 815 for (CeedInt j = 0; j < basis->Q_1d; j++) { 816 if (j != i) *is_collocated = *is_collocated && (fabs(basis->interp_1d[j + basis->P_1d * i]) < 10 * CEED_EPSILON); 817 } 818 } 819 } else { 820 *is_collocated = false; 821 } 822 return CEED_ERROR_SUCCESS; 823 } 824 825 /** 826 @brief Get backend data of a `CeedBasis` 827 828 @param[in] basis `CeedBasis` 829 @param[out] data Variable to store data 830 831 @return An error code: 0 - success, otherwise - failure 832 833 @ref Backend 834 **/ 835 int CeedBasisGetData(CeedBasis basis, void *data) { 836 *(void **)data = basis->data; 837 return CEED_ERROR_SUCCESS; 838 } 839 840 /** 841 @brief Set backend data of a `CeedBasis` 842 843 @param[in,out] basis `CeedBasis` 844 @param[in] data Data to set 845 846 @return An error code: 0 - success, otherwise - failure 847 848 @ref Backend 849 **/ 850 int CeedBasisSetData(CeedBasis basis, void *data) { 851 basis->data = data; 852 return CEED_ERROR_SUCCESS; 853 } 854 855 /** 856 @brief Increment the reference counter for a `CeedBasis` 857 858 @param[in,out] basis `CeedBasis` to increment the reference counter 859 860 @return An error code: 0 - success, otherwise - failure 861 862 @ref Backend 863 **/ 864 int CeedBasisReference(CeedBasis basis) { 865 basis->ref_count++; 866 return CEED_ERROR_SUCCESS; 867 } 868 869 /** 870 @brief Get number of Q-vector components for given `CeedBasis` 871 872 @param[in] basis `CeedBasis` 873 @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 874 @ref CEED_EVAL_GRAD to use gradients, 875 @ref CEED_EVAL_DIV to use divergence, 876 @ref CEED_EVAL_CURL to use curl 877 @param[out] q_comp Variable to store number of Q-vector components of basis 878 879 @return An error code: 0 - success, otherwise - failure 880 881 @ref Backend 882 **/ 883 int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode, CeedInt *q_comp) { 884 CeedInt dim; 885 886 CeedCall(CeedBasisGetDimension(basis, &dim)); 887 switch (eval_mode) { 888 case CEED_EVAL_INTERP: { 889 CeedFESpace fe_space; 890 891 CeedCall(CeedBasisGetFESpace(basis, &fe_space)); 892 *q_comp = (fe_space == CEED_FE_SPACE_H1) ? 1 : dim; 893 } break; 894 case CEED_EVAL_GRAD: 895 *q_comp = dim; 896 break; 897 case CEED_EVAL_DIV: 898 *q_comp = 1; 899 break; 900 case CEED_EVAL_CURL: 901 *q_comp = (dim < 3) ? 1 : dim; 902 break; 903 case CEED_EVAL_NONE: 904 case CEED_EVAL_WEIGHT: 905 *q_comp = 1; 906 break; 907 } 908 return CEED_ERROR_SUCCESS; 909 } 910 911 /** 912 @brief Estimate number of FLOPs required to apply `CeedBasis` in `t_mode` and `eval_mode` 913 914 @param[in] basis `CeedBasis` to estimate FLOPs for 915 @param[in] t_mode Apply basis or transpose 916 @param[in] eval_mode @ref CeedEvalMode 917 @param[in] is_at_points Evaluate the basis at points or quadrature points 918 @param[in] num_points Number of points basis is evaluated at 919 @param[out] flops Address of variable to hold FLOPs estimate 920 921 @ref Backend 922 **/ 923 int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, bool is_at_points, CeedInt num_points, 924 CeedSize *flops) { 925 bool is_tensor; 926 927 CeedCall(CeedBasisIsTensor(basis, &is_tensor)); 928 CeedCheck(!is_at_points || is_tensor, CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Can only evaluate tensor-product bases at points"); 929 if (is_tensor) { 930 CeedInt dim, num_comp, P_1d, Q_1d; 931 932 CeedCall(CeedBasisGetDimension(basis, &dim)); 933 CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 934 CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 935 CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 936 if (t_mode == CEED_TRANSPOSE) { 937 P_1d = Q_1d; 938 Q_1d = P_1d; 939 } 940 CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim - 1), post = 1; 941 942 for (CeedInt d = 0; d < dim; d++) { 943 tensor_flops += 2 * pre * P_1d * post * Q_1d; 944 pre /= P_1d; 945 post *= Q_1d; 946 } 947 if (is_at_points) { 948 bool is_gpu = false; 949 950 { 951 CeedMemType mem_type; 952 953 CeedCall(CeedGetPreferredMemType(CeedBasisReturnCeed(basis), &mem_type)); 954 is_gpu = mem_type == CEED_MEM_DEVICE; 955 } 956 957 CeedInt chebyshev_flops = (Q_1d - 2) * 3 + 1, d_chebyshev_flops = (Q_1d - 2) * 8 + 1; 958 CeedInt point_tensor_flops = 0, pre = CeedIntPow(Q_1d, dim - 1), post = 1; 959 960 for (CeedInt d = 0; d < dim; d++) { 961 point_tensor_flops += 2 * pre * Q_1d * post * 1; 962 pre /= P_1d; 963 post *= Q_1d; 964 } 965 966 switch (eval_mode) { 967 case CEED_EVAL_NONE: 968 *flops = 0; 969 break; 970 case CEED_EVAL_INTERP: { 971 *flops = tensor_flops + num_points * num_comp * (point_tensor_flops + (t_mode == CEED_TRANSPOSE ? CeedIntPow(Q_1d, dim) : 0)); 972 if (dim == 3 && is_gpu) { 973 *flops += num_points * Q_1d * 974 (chebyshev_flops + num_comp * (2 * chebyshev_flops + 2 * Q_1d * Q_1d + (t_mode == CEED_TRANSPOSE ? 2 * Q_1d + 1 : 3 * Q_1d))); 975 } else { 976 *flops += num_points * (is_gpu ? num_comp : 1) * dim * chebyshev_flops; 977 } 978 break; 979 } 980 case CEED_EVAL_GRAD: { 981 *flops = tensor_flops + num_points * num_comp * (point_tensor_flops + (t_mode == CEED_TRANSPOSE ? CeedIntPow(Q_1d, dim) : 0)); 982 if (dim == 3 && is_gpu) { 983 CeedInt inner_flops = 984 dim * (2 * Q_1d * Q_1d + (t_mode == CEED_TRANSPOSE ? 2 : 3) * Q_1d) + (dim - 1) * (2 * chebyshev_flops + d_chebyshev_flops); 985 986 *flops += num_points * Q_1d * (chebyshev_flops + d_chebyshev_flops + num_comp * (inner_flops + (t_mode == CEED_TRANSPOSE ? 1 : 0))); 987 } else { 988 *flops += num_points * (is_gpu ? num_comp : 1) * dim * (d_chebyshev_flops + (dim - 1) * chebyshev_flops); 989 } 990 break; 991 } 992 case CEED_EVAL_DIV: 993 case CEED_EVAL_CURL: { 994 // LCOV_EXCL_START 995 return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported at points", 996 CeedEvalModes[eval_mode]); 997 break; 998 // LCOV_EXCL_STOP 999 } 1000 case CEED_EVAL_WEIGHT: 1001 *flops = num_points; 1002 break; 1003 } 1004 } else { 1005 switch (eval_mode) { 1006 case CEED_EVAL_NONE: 1007 *flops = 0; 1008 break; 1009 case CEED_EVAL_INTERP: 1010 *flops = tensor_flops; 1011 break; 1012 case CEED_EVAL_GRAD: 1013 *flops = tensor_flops * 2; 1014 break; 1015 case CEED_EVAL_DIV: 1016 case CEED_EVAL_CURL: { 1017 // LCOV_EXCL_START 1018 return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported", 1019 CeedEvalModes[eval_mode]); 1020 break; 1021 // LCOV_EXCL_STOP 1022 } 1023 case CEED_EVAL_WEIGHT: 1024 *flops = dim * CeedIntPow(Q_1d, dim); 1025 break; 1026 } 1027 } 1028 } else { 1029 CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; 1030 1031 CeedCall(CeedBasisGetDimension(basis, &dim)); 1032 CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 1033 CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 1034 CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 1035 CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 1036 switch (eval_mode) { 1037 case CEED_EVAL_NONE: 1038 *flops = 0; 1039 break; 1040 case CEED_EVAL_INTERP: 1041 case CEED_EVAL_GRAD: 1042 case CEED_EVAL_DIV: 1043 case CEED_EVAL_CURL: 1044 *flops = num_nodes * num_qpts * num_comp * q_comp; 1045 break; 1046 case CEED_EVAL_WEIGHT: 1047 *flops = 0; 1048 break; 1049 } 1050 } 1051 return CEED_ERROR_SUCCESS; 1052 } 1053 1054 /** 1055 @brief Get `CeedFESpace` for a `CeedBasis` 1056 1057 @param[in] basis `CeedBasis` 1058 @param[out] fe_space Variable to store `CeedFESpace` 1059 1060 @return An error code: 0 - success, otherwise - failure 1061 1062 @ref Backend 1063 **/ 1064 int CeedBasisGetFESpace(CeedBasis basis, CeedFESpace *fe_space) { 1065 *fe_space = basis->fe_space; 1066 return CEED_ERROR_SUCCESS; 1067 } 1068 1069 /** 1070 @brief Get dimension for given `CeedElemTopology` 1071 1072 @param[in] topo `CeedElemTopology` 1073 @param[out] dim Variable to store dimension of topology 1074 1075 @return An error code: 0 - success, otherwise - failure 1076 1077 @ref Backend 1078 **/ 1079 int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim) { 1080 *dim = (CeedInt)topo >> 16; 1081 return CEED_ERROR_SUCCESS; 1082 } 1083 1084 /** 1085 @brief Get `CeedTensorContract` of a `CeedBasis` 1086 1087 @param[in] basis `CeedBasis` 1088 @param[out] contract Variable to store `CeedTensorContract` 1089 1090 @return An error code: 0 - success, otherwise - failure 1091 1092 @ref Backend 1093 **/ 1094 int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract) { 1095 *contract = basis->contract; 1096 return CEED_ERROR_SUCCESS; 1097 } 1098 1099 /** 1100 @brief Set `CeedTensorContract` of a `CeedBasis` 1101 1102 @param[in,out] basis `CeedBasis` 1103 @param[in] contract `CeedTensorContract` to set 1104 1105 @return An error code: 0 - success, otherwise - failure 1106 1107 @ref Backend 1108 **/ 1109 int CeedBasisSetTensorContract(CeedBasis basis, CeedTensorContract contract) { 1110 basis->contract = contract; 1111 CeedCall(CeedTensorContractReference(contract)); 1112 return CEED_ERROR_SUCCESS; 1113 } 1114 1115 /** 1116 @brief Return a reference implementation of matrix multiplication \f$C = A B\f$. 1117 1118 Note: This is a reference implementation for CPU `CeedScalar` pointers that is not intended for high performance. 1119 1120 @param[in] ceed `Ceed` context for error handling 1121 @param[in] mat_A Row-major matrix `A` 1122 @param[in] mat_B Row-major matrix `B` 1123 @param[out] mat_C Row-major output matrix `C` 1124 @param[in] m Number of rows of `C` 1125 @param[in] n Number of columns of `C` 1126 @param[in] kk Number of columns of `A`/rows of `B` 1127 1128 @return An error code: 0 - success, otherwise - failure 1129 1130 @ref Utility 1131 **/ 1132 int CeedMatrixMatrixMultiply(Ceed ceed, const CeedScalar *mat_A, const CeedScalar *mat_B, CeedScalar *mat_C, CeedInt m, CeedInt n, CeedInt kk) { 1133 for (CeedInt i = 0; i < m; i++) { 1134 for (CeedInt j = 0; j < n; j++) { 1135 CeedScalar sum = 0; 1136 1137 for (CeedInt k = 0; k < kk; k++) sum += mat_A[k + i * kk] * mat_B[j + k * n]; 1138 mat_C[j + i * n] = sum; 1139 } 1140 } 1141 return CEED_ERROR_SUCCESS; 1142 } 1143 1144 /** 1145 @brief Return QR Factorization of a matrix 1146 1147 @param[in] ceed `Ceed` context for error handling 1148 @param[in,out] mat Row-major matrix to be factorized in place 1149 @param[in,out] tau Vector of length `m` of scaling factors 1150 @param[in] m Number of rows 1151 @param[in] n Number of columns 1152 1153 @return An error code: 0 - success, otherwise - failure 1154 1155 @ref Utility 1156 **/ 1157 int CeedQRFactorization(Ceed ceed, CeedScalar *mat, CeedScalar *tau, CeedInt m, CeedInt n) { 1158 CeedScalar v[m]; 1159 1160 // Check matrix shape 1161 CeedCheck(n <= m, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute QR factorization with n > m"); 1162 1163 for (CeedInt i = 0; i < n; i++) { 1164 CeedScalar sigma = 0.0; 1165 1166 if (i >= m - 1) { // last row of matrix, no reflection needed 1167 tau[i] = 0.; 1168 break; 1169 } 1170 // Calculate Householder vector, magnitude 1171 v[i] = mat[i + n * i]; 1172 for (CeedInt j = i + 1; j < m; j++) { 1173 v[j] = mat[i + n * j]; 1174 sigma += v[j] * v[j]; 1175 } 1176 const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:m] 1177 const CeedScalar R_ii = -copysign(norm, v[i]); 1178 1179 v[i] -= R_ii; 1180 // norm of v[i:m] after modification above and scaling below 1181 // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 1182 // tau = 2 / (norm*norm) 1183 tau[i] = 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 1184 for (CeedInt j = i + 1; j < m; j++) v[j] /= v[i]; 1185 1186 // Apply Householder reflector to lower right panel 1187 CeedHouseholderReflect(&mat[i * n + i + 1], &v[i], tau[i], m - i, n - i - 1, n, 1); 1188 // Save v 1189 mat[i + n * i] = R_ii; 1190 for (CeedInt j = i + 1; j < m; j++) mat[i + n * j] = v[j]; 1191 } 1192 return CEED_ERROR_SUCCESS; 1193 } 1194 1195 /** 1196 @brief Apply Householder Q matrix 1197 1198 Compute `mat_A = mat_Q mat_A`, where `mat_Q` is \f$m \times m\f$ and `mat_A` is \f$m \times n\f$. 1199 1200 @param[in,out] mat_A Matrix to apply Householder Q to, in place 1201 @param[in] mat_Q Householder Q matrix 1202 @param[in] tau Householder scaling factors 1203 @param[in] t_mode Transpose mode for application 1204 @param[in] m Number of rows in `A` 1205 @param[in] n Number of columns in `A` 1206 @param[in] k Number of elementary reflectors in Q, `k < m` 1207 @param[in] row Row stride in `A` 1208 @param[in] col Col stride in `A` 1209 1210 @return An error code: 0 - success, otherwise - failure 1211 1212 @ref Utility 1213 **/ 1214 int CeedHouseholderApplyQ(CeedScalar *mat_A, const CeedScalar *mat_Q, const CeedScalar *tau, CeedTransposeMode t_mode, CeedInt m, CeedInt n, 1215 CeedInt k, CeedInt row, CeedInt col) { 1216 CeedScalar *v; 1217 1218 CeedCall(CeedMalloc(m, &v)); 1219 for (CeedInt ii = 0; ii < k; ii++) { 1220 CeedInt i = t_mode == CEED_TRANSPOSE ? ii : k - 1 - ii; 1221 for (CeedInt j = i + 1; j < m; j++) v[j] = mat_Q[j * k + i]; 1222 // Apply Householder reflector (I - tau v v^T) collo_grad_1d^T 1223 CeedCall(CeedHouseholderReflect(&mat_A[i * row], &v[i], tau[i], m - i, n, row, col)); 1224 } 1225 CeedCall(CeedFree(&v)); 1226 return CEED_ERROR_SUCCESS; 1227 } 1228 1229 /** 1230 @brief Return pseudoinverse of a matrix 1231 1232 @param[in] ceed Ceed context for error handling 1233 @param[in] mat Row-major matrix to compute pseudoinverse of 1234 @param[in] m Number of rows 1235 @param[in] n Number of columns 1236 @param[out] mat_pinv Row-major pseudoinverse matrix 1237 1238 @return An error code: 0 - success, otherwise - failure 1239 1240 @ref Utility 1241 **/ 1242 int CeedMatrixPseudoinverse(Ceed ceed, const CeedScalar *mat, CeedInt m, CeedInt n, CeedScalar *mat_pinv) { 1243 CeedScalar *tau, *I, *mat_copy; 1244 1245 CeedCall(CeedCalloc(m, &tau)); 1246 CeedCall(CeedCalloc(m * m, &I)); 1247 CeedCall(CeedCalloc(m * n, &mat_copy)); 1248 memcpy(mat_copy, mat, m * n * sizeof mat[0]); 1249 1250 // QR Factorization, mat = Q R 1251 CeedCall(CeedQRFactorization(ceed, mat_copy, tau, m, n)); 1252 1253 // -- Apply Q^T, I = Q^T * I 1254 for (CeedInt i = 0; i < m; i++) I[i * m + i] = 1.0; 1255 CeedCall(CeedHouseholderApplyQ(I, mat_copy, tau, CEED_TRANSPOSE, m, m, n, m, 1)); 1256 // -- Apply R_inv, mat_pinv = R_inv * Q^T 1257 for (CeedInt j = 0; j < m; j++) { // Column j 1258 mat_pinv[j + m * (n - 1)] = I[j + m * (n - 1)] / mat_copy[n * n - 1]; 1259 for (CeedInt i = n - 2; i >= 0; i--) { // Row i 1260 mat_pinv[j + m * i] = I[j + m * i]; 1261 for (CeedInt k = i + 1; k < n; k++) mat_pinv[j + m * i] -= mat_copy[k + n * i] * mat_pinv[j + m * k]; 1262 mat_pinv[j + m * i] /= mat_copy[i + n * i]; 1263 } 1264 } 1265 1266 // Cleanup 1267 CeedCall(CeedFree(&I)); 1268 CeedCall(CeedFree(&tau)); 1269 CeedCall(CeedFree(&mat_copy)); 1270 return CEED_ERROR_SUCCESS; 1271 } 1272 1273 /** 1274 @brief Return symmetric Schur decomposition of the symmetric matrix mat via symmetric QR factorization 1275 1276 @param[in] ceed `Ceed` context for error handling 1277 @param[in,out] mat Row-major matrix to be factorized in place 1278 @param[out] lambda Vector of length n of eigenvalues 1279 @param[in] n Number of rows/columns 1280 1281 @return An error code: 0 - success, otherwise - failure 1282 1283 @ref Utility 1284 **/ 1285 CeedPragmaOptimizeOff 1286 int CeedSymmetricSchurDecomposition(Ceed ceed, CeedScalar *mat, CeedScalar *lambda, CeedInt n) { 1287 // Check bounds for clang-tidy 1288 CeedCheck(n > 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot compute symmetric Schur decomposition of scalars"); 1289 1290 CeedScalar v[n - 1], tau[n - 1], mat_T[n * n]; 1291 1292 // Copy mat to mat_T and set mat to I 1293 memcpy(mat_T, mat, n * n * sizeof(mat[0])); 1294 for (CeedInt i = 0; i < n; i++) { 1295 for (CeedInt j = 0; j < n; j++) mat[j + n * i] = (i == j) ? 1 : 0; 1296 } 1297 1298 // Reduce to tridiagonal 1299 for (CeedInt i = 0; i < n - 1; i++) { 1300 // Calculate Householder vector, magnitude 1301 CeedScalar sigma = 0.0; 1302 1303 v[i] = mat_T[i + n * (i + 1)]; 1304 for (CeedInt j = i + 1; j < n - 1; j++) { 1305 v[j] = mat_T[i + n * (j + 1)]; 1306 sigma += v[j] * v[j]; 1307 } 1308 const CeedScalar norm = sqrt(v[i] * v[i] + sigma); // norm of v[i:n-1] 1309 const CeedScalar R_ii = -copysign(norm, v[i]); 1310 1311 v[i] -= R_ii; 1312 // norm of v[i:m] after modification above and scaling below 1313 // norm = sqrt(v[i]*v[i] + sigma) / v[i]; 1314 // tau = 2 / (norm*norm) 1315 tau[i] = i == n - 2 ? 2 : 2 * v[i] * v[i] / (v[i] * v[i] + sigma); 1316 for (CeedInt j = i + 1; j < n - 1; j++) v[j] /= v[i]; 1317 1318 // Update sub and super diagonal 1319 for (CeedInt j = i + 2; j < n; j++) { 1320 mat_T[i + n * j] = 0; 1321 mat_T[j + n * i] = 0; 1322 } 1323 // Apply symmetric Householder reflector to lower right panel 1324 CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 1325 CeedHouseholderReflect(&mat_T[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), 1, n); 1326 1327 // Save v 1328 mat_T[i + n * (i + 1)] = R_ii; 1329 mat_T[(i + 1) + n * i] = R_ii; 1330 for (CeedInt j = i + 1; j < n - 1; j++) { 1331 mat_T[i + n * (j + 1)] = v[j]; 1332 } 1333 } 1334 // Backwards accumulation of Q 1335 for (CeedInt i = n - 2; i >= 0; i--) { 1336 if (tau[i] > 0.0) { 1337 v[i] = 1; 1338 for (CeedInt j = i + 1; j < n - 1; j++) { 1339 v[j] = mat_T[i + n * (j + 1)]; 1340 mat_T[i + n * (j + 1)] = 0; 1341 } 1342 CeedHouseholderReflect(&mat[(i + 1) + n * (i + 1)], &v[i], tau[i], n - (i + 1), n - (i + 1), n, 1); 1343 } 1344 } 1345 1346 // Reduce sub and super diagonal 1347 CeedInt p = 0, q = 0, itr = 0, max_itr = n * n * n * n; 1348 CeedScalar tol = CEED_EPSILON; 1349 1350 while (itr < max_itr) { 1351 // Update p, q, size of reduced portions of diagonal 1352 p = 0; 1353 q = 0; 1354 for (CeedInt i = n - 2; i >= 0; i--) { 1355 if (fabs(mat_T[i + n * (i + 1)]) < tol) q += 1; 1356 else break; 1357 } 1358 for (CeedInt i = 0; i < n - q - 1; i++) { 1359 if (fabs(mat_T[i + n * (i + 1)]) < tol) p += 1; 1360 else break; 1361 } 1362 if (q == n - 1) break; // Finished reducing 1363 1364 // Reduce tridiagonal portion 1365 CeedScalar t_nn = mat_T[(n - 1 - q) + n * (n - 1 - q)], t_nnm1 = mat_T[(n - 2 - q) + n * (n - 1 - q)]; 1366 CeedScalar d = (mat_T[(n - 2 - q) + n * (n - 2 - q)] - t_nn) / 2; 1367 CeedScalar mu = t_nn - t_nnm1 * t_nnm1 / (d + copysign(sqrt(d * d + t_nnm1 * t_nnm1), d)); 1368 CeedScalar x = mat_T[p + n * p] - mu; 1369 CeedScalar z = mat_T[p + n * (p + 1)]; 1370 1371 for (CeedInt k = p; k < n - q - 1; k++) { 1372 // Compute Givens rotation 1373 CeedScalar c = 1, s = 0; 1374 1375 if (fabs(z) > tol) { 1376 if (fabs(z) > fabs(x)) { 1377 const CeedScalar tau = -x / z; 1378 1379 s = 1 / sqrt(1 + tau * tau); 1380 c = s * tau; 1381 } else { 1382 const CeedScalar tau = -z / x; 1383 1384 c = 1 / sqrt(1 + tau * tau); 1385 s = c * tau; 1386 } 1387 } 1388 1389 // Apply Givens rotation to T 1390 CeedGivensRotation(mat_T, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 1391 CeedGivensRotation(mat_T, c, s, CEED_TRANSPOSE, k, k + 1, n, n); 1392 1393 // Apply Givens rotation to Q 1394 CeedGivensRotation(mat, c, s, CEED_NOTRANSPOSE, k, k + 1, n, n); 1395 1396 // Update x, z 1397 if (k < n - q - 2) { 1398 x = mat_T[k + n * (k + 1)]; 1399 z = mat_T[k + n * (k + 2)]; 1400 } 1401 } 1402 itr++; 1403 } 1404 1405 // Save eigenvalues 1406 for (CeedInt i = 0; i < n; i++) lambda[i] = mat_T[i + n * i]; 1407 1408 // Check convergence 1409 CeedCheck(itr < max_itr || q > n, ceed, CEED_ERROR_MINOR, "Symmetric QR failed to converge"); 1410 return CEED_ERROR_SUCCESS; 1411 } 1412 CeedPragmaOptimizeOn 1413 1414 /** 1415 @brief Return Simultaneous Diagonalization of two matrices. 1416 1417 This solves the generalized eigenvalue problem `A x = lambda B x`, where `A` and `B` are symmetric and `B` is positive definite. 1418 We generate the matrix `X` and vector `Lambda` such that `X^T A X = Lambda` and `X^T B X = I`. 1419 This is equivalent to the LAPACK routine 'sygv' with `TYPE = 1`. 1420 1421 @param[in] ceed `Ceed` context for error handling 1422 @param[in] mat_A Row-major matrix to be factorized with eigenvalues 1423 @param[in] mat_B Row-major matrix to be factorized to identity 1424 @param[out] mat_X Row-major orthogonal matrix 1425 @param[out] lambda Vector of length `n` of generalized eigenvalues 1426 @param[in] n Number of rows/columns 1427 1428 @return An error code: 0 - success, otherwise - failure 1429 1430 @ref Utility 1431 **/ 1432 CeedPragmaOptimizeOff 1433 int CeedSimultaneousDiagonalization(Ceed ceed, CeedScalar *mat_A, CeedScalar *mat_B, CeedScalar *mat_X, CeedScalar *lambda, CeedInt n) { 1434 CeedScalar *mat_C, *mat_G, *vec_D; 1435 1436 CeedCall(CeedCalloc(n * n, &mat_C)); 1437 CeedCall(CeedCalloc(n * n, &mat_G)); 1438 CeedCall(CeedCalloc(n, &vec_D)); 1439 1440 // Compute B = G D G^T 1441 memcpy(mat_G, mat_B, n * n * sizeof(mat_B[0])); 1442 CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_G, vec_D, n)); 1443 1444 // Sort eigenvalues 1445 for (CeedInt i = n - 1; i >= 0; i--) { 1446 for (CeedInt j = 0; j < i; j++) { 1447 if (fabs(vec_D[j]) > fabs(vec_D[j + 1])) { 1448 CeedScalarSwap(vec_D[j], vec_D[j + 1]); 1449 for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_G[k * n + j], mat_G[k * n + j + 1]); 1450 } 1451 } 1452 } 1453 1454 // Compute C = (G D^1/2)^-1 A (G D^1/2)^-T 1455 // = D^-1/2 G^T A G D^-1/2 1456 // -- D = D^-1/2 1457 for (CeedInt i = 0; i < n; i++) vec_D[i] = 1. / sqrt(vec_D[i]); 1458 // -- G = G D^-1/2 1459 // -- C = D^-1/2 G^T 1460 for (CeedInt i = 0; i < n; i++) { 1461 for (CeedInt j = 0; j < n; j++) { 1462 mat_G[i * n + j] *= vec_D[j]; 1463 mat_C[j * n + i] = mat_G[i * n + j]; 1464 } 1465 } 1466 // -- X = (D^-1/2 G^T) A 1467 CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_C, (const CeedScalar *)mat_A, mat_X, n, n, n)); 1468 // -- C = (D^-1/2 G^T A) (G D^-1/2) 1469 CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_X, (const CeedScalar *)mat_G, mat_C, n, n, n)); 1470 1471 // Compute Q^T C Q = lambda 1472 CeedCall(CeedSymmetricSchurDecomposition(ceed, mat_C, lambda, n)); 1473 1474 // Sort eigenvalues 1475 for (CeedInt i = n - 1; i >= 0; i--) { 1476 for (CeedInt j = 0; j < i; j++) { 1477 if (fabs(lambda[j]) > fabs(lambda[j + 1])) { 1478 CeedScalarSwap(lambda[j], lambda[j + 1]); 1479 for (CeedInt k = 0; k < n; k++) CeedScalarSwap(mat_C[k * n + j], mat_C[k * n + j + 1]); 1480 } 1481 } 1482 } 1483 1484 // Set X = (G D^1/2)^-T Q 1485 // = G D^-1/2 Q 1486 CeedCall(CeedMatrixMatrixMultiply(ceed, (const CeedScalar *)mat_G, (const CeedScalar *)mat_C, mat_X, n, n, n)); 1487 1488 // Cleanup 1489 CeedCall(CeedFree(&mat_C)); 1490 CeedCall(CeedFree(&mat_G)); 1491 CeedCall(CeedFree(&vec_D)); 1492 return CEED_ERROR_SUCCESS; 1493 } 1494 CeedPragmaOptimizeOn 1495 1496 /// @} 1497 1498 /// ---------------------------------------------------------------------------- 1499 /// CeedBasis Public API 1500 /// ---------------------------------------------------------------------------- 1501 /// @addtogroup CeedBasisUser 1502 /// @{ 1503 1504 /** 1505 @brief Create a tensor-product basis for \f$H^1\f$ discretizations 1506 1507 @param[in] ceed `Ceed` object used to create the `CeedBasis` 1508 @param[in] dim Topological dimension 1509 @param[in] num_comp Number of field components (1 for scalar fields) 1510 @param[in] P_1d Number of nodes in one dimension 1511 @param[in] Q_1d Number of quadrature points in one dimension 1512 @param[in] interp_1d Row-major (`Q_1d * P_1d`) matrix expressing the values of nodal basis functions at quadrature points 1513 @param[in] grad_1d Row-major (`Q_1d * P_1d`) matrix expressing derivatives of nodal basis functions at quadrature points 1514 @param[in] q_ref_1d Array of length `Q_1d` holding the locations of quadrature points on the 1D reference element `[-1, 1]` 1515 @param[in] q_weight_1d Array of length `Q_1d` holding the quadrature weights on the reference element 1516 @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1517 1518 @return An error code: 0 - success, otherwise - failure 1519 1520 @ref User 1521 **/ 1522 int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, 1523 const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis *basis) { 1524 if (!ceed->BasisCreateTensorH1) { 1525 Ceed delegate; 1526 1527 CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 1528 CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateTensorH1"); 1529 CeedCall(CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 1530 CeedCall(CeedDestroy(&delegate)); 1531 return CEED_ERROR_SUCCESS; 1532 } 1533 1534 CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value"); 1535 CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1536 CeedCheck(P_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1537 CeedCheck(Q_1d > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1538 1539 CeedElemTopology topo = dim == 1 ? CEED_TOPOLOGY_LINE : dim == 2 ? CEED_TOPOLOGY_QUAD : CEED_TOPOLOGY_HEX; 1540 1541 CeedCall(CeedCalloc(1, basis)); 1542 CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1543 (*basis)->ref_count = 1; 1544 (*basis)->is_tensor_basis = true; 1545 (*basis)->dim = dim; 1546 (*basis)->topo = topo; 1547 (*basis)->num_comp = num_comp; 1548 (*basis)->P_1d = P_1d; 1549 (*basis)->Q_1d = Q_1d; 1550 (*basis)->P = CeedIntPow(P_1d, dim); 1551 (*basis)->Q = CeedIntPow(Q_1d, dim); 1552 (*basis)->fe_space = CEED_FE_SPACE_H1; 1553 CeedCall(CeedCalloc(Q_1d, &(*basis)->q_ref_1d)); 1554 CeedCall(CeedCalloc(Q_1d, &(*basis)->q_weight_1d)); 1555 if (q_ref_1d) memcpy((*basis)->q_ref_1d, q_ref_1d, Q_1d * sizeof(q_ref_1d[0])); 1556 if (q_weight_1d) memcpy((*basis)->q_weight_1d, q_weight_1d, Q_1d * sizeof(q_weight_1d[0])); 1557 CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->interp_1d)); 1558 CeedCall(CeedCalloc(Q_1d * P_1d, &(*basis)->grad_1d)); 1559 if (interp_1d) memcpy((*basis)->interp_1d, interp_1d, Q_1d * P_1d * sizeof(interp_1d[0])); 1560 if (grad_1d) memcpy((*basis)->grad_1d, grad_1d, Q_1d * P_1d * sizeof(grad_1d[0])); 1561 CeedCall(ceed->BasisCreateTensorH1(dim, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, *basis)); 1562 return CEED_ERROR_SUCCESS; 1563 } 1564 1565 /** 1566 @brief Create a tensor-product \f$H^1\f$ Lagrange basis 1567 1568 @param[in] ceed `Ceed` object used to create the `CeedBasis` 1569 @param[in] dim Topological dimension of element 1570 @param[in] num_comp Number of field components (1 for scalar fields) 1571 @param[in] P Number of Gauss-Lobatto nodes in one dimension. 1572 The polynomial degree of the resulting `Q_k` element is `k = P - 1`. 1573 @param[in] Q Number of quadrature points in one dimension. 1574 @param[in] quad_mode Distribution of the `Q` quadrature points (affects order of accuracy for the quadrature) 1575 @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1576 1577 @return An error code: 0 - success, otherwise - failure 1578 1579 @ref User 1580 **/ 1581 int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P, CeedInt Q, CeedQuadMode quad_mode, CeedBasis *basis) { 1582 // Allocate 1583 int ierr = CEED_ERROR_SUCCESS; 1584 CeedScalar c1, c2, c3, c4, dx, *nodes, *interp_1d, *grad_1d, *q_ref_1d, *q_weight_1d; 1585 1586 CeedCheck(dim > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis dimension must be a positive value"); 1587 CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1588 CeedCheck(P > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1589 CeedCheck(Q > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1590 1591 // Get Nodes and Weights 1592 CeedCall(CeedCalloc(P * Q, &interp_1d)); 1593 CeedCall(CeedCalloc(P * Q, &grad_1d)); 1594 CeedCall(CeedCalloc(P, &nodes)); 1595 CeedCall(CeedCalloc(Q, &q_ref_1d)); 1596 CeedCall(CeedCalloc(Q, &q_weight_1d)); 1597 if (CeedLobattoQuadrature(P, nodes, NULL) != CEED_ERROR_SUCCESS) goto cleanup; 1598 switch (quad_mode) { 1599 case CEED_GAUSS: 1600 ierr = CeedGaussQuadrature(Q, q_ref_1d, q_weight_1d); 1601 break; 1602 case CEED_GAUSS_LOBATTO: 1603 ierr = CeedLobattoQuadrature(Q, q_ref_1d, q_weight_1d); 1604 break; 1605 } 1606 if (ierr != CEED_ERROR_SUCCESS) goto cleanup; 1607 1608 // Build B, D matrix 1609 // Fornberg, 1998 1610 for (CeedInt i = 0; i < Q; i++) { 1611 c1 = 1.0; 1612 c3 = nodes[0] - q_ref_1d[i]; 1613 interp_1d[i * P + 0] = 1.0; 1614 for (CeedInt j = 1; j < P; j++) { 1615 c2 = 1.0; 1616 c4 = c3; 1617 c3 = nodes[j] - q_ref_1d[i]; 1618 for (CeedInt k = 0; k < j; k++) { 1619 dx = nodes[j] - nodes[k]; 1620 c2 *= dx; 1621 if (k == j - 1) { 1622 grad_1d[i * P + j] = c1 * (interp_1d[i * P + k] - c4 * grad_1d[i * P + k]) / c2; 1623 interp_1d[i * P + j] = -c1 * c4 * interp_1d[i * P + k] / c2; 1624 } 1625 grad_1d[i * P + k] = (c3 * grad_1d[i * P + k] - interp_1d[i * P + k]) / dx; 1626 interp_1d[i * P + k] = c3 * interp_1d[i * P + k] / dx; 1627 } 1628 c1 = c2; 1629 } 1630 } 1631 // Pass to CeedBasisCreateTensorH1 1632 CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P, Q, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); 1633 cleanup: 1634 CeedCall(CeedFree(&interp_1d)); 1635 CeedCall(CeedFree(&grad_1d)); 1636 CeedCall(CeedFree(&nodes)); 1637 CeedCall(CeedFree(&q_ref_1d)); 1638 CeedCall(CeedFree(&q_weight_1d)); 1639 return CEED_ERROR_SUCCESS; 1640 } 1641 1642 /** 1643 @brief Create a non tensor-product basis for \f$H^1\f$ discretizations 1644 1645 @param[in] ceed `Ceed` object used to create the `CeedBasis` 1646 @param[in] topo Topology of element, e.g. hypercube, simplex, etc 1647 @param[in] num_comp Number of field components (1 for scalar fields) 1648 @param[in] num_nodes Total number of nodes 1649 @param[in] num_qpts Total number of quadrature points 1650 @param[in] interp Row-major (`num_qpts * num_nodes`) matrix expressing the values of nodal basis functions at quadrature points 1651 @param[in] grad Row-major (`dim * num_qpts * num_nodes`) matrix expressing derivatives of nodal basis functions at quadrature points 1652 @param[in] q_ref Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element 1653 @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1654 @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1655 1656 @return An error code: 0 - success, otherwise - failure 1657 1658 @ref User 1659 **/ 1660 int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 1661 const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1662 CeedInt P = num_nodes, Q = num_qpts, dim = 0; 1663 1664 if (!ceed->BasisCreateH1) { 1665 Ceed delegate; 1666 1667 CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 1668 CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateH1"); 1669 CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis)); 1670 CeedCall(CeedDestroy(&delegate)); 1671 return CEED_ERROR_SUCCESS; 1672 } 1673 1674 CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1675 CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1676 CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1677 1678 CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1679 1680 CeedCall(CeedCalloc(1, basis)); 1681 CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1682 (*basis)->ref_count = 1; 1683 (*basis)->is_tensor_basis = false; 1684 (*basis)->dim = dim; 1685 (*basis)->topo = topo; 1686 (*basis)->num_comp = num_comp; 1687 (*basis)->P = P; 1688 (*basis)->Q = Q; 1689 (*basis)->fe_space = CEED_FE_SPACE_H1; 1690 CeedCall(CeedCalloc(Q * dim, &(*basis)->q_ref_1d)); 1691 CeedCall(CeedCalloc(Q, &(*basis)->q_weight_1d)); 1692 if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1693 if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 1694 CeedCall(CeedCalloc(Q * P, &(*basis)->interp)); 1695 CeedCall(CeedCalloc(dim * Q * P, &(*basis)->grad)); 1696 if (interp) memcpy((*basis)->interp, interp, Q * P * sizeof(interp[0])); 1697 if (grad) memcpy((*basis)->grad, grad, dim * Q * P * sizeof(grad[0])); 1698 CeedCall(ceed->BasisCreateH1(topo, dim, P, Q, interp, grad, q_ref, q_weight, *basis)); 1699 return CEED_ERROR_SUCCESS; 1700 } 1701 1702 /** 1703 @brief Create a non tensor-product basis for \f$H(\mathrm{div})\f$ discretizations 1704 1705 @param[in] ceed `Ceed` object used to create the `CeedBasis` 1706 @param[in] topo Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below 1707 @param[in] num_comp Number of components (usually 1 for vectors in H(div) bases) 1708 @param[in] num_nodes Total number of nodes (DoFs per element) 1709 @param[in] num_qpts Total number of quadrature points 1710 @param[in] interp Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points 1711 @param[in] div Row-major (`num_qpts * num_nodes`) matrix expressing divergence of basis functions at quadrature points 1712 @param[in] q_ref Array of length `num_qpts` * dim holding the locations of quadrature points on the reference element 1713 @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1714 @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1715 1716 @return An error code: 0 - success, otherwise - failure 1717 1718 @ref User 1719 **/ 1720 int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 1721 const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1722 CeedInt Q = num_qpts, P = num_nodes, dim = 0; 1723 1724 if (!ceed->BasisCreateHdiv) { 1725 Ceed delegate; 1726 1727 CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 1728 CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv"); 1729 CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis)); 1730 CeedCall(CeedDestroy(&delegate)); 1731 return CEED_ERROR_SUCCESS; 1732 } 1733 1734 CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1735 CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1736 CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1737 1738 CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1739 1740 CeedCall(CeedCalloc(1, basis)); 1741 CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1742 (*basis)->ref_count = 1; 1743 (*basis)->is_tensor_basis = false; 1744 (*basis)->dim = dim; 1745 (*basis)->topo = topo; 1746 (*basis)->num_comp = num_comp; 1747 (*basis)->P = P; 1748 (*basis)->Q = Q; 1749 (*basis)->fe_space = CEED_FE_SPACE_HDIV; 1750 CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 1751 CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 1752 if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1753 if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 1754 CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 1755 CeedCall(CeedMalloc(Q * P, &(*basis)->div)); 1756 if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 1757 if (div) memcpy((*basis)->div, div, Q * P * sizeof(div[0])); 1758 CeedCall(ceed->BasisCreateHdiv(topo, dim, P, Q, interp, div, q_ref, q_weight, *basis)); 1759 return CEED_ERROR_SUCCESS; 1760 } 1761 1762 /** 1763 @brief Create a non tensor-product basis for \f$H(\mathrm{curl})\f$ discretizations 1764 1765 @param[in] ceed `Ceed` object used to create the `CeedBasis` 1766 @param[in] topo Topology of element (`CEED_TOPOLOGY_QUAD`, `CEED_TOPOLOGY_PRISM`, etc.), dimension of which is used in some array sizes below 1767 @param[in] num_comp Number of components (usually 1 for vectors in \f$H(\mathrm{curl})\f$ bases) 1768 @param[in] num_nodes Total number of nodes (DoFs per element) 1769 @param[in] num_qpts Total number of quadrature points 1770 @param[in] interp Row-major (`dim * num_qpts * num_nodes`) matrix expressing the values of basis functions at quadrature points 1771 @param[in] curl Row-major (`curl_comp * num_qpts * num_nodes`, `curl_comp = 1` if `dim < 3` otherwise `curl_comp = dim`) matrix expressing curl of basis functions at quadrature points 1772 @param[in] q_ref Array of length `num_qpts * dim` holding the locations of quadrature points on the reference element 1773 @param[in] q_weight Array of length `num_qpts` holding the quadrature weights on the reference element 1774 @param[out] basis Address of the variable where the newly created `CeedBasis` will be stored 1775 1776 @return An error code: 0 - success, otherwise - failure 1777 1778 @ref User 1779 **/ 1780 int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 1781 const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis *basis) { 1782 CeedInt Q = num_qpts, P = num_nodes, dim = 0, curl_comp = 0; 1783 1784 if (!ceed->BasisCreateHcurl) { 1785 Ceed delegate; 1786 1787 CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); 1788 CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl"); 1789 CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis)); 1790 CeedCall(CeedDestroy(&delegate)); 1791 return CEED_ERROR_SUCCESS; 1792 } 1793 1794 CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 component"); 1795 CeedCheck(num_nodes > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 node"); 1796 CeedCheck(num_qpts > 0, ceed, CEED_ERROR_DIMENSION, "CeedBasis must have at least 1 quadrature point"); 1797 1798 CeedCall(CeedBasisGetTopologyDimension(topo, &dim)); 1799 curl_comp = (dim < 3) ? 1 : dim; 1800 1801 CeedCall(CeedCalloc(1, basis)); 1802 CeedCall(CeedReferenceCopy(ceed, &(*basis)->ceed)); 1803 (*basis)->ref_count = 1; 1804 (*basis)->is_tensor_basis = false; 1805 (*basis)->dim = dim; 1806 (*basis)->topo = topo; 1807 (*basis)->num_comp = num_comp; 1808 (*basis)->P = P; 1809 (*basis)->Q = Q; 1810 (*basis)->fe_space = CEED_FE_SPACE_HCURL; 1811 CeedCall(CeedMalloc(Q * dim, &(*basis)->q_ref_1d)); 1812 CeedCall(CeedMalloc(Q, &(*basis)->q_weight_1d)); 1813 if (q_ref) memcpy((*basis)->q_ref_1d, q_ref, Q * dim * sizeof(q_ref[0])); 1814 if (q_weight) memcpy((*basis)->q_weight_1d, q_weight, Q * sizeof(q_weight[0])); 1815 CeedCall(CeedMalloc(dim * Q * P, &(*basis)->interp)); 1816 CeedCall(CeedMalloc(curl_comp * Q * P, &(*basis)->curl)); 1817 if (interp) memcpy((*basis)->interp, interp, dim * Q * P * sizeof(interp[0])); 1818 if (curl) memcpy((*basis)->curl, curl, curl_comp * Q * P * sizeof(curl[0])); 1819 CeedCall(ceed->BasisCreateHcurl(topo, dim, P, Q, interp, curl, q_ref, q_weight, *basis)); 1820 return CEED_ERROR_SUCCESS; 1821 } 1822 1823 /** 1824 @brief Create a `CeedBasis` for projection from the nodes of `basis_from` to the nodes of `basis_to`. 1825 1826 Only @ref CEED_EVAL_INTERP will be valid for the new basis, `basis_project`. 1827 For \f$H^1\f$ spaces, @ref CEED_EVAL_GRAD will also be valid. 1828 The interpolation is given by `interp_project = interp_to^+ * interp_from`, where the pseudoinverse `interp_to^+` is given by QR factorization. 1829 The gradient (for the \f$H^1\f$ case) is given by `grad_project = interp_to^+ * grad_from`. 1830 1831 Note: `basis_from` and `basis_to` must have compatible quadrature spaces. 1832 1833 Note: `basis_project` will have the same number of components as `basis_from`, regardless of the number of components that `basis_to` has. 1834 If `basis_from` has 3 components and `basis_to` has 5 components, then `basis_project` will have 3 components. 1835 1836 Note: If either `basis_from` or `basis_to` are non-tensor, then `basis_project` will also be non-tensor 1837 1838 @param[in] basis_from `CeedBasis` to prolong from 1839 @param[in] basis_to `CeedBasis` to prolong to 1840 @param[out] basis_project Address of the variable where the newly created `CeedBasis` will be stored 1841 1842 @return An error code: 0 - success, otherwise - failure 1843 1844 @ref User 1845 **/ 1846 int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasis *basis_project) { 1847 Ceed ceed; 1848 bool create_tensor; 1849 CeedInt dim, num_comp; 1850 CeedScalar *interp_project, *grad_project; 1851 1852 CeedCall(CeedBasisGetCeed(basis_to, &ceed)); 1853 1854 // Create projection matrix 1855 CeedCall(CeedBasisCreateProjectionMatrices(basis_from, basis_to, &interp_project, &grad_project)); 1856 1857 // Build basis 1858 { 1859 bool is_tensor_to, is_tensor_from; 1860 1861 CeedCall(CeedBasisIsTensor(basis_to, &is_tensor_to)); 1862 CeedCall(CeedBasisIsTensor(basis_from, &is_tensor_from)); 1863 create_tensor = is_tensor_from && is_tensor_to; 1864 } 1865 CeedCall(CeedBasisGetDimension(basis_to, &dim)); 1866 CeedCall(CeedBasisGetNumComponents(basis_from, &num_comp)); 1867 if (create_tensor) { 1868 CeedInt P_1d_to, P_1d_from; 1869 1870 CeedCall(CeedBasisGetNumNodes1D(basis_from, &P_1d_from)); 1871 CeedCall(CeedBasisGetNumNodes1D(basis_to, &P_1d_to)); 1872 CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_from, P_1d_to, interp_project, grad_project, NULL, NULL, basis_project)); 1873 } else { 1874 // Even if basis_to and basis_from are not H1, the resulting basis is H1 for interpolation to work 1875 CeedInt num_nodes_to, num_nodes_from; 1876 CeedElemTopology topo; 1877 1878 CeedCall(CeedBasisGetTopology(basis_from, &topo)); 1879 CeedCall(CeedBasisGetNumNodes(basis_from, &num_nodes_from)); 1880 CeedCall(CeedBasisGetNumNodes(basis_to, &num_nodes_to)); 1881 CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_from, num_nodes_to, interp_project, grad_project, NULL, NULL, basis_project)); 1882 } 1883 1884 // Cleanup 1885 CeedCall(CeedFree(&interp_project)); 1886 CeedCall(CeedFree(&grad_project)); 1887 CeedCall(CeedDestroy(&ceed)); 1888 return CEED_ERROR_SUCCESS; 1889 } 1890 1891 /** 1892 @brief Copy the pointer to a `CeedBasis`. 1893 1894 Note: If the value of `*basis_copy` passed into this function is non-`NULL`, then it is assumed that `*basis_copy` is a pointer to a `CeedBasis`. 1895 This `CeedBasis` will be destroyed if `*basis_copy` is the only reference to this `CeedBasis`. 1896 1897 @param[in] basis `CeedBasis` to copy reference to 1898 @param[in,out] basis_copy Variable to store copied reference 1899 1900 @return An error code: 0 - success, otherwise - failure 1901 1902 @ref User 1903 **/ 1904 int CeedBasisReferenceCopy(CeedBasis basis, CeedBasis *basis_copy) { 1905 if (basis != CEED_BASIS_NONE) CeedCall(CeedBasisReference(basis)); 1906 CeedCall(CeedBasisDestroy(basis_copy)); 1907 *basis_copy = basis; 1908 return CEED_ERROR_SUCCESS; 1909 } 1910 1911 /** 1912 @brief Set the number of tabs to indent for @ref CeedBasisView() output 1913 1914 @param[in] basis `CeedBasis` to set the number of view tabs 1915 @param[in] num_tabs Number of view tabs to set 1916 1917 @return Error code: 0 - success, otherwise - failure 1918 1919 @ref User 1920 **/ 1921 int CeedBasisSetNumViewTabs(CeedBasis basis, CeedInt num_tabs) { 1922 CeedCheck(num_tabs >= 0, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Number of view tabs must be non-negative"); 1923 basis->num_tabs = num_tabs; 1924 return CEED_ERROR_SUCCESS; 1925 } 1926 1927 /** 1928 @brief View a `CeedBasis` 1929 1930 @param[in] basis `CeedBasis` to view 1931 @param[in] stream Stream to view to, e.g., `stdout` 1932 1933 @return An error code: 0 - success, otherwise - failure 1934 1935 @ref User 1936 **/ 1937 int CeedBasisView(CeedBasis basis, FILE *stream) { 1938 bool is_tensor_basis; 1939 char *tabs = NULL; 1940 CeedElemTopology topo; 1941 CeedFESpace fe_space; 1942 1943 // Basis data 1944 CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 1945 CeedCall(CeedBasisGetTopology(basis, &topo)); 1946 CeedCall(CeedBasisGetFESpace(basis, &fe_space)); 1947 1948 { 1949 CeedInt num_tabs = 0; 1950 1951 CeedCall(CeedBasisGetNumViewTabs(basis, &num_tabs)); 1952 CeedCall(CeedCalloc(CEED_TAB_WIDTH * num_tabs + 1, &tabs)); 1953 for (CeedInt i = 0; i < CEED_TAB_WIDTH * num_tabs; i++) tabs[i] = ' '; 1954 } 1955 1956 // Print FE space and element topology of the basis 1957 fprintf(stream, "%sCeedBasis in a %s on a %s element\n", tabs, CeedFESpaces[fe_space], CeedElemTopologies[topo]); 1958 if (is_tensor_basis) { 1959 fprintf(stream, "%s P: %" CeedInt_FMT "\n%s Q: %" CeedInt_FMT "\n", tabs, basis->P_1d, tabs, basis->Q_1d); 1960 } else { 1961 fprintf(stream, "%s P: %" CeedInt_FMT "\n%s Q: %" CeedInt_FMT "\n", tabs, basis->P, tabs, basis->Q); 1962 } 1963 fprintf(stream, "%s dimension: %" CeedInt_FMT "\n%s field components: %" CeedInt_FMT "\n", tabs, basis->dim, tabs, basis->num_comp); 1964 // Print quadrature data, interpolation/gradient/divergence/curl of the basis 1965 if (is_tensor_basis) { // tensor basis 1966 CeedInt P_1d, Q_1d; 1967 const CeedScalar *q_ref_1d, *q_weight_1d, *interp_1d, *grad_1d; 1968 1969 CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 1970 CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 1971 CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); 1972 CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d)); 1973 CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 1974 CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 1975 1976 CeedCall(CeedScalarView("qref1d", "\t% 12.8f", 1, Q_1d, q_ref_1d, tabs, stream)); 1977 CeedCall(CeedScalarView("qweight1d", "\t% 12.8f", 1, Q_1d, q_weight_1d, tabs, stream)); 1978 CeedCall(CeedScalarView("interp1d", "\t% 12.8f", Q_1d, P_1d, interp_1d, tabs, stream)); 1979 CeedCall(CeedScalarView("grad1d", "\t% 12.8f", Q_1d, P_1d, grad_1d, tabs, stream)); 1980 } else { // non-tensor basis 1981 CeedInt P, Q, dim, q_comp; 1982 const CeedScalar *q_ref, *q_weight, *interp, *grad, *div, *curl; 1983 1984 CeedCall(CeedBasisGetNumNodes(basis, &P)); 1985 CeedCall(CeedBasisGetNumQuadraturePoints(basis, &Q)); 1986 CeedCall(CeedBasisGetDimension(basis, &dim)); 1987 CeedCall(CeedBasisGetQRef(basis, &q_ref)); 1988 CeedCall(CeedBasisGetQWeights(basis, &q_weight)); 1989 CeedCall(CeedBasisGetInterp(basis, &interp)); 1990 CeedCall(CeedBasisGetGrad(basis, &grad)); 1991 CeedCall(CeedBasisGetDiv(basis, &div)); 1992 CeedCall(CeedBasisGetCurl(basis, &curl)); 1993 1994 CeedCall(CeedScalarView("qref", "\t% 12.8f", 1, Q * dim, q_ref, tabs, stream)); 1995 CeedCall(CeedScalarView("qweight", "\t% 12.8f", 1, Q, q_weight, tabs, stream)); 1996 CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp)); 1997 CeedCall(CeedScalarView("interp", "\t% 12.8f", q_comp * Q, P, interp, tabs, stream)); 1998 if (grad) { 1999 CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp)); 2000 CeedCall(CeedScalarView("grad", "\t% 12.8f", q_comp * Q, P, grad, tabs, stream)); 2001 } 2002 if (div) { 2003 CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp)); 2004 CeedCall(CeedScalarView("div", "\t% 12.8f", q_comp * Q, P, div, tabs, stream)); 2005 } 2006 if (curl) { 2007 CeedCall(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp)); 2008 CeedCall(CeedScalarView("curl", "\t% 12.8f", q_comp * Q, P, curl, tabs, stream)); 2009 } 2010 } 2011 CeedCall(CeedFree(&tabs)); 2012 return CEED_ERROR_SUCCESS; 2013 } 2014 2015 /** 2016 @brief Apply basis evaluation from nodes to quadrature points or vice versa 2017 2018 @param[in] basis `CeedBasis` to evaluate 2019 @param[in] num_elem The number of elements to apply the basis evaluation to; 2020 the backend will specify the ordering in @ref CeedElemRestrictionCreate() 2021 @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to quadrature points; 2022 @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes 2023 @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 2024 @ref CEED_EVAL_INTERP to use interpolated values, 2025 @ref CEED_EVAL_GRAD to use gradients, 2026 @ref CEED_EVAL_DIV to use divergence, 2027 @ref CEED_EVAL_CURL to use curl, 2028 @ref CEED_EVAL_WEIGHT to use quadrature weights 2029 @param[in] u Input `CeedVector` 2030 @param[out] v Output `CeedVector` 2031 2032 @return An error code: 0 - success, otherwise - failure 2033 2034 @ref User 2035 **/ 2036 int CeedBasisApply(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 2037 CeedCall(CeedBasisApplyCheckDims(basis, num_elem, t_mode, eval_mode, u, v)); 2038 CeedCheck(basis->Apply, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedBasisApply"); 2039 CeedCall(basis->Apply(basis, num_elem, t_mode, eval_mode, u, v)); 2040 return CEED_ERROR_SUCCESS; 2041 } 2042 2043 /** 2044 @brief Apply basis evaluation from quadrature points to nodes and sum into target vector 2045 2046 @param[in] basis `CeedBasis` to evaluate 2047 @param[in] num_elem The number of elements to apply the basis evaluation to; 2048 the backend will specify the ordering in @ref CeedElemRestrictionCreate() 2049 @param[in] t_mode @ref CEED_TRANSPOSE to apply the transpose, mapping from quadrature points to nodes; 2050 @ref CEED_NOTRANSPOSE is not valid for `CeedBasisApplyAdd()` 2051 @param[in] eval_mode @ref CEED_EVAL_NONE to use values directly, 2052 @ref CEED_EVAL_INTERP to use interpolated values, 2053 @ref CEED_EVAL_GRAD to use gradients, 2054 @ref CEED_EVAL_DIV to use divergence, 2055 @ref CEED_EVAL_CURL to use curl, 2056 @ref CEED_EVAL_WEIGHT to use quadrature weights 2057 @param[in] u Input `CeedVector` 2058 @param[out] v Output `CeedVector` to sum into 2059 2060 @return An error code: 0 - success, otherwise - failure 2061 2062 @ref User 2063 **/ 2064 int CeedBasisApplyAdd(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { 2065 CeedCheck(t_mode == CEED_TRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "CeedBasisApplyAdd only supports CEED_TRANSPOSE"); 2066 CeedCall(CeedBasisApplyCheckDims(basis, num_elem, t_mode, eval_mode, u, v)); 2067 CeedCheck(basis->ApplyAdd, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedBasisApplyAdd"); 2068 CeedCall(basis->ApplyAdd(basis, num_elem, t_mode, eval_mode, u, v)); 2069 return CEED_ERROR_SUCCESS; 2070 } 2071 2072 /** 2073 @brief Apply basis evaluation from nodes to arbitrary points 2074 2075 @param[in] basis `CeedBasis` to evaluate 2076 @param[in] num_elem The number of elements to apply the basis evaluation to; 2077 the backend will specify the ordering in @ref CeedElemRestrictionCreate() 2078 @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 2079 @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 2080 @ref CEED_TRANSPOSE to apply the transpose, mapping from points to nodes 2081 @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 2082 @ref CEED_EVAL_GRAD to use gradients, 2083 @ref CEED_EVAL_WEIGHT to use quadrature weights 2084 @param[in] x_ref `CeedVector` holding reference coordinates of each point 2085 @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 2086 @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 2087 2088 @return An error code: 0 - success, otherwise - failure 2089 2090 @ref User 2091 **/ 2092 int CeedBasisApplyAtPoints(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, 2093 CeedVector x_ref, CeedVector u, CeedVector v) { 2094 CeedCall(CeedBasisApplyAtPointsCheckDims(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2095 if (basis->ApplyAtPoints) { 2096 CeedCall(basis->ApplyAtPoints(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2097 } else { 2098 CeedCall(CeedBasisApplyAtPoints_Core(basis, false, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2099 } 2100 return CEED_ERROR_SUCCESS; 2101 } 2102 2103 /** 2104 @brief Apply basis evaluation from nodes to arbitrary points and sum into target vector 2105 2106 @param[in] basis `CeedBasis` to evaluate 2107 @param[in] num_elem The number of elements to apply the basis evaluation to; 2108 the backend will specify the ordering in @ref CeedElemRestrictionCreate() 2109 @param[in] num_points Array of the number of points to apply the basis evaluation to in each element, size `num_elem` 2110 @param[in] t_mode @ref CEED_NOTRANSPOSE to evaluate from nodes to points; 2111 @ref CEED_NOTRANSPOSE is not valid for `CeedBasisApplyAddAtPoints()` 2112 @param[in] eval_mode @ref CEED_EVAL_INTERP to use interpolated values, 2113 @ref CEED_EVAL_GRAD to use gradients, 2114 @ref CEED_EVAL_WEIGHT to use quadrature weights 2115 @param[in] x_ref `CeedVector` holding reference coordinates of each point 2116 @param[in] u Input `CeedVector`, of length `num_nodes * num_comp` for @ref CEED_NOTRANSPOSE 2117 @param[out] v Output `CeedVector`, of length `num_points * num_q_comp` for @ref CEED_NOTRANSPOSE with @ref CEED_EVAL_INTERP 2118 2119 @return An error code: 0 - success, otherwise - failure 2120 2121 @ref User 2122 **/ 2123 int CeedBasisApplyAddAtPoints(CeedBasis basis, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, 2124 CeedVector x_ref, CeedVector u, CeedVector v) { 2125 CeedCheck(t_mode == CEED_TRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "CeedBasisApplyAddAtPoints only supports CEED_TRANSPOSE"); 2126 CeedCall(CeedBasisApplyAtPointsCheckDims(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2127 if (basis->ApplyAddAtPoints) { 2128 CeedCall(basis->ApplyAddAtPoints(basis, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2129 } else { 2130 CeedCall(CeedBasisApplyAtPoints_Core(basis, true, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 2131 } 2132 return CEED_ERROR_SUCCESS; 2133 } 2134 2135 /** 2136 @brief Get the `Ceed` associated with a `CeedBasis` 2137 2138 @param[in] basis `CeedBasis` 2139 @param[out] ceed Variable to store `Ceed` 2140 2141 @return An error code: 0 - success, otherwise - failure 2142 2143 @ref Advanced 2144 **/ 2145 int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) { 2146 *ceed = NULL; 2147 CeedCall(CeedReferenceCopy(CeedBasisReturnCeed(basis), ceed)); 2148 return CEED_ERROR_SUCCESS; 2149 } 2150 2151 /** 2152 @brief Return the `Ceed` associated with a `CeedBasis` 2153 2154 @param[in] basis `CeedBasis` 2155 2156 @return `Ceed` associated with the `basis` 2157 2158 @ref Advanced 2159 **/ 2160 Ceed CeedBasisReturnCeed(CeedBasis basis) { return basis->ceed; } 2161 2162 /** 2163 @brief Get dimension for given `CeedBasis` 2164 2165 @param[in] basis `CeedBasis` 2166 @param[out] dim Variable to store dimension of basis 2167 2168 @return An error code: 0 - success, otherwise - failure 2169 2170 @ref Advanced 2171 **/ 2172 int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim) { 2173 *dim = basis->dim; 2174 return CEED_ERROR_SUCCESS; 2175 } 2176 2177 /** 2178 @brief Get topology for given `CeedBasis` 2179 2180 @param[in] basis `CeedBasis` 2181 @param[out] topo Variable to store topology of basis 2182 2183 @return An error code: 0 - success, otherwise - failure 2184 2185 @ref Advanced 2186 **/ 2187 int CeedBasisGetTopology(CeedBasis basis, CeedElemTopology *topo) { 2188 *topo = basis->topo; 2189 return CEED_ERROR_SUCCESS; 2190 } 2191 2192 /** 2193 @brief Get number of components for given `CeedBasis` 2194 2195 @param[in] basis `CeedBasis` 2196 @param[out] num_comp Variable to store number of components 2197 2198 @return An error code: 0 - success, otherwise - failure 2199 2200 @ref Advanced 2201 **/ 2202 int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *num_comp) { 2203 *num_comp = basis->num_comp; 2204 return CEED_ERROR_SUCCESS; 2205 } 2206 2207 /** 2208 @brief Get total number of nodes (in `dim` dimensions) of a `CeedBasis` 2209 2210 @param[in] basis `CeedBasis` 2211 @param[out] P Variable to store number of nodes 2212 2213 @return An error code: 0 - success, otherwise - failure 2214 2215 @ref Utility 2216 **/ 2217 int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P) { 2218 *P = basis->P; 2219 return CEED_ERROR_SUCCESS; 2220 } 2221 2222 /** 2223 @brief Get total number of nodes (in 1 dimension) of a `CeedBasis` 2224 2225 @param[in] basis `CeedBasis` 2226 @param[out] P_1d Variable to store number of nodes 2227 2228 @return An error code: 0 - success, otherwise - failure 2229 2230 @ref Advanced 2231 **/ 2232 int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P_1d) { 2233 CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply P_1d for non-tensor CeedBasis"); 2234 *P_1d = basis->P_1d; 2235 return CEED_ERROR_SUCCESS; 2236 } 2237 2238 /** 2239 @brief Get total number of quadrature points (in `dim` dimensions) of a `CeedBasis` 2240 2241 @param[in] basis `CeedBasis` 2242 @param[out] Q Variable to store number of quadrature points 2243 2244 @return An error code: 0 - success, otherwise - failure 2245 2246 @ref Utility 2247 **/ 2248 int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q) { 2249 *Q = basis->Q; 2250 return CEED_ERROR_SUCCESS; 2251 } 2252 2253 /** 2254 @brief Get total number of quadrature points (in 1 dimension) of a `CeedBasis` 2255 2256 @param[in] basis `CeedBasis` 2257 @param[out] Q_1d Variable to store number of quadrature points 2258 2259 @return An error code: 0 - success, otherwise - failure 2260 2261 @ref Advanced 2262 **/ 2263 int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q_1d) { 2264 CeedCheck(basis->is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "Cannot supply Q_1d for non-tensor CeedBasis"); 2265 *Q_1d = basis->Q_1d; 2266 return CEED_ERROR_SUCCESS; 2267 } 2268 2269 /** 2270 @brief Get reference coordinates of quadrature points (in `dim` dimensions) of a `CeedBasis` 2271 2272 @param[in] basis `CeedBasis` 2273 @param[out] q_ref Variable to store reference coordinates of quadrature points 2274 2275 @return An error code: 0 - success, otherwise - failure 2276 2277 @ref Advanced 2278 **/ 2279 int CeedBasisGetQRef(CeedBasis basis, const CeedScalar **q_ref) { 2280 *q_ref = basis->q_ref_1d; 2281 return CEED_ERROR_SUCCESS; 2282 } 2283 2284 /** 2285 @brief Get quadrature weights of quadrature points (in `dim` dimensions) of a `CeedBasis` 2286 2287 @param[in] basis `CeedBasis` 2288 @param[out] q_weight Variable to store quadrature weights 2289 2290 @return An error code: 0 - success, otherwise - failure 2291 2292 @ref Advanced 2293 **/ 2294 int CeedBasisGetQWeights(CeedBasis basis, const CeedScalar **q_weight) { 2295 *q_weight = basis->q_weight_1d; 2296 return CEED_ERROR_SUCCESS; 2297 } 2298 2299 /** 2300 @brief Get interpolation matrix of a `CeedBasis` 2301 2302 @param[in] basis `CeedBasis` 2303 @param[out] interp Variable to store interpolation matrix 2304 2305 @return An error code: 0 - success, otherwise - failure 2306 2307 @ref Advanced 2308 **/ 2309 int CeedBasisGetInterp(CeedBasis basis, const CeedScalar **interp) { 2310 if (!basis->interp && basis->is_tensor_basis) { 2311 // Allocate 2312 CeedCall(CeedMalloc(basis->Q * basis->P, &basis->interp)); 2313 2314 // Initialize 2315 for (CeedInt i = 0; i < basis->Q * basis->P; i++) basis->interp[i] = 1.0; 2316 2317 // Calculate 2318 for (CeedInt d = 0; d < basis->dim; d++) { 2319 for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 2320 for (CeedInt node = 0; node < basis->P; node++) { 2321 CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 2322 CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 2323 2324 basis->interp[qpt * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 2325 } 2326 } 2327 } 2328 } 2329 *interp = basis->interp; 2330 return CEED_ERROR_SUCCESS; 2331 } 2332 2333 /** 2334 @brief Get 1D interpolation matrix of a tensor product `CeedBasis` 2335 2336 @param[in] basis `CeedBasis` 2337 @param[out] interp_1d Variable to store interpolation matrix 2338 2339 @return An error code: 0 - success, otherwise - failure 2340 2341 @ref Backend 2342 **/ 2343 int CeedBasisGetInterp1D(CeedBasis basis, const CeedScalar **interp_1d) { 2344 bool is_tensor_basis; 2345 2346 CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 2347 CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis"); 2348 *interp_1d = basis->interp_1d; 2349 return CEED_ERROR_SUCCESS; 2350 } 2351 2352 /** 2353 @brief Get gradient matrix of a `CeedBasis` 2354 2355 @param[in] basis `CeedBasis` 2356 @param[out] grad Variable to store gradient matrix 2357 2358 @return An error code: 0 - success, otherwise - failure 2359 2360 @ref Advanced 2361 **/ 2362 int CeedBasisGetGrad(CeedBasis basis, const CeedScalar **grad) { 2363 if (!basis->grad && basis->is_tensor_basis) { 2364 // Allocate 2365 CeedCall(CeedMalloc(basis->dim * basis->Q * basis->P, &basis->grad)); 2366 2367 // Initialize 2368 for (CeedInt i = 0; i < basis->dim * basis->Q * basis->P; i++) basis->grad[i] = 1.0; 2369 2370 // Calculate 2371 for (CeedInt d = 0; d < basis->dim; d++) { 2372 for (CeedInt i = 0; i < basis->dim; i++) { 2373 for (CeedInt qpt = 0; qpt < basis->Q; qpt++) { 2374 for (CeedInt node = 0; node < basis->P; node++) { 2375 CeedInt p = (node / CeedIntPow(basis->P_1d, d)) % basis->P_1d; 2376 CeedInt q = (qpt / CeedIntPow(basis->Q_1d, d)) % basis->Q_1d; 2377 2378 if (i == d) basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->grad_1d[q * basis->P_1d + p]; 2379 else basis->grad[(i * basis->Q + qpt) * (basis->P) + node] *= basis->interp_1d[q * basis->P_1d + p]; 2380 } 2381 } 2382 } 2383 } 2384 } 2385 *grad = basis->grad; 2386 return CEED_ERROR_SUCCESS; 2387 } 2388 2389 /** 2390 @brief Get 1D gradient matrix of a tensor product `CeedBasis` 2391 2392 @param[in] basis `CeedBasis` 2393 @param[out] grad_1d Variable to store gradient matrix 2394 2395 @return An error code: 0 - success, otherwise - failure 2396 2397 @ref Advanced 2398 **/ 2399 int CeedBasisGetGrad1D(CeedBasis basis, const CeedScalar **grad_1d) { 2400 bool is_tensor_basis; 2401 2402 CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 2403 CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_MINOR, "CeedBasis is not a tensor product CeedBasis"); 2404 *grad_1d = basis->grad_1d; 2405 return CEED_ERROR_SUCCESS; 2406 } 2407 2408 /** 2409 @brief Get divergence matrix of a `CeedBasis` 2410 2411 @param[in] basis `CeedBasis` 2412 @param[out] div Variable to store divergence matrix 2413 2414 @return An error code: 0 - success, otherwise - failure 2415 2416 @ref Advanced 2417 **/ 2418 int CeedBasisGetDiv(CeedBasis basis, const CeedScalar **div) { 2419 *div = basis->div; 2420 return CEED_ERROR_SUCCESS; 2421 } 2422 2423 /** 2424 @brief Get curl matrix of a `CeedBasis` 2425 2426 @param[in] basis `CeedBasis` 2427 @param[out] curl Variable to store curl matrix 2428 2429 @return An error code: 0 - success, otherwise - failure 2430 2431 @ref Advanced 2432 **/ 2433 int CeedBasisGetCurl(CeedBasis basis, const CeedScalar **curl) { 2434 *curl = basis->curl; 2435 return CEED_ERROR_SUCCESS; 2436 } 2437 2438 /** 2439 @brief Destroy a @ref CeedBasis 2440 2441 @param[in,out] basis `CeedBasis` to destroy 2442 2443 @return An error code: 0 - success, otherwise - failure 2444 2445 @ref User 2446 **/ 2447 int CeedBasisDestroy(CeedBasis *basis) { 2448 if (!*basis || *basis == CEED_BASIS_NONE || --(*basis)->ref_count > 0) { 2449 *basis = NULL; 2450 return CEED_ERROR_SUCCESS; 2451 } 2452 if ((*basis)->Destroy) CeedCall((*basis)->Destroy(*basis)); 2453 CeedCall(CeedTensorContractDestroy(&(*basis)->contract)); 2454 CeedCall(CeedFree(&(*basis)->q_ref_1d)); 2455 CeedCall(CeedFree(&(*basis)->q_weight_1d)); 2456 CeedCall(CeedFree(&(*basis)->interp)); 2457 CeedCall(CeedFree(&(*basis)->interp_1d)); 2458 CeedCall(CeedFree(&(*basis)->grad)); 2459 CeedCall(CeedFree(&(*basis)->grad_1d)); 2460 CeedCall(CeedFree(&(*basis)->div)); 2461 CeedCall(CeedFree(&(*basis)->curl)); 2462 CeedCall(CeedVectorDestroy(&(*basis)->vec_chebyshev)); 2463 CeedCall(CeedBasisDestroy(&(*basis)->basis_chebyshev)); 2464 CeedCall(CeedDestroy(&(*basis)->ceed)); 2465 CeedCall(CeedFree(basis)); 2466 return CEED_ERROR_SUCCESS; 2467 } 2468 2469 /** 2470 @brief Construct a Gauss-Legendre quadrature 2471 2472 @param[in] Q Number of quadrature points (integrates polynomials of degree `2*Q-1` exactly) 2473 @param[out] q_ref_1d Array of length `Q` to hold the abscissa on `[-1, 1]` 2474 @param[out] q_weight_1d Array of length `Q` to hold the weights 2475 2476 @return An error code: 0 - success, otherwise - failure 2477 2478 @ref Utility 2479 **/ 2480 int CeedGaussQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2481 CeedScalar P0, P1, P2, dP2, xi, wi, PI = 4.0 * atan(1.0); 2482 2483 // Build q_ref_1d, q_weight_1d 2484 for (CeedInt i = 0; i <= Q / 2; i++) { 2485 // Guess 2486 xi = cos(PI * (CeedScalar)(2 * i + 1) / ((CeedScalar)(2 * Q))); 2487 // Pn(xi) 2488 P0 = 1.0; 2489 P1 = xi; 2490 P2 = 0.0; 2491 for (CeedInt j = 2; j <= Q; j++) { 2492 P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2493 P0 = P1; 2494 P1 = P2; 2495 } 2496 // First Newton Step 2497 dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2498 xi = xi - P2 / dP2; 2499 // Newton to convergence 2500 for (CeedInt k = 0; k < 100 && fabs(P2) > 10 * CEED_EPSILON; k++) { 2501 P0 = 1.0; 2502 P1 = xi; 2503 for (CeedInt j = 2; j <= Q; j++) { 2504 P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2505 P0 = P1; 2506 P1 = P2; 2507 } 2508 dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2509 xi = xi - P2 / dP2; 2510 } 2511 // Save xi, wi 2512 wi = 2.0 / ((1.0 - xi * xi) * dP2 * dP2); 2513 q_weight_1d[i] = wi; 2514 q_weight_1d[Q - 1 - i] = wi; 2515 q_ref_1d[i] = -xi; 2516 q_ref_1d[Q - 1 - i] = xi; 2517 } 2518 return CEED_ERROR_SUCCESS; 2519 } 2520 2521 /** 2522 @brief Construct a Gauss-Legendre-Lobatto quadrature 2523 2524 @param[in] Q Number of quadrature points (integrates polynomials of degree `2*Q-3` exactly) 2525 @param[out] q_ref_1d Array of length `Q` to hold the abscissa on `[-1, 1]` 2526 @param[out] q_weight_1d Array of length `Q` to hold the weights 2527 2528 @return An error code: 0 - success, otherwise - failure 2529 2530 @ref Utility 2531 **/ 2532 int CeedLobattoQuadrature(CeedInt Q, CeedScalar *q_ref_1d, CeedScalar *q_weight_1d) { 2533 CeedScalar P0, P1, P2, dP2, d2P2, xi, wi, PI = 4.0 * atan(1.0); 2534 2535 // Build q_ref_1d, q_weight_1d 2536 // Set endpoints 2537 CeedCheck(Q > 1, NULL, CEED_ERROR_DIMENSION, "Cannot create Lobatto quadrature with Q=%" CeedInt_FMT " < 2 points", Q); 2538 wi = 2.0 / ((CeedScalar)(Q * (Q - 1))); 2539 if (q_weight_1d) { 2540 q_weight_1d[0] = wi; 2541 q_weight_1d[Q - 1] = wi; 2542 } 2543 q_ref_1d[0] = -1.0; 2544 q_ref_1d[Q - 1] = 1.0; 2545 // Interior 2546 for (CeedInt i = 1; i <= (Q - 1) / 2; i++) { 2547 // Guess 2548 xi = cos(PI * (CeedScalar)(i) / (CeedScalar)(Q - 1)); 2549 // Pn(xi) 2550 P0 = 1.0; 2551 P1 = xi; 2552 P2 = 0.0; 2553 for (CeedInt j = 2; j < Q; j++) { 2554 P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2555 P0 = P1; 2556 P1 = P2; 2557 } 2558 // First Newton step 2559 dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2560 d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2561 xi = xi - dP2 / d2P2; 2562 // Newton to convergence 2563 for (CeedInt k = 0; k < 100 && fabs(dP2) > 10 * CEED_EPSILON; k++) { 2564 P0 = 1.0; 2565 P1 = xi; 2566 for (CeedInt j = 2; j < Q; j++) { 2567 P2 = (((CeedScalar)(2 * j - 1)) * xi * P1 - ((CeedScalar)(j - 1)) * P0) / ((CeedScalar)(j)); 2568 P0 = P1; 2569 P1 = P2; 2570 } 2571 dP2 = (xi * P2 - P0) * (CeedScalar)Q / (xi * xi - 1.0); 2572 d2P2 = (2 * xi * dP2 - (CeedScalar)(Q * (Q - 1)) * P2) / (1.0 - xi * xi); 2573 xi = xi - dP2 / d2P2; 2574 } 2575 // Save xi, wi 2576 wi = 2.0 / (((CeedScalar)(Q * (Q - 1))) * P2 * P2); 2577 if (q_weight_1d) { 2578 q_weight_1d[i] = wi; 2579 q_weight_1d[Q - 1 - i] = wi; 2580 } 2581 q_ref_1d[i] = -xi; 2582 q_ref_1d[Q - 1 - i] = xi; 2583 } 2584 return CEED_ERROR_SUCCESS; 2585 } 2586 2587 /// @} 2588