xref: /libCEED/interface/ceed-qfunction.c (revision 442e7f0bef50859918b48bfcea0f6ad2d729e89e)
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
23*442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none;
24*442e7f0bSjeremylt /// @endcond
25*442e7f0bSjeremylt 
26*442e7f0bSjeremylt /// @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;
151288c0443SJeremy L Thompson 
152288c0443SJeremy L Thompson   // Find matching backend
153288c0443SJeremy L Thompson   if (!name) return CeedError(NULL, 1, "No QFunction name provided");
154288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
155288c0443SJeremy L Thompson     size_t n;
156288c0443SJeremy L Thompson     const char *currname = qfunctions[i].name;
157288c0443SJeremy L Thompson     for (n = 0; currname[n] && currname[n] == name[n]; n++) {}
158288c0443SJeremy L Thompson     if (n > matchlen) {
159288c0443SJeremy L Thompson       matchlen = n;
160288c0443SJeremy L Thompson       matchidx = i;
161288c0443SJeremy L Thompson     }
162288c0443SJeremy L Thompson   }
1631d102b48SJeremy L Thompson   if (!matchlen)
164138d4072Sjeremylt     // LCOV_EXCL_START
1651d102b48SJeremy L Thompson     return CeedError(NULL, 1, "No suitable gallery QFunction");
166138d4072Sjeremylt   // LCOV_EXCL_STOP
167288c0443SJeremy L Thompson 
168288c0443SJeremy L Thompson   // Create QFunction
169288c0443SJeremy L Thompson   ierr = CeedQFunctionCreateInterior(ceed, qfunctions[matchidx].vlength,
170288c0443SJeremy L Thompson                                      qfunctions[matchidx].f,
171288c0443SJeremy L Thompson                                      qfunctions[matchidx].source, qf);
172288c0443SJeremy L Thompson   CeedChk(ierr);
173288c0443SJeremy L Thompson 
174288c0443SJeremy L Thompson   // QFunction specific setup
175288c0443SJeremy L Thompson   ierr = qfunctions[matchidx].init(ceed, name, *qf); CeedChk(ierr);
176288c0443SJeremy L Thompson 
177288c0443SJeremy L Thompson   return 0;
178288c0443SJeremy L Thompson }
179288c0443SJeremy L Thompson 
180288c0443SJeremy L Thompson /**
1810219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
1820219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
1830219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
1840219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
1850219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
1860219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
1870219ea01SJeremy L Thompson            location for both.
1880219ea01SJeremy L Thompson 
1890219ea01SJeremy L Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
19060f77c51Sjeremylt   @param[in] size    Size of the qfunction fields
19160f77c51Sjeremylt   @param[in] inmode  CeedEvalMode for input to CeedQFunction
19260f77c51Sjeremylt   @param[in] outmode CeedEvalMode for output to CeedQFunction
1930219ea01SJeremy L Thompson   @param[out] qf     Address of the variable where the newly created
1940219ea01SJeremy L Thompson                        CeedQFunction will be stored
1950219ea01SJeremy L Thompson 
1960219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1970219ea01SJeremy L Thompson 
1980219ea01SJeremy L Thompson   @ref Basic
1990219ea01SJeremy L Thompson **/
20060f77c51Sjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode inmode,
20160f77c51Sjeremylt                                 CeedEvalMode outmode, CeedQFunction *qf) {
2020219ea01SJeremy L Thompson   int ierr;
2030219ea01SJeremy L Thompson 
20460f77c51Sjeremylt   if (inmode == CEED_EVAL_NONE && outmode == CEED_EVAL_NONE)
20560f77c51Sjeremylt     // LCOV_EXCL_START
20660f77c51Sjeremylt     return CeedError(ceed, 1, "CEED_EVAL_NONE for a both the input and "
20760f77c51Sjeremylt                      "output does not make sense with an identity QFunction");
20860f77c51Sjeremylt   // LCOV_EXCL_STOP
20960f77c51Sjeremylt 
2100219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
21160f77c51Sjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", 1, inmode); CeedChk(ierr);
21260f77c51Sjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", 1, outmode); CeedChk(ierr);
2130219ea01SJeremy L Thompson 
2140219ea01SJeremy L Thompson   (*qf)->identity = 1;
2150219ea01SJeremy L Thompson   if (size > 1) {
2160219ea01SJeremy L Thompson     CeedInt *ctx;
2170219ea01SJeremy L Thompson     ierr = CeedCalloc(1, &ctx); CeedChk(ierr);
2180219ea01SJeremy L Thompson     ctx[0] = size;
2190219ea01SJeremy L Thompson     ierr = CeedQFunctionSetContext(*qf, ctx, sizeof(ctx)); CeedChk(ierr);
220c0fea178Sjeremylt     (*qf)->inputfields[0]->size = size;
221c0fea178Sjeremylt     (*qf)->outputfields[0]->size = size;
2220219ea01SJeremy L Thompson   }
2230219ea01SJeremy L Thompson 
2240219ea01SJeremy L Thompson   return 0;
2250219ea01SJeremy L Thompson }
2260219ea01SJeremy L Thompson 
2270219ea01SJeremy L Thompson /**
228a0a97fcfSJed Brown   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
229b11c1e72Sjeremylt 
230b11c1e72Sjeremylt   @param f          CeedQFunctionField
231b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
2324d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
23323e8ed12Sjeremylt                       1 for CEED_EVAL_NONE, CEED_EVAL_INTERP, and CEED_EVAL_WEIGHT)
234b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
235b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
23623e8ed12Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients,
23723e8ed12Sjeremylt                       \ref CEED_EVAL_WEIGHT to use quadrature weights.
238b11c1e72Sjeremylt 
239b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
240dfdf5a53Sjeremylt 
241dfdf5a53Sjeremylt   @ref Developer
242b11c1e72Sjeremylt **/
243fe2413ffSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *fieldname,
2444d537eeaSYohann                                  CeedInt size, CeedEvalMode emode) {
245d7b241e6Sjeremylt   size_t len = strlen(fieldname);
246d7b241e6Sjeremylt   char *tmp;
247fe2413ffSjeremylt   int ierr;
248fe2413ffSjeremylt   ierr = CeedCalloc(1,f); CeedChk(ierr);
249fe2413ffSjeremylt 
250fe2413ffSjeremylt   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
251d7b241e6Sjeremylt   memcpy(tmp, fieldname, len+1);
252fe2413ffSjeremylt   (*f)->fieldname = tmp;
2534d537eeaSYohann   (*f)->size = size;
254fe2413ffSjeremylt   (*f)->emode = emode;
255d7b241e6Sjeremylt   return 0;
256d7b241e6Sjeremylt }
257d7b241e6Sjeremylt 
258b11c1e72Sjeremylt /**
259a0a97fcfSJed Brown   @brief Add a CeedQFunction input
260b11c1e72Sjeremylt 
261b11c1e72Sjeremylt   @param qf         CeedQFunction
262b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
2634d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
2644d537eeaSYohann                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
265b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
266b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
267b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
268b11c1e72Sjeremylt 
269b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
270dfdf5a53Sjeremylt 
271dfdf5a53Sjeremylt   @ref Basic
272b11c1e72Sjeremylt **/
2731d102b48SJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname, CeedInt size,
2741d102b48SJeremy L Thompson                           CeedEvalMode emode) {
275fe2413ffSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->inputfields[qf->numinputfields],
2764d537eeaSYohann                                    fieldname, size, emode);
277fe2413ffSjeremylt   CeedChk(ierr);
278fe2413ffSjeremylt   qf->numinputfields++;
279d7b241e6Sjeremylt   return 0;
280d7b241e6Sjeremylt }
281d7b241e6Sjeremylt 
282b11c1e72Sjeremylt /**
283a0a97fcfSJed Brown   @brief Add a CeedQFunction output
284b11c1e72Sjeremylt 
285b11c1e72Sjeremylt   @param qf         CeedQFunction
286b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
2874d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
2884d537eeaSYohann                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
289b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
290b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
291b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
292b11c1e72Sjeremylt 
293b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
294dfdf5a53Sjeremylt 
295dfdf5a53Sjeremylt   @ref Basic
296b11c1e72Sjeremylt **/
297d7b241e6Sjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
2984d537eeaSYohann                            CeedInt size, CeedEvalMode emode) {
299d7b241e6Sjeremylt   if (emode == CEED_EVAL_WEIGHT)
300c042f62fSJeremy L Thompson     // LCOV_EXCL_START
3011d102b48SJeremy L Thompson     return CeedError(qf->ceed, 1, "Cannot create QFunction output with "
3021d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
303c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
304fe2413ffSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->outputfields[qf->numoutputfields],
3054d537eeaSYohann                                    fieldname, size, emode);
306fe2413ffSjeremylt   CeedChk(ierr);
307fe2413ffSjeremylt   qf->numoutputfields++;
308d7b241e6Sjeremylt   return 0;
309d7b241e6Sjeremylt }
310d7b241e6Sjeremylt 
311dfdf5a53Sjeremylt /**
3124ce2993fSjeremylt   @brief Get the Ceed associated with a CeedQFunction
3134ce2993fSjeremylt 
3144ce2993fSjeremylt   @param qf              CeedQFunction
3154ce2993fSjeremylt   @param[out] ceed       Variable to store Ceed
3164ce2993fSjeremylt 
3174ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3184ce2993fSjeremylt 
31923617272Sjeremylt   @ref Advanced
3204ce2993fSjeremylt **/
3214ce2993fSjeremylt 
3224ce2993fSjeremylt int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
3234ce2993fSjeremylt   *ceed = qf->ceed;
3244ce2993fSjeremylt   return 0;
3254ce2993fSjeremylt }
3264ce2993fSjeremylt 
3274ce2993fSjeremylt /**
3284ce2993fSjeremylt   @brief Get the vector length of a CeedQFunction
3294ce2993fSjeremylt 
3304ce2993fSjeremylt   @param qf            CeedQFunction
331288c0443SJeremy L Thompson   @param[out] vlength  Variable to store vector length
3324ce2993fSjeremylt 
3334ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3344ce2993fSjeremylt 
33523617272Sjeremylt   @ref Advanced
3364ce2993fSjeremylt **/
3374ce2993fSjeremylt 
3384ce2993fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength) {
3394ce2993fSjeremylt   *vlength = qf->vlength;
3404ce2993fSjeremylt   return 0;
3414ce2993fSjeremylt }
3424ce2993fSjeremylt 
3434ce2993fSjeremylt /**
344dfdf5a53Sjeremylt   @brief Get the number of inputs and outputs to a CeedQFunction
345dfdf5a53Sjeremylt 
346dfdf5a53Sjeremylt   @param qf              CeedQFunction
3474ce2993fSjeremylt   @param[out] numinput   Variable to store number of input fields
3484ce2993fSjeremylt   @param[out] numoutput  Variable to store number of output fields
349dfdf5a53Sjeremylt 
350dfdf5a53Sjeremylt   @return An error code: 0 - success, otherwise - failure
351dfdf5a53Sjeremylt 
35223617272Sjeremylt   @ref Advanced
353dfdf5a53Sjeremylt **/
354dfdf5a53Sjeremylt 
355d7b241e6Sjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput,
356d7b241e6Sjeremylt                             CeedInt *numoutput) {
3571a4ead9bSjeremylt   if (numinput) *numinput = qf->numinputfields;
3581a4ead9bSjeremylt   if (numoutput) *numoutput = qf->numoutputfields;
359d7b241e6Sjeremylt   return 0;
360d7b241e6Sjeremylt }
361d7b241e6Sjeremylt 
362d7b241e6Sjeremylt /**
363288c0443SJeremy L Thompson   @brief Get the source path string for a CeedQFunction
3644ce2993fSjeremylt 
3654ce2993fSjeremylt   @param qf              CeedQFunction
366288c0443SJeremy L Thompson   @param[out] source     Variable to store source path string
3674ce2993fSjeremylt 
3684ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3694ce2993fSjeremylt 
37023617272Sjeremylt   @ref Advanced
3714ce2993fSjeremylt **/
3724ce2993fSjeremylt 
373288c0443SJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source) {
374288c0443SJeremy L Thompson   *source = (char *) qf->sourcepath;
3754ce2993fSjeremylt   return 0;
3764ce2993fSjeremylt }
3774ce2993fSjeremylt 
3784ce2993fSjeremylt /**
379fe2413ffSjeremylt   @brief Get the User Function for a CeedQFunction
380fe2413ffSjeremylt 
381fe2413ffSjeremylt   @param qf              CeedQFunction
382fe2413ffSjeremylt   @param[out] f          Variable to store user function
383fe2413ffSjeremylt 
384fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
385fe2413ffSjeremylt 
386fe2413ffSjeremylt   @ref Advanced
387fe2413ffSjeremylt **/
388fe2413ffSjeremylt 
389d4f68153Sjeremylt int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
390d4f68153Sjeremylt   *f = qf->function;
391fe2413ffSjeremylt   return 0;
392fe2413ffSjeremylt }
393fe2413ffSjeremylt 
394fe2413ffSjeremylt /**
3954ce2993fSjeremylt   @brief Get global context size for a CeedQFunction
3964ce2993fSjeremylt 
3974ce2993fSjeremylt   @param qf              CeedQFunction
3984ce2993fSjeremylt   @param[out] ctxsize    Variable to store size of context data values
3994ce2993fSjeremylt 
4004ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
4014ce2993fSjeremylt 
40223617272Sjeremylt   @ref Advanced
4034ce2993fSjeremylt **/
4044ce2993fSjeremylt 
4054ce2993fSjeremylt int CeedQFunctionGetContextSize(CeedQFunction qf, size_t *ctxsize) {
406069aeabaSjeremylt   if (qf->fortranstatus) {
407069aeabaSjeremylt     fContext *fctx = qf->ctx;
408069aeabaSjeremylt     *ctxsize = fctx->innerctxsize;
409069aeabaSjeremylt   } else {
4104ce2993fSjeremylt     *ctxsize = qf->ctxsize;
411069aeabaSjeremylt   }
4124ce2993fSjeremylt   return 0;
4134ce2993fSjeremylt }
4144ce2993fSjeremylt 
4154ce2993fSjeremylt /**
4164ce2993fSjeremylt   @brief Get global context for a CeedQFunction
4174ce2993fSjeremylt 
4184ce2993fSjeremylt   @param qf              CeedQFunction
4194ce2993fSjeremylt   @param[out] ctx        Variable to store context data values
4204ce2993fSjeremylt 
4214ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
4224ce2993fSjeremylt 
42323617272Sjeremylt   @ref Advanced
4244ce2993fSjeremylt **/
4254ce2993fSjeremylt 
4264ce2993fSjeremylt int CeedQFunctionGetContext(CeedQFunction qf, void **ctx) {
4274ce2993fSjeremylt   *ctx = qf->ctx;
4284ce2993fSjeremylt   return 0;
4294ce2993fSjeremylt }
4304ce2993fSjeremylt 
4314ce2993fSjeremylt /**
432418fb8c2Sjeremylt   @brief Determine if Fortran interface was used
433418fb8c2Sjeremylt 
434418fb8c2Sjeremylt   @param qf                  CeedQFunction
435418fb8c2Sjeremylt   @param[out] fortranstatus  Variable to store Fortran status
436418fb8c2Sjeremylt 
437418fb8c2Sjeremylt   @return An error code: 0 - success, otherwise - failure
438418fb8c2Sjeremylt 
439418fb8c2Sjeremylt   @ref Advanced
440418fb8c2Sjeremylt **/
441418fb8c2Sjeremylt 
442418fb8c2Sjeremylt int CeedQFunctionGetFortranStatus(CeedQFunction qf, bool *fortranstatus) {
443418fb8c2Sjeremylt   *fortranstatus = qf->fortranstatus;
444418fb8c2Sjeremylt   return 0;
445418fb8c2Sjeremylt }
446418fb8c2Sjeremylt 
447418fb8c2Sjeremylt /**
4480219ea01SJeremy L Thompson   @brief Determine if QFunction is identity
4490219ea01SJeremy L Thompson 
4500219ea01SJeremy L Thompson   @param qf               CeedQFunction
4510219ea01SJeremy L Thompson   @param[out] identity    Variable to store identity status
4520219ea01SJeremy L Thompson 
4530219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4540219ea01SJeremy L Thompson 
4550219ea01SJeremy L Thompson   @ref Advanced
4560219ea01SJeremy L Thompson **/
4570219ea01SJeremy L Thompson 
4580219ea01SJeremy L Thompson int CeedQFunctionGetIdentityStatus(CeedQFunction qf, bool *identity) {
4590219ea01SJeremy L Thompson   *identity = qf->identity;
4600219ea01SJeremy L Thompson   return 0;
4610219ea01SJeremy L Thompson }
4620219ea01SJeremy L Thompson 
4630219ea01SJeremy L Thompson /**
46414922b2aSjeremylt   @brief Get true user context for a CeedQFunction
465069aeabaSjeremylt 
466069aeabaSjeremylt   @param qf              CeedQFunction
467069aeabaSjeremylt   @param[out] ctx        Variable to store context data values
468069aeabaSjeremylt 
469069aeabaSjeremylt   @return An error code: 0 - success, otherwise - failure
470069aeabaSjeremylt 
471069aeabaSjeremylt   @ref Advanced
472069aeabaSjeremylt **/
473069aeabaSjeremylt 
47414922b2aSjeremylt int CeedQFunctionGetInnerContext(CeedQFunction qf, void **ctx) {
47514922b2aSjeremylt   if (qf->fortranstatus) {
476069aeabaSjeremylt     fContext *fctx = qf->ctx;
477069aeabaSjeremylt     *ctx = fctx->innerctx;
47814922b2aSjeremylt   } else {
47914922b2aSjeremylt     *ctx = qf->ctx;
48014922b2aSjeremylt   }
48114922b2aSjeremylt 
4829f0427d9SYohann 
483069aeabaSjeremylt   return 0;
484069aeabaSjeremylt }
485069aeabaSjeremylt 
486069aeabaSjeremylt /**
4874ce2993fSjeremylt   @brief Get backend data of a CeedQFunction
4884ce2993fSjeremylt 
4894ce2993fSjeremylt   @param qf              CeedQFunction
4904ce2993fSjeremylt   @param[out] data       Variable to store data
4914ce2993fSjeremylt 
4924ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
4934ce2993fSjeremylt 
49423617272Sjeremylt   @ref Advanced
4954ce2993fSjeremylt **/
4964ce2993fSjeremylt 
4974ce2993fSjeremylt int CeedQFunctionGetData(CeedQFunction qf, void **data) {
4984ce2993fSjeremylt   *data = qf->data;
4994ce2993fSjeremylt   return 0;
5004ce2993fSjeremylt }
5014ce2993fSjeremylt 
5024ce2993fSjeremylt /**
503fe2413ffSjeremylt   @brief Set backend data of a CeedQFunction
504fe2413ffSjeremylt 
505fe2413ffSjeremylt   @param[out] qf         CeedQFunction
506fe2413ffSjeremylt   @param data            Data to set
507fe2413ffSjeremylt 
508fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
509fe2413ffSjeremylt 
510fe2413ffSjeremylt   @ref Advanced
511fe2413ffSjeremylt **/
512fe2413ffSjeremylt 
513fe2413ffSjeremylt int CeedQFunctionSetData(CeedQFunction qf, void **data) {
514fe2413ffSjeremylt   qf->data = *data;
515fe2413ffSjeremylt   return 0;
516fe2413ffSjeremylt }
517fe2413ffSjeremylt 
518fe2413ffSjeremylt /**
5194ce2993fSjeremylt   @brief Set global context for a CeedQFunction
520b11c1e72Sjeremylt 
521b11c1e72Sjeremylt   @param qf       CeedQFunction
522b11c1e72Sjeremylt   @param ctx      Context data to set
523b11c1e72Sjeremylt   @param ctxsize  Size of context data values
524b11c1e72Sjeremylt 
525b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
526dfdf5a53Sjeremylt 
527dfdf5a53Sjeremylt   @ref Basic
528b11c1e72Sjeremylt **/
529d7b241e6Sjeremylt int CeedQFunctionSetContext(CeedQFunction qf, void *ctx, size_t ctxsize) {
530d7b241e6Sjeremylt   qf->ctx = ctx;
531d7b241e6Sjeremylt   qf->ctxsize = ctxsize;
532d7b241e6Sjeremylt   return 0;
533d7b241e6Sjeremylt }
534d7b241e6Sjeremylt 
535b11c1e72Sjeremylt /**
536b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
537b11c1e72Sjeremylt 
538b11c1e72Sjeremylt   @param qf      CeedQFunction
539b11c1e72Sjeremylt   @param Q       Number of quadrature points
540b11c1e72Sjeremylt   @param[in] u   Array of input data arrays
541b11c1e72Sjeremylt   @param[out] v  Array of output data arrays
542b11c1e72Sjeremylt 
543b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
544dfdf5a53Sjeremylt 
545dfdf5a53Sjeremylt   @ref Advanced
546b11c1e72Sjeremylt **/
547d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
548aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
549d7b241e6Sjeremylt   int ierr;
550d7b241e6Sjeremylt   if (!qf->Apply)
551c042f62fSJeremy L Thompson     // LCOV_EXCL_START
552d7b241e6Sjeremylt     return CeedError(qf->ceed, 1, "Backend does not support QFunctionApply");
553c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
554d7b241e6Sjeremylt   if (Q % qf->vlength)
555c042f62fSJeremy L Thompson     // LCOV_EXCL_START
5561d102b48SJeremy L Thompson     return CeedError(qf->ceed, 2, "Number of quadrature points %d must be a "
5571d102b48SJeremy L Thompson                      "multiple of %d", Q, qf->vlength);
558c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
559d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
560d7b241e6Sjeremylt   return 0;
561d7b241e6Sjeremylt }
562d7b241e6Sjeremylt 
563b11c1e72Sjeremylt /**
564d1bcdac9Sjeremylt   @brief Get the CeedQFunctionFields of a CeedQFunction
565d1bcdac9Sjeremylt 
566d1bcdac9Sjeremylt   @param qf                 CeedQFunction
567d1bcdac9Sjeremylt   @param[out] inputfields   Variable to store inputfields
568d1bcdac9Sjeremylt   @param[out] outputfields  Variable to store outputfields
569d1bcdac9Sjeremylt 
570d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
571d1bcdac9Sjeremylt 
572d1bcdac9Sjeremylt   @ref Advanced
573d1bcdac9Sjeremylt **/
574d1bcdac9Sjeremylt 
575692c2638Sjeremylt int CeedQFunctionGetFields(CeedQFunction qf, CeedQFunctionField **inputfields,
576d1bcdac9Sjeremylt                            CeedQFunctionField **outputfields) {
5771d102b48SJeremy L Thompson   if (inputfields)
5781d102b48SJeremy L Thompson     *inputfields = qf->inputfields;
5791d102b48SJeremy L Thompson   if (outputfields)
5801d102b48SJeremy L Thompson     *outputfields = qf->outputfields;
581d1bcdac9Sjeremylt   return 0;
582d1bcdac9Sjeremylt }
583d1bcdac9Sjeremylt 
584d1bcdac9Sjeremylt /**
585fe2413ffSjeremylt   @brief Get the name of a CeedQFunctionField
586fe2413ffSjeremylt 
587fe2413ffSjeremylt   @param qffield         CeedQFunctionField
588fe2413ffSjeremylt   @param[out] fieldname  Variable to store the field name
589fe2413ffSjeremylt 
590fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
591fe2413ffSjeremylt 
592fe2413ffSjeremylt   @ref Advanced
593fe2413ffSjeremylt **/
594fe2413ffSjeremylt 
595692c2638Sjeremylt int CeedQFunctionFieldGetName(CeedQFunctionField qffield, char **fieldname) {
596fe2413ffSjeremylt   *fieldname = (char *)qffield->fieldname;
597fe2413ffSjeremylt   return 0;
598fe2413ffSjeremylt }
599fe2413ffSjeremylt 
600fe2413ffSjeremylt /**
601d1bcdac9Sjeremylt   @brief Get the number of components of a CeedQFunctionField
602d1bcdac9Sjeremylt 
603d1bcdac9Sjeremylt   @param qffield    CeedQFunctionField
6044d537eeaSYohann   @param[out] size  Variable to store the size of the field
605d1bcdac9Sjeremylt 
606d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
607d1bcdac9Sjeremylt 
608d1bcdac9Sjeremylt   @ref Advanced
609d1bcdac9Sjeremylt **/
610d1bcdac9Sjeremylt 
6114d537eeaSYohann int CeedQFunctionFieldGetSize(CeedQFunctionField qffield, CeedInt *size) {
6124d537eeaSYohann   *size = qffield->size;
613d1bcdac9Sjeremylt   return 0;
614d1bcdac9Sjeremylt }
615d1bcdac9Sjeremylt 
616d1bcdac9Sjeremylt /**
617d1bcdac9Sjeremylt   @brief Get the CeedEvalMode of a CeedQFunctionField
618d1bcdac9Sjeremylt 
619d1bcdac9Sjeremylt   @param qffield         CeedQFunctionField
620288c0443SJeremy L Thompson   @param[out] emode      Variable to store the field evaluation mode
621d1bcdac9Sjeremylt 
622d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
623d1bcdac9Sjeremylt 
624d1bcdac9Sjeremylt   @ref Advanced
625d1bcdac9Sjeremylt **/
626d1bcdac9Sjeremylt 
627d1bcdac9Sjeremylt int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qffield,
628d1bcdac9Sjeremylt                                   CeedEvalMode *emode) {
629fe2413ffSjeremylt   *emode = qffield->emode;
630d1bcdac9Sjeremylt   return 0;
631d1bcdac9Sjeremylt }
632d1bcdac9Sjeremylt 
633d1bcdac9Sjeremylt /**
634b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
635b11c1e72Sjeremylt 
636b11c1e72Sjeremylt   @param qf CeedQFunction to destroy
637b11c1e72Sjeremylt 
638b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
639dfdf5a53Sjeremylt 
640dfdf5a53Sjeremylt   @ref Basic
641b11c1e72Sjeremylt **/
642d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
643d7b241e6Sjeremylt   int ierr;
644d7b241e6Sjeremylt 
6451d102b48SJeremy L Thompson   if (!*qf || --(*qf)->refcount > 0)
6461d102b48SJeremy L Thompson     return 0;
647fe2413ffSjeremylt   // Backend destroy
648d7b241e6Sjeremylt   if ((*qf)->Destroy) {
649d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
650d7b241e6Sjeremylt   }
651fe2413ffSjeremylt   // Free fields
652fe2413ffSjeremylt   for (int i=0; i<(*qf)->numinputfields; i++) {
653fe2413ffSjeremylt     ierr = CeedFree(&(*(*qf)->inputfields[i]).fieldname); CeedChk(ierr);
654fe2413ffSjeremylt     ierr = CeedFree(&(*qf)->inputfields[i]); CeedChk(ierr);
655fe2413ffSjeremylt   }
656fe2413ffSjeremylt   for (int i=0; i<(*qf)->numoutputfields; i++) {
657fe2413ffSjeremylt     ierr = CeedFree(&(*(*qf)->outputfields[i]).fieldname); CeedChk(ierr);
658fe2413ffSjeremylt     ierr = CeedFree(&(*qf)->outputfields[i]); CeedChk(ierr);
659fe2413ffSjeremylt   }
660fe2413ffSjeremylt   ierr = CeedFree(&(*qf)->inputfields); CeedChk(ierr);
661fe2413ffSjeremylt   ierr = CeedFree(&(*qf)->outputfields); CeedChk(ierr);
6620219ea01SJeremy L Thompson   // Free ctx if identity
6630219ea01SJeremy L Thompson   if ((*qf)->identity) {
6640219ea01SJeremy L Thompson     ierr = CeedFree(&(*qf)->ctx); CeedChk(ierr);
6650219ea01SJeremy L Thompson   }
666fe2413ffSjeremylt 
667288c0443SJeremy L Thompson   ierr = CeedFree(&(*qf)->sourcepath); CeedChk(ierr);
668d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
669d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
670d7b241e6Sjeremylt   return 0;
671d7b241e6Sjeremylt }
672d7b241e6Sjeremylt 
673*442e7f0bSjeremylt /// @cond DOXYGEN_SKIP
674*442e7f0bSjeremylt // Indicate that no QFunction is provided by the user
675*442e7f0bSjeremylt CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
676*442e7f0bSjeremylt /// @endcond
677d7b241e6Sjeremylt /// @}
678