xref: /libCEED/interface/ceed-qfunction.c (revision 23e8ed120698a6abe85191780d601293bf464944)
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)
70c042f62fSJeremy L Thompson       // LCOV_EXCL_START
71d7b241e6Sjeremylt       return CeedError(ceed, 1, "Backend does not support QFunctionCreate");
72c042f62fSJeremy 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;
8607a02837SJed Brown   size_t slen = strlen(source) + 1;
8707a02837SJed Brown   ierr = CeedMalloc(slen, &source_copy); CeedChk(ierr);
8807a02837SJed Brown   memcpy(source_copy, source, slen);
89288c0443SJeremy L Thompson   (*qf)->sourcepath = source_copy;
90fe2413ffSjeremylt   ierr = CeedCalloc(16, &(*qf)->inputfields); CeedChk(ierr);
91fe2413ffSjeremylt   ierr = CeedCalloc(16, &(*qf)->outputfields); CeedChk(ierr);
92d7b241e6Sjeremylt   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
93d7b241e6Sjeremylt   return 0;
94d7b241e6Sjeremylt }
95d7b241e6Sjeremylt 
96b11c1e72Sjeremylt /**
97288c0443SJeremy L Thompson   @brief Register a gallery QFunction
98288c0443SJeremy L Thompson 
99288c0443SJeremy L Thompson   @param name    Name for this backend to respond to
1001176cc3aSJeremy L Thompson   @param source  Absolute path to source of QFunction,
1011176cc3aSJeremy L Thompson                    "\path\CEED_DIR\gallery\folder\file.h:function_name"
102288c0443SJeremy L Thompson   @param vlength Vector length.  Caller must ensure that number of quadrature
103288c0443SJeremy L Thompson                    points is a multiple of vlength.
104288c0443SJeremy L Thompson   @param f       Function pointer to evaluate action at quadrature points.
105288c0443SJeremy L Thompson                    See \ref CeedQFunctionUser.
106288c0443SJeremy L Thompson   @param init    Initialization function called by CeedQFunctionInit() when the
107288c0443SJeremy L Thompson                    QFunction is selected.
108288c0443SJeremy L Thompson 
109288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
110288c0443SJeremy L Thompson 
111288c0443SJeremy L Thompson   @ref Advanced
112288c0443SJeremy L Thompson **/
113288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source,
114288c0443SJeremy L Thompson                           CeedInt vlength, CeedQFunctionUser f,
115288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
116c042f62fSJeremy L Thompson   if (num_qfunctions >= sizeof(qfunctions) / sizeof(qfunctions[0]))
117c042f62fSJeremy L Thompson     // LCOV_EXCL_START
118288c0443SJeremy L Thompson     return CeedError(NULL, 1, "Too many gallery QFunctions");
119c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
120c042f62fSJeremy L Thompson 
121288c0443SJeremy L Thompson   strncpy(qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
122288c0443SJeremy L Thompson   qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
123288c0443SJeremy L Thompson   strncpy(qfunctions[num_qfunctions].source, source, CEED_MAX_RESOURCE_LEN);
124288c0443SJeremy L Thompson   qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
125288c0443SJeremy L Thompson   qfunctions[num_qfunctions].vlength = vlength;
126288c0443SJeremy L Thompson   qfunctions[num_qfunctions].f = f;
127288c0443SJeremy L Thompson   qfunctions[num_qfunctions].init = init;
128288c0443SJeremy L Thompson   num_qfunctions++;
129288c0443SJeremy L Thompson   return 0;
130288c0443SJeremy L Thompson }
131288c0443SJeremy L Thompson 
132288c0443SJeremy L Thompson /**
133288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
134288c0443SJeremy L Thompson 
135288c0443SJeremy L Thompson   @param ceed       A Ceed object where the CeedQFunction will be created
136288c0443SJeremy L Thompson   @param name       Name of QFunction to use from gallery
137288c0443SJeremy L Thompson   @param[out] qf    Address of the variable where the newly created
138288c0443SJeremy L Thompson                       CeedQFunction will be stored
139288c0443SJeremy L Thompson 
140288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
141288c0443SJeremy L Thompson 
142288c0443SJeremy L Thompson   @ref Basic
143288c0443SJeremy L Thompson **/
144288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
145288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
146288c0443SJeremy L Thompson   int ierr;
147288c0443SJeremy L Thompson   size_t matchlen = 0, matchidx = UINT_MAX;
148288c0443SJeremy L Thompson 
149288c0443SJeremy L Thompson   // Find matching backend
150288c0443SJeremy L Thompson   if (!name) return CeedError(NULL, 1, "No QFunction name provided");
151288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
152288c0443SJeremy L Thompson     size_t n;
153288c0443SJeremy L Thompson     const char *currname = qfunctions[i].name;
154288c0443SJeremy L Thompson     for (n = 0; currname[n] && currname[n] == name[n]; n++) {}
155288c0443SJeremy L Thompson     if (n > matchlen) {
156288c0443SJeremy L Thompson       matchlen = n;
157288c0443SJeremy L Thompson       matchidx = i;
158288c0443SJeremy L Thompson     }
159288c0443SJeremy L Thompson   }
1601d102b48SJeremy L Thompson   if (!matchlen)
161138d4072Sjeremylt     // LCOV_EXCL_START
1621d102b48SJeremy L Thompson     return CeedError(NULL, 1, "No suitable gallery QFunction");
163138d4072Sjeremylt   // LCOV_EXCL_STOP
164288c0443SJeremy L Thompson 
165288c0443SJeremy L Thompson   // Create QFunction
166288c0443SJeremy L Thompson   ierr = CeedQFunctionCreateInterior(ceed, qfunctions[matchidx].vlength,
167288c0443SJeremy L Thompson                                      qfunctions[matchidx].f,
168288c0443SJeremy L Thompson                                      qfunctions[matchidx].source, qf);
169288c0443SJeremy L Thompson   CeedChk(ierr);
170288c0443SJeremy L Thompson 
171288c0443SJeremy L Thompson   // QFunction specific setup
172288c0443SJeremy L Thompson   ierr = qfunctions[matchidx].init(ceed, name, *qf); CeedChk(ierr);
173288c0443SJeremy L Thompson 
174288c0443SJeremy L Thompson   return 0;
175288c0443SJeremy L Thompson }
176288c0443SJeremy L Thompson 
177288c0443SJeremy L Thompson /**
1780219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
1790219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
1800219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
1810219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
1820219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
1830219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
1840219ea01SJeremy L Thompson            location for both.
1850219ea01SJeremy L Thompson 
1860219ea01SJeremy L Thompson   @param ceed       A Ceed object where the CeedQFunction will be created
1870219ea01SJeremy L Thompson   @param size       Size of the qfunction fields
1880219ea01SJeremy L Thompson   @param[out] qf    Address of the variable where the newly created
1890219ea01SJeremy L Thompson                       CeedQFunction will be stored
1900219ea01SJeremy L Thompson 
1910219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1920219ea01SJeremy L Thompson 
1930219ea01SJeremy L Thompson   @ref Basic
1940219ea01SJeremy L Thompson **/
1950219ea01SJeremy L Thompson int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedQFunction *qf) {
1960219ea01SJeremy L Thompson   int ierr;
1970219ea01SJeremy L Thompson 
1980219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
1990219ea01SJeremy L Thompson 
2000219ea01SJeremy L Thompson   (*qf)->identity = 1;
2010219ea01SJeremy L Thompson   if (size > 1) {
2020219ea01SJeremy L Thompson     CeedInt *ctx;
2030219ea01SJeremy L Thompson     ierr = CeedCalloc(1, &ctx); CeedChk(ierr);
2040219ea01SJeremy L Thompson     ctx[0] = size;
2050219ea01SJeremy L Thompson     ierr = CeedQFunctionSetContext(*qf, ctx, sizeof(ctx)); CeedChk(ierr);
206c0fea178Sjeremylt     (*qf)->inputfields[0]->size = size;
207c0fea178Sjeremylt     (*qf)->outputfields[0]->size = size;
2080219ea01SJeremy L Thompson   }
2090219ea01SJeremy L Thompson 
2100219ea01SJeremy L Thompson   return 0;
2110219ea01SJeremy L Thompson }
2120219ea01SJeremy L Thompson 
2130219ea01SJeremy L Thompson /**
214a0a97fcfSJed Brown   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
215b11c1e72Sjeremylt 
216b11c1e72Sjeremylt   @param f          CeedQFunctionField
217b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
2184d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
219*23e8ed12Sjeremylt                       1 for CEED_EVAL_NONE, CEED_EVAL_INTERP, and CEED_EVAL_WEIGHT)
220b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
221b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
222*23e8ed12Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients,
223*23e8ed12Sjeremylt                       \ref CEED_EVAL_WEIGHT to use quadrature weights.
224b11c1e72Sjeremylt 
225b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
226dfdf5a53Sjeremylt 
227dfdf5a53Sjeremylt   @ref Developer
228b11c1e72Sjeremylt **/
229fe2413ffSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *fieldname,
2304d537eeaSYohann                                  CeedInt size, CeedEvalMode emode) {
231d7b241e6Sjeremylt   size_t len = strlen(fieldname);
232d7b241e6Sjeremylt   char *tmp;
233fe2413ffSjeremylt   int ierr;
234fe2413ffSjeremylt   ierr = CeedCalloc(1,f); CeedChk(ierr);
235fe2413ffSjeremylt 
236fe2413ffSjeremylt   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
237d7b241e6Sjeremylt   memcpy(tmp, fieldname, len+1);
238fe2413ffSjeremylt   (*f)->fieldname = tmp;
2394d537eeaSYohann   (*f)->size = size;
240fe2413ffSjeremylt   (*f)->emode = emode;
241d7b241e6Sjeremylt   return 0;
242d7b241e6Sjeremylt }
243d7b241e6Sjeremylt 
244b11c1e72Sjeremylt /**
245a0a97fcfSJed Brown   @brief Add a CeedQFunction input
246b11c1e72Sjeremylt 
247b11c1e72Sjeremylt   @param qf         CeedQFunction
248b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
2494d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
2504d537eeaSYohann                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
251b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
252b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
253b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
254b11c1e72Sjeremylt 
255b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
256dfdf5a53Sjeremylt 
257dfdf5a53Sjeremylt   @ref Basic
258b11c1e72Sjeremylt **/
2591d102b48SJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname, CeedInt size,
2601d102b48SJeremy L Thompson                           CeedEvalMode emode) {
261fe2413ffSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->inputfields[qf->numinputfields],
2624d537eeaSYohann                                    fieldname, size, emode);
263fe2413ffSjeremylt   CeedChk(ierr);
264fe2413ffSjeremylt   qf->numinputfields++;
265d7b241e6Sjeremylt   return 0;
266d7b241e6Sjeremylt }
267d7b241e6Sjeremylt 
268b11c1e72Sjeremylt /**
269a0a97fcfSJed Brown   @brief Add a CeedQFunction output
270b11c1e72Sjeremylt 
271b11c1e72Sjeremylt   @param qf         CeedQFunction
272b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
2734d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
2744d537eeaSYohann                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
275b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
276b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
277b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
278b11c1e72Sjeremylt 
279b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
280dfdf5a53Sjeremylt 
281dfdf5a53Sjeremylt   @ref Basic
282b11c1e72Sjeremylt **/
283d7b241e6Sjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
2844d537eeaSYohann                            CeedInt size, CeedEvalMode emode) {
285d7b241e6Sjeremylt   if (emode == CEED_EVAL_WEIGHT)
286c042f62fSJeremy L Thompson     // LCOV_EXCL_START
2871d102b48SJeremy L Thompson     return CeedError(qf->ceed, 1, "Cannot create QFunction output with "
2881d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
289c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
290fe2413ffSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->outputfields[qf->numoutputfields],
2914d537eeaSYohann                                    fieldname, size, emode);
292fe2413ffSjeremylt   CeedChk(ierr);
293fe2413ffSjeremylt   qf->numoutputfields++;
294d7b241e6Sjeremylt   return 0;
295d7b241e6Sjeremylt }
296d7b241e6Sjeremylt 
297dfdf5a53Sjeremylt /**
2984ce2993fSjeremylt   @brief Get the Ceed associated with a CeedQFunction
2994ce2993fSjeremylt 
3004ce2993fSjeremylt   @param qf              CeedQFunction
3014ce2993fSjeremylt   @param[out] ceed       Variable to store Ceed
3024ce2993fSjeremylt 
3034ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3044ce2993fSjeremylt 
30523617272Sjeremylt   @ref Advanced
3064ce2993fSjeremylt **/
3074ce2993fSjeremylt 
3084ce2993fSjeremylt int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
3094ce2993fSjeremylt   *ceed = qf->ceed;
3104ce2993fSjeremylt   return 0;
3114ce2993fSjeremylt }
3124ce2993fSjeremylt 
3134ce2993fSjeremylt /**
3144ce2993fSjeremylt   @brief Get the vector length of a CeedQFunction
3154ce2993fSjeremylt 
3164ce2993fSjeremylt   @param qf            CeedQFunction
317288c0443SJeremy L Thompson   @param[out] vlength  Variable to store vector length
3184ce2993fSjeremylt 
3194ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3204ce2993fSjeremylt 
32123617272Sjeremylt   @ref Advanced
3224ce2993fSjeremylt **/
3234ce2993fSjeremylt 
3244ce2993fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength) {
3254ce2993fSjeremylt   *vlength = qf->vlength;
3264ce2993fSjeremylt   return 0;
3274ce2993fSjeremylt }
3284ce2993fSjeremylt 
3294ce2993fSjeremylt /**
330dfdf5a53Sjeremylt   @brief Get the number of inputs and outputs to a CeedQFunction
331dfdf5a53Sjeremylt 
332dfdf5a53Sjeremylt   @param qf              CeedQFunction
3334ce2993fSjeremylt   @param[out] numinput   Variable to store number of input fields
3344ce2993fSjeremylt   @param[out] numoutput  Variable to store number of output fields
335dfdf5a53Sjeremylt 
336dfdf5a53Sjeremylt   @return An error code: 0 - success, otherwise - failure
337dfdf5a53Sjeremylt 
33823617272Sjeremylt   @ref Advanced
339dfdf5a53Sjeremylt **/
340dfdf5a53Sjeremylt 
341d7b241e6Sjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput,
342d7b241e6Sjeremylt                             CeedInt *numoutput) {
3431a4ead9bSjeremylt   if (numinput) *numinput = qf->numinputfields;
3441a4ead9bSjeremylt   if (numoutput) *numoutput = qf->numoutputfields;
345d7b241e6Sjeremylt   return 0;
346d7b241e6Sjeremylt }
347d7b241e6Sjeremylt 
348d7b241e6Sjeremylt /**
349288c0443SJeremy L Thompson   @brief Get the source path string for a CeedQFunction
3504ce2993fSjeremylt 
3514ce2993fSjeremylt   @param qf              CeedQFunction
352288c0443SJeremy L Thompson   @param[out] source     Variable to store source path string
3534ce2993fSjeremylt 
3544ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3554ce2993fSjeremylt 
35623617272Sjeremylt   @ref Advanced
3574ce2993fSjeremylt **/
3584ce2993fSjeremylt 
359288c0443SJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char* *source) {
360288c0443SJeremy L Thompson   *source = (char *) qf->sourcepath;
3614ce2993fSjeremylt   return 0;
3624ce2993fSjeremylt }
3634ce2993fSjeremylt 
3644ce2993fSjeremylt /**
365fe2413ffSjeremylt   @brief Get the User Function for a CeedQFunction
366fe2413ffSjeremylt 
367fe2413ffSjeremylt   @param qf              CeedQFunction
368fe2413ffSjeremylt   @param[out] f          Variable to store user function
369fe2413ffSjeremylt 
370fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
371fe2413ffSjeremylt 
372fe2413ffSjeremylt   @ref Advanced
373fe2413ffSjeremylt **/
374fe2413ffSjeremylt 
37528d161eeSjeremylt int CeedQFunctionGetUserFunction(CeedQFunction qf, int (**f)()) {
37628d161eeSjeremylt   *f = (int (*)())qf->function;
377fe2413ffSjeremylt   return 0;
378fe2413ffSjeremylt }
379fe2413ffSjeremylt 
380fe2413ffSjeremylt /**
3814ce2993fSjeremylt   @brief Get global context size for a CeedQFunction
3824ce2993fSjeremylt 
3834ce2993fSjeremylt   @param qf              CeedQFunction
3844ce2993fSjeremylt   @param[out] ctxsize    Variable to store size of context data values
3854ce2993fSjeremylt 
3864ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3874ce2993fSjeremylt 
38823617272Sjeremylt   @ref Advanced
3894ce2993fSjeremylt **/
3904ce2993fSjeremylt 
3914ce2993fSjeremylt int CeedQFunctionGetContextSize(CeedQFunction qf, size_t *ctxsize) {
392069aeabaSjeremylt   if (qf->fortranstatus) {
393069aeabaSjeremylt     fContext *fctx = qf->ctx;
394069aeabaSjeremylt     *ctxsize = fctx->innerctxsize;
395069aeabaSjeremylt   } else {
3964ce2993fSjeremylt     *ctxsize = qf->ctxsize;
397069aeabaSjeremylt   }
3984ce2993fSjeremylt   return 0;
3994ce2993fSjeremylt }
4004ce2993fSjeremylt 
4014ce2993fSjeremylt /**
4024ce2993fSjeremylt   @brief Get global context for a CeedQFunction
4034ce2993fSjeremylt 
4044ce2993fSjeremylt   @param qf              CeedQFunction
4054ce2993fSjeremylt   @param[out] ctx        Variable to store context data values
4064ce2993fSjeremylt 
4074ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
4084ce2993fSjeremylt 
40923617272Sjeremylt   @ref Advanced
4104ce2993fSjeremylt **/
4114ce2993fSjeremylt 
4124ce2993fSjeremylt int CeedQFunctionGetContext(CeedQFunction qf, void* *ctx) {
4134ce2993fSjeremylt   *ctx = qf->ctx;
4144ce2993fSjeremylt   return 0;
4154ce2993fSjeremylt }
4164ce2993fSjeremylt 
4174ce2993fSjeremylt /**
418418fb8c2Sjeremylt   @brief Determine if Fortran interface was used
419418fb8c2Sjeremylt 
420418fb8c2Sjeremylt   @param qf                  CeedQFunction
421418fb8c2Sjeremylt   @param[out] fortranstatus  Variable to store Fortran status
422418fb8c2Sjeremylt 
423418fb8c2Sjeremylt   @return An error code: 0 - success, otherwise - failure
424418fb8c2Sjeremylt 
425418fb8c2Sjeremylt   @ref Advanced
426418fb8c2Sjeremylt **/
427418fb8c2Sjeremylt 
428418fb8c2Sjeremylt int CeedQFunctionGetFortranStatus(CeedQFunction qf, bool *fortranstatus) {
429418fb8c2Sjeremylt   *fortranstatus = qf->fortranstatus;
430418fb8c2Sjeremylt   return 0;
431418fb8c2Sjeremylt }
432418fb8c2Sjeremylt 
433418fb8c2Sjeremylt /**
4340219ea01SJeremy L Thompson   @brief Determine if QFunction is identity
4350219ea01SJeremy L Thompson 
4360219ea01SJeremy L Thompson   @param qf               CeedQFunction
4370219ea01SJeremy L Thompson   @param[out] identity    Variable to store identity status
4380219ea01SJeremy L Thompson 
4390219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4400219ea01SJeremy L Thompson 
4410219ea01SJeremy L Thompson   @ref Advanced
4420219ea01SJeremy L Thompson **/
4430219ea01SJeremy L Thompson 
4440219ea01SJeremy L Thompson int CeedQFunctionGetIdentityStatus(CeedQFunction qf, bool *identity) {
4450219ea01SJeremy L Thompson   *identity = qf->identity;
4460219ea01SJeremy L Thompson   return 0;
4470219ea01SJeremy L Thompson }
4480219ea01SJeremy L Thompson 
4490219ea01SJeremy L Thompson /**
45014922b2aSjeremylt   @brief Get true user context for a CeedQFunction
451069aeabaSjeremylt 
452069aeabaSjeremylt   @param qf              CeedQFunction
453069aeabaSjeremylt   @param[out] ctx        Variable to store context data values
454069aeabaSjeremylt 
455069aeabaSjeremylt   @return An error code: 0 - success, otherwise - failure
456069aeabaSjeremylt 
457069aeabaSjeremylt   @ref Advanced
458069aeabaSjeremylt **/
459069aeabaSjeremylt 
46014922b2aSjeremylt int CeedQFunctionGetInnerContext(CeedQFunction qf, void* *ctx) {
46114922b2aSjeremylt   if (qf->fortranstatus) {
462069aeabaSjeremylt     fContext *fctx = qf->ctx;
463069aeabaSjeremylt     *ctx = fctx->innerctx;
46414922b2aSjeremylt   } else {
46514922b2aSjeremylt     *ctx = qf->ctx;
46614922b2aSjeremylt   }
46714922b2aSjeremylt 
4689f0427d9SYohann 
469069aeabaSjeremylt   return 0;
470069aeabaSjeremylt }
471069aeabaSjeremylt 
472069aeabaSjeremylt /**
4734ce2993fSjeremylt   @brief Get backend data of a CeedQFunction
4744ce2993fSjeremylt 
4754ce2993fSjeremylt   @param qf              CeedQFunction
4764ce2993fSjeremylt   @param[out] data       Variable to store data
4774ce2993fSjeremylt 
4784ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
4794ce2993fSjeremylt 
48023617272Sjeremylt   @ref Advanced
4814ce2993fSjeremylt **/
4824ce2993fSjeremylt 
4834ce2993fSjeremylt int CeedQFunctionGetData(CeedQFunction qf, void* *data) {
4844ce2993fSjeremylt   *data = qf->data;
4854ce2993fSjeremylt   return 0;
4864ce2993fSjeremylt }
4874ce2993fSjeremylt 
4884ce2993fSjeremylt /**
489fe2413ffSjeremylt   @brief Set backend data of a CeedQFunction
490fe2413ffSjeremylt 
491fe2413ffSjeremylt   @param[out] qf         CeedQFunction
492fe2413ffSjeremylt   @param data            Data to set
493fe2413ffSjeremylt 
494fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
495fe2413ffSjeremylt 
496fe2413ffSjeremylt   @ref Advanced
497fe2413ffSjeremylt **/
498fe2413ffSjeremylt 
499fe2413ffSjeremylt int CeedQFunctionSetData(CeedQFunction qf, void* *data) {
500fe2413ffSjeremylt   qf->data = *data;
501fe2413ffSjeremylt   return 0;
502fe2413ffSjeremylt }
503fe2413ffSjeremylt 
504fe2413ffSjeremylt /**
5054ce2993fSjeremylt   @brief Set global context for a CeedQFunction
506b11c1e72Sjeremylt 
507b11c1e72Sjeremylt   @param qf       CeedQFunction
508b11c1e72Sjeremylt   @param ctx      Context data to set
509b11c1e72Sjeremylt   @param ctxsize  Size of context data values
510b11c1e72Sjeremylt 
511b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
512dfdf5a53Sjeremylt 
513dfdf5a53Sjeremylt   @ref Basic
514b11c1e72Sjeremylt **/
515d7b241e6Sjeremylt int CeedQFunctionSetContext(CeedQFunction qf, void *ctx, size_t ctxsize) {
516d7b241e6Sjeremylt   qf->ctx = ctx;
517d7b241e6Sjeremylt   qf->ctxsize = ctxsize;
518d7b241e6Sjeremylt   return 0;
519d7b241e6Sjeremylt }
520d7b241e6Sjeremylt 
521b11c1e72Sjeremylt /**
522b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
523b11c1e72Sjeremylt 
524b11c1e72Sjeremylt   @param qf      CeedQFunction
525b11c1e72Sjeremylt   @param Q       Number of quadrature points
526b11c1e72Sjeremylt   @param[in] u   Array of input data arrays
527b11c1e72Sjeremylt   @param[out] v  Array of output data arrays
528b11c1e72Sjeremylt 
529b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
530dfdf5a53Sjeremylt 
531dfdf5a53Sjeremylt   @ref Advanced
532b11c1e72Sjeremylt **/
533d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
534aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
535d7b241e6Sjeremylt   int ierr;
536d7b241e6Sjeremylt   if (!qf->Apply)
537c042f62fSJeremy L Thompson     // LCOV_EXCL_START
538d7b241e6Sjeremylt     return CeedError(qf->ceed, 1, "Backend does not support QFunctionApply");
539c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
540d7b241e6Sjeremylt   if (Q % qf->vlength)
541c042f62fSJeremy L Thompson     // LCOV_EXCL_START
5421d102b48SJeremy L Thompson     return CeedError(qf->ceed, 2, "Number of quadrature points %d must be a "
5431d102b48SJeremy L Thompson                      "multiple of %d", Q, qf->vlength);
544c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
545d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
546d7b241e6Sjeremylt   return 0;
547d7b241e6Sjeremylt }
548d7b241e6Sjeremylt 
549b11c1e72Sjeremylt /**
550d1bcdac9Sjeremylt   @brief Get the CeedQFunctionFields of a CeedQFunction
551d1bcdac9Sjeremylt 
552d1bcdac9Sjeremylt   @param qf                 CeedQFunction
553d1bcdac9Sjeremylt   @param[out] inputfields   Variable to store inputfields
554d1bcdac9Sjeremylt   @param[out] outputfields  Variable to store outputfields
555d1bcdac9Sjeremylt 
556d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
557d1bcdac9Sjeremylt 
558d1bcdac9Sjeremylt   @ref Advanced
559d1bcdac9Sjeremylt **/
560d1bcdac9Sjeremylt 
561d1bcdac9Sjeremylt int CeedQFunctionGetFields(CeedQFunction qf,
562d1bcdac9Sjeremylt                            CeedQFunctionField* *inputfields,
563d1bcdac9Sjeremylt                            CeedQFunctionField* *outputfields) {
5641d102b48SJeremy L Thompson   if (inputfields)
5651d102b48SJeremy L Thompson     *inputfields = qf->inputfields;
5661d102b48SJeremy L Thompson   if (outputfields)
5671d102b48SJeremy L Thompson     *outputfields = qf->outputfields;
568d1bcdac9Sjeremylt   return 0;
569d1bcdac9Sjeremylt }
570d1bcdac9Sjeremylt 
571d1bcdac9Sjeremylt /**
572fe2413ffSjeremylt   @brief Get the name of a CeedQFunctionField
573fe2413ffSjeremylt 
574fe2413ffSjeremylt   @param qffield         CeedQFunctionField
575fe2413ffSjeremylt   @param[out] fieldname  Variable to store the field name
576fe2413ffSjeremylt 
577fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
578fe2413ffSjeremylt 
579fe2413ffSjeremylt   @ref Advanced
580fe2413ffSjeremylt **/
581fe2413ffSjeremylt 
582fe2413ffSjeremylt int CeedQFunctionFieldGetName(CeedQFunctionField qffield,
583fe2413ffSjeremylt                               char* *fieldname) {
584fe2413ffSjeremylt   *fieldname = (char *)qffield->fieldname;
585fe2413ffSjeremylt   return 0;
586fe2413ffSjeremylt }
587fe2413ffSjeremylt 
588fe2413ffSjeremylt /**
589d1bcdac9Sjeremylt   @brief Get the number of components of a CeedQFunctionField
590d1bcdac9Sjeremylt 
591d1bcdac9Sjeremylt   @param qffield    CeedQFunctionField
5924d537eeaSYohann   @param[out] size  Variable to store the size of the field
593d1bcdac9Sjeremylt 
594d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
595d1bcdac9Sjeremylt 
596d1bcdac9Sjeremylt   @ref Advanced
597d1bcdac9Sjeremylt **/
598d1bcdac9Sjeremylt 
5994d537eeaSYohann int CeedQFunctionFieldGetSize(CeedQFunctionField qffield, CeedInt *size) {
6004d537eeaSYohann   *size = qffield->size;
601d1bcdac9Sjeremylt   return 0;
602d1bcdac9Sjeremylt }
603d1bcdac9Sjeremylt 
604d1bcdac9Sjeremylt /**
605d1bcdac9Sjeremylt   @brief Get the CeedEvalMode of a CeedQFunctionField
606d1bcdac9Sjeremylt 
607d1bcdac9Sjeremylt   @param qffield         CeedQFunctionField
608288c0443SJeremy L Thompson   @param[out] emode      Variable to store the field evaluation mode
609d1bcdac9Sjeremylt 
610d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
611d1bcdac9Sjeremylt 
612d1bcdac9Sjeremylt   @ref Advanced
613d1bcdac9Sjeremylt **/
614d1bcdac9Sjeremylt 
615d1bcdac9Sjeremylt int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qffield,
616d1bcdac9Sjeremylt                                   CeedEvalMode *emode) {
617fe2413ffSjeremylt   *emode = qffield->emode;
618d1bcdac9Sjeremylt   return 0;
619d1bcdac9Sjeremylt }
620d1bcdac9Sjeremylt 
621d1bcdac9Sjeremylt /**
622b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
623b11c1e72Sjeremylt 
624b11c1e72Sjeremylt   @param qf CeedQFunction to destroy
625b11c1e72Sjeremylt 
626b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
627dfdf5a53Sjeremylt 
628dfdf5a53Sjeremylt   @ref Basic
629b11c1e72Sjeremylt **/
630d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
631d7b241e6Sjeremylt   int ierr;
632d7b241e6Sjeremylt 
6331d102b48SJeremy L Thompson   if (!*qf || --(*qf)->refcount > 0)
6341d102b48SJeremy L Thompson     return 0;
635fe2413ffSjeremylt   // Backend destroy
636d7b241e6Sjeremylt   if ((*qf)->Destroy) {
637d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
638d7b241e6Sjeremylt   }
639fe2413ffSjeremylt   // Free fields
640fe2413ffSjeremylt   for (int i=0; i<(*qf)->numinputfields; i++) {
641fe2413ffSjeremylt     ierr = CeedFree(&(*(*qf)->inputfields[i]).fieldname); CeedChk(ierr);
642fe2413ffSjeremylt     ierr = CeedFree(&(*qf)->inputfields[i]); CeedChk(ierr);
643fe2413ffSjeremylt   }
644fe2413ffSjeremylt   for (int i=0; i<(*qf)->numoutputfields; i++) {
645fe2413ffSjeremylt     ierr = CeedFree(&(*(*qf)->outputfields[i]).fieldname); CeedChk(ierr);
646fe2413ffSjeremylt     ierr = CeedFree(&(*qf)->outputfields[i]); CeedChk(ierr);
647fe2413ffSjeremylt   }
648fe2413ffSjeremylt   ierr = CeedFree(&(*qf)->inputfields); CeedChk(ierr);
649fe2413ffSjeremylt   ierr = CeedFree(&(*qf)->outputfields); CeedChk(ierr);
6500219ea01SJeremy L Thompson   // Free ctx if identity
6510219ea01SJeremy L Thompson   if ((*qf)->identity) {
6520219ea01SJeremy L Thompson     ierr = CeedFree(&(*qf)->ctx); CeedChk(ierr);
6530219ea01SJeremy L Thompson   }
654fe2413ffSjeremylt 
655288c0443SJeremy L Thompson   ierr = CeedFree(&(*qf)->sourcepath); CeedChk(ierr);
656d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
657d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
658d7b241e6Sjeremylt   return 0;
659d7b241e6Sjeremylt }
660d7b241e6Sjeremylt 
661d7b241e6Sjeremylt /// @}
662