xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision 9ba83ac0e4b1fca39d6fa6737a318a9f0cbc172d)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, 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 
83d576824SJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
112b730f8bSJeremy L Thompson #include <ceed/jit-tools.h>
12288c0443SJeremy L Thompson #include <limits.h>
133d576824SJeremy L Thompson #include <stdbool.h>
143d576824SJeremy L Thompson #include <stdio.h>
153d576824SJeremy L Thompson #include <string.h>
16288c0443SJeremy L Thompson 
177a982d89SJeremy L. Thompson /// @file
187a982d89SJeremy L. Thompson /// Implementation of public CeedQFunction interfaces
197a982d89SJeremy L. Thompson 
20288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP
21442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none;
22442e7f0bSjeremylt /// @endcond
23442e7f0bSjeremylt 
247a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
257a982d89SJeremy L. Thompson /// @{
267a982d89SJeremy L. Thompson 
27ca94c3ddSJeremy L Thompson // Indicate that no `CeedQFunction` is provided by the user
287a982d89SJeremy L. Thompson const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
297a982d89SJeremy L. Thompson 
307a982d89SJeremy L. Thompson /// @}
317a982d89SJeremy L. Thompson 
32442e7f0bSjeremylt /// @cond DOXYGEN_SKIP
33288c0443SJeremy L Thompson static struct {
34288c0443SJeremy L Thompson   char              name[CEED_MAX_RESOURCE_LEN];
35288c0443SJeremy L Thompson   char              source[CEED_MAX_RESOURCE_LEN];
36d1d35e2fSjeremylt   CeedInt           vec_length;
37288c0443SJeremy L Thompson   CeedQFunctionUser f;
38288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
39d1d35e2fSjeremylt } gallery_qfunctions[1024];
40288c0443SJeremy L Thompson static size_t num_qfunctions;
41288c0443SJeremy L Thompson /// @endcond
42d7b241e6Sjeremylt 
43777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
44777ff853SJeremy L Thompson /// CeedQFunction Library Internal Functions
45777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
46777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper
47777ff853SJeremy L Thompson /// @{
48777ff853SJeremy L Thompson 
49b11c1e72Sjeremylt /**
50ca94c3ddSJeremy L Thompson   @brief Register a gallery `CeedQFunction`
51288c0443SJeremy L Thompson 
52ea61e9acSJeremy L Thompson   @param[in] name       Name for this backend to respond to
53ca94c3ddSJeremy L Thompson   @param[in] source     Absolute path to source of `CeedQFunction`, "\path\CEED_DIR\gallery\folder\file.h:function_name"
54ca94c3ddSJeremy L Thompson   @param[in] vec_length Vector length.
55ca94c3ddSJeremy L Thompson                           Caller must ensure that number of quadrature points is a multiple of `vec_length`.
56ea61e9acSJeremy L Thompson   @param[in] f          Function pointer to evaluate action at quadrature points.
57ca94c3ddSJeremy L Thompson                           See `CeedQFunctionUser`.
58ca94c3ddSJeremy L Thompson   @param[in] init       Initialization function called by @ref CeedQFunctionCreateInteriorByName() when the `CeedQFunction` is selected.
59288c0443SJeremy L Thompson 
60288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
61288c0443SJeremy L Thompson 
627a982d89SJeremy L. Thompson   @ref Developer
63288c0443SJeremy L Thompson **/
642b730f8bSJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source, CeedInt vec_length, CeedQFunctionUser f,
65288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
661c66c397SJeremy L Thompson   const char *relative_file_path;
6758c07c4fSSebastian Grimberg   int         ierr = 0;
68c042f62fSJeremy L Thompson 
698ccf1006SJeremy L Thompson   CeedDebugEnv("Gallery Register: %s", name);
702b730f8bSJeremy L Thompson   CeedCall(CeedGetJitRelativePath(source, &relative_file_path));
7158c07c4fSSebastian Grimberg   CeedPragmaCritical(CeedQFunctionRegister) {
7258c07c4fSSebastian Grimberg     if (num_qfunctions < sizeof(gallery_qfunctions) / sizeof(gallery_qfunctions[0])) {
73d1d35e2fSjeremylt       strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
74d1d35e2fSjeremylt       gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN - 1] = 0;
752b730f8bSJeremy L Thompson       strncpy(gallery_qfunctions[num_qfunctions].source, relative_file_path, CEED_MAX_RESOURCE_LEN);
76d1d35e2fSjeremylt       gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN - 1] = 0;
77d1d35e2fSjeremylt       gallery_qfunctions[num_qfunctions].vec_length                        = vec_length;
78d1d35e2fSjeremylt       gallery_qfunctions[num_qfunctions].f                                 = f;
79d1d35e2fSjeremylt       gallery_qfunctions[num_qfunctions].init                              = init;
80288c0443SJeremy L Thompson       num_qfunctions++;
8158c07c4fSSebastian Grimberg     } else {
8258c07c4fSSebastian Grimberg       ierr = 1;
8358c07c4fSSebastian Grimberg     }
8458c07c4fSSebastian Grimberg   }
85ca94c3ddSJeremy L Thompson   CeedCheck(ierr == 0, NULL, CEED_ERROR_MAJOR, "Too many gallery CeedQFunctions");
86e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
87288c0443SJeremy L Thompson }
88288c0443SJeremy L Thompson 
89288c0443SJeremy L Thompson /**
90ca94c3ddSJeremy L Thompson   @brief Set a `CeedQFunction` field, used by @ref CeedQFunctionAddInput() and @ref CeedQFunctionAddOutput()
917a982d89SJeremy L. Thompson 
92ca94c3ddSJeremy L Thompson   @param[out] f           `CeedQFunctionField`
93ca94c3ddSJeremy L Thompson   @param[in]  field_name  Name of `CeedQFunction` field
94ca94c3ddSJeremy L Thompson   @param[in]  size        Size of `CeedQFunction` field, (`num_comp * 1`) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_WEIGHT, (`num_comp * 1`) for @ref CEED_EVAL_INTERP for an \f$H^1\f$ space or (`num_comp * dim`) for an \f$H(\mathrm{div})\f$ or \f$H(\mathrm{curl})\f$ space, (`num_comp * dim`) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` and `curl_dim = dim` for @ref CEED_EVAL_CURL.
95ca94c3ddSJeremy L Thompson   @param[in]  eval_mode   @ref CEED_EVAL_NONE to use values directly,
96ca94c3ddSJeremy L Thompson                             @ref CEED_EVAL_WEIGHT to use quadrature weights,
97ca94c3ddSJeremy L Thompson                             @ref CEED_EVAL_INTERP to use interpolated values,
98ca94c3ddSJeremy L Thompson                             @ref CEED_EVAL_GRAD to use gradients,
99ca94c3ddSJeremy L Thompson                             @ref CEED_EVAL_DIV to use divergence,
100ca94c3ddSJeremy L Thompson                             @ref CEED_EVAL_CURL to use curl
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 **/
1062b730f8bSJeremy L Thompson static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
1072b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, f));
1082b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*f)->field_name));
1097a982d89SJeremy L. Thompson   (*f)->size      = size;
110d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
111e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1127a982d89SJeremy L. Thompson }
1137a982d89SJeremy L. Thompson 
1147a982d89SJeremy L. Thompson /**
115ca94c3ddSJeremy L Thompson   @brief View a field of a `CeedQFunction`
1167a982d89SJeremy L. Thompson 
117ca94c3ddSJeremy L Thompson   @param[in] field        `CeedQFunction` field to view
118d1d35e2fSjeremylt   @param[in] field_number Number of field being viewed
1194c4400c7SValeria Barra   @param[in] in           true for input field, false for output
1204c789ea2SJeremy L Thompson   @param[in] tabs         Tabs to append before each new line
121ca94c3ddSJeremy L Thompson   @param[in] stream       Stream to view to, e.g., `stdout`
1227a982d89SJeremy L. Thompson 
1237a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1247a982d89SJeremy L. Thompson 
1257a982d89SJeremy L. Thompson   @ref Utility
1267a982d89SJeremy L. Thompson **/
1274c789ea2SJeremy L Thompson static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt field_number, bool in, const char *tabs, FILE *stream) {
1287a982d89SJeremy L. Thompson   const char  *inout = in ? "Input" : "Output";
1296f8994e9SJeremy L Thompson   const char  *field_name;
1308229195eSjeremylt   CeedInt      size;
1318229195eSjeremylt   CeedEvalMode eval_mode;
1321c66c397SJeremy L Thompson 
133ab747706SJeremy L Thompson   CeedCall(CeedQFunctionFieldGetData(field, &field_name, &size, &eval_mode));
1342b730f8bSJeremy L Thompson   fprintf(stream,
1354c789ea2SJeremy L Thompson           "%s    %s field %" CeedInt_FMT
1364c789ea2SJeremy L Thompson           ":\n%s"
1374c789ea2SJeremy L Thompson           "      Name: \"%s\"\n%s"
1382b730f8bSJeremy L Thompson           "      Size: %" CeedInt_FMT
1394c789ea2SJeremy L Thompson           "\n%s"
1407a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1414c789ea2SJeremy L Thompson           tabs, inout, field_number, tabs, field_name, tabs, size, tabs, CeedEvalModes[eval_mode]);
142e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1437a982d89SJeremy L. Thompson }
1447a982d89SJeremy L. Thompson 
145777ff853SJeremy L Thompson /**
146777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
147777ff853SJeremy L Thompson 
148ea61e9acSJeremy L Thompson   @param[in,out] qf     CeedQFunction
149ea61e9acSJeremy L Thompson   @param[in]     status Boolean value to set as Fortran status
150777ff853SJeremy L Thompson 
151777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
152777ff853SJeremy L Thompson 
153777ff853SJeremy L Thompson   @ref Backend
154777ff853SJeremy L Thompson **/
155777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
156f04ea552SJeremy L Thompson   qf->is_fortran = status;
157e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
158777ff853SJeremy L Thompson }
159777ff853SJeremy L Thompson 
1607a982d89SJeremy L. Thompson /// @}
1617a982d89SJeremy L. Thompson 
1627a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1637a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1647a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1657a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1667a982d89SJeremy L. Thompson /// @{
1677a982d89SJeremy L. Thompson 
1687a982d89SJeremy L. Thompson /**
169ca94c3ddSJeremy L Thompson   @brief Get the vector length of a `CeedQFunction`
1707a982d89SJeremy L. Thompson 
171ca94c3ddSJeremy L Thompson   @param[in]  qf         `CeedQFunction`
172d1d35e2fSjeremylt   @param[out] vec_length Variable to store vector length
1737a982d89SJeremy L. Thompson 
1747a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1757a982d89SJeremy L. Thompson 
1767a982d89SJeremy L. Thompson   @ref Backend
1777a982d89SJeremy L. Thompson **/
178d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
179d1d35e2fSjeremylt   *vec_length = qf->vec_length;
180e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1817a982d89SJeremy L. Thompson }
1827a982d89SJeremy L. Thompson 
1837a982d89SJeremy L. Thompson /**
184ca94c3ddSJeremy L Thompson   @brief Get the number of inputs and outputs to a `CeedQFunction`
1857a982d89SJeremy L. Thompson 
186ca94c3ddSJeremy L Thompson   @param[in]  qf         `CeedQFunction`
187d1d35e2fSjeremylt   @param[out] num_input  Variable to store number of input fields
188d1d35e2fSjeremylt   @param[out] num_output Variable to store number of output fields
1897a982d89SJeremy L. Thompson 
1907a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1917a982d89SJeremy L. Thompson 
1927a982d89SJeremy L. Thompson   @ref Backend
1937a982d89SJeremy L. Thompson **/
1942b730f8bSJeremy L Thompson int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, CeedInt *num_output) {
195d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
196d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
197e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1987a982d89SJeremy L. Thompson }
1997a982d89SJeremy L. Thompson 
2007a982d89SJeremy L. Thompson /**
201d3d5610dSJeremy L Thompson   @brief Get the name of the `CeedQFunction`.
202d3d5610dSJeremy L Thompson     Use the `name` if created via @ref `CeedQFunctionCreateInteriorByName`, otherwise return the kernel name via @ref `CeedQFunctionGetKernelName`.
203d3d5610dSJeremy L Thompson 
204d3d5610dSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
205d3d5610dSJeremy L Thompson   @param[out] kernel_name Variable to store `CeedQFunction` name
206d3d5610dSJeremy L Thompson 
207d3d5610dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
208d3d5610dSJeremy L Thompson 
209d3d5610dSJeremy L Thompson   @ref Backend
210d3d5610dSJeremy L Thompson **/
211d3d5610dSJeremy L Thompson int CeedQFunctionGetName(CeedQFunction qf, const char **name) {
212d3d5610dSJeremy L Thompson   if (qf->is_gallery) {
213d3d5610dSJeremy L Thompson     *name = qf->gallery_name;
214d3d5610dSJeremy L Thompson   } else {
215d3d5610dSJeremy L Thompson     CeedCall(CeedQFunctionGetKernelName(qf, name));
216d3d5610dSJeremy L Thompson   }
217d3d5610dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
218d3d5610dSJeremy L Thompson }
219d3d5610dSJeremy L Thompson 
220d3d5610dSJeremy L Thompson /**
221ca94c3ddSJeremy L Thompson   @brief Get the name of the user function for a `CeedQFunction`
2227a982d89SJeremy L. Thompson 
223ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
224d3d5610dSJeremy L Thompson   @param[out] kernel_name Variable to store string holding kernel name
2257a982d89SJeremy L. Thompson 
2267a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2277a982d89SJeremy L. Thompson 
2287a982d89SJeremy L. Thompson   @ref Backend
2297a982d89SJeremy L. Thompson **/
2307d023984SJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, const char **kernel_name) {
231ca5eadf8SJeremy L Thompson   if (!qf->kernel_name) {
232ca5eadf8SJeremy L Thompson     char *kernel_name_copy;
233ca5eadf8SJeremy L Thompson 
234ca5eadf8SJeremy L Thompson     if (qf->user_source) {
235ca5eadf8SJeremy L Thompson       const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
236ca5eadf8SJeremy L Thompson       size_t      kernel_name_len = strlen(kernel_name);
237ca5eadf8SJeremy L Thompson 
2382b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(kernel_name_len + 1, &kernel_name_copy));
239ca5eadf8SJeremy L Thompson       memcpy(kernel_name_copy, kernel_name, kernel_name_len);
240ca5eadf8SJeremy L Thompson     } else {
2412b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &kernel_name_copy));
242ca5eadf8SJeremy L Thompson     }
243ca5eadf8SJeremy L Thompson     qf->kernel_name = kernel_name_copy;
244ca5eadf8SJeremy L Thompson   }
245ca5eadf8SJeremy L Thompson 
2467d023984SJeremy L Thompson   *kernel_name = qf->kernel_name;
24743e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
24843e1b16fSJeremy L Thompson }
24943e1b16fSJeremy L Thompson 
25043e1b16fSJeremy L Thompson /**
251ca94c3ddSJeremy L Thompson   @brief Get the source path string for a `CeedQFunction`
25243e1b16fSJeremy L Thompson 
253ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
25443e1b16fSJeremy L Thompson   @param[out] source_path Variable to store source path string
25543e1b16fSJeremy L Thompson 
25643e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
25743e1b16fSJeremy L Thompson 
25843e1b16fSJeremy L Thompson   @ref Backend
25943e1b16fSJeremy L Thompson **/
26034ffed21SJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, const char **source_path) {
261ca5eadf8SJeremy L Thompson   if (!qf->source_path && qf->user_source) {
262ca5eadf8SJeremy L Thompson     Ceed        ceed;
263ca5eadf8SJeremy L Thompson     bool        is_absolute_path;
26422070f95SJeremy L Thompson     char       *source_path_copy;
26522070f95SJeremy L Thompson     const char *absolute_path;
266ca5eadf8SJeremy L Thompson     const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
267ca5eadf8SJeremy L Thompson     size_t      kernel_name_len = strlen(kernel_name);
268ca5eadf8SJeremy L Thompson 
2692b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
2702b730f8bSJeremy L Thompson     CeedCall(CeedCheckFilePath(ceed, qf->user_source, &is_absolute_path));
271ca5eadf8SJeremy L Thompson     if (is_absolute_path) {
272ca5eadf8SJeremy L Thompson       absolute_path = (char *)qf->user_source;
273ca5eadf8SJeremy L Thompson     } else {
2742b730f8bSJeremy L Thompson       CeedCall(CeedGetJitAbsolutePath(ceed, qf->user_source, &absolute_path));
275ca5eadf8SJeremy L Thompson     }
2769bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&ceed));
277ca5eadf8SJeremy L Thompson 
278ca5eadf8SJeremy L Thompson     size_t source_len = strlen(absolute_path) - kernel_name_len - 1;
2791c66c397SJeremy L Thompson 
2802b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(source_len + 1, &source_path_copy));
281ca5eadf8SJeremy L Thompson     memcpy(source_path_copy, absolute_path, source_len);
282ca5eadf8SJeremy L Thompson     qf->source_path = source_path_copy;
283ca5eadf8SJeremy L Thompson 
2842b730f8bSJeremy L Thompson     if (!is_absolute_path) CeedCall(CeedFree(&absolute_path));
285ca5eadf8SJeremy L Thompson   }
286ca5eadf8SJeremy L Thompson 
28743e1b16fSJeremy L Thompson   *source_path = (char *)qf->source_path;
288e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2897a982d89SJeremy L. Thompson }
2907a982d89SJeremy L. Thompson 
2917a982d89SJeremy L. Thompson /**
292ca94c3ddSJeremy L Thompson   @brief Initialize and load `CeedQFunction` source file into string buffer, including full text of local files in place of `#include "local.h"`.
2934385fb7fSSebastian Grimberg 
294ca94c3ddSJeremy L Thompson   The `buffer` is set to `NULL` if there is no `CeedQFunction` source file.
2954385fb7fSSebastian Grimberg 
296f8d308faSJed Brown   Note: This function may as well return a mutable buffer, but all current uses
297f8d308faSJed Brown   do not modify it. (This is just a downside of `const` semantics with output
298f8d308faSJed Brown   arguments instead of returns.)
299f8d308faSJed Brown 
300ca94c3ddSJeremy L Thompson   Note: Caller is responsible for freeing the string buffer with @ref CeedFree().
3013d3250a0SJeremy L Thompson 
302ca94c3ddSJeremy L Thompson   @param[in]  qf            `CeedQFunction`
303f74ec584SJeremy L Thompson   @param[out] source_buffer String buffer for source file contents
3043d3250a0SJeremy L Thompson 
3053d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3063d3250a0SJeremy L Thompson 
3073d3250a0SJeremy L Thompson   @ref Backend
3083d3250a0SJeremy L Thompson **/
309f8d308faSJed Brown int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, const char **source_buffer) {
31034ffed21SJeremy L Thompson   const char *source_path;
3113d3250a0SJeremy L Thompson 
3122b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetSourcePath(qf, &source_path));
3133d3250a0SJeremy L Thompson   *source_buffer = NULL;
3143d3250a0SJeremy L Thompson   if (source_path) {
3151203703bSJeremy L Thompson     Ceed  ceed;
316f8d308faSJed Brown     char *buffer = NULL;
3171203703bSJeremy L Thompson 
3181203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
319f8d308faSJed Brown     CeedCall(CeedLoadSourceToBuffer(ceed, source_path, &buffer));
3209bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&ceed));
321f8d308faSJed Brown     *source_buffer = buffer;
3223d3250a0SJeremy L Thompson   }
3233d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3243d3250a0SJeremy L Thompson }
3253d3250a0SJeremy L Thompson 
3263d3250a0SJeremy L Thompson /**
327ca94c3ddSJeremy L Thompson   @brief Get the User Function for a `CeedQFunction`
3287a982d89SJeremy L. Thompson 
329ca94c3ddSJeremy L Thompson   @param[in]  qf `CeedQFunction`
3307a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
3317a982d89SJeremy L. Thompson 
3327a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3337a982d89SJeremy L. Thompson 
3347a982d89SJeremy L. Thompson   @ref Backend
3357a982d89SJeremy L. Thompson **/
3367a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
3377a982d89SJeremy L. Thompson   *f = qf->function;
338e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3397a982d89SJeremy L. Thompson }
3407a982d89SJeremy L. Thompson 
3417a982d89SJeremy L. Thompson /**
342ca94c3ddSJeremy L Thompson   @brief Get global context for a `CeedQFunction`.
3434385fb7fSSebastian Grimberg 
344ca94c3ddSJeremy L Thompson   Note: For `CeedQFunction` from the Fortran interface, this function will return the Fortran context `CeedQFunctionContext`.
3457a982d89SJeremy L. Thompson 
346ea61e9acSJeremy L Thompson   @param[in]  qf  CeedQFunction
347777ff853SJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
3487a982d89SJeremy L. Thompson 
3497a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3507a982d89SJeremy L. Thompson 
3517a982d89SJeremy L. Thompson   @ref Backend
3527a982d89SJeremy L. Thompson **/
353777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
3541485364cSJeremy L Thompson   *ctx = NULL;
3551485364cSJeremy L Thompson   if (qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(qf->ctx, ctx));
356e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3577a982d89SJeremy L. Thompson }
3587a982d89SJeremy L. Thompson 
3597a982d89SJeremy L. Thompson /**
360ca94c3ddSJeremy L Thompson   @brief Get context data of a `CeedQFunction`
361441428dfSJeremy L Thompson 
362ca94c3ddSJeremy L Thompson   @param[in]  qf       `CeedQFunction`
363ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
364ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
365441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
366441428dfSJeremy L Thompson 
367441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
368441428dfSJeremy L Thompson 
369441428dfSJeremy L Thompson   @ref Backend
370441428dfSJeremy L Thompson **/
3712b730f8bSJeremy L Thompson int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
372441428dfSJeremy L Thompson   bool                 is_writable;
373441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
374441428dfSJeremy L Thompson 
3752b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
376441428dfSJeremy L Thompson   if (ctx) {
3772b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
378441428dfSJeremy L Thompson     if (is_writable) {
3792b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
380441428dfSJeremy L Thompson     } else {
3812b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
382441428dfSJeremy L Thompson     }
383441428dfSJeremy L Thompson   } else {
384441428dfSJeremy L Thompson     *(void **)data = NULL;
385441428dfSJeremy L Thompson   }
3861485364cSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx));
387441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
388441428dfSJeremy L Thompson }
389441428dfSJeremy L Thompson 
390441428dfSJeremy L Thompson /**
391ca94c3ddSJeremy L Thompson   @brief Restore context data of a `CeedQFunction`
392441428dfSJeremy L Thompson 
393ca94c3ddSJeremy L Thompson   @param[in]     qf   `CeedQFunction`
394ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
395441428dfSJeremy L Thompson 
396441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
397441428dfSJeremy L Thompson 
398441428dfSJeremy L Thompson   @ref Backend
399441428dfSJeremy L Thompson **/
400441428dfSJeremy L Thompson int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) {
401441428dfSJeremy L Thompson   bool                 is_writable;
402441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
403441428dfSJeremy L Thompson 
4042b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
405441428dfSJeremy L Thompson   if (ctx) {
4062b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
407441428dfSJeremy L Thompson     if (is_writable) {
4082b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
409441428dfSJeremy L Thompson     } else {
4102b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
411441428dfSJeremy L Thompson     }
412441428dfSJeremy L Thompson   }
4131485364cSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx));
414441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
415441428dfSJeremy L Thompson }
416441428dfSJeremy L Thompson 
417441428dfSJeremy L Thompson /**
418ca94c3ddSJeremy L Thompson   @brief Get true user context for a `CeedQFunction`
4194385fb7fSSebastian Grimberg 
420ca94c3ddSJeremy L Thompson   Note: For all `CeedQFunction` this function will return the user `CeedQFunctionContext` and not interface context `CeedQFunctionContext`, if any such object exists.
4217a982d89SJeremy L. Thompson 
422ca94c3ddSJeremy L Thompson   @param[in]  qf  `CeedQFunction`
423ca94c3ddSJeremy L Thompson   @param[out] ctx Variable to store `CeedQFunctionContext`
4247a982d89SJeremy L. Thompson 
4257a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4267a982d89SJeremy L. Thompson   @ref Backend
4277a982d89SJeremy L. Thompson **/
428777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
4291203703bSJeremy L Thompson   CeedQFunctionContext qf_ctx;
4301203703bSJeremy L Thompson 
4311203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &qf_ctx));
432f04ea552SJeremy L Thompson   if (qf->is_fortran) {
433d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
4341c66c397SJeremy L Thompson 
4351203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetData(qf_ctx, CEED_MEM_HOST, &fortran_ctx));
4368cb0412aSJeremy L Thompson     *ctx = fortran_ctx->inner_ctx;
4379a25c351SJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreData(qf_ctx, &fortran_ctx));
4387a982d89SJeremy L. Thompson   } else {
4391203703bSJeremy L Thompson     *ctx = qf_ctx;
4407a982d89SJeremy L. Thompson   }
4411485364cSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&qf_ctx));
442e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4437a982d89SJeremy L. Thompson }
4447a982d89SJeremy L. Thompson 
4457a982d89SJeremy L. Thompson /**
446ca94c3ddSJeremy L Thompson   @brief Get inner context data of a `CeedQFunction`
447441428dfSJeremy L Thompson 
448ca94c3ddSJeremy L Thompson   @param[in]  qf       `CeedQFunction`
449ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
450ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
451441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
452441428dfSJeremy L Thompson 
453441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
454441428dfSJeremy L Thompson 
455441428dfSJeremy L Thompson   @ref Backend
456441428dfSJeremy L Thompson **/
4572b730f8bSJeremy L Thompson int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
458441428dfSJeremy L Thompson   bool                 is_writable;
459441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
460441428dfSJeremy L Thompson 
4612b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
462441428dfSJeremy L Thompson   if (ctx) {
4632b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
464441428dfSJeremy L Thompson     if (is_writable) {
4652b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
466441428dfSJeremy L Thompson     } else {
4672b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
468441428dfSJeremy L Thompson     }
469441428dfSJeremy L Thompson   } else {
470441428dfSJeremy L Thompson     *(void **)data = NULL;
471441428dfSJeremy L Thompson   }
472441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
473441428dfSJeremy L Thompson }
474441428dfSJeremy L Thompson 
475441428dfSJeremy L Thompson /**
476ca94c3ddSJeremy L Thompson   @brief Restore inner context data of a `CeedQFunction`
477441428dfSJeremy L Thompson 
478ca94c3ddSJeremy L Thompson   @param[in]     qf   `CeedQFunction`
479ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
480441428dfSJeremy L Thompson 
481441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
482441428dfSJeremy L Thompson 
483441428dfSJeremy L Thompson   @ref Backend
484441428dfSJeremy L Thompson **/
485441428dfSJeremy L Thompson int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) {
486441428dfSJeremy L Thompson   bool                 is_writable;
487441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
488441428dfSJeremy L Thompson 
4892b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
490441428dfSJeremy L Thompson   if (ctx) {
4912b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
492441428dfSJeremy L Thompson     if (is_writable) {
4932b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
494441428dfSJeremy L Thompson     } else {
4952b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
496441428dfSJeremy L Thompson     }
497441428dfSJeremy L Thompson   }
4985f249b39SJeremy L Thompson   *(void **)data = NULL;
499441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
500441428dfSJeremy L Thompson }
501441428dfSJeremy L Thompson 
502441428dfSJeremy L Thompson /**
503ca94c3ddSJeremy L Thompson   @brief Determine if `CeedQFunction` is identity
5047a982d89SJeremy L. Thompson 
505ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
506d1d35e2fSjeremylt   @param[out] is_identity Variable to store identity status
5077a982d89SJeremy L. Thompson 
5087a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5097a982d89SJeremy L. Thompson 
5107a982d89SJeremy L. Thompson   @ref Backend
5117a982d89SJeremy L. Thompson **/
512d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
513f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
514e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5157a982d89SJeremy L. Thompson }
5167a982d89SJeremy L. Thompson 
5177a982d89SJeremy L. Thompson /**
518ca94c3ddSJeremy L Thompson   @brief Determine if `CeedQFunctionContext` is writable
519441428dfSJeremy L Thompson 
520ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
521ea61e9acSJeremy L Thompson   @param[out] is_writable Variable to store context writeable status
522441428dfSJeremy L Thompson 
523441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
524441428dfSJeremy L Thompson 
525441428dfSJeremy L Thompson   @ref Backend
526441428dfSJeremy L Thompson **/
527441428dfSJeremy L Thompson int CeedQFunctionIsContextWritable(CeedQFunction qf, bool *is_writable) {
528441428dfSJeremy L Thompson   *is_writable = qf->is_context_writable;
529441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
530441428dfSJeremy L Thompson }
531441428dfSJeremy L Thompson 
532441428dfSJeremy L Thompson /**
533ca94c3ddSJeremy L Thompson   @brief Get backend data of a `CeedQFunction`
5347a982d89SJeremy L. Thompson 
535ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction`
5367a982d89SJeremy L. Thompson   @param[out] data Variable to store data
5377a982d89SJeremy L. Thompson 
5387a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5397a982d89SJeremy L. Thompson 
5407a982d89SJeremy L. Thompson   @ref Backend
5417a982d89SJeremy L. Thompson **/
542777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
543777ff853SJeremy L Thompson   *(void **)data = qf->data;
544e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5457a982d89SJeremy L. Thompson }
5467a982d89SJeremy L. Thompson 
5477a982d89SJeremy L. Thompson /**
548ca94c3ddSJeremy L Thompson   @brief Set backend data of a `CeedQFunction`
5497a982d89SJeremy L. Thompson 
550ca94c3ddSJeremy L Thompson   @param[in,out] qf   `CeedQFunction`
551ea61e9acSJeremy L Thompson   @param[in]     data Data to set
5527a982d89SJeremy L. Thompson 
5537a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5547a982d89SJeremy L. Thompson 
5557a982d89SJeremy L. Thompson   @ref Backend
5567a982d89SJeremy L. Thompson **/
557777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
558777ff853SJeremy L Thompson   qf->data = data;
559e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5607a982d89SJeremy L. Thompson }
5617a982d89SJeremy L. Thompson 
5627a982d89SJeremy L. Thompson /**
5631203703bSJeremy L Thompson   @brief Get a boolean value indicating if the `CeedQFunction` is immutable
5641203703bSJeremy L Thompson 
5651203703bSJeremy L Thompson   @param[in]  qf           `CeedOperator`
5661203703bSJeremy L Thompson   @param[out] is_immutable Variable to store immutability status
5671203703bSJeremy L Thompson 
5681203703bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5691203703bSJeremy L Thompson 
5701203703bSJeremy L Thompson   @ref Backend
5711203703bSJeremy L Thompson **/
5721203703bSJeremy L Thompson int CeedQFunctionIsImmutable(CeedQFunction qf, bool *is_immutable) {
5731203703bSJeremy L Thompson   *is_immutable = qf->is_immutable;
5741203703bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
5751203703bSJeremy L Thompson }
5761203703bSJeremy L Thompson 
5771203703bSJeremy L Thompson /**
5781203703bSJeremy L Thompson   @brief Set the immutable flag of a `CeedQFunction` to `true`
5791203703bSJeremy L Thompson 
5801203703bSJeremy L Thompson   @param[in,out] qf `CeedQFunction`
5811203703bSJeremy L Thompson 
5821203703bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5831203703bSJeremy L Thompson 
5841203703bSJeremy L Thompson   @ref Backend
5851203703bSJeremy L Thompson **/
5861203703bSJeremy L Thompson int CeedQFunctionSetImmutable(CeedQFunction qf) {
5871203703bSJeremy L Thompson   qf->is_immutable = true;
5881203703bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
5891203703bSJeremy L Thompson }
5901203703bSJeremy L Thompson 
5911203703bSJeremy L Thompson /**
592ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedQFunction`
59334359f16Sjeremylt 
594ca94c3ddSJeremy L Thompson   @param[in,out] qf `CeedQFunction` to increment the reference counter
59534359f16Sjeremylt 
59634359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
59734359f16Sjeremylt 
59834359f16Sjeremylt   @ref Backend
59934359f16Sjeremylt **/
6009560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
60134359f16Sjeremylt   qf->ref_count++;
60234359f16Sjeremylt   return CEED_ERROR_SUCCESS;
60334359f16Sjeremylt }
60434359f16Sjeremylt 
6056e15d496SJeremy L Thompson /**
606ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs per quadrature required to apply `CeedQFunction`
6076e15d496SJeremy L Thompson 
608ca94c3ddSJeremy L Thompson   @param[in]  qf    `CeedQFunction` to estimate FLOPs for
609ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
6106e15d496SJeremy L Thompson 
6116e15d496SJeremy L Thompson   @ref Backend
6126e15d496SJeremy L Thompson **/
6139d36ca50SJeremy L Thompson int CeedQFunctionGetFlopsEstimate(CeedQFunction qf, CeedSize *flops) {
6146e15d496SJeremy L Thompson   *flops = qf->user_flop_estimate;
6156e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6166e15d496SJeremy L Thompson }
6176e15d496SJeremy L Thompson 
6187a982d89SJeremy L. Thompson /// @}
6197a982d89SJeremy L. Thompson 
6207a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6217a982d89SJeremy L. Thompson /// CeedQFunction Public API
6227a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6237a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
6247a982d89SJeremy L. Thompson /// @{
6257a982d89SJeremy L. Thompson 
6267a982d89SJeremy L. Thompson /**
627ca94c3ddSJeremy L Thompson   @brief Create a `CeedQFunction` for evaluating interior (volumetric) terms
6287a982d89SJeremy L. Thompson 
629ca94c3ddSJeremy L Thompson   @param[in]  ceed       `Ceed` object used to create the `CeedQFunction`
630ca94c3ddSJeremy L Thompson   @param[in]  vec_length Vector length.
631ca94c3ddSJeremy L Thompson                            Caller must ensure that number of quadrature points is a multiple of `vec_length`.
632ea61e9acSJeremy L Thompson   @param[in]  f          Function pointer to evaluate action at quadrature points.
633ca94c3ddSJeremy L Thompson                            See `CeedQFunctionUser`.
634ca94c3ddSJeremy L Thompson   @param[in]  source     Absolute path to source of `CeedQFunctionUser`, "\abs_path\file.h:function_name".
635ca94c3ddSJeremy L Thompson                            The entire source file must only contain constructs supported by all targeted backends (i.e. CUDA for `/gpu/cuda`, OpenCL/SYCL for `/gpu/sycl`, etc.).
6364ada38baSJames Wright                            The entire contents of this file and all locally included files are used during JiT compilation for GPU backends.
637c0b5abf0SJeremy L Thompson                            The header `ceed/types.h` is preferred over `ceed.h` or `ceed/ceed.h` for `CeedQFunction` source files.
638c0b5abf0SJeremy L Thompson                            The macro `CEED_RUNNING_JIT_PASS` is set during JiT and can be used to guard include statements that JiT compilers cannot use, such as `math.h` or `std*.h`.
6394ada38baSJames Wright                            All source files must be at the provided filepath at runtime for JiT to function.
640ca94c3ddSJeremy L Thompson   @param[out] qf         Address of the variable where the newly created `CeedQFunction` will be stored
6417a982d89SJeremy L. Thompson 
6427a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6437a982d89SJeremy L. Thompson 
644ca94c3ddSJeremy L Thompson   See \ref CeedQFunctionUser for details on the call-back function `f` arguments.
6457a982d89SJeremy L. Thompson 
6467a982d89SJeremy L. Thompson   @ref User
6477a982d89SJeremy L. Thompson **/
6482b730f8bSJeremy L Thompson int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length, CeedQFunctionUser f, const char *source, CeedQFunction *qf) {
649ca5eadf8SJeremy L Thompson   char *user_source_copy;
6507a982d89SJeremy L. Thompson 
6517a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
6527a982d89SJeremy L. Thompson     Ceed delegate;
6536574a04fSJeremy L Thompson 
6542b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "QFunction"));
6551ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedQFunctionCreateInterior");
6562b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf));
6579bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
658e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
6597a982d89SJeremy L. Thompson   }
6607a982d89SJeremy L. Thompson 
6616574a04fSJeremy L Thompson   CeedCheck(!strlen(source) || strrchr(source, ':'), ceed, CEED_ERROR_INCOMPLETE,
6626574a04fSJeremy L Thompson             "Provided path to source does not include function name. Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"", source);
66343e1b16fSJeremy L Thompson 
6642b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, qf));
665db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*qf)->ceed));
666d1d35e2fSjeremylt   (*qf)->ref_count           = 1;
667d1d35e2fSjeremylt   (*qf)->vec_length          = vec_length;
668f04ea552SJeremy L Thompson   (*qf)->is_identity         = false;
669441428dfSJeremy L Thompson   (*qf)->is_context_writable = true;
6707a982d89SJeremy L. Thompson   (*qf)->function            = f;
6716e15d496SJeremy L Thompson   (*qf)->user_flop_estimate  = -1;
67243e1b16fSJeremy L Thompson   if (strlen(source)) {
673ca5eadf8SJeremy L Thompson     size_t user_source_len = strlen(source);
674ee5a26f2SJeremy L Thompson 
6752b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(user_source_len + 1, &user_source_copy));
676ca5eadf8SJeremy L Thompson     memcpy(user_source_copy, source, user_source_len);
677ca5eadf8SJeremy L Thompson     (*qf)->user_source = user_source_copy;
67843e1b16fSJeremy L Thompson   }
6792b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields));
6802b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields));
6812b730f8bSJeremy L Thompson   CeedCall(ceed->QFunctionCreate(*qf));
682e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6837a982d89SJeremy L. Thompson }
6847a982d89SJeremy L. Thompson 
6857a982d89SJeremy L. Thompson /**
686ca94c3ddSJeremy L Thompson   @brief Create a `CeedQFunction` for evaluating interior (volumetric) terms by name
687288c0443SJeremy L Thompson 
688ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedQFunction`
689ca94c3ddSJeremy L Thompson   @param[in]  name Name of `CeedQFunction` to use from gallery
690ca94c3ddSJeremy L Thompson   @param[out] qf   Address of the variable where the newly created `CeedQFunction` will be stored
691288c0443SJeremy L Thompson 
692288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
693288c0443SJeremy L Thompson 
6947a982d89SJeremy L. Thompson   @ref User
695288c0443SJeremy L Thompson **/
6962b730f8bSJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, CeedQFunction *qf) {
697f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
698288c0443SJeremy L Thompson 
6992b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionRegisterAll());
700288c0443SJeremy L Thompson   // Find matching backend
701ca94c3ddSJeremy L Thompson   CeedCheck(name, ceed, CEED_ERROR_INCOMPLETE, "No CeedQFunction name provided");
702288c0443SJeremy L Thompson   for (size_t i = 0; i < num_qfunctions; i++) {
703288c0443SJeremy L Thompson     size_t      n;
704d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
7052b730f8bSJeremy L Thompson     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {
7062b730f8bSJeremy L Thompson     }
707d1d35e2fSjeremylt     if (n > match_len) {
708d1d35e2fSjeremylt       match_len   = n;
709f7e22acaSJeremy L Thompson       match_index = i;
710288c0443SJeremy L Thompson     }
711288c0443SJeremy L Thompson   }
712ca94c3ddSJeremy L Thompson   CeedCheck(match_len > 0, ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery CeedQFunction");
713288c0443SJeremy L Thompson 
714288c0443SJeremy L Thompson   // Create QFunction
7152b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInterior(ceed, gallery_qfunctions[match_index].vec_length, gallery_qfunctions[match_index].f,
7162b730f8bSJeremy L Thompson                                        gallery_qfunctions[match_index].source, qf));
717288c0443SJeremy L Thompson 
718288c0443SJeremy L Thompson   // QFunction specific setup
7192b730f8bSJeremy L Thompson   CeedCall(gallery_qfunctions[match_index].init(ceed, name, *qf));
720288c0443SJeremy L Thompson 
72175affc3bSjeremylt   // Copy name
7222b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name));
72343e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
724e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
725288c0443SJeremy L Thompson }
726288c0443SJeremy L Thompson 
727288c0443SJeremy L Thompson /**
728ca94c3ddSJeremy L Thompson   @brief Create an identity `CeedQFunction`.
7294385fb7fSSebastian Grimberg 
730ea61e9acSJeremy L Thompson   Inputs are written into outputs in the order given.
731ca94c3ddSJeremy L Thompson   This is useful for `CeedOperator that can be represented with only the action of a `CeedElemRestriction` and `CeedBasis`, such as restriction and prolongation operators for p-multigrid.
732ca94c3ddSJeremy L Thompson   Backends may optimize `CeedOperator` with this `CeedQFunction` to avoid the copy of input data to output fields by using the same memory location for both.
7330219ea01SJeremy L Thompson 
734ca94c3ddSJeremy L Thompson   @param[in]  ceed     `Ceed` object used to create the `CeedQFunction`
735ca94c3ddSJeremy L Thompson   @param[in]  size     Size of the `CeedQFunction` fields
736ca94c3ddSJeremy L Thompson   @param[in]  in_mode  @ref CeedEvalMode for input to `CeedQFunction`
737ca94c3ddSJeremy L Thompson   @param[in]  out_mode @ref CeedEvalMode for output to `CeedQFunction`
738ca94c3ddSJeremy L Thompson   @param[out] qf       Address of the variable where the newly created `CeedQFunction` will be stored
7390219ea01SJeremy L Thompson 
7400219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
7410219ea01SJeremy L Thompson 
7427a982d89SJeremy L. Thompson   @ref User
7430219ea01SJeremy L Thompson **/
7442b730f8bSJeremy L Thompson int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, CeedEvalMode out_mode, CeedQFunction *qf) {
7451c66c397SJeremy L Thompson   CeedQFunctionContext  ctx;
7461c66c397SJeremy L Thompson   CeedContextFieldLabel size_label;
7471c66c397SJeremy L Thompson 
7482b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Identity", qf));
7492b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(*qf, "input", size, in_mode));
7502b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(*qf, "output", size, out_mode));
7510219ea01SJeremy L Thompson 
752f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
753547dbd6fSJeremy L Thompson 
7542b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(*qf, &ctx));
7552b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label));
7562b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetInt32(ctx, size_label, &size));
7571485364cSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx));
758e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7590219ea01SJeremy L Thompson }
7600219ea01SJeremy L Thompson 
7610219ea01SJeremy L Thompson /**
762ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedQFunction`.
7634385fb7fSSebastian Grimberg 
764ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedQFunctionDestroy().
765512bb800SJeremy L Thompson 
766ca94c3ddSJeremy L Thompson   Note: If the value of `*qf_copy` passed to this function is non-NULL, then it is assumed that `*qf_copy` is a pointer to a `CeedQFunction`.
767ca94c3ddSJeremy L Thompson         This `CeedQFunction` will be destroyed if `*qf_copy` is the only reference to this `CeedQFunction`.
7689560d06aSjeremylt 
769ca94c3ddSJeremy L Thompson   @param[in]  qf      `CeedQFunction` to copy reference to
7709560d06aSjeremylt   @param[out] qf_copy Variable to store copied reference
7719560d06aSjeremylt 
7729560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7739560d06aSjeremylt 
7749560d06aSjeremylt   @ref User
7759560d06aSjeremylt **/
7769560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
7772b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionReference(qf));
7782b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(qf_copy));
7799560d06aSjeremylt   *qf_copy = qf;
7809560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7819560d06aSjeremylt }
7829560d06aSjeremylt 
7839560d06aSjeremylt /**
784ca94c3ddSJeremy L Thompson   @brief Add a `CeedQFunction` input
785b11c1e72Sjeremylt 
786ca94c3ddSJeremy L Thompson   @param[in,out] qf         `CeedQFunction`
787ca94c3ddSJeremy L Thompson   @param[in]     field_name Name of `CeedQFunction` field
788d538d163SJeremy L Thompson   @param[in]     size       Size of `CeedQFunction` field,
789d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_NONE,
790d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_INTERP for an \f$H^1\f$ space or (`num_comp * dim`) for an \f$H(\mathrm{div})\f$ or \f$H(\mathrm{curl})\f$ space,
791d538d163SJeremy L Thompson                               (`num_comp * dim`) for @ref CEED_EVAL_GRAD,
792d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_DIV, and
793d538d163SJeremy L Thompson                               (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` otherwise `curl_dim = dim` for @ref CEED_EVAL_CURL.
794ca94c3ddSJeremy L Thompson   @param[in]     eval_mode  @ref CEED_EVAL_NONE to use values directly,
795ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_INTERP to use interpolated values,
796ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_GRAD to use gradients,
797ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_DIV to use divergence,
798ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_CURL to use curl
799b11c1e72Sjeremylt 
800d538d163SJeremy L Thompson   Note: In the user `CeedQFunctionUser`, the `in` argument list the fields in the order given by the calls to `CeedQFunctionAddInput`.
801d538d163SJeremy L Thompson 
802b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
803dfdf5a53Sjeremylt 
8047a982d89SJeremy L. Thompson   @ref User
805b11c1e72Sjeremylt **/
8062b730f8bSJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
8071203703bSJeremy L Thompson   bool is_immutable;
8081203703bSJeremy L Thompson 
8091203703bSJeremy L Thompson   CeedCall(CeedQFunctionIsImmutable(qf, &is_immutable));
8109bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedQFunctionReturnCeed(qf), CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
8119bc66399SJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT || size == 1, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION, "CEED_EVAL_WEIGHT should have size 1");
812643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
8139bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8143f08121cSJeremy L Thompson               "CeedQFunction field names must be unique. Duplicate name: %s", field_name);
815643fbb69SJeremy L Thompson   }
816643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
8179bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8183f08121cSJeremy L Thompson               "CeedQFunction field names must be unique. Duplicate name: %s", field_name);
819643fbb69SJeremy L Thompson   }
8202b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], field_name, size, eval_mode));
821d1d35e2fSjeremylt   qf->num_input_fields++;
822e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
823d7b241e6Sjeremylt }
824d7b241e6Sjeremylt 
825b11c1e72Sjeremylt /**
826ca94c3ddSJeremy L Thompson   @brief Add a `CeedQFunction` output
827b11c1e72Sjeremylt 
828ca94c3ddSJeremy L Thompson   @param[in,out] qf         `CeedQFunction`
829ca94c3ddSJeremy L Thompson   @param[in]     field_name Name of `CeedQFunction` field
830d538d163SJeremy L Thompson   @param[in]     size       Size of `CeedQFunction` field,
831d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_NONE,
832d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_INTERP for an \f$H^1\f$ space or (`num_comp * dim`) for an \f$H(\mathrm{div})\f$ or \f$H(\mathrm{curl})\f$ space,
833d538d163SJeremy L Thompson                               (`num_comp * dim`) for @ref CEED_EVAL_GRAD,
834d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_DIV, and
835d538d163SJeremy L Thompson                               (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` otherwise `curl_dim = dim` for @ref CEED_EVAL_CURL.
836ca94c3ddSJeremy L Thompson   @param[in]     eval_mode  @ref CEED_EVAL_NONE to use values directly,
837ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_INTERP to use interpolated values,
838ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_GRAD to use gradients,
839ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_DIV to use divergence,
840ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_CURL to use curl.
841b11c1e72Sjeremylt 
842d538d163SJeremy L Thompson   Note: In the user `CeedQFunctionUser`, the `out` argument list the fields in the order given by the calls to `CeedQFunctionAddOutput`.
843d538d163SJeremy L Thompson 
844b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
845dfdf5a53Sjeremylt 
8467a982d89SJeremy L. Thompson   @ref User
847b11c1e72Sjeremylt **/
8482b730f8bSJeremy L Thompson int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
8491203703bSJeremy L Thompson   bool is_immutable;
8501203703bSJeremy L Thompson 
8511203703bSJeremy L Thompson   CeedCall(CeedQFunctionIsImmutable(qf, &is_immutable));
8529bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedQFunctionReturnCeed(qf), CEED_ERROR_MAJOR, "CeedQFunction cannot be changed after set as immutable");
8539bc66399SJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION,
8549bc66399SJeremy L Thompson             "Cannot create CeedQFunction output with CEED_EVAL_WEIGHT");
855643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
8569bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8579bc66399SJeremy L Thompson               "CeedQFunction field names must be unique");
858643fbb69SJeremy L Thompson   }
859643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
8609bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8619bc66399SJeremy L Thompson               "CeedQFunction field names must be unique");
862643fbb69SJeremy L Thompson   }
8632b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], field_name, size, eval_mode));
864d1d35e2fSjeremylt   qf->num_output_fields++;
865e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
866d7b241e6Sjeremylt }
867d7b241e6Sjeremylt 
868dfdf5a53Sjeremylt /**
869ca94c3ddSJeremy L Thompson   @brief Get the `CeedQFunctionField` of a `CeedQFunction`
87043bbe138SJeremy L Thompson 
871ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedQFunction` as immutable.
872f04ea552SJeremy L Thompson 
873ca94c3ddSJeremy L Thompson   @param[in]  qf                `CeedQFunction`
874f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
875f74ec584SJeremy L Thompson   @param[out] input_fields      Variable to store input fields
876f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
877f74ec584SJeremy L Thompson   @param[out] output_fields     Variable to store output fields
87843bbe138SJeremy L Thompson 
87943bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
88043bbe138SJeremy L Thompson 
881e9b533fbSJeremy L Thompson   @ref Advanced
88243bbe138SJeremy L Thompson **/
8832b730f8bSJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields, CeedQFunctionField **input_fields, CeedInt *num_output_fields,
88443bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
8851203703bSJeremy L Thompson   CeedCall(CeedQFunctionSetImmutable(qf));
88643bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
88743bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
88843bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
88943bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
89043bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
89143bbe138SJeremy L Thompson }
89243bbe138SJeremy L Thompson 
89343bbe138SJeremy L Thompson /**
894ca94c3ddSJeremy L Thompson   @brief Get the name of a `CeedQFunctionField`
89543bbe138SJeremy L Thompson 
896ca94c3ddSJeremy L Thompson   @param[in]  qf_field   `CeedQFunctionField`
89743bbe138SJeremy L Thompson   @param[out] field_name Variable to store the field name
89843bbe138SJeremy L Thompson 
89943bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
90043bbe138SJeremy L Thompson 
901e9b533fbSJeremy L Thompson   @ref Advanced
90243bbe138SJeremy L Thompson **/
9036f8994e9SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, const char **field_name) {
9046f8994e9SJeremy L Thompson   *field_name = qf_field->field_name;
90543bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
90643bbe138SJeremy L Thompson }
90743bbe138SJeremy L Thompson 
90843bbe138SJeremy L Thompson /**
909ca94c3ddSJeremy L Thompson   @brief Get the number of components of a `CeedQFunctionField`
91043bbe138SJeremy L Thompson 
911ca94c3ddSJeremy L Thompson   @param[in]  qf_field `CeedQFunctionField`
91243bbe138SJeremy L Thompson   @param[out] size     Variable to store the size of the field
91343bbe138SJeremy L Thompson 
91443bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
91543bbe138SJeremy L Thompson 
916e9b533fbSJeremy L Thompson   @ref Advanced
91743bbe138SJeremy L Thompson **/
91843bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
91943bbe138SJeremy L Thompson   *size = qf_field->size;
92043bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
92143bbe138SJeremy L Thompson }
92243bbe138SJeremy L Thompson 
92343bbe138SJeremy L Thompson /**
924ca94c3ddSJeremy L Thompson   @brief Get the @ref CeedEvalMode of a `CeedQFunctionField`
92543bbe138SJeremy L Thompson 
926ca94c3ddSJeremy L Thompson   @param[in]  qf_field  `CeedQFunctionField`
92743bbe138SJeremy L Thompson   @param[out] eval_mode Variable to store the field evaluation mode
92843bbe138SJeremy L Thompson 
92943bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
93043bbe138SJeremy L Thompson 
931e9b533fbSJeremy L Thompson   @ref Advanced
93243bbe138SJeremy L Thompson **/
9332b730f8bSJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, CeedEvalMode *eval_mode) {
93443bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
93543bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
93643bbe138SJeremy L Thompson }
93743bbe138SJeremy L Thompson 
93843bbe138SJeremy L Thompson /**
939ab747706SJeremy L Thompson   @brief Get the data of a `CeedQFunctionField`.
940ab747706SJeremy L Thompson 
941ab747706SJeremy L Thompson   Any arguments set as `NULL` are ignored.
942ab747706SJeremy L Thompson 
943ab747706SJeremy L Thompson   @param[in]  qf_field   `CeedQFunctionField`
944ab747706SJeremy L Thompson   @param[out] field_name Variable to store the field name
945ab747706SJeremy L Thompson   @param[out] size       Variable to store the size of the field
946ab747706SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
947ab747706SJeremy L Thompson 
948ab747706SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
949ab747706SJeremy L Thompson 
950ab747706SJeremy L Thompson   @ref Advanced
951ab747706SJeremy L Thompson **/
9526f8994e9SJeremy L Thompson int CeedQFunctionFieldGetData(CeedQFunctionField qf_field, const char **field_name, CeedInt *size, CeedEvalMode *eval_mode) {
953ab747706SJeremy L Thompson   if (field_name) CeedCall(CeedQFunctionFieldGetName(qf_field, field_name));
954ab747706SJeremy L Thompson   if (size) CeedCall(CeedQFunctionFieldGetSize(qf_field, size));
955ab747706SJeremy L Thompson   if (eval_mode) CeedCall(CeedQFunctionFieldGetEvalMode(qf_field, eval_mode));
956ab747706SJeremy L Thompson   return CEED_ERROR_SUCCESS;
957ab747706SJeremy L Thompson }
958ab747706SJeremy L Thompson 
959ab747706SJeremy L Thompson /**
960ca94c3ddSJeremy L Thompson   @brief Set global context for a `CeedQFunction`
961b11c1e72Sjeremylt 
962ca94c3ddSJeremy L Thompson   @param[in,out] qf  `CeedQFunction`
963ea61e9acSJeremy L Thompson   @param[in]     ctx Context data to set
964b11c1e72Sjeremylt 
965b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
966dfdf5a53Sjeremylt 
9677a982d89SJeremy L. Thompson   @ref User
968b11c1e72Sjeremylt **/
969777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
9702b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&qf->ctx));
971d7b241e6Sjeremylt   qf->ctx = ctx;
972db002c03SJeremy L Thompson   if (ctx) CeedCall(CeedQFunctionContextReference(ctx));
973e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
974d7b241e6Sjeremylt }
975d7b241e6Sjeremylt 
976b11c1e72Sjeremylt /**
977ca94c3ddSJeremy L Thompson   @brief Set writability of `CeedQFunctionContext` when calling the `CeedQFunctionUser`.
9784385fb7fSSebastian Grimberg 
979859c15bbSJames Wright   The default value is `is_writable == true`.
980441428dfSJeremy L Thompson 
981ca94c3ddSJeremy L Thompson   Setting `is_writable == true` indicates the `CeedQFunctionUser` writes into the `CeedQFunctionContext` and requires memory synchronization after calling @ref CeedQFunctionApply().
982441428dfSJeremy L Thompson 
983ca94c3ddSJeremy L Thompson   Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not modify the `CeedQFunctionContext`.
984ea61e9acSJeremy L Thompson   Violating this assertion may lead to inconsistent data.
985441428dfSJeremy L Thompson 
986441428dfSJeremy L Thompson   Setting `is_writable == false` may offer a performance improvement on GPU backends.
987441428dfSJeremy L Thompson 
988ca94c3ddSJeremy L Thompson   @param[in,out] qf          `CeedQFunction`
989ca94c3ddSJeremy L Thompson   @param[in]     is_writable Boolean flag for writability status
990441428dfSJeremy L Thompson 
991441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
992441428dfSJeremy L Thompson 
993441428dfSJeremy L Thompson   @ref User
994441428dfSJeremy L Thompson **/
995441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
996441428dfSJeremy L Thompson   qf->is_context_writable = is_writable;
997441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
998441428dfSJeremy L Thompson }
999441428dfSJeremy L Thompson 
1000441428dfSJeremy L Thompson /**
1001ca94c3ddSJeremy L Thompson   @brief Set estimated number of FLOPs per quadrature required to apply `CeedQFunction`
10026e15d496SJeremy L Thompson 
1003ca94c3ddSJeremy L Thompson   @param[in]  qf    `CeedQFunction` to estimate FLOPs for
1004ea61e9acSJeremy L Thompson   @param[out] flops FLOPs per quadrature point estimate
10056e15d496SJeremy L Thompson 
10066e15d496SJeremy L Thompson   @ref Backend
10076e15d496SJeremy L Thompson **/
10089d36ca50SJeremy L Thompson int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
10096e536b99SJeremy L Thompson   CeedCheck(flops >= 0, CeedQFunctionReturnCeed(qf), CEED_ERROR_INCOMPATIBLE, "Must set non-negative FLOPs estimate");
10106e15d496SJeremy L Thompson   qf->user_flop_estimate = flops;
10116e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10126e15d496SJeremy L Thompson }
10136e15d496SJeremy L Thompson 
10146e15d496SJeremy L Thompson /**
10154c789ea2SJeremy L Thompson   @brief Set the number of tabs to indent for @ref CeedQFunctionView() output
10164c789ea2SJeremy L Thompson 
10174c789ea2SJeremy L Thompson   @param[in] qf       `CeedQFunction` to set the number of view tabs
10184c789ea2SJeremy L Thompson   @param[in] num_tabs Number of view tabs to set
10194c789ea2SJeremy L Thompson 
10204c789ea2SJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
10214c789ea2SJeremy L Thompson 
10224c789ea2SJeremy L Thompson   @ref User
10234c789ea2SJeremy L Thompson **/
10244c789ea2SJeremy L Thompson int CeedQFunctionSetNumViewTabs(CeedQFunction qf, CeedInt num_tabs) {
10254c789ea2SJeremy L Thompson   CeedCheck(num_tabs >= 0, CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR, "Number of view tabs must be non-negative");
10264c789ea2SJeremy L Thompson   qf->num_tabs = num_tabs;
10274c789ea2SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10284c789ea2SJeremy L Thompson }
10294c789ea2SJeremy L Thompson 
10304c789ea2SJeremy L Thompson /**
1031690992b2SZach Atkins   @brief Get the number of tabs to indent for @ref CeedQFunctionView() output
1032690992b2SZach Atkins 
1033690992b2SZach Atkins   @param[in]  qf       `CeedQFunction` to get the number of view tabs
1034690992b2SZach Atkins   @param[out] num_tabs Number of view tabs
1035690992b2SZach Atkins 
1036690992b2SZach Atkins   @return Error code: 0 - success, otherwise - failure
1037690992b2SZach Atkins 
1038690992b2SZach Atkins   @ref User
1039690992b2SZach Atkins **/
1040690992b2SZach Atkins int CeedQFunctionGetNumViewTabs(CeedQFunction qf, CeedInt *num_tabs) {
1041690992b2SZach Atkins   *num_tabs = qf->num_tabs;
1042690992b2SZach Atkins   return CEED_ERROR_SUCCESS;
1043690992b2SZach Atkins }
1044690992b2SZach Atkins 
1045690992b2SZach Atkins /**
1046ca94c3ddSJeremy L Thompson   @brief View a `CeedQFunction`
104775affc3bSjeremylt 
1048ca94c3ddSJeremy L Thompson   @param[in] qf     `CeedQFunction` to view
1049ca94c3ddSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
105075affc3bSjeremylt 
105175affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
105275affc3bSjeremylt 
10537a982d89SJeremy L. Thompson   @ref User
105475affc3bSjeremylt **/
105575affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
10564c789ea2SJeremy L Thompson   char       *tabs = NULL;
1057d3d5610dSJeremy L Thompson   const char *name;
105875affc3bSjeremylt 
10594c789ea2SJeremy L Thompson   {
10604c789ea2SJeremy L Thompson     CeedInt num_tabs = 0;
10614c789ea2SJeremy L Thompson 
10624c789ea2SJeremy L Thompson     CeedCall(CeedQFunctionGetNumViewTabs(qf, &num_tabs));
10634c789ea2SJeremy L Thompson     CeedCall(CeedCalloc(CEED_TAB_WIDTH * num_tabs + 1, &tabs));
10644c789ea2SJeremy L Thompson     for (CeedInt i = 0; i < CEED_TAB_WIDTH * num_tabs; i++) tabs[i] = ' ';
10654c789ea2SJeremy L Thompson   }
10664c789ea2SJeremy L Thompson 
1067d3d5610dSJeremy L Thompson   CeedCall(CeedQFunctionGetName(qf, &name));
10684c789ea2SJeremy L Thompson   fprintf(stream, "%s%sCeedQFunction - %s\n", tabs, qf->is_gallery ? "Gallery " : "User ", name);
106975affc3bSjeremylt 
10704c789ea2SJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", tabs, qf->num_input_fields, qf->num_input_fields > 1 ? "s" : "");
1071d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
10724c789ea2SJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->input_fields[i], i, 1, tabs, stream));
107375affc3bSjeremylt   }
107475affc3bSjeremylt 
10754c789ea2SJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", tabs, qf->num_output_fields, qf->num_output_fields > 1 ? "s" : "");
1076d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
10774c789ea2SJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->output_fields[i], i, 0, tabs, stream));
107875affc3bSjeremylt   }
10794c789ea2SJeremy L Thompson   CeedCall(CeedFree(&tabs));
1080e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
108175affc3bSjeremylt }
108275affc3bSjeremylt 
108375affc3bSjeremylt /**
1084ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedQFunction`
1085b7c9bbdaSJeremy L Thompson 
1086ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction`
1087ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store`Ceed`
1088b7c9bbdaSJeremy L Thompson 
1089b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1090b7c9bbdaSJeremy L Thompson 
1091b7c9bbdaSJeremy L Thompson   @ref Advanced
1092b7c9bbdaSJeremy L Thompson **/
1093b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
10949bc66399SJeremy L Thompson   *ceed = NULL;
10959bc66399SJeremy L Thompson   CeedCall(CeedReferenceCopy(CeedQFunctionReturnCeed(qf), ceed));
1096b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1097b7c9bbdaSJeremy L Thompson }
1098b7c9bbdaSJeremy L Thompson 
1099b7c9bbdaSJeremy L Thompson /**
11006e536b99SJeremy L Thompson   @brief Return the `Ceed` associated with a `CeedQFunction`
11016e536b99SJeremy L Thompson 
11026e536b99SJeremy L Thompson   @param[in]  qf   `CeedQFunction`
11036e536b99SJeremy L Thompson 
11046e536b99SJeremy L Thompson   @return `Ceed` associated with the `qf`
11056e536b99SJeremy L Thompson 
11066e536b99SJeremy L Thompson   @ref Advanced
11076e536b99SJeremy L Thompson **/
11086e536b99SJeremy L Thompson Ceed CeedQFunctionReturnCeed(CeedQFunction qf) { return qf->ceed; }
11096e536b99SJeremy L Thompson 
11106e536b99SJeremy L Thompson /**
1111ca94c3ddSJeremy L Thompson   @brief Apply the action of a `CeedQFunction`
1112b11c1e72Sjeremylt 
1113ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedQFunction` as immutable.
1114f04ea552SJeremy L Thompson 
1115ca94c3ddSJeremy L Thompson   @param[in]  qf `CeedQFunction`
1116ea61e9acSJeremy L Thompson   @param[in]  Q  Number of quadrature points
1117ca94c3ddSJeremy L Thompson   @param[in]  u  Array of input `CeedVector`
1118ca94c3ddSJeremy L Thompson   @param[out] v  Array of output `CeedVector`
1119b11c1e72Sjeremylt 
1120b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1121dfdf5a53Sjeremylt 
11227a982d89SJeremy L. Thompson   @ref User
1123b11c1e72Sjeremylt **/
11242b730f8bSJeremy L Thompson int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, CeedVector *u, CeedVector *v) {
11251203703bSJeremy L Thompson   CeedInt vec_length;
11261203703bSJeremy L Thompson 
11279bc66399SJeremy L Thompson   CeedCheck(qf->Apply, CeedQFunctionReturnCeed(qf), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionApply");
11281203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetVectorLength(qf, &vec_length));
11299bc66399SJeremy L Thompson   CeedCheck(Q % vec_length == 0, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION,
11309bc66399SJeremy L Thompson             "Number of quadrature points %" CeedInt_FMT " must be a multiple of %" CeedInt_FMT, Q, qf->vec_length);
11311203703bSJeremy L Thompson   CeedCall(CeedQFunctionSetImmutable(qf));
11322b730f8bSJeremy L Thompson   CeedCall(qf->Apply(qf, Q, u, v));
1133e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1134d7b241e6Sjeremylt }
1135d7b241e6Sjeremylt 
1136b11c1e72Sjeremylt /**
1137ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedQFunction`
1138b11c1e72Sjeremylt 
1139ca94c3ddSJeremy L Thompson   @param[in,out] qf `CeedQFunction` to destroy
1140b11c1e72Sjeremylt 
1141b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1142dfdf5a53Sjeremylt 
11437a982d89SJeremy L. Thompson   @ref User
1144b11c1e72Sjeremylt **/
1145d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
1146ad6481ceSJeremy L Thompson   if (!*qf || --(*qf)->ref_count > 0) {
1147ad6481ceSJeremy L Thompson     *qf = NULL;
1148ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1149ad6481ceSJeremy L Thompson   }
1150fe2413ffSjeremylt   // Backend destroy
1151d7b241e6Sjeremylt   if ((*qf)->Destroy) {
11522b730f8bSJeremy L Thompson     CeedCall((*qf)->Destroy(*qf));
1153d7b241e6Sjeremylt   }
1154fe2413ffSjeremylt   // Free fields
115592ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_input_fields; i++) {
11562b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->input_fields[i]).field_name));
11572b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->input_fields[i]));
1158fe2413ffSjeremylt   }
115992ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_output_fields; i++) {
11602b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->output_fields[i]).field_name));
11612b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->output_fields[i]));
1162fe2413ffSjeremylt   }
11632b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->input_fields));
11642b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->output_fields));
1165777ff853SJeremy L Thompson 
1166777ff853SJeremy L Thompson   // User context data object
11672b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&(*qf)->ctx));
1168fe2413ffSjeremylt 
11692b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->user_source));
11702b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->source_path));
11712b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->gallery_name));
11722b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->kernel_name));
11732b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*qf)->ceed));
11742b730f8bSJeremy L Thompson   CeedCall(CeedFree(qf));
1175e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1176d7b241e6Sjeremylt }
1177d7b241e6Sjeremylt 
1178d7b241e6Sjeremylt /// @}
1179