xref: /libCEED/interface/ceed-qfunction.c (revision 4385fb7f4421da9c7ce289d92dc27dadf16843ac)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3d7b241e6Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5d7b241e6Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7d7b241e6Sjeremylt 
83d576824SJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
112b730f8bSJeremy L Thompson #include <ceed/jit-tools.h>
12288c0443SJeremy L Thompson #include <limits.h>
133d576824SJeremy L Thompson #include <stdbool.h>
143d576824SJeremy L Thompson #include <stdio.h>
153d576824SJeremy L Thompson #include <string.h>
16288c0443SJeremy L Thompson 
177a982d89SJeremy L. Thompson /// @file
187a982d89SJeremy L. Thompson /// Implementation of public CeedQFunction interfaces
197a982d89SJeremy L. Thompson 
20288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP
21442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none;
22442e7f0bSjeremylt /// @endcond
23442e7f0bSjeremylt 
247a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
257a982d89SJeremy L. Thompson /// @{
267a982d89SJeremy L. Thompson 
277a982d89SJeremy L. Thompson // Indicate that no QFunction is provided by the user
287a982d89SJeremy L. Thompson const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
297a982d89SJeremy L. Thompson 
307a982d89SJeremy L. Thompson /// @}
317a982d89SJeremy L. Thompson 
32442e7f0bSjeremylt /// @cond DOXYGEN_SKIP
33288c0443SJeremy L Thompson static struct {
34288c0443SJeremy L Thompson   char              name[CEED_MAX_RESOURCE_LEN];
35288c0443SJeremy L Thompson   char              source[CEED_MAX_RESOURCE_LEN];
36d1d35e2fSjeremylt   CeedInt           vec_length;
37288c0443SJeremy L Thompson   CeedQFunctionUser f;
38288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
39d1d35e2fSjeremylt } gallery_qfunctions[1024];
40288c0443SJeremy L Thompson static size_t num_qfunctions;
41288c0443SJeremy L Thompson /// @endcond
42d7b241e6Sjeremylt 
43777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
44777ff853SJeremy L Thompson /// CeedQFunction Library Internal Functions
45777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
46777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper
47777ff853SJeremy L Thompson /// @{
48777ff853SJeremy L Thompson 
49b11c1e72Sjeremylt /**
50288c0443SJeremy L Thompson   @brief Register a gallery QFunction
51288c0443SJeremy L Thompson 
52ea61e9acSJeremy L Thompson   @param[in]  name       Name for this backend to respond to
53ea61e9acSJeremy L Thompson   @param[in]  source     Absolute path to source of QFunction, "\path\CEED_DIR\gallery\folder\file.h:function_name"
54ea61e9acSJeremy L Thompson   @param[in]  vec_length Vector length.  Caller must ensure that number of quadrature points is a multiple of vec_length.
55ea61e9acSJeremy L Thompson   @param[in]  f          Function pointer to evaluate action at quadrature points.
56288c0443SJeremy L Thompson                            See \ref CeedQFunctionUser.
57ea61e9acSJeremy L Thompson   @param[in]  init       Initialization function called by CeedQFunctionInit() when the QFunction is selected.
58288c0443SJeremy L Thompson 
59288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
60288c0443SJeremy L Thompson 
617a982d89SJeremy L. Thompson   @ref Developer
62288c0443SJeremy L Thompson **/
632b730f8bSJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source, CeedInt vec_length, CeedQFunctionUser f,
64288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
652b730f8bSJeremy L Thompson   if (num_qfunctions >= sizeof(gallery_qfunctions) / sizeof(gallery_qfunctions[0])) {
66c042f62fSJeremy L Thompson     // LCOV_EXCL_START
67e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
68c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
692b730f8bSJeremy L Thompson   }
70c042f62fSJeremy L Thompson 
718ccf1006SJeremy L Thompson   CeedDebugEnv("Gallery Register: %s", name);
728ccf1006SJeremy L Thompson 
736eb0d8b4SJeremy L Thompson   const char *relative_file_path;
742b730f8bSJeremy L Thompson   CeedCall(CeedGetJitRelativePath(source, &relative_file_path));
756eb0d8b4SJeremy L Thompson 
76d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
77d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN - 1] = 0;
782b730f8bSJeremy L Thompson   strncpy(gallery_qfunctions[num_qfunctions].source, relative_file_path, CEED_MAX_RESOURCE_LEN);
79d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN - 1] = 0;
80d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].vec_length                        = vec_length;
81d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].f                                 = f;
82d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].init                              = init;
83288c0443SJeremy L Thompson   num_qfunctions++;
84e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
85288c0443SJeremy L Thompson }
86288c0443SJeremy L Thompson 
87288c0443SJeremy L Thompson /**
887a982d89SJeremy L. Thompson   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
897a982d89SJeremy L. Thompson 
90ea61e9acSJeremy L Thompson   @param[out] f           CeedQFunctionField
91ea61e9acSJeremy L Thompson   @param[in]  field_name  Name of QFunction field
925330800fSSebastian Grimberg   @param[in]  size        Size of QFunction field, (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_WEIGHT,
935330800fSSebastian 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,
945330800fSSebastian Grimberg (num_comp * dim) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and
955330800fSSebastian Grimberg (num_comp * curl_dim) with curl_dim = 1 if dim < 3 else dim for @ref CEED_EVAL_CURL.
96ea61e9acSJeremy L Thompson   @param[in]  eval_mode   \ref CEED_EVAL_NONE to use values directly,
975330800fSSebastian Grimberg                             \ref CEED_EVAL_WEIGHT to use quadrature weights,
987a982d89SJeremy L. Thompson                             \ref CEED_EVAL_INTERP to use interpolated values,
997a982d89SJeremy L. Thompson                             \ref CEED_EVAL_GRAD to use gradients,
1005330800fSSebastian Grimberg                             \ref CEED_EVAL_DIV to use divergence,
1015330800fSSebastian Grimberg                             \ref CEED_EVAL_CURL to use curl.
1027a982d89SJeremy L. Thompson 
1037a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1047a982d89SJeremy L. Thompson 
1057a982d89SJeremy L. Thompson   @ref Developer
1067a982d89SJeremy L. Thompson **/
1072b730f8bSJeremy L Thompson static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
1082b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, f));
1092b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*f)->field_name));
1107a982d89SJeremy L. Thompson   (*f)->size      = size;
111d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
112e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1137a982d89SJeremy L. Thompson }
1147a982d89SJeremy L. Thompson 
1157a982d89SJeremy L. Thompson /**
1167a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1177a982d89SJeremy L. Thompson 
1187a982d89SJeremy L. Thompson   @param[in] field        QFunction field to view
119d1d35e2fSjeremylt   @param[in] field_number Number of field being viewed
1204c4400c7SValeria Barra   @param[in] in           true for input field, false for output
1217a982d89SJeremy L. Thompson   @param[in] stream       Stream to view to, e.g., stdout
1227a982d89SJeremy L. Thompson 
1237a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1247a982d89SJeremy L. Thompson 
1257a982d89SJeremy L. Thompson   @ref Utility
1267a982d89SJeremy L. Thompson **/
1272b730f8bSJeremy L Thompson static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt field_number, bool in, FILE *stream) {
1287a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1298229195eSjeremylt   char       *field_name;
1302b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldGetName(field, &field_name));
1318229195eSjeremylt   CeedInt size;
1322b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldGetSize(field, &size));
1338229195eSjeremylt   CeedEvalMode eval_mode;
1342b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldGetEvalMode(field, &eval_mode));
1352b730f8bSJeremy L Thompson   fprintf(stream,
1362b730f8bSJeremy L Thompson           "    %s field %" CeedInt_FMT
1372b730f8bSJeremy L Thompson           ":\n"
1387a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1392b730f8bSJeremy L Thompson           "      Size: %" CeedInt_FMT
1402b730f8bSJeremy L Thompson           "\n"
1417a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1428229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
143e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1447a982d89SJeremy L. Thompson }
1457a982d89SJeremy L. Thompson 
146777ff853SJeremy L Thompson /**
147777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
148777ff853SJeremy L Thompson 
149ea61e9acSJeremy L Thompson   @param[in,out] qf     CeedQFunction
150ea61e9acSJeremy L Thompson   @param[in]     status Boolean value to set as Fortran status
151777ff853SJeremy L Thompson 
152777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
153777ff853SJeremy L Thompson 
154777ff853SJeremy L Thompson   @ref Backend
155777ff853SJeremy L Thompson **/
156777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
157f04ea552SJeremy L Thompson   qf->is_fortran = status;
158e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
159777ff853SJeremy L Thompson }
160777ff853SJeremy L Thompson 
1617a982d89SJeremy L. Thompson /// @}
1627a982d89SJeremy L. Thompson 
1637a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1647a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1657a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1667a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1677a982d89SJeremy L. Thompson /// @{
1687a982d89SJeremy L. Thompson 
1697a982d89SJeremy L. Thompson /**
1707a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1717a982d89SJeremy L. Thompson 
172ea61e9acSJeremy L Thompson   @param[in]  qf         CeedQFunction
173d1d35e2fSjeremylt   @param[out] vec_length Variable to store vector length
1747a982d89SJeremy L. Thompson 
1757a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1767a982d89SJeremy L. Thompson 
1777a982d89SJeremy L. Thompson   @ref Backend
1787a982d89SJeremy L. Thompson **/
179d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
180d1d35e2fSjeremylt   *vec_length = qf->vec_length;
181e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1827a982d89SJeremy L. Thompson }
1837a982d89SJeremy L. Thompson 
1847a982d89SJeremy L. Thompson /**
1857a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
1867a982d89SJeremy L. Thompson 
187ea61e9acSJeremy L Thompson   @param[in]  qf         CeedQFunction
188d1d35e2fSjeremylt   @param[out] num_input  Variable to store number of input fields
189d1d35e2fSjeremylt   @param[out] num_output Variable to store number of output fields
1907a982d89SJeremy L. Thompson 
1917a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1927a982d89SJeremy L. Thompson 
1937a982d89SJeremy L. Thompson   @ref Backend
1947a982d89SJeremy L. Thompson **/
1952b730f8bSJeremy L Thompson int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, CeedInt *num_output) {
196d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
197d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
198e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1997a982d89SJeremy L. Thompson }
2007a982d89SJeremy L. Thompson 
2017a982d89SJeremy L. Thompson /**
20243e1b16fSJeremy L Thompson   @brief Get the name of the user function for a CeedQFunction
2037a982d89SJeremy L. Thompson 
204ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
20543e1b16fSJeremy L Thompson   @param[out] kernel_name Variable to store source path string
2067a982d89SJeremy L. Thompson 
2077a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2087a982d89SJeremy L. Thompson 
2097a982d89SJeremy L. Thompson   @ref Backend
2107a982d89SJeremy L. Thompson **/
21143e1b16fSJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, char **kernel_name) {
212ca5eadf8SJeremy L Thompson   if (!qf->kernel_name) {
213ca5eadf8SJeremy L Thompson     Ceed  ceed;
214ca5eadf8SJeremy L Thompson     char *kernel_name_copy;
2152b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
216ca5eadf8SJeremy L Thompson 
217ca5eadf8SJeremy L Thompson     if (qf->user_source) {
218ca5eadf8SJeremy L Thompson       const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
219ca5eadf8SJeremy L Thompson       size_t      kernel_name_len = strlen(kernel_name);
220ca5eadf8SJeremy L Thompson 
2212b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(kernel_name_len + 1, &kernel_name_copy));
222ca5eadf8SJeremy L Thompson       memcpy(kernel_name_copy, kernel_name, kernel_name_len);
223ca5eadf8SJeremy L Thompson     } else {
2242b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &kernel_name_copy));
225ca5eadf8SJeremy L Thompson     }
226ca5eadf8SJeremy L Thompson     qf->kernel_name = kernel_name_copy;
227ca5eadf8SJeremy L Thompson   }
228ca5eadf8SJeremy L Thompson 
22943e1b16fSJeremy L Thompson   *kernel_name = (char *)qf->kernel_name;
23043e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
23143e1b16fSJeremy L Thompson }
23243e1b16fSJeremy L Thompson 
23343e1b16fSJeremy L Thompson /**
23443e1b16fSJeremy L Thompson   @brief Get the source path string for a CeedQFunction
23543e1b16fSJeremy L Thompson 
236ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
23743e1b16fSJeremy L Thompson   @param[out] source_path Variable to store source path string
23843e1b16fSJeremy L Thompson 
23943e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
24043e1b16fSJeremy L Thompson 
24143e1b16fSJeremy L Thompson   @ref Backend
24243e1b16fSJeremy L Thompson **/
24343e1b16fSJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) {
244ca5eadf8SJeremy L Thompson   if (!qf->source_path && qf->user_source) {
245ca5eadf8SJeremy L Thompson     Ceed        ceed;
246ca5eadf8SJeremy L Thompson     bool        is_absolute_path;
247ca5eadf8SJeremy L Thompson     char       *absolute_path, *source_path_copy;
248ca5eadf8SJeremy L Thompson     const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
249ca5eadf8SJeremy L Thompson     size_t      kernel_name_len = strlen(kernel_name);
250ca5eadf8SJeremy L Thompson 
2512b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
252ca5eadf8SJeremy L Thompson 
2532b730f8bSJeremy L Thompson     CeedCall(CeedCheckFilePath(ceed, qf->user_source, &is_absolute_path));
254ca5eadf8SJeremy L Thompson     if (is_absolute_path) {
255ca5eadf8SJeremy L Thompson       absolute_path = (char *)qf->user_source;
256ca5eadf8SJeremy L Thompson     } else {
2572b730f8bSJeremy L Thompson       CeedCall(CeedGetJitAbsolutePath(ceed, qf->user_source, &absolute_path));
258ca5eadf8SJeremy L Thompson     }
259ca5eadf8SJeremy L Thompson 
260ca5eadf8SJeremy L Thompson     size_t source_len = strlen(absolute_path) - kernel_name_len - 1;
2612b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(source_len + 1, &source_path_copy));
262ca5eadf8SJeremy L Thompson     memcpy(source_path_copy, absolute_path, source_len);
263ca5eadf8SJeremy L Thompson     qf->source_path = source_path_copy;
264ca5eadf8SJeremy L Thompson 
2652b730f8bSJeremy L Thompson     if (!is_absolute_path) CeedCall(CeedFree(&absolute_path));
266ca5eadf8SJeremy L Thompson   }
267ca5eadf8SJeremy L Thompson 
26843e1b16fSJeremy L Thompson   *source_path = (char *)qf->source_path;
269e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2707a982d89SJeremy L. Thompson }
2717a982d89SJeremy L. Thompson 
2727a982d89SJeremy L. Thompson /**
273ea61e9acSJeremy L Thompson   @brief Initialize and load QFunction source file into string buffer, including full text of local files in place of `#include "local.h"`.
274*4385fb7fSSebastian Grimberg 
2753d3250a0SJeremy L Thompson   The `buffer` is set to `NULL` if there is no QFunction source file.
276*4385fb7fSSebastian Grimberg 
2773d3250a0SJeremy L Thompson   Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
2783d3250a0SJeremy L Thompson 
279ea61e9acSJeremy L Thompson   @param[in]  qf            CeedQFunction
280f74ec584SJeremy L Thompson   @param[out] source_buffer String buffer for source file contents
2813d3250a0SJeremy L Thompson 
2823d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2833d3250a0SJeremy L Thompson 
2843d3250a0SJeremy L Thompson   @ref Backend
2853d3250a0SJeremy L Thompson **/
2863d3250a0SJeremy L Thompson int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, char **source_buffer) {
2873d3250a0SJeremy L Thompson   char *source_path;
2883d3250a0SJeremy L Thompson 
2892b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetSourcePath(qf, &source_path));
2903d3250a0SJeremy L Thompson   *source_buffer = NULL;
2913d3250a0SJeremy L Thompson   if (source_path) {
2922b730f8bSJeremy L Thompson     CeedCall(CeedLoadSourceToBuffer(qf->ceed, source_path, source_buffer));
2933d3250a0SJeremy L Thompson   }
2943d3250a0SJeremy L Thompson 
2953d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2963d3250a0SJeremy L Thompson }
2973d3250a0SJeremy L Thompson 
2983d3250a0SJeremy L Thompson /**
2997a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
3007a982d89SJeremy L. Thompson 
301ea61e9acSJeremy L Thompson   @param[in]  qf CeedQFunction
3027a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
3037a982d89SJeremy L. Thompson 
3047a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3057a982d89SJeremy L. Thompson 
3067a982d89SJeremy L. Thompson   @ref Backend
3077a982d89SJeremy L. Thompson **/
3087a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
3097a982d89SJeremy L. Thompson   *f = qf->function;
310e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3117a982d89SJeremy L. Thompson }
3127a982d89SJeremy L. Thompson 
3137a982d89SJeremy L. Thompson /**
314777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
315*4385fb7fSSebastian Grimberg 
316ea61e9acSJeremy L Thompson   Note: For QFunctions from the Fortran interface, this function will return the Fortran context CeedQFunctionContext.
3177a982d89SJeremy L. Thompson 
318ea61e9acSJeremy L Thompson   @param[in]  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 
333ea61e9acSJeremy L Thompson   @param[in]  qf       CeedQFunction
334ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
335ea61e9acSJeremy L Thompson                          If the backend 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 **/
3422b730f8bSJeremy 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 
3462b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
347441428dfSJeremy L Thompson   if (ctx) {
3482b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
349441428dfSJeremy L Thompson     if (is_writable) {
3502b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
351441428dfSJeremy L Thompson     } else {
3522b730f8bSJeremy 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 
363ea61e9acSJeremy L Thompson   @param[in]     qf   CeedQFunction
364ea61e9acSJeremy L Thompson   @param[in,out] 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 
3742b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
375441428dfSJeremy L Thompson   if (ctx) {
3762b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
377441428dfSJeremy L Thompson     if (is_writable) {
3782b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
379441428dfSJeremy L Thompson     } else {
3802b730f8bSJeremy 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
388*4385fb7fSSebastian Grimberg 
389ea61e9acSJeremy L Thompson   Note: For all QFunctions this function will return the user CeedQFunctionContext and not interface context CeedQFunctionContext, if any
390ea61e9acSJeremy L Thompson such object exists.
3917a982d89SJeremy L. Thompson 
392ea61e9acSJeremy L Thompson   @param[in]  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;
4012b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx));
4028cb0412aSJeremy L Thompson     *ctx = fortran_ctx->inner_ctx;
4032b730f8bSJeremy 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 
413ea61e9acSJeremy L Thompson   @param[in]  qf       CeedQFunction
414ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
415ea61e9acSJeremy L Thompson                          If the backend 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 **/
4222b730f8bSJeremy 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 
4262b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
427441428dfSJeremy L Thompson   if (ctx) {
4282b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
429441428dfSJeremy L Thompson     if (is_writable) {
4302b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
431441428dfSJeremy L Thompson     } else {
4322b730f8bSJeremy 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 
443ea61e9acSJeremy L Thompson   @param[in]     qf   CeedQFunction
444ea61e9acSJeremy L Thompson   @param[in,out] 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 
4542b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
455441428dfSJeremy L Thompson   if (ctx) {
4562b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
457441428dfSJeremy L Thompson     if (is_writable) {
4582b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
459441428dfSJeremy L Thompson     } else {
4602b730f8bSJeremy 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 
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) {
5492b730f8bSJeremy L Thompson   if (qf->user_flop_estimate == -1) {
5506e15d496SJeremy L Thompson     // LCOV_EXCL_START
5512b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_INCOMPLETE, "Must set FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
5526e15d496SJeremy L Thompson     // LCOV_EXCL_STOP
5532b730f8bSJeremy 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 
569ea61e9acSJeremy L Thompson   @param[in]  ceed       Ceed object where the CeedQFunction will be created
570ea61e9acSJeremy L Thompson   @param[in]  vec_length Vector length. Caller must ensure that number of quadrature points is a multiple of vec_length.
571ea61e9acSJeremy L Thompson   @param[in]  f          Function pointer to evaluate action at quadrature points.
5727a982d89SJeremy L. Thompson                            See \ref CeedQFunctionUser.
573ea61e9acSJeremy L Thompson   @param[in]  source     Absolute path to source of QFunction, "\abs_path\file.h:function_name".
574ea61e9acSJeremy L Thompson                            For support across all backends, this source must only contain constructs supported by C99, C++11, and CUDA.
575ea61e9acSJeremy L Thompson   @param[out] qf         Address of the variable where the newly created CeedQFunction will be stored
5767a982d89SJeremy L. Thompson 
5777a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5787a982d89SJeremy L. Thompson 
579ea61e9acSJeremy L Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's arguments.
5807a982d89SJeremy L. Thompson 
5817a982d89SJeremy L. Thompson   @ref User
5827a982d89SJeremy L. Thompson **/
5832b730f8bSJeremy L Thompson int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length, CeedQFunctionUser f, const char *source, CeedQFunction *qf) {
584ca5eadf8SJeremy L Thompson   char *user_source_copy;
5857a982d89SJeremy L. Thompson 
5867a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
5877a982d89SJeremy L. Thompson     Ceed delegate;
5882b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "QFunction"));
5897a982d89SJeremy L. Thompson 
5902b730f8bSJeremy L Thompson     if (!delegate) {
5917a982d89SJeremy L. Thompson       // LCOV_EXCL_START
5922b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support QFunctionCreate");
5937a982d89SJeremy L. Thompson       // LCOV_EXCL_STOP
5942b730f8bSJeremy L Thompson     }
5957a982d89SJeremy L. Thompson 
5962b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf));
597e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
5987a982d89SJeremy L. Thompson   }
5997a982d89SJeremy L. Thompson 
6002b730f8bSJeremy L Thompson   if (strlen(source) && !strrchr(source, ':')) {
60143e1b16fSJeremy L Thompson     // LCOV_EXCL_START
60243e1b16fSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPLETE,
6032b730f8bSJeremy L Thompson                      "Provided path to source does not include function name. Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"",
60443e1b16fSJeremy L Thompson                      source);
60543e1b16fSJeremy L Thompson     // LCOV_EXCL_STOP
6062b730f8bSJeremy L Thompson   }
60743e1b16fSJeremy L Thompson 
6082b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, qf));
6097a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
6102b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
611d1d35e2fSjeremylt   (*qf)->ref_count           = 1;
612d1d35e2fSjeremylt   (*qf)->vec_length          = vec_length;
613f04ea552SJeremy L Thompson   (*qf)->is_identity         = false;
614441428dfSJeremy L Thompson   (*qf)->is_context_writable = true;
6157a982d89SJeremy L. Thompson   (*qf)->function            = f;
6166e15d496SJeremy L Thompson   (*qf)->user_flop_estimate  = -1;
61743e1b16fSJeremy L Thompson   if (strlen(source)) {
618ca5eadf8SJeremy L Thompson     size_t user_source_len = strlen(source);
619ee5a26f2SJeremy L Thompson 
6202b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(user_source_len + 1, &user_source_copy));
621ca5eadf8SJeremy L Thompson     memcpy(user_source_copy, source, user_source_len);
622ca5eadf8SJeremy L Thompson     (*qf)->user_source = user_source_copy;
62343e1b16fSJeremy L Thompson   }
6242b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields));
6252b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields));
6262b730f8bSJeremy L Thompson   CeedCall(ceed->QFunctionCreate(*qf));
627e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6287a982d89SJeremy L. Thompson }
6297a982d89SJeremy L. Thompson 
6307a982d89SJeremy L. Thompson /**
631288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
632288c0443SJeremy L Thompson 
633ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedQFunction will be created
634ea61e9acSJeremy L Thompson   @param[in]  name Name of QFunction to use from gallery
635ea61e9acSJeremy L Thompson   @param[out] qf   Address of the variable where the newly created CeedQFunction will be stored
636288c0443SJeremy L Thompson 
637288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
638288c0443SJeremy L Thompson 
6397a982d89SJeremy L. Thompson   @ref User
640288c0443SJeremy L Thompson **/
6412b730f8bSJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, CeedQFunction *qf) {
642f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
643288c0443SJeremy L Thompson 
6442b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionRegisterAll());
645288c0443SJeremy L Thompson   // Find matching backend
6462b730f8bSJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE, "No QFunction name provided");
647288c0443SJeremy L Thompson   for (size_t i = 0; i < num_qfunctions; i++) {
648288c0443SJeremy L Thompson     size_t      n;
649d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
6502b730f8bSJeremy L Thompson     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {
6512b730f8bSJeremy L Thompson     }
652d1d35e2fSjeremylt     if (n > match_len) {
653d1d35e2fSjeremylt       match_len   = n;
654f7e22acaSJeremy L Thompson       match_index = i;
655288c0443SJeremy L Thompson     }
656288c0443SJeremy L Thompson   }
6572b730f8bSJeremy L Thompson   if (!match_len) {
658138d4072Sjeremylt     // LCOV_EXCL_START
659e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
660138d4072Sjeremylt     // LCOV_EXCL_STOP
6612b730f8bSJeremy L Thompson   }
662288c0443SJeremy L Thompson 
663288c0443SJeremy L Thompson   // Create QFunction
6642b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInterior(ceed, gallery_qfunctions[match_index].vec_length, gallery_qfunctions[match_index].f,
6652b730f8bSJeremy L Thompson                                        gallery_qfunctions[match_index].source, qf));
666288c0443SJeremy L Thompson 
667288c0443SJeremy L Thompson   // QFunction specific setup
6682b730f8bSJeremy L Thompson   CeedCall(gallery_qfunctions[match_index].init(ceed, name, *qf));
669288c0443SJeremy L Thompson 
67075affc3bSjeremylt   // Copy name
6712b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name));
67243e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
673e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
674288c0443SJeremy L Thompson }
675288c0443SJeremy L Thompson 
676288c0443SJeremy L Thompson /**
677ea61e9acSJeremy L Thompson   @brief Create an identity CeedQFunction.
678*4385fb7fSSebastian Grimberg 
679ea61e9acSJeremy L Thompson   Inputs are written into outputs in the order given.
680859c15bbSJames Wright   This is useful for CeedOperators that can be represented with only the action of a CeedElemRestriction and CeedBasis, such as restriction
681859c15bbSJames Wright and prolongation operators for p-multigrid. Backends may optimize CeedOperators with this CeedQFunction to avoid the copy of input data to output
682859c15bbSJames Wright fields by using the same memory location for both.
6830219ea01SJeremy L Thompson 
684ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed object where the CeedQFunction will be created
685d1d35e2fSjeremylt   @param[in]  size     Size of the QFunction fields
686d1d35e2fSjeremylt   @param[in]  in_mode  CeedEvalMode for input to CeedQFunction
687d1d35e2fSjeremylt   @param[in]  out_mode CeedEvalMode for output to CeedQFunction
688ea61e9acSJeremy L Thompson   @param[out] qf       Address of the variable where the newly created CeedQFunction will be stored
6890219ea01SJeremy L Thompson 
6900219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
6910219ea01SJeremy L Thompson 
6927a982d89SJeremy L. Thompson   @ref User
6930219ea01SJeremy L Thompson **/
6942b730f8bSJeremy L Thompson int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, CeedEvalMode out_mode, CeedQFunction *qf) {
6952b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Identity", qf));
6962b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(*qf, "input", size, in_mode));
6972b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(*qf, "output", size, out_mode));
6980219ea01SJeremy L Thompson 
699f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
700547dbd6fSJeremy L Thompson 
701777ff853SJeremy L Thompson   CeedQFunctionContext  ctx;
7023668ca4bSJeremy L Thompson   CeedContextFieldLabel size_label;
7032b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(*qf, &ctx));
7042b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label));
7052b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetInt32(ctx, size_label, &size));
706547dbd6fSJeremy L Thompson 
707e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7080219ea01SJeremy L Thompson }
7090219ea01SJeremy L Thompson 
7100219ea01SJeremy L Thompson /**
711ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedQFunction.
712*4385fb7fSSebastian Grimberg 
713ea61e9acSJeremy L Thompson   Both pointers should be destroyed with `CeedQFunctionDestroy()`.
714512bb800SJeremy L Thompson 
715512bb800SJeremy 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.
716ea61e9acSJeremy L Thompson         This CeedQFunction will be destroyed if `*qf_copy` is the only reference to this CeedQFunction.
7179560d06aSjeremylt 
718ea61e9acSJeremy L Thompson   @param[in]  qf      CeedQFunction to copy reference to
7199560d06aSjeremylt   @param[out] qf_copy Variable to store copied reference
7209560d06aSjeremylt 
7219560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7229560d06aSjeremylt 
7239560d06aSjeremylt   @ref User
7249560d06aSjeremylt **/
7259560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
7262b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionReference(qf));
7272b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(qf_copy));
7289560d06aSjeremylt   *qf_copy = qf;
7299560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7309560d06aSjeremylt }
7319560d06aSjeremylt 
7329560d06aSjeremylt /**
733a0a97fcfSJed Brown   @brief Add a CeedQFunction input
734b11c1e72Sjeremylt 
735ea61e9acSJeremy L Thompson   @param[in,out] qf         CeedQFunction
736ea61e9acSJeremy L Thompson   @param[in]     field_name Name of QFunction field
7375330800fSSebastian Grimberg   @param[in]     size       Size of QFunction field, (num_comp * 1) for @ref CEED_EVAL_NONE,
7385330800fSSebastian 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,
7395330800fSSebastian Grimberg (num_comp * dim) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and
7405330800fSSebastian Grimberg (num_comp * curl_dim) with curl_dim = 1 if dim < 3 else dim for @ref CEED_EVAL_CURL.
741ea61e9acSJeremy L Thompson   @param[in]     eval_mode  \ref CEED_EVAL_NONE to use values directly,
742b11c1e72Sjeremylt                               \ref CEED_EVAL_INTERP to use interpolated values,
7435330800fSSebastian Grimberg                               \ref CEED_EVAL_GRAD to use gradients,
7445330800fSSebastian Grimberg                               \ref CEED_EVAL_DIV to use divergence,
7455330800fSSebastian Grimberg                               \ref CEED_EVAL_CURL to use curl.
746b11c1e72Sjeremylt 
747b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
748dfdf5a53Sjeremylt 
7497a982d89SJeremy L. Thompson   @ref User
750b11c1e72Sjeremylt **/
7512b730f8bSJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
7522b730f8bSJeremy L Thompson   if (qf->is_immutable) {
753e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
7542b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
755e15f9bd0SJeremy L Thompson     // LCOV_EXCL_STOP
7562b730f8bSJeremy L Thompson   }
7572b730f8bSJeremy L Thompson   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1)) {
758e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
7592b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION, "CEED_EVAL_WEIGHT should have size 1");
760e15f9bd0SJeremy L Thompson     // LCOV_EXCL_STOP
7612b730f8bSJeremy L Thompson   }
762643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
763643fbb69SJeremy L Thompson     if (!strcmp(field_name, qf->input_fields[i]->field_name)) {
764643fbb69SJeremy L Thompson       // LCOV_EXCL_START
765643fbb69SJeremy L Thompson       return CeedError(qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
766643fbb69SJeremy L Thompson       // LCOV_EXCL_STOP
767643fbb69SJeremy L Thompson     }
768643fbb69SJeremy L Thompson   }
769643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
770643fbb69SJeremy L Thompson     if (!strcmp(field_name, qf->output_fields[i]->field_name)) {
771643fbb69SJeremy L Thompson       // LCOV_EXCL_START
772643fbb69SJeremy L Thompson       return CeedError(qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
773643fbb69SJeremy L Thompson       // LCOV_EXCL_STOP
774643fbb69SJeremy L Thompson     }
775643fbb69SJeremy L Thompson   }
7762b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], field_name, size, eval_mode));
777d1d35e2fSjeremylt   qf->num_input_fields++;
778e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
779d7b241e6Sjeremylt }
780d7b241e6Sjeremylt 
781b11c1e72Sjeremylt /**
782a0a97fcfSJed Brown   @brief Add a CeedQFunction output
783b11c1e72Sjeremylt 
784ea61e9acSJeremy L Thompson   @param[in,out] qf         CeedQFunction
785ea61e9acSJeremy L Thompson   @param[in]     field_name Name of QFunction field
7865330800fSSebastian Grimberg   @param[in]     size       Size of QFunction field, (num_comp * 1) for @ref CEED_EVAL_NONE,
7875330800fSSebastian 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,
7885330800fSSebastian Grimberg (num_comp * dim) for @ref CEED_EVAL_GRAD, or (num_comp * 1) for @ref CEED_EVAL_DIV, and
7895330800fSSebastian Grimberg (num_comp * curl_dim) with curl_dim = 1 if dim < 3 else dim for @ref CEED_EVAL_CURL.
790ea61e9acSJeremy L Thompson   @param[in]     eval_mode  \ref CEED_EVAL_NONE to use values directly,
791b11c1e72Sjeremylt                               \ref CEED_EVAL_INTERP to use interpolated values,
7925330800fSSebastian Grimberg                               \ref CEED_EVAL_GRAD to use gradients,
7935330800fSSebastian Grimberg                               \ref CEED_EVAL_DIV to use divergence,
7945330800fSSebastian Grimberg                               \ref CEED_EVAL_CURL to use curl.
795b11c1e72Sjeremylt 
796b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
797dfdf5a53Sjeremylt 
7987a982d89SJeremy L. Thompson   @ref User
799b11c1e72Sjeremylt **/
8002b730f8bSJeremy L Thompson int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
8012b730f8bSJeremy L Thompson   if (qf->is_immutable) {
802e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
8032b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
804e15f9bd0SJeremy L Thompson     // LCOV_EXCL_STOP
8052b730f8bSJeremy L Thompson   }
8062b730f8bSJeremy L Thompson   if (eval_mode == CEED_EVAL_WEIGHT) {
807c042f62fSJeremy L Thompson     // LCOV_EXCL_START
8082b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION, "Cannot create QFunction output with CEED_EVAL_WEIGHT");
809c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
8102b730f8bSJeremy L Thompson   }
811643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
812643fbb69SJeremy L Thompson     if (!strcmp(field_name, qf->input_fields[i]->field_name)) {
813643fbb69SJeremy L Thompson       // LCOV_EXCL_START
814643fbb69SJeremy L Thompson       return CeedError(qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
815643fbb69SJeremy L Thompson       // LCOV_EXCL_STOP
816643fbb69SJeremy L Thompson     }
817643fbb69SJeremy L Thompson   }
818643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
819643fbb69SJeremy L Thompson     if (!strcmp(field_name, qf->output_fields[i]->field_name)) {
820643fbb69SJeremy L Thompson       // LCOV_EXCL_START
821643fbb69SJeremy L Thompson       return CeedError(qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
822643fbb69SJeremy L Thompson       // LCOV_EXCL_STOP
823643fbb69SJeremy L Thompson     }
824643fbb69SJeremy L Thompson   }
8252b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], field_name, size, eval_mode));
826d1d35e2fSjeremylt   qf->num_output_fields++;
827e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
828d7b241e6Sjeremylt }
829d7b241e6Sjeremylt 
830dfdf5a53Sjeremylt /**
83143bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
83243bbe138SJeremy L Thompson 
833ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedQFunction as immutable.
834f04ea552SJeremy L Thompson 
835ea61e9acSJeremy L Thompson   @param[in]  qf                CeedQFunction
836f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
837f74ec584SJeremy L Thompson   @param[out] input_fields      Variable to store input fields
838f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
839f74ec584SJeremy L Thompson   @param[out] output_fields     Variable to store output fields
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 CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields, CeedQFunctionField **input_fields, CeedInt *num_output_fields,
84643bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
847f04ea552SJeremy L Thompson   qf->is_immutable = true;
84843bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
84943bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
85043bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
85143bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
85243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
85343bbe138SJeremy L Thompson }
85443bbe138SJeremy L Thompson 
85543bbe138SJeremy L Thompson /**
85643bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
85743bbe138SJeremy L Thompson 
858ea61e9acSJeremy L Thompson   @param[in]  qf_field   CeedQFunctionField
85943bbe138SJeremy L Thompson   @param[out] field_name Variable to store the field name
86043bbe138SJeremy L Thompson 
86143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
86243bbe138SJeremy L Thompson 
863e9b533fbSJeremy L Thompson   @ref Advanced
86443bbe138SJeremy L Thompson **/
86543bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
86643bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
86743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
86843bbe138SJeremy L Thompson }
86943bbe138SJeremy L Thompson 
87043bbe138SJeremy L Thompson /**
87143bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
87243bbe138SJeremy L Thompson 
873ea61e9acSJeremy L Thompson   @param[in]  qf_field CeedQFunctionField
87443bbe138SJeremy L Thompson   @param[out] size     Variable to store the size of the field
87543bbe138SJeremy L Thompson 
87643bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
87743bbe138SJeremy L Thompson 
878e9b533fbSJeremy L Thompson   @ref Advanced
87943bbe138SJeremy L Thompson **/
88043bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
88143bbe138SJeremy L Thompson   *size = qf_field->size;
88243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
88343bbe138SJeremy L Thompson }
88443bbe138SJeremy L Thompson 
88543bbe138SJeremy L Thompson /**
88643bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
88743bbe138SJeremy L Thompson 
888ea61e9acSJeremy L Thompson   @param[in]  qf_field  CeedQFunctionField
88943bbe138SJeremy L Thompson   @param[out] eval_mode Variable to store the field evaluation mode
89043bbe138SJeremy L Thompson 
89143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
89243bbe138SJeremy L Thompson 
893e9b533fbSJeremy L Thompson   @ref Advanced
89443bbe138SJeremy L Thompson **/
8952b730f8bSJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, CeedEvalMode *eval_mode) {
89643bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
89743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
89843bbe138SJeremy L Thompson }
89943bbe138SJeremy L Thompson 
90043bbe138SJeremy L Thompson /**
9014ce2993fSjeremylt   @brief Set global context for a CeedQFunction
902b11c1e72Sjeremylt 
903ea61e9acSJeremy L Thompson   @param[in,out] qf  CeedQFunction
904ea61e9acSJeremy L Thompson   @param[in]     ctx Context data to set
905b11c1e72Sjeremylt 
906b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
907dfdf5a53Sjeremylt 
9087a982d89SJeremy L. Thompson   @ref User
909b11c1e72Sjeremylt **/
910777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
9112b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&qf->ctx));
912d7b241e6Sjeremylt   qf->ctx = ctx;
9139e77b9c8SJeremy L Thompson   if (ctx) {
9142b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextReference(ctx));
9159e77b9c8SJeremy L Thompson   }
916e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
917d7b241e6Sjeremylt }
918d7b241e6Sjeremylt 
919b11c1e72Sjeremylt /**
920441428dfSJeremy L Thompson   @brief Set writability of CeedQFunctionContext when calling the `CeedQFunctionUser`.
921*4385fb7fSSebastian Grimberg 
922859c15bbSJames Wright   The default value is `is_writable == true`.
923441428dfSJeremy L Thompson 
924ea61e9acSJeremy L Thompson   Setting `is_writable == true` indicates the `CeedQFunctionUser` writes into the CeedQFunctionContextData and requires memory syncronization
925441428dfSJeremy L Thompson after calling `CeedQFunctionApply()`.
926441428dfSJeremy L Thompson 
927ea61e9acSJeremy L Thompson   Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not modify the CeedQFunctionContextData.
928ea61e9acSJeremy L Thompson   Violating this assertion may lead to inconsistent data.
929441428dfSJeremy L Thompson 
930441428dfSJeremy L Thompson   Setting `is_writable == false` may offer a performance improvement on GPU backends.
931441428dfSJeremy L Thompson 
932ea61e9acSJeremy L Thompson   @param[in,out] qf          CeedQFunction
933ea61e9acSJeremy L Thompson   @param[in]     is_writable Writability status
934441428dfSJeremy L Thompson 
935441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
936441428dfSJeremy L Thompson 
937441428dfSJeremy L Thompson   @ref User
938441428dfSJeremy L Thompson **/
939441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
940441428dfSJeremy L Thompson   qf->is_context_writable = is_writable;
941441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
942441428dfSJeremy L Thompson }
943441428dfSJeremy L Thompson 
944441428dfSJeremy L Thompson /**
9456e15d496SJeremy L Thompson   @brief Set estimated number of FLOPs per quadrature required to apply QFunction
9466e15d496SJeremy L Thompson 
947ea61e9acSJeremy L Thompson   @param[in]  qf    QFunction to estimate FLOPs for
948ea61e9acSJeremy L Thompson   @param[out] flops FLOPs per quadrature point estimate
9496e15d496SJeremy L Thompson 
9506e15d496SJeremy L Thompson   @ref Backend
9516e15d496SJeremy L Thompson **/
9529d36ca50SJeremy L Thompson int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
9532b730f8bSJeremy L Thompson   if (flops < 0) {
9546e15d496SJeremy L Thompson     // LCOV_EXCL_START
9552b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_INCOMPATIBLE, "Must set non-negative FLOPs estimate");
9566e15d496SJeremy L Thompson     // LCOV_EXCL_STOP
9572b730f8bSJeremy L Thompson   }
9586e15d496SJeremy L Thompson   qf->user_flop_estimate = flops;
9596e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9606e15d496SJeremy L Thompson }
9616e15d496SJeremy L Thompson 
9626e15d496SJeremy L Thompson /**
96375affc3bSjeremylt   @brief View a CeedQFunction
96475affc3bSjeremylt 
96575affc3bSjeremylt   @param[in] qf     CeedQFunction to view
96675affc3bSjeremylt   @param[in] stream Stream to write; typically stdout/stderr or a file
96775affc3bSjeremylt 
96875affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
96975affc3bSjeremylt 
9707a982d89SJeremy L. Thompson   @ref User
97175affc3bSjeremylt **/
97275affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
973ca5eadf8SJeremy L Thompson   char *kernel_name;
97475affc3bSjeremylt 
9752b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetKernelName(qf, &kernel_name));
9762b730f8bSJeremy L Thompson   fprintf(stream, "%sCeedQFunction - %s\n", qf->is_gallery ? "Gallery " : "User ", qf->is_gallery ? qf->gallery_name : kernel_name);
97775affc3bSjeremylt 
9782b730f8bSJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " input field%s:\n", qf->num_input_fields, qf->num_input_fields > 1 ? "s" : "");
979d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
9802b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream));
98175affc3bSjeremylt   }
98275affc3bSjeremylt 
9832b730f8bSJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " output field%s:\n", qf->num_output_fields, qf->num_output_fields > 1 ? "s" : "");
984d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
9852b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream));
98675affc3bSjeremylt   }
987e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
98875affc3bSjeremylt }
98975affc3bSjeremylt 
99075affc3bSjeremylt /**
991b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunction
992b7c9bbdaSJeremy L Thompson 
993ea61e9acSJeremy L Thompson   @param[in]  qf   CeedQFunction
994b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store Ceed
995b7c9bbdaSJeremy L Thompson 
996b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
997b7c9bbdaSJeremy L Thompson 
998b7c9bbdaSJeremy L Thompson   @ref Advanced
999b7c9bbdaSJeremy L Thompson **/
1000b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
1001b7c9bbdaSJeremy L Thompson   *ceed = qf->ceed;
1002b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1003b7c9bbdaSJeremy L Thompson }
1004b7c9bbdaSJeremy L Thompson 
1005b7c9bbdaSJeremy L Thompson /**
1006b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
1007b11c1e72Sjeremylt 
1008ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedQFunction as immutable.
1009f04ea552SJeremy L Thompson 
1010ea61e9acSJeremy L Thompson   @param[in]  qf CeedQFunction
1011ea61e9acSJeremy L Thompson   @param[in]  Q  Number of quadrature points
101234138859Sjeremylt   @param[in]  u  Array of input CeedVectors
101334138859Sjeremylt   @param[out] v  Array of output CeedVectors
1014b11c1e72Sjeremylt 
1015b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1016dfdf5a53Sjeremylt 
10177a982d89SJeremy L. Thompson   @ref User
1018b11c1e72Sjeremylt **/
10192b730f8bSJeremy L Thompson int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, CeedVector *u, CeedVector *v) {
10202b730f8bSJeremy L Thompson   if (!qf->Apply) {
1021c042f62fSJeremy L Thompson     // LCOV_EXCL_START
10222b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support QFunctionApply");
1023c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
10242b730f8bSJeremy L Thompson   }
10252b730f8bSJeremy L Thompson   if (Q % qf->vec_length) {
1026c042f62fSJeremy L Thompson     // LCOV_EXCL_START
10272b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION, "Number of quadrature points %" CeedInt_FMT " must be a multiple of %" CeedInt_FMT, Q,
10282b730f8bSJeremy L Thompson                      qf->vec_length);
1029c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
10302b730f8bSJeremy L Thompson   }
1031f04ea552SJeremy L Thompson   qf->is_immutable = true;
10322b730f8bSJeremy L Thompson   CeedCall(qf->Apply(qf, Q, u, v));
1033e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1034d7b241e6Sjeremylt }
1035d7b241e6Sjeremylt 
1036b11c1e72Sjeremylt /**
1037b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
1038b11c1e72Sjeremylt 
1039ea61e9acSJeremy L Thompson   @param[in,out] qf CeedQFunction to destroy
1040b11c1e72Sjeremylt 
1041b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1042dfdf5a53Sjeremylt 
10437a982d89SJeremy L. Thompson   @ref User
1044b11c1e72Sjeremylt **/
1045d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
1046ad6481ceSJeremy L Thompson   if (!*qf || --(*qf)->ref_count > 0) {
1047ad6481ceSJeremy L Thompson     *qf = NULL;
1048ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1049ad6481ceSJeremy L Thompson   }
1050fe2413ffSjeremylt   // Backend destroy
1051d7b241e6Sjeremylt   if ((*qf)->Destroy) {
10522b730f8bSJeremy L Thompson     CeedCall((*qf)->Destroy(*qf));
1053d7b241e6Sjeremylt   }
1054fe2413ffSjeremylt   // Free fields
105592ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_input_fields; i++) {
10562b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->input_fields[i]).field_name));
10572b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->input_fields[i]));
1058fe2413ffSjeremylt   }
105992ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_output_fields; i++) {
10602b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->output_fields[i]).field_name));
10612b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->output_fields[i]));
1062fe2413ffSjeremylt   }
10632b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->input_fields));
10642b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->output_fields));
1065777ff853SJeremy L Thompson 
1066777ff853SJeremy L Thompson   // User context data object
10672b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&(*qf)->ctx));
1068fe2413ffSjeremylt 
10692b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->user_source));
10702b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->source_path));
10712b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->gallery_name));
10722b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->kernel_name));
10732b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*qf)->ceed));
10742b730f8bSJeremy L Thompson   CeedCall(CeedFree(qf));
1075e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1076d7b241e6Sjeremylt }
1077d7b241e6Sjeremylt 
1078d7b241e6Sjeremylt /// @}
1079