xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision ca5eadf8df4f5a5d6322e2e571e4886ce218945b)
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 
8ec3da8bcSJed Brown #include <ceed/ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
103d3250a0SJeremy L Thompson #include <ceed/jit-tools.h>
113d576824SJeremy L Thompson #include <ceed-impl.h>
12288c0443SJeremy L Thompson #include <limits.h>
133d576824SJeremy L Thompson #include <stdbool.h>
143d576824SJeremy L Thompson #include <stdio.h>
1543e1b16fSJeremy L Thompson #include <stdlib.h>
163d576824SJeremy L Thompson #include <string.h>
17288c0443SJeremy L Thompson 
187a982d89SJeremy L. Thompson /// @file
197a982d89SJeremy L. Thompson /// Implementation of public CeedQFunction interfaces
207a982d89SJeremy L. Thompson 
21288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP
22442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none;
23442e7f0bSjeremylt /// @endcond
24442e7f0bSjeremylt 
257a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
267a982d89SJeremy L. Thompson /// @{
277a982d89SJeremy L. Thompson 
287a982d89SJeremy L. Thompson // Indicate that no QFunction is provided by the user
297a982d89SJeremy L. Thompson const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
307a982d89SJeremy L. Thompson 
317a982d89SJeremy L. Thompson /// @}
327a982d89SJeremy L. Thompson 
33442e7f0bSjeremylt /// @cond DOXYGEN_SKIP
34288c0443SJeremy L Thompson static struct {
35288c0443SJeremy L Thompson   char name[CEED_MAX_RESOURCE_LEN];
36288c0443SJeremy L Thompson   char source[CEED_MAX_RESOURCE_LEN];
37d1d35e2fSjeremylt   CeedInt vec_length;
38288c0443SJeremy L Thompson   CeedQFunctionUser f;
39288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
40d1d35e2fSjeremylt } gallery_qfunctions[1024];
41288c0443SJeremy L Thompson static size_t num_qfunctions;
42288c0443SJeremy L Thompson /// @endcond
43d7b241e6Sjeremylt 
44777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
45777ff853SJeremy L Thompson /// CeedQFunction Library Internal Functions
46777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
47777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper
48777ff853SJeremy L Thompson /// @{
49777ff853SJeremy L Thompson 
50b11c1e72Sjeremylt /**
51288c0443SJeremy L Thompson   @brief Register a gallery QFunction
52288c0443SJeremy L Thompson 
53288c0443SJeremy L Thompson   @param name        Name for this backend to respond to
541176cc3aSJeremy L Thompson   @param source      Absolute path to source of QFunction,
551176cc3aSJeremy L Thompson                        "\path\CEED_DIR\gallery\folder\file.h:function_name"
56d1d35e2fSjeremylt   @param vec_length  Vector length.  Caller must ensure that number of quadrature
57d1d35e2fSjeremylt                        points is a multiple of vec_length.
58288c0443SJeremy L Thompson   @param f           Function pointer to evaluate action at quadrature points.
59288c0443SJeremy L Thompson                        See \ref CeedQFunctionUser.
60288c0443SJeremy L Thompson   @param init        Initialization function called by CeedQFunctionInit() when the
61288c0443SJeremy L Thompson                        QFunction is selected.
62288c0443SJeremy L Thompson 
63288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
64288c0443SJeremy L Thompson 
657a982d89SJeremy L. Thompson   @ref Developer
66288c0443SJeremy L Thompson **/
67288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source,
68d1d35e2fSjeremylt                           CeedInt vec_length, CeedQFunctionUser f,
69288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
706eb0d8b4SJeremy L Thompson   int ierr;
716eb0d8b4SJeremy L Thompson 
72d1d35e2fSjeremylt   if (num_qfunctions >= sizeof(gallery_qfunctions) / sizeof(
73d1d35e2fSjeremylt         gallery_qfunctions[0]))
74c042f62fSJeremy L Thompson     // LCOV_EXCL_START
75e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
76c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
77c042f62fSJeremy L Thompson 
788ccf1006SJeremy L Thompson   CeedDebugEnv("Gallery Register: %s", name);
798ccf1006SJeremy L Thompson 
806eb0d8b4SJeremy L Thompson   const char *relative_file_path;
816eb0d8b4SJeremy L Thompson   ierr = CeedGetJitRelativePath(source, &relative_file_path); CeedChk(ierr);
826eb0d8b4SJeremy L Thompson 
83d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
84d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
856eb0d8b4SJeremy L Thompson   strncpy(gallery_qfunctions[num_qfunctions].source, relative_file_path,
86d1d35e2fSjeremylt           CEED_MAX_RESOURCE_LEN);
87d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
88d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].vec_length = vec_length;
89d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].f = f;
90d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].init = init;
91288c0443SJeremy L Thompson   num_qfunctions++;
92e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
93288c0443SJeremy L Thompson }
94288c0443SJeremy L Thompson 
95288c0443SJeremy L Thompson /**
967a982d89SJeremy L. Thompson   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
977a982d89SJeremy L. Thompson 
987a982d89SJeremy L. Thompson   @param f           CeedQFunctionField
99d1d35e2fSjeremylt   @param field_name  Name of QFunction field
100d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
101d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT
102d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
1037a982d89SJeremy L. Thompson                        \ref CEED_EVAL_INTERP to use interpolated values,
1047a982d89SJeremy L. Thompson                        \ref CEED_EVAL_GRAD to use gradients,
1057a982d89SJeremy L. Thompson                        \ref CEED_EVAL_WEIGHT to use quadrature weights.
1067a982d89SJeremy L. Thompson 
1077a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1087a982d89SJeremy L. Thompson 
1097a982d89SJeremy L. Thompson   @ref Developer
1107a982d89SJeremy L. Thompson **/
111d1d35e2fSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name,
112d1d35e2fSjeremylt                                  CeedInt size, CeedEvalMode eval_mode) {
113cdf32b93SJeremy L Thompson   int ierr;
1147a982d89SJeremy L. Thompson 
115e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, f); CeedChk(ierr);
116f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(field_name, (char **)&(*f)->field_name);
117f7e22acaSJeremy L Thompson   CeedChk(ierr);
1187a982d89SJeremy L. Thompson   (*f)->size = size;
119d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
120e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1217a982d89SJeremy L. Thompson }
1227a982d89SJeremy L. Thompson 
1237a982d89SJeremy L. Thompson /**
1247a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1257a982d89SJeremy L. Thompson 
1267a982d89SJeremy L. Thompson   @param[in] field         QFunction field to view
127d1d35e2fSjeremylt   @param[in] field_number  Number of field being viewed
1284c4400c7SValeria Barra   @param[in] in            true for input field, false for output
1297a982d89SJeremy L. Thompson   @param[in] stream        Stream to view to, e.g., stdout
1307a982d89SJeremy L. Thompson 
1317a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1327a982d89SJeremy L. Thompson 
1337a982d89SJeremy L. Thompson   @ref Utility
1347a982d89SJeremy L. Thompson **/
135d1d35e2fSjeremylt static int CeedQFunctionFieldView(CeedQFunctionField field,
136d1d35e2fSjeremylt                                   CeedInt field_number,
1377a982d89SJeremy L. Thompson                                   bool in, FILE *stream) {
1388229195eSjeremylt   int ierr;
1397a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1408229195eSjeremylt   char *field_name;
1418229195eSjeremylt   ierr = CeedQFunctionFieldGetName(field, &field_name); CeedChk(ierr);
1428229195eSjeremylt   CeedInt size;
1438229195eSjeremylt   ierr = CeedQFunctionFieldGetSize(field, &size); CeedChk(ierr);
1448229195eSjeremylt   CeedEvalMode eval_mode;
1458229195eSjeremylt   ierr = CeedQFunctionFieldGetEvalMode(field, &eval_mode); CeedChk(ierr);
146990fdeb6SJeremy L Thompson   fprintf(stream, "    %s field %" CeedInt_FMT ":\n"
1477a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
148990fdeb6SJeremy L Thompson           "      Size: %" CeedInt_FMT "\n"
1497a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1508229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
151e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1527a982d89SJeremy L. Thompson }
1537a982d89SJeremy L. Thompson 
154777ff853SJeremy L Thompson /**
155777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
156777ff853SJeremy L Thompson 
157777ff853SJeremy L Thompson   @param qf      CeedQFunction
158777ff853SJeremy L Thompson   @param status  Boolean value to set as Fortran status
159777ff853SJeremy L Thompson 
160777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
161777ff853SJeremy L Thompson 
162777ff853SJeremy L Thompson   @ref Backend
163777ff853SJeremy L Thompson **/
164777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
165f04ea552SJeremy L Thompson   qf->is_fortran = status;
166e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
167777ff853SJeremy L Thompson }
168777ff853SJeremy L Thompson 
1697a982d89SJeremy L. Thompson /// @}
1707a982d89SJeremy L. Thompson 
1717a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1727a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1737a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1747a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1757a982d89SJeremy L. Thompson /// @{
1767a982d89SJeremy L. Thompson 
1777a982d89SJeremy L. Thompson /**
1787a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1797a982d89SJeremy L. Thompson 
1807a982d89SJeremy L. Thompson   @param qf               CeedQFunction
181d1d35e2fSjeremylt   @param[out] vec_length  Variable to store vector length
1827a982d89SJeremy L. Thompson 
1837a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1847a982d89SJeremy L. Thompson 
1857a982d89SJeremy L. Thompson   @ref Backend
1867a982d89SJeremy L. Thompson **/
187d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
188d1d35e2fSjeremylt   *vec_length = qf->vec_length;
189e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1907a982d89SJeremy L. Thompson }
1917a982d89SJeremy L. Thompson 
1927a982d89SJeremy L. Thompson /**
1937a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
1947a982d89SJeremy L. Thompson 
1957a982d89SJeremy L. Thompson   @param qf               CeedQFunction
196d1d35e2fSjeremylt   @param[out] num_input   Variable to store number of input fields
197d1d35e2fSjeremylt   @param[out] num_output  Variable to store number of output fields
1987a982d89SJeremy L. Thompson 
1997a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2007a982d89SJeremy L. Thompson 
2017a982d89SJeremy L. Thompson   @ref Backend
2027a982d89SJeremy L. Thompson **/
203d1d35e2fSjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input,
204d1d35e2fSjeremylt                             CeedInt *num_output) {
205d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
206d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
207e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2087a982d89SJeremy L. Thompson }
2097a982d89SJeremy L. Thompson 
2107a982d89SJeremy L. Thompson /**
21143e1b16fSJeremy L Thompson   @brief Get the name of the user function for a CeedQFunction
2127a982d89SJeremy L. Thompson 
2137a982d89SJeremy L. Thompson   @param qf                CeedQFunction
21443e1b16fSJeremy L Thompson   @param[out] kernel_name  Variable to store source path string
2157a982d89SJeremy L. Thompson 
2167a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2177a982d89SJeremy L. Thompson 
2187a982d89SJeremy L. Thompson   @ref Backend
2197a982d89SJeremy L. Thompson **/
22043e1b16fSJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, char **kernel_name) {
221*ca5eadf8SJeremy L Thompson   int ierr;
222*ca5eadf8SJeremy L Thompson 
223*ca5eadf8SJeremy L Thompson   if (!qf->kernel_name ) {
224*ca5eadf8SJeremy L Thompson     Ceed ceed;
225*ca5eadf8SJeremy L Thompson     char *kernel_name_copy;
226*ca5eadf8SJeremy L Thompson     ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChk(ierr);
227*ca5eadf8SJeremy L Thompson 
228*ca5eadf8SJeremy L Thompson     if (qf->user_source) {
229*ca5eadf8SJeremy L Thompson       const char *kernel_name = strrchr(qf->user_source, ':') + 1;
230*ca5eadf8SJeremy L Thompson       size_t kernel_name_len = strlen(kernel_name);
231*ca5eadf8SJeremy L Thompson 
232*ca5eadf8SJeremy L Thompson       ierr = CeedCalloc(kernel_name_len + 1, &kernel_name_copy); CeedChk(ierr);
233*ca5eadf8SJeremy L Thompson       memcpy(kernel_name_copy, kernel_name, kernel_name_len);
234*ca5eadf8SJeremy L Thompson     } else {
235*ca5eadf8SJeremy L Thompson       ierr = CeedCalloc(1, &kernel_name_copy); CeedChk(ierr);
236*ca5eadf8SJeremy L Thompson     }
237*ca5eadf8SJeremy L Thompson     qf->kernel_name = kernel_name_copy;
238*ca5eadf8SJeremy L Thompson   }
239*ca5eadf8SJeremy L Thompson 
24043e1b16fSJeremy L Thompson   *kernel_name = (char *) qf->kernel_name;
24143e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
24243e1b16fSJeremy L Thompson }
24343e1b16fSJeremy L Thompson 
24443e1b16fSJeremy L Thompson /**
24543e1b16fSJeremy L Thompson   @brief Get the source path string for a CeedQFunction
24643e1b16fSJeremy L Thompson 
24743e1b16fSJeremy L Thompson   @param qf                CeedQFunction
24843e1b16fSJeremy L Thompson   @param[out] source_path  Variable to store source path string
24943e1b16fSJeremy L Thompson 
25043e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
25143e1b16fSJeremy L Thompson 
25243e1b16fSJeremy L Thompson   @ref Backend
25343e1b16fSJeremy L Thompson **/
25443e1b16fSJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) {
255*ca5eadf8SJeremy L Thompson   int ierr;
256*ca5eadf8SJeremy L Thompson 
257*ca5eadf8SJeremy L Thompson   if (!qf->source_path && qf->user_source) {
258*ca5eadf8SJeremy L Thompson     Ceed ceed;
259*ca5eadf8SJeremy L Thompson     bool is_absolute_path;
260*ca5eadf8SJeremy L Thompson     char *absolute_path, *source_path_copy;
261*ca5eadf8SJeremy L Thompson     const char *kernel_name = strrchr(qf->user_source, ':') + 1;
262*ca5eadf8SJeremy L Thompson     size_t kernel_name_len = strlen(kernel_name);
263*ca5eadf8SJeremy L Thompson 
264*ca5eadf8SJeremy L Thompson     ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChk(ierr);
265*ca5eadf8SJeremy L Thompson 
266*ca5eadf8SJeremy L Thompson     ierr = CeedCheckFilePath(ceed, qf->user_source, &is_absolute_path);
267*ca5eadf8SJeremy L Thompson     CeedChk(ierr);
268*ca5eadf8SJeremy L Thompson     if (is_absolute_path) {
269*ca5eadf8SJeremy L Thompson       absolute_path = (char *)qf->user_source;
270*ca5eadf8SJeremy L Thompson     } else {
271*ca5eadf8SJeremy L Thompson       ierr = CeedGetJitAbsolutePath(ceed, qf->user_source, &absolute_path);
272*ca5eadf8SJeremy L Thompson       CeedChk(ierr);
273*ca5eadf8SJeremy L Thompson     }
274*ca5eadf8SJeremy L Thompson 
275*ca5eadf8SJeremy L Thompson     size_t source_len = strlen(absolute_path) - kernel_name_len - 1;
276*ca5eadf8SJeremy L Thompson     ierr = CeedCalloc(source_len + 1, &source_path_copy); CeedChk(ierr);
277*ca5eadf8SJeremy L Thompson     memcpy(source_path_copy, absolute_path, source_len);
278*ca5eadf8SJeremy L Thompson     qf->source_path = source_path_copy;
279*ca5eadf8SJeremy L Thompson 
280*ca5eadf8SJeremy L Thompson     if (!is_absolute_path) {
281*ca5eadf8SJeremy L Thompson       ierr = CeedFree(&absolute_path); CeedChk(ierr);
282*ca5eadf8SJeremy L Thompson     }
283*ca5eadf8SJeremy L Thompson   }
284*ca5eadf8SJeremy L Thompson 
28543e1b16fSJeremy L Thompson   *source_path = (char *) qf->source_path;
286e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2877a982d89SJeremy L. Thompson }
2887a982d89SJeremy L. Thompson 
2897a982d89SJeremy L. Thompson /**
2903d3250a0SJeremy L Thompson   @brief Initalize and load QFunction source file into string buffer, including
2913d3250a0SJeremy L Thompson            full text of local files in place of `#include "local.h"`.
2923d3250a0SJeremy L Thompson            The `buffer` is set to `NULL` if there is no QFunction source file.
2933d3250a0SJeremy L Thompson          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
2943d3250a0SJeremy L Thompson 
2953d3250a0SJeremy L Thompson   @param qf                  CeedQFunction
296f74ec584SJeremy L Thompson   @param[out] source_buffer  String buffer for source file contents
2973d3250a0SJeremy L Thompson 
2983d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2993d3250a0SJeremy L Thompson 
3003d3250a0SJeremy L Thompson   @ref Backend
3013d3250a0SJeremy L Thompson **/
3023d3250a0SJeremy L Thompson int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, char **source_buffer) {
3033d3250a0SJeremy L Thompson   int ierr;
3043d3250a0SJeremy L Thompson   char *source_path;
3053d3250a0SJeremy L Thompson 
3063d3250a0SJeremy L Thompson   ierr = CeedQFunctionGetSourcePath(qf, &source_path); CeedChk(ierr);
3073d3250a0SJeremy L Thompson   *source_buffer = NULL;
3083d3250a0SJeremy L Thompson   if (source_path) {
3093d3250a0SJeremy L Thompson     ierr = CeedLoadSourceToBuffer(qf->ceed, source_path, source_buffer);
3103d3250a0SJeremy L Thompson     CeedChk(ierr);
3113d3250a0SJeremy L Thompson   }
3123d3250a0SJeremy L Thompson 
3133d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3143d3250a0SJeremy L Thompson }
3153d3250a0SJeremy L Thompson 
3163d3250a0SJeremy L Thompson /**
3177a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
3187a982d89SJeremy L. Thompson 
3197a982d89SJeremy L. Thompson   @param qf      CeedQFunction
3207a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
3217a982d89SJeremy L. Thompson 
3227a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3237a982d89SJeremy L. Thompson 
3247a982d89SJeremy L. Thompson   @ref Backend
3257a982d89SJeremy L. Thompson **/
3267a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
3277a982d89SJeremy L. Thompson   *f = qf->function;
328e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3297a982d89SJeremy L. Thompson }
3307a982d89SJeremy L. Thompson 
3317a982d89SJeremy L. Thompson /**
332777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
333777ff853SJeremy L Thompson            Note: For QFunctions from the Fortran interface, this
334777ff853SJeremy L Thompson              function will return the Fortran context
335777ff853SJeremy L Thompson              CeedQFunctionContext.
3367a982d89SJeremy L. Thompson 
3377a982d89SJeremy L. Thompson   @param qf        CeedQFunction
338777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
3397a982d89SJeremy L. Thompson 
3407a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3417a982d89SJeremy L. Thompson 
3427a982d89SJeremy L. Thompson   @ref Backend
3437a982d89SJeremy L. Thompson **/
344777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
3457a982d89SJeremy L. Thompson   *ctx = qf->ctx;
346e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3477a982d89SJeremy L. Thompson }
3487a982d89SJeremy L. Thompson 
3497a982d89SJeremy L. Thompson /**
350441428dfSJeremy L Thompson   @brief Get context data of a CeedQFunction
351441428dfSJeremy L Thompson 
352441428dfSJeremy L Thompson   @param qf         CeedQFunction
353441428dfSJeremy L Thompson   @param mem_type   Memory type on which to access the data. If the backend
354441428dfSJeremy L Thompson                       uses a different memory type, this will perform a copy.
355441428dfSJeremy L Thompson   @param[out] data  Data on memory type mem_type
356441428dfSJeremy L Thompson 
357441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
358441428dfSJeremy L Thompson 
359441428dfSJeremy L Thompson   @ref Backend
360441428dfSJeremy L Thompson **/
361441428dfSJeremy L Thompson int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type,
362441428dfSJeremy L Thompson                                 void *data) {
363441428dfSJeremy L Thompson   int ierr;
364441428dfSJeremy L Thompson   bool is_writable;
365441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
366441428dfSJeremy L Thompson 
367441428dfSJeremy L Thompson   ierr = CeedQFunctionGetContext(qf, &ctx); CeedChk(ierr);
368441428dfSJeremy L Thompson   if (ctx) {
369441428dfSJeremy L Thompson     ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr);
370441428dfSJeremy L Thompson     if (is_writable) {
371441428dfSJeremy L Thompson       ierr = CeedQFunctionContextGetData(ctx, mem_type, data); CeedChk(ierr);
372441428dfSJeremy L Thompson     } else {
373441428dfSJeremy L Thompson       ierr = CeedQFunctionContextGetDataRead(ctx, mem_type, data); CeedChk(ierr);
374441428dfSJeremy L Thompson     }
375441428dfSJeremy L Thompson   } else {
376441428dfSJeremy L Thompson     *(void **)data = NULL;
377441428dfSJeremy L Thompson   }
378441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
379441428dfSJeremy L Thompson }
380441428dfSJeremy L Thompson 
381441428dfSJeremy L Thompson /**
382441428dfSJeremy L Thompson   @brief Restore context data of a CeedQFunction
383441428dfSJeremy L Thompson 
384441428dfSJeremy L Thompson   @param qf    CeedQFunction
385441428dfSJeremy L Thompson   @param data  Data to restore
386441428dfSJeremy L Thompson 
387441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
388441428dfSJeremy L Thompson 
389441428dfSJeremy L Thompson   @ref Backend
390441428dfSJeremy L Thompson **/
391441428dfSJeremy L Thompson int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) {
392441428dfSJeremy L Thompson   int ierr;
393441428dfSJeremy L Thompson   bool is_writable;
394441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
395441428dfSJeremy L Thompson 
396441428dfSJeremy L Thompson   ierr = CeedQFunctionGetContext(qf, &ctx); CeedChk(ierr);
397441428dfSJeremy L Thompson   if (ctx) {
398441428dfSJeremy L Thompson     ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr);
399441428dfSJeremy L Thompson     if (is_writable) {
400441428dfSJeremy L Thompson       ierr = CeedQFunctionContextRestoreData(ctx, data); CeedChk(ierr);
401441428dfSJeremy L Thompson     } else {
402441428dfSJeremy L Thompson       ierr = CeedQFunctionContextRestoreDataRead(ctx, data); CeedChk(ierr);
403441428dfSJeremy L Thompson     }
404441428dfSJeremy L Thompson   }
405441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
406441428dfSJeremy L Thompson }
407441428dfSJeremy L Thompson 
408441428dfSJeremy L Thompson /**
4097a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
410777ff853SJeremy L Thompson            Note: For all QFunctions this function will return the user
411777ff853SJeremy L Thompson              CeedQFunctionContext and not interface context
412777ff853SJeremy L Thompson              CeedQFunctionContext, if any such object exists.
4137a982d89SJeremy L. Thompson 
4147a982d89SJeremy L. Thompson   @param qf        CeedQFunction
415777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
4167a982d89SJeremy L. Thompson 
4177a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4187a982d89SJeremy L. Thompson   @ref Backend
4197a982d89SJeremy L. Thompson **/
420777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
421777ff853SJeremy L Thompson   int ierr;
422f04ea552SJeremy L Thompson   if (qf->is_fortran) {
423d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
424d1d35e2fSjeremylt     ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx);
425777ff853SJeremy L Thompson     CeedChk(ierr);
4268cb0412aSJeremy L Thompson     *ctx = fortran_ctx->inner_ctx;
427d1d35e2fSjeremylt     ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx);
428d1d35e2fSjeremylt     CeedChk(ierr);
4297a982d89SJeremy L. Thompson   } else {
4307a982d89SJeremy L. Thompson     *ctx = qf->ctx;
4317a982d89SJeremy L. Thompson   }
432e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4337a982d89SJeremy L. Thompson }
4347a982d89SJeremy L. Thompson 
4357a982d89SJeremy L. Thompson /**
436441428dfSJeremy L Thompson   @brief Get inner context data of a CeedQFunction
437441428dfSJeremy L Thompson 
438441428dfSJeremy L Thompson   @param qf         CeedQFunction
439441428dfSJeremy L Thompson   @param mem_type   Memory type on which to access the data. If the backend
440441428dfSJeremy L Thompson                       uses a different memory type, this will perform a copy.
441441428dfSJeremy L Thompson   @param[out] data  Data on memory type mem_type
442441428dfSJeremy L Thompson 
443441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
444441428dfSJeremy L Thompson 
445441428dfSJeremy L Thompson   @ref Backend
446441428dfSJeremy L Thompson **/
447441428dfSJeremy L Thompson int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type,
448441428dfSJeremy L Thompson                                      void *data) {
449441428dfSJeremy L Thompson   int ierr;
450441428dfSJeremy L Thompson   bool is_writable;
451441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
452441428dfSJeremy L Thompson 
453441428dfSJeremy L Thompson   ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChk(ierr);
454441428dfSJeremy L Thompson   if (ctx) {
455441428dfSJeremy L Thompson     ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr);
456441428dfSJeremy L Thompson     if (is_writable) {
457441428dfSJeremy L Thompson       ierr = CeedQFunctionContextGetData(ctx, mem_type, data); CeedChk(ierr);
458441428dfSJeremy L Thompson     } else {
459441428dfSJeremy L Thompson       ierr = CeedQFunctionContextGetDataRead(ctx, mem_type, data); CeedChk(ierr);
460441428dfSJeremy L Thompson     }
461441428dfSJeremy L Thompson   } else {
462441428dfSJeremy L Thompson     *(void **)data = NULL;
463441428dfSJeremy L Thompson   }
464441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
465441428dfSJeremy L Thompson }
466441428dfSJeremy L Thompson 
467441428dfSJeremy L Thompson /**
468441428dfSJeremy L Thompson   @brief Restore inner context data of a CeedQFunction
469441428dfSJeremy L Thompson 
470441428dfSJeremy L Thompson   @param qf    CeedQFunction
471441428dfSJeremy L Thompson   @param data  Data to restore
472441428dfSJeremy L Thompson 
473441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
474441428dfSJeremy L Thompson 
475441428dfSJeremy L Thompson   @ref Backend
476441428dfSJeremy L Thompson **/
477441428dfSJeremy L Thompson int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) {
478441428dfSJeremy L Thompson   int ierr;
479441428dfSJeremy L Thompson   bool is_writable;
480441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
481441428dfSJeremy L Thompson 
482441428dfSJeremy L Thompson   ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChk(ierr);
483441428dfSJeremy L Thompson   if (ctx) {
484441428dfSJeremy L Thompson     ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr);
485441428dfSJeremy L Thompson     if (is_writable) {
486441428dfSJeremy L Thompson       ierr = CeedQFunctionContextRestoreData(ctx, data); CeedChk(ierr);
487441428dfSJeremy L Thompson     } else {
488441428dfSJeremy L Thompson       ierr = CeedQFunctionContextRestoreDataRead(ctx, data); CeedChk(ierr);
489441428dfSJeremy L Thompson     }
490441428dfSJeremy L Thompson   }
491441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
492441428dfSJeremy L Thompson }
493441428dfSJeremy L Thompson 
494441428dfSJeremy L Thompson /**
4957a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
4967a982d89SJeremy L. Thompson 
4977a982d89SJeremy L. Thompson   @param qf                CeedQFunction
498d1d35e2fSjeremylt   @param[out] is_identity  Variable to store identity status
4997a982d89SJeremy L. Thompson 
5007a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5017a982d89SJeremy L. Thompson 
5027a982d89SJeremy L. Thompson   @ref Backend
5037a982d89SJeremy L. Thompson **/
504d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
505f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
506e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5077a982d89SJeremy L. Thompson }
5087a982d89SJeremy L. Thompson 
5097a982d89SJeremy L. Thompson /**
510441428dfSJeremy L Thompson   @brief Determine if QFunctionContext is writable
511441428dfSJeremy L Thompson 
512441428dfSJeremy L Thompson   @param qf                CeedQFunction
513441428dfSJeremy L Thompson   @param[out] is_writable  Variable to store context writeable staus
514441428dfSJeremy L Thompson 
515441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
516441428dfSJeremy L Thompson 
517441428dfSJeremy L Thompson   @ref Backend
518441428dfSJeremy L Thompson **/
519441428dfSJeremy L Thompson int CeedQFunctionIsContextWritable(CeedQFunction qf, bool *is_writable) {
520441428dfSJeremy L Thompson   *is_writable = qf->is_context_writable;
521441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
522441428dfSJeremy L Thompson }
523441428dfSJeremy L Thompson 
524441428dfSJeremy L Thompson /**
5257a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
5267a982d89SJeremy L. Thompson 
5277a982d89SJeremy L. Thompson   @param qf         CeedQFunction
5287a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
5297a982d89SJeremy L. Thompson 
5307a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5317a982d89SJeremy L. Thompson 
5327a982d89SJeremy L. Thompson   @ref Backend
5337a982d89SJeremy L. Thompson **/
534777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
535777ff853SJeremy L Thompson   *(void **)data = qf->data;
536e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5377a982d89SJeremy L. Thompson }
5387a982d89SJeremy L. Thompson 
5397a982d89SJeremy L. Thompson /**
5407a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
5417a982d89SJeremy L. Thompson 
5427a982d89SJeremy L. Thompson   @param[out] qf  CeedQFunction
5437a982d89SJeremy L. Thompson   @param data     Data to set
5447a982d89SJeremy L. Thompson 
5457a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5467a982d89SJeremy L. Thompson 
5477a982d89SJeremy L. Thompson   @ref Backend
5487a982d89SJeremy L. Thompson **/
549777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
550777ff853SJeremy L Thompson   qf->data = data;
551e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5527a982d89SJeremy L. Thompson }
5537a982d89SJeremy L. Thompson 
5547a982d89SJeremy L. Thompson /**
55534359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
55634359f16Sjeremylt 
55734359f16Sjeremylt   @param qf  CeedQFunction to increment the reference counter
55834359f16Sjeremylt 
55934359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
56034359f16Sjeremylt 
56134359f16Sjeremylt   @ref Backend
56234359f16Sjeremylt **/
5639560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
56434359f16Sjeremylt   qf->ref_count++;
56534359f16Sjeremylt   return CEED_ERROR_SUCCESS;
56634359f16Sjeremylt }
56734359f16Sjeremylt 
5686e15d496SJeremy L Thompson /**
5696e15d496SJeremy L Thompson   @brief Estimate number of FLOPs per quadrature required to apply QFunction
5706e15d496SJeremy L Thompson 
5716e15d496SJeremy L Thompson   @param qf    QFunction to estimate FLOPs for
5726e15d496SJeremy L Thompson   @param flops Address of variable to hold FLOPs estimate
5736e15d496SJeremy L Thompson 
5746e15d496SJeremy L Thompson   @ref Backend
5756e15d496SJeremy L Thompson **/
5769d36ca50SJeremy L Thompson int CeedQFunctionGetFlopsEstimate(CeedQFunction qf, CeedSize *flops) {
5776e15d496SJeremy L Thompson   if (qf->user_flop_estimate == -1)
5786e15d496SJeremy L Thompson     // LCOV_EXCL_START
5796e15d496SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_INCOMPLETE,
5806e15d496SJeremy L Thompson                      "Must set FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
5816e15d496SJeremy L Thompson   // LCOV_EXCL_STOP
5826e15d496SJeremy L Thompson   *flops = qf->user_flop_estimate;
5836e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5846e15d496SJeremy L Thompson }
5856e15d496SJeremy L Thompson 
5867a982d89SJeremy L. Thompson /// @}
5877a982d89SJeremy L. Thompson 
5887a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5897a982d89SJeremy L. Thompson /// CeedQFunction Public API
5907a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5917a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
5927a982d89SJeremy L. Thompson /// @{
5937a982d89SJeremy L. Thompson 
5947a982d89SJeremy L. Thompson /**
5957a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
5967a982d89SJeremy L. Thompson 
5977a982d89SJeremy L. Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
598d1d35e2fSjeremylt   @param vec_length  Vector length. Caller must ensure that number of quadrature
599d1d35e2fSjeremylt                        points is a multiple of vec_length.
6007a982d89SJeremy L. Thompson   @param f           Function pointer to evaluate action at quadrature points.
6017a982d89SJeremy L. Thompson                        See \ref CeedQFunctionUser.
6027a982d89SJeremy L. Thompson   @param source      Absolute path to source of QFunction,
603c8d5b03cSJeremy L Thompson                        "\abs_path\file.h:function_name".
604c8d5b03cSJeremy L Thompson                        For support across all backends, this source must only
605c8d5b03cSJeremy L Thompson                        contain constructs supported by C99, C++11, and CUDA.
6067a982d89SJeremy L. Thompson   @param[out] qf     Address of the variable where the newly created
6077a982d89SJeremy L. Thompson                        CeedQFunction will be stored
6087a982d89SJeremy L. Thompson 
6097a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6107a982d89SJeremy L. Thompson 
6117a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
6127a982d89SJeremy L. Thompson     arguments.
6137a982d89SJeremy L. Thompson 
6147a982d89SJeremy L. Thompson   @ref User
6157a982d89SJeremy L. Thompson **/
616d1d35e2fSjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length,
617d1d35e2fSjeremylt                                 CeedQFunctionUser f,
6187a982d89SJeremy L. Thompson                                 const char *source, CeedQFunction *qf) {
6197a982d89SJeremy L. Thompson   int ierr;
620*ca5eadf8SJeremy L Thompson   char *user_source_copy;
6217a982d89SJeremy L. Thompson 
6227a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
6237a982d89SJeremy L. Thompson     Ceed delegate;
6247a982d89SJeremy L. Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
6257a982d89SJeremy L. Thompson 
6267a982d89SJeremy L. Thompson     if (!delegate)
6277a982d89SJeremy L. Thompson       // LCOV_EXCL_START
628e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
629e15f9bd0SJeremy L Thompson                        "Backend does not support QFunctionCreate");
6307a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
6317a982d89SJeremy L. Thompson 
632d1d35e2fSjeremylt     ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf);
6337a982d89SJeremy L. Thompson     CeedChk(ierr);
634e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
6357a982d89SJeremy L. Thompson   }
6367a982d89SJeremy L. Thompson 
637edfb5f23SJeremy L Thompson   if (strlen(source) && !strrchr(source, ':'))
63843e1b16fSJeremy L Thompson     // LCOV_EXCL_START
63943e1b16fSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPLETE,
64043e1b16fSJeremy L Thompson                      "Provided path to source does not include function name. "
64143e1b16fSJeremy L Thompson                      "Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"",
64243e1b16fSJeremy L Thompson                      source);
64343e1b16fSJeremy L Thompson   // LCOV_EXCL_STOP
64443e1b16fSJeremy L Thompson 
6457a982d89SJeremy L. Thompson   ierr = CeedCalloc(1, qf); CeedChk(ierr);
6467a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
6479560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
648d1d35e2fSjeremylt   (*qf)->ref_count = 1;
649d1d35e2fSjeremylt   (*qf)->vec_length = vec_length;
650f04ea552SJeremy L Thompson   (*qf)->is_identity = false;
651441428dfSJeremy L Thompson   (*qf)->is_context_writable = true;
6527a982d89SJeremy L. Thompson   (*qf)->function = f;
6536e15d496SJeremy L Thompson   (*qf)->user_flop_estimate = -1;
65443e1b16fSJeremy L Thompson   if (strlen(source)) {
655*ca5eadf8SJeremy L Thompson     size_t user_source_len = strlen(source);
656ee5a26f2SJeremy L Thompson 
657*ca5eadf8SJeremy L Thompson     ierr = CeedCalloc(user_source_len + 1, &user_source_copy); CeedChk(ierr);
658*ca5eadf8SJeremy L Thompson     memcpy(user_source_copy, source, user_source_len);
659*ca5eadf8SJeremy L Thompson     (*qf)->user_source = user_source_copy;
66043e1b16fSJeremy L Thompson   }
661bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields); CeedChk(ierr);
662bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields); CeedChk(ierr);
6637a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
664e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6657a982d89SJeremy L. Thompson }
6667a982d89SJeremy L. Thompson 
6677a982d89SJeremy L. Thompson /**
668288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
669288c0443SJeremy L Thompson 
670288c0443SJeremy L Thompson   @param ceed     A Ceed object where the CeedQFunction will be created
671288c0443SJeremy L Thompson   @param name     Name of QFunction to use from gallery
672288c0443SJeremy L Thompson   @param[out] qf  Address of the variable where the newly created
673288c0443SJeremy L Thompson                     CeedQFunction will be stored
674288c0443SJeremy L Thompson 
675288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
676288c0443SJeremy L Thompson 
6777a982d89SJeremy L. Thompson   @ref User
678288c0443SJeremy L Thompson **/
679288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
680288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
681288c0443SJeremy L Thompson   int ierr;
682f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
683288c0443SJeremy L Thompson 
6841d013790SJed Brown   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
685288c0443SJeremy L Thompson   // Find matching backend
686e15f9bd0SJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE,
687e15f9bd0SJeremy L Thompson                                 "No QFunction name provided");
688288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
689288c0443SJeremy L Thompson     size_t n;
690d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
691d1d35e2fSjeremylt     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {}
692d1d35e2fSjeremylt     if (n > match_len) {
693d1d35e2fSjeremylt       match_len = n;
694f7e22acaSJeremy L Thompson       match_index = i;
695288c0443SJeremy L Thompson     }
696288c0443SJeremy L Thompson   }
697d1d35e2fSjeremylt   if (!match_len)
698138d4072Sjeremylt     // LCOV_EXCL_START
699e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
700138d4072Sjeremylt   // LCOV_EXCL_STOP
701288c0443SJeremy L Thompson 
702288c0443SJeremy L Thompson   // Create QFunction
703d1d35e2fSjeremylt   ierr = CeedQFunctionCreateInterior(ceed,
704f7e22acaSJeremy L Thompson                                      gallery_qfunctions[match_index].vec_length,
705f7e22acaSJeremy L Thompson                                      gallery_qfunctions[match_index].f,
706ee5a26f2SJeremy L Thompson                                      gallery_qfunctions[match_index].source, qf);
707288c0443SJeremy L Thompson   CeedChk(ierr);
708288c0443SJeremy L Thompson 
709288c0443SJeremy L Thompson   // QFunction specific setup
710f7e22acaSJeremy L Thompson   ierr = gallery_qfunctions[match_index].init(ceed, name, *qf); CeedChk(ierr);
711288c0443SJeremy L Thompson 
71275affc3bSjeremylt   // Copy name
713f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name); CeedChk(ierr);
71443e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
715e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
716288c0443SJeremy L Thompson }
717288c0443SJeremy L Thompson 
718288c0443SJeremy L Thompson /**
7190219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
7200219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
7210219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
7220219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
7230219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
7240219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
7250219ea01SJeremy L Thompson            location for both.
7260219ea01SJeremy L Thompson 
7270219ea01SJeremy L Thompson   @param ceed          A Ceed object where the CeedQFunction will be created
728d1d35e2fSjeremylt   @param[in] size      Size of the QFunction fields
729d1d35e2fSjeremylt   @param[in] in_mode   CeedEvalMode for input to CeedQFunction
730d1d35e2fSjeremylt   @param[in] out_mode  CeedEvalMode for output to CeedQFunction
7310219ea01SJeremy L Thompson   @param[out] qf       Address of the variable where the newly created
7320219ea01SJeremy L Thompson                          CeedQFunction will be stored
7330219ea01SJeremy L Thompson 
7340219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
7350219ea01SJeremy L Thompson 
7367a982d89SJeremy L. Thompson   @ref User
7370219ea01SJeremy L Thompson **/
738d1d35e2fSjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode,
739d1d35e2fSjeremylt                                 CeedEvalMode out_mode, CeedQFunction *qf) {
7400219ea01SJeremy L Thompson   int ierr;
7410219ea01SJeremy L Thompson 
7420219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
743d1d35e2fSjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr);
744d1d35e2fSjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr);
7450219ea01SJeremy L Thompson 
746f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
747547dbd6fSJeremy L Thompson 
748777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
7493668ca4bSJeremy L Thompson   CeedContextFieldLabel size_label;
750547dbd6fSJeremy L Thompson   ierr = CeedQFunctionGetContext(*qf, &ctx); CeedChk(ierr);
7513668ca4bSJeremy L Thompson   ierr = CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label);
7523668ca4bSJeremy L Thompson   CeedChk(ierr);
7537bfe0f0eSJeremy L Thompson   ierr = CeedQFunctionContextSetInt32(ctx, size_label, &size); CeedChk(ierr);
754547dbd6fSJeremy L Thompson 
755e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7560219ea01SJeremy L Thompson }
7570219ea01SJeremy L Thompson 
7580219ea01SJeremy L Thompson /**
7599560d06aSjeremylt   @brief Copy the pointer to a CeedQFunction. Both pointers should
7609560d06aSjeremylt            be destroyed with `CeedQFunctionDestroy()`;
7619560d06aSjeremylt            Note: If `*qf_copy` is non-NULL, then it is assumed that
7629560d06aSjeremylt            `*qf_copy` is a pointer to a CeedQFunction. This
7639560d06aSjeremylt            CeedQFunction will be destroyed if `*qf_copy` is the only
7649560d06aSjeremylt            reference to this CeedQFunction.
7659560d06aSjeremylt 
7669560d06aSjeremylt   @param qf            CeedQFunction to copy reference to
7679560d06aSjeremylt   @param[out] qf_copy  Variable to store copied reference
7689560d06aSjeremylt 
7699560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7709560d06aSjeremylt 
7719560d06aSjeremylt   @ref User
7729560d06aSjeremylt **/
7739560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
7749560d06aSjeremylt   int ierr;
7759560d06aSjeremylt 
7769560d06aSjeremylt   ierr = CeedQFunctionReference(qf); CeedChk(ierr);
7779560d06aSjeremylt   ierr = CeedQFunctionDestroy(qf_copy); CeedChk(ierr);
7789560d06aSjeremylt   *qf_copy = qf;
7799560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7809560d06aSjeremylt }
7819560d06aSjeremylt 
7829560d06aSjeremylt /**
783a0a97fcfSJed Brown   @brief Add a CeedQFunction input
784b11c1e72Sjeremylt 
785b11c1e72Sjeremylt   @param qf          CeedQFunction
786d1d35e2fSjeremylt   @param field_name  Name of QFunction field
787d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
788d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
789d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
790b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
791b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
792b11c1e72Sjeremylt 
793b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
794dfdf5a53Sjeremylt 
7957a982d89SJeremy L. Thompson   @ref User
796b11c1e72Sjeremylt **/
797d1d35e2fSjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name,
798d1d35e2fSjeremylt                           CeedInt size,
799d1d35e2fSjeremylt                           CeedEvalMode eval_mode) {
800f04ea552SJeremy L Thompson   if (qf->is_immutable)
801e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
802e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
803f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
804e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
805d1d35e2fSjeremylt   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1))
806e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
807e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
808e15f9bd0SJeremy L Thompson                      "CEED_EVAL_WEIGHT should have size 1");
809e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
810d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields],
811d1d35e2fSjeremylt                                    field_name, size, eval_mode);
812fe2413ffSjeremylt   CeedChk(ierr);
813d1d35e2fSjeremylt   qf->num_input_fields++;
814e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
815d7b241e6Sjeremylt }
816d7b241e6Sjeremylt 
817b11c1e72Sjeremylt /**
818a0a97fcfSJed Brown   @brief Add a CeedQFunction output
819b11c1e72Sjeremylt 
820b11c1e72Sjeremylt   @param qf          CeedQFunction
821d1d35e2fSjeremylt   @param field_name  Name of QFunction field
822d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
823d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
824d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
825b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
826b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
827b11c1e72Sjeremylt 
828b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
829dfdf5a53Sjeremylt 
8307a982d89SJeremy L. Thompson   @ref User
831b11c1e72Sjeremylt **/
832d1d35e2fSjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name,
833d1d35e2fSjeremylt                            CeedInt size, CeedEvalMode eval_mode) {
834f04ea552SJeremy L Thompson   if (qf->is_immutable)
835e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
836e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
837f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
838e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
839d1d35e2fSjeremylt   if (eval_mode == CEED_EVAL_WEIGHT)
840c042f62fSJeremy L Thompson     // LCOV_EXCL_START
841e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
842e15f9bd0SJeremy L Thompson                      "Cannot create QFunction output with "
8431d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
844c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
845d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields],
846d1d35e2fSjeremylt                                    field_name, size, eval_mode);
847fe2413ffSjeremylt   CeedChk(ierr);
848d1d35e2fSjeremylt   qf->num_output_fields++;
849e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
850d7b241e6Sjeremylt }
851d7b241e6Sjeremylt 
852dfdf5a53Sjeremylt /**
85343bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
85443bbe138SJeremy L Thompson 
855f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
856f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
857f04ea552SJeremy L Thompson 
85843bbe138SJeremy L Thompson   @param qf                      CeedQFunction
859f74ec584SJeremy L Thompson   @param[out] num_input_fields   Variable to store number of input fields
860f74ec584SJeremy L Thompson   @param[out] input_fields       Variable to store input fields
861f74ec584SJeremy L Thompson   @param[out] num_output_fields  Variable to store number of output fields
862f74ec584SJeremy L Thompson   @param[out] output_fields      Variable to store output fields
86343bbe138SJeremy L Thompson 
86443bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
86543bbe138SJeremy L Thompson 
866e9b533fbSJeremy L Thompson   @ref Advanced
86743bbe138SJeremy L Thompson **/
86843bbe138SJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields,
86943bbe138SJeremy L Thompson                            CeedQFunctionField **input_fields,
87043bbe138SJeremy L Thompson                            CeedInt *num_output_fields,
87143bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
872f04ea552SJeremy L Thompson   qf->is_immutable = true;
87343bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
87443bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
87543bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
87643bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
87743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
87843bbe138SJeremy L Thompson }
87943bbe138SJeremy L Thompson 
88043bbe138SJeremy L Thompson /**
88143bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
88243bbe138SJeremy L Thompson 
88343bbe138SJeremy L Thompson   @param qf_field         CeedQFunctionField
88443bbe138SJeremy L Thompson   @param[out] field_name  Variable to store the field name
88543bbe138SJeremy L Thompson 
88643bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
88743bbe138SJeremy L Thompson 
888e9b533fbSJeremy L Thompson   @ref Advanced
88943bbe138SJeremy L Thompson **/
89043bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
89143bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
89243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
89343bbe138SJeremy L Thompson }
89443bbe138SJeremy L Thompson 
89543bbe138SJeremy L Thompson /**
89643bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
89743bbe138SJeremy L Thompson 
89843bbe138SJeremy L Thompson   @param qf_field   CeedQFunctionField
89943bbe138SJeremy L Thompson   @param[out] size  Variable to store the size of the field
90043bbe138SJeremy L Thompson 
90143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
90243bbe138SJeremy L Thompson 
903e9b533fbSJeremy L Thompson   @ref Advanced
90443bbe138SJeremy L Thompson **/
90543bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
90643bbe138SJeremy L Thompson   *size = qf_field->size;
90743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
90843bbe138SJeremy L Thompson }
90943bbe138SJeremy L Thompson 
91043bbe138SJeremy L Thompson /**
91143bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
91243bbe138SJeremy L Thompson 
91343bbe138SJeremy L Thompson   @param qf_field        CeedQFunctionField
91443bbe138SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
91543bbe138SJeremy L Thompson 
91643bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
91743bbe138SJeremy L Thompson 
918e9b533fbSJeremy L Thompson   @ref Advanced
91943bbe138SJeremy L Thompson **/
92043bbe138SJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field,
92143bbe138SJeremy L Thompson                                   CeedEvalMode *eval_mode) {
92243bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
92343bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
92443bbe138SJeremy L Thompson }
92543bbe138SJeremy L Thompson 
92643bbe138SJeremy L Thompson /**
9274ce2993fSjeremylt   @brief Set global context for a CeedQFunction
928b11c1e72Sjeremylt 
929b11c1e72Sjeremylt   @param qf   CeedQFunction
930b11c1e72Sjeremylt   @param ctx  Context data to set
931b11c1e72Sjeremylt 
932b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
933dfdf5a53Sjeremylt 
9347a982d89SJeremy L. Thompson   @ref User
935b11c1e72Sjeremylt **/
936777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
93734359f16Sjeremylt   int ierr;
9381389e7feSJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&qf->ctx); CeedChk(ierr);
939d7b241e6Sjeremylt   qf->ctx = ctx;
9409e77b9c8SJeremy L Thompson   if (ctx) {
9419560d06aSjeremylt     ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr);
9429e77b9c8SJeremy L Thompson   }
943e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
944d7b241e6Sjeremylt }
945d7b241e6Sjeremylt 
946b11c1e72Sjeremylt /**
947441428dfSJeremy L Thompson   @brief Set writability of CeedQFunctionContext when calling the `CeedQFunctionUser`.
948441428dfSJeremy L Thompson            The default value is 'is_writable == true'.
949441428dfSJeremy L Thompson 
950441428dfSJeremy L Thompson            Setting `is_writable == true` indicates the `CeedQFunctionUser` writes
951441428dfSJeremy L Thompson            into the CeedQFunctionContextData and requires memory syncronization
952441428dfSJeremy L Thompson            after calling `CeedQFunctionApply()`.
953441428dfSJeremy L Thompson 
954441428dfSJeremy L Thompson            Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not
955441428dfSJeremy L Thompson            modify the CeedQFunctionContextData. Violating this assertion may lead
956441428dfSJeremy L Thompson            to inconsistent data.
957441428dfSJeremy L Thompson 
958441428dfSJeremy L Thompson            Setting `is_writable == false` may offer a performance improvement on GPU backends.
959441428dfSJeremy L Thompson 
960441428dfSJeremy L Thompson   @param qf           CeedQFunction
961441428dfSJeremy L Thompson   @param is_writable  Writability status
962441428dfSJeremy L Thompson 
963441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
964441428dfSJeremy L Thompson 
965441428dfSJeremy L Thompson   @ref User
966441428dfSJeremy L Thompson **/
967441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
968441428dfSJeremy L Thompson   qf->is_context_writable = is_writable;
969441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
970441428dfSJeremy L Thompson }
971441428dfSJeremy L Thompson 
972441428dfSJeremy L Thompson /**
9736e15d496SJeremy L Thompson   @brief Set estimated number of FLOPs per quadrature required to apply QFunction
9746e15d496SJeremy L Thompson 
9756e15d496SJeremy L Thompson   @param qf    QFunction to estimate FLOPs for
9766e15d496SJeremy L Thompson   @param flops FLOPs per quadrature point estimate
9776e15d496SJeremy L Thompson 
9786e15d496SJeremy L Thompson   @ref Backend
9796e15d496SJeremy L Thompson **/
9809d36ca50SJeremy L Thompson int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
9816e15d496SJeremy L Thompson   if (flops < 0)
9826e15d496SJeremy L Thompson     // LCOV_EXCL_START
9836e15d496SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_INCOMPATIBLE,
9846e15d496SJeremy L Thompson                      "Must set non-negative FLOPs estimate");
9856e15d496SJeremy L Thompson   // LCOV_EXCL_STOP
9866e15d496SJeremy L Thompson   qf->user_flop_estimate = flops;
9876e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9886e15d496SJeremy L Thompson }
9896e15d496SJeremy L Thompson 
9906e15d496SJeremy L Thompson /**
99175affc3bSjeremylt   @brief View a CeedQFunction
99275affc3bSjeremylt 
99375affc3bSjeremylt   @param[in] qf      CeedQFunction to view
99475affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
99575affc3bSjeremylt 
99675affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
99775affc3bSjeremylt 
9987a982d89SJeremy L. Thompson   @ref User
99975affc3bSjeremylt **/
100075affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
100175affc3bSjeremylt   int ierr;
1002*ca5eadf8SJeremy L Thompson   char *kernel_name;
100375affc3bSjeremylt 
1004*ca5eadf8SJeremy L Thompson   ierr = CeedQFunctionGetKernelName(qf, &kernel_name); CeedChk(ierr);
1005ea6b5821SJeremy L Thompson   fprintf(stream, "%sCeedQFunction - %s\n",
100643e1b16fSJeremy L Thompson           qf->is_gallery ? "Gallery " : "User ",
1007*ca5eadf8SJeremy L Thompson           qf->is_gallery ? qf->gallery_name : kernel_name);
100875affc3bSjeremylt 
1009990fdeb6SJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " input field%s:\n", qf->num_input_fields,
1010d1d35e2fSjeremylt           qf->num_input_fields>1 ? "s" : "");
1011d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_input_fields; i++) {
1012d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream);
10132da88da5Sjeremylt     CeedChk(ierr);
101475affc3bSjeremylt   }
101575affc3bSjeremylt 
1016990fdeb6SJeremy L Thompson   fprintf(stream, "  %" CeedInt_FMT " output field%s:\n", qf->num_output_fields,
1017d1d35e2fSjeremylt           qf->num_output_fields>1 ? "s" : "");
1018d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_output_fields; i++) {
1019d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream);
102075affc3bSjeremylt     CeedChk(ierr);
102175affc3bSjeremylt   }
1022e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
102375affc3bSjeremylt }
102475affc3bSjeremylt 
102575affc3bSjeremylt /**
1026b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunction
1027b7c9bbdaSJeremy L Thompson 
1028b7c9bbdaSJeremy L Thompson   @param qf              CeedQFunction
1029b7c9bbdaSJeremy L Thompson   @param[out] ceed       Variable to store Ceed
1030b7c9bbdaSJeremy L Thompson 
1031b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1032b7c9bbdaSJeremy L Thompson 
1033b7c9bbdaSJeremy L Thompson   @ref Advanced
1034b7c9bbdaSJeremy L Thompson **/
1035b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
1036b7c9bbdaSJeremy L Thompson   *ceed = qf->ceed;
1037b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1038b7c9bbdaSJeremy L Thompson }
1039b7c9bbdaSJeremy L Thompson 
1040b7c9bbdaSJeremy L Thompson /**
1041b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
1042b11c1e72Sjeremylt 
1043f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
1044f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
1045f04ea552SJeremy L Thompson 
1046b11c1e72Sjeremylt   @param qf      CeedQFunction
1047b11c1e72Sjeremylt   @param Q       Number of quadrature points
104834138859Sjeremylt   @param[in] u   Array of input CeedVectors
104934138859Sjeremylt   @param[out] v  Array of output CeedVectors
1050b11c1e72Sjeremylt 
1051b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1052dfdf5a53Sjeremylt 
10537a982d89SJeremy L. Thompson   @ref User
1054b11c1e72Sjeremylt **/
1055d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
1056aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
1057d7b241e6Sjeremylt   int ierr;
1058d7b241e6Sjeremylt   if (!qf->Apply)
1059c042f62fSJeremy L Thompson     // LCOV_EXCL_START
1060e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED,
1061e15f9bd0SJeremy L Thompson                      "Backend does not support QFunctionApply");
1062c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
1063d1d35e2fSjeremylt   if (Q % qf->vec_length)
1064c042f62fSJeremy L Thompson     // LCOV_EXCL_START
1065e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
1066990fdeb6SJeremy L Thompson                      "Number of quadrature points %" CeedInt_FMT " must be a "
1067990fdeb6SJeremy L Thompson                      "multiple of %" CeedInt_FMT, Q, qf->vec_length);
1068c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
1069f04ea552SJeremy L Thompson   qf->is_immutable = true;
1070d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
1071e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1072d7b241e6Sjeremylt }
1073d7b241e6Sjeremylt 
1074b11c1e72Sjeremylt /**
1075b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
1076b11c1e72Sjeremylt 
1077b11c1e72Sjeremylt   @param qf  CeedQFunction to destroy
1078b11c1e72Sjeremylt 
1079b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1080dfdf5a53Sjeremylt 
10817a982d89SJeremy L. Thompson   @ref User
1082b11c1e72Sjeremylt **/
1083d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
1084d7b241e6Sjeremylt   int ierr;
1085d7b241e6Sjeremylt 
1086d1d35e2fSjeremylt   if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS;
1087fe2413ffSjeremylt   // Backend destroy
1088d7b241e6Sjeremylt   if ((*qf)->Destroy) {
1089d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
1090d7b241e6Sjeremylt   }
1091fe2413ffSjeremylt   // Free fields
109292ae7e47SJeremy L Thompson   for (CeedInt i=0; i<(*qf)->num_input_fields; i++) {
1093d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr);
1094d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr);
1095fe2413ffSjeremylt   }
109692ae7e47SJeremy L Thompson   for (CeedInt i=0; i<(*qf)->num_output_fields; i++) {
1097d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr);
1098d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr);
1099fe2413ffSjeremylt   }
1100d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr);
1101d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr);
1102777ff853SJeremy L Thompson 
1103777ff853SJeremy L Thompson   // User context data object
1104777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
1105fe2413ffSjeremylt 
1106*ca5eadf8SJeremy L Thompson   ierr = CeedFree(&(*qf)->user_source); CeedChk(ierr);
1107d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr);
110843e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->gallery_name); CeedChk(ierr);
110943e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->kernel_name); CeedChk(ierr);
1110d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
1111d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
1112e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1113d7b241e6Sjeremylt }
1114d7b241e6Sjeremylt 
1115d7b241e6Sjeremylt /// @}
1116