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