xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision 4c789ea28eed8450eb29ac731b0b6c80a3f128bb)
1d275d636SJeremy L Thompson // Copyright (c) 2017-2025, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3d7b241e6Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5d7b241e6Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7d7b241e6Sjeremylt 
83d576824SJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
112b730f8bSJeremy L Thompson #include <ceed/jit-tools.h>
12288c0443SJeremy L Thompson #include <limits.h>
133d576824SJeremy L Thompson #include <stdbool.h>
143d576824SJeremy L Thompson #include <stdio.h>
153d576824SJeremy L Thompson #include <string.h>
16288c0443SJeremy L Thompson 
177a982d89SJeremy L. Thompson /// @file
187a982d89SJeremy L. Thompson /// Implementation of public CeedQFunction interfaces
197a982d89SJeremy L. Thompson 
20288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP
21442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none;
22442e7f0bSjeremylt /// @endcond
23442e7f0bSjeremylt 
247a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
257a982d89SJeremy L. Thompson /// @{
267a982d89SJeremy L. Thompson 
27ca94c3ddSJeremy L Thompson // Indicate that no `CeedQFunction` is provided by the user
287a982d89SJeremy L. Thompson const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
297a982d89SJeremy L. Thompson 
307a982d89SJeremy L. Thompson /// @}
317a982d89SJeremy L. Thompson 
32442e7f0bSjeremylt /// @cond DOXYGEN_SKIP
33288c0443SJeremy L Thompson static struct {
34288c0443SJeremy L Thompson   char              name[CEED_MAX_RESOURCE_LEN];
35288c0443SJeremy L Thompson   char              source[CEED_MAX_RESOURCE_LEN];
36d1d35e2fSjeremylt   CeedInt           vec_length;
37288c0443SJeremy L Thompson   CeedQFunctionUser f;
38288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
39d1d35e2fSjeremylt } gallery_qfunctions[1024];
40288c0443SJeremy L Thompson static size_t num_qfunctions;
41288c0443SJeremy L Thompson /// @endcond
42d7b241e6Sjeremylt 
43777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
44777ff853SJeremy L Thompson /// CeedQFunction Library Internal Functions
45777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
46777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper
47777ff853SJeremy L Thompson /// @{
48777ff853SJeremy L Thompson 
49b11c1e72Sjeremylt /**
50ca94c3ddSJeremy L Thompson   @brief Register a gallery `CeedQFunction`
51288c0443SJeremy L Thompson 
52ea61e9acSJeremy L Thompson   @param[in] name       Name for this backend to respond to
53ca94c3ddSJeremy L Thompson   @param[in] source     Absolute path to source of `CeedQFunction`, "\path\CEED_DIR\gallery\folder\file.h:function_name"
54ca94c3ddSJeremy L Thompson   @param[in] vec_length Vector length.
55ca94c3ddSJeremy L Thompson                           Caller must ensure that number of quadrature points is a multiple of `vec_length`.
56ea61e9acSJeremy L Thompson   @param[in] f          Function pointer to evaluate action at quadrature points.
57ca94c3ddSJeremy L Thompson                           See `CeedQFunctionUser`.
58ca94c3ddSJeremy L Thompson   @param[in] init       Initialization function called by @ref CeedQFunctionCreateInteriorByName() when the `CeedQFunction` is selected.
59288c0443SJeremy L Thompson 
60288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
61288c0443SJeremy L Thompson 
627a982d89SJeremy L. Thompson   @ref Developer
63288c0443SJeremy L Thompson **/
642b730f8bSJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source, CeedInt vec_length, CeedQFunctionUser f,
65288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
661c66c397SJeremy L Thompson   const char *relative_file_path;
6758c07c4fSSebastian Grimberg   int         ierr = 0;
68c042f62fSJeremy L Thompson 
698ccf1006SJeremy L Thompson   CeedDebugEnv("Gallery Register: %s", name);
702b730f8bSJeremy L Thompson   CeedCall(CeedGetJitRelativePath(source, &relative_file_path));
7158c07c4fSSebastian Grimberg   CeedPragmaCritical(CeedQFunctionRegister) {
7258c07c4fSSebastian Grimberg     if (num_qfunctions < sizeof(gallery_qfunctions) / sizeof(gallery_qfunctions[0])) {
73d1d35e2fSjeremylt       strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
74d1d35e2fSjeremylt       gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN - 1] = 0;
752b730f8bSJeremy L Thompson       strncpy(gallery_qfunctions[num_qfunctions].source, relative_file_path, CEED_MAX_RESOURCE_LEN);
76d1d35e2fSjeremylt       gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN - 1] = 0;
77d1d35e2fSjeremylt       gallery_qfunctions[num_qfunctions].vec_length                        = vec_length;
78d1d35e2fSjeremylt       gallery_qfunctions[num_qfunctions].f                                 = f;
79d1d35e2fSjeremylt       gallery_qfunctions[num_qfunctions].init                              = init;
80288c0443SJeremy L Thompson       num_qfunctions++;
8158c07c4fSSebastian Grimberg     } else {
8258c07c4fSSebastian Grimberg       ierr = 1;
8358c07c4fSSebastian Grimberg     }
8458c07c4fSSebastian Grimberg   }
85ca94c3ddSJeremy L Thompson   CeedCheck(ierr == 0, NULL, CEED_ERROR_MAJOR, "Too many gallery CeedQFunctions");
86e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
87288c0443SJeremy L Thompson }
88288c0443SJeremy L Thompson 
89288c0443SJeremy L Thompson /**
90ca94c3ddSJeremy L Thompson   @brief Set a `CeedQFunction` field, used by @ref CeedQFunctionAddInput() and @ref CeedQFunctionAddOutput()
917a982d89SJeremy L. Thompson 
92ca94c3ddSJeremy L Thompson   @param[out] f           `CeedQFunctionField`
93ca94c3ddSJeremy L Thompson   @param[in]  field_name  Name of `CeedQFunction` field
94ca94c3ddSJeremy L Thompson   @param[in]  size        Size of `CeedQFunction` field, (`num_comp * 1`) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_WEIGHT, (`num_comp * 1`) for @ref CEED_EVAL_INTERP for an \f$H^1\f$ space or (`num_comp * dim`) for an \f$H(\mathrm{div})\f$ or \f$H(\mathrm{curl})\f$ space, (`num_comp * dim`) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` and `curl_dim = dim` for @ref CEED_EVAL_CURL.
95ca94c3ddSJeremy L Thompson   @param[in]  eval_mode   @ref CEED_EVAL_NONE to use values directly,
96ca94c3ddSJeremy L Thompson                             @ref CEED_EVAL_WEIGHT to use quadrature weights,
97ca94c3ddSJeremy L Thompson                             @ref CEED_EVAL_INTERP to use interpolated values,
98ca94c3ddSJeremy L Thompson                             @ref CEED_EVAL_GRAD to use gradients,
99ca94c3ddSJeremy L Thompson                             @ref CEED_EVAL_DIV to use divergence,
100ca94c3ddSJeremy L Thompson                             @ref CEED_EVAL_CURL to use curl
1017a982d89SJeremy L. Thompson 
1027a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1037a982d89SJeremy L. Thompson 
1047a982d89SJeremy L. Thompson   @ref Developer
1057a982d89SJeremy L. Thompson **/
1062b730f8bSJeremy L Thompson static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
1072b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, f));
1082b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*f)->field_name));
1097a982d89SJeremy L. Thompson   (*f)->size      = size;
110d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
111e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1127a982d89SJeremy L. Thompson }
1137a982d89SJeremy L. Thompson 
1147a982d89SJeremy L. Thompson /**
115ca94c3ddSJeremy L Thompson   @brief View a field of a `CeedQFunction`
1167a982d89SJeremy L. Thompson 
117ca94c3ddSJeremy L Thompson   @param[in] field        `CeedQFunction` field to view
118d1d35e2fSjeremylt   @param[in] field_number Number of field being viewed
1194c4400c7SValeria Barra   @param[in] in           true for input field, false for output
120*4c789ea2SJeremy 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 **/
127*4c789ea2SJeremy 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,
135*4c789ea2SJeremy L Thompson           "%s    %s field %" CeedInt_FMT
136*4c789ea2SJeremy L Thompson           ":\n%s"
137*4c789ea2SJeremy L Thompson           "      Name: \"%s\"\n%s"
1382b730f8bSJeremy L Thompson           "      Size: %" CeedInt_FMT
139*4c789ea2SJeremy L Thompson           "\n%s"
1407a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
141*4c789ea2SJeremy L Thompson           tabs, inout, field_number, tabs, field_name, tabs, size, tabs, CeedEvalModes[eval_mode]);
142e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1437a982d89SJeremy L. Thompson }
1447a982d89SJeremy L. Thompson 
145777ff853SJeremy L Thompson /**
146777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
147777ff853SJeremy L Thompson 
148ea61e9acSJeremy L Thompson   @param[in,out] qf     CeedQFunction
149ea61e9acSJeremy L Thompson   @param[in]     status Boolean value to set as Fortran status
150777ff853SJeremy L Thompson 
151777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
152777ff853SJeremy L Thompson 
153777ff853SJeremy L Thompson   @ref Backend
154777ff853SJeremy L Thompson **/
155777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
156f04ea552SJeremy L Thompson   qf->is_fortran = status;
157e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
158777ff853SJeremy L Thompson }
159777ff853SJeremy L Thompson 
1607a982d89SJeremy L. Thompson /// @}
1617a982d89SJeremy L. Thompson 
1627a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1637a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1647a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1657a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1667a982d89SJeremy L. Thompson /// @{
1677a982d89SJeremy L. Thompson 
1687a982d89SJeremy L. Thompson /**
169ca94c3ddSJeremy L Thompson   @brief Get the vector length of a `CeedQFunction`
1707a982d89SJeremy L. Thompson 
171ca94c3ddSJeremy L Thompson   @param[in]  qf         `CeedQFunction`
172d1d35e2fSjeremylt   @param[out] vec_length Variable to store vector length
1737a982d89SJeremy L. Thompson 
1747a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1757a982d89SJeremy L. Thompson 
1767a982d89SJeremy L. Thompson   @ref Backend
1777a982d89SJeremy L. Thompson **/
178d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
179d1d35e2fSjeremylt   *vec_length = qf->vec_length;
180e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1817a982d89SJeremy L. Thompson }
1827a982d89SJeremy L. Thompson 
1837a982d89SJeremy L. Thompson /**
184ca94c3ddSJeremy L Thompson   @brief Get the number of inputs and outputs to a `CeedQFunction`
1857a982d89SJeremy L. Thompson 
186ca94c3ddSJeremy L Thompson   @param[in]  qf         `CeedQFunction`
187d1d35e2fSjeremylt   @param[out] num_input  Variable to store number of input fields
188d1d35e2fSjeremylt   @param[out] num_output Variable to store number of output fields
1897a982d89SJeremy L. Thompson 
1907a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1917a982d89SJeremy L. Thompson 
1927a982d89SJeremy L. Thompson   @ref Backend
1937a982d89SJeremy L. Thompson **/
1942b730f8bSJeremy L Thompson int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, CeedInt *num_output) {
195d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
196d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
197e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1987a982d89SJeremy L. Thompson }
1997a982d89SJeremy L. Thompson 
2007a982d89SJeremy L. Thompson /**
201d3d5610dSJeremy L Thompson   @brief Get the name of the `CeedQFunction`.
202d3d5610dSJeremy L Thompson     Use the `name` if created via @ref `CeedQFunctionCreateInteriorByName`, otherwise return the kernel name via @ref `CeedQFunctionGetKernelName`.
203d3d5610dSJeremy L Thompson 
204d3d5610dSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
205d3d5610dSJeremy L Thompson   @param[out] kernel_name Variable to store `CeedQFunction` name
206d3d5610dSJeremy L Thompson 
207d3d5610dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
208d3d5610dSJeremy L Thompson 
209d3d5610dSJeremy L Thompson   @ref Backend
210d3d5610dSJeremy L Thompson **/
211d3d5610dSJeremy L Thompson int CeedQFunctionGetName(CeedQFunction qf, const char **name) {
212d3d5610dSJeremy L Thompson   if (qf->is_gallery) {
213d3d5610dSJeremy L Thompson     *name = qf->gallery_name;
214d3d5610dSJeremy L Thompson   } else {
215d3d5610dSJeremy L Thompson     CeedCall(CeedQFunctionGetKernelName(qf, name));
216d3d5610dSJeremy L Thompson   }
217d3d5610dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
218d3d5610dSJeremy L Thompson }
219d3d5610dSJeremy L Thompson 
220d3d5610dSJeremy L Thompson /**
221ca94c3ddSJeremy L Thompson   @brief Get the name of the user function for a `CeedQFunction`
2227a982d89SJeremy L. Thompson 
223ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
224d3d5610dSJeremy L Thompson   @param[out] kernel_name Variable to store string holding kernel name
2257a982d89SJeremy L. Thompson 
2267a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2277a982d89SJeremy L. Thompson 
2287a982d89SJeremy L. Thompson   @ref Backend
2297a982d89SJeremy L. Thompson **/
2307d023984SJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, const char **kernel_name) {
231ca5eadf8SJeremy L Thompson   if (!qf->kernel_name) {
232ca5eadf8SJeremy L Thompson     char *kernel_name_copy;
233ca5eadf8SJeremy L Thompson 
234ca5eadf8SJeremy L Thompson     if (qf->user_source) {
235ca5eadf8SJeremy L Thompson       const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
236ca5eadf8SJeremy L Thompson       size_t      kernel_name_len = strlen(kernel_name);
237ca5eadf8SJeremy L Thompson 
2382b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(kernel_name_len + 1, &kernel_name_copy));
239ca5eadf8SJeremy L Thompson       memcpy(kernel_name_copy, kernel_name, kernel_name_len);
240ca5eadf8SJeremy L Thompson     } else {
2412b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &kernel_name_copy));
242ca5eadf8SJeremy L Thompson     }
243ca5eadf8SJeremy L Thompson     qf->kernel_name = kernel_name_copy;
244ca5eadf8SJeremy L Thompson   }
245ca5eadf8SJeremy L Thompson 
2467d023984SJeremy L Thompson   *kernel_name = qf->kernel_name;
24743e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
24843e1b16fSJeremy L Thompson }
24943e1b16fSJeremy L Thompson 
25043e1b16fSJeremy L Thompson /**
251ca94c3ddSJeremy L Thompson   @brief Get the source path string for a `CeedQFunction`
25243e1b16fSJeremy L Thompson 
253ca94c3ddSJeremy L Thompson   @param[in]  qf          `CeedQFunction`
25443e1b16fSJeremy L Thompson   @param[out] source_path Variable to store source path string
25543e1b16fSJeremy L Thompson 
25643e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
25743e1b16fSJeremy L Thompson 
25843e1b16fSJeremy L Thompson   @ref Backend
25943e1b16fSJeremy L Thompson **/
26034ffed21SJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, const char **source_path) {
261ca5eadf8SJeremy L Thompson   if (!qf->source_path && qf->user_source) {
262ca5eadf8SJeremy L Thompson     Ceed        ceed;
263ca5eadf8SJeremy L Thompson     bool        is_absolute_path;
26422070f95SJeremy L Thompson     char       *source_path_copy;
26522070f95SJeremy L Thompson     const char *absolute_path;
266ca5eadf8SJeremy L Thompson     const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
267ca5eadf8SJeremy L Thompson     size_t      kernel_name_len = strlen(kernel_name);
268ca5eadf8SJeremy L Thompson 
2692b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
2702b730f8bSJeremy L Thompson     CeedCall(CeedCheckFilePath(ceed, qf->user_source, &is_absolute_path));
271ca5eadf8SJeremy L Thompson     if (is_absolute_path) {
272ca5eadf8SJeremy L Thompson       absolute_path = (char *)qf->user_source;
273ca5eadf8SJeremy L Thompson     } else {
2742b730f8bSJeremy L Thompson       CeedCall(CeedGetJitAbsolutePath(ceed, qf->user_source, &absolute_path));
275ca5eadf8SJeremy L Thompson     }
2769bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&ceed));
277ca5eadf8SJeremy L Thompson 
278ca5eadf8SJeremy L Thompson     size_t source_len = strlen(absolute_path) - kernel_name_len - 1;
2791c66c397SJeremy L Thompson 
2802b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(source_len + 1, &source_path_copy));
281ca5eadf8SJeremy L Thompson     memcpy(source_path_copy, absolute_path, source_len);
282ca5eadf8SJeremy L Thompson     qf->source_path = source_path_copy;
283ca5eadf8SJeremy L Thompson 
2842b730f8bSJeremy L Thompson     if (!is_absolute_path) CeedCall(CeedFree(&absolute_path));
285ca5eadf8SJeremy L Thompson   }
286ca5eadf8SJeremy L Thompson 
28743e1b16fSJeremy L Thompson   *source_path = (char *)qf->source_path;
288e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2897a982d89SJeremy L. Thompson }
2907a982d89SJeremy L. Thompson 
2917a982d89SJeremy L. Thompson /**
292ca94c3ddSJeremy L Thompson   @brief Initialize and load `CeedQFunction` source file into string buffer, including full text of local files in place of `#include "local.h"`.
2934385fb7fSSebastian Grimberg 
294ca94c3ddSJeremy L Thompson   The `buffer` is set to `NULL` if there is no `CeedQFunction` source file.
2954385fb7fSSebastian Grimberg 
296f8d308faSJed Brown   Note: This function may as well return a mutable buffer, but all current uses
297f8d308faSJed Brown   do not modify it. (This is just a downside of `const` semantics with output
298f8d308faSJed Brown   arguments instead of returns.)
299f8d308faSJed Brown 
300ca94c3ddSJeremy L Thompson   Note: Caller is responsible for freeing the string buffer with @ref CeedFree().
3013d3250a0SJeremy L Thompson 
302ca94c3ddSJeremy L Thompson   @param[in]  qf            `CeedQFunction`
303f74ec584SJeremy L Thompson   @param[out] source_buffer String buffer for source file contents
3043d3250a0SJeremy L Thompson 
3053d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3063d3250a0SJeremy L Thompson 
3073d3250a0SJeremy L Thompson   @ref Backend
3083d3250a0SJeremy L Thompson **/
309f8d308faSJed Brown int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, const char **source_buffer) {
31034ffed21SJeremy L Thompson   const char *source_path;
3113d3250a0SJeremy L Thompson 
3122b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetSourcePath(qf, &source_path));
3133d3250a0SJeremy L Thompson   *source_buffer = NULL;
3143d3250a0SJeremy L Thompson   if (source_path) {
3151203703bSJeremy L Thompson     Ceed  ceed;
316f8d308faSJed Brown     char *buffer = NULL;
3171203703bSJeremy L Thompson 
3181203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
319f8d308faSJed Brown     CeedCall(CeedLoadSourceToBuffer(ceed, source_path, &buffer));
3209bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&ceed));
321f8d308faSJed Brown     *source_buffer = buffer;
3223d3250a0SJeremy L Thompson   }
3233d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3243d3250a0SJeremy L Thompson }
3253d3250a0SJeremy L Thompson 
3263d3250a0SJeremy L Thompson /**
327ca94c3ddSJeremy L Thompson   @brief Get the User Function for a `CeedQFunction`
3287a982d89SJeremy L. Thompson 
329ca94c3ddSJeremy L Thompson   @param[in]  qf `CeedQFunction`
3307a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
3317a982d89SJeremy L. Thompson 
3327a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3337a982d89SJeremy L. Thompson 
3347a982d89SJeremy L. Thompson   @ref Backend
3357a982d89SJeremy L. Thompson **/
3367a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
3377a982d89SJeremy L. Thompson   *f = qf->function;
338e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3397a982d89SJeremy L. Thompson }
3407a982d89SJeremy L. Thompson 
3417a982d89SJeremy L. Thompson /**
342*4c789ea2SJeremy L Thompson   @brief Get the number of tabs to indent for @ref CeedQFunctionView() output
343*4c789ea2SJeremy L Thompson 
344*4c789ea2SJeremy L Thompson   @param[in]  qf       `CeedQFunction` to get the number of view tabs
345*4c789ea2SJeremy L Thompson   @param[out] num_tabs Number of view tabs
346*4c789ea2SJeremy L Thompson 
347*4c789ea2SJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
348*4c789ea2SJeremy L Thompson 
349*4c789ea2SJeremy L Thompson   @ref Backend
350*4c789ea2SJeremy L Thompson **/
351*4c789ea2SJeremy L Thompson int CeedQFunctionGetNumViewTabs(CeedQFunction qf, CeedInt *num_tabs) {
352*4c789ea2SJeremy L Thompson   *num_tabs = qf->num_tabs;
353*4c789ea2SJeremy L Thompson   return CEED_ERROR_SUCCESS;
354*4c789ea2SJeremy L Thompson }
355*4c789ea2SJeremy L Thompson 
356*4c789ea2SJeremy 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) {
61634359f16Sjeremylt   qf->ref_count++;
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));
680db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*qf)->ceed));
681d1d35e2fSjeremylt   (*qf)->ref_count           = 1;
682d1d35e2fSjeremylt   (*qf)->vec_length          = vec_length;
683f04ea552SJeremy L Thompson   (*qf)->is_identity         = false;
684441428dfSJeremy L Thompson   (*qf)->is_context_writable = true;
6857a982d89SJeremy L. Thompson   (*qf)->function            = f;
6866e15d496SJeremy L Thompson   (*qf)->user_flop_estimate  = -1;
68743e1b16fSJeremy L Thompson   if (strlen(source)) {
688ca5eadf8SJeremy L Thompson     size_t user_source_len = strlen(source);
689ee5a26f2SJeremy L Thompson 
6902b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(user_source_len + 1, &user_source_copy));
691ca5eadf8SJeremy L Thompson     memcpy(user_source_copy, source, user_source_len);
692ca5eadf8SJeremy L Thompson     (*qf)->user_source = user_source_copy;
69343e1b16fSJeremy L Thompson   }
6942b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields));
6952b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields));
6962b730f8bSJeremy L Thompson   CeedCall(ceed->QFunctionCreate(*qf));
697e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6987a982d89SJeremy L. Thompson }
6997a982d89SJeremy L. Thompson 
7007a982d89SJeremy L. Thompson /**
701ca94c3ddSJeremy L Thompson   @brief Create a `CeedQFunction` for evaluating interior (volumetric) terms by name
702288c0443SJeremy L Thompson 
703ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedQFunction`
704ca94c3ddSJeremy L Thompson   @param[in]  name Name of `CeedQFunction` to use from gallery
705ca94c3ddSJeremy L Thompson   @param[out] qf   Address of the variable where the newly created `CeedQFunction` will be stored
706288c0443SJeremy L Thompson 
707288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
708288c0443SJeremy L Thompson 
7097a982d89SJeremy L. Thompson   @ref User
710288c0443SJeremy L Thompson **/
7112b730f8bSJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, CeedQFunction *qf) {
712f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
713288c0443SJeremy L Thompson 
7142b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionRegisterAll());
715288c0443SJeremy L Thompson   // Find matching backend
716ca94c3ddSJeremy L Thompson   CeedCheck(name, ceed, CEED_ERROR_INCOMPLETE, "No CeedQFunction name provided");
717288c0443SJeremy L Thompson   for (size_t i = 0; i < num_qfunctions; i++) {
718288c0443SJeremy L Thompson     size_t      n;
719d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
7202b730f8bSJeremy L Thompson     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {
7212b730f8bSJeremy L Thompson     }
722d1d35e2fSjeremylt     if (n > match_len) {
723d1d35e2fSjeremylt       match_len   = n;
724f7e22acaSJeremy L Thompson       match_index = i;
725288c0443SJeremy L Thompson     }
726288c0443SJeremy L Thompson   }
727ca94c3ddSJeremy L Thompson   CeedCheck(match_len > 0, ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery CeedQFunction");
728288c0443SJeremy L Thompson 
729288c0443SJeremy L Thompson   // Create QFunction
7302b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInterior(ceed, gallery_qfunctions[match_index].vec_length, gallery_qfunctions[match_index].f,
7312b730f8bSJeremy L Thompson                                        gallery_qfunctions[match_index].source, qf));
732288c0443SJeremy L Thompson 
733288c0443SJeremy L Thompson   // QFunction specific setup
7342b730f8bSJeremy L Thompson   CeedCall(gallery_qfunctions[match_index].init(ceed, name, *qf));
735288c0443SJeremy L Thompson 
73675affc3bSjeremylt   // Copy name
7372b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name));
73843e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
739e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
740288c0443SJeremy L Thompson }
741288c0443SJeremy L Thompson 
742288c0443SJeremy L Thompson /**
743ca94c3ddSJeremy L Thompson   @brief Create an identity `CeedQFunction`.
7444385fb7fSSebastian Grimberg 
745ea61e9acSJeremy L Thompson   Inputs are written into outputs in the order given.
746ca94c3ddSJeremy 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.
747ca94c3ddSJeremy 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.
7480219ea01SJeremy L Thompson 
749ca94c3ddSJeremy L Thompson   @param[in]  ceed     `Ceed` object used to create the `CeedQFunction`
750ca94c3ddSJeremy L Thompson   @param[in]  size     Size of the `CeedQFunction` fields
751ca94c3ddSJeremy L Thompson   @param[in]  in_mode  @ref CeedEvalMode for input to `CeedQFunction`
752ca94c3ddSJeremy L Thompson   @param[in]  out_mode @ref CeedEvalMode for output to `CeedQFunction`
753ca94c3ddSJeremy L Thompson   @param[out] qf       Address of the variable where the newly created `CeedQFunction` will be stored
7540219ea01SJeremy L Thompson 
7550219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
7560219ea01SJeremy L Thompson 
7577a982d89SJeremy L. Thompson   @ref User
7580219ea01SJeremy L Thompson **/
7592b730f8bSJeremy L Thompson int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, CeedEvalMode out_mode, CeedQFunction *qf) {
7601c66c397SJeremy L Thompson   CeedQFunctionContext  ctx;
7611c66c397SJeremy L Thompson   CeedContextFieldLabel size_label;
7621c66c397SJeremy L Thompson 
7632b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Identity", qf));
7642b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(*qf, "input", size, in_mode));
7652b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(*qf, "output", size, out_mode));
7660219ea01SJeremy L Thompson 
767f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
768547dbd6fSJeremy L Thompson 
7692b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(*qf, &ctx));
7702b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label));
7712b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetInt32(ctx, size_label, &size));
7721485364cSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx));
773e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7740219ea01SJeremy L Thompson }
7750219ea01SJeremy L Thompson 
7760219ea01SJeremy L Thompson /**
777ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedQFunction`.
7784385fb7fSSebastian Grimberg 
779ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedQFunctionDestroy().
780512bb800SJeremy L Thompson 
781ca94c3ddSJeremy 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`.
782ca94c3ddSJeremy L Thompson         This `CeedQFunction` will be destroyed if `*qf_copy` is the only reference to this `CeedQFunction`.
7839560d06aSjeremylt 
784ca94c3ddSJeremy L Thompson   @param[in]  qf      `CeedQFunction` to copy reference to
7859560d06aSjeremylt   @param[out] qf_copy Variable to store copied reference
7869560d06aSjeremylt 
7879560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7889560d06aSjeremylt 
7899560d06aSjeremylt   @ref User
7909560d06aSjeremylt **/
7919560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
7922b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionReference(qf));
7932b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(qf_copy));
7949560d06aSjeremylt   *qf_copy = qf;
7959560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7969560d06aSjeremylt }
7979560d06aSjeremylt 
7989560d06aSjeremylt /**
799ca94c3ddSJeremy L Thompson   @brief Add a `CeedQFunction` input
800b11c1e72Sjeremylt 
801ca94c3ddSJeremy L Thompson   @param[in,out] qf         `CeedQFunction`
802ca94c3ddSJeremy L Thompson   @param[in]     field_name Name of `CeedQFunction` field
803d538d163SJeremy L Thompson   @param[in]     size       Size of `CeedQFunction` field,
804d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_NONE,
805d538d163SJeremy 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,
806d538d163SJeremy L Thompson                               (`num_comp * dim`) for @ref CEED_EVAL_GRAD,
807d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_DIV, and
808d538d163SJeremy L Thompson                               (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` otherwise `curl_dim = dim` for @ref CEED_EVAL_CURL.
809ca94c3ddSJeremy L Thompson   @param[in]     eval_mode  @ref CEED_EVAL_NONE to use values directly,
810ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_INTERP to use interpolated values,
811ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_GRAD to use gradients,
812ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_DIV to use divergence,
813ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_CURL to use curl
814b11c1e72Sjeremylt 
815d538d163SJeremy L Thompson   Note: In the user `CeedQFunctionUser`, the `in` argument list the fields in the order given by the calls to `CeedQFunctionAddInput`.
816d538d163SJeremy L Thompson 
817b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
818dfdf5a53Sjeremylt 
8197a982d89SJeremy L. Thompson   @ref User
820b11c1e72Sjeremylt **/
8212b730f8bSJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
8221203703bSJeremy L Thompson   bool is_immutable;
8231203703bSJeremy L Thompson 
8241203703bSJeremy L Thompson   CeedCall(CeedQFunctionIsImmutable(qf, &is_immutable));
8259bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedQFunctionReturnCeed(qf), CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
8269bc66399SJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT || size == 1, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION, "CEED_EVAL_WEIGHT should have size 1");
827643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
8289bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8293f08121cSJeremy L Thompson               "CeedQFunction field names must be unique. Duplicate name: %s", field_name);
830643fbb69SJeremy L Thompson   }
831643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
8329bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8333f08121cSJeremy L Thompson               "CeedQFunction field names must be unique. Duplicate name: %s", field_name);
834643fbb69SJeremy L Thompson   }
8352b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], field_name, size, eval_mode));
836d1d35e2fSjeremylt   qf->num_input_fields++;
837e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
838d7b241e6Sjeremylt }
839d7b241e6Sjeremylt 
840b11c1e72Sjeremylt /**
841ca94c3ddSJeremy L Thompson   @brief Add a `CeedQFunction` output
842b11c1e72Sjeremylt 
843ca94c3ddSJeremy L Thompson   @param[in,out] qf         `CeedQFunction`
844ca94c3ddSJeremy L Thompson   @param[in]     field_name Name of `CeedQFunction` field
845d538d163SJeremy L Thompson   @param[in]     size       Size of `CeedQFunction` field,
846d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_NONE,
847d538d163SJeremy 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,
848d538d163SJeremy L Thompson                               (`num_comp * dim`) for @ref CEED_EVAL_GRAD,
849d538d163SJeremy L Thompson                               (`num_comp * 1`) for @ref CEED_EVAL_DIV, and
850d538d163SJeremy L Thompson                               (`num_comp * curl_dim`) with `curl_dim = 1` if `dim < 3` otherwise `curl_dim = dim` for @ref CEED_EVAL_CURL.
851ca94c3ddSJeremy L Thompson   @param[in]     eval_mode  @ref CEED_EVAL_NONE to use values directly,
852ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_INTERP to use interpolated values,
853ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_GRAD to use gradients,
854ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_DIV to use divergence,
855ca94c3ddSJeremy L Thompson                               @ref CEED_EVAL_CURL to use curl.
856b11c1e72Sjeremylt 
857d538d163SJeremy L Thompson   Note: In the user `CeedQFunctionUser`, the `out` argument list the fields in the order given by the calls to `CeedQFunctionAddOutput`.
858d538d163SJeremy L Thompson 
859b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
860dfdf5a53Sjeremylt 
8617a982d89SJeremy L. Thompson   @ref User
862b11c1e72Sjeremylt **/
8632b730f8bSJeremy L Thompson int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
8641203703bSJeremy L Thompson   bool is_immutable;
8651203703bSJeremy L Thompson 
8661203703bSJeremy L Thompson   CeedCall(CeedQFunctionIsImmutable(qf, &is_immutable));
8679bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedQFunctionReturnCeed(qf), CEED_ERROR_MAJOR, "CeedQFunction cannot be changed after set as immutable");
8689bc66399SJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION,
8699bc66399SJeremy L Thompson             "Cannot create CeedQFunction output with CEED_EVAL_WEIGHT");
870643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
8719bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8729bc66399SJeremy L Thompson               "CeedQFunction field names must be unique");
873643fbb69SJeremy L Thompson   }
874643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
8759bc66399SJeremy L Thompson     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR,
8769bc66399SJeremy L Thompson               "CeedQFunction field names must be unique");
877643fbb69SJeremy L Thompson   }
8782b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], field_name, size, eval_mode));
879d1d35e2fSjeremylt   qf->num_output_fields++;
880e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
881d7b241e6Sjeremylt }
882d7b241e6Sjeremylt 
883dfdf5a53Sjeremylt /**
884ca94c3ddSJeremy L Thompson   @brief Get the `CeedQFunctionField` of a `CeedQFunction`
88543bbe138SJeremy L Thompson 
886ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedQFunction` as immutable.
887f04ea552SJeremy L Thompson 
888ca94c3ddSJeremy L Thompson   @param[in]  qf                `CeedQFunction`
889f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
890f74ec584SJeremy L Thompson   @param[out] input_fields      Variable to store input fields
891f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
892f74ec584SJeremy L Thompson   @param[out] output_fields     Variable to store output fields
89343bbe138SJeremy L Thompson 
89443bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
89543bbe138SJeremy L Thompson 
896e9b533fbSJeremy L Thompson   @ref Advanced
89743bbe138SJeremy L Thompson **/
8982b730f8bSJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields, CeedQFunctionField **input_fields, CeedInt *num_output_fields,
89943bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
9001203703bSJeremy L Thompson   CeedCall(CeedQFunctionSetImmutable(qf));
90143bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
90243bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
90343bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
90443bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
90543bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
90643bbe138SJeremy L Thompson }
90743bbe138SJeremy L Thompson 
90843bbe138SJeremy L Thompson /**
909ca94c3ddSJeremy L Thompson   @brief Get the name of a `CeedQFunctionField`
91043bbe138SJeremy L Thompson 
911ca94c3ddSJeremy L Thompson   @param[in]  qf_field   `CeedQFunctionField`
91243bbe138SJeremy L Thompson   @param[out] field_name Variable to store the field name
91343bbe138SJeremy L Thompson 
91443bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
91543bbe138SJeremy L Thompson 
916e9b533fbSJeremy L Thompson   @ref Advanced
91743bbe138SJeremy L Thompson **/
9186f8994e9SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, const char **field_name) {
9196f8994e9SJeremy L Thompson   *field_name = qf_field->field_name;
92043bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
92143bbe138SJeremy L Thompson }
92243bbe138SJeremy L Thompson 
92343bbe138SJeremy L Thompson /**
924ca94c3ddSJeremy L Thompson   @brief Get the number of components of a `CeedQFunctionField`
92543bbe138SJeremy L Thompson 
926ca94c3ddSJeremy L Thompson   @param[in]  qf_field `CeedQFunctionField`
92743bbe138SJeremy L Thompson   @param[out] size     Variable to store the size of the field
92843bbe138SJeremy L Thompson 
92943bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
93043bbe138SJeremy L Thompson 
931e9b533fbSJeremy L Thompson   @ref Advanced
93243bbe138SJeremy L Thompson **/
93343bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
93443bbe138SJeremy L Thompson   *size = qf_field->size;
93543bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
93643bbe138SJeremy L Thompson }
93743bbe138SJeremy L Thompson 
93843bbe138SJeremy L Thompson /**
939ca94c3ddSJeremy L Thompson   @brief Get the @ref CeedEvalMode of a `CeedQFunctionField`
94043bbe138SJeremy L Thompson 
941ca94c3ddSJeremy L Thompson   @param[in]  qf_field  `CeedQFunctionField`
94243bbe138SJeremy L Thompson   @param[out] eval_mode Variable to store the field evaluation mode
94343bbe138SJeremy L Thompson 
94443bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
94543bbe138SJeremy L Thompson 
946e9b533fbSJeremy L Thompson   @ref Advanced
94743bbe138SJeremy L Thompson **/
9482b730f8bSJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, CeedEvalMode *eval_mode) {
94943bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
95043bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
95143bbe138SJeremy L Thompson }
95243bbe138SJeremy L Thompson 
95343bbe138SJeremy L Thompson /**
954ab747706SJeremy L Thompson   @brief Get the data of a `CeedQFunctionField`.
955ab747706SJeremy L Thompson 
956ab747706SJeremy L Thompson   Any arguments set as `NULL` are ignored.
957ab747706SJeremy L Thompson 
958ab747706SJeremy L Thompson   @param[in]  qf_field   `CeedQFunctionField`
959ab747706SJeremy L Thompson   @param[out] field_name Variable to store the field name
960ab747706SJeremy L Thompson   @param[out] size       Variable to store the size of the field
961ab747706SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
962ab747706SJeremy L Thompson 
963ab747706SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
964ab747706SJeremy L Thompson 
965ab747706SJeremy L Thompson   @ref Advanced
966ab747706SJeremy L Thompson **/
9676f8994e9SJeremy L Thompson int CeedQFunctionFieldGetData(CeedQFunctionField qf_field, const char **field_name, CeedInt *size, CeedEvalMode *eval_mode) {
968ab747706SJeremy L Thompson   if (field_name) CeedCall(CeedQFunctionFieldGetName(qf_field, field_name));
969ab747706SJeremy L Thompson   if (size) CeedCall(CeedQFunctionFieldGetSize(qf_field, size));
970ab747706SJeremy L Thompson   if (eval_mode) CeedCall(CeedQFunctionFieldGetEvalMode(qf_field, eval_mode));
971ab747706SJeremy L Thompson   return CEED_ERROR_SUCCESS;
972ab747706SJeremy L Thompson }
973ab747706SJeremy L Thompson 
974ab747706SJeremy L Thompson /**
975ca94c3ddSJeremy L Thompson   @brief Set global context for a `CeedQFunction`
976b11c1e72Sjeremylt 
977ca94c3ddSJeremy L Thompson   @param[in,out] qf  `CeedQFunction`
978ea61e9acSJeremy L Thompson   @param[in]     ctx Context data to set
979b11c1e72Sjeremylt 
980b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
981dfdf5a53Sjeremylt 
9827a982d89SJeremy L. Thompson   @ref User
983b11c1e72Sjeremylt **/
984777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
9852b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&qf->ctx));
986d7b241e6Sjeremylt   qf->ctx = ctx;
987db002c03SJeremy L Thompson   if (ctx) CeedCall(CeedQFunctionContextReference(ctx));
988e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
989d7b241e6Sjeremylt }
990d7b241e6Sjeremylt 
991b11c1e72Sjeremylt /**
992ca94c3ddSJeremy L Thompson   @brief Set writability of `CeedQFunctionContext` when calling the `CeedQFunctionUser`.
9934385fb7fSSebastian Grimberg 
994859c15bbSJames Wright   The default value is `is_writable == true`.
995441428dfSJeremy L Thompson 
996ca94c3ddSJeremy L Thompson   Setting `is_writable == true` indicates the `CeedQFunctionUser` writes into the `CeedQFunctionContext` and requires memory synchronization after calling @ref CeedQFunctionApply().
997441428dfSJeremy L Thompson 
998ca94c3ddSJeremy L Thompson   Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not modify the `CeedQFunctionContext`.
999ea61e9acSJeremy L Thompson   Violating this assertion may lead to inconsistent data.
1000441428dfSJeremy L Thompson 
1001441428dfSJeremy L Thompson   Setting `is_writable == false` may offer a performance improvement on GPU backends.
1002441428dfSJeremy L Thompson 
1003ca94c3ddSJeremy L Thompson   @param[in,out] qf          `CeedQFunction`
1004ca94c3ddSJeremy L Thompson   @param[in]     is_writable Boolean flag for writability status
1005441428dfSJeremy L Thompson 
1006441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1007441428dfSJeremy L Thompson 
1008441428dfSJeremy L Thompson   @ref User
1009441428dfSJeremy L Thompson **/
1010441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
1011441428dfSJeremy L Thompson   qf->is_context_writable = is_writable;
1012441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1013441428dfSJeremy L Thompson }
1014441428dfSJeremy L Thompson 
1015441428dfSJeremy L Thompson /**
1016ca94c3ddSJeremy L Thompson   @brief Set estimated number of FLOPs per quadrature required to apply `CeedQFunction`
10176e15d496SJeremy L Thompson 
1018ca94c3ddSJeremy L Thompson   @param[in]  qf    `CeedQFunction` to estimate FLOPs for
1019ea61e9acSJeremy L Thompson   @param[out] flops FLOPs per quadrature point estimate
10206e15d496SJeremy L Thompson 
10216e15d496SJeremy L Thompson   @ref Backend
10226e15d496SJeremy L Thompson **/
10239d36ca50SJeremy L Thompson int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
10246e536b99SJeremy L Thompson   CeedCheck(flops >= 0, CeedQFunctionReturnCeed(qf), CEED_ERROR_INCOMPATIBLE, "Must set non-negative FLOPs estimate");
10256e15d496SJeremy L Thompson   qf->user_flop_estimate = flops;
10266e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10276e15d496SJeremy L Thompson }
10286e15d496SJeremy L Thompson 
10296e15d496SJeremy L Thompson /**
1030*4c789ea2SJeremy L Thompson   @brief Set the number of tabs to indent for @ref CeedQFunctionView() output
1031*4c789ea2SJeremy L Thompson 
1032*4c789ea2SJeremy L Thompson   @param[in] qf       `CeedQFunction` to set the number of view tabs
1033*4c789ea2SJeremy L Thompson   @param[in] num_tabs Number of view tabs to set
1034*4c789ea2SJeremy L Thompson 
1035*4c789ea2SJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
1036*4c789ea2SJeremy L Thompson 
1037*4c789ea2SJeremy L Thompson   @ref User
1038*4c789ea2SJeremy L Thompson **/
1039*4c789ea2SJeremy L Thompson int CeedQFunctionSetNumViewTabs(CeedQFunction qf, CeedInt num_tabs) {
1040*4c789ea2SJeremy L Thompson   CeedCheck(num_tabs >= 0, CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR, "Number of view tabs must be non-negative");
1041*4c789ea2SJeremy L Thompson   qf->num_tabs = num_tabs;
1042*4c789ea2SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1043*4c789ea2SJeremy L Thompson }
1044*4c789ea2SJeremy L Thompson 
1045*4c789ea2SJeremy L Thompson /**
1046ca94c3ddSJeremy L Thompson   @brief View a `CeedQFunction`
104775affc3bSjeremylt 
1048ca94c3ddSJeremy L Thompson   @param[in] qf     `CeedQFunction` to view
1049ca94c3ddSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
105075affc3bSjeremylt 
105175affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
105275affc3bSjeremylt 
10537a982d89SJeremy L. Thompson   @ref User
105475affc3bSjeremylt **/
105575affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
1056*4c789ea2SJeremy L Thompson   char       *tabs = NULL;
1057d3d5610dSJeremy L Thompson   const char *name;
105875affc3bSjeremylt 
1059*4c789ea2SJeremy L Thompson   {
1060*4c789ea2SJeremy L Thompson     CeedInt num_tabs = 0;
1061*4c789ea2SJeremy L Thompson 
1062*4c789ea2SJeremy L Thompson     CeedCall(CeedQFunctionGetNumViewTabs(qf, &num_tabs));
1063*4c789ea2SJeremy L Thompson     CeedCall(CeedCalloc(CEED_TAB_WIDTH * num_tabs + 1, &tabs));
1064*4c789ea2SJeremy L Thompson     for (CeedInt i = 0; i < CEED_TAB_WIDTH * num_tabs; i++) tabs[i] = ' ';
1065*4c789ea2SJeremy L Thompson   }
1066*4c789ea2SJeremy L Thompson 
1067d3d5610dSJeremy L Thompson   CeedCall(CeedQFunctionGetName(qf, &name));
1068*4c789ea2SJeremy L Thompson   fprintf(stream, "%s%sCeedQFunction - %s\n", tabs, qf->is_gallery ? "Gallery " : "User ", name);
106975affc3bSjeremylt 
1070*4c789ea2SJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", tabs, qf->num_input_fields, qf->num_input_fields > 1 ? "s" : "");
1071d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
1072*4c789ea2SJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->input_fields[i], i, 1, tabs, stream));
107375affc3bSjeremylt   }
107475affc3bSjeremylt 
1075*4c789ea2SJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", tabs, qf->num_output_fields, qf->num_output_fields > 1 ? "s" : "");
1076d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
1077*4c789ea2SJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->output_fields[i], i, 0, tabs, stream));
107875affc3bSjeremylt   }
1079*4c789ea2SJeremy L Thompson   CeedCall(CeedFree(&tabs));
1080e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
108175affc3bSjeremylt }
108275affc3bSjeremylt 
108375affc3bSjeremylt /**
1084ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedQFunction`
1085b7c9bbdaSJeremy L Thompson 
1086ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction`
1087ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store`Ceed`
1088b7c9bbdaSJeremy L Thompson 
1089b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1090b7c9bbdaSJeremy L Thompson 
1091b7c9bbdaSJeremy L Thompson   @ref Advanced
1092b7c9bbdaSJeremy L Thompson **/
1093b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
10949bc66399SJeremy L Thompson   *ceed = NULL;
10959bc66399SJeremy L Thompson   CeedCall(CeedReferenceCopy(CeedQFunctionReturnCeed(qf), ceed));
1096b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1097b7c9bbdaSJeremy L Thompson }
1098b7c9bbdaSJeremy L Thompson 
1099b7c9bbdaSJeremy L Thompson /**
11006e536b99SJeremy L Thompson   @brief Return the `Ceed` associated with a `CeedQFunction`
11016e536b99SJeremy L Thompson 
11026e536b99SJeremy L Thompson   @param[in]  qf   `CeedQFunction`
11036e536b99SJeremy L Thompson 
11046e536b99SJeremy L Thompson   @return `Ceed` associated with the `qf`
11056e536b99SJeremy L Thompson 
11066e536b99SJeremy L Thompson   @ref Advanced
11076e536b99SJeremy L Thompson **/
11086e536b99SJeremy L Thompson Ceed CeedQFunctionReturnCeed(CeedQFunction qf) { return qf->ceed; }
11096e536b99SJeremy L Thompson 
11106e536b99SJeremy L Thompson /**
1111ca94c3ddSJeremy L Thompson   @brief Apply the action of a `CeedQFunction`
1112b11c1e72Sjeremylt 
1113ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedQFunction` as immutable.
1114f04ea552SJeremy L Thompson 
1115ca94c3ddSJeremy L Thompson   @param[in]  qf `CeedQFunction`
1116ea61e9acSJeremy L Thompson   @param[in]  Q  Number of quadrature points
1117ca94c3ddSJeremy L Thompson   @param[in]  u  Array of input `CeedVector`
1118ca94c3ddSJeremy L Thompson   @param[out] v  Array of output `CeedVector`
1119b11c1e72Sjeremylt 
1120b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1121dfdf5a53Sjeremylt 
11227a982d89SJeremy L. Thompson   @ref User
1123b11c1e72Sjeremylt **/
11242b730f8bSJeremy L Thompson int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, CeedVector *u, CeedVector *v) {
11251203703bSJeremy L Thompson   CeedInt vec_length;
11261203703bSJeremy L Thompson 
11279bc66399SJeremy L Thompson   CeedCheck(qf->Apply, CeedQFunctionReturnCeed(qf), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionApply");
11281203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetVectorLength(qf, &vec_length));
11299bc66399SJeremy L Thompson   CeedCheck(Q % vec_length == 0, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION,
11309bc66399SJeremy L Thompson             "Number of quadrature points %" CeedInt_FMT " must be a multiple of %" CeedInt_FMT, Q, qf->vec_length);
11311203703bSJeremy L Thompson   CeedCall(CeedQFunctionSetImmutable(qf));
11322b730f8bSJeremy L Thompson   CeedCall(qf->Apply(qf, Q, u, v));
1133e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1134d7b241e6Sjeremylt }
1135d7b241e6Sjeremylt 
1136b11c1e72Sjeremylt /**
1137ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedQFunction`
1138b11c1e72Sjeremylt 
1139ca94c3ddSJeremy L Thompson   @param[in,out] qf `CeedQFunction` to destroy
1140b11c1e72Sjeremylt 
1141b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1142dfdf5a53Sjeremylt 
11437a982d89SJeremy L. Thompson   @ref User
1144b11c1e72Sjeremylt **/
1145d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
1146ad6481ceSJeremy L Thompson   if (!*qf || --(*qf)->ref_count > 0) {
1147ad6481ceSJeremy L Thompson     *qf = NULL;
1148ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1149ad6481ceSJeremy L Thompson   }
1150fe2413ffSjeremylt   // Backend destroy
1151d7b241e6Sjeremylt   if ((*qf)->Destroy) {
11522b730f8bSJeremy L Thompson     CeedCall((*qf)->Destroy(*qf));
1153d7b241e6Sjeremylt   }
1154fe2413ffSjeremylt   // Free fields
115592ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_input_fields; i++) {
11562b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->input_fields[i]).field_name));
11572b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->input_fields[i]));
1158fe2413ffSjeremylt   }
115992ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_output_fields; i++) {
11602b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->output_fields[i]).field_name));
11612b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->output_fields[i]));
1162fe2413ffSjeremylt   }
11632b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->input_fields));
11642b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->output_fields));
1165777ff853SJeremy L Thompson 
1166777ff853SJeremy L Thompson   // User context data object
11672b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&(*qf)->ctx));
1168fe2413ffSjeremylt 
11692b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->user_source));
11702b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->source_path));
11712b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->gallery_name));
11722b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->kernel_name));
11732b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*qf)->ceed));
11742b730f8bSJeremy L Thompson   CeedCall(CeedFree(qf));
1175e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1176d7b241e6Sjeremylt }
1177d7b241e6Sjeremylt 
1178d7b241e6Sjeremylt /// @}
1179