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