xref: /libCEED/interface/ceed-qfunction.c (revision 0219ea01e2c00bd70a330a05b50ef0218d6ddcb0)
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
23288c0443SJeremy L Thompson static struct {
24288c0443SJeremy L Thompson   char name[CEED_MAX_RESOURCE_LEN];
25288c0443SJeremy L Thompson   char source[CEED_MAX_RESOURCE_LEN];
26288c0443SJeremy L Thompson   CeedInt vlength;
27288c0443SJeremy L Thompson   CeedQFunctionUser f;
28288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
29288c0443SJeremy L Thompson } qfunctions[1024];
30288c0443SJeremy L Thompson static size_t num_qfunctions;
31288c0443SJeremy L Thompson /// @endcond
32d7b241e6Sjeremylt 
33dfdf5a53Sjeremylt /// @file
34dfdf5a53Sjeremylt /// Implementation of public CeedQFunction interfaces
35dfdf5a53Sjeremylt ///
36dfdf5a53Sjeremylt /// @addtogroup CeedQFunction
37dfdf5a53Sjeremylt /// @{
38d7b241e6Sjeremylt 
39d7b241e6Sjeremylt /**
40d7b241e6Sjeremylt   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
41d7b241e6Sjeremylt 
42b11c1e72Sjeremylt   @param ceed       A Ceed object where the CeedQFunction will be created
43d7b241e6Sjeremylt   @param vlength    Vector length.  Caller must ensure that number of quadrature
44d7b241e6Sjeremylt                     points is a multiple of vlength.
45d7b241e6Sjeremylt   @param f          Function pointer to evaluate action at quadrature points.
469f0427d9SYohann                     See \ref CeedQFunctionUser.
471176cc3aSJeremy L Thompson   @param source     Absolute path to source of QFunction,
481176cc3aSJeremy L Thompson                       "\abs_path\file.h:function_name"
49b11c1e72Sjeremylt   @param[out] qf    Address of the variable where the newly created
50b11c1e72Sjeremylt                       CeedQFunction will be stored
51b11c1e72Sjeremylt 
52b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
53d7b241e6Sjeremylt 
548795c945Sjeremylt   See \ref CeedQFunctionUser for details on the call-back function @a f's
558795c945Sjeremylt     arguments.
56d7b241e6Sjeremylt 
57dfdf5a53Sjeremylt   @ref Basic
58dfdf5a53Sjeremylt **/
59d7b241e6Sjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength,
609f0427d9SYohann                                 CeedQFunctionUser f,
61288c0443SJeremy L Thompson                                 const char *source, CeedQFunction *qf) {
62d7b241e6Sjeremylt   int ierr;
63288c0443SJeremy L Thompson   char *source_copy;
64d7b241e6Sjeremylt 
655fe0d4faSjeremylt   if (!ceed->QFunctionCreate) {
665fe0d4faSjeremylt     Ceed delegate;
67aefd8378Sjeremylt     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
685fe0d4faSjeremylt 
695fe0d4faSjeremylt     if (!delegate)
70d7b241e6Sjeremylt       return CeedError(ceed, 1, "Backend does not support QFunctionCreate");
715fe0d4faSjeremylt 
72288c0443SJeremy L Thompson     ierr = CeedQFunctionCreateInterior(delegate, vlength, f, source, qf);
731dfeef1dSjeremylt     CeedChk(ierr);
745fe0d4faSjeremylt     return 0;
755fe0d4faSjeremylt   }
765fe0d4faSjeremylt 
77d7b241e6Sjeremylt   ierr = CeedCalloc(1,qf); CeedChk(ierr);
78d7b241e6Sjeremylt   (*qf)->ceed = ceed;
79d7b241e6Sjeremylt   ceed->refcount++;
80d7b241e6Sjeremylt   (*qf)->refcount = 1;
81d7b241e6Sjeremylt   (*qf)->vlength = vlength;
82*0219ea01SJeremy L Thompson   (*qf)->identity = 0;
83d7b241e6Sjeremylt   (*qf)->function = f;
84288c0443SJeremy L Thompson   ierr = CeedCalloc(strlen(source)+1, &source_copy); CeedChk(ierr);
85288c0443SJeremy L Thompson   strncpy(source_copy, source, strlen(source));
86288c0443SJeremy L Thompson   (*qf)->sourcepath = source_copy;
87fe2413ffSjeremylt   ierr = CeedCalloc(16, &(*qf)->inputfields); CeedChk(ierr);
88fe2413ffSjeremylt   ierr = CeedCalloc(16, &(*qf)->outputfields); CeedChk(ierr);
89d7b241e6Sjeremylt   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
90d7b241e6Sjeremylt   return 0;
91d7b241e6Sjeremylt }
92d7b241e6Sjeremylt 
93b11c1e72Sjeremylt /**
94288c0443SJeremy L Thompson   @brief Register a gallery QFunction
95288c0443SJeremy L Thompson 
96288c0443SJeremy L Thompson   @param name    Name for this backend to respond to
971176cc3aSJeremy L Thompson   @param source  Absolute path to source of QFunction,
981176cc3aSJeremy L Thompson                    "\path\CEED_DIR\gallery\folder\file.h:function_name"
99288c0443SJeremy L Thompson   @param vlength Vector length.  Caller must ensure that number of quadrature
100288c0443SJeremy L Thompson                    points is a multiple of vlength.
101288c0443SJeremy L Thompson   @param f       Function pointer to evaluate action at quadrature points.
102288c0443SJeremy L Thompson                    See \ref CeedQFunctionUser.
103288c0443SJeremy L Thompson   @param init    Initialization function called by CeedQFunctionInit() when the
104288c0443SJeremy L Thompson                    QFunction is selected.
105288c0443SJeremy L Thompson 
106288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
107288c0443SJeremy L Thompson 
108288c0443SJeremy L Thompson   @ref Advanced
109288c0443SJeremy L Thompson **/
110288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source,
111288c0443SJeremy L Thompson                           CeedInt vlength, CeedQFunctionUser f,
112288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
113288c0443SJeremy L Thompson   if (num_qfunctions >= sizeof(qfunctions) / sizeof(qfunctions[0])) {
114288c0443SJeremy L Thompson     return CeedError(NULL, 1, "Too many gallery QFunctions");
115288c0443SJeremy L Thompson   }
116288c0443SJeremy L Thompson   strncpy(qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
117288c0443SJeremy L Thompson   qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
118288c0443SJeremy L Thompson   strncpy(qfunctions[num_qfunctions].source, source, CEED_MAX_RESOURCE_LEN);
119288c0443SJeremy L Thompson   qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
120288c0443SJeremy L Thompson   qfunctions[num_qfunctions].vlength = vlength;
121288c0443SJeremy L Thompson   qfunctions[num_qfunctions].f = f;
122288c0443SJeremy L Thompson   qfunctions[num_qfunctions].init = init;
123288c0443SJeremy L Thompson   num_qfunctions++;
124288c0443SJeremy L Thompson   return 0;
125288c0443SJeremy L Thompson }
126288c0443SJeremy L Thompson 
127288c0443SJeremy L Thompson /**
128288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
129288c0443SJeremy L Thompson 
130288c0443SJeremy L Thompson   @param ceed       A Ceed object where the CeedQFunction will be created
131288c0443SJeremy L Thompson   @param name       Name of QFunction to use from gallery
132288c0443SJeremy L Thompson   @param[out] qf    Address of the variable where the newly created
133288c0443SJeremy L Thompson                       CeedQFunction will be stored
134288c0443SJeremy L Thompson 
135288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
136288c0443SJeremy L Thompson 
137288c0443SJeremy L Thompson   @ref Basic
138288c0443SJeremy L Thompson **/
139288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
140288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
141288c0443SJeremy L Thompson   int ierr;
142288c0443SJeremy L Thompson   size_t matchlen = 0, matchidx = UINT_MAX;
143288c0443SJeremy L Thompson 
144288c0443SJeremy L Thompson   // Find matching backend
145288c0443SJeremy L Thompson   if (!name) return CeedError(NULL, 1, "No QFunction name provided");
146288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
147288c0443SJeremy L Thompson     size_t n;
148288c0443SJeremy L Thompson     const char *currname = qfunctions[i].name;
149288c0443SJeremy L Thompson     for (n = 0; currname[n] && currname[n] == name[n]; n++) {}
150288c0443SJeremy L Thompson     if (n > matchlen) {
151288c0443SJeremy L Thompson       matchlen = n;
152288c0443SJeremy L Thompson       matchidx = i;
153288c0443SJeremy L Thompson     }
154288c0443SJeremy L Thompson   }
155288c0443SJeremy L Thompson   if (!matchlen) return CeedError(NULL, 1, "No suitable gallery QFunction");
156288c0443SJeremy L Thompson 
157288c0443SJeremy L Thompson   // Create QFunction
158288c0443SJeremy L Thompson   ierr = CeedQFunctionCreateInterior(ceed, qfunctions[matchidx].vlength,
159288c0443SJeremy L Thompson                                      qfunctions[matchidx].f,
160288c0443SJeremy L Thompson                                      qfunctions[matchidx].source, qf);
161288c0443SJeremy L Thompson   CeedChk(ierr);
162288c0443SJeremy L Thompson 
163288c0443SJeremy L Thompson   // QFunction specific setup
164288c0443SJeremy L Thompson   ierr = qfunctions[matchidx].init(ceed, name, *qf); CeedChk(ierr);
165288c0443SJeremy L Thompson 
166288c0443SJeremy L Thompson   return 0;
167288c0443SJeremy L Thompson }
168288c0443SJeremy L Thompson 
169288c0443SJeremy L Thompson /**
170*0219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
171*0219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
172*0219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
173*0219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
174*0219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
175*0219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
176*0219ea01SJeremy L Thompson            location for both.
177*0219ea01SJeremy L Thompson 
178*0219ea01SJeremy L Thompson   @param ceed       A Ceed object where the CeedQFunction will be created
179*0219ea01SJeremy L Thompson   @param size       Size of the qfunction fields
180*0219ea01SJeremy L Thompson   @param[out] qf    Address of the variable where the newly created
181*0219ea01SJeremy L Thompson                       CeedQFunction will be stored
182*0219ea01SJeremy L Thompson 
183*0219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
184*0219ea01SJeremy L Thompson 
185*0219ea01SJeremy L Thompson   @ref Basic
186*0219ea01SJeremy L Thompson **/
187*0219ea01SJeremy L Thompson int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedQFunction *qf) {
188*0219ea01SJeremy L Thompson   int ierr;
189*0219ea01SJeremy L Thompson 
190*0219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
191*0219ea01SJeremy L Thompson 
192*0219ea01SJeremy L Thompson   (*qf)->identity = 1;
193*0219ea01SJeremy L Thompson   if (size > 1) {
194*0219ea01SJeremy L Thompson     CeedInt *ctx;
195*0219ea01SJeremy L Thompson     ierr = CeedCalloc(1, &ctx); CeedChk(ierr);
196*0219ea01SJeremy L Thompson     ctx[0] = size;
197*0219ea01SJeremy L Thompson     ierr = CeedQFunctionSetContext(*qf, ctx, sizeof(ctx)); CeedChk(ierr);
198*0219ea01SJeremy L Thompson   }
199*0219ea01SJeremy L Thompson 
200*0219ea01SJeremy L Thompson   return 0;
201*0219ea01SJeremy L Thompson }
202*0219ea01SJeremy L Thompson 
203*0219ea01SJeremy L Thompson /**
204a0a97fcfSJed Brown   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
205b11c1e72Sjeremylt 
206b11c1e72Sjeremylt   @param f          CeedQFunctionField
207b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
2084d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
2094d537eeaSYohann                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
210b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
211b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
212b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
213b11c1e72Sjeremylt 
214b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
215dfdf5a53Sjeremylt 
216dfdf5a53Sjeremylt   @ref Developer
217b11c1e72Sjeremylt **/
218fe2413ffSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *fieldname,
2194d537eeaSYohann                                  CeedInt size, CeedEvalMode emode) {
220d7b241e6Sjeremylt   size_t len = strlen(fieldname);
221d7b241e6Sjeremylt   char *tmp;
222fe2413ffSjeremylt   int ierr;
223fe2413ffSjeremylt   ierr = CeedCalloc(1,f); CeedChk(ierr);
224fe2413ffSjeremylt 
225fe2413ffSjeremylt   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
226d7b241e6Sjeremylt   memcpy(tmp, fieldname, len+1);
227fe2413ffSjeremylt   (*f)->fieldname = tmp;
2284d537eeaSYohann   (*f)->size = size;
229fe2413ffSjeremylt   (*f)->emode = emode;
230d7b241e6Sjeremylt   return 0;
231d7b241e6Sjeremylt }
232d7b241e6Sjeremylt 
233b11c1e72Sjeremylt /**
234a0a97fcfSJed Brown   @brief Add a CeedQFunction input
235b11c1e72Sjeremylt 
236b11c1e72Sjeremylt   @param qf         CeedQFunction
237b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
2384d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
2394d537eeaSYohann                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
240b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
241b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
242b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
243b11c1e72Sjeremylt 
244b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
245dfdf5a53Sjeremylt 
246dfdf5a53Sjeremylt   @ref Basic
247b11c1e72Sjeremylt **/
248d7b241e6Sjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname,
2494d537eeaSYohann                           CeedInt size, CeedEvalMode emode) {
250fe2413ffSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->inputfields[qf->numinputfields],
2514d537eeaSYohann                                    fieldname, size, emode);
252fe2413ffSjeremylt   CeedChk(ierr);
253fe2413ffSjeremylt   qf->numinputfields++;
254d7b241e6Sjeremylt   return 0;
255d7b241e6Sjeremylt }
256d7b241e6Sjeremylt 
257b11c1e72Sjeremylt /**
258a0a97fcfSJed Brown   @brief Add a CeedQFunction output
259b11c1e72Sjeremylt 
260b11c1e72Sjeremylt   @param qf         CeedQFunction
261b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
2624d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
2634d537eeaSYohann                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
264b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
265b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
266b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
267b11c1e72Sjeremylt 
268b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
269dfdf5a53Sjeremylt 
270dfdf5a53Sjeremylt   @ref Basic
271b11c1e72Sjeremylt **/
272d7b241e6Sjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
2734d537eeaSYohann                            CeedInt size, CeedEvalMode emode) {
274d7b241e6Sjeremylt   if (emode == CEED_EVAL_WEIGHT)
275d7b241e6Sjeremylt     return CeedError(qf->ceed, 1,
2768c91a0c9SJeremy L Thompson                      "Cannot create QFunction output with CEED_EVAL_WEIGHT");
277fe2413ffSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->outputfields[qf->numoutputfields],
2784d537eeaSYohann                                    fieldname, size, emode);
279fe2413ffSjeremylt   CeedChk(ierr);
280fe2413ffSjeremylt   qf->numoutputfields++;
281d7b241e6Sjeremylt   return 0;
282d7b241e6Sjeremylt }
283d7b241e6Sjeremylt 
284dfdf5a53Sjeremylt /**
2854ce2993fSjeremylt   @brief Get the Ceed associated with a CeedQFunction
2864ce2993fSjeremylt 
2874ce2993fSjeremylt   @param qf              CeedQFunction
2884ce2993fSjeremylt   @param[out] ceed       Variable to store Ceed
2894ce2993fSjeremylt 
2904ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
2914ce2993fSjeremylt 
29223617272Sjeremylt   @ref Advanced
2934ce2993fSjeremylt **/
2944ce2993fSjeremylt 
2954ce2993fSjeremylt int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
2964ce2993fSjeremylt   *ceed = qf->ceed;
2974ce2993fSjeremylt   return 0;
2984ce2993fSjeremylt }
2994ce2993fSjeremylt 
3004ce2993fSjeremylt /**
3014ce2993fSjeremylt   @brief Get the vector length of a CeedQFunction
3024ce2993fSjeremylt 
3034ce2993fSjeremylt   @param qf            CeedQFunction
304288c0443SJeremy L Thompson   @param[out] vlength  Variable to store vector length
3054ce2993fSjeremylt 
3064ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3074ce2993fSjeremylt 
30823617272Sjeremylt   @ref Advanced
3094ce2993fSjeremylt **/
3104ce2993fSjeremylt 
3114ce2993fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength) {
3124ce2993fSjeremylt   *vlength = qf->vlength;
3134ce2993fSjeremylt   return 0;
3144ce2993fSjeremylt }
3154ce2993fSjeremylt 
3164ce2993fSjeremylt /**
317dfdf5a53Sjeremylt   @brief Get the number of inputs and outputs to a CeedQFunction
318dfdf5a53Sjeremylt 
319dfdf5a53Sjeremylt   @param qf              CeedQFunction
3204ce2993fSjeremylt   @param[out] numinput   Variable to store number of input fields
3214ce2993fSjeremylt   @param[out] numoutput  Variable to store number of output fields
322dfdf5a53Sjeremylt 
323dfdf5a53Sjeremylt   @return An error code: 0 - success, otherwise - failure
324dfdf5a53Sjeremylt 
32523617272Sjeremylt   @ref Advanced
326dfdf5a53Sjeremylt **/
327dfdf5a53Sjeremylt 
328d7b241e6Sjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput,
329d7b241e6Sjeremylt                             CeedInt *numoutput) {
3301a4ead9bSjeremylt   if (numinput) *numinput = qf->numinputfields;
3311a4ead9bSjeremylt   if (numoutput) *numoutput = qf->numoutputfields;
332d7b241e6Sjeremylt   return 0;
333d7b241e6Sjeremylt }
334d7b241e6Sjeremylt 
335d7b241e6Sjeremylt /**
336288c0443SJeremy L Thompson   @brief Get the source path string for a CeedQFunction
3374ce2993fSjeremylt 
3384ce2993fSjeremylt   @param qf              CeedQFunction
339288c0443SJeremy L Thompson   @param[out] source     Variable to store source path string
3404ce2993fSjeremylt 
3414ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3424ce2993fSjeremylt 
34323617272Sjeremylt   @ref Advanced
3444ce2993fSjeremylt **/
3454ce2993fSjeremylt 
346288c0443SJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char* *source) {
347288c0443SJeremy L Thompson   *source = (char *) qf->sourcepath;
3484ce2993fSjeremylt   return 0;
3494ce2993fSjeremylt }
3504ce2993fSjeremylt 
3514ce2993fSjeremylt /**
352fe2413ffSjeremylt   @brief Get the User Function for a CeedQFunction
353fe2413ffSjeremylt 
354fe2413ffSjeremylt   @param qf              CeedQFunction
355fe2413ffSjeremylt   @param[out] f          Variable to store user function
356fe2413ffSjeremylt 
357fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
358fe2413ffSjeremylt 
359fe2413ffSjeremylt   @ref Advanced
360fe2413ffSjeremylt **/
361fe2413ffSjeremylt 
36228d161eeSjeremylt int CeedQFunctionGetUserFunction(CeedQFunction qf, int (**f)()) {
36328d161eeSjeremylt   *f = (int (*)())qf->function;
364fe2413ffSjeremylt   return 0;
365fe2413ffSjeremylt }
366fe2413ffSjeremylt 
367fe2413ffSjeremylt /**
3684ce2993fSjeremylt   @brief Get global context size for a CeedQFunction
3694ce2993fSjeremylt 
3704ce2993fSjeremylt   @param qf              CeedQFunction
3714ce2993fSjeremylt   @param[out] ctxsize    Variable to store size of context data values
3724ce2993fSjeremylt 
3734ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3744ce2993fSjeremylt 
37523617272Sjeremylt   @ref Advanced
3764ce2993fSjeremylt **/
3774ce2993fSjeremylt 
3784ce2993fSjeremylt int CeedQFunctionGetContextSize(CeedQFunction qf, size_t *ctxsize) {
379069aeabaSjeremylt   if (qf->fortranstatus) {
380069aeabaSjeremylt     fContext *fctx = qf->ctx;
381069aeabaSjeremylt     *ctxsize = fctx->innerctxsize;
382069aeabaSjeremylt   } else {
3834ce2993fSjeremylt     *ctxsize = qf->ctxsize;
384069aeabaSjeremylt   }
3854ce2993fSjeremylt   return 0;
3864ce2993fSjeremylt }
3874ce2993fSjeremylt 
3884ce2993fSjeremylt /**
3894ce2993fSjeremylt   @brief Get global context for a CeedQFunction
3904ce2993fSjeremylt 
3914ce2993fSjeremylt   @param qf              CeedQFunction
3924ce2993fSjeremylt   @param[out] ctx        Variable to store context data values
3934ce2993fSjeremylt 
3944ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3954ce2993fSjeremylt 
39623617272Sjeremylt   @ref Advanced
3974ce2993fSjeremylt **/
3984ce2993fSjeremylt 
3994ce2993fSjeremylt int CeedQFunctionGetContext(CeedQFunction qf, void* *ctx) {
4004ce2993fSjeremylt   *ctx = qf->ctx;
4014ce2993fSjeremylt   return 0;
4024ce2993fSjeremylt }
4034ce2993fSjeremylt 
4044ce2993fSjeremylt /**
405418fb8c2Sjeremylt   @brief Determine if Fortran interface was used
406418fb8c2Sjeremylt 
407418fb8c2Sjeremylt   @param qf                  CeedQFunction
408418fb8c2Sjeremylt   @param[out] fortranstatus  Variable to store Fortran status
409418fb8c2Sjeremylt 
410418fb8c2Sjeremylt   @return An error code: 0 - success, otherwise - failure
411418fb8c2Sjeremylt 
412418fb8c2Sjeremylt   @ref Advanced
413418fb8c2Sjeremylt **/
414418fb8c2Sjeremylt 
415418fb8c2Sjeremylt int CeedQFunctionGetFortranStatus(CeedQFunction qf, bool *fortranstatus) {
416418fb8c2Sjeremylt   *fortranstatus = qf->fortranstatus;
417418fb8c2Sjeremylt   return 0;
418418fb8c2Sjeremylt }
419418fb8c2Sjeremylt 
420418fb8c2Sjeremylt /**
421*0219ea01SJeremy L Thompson   @brief Determine if QFunction is identity
422*0219ea01SJeremy L Thompson 
423*0219ea01SJeremy L Thompson   @param qf               CeedQFunction
424*0219ea01SJeremy L Thompson   @param[out] identity    Variable to store identity status
425*0219ea01SJeremy L Thompson 
426*0219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
427*0219ea01SJeremy L Thompson 
428*0219ea01SJeremy L Thompson   @ref Advanced
429*0219ea01SJeremy L Thompson **/
430*0219ea01SJeremy L Thompson 
431*0219ea01SJeremy L Thompson int CeedQFunctionGetIdentityStatus(CeedQFunction qf, bool *identity) {
432*0219ea01SJeremy L Thompson   *identity = qf->identity;
433*0219ea01SJeremy L Thompson   return 0;
434*0219ea01SJeremy L Thompson }
435*0219ea01SJeremy L Thompson 
436*0219ea01SJeremy L Thompson /**
43714922b2aSjeremylt   @brief Get true user context for a CeedQFunction
438069aeabaSjeremylt 
439069aeabaSjeremylt   @param qf              CeedQFunction
440069aeabaSjeremylt   @param[out] ctx        Variable to store context data values
441069aeabaSjeremylt 
442069aeabaSjeremylt   @return An error code: 0 - success, otherwise - failure
443069aeabaSjeremylt 
444069aeabaSjeremylt   @ref Advanced
445069aeabaSjeremylt **/
446069aeabaSjeremylt 
44714922b2aSjeremylt int CeedQFunctionGetInnerContext(CeedQFunction qf, void* *ctx) {
44814922b2aSjeremylt   if (qf->fortranstatus) {
449069aeabaSjeremylt     fContext *fctx = qf->ctx;
450069aeabaSjeremylt     *ctx = fctx->innerctx;
45114922b2aSjeremylt   } else {
45214922b2aSjeremylt     *ctx = qf->ctx;
45314922b2aSjeremylt   }
45414922b2aSjeremylt 
4559f0427d9SYohann 
456069aeabaSjeremylt   return 0;
457069aeabaSjeremylt }
458069aeabaSjeremylt 
459069aeabaSjeremylt /**
4604ce2993fSjeremylt   @brief Get backend data of a CeedQFunction
4614ce2993fSjeremylt 
4624ce2993fSjeremylt   @param qf              CeedQFunction
4634ce2993fSjeremylt   @param[out] data       Variable to store data
4644ce2993fSjeremylt 
4654ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
4664ce2993fSjeremylt 
46723617272Sjeremylt   @ref Advanced
4684ce2993fSjeremylt **/
4694ce2993fSjeremylt 
4704ce2993fSjeremylt int CeedQFunctionGetData(CeedQFunction qf, void* *data) {
4714ce2993fSjeremylt   *data = qf->data;
4724ce2993fSjeremylt   return 0;
4734ce2993fSjeremylt }
4744ce2993fSjeremylt 
4754ce2993fSjeremylt /**
476fe2413ffSjeremylt   @brief Set backend data of a CeedQFunction
477fe2413ffSjeremylt 
478fe2413ffSjeremylt   @param[out] qf         CeedQFunction
479fe2413ffSjeremylt   @param data            Data to set
480fe2413ffSjeremylt 
481fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
482fe2413ffSjeremylt 
483fe2413ffSjeremylt   @ref Advanced
484fe2413ffSjeremylt **/
485fe2413ffSjeremylt 
486fe2413ffSjeremylt int CeedQFunctionSetData(CeedQFunction qf, void* *data) {
487fe2413ffSjeremylt   qf->data = *data;
488fe2413ffSjeremylt   return 0;
489fe2413ffSjeremylt }
490fe2413ffSjeremylt 
491fe2413ffSjeremylt /**
4924ce2993fSjeremylt   @brief Set global context for a CeedQFunction
493b11c1e72Sjeremylt 
494b11c1e72Sjeremylt   @param qf       CeedQFunction
495b11c1e72Sjeremylt   @param ctx      Context data to set
496b11c1e72Sjeremylt   @param ctxsize  Size of context data values
497b11c1e72Sjeremylt 
498b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
499dfdf5a53Sjeremylt 
500dfdf5a53Sjeremylt   @ref Basic
501b11c1e72Sjeremylt **/
502d7b241e6Sjeremylt int CeedQFunctionSetContext(CeedQFunction qf, void *ctx, size_t ctxsize) {
503d7b241e6Sjeremylt   qf->ctx = ctx;
504d7b241e6Sjeremylt   qf->ctxsize = ctxsize;
505d7b241e6Sjeremylt   return 0;
506d7b241e6Sjeremylt }
507d7b241e6Sjeremylt 
508b11c1e72Sjeremylt /**
509b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
510b11c1e72Sjeremylt 
511b11c1e72Sjeremylt   @param qf      CeedQFunction
512b11c1e72Sjeremylt   @param Q       Number of quadrature points
513b11c1e72Sjeremylt   @param[in] u   Array of input data arrays
514b11c1e72Sjeremylt   @param[out] v  Array of output data arrays
515b11c1e72Sjeremylt 
516b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
517dfdf5a53Sjeremylt 
518dfdf5a53Sjeremylt   @ref Advanced
519b11c1e72Sjeremylt **/
520d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
521aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
522d7b241e6Sjeremylt   int ierr;
523d7b241e6Sjeremylt   if (!qf->Apply)
524d7b241e6Sjeremylt     return CeedError(qf->ceed, 1, "Backend does not support QFunctionApply");
525d7b241e6Sjeremylt   if (Q % qf->vlength)
526d7b241e6Sjeremylt     return CeedError(qf->ceed, 2,
527d7b241e6Sjeremylt                      "Number of quadrature points %d must be a multiple of %d",
528d7b241e6Sjeremylt                      Q, qf->vlength);
529d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
530d7b241e6Sjeremylt   return 0;
531d7b241e6Sjeremylt }
532d7b241e6Sjeremylt 
533b11c1e72Sjeremylt /**
534d1bcdac9Sjeremylt   @brief Get the CeedQFunctionFields of a CeedQFunction
535d1bcdac9Sjeremylt 
536d1bcdac9Sjeremylt   @param qf                 CeedQFunction
537d1bcdac9Sjeremylt   @param[out] inputfields   Variable to store inputfields
538d1bcdac9Sjeremylt   @param[out] outputfields  Variable to store outputfields
539d1bcdac9Sjeremylt 
540d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
541d1bcdac9Sjeremylt 
542d1bcdac9Sjeremylt   @ref Advanced
543d1bcdac9Sjeremylt **/
544d1bcdac9Sjeremylt 
545d1bcdac9Sjeremylt int CeedQFunctionGetFields(CeedQFunction qf,
546d1bcdac9Sjeremylt                            CeedQFunctionField* *inputfields,
547d1bcdac9Sjeremylt                            CeedQFunctionField* *outputfields) {
548d1bcdac9Sjeremylt   if (inputfields) *inputfields = qf->inputfields;
549d1bcdac9Sjeremylt   if (outputfields) *outputfields = qf->outputfields;
550d1bcdac9Sjeremylt   return 0;
551d1bcdac9Sjeremylt }
552d1bcdac9Sjeremylt 
553d1bcdac9Sjeremylt /**
554fe2413ffSjeremylt   @brief Get the name of a CeedQFunctionField
555fe2413ffSjeremylt 
556fe2413ffSjeremylt   @param qffield         CeedQFunctionField
557fe2413ffSjeremylt   @param[out] fieldname  Variable to store the field name
558fe2413ffSjeremylt 
559fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
560fe2413ffSjeremylt 
561fe2413ffSjeremylt   @ref Advanced
562fe2413ffSjeremylt **/
563fe2413ffSjeremylt 
564fe2413ffSjeremylt int CeedQFunctionFieldGetName(CeedQFunctionField qffield,
565fe2413ffSjeremylt                               char* *fieldname) {
566fe2413ffSjeremylt   *fieldname = (char *)qffield->fieldname;
567fe2413ffSjeremylt   return 0;
568fe2413ffSjeremylt }
569fe2413ffSjeremylt 
570fe2413ffSjeremylt /**
571d1bcdac9Sjeremylt   @brief Get the number of components of a CeedQFunctionField
572d1bcdac9Sjeremylt 
573d1bcdac9Sjeremylt   @param qffield    CeedQFunctionField
5744d537eeaSYohann   @param[out] size  Variable to store the size of the field
575d1bcdac9Sjeremylt 
576d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
577d1bcdac9Sjeremylt 
578d1bcdac9Sjeremylt   @ref Advanced
579d1bcdac9Sjeremylt **/
580d1bcdac9Sjeremylt 
5814d537eeaSYohann int CeedQFunctionFieldGetSize(CeedQFunctionField qffield, CeedInt *size) {
5824d537eeaSYohann   *size = qffield->size;
583d1bcdac9Sjeremylt   return 0;
584d1bcdac9Sjeremylt }
585d1bcdac9Sjeremylt 
586d1bcdac9Sjeremylt /**
587d1bcdac9Sjeremylt   @brief Get the CeedEvalMode of a CeedQFunctionField
588d1bcdac9Sjeremylt 
589d1bcdac9Sjeremylt   @param qffield         CeedQFunctionField
590288c0443SJeremy L Thompson   @param[out] emode      Variable to store the field evaluation mode
591d1bcdac9Sjeremylt 
592d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
593d1bcdac9Sjeremylt 
594d1bcdac9Sjeremylt   @ref Advanced
595d1bcdac9Sjeremylt **/
596d1bcdac9Sjeremylt 
597d1bcdac9Sjeremylt int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qffield,
598d1bcdac9Sjeremylt                                   CeedEvalMode *emode) {
599fe2413ffSjeremylt   *emode = qffield->emode;
600d1bcdac9Sjeremylt   return 0;
601d1bcdac9Sjeremylt }
602d1bcdac9Sjeremylt 
603d1bcdac9Sjeremylt /**
604b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
605b11c1e72Sjeremylt 
606b11c1e72Sjeremylt   @param qf CeedQFunction to destroy
607b11c1e72Sjeremylt 
608b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
609dfdf5a53Sjeremylt 
610dfdf5a53Sjeremylt   @ref Basic
611b11c1e72Sjeremylt **/
612d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
613d7b241e6Sjeremylt   int ierr;
614d7b241e6Sjeremylt 
615d7b241e6Sjeremylt   if (!*qf || --(*qf)->refcount > 0) return 0;
616fe2413ffSjeremylt   // Backend destroy
617d7b241e6Sjeremylt   if ((*qf)->Destroy) {
618d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
619d7b241e6Sjeremylt   }
620fe2413ffSjeremylt   // Free fields
621fe2413ffSjeremylt   for (int i=0; i<(*qf)->numinputfields; i++) {
622fe2413ffSjeremylt     ierr = CeedFree(&(*(*qf)->inputfields[i]).fieldname); CeedChk(ierr);
623fe2413ffSjeremylt     ierr = CeedFree(&(*qf)->inputfields[i]); CeedChk(ierr);
624fe2413ffSjeremylt   }
625fe2413ffSjeremylt   for (int i=0; i<(*qf)->numoutputfields; i++) {
626fe2413ffSjeremylt     ierr = CeedFree(&(*(*qf)->outputfields[i]).fieldname); CeedChk(ierr);
627fe2413ffSjeremylt     ierr = CeedFree(&(*qf)->outputfields[i]); CeedChk(ierr);
628fe2413ffSjeremylt   }
629fe2413ffSjeremylt   ierr = CeedFree(&(*qf)->inputfields); CeedChk(ierr);
630fe2413ffSjeremylt   ierr = CeedFree(&(*qf)->outputfields); CeedChk(ierr);
631*0219ea01SJeremy L Thompson   // Free ctx if identity
632*0219ea01SJeremy L Thompson   if ((*qf)->identity) {
633*0219ea01SJeremy L Thompson     ierr = CeedFree(&(*qf)->ctx); CeedChk(ierr);
634*0219ea01SJeremy L Thompson   }
635fe2413ffSjeremylt 
636288c0443SJeremy L Thompson   ierr = CeedFree(&(*qf)->sourcepath); CeedChk(ierr);
637d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
638d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
639d7b241e6Sjeremylt   return 0;
640d7b241e6Sjeremylt }
641d7b241e6Sjeremylt 
642d7b241e6Sjeremylt /// @}
643