xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision 5f249b39e15f2bbae6856732ff8a25e57f5001b1)
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)) {
656574a04fSJeremy L Thompson   CeedCheck(num_qfunctions < sizeof(gallery_qfunctions) / sizeof(gallery_qfunctions[0]), NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
66c042f62fSJeremy L Thompson 
678ccf1006SJeremy L Thompson   CeedDebugEnv("Gallery Register: %s", name);
688ccf1006SJeremy L Thompson 
696eb0d8b4SJeremy L Thompson   const char *relative_file_path;
702b730f8bSJeremy L Thompson   CeedCall(CeedGetJitRelativePath(source, &relative_file_path));
716eb0d8b4SJeremy L Thompson 
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++;
80e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
81288c0443SJeremy L Thompson }
82288c0443SJeremy L Thompson 
83288c0443SJeremy L Thompson /**
847a982d89SJeremy L. Thompson   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
857a982d89SJeremy L. Thompson 
86ea61e9acSJeremy L Thompson   @param[out] f           CeedQFunctionField
87ea61e9acSJeremy L Thompson   @param[in]  field_name  Name of QFunction field
885330800fSSebastian Grimberg   @param[in]  size        Size of QFunction field, (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_WEIGHT,
895330800fSSebastian 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,
905330800fSSebastian Grimberg (num_comp * dim) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and
915330800fSSebastian Grimberg (num_comp * curl_dim) with curl_dim = 1 if dim < 3 else dim for @ref CEED_EVAL_CURL.
92ea61e9acSJeremy L Thompson   @param[in]  eval_mode   \ref CEED_EVAL_NONE to use values directly,
935330800fSSebastian Grimberg                             \ref CEED_EVAL_WEIGHT to use quadrature weights,
947a982d89SJeremy L. Thompson                             \ref CEED_EVAL_INTERP to use interpolated values,
957a982d89SJeremy L. Thompson                             \ref CEED_EVAL_GRAD to use gradients,
965330800fSSebastian Grimberg                             \ref CEED_EVAL_DIV to use divergence,
975330800fSSebastian Grimberg                             \ref CEED_EVAL_CURL to use curl.
987a982d89SJeremy L. Thompson 
997a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1007a982d89SJeremy L. Thompson 
1017a982d89SJeremy L. Thompson   @ref Developer
1027a982d89SJeremy L. Thompson **/
1032b730f8bSJeremy L Thompson static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
1042b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, f));
1052b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*f)->field_name));
1067a982d89SJeremy L. Thompson   (*f)->size      = size;
107d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
108e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1097a982d89SJeremy L. Thompson }
1107a982d89SJeremy L. Thompson 
1117a982d89SJeremy L. Thompson /**
1127a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1137a982d89SJeremy L. Thompson 
1147a982d89SJeremy L. Thompson   @param[in] field        QFunction field to view
115d1d35e2fSjeremylt   @param[in] field_number Number of field being viewed
1164c4400c7SValeria Barra   @param[in] in           true for input field, false for output
1177a982d89SJeremy L. Thompson   @param[in] stream       Stream to view to, e.g., stdout
1187a982d89SJeremy L. Thompson 
1197a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1207a982d89SJeremy L. Thompson 
1217a982d89SJeremy L. Thompson   @ref Utility
1227a982d89SJeremy L. Thompson **/
1232b730f8bSJeremy L Thompson static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt field_number, bool in, FILE *stream) {
1247a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1258229195eSjeremylt   char       *field_name;
1262b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldGetName(field, &field_name));
1278229195eSjeremylt   CeedInt size;
1282b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldGetSize(field, &size));
1298229195eSjeremylt   CeedEvalMode eval_mode;
1302b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldGetEvalMode(field, &eval_mode));
1312b730f8bSJeremy L Thompson   fprintf(stream,
1322b730f8bSJeremy L Thompson           "    %s field %" CeedInt_FMT
1332b730f8bSJeremy L Thompson           ":\n"
1347a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1352b730f8bSJeremy L Thompson           "      Size: %" CeedInt_FMT
1362b730f8bSJeremy L Thompson           "\n"
1377a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1388229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
139e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1407a982d89SJeremy L. Thompson }
1417a982d89SJeremy L. Thompson 
142777ff853SJeremy L Thompson /**
143777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
144777ff853SJeremy L Thompson 
145ea61e9acSJeremy L Thompson   @param[in,out] qf     CeedQFunction
146ea61e9acSJeremy L Thompson   @param[in]     status Boolean value to set as Fortran status
147777ff853SJeremy L Thompson 
148777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
149777ff853SJeremy L Thompson 
150777ff853SJeremy L Thompson   @ref Backend
151777ff853SJeremy L Thompson **/
152777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
153f04ea552SJeremy L Thompson   qf->is_fortran = status;
154e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
155777ff853SJeremy L Thompson }
156777ff853SJeremy L Thompson 
1577a982d89SJeremy L. Thompson /// @}
1587a982d89SJeremy L. Thompson 
1597a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1607a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1617a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1627a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1637a982d89SJeremy L. Thompson /// @{
1647a982d89SJeremy L. Thompson 
1657a982d89SJeremy L. Thompson /**
1667a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1677a982d89SJeremy L. Thompson 
168ea61e9acSJeremy L Thompson   @param[in]  qf         CeedQFunction
169d1d35e2fSjeremylt   @param[out] vec_length Variable to store vector length
1707a982d89SJeremy L. Thompson 
1717a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1727a982d89SJeremy L. Thompson 
1737a982d89SJeremy L. Thompson   @ref Backend
1747a982d89SJeremy L. Thompson **/
175d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
176d1d35e2fSjeremylt   *vec_length = qf->vec_length;
177e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1787a982d89SJeremy L. Thompson }
1797a982d89SJeremy L. Thompson 
1807a982d89SJeremy L. Thompson /**
1817a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
1827a982d89SJeremy L. Thompson 
183ea61e9acSJeremy L Thompson   @param[in]  qf         CeedQFunction
184d1d35e2fSjeremylt   @param[out] num_input  Variable to store number of input fields
185d1d35e2fSjeremylt   @param[out] num_output Variable to store number of output fields
1867a982d89SJeremy L. Thompson 
1877a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1887a982d89SJeremy L. Thompson 
1897a982d89SJeremy L. Thompson   @ref Backend
1907a982d89SJeremy L. Thompson **/
1912b730f8bSJeremy L Thompson int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, CeedInt *num_output) {
192d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
193d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
194e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1957a982d89SJeremy L. Thompson }
1967a982d89SJeremy L. Thompson 
1977a982d89SJeremy L. Thompson /**
19843e1b16fSJeremy L Thompson   @brief Get the name of the user function for a CeedQFunction
1997a982d89SJeremy L. Thompson 
200ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
20143e1b16fSJeremy L Thompson   @param[out] kernel_name Variable to store source path string
2027a982d89SJeremy L. Thompson 
2037a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2047a982d89SJeremy L. Thompson 
2057a982d89SJeremy L. Thompson   @ref Backend
2067a982d89SJeremy L. Thompson **/
20743e1b16fSJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, char **kernel_name) {
208ca5eadf8SJeremy L Thompson   if (!qf->kernel_name) {
209ca5eadf8SJeremy L Thompson     Ceed  ceed;
210ca5eadf8SJeremy L Thompson     char *kernel_name_copy;
2112b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
212ca5eadf8SJeremy L Thompson 
213ca5eadf8SJeremy L Thompson     if (qf->user_source) {
214ca5eadf8SJeremy L Thompson       const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
215ca5eadf8SJeremy L Thompson       size_t      kernel_name_len = strlen(kernel_name);
216ca5eadf8SJeremy L Thompson 
2172b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(kernel_name_len + 1, &kernel_name_copy));
218ca5eadf8SJeremy L Thompson       memcpy(kernel_name_copy, kernel_name, kernel_name_len);
219ca5eadf8SJeremy L Thompson     } else {
2202b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &kernel_name_copy));
221ca5eadf8SJeremy L Thompson     }
222ca5eadf8SJeremy L Thompson     qf->kernel_name = kernel_name_copy;
223ca5eadf8SJeremy L Thompson   }
224ca5eadf8SJeremy L Thompson 
22543e1b16fSJeremy L Thompson   *kernel_name = (char *)qf->kernel_name;
22643e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
22743e1b16fSJeremy L Thompson }
22843e1b16fSJeremy L Thompson 
22943e1b16fSJeremy L Thompson /**
23043e1b16fSJeremy L Thompson   @brief Get the source path string for a CeedQFunction
23143e1b16fSJeremy L Thompson 
232ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
23343e1b16fSJeremy L Thompson   @param[out] source_path Variable to store source path string
23443e1b16fSJeremy L Thompson 
23543e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
23643e1b16fSJeremy L Thompson 
23743e1b16fSJeremy L Thompson   @ref Backend
23843e1b16fSJeremy L Thompson **/
23943e1b16fSJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) {
240ca5eadf8SJeremy L Thompson   if (!qf->source_path && qf->user_source) {
241ca5eadf8SJeremy L Thompson     Ceed        ceed;
242ca5eadf8SJeremy L Thompson     bool        is_absolute_path;
243ca5eadf8SJeremy L Thompson     char       *absolute_path, *source_path_copy;
244ca5eadf8SJeremy L Thompson     const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
245ca5eadf8SJeremy L Thompson     size_t      kernel_name_len = strlen(kernel_name);
246ca5eadf8SJeremy L Thompson 
2472b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
248ca5eadf8SJeremy L Thompson 
2492b730f8bSJeremy L Thompson     CeedCall(CeedCheckFilePath(ceed, qf->user_source, &is_absolute_path));
250ca5eadf8SJeremy L Thompson     if (is_absolute_path) {
251ca5eadf8SJeremy L Thompson       absolute_path = (char *)qf->user_source;
252ca5eadf8SJeremy L Thompson     } else {
2532b730f8bSJeremy L Thompson       CeedCall(CeedGetJitAbsolutePath(ceed, qf->user_source, &absolute_path));
254ca5eadf8SJeremy L Thompson     }
255ca5eadf8SJeremy L Thompson 
256ca5eadf8SJeremy L Thompson     size_t source_len = strlen(absolute_path) - kernel_name_len - 1;
2572b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(source_len + 1, &source_path_copy));
258ca5eadf8SJeremy L Thompson     memcpy(source_path_copy, absolute_path, source_len);
259ca5eadf8SJeremy L Thompson     qf->source_path = source_path_copy;
260ca5eadf8SJeremy L Thompson 
2612b730f8bSJeremy L Thompson     if (!is_absolute_path) CeedCall(CeedFree(&absolute_path));
262ca5eadf8SJeremy L Thompson   }
263ca5eadf8SJeremy L Thompson 
26443e1b16fSJeremy L Thompson   *source_path = (char *)qf->source_path;
265e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2667a982d89SJeremy L. Thompson }
2677a982d89SJeremy L. Thompson 
2687a982d89SJeremy L. Thompson /**
269ea61e9acSJeremy L Thompson   @brief Initialize and load QFunction source file into string buffer, including full text of local files in place of `#include "local.h"`.
2704385fb7fSSebastian Grimberg 
2713d3250a0SJeremy L Thompson   The `buffer` is set to `NULL` if there is no QFunction source file.
2724385fb7fSSebastian Grimberg 
2733d3250a0SJeremy L Thompson   Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
2743d3250a0SJeremy L Thompson 
275ea61e9acSJeremy L Thompson   @param[in]  qf            CeedQFunction
276f74ec584SJeremy L Thompson   @param[out] source_buffer String buffer for source file contents
2773d3250a0SJeremy L Thompson 
2783d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2793d3250a0SJeremy L Thompson 
2803d3250a0SJeremy L Thompson   @ref Backend
2813d3250a0SJeremy L Thompson **/
2823d3250a0SJeremy L Thompson int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, char **source_buffer) {
2833d3250a0SJeremy L Thompson   char *source_path;
2843d3250a0SJeremy L Thompson 
2852b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetSourcePath(qf, &source_path));
2863d3250a0SJeremy L Thompson   *source_buffer = NULL;
2873d3250a0SJeremy L Thompson   if (source_path) {
2882b730f8bSJeremy L Thompson     CeedCall(CeedLoadSourceToBuffer(qf->ceed, source_path, source_buffer));
2893d3250a0SJeremy L Thompson   }
2903d3250a0SJeremy L Thompson 
2913d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2923d3250a0SJeremy L Thompson }
2933d3250a0SJeremy L Thompson 
2943d3250a0SJeremy L Thompson /**
2957a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2967a982d89SJeremy L. Thompson 
297ea61e9acSJeremy L Thompson   @param[in]  qf CeedQFunction
2987a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
2997a982d89SJeremy L. Thompson 
3007a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3017a982d89SJeremy L. Thompson 
3027a982d89SJeremy L. Thompson   @ref Backend
3037a982d89SJeremy L. Thompson **/
3047a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
3057a982d89SJeremy L. Thompson   *f = qf->function;
306e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3077a982d89SJeremy L. Thompson }
3087a982d89SJeremy L. Thompson 
3097a982d89SJeremy L. Thompson /**
310777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
3114385fb7fSSebastian Grimberg 
312ea61e9acSJeremy L Thompson   Note: For QFunctions from the Fortran interface, this function will return the Fortran context CeedQFunctionContext.
3137a982d89SJeremy L. Thompson 
314ea61e9acSJeremy L Thompson   @param[in]  qf  CeedQFunction
315777ff853SJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
3167a982d89SJeremy L. Thompson 
3177a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3187a982d89SJeremy L. Thompson 
3197a982d89SJeremy L. Thompson   @ref Backend
3207a982d89SJeremy L. Thompson **/
321777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
3227a982d89SJeremy L. Thompson   *ctx = qf->ctx;
323e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3247a982d89SJeremy L. Thompson }
3257a982d89SJeremy L. Thompson 
3267a982d89SJeremy L. Thompson /**
327441428dfSJeremy L Thompson   @brief Get context data of a CeedQFunction
328441428dfSJeremy L Thompson 
329ea61e9acSJeremy L Thompson   @param[in]  qf       CeedQFunction
330ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
331ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
332441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
333441428dfSJeremy L Thompson 
334441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
335441428dfSJeremy L Thompson 
336441428dfSJeremy L Thompson   @ref Backend
337441428dfSJeremy L Thompson **/
3382b730f8bSJeremy L Thompson int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
339441428dfSJeremy L Thompson   bool                 is_writable;
340441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
341441428dfSJeremy L Thompson 
3422b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
343441428dfSJeremy L Thompson   if (ctx) {
3442b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
345441428dfSJeremy L Thompson     if (is_writable) {
3462b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
347441428dfSJeremy L Thompson     } else {
3482b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
349441428dfSJeremy L Thompson     }
350441428dfSJeremy L Thompson   } else {
351441428dfSJeremy L Thompson     *(void **)data = NULL;
352441428dfSJeremy L Thompson   }
353441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
354441428dfSJeremy L Thompson }
355441428dfSJeremy L Thompson 
356441428dfSJeremy L Thompson /**
357441428dfSJeremy L Thompson   @brief Restore context data of a CeedQFunction
358441428dfSJeremy L Thompson 
359ea61e9acSJeremy L Thompson   @param[in]     qf   CeedQFunction
360ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
361441428dfSJeremy L Thompson 
362441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
363441428dfSJeremy L Thompson 
364441428dfSJeremy L Thompson   @ref Backend
365441428dfSJeremy L Thompson **/
366441428dfSJeremy L Thompson int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) {
367441428dfSJeremy L Thompson   bool                 is_writable;
368441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
369441428dfSJeremy L Thompson 
3702b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
371441428dfSJeremy L Thompson   if (ctx) {
3722b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
373441428dfSJeremy L Thompson     if (is_writable) {
3742b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
375441428dfSJeremy L Thompson     } else {
3762b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
377441428dfSJeremy L Thompson     }
378441428dfSJeremy L Thompson   }
379*5f249b39SJeremy L Thompson   *(void **)data = NULL;
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
3854385fb7fSSebastian Grimberg 
386ea61e9acSJeremy L Thompson   Note: For all QFunctions this function will return the user CeedQFunctionContext and not interface context CeedQFunctionContext, if any
387ea61e9acSJeremy L Thompson such object exists.
3887a982d89SJeremy L. Thompson 
389ea61e9acSJeremy L Thompson   @param[in]  qf  CeedQFunction
390777ff853SJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
3917a982d89SJeremy L. Thompson 
3927a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3937a982d89SJeremy L. Thompson   @ref Backend
3947a982d89SJeremy L. Thompson **/
395777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
396f04ea552SJeremy L Thompson   if (qf->is_fortran) {
397d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
3982b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx));
3998cb0412aSJeremy L Thompson     *ctx = fortran_ctx->inner_ctx;
4002b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx));
4017a982d89SJeremy L. Thompson   } else {
4027a982d89SJeremy L. Thompson     *ctx = qf->ctx;
4037a982d89SJeremy L. Thompson   }
404e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4057a982d89SJeremy L. Thompson }
4067a982d89SJeremy L. Thompson 
4077a982d89SJeremy L. Thompson /**
408441428dfSJeremy L Thompson   @brief Get inner context data of a CeedQFunction
409441428dfSJeremy L Thompson 
410ea61e9acSJeremy L Thompson   @param[in]  qf       CeedQFunction
411ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
412ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
413441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
414441428dfSJeremy L Thompson 
415441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
416441428dfSJeremy L Thompson 
417441428dfSJeremy L Thompson   @ref Backend
418441428dfSJeremy L Thompson **/
4192b730f8bSJeremy L Thompson int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
420441428dfSJeremy L Thompson   bool                 is_writable;
421441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
422441428dfSJeremy L Thompson 
4232b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
424441428dfSJeremy L Thompson   if (ctx) {
4252b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
426441428dfSJeremy L Thompson     if (is_writable) {
4272b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
428441428dfSJeremy L Thompson     } else {
4292b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
430441428dfSJeremy L Thompson     }
431441428dfSJeremy L Thompson   } else {
432441428dfSJeremy L Thompson     *(void **)data = NULL;
433441428dfSJeremy L Thompson   }
434441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
435441428dfSJeremy L Thompson }
436441428dfSJeremy L Thompson 
437441428dfSJeremy L Thompson /**
438441428dfSJeremy L Thompson   @brief Restore inner context data of a CeedQFunction
439441428dfSJeremy L Thompson 
440ea61e9acSJeremy L Thompson   @param[in]     qf   CeedQFunction
441ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
442441428dfSJeremy L Thompson 
443441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
444441428dfSJeremy L Thompson 
445441428dfSJeremy L Thompson   @ref Backend
446441428dfSJeremy L Thompson **/
447441428dfSJeremy L Thompson int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) {
448441428dfSJeremy L Thompson   bool                 is_writable;
449441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
450441428dfSJeremy L Thompson 
4512b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
452441428dfSJeremy L Thompson   if (ctx) {
4532b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
454441428dfSJeremy L Thompson     if (is_writable) {
4552b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
456441428dfSJeremy L Thompson     } else {
4572b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
458441428dfSJeremy L Thompson     }
459441428dfSJeremy L Thompson   }
460*5f249b39SJeremy L Thompson   *(void **)data = NULL;
461441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
462441428dfSJeremy L Thompson }
463441428dfSJeremy L Thompson 
464441428dfSJeremy L Thompson /**
4657a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
4667a982d89SJeremy L. Thompson 
467ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
468d1d35e2fSjeremylt   @param[out] is_identity Variable to store identity status
4697a982d89SJeremy L. Thompson 
4707a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4717a982d89SJeremy L. Thompson 
4727a982d89SJeremy L. Thompson   @ref Backend
4737a982d89SJeremy L. Thompson **/
474d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
475f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
476e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4777a982d89SJeremy L. Thompson }
4787a982d89SJeremy L. Thompson 
4797a982d89SJeremy L. Thompson /**
480441428dfSJeremy L Thompson   @brief Determine if QFunctionContext is writable
481441428dfSJeremy L Thompson 
482ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
483ea61e9acSJeremy L Thompson   @param[out] is_writable Variable to store context writeable status
484441428dfSJeremy L Thompson 
485441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
486441428dfSJeremy L Thompson 
487441428dfSJeremy L Thompson   @ref Backend
488441428dfSJeremy L Thompson **/
489441428dfSJeremy L Thompson int CeedQFunctionIsContextWritable(CeedQFunction qf, bool *is_writable) {
490441428dfSJeremy L Thompson   *is_writable = qf->is_context_writable;
491441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
492441428dfSJeremy L Thompson }
493441428dfSJeremy L Thompson 
494441428dfSJeremy L Thompson /**
4957a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
4967a982d89SJeremy L. Thompson 
497ea61e9acSJeremy L Thompson   @param[in]  qf   CeedQFunction
4987a982d89SJeremy L. Thompson   @param[out] data Variable to store data
4997a982d89SJeremy L. Thompson 
5007a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5017a982d89SJeremy L. Thompson 
5027a982d89SJeremy L. Thompson   @ref Backend
5037a982d89SJeremy L. Thompson **/
504777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
505777ff853SJeremy L Thompson   *(void **)data = qf->data;
506e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5077a982d89SJeremy L. Thompson }
5087a982d89SJeremy L. Thompson 
5097a982d89SJeremy L. Thompson /**
5107a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
5117a982d89SJeremy L. Thompson 
512ea61e9acSJeremy L Thompson   @param[in,out] qf   CeedQFunction
513ea61e9acSJeremy L Thompson   @param[in]     data Data to set
5147a982d89SJeremy L. Thompson 
5157a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5167a982d89SJeremy L. Thompson 
5177a982d89SJeremy L. Thompson   @ref Backend
5187a982d89SJeremy L. Thompson **/
519777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
520777ff853SJeremy L Thompson   qf->data = data;
521e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5227a982d89SJeremy L. Thompson }
5237a982d89SJeremy L. Thompson 
5247a982d89SJeremy L. Thompson /**
52534359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
52634359f16Sjeremylt 
527ea61e9acSJeremy L Thompson   @param[in,out] qf CeedQFunction to increment the reference counter
52834359f16Sjeremylt 
52934359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
53034359f16Sjeremylt 
53134359f16Sjeremylt   @ref Backend
53234359f16Sjeremylt **/
5339560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
53434359f16Sjeremylt   qf->ref_count++;
53534359f16Sjeremylt   return CEED_ERROR_SUCCESS;
53634359f16Sjeremylt }
53734359f16Sjeremylt 
5386e15d496SJeremy L Thompson /**
5396e15d496SJeremy L Thompson   @brief Estimate number of FLOPs per quadrature required to apply QFunction
5406e15d496SJeremy L Thompson 
541ea61e9acSJeremy L Thompson   @param[in]  qf    QFunction to estimate FLOPs for
542ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
5436e15d496SJeremy L Thompson 
5446e15d496SJeremy L Thompson   @ref Backend
5456e15d496SJeremy L Thompson **/
5469d36ca50SJeremy L Thompson int CeedQFunctionGetFlopsEstimate(CeedQFunction qf, CeedSize *flops) {
5476574a04fSJeremy L Thompson   CeedCheck(qf->user_flop_estimate > -1, qf->ceed, CEED_ERROR_INCOMPLETE, "Must set FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
5486e15d496SJeremy L Thompson   *flops = qf->user_flop_estimate;
5496e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5506e15d496SJeremy L Thompson }
5516e15d496SJeremy L Thompson 
5527a982d89SJeremy L. Thompson /// @}
5537a982d89SJeremy L. Thompson 
5547a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5557a982d89SJeremy L. Thompson /// CeedQFunction Public API
5567a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5577a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
5587a982d89SJeremy L. Thompson /// @{
5597a982d89SJeremy L. Thompson 
5607a982d89SJeremy L. Thompson /**
5617a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
5627a982d89SJeremy L. Thompson 
563ea61e9acSJeremy L Thompson   @param[in]  ceed       Ceed object where the CeedQFunction will be created
564ea61e9acSJeremy L Thompson   @param[in]  vec_length Vector length. Caller must ensure that number of quadrature points is a multiple of vec_length.
565ea61e9acSJeremy L Thompson   @param[in]  f          Function pointer to evaluate action at quadrature points.
5667a982d89SJeremy L. Thompson                            See \ref CeedQFunctionUser.
567ea61e9acSJeremy L Thompson   @param[in]  source     Absolute path to source of QFunction, "\abs_path\file.h:function_name".
568ea61e9acSJeremy L Thompson                            For support across all backends, this source must only contain constructs supported by C99, C++11, and CUDA.
569ea61e9acSJeremy L Thompson   @param[out] qf         Address of the variable where the newly created CeedQFunction will be stored
5707a982d89SJeremy L. Thompson 
5717a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5727a982d89SJeremy L. Thompson 
573ea61e9acSJeremy L Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's arguments.
5747a982d89SJeremy L. Thompson 
5757a982d89SJeremy L. Thompson   @ref User
5767a982d89SJeremy L. Thompson **/
5772b730f8bSJeremy L Thompson int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length, CeedQFunctionUser f, const char *source, CeedQFunction *qf) {
578ca5eadf8SJeremy L Thompson   char *user_source_copy;
5797a982d89SJeremy L. Thompson 
5807a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
5817a982d89SJeremy L. Thompson     Ceed delegate;
5826574a04fSJeremy L Thompson 
5832b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "QFunction"));
5846574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support QFunctionCreate");
5852b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf));
586e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
5877a982d89SJeremy L. Thompson   }
5887a982d89SJeremy L. Thompson 
5896574a04fSJeremy L Thompson   CeedCheck(!strlen(source) || strrchr(source, ':'), ceed, CEED_ERROR_INCOMPLETE,
5906574a04fSJeremy L Thompson             "Provided path to source does not include function name. Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"", source);
59143e1b16fSJeremy L Thompson 
5922b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, qf));
593db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*qf)->ceed));
594d1d35e2fSjeremylt   (*qf)->ref_count           = 1;
595d1d35e2fSjeremylt   (*qf)->vec_length          = vec_length;
596f04ea552SJeremy L Thompson   (*qf)->is_identity         = false;
597441428dfSJeremy L Thompson   (*qf)->is_context_writable = true;
5987a982d89SJeremy L. Thompson   (*qf)->function            = f;
5996e15d496SJeremy L Thompson   (*qf)->user_flop_estimate  = -1;
60043e1b16fSJeremy L Thompson   if (strlen(source)) {
601ca5eadf8SJeremy L Thompson     size_t user_source_len = strlen(source);
602ee5a26f2SJeremy L Thompson 
6032b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(user_source_len + 1, &user_source_copy));
604ca5eadf8SJeremy L Thompson     memcpy(user_source_copy, source, user_source_len);
605ca5eadf8SJeremy L Thompson     (*qf)->user_source = user_source_copy;
60643e1b16fSJeremy L Thompson   }
6072b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields));
6082b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields));
6092b730f8bSJeremy L Thompson   CeedCall(ceed->QFunctionCreate(*qf));
610e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6117a982d89SJeremy L. Thompson }
6127a982d89SJeremy L. Thompson 
6137a982d89SJeremy L. Thompson /**
614288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
615288c0443SJeremy L Thompson 
616ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedQFunction will be created
617ea61e9acSJeremy L Thompson   @param[in]  name Name of QFunction to use from gallery
618ea61e9acSJeremy L Thompson   @param[out] qf   Address of the variable where the newly created CeedQFunction will be stored
619288c0443SJeremy L Thompson 
620288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
621288c0443SJeremy L Thompson 
6227a982d89SJeremy L. Thompson   @ref User
623288c0443SJeremy L Thompson **/
6242b730f8bSJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, CeedQFunction *qf) {
625f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
626288c0443SJeremy L Thompson 
6272b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionRegisterAll());
628288c0443SJeremy L Thompson   // Find matching backend
6296574a04fSJeremy L Thompson   CeedCheck(name, ceed, CEED_ERROR_INCOMPLETE, "No QFunction name provided");
630288c0443SJeremy L Thompson   for (size_t i = 0; i < num_qfunctions; i++) {
631288c0443SJeremy L Thompson     size_t      n;
632d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
6332b730f8bSJeremy L Thompson     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {
6342b730f8bSJeremy L Thompson     }
635d1d35e2fSjeremylt     if (n > match_len) {
636d1d35e2fSjeremylt       match_len   = n;
637f7e22acaSJeremy L Thompson       match_index = i;
638288c0443SJeremy L Thompson     }
639288c0443SJeremy L Thompson   }
6406574a04fSJeremy L Thompson   CeedCheck(match_len > 0, ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
641288c0443SJeremy L Thompson 
642288c0443SJeremy L Thompson   // Create QFunction
6432b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInterior(ceed, gallery_qfunctions[match_index].vec_length, gallery_qfunctions[match_index].f,
6442b730f8bSJeremy L Thompson                                        gallery_qfunctions[match_index].source, qf));
645288c0443SJeremy L Thompson 
646288c0443SJeremy L Thompson   // QFunction specific setup
6472b730f8bSJeremy L Thompson   CeedCall(gallery_qfunctions[match_index].init(ceed, name, *qf));
648288c0443SJeremy L Thompson 
64975affc3bSjeremylt   // Copy name
6502b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name));
65143e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
652e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
653288c0443SJeremy L Thompson }
654288c0443SJeremy L Thompson 
655288c0443SJeremy L Thompson /**
656ea61e9acSJeremy L Thompson   @brief Create an identity CeedQFunction.
6574385fb7fSSebastian Grimberg 
658ea61e9acSJeremy L Thompson   Inputs are written into outputs in the order given.
659859c15bbSJames Wright   This is useful for CeedOperators that can be represented with only the action of a CeedElemRestriction and CeedBasis, such as restriction
6609fd66db6SSebastian Grimberg and prolongation operators for p-multigrid.
6619fd66db6SSebastian 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
6629fd66db6SSebastian Grimberg both.
6630219ea01SJeremy L Thompson 
664ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed object where the CeedQFunction will be created
665d1d35e2fSjeremylt   @param[in]  size     Size of the QFunction fields
666d1d35e2fSjeremylt   @param[in]  in_mode  CeedEvalMode for input to CeedQFunction
667d1d35e2fSjeremylt   @param[in]  out_mode CeedEvalMode for output to CeedQFunction
668ea61e9acSJeremy L Thompson   @param[out] qf       Address of the variable where the newly created CeedQFunction will be stored
6690219ea01SJeremy L Thompson 
6700219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
6710219ea01SJeremy L Thompson 
6727a982d89SJeremy L. Thompson   @ref User
6730219ea01SJeremy L Thompson **/
6742b730f8bSJeremy L Thompson int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, CeedEvalMode out_mode, CeedQFunction *qf) {
6752b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Identity", qf));
6762b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(*qf, "input", size, in_mode));
6772b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(*qf, "output", size, out_mode));
6780219ea01SJeremy L Thompson 
679f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
680547dbd6fSJeremy L Thompson 
681777ff853SJeremy L Thompson   CeedQFunctionContext  ctx;
6823668ca4bSJeremy L Thompson   CeedContextFieldLabel size_label;
6832b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(*qf, &ctx));
6842b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label));
6852b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetInt32(ctx, size_label, &size));
686547dbd6fSJeremy L Thompson 
687e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6880219ea01SJeremy L Thompson }
6890219ea01SJeremy L Thompson 
6900219ea01SJeremy L Thompson /**
691ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedQFunction.
6924385fb7fSSebastian Grimberg 
693ea61e9acSJeremy L Thompson   Both pointers should be destroyed with `CeedQFunctionDestroy()`.
694512bb800SJeremy L Thompson 
695512bb800SJeremy 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.
696ea61e9acSJeremy L Thompson         This CeedQFunction will be destroyed if `*qf_copy` is the only reference to this CeedQFunction.
6979560d06aSjeremylt 
698ea61e9acSJeremy L Thompson   @param[in]  qf      CeedQFunction to copy reference to
6999560d06aSjeremylt   @param[out] qf_copy Variable to store copied reference
7009560d06aSjeremylt 
7019560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7029560d06aSjeremylt 
7039560d06aSjeremylt   @ref User
7049560d06aSjeremylt **/
7059560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
7062b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionReference(qf));
7072b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(qf_copy));
7089560d06aSjeremylt   *qf_copy = qf;
7099560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7109560d06aSjeremylt }
7119560d06aSjeremylt 
7129560d06aSjeremylt /**
713a0a97fcfSJed Brown   @brief Add a CeedQFunction input
714b11c1e72Sjeremylt 
715ea61e9acSJeremy L Thompson   @param[in,out] qf         CeedQFunction
716ea61e9acSJeremy L Thompson   @param[in]     field_name Name of QFunction field
7175330800fSSebastian Grimberg   @param[in]     size       Size of QFunction field, (num_comp * 1) for @ref CEED_EVAL_NONE,
7185330800fSSebastian 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,
7195330800fSSebastian Grimberg (num_comp * dim) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and
7205330800fSSebastian Grimberg (num_comp * curl_dim) with curl_dim = 1 if dim < 3 else dim for @ref CEED_EVAL_CURL.
721ea61e9acSJeremy L Thompson   @param[in]     eval_mode  \ref CEED_EVAL_NONE to use values directly,
722b11c1e72Sjeremylt                               \ref CEED_EVAL_INTERP to use interpolated values,
7235330800fSSebastian Grimberg                               \ref CEED_EVAL_GRAD to use gradients,
7245330800fSSebastian Grimberg                               \ref CEED_EVAL_DIV to use divergence,
7255330800fSSebastian Grimberg                               \ref CEED_EVAL_CURL to use curl.
726b11c1e72Sjeremylt 
727b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
728dfdf5a53Sjeremylt 
7297a982d89SJeremy L. Thompson   @ref User
730b11c1e72Sjeremylt **/
7312b730f8bSJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
7326574a04fSJeremy L Thompson   CeedCheck(!qf->is_immutable, qf->ceed, CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
7336574a04fSJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT || size == 1, qf->ceed, CEED_ERROR_DIMENSION, "CEED_EVAL_WEIGHT should have size 1");
734643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
7356574a04fSJeremy L Thompson     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
736643fbb69SJeremy L Thompson   }
737643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
7386574a04fSJeremy L Thompson     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
739643fbb69SJeremy L Thompson   }
7402b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], field_name, size, eval_mode));
741d1d35e2fSjeremylt   qf->num_input_fields++;
742e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
743d7b241e6Sjeremylt }
744d7b241e6Sjeremylt 
745b11c1e72Sjeremylt /**
746a0a97fcfSJed Brown   @brief Add a CeedQFunction output
747b11c1e72Sjeremylt 
748ea61e9acSJeremy L Thompson   @param[in,out] qf         CeedQFunction
749ea61e9acSJeremy L Thompson   @param[in]     field_name Name of QFunction field
7505330800fSSebastian Grimberg   @param[in]     size       Size of QFunction field, (num_comp * 1) for @ref CEED_EVAL_NONE,
7515330800fSSebastian 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,
7525330800fSSebastian Grimberg (num_comp * dim) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and
7535330800fSSebastian Grimberg (num_comp * curl_dim) with curl_dim = 1 if dim < 3 else dim for @ref CEED_EVAL_CURL.
754ea61e9acSJeremy L Thompson   @param[in]     eval_mode  \ref CEED_EVAL_NONE to use values directly,
755b11c1e72Sjeremylt                               \ref CEED_EVAL_INTERP to use interpolated values,
7565330800fSSebastian Grimberg                               \ref CEED_EVAL_GRAD to use gradients,
7575330800fSSebastian Grimberg                               \ref CEED_EVAL_DIV to use divergence,
7585330800fSSebastian Grimberg                               \ref CEED_EVAL_CURL to use curl.
759b11c1e72Sjeremylt 
760b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
761dfdf5a53Sjeremylt 
7627a982d89SJeremy L. Thompson   @ref User
763b11c1e72Sjeremylt **/
7642b730f8bSJeremy L Thompson int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
7656574a04fSJeremy L Thompson   CeedCheck(!qf->is_immutable, qf->ceed, CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
7666574a04fSJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT, qf->ceed, CEED_ERROR_DIMENSION, "Cannot create QFunction output with CEED_EVAL_WEIGHT");
767643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
7686574a04fSJeremy L Thompson     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
769643fbb69SJeremy L Thompson   }
770643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
7716574a04fSJeremy L Thompson     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
772643fbb69SJeremy L Thompson   }
7732b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], field_name, size, eval_mode));
774d1d35e2fSjeremylt   qf->num_output_fields++;
775e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
776d7b241e6Sjeremylt }
777d7b241e6Sjeremylt 
778dfdf5a53Sjeremylt /**
77943bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
78043bbe138SJeremy L Thompson 
781ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedQFunction as immutable.
782f04ea552SJeremy L Thompson 
783ea61e9acSJeremy L Thompson   @param[in]  qf                CeedQFunction
784f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
785f74ec584SJeremy L Thompson   @param[out] input_fields      Variable to store input fields
786f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
787f74ec584SJeremy L Thompson   @param[out] output_fields     Variable to store output fields
78843bbe138SJeremy L Thompson 
78943bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
79043bbe138SJeremy L Thompson 
791e9b533fbSJeremy L Thompson   @ref Advanced
79243bbe138SJeremy L Thompson **/
7932b730f8bSJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields, CeedQFunctionField **input_fields, CeedInt *num_output_fields,
79443bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
795f04ea552SJeremy L Thompson   qf->is_immutable = true;
79643bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
79743bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
79843bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
79943bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
80043bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
80143bbe138SJeremy L Thompson }
80243bbe138SJeremy L Thompson 
80343bbe138SJeremy L Thompson /**
80443bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
80543bbe138SJeremy L Thompson 
806ea61e9acSJeremy L Thompson   @param[in]  qf_field   CeedQFunctionField
80743bbe138SJeremy L Thompson   @param[out] field_name Variable to store the field name
80843bbe138SJeremy L Thompson 
80943bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
81043bbe138SJeremy L Thompson 
811e9b533fbSJeremy L Thompson   @ref Advanced
81243bbe138SJeremy L Thompson **/
81343bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
81443bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
81543bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
81643bbe138SJeremy L Thompson }
81743bbe138SJeremy L Thompson 
81843bbe138SJeremy L Thompson /**
81943bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
82043bbe138SJeremy L Thompson 
821ea61e9acSJeremy L Thompson   @param[in]  qf_field CeedQFunctionField
82243bbe138SJeremy L Thompson   @param[out] size     Variable to store the size of the field
82343bbe138SJeremy L Thompson 
82443bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
82543bbe138SJeremy L Thompson 
826e9b533fbSJeremy L Thompson   @ref Advanced
82743bbe138SJeremy L Thompson **/
82843bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
82943bbe138SJeremy L Thompson   *size = qf_field->size;
83043bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
83143bbe138SJeremy L Thompson }
83243bbe138SJeremy L Thompson 
83343bbe138SJeremy L Thompson /**
83443bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
83543bbe138SJeremy L Thompson 
836ea61e9acSJeremy L Thompson   @param[in]  qf_field  CeedQFunctionField
83743bbe138SJeremy L Thompson   @param[out] eval_mode Variable to store the field evaluation mode
83843bbe138SJeremy L Thompson 
83943bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
84043bbe138SJeremy L Thompson 
841e9b533fbSJeremy L Thompson   @ref Advanced
84243bbe138SJeremy L Thompson **/
8432b730f8bSJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, CeedEvalMode *eval_mode) {
84443bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
84543bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
84643bbe138SJeremy L Thompson }
84743bbe138SJeremy L Thompson 
84843bbe138SJeremy L Thompson /**
8494ce2993fSjeremylt   @brief Set global context for a CeedQFunction
850b11c1e72Sjeremylt 
851ea61e9acSJeremy L Thompson   @param[in,out] qf  CeedQFunction
852ea61e9acSJeremy L Thompson   @param[in]     ctx Context data to set
853b11c1e72Sjeremylt 
854b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
855dfdf5a53Sjeremylt 
8567a982d89SJeremy L. Thompson   @ref User
857b11c1e72Sjeremylt **/
858777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
8592b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&qf->ctx));
860d7b241e6Sjeremylt   qf->ctx = ctx;
861db002c03SJeremy L Thompson   if (ctx) CeedCall(CeedQFunctionContextReference(ctx));
862e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
863d7b241e6Sjeremylt }
864d7b241e6Sjeremylt 
865b11c1e72Sjeremylt /**
866441428dfSJeremy L Thompson   @brief Set writability of CeedQFunctionContext when calling the `CeedQFunctionUser`.
8674385fb7fSSebastian Grimberg 
868859c15bbSJames Wright   The default value is `is_writable == true`.
869441428dfSJeremy L Thompson 
870ea61e9acSJeremy L Thompson   Setting `is_writable == true` indicates the `CeedQFunctionUser` writes into the CeedQFunctionContextData and requires memory syncronization
871441428dfSJeremy L Thompson after calling `CeedQFunctionApply()`.
872441428dfSJeremy L Thompson 
873ea61e9acSJeremy L Thompson   Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not modify the CeedQFunctionContextData.
874ea61e9acSJeremy L Thompson   Violating this assertion may lead to inconsistent data.
875441428dfSJeremy L Thompson 
876441428dfSJeremy L Thompson   Setting `is_writable == false` may offer a performance improvement on GPU backends.
877441428dfSJeremy L Thompson 
878ea61e9acSJeremy L Thompson   @param[in,out] qf          CeedQFunction
879ea61e9acSJeremy L Thompson   @param[in]     is_writable Writability status
880441428dfSJeremy L Thompson 
881441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
882441428dfSJeremy L Thompson 
883441428dfSJeremy L Thompson   @ref User
884441428dfSJeremy L Thompson **/
885441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
886441428dfSJeremy L Thompson   qf->is_context_writable = is_writable;
887441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
888441428dfSJeremy L Thompson }
889441428dfSJeremy L Thompson 
890441428dfSJeremy L Thompson /**
8916e15d496SJeremy L Thompson   @brief Set estimated number of FLOPs per quadrature required to apply QFunction
8926e15d496SJeremy L Thompson 
893ea61e9acSJeremy L Thompson   @param[in]  qf    QFunction to estimate FLOPs for
894ea61e9acSJeremy L Thompson   @param[out] flops FLOPs per quadrature point estimate
8956e15d496SJeremy L Thompson 
8966e15d496SJeremy L Thompson   @ref Backend
8976e15d496SJeremy L Thompson **/
8989d36ca50SJeremy L Thompson int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
8996574a04fSJeremy L Thompson   CeedCheck(flops >= 0, qf->ceed, CEED_ERROR_INCOMPATIBLE, "Must set non-negative FLOPs estimate");
9006e15d496SJeremy L Thompson   qf->user_flop_estimate = flops;
9016e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9026e15d496SJeremy L Thompson }
9036e15d496SJeremy L Thompson 
9046e15d496SJeremy L Thompson /**
90575affc3bSjeremylt   @brief View a CeedQFunction
90675affc3bSjeremylt 
90775affc3bSjeremylt   @param[in] qf     CeedQFunction to view
90875affc3bSjeremylt   @param[in] stream Stream to write; typically stdout/stderr or a file
90975affc3bSjeremylt 
91075affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
91175affc3bSjeremylt 
9127a982d89SJeremy L. Thompson   @ref User
91375affc3bSjeremylt **/
91475affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
915ca5eadf8SJeremy L Thompson   char *kernel_name;
91675affc3bSjeremylt 
9172b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetKernelName(qf, &kernel_name));
9182b730f8bSJeremy L Thompson   fprintf(stream, "%sCeedQFunction - %s\n", qf->is_gallery ? "Gallery " : "User ", qf->is_gallery ? qf->gallery_name : kernel_name);
91975affc3bSjeremylt 
9202b730f8bSJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " input field%s:\n", qf->num_input_fields, qf->num_input_fields > 1 ? "s" : "");
921d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
9222b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream));
92375affc3bSjeremylt   }
92475affc3bSjeremylt 
9252b730f8bSJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " output field%s:\n", qf->num_output_fields, qf->num_output_fields > 1 ? "s" : "");
926d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
9272b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream));
92875affc3bSjeremylt   }
929e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
93075affc3bSjeremylt }
93175affc3bSjeremylt 
93275affc3bSjeremylt /**
933b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunction
934b7c9bbdaSJeremy L Thompson 
935ea61e9acSJeremy L Thompson   @param[in]  qf   CeedQFunction
936b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store Ceed
937b7c9bbdaSJeremy L Thompson 
938b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
939b7c9bbdaSJeremy L Thompson 
940b7c9bbdaSJeremy L Thompson   @ref Advanced
941b7c9bbdaSJeremy L Thompson **/
942b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
943b7c9bbdaSJeremy L Thompson   *ceed = qf->ceed;
944b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
945b7c9bbdaSJeremy L Thompson }
946b7c9bbdaSJeremy L Thompson 
947b7c9bbdaSJeremy L Thompson /**
948b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
949b11c1e72Sjeremylt 
950ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedQFunction as immutable.
951f04ea552SJeremy L Thompson 
952ea61e9acSJeremy L Thompson   @param[in]  qf CeedQFunction
953ea61e9acSJeremy L Thompson   @param[in]  Q  Number of quadrature points
95434138859Sjeremylt   @param[in]  u  Array of input CeedVectors
95534138859Sjeremylt   @param[out] v  Array of output CeedVectors
956b11c1e72Sjeremylt 
957b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
958dfdf5a53Sjeremylt 
9597a982d89SJeremy L. Thompson   @ref User
960b11c1e72Sjeremylt **/
9612b730f8bSJeremy L Thompson int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, CeedVector *u, CeedVector *v) {
9626574a04fSJeremy L Thompson   CeedCheck(qf->Apply, qf->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support QFunctionApply");
9636574a04fSJeremy L Thompson   CeedCheck(Q % qf->vec_length == 0, qf->ceed, CEED_ERROR_DIMENSION,
9646574a04fSJeremy L Thompson             "Number of quadrature points %" CeedInt_FMT " must be a multiple of %" CeedInt_FMT, Q, qf->vec_length);
965f04ea552SJeremy L Thompson   qf->is_immutable = true;
9662b730f8bSJeremy L Thompson   CeedCall(qf->Apply(qf, Q, u, v));
967e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
968d7b241e6Sjeremylt }
969d7b241e6Sjeremylt 
970b11c1e72Sjeremylt /**
971b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
972b11c1e72Sjeremylt 
973ea61e9acSJeremy L Thompson   @param[in,out] qf CeedQFunction to destroy
974b11c1e72Sjeremylt 
975b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
976dfdf5a53Sjeremylt 
9777a982d89SJeremy L. Thompson   @ref User
978b11c1e72Sjeremylt **/
979d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
980ad6481ceSJeremy L Thompson   if (!*qf || --(*qf)->ref_count > 0) {
981ad6481ceSJeremy L Thompson     *qf = NULL;
982ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
983ad6481ceSJeremy L Thompson   }
984fe2413ffSjeremylt   // Backend destroy
985d7b241e6Sjeremylt   if ((*qf)->Destroy) {
9862b730f8bSJeremy L Thompson     CeedCall((*qf)->Destroy(*qf));
987d7b241e6Sjeremylt   }
988fe2413ffSjeremylt   // Free fields
98992ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_input_fields; i++) {
9902b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->input_fields[i]).field_name));
9912b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->input_fields[i]));
992fe2413ffSjeremylt   }
99392ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_output_fields; i++) {
9942b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->output_fields[i]).field_name));
9952b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->output_fields[i]));
996fe2413ffSjeremylt   }
9972b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->input_fields));
9982b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->output_fields));
999777ff853SJeremy L Thompson 
1000777ff853SJeremy L Thompson   // User context data object
10012b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&(*qf)->ctx));
1002fe2413ffSjeremylt 
10032b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->user_source));
10042b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->source_path));
10052b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->gallery_name));
10062b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->kernel_name));
10072b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*qf)->ceed));
10082b730f8bSJeremy L Thompson   CeedCall(CeedFree(qf));
1009e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1010d7b241e6Sjeremylt }
1011d7b241e6Sjeremylt 
1012d7b241e6Sjeremylt /// @}
1013