xref: /libCEED/interface/ceed-qfunction.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
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>
9*2b730f8bSJeremy L Thompson #include <ceed/backend.h>
10*2b730f8bSJeremy L Thompson #include <ceed/ceed.h>
11*2b730f8bSJeremy 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>
1543e1b16fSJeremy L Thompson #include <stdlib.h>
163d576824SJeremy L Thompson #include <string.h>
17288c0443SJeremy L Thompson 
187a982d89SJeremy L. Thompson /// @file
197a982d89SJeremy L. Thompson /// Implementation of public CeedQFunction interfaces
207a982d89SJeremy L. Thompson 
21288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP
22442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none;
23442e7f0bSjeremylt /// @endcond
24442e7f0bSjeremylt 
257a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
267a982d89SJeremy L. Thompson /// @{
277a982d89SJeremy L. Thompson 
287a982d89SJeremy L. Thompson // Indicate that no QFunction is provided by the user
297a982d89SJeremy L. Thompson const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
307a982d89SJeremy L. Thompson 
317a982d89SJeremy L. Thompson /// @}
327a982d89SJeremy L. Thompson 
33442e7f0bSjeremylt /// @cond DOXYGEN_SKIP
34288c0443SJeremy L Thompson static struct {
35288c0443SJeremy L Thompson   char              name[CEED_MAX_RESOURCE_LEN];
36288c0443SJeremy L Thompson   char              source[CEED_MAX_RESOURCE_LEN];
37d1d35e2fSjeremylt   CeedInt           vec_length;
38288c0443SJeremy L Thompson   CeedQFunctionUser f;
39288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
40d1d35e2fSjeremylt } gallery_qfunctions[1024];
41288c0443SJeremy L Thompson static size_t num_qfunctions;
42288c0443SJeremy L Thompson /// @endcond
43d7b241e6Sjeremylt 
44777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
45777ff853SJeremy L Thompson /// CeedQFunction Library Internal Functions
46777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
47777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper
48777ff853SJeremy L Thompson /// @{
49777ff853SJeremy L Thompson 
50b11c1e72Sjeremylt /**
51288c0443SJeremy L Thompson   @brief Register a gallery QFunction
52288c0443SJeremy L Thompson 
53288c0443SJeremy L Thompson   @param name        Name for this backend to respond to
541176cc3aSJeremy L Thompson   @param source      Absolute path to source of QFunction,
551176cc3aSJeremy L Thompson                        "\path\CEED_DIR\gallery\folder\file.h:function_name"
56d1d35e2fSjeremylt   @param vec_length  Vector length.  Caller must ensure that number of quadrature
57d1d35e2fSjeremylt                        points is a multiple of vec_length.
58288c0443SJeremy L Thompson   @param f           Function pointer to evaluate action at quadrature points.
59288c0443SJeremy L Thompson                        See \ref CeedQFunctionUser.
60288c0443SJeremy L Thompson   @param init        Initialization function called by CeedQFunctionInit() when the
61288c0443SJeremy L Thompson                        QFunction is selected.
62288c0443SJeremy L Thompson 
63288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
64288c0443SJeremy L Thompson 
657a982d89SJeremy L. Thompson   @ref Developer
66288c0443SJeremy L Thompson **/
67*2b730f8bSJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source, CeedInt vec_length, CeedQFunctionUser f,
68288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
69*2b730f8bSJeremy L Thompson   if (num_qfunctions >= sizeof(gallery_qfunctions) / sizeof(gallery_qfunctions[0])) {
70c042f62fSJeremy L Thompson     // LCOV_EXCL_START
71e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
72c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
73*2b730f8bSJeremy L Thompson   }
74c042f62fSJeremy L Thompson 
758ccf1006SJeremy L Thompson   CeedDebugEnv("Gallery Register: %s", name);
768ccf1006SJeremy L Thompson 
776eb0d8b4SJeremy L Thompson   const char *relative_file_path;
78*2b730f8bSJeremy L Thompson   CeedCall(CeedGetJitRelativePath(source, &relative_file_path));
796eb0d8b4SJeremy L Thompson 
80d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
81d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN - 1] = 0;
82*2b730f8bSJeremy L Thompson   strncpy(gallery_qfunctions[num_qfunctions].source, relative_file_path, CEED_MAX_RESOURCE_LEN);
83d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN - 1] = 0;
84d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].vec_length                        = vec_length;
85d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].f                                 = f;
86d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].init                              = init;
87288c0443SJeremy L Thompson   num_qfunctions++;
88e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
89288c0443SJeremy L Thompson }
90288c0443SJeremy L Thompson 
91288c0443SJeremy L Thompson /**
927a982d89SJeremy L. Thompson   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
937a982d89SJeremy L. Thompson 
947a982d89SJeremy L. Thompson   @param f           CeedQFunctionField
95d1d35e2fSjeremylt   @param field_name  Name of QFunction field
96d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
97d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT
98d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
997a982d89SJeremy L. Thompson                        \ref CEED_EVAL_INTERP to use interpolated values,
1007a982d89SJeremy L. Thompson                        \ref CEED_EVAL_GRAD to use gradients,
1017a982d89SJeremy L. Thompson                        \ref CEED_EVAL_WEIGHT to use quadrature weights.
1027a982d89SJeremy L. Thompson 
1037a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1047a982d89SJeremy L. Thompson 
1057a982d89SJeremy L. Thompson   @ref Developer
1067a982d89SJeremy L. Thompson **/
107*2b730f8bSJeremy L Thompson static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
108*2b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, f));
109*2b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*f)->field_name));
1107a982d89SJeremy L. Thompson   (*f)->size      = size;
111d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
112e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1137a982d89SJeremy L. Thompson }
1147a982d89SJeremy L. Thompson 
1157a982d89SJeremy L. Thompson /**
1167a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1177a982d89SJeremy L. Thompson 
1187a982d89SJeremy L. Thompson   @param[in] field         QFunction field to view
119d1d35e2fSjeremylt   @param[in] field_number  Number of field being viewed
1204c4400c7SValeria Barra   @param[in] in            true for input field, false for output
1217a982d89SJeremy L. Thompson   @param[in] stream        Stream to view to, e.g., stdout
1227a982d89SJeremy L. Thompson 
1237a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1247a982d89SJeremy L. Thompson 
1257a982d89SJeremy L. Thompson   @ref Utility
1267a982d89SJeremy L. Thompson **/
127*2b730f8bSJeremy L Thompson static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt field_number, bool in, FILE *stream) {
1287a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1298229195eSjeremylt   char       *field_name;
130*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldGetName(field, &field_name));
1318229195eSjeremylt   CeedInt size;
132*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldGetSize(field, &size));
1338229195eSjeremylt   CeedEvalMode eval_mode;
134*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldGetEvalMode(field, &eval_mode));
135*2b730f8bSJeremy L Thompson   fprintf(stream,
136*2b730f8bSJeremy L Thompson           "    %s field %" CeedInt_FMT
137*2b730f8bSJeremy L Thompson           ":\n"
1387a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
139*2b730f8bSJeremy L Thompson           "      Size: %" CeedInt_FMT
140*2b730f8bSJeremy L Thompson           "\n"
1417a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1428229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
143e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1447a982d89SJeremy L. Thompson }
1457a982d89SJeremy L. Thompson 
146777ff853SJeremy L Thompson /**
147777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
148777ff853SJeremy L Thompson 
149777ff853SJeremy L Thompson   @param qf      CeedQFunction
150777ff853SJeremy L Thompson   @param status  Boolean value to set as Fortran status
151777ff853SJeremy L Thompson 
152777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
153777ff853SJeremy L Thompson 
154777ff853SJeremy L Thompson   @ref Backend
155777ff853SJeremy L Thompson **/
156777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
157f04ea552SJeremy L Thompson   qf->is_fortran = status;
158e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
159777ff853SJeremy L Thompson }
160777ff853SJeremy L Thompson 
1617a982d89SJeremy L. Thompson /// @}
1627a982d89SJeremy L. Thompson 
1637a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1647a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1657a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1667a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1677a982d89SJeremy L. Thompson /// @{
1687a982d89SJeremy L. Thompson 
1697a982d89SJeremy L. Thompson /**
1707a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1717a982d89SJeremy L. Thompson 
1727a982d89SJeremy L. Thompson   @param qf               CeedQFunction
173d1d35e2fSjeremylt   @param[out] vec_length  Variable to store vector length
1747a982d89SJeremy L. Thompson 
1757a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1767a982d89SJeremy L. Thompson 
1777a982d89SJeremy L. Thompson   @ref Backend
1787a982d89SJeremy L. Thompson **/
179d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
180d1d35e2fSjeremylt   *vec_length = qf->vec_length;
181e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1827a982d89SJeremy L. Thompson }
1837a982d89SJeremy L. Thompson 
1847a982d89SJeremy L. Thompson /**
1857a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
1867a982d89SJeremy L. Thompson 
1877a982d89SJeremy L. Thompson   @param qf               CeedQFunction
188d1d35e2fSjeremylt   @param[out] num_input   Variable to store number of input fields
189d1d35e2fSjeremylt   @param[out] num_output  Variable to store number of output fields
1907a982d89SJeremy L. Thompson 
1917a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1927a982d89SJeremy L. Thompson 
1937a982d89SJeremy L. Thompson   @ref Backend
1947a982d89SJeremy L. Thompson **/
195*2b730f8bSJeremy L Thompson int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, CeedInt *num_output) {
196d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
197d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
198e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1997a982d89SJeremy L. Thompson }
2007a982d89SJeremy L. Thompson 
2017a982d89SJeremy L. Thompson /**
20243e1b16fSJeremy L Thompson   @brief Get the name of the user function for a CeedQFunction
2037a982d89SJeremy L. Thompson 
2047a982d89SJeremy L. Thompson   @param qf                CeedQFunction
20543e1b16fSJeremy L Thompson   @param[out] kernel_name  Variable to store source path string
2067a982d89SJeremy L. Thompson 
2077a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2087a982d89SJeremy L. Thompson 
2097a982d89SJeremy L. Thompson   @ref Backend
2107a982d89SJeremy L. Thompson **/
21143e1b16fSJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, char **kernel_name) {
212ca5eadf8SJeremy L Thompson   if (!qf->kernel_name) {
213ca5eadf8SJeremy L Thompson     Ceed  ceed;
214ca5eadf8SJeremy L Thompson     char *kernel_name_copy;
215*2b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
216ca5eadf8SJeremy L Thompson 
217ca5eadf8SJeremy L Thompson     if (qf->user_source) {
218ca5eadf8SJeremy L Thompson       const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
219ca5eadf8SJeremy L Thompson       size_t      kernel_name_len = strlen(kernel_name);
220ca5eadf8SJeremy L Thompson 
221*2b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(kernel_name_len + 1, &kernel_name_copy));
222ca5eadf8SJeremy L Thompson       memcpy(kernel_name_copy, kernel_name, kernel_name_len);
223ca5eadf8SJeremy L Thompson     } else {
224*2b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &kernel_name_copy));
225ca5eadf8SJeremy L Thompson     }
226ca5eadf8SJeremy L Thompson     qf->kernel_name = kernel_name_copy;
227ca5eadf8SJeremy L Thompson   }
228ca5eadf8SJeremy L Thompson 
22943e1b16fSJeremy L Thompson   *kernel_name = (char *)qf->kernel_name;
23043e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
23143e1b16fSJeremy L Thompson }
23243e1b16fSJeremy L Thompson 
23343e1b16fSJeremy L Thompson /**
23443e1b16fSJeremy L Thompson   @brief Get the source path string for a CeedQFunction
23543e1b16fSJeremy L Thompson 
23643e1b16fSJeremy L Thompson   @param qf                CeedQFunction
23743e1b16fSJeremy L Thompson   @param[out] source_path  Variable to store source path string
23843e1b16fSJeremy L Thompson 
23943e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
24043e1b16fSJeremy L Thompson 
24143e1b16fSJeremy L Thompson   @ref Backend
24243e1b16fSJeremy L Thompson **/
24343e1b16fSJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) {
244ca5eadf8SJeremy L Thompson   if (!qf->source_path && qf->user_source) {
245ca5eadf8SJeremy L Thompson     Ceed        ceed;
246ca5eadf8SJeremy L Thompson     bool        is_absolute_path;
247ca5eadf8SJeremy L Thompson     char       *absolute_path, *source_path_copy;
248ca5eadf8SJeremy L Thompson     const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
249ca5eadf8SJeremy L Thompson     size_t      kernel_name_len = strlen(kernel_name);
250ca5eadf8SJeremy L Thompson 
251*2b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
252ca5eadf8SJeremy L Thompson 
253*2b730f8bSJeremy L Thompson     CeedCall(CeedCheckFilePath(ceed, qf->user_source, &is_absolute_path));
254ca5eadf8SJeremy L Thompson     if (is_absolute_path) {
255ca5eadf8SJeremy L Thompson       absolute_path = (char *)qf->user_source;
256ca5eadf8SJeremy L Thompson     } else {
257*2b730f8bSJeremy L Thompson       CeedCall(CeedGetJitAbsolutePath(ceed, qf->user_source, &absolute_path));
258ca5eadf8SJeremy L Thompson     }
259ca5eadf8SJeremy L Thompson 
260ca5eadf8SJeremy L Thompson     size_t source_len = strlen(absolute_path) - kernel_name_len - 1;
261*2b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(source_len + 1, &source_path_copy));
262ca5eadf8SJeremy L Thompson     memcpy(source_path_copy, absolute_path, source_len);
263ca5eadf8SJeremy L Thompson     qf->source_path = source_path_copy;
264ca5eadf8SJeremy L Thompson 
265*2b730f8bSJeremy L Thompson     if (!is_absolute_path) CeedCall(CeedFree(&absolute_path));
266ca5eadf8SJeremy L Thompson   }
267ca5eadf8SJeremy L Thompson 
26843e1b16fSJeremy L Thompson   *source_path = (char *)qf->source_path;
269e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2707a982d89SJeremy L. Thompson }
2717a982d89SJeremy L. Thompson 
2727a982d89SJeremy L. Thompson /**
2733d3250a0SJeremy L Thompson   @brief Initalize and load QFunction source file into string buffer, including
2743d3250a0SJeremy L Thompson            full text of local files in place of `#include "local.h"`.
2753d3250a0SJeremy L Thompson            The `buffer` is set to `NULL` if there is no QFunction source file.
2763d3250a0SJeremy L Thompson          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
2773d3250a0SJeremy L Thompson 
2783d3250a0SJeremy L Thompson   @param qf                  CeedQFunction
279f74ec584SJeremy L Thompson   @param[out] source_buffer  String buffer for source file contents
2803d3250a0SJeremy L Thompson 
2813d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2823d3250a0SJeremy L Thompson 
2833d3250a0SJeremy L Thompson   @ref Backend
2843d3250a0SJeremy L Thompson **/
2853d3250a0SJeremy L Thompson int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, char **source_buffer) {
2863d3250a0SJeremy L Thompson   char *source_path;
2873d3250a0SJeremy L Thompson 
288*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetSourcePath(qf, &source_path));
2893d3250a0SJeremy L Thompson   *source_buffer = NULL;
2903d3250a0SJeremy L Thompson   if (source_path) {
291*2b730f8bSJeremy L Thompson     CeedCall(CeedLoadSourceToBuffer(qf->ceed, source_path, source_buffer));
2923d3250a0SJeremy L Thompson   }
2933d3250a0SJeremy L Thompson 
2943d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2953d3250a0SJeremy L Thompson }
2963d3250a0SJeremy L Thompson 
2973d3250a0SJeremy L Thompson /**
2987a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2997a982d89SJeremy L. Thompson 
3007a982d89SJeremy L. Thompson   @param qf      CeedQFunction
3017a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
3027a982d89SJeremy L. Thompson 
3037a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3047a982d89SJeremy L. Thompson 
3057a982d89SJeremy L. Thompson   @ref Backend
3067a982d89SJeremy L. Thompson **/
3077a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
3087a982d89SJeremy L. Thompson   *f = qf->function;
309e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3107a982d89SJeremy L. Thompson }
3117a982d89SJeremy L. Thompson 
3127a982d89SJeremy L. Thompson /**
313777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
314777ff853SJeremy L Thompson            Note: For QFunctions from the Fortran interface, this
315777ff853SJeremy L Thompson              function will return the Fortran context
316777ff853SJeremy L Thompson              CeedQFunctionContext.
3177a982d89SJeremy L. Thompson 
3187a982d89SJeremy L. Thompson   @param qf        CeedQFunction
319777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
3207a982d89SJeremy L. Thompson 
3217a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3227a982d89SJeremy L. Thompson 
3237a982d89SJeremy L. Thompson   @ref Backend
3247a982d89SJeremy L. Thompson **/
325777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
3267a982d89SJeremy L. Thompson   *ctx = qf->ctx;
327e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3287a982d89SJeremy L. Thompson }
3297a982d89SJeremy L. Thompson 
3307a982d89SJeremy L. Thompson /**
331441428dfSJeremy L Thompson   @brief Get context data of a CeedQFunction
332441428dfSJeremy L Thompson 
333441428dfSJeremy L Thompson   @param qf         CeedQFunction
334441428dfSJeremy L Thompson   @param mem_type   Memory type on which to access the data. If the backend
335441428dfSJeremy L Thompson                       uses a different memory type, this will perform a copy.
336441428dfSJeremy L Thompson   @param[out] data  Data on memory type mem_type
337441428dfSJeremy L Thompson 
338441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
339441428dfSJeremy L Thompson 
340441428dfSJeremy L Thompson   @ref Backend
341441428dfSJeremy L Thompson **/
342*2b730f8bSJeremy L Thompson int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
343441428dfSJeremy L Thompson   bool                 is_writable;
344441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
345441428dfSJeremy L Thompson 
346*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
347441428dfSJeremy L Thompson   if (ctx) {
348*2b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
349441428dfSJeremy L Thompson     if (is_writable) {
350*2b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
351441428dfSJeremy L Thompson     } else {
352*2b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
353441428dfSJeremy L Thompson     }
354441428dfSJeremy L Thompson   } else {
355441428dfSJeremy L Thompson     *(void **)data = NULL;
356441428dfSJeremy L Thompson   }
357441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
358441428dfSJeremy L Thompson }
359441428dfSJeremy L Thompson 
360441428dfSJeremy L Thompson /**
361441428dfSJeremy L Thompson   @brief Restore context data of a CeedQFunction
362441428dfSJeremy L Thompson 
363441428dfSJeremy L Thompson   @param qf    CeedQFunction
364441428dfSJeremy L Thompson   @param data  Data to restore
365441428dfSJeremy L Thompson 
366441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
367441428dfSJeremy L Thompson 
368441428dfSJeremy L Thompson   @ref Backend
369441428dfSJeremy L Thompson **/
370441428dfSJeremy L Thompson int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) {
371441428dfSJeremy L Thompson   bool                 is_writable;
372441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
373441428dfSJeremy L Thompson 
374*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
375441428dfSJeremy L Thompson   if (ctx) {
376*2b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
377441428dfSJeremy L Thompson     if (is_writable) {
378*2b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
379441428dfSJeremy L Thompson     } else {
380*2b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
381441428dfSJeremy L Thompson     }
382441428dfSJeremy L Thompson   }
383441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
384441428dfSJeremy L Thompson }
385441428dfSJeremy L Thompson 
386441428dfSJeremy L Thompson /**
3877a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
388777ff853SJeremy L Thompson            Note: For all QFunctions this function will return the user
389777ff853SJeremy L Thompson              CeedQFunctionContext and not interface context
390777ff853SJeremy L Thompson              CeedQFunctionContext, if any such object exists.
3917a982d89SJeremy L. Thompson 
3927a982d89SJeremy L. Thompson   @param qf        CeedQFunction
393777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
3947a982d89SJeremy L. Thompson 
3957a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3967a982d89SJeremy L. Thompson   @ref Backend
3977a982d89SJeremy L. Thompson **/
398777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
399f04ea552SJeremy L Thompson   if (qf->is_fortran) {
400d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
401*2b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx));
4028cb0412aSJeremy L Thompson     *ctx = fortran_ctx->inner_ctx;
403*2b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx));
4047a982d89SJeremy L. Thompson   } else {
4057a982d89SJeremy L. Thompson     *ctx = qf->ctx;
4067a982d89SJeremy L. Thompson   }
407e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4087a982d89SJeremy L. Thompson }
4097a982d89SJeremy L. Thompson 
4107a982d89SJeremy L. Thompson /**
411441428dfSJeremy L Thompson   @brief Get inner context data of a CeedQFunction
412441428dfSJeremy L Thompson 
413441428dfSJeremy L Thompson   @param qf         CeedQFunction
414441428dfSJeremy L Thompson   @param mem_type   Memory type on which to access the data. If the backend
415441428dfSJeremy L Thompson                       uses a different memory type, this will perform a copy.
416441428dfSJeremy L Thompson   @param[out] data  Data on memory type mem_type
417441428dfSJeremy L Thompson 
418441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
419441428dfSJeremy L Thompson 
420441428dfSJeremy L Thompson   @ref Backend
421441428dfSJeremy L Thompson **/
422*2b730f8bSJeremy L Thompson int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
423441428dfSJeremy L Thompson   bool                 is_writable;
424441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
425441428dfSJeremy L Thompson 
426*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
427441428dfSJeremy L Thompson   if (ctx) {
428*2b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
429441428dfSJeremy L Thompson     if (is_writable) {
430*2b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
431441428dfSJeremy L Thompson     } else {
432*2b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
433441428dfSJeremy L Thompson     }
434441428dfSJeremy L Thompson   } else {
435441428dfSJeremy L Thompson     *(void **)data = NULL;
436441428dfSJeremy L Thompson   }
437441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
438441428dfSJeremy L Thompson }
439441428dfSJeremy L Thompson 
440441428dfSJeremy L Thompson /**
441441428dfSJeremy L Thompson   @brief Restore inner context data of a CeedQFunction
442441428dfSJeremy L Thompson 
443441428dfSJeremy L Thompson   @param qf    CeedQFunction
444441428dfSJeremy L Thompson   @param data  Data to restore
445441428dfSJeremy L Thompson 
446441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
447441428dfSJeremy L Thompson 
448441428dfSJeremy L Thompson   @ref Backend
449441428dfSJeremy L Thompson **/
450441428dfSJeremy L Thompson int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) {
451441428dfSJeremy L Thompson   bool                 is_writable;
452441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
453441428dfSJeremy L Thompson 
454*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
455441428dfSJeremy L Thompson   if (ctx) {
456*2b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
457441428dfSJeremy L Thompson     if (is_writable) {
458*2b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
459441428dfSJeremy L Thompson     } else {
460*2b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
461441428dfSJeremy L Thompson     }
462441428dfSJeremy L Thompson   }
463441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
464441428dfSJeremy L Thompson }
465441428dfSJeremy L Thompson 
466441428dfSJeremy L Thompson /**
4677a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
4687a982d89SJeremy L. Thompson 
4697a982d89SJeremy L. Thompson   @param qf                CeedQFunction
470d1d35e2fSjeremylt   @param[out] is_identity  Variable to store identity status
4717a982d89SJeremy L. Thompson 
4727a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4737a982d89SJeremy L. Thompson 
4747a982d89SJeremy L. Thompson   @ref Backend
4757a982d89SJeremy L. Thompson **/
476d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
477f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
478e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4797a982d89SJeremy L. Thompson }
4807a982d89SJeremy L. Thompson 
4817a982d89SJeremy L. Thompson /**
482441428dfSJeremy L Thompson   @brief Determine if QFunctionContext is writable
483441428dfSJeremy L Thompson 
484441428dfSJeremy L Thompson   @param qf                CeedQFunction
485441428dfSJeremy L Thompson   @param[out] is_writable  Variable to store context writeable staus
486441428dfSJeremy L Thompson 
487441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
488441428dfSJeremy L Thompson 
489441428dfSJeremy L Thompson   @ref Backend
490441428dfSJeremy L Thompson **/
491441428dfSJeremy L Thompson int CeedQFunctionIsContextWritable(CeedQFunction qf, bool *is_writable) {
492441428dfSJeremy L Thompson   *is_writable = qf->is_context_writable;
493441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
494441428dfSJeremy L Thompson }
495441428dfSJeremy L Thompson 
496441428dfSJeremy L Thompson /**
4977a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
4987a982d89SJeremy L. Thompson 
4997a982d89SJeremy L. Thompson   @param qf         CeedQFunction
5007a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
5017a982d89SJeremy L. Thompson 
5027a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5037a982d89SJeremy L. Thompson 
5047a982d89SJeremy L. Thompson   @ref Backend
5057a982d89SJeremy L. Thompson **/
506777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
507777ff853SJeremy L Thompson   *(void **)data = qf->data;
508e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5097a982d89SJeremy L. Thompson }
5107a982d89SJeremy L. Thompson 
5117a982d89SJeremy L. Thompson /**
5127a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
5137a982d89SJeremy L. Thompson 
5147a982d89SJeremy L. Thompson   @param[out] qf  CeedQFunction
5157a982d89SJeremy L. Thompson   @param data     Data to set
5167a982d89SJeremy L. Thompson 
5177a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5187a982d89SJeremy L. Thompson 
5197a982d89SJeremy L. Thompson   @ref Backend
5207a982d89SJeremy L. Thompson **/
521777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
522777ff853SJeremy L Thompson   qf->data = data;
523e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5247a982d89SJeremy L. Thompson }
5257a982d89SJeremy L. Thompson 
5267a982d89SJeremy L. Thompson /**
52734359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
52834359f16Sjeremylt 
52934359f16Sjeremylt   @param qf  CeedQFunction to increment the reference counter
53034359f16Sjeremylt 
53134359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
53234359f16Sjeremylt 
53334359f16Sjeremylt   @ref Backend
53434359f16Sjeremylt **/
5359560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
53634359f16Sjeremylt   qf->ref_count++;
53734359f16Sjeremylt   return CEED_ERROR_SUCCESS;
53834359f16Sjeremylt }
53934359f16Sjeremylt 
5406e15d496SJeremy L Thompson /**
5416e15d496SJeremy L Thompson   @brief Estimate number of FLOPs per quadrature required to apply QFunction
5426e15d496SJeremy L Thompson 
5436e15d496SJeremy L Thompson   @param qf    QFunction to estimate FLOPs for
5446e15d496SJeremy L Thompson   @param flops Address of variable to hold FLOPs estimate
5456e15d496SJeremy L Thompson 
5466e15d496SJeremy L Thompson   @ref Backend
5476e15d496SJeremy L Thompson **/
5489d36ca50SJeremy L Thompson int CeedQFunctionGetFlopsEstimate(CeedQFunction qf, CeedSize *flops) {
549*2b730f8bSJeremy L Thompson   if (qf->user_flop_estimate == -1) {
5506e15d496SJeremy L Thompson     // LCOV_EXCL_START
551*2b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_INCOMPLETE, "Must set FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
5526e15d496SJeremy L Thompson     // LCOV_EXCL_STOP
553*2b730f8bSJeremy L Thompson   }
5546e15d496SJeremy L Thompson   *flops = qf->user_flop_estimate;
5556e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5566e15d496SJeremy L Thompson }
5576e15d496SJeremy L Thompson 
5587a982d89SJeremy L. Thompson /// @}
5597a982d89SJeremy L. Thompson 
5607a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5617a982d89SJeremy L. Thompson /// CeedQFunction Public API
5627a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5637a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
5647a982d89SJeremy L. Thompson /// @{
5657a982d89SJeremy L. Thompson 
5667a982d89SJeremy L. Thompson /**
5677a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
5687a982d89SJeremy L. Thompson 
5697a982d89SJeremy L. Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
570d1d35e2fSjeremylt   @param vec_length  Vector length. Caller must ensure that number of quadrature
571d1d35e2fSjeremylt                        points is a multiple of vec_length.
5727a982d89SJeremy L. Thompson   @param f           Function pointer to evaluate action at quadrature points.
5737a982d89SJeremy L. Thompson                        See \ref CeedQFunctionUser.
5747a982d89SJeremy L. Thompson   @param source      Absolute path to source of QFunction,
575c8d5b03cSJeremy L Thompson                        "\abs_path\file.h:function_name".
576c8d5b03cSJeremy L Thompson                        For support across all backends, this source must only
577c8d5b03cSJeremy L Thompson                        contain constructs supported by C99, C++11, and CUDA.
5787a982d89SJeremy L. Thompson   @param[out] qf     Address of the variable where the newly created
5797a982d89SJeremy L. Thompson                        CeedQFunction will be stored
5807a982d89SJeremy L. Thompson 
5817a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5827a982d89SJeremy L. Thompson 
5837a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
5847a982d89SJeremy L. Thompson     arguments.
5857a982d89SJeremy L. Thompson 
5867a982d89SJeremy L. Thompson   @ref User
5877a982d89SJeremy L. Thompson **/
588*2b730f8bSJeremy 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;
593*2b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "QFunction"));
5947a982d89SJeremy L. Thompson 
595*2b730f8bSJeremy L Thompson     if (!delegate) {
5967a982d89SJeremy L. Thompson       // LCOV_EXCL_START
597*2b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support QFunctionCreate");
5987a982d89SJeremy L. Thompson       // LCOV_EXCL_STOP
599*2b730f8bSJeremy L Thompson     }
6007a982d89SJeremy L. Thompson 
601*2b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf));
602e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
6037a982d89SJeremy L. Thompson   }
6047a982d89SJeremy L. Thompson 
605*2b730f8bSJeremy L Thompson   if (strlen(source) && !strrchr(source, ':')) {
60643e1b16fSJeremy L Thompson     // LCOV_EXCL_START
60743e1b16fSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPLETE,
608*2b730f8bSJeremy L Thompson                      "Provided path to source does not include function name. Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"",
60943e1b16fSJeremy L Thompson                      source);
61043e1b16fSJeremy L Thompson     // LCOV_EXCL_STOP
611*2b730f8bSJeremy L Thompson   }
61243e1b16fSJeremy L Thompson 
613*2b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, qf));
6147a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
615*2b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
616d1d35e2fSjeremylt   (*qf)->ref_count           = 1;
617d1d35e2fSjeremylt   (*qf)->vec_length          = vec_length;
618f04ea552SJeremy L Thompson   (*qf)->is_identity         = false;
619441428dfSJeremy L Thompson   (*qf)->is_context_writable = true;
6207a982d89SJeremy L. Thompson   (*qf)->function            = f;
6216e15d496SJeremy L Thompson   (*qf)->user_flop_estimate  = -1;
62243e1b16fSJeremy L Thompson   if (strlen(source)) {
623ca5eadf8SJeremy L Thompson     size_t user_source_len = strlen(source);
624ee5a26f2SJeremy L Thompson 
625*2b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(user_source_len + 1, &user_source_copy));
626ca5eadf8SJeremy L Thompson     memcpy(user_source_copy, source, user_source_len);
627ca5eadf8SJeremy L Thompson     (*qf)->user_source = user_source_copy;
62843e1b16fSJeremy L Thompson   }
629*2b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields));
630*2b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields));
631*2b730f8bSJeremy L Thompson   CeedCall(ceed->QFunctionCreate(*qf));
632e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6337a982d89SJeremy L. Thompson }
6347a982d89SJeremy L. Thompson 
6357a982d89SJeremy L. Thompson /**
636288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
637288c0443SJeremy L Thompson 
638288c0443SJeremy L Thompson   @param ceed     A Ceed object where the CeedQFunction will be created
639288c0443SJeremy L Thompson   @param name     Name of QFunction to use from gallery
640288c0443SJeremy L Thompson   @param[out] qf  Address of the variable where the newly created
641288c0443SJeremy L Thompson                     CeedQFunction will be stored
642288c0443SJeremy L Thompson 
643288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
644288c0443SJeremy L Thompson 
6457a982d89SJeremy L. Thompson   @ref User
646288c0443SJeremy L Thompson **/
647*2b730f8bSJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, CeedQFunction *qf) {
648f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
649288c0443SJeremy L Thompson 
650*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionRegisterAll());
651288c0443SJeremy L Thompson   // Find matching backend
652*2b730f8bSJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE, "No QFunction name provided");
653288c0443SJeremy L Thompson   for (size_t i = 0; i < num_qfunctions; i++) {
654288c0443SJeremy L Thompson     size_t      n;
655d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
656*2b730f8bSJeremy L Thompson     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {
657*2b730f8bSJeremy L Thompson     }
658d1d35e2fSjeremylt     if (n > match_len) {
659d1d35e2fSjeremylt       match_len   = n;
660f7e22acaSJeremy L Thompson       match_index = i;
661288c0443SJeremy L Thompson     }
662288c0443SJeremy L Thompson   }
663*2b730f8bSJeremy L Thompson   if (!match_len) {
664138d4072Sjeremylt     // LCOV_EXCL_START
665e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
666138d4072Sjeremylt     // LCOV_EXCL_STOP
667*2b730f8bSJeremy L Thompson   }
668288c0443SJeremy L Thompson 
669288c0443SJeremy L Thompson   // Create QFunction
670*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInterior(ceed, gallery_qfunctions[match_index].vec_length, gallery_qfunctions[match_index].f,
671*2b730f8bSJeremy L Thompson                                        gallery_qfunctions[match_index].source, qf));
672288c0443SJeremy L Thompson 
673288c0443SJeremy L Thompson   // QFunction specific setup
674*2b730f8bSJeremy L Thompson   CeedCall(gallery_qfunctions[match_index].init(ceed, name, *qf));
675288c0443SJeremy L Thompson 
67675affc3bSjeremylt   // Copy name
677*2b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name));
67843e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
679e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
680288c0443SJeremy L Thompson }
681288c0443SJeremy L Thompson 
682288c0443SJeremy L Thompson /**
6830219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
6840219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
6850219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
6860219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
6870219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
6880219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
6890219ea01SJeremy L Thompson            location for both.
6900219ea01SJeremy L Thompson 
6910219ea01SJeremy L Thompson   @param ceed          A Ceed object where the CeedQFunction will be created
692d1d35e2fSjeremylt   @param[in] size      Size of the QFunction fields
693d1d35e2fSjeremylt   @param[in] in_mode   CeedEvalMode for input to CeedQFunction
694d1d35e2fSjeremylt   @param[in] out_mode  CeedEvalMode for output to CeedQFunction
6950219ea01SJeremy L Thompson   @param[out] qf       Address of the variable where the newly created
6960219ea01SJeremy L Thompson                          CeedQFunction will be stored
6970219ea01SJeremy L Thompson 
6980219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
6990219ea01SJeremy L Thompson 
7007a982d89SJeremy L. Thompson   @ref User
7010219ea01SJeremy L Thompson **/
702*2b730f8bSJeremy L Thompson int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, CeedEvalMode out_mode, CeedQFunction *qf) {
703*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Identity", qf));
704*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(*qf, "input", size, in_mode));
705*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(*qf, "output", size, out_mode));
7060219ea01SJeremy L Thompson 
707f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
708547dbd6fSJeremy L Thompson 
709777ff853SJeremy L Thompson   CeedQFunctionContext  ctx;
7103668ca4bSJeremy L Thompson   CeedContextFieldLabel size_label;
711*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(*qf, &ctx));
712*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label));
713*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetInt32(ctx, size_label, &size));
714547dbd6fSJeremy L Thompson 
715e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7160219ea01SJeremy L Thompson }
7170219ea01SJeremy L Thompson 
7180219ea01SJeremy L Thompson /**
7199560d06aSjeremylt   @brief Copy the pointer to a CeedQFunction. Both pointers should
7209560d06aSjeremylt            be destroyed with `CeedQFunctionDestroy()`;
7219560d06aSjeremylt            Note: If `*qf_copy` is non-NULL, then it is assumed that
7229560d06aSjeremylt            `*qf_copy` is a pointer to a CeedQFunction. This
7239560d06aSjeremylt            CeedQFunction will be destroyed if `*qf_copy` is the only
7249560d06aSjeremylt            reference to this CeedQFunction.
7259560d06aSjeremylt 
7269560d06aSjeremylt   @param qf            CeedQFunction to copy reference to
7279560d06aSjeremylt   @param[out] qf_copy  Variable to store copied reference
7289560d06aSjeremylt 
7299560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7309560d06aSjeremylt 
7319560d06aSjeremylt   @ref User
7329560d06aSjeremylt **/
7339560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
734*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionReference(qf));
735*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(qf_copy));
7369560d06aSjeremylt   *qf_copy = qf;
7379560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7389560d06aSjeremylt }
7399560d06aSjeremylt 
7409560d06aSjeremylt /**
741a0a97fcfSJed Brown   @brief Add a CeedQFunction input
742b11c1e72Sjeremylt 
743b11c1e72Sjeremylt   @param qf          CeedQFunction
744d1d35e2fSjeremylt   @param field_name  Name of QFunction field
745d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
746d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
747d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
748b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
749b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
750b11c1e72Sjeremylt 
751b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
752dfdf5a53Sjeremylt 
7537a982d89SJeremy L. Thompson   @ref User
754b11c1e72Sjeremylt **/
755*2b730f8bSJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
756*2b730f8bSJeremy L Thompson   if (qf->is_immutable) {
757e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
758*2b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
759e15f9bd0SJeremy L Thompson     // LCOV_EXCL_STOP
760*2b730f8bSJeremy L Thompson   }
761*2b730f8bSJeremy L Thompson   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1)) {
762e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
763*2b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION, "CEED_EVAL_WEIGHT should have size 1");
764e15f9bd0SJeremy L Thompson     // LCOV_EXCL_STOP
765*2b730f8bSJeremy L Thompson   }
766*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], field_name, size, eval_mode));
767d1d35e2fSjeremylt   qf->num_input_fields++;
768e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
769d7b241e6Sjeremylt }
770d7b241e6Sjeremylt 
771b11c1e72Sjeremylt /**
772a0a97fcfSJed Brown   @brief Add a CeedQFunction output
773b11c1e72Sjeremylt 
774b11c1e72Sjeremylt   @param qf          CeedQFunction
775d1d35e2fSjeremylt   @param field_name  Name of QFunction field
776d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
777d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
778d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
779b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
780b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
781b11c1e72Sjeremylt 
782b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
783dfdf5a53Sjeremylt 
7847a982d89SJeremy L. Thompson   @ref User
785b11c1e72Sjeremylt **/
786*2b730f8bSJeremy L Thompson int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
787*2b730f8bSJeremy L Thompson   if (qf->is_immutable) {
788e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
789*2b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
790e15f9bd0SJeremy L Thompson     // LCOV_EXCL_STOP
791*2b730f8bSJeremy L Thompson   }
792*2b730f8bSJeremy L Thompson   if (eval_mode == CEED_EVAL_WEIGHT) {
793c042f62fSJeremy L Thompson     // LCOV_EXCL_START
794*2b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION, "Cannot create QFunction output with CEED_EVAL_WEIGHT");
795c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
796*2b730f8bSJeremy L Thompson   }
797*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], field_name, size, eval_mode));
798d1d35e2fSjeremylt   qf->num_output_fields++;
799e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
800d7b241e6Sjeremylt }
801d7b241e6Sjeremylt 
802dfdf5a53Sjeremylt /**
80343bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
80443bbe138SJeremy L Thompson 
805f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
806f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
807f04ea552SJeremy L Thompson 
80843bbe138SJeremy L Thompson   @param qf                      CeedQFunction
809f74ec584SJeremy L Thompson   @param[out] num_input_fields   Variable to store number of input fields
810f74ec584SJeremy L Thompson   @param[out] input_fields       Variable to store input fields
811f74ec584SJeremy L Thompson   @param[out] num_output_fields  Variable to store number of output fields
812f74ec584SJeremy L Thompson   @param[out] output_fields      Variable to store output fields
81343bbe138SJeremy L Thompson 
81443bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
81543bbe138SJeremy L Thompson 
816e9b533fbSJeremy L Thompson   @ref Advanced
81743bbe138SJeremy L Thompson **/
818*2b730f8bSJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields, CeedQFunctionField **input_fields, CeedInt *num_output_fields,
81943bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
820f04ea552SJeremy L Thompson   qf->is_immutable = true;
82143bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
82243bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
82343bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
82443bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
82543bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
82643bbe138SJeremy L Thompson }
82743bbe138SJeremy L Thompson 
82843bbe138SJeremy L Thompson /**
82943bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
83043bbe138SJeremy L Thompson 
83143bbe138SJeremy L Thompson   @param qf_field         CeedQFunctionField
83243bbe138SJeremy L Thompson   @param[out] field_name  Variable to store the field name
83343bbe138SJeremy L Thompson 
83443bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
83543bbe138SJeremy L Thompson 
836e9b533fbSJeremy L Thompson   @ref Advanced
83743bbe138SJeremy L Thompson **/
83843bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
83943bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
84043bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
84143bbe138SJeremy L Thompson }
84243bbe138SJeremy L Thompson 
84343bbe138SJeremy L Thompson /**
84443bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
84543bbe138SJeremy L Thompson 
84643bbe138SJeremy L Thompson   @param qf_field   CeedQFunctionField
84743bbe138SJeremy L Thompson   @param[out] size  Variable to store the size of the field
84843bbe138SJeremy L Thompson 
84943bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
85043bbe138SJeremy L Thompson 
851e9b533fbSJeremy L Thompson   @ref Advanced
85243bbe138SJeremy L Thompson **/
85343bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
85443bbe138SJeremy L Thompson   *size = qf_field->size;
85543bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
85643bbe138SJeremy L Thompson }
85743bbe138SJeremy L Thompson 
85843bbe138SJeremy L Thompson /**
85943bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
86043bbe138SJeremy L Thompson 
86143bbe138SJeremy L Thompson   @param qf_field        CeedQFunctionField
86243bbe138SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
86343bbe138SJeremy L Thompson 
86443bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
86543bbe138SJeremy L Thompson 
866e9b533fbSJeremy L Thompson   @ref Advanced
86743bbe138SJeremy L Thompson **/
868*2b730f8bSJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, CeedEvalMode *eval_mode) {
86943bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
87043bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
87143bbe138SJeremy L Thompson }
87243bbe138SJeremy L Thompson 
87343bbe138SJeremy L Thompson /**
8744ce2993fSjeremylt   @brief Set global context for a CeedQFunction
875b11c1e72Sjeremylt 
876b11c1e72Sjeremylt   @param qf   CeedQFunction
877b11c1e72Sjeremylt   @param ctx  Context data to set
878b11c1e72Sjeremylt 
879b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
880dfdf5a53Sjeremylt 
8817a982d89SJeremy L. Thompson   @ref User
882b11c1e72Sjeremylt **/
883777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
884*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&qf->ctx));
885d7b241e6Sjeremylt   qf->ctx = ctx;
8869e77b9c8SJeremy L Thompson   if (ctx) {
887*2b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextReference(ctx));
8889e77b9c8SJeremy L Thompson   }
889e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
890d7b241e6Sjeremylt }
891d7b241e6Sjeremylt 
892b11c1e72Sjeremylt /**
893441428dfSJeremy L Thompson   @brief Set writability of CeedQFunctionContext when calling the `CeedQFunctionUser`.
894441428dfSJeremy L Thompson            The default value is 'is_writable == true'.
895441428dfSJeremy L Thompson 
896441428dfSJeremy L Thompson            Setting `is_writable == true` indicates the `CeedQFunctionUser` writes
897441428dfSJeremy L Thompson            into the CeedQFunctionContextData and requires memory syncronization
898441428dfSJeremy L Thompson            after calling `CeedQFunctionApply()`.
899441428dfSJeremy L Thompson 
900441428dfSJeremy L Thompson            Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not
901441428dfSJeremy L Thompson            modify the CeedQFunctionContextData. Violating this assertion may lead
902441428dfSJeremy L Thompson            to inconsistent data.
903441428dfSJeremy L Thompson 
904441428dfSJeremy L Thompson            Setting `is_writable == false` may offer a performance improvement on GPU backends.
905441428dfSJeremy L Thompson 
906441428dfSJeremy L Thompson   @param qf           CeedQFunction
907441428dfSJeremy L Thompson   @param is_writable  Writability status
908441428dfSJeremy L Thompson 
909441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
910441428dfSJeremy L Thompson 
911441428dfSJeremy L Thompson   @ref User
912441428dfSJeremy L Thompson **/
913441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
914441428dfSJeremy L Thompson   qf->is_context_writable = is_writable;
915441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
916441428dfSJeremy L Thompson }
917441428dfSJeremy L Thompson 
918441428dfSJeremy L Thompson /**
9196e15d496SJeremy L Thompson   @brief Set estimated number of FLOPs per quadrature required to apply QFunction
9206e15d496SJeremy L Thompson 
9216e15d496SJeremy L Thompson   @param qf    QFunction to estimate FLOPs for
9226e15d496SJeremy L Thompson   @param flops FLOPs per quadrature point estimate
9236e15d496SJeremy L Thompson 
9246e15d496SJeremy L Thompson   @ref Backend
9256e15d496SJeremy L Thompson **/
9269d36ca50SJeremy L Thompson int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
927*2b730f8bSJeremy L Thompson   if (flops < 0) {
9286e15d496SJeremy L Thompson     // LCOV_EXCL_START
929*2b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_INCOMPATIBLE, "Must set non-negative FLOPs estimate");
9306e15d496SJeremy L Thompson     // LCOV_EXCL_STOP
931*2b730f8bSJeremy L Thompson   }
9326e15d496SJeremy L Thompson   qf->user_flop_estimate = flops;
9336e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9346e15d496SJeremy L Thompson }
9356e15d496SJeremy L Thompson 
9366e15d496SJeremy L Thompson /**
93775affc3bSjeremylt   @brief View a CeedQFunction
93875affc3bSjeremylt 
93975affc3bSjeremylt   @param[in] qf      CeedQFunction to view
94075affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
94175affc3bSjeremylt 
94275affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
94375affc3bSjeremylt 
9447a982d89SJeremy L. Thompson   @ref User
94575affc3bSjeremylt **/
94675affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
947ca5eadf8SJeremy L Thompson   char *kernel_name;
94875affc3bSjeremylt 
949*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetKernelName(qf, &kernel_name));
950*2b730f8bSJeremy L Thompson   fprintf(stream, "%sCeedQFunction - %s\n", qf->is_gallery ? "Gallery " : "User ", qf->is_gallery ? qf->gallery_name : kernel_name);
95175affc3bSjeremylt 
952*2b730f8bSJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " input field%s:\n", qf->num_input_fields, qf->num_input_fields > 1 ? "s" : "");
953d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
954*2b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream));
95575affc3bSjeremylt   }
95675affc3bSjeremylt 
957*2b730f8bSJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " output field%s:\n", qf->num_output_fields, qf->num_output_fields > 1 ? "s" : "");
958d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
959*2b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream));
96075affc3bSjeremylt   }
961e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
96275affc3bSjeremylt }
96375affc3bSjeremylt 
96475affc3bSjeremylt /**
965b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunction
966b7c9bbdaSJeremy L Thompson 
967b7c9bbdaSJeremy L Thompson   @param qf              CeedQFunction
968b7c9bbdaSJeremy L Thompson   @param[out] ceed       Variable to store Ceed
969b7c9bbdaSJeremy L Thompson 
970b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
971b7c9bbdaSJeremy L Thompson 
972b7c9bbdaSJeremy L Thompson   @ref Advanced
973b7c9bbdaSJeremy L Thompson **/
974b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
975b7c9bbdaSJeremy L Thompson   *ceed = qf->ceed;
976b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
977b7c9bbdaSJeremy L Thompson }
978b7c9bbdaSJeremy L Thompson 
979b7c9bbdaSJeremy L Thompson /**
980b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
981b11c1e72Sjeremylt 
982f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
983f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
984f04ea552SJeremy L Thompson 
985b11c1e72Sjeremylt   @param qf      CeedQFunction
986b11c1e72Sjeremylt   @param Q       Number of quadrature points
98734138859Sjeremylt   @param[in] u   Array of input CeedVectors
98834138859Sjeremylt   @param[out] v  Array of output CeedVectors
989b11c1e72Sjeremylt 
990b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
991dfdf5a53Sjeremylt 
9927a982d89SJeremy L. Thompson   @ref User
993b11c1e72Sjeremylt **/
994*2b730f8bSJeremy L Thompson int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, CeedVector *u, CeedVector *v) {
995*2b730f8bSJeremy L Thompson   if (!qf->Apply) {
996c042f62fSJeremy L Thompson     // LCOV_EXCL_START
997*2b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support QFunctionApply");
998c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
999*2b730f8bSJeremy L Thompson   }
1000*2b730f8bSJeremy L Thompson   if (Q % qf->vec_length) {
1001c042f62fSJeremy L Thompson     // LCOV_EXCL_START
1002*2b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION, "Number of quadrature points %" CeedInt_FMT " must be a multiple of %" CeedInt_FMT, Q,
1003*2b730f8bSJeremy L Thompson                      qf->vec_length);
1004c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
1005*2b730f8bSJeremy L Thompson   }
1006f04ea552SJeremy L Thompson   qf->is_immutable = true;
1007*2b730f8bSJeremy L Thompson   CeedCall(qf->Apply(qf, Q, u, v));
1008e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1009d7b241e6Sjeremylt }
1010d7b241e6Sjeremylt 
1011b11c1e72Sjeremylt /**
1012b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
1013b11c1e72Sjeremylt 
1014b11c1e72Sjeremylt   @param qf  CeedQFunction to destroy
1015b11c1e72Sjeremylt 
1016b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1017dfdf5a53Sjeremylt 
10187a982d89SJeremy L. Thompson   @ref User
1019b11c1e72Sjeremylt **/
1020d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
1021d1d35e2fSjeremylt   if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS;
1022fe2413ffSjeremylt   // Backend destroy
1023d7b241e6Sjeremylt   if ((*qf)->Destroy) {
1024*2b730f8bSJeremy L Thompson     CeedCall((*qf)->Destroy(*qf));
1025d7b241e6Sjeremylt   }
1026fe2413ffSjeremylt   // Free fields
102792ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_input_fields; i++) {
1028*2b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->input_fields[i]).field_name));
1029*2b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->input_fields[i]));
1030fe2413ffSjeremylt   }
103192ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_output_fields; i++) {
1032*2b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->output_fields[i]).field_name));
1033*2b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->output_fields[i]));
1034fe2413ffSjeremylt   }
1035*2b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->input_fields));
1036*2b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->output_fields));
1037777ff853SJeremy L Thompson 
1038777ff853SJeremy L Thompson   // User context data object
1039*2b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&(*qf)->ctx));
1040fe2413ffSjeremylt 
1041*2b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->user_source));
1042*2b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->source_path));
1043*2b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->gallery_name));
1044*2b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->kernel_name));
1045*2b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*qf)->ceed));
1046*2b730f8bSJeremy L Thompson   CeedCall(CeedFree(qf));
1047e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1048d7b241e6Sjeremylt }
1049d7b241e6Sjeremylt 
1050d7b241e6Sjeremylt /// @}
1051