xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision ec3da8bcb94d9f0073544b37b5081a06981a86f7)
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 
17*ec3da8bcSJed Brown #include <ceed/ceed.h>
18*ec3da8bcSJed Brown #include <ceed/backend.h>
193d576824SJeremy L Thompson #include <ceed-impl.h>
20288c0443SJeremy L Thompson #include <limits.h>
213d576824SJeremy L Thompson #include <stdbool.h>
223d576824SJeremy L Thompson #include <stdio.h>
233d576824SJeremy L Thompson #include <string.h>
24288c0443SJeremy L Thompson 
257a982d89SJeremy L. Thompson /// @file
267a982d89SJeremy L. Thompson /// Implementation of public CeedQFunction interfaces
277a982d89SJeremy L. Thompson 
28288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP
29442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none;
30442e7f0bSjeremylt /// @endcond
31442e7f0bSjeremylt 
327a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
337a982d89SJeremy L. Thompson /// @{
347a982d89SJeremy L. Thompson 
357a982d89SJeremy L. Thompson // Indicate that no QFunction is provided by the user
367a982d89SJeremy L. Thompson const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
377a982d89SJeremy L. Thompson 
387a982d89SJeremy L. Thompson /// @}
397a982d89SJeremy L. Thompson 
40442e7f0bSjeremylt /// @cond DOXYGEN_SKIP
41288c0443SJeremy L Thompson static struct {
42288c0443SJeremy L Thompson   char name[CEED_MAX_RESOURCE_LEN];
43288c0443SJeremy L Thompson   char source[CEED_MAX_RESOURCE_LEN];
44288c0443SJeremy L Thompson   CeedInt vlength;
45288c0443SJeremy L Thompson   CeedQFunctionUser f;
46288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
47288c0443SJeremy L Thompson } qfunctions[1024];
48288c0443SJeremy L Thompson static size_t num_qfunctions;
49288c0443SJeremy L Thompson /// @endcond
50d7b241e6Sjeremylt 
51777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
52777ff853SJeremy L Thompson /// CeedQFunction Library Internal Functions
53777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
54777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper
55777ff853SJeremy L Thompson /// @{
56777ff853SJeremy L Thompson 
57b11c1e72Sjeremylt /**
58288c0443SJeremy L Thompson   @brief Register a gallery QFunction
59288c0443SJeremy L Thompson 
60288c0443SJeremy L Thompson   @param name     Name for this backend to respond to
611176cc3aSJeremy L Thompson   @param source   Absolute path to source of QFunction,
621176cc3aSJeremy L Thompson                     "\path\CEED_DIR\gallery\folder\file.h:function_name"
63288c0443SJeremy L Thompson   @param vlength  Vector length.  Caller must ensure that number of quadrature
64288c0443SJeremy L Thompson                     points is a multiple of vlength.
65288c0443SJeremy L Thompson   @param f        Function pointer to evaluate action at quadrature points.
66288c0443SJeremy L Thompson                     See \ref CeedQFunctionUser.
67288c0443SJeremy L Thompson   @param init     Initialization function called by CeedQFunctionInit() when the
68288c0443SJeremy L Thompson                     QFunction is selected.
69288c0443SJeremy L Thompson 
70288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
71288c0443SJeremy L Thompson 
727a982d89SJeremy L. Thompson   @ref Developer
73288c0443SJeremy L Thompson **/
74288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source,
75288c0443SJeremy L Thompson                           CeedInt vlength, CeedQFunctionUser f,
76288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
77c042f62fSJeremy L Thompson   if (num_qfunctions >= sizeof(qfunctions) / sizeof(qfunctions[0]))
78c042f62fSJeremy L Thompson     // LCOV_EXCL_START
79e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
80c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
81c042f62fSJeremy L Thompson 
82288c0443SJeremy L Thompson   strncpy(qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
83288c0443SJeremy L Thompson   qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
84288c0443SJeremy L Thompson   strncpy(qfunctions[num_qfunctions].source, source, CEED_MAX_RESOURCE_LEN);
85288c0443SJeremy L Thompson   qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
86288c0443SJeremy L Thompson   qfunctions[num_qfunctions].vlength = vlength;
87288c0443SJeremy L Thompson   qfunctions[num_qfunctions].f = f;
88288c0443SJeremy L Thompson   qfunctions[num_qfunctions].init = init;
89288c0443SJeremy L Thompson   num_qfunctions++;
90e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
91288c0443SJeremy L Thompson }
92288c0443SJeremy L Thompson 
93288c0443SJeremy L Thompson /**
947a982d89SJeremy L. Thompson   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
957a982d89SJeremy L. Thompson 
967a982d89SJeremy L. Thompson   @param f          CeedQFunctionField
977a982d89SJeremy L. Thompson   @param fieldname  Name of QFunction field
984cc79fe7SJed Brown   @param size       Size of QFunction field, (ncomp * dim) for @ref CEED_EVAL_GRAD or
994cc79fe7SJed Brown                       (ncomp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT
1007a982d89SJeremy L. Thompson   @param emode      \ref CEED_EVAL_NONE to use values directly,
1017a982d89SJeremy L. Thompson                       \ref CEED_EVAL_INTERP to use interpolated values,
1027a982d89SJeremy L. Thompson                       \ref CEED_EVAL_GRAD to use gradients,
1037a982d89SJeremy L. Thompson                       \ref CEED_EVAL_WEIGHT to use quadrature weights.
1047a982d89SJeremy L. Thompson 
1057a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1067a982d89SJeremy L. Thompson 
1077a982d89SJeremy L. Thompson   @ref Developer
1087a982d89SJeremy L. Thompson **/
1097a982d89SJeremy L. Thompson static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *fieldname,
1107a982d89SJeremy L. Thompson                                  CeedInt size, CeedEvalMode emode) {
1117a982d89SJeremy L. Thompson   size_t len = strlen(fieldname);
1127a982d89SJeremy L. Thompson   char *tmp;
1137a982d89SJeremy L. Thompson   int ierr;
1147a982d89SJeremy L. Thompson 
115e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, f); CeedChk(ierr);
1167a982d89SJeremy L. Thompson   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
1177a982d89SJeremy L. Thompson   memcpy(tmp, fieldname, len+1);
1187a982d89SJeremy L. Thompson   (*f)->fieldname = tmp;
1197a982d89SJeremy L. Thompson   (*f)->size = size;
1207a982d89SJeremy L. Thompson   (*f)->emode = emode;
121e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1227a982d89SJeremy L. Thompson }
1237a982d89SJeremy L. Thompson 
1247a982d89SJeremy L. Thompson /**
1257a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1267a982d89SJeremy L. Thompson 
1277a982d89SJeremy L. Thompson   @param[in] field        QFunction field to view
1287a982d89SJeremy L. Thompson   @param[in] fieldnumber  Number of field being viewed
1294c4400c7SValeria Barra   @param[in] in           true for input field, false for output
1307a982d89SJeremy L. Thompson   @param[in] stream       Stream to view to, e.g., stdout
1317a982d89SJeremy L. Thompson 
1327a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1337a982d89SJeremy L. Thompson 
1347a982d89SJeremy L. Thompson   @ref Utility
1357a982d89SJeremy L. Thompson **/
1367a982d89SJeremy L. Thompson static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt fieldnumber,
1377a982d89SJeremy L. Thompson                                   bool in, FILE *stream) {
1387a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1397a982d89SJeremy L. Thompson   fprintf(stream, "    %s Field [%d]:\n"
1407a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1417a982d89SJeremy L. Thompson           "      Size: %d\n"
1427a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1437a982d89SJeremy L. Thompson           inout, fieldnumber, field->fieldname, field->size,
1447a982d89SJeremy L. Thompson           CeedEvalModes[field->emode]);
145e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1467a982d89SJeremy L. Thompson }
1477a982d89SJeremy L. Thompson 
148777ff853SJeremy L Thompson /**
149777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
150777ff853SJeremy L Thompson 
151777ff853SJeremy L Thompson   @param qf                  CeedQFunction
152777ff853SJeremy L Thompson   @param status              Boolean value to set as Fortran status
153777ff853SJeremy L Thompson 
154777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
155777ff853SJeremy L Thompson 
156777ff853SJeremy L Thompson   @ref Backend
157777ff853SJeremy L Thompson **/
158777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
159777ff853SJeremy L Thompson   qf->fortranstatus = status;
160e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
161777ff853SJeremy L Thompson }
162777ff853SJeremy L Thompson 
1637a982d89SJeremy L. Thompson /// @}
1647a982d89SJeremy L. Thompson 
1657a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1667a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1677a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1687a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1697a982d89SJeremy L. Thompson /// @{
1707a982d89SJeremy L. Thompson 
1717a982d89SJeremy L. Thompson /**
1727a982d89SJeremy L. Thompson   @brief Get the Ceed associated with a CeedQFunction
1737a982d89SJeremy L. Thompson 
1747a982d89SJeremy L. Thompson   @param qf              CeedQFunction
1757a982d89SJeremy L. Thompson   @param[out] ceed       Variable to store Ceed
1767a982d89SJeremy L. Thompson 
1777a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1787a982d89SJeremy L. Thompson 
1797a982d89SJeremy L. Thompson   @ref Backend
1807a982d89SJeremy L. Thompson **/
1817a982d89SJeremy L. Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
1827a982d89SJeremy L. Thompson   *ceed = qf->ceed;
183e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1847a982d89SJeremy L. Thompson }
1857a982d89SJeremy L. Thompson 
1867a982d89SJeremy L. Thompson /**
1877a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1887a982d89SJeremy L. Thompson 
1897a982d89SJeremy L. Thompson   @param qf            CeedQFunction
1907a982d89SJeremy L. Thompson   @param[out] vlength  Variable to store vector length
1917a982d89SJeremy L. Thompson 
1927a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1937a982d89SJeremy L. Thompson 
1947a982d89SJeremy L. Thompson   @ref Backend
1957a982d89SJeremy L. Thompson **/
1967a982d89SJeremy L. Thompson int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength) {
1977a982d89SJeremy L. Thompson   *vlength = qf->vlength;
198e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1997a982d89SJeremy L. Thompson }
2007a982d89SJeremy L. Thompson 
2017a982d89SJeremy L. Thompson /**
2027a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
2037a982d89SJeremy L. Thompson 
2047a982d89SJeremy L. Thompson   @param qf              CeedQFunction
2057a982d89SJeremy L. Thompson   @param[out] numinput   Variable to store number of input fields
2067a982d89SJeremy L. Thompson   @param[out] numoutput  Variable to store number of output fields
2077a982d89SJeremy L. Thompson 
2087a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2097a982d89SJeremy L. Thompson 
2107a982d89SJeremy L. Thompson   @ref Backend
2117a982d89SJeremy L. Thompson **/
2127a982d89SJeremy L. Thompson int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput,
2137a982d89SJeremy L. Thompson                             CeedInt *numoutput) {
2147a982d89SJeremy L. Thompson   if (numinput) *numinput = qf->numinputfields;
2157a982d89SJeremy L. Thompson   if (numoutput) *numoutput = qf->numoutputfields;
216e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2177a982d89SJeremy L. Thompson }
2187a982d89SJeremy L. Thompson 
2197a982d89SJeremy L. Thompson /**
2207a982d89SJeremy L. Thompson   @brief Get the source path string for a CeedQFunction
2217a982d89SJeremy L. Thompson 
2227a982d89SJeremy L. Thompson   @param qf              CeedQFunction
2237a982d89SJeremy L. Thompson   @param[out] source     Variable to store source path string
2247a982d89SJeremy L. Thompson 
2257a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2267a982d89SJeremy L. Thompson 
2277a982d89SJeremy L. Thompson   @ref Backend
2287a982d89SJeremy L. Thompson **/
2297a982d89SJeremy L. Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source) {
2307a982d89SJeremy L. Thompson   *source = (char *) qf->sourcepath;
231e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2327a982d89SJeremy L. Thompson }
2337a982d89SJeremy L. Thompson 
2347a982d89SJeremy L. Thompson /**
2357a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2367a982d89SJeremy L. Thompson 
2377a982d89SJeremy L. Thompson   @param qf              CeedQFunction
2387a982d89SJeremy L. Thompson   @param[out] f          Variable to store user function
2397a982d89SJeremy L. Thompson 
2407a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2417a982d89SJeremy L. Thompson 
2427a982d89SJeremy L. Thompson   @ref Backend
2437a982d89SJeremy L. Thompson **/
2447a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
2457a982d89SJeremy L. Thompson   *f = qf->function;
246e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2477a982d89SJeremy L. Thompson }
2487a982d89SJeremy L. Thompson 
2497a982d89SJeremy L. Thompson /**
250777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
251777ff853SJeremy L Thompson          Note: For QFunctions from the Fortran interface, this
252777ff853SJeremy L Thompson                function will return the Fortran context
253777ff853SJeremy L Thompson                CeedQFunctionContext.
2547a982d89SJeremy L. Thompson 
2557a982d89SJeremy L. Thompson   @param qf              CeedQFunction
256777ff853SJeremy L Thompson   @param[out] ctx        Variable to store CeedQFunctionContext
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 **/
262777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
2637a982d89SJeremy L. Thompson   *ctx = qf->ctx;
264e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2657a982d89SJeremy L. Thompson }
2667a982d89SJeremy L. Thompson 
2677a982d89SJeremy L. Thompson /**
2687a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
269777ff853SJeremy L Thompson          Note: For all QFunctions this function will return the user
270777ff853SJeremy L Thompson                CeedQFunctionContext and not interface context
271777ff853SJeremy L Thompson                CeedQFunctionContext, if any such object exists.
2727a982d89SJeremy L. Thompson 
2737a982d89SJeremy L. Thompson   @param qf              CeedQFunction
274777ff853SJeremy L Thompson   @param[out] ctx        Variable to store CeedQFunctionContext
2757a982d89SJeremy L. Thompson 
2767a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2777a982d89SJeremy L. Thompson   @ref Backend
2787a982d89SJeremy L. Thompson **/
279777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
280777ff853SJeremy L Thompson   int ierr;
2817a982d89SJeremy L. Thompson   if (qf->fortranstatus) {
282777ff853SJeremy L Thompson     CeedFortranContext fctx = NULL;
283777ff853SJeremy L Thompson     ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fctx);
284777ff853SJeremy L Thompson     CeedChk(ierr);
2857a982d89SJeremy L. Thompson     *ctx = fctx->innerctx;
286777ff853SJeremy L Thompson     ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fctx); CeedChk(ierr);
2877a982d89SJeremy L. Thompson   } else {
2887a982d89SJeremy L. Thompson     *ctx = qf->ctx;
2897a982d89SJeremy L. Thompson   }
290e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2917a982d89SJeremy L. Thompson }
2927a982d89SJeremy L. Thompson 
2937a982d89SJeremy L. Thompson /**
2947a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
2957a982d89SJeremy L. Thompson 
2967a982d89SJeremy L. Thompson   @param qf               CeedQFunction
297fd364f38SJeremy L Thompson   @param[out] isidentity  Variable to store identity status
2987a982d89SJeremy L. Thompson 
2997a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3007a982d89SJeremy L. Thompson 
3017a982d89SJeremy L. Thompson   @ref Backend
3027a982d89SJeremy L. Thompson **/
303fd364f38SJeremy L Thompson int CeedQFunctionIsIdentity(CeedQFunction qf, bool *isidentity) {
304fd364f38SJeremy L Thompson   *isidentity = qf->identity;
305e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3067a982d89SJeremy L. Thompson }
3077a982d89SJeremy L. Thompson 
3087a982d89SJeremy L. Thompson /**
3097a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
3107a982d89SJeremy L. Thompson 
3117a982d89SJeremy L. Thompson   @param qf              CeedQFunction
3127a982d89SJeremy L. Thompson   @param[out] data       Variable to store data
3137a982d89SJeremy L. Thompson 
3147a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3157a982d89SJeremy L. Thompson 
3167a982d89SJeremy L. Thompson   @ref Backend
3177a982d89SJeremy L. Thompson **/
318777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
319777ff853SJeremy L Thompson   *(void **)data = qf->data;
320e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3217a982d89SJeremy L. Thompson }
3227a982d89SJeremy L. Thompson 
3237a982d89SJeremy L. Thompson /**
3247a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
3257a982d89SJeremy L. Thompson 
3267a982d89SJeremy L. Thompson   @param[out] qf         CeedQFunction
3277a982d89SJeremy L. Thompson   @param data            Data to set
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 **/
333777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
334777ff853SJeremy L Thompson   qf->data = data;
335e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3367a982d89SJeremy L. Thompson }
3377a982d89SJeremy L. Thompson 
3387a982d89SJeremy L. Thompson /**
3397a982d89SJeremy L. Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
3407a982d89SJeremy L. Thompson 
3417a982d89SJeremy L. Thompson   @param qf                 CeedQFunction
3427a982d89SJeremy L. Thompson   @param[out] inputfields   Variable to store inputfields
3437a982d89SJeremy L. Thompson   @param[out] outputfields  Variable to store outputfields
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 int CeedQFunctionGetFields(CeedQFunction qf, CeedQFunctionField **inputfields,
3507a982d89SJeremy L. Thompson                            CeedQFunctionField **outputfields) {
351e15f9bd0SJeremy L Thompson   if (inputfields) *inputfields = qf->inputfields;
352e15f9bd0SJeremy L Thompson   if (outputfields) *outputfields = qf->outputfields;
353e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3547a982d89SJeremy L. Thompson }
3557a982d89SJeremy L. Thompson 
3567a982d89SJeremy L. Thompson /**
3577a982d89SJeremy L. Thompson   @brief Get the name of a CeedQFunctionField
3587a982d89SJeremy L. Thompson 
3597a982d89SJeremy L. Thompson   @param qffield         CeedQFunctionField
3607a982d89SJeremy L. Thompson   @param[out] fieldname  Variable to store the field name
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 int CeedQFunctionFieldGetName(CeedQFunctionField qffield, char **fieldname) {
3677a982d89SJeremy L. Thompson   *fieldname = (char *)qffield->fieldname;
368e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3697a982d89SJeremy L. Thompson }
3707a982d89SJeremy L. Thompson 
3717a982d89SJeremy L. Thompson /**
3727a982d89SJeremy L. Thompson   @brief Get the number of components of a CeedQFunctionField
3737a982d89SJeremy L. Thompson 
3747a982d89SJeremy L. Thompson   @param qffield    CeedQFunctionField
3757a982d89SJeremy L. Thompson   @param[out] size  Variable to store the size of the field
3767a982d89SJeremy L. Thompson 
3777a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3787a982d89SJeremy L. Thompson 
3797a982d89SJeremy L. Thompson   @ref Backend
3807a982d89SJeremy L. Thompson **/
3817a982d89SJeremy L. Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qffield, CeedInt *size) {
3827a982d89SJeremy L. Thompson   *size = qffield->size;
383e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3847a982d89SJeremy L. Thompson }
3857a982d89SJeremy L. Thompson 
3867a982d89SJeremy L. Thompson /**
3877a982d89SJeremy L. Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
3887a982d89SJeremy L. Thompson 
3897a982d89SJeremy L. Thompson   @param qffield         CeedQFunctionField
3907a982d89SJeremy L. Thompson   @param[out] emode      Variable to store the field evaluation mode
3917a982d89SJeremy L. Thompson 
3927a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3937a982d89SJeremy L. Thompson 
3947a982d89SJeremy L. Thompson   @ref Backend
3957a982d89SJeremy L. Thompson **/
3967a982d89SJeremy L. Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qffield,
3977a982d89SJeremy L. Thompson                                   CeedEvalMode *emode) {
3987a982d89SJeremy L. Thompson   *emode = qffield->emode;
399e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4007a982d89SJeremy L. Thompson }
4017a982d89SJeremy L. Thompson 
4027a982d89SJeremy L. Thompson /// @}
4037a982d89SJeremy L. Thompson 
4047a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4057a982d89SJeremy L. Thompson /// CeedQFunction Public API
4067a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4077a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
4087a982d89SJeremy L. Thompson /// @{
4097a982d89SJeremy L. Thompson 
4107a982d89SJeremy L. Thompson /**
4117a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
4127a982d89SJeremy L. Thompson 
4137a982d89SJeremy L. Thompson   @param ceed       A Ceed object where the CeedQFunction will be created
4147a982d89SJeremy L. Thompson   @param vlength    Vector length. Caller must ensure that number of quadrature
4157a982d89SJeremy L. Thompson                       points is a multiple of vlength.
4167a982d89SJeremy L. Thompson   @param f          Function pointer to evaluate action at quadrature points.
4177a982d89SJeremy L. Thompson                       See \ref CeedQFunctionUser.
4187a982d89SJeremy L. Thompson   @param source     Absolute path to source of QFunction,
419c8d5b03cSJeremy L Thompson                       "\abs_path\file.h:function_name".
420c8d5b03cSJeremy L Thompson                       For support across all backends, this source must only
421c8d5b03cSJeremy L Thompson                       contain constructs supported by C99, C++11, and CUDA.
4227a982d89SJeremy L. Thompson   @param[out] qf    Address of the variable where the newly created
4237a982d89SJeremy L. Thompson                       CeedQFunction will be stored
4247a982d89SJeremy L. Thompson 
4257a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4267a982d89SJeremy L. Thompson 
4277a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
4287a982d89SJeremy L. Thompson     arguments.
4297a982d89SJeremy L. Thompson 
4307a982d89SJeremy L. Thompson   @ref User
4317a982d89SJeremy L. Thompson **/
4327a982d89SJeremy L. Thompson int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength, CeedQFunctionUser f,
4337a982d89SJeremy L. Thompson                                 const char *source, CeedQFunction *qf) {
4347a982d89SJeremy L. Thompson   int ierr;
4357a982d89SJeremy L. Thompson   char *source_copy;
4367a982d89SJeremy L. Thompson 
4377a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
4387a982d89SJeremy L. Thompson     Ceed delegate;
4397a982d89SJeremy L. Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
4407a982d89SJeremy L. Thompson 
4417a982d89SJeremy L. Thompson     if (!delegate)
4427a982d89SJeremy L. Thompson       // LCOV_EXCL_START
443e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
444e15f9bd0SJeremy L Thompson                        "Backend does not support QFunctionCreate");
4457a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
4467a982d89SJeremy L. Thompson 
4477a982d89SJeremy L. Thompson     ierr = CeedQFunctionCreateInterior(delegate, vlength, f, source, qf);
4487a982d89SJeremy L. Thompson     CeedChk(ierr);
449e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
4507a982d89SJeremy L. Thompson   }
4517a982d89SJeremy L. Thompson 
4527a982d89SJeremy L. Thompson   ierr = CeedCalloc(1, qf); CeedChk(ierr);
4537a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
4547a982d89SJeremy L. Thompson   ceed->refcount++;
4557a982d89SJeremy L. Thompson   (*qf)->refcount = 1;
4567a982d89SJeremy L. Thompson   (*qf)->vlength = vlength;
4577a982d89SJeremy L. Thompson   (*qf)->identity = 0;
4587a982d89SJeremy L. Thompson   (*qf)->function = f;
4597a982d89SJeremy L. Thompson   size_t slen = strlen(source) + 1;
4607a982d89SJeremy L. Thompson   ierr = CeedMalloc(slen, &source_copy); CeedChk(ierr);
4617a982d89SJeremy L. Thompson   memcpy(source_copy, source, slen);
4627a982d89SJeremy L. Thompson   (*qf)->sourcepath = source_copy;
4637a982d89SJeremy L. Thompson   ierr = CeedCalloc(16, &(*qf)->inputfields); CeedChk(ierr);
4647a982d89SJeremy L. Thompson   ierr = CeedCalloc(16, &(*qf)->outputfields); CeedChk(ierr);
4657a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
466e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4677a982d89SJeremy L. Thompson }
4687a982d89SJeremy L. Thompson 
4697a982d89SJeremy L. Thompson /**
470288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
471288c0443SJeremy L Thompson 
472288c0443SJeremy L Thompson   @param ceed       A Ceed object where the CeedQFunction will be created
473288c0443SJeremy L Thompson   @param name       Name of QFunction to use from gallery
474288c0443SJeremy L Thompson   @param[out] qf    Address of the variable where the newly created
475288c0443SJeremy L Thompson                       CeedQFunction will be stored
476288c0443SJeremy L Thompson 
477288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
478288c0443SJeremy L Thompson 
4797a982d89SJeremy L. Thompson   @ref User
480288c0443SJeremy L Thompson **/
481288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
482288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
483288c0443SJeremy L Thompson   int ierr;
484288c0443SJeremy L Thompson   size_t matchlen = 0, matchidx = UINT_MAX;
48575affc3bSjeremylt   char *name_copy;
486288c0443SJeremy L Thompson 
4871d013790SJed Brown   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
488288c0443SJeremy L Thompson   // Find matching backend
489e15f9bd0SJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE,
490e15f9bd0SJeremy L Thompson                                 "No QFunction name provided");
491288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
492288c0443SJeremy L Thompson     size_t n;
493288c0443SJeremy L Thompson     const char *currname = qfunctions[i].name;
494288c0443SJeremy L Thompson     for (n = 0; currname[n] && currname[n] == name[n]; n++) {}
495288c0443SJeremy L Thompson     if (n > matchlen) {
496288c0443SJeremy L Thompson       matchlen = n;
497288c0443SJeremy L Thompson       matchidx = i;
498288c0443SJeremy L Thompson     }
499288c0443SJeremy L Thompson   }
5001d102b48SJeremy L Thompson   if (!matchlen)
501138d4072Sjeremylt     // LCOV_EXCL_START
502e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
503138d4072Sjeremylt   // LCOV_EXCL_STOP
504288c0443SJeremy L Thompson 
505288c0443SJeremy L Thompson   // Create QFunction
506288c0443SJeremy L Thompson   ierr = CeedQFunctionCreateInterior(ceed, qfunctions[matchidx].vlength,
507288c0443SJeremy L Thompson                                      qfunctions[matchidx].f,
508288c0443SJeremy L Thompson                                      qfunctions[matchidx].source, qf);
509288c0443SJeremy L Thompson   CeedChk(ierr);
510288c0443SJeremy L Thompson 
511288c0443SJeremy L Thompson   // QFunction specific setup
512288c0443SJeremy L Thompson   ierr = qfunctions[matchidx].init(ceed, name, *qf); CeedChk(ierr);
513288c0443SJeremy L Thompson 
51475affc3bSjeremylt   // Copy name
51575affc3bSjeremylt   size_t slen = strlen(name) + 1;
51675affc3bSjeremylt   ierr = CeedMalloc(slen, &name_copy); CeedChk(ierr);
51775affc3bSjeremylt   memcpy(name_copy, name, slen);
51875affc3bSjeremylt   (*qf)->qfname = name_copy;
519e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
520288c0443SJeremy L Thompson }
521288c0443SJeremy L Thompson 
522288c0443SJeremy L Thompson /**
5230219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
5240219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
5250219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
5260219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
5270219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
5280219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
5290219ea01SJeremy L Thompson            location for both.
5300219ea01SJeremy L Thompson 
5310219ea01SJeremy L Thompson   @param ceed         A Ceed object where the CeedQFunction will be created
53260f77c51Sjeremylt   @param[in] size     Size of the qfunction fields
53360f77c51Sjeremylt   @param[in] inmode   CeedEvalMode for input to CeedQFunction
53460f77c51Sjeremylt   @param[in] outmode  CeedEvalMode for output to CeedQFunction
5350219ea01SJeremy L Thompson   @param[out] qf      Address of the variable where the newly created
5360219ea01SJeremy L Thompson                         CeedQFunction will be stored
5370219ea01SJeremy L Thompson 
5380219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5390219ea01SJeremy L Thompson 
5407a982d89SJeremy L. Thompson   @ref User
5410219ea01SJeremy L Thompson **/
54260f77c51Sjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode inmode,
54360f77c51Sjeremylt                                 CeedEvalMode outmode, CeedQFunction *qf) {
5440219ea01SJeremy L Thompson   int ierr;
5450219ea01SJeremy L Thompson 
54660f77c51Sjeremylt   if (inmode == CEED_EVAL_NONE && outmode == CEED_EVAL_NONE)
54760f77c51Sjeremylt     // LCOV_EXCL_START
548e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
549e15f9bd0SJeremy L Thompson                      "CEED_EVAL_NONE for a both the input and "
55060f77c51Sjeremylt                      "output does not make sense with an identity QFunction");
55160f77c51Sjeremylt   // LCOV_EXCL_STOP
55260f77c51Sjeremylt 
5530219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
5541dfeea39SJeremy L Thompson   ierr = CeedQFunctionAddInput(*qf, "input", size, inmode); CeedChk(ierr);
5551dfeea39SJeremy L Thompson   ierr = CeedQFunctionAddOutput(*qf, "output", size, outmode); CeedChk(ierr);
5560219ea01SJeremy L Thompson 
5570219ea01SJeremy L Thompson   (*qf)->identity = 1;
558777ff853SJeremy L Thompson   CeedInt *sizeData;
559777ff853SJeremy L Thompson   ierr = CeedCalloc(1, &sizeData); CeedChk(ierr);
560777ff853SJeremy L Thompson   sizeData[0] = size;
561777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
562777ff853SJeremy L Thompson   ierr = CeedQFunctionContextCreate(ceed, &ctx); CeedChk(ierr);
563777ff853SJeremy L Thompson   ierr = CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_OWN_POINTER,
564777ff853SJeremy L Thompson                                      sizeof(*sizeData), (void *)sizeData);
565777ff853SJeremy L Thompson   CeedChk(ierr);
566777ff853SJeremy L Thompson   ierr = CeedQFunctionSetContext(*qf, ctx); CeedChk(ierr);
567777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&ctx); CeedChk(ierr);
568e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5690219ea01SJeremy L Thompson }
5700219ea01SJeremy L Thompson 
5710219ea01SJeremy L Thompson /**
572a0a97fcfSJed Brown   @brief Add a CeedQFunction input
573b11c1e72Sjeremylt 
574b11c1e72Sjeremylt   @param qf         CeedQFunction
575b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
5764cc79fe7SJed Brown   @param size       Size of QFunction field, (ncomp * dim) for @ref CEED_EVAL_GRAD or
5774cc79fe7SJed Brown                       (ncomp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
578b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
579b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
580b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
581b11c1e72Sjeremylt 
582b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
583dfdf5a53Sjeremylt 
5847a982d89SJeremy L. Thompson   @ref User
585b11c1e72Sjeremylt **/
5861d102b48SJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname, CeedInt size,
5871d102b48SJeremy L Thompson                           CeedEvalMode emode) {
588e15f9bd0SJeremy L Thompson   if (qf->operatorsset)
589e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
590e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
591e15f9bd0SJeremy L Thompson                      "QFunction cannot be changed when in use by an operator");
592e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
593e15f9bd0SJeremy L Thompson   if ((emode == CEED_EVAL_WEIGHT) && (size != 1))
594e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
595e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
596e15f9bd0SJeremy L Thompson                      "CEED_EVAL_WEIGHT should have size 1");
597e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
598fe2413ffSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->inputfields[qf->numinputfields],
5994d537eeaSYohann                                    fieldname, size, emode);
600fe2413ffSjeremylt   CeedChk(ierr);
601fe2413ffSjeremylt   qf->numinputfields++;
602e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
603d7b241e6Sjeremylt }
604d7b241e6Sjeremylt 
605b11c1e72Sjeremylt /**
606a0a97fcfSJed Brown   @brief Add a CeedQFunction output
607b11c1e72Sjeremylt 
608b11c1e72Sjeremylt   @param qf         CeedQFunction
609b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
6104cc79fe7SJed Brown   @param size       Size of QFunction field, (ncomp * dim) for @ref CEED_EVAL_GRAD or
6114cc79fe7SJed Brown                       (ncomp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
612b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
613b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
614b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
615b11c1e72Sjeremylt 
616b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
617dfdf5a53Sjeremylt 
6187a982d89SJeremy L. Thompson   @ref User
619b11c1e72Sjeremylt **/
620d7b241e6Sjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
6214d537eeaSYohann                            CeedInt size, CeedEvalMode emode) {
622e15f9bd0SJeremy L Thompson   if (qf->operatorsset)
623e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
624e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
625e15f9bd0SJeremy L Thompson                      "QFunction cannot be changed when in use by an operator");
626e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
627d7b241e6Sjeremylt   if (emode == CEED_EVAL_WEIGHT)
628c042f62fSJeremy L Thompson     // LCOV_EXCL_START
629e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
630e15f9bd0SJeremy L Thompson                      "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++;
637e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
638d7b241e6Sjeremylt }
639d7b241e6Sjeremylt 
640dfdf5a53Sjeremylt /**
6414ce2993fSjeremylt   @brief Set global context for a CeedQFunction
642b11c1e72Sjeremylt 
643b11c1e72Sjeremylt   @param qf       CeedQFunction
644b11c1e72Sjeremylt   @param ctx      Context data to set
645b11c1e72Sjeremylt 
646b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
647dfdf5a53Sjeremylt 
6487a982d89SJeremy L. Thompson   @ref User
649b11c1e72Sjeremylt **/
650777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
651d7b241e6Sjeremylt   qf->ctx = ctx;
652777ff853SJeremy L Thompson   ctx->refcount++;
653e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
654d7b241e6Sjeremylt }
655d7b241e6Sjeremylt 
656b11c1e72Sjeremylt /**
65775affc3bSjeremylt   @brief View a CeedQFunction
65875affc3bSjeremylt 
65975affc3bSjeremylt   @param[in] qf      CeedQFunction to view
66075affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
66175affc3bSjeremylt 
66275affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
66375affc3bSjeremylt 
6647a982d89SJeremy L. Thompson   @ref User
66575affc3bSjeremylt **/
66675affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
66775affc3bSjeremylt   int ierr;
66875affc3bSjeremylt 
66975affc3bSjeremylt   fprintf(stream, "%sCeedQFunction %s\n",
67075affc3bSjeremylt           qf->qfname ? "Gallery " : "User ", qf->qfname ? qf->qfname : "");
67175affc3bSjeremylt 
6722da88da5Sjeremylt   fprintf(stream, "  %d Input Field%s:\n", qf->numinputfields,
67375affc3bSjeremylt           qf->numinputfields>1 ? "s" : "");
67475affc3bSjeremylt   for (CeedInt i=0; i<qf->numinputfields; i++) {
6752da88da5Sjeremylt     ierr = CeedQFunctionFieldView(qf->inputfields[i], i, 1, stream);
6762da88da5Sjeremylt     CeedChk(ierr);
67775affc3bSjeremylt   }
67875affc3bSjeremylt 
6792da88da5Sjeremylt   fprintf(stream, "  %d Output Field%s:\n", qf->numoutputfields,
68075affc3bSjeremylt           qf->numoutputfields>1 ? "s" : "");
68175affc3bSjeremylt   for (CeedInt i=0; i<qf->numoutputfields; i++) {
6822da88da5Sjeremylt     ierr = CeedQFunctionFieldView(qf->outputfields[i], i, 0, stream);
68375affc3bSjeremylt     CeedChk(ierr);
68475affc3bSjeremylt   }
685e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
68675affc3bSjeremylt }
68775affc3bSjeremylt 
68875affc3bSjeremylt /**
689b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
690b11c1e72Sjeremylt 
691b11c1e72Sjeremylt   @param qf      CeedQFunction
692b11c1e72Sjeremylt   @param Q       Number of quadrature points
69334138859Sjeremylt   @param[in] u   Array of input CeedVectors
69434138859Sjeremylt   @param[out] v  Array of output CeedVectors
695b11c1e72Sjeremylt 
696b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
697dfdf5a53Sjeremylt 
6987a982d89SJeremy L. Thompson   @ref User
699b11c1e72Sjeremylt **/
700d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
701aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
702d7b241e6Sjeremylt   int ierr;
703d7b241e6Sjeremylt   if (!qf->Apply)
704c042f62fSJeremy L Thompson     // LCOV_EXCL_START
705e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED,
706e15f9bd0SJeremy L Thompson                      "Backend does not support QFunctionApply");
707c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
708d7b241e6Sjeremylt   if (Q % qf->vlength)
709c042f62fSJeremy L Thompson     // LCOV_EXCL_START
710e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
711e15f9bd0SJeremy L Thompson                      "Number of quadrature points %d must be a "
7121d102b48SJeremy L Thompson                      "multiple of %d", Q, qf->vlength);
713c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
714d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
715e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
716d7b241e6Sjeremylt }
717d7b241e6Sjeremylt 
718b11c1e72Sjeremylt /**
719b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
720b11c1e72Sjeremylt 
721b11c1e72Sjeremylt   @param qf CeedQFunction to destroy
722b11c1e72Sjeremylt 
723b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
724dfdf5a53Sjeremylt 
7257a982d89SJeremy L. Thompson   @ref User
726b11c1e72Sjeremylt **/
727d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
728d7b241e6Sjeremylt   int ierr;
729d7b241e6Sjeremylt 
730e15f9bd0SJeremy L Thompson   if (!*qf || --(*qf)->refcount > 0) return CEED_ERROR_SUCCESS;
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);
746777ff853SJeremy L Thompson 
747777ff853SJeremy L Thompson   // User context data object
748777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
749fe2413ffSjeremylt 
750288c0443SJeremy L Thompson   ierr = CeedFree(&(*qf)->sourcepath); CeedChk(ierr);
75175affc3bSjeremylt   ierr = CeedFree(&(*qf)->qfname); CeedChk(ierr);
752d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
753d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
754e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
755d7b241e6Sjeremylt }
756d7b241e6Sjeremylt 
757d7b241e6Sjeremylt /// @}
758