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.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 vlength; 45 CeedQFunctionUser f; 46 int (*init)(Ceed ceed, const char *name, CeedQFunction qf); 47 } 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 vlength Vector length. Caller must ensure that number of quadrature 64 points is a multiple of vlength. 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 vlength, CeedQFunctionUser f, 76 int (*init)(Ceed, const char *, CeedQFunction)) { 77 if (num_qfunctions >= sizeof(qfunctions) / sizeof(qfunctions[0])) 78 // LCOV_EXCL_START 79 return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions"); 80 // LCOV_EXCL_STOP 81 82 strncpy(qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN); 83 qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0; 84 strncpy(qfunctions[num_qfunctions].source, source, CEED_MAX_RESOURCE_LEN); 85 qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0; 86 qfunctions[num_qfunctions].vlength = vlength; 87 qfunctions[num_qfunctions].f = f; 88 qfunctions[num_qfunctions].init = init; 89 num_qfunctions++; 90 return CEED_ERROR_SUCCESS; 91 } 92 93 /** 94 @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output 95 96 @param f CeedQFunctionField 97 @param fieldname Name of QFunction field 98 @param size Size of QFunction field, (ncomp * dim) for @ref CEED_EVAL_GRAD or 99 (ncomp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT 100 @param emode \ref CEED_EVAL_NONE to use values directly, 101 \ref CEED_EVAL_INTERP to use interpolated values, 102 \ref CEED_EVAL_GRAD to use gradients, 103 \ref CEED_EVAL_WEIGHT to use quadrature weights. 104 105 @return An error code: 0 - success, otherwise - failure 106 107 @ref Developer 108 **/ 109 static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *fieldname, 110 CeedInt size, CeedEvalMode emode) { 111 size_t len = strlen(fieldname); 112 char *tmp; 113 int ierr; 114 115 ierr = CeedCalloc(1, f); CeedChk(ierr); 116 ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr); 117 memcpy(tmp, fieldname, len+1); 118 (*f)->fieldname = tmp; 119 (*f)->size = size; 120 (*f)->emode = emode; 121 return CEED_ERROR_SUCCESS; 122 } 123 124 /** 125 @brief View a field of a CeedQFunction 126 127 @param[in] field QFunction field to view 128 @param[in] fieldnumber Number of field being viewed 129 @param[in] in true for input field, false for output 130 @param[in] stream Stream to view to, e.g., stdout 131 132 @return An error code: 0 - success, otherwise - failure 133 134 @ref Utility 135 **/ 136 static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt fieldnumber, 137 bool in, FILE *stream) { 138 const char *inout = in ? "Input" : "Output"; 139 fprintf(stream, " %s Field [%d]:\n" 140 " Name: \"%s\"\n" 141 " Size: %d\n" 142 " EvalMode: \"%s\"\n", 143 inout, fieldnumber, field->fieldname, field->size, 144 CeedEvalModes[field->emode]); 145 return CEED_ERROR_SUCCESS; 146 } 147 148 149 /** 150 @brief Set flag to determine if Fortran interface is used 151 152 @param qf CeedQFunction 153 @param status Boolean value to set as Fortran status 154 155 @return An error code: 0 - success, otherwise - failure 156 157 @ref Backend 158 **/ 159 int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) { 160 qf->fortranstatus = status; 161 return CEED_ERROR_SUCCESS; 162 } 163 164 /// @} 165 166 /// ---------------------------------------------------------------------------- 167 /// CeedQFunction Backend API 168 /// ---------------------------------------------------------------------------- 169 /// @addtogroup CeedQFunctionBackend 170 /// @{ 171 172 /** 173 @brief Get the Ceed associated with a CeedQFunction 174 175 @param qf CeedQFunction 176 @param[out] ceed Variable to store Ceed 177 178 @return An error code: 0 - success, otherwise - failure 179 180 @ref Backend 181 **/ 182 int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) { 183 *ceed = qf->ceed; 184 return CEED_ERROR_SUCCESS; 185 } 186 187 /** 188 @brief Get the vector length of a CeedQFunction 189 190 @param qf CeedQFunction 191 @param[out] vlength Variable to store vector length 192 193 @return An error code: 0 - success, otherwise - failure 194 195 @ref Backend 196 **/ 197 int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength) { 198 *vlength = qf->vlength; 199 return CEED_ERROR_SUCCESS; 200 } 201 202 /** 203 @brief Get the number of inputs and outputs to a CeedQFunction 204 205 @param qf CeedQFunction 206 @param[out] numinput Variable to store number of input fields 207 @param[out] numoutput Variable to store number of output fields 208 209 @return An error code: 0 - success, otherwise - failure 210 211 @ref Backend 212 **/ 213 int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput, 214 CeedInt *numoutput) { 215 if (numinput) *numinput = qf->numinputfields; 216 if (numoutput) *numoutput = qf->numoutputfields; 217 return CEED_ERROR_SUCCESS; 218 } 219 220 /** 221 @brief Get the source path string for a CeedQFunction 222 223 @param qf CeedQFunction 224 @param[out] source Variable to store source path string 225 226 @return An error code: 0 - success, otherwise - failure 227 228 @ref Backend 229 **/ 230 int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source) { 231 *source = (char *) qf->sourcepath; 232 return CEED_ERROR_SUCCESS; 233 } 234 235 /** 236 @brief Get the User Function for a CeedQFunction 237 238 @param qf CeedQFunction 239 @param[out] f Variable to store user function 240 241 @return An error code: 0 - success, otherwise - failure 242 243 @ref Backend 244 **/ 245 int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) { 246 *f = qf->function; 247 return CEED_ERROR_SUCCESS; 248 } 249 250 /** 251 @brief Get global context for a CeedQFunction. 252 Note: For QFunctions from the Fortran interface, this 253 function will return the Fortran context 254 CeedQFunctionContext. 255 256 @param qf CeedQFunction 257 @param[out] ctx Variable to store CeedQFunctionContext 258 259 @return An error code: 0 - success, otherwise - failure 260 261 @ref Backend 262 **/ 263 int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) { 264 *ctx = qf->ctx; 265 return CEED_ERROR_SUCCESS; 266 } 267 268 /** 269 @brief Get true user context for a CeedQFunction 270 Note: For all QFunctions this function will return the user 271 CeedQFunctionContext and not interface context 272 CeedQFunctionContext, if any such object exists. 273 274 @param qf CeedQFunction 275 @param[out] ctx Variable to store CeedQFunctionContext 276 277 @return An error code: 0 - success, otherwise - failure 278 @ref Backend 279 **/ 280 int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) { 281 int ierr; 282 if (qf->fortranstatus) { 283 CeedFortranContext fctx = NULL; 284 ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fctx); 285 CeedChk(ierr); 286 *ctx = fctx->innerctx; 287 ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fctx); CeedChk(ierr); 288 } else { 289 *ctx = qf->ctx; 290 } 291 return CEED_ERROR_SUCCESS; 292 } 293 294 /** 295 @brief Determine if QFunction is identity 296 297 @param qf CeedQFunction 298 @param[out] isidentity Variable to store identity status 299 300 @return An error code: 0 - success, otherwise - failure 301 302 @ref Backend 303 **/ 304 int CeedQFunctionIsIdentity(CeedQFunction qf, bool *isidentity) { 305 *isidentity = qf->identity; 306 return CEED_ERROR_SUCCESS; 307 } 308 309 /** 310 @brief Get backend data of a CeedQFunction 311 312 @param qf CeedQFunction 313 @param[out] data Variable to store data 314 315 @return An error code: 0 - success, otherwise - failure 316 317 @ref Backend 318 **/ 319 int CeedQFunctionGetData(CeedQFunction qf, void *data) { 320 *(void **)data = qf->data; 321 return CEED_ERROR_SUCCESS; 322 } 323 324 /** 325 @brief Set backend data of a CeedQFunction 326 327 @param[out] qf CeedQFunction 328 @param data Data to set 329 330 @return An error code: 0 - success, otherwise - failure 331 332 @ref Backend 333 **/ 334 int CeedQFunctionSetData(CeedQFunction qf, void *data) { 335 qf->data = data; 336 return CEED_ERROR_SUCCESS; 337 } 338 339 /** 340 @brief Get the CeedQFunctionFields of a CeedQFunction 341 342 @param qf CeedQFunction 343 @param[out] inputfields Variable to store inputfields 344 @param[out] outputfields Variable to store outputfields 345 346 @return An error code: 0 - success, otherwise - failure 347 348 @ref Backend 349 **/ 350 int CeedQFunctionGetFields(CeedQFunction qf, CeedQFunctionField **inputfields, 351 CeedQFunctionField **outputfields) { 352 if (inputfields) *inputfields = qf->inputfields; 353 if (outputfields) *outputfields = qf->outputfields; 354 return CEED_ERROR_SUCCESS; 355 } 356 357 /** 358 @brief Get the name of a CeedQFunctionField 359 360 @param qffield CeedQFunctionField 361 @param[out] fieldname Variable to store the field name 362 363 @return An error code: 0 - success, otherwise - failure 364 365 @ref Backend 366 **/ 367 int CeedQFunctionFieldGetName(CeedQFunctionField qffield, char **fieldname) { 368 *fieldname = (char *)qffield->fieldname; 369 return CEED_ERROR_SUCCESS; 370 } 371 372 /** 373 @brief Get the number of components of a CeedQFunctionField 374 375 @param qffield CeedQFunctionField 376 @param[out] size Variable to store the size of the field 377 378 @return An error code: 0 - success, otherwise - failure 379 380 @ref Backend 381 **/ 382 int CeedQFunctionFieldGetSize(CeedQFunctionField qffield, CeedInt *size) { 383 *size = qffield->size; 384 return CEED_ERROR_SUCCESS; 385 } 386 387 /** 388 @brief Get the CeedEvalMode of a CeedQFunctionField 389 390 @param qffield CeedQFunctionField 391 @param[out] emode Variable to store the field evaluation mode 392 393 @return An error code: 0 - success, otherwise - failure 394 395 @ref Backend 396 **/ 397 int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qffield, 398 CeedEvalMode *emode) { 399 *emode = qffield->emode; 400 return CEED_ERROR_SUCCESS; 401 } 402 403 /// @} 404 405 /// ---------------------------------------------------------------------------- 406 /// CeedQFunction Public API 407 /// ---------------------------------------------------------------------------- 408 /// @addtogroup CeedQFunctionUser 409 /// @{ 410 411 /** 412 @brief Create a CeedQFunction for evaluating interior (volumetric) terms. 413 414 @param ceed A Ceed object where the CeedQFunction will be created 415 @param vlength Vector length. Caller must ensure that number of quadrature 416 points is a multiple of vlength. 417 @param f Function pointer to evaluate action at quadrature points. 418 See \ref CeedQFunctionUser. 419 @param source Absolute path to source of QFunction, 420 "\abs_path\file.h:function_name". 421 For support across all backends, this source must only 422 contain constructs supported by C99, C++11, and CUDA. 423 @param[out] qf Address of the variable where the newly created 424 CeedQFunction will be stored 425 426 @return An error code: 0 - success, otherwise - failure 427 428 See \ref CeedQFunctionUser for details on the call-back function @a f's 429 arguments. 430 431 @ref User 432 **/ 433 int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength, CeedQFunctionUser f, 434 const char *source, CeedQFunction *qf) { 435 int ierr; 436 char *source_copy; 437 438 if (!ceed->QFunctionCreate) { 439 Ceed delegate; 440 ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr); 441 442 if (!delegate) 443 // LCOV_EXCL_START 444 return CeedError(ceed, CEED_ERROR_UNSUPPORTED, 445 "Backend does not support QFunctionCreate"); 446 // LCOV_EXCL_STOP 447 448 ierr = CeedQFunctionCreateInterior(delegate, vlength, f, source, qf); 449 CeedChk(ierr); 450 return CEED_ERROR_SUCCESS; 451 } 452 453 ierr = CeedCalloc(1, qf); CeedChk(ierr); 454 (*qf)->ceed = ceed; 455 ceed->refcount++; 456 (*qf)->refcount = 1; 457 (*qf)->vlength = vlength; 458 (*qf)->identity = 0; 459 (*qf)->function = f; 460 size_t slen = strlen(source) + 1; 461 ierr = CeedMalloc(slen, &source_copy); CeedChk(ierr); 462 memcpy(source_copy, source, slen); 463 (*qf)->sourcepath = source_copy; 464 ierr = CeedCalloc(16, &(*qf)->inputfields); CeedChk(ierr); 465 ierr = CeedCalloc(16, &(*qf)->outputfields); CeedChk(ierr); 466 ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr); 467 return CEED_ERROR_SUCCESS; 468 } 469 470 /** 471 @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name. 472 473 @param ceed A Ceed object where the CeedQFunction will be created 474 @param name Name of QFunction to use from gallery 475 @param[out] qf Address of the variable where the newly created 476 CeedQFunction will be stored 477 478 @return An error code: 0 - success, otherwise - failure 479 480 @ref User 481 **/ 482 int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, 483 CeedQFunction *qf) { 484 int ierr; 485 size_t matchlen = 0, matchidx = UINT_MAX; 486 char *name_copy; 487 488 ierr = CeedQFunctionRegisterAll(); CeedChk(ierr); 489 // Find matching backend 490 if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE, 491 "No QFunction name provided"); 492 for (size_t i=0; i<num_qfunctions; i++) { 493 size_t n; 494 const char *currname = qfunctions[i].name; 495 for (n = 0; currname[n] && currname[n] == name[n]; n++) {} 496 if (n > matchlen) { 497 matchlen = n; 498 matchidx = i; 499 } 500 } 501 if (!matchlen) 502 // LCOV_EXCL_START 503 return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction"); 504 // LCOV_EXCL_STOP 505 506 // Create QFunction 507 ierr = CeedQFunctionCreateInterior(ceed, qfunctions[matchidx].vlength, 508 qfunctions[matchidx].f, 509 qfunctions[matchidx].source, qf); 510 CeedChk(ierr); 511 512 // QFunction specific setup 513 ierr = qfunctions[matchidx].init(ceed, name, *qf); CeedChk(ierr); 514 515 // Copy name 516 size_t slen = strlen(name) + 1; 517 ierr = CeedMalloc(slen, &name_copy); CeedChk(ierr); 518 memcpy(name_copy, name, slen); 519 (*qf)->qfname = name_copy; 520 return CEED_ERROR_SUCCESS; 521 } 522 523 /** 524 @brief Create an identity CeedQFunction. Inputs are written into outputs in 525 the order given. This is useful for CeedOperators that can be 526 represented with only the action of a CeedRestriction and CeedBasis, 527 such as restriction and prolongation operators for p-multigrid. 528 Backends may optimize CeedOperators with this CeedQFunction to avoid 529 the copy of input data to output fields by using the same memory 530 location for both. 531 532 @param ceed A Ceed object where the CeedQFunction will be created 533 @param[in] size Size of the qfunction fields 534 @param[in] inmode CeedEvalMode for input to CeedQFunction 535 @param[in] outmode CeedEvalMode for output to CeedQFunction 536 @param[out] qf Address of the variable where the newly created 537 CeedQFunction will be stored 538 539 @return An error code: 0 - success, otherwise - failure 540 541 @ref User 542 **/ 543 int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode inmode, 544 CeedEvalMode outmode, CeedQFunction *qf) { 545 int ierr; 546 547 if (inmode == CEED_EVAL_NONE && outmode == CEED_EVAL_NONE) 548 // LCOV_EXCL_START 549 return CeedError(ceed, CEED_ERROR_UNSUPPORTED, 550 "CEED_EVAL_NONE for a both the input and " 551 "output does not make sense with an identity QFunction"); 552 // LCOV_EXCL_STOP 553 554 ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr); 555 ierr = CeedQFunctionAddInput(*qf, "input", size, inmode); CeedChk(ierr); 556 ierr = CeedQFunctionAddOutput(*qf, "output", size, outmode); CeedChk(ierr); 557 558 (*qf)->identity = 1; 559 CeedInt *sizeData; 560 ierr = CeedCalloc(1, &sizeData); CeedChk(ierr); 561 sizeData[0] = size; 562 CeedQFunctionContext ctx; 563 ierr = CeedQFunctionContextCreate(ceed, &ctx); CeedChk(ierr); 564 ierr = CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_OWN_POINTER, 565 sizeof(*sizeData), (void *)sizeData); 566 CeedChk(ierr); 567 ierr = CeedQFunctionSetContext(*qf, ctx); CeedChk(ierr); 568 ierr = CeedQFunctionContextDestroy(&ctx); CeedChk(ierr); 569 return CEED_ERROR_SUCCESS; 570 } 571 572 /** 573 @brief Add a CeedQFunction input 574 575 @param qf CeedQFunction 576 @param fieldname Name of QFunction field 577 @param size Size of QFunction field, (ncomp * dim) for @ref CEED_EVAL_GRAD or 578 (ncomp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP 579 @param emode \ref CEED_EVAL_NONE to use values directly, 580 \ref CEED_EVAL_INTERP to use interpolated values, 581 \ref CEED_EVAL_GRAD to use gradients. 582 583 @return An error code: 0 - success, otherwise - failure 584 585 @ref User 586 **/ 587 int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname, CeedInt size, 588 CeedEvalMode emode) { 589 if (qf->operatorsset) 590 // LCOV_EXCL_START 591 return CeedError(qf->ceed, CEED_ERROR_MAJOR, 592 "QFunction cannot be changed when in use by an operator"); 593 // LCOV_EXCL_STOP 594 if ((emode == CEED_EVAL_WEIGHT) && (size != 1)) 595 // LCOV_EXCL_START 596 return CeedError(qf->ceed, CEED_ERROR_DIMENSION, 597 "CEED_EVAL_WEIGHT should have size 1"); 598 // LCOV_EXCL_STOP 599 int ierr = CeedQFunctionFieldSet(&qf->inputfields[qf->numinputfields], 600 fieldname, size, emode); 601 CeedChk(ierr); 602 qf->numinputfields++; 603 return CEED_ERROR_SUCCESS; 604 } 605 606 /** 607 @brief Add a CeedQFunction output 608 609 @param qf CeedQFunction 610 @param fieldname Name of QFunction field 611 @param size Size of QFunction field, (ncomp * dim) for @ref CEED_EVAL_GRAD or 612 (ncomp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP 613 @param emode \ref CEED_EVAL_NONE to use values directly, 614 \ref CEED_EVAL_INTERP to use interpolated values, 615 \ref CEED_EVAL_GRAD to use gradients. 616 617 @return An error code: 0 - success, otherwise - failure 618 619 @ref User 620 **/ 621 int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname, 622 CeedInt size, CeedEvalMode emode) { 623 if (qf->operatorsset) 624 // LCOV_EXCL_START 625 return CeedError(qf->ceed, CEED_ERROR_MAJOR, 626 "QFunction cannot be changed when in use by an operator"); 627 // LCOV_EXCL_STOP 628 if (emode == CEED_EVAL_WEIGHT) 629 // LCOV_EXCL_START 630 return CeedError(qf->ceed, CEED_ERROR_DIMENSION, 631 "Cannot create QFunction output with " 632 "CEED_EVAL_WEIGHT"); 633 // LCOV_EXCL_STOP 634 int ierr = CeedQFunctionFieldSet(&qf->outputfields[qf->numoutputfields], 635 fieldname, size, emode); 636 CeedChk(ierr); 637 qf->numoutputfields++; 638 return CEED_ERROR_SUCCESS; 639 } 640 641 /** 642 @brief Set global context for a CeedQFunction 643 644 @param qf CeedQFunction 645 @param ctx Context data to set 646 647 @return An error code: 0 - success, otherwise - failure 648 649 @ref User 650 **/ 651 int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) { 652 qf->ctx = ctx; 653 ctx->refcount++; 654 return CEED_ERROR_SUCCESS; 655 } 656 657 /** 658 @brief View a CeedQFunction 659 660 @param[in] qf CeedQFunction to view 661 @param[in] stream Stream to write; typically stdout/stderr or a file 662 663 @return Error code: 0 - success, otherwise - failure 664 665 @ref User 666 **/ 667 int CeedQFunctionView(CeedQFunction qf, FILE *stream) { 668 int ierr; 669 670 fprintf(stream, "%sCeedQFunction %s\n", 671 qf->qfname ? "Gallery " : "User ", qf->qfname ? qf->qfname : ""); 672 673 fprintf(stream, " %d Input Field%s:\n", qf->numinputfields, 674 qf->numinputfields>1 ? "s" : ""); 675 for (CeedInt i=0; i<qf->numinputfields; i++) { 676 ierr = CeedQFunctionFieldView(qf->inputfields[i], i, 1, stream); 677 CeedChk(ierr); 678 } 679 680 fprintf(stream, " %d Output Field%s:\n", qf->numoutputfields, 681 qf->numoutputfields>1 ? "s" : ""); 682 for (CeedInt i=0; i<qf->numoutputfields; i++) { 683 ierr = CeedQFunctionFieldView(qf->outputfields[i], i, 0, stream); 684 CeedChk(ierr); 685 } 686 return CEED_ERROR_SUCCESS; 687 } 688 689 /** 690 @brief Apply the action of a CeedQFunction 691 692 @param qf CeedQFunction 693 @param Q Number of quadrature points 694 @param[in] u Array of input CeedVectors 695 @param[out] v Array of output CeedVectors 696 697 @return An error code: 0 - success, otherwise - failure 698 699 @ref User 700 **/ 701 int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, 702 CeedVector *u, CeedVector *v) { 703 int ierr; 704 if (!qf->Apply) 705 // LCOV_EXCL_START 706 return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED, 707 "Backend does not support QFunctionApply"); 708 // LCOV_EXCL_STOP 709 if (Q % qf->vlength) 710 // LCOV_EXCL_START 711 return CeedError(qf->ceed, CEED_ERROR_DIMENSION, 712 "Number of quadrature points %d must be a " 713 "multiple of %d", Q, qf->vlength); 714 // LCOV_EXCL_STOP 715 ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr); 716 return CEED_ERROR_SUCCESS; 717 } 718 719 /** 720 @brief Destroy a CeedQFunction 721 722 @param qf CeedQFunction to destroy 723 724 @return An error code: 0 - success, otherwise - failure 725 726 @ref User 727 **/ 728 int CeedQFunctionDestroy(CeedQFunction *qf) { 729 int ierr; 730 731 if (!*qf || --(*qf)->refcount > 0) return CEED_ERROR_SUCCESS; 732 // Backend destroy 733 if ((*qf)->Destroy) { 734 ierr = (*qf)->Destroy(*qf); CeedChk(ierr); 735 } 736 // Free fields 737 for (int i=0; i<(*qf)->numinputfields; i++) { 738 ierr = CeedFree(&(*(*qf)->inputfields[i]).fieldname); CeedChk(ierr); 739 ierr = CeedFree(&(*qf)->inputfields[i]); CeedChk(ierr); 740 } 741 for (int i=0; i<(*qf)->numoutputfields; i++) { 742 ierr = CeedFree(&(*(*qf)->outputfields[i]).fieldname); CeedChk(ierr); 743 ierr = CeedFree(&(*qf)->outputfields[i]); CeedChk(ierr); 744 } 745 ierr = CeedFree(&(*qf)->inputfields); CeedChk(ierr); 746 ierr = CeedFree(&(*qf)->outputfields); CeedChk(ierr); 747 748 // User context data object 749 ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr); 750 751 ierr = CeedFree(&(*qf)->sourcepath); CeedChk(ierr); 752 ierr = CeedFree(&(*qf)->qfname); CeedChk(ierr); 753 ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr); 754 ierr = CeedFree(qf); CeedChk(ierr); 755 return CEED_ERROR_SUCCESS; 756 } 757 758 /// @} 759