xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision d99fa3c5cd91a1690aedf0679cbf290d44fec74c)
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 
227a982d89SJeremy L. Thompson /// @file
237a982d89SJeremy L. Thompson /// Implementation of public CeedQFunction interfaces
247a982d89SJeremy L. Thompson 
25288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP
26442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none;
27442e7f0bSjeremylt /// @endcond
28442e7f0bSjeremylt 
297a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
307a982d89SJeremy L. Thompson /// @{
317a982d89SJeremy L. Thompson 
327a982d89SJeremy L. Thompson // Indicate that no QFunction is provided by the user
337a982d89SJeremy L. Thompson const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
347a982d89SJeremy L. Thompson 
357a982d89SJeremy L. Thompson /// @}
367a982d89SJeremy L. Thompson 
37442e7f0bSjeremylt /// @cond DOXYGEN_SKIP
38288c0443SJeremy L Thompson static struct {
39288c0443SJeremy L Thompson   char name[CEED_MAX_RESOURCE_LEN];
40288c0443SJeremy L Thompson   char source[CEED_MAX_RESOURCE_LEN];
41288c0443SJeremy L Thompson   CeedInt vlength;
42288c0443SJeremy L Thompson   CeedQFunctionUser f;
43288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
44288c0443SJeremy L Thompson } qfunctions[1024];
45288c0443SJeremy L Thompson static size_t num_qfunctions;
46288c0443SJeremy L Thompson /// @endcond
47d7b241e6Sjeremylt 
48b11c1e72Sjeremylt /**
49288c0443SJeremy L Thompson   @brief Register a gallery QFunction
50288c0443SJeremy L Thompson 
51288c0443SJeremy L Thompson   @param name     Name for this backend to respond to
521176cc3aSJeremy L Thompson   @param source   Absolute path to source of QFunction,
531176cc3aSJeremy L Thompson                     "\path\CEED_DIR\gallery\folder\file.h:function_name"
54288c0443SJeremy L Thompson   @param vlength  Vector length.  Caller must ensure that number of quadrature
55288c0443SJeremy L Thompson                     points is a multiple of vlength.
56288c0443SJeremy L Thompson   @param f        Function pointer to evaluate action at quadrature points.
57288c0443SJeremy L Thompson                     See \ref CeedQFunctionUser.
58288c0443SJeremy L Thompson   @param init     Initialization function called by CeedQFunctionInit() when the
59288c0443SJeremy L Thompson                     QFunction is selected.
60288c0443SJeremy L Thompson 
61288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
62288c0443SJeremy L Thompson 
637a982d89SJeremy L. Thompson   @ref Developer
64288c0443SJeremy L Thompson **/
65288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source,
66288c0443SJeremy L Thompson                           CeedInt vlength, CeedQFunctionUser f,
67288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
68c042f62fSJeremy L Thompson   if (num_qfunctions >= sizeof(qfunctions) / sizeof(qfunctions[0]))
69c042f62fSJeremy L Thompson     // LCOV_EXCL_START
70288c0443SJeremy L Thompson     return CeedError(NULL, 1, "Too many gallery QFunctions");
71c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
72c042f62fSJeremy L Thompson 
73288c0443SJeremy L Thompson   strncpy(qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
74288c0443SJeremy L Thompson   qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
75288c0443SJeremy L Thompson   strncpy(qfunctions[num_qfunctions].source, source, CEED_MAX_RESOURCE_LEN);
76288c0443SJeremy L Thompson   qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
77288c0443SJeremy L Thompson   qfunctions[num_qfunctions].vlength = vlength;
78288c0443SJeremy L Thompson   qfunctions[num_qfunctions].f = f;
79288c0443SJeremy L Thompson   qfunctions[num_qfunctions].init = init;
80288c0443SJeremy L Thompson   num_qfunctions++;
81288c0443SJeremy L Thompson   return 0;
82288c0443SJeremy L Thompson }
83288c0443SJeremy L Thompson 
84288c0443SJeremy L Thompson /**
857a982d89SJeremy L. Thompson   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
867a982d89SJeremy L. Thompson 
877a982d89SJeremy L. Thompson   @param f          CeedQFunctionField
887a982d89SJeremy L. Thompson   @param fieldname  Name of QFunction field
894cc79fe7SJed Brown   @param size       Size of QFunction field, (ncomp * dim) for @ref CEED_EVAL_GRAD or
904cc79fe7SJed Brown                       (ncomp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT
917a982d89SJeremy L. Thompson   @param emode      \ref CEED_EVAL_NONE to use values directly,
927a982d89SJeremy L. Thompson                       \ref CEED_EVAL_INTERP to use interpolated values,
937a982d89SJeremy L. Thompson                       \ref CEED_EVAL_GRAD to use gradients,
947a982d89SJeremy L. Thompson                       \ref CEED_EVAL_WEIGHT to use quadrature weights.
957a982d89SJeremy L. Thompson 
967a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
977a982d89SJeremy L. Thompson 
987a982d89SJeremy L. Thompson   @ref Developer
997a982d89SJeremy L. Thompson **/
1007a982d89SJeremy L. Thompson static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *fieldname,
1017a982d89SJeremy L. Thompson                                  CeedInt size, CeedEvalMode emode) {
1027a982d89SJeremy L. Thompson   size_t len = strlen(fieldname);
1037a982d89SJeremy L. Thompson   char *tmp;
1047a982d89SJeremy L. Thompson   int ierr;
1057a982d89SJeremy L. Thompson   ierr = CeedCalloc(1,f); CeedChk(ierr);
1067a982d89SJeremy L. Thompson 
1077a982d89SJeremy L. Thompson   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
1087a982d89SJeremy L. Thompson   memcpy(tmp, fieldname, len+1);
1097a982d89SJeremy L. Thompson   (*f)->fieldname = tmp;
1107a982d89SJeremy L. Thompson   (*f)->size = size;
1117a982d89SJeremy L. Thompson   (*f)->emode = emode;
1127a982d89SJeremy L. Thompson   return 0;
1137a982d89SJeremy L. Thompson }
1147a982d89SJeremy L. Thompson 
1157a982d89SJeremy L. Thompson /**
1167a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1177a982d89SJeremy L. Thompson 
1187a982d89SJeremy L. Thompson   @param[in] field        QFunction field to view
1197a982d89SJeremy L. Thompson   @param[in] fieldnumber  Number of field being viewed
1204c4400c7SValeria Barra   @param[in] in           true for input field, false for output
1217a982d89SJeremy L. Thompson   @param[in] stream       Stream to view to, e.g., stdout
1227a982d89SJeremy L. Thompson 
1237a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1247a982d89SJeremy L. Thompson 
1257a982d89SJeremy L. Thompson   @ref Utility
1267a982d89SJeremy L. Thompson **/
1277a982d89SJeremy L. Thompson static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt fieldnumber,
1287a982d89SJeremy L. Thompson                                   bool in, FILE *stream) {
1297a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1307a982d89SJeremy L. Thompson   fprintf(stream, "    %s Field [%d]:\n"
1317a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1327a982d89SJeremy L. Thompson           "      Size: %d\n"
1337a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1347a982d89SJeremy L. Thompson           inout, fieldnumber, field->fieldname, field->size,
1357a982d89SJeremy L. Thompson           CeedEvalModes[field->emode]);
1367a982d89SJeremy L. Thompson 
1377a982d89SJeremy L. Thompson   return 0;
1387a982d89SJeremy L. Thompson }
1397a982d89SJeremy L. Thompson 
1407a982d89SJeremy L. Thompson /// @}
1417a982d89SJeremy L. Thompson 
1427a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1437a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1447a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1457a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1467a982d89SJeremy L. Thompson /// @{
1477a982d89SJeremy L. Thompson 
1487a982d89SJeremy L. Thompson /**
1497a982d89SJeremy L. Thompson   @brief Get the Ceed associated with a CeedQFunction
1507a982d89SJeremy L. Thompson 
1517a982d89SJeremy L. Thompson   @param qf              CeedQFunction
1527a982d89SJeremy L. Thompson   @param[out] ceed       Variable to store Ceed
1537a982d89SJeremy L. Thompson 
1547a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1557a982d89SJeremy L. Thompson 
1567a982d89SJeremy L. Thompson   @ref Backend
1577a982d89SJeremy L. Thompson **/
1587a982d89SJeremy L. Thompson 
1597a982d89SJeremy L. Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
1607a982d89SJeremy L. Thompson   *ceed = qf->ceed;
1617a982d89SJeremy L. Thompson   return 0;
1627a982d89SJeremy L. Thompson }
1637a982d89SJeremy L. Thompson 
1647a982d89SJeremy L. Thompson /**
1657a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1667a982d89SJeremy L. Thompson 
1677a982d89SJeremy L. Thompson   @param qf            CeedQFunction
1687a982d89SJeremy L. Thompson   @param[out] vlength  Variable to store vector length
1697a982d89SJeremy L. Thompson 
1707a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1717a982d89SJeremy L. Thompson 
1727a982d89SJeremy L. Thompson   @ref Backend
1737a982d89SJeremy L. Thompson **/
1747a982d89SJeremy L. Thompson 
1757a982d89SJeremy L. Thompson int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength) {
1767a982d89SJeremy L. Thompson   *vlength = qf->vlength;
1777a982d89SJeremy L. Thompson   return 0;
1787a982d89SJeremy L. Thompson }
1797a982d89SJeremy L. Thompson 
1807a982d89SJeremy L. Thompson /**
1817a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
1827a982d89SJeremy L. Thompson 
1837a982d89SJeremy L. Thompson   @param qf              CeedQFunction
1847a982d89SJeremy L. Thompson   @param[out] numinput   Variable to store number of input fields
1857a982d89SJeremy L. Thompson   @param[out] numoutput  Variable to store number of output fields
1867a982d89SJeremy L. Thompson 
1877a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1887a982d89SJeremy L. Thompson 
1897a982d89SJeremy L. Thompson   @ref Backend
1907a982d89SJeremy L. Thompson **/
1917a982d89SJeremy L. Thompson 
1927a982d89SJeremy L. Thompson int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput,
1937a982d89SJeremy L. Thompson                             CeedInt *numoutput) {
1947a982d89SJeremy L. Thompson   if (numinput) *numinput = qf->numinputfields;
1957a982d89SJeremy L. Thompson   if (numoutput) *numoutput = qf->numoutputfields;
1967a982d89SJeremy L. Thompson   return 0;
1977a982d89SJeremy L. Thompson }
1987a982d89SJeremy L. Thompson 
1997a982d89SJeremy L. Thompson /**
2007a982d89SJeremy L. Thompson   @brief Get the source path string for a CeedQFunction
2017a982d89SJeremy L. Thompson 
2027a982d89SJeremy L. Thompson   @param qf              CeedQFunction
2037a982d89SJeremy L. Thompson   @param[out] source     Variable to store source path string
2047a982d89SJeremy L. Thompson 
2057a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2067a982d89SJeremy L. Thompson 
2077a982d89SJeremy L. Thompson   @ref Backend
2087a982d89SJeremy L. Thompson **/
2097a982d89SJeremy L. Thompson 
2107a982d89SJeremy L. Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source) {
2117a982d89SJeremy L. Thompson   *source = (char *) qf->sourcepath;
2127a982d89SJeremy L. Thompson   return 0;
2137a982d89SJeremy L. Thompson }
2147a982d89SJeremy L. Thompson 
2157a982d89SJeremy L. Thompson /**
2167a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2177a982d89SJeremy L. Thompson 
2187a982d89SJeremy L. Thompson   @param qf              CeedQFunction
2197a982d89SJeremy L. Thompson   @param[out] f          Variable to store user function
2207a982d89SJeremy L. Thompson 
2217a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2227a982d89SJeremy L. Thompson 
2237a982d89SJeremy L. Thompson   @ref Backend
2247a982d89SJeremy L. Thompson **/
2257a982d89SJeremy L. Thompson 
2267a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
2277a982d89SJeremy L. Thompson   *f = qf->function;
2287a982d89SJeremy L. Thompson   return 0;
2297a982d89SJeremy L. Thompson }
2307a982d89SJeremy L. Thompson 
2317a982d89SJeremy L. Thompson /**
2327a982d89SJeremy L. Thompson   @brief Get global context size for a CeedQFunction
2337a982d89SJeremy L. Thompson 
2347a982d89SJeremy L. Thompson   @param qf              CeedQFunction
2357a982d89SJeremy L. Thompson   @param[out] ctxsize    Variable to store size of context data values
2367a982d89SJeremy L. Thompson 
2377a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2387a982d89SJeremy L. Thompson 
2397a982d89SJeremy L. Thompson   @ref Backend
2407a982d89SJeremy L. Thompson **/
2417a982d89SJeremy L. Thompson 
2427a982d89SJeremy L. Thompson int CeedQFunctionGetContextSize(CeedQFunction qf, size_t *ctxsize) {
2437a982d89SJeremy L. Thompson   if (qf->fortranstatus) {
2447a982d89SJeremy L. Thompson     fContext *fctx = qf->ctx;
2457a982d89SJeremy L. Thompson     *ctxsize = fctx->innerctxsize;
2467a982d89SJeremy L. Thompson   } else {
2477a982d89SJeremy L. Thompson     *ctxsize = qf->ctxsize;
2487a982d89SJeremy L. Thompson   }
2497a982d89SJeremy L. Thompson   return 0;
2507a982d89SJeremy L. Thompson }
2517a982d89SJeremy L. Thompson 
2527a982d89SJeremy L. Thompson /**
2537a982d89SJeremy L. Thompson   @brief Get global context for a CeedQFunction
2547a982d89SJeremy L. Thompson 
2557a982d89SJeremy L. Thompson   @param qf              CeedQFunction
2567a982d89SJeremy L. Thompson   @param[out] ctx        Variable to store context data values
2577a982d89SJeremy L. Thompson 
2587a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2597a982d89SJeremy L. Thompson 
2607a982d89SJeremy L. Thompson   @ref Backend
2617a982d89SJeremy L. Thompson **/
2627a982d89SJeremy L. Thompson 
2637a982d89SJeremy L. Thompson int CeedQFunctionGetContext(CeedQFunction qf, void **ctx) {
2647a982d89SJeremy L. Thompson   *ctx = qf->ctx;
2657a982d89SJeremy L. Thompson   return 0;
2667a982d89SJeremy L. Thompson }
2677a982d89SJeremy L. Thompson 
2687a982d89SJeremy L. Thompson /**
2697a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
2707a982d89SJeremy L. Thompson 
2717a982d89SJeremy L. Thompson   @param qf              CeedQFunction
2727a982d89SJeremy L. Thompson   @param[out] ctx        Variable to store context data values
2737a982d89SJeremy L. Thompson 
2747a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2757a982d89SJeremy L. Thompson 
2767a982d89SJeremy L. Thompson   @ref Backend
2777a982d89SJeremy L. Thompson **/
2787a982d89SJeremy L. Thompson 
2797a982d89SJeremy L. Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, void **ctx) {
2807a982d89SJeremy L. Thompson   if (qf->fortranstatus) {
2817a982d89SJeremy L. Thompson     fContext *fctx = qf->ctx;
2827a982d89SJeremy L. Thompson     *ctx = fctx->innerctx;
2837a982d89SJeremy L. Thompson   } else {
2847a982d89SJeremy L. Thompson     *ctx = qf->ctx;
2857a982d89SJeremy L. Thompson   }
2867a982d89SJeremy L. Thompson 
2877a982d89SJeremy L. Thompson 
2887a982d89SJeremy L. Thompson   return 0;
2897a982d89SJeremy L. Thompson }
2907a982d89SJeremy L. Thompson 
2917a982d89SJeremy L. Thompson /**
2927a982d89SJeremy L. Thompson   @brief Determine if Fortran interface was used
2937a982d89SJeremy L. Thompson 
2947a982d89SJeremy L. Thompson   @param qf                  CeedQFunction
295fd364f38SJeremy L Thompson   @param[out] isfortran      Variable to store Fortran status
2967a982d89SJeremy L. Thompson 
2977a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2987a982d89SJeremy L. Thompson 
2997a982d89SJeremy L. Thompson   @ref Backend
3007a982d89SJeremy L. Thompson **/
3017a982d89SJeremy L. Thompson 
302fd364f38SJeremy L Thompson int CeedQFunctionIsFortran(CeedQFunction qf, bool *isfortran) {
303fd364f38SJeremy L Thompson   *isfortran = qf->fortranstatus;
3047a982d89SJeremy L. Thompson   return 0;
3057a982d89SJeremy L. Thompson }
3067a982d89SJeremy L. Thompson 
3077a982d89SJeremy L. Thompson /**
3087a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
3097a982d89SJeremy L. Thompson 
3107a982d89SJeremy L. Thompson   @param qf               CeedQFunction
311fd364f38SJeremy L Thompson   @param[out] isidentity  Variable to store identity status
3127a982d89SJeremy L. Thompson 
3137a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3147a982d89SJeremy L. Thompson 
3157a982d89SJeremy L. Thompson   @ref Backend
3167a982d89SJeremy L. Thompson **/
3177a982d89SJeremy L. Thompson 
318fd364f38SJeremy L Thompson int CeedQFunctionIsIdentity(CeedQFunction qf, bool *isidentity) {
319fd364f38SJeremy L Thompson   *isidentity = qf->identity;
3207a982d89SJeremy L. Thompson   return 0;
3217a982d89SJeremy L. Thompson }
3227a982d89SJeremy L. Thompson 
3237a982d89SJeremy L. Thompson /**
3247a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
3257a982d89SJeremy L. Thompson 
3267a982d89SJeremy L. Thompson   @param qf              CeedQFunction
3277a982d89SJeremy L. Thompson   @param[out] data       Variable to store data
3287a982d89SJeremy L. Thompson 
3297a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3307a982d89SJeremy L. Thompson 
3317a982d89SJeremy L. Thompson   @ref Backend
3327a982d89SJeremy L. Thompson **/
3337a982d89SJeremy L. Thompson 
3347a982d89SJeremy L. Thompson int CeedQFunctionGetData(CeedQFunction qf, void **data) {
3357a982d89SJeremy L. Thompson   *data = qf->data;
3367a982d89SJeremy L. Thompson   return 0;
3377a982d89SJeremy L. Thompson }
3387a982d89SJeremy L. Thompson 
3397a982d89SJeremy L. Thompson /**
3407a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
3417a982d89SJeremy L. Thompson 
3427a982d89SJeremy L. Thompson   @param[out] qf         CeedQFunction
3437a982d89SJeremy L. Thompson   @param data            Data to set
3447a982d89SJeremy L. Thompson 
3457a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3467a982d89SJeremy L. Thompson 
3477a982d89SJeremy L. Thompson   @ref Backend
3487a982d89SJeremy L. Thompson **/
3497a982d89SJeremy L. Thompson 
3507a982d89SJeremy L. Thompson int CeedQFunctionSetData(CeedQFunction qf, void **data) {
3517a982d89SJeremy L. Thompson   qf->data = *data;
3527a982d89SJeremy L. Thompson   return 0;
3537a982d89SJeremy L. Thompson }
3547a982d89SJeremy L. Thompson 
3557a982d89SJeremy L. Thompson /**
3567a982d89SJeremy L. Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
3577a982d89SJeremy L. Thompson 
3587a982d89SJeremy L. Thompson   @param qf                 CeedQFunction
3597a982d89SJeremy L. Thompson   @param[out] inputfields   Variable to store inputfields
3607a982d89SJeremy L. Thompson   @param[out] outputfields  Variable to store outputfields
3617a982d89SJeremy L. Thompson 
3627a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3637a982d89SJeremy L. Thompson 
3647a982d89SJeremy L. Thompson   @ref Backend
3657a982d89SJeremy L. Thompson **/
3667a982d89SJeremy L. Thompson 
3677a982d89SJeremy L. Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedQFunctionField **inputfields,
3687a982d89SJeremy L. Thompson                            CeedQFunctionField **outputfields) {
3697a982d89SJeremy L. Thompson   if (inputfields)
3707a982d89SJeremy L. Thompson     *inputfields = qf->inputfields;
3717a982d89SJeremy L. Thompson   if (outputfields)
3727a982d89SJeremy L. Thompson     *outputfields = qf->outputfields;
3737a982d89SJeremy L. Thompson   return 0;
3747a982d89SJeremy L. Thompson }
3757a982d89SJeremy L. Thompson 
3767a982d89SJeremy L. Thompson /**
3777a982d89SJeremy L. Thompson   @brief Get the name of a CeedQFunctionField
3787a982d89SJeremy L. Thompson 
3797a982d89SJeremy L. Thompson   @param qffield         CeedQFunctionField
3807a982d89SJeremy L. Thompson   @param[out] fieldname  Variable to store the field name
3817a982d89SJeremy L. Thompson 
3827a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3837a982d89SJeremy L. Thompson 
3847a982d89SJeremy L. Thompson   @ref Backend
3857a982d89SJeremy L. Thompson **/
3867a982d89SJeremy L. Thompson 
3877a982d89SJeremy L. Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qffield, char **fieldname) {
3887a982d89SJeremy L. Thompson   *fieldname = (char *)qffield->fieldname;
3897a982d89SJeremy L. Thompson   return 0;
3907a982d89SJeremy L. Thompson }
3917a982d89SJeremy L. Thompson 
3927a982d89SJeremy L. Thompson /**
3937a982d89SJeremy L. Thompson   @brief Get the number of components of a CeedQFunctionField
3947a982d89SJeremy L. Thompson 
3957a982d89SJeremy L. Thompson   @param qffield    CeedQFunctionField
3967a982d89SJeremy L. Thompson   @param[out] size  Variable to store the size of the field
3977a982d89SJeremy L. Thompson 
3987a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3997a982d89SJeremy L. Thompson 
4007a982d89SJeremy L. Thompson   @ref Backend
4017a982d89SJeremy L. Thompson **/
4027a982d89SJeremy L. Thompson 
4037a982d89SJeremy L. Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qffield, CeedInt *size) {
4047a982d89SJeremy L. Thompson   *size = qffield->size;
4057a982d89SJeremy L. Thompson   return 0;
4067a982d89SJeremy L. Thompson }
4077a982d89SJeremy L. Thompson 
4087a982d89SJeremy L. Thompson /**
4097a982d89SJeremy L. Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
4107a982d89SJeremy L. Thompson 
4117a982d89SJeremy L. Thompson   @param qffield         CeedQFunctionField
4127a982d89SJeremy L. Thompson   @param[out] emode      Variable to store the field evaluation mode
4137a982d89SJeremy L. Thompson 
4147a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4157a982d89SJeremy L. Thompson 
4167a982d89SJeremy L. Thompson   @ref Backend
4177a982d89SJeremy L. Thompson **/
4187a982d89SJeremy L. Thompson 
4197a982d89SJeremy L. Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qffield,
4207a982d89SJeremy L. Thompson                                   CeedEvalMode *emode) {
4217a982d89SJeremy L. Thompson   *emode = qffield->emode;
4227a982d89SJeremy L. Thompson   return 0;
4237a982d89SJeremy L. Thompson }
4247a982d89SJeremy L. Thompson 
4257a982d89SJeremy L. Thompson /// @}
4267a982d89SJeremy L. Thompson 
4277a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4287a982d89SJeremy L. Thompson /// CeedQFunction Public API
4297a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4307a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
4317a982d89SJeremy L. Thompson /// @{
4327a982d89SJeremy L. Thompson 
4337a982d89SJeremy L. Thompson /**
4347a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
4357a982d89SJeremy L. Thompson 
4367a982d89SJeremy L. Thompson   @param ceed       A Ceed object where the CeedQFunction will be created
4377a982d89SJeremy L. Thompson   @param vlength    Vector length. Caller must ensure that number of quadrature
4387a982d89SJeremy L. Thompson                       points is a multiple of vlength.
4397a982d89SJeremy L. Thompson   @param f          Function pointer to evaluate action at quadrature points.
4407a982d89SJeremy L. Thompson                       See \ref CeedQFunctionUser.
4417a982d89SJeremy L. Thompson   @param source     Absolute path to source of QFunction,
442c8d5b03cSJeremy L Thompson                       "\abs_path\file.h:function_name".
443c8d5b03cSJeremy L Thompson                       For support across all backends, this source must only
444c8d5b03cSJeremy L Thompson                       contain constructs supported by C99, C++11, and CUDA.
4457a982d89SJeremy L. Thompson   @param[out] qf    Address of the variable where the newly created
4467a982d89SJeremy L. Thompson                       CeedQFunction will be stored
4477a982d89SJeremy L. Thompson 
4487a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4497a982d89SJeremy L. Thompson 
4507a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
4517a982d89SJeremy L. Thompson     arguments.
4527a982d89SJeremy L. Thompson 
4537a982d89SJeremy L. Thompson   @ref User
4547a982d89SJeremy L. Thompson **/
4557a982d89SJeremy L. Thompson int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength, CeedQFunctionUser f,
4567a982d89SJeremy L. Thompson                                 const char *source, CeedQFunction *qf) {
4577a982d89SJeremy L. Thompson   int ierr;
4587a982d89SJeremy L. Thompson   char *source_copy;
4597a982d89SJeremy L. Thompson 
4607a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
4617a982d89SJeremy L. Thompson     Ceed delegate;
4627a982d89SJeremy L. Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
4637a982d89SJeremy L. Thompson 
4647a982d89SJeremy L. Thompson     if (!delegate)
4657a982d89SJeremy L. Thompson       // LCOV_EXCL_START
4667a982d89SJeremy L. Thompson       return CeedError(ceed, 1, "Backend does not support QFunctionCreate");
4677a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
4687a982d89SJeremy L. Thompson 
4697a982d89SJeremy L. Thompson     ierr = CeedQFunctionCreateInterior(delegate, vlength, f, source, qf);
4707a982d89SJeremy L. Thompson     CeedChk(ierr);
4717a982d89SJeremy L. Thompson     return 0;
4727a982d89SJeremy L. Thompson   }
4737a982d89SJeremy L. Thompson 
4747a982d89SJeremy L. Thompson   ierr = CeedCalloc(1, qf); CeedChk(ierr);
4757a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
4767a982d89SJeremy L. Thompson   ceed->refcount++;
4777a982d89SJeremy L. Thompson   (*qf)->refcount = 1;
4787a982d89SJeremy L. Thompson   (*qf)->vlength = vlength;
4797a982d89SJeremy L. Thompson   (*qf)->identity = 0;
4807a982d89SJeremy L. Thompson   (*qf)->function = f;
4817a982d89SJeremy L. Thompson   size_t slen = strlen(source) + 1;
4827a982d89SJeremy L. Thompson   ierr = CeedMalloc(slen, &source_copy); CeedChk(ierr);
4837a982d89SJeremy L. Thompson   memcpy(source_copy, source, slen);
4847a982d89SJeremy L. Thompson   (*qf)->sourcepath = source_copy;
4857a982d89SJeremy L. Thompson   ierr = CeedCalloc(16, &(*qf)->inputfields); CeedChk(ierr);
4867a982d89SJeremy L. Thompson   ierr = CeedCalloc(16, &(*qf)->outputfields); CeedChk(ierr);
4877a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
4887a982d89SJeremy L. Thompson   return 0;
4897a982d89SJeremy L. Thompson }
4907a982d89SJeremy L. Thompson 
4917a982d89SJeremy L. Thompson /**
492288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
493288c0443SJeremy L Thompson 
494288c0443SJeremy L Thompson   @param ceed       A Ceed object where the CeedQFunction will be created
495288c0443SJeremy L Thompson   @param name       Name of QFunction to use from gallery
496288c0443SJeremy L Thompson   @param[out] qf    Address of the variable where the newly created
497288c0443SJeremy L Thompson                       CeedQFunction will be stored
498288c0443SJeremy L Thompson 
499288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
500288c0443SJeremy L Thompson 
5017a982d89SJeremy L. Thompson   @ref User
502288c0443SJeremy L Thompson **/
503288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
504288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
505288c0443SJeremy L Thompson   int ierr;
506288c0443SJeremy L Thompson   size_t matchlen = 0, matchidx = UINT_MAX;
50775affc3bSjeremylt   char *name_copy;
508288c0443SJeremy L Thompson 
509288c0443SJeremy L Thompson   // Find matching backend
510288c0443SJeremy L Thompson   if (!name) return CeedError(NULL, 1, "No QFunction name provided");
511288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
512288c0443SJeremy L Thompson     size_t n;
513288c0443SJeremy L Thompson     const char *currname = qfunctions[i].name;
514288c0443SJeremy L Thompson     for (n = 0; currname[n] && currname[n] == name[n]; n++) {}
515288c0443SJeremy L Thompson     if (n > matchlen) {
516288c0443SJeremy L Thompson       matchlen = n;
517288c0443SJeremy L Thompson       matchidx = i;
518288c0443SJeremy L Thompson     }
519288c0443SJeremy L Thompson   }
5201d102b48SJeremy L Thompson   if (!matchlen)
521138d4072Sjeremylt     // LCOV_EXCL_START
5221d102b48SJeremy L Thompson     return CeedError(NULL, 1, "No suitable gallery QFunction");
523138d4072Sjeremylt   // LCOV_EXCL_STOP
524288c0443SJeremy L Thompson 
525288c0443SJeremy L Thompson   // Create QFunction
526288c0443SJeremy L Thompson   ierr = CeedQFunctionCreateInterior(ceed, qfunctions[matchidx].vlength,
527288c0443SJeremy L Thompson                                      qfunctions[matchidx].f,
528288c0443SJeremy L Thompson                                      qfunctions[matchidx].source, qf);
529288c0443SJeremy L Thompson   CeedChk(ierr);
530288c0443SJeremy L Thompson 
531288c0443SJeremy L Thompson   // QFunction specific setup
532288c0443SJeremy L Thompson   ierr = qfunctions[matchidx].init(ceed, name, *qf); CeedChk(ierr);
533288c0443SJeremy L Thompson 
53475affc3bSjeremylt   // Copy name
53575affc3bSjeremylt   size_t slen = strlen(name) + 1;
53675affc3bSjeremylt   ierr = CeedMalloc(slen, &name_copy); CeedChk(ierr);
53775affc3bSjeremylt   memcpy(name_copy, name, slen);
53875affc3bSjeremylt   (*qf)->qfname = name_copy;
53975affc3bSjeremylt 
540288c0443SJeremy L Thompson   return 0;
541288c0443SJeremy L Thompson }
542288c0443SJeremy L Thompson 
543288c0443SJeremy L Thompson /**
5440219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
5450219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
5460219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
5470219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
5480219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
5490219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
5500219ea01SJeremy L Thompson            location for both.
5510219ea01SJeremy L Thompson 
5520219ea01SJeremy L Thompson   @param ceed         A Ceed object where the CeedQFunction will be created
55360f77c51Sjeremylt   @param[in] size     Size of the qfunction fields
55460f77c51Sjeremylt   @param[in] inmode   CeedEvalMode for input to CeedQFunction
55560f77c51Sjeremylt   @param[in] outmode  CeedEvalMode for output to CeedQFunction
5560219ea01SJeremy L Thompson   @param[out] qf      Address of the variable where the newly created
5570219ea01SJeremy L Thompson                         CeedQFunction will be stored
5580219ea01SJeremy L Thompson 
5590219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5600219ea01SJeremy L Thompson 
5617a982d89SJeremy L. Thompson   @ref User
5620219ea01SJeremy L Thompson **/
56360f77c51Sjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode inmode,
56460f77c51Sjeremylt                                 CeedEvalMode outmode, CeedQFunction *qf) {
5650219ea01SJeremy L Thompson   int ierr;
5660219ea01SJeremy L Thompson 
56760f77c51Sjeremylt   if (inmode == CEED_EVAL_NONE && outmode == CEED_EVAL_NONE)
56860f77c51Sjeremylt     // LCOV_EXCL_START
56960f77c51Sjeremylt     return CeedError(ceed, 1, "CEED_EVAL_NONE for a both the input and "
57060f77c51Sjeremylt                      "output does not make sense with an identity QFunction");
57160f77c51Sjeremylt   // LCOV_EXCL_STOP
57260f77c51Sjeremylt 
5730219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
5741dfeea39SJeremy L Thompson   ierr = CeedQFunctionAddInput(*qf, "input", size, inmode); CeedChk(ierr);
5751dfeea39SJeremy L Thompson   ierr = CeedQFunctionAddOutput(*qf, "output", size, outmode); CeedChk(ierr);
5760219ea01SJeremy L Thompson 
5770219ea01SJeremy L Thompson   (*qf)->identity = 1;
5780219ea01SJeremy L Thompson   CeedInt *ctx;
5790219ea01SJeremy L Thompson   ierr = CeedCalloc(1, &ctx); CeedChk(ierr);
5800219ea01SJeremy L Thompson   ctx[0] = size;
5811dfeea39SJeremy L Thompson   ierr = CeedQFunctionSetContext(*qf, ctx, sizeof(*ctx)); CeedChk(ierr);
582*d99fa3c5SJeremy L Thompson   (*qf)->ctx_allocated = (*qf)->ctx;
5830219ea01SJeremy L Thompson 
5840219ea01SJeremy L Thompson   return 0;
5850219ea01SJeremy L Thompson }
5860219ea01SJeremy L Thompson 
5870219ea01SJeremy L Thompson /**
588a0a97fcfSJed Brown   @brief Add a CeedQFunction input
589b11c1e72Sjeremylt 
590b11c1e72Sjeremylt   @param qf         CeedQFunction
591b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
5924cc79fe7SJed Brown   @param size       Size of QFunction field, (ncomp * dim) for @ref CEED_EVAL_GRAD or
5934cc79fe7SJed Brown                       (ncomp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
594b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
595b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
596b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
597b11c1e72Sjeremylt 
598b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
599dfdf5a53Sjeremylt 
6007a982d89SJeremy L. Thompson   @ref User
601b11c1e72Sjeremylt **/
6021d102b48SJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname, CeedInt size,
6031d102b48SJeremy L Thompson                           CeedEvalMode emode) {
604fe2413ffSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->inputfields[qf->numinputfields],
6054d537eeaSYohann                                    fieldname, size, emode);
606fe2413ffSjeremylt   CeedChk(ierr);
607fe2413ffSjeremylt   qf->numinputfields++;
608d7b241e6Sjeremylt   return 0;
609d7b241e6Sjeremylt }
610d7b241e6Sjeremylt 
611b11c1e72Sjeremylt /**
612a0a97fcfSJed Brown   @brief Add a CeedQFunction output
613b11c1e72Sjeremylt 
614b11c1e72Sjeremylt   @param qf         CeedQFunction
615b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
6164cc79fe7SJed Brown   @param size       Size of QFunction field, (ncomp * dim) for @ref CEED_EVAL_GRAD or
6174cc79fe7SJed Brown                       (ncomp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
618b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
619b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
620b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
621b11c1e72Sjeremylt 
622b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
623dfdf5a53Sjeremylt 
6247a982d89SJeremy L. Thompson   @ref User
625b11c1e72Sjeremylt **/
626d7b241e6Sjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
6274d537eeaSYohann                            CeedInt size, CeedEvalMode emode) {
628d7b241e6Sjeremylt   if (emode == CEED_EVAL_WEIGHT)
629c042f62fSJeremy L Thompson     // LCOV_EXCL_START
6301d102b48SJeremy L Thompson     return CeedError(qf->ceed, 1, "Cannot create QFunction output with "
6311d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
632c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
633fe2413ffSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->outputfields[qf->numoutputfields],
6344d537eeaSYohann                                    fieldname, size, emode);
635fe2413ffSjeremylt   CeedChk(ierr);
636fe2413ffSjeremylt   qf->numoutputfields++;
637d7b241e6Sjeremylt   return 0;
638d7b241e6Sjeremylt }
639d7b241e6Sjeremylt 
640dfdf5a53Sjeremylt /**
6414ce2993fSjeremylt   @brief Set global context for a CeedQFunction
642b11c1e72Sjeremylt 
643b11c1e72Sjeremylt   @param qf       CeedQFunction
644b11c1e72Sjeremylt   @param ctx      Context data to set
645b11c1e72Sjeremylt   @param ctxsize  Size of context data values
646b11c1e72Sjeremylt 
647b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
648dfdf5a53Sjeremylt 
6497a982d89SJeremy L. Thompson   @ref User
650b11c1e72Sjeremylt **/
651d7b241e6Sjeremylt int CeedQFunctionSetContext(CeedQFunction qf, void *ctx, size_t ctxsize) {
652d7b241e6Sjeremylt   qf->ctx = ctx;
653d7b241e6Sjeremylt   qf->ctxsize = ctxsize;
654d7b241e6Sjeremylt   return 0;
655d7b241e6Sjeremylt }
656d7b241e6Sjeremylt 
657b11c1e72Sjeremylt /**
65875affc3bSjeremylt   @brief View a CeedQFunction
65975affc3bSjeremylt 
66075affc3bSjeremylt   @param[in] qf      CeedQFunction to view
66175affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
66275affc3bSjeremylt 
66375affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
66475affc3bSjeremylt 
6657a982d89SJeremy L. Thompson   @ref User
66675affc3bSjeremylt **/
66775affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
66875affc3bSjeremylt   int ierr;
66975affc3bSjeremylt 
67075affc3bSjeremylt   fprintf(stream, "%sCeedQFunction %s\n",
67175affc3bSjeremylt           qf->qfname ? "Gallery " : "User ", qf->qfname ? qf->qfname : "");
67275affc3bSjeremylt 
6732da88da5Sjeremylt   fprintf(stream, "  %d Input Field%s:\n", qf->numinputfields,
67475affc3bSjeremylt           qf->numinputfields>1 ? "s" : "");
67575affc3bSjeremylt   for (CeedInt i=0; i<qf->numinputfields; i++) {
6762da88da5Sjeremylt     ierr = CeedQFunctionFieldView(qf->inputfields[i], i, 1, stream);
6772da88da5Sjeremylt     CeedChk(ierr);
67875affc3bSjeremylt   }
67975affc3bSjeremylt 
6802da88da5Sjeremylt   fprintf(stream, "  %d Output Field%s:\n", qf->numoutputfields,
68175affc3bSjeremylt           qf->numoutputfields>1 ? "s" : "");
68275affc3bSjeremylt   for (CeedInt i=0; i<qf->numoutputfields; i++) {
6832da88da5Sjeremylt     ierr = CeedQFunctionFieldView(qf->outputfields[i], i, 0, stream);
68475affc3bSjeremylt     CeedChk(ierr);
68575affc3bSjeremylt   }
68675affc3bSjeremylt   return 0;
68775affc3bSjeremylt }
68875affc3bSjeremylt 
68975affc3bSjeremylt /**
690b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
691b11c1e72Sjeremylt 
692b11c1e72Sjeremylt   @param qf      CeedQFunction
693b11c1e72Sjeremylt   @param Q       Number of quadrature points
69434138859Sjeremylt   @param[in] u   Array of input CeedVectors
69534138859Sjeremylt   @param[out] v  Array of output CeedVectors
696b11c1e72Sjeremylt 
697b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
698dfdf5a53Sjeremylt 
6997a982d89SJeremy L. Thompson   @ref User
700b11c1e72Sjeremylt **/
701d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
702aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
703d7b241e6Sjeremylt   int ierr;
704d7b241e6Sjeremylt   if (!qf->Apply)
705c042f62fSJeremy L Thompson     // LCOV_EXCL_START
706d7b241e6Sjeremylt     return CeedError(qf->ceed, 1, "Backend does not support QFunctionApply");
707c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
708d7b241e6Sjeremylt   if (Q % qf->vlength)
709c042f62fSJeremy L Thompson     // LCOV_EXCL_START
7101d102b48SJeremy L Thompson     return CeedError(qf->ceed, 2, "Number of quadrature points %d must be a "
7111d102b48SJeremy L Thompson                      "multiple of %d", Q, qf->vlength);
712c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
713d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
714d7b241e6Sjeremylt   return 0;
715d7b241e6Sjeremylt }
716d7b241e6Sjeremylt 
717b11c1e72Sjeremylt /**
718b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
719b11c1e72Sjeremylt 
720b11c1e72Sjeremylt   @param qf CeedQFunction to destroy
721b11c1e72Sjeremylt 
722b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
723dfdf5a53Sjeremylt 
7247a982d89SJeremy L. Thompson   @ref User
725b11c1e72Sjeremylt **/
726d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
727d7b241e6Sjeremylt   int ierr;
728d7b241e6Sjeremylt 
7291d102b48SJeremy L Thompson   if (!*qf || --(*qf)->refcount > 0)
7301d102b48SJeremy L Thompson     return 0;
731fe2413ffSjeremylt   // Backend destroy
732d7b241e6Sjeremylt   if ((*qf)->Destroy) {
733d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
734d7b241e6Sjeremylt   }
735fe2413ffSjeremylt   // Free fields
736fe2413ffSjeremylt   for (int i=0; i<(*qf)->numinputfields; i++) {
737fe2413ffSjeremylt     ierr = CeedFree(&(*(*qf)->inputfields[i]).fieldname); CeedChk(ierr);
738fe2413ffSjeremylt     ierr = CeedFree(&(*qf)->inputfields[i]); CeedChk(ierr);
739fe2413ffSjeremylt   }
740fe2413ffSjeremylt   for (int i=0; i<(*qf)->numoutputfields; i++) {
741fe2413ffSjeremylt     ierr = CeedFree(&(*(*qf)->outputfields[i]).fieldname); CeedChk(ierr);
742fe2413ffSjeremylt     ierr = CeedFree(&(*qf)->outputfields[i]); CeedChk(ierr);
743fe2413ffSjeremylt   }
744fe2413ffSjeremylt   ierr = CeedFree(&(*qf)->inputfields); CeedChk(ierr);
745fe2413ffSjeremylt   ierr = CeedFree(&(*qf)->outputfields); CeedChk(ierr);
746*d99fa3c5SJeremy L Thompson   // Free ctx if owned by QFunction
747*d99fa3c5SJeremy L Thompson   ierr = CeedFree(&(*qf)->ctx_allocated); CeedChk(ierr);
748fe2413ffSjeremylt 
749288c0443SJeremy L Thompson   ierr = CeedFree(&(*qf)->sourcepath); CeedChk(ierr);
75075affc3bSjeremylt   ierr = CeedFree(&(*qf)->qfname); CeedChk(ierr);
751d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
752d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
753d7b241e6Sjeremylt   return 0;
754d7b241e6Sjeremylt }
755d7b241e6Sjeremylt 
756d7b241e6Sjeremylt /// @}
757