xref: /libCEED/interface/ceed-qfunction.c (revision c042f62f62e10e1321eb699b116e67a6568d5716)
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)
70*c042f62fSJeremy L Thompson       // LCOV_EXCL_START
71d7b241e6Sjeremylt       return CeedError(ceed, 1, "Backend does not support QFunctionCreate");
72*c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
735fe0d4faSjeremylt 
74288c0443SJeremy L Thompson     ierr = CeedQFunctionCreateInterior(delegate, vlength, f, source, qf);
751dfeef1dSjeremylt     CeedChk(ierr);
765fe0d4faSjeremylt     return 0;
775fe0d4faSjeremylt   }
785fe0d4faSjeremylt 
79d7b241e6Sjeremylt   ierr = CeedCalloc(1,qf); CeedChk(ierr);
80d7b241e6Sjeremylt   (*qf)->ceed = ceed;
81d7b241e6Sjeremylt   ceed->refcount++;
82d7b241e6Sjeremylt   (*qf)->refcount = 1;
83d7b241e6Sjeremylt   (*qf)->vlength = vlength;
840219ea01SJeremy L Thompson   (*qf)->identity = 0;
85d7b241e6Sjeremylt   (*qf)->function = f;
86288c0443SJeremy L Thompson   ierr = CeedCalloc(strlen(source)+1, &source_copy); CeedChk(ierr);
87288c0443SJeremy L Thompson   strncpy(source_copy, source, strlen(source));
88288c0443SJeremy L Thompson   (*qf)->sourcepath = source_copy;
89fe2413ffSjeremylt   ierr = CeedCalloc(16, &(*qf)->inputfields); CeedChk(ierr);
90fe2413ffSjeremylt   ierr = CeedCalloc(16, &(*qf)->outputfields); CeedChk(ierr);
91d7b241e6Sjeremylt   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
92d7b241e6Sjeremylt   return 0;
93d7b241e6Sjeremylt }
94d7b241e6Sjeremylt 
95b11c1e72Sjeremylt /**
96288c0443SJeremy L Thompson   @brief Register a gallery QFunction
97288c0443SJeremy L Thompson 
98288c0443SJeremy L Thompson   @param name    Name for this backend to respond to
991176cc3aSJeremy L Thompson   @param source  Absolute path to source of QFunction,
1001176cc3aSJeremy L Thompson                    "\path\CEED_DIR\gallery\folder\file.h:function_name"
101288c0443SJeremy L Thompson   @param vlength Vector length.  Caller must ensure that number of quadrature
102288c0443SJeremy L Thompson                    points is a multiple of vlength.
103288c0443SJeremy L Thompson   @param f       Function pointer to evaluate action at quadrature points.
104288c0443SJeremy L Thompson                    See \ref CeedQFunctionUser.
105288c0443SJeremy L Thompson   @param init    Initialization function called by CeedQFunctionInit() when the
106288c0443SJeremy L Thompson                    QFunction is selected.
107288c0443SJeremy L Thompson 
108288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
109288c0443SJeremy L Thompson 
110288c0443SJeremy L Thompson   @ref Advanced
111288c0443SJeremy L Thompson **/
112288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source,
113288c0443SJeremy L Thompson                           CeedInt vlength, CeedQFunctionUser f,
114288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
115*c042f62fSJeremy L Thompson   if (num_qfunctions >= sizeof(qfunctions) / sizeof(qfunctions[0]))
116*c042f62fSJeremy L Thompson     // LCOV_EXCL_START
117288c0443SJeremy L Thompson     return CeedError(NULL, 1, "Too many gallery QFunctions");
118*c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
119*c042f62fSJeremy L Thompson 
120288c0443SJeremy L Thompson   strncpy(qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
121288c0443SJeremy L Thompson   qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
122288c0443SJeremy L Thompson   strncpy(qfunctions[num_qfunctions].source, source, CEED_MAX_RESOURCE_LEN);
123288c0443SJeremy L Thompson   qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
124288c0443SJeremy L Thompson   qfunctions[num_qfunctions].vlength = vlength;
125288c0443SJeremy L Thompson   qfunctions[num_qfunctions].f = f;
126288c0443SJeremy L Thompson   qfunctions[num_qfunctions].init = init;
127288c0443SJeremy L Thompson   num_qfunctions++;
128288c0443SJeremy L Thompson   return 0;
129288c0443SJeremy L Thompson }
130288c0443SJeremy L Thompson 
131288c0443SJeremy L Thompson /**
132288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
133288c0443SJeremy L Thompson 
134288c0443SJeremy L Thompson   @param ceed       A Ceed object where the CeedQFunction will be created
135288c0443SJeremy L Thompson   @param name       Name of QFunction to use from gallery
136288c0443SJeremy L Thompson   @param[out] qf    Address of the variable where the newly created
137288c0443SJeremy L Thompson                       CeedQFunction will be stored
138288c0443SJeremy L Thompson 
139288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
140288c0443SJeremy L Thompson 
141288c0443SJeremy L Thompson   @ref Basic
142288c0443SJeremy L Thompson **/
143288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
144288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
145288c0443SJeremy L Thompson   int ierr;
146288c0443SJeremy L Thompson   size_t matchlen = 0, matchidx = UINT_MAX;
147288c0443SJeremy L Thompson 
148288c0443SJeremy L Thompson   // Find matching backend
149288c0443SJeremy L Thompson   if (!name) return CeedError(NULL, 1, "No QFunction name provided");
150288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
151288c0443SJeremy L Thompson     size_t n;
152288c0443SJeremy L Thompson     const char *currname = qfunctions[i].name;
153288c0443SJeremy L Thompson     for (n = 0; currname[n] && currname[n] == name[n]; n++) {}
154288c0443SJeremy L Thompson     if (n > matchlen) {
155288c0443SJeremy L Thompson       matchlen = n;
156288c0443SJeremy L Thompson       matchidx = i;
157288c0443SJeremy L Thompson     }
158288c0443SJeremy L Thompson   }
159288c0443SJeremy L Thompson   if (!matchlen) return CeedError(NULL, 1, "No suitable gallery QFunction");
160288c0443SJeremy L Thompson 
161288c0443SJeremy L Thompson   // Create QFunction
162288c0443SJeremy L Thompson   ierr = CeedQFunctionCreateInterior(ceed, qfunctions[matchidx].vlength,
163288c0443SJeremy L Thompson                                      qfunctions[matchidx].f,
164288c0443SJeremy L Thompson                                      qfunctions[matchidx].source, qf);
165288c0443SJeremy L Thompson   CeedChk(ierr);
166288c0443SJeremy L Thompson 
167288c0443SJeremy L Thompson   // QFunction specific setup
168288c0443SJeremy L Thompson   ierr = qfunctions[matchidx].init(ceed, name, *qf); CeedChk(ierr);
169288c0443SJeremy L Thompson 
170288c0443SJeremy L Thompson   return 0;
171288c0443SJeremy L Thompson }
172288c0443SJeremy L Thompson 
173288c0443SJeremy L Thompson /**
1740219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
1750219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
1760219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
1770219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
1780219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
1790219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
1800219ea01SJeremy L Thompson            location for both.
1810219ea01SJeremy L Thompson 
1820219ea01SJeremy L Thompson   @param ceed       A Ceed object where the CeedQFunction will be created
1830219ea01SJeremy L Thompson   @param size       Size of the qfunction fields
1840219ea01SJeremy L Thompson   @param[out] qf    Address of the variable where the newly created
1850219ea01SJeremy L Thompson                       CeedQFunction will be stored
1860219ea01SJeremy L Thompson 
1870219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1880219ea01SJeremy L Thompson 
1890219ea01SJeremy L Thompson   @ref Basic
1900219ea01SJeremy L Thompson **/
1910219ea01SJeremy L Thompson int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedQFunction *qf) {
1920219ea01SJeremy L Thompson   int ierr;
1930219ea01SJeremy L Thompson 
1940219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
1950219ea01SJeremy L Thompson 
1960219ea01SJeremy L Thompson   (*qf)->identity = 1;
1970219ea01SJeremy L Thompson   if (size > 1) {
1980219ea01SJeremy L Thompson     CeedInt *ctx;
1990219ea01SJeremy L Thompson     ierr = CeedCalloc(1, &ctx); CeedChk(ierr);
2000219ea01SJeremy L Thompson     ctx[0] = size;
2010219ea01SJeremy L Thompson     ierr = CeedQFunctionSetContext(*qf, ctx, sizeof(ctx)); CeedChk(ierr);
2020219ea01SJeremy L Thompson   }
2030219ea01SJeremy L Thompson 
2040219ea01SJeremy L Thompson   return 0;
2050219ea01SJeremy L Thompson }
2060219ea01SJeremy L Thompson 
2070219ea01SJeremy L Thompson /**
208a0a97fcfSJed Brown   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
209b11c1e72Sjeremylt 
210b11c1e72Sjeremylt   @param f          CeedQFunctionField
211b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
2124d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
2134d537eeaSYohann                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
214b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
215b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
216b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
217b11c1e72Sjeremylt 
218b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
219dfdf5a53Sjeremylt 
220dfdf5a53Sjeremylt   @ref Developer
221b11c1e72Sjeremylt **/
222fe2413ffSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *fieldname,
2234d537eeaSYohann                                  CeedInt size, CeedEvalMode emode) {
224d7b241e6Sjeremylt   size_t len = strlen(fieldname);
225d7b241e6Sjeremylt   char *tmp;
226fe2413ffSjeremylt   int ierr;
227fe2413ffSjeremylt   ierr = CeedCalloc(1,f); CeedChk(ierr);
228fe2413ffSjeremylt 
229fe2413ffSjeremylt   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
230d7b241e6Sjeremylt   memcpy(tmp, fieldname, len+1);
231fe2413ffSjeremylt   (*f)->fieldname = tmp;
2324d537eeaSYohann   (*f)->size = size;
233fe2413ffSjeremylt   (*f)->emode = emode;
234d7b241e6Sjeremylt   return 0;
235d7b241e6Sjeremylt }
236d7b241e6Sjeremylt 
237b11c1e72Sjeremylt /**
238a0a97fcfSJed Brown   @brief Add a CeedQFunction input
239b11c1e72Sjeremylt 
240b11c1e72Sjeremylt   @param qf         CeedQFunction
241b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
2424d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
2434d537eeaSYohann                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
244b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
245b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
246b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
247b11c1e72Sjeremylt 
248b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
249dfdf5a53Sjeremylt 
250dfdf5a53Sjeremylt   @ref Basic
251b11c1e72Sjeremylt **/
252d7b241e6Sjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname,
2534d537eeaSYohann                           CeedInt size, CeedEvalMode emode) {
254fe2413ffSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->inputfields[qf->numinputfields],
2554d537eeaSYohann                                    fieldname, size, emode);
256fe2413ffSjeremylt   CeedChk(ierr);
257fe2413ffSjeremylt   qf->numinputfields++;
258d7b241e6Sjeremylt   return 0;
259d7b241e6Sjeremylt }
260d7b241e6Sjeremylt 
261b11c1e72Sjeremylt /**
262a0a97fcfSJed Brown   @brief Add a CeedQFunction output
263b11c1e72Sjeremylt 
264b11c1e72Sjeremylt   @param qf         CeedQFunction
265b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
2664d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
2674d537eeaSYohann                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
268b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
269b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
270b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
271b11c1e72Sjeremylt 
272b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
273dfdf5a53Sjeremylt 
274dfdf5a53Sjeremylt   @ref Basic
275b11c1e72Sjeremylt **/
276d7b241e6Sjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
2774d537eeaSYohann                            CeedInt size, CeedEvalMode emode) {
278d7b241e6Sjeremylt   if (emode == CEED_EVAL_WEIGHT)
279*c042f62fSJeremy L Thompson     // LCOV_EXCL_START
280d7b241e6Sjeremylt     return CeedError(qf->ceed, 1,
2818c91a0c9SJeremy L Thompson                      "Cannot create QFunction output with CEED_EVAL_WEIGHT");
282*c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
283fe2413ffSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->outputfields[qf->numoutputfields],
2844d537eeaSYohann                                    fieldname, size, emode);
285fe2413ffSjeremylt   CeedChk(ierr);
286fe2413ffSjeremylt   qf->numoutputfields++;
287d7b241e6Sjeremylt   return 0;
288d7b241e6Sjeremylt }
289d7b241e6Sjeremylt 
290dfdf5a53Sjeremylt /**
2914ce2993fSjeremylt   @brief Get the Ceed associated with a CeedQFunction
2924ce2993fSjeremylt 
2934ce2993fSjeremylt   @param qf              CeedQFunction
2944ce2993fSjeremylt   @param[out] ceed       Variable to store Ceed
2954ce2993fSjeremylt 
2964ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
2974ce2993fSjeremylt 
29823617272Sjeremylt   @ref Advanced
2994ce2993fSjeremylt **/
3004ce2993fSjeremylt 
3014ce2993fSjeremylt int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
3024ce2993fSjeremylt   *ceed = qf->ceed;
3034ce2993fSjeremylt   return 0;
3044ce2993fSjeremylt }
3054ce2993fSjeremylt 
3064ce2993fSjeremylt /**
3074ce2993fSjeremylt   @brief Get the vector length of a CeedQFunction
3084ce2993fSjeremylt 
3094ce2993fSjeremylt   @param qf            CeedQFunction
310288c0443SJeremy L Thompson   @param[out] vlength  Variable to store vector length
3114ce2993fSjeremylt 
3124ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3134ce2993fSjeremylt 
31423617272Sjeremylt   @ref Advanced
3154ce2993fSjeremylt **/
3164ce2993fSjeremylt 
3174ce2993fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength) {
3184ce2993fSjeremylt   *vlength = qf->vlength;
3194ce2993fSjeremylt   return 0;
3204ce2993fSjeremylt }
3214ce2993fSjeremylt 
3224ce2993fSjeremylt /**
323dfdf5a53Sjeremylt   @brief Get the number of inputs and outputs to a CeedQFunction
324dfdf5a53Sjeremylt 
325dfdf5a53Sjeremylt   @param qf              CeedQFunction
3264ce2993fSjeremylt   @param[out] numinput   Variable to store number of input fields
3274ce2993fSjeremylt   @param[out] numoutput  Variable to store number of output fields
328dfdf5a53Sjeremylt 
329dfdf5a53Sjeremylt   @return An error code: 0 - success, otherwise - failure
330dfdf5a53Sjeremylt 
33123617272Sjeremylt   @ref Advanced
332dfdf5a53Sjeremylt **/
333dfdf5a53Sjeremylt 
334d7b241e6Sjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput,
335d7b241e6Sjeremylt                             CeedInt *numoutput) {
3361a4ead9bSjeremylt   if (numinput) *numinput = qf->numinputfields;
3371a4ead9bSjeremylt   if (numoutput) *numoutput = qf->numoutputfields;
338d7b241e6Sjeremylt   return 0;
339d7b241e6Sjeremylt }
340d7b241e6Sjeremylt 
341d7b241e6Sjeremylt /**
342288c0443SJeremy L Thompson   @brief Get the source path string for a CeedQFunction
3434ce2993fSjeremylt 
3444ce2993fSjeremylt   @param qf              CeedQFunction
345288c0443SJeremy L Thompson   @param[out] source     Variable to store source path string
3464ce2993fSjeremylt 
3474ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3484ce2993fSjeremylt 
34923617272Sjeremylt   @ref Advanced
3504ce2993fSjeremylt **/
3514ce2993fSjeremylt 
352288c0443SJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char* *source) {
353288c0443SJeremy L Thompson   *source = (char *) qf->sourcepath;
3544ce2993fSjeremylt   return 0;
3554ce2993fSjeremylt }
3564ce2993fSjeremylt 
3574ce2993fSjeremylt /**
358fe2413ffSjeremylt   @brief Get the User Function for a CeedQFunction
359fe2413ffSjeremylt 
360fe2413ffSjeremylt   @param qf              CeedQFunction
361fe2413ffSjeremylt   @param[out] f          Variable to store user function
362fe2413ffSjeremylt 
363fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
364fe2413ffSjeremylt 
365fe2413ffSjeremylt   @ref Advanced
366fe2413ffSjeremylt **/
367fe2413ffSjeremylt 
36828d161eeSjeremylt int CeedQFunctionGetUserFunction(CeedQFunction qf, int (**f)()) {
36928d161eeSjeremylt   *f = (int (*)())qf->function;
370fe2413ffSjeremylt   return 0;
371fe2413ffSjeremylt }
372fe2413ffSjeremylt 
373fe2413ffSjeremylt /**
3744ce2993fSjeremylt   @brief Get global context size for a CeedQFunction
3754ce2993fSjeremylt 
3764ce2993fSjeremylt   @param qf              CeedQFunction
3774ce2993fSjeremylt   @param[out] ctxsize    Variable to store size of context data values
3784ce2993fSjeremylt 
3794ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3804ce2993fSjeremylt 
38123617272Sjeremylt   @ref Advanced
3824ce2993fSjeremylt **/
3834ce2993fSjeremylt 
3844ce2993fSjeremylt int CeedQFunctionGetContextSize(CeedQFunction qf, size_t *ctxsize) {
385069aeabaSjeremylt   if (qf->fortranstatus) {
386069aeabaSjeremylt     fContext *fctx = qf->ctx;
387069aeabaSjeremylt     *ctxsize = fctx->innerctxsize;
388069aeabaSjeremylt   } else {
3894ce2993fSjeremylt     *ctxsize = qf->ctxsize;
390069aeabaSjeremylt   }
3914ce2993fSjeremylt   return 0;
3924ce2993fSjeremylt }
3934ce2993fSjeremylt 
3944ce2993fSjeremylt /**
3954ce2993fSjeremylt   @brief Get global context for a CeedQFunction
3964ce2993fSjeremylt 
3974ce2993fSjeremylt   @param qf              CeedQFunction
3984ce2993fSjeremylt   @param[out] ctx        Variable to store context data values
3994ce2993fSjeremylt 
4004ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
4014ce2993fSjeremylt 
40223617272Sjeremylt   @ref Advanced
4034ce2993fSjeremylt **/
4044ce2993fSjeremylt 
4054ce2993fSjeremylt int CeedQFunctionGetContext(CeedQFunction qf, void* *ctx) {
4064ce2993fSjeremylt   *ctx = qf->ctx;
4074ce2993fSjeremylt   return 0;
4084ce2993fSjeremylt }
4094ce2993fSjeremylt 
4104ce2993fSjeremylt /**
411418fb8c2Sjeremylt   @brief Determine if Fortran interface was used
412418fb8c2Sjeremylt 
413418fb8c2Sjeremylt   @param qf                  CeedQFunction
414418fb8c2Sjeremylt   @param[out] fortranstatus  Variable to store Fortran status
415418fb8c2Sjeremylt 
416418fb8c2Sjeremylt   @return An error code: 0 - success, otherwise - failure
417418fb8c2Sjeremylt 
418418fb8c2Sjeremylt   @ref Advanced
419418fb8c2Sjeremylt **/
420418fb8c2Sjeremylt 
421418fb8c2Sjeremylt int CeedQFunctionGetFortranStatus(CeedQFunction qf, bool *fortranstatus) {
422418fb8c2Sjeremylt   *fortranstatus = qf->fortranstatus;
423418fb8c2Sjeremylt   return 0;
424418fb8c2Sjeremylt }
425418fb8c2Sjeremylt 
426418fb8c2Sjeremylt /**
4270219ea01SJeremy L Thompson   @brief Determine if QFunction is identity
4280219ea01SJeremy L Thompson 
4290219ea01SJeremy L Thompson   @param qf               CeedQFunction
4300219ea01SJeremy L Thompson   @param[out] identity    Variable to store identity status
4310219ea01SJeremy L Thompson 
4320219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4330219ea01SJeremy L Thompson 
4340219ea01SJeremy L Thompson   @ref Advanced
4350219ea01SJeremy L Thompson **/
4360219ea01SJeremy L Thompson 
4370219ea01SJeremy L Thompson int CeedQFunctionGetIdentityStatus(CeedQFunction qf, bool *identity) {
4380219ea01SJeremy L Thompson   *identity = qf->identity;
4390219ea01SJeremy L Thompson   return 0;
4400219ea01SJeremy L Thompson }
4410219ea01SJeremy L Thompson 
4420219ea01SJeremy L Thompson /**
44314922b2aSjeremylt   @brief Get true user context for a CeedQFunction
444069aeabaSjeremylt 
445069aeabaSjeremylt   @param qf              CeedQFunction
446069aeabaSjeremylt   @param[out] ctx        Variable to store context data values
447069aeabaSjeremylt 
448069aeabaSjeremylt   @return An error code: 0 - success, otherwise - failure
449069aeabaSjeremylt 
450069aeabaSjeremylt   @ref Advanced
451069aeabaSjeremylt **/
452069aeabaSjeremylt 
45314922b2aSjeremylt int CeedQFunctionGetInnerContext(CeedQFunction qf, void* *ctx) {
45414922b2aSjeremylt   if (qf->fortranstatus) {
455069aeabaSjeremylt     fContext *fctx = qf->ctx;
456069aeabaSjeremylt     *ctx = fctx->innerctx;
45714922b2aSjeremylt   } else {
45814922b2aSjeremylt     *ctx = qf->ctx;
45914922b2aSjeremylt   }
46014922b2aSjeremylt 
4619f0427d9SYohann 
462069aeabaSjeremylt   return 0;
463069aeabaSjeremylt }
464069aeabaSjeremylt 
465069aeabaSjeremylt /**
4664ce2993fSjeremylt   @brief Get backend data of a CeedQFunction
4674ce2993fSjeremylt 
4684ce2993fSjeremylt   @param qf              CeedQFunction
4694ce2993fSjeremylt   @param[out] data       Variable to store data
4704ce2993fSjeremylt 
4714ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
4724ce2993fSjeremylt 
47323617272Sjeremylt   @ref Advanced
4744ce2993fSjeremylt **/
4754ce2993fSjeremylt 
4764ce2993fSjeremylt int CeedQFunctionGetData(CeedQFunction qf, void* *data) {
4774ce2993fSjeremylt   *data = qf->data;
4784ce2993fSjeremylt   return 0;
4794ce2993fSjeremylt }
4804ce2993fSjeremylt 
4814ce2993fSjeremylt /**
482fe2413ffSjeremylt   @brief Set backend data of a CeedQFunction
483fe2413ffSjeremylt 
484fe2413ffSjeremylt   @param[out] qf         CeedQFunction
485fe2413ffSjeremylt   @param data            Data to set
486fe2413ffSjeremylt 
487fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
488fe2413ffSjeremylt 
489fe2413ffSjeremylt   @ref Advanced
490fe2413ffSjeremylt **/
491fe2413ffSjeremylt 
492fe2413ffSjeremylt int CeedQFunctionSetData(CeedQFunction qf, void* *data) {
493fe2413ffSjeremylt   qf->data = *data;
494fe2413ffSjeremylt   return 0;
495fe2413ffSjeremylt }
496fe2413ffSjeremylt 
497fe2413ffSjeremylt /**
4984ce2993fSjeremylt   @brief Set global context for a CeedQFunction
499b11c1e72Sjeremylt 
500b11c1e72Sjeremylt   @param qf       CeedQFunction
501b11c1e72Sjeremylt   @param ctx      Context data to set
502b11c1e72Sjeremylt   @param ctxsize  Size of context data values
503b11c1e72Sjeremylt 
504b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
505dfdf5a53Sjeremylt 
506dfdf5a53Sjeremylt   @ref Basic
507b11c1e72Sjeremylt **/
508d7b241e6Sjeremylt int CeedQFunctionSetContext(CeedQFunction qf, void *ctx, size_t ctxsize) {
509d7b241e6Sjeremylt   qf->ctx = ctx;
510d7b241e6Sjeremylt   qf->ctxsize = ctxsize;
511d7b241e6Sjeremylt   return 0;
512d7b241e6Sjeremylt }
513d7b241e6Sjeremylt 
514b11c1e72Sjeremylt /**
515b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
516b11c1e72Sjeremylt 
517b11c1e72Sjeremylt   @param qf      CeedQFunction
518b11c1e72Sjeremylt   @param Q       Number of quadrature points
519b11c1e72Sjeremylt   @param[in] u   Array of input data arrays
520b11c1e72Sjeremylt   @param[out] v  Array of output data arrays
521b11c1e72Sjeremylt 
522b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
523dfdf5a53Sjeremylt 
524dfdf5a53Sjeremylt   @ref Advanced
525b11c1e72Sjeremylt **/
526d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
527aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
528d7b241e6Sjeremylt   int ierr;
529d7b241e6Sjeremylt   if (!qf->Apply)
530*c042f62fSJeremy L Thompson     // LCOV_EXCL_START
531d7b241e6Sjeremylt     return CeedError(qf->ceed, 1, "Backend does not support QFunctionApply");
532*c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
533d7b241e6Sjeremylt   if (Q % qf->vlength)
534*c042f62fSJeremy L Thompson     // LCOV_EXCL_START
535d7b241e6Sjeremylt     return CeedError(qf->ceed, 2,
536d7b241e6Sjeremylt                      "Number of quadrature points %d must be a multiple of %d",
537d7b241e6Sjeremylt                      Q, qf->vlength);
538*c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
539d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
540d7b241e6Sjeremylt   return 0;
541d7b241e6Sjeremylt }
542d7b241e6Sjeremylt 
543b11c1e72Sjeremylt /**
544d1bcdac9Sjeremylt   @brief Get the CeedQFunctionFields of a CeedQFunction
545d1bcdac9Sjeremylt 
546d1bcdac9Sjeremylt   @param qf                 CeedQFunction
547d1bcdac9Sjeremylt   @param[out] inputfields   Variable to store inputfields
548d1bcdac9Sjeremylt   @param[out] outputfields  Variable to store outputfields
549d1bcdac9Sjeremylt 
550d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
551d1bcdac9Sjeremylt 
552d1bcdac9Sjeremylt   @ref Advanced
553d1bcdac9Sjeremylt **/
554d1bcdac9Sjeremylt 
555d1bcdac9Sjeremylt int CeedQFunctionGetFields(CeedQFunction qf,
556d1bcdac9Sjeremylt                            CeedQFunctionField* *inputfields,
557d1bcdac9Sjeremylt                            CeedQFunctionField* *outputfields) {
558d1bcdac9Sjeremylt   if (inputfields) *inputfields = qf->inputfields;
559d1bcdac9Sjeremylt   if (outputfields) *outputfields = qf->outputfields;
560d1bcdac9Sjeremylt   return 0;
561d1bcdac9Sjeremylt }
562d1bcdac9Sjeremylt 
563d1bcdac9Sjeremylt /**
564fe2413ffSjeremylt   @brief Get the name of a CeedQFunctionField
565fe2413ffSjeremylt 
566fe2413ffSjeremylt   @param qffield         CeedQFunctionField
567fe2413ffSjeremylt   @param[out] fieldname  Variable to store the field name
568fe2413ffSjeremylt 
569fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
570fe2413ffSjeremylt 
571fe2413ffSjeremylt   @ref Advanced
572fe2413ffSjeremylt **/
573fe2413ffSjeremylt 
574fe2413ffSjeremylt int CeedQFunctionFieldGetName(CeedQFunctionField qffield,
575fe2413ffSjeremylt                               char* *fieldname) {
576fe2413ffSjeremylt   *fieldname = (char *)qffield->fieldname;
577fe2413ffSjeremylt   return 0;
578fe2413ffSjeremylt }
579fe2413ffSjeremylt 
580fe2413ffSjeremylt /**
581d1bcdac9Sjeremylt   @brief Get the number of components of a CeedQFunctionField
582d1bcdac9Sjeremylt 
583d1bcdac9Sjeremylt   @param qffield    CeedQFunctionField
5844d537eeaSYohann   @param[out] size  Variable to store the size of the field
585d1bcdac9Sjeremylt 
586d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
587d1bcdac9Sjeremylt 
588d1bcdac9Sjeremylt   @ref Advanced
589d1bcdac9Sjeremylt **/
590d1bcdac9Sjeremylt 
5914d537eeaSYohann int CeedQFunctionFieldGetSize(CeedQFunctionField qffield, CeedInt *size) {
5924d537eeaSYohann   *size = qffield->size;
593d1bcdac9Sjeremylt   return 0;
594d1bcdac9Sjeremylt }
595d1bcdac9Sjeremylt 
596d1bcdac9Sjeremylt /**
597d1bcdac9Sjeremylt   @brief Get the CeedEvalMode of a CeedQFunctionField
598d1bcdac9Sjeremylt 
599d1bcdac9Sjeremylt   @param qffield         CeedQFunctionField
600288c0443SJeremy L Thompson   @param[out] emode      Variable to store the field evaluation mode
601d1bcdac9Sjeremylt 
602d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
603d1bcdac9Sjeremylt 
604d1bcdac9Sjeremylt   @ref Advanced
605d1bcdac9Sjeremylt **/
606d1bcdac9Sjeremylt 
607d1bcdac9Sjeremylt int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qffield,
608d1bcdac9Sjeremylt                                   CeedEvalMode *emode) {
609fe2413ffSjeremylt   *emode = qffield->emode;
610d1bcdac9Sjeremylt   return 0;
611d1bcdac9Sjeremylt }
612d1bcdac9Sjeremylt 
613d1bcdac9Sjeremylt /**
614b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
615b11c1e72Sjeremylt 
616b11c1e72Sjeremylt   @param qf CeedQFunction to destroy
617b11c1e72Sjeremylt 
618b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
619dfdf5a53Sjeremylt 
620dfdf5a53Sjeremylt   @ref Basic
621b11c1e72Sjeremylt **/
622d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
623d7b241e6Sjeremylt   int ierr;
624d7b241e6Sjeremylt 
625d7b241e6Sjeremylt   if (!*qf || --(*qf)->refcount > 0) return 0;
626fe2413ffSjeremylt   // Backend destroy
627d7b241e6Sjeremylt   if ((*qf)->Destroy) {
628d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
629d7b241e6Sjeremylt   }
630fe2413ffSjeremylt   // Free fields
631fe2413ffSjeremylt   for (int i=0; i<(*qf)->numinputfields; i++) {
632fe2413ffSjeremylt     ierr = CeedFree(&(*(*qf)->inputfields[i]).fieldname); CeedChk(ierr);
633fe2413ffSjeremylt     ierr = CeedFree(&(*qf)->inputfields[i]); CeedChk(ierr);
634fe2413ffSjeremylt   }
635fe2413ffSjeremylt   for (int i=0; i<(*qf)->numoutputfields; i++) {
636fe2413ffSjeremylt     ierr = CeedFree(&(*(*qf)->outputfields[i]).fieldname); CeedChk(ierr);
637fe2413ffSjeremylt     ierr = CeedFree(&(*qf)->outputfields[i]); CeedChk(ierr);
638fe2413ffSjeremylt   }
639fe2413ffSjeremylt   ierr = CeedFree(&(*qf)->inputfields); CeedChk(ierr);
640fe2413ffSjeremylt   ierr = CeedFree(&(*qf)->outputfields); CeedChk(ierr);
6410219ea01SJeremy L Thompson   // Free ctx if identity
6420219ea01SJeremy L Thompson   if ((*qf)->identity) {
6430219ea01SJeremy L Thompson     ierr = CeedFree(&(*qf)->ctx); CeedChk(ierr);
6440219ea01SJeremy L Thompson   }
645fe2413ffSjeremylt 
646288c0443SJeremy L Thompson   ierr = CeedFree(&(*qf)->sourcepath); CeedChk(ierr);
647d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
648d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
649d7b241e6Sjeremylt   return 0;
650d7b241e6Sjeremylt }
651d7b241e6Sjeremylt 
652d7b241e6Sjeremylt /// @}
653