xref: /libCEED/interface/ceed-qfunction.c (revision 1c66c397a67401e1a222857807e6e5b7c45b88c0)
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)) {
65*1c66c397SJeremy L Thompson   const char *relative_file_path;
66*1c66c397SJeremy L Thompson 
676574a04fSJeremy L Thompson   CeedCheck(num_qfunctions < sizeof(gallery_qfunctions) / sizeof(gallery_qfunctions[0]), NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
68c042f62fSJeremy L Thompson 
698ccf1006SJeremy L Thompson   CeedDebugEnv("Gallery Register: %s", name);
708ccf1006SJeremy L Thompson 
712b730f8bSJeremy L Thompson   CeedCall(CeedGetJitRelativePath(source, &relative_file_path));
726eb0d8b4SJeremy L Thompson 
73d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
74d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN - 1] = 0;
752b730f8bSJeremy L Thompson   strncpy(gallery_qfunctions[num_qfunctions].source, relative_file_path, CEED_MAX_RESOURCE_LEN);
76d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN - 1] = 0;
77d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].vec_length                        = vec_length;
78d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].f                                 = f;
79d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].init                              = init;
80288c0443SJeremy L Thompson   num_qfunctions++;
81e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
82288c0443SJeremy L Thompson }
83288c0443SJeremy L Thompson 
84288c0443SJeremy L Thompson /**
857a982d89SJeremy L. Thompson   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
867a982d89SJeremy L. Thompson 
87ea61e9acSJeremy L Thompson   @param[out] f           CeedQFunctionField
88ea61e9acSJeremy L Thompson   @param[in]  field_name  Name of QFunction field
895330800fSSebastian Grimberg   @param[in]  size        Size of QFunction field, (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_WEIGHT,
905330800fSSebastian 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,
915330800fSSebastian Grimberg (num_comp * dim) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and
925330800fSSebastian Grimberg (num_comp * curl_dim) with curl_dim = 1 if dim < 3 else dim for @ref CEED_EVAL_CURL.
93ea61e9acSJeremy L Thompson   @param[in]  eval_mode   \ref CEED_EVAL_NONE to use values directly,
945330800fSSebastian Grimberg                             \ref CEED_EVAL_WEIGHT to use quadrature weights,
957a982d89SJeremy L. Thompson                             \ref CEED_EVAL_INTERP to use interpolated values,
967a982d89SJeremy L. Thompson                             \ref CEED_EVAL_GRAD to use gradients,
975330800fSSebastian Grimberg                             \ref CEED_EVAL_DIV to use divergence,
985330800fSSebastian Grimberg                             \ref CEED_EVAL_CURL to use curl.
997a982d89SJeremy L. Thompson 
1007a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1017a982d89SJeremy L. Thompson 
1027a982d89SJeremy L. Thompson   @ref Developer
1037a982d89SJeremy L. Thompson **/
1042b730f8bSJeremy L Thompson static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
1052b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, f));
1062b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*f)->field_name));
1077a982d89SJeremy L. Thompson   (*f)->size      = size;
108d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
109e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1107a982d89SJeremy L. Thompson }
1117a982d89SJeremy L. Thompson 
1127a982d89SJeremy L. Thompson /**
1137a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1147a982d89SJeremy L. Thompson 
1157a982d89SJeremy L. Thompson   @param[in] field        QFunction field to view
116d1d35e2fSjeremylt   @param[in] field_number Number of field being viewed
1174c4400c7SValeria Barra   @param[in] in           true for input field, false for output
1187a982d89SJeremy L. Thompson   @param[in] stream       Stream to view to, e.g., stdout
1197a982d89SJeremy L. Thompson 
1207a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1217a982d89SJeremy L. Thompson 
1227a982d89SJeremy L. Thompson   @ref Utility
1237a982d89SJeremy L. Thompson **/
1242b730f8bSJeremy L Thompson static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt field_number, bool in, FILE *stream) {
1257a982d89SJeremy L. Thompson   const char  *inout = in ? "Input" : "Output";
1268229195eSjeremylt   char        *field_name;
1278229195eSjeremylt   CeedInt      size;
1288229195eSjeremylt   CeedEvalMode eval_mode;
129*1c66c397SJeremy L Thompson 
130*1c66c397SJeremy L Thompson   CeedCall(CeedQFunctionFieldGetName(field, &field_name));
131*1c66c397SJeremy L Thompson   CeedCall(CeedQFunctionFieldGetSize(field, &size));
1322b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldGetEvalMode(field, &eval_mode));
1332b730f8bSJeremy L Thompson   fprintf(stream,
1342b730f8bSJeremy L Thompson           "    %s field %" CeedInt_FMT
1352b730f8bSJeremy L Thompson           ":\n"
1367a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1372b730f8bSJeremy L Thompson           "      Size: %" CeedInt_FMT
1382b730f8bSJeremy L Thompson           "\n"
1397a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1408229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
141e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1427a982d89SJeremy L. Thompson }
1437a982d89SJeremy L. Thompson 
144777ff853SJeremy L Thompson /**
145777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
146777ff853SJeremy L Thompson 
147ea61e9acSJeremy L Thompson   @param[in,out] qf     CeedQFunction
148ea61e9acSJeremy L Thompson   @param[in]     status Boolean value to set as Fortran status
149777ff853SJeremy L Thompson 
150777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
151777ff853SJeremy L Thompson 
152777ff853SJeremy L Thompson   @ref Backend
153777ff853SJeremy L Thompson **/
154777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
155f04ea552SJeremy L Thompson   qf->is_fortran = status;
156e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
157777ff853SJeremy L Thompson }
158777ff853SJeremy L Thompson 
1597a982d89SJeremy L. Thompson /// @}
1607a982d89SJeremy L. Thompson 
1617a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1627a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1637a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1647a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1657a982d89SJeremy L. Thompson /// @{
1667a982d89SJeremy L. Thompson 
1677a982d89SJeremy L. Thompson /**
1687a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1697a982d89SJeremy L. Thompson 
170ea61e9acSJeremy L Thompson   @param[in]  qf         CeedQFunction
171d1d35e2fSjeremylt   @param[out] vec_length Variable to store vector length
1727a982d89SJeremy L. Thompson 
1737a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1747a982d89SJeremy L. Thompson 
1757a982d89SJeremy L. Thompson   @ref Backend
1767a982d89SJeremy L. Thompson **/
177d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
178d1d35e2fSjeremylt   *vec_length = qf->vec_length;
179e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1807a982d89SJeremy L. Thompson }
1817a982d89SJeremy L. Thompson 
1827a982d89SJeremy L. Thompson /**
1837a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
1847a982d89SJeremy L. Thompson 
185ea61e9acSJeremy L Thompson   @param[in]  qf         CeedQFunction
186d1d35e2fSjeremylt   @param[out] num_input  Variable to store number of input fields
187d1d35e2fSjeremylt   @param[out] num_output Variable to store number of output fields
1887a982d89SJeremy L. Thompson 
1897a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1907a982d89SJeremy L. Thompson 
1917a982d89SJeremy L. Thompson   @ref Backend
1927a982d89SJeremy L. Thompson **/
1932b730f8bSJeremy L Thompson int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, CeedInt *num_output) {
194d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
195d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
196e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1977a982d89SJeremy L. Thompson }
1987a982d89SJeremy L. Thompson 
1997a982d89SJeremy L. Thompson /**
20043e1b16fSJeremy L Thompson   @brief Get the name of the user function for a CeedQFunction
2017a982d89SJeremy L. Thompson 
202ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
20343e1b16fSJeremy L Thompson   @param[out] kernel_name Variable to store source path string
2047a982d89SJeremy L. Thompson 
2057a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2067a982d89SJeremy L. Thompson 
2077a982d89SJeremy L. Thompson   @ref Backend
2087a982d89SJeremy L. Thompson **/
20943e1b16fSJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, char **kernel_name) {
210ca5eadf8SJeremy L Thompson   if (!qf->kernel_name) {
211ca5eadf8SJeremy L Thompson     Ceed  ceed;
212ca5eadf8SJeremy L Thompson     char *kernel_name_copy;
213ca5eadf8SJeremy L Thompson 
214*1c66c397SJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
215ca5eadf8SJeremy L Thompson     if (qf->user_source) {
216ca5eadf8SJeremy L Thompson       const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
217ca5eadf8SJeremy L Thompson       size_t      kernel_name_len = strlen(kernel_name);
218ca5eadf8SJeremy L Thompson 
2192b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(kernel_name_len + 1, &kernel_name_copy));
220ca5eadf8SJeremy L Thompson       memcpy(kernel_name_copy, kernel_name, kernel_name_len);
221ca5eadf8SJeremy L Thompson     } else {
2222b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &kernel_name_copy));
223ca5eadf8SJeremy L Thompson     }
224ca5eadf8SJeremy L Thompson     qf->kernel_name = kernel_name_copy;
225ca5eadf8SJeremy L Thompson   }
226ca5eadf8SJeremy L Thompson 
22743e1b16fSJeremy L Thompson   *kernel_name = (char *)qf->kernel_name;
22843e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
22943e1b16fSJeremy L Thompson }
23043e1b16fSJeremy L Thompson 
23143e1b16fSJeremy L Thompson /**
23243e1b16fSJeremy L Thompson   @brief Get the source path string for a CeedQFunction
23343e1b16fSJeremy L Thompson 
234ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
23543e1b16fSJeremy L Thompson   @param[out] source_path Variable to store source path string
23643e1b16fSJeremy L Thompson 
23743e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
23843e1b16fSJeremy L Thompson 
23943e1b16fSJeremy L Thompson   @ref Backend
24043e1b16fSJeremy L Thompson **/
24143e1b16fSJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) {
242ca5eadf8SJeremy L Thompson   if (!qf->source_path && qf->user_source) {
243ca5eadf8SJeremy L Thompson     Ceed        ceed;
244ca5eadf8SJeremy L Thompson     bool        is_absolute_path;
245ca5eadf8SJeremy L Thompson     char       *absolute_path, *source_path_copy;
246ca5eadf8SJeremy L Thompson     const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
247ca5eadf8SJeremy L Thompson     size_t      kernel_name_len = strlen(kernel_name);
248ca5eadf8SJeremy L Thompson 
2492b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
2502b730f8bSJeremy L Thompson     CeedCall(CeedCheckFilePath(ceed, qf->user_source, &is_absolute_path));
251ca5eadf8SJeremy L Thompson     if (is_absolute_path) {
252ca5eadf8SJeremy L Thompson       absolute_path = (char *)qf->user_source;
253ca5eadf8SJeremy L Thompson     } else {
2542b730f8bSJeremy L Thompson       CeedCall(CeedGetJitAbsolutePath(ceed, qf->user_source, &absolute_path));
255ca5eadf8SJeremy L Thompson     }
256ca5eadf8SJeremy L Thompson 
257ca5eadf8SJeremy L Thompson     size_t source_len = strlen(absolute_path) - kernel_name_len - 1;
258*1c66c397SJeremy L Thompson 
2592b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(source_len + 1, &source_path_copy));
260ca5eadf8SJeremy L Thompson     memcpy(source_path_copy, absolute_path, source_len);
261ca5eadf8SJeremy L Thompson     qf->source_path = source_path_copy;
262ca5eadf8SJeremy L Thompson 
2632b730f8bSJeremy L Thompson     if (!is_absolute_path) CeedCall(CeedFree(&absolute_path));
264ca5eadf8SJeremy L Thompson   }
265ca5eadf8SJeremy L Thompson 
26643e1b16fSJeremy L Thompson   *source_path = (char *)qf->source_path;
267e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2687a982d89SJeremy L. Thompson }
2697a982d89SJeremy L. Thompson 
2707a982d89SJeremy L. Thompson /**
271ea61e9acSJeremy L Thompson   @brief Initialize and load QFunction source file into string buffer, including full text of local files in place of `#include "local.h"`.
2724385fb7fSSebastian Grimberg 
2733d3250a0SJeremy L Thompson   The `buffer` is set to `NULL` if there is no QFunction source file.
2744385fb7fSSebastian Grimberg 
2753d3250a0SJeremy L Thompson   Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
2763d3250a0SJeremy L Thompson 
277ea61e9acSJeremy L Thompson   @param[in]  qf            CeedQFunction
278f74ec584SJeremy L Thompson   @param[out] source_buffer String buffer for source file contents
2793d3250a0SJeremy L Thompson 
2803d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2813d3250a0SJeremy L Thompson 
2823d3250a0SJeremy L Thompson   @ref Backend
2833d3250a0SJeremy L Thompson **/
2843d3250a0SJeremy L Thompson int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, char **source_buffer) {
2853d3250a0SJeremy L Thompson   char *source_path;
2863d3250a0SJeremy L Thompson 
2872b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetSourcePath(qf, &source_path));
2883d3250a0SJeremy L Thompson   *source_buffer = NULL;
2893d3250a0SJeremy L Thompson   if (source_path) {
2902b730f8bSJeremy L Thompson     CeedCall(CeedLoadSourceToBuffer(qf->ceed, source_path, source_buffer));
2913d3250a0SJeremy L Thompson   }
2923d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2933d3250a0SJeremy L Thompson }
2943d3250a0SJeremy L Thompson 
2953d3250a0SJeremy L Thompson /**
2967a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2977a982d89SJeremy L. Thompson 
298ea61e9acSJeremy L Thompson   @param[in]  qf CeedQFunction
2997a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
3007a982d89SJeremy L. Thompson 
3017a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3027a982d89SJeremy L. Thompson 
3037a982d89SJeremy L. Thompson   @ref Backend
3047a982d89SJeremy L. Thompson **/
3057a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
3067a982d89SJeremy L. Thompson   *f = qf->function;
307e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3087a982d89SJeremy L. Thompson }
3097a982d89SJeremy L. Thompson 
3107a982d89SJeremy L. Thompson /**
311777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
3124385fb7fSSebastian Grimberg 
313ea61e9acSJeremy L Thompson   Note: For QFunctions from the Fortran interface, this function will return the Fortran context CeedQFunctionContext.
3147a982d89SJeremy L. Thompson 
315ea61e9acSJeremy L Thompson   @param[in]  qf  CeedQFunction
316777ff853SJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
3177a982d89SJeremy L. Thompson 
3187a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3197a982d89SJeremy L. Thompson 
3207a982d89SJeremy L. Thompson   @ref Backend
3217a982d89SJeremy L. Thompson **/
322777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
3237a982d89SJeremy L. Thompson   *ctx = qf->ctx;
324e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3257a982d89SJeremy L. Thompson }
3267a982d89SJeremy L. Thompson 
3277a982d89SJeremy L. Thompson /**
328441428dfSJeremy L Thompson   @brief Get context data of a CeedQFunction
329441428dfSJeremy L Thompson 
330ea61e9acSJeremy L Thompson   @param[in]  qf       CeedQFunction
331ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
332ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
333441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
334441428dfSJeremy L Thompson 
335441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
336441428dfSJeremy L Thompson 
337441428dfSJeremy L Thompson   @ref Backend
338441428dfSJeremy L Thompson **/
3392b730f8bSJeremy L Thompson int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
340441428dfSJeremy L Thompson   bool                 is_writable;
341441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
342441428dfSJeremy L Thompson 
3432b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
344441428dfSJeremy L Thompson   if (ctx) {
3452b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
346441428dfSJeremy L Thompson     if (is_writable) {
3472b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
348441428dfSJeremy L Thompson     } else {
3492b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
350441428dfSJeremy L Thompson     }
351441428dfSJeremy L Thompson   } else {
352441428dfSJeremy L Thompson     *(void **)data = NULL;
353441428dfSJeremy L Thompson   }
354441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
355441428dfSJeremy L Thompson }
356441428dfSJeremy L Thompson 
357441428dfSJeremy L Thompson /**
358441428dfSJeremy L Thompson   @brief Restore context data of a CeedQFunction
359441428dfSJeremy L Thompson 
360ea61e9acSJeremy L Thompson   @param[in]     qf   CeedQFunction
361ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
362441428dfSJeremy L Thompson 
363441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
364441428dfSJeremy L Thompson 
365441428dfSJeremy L Thompson   @ref Backend
366441428dfSJeremy L Thompson **/
367441428dfSJeremy L Thompson int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) {
368441428dfSJeremy L Thompson   bool                 is_writable;
369441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
370441428dfSJeremy L Thompson 
3712b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
372441428dfSJeremy L Thompson   if (ctx) {
3732b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
374441428dfSJeremy L Thompson     if (is_writable) {
3752b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
376441428dfSJeremy L Thompson     } else {
3772b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
378441428dfSJeremy L Thompson     }
379441428dfSJeremy L Thompson   }
3805f249b39SJeremy L Thompson   *(void **)data = NULL;
381441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
382441428dfSJeremy L Thompson }
383441428dfSJeremy L Thompson 
384441428dfSJeremy L Thompson /**
3857a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
3864385fb7fSSebastian Grimberg 
387ea61e9acSJeremy L Thompson   Note: For all QFunctions this function will return the user CeedQFunctionContext and not interface context CeedQFunctionContext, if any
388ea61e9acSJeremy L Thompson such object exists.
3897a982d89SJeremy L. Thompson 
390ea61e9acSJeremy L Thompson   @param[in]  qf  CeedQFunction
391777ff853SJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
3927a982d89SJeremy L. Thompson 
3937a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3947a982d89SJeremy L. Thompson   @ref Backend
3957a982d89SJeremy L. Thompson **/
396777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
397f04ea552SJeremy L Thompson   if (qf->is_fortran) {
398d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
399*1c66c397SJeremy L Thompson 
4002b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx));
4018cb0412aSJeremy L Thompson     *ctx = fortran_ctx->inner_ctx;
4022b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx));
4037a982d89SJeremy L. Thompson   } else {
4047a982d89SJeremy L. Thompson     *ctx = qf->ctx;
4057a982d89SJeremy L. Thompson   }
406e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4077a982d89SJeremy L. Thompson }
4087a982d89SJeremy L. Thompson 
4097a982d89SJeremy L. Thompson /**
410441428dfSJeremy L Thompson   @brief Get inner context data of a CeedQFunction
411441428dfSJeremy L Thompson 
412ea61e9acSJeremy L Thompson   @param[in]  qf       CeedQFunction
413ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
414ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
415441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
416441428dfSJeremy L Thompson 
417441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
418441428dfSJeremy L Thompson 
419441428dfSJeremy L Thompson   @ref Backend
420441428dfSJeremy L Thompson **/
4212b730f8bSJeremy L Thompson int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
422441428dfSJeremy L Thompson   bool                 is_writable;
423441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
424441428dfSJeremy L Thompson 
4252b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
426441428dfSJeremy L Thompson   if (ctx) {
4272b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
428441428dfSJeremy L Thompson     if (is_writable) {
4292b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
430441428dfSJeremy L Thompson     } else {
4312b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
432441428dfSJeremy L Thompson     }
433441428dfSJeremy L Thompson   } else {
434441428dfSJeremy L Thompson     *(void **)data = NULL;
435441428dfSJeremy L Thompson   }
436441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
437441428dfSJeremy L Thompson }
438441428dfSJeremy L Thompson 
439441428dfSJeremy L Thompson /**
440441428dfSJeremy L Thompson   @brief Restore inner context data of a CeedQFunction
441441428dfSJeremy L Thompson 
442ea61e9acSJeremy L Thompson   @param[in]     qf   CeedQFunction
443ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
444441428dfSJeremy L Thompson 
445441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
446441428dfSJeremy L Thompson 
447441428dfSJeremy L Thompson   @ref Backend
448441428dfSJeremy L Thompson **/
449441428dfSJeremy L Thompson int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) {
450441428dfSJeremy L Thompson   bool                 is_writable;
451441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
452441428dfSJeremy L Thompson 
4532b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
454441428dfSJeremy L Thompson   if (ctx) {
4552b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
456441428dfSJeremy L Thompson     if (is_writable) {
4572b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
458441428dfSJeremy L Thompson     } else {
4592b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
460441428dfSJeremy L Thompson     }
461441428dfSJeremy L Thompson   }
4625f249b39SJeremy L Thompson   *(void **)data = NULL;
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 
469ea61e9acSJeremy L Thompson   @param[in]  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 
484ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
485ea61e9acSJeremy L Thompson   @param[out] is_writable Variable to store context writeable status
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 
499ea61e9acSJeremy L Thompson   @param[in]  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 
514ea61e9acSJeremy L Thompson   @param[in,out] qf   CeedQFunction
515ea61e9acSJeremy L Thompson   @param[in]     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 
529ea61e9acSJeremy L Thompson   @param[in,out] 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 
543ea61e9acSJeremy L Thompson   @param[in]  qf    QFunction to estimate FLOPs for
544ea61e9acSJeremy L Thompson   @param[out] 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) {
5496574a04fSJeremy L Thompson   CeedCheck(qf->user_flop_estimate > -1, qf->ceed, CEED_ERROR_INCOMPLETE, "Must set FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
5506e15d496SJeremy L Thompson   *flops = qf->user_flop_estimate;
5516e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5526e15d496SJeremy L Thompson }
5536e15d496SJeremy L Thompson 
5547a982d89SJeremy L. Thompson /// @}
5557a982d89SJeremy L. Thompson 
5567a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5577a982d89SJeremy L. Thompson /// CeedQFunction Public API
5587a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5597a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
5607a982d89SJeremy L. Thompson /// @{
5617a982d89SJeremy L. Thompson 
5627a982d89SJeremy L. Thompson /**
5637a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
5647a982d89SJeremy L. Thompson 
565ea61e9acSJeremy L Thompson   @param[in]  ceed       Ceed object where the CeedQFunction will be created
566ea61e9acSJeremy L Thompson   @param[in]  vec_length Vector length. Caller must ensure that number of quadrature points is a multiple of vec_length.
567ea61e9acSJeremy L Thompson   @param[in]  f          Function pointer to evaluate action at quadrature points.
5687a982d89SJeremy L. Thompson                            See \ref CeedQFunctionUser.
569ea61e9acSJeremy L Thompson   @param[in]  source     Absolute path to source of QFunction, "\abs_path\file.h:function_name".
570ea61e9acSJeremy L Thompson                            For support across all backends, this source must only contain constructs supported by C99, C++11, and CUDA.
571ea61e9acSJeremy L Thompson   @param[out] qf         Address of the variable where the newly created CeedQFunction will be stored
5727a982d89SJeremy L. Thompson 
5737a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5747a982d89SJeremy L. Thompson 
575ea61e9acSJeremy L Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's arguments.
5767a982d89SJeremy L. Thompson 
5777a982d89SJeremy L. Thompson   @ref User
5787a982d89SJeremy L. Thompson **/
5792b730f8bSJeremy L Thompson int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length, CeedQFunctionUser f, const char *source, CeedQFunction *qf) {
580ca5eadf8SJeremy L Thompson   char *user_source_copy;
5817a982d89SJeremy L. Thompson 
5827a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
5837a982d89SJeremy L. Thompson     Ceed delegate;
5846574a04fSJeremy L Thompson 
5852b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "QFunction"));
5866574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support QFunctionCreate");
5872b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf));
588e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
5897a982d89SJeremy L. Thompson   }
5907a982d89SJeremy L. Thompson 
5916574a04fSJeremy L Thompson   CeedCheck(!strlen(source) || strrchr(source, ':'), ceed, CEED_ERROR_INCOMPLETE,
5926574a04fSJeremy L Thompson             "Provided path to source does not include function name. Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"", source);
59343e1b16fSJeremy L Thompson 
5942b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, qf));
595db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*qf)->ceed));
596d1d35e2fSjeremylt   (*qf)->ref_count           = 1;
597d1d35e2fSjeremylt   (*qf)->vec_length          = vec_length;
598f04ea552SJeremy L Thompson   (*qf)->is_identity         = false;
599441428dfSJeremy L Thompson   (*qf)->is_context_writable = true;
6007a982d89SJeremy L. Thompson   (*qf)->function            = f;
6016e15d496SJeremy L Thompson   (*qf)->user_flop_estimate  = -1;
60243e1b16fSJeremy L Thompson   if (strlen(source)) {
603ca5eadf8SJeremy L Thompson     size_t user_source_len = strlen(source);
604ee5a26f2SJeremy L Thompson 
6052b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(user_source_len + 1, &user_source_copy));
606ca5eadf8SJeremy L Thompson     memcpy(user_source_copy, source, user_source_len);
607ca5eadf8SJeremy L Thompson     (*qf)->user_source = user_source_copy;
60843e1b16fSJeremy L Thompson   }
6092b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields));
6102b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields));
6112b730f8bSJeremy L Thompson   CeedCall(ceed->QFunctionCreate(*qf));
612e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6137a982d89SJeremy L. Thompson }
6147a982d89SJeremy L. Thompson 
6157a982d89SJeremy L. Thompson /**
616288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
617288c0443SJeremy L Thompson 
618ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedQFunction will be created
619ea61e9acSJeremy L Thompson   @param[in]  name Name of QFunction to use from gallery
620ea61e9acSJeremy L Thompson   @param[out] qf   Address of the variable where the newly created CeedQFunction will be stored
621288c0443SJeremy L Thompson 
622288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
623288c0443SJeremy L Thompson 
6247a982d89SJeremy L. Thompson   @ref User
625288c0443SJeremy L Thompson **/
6262b730f8bSJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, CeedQFunction *qf) {
627f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
628288c0443SJeremy L Thompson 
6292b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionRegisterAll());
630288c0443SJeremy L Thompson   // Find matching backend
6316574a04fSJeremy L Thompson   CeedCheck(name, ceed, CEED_ERROR_INCOMPLETE, "No QFunction name provided");
632288c0443SJeremy L Thompson   for (size_t i = 0; i < num_qfunctions; i++) {
633288c0443SJeremy L Thompson     size_t      n;
634d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
6352b730f8bSJeremy L Thompson     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {
6362b730f8bSJeremy L Thompson     }
637d1d35e2fSjeremylt     if (n > match_len) {
638d1d35e2fSjeremylt       match_len   = n;
639f7e22acaSJeremy L Thompson       match_index = i;
640288c0443SJeremy L Thompson     }
641288c0443SJeremy L Thompson   }
6426574a04fSJeremy L Thompson   CeedCheck(match_len > 0, ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
643288c0443SJeremy L Thompson 
644288c0443SJeremy L Thompson   // Create QFunction
6452b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInterior(ceed, gallery_qfunctions[match_index].vec_length, gallery_qfunctions[match_index].f,
6462b730f8bSJeremy L Thompson                                        gallery_qfunctions[match_index].source, qf));
647288c0443SJeremy L Thompson 
648288c0443SJeremy L Thompson   // QFunction specific setup
6492b730f8bSJeremy L Thompson   CeedCall(gallery_qfunctions[match_index].init(ceed, name, *qf));
650288c0443SJeremy L Thompson 
65175affc3bSjeremylt   // Copy name
6522b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name));
65343e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
654e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
655288c0443SJeremy L Thompson }
656288c0443SJeremy L Thompson 
657288c0443SJeremy L Thompson /**
658ea61e9acSJeremy L Thompson   @brief Create an identity CeedQFunction.
6594385fb7fSSebastian Grimberg 
660ea61e9acSJeremy L Thompson   Inputs are written into outputs in the order given.
661859c15bbSJames Wright   This is useful for CeedOperators that can be represented with only the action of a CeedElemRestriction and CeedBasis, such as restriction
6629fd66db6SSebastian Grimberg and prolongation operators for p-multigrid.
6639fd66db6SSebastian 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
6649fd66db6SSebastian Grimberg both.
6650219ea01SJeremy L Thompson 
666ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed object where the CeedQFunction will be created
667d1d35e2fSjeremylt   @param[in]  size     Size of the QFunction fields
668d1d35e2fSjeremylt   @param[in]  in_mode  CeedEvalMode for input to CeedQFunction
669d1d35e2fSjeremylt   @param[in]  out_mode CeedEvalMode for output to CeedQFunction
670ea61e9acSJeremy L Thompson   @param[out] qf       Address of the variable where the newly created CeedQFunction will be stored
6710219ea01SJeremy L Thompson 
6720219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
6730219ea01SJeremy L Thompson 
6747a982d89SJeremy L. Thompson   @ref User
6750219ea01SJeremy L Thompson **/
6762b730f8bSJeremy L Thompson int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, CeedEvalMode out_mode, CeedQFunction *qf) {
677*1c66c397SJeremy L Thompson   CeedQFunctionContext  ctx;
678*1c66c397SJeremy L Thompson   CeedContextFieldLabel size_label;
679*1c66c397SJeremy L Thompson 
6802b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Identity", qf));
6812b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(*qf, "input", size, in_mode));
6822b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(*qf, "output", size, out_mode));
6830219ea01SJeremy L Thompson 
684f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
685547dbd6fSJeremy L Thompson 
6862b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(*qf, &ctx));
6872b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label));
6882b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetInt32(ctx, size_label, &size));
689e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6900219ea01SJeremy L Thompson }
6910219ea01SJeremy L Thompson 
6920219ea01SJeremy L Thompson /**
693ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedQFunction.
6944385fb7fSSebastian Grimberg 
695ea61e9acSJeremy L Thompson   Both pointers should be destroyed with `CeedQFunctionDestroy()`.
696512bb800SJeremy L Thompson 
697512bb800SJeremy 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.
698ea61e9acSJeremy L Thompson         This CeedQFunction will be destroyed if `*qf_copy` is the only reference to this CeedQFunction.
6999560d06aSjeremylt 
700ea61e9acSJeremy L Thompson   @param[in]  qf      CeedQFunction to copy reference to
7019560d06aSjeremylt   @param[out] qf_copy Variable to store copied reference
7029560d06aSjeremylt 
7039560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7049560d06aSjeremylt 
7059560d06aSjeremylt   @ref User
7069560d06aSjeremylt **/
7079560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
7082b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionReference(qf));
7092b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(qf_copy));
7109560d06aSjeremylt   *qf_copy = qf;
7119560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7129560d06aSjeremylt }
7139560d06aSjeremylt 
7149560d06aSjeremylt /**
715a0a97fcfSJed Brown   @brief Add a CeedQFunction input
716b11c1e72Sjeremylt 
717ea61e9acSJeremy L Thompson   @param[in,out] qf         CeedQFunction
718ea61e9acSJeremy L Thompson   @param[in]     field_name Name of QFunction field
7195330800fSSebastian Grimberg   @param[in]     size       Size of QFunction field, (num_comp * 1) for @ref CEED_EVAL_NONE,
7205330800fSSebastian 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,
7215330800fSSebastian Grimberg (num_comp * dim) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and
7225330800fSSebastian Grimberg (num_comp * curl_dim) with curl_dim = 1 if dim < 3 else dim for @ref CEED_EVAL_CURL.
723ea61e9acSJeremy L Thompson   @param[in]     eval_mode  \ref CEED_EVAL_NONE to use values directly,
724b11c1e72Sjeremylt                               \ref CEED_EVAL_INTERP to use interpolated values,
7255330800fSSebastian Grimberg                               \ref CEED_EVAL_GRAD to use gradients,
7265330800fSSebastian Grimberg                               \ref CEED_EVAL_DIV to use divergence,
7275330800fSSebastian Grimberg                               \ref CEED_EVAL_CURL to use curl.
728b11c1e72Sjeremylt 
729b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
730dfdf5a53Sjeremylt 
7317a982d89SJeremy L. Thompson   @ref User
732b11c1e72Sjeremylt **/
7332b730f8bSJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
7346574a04fSJeremy L Thompson   CeedCheck(!qf->is_immutable, qf->ceed, CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
7356574a04fSJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT || size == 1, qf->ceed, CEED_ERROR_DIMENSION, "CEED_EVAL_WEIGHT should have size 1");
736643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
7376574a04fSJeremy L Thompson     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
738643fbb69SJeremy L Thompson   }
739643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
7406574a04fSJeremy L Thompson     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
741643fbb69SJeremy L Thompson   }
7422b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], field_name, size, eval_mode));
743d1d35e2fSjeremylt   qf->num_input_fields++;
744e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
745d7b241e6Sjeremylt }
746d7b241e6Sjeremylt 
747b11c1e72Sjeremylt /**
748a0a97fcfSJed Brown   @brief Add a CeedQFunction output
749b11c1e72Sjeremylt 
750ea61e9acSJeremy L Thompson   @param[in,out] qf         CeedQFunction
751ea61e9acSJeremy L Thompson   @param[in]     field_name Name of QFunction field
7525330800fSSebastian Grimberg   @param[in]     size       Size of QFunction field, (num_comp * 1) for @ref CEED_EVAL_NONE,
7535330800fSSebastian 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,
7545330800fSSebastian Grimberg (num_comp * dim) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and
7555330800fSSebastian Grimberg (num_comp * curl_dim) with curl_dim = 1 if dim < 3 else dim for @ref CEED_EVAL_CURL.
756ea61e9acSJeremy L Thompson   @param[in]     eval_mode  \ref CEED_EVAL_NONE to use values directly,
757b11c1e72Sjeremylt                               \ref CEED_EVAL_INTERP to use interpolated values,
7585330800fSSebastian Grimberg                               \ref CEED_EVAL_GRAD to use gradients,
7595330800fSSebastian Grimberg                               \ref CEED_EVAL_DIV to use divergence,
7605330800fSSebastian Grimberg                               \ref CEED_EVAL_CURL to use curl.
761b11c1e72Sjeremylt 
762b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
763dfdf5a53Sjeremylt 
7647a982d89SJeremy L. Thompson   @ref User
765b11c1e72Sjeremylt **/
7662b730f8bSJeremy L Thompson int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
7676574a04fSJeremy L Thompson   CeedCheck(!qf->is_immutable, qf->ceed, CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
7686574a04fSJeremy L Thompson   CeedCheck(eval_mode != CEED_EVAL_WEIGHT, qf->ceed, CEED_ERROR_DIMENSION, "Cannot create QFunction output with CEED_EVAL_WEIGHT");
769643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
7706574a04fSJeremy L Thompson     CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
771643fbb69SJeremy L Thompson   }
772643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
7736574a04fSJeremy L Thompson     CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
774643fbb69SJeremy L Thompson   }
7752b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], field_name, size, eval_mode));
776d1d35e2fSjeremylt   qf->num_output_fields++;
777e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
778d7b241e6Sjeremylt }
779d7b241e6Sjeremylt 
780dfdf5a53Sjeremylt /**
78143bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
78243bbe138SJeremy L Thompson 
783ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedQFunction as immutable.
784f04ea552SJeremy L Thompson 
785ea61e9acSJeremy L Thompson   @param[in]  qf                CeedQFunction
786f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
787f74ec584SJeremy L Thompson   @param[out] input_fields      Variable to store input fields
788f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
789f74ec584SJeremy L Thompson   @param[out] output_fields     Variable to store output fields
79043bbe138SJeremy L Thompson 
79143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
79243bbe138SJeremy L Thompson 
793e9b533fbSJeremy L Thompson   @ref Advanced
79443bbe138SJeremy L Thompson **/
7952b730f8bSJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields, CeedQFunctionField **input_fields, CeedInt *num_output_fields,
79643bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
797f04ea552SJeremy L Thompson   qf->is_immutable = true;
79843bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
79943bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
80043bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
80143bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
80243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
80343bbe138SJeremy L Thompson }
80443bbe138SJeremy L Thompson 
80543bbe138SJeremy L Thompson /**
80643bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
80743bbe138SJeremy L Thompson 
808ea61e9acSJeremy L Thompson   @param[in]  qf_field   CeedQFunctionField
80943bbe138SJeremy L Thompson   @param[out] field_name Variable to store the field name
81043bbe138SJeremy L Thompson 
81143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
81243bbe138SJeremy L Thompson 
813e9b533fbSJeremy L Thompson   @ref Advanced
81443bbe138SJeremy L Thompson **/
81543bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
81643bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
81743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
81843bbe138SJeremy L Thompson }
81943bbe138SJeremy L Thompson 
82043bbe138SJeremy L Thompson /**
82143bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
82243bbe138SJeremy L Thompson 
823ea61e9acSJeremy L Thompson   @param[in]  qf_field CeedQFunctionField
82443bbe138SJeremy L Thompson   @param[out] size     Variable to store the size of the field
82543bbe138SJeremy L Thompson 
82643bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
82743bbe138SJeremy L Thompson 
828e9b533fbSJeremy L Thompson   @ref Advanced
82943bbe138SJeremy L Thompson **/
83043bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
83143bbe138SJeremy L Thompson   *size = qf_field->size;
83243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
83343bbe138SJeremy L Thompson }
83443bbe138SJeremy L Thompson 
83543bbe138SJeremy L Thompson /**
83643bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
83743bbe138SJeremy L Thompson 
838ea61e9acSJeremy L Thompson   @param[in]  qf_field  CeedQFunctionField
83943bbe138SJeremy L Thompson   @param[out] eval_mode Variable to store the field evaluation mode
84043bbe138SJeremy L Thompson 
84143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
84243bbe138SJeremy L Thompson 
843e9b533fbSJeremy L Thompson   @ref Advanced
84443bbe138SJeremy L Thompson **/
8452b730f8bSJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, CeedEvalMode *eval_mode) {
84643bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
84743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
84843bbe138SJeremy L Thompson }
84943bbe138SJeremy L Thompson 
85043bbe138SJeremy L Thompson /**
8514ce2993fSjeremylt   @brief Set global context for a CeedQFunction
852b11c1e72Sjeremylt 
853ea61e9acSJeremy L Thompson   @param[in,out] qf  CeedQFunction
854ea61e9acSJeremy L Thompson   @param[in]     ctx Context data to set
855b11c1e72Sjeremylt 
856b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
857dfdf5a53Sjeremylt 
8587a982d89SJeremy L. Thompson   @ref User
859b11c1e72Sjeremylt **/
860777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
8612b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&qf->ctx));
862d7b241e6Sjeremylt   qf->ctx = ctx;
863db002c03SJeremy L Thompson   if (ctx) CeedCall(CeedQFunctionContextReference(ctx));
864e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
865d7b241e6Sjeremylt }
866d7b241e6Sjeremylt 
867b11c1e72Sjeremylt /**
868441428dfSJeremy L Thompson   @brief Set writability of CeedQFunctionContext when calling the `CeedQFunctionUser`.
8694385fb7fSSebastian Grimberg 
870859c15bbSJames Wright   The default value is `is_writable == true`.
871441428dfSJeremy L Thompson 
872ea61e9acSJeremy L Thompson   Setting `is_writable == true` indicates the `CeedQFunctionUser` writes into the CeedQFunctionContextData and requires memory syncronization
873441428dfSJeremy L Thompson after calling `CeedQFunctionApply()`.
874441428dfSJeremy L Thompson 
875ea61e9acSJeremy L Thompson   Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not modify the CeedQFunctionContextData.
876ea61e9acSJeremy L Thompson   Violating this assertion may lead to inconsistent data.
877441428dfSJeremy L Thompson 
878441428dfSJeremy L Thompson   Setting `is_writable == false` may offer a performance improvement on GPU backends.
879441428dfSJeremy L Thompson 
880ea61e9acSJeremy L Thompson   @param[in,out] qf          CeedQFunction
881ea61e9acSJeremy L Thompson   @param[in]     is_writable Writability status
882441428dfSJeremy L Thompson 
883441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
884441428dfSJeremy L Thompson 
885441428dfSJeremy L Thompson   @ref User
886441428dfSJeremy L Thompson **/
887441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
888441428dfSJeremy L Thompson   qf->is_context_writable = is_writable;
889441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
890441428dfSJeremy L Thompson }
891441428dfSJeremy L Thompson 
892441428dfSJeremy L Thompson /**
8936e15d496SJeremy L Thompson   @brief Set estimated number of FLOPs per quadrature required to apply QFunction
8946e15d496SJeremy L Thompson 
895ea61e9acSJeremy L Thompson   @param[in]  qf    QFunction to estimate FLOPs for
896ea61e9acSJeremy L Thompson   @param[out] flops FLOPs per quadrature point estimate
8976e15d496SJeremy L Thompson 
8986e15d496SJeremy L Thompson   @ref Backend
8996e15d496SJeremy L Thompson **/
9009d36ca50SJeremy L Thompson int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
9016574a04fSJeremy L Thompson   CeedCheck(flops >= 0, qf->ceed, CEED_ERROR_INCOMPATIBLE, "Must set non-negative FLOPs estimate");
9026e15d496SJeremy L Thompson   qf->user_flop_estimate = flops;
9036e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9046e15d496SJeremy L Thompson }
9056e15d496SJeremy L Thompson 
9066e15d496SJeremy L Thompson /**
90775affc3bSjeremylt   @brief View a CeedQFunction
90875affc3bSjeremylt 
90975affc3bSjeremylt   @param[in] qf     CeedQFunction to view
91075affc3bSjeremylt   @param[in] stream Stream to write; typically stdout/stderr or a file
91175affc3bSjeremylt 
91275affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
91375affc3bSjeremylt 
9147a982d89SJeremy L. Thompson   @ref User
91575affc3bSjeremylt **/
91675affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
917ca5eadf8SJeremy L Thompson   char *kernel_name;
91875affc3bSjeremylt 
9192b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetKernelName(qf, &kernel_name));
9202b730f8bSJeremy L Thompson   fprintf(stream, "%sCeedQFunction - %s\n", qf->is_gallery ? "Gallery " : "User ", qf->is_gallery ? qf->gallery_name : kernel_name);
92175affc3bSjeremylt 
9222b730f8bSJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " input field%s:\n", qf->num_input_fields, qf->num_input_fields > 1 ? "s" : "");
923d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
9242b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream));
92575affc3bSjeremylt   }
92675affc3bSjeremylt 
9272b730f8bSJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " output field%s:\n", qf->num_output_fields, qf->num_output_fields > 1 ? "s" : "");
928d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
9292b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream));
93075affc3bSjeremylt   }
931e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
93275affc3bSjeremylt }
93375affc3bSjeremylt 
93475affc3bSjeremylt /**
935b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunction
936b7c9bbdaSJeremy L Thompson 
937ea61e9acSJeremy L Thompson   @param[in]  qf   CeedQFunction
938b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store Ceed
939b7c9bbdaSJeremy L Thompson 
940b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
941b7c9bbdaSJeremy L Thompson 
942b7c9bbdaSJeremy L Thompson   @ref Advanced
943b7c9bbdaSJeremy L Thompson **/
944b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
945b7c9bbdaSJeremy L Thompson   *ceed = qf->ceed;
946b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
947b7c9bbdaSJeremy L Thompson }
948b7c9bbdaSJeremy L Thompson 
949b7c9bbdaSJeremy L Thompson /**
950b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
951b11c1e72Sjeremylt 
952ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedQFunction as immutable.
953f04ea552SJeremy L Thompson 
954ea61e9acSJeremy L Thompson   @param[in]  qf CeedQFunction
955ea61e9acSJeremy L Thompson   @param[in]  Q  Number of quadrature points
95634138859Sjeremylt   @param[in]  u  Array of input CeedVectors
95734138859Sjeremylt   @param[out] v  Array of output CeedVectors
958b11c1e72Sjeremylt 
959b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
960dfdf5a53Sjeremylt 
9617a982d89SJeremy L. Thompson   @ref User
962b11c1e72Sjeremylt **/
9632b730f8bSJeremy L Thompson int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, CeedVector *u, CeedVector *v) {
9646574a04fSJeremy L Thompson   CeedCheck(qf->Apply, qf->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support QFunctionApply");
9656574a04fSJeremy L Thompson   CeedCheck(Q % qf->vec_length == 0, qf->ceed, CEED_ERROR_DIMENSION,
9666574a04fSJeremy L Thompson             "Number of quadrature points %" CeedInt_FMT " must be a multiple of %" CeedInt_FMT, Q, qf->vec_length);
967f04ea552SJeremy L Thompson   qf->is_immutable = true;
9682b730f8bSJeremy L Thompson   CeedCall(qf->Apply(qf, Q, u, v));
969e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
970d7b241e6Sjeremylt }
971d7b241e6Sjeremylt 
972b11c1e72Sjeremylt /**
973b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
974b11c1e72Sjeremylt 
975ea61e9acSJeremy L Thompson   @param[in,out] qf CeedQFunction to destroy
976b11c1e72Sjeremylt 
977b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
978dfdf5a53Sjeremylt 
9797a982d89SJeremy L. Thompson   @ref User
980b11c1e72Sjeremylt **/
981d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
982ad6481ceSJeremy L Thompson   if (!*qf || --(*qf)->ref_count > 0) {
983ad6481ceSJeremy L Thompson     *qf = NULL;
984ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
985ad6481ceSJeremy L Thompson   }
986fe2413ffSjeremylt   // Backend destroy
987d7b241e6Sjeremylt   if ((*qf)->Destroy) {
9882b730f8bSJeremy L Thompson     CeedCall((*qf)->Destroy(*qf));
989d7b241e6Sjeremylt   }
990fe2413ffSjeremylt   // Free fields
99192ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_input_fields; i++) {
9922b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->input_fields[i]).field_name));
9932b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->input_fields[i]));
994fe2413ffSjeremylt   }
99592ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_output_fields; i++) {
9962b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->output_fields[i]).field_name));
9972b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->output_fields[i]));
998fe2413ffSjeremylt   }
9992b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->input_fields));
10002b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->output_fields));
1001777ff853SJeremy L Thompson 
1002777ff853SJeremy L Thompson   // User context data object
10032b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&(*qf)->ctx));
1004fe2413ffSjeremylt 
10052b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->user_source));
10062b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->source_path));
10072b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->gallery_name));
10082b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->kernel_name));
10092b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*qf)->ceed));
10102b730f8bSJeremy L Thompson   CeedCall(CeedFree(qf));
1011e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1012d7b241e6Sjeremylt }
1013d7b241e6Sjeremylt 
1014d7b241e6Sjeremylt /// @}
1015