1d7b241e6Sjeremylt // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at 2d7b241e6Sjeremylt // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights 3d7b241e6Sjeremylt // reserved. See files LICENSE and NOTICE for details. 4d7b241e6Sjeremylt // 5d7b241e6Sjeremylt // This file is part of CEED, a collection of benchmarks, miniapps, software 6d7b241e6Sjeremylt // libraries and APIs for efficient high-order finite element and spectral 7d7b241e6Sjeremylt // element discretizations for exascale applications. For more information and 8d7b241e6Sjeremylt // source code availability see http://github.com/ceed. 9d7b241e6Sjeremylt // 10d7b241e6Sjeremylt // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 11d7b241e6Sjeremylt // a collaborative effort of two U.S. Department of Energy organizations (Office 12d7b241e6Sjeremylt // of Science and the National Nuclear Security Administration) responsible for 13d7b241e6Sjeremylt // the planning and preparation of a capable exascale ecosystem, including 14d7b241e6Sjeremylt // software, applications, hardware, advanced system engineering and early 15d7b241e6Sjeremylt // testbed platforms, in support of the nation's exascale computing imperative. 16d7b241e6Sjeremylt 17d7b241e6Sjeremylt #include <ceed-impl.h> 18d863ab9bSjeremylt #include <ceed-backend.h> 19d7b241e6Sjeremylt #include <string.h> 20288c0443SJeremy L Thompson #include <limits.h> 21288c0443SJeremy L Thompson 22288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP 23442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none; 24442e7f0bSjeremylt /// @endcond 25442e7f0bSjeremylt 26442e7f0bSjeremylt /// @cond DOXYGEN_SKIP 27288c0443SJeremy L Thompson static struct { 28288c0443SJeremy L Thompson char name[CEED_MAX_RESOURCE_LEN]; 29288c0443SJeremy L Thompson char source[CEED_MAX_RESOURCE_LEN]; 30288c0443SJeremy L Thompson CeedInt vlength; 31288c0443SJeremy L Thompson CeedQFunctionUser f; 32288c0443SJeremy L Thompson int (*init)(Ceed ceed, const char *name, CeedQFunction qf); 33288c0443SJeremy L Thompson } qfunctions[1024]; 34288c0443SJeremy L Thompson static size_t num_qfunctions; 35288c0443SJeremy L Thompson /// @endcond 36d7b241e6Sjeremylt 37dfdf5a53Sjeremylt /// @file 38dfdf5a53Sjeremylt /// Implementation of public CeedQFunction interfaces 39dfdf5a53Sjeremylt /// 40dfdf5a53Sjeremylt /// @addtogroup CeedQFunction 41dfdf5a53Sjeremylt /// @{ 42d7b241e6Sjeremylt 43d7b241e6Sjeremylt /** 44d7b241e6Sjeremylt @brief Create a CeedQFunction for evaluating interior (volumetric) terms. 45d7b241e6Sjeremylt 46b11c1e72Sjeremylt @param ceed A Ceed object where the CeedQFunction will be created 47d7b241e6Sjeremylt @param vlength Vector length. Caller must ensure that number of quadrature 48d7b241e6Sjeremylt points is a multiple of vlength. 49d7b241e6Sjeremylt @param f Function pointer to evaluate action at quadrature points. 509f0427d9SYohann See \ref CeedQFunctionUser. 511176cc3aSJeremy L Thompson @param source Absolute path to source of QFunction, 521176cc3aSJeremy L Thompson "\abs_path\file.h:function_name" 53b11c1e72Sjeremylt @param[out] qf Address of the variable where the newly created 54b11c1e72Sjeremylt CeedQFunction will be stored 55b11c1e72Sjeremylt 56b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 57d7b241e6Sjeremylt 588795c945Sjeremylt See \ref CeedQFunctionUser for details on the call-back function @a f's 598795c945Sjeremylt arguments. 60d7b241e6Sjeremylt 61dfdf5a53Sjeremylt @ref Basic 62dfdf5a53Sjeremylt **/ 63692c2638Sjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength, CeedQFunctionUser f, 64288c0443SJeremy L Thompson const char *source, CeedQFunction *qf) { 65d7b241e6Sjeremylt int ierr; 66288c0443SJeremy L Thompson char *source_copy; 67d7b241e6Sjeremylt 685fe0d4faSjeremylt if (!ceed->QFunctionCreate) { 695fe0d4faSjeremylt Ceed delegate; 70aefd8378Sjeremylt ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr); 715fe0d4faSjeremylt 725fe0d4faSjeremylt if (!delegate) 73c042f62fSJeremy L Thompson // LCOV_EXCL_START 74d7b241e6Sjeremylt return CeedError(ceed, 1, "Backend does not support QFunctionCreate"); 75c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 765fe0d4faSjeremylt 77288c0443SJeremy L Thompson ierr = CeedQFunctionCreateInterior(delegate, vlength, f, source, qf); 781dfeef1dSjeremylt CeedChk(ierr); 795fe0d4faSjeremylt return 0; 805fe0d4faSjeremylt } 815fe0d4faSjeremylt 82d7b241e6Sjeremylt ierr = CeedCalloc(1, qf); CeedChk(ierr); 83d7b241e6Sjeremylt (*qf)->ceed = ceed; 84d7b241e6Sjeremylt ceed->refcount++; 85d7b241e6Sjeremylt (*qf)->refcount = 1; 86d7b241e6Sjeremylt (*qf)->vlength = vlength; 870219ea01SJeremy L Thompson (*qf)->identity = 0; 88d7b241e6Sjeremylt (*qf)->function = f; 8907a02837SJed Brown size_t slen = strlen(source) + 1; 9007a02837SJed Brown ierr = CeedMalloc(slen, &source_copy); CeedChk(ierr); 9107a02837SJed Brown memcpy(source_copy, source, slen); 92288c0443SJeremy L Thompson (*qf)->sourcepath = source_copy; 93fe2413ffSjeremylt ierr = CeedCalloc(16, &(*qf)->inputfields); CeedChk(ierr); 94fe2413ffSjeremylt ierr = CeedCalloc(16, &(*qf)->outputfields); CeedChk(ierr); 95d7b241e6Sjeremylt ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr); 96d7b241e6Sjeremylt return 0; 97d7b241e6Sjeremylt } 98d7b241e6Sjeremylt 99b11c1e72Sjeremylt /** 100288c0443SJeremy L Thompson @brief Register a gallery QFunction 101288c0443SJeremy L Thompson 102288c0443SJeremy L Thompson @param name Name for this backend to respond to 1031176cc3aSJeremy L Thompson @param source Absolute path to source of QFunction, 1041176cc3aSJeremy L Thompson "\path\CEED_DIR\gallery\folder\file.h:function_name" 105288c0443SJeremy L Thompson @param vlength Vector length. Caller must ensure that number of quadrature 106288c0443SJeremy L Thompson points is a multiple of vlength. 107288c0443SJeremy L Thompson @param f Function pointer to evaluate action at quadrature points. 108288c0443SJeremy L Thompson See \ref CeedQFunctionUser. 109288c0443SJeremy L Thompson @param init Initialization function called by CeedQFunctionInit() when the 110288c0443SJeremy L Thompson QFunction is selected. 111288c0443SJeremy L Thompson 112288c0443SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 113288c0443SJeremy L Thompson 114288c0443SJeremy L Thompson @ref Advanced 115288c0443SJeremy L Thompson **/ 116288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source, 117288c0443SJeremy L Thompson CeedInt vlength, CeedQFunctionUser f, 118288c0443SJeremy L Thompson int (*init)(Ceed, const char *, CeedQFunction)) { 119c042f62fSJeremy L Thompson if (num_qfunctions >= sizeof(qfunctions) / sizeof(qfunctions[0])) 120c042f62fSJeremy L Thompson // LCOV_EXCL_START 121288c0443SJeremy L Thompson return CeedError(NULL, 1, "Too many gallery QFunctions"); 122c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 123c042f62fSJeremy L Thompson 124288c0443SJeremy L Thompson strncpy(qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN); 125288c0443SJeremy L Thompson qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0; 126288c0443SJeremy L Thompson strncpy(qfunctions[num_qfunctions].source, source, CEED_MAX_RESOURCE_LEN); 127288c0443SJeremy L Thompson qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0; 128288c0443SJeremy L Thompson qfunctions[num_qfunctions].vlength = vlength; 129288c0443SJeremy L Thompson qfunctions[num_qfunctions].f = f; 130288c0443SJeremy L Thompson qfunctions[num_qfunctions].init = init; 131288c0443SJeremy L Thompson num_qfunctions++; 132288c0443SJeremy L Thompson return 0; 133288c0443SJeremy L Thompson } 134288c0443SJeremy L Thompson 135288c0443SJeremy L Thompson /** 136288c0443SJeremy L Thompson @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name. 137288c0443SJeremy L Thompson 138288c0443SJeremy L Thompson @param ceed A Ceed object where the CeedQFunction will be created 139288c0443SJeremy L Thompson @param name Name of QFunction to use from gallery 140288c0443SJeremy L Thompson @param[out] qf Address of the variable where the newly created 141288c0443SJeremy L Thompson CeedQFunction will be stored 142288c0443SJeremy L Thompson 143288c0443SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 144288c0443SJeremy L Thompson 145288c0443SJeremy L Thompson @ref Basic 146288c0443SJeremy L Thompson **/ 147288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, 148288c0443SJeremy L Thompson CeedQFunction *qf) { 149288c0443SJeremy L Thompson int ierr; 150288c0443SJeremy L Thompson size_t matchlen = 0, matchidx = UINT_MAX; 15175affc3bSjeremylt char *name_copy; 152288c0443SJeremy L Thompson 153288c0443SJeremy L Thompson // Find matching backend 154288c0443SJeremy L Thompson if (!name) return CeedError(NULL, 1, "No QFunction name provided"); 155288c0443SJeremy L Thompson for (size_t i=0; i<num_qfunctions; i++) { 156288c0443SJeremy L Thompson size_t n; 157288c0443SJeremy L Thompson const char *currname = qfunctions[i].name; 158288c0443SJeremy L Thompson for (n = 0; currname[n] && currname[n] == name[n]; n++) {} 159288c0443SJeremy L Thompson if (n > matchlen) { 160288c0443SJeremy L Thompson matchlen = n; 161288c0443SJeremy L Thompson matchidx = i; 162288c0443SJeremy L Thompson } 163288c0443SJeremy L Thompson } 1641d102b48SJeremy L Thompson if (!matchlen) 165138d4072Sjeremylt // LCOV_EXCL_START 1661d102b48SJeremy L Thompson return CeedError(NULL, 1, "No suitable gallery QFunction"); 167138d4072Sjeremylt // LCOV_EXCL_STOP 168288c0443SJeremy L Thompson 169288c0443SJeremy L Thompson // Create QFunction 170288c0443SJeremy L Thompson ierr = CeedQFunctionCreateInterior(ceed, qfunctions[matchidx].vlength, 171288c0443SJeremy L Thompson qfunctions[matchidx].f, 172288c0443SJeremy L Thompson qfunctions[matchidx].source, qf); 173288c0443SJeremy L Thompson CeedChk(ierr); 174288c0443SJeremy L Thompson 175288c0443SJeremy L Thompson // QFunction specific setup 176288c0443SJeremy L Thompson ierr = qfunctions[matchidx].init(ceed, name, *qf); CeedChk(ierr); 177288c0443SJeremy L Thompson 17875affc3bSjeremylt // Copy name 17975affc3bSjeremylt size_t slen = strlen(name) + 1; 18075affc3bSjeremylt ierr = CeedMalloc(slen, &name_copy); CeedChk(ierr); 18175affc3bSjeremylt memcpy(name_copy, name, slen); 18275affc3bSjeremylt (*qf)->qfname = name_copy; 18375affc3bSjeremylt 184288c0443SJeremy L Thompson return 0; 185288c0443SJeremy L Thompson } 186288c0443SJeremy L Thompson 187288c0443SJeremy L Thompson /** 1880219ea01SJeremy L Thompson @brief Create an identity CeedQFunction. Inputs are written into outputs in 1890219ea01SJeremy L Thompson the order given. This is useful for CeedOperators that can be 1900219ea01SJeremy L Thompson represented with only the action of a CeedRestriction and CeedBasis, 1910219ea01SJeremy L Thompson such as restriction and prolongation operators for p-multigrid. 1920219ea01SJeremy L Thompson Backends may optimize CeedOperators with this CeedQFunction to avoid 1930219ea01SJeremy L Thompson the copy of input data to output fields by using the same memory 1940219ea01SJeremy L Thompson location for both. 1950219ea01SJeremy L Thompson 1960219ea01SJeremy L Thompson @param ceed A Ceed object where the CeedQFunction will be created 19760f77c51Sjeremylt @param[in] size Size of the qfunction fields 19860f77c51Sjeremylt @param[in] inmode CeedEvalMode for input to CeedQFunction 19960f77c51Sjeremylt @param[in] outmode CeedEvalMode for output to CeedQFunction 2000219ea01SJeremy L Thompson @param[out] qf Address of the variable where the newly created 2010219ea01SJeremy L Thompson CeedQFunction will be stored 2020219ea01SJeremy L Thompson 2030219ea01SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2040219ea01SJeremy L Thompson 2050219ea01SJeremy L Thompson @ref Basic 2060219ea01SJeremy L Thompson **/ 20760f77c51Sjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode inmode, 20860f77c51Sjeremylt CeedEvalMode outmode, CeedQFunction *qf) { 2090219ea01SJeremy L Thompson int ierr; 2100219ea01SJeremy L Thompson 21160f77c51Sjeremylt if (inmode == CEED_EVAL_NONE && outmode == CEED_EVAL_NONE) 21260f77c51Sjeremylt // LCOV_EXCL_START 21360f77c51Sjeremylt return CeedError(ceed, 1, "CEED_EVAL_NONE for a both the input and " 21460f77c51Sjeremylt "output does not make sense with an identity QFunction"); 21560f77c51Sjeremylt // LCOV_EXCL_STOP 21660f77c51Sjeremylt 2170219ea01SJeremy L Thompson ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr); 21860f77c51Sjeremylt ierr = CeedQFunctionAddInput(*qf, "input", 1, inmode); CeedChk(ierr); 21960f77c51Sjeremylt ierr = CeedQFunctionAddOutput(*qf, "output", 1, outmode); CeedChk(ierr); 2200219ea01SJeremy L Thompson 2210219ea01SJeremy L Thompson (*qf)->identity = 1; 2220219ea01SJeremy L Thompson if (size > 1) { 2230219ea01SJeremy L Thompson CeedInt *ctx; 2240219ea01SJeremy L Thompson ierr = CeedCalloc(1, &ctx); CeedChk(ierr); 2250219ea01SJeremy L Thompson ctx[0] = size; 2260219ea01SJeremy L Thompson ierr = CeedQFunctionSetContext(*qf, ctx, sizeof(ctx)); CeedChk(ierr); 227c0fea178Sjeremylt (*qf)->inputfields[0]->size = size; 228c0fea178Sjeremylt (*qf)->outputfields[0]->size = size; 2290219ea01SJeremy L Thompson } 2300219ea01SJeremy L Thompson 2310219ea01SJeremy L Thompson return 0; 2320219ea01SJeremy L Thompson } 2330219ea01SJeremy L Thompson 2340219ea01SJeremy L Thompson /** 235a0a97fcfSJed Brown @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output 236b11c1e72Sjeremylt 237b11c1e72Sjeremylt @param f CeedQFunctionField 238b11c1e72Sjeremylt @param fieldname Name of QFunction field 2394d537eeaSYohann @param size Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or 24023e8ed12Sjeremylt 1 for CEED_EVAL_NONE, CEED_EVAL_INTERP, and CEED_EVAL_WEIGHT) 241b11c1e72Sjeremylt @param emode \ref CEED_EVAL_NONE to use values directly, 242b11c1e72Sjeremylt \ref CEED_EVAL_INTERP to use interpolated values, 24323e8ed12Sjeremylt \ref CEED_EVAL_GRAD to use gradients, 24423e8ed12Sjeremylt \ref CEED_EVAL_WEIGHT to use quadrature weights. 245b11c1e72Sjeremylt 246b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 247dfdf5a53Sjeremylt 248dfdf5a53Sjeremylt @ref Developer 249b11c1e72Sjeremylt **/ 250fe2413ffSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *fieldname, 2514d537eeaSYohann CeedInt size, CeedEvalMode emode) { 252d7b241e6Sjeremylt size_t len = strlen(fieldname); 253d7b241e6Sjeremylt char *tmp; 254fe2413ffSjeremylt int ierr; 255fe2413ffSjeremylt ierr = CeedCalloc(1,f); CeedChk(ierr); 256fe2413ffSjeremylt 257fe2413ffSjeremylt ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr); 258d7b241e6Sjeremylt memcpy(tmp, fieldname, len+1); 259fe2413ffSjeremylt (*f)->fieldname = tmp; 2604d537eeaSYohann (*f)->size = size; 261fe2413ffSjeremylt (*f)->emode = emode; 262d7b241e6Sjeremylt return 0; 263d7b241e6Sjeremylt } 264d7b241e6Sjeremylt 265b11c1e72Sjeremylt /** 266a0a97fcfSJed Brown @brief Add a CeedQFunction input 267b11c1e72Sjeremylt 268b11c1e72Sjeremylt @param qf CeedQFunction 269b11c1e72Sjeremylt @param fieldname Name of QFunction field 2704d537eeaSYohann @param size Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or 2714d537eeaSYohann 1 for CEED_EVAL_NONE and CEED_EVAL_INTERP) 272b11c1e72Sjeremylt @param emode \ref CEED_EVAL_NONE to use values directly, 273b11c1e72Sjeremylt \ref CEED_EVAL_INTERP to use interpolated values, 274b11c1e72Sjeremylt \ref CEED_EVAL_GRAD to use gradients. 275b11c1e72Sjeremylt 276b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 277dfdf5a53Sjeremylt 278dfdf5a53Sjeremylt @ref Basic 279b11c1e72Sjeremylt **/ 2801d102b48SJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname, CeedInt size, 2811d102b48SJeremy L Thompson CeedEvalMode emode) { 282fe2413ffSjeremylt int ierr = CeedQFunctionFieldSet(&qf->inputfields[qf->numinputfields], 2834d537eeaSYohann fieldname, size, emode); 284fe2413ffSjeremylt CeedChk(ierr); 285fe2413ffSjeremylt qf->numinputfields++; 286d7b241e6Sjeremylt return 0; 287d7b241e6Sjeremylt } 288d7b241e6Sjeremylt 289b11c1e72Sjeremylt /** 290a0a97fcfSJed Brown @brief Add a CeedQFunction output 291b11c1e72Sjeremylt 292b11c1e72Sjeremylt @param qf CeedQFunction 293b11c1e72Sjeremylt @param fieldname Name of QFunction field 2944d537eeaSYohann @param size Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or 2954d537eeaSYohann 1 for CEED_EVAL_NONE and CEED_EVAL_INTERP) 296b11c1e72Sjeremylt @param emode \ref CEED_EVAL_NONE to use values directly, 297b11c1e72Sjeremylt \ref CEED_EVAL_INTERP to use interpolated values, 298b11c1e72Sjeremylt \ref CEED_EVAL_GRAD to use gradients. 299b11c1e72Sjeremylt 300b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 301dfdf5a53Sjeremylt 302dfdf5a53Sjeremylt @ref Basic 303b11c1e72Sjeremylt **/ 304d7b241e6Sjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname, 3054d537eeaSYohann CeedInt size, CeedEvalMode emode) { 306d7b241e6Sjeremylt if (emode == CEED_EVAL_WEIGHT) 307c042f62fSJeremy L Thompson // LCOV_EXCL_START 3081d102b48SJeremy L Thompson return CeedError(qf->ceed, 1, "Cannot create QFunction output with " 3091d102b48SJeremy L Thompson "CEED_EVAL_WEIGHT"); 310c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 311fe2413ffSjeremylt int ierr = CeedQFunctionFieldSet(&qf->outputfields[qf->numoutputfields], 3124d537eeaSYohann fieldname, size, emode); 313fe2413ffSjeremylt CeedChk(ierr); 314fe2413ffSjeremylt qf->numoutputfields++; 315d7b241e6Sjeremylt return 0; 316d7b241e6Sjeremylt } 317d7b241e6Sjeremylt 318dfdf5a53Sjeremylt /** 3194ce2993fSjeremylt @brief Get the Ceed associated with a CeedQFunction 3204ce2993fSjeremylt 3214ce2993fSjeremylt @param qf CeedQFunction 3224ce2993fSjeremylt @param[out] ceed Variable to store Ceed 3234ce2993fSjeremylt 3244ce2993fSjeremylt @return An error code: 0 - success, otherwise - failure 3254ce2993fSjeremylt 32623617272Sjeremylt @ref Advanced 3274ce2993fSjeremylt **/ 3284ce2993fSjeremylt 3294ce2993fSjeremylt int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) { 3304ce2993fSjeremylt *ceed = qf->ceed; 3314ce2993fSjeremylt return 0; 3324ce2993fSjeremylt } 3334ce2993fSjeremylt 3344ce2993fSjeremylt /** 3354ce2993fSjeremylt @brief Get the vector length of a CeedQFunction 3364ce2993fSjeremylt 3374ce2993fSjeremylt @param qf CeedQFunction 338288c0443SJeremy L Thompson @param[out] vlength Variable to store vector length 3394ce2993fSjeremylt 3404ce2993fSjeremylt @return An error code: 0 - success, otherwise - failure 3414ce2993fSjeremylt 34223617272Sjeremylt @ref Advanced 3434ce2993fSjeremylt **/ 3444ce2993fSjeremylt 3454ce2993fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength) { 3464ce2993fSjeremylt *vlength = qf->vlength; 3474ce2993fSjeremylt return 0; 3484ce2993fSjeremylt } 3494ce2993fSjeremylt 3504ce2993fSjeremylt /** 351dfdf5a53Sjeremylt @brief Get the number of inputs and outputs to a CeedQFunction 352dfdf5a53Sjeremylt 353dfdf5a53Sjeremylt @param qf CeedQFunction 3544ce2993fSjeremylt @param[out] numinput Variable to store number of input fields 3554ce2993fSjeremylt @param[out] numoutput Variable to store number of output fields 356dfdf5a53Sjeremylt 357dfdf5a53Sjeremylt @return An error code: 0 - success, otherwise - failure 358dfdf5a53Sjeremylt 35923617272Sjeremylt @ref Advanced 360dfdf5a53Sjeremylt **/ 361dfdf5a53Sjeremylt 362d7b241e6Sjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput, 363d7b241e6Sjeremylt CeedInt *numoutput) { 3641a4ead9bSjeremylt if (numinput) *numinput = qf->numinputfields; 3651a4ead9bSjeremylt if (numoutput) *numoutput = qf->numoutputfields; 366d7b241e6Sjeremylt return 0; 367d7b241e6Sjeremylt } 368d7b241e6Sjeremylt 369d7b241e6Sjeremylt /** 370288c0443SJeremy L Thompson @brief Get the source path string for a CeedQFunction 3714ce2993fSjeremylt 3724ce2993fSjeremylt @param qf CeedQFunction 373288c0443SJeremy L Thompson @param[out] source Variable to store source path string 3744ce2993fSjeremylt 3754ce2993fSjeremylt @return An error code: 0 - success, otherwise - failure 3764ce2993fSjeremylt 37723617272Sjeremylt @ref Advanced 3784ce2993fSjeremylt **/ 3794ce2993fSjeremylt 380288c0443SJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source) { 381288c0443SJeremy L Thompson *source = (char *) qf->sourcepath; 3824ce2993fSjeremylt return 0; 3834ce2993fSjeremylt } 3844ce2993fSjeremylt 3854ce2993fSjeremylt /** 386fe2413ffSjeremylt @brief Get the User Function for a CeedQFunction 387fe2413ffSjeremylt 388fe2413ffSjeremylt @param qf CeedQFunction 389fe2413ffSjeremylt @param[out] f Variable to store user function 390fe2413ffSjeremylt 391fe2413ffSjeremylt @return An error code: 0 - success, otherwise - failure 392fe2413ffSjeremylt 393fe2413ffSjeremylt @ref Advanced 394fe2413ffSjeremylt **/ 395fe2413ffSjeremylt 396d4f68153Sjeremylt int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) { 397d4f68153Sjeremylt *f = qf->function; 398fe2413ffSjeremylt return 0; 399fe2413ffSjeremylt } 400fe2413ffSjeremylt 401fe2413ffSjeremylt /** 4024ce2993fSjeremylt @brief Get global context size for a CeedQFunction 4034ce2993fSjeremylt 4044ce2993fSjeremylt @param qf CeedQFunction 4054ce2993fSjeremylt @param[out] ctxsize Variable to store size of context data values 4064ce2993fSjeremylt 4074ce2993fSjeremylt @return An error code: 0 - success, otherwise - failure 4084ce2993fSjeremylt 40923617272Sjeremylt @ref Advanced 4104ce2993fSjeremylt **/ 4114ce2993fSjeremylt 4124ce2993fSjeremylt int CeedQFunctionGetContextSize(CeedQFunction qf, size_t *ctxsize) { 413069aeabaSjeremylt if (qf->fortranstatus) { 414069aeabaSjeremylt fContext *fctx = qf->ctx; 415069aeabaSjeremylt *ctxsize = fctx->innerctxsize; 416069aeabaSjeremylt } else { 4174ce2993fSjeremylt *ctxsize = qf->ctxsize; 418069aeabaSjeremylt } 4194ce2993fSjeremylt return 0; 4204ce2993fSjeremylt } 4214ce2993fSjeremylt 4224ce2993fSjeremylt /** 4234ce2993fSjeremylt @brief Get global context for a CeedQFunction 4244ce2993fSjeremylt 4254ce2993fSjeremylt @param qf CeedQFunction 4264ce2993fSjeremylt @param[out] ctx Variable to store context data values 4274ce2993fSjeremylt 4284ce2993fSjeremylt @return An error code: 0 - success, otherwise - failure 4294ce2993fSjeremylt 43023617272Sjeremylt @ref Advanced 4314ce2993fSjeremylt **/ 4324ce2993fSjeremylt 4334ce2993fSjeremylt int CeedQFunctionGetContext(CeedQFunction qf, void **ctx) { 4344ce2993fSjeremylt *ctx = qf->ctx; 4354ce2993fSjeremylt return 0; 4364ce2993fSjeremylt } 4374ce2993fSjeremylt 4384ce2993fSjeremylt /** 439418fb8c2Sjeremylt @brief Determine if Fortran interface was used 440418fb8c2Sjeremylt 441418fb8c2Sjeremylt @param qf CeedQFunction 442418fb8c2Sjeremylt @param[out] fortranstatus Variable to store Fortran status 443418fb8c2Sjeremylt 444418fb8c2Sjeremylt @return An error code: 0 - success, otherwise - failure 445418fb8c2Sjeremylt 446418fb8c2Sjeremylt @ref Advanced 447418fb8c2Sjeremylt **/ 448418fb8c2Sjeremylt 449418fb8c2Sjeremylt int CeedQFunctionGetFortranStatus(CeedQFunction qf, bool *fortranstatus) { 450418fb8c2Sjeremylt *fortranstatus = qf->fortranstatus; 451418fb8c2Sjeremylt return 0; 452418fb8c2Sjeremylt } 453418fb8c2Sjeremylt 454418fb8c2Sjeremylt /** 4550219ea01SJeremy L Thompson @brief Determine if QFunction is identity 4560219ea01SJeremy L Thompson 4570219ea01SJeremy L Thompson @param qf CeedQFunction 4580219ea01SJeremy L Thompson @param[out] identity Variable to store identity status 4590219ea01SJeremy L Thompson 4600219ea01SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4610219ea01SJeremy L Thompson 4620219ea01SJeremy L Thompson @ref Advanced 4630219ea01SJeremy L Thompson **/ 4640219ea01SJeremy L Thompson 4650219ea01SJeremy L Thompson int CeedQFunctionGetIdentityStatus(CeedQFunction qf, bool *identity) { 4660219ea01SJeremy L Thompson *identity = qf->identity; 4670219ea01SJeremy L Thompson return 0; 4680219ea01SJeremy L Thompson } 4690219ea01SJeremy L Thompson 4700219ea01SJeremy L Thompson /** 47114922b2aSjeremylt @brief Get true user context for a CeedQFunction 472069aeabaSjeremylt 473069aeabaSjeremylt @param qf CeedQFunction 474069aeabaSjeremylt @param[out] ctx Variable to store context data values 475069aeabaSjeremylt 476069aeabaSjeremylt @return An error code: 0 - success, otherwise - failure 477069aeabaSjeremylt 478069aeabaSjeremylt @ref Advanced 479069aeabaSjeremylt **/ 480069aeabaSjeremylt 48114922b2aSjeremylt int CeedQFunctionGetInnerContext(CeedQFunction qf, void **ctx) { 48214922b2aSjeremylt if (qf->fortranstatus) { 483069aeabaSjeremylt fContext *fctx = qf->ctx; 484069aeabaSjeremylt *ctx = fctx->innerctx; 48514922b2aSjeremylt } else { 48614922b2aSjeremylt *ctx = qf->ctx; 48714922b2aSjeremylt } 48814922b2aSjeremylt 4899f0427d9SYohann 490069aeabaSjeremylt return 0; 491069aeabaSjeremylt } 492069aeabaSjeremylt 493069aeabaSjeremylt /** 4944ce2993fSjeremylt @brief Get backend data of a CeedQFunction 4954ce2993fSjeremylt 4964ce2993fSjeremylt @param qf CeedQFunction 4974ce2993fSjeremylt @param[out] data Variable to store data 4984ce2993fSjeremylt 4994ce2993fSjeremylt @return An error code: 0 - success, otherwise - failure 5004ce2993fSjeremylt 50123617272Sjeremylt @ref Advanced 5024ce2993fSjeremylt **/ 5034ce2993fSjeremylt 5044ce2993fSjeremylt int CeedQFunctionGetData(CeedQFunction qf, void **data) { 5054ce2993fSjeremylt *data = qf->data; 5064ce2993fSjeremylt return 0; 5074ce2993fSjeremylt } 5084ce2993fSjeremylt 5094ce2993fSjeremylt /** 510fe2413ffSjeremylt @brief Set backend data of a CeedQFunction 511fe2413ffSjeremylt 512fe2413ffSjeremylt @param[out] qf CeedQFunction 513fe2413ffSjeremylt @param data Data to set 514fe2413ffSjeremylt 515fe2413ffSjeremylt @return An error code: 0 - success, otherwise - failure 516fe2413ffSjeremylt 517fe2413ffSjeremylt @ref Advanced 518fe2413ffSjeremylt **/ 519fe2413ffSjeremylt 520fe2413ffSjeremylt int CeedQFunctionSetData(CeedQFunction qf, void **data) { 521fe2413ffSjeremylt qf->data = *data; 522fe2413ffSjeremylt return 0; 523fe2413ffSjeremylt } 524fe2413ffSjeremylt 525fe2413ffSjeremylt /** 5264ce2993fSjeremylt @brief Set global context for a CeedQFunction 527b11c1e72Sjeremylt 528b11c1e72Sjeremylt @param qf CeedQFunction 529b11c1e72Sjeremylt @param ctx Context data to set 530b11c1e72Sjeremylt @param ctxsize Size of context data values 531b11c1e72Sjeremylt 532b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 533dfdf5a53Sjeremylt 534dfdf5a53Sjeremylt @ref Basic 535b11c1e72Sjeremylt **/ 536d7b241e6Sjeremylt int CeedQFunctionSetContext(CeedQFunction qf, void *ctx, size_t ctxsize) { 537d7b241e6Sjeremylt qf->ctx = ctx; 538d7b241e6Sjeremylt qf->ctxsize = ctxsize; 539d7b241e6Sjeremylt return 0; 540d7b241e6Sjeremylt } 541d7b241e6Sjeremylt 542b11c1e72Sjeremylt /** 54375affc3bSjeremylt @brief View a field of a CeedQFunction 54475affc3bSjeremylt 54575affc3bSjeremylt @param[in] field QFunction field to view 5462ebaca42Sjeremylt @param[in] fieldnumber Number of field being viewed 54775affc3bSjeremylt @param[in] stream Stream to view to, e.g., stdout 54875affc3bSjeremylt 54975affc3bSjeremylt @return An error code: 0 - success, otherwise - failure 55075affc3bSjeremylt 55175affc3bSjeremylt @ref Utility 55275affc3bSjeremylt **/ 55375affc3bSjeremylt static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt fieldnumber, 5542da88da5Sjeremylt bool in, FILE *stream) { 5552da88da5Sjeremylt const char *inout = in ? "Input" : "Output"; 5562da88da5Sjeremylt fprintf(stream, " %s Field [%d]:\n" 5572da88da5Sjeremylt " Name: \"%s\"\n" 5582da88da5Sjeremylt " Size: %d\n" 5592da88da5Sjeremylt " EvalMode: \"%s\"\n", 5602da88da5Sjeremylt inout, fieldnumber, field->fieldname, field->size, 56175affc3bSjeremylt CeedEvalModes[field->emode]); 56275affc3bSjeremylt 56375affc3bSjeremylt return 0; 56475affc3bSjeremylt } 56575affc3bSjeremylt 56675affc3bSjeremylt /** 56775affc3bSjeremylt @brief View a CeedQFunction 56875affc3bSjeremylt 56975affc3bSjeremylt @param[in] qf CeedQFunction to view 57075affc3bSjeremylt @param[in] stream Stream to write; typically stdout/stderr or a file 57175affc3bSjeremylt 57275affc3bSjeremylt @return Error code: 0 - success, otherwise - failure 57375affc3bSjeremylt 57475affc3bSjeremylt @ref Utility 57575affc3bSjeremylt **/ 57675affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) { 57775affc3bSjeremylt int ierr; 57875affc3bSjeremylt 57975affc3bSjeremylt fprintf(stream, "%sCeedQFunction %s\n", 58075affc3bSjeremylt qf->qfname ? "Gallery " : "User ", qf->qfname ? qf->qfname : ""); 58175affc3bSjeremylt 5822da88da5Sjeremylt fprintf(stream, " %d Input Field%s:\n", qf->numinputfields, 58375affc3bSjeremylt qf->numinputfields>1 ? "s" : ""); 58475affc3bSjeremylt for (CeedInt i=0; i<qf->numinputfields; i++) { 5852da88da5Sjeremylt ierr = CeedQFunctionFieldView(qf->inputfields[i], i, 1, stream); 5862da88da5Sjeremylt CeedChk(ierr); 58775affc3bSjeremylt } 58875affc3bSjeremylt 5892da88da5Sjeremylt fprintf(stream, " %d Output Field%s:\n", qf->numoutputfields, 59075affc3bSjeremylt qf->numoutputfields>1 ? "s" : ""); 59175affc3bSjeremylt for (CeedInt i=0; i<qf->numoutputfields; i++) { 5922da88da5Sjeremylt ierr = CeedQFunctionFieldView(qf->outputfields[i], i, 0, stream); 59375affc3bSjeremylt CeedChk(ierr); 59475affc3bSjeremylt } 59575affc3bSjeremylt return 0; 59675affc3bSjeremylt } 59775affc3bSjeremylt 59875affc3bSjeremylt /** 599b11c1e72Sjeremylt @brief Apply the action of a CeedQFunction 600b11c1e72Sjeremylt 601b11c1e72Sjeremylt @param qf CeedQFunction 602b11c1e72Sjeremylt @param Q Number of quadrature points 603*34138859Sjeremylt @param[in] u Array of input CeedVectors 604*34138859Sjeremylt @param[out] v Array of output CeedVectors 605b11c1e72Sjeremylt 606b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 607dfdf5a53Sjeremylt 608dfdf5a53Sjeremylt @ref Advanced 609b11c1e72Sjeremylt **/ 610d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, 611aedaa0e5Sjeremylt CeedVector *u, CeedVector *v) { 612d7b241e6Sjeremylt int ierr; 613d7b241e6Sjeremylt if (!qf->Apply) 614c042f62fSJeremy L Thompson // LCOV_EXCL_START 615d7b241e6Sjeremylt return CeedError(qf->ceed, 1, "Backend does not support QFunctionApply"); 616c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 617d7b241e6Sjeremylt if (Q % qf->vlength) 618c042f62fSJeremy L Thompson // LCOV_EXCL_START 6191d102b48SJeremy L Thompson return CeedError(qf->ceed, 2, "Number of quadrature points %d must be a " 6201d102b48SJeremy L Thompson "multiple of %d", Q, qf->vlength); 621c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 622d7b241e6Sjeremylt ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr); 623d7b241e6Sjeremylt return 0; 624d7b241e6Sjeremylt } 625d7b241e6Sjeremylt 626b11c1e72Sjeremylt /** 627d1bcdac9Sjeremylt @brief Get the CeedQFunctionFields of a CeedQFunction 628d1bcdac9Sjeremylt 629d1bcdac9Sjeremylt @param qf CeedQFunction 630d1bcdac9Sjeremylt @param[out] inputfields Variable to store inputfields 631d1bcdac9Sjeremylt @param[out] outputfields Variable to store outputfields 632d1bcdac9Sjeremylt 633d1bcdac9Sjeremylt @return An error code: 0 - success, otherwise - failure 634d1bcdac9Sjeremylt 635d1bcdac9Sjeremylt @ref Advanced 636d1bcdac9Sjeremylt **/ 637d1bcdac9Sjeremylt 638692c2638Sjeremylt int CeedQFunctionGetFields(CeedQFunction qf, CeedQFunctionField **inputfields, 639d1bcdac9Sjeremylt CeedQFunctionField **outputfields) { 6401d102b48SJeremy L Thompson if (inputfields) 6411d102b48SJeremy L Thompson *inputfields = qf->inputfields; 6421d102b48SJeremy L Thompson if (outputfields) 6431d102b48SJeremy L Thompson *outputfields = qf->outputfields; 644d1bcdac9Sjeremylt return 0; 645d1bcdac9Sjeremylt } 646d1bcdac9Sjeremylt 647d1bcdac9Sjeremylt /** 648fe2413ffSjeremylt @brief Get the name of a CeedQFunctionField 649fe2413ffSjeremylt 650fe2413ffSjeremylt @param qffield CeedQFunctionField 651fe2413ffSjeremylt @param[out] fieldname Variable to store the field name 652fe2413ffSjeremylt 653fe2413ffSjeremylt @return An error code: 0 - success, otherwise - failure 654fe2413ffSjeremylt 655fe2413ffSjeremylt @ref Advanced 656fe2413ffSjeremylt **/ 657fe2413ffSjeremylt 658692c2638Sjeremylt int CeedQFunctionFieldGetName(CeedQFunctionField qffield, char **fieldname) { 659fe2413ffSjeremylt *fieldname = (char *)qffield->fieldname; 660fe2413ffSjeremylt return 0; 661fe2413ffSjeremylt } 662fe2413ffSjeremylt 663fe2413ffSjeremylt /** 664d1bcdac9Sjeremylt @brief Get the number of components of a CeedQFunctionField 665d1bcdac9Sjeremylt 666d1bcdac9Sjeremylt @param qffield CeedQFunctionField 6674d537eeaSYohann @param[out] size Variable to store the size of the field 668d1bcdac9Sjeremylt 669d1bcdac9Sjeremylt @return An error code: 0 - success, otherwise - failure 670d1bcdac9Sjeremylt 671d1bcdac9Sjeremylt @ref Advanced 672d1bcdac9Sjeremylt **/ 673d1bcdac9Sjeremylt 6744d537eeaSYohann int CeedQFunctionFieldGetSize(CeedQFunctionField qffield, CeedInt *size) { 6754d537eeaSYohann *size = qffield->size; 676d1bcdac9Sjeremylt return 0; 677d1bcdac9Sjeremylt } 678d1bcdac9Sjeremylt 679d1bcdac9Sjeremylt /** 680d1bcdac9Sjeremylt @brief Get the CeedEvalMode of a CeedQFunctionField 681d1bcdac9Sjeremylt 682d1bcdac9Sjeremylt @param qffield CeedQFunctionField 683288c0443SJeremy L Thompson @param[out] emode Variable to store the field evaluation mode 684d1bcdac9Sjeremylt 685d1bcdac9Sjeremylt @return An error code: 0 - success, otherwise - failure 686d1bcdac9Sjeremylt 687d1bcdac9Sjeremylt @ref Advanced 688d1bcdac9Sjeremylt **/ 689d1bcdac9Sjeremylt 690d1bcdac9Sjeremylt int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qffield, 691d1bcdac9Sjeremylt CeedEvalMode *emode) { 692fe2413ffSjeremylt *emode = qffield->emode; 693d1bcdac9Sjeremylt return 0; 694d1bcdac9Sjeremylt } 695d1bcdac9Sjeremylt 696d1bcdac9Sjeremylt /** 697b11c1e72Sjeremylt @brief Destroy a CeedQFunction 698b11c1e72Sjeremylt 699b11c1e72Sjeremylt @param qf CeedQFunction to destroy 700b11c1e72Sjeremylt 701b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 702dfdf5a53Sjeremylt 703dfdf5a53Sjeremylt @ref Basic 704b11c1e72Sjeremylt **/ 705d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) { 706d7b241e6Sjeremylt int ierr; 707d7b241e6Sjeremylt 7081d102b48SJeremy L Thompson if (!*qf || --(*qf)->refcount > 0) 7091d102b48SJeremy L Thompson return 0; 710fe2413ffSjeremylt // Backend destroy 711d7b241e6Sjeremylt if ((*qf)->Destroy) { 712d7b241e6Sjeremylt ierr = (*qf)->Destroy(*qf); CeedChk(ierr); 713d7b241e6Sjeremylt } 714fe2413ffSjeremylt // Free fields 715fe2413ffSjeremylt for (int i=0; i<(*qf)->numinputfields; i++) { 716fe2413ffSjeremylt ierr = CeedFree(&(*(*qf)->inputfields[i]).fieldname); CeedChk(ierr); 717fe2413ffSjeremylt ierr = CeedFree(&(*qf)->inputfields[i]); CeedChk(ierr); 718fe2413ffSjeremylt } 719fe2413ffSjeremylt for (int i=0; i<(*qf)->numoutputfields; i++) { 720fe2413ffSjeremylt ierr = CeedFree(&(*(*qf)->outputfields[i]).fieldname); CeedChk(ierr); 721fe2413ffSjeremylt ierr = CeedFree(&(*qf)->outputfields[i]); CeedChk(ierr); 722fe2413ffSjeremylt } 723fe2413ffSjeremylt ierr = CeedFree(&(*qf)->inputfields); CeedChk(ierr); 724fe2413ffSjeremylt ierr = CeedFree(&(*qf)->outputfields); CeedChk(ierr); 7250219ea01SJeremy L Thompson // Free ctx if identity 7260219ea01SJeremy L Thompson if ((*qf)->identity) { 7270219ea01SJeremy L Thompson ierr = CeedFree(&(*qf)->ctx); CeedChk(ierr); 7280219ea01SJeremy L Thompson } 729fe2413ffSjeremylt 730288c0443SJeremy L Thompson ierr = CeedFree(&(*qf)->sourcepath); CeedChk(ierr); 73175affc3bSjeremylt ierr = CeedFree(&(*qf)->qfname); CeedChk(ierr); 732d7b241e6Sjeremylt ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr); 733d7b241e6Sjeremylt ierr = CeedFree(qf); CeedChk(ierr); 734d7b241e6Sjeremylt return 0; 735d7b241e6Sjeremylt } 736d7b241e6Sjeremylt 737442e7f0bSjeremylt /// @cond DOXYGEN_SKIP 738442e7f0bSjeremylt // Indicate that no QFunction is provided by the user 739442e7f0bSjeremylt CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none; 740442e7f0bSjeremylt /// @endcond 741d7b241e6Sjeremylt /// @} 742