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