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