xref: /libCEED/interface/ceed-qfunction.c (revision d3d5610df460248361ab17f2fa259b4661019597)
1d275d636SJeremy L Thompson // Copyright (c) 2017-2025, 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
120ca94c3ddSJeremy L Thompson   @param[in] stream       Stream to view to, e.g., `stdout`
1217a982d89SJeremy L. Thompson 
1227a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1237a982d89SJeremy L. Thompson 
1247a982d89SJeremy L. Thompson   @ref Utility
1257a982d89SJeremy L. Thompson **/
1262b730f8bSJeremy L Thompson static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt field_number, bool in, FILE *stream) {
1277a982d89SJeremy L. Thompson   const char  *inout = in ? "Input" : "Output";
1286f8994e9SJeremy L Thompson   const char  *field_name;
1298229195eSjeremylt   CeedInt      size;
1308229195eSjeremylt   CeedEvalMode eval_mode;
1311c66c397SJeremy L Thompson 
132ab747706SJeremy L Thompson   CeedCall(CeedQFunctionFieldGetData(field, &field_name, &size, &eval_mode));
1332b730f8bSJeremy L Thompson   fprintf(stream,
1342b730f8bSJeremy L Thompson           "    %s field %" CeedInt_FMT
1352b730f8bSJeremy L Thompson           ":\n"
1367a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1372b730f8bSJeremy L Thompson           "      Size: %" CeedInt_FMT
1382b730f8bSJeremy L Thompson           "\n"
1397a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1408229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
141e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1427a982d89SJeremy L. Thompson }
1437a982d89SJeremy L. Thompson 
144777ff853SJeremy L Thompson /**
145777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
146777ff853SJeremy L Thompson 
147ea61e9acSJeremy L Thompson   @param[in,out] qf     CeedQFunction
148ea61e9acSJeremy L Thompson   @param[in]     status Boolean value to set as Fortran status
149777ff853SJeremy L Thompson 
150777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
151777ff853SJeremy L Thompson 
152777ff853SJeremy L Thompson   @ref Backend
153777ff853SJeremy L Thompson **/
154777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
155f04ea552SJeremy L Thompson   qf->is_fortran = status;
156e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
157777ff853SJeremy L Thompson }
158777ff853SJeremy L Thompson 
1597a982d89SJeremy L. Thompson /// @}
1607a982d89SJeremy L. Thompson 
1617a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1627a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1637a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1647a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1657a982d89SJeremy L. Thompson /// @{
1667a982d89SJeremy L. Thompson 
1677a982d89SJeremy L. Thompson /**
168ca94c3ddSJeremy L Thompson   @brief Get the vector length of a `CeedQFunction`
1697a982d89SJeremy L. Thompson 
170ca94c3ddSJeremy L Thompson   @param[in]  qf         `CeedQFunction`
171d1d35e2fSjeremylt   @param[out] vec_length Variable to store vector length
1727a982d89SJeremy L. Thompson 
1737a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1747a982d89SJeremy L. Thompson 
1757a982d89SJeremy L. Thompson   @ref Backend
1767a982d89SJeremy L. Thompson **/
177d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
178d1d35e2fSjeremylt   *vec_length = qf->vec_length;
179e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1807a982d89SJeremy L. Thompson }
1817a982d89SJeremy L. Thompson 
1827a982d89SJeremy L. Thompson /**
183ca94c3ddSJeremy L Thompson   @brief Get the number of inputs and outputs to a `CeedQFunction`
1847a982d89SJeremy L. Thompson 
185ca94c3ddSJeremy L Thompson   @param[in]  qf         `CeedQFunction`
186d1d35e2fSjeremylt   @param[out] num_input  Variable to store number of input fields
187d1d35e2fSjeremylt   @param[out] num_output Variable to store number of output fields
1887a982d89SJeremy L. Thompson 
1897a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1907a982d89SJeremy L. Thompson 
1917a982d89SJeremy L. Thompson   @ref Backend
1927a982d89SJeremy L. Thompson **/
1932b730f8bSJeremy L Thompson int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, CeedInt *num_output) {
194d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
195d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
196e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1977a982d89SJeremy L. Thompson }
1987a982d89SJeremy L. Thompson 
1997a982d89SJeremy L. Thompson /**
200*d3d5610dSJeremy L Thompson   @brief Get the name of the `CeedQFunction`.
201*d3d5610dSJeremy L Thompson     Use the `name` if created via @ref `CeedQFunctionCreateInteriorByName`, otherwise return the kernel name via @ref `CeedQFunctionGetKernelName`.
202*d3d5610dSJeremy L Thompson 
203*d3d5610dSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
204*d3d5610dSJeremy L Thompson   @param[out] kernel_name Variable to store `CeedQFunction` name
205*d3d5610dSJeremy L Thompson 
206*d3d5610dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
207*d3d5610dSJeremy L Thompson 
208*d3d5610dSJeremy L Thompson   @ref Backend
209*d3d5610dSJeremy L Thompson **/
210*d3d5610dSJeremy L Thompson int CeedQFunctionGetName(CeedQFunction qf, const char **name) {
211*d3d5610dSJeremy L Thompson   if (qf->is_gallery) {
212*d3d5610dSJeremy L Thompson     *name = qf->gallery_name;
213*d3d5610dSJeremy L Thompson   } else {
214*d3d5610dSJeremy L Thompson     CeedCall(CeedQFunctionGetKernelName(qf, name));
215*d3d5610dSJeremy L Thompson   }
216*d3d5610dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
217*d3d5610dSJeremy L Thompson }
218*d3d5610dSJeremy L Thompson 
219*d3d5610dSJeremy L Thompson /**
220ca94c3ddSJeremy L Thompson   @brief Get the name of the user function for a `CeedQFunction`
2217a982d89SJeremy L. Thompson 
222ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
223*d3d5610dSJeremy L Thompson   @param[out] kernel_name Variable to store string holding kernel name
2247a982d89SJeremy L. Thompson 
2257a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2267a982d89SJeremy L. Thompson 
2277a982d89SJeremy L. Thompson   @ref Backend
2287a982d89SJeremy L. Thompson **/
2297d023984SJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, const char **kernel_name) {
230ca5eadf8SJeremy L Thompson   if (!qf->kernel_name) {
231ca5eadf8SJeremy L Thompson     char *kernel_name_copy;
232ca5eadf8SJeremy L Thompson 
233ca5eadf8SJeremy L Thompson     if (qf->user_source) {
234ca5eadf8SJeremy L Thompson       const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
235ca5eadf8SJeremy L Thompson       size_t      kernel_name_len = strlen(kernel_name);
236ca5eadf8SJeremy L Thompson 
2372b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(kernel_name_len + 1, &kernel_name_copy));
238ca5eadf8SJeremy L Thompson       memcpy(kernel_name_copy, kernel_name, kernel_name_len);
239ca5eadf8SJeremy L Thompson     } else {
2402b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &kernel_name_copy));
241ca5eadf8SJeremy L Thompson     }
242ca5eadf8SJeremy L Thompson     qf->kernel_name = kernel_name_copy;
243ca5eadf8SJeremy L Thompson   }
244ca5eadf8SJeremy L Thompson 
2457d023984SJeremy L Thompson   *kernel_name = qf->kernel_name;
24643e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
24743e1b16fSJeremy L Thompson }
24843e1b16fSJeremy L Thompson 
24943e1b16fSJeremy L Thompson /**
250ca94c3ddSJeremy L Thompson   @brief Get the source path string for a `CeedQFunction`
25143e1b16fSJeremy L Thompson 
252ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
25343e1b16fSJeremy L Thompson   @param[out] source_path Variable to store source path string
25443e1b16fSJeremy L Thompson 
25543e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
25643e1b16fSJeremy L Thompson 
25743e1b16fSJeremy L Thompson   @ref Backend
25843e1b16fSJeremy L Thompson **/
25934ffed21SJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, const char **source_path) {
260ca5eadf8SJeremy L Thompson   if (!qf->source_path && qf->user_source) {
261ca5eadf8SJeremy L Thompson     Ceed        ceed;
262ca5eadf8SJeremy L Thompson     bool        is_absolute_path;
26322070f95SJeremy L Thompson     char       *source_path_copy;
26422070f95SJeremy L Thompson     const char *absolute_path;
265ca5eadf8SJeremy L Thompson     const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
266ca5eadf8SJeremy L Thompson     size_t      kernel_name_len = strlen(kernel_name);
267ca5eadf8SJeremy L Thompson 
2682b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
2692b730f8bSJeremy L Thompson     CeedCall(CeedCheckFilePath(ceed, qf->user_source, &is_absolute_path));
270ca5eadf8SJeremy L Thompson     if (is_absolute_path) {
271ca5eadf8SJeremy L Thompson       absolute_path = (char *)qf->user_source;
272ca5eadf8SJeremy L Thompson     } else {
2732b730f8bSJeremy L Thompson       CeedCall(CeedGetJitAbsolutePath(ceed, qf->user_source, &absolute_path));
274ca5eadf8SJeremy L Thompson     }
2759bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&ceed));
276ca5eadf8SJeremy L Thompson 
277ca5eadf8SJeremy L Thompson     size_t source_len = strlen(absolute_path) - kernel_name_len - 1;
2781c66c397SJeremy L Thompson 
2792b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(source_len + 1, &source_path_copy));
280ca5eadf8SJeremy L Thompson     memcpy(source_path_copy, absolute_path, source_len);
281ca5eadf8SJeremy L Thompson     qf->source_path = source_path_copy;
282ca5eadf8SJeremy L Thompson 
2832b730f8bSJeremy L Thompson     if (!is_absolute_path) CeedCall(CeedFree(&absolute_path));
284ca5eadf8SJeremy L Thompson   }
285ca5eadf8SJeremy L Thompson 
28643e1b16fSJeremy L Thompson   *source_path = (char *)qf->source_path;
287e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2887a982d89SJeremy L. Thompson }
2897a982d89SJeremy L. Thompson 
2907a982d89SJeremy L. Thompson /**
291ca94c3ddSJeremy L Thompson   @brief Initialize and load `CeedQFunction` source file into string buffer, including full text of local files in place of `#include "local.h"`.
2924385fb7fSSebastian Grimberg 
293ca94c3ddSJeremy L Thompson   The `buffer` is set to `NULL` if there is no `CeedQFunction` source file.
2944385fb7fSSebastian Grimberg 
295f8d308faSJed Brown   Note: This function may as well return a mutable buffer, but all current uses
296f8d308faSJed Brown   do not modify it. (This is just a downside of `const` semantics with output
297f8d308faSJed Brown   arguments instead of returns.)
298f8d308faSJed Brown 
299ca94c3ddSJeremy L Thompson   Note: Caller is responsible for freeing the string buffer with @ref CeedFree().
3003d3250a0SJeremy L Thompson 
301ca94c3ddSJeremy L Thompson   @param[in]  qf            `CeedQFunction`
302f74ec584SJeremy L Thompson   @param[out] source_buffer String buffer for source file contents
3033d3250a0SJeremy L Thompson 
3043d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3053d3250a0SJeremy L Thompson 
3063d3250a0SJeremy L Thompson   @ref Backend
3073d3250a0SJeremy L Thompson **/
308f8d308faSJed Brown int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, const char **source_buffer) {
30934ffed21SJeremy L Thompson   const char *source_path;
3103d3250a0SJeremy L Thompson 
3112b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetSourcePath(qf, &source_path));
3123d3250a0SJeremy L Thompson   *source_buffer = NULL;
3133d3250a0SJeremy L Thompson   if (source_path) {
3141203703bSJeremy L Thompson     Ceed  ceed;
315f8d308faSJed Brown     char *buffer = NULL;
3161203703bSJeremy L Thompson 
3171203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
318f8d308faSJed Brown     CeedCall(CeedLoadSourceToBuffer(ceed, source_path, &buffer));
3199bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&ceed));
320f8d308faSJed Brown     *source_buffer = buffer;
3213d3250a0SJeremy L Thompson   }
3223d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3233d3250a0SJeremy L Thompson }
3243d3250a0SJeremy L Thompson 
3253d3250a0SJeremy L Thompson /**
326ca94c3ddSJeremy L Thompson   @brief Get the User Function for a `CeedQFunction`
3277a982d89SJeremy L. Thompson 
328ca94c3ddSJeremy L Thompson   @param[in]  qf `CeedQFunction`
3297a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
3307a982d89SJeremy L. Thompson 
3317a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3327a982d89SJeremy L. Thompson 
3337a982d89SJeremy L. Thompson   @ref Backend
3347a982d89SJeremy L. Thompson **/
3357a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
3367a982d89SJeremy L. Thompson   *f = qf->function;
337e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3387a982d89SJeremy L. Thompson }
3397a982d89SJeremy L. Thompson 
3407a982d89SJeremy L. Thompson /**
341ca94c3ddSJeremy L Thompson   @brief Get global context for a `CeedQFunction`.
3424385fb7fSSebastian Grimberg 
343ca94c3ddSJeremy L Thompson   Note: For `CeedQFunction` from the Fortran interface, this function will return the Fortran context `CeedQFunctionContext`.
3447a982d89SJeremy L. Thompson 
345ea61e9acSJeremy L Thompson   @param[in]  qf  CeedQFunction
346777ff853SJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
3477a982d89SJeremy L. Thompson 
3487a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3497a982d89SJeremy L. Thompson 
3507a982d89SJeremy L. Thompson   @ref Backend
3517a982d89SJeremy L. Thompson **/
352777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
3531485364cSJeremy L Thompson   *ctx = NULL;
3541485364cSJeremy L Thompson   if (qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(qf->ctx, ctx));
355e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3567a982d89SJeremy L. Thompson }
3577a982d89SJeremy L. Thompson 
3587a982d89SJeremy L. Thompson /**
359ca94c3ddSJeremy L Thompson   @brief Get context data of a `CeedQFunction`
360441428dfSJeremy L Thompson 
361ca94c3ddSJeremy L Thompson   @param[in]  qf       `CeedQFunction`
362ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
363ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
364441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
365441428dfSJeremy L Thompson 
366441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
367441428dfSJeremy L Thompson 
368441428dfSJeremy L Thompson   @ref Backend
369441428dfSJeremy L Thompson **/
3702b730f8bSJeremy L Thompson int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
371441428dfSJeremy L Thompson   bool                 is_writable;
372441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
373441428dfSJeremy L Thompson 
3742b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
375441428dfSJeremy L Thompson   if (ctx) {
3762b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
377441428dfSJeremy L Thompson     if (is_writable) {
3782b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
379441428dfSJeremy L Thompson     } else {
3802b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
381441428dfSJeremy L Thompson     }
382441428dfSJeremy L Thompson   } else {
383441428dfSJeremy L Thompson     *(void **)data = NULL;
384441428dfSJeremy L Thompson   }
3851485364cSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx));
386441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
387441428dfSJeremy L Thompson }
388441428dfSJeremy L Thompson 
389441428dfSJeremy L Thompson /**
390ca94c3ddSJeremy L Thompson   @brief Restore context data of a `CeedQFunction`
391441428dfSJeremy L Thompson 
392ca94c3ddSJeremy L Thompson   @param[in]     qf   `CeedQFunction`
393ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
394441428dfSJeremy L Thompson 
395441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
396441428dfSJeremy L Thompson 
397441428dfSJeremy L Thompson   @ref Backend
398441428dfSJeremy L Thompson **/
399441428dfSJeremy L Thompson int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) {
400441428dfSJeremy L Thompson   bool                 is_writable;
401441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
402441428dfSJeremy L Thompson 
4032b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
404441428dfSJeremy L Thompson   if (ctx) {
4052b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
406441428dfSJeremy L Thompson     if (is_writable) {
4072b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
408441428dfSJeremy L Thompson     } else {
4092b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
410441428dfSJeremy L Thompson     }
411441428dfSJeremy L Thompson   }
4121485364cSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx));
413441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
414441428dfSJeremy L Thompson }
415441428dfSJeremy L Thompson 
416441428dfSJeremy L Thompson /**
417ca94c3ddSJeremy L Thompson   @brief Get true user context for a `CeedQFunction`
4184385fb7fSSebastian Grimberg 
419ca94c3ddSJeremy L Thompson   Note: For all `CeedQFunction` this function will return the user `CeedQFunctionContext` and not interface context `CeedQFunctionContext`, if any such object exists.
4207a982d89SJeremy L. Thompson 
421ca94c3ddSJeremy L Thompson   @param[in]  qf  `CeedQFunction`
422ca94c3ddSJeremy L Thompson   @param[out] ctx Variable to store `CeedQFunctionContext`
4237a982d89SJeremy L. Thompson 
4247a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4257a982d89SJeremy L. Thompson   @ref Backend
4267a982d89SJeremy L. Thompson **/
427777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
4281203703bSJeremy L Thompson   CeedQFunctionContext qf_ctx;
4291203703bSJeremy L Thompson 
4301203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &qf_ctx));
431f04ea552SJeremy L Thompson   if (qf->is_fortran) {
432d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
4331c66c397SJeremy L Thompson 
4341203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetData(qf_ctx, CEED_MEM_HOST, &fortran_ctx));
4358cb0412aSJeremy L Thompson     *ctx = fortran_ctx->inner_ctx;
4369a25c351SJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreData(qf_ctx, &fortran_ctx));
4377a982d89SJeremy L. Thompson   } else {
4381203703bSJeremy L Thompson     *ctx = qf_ctx;
4397a982d89SJeremy L. Thompson   }
4401485364cSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&qf_ctx));
441e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4427a982d89SJeremy L. Thompson }
4437a982d89SJeremy L. Thompson 
4447a982d89SJeremy L. Thompson /**
445ca94c3ddSJeremy L Thompson   @brief Get inner context data of a `CeedQFunction`
446441428dfSJeremy L Thompson 
447ca94c3ddSJeremy L Thompson   @param[in]  qf       `CeedQFunction`
448ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
449ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
450441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
451441428dfSJeremy L Thompson 
452441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
453441428dfSJeremy L Thompson 
454441428dfSJeremy L Thompson   @ref Backend
455441428dfSJeremy L Thompson **/
4562b730f8bSJeremy L Thompson int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
457441428dfSJeremy L Thompson   bool                 is_writable;
458441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
459441428dfSJeremy L Thompson 
4602b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
461441428dfSJeremy L Thompson   if (ctx) {
4622b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
463441428dfSJeremy L Thompson     if (is_writable) {
4642b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
465441428dfSJeremy L Thompson     } else {
4662b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
467441428dfSJeremy L Thompson     }
468441428dfSJeremy L Thompson   } else {
469441428dfSJeremy L Thompson     *(void **)data = NULL;
470441428dfSJeremy L Thompson   }
471441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
472441428dfSJeremy L Thompson }
473441428dfSJeremy L Thompson 
474441428dfSJeremy L Thompson /**
475ca94c3ddSJeremy L Thompson   @brief Restore inner context data of a `CeedQFunction`
476441428dfSJeremy L Thompson 
477ca94c3ddSJeremy L Thompson   @param[in]     qf   `CeedQFunction`
478ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
479441428dfSJeremy L Thompson 
480441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
481441428dfSJeremy L Thompson 
482441428dfSJeremy L Thompson   @ref Backend
483441428dfSJeremy L Thompson **/
484441428dfSJeremy L Thompson int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) {
485441428dfSJeremy L Thompson   bool                 is_writable;
486441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
487441428dfSJeremy L Thompson 
4882b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
489441428dfSJeremy L Thompson   if (ctx) {
4902b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
491441428dfSJeremy L Thompson     if (is_writable) {
4922b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
493441428dfSJeremy L Thompson     } else {
4942b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
495441428dfSJeremy L Thompson     }
496441428dfSJeremy L Thompson   }
4975f249b39SJeremy L Thompson   *(void **)data = NULL;
498441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
499441428dfSJeremy L Thompson }
500441428dfSJeremy L Thompson 
501441428dfSJeremy L Thompson /**
502ca94c3ddSJeremy L Thompson   @brief Determine if `CeedQFunction` is identity
5037a982d89SJeremy L. Thompson 
504ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
505d1d35e2fSjeremylt   @param[out] is_identity Variable to store identity status
5067a982d89SJeremy L. Thompson 
5077a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5087a982d89SJeremy L. Thompson 
5097a982d89SJeremy L. Thompson   @ref Backend
5107a982d89SJeremy L. Thompson **/
511d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
512f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
513e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5147a982d89SJeremy L. Thompson }
5157a982d89SJeremy L. Thompson 
5167a982d89SJeremy L. Thompson /**
517ca94c3ddSJeremy L Thompson   @brief Determine if `CeedQFunctionContext` is writable
518441428dfSJeremy L Thompson 
519ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
520ea61e9acSJeremy L Thompson   @param[out] is_writable Variable to store context writeable status
521441428dfSJeremy L Thompson 
522441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
523441428dfSJeremy L Thompson 
524441428dfSJeremy L Thompson   @ref Backend
525441428dfSJeremy L Thompson **/
526441428dfSJeremy L Thompson int CeedQFunctionIsContextWritable(CeedQFunction qf, bool *is_writable) {
527441428dfSJeremy L Thompson   *is_writable = qf->is_context_writable;
528441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
529441428dfSJeremy L Thompson }
530441428dfSJeremy L Thompson 
531441428dfSJeremy L Thompson /**
532ca94c3ddSJeremy L Thompson   @brief Get backend data of a `CeedQFunction`
5337a982d89SJeremy L. Thompson 
534ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction`
5357a982d89SJeremy L. Thompson   @param[out] data Variable to store data
5367a982d89SJeremy L. Thompson 
5377a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5387a982d89SJeremy L. Thompson 
5397a982d89SJeremy L. Thompson   @ref Backend
5407a982d89SJeremy L. Thompson **/
541777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
542777ff853SJeremy L Thompson   *(void **)data = qf->data;
543e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5447a982d89SJeremy L. Thompson }
5457a982d89SJeremy L. Thompson 
5467a982d89SJeremy L. Thompson /**
547ca94c3ddSJeremy L Thompson   @brief Set backend data of a `CeedQFunction`
5487a982d89SJeremy L. Thompson 
549ca94c3ddSJeremy L Thompson   @param[in,out] qf   `CeedQFunction`
550ea61e9acSJeremy L Thompson   @param[in]     data Data to set
5517a982d89SJeremy L. Thompson 
5527a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5537a982d89SJeremy L. Thompson 
5547a982d89SJeremy L. Thompson   @ref Backend
5557a982d89SJeremy L. Thompson **/
556777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
557777ff853SJeremy L Thompson   qf->data = data;
558e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5597a982d89SJeremy L. Thompson }
5607a982d89SJeremy L. Thompson 
5617a982d89SJeremy L. Thompson /**
5621203703bSJeremy L Thompson   @brief Get a boolean value indicating if the `CeedQFunction` is immutable
5631203703bSJeremy L Thompson 
5641203703bSJeremy L Thompson   @param[in]  qf           `CeedOperator`
5651203703bSJeremy L Thompson   @param[out] is_immutable Variable to store immutability status
5661203703bSJeremy L Thompson 
5671203703bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5681203703bSJeremy L Thompson 
5691203703bSJeremy L Thompson   @ref Backend
5701203703bSJeremy L Thompson **/
5711203703bSJeremy L Thompson int CeedQFunctionIsImmutable(CeedQFunction qf, bool *is_immutable) {
5721203703bSJeremy L Thompson   *is_immutable = qf->is_immutable;
5731203703bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
5741203703bSJeremy L Thompson }
5751203703bSJeremy L Thompson 
5761203703bSJeremy L Thompson /**
5771203703bSJeremy L Thompson   @brief Set the immutable flag of a `CeedQFunction` to `true`
5781203703bSJeremy L Thompson 
5791203703bSJeremy L Thompson   @param[in,out] qf `CeedQFunction`
5801203703bSJeremy L Thompson 
5811203703bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5821203703bSJeremy L Thompson 
5831203703bSJeremy L Thompson   @ref Backend
5841203703bSJeremy L Thompson **/
5851203703bSJeremy L Thompson int CeedQFunctionSetImmutable(CeedQFunction qf) {
5861203703bSJeremy L Thompson   qf->is_immutable = true;
5871203703bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
5881203703bSJeremy L Thompson }
5891203703bSJeremy L Thompson 
5901203703bSJeremy L Thompson /**
591ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedQFunction`
59234359f16Sjeremylt 
593ca94c3ddSJeremy L Thompson   @param[in,out] qf `CeedQFunction` to increment the reference counter
59434359f16Sjeremylt 
59534359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
59634359f16Sjeremylt 
59734359f16Sjeremylt   @ref Backend
59834359f16Sjeremylt **/
5999560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
60034359f16Sjeremylt   qf->ref_count++;
60134359f16Sjeremylt   return CEED_ERROR_SUCCESS;
60234359f16Sjeremylt }
60334359f16Sjeremylt 
6046e15d496SJeremy L Thompson /**
605ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs per quadrature required to apply `CeedQFunction`
6066e15d496SJeremy L Thompson 
607ca94c3ddSJeremy L Thompson   @param[in]  qf    `CeedQFunction` to estimate FLOPs for
608ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
6096e15d496SJeremy L Thompson 
6106e15d496SJeremy L Thompson   @ref Backend
6116e15d496SJeremy L Thompson **/
6129d36ca50SJeremy L Thompson int CeedQFunctionGetFlopsEstimate(CeedQFunction qf, CeedSize *flops) {
6136e15d496SJeremy L Thompson   *flops = qf->user_flop_estimate;
6146e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6156e15d496SJeremy L Thompson }
6166e15d496SJeremy L Thompson 
6177a982d89SJeremy L. Thompson /// @}
6187a982d89SJeremy L. Thompson 
6197a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6207a982d89SJeremy L. Thompson /// CeedQFunction Public API
6217a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6227a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
6237a982d89SJeremy L. Thompson /// @{
6247a982d89SJeremy L. Thompson 
6257a982d89SJeremy L. Thompson /**
626ca94c3ddSJeremy L Thompson   @brief Create a `CeedQFunction` for evaluating interior (volumetric) terms
6277a982d89SJeremy L. Thompson 
628ca94c3ddSJeremy L Thompson   @param[in]  ceed       `Ceed` object used to create the `CeedQFunction`
629ca94c3ddSJeremy L Thompson   @param[in]  vec_length Vector length.
630ca94c3ddSJeremy L Thompson                            Caller must ensure that number of quadrature points is a multiple of `vec_length`.
631ea61e9acSJeremy L Thompson   @param[in]  f          Function pointer to evaluate action at quadrature points.
632ca94c3ddSJeremy L Thompson                            See `CeedQFunctionUser`.
633ca94c3ddSJeremy L Thompson   @param[in]  source     Absolute path to source of `CeedQFunctionUser`, "\abs_path\file.h:function_name".
634ca94c3ddSJeremy 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.).
6354ada38baSJames Wright                            The entire contents of this file and all locally included files are used during JiT compilation for GPU backends.
636c0b5abf0SJeremy L Thompson                            The header `ceed/types.h` is preferred over `ceed.h` or `ceed/ceed.h` for `CeedQFunction` source files.
637c0b5abf0SJeremy 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`.
6384ada38baSJames Wright                            All source files must be at the provided filepath at runtime for JiT to function.
639ca94c3ddSJeremy L Thompson   @param[out] qf         Address of the variable where the newly created `CeedQFunction` will be stored
6407a982d89SJeremy L. Thompson 
6417a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6427a982d89SJeremy L. Thompson 
643ca94c3ddSJeremy L Thompson   See \ref CeedQFunctionUser for details on the call-back function `f` arguments.
6447a982d89SJeremy L. Thompson 
6457a982d89SJeremy L. Thompson   @ref User
6467a982d89SJeremy L. Thompson **/
6472b730f8bSJeremy L Thompson int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length, CeedQFunctionUser f, const char *source, CeedQFunction *qf) {
648ca5eadf8SJeremy L Thompson   char *user_source_copy;
6497a982d89SJeremy L. Thompson 
6507a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
6517a982d89SJeremy L. Thompson     Ceed delegate;
6526574a04fSJeremy L Thompson 
6532b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "QFunction"));
6541ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedQFunctionCreateInterior");
6552b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf));
6569bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
657e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
6587a982d89SJeremy L. Thompson   }
6597a982d89SJeremy L. Thompson 
6606574a04fSJeremy L Thompson   CeedCheck(!strlen(source) || strrchr(source, ':'), ceed, CEED_ERROR_INCOMPLETE,
6616574a04fSJeremy L Thompson             "Provided path to source does not include function name. Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"", source);
66243e1b16fSJeremy L Thompson 
6632b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, qf));
664db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*qf)->ceed));
665d1d35e2fSjeremylt   (*qf)->ref_count           = 1;
666d1d35e2fSjeremylt   (*qf)->vec_length          = vec_length;
667f04ea552SJeremy L Thompson   (*qf)->is_identity         = false;
668441428dfSJeremy L Thompson   (*qf)->is_context_writable = true;
6697a982d89SJeremy L. Thompson   (*qf)->function            = f;
6706e15d496SJeremy L Thompson   (*qf)->user_flop_estimate  = -1;
67143e1b16fSJeremy L Thompson   if (strlen(source)) {
672ca5eadf8SJeremy L Thompson     size_t user_source_len = strlen(source);
673ee5a26f2SJeremy L Thompson 
6742b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(user_source_len + 1, &user_source_copy));
675ca5eadf8SJeremy L Thompson     memcpy(user_source_copy, source, user_source_len);
676ca5eadf8SJeremy L Thompson     (*qf)->user_source = user_source_copy;
67743e1b16fSJeremy L Thompson   }
6782b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields));
6792b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields));
6802b730f8bSJeremy L Thompson   CeedCall(ceed->QFunctionCreate(*qf));
681e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6827a982d89SJeremy L. Thompson }
6837a982d89SJeremy L. Thompson 
6847a982d89SJeremy L. Thompson /**
685ca94c3ddSJeremy L Thompson   @brief Create a `CeedQFunction` for evaluating interior (volumetric) terms by name
686288c0443SJeremy L Thompson 
687ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedQFunction`
688ca94c3ddSJeremy L Thompson   @param[in]  name Name of `CeedQFunction` to use from gallery
689ca94c3ddSJeremy L Thompson   @param[out] qf   Address of the variable where the newly created `CeedQFunction` will be stored
690288c0443SJeremy L Thompson 
691288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
692288c0443SJeremy L Thompson 
6937a982d89SJeremy L. Thompson   @ref User
694288c0443SJeremy L Thompson **/
6952b730f8bSJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, CeedQFunction *qf) {
696f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
697288c0443SJeremy L Thompson 
6982b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionRegisterAll());
699288c0443SJeremy L Thompson   // Find matching backend
700ca94c3ddSJeremy L Thompson   CeedCheck(name, ceed, CEED_ERROR_INCOMPLETE, "No CeedQFunction name provided");
701288c0443SJeremy L Thompson   for (size_t i = 0; i < num_qfunctions; i++) {
702288c0443SJeremy L Thompson     size_t      n;
703d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
7042b730f8bSJeremy L Thompson     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {
7052b730f8bSJeremy L Thompson     }
706d1d35e2fSjeremylt     if (n > match_len) {
707d1d35e2fSjeremylt       match_len   = n;
708f7e22acaSJeremy L Thompson       match_index = i;
709288c0443SJeremy L Thompson     }
710288c0443SJeremy L Thompson   }
711ca94c3ddSJeremy L Thompson   CeedCheck(match_len > 0, ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery CeedQFunction");
712288c0443SJeremy L Thompson 
713288c0443SJeremy L Thompson   // Create QFunction
7142b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInterior(ceed, gallery_qfunctions[match_index].vec_length, gallery_qfunctions[match_index].f,
7152b730f8bSJeremy L Thompson                                        gallery_qfunctions[match_index].source, qf));
716288c0443SJeremy L Thompson 
717288c0443SJeremy L Thompson   // QFunction specific setup
7182b730f8bSJeremy L Thompson   CeedCall(gallery_qfunctions[match_index].init(ceed, name, *qf));
719288c0443SJeremy L Thompson 
72075affc3bSjeremylt   // Copy name
7212b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name));
72243e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
723e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
724288c0443SJeremy L Thompson }
725288c0443SJeremy L Thompson 
726288c0443SJeremy L Thompson /**
727ca94c3ddSJeremy L Thompson   @brief Create an identity `CeedQFunction`.
7284385fb7fSSebastian Grimberg 
729ea61e9acSJeremy L Thompson   Inputs are written into outputs in the order given.
730ca94c3ddSJeremy 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.
731ca94c3ddSJeremy 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.
7320219ea01SJeremy L Thompson 
733ca94c3ddSJeremy L Thompson   @param[in]  ceed     `Ceed` object used to create the `CeedQFunction`
734ca94c3ddSJeremy L Thompson   @param[in]  size     Size of the `CeedQFunction` fields
735ca94c3ddSJeremy L Thompson   @param[in]  in_mode  @ref CeedEvalMode for input to `CeedQFunction`
736ca94c3ddSJeremy L Thompson   @param[in]  out_mode @ref CeedEvalMode for output to `CeedQFunction`
737ca94c3ddSJeremy L Thompson   @param[out] qf       Address of the variable where the newly created `CeedQFunction` will be stored
7380219ea01SJeremy L Thompson 
7390219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
7400219ea01SJeremy L Thompson 
7417a982d89SJeremy L. Thompson   @ref User
7420219ea01SJeremy L Thompson **/
7432b730f8bSJeremy L Thompson int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, CeedEvalMode out_mode, CeedQFunction *qf) {
7441c66c397SJeremy L Thompson   CeedQFunctionContext  ctx;
7451c66c397SJeremy L Thompson   CeedContextFieldLabel size_label;
7461c66c397SJeremy L Thompson 
7472b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Identity", qf));
7482b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(*qf, "input", size, in_mode));
7492b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(*qf, "output", size, out_mode));
7500219ea01SJeremy L Thompson 
751f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
752547dbd6fSJeremy L Thompson 
7532b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(*qf, &ctx));
7542b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label));
7552b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetInt32(ctx, size_label, &size));
7561485364cSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx));
757e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7580219ea01SJeremy L Thompson }
7590219ea01SJeremy L Thompson 
7600219ea01SJeremy L Thompson /**
761ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedQFunction`.
7624385fb7fSSebastian Grimberg 
763ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedQFunctionDestroy().
764512bb800SJeremy L Thompson 
765ca94c3ddSJeremy 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`.
766ca94c3ddSJeremy L Thompson         This `CeedQFunction` will be destroyed if `*qf_copy` is the only reference to this `CeedQFunction`.
7679560d06aSjeremylt 
768ca94c3ddSJeremy L Thompson   @param[in]  qf      `CeedQFunction` to copy reference to
7699560d06aSjeremylt   @param[out] qf_copy Variable to store copied reference
7709560d06aSjeremylt 
7719560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7729560d06aSjeremylt 
7739560d06aSjeremylt   @ref User
7749560d06aSjeremylt **/
7759560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
7762b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionReference(qf));
7772b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(qf_copy));
7789560d06aSjeremylt   *qf_copy = qf;
7799560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7809560d06aSjeremylt }
7819560d06aSjeremylt 
7829560d06aSjeremylt /**
783ca94c3ddSJeremy L Thompson   @brief Add a `CeedQFunction` input
784b11c1e72Sjeremylt 
785ca94c3ddSJeremy L Thompson   @param[in,out] qf         `CeedQFunction`
786ca94c3ddSJeremy L Thompson   @param[in]     field_name Name of `CeedQFunction` field
787d538d163SJeremy L Thompson   @param[in]     size       Size of `CeedQFunction` field,
788d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_NONE,
789d538d163SJeremy 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,
790d538d163SJeremy L Thompson                               (`num_comp * dim`) for @ref CEED_EVAL_GRAD,
791d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_DIV, and
792d538d163SJeremy L Thompson                               (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` otherwise `curl_dim = dim` for @ref CEED_EVAL_CURL.
793ca94c3ddSJeremy L Thompson   @param[in]     eval_mode  @ref CEED_EVAL_NONE to use values directly,
794ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_INTERP to use interpolated values,
795ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_GRAD to use gradients,
796ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_DIV to use divergence,
797ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_CURL to use curl
798b11c1e72Sjeremylt 
799d538d163SJeremy L Thompson   Note: In the user `CeedQFunctionUser`, the `in` argument list the fields in the order given by the calls to `CeedQFunctionAddInput`.
800d538d163SJeremy L Thompson 
801b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
802dfdf5a53Sjeremylt 
8037a982d89SJeremy L. Thompson   @ref User
804b11c1e72Sjeremylt **/
8052b730f8bSJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
8061203703bSJeremy L Thompson   bool is_immutable;
8071203703bSJeremy L Thompson 
8081203703bSJeremy L Thompson   CeedCall(CeedQFunctionIsImmutable(qf, &is_immutable));
8099bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedQFunctionReturnCeed(qf), CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
8109bc66399SJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT || size == 1, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION, "CEED_EVAL_WEIGHT should have size 1");
811643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
8129bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8133f08121cSJeremy L Thompson               "CeedQFunction field names must be unique. Duplicate name: %s", field_name);
814643fbb69SJeremy L Thompson   }
815643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
8169bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8173f08121cSJeremy L Thompson               "CeedQFunction field names must be unique. Duplicate name: %s", field_name);
818643fbb69SJeremy L Thompson   }
8192b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], field_name, size, eval_mode));
820d1d35e2fSjeremylt   qf->num_input_fields++;
821e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
822d7b241e6Sjeremylt }
823d7b241e6Sjeremylt 
824b11c1e72Sjeremylt /**
825ca94c3ddSJeremy L Thompson   @brief Add a `CeedQFunction` output
826b11c1e72Sjeremylt 
827ca94c3ddSJeremy L Thompson   @param[in,out] qf         `CeedQFunction`
828ca94c3ddSJeremy L Thompson   @param[in]     field_name Name of `CeedQFunction` field
829d538d163SJeremy L Thompson   @param[in]     size       Size of `CeedQFunction` field,
830d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_NONE,
831d538d163SJeremy 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,
832d538d163SJeremy L Thompson                               (`num_comp * dim`) for @ref CEED_EVAL_GRAD,
833d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_DIV, and
834d538d163SJeremy L Thompson                               (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` otherwise `curl_dim = dim` for @ref CEED_EVAL_CURL.
835ca94c3ddSJeremy L Thompson   @param[in]     eval_mode  @ref CEED_EVAL_NONE to use values directly,
836ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_INTERP to use interpolated values,
837ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_GRAD to use gradients,
838ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_DIV to use divergence,
839ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_CURL to use curl.
840b11c1e72Sjeremylt 
841d538d163SJeremy L Thompson   Note: In the user `CeedQFunctionUser`, the `out` argument list the fields in the order given by the calls to `CeedQFunctionAddOutput`.
842d538d163SJeremy L Thompson 
843b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
844dfdf5a53Sjeremylt 
8457a982d89SJeremy L. Thompson   @ref User
846b11c1e72Sjeremylt **/
8472b730f8bSJeremy L Thompson int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
8481203703bSJeremy L Thompson   bool is_immutable;
8491203703bSJeremy L Thompson 
8501203703bSJeremy L Thompson   CeedCall(CeedQFunctionIsImmutable(qf, &is_immutable));
8519bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedQFunctionReturnCeed(qf), CEED_ERROR_MAJOR, "CeedQFunction cannot be changed after set as immutable");
8529bc66399SJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION,
8539bc66399SJeremy L Thompson             "Cannot create CeedQFunction output with CEED_EVAL_WEIGHT");
854643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
8559bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8569bc66399SJeremy L Thompson               "CeedQFunction field names must be unique");
857643fbb69SJeremy L Thompson   }
858643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
8599bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8609bc66399SJeremy L Thompson               "CeedQFunction field names must be unique");
861643fbb69SJeremy L Thompson   }
8622b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], field_name, size, eval_mode));
863d1d35e2fSjeremylt   qf->num_output_fields++;
864e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
865d7b241e6Sjeremylt }
866d7b241e6Sjeremylt 
867dfdf5a53Sjeremylt /**
868ca94c3ddSJeremy L Thompson   @brief Get the `CeedQFunctionField` of a `CeedQFunction`
86943bbe138SJeremy L Thompson 
870ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedQFunction` as immutable.
871f04ea552SJeremy L Thompson 
872ca94c3ddSJeremy L Thompson   @param[in]  qf                `CeedQFunction`
873f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
874f74ec584SJeremy L Thompson   @param[out] input_fields      Variable to store input fields
875f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
876f74ec584SJeremy L Thompson   @param[out] output_fields     Variable to store output fields
87743bbe138SJeremy L Thompson 
87843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
87943bbe138SJeremy L Thompson 
880e9b533fbSJeremy L Thompson   @ref Advanced
88143bbe138SJeremy L Thompson **/
8822b730f8bSJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields, CeedQFunctionField **input_fields, CeedInt *num_output_fields,
88343bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
8841203703bSJeremy L Thompson   CeedCall(CeedQFunctionSetImmutable(qf));
88543bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
88643bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
88743bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
88843bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
88943bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
89043bbe138SJeremy L Thompson }
89143bbe138SJeremy L Thompson 
89243bbe138SJeremy L Thompson /**
893ca94c3ddSJeremy L Thompson   @brief Get the name of a `CeedQFunctionField`
89443bbe138SJeremy L Thompson 
895ca94c3ddSJeremy L Thompson   @param[in]  qf_field   `CeedQFunctionField`
89643bbe138SJeremy L Thompson   @param[out] field_name Variable to store the field name
89743bbe138SJeremy L Thompson 
89843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
89943bbe138SJeremy L Thompson 
900e9b533fbSJeremy L Thompson   @ref Advanced
90143bbe138SJeremy L Thompson **/
9026f8994e9SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, const char **field_name) {
9036f8994e9SJeremy L Thompson   *field_name = qf_field->field_name;
90443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
90543bbe138SJeremy L Thompson }
90643bbe138SJeremy L Thompson 
90743bbe138SJeremy L Thompson /**
908ca94c3ddSJeremy L Thompson   @brief Get the number of components of a `CeedQFunctionField`
90943bbe138SJeremy L Thompson 
910ca94c3ddSJeremy L Thompson   @param[in]  qf_field `CeedQFunctionField`
91143bbe138SJeremy L Thompson   @param[out] size     Variable to store the size of the field
91243bbe138SJeremy L Thompson 
91343bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
91443bbe138SJeremy L Thompson 
915e9b533fbSJeremy L Thompson   @ref Advanced
91643bbe138SJeremy L Thompson **/
91743bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
91843bbe138SJeremy L Thompson   *size = qf_field->size;
91943bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
92043bbe138SJeremy L Thompson }
92143bbe138SJeremy L Thompson 
92243bbe138SJeremy L Thompson /**
923ca94c3ddSJeremy L Thompson   @brief Get the @ref CeedEvalMode of a `CeedQFunctionField`
92443bbe138SJeremy L Thompson 
925ca94c3ddSJeremy L Thompson   @param[in]  qf_field  `CeedQFunctionField`
92643bbe138SJeremy L Thompson   @param[out] eval_mode Variable to store the field evaluation mode
92743bbe138SJeremy L Thompson 
92843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
92943bbe138SJeremy L Thompson 
930e9b533fbSJeremy L Thompson   @ref Advanced
93143bbe138SJeremy L Thompson **/
9322b730f8bSJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, CeedEvalMode *eval_mode) {
93343bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
93443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
93543bbe138SJeremy L Thompson }
93643bbe138SJeremy L Thompson 
93743bbe138SJeremy L Thompson /**
938ab747706SJeremy L Thompson   @brief Get the data of a `CeedQFunctionField`.
939ab747706SJeremy L Thompson 
940ab747706SJeremy L Thompson   Any arguments set as `NULL` are ignored.
941ab747706SJeremy L Thompson 
942ab747706SJeremy L Thompson   @param[in]  qf_field   `CeedQFunctionField`
943ab747706SJeremy L Thompson   @param[out] field_name Variable to store the field name
944ab747706SJeremy L Thompson   @param[out] size       Variable to store the size of the field
945ab747706SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
946ab747706SJeremy L Thompson 
947ab747706SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
948ab747706SJeremy L Thompson 
949ab747706SJeremy L Thompson   @ref Advanced
950ab747706SJeremy L Thompson **/
9516f8994e9SJeremy L Thompson int CeedQFunctionFieldGetData(CeedQFunctionField qf_field, const char **field_name, CeedInt *size, CeedEvalMode *eval_mode) {
952ab747706SJeremy L Thompson   if (field_name) CeedCall(CeedQFunctionFieldGetName(qf_field, field_name));
953ab747706SJeremy L Thompson   if (size) CeedCall(CeedQFunctionFieldGetSize(qf_field, size));
954ab747706SJeremy L Thompson   if (eval_mode) CeedCall(CeedQFunctionFieldGetEvalMode(qf_field, eval_mode));
955ab747706SJeremy L Thompson   return CEED_ERROR_SUCCESS;
956ab747706SJeremy L Thompson }
957ab747706SJeremy L Thompson 
958ab747706SJeremy L Thompson /**
959ca94c3ddSJeremy L Thompson   @brief Set global context for a `CeedQFunction`
960b11c1e72Sjeremylt 
961ca94c3ddSJeremy L Thompson   @param[in,out] qf  `CeedQFunction`
962ea61e9acSJeremy L Thompson   @param[in]     ctx Context data to set
963b11c1e72Sjeremylt 
964b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
965dfdf5a53Sjeremylt 
9667a982d89SJeremy L. Thompson   @ref User
967b11c1e72Sjeremylt **/
968777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
9692b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&qf->ctx));
970d7b241e6Sjeremylt   qf->ctx = ctx;
971db002c03SJeremy L Thompson   if (ctx) CeedCall(CeedQFunctionContextReference(ctx));
972e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
973d7b241e6Sjeremylt }
974d7b241e6Sjeremylt 
975b11c1e72Sjeremylt /**
976ca94c3ddSJeremy L Thompson   @brief Set writability of `CeedQFunctionContext` when calling the `CeedQFunctionUser`.
9774385fb7fSSebastian Grimberg 
978859c15bbSJames Wright   The default value is `is_writable == true`.
979441428dfSJeremy L Thompson 
980ca94c3ddSJeremy L Thompson   Setting `is_writable == true` indicates the `CeedQFunctionUser` writes into the `CeedQFunctionContext` and requires memory synchronization after calling @ref CeedQFunctionApply().
981441428dfSJeremy L Thompson 
982ca94c3ddSJeremy L Thompson   Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not modify the `CeedQFunctionContext`.
983ea61e9acSJeremy L Thompson   Violating this assertion may lead to inconsistent data.
984441428dfSJeremy L Thompson 
985441428dfSJeremy L Thompson   Setting `is_writable == false` may offer a performance improvement on GPU backends.
986441428dfSJeremy L Thompson 
987ca94c3ddSJeremy L Thompson   @param[in,out] qf          `CeedQFunction`
988ca94c3ddSJeremy L Thompson   @param[in]     is_writable Boolean flag for writability status
989441428dfSJeremy L Thompson 
990441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
991441428dfSJeremy L Thompson 
992441428dfSJeremy L Thompson   @ref User
993441428dfSJeremy L Thompson **/
994441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
995441428dfSJeremy L Thompson   qf->is_context_writable = is_writable;
996441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
997441428dfSJeremy L Thompson }
998441428dfSJeremy L Thompson 
999441428dfSJeremy L Thompson /**
1000ca94c3ddSJeremy L Thompson   @brief Set estimated number of FLOPs per quadrature required to apply `CeedQFunction`
10016e15d496SJeremy L Thompson 
1002ca94c3ddSJeremy L Thompson   @param[in]  qf    `CeedQFunction` to estimate FLOPs for
1003ea61e9acSJeremy L Thompson   @param[out] flops FLOPs per quadrature point estimate
10046e15d496SJeremy L Thompson 
10056e15d496SJeremy L Thompson   @ref Backend
10066e15d496SJeremy L Thompson **/
10079d36ca50SJeremy L Thompson int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
10086e536b99SJeremy L Thompson   CeedCheck(flops >= 0, CeedQFunctionReturnCeed(qf), CEED_ERROR_INCOMPATIBLE, "Must set non-negative FLOPs estimate");
10096e15d496SJeremy L Thompson   qf->user_flop_estimate = flops;
10106e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10116e15d496SJeremy L Thompson }
10126e15d496SJeremy L Thompson 
10136e15d496SJeremy L Thompson /**
1014ca94c3ddSJeremy L Thompson   @brief View a `CeedQFunction`
101575affc3bSjeremylt 
1016ca94c3ddSJeremy L Thompson   @param[in] qf     `CeedQFunction` to view
1017ca94c3ddSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
101875affc3bSjeremylt 
101975affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
102075affc3bSjeremylt 
10217a982d89SJeremy L. Thompson   @ref User
102275affc3bSjeremylt **/
102375affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
1024*d3d5610dSJeremy L Thompson   const char *name;
102575affc3bSjeremylt 
1026*d3d5610dSJeremy L Thompson   CeedCall(CeedQFunctionGetName(qf, &name));
1027*d3d5610dSJeremy L Thompson   fprintf(stream, "%sCeedQFunction - %s\n", qf->is_gallery ? "Gallery " : "User ", name);
102875affc3bSjeremylt 
10292b730f8bSJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " input field%s:\n", qf->num_input_fields, qf->num_input_fields > 1 ? "s" : "");
1030d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
10312b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream));
103275affc3bSjeremylt   }
103375affc3bSjeremylt 
10342b730f8bSJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " output field%s:\n", qf->num_output_fields, qf->num_output_fields > 1 ? "s" : "");
1035d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
10362b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream));
103775affc3bSjeremylt   }
1038e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
103975affc3bSjeremylt }
104075affc3bSjeremylt 
104175affc3bSjeremylt /**
1042ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedQFunction`
1043b7c9bbdaSJeremy L Thompson 
1044ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction`
1045ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store`Ceed`
1046b7c9bbdaSJeremy L Thompson 
1047b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1048b7c9bbdaSJeremy L Thompson 
1049b7c9bbdaSJeremy L Thompson   @ref Advanced
1050b7c9bbdaSJeremy L Thompson **/
1051b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
10529bc66399SJeremy L Thompson   *ceed = NULL;
10539bc66399SJeremy L Thompson   CeedCall(CeedReferenceCopy(CeedQFunctionReturnCeed(qf), ceed));
1054b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1055b7c9bbdaSJeremy L Thompson }
1056b7c9bbdaSJeremy L Thompson 
1057b7c9bbdaSJeremy L Thompson /**
10586e536b99SJeremy L Thompson   @brief Return the `Ceed` associated with a `CeedQFunction`
10596e536b99SJeremy L Thompson 
10606e536b99SJeremy L Thompson   @param[in]  qf   `CeedQFunction`
10616e536b99SJeremy L Thompson 
10626e536b99SJeremy L Thompson   @return `Ceed` associated with the `qf`
10636e536b99SJeremy L Thompson 
10646e536b99SJeremy L Thompson   @ref Advanced
10656e536b99SJeremy L Thompson **/
10666e536b99SJeremy L Thompson Ceed CeedQFunctionReturnCeed(CeedQFunction qf) { return qf->ceed; }
10676e536b99SJeremy L Thompson 
10686e536b99SJeremy L Thompson /**
1069ca94c3ddSJeremy L Thompson   @brief Apply the action of a `CeedQFunction`
1070b11c1e72Sjeremylt 
1071ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedQFunction` as immutable.
1072f04ea552SJeremy L Thompson 
1073ca94c3ddSJeremy L Thompson   @param[in]  qf `CeedQFunction`
1074ea61e9acSJeremy L Thompson   @param[in]  Q  Number of quadrature points
1075ca94c3ddSJeremy L Thompson   @param[in]  u  Array of input `CeedVector`
1076ca94c3ddSJeremy L Thompson   @param[out] v  Array of output `CeedVector`
1077b11c1e72Sjeremylt 
1078b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1079dfdf5a53Sjeremylt 
10807a982d89SJeremy L. Thompson   @ref User
1081b11c1e72Sjeremylt **/
10822b730f8bSJeremy L Thompson int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, CeedVector *u, CeedVector *v) {
10831203703bSJeremy L Thompson   CeedInt vec_length;
10841203703bSJeremy L Thompson 
10859bc66399SJeremy L Thompson   CeedCheck(qf->Apply, CeedQFunctionReturnCeed(qf), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionApply");
10861203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetVectorLength(qf, &vec_length));
10879bc66399SJeremy L Thompson   CeedCheck(Q % vec_length == 0, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION,
10889bc66399SJeremy L Thompson             "Number of quadrature points %" CeedInt_FMT " must be a multiple of %" CeedInt_FMT, Q, qf->vec_length);
10891203703bSJeremy L Thompson   CeedCall(CeedQFunctionSetImmutable(qf));
10902b730f8bSJeremy L Thompson   CeedCall(qf->Apply(qf, Q, u, v));
1091e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1092d7b241e6Sjeremylt }
1093d7b241e6Sjeremylt 
1094b11c1e72Sjeremylt /**
1095ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedQFunction`
1096b11c1e72Sjeremylt 
1097ca94c3ddSJeremy L Thompson   @param[in,out] qf `CeedQFunction` to destroy
1098b11c1e72Sjeremylt 
1099b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1100dfdf5a53Sjeremylt 
11017a982d89SJeremy L. Thompson   @ref User
1102b11c1e72Sjeremylt **/
1103d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
1104ad6481ceSJeremy L Thompson   if (!*qf || --(*qf)->ref_count > 0) {
1105ad6481ceSJeremy L Thompson     *qf = NULL;
1106ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1107ad6481ceSJeremy L Thompson   }
1108fe2413ffSjeremylt   // Backend destroy
1109d7b241e6Sjeremylt   if ((*qf)->Destroy) {
11102b730f8bSJeremy L Thompson     CeedCall((*qf)->Destroy(*qf));
1111d7b241e6Sjeremylt   }
1112fe2413ffSjeremylt   // Free fields
111392ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_input_fields; i++) {
11142b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->input_fields[i]).field_name));
11152b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->input_fields[i]));
1116fe2413ffSjeremylt   }
111792ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_output_fields; i++) {
11182b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->output_fields[i]).field_name));
11192b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->output_fields[i]));
1120fe2413ffSjeremylt   }
11212b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->input_fields));
11222b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->output_fields));
1123777ff853SJeremy L Thompson 
1124777ff853SJeremy L Thompson   // User context data object
11252b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&(*qf)->ctx));
1126fe2413ffSjeremylt 
11272b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->user_source));
11282b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->source_path));
11292b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->gallery_name));
11302b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->kernel_name));
11312b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*qf)->ceed));
11322b730f8bSJeremy L Thompson   CeedCall(CeedFree(qf));
1133e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1134d7b241e6Sjeremylt }
1135d7b241e6Sjeremylt 
1136d7b241e6Sjeremylt /// @}
1137