xref: /libCEED/interface/ceed-qfunction.c (revision 441428df2c2036d88c450263ab42e401d8838e73)
1d7b241e6Sjeremylt // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2d7b241e6Sjeremylt // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3d7b241e6Sjeremylt // reserved. See files LICENSE and NOTICE for details.
4d7b241e6Sjeremylt //
5d7b241e6Sjeremylt // This file is part of CEED, a collection of benchmarks, miniapps, software
6d7b241e6Sjeremylt // libraries and APIs for efficient high-order finite element and spectral
7d7b241e6Sjeremylt // element discretizations for exascale applications. For more information and
8d7b241e6Sjeremylt // source code availability see http://github.com/ceed.
9d7b241e6Sjeremylt //
10d7b241e6Sjeremylt // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11d7b241e6Sjeremylt // a collaborative effort of two U.S. Department of Energy organizations (Office
12d7b241e6Sjeremylt // of Science and the National Nuclear Security Administration) responsible for
13d7b241e6Sjeremylt // the planning and preparation of a capable exascale ecosystem, including
14d7b241e6Sjeremylt // software, applications, hardware, advanced system engineering and early
15d7b241e6Sjeremylt // testbed platforms, in support of the nation's exascale computing imperative.
16d7b241e6Sjeremylt 
17ec3da8bcSJed Brown #include <ceed/ceed.h>
18ec3da8bcSJed Brown #include <ceed/backend.h>
193d3250a0SJeremy L Thompson #include <ceed/jit-tools.h>
203d576824SJeremy L Thompson #include <ceed-impl.h>
21288c0443SJeremy L Thompson #include <limits.h>
223d576824SJeremy L Thompson #include <stdbool.h>
233d576824SJeremy L Thompson #include <stdio.h>
2443e1b16fSJeremy L Thompson #include <stdlib.h>
253d576824SJeremy L Thompson #include <string.h>
26288c0443SJeremy L Thompson 
277a982d89SJeremy L. Thompson /// @file
287a982d89SJeremy L. Thompson /// Implementation of public CeedQFunction interfaces
297a982d89SJeremy L. Thompson 
30288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP
31442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none;
32442e7f0bSjeremylt /// @endcond
33442e7f0bSjeremylt 
347a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
357a982d89SJeremy L. Thompson /// @{
367a982d89SJeremy L. Thompson 
377a982d89SJeremy L. Thompson // Indicate that no QFunction is provided by the user
387a982d89SJeremy L. Thompson const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
397a982d89SJeremy L. Thompson 
407a982d89SJeremy L. Thompson /// @}
417a982d89SJeremy L. Thompson 
42442e7f0bSjeremylt /// @cond DOXYGEN_SKIP
43288c0443SJeremy L Thompson static struct {
44288c0443SJeremy L Thompson   char name[CEED_MAX_RESOURCE_LEN];
45288c0443SJeremy L Thompson   char source[CEED_MAX_RESOURCE_LEN];
46d1d35e2fSjeremylt   CeedInt vec_length;
47288c0443SJeremy L Thompson   CeedQFunctionUser f;
48288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
49d1d35e2fSjeremylt } gallery_qfunctions[1024];
50288c0443SJeremy L Thompson static size_t num_qfunctions;
51288c0443SJeremy L Thompson /// @endcond
52d7b241e6Sjeremylt 
53777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
54777ff853SJeremy L Thompson /// CeedQFunction Library Internal Functions
55777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
56777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper
57777ff853SJeremy L Thompson /// @{
58777ff853SJeremy L Thompson 
59b11c1e72Sjeremylt /**
60288c0443SJeremy L Thompson   @brief Register a gallery QFunction
61288c0443SJeremy L Thompson 
62288c0443SJeremy L Thompson   @param name        Name for this backend to respond to
631176cc3aSJeremy L Thompson   @param source      Absolute path to source of QFunction,
641176cc3aSJeremy L Thompson                        "\path\CEED_DIR\gallery\folder\file.h:function_name"
65d1d35e2fSjeremylt   @param vec_length  Vector length.  Caller must ensure that number of quadrature
66d1d35e2fSjeremylt                        points is a multiple of vec_length.
67288c0443SJeremy L Thompson   @param f           Function pointer to evaluate action at quadrature points.
68288c0443SJeremy L Thompson                        See \ref CeedQFunctionUser.
69288c0443SJeremy L Thompson   @param init        Initialization function called by CeedQFunctionInit() when the
70288c0443SJeremy L Thompson                        QFunction is selected.
71288c0443SJeremy L Thompson 
72288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
73288c0443SJeremy L Thompson 
747a982d89SJeremy L. Thompson   @ref Developer
75288c0443SJeremy L Thompson **/
76288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source,
77d1d35e2fSjeremylt                           CeedInt vec_length, CeedQFunctionUser f,
78288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
79d1d35e2fSjeremylt   if (num_qfunctions >= sizeof(gallery_qfunctions) / sizeof(
80d1d35e2fSjeremylt         gallery_qfunctions[0]))
81c042f62fSJeremy L Thompson     // LCOV_EXCL_START
82e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
83c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
84c042f62fSJeremy L Thompson 
858ccf1006SJeremy L Thompson   CeedDebugEnv("Gallery Register: %s", name);
868ccf1006SJeremy L Thompson 
87d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
88d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
89d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].source, source,
90d1d35e2fSjeremylt           CEED_MAX_RESOURCE_LEN);
91d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
92d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].vec_length = vec_length;
93d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].f = f;
94d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].init = init;
95288c0443SJeremy L Thompson   num_qfunctions++;
96e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
97288c0443SJeremy L Thompson }
98288c0443SJeremy L Thompson 
99288c0443SJeremy L Thompson /**
1007a982d89SJeremy L. Thompson   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
1017a982d89SJeremy L. Thompson 
1027a982d89SJeremy L. Thompson   @param f           CeedQFunctionField
103d1d35e2fSjeremylt   @param field_name  Name of QFunction field
104d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
105d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT
106d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
1077a982d89SJeremy L. Thompson                        \ref CEED_EVAL_INTERP to use interpolated values,
1087a982d89SJeremy L. Thompson                        \ref CEED_EVAL_GRAD to use gradients,
1097a982d89SJeremy L. Thompson                        \ref CEED_EVAL_WEIGHT to use quadrature weights.
1107a982d89SJeremy L. Thompson 
1117a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1127a982d89SJeremy L. Thompson 
1137a982d89SJeremy L. Thompson   @ref Developer
1147a982d89SJeremy L. Thompson **/
115d1d35e2fSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name,
116d1d35e2fSjeremylt                                  CeedInt size, CeedEvalMode eval_mode) {
117cdf32b93SJeremy L Thompson   int ierr;
1187a982d89SJeremy L. Thompson 
119e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, f); CeedChk(ierr);
120f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(field_name, (char **)&(*f)->field_name);
121f7e22acaSJeremy L Thompson   CeedChk(ierr);
1227a982d89SJeremy L. Thompson   (*f)->size = size;
123d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
124e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1257a982d89SJeremy L. Thompson }
1267a982d89SJeremy L. Thompson 
1277a982d89SJeremy L. Thompson /**
1287a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1297a982d89SJeremy L. Thompson 
1307a982d89SJeremy L. Thompson   @param[in] field         QFunction field to view
131d1d35e2fSjeremylt   @param[in] field_number  Number of field being viewed
1324c4400c7SValeria Barra   @param[in] in            true for input field, false for output
1337a982d89SJeremy L. Thompson   @param[in] stream        Stream to view to, e.g., stdout
1347a982d89SJeremy L. Thompson 
1357a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1367a982d89SJeremy L. Thompson 
1377a982d89SJeremy L. Thompson   @ref Utility
1387a982d89SJeremy L. Thompson **/
139d1d35e2fSjeremylt static int CeedQFunctionFieldView(CeedQFunctionField field,
140d1d35e2fSjeremylt                                   CeedInt field_number,
1417a982d89SJeremy L. Thompson                                   bool in, FILE *stream) {
1428229195eSjeremylt   int ierr;
1437a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1448229195eSjeremylt   char *field_name;
1458229195eSjeremylt   ierr = CeedQFunctionFieldGetName(field, &field_name); CeedChk(ierr);
1468229195eSjeremylt   CeedInt size;
1478229195eSjeremylt   ierr = CeedQFunctionFieldGetSize(field, &size); CeedChk(ierr);
1488229195eSjeremylt   CeedEvalMode eval_mode;
1498229195eSjeremylt   ierr = CeedQFunctionFieldGetEvalMode(field, &eval_mode); CeedChk(ierr);
1507a982d89SJeremy L. Thompson   fprintf(stream, "    %s Field [%d]:\n"
1517a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1527a982d89SJeremy L. Thompson           "      Size: %d\n"
1537a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1548229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
155e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1567a982d89SJeremy L. Thompson }
1577a982d89SJeremy L. Thompson 
158777ff853SJeremy L Thompson /**
159777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
160777ff853SJeremy L Thompson 
161777ff853SJeremy L Thompson   @param qf      CeedQFunction
162777ff853SJeremy L Thompson   @param status  Boolean value to set as Fortran status
163777ff853SJeremy L Thompson 
164777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
165777ff853SJeremy L Thompson 
166777ff853SJeremy L Thompson   @ref Backend
167777ff853SJeremy L Thompson **/
168777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
169f04ea552SJeremy L Thompson   qf->is_fortran = status;
170e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
171777ff853SJeremy L Thompson }
172777ff853SJeremy L Thompson 
1737a982d89SJeremy L. Thompson /// @}
1747a982d89SJeremy L. Thompson 
1757a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1767a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1777a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1787a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1797a982d89SJeremy L. Thompson /// @{
1807a982d89SJeremy L. Thompson 
1817a982d89SJeremy L. Thompson /**
1827a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1837a982d89SJeremy L. Thompson 
1847a982d89SJeremy L. Thompson   @param qf               CeedQFunction
185d1d35e2fSjeremylt   @param[out] vec_length  Variable to store vector length
1867a982d89SJeremy L. Thompson 
1877a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1887a982d89SJeremy L. Thompson 
1897a982d89SJeremy L. Thompson   @ref Backend
1907a982d89SJeremy L. Thompson **/
191d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
192d1d35e2fSjeremylt   *vec_length = qf->vec_length;
193e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1947a982d89SJeremy L. Thompson }
1957a982d89SJeremy L. Thompson 
1967a982d89SJeremy L. Thompson /**
1977a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
1987a982d89SJeremy L. Thompson 
1997a982d89SJeremy L. Thompson   @param qf               CeedQFunction
200d1d35e2fSjeremylt   @param[out] num_input   Variable to store number of input fields
201d1d35e2fSjeremylt   @param[out] num_output  Variable to store number of output fields
2027a982d89SJeremy L. Thompson 
2037a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2047a982d89SJeremy L. Thompson 
2057a982d89SJeremy L. Thompson   @ref Backend
2067a982d89SJeremy L. Thompson **/
207d1d35e2fSjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input,
208d1d35e2fSjeremylt                             CeedInt *num_output) {
209d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
210d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
211e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2127a982d89SJeremy L. Thompson }
2137a982d89SJeremy L. Thompson 
2147a982d89SJeremy L. Thompson /**
21543e1b16fSJeremy L Thompson   @brief Get the name of the user function for a CeedQFunction
2167a982d89SJeremy L. Thompson 
2177a982d89SJeremy L. Thompson   @param qf                CeedQFunction
21843e1b16fSJeremy L Thompson   @param[out] kernel_name  Variable to store source path string
2197a982d89SJeremy L. Thompson 
2207a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2217a982d89SJeremy L. Thompson 
2227a982d89SJeremy L. Thompson   @ref Backend
2237a982d89SJeremy L. Thompson **/
22443e1b16fSJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, char **kernel_name) {
22543e1b16fSJeremy L Thompson   *kernel_name = (char *) qf->kernel_name;
22643e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
22743e1b16fSJeremy L Thompson }
22843e1b16fSJeremy L Thompson 
22943e1b16fSJeremy L Thompson /**
23043e1b16fSJeremy L Thompson   @brief Get the source path string for a CeedQFunction
23143e1b16fSJeremy L Thompson 
23243e1b16fSJeremy L Thompson   @param qf                CeedQFunction
23343e1b16fSJeremy L Thompson   @param[out] source_path  Variable to store source path string
23443e1b16fSJeremy L Thompson 
23543e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
23643e1b16fSJeremy L Thompson 
23743e1b16fSJeremy L Thompson   @ref Backend
23843e1b16fSJeremy L Thompson **/
23943e1b16fSJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) {
24043e1b16fSJeremy L Thompson   *source_path = (char *) qf->source_path;
241e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2427a982d89SJeremy L. Thompson }
2437a982d89SJeremy L. Thompson 
2447a982d89SJeremy L. Thompson /**
2453d3250a0SJeremy L Thompson   @brief Initalize and load QFunction source file into string buffer, including
2463d3250a0SJeremy L Thompson            full text of local files in place of `#include "local.h"`.
2473d3250a0SJeremy L Thompson            The `buffer` is set to `NULL` if there is no QFunction source file.
2483d3250a0SJeremy L Thompson          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
2493d3250a0SJeremy L Thompson 
2503d3250a0SJeremy L Thompson   @param qf                  CeedQFunction
251f74ec584SJeremy L Thompson   @param[out] source_buffer  String buffer for source file contents
2523d3250a0SJeremy L Thompson 
2533d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2543d3250a0SJeremy L Thompson 
2553d3250a0SJeremy L Thompson   @ref Backend
2563d3250a0SJeremy L Thompson **/
2573d3250a0SJeremy L Thompson int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, char **source_buffer) {
2583d3250a0SJeremy L Thompson   int ierr;
2593d3250a0SJeremy L Thompson   char *source_path;
2603d3250a0SJeremy L Thompson 
2613d3250a0SJeremy L Thompson   ierr = CeedQFunctionGetSourcePath(qf, &source_path); CeedChk(ierr);
2623d3250a0SJeremy L Thompson   *source_buffer = NULL;
2633d3250a0SJeremy L Thompson   if (source_path) {
2643d3250a0SJeremy L Thompson     ierr = CeedLoadSourceToBuffer(qf->ceed, source_path, source_buffer);
2653d3250a0SJeremy L Thompson     CeedChk(ierr);
2663d3250a0SJeremy L Thompson   }
2673d3250a0SJeremy L Thompson 
2683d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2693d3250a0SJeremy L Thompson }
2703d3250a0SJeremy L Thompson 
2713d3250a0SJeremy L Thompson /**
2727a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2737a982d89SJeremy L. Thompson 
2747a982d89SJeremy L. Thompson   @param qf      CeedQFunction
2757a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
2767a982d89SJeremy L. Thompson 
2777a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2787a982d89SJeremy L. Thompson 
2797a982d89SJeremy L. Thompson   @ref Backend
2807a982d89SJeremy L. Thompson **/
2817a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
2827a982d89SJeremy L. Thompson   *f = qf->function;
283e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2847a982d89SJeremy L. Thompson }
2857a982d89SJeremy L. Thompson 
2867a982d89SJeremy L. Thompson /**
287777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
288777ff853SJeremy L Thompson            Note: For QFunctions from the Fortran interface, this
289777ff853SJeremy L Thompson              function will return the Fortran context
290777ff853SJeremy L Thompson              CeedQFunctionContext.
2917a982d89SJeremy L. Thompson 
2927a982d89SJeremy L. Thompson   @param qf        CeedQFunction
293777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2947a982d89SJeremy L. Thompson 
2957a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2967a982d89SJeremy L. Thompson 
2977a982d89SJeremy L. Thompson   @ref Backend
2987a982d89SJeremy L. Thompson **/
299777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
3007a982d89SJeremy L. Thompson   *ctx = qf->ctx;
301e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3027a982d89SJeremy L. Thompson }
3037a982d89SJeremy L. Thompson 
3047a982d89SJeremy L. Thompson /**
305*441428dfSJeremy L Thompson   @brief Get context data of a CeedQFunction
306*441428dfSJeremy L Thompson 
307*441428dfSJeremy L Thompson   @param qf         CeedQFunction
308*441428dfSJeremy L Thompson   @param mem_type   Memory type on which to access the data. If the backend
309*441428dfSJeremy L Thompson                       uses a different memory type, this will perform a copy.
310*441428dfSJeremy L Thompson   @param[out] data  Data on memory type mem_type
311*441428dfSJeremy L Thompson 
312*441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
313*441428dfSJeremy L Thompson 
314*441428dfSJeremy L Thompson   @ref Backend
315*441428dfSJeremy L Thompson **/
316*441428dfSJeremy L Thompson int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type,
317*441428dfSJeremy L Thompson                                 void *data) {
318*441428dfSJeremy L Thompson   int ierr;
319*441428dfSJeremy L Thompson   bool is_writable;
320*441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
321*441428dfSJeremy L Thompson 
322*441428dfSJeremy L Thompson   ierr = CeedQFunctionGetContext(qf, &ctx); CeedChk(ierr);
323*441428dfSJeremy L Thompson   if (ctx) {
324*441428dfSJeremy L Thompson     ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr);
325*441428dfSJeremy L Thompson     if (is_writable) {
326*441428dfSJeremy L Thompson       ierr = CeedQFunctionContextGetData(ctx, mem_type, data); CeedChk(ierr);
327*441428dfSJeremy L Thompson     } else {
328*441428dfSJeremy L Thompson       ierr = CeedQFunctionContextGetDataRead(ctx, mem_type, data); CeedChk(ierr);
329*441428dfSJeremy L Thompson     }
330*441428dfSJeremy L Thompson   } else {
331*441428dfSJeremy L Thompson     *(void **)data = NULL;
332*441428dfSJeremy L Thompson   }
333*441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
334*441428dfSJeremy L Thompson }
335*441428dfSJeremy L Thompson 
336*441428dfSJeremy L Thompson /**
337*441428dfSJeremy L Thompson   @brief Restore context data of a CeedQFunction
338*441428dfSJeremy L Thompson 
339*441428dfSJeremy L Thompson   @param qf    CeedQFunction
340*441428dfSJeremy L Thompson   @param data  Data to restore
341*441428dfSJeremy L Thompson 
342*441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
343*441428dfSJeremy L Thompson 
344*441428dfSJeremy L Thompson   @ref Backend
345*441428dfSJeremy L Thompson **/
346*441428dfSJeremy L Thompson int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) {
347*441428dfSJeremy L Thompson   int ierr;
348*441428dfSJeremy L Thompson   bool is_writable;
349*441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
350*441428dfSJeremy L Thompson 
351*441428dfSJeremy L Thompson   ierr = CeedQFunctionGetContext(qf, &ctx); CeedChk(ierr);
352*441428dfSJeremy L Thompson   if (ctx) {
353*441428dfSJeremy L Thompson     ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr);
354*441428dfSJeremy L Thompson     if (is_writable) {
355*441428dfSJeremy L Thompson       ierr = CeedQFunctionContextRestoreData(ctx, data); CeedChk(ierr);
356*441428dfSJeremy L Thompson     } else {
357*441428dfSJeremy L Thompson       ierr = CeedQFunctionContextRestoreDataRead(ctx, data); CeedChk(ierr);
358*441428dfSJeremy L Thompson     }
359*441428dfSJeremy L Thompson   }
360*441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
361*441428dfSJeremy L Thompson }
362*441428dfSJeremy L Thompson 
363*441428dfSJeremy L Thompson /**
3647a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
365777ff853SJeremy L Thompson            Note: For all QFunctions this function will return the user
366777ff853SJeremy L Thompson              CeedQFunctionContext and not interface context
367777ff853SJeremy L Thompson              CeedQFunctionContext, if any such object exists.
3687a982d89SJeremy L. Thompson 
3697a982d89SJeremy L. Thompson   @param qf        CeedQFunction
370777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
3717a982d89SJeremy L. Thompson 
3727a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3737a982d89SJeremy L. Thompson   @ref Backend
3747a982d89SJeremy L. Thompson **/
375777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
376777ff853SJeremy L Thompson   int ierr;
377f04ea552SJeremy L Thompson   if (qf->is_fortran) {
378d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
379d1d35e2fSjeremylt     ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx);
380777ff853SJeremy L Thompson     CeedChk(ierr);
3818cb0412aSJeremy L Thompson     *ctx = fortran_ctx->inner_ctx;
382d1d35e2fSjeremylt     ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx);
383d1d35e2fSjeremylt     CeedChk(ierr);
3847a982d89SJeremy L. Thompson   } else {
3857a982d89SJeremy L. Thompson     *ctx = qf->ctx;
3867a982d89SJeremy L. Thompson   }
387e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3887a982d89SJeremy L. Thompson }
3897a982d89SJeremy L. Thompson 
3907a982d89SJeremy L. Thompson /**
391*441428dfSJeremy L Thompson   @brief Get inner context data of a CeedQFunction
392*441428dfSJeremy L Thompson 
393*441428dfSJeremy L Thompson   @param qf         CeedQFunction
394*441428dfSJeremy L Thompson   @param mem_type   Memory type on which to access the data. If the backend
395*441428dfSJeremy L Thompson                       uses a different memory type, this will perform a copy.
396*441428dfSJeremy L Thompson   @param[out] data  Data on memory type mem_type
397*441428dfSJeremy L Thompson 
398*441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
399*441428dfSJeremy L Thompson 
400*441428dfSJeremy L Thompson   @ref Backend
401*441428dfSJeremy L Thompson **/
402*441428dfSJeremy L Thompson int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type,
403*441428dfSJeremy L Thompson                                      void *data) {
404*441428dfSJeremy L Thompson   int ierr;
405*441428dfSJeremy L Thompson   bool is_writable;
406*441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
407*441428dfSJeremy L Thompson 
408*441428dfSJeremy L Thompson   ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChk(ierr);
409*441428dfSJeremy L Thompson   if (ctx) {
410*441428dfSJeremy L Thompson     ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr);
411*441428dfSJeremy L Thompson     if (is_writable) {
412*441428dfSJeremy L Thompson       ierr = CeedQFunctionContextGetData(ctx, mem_type, data); CeedChk(ierr);
413*441428dfSJeremy L Thompson     } else {
414*441428dfSJeremy L Thompson       ierr = CeedQFunctionContextGetDataRead(ctx, mem_type, data); CeedChk(ierr);
415*441428dfSJeremy L Thompson     }
416*441428dfSJeremy L Thompson   } else {
417*441428dfSJeremy L Thompson     *(void **)data = NULL;
418*441428dfSJeremy L Thompson   }
419*441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
420*441428dfSJeremy L Thompson }
421*441428dfSJeremy L Thompson 
422*441428dfSJeremy L Thompson /**
423*441428dfSJeremy L Thompson   @brief Restore inner context data of a CeedQFunction
424*441428dfSJeremy L Thompson 
425*441428dfSJeremy L Thompson   @param qf    CeedQFunction
426*441428dfSJeremy L Thompson   @param data  Data to restore
427*441428dfSJeremy L Thompson 
428*441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
429*441428dfSJeremy L Thompson 
430*441428dfSJeremy L Thompson   @ref Backend
431*441428dfSJeremy L Thompson **/
432*441428dfSJeremy L Thompson int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) {
433*441428dfSJeremy L Thompson   int ierr;
434*441428dfSJeremy L Thompson   bool is_writable;
435*441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
436*441428dfSJeremy L Thompson 
437*441428dfSJeremy L Thompson   ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChk(ierr);
438*441428dfSJeremy L Thompson   if (ctx) {
439*441428dfSJeremy L Thompson     ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr);
440*441428dfSJeremy L Thompson     if (is_writable) {
441*441428dfSJeremy L Thompson       ierr = CeedQFunctionContextRestoreData(ctx, data); CeedChk(ierr);
442*441428dfSJeremy L Thompson     } else {
443*441428dfSJeremy L Thompson       ierr = CeedQFunctionContextRestoreDataRead(ctx, data); CeedChk(ierr);
444*441428dfSJeremy L Thompson     }
445*441428dfSJeremy L Thompson   }
446*441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
447*441428dfSJeremy L Thompson }
448*441428dfSJeremy L Thompson 
449*441428dfSJeremy L Thompson /**
4507a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
4517a982d89SJeremy L. Thompson 
4527a982d89SJeremy L. Thompson   @param qf                CeedQFunction
453d1d35e2fSjeremylt   @param[out] is_identity  Variable to store identity status
4547a982d89SJeremy L. Thompson 
4557a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4567a982d89SJeremy L. Thompson 
4577a982d89SJeremy L. Thompson   @ref Backend
4587a982d89SJeremy L. Thompson **/
459d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
460f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
461e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4627a982d89SJeremy L. Thompson }
4637a982d89SJeremy L. Thompson 
4647a982d89SJeremy L. Thompson /**
465*441428dfSJeremy L Thompson   @brief Determine if QFunctionContext is writable
466*441428dfSJeremy L Thompson 
467*441428dfSJeremy L Thompson   @param qf                CeedQFunction
468*441428dfSJeremy L Thompson   @param[out] is_writable  Variable to store context writeable staus
469*441428dfSJeremy L Thompson 
470*441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
471*441428dfSJeremy L Thompson 
472*441428dfSJeremy L Thompson   @ref Backend
473*441428dfSJeremy L Thompson **/
474*441428dfSJeremy L Thompson int CeedQFunctionIsContextWritable(CeedQFunction qf, bool *is_writable) {
475*441428dfSJeremy L Thompson   *is_writable = qf->is_context_writable;
476*441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
477*441428dfSJeremy L Thompson }
478*441428dfSJeremy L Thompson 
479*441428dfSJeremy L Thompson /**
4807a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
4817a982d89SJeremy L. Thompson 
4827a982d89SJeremy L. Thompson   @param qf         CeedQFunction
4837a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
4847a982d89SJeremy L. Thompson 
4857a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4867a982d89SJeremy L. Thompson 
4877a982d89SJeremy L. Thompson   @ref Backend
4887a982d89SJeremy L. Thompson **/
489777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
490777ff853SJeremy L Thompson   *(void **)data = qf->data;
491e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4927a982d89SJeremy L. Thompson }
4937a982d89SJeremy L. Thompson 
4947a982d89SJeremy L. Thompson /**
4957a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
4967a982d89SJeremy L. Thompson 
4977a982d89SJeremy L. Thompson   @param[out] qf  CeedQFunction
4987a982d89SJeremy L. Thompson   @param data     Data to set
4997a982d89SJeremy L. Thompson 
5007a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5017a982d89SJeremy L. Thompson 
5027a982d89SJeremy L. Thompson   @ref Backend
5037a982d89SJeremy L. Thompson **/
504777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
505777ff853SJeremy L Thompson   qf->data = data;
506e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5077a982d89SJeremy L. Thompson }
5087a982d89SJeremy L. Thompson 
5097a982d89SJeremy L. Thompson /**
51034359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
51134359f16Sjeremylt 
51234359f16Sjeremylt   @param qf  CeedQFunction to increment the reference counter
51334359f16Sjeremylt 
51434359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
51534359f16Sjeremylt 
51634359f16Sjeremylt   @ref Backend
51734359f16Sjeremylt **/
5189560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
51934359f16Sjeremylt   qf->ref_count++;
52034359f16Sjeremylt   return CEED_ERROR_SUCCESS;
52134359f16Sjeremylt }
52234359f16Sjeremylt 
5237a982d89SJeremy L. Thompson /// @}
5247a982d89SJeremy L. Thompson 
5257a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5267a982d89SJeremy L. Thompson /// CeedQFunction Public API
5277a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5287a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
5297a982d89SJeremy L. Thompson /// @{
5307a982d89SJeremy L. Thompson 
5317a982d89SJeremy L. Thompson /**
5327a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
5337a982d89SJeremy L. Thompson 
5347a982d89SJeremy L. Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
535d1d35e2fSjeremylt   @param vec_length  Vector length. Caller must ensure that number of quadrature
536d1d35e2fSjeremylt                        points is a multiple of vec_length.
5377a982d89SJeremy L. Thompson   @param f           Function pointer to evaluate action at quadrature points.
5387a982d89SJeremy L. Thompson                        See \ref CeedQFunctionUser.
5397a982d89SJeremy L. Thompson   @param source      Absolute path to source of QFunction,
540c8d5b03cSJeremy L Thompson                        "\abs_path\file.h:function_name".
541c8d5b03cSJeremy L Thompson                        For support across all backends, this source must only
542c8d5b03cSJeremy L Thompson                        contain constructs supported by C99, C++11, and CUDA.
5437a982d89SJeremy L. Thompson   @param[out] qf     Address of the variable where the newly created
5447a982d89SJeremy L. Thompson                        CeedQFunction will be stored
5457a982d89SJeremy L. Thompson 
5467a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5477a982d89SJeremy L. Thompson 
5487a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
5497a982d89SJeremy L. Thompson     arguments.
5507a982d89SJeremy L. Thompson 
5517a982d89SJeremy L. Thompson   @ref User
5527a982d89SJeremy L. Thompson **/
553d1d35e2fSjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length,
554d1d35e2fSjeremylt                                 CeedQFunctionUser f,
5557a982d89SJeremy L. Thompson                                 const char *source, CeedQFunction *qf) {
5567a982d89SJeremy L. Thompson   int ierr;
55743e1b16fSJeremy L Thompson   char *source_copy, *kernel_name_copy;
5587a982d89SJeremy L. Thompson 
5597a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
5607a982d89SJeremy L. Thompson     Ceed delegate;
5617a982d89SJeremy L. Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
5627a982d89SJeremy L. Thompson 
5637a982d89SJeremy L. Thompson     if (!delegate)
5647a982d89SJeremy L. Thompson       // LCOV_EXCL_START
565e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
566e15f9bd0SJeremy L Thompson                        "Backend does not support QFunctionCreate");
5677a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
5687a982d89SJeremy L. Thompson 
569d1d35e2fSjeremylt     ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf);
5707a982d89SJeremy L. Thompson     CeedChk(ierr);
571e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
5727a982d89SJeremy L. Thompson   }
5737a982d89SJeremy L. Thompson 
574edfb5f23SJeremy L Thompson   if (strlen(source) && !strrchr(source, ':'))
57543e1b16fSJeremy L Thompson     // LCOV_EXCL_START
57643e1b16fSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPLETE,
57743e1b16fSJeremy L Thompson                      "Provided path to source does not include function name. "
57843e1b16fSJeremy L Thompson                      "Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"",
57943e1b16fSJeremy L Thompson                      source);
58043e1b16fSJeremy L Thompson   // LCOV_EXCL_STOP
58143e1b16fSJeremy L Thompson 
5827a982d89SJeremy L. Thompson   ierr = CeedCalloc(1, qf); CeedChk(ierr);
5837a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
5849560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
585d1d35e2fSjeremylt   (*qf)->ref_count = 1;
586d1d35e2fSjeremylt   (*qf)->vec_length = vec_length;
587f04ea552SJeremy L Thompson   (*qf)->is_identity = false;
588*441428dfSJeremy L Thompson   (*qf)->is_context_writable = true;
5897a982d89SJeremy L. Thompson   (*qf)->function = f;
59043e1b16fSJeremy L Thompson   if (strlen(source)) {
59143e1b16fSJeremy L Thompson     const char *kernel_name = strrchr(source, ':') + 1;
59243e1b16fSJeremy L Thompson     size_t kernel_name_len = strlen(kernel_name);
59343e1b16fSJeremy L Thompson     ierr = CeedCalloc(kernel_name_len + 1, &kernel_name_copy); CeedChk(ierr);
59443e1b16fSJeremy L Thompson     strncpy(kernel_name_copy, kernel_name, kernel_name_len);
59543e1b16fSJeremy L Thompson     (*qf)->kernel_name = kernel_name_copy;
59643e1b16fSJeremy L Thompson 
59743e1b16fSJeremy L Thompson     size_t source_len = strlen(source) - kernel_name_len - 1;
59843e1b16fSJeremy L Thompson     ierr = CeedCalloc(source_len + 1, &source_copy); CeedChk(ierr);
59943e1b16fSJeremy L Thompson     strncpy(source_copy, source, source_len);
600d1d35e2fSjeremylt     (*qf)->source_path = source_copy;
60143e1b16fSJeremy L Thompson   }
602bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields); CeedChk(ierr);
603bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields); CeedChk(ierr);
6047a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
605e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6067a982d89SJeremy L. Thompson }
6077a982d89SJeremy L. Thompson 
6087a982d89SJeremy L. Thompson /**
609288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
610288c0443SJeremy L Thompson 
611288c0443SJeremy L Thompson   @param ceed     A Ceed object where the CeedQFunction will be created
612288c0443SJeremy L Thompson   @param name     Name of QFunction to use from gallery
613288c0443SJeremy L Thompson   @param[out] qf  Address of the variable where the newly created
614288c0443SJeremy L Thompson                     CeedQFunction will be stored
615288c0443SJeremy L Thompson 
616288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
617288c0443SJeremy L Thompson 
6187a982d89SJeremy L. Thompson   @ref User
619288c0443SJeremy L Thompson **/
620288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
621288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
622288c0443SJeremy L Thompson   int ierr;
623f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
624288c0443SJeremy L Thompson 
6251d013790SJed Brown   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
626288c0443SJeremy L Thompson   // Find matching backend
627e15f9bd0SJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE,
628e15f9bd0SJeremy L Thompson                                 "No QFunction name provided");
629288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
630288c0443SJeremy L Thompson     size_t n;
631d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
632d1d35e2fSjeremylt     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {}
633d1d35e2fSjeremylt     if (n > match_len) {
634d1d35e2fSjeremylt       match_len = n;
635f7e22acaSJeremy L Thompson       match_index = i;
636288c0443SJeremy L Thompson     }
637288c0443SJeremy L Thompson   }
638d1d35e2fSjeremylt   if (!match_len)
639138d4072Sjeremylt     // LCOV_EXCL_START
640e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
641138d4072Sjeremylt   // LCOV_EXCL_STOP
642288c0443SJeremy L Thompson 
643288c0443SJeremy L Thompson   // Create QFunction
644d1d35e2fSjeremylt   ierr = CeedQFunctionCreateInterior(ceed,
645f7e22acaSJeremy L Thompson                                      gallery_qfunctions[match_index].vec_length,
646f7e22acaSJeremy L Thompson                                      gallery_qfunctions[match_index].f,
647f7e22acaSJeremy L Thompson                                      gallery_qfunctions[match_index].source, qf);
648288c0443SJeremy L Thompson   CeedChk(ierr);
649288c0443SJeremy L Thompson 
650288c0443SJeremy L Thompson   // QFunction specific setup
651f7e22acaSJeremy L Thompson   ierr = gallery_qfunctions[match_index].init(ceed, name, *qf); CeedChk(ierr);
652288c0443SJeremy L Thompson 
65375affc3bSjeremylt   // Copy name
654f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name); CeedChk(ierr);
65543e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
656e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
657288c0443SJeremy L Thompson }
658288c0443SJeremy L Thompson 
659288c0443SJeremy L Thompson /**
6600219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
6610219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
6620219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
6630219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
6640219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
6650219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
6660219ea01SJeremy L Thompson            location for both.
6670219ea01SJeremy L Thompson 
6680219ea01SJeremy L Thompson   @param ceed          A Ceed object where the CeedQFunction will be created
669d1d35e2fSjeremylt   @param[in] size      Size of the QFunction fields
670d1d35e2fSjeremylt   @param[in] in_mode   CeedEvalMode for input to CeedQFunction
671d1d35e2fSjeremylt   @param[in] out_mode  CeedEvalMode for output to CeedQFunction
6720219ea01SJeremy L Thompson   @param[out] qf       Address of the variable where the newly created
6730219ea01SJeremy L Thompson                          CeedQFunction will be stored
6740219ea01SJeremy L Thompson 
6750219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
6760219ea01SJeremy L Thompson 
6777a982d89SJeremy L. Thompson   @ref User
6780219ea01SJeremy L Thompson **/
679d1d35e2fSjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode,
680d1d35e2fSjeremylt                                 CeedEvalMode out_mode, CeedQFunction *qf) {
6810219ea01SJeremy L Thompson   int ierr;
6820219ea01SJeremy L Thompson 
6830219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
684d1d35e2fSjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr);
685d1d35e2fSjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr);
6860219ea01SJeremy L Thompson 
687f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
688547dbd6fSJeremy L Thompson 
689777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
6903668ca4bSJeremy L Thompson   CeedContextFieldLabel size_label;
691547dbd6fSJeremy L Thompson   ierr = CeedQFunctionGetContext(*qf, &ctx); CeedChk(ierr);
6923668ca4bSJeremy L Thompson   ierr = CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label);
6933668ca4bSJeremy L Thompson   CeedChk(ierr);
6947bfe0f0eSJeremy L Thompson   ierr = CeedQFunctionContextSetInt32(ctx, size_label, &size); CeedChk(ierr);
695547dbd6fSJeremy L Thompson 
696e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6970219ea01SJeremy L Thompson }
6980219ea01SJeremy L Thompson 
6990219ea01SJeremy L Thompson /**
7009560d06aSjeremylt   @brief Copy the pointer to a CeedQFunction. Both pointers should
7019560d06aSjeremylt            be destroyed with `CeedQFunctionDestroy()`;
7029560d06aSjeremylt            Note: If `*qf_copy` is non-NULL, then it is assumed that
7039560d06aSjeremylt            `*qf_copy` is a pointer to a CeedQFunction. This
7049560d06aSjeremylt            CeedQFunction will be destroyed if `*qf_copy` is the only
7059560d06aSjeremylt            reference to this CeedQFunction.
7069560d06aSjeremylt 
7079560d06aSjeremylt   @param qf            CeedQFunction to copy reference to
7089560d06aSjeremylt   @param[out] qf_copy  Variable to store copied reference
7099560d06aSjeremylt 
7109560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7119560d06aSjeremylt 
7129560d06aSjeremylt   @ref User
7139560d06aSjeremylt **/
7149560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
7159560d06aSjeremylt   int ierr;
7169560d06aSjeremylt 
7179560d06aSjeremylt   ierr = CeedQFunctionReference(qf); CeedChk(ierr);
7189560d06aSjeremylt   ierr = CeedQFunctionDestroy(qf_copy); CeedChk(ierr);
7199560d06aSjeremylt   *qf_copy = qf;
7209560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7219560d06aSjeremylt }
7229560d06aSjeremylt 
7239560d06aSjeremylt /**
724a0a97fcfSJed Brown   @brief Add a CeedQFunction input
725b11c1e72Sjeremylt 
726b11c1e72Sjeremylt   @param qf          CeedQFunction
727d1d35e2fSjeremylt   @param field_name  Name of QFunction field
728d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
729d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
730d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
731b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
732b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
733b11c1e72Sjeremylt 
734b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
735dfdf5a53Sjeremylt 
7367a982d89SJeremy L. Thompson   @ref User
737b11c1e72Sjeremylt **/
738d1d35e2fSjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name,
739d1d35e2fSjeremylt                           CeedInt size,
740d1d35e2fSjeremylt                           CeedEvalMode eval_mode) {
741f04ea552SJeremy L Thompson   if (qf->is_immutable)
742e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
743e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
744f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
745e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
746d1d35e2fSjeremylt   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1))
747e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
748e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
749e15f9bd0SJeremy L Thompson                      "CEED_EVAL_WEIGHT should have size 1");
750e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
751d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields],
752d1d35e2fSjeremylt                                    field_name, size, eval_mode);
753fe2413ffSjeremylt   CeedChk(ierr);
754d1d35e2fSjeremylt   qf->num_input_fields++;
755e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
756d7b241e6Sjeremylt }
757d7b241e6Sjeremylt 
758b11c1e72Sjeremylt /**
759a0a97fcfSJed Brown   @brief Add a CeedQFunction output
760b11c1e72Sjeremylt 
761b11c1e72Sjeremylt   @param qf          CeedQFunction
762d1d35e2fSjeremylt   @param field_name  Name of QFunction field
763d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
764d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
765d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
766b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
767b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
768b11c1e72Sjeremylt 
769b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
770dfdf5a53Sjeremylt 
7717a982d89SJeremy L. Thompson   @ref User
772b11c1e72Sjeremylt **/
773d1d35e2fSjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name,
774d1d35e2fSjeremylt                            CeedInt size, CeedEvalMode eval_mode) {
775f04ea552SJeremy L Thompson   if (qf->is_immutable)
776e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
777e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
778f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
779e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
780d1d35e2fSjeremylt   if (eval_mode == CEED_EVAL_WEIGHT)
781c042f62fSJeremy L Thompson     // LCOV_EXCL_START
782e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
783e15f9bd0SJeremy L Thompson                      "Cannot create QFunction output with "
7841d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
785c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
786d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields],
787d1d35e2fSjeremylt                                    field_name, size, eval_mode);
788fe2413ffSjeremylt   CeedChk(ierr);
789d1d35e2fSjeremylt   qf->num_output_fields++;
790e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
791d7b241e6Sjeremylt }
792d7b241e6Sjeremylt 
793dfdf5a53Sjeremylt /**
79443bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
79543bbe138SJeremy L Thompson 
796f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
797f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
798f04ea552SJeremy L Thompson 
79943bbe138SJeremy L Thompson   @param qf                      CeedQFunction
800f74ec584SJeremy L Thompson   @param[out] num_input_fields   Variable to store number of input fields
801f74ec584SJeremy L Thompson   @param[out] input_fields       Variable to store input fields
802f74ec584SJeremy L Thompson   @param[out] num_output_fields  Variable to store number of output fields
803f74ec584SJeremy L Thompson   @param[out] output_fields      Variable to store output fields
80443bbe138SJeremy L Thompson 
80543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
80643bbe138SJeremy L Thompson 
807e9b533fbSJeremy L Thompson   @ref Advanced
80843bbe138SJeremy L Thompson **/
80943bbe138SJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields,
81043bbe138SJeremy L Thompson                            CeedQFunctionField **input_fields,
81143bbe138SJeremy L Thompson                            CeedInt *num_output_fields,
81243bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
813f04ea552SJeremy L Thompson   qf->is_immutable = true;
81443bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
81543bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
81643bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
81743bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
81843bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
81943bbe138SJeremy L Thompson }
82043bbe138SJeremy L Thompson 
82143bbe138SJeremy L Thompson /**
82243bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
82343bbe138SJeremy L Thompson 
82443bbe138SJeremy L Thompson   @param qf_field         CeedQFunctionField
82543bbe138SJeremy L Thompson   @param[out] field_name  Variable to store the field name
82643bbe138SJeremy L Thompson 
82743bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
82843bbe138SJeremy L Thompson 
829e9b533fbSJeremy L Thompson   @ref Advanced
83043bbe138SJeremy L Thompson **/
83143bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
83243bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
83343bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
83443bbe138SJeremy L Thompson }
83543bbe138SJeremy L Thompson 
83643bbe138SJeremy L Thompson /**
83743bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
83843bbe138SJeremy L Thompson 
83943bbe138SJeremy L Thompson   @param qf_field   CeedQFunctionField
84043bbe138SJeremy L Thompson   @param[out] size  Variable to store the size of the field
84143bbe138SJeremy L Thompson 
84243bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
84343bbe138SJeremy L Thompson 
844e9b533fbSJeremy L Thompson   @ref Advanced
84543bbe138SJeremy L Thompson **/
84643bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
84743bbe138SJeremy L Thompson   *size = qf_field->size;
84843bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
84943bbe138SJeremy L Thompson }
85043bbe138SJeremy L Thompson 
85143bbe138SJeremy L Thompson /**
85243bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
85343bbe138SJeremy L Thompson 
85443bbe138SJeremy L Thompson   @param qf_field        CeedQFunctionField
85543bbe138SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
85643bbe138SJeremy L Thompson 
85743bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
85843bbe138SJeremy L Thompson 
859e9b533fbSJeremy L Thompson   @ref Advanced
86043bbe138SJeremy L Thompson **/
86143bbe138SJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field,
86243bbe138SJeremy L Thompson                                   CeedEvalMode *eval_mode) {
86343bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
86443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
86543bbe138SJeremy L Thompson }
86643bbe138SJeremy L Thompson 
86743bbe138SJeremy L Thompson /**
8684ce2993fSjeremylt   @brief Set global context for a CeedQFunction
869b11c1e72Sjeremylt 
870b11c1e72Sjeremylt   @param qf   CeedQFunction
871b11c1e72Sjeremylt   @param ctx  Context data to set
872b11c1e72Sjeremylt 
873b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
874dfdf5a53Sjeremylt 
8757a982d89SJeremy L. Thompson   @ref User
876b11c1e72Sjeremylt **/
877777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
87834359f16Sjeremylt   int ierr;
8791389e7feSJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&qf->ctx); CeedChk(ierr);
880d7b241e6Sjeremylt   qf->ctx = ctx;
8819560d06aSjeremylt   ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr);
882e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
883d7b241e6Sjeremylt }
884d7b241e6Sjeremylt 
885b11c1e72Sjeremylt /**
886*441428dfSJeremy L Thompson   @brief Set writability of CeedQFunctionContext when calling the `CeedQFunctionUser`.
887*441428dfSJeremy L Thompson            The default value is 'is_writable == true'.
888*441428dfSJeremy L Thompson 
889*441428dfSJeremy L Thompson            Setting `is_writable == true` indicates the `CeedQFunctionUser` writes
890*441428dfSJeremy L Thompson            into the CeedQFunctionContextData and requires memory syncronization
891*441428dfSJeremy L Thompson            after calling `CeedQFunctionApply()`.
892*441428dfSJeremy L Thompson 
893*441428dfSJeremy L Thompson            Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not
894*441428dfSJeremy L Thompson            modify the CeedQFunctionContextData. Violating this assertion may lead
895*441428dfSJeremy L Thompson            to inconsistent data.
896*441428dfSJeremy L Thompson 
897*441428dfSJeremy L Thompson            Setting `is_writable == false` may offer a performance improvement on GPU backends.
898*441428dfSJeremy L Thompson 
899*441428dfSJeremy L Thompson   @param qf           CeedQFunction
900*441428dfSJeremy L Thompson   @param is_writable  Writability status
901*441428dfSJeremy L Thompson 
902*441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
903*441428dfSJeremy L Thompson 
904*441428dfSJeremy L Thompson   @ref User
905*441428dfSJeremy L Thompson **/
906*441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
907*441428dfSJeremy L Thompson   qf->is_context_writable = is_writable;
908*441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
909*441428dfSJeremy L Thompson }
910*441428dfSJeremy L Thompson 
911*441428dfSJeremy L Thompson /**
91275affc3bSjeremylt   @brief View a CeedQFunction
91375affc3bSjeremylt 
91475affc3bSjeremylt   @param[in] qf      CeedQFunction to view
91575affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
91675affc3bSjeremylt 
91775affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
91875affc3bSjeremylt 
9197a982d89SJeremy L. Thompson   @ref User
92075affc3bSjeremylt **/
92175affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
92275affc3bSjeremylt   int ierr;
92375affc3bSjeremylt 
92475affc3bSjeremylt   fprintf(stream, "%sCeedQFunction %s\n",
92543e1b16fSJeremy L Thompson           qf->is_gallery ? "Gallery " : "User ",
92643e1b16fSJeremy L Thompson           qf->is_gallery ? qf->gallery_name : qf->kernel_name);
92775affc3bSjeremylt 
928d1d35e2fSjeremylt   fprintf(stream, "  %d Input Field%s:\n", qf->num_input_fields,
929d1d35e2fSjeremylt           qf->num_input_fields>1 ? "s" : "");
930d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_input_fields; i++) {
931d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream);
9322da88da5Sjeremylt     CeedChk(ierr);
93375affc3bSjeremylt   }
93475affc3bSjeremylt 
935d1d35e2fSjeremylt   fprintf(stream, "  %d Output Field%s:\n", qf->num_output_fields,
936d1d35e2fSjeremylt           qf->num_output_fields>1 ? "s" : "");
937d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_output_fields; i++) {
938d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream);
93975affc3bSjeremylt     CeedChk(ierr);
94075affc3bSjeremylt   }
941e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
94275affc3bSjeremylt }
94375affc3bSjeremylt 
94475affc3bSjeremylt /**
945b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunction
946b7c9bbdaSJeremy L Thompson 
947b7c9bbdaSJeremy L Thompson   @param qf              CeedQFunction
948b7c9bbdaSJeremy L Thompson   @param[out] ceed       Variable to store Ceed
949b7c9bbdaSJeremy L Thompson 
950b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
951b7c9bbdaSJeremy L Thompson 
952b7c9bbdaSJeremy L Thompson   @ref Advanced
953b7c9bbdaSJeremy L Thompson **/
954b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
955b7c9bbdaSJeremy L Thompson   *ceed = qf->ceed;
956b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
957b7c9bbdaSJeremy L Thompson }
958b7c9bbdaSJeremy L Thompson 
959b7c9bbdaSJeremy L Thompson /**
960b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
961b11c1e72Sjeremylt 
962f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
963f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
964f04ea552SJeremy L Thompson 
965b11c1e72Sjeremylt   @param qf      CeedQFunction
966b11c1e72Sjeremylt   @param Q       Number of quadrature points
96734138859Sjeremylt   @param[in] u   Array of input CeedVectors
96834138859Sjeremylt   @param[out] v  Array of output CeedVectors
969b11c1e72Sjeremylt 
970b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
971dfdf5a53Sjeremylt 
9727a982d89SJeremy L. Thompson   @ref User
973b11c1e72Sjeremylt **/
974d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
975aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
976d7b241e6Sjeremylt   int ierr;
977d7b241e6Sjeremylt   if (!qf->Apply)
978c042f62fSJeremy L Thompson     // LCOV_EXCL_START
979e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED,
980e15f9bd0SJeremy L Thompson                      "Backend does not support QFunctionApply");
981c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
982d1d35e2fSjeremylt   if (Q % qf->vec_length)
983c042f62fSJeremy L Thompson     // LCOV_EXCL_START
984e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
985e15f9bd0SJeremy L Thompson                      "Number of quadrature points %d must be a "
986d1d35e2fSjeremylt                      "multiple of %d", Q, qf->vec_length);
987c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
988f04ea552SJeremy L Thompson   qf->is_immutable = true;
989d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
990e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
991d7b241e6Sjeremylt }
992d7b241e6Sjeremylt 
993b11c1e72Sjeremylt /**
994b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
995b11c1e72Sjeremylt 
996b11c1e72Sjeremylt   @param qf  CeedQFunction to destroy
997b11c1e72Sjeremylt 
998b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
999dfdf5a53Sjeremylt 
10007a982d89SJeremy L. Thompson   @ref User
1001b11c1e72Sjeremylt **/
1002d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
1003d7b241e6Sjeremylt   int ierr;
1004d7b241e6Sjeremylt 
1005d1d35e2fSjeremylt   if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS;
1006fe2413ffSjeremylt   // Backend destroy
1007d7b241e6Sjeremylt   if ((*qf)->Destroy) {
1008d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
1009d7b241e6Sjeremylt   }
1010fe2413ffSjeremylt   // Free fields
1011d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_input_fields; i++) {
1012d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr);
1013d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr);
1014fe2413ffSjeremylt   }
1015d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_output_fields; i++) {
1016d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr);
1017d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr);
1018fe2413ffSjeremylt   }
1019d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr);
1020d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr);
1021777ff853SJeremy L Thompson 
1022777ff853SJeremy L Thompson   // User context data object
1023777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
1024fe2413ffSjeremylt 
1025d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr);
102643e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->gallery_name); CeedChk(ierr);
102743e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->kernel_name); CeedChk(ierr);
1028d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
1029d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
1030e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1031d7b241e6Sjeremylt }
1032d7b241e6Sjeremylt 
1033d7b241e6Sjeremylt /// @}
1034