1 // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at 2 // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights 3 // reserved. See files LICENSE and NOTICE for details. 4 // 5 // This file is part of CEED, a collection of benchmarks, miniapps, software 6 // libraries and APIs for efficient high-order finite element and spectral 7 // element discretizations for exascale applications. For more information and 8 // source code availability see http://github.com/ceed. 9 // 10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 11 // a collaborative effort of two U.S. Department of Energy organizations (Office 12 // of Science and the National Nuclear Security Administration) responsible for 13 // the planning and preparation of a capable exascale ecosystem, including 14 // software, applications, hardware, advanced system engineering and early 15 // testbed platforms, in support of the nation's exascale computing imperative. 16 17 #include <ceed/ceed.h> 18 #include <ceed/backend.h> 19 #include <ceed-impl.h> 20 #include <limits.h> 21 #include <stdbool.h> 22 #include <stdio.h> 23 #include <string.h> 24 25 /// @file 26 /// Implementation of public CeedQFunction interfaces 27 28 /// @cond DOXYGEN_SKIP 29 static struct CeedQFunction_private ceed_qfunction_none; 30 /// @endcond 31 32 /// @addtogroup CeedQFunctionUser 33 /// @{ 34 35 // Indicate that no QFunction is provided by the user 36 const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none; 37 38 /// @} 39 40 /// @cond DOXYGEN_SKIP 41 static struct { 42 char name[CEED_MAX_RESOURCE_LEN]; 43 char source[CEED_MAX_RESOURCE_LEN]; 44 CeedInt vec_length; 45 CeedQFunctionUser f; 46 int (*init)(Ceed ceed, const char *name, CeedQFunction qf); 47 } gallery_qfunctions[1024]; 48 static size_t num_qfunctions; 49 /// @endcond 50 51 /// ---------------------------------------------------------------------------- 52 /// CeedQFunction Library Internal Functions 53 /// ---------------------------------------------------------------------------- 54 /// @addtogroup CeedQFunctionDeveloper 55 /// @{ 56 57 /** 58 @brief Register a gallery QFunction 59 60 @param name Name for this backend to respond to 61 @param source Absolute path to source of QFunction, 62 "\path\CEED_DIR\gallery\folder\file.h:function_name" 63 @param vec_length Vector length. Caller must ensure that number of quadrature 64 points is a multiple of vec_length. 65 @param f Function pointer to evaluate action at quadrature points. 66 See \ref CeedQFunctionUser. 67 @param init Initialization function called by CeedQFunctionInit() when the 68 QFunction is selected. 69 70 @return An error code: 0 - success, otherwise - failure 71 72 @ref Developer 73 **/ 74 int CeedQFunctionRegister(const char *name, const char *source, 75 CeedInt vec_length, CeedQFunctionUser f, 76 int (*init)(Ceed, const char *, CeedQFunction)) { 77 if (num_qfunctions >= sizeof(gallery_qfunctions) / sizeof( 78 gallery_qfunctions[0])) 79 // LCOV_EXCL_START 80 return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions"); 81 // LCOV_EXCL_STOP 82 83 strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN); 84 gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0; 85 strncpy(gallery_qfunctions[num_qfunctions].source, source, 86 CEED_MAX_RESOURCE_LEN); 87 gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0; 88 gallery_qfunctions[num_qfunctions].vec_length = vec_length; 89 gallery_qfunctions[num_qfunctions].f = f; 90 gallery_qfunctions[num_qfunctions].init = init; 91 num_qfunctions++; 92 return CEED_ERROR_SUCCESS; 93 } 94 95 /** 96 @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output 97 98 @param f CeedQFunctionField 99 @param field_name Name of QFunction field 100 @param size Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or 101 (num_comp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT 102 @param eval_mode \ref CEED_EVAL_NONE to use values directly, 103 \ref CEED_EVAL_INTERP to use interpolated values, 104 \ref CEED_EVAL_GRAD to use gradients, 105 \ref CEED_EVAL_WEIGHT to use quadrature weights. 106 107 @return An error code: 0 - success, otherwise - failure 108 109 @ref Developer 110 **/ 111 static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *field_name, 112 CeedInt size, CeedEvalMode eval_mode) { 113 size_t len = strlen(field_name); 114 char *tmp; 115 int ierr; 116 117 ierr = CeedCalloc(1, f); CeedChk(ierr); 118 ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr); 119 memcpy(tmp, field_name, len+1); 120 (*f)->field_name = tmp; 121 (*f)->size = size; 122 (*f)->eval_mode = eval_mode; 123 return CEED_ERROR_SUCCESS; 124 } 125 126 /** 127 @brief View a field of a CeedQFunction 128 129 @param[in] field QFunction field to view 130 @param[in] field_number Number of field being viewed 131 @param[in] in true for input field, false for output 132 @param[in] stream Stream to view to, e.g., stdout 133 134 @return An error code: 0 - success, otherwise - failure 135 136 @ref Utility 137 **/ 138 static int CeedQFunctionFieldView(CeedQFunctionField field, 139 CeedInt field_number, 140 bool in, FILE *stream) { 141 int ierr; 142 const char *inout = in ? "Input" : "Output"; 143 char *field_name; 144 ierr = CeedQFunctionFieldGetName(field, &field_name); CeedChk(ierr); 145 CeedInt size; 146 ierr = CeedQFunctionFieldGetSize(field, &size); CeedChk(ierr); 147 CeedEvalMode eval_mode; 148 ierr = CeedQFunctionFieldGetEvalMode(field, &eval_mode); CeedChk(ierr); 149 fprintf(stream, " %s Field [%d]:\n" 150 " Name: \"%s\"\n" 151 " Size: %d\n" 152 " EvalMode: \"%s\"\n", 153 inout, field_number, field_name, size, CeedEvalModes[eval_mode]); 154 return CEED_ERROR_SUCCESS; 155 } 156 157 /** 158 @brief Set flag to determine if Fortran interface is used 159 160 @param qf CeedQFunction 161 @param status Boolean value to set as Fortran status 162 163 @return An error code: 0 - success, otherwise - failure 164 165 @ref Backend 166 **/ 167 int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) { 168 qf->fortran_status = status; 169 return CEED_ERROR_SUCCESS; 170 } 171 172 /// @} 173 174 /// ---------------------------------------------------------------------------- 175 /// CeedQFunction Backend API 176 /// ---------------------------------------------------------------------------- 177 /// @addtogroup CeedQFunctionBackend 178 /// @{ 179 180 /** 181 @brief Get the Ceed associated with a CeedQFunction 182 183 @param qf CeedQFunction 184 @param[out] ceed Variable to store Ceed 185 186 @return An error code: 0 - success, otherwise - failure 187 188 @ref Backend 189 **/ 190 int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) { 191 *ceed = qf->ceed; 192 return CEED_ERROR_SUCCESS; 193 } 194 195 /** 196 @brief Get the vector length of a CeedQFunction 197 198 @param qf CeedQFunction 199 @param[out] vec_length Variable to store vector length 200 201 @return An error code: 0 - success, otherwise - failure 202 203 @ref Backend 204 **/ 205 int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) { 206 *vec_length = qf->vec_length; 207 return CEED_ERROR_SUCCESS; 208 } 209 210 /** 211 @brief Get the number of inputs and outputs to a CeedQFunction 212 213 @param qf CeedQFunction 214 @param[out] num_input Variable to store number of input fields 215 @param[out] num_output Variable to store number of output fields 216 217 @return An error code: 0 - success, otherwise - failure 218 219 @ref Backend 220 **/ 221 int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, 222 CeedInt *num_output) { 223 if (num_input) *num_input = qf->num_input_fields; 224 if (num_output) *num_output = qf->num_output_fields; 225 return CEED_ERROR_SUCCESS; 226 } 227 228 /** 229 @brief Get the source path string for a CeedQFunction 230 231 @param qf CeedQFunction 232 @param[out] source Variable to store source path string 233 234 @return An error code: 0 - success, otherwise - failure 235 236 @ref Backend 237 **/ 238 int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source) { 239 *source = (char *) qf->source_path; 240 return CEED_ERROR_SUCCESS; 241 } 242 243 /** 244 @brief Get the User Function for a CeedQFunction 245 246 @param qf CeedQFunction 247 @param[out] f Variable to store user function 248 249 @return An error code: 0 - success, otherwise - failure 250 251 @ref Backend 252 **/ 253 int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) { 254 *f = qf->function; 255 return CEED_ERROR_SUCCESS; 256 } 257 258 /** 259 @brief Get global context for a CeedQFunction. 260 Note: For QFunctions from the Fortran interface, this 261 function will return the Fortran context 262 CeedQFunctionContext. 263 264 @param qf CeedQFunction 265 @param[out] ctx Variable to store CeedQFunctionContext 266 267 @return An error code: 0 - success, otherwise - failure 268 269 @ref Backend 270 **/ 271 int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) { 272 *ctx = qf->ctx; 273 return CEED_ERROR_SUCCESS; 274 } 275 276 /** 277 @brief Get true user context for a CeedQFunction 278 Note: For all QFunctions this function will return the user 279 CeedQFunctionContext and not interface context 280 CeedQFunctionContext, if any such object exists. 281 282 @param qf CeedQFunction 283 @param[out] ctx Variable to store CeedQFunctionContext 284 285 @return An error code: 0 - success, otherwise - failure 286 @ref Backend 287 **/ 288 int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) { 289 int ierr; 290 if (qf->fortran_status) { 291 CeedFortranContext fortran_ctx = NULL; 292 ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx); 293 CeedChk(ierr); 294 *ctx = fortran_ctx->innerctx; 295 ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx); 296 CeedChk(ierr); 297 } else { 298 *ctx = qf->ctx; 299 } 300 return CEED_ERROR_SUCCESS; 301 } 302 303 /** 304 @brief Determine if QFunction is identity 305 306 @param qf CeedQFunction 307 @param[out] is_identity Variable to store identity status 308 309 @return An error code: 0 - success, otherwise - failure 310 311 @ref Backend 312 **/ 313 int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) { 314 *is_identity = qf->identity; 315 return CEED_ERROR_SUCCESS; 316 } 317 318 /** 319 @brief Get backend data of a CeedQFunction 320 321 @param qf CeedQFunction 322 @param[out] data Variable to store data 323 324 @return An error code: 0 - success, otherwise - failure 325 326 @ref Backend 327 **/ 328 int CeedQFunctionGetData(CeedQFunction qf, void *data) { 329 *(void **)data = qf->data; 330 return CEED_ERROR_SUCCESS; 331 } 332 333 /** 334 @brief Set backend data of a CeedQFunction 335 336 @param[out] qf CeedQFunction 337 @param data Data to set 338 339 @return An error code: 0 - success, otherwise - failure 340 341 @ref Backend 342 **/ 343 int CeedQFunctionSetData(CeedQFunction qf, void *data) { 344 qf->data = data; 345 return CEED_ERROR_SUCCESS; 346 } 347 348 /** 349 @brief Increment the reference counter for a CeedQFunction 350 351 @param qf CeedQFunction to increment the reference counter 352 353 @return An error code: 0 - success, otherwise - failure 354 355 @ref Backend 356 **/ 357 int CeedQFunctionReference(CeedQFunction qf) { 358 qf->ref_count++; 359 return CEED_ERROR_SUCCESS; 360 } 361 362 /** 363 @brief Get the CeedQFunctionFields of a CeedQFunction 364 365 @param qf CeedQFunction 366 @param[out] input_fields Variable to store input_fields 367 @param[out] output_fields Variable to store output_fields 368 369 @return An error code: 0 - success, otherwise - failure 370 371 @ref Backend 372 **/ 373 int CeedQFunctionGetFields(CeedQFunction qf, CeedQFunctionField **input_fields, 374 CeedQFunctionField **output_fields) { 375 if (input_fields) *input_fields = qf->input_fields; 376 if (output_fields) *output_fields = qf->output_fields; 377 return CEED_ERROR_SUCCESS; 378 } 379 380 /** 381 @brief Get the name of a CeedQFunctionField 382 383 @param qf_field CeedQFunctionField 384 @param[out] field_name Variable to store the field name 385 386 @return An error code: 0 - success, otherwise - failure 387 388 @ref Backend 389 **/ 390 int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) { 391 *field_name = (char *)qf_field->field_name; 392 return CEED_ERROR_SUCCESS; 393 } 394 395 /** 396 @brief Get the number of components of a CeedQFunctionField 397 398 @param qf_field CeedQFunctionField 399 @param[out] size Variable to store the size of the field 400 401 @return An error code: 0 - success, otherwise - failure 402 403 @ref Backend 404 **/ 405 int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) { 406 *size = qf_field->size; 407 return CEED_ERROR_SUCCESS; 408 } 409 410 /** 411 @brief Get the CeedEvalMode of a CeedQFunctionField 412 413 @param qf_field CeedQFunctionField 414 @param[out] eval_mode Variable to store the field evaluation mode 415 416 @return An error code: 0 - success, otherwise - failure 417 418 @ref Backend 419 **/ 420 int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, 421 CeedEvalMode *eval_mode) { 422 *eval_mode = qf_field->eval_mode; 423 return CEED_ERROR_SUCCESS; 424 } 425 426 /// @} 427 428 /// ---------------------------------------------------------------------------- 429 /// CeedQFunction Public API 430 /// ---------------------------------------------------------------------------- 431 /// @addtogroup CeedQFunctionUser 432 /// @{ 433 434 /** 435 @brief Create a CeedQFunction for evaluating interior (volumetric) terms. 436 437 @param ceed A Ceed object where the CeedQFunction will be created 438 @param vec_length Vector length. Caller must ensure that number of quadrature 439 points is a multiple of vec_length. 440 @param f Function pointer to evaluate action at quadrature points. 441 See \ref CeedQFunctionUser. 442 @param source Absolute path to source of QFunction, 443 "\abs_path\file.h:function_name". 444 For support across all backends, this source must only 445 contain constructs supported by C99, C++11, and CUDA. 446 @param[out] qf Address of the variable where the newly created 447 CeedQFunction will be stored 448 449 @return An error code: 0 - success, otherwise - failure 450 451 See \ref CeedQFunctionUser for details on the call-back function @a f's 452 arguments. 453 454 @ref User 455 **/ 456 int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length, 457 CeedQFunctionUser f, 458 const char *source, CeedQFunction *qf) { 459 int ierr; 460 char *source_copy; 461 462 if (!ceed->QFunctionCreate) { 463 Ceed delegate; 464 ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr); 465 466 if (!delegate) 467 // LCOV_EXCL_START 468 return CeedError(ceed, CEED_ERROR_UNSUPPORTED, 469 "Backend does not support QFunctionCreate"); 470 // LCOV_EXCL_STOP 471 472 ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf); 473 CeedChk(ierr); 474 return CEED_ERROR_SUCCESS; 475 } 476 477 ierr = CeedCalloc(1, qf); CeedChk(ierr); 478 (*qf)->ceed = ceed; 479 ierr = CeedReference(ceed); CeedChk(ierr); 480 (*qf)->ref_count = 1; 481 (*qf)->vec_length = vec_length; 482 (*qf)->identity = 0; 483 (*qf)->function = f; 484 size_t slen = strlen(source) + 1; 485 ierr = CeedMalloc(slen, &source_copy); CeedChk(ierr); 486 memcpy(source_copy, source, slen); 487 (*qf)->source_path = source_copy; 488 ierr = CeedCalloc(16, &(*qf)->input_fields); CeedChk(ierr); 489 ierr = CeedCalloc(16, &(*qf)->output_fields); CeedChk(ierr); 490 ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr); 491 return CEED_ERROR_SUCCESS; 492 } 493 494 /** 495 @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name. 496 497 @param ceed A Ceed object where the CeedQFunction will be created 498 @param name Name of QFunction to use from gallery 499 @param[out] qf Address of the variable where the newly created 500 CeedQFunction will be stored 501 502 @return An error code: 0 - success, otherwise - failure 503 504 @ref User 505 **/ 506 int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, 507 CeedQFunction *qf) { 508 int ierr; 509 size_t match_len = 0, match_idx = UINT_MAX; 510 char *name_copy; 511 512 ierr = CeedQFunctionRegisterAll(); CeedChk(ierr); 513 // Find matching backend 514 if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE, 515 "No QFunction name provided"); 516 for (size_t i=0; i<num_qfunctions; i++) { 517 size_t n; 518 const char *curr_name = gallery_qfunctions[i].name; 519 for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {} 520 if (n > match_len) { 521 match_len = n; 522 match_idx = i; 523 } 524 } 525 if (!match_len) 526 // LCOV_EXCL_START 527 return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction"); 528 // LCOV_EXCL_STOP 529 530 // Create QFunction 531 ierr = CeedQFunctionCreateInterior(ceed, 532 gallery_qfunctions[match_idx].vec_length, 533 gallery_qfunctions[match_idx].f, 534 gallery_qfunctions[match_idx].source, qf); 535 CeedChk(ierr); 536 537 // QFunction specific setup 538 ierr = gallery_qfunctions[match_idx].init(ceed, name, *qf); CeedChk(ierr); 539 540 // Copy name 541 size_t slen = strlen(name) + 1; 542 ierr = CeedMalloc(slen, &name_copy); CeedChk(ierr); 543 memcpy(name_copy, name, slen); 544 (*qf)->qf_name = name_copy; 545 return CEED_ERROR_SUCCESS; 546 } 547 548 /** 549 @brief Create an identity CeedQFunction. Inputs are written into outputs in 550 the order given. This is useful for CeedOperators that can be 551 represented with only the action of a CeedRestriction and CeedBasis, 552 such as restriction and prolongation operators for p-multigrid. 553 Backends may optimize CeedOperators with this CeedQFunction to avoid 554 the copy of input data to output fields by using the same memory 555 location for both. 556 557 @param ceed A Ceed object where the CeedQFunction will be created 558 @param[in] size Size of the QFunction fields 559 @param[in] in_mode CeedEvalMode for input to CeedQFunction 560 @param[in] out_mode CeedEvalMode for output to CeedQFunction 561 @param[out] qf Address of the variable where the newly created 562 CeedQFunction will be stored 563 564 @return An error code: 0 - success, otherwise - failure 565 566 @ref User 567 **/ 568 int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, 569 CeedEvalMode out_mode, CeedQFunction *qf) { 570 int ierr; 571 572 if (in_mode == CEED_EVAL_NONE && out_mode == CEED_EVAL_NONE) 573 // LCOV_EXCL_START 574 return CeedError(ceed, CEED_ERROR_UNSUPPORTED, 575 "CEED_EVAL_NONE for a both the input and " 576 "output does not make sense with an identity QFunction"); 577 // LCOV_EXCL_STOP 578 579 ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr); 580 ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr); 581 ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr); 582 583 (*qf)->identity = 1; 584 CeedInt *size_data; 585 ierr = CeedCalloc(1, &size_data); CeedChk(ierr); 586 size_data[0] = size; 587 CeedQFunctionContext ctx; 588 ierr = CeedQFunctionContextCreate(ceed, &ctx); CeedChk(ierr); 589 ierr = CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_OWN_POINTER, 590 sizeof(*size_data), (void *)size_data); 591 CeedChk(ierr); 592 ierr = CeedQFunctionSetContext(*qf, ctx); CeedChk(ierr); 593 ierr = CeedQFunctionContextDestroy(&ctx); CeedChk(ierr); 594 return CEED_ERROR_SUCCESS; 595 } 596 597 /** 598 @brief Copy the pointer to a CeedQFunction. Both pointers should 599 be destroyed with `CeedQFunctionDestroy()`; 600 Note: If `*qf_copy` is non-NULL, then it is assumed that 601 `*qf_copy` is a pointer to a CeedQFunction. This 602 CeedQFunction will be destroyed if `*qf_copy` is the only 603 reference to this CeedQFunction. 604 605 @param qf CeedQFunction to copy reference to 606 @param[out] qf_copy Variable to store copied reference 607 608 @return An error code: 0 - success, otherwise - failure 609 610 @ref User 611 **/ 612 int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) { 613 int ierr; 614 615 ierr = CeedQFunctionReference(qf); CeedChk(ierr); 616 ierr = CeedQFunctionDestroy(qf_copy); CeedChk(ierr); 617 *qf_copy = qf; 618 return CEED_ERROR_SUCCESS; 619 } 620 621 /** 622 @brief Add a CeedQFunction input 623 624 @param qf CeedQFunction 625 @param field_name Name of QFunction field 626 @param size Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or 627 (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP 628 @param eval_mode \ref CEED_EVAL_NONE to use values directly, 629 \ref CEED_EVAL_INTERP to use interpolated values, 630 \ref CEED_EVAL_GRAD to use gradients. 631 632 @return An error code: 0 - success, otherwise - failure 633 634 @ref User 635 **/ 636 int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, 637 CeedInt size, 638 CeedEvalMode eval_mode) { 639 if (qf->operators_set) 640 // LCOV_EXCL_START 641 return CeedError(qf->ceed, CEED_ERROR_MAJOR, 642 "QFunction cannot be changed when in use by an operator"); 643 // LCOV_EXCL_STOP 644 if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1)) 645 // LCOV_EXCL_START 646 return CeedError(qf->ceed, CEED_ERROR_DIMENSION, 647 "CEED_EVAL_WEIGHT should have size 1"); 648 // LCOV_EXCL_STOP 649 int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], 650 field_name, size, eval_mode); 651 CeedChk(ierr); 652 qf->num_input_fields++; 653 return CEED_ERROR_SUCCESS; 654 } 655 656 /** 657 @brief Add a CeedQFunction output 658 659 @param qf CeedQFunction 660 @param field_name Name of QFunction field 661 @param size Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or 662 (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP 663 @param eval_mode \ref CEED_EVAL_NONE to use values directly, 664 \ref CEED_EVAL_INTERP to use interpolated values, 665 \ref CEED_EVAL_GRAD to use gradients. 666 667 @return An error code: 0 - success, otherwise - failure 668 669 @ref User 670 **/ 671 int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, 672 CeedInt size, CeedEvalMode eval_mode) { 673 if (qf->operators_set) 674 // LCOV_EXCL_START 675 return CeedError(qf->ceed, CEED_ERROR_MAJOR, 676 "QFunction cannot be changed when in use by an operator"); 677 // LCOV_EXCL_STOP 678 if (eval_mode == CEED_EVAL_WEIGHT) 679 // LCOV_EXCL_START 680 return CeedError(qf->ceed, CEED_ERROR_DIMENSION, 681 "Cannot create QFunction output with " 682 "CEED_EVAL_WEIGHT"); 683 // LCOV_EXCL_STOP 684 int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], 685 field_name, size, eval_mode); 686 CeedChk(ierr); 687 qf->num_output_fields++; 688 return CEED_ERROR_SUCCESS; 689 } 690 691 /** 692 @brief Set global context for a CeedQFunction 693 694 @param qf CeedQFunction 695 @param ctx Context data to set 696 697 @return An error code: 0 - success, otherwise - failure 698 699 @ref User 700 **/ 701 int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) { 702 int ierr; 703 qf->ctx = ctx; 704 ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr); 705 return CEED_ERROR_SUCCESS; 706 } 707 708 /** 709 @brief View a CeedQFunction 710 711 @param[in] qf CeedQFunction to view 712 @param[in] stream Stream to write; typically stdout/stderr or a file 713 714 @return Error code: 0 - success, otherwise - failure 715 716 @ref User 717 **/ 718 int CeedQFunctionView(CeedQFunction qf, FILE *stream) { 719 int ierr; 720 721 fprintf(stream, "%sCeedQFunction %s\n", 722 qf->qf_name ? "Gallery " : "User ", qf->qf_name ? qf->qf_name : ""); 723 724 fprintf(stream, " %d Input Field%s:\n", qf->num_input_fields, 725 qf->num_input_fields>1 ? "s" : ""); 726 for (CeedInt i=0; i<qf->num_input_fields; i++) { 727 ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream); 728 CeedChk(ierr); 729 } 730 731 fprintf(stream, " %d Output Field%s:\n", qf->num_output_fields, 732 qf->num_output_fields>1 ? "s" : ""); 733 for (CeedInt i=0; i<qf->num_output_fields; i++) { 734 ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream); 735 CeedChk(ierr); 736 } 737 return CEED_ERROR_SUCCESS; 738 } 739 740 /** 741 @brief Apply the action of a CeedQFunction 742 743 @param qf CeedQFunction 744 @param Q Number of quadrature points 745 @param[in] u Array of input CeedVectors 746 @param[out] v Array of output CeedVectors 747 748 @return An error code: 0 - success, otherwise - failure 749 750 @ref User 751 **/ 752 int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, 753 CeedVector *u, CeedVector *v) { 754 int ierr; 755 if (!qf->Apply) 756 // LCOV_EXCL_START 757 return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED, 758 "Backend does not support QFunctionApply"); 759 // LCOV_EXCL_STOP 760 if (Q % qf->vec_length) 761 // LCOV_EXCL_START 762 return CeedError(qf->ceed, CEED_ERROR_DIMENSION, 763 "Number of quadrature points %d must be a " 764 "multiple of %d", Q, qf->vec_length); 765 // LCOV_EXCL_STOP 766 ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr); 767 return CEED_ERROR_SUCCESS; 768 } 769 770 /** 771 @brief Destroy a CeedQFunction 772 773 @param qf CeedQFunction to destroy 774 775 @return An error code: 0 - success, otherwise - failure 776 777 @ref User 778 **/ 779 int CeedQFunctionDestroy(CeedQFunction *qf) { 780 int ierr; 781 782 if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS; 783 // Backend destroy 784 if ((*qf)->Destroy) { 785 ierr = (*qf)->Destroy(*qf); CeedChk(ierr); 786 } 787 // Free fields 788 for (int i=0; i<(*qf)->num_input_fields; i++) { 789 ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr); 790 ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr); 791 } 792 for (int i=0; i<(*qf)->num_output_fields; i++) { 793 ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr); 794 ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr); 795 } 796 ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr); 797 ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr); 798 799 // User context data object 800 ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr); 801 802 ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr); 803 ierr = CeedFree(&(*qf)->qf_name); CeedChk(ierr); 804 ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr); 805 ierr = CeedFree(qf); CeedChk(ierr); 806 return CEED_ERROR_SUCCESS; 807 } 808 809 /// @} 810