xref: /libCEED/interface/ceed-qfunction.c (revision 4ada38ba1e15e9840166c2c184ebc077f3a7a5ae)
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)) {
651c66c397SJeremy L Thompson   const char *relative_file_path;
6658c07c4fSSebastian Grimberg   int         ierr = 0;
67c042f62fSJeremy L Thompson 
688ccf1006SJeremy L Thompson   CeedDebugEnv("Gallery Register: %s", name);
692b730f8bSJeremy L Thompson   CeedCall(CeedGetJitRelativePath(source, &relative_file_path));
7058c07c4fSSebastian Grimberg   CeedPragmaCritical(CeedQFunctionRegister) {
7158c07c4fSSebastian Grimberg     if (num_qfunctions < sizeof(gallery_qfunctions) / sizeof(gallery_qfunctions[0])) {
72d1d35e2fSjeremylt       strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
73d1d35e2fSjeremylt       gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN - 1] = 0;
742b730f8bSJeremy L Thompson       strncpy(gallery_qfunctions[num_qfunctions].source, relative_file_path, CEED_MAX_RESOURCE_LEN);
75d1d35e2fSjeremylt       gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN - 1] = 0;
76d1d35e2fSjeremylt       gallery_qfunctions[num_qfunctions].vec_length                        = vec_length;
77d1d35e2fSjeremylt       gallery_qfunctions[num_qfunctions].f                                 = f;
78d1d35e2fSjeremylt       gallery_qfunctions[num_qfunctions].init                              = init;
79288c0443SJeremy L Thompson       num_qfunctions++;
8058c07c4fSSebastian Grimberg     } else {
8158c07c4fSSebastian Grimberg       ierr = 1;
8258c07c4fSSebastian Grimberg     }
8358c07c4fSSebastian Grimberg   }
8458c07c4fSSebastian Grimberg   // LCOV_EXCL_START
8558c07c4fSSebastian Grimberg   CeedCheck(ierr == 0, NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
8658c07c4fSSebastian Grimberg   // LCOV_EXCL_STOP
87e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
88288c0443SJeremy L Thompson }
89288c0443SJeremy L Thompson 
90288c0443SJeremy L Thompson /**
917a982d89SJeremy L. Thompson   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
927a982d89SJeremy L. Thompson 
93ea61e9acSJeremy L Thompson   @param[out] f           CeedQFunctionField
94ea61e9acSJeremy L Thompson   @param[in]  field_name  Name of QFunction field
955330800fSSebastian Grimberg   @param[in]  size        Size of QFunction field, (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_WEIGHT,
965330800fSSebastian 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,
975330800fSSebastian Grimberg (num_comp * dim) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and
985330800fSSebastian Grimberg (num_comp * curl_dim) with curl_dim = 1 if dim < 3 else dim for @ref CEED_EVAL_CURL.
99ea61e9acSJeremy L Thompson   @param[in]  eval_mode   \ref CEED_EVAL_NONE to use values directly,
1005330800fSSebastian Grimberg                             \ref CEED_EVAL_WEIGHT to use quadrature weights,
1017a982d89SJeremy L. Thompson                             \ref CEED_EVAL_INTERP to use interpolated values,
1027a982d89SJeremy L. Thompson                             \ref CEED_EVAL_GRAD to use gradients,
1035330800fSSebastian Grimberg                             \ref CEED_EVAL_DIV to use divergence,
1045330800fSSebastian Grimberg                             \ref CEED_EVAL_CURL to use curl.
1057a982d89SJeremy L. Thompson 
1067a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1077a982d89SJeremy L. Thompson 
1087a982d89SJeremy L. Thompson   @ref Developer
1097a982d89SJeremy L. Thompson **/
1102b730f8bSJeremy L Thompson static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
1112b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, f));
1122b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*f)->field_name));
1137a982d89SJeremy L. Thompson   (*f)->size      = size;
114d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
115e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1167a982d89SJeremy L. Thompson }
1177a982d89SJeremy L. Thompson 
1187a982d89SJeremy L. Thompson /**
1197a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1207a982d89SJeremy L. Thompson 
1217a982d89SJeremy L. Thompson   @param[in] field        QFunction field to view
122d1d35e2fSjeremylt   @param[in] field_number Number of field being viewed
1234c4400c7SValeria Barra   @param[in] in           true for input field, false for output
1247a982d89SJeremy L. Thompson   @param[in] stream       Stream to view to, e.g., stdout
1257a982d89SJeremy L. Thompson 
1267a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1277a982d89SJeremy L. Thompson 
1287a982d89SJeremy L. Thompson   @ref Utility
1297a982d89SJeremy L. Thompson **/
1302b730f8bSJeremy L Thompson static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt field_number, bool in, FILE *stream) {
1317a982d89SJeremy L. Thompson   const char  *inout = in ? "Input" : "Output";
1328229195eSjeremylt   char        *field_name;
1338229195eSjeremylt   CeedInt      size;
1348229195eSjeremylt   CeedEvalMode eval_mode;
1351c66c397SJeremy L Thompson 
1361c66c397SJeremy L Thompson   CeedCall(CeedQFunctionFieldGetName(field, &field_name));
1371c66c397SJeremy L Thompson   CeedCall(CeedQFunctionFieldGetSize(field, &size));
1382b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldGetEvalMode(field, &eval_mode));
1392b730f8bSJeremy L Thompson   fprintf(stream,
1402b730f8bSJeremy L Thompson           "    %s field %" CeedInt_FMT
1412b730f8bSJeremy L Thompson           ":\n"
1427a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1432b730f8bSJeremy L Thompson           "      Size: %" CeedInt_FMT
1442b730f8bSJeremy L Thompson           "\n"
1457a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1468229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
147e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1487a982d89SJeremy L. Thompson }
1497a982d89SJeremy L. Thompson 
150777ff853SJeremy L Thompson /**
151777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
152777ff853SJeremy L Thompson 
153ea61e9acSJeremy L Thompson   @param[in,out] qf     CeedQFunction
154ea61e9acSJeremy L Thompson   @param[in]     status Boolean value to set as Fortran status
155777ff853SJeremy L Thompson 
156777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
157777ff853SJeremy L Thompson 
158777ff853SJeremy L Thompson   @ref Backend
159777ff853SJeremy L Thompson **/
160777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
161f04ea552SJeremy L Thompson   qf->is_fortran = status;
162e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
163777ff853SJeremy L Thompson }
164777ff853SJeremy L Thompson 
1657a982d89SJeremy L. Thompson /// @}
1667a982d89SJeremy L. Thompson 
1677a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1687a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1697a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1707a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1717a982d89SJeremy L. Thompson /// @{
1727a982d89SJeremy L. Thompson 
1737a982d89SJeremy L. Thompson /**
1747a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1757a982d89SJeremy L. Thompson 
176ea61e9acSJeremy L Thompson   @param[in]  qf         CeedQFunction
177d1d35e2fSjeremylt   @param[out] vec_length Variable to store vector length
1787a982d89SJeremy L. Thompson 
1797a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1807a982d89SJeremy L. Thompson 
1817a982d89SJeremy L. Thompson   @ref Backend
1827a982d89SJeremy L. Thompson **/
183d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
184d1d35e2fSjeremylt   *vec_length = qf->vec_length;
185e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1867a982d89SJeremy L. Thompson }
1877a982d89SJeremy L. Thompson 
1887a982d89SJeremy L. Thompson /**
1897a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
1907a982d89SJeremy L. Thompson 
191ea61e9acSJeremy L Thompson   @param[in]  qf         CeedQFunction
192d1d35e2fSjeremylt   @param[out] num_input  Variable to store number of input fields
193d1d35e2fSjeremylt   @param[out] num_output Variable to store number of output fields
1947a982d89SJeremy L. Thompson 
1957a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1967a982d89SJeremy L. Thompson 
1977a982d89SJeremy L. Thompson   @ref Backend
1987a982d89SJeremy L. Thompson **/
1992b730f8bSJeremy L Thompson int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, CeedInt *num_output) {
200d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
201d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
202e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2037a982d89SJeremy L. Thompson }
2047a982d89SJeremy L. Thompson 
2057a982d89SJeremy L. Thompson /**
20643e1b16fSJeremy L Thompson   @brief Get the name of the user function for a CeedQFunction
2077a982d89SJeremy L. Thompson 
208ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
20943e1b16fSJeremy L Thompson   @param[out] kernel_name Variable to store source path string
2107a982d89SJeremy L. Thompson 
2117a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2127a982d89SJeremy L. Thompson 
2137a982d89SJeremy L. Thompson   @ref Backend
2147a982d89SJeremy L. Thompson **/
21543e1b16fSJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, char **kernel_name) {
216ca5eadf8SJeremy L Thompson   if (!qf->kernel_name) {
217ca5eadf8SJeremy L Thompson     Ceed  ceed;
218ca5eadf8SJeremy L Thompson     char *kernel_name_copy;
219ca5eadf8SJeremy L Thompson 
2201c66c397SJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
221ca5eadf8SJeremy L Thompson     if (qf->user_source) {
222ca5eadf8SJeremy L Thompson       const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
223ca5eadf8SJeremy L Thompson       size_t      kernel_name_len = strlen(kernel_name);
224ca5eadf8SJeremy L Thompson 
2252b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(kernel_name_len + 1, &kernel_name_copy));
226ca5eadf8SJeremy L Thompson       memcpy(kernel_name_copy, kernel_name, kernel_name_len);
227ca5eadf8SJeremy L Thompson     } else {
2282b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &kernel_name_copy));
229ca5eadf8SJeremy L Thompson     }
230ca5eadf8SJeremy L Thompson     qf->kernel_name = kernel_name_copy;
231ca5eadf8SJeremy L Thompson   }
232ca5eadf8SJeremy L Thompson 
23343e1b16fSJeremy L Thompson   *kernel_name = (char *)qf->kernel_name;
23443e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
23543e1b16fSJeremy L Thompson }
23643e1b16fSJeremy L Thompson 
23743e1b16fSJeremy L Thompson /**
23843e1b16fSJeremy L Thompson   @brief Get the source path string for a CeedQFunction
23943e1b16fSJeremy L Thompson 
240ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
24143e1b16fSJeremy L Thompson   @param[out] source_path Variable to store source path string
24243e1b16fSJeremy L Thompson 
24343e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
24443e1b16fSJeremy L Thompson 
24543e1b16fSJeremy L Thompson   @ref Backend
24643e1b16fSJeremy L Thompson **/
24743e1b16fSJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) {
248ca5eadf8SJeremy L Thompson   if (!qf->source_path && qf->user_source) {
249ca5eadf8SJeremy L Thompson     Ceed        ceed;
250ca5eadf8SJeremy L Thompson     bool        is_absolute_path;
251ca5eadf8SJeremy L Thompson     char       *absolute_path, *source_path_copy;
252ca5eadf8SJeremy L Thompson     const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
253ca5eadf8SJeremy L Thompson     size_t      kernel_name_len = strlen(kernel_name);
254ca5eadf8SJeremy L Thompson 
2552b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
2562b730f8bSJeremy L Thompson     CeedCall(CeedCheckFilePath(ceed, qf->user_source, &is_absolute_path));
257ca5eadf8SJeremy L Thompson     if (is_absolute_path) {
258ca5eadf8SJeremy L Thompson       absolute_path = (char *)qf->user_source;
259ca5eadf8SJeremy L Thompson     } else {
2602b730f8bSJeremy L Thompson       CeedCall(CeedGetJitAbsolutePath(ceed, qf->user_source, &absolute_path));
261ca5eadf8SJeremy L Thompson     }
262ca5eadf8SJeremy L Thompson 
263ca5eadf8SJeremy L Thompson     size_t source_len = strlen(absolute_path) - kernel_name_len - 1;
2641c66c397SJeremy L Thompson 
2652b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(source_len + 1, &source_path_copy));
266ca5eadf8SJeremy L Thompson     memcpy(source_path_copy, absolute_path, source_len);
267ca5eadf8SJeremy L Thompson     qf->source_path = source_path_copy;
268ca5eadf8SJeremy L Thompson 
2692b730f8bSJeremy L Thompson     if (!is_absolute_path) CeedCall(CeedFree(&absolute_path));
270ca5eadf8SJeremy L Thompson   }
271ca5eadf8SJeremy L Thompson 
27243e1b16fSJeremy L Thompson   *source_path = (char *)qf->source_path;
273e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2747a982d89SJeremy L. Thompson }
2757a982d89SJeremy L. Thompson 
2767a982d89SJeremy L. Thompson /**
277ea61e9acSJeremy L Thompson   @brief Initialize and load QFunction source file into string buffer, including full text of local files in place of `#include "local.h"`.
2784385fb7fSSebastian Grimberg 
2793d3250a0SJeremy L Thompson   The `buffer` is set to `NULL` if there is no QFunction source file.
2804385fb7fSSebastian Grimberg 
2813d3250a0SJeremy L Thompson   Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
2823d3250a0SJeremy L Thompson 
283ea61e9acSJeremy L Thompson   @param[in]  qf            CeedQFunction
284f74ec584SJeremy L Thompson   @param[out] source_buffer String buffer for source file contents
2853d3250a0SJeremy L Thompson 
2863d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2873d3250a0SJeremy L Thompson 
2883d3250a0SJeremy L Thompson   @ref Backend
2893d3250a0SJeremy L Thompson **/
2903d3250a0SJeremy L Thompson int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, char **source_buffer) {
2913d3250a0SJeremy L Thompson   char *source_path;
2923d3250a0SJeremy L Thompson 
2932b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetSourcePath(qf, &source_path));
2943d3250a0SJeremy L Thompson   *source_buffer = NULL;
2953d3250a0SJeremy L Thompson   if (source_path) {
2962b730f8bSJeremy L Thompson     CeedCall(CeedLoadSourceToBuffer(qf->ceed, source_path, source_buffer));
2973d3250a0SJeremy L Thompson   }
2983d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2993d3250a0SJeremy L Thompson }
3003d3250a0SJeremy L Thompson 
3013d3250a0SJeremy L Thompson /**
3027a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
3037a982d89SJeremy L. Thompson 
304ea61e9acSJeremy L Thompson   @param[in]  qf CeedQFunction
3057a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
3067a982d89SJeremy L. Thompson 
3077a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3087a982d89SJeremy L. Thompson 
3097a982d89SJeremy L. Thompson   @ref Backend
3107a982d89SJeremy L. Thompson **/
3117a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
3127a982d89SJeremy L. Thompson   *f = qf->function;
313e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3147a982d89SJeremy L. Thompson }
3157a982d89SJeremy L. Thompson 
3167a982d89SJeremy L. Thompson /**
317777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
3184385fb7fSSebastian Grimberg 
319ea61e9acSJeremy L Thompson   Note: For QFunctions from the Fortran interface, this function will return the Fortran context CeedQFunctionContext.
3207a982d89SJeremy L. Thompson 
321ea61e9acSJeremy L Thompson   @param[in]  qf  CeedQFunction
322777ff853SJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
3237a982d89SJeremy L. Thompson 
3247a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3257a982d89SJeremy L. Thompson 
3267a982d89SJeremy L. Thompson   @ref Backend
3277a982d89SJeremy L. Thompson **/
328777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
3297a982d89SJeremy L. Thompson   *ctx = qf->ctx;
330e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3317a982d89SJeremy L. Thompson }
3327a982d89SJeremy L. Thompson 
3337a982d89SJeremy L. Thompson /**
334441428dfSJeremy L Thompson   @brief Get context data of a CeedQFunction
335441428dfSJeremy L Thompson 
336ea61e9acSJeremy L Thompson   @param[in]  qf       CeedQFunction
337ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
338ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
339441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
340441428dfSJeremy L Thompson 
341441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
342441428dfSJeremy L Thompson 
343441428dfSJeremy L Thompson   @ref Backend
344441428dfSJeremy L Thompson **/
3452b730f8bSJeremy L Thompson int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
346441428dfSJeremy L Thompson   bool                 is_writable;
347441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
348441428dfSJeremy L Thompson 
3492b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
350441428dfSJeremy L Thompson   if (ctx) {
3512b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
352441428dfSJeremy L Thompson     if (is_writable) {
3532b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
354441428dfSJeremy L Thompson     } else {
3552b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
356441428dfSJeremy L Thompson     }
357441428dfSJeremy L Thompson   } else {
358441428dfSJeremy L Thompson     *(void **)data = NULL;
359441428dfSJeremy L Thompson   }
360441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
361441428dfSJeremy L Thompson }
362441428dfSJeremy L Thompson 
363441428dfSJeremy L Thompson /**
364441428dfSJeremy L Thompson   @brief Restore context data of a CeedQFunction
365441428dfSJeremy L Thompson 
366ea61e9acSJeremy L Thompson   @param[in]     qf   CeedQFunction
367ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
368441428dfSJeremy L Thompson 
369441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
370441428dfSJeremy L Thompson 
371441428dfSJeremy L Thompson   @ref Backend
372441428dfSJeremy L Thompson **/
373441428dfSJeremy L Thompson int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) {
374441428dfSJeremy L Thompson   bool                 is_writable;
375441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
376441428dfSJeremy L Thompson 
3772b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
378441428dfSJeremy L Thompson   if (ctx) {
3792b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
380441428dfSJeremy L Thompson     if (is_writable) {
3812b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
382441428dfSJeremy L Thompson     } else {
3832b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
384441428dfSJeremy L Thompson     }
385441428dfSJeremy L Thompson   }
3865f249b39SJeremy L Thompson   *(void **)data = NULL;
387441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
388441428dfSJeremy L Thompson }
389441428dfSJeremy L Thompson 
390441428dfSJeremy L Thompson /**
3917a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
3924385fb7fSSebastian Grimberg 
393ea61e9acSJeremy L Thompson   Note: For all QFunctions this function will return the user CeedQFunctionContext and not interface context CeedQFunctionContext, if any
394ea61e9acSJeremy L Thompson such object exists.
3957a982d89SJeremy L. Thompson 
396ea61e9acSJeremy L Thompson   @param[in]  qf  CeedQFunction
397777ff853SJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
3987a982d89SJeremy L. Thompson 
3997a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4007a982d89SJeremy L. Thompson   @ref Backend
4017a982d89SJeremy L. Thompson **/
402777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
403f04ea552SJeremy L Thompson   if (qf->is_fortran) {
404d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
4051c66c397SJeremy L Thompson 
4062b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx));
4078cb0412aSJeremy L Thompson     *ctx = fortran_ctx->inner_ctx;
4082b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx));
4097a982d89SJeremy L. Thompson   } else {
4107a982d89SJeremy L. Thompson     *ctx = qf->ctx;
4117a982d89SJeremy L. Thompson   }
412e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4137a982d89SJeremy L. Thompson }
4147a982d89SJeremy L. Thompson 
4157a982d89SJeremy L. Thompson /**
416441428dfSJeremy L Thompson   @brief Get inner context data of a CeedQFunction
417441428dfSJeremy L Thompson 
418ea61e9acSJeremy L Thompson   @param[in]  qf       CeedQFunction
419ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
420ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
421441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
422441428dfSJeremy L Thompson 
423441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
424441428dfSJeremy L Thompson 
425441428dfSJeremy L Thompson   @ref Backend
426441428dfSJeremy L Thompson **/
4272b730f8bSJeremy L Thompson int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
428441428dfSJeremy L Thompson   bool                 is_writable;
429441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
430441428dfSJeremy L Thompson 
4312b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
432441428dfSJeremy L Thompson   if (ctx) {
4332b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
434441428dfSJeremy L Thompson     if (is_writable) {
4352b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
436441428dfSJeremy L Thompson     } else {
4372b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
438441428dfSJeremy L Thompson     }
439441428dfSJeremy L Thompson   } else {
440441428dfSJeremy L Thompson     *(void **)data = NULL;
441441428dfSJeremy L Thompson   }
442441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
443441428dfSJeremy L Thompson }
444441428dfSJeremy L Thompson 
445441428dfSJeremy L Thompson /**
446441428dfSJeremy L Thompson   @brief Restore inner context data of a CeedQFunction
447441428dfSJeremy L Thompson 
448ea61e9acSJeremy L Thompson   @param[in]     qf   CeedQFunction
449ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
450441428dfSJeremy L Thompson 
451441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
452441428dfSJeremy L Thompson 
453441428dfSJeremy L Thompson   @ref Backend
454441428dfSJeremy L Thompson **/
455441428dfSJeremy L Thompson int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) {
456441428dfSJeremy L Thompson   bool                 is_writable;
457441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
458441428dfSJeremy L Thompson 
4592b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
460441428dfSJeremy L Thompson   if (ctx) {
4612b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
462441428dfSJeremy L Thompson     if (is_writable) {
4632b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
464441428dfSJeremy L Thompson     } else {
4652b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
466441428dfSJeremy L Thompson     }
467441428dfSJeremy L Thompson   }
4685f249b39SJeremy L Thompson   *(void **)data = NULL;
469441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
470441428dfSJeremy L Thompson }
471441428dfSJeremy L Thompson 
472441428dfSJeremy L Thompson /**
4737a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
4747a982d89SJeremy L. Thompson 
475ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
476d1d35e2fSjeremylt   @param[out] is_identity Variable to store identity status
4777a982d89SJeremy L. Thompson 
4787a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4797a982d89SJeremy L. Thompson 
4807a982d89SJeremy L. Thompson   @ref Backend
4817a982d89SJeremy L. Thompson **/
482d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
483f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
484e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4857a982d89SJeremy L. Thompson }
4867a982d89SJeremy L. Thompson 
4877a982d89SJeremy L. Thompson /**
488441428dfSJeremy L Thompson   @brief Determine if QFunctionContext is writable
489441428dfSJeremy L Thompson 
490ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
491ea61e9acSJeremy L Thompson   @param[out] is_writable Variable to store context writeable status
492441428dfSJeremy L Thompson 
493441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
494441428dfSJeremy L Thompson 
495441428dfSJeremy L Thompson   @ref Backend
496441428dfSJeremy L Thompson **/
497441428dfSJeremy L Thompson int CeedQFunctionIsContextWritable(CeedQFunction qf, bool *is_writable) {
498441428dfSJeremy L Thompson   *is_writable = qf->is_context_writable;
499441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
500441428dfSJeremy L Thompson }
501441428dfSJeremy L Thompson 
502441428dfSJeremy L Thompson /**
5037a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
5047a982d89SJeremy L. Thompson 
505ea61e9acSJeremy L Thompson   @param[in]  qf   CeedQFunction
5067a982d89SJeremy L. Thompson   @param[out] data Variable to store data
5077a982d89SJeremy L. Thompson 
5087a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5097a982d89SJeremy L. Thompson 
5107a982d89SJeremy L. Thompson   @ref Backend
5117a982d89SJeremy L. Thompson **/
512777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
513777ff853SJeremy L Thompson   *(void **)data = qf->data;
514e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5157a982d89SJeremy L. Thompson }
5167a982d89SJeremy L. Thompson 
5177a982d89SJeremy L. Thompson /**
5187a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
5197a982d89SJeremy L. Thompson 
520ea61e9acSJeremy L Thompson   @param[in,out] qf   CeedQFunction
521ea61e9acSJeremy L Thompson   @param[in]     data Data to set
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 **/
527777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
528777ff853SJeremy L Thompson   qf->data = data;
529e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5307a982d89SJeremy L. Thompson }
5317a982d89SJeremy L. Thompson 
5327a982d89SJeremy L. Thompson /**
53334359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
53434359f16Sjeremylt 
535ea61e9acSJeremy L Thompson   @param[in,out] qf CeedQFunction to increment the reference counter
53634359f16Sjeremylt 
53734359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
53834359f16Sjeremylt 
53934359f16Sjeremylt   @ref Backend
54034359f16Sjeremylt **/
5419560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
54234359f16Sjeremylt   qf->ref_count++;
54334359f16Sjeremylt   return CEED_ERROR_SUCCESS;
54434359f16Sjeremylt }
54534359f16Sjeremylt 
5466e15d496SJeremy L Thompson /**
5476e15d496SJeremy L Thompson   @brief Estimate number of FLOPs per quadrature required to apply QFunction
5486e15d496SJeremy L Thompson 
549ea61e9acSJeremy L Thompson   @param[in]  qf    QFunction to estimate FLOPs for
550ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
5516e15d496SJeremy L Thompson 
5526e15d496SJeremy L Thompson   @ref Backend
5536e15d496SJeremy L Thompson **/
5549d36ca50SJeremy L Thompson int CeedQFunctionGetFlopsEstimate(CeedQFunction qf, CeedSize *flops) {
5556574a04fSJeremy L Thompson   CeedCheck(qf->user_flop_estimate > -1, qf->ceed, CEED_ERROR_INCOMPLETE, "Must set FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
5566e15d496SJeremy L Thompson   *flops = qf->user_flop_estimate;
5576e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5586e15d496SJeremy L Thompson }
5596e15d496SJeremy L Thompson 
5607a982d89SJeremy L. Thompson /// @}
5617a982d89SJeremy L. Thompson 
5627a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5637a982d89SJeremy L. Thompson /// CeedQFunction Public API
5647a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5657a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
5667a982d89SJeremy L. Thompson /// @{
5677a982d89SJeremy L. Thompson 
5687a982d89SJeremy L. Thompson /**
5697a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
5707a982d89SJeremy L. Thompson 
571ea61e9acSJeremy L Thompson   @param[in]  ceed       Ceed object where the CeedQFunction will be created
572ea61e9acSJeremy L Thompson   @param[in]  vec_length Vector length. Caller must ensure that number of quadrature points is a multiple of vec_length.
573ea61e9acSJeremy L Thompson   @param[in]  f          Function pointer to evaluate action at quadrature points.
5747a982d89SJeremy L. Thompson                            See \ref CeedQFunctionUser.
575ea61e9acSJeremy L Thompson   @param[in]  source     Absolute path to source of QFunction, "\abs_path\file.h:function_name".
576*4ada38baSJames Wright                            The entire source file must only contain constructs supported by all targeted backends (i.e. CUDA for `/gpu/cuda`,
577*4ada38baSJames Wright                            OpenCL/SYCL for `/gpu/sycl`, etc.).
578*4ada38baSJames Wright                            The entire contents of this file and all locally included files are used during JiT compilation for GPU backends.
579*4ada38baSJames Wright                            All source files must be at the provided filepath at runtime for JiT to function.
580ea61e9acSJeremy L Thompson   @param[out] qf         Address of the variable where the newly created CeedQFunction will be stored
5817a982d89SJeremy L. Thompson 
5827a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5837a982d89SJeremy L. Thompson 
584ea61e9acSJeremy L Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's arguments.
5857a982d89SJeremy L. Thompson 
5867a982d89SJeremy L. Thompson   @ref User
5877a982d89SJeremy L. Thompson **/
5882b730f8bSJeremy L Thompson int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length, CeedQFunctionUser f, const char *source, CeedQFunction *qf) {
589ca5eadf8SJeremy L Thompson   char *user_source_copy;
5907a982d89SJeremy L. Thompson 
5917a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
5927a982d89SJeremy L. Thompson     Ceed delegate;
5936574a04fSJeremy L Thompson 
5942b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "QFunction"));
5956574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support QFunctionCreate");
5962b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf));
597e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
5987a982d89SJeremy L. Thompson   }
5997a982d89SJeremy L. Thompson 
6006574a04fSJeremy L Thompson   CeedCheck(!strlen(source) || strrchr(source, ':'), ceed, CEED_ERROR_INCOMPLETE,
6016574a04fSJeremy L Thompson             "Provided path to source does not include function name. Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"", source);
60243e1b16fSJeremy L Thompson 
6032b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, qf));
604db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*qf)->ceed));
605d1d35e2fSjeremylt   (*qf)->ref_count           = 1;
606d1d35e2fSjeremylt   (*qf)->vec_length          = vec_length;
607f04ea552SJeremy L Thompson   (*qf)->is_identity         = false;
608441428dfSJeremy L Thompson   (*qf)->is_context_writable = true;
6097a982d89SJeremy L. Thompson   (*qf)->function            = f;
6106e15d496SJeremy L Thompson   (*qf)->user_flop_estimate  = -1;
61143e1b16fSJeremy L Thompson   if (strlen(source)) {
612ca5eadf8SJeremy L Thompson     size_t user_source_len = strlen(source);
613ee5a26f2SJeremy L Thompson 
6142b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(user_source_len + 1, &user_source_copy));
615ca5eadf8SJeremy L Thompson     memcpy(user_source_copy, source, user_source_len);
616ca5eadf8SJeremy L Thompson     (*qf)->user_source = user_source_copy;
61743e1b16fSJeremy L Thompson   }
6182b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields));
6192b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields));
6202b730f8bSJeremy L Thompson   CeedCall(ceed->QFunctionCreate(*qf));
621e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6227a982d89SJeremy L. Thompson }
6237a982d89SJeremy L. Thompson 
6247a982d89SJeremy L. Thompson /**
625288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
626288c0443SJeremy L Thompson 
627ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedQFunction will be created
628ea61e9acSJeremy L Thompson   @param[in]  name Name of QFunction to use from gallery
629ea61e9acSJeremy L Thompson   @param[out] qf   Address of the variable where the newly created CeedQFunction will be stored
630288c0443SJeremy L Thompson 
631288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
632288c0443SJeremy L Thompson 
6337a982d89SJeremy L. Thompson   @ref User
634288c0443SJeremy L Thompson **/
6352b730f8bSJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, CeedQFunction *qf) {
636f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
637288c0443SJeremy L Thompson 
6382b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionRegisterAll());
639288c0443SJeremy L Thompson   // Find matching backend
6406574a04fSJeremy L Thompson   CeedCheck(name, ceed, CEED_ERROR_INCOMPLETE, "No QFunction name provided");
641288c0443SJeremy L Thompson   for (size_t i = 0; i < num_qfunctions; i++) {
642288c0443SJeremy L Thompson     size_t      n;
643d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
6442b730f8bSJeremy L Thompson     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {
6452b730f8bSJeremy L Thompson     }
646d1d35e2fSjeremylt     if (n > match_len) {
647d1d35e2fSjeremylt       match_len   = n;
648f7e22acaSJeremy L Thompson       match_index = i;
649288c0443SJeremy L Thompson     }
650288c0443SJeremy L Thompson   }
6516574a04fSJeremy L Thompson   CeedCheck(match_len > 0, ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
652288c0443SJeremy L Thompson 
653288c0443SJeremy L Thompson   // Create QFunction
6542b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInterior(ceed, gallery_qfunctions[match_index].vec_length, gallery_qfunctions[match_index].f,
6552b730f8bSJeremy L Thompson                                        gallery_qfunctions[match_index].source, qf));
656288c0443SJeremy L Thompson 
657288c0443SJeremy L Thompson   // QFunction specific setup
6582b730f8bSJeremy L Thompson   CeedCall(gallery_qfunctions[match_index].init(ceed, name, *qf));
659288c0443SJeremy L Thompson 
66075affc3bSjeremylt   // Copy name
6612b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name));
66243e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
663e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
664288c0443SJeremy L Thompson }
665288c0443SJeremy L Thompson 
666288c0443SJeremy L Thompson /**
667ea61e9acSJeremy L Thompson   @brief Create an identity CeedQFunction.
6684385fb7fSSebastian Grimberg 
669ea61e9acSJeremy L Thompson   Inputs are written into outputs in the order given.
670859c15bbSJames Wright   This is useful for CeedOperators that can be represented with only the action of a CeedElemRestriction and CeedBasis, such as restriction
6719fd66db6SSebastian Grimberg and prolongation operators for p-multigrid.
6729fd66db6SSebastian Grimberg   Backends may optimize CeedOperators with this CeedQFunction to avoid the copy of input data to output fields by using the same memory location for
6739fd66db6SSebastian Grimberg both.
6740219ea01SJeremy L Thompson 
675ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed object where the CeedQFunction will be created
676d1d35e2fSjeremylt   @param[in]  size     Size of the QFunction fields
677d1d35e2fSjeremylt   @param[in]  in_mode  CeedEvalMode for input to CeedQFunction
678d1d35e2fSjeremylt   @param[in]  out_mode CeedEvalMode for output to CeedQFunction
679ea61e9acSJeremy L Thompson   @param[out] qf       Address of the variable where the newly created CeedQFunction will be stored
6800219ea01SJeremy L Thompson 
6810219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
6820219ea01SJeremy L Thompson 
6837a982d89SJeremy L. Thompson   @ref User
6840219ea01SJeremy L Thompson **/
6852b730f8bSJeremy L Thompson int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, CeedEvalMode out_mode, CeedQFunction *qf) {
6861c66c397SJeremy L Thompson   CeedQFunctionContext  ctx;
6871c66c397SJeremy L Thompson   CeedContextFieldLabel size_label;
6881c66c397SJeremy L Thompson 
6892b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Identity", qf));
6902b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(*qf, "input", size, in_mode));
6912b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(*qf, "output", size, out_mode));
6920219ea01SJeremy L Thompson 
693f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
694547dbd6fSJeremy L Thompson 
6952b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(*qf, &ctx));
6962b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label));
6972b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetInt32(ctx, size_label, &size));
698e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6990219ea01SJeremy L Thompson }
7000219ea01SJeremy L Thompson 
7010219ea01SJeremy L Thompson /**
702ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedQFunction.
7034385fb7fSSebastian Grimberg 
704ea61e9acSJeremy L Thompson   Both pointers should be destroyed with `CeedQFunctionDestroy()`.
705512bb800SJeremy L Thompson 
706512bb800SJeremy 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.
707ea61e9acSJeremy L Thompson         This CeedQFunction will be destroyed if `*qf_copy` is the only reference to this CeedQFunction.
7089560d06aSjeremylt 
709ea61e9acSJeremy L Thompson   @param[in]  qf      CeedQFunction to copy reference to
7109560d06aSjeremylt   @param[out] qf_copy Variable to store copied reference
7119560d06aSjeremylt 
7129560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7139560d06aSjeremylt 
7149560d06aSjeremylt   @ref User
7159560d06aSjeremylt **/
7169560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
7172b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionReference(qf));
7182b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(qf_copy));
7199560d06aSjeremylt   *qf_copy = qf;
7209560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7219560d06aSjeremylt }
7229560d06aSjeremylt 
7239560d06aSjeremylt /**
724a0a97fcfSJed Brown   @brief Add a CeedQFunction input
725b11c1e72Sjeremylt 
726ea61e9acSJeremy L Thompson   @param[in,out] qf         CeedQFunction
727ea61e9acSJeremy L Thompson   @param[in]     field_name Name of QFunction field
7285330800fSSebastian Grimberg   @param[in]     size       Size of QFunction field, (num_comp * 1) for @ref CEED_EVAL_NONE,
7295330800fSSebastian 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,
7305330800fSSebastian Grimberg (num_comp * dim) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and
7315330800fSSebastian Grimberg (num_comp * curl_dim) with curl_dim = 1 if dim < 3 else dim for @ref CEED_EVAL_CURL.
732ea61e9acSJeremy L Thompson   @param[in]     eval_mode  \ref CEED_EVAL_NONE to use values directly,
733b11c1e72Sjeremylt                               \ref CEED_EVAL_INTERP to use interpolated values,
7345330800fSSebastian Grimberg                               \ref CEED_EVAL_GRAD to use gradients,
7355330800fSSebastian Grimberg                               \ref CEED_EVAL_DIV to use divergence,
7365330800fSSebastian Grimberg                               \ref CEED_EVAL_CURL to use curl.
737b11c1e72Sjeremylt 
738b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
739dfdf5a53Sjeremylt 
7407a982d89SJeremy L. Thompson   @ref User
741b11c1e72Sjeremylt **/
7422b730f8bSJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
7436574a04fSJeremy L Thompson   CeedCheck(!qf->is_immutable, qf->ceed, CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
7446574a04fSJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT || size == 1, qf->ceed, CEED_ERROR_DIMENSION, "CEED_EVAL_WEIGHT should have size 1");
745643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
7466574a04fSJeremy L Thompson     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
747643fbb69SJeremy L Thompson   }
748643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
7496574a04fSJeremy L Thompson     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
750643fbb69SJeremy L Thompson   }
7512b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], field_name, size, eval_mode));
752d1d35e2fSjeremylt   qf->num_input_fields++;
753e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
754d7b241e6Sjeremylt }
755d7b241e6Sjeremylt 
756b11c1e72Sjeremylt /**
757a0a97fcfSJed Brown   @brief Add a CeedQFunction output
758b11c1e72Sjeremylt 
759ea61e9acSJeremy L Thompson   @param[in,out] qf         CeedQFunction
760ea61e9acSJeremy L Thompson   @param[in]     field_name Name of QFunction field
7615330800fSSebastian Grimberg   @param[in]     size       Size of QFunction field, (num_comp * 1) for @ref CEED_EVAL_NONE,
7625330800fSSebastian 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,
7635330800fSSebastian Grimberg (num_comp * dim) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and
7645330800fSSebastian Grimberg (num_comp * curl_dim) with curl_dim = 1 if dim < 3 else dim for @ref CEED_EVAL_CURL.
765ea61e9acSJeremy L Thompson   @param[in]     eval_mode  \ref CEED_EVAL_NONE to use values directly,
766b11c1e72Sjeremylt                               \ref CEED_EVAL_INTERP to use interpolated values,
7675330800fSSebastian Grimberg                               \ref CEED_EVAL_GRAD to use gradients,
7685330800fSSebastian Grimberg                               \ref CEED_EVAL_DIV to use divergence,
7695330800fSSebastian Grimberg                               \ref CEED_EVAL_CURL to use curl.
770b11c1e72Sjeremylt 
771b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
772dfdf5a53Sjeremylt 
7737a982d89SJeremy L. Thompson   @ref User
774b11c1e72Sjeremylt **/
7752b730f8bSJeremy L Thompson int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
7766574a04fSJeremy L Thompson   CeedCheck(!qf->is_immutable, qf->ceed, CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
7776574a04fSJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT, qf->ceed, CEED_ERROR_DIMENSION, "Cannot create QFunction output with CEED_EVAL_WEIGHT");
778643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
7796574a04fSJeremy L Thompson     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
780643fbb69SJeremy L Thompson   }
781643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
7826574a04fSJeremy L Thompson     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
783643fbb69SJeremy L Thompson   }
7842b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], field_name, size, eval_mode));
785d1d35e2fSjeremylt   qf->num_output_fields++;
786e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
787d7b241e6Sjeremylt }
788d7b241e6Sjeremylt 
789dfdf5a53Sjeremylt /**
79043bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
79143bbe138SJeremy L Thompson 
792ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedQFunction as immutable.
793f04ea552SJeremy L Thompson 
794ea61e9acSJeremy L Thompson   @param[in]  qf                CeedQFunction
795f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
796f74ec584SJeremy L Thompson   @param[out] input_fields      Variable to store input fields
797f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
798f74ec584SJeremy L Thompson   @param[out] output_fields     Variable to store output fields
79943bbe138SJeremy L Thompson 
80043bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
80143bbe138SJeremy L Thompson 
802e9b533fbSJeremy L Thompson   @ref Advanced
80343bbe138SJeremy L Thompson **/
8042b730f8bSJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields, CeedQFunctionField **input_fields, CeedInt *num_output_fields,
80543bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
806f04ea552SJeremy L Thompson   qf->is_immutable = true;
80743bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
80843bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
80943bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
81043bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
81143bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
81243bbe138SJeremy L Thompson }
81343bbe138SJeremy L Thompson 
81443bbe138SJeremy L Thompson /**
81543bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
81643bbe138SJeremy L Thompson 
817ea61e9acSJeremy L Thompson   @param[in]  qf_field   CeedQFunctionField
81843bbe138SJeremy L Thompson   @param[out] field_name Variable to store the field name
81943bbe138SJeremy L Thompson 
82043bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
82143bbe138SJeremy L Thompson 
822e9b533fbSJeremy L Thompson   @ref Advanced
82343bbe138SJeremy L Thompson **/
82443bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
82543bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
82643bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
82743bbe138SJeremy L Thompson }
82843bbe138SJeremy L Thompson 
82943bbe138SJeremy L Thompson /**
83043bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
83143bbe138SJeremy L Thompson 
832ea61e9acSJeremy L Thompson   @param[in]  qf_field CeedQFunctionField
83343bbe138SJeremy L Thompson   @param[out] size     Variable to store the size of the field
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 **/
83943bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
84043bbe138SJeremy L Thompson   *size = qf_field->size;
84143bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
84243bbe138SJeremy L Thompson }
84343bbe138SJeremy L Thompson 
84443bbe138SJeremy L Thompson /**
84543bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
84643bbe138SJeremy L Thompson 
847ea61e9acSJeremy L Thompson   @param[in]  qf_field  CeedQFunctionField
84843bbe138SJeremy L Thompson   @param[out] eval_mode Variable to store the field evaluation mode
84943bbe138SJeremy L Thompson 
85043bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
85143bbe138SJeremy L Thompson 
852e9b533fbSJeremy L Thompson   @ref Advanced
85343bbe138SJeremy L Thompson **/
8542b730f8bSJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, CeedEvalMode *eval_mode) {
85543bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
85643bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
85743bbe138SJeremy L Thompson }
85843bbe138SJeremy L Thompson 
85943bbe138SJeremy L Thompson /**
8604ce2993fSjeremylt   @brief Set global context for a CeedQFunction
861b11c1e72Sjeremylt 
862ea61e9acSJeremy L Thompson   @param[in,out] qf  CeedQFunction
863ea61e9acSJeremy L Thompson   @param[in]     ctx Context data to set
864b11c1e72Sjeremylt 
865b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
866dfdf5a53Sjeremylt 
8677a982d89SJeremy L. Thompson   @ref User
868b11c1e72Sjeremylt **/
869777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
8702b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&qf->ctx));
871d7b241e6Sjeremylt   qf->ctx = ctx;
872db002c03SJeremy L Thompson   if (ctx) CeedCall(CeedQFunctionContextReference(ctx));
873e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
874d7b241e6Sjeremylt }
875d7b241e6Sjeremylt 
876b11c1e72Sjeremylt /**
877441428dfSJeremy L Thompson   @brief Set writability of CeedQFunctionContext when calling the `CeedQFunctionUser`.
8784385fb7fSSebastian Grimberg 
879859c15bbSJames Wright   The default value is `is_writable == true`.
880441428dfSJeremy L Thompson 
881ea61e9acSJeremy L Thompson   Setting `is_writable == true` indicates the `CeedQFunctionUser` writes into the CeedQFunctionContextData and requires memory syncronization
882441428dfSJeremy L Thompson after calling `CeedQFunctionApply()`.
883441428dfSJeremy L Thompson 
884ea61e9acSJeremy L Thompson   Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not modify the CeedQFunctionContextData.
885ea61e9acSJeremy L Thompson   Violating this assertion may lead to inconsistent data.
886441428dfSJeremy L Thompson 
887441428dfSJeremy L Thompson   Setting `is_writable == false` may offer a performance improvement on GPU backends.
888441428dfSJeremy L Thompson 
889ea61e9acSJeremy L Thompson   @param[in,out] qf          CeedQFunction
890ea61e9acSJeremy L Thompson   @param[in]     is_writable Writability status
891441428dfSJeremy L Thompson 
892441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
893441428dfSJeremy L Thompson 
894441428dfSJeremy L Thompson   @ref User
895441428dfSJeremy L Thompson **/
896441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
897441428dfSJeremy L Thompson   qf->is_context_writable = is_writable;
898441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
899441428dfSJeremy L Thompson }
900441428dfSJeremy L Thompson 
901441428dfSJeremy L Thompson /**
9026e15d496SJeremy L Thompson   @brief Set estimated number of FLOPs per quadrature required to apply QFunction
9036e15d496SJeremy L Thompson 
904ea61e9acSJeremy L Thompson   @param[in]  qf    QFunction to estimate FLOPs for
905ea61e9acSJeremy L Thompson   @param[out] flops FLOPs per quadrature point estimate
9066e15d496SJeremy L Thompson 
9076e15d496SJeremy L Thompson   @ref Backend
9086e15d496SJeremy L Thompson **/
9099d36ca50SJeremy L Thompson int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
9106574a04fSJeremy L Thompson   CeedCheck(flops >= 0, qf->ceed, CEED_ERROR_INCOMPATIBLE, "Must set non-negative FLOPs estimate");
9116e15d496SJeremy L Thompson   qf->user_flop_estimate = flops;
9126e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9136e15d496SJeremy L Thompson }
9146e15d496SJeremy L Thompson 
9156e15d496SJeremy L Thompson /**
91675affc3bSjeremylt   @brief View a CeedQFunction
91775affc3bSjeremylt 
91875affc3bSjeremylt   @param[in] qf     CeedQFunction to view
91975affc3bSjeremylt   @param[in] stream Stream to write; typically stdout/stderr or a file
92075affc3bSjeremylt 
92175affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
92275affc3bSjeremylt 
9237a982d89SJeremy L. Thompson   @ref User
92475affc3bSjeremylt **/
92575affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
926ca5eadf8SJeremy L Thompson   char *kernel_name;
92775affc3bSjeremylt 
9282b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetKernelName(qf, &kernel_name));
9292b730f8bSJeremy L Thompson   fprintf(stream, "%sCeedQFunction - %s\n", qf->is_gallery ? "Gallery " : "User ", qf->is_gallery ? qf->gallery_name : kernel_name);
93075affc3bSjeremylt 
9312b730f8bSJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " input field%s:\n", qf->num_input_fields, qf->num_input_fields > 1 ? "s" : "");
932d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
9332b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream));
93475affc3bSjeremylt   }
93575affc3bSjeremylt 
9362b730f8bSJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " output field%s:\n", qf->num_output_fields, qf->num_output_fields > 1 ? "s" : "");
937d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
9382b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream));
93975affc3bSjeremylt   }
940e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
94175affc3bSjeremylt }
94275affc3bSjeremylt 
94375affc3bSjeremylt /**
944b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunction
945b7c9bbdaSJeremy L Thompson 
946ea61e9acSJeremy L Thompson   @param[in]  qf   CeedQFunction
947b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store Ceed
948b7c9bbdaSJeremy L Thompson 
949b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
950b7c9bbdaSJeremy L Thompson 
951b7c9bbdaSJeremy L Thompson   @ref Advanced
952b7c9bbdaSJeremy L Thompson **/
953b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
954b7c9bbdaSJeremy L Thompson   *ceed = qf->ceed;
955b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
956b7c9bbdaSJeremy L Thompson }
957b7c9bbdaSJeremy L Thompson 
958b7c9bbdaSJeremy L Thompson /**
959b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
960b11c1e72Sjeremylt 
961ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedQFunction as immutable.
962f04ea552SJeremy L Thompson 
963ea61e9acSJeremy L Thompson   @param[in]  qf CeedQFunction
964ea61e9acSJeremy L Thompson   @param[in]  Q  Number of quadrature points
96534138859Sjeremylt   @param[in]  u  Array of input CeedVectors
96634138859Sjeremylt   @param[out] v  Array of output CeedVectors
967b11c1e72Sjeremylt 
968b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
969dfdf5a53Sjeremylt 
9707a982d89SJeremy L. Thompson   @ref User
971b11c1e72Sjeremylt **/
9722b730f8bSJeremy L Thompson int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, CeedVector *u, CeedVector *v) {
9736574a04fSJeremy L Thompson   CeedCheck(qf->Apply, qf->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support QFunctionApply");
9746574a04fSJeremy L Thompson   CeedCheck(Q % qf->vec_length == 0, qf->ceed, CEED_ERROR_DIMENSION,
9756574a04fSJeremy L Thompson             "Number of quadrature points %" CeedInt_FMT " must be a multiple of %" CeedInt_FMT, Q, qf->vec_length);
976f04ea552SJeremy L Thompson   qf->is_immutable = true;
9772b730f8bSJeremy L Thompson   CeedCall(qf->Apply(qf, Q, u, v));
978e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
979d7b241e6Sjeremylt }
980d7b241e6Sjeremylt 
981b11c1e72Sjeremylt /**
982b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
983b11c1e72Sjeremylt 
984ea61e9acSJeremy L Thompson   @param[in,out] qf CeedQFunction to destroy
985b11c1e72Sjeremylt 
986b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
987dfdf5a53Sjeremylt 
9887a982d89SJeremy L. Thompson   @ref User
989b11c1e72Sjeremylt **/
990d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
991ad6481ceSJeremy L Thompson   if (!*qf || --(*qf)->ref_count > 0) {
992ad6481ceSJeremy L Thompson     *qf = NULL;
993ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
994ad6481ceSJeremy L Thompson   }
995fe2413ffSjeremylt   // Backend destroy
996d7b241e6Sjeremylt   if ((*qf)->Destroy) {
9972b730f8bSJeremy L Thompson     CeedCall((*qf)->Destroy(*qf));
998d7b241e6Sjeremylt   }
999fe2413ffSjeremylt   // Free fields
100092ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_input_fields; i++) {
10012b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->input_fields[i]).field_name));
10022b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->input_fields[i]));
1003fe2413ffSjeremylt   }
100492ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_output_fields; i++) {
10052b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->output_fields[i]).field_name));
10062b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->output_fields[i]));
1007fe2413ffSjeremylt   }
10082b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->input_fields));
10092b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->output_fields));
1010777ff853SJeremy L Thompson 
1011777ff853SJeremy L Thompson   // User context data object
10122b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&(*qf)->ctx));
1013fe2413ffSjeremylt 
10142b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->user_source));
10152b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->source_path));
10162b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->gallery_name));
10172b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->kernel_name));
10182b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*qf)->ceed));
10192b730f8bSJeremy L Thompson   CeedCall(CeedFree(qf));
1020e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1021d7b241e6Sjeremylt }
1022d7b241e6Sjeremylt 
1023d7b241e6Sjeremylt /// @}
1024