xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision 1176cc3a9721a4533d0e68f729d54c54717d4a1d)
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.
47*1176cc3aSJeremy L Thompson   @param source     Absolute path to source of QFunction,
48*1176cc3aSJeremy 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;
82d7b241e6Sjeremylt   (*qf)->function = f;
83288c0443SJeremy L Thompson   ierr = CeedCalloc(strlen(source)+1, &source_copy); CeedChk(ierr);
84288c0443SJeremy L Thompson   strncpy(source_copy, source, strlen(source));
85288c0443SJeremy L Thompson   (*qf)->sourcepath = source_copy;
86fe2413ffSjeremylt   ierr = CeedCalloc(16, &(*qf)->inputfields); CeedChk(ierr);
87fe2413ffSjeremylt   ierr = CeedCalloc(16, &(*qf)->outputfields); CeedChk(ierr);
88d7b241e6Sjeremylt   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
89d7b241e6Sjeremylt   return 0;
90d7b241e6Sjeremylt }
91d7b241e6Sjeremylt 
92b11c1e72Sjeremylt /**
93288c0443SJeremy L Thompson   @brief Register a gallery QFunction
94288c0443SJeremy L Thompson 
95288c0443SJeremy L Thompson   @param name    Name for this backend to respond to
96*1176cc3aSJeremy L Thompson   @param source  Absolute path to source of QFunction,
97*1176cc3aSJeremy L Thompson                    "\path\CEED_DIR\gallery\folder\file.h:function_name"
98288c0443SJeremy L Thompson   @param vlength Vector length.  Caller must ensure that number of quadrature
99288c0443SJeremy L Thompson                    points is a multiple of vlength.
100288c0443SJeremy L Thompson   @param f       Function pointer to evaluate action at quadrature points.
101288c0443SJeremy L Thompson                    See \ref CeedQFunctionUser.
102288c0443SJeremy L Thompson   @param init    Initialization function called by CeedQFunctionInit() when the
103288c0443SJeremy L Thompson                    QFunction is selected.
104288c0443SJeremy L Thompson 
105288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
106288c0443SJeremy L Thompson 
107288c0443SJeremy L Thompson   @ref Advanced
108288c0443SJeremy L Thompson **/
109288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source,
110288c0443SJeremy L Thompson                           CeedInt vlength, CeedQFunctionUser f,
111288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
112288c0443SJeremy L Thompson   if (num_qfunctions >= sizeof(qfunctions) / sizeof(qfunctions[0])) {
113288c0443SJeremy L Thompson     return CeedError(NULL, 1, "Too many gallery QFunctions");
114288c0443SJeremy L Thompson   }
115288c0443SJeremy L Thompson   strncpy(qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
116288c0443SJeremy L Thompson   qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
117288c0443SJeremy L Thompson   strncpy(qfunctions[num_qfunctions].source, source, CEED_MAX_RESOURCE_LEN);
118288c0443SJeremy L Thompson   qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
119288c0443SJeremy L Thompson   qfunctions[num_qfunctions].vlength = vlength;
120288c0443SJeremy L Thompson   qfunctions[num_qfunctions].f = f;
121288c0443SJeremy L Thompson   qfunctions[num_qfunctions].init = init;
122288c0443SJeremy L Thompson   num_qfunctions++;
123288c0443SJeremy L Thompson   return 0;
124288c0443SJeremy L Thompson }
125288c0443SJeremy L Thompson 
126288c0443SJeremy L Thompson /**
127288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
128288c0443SJeremy L Thompson 
129288c0443SJeremy L Thompson   @param ceed       A Ceed object where the CeedQFunction will be created
130288c0443SJeremy L Thompson   @param name       Name of QFunction to use from gallery
131288c0443SJeremy L Thompson   @param[out] qf    Address of the variable where the newly created
132288c0443SJeremy L Thompson                       CeedQFunction will be stored
133288c0443SJeremy L Thompson 
134288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
135288c0443SJeremy L Thompson 
136288c0443SJeremy L Thompson   @ref Basic
137288c0443SJeremy L Thompson **/
138288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
139288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
140288c0443SJeremy L Thompson   int ierr;
141288c0443SJeremy L Thompson   size_t matchlen = 0, matchidx = UINT_MAX;
142288c0443SJeremy L Thompson 
143288c0443SJeremy L Thompson   // Find matching backend
144288c0443SJeremy L Thompson   if (!name) return CeedError(NULL, 1, "No QFunction name provided");
145288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
146288c0443SJeremy L Thompson     size_t n;
147288c0443SJeremy L Thompson     const char *currname = qfunctions[i].name;
148288c0443SJeremy L Thompson     for (n = 0; currname[n] && currname[n] == name[n]; n++) {}
149288c0443SJeremy L Thompson     if (n > matchlen) {
150288c0443SJeremy L Thompson       matchlen = n;
151288c0443SJeremy L Thompson       matchidx = i;
152288c0443SJeremy L Thompson     }
153288c0443SJeremy L Thompson   }
154288c0443SJeremy L Thompson   if (!matchlen) return CeedError(NULL, 1, "No suitable gallery QFunction");
155288c0443SJeremy L Thompson 
156288c0443SJeremy L Thompson   // Create QFunction
157288c0443SJeremy L Thompson   ierr = CeedQFunctionCreateInterior(ceed, qfunctions[matchidx].vlength,
158288c0443SJeremy L Thompson                                      qfunctions[matchidx].f,
159288c0443SJeremy L Thompson                                      qfunctions[matchidx].source, qf);
160288c0443SJeremy L Thompson   CeedChk(ierr);
161288c0443SJeremy L Thompson 
162288c0443SJeremy L Thompson   // QFunction specific setup
163288c0443SJeremy L Thompson   ierr = qfunctions[matchidx].init(ceed, name, *qf); CeedChk(ierr);
164288c0443SJeremy L Thompson 
165288c0443SJeremy L Thompson   return 0;
166288c0443SJeremy L Thompson }
167288c0443SJeremy L Thompson 
168288c0443SJeremy L Thompson /**
169a0a97fcfSJed Brown   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
170b11c1e72Sjeremylt 
171b11c1e72Sjeremylt   @param f          CeedQFunctionField
172b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
1734d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
1744d537eeaSYohann                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
175b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
176b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
177b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
178b11c1e72Sjeremylt 
179b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
180dfdf5a53Sjeremylt 
181dfdf5a53Sjeremylt   @ref Developer
182b11c1e72Sjeremylt **/
183fe2413ffSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *fieldname,
1844d537eeaSYohann                                  CeedInt size, CeedEvalMode emode) {
185d7b241e6Sjeremylt   size_t len = strlen(fieldname);
186d7b241e6Sjeremylt   char *tmp;
187fe2413ffSjeremylt   int ierr;
188fe2413ffSjeremylt   ierr = CeedCalloc(1,f); CeedChk(ierr);
189fe2413ffSjeremylt 
190fe2413ffSjeremylt   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
191d7b241e6Sjeremylt   memcpy(tmp, fieldname, len+1);
192fe2413ffSjeremylt   (*f)->fieldname = tmp;
1934d537eeaSYohann   (*f)->size = size;
194fe2413ffSjeremylt   (*f)->emode = emode;
195d7b241e6Sjeremylt   return 0;
196d7b241e6Sjeremylt }
197d7b241e6Sjeremylt 
198b11c1e72Sjeremylt /**
199a0a97fcfSJed Brown   @brief Add a CeedQFunction input
200b11c1e72Sjeremylt 
201b11c1e72Sjeremylt   @param qf         CeedQFunction
202b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
2034d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
2044d537eeaSYohann                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
205b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
206b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
207b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
208b11c1e72Sjeremylt 
209b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
210dfdf5a53Sjeremylt 
211dfdf5a53Sjeremylt   @ref Basic
212b11c1e72Sjeremylt **/
213d7b241e6Sjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname,
2144d537eeaSYohann                           CeedInt size, CeedEvalMode emode) {
215fe2413ffSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->inputfields[qf->numinputfields],
2164d537eeaSYohann                                    fieldname, size, emode);
217fe2413ffSjeremylt   CeedChk(ierr);
218fe2413ffSjeremylt   qf->numinputfields++;
219d7b241e6Sjeremylt   return 0;
220d7b241e6Sjeremylt }
221d7b241e6Sjeremylt 
222b11c1e72Sjeremylt /**
223a0a97fcfSJed Brown   @brief Add a CeedQFunction output
224b11c1e72Sjeremylt 
225b11c1e72Sjeremylt   @param qf         CeedQFunction
226b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
2274d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
2284d537eeaSYohann                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
229b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
230b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
231b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
232b11c1e72Sjeremylt 
233b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
234dfdf5a53Sjeremylt 
235dfdf5a53Sjeremylt   @ref Basic
236b11c1e72Sjeremylt **/
237d7b241e6Sjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
2384d537eeaSYohann                            CeedInt size, CeedEvalMode emode) {
239d7b241e6Sjeremylt   if (emode == CEED_EVAL_WEIGHT)
240d7b241e6Sjeremylt     return CeedError(qf->ceed, 1,
2418c91a0c9SJeremy L Thompson                      "Cannot create QFunction output with CEED_EVAL_WEIGHT");
242fe2413ffSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->outputfields[qf->numoutputfields],
2434d537eeaSYohann                                    fieldname, size, emode);
244fe2413ffSjeremylt   CeedChk(ierr);
245fe2413ffSjeremylt   qf->numoutputfields++;
246d7b241e6Sjeremylt   return 0;
247d7b241e6Sjeremylt }
248d7b241e6Sjeremylt 
249dfdf5a53Sjeremylt /**
2504ce2993fSjeremylt   @brief Get the Ceed associated with a CeedQFunction
2514ce2993fSjeremylt 
2524ce2993fSjeremylt   @param qf              CeedQFunction
2534ce2993fSjeremylt   @param[out] ceed       Variable to store Ceed
2544ce2993fSjeremylt 
2554ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
2564ce2993fSjeremylt 
25723617272Sjeremylt   @ref Advanced
2584ce2993fSjeremylt **/
2594ce2993fSjeremylt 
2604ce2993fSjeremylt int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
2614ce2993fSjeremylt   *ceed = qf->ceed;
2624ce2993fSjeremylt   return 0;
2634ce2993fSjeremylt }
2644ce2993fSjeremylt 
2654ce2993fSjeremylt /**
2664ce2993fSjeremylt   @brief Get the vector length of a CeedQFunction
2674ce2993fSjeremylt 
2684ce2993fSjeremylt   @param qf            CeedQFunction
269288c0443SJeremy L Thompson   @param[out] vlength  Variable to store vector length
2704ce2993fSjeremylt 
2714ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
2724ce2993fSjeremylt 
27323617272Sjeremylt   @ref Advanced
2744ce2993fSjeremylt **/
2754ce2993fSjeremylt 
2764ce2993fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength) {
2774ce2993fSjeremylt   *vlength = qf->vlength;
2784ce2993fSjeremylt   return 0;
2794ce2993fSjeremylt }
2804ce2993fSjeremylt 
2814ce2993fSjeremylt /**
282dfdf5a53Sjeremylt   @brief Get the number of inputs and outputs to a CeedQFunction
283dfdf5a53Sjeremylt 
284dfdf5a53Sjeremylt   @param qf              CeedQFunction
2854ce2993fSjeremylt   @param[out] numinput   Variable to store number of input fields
2864ce2993fSjeremylt   @param[out] numoutput  Variable to store number of output fields
287dfdf5a53Sjeremylt 
288dfdf5a53Sjeremylt   @return An error code: 0 - success, otherwise - failure
289dfdf5a53Sjeremylt 
29023617272Sjeremylt   @ref Advanced
291dfdf5a53Sjeremylt **/
292dfdf5a53Sjeremylt 
293d7b241e6Sjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput,
294d7b241e6Sjeremylt                             CeedInt *numoutput) {
2951a4ead9bSjeremylt   if (numinput) *numinput = qf->numinputfields;
2961a4ead9bSjeremylt   if (numoutput) *numoutput = qf->numoutputfields;
297d7b241e6Sjeremylt   return 0;
298d7b241e6Sjeremylt }
299d7b241e6Sjeremylt 
300d7b241e6Sjeremylt /**
301288c0443SJeremy L Thompson   @brief Get the source path string for a CeedQFunction
3024ce2993fSjeremylt 
3034ce2993fSjeremylt   @param qf              CeedQFunction
304288c0443SJeremy L Thompson   @param[out] source     Variable to store source path string
3054ce2993fSjeremylt 
3064ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3074ce2993fSjeremylt 
30823617272Sjeremylt   @ref Advanced
3094ce2993fSjeremylt **/
3104ce2993fSjeremylt 
311288c0443SJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char* *source) {
312288c0443SJeremy L Thompson   *source = (char *) qf->sourcepath;
3134ce2993fSjeremylt   return 0;
3144ce2993fSjeremylt }
3154ce2993fSjeremylt 
3164ce2993fSjeremylt /**
317fe2413ffSjeremylt   @brief Get the User Function for a CeedQFunction
318fe2413ffSjeremylt 
319fe2413ffSjeremylt   @param qf              CeedQFunction
320fe2413ffSjeremylt   @param[out] f          Variable to store user function
321fe2413ffSjeremylt 
322fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
323fe2413ffSjeremylt 
324fe2413ffSjeremylt   @ref Advanced
325fe2413ffSjeremylt **/
326fe2413ffSjeremylt 
32728d161eeSjeremylt int CeedQFunctionGetUserFunction(CeedQFunction qf, int (**f)()) {
32828d161eeSjeremylt   *f = (int (*)())qf->function;
329fe2413ffSjeremylt   return 0;
330fe2413ffSjeremylt }
331fe2413ffSjeremylt 
332fe2413ffSjeremylt /**
3334ce2993fSjeremylt   @brief Get global context size for a CeedQFunction
3344ce2993fSjeremylt 
3354ce2993fSjeremylt   @param qf              CeedQFunction
3364ce2993fSjeremylt   @param[out] ctxsize    Variable to store size of context data values
3374ce2993fSjeremylt 
3384ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3394ce2993fSjeremylt 
34023617272Sjeremylt   @ref Advanced
3414ce2993fSjeremylt **/
3424ce2993fSjeremylt 
3434ce2993fSjeremylt int CeedQFunctionGetContextSize(CeedQFunction qf, size_t *ctxsize) {
344069aeabaSjeremylt   if (qf->fortranstatus) {
345069aeabaSjeremylt     fContext *fctx = qf->ctx;
346069aeabaSjeremylt     *ctxsize = fctx->innerctxsize;
347069aeabaSjeremylt   } else {
3484ce2993fSjeremylt     *ctxsize = qf->ctxsize;
349069aeabaSjeremylt   }
3504ce2993fSjeremylt   return 0;
3514ce2993fSjeremylt }
3524ce2993fSjeremylt 
3534ce2993fSjeremylt /**
3544ce2993fSjeremylt   @brief Get global context for a CeedQFunction
3554ce2993fSjeremylt 
3564ce2993fSjeremylt   @param qf              CeedQFunction
3574ce2993fSjeremylt   @param[out] ctx        Variable to store context data values
3584ce2993fSjeremylt 
3594ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3604ce2993fSjeremylt 
36123617272Sjeremylt   @ref Advanced
3624ce2993fSjeremylt **/
3634ce2993fSjeremylt 
3644ce2993fSjeremylt int CeedQFunctionGetContext(CeedQFunction qf, void* *ctx) {
3654ce2993fSjeremylt   *ctx = qf->ctx;
3664ce2993fSjeremylt   return 0;
3674ce2993fSjeremylt }
3684ce2993fSjeremylt 
3694ce2993fSjeremylt /**
370418fb8c2Sjeremylt   @brief Determine if Fortran interface was used
371418fb8c2Sjeremylt 
372418fb8c2Sjeremylt   @param qf                  CeedQFunction
373418fb8c2Sjeremylt   @param[out] fortranstatus  Variable to store Fortran status
374418fb8c2Sjeremylt 
375418fb8c2Sjeremylt   @return An error code: 0 - success, otherwise - failure
376418fb8c2Sjeremylt 
377418fb8c2Sjeremylt   @ref Advanced
378418fb8c2Sjeremylt **/
379418fb8c2Sjeremylt 
380418fb8c2Sjeremylt int CeedQFunctionGetFortranStatus(CeedQFunction qf, bool *fortranstatus) {
381418fb8c2Sjeremylt   *fortranstatus = qf->fortranstatus;
382418fb8c2Sjeremylt   return 0;
383418fb8c2Sjeremylt }
384418fb8c2Sjeremylt 
385418fb8c2Sjeremylt /**
38614922b2aSjeremylt   @brief Get true user context for a CeedQFunction
387069aeabaSjeremylt 
388069aeabaSjeremylt   @param qf              CeedQFunction
389069aeabaSjeremylt   @param[out] ctx        Variable to store context data values
390069aeabaSjeremylt 
391069aeabaSjeremylt   @return An error code: 0 - success, otherwise - failure
392069aeabaSjeremylt 
393069aeabaSjeremylt   @ref Advanced
394069aeabaSjeremylt **/
395069aeabaSjeremylt 
39614922b2aSjeremylt int CeedQFunctionGetInnerContext(CeedQFunction qf, void* *ctx) {
39714922b2aSjeremylt   if (qf->fortranstatus) {
398069aeabaSjeremylt     fContext *fctx = qf->ctx;
399069aeabaSjeremylt     *ctx = fctx->innerctx;
40014922b2aSjeremylt   } else {
40114922b2aSjeremylt     *ctx = qf->ctx;
40214922b2aSjeremylt   }
40314922b2aSjeremylt 
4049f0427d9SYohann 
405069aeabaSjeremylt   return 0;
406069aeabaSjeremylt }
407069aeabaSjeremylt 
408069aeabaSjeremylt /**
4094ce2993fSjeremylt   @brief Get backend data of a CeedQFunction
4104ce2993fSjeremylt 
4114ce2993fSjeremylt   @param qf              CeedQFunction
4124ce2993fSjeremylt   @param[out] data       Variable to store data
4134ce2993fSjeremylt 
4144ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
4154ce2993fSjeremylt 
41623617272Sjeremylt   @ref Advanced
4174ce2993fSjeremylt **/
4184ce2993fSjeremylt 
4194ce2993fSjeremylt int CeedQFunctionGetData(CeedQFunction qf, void* *data) {
4204ce2993fSjeremylt   *data = qf->data;
4214ce2993fSjeremylt   return 0;
4224ce2993fSjeremylt }
4234ce2993fSjeremylt 
4244ce2993fSjeremylt /**
425fe2413ffSjeremylt   @brief Set backend data of a CeedQFunction
426fe2413ffSjeremylt 
427fe2413ffSjeremylt   @param[out] qf         CeedQFunction
428fe2413ffSjeremylt   @param data            Data to set
429fe2413ffSjeremylt 
430fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
431fe2413ffSjeremylt 
432fe2413ffSjeremylt   @ref Advanced
433fe2413ffSjeremylt **/
434fe2413ffSjeremylt 
435fe2413ffSjeremylt int CeedQFunctionSetData(CeedQFunction qf, void* *data) {
436fe2413ffSjeremylt   qf->data = *data;
437fe2413ffSjeremylt   return 0;
438fe2413ffSjeremylt }
439fe2413ffSjeremylt 
440fe2413ffSjeremylt /**
4414ce2993fSjeremylt   @brief Set global context for a CeedQFunction
442b11c1e72Sjeremylt 
443b11c1e72Sjeremylt   @param qf       CeedQFunction
444b11c1e72Sjeremylt   @param ctx      Context data to set
445b11c1e72Sjeremylt   @param ctxsize  Size of context data values
446b11c1e72Sjeremylt 
447b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
448dfdf5a53Sjeremylt 
449dfdf5a53Sjeremylt   @ref Basic
450b11c1e72Sjeremylt **/
451d7b241e6Sjeremylt int CeedQFunctionSetContext(CeedQFunction qf, void *ctx, size_t ctxsize) {
452d7b241e6Sjeremylt   qf->ctx = ctx;
453d7b241e6Sjeremylt   qf->ctxsize = ctxsize;
454d7b241e6Sjeremylt   return 0;
455d7b241e6Sjeremylt }
456d7b241e6Sjeremylt 
457b11c1e72Sjeremylt /**
458b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
459b11c1e72Sjeremylt 
460b11c1e72Sjeremylt   @param qf      CeedQFunction
461b11c1e72Sjeremylt   @param Q       Number of quadrature points
462b11c1e72Sjeremylt   @param[in] u   Array of input data arrays
463b11c1e72Sjeremylt   @param[out] v  Array of output data arrays
464b11c1e72Sjeremylt 
465b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
466dfdf5a53Sjeremylt 
467dfdf5a53Sjeremylt   @ref Advanced
468b11c1e72Sjeremylt **/
469d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
470aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
471d7b241e6Sjeremylt   int ierr;
472d7b241e6Sjeremylt   if (!qf->Apply)
473d7b241e6Sjeremylt     return CeedError(qf->ceed, 1, "Backend does not support QFunctionApply");
474d7b241e6Sjeremylt   if (Q % qf->vlength)
475d7b241e6Sjeremylt     return CeedError(qf->ceed, 2,
476d7b241e6Sjeremylt                      "Number of quadrature points %d must be a multiple of %d",
477d7b241e6Sjeremylt                      Q, qf->vlength);
478d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
479d7b241e6Sjeremylt   return 0;
480d7b241e6Sjeremylt }
481d7b241e6Sjeremylt 
482b11c1e72Sjeremylt /**
483d1bcdac9Sjeremylt   @brief Get the CeedQFunctionFields of a CeedQFunction
484d1bcdac9Sjeremylt 
485d1bcdac9Sjeremylt   @param qf                 CeedQFunction
486d1bcdac9Sjeremylt   @param[out] inputfields   Variable to store inputfields
487d1bcdac9Sjeremylt   @param[out] outputfields  Variable to store outputfields
488d1bcdac9Sjeremylt 
489d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
490d1bcdac9Sjeremylt 
491d1bcdac9Sjeremylt   @ref Advanced
492d1bcdac9Sjeremylt **/
493d1bcdac9Sjeremylt 
494d1bcdac9Sjeremylt int CeedQFunctionGetFields(CeedQFunction qf,
495d1bcdac9Sjeremylt                            CeedQFunctionField* *inputfields,
496d1bcdac9Sjeremylt                            CeedQFunctionField* *outputfields) {
497d1bcdac9Sjeremylt   if (inputfields) *inputfields = qf->inputfields;
498d1bcdac9Sjeremylt   if (outputfields) *outputfields = qf->outputfields;
499d1bcdac9Sjeremylt   return 0;
500d1bcdac9Sjeremylt }
501d1bcdac9Sjeremylt 
502d1bcdac9Sjeremylt /**
503fe2413ffSjeremylt   @brief Get the name of a CeedQFunctionField
504fe2413ffSjeremylt 
505fe2413ffSjeremylt   @param qffield         CeedQFunctionField
506fe2413ffSjeremylt   @param[out] fieldname  Variable to store the field name
507fe2413ffSjeremylt 
508fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
509fe2413ffSjeremylt 
510fe2413ffSjeremylt   @ref Advanced
511fe2413ffSjeremylt **/
512fe2413ffSjeremylt 
513fe2413ffSjeremylt int CeedQFunctionFieldGetName(CeedQFunctionField qffield,
514fe2413ffSjeremylt                               char* *fieldname) {
515fe2413ffSjeremylt   *fieldname = (char *)qffield->fieldname;
516fe2413ffSjeremylt   return 0;
517fe2413ffSjeremylt }
518fe2413ffSjeremylt 
519fe2413ffSjeremylt /**
520d1bcdac9Sjeremylt   @brief Get the number of components of a CeedQFunctionField
521d1bcdac9Sjeremylt 
522d1bcdac9Sjeremylt   @param qffield    CeedQFunctionField
5234d537eeaSYohann   @param[out] size  Variable to store the size of the field
524d1bcdac9Sjeremylt 
525d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
526d1bcdac9Sjeremylt 
527d1bcdac9Sjeremylt   @ref Advanced
528d1bcdac9Sjeremylt **/
529d1bcdac9Sjeremylt 
5304d537eeaSYohann int CeedQFunctionFieldGetSize(CeedQFunctionField qffield, CeedInt *size) {
5314d537eeaSYohann   *size = qffield->size;
532d1bcdac9Sjeremylt   return 0;
533d1bcdac9Sjeremylt }
534d1bcdac9Sjeremylt 
535d1bcdac9Sjeremylt /**
536d1bcdac9Sjeremylt   @brief Get the CeedEvalMode of a CeedQFunctionField
537d1bcdac9Sjeremylt 
538d1bcdac9Sjeremylt   @param qffield         CeedQFunctionField
539288c0443SJeremy L Thompson   @param[out] emode      Variable to store the field evaluation mode
540d1bcdac9Sjeremylt 
541d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
542d1bcdac9Sjeremylt 
543d1bcdac9Sjeremylt   @ref Advanced
544d1bcdac9Sjeremylt **/
545d1bcdac9Sjeremylt 
546d1bcdac9Sjeremylt int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qffield,
547d1bcdac9Sjeremylt                                   CeedEvalMode *emode) {
548fe2413ffSjeremylt   *emode = qffield->emode;
549d1bcdac9Sjeremylt   return 0;
550d1bcdac9Sjeremylt }
551d1bcdac9Sjeremylt 
552d1bcdac9Sjeremylt /**
553b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
554b11c1e72Sjeremylt 
555b11c1e72Sjeremylt   @param qf CeedQFunction to destroy
556b11c1e72Sjeremylt 
557b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
558dfdf5a53Sjeremylt 
559dfdf5a53Sjeremylt   @ref Basic
560b11c1e72Sjeremylt **/
561d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
562d7b241e6Sjeremylt   int ierr;
563d7b241e6Sjeremylt 
564d7b241e6Sjeremylt   if (!*qf || --(*qf)->refcount > 0) return 0;
565fe2413ffSjeremylt   // Backend destroy
566d7b241e6Sjeremylt   if ((*qf)->Destroy) {
567d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
568d7b241e6Sjeremylt   }
569fe2413ffSjeremylt   // Free fields
570fe2413ffSjeremylt   for (int i=0; i<(*qf)->numinputfields; i++) {
571fe2413ffSjeremylt     ierr = CeedFree(&(*(*qf)->inputfields[i]).fieldname); CeedChk(ierr);
572fe2413ffSjeremylt     ierr = CeedFree(&(*qf)->inputfields[i]); CeedChk(ierr);
573fe2413ffSjeremylt   }
574fe2413ffSjeremylt   for (int i=0; i<(*qf)->numoutputfields; i++) {
575fe2413ffSjeremylt     ierr = CeedFree(&(*(*qf)->outputfields[i]).fieldname); CeedChk(ierr);
576fe2413ffSjeremylt     ierr = CeedFree(&(*qf)->outputfields[i]); CeedChk(ierr);
577fe2413ffSjeremylt   }
578fe2413ffSjeremylt   ierr = CeedFree(&(*qf)->inputfields); CeedChk(ierr);
579fe2413ffSjeremylt   ierr = CeedFree(&(*qf)->outputfields); CeedChk(ierr);
580fe2413ffSjeremylt 
581288c0443SJeremy L Thompson   ierr = CeedFree(&(*qf)->sourcepath); CeedChk(ierr);
582d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
583d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
584d7b241e6Sjeremylt   return 0;
585d7b241e6Sjeremylt }
586d7b241e6Sjeremylt 
587d7b241e6Sjeremylt /// @}
588