xref: /libCEED/interface/ceed-qfunction.c (revision cae8b89ab4384ccb591c7143fd0ca8a192ac9d03)
1 // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3 // reserved. See files LICENSE and NOTICE for details.
4 //
5 // This file is part of CEED, a collection of benchmarks, miniapps, software
6 // libraries and APIs for efficient high-order finite element and spectral
7 // element discretizations for exascale applications. For more information and
8 // source code availability see http://github.com/ceed.
9 //
10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11 // a collaborative effort of two U.S. Department of Energy organizations (Office
12 // of Science and the National Nuclear Security Administration) responsible for
13 // the planning and preparation of a capable exascale ecosystem, including
14 // software, applications, hardware, advanced system engineering and early
15 // testbed platforms, in support of the nation's exascale computing imperative.
16 
17 #include <ceed-impl.h>
18 #include <ceed-backend.h>
19 #include <string.h>
20 #include <limits.h>
21 
22 /// @cond DOXYGEN_SKIP
23 static struct CeedQFunction_private ceed_qfunction_none;
24 /// @endcond
25 
26 /// @cond DOXYGEN_SKIP
27 static struct {
28   char name[CEED_MAX_RESOURCE_LEN];
29   char source[CEED_MAX_RESOURCE_LEN];
30   CeedInt vlength;
31   CeedQFunctionUser f;
32   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
33 } qfunctions[1024];
34 static size_t num_qfunctions;
35 /// @endcond
36 
37 /// @file
38 /// Implementation of public CeedQFunction interfaces
39 ///
40 /// @addtogroup CeedQFunction
41 /// @{
42 
43 /**
44   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
45 
46   @param ceed       A Ceed object where the CeedQFunction will be created
47   @param vlength    Vector length.  Caller must ensure that number of quadrature
48                     points is a multiple of vlength.
49   @param f          Function pointer to evaluate action at quadrature points.
50                     See \ref CeedQFunctionUser.
51   @param source     Absolute path to source of QFunction,
52                       "\abs_path\file.h:function_name"
53   @param[out] qf    Address of the variable where the newly created
54                       CeedQFunction will be stored
55 
56   @return An error code: 0 - success, otherwise - failure
57 
58   See \ref CeedQFunctionUser for details on the call-back function @a f's
59     arguments.
60 
61   @ref Basic
62 **/
63 int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength, CeedQFunctionUser f,
64                                 const char *source, CeedQFunction *qf) {
65   int ierr;
66   char *source_copy;
67 
68   if (!ceed->QFunctionCreate) {
69     Ceed delegate;
70     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
71 
72     if (!delegate)
73       // LCOV_EXCL_START
74       return CeedError(ceed, 1, "Backend does not support QFunctionCreate");
75     // LCOV_EXCL_STOP
76 
77     ierr = CeedQFunctionCreateInterior(delegate, vlength, f, source, qf);
78     CeedChk(ierr);
79     return 0;
80   }
81 
82   ierr = CeedCalloc(1,qf); CeedChk(ierr);
83   (*qf)->ceed = ceed;
84   ceed->refcount++;
85   (*qf)->refcount = 1;
86   (*qf)->vlength = vlength;
87   (*qf)->identity = 0;
88   (*qf)->function = f;
89   size_t slen = strlen(source) + 1;
90   ierr = CeedMalloc(slen, &source_copy); CeedChk(ierr);
91   memcpy(source_copy, source, slen);
92   (*qf)->sourcepath = source_copy;
93   ierr = CeedCalloc(16, &(*qf)->inputfields); CeedChk(ierr);
94   ierr = CeedCalloc(16, &(*qf)->outputfields); CeedChk(ierr);
95   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
96   return 0;
97 }
98 
99 /**
100   @brief Register a gallery QFunction
101 
102   @param name    Name for this backend to respond to
103   @param source  Absolute path to source of QFunction,
104                    "\path\CEED_DIR\gallery\folder\file.h:function_name"
105   @param vlength Vector length.  Caller must ensure that number of quadrature
106                    points is a multiple of vlength.
107   @param f       Function pointer to evaluate action at quadrature points.
108                    See \ref CeedQFunctionUser.
109   @param init    Initialization function called by CeedQFunctionInit() when the
110                    QFunction is selected.
111 
112   @return An error code: 0 - success, otherwise - failure
113 
114   @ref Advanced
115 **/
116 int CeedQFunctionRegister(const char *name, const char *source,
117                           CeedInt vlength, CeedQFunctionUser f,
118                           int (*init)(Ceed, const char *, CeedQFunction)) {
119   if (num_qfunctions >= sizeof(qfunctions) / sizeof(qfunctions[0]))
120     // LCOV_EXCL_START
121     return CeedError(NULL, 1, "Too many gallery QFunctions");
122   // LCOV_EXCL_STOP
123 
124   strncpy(qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
125   qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
126   strncpy(qfunctions[num_qfunctions].source, source, CEED_MAX_RESOURCE_LEN);
127   qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
128   qfunctions[num_qfunctions].vlength = vlength;
129   qfunctions[num_qfunctions].f = f;
130   qfunctions[num_qfunctions].init = init;
131   num_qfunctions++;
132   return 0;
133 }
134 
135 /**
136   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
137 
138   @param ceed       A Ceed object where the CeedQFunction will be created
139   @param name       Name of QFunction to use from gallery
140   @param[out] qf    Address of the variable where the newly created
141                       CeedQFunction will be stored
142 
143   @return An error code: 0 - success, otherwise - failure
144 
145   @ref Basic
146 **/
147 int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
148                                       CeedQFunction *qf) {
149   int ierr;
150   size_t matchlen = 0, matchidx = UINT_MAX;
151   char *name_copy;
152 
153   // Find matching backend
154   if (!name) return CeedError(NULL, 1, "No QFunction name provided");
155   for (size_t i=0; i<num_qfunctions; i++) {
156     size_t n;
157     const char *currname = qfunctions[i].name;
158     for (n = 0; currname[n] && currname[n] == name[n]; n++) {}
159     if (n > matchlen) {
160       matchlen = n;
161       matchidx = i;
162     }
163   }
164   if (!matchlen)
165     // LCOV_EXCL_START
166     return CeedError(NULL, 1, "No suitable gallery QFunction");
167   // LCOV_EXCL_STOP
168 
169   // Create QFunction
170   ierr = CeedQFunctionCreateInterior(ceed, qfunctions[matchidx].vlength,
171                                      qfunctions[matchidx].f,
172                                      qfunctions[matchidx].source, qf);
173   CeedChk(ierr);
174 
175   // QFunction specific setup
176   ierr = qfunctions[matchidx].init(ceed, name, *qf); CeedChk(ierr);
177 
178   // Copy name
179   size_t slen = strlen(name) + 1;
180   ierr = CeedMalloc(slen, &name_copy); CeedChk(ierr);
181   memcpy(name_copy, name, slen);
182   (*qf)->qfname = name_copy;
183 
184   return 0;
185 }
186 
187 /**
188   @brief Create an identity CeedQFunction. Inputs are written into outputs in
189            the order given. This is useful for CeedOperators that can be
190            represented with only the action of a CeedRestriction and CeedBasis,
191            such as restriction and prolongation operators for p-multigrid.
192            Backends may optimize CeedOperators with this CeedQFunction to avoid
193            the copy of input data to output fields by using the same memory
194            location for both.
195 
196   @param ceed        A Ceed object where the CeedQFunction will be created
197   @param[in] size    Size of the qfunction fields
198   @param[in] inmode  CeedEvalMode for input to CeedQFunction
199   @param[in] outmode CeedEvalMode for output to CeedQFunction
200   @param[out] qf     Address of the variable where the newly created
201                        CeedQFunction will be stored
202 
203   @return An error code: 0 - success, otherwise - failure
204 
205   @ref Basic
206 **/
207 int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode inmode,
208                                 CeedEvalMode outmode, CeedQFunction *qf) {
209   int ierr;
210 
211   if (inmode == CEED_EVAL_NONE && outmode == CEED_EVAL_NONE)
212     // LCOV_EXCL_START
213     return CeedError(ceed, 1, "CEED_EVAL_NONE for a both the input and "
214                      "output does not make sense with an identity QFunction");
215   // LCOV_EXCL_STOP
216 
217   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
218   ierr = CeedQFunctionAddInput(*qf, "input", 1, inmode); CeedChk(ierr);
219   ierr = CeedQFunctionAddOutput(*qf, "output", 1, outmode); CeedChk(ierr);
220 
221   (*qf)->identity = 1;
222   if (size > 1) {
223     CeedInt *ctx;
224     ierr = CeedCalloc(1, &ctx); CeedChk(ierr);
225     ctx[0] = size;
226     ierr = CeedQFunctionSetContext(*qf, ctx, sizeof(ctx)); CeedChk(ierr);
227     (*qf)->inputfields[0]->size = size;
228     (*qf)->outputfields[0]->size = size;
229   }
230 
231   return 0;
232 }
233 
234 /**
235   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
236 
237   @param f          CeedQFunctionField
238   @param fieldname  Name of QFunction field
239   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
240                       1 for CEED_EVAL_NONE, CEED_EVAL_INTERP, and CEED_EVAL_WEIGHT)
241   @param emode      \ref CEED_EVAL_NONE to use values directly,
242                       \ref CEED_EVAL_INTERP to use interpolated values,
243                       \ref CEED_EVAL_GRAD to use gradients,
244                       \ref CEED_EVAL_WEIGHT to use quadrature weights.
245 
246   @return An error code: 0 - success, otherwise - failure
247 
248   @ref Developer
249 **/
250 static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *fieldname,
251                                  CeedInt size, CeedEvalMode emode) {
252   size_t len = strlen(fieldname);
253   char *tmp;
254   int ierr;
255   ierr = CeedCalloc(1,f); CeedChk(ierr);
256 
257   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
258   memcpy(tmp, fieldname, len+1);
259   (*f)->fieldname = tmp;
260   (*f)->size = size;
261   (*f)->emode = emode;
262   return 0;
263 }
264 
265 /**
266   @brief Add a CeedQFunction input
267 
268   @param qf         CeedQFunction
269   @param fieldname  Name of QFunction field
270   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
271                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
272   @param emode      \ref CEED_EVAL_NONE to use values directly,
273                       \ref CEED_EVAL_INTERP to use interpolated values,
274                       \ref CEED_EVAL_GRAD to use gradients.
275 
276   @return An error code: 0 - success, otherwise - failure
277 
278   @ref Basic
279 **/
280 int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname, CeedInt size,
281                           CeedEvalMode emode) {
282   int ierr = CeedQFunctionFieldSet(&qf->inputfields[qf->numinputfields],
283                                    fieldname, size, emode);
284   CeedChk(ierr);
285   qf->numinputfields++;
286   return 0;
287 }
288 
289 /**
290   @brief Add a CeedQFunction output
291 
292   @param qf         CeedQFunction
293   @param fieldname  Name of QFunction field
294   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
295                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
296   @param emode      \ref CEED_EVAL_NONE to use values directly,
297                       \ref CEED_EVAL_INTERP to use interpolated values,
298                       \ref CEED_EVAL_GRAD to use gradients.
299 
300   @return An error code: 0 - success, otherwise - failure
301 
302   @ref Basic
303 **/
304 int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
305                            CeedInt size, CeedEvalMode emode) {
306   if (emode == CEED_EVAL_WEIGHT)
307     // LCOV_EXCL_START
308     return CeedError(qf->ceed, 1, "Cannot create QFunction output with "
309                      "CEED_EVAL_WEIGHT");
310   // LCOV_EXCL_STOP
311   int ierr = CeedQFunctionFieldSet(&qf->outputfields[qf->numoutputfields],
312                                    fieldname, size, emode);
313   CeedChk(ierr);
314   qf->numoutputfields++;
315   return 0;
316 }
317 
318 /**
319   @brief Get the Ceed associated with a CeedQFunction
320 
321   @param qf              CeedQFunction
322   @param[out] ceed       Variable to store Ceed
323 
324   @return An error code: 0 - success, otherwise - failure
325 
326   @ref Advanced
327 **/
328 
329 int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
330   *ceed = qf->ceed;
331   return 0;
332 }
333 
334 /**
335   @brief Get the vector length of a CeedQFunction
336 
337   @param qf            CeedQFunction
338   @param[out] vlength  Variable to store vector length
339 
340   @return An error code: 0 - success, otherwise - failure
341 
342   @ref Advanced
343 **/
344 
345 int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength) {
346   *vlength = qf->vlength;
347   return 0;
348 }
349 
350 /**
351   @brief Get the number of inputs and outputs to a CeedQFunction
352 
353   @param qf              CeedQFunction
354   @param[out] numinput   Variable to store number of input fields
355   @param[out] numoutput  Variable to store number of output fields
356 
357   @return An error code: 0 - success, otherwise - failure
358 
359   @ref Advanced
360 **/
361 
362 int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput,
363                             CeedInt *numoutput) {
364   if (numinput) *numinput = qf->numinputfields;
365   if (numoutput) *numoutput = qf->numoutputfields;
366   return 0;
367 }
368 
369 /**
370   @brief Get the source path string for a CeedQFunction
371 
372   @param qf              CeedQFunction
373   @param[out] source     Variable to store source path string
374 
375   @return An error code: 0 - success, otherwise - failure
376 
377   @ref Advanced
378 **/
379 
380 int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source) {
381   *source = (char *) qf->sourcepath;
382   return 0;
383 }
384 
385 /**
386   @brief Get the User Function for a CeedQFunction
387 
388   @param qf              CeedQFunction
389   @param[out] f          Variable to store user function
390 
391   @return An error code: 0 - success, otherwise - failure
392 
393   @ref Advanced
394 **/
395 
396 int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
397   *f = qf->function;
398   return 0;
399 }
400 
401 /**
402   @brief Get global context size for a CeedQFunction
403 
404   @param qf              CeedQFunction
405   @param[out] ctxsize    Variable to store size of context data values
406 
407   @return An error code: 0 - success, otherwise - failure
408 
409   @ref Advanced
410 **/
411 
412 int CeedQFunctionGetContextSize(CeedQFunction qf, size_t *ctxsize) {
413   if (qf->fortranstatus) {
414     fContext *fctx = qf->ctx;
415     *ctxsize = fctx->innerctxsize;
416   } else {
417     *ctxsize = qf->ctxsize;
418   }
419   return 0;
420 }
421 
422 /**
423   @brief Get global context for a CeedQFunction
424 
425   @param qf              CeedQFunction
426   @param[out] ctx        Variable to store context data values
427 
428   @return An error code: 0 - success, otherwise - failure
429 
430   @ref Advanced
431 **/
432 
433 int CeedQFunctionGetContext(CeedQFunction qf, void **ctx) {
434   *ctx = qf->ctx;
435   return 0;
436 }
437 
438 /**
439   @brief Determine if Fortran interface was used
440 
441   @param qf                  CeedQFunction
442   @param[out] fortranstatus  Variable to store Fortran status
443 
444   @return An error code: 0 - success, otherwise - failure
445 
446   @ref Advanced
447 **/
448 
449 int CeedQFunctionGetFortranStatus(CeedQFunction qf, bool *fortranstatus) {
450   *fortranstatus = qf->fortranstatus;
451   return 0;
452 }
453 
454 /**
455   @brief Determine if QFunction is identity
456 
457   @param qf               CeedQFunction
458   @param[out] identity    Variable to store identity status
459 
460   @return An error code: 0 - success, otherwise - failure
461 
462   @ref Advanced
463 **/
464 
465 int CeedQFunctionGetIdentityStatus(CeedQFunction qf, bool *identity) {
466   *identity = qf->identity;
467   return 0;
468 }
469 
470 /**
471   @brief Get true user context for a CeedQFunction
472 
473   @param qf              CeedQFunction
474   @param[out] ctx        Variable to store context data values
475 
476   @return An error code: 0 - success, otherwise - failure
477 
478   @ref Advanced
479 **/
480 
481 int CeedQFunctionGetInnerContext(CeedQFunction qf, void **ctx) {
482   if (qf->fortranstatus) {
483     fContext *fctx = qf->ctx;
484     *ctx = fctx->innerctx;
485   } else {
486     *ctx = qf->ctx;
487   }
488 
489 
490   return 0;
491 }
492 
493 /**
494   @brief Get backend data of a CeedQFunction
495 
496   @param qf              CeedQFunction
497   @param[out] data       Variable to store data
498 
499   @return An error code: 0 - success, otherwise - failure
500 
501   @ref Advanced
502 **/
503 
504 int CeedQFunctionGetData(CeedQFunction qf, void **data) {
505   *data = qf->data;
506   return 0;
507 }
508 
509 /**
510   @brief Set backend data of a CeedQFunction
511 
512   @param[out] qf         CeedQFunction
513   @param data            Data to set
514 
515   @return An error code: 0 - success, otherwise - failure
516 
517   @ref Advanced
518 **/
519 
520 int CeedQFunctionSetData(CeedQFunction qf, void **data) {
521   qf->data = *data;
522   return 0;
523 }
524 
525 /**
526   @brief Set global context for a CeedQFunction
527 
528   @param qf       CeedQFunction
529   @param ctx      Context data to set
530   @param ctxsize  Size of context data values
531 
532   @return An error code: 0 - success, otherwise - failure
533 
534   @ref Basic
535 **/
536 int CeedQFunctionSetContext(CeedQFunction qf, void *ctx, size_t ctxsize) {
537   qf->ctx = ctx;
538   qf->ctxsize = ctxsize;
539   return 0;
540 }
541 
542 /**
543   @brief View a field of a CeedQFunction
544 
545   @param[in] field       QFunction field to view
546   @param[in] fieldnumber Number of field being viewed
547   @param[in] stream      Stream to view to, e.g., stdout
548 
549   @return An error code: 0 - success, otherwise - failure
550 
551   @ref Utility
552 **/
553 static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt fieldnumber,
554                                   bool in, FILE *stream) {
555   const char *inout = in ? "Input" : "Output";
556   fprintf(stream, "    %s Field [%d]:\n"
557           "      Name: \"%s\"\n"
558           "      Size: %d\n"
559           "      EvalMode: \"%s\"\n",
560           inout, fieldnumber, field->fieldname, field->size,
561           CeedEvalModes[field->emode]);
562 
563   return 0;
564 }
565 
566 /**
567   @brief View a CeedQFunction
568 
569   @param[in] qf     CeedQFunction to view
570   @param[in] stream Stream to write; typically stdout/stderr or a file
571 
572   @return Error code: 0 - success, otherwise - failure
573 
574   @ref Utility
575 **/
576 int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
577   int ierr;
578 
579   fprintf(stream, "%sCeedQFunction %s\n",
580           qf->qfname ? "Gallery " : "User ", qf->qfname ? qf->qfname : "");
581 
582   fprintf(stream, "  %d Input Field%s:\n", qf->numinputfields,
583           qf->numinputfields>1 ? "s" : "");
584   for (CeedInt i=0; i<qf->numinputfields; i++) {
585     ierr = CeedQFunctionFieldView(qf->inputfields[i], i, 1, stream);
586     CeedChk(ierr);
587   }
588 
589   fprintf(stream, "  %d Output Field%s:\n", qf->numoutputfields,
590           qf->numoutputfields>1 ? "s" : "");
591   for (CeedInt i=0; i<qf->numoutputfields; i++) {
592     ierr = CeedQFunctionFieldView(qf->outputfields[i], i, 0, stream);
593     CeedChk(ierr);
594   }
595   return 0;
596 }
597 
598 /**
599   @brief Apply the action of a CeedQFunction
600 
601   @param qf      CeedQFunction
602   @param Q       Number of quadrature points
603   @param[in] u   Array of input data arrays
604   @param[out] v  Array of output data arrays
605 
606   @return An error code: 0 - success, otherwise - failure
607 
608   @ref Advanced
609 **/
610 int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
611                        CeedVector *u, CeedVector *v) {
612   int ierr;
613   if (!qf->Apply)
614     // LCOV_EXCL_START
615     return CeedError(qf->ceed, 1, "Backend does not support QFunctionApply");
616   // LCOV_EXCL_STOP
617   if (Q % qf->vlength)
618     // LCOV_EXCL_START
619     return CeedError(qf->ceed, 2, "Number of quadrature points %d must be a "
620                      "multiple of %d", Q, qf->vlength);
621   // LCOV_EXCL_STOP
622   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
623   return 0;
624 }
625 
626 /**
627   @brief Get the CeedQFunctionFields of a CeedQFunction
628 
629   @param qf                 CeedQFunction
630   @param[out] inputfields   Variable to store inputfields
631   @param[out] outputfields  Variable to store outputfields
632 
633   @return An error code: 0 - success, otherwise - failure
634 
635   @ref Advanced
636 **/
637 
638 int CeedQFunctionGetFields(CeedQFunction qf, CeedQFunctionField **inputfields,
639                            CeedQFunctionField **outputfields) {
640   if (inputfields)
641     *inputfields = qf->inputfields;
642   if (outputfields)
643     *outputfields = qf->outputfields;
644   return 0;
645 }
646 
647 /**
648   @brief Get the name of a CeedQFunctionField
649 
650   @param qffield         CeedQFunctionField
651   @param[out] fieldname  Variable to store the field name
652 
653   @return An error code: 0 - success, otherwise - failure
654 
655   @ref Advanced
656 **/
657 
658 int CeedQFunctionFieldGetName(CeedQFunctionField qffield, char **fieldname) {
659   *fieldname = (char *)qffield->fieldname;
660   return 0;
661 }
662 
663 /**
664   @brief Get the number of components of a CeedQFunctionField
665 
666   @param qffield    CeedQFunctionField
667   @param[out] size  Variable to store the size of the field
668 
669   @return An error code: 0 - success, otherwise - failure
670 
671   @ref Advanced
672 **/
673 
674 int CeedQFunctionFieldGetSize(CeedQFunctionField qffield, CeedInt *size) {
675   *size = qffield->size;
676   return 0;
677 }
678 
679 /**
680   @brief Get the CeedEvalMode of a CeedQFunctionField
681 
682   @param qffield         CeedQFunctionField
683   @param[out] emode      Variable to store the field evaluation mode
684 
685   @return An error code: 0 - success, otherwise - failure
686 
687   @ref Advanced
688 **/
689 
690 int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qffield,
691                                   CeedEvalMode *emode) {
692   *emode = qffield->emode;
693   return 0;
694 }
695 
696 /**
697   @brief Destroy a CeedQFunction
698 
699   @param qf CeedQFunction to destroy
700 
701   @return An error code: 0 - success, otherwise - failure
702 
703   @ref Basic
704 **/
705 int CeedQFunctionDestroy(CeedQFunction *qf) {
706   int ierr;
707 
708   if (!*qf || --(*qf)->refcount > 0)
709     return 0;
710   // Backend destroy
711   if ((*qf)->Destroy) {
712     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
713   }
714   // Free fields
715   for (int i=0; i<(*qf)->numinputfields; i++) {
716     ierr = CeedFree(&(*(*qf)->inputfields[i]).fieldname); CeedChk(ierr);
717     ierr = CeedFree(&(*qf)->inputfields[i]); CeedChk(ierr);
718   }
719   for (int i=0; i<(*qf)->numoutputfields; i++) {
720     ierr = CeedFree(&(*(*qf)->outputfields[i]).fieldname); CeedChk(ierr);
721     ierr = CeedFree(&(*qf)->outputfields[i]); CeedChk(ierr);
722   }
723   ierr = CeedFree(&(*qf)->inputfields); CeedChk(ierr);
724   ierr = CeedFree(&(*qf)->outputfields); CeedChk(ierr);
725   // Free ctx if identity
726   if ((*qf)->identity) {
727     ierr = CeedFree(&(*qf)->ctx); CeedChk(ierr);
728   }
729 
730   ierr = CeedFree(&(*qf)->sourcepath); CeedChk(ierr);
731   ierr = CeedFree(&(*qf)->qfname); CeedChk(ierr);
732   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
733   ierr = CeedFree(qf); CeedChk(ierr);
734   return 0;
735 }
736 
737 /// @cond DOXYGEN_SKIP
738 // Indicate that no QFunction is provided by the user
739 CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
740 /// @endcond
741 /// @}
742