xref: /libCEED/interface/ceed-qfunction.c (revision a299a25b9879b9b54a5c96c9f88ed0cb403a2fde)
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 /**
161777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
162777ff853SJeremy L Thompson 
163ea61e9acSJeremy L Thompson   @param[in,out] qf     CeedQFunction
164ea61e9acSJeremy L Thompson   @param[in]     status Boolean value to set as Fortran status
165777ff853SJeremy L Thompson 
166777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
167777ff853SJeremy L Thompson 
168777ff853SJeremy L Thompson   @ref Backend
169777ff853SJeremy L Thompson **/
170777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
171f04ea552SJeremy L Thompson   qf->is_fortran = status;
172e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
173777ff853SJeremy L Thompson }
174777ff853SJeremy L Thompson 
1757a982d89SJeremy L. Thompson /// @}
1767a982d89SJeremy L. Thompson 
1777a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1787a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1797a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1807a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1817a982d89SJeremy L. Thompson /// @{
1827a982d89SJeremy L. Thompson 
1837a982d89SJeremy L. Thompson /**
184ca94c3ddSJeremy L Thompson   @brief Get the vector length of a `CeedQFunction`
1857a982d89SJeremy L. Thompson 
186ca94c3ddSJeremy L Thompson   @param[in]  qf         `CeedQFunction`
187d1d35e2fSjeremylt   @param[out] vec_length Variable to store vector length
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 **/
193d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
194d1d35e2fSjeremylt   *vec_length = qf->vec_length;
195e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1967a982d89SJeremy L. Thompson }
1977a982d89SJeremy L. Thompson 
1987a982d89SJeremy L. Thompson /**
199ca94c3ddSJeremy L Thompson   @brief Get the number of inputs and outputs to a `CeedQFunction`
2007a982d89SJeremy L. Thompson 
201ca94c3ddSJeremy L Thompson   @param[in]  qf         `CeedQFunction`
202d1d35e2fSjeremylt   @param[out] num_input  Variable to store number of input fields
203d1d35e2fSjeremylt   @param[out] num_output Variable to store number of output fields
2047a982d89SJeremy L. Thompson 
2057a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2067a982d89SJeremy L. Thompson 
2077a982d89SJeremy L. Thompson   @ref Backend
2087a982d89SJeremy L. Thompson **/
2092b730f8bSJeremy L Thompson int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, CeedInt *num_output) {
210d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
211d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
212e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2137a982d89SJeremy L. Thompson }
2147a982d89SJeremy L. Thompson 
2157a982d89SJeremy L. Thompson /**
216d3d5610dSJeremy L Thompson   @brief Get the name of the `CeedQFunction`.
217d3d5610dSJeremy L Thompson     Use the `name` if created via @ref `CeedQFunctionCreateInteriorByName`, otherwise return the kernel name via @ref `CeedQFunctionGetKernelName`.
218d3d5610dSJeremy L Thompson 
219d3d5610dSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
220d3d5610dSJeremy L Thompson   @param[out] kernel_name Variable to store `CeedQFunction` name
221d3d5610dSJeremy L Thompson 
222d3d5610dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
223d3d5610dSJeremy L Thompson 
224d3d5610dSJeremy L Thompson   @ref Backend
225d3d5610dSJeremy L Thompson **/
226d3d5610dSJeremy L Thompson int CeedQFunctionGetName(CeedQFunction qf, const char **name) {
227d3d5610dSJeremy L Thompson   if (qf->is_gallery) {
228d3d5610dSJeremy L Thompson     *name = qf->gallery_name;
229d3d5610dSJeremy L Thompson   } else {
230d3d5610dSJeremy L Thompson     CeedCall(CeedQFunctionGetKernelName(qf, name));
231d3d5610dSJeremy L Thompson   }
232d3d5610dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
233d3d5610dSJeremy L Thompson }
234d3d5610dSJeremy L Thompson 
235d3d5610dSJeremy L Thompson /**
236ca94c3ddSJeremy L Thompson   @brief Get the name of the user function for a `CeedQFunction`
2377a982d89SJeremy L. Thompson 
238ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
239d3d5610dSJeremy L Thompson   @param[out] kernel_name Variable to store string holding kernel name
2407a982d89SJeremy L. Thompson 
2417a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2427a982d89SJeremy L. Thompson 
2437a982d89SJeremy L. Thompson   @ref Backend
2447a982d89SJeremy L. Thompson **/
2457d023984SJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, const char **kernel_name) {
246ca5eadf8SJeremy L Thompson   if (!qf->kernel_name) {
247ca5eadf8SJeremy L Thompson     char *kernel_name_copy;
248ca5eadf8SJeremy L Thompson 
249ca5eadf8SJeremy L Thompson     if (qf->user_source) {
250ca5eadf8SJeremy L Thompson       const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
251ca5eadf8SJeremy L Thompson       size_t      kernel_name_len = strlen(kernel_name);
252ca5eadf8SJeremy L Thompson 
2532b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(kernel_name_len + 1, &kernel_name_copy));
254ca5eadf8SJeremy L Thompson       memcpy(kernel_name_copy, kernel_name, kernel_name_len);
255ca5eadf8SJeremy L Thompson     } else {
2562b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &kernel_name_copy));
257ca5eadf8SJeremy L Thompson     }
258ca5eadf8SJeremy L Thompson     qf->kernel_name = kernel_name_copy;
259ca5eadf8SJeremy L Thompson   }
260ca5eadf8SJeremy L Thompson 
2617d023984SJeremy L Thompson   *kernel_name = qf->kernel_name;
26243e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
26343e1b16fSJeremy L Thompson }
26443e1b16fSJeremy L Thompson 
26543e1b16fSJeremy L Thompson /**
266ca94c3ddSJeremy L Thompson   @brief Get the source path string for a `CeedQFunction`
26743e1b16fSJeremy L Thompson 
268ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
26943e1b16fSJeremy L Thompson   @param[out] source_path Variable to store source path string
27043e1b16fSJeremy L Thompson 
27143e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
27243e1b16fSJeremy L Thompson 
27343e1b16fSJeremy L Thompson   @ref Backend
27443e1b16fSJeremy L Thompson **/
27534ffed21SJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, const char **source_path) {
276ca5eadf8SJeremy L Thompson   if (!qf->source_path && qf->user_source) {
277ca5eadf8SJeremy L Thompson     Ceed        ceed;
278ca5eadf8SJeremy L Thompson     bool        is_absolute_path;
27922070f95SJeremy L Thompson     char       *source_path_copy;
28022070f95SJeremy L Thompson     const char *absolute_path;
281ca5eadf8SJeremy L Thompson     const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
282ca5eadf8SJeremy L Thompson     size_t      kernel_name_len = strlen(kernel_name);
283ca5eadf8SJeremy L Thompson 
2842b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
2852b730f8bSJeremy L Thompson     CeedCall(CeedCheckFilePath(ceed, qf->user_source, &is_absolute_path));
286ca5eadf8SJeremy L Thompson     if (is_absolute_path) {
287ca5eadf8SJeremy L Thompson       absolute_path = (char *)qf->user_source;
288ca5eadf8SJeremy L Thompson     } else {
2892b730f8bSJeremy L Thompson       CeedCall(CeedGetJitAbsolutePath(ceed, qf->user_source, &absolute_path));
290ca5eadf8SJeremy L Thompson     }
2919bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&ceed));
292ca5eadf8SJeremy L Thompson 
293ca5eadf8SJeremy L Thompson     size_t source_len = strlen(absolute_path) - kernel_name_len - 1;
2941c66c397SJeremy L Thompson 
2952b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(source_len + 1, &source_path_copy));
296ca5eadf8SJeremy L Thompson     memcpy(source_path_copy, absolute_path, source_len);
297ca5eadf8SJeremy L Thompson     qf->source_path = source_path_copy;
298ca5eadf8SJeremy L Thompson 
2992b730f8bSJeremy L Thompson     if (!is_absolute_path) CeedCall(CeedFree(&absolute_path));
300ca5eadf8SJeremy L Thompson   }
301ca5eadf8SJeremy L Thompson 
30243e1b16fSJeremy L Thompson   *source_path = (char *)qf->source_path;
303e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3047a982d89SJeremy L. Thompson }
3057a982d89SJeremy L. Thompson 
3067a982d89SJeremy L. Thompson /**
307ca94c3ddSJeremy L Thompson   @brief Initialize and load `CeedQFunction` source file into string buffer, including full text of local files in place of `#include "local.h"`.
3084385fb7fSSebastian Grimberg 
309ca94c3ddSJeremy L Thompson   The `buffer` is set to `NULL` if there is no `CeedQFunction` source file.
3104385fb7fSSebastian Grimberg 
311f8d308faSJed Brown   Note: This function may as well return a mutable buffer, but all current uses
312f8d308faSJed Brown   do not modify it. (This is just a downside of `const` semantics with output
313f8d308faSJed Brown   arguments instead of returns.)
314f8d308faSJed Brown 
315ca94c3ddSJeremy L Thompson   Note: Caller is responsible for freeing the string buffer with @ref CeedFree().
3163d3250a0SJeremy L Thompson 
317ca94c3ddSJeremy L Thompson   @param[in]  qf            `CeedQFunction`
318f74ec584SJeremy L Thompson   @param[out] source_buffer String buffer for source file contents
3193d3250a0SJeremy L Thompson 
3203d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3213d3250a0SJeremy L Thompson 
3223d3250a0SJeremy L Thompson   @ref Backend
3233d3250a0SJeremy L Thompson **/
324f8d308faSJed Brown int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, const char **source_buffer) {
32534ffed21SJeremy L Thompson   const char *source_path;
3263d3250a0SJeremy L Thompson 
3272b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetSourcePath(qf, &source_path));
3283d3250a0SJeremy L Thompson   *source_buffer = NULL;
3293d3250a0SJeremy L Thompson   if (source_path) {
3301203703bSJeremy L Thompson     Ceed  ceed;
331f8d308faSJed Brown     char *buffer = NULL;
3321203703bSJeremy L Thompson 
3331203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
334f8d308faSJed Brown     CeedCall(CeedLoadSourceToBuffer(ceed, source_path, &buffer));
3359bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&ceed));
336f8d308faSJed Brown     *source_buffer = buffer;
3373d3250a0SJeremy L Thompson   }
3383d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3393d3250a0SJeremy L Thompson }
3403d3250a0SJeremy L Thompson 
3413d3250a0SJeremy L Thompson /**
342ca94c3ddSJeremy L Thompson   @brief Get the User Function for a `CeedQFunction`
3437a982d89SJeremy L. Thompson 
344ca94c3ddSJeremy L Thompson   @param[in]  qf `CeedQFunction`
3457a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
3467a982d89SJeremy L. Thompson 
3477a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3487a982d89SJeremy L. Thompson 
3497a982d89SJeremy L. Thompson   @ref Backend
3507a982d89SJeremy L. Thompson **/
3517a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
3527a982d89SJeremy L. Thompson   *f = qf->function;
353e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3547a982d89SJeremy L. Thompson }
3557a982d89SJeremy L. Thompson 
3567a982d89SJeremy L. Thompson /**
357ca94c3ddSJeremy L Thompson   @brief Get global context for a `CeedQFunction`.
3584385fb7fSSebastian Grimberg 
359ca94c3ddSJeremy L Thompson   Note: For `CeedQFunction` from the Fortran interface, this function will return the Fortran context `CeedQFunctionContext`.
3607a982d89SJeremy L. Thompson 
361ea61e9acSJeremy L Thompson   @param[in]  qf  CeedQFunction
362777ff853SJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
3637a982d89SJeremy L. Thompson 
3647a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3657a982d89SJeremy L. Thompson 
3667a982d89SJeremy L. Thompson   @ref Backend
3677a982d89SJeremy L. Thompson **/
368777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
3691485364cSJeremy L Thompson   *ctx = NULL;
3701485364cSJeremy L Thompson   if (qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(qf->ctx, ctx));
371e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3727a982d89SJeremy L. Thompson }
3737a982d89SJeremy L. Thompson 
3747a982d89SJeremy L. Thompson /**
375ca94c3ddSJeremy L Thompson   @brief Get context data of a `CeedQFunction`
376441428dfSJeremy L Thompson 
377ca94c3ddSJeremy L Thompson   @param[in]  qf       `CeedQFunction`
378ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
379ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
380441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
381441428dfSJeremy L Thompson 
382441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
383441428dfSJeremy L Thompson 
384441428dfSJeremy L Thompson   @ref Backend
385441428dfSJeremy L Thompson **/
3862b730f8bSJeremy L Thompson int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
387441428dfSJeremy L Thompson   bool                 is_writable;
388441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
389441428dfSJeremy L Thompson 
3902b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
391441428dfSJeremy L Thompson   if (ctx) {
3922b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
393441428dfSJeremy L Thompson     if (is_writable) {
3942b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
395441428dfSJeremy L Thompson     } else {
3962b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
397441428dfSJeremy L Thompson     }
398441428dfSJeremy L Thompson   } else {
399441428dfSJeremy L Thompson     *(void **)data = NULL;
400441428dfSJeremy L Thompson   }
4011485364cSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx));
402441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
403441428dfSJeremy L Thompson }
404441428dfSJeremy L Thompson 
405441428dfSJeremy L Thompson /**
406ca94c3ddSJeremy L Thompson   @brief Restore context data of a `CeedQFunction`
407441428dfSJeremy L Thompson 
408ca94c3ddSJeremy L Thompson   @param[in]     qf   `CeedQFunction`
409ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
410441428dfSJeremy L Thompson 
411441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
412441428dfSJeremy L Thompson 
413441428dfSJeremy L Thompson   @ref Backend
414441428dfSJeremy L Thompson **/
415441428dfSJeremy L Thompson int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) {
416441428dfSJeremy L Thompson   bool                 is_writable;
417441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
418441428dfSJeremy L Thompson 
4192b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
420441428dfSJeremy L Thompson   if (ctx) {
4212b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
422441428dfSJeremy L Thompson     if (is_writable) {
4232b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
424441428dfSJeremy L Thompson     } else {
4252b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
426441428dfSJeremy L Thompson     }
427441428dfSJeremy L Thompson   }
4281485364cSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx));
429441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
430441428dfSJeremy L Thompson }
431441428dfSJeremy L Thompson 
432441428dfSJeremy L Thompson /**
433ca94c3ddSJeremy L Thompson   @brief Get true user context for a `CeedQFunction`
4344385fb7fSSebastian Grimberg 
435ca94c3ddSJeremy L Thompson   Note: For all `CeedQFunction` this function will return the user `CeedQFunctionContext` and not interface context `CeedQFunctionContext`, if any such object exists.
4367a982d89SJeremy L. Thompson 
437ca94c3ddSJeremy L Thompson   @param[in]  qf  `CeedQFunction`
438ca94c3ddSJeremy L Thompson   @param[out] ctx Variable to store `CeedQFunctionContext`
4397a982d89SJeremy L. Thompson 
4407a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4417a982d89SJeremy L. Thompson   @ref Backend
4427a982d89SJeremy L. Thompson **/
443777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
4441203703bSJeremy L Thompson   CeedQFunctionContext qf_ctx;
4451203703bSJeremy L Thompson 
4461203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &qf_ctx));
447f04ea552SJeremy L Thompson   if (qf->is_fortran) {
448d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
4491c66c397SJeremy L Thompson 
4501203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetData(qf_ctx, CEED_MEM_HOST, &fortran_ctx));
4518cb0412aSJeremy L Thompson     *ctx = fortran_ctx->inner_ctx;
4529a25c351SJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreData(qf_ctx, &fortran_ctx));
4537a982d89SJeremy L. Thompson   } else {
4541203703bSJeremy L Thompson     *ctx = qf_ctx;
4557a982d89SJeremy L. Thompson   }
4561485364cSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&qf_ctx));
457e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4587a982d89SJeremy L. Thompson }
4597a982d89SJeremy L. Thompson 
4607a982d89SJeremy L. Thompson /**
461ca94c3ddSJeremy L Thompson   @brief Get inner context data of a `CeedQFunction`
462441428dfSJeremy L Thompson 
463ca94c3ddSJeremy L Thompson   @param[in]  qf       `CeedQFunction`
464ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
465ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
466441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
467441428dfSJeremy L Thompson 
468441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
469441428dfSJeremy L Thompson 
470441428dfSJeremy L Thompson   @ref Backend
471441428dfSJeremy L Thompson **/
4722b730f8bSJeremy L Thompson int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
473441428dfSJeremy L Thompson   bool                 is_writable;
474441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
475441428dfSJeremy L Thompson 
4762b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
477441428dfSJeremy L Thompson   if (ctx) {
4782b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
479441428dfSJeremy L Thompson     if (is_writable) {
4802b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
481441428dfSJeremy L Thompson     } else {
4822b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
483441428dfSJeremy L Thompson     }
484441428dfSJeremy L Thompson   } else {
485441428dfSJeremy L Thompson     *(void **)data = NULL;
486441428dfSJeremy L Thompson   }
487441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
488441428dfSJeremy L Thompson }
489441428dfSJeremy L Thompson 
490441428dfSJeremy L Thompson /**
491ca94c3ddSJeremy L Thompson   @brief Restore inner context data of a `CeedQFunction`
492441428dfSJeremy L Thompson 
493ca94c3ddSJeremy L Thompson   @param[in]     qf   `CeedQFunction`
494ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
495441428dfSJeremy L Thompson 
496441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
497441428dfSJeremy L Thompson 
498441428dfSJeremy L Thompson   @ref Backend
499441428dfSJeremy L Thompson **/
500441428dfSJeremy L Thompson int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) {
501441428dfSJeremy L Thompson   bool                 is_writable;
502441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
503441428dfSJeremy L Thompson 
5042b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
505441428dfSJeremy L Thompson   if (ctx) {
5062b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
507441428dfSJeremy L Thompson     if (is_writable) {
5082b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
509441428dfSJeremy L Thompson     } else {
5102b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
511441428dfSJeremy L Thompson     }
512441428dfSJeremy L Thompson   }
5135f249b39SJeremy L Thompson   *(void **)data = NULL;
514441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
515441428dfSJeremy L Thompson }
516441428dfSJeremy L Thompson 
517441428dfSJeremy L Thompson /**
518ca94c3ddSJeremy L Thompson   @brief Determine if `CeedQFunction` is identity
5197a982d89SJeremy L. Thompson 
520ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
521d1d35e2fSjeremylt   @param[out] is_identity Variable to store identity status
5227a982d89SJeremy L. Thompson 
5237a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5247a982d89SJeremy L. Thompson 
5257a982d89SJeremy L. Thompson   @ref Backend
5267a982d89SJeremy L. Thompson **/
527d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
528f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
529e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5307a982d89SJeremy L. Thompson }
5317a982d89SJeremy L. Thompson 
5327a982d89SJeremy L. Thompson /**
533ca94c3ddSJeremy L Thompson   @brief Determine if `CeedQFunctionContext` is writable
534441428dfSJeremy L Thompson 
535ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
536ea61e9acSJeremy L Thompson   @param[out] is_writable Variable to store context writeable status
537441428dfSJeremy L Thompson 
538441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
539441428dfSJeremy L Thompson 
540441428dfSJeremy L Thompson   @ref Backend
541441428dfSJeremy L Thompson **/
542441428dfSJeremy L Thompson int CeedQFunctionIsContextWritable(CeedQFunction qf, bool *is_writable) {
543441428dfSJeremy L Thompson   *is_writable = qf->is_context_writable;
544441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
545441428dfSJeremy L Thompson }
546441428dfSJeremy L Thompson 
547441428dfSJeremy L Thompson /**
548ca94c3ddSJeremy L Thompson   @brief Get backend data of a `CeedQFunction`
5497a982d89SJeremy L. Thompson 
550ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction`
5517a982d89SJeremy L. Thompson   @param[out] data Variable to store data
5527a982d89SJeremy L. Thompson 
5537a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5547a982d89SJeremy L. Thompson 
5557a982d89SJeremy L. Thompson   @ref Backend
5567a982d89SJeremy L. Thompson **/
557777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
558777ff853SJeremy L Thompson   *(void **)data = qf->data;
559e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5607a982d89SJeremy L. Thompson }
5617a982d89SJeremy L. Thompson 
5627a982d89SJeremy L. Thompson /**
563ca94c3ddSJeremy L Thompson   @brief Set backend data of a `CeedQFunction`
5647a982d89SJeremy L. Thompson 
565ca94c3ddSJeremy L Thompson   @param[in,out] qf   `CeedQFunction`
566ea61e9acSJeremy L Thompson   @param[in]     data Data to set
5677a982d89SJeremy L. Thompson 
5687a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5697a982d89SJeremy L. Thompson 
5707a982d89SJeremy L. Thompson   @ref Backend
5717a982d89SJeremy L. Thompson **/
572777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
573777ff853SJeremy L Thompson   qf->data = data;
574e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5757a982d89SJeremy L. Thompson }
5767a982d89SJeremy L. Thompson 
5777a982d89SJeremy L. Thompson /**
5781203703bSJeremy L Thompson   @brief Get a boolean value indicating if the `CeedQFunction` is immutable
5791203703bSJeremy L Thompson 
5801203703bSJeremy L Thompson   @param[in]  qf           `CeedOperator`
5811203703bSJeremy L Thompson   @param[out] is_immutable Variable to store immutability status
5821203703bSJeremy L Thompson 
5831203703bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5841203703bSJeremy L Thompson 
5851203703bSJeremy L Thompson   @ref Backend
5861203703bSJeremy L Thompson **/
5871203703bSJeremy L Thompson int CeedQFunctionIsImmutable(CeedQFunction qf, bool *is_immutable) {
5881203703bSJeremy L Thompson   *is_immutable = qf->is_immutable;
5891203703bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
5901203703bSJeremy L Thompson }
5911203703bSJeremy L Thompson 
5921203703bSJeremy L Thompson /**
5931203703bSJeremy L Thompson   @brief Set the immutable flag of a `CeedQFunction` to `true`
5941203703bSJeremy L Thompson 
5951203703bSJeremy L Thompson   @param[in,out] qf `CeedQFunction`
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 CeedQFunctionSetImmutable(CeedQFunction qf) {
6021203703bSJeremy L Thompson   qf->is_immutable = true;
6031203703bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
6041203703bSJeremy L Thompson }
6051203703bSJeremy L Thompson 
6061203703bSJeremy L Thompson /**
607ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedQFunction`
60834359f16Sjeremylt 
609ca94c3ddSJeremy L Thompson   @param[in,out] qf `CeedQFunction` to increment the reference counter
61034359f16Sjeremylt 
61134359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
61234359f16Sjeremylt 
61334359f16Sjeremylt   @ref Backend
61434359f16Sjeremylt **/
6159560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
616b0f67a9cSJeremy L Thompson   CeedCall(CeedObjectReference((CeedObject)qf));
61734359f16Sjeremylt   return CEED_ERROR_SUCCESS;
61834359f16Sjeremylt }
61934359f16Sjeremylt 
6206e15d496SJeremy L Thompson /**
621ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs per quadrature required to apply `CeedQFunction`
6226e15d496SJeremy L Thompson 
623ca94c3ddSJeremy L Thompson   @param[in]  qf    `CeedQFunction` to estimate FLOPs for
624ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
6256e15d496SJeremy L Thompson 
6266e15d496SJeremy L Thompson   @ref Backend
6276e15d496SJeremy L Thompson **/
6289d36ca50SJeremy L Thompson int CeedQFunctionGetFlopsEstimate(CeedQFunction qf, CeedSize *flops) {
6296e15d496SJeremy L Thompson   *flops = qf->user_flop_estimate;
6306e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6316e15d496SJeremy L Thompson }
6326e15d496SJeremy L Thompson 
6337a982d89SJeremy L. Thompson /// @}
6347a982d89SJeremy L. Thompson 
6357a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6367a982d89SJeremy L. Thompson /// CeedQFunction Public API
6377a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6387a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
6397a982d89SJeremy L. Thompson /// @{
6407a982d89SJeremy L. Thompson 
6417a982d89SJeremy L. Thompson /**
642ca94c3ddSJeremy L Thompson   @brief Create a `CeedQFunction` for evaluating interior (volumetric) terms
6437a982d89SJeremy L. Thompson 
644ca94c3ddSJeremy L Thompson   @param[in]  ceed       `Ceed` object used to create the `CeedQFunction`
645ca94c3ddSJeremy L Thompson   @param[in]  vec_length Vector length.
646ca94c3ddSJeremy L Thompson                            Caller must ensure that number of quadrature points is a multiple of `vec_length`.
647ea61e9acSJeremy L Thompson   @param[in]  f          Function pointer to evaluate action at quadrature points.
648ca94c3ddSJeremy L Thompson                            See `CeedQFunctionUser`.
649ca94c3ddSJeremy L Thompson   @param[in]  source     Absolute path to source of `CeedQFunctionUser`, "\abs_path\file.h:function_name".
650ca94c3ddSJeremy 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.).
6514ada38baSJames Wright                            The entire contents of this file and all locally included files are used during JiT compilation for GPU backends.
652c0b5abf0SJeremy L Thompson                            The header `ceed/types.h` is preferred over `ceed.h` or `ceed/ceed.h` for `CeedQFunction` source files.
653c0b5abf0SJeremy 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`.
6544ada38baSJames Wright                            All source files must be at the provided filepath at runtime for JiT to function.
655ca94c3ddSJeremy L Thompson   @param[out] qf         Address of the variable where the newly created `CeedQFunction` will be stored
6567a982d89SJeremy L. Thompson 
6577a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6587a982d89SJeremy L. Thompson 
659ca94c3ddSJeremy L Thompson   See \ref CeedQFunctionUser for details on the call-back function `f` arguments.
6607a982d89SJeremy L. Thompson 
6617a982d89SJeremy L. Thompson   @ref User
6627a982d89SJeremy L. Thompson **/
6632b730f8bSJeremy L Thompson int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length, CeedQFunctionUser f, const char *source, CeedQFunction *qf) {
664ca5eadf8SJeremy L Thompson   char *user_source_copy;
6657a982d89SJeremy L. Thompson 
6667a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
6677a982d89SJeremy L. Thompson     Ceed delegate;
6686574a04fSJeremy L Thompson 
6692b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "QFunction"));
6701ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedQFunctionCreateInterior");
6712b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf));
6729bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
673e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
6747a982d89SJeremy L. Thompson   }
6757a982d89SJeremy L. Thompson 
6766574a04fSJeremy L Thompson   CeedCheck(!strlen(source) || strrchr(source, ':'), ceed, CEED_ERROR_INCOMPLETE,
6776574a04fSJeremy L Thompson             "Provided path to source does not include function name. Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"", source);
67843e1b16fSJeremy L Thompson 
6792b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, qf));
680b0f67a9cSJeremy L Thompson   CeedCall(CeedObjectCreate(ceed, CeedQFunctionView_Object, &(*qf)->obj));
681d1d35e2fSjeremylt   (*qf)->vec_length          = vec_length;
682f04ea552SJeremy L Thompson   (*qf)->is_identity         = false;
683441428dfSJeremy L Thompson   (*qf)->is_context_writable = true;
6847a982d89SJeremy L. Thompson   (*qf)->function            = f;
6856e15d496SJeremy L Thompson   (*qf)->user_flop_estimate  = -1;
68643e1b16fSJeremy L Thompson   if (strlen(source)) {
687ca5eadf8SJeremy L Thompson     size_t user_source_len = strlen(source);
688ee5a26f2SJeremy L Thompson 
6892b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(user_source_len + 1, &user_source_copy));
690ca5eadf8SJeremy L Thompson     memcpy(user_source_copy, source, user_source_len);
691ca5eadf8SJeremy L Thompson     (*qf)->user_source = user_source_copy;
69243e1b16fSJeremy L Thompson   }
6932b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields));
6942b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields));
6952b730f8bSJeremy L Thompson   CeedCall(ceed->QFunctionCreate(*qf));
696e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6977a982d89SJeremy L. Thompson }
6987a982d89SJeremy L. Thompson 
6997a982d89SJeremy L. Thompson /**
700ca94c3ddSJeremy L Thompson   @brief Create a `CeedQFunction` for evaluating interior (volumetric) terms by name
701288c0443SJeremy L Thompson 
702ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedQFunction`
703ca94c3ddSJeremy L Thompson   @param[in]  name Name of `CeedQFunction` to use from gallery
704ca94c3ddSJeremy L Thompson   @param[out] qf   Address of the variable where the newly created `CeedQFunction` will be stored
705288c0443SJeremy L Thompson 
706288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
707288c0443SJeremy L Thompson 
7087a982d89SJeremy L. Thompson   @ref User
709288c0443SJeremy L Thompson **/
7102b730f8bSJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, CeedQFunction *qf) {
711f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
712288c0443SJeremy L Thompson 
7132b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionRegisterAll());
714288c0443SJeremy L Thompson   // Find matching backend
715ca94c3ddSJeremy L Thompson   CeedCheck(name, ceed, CEED_ERROR_INCOMPLETE, "No CeedQFunction name provided");
716288c0443SJeremy L Thompson   for (size_t i = 0; i < num_qfunctions; i++) {
717288c0443SJeremy L Thompson     size_t      n;
718d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
7192b730f8bSJeremy L Thompson     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {
7202b730f8bSJeremy L Thompson     }
721d1d35e2fSjeremylt     if (n > match_len) {
722d1d35e2fSjeremylt       match_len   = n;
723f7e22acaSJeremy L Thompson       match_index = i;
724288c0443SJeremy L Thompson     }
725288c0443SJeremy L Thompson   }
726ca94c3ddSJeremy L Thompson   CeedCheck(match_len > 0, ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery CeedQFunction");
727288c0443SJeremy L Thompson 
728288c0443SJeremy L Thompson   // Create QFunction
7292b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInterior(ceed, gallery_qfunctions[match_index].vec_length, gallery_qfunctions[match_index].f,
7302b730f8bSJeremy L Thompson                                        gallery_qfunctions[match_index].source, qf));
731288c0443SJeremy L Thompson 
732288c0443SJeremy L Thompson   // QFunction specific setup
7332b730f8bSJeremy L Thompson   CeedCall(gallery_qfunctions[match_index].init(ceed, name, *qf));
734288c0443SJeremy L Thompson 
73575affc3bSjeremylt   // Copy name
7362b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name));
73743e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
738e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
739288c0443SJeremy L Thompson }
740288c0443SJeremy L Thompson 
741288c0443SJeremy L Thompson /**
742ca94c3ddSJeremy L Thompson   @brief Create an identity `CeedQFunction`.
7434385fb7fSSebastian Grimberg 
744ea61e9acSJeremy L Thompson   Inputs are written into outputs in the order given.
745ca94c3ddSJeremy 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.
746ca94c3ddSJeremy 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.
7470219ea01SJeremy L Thompson 
748ca94c3ddSJeremy L Thompson   @param[in]  ceed     `Ceed` object used to create the `CeedQFunction`
749ca94c3ddSJeremy L Thompson   @param[in]  size     Size of the `CeedQFunction` fields
750ca94c3ddSJeremy L Thompson   @param[in]  in_mode  @ref CeedEvalMode for input to `CeedQFunction`
751ca94c3ddSJeremy L Thompson   @param[in]  out_mode @ref CeedEvalMode for output to `CeedQFunction`
752ca94c3ddSJeremy L Thompson   @param[out] qf       Address of the variable where the newly created `CeedQFunction` will be stored
7530219ea01SJeremy L Thompson 
7540219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
7550219ea01SJeremy L Thompson 
7567a982d89SJeremy L. Thompson   @ref User
7570219ea01SJeremy L Thompson **/
7582b730f8bSJeremy L Thompson int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, CeedEvalMode out_mode, CeedQFunction *qf) {
7591c66c397SJeremy L Thompson   CeedQFunctionContext  ctx;
7601c66c397SJeremy L Thompson   CeedContextFieldLabel size_label;
7611c66c397SJeremy L Thompson 
7622b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Identity", qf));
7632b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(*qf, "input", size, in_mode));
7642b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(*qf, "output", size, out_mode));
7650219ea01SJeremy L Thompson 
766f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
767547dbd6fSJeremy L Thompson 
7682b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(*qf, &ctx));
7692b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label));
7702b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetInt32(ctx, size_label, &size));
7711485364cSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx));
772e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7730219ea01SJeremy L Thompson }
7740219ea01SJeremy L Thompson 
7750219ea01SJeremy L Thompson /**
776ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedQFunction`.
7774385fb7fSSebastian Grimberg 
778ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedQFunctionDestroy().
779512bb800SJeremy L Thompson 
780ca94c3ddSJeremy 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`.
781ca94c3ddSJeremy L Thompson         This `CeedQFunction` will be destroyed if `*qf_copy` is the only reference to this `CeedQFunction`.
7829560d06aSjeremylt 
783ca94c3ddSJeremy L Thompson   @param[in]  qf      `CeedQFunction` to copy reference to
7849560d06aSjeremylt   @param[out] qf_copy Variable to store copied reference
7859560d06aSjeremylt 
7869560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7879560d06aSjeremylt 
7889560d06aSjeremylt   @ref User
7899560d06aSjeremylt **/
7909560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
7912b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionReference(qf));
7922b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(qf_copy));
7939560d06aSjeremylt   *qf_copy = qf;
7949560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7959560d06aSjeremylt }
7969560d06aSjeremylt 
7979560d06aSjeremylt /**
798ca94c3ddSJeremy L Thompson   @brief Add a `CeedQFunction` input
799b11c1e72Sjeremylt 
800ca94c3ddSJeremy L Thompson   @param[in,out] qf         `CeedQFunction`
801ca94c3ddSJeremy L Thompson   @param[in]     field_name Name of `CeedQFunction` field
802d538d163SJeremy L Thompson   @param[in]     size       Size of `CeedQFunction` field,
803d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_NONE,
804d538d163SJeremy 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,
805d538d163SJeremy L Thompson                               (`num_comp * dim`) for @ref CEED_EVAL_GRAD,
806d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_DIV, and
807d538d163SJeremy L Thompson                               (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` otherwise `curl_dim = dim` for @ref CEED_EVAL_CURL.
808ca94c3ddSJeremy L Thompson   @param[in]     eval_mode  @ref CEED_EVAL_NONE to use values directly,
809ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_INTERP to use interpolated values,
810ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_GRAD to use gradients,
811ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_DIV to use divergence,
812ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_CURL to use curl
813b11c1e72Sjeremylt 
814d538d163SJeremy L Thompson   Note: In the user `CeedQFunctionUser`, the `in` argument list the fields in the order given by the calls to `CeedQFunctionAddInput`.
815d538d163SJeremy L Thompson 
816b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
817dfdf5a53Sjeremylt 
8187a982d89SJeremy L. Thompson   @ref User
819b11c1e72Sjeremylt **/
8202b730f8bSJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
8211203703bSJeremy L Thompson   bool is_immutable;
8221203703bSJeremy L Thompson 
8231203703bSJeremy L Thompson   CeedCall(CeedQFunctionIsImmutable(qf, &is_immutable));
8249bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedQFunctionReturnCeed(qf), CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
8259bc66399SJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT || size == 1, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION, "CEED_EVAL_WEIGHT should have size 1");
826643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
8279bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8283f08121cSJeremy L Thompson               "CeedQFunction field names must be unique. Duplicate name: %s", field_name);
829643fbb69SJeremy L Thompson   }
830643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
8319bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8323f08121cSJeremy L Thompson               "CeedQFunction field names must be unique. Duplicate name: %s", field_name);
833643fbb69SJeremy L Thompson   }
8342b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], field_name, size, eval_mode));
835d1d35e2fSjeremylt   qf->num_input_fields++;
836e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
837d7b241e6Sjeremylt }
838d7b241e6Sjeremylt 
839b11c1e72Sjeremylt /**
840ca94c3ddSJeremy L Thompson   @brief Add a `CeedQFunction` output
841b11c1e72Sjeremylt 
842ca94c3ddSJeremy L Thompson   @param[in,out] qf         `CeedQFunction`
843ca94c3ddSJeremy L Thompson   @param[in]     field_name Name of `CeedQFunction` field
844d538d163SJeremy L Thompson   @param[in]     size       Size of `CeedQFunction` field,
845d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_NONE,
846d538d163SJeremy 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,
847d538d163SJeremy L Thompson                               (`num_comp * dim`) for @ref CEED_EVAL_GRAD,
848d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_DIV, and
849d538d163SJeremy L Thompson                               (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` otherwise `curl_dim = dim` for @ref CEED_EVAL_CURL.
850ca94c3ddSJeremy L Thompson   @param[in]     eval_mode  @ref CEED_EVAL_NONE to use values directly,
851ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_INTERP to use interpolated values,
852ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_GRAD to use gradients,
853ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_DIV to use divergence,
854ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_CURL to use curl.
855b11c1e72Sjeremylt 
856d538d163SJeremy L Thompson   Note: In the user `CeedQFunctionUser`, the `out` argument list the fields in the order given by the calls to `CeedQFunctionAddOutput`.
857d538d163SJeremy L Thompson 
858b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
859dfdf5a53Sjeremylt 
8607a982d89SJeremy L. Thompson   @ref User
861b11c1e72Sjeremylt **/
8622b730f8bSJeremy L Thompson int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
8631203703bSJeremy L Thompson   bool is_immutable;
8641203703bSJeremy L Thompson 
8651203703bSJeremy L Thompson   CeedCall(CeedQFunctionIsImmutable(qf, &is_immutable));
8669bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedQFunctionReturnCeed(qf), CEED_ERROR_MAJOR, "CeedQFunction cannot be changed after set as immutable");
8679bc66399SJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION,
8689bc66399SJeremy L Thompson             "Cannot create CeedQFunction output with CEED_EVAL_WEIGHT");
869643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
8709bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8719bc66399SJeremy L Thompson               "CeedQFunction field names must be unique");
872643fbb69SJeremy L Thompson   }
873643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
8749bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8759bc66399SJeremy L Thompson               "CeedQFunction field names must be unique");
876643fbb69SJeremy L Thompson   }
8772b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], field_name, size, eval_mode));
878d1d35e2fSjeremylt   qf->num_output_fields++;
879e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
880d7b241e6Sjeremylt }
881d7b241e6Sjeremylt 
882dfdf5a53Sjeremylt /**
883ca94c3ddSJeremy L Thompson   @brief Get the `CeedQFunctionField` of a `CeedQFunction`
88443bbe138SJeremy L Thompson 
885ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedQFunction` as immutable.
886f04ea552SJeremy L Thompson 
887ca94c3ddSJeremy L Thompson   @param[in]  qf                `CeedQFunction`
888f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
889f74ec584SJeremy L Thompson   @param[out] input_fields      Variable to store input fields
890f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
891f74ec584SJeremy L Thompson   @param[out] output_fields     Variable to store output fields
89243bbe138SJeremy L Thompson 
89343bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
89443bbe138SJeremy L Thompson 
895e9b533fbSJeremy L Thompson   @ref Advanced
89643bbe138SJeremy L Thompson **/
8972b730f8bSJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields, CeedQFunctionField **input_fields, CeedInt *num_output_fields,
89843bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
8991203703bSJeremy L Thompson   CeedCall(CeedQFunctionSetImmutable(qf));
90043bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
90143bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
90243bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
90343bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
90443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
90543bbe138SJeremy L Thompson }
90643bbe138SJeremy L Thompson 
90743bbe138SJeremy L Thompson /**
908ca94c3ddSJeremy L Thompson   @brief Get the name of a `CeedQFunctionField`
90943bbe138SJeremy L Thompson 
910ca94c3ddSJeremy L Thompson   @param[in]  qf_field   `CeedQFunctionField`
91143bbe138SJeremy L Thompson   @param[out] field_name Variable to store the field name
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 **/
9176f8994e9SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, const char **field_name) {
9186f8994e9SJeremy L Thompson   *field_name = qf_field->field_name;
91943bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
92043bbe138SJeremy L Thompson }
92143bbe138SJeremy L Thompson 
92243bbe138SJeremy L Thompson /**
923ca94c3ddSJeremy L Thompson   @brief Get the number of components of a `CeedQFunctionField`
92443bbe138SJeremy L Thompson 
925ca94c3ddSJeremy L Thompson   @param[in]  qf_field `CeedQFunctionField`
92643bbe138SJeremy L Thompson   @param[out] size     Variable to store the size of the field
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 **/
93243bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
93343bbe138SJeremy L Thompson   *size = qf_field->size;
93443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
93543bbe138SJeremy L Thompson }
93643bbe138SJeremy L Thompson 
93743bbe138SJeremy L Thompson /**
938ca94c3ddSJeremy L Thompson   @brief Get the @ref CeedEvalMode of a `CeedQFunctionField`
93943bbe138SJeremy L Thompson 
940ca94c3ddSJeremy L Thompson   @param[in]  qf_field  `CeedQFunctionField`
94143bbe138SJeremy L Thompson   @param[out] eval_mode Variable to store the field evaluation mode
94243bbe138SJeremy L Thompson 
94343bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
94443bbe138SJeremy L Thompson 
945e9b533fbSJeremy L Thompson   @ref Advanced
94643bbe138SJeremy L Thompson **/
9472b730f8bSJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, CeedEvalMode *eval_mode) {
94843bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
94943bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
95043bbe138SJeremy L Thompson }
95143bbe138SJeremy L Thompson 
95243bbe138SJeremy L Thompson /**
953ab747706SJeremy L Thompson   @brief Get the data of a `CeedQFunctionField`.
954ab747706SJeremy L Thompson 
955ab747706SJeremy L Thompson   Any arguments set as `NULL` are ignored.
956ab747706SJeremy L Thompson 
957ab747706SJeremy L Thompson   @param[in]  qf_field   `CeedQFunctionField`
958ab747706SJeremy L Thompson   @param[out] field_name Variable to store the field name
959ab747706SJeremy L Thompson   @param[out] size       Variable to store the size of the field
960ab747706SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
961ab747706SJeremy L Thompson 
962ab747706SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
963ab747706SJeremy L Thompson 
964ab747706SJeremy L Thompson   @ref Advanced
965ab747706SJeremy L Thompson **/
9666f8994e9SJeremy L Thompson int CeedQFunctionFieldGetData(CeedQFunctionField qf_field, const char **field_name, CeedInt *size, CeedEvalMode *eval_mode) {
967ab747706SJeremy L Thompson   if (field_name) CeedCall(CeedQFunctionFieldGetName(qf_field, field_name));
968ab747706SJeremy L Thompson   if (size) CeedCall(CeedQFunctionFieldGetSize(qf_field, size));
969ab747706SJeremy L Thompson   if (eval_mode) CeedCall(CeedQFunctionFieldGetEvalMode(qf_field, eval_mode));
970ab747706SJeremy L Thompson   return CEED_ERROR_SUCCESS;
971ab747706SJeremy L Thompson }
972ab747706SJeremy L Thompson 
973ab747706SJeremy L Thompson /**
974ca94c3ddSJeremy L Thompson   @brief Set global context for a `CeedQFunction`
975b11c1e72Sjeremylt 
976ca94c3ddSJeremy L Thompson   @param[in,out] qf  `CeedQFunction`
977ea61e9acSJeremy L Thompson   @param[in]     ctx Context data to set
978b11c1e72Sjeremylt 
979b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
980dfdf5a53Sjeremylt 
9817a982d89SJeremy L. Thompson   @ref User
982b11c1e72Sjeremylt **/
983777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
9842b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&qf->ctx));
985d7b241e6Sjeremylt   qf->ctx = ctx;
986db002c03SJeremy L Thompson   if (ctx) CeedCall(CeedQFunctionContextReference(ctx));
987e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
988d7b241e6Sjeremylt }
989d7b241e6Sjeremylt 
990b11c1e72Sjeremylt /**
991ca94c3ddSJeremy L Thompson   @brief Set writability of `CeedQFunctionContext` when calling the `CeedQFunctionUser`.
9924385fb7fSSebastian Grimberg 
993859c15bbSJames Wright   The default value is `is_writable == true`.
994441428dfSJeremy L Thompson 
995ca94c3ddSJeremy L Thompson   Setting `is_writable == true` indicates the `CeedQFunctionUser` writes into the `CeedQFunctionContext` and requires memory synchronization after calling @ref CeedQFunctionApply().
996441428dfSJeremy L Thompson 
997ca94c3ddSJeremy L Thompson   Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not modify the `CeedQFunctionContext`.
998ea61e9acSJeremy L Thompson   Violating this assertion may lead to inconsistent data.
999441428dfSJeremy L Thompson 
1000441428dfSJeremy L Thompson   Setting `is_writable == false` may offer a performance improvement on GPU backends.
1001441428dfSJeremy L Thompson 
1002ca94c3ddSJeremy L Thompson   @param[in,out] qf          `CeedQFunction`
1003ca94c3ddSJeremy L Thompson   @param[in]     is_writable Boolean flag for writability status
1004441428dfSJeremy L Thompson 
1005441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1006441428dfSJeremy L Thompson 
1007441428dfSJeremy L Thompson   @ref User
1008441428dfSJeremy L Thompson **/
1009441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
1010441428dfSJeremy L Thompson   qf->is_context_writable = is_writable;
1011441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1012441428dfSJeremy L Thompson }
1013441428dfSJeremy L Thompson 
1014441428dfSJeremy L Thompson /**
1015ca94c3ddSJeremy L Thompson   @brief Set estimated number of FLOPs per quadrature required to apply `CeedQFunction`
10166e15d496SJeremy L Thompson 
1017ca94c3ddSJeremy L Thompson   @param[in]  qf    `CeedQFunction` to estimate FLOPs for
1018ea61e9acSJeremy L Thompson   @param[out] flops FLOPs per quadrature point estimate
10196e15d496SJeremy L Thompson 
10206e15d496SJeremy L Thompson   @ref Backend
10216e15d496SJeremy L Thompson **/
10229d36ca50SJeremy L Thompson int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
10236e536b99SJeremy L Thompson   CeedCheck(flops >= 0, CeedQFunctionReturnCeed(qf), CEED_ERROR_INCOMPATIBLE, "Must set non-negative FLOPs estimate");
10246e15d496SJeremy L Thompson   qf->user_flop_estimate = flops;
10256e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10266e15d496SJeremy L Thompson }
10276e15d496SJeremy L Thompson 
10286e15d496SJeremy L Thompson /**
10294c789ea2SJeremy L Thompson   @brief Set the number of tabs to indent for @ref CeedQFunctionView() output
10304c789ea2SJeremy L Thompson 
10314c789ea2SJeremy L Thompson   @param[in] qf       `CeedQFunction` to set the number of view tabs
10324c789ea2SJeremy L Thompson   @param[in] num_tabs Number of view tabs to set
10334c789ea2SJeremy L Thompson 
10344c789ea2SJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
10354c789ea2SJeremy L Thompson 
10364c789ea2SJeremy L Thompson   @ref User
10374c789ea2SJeremy L Thompson **/
10384c789ea2SJeremy L Thompson int CeedQFunctionSetNumViewTabs(CeedQFunction qf, CeedInt num_tabs) {
1039*a299a25bSJeremy L Thompson   CeedCall(CeedObjectSetNumViewTabs((CeedObject)qf, num_tabs));
10404c789ea2SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10414c789ea2SJeremy L Thompson }
10424c789ea2SJeremy L Thompson 
10434c789ea2SJeremy L Thompson /**
1044690992b2SZach Atkins   @brief Get the number of tabs to indent for @ref CeedQFunctionView() output
1045690992b2SZach Atkins 
1046690992b2SZach Atkins   @param[in]  qf       `CeedQFunction` to get the number of view tabs
1047690992b2SZach Atkins   @param[out] num_tabs Number of view tabs
1048690992b2SZach Atkins 
1049690992b2SZach Atkins   @return Error code: 0 - success, otherwise - failure
1050690992b2SZach Atkins 
1051690992b2SZach Atkins   @ref User
1052690992b2SZach Atkins **/
1053690992b2SZach Atkins int CeedQFunctionGetNumViewTabs(CeedQFunction qf, CeedInt *num_tabs) {
1054*a299a25bSJeremy L Thompson   CeedCall(CeedObjectGetNumViewTabs((CeedObject)qf, num_tabs));
1055690992b2SZach Atkins   return CEED_ERROR_SUCCESS;
1056690992b2SZach Atkins }
1057690992b2SZach Atkins 
1058690992b2SZach Atkins /**
1059ca94c3ddSJeremy L Thompson   @brief View a `CeedQFunction`
106075affc3bSjeremylt 
1061ca94c3ddSJeremy L Thompson   @param[in] qf     `CeedQFunction` to view
1062ca94c3ddSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
106375affc3bSjeremylt 
106475affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
106575affc3bSjeremylt 
10667a982d89SJeremy L. Thompson   @ref User
106775affc3bSjeremylt **/
106875affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
10694c789ea2SJeremy L Thompson   char       *tabs = NULL;
1070d3d5610dSJeremy L Thompson   const char *name;
107175affc3bSjeremylt 
10724c789ea2SJeremy L Thompson   {
10734c789ea2SJeremy L Thompson     CeedInt num_tabs = 0;
10744c789ea2SJeremy L Thompson 
10754c789ea2SJeremy L Thompson     CeedCall(CeedQFunctionGetNumViewTabs(qf, &num_tabs));
10764c789ea2SJeremy L Thompson     CeedCall(CeedCalloc(CEED_TAB_WIDTH * num_tabs + 1, &tabs));
10774c789ea2SJeremy L Thompson     for (CeedInt i = 0; i < CEED_TAB_WIDTH * num_tabs; i++) tabs[i] = ' ';
10784c789ea2SJeremy L Thompson   }
10794c789ea2SJeremy L Thompson 
1080d3d5610dSJeremy L Thompson   CeedCall(CeedQFunctionGetName(qf, &name));
10814c789ea2SJeremy L Thompson   fprintf(stream, "%s%sCeedQFunction - %s\n", tabs, qf->is_gallery ? "Gallery " : "User ", name);
108275affc3bSjeremylt 
10834c789ea2SJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", tabs, qf->num_input_fields, qf->num_input_fields > 1 ? "s" : "");
1084d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
10854c789ea2SJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->input_fields[i], i, 1, tabs, stream));
108675affc3bSjeremylt   }
108775affc3bSjeremylt 
10884c789ea2SJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", tabs, qf->num_output_fields, qf->num_output_fields > 1 ? "s" : "");
1089d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
10904c789ea2SJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->output_fields[i], i, 0, tabs, stream));
109175affc3bSjeremylt   }
10924c789ea2SJeremy L Thompson   CeedCall(CeedFree(&tabs));
1093e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
109475affc3bSjeremylt }
109575affc3bSjeremylt 
109675affc3bSjeremylt /**
1097ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedQFunction`
1098b7c9bbdaSJeremy L Thompson 
1099ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction`
1100ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store`Ceed`
1101b7c9bbdaSJeremy L Thompson 
1102b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1103b7c9bbdaSJeremy L Thompson 
1104b7c9bbdaSJeremy L Thompson   @ref Advanced
1105b7c9bbdaSJeremy L Thompson **/
1106b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
1107b0f67a9cSJeremy L Thompson   CeedCall(CeedObjectGetCeed((CeedObject)qf, ceed));
1108b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1109b7c9bbdaSJeremy L Thompson }
1110b7c9bbdaSJeremy L Thompson 
1111b7c9bbdaSJeremy L Thompson /**
11126e536b99SJeremy L Thompson   @brief Return the `Ceed` associated with a `CeedQFunction`
11136e536b99SJeremy L Thompson 
11146e536b99SJeremy L Thompson   @param[in]  qf   `CeedQFunction`
11156e536b99SJeremy L Thompson 
11166e536b99SJeremy L Thompson   @return `Ceed` associated with the `qf`
11176e536b99SJeremy L Thompson 
11186e536b99SJeremy L Thompson   @ref Advanced
11196e536b99SJeremy L Thompson **/
1120b0f67a9cSJeremy L Thompson Ceed CeedQFunctionReturnCeed(CeedQFunction qf) { return CeedObjectReturnCeed((CeedObject)qf); }
11216e536b99SJeremy L Thompson 
11226e536b99SJeremy L Thompson /**
1123ca94c3ddSJeremy L Thompson   @brief Apply the action of a `CeedQFunction`
1124b11c1e72Sjeremylt 
1125ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedQFunction` as immutable.
1126f04ea552SJeremy L Thompson 
1127ca94c3ddSJeremy L Thompson   @param[in]  qf `CeedQFunction`
1128ea61e9acSJeremy L Thompson   @param[in]  Q  Number of quadrature points
1129ca94c3ddSJeremy L Thompson   @param[in]  u  Array of input `CeedVector`
1130ca94c3ddSJeremy L Thompson   @param[out] v  Array of output `CeedVector`
1131b11c1e72Sjeremylt 
1132b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1133dfdf5a53Sjeremylt 
11347a982d89SJeremy L. Thompson   @ref User
1135b11c1e72Sjeremylt **/
11362b730f8bSJeremy L Thompson int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, CeedVector *u, CeedVector *v) {
11371203703bSJeremy L Thompson   CeedInt vec_length;
11381203703bSJeremy L Thompson 
11399bc66399SJeremy L Thompson   CeedCheck(qf->Apply, CeedQFunctionReturnCeed(qf), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionApply");
11401203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetVectorLength(qf, &vec_length));
11419bc66399SJeremy L Thompson   CeedCheck(Q % vec_length == 0, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION,
11429bc66399SJeremy L Thompson             "Number of quadrature points %" CeedInt_FMT " must be a multiple of %" CeedInt_FMT, Q, qf->vec_length);
11431203703bSJeremy L Thompson   CeedCall(CeedQFunctionSetImmutable(qf));
11442b730f8bSJeremy L Thompson   CeedCall(qf->Apply(qf, Q, u, v));
1145e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1146d7b241e6Sjeremylt }
1147d7b241e6Sjeremylt 
1148b11c1e72Sjeremylt /**
1149ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedQFunction`
1150b11c1e72Sjeremylt 
1151ca94c3ddSJeremy L Thompson   @param[in,out] qf `CeedQFunction` to destroy
1152b11c1e72Sjeremylt 
1153b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1154dfdf5a53Sjeremylt 
11557a982d89SJeremy L. Thompson   @ref User
1156b11c1e72Sjeremylt **/
1157d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
1158b0f67a9cSJeremy L Thompson   if (!*qf || CeedObjectDereference((CeedObject)*qf) > 0) {
1159ad6481ceSJeremy L Thompson     *qf = NULL;
1160ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1161ad6481ceSJeremy L Thompson   }
1162fe2413ffSjeremylt   // Backend destroy
1163d7b241e6Sjeremylt   if ((*qf)->Destroy) {
11642b730f8bSJeremy L Thompson     CeedCall((*qf)->Destroy(*qf));
1165d7b241e6Sjeremylt   }
1166fe2413ffSjeremylt   // Free fields
116792ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_input_fields; i++) {
11682b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->input_fields[i]).field_name));
11692b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->input_fields[i]));
1170fe2413ffSjeremylt   }
117192ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_output_fields; i++) {
11722b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->output_fields[i]).field_name));
11732b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->output_fields[i]));
1174fe2413ffSjeremylt   }
11752b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->input_fields));
11762b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->output_fields));
1177777ff853SJeremy L Thompson 
1178777ff853SJeremy L Thompson   // User context data object
11792b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&(*qf)->ctx));
1180fe2413ffSjeremylt 
11812b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->user_source));
11822b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->source_path));
11832b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->gallery_name));
11842b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->kernel_name));
1185b0f67a9cSJeremy L Thompson   CeedCall(CeedObjectDestroy(&(*qf)->obj));
11862b730f8bSJeremy L Thompson   CeedCall(CeedFree(qf));
1187e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1188d7b241e6Sjeremylt }
1189d7b241e6Sjeremylt 
1190d7b241e6Sjeremylt /// @}
1191