xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision 5330800f8a87f3cc5848b1cc2c3c90a0eb696cee)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, 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 
277a982d89SJeremy L. Thompson // Indicate that no QFunction 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 /**
50288c0443SJeremy L Thompson   @brief Register a gallery QFunction
51288c0443SJeremy L Thompson 
52ea61e9acSJeremy L Thompson   @param[in]  name       Name for this backend to respond to
53ea61e9acSJeremy L Thompson   @param[in]  source     Absolute path to source of QFunction, "\path\CEED_DIR\gallery\folder\file.h:function_name"
54ea61e9acSJeremy L Thompson   @param[in]  vec_length Vector length.  Caller must ensure that number of quadrature points is a multiple of vec_length.
55ea61e9acSJeremy L Thompson   @param[in]  f          Function pointer to evaluate action at quadrature points.
56288c0443SJeremy L Thompson                            See \ref CeedQFunctionUser.
57ea61e9acSJeremy L Thompson   @param[in]  init       Initialization function called by CeedQFunctionInit() when the QFunction is selected.
58288c0443SJeremy L Thompson 
59288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
60288c0443SJeremy L Thompson 
617a982d89SJeremy L. Thompson   @ref Developer
62288c0443SJeremy L Thompson **/
632b730f8bSJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source, CeedInt vec_length, CeedQFunctionUser f,
64288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
652b730f8bSJeremy L Thompson   if (num_qfunctions >= sizeof(gallery_qfunctions) / sizeof(gallery_qfunctions[0])) {
66c042f62fSJeremy L Thompson     // LCOV_EXCL_START
67e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
68c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
692b730f8bSJeremy L Thompson   }
70c042f62fSJeremy L Thompson 
718ccf1006SJeremy L Thompson   CeedDebugEnv("Gallery Register: %s", name);
728ccf1006SJeremy L Thompson 
736eb0d8b4SJeremy L Thompson   const char *relative_file_path;
742b730f8bSJeremy L Thompson   CeedCall(CeedGetJitRelativePath(source, &relative_file_path));
756eb0d8b4SJeremy L Thompson 
76d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
77d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN - 1] = 0;
782b730f8bSJeremy L Thompson   strncpy(gallery_qfunctions[num_qfunctions].source, relative_file_path, CEED_MAX_RESOURCE_LEN);
79d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN - 1] = 0;
80d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].vec_length                        = vec_length;
81d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].f                                 = f;
82d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].init                              = init;
83288c0443SJeremy L Thompson   num_qfunctions++;
84e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
85288c0443SJeremy L Thompson }
86288c0443SJeremy L Thompson 
87288c0443SJeremy L Thompson /**
887a982d89SJeremy L. Thompson   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
897a982d89SJeremy L. Thompson 
90ea61e9acSJeremy L Thompson   @param[out] f           CeedQFunctionField
91ea61e9acSJeremy L Thompson   @param[in]  field_name  Name of QFunction field
92*5330800fSSebastian Grimberg   @param[in]  size        Size of QFunction field, (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_WEIGHT,
93*5330800fSSebastian Grimberg (num_comp * 1) for @ref CEED_EVAL_INTERP for an H^1 space or (num_comp * dim) for an H(div) or H(curl) space,
94*5330800fSSebastian Grimberg (num_comp * dim) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and
95*5330800fSSebastian Grimberg (num_comp * curl_dim) with curl_dim = 1 if dim < 3 else dim for @ref CEED_EVAL_CURL.
96ea61e9acSJeremy L Thompson   @param[in]  eval_mode   \ref CEED_EVAL_NONE to use values directly,
97*5330800fSSebastian Grimberg                             \ref CEED_EVAL_WEIGHT to use quadrature weights,
987a982d89SJeremy L. Thompson                             \ref CEED_EVAL_INTERP to use interpolated values,
997a982d89SJeremy L. Thompson                             \ref CEED_EVAL_GRAD to use gradients,
100*5330800fSSebastian Grimberg                             \ref CEED_EVAL_DIV to use divergence,
101*5330800fSSebastian Grimberg                             \ref CEED_EVAL_CURL to use curl.
1027a982d89SJeremy L. Thompson 
1037a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1047a982d89SJeremy L. Thompson 
1057a982d89SJeremy L. Thompson   @ref Developer
1067a982d89SJeremy L. Thompson **/
1072b730f8bSJeremy L Thompson static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
1082b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, f));
1092b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*f)->field_name));
1107a982d89SJeremy L. Thompson   (*f)->size      = size;
111d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
112e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1137a982d89SJeremy L. Thompson }
1147a982d89SJeremy L. Thompson 
1157a982d89SJeremy L. Thompson /**
1167a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1177a982d89SJeremy L. Thompson 
1187a982d89SJeremy L. Thompson   @param[in] field        QFunction field to view
119d1d35e2fSjeremylt   @param[in] field_number Number of field being viewed
1204c4400c7SValeria Barra   @param[in] in           true for input field, false for output
1217a982d89SJeremy 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 **/
1272b730f8bSJeremy L Thompson static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt field_number, bool in, FILE *stream) {
1287a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1298229195eSjeremylt   char       *field_name;
1302b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldGetName(field, &field_name));
1318229195eSjeremylt   CeedInt size;
1322b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldGetSize(field, &size));
1338229195eSjeremylt   CeedEvalMode eval_mode;
1342b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldGetEvalMode(field, &eval_mode));
1352b730f8bSJeremy L Thompson   fprintf(stream,
1362b730f8bSJeremy L Thompson           "    %s field %" CeedInt_FMT
1372b730f8bSJeremy L Thompson           ":\n"
1387a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1392b730f8bSJeremy L Thompson           "      Size: %" CeedInt_FMT
1402b730f8bSJeremy L Thompson           "\n"
1417a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1428229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
143e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1447a982d89SJeremy L. Thompson }
1457a982d89SJeremy L. Thompson 
146777ff853SJeremy L Thompson /**
147777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
148777ff853SJeremy L Thompson 
149ea61e9acSJeremy L Thompson   @param[in,out] qf     CeedQFunction
150ea61e9acSJeremy L Thompson   @param[in]     status Boolean value to set as Fortran status
151777ff853SJeremy L Thompson 
152777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
153777ff853SJeremy L Thompson 
154777ff853SJeremy L Thompson   @ref Backend
155777ff853SJeremy L Thompson **/
156777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
157f04ea552SJeremy L Thompson   qf->is_fortran = status;
158e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
159777ff853SJeremy L Thompson }
160777ff853SJeremy L Thompson 
1617a982d89SJeremy L. Thompson /// @}
1627a982d89SJeremy L. Thompson 
1637a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1647a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1657a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1667a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1677a982d89SJeremy L. Thompson /// @{
1687a982d89SJeremy L. Thompson 
1697a982d89SJeremy L. Thompson /**
1707a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1717a982d89SJeremy L. Thompson 
172ea61e9acSJeremy L Thompson   @param[in]  qf         CeedQFunction
173d1d35e2fSjeremylt   @param[out] vec_length Variable to store vector length
1747a982d89SJeremy L. Thompson 
1757a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1767a982d89SJeremy L. Thompson 
1777a982d89SJeremy L. Thompson   @ref Backend
1787a982d89SJeremy L. Thompson **/
179d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
180d1d35e2fSjeremylt   *vec_length = qf->vec_length;
181e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1827a982d89SJeremy L. Thompson }
1837a982d89SJeremy L. Thompson 
1847a982d89SJeremy L. Thompson /**
1857a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
1867a982d89SJeremy L. Thompson 
187ea61e9acSJeremy L Thompson   @param[in]  qf         CeedQFunction
188d1d35e2fSjeremylt   @param[out] num_input  Variable to store number of input fields
189d1d35e2fSjeremylt   @param[out] num_output Variable to store number of output fields
1907a982d89SJeremy L. Thompson 
1917a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1927a982d89SJeremy L. Thompson 
1937a982d89SJeremy L. Thompson   @ref Backend
1947a982d89SJeremy L. Thompson **/
1952b730f8bSJeremy L Thompson int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, CeedInt *num_output) {
196d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
197d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
198e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1997a982d89SJeremy L. Thompson }
2007a982d89SJeremy L. Thompson 
2017a982d89SJeremy L. Thompson /**
20243e1b16fSJeremy L Thompson   @brief Get the name of the user function for a CeedQFunction
2037a982d89SJeremy L. Thompson 
204ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
20543e1b16fSJeremy L Thompson   @param[out] kernel_name Variable to store source path string
2067a982d89SJeremy L. Thompson 
2077a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2087a982d89SJeremy L. Thompson 
2097a982d89SJeremy L. Thompson   @ref Backend
2107a982d89SJeremy L. Thompson **/
21143e1b16fSJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, char **kernel_name) {
212ca5eadf8SJeremy L Thompson   if (!qf->kernel_name) {
213ca5eadf8SJeremy L Thompson     Ceed  ceed;
214ca5eadf8SJeremy L Thompson     char *kernel_name_copy;
2152b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
216ca5eadf8SJeremy L Thompson 
217ca5eadf8SJeremy L Thompson     if (qf->user_source) {
218ca5eadf8SJeremy L Thompson       const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
219ca5eadf8SJeremy L Thompson       size_t      kernel_name_len = strlen(kernel_name);
220ca5eadf8SJeremy L Thompson 
2212b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(kernel_name_len + 1, &kernel_name_copy));
222ca5eadf8SJeremy L Thompson       memcpy(kernel_name_copy, kernel_name, kernel_name_len);
223ca5eadf8SJeremy L Thompson     } else {
2242b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &kernel_name_copy));
225ca5eadf8SJeremy L Thompson     }
226ca5eadf8SJeremy L Thompson     qf->kernel_name = kernel_name_copy;
227ca5eadf8SJeremy L Thompson   }
228ca5eadf8SJeremy L Thompson 
22943e1b16fSJeremy L Thompson   *kernel_name = (char *)qf->kernel_name;
23043e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
23143e1b16fSJeremy L Thompson }
23243e1b16fSJeremy L Thompson 
23343e1b16fSJeremy L Thompson /**
23443e1b16fSJeremy L Thompson   @brief Get the source path string for a CeedQFunction
23543e1b16fSJeremy L Thompson 
236ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
23743e1b16fSJeremy L Thompson   @param[out] source_path Variable to store source path string
23843e1b16fSJeremy L Thompson 
23943e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
24043e1b16fSJeremy L Thompson 
24143e1b16fSJeremy L Thompson   @ref Backend
24243e1b16fSJeremy L Thompson **/
24343e1b16fSJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) {
244ca5eadf8SJeremy L Thompson   if (!qf->source_path && qf->user_source) {
245ca5eadf8SJeremy L Thompson     Ceed        ceed;
246ca5eadf8SJeremy L Thompson     bool        is_absolute_path;
247ca5eadf8SJeremy L Thompson     char       *absolute_path, *source_path_copy;
248ca5eadf8SJeremy L Thompson     const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
249ca5eadf8SJeremy L Thompson     size_t      kernel_name_len = strlen(kernel_name);
250ca5eadf8SJeremy L Thompson 
2512b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
252ca5eadf8SJeremy L Thompson 
2532b730f8bSJeremy L Thompson     CeedCall(CeedCheckFilePath(ceed, qf->user_source, &is_absolute_path));
254ca5eadf8SJeremy L Thompson     if (is_absolute_path) {
255ca5eadf8SJeremy L Thompson       absolute_path = (char *)qf->user_source;
256ca5eadf8SJeremy L Thompson     } else {
2572b730f8bSJeremy L Thompson       CeedCall(CeedGetJitAbsolutePath(ceed, qf->user_source, &absolute_path));
258ca5eadf8SJeremy L Thompson     }
259ca5eadf8SJeremy L Thompson 
260ca5eadf8SJeremy L Thompson     size_t source_len = strlen(absolute_path) - kernel_name_len - 1;
2612b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(source_len + 1, &source_path_copy));
262ca5eadf8SJeremy L Thompson     memcpy(source_path_copy, absolute_path, source_len);
263ca5eadf8SJeremy L Thompson     qf->source_path = source_path_copy;
264ca5eadf8SJeremy L Thompson 
2652b730f8bSJeremy L Thompson     if (!is_absolute_path) CeedCall(CeedFree(&absolute_path));
266ca5eadf8SJeremy L Thompson   }
267ca5eadf8SJeremy L Thompson 
26843e1b16fSJeremy L Thompson   *source_path = (char *)qf->source_path;
269e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2707a982d89SJeremy L. Thompson }
2717a982d89SJeremy L. Thompson 
2727a982d89SJeremy L. Thompson /**
273ea61e9acSJeremy L Thompson   @brief Initialize and load QFunction source file into string buffer, including full text of local files in place of `#include "local.h"`.
2743d3250a0SJeremy L Thompson            The `buffer` is set to `NULL` if there is no QFunction source file.
2753d3250a0SJeremy L Thompson          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
2763d3250a0SJeremy L Thompson 
277ea61e9acSJeremy L Thompson   @param[in]  qf            CeedQFunction
278f74ec584SJeremy L Thompson   @param[out] source_buffer String buffer for source file contents
2793d3250a0SJeremy L Thompson 
2803d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2813d3250a0SJeremy L Thompson 
2823d3250a0SJeremy L Thompson   @ref Backend
2833d3250a0SJeremy L Thompson **/
2843d3250a0SJeremy L Thompson int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, char **source_buffer) {
2853d3250a0SJeremy L Thompson   char *source_path;
2863d3250a0SJeremy L Thompson 
2872b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetSourcePath(qf, &source_path));
2883d3250a0SJeremy L Thompson   *source_buffer = NULL;
2893d3250a0SJeremy L Thompson   if (source_path) {
2902b730f8bSJeremy L Thompson     CeedCall(CeedLoadSourceToBuffer(qf->ceed, source_path, source_buffer));
2913d3250a0SJeremy L Thompson   }
2923d3250a0SJeremy L Thompson 
2933d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2943d3250a0SJeremy L Thompson }
2953d3250a0SJeremy L Thompson 
2963d3250a0SJeremy L Thompson /**
2977a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2987a982d89SJeremy L. Thompson 
299ea61e9acSJeremy L Thompson   @param[in]  qf CeedQFunction
3007a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
3017a982d89SJeremy L. Thompson 
3027a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3037a982d89SJeremy L. Thompson 
3047a982d89SJeremy L. Thompson   @ref Backend
3057a982d89SJeremy L. Thompson **/
3067a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
3077a982d89SJeremy L. Thompson   *f = qf->function;
308e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3097a982d89SJeremy L. Thompson }
3107a982d89SJeremy L. Thompson 
3117a982d89SJeremy L. Thompson /**
312777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
313ea61e9acSJeremy L Thompson            Note: For QFunctions from the Fortran interface, this function will return the Fortran context CeedQFunctionContext.
3147a982d89SJeremy L. Thompson 
315ea61e9acSJeremy L Thompson   @param[in]  qf  CeedQFunction
316777ff853SJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
3177a982d89SJeremy L. Thompson 
3187a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3197a982d89SJeremy L. Thompson 
3207a982d89SJeremy L. Thompson   @ref Backend
3217a982d89SJeremy L. Thompson **/
322777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
3237a982d89SJeremy L. Thompson   *ctx = qf->ctx;
324e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3257a982d89SJeremy L. Thompson }
3267a982d89SJeremy L. Thompson 
3277a982d89SJeremy L. Thompson /**
328441428dfSJeremy L Thompson   @brief Get context data of a CeedQFunction
329441428dfSJeremy L Thompson 
330ea61e9acSJeremy L Thompson   @param[in]  qf       CeedQFunction
331ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
332ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
333441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
334441428dfSJeremy L Thompson 
335441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
336441428dfSJeremy L Thompson 
337441428dfSJeremy L Thompson   @ref Backend
338441428dfSJeremy L Thompson **/
3392b730f8bSJeremy L Thompson int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
340441428dfSJeremy L Thompson   bool                 is_writable;
341441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
342441428dfSJeremy L Thompson 
3432b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
344441428dfSJeremy L Thompson   if (ctx) {
3452b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
346441428dfSJeremy L Thompson     if (is_writable) {
3472b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
348441428dfSJeremy L Thompson     } else {
3492b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
350441428dfSJeremy L Thompson     }
351441428dfSJeremy L Thompson   } else {
352441428dfSJeremy L Thompson     *(void **)data = NULL;
353441428dfSJeremy L Thompson   }
354441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
355441428dfSJeremy L Thompson }
356441428dfSJeremy L Thompson 
357441428dfSJeremy L Thompson /**
358441428dfSJeremy L Thompson   @brief Restore context data of a CeedQFunction
359441428dfSJeremy L Thompson 
360ea61e9acSJeremy L Thompson   @param[in]     qf   CeedQFunction
361ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
362441428dfSJeremy L Thompson 
363441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
364441428dfSJeremy L Thompson 
365441428dfSJeremy L Thompson   @ref Backend
366441428dfSJeremy L Thompson **/
367441428dfSJeremy L Thompson int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) {
368441428dfSJeremy L Thompson   bool                 is_writable;
369441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
370441428dfSJeremy L Thompson 
3712b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
372441428dfSJeremy L Thompson   if (ctx) {
3732b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
374441428dfSJeremy L Thompson     if (is_writable) {
3752b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
376441428dfSJeremy L Thompson     } else {
3772b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
378441428dfSJeremy L Thompson     }
379441428dfSJeremy L Thompson   }
380441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
381441428dfSJeremy L Thompson }
382441428dfSJeremy L Thompson 
383441428dfSJeremy L Thompson /**
3847a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
385ea61e9acSJeremy L Thompson            Note: For all QFunctions this function will return the user CeedQFunctionContext and not interface context CeedQFunctionContext, if any
386ea61e9acSJeremy L Thompson such object exists.
3877a982d89SJeremy L. Thompson 
388ea61e9acSJeremy L Thompson   @param[in]  qf  CeedQFunction
389777ff853SJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
3907a982d89SJeremy L. Thompson 
3917a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3927a982d89SJeremy L. Thompson   @ref Backend
3937a982d89SJeremy L. Thompson **/
394777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
395f04ea552SJeremy L Thompson   if (qf->is_fortran) {
396d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
3972b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx));
3988cb0412aSJeremy L Thompson     *ctx = fortran_ctx->inner_ctx;
3992b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx));
4007a982d89SJeremy L. Thompson   } else {
4017a982d89SJeremy L. Thompson     *ctx = qf->ctx;
4027a982d89SJeremy L. Thompson   }
403e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4047a982d89SJeremy L. Thompson }
4057a982d89SJeremy L. Thompson 
4067a982d89SJeremy L. Thompson /**
407441428dfSJeremy L Thompson   @brief Get inner context data of a CeedQFunction
408441428dfSJeremy L Thompson 
409ea61e9acSJeremy L Thompson   @param[in]  qf       CeedQFunction
410ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
411ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
412441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
413441428dfSJeremy L Thompson 
414441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
415441428dfSJeremy L Thompson 
416441428dfSJeremy L Thompson   @ref Backend
417441428dfSJeremy L Thompson **/
4182b730f8bSJeremy L Thompson int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
419441428dfSJeremy L Thompson   bool                 is_writable;
420441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
421441428dfSJeremy L Thompson 
4222b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
423441428dfSJeremy L Thompson   if (ctx) {
4242b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
425441428dfSJeremy L Thompson     if (is_writable) {
4262b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
427441428dfSJeremy L Thompson     } else {
4282b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
429441428dfSJeremy L Thompson     }
430441428dfSJeremy L Thompson   } else {
431441428dfSJeremy L Thompson     *(void **)data = NULL;
432441428dfSJeremy L Thompson   }
433441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
434441428dfSJeremy L Thompson }
435441428dfSJeremy L Thompson 
436441428dfSJeremy L Thompson /**
437441428dfSJeremy L Thompson   @brief Restore inner context data of a CeedQFunction
438441428dfSJeremy L Thompson 
439ea61e9acSJeremy L Thompson   @param[in]     qf   CeedQFunction
440ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
441441428dfSJeremy L Thompson 
442441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
443441428dfSJeremy L Thompson 
444441428dfSJeremy L Thompson   @ref Backend
445441428dfSJeremy L Thompson **/
446441428dfSJeremy L Thompson int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) {
447441428dfSJeremy L Thompson   bool                 is_writable;
448441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
449441428dfSJeremy L Thompson 
4502b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
451441428dfSJeremy L Thompson   if (ctx) {
4522b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
453441428dfSJeremy L Thompson     if (is_writable) {
4542b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
455441428dfSJeremy L Thompson     } else {
4562b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
457441428dfSJeremy L Thompson     }
458441428dfSJeremy L Thompson   }
459441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
460441428dfSJeremy L Thompson }
461441428dfSJeremy L Thompson 
462441428dfSJeremy L Thompson /**
4637a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
4647a982d89SJeremy L. Thompson 
465ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
466d1d35e2fSjeremylt   @param[out] is_identity Variable to store identity status
4677a982d89SJeremy L. Thompson 
4687a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4697a982d89SJeremy L. Thompson 
4707a982d89SJeremy L. Thompson   @ref Backend
4717a982d89SJeremy L. Thompson **/
472d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
473f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
474e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4757a982d89SJeremy L. Thompson }
4767a982d89SJeremy L. Thompson 
4777a982d89SJeremy L. Thompson /**
478441428dfSJeremy L Thompson   @brief Determine if QFunctionContext is writable
479441428dfSJeremy L Thompson 
480ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
481ea61e9acSJeremy L Thompson   @param[out] is_writable Variable to store context writeable status
482441428dfSJeremy L Thompson 
483441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
484441428dfSJeremy L Thompson 
485441428dfSJeremy L Thompson   @ref Backend
486441428dfSJeremy L Thompson **/
487441428dfSJeremy L Thompson int CeedQFunctionIsContextWritable(CeedQFunction qf, bool *is_writable) {
488441428dfSJeremy L Thompson   *is_writable = qf->is_context_writable;
489441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
490441428dfSJeremy L Thompson }
491441428dfSJeremy L Thompson 
492441428dfSJeremy L Thompson /**
4937a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
4947a982d89SJeremy L. Thompson 
495ea61e9acSJeremy L Thompson   @param[in]  qf   CeedQFunction
4967a982d89SJeremy L. Thompson   @param[out] data Variable to store data
4977a982d89SJeremy L. Thompson 
4987a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4997a982d89SJeremy L. Thompson 
5007a982d89SJeremy L. Thompson   @ref Backend
5017a982d89SJeremy L. Thompson **/
502777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
503777ff853SJeremy L Thompson   *(void **)data = qf->data;
504e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5057a982d89SJeremy L. Thompson }
5067a982d89SJeremy L. Thompson 
5077a982d89SJeremy L. Thompson /**
5087a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
5097a982d89SJeremy L. Thompson 
510ea61e9acSJeremy L Thompson   @param[in,out] qf   CeedQFunction
511ea61e9acSJeremy L Thompson   @param[in]     data Data to set
5127a982d89SJeremy L. Thompson 
5137a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5147a982d89SJeremy L. Thompson 
5157a982d89SJeremy L. Thompson   @ref Backend
5167a982d89SJeremy L. Thompson **/
517777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
518777ff853SJeremy L Thompson   qf->data = data;
519e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5207a982d89SJeremy L. Thompson }
5217a982d89SJeremy L. Thompson 
5227a982d89SJeremy L. Thompson /**
52334359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
52434359f16Sjeremylt 
525ea61e9acSJeremy L Thompson   @param[in,out] qf CeedQFunction to increment the reference counter
52634359f16Sjeremylt 
52734359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
52834359f16Sjeremylt 
52934359f16Sjeremylt   @ref Backend
53034359f16Sjeremylt **/
5319560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
53234359f16Sjeremylt   qf->ref_count++;
53334359f16Sjeremylt   return CEED_ERROR_SUCCESS;
53434359f16Sjeremylt }
53534359f16Sjeremylt 
5366e15d496SJeremy L Thompson /**
5376e15d496SJeremy L Thompson   @brief Estimate number of FLOPs per quadrature required to apply QFunction
5386e15d496SJeremy L Thompson 
539ea61e9acSJeremy L Thompson   @param[in]  qf    QFunction to estimate FLOPs for
540ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
5416e15d496SJeremy L Thompson 
5426e15d496SJeremy L Thompson   @ref Backend
5436e15d496SJeremy L Thompson **/
5449d36ca50SJeremy L Thompson int CeedQFunctionGetFlopsEstimate(CeedQFunction qf, CeedSize *flops) {
5452b730f8bSJeremy L Thompson   if (qf->user_flop_estimate == -1) {
5466e15d496SJeremy L Thompson     // LCOV_EXCL_START
5472b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_INCOMPLETE, "Must set FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
5486e15d496SJeremy L Thompson     // LCOV_EXCL_STOP
5492b730f8bSJeremy L Thompson   }
5506e15d496SJeremy L Thompson   *flops = qf->user_flop_estimate;
5516e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5526e15d496SJeremy L Thompson }
5536e15d496SJeremy L Thompson 
5547a982d89SJeremy L. Thompson /// @}
5557a982d89SJeremy L. Thompson 
5567a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5577a982d89SJeremy L. Thompson /// CeedQFunction Public API
5587a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5597a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
5607a982d89SJeremy L. Thompson /// @{
5617a982d89SJeremy L. Thompson 
5627a982d89SJeremy L. Thompson /**
5637a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
5647a982d89SJeremy L. Thompson 
565ea61e9acSJeremy L Thompson   @param[in]  ceed       Ceed object where the CeedQFunction will be created
566ea61e9acSJeremy L Thompson   @param[in]  vec_length Vector length. Caller must ensure that number of quadrature points is a multiple of vec_length.
567ea61e9acSJeremy L Thompson   @param[in]  f          Function pointer to evaluate action at quadrature points.
5687a982d89SJeremy L. Thompson                            See \ref CeedQFunctionUser.
569ea61e9acSJeremy L Thompson   @param[in]  source     Absolute path to source of QFunction, "\abs_path\file.h:function_name".
570ea61e9acSJeremy L Thompson                            For support across all backends, this source must only contain constructs supported by C99, C++11, and CUDA.
571ea61e9acSJeremy L Thompson   @param[out] qf         Address of the variable where the newly created CeedQFunction will be stored
5727a982d89SJeremy L. Thompson 
5737a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5747a982d89SJeremy L. Thompson 
575ea61e9acSJeremy L Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's arguments.
5767a982d89SJeremy L. Thompson 
5777a982d89SJeremy L. Thompson   @ref User
5787a982d89SJeremy L. Thompson **/
5792b730f8bSJeremy L Thompson int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length, CeedQFunctionUser f, const char *source, CeedQFunction *qf) {
580ca5eadf8SJeremy L Thompson   char *user_source_copy;
5817a982d89SJeremy L. Thompson 
5827a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
5837a982d89SJeremy L. Thompson     Ceed delegate;
5842b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "QFunction"));
5857a982d89SJeremy L. Thompson 
5862b730f8bSJeremy L Thompson     if (!delegate) {
5877a982d89SJeremy L. Thompson       // LCOV_EXCL_START
5882b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support QFunctionCreate");
5897a982d89SJeremy L. Thompson       // LCOV_EXCL_STOP
5902b730f8bSJeremy L Thompson     }
5917a982d89SJeremy L. Thompson 
5922b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf));
593e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
5947a982d89SJeremy L. Thompson   }
5957a982d89SJeremy L. Thompson 
5962b730f8bSJeremy L Thompson   if (strlen(source) && !strrchr(source, ':')) {
59743e1b16fSJeremy L Thompson     // LCOV_EXCL_START
59843e1b16fSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPLETE,
5992b730f8bSJeremy L Thompson                      "Provided path to source does not include function name. Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"",
60043e1b16fSJeremy L Thompson                      source);
60143e1b16fSJeremy L Thompson     // LCOV_EXCL_STOP
6022b730f8bSJeremy L Thompson   }
60343e1b16fSJeremy L Thompson 
6042b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, qf));
6057a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
6062b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
607d1d35e2fSjeremylt   (*qf)->ref_count           = 1;
608d1d35e2fSjeremylt   (*qf)->vec_length          = vec_length;
609f04ea552SJeremy L Thompson   (*qf)->is_identity         = false;
610441428dfSJeremy L Thompson   (*qf)->is_context_writable = true;
6117a982d89SJeremy L. Thompson   (*qf)->function            = f;
6126e15d496SJeremy L Thompson   (*qf)->user_flop_estimate  = -1;
61343e1b16fSJeremy L Thompson   if (strlen(source)) {
614ca5eadf8SJeremy L Thompson     size_t user_source_len = strlen(source);
615ee5a26f2SJeremy L Thompson 
6162b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(user_source_len + 1, &user_source_copy));
617ca5eadf8SJeremy L Thompson     memcpy(user_source_copy, source, user_source_len);
618ca5eadf8SJeremy L Thompson     (*qf)->user_source = user_source_copy;
61943e1b16fSJeremy L Thompson   }
6202b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields));
6212b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields));
6222b730f8bSJeremy L Thompson   CeedCall(ceed->QFunctionCreate(*qf));
623e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6247a982d89SJeremy L. Thompson }
6257a982d89SJeremy L. Thompson 
6267a982d89SJeremy L. Thompson /**
627288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
628288c0443SJeremy L Thompson 
629ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedQFunction will be created
630ea61e9acSJeremy L Thompson   @param[in]  name Name of QFunction to use from gallery
631ea61e9acSJeremy L Thompson   @param[out] qf   Address of the variable where the newly created CeedQFunction will be stored
632288c0443SJeremy L Thompson 
633288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
634288c0443SJeremy L Thompson 
6357a982d89SJeremy L. Thompson   @ref User
636288c0443SJeremy L Thompson **/
6372b730f8bSJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, CeedQFunction *qf) {
638f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
639288c0443SJeremy L Thompson 
6402b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionRegisterAll());
641288c0443SJeremy L Thompson   // Find matching backend
6422b730f8bSJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE, "No QFunction name provided");
643288c0443SJeremy L Thompson   for (size_t i = 0; i < num_qfunctions; i++) {
644288c0443SJeremy L Thompson     size_t      n;
645d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
6462b730f8bSJeremy L Thompson     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {
6472b730f8bSJeremy L Thompson     }
648d1d35e2fSjeremylt     if (n > match_len) {
649d1d35e2fSjeremylt       match_len   = n;
650f7e22acaSJeremy L Thompson       match_index = i;
651288c0443SJeremy L Thompson     }
652288c0443SJeremy L Thompson   }
6532b730f8bSJeremy L Thompson   if (!match_len) {
654138d4072Sjeremylt     // LCOV_EXCL_START
655e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
656138d4072Sjeremylt     // LCOV_EXCL_STOP
6572b730f8bSJeremy L Thompson   }
658288c0443SJeremy L Thompson 
659288c0443SJeremy L Thompson   // Create QFunction
6602b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInterior(ceed, gallery_qfunctions[match_index].vec_length, gallery_qfunctions[match_index].f,
6612b730f8bSJeremy L Thompson                                        gallery_qfunctions[match_index].source, qf));
662288c0443SJeremy L Thompson 
663288c0443SJeremy L Thompson   // QFunction specific setup
6642b730f8bSJeremy L Thompson   CeedCall(gallery_qfunctions[match_index].init(ceed, name, *qf));
665288c0443SJeremy L Thompson 
66675affc3bSjeremylt   // Copy name
6672b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name));
66843e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
669e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
670288c0443SJeremy L Thompson }
671288c0443SJeremy L Thompson 
672288c0443SJeremy L Thompson /**
673ea61e9acSJeremy L Thompson   @brief Create an identity CeedQFunction.
674ea61e9acSJeremy L Thompson            Inputs are written into outputs in the order given.
675859c15bbSJames Wright            This is useful for CeedOperators that can be represented with only the action of a CeedElemRestriction and CeedBasis, such as restriction
676859c15bbSJames Wright and prolongation operators for p-multigrid. Backends may optimize CeedOperators with this CeedQFunction to avoid the copy of input data to output
677859c15bbSJames Wright fields by using the same memory location for both.
6780219ea01SJeremy L Thompson 
679ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed object where the CeedQFunction will be created
680d1d35e2fSjeremylt   @param[in]  size     Size of the QFunction fields
681d1d35e2fSjeremylt   @param[in]  in_mode  CeedEvalMode for input to CeedQFunction
682d1d35e2fSjeremylt   @param[in]  out_mode CeedEvalMode for output to CeedQFunction
683ea61e9acSJeremy L Thompson   @param[out] qf       Address of the variable where the newly created CeedQFunction will be stored
6840219ea01SJeremy L Thompson 
6850219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
6860219ea01SJeremy L Thompson 
6877a982d89SJeremy L. Thompson   @ref User
6880219ea01SJeremy L Thompson **/
6892b730f8bSJeremy L Thompson int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, CeedEvalMode out_mode, CeedQFunction *qf) {
6902b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Identity", qf));
6912b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(*qf, "input", size, in_mode));
6922b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(*qf, "output", size, out_mode));
6930219ea01SJeremy L Thompson 
694f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
695547dbd6fSJeremy L Thompson 
696777ff853SJeremy L Thompson   CeedQFunctionContext  ctx;
6973668ca4bSJeremy L Thompson   CeedContextFieldLabel size_label;
6982b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(*qf, &ctx));
6992b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label));
7002b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetInt32(ctx, size_label, &size));
701547dbd6fSJeremy L Thompson 
702e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7030219ea01SJeremy L Thompson }
7040219ea01SJeremy L Thompson 
7050219ea01SJeremy L Thompson /**
706ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedQFunction.
707ea61e9acSJeremy L Thompson            Both pointers should be destroyed with `CeedQFunctionDestroy()`.
708512bb800SJeremy L Thompson 
709512bb800SJeremy 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.
710ea61e9acSJeremy L Thompson              This CeedQFunction will be destroyed if `*qf_copy` is the only reference to this CeedQFunction.
7119560d06aSjeremylt 
712ea61e9acSJeremy L Thompson   @param[in]  qf      CeedQFunction to copy reference to
7139560d06aSjeremylt   @param[out] qf_copy Variable to store copied reference
7149560d06aSjeremylt 
7159560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7169560d06aSjeremylt 
7179560d06aSjeremylt   @ref User
7189560d06aSjeremylt **/
7199560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
7202b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionReference(qf));
7212b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(qf_copy));
7229560d06aSjeremylt   *qf_copy = qf;
7239560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7249560d06aSjeremylt }
7259560d06aSjeremylt 
7269560d06aSjeremylt /**
727a0a97fcfSJed Brown   @brief Add a CeedQFunction input
728b11c1e72Sjeremylt 
729ea61e9acSJeremy L Thompson   @param[in,out] qf         CeedQFunction
730ea61e9acSJeremy L Thompson   @param[in]     field_name Name of QFunction field
731*5330800fSSebastian Grimberg   @param[in]     size       Size of QFunction field, (num_comp * 1) for @ref CEED_EVAL_NONE,
732*5330800fSSebastian Grimberg (num_comp * 1) for @ref CEED_EVAL_INTERP for an H^1 space or (num_comp * dim) for an H(div) or H(curl) space,
733*5330800fSSebastian Grimberg (num_comp * dim) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and
734*5330800fSSebastian Grimberg (num_comp * curl_dim) with curl_dim = 1 if dim < 3 else dim for @ref CEED_EVAL_CURL.
735ea61e9acSJeremy L Thompson   @param[in]     eval_mode  \ref CEED_EVAL_NONE to use values directly,
736b11c1e72Sjeremylt                               \ref CEED_EVAL_INTERP to use interpolated values,
737*5330800fSSebastian Grimberg                               \ref CEED_EVAL_GRAD to use gradients,
738*5330800fSSebastian Grimberg                               \ref CEED_EVAL_DIV to use divergence,
739*5330800fSSebastian Grimberg                               \ref CEED_EVAL_CURL to use curl.
740b11c1e72Sjeremylt 
741b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
742dfdf5a53Sjeremylt 
7437a982d89SJeremy L. Thompson   @ref User
744b11c1e72Sjeremylt **/
7452b730f8bSJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
7462b730f8bSJeremy L Thompson   if (qf->is_immutable) {
747e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
7482b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
749e15f9bd0SJeremy L Thompson     // LCOV_EXCL_STOP
7502b730f8bSJeremy L Thompson   }
7512b730f8bSJeremy L Thompson   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1)) {
752e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
7532b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION, "CEED_EVAL_WEIGHT should have size 1");
754e15f9bd0SJeremy L Thompson     // LCOV_EXCL_STOP
7552b730f8bSJeremy L Thompson   }
756643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
757643fbb69SJeremy L Thompson     if (!strcmp(field_name, qf->input_fields[i]->field_name)) {
758643fbb69SJeremy L Thompson       // LCOV_EXCL_START
759643fbb69SJeremy L Thompson       return CeedError(qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
760643fbb69SJeremy L Thompson       // LCOV_EXCL_STOP
761643fbb69SJeremy L Thompson     }
762643fbb69SJeremy L Thompson   }
763643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
764643fbb69SJeremy L Thompson     if (!strcmp(field_name, qf->output_fields[i]->field_name)) {
765643fbb69SJeremy L Thompson       // LCOV_EXCL_START
766643fbb69SJeremy L Thompson       return CeedError(qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
767643fbb69SJeremy L Thompson       // LCOV_EXCL_STOP
768643fbb69SJeremy L Thompson     }
769643fbb69SJeremy L Thompson   }
7702b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], field_name, size, eval_mode));
771d1d35e2fSjeremylt   qf->num_input_fields++;
772e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
773d7b241e6Sjeremylt }
774d7b241e6Sjeremylt 
775b11c1e72Sjeremylt /**
776a0a97fcfSJed Brown   @brief Add a CeedQFunction output
777b11c1e72Sjeremylt 
778ea61e9acSJeremy L Thompson   @param[in,out] qf         CeedQFunction
779ea61e9acSJeremy L Thompson   @param[in]     field_name Name of QFunction field
780*5330800fSSebastian Grimberg   @param[in]     size       Size of QFunction field, (num_comp * 1) for @ref CEED_EVAL_NONE,
781*5330800fSSebastian Grimberg (num_comp * 1) for @ref CEED_EVAL_INTERP for an H^1 space or (num_comp * dim) for an H(div) or H(curl) space,
782*5330800fSSebastian Grimberg (num_comp * dim) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and
783*5330800fSSebastian Grimberg (num_comp * curl_dim) with curl_dim = 1 if dim < 3 else dim for @ref CEED_EVAL_CURL.
784ea61e9acSJeremy L Thompson   @param[in]     eval_mode  \ref CEED_EVAL_NONE to use values directly,
785b11c1e72Sjeremylt                               \ref CEED_EVAL_INTERP to use interpolated values,
786*5330800fSSebastian Grimberg                               \ref CEED_EVAL_GRAD to use gradients,
787*5330800fSSebastian Grimberg                               \ref CEED_EVAL_DIV to use divergence,
788*5330800fSSebastian Grimberg                               \ref CEED_EVAL_CURL to use curl.
789b11c1e72Sjeremylt 
790b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
791dfdf5a53Sjeremylt 
7927a982d89SJeremy L. Thompson   @ref User
793b11c1e72Sjeremylt **/
7942b730f8bSJeremy L Thompson int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
7952b730f8bSJeremy L Thompson   if (qf->is_immutable) {
796e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
7972b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
798e15f9bd0SJeremy L Thompson     // LCOV_EXCL_STOP
7992b730f8bSJeremy L Thompson   }
8002b730f8bSJeremy L Thompson   if (eval_mode == CEED_EVAL_WEIGHT) {
801c042f62fSJeremy L Thompson     // LCOV_EXCL_START
8022b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION, "Cannot create QFunction output with CEED_EVAL_WEIGHT");
803c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
8042b730f8bSJeremy L Thompson   }
805643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
806643fbb69SJeremy L Thompson     if (!strcmp(field_name, qf->input_fields[i]->field_name)) {
807643fbb69SJeremy L Thompson       // LCOV_EXCL_START
808643fbb69SJeremy L Thompson       return CeedError(qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
809643fbb69SJeremy L Thompson       // LCOV_EXCL_STOP
810643fbb69SJeremy L Thompson     }
811643fbb69SJeremy L Thompson   }
812643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
813643fbb69SJeremy L Thompson     if (!strcmp(field_name, qf->output_fields[i]->field_name)) {
814643fbb69SJeremy L Thompson       // LCOV_EXCL_START
815643fbb69SJeremy L Thompson       return CeedError(qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
816643fbb69SJeremy L Thompson       // LCOV_EXCL_STOP
817643fbb69SJeremy L Thompson     }
818643fbb69SJeremy L Thompson   }
8192b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], field_name, size, eval_mode));
820d1d35e2fSjeremylt   qf->num_output_fields++;
821e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
822d7b241e6Sjeremylt }
823d7b241e6Sjeremylt 
824dfdf5a53Sjeremylt /**
82543bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
82643bbe138SJeremy L Thompson 
827ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedQFunction as immutable.
828f04ea552SJeremy L Thompson 
829ea61e9acSJeremy L Thompson   @param[in]  qf                CeedQFunction
830f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
831f74ec584SJeremy L Thompson   @param[out] input_fields      Variable to store input fields
832f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
833f74ec584SJeremy L Thompson   @param[out] output_fields     Variable to store output fields
83443bbe138SJeremy L Thompson 
83543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
83643bbe138SJeremy L Thompson 
837e9b533fbSJeremy L Thompson   @ref Advanced
83843bbe138SJeremy L Thompson **/
8392b730f8bSJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields, CeedQFunctionField **input_fields, CeedInt *num_output_fields,
84043bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
841f04ea552SJeremy L Thompson   qf->is_immutable = true;
84243bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
84343bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
84443bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
84543bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
84643bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
84743bbe138SJeremy L Thompson }
84843bbe138SJeremy L Thompson 
84943bbe138SJeremy L Thompson /**
85043bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
85143bbe138SJeremy L Thompson 
852ea61e9acSJeremy L Thompson   @param[in]  qf_field   CeedQFunctionField
85343bbe138SJeremy L Thompson   @param[out] field_name Variable to store the field name
85443bbe138SJeremy L Thompson 
85543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
85643bbe138SJeremy L Thompson 
857e9b533fbSJeremy L Thompson   @ref Advanced
85843bbe138SJeremy L Thompson **/
85943bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
86043bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
86143bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
86243bbe138SJeremy L Thompson }
86343bbe138SJeremy L Thompson 
86443bbe138SJeremy L Thompson /**
86543bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
86643bbe138SJeremy L Thompson 
867ea61e9acSJeremy L Thompson   @param[in]  qf_field CeedQFunctionField
86843bbe138SJeremy L Thompson   @param[out] size     Variable to store the size of the field
86943bbe138SJeremy L Thompson 
87043bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
87143bbe138SJeremy L Thompson 
872e9b533fbSJeremy L Thompson   @ref Advanced
87343bbe138SJeremy L Thompson **/
87443bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
87543bbe138SJeremy L Thompson   *size = qf_field->size;
87643bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
87743bbe138SJeremy L Thompson }
87843bbe138SJeremy L Thompson 
87943bbe138SJeremy L Thompson /**
88043bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
88143bbe138SJeremy L Thompson 
882ea61e9acSJeremy L Thompson   @param[in]  qf_field  CeedQFunctionField
88343bbe138SJeremy L Thompson   @param[out] eval_mode Variable to store the field evaluation mode
88443bbe138SJeremy L Thompson 
88543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
88643bbe138SJeremy L Thompson 
887e9b533fbSJeremy L Thompson   @ref Advanced
88843bbe138SJeremy L Thompson **/
8892b730f8bSJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, CeedEvalMode *eval_mode) {
89043bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
89143bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
89243bbe138SJeremy L Thompson }
89343bbe138SJeremy L Thompson 
89443bbe138SJeremy L Thompson /**
8954ce2993fSjeremylt   @brief Set global context for a CeedQFunction
896b11c1e72Sjeremylt 
897ea61e9acSJeremy L Thompson   @param[in,out] qf  CeedQFunction
898ea61e9acSJeremy L Thompson   @param[in]     ctx Context data to set
899b11c1e72Sjeremylt 
900b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
901dfdf5a53Sjeremylt 
9027a982d89SJeremy L. Thompson   @ref User
903b11c1e72Sjeremylt **/
904777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
9052b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&qf->ctx));
906d7b241e6Sjeremylt   qf->ctx = ctx;
9079e77b9c8SJeremy L Thompson   if (ctx) {
9082b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextReference(ctx));
9099e77b9c8SJeremy L Thompson   }
910e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
911d7b241e6Sjeremylt }
912d7b241e6Sjeremylt 
913b11c1e72Sjeremylt /**
914441428dfSJeremy L Thompson   @brief Set writability of CeedQFunctionContext when calling the `CeedQFunctionUser`.
915859c15bbSJames Wright            The default value is `is_writable == true`.
916441428dfSJeremy L Thompson 
917ea61e9acSJeremy L Thompson            Setting `is_writable == true` indicates the `CeedQFunctionUser` writes into the CeedQFunctionContextData and requires memory syncronization
918441428dfSJeremy L Thompson after calling `CeedQFunctionApply()`.
919441428dfSJeremy L Thompson 
920ea61e9acSJeremy L Thompson            Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not modify the CeedQFunctionContextData.
921ea61e9acSJeremy L Thompson            Violating this assertion may lead to inconsistent data.
922441428dfSJeremy L Thompson 
923441428dfSJeremy L Thompson            Setting `is_writable == false` may offer a performance improvement on GPU backends.
924441428dfSJeremy L Thompson 
925ea61e9acSJeremy L Thompson   @param[in,out] qf          CeedQFunction
926ea61e9acSJeremy L Thompson   @param[in]     is_writable Writability status
927441428dfSJeremy L Thompson 
928441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
929441428dfSJeremy L Thompson 
930441428dfSJeremy L Thompson   @ref User
931441428dfSJeremy L Thompson **/
932441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
933441428dfSJeremy L Thompson   qf->is_context_writable = is_writable;
934441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
935441428dfSJeremy L Thompson }
936441428dfSJeremy L Thompson 
937441428dfSJeremy L Thompson /**
9386e15d496SJeremy L Thompson   @brief Set estimated number of FLOPs per quadrature required to apply QFunction
9396e15d496SJeremy L Thompson 
940ea61e9acSJeremy L Thompson   @param[in]  qf    QFunction to estimate FLOPs for
941ea61e9acSJeremy L Thompson   @param[out] flops FLOPs per quadrature point estimate
9426e15d496SJeremy L Thompson 
9436e15d496SJeremy L Thompson   @ref Backend
9446e15d496SJeremy L Thompson **/
9459d36ca50SJeremy L Thompson int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
9462b730f8bSJeremy L Thompson   if (flops < 0) {
9476e15d496SJeremy L Thompson     // LCOV_EXCL_START
9482b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_INCOMPATIBLE, "Must set non-negative FLOPs estimate");
9496e15d496SJeremy L Thompson     // LCOV_EXCL_STOP
9502b730f8bSJeremy L Thompson   }
9516e15d496SJeremy L Thompson   qf->user_flop_estimate = flops;
9526e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9536e15d496SJeremy L Thompson }
9546e15d496SJeremy L Thompson 
9556e15d496SJeremy L Thompson /**
95675affc3bSjeremylt   @brief View a CeedQFunction
95775affc3bSjeremylt 
95875affc3bSjeremylt   @param[in] qf     CeedQFunction to view
95975affc3bSjeremylt   @param[in] stream Stream to write; typically stdout/stderr or a file
96075affc3bSjeremylt 
96175affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
96275affc3bSjeremylt 
9637a982d89SJeremy L. Thompson   @ref User
96475affc3bSjeremylt **/
96575affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
966ca5eadf8SJeremy L Thompson   char *kernel_name;
96775affc3bSjeremylt 
9682b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetKernelName(qf, &kernel_name));
9692b730f8bSJeremy L Thompson   fprintf(stream, "%sCeedQFunction - %s\n", qf->is_gallery ? "Gallery " : "User ", qf->is_gallery ? qf->gallery_name : kernel_name);
97075affc3bSjeremylt 
9712b730f8bSJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " input field%s:\n", qf->num_input_fields, qf->num_input_fields > 1 ? "s" : "");
972d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
9732b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream));
97475affc3bSjeremylt   }
97575affc3bSjeremylt 
9762b730f8bSJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " output field%s:\n", qf->num_output_fields, qf->num_output_fields > 1 ? "s" : "");
977d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
9782b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream));
97975affc3bSjeremylt   }
980e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
98175affc3bSjeremylt }
98275affc3bSjeremylt 
98375affc3bSjeremylt /**
984b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunction
985b7c9bbdaSJeremy L Thompson 
986ea61e9acSJeremy L Thompson   @param[in]  qf   CeedQFunction
987b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store Ceed
988b7c9bbdaSJeremy L Thompson 
989b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
990b7c9bbdaSJeremy L Thompson 
991b7c9bbdaSJeremy L Thompson   @ref Advanced
992b7c9bbdaSJeremy L Thompson **/
993b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
994b7c9bbdaSJeremy L Thompson   *ceed = qf->ceed;
995b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
996b7c9bbdaSJeremy L Thompson }
997b7c9bbdaSJeremy L Thompson 
998b7c9bbdaSJeremy L Thompson /**
999b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
1000b11c1e72Sjeremylt 
1001ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedQFunction as immutable.
1002f04ea552SJeremy L Thompson 
1003ea61e9acSJeremy L Thompson   @param[in]  qf CeedQFunction
1004ea61e9acSJeremy L Thompson   @param[in]  Q  Number of quadrature points
100534138859Sjeremylt   @param[in]  u  Array of input CeedVectors
100634138859Sjeremylt   @param[out] v  Array of output CeedVectors
1007b11c1e72Sjeremylt 
1008b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1009dfdf5a53Sjeremylt 
10107a982d89SJeremy L. Thompson   @ref User
1011b11c1e72Sjeremylt **/
10122b730f8bSJeremy L Thompson int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, CeedVector *u, CeedVector *v) {
10132b730f8bSJeremy L Thompson   if (!qf->Apply) {
1014c042f62fSJeremy L Thompson     // LCOV_EXCL_START
10152b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support QFunctionApply");
1016c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
10172b730f8bSJeremy L Thompson   }
10182b730f8bSJeremy L Thompson   if (Q % qf->vec_length) {
1019c042f62fSJeremy L Thompson     // LCOV_EXCL_START
10202b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION, "Number of quadrature points %" CeedInt_FMT " must be a multiple of %" CeedInt_FMT, Q,
10212b730f8bSJeremy L Thompson                      qf->vec_length);
1022c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
10232b730f8bSJeremy L Thompson   }
1024f04ea552SJeremy L Thompson   qf->is_immutable = true;
10252b730f8bSJeremy L Thompson   CeedCall(qf->Apply(qf, Q, u, v));
1026e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1027d7b241e6Sjeremylt }
1028d7b241e6Sjeremylt 
1029b11c1e72Sjeremylt /**
1030b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
1031b11c1e72Sjeremylt 
1032ea61e9acSJeremy L Thompson   @param[in,out] qf CeedQFunction to destroy
1033b11c1e72Sjeremylt 
1034b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1035dfdf5a53Sjeremylt 
10367a982d89SJeremy L. Thompson   @ref User
1037b11c1e72Sjeremylt **/
1038d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
1039ad6481ceSJeremy L Thompson   if (!*qf || --(*qf)->ref_count > 0) {
1040ad6481ceSJeremy L Thompson     *qf = NULL;
1041ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1042ad6481ceSJeremy L Thompson   }
1043fe2413ffSjeremylt   // Backend destroy
1044d7b241e6Sjeremylt   if ((*qf)->Destroy) {
10452b730f8bSJeremy L Thompson     CeedCall((*qf)->Destroy(*qf));
1046d7b241e6Sjeremylt   }
1047fe2413ffSjeremylt   // Free fields
104892ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_input_fields; i++) {
10492b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->input_fields[i]).field_name));
10502b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->input_fields[i]));
1051fe2413ffSjeremylt   }
105292ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_output_fields; i++) {
10532b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->output_fields[i]).field_name));
10542b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->output_fields[i]));
1055fe2413ffSjeremylt   }
10562b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->input_fields));
10572b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->output_fields));
1058777ff853SJeremy L Thompson 
1059777ff853SJeremy L Thompson   // User context data object
10602b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&(*qf)->ctx));
1061fe2413ffSjeremylt 
10622b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->user_source));
10632b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->source_path));
10642b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->gallery_name));
10652b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->kernel_name));
10662b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*qf)->ceed));
10672b730f8bSJeremy L Thompson   CeedCall(CeedFree(qf));
1068e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1069d7b241e6Sjeremylt }
1070d7b241e6Sjeremylt 
1071d7b241e6Sjeremylt /// @}
1072