xref: /libCEED/interface/ceed-qfunction.c (revision 49aac155e7a09736f56fb3abac0f57dab29f7cbf)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3d7b241e6Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5d7b241e6Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7d7b241e6Sjeremylt 
83d576824SJeremy L Thompson #include <ceed-impl.h>
9*49aac155SJeremy 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
92ea61e9acSJeremy L Thompson   @param[in]  size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or (num_comp * 1) for @ref CEED_EVAL_NONE, @ref
93ea61e9acSJeremy L Thompson CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT
94ea61e9acSJeremy L Thompson   @param[in]  eval_mode   \ref CEED_EVAL_NONE to use values directly,
957a982d89SJeremy L. Thompson                             \ref CEED_EVAL_INTERP to use interpolated values,
967a982d89SJeremy L. Thompson                             \ref CEED_EVAL_GRAD to use gradients,
977a982d89SJeremy L. Thompson                             \ref CEED_EVAL_WEIGHT to use quadrature weights.
987a982d89SJeremy L. Thompson 
997a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1007a982d89SJeremy L. Thompson 
1017a982d89SJeremy L. Thompson   @ref Developer
1027a982d89SJeremy L. Thompson **/
1032b730f8bSJeremy L Thompson static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
1042b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, f));
1052b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*f)->field_name));
1067a982d89SJeremy L. Thompson   (*f)->size      = size;
107d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
108e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1097a982d89SJeremy L. Thompson }
1107a982d89SJeremy L. Thompson 
1117a982d89SJeremy L. Thompson /**
1127a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1137a982d89SJeremy L. Thompson 
1147a982d89SJeremy L. Thompson   @param[in] field        QFunction field to view
115d1d35e2fSjeremylt   @param[in] field_number Number of field being viewed
1164c4400c7SValeria Barra   @param[in] in           true for input field, false for output
1177a982d89SJeremy L. Thompson   @param[in] stream       Stream to view to, e.g., stdout
1187a982d89SJeremy L. Thompson 
1197a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1207a982d89SJeremy L. Thompson 
1217a982d89SJeremy L. Thompson   @ref Utility
1227a982d89SJeremy L. Thompson **/
1232b730f8bSJeremy L Thompson static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt field_number, bool in, FILE *stream) {
1247a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1258229195eSjeremylt   char       *field_name;
1262b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldGetName(field, &field_name));
1278229195eSjeremylt   CeedInt size;
1282b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldGetSize(field, &size));
1298229195eSjeremylt   CeedEvalMode eval_mode;
1302b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldGetEvalMode(field, &eval_mode));
1312b730f8bSJeremy L Thompson   fprintf(stream,
1322b730f8bSJeremy L Thompson           "    %s field %" CeedInt_FMT
1332b730f8bSJeremy L Thompson           ":\n"
1347a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1352b730f8bSJeremy L Thompson           "      Size: %" CeedInt_FMT
1362b730f8bSJeremy L Thompson           "\n"
1377a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1388229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
139e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1407a982d89SJeremy L. Thompson }
1417a982d89SJeremy L. Thompson 
142777ff853SJeremy L Thompson /**
143777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
144777ff853SJeremy L Thompson 
145ea61e9acSJeremy L Thompson   @param[in,out] qf     CeedQFunction
146ea61e9acSJeremy L Thompson   @param[in]     status Boolean value to set as Fortran status
147777ff853SJeremy L Thompson 
148777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
149777ff853SJeremy L Thompson 
150777ff853SJeremy L Thompson   @ref Backend
151777ff853SJeremy L Thompson **/
152777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
153f04ea552SJeremy L Thompson   qf->is_fortran = status;
154e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
155777ff853SJeremy L Thompson }
156777ff853SJeremy L Thompson 
1577a982d89SJeremy L. Thompson /// @}
1587a982d89SJeremy L. Thompson 
1597a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1607a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1617a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1627a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1637a982d89SJeremy L. Thompson /// @{
1647a982d89SJeremy L. Thompson 
1657a982d89SJeremy L. Thompson /**
1667a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1677a982d89SJeremy L. Thompson 
168ea61e9acSJeremy L Thompson   @param[in]  qf         CeedQFunction
169d1d35e2fSjeremylt   @param[out] vec_length Variable to store vector length
1707a982d89SJeremy L. Thompson 
1717a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1727a982d89SJeremy L. Thompson 
1737a982d89SJeremy L. Thompson   @ref Backend
1747a982d89SJeremy L. Thompson **/
175d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
176d1d35e2fSjeremylt   *vec_length = qf->vec_length;
177e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1787a982d89SJeremy L. Thompson }
1797a982d89SJeremy L. Thompson 
1807a982d89SJeremy L. Thompson /**
1817a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
1827a982d89SJeremy L. Thompson 
183ea61e9acSJeremy L Thompson   @param[in]  qf         CeedQFunction
184d1d35e2fSjeremylt   @param[out] num_input  Variable to store number of input fields
185d1d35e2fSjeremylt   @param[out] num_output Variable to store number of output fields
1867a982d89SJeremy L. Thompson 
1877a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1887a982d89SJeremy L. Thompson 
1897a982d89SJeremy L. Thompson   @ref Backend
1907a982d89SJeremy L. Thompson **/
1912b730f8bSJeremy L Thompson int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, CeedInt *num_output) {
192d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
193d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
194e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1957a982d89SJeremy L. Thompson }
1967a982d89SJeremy L. Thompson 
1977a982d89SJeremy L. Thompson /**
19843e1b16fSJeremy L Thompson   @brief Get the name of the user function for a CeedQFunction
1997a982d89SJeremy L. Thompson 
200ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
20143e1b16fSJeremy L Thompson   @param[out] kernel_name Variable to store source path string
2027a982d89SJeremy L. Thompson 
2037a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2047a982d89SJeremy L. Thompson 
2057a982d89SJeremy L. Thompson   @ref Backend
2067a982d89SJeremy L. Thompson **/
20743e1b16fSJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, char **kernel_name) {
208ca5eadf8SJeremy L Thompson   if (!qf->kernel_name) {
209ca5eadf8SJeremy L Thompson     Ceed  ceed;
210ca5eadf8SJeremy L Thompson     char *kernel_name_copy;
2112b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
212ca5eadf8SJeremy L Thompson 
213ca5eadf8SJeremy L Thompson     if (qf->user_source) {
214ca5eadf8SJeremy L Thompson       const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
215ca5eadf8SJeremy L Thompson       size_t      kernel_name_len = strlen(kernel_name);
216ca5eadf8SJeremy L Thompson 
2172b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(kernel_name_len + 1, &kernel_name_copy));
218ca5eadf8SJeremy L Thompson       memcpy(kernel_name_copy, kernel_name, kernel_name_len);
219ca5eadf8SJeremy L Thompson     } else {
2202b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &kernel_name_copy));
221ca5eadf8SJeremy L Thompson     }
222ca5eadf8SJeremy L Thompson     qf->kernel_name = kernel_name_copy;
223ca5eadf8SJeremy L Thompson   }
224ca5eadf8SJeremy L Thompson 
22543e1b16fSJeremy L Thompson   *kernel_name = (char *)qf->kernel_name;
22643e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
22743e1b16fSJeremy L Thompson }
22843e1b16fSJeremy L Thompson 
22943e1b16fSJeremy L Thompson /**
23043e1b16fSJeremy L Thompson   @brief Get the source path string for a CeedQFunction
23143e1b16fSJeremy L Thompson 
232ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
23343e1b16fSJeremy L Thompson   @param[out] source_path Variable to store source path string
23443e1b16fSJeremy L Thompson 
23543e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
23643e1b16fSJeremy L Thompson 
23743e1b16fSJeremy L Thompson   @ref Backend
23843e1b16fSJeremy L Thompson **/
23943e1b16fSJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) {
240ca5eadf8SJeremy L Thompson   if (!qf->source_path && qf->user_source) {
241ca5eadf8SJeremy L Thompson     Ceed        ceed;
242ca5eadf8SJeremy L Thompson     bool        is_absolute_path;
243ca5eadf8SJeremy L Thompson     char       *absolute_path, *source_path_copy;
244ca5eadf8SJeremy L Thompson     const char *kernel_name     = strrchr(qf->user_source, ':') + 1;
245ca5eadf8SJeremy L Thompson     size_t      kernel_name_len = strlen(kernel_name);
246ca5eadf8SJeremy L Thompson 
2472b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetCeed(qf, &ceed));
248ca5eadf8SJeremy L Thompson 
2492b730f8bSJeremy L Thompson     CeedCall(CeedCheckFilePath(ceed, qf->user_source, &is_absolute_path));
250ca5eadf8SJeremy L Thompson     if (is_absolute_path) {
251ca5eadf8SJeremy L Thompson       absolute_path = (char *)qf->user_source;
252ca5eadf8SJeremy L Thompson     } else {
2532b730f8bSJeremy L Thompson       CeedCall(CeedGetJitAbsolutePath(ceed, qf->user_source, &absolute_path));
254ca5eadf8SJeremy L Thompson     }
255ca5eadf8SJeremy L Thompson 
256ca5eadf8SJeremy L Thompson     size_t source_len = strlen(absolute_path) - kernel_name_len - 1;
2572b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(source_len + 1, &source_path_copy));
258ca5eadf8SJeremy L Thompson     memcpy(source_path_copy, absolute_path, source_len);
259ca5eadf8SJeremy L Thompson     qf->source_path = source_path_copy;
260ca5eadf8SJeremy L Thompson 
2612b730f8bSJeremy L Thompson     if (!is_absolute_path) CeedCall(CeedFree(&absolute_path));
262ca5eadf8SJeremy L Thompson   }
263ca5eadf8SJeremy L Thompson 
26443e1b16fSJeremy L Thompson   *source_path = (char *)qf->source_path;
265e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2667a982d89SJeremy L. Thompson }
2677a982d89SJeremy L. Thompson 
2687a982d89SJeremy L. Thompson /**
269ea61e9acSJeremy L Thompson   @brief Initialize and load QFunction source file into string buffer, including full text of local files in place of `#include "local.h"`.
2703d3250a0SJeremy L Thompson            The `buffer` is set to `NULL` if there is no QFunction source file.
2713d3250a0SJeremy L Thompson          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
2723d3250a0SJeremy L Thompson 
273ea61e9acSJeremy L Thompson   @param[in]  qf            CeedQFunction
274f74ec584SJeremy L Thompson   @param[out] source_buffer String buffer for source file contents
2753d3250a0SJeremy L Thompson 
2763d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2773d3250a0SJeremy L Thompson 
2783d3250a0SJeremy L Thompson   @ref Backend
2793d3250a0SJeremy L Thompson **/
2803d3250a0SJeremy L Thompson int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, char **source_buffer) {
2813d3250a0SJeremy L Thompson   char *source_path;
2823d3250a0SJeremy L Thompson 
2832b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetSourcePath(qf, &source_path));
2843d3250a0SJeremy L Thompson   *source_buffer = NULL;
2853d3250a0SJeremy L Thompson   if (source_path) {
2862b730f8bSJeremy L Thompson     CeedCall(CeedLoadSourceToBuffer(qf->ceed, source_path, source_buffer));
2873d3250a0SJeremy L Thompson   }
2883d3250a0SJeremy L Thompson 
2893d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2903d3250a0SJeremy L Thompson }
2913d3250a0SJeremy L Thompson 
2923d3250a0SJeremy L Thompson /**
2937a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2947a982d89SJeremy L. Thompson 
295ea61e9acSJeremy L Thompson   @param[in]  qf CeedQFunction
2967a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
2977a982d89SJeremy L. Thompson 
2987a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2997a982d89SJeremy L. Thompson 
3007a982d89SJeremy L. Thompson   @ref Backend
3017a982d89SJeremy L. Thompson **/
3027a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
3037a982d89SJeremy L. Thompson   *f = qf->function;
304e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3057a982d89SJeremy L. Thompson }
3067a982d89SJeremy L. Thompson 
3077a982d89SJeremy L. Thompson /**
308777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
309ea61e9acSJeremy L Thompson            Note: For QFunctions from the Fortran interface, this function will return the Fortran context CeedQFunctionContext.
3107a982d89SJeremy L. Thompson 
311ea61e9acSJeremy L Thompson   @param[in]  qf  CeedQFunction
312777ff853SJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
3137a982d89SJeremy L. Thompson 
3147a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3157a982d89SJeremy L. Thompson 
3167a982d89SJeremy L. Thompson   @ref Backend
3177a982d89SJeremy L. Thompson **/
318777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
3197a982d89SJeremy L. Thompson   *ctx = qf->ctx;
320e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3217a982d89SJeremy L. Thompson }
3227a982d89SJeremy L. Thompson 
3237a982d89SJeremy L. Thompson /**
324441428dfSJeremy L Thompson   @brief Get context data of a CeedQFunction
325441428dfSJeremy L Thompson 
326ea61e9acSJeremy L Thompson   @param[in]  qf       CeedQFunction
327ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
328ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
329441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
330441428dfSJeremy L Thompson 
331441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
332441428dfSJeremy L Thompson 
333441428dfSJeremy L Thompson   @ref Backend
334441428dfSJeremy L Thompson **/
3352b730f8bSJeremy L Thompson int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
336441428dfSJeremy L Thompson   bool                 is_writable;
337441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
338441428dfSJeremy L Thompson 
3392b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
340441428dfSJeremy L Thompson   if (ctx) {
3412b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
342441428dfSJeremy L Thompson     if (is_writable) {
3432b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
344441428dfSJeremy L Thompson     } else {
3452b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
346441428dfSJeremy L Thompson     }
347441428dfSJeremy L Thompson   } else {
348441428dfSJeremy L Thompson     *(void **)data = NULL;
349441428dfSJeremy L Thompson   }
350441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
351441428dfSJeremy L Thompson }
352441428dfSJeremy L Thompson 
353441428dfSJeremy L Thompson /**
354441428dfSJeremy L Thompson   @brief Restore context data of a CeedQFunction
355441428dfSJeremy L Thompson 
356ea61e9acSJeremy L Thompson   @param[in]     qf   CeedQFunction
357ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
358441428dfSJeremy L Thompson 
359441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
360441428dfSJeremy L Thompson 
361441428dfSJeremy L Thompson   @ref Backend
362441428dfSJeremy L Thompson **/
363441428dfSJeremy L Thompson int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) {
364441428dfSJeremy L Thompson   bool                 is_writable;
365441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
366441428dfSJeremy L Thompson 
3672b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(qf, &ctx));
368441428dfSJeremy L Thompson   if (ctx) {
3692b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
370441428dfSJeremy L Thompson     if (is_writable) {
3712b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
372441428dfSJeremy L Thompson     } else {
3732b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
374441428dfSJeremy L Thompson     }
375441428dfSJeremy L Thompson   }
376441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
377441428dfSJeremy L Thompson }
378441428dfSJeremy L Thompson 
379441428dfSJeremy L Thompson /**
3807a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
381ea61e9acSJeremy L Thompson            Note: For all QFunctions this function will return the user CeedQFunctionContext and not interface context CeedQFunctionContext, if any
382ea61e9acSJeremy L Thompson such object exists.
3837a982d89SJeremy L. Thompson 
384ea61e9acSJeremy L Thompson   @param[in]  qf  CeedQFunction
385777ff853SJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
3867a982d89SJeremy L. Thompson 
3877a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3887a982d89SJeremy L. Thompson   @ref Backend
3897a982d89SJeremy L. Thompson **/
390777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
391f04ea552SJeremy L Thompson   if (qf->is_fortran) {
392d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
3932b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx));
3948cb0412aSJeremy L Thompson     *ctx = fortran_ctx->inner_ctx;
3952b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx));
3967a982d89SJeremy L. Thompson   } else {
3977a982d89SJeremy L. Thompson     *ctx = qf->ctx;
3987a982d89SJeremy L. Thompson   }
399e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4007a982d89SJeremy L. Thompson }
4017a982d89SJeremy L. Thompson 
4027a982d89SJeremy L. Thompson /**
403441428dfSJeremy L Thompson   @brief Get inner context data of a CeedQFunction
404441428dfSJeremy L Thompson 
405ea61e9acSJeremy L Thompson   @param[in]  qf       CeedQFunction
406ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
407ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
408441428dfSJeremy L Thompson   @param[out] data     Data on memory type mem_type
409441428dfSJeremy L Thompson 
410441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
411441428dfSJeremy L Thompson 
412441428dfSJeremy L Thompson   @ref Backend
413441428dfSJeremy L Thompson **/
4142b730f8bSJeremy L Thompson int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
415441428dfSJeremy L Thompson   bool                 is_writable;
416441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
417441428dfSJeremy L Thompson 
4182b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
419441428dfSJeremy L Thompson   if (ctx) {
4202b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
421441428dfSJeremy L Thompson     if (is_writable) {
4222b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
423441428dfSJeremy L Thompson     } else {
4242b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
425441428dfSJeremy L Thompson     }
426441428dfSJeremy L Thompson   } else {
427441428dfSJeremy L Thompson     *(void **)data = NULL;
428441428dfSJeremy L Thompson   }
429441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
430441428dfSJeremy L Thompson }
431441428dfSJeremy L Thompson 
432441428dfSJeremy L Thompson /**
433441428dfSJeremy L Thompson   @brief Restore inner context data of a CeedQFunction
434441428dfSJeremy L Thompson 
435ea61e9acSJeremy L Thompson   @param[in]     qf   CeedQFunction
436ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
437441428dfSJeremy L Thompson 
438441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
439441428dfSJeremy L Thompson 
440441428dfSJeremy L Thompson   @ref Backend
441441428dfSJeremy L Thompson **/
442441428dfSJeremy L Thompson int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) {
443441428dfSJeremy L Thompson   bool                 is_writable;
444441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
445441428dfSJeremy L Thompson 
4462b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
447441428dfSJeremy L Thompson   if (ctx) {
4482b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
449441428dfSJeremy L Thompson     if (is_writable) {
4502b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, data));
451441428dfSJeremy L Thompson     } else {
4522b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
453441428dfSJeremy L Thompson     }
454441428dfSJeremy L Thompson   }
455441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
456441428dfSJeremy L Thompson }
457441428dfSJeremy L Thompson 
458441428dfSJeremy L Thompson /**
4597a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
4607a982d89SJeremy L. Thompson 
461ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
462d1d35e2fSjeremylt   @param[out] is_identity Variable to store identity status
4637a982d89SJeremy L. Thompson 
4647a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4657a982d89SJeremy L. Thompson 
4667a982d89SJeremy L. Thompson   @ref Backend
4677a982d89SJeremy L. Thompson **/
468d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
469f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
470e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4717a982d89SJeremy L. Thompson }
4727a982d89SJeremy L. Thompson 
4737a982d89SJeremy L. Thompson /**
474441428dfSJeremy L Thompson   @brief Determine if QFunctionContext is writable
475441428dfSJeremy L Thompson 
476ea61e9acSJeremy L Thompson   @param[in]  qf          CeedQFunction
477ea61e9acSJeremy L Thompson   @param[out] is_writable Variable to store context writeable status
478441428dfSJeremy L Thompson 
479441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
480441428dfSJeremy L Thompson 
481441428dfSJeremy L Thompson   @ref Backend
482441428dfSJeremy L Thompson **/
483441428dfSJeremy L Thompson int CeedQFunctionIsContextWritable(CeedQFunction qf, bool *is_writable) {
484441428dfSJeremy L Thompson   *is_writable = qf->is_context_writable;
485441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
486441428dfSJeremy L Thompson }
487441428dfSJeremy L Thompson 
488441428dfSJeremy L Thompson /**
4897a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
4907a982d89SJeremy L. Thompson 
491ea61e9acSJeremy L Thompson   @param[in]  qf   CeedQFunction
4927a982d89SJeremy L. Thompson   @param[out] data Variable to store data
4937a982d89SJeremy L. Thompson 
4947a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4957a982d89SJeremy L. Thompson 
4967a982d89SJeremy L. Thompson   @ref Backend
4977a982d89SJeremy L. Thompson **/
498777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
499777ff853SJeremy L Thompson   *(void **)data = qf->data;
500e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5017a982d89SJeremy L. Thompson }
5027a982d89SJeremy L. Thompson 
5037a982d89SJeremy L. Thompson /**
5047a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
5057a982d89SJeremy L. Thompson 
506ea61e9acSJeremy L Thompson   @param[in,out] qf   CeedQFunction
507ea61e9acSJeremy L Thompson   @param[in]     data Data to set
5087a982d89SJeremy L. Thompson 
5097a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5107a982d89SJeremy L. Thompson 
5117a982d89SJeremy L. Thompson   @ref Backend
5127a982d89SJeremy L. Thompson **/
513777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
514777ff853SJeremy L Thompson   qf->data = data;
515e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5167a982d89SJeremy L. Thompson }
5177a982d89SJeremy L. Thompson 
5187a982d89SJeremy L. Thompson /**
51934359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
52034359f16Sjeremylt 
521ea61e9acSJeremy L Thompson   @param[in,out] qf CeedQFunction to increment the reference counter
52234359f16Sjeremylt 
52334359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
52434359f16Sjeremylt 
52534359f16Sjeremylt   @ref Backend
52634359f16Sjeremylt **/
5279560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
52834359f16Sjeremylt   qf->ref_count++;
52934359f16Sjeremylt   return CEED_ERROR_SUCCESS;
53034359f16Sjeremylt }
53134359f16Sjeremylt 
5326e15d496SJeremy L Thompson /**
5336e15d496SJeremy L Thompson   @brief Estimate number of FLOPs per quadrature required to apply QFunction
5346e15d496SJeremy L Thompson 
535ea61e9acSJeremy L Thompson   @param[in]  qf    QFunction to estimate FLOPs for
536ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
5376e15d496SJeremy L Thompson 
5386e15d496SJeremy L Thompson   @ref Backend
5396e15d496SJeremy L Thompson **/
5409d36ca50SJeremy L Thompson int CeedQFunctionGetFlopsEstimate(CeedQFunction qf, CeedSize *flops) {
5412b730f8bSJeremy L Thompson   if (qf->user_flop_estimate == -1) {
5426e15d496SJeremy L Thompson     // LCOV_EXCL_START
5432b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_INCOMPLETE, "Must set FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
5446e15d496SJeremy L Thompson     // LCOV_EXCL_STOP
5452b730f8bSJeremy L Thompson   }
5466e15d496SJeremy L Thompson   *flops = qf->user_flop_estimate;
5476e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5486e15d496SJeremy L Thompson }
5496e15d496SJeremy L Thompson 
5507a982d89SJeremy L. Thompson /// @}
5517a982d89SJeremy L. Thompson 
5527a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5537a982d89SJeremy L. Thompson /// CeedQFunction Public API
5547a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5557a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
5567a982d89SJeremy L. Thompson /// @{
5577a982d89SJeremy L. Thompson 
5587a982d89SJeremy L. Thompson /**
5597a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
5607a982d89SJeremy L. Thompson 
561ea61e9acSJeremy L Thompson   @param[in]  ceed       Ceed object where the CeedQFunction will be created
562ea61e9acSJeremy L Thompson   @param[in]  vec_length Vector length. Caller must ensure that number of quadrature points is a multiple of vec_length.
563ea61e9acSJeremy L Thompson   @param[in]  f          Function pointer to evaluate action at quadrature points.
5647a982d89SJeremy L. Thompson                            See \ref CeedQFunctionUser.
565ea61e9acSJeremy L Thompson   @param[in]  source     Absolute path to source of QFunction, "\abs_path\file.h:function_name".
566ea61e9acSJeremy L Thompson                            For support across all backends, this source must only contain constructs supported by C99, C++11, and CUDA.
567ea61e9acSJeremy L Thompson   @param[out] qf         Address of the variable where the newly created CeedQFunction will be stored
5687a982d89SJeremy L. Thompson 
5697a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5707a982d89SJeremy L. Thompson 
571ea61e9acSJeremy L Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's arguments.
5727a982d89SJeremy L. Thompson 
5737a982d89SJeremy L. Thompson   @ref User
5747a982d89SJeremy L. Thompson **/
5752b730f8bSJeremy L Thompson int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length, CeedQFunctionUser f, const char *source, CeedQFunction *qf) {
576ca5eadf8SJeremy L Thompson   char *user_source_copy;
5777a982d89SJeremy L. Thompson 
5787a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
5797a982d89SJeremy L. Thompson     Ceed delegate;
5802b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "QFunction"));
5817a982d89SJeremy L. Thompson 
5822b730f8bSJeremy L Thompson     if (!delegate) {
5837a982d89SJeremy L. Thompson       // LCOV_EXCL_START
5842b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support QFunctionCreate");
5857a982d89SJeremy L. Thompson       // LCOV_EXCL_STOP
5862b730f8bSJeremy L Thompson     }
5877a982d89SJeremy L. Thompson 
5882b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf));
589e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
5907a982d89SJeremy L. Thompson   }
5917a982d89SJeremy L. Thompson 
5922b730f8bSJeremy L Thompson   if (strlen(source) && !strrchr(source, ':')) {
59343e1b16fSJeremy L Thompson     // LCOV_EXCL_START
59443e1b16fSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPLETE,
5952b730f8bSJeremy L Thompson                      "Provided path to source does not include function name. Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"",
59643e1b16fSJeremy L Thompson                      source);
59743e1b16fSJeremy L Thompson     // LCOV_EXCL_STOP
5982b730f8bSJeremy L Thompson   }
59943e1b16fSJeremy L Thompson 
6002b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, qf));
6017a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
6022b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
603d1d35e2fSjeremylt   (*qf)->ref_count           = 1;
604d1d35e2fSjeremylt   (*qf)->vec_length          = vec_length;
605f04ea552SJeremy L Thompson   (*qf)->is_identity         = false;
606441428dfSJeremy L Thompson   (*qf)->is_context_writable = true;
6077a982d89SJeremy L. Thompson   (*qf)->function            = f;
6086e15d496SJeremy L Thompson   (*qf)->user_flop_estimate  = -1;
60943e1b16fSJeremy L Thompson   if (strlen(source)) {
610ca5eadf8SJeremy L Thompson     size_t user_source_len = strlen(source);
611ee5a26f2SJeremy L Thompson 
6122b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(user_source_len + 1, &user_source_copy));
613ca5eadf8SJeremy L Thompson     memcpy(user_source_copy, source, user_source_len);
614ca5eadf8SJeremy L Thompson     (*qf)->user_source = user_source_copy;
61543e1b16fSJeremy L Thompson   }
6162b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields));
6172b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields));
6182b730f8bSJeremy L Thompson   CeedCall(ceed->QFunctionCreate(*qf));
619e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6207a982d89SJeremy L. Thompson }
6217a982d89SJeremy L. Thompson 
6227a982d89SJeremy L. Thompson /**
623288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
624288c0443SJeremy L Thompson 
625ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedQFunction will be created
626ea61e9acSJeremy L Thompson   @param[in]  name Name of QFunction to use from gallery
627ea61e9acSJeremy L Thompson   @param[out] qf   Address of the variable where the newly created CeedQFunction will be stored
628288c0443SJeremy L Thompson 
629288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
630288c0443SJeremy L Thompson 
6317a982d89SJeremy L. Thompson   @ref User
632288c0443SJeremy L Thompson **/
6332b730f8bSJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, CeedQFunction *qf) {
634f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
635288c0443SJeremy L Thompson 
6362b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionRegisterAll());
637288c0443SJeremy L Thompson   // Find matching backend
6382b730f8bSJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE, "No QFunction name provided");
639288c0443SJeremy L Thompson   for (size_t i = 0; i < num_qfunctions; i++) {
640288c0443SJeremy L Thompson     size_t      n;
641d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
6422b730f8bSJeremy L Thompson     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {
6432b730f8bSJeremy L Thompson     }
644d1d35e2fSjeremylt     if (n > match_len) {
645d1d35e2fSjeremylt       match_len   = n;
646f7e22acaSJeremy L Thompson       match_index = i;
647288c0443SJeremy L Thompson     }
648288c0443SJeremy L Thompson   }
6492b730f8bSJeremy L Thompson   if (!match_len) {
650138d4072Sjeremylt     // LCOV_EXCL_START
651e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
652138d4072Sjeremylt     // LCOV_EXCL_STOP
6532b730f8bSJeremy L Thompson   }
654288c0443SJeremy L Thompson 
655288c0443SJeremy L Thompson   // Create QFunction
6562b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInterior(ceed, gallery_qfunctions[match_index].vec_length, gallery_qfunctions[match_index].f,
6572b730f8bSJeremy L Thompson                                        gallery_qfunctions[match_index].source, qf));
658288c0443SJeremy L Thompson 
659288c0443SJeremy L Thompson   // QFunction specific setup
6602b730f8bSJeremy L Thompson   CeedCall(gallery_qfunctions[match_index].init(ceed, name, *qf));
661288c0443SJeremy L Thompson 
66275affc3bSjeremylt   // Copy name
6632b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name));
66443e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
665e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
666288c0443SJeremy L Thompson }
667288c0443SJeremy L Thompson 
668288c0443SJeremy L Thompson /**
669ea61e9acSJeremy L Thompson   @brief Create an identity CeedQFunction.
670ea61e9acSJeremy L Thompson            Inputs are written into outputs in the order given.
671859c15bbSJames Wright            This is useful for CeedOperators that can be represented with only the action of a CeedElemRestriction and CeedBasis, such as restriction
672859c15bbSJames Wright and prolongation operators for p-multigrid. Backends may optimize CeedOperators with this CeedQFunction to avoid the copy of input data to output
673859c15bbSJames Wright fields by using the same memory location for both.
6740219ea01SJeremy L Thompson 
675ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed object where the CeedQFunction will be created
676d1d35e2fSjeremylt   @param[in]  size     Size of the QFunction fields
677d1d35e2fSjeremylt   @param[in]  in_mode  CeedEvalMode for input to CeedQFunction
678d1d35e2fSjeremylt   @param[in]  out_mode CeedEvalMode for output to CeedQFunction
679ea61e9acSJeremy L Thompson   @param[out] qf       Address of the variable where the newly created CeedQFunction will be stored
6800219ea01SJeremy L Thompson 
6810219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
6820219ea01SJeremy L Thompson 
6837a982d89SJeremy L. Thompson   @ref User
6840219ea01SJeremy L Thompson **/
6852b730f8bSJeremy L Thompson int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, CeedEvalMode out_mode, CeedQFunction *qf) {
6862b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Identity", qf));
6872b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(*qf, "input", size, in_mode));
6882b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(*qf, "output", size, out_mode));
6890219ea01SJeremy L Thompson 
690f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
691547dbd6fSJeremy L Thompson 
692777ff853SJeremy L Thompson   CeedQFunctionContext  ctx;
6933668ca4bSJeremy L Thompson   CeedContextFieldLabel size_label;
6942b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetContext(*qf, &ctx));
6952b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label));
6962b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetInt32(ctx, size_label, &size));
697547dbd6fSJeremy L Thompson 
698e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6990219ea01SJeremy L Thompson }
7000219ea01SJeremy L Thompson 
7010219ea01SJeremy L Thompson /**
702ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedQFunction.
703ea61e9acSJeremy L Thompson            Both pointers should be destroyed with `CeedQFunctionDestroy()`.
704512bb800SJeremy L Thompson 
705512bb800SJeremy 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.
706ea61e9acSJeremy L Thompson              This CeedQFunction will be destroyed if `*qf_copy` is the only reference to this CeedQFunction.
7079560d06aSjeremylt 
708ea61e9acSJeremy L Thompson   @param[in]  qf      CeedQFunction to copy reference to
7099560d06aSjeremylt   @param[out] qf_copy Variable to store copied reference
7109560d06aSjeremylt 
7119560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7129560d06aSjeremylt 
7139560d06aSjeremylt   @ref User
7149560d06aSjeremylt **/
7159560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
7162b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionReference(qf));
7172b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(qf_copy));
7189560d06aSjeremylt   *qf_copy = qf;
7199560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7209560d06aSjeremylt }
7219560d06aSjeremylt 
7229560d06aSjeremylt /**
723a0a97fcfSJed Brown   @brief Add a CeedQFunction input
724b11c1e72Sjeremylt 
725ea61e9acSJeremy L Thompson   @param[in,out] qf      CeedQFunction
726ea61e9acSJeremy L Thompson   @param[in]  field_name Name of QFunction field
727ea61e9acSJeremy L Thompson   @param[in]  size       Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or (num_comp * 1) for @ref CEED_EVAL_NONE and @ref
728ea61e9acSJeremy L Thompson CEED_EVAL_INTERP
729ea61e9acSJeremy L Thompson   @param[in]  eval_mode  \ref CEED_EVAL_NONE to use values directly,
730b11c1e72Sjeremylt                            \ref CEED_EVAL_INTERP to use interpolated values,
731b11c1e72Sjeremylt                            \ref CEED_EVAL_GRAD to use gradients.
732b11c1e72Sjeremylt 
733b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
734dfdf5a53Sjeremylt 
7357a982d89SJeremy L. Thompson   @ref User
736b11c1e72Sjeremylt **/
7372b730f8bSJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
7382b730f8bSJeremy L Thompson   if (qf->is_immutable) {
739e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
7402b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
741e15f9bd0SJeremy L Thompson     // LCOV_EXCL_STOP
7422b730f8bSJeremy L Thompson   }
7432b730f8bSJeremy L Thompson   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1)) {
744e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
7452b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION, "CEED_EVAL_WEIGHT should have size 1");
746e15f9bd0SJeremy L Thompson     // LCOV_EXCL_STOP
7472b730f8bSJeremy L Thompson   }
748643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
749643fbb69SJeremy L Thompson     if (!strcmp(field_name, qf->input_fields[i]->field_name)) {
750643fbb69SJeremy L Thompson       // LCOV_EXCL_START
751643fbb69SJeremy L Thompson       return CeedError(qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
752643fbb69SJeremy L Thompson       // LCOV_EXCL_STOP
753643fbb69SJeremy L Thompson     }
754643fbb69SJeremy L Thompson   }
755643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
756643fbb69SJeremy L Thompson     if (!strcmp(field_name, qf->output_fields[i]->field_name)) {
757643fbb69SJeremy L Thompson       // LCOV_EXCL_START
758643fbb69SJeremy L Thompson       return CeedError(qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
759643fbb69SJeremy L Thompson       // LCOV_EXCL_STOP
760643fbb69SJeremy L Thompson     }
761643fbb69SJeremy L Thompson   }
7622b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], field_name, size, eval_mode));
763d1d35e2fSjeremylt   qf->num_input_fields++;
764e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
765d7b241e6Sjeremylt }
766d7b241e6Sjeremylt 
767b11c1e72Sjeremylt /**
768a0a97fcfSJed Brown   @brief Add a CeedQFunction output
769b11c1e72Sjeremylt 
770ea61e9acSJeremy L Thompson   @param[in,out] qf         CeedQFunction
771ea61e9acSJeremy L Thompson   @param[in]     field_name Name of QFunction field
772ea61e9acSJeremy L Thompson   @param[in]     size       Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or (num_comp * 1) for @ref CEED_EVAL_NONE and @ref
773ea61e9acSJeremy L Thompson CEED_EVAL_INTERP
774ea61e9acSJeremy L Thompson   @param[in]     eval_mode  \ref CEED_EVAL_NONE to use values directly,
775b11c1e72Sjeremylt                               \ref CEED_EVAL_INTERP to use interpolated values,
776b11c1e72Sjeremylt                               \ref CEED_EVAL_GRAD to use gradients.
777b11c1e72Sjeremylt 
778b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
779dfdf5a53Sjeremylt 
7807a982d89SJeremy L. Thompson   @ref User
781b11c1e72Sjeremylt **/
7822b730f8bSJeremy L Thompson int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
7832b730f8bSJeremy L Thompson   if (qf->is_immutable) {
784e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
7852b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
786e15f9bd0SJeremy L Thompson     // LCOV_EXCL_STOP
7872b730f8bSJeremy L Thompson   }
7882b730f8bSJeremy L Thompson   if (eval_mode == CEED_EVAL_WEIGHT) {
789c042f62fSJeremy L Thompson     // LCOV_EXCL_START
7902b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION, "Cannot create QFunction output with CEED_EVAL_WEIGHT");
791c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
7922b730f8bSJeremy L Thompson   }
793643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
794643fbb69SJeremy L Thompson     if (!strcmp(field_name, qf->input_fields[i]->field_name)) {
795643fbb69SJeremy L Thompson       // LCOV_EXCL_START
796643fbb69SJeremy L Thompson       return CeedError(qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
797643fbb69SJeremy L Thompson       // LCOV_EXCL_STOP
798643fbb69SJeremy L Thompson     }
799643fbb69SJeremy L Thompson   }
800643fbb69SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
801643fbb69SJeremy L Thompson     if (!strcmp(field_name, qf->output_fields[i]->field_name)) {
802643fbb69SJeremy L Thompson       // LCOV_EXCL_START
803643fbb69SJeremy L Thompson       return CeedError(qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
804643fbb69SJeremy L Thompson       // LCOV_EXCL_STOP
805643fbb69SJeremy L Thompson     }
806643fbb69SJeremy L Thompson   }
8072b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], field_name, size, eval_mode));
808d1d35e2fSjeremylt   qf->num_output_fields++;
809e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
810d7b241e6Sjeremylt }
811d7b241e6Sjeremylt 
812dfdf5a53Sjeremylt /**
81343bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
81443bbe138SJeremy L Thompson 
815ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedQFunction as immutable.
816f04ea552SJeremy L Thompson 
817ea61e9acSJeremy L Thompson   @param[in]  qf                CeedQFunction
818f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
819f74ec584SJeremy L Thompson   @param[out] input_fields      Variable to store input fields
820f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
821f74ec584SJeremy L Thompson   @param[out] output_fields     Variable to store output fields
82243bbe138SJeremy L Thompson 
82343bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
82443bbe138SJeremy L Thompson 
825e9b533fbSJeremy L Thompson   @ref Advanced
82643bbe138SJeremy L Thompson **/
8272b730f8bSJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields, CeedQFunctionField **input_fields, CeedInt *num_output_fields,
82843bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
829f04ea552SJeremy L Thompson   qf->is_immutable = true;
83043bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
83143bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
83243bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
83343bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
83443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
83543bbe138SJeremy L Thompson }
83643bbe138SJeremy L Thompson 
83743bbe138SJeremy L Thompson /**
83843bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
83943bbe138SJeremy L Thompson 
840ea61e9acSJeremy L Thompson   @param[in]  qf_field   CeedQFunctionField
84143bbe138SJeremy L Thompson   @param[out] field_name Variable to store the field name
84243bbe138SJeremy L Thompson 
84343bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
84443bbe138SJeremy L Thompson 
845e9b533fbSJeremy L Thompson   @ref Advanced
84643bbe138SJeremy L Thompson **/
84743bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
84843bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
84943bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
85043bbe138SJeremy L Thompson }
85143bbe138SJeremy L Thompson 
85243bbe138SJeremy L Thompson /**
85343bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
85443bbe138SJeremy L Thompson 
855ea61e9acSJeremy L Thompson   @param[in]  qf_field CeedQFunctionField
85643bbe138SJeremy L Thompson   @param[out] size     Variable to store the size of the field
85743bbe138SJeremy L Thompson 
85843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
85943bbe138SJeremy L Thompson 
860e9b533fbSJeremy L Thompson   @ref Advanced
86143bbe138SJeremy L Thompson **/
86243bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
86343bbe138SJeremy L Thompson   *size = qf_field->size;
86443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
86543bbe138SJeremy L Thompson }
86643bbe138SJeremy L Thompson 
86743bbe138SJeremy L Thompson /**
86843bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
86943bbe138SJeremy L Thompson 
870ea61e9acSJeremy L Thompson   @param[in]  qf_field  CeedQFunctionField
87143bbe138SJeremy L Thompson   @param[out] eval_mode Variable to store the field evaluation mode
87243bbe138SJeremy L Thompson 
87343bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
87443bbe138SJeremy L Thompson 
875e9b533fbSJeremy L Thompson   @ref Advanced
87643bbe138SJeremy L Thompson **/
8772b730f8bSJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, CeedEvalMode *eval_mode) {
87843bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
87943bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
88043bbe138SJeremy L Thompson }
88143bbe138SJeremy L Thompson 
88243bbe138SJeremy L Thompson /**
8834ce2993fSjeremylt   @brief Set global context for a CeedQFunction
884b11c1e72Sjeremylt 
885ea61e9acSJeremy L Thompson   @param[in,out] qf  CeedQFunction
886ea61e9acSJeremy L Thompson   @param[in]     ctx Context data to set
887b11c1e72Sjeremylt 
888b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
889dfdf5a53Sjeremylt 
8907a982d89SJeremy L. Thompson   @ref User
891b11c1e72Sjeremylt **/
892777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
8932b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&qf->ctx));
894d7b241e6Sjeremylt   qf->ctx = ctx;
8959e77b9c8SJeremy L Thompson   if (ctx) {
8962b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextReference(ctx));
8979e77b9c8SJeremy L Thompson   }
898e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
899d7b241e6Sjeremylt }
900d7b241e6Sjeremylt 
901b11c1e72Sjeremylt /**
902441428dfSJeremy L Thompson   @brief Set writability of CeedQFunctionContext when calling the `CeedQFunctionUser`.
903859c15bbSJames Wright            The default value is `is_writable == true`.
904441428dfSJeremy L Thompson 
905ea61e9acSJeremy L Thompson            Setting `is_writable == true` indicates the `CeedQFunctionUser` writes into the CeedQFunctionContextData and requires memory syncronization
906441428dfSJeremy L Thompson after calling `CeedQFunctionApply()`.
907441428dfSJeremy L Thompson 
908ea61e9acSJeremy L Thompson            Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not modify the CeedQFunctionContextData.
909ea61e9acSJeremy L Thompson            Violating this assertion may lead to inconsistent data.
910441428dfSJeremy L Thompson 
911441428dfSJeremy L Thompson            Setting `is_writable == false` may offer a performance improvement on GPU backends.
912441428dfSJeremy L Thompson 
913ea61e9acSJeremy L Thompson   @param[in,out] qf          CeedQFunction
914ea61e9acSJeremy L Thompson   @param[in]     is_writable Writability status
915441428dfSJeremy L Thompson 
916441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
917441428dfSJeremy L Thompson 
918441428dfSJeremy L Thompson   @ref User
919441428dfSJeremy L Thompson **/
920441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
921441428dfSJeremy L Thompson   qf->is_context_writable = is_writable;
922441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
923441428dfSJeremy L Thompson }
924441428dfSJeremy L Thompson 
925441428dfSJeremy L Thompson /**
9266e15d496SJeremy L Thompson   @brief Set estimated number of FLOPs per quadrature required to apply QFunction
9276e15d496SJeremy L Thompson 
928ea61e9acSJeremy L Thompson   @param[in]  qf    QFunction to estimate FLOPs for
929ea61e9acSJeremy L Thompson   @param[out] flops FLOPs per quadrature point estimate
9306e15d496SJeremy L Thompson 
9316e15d496SJeremy L Thompson   @ref Backend
9326e15d496SJeremy L Thompson **/
9339d36ca50SJeremy L Thompson int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
9342b730f8bSJeremy L Thompson   if (flops < 0) {
9356e15d496SJeremy L Thompson     // LCOV_EXCL_START
9362b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_INCOMPATIBLE, "Must set non-negative FLOPs estimate");
9376e15d496SJeremy L Thompson     // LCOV_EXCL_STOP
9382b730f8bSJeremy L Thompson   }
9396e15d496SJeremy L Thompson   qf->user_flop_estimate = flops;
9406e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9416e15d496SJeremy L Thompson }
9426e15d496SJeremy L Thompson 
9436e15d496SJeremy L Thompson /**
94475affc3bSjeremylt   @brief View a CeedQFunction
94575affc3bSjeremylt 
94675affc3bSjeremylt   @param[in] qf     CeedQFunction to view
94775affc3bSjeremylt   @param[in] stream Stream to write; typically stdout/stderr or a file
94875affc3bSjeremylt 
94975affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
95075affc3bSjeremylt 
9517a982d89SJeremy L. Thompson   @ref User
95275affc3bSjeremylt **/
95375affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
954ca5eadf8SJeremy L Thompson   char *kernel_name;
95575affc3bSjeremylt 
9562b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetKernelName(qf, &kernel_name));
9572b730f8bSJeremy L Thompson   fprintf(stream, "%sCeedQFunction - %s\n", qf->is_gallery ? "Gallery " : "User ", qf->is_gallery ? qf->gallery_name : kernel_name);
95875affc3bSjeremylt 
9592b730f8bSJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " input field%s:\n", qf->num_input_fields, qf->num_input_fields > 1 ? "s" : "");
960d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
9612b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream));
96275affc3bSjeremylt   }
96375affc3bSjeremylt 
9642b730f8bSJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " output field%s:\n", qf->num_output_fields, qf->num_output_fields > 1 ? "s" : "");
965d1d35e2fSjeremylt   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
9662b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream));
96775affc3bSjeremylt   }
968e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
96975affc3bSjeremylt }
97075affc3bSjeremylt 
97175affc3bSjeremylt /**
972b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunction
973b7c9bbdaSJeremy L Thompson 
974ea61e9acSJeremy L Thompson   @param[in]  qf   CeedQFunction
975b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store Ceed
976b7c9bbdaSJeremy L Thompson 
977b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
978b7c9bbdaSJeremy L Thompson 
979b7c9bbdaSJeremy L Thompson   @ref Advanced
980b7c9bbdaSJeremy L Thompson **/
981b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
982b7c9bbdaSJeremy L Thompson   *ceed = qf->ceed;
983b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
984b7c9bbdaSJeremy L Thompson }
985b7c9bbdaSJeremy L Thompson 
986b7c9bbdaSJeremy L Thompson /**
987b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
988b11c1e72Sjeremylt 
989ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedQFunction as immutable.
990f04ea552SJeremy L Thompson 
991ea61e9acSJeremy L Thompson   @param[in]  qf CeedQFunction
992ea61e9acSJeremy L Thompson   @param[in]  Q  Number of quadrature points
99334138859Sjeremylt   @param[in]  u  Array of input CeedVectors
99434138859Sjeremylt   @param[out] v  Array of output CeedVectors
995b11c1e72Sjeremylt 
996b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
997dfdf5a53Sjeremylt 
9987a982d89SJeremy L. Thompson   @ref User
999b11c1e72Sjeremylt **/
10002b730f8bSJeremy L Thompson int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, CeedVector *u, CeedVector *v) {
10012b730f8bSJeremy L Thompson   if (!qf->Apply) {
1002c042f62fSJeremy L Thompson     // LCOV_EXCL_START
10032b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support QFunctionApply");
1004c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
10052b730f8bSJeremy L Thompson   }
10062b730f8bSJeremy L Thompson   if (Q % qf->vec_length) {
1007c042f62fSJeremy L Thompson     // LCOV_EXCL_START
10082b730f8bSJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION, "Number of quadrature points %" CeedInt_FMT " must be a multiple of %" CeedInt_FMT, Q,
10092b730f8bSJeremy L Thompson                      qf->vec_length);
1010c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
10112b730f8bSJeremy L Thompson   }
1012f04ea552SJeremy L Thompson   qf->is_immutable = true;
10132b730f8bSJeremy L Thompson   CeedCall(qf->Apply(qf, Q, u, v));
1014e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1015d7b241e6Sjeremylt }
1016d7b241e6Sjeremylt 
1017b11c1e72Sjeremylt /**
1018b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
1019b11c1e72Sjeremylt 
1020ea61e9acSJeremy L Thompson   @param[in,out] qf CeedQFunction to destroy
1021b11c1e72Sjeremylt 
1022b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1023dfdf5a53Sjeremylt 
10247a982d89SJeremy L. Thompson   @ref User
1025b11c1e72Sjeremylt **/
1026d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
1027ad6481ceSJeremy L Thompson   if (!*qf || --(*qf)->ref_count > 0) {
1028ad6481ceSJeremy L Thompson     *qf = NULL;
1029ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1030ad6481ceSJeremy L Thompson   }
1031fe2413ffSjeremylt   // Backend destroy
1032d7b241e6Sjeremylt   if ((*qf)->Destroy) {
10332b730f8bSJeremy L Thompson     CeedCall((*qf)->Destroy(*qf));
1034d7b241e6Sjeremylt   }
1035fe2413ffSjeremylt   // Free fields
103692ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_input_fields; i++) {
10372b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->input_fields[i]).field_name));
10382b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->input_fields[i]));
1039fe2413ffSjeremylt   }
104092ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < (*qf)->num_output_fields; i++) {
10412b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*(*qf)->output_fields[i]).field_name));
10422b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*qf)->output_fields[i]));
1043fe2413ffSjeremylt   }
10442b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->input_fields));
10452b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->output_fields));
1046777ff853SJeremy L Thompson 
1047777ff853SJeremy L Thompson   // User context data object
10482b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&(*qf)->ctx));
1049fe2413ffSjeremylt 
10502b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->user_source));
10512b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->source_path));
10522b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->gallery_name));
10532b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*qf)->kernel_name));
10542b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*qf)->ceed));
10552b730f8bSJeremy L Thompson   CeedCall(CeedFree(qf));
1056e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1057d7b241e6Sjeremylt }
1058d7b241e6Sjeremylt 
1059d7b241e6Sjeremylt /// @}
1060