xref: /libCEED/interface/ceed-qfunction.c (revision e9f76d14a1d2b6594a8aca2075cf16ebd9c3bf6f)
19ba83ac0SJeremy 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 /**
146b0f67a9cSJeremy L Thompson   @brief View a `CeedQFunction` passed as a `CeedObject`
147b0f67a9cSJeremy L Thompson 
148b0f67a9cSJeremy L Thompson   @param[in] qf     `CeedQFunction` to view
149b0f67a9cSJeremy L Thompson   @param[in] stream Filestream to write to
150b0f67a9cSJeremy L Thompson 
151b0f67a9cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
152b0f67a9cSJeremy L Thompson 
153b0f67a9cSJeremy L Thompson   @ref Developer
154b0f67a9cSJeremy L Thompson **/
155b0f67a9cSJeremy L Thompson static int CeedQFunctionView_Object(CeedObject qf, FILE *stream) {
156b0f67a9cSJeremy L Thompson   CeedCall(CeedQFunctionView((CeedQFunction)qf, stream));
157b0f67a9cSJeremy L Thompson   return CEED_ERROR_SUCCESS;
158b0f67a9cSJeremy L Thompson }
159b0f67a9cSJeremy L Thompson 
160b0f67a9cSJeremy L Thompson /**
1616c328a79SJeremy L Thompson   @brief Destroy a `CeedQFunction` passed as a `CeedObject`
1626c328a79SJeremy L Thompson 
1636c328a79SJeremy L Thompson   @param[in,out] qf Address of `CeedQFunction` to destroy
1646c328a79SJeremy L Thompson 
1656c328a79SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1666c328a79SJeremy L Thompson 
1676c328a79SJeremy L Thompson   @ref Developer
1686c328a79SJeremy L Thompson **/
1696c328a79SJeremy L Thompson static int CeedQFunctionDestroy_Object(CeedObject *qf) {
1706c328a79SJeremy L Thompson   CeedCall(CeedQFunctionDestroy((CeedQFunction *)qf));
1716c328a79SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1726c328a79SJeremy L Thompson }
1736c328a79SJeremy L Thompson 
1746c328a79SJeremy L Thompson /**
175777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
176777ff853SJeremy L Thompson 
177ea61e9acSJeremy L Thompson   @param[in,out] qf     CeedQFunction
178ea61e9acSJeremy L Thompson   @param[in]     status Boolean value to set as Fortran status
179777ff853SJeremy L Thompson 
180777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
181777ff853SJeremy L Thompson 
182777ff853SJeremy L Thompson   @ref Backend
183777ff853SJeremy L Thompson **/
184777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
185f04ea552SJeremy L Thompson   qf->is_fortran = status;
186e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
187777ff853SJeremy L Thompson }
188777ff853SJeremy L Thompson 
1897a982d89SJeremy L. Thompson /// @}
1907a982d89SJeremy L. Thompson 
1917a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1927a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1937a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1947a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1957a982d89SJeremy L. Thompson /// @{
1967a982d89SJeremy L. Thompson 
1977a982d89SJeremy L. Thompson /**
198ca94c3ddSJeremy L Thompson   @brief Get the vector length of a `CeedQFunction`
1997a982d89SJeremy L. Thompson 
200ca94c3ddSJeremy L Thompson   @param[in]  qf         `CeedQFunction`
201d1d35e2fSjeremylt   @param[out] vec_length Variable to store vector length
2027a982d89SJeremy L. Thompson 
2037a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2047a982d89SJeremy L. Thompson 
2057a982d89SJeremy L. Thompson   @ref Backend
2067a982d89SJeremy L. Thompson **/
207d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
208d1d35e2fSjeremylt   *vec_length = qf->vec_length;
209e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2107a982d89SJeremy L. Thompson }
2117a982d89SJeremy L. Thompson 
2127a982d89SJeremy L. Thompson /**
213ca94c3ddSJeremy L Thompson   @brief Get the number of inputs and outputs to a `CeedQFunction`
2147a982d89SJeremy L. Thompson 
215ca94c3ddSJeremy L Thompson   @param[in]  qf         `CeedQFunction`
216d1d35e2fSjeremylt   @param[out] num_input  Variable to store number of input fields
217d1d35e2fSjeremylt   @param[out] num_output Variable to store number of output fields
2187a982d89SJeremy L. Thompson 
2197a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2207a982d89SJeremy L. Thompson 
2217a982d89SJeremy L. Thompson   @ref Backend
2227a982d89SJeremy L. Thompson **/
2232b730f8bSJeremy L Thompson int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, CeedInt *num_output) {
224d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
225d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
226e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2277a982d89SJeremy L. Thompson }
2287a982d89SJeremy L. Thompson 
2297a982d89SJeremy L. Thompson /**
230d3d5610dSJeremy L Thompson   @brief Get the name of the `CeedQFunction`.
231*e9f76d14SJeremy L Thompson     Use the `name` if created via @ref CeedQFunctionCreateInteriorByName(), otherwise return the kernel name via @ref CeedQFunctionGetKernelName().
232d3d5610dSJeremy L Thompson 
233d3d5610dSJeremy L Thompson   @param[in]  qf   `CeedQFunction`
234*e9f76d14SJeremy L Thompson   @param[out] name Variable to store `CeedQFunction` name
235d3d5610dSJeremy L Thompson 
236d3d5610dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
237d3d5610dSJeremy L Thompson 
238d3d5610dSJeremy L Thompson   @ref Backend
239d3d5610dSJeremy L Thompson **/
240d3d5610dSJeremy L Thompson int CeedQFunctionGetName(CeedQFunction qf, const char **name) {
241d3d5610dSJeremy L Thompson   if (qf->is_gallery) {
242d3d5610dSJeremy L Thompson     *name = qf->gallery_name;
243d3d5610dSJeremy L Thompson   } else {
244d3d5610dSJeremy L Thompson     CeedCall(CeedQFunctionGetKernelName(qf, name));
245d3d5610dSJeremy L Thompson   }
246d3d5610dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
247d3d5610dSJeremy L Thompson }
248d3d5610dSJeremy L Thompson 
249d3d5610dSJeremy L Thompson /**
250ca94c3ddSJeremy L Thompson   @brief Get the name of the user function for a `CeedQFunction`
2517a982d89SJeremy L. Thompson 
252ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
253d3d5610dSJeremy L Thompson   @param[out] kernel_name Variable to store string holding kernel name
2547a982d89SJeremy L. Thompson 
2557a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2567a982d89SJeremy L. Thompson 
2577a982d89SJeremy L. Thompson   @ref Backend
2587a982d89SJeremy L. Thompson **/
2597d023984SJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, const char **kernel_name) {
260ca5eadf8SJeremy L Thompson   if (!qf->kernel_name) {
261ca5eadf8SJeremy L Thompson     char *kernel_name_copy;
262ca5eadf8SJeremy L Thompson 
263ca5eadf8SJeremy L Thompson     if (qf->user_source) {
264ca5eadf8SJeremy L Thompson       const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
265ca5eadf8SJeremy L Thompson       size_t      kernel_name_len = strlen(kernel_name);
266ca5eadf8SJeremy L Thompson 
2672b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(kernel_name_len + 1, &kernel_name_copy));
268ca5eadf8SJeremy L Thompson       memcpy(kernel_name_copy, kernel_name, kernel_name_len);
269ca5eadf8SJeremy L Thompson     } else {
2702b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &kernel_name_copy));
271ca5eadf8SJeremy L Thompson     }
272ca5eadf8SJeremy L Thompson     qf->kernel_name = kernel_name_copy;
273ca5eadf8SJeremy L Thompson   }
274ca5eadf8SJeremy L Thompson 
2757d023984SJeremy L Thompson   *kernel_name = qf->kernel_name;
27643e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
27743e1b16fSJeremy L Thompson }
27843e1b16fSJeremy L Thompson 
27943e1b16fSJeremy L Thompson /**
280ca94c3ddSJeremy L Thompson   @brief Get the source path string for a `CeedQFunction`
28143e1b16fSJeremy L Thompson 
282ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
28343e1b16fSJeremy L Thompson   @param[out] source_path Variable to store source path string
28443e1b16fSJeremy L Thompson 
28543e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
28643e1b16fSJeremy L Thompson 
28743e1b16fSJeremy L Thompson   @ref Backend
28843e1b16fSJeremy L Thompson **/
28934ffed21SJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, const char **source_path) {
290ca5eadf8SJeremy L Thompson   if (!qf->source_path && qf->user_source) {
291ca5eadf8SJeremy L Thompson     Ceed        ceed;
292ca5eadf8SJeremy L Thompson     bool        is_absolute_path;
29322070f95SJeremy L Thompson     char       *source_path_copy;
29422070f95SJeremy L Thompson     const char *absolute_path;
295ca5eadf8SJeremy L Thompson     const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
296ca5eadf8SJeremy L Thompson     size_t      kernel_name_len = strlen(kernel_name);
297ca5eadf8SJeremy L Thompson 
2982b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
2992b730f8bSJeremy L Thompson     CeedCall(CeedCheckFilePath(ceed, qf->user_source, &is_absolute_path));
300ca5eadf8SJeremy L Thompson     if (is_absolute_path) {
301ca5eadf8SJeremy L Thompson       absolute_path = (char *)qf->user_source;
302ca5eadf8SJeremy L Thompson     } else {
3032b730f8bSJeremy L Thompson       CeedCall(CeedGetJitAbsolutePath(ceed, qf->user_source, &absolute_path));
304ca5eadf8SJeremy L Thompson     }
3059bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&ceed));
306ca5eadf8SJeremy L Thompson 
307ca5eadf8SJeremy L Thompson     size_t source_len = strlen(absolute_path) - kernel_name_len - 1;
3081c66c397SJeremy L Thompson 
3092b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(source_len + 1, &source_path_copy));
310ca5eadf8SJeremy L Thompson     memcpy(source_path_copy, absolute_path, source_len);
311ca5eadf8SJeremy L Thompson     qf->source_path = source_path_copy;
312ca5eadf8SJeremy L Thompson 
3132b730f8bSJeremy L Thompson     if (!is_absolute_path) CeedCall(CeedFree(&absolute_path));
314ca5eadf8SJeremy L Thompson   }
315ca5eadf8SJeremy L Thompson 
31643e1b16fSJeremy L Thompson   *source_path = (char *)qf->source_path;
317e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3187a982d89SJeremy L. Thompson }
3197a982d89SJeremy L. Thompson 
3207a982d89SJeremy L. Thompson /**
321ca94c3ddSJeremy L Thompson   @brief Initialize and load `CeedQFunction` source file into string buffer, including full text of local files in place of `#include "local.h"`.
3224385fb7fSSebastian Grimberg 
323ca94c3ddSJeremy L Thompson   The `buffer` is set to `NULL` if there is no `CeedQFunction` source file.
3244385fb7fSSebastian Grimberg 
325f8d308faSJed Brown   Note: This function may as well return a mutable buffer, but all current uses
326f8d308faSJed Brown   do not modify it. (This is just a downside of `const` semantics with output
327f8d308faSJed Brown   arguments instead of returns.)
328f8d308faSJed Brown 
329ca94c3ddSJeremy L Thompson   Note: Caller is responsible for freeing the string buffer with @ref CeedFree().
3303d3250a0SJeremy L Thompson 
331ca94c3ddSJeremy L Thompson   @param[in]  qf            `CeedQFunction`
332f74ec584SJeremy L Thompson   @param[out] source_buffer String buffer for source file contents
3333d3250a0SJeremy L Thompson 
3343d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3353d3250a0SJeremy L Thompson 
3363d3250a0SJeremy L Thompson   @ref Backend
3373d3250a0SJeremy L Thompson **/
338f8d308faSJed Brown int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, const char **source_buffer) {
33934ffed21SJeremy L Thompson   const char *source_path;
3403d3250a0SJeremy L Thompson 
3412b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetSourcePath(qf, &source_path));
3423d3250a0SJeremy L Thompson   *source_buffer = NULL;
3433d3250a0SJeremy L Thompson   if (source_path) {
3441203703bSJeremy L Thompson     Ceed  ceed;
345f8d308faSJed Brown     char *buffer = NULL;
3461203703bSJeremy L Thompson 
3471203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
348f8d308faSJed Brown     CeedCall(CeedLoadSourceToBuffer(ceed, source_path, &buffer));
3499bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&ceed));
350f8d308faSJed Brown     *source_buffer = buffer;
3513d3250a0SJeremy L Thompson   }
3523d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3533d3250a0SJeremy L Thompson }
3543d3250a0SJeremy L Thompson 
3553d3250a0SJeremy L Thompson /**
356ca94c3ddSJeremy L Thompson   @brief Get the User Function for a `CeedQFunction`
3577a982d89SJeremy L. Thompson 
358ca94c3ddSJeremy L Thompson   @param[in]  qf `CeedQFunction`
3597a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
3607a982d89SJeremy L. Thompson 
3617a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3627a982d89SJeremy L. Thompson 
3637a982d89SJeremy L. Thompson   @ref Backend
3647a982d89SJeremy L. Thompson **/
3657a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
3667a982d89SJeremy L. Thompson   *f = qf->function;
367e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3687a982d89SJeremy L. Thompson }
3697a982d89SJeremy L. Thompson 
3707a982d89SJeremy L. Thompson /**
371ca94c3ddSJeremy L Thompson   @brief Get global context for a `CeedQFunction`.
3724385fb7fSSebastian Grimberg 
373ca94c3ddSJeremy L Thompson   Note: For `CeedQFunction` from the Fortran interface, this function will return the Fortran context `CeedQFunctionContext`.
3747a982d89SJeremy L. Thompson 
375ea61e9acSJeremy L Thompson   @param[in]  qf  CeedQFunction
376777ff853SJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
3777a982d89SJeremy L. Thompson 
3787a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3797a982d89SJeremy L. Thompson 
3807a982d89SJeremy L. Thompson   @ref Backend
3817a982d89SJeremy L. Thompson **/
382777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
3831485364cSJeremy L Thompson   *ctx = NULL;
3841485364cSJeremy L Thompson   if (qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(qf->ctx, ctx));
385e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3867a982d89SJeremy L. Thompson }
3877a982d89SJeremy L. Thompson 
3887a982d89SJeremy L. Thompson /**
389ca94c3ddSJeremy L Thompson   @brief Get context data of a `CeedQFunction`
390441428dfSJeremy L Thompson 
391ca94c3ddSJeremy L Thompson   @param[in]  qf       `CeedQFunction`
392ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
393ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
394441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
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 **/
4002b730f8bSJeremy L Thompson int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type, 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(CeedQFunctionContextGetData(ctx, mem_type, data));
409441428dfSJeremy L Thompson     } else {
4102b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
411441428dfSJeremy L Thompson     }
412441428dfSJeremy L Thompson   } else {
413441428dfSJeremy L Thompson     *(void **)data = NULL;
414441428dfSJeremy L Thompson   }
4151485364cSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx));
416441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
417441428dfSJeremy L Thompson }
418441428dfSJeremy L Thompson 
419441428dfSJeremy L Thompson /**
420ca94c3ddSJeremy L Thompson   @brief Restore context data of a `CeedQFunction`
421441428dfSJeremy L Thompson 
422ca94c3ddSJeremy L Thompson   @param[in]     qf   `CeedQFunction`
423ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
424441428dfSJeremy L Thompson 
425441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
426441428dfSJeremy L Thompson 
427441428dfSJeremy L Thompson   @ref Backend
428441428dfSJeremy L Thompson **/
429441428dfSJeremy L Thompson int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) {
430441428dfSJeremy L Thompson   bool                 is_writable;
431441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
432441428dfSJeremy L Thompson 
4332b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
434441428dfSJeremy L Thompson   if (ctx) {
4352b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
436441428dfSJeremy L Thompson     if (is_writable) {
4372b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
438441428dfSJeremy L Thompson     } else {
4392b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
440441428dfSJeremy L Thompson     }
441441428dfSJeremy L Thompson   }
4421485364cSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx));
443441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
444441428dfSJeremy L Thompson }
445441428dfSJeremy L Thompson 
446441428dfSJeremy L Thompson /**
447ca94c3ddSJeremy L Thompson   @brief Get true user context for a `CeedQFunction`
4484385fb7fSSebastian Grimberg 
449ca94c3ddSJeremy L Thompson   Note: For all `CeedQFunction` this function will return the user `CeedQFunctionContext` and not interface context `CeedQFunctionContext`, if any such object exists.
4507a982d89SJeremy L. Thompson 
451ca94c3ddSJeremy L Thompson   @param[in]  qf  `CeedQFunction`
452ca94c3ddSJeremy L Thompson   @param[out] ctx Variable to store `CeedQFunctionContext`
4537a982d89SJeremy L. Thompson 
4547a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4557a982d89SJeremy L. Thompson   @ref Backend
4567a982d89SJeremy L. Thompson **/
457777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
4581203703bSJeremy L Thompson   CeedQFunctionContext qf_ctx;
4591203703bSJeremy L Thompson 
4601203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &qf_ctx));
461f04ea552SJeremy L Thompson   if (qf->is_fortran) {
462d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
4631c66c397SJeremy L Thompson 
4641203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetData(qf_ctx, CEED_MEM_HOST, &fortran_ctx));
4658cb0412aSJeremy L Thompson     *ctx = fortran_ctx->inner_ctx;
4669a25c351SJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreData(qf_ctx, &fortran_ctx));
4677a982d89SJeremy L. Thompson   } else {
4681203703bSJeremy L Thompson     *ctx = qf_ctx;
4697a982d89SJeremy L. Thompson   }
4701485364cSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&qf_ctx));
471e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4727a982d89SJeremy L. Thompson }
4737a982d89SJeremy L. Thompson 
4747a982d89SJeremy L. Thompson /**
475ca94c3ddSJeremy L Thompson   @brief Get inner context data of a `CeedQFunction`
476441428dfSJeremy L Thompson 
477ca94c3ddSJeremy L Thompson   @param[in]  qf       `CeedQFunction`
478ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
479ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
480441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
481441428dfSJeremy L Thompson 
482441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
483441428dfSJeremy L Thompson 
484441428dfSJeremy L Thompson   @ref Backend
485441428dfSJeremy L Thompson **/
4862b730f8bSJeremy L Thompson int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
487441428dfSJeremy L Thompson   bool                 is_writable;
488441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
489441428dfSJeremy L Thompson 
4902b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
491441428dfSJeremy L Thompson   if (ctx) {
4922b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
493441428dfSJeremy L Thompson     if (is_writable) {
4942b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
495441428dfSJeremy L Thompson     } else {
4962b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
497441428dfSJeremy L Thompson     }
498441428dfSJeremy L Thompson   } else {
499441428dfSJeremy L Thompson     *(void **)data = NULL;
500441428dfSJeremy L Thompson   }
501441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
502441428dfSJeremy L Thompson }
503441428dfSJeremy L Thompson 
504441428dfSJeremy L Thompson /**
505ca94c3ddSJeremy L Thompson   @brief Restore inner context data of a `CeedQFunction`
506441428dfSJeremy L Thompson 
507ca94c3ddSJeremy L Thompson   @param[in]     qf   `CeedQFunction`
508ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
509441428dfSJeremy L Thompson 
510441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
511441428dfSJeremy L Thompson 
512441428dfSJeremy L Thompson   @ref Backend
513441428dfSJeremy L Thompson **/
514441428dfSJeremy L Thompson int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) {
515441428dfSJeremy L Thompson   bool                 is_writable;
516441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
517441428dfSJeremy L Thompson 
5182b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
519441428dfSJeremy L Thompson   if (ctx) {
5202b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
521441428dfSJeremy L Thompson     if (is_writable) {
5222b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
523441428dfSJeremy L Thompson     } else {
5242b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
525441428dfSJeremy L Thompson     }
526441428dfSJeremy L Thompson   }
5275f249b39SJeremy L Thompson   *(void **)data = NULL;
528441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
529441428dfSJeremy L Thompson }
530441428dfSJeremy L Thompson 
531441428dfSJeremy L Thompson /**
532ca94c3ddSJeremy L Thompson   @brief Determine if `CeedQFunction` is identity
5337a982d89SJeremy L. Thompson 
534ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
535d1d35e2fSjeremylt   @param[out] is_identity Variable to store identity status
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 **/
541d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
542f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
543e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5447a982d89SJeremy L. Thompson }
5457a982d89SJeremy L. Thompson 
5467a982d89SJeremy L. Thompson /**
547ca94c3ddSJeremy L Thompson   @brief Determine if `CeedQFunctionContext` is writable
548441428dfSJeremy L Thompson 
549ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
550ea61e9acSJeremy L Thompson   @param[out] is_writable Variable to store context writeable status
551441428dfSJeremy L Thompson 
552441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
553441428dfSJeremy L Thompson 
554441428dfSJeremy L Thompson   @ref Backend
555441428dfSJeremy L Thompson **/
556441428dfSJeremy L Thompson int CeedQFunctionIsContextWritable(CeedQFunction qf, bool *is_writable) {
557441428dfSJeremy L Thompson   *is_writable = qf->is_context_writable;
558441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
559441428dfSJeremy L Thompson }
560441428dfSJeremy L Thompson 
561441428dfSJeremy L Thompson /**
562ca94c3ddSJeremy L Thompson   @brief Get backend data of a `CeedQFunction`
5637a982d89SJeremy L. Thompson 
564ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction`
5657a982d89SJeremy L. Thompson   @param[out] data Variable to store data
5667a982d89SJeremy L. Thompson 
5677a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5687a982d89SJeremy L. Thompson 
5697a982d89SJeremy L. Thompson   @ref Backend
5707a982d89SJeremy L. Thompson **/
571777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
572777ff853SJeremy L Thompson   *(void **)data = qf->data;
573e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5747a982d89SJeremy L. Thompson }
5757a982d89SJeremy L. Thompson 
5767a982d89SJeremy L. Thompson /**
577ca94c3ddSJeremy L Thompson   @brief Set backend data of a `CeedQFunction`
5787a982d89SJeremy L. Thompson 
579ca94c3ddSJeremy L Thompson   @param[in,out] qf   `CeedQFunction`
580ea61e9acSJeremy L Thompson   @param[in]     data Data to set
5817a982d89SJeremy L. Thompson 
5827a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5837a982d89SJeremy L. Thompson 
5847a982d89SJeremy L. Thompson   @ref Backend
5857a982d89SJeremy L. Thompson **/
586777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
587777ff853SJeremy L Thompson   qf->data = data;
588e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5897a982d89SJeremy L. Thompson }
5907a982d89SJeremy L. Thompson 
5917a982d89SJeremy L. Thompson /**
5921203703bSJeremy L Thompson   @brief Get a boolean value indicating if the `CeedQFunction` is immutable
5931203703bSJeremy L Thompson 
5941203703bSJeremy L Thompson   @param[in]  qf           `CeedOperator`
5951203703bSJeremy L Thompson   @param[out] is_immutable Variable to store immutability status
5961203703bSJeremy L Thompson 
5971203703bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5981203703bSJeremy L Thompson 
5991203703bSJeremy L Thompson   @ref Backend
6001203703bSJeremy L Thompson **/
6011203703bSJeremy L Thompson int CeedQFunctionIsImmutable(CeedQFunction qf, bool *is_immutable) {
6021203703bSJeremy L Thompson   *is_immutable = qf->is_immutable;
6031203703bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
6041203703bSJeremy L Thompson }
6051203703bSJeremy L Thompson 
6061203703bSJeremy L Thompson /**
6071203703bSJeremy L Thompson   @brief Set the immutable flag of a `CeedQFunction` to `true`
6081203703bSJeremy L Thompson 
6091203703bSJeremy L Thompson   @param[in,out] qf `CeedQFunction`
6101203703bSJeremy L Thompson 
6111203703bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
6121203703bSJeremy L Thompson 
6131203703bSJeremy L Thompson   @ref Backend
6141203703bSJeremy L Thompson **/
6151203703bSJeremy L Thompson int CeedQFunctionSetImmutable(CeedQFunction qf) {
6161203703bSJeremy L Thompson   qf->is_immutable = true;
6171203703bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
6181203703bSJeremy L Thompson }
6191203703bSJeremy L Thompson 
6201203703bSJeremy L Thompson /**
621ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedQFunction`
62234359f16Sjeremylt 
623ca94c3ddSJeremy L Thompson   @param[in,out] qf `CeedQFunction` to increment the reference counter
62434359f16Sjeremylt 
62534359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
62634359f16Sjeremylt 
62734359f16Sjeremylt   @ref Backend
62834359f16Sjeremylt **/
6299560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
630b0f67a9cSJeremy L Thompson   CeedCall(CeedObjectReference((CeedObject)qf));
63134359f16Sjeremylt   return CEED_ERROR_SUCCESS;
63234359f16Sjeremylt }
63334359f16Sjeremylt 
6346e15d496SJeremy L Thompson /**
635ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs per quadrature required to apply `CeedQFunction`
6366e15d496SJeremy L Thompson 
637ca94c3ddSJeremy L Thompson   @param[in]  qf    `CeedQFunction` to estimate FLOPs for
638ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
6396e15d496SJeremy L Thompson 
6406e15d496SJeremy L Thompson   @ref Backend
6416e15d496SJeremy L Thompson **/
6429d36ca50SJeremy L Thompson int CeedQFunctionGetFlopsEstimate(CeedQFunction qf, CeedSize *flops) {
6436e15d496SJeremy L Thompson   *flops = qf->user_flop_estimate;
6446e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6456e15d496SJeremy L Thompson }
6466e15d496SJeremy L Thompson 
6477a982d89SJeremy L. Thompson /// @}
6487a982d89SJeremy L. Thompson 
6497a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6507a982d89SJeremy L. Thompson /// CeedQFunction Public API
6517a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6527a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
6537a982d89SJeremy L. Thompson /// @{
6547a982d89SJeremy L. Thompson 
6557a982d89SJeremy L. Thompson /**
656ca94c3ddSJeremy L Thompson   @brief Create a `CeedQFunction` for evaluating interior (volumetric) terms
6577a982d89SJeremy L. Thompson 
658ca94c3ddSJeremy L Thompson   @param[in]  ceed       `Ceed` object used to create the `CeedQFunction`
659ca94c3ddSJeremy L Thompson   @param[in]  vec_length Vector length.
660ca94c3ddSJeremy L Thompson                            Caller must ensure that number of quadrature points is a multiple of `vec_length`.
661ea61e9acSJeremy L Thompson   @param[in]  f          Function pointer to evaluate action at quadrature points.
662ca94c3ddSJeremy L Thompson                            See `CeedQFunctionUser`.
663ca94c3ddSJeremy L Thompson   @param[in]  source     Absolute path to source of `CeedQFunctionUser`, "\abs_path\file.h:function_name".
664ca94c3ddSJeremy 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.).
6654ada38baSJames Wright                            The entire contents of this file and all locally included files are used during JiT compilation for GPU backends.
666c0b5abf0SJeremy L Thompson                            The header `ceed/types.h` is preferred over `ceed.h` or `ceed/ceed.h` for `CeedQFunction` source files.
667c0b5abf0SJeremy 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`.
6684ada38baSJames Wright                            All source files must be at the provided filepath at runtime for JiT to function.
669ca94c3ddSJeremy L Thompson   @param[out] qf         Address of the variable where the newly created `CeedQFunction` will be stored
6707a982d89SJeremy L. Thompson 
6717a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6727a982d89SJeremy L. Thompson 
673ca94c3ddSJeremy L Thompson   See \ref CeedQFunctionUser for details on the call-back function `f` arguments.
6747a982d89SJeremy L. Thompson 
6757a982d89SJeremy L. Thompson   @ref User
6767a982d89SJeremy L. Thompson **/
6772b730f8bSJeremy L Thompson int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length, CeedQFunctionUser f, const char *source, CeedQFunction *qf) {
678ca5eadf8SJeremy L Thompson   char *user_source_copy;
6797a982d89SJeremy L. Thompson 
6807a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
6817a982d89SJeremy L. Thompson     Ceed delegate;
6826574a04fSJeremy L Thompson 
6832b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "QFunction"));
6841ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedQFunctionCreateInterior");
6852b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf));
6869bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
687e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
6887a982d89SJeremy L. Thompson   }
6897a982d89SJeremy L. Thompson 
6906574a04fSJeremy L Thompson   CeedCheck(!strlen(source) || strrchr(source, ':'), ceed, CEED_ERROR_INCOMPLETE,
6916574a04fSJeremy L Thompson             "Provided path to source does not include function name. Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"", source);
69243e1b16fSJeremy L Thompson 
6932b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, qf));
6946c328a79SJeremy L Thompson   CeedCall(CeedObjectCreate(ceed, CeedQFunctionView_Object, CeedQFunctionDestroy_Object, &(*qf)->obj));
695d1d35e2fSjeremylt   (*qf)->vec_length          = vec_length;
696f04ea552SJeremy L Thompson   (*qf)->is_identity         = false;
697441428dfSJeremy L Thompson   (*qf)->is_context_writable = true;
6987a982d89SJeremy L. Thompson   (*qf)->function            = f;
6996e15d496SJeremy L Thompson   (*qf)->user_flop_estimate  = -1;
70043e1b16fSJeremy L Thompson   if (strlen(source)) {
701ca5eadf8SJeremy L Thompson     size_t user_source_len = strlen(source);
702ee5a26f2SJeremy L Thompson 
7032b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(user_source_len + 1, &user_source_copy));
704ca5eadf8SJeremy L Thompson     memcpy(user_source_copy, source, user_source_len);
705ca5eadf8SJeremy L Thompson     (*qf)->user_source = user_source_copy;
70643e1b16fSJeremy L Thompson   }
7072b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields));
7082b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields));
7092b730f8bSJeremy L Thompson   CeedCall(ceed->QFunctionCreate(*qf));
710e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7117a982d89SJeremy L. Thompson }
7127a982d89SJeremy L. Thompson 
7137a982d89SJeremy L. Thompson /**
714ca94c3ddSJeremy L Thompson   @brief Create a `CeedQFunction` for evaluating interior (volumetric) terms by name
715288c0443SJeremy L Thompson 
716ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedQFunction`
717ca94c3ddSJeremy L Thompson   @param[in]  name Name of `CeedQFunction` to use from gallery
718ca94c3ddSJeremy L Thompson   @param[out] qf   Address of the variable where the newly created `CeedQFunction` will be stored
719288c0443SJeremy L Thompson 
720288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
721288c0443SJeremy L Thompson 
7227a982d89SJeremy L. Thompson   @ref User
723288c0443SJeremy L Thompson **/
7242b730f8bSJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, CeedQFunction *qf) {
725f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
726288c0443SJeremy L Thompson 
7272b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionRegisterAll());
728288c0443SJeremy L Thompson   // Find matching backend
729ca94c3ddSJeremy L Thompson   CeedCheck(name, ceed, CEED_ERROR_INCOMPLETE, "No CeedQFunction name provided");
730288c0443SJeremy L Thompson   for (size_t i = 0; i < num_qfunctions; i++) {
731288c0443SJeremy L Thompson     size_t      n;
732d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
7332b730f8bSJeremy L Thompson     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {
7342b730f8bSJeremy L Thompson     }
735d1d35e2fSjeremylt     if (n > match_len) {
736d1d35e2fSjeremylt       match_len   = n;
737f7e22acaSJeremy L Thompson       match_index = i;
738288c0443SJeremy L Thompson     }
739288c0443SJeremy L Thompson   }
740ca94c3ddSJeremy L Thompson   CeedCheck(match_len > 0, ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery CeedQFunction");
741288c0443SJeremy L Thompson 
742288c0443SJeremy L Thompson   // Create QFunction
7432b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInterior(ceed, gallery_qfunctions[match_index].vec_length, gallery_qfunctions[match_index].f,
7442b730f8bSJeremy L Thompson                                        gallery_qfunctions[match_index].source, qf));
745288c0443SJeremy L Thompson 
746288c0443SJeremy L Thompson   // QFunction specific setup
7472b730f8bSJeremy L Thompson   CeedCall(gallery_qfunctions[match_index].init(ceed, name, *qf));
748288c0443SJeremy L Thompson 
74975affc3bSjeremylt   // Copy name
7502b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name));
75143e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
752e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
753288c0443SJeremy L Thompson }
754288c0443SJeremy L Thompson 
755288c0443SJeremy L Thompson /**
756ca94c3ddSJeremy L Thompson   @brief Create an identity `CeedQFunction`.
7574385fb7fSSebastian Grimberg 
758ea61e9acSJeremy L Thompson   Inputs are written into outputs in the order given.
759ca94c3ddSJeremy 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.
760ca94c3ddSJeremy 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.
7610219ea01SJeremy L Thompson 
762ca94c3ddSJeremy L Thompson   @param[in]  ceed     `Ceed` object used to create the `CeedQFunction`
763ca94c3ddSJeremy L Thompson   @param[in]  size     Size of the `CeedQFunction` fields
764ca94c3ddSJeremy L Thompson   @param[in]  in_mode  @ref CeedEvalMode for input to `CeedQFunction`
765ca94c3ddSJeremy L Thompson   @param[in]  out_mode @ref CeedEvalMode for output to `CeedQFunction`
766ca94c3ddSJeremy L Thompson   @param[out] qf       Address of the variable where the newly created `CeedQFunction` will be stored
7670219ea01SJeremy L Thompson 
7680219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
7690219ea01SJeremy L Thompson 
7707a982d89SJeremy L. Thompson   @ref User
7710219ea01SJeremy L Thompson **/
7722b730f8bSJeremy L Thompson int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, CeedEvalMode out_mode, CeedQFunction *qf) {
7731c66c397SJeremy L Thompson   CeedQFunctionContext  ctx;
7741c66c397SJeremy L Thompson   CeedContextFieldLabel size_label;
7751c66c397SJeremy L Thompson 
7762b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Identity", qf));
7772b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(*qf, "input", size, in_mode));
7782b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(*qf, "output", size, out_mode));
7790219ea01SJeremy L Thompson 
780f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
781547dbd6fSJeremy L Thompson 
7822b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(*qf, &ctx));
7832b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label));
7842b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetInt32(ctx, size_label, &size));
7851485364cSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx));
786e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7870219ea01SJeremy L Thompson }
7880219ea01SJeremy L Thompson 
7890219ea01SJeremy L Thompson /**
790ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedQFunction`.
7914385fb7fSSebastian Grimberg 
792ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedQFunctionDestroy().
793512bb800SJeremy L Thompson 
794ca94c3ddSJeremy 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`.
795ca94c3ddSJeremy L Thompson         This `CeedQFunction` will be destroyed if `*qf_copy` is the only reference to this `CeedQFunction`.
7969560d06aSjeremylt 
797ca94c3ddSJeremy L Thompson   @param[in]  qf      `CeedQFunction` to copy reference to
7989560d06aSjeremylt   @param[out] qf_copy Variable to store copied reference
7999560d06aSjeremylt 
8009560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
8019560d06aSjeremylt 
8029560d06aSjeremylt   @ref User
8039560d06aSjeremylt **/
8049560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
8052b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionReference(qf));
8062b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(qf_copy));
8079560d06aSjeremylt   *qf_copy = qf;
8089560d06aSjeremylt   return CEED_ERROR_SUCCESS;
8099560d06aSjeremylt }
8109560d06aSjeremylt 
8119560d06aSjeremylt /**
812ca94c3ddSJeremy L Thompson   @brief Add a `CeedQFunction` input
813b11c1e72Sjeremylt 
814ca94c3ddSJeremy L Thompson   @param[in,out] qf         `CeedQFunction`
815ca94c3ddSJeremy L Thompson   @param[in]     field_name Name of `CeedQFunction` field
816d538d163SJeremy L Thompson   @param[in]     size       Size of `CeedQFunction` field,
817d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_NONE,
818d538d163SJeremy 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,
819d538d163SJeremy L Thompson                               (`num_comp * dim`) for @ref CEED_EVAL_GRAD,
820d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_DIV, and
821d538d163SJeremy L Thompson                               (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` otherwise `curl_dim = dim` for @ref CEED_EVAL_CURL.
822ca94c3ddSJeremy L Thompson   @param[in]     eval_mode  @ref CEED_EVAL_NONE to use values directly,
823ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_INTERP to use interpolated values,
824ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_GRAD to use gradients,
825ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_DIV to use divergence,
826ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_CURL to use curl
827b11c1e72Sjeremylt 
828d538d163SJeremy L Thompson   Note: In the user `CeedQFunctionUser`, the `in` argument list the fields in the order given by the calls to `CeedQFunctionAddInput`.
829d538d163SJeremy L Thompson 
830b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
831dfdf5a53Sjeremylt 
8327a982d89SJeremy L. Thompson   @ref User
833b11c1e72Sjeremylt **/
8342b730f8bSJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
8351203703bSJeremy L Thompson   bool is_immutable;
8361203703bSJeremy L Thompson 
8371203703bSJeremy L Thompson   CeedCall(CeedQFunctionIsImmutable(qf, &is_immutable));
8389bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedQFunctionReturnCeed(qf), CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
8399bc66399SJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT || size == 1, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION, "CEED_EVAL_WEIGHT should have size 1");
840643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
8419bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8423f08121cSJeremy L Thompson               "CeedQFunction field names must be unique. Duplicate name: %s", field_name);
843643fbb69SJeremy L Thompson   }
844643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
8459bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8463f08121cSJeremy L Thompson               "CeedQFunction field names must be unique. Duplicate name: %s", field_name);
847643fbb69SJeremy L Thompson   }
8482b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], field_name, size, eval_mode));
849d1d35e2fSjeremylt   qf->num_input_fields++;
850e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
851d7b241e6Sjeremylt }
852d7b241e6Sjeremylt 
853b11c1e72Sjeremylt /**
854ca94c3ddSJeremy L Thompson   @brief Add a `CeedQFunction` output
855b11c1e72Sjeremylt 
856ca94c3ddSJeremy L Thompson   @param[in,out] qf         `CeedQFunction`
857ca94c3ddSJeremy L Thompson   @param[in]     field_name Name of `CeedQFunction` field
858d538d163SJeremy L Thompson   @param[in]     size       Size of `CeedQFunction` field,
859d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_NONE,
860d538d163SJeremy 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,
861d538d163SJeremy L Thompson                               (`num_comp * dim`) for @ref CEED_EVAL_GRAD,
862d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_DIV, and
863d538d163SJeremy L Thompson                               (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` otherwise `curl_dim = dim` for @ref CEED_EVAL_CURL.
864ca94c3ddSJeremy L Thompson   @param[in]     eval_mode  @ref CEED_EVAL_NONE to use values directly,
865ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_INTERP to use interpolated values,
866ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_GRAD to use gradients,
867ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_DIV to use divergence,
868ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_CURL to use curl.
869b11c1e72Sjeremylt 
870d538d163SJeremy L Thompson   Note: In the user `CeedQFunctionUser`, the `out` argument list the fields in the order given by the calls to `CeedQFunctionAddOutput`.
871d538d163SJeremy L Thompson 
872b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
873dfdf5a53Sjeremylt 
8747a982d89SJeremy L. Thompson   @ref User
875b11c1e72Sjeremylt **/
8762b730f8bSJeremy L Thompson int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
8771203703bSJeremy L Thompson   bool is_immutable;
8781203703bSJeremy L Thompson 
8791203703bSJeremy L Thompson   CeedCall(CeedQFunctionIsImmutable(qf, &is_immutable));
8809bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedQFunctionReturnCeed(qf), CEED_ERROR_MAJOR, "CeedQFunction cannot be changed after set as immutable");
8819bc66399SJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION,
8829bc66399SJeremy L Thompson             "Cannot create CeedQFunction output with CEED_EVAL_WEIGHT");
883643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
8849bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8859bc66399SJeremy L Thompson               "CeedQFunction field names must be unique");
886643fbb69SJeremy L Thompson   }
887643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
8889bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8899bc66399SJeremy L Thompson               "CeedQFunction field names must be unique");
890643fbb69SJeremy L Thompson   }
8912b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], field_name, size, eval_mode));
892d1d35e2fSjeremylt   qf->num_output_fields++;
893e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
894d7b241e6Sjeremylt }
895d7b241e6Sjeremylt 
896dfdf5a53Sjeremylt /**
897ca94c3ddSJeremy L Thompson   @brief Get the `CeedQFunctionField` of a `CeedQFunction`
89843bbe138SJeremy L Thompson 
899ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedQFunction` as immutable.
900f04ea552SJeremy L Thompson 
901ca94c3ddSJeremy L Thompson   @param[in]  qf                `CeedQFunction`
902f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
903f74ec584SJeremy L Thompson   @param[out] input_fields      Variable to store input fields
904f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
905f74ec584SJeremy L Thompson   @param[out] output_fields     Variable to store output fields
90643bbe138SJeremy L Thompson 
90743bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
90843bbe138SJeremy L Thompson 
909e9b533fbSJeremy L Thompson   @ref Advanced
91043bbe138SJeremy L Thompson **/
9112b730f8bSJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields, CeedQFunctionField **input_fields, CeedInt *num_output_fields,
91243bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
9131203703bSJeremy L Thompson   CeedCall(CeedQFunctionSetImmutable(qf));
91443bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
91543bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
91643bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
91743bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
91843bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
91943bbe138SJeremy L Thompson }
92043bbe138SJeremy L Thompson 
92143bbe138SJeremy L Thompson /**
922ca94c3ddSJeremy L Thompson   @brief Get the name of a `CeedQFunctionField`
92343bbe138SJeremy L Thompson 
924ca94c3ddSJeremy L Thompson   @param[in]  qf_field   `CeedQFunctionField`
92543bbe138SJeremy L Thompson   @param[out] field_name Variable to store the field name
92643bbe138SJeremy L Thompson 
92743bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
92843bbe138SJeremy L Thompson 
929e9b533fbSJeremy L Thompson   @ref Advanced
93043bbe138SJeremy L Thompson **/
9316f8994e9SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, const char **field_name) {
9326f8994e9SJeremy L Thompson   *field_name = qf_field->field_name;
93343bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
93443bbe138SJeremy L Thompson }
93543bbe138SJeremy L Thompson 
93643bbe138SJeremy L Thompson /**
937ca94c3ddSJeremy L Thompson   @brief Get the number of components of a `CeedQFunctionField`
93843bbe138SJeremy L Thompson 
939ca94c3ddSJeremy L Thompson   @param[in]  qf_field `CeedQFunctionField`
94043bbe138SJeremy L Thompson   @param[out] size     Variable to store the size of the field
94143bbe138SJeremy L Thompson 
94243bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
94343bbe138SJeremy L Thompson 
944e9b533fbSJeremy L Thompson   @ref Advanced
94543bbe138SJeremy L Thompson **/
94643bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
94743bbe138SJeremy L Thompson   *size = qf_field->size;
94843bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
94943bbe138SJeremy L Thompson }
95043bbe138SJeremy L Thompson 
95143bbe138SJeremy L Thompson /**
952ca94c3ddSJeremy L Thompson   @brief Get the @ref CeedEvalMode of a `CeedQFunctionField`
95343bbe138SJeremy L Thompson 
954ca94c3ddSJeremy L Thompson   @param[in]  qf_field  `CeedQFunctionField`
95543bbe138SJeremy L Thompson   @param[out] eval_mode Variable to store the field evaluation mode
95643bbe138SJeremy L Thompson 
95743bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
95843bbe138SJeremy L Thompson 
959e9b533fbSJeremy L Thompson   @ref Advanced
96043bbe138SJeremy L Thompson **/
9612b730f8bSJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, CeedEvalMode *eval_mode) {
96243bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
96343bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
96443bbe138SJeremy L Thompson }
96543bbe138SJeremy L Thompson 
96643bbe138SJeremy L Thompson /**
967ab747706SJeremy L Thompson   @brief Get the data of a `CeedQFunctionField`.
968ab747706SJeremy L Thompson 
969ab747706SJeremy L Thompson   Any arguments set as `NULL` are ignored.
970ab747706SJeremy L Thompson 
971ab747706SJeremy L Thompson   @param[in]  qf_field   `CeedQFunctionField`
972ab747706SJeremy L Thompson   @param[out] field_name Variable to store the field name
973ab747706SJeremy L Thompson   @param[out] size       Variable to store the size of the field
974ab747706SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
975ab747706SJeremy L Thompson 
976ab747706SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
977ab747706SJeremy L Thompson 
978ab747706SJeremy L Thompson   @ref Advanced
979ab747706SJeremy L Thompson **/
9806f8994e9SJeremy L Thompson int CeedQFunctionFieldGetData(CeedQFunctionField qf_field, const char **field_name, CeedInt *size, CeedEvalMode *eval_mode) {
981ab747706SJeremy L Thompson   if (field_name) CeedCall(CeedQFunctionFieldGetName(qf_field, field_name));
982ab747706SJeremy L Thompson   if (size) CeedCall(CeedQFunctionFieldGetSize(qf_field, size));
983ab747706SJeremy L Thompson   if (eval_mode) CeedCall(CeedQFunctionFieldGetEvalMode(qf_field, eval_mode));
984ab747706SJeremy L Thompson   return CEED_ERROR_SUCCESS;
985ab747706SJeremy L Thompson }
986ab747706SJeremy L Thompson 
987ab747706SJeremy L Thompson /**
988ca94c3ddSJeremy L Thompson   @brief Set global context for a `CeedQFunction`
989b11c1e72Sjeremylt 
990ca94c3ddSJeremy L Thompson   @param[in,out] qf  `CeedQFunction`
991ea61e9acSJeremy L Thompson   @param[in]     ctx Context data to set
992b11c1e72Sjeremylt 
993b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
994dfdf5a53Sjeremylt 
9957a982d89SJeremy L. Thompson   @ref User
996b11c1e72Sjeremylt **/
997777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
9982b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&qf->ctx));
999d7b241e6Sjeremylt   qf->ctx = ctx;
1000db002c03SJeremy L Thompson   if (ctx) CeedCall(CeedQFunctionContextReference(ctx));
1001e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1002d7b241e6Sjeremylt }
1003d7b241e6Sjeremylt 
1004b11c1e72Sjeremylt /**
1005ca94c3ddSJeremy L Thompson   @brief Set writability of `CeedQFunctionContext` when calling the `CeedQFunctionUser`.
10064385fb7fSSebastian Grimberg 
1007859c15bbSJames Wright   The default value is `is_writable == true`.
1008441428dfSJeremy L Thompson 
1009ca94c3ddSJeremy L Thompson   Setting `is_writable == true` indicates the `CeedQFunctionUser` writes into the `CeedQFunctionContext` and requires memory synchronization after calling @ref CeedQFunctionApply().
1010441428dfSJeremy L Thompson 
1011ca94c3ddSJeremy L Thompson   Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not modify the `CeedQFunctionContext`.
1012ea61e9acSJeremy L Thompson   Violating this assertion may lead to inconsistent data.
1013441428dfSJeremy L Thompson 
1014441428dfSJeremy L Thompson   Setting `is_writable == false` may offer a performance improvement on GPU backends.
1015441428dfSJeremy L Thompson 
1016ca94c3ddSJeremy L Thompson   @param[in,out] qf          `CeedQFunction`
1017ca94c3ddSJeremy L Thompson   @param[in]     is_writable Boolean flag for writability status
1018441428dfSJeremy L Thompson 
1019441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1020441428dfSJeremy L Thompson 
1021441428dfSJeremy L Thompson   @ref User
1022441428dfSJeremy L Thompson **/
1023441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
1024441428dfSJeremy L Thompson   qf->is_context_writable = is_writable;
1025441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1026441428dfSJeremy L Thompson }
1027441428dfSJeremy L Thompson 
1028441428dfSJeremy L Thompson /**
1029ca94c3ddSJeremy L Thompson   @brief Set estimated number of FLOPs per quadrature required to apply `CeedQFunction`
10306e15d496SJeremy L Thompson 
1031ca94c3ddSJeremy L Thompson   @param[in]  qf    `CeedQFunction` to estimate FLOPs for
1032ea61e9acSJeremy L Thompson   @param[out] flops FLOPs per quadrature point estimate
10336e15d496SJeremy L Thompson 
10346e15d496SJeremy L Thompson   @ref Backend
10356e15d496SJeremy L Thompson **/
10369d36ca50SJeremy L Thompson int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
10376e536b99SJeremy L Thompson   CeedCheck(flops >= 0, CeedQFunctionReturnCeed(qf), CEED_ERROR_INCOMPATIBLE, "Must set non-negative FLOPs estimate");
10386e15d496SJeremy L Thompson   qf->user_flop_estimate = flops;
10396e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10406e15d496SJeremy L Thompson }
10416e15d496SJeremy L Thompson 
10426e15d496SJeremy L Thompson /**
10434c789ea2SJeremy L Thompson   @brief Set the number of tabs to indent for @ref CeedQFunctionView() output
10444c789ea2SJeremy L Thompson 
10454c789ea2SJeremy L Thompson   @param[in] qf       `CeedQFunction` to set the number of view tabs
10464c789ea2SJeremy L Thompson   @param[in] num_tabs Number of view tabs to set
10474c789ea2SJeremy L Thompson 
10484c789ea2SJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
10494c789ea2SJeremy L Thompson 
10504c789ea2SJeremy L Thompson   @ref User
10514c789ea2SJeremy L Thompson **/
10524c789ea2SJeremy L Thompson int CeedQFunctionSetNumViewTabs(CeedQFunction qf, CeedInt num_tabs) {
1053a299a25bSJeremy L Thompson   CeedCall(CeedObjectSetNumViewTabs((CeedObject)qf, num_tabs));
10544c789ea2SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10554c789ea2SJeremy L Thompson }
10564c789ea2SJeremy L Thompson 
10574c789ea2SJeremy L Thompson /**
1058690992b2SZach Atkins   @brief Get the number of tabs to indent for @ref CeedQFunctionView() output
1059690992b2SZach Atkins 
1060690992b2SZach Atkins   @param[in]  qf       `CeedQFunction` to get the number of view tabs
1061690992b2SZach Atkins   @param[out] num_tabs Number of view tabs
1062690992b2SZach Atkins 
1063690992b2SZach Atkins   @return Error code: 0 - success, otherwise - failure
1064690992b2SZach Atkins 
1065690992b2SZach Atkins   @ref User
1066690992b2SZach Atkins **/
1067690992b2SZach Atkins int CeedQFunctionGetNumViewTabs(CeedQFunction qf, CeedInt *num_tabs) {
1068a299a25bSJeremy L Thompson   CeedCall(CeedObjectGetNumViewTabs((CeedObject)qf, num_tabs));
1069690992b2SZach Atkins   return CEED_ERROR_SUCCESS;
1070690992b2SZach Atkins }
1071690992b2SZach Atkins 
1072690992b2SZach Atkins /**
1073ca94c3ddSJeremy L Thompson   @brief View a `CeedQFunction`
107475affc3bSjeremylt 
1075ca94c3ddSJeremy L Thompson   @param[in] qf     `CeedQFunction` to view
1076ca94c3ddSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
107775affc3bSjeremylt 
107875affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
107975affc3bSjeremylt 
10807a982d89SJeremy L. Thompson   @ref User
108175affc3bSjeremylt **/
108275affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
10834c789ea2SJeremy L Thompson   char       *tabs = NULL;
1084d3d5610dSJeremy L Thompson   const char *name;
108575affc3bSjeremylt 
10864c789ea2SJeremy L Thompson   {
10874c789ea2SJeremy L Thompson     CeedInt num_tabs = 0;
10884c789ea2SJeremy L Thompson 
10894c789ea2SJeremy L Thompson     CeedCall(CeedQFunctionGetNumViewTabs(qf, &num_tabs));
10904c789ea2SJeremy L Thompson     CeedCall(CeedCalloc(CEED_TAB_WIDTH * num_tabs + 1, &tabs));
10914c789ea2SJeremy L Thompson     for (CeedInt i = 0; i < CEED_TAB_WIDTH * num_tabs; i++) tabs[i] = ' ';
10924c789ea2SJeremy L Thompson   }
10934c789ea2SJeremy L Thompson 
1094d3d5610dSJeremy L Thompson   CeedCall(CeedQFunctionGetName(qf, &name));
10954c789ea2SJeremy L Thompson   fprintf(stream, "%s%sCeedQFunction - %s\n", tabs, qf->is_gallery ? "Gallery " : "User ", name);
109675affc3bSjeremylt 
10974c789ea2SJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", tabs, qf->num_input_fields, qf->num_input_fields > 1 ? "s" : "");
1098d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
10994c789ea2SJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->input_fields[i], i, 1, tabs, stream));
110075affc3bSjeremylt   }
110175affc3bSjeremylt 
11024c789ea2SJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", tabs, qf->num_output_fields, qf->num_output_fields > 1 ? "s" : "");
1103d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
11044c789ea2SJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->output_fields[i], i, 0, tabs, stream));
110575affc3bSjeremylt   }
11064c789ea2SJeremy L Thompson   CeedCall(CeedFree(&tabs));
1107e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
110875affc3bSjeremylt }
110975affc3bSjeremylt 
111075affc3bSjeremylt /**
1111ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedQFunction`
1112b7c9bbdaSJeremy L Thompson 
1113ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction`
1114ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store`Ceed`
1115b7c9bbdaSJeremy L Thompson 
1116b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1117b7c9bbdaSJeremy L Thompson 
1118b7c9bbdaSJeremy L Thompson   @ref Advanced
1119b7c9bbdaSJeremy L Thompson **/
1120b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
1121b0f67a9cSJeremy L Thompson   CeedCall(CeedObjectGetCeed((CeedObject)qf, ceed));
1122b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1123b7c9bbdaSJeremy L Thompson }
1124b7c9bbdaSJeremy L Thompson 
1125b7c9bbdaSJeremy L Thompson /**
11266e536b99SJeremy L Thompson   @brief Return the `Ceed` associated with a `CeedQFunction`
11276e536b99SJeremy L Thompson 
11286e536b99SJeremy L Thompson   @param[in]  qf   `CeedQFunction`
11296e536b99SJeremy L Thompson 
11306e536b99SJeremy L Thompson   @return `Ceed` associated with the `qf`
11316e536b99SJeremy L Thompson 
11326e536b99SJeremy L Thompson   @ref Advanced
11336e536b99SJeremy L Thompson **/
1134b0f67a9cSJeremy L Thompson Ceed CeedQFunctionReturnCeed(CeedQFunction qf) { return CeedObjectReturnCeed((CeedObject)qf); }
11356e536b99SJeremy L Thompson 
11366e536b99SJeremy L Thompson /**
1137ca94c3ddSJeremy L Thompson   @brief Apply the action of a `CeedQFunction`
1138b11c1e72Sjeremylt 
1139ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedQFunction` as immutable.
1140f04ea552SJeremy L Thompson 
1141ca94c3ddSJeremy L Thompson   @param[in]  qf `CeedQFunction`
1142ea61e9acSJeremy L Thompson   @param[in]  Q  Number of quadrature points
1143ca94c3ddSJeremy L Thompson   @param[in]  u  Array of input `CeedVector`
1144ca94c3ddSJeremy L Thompson   @param[out] v  Array of output `CeedVector`
1145b11c1e72Sjeremylt 
1146b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1147dfdf5a53Sjeremylt 
11487a982d89SJeremy L. Thompson   @ref User
1149b11c1e72Sjeremylt **/
11502b730f8bSJeremy L Thompson int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, CeedVector *u, CeedVector *v) {
11511203703bSJeremy L Thompson   CeedInt vec_length;
11521203703bSJeremy L Thompson 
11539bc66399SJeremy L Thompson   CeedCheck(qf->Apply, CeedQFunctionReturnCeed(qf), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionApply");
11541203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetVectorLength(qf, &vec_length));
11559bc66399SJeremy L Thompson   CeedCheck(Q % vec_length == 0, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION,
11569bc66399SJeremy L Thompson             "Number of quadrature points %" CeedInt_FMT " must be a multiple of %" CeedInt_FMT, Q, qf->vec_length);
11571203703bSJeremy L Thompson   CeedCall(CeedQFunctionSetImmutable(qf));
11582b730f8bSJeremy L Thompson   CeedCall(qf->Apply(qf, Q, u, v));
1159e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1160d7b241e6Sjeremylt }
1161d7b241e6Sjeremylt 
1162b11c1e72Sjeremylt /**
1163ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedQFunction`
1164b11c1e72Sjeremylt 
1165ca94c3ddSJeremy L Thompson   @param[in,out] qf `CeedQFunction` to destroy
1166b11c1e72Sjeremylt 
1167b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1168dfdf5a53Sjeremylt 
11697a982d89SJeremy L. Thompson   @ref User
1170b11c1e72Sjeremylt **/
1171d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
1172b0f67a9cSJeremy L Thompson   if (!*qf || CeedObjectDereference((CeedObject)*qf) > 0) {
1173ad6481ceSJeremy L Thompson     *qf = NULL;
1174ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1175ad6481ceSJeremy L Thompson   }
1176fe2413ffSjeremylt   // Backend destroy
1177d7b241e6Sjeremylt   if ((*qf)->Destroy) {
11782b730f8bSJeremy L Thompson     CeedCall((*qf)->Destroy(*qf));
1179d7b241e6Sjeremylt   }
1180fe2413ffSjeremylt   // Free fields
118192ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_input_fields; i++) {
11822b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->input_fields[i]).field_name));
11832b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->input_fields[i]));
1184fe2413ffSjeremylt   }
118592ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_output_fields; i++) {
11862b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->output_fields[i]).field_name));
11872b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->output_fields[i]));
1188fe2413ffSjeremylt   }
11892b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->input_fields));
11902b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->output_fields));
1191777ff853SJeremy L Thompson 
1192777ff853SJeremy L Thompson   // User context data object
11932b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&(*qf)->ctx));
1194fe2413ffSjeremylt 
11952b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->user_source));
11962b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->source_path));
11972b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->gallery_name));
11982b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->kernel_name));
11996c328a79SJeremy L Thompson   CeedCall(CeedObjectDestroy_Private(&(*qf)->obj));
12002b730f8bSJeremy L Thompson   CeedCall(CeedFree(qf));
1201e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1202d7b241e6Sjeremylt }
1203d7b241e6Sjeremylt 
1204d7b241e6Sjeremylt /// @}
1205