xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision 9d36ca5098a5cdf0ec02c0eb5de06d03bc121296)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3d7b241e6Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5d7b241e6Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7d7b241e6Sjeremylt 
8ec3da8bcSJed Brown #include <ceed/ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
103d3250a0SJeremy L Thompson #include <ceed/jit-tools.h>
113d576824SJeremy L Thompson #include <ceed-impl.h>
12288c0443SJeremy L Thompson #include <limits.h>
133d576824SJeremy L Thompson #include <stdbool.h>
143d576824SJeremy L Thompson #include <stdio.h>
1543e1b16fSJeremy L Thompson #include <stdlib.h>
163d576824SJeremy L Thompson #include <string.h>
17288c0443SJeremy L Thompson 
187a982d89SJeremy L. Thompson /// @file
197a982d89SJeremy L. Thompson /// Implementation of public CeedQFunction interfaces
207a982d89SJeremy L. Thompson 
21288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP
22442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none;
23442e7f0bSjeremylt /// @endcond
24442e7f0bSjeremylt 
257a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
267a982d89SJeremy L. Thompson /// @{
277a982d89SJeremy L. Thompson 
287a982d89SJeremy L. Thompson // Indicate that no QFunction is provided by the user
297a982d89SJeremy L. Thompson const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
307a982d89SJeremy L. Thompson 
317a982d89SJeremy L. Thompson /// @}
327a982d89SJeremy L. Thompson 
33442e7f0bSjeremylt /// @cond DOXYGEN_SKIP
34288c0443SJeremy L Thompson static struct {
35288c0443SJeremy L Thompson   char name[CEED_MAX_RESOURCE_LEN];
36288c0443SJeremy L Thompson   char source[CEED_MAX_RESOURCE_LEN];
37d1d35e2fSjeremylt   CeedInt vec_length;
38288c0443SJeremy L Thompson   CeedQFunctionUser f;
39288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
40d1d35e2fSjeremylt } gallery_qfunctions[1024];
41288c0443SJeremy L Thompson static size_t num_qfunctions;
42288c0443SJeremy L Thompson /// @endcond
43d7b241e6Sjeremylt 
44777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
45777ff853SJeremy L Thompson /// CeedQFunction Library Internal Functions
46777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
47777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper
48777ff853SJeremy L Thompson /// @{
49777ff853SJeremy L Thompson 
50b11c1e72Sjeremylt /**
51288c0443SJeremy L Thompson   @brief Register a gallery QFunction
52288c0443SJeremy L Thompson 
53288c0443SJeremy L Thompson   @param name        Name for this backend to respond to
541176cc3aSJeremy L Thompson   @param source      Absolute path to source of QFunction,
551176cc3aSJeremy L Thompson                        "\path\CEED_DIR\gallery\folder\file.h:function_name"
56d1d35e2fSjeremylt   @param vec_length  Vector length.  Caller must ensure that number of quadrature
57d1d35e2fSjeremylt                        points is a multiple of vec_length.
58288c0443SJeremy L Thompson   @param f           Function pointer to evaluate action at quadrature points.
59288c0443SJeremy L Thompson                        See \ref CeedQFunctionUser.
60288c0443SJeremy L Thompson   @param init        Initialization function called by CeedQFunctionInit() when the
61288c0443SJeremy L Thompson                        QFunction is selected.
62288c0443SJeremy L Thompson 
63288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
64288c0443SJeremy L Thompson 
657a982d89SJeremy L. Thompson   @ref Developer
66288c0443SJeremy L Thompson **/
67288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source,
68d1d35e2fSjeremylt                           CeedInt vec_length, CeedQFunctionUser f,
69288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
70d1d35e2fSjeremylt   if (num_qfunctions >= sizeof(gallery_qfunctions) / sizeof(
71d1d35e2fSjeremylt         gallery_qfunctions[0]))
72c042f62fSJeremy L Thompson     // LCOV_EXCL_START
73e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
74c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
75c042f62fSJeremy L Thompson 
768ccf1006SJeremy L Thompson   CeedDebugEnv("Gallery Register: %s", name);
778ccf1006SJeremy L Thompson 
78d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
79d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
80d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].source, source,
81d1d35e2fSjeremylt           CEED_MAX_RESOURCE_LEN);
82d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
83d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].vec_length = vec_length;
84d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].f = f;
85d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].init = init;
86288c0443SJeremy L Thompson   num_qfunctions++;
87e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
88288c0443SJeremy L Thompson }
89288c0443SJeremy L Thompson 
90288c0443SJeremy L Thompson /**
917a982d89SJeremy L. Thompson   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
927a982d89SJeremy L. Thompson 
937a982d89SJeremy L. Thompson   @param f           CeedQFunctionField
94d1d35e2fSjeremylt   @param field_name  Name of QFunction field
95d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
96d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT
97d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
987a982d89SJeremy L. Thompson                        \ref CEED_EVAL_INTERP to use interpolated values,
997a982d89SJeremy L. Thompson                        \ref CEED_EVAL_GRAD to use gradients,
1007a982d89SJeremy L. Thompson                        \ref CEED_EVAL_WEIGHT to use quadrature weights.
1017a982d89SJeremy L. Thompson 
1027a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1037a982d89SJeremy L. Thompson 
1047a982d89SJeremy L. Thompson   @ref Developer
1057a982d89SJeremy L. Thompson **/
106d1d35e2fSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name,
107d1d35e2fSjeremylt                                  CeedInt size, CeedEvalMode eval_mode) {
108cdf32b93SJeremy L Thompson   int ierr;
1097a982d89SJeremy L. Thompson 
110e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, f); CeedChk(ierr);
111f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(field_name, (char **)&(*f)->field_name);
112f7e22acaSJeremy L Thompson   CeedChk(ierr);
1137a982d89SJeremy L. Thompson   (*f)->size = size;
114d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
115e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1167a982d89SJeremy L. Thompson }
1177a982d89SJeremy L. Thompson 
1187a982d89SJeremy L. Thompson /**
1197a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1207a982d89SJeremy L. Thompson 
1217a982d89SJeremy L. Thompson   @param[in] field         QFunction field to view
122d1d35e2fSjeremylt   @param[in] field_number  Number of field being viewed
1234c4400c7SValeria Barra   @param[in] in            true for input field, false for output
1247a982d89SJeremy L. Thompson   @param[in] stream        Stream to view to, e.g., stdout
1257a982d89SJeremy L. Thompson 
1267a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1277a982d89SJeremy L. Thompson 
1287a982d89SJeremy L. Thompson   @ref Utility
1297a982d89SJeremy L. Thompson **/
130d1d35e2fSjeremylt static int CeedQFunctionFieldView(CeedQFunctionField field,
131d1d35e2fSjeremylt                                   CeedInt field_number,
1327a982d89SJeremy L. Thompson                                   bool in, FILE *stream) {
1338229195eSjeremylt   int ierr;
1347a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1358229195eSjeremylt   char *field_name;
1368229195eSjeremylt   ierr = CeedQFunctionFieldGetName(field, &field_name); CeedChk(ierr);
1378229195eSjeremylt   CeedInt size;
1388229195eSjeremylt   ierr = CeedQFunctionFieldGetSize(field, &size); CeedChk(ierr);
1398229195eSjeremylt   CeedEvalMode eval_mode;
1408229195eSjeremylt   ierr = CeedQFunctionFieldGetEvalMode(field, &eval_mode); CeedChk(ierr);
1417a982d89SJeremy L. Thompson   fprintf(stream, "    %s Field [%d]:\n"
1427a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1437a982d89SJeremy L. Thompson           "      Size: %d\n"
1447a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1458229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
146e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1477a982d89SJeremy L. Thompson }
1487a982d89SJeremy L. Thompson 
149777ff853SJeremy L Thompson /**
150777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
151777ff853SJeremy L Thompson 
152777ff853SJeremy L Thompson   @param qf      CeedQFunction
153777ff853SJeremy L Thompson   @param status  Boolean value to set as Fortran status
154777ff853SJeremy L Thompson 
155777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
156777ff853SJeremy L Thompson 
157777ff853SJeremy L Thompson   @ref Backend
158777ff853SJeremy L Thompson **/
159777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
160f04ea552SJeremy L Thompson   qf->is_fortran = status;
161e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
162777ff853SJeremy L Thompson }
163777ff853SJeremy L Thompson 
1647a982d89SJeremy L. Thompson /// @}
1657a982d89SJeremy L. Thompson 
1667a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1677a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1687a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1697a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1707a982d89SJeremy L. Thompson /// @{
1717a982d89SJeremy L. Thompson 
1727a982d89SJeremy L. Thompson /**
1737a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1747a982d89SJeremy L. Thompson 
1757a982d89SJeremy L. Thompson   @param qf               CeedQFunction
176d1d35e2fSjeremylt   @param[out] vec_length  Variable to store vector length
1777a982d89SJeremy L. Thompson 
1787a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1797a982d89SJeremy L. Thompson 
1807a982d89SJeremy L. Thompson   @ref Backend
1817a982d89SJeremy L. Thompson **/
182d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
183d1d35e2fSjeremylt   *vec_length = qf->vec_length;
184e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1857a982d89SJeremy L. Thompson }
1867a982d89SJeremy L. Thompson 
1877a982d89SJeremy L. Thompson /**
1887a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
1897a982d89SJeremy L. Thompson 
1907a982d89SJeremy L. Thompson   @param qf               CeedQFunction
191d1d35e2fSjeremylt   @param[out] num_input   Variable to store number of input fields
192d1d35e2fSjeremylt   @param[out] num_output  Variable to store number of output fields
1937a982d89SJeremy L. Thompson 
1947a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1957a982d89SJeremy L. Thompson 
1967a982d89SJeremy L. Thompson   @ref Backend
1977a982d89SJeremy L. Thompson **/
198d1d35e2fSjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input,
199d1d35e2fSjeremylt                             CeedInt *num_output) {
200d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
201d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
202e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2037a982d89SJeremy L. Thompson }
2047a982d89SJeremy L. Thompson 
2057a982d89SJeremy L. Thompson /**
20643e1b16fSJeremy L Thompson   @brief Get the name of the user function for a CeedQFunction
2077a982d89SJeremy L. Thompson 
2087a982d89SJeremy L. Thompson   @param qf                CeedQFunction
20943e1b16fSJeremy L Thompson   @param[out] kernel_name  Variable to store source path string
2107a982d89SJeremy L. Thompson 
2117a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2127a982d89SJeremy L. Thompson 
2137a982d89SJeremy L. Thompson   @ref Backend
2147a982d89SJeremy L. Thompson **/
21543e1b16fSJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, char **kernel_name) {
21643e1b16fSJeremy L Thompson   *kernel_name = (char *) qf->kernel_name;
21743e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
21843e1b16fSJeremy L Thompson }
21943e1b16fSJeremy L Thompson 
22043e1b16fSJeremy L Thompson /**
22143e1b16fSJeremy L Thompson   @brief Get the source path string for a CeedQFunction
22243e1b16fSJeremy L Thompson 
22343e1b16fSJeremy L Thompson   @param qf                CeedQFunction
22443e1b16fSJeremy L Thompson   @param[out] source_path  Variable to store source path string
22543e1b16fSJeremy L Thompson 
22643e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
22743e1b16fSJeremy L Thompson 
22843e1b16fSJeremy L Thompson   @ref Backend
22943e1b16fSJeremy L Thompson **/
23043e1b16fSJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) {
23143e1b16fSJeremy L Thompson   *source_path = (char *) qf->source_path;
232e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2337a982d89SJeremy L. Thompson }
2347a982d89SJeremy L. Thompson 
2357a982d89SJeremy L. Thompson /**
2363d3250a0SJeremy L Thompson   @brief Initalize and load QFunction source file into string buffer, including
2373d3250a0SJeremy L Thompson            full text of local files in place of `#include "local.h"`.
2383d3250a0SJeremy L Thompson            The `buffer` is set to `NULL` if there is no QFunction source file.
2393d3250a0SJeremy L Thompson          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
2403d3250a0SJeremy L Thompson 
2413d3250a0SJeremy L Thompson   @param qf                  CeedQFunction
242f74ec584SJeremy L Thompson   @param[out] source_buffer  String buffer for source file contents
2433d3250a0SJeremy L Thompson 
2443d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2453d3250a0SJeremy L Thompson 
2463d3250a0SJeremy L Thompson   @ref Backend
2473d3250a0SJeremy L Thompson **/
2483d3250a0SJeremy L Thompson int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, char **source_buffer) {
2493d3250a0SJeremy L Thompson   int ierr;
2503d3250a0SJeremy L Thompson   char *source_path;
2513d3250a0SJeremy L Thompson 
2523d3250a0SJeremy L Thompson   ierr = CeedQFunctionGetSourcePath(qf, &source_path); CeedChk(ierr);
2533d3250a0SJeremy L Thompson   *source_buffer = NULL;
2543d3250a0SJeremy L Thompson   if (source_path) {
2553d3250a0SJeremy L Thompson     ierr = CeedLoadSourceToBuffer(qf->ceed, source_path, source_buffer);
2563d3250a0SJeremy L Thompson     CeedChk(ierr);
2573d3250a0SJeremy L Thompson   }
2583d3250a0SJeremy L Thompson 
2593d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2603d3250a0SJeremy L Thompson }
2613d3250a0SJeremy L Thompson 
2623d3250a0SJeremy L Thompson /**
2637a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2647a982d89SJeremy L. Thompson 
2657a982d89SJeremy L. Thompson   @param qf      CeedQFunction
2667a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
2677a982d89SJeremy L. Thompson 
2687a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2697a982d89SJeremy L. Thompson 
2707a982d89SJeremy L. Thompson   @ref Backend
2717a982d89SJeremy L. Thompson **/
2727a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
2737a982d89SJeremy L. Thompson   *f = qf->function;
274e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2757a982d89SJeremy L. Thompson }
2767a982d89SJeremy L. Thompson 
2777a982d89SJeremy L. Thompson /**
278777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
279777ff853SJeremy L Thompson            Note: For QFunctions from the Fortran interface, this
280777ff853SJeremy L Thompson              function will return the Fortran context
281777ff853SJeremy L Thompson              CeedQFunctionContext.
2827a982d89SJeremy L. Thompson 
2837a982d89SJeremy L. Thompson   @param qf        CeedQFunction
284777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2857a982d89SJeremy L. Thompson 
2867a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2877a982d89SJeremy L. Thompson 
2887a982d89SJeremy L. Thompson   @ref Backend
2897a982d89SJeremy L. Thompson **/
290777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
2917a982d89SJeremy L. Thompson   *ctx = qf->ctx;
292e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2937a982d89SJeremy L. Thompson }
2947a982d89SJeremy L. Thompson 
2957a982d89SJeremy L. Thompson /**
296441428dfSJeremy L Thompson   @brief Get context data of a CeedQFunction
297441428dfSJeremy L Thompson 
298441428dfSJeremy L Thompson   @param qf         CeedQFunction
299441428dfSJeremy L Thompson   @param mem_type   Memory type on which to access the data. If the backend
300441428dfSJeremy L Thompson                       uses a different memory type, this will perform a copy.
301441428dfSJeremy L Thompson   @param[out] data  Data on memory type mem_type
302441428dfSJeremy L Thompson 
303441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
304441428dfSJeremy L Thompson 
305441428dfSJeremy L Thompson   @ref Backend
306441428dfSJeremy L Thompson **/
307441428dfSJeremy L Thompson int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type,
308441428dfSJeremy L Thompson                                 void *data) {
309441428dfSJeremy L Thompson   int ierr;
310441428dfSJeremy L Thompson   bool is_writable;
311441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
312441428dfSJeremy L Thompson 
313441428dfSJeremy L Thompson   ierr = CeedQFunctionGetContext(qf, &ctx); CeedChk(ierr);
314441428dfSJeremy L Thompson   if (ctx) {
315441428dfSJeremy L Thompson     ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr);
316441428dfSJeremy L Thompson     if (is_writable) {
317441428dfSJeremy L Thompson       ierr = CeedQFunctionContextGetData(ctx, mem_type, data); CeedChk(ierr);
318441428dfSJeremy L Thompson     } else {
319441428dfSJeremy L Thompson       ierr = CeedQFunctionContextGetDataRead(ctx, mem_type, data); CeedChk(ierr);
320441428dfSJeremy L Thompson     }
321441428dfSJeremy L Thompson   } else {
322441428dfSJeremy L Thompson     *(void **)data = NULL;
323441428dfSJeremy L Thompson   }
324441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
325441428dfSJeremy L Thompson }
326441428dfSJeremy L Thompson 
327441428dfSJeremy L Thompson /**
328441428dfSJeremy L Thompson   @brief Restore context data of a CeedQFunction
329441428dfSJeremy L Thompson 
330441428dfSJeremy L Thompson   @param qf    CeedQFunction
331441428dfSJeremy L Thompson   @param data  Data to restore
332441428dfSJeremy L Thompson 
333441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
334441428dfSJeremy L Thompson 
335441428dfSJeremy L Thompson   @ref Backend
336441428dfSJeremy L Thompson **/
337441428dfSJeremy L Thompson int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) {
338441428dfSJeremy L Thompson   int ierr;
339441428dfSJeremy L Thompson   bool is_writable;
340441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
341441428dfSJeremy L Thompson 
342441428dfSJeremy L Thompson   ierr = CeedQFunctionGetContext(qf, &ctx); CeedChk(ierr);
343441428dfSJeremy L Thompson   if (ctx) {
344441428dfSJeremy L Thompson     ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr);
345441428dfSJeremy L Thompson     if (is_writable) {
346441428dfSJeremy L Thompson       ierr = CeedQFunctionContextRestoreData(ctx, data); CeedChk(ierr);
347441428dfSJeremy L Thompson     } else {
348441428dfSJeremy L Thompson       ierr = CeedQFunctionContextRestoreDataRead(ctx, data); CeedChk(ierr);
349441428dfSJeremy L Thompson     }
350441428dfSJeremy L Thompson   }
351441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
352441428dfSJeremy L Thompson }
353441428dfSJeremy L Thompson 
354441428dfSJeremy L Thompson /**
3557a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
356777ff853SJeremy L Thompson            Note: For all QFunctions this function will return the user
357777ff853SJeremy L Thompson              CeedQFunctionContext and not interface context
358777ff853SJeremy L Thompson              CeedQFunctionContext, if any such object exists.
3597a982d89SJeremy L. Thompson 
3607a982d89SJeremy L. Thompson   @param qf        CeedQFunction
361777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
3627a982d89SJeremy L. Thompson 
3637a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3647a982d89SJeremy L. Thompson   @ref Backend
3657a982d89SJeremy L. Thompson **/
366777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
367777ff853SJeremy L Thompson   int ierr;
368f04ea552SJeremy L Thompson   if (qf->is_fortran) {
369d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
370d1d35e2fSjeremylt     ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx);
371777ff853SJeremy L Thompson     CeedChk(ierr);
3728cb0412aSJeremy L Thompson     *ctx = fortran_ctx->inner_ctx;
373d1d35e2fSjeremylt     ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx);
374d1d35e2fSjeremylt     CeedChk(ierr);
3757a982d89SJeremy L. Thompson   } else {
3767a982d89SJeremy L. Thompson     *ctx = qf->ctx;
3777a982d89SJeremy L. Thompson   }
378e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3797a982d89SJeremy L. Thompson }
3807a982d89SJeremy L. Thompson 
3817a982d89SJeremy L. Thompson /**
382441428dfSJeremy L Thompson   @brief Get inner context data of a CeedQFunction
383441428dfSJeremy L Thompson 
384441428dfSJeremy L Thompson   @param qf         CeedQFunction
385441428dfSJeremy L Thompson   @param mem_type   Memory type on which to access the data. If the backend
386441428dfSJeremy L Thompson                       uses a different memory type, this will perform a copy.
387441428dfSJeremy L Thompson   @param[out] data  Data on memory type mem_type
388441428dfSJeremy L Thompson 
389441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
390441428dfSJeremy L Thompson 
391441428dfSJeremy L Thompson   @ref Backend
392441428dfSJeremy L Thompson **/
393441428dfSJeremy L Thompson int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type,
394441428dfSJeremy L Thompson                                      void *data) {
395441428dfSJeremy L Thompson   int ierr;
396441428dfSJeremy L Thompson   bool is_writable;
397441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
398441428dfSJeremy L Thompson 
399441428dfSJeremy L Thompson   ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChk(ierr);
400441428dfSJeremy L Thompson   if (ctx) {
401441428dfSJeremy L Thompson     ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr);
402441428dfSJeremy L Thompson     if (is_writable) {
403441428dfSJeremy L Thompson       ierr = CeedQFunctionContextGetData(ctx, mem_type, data); CeedChk(ierr);
404441428dfSJeremy L Thompson     } else {
405441428dfSJeremy L Thompson       ierr = CeedQFunctionContextGetDataRead(ctx, mem_type, data); CeedChk(ierr);
406441428dfSJeremy L Thompson     }
407441428dfSJeremy L Thompson   } else {
408441428dfSJeremy L Thompson     *(void **)data = NULL;
409441428dfSJeremy L Thompson   }
410441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
411441428dfSJeremy L Thompson }
412441428dfSJeremy L Thompson 
413441428dfSJeremy L Thompson /**
414441428dfSJeremy L Thompson   @brief Restore inner context data of a CeedQFunction
415441428dfSJeremy L Thompson 
416441428dfSJeremy L Thompson   @param qf    CeedQFunction
417441428dfSJeremy L Thompson   @param data  Data to restore
418441428dfSJeremy L Thompson 
419441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
420441428dfSJeremy L Thompson 
421441428dfSJeremy L Thompson   @ref Backend
422441428dfSJeremy L Thompson **/
423441428dfSJeremy L Thompson int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) {
424441428dfSJeremy L Thompson   int ierr;
425441428dfSJeremy L Thompson   bool is_writable;
426441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
427441428dfSJeremy L Thompson 
428441428dfSJeremy L Thompson   ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChk(ierr);
429441428dfSJeremy L Thompson   if (ctx) {
430441428dfSJeremy L Thompson     ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr);
431441428dfSJeremy L Thompson     if (is_writable) {
432441428dfSJeremy L Thompson       ierr = CeedQFunctionContextRestoreData(ctx, data); CeedChk(ierr);
433441428dfSJeremy L Thompson     } else {
434441428dfSJeremy L Thompson       ierr = CeedQFunctionContextRestoreDataRead(ctx, data); CeedChk(ierr);
435441428dfSJeremy L Thompson     }
436441428dfSJeremy L Thompson   }
437441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
438441428dfSJeremy L Thompson }
439441428dfSJeremy L Thompson 
440441428dfSJeremy L Thompson /**
4417a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
4427a982d89SJeremy L. Thompson 
4437a982d89SJeremy L. Thompson   @param qf                CeedQFunction
444d1d35e2fSjeremylt   @param[out] is_identity  Variable to store identity status
4457a982d89SJeremy L. Thompson 
4467a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4477a982d89SJeremy L. Thompson 
4487a982d89SJeremy L. Thompson   @ref Backend
4497a982d89SJeremy L. Thompson **/
450d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
451f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
452e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4537a982d89SJeremy L. Thompson }
4547a982d89SJeremy L. Thompson 
4557a982d89SJeremy L. Thompson /**
456441428dfSJeremy L Thompson   @brief Determine if QFunctionContext is writable
457441428dfSJeremy L Thompson 
458441428dfSJeremy L Thompson   @param qf                CeedQFunction
459441428dfSJeremy L Thompson   @param[out] is_writable  Variable to store context writeable staus
460441428dfSJeremy L Thompson 
461441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
462441428dfSJeremy L Thompson 
463441428dfSJeremy L Thompson   @ref Backend
464441428dfSJeremy L Thompson **/
465441428dfSJeremy L Thompson int CeedQFunctionIsContextWritable(CeedQFunction qf, bool *is_writable) {
466441428dfSJeremy L Thompson   *is_writable = qf->is_context_writable;
467441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
468441428dfSJeremy L Thompson }
469441428dfSJeremy L Thompson 
470441428dfSJeremy L Thompson /**
4717a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
4727a982d89SJeremy L. Thompson 
4737a982d89SJeremy L. Thompson   @param qf         CeedQFunction
4747a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
4757a982d89SJeremy L. Thompson 
4767a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4777a982d89SJeremy L. Thompson 
4787a982d89SJeremy L. Thompson   @ref Backend
4797a982d89SJeremy L. Thompson **/
480777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
481777ff853SJeremy L Thompson   *(void **)data = qf->data;
482e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4837a982d89SJeremy L. Thompson }
4847a982d89SJeremy L. Thompson 
4857a982d89SJeremy L. Thompson /**
4867a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
4877a982d89SJeremy L. Thompson 
4887a982d89SJeremy L. Thompson   @param[out] qf  CeedQFunction
4897a982d89SJeremy L. Thompson   @param data     Data to set
4907a982d89SJeremy L. Thompson 
4917a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4927a982d89SJeremy L. Thompson 
4937a982d89SJeremy L. Thompson   @ref Backend
4947a982d89SJeremy L. Thompson **/
495777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
496777ff853SJeremy L Thompson   qf->data = data;
497e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4987a982d89SJeremy L. Thompson }
4997a982d89SJeremy L. Thompson 
5007a982d89SJeremy L. Thompson /**
50134359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
50234359f16Sjeremylt 
50334359f16Sjeremylt   @param qf  CeedQFunction to increment the reference counter
50434359f16Sjeremylt 
50534359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
50634359f16Sjeremylt 
50734359f16Sjeremylt   @ref Backend
50834359f16Sjeremylt **/
5099560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
51034359f16Sjeremylt   qf->ref_count++;
51134359f16Sjeremylt   return CEED_ERROR_SUCCESS;
51234359f16Sjeremylt }
51334359f16Sjeremylt 
5146e15d496SJeremy L Thompson /**
5156e15d496SJeremy L Thompson   @brief Estimate number of FLOPs per quadrature required to apply QFunction
5166e15d496SJeremy L Thompson 
5176e15d496SJeremy L Thompson   @param qf    QFunction to estimate FLOPs for
5186e15d496SJeremy L Thompson   @param flops Address of variable to hold FLOPs estimate
5196e15d496SJeremy L Thompson 
5206e15d496SJeremy L Thompson   @ref Backend
5216e15d496SJeremy L Thompson **/
522*9d36ca50SJeremy L Thompson int CeedQFunctionGetFlopsEstimate(CeedQFunction qf, CeedSize *flops) {
5236e15d496SJeremy L Thompson   if (qf->user_flop_estimate == -1)
5246e15d496SJeremy L Thompson     // LCOV_EXCL_START
5256e15d496SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_INCOMPLETE,
5266e15d496SJeremy L Thompson                      "Must set FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
5276e15d496SJeremy L Thompson   // LCOV_EXCL_STOP
5286e15d496SJeremy L Thompson   *flops = qf->user_flop_estimate;
5296e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5306e15d496SJeremy L Thompson }
5316e15d496SJeremy L Thompson 
5327a982d89SJeremy L. Thompson /// @}
5337a982d89SJeremy L. Thompson 
5347a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5357a982d89SJeremy L. Thompson /// CeedQFunction Public API
5367a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5377a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
5387a982d89SJeremy L. Thompson /// @{
5397a982d89SJeremy L. Thompson 
5407a982d89SJeremy L. Thompson /**
5417a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
5427a982d89SJeremy L. Thompson 
5437a982d89SJeremy L. Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
544d1d35e2fSjeremylt   @param vec_length  Vector length. Caller must ensure that number of quadrature
545d1d35e2fSjeremylt                        points is a multiple of vec_length.
5467a982d89SJeremy L. Thompson   @param f           Function pointer to evaluate action at quadrature points.
5477a982d89SJeremy L. Thompson                        See \ref CeedQFunctionUser.
5487a982d89SJeremy L. Thompson   @param source      Absolute path to source of QFunction,
549c8d5b03cSJeremy L Thompson                        "\abs_path\file.h:function_name".
550c8d5b03cSJeremy L Thompson                        For support across all backends, this source must only
551c8d5b03cSJeremy L Thompson                        contain constructs supported by C99, C++11, and CUDA.
5527a982d89SJeremy L. Thompson   @param[out] qf     Address of the variable where the newly created
5537a982d89SJeremy L. Thompson                        CeedQFunction will be stored
5547a982d89SJeremy L. Thompson 
5557a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5567a982d89SJeremy L. Thompson 
5577a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
5587a982d89SJeremy L. Thompson     arguments.
5597a982d89SJeremy L. Thompson 
5607a982d89SJeremy L. Thompson   @ref User
5617a982d89SJeremy L. Thompson **/
562d1d35e2fSjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length,
563d1d35e2fSjeremylt                                 CeedQFunctionUser f,
5647a982d89SJeremy L. Thompson                                 const char *source, CeedQFunction *qf) {
5657a982d89SJeremy L. Thompson   int ierr;
56643e1b16fSJeremy L Thompson   char *source_copy, *kernel_name_copy;
5677a982d89SJeremy L. Thompson 
5687a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
5697a982d89SJeremy L. Thompson     Ceed delegate;
5707a982d89SJeremy L. Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
5717a982d89SJeremy L. Thompson 
5727a982d89SJeremy L. Thompson     if (!delegate)
5737a982d89SJeremy L. Thompson       // LCOV_EXCL_START
574e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
575e15f9bd0SJeremy L Thompson                        "Backend does not support QFunctionCreate");
5767a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
5777a982d89SJeremy L. Thompson 
578d1d35e2fSjeremylt     ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf);
5797a982d89SJeremy L. Thompson     CeedChk(ierr);
580e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
5817a982d89SJeremy L. Thompson   }
5827a982d89SJeremy L. Thompson 
583edfb5f23SJeremy L Thompson   if (strlen(source) && !strrchr(source, ':'))
58443e1b16fSJeremy L Thompson     // LCOV_EXCL_START
58543e1b16fSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPLETE,
58643e1b16fSJeremy L Thompson                      "Provided path to source does not include function name. "
58743e1b16fSJeremy L Thompson                      "Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"",
58843e1b16fSJeremy L Thompson                      source);
58943e1b16fSJeremy L Thompson   // LCOV_EXCL_STOP
59043e1b16fSJeremy L Thompson 
5917a982d89SJeremy L. Thompson   ierr = CeedCalloc(1, qf); CeedChk(ierr);
5927a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
5939560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
594d1d35e2fSjeremylt   (*qf)->ref_count = 1;
595d1d35e2fSjeremylt   (*qf)->vec_length = vec_length;
596f04ea552SJeremy L Thompson   (*qf)->is_identity = false;
597441428dfSJeremy L Thompson   (*qf)->is_context_writable = true;
5987a982d89SJeremy L. Thompson   (*qf)->function = f;
5996e15d496SJeremy L Thompson   (*qf)->user_flop_estimate = -1;
60043e1b16fSJeremy L Thompson   if (strlen(source)) {
60143e1b16fSJeremy L Thompson     const char *kernel_name = strrchr(source, ':') + 1;
60243e1b16fSJeremy L Thompson     size_t kernel_name_len = strlen(kernel_name);
60343e1b16fSJeremy L Thompson     ierr = CeedCalloc(kernel_name_len + 1, &kernel_name_copy); CeedChk(ierr);
60443e1b16fSJeremy L Thompson     strncpy(kernel_name_copy, kernel_name, kernel_name_len);
60543e1b16fSJeremy L Thompson     (*qf)->kernel_name = kernel_name_copy;
60643e1b16fSJeremy L Thompson 
60743e1b16fSJeremy L Thompson     size_t source_len = strlen(source) - kernel_name_len - 1;
60843e1b16fSJeremy L Thompson     ierr = CeedCalloc(source_len + 1, &source_copy); CeedChk(ierr);
60943e1b16fSJeremy L Thompson     strncpy(source_copy, source, source_len);
610d1d35e2fSjeremylt     (*qf)->source_path = source_copy;
61143e1b16fSJeremy L Thompson   }
612bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields); CeedChk(ierr);
613bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields); CeedChk(ierr);
6147a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
615e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6167a982d89SJeremy L. Thompson }
6177a982d89SJeremy L. Thompson 
6187a982d89SJeremy L. Thompson /**
619288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
620288c0443SJeremy L Thompson 
621288c0443SJeremy L Thompson   @param ceed     A Ceed object where the CeedQFunction will be created
622288c0443SJeremy L Thompson   @param name     Name of QFunction to use from gallery
623288c0443SJeremy L Thompson   @param[out] qf  Address of the variable where the newly created
624288c0443SJeremy L Thompson                     CeedQFunction will be stored
625288c0443SJeremy L Thompson 
626288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
627288c0443SJeremy L Thompson 
6287a982d89SJeremy L. Thompson   @ref User
629288c0443SJeremy L Thompson **/
630288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
631288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
632288c0443SJeremy L Thompson   int ierr;
633f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
634288c0443SJeremy L Thompson 
6351d013790SJed Brown   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
636288c0443SJeremy L Thompson   // Find matching backend
637e15f9bd0SJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE,
638e15f9bd0SJeremy L Thompson                                 "No QFunction name provided");
639288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
640288c0443SJeremy L Thompson     size_t n;
641d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
642d1d35e2fSjeremylt     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {}
643d1d35e2fSjeremylt     if (n > match_len) {
644d1d35e2fSjeremylt       match_len = n;
645f7e22acaSJeremy L Thompson       match_index = i;
646288c0443SJeremy L Thompson     }
647288c0443SJeremy L Thompson   }
648d1d35e2fSjeremylt   if (!match_len)
649138d4072Sjeremylt     // LCOV_EXCL_START
650e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
651138d4072Sjeremylt   // LCOV_EXCL_STOP
652288c0443SJeremy L Thompson 
653288c0443SJeremy L Thompson   // Create QFunction
654d1d35e2fSjeremylt   ierr = CeedQFunctionCreateInterior(ceed,
655f7e22acaSJeremy L Thompson                                      gallery_qfunctions[match_index].vec_length,
656f7e22acaSJeremy L Thompson                                      gallery_qfunctions[match_index].f,
657f7e22acaSJeremy L Thompson                                      gallery_qfunctions[match_index].source, qf);
658288c0443SJeremy L Thompson   CeedChk(ierr);
659288c0443SJeremy L Thompson 
660288c0443SJeremy L Thompson   // QFunction specific setup
661f7e22acaSJeremy L Thompson   ierr = gallery_qfunctions[match_index].init(ceed, name, *qf); CeedChk(ierr);
662288c0443SJeremy L Thompson 
66375affc3bSjeremylt   // Copy name
664f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name); CeedChk(ierr);
66543e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
666e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
667288c0443SJeremy L Thompson }
668288c0443SJeremy L Thompson 
669288c0443SJeremy L Thompson /**
6700219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
6710219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
6720219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
6730219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
6740219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
6750219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
6760219ea01SJeremy L Thompson            location for both.
6770219ea01SJeremy L Thompson 
6780219ea01SJeremy L Thompson   @param ceed          A Ceed object where the CeedQFunction will be created
679d1d35e2fSjeremylt   @param[in] size      Size of the QFunction fields
680d1d35e2fSjeremylt   @param[in] in_mode   CeedEvalMode for input to CeedQFunction
681d1d35e2fSjeremylt   @param[in] out_mode  CeedEvalMode for output to CeedQFunction
6820219ea01SJeremy L Thompson   @param[out] qf       Address of the variable where the newly created
6830219ea01SJeremy L Thompson                          CeedQFunction will be stored
6840219ea01SJeremy L Thompson 
6850219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
6860219ea01SJeremy L Thompson 
6877a982d89SJeremy L. Thompson   @ref User
6880219ea01SJeremy L Thompson **/
689d1d35e2fSjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode,
690d1d35e2fSjeremylt                                 CeedEvalMode out_mode, CeedQFunction *qf) {
6910219ea01SJeremy L Thompson   int ierr;
6920219ea01SJeremy L Thompson 
6930219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
694d1d35e2fSjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr);
695d1d35e2fSjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr);
6960219ea01SJeremy L Thompson 
697f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
698547dbd6fSJeremy L Thompson 
699777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
7003668ca4bSJeremy L Thompson   CeedContextFieldLabel size_label;
701547dbd6fSJeremy L Thompson   ierr = CeedQFunctionGetContext(*qf, &ctx); CeedChk(ierr);
7023668ca4bSJeremy L Thompson   ierr = CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label);
7033668ca4bSJeremy L Thompson   CeedChk(ierr);
7047bfe0f0eSJeremy L Thompson   ierr = CeedQFunctionContextSetInt32(ctx, size_label, &size); CeedChk(ierr);
705547dbd6fSJeremy L Thompson 
706e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7070219ea01SJeremy L Thompson }
7080219ea01SJeremy L Thompson 
7090219ea01SJeremy L Thompson /**
7109560d06aSjeremylt   @brief Copy the pointer to a CeedQFunction. Both pointers should
7119560d06aSjeremylt            be destroyed with `CeedQFunctionDestroy()`;
7129560d06aSjeremylt            Note: If `*qf_copy` is non-NULL, then it is assumed that
7139560d06aSjeremylt            `*qf_copy` is a pointer to a CeedQFunction. This
7149560d06aSjeremylt            CeedQFunction will be destroyed if `*qf_copy` is the only
7159560d06aSjeremylt            reference to this CeedQFunction.
7169560d06aSjeremylt 
7179560d06aSjeremylt   @param qf            CeedQFunction to copy reference to
7189560d06aSjeremylt   @param[out] qf_copy  Variable to store copied reference
7199560d06aSjeremylt 
7209560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7219560d06aSjeremylt 
7229560d06aSjeremylt   @ref User
7239560d06aSjeremylt **/
7249560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
7259560d06aSjeremylt   int ierr;
7269560d06aSjeremylt 
7279560d06aSjeremylt   ierr = CeedQFunctionReference(qf); CeedChk(ierr);
7289560d06aSjeremylt   ierr = CeedQFunctionDestroy(qf_copy); CeedChk(ierr);
7299560d06aSjeremylt   *qf_copy = qf;
7309560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7319560d06aSjeremylt }
7329560d06aSjeremylt 
7339560d06aSjeremylt /**
734a0a97fcfSJed Brown   @brief Add a CeedQFunction input
735b11c1e72Sjeremylt 
736b11c1e72Sjeremylt   @param qf          CeedQFunction
737d1d35e2fSjeremylt   @param field_name  Name of QFunction field
738d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
739d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
740d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
741b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
742b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
743b11c1e72Sjeremylt 
744b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
745dfdf5a53Sjeremylt 
7467a982d89SJeremy L. Thompson   @ref User
747b11c1e72Sjeremylt **/
748d1d35e2fSjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name,
749d1d35e2fSjeremylt                           CeedInt size,
750d1d35e2fSjeremylt                           CeedEvalMode eval_mode) {
751f04ea552SJeremy L Thompson   if (qf->is_immutable)
752e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
753e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
754f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
755e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
756d1d35e2fSjeremylt   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1))
757e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
758e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
759e15f9bd0SJeremy L Thompson                      "CEED_EVAL_WEIGHT should have size 1");
760e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
761d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields],
762d1d35e2fSjeremylt                                    field_name, size, eval_mode);
763fe2413ffSjeremylt   CeedChk(ierr);
764d1d35e2fSjeremylt   qf->num_input_fields++;
765e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
766d7b241e6Sjeremylt }
767d7b241e6Sjeremylt 
768b11c1e72Sjeremylt /**
769a0a97fcfSJed Brown   @brief Add a CeedQFunction output
770b11c1e72Sjeremylt 
771b11c1e72Sjeremylt   @param qf          CeedQFunction
772d1d35e2fSjeremylt   @param field_name  Name of QFunction field
773d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
774d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
775d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
776b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
777b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
778b11c1e72Sjeremylt 
779b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
780dfdf5a53Sjeremylt 
7817a982d89SJeremy L. Thompson   @ref User
782b11c1e72Sjeremylt **/
783d1d35e2fSjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name,
784d1d35e2fSjeremylt                            CeedInt size, CeedEvalMode eval_mode) {
785f04ea552SJeremy L Thompson   if (qf->is_immutable)
786e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
787e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
788f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
789e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
790d1d35e2fSjeremylt   if (eval_mode == CEED_EVAL_WEIGHT)
791c042f62fSJeremy L Thompson     // LCOV_EXCL_START
792e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
793e15f9bd0SJeremy L Thompson                      "Cannot create QFunction output with "
7941d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
795c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
796d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields],
797d1d35e2fSjeremylt                                    field_name, size, eval_mode);
798fe2413ffSjeremylt   CeedChk(ierr);
799d1d35e2fSjeremylt   qf->num_output_fields++;
800e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
801d7b241e6Sjeremylt }
802d7b241e6Sjeremylt 
803dfdf5a53Sjeremylt /**
80443bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
80543bbe138SJeremy L Thompson 
806f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
807f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
808f04ea552SJeremy L Thompson 
80943bbe138SJeremy L Thompson   @param qf                      CeedQFunction
810f74ec584SJeremy L Thompson   @param[out] num_input_fields   Variable to store number of input fields
811f74ec584SJeremy L Thompson   @param[out] input_fields       Variable to store input fields
812f74ec584SJeremy L Thompson   @param[out] num_output_fields  Variable to store number of output fields
813f74ec584SJeremy L Thompson   @param[out] output_fields      Variable to store output fields
81443bbe138SJeremy L Thompson 
81543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
81643bbe138SJeremy L Thompson 
817e9b533fbSJeremy L Thompson   @ref Advanced
81843bbe138SJeremy L Thompson **/
81943bbe138SJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields,
82043bbe138SJeremy L Thompson                            CeedQFunctionField **input_fields,
82143bbe138SJeremy L Thompson                            CeedInt *num_output_fields,
82243bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
823f04ea552SJeremy L Thompson   qf->is_immutable = true;
82443bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
82543bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
82643bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
82743bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
82843bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
82943bbe138SJeremy L Thompson }
83043bbe138SJeremy L Thompson 
83143bbe138SJeremy L Thompson /**
83243bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
83343bbe138SJeremy L Thompson 
83443bbe138SJeremy L Thompson   @param qf_field         CeedQFunctionField
83543bbe138SJeremy L Thompson   @param[out] field_name  Variable to store the field name
83643bbe138SJeremy L Thompson 
83743bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
83843bbe138SJeremy L Thompson 
839e9b533fbSJeremy L Thompson   @ref Advanced
84043bbe138SJeremy L Thompson **/
84143bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
84243bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
84343bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
84443bbe138SJeremy L Thompson }
84543bbe138SJeremy L Thompson 
84643bbe138SJeremy L Thompson /**
84743bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
84843bbe138SJeremy L Thompson 
84943bbe138SJeremy L Thompson   @param qf_field   CeedQFunctionField
85043bbe138SJeremy L Thompson   @param[out] size  Variable to store the size of the field
85143bbe138SJeremy L Thompson 
85243bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
85343bbe138SJeremy L Thompson 
854e9b533fbSJeremy L Thompson   @ref Advanced
85543bbe138SJeremy L Thompson **/
85643bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
85743bbe138SJeremy L Thompson   *size = qf_field->size;
85843bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
85943bbe138SJeremy L Thompson }
86043bbe138SJeremy L Thompson 
86143bbe138SJeremy L Thompson /**
86243bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
86343bbe138SJeremy L Thompson 
86443bbe138SJeremy L Thompson   @param qf_field        CeedQFunctionField
86543bbe138SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
86643bbe138SJeremy L Thompson 
86743bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
86843bbe138SJeremy L Thompson 
869e9b533fbSJeremy L Thompson   @ref Advanced
87043bbe138SJeremy L Thompson **/
87143bbe138SJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field,
87243bbe138SJeremy L Thompson                                   CeedEvalMode *eval_mode) {
87343bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
87443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
87543bbe138SJeremy L Thompson }
87643bbe138SJeremy L Thompson 
87743bbe138SJeremy L Thompson /**
8784ce2993fSjeremylt   @brief Set global context for a CeedQFunction
879b11c1e72Sjeremylt 
880b11c1e72Sjeremylt   @param qf   CeedQFunction
881b11c1e72Sjeremylt   @param ctx  Context data to set
882b11c1e72Sjeremylt 
883b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
884dfdf5a53Sjeremylt 
8857a982d89SJeremy L. Thompson   @ref User
886b11c1e72Sjeremylt **/
887777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
88834359f16Sjeremylt   int ierr;
8891389e7feSJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&qf->ctx); CeedChk(ierr);
890d7b241e6Sjeremylt   qf->ctx = ctx;
8919560d06aSjeremylt   ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr);
892e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
893d7b241e6Sjeremylt }
894d7b241e6Sjeremylt 
895b11c1e72Sjeremylt /**
896441428dfSJeremy L Thompson   @brief Set writability of CeedQFunctionContext when calling the `CeedQFunctionUser`.
897441428dfSJeremy L Thompson            The default value is 'is_writable == true'.
898441428dfSJeremy L Thompson 
899441428dfSJeremy L Thompson            Setting `is_writable == true` indicates the `CeedQFunctionUser` writes
900441428dfSJeremy L Thompson            into the CeedQFunctionContextData and requires memory syncronization
901441428dfSJeremy L Thompson            after calling `CeedQFunctionApply()`.
902441428dfSJeremy L Thompson 
903441428dfSJeremy L Thompson            Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not
904441428dfSJeremy L Thompson            modify the CeedQFunctionContextData. Violating this assertion may lead
905441428dfSJeremy L Thompson            to inconsistent data.
906441428dfSJeremy L Thompson 
907441428dfSJeremy L Thompson            Setting `is_writable == false` may offer a performance improvement on GPU backends.
908441428dfSJeremy L Thompson 
909441428dfSJeremy L Thompson   @param qf           CeedQFunction
910441428dfSJeremy L Thompson   @param is_writable  Writability status
911441428dfSJeremy L Thompson 
912441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
913441428dfSJeremy L Thompson 
914441428dfSJeremy L Thompson   @ref User
915441428dfSJeremy L Thompson **/
916441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
917441428dfSJeremy L Thompson   qf->is_context_writable = is_writable;
918441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
919441428dfSJeremy L Thompson }
920441428dfSJeremy L Thompson 
921441428dfSJeremy L Thompson /**
9226e15d496SJeremy L Thompson   @brief Set estimated number of FLOPs per quadrature required to apply QFunction
9236e15d496SJeremy L Thompson 
9246e15d496SJeremy L Thompson   @param qf    QFunction to estimate FLOPs for
9256e15d496SJeremy L Thompson   @param flops FLOPs per quadrature point estimate
9266e15d496SJeremy L Thompson 
9276e15d496SJeremy L Thompson   @ref Backend
9286e15d496SJeremy L Thompson **/
929*9d36ca50SJeremy L Thompson int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
9306e15d496SJeremy L Thompson   if (flops < 0)
9316e15d496SJeremy L Thompson     // LCOV_EXCL_START
9326e15d496SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_INCOMPATIBLE,
9336e15d496SJeremy L Thompson                      "Must set non-negative FLOPs estimate");
9346e15d496SJeremy L Thompson   // LCOV_EXCL_STOP
9356e15d496SJeremy L Thompson   qf->user_flop_estimate = flops;
9366e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9376e15d496SJeremy L Thompson }
9386e15d496SJeremy L Thompson 
9396e15d496SJeremy L Thompson /**
94075affc3bSjeremylt   @brief View a CeedQFunction
94175affc3bSjeremylt 
94275affc3bSjeremylt   @param[in] qf      CeedQFunction to view
94375affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
94475affc3bSjeremylt 
94575affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
94675affc3bSjeremylt 
9477a982d89SJeremy L. Thompson   @ref User
94875affc3bSjeremylt **/
94975affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
95075affc3bSjeremylt   int ierr;
95175affc3bSjeremylt 
95275affc3bSjeremylt   fprintf(stream, "%sCeedQFunction %s\n",
95343e1b16fSJeremy L Thompson           qf->is_gallery ? "Gallery " : "User ",
95443e1b16fSJeremy L Thompson           qf->is_gallery ? qf->gallery_name : qf->kernel_name);
95575affc3bSjeremylt 
956d1d35e2fSjeremylt   fprintf(stream, "  %d Input Field%s:\n", qf->num_input_fields,
957d1d35e2fSjeremylt           qf->num_input_fields>1 ? "s" : "");
958d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_input_fields; i++) {
959d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream);
9602da88da5Sjeremylt     CeedChk(ierr);
96175affc3bSjeremylt   }
96275affc3bSjeremylt 
963d1d35e2fSjeremylt   fprintf(stream, "  %d Output Field%s:\n", qf->num_output_fields,
964d1d35e2fSjeremylt           qf->num_output_fields>1 ? "s" : "");
965d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_output_fields; i++) {
966d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream);
96775affc3bSjeremylt     CeedChk(ierr);
96875affc3bSjeremylt   }
969e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
97075affc3bSjeremylt }
97175affc3bSjeremylt 
97275affc3bSjeremylt /**
973b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunction
974b7c9bbdaSJeremy L Thompson 
975b7c9bbdaSJeremy L Thompson   @param qf              CeedQFunction
976b7c9bbdaSJeremy L Thompson   @param[out] ceed       Variable to store Ceed
977b7c9bbdaSJeremy L Thompson 
978b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
979b7c9bbdaSJeremy L Thompson 
980b7c9bbdaSJeremy L Thompson   @ref Advanced
981b7c9bbdaSJeremy L Thompson **/
982b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
983b7c9bbdaSJeremy L Thompson   *ceed = qf->ceed;
984b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
985b7c9bbdaSJeremy L Thompson }
986b7c9bbdaSJeremy L Thompson 
987b7c9bbdaSJeremy L Thompson /**
988b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
989b11c1e72Sjeremylt 
990f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
991f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
992f04ea552SJeremy L Thompson 
993b11c1e72Sjeremylt   @param qf      CeedQFunction
994b11c1e72Sjeremylt   @param Q       Number of quadrature points
99534138859Sjeremylt   @param[in] u   Array of input CeedVectors
99634138859Sjeremylt   @param[out] v  Array of output CeedVectors
997b11c1e72Sjeremylt 
998b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
999dfdf5a53Sjeremylt 
10007a982d89SJeremy L. Thompson   @ref User
1001b11c1e72Sjeremylt **/
1002d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
1003aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
1004d7b241e6Sjeremylt   int ierr;
1005d7b241e6Sjeremylt   if (!qf->Apply)
1006c042f62fSJeremy L Thompson     // LCOV_EXCL_START
1007e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED,
1008e15f9bd0SJeremy L Thompson                      "Backend does not support QFunctionApply");
1009c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
1010d1d35e2fSjeremylt   if (Q % qf->vec_length)
1011c042f62fSJeremy L Thompson     // LCOV_EXCL_START
1012e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
1013e15f9bd0SJeremy L Thompson                      "Number of quadrature points %d must be a "
1014d1d35e2fSjeremylt                      "multiple of %d", Q, qf->vec_length);
1015c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
1016f04ea552SJeremy L Thompson   qf->is_immutable = true;
1017d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
1018e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1019d7b241e6Sjeremylt }
1020d7b241e6Sjeremylt 
1021b11c1e72Sjeremylt /**
1022b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
1023b11c1e72Sjeremylt 
1024b11c1e72Sjeremylt   @param qf  CeedQFunction to destroy
1025b11c1e72Sjeremylt 
1026b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1027dfdf5a53Sjeremylt 
10287a982d89SJeremy L. Thompson   @ref User
1029b11c1e72Sjeremylt **/
1030d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
1031d7b241e6Sjeremylt   int ierr;
1032d7b241e6Sjeremylt 
1033d1d35e2fSjeremylt   if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS;
1034fe2413ffSjeremylt   // Backend destroy
1035d7b241e6Sjeremylt   if ((*qf)->Destroy) {
1036d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
1037d7b241e6Sjeremylt   }
1038fe2413ffSjeremylt   // Free fields
1039d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_input_fields; i++) {
1040d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr);
1041d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr);
1042fe2413ffSjeremylt   }
1043d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_output_fields; i++) {
1044d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr);
1045d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr);
1046fe2413ffSjeremylt   }
1047d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr);
1048d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr);
1049777ff853SJeremy L Thompson 
1050777ff853SJeremy L Thompson   // User context data object
1051777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
1052fe2413ffSjeremylt 
1053d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr);
105443e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->gallery_name); CeedChk(ierr);
105543e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->kernel_name); CeedChk(ierr);
1056d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
1057d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
1058e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1059d7b241e6Sjeremylt }
1060d7b241e6Sjeremylt 
1061d7b241e6Sjeremylt /// @}
1062