xref: /libCEED/interface/ceed-qfunction.c (revision e9f76d14a1d2b6594a8aca2075cf16ebd9c3bf6f)
1 // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include <ceed-impl.h>
9 #include <ceed.h>
10 #include <ceed/backend.h>
11 #include <ceed/jit-tools.h>
12 #include <limits.h>
13 #include <stdbool.h>
14 #include <stdio.h>
15 #include <string.h>
16 
17 /// @file
18 /// Implementation of public CeedQFunction interfaces
19 
20 /// @cond DOXYGEN_SKIP
21 static struct CeedQFunction_private ceed_qfunction_none;
22 /// @endcond
23 
24 /// @addtogroup CeedQFunctionUser
25 /// @{
26 
27 // Indicate that no `CeedQFunction` is provided by the user
28 const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
29 
30 /// @}
31 
32 /// @cond DOXYGEN_SKIP
33 static struct {
34   char              name[CEED_MAX_RESOURCE_LEN];
35   char              source[CEED_MAX_RESOURCE_LEN];
36   CeedInt           vec_length;
37   CeedQFunctionUser f;
38   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
39 } gallery_qfunctions[1024];
40 static size_t num_qfunctions;
41 /// @endcond
42 
43 /// ----------------------------------------------------------------------------
44 /// CeedQFunction Library Internal Functions
45 /// ----------------------------------------------------------------------------
46 /// @addtogroup CeedQFunctionDeveloper
47 /// @{
48 
49 /**
50   @brief Register a gallery `CeedQFunction`
51 
52   @param[in] name       Name for this backend to respond to
53   @param[in] source     Absolute path to source of `CeedQFunction`, "\path\CEED_DIR\gallery\folder\file.h:function_name"
54   @param[in] vec_length Vector length.
55                           Caller must ensure that number of quadrature points is a multiple of `vec_length`.
56   @param[in] f          Function pointer to evaluate action at quadrature points.
57                           See `CeedQFunctionUser`.
58   @param[in] init       Initialization function called by @ref CeedQFunctionCreateInteriorByName() when the `CeedQFunction` is selected.
59 
60   @return An error code: 0 - success, otherwise - failure
61 
62   @ref Developer
63 **/
64 int CeedQFunctionRegister(const char *name, const char *source, CeedInt vec_length, CeedQFunctionUser f,
65                           int (*init)(Ceed, const char *, CeedQFunction)) {
66   const char *relative_file_path;
67   int         ierr = 0;
68 
69   CeedDebugEnv("Gallery Register: %s", name);
70   CeedCall(CeedGetJitRelativePath(source, &relative_file_path));
71   CeedPragmaCritical(CeedQFunctionRegister) {
72     if (num_qfunctions < sizeof(gallery_qfunctions) / sizeof(gallery_qfunctions[0])) {
73       strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
74       gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN - 1] = 0;
75       strncpy(gallery_qfunctions[num_qfunctions].source, relative_file_path, CEED_MAX_RESOURCE_LEN);
76       gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN - 1] = 0;
77       gallery_qfunctions[num_qfunctions].vec_length                        = vec_length;
78       gallery_qfunctions[num_qfunctions].f                                 = f;
79       gallery_qfunctions[num_qfunctions].init                              = init;
80       num_qfunctions++;
81     } else {
82       ierr = 1;
83     }
84   }
85   CeedCheck(ierr == 0, NULL, CEED_ERROR_MAJOR, "Too many gallery CeedQFunctions");
86   return CEED_ERROR_SUCCESS;
87 }
88 
89 /**
90   @brief Set a `CeedQFunction` field, used by @ref CeedQFunctionAddInput() and @ref CeedQFunctionAddOutput()
91 
92   @param[out] f           `CeedQFunctionField`
93   @param[in]  field_name  Name of `CeedQFunction` field
94   @param[in]  size        Size of `CeedQFunction` field, (`num_comp * 1`) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_WEIGHT, (`num_comp * 1`) for @ref CEED_EVAL_INTERP for an \f$H^1\f$ space or (`num_comp * dim`) for an \f$H(\mathrm{div})\f$ or \f$H(\mathrm{curl})\f$ space, (`num_comp * dim`) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` and `curl_dim = dim` for @ref CEED_EVAL_CURL.
95   @param[in]  eval_mode   @ref CEED_EVAL_NONE to use values directly,
96                             @ref CEED_EVAL_WEIGHT to use quadrature weights,
97                             @ref CEED_EVAL_INTERP to use interpolated values,
98                             @ref CEED_EVAL_GRAD to use gradients,
99                             @ref CEED_EVAL_DIV to use divergence,
100                             @ref CEED_EVAL_CURL to use curl
101 
102   @return An error code: 0 - success, otherwise - failure
103 
104   @ref Developer
105 **/
106 static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
107   CeedCall(CeedCalloc(1, f));
108   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*f)->field_name));
109   (*f)->size      = size;
110   (*f)->eval_mode = eval_mode;
111   return CEED_ERROR_SUCCESS;
112 }
113 
114 /**
115   @brief View a field of a `CeedQFunction`
116 
117   @param[in] field        `CeedQFunction` field to view
118   @param[in] field_number Number of field being viewed
119   @param[in] in           true for input field, false for output
120   @param[in] tabs         Tabs to append before each new line
121   @param[in] stream       Stream to view to, e.g., `stdout`
122 
123   @return An error code: 0 - success, otherwise - failure
124 
125   @ref Utility
126 **/
127 static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt field_number, bool in, const char *tabs, FILE *stream) {
128   const char  *inout = in ? "Input" : "Output";
129   const char  *field_name;
130   CeedInt      size;
131   CeedEvalMode eval_mode;
132 
133   CeedCall(CeedQFunctionFieldGetData(field, &field_name, &size, &eval_mode));
134   fprintf(stream,
135           "%s    %s field %" CeedInt_FMT
136           ":\n%s"
137           "      Name: \"%s\"\n%s"
138           "      Size: %" CeedInt_FMT
139           "\n%s"
140           "      EvalMode: \"%s\"\n",
141           tabs, inout, field_number, tabs, field_name, tabs, size, tabs, CeedEvalModes[eval_mode]);
142   return CEED_ERROR_SUCCESS;
143 }
144 
145 /**
146   @brief View a `CeedQFunction` passed as a `CeedObject`
147 
148   @param[in] qf     `CeedQFunction` to view
149   @param[in] stream Filestream to write to
150 
151   @return An error code: 0 - success, otherwise - failure
152 
153   @ref Developer
154 **/
155 static int CeedQFunctionView_Object(CeedObject qf, FILE *stream) {
156   CeedCall(CeedQFunctionView((CeedQFunction)qf, stream));
157   return CEED_ERROR_SUCCESS;
158 }
159 
160 /**
161   @brief Destroy a `CeedQFunction` passed as a `CeedObject`
162 
163   @param[in,out] qf Address of `CeedQFunction` to destroy
164 
165   @return An error code: 0 - success, otherwise - failure
166 
167   @ref Developer
168 **/
169 static int CeedQFunctionDestroy_Object(CeedObject *qf) {
170   CeedCall(CeedQFunctionDestroy((CeedQFunction *)qf));
171   return CEED_ERROR_SUCCESS;
172 }
173 
174 /**
175   @brief Set flag to determine if Fortran interface is used
176 
177   @param[in,out] qf     CeedQFunction
178   @param[in]     status Boolean value to set as Fortran status
179 
180   @return An error code: 0 - success, otherwise - failure
181 
182   @ref Backend
183 **/
184 int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
185   qf->is_fortran = status;
186   return CEED_ERROR_SUCCESS;
187 }
188 
189 /// @}
190 
191 /// ----------------------------------------------------------------------------
192 /// CeedQFunction Backend API
193 /// ----------------------------------------------------------------------------
194 /// @addtogroup CeedQFunctionBackend
195 /// @{
196 
197 /**
198   @brief Get the vector length of a `CeedQFunction`
199 
200   @param[in]  qf         `CeedQFunction`
201   @param[out] vec_length Variable to store vector length
202 
203   @return An error code: 0 - success, otherwise - failure
204 
205   @ref Backend
206 **/
207 int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
208   *vec_length = qf->vec_length;
209   return CEED_ERROR_SUCCESS;
210 }
211 
212 /**
213   @brief Get the number of inputs and outputs to a `CeedQFunction`
214 
215   @param[in]  qf         `CeedQFunction`
216   @param[out] num_input  Variable to store number of input fields
217   @param[out] num_output Variable to store number of output fields
218 
219   @return An error code: 0 - success, otherwise - failure
220 
221   @ref Backend
222 **/
223 int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, CeedInt *num_output) {
224   if (num_input) *num_input = qf->num_input_fields;
225   if (num_output) *num_output = qf->num_output_fields;
226   return CEED_ERROR_SUCCESS;
227 }
228 
229 /**
230   @brief Get the name of the `CeedQFunction`.
231     Use the `name` if created via @ref CeedQFunctionCreateInteriorByName(), otherwise return the kernel name via @ref CeedQFunctionGetKernelName().
232 
233   @param[in]  qf   `CeedQFunction`
234   @param[out] name Variable to store `CeedQFunction` name
235 
236   @return An error code: 0 - success, otherwise - failure
237 
238   @ref Backend
239 **/
240 int CeedQFunctionGetName(CeedQFunction qf, const char **name) {
241   if (qf->is_gallery) {
242     *name = qf->gallery_name;
243   } else {
244     CeedCall(CeedQFunctionGetKernelName(qf, name));
245   }
246   return CEED_ERROR_SUCCESS;
247 }
248 
249 /**
250   @brief Get the name of the user function for a `CeedQFunction`
251 
252   @param[in]  qf          `CeedQFunction`
253   @param[out] kernel_name Variable to store string holding kernel name
254 
255   @return An error code: 0 - success, otherwise - failure
256 
257   @ref Backend
258 **/
259 int CeedQFunctionGetKernelName(CeedQFunction qf, const char **kernel_name) {
260   if (!qf->kernel_name) {
261     char *kernel_name_copy;
262 
263     if (qf->user_source) {
264       const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
265       size_t      kernel_name_len = strlen(kernel_name);
266 
267       CeedCall(CeedCalloc(kernel_name_len + 1, &kernel_name_copy));
268       memcpy(kernel_name_copy, kernel_name, kernel_name_len);
269     } else {
270       CeedCall(CeedCalloc(1, &kernel_name_copy));
271     }
272     qf->kernel_name = kernel_name_copy;
273   }
274 
275   *kernel_name = qf->kernel_name;
276   return CEED_ERROR_SUCCESS;
277 }
278 
279 /**
280   @brief Get the source path string for a `CeedQFunction`
281 
282   @param[in]  qf          `CeedQFunction`
283   @param[out] source_path Variable to store source path string
284 
285   @return An error code: 0 - success, otherwise - failure
286 
287   @ref Backend
288 **/
289 int CeedQFunctionGetSourcePath(CeedQFunction qf, const char **source_path) {
290   if (!qf->source_path && qf->user_source) {
291     Ceed        ceed;
292     bool        is_absolute_path;
293     char       *source_path_copy;
294     const char *absolute_path;
295     const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
296     size_t      kernel_name_len = strlen(kernel_name);
297 
298     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
299     CeedCall(CeedCheckFilePath(ceed, qf->user_source, &is_absolute_path));
300     if (is_absolute_path) {
301       absolute_path = (char *)qf->user_source;
302     } else {
303       CeedCall(CeedGetJitAbsolutePath(ceed, qf->user_source, &absolute_path));
304     }
305     CeedCall(CeedDestroy(&ceed));
306 
307     size_t source_len = strlen(absolute_path) - kernel_name_len - 1;
308 
309     CeedCall(CeedCalloc(source_len + 1, &source_path_copy));
310     memcpy(source_path_copy, absolute_path, source_len);
311     qf->source_path = source_path_copy;
312 
313     if (!is_absolute_path) CeedCall(CeedFree(&absolute_path));
314   }
315 
316   *source_path = (char *)qf->source_path;
317   return CEED_ERROR_SUCCESS;
318 }
319 
320 /**
321   @brief Initialize and load `CeedQFunction` source file into string buffer, including full text of local files in place of `#include "local.h"`.
322 
323   The `buffer` is set to `NULL` if there is no `CeedQFunction` source file.
324 
325   Note: This function may as well return a mutable buffer, but all current uses
326   do not modify it. (This is just a downside of `const` semantics with output
327   arguments instead of returns.)
328 
329   Note: Caller is responsible for freeing the string buffer with @ref CeedFree().
330 
331   @param[in]  qf            `CeedQFunction`
332   @param[out] source_buffer String buffer for source file contents
333 
334   @return An error code: 0 - success, otherwise - failure
335 
336   @ref Backend
337 **/
338 int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, const char **source_buffer) {
339   const char *source_path;
340 
341   CeedCall(CeedQFunctionGetSourcePath(qf, &source_path));
342   *source_buffer = NULL;
343   if (source_path) {
344     Ceed  ceed;
345     char *buffer = NULL;
346 
347     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
348     CeedCall(CeedLoadSourceToBuffer(ceed, source_path, &buffer));
349     CeedCall(CeedDestroy(&ceed));
350     *source_buffer = buffer;
351   }
352   return CEED_ERROR_SUCCESS;
353 }
354 
355 /**
356   @brief Get the User Function for a `CeedQFunction`
357 
358   @param[in]  qf `CeedQFunction`
359   @param[out] f  Variable to store user function
360 
361   @return An error code: 0 - success, otherwise - failure
362 
363   @ref Backend
364 **/
365 int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
366   *f = qf->function;
367   return CEED_ERROR_SUCCESS;
368 }
369 
370 /**
371   @brief Get global context for a `CeedQFunction`.
372 
373   Note: For `CeedQFunction` from the Fortran interface, this function will return the Fortran context `CeedQFunctionContext`.
374 
375   @param[in]  qf  CeedQFunction
376   @param[out] ctx Variable to store CeedQFunctionContext
377 
378   @return An error code: 0 - success, otherwise - failure
379 
380   @ref Backend
381 **/
382 int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
383   *ctx = NULL;
384   if (qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(qf->ctx, ctx));
385   return CEED_ERROR_SUCCESS;
386 }
387 
388 /**
389   @brief Get context data of a `CeedQFunction`
390 
391   @param[in]  qf       `CeedQFunction`
392   @param[in]  mem_type Memory type on which to access the data.
393                          If the backend uses a different memory type, this will perform a copy.
394   @param[out] data     Data on memory type mem_type
395 
396   @return An error code: 0 - success, otherwise - failure
397 
398   @ref Backend
399 **/
400 int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
401   bool                 is_writable;
402   CeedQFunctionContext ctx;
403 
404   CeedCall(CeedQFunctionGetContext(qf, &ctx));
405   if (ctx) {
406     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
407     if (is_writable) {
408       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
409     } else {
410       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
411     }
412   } else {
413     *(void **)data = NULL;
414   }
415   CeedCall(CeedQFunctionContextDestroy(&ctx));
416   return CEED_ERROR_SUCCESS;
417 }
418 
419 /**
420   @brief Restore context data of a `CeedQFunction`
421 
422   @param[in]     qf   `CeedQFunction`
423   @param[in,out] data Data to restore
424 
425   @return An error code: 0 - success, otherwise - failure
426 
427   @ref Backend
428 **/
429 int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) {
430   bool                 is_writable;
431   CeedQFunctionContext ctx;
432 
433   CeedCall(CeedQFunctionGetContext(qf, &ctx));
434   if (ctx) {
435     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
436     if (is_writable) {
437       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
438     } else {
439       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
440     }
441   }
442   CeedCall(CeedQFunctionContextDestroy(&ctx));
443   return CEED_ERROR_SUCCESS;
444 }
445 
446 /**
447   @brief Get true user context for a `CeedQFunction`
448 
449   Note: For all `CeedQFunction` this function will return the user `CeedQFunctionContext` and not interface context `CeedQFunctionContext`, if any such object exists.
450 
451   @param[in]  qf  `CeedQFunction`
452   @param[out] ctx Variable to store `CeedQFunctionContext`
453 
454   @return An error code: 0 - success, otherwise - failure
455   @ref Backend
456 **/
457 int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
458   CeedQFunctionContext qf_ctx;
459 
460   CeedCall(CeedQFunctionGetContext(qf, &qf_ctx));
461   if (qf->is_fortran) {
462     CeedFortranContext fortran_ctx = NULL;
463 
464     CeedCall(CeedQFunctionContextGetData(qf_ctx, CEED_MEM_HOST, &fortran_ctx));
465     *ctx = fortran_ctx->inner_ctx;
466     CeedCall(CeedQFunctionContextRestoreData(qf_ctx, &fortran_ctx));
467   } else {
468     *ctx = qf_ctx;
469   }
470   CeedCall(CeedQFunctionContextDestroy(&qf_ctx));
471   return CEED_ERROR_SUCCESS;
472 }
473 
474 /**
475   @brief Get inner context data of a `CeedQFunction`
476 
477   @param[in]  qf       `CeedQFunction`
478   @param[in]  mem_type Memory type on which to access the data.
479                          If the backend uses a different memory type, this will perform a copy.
480   @param[out] data     Data on memory type mem_type
481 
482   @return An error code: 0 - success, otherwise - failure
483 
484   @ref Backend
485 **/
486 int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
487   bool                 is_writable;
488   CeedQFunctionContext ctx;
489 
490   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
491   if (ctx) {
492     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
493     if (is_writable) {
494       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
495     } else {
496       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
497     }
498   } else {
499     *(void **)data = NULL;
500   }
501   return CEED_ERROR_SUCCESS;
502 }
503 
504 /**
505   @brief Restore inner context data of a `CeedQFunction`
506 
507   @param[in]     qf   `CeedQFunction`
508   @param[in,out] data Data to restore
509 
510   @return An error code: 0 - success, otherwise - failure
511 
512   @ref Backend
513 **/
514 int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) {
515   bool                 is_writable;
516   CeedQFunctionContext ctx;
517 
518   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
519   if (ctx) {
520     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
521     if (is_writable) {
522       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
523     } else {
524       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
525     }
526   }
527   *(void **)data = NULL;
528   return CEED_ERROR_SUCCESS;
529 }
530 
531 /**
532   @brief Determine if `CeedQFunction` is identity
533 
534   @param[in]  qf          `CeedQFunction`
535   @param[out] is_identity Variable to store identity status
536 
537   @return An error code: 0 - success, otherwise - failure
538 
539   @ref Backend
540 **/
541 int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
542   *is_identity = qf->is_identity;
543   return CEED_ERROR_SUCCESS;
544 }
545 
546 /**
547   @brief Determine if `CeedQFunctionContext` is writable
548 
549   @param[in]  qf          `CeedQFunction`
550   @param[out] is_writable Variable to store context writeable status
551 
552   @return An error code: 0 - success, otherwise - failure
553 
554   @ref Backend
555 **/
556 int CeedQFunctionIsContextWritable(CeedQFunction qf, bool *is_writable) {
557   *is_writable = qf->is_context_writable;
558   return CEED_ERROR_SUCCESS;
559 }
560 
561 /**
562   @brief Get backend data of a `CeedQFunction`
563 
564   @param[in]  qf   `CeedQFunction`
565   @param[out] data Variable to store data
566 
567   @return An error code: 0 - success, otherwise - failure
568 
569   @ref Backend
570 **/
571 int CeedQFunctionGetData(CeedQFunction qf, void *data) {
572   *(void **)data = qf->data;
573   return CEED_ERROR_SUCCESS;
574 }
575 
576 /**
577   @brief Set backend data of a `CeedQFunction`
578 
579   @param[in,out] qf   `CeedQFunction`
580   @param[in]     data Data to set
581 
582   @return An error code: 0 - success, otherwise - failure
583 
584   @ref Backend
585 **/
586 int CeedQFunctionSetData(CeedQFunction qf, void *data) {
587   qf->data = data;
588   return CEED_ERROR_SUCCESS;
589 }
590 
591 /**
592   @brief Get a boolean value indicating if the `CeedQFunction` is immutable
593 
594   @param[in]  qf           `CeedOperator`
595   @param[out] is_immutable Variable to store immutability status
596 
597   @return An error code: 0 - success, otherwise - failure
598 
599   @ref Backend
600 **/
601 int CeedQFunctionIsImmutable(CeedQFunction qf, bool *is_immutable) {
602   *is_immutable = qf->is_immutable;
603   return CEED_ERROR_SUCCESS;
604 }
605 
606 /**
607   @brief Set the immutable flag of a `CeedQFunction` to `true`
608 
609   @param[in,out] qf `CeedQFunction`
610 
611   @return An error code: 0 - success, otherwise - failure
612 
613   @ref Backend
614 **/
615 int CeedQFunctionSetImmutable(CeedQFunction qf) {
616   qf->is_immutable = true;
617   return CEED_ERROR_SUCCESS;
618 }
619 
620 /**
621   @brief Increment the reference counter for a `CeedQFunction`
622 
623   @param[in,out] qf `CeedQFunction` to increment the reference counter
624 
625   @return An error code: 0 - success, otherwise - failure
626 
627   @ref Backend
628 **/
629 int CeedQFunctionReference(CeedQFunction qf) {
630   CeedCall(CeedObjectReference((CeedObject)qf));
631   return CEED_ERROR_SUCCESS;
632 }
633 
634 /**
635   @brief Estimate number of FLOPs per quadrature required to apply `CeedQFunction`
636 
637   @param[in]  qf    `CeedQFunction` to estimate FLOPs for
638   @param[out] flops Address of variable to hold FLOPs estimate
639 
640   @ref Backend
641 **/
642 int CeedQFunctionGetFlopsEstimate(CeedQFunction qf, CeedSize *flops) {
643   *flops = qf->user_flop_estimate;
644   return CEED_ERROR_SUCCESS;
645 }
646 
647 /// @}
648 
649 /// ----------------------------------------------------------------------------
650 /// CeedQFunction Public API
651 /// ----------------------------------------------------------------------------
652 /// @addtogroup CeedQFunctionUser
653 /// @{
654 
655 /**
656   @brief Create a `CeedQFunction` for evaluating interior (volumetric) terms
657 
658   @param[in]  ceed       `Ceed` object used to create the `CeedQFunction`
659   @param[in]  vec_length Vector length.
660                            Caller must ensure that number of quadrature points is a multiple of `vec_length`.
661   @param[in]  f          Function pointer to evaluate action at quadrature points.
662                            See `CeedQFunctionUser`.
663   @param[in]  source     Absolute path to source of `CeedQFunctionUser`, "\abs_path\file.h:function_name".
664                            The entire source file must only contain constructs supported by all targeted backends (i.e. CUDA for `/gpu/cuda`, OpenCL/SYCL for `/gpu/sycl`, etc.).
665                            The entire contents of this file and all locally included files are used during JiT compilation for GPU backends.
666                            The header `ceed/types.h` is preferred over `ceed.h` or `ceed/ceed.h` for `CeedQFunction` source files.
667                            The macro `CEED_RUNNING_JIT_PASS` is set during JiT and can be used to guard include statements that JiT compilers cannot use, such as `math.h` or `std*.h`.
668                            All source files must be at the provided filepath at runtime for JiT to function.
669   @param[out] qf         Address of the variable where the newly created `CeedQFunction` will be stored
670 
671   @return An error code: 0 - success, otherwise - failure
672 
673   See \ref CeedQFunctionUser for details on the call-back function `f` arguments.
674 
675   @ref User
676 **/
677 int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length, CeedQFunctionUser f, const char *source, CeedQFunction *qf) {
678   char *user_source_copy;
679 
680   if (!ceed->QFunctionCreate) {
681     Ceed delegate;
682 
683     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "QFunction"));
684     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedQFunctionCreateInterior");
685     CeedCall(CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf));
686     CeedCall(CeedDestroy(&delegate));
687     return CEED_ERROR_SUCCESS;
688   }
689 
690   CeedCheck(!strlen(source) || strrchr(source, ':'), ceed, CEED_ERROR_INCOMPLETE,
691             "Provided path to source does not include function name. Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"", source);
692 
693   CeedCall(CeedCalloc(1, qf));
694   CeedCall(CeedObjectCreate(ceed, CeedQFunctionView_Object, CeedQFunctionDestroy_Object, &(*qf)->obj));
695   (*qf)->vec_length          = vec_length;
696   (*qf)->is_identity         = false;
697   (*qf)->is_context_writable = true;
698   (*qf)->function            = f;
699   (*qf)->user_flop_estimate  = -1;
700   if (strlen(source)) {
701     size_t user_source_len = strlen(source);
702 
703     CeedCall(CeedCalloc(user_source_len + 1, &user_source_copy));
704     memcpy(user_source_copy, source, user_source_len);
705     (*qf)->user_source = user_source_copy;
706   }
707   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields));
708   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields));
709   CeedCall(ceed->QFunctionCreate(*qf));
710   return CEED_ERROR_SUCCESS;
711 }
712 
713 /**
714   @brief Create a `CeedQFunction` for evaluating interior (volumetric) terms by name
715 
716   @param[in]  ceed `Ceed` object used to create the `CeedQFunction`
717   @param[in]  name Name of `CeedQFunction` to use from gallery
718   @param[out] qf   Address of the variable where the newly created `CeedQFunction` will be stored
719 
720   @return An error code: 0 - success, otherwise - failure
721 
722   @ref User
723 **/
724 int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, CeedQFunction *qf) {
725   size_t match_len = 0, match_index = UINT_MAX;
726 
727   CeedCall(CeedQFunctionRegisterAll());
728   // Find matching backend
729   CeedCheck(name, ceed, CEED_ERROR_INCOMPLETE, "No CeedQFunction name provided");
730   for (size_t i = 0; i < num_qfunctions; i++) {
731     size_t      n;
732     const char *curr_name = gallery_qfunctions[i].name;
733     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {
734     }
735     if (n > match_len) {
736       match_len   = n;
737       match_index = i;
738     }
739   }
740   CeedCheck(match_len > 0, ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery CeedQFunction");
741 
742   // Create QFunction
743   CeedCall(CeedQFunctionCreateInterior(ceed, gallery_qfunctions[match_index].vec_length, gallery_qfunctions[match_index].f,
744                                        gallery_qfunctions[match_index].source, qf));
745 
746   // QFunction specific setup
747   CeedCall(gallery_qfunctions[match_index].init(ceed, name, *qf));
748 
749   // Copy name
750   CeedCall(CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name));
751   (*qf)->is_gallery = true;
752   return CEED_ERROR_SUCCESS;
753 }
754 
755 /**
756   @brief Create an identity `CeedQFunction`.
757 
758   Inputs are written into outputs in the order given.
759   This is useful for `CeedOperator that can be represented with only the action of a `CeedElemRestriction` and `CeedBasis`, such as restriction and prolongation operators for p-multigrid.
760   Backends may optimize `CeedOperator` with this `CeedQFunction` to avoid the copy of input data to output fields by using the same memory location for both.
761 
762   @param[in]  ceed     `Ceed` object used to create the `CeedQFunction`
763   @param[in]  size     Size of the `CeedQFunction` fields
764   @param[in]  in_mode  @ref CeedEvalMode for input to `CeedQFunction`
765   @param[in]  out_mode @ref CeedEvalMode for output to `CeedQFunction`
766   @param[out] qf       Address of the variable where the newly created `CeedQFunction` will be stored
767 
768   @return An error code: 0 - success, otherwise - failure
769 
770   @ref User
771 **/
772 int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, CeedEvalMode out_mode, CeedQFunction *qf) {
773   CeedQFunctionContext  ctx;
774   CeedContextFieldLabel size_label;
775 
776   CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Identity", qf));
777   CeedCall(CeedQFunctionAddInput(*qf, "input", size, in_mode));
778   CeedCall(CeedQFunctionAddOutput(*qf, "output", size, out_mode));
779 
780   (*qf)->is_identity = true;
781 
782   CeedCall(CeedQFunctionGetContext(*qf, &ctx));
783   CeedCall(CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label));
784   CeedCall(CeedQFunctionContextSetInt32(ctx, size_label, &size));
785   CeedCall(CeedQFunctionContextDestroy(&ctx));
786   return CEED_ERROR_SUCCESS;
787 }
788 
789 /**
790   @brief Copy the pointer to a `CeedQFunction`.
791 
792   Both pointers should be destroyed with @ref CeedQFunctionDestroy().
793 
794   Note: If the value of `*qf_copy` passed to this function is non-NULL, then it is assumed that `*qf_copy` is a pointer to a `CeedQFunction`.
795         This `CeedQFunction` will be destroyed if `*qf_copy` is the only reference to this `CeedQFunction`.
796 
797   @param[in]  qf      `CeedQFunction` to copy reference to
798   @param[out] qf_copy Variable to store copied reference
799 
800   @return An error code: 0 - success, otherwise - failure
801 
802   @ref User
803 **/
804 int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
805   CeedCall(CeedQFunctionReference(qf));
806   CeedCall(CeedQFunctionDestroy(qf_copy));
807   *qf_copy = qf;
808   return CEED_ERROR_SUCCESS;
809 }
810 
811 /**
812   @brief Add a `CeedQFunction` input
813 
814   @param[in,out] qf         `CeedQFunction`
815   @param[in]     field_name Name of `CeedQFunction` field
816   @param[in]     size       Size of `CeedQFunction` field,
817                               (`num_comp * 1`) for @ref CEED_EVAL_NONE,
818                               (`num_comp * 1`) for @ref CEED_EVAL_INTERP for an \f$H^1\f$ space or (`num_comp * dim`) for an \f$H(\mathrm{div})\f$ or \f$H(\mathrm{curl})\f$ space,
819                               (`num_comp * dim`) for @ref CEED_EVAL_GRAD,
820                               (`num_comp * 1`) for @ref CEED_EVAL_DIV, and
821                               (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` otherwise `curl_dim = dim` for @ref CEED_EVAL_CURL.
822   @param[in]     eval_mode  @ref CEED_EVAL_NONE to use values directly,
823                               @ref CEED_EVAL_INTERP to use interpolated values,
824                               @ref CEED_EVAL_GRAD to use gradients,
825                               @ref CEED_EVAL_DIV to use divergence,
826                               @ref CEED_EVAL_CURL to use curl
827 
828   Note: In the user `CeedQFunctionUser`, the `in` argument list the fields in the order given by the calls to `CeedQFunctionAddInput`.
829 
830   @return An error code: 0 - success, otherwise - failure
831 
832   @ref User
833 **/
834 int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
835   bool is_immutable;
836 
837   CeedCall(CeedQFunctionIsImmutable(qf, &is_immutable));
838   CeedCheck(!is_immutable, CeedQFunctionReturnCeed(qf), CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
839   CeedCheck(eval_mode != CEED_EVAL_WEIGHT || size == 1, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION, "CEED_EVAL_WEIGHT should have size 1");
840   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
841     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
842               "CeedQFunction field names must be unique. Duplicate name: %s", field_name);
843   }
844   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
845     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
846               "CeedQFunction field names must be unique. Duplicate name: %s", field_name);
847   }
848   CeedCall(CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], field_name, size, eval_mode));
849   qf->num_input_fields++;
850   return CEED_ERROR_SUCCESS;
851 }
852 
853 /**
854   @brief Add a `CeedQFunction` output
855 
856   @param[in,out] qf         `CeedQFunction`
857   @param[in]     field_name Name of `CeedQFunction` field
858   @param[in]     size       Size of `CeedQFunction` field,
859                               (`num_comp * 1`) for @ref CEED_EVAL_NONE,
860                               (`num_comp * 1`) for @ref CEED_EVAL_INTERP for an \f$H^1\f$ space or (`num_comp * dim`) for an \f$H(\mathrm{div})\f$ or \f$H(\mathrm{curl})\f$ space,
861                               (`num_comp * dim`) for @ref CEED_EVAL_GRAD,
862                               (`num_comp * 1`) for @ref CEED_EVAL_DIV, and
863                               (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` otherwise `curl_dim = dim` for @ref CEED_EVAL_CURL.
864   @param[in]     eval_mode  @ref CEED_EVAL_NONE to use values directly,
865                               @ref CEED_EVAL_INTERP to use interpolated values,
866                               @ref CEED_EVAL_GRAD to use gradients,
867                               @ref CEED_EVAL_DIV to use divergence,
868                               @ref CEED_EVAL_CURL to use curl.
869 
870   Note: In the user `CeedQFunctionUser`, the `out` argument list the fields in the order given by the calls to `CeedQFunctionAddOutput`.
871 
872   @return An error code: 0 - success, otherwise - failure
873 
874   @ref User
875 **/
876 int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
877   bool is_immutable;
878 
879   CeedCall(CeedQFunctionIsImmutable(qf, &is_immutable));
880   CeedCheck(!is_immutable, CeedQFunctionReturnCeed(qf), CEED_ERROR_MAJOR, "CeedQFunction cannot be changed after set as immutable");
881   CeedCheck(eval_mode != CEED_EVAL_WEIGHT, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION,
882             "Cannot create CeedQFunction output with CEED_EVAL_WEIGHT");
883   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
884     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
885               "CeedQFunction field names must be unique");
886   }
887   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
888     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
889               "CeedQFunction field names must be unique");
890   }
891   CeedCall(CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], field_name, size, eval_mode));
892   qf->num_output_fields++;
893   return CEED_ERROR_SUCCESS;
894 }
895 
896 /**
897   @brief Get the `CeedQFunctionField` of a `CeedQFunction`
898 
899   Note: Calling this function asserts that setup is complete and sets the `CeedQFunction` as immutable.
900 
901   @param[in]  qf                `CeedQFunction`
902   @param[out] num_input_fields  Variable to store number of input fields
903   @param[out] input_fields      Variable to store input fields
904   @param[out] num_output_fields Variable to store number of output fields
905   @param[out] output_fields     Variable to store output fields
906 
907   @return An error code: 0 - success, otherwise - failure
908 
909   @ref Advanced
910 **/
911 int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields, CeedQFunctionField **input_fields, CeedInt *num_output_fields,
912                            CeedQFunctionField **output_fields) {
913   CeedCall(CeedQFunctionSetImmutable(qf));
914   if (num_input_fields) *num_input_fields = qf->num_input_fields;
915   if (input_fields) *input_fields = qf->input_fields;
916   if (num_output_fields) *num_output_fields = qf->num_output_fields;
917   if (output_fields) *output_fields = qf->output_fields;
918   return CEED_ERROR_SUCCESS;
919 }
920 
921 /**
922   @brief Get the name of a `CeedQFunctionField`
923 
924   @param[in]  qf_field   `CeedQFunctionField`
925   @param[out] field_name Variable to store the field name
926 
927   @return An error code: 0 - success, otherwise - failure
928 
929   @ref Advanced
930 **/
931 int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, const char **field_name) {
932   *field_name = qf_field->field_name;
933   return CEED_ERROR_SUCCESS;
934 }
935 
936 /**
937   @brief Get the number of components of a `CeedQFunctionField`
938 
939   @param[in]  qf_field `CeedQFunctionField`
940   @param[out] size     Variable to store the size of the field
941 
942   @return An error code: 0 - success, otherwise - failure
943 
944   @ref Advanced
945 **/
946 int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
947   *size = qf_field->size;
948   return CEED_ERROR_SUCCESS;
949 }
950 
951 /**
952   @brief Get the @ref CeedEvalMode of a `CeedQFunctionField`
953 
954   @param[in]  qf_field  `CeedQFunctionField`
955   @param[out] eval_mode Variable to store the field evaluation mode
956 
957   @return An error code: 0 - success, otherwise - failure
958 
959   @ref Advanced
960 **/
961 int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, CeedEvalMode *eval_mode) {
962   *eval_mode = qf_field->eval_mode;
963   return CEED_ERROR_SUCCESS;
964 }
965 
966 /**
967   @brief Get the data of a `CeedQFunctionField`.
968 
969   Any arguments set as `NULL` are ignored.
970 
971   @param[in]  qf_field   `CeedQFunctionField`
972   @param[out] field_name Variable to store the field name
973   @param[out] size       Variable to store the size of the field
974   @param[out] eval_mode  Variable to store the field evaluation mode
975 
976   @return An error code: 0 - success, otherwise - failure
977 
978   @ref Advanced
979 **/
980 int CeedQFunctionFieldGetData(CeedQFunctionField qf_field, const char **field_name, CeedInt *size, CeedEvalMode *eval_mode) {
981   if (field_name) CeedCall(CeedQFunctionFieldGetName(qf_field, field_name));
982   if (size) CeedCall(CeedQFunctionFieldGetSize(qf_field, size));
983   if (eval_mode) CeedCall(CeedQFunctionFieldGetEvalMode(qf_field, eval_mode));
984   return CEED_ERROR_SUCCESS;
985 }
986 
987 /**
988   @brief Set global context for a `CeedQFunction`
989 
990   @param[in,out] qf  `CeedQFunction`
991   @param[in]     ctx Context data to set
992 
993   @return An error code: 0 - success, otherwise - failure
994 
995   @ref User
996 **/
997 int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
998   CeedCall(CeedQFunctionContextDestroy(&qf->ctx));
999   qf->ctx = ctx;
1000   if (ctx) CeedCall(CeedQFunctionContextReference(ctx));
1001   return CEED_ERROR_SUCCESS;
1002 }
1003 
1004 /**
1005   @brief Set writability of `CeedQFunctionContext` when calling the `CeedQFunctionUser`.
1006 
1007   The default value is `is_writable == true`.
1008 
1009   Setting `is_writable == true` indicates the `CeedQFunctionUser` writes into the `CeedQFunctionContext` and requires memory synchronization after calling @ref CeedQFunctionApply().
1010 
1011   Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not modify the `CeedQFunctionContext`.
1012   Violating this assertion may lead to inconsistent data.
1013 
1014   Setting `is_writable == false` may offer a performance improvement on GPU backends.
1015 
1016   @param[in,out] qf          `CeedQFunction`
1017   @param[in]     is_writable Boolean flag for writability status
1018 
1019   @return An error code: 0 - success, otherwise - failure
1020 
1021   @ref User
1022 **/
1023 int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
1024   qf->is_context_writable = is_writable;
1025   return CEED_ERROR_SUCCESS;
1026 }
1027 
1028 /**
1029   @brief Set estimated number of FLOPs per quadrature required to apply `CeedQFunction`
1030 
1031   @param[in]  qf    `CeedQFunction` to estimate FLOPs for
1032   @param[out] flops FLOPs per quadrature point estimate
1033 
1034   @ref Backend
1035 **/
1036 int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
1037   CeedCheck(flops >= 0, CeedQFunctionReturnCeed(qf), CEED_ERROR_INCOMPATIBLE, "Must set non-negative FLOPs estimate");
1038   qf->user_flop_estimate = flops;
1039   return CEED_ERROR_SUCCESS;
1040 }
1041 
1042 /**
1043   @brief Set the number of tabs to indent for @ref CeedQFunctionView() output
1044 
1045   @param[in] qf       `CeedQFunction` to set the number of view tabs
1046   @param[in] num_tabs Number of view tabs to set
1047 
1048   @return Error code: 0 - success, otherwise - failure
1049 
1050   @ref User
1051 **/
1052 int CeedQFunctionSetNumViewTabs(CeedQFunction qf, CeedInt num_tabs) {
1053   CeedCall(CeedObjectSetNumViewTabs((CeedObject)qf, num_tabs));
1054   return CEED_ERROR_SUCCESS;
1055 }
1056 
1057 /**
1058   @brief Get the number of tabs to indent for @ref CeedQFunctionView() output
1059 
1060   @param[in]  qf       `CeedQFunction` to get the number of view tabs
1061   @param[out] num_tabs Number of view tabs
1062 
1063   @return Error code: 0 - success, otherwise - failure
1064 
1065   @ref User
1066 **/
1067 int CeedQFunctionGetNumViewTabs(CeedQFunction qf, CeedInt *num_tabs) {
1068   CeedCall(CeedObjectGetNumViewTabs((CeedObject)qf, num_tabs));
1069   return CEED_ERROR_SUCCESS;
1070 }
1071 
1072 /**
1073   @brief View a `CeedQFunction`
1074 
1075   @param[in] qf     `CeedQFunction` to view
1076   @param[in] stream Stream to write; typically `stdout` or a file
1077 
1078   @return Error code: 0 - success, otherwise - failure
1079 
1080   @ref User
1081 **/
1082 int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
1083   char       *tabs = NULL;
1084   const char *name;
1085 
1086   {
1087     CeedInt num_tabs = 0;
1088 
1089     CeedCall(CeedQFunctionGetNumViewTabs(qf, &num_tabs));
1090     CeedCall(CeedCalloc(CEED_TAB_WIDTH * num_tabs + 1, &tabs));
1091     for (CeedInt i = 0; i < CEED_TAB_WIDTH * num_tabs; i++) tabs[i] = ' ';
1092   }
1093 
1094   CeedCall(CeedQFunctionGetName(qf, &name));
1095   fprintf(stream, "%s%sCeedQFunction - %s\n", tabs, qf->is_gallery ? "Gallery " : "User ", name);
1096 
1097   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", tabs, qf->num_input_fields, qf->num_input_fields > 1 ? "s" : "");
1098   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
1099     CeedCall(CeedQFunctionFieldView(qf->input_fields[i], i, 1, tabs, stream));
1100   }
1101 
1102   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", tabs, qf->num_output_fields, qf->num_output_fields > 1 ? "s" : "");
1103   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
1104     CeedCall(CeedQFunctionFieldView(qf->output_fields[i], i, 0, tabs, stream));
1105   }
1106   CeedCall(CeedFree(&tabs));
1107   return CEED_ERROR_SUCCESS;
1108 }
1109 
1110 /**
1111   @brief Get the `Ceed` associated with a `CeedQFunction`
1112 
1113   @param[in]  qf   `CeedQFunction`
1114   @param[out] ceed Variable to store`Ceed`
1115 
1116   @return An error code: 0 - success, otherwise - failure
1117 
1118   @ref Advanced
1119 **/
1120 int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
1121   CeedCall(CeedObjectGetCeed((CeedObject)qf, ceed));
1122   return CEED_ERROR_SUCCESS;
1123 }
1124 
1125 /**
1126   @brief Return the `Ceed` associated with a `CeedQFunction`
1127 
1128   @param[in]  qf   `CeedQFunction`
1129 
1130   @return `Ceed` associated with the `qf`
1131 
1132   @ref Advanced
1133 **/
1134 Ceed CeedQFunctionReturnCeed(CeedQFunction qf) { return CeedObjectReturnCeed((CeedObject)qf); }
1135 
1136 /**
1137   @brief Apply the action of a `CeedQFunction`
1138 
1139   Note: Calling this function asserts that setup is complete and sets the `CeedQFunction` as immutable.
1140 
1141   @param[in]  qf `CeedQFunction`
1142   @param[in]  Q  Number of quadrature points
1143   @param[in]  u  Array of input `CeedVector`
1144   @param[out] v  Array of output `CeedVector`
1145 
1146   @return An error code: 0 - success, otherwise - failure
1147 
1148   @ref User
1149 **/
1150 int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, CeedVector *u, CeedVector *v) {
1151   CeedInt vec_length;
1152 
1153   CeedCheck(qf->Apply, CeedQFunctionReturnCeed(qf), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionApply");
1154   CeedCall(CeedQFunctionGetVectorLength(qf, &vec_length));
1155   CeedCheck(Q % vec_length == 0, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION,
1156             "Number of quadrature points %" CeedInt_FMT " must be a multiple of %" CeedInt_FMT, Q, qf->vec_length);
1157   CeedCall(CeedQFunctionSetImmutable(qf));
1158   CeedCall(qf->Apply(qf, Q, u, v));
1159   return CEED_ERROR_SUCCESS;
1160 }
1161 
1162 /**
1163   @brief Destroy a `CeedQFunction`
1164 
1165   @param[in,out] qf `CeedQFunction` to destroy
1166 
1167   @return An error code: 0 - success, otherwise - failure
1168 
1169   @ref User
1170 **/
1171 int CeedQFunctionDestroy(CeedQFunction *qf) {
1172   if (!*qf || CeedObjectDereference((CeedObject)*qf) > 0) {
1173     *qf = NULL;
1174     return CEED_ERROR_SUCCESS;
1175   }
1176   // Backend destroy
1177   if ((*qf)->Destroy) {
1178     CeedCall((*qf)->Destroy(*qf));
1179   }
1180   // Free fields
1181   for (CeedInt i = 0; i < (*qf)->num_input_fields; i++) {
1182     CeedCall(CeedFree(&(*(*qf)->input_fields[i]).field_name));
1183     CeedCall(CeedFree(&(*qf)->input_fields[i]));
1184   }
1185   for (CeedInt i = 0; i < (*qf)->num_output_fields; i++) {
1186     CeedCall(CeedFree(&(*(*qf)->output_fields[i]).field_name));
1187     CeedCall(CeedFree(&(*qf)->output_fields[i]));
1188   }
1189   CeedCall(CeedFree(&(*qf)->input_fields));
1190   CeedCall(CeedFree(&(*qf)->output_fields));
1191 
1192   // User context data object
1193   CeedCall(CeedQFunctionContextDestroy(&(*qf)->ctx));
1194 
1195   CeedCall(CeedFree(&(*qf)->user_source));
1196   CeedCall(CeedFree(&(*qf)->source_path));
1197   CeedCall(CeedFree(&(*qf)->gallery_name));
1198   CeedCall(CeedFree(&(*qf)->kernel_name));
1199   CeedCall(CeedObjectDestroy_Private(&(*qf)->obj));
1200   CeedCall(CeedFree(qf));
1201   return CEED_ERROR_SUCCESS;
1202 }
1203 
1204 /// @}
1205