xref: /libCEED/interface/ceed-qfunction.c (revision ee5a26f213b810c411fd8b16edb331d78b70003c)
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);
1467a982d89SJeremy L. Thompson   fprintf(stream, "    %s Field [%d]:\n"
1477a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1487a982d89SJeremy L. Thompson           "      Size: %d\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) {
22143e1b16fSJeremy L Thompson   *kernel_name = (char *) qf->kernel_name;
22243e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
22343e1b16fSJeremy L Thompson }
22443e1b16fSJeremy L Thompson 
22543e1b16fSJeremy L Thompson /**
22643e1b16fSJeremy L Thompson   @brief Get the source path string for a CeedQFunction
22743e1b16fSJeremy L Thompson 
22843e1b16fSJeremy L Thompson   @param qf                CeedQFunction
22943e1b16fSJeremy L Thompson   @param[out] source_path  Variable to store source path string
23043e1b16fSJeremy L Thompson 
23143e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
23243e1b16fSJeremy L Thompson 
23343e1b16fSJeremy L Thompson   @ref Backend
23443e1b16fSJeremy L Thompson **/
23543e1b16fSJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) {
23643e1b16fSJeremy L Thompson   *source_path = (char *) qf->source_path;
237e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2387a982d89SJeremy L. Thompson }
2397a982d89SJeremy L. Thompson 
2407a982d89SJeremy L. Thompson /**
2413d3250a0SJeremy L Thompson   @brief Initalize and load QFunction source file into string buffer, including
2423d3250a0SJeremy L Thompson            full text of local files in place of `#include "local.h"`.
2433d3250a0SJeremy L Thompson            The `buffer` is set to `NULL` if there is no QFunction source file.
2443d3250a0SJeremy L Thompson          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
2453d3250a0SJeremy L Thompson 
2463d3250a0SJeremy L Thompson   @param qf                  CeedQFunction
247f74ec584SJeremy L Thompson   @param[out] source_buffer  String buffer for source file contents
2483d3250a0SJeremy L Thompson 
2493d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2503d3250a0SJeremy L Thompson 
2513d3250a0SJeremy L Thompson   @ref Backend
2523d3250a0SJeremy L Thompson **/
2533d3250a0SJeremy L Thompson int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, char **source_buffer) {
2543d3250a0SJeremy L Thompson   int ierr;
2553d3250a0SJeremy L Thompson   char *source_path;
2563d3250a0SJeremy L Thompson 
2573d3250a0SJeremy L Thompson   ierr = CeedQFunctionGetSourcePath(qf, &source_path); CeedChk(ierr);
2583d3250a0SJeremy L Thompson   *source_buffer = NULL;
2593d3250a0SJeremy L Thompson   if (source_path) {
2603d3250a0SJeremy L Thompson     ierr = CeedLoadSourceToBuffer(qf->ceed, source_path, source_buffer);
2613d3250a0SJeremy L Thompson     CeedChk(ierr);
2623d3250a0SJeremy L Thompson   }
2633d3250a0SJeremy L Thompson 
2643d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2653d3250a0SJeremy L Thompson }
2663d3250a0SJeremy L Thompson 
2673d3250a0SJeremy L Thompson /**
2687a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2697a982d89SJeremy L. Thompson 
2707a982d89SJeremy L. Thompson   @param qf      CeedQFunction
2717a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
2727a982d89SJeremy L. Thompson 
2737a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2747a982d89SJeremy L. Thompson 
2757a982d89SJeremy L. Thompson   @ref Backend
2767a982d89SJeremy L. Thompson **/
2777a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
2787a982d89SJeremy L. Thompson   *f = qf->function;
279e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2807a982d89SJeremy L. Thompson }
2817a982d89SJeremy L. Thompson 
2827a982d89SJeremy L. Thompson /**
283777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
284777ff853SJeremy L Thompson            Note: For QFunctions from the Fortran interface, this
285777ff853SJeremy L Thompson              function will return the Fortran context
286777ff853SJeremy L Thompson              CeedQFunctionContext.
2877a982d89SJeremy L. Thompson 
2887a982d89SJeremy L. Thompson   @param qf        CeedQFunction
289777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2907a982d89SJeremy L. Thompson 
2917a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2927a982d89SJeremy L. Thompson 
2937a982d89SJeremy L. Thompson   @ref Backend
2947a982d89SJeremy L. Thompson **/
295777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
2967a982d89SJeremy L. Thompson   *ctx = qf->ctx;
297e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2987a982d89SJeremy L. Thompson }
2997a982d89SJeremy L. Thompson 
3007a982d89SJeremy L. Thompson /**
301441428dfSJeremy L Thompson   @brief Get context data of a CeedQFunction
302441428dfSJeremy L Thompson 
303441428dfSJeremy L Thompson   @param qf         CeedQFunction
304441428dfSJeremy L Thompson   @param mem_type   Memory type on which to access the data. If the backend
305441428dfSJeremy L Thompson                       uses a different memory type, this will perform a copy.
306441428dfSJeremy L Thompson   @param[out] data  Data on memory type mem_type
307441428dfSJeremy L Thompson 
308441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
309441428dfSJeremy L Thompson 
310441428dfSJeremy L Thompson   @ref Backend
311441428dfSJeremy L Thompson **/
312441428dfSJeremy L Thompson int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type,
313441428dfSJeremy L Thompson                                 void *data) {
314441428dfSJeremy L Thompson   int ierr;
315441428dfSJeremy L Thompson   bool is_writable;
316441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
317441428dfSJeremy L Thompson 
318441428dfSJeremy L Thompson   ierr = CeedQFunctionGetContext(qf, &ctx); CeedChk(ierr);
319441428dfSJeremy L Thompson   if (ctx) {
320441428dfSJeremy L Thompson     ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr);
321441428dfSJeremy L Thompson     if (is_writable) {
322441428dfSJeremy L Thompson       ierr = CeedQFunctionContextGetData(ctx, mem_type, data); CeedChk(ierr);
323441428dfSJeremy L Thompson     } else {
324441428dfSJeremy L Thompson       ierr = CeedQFunctionContextGetDataRead(ctx, mem_type, data); CeedChk(ierr);
325441428dfSJeremy L Thompson     }
326441428dfSJeremy L Thompson   } else {
327441428dfSJeremy L Thompson     *(void **)data = NULL;
328441428dfSJeremy L Thompson   }
329441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
330441428dfSJeremy L Thompson }
331441428dfSJeremy L Thompson 
332441428dfSJeremy L Thompson /**
333441428dfSJeremy L Thompson   @brief Restore context data of a CeedQFunction
334441428dfSJeremy L Thompson 
335441428dfSJeremy L Thompson   @param qf    CeedQFunction
336441428dfSJeremy L Thompson   @param data  Data to restore
337441428dfSJeremy L Thompson 
338441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
339441428dfSJeremy L Thompson 
340441428dfSJeremy L Thompson   @ref Backend
341441428dfSJeremy L Thompson **/
342441428dfSJeremy L Thompson int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) {
343441428dfSJeremy L Thompson   int ierr;
344441428dfSJeremy L Thompson   bool is_writable;
345441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
346441428dfSJeremy L Thompson 
347441428dfSJeremy L Thompson   ierr = CeedQFunctionGetContext(qf, &ctx); CeedChk(ierr);
348441428dfSJeremy L Thompson   if (ctx) {
349441428dfSJeremy L Thompson     ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr);
350441428dfSJeremy L Thompson     if (is_writable) {
351441428dfSJeremy L Thompson       ierr = CeedQFunctionContextRestoreData(ctx, data); CeedChk(ierr);
352441428dfSJeremy L Thompson     } else {
353441428dfSJeremy L Thompson       ierr = CeedQFunctionContextRestoreDataRead(ctx, data); CeedChk(ierr);
354441428dfSJeremy L Thompson     }
355441428dfSJeremy L Thompson   }
356441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
357441428dfSJeremy L Thompson }
358441428dfSJeremy L Thompson 
359441428dfSJeremy L Thompson /**
3607a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
361777ff853SJeremy L Thompson            Note: For all QFunctions this function will return the user
362777ff853SJeremy L Thompson              CeedQFunctionContext and not interface context
363777ff853SJeremy L Thompson              CeedQFunctionContext, if any such object exists.
3647a982d89SJeremy L. Thompson 
3657a982d89SJeremy L. Thompson   @param qf        CeedQFunction
366777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
3677a982d89SJeremy L. Thompson 
3687a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3697a982d89SJeremy L. Thompson   @ref Backend
3707a982d89SJeremy L. Thompson **/
371777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
372777ff853SJeremy L Thompson   int ierr;
373f04ea552SJeremy L Thompson   if (qf->is_fortran) {
374d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
375d1d35e2fSjeremylt     ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx);
376777ff853SJeremy L Thompson     CeedChk(ierr);
3778cb0412aSJeremy L Thompson     *ctx = fortran_ctx->inner_ctx;
378d1d35e2fSjeremylt     ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx);
379d1d35e2fSjeremylt     CeedChk(ierr);
3807a982d89SJeremy L. Thompson   } else {
3817a982d89SJeremy L. Thompson     *ctx = qf->ctx;
3827a982d89SJeremy L. Thompson   }
383e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3847a982d89SJeremy L. Thompson }
3857a982d89SJeremy L. Thompson 
3867a982d89SJeremy L. Thompson /**
387441428dfSJeremy L Thompson   @brief Get inner context data of a CeedQFunction
388441428dfSJeremy L Thompson 
389441428dfSJeremy L Thompson   @param qf         CeedQFunction
390441428dfSJeremy L Thompson   @param mem_type   Memory type on which to access the data. If the backend
391441428dfSJeremy L Thompson                       uses a different memory type, this will perform a copy.
392441428dfSJeremy L Thompson   @param[out] data  Data on memory type mem_type
393441428dfSJeremy L Thompson 
394441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
395441428dfSJeremy L Thompson 
396441428dfSJeremy L Thompson   @ref Backend
397441428dfSJeremy L Thompson **/
398441428dfSJeremy L Thompson int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type,
399441428dfSJeremy L Thompson                                      void *data) {
400441428dfSJeremy L Thompson   int ierr;
401441428dfSJeremy L Thompson   bool is_writable;
402441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
403441428dfSJeremy L Thompson 
404441428dfSJeremy L Thompson   ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChk(ierr);
405441428dfSJeremy L Thompson   if (ctx) {
406441428dfSJeremy L Thompson     ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr);
407441428dfSJeremy L Thompson     if (is_writable) {
408441428dfSJeremy L Thompson       ierr = CeedQFunctionContextGetData(ctx, mem_type, data); CeedChk(ierr);
409441428dfSJeremy L Thompson     } else {
410441428dfSJeremy L Thompson       ierr = CeedQFunctionContextGetDataRead(ctx, mem_type, data); CeedChk(ierr);
411441428dfSJeremy L Thompson     }
412441428dfSJeremy L Thompson   } else {
413441428dfSJeremy L Thompson     *(void **)data = NULL;
414441428dfSJeremy L Thompson   }
415441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
416441428dfSJeremy L Thompson }
417441428dfSJeremy L Thompson 
418441428dfSJeremy L Thompson /**
419441428dfSJeremy L Thompson   @brief Restore inner context data of a CeedQFunction
420441428dfSJeremy L Thompson 
421441428dfSJeremy L Thompson   @param qf    CeedQFunction
422441428dfSJeremy L Thompson   @param data  Data to restore
423441428dfSJeremy L Thompson 
424441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
425441428dfSJeremy L Thompson 
426441428dfSJeremy L Thompson   @ref Backend
427441428dfSJeremy L Thompson **/
428441428dfSJeremy L Thompson int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) {
429441428dfSJeremy L Thompson   int ierr;
430441428dfSJeremy L Thompson   bool is_writable;
431441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
432441428dfSJeremy L Thompson 
433441428dfSJeremy L Thompson   ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChk(ierr);
434441428dfSJeremy L Thompson   if (ctx) {
435441428dfSJeremy L Thompson     ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr);
436441428dfSJeremy L Thompson     if (is_writable) {
437441428dfSJeremy L Thompson       ierr = CeedQFunctionContextRestoreData(ctx, data); CeedChk(ierr);
438441428dfSJeremy L Thompson     } else {
439441428dfSJeremy L Thompson       ierr = CeedQFunctionContextRestoreDataRead(ctx, data); CeedChk(ierr);
440441428dfSJeremy L Thompson     }
441441428dfSJeremy L Thompson   }
442441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
443441428dfSJeremy L Thompson }
444441428dfSJeremy L Thompson 
445441428dfSJeremy L Thompson /**
4467a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
4477a982d89SJeremy L. Thompson 
4487a982d89SJeremy L. Thompson   @param qf                CeedQFunction
449d1d35e2fSjeremylt   @param[out] is_identity  Variable to store identity status
4507a982d89SJeremy L. Thompson 
4517a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4527a982d89SJeremy L. Thompson 
4537a982d89SJeremy L. Thompson   @ref Backend
4547a982d89SJeremy L. Thompson **/
455d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
456f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
457e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4587a982d89SJeremy L. Thompson }
4597a982d89SJeremy L. Thompson 
4607a982d89SJeremy L. Thompson /**
461441428dfSJeremy L Thompson   @brief Determine if QFunctionContext is writable
462441428dfSJeremy L Thompson 
463441428dfSJeremy L Thompson   @param qf                CeedQFunction
464441428dfSJeremy L Thompson   @param[out] is_writable  Variable to store context writeable staus
465441428dfSJeremy L Thompson 
466441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
467441428dfSJeremy L Thompson 
468441428dfSJeremy L Thompson   @ref Backend
469441428dfSJeremy L Thompson **/
470441428dfSJeremy L Thompson int CeedQFunctionIsContextWritable(CeedQFunction qf, bool *is_writable) {
471441428dfSJeremy L Thompson   *is_writable = qf->is_context_writable;
472441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
473441428dfSJeremy L Thompson }
474441428dfSJeremy L Thompson 
475441428dfSJeremy L Thompson /**
4767a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
4777a982d89SJeremy L. Thompson 
4787a982d89SJeremy L. Thompson   @param qf         CeedQFunction
4797a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
4807a982d89SJeremy L. Thompson 
4817a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4827a982d89SJeremy L. Thompson 
4837a982d89SJeremy L. Thompson   @ref Backend
4847a982d89SJeremy L. Thompson **/
485777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
486777ff853SJeremy L Thompson   *(void **)data = qf->data;
487e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4887a982d89SJeremy L. Thompson }
4897a982d89SJeremy L. Thompson 
4907a982d89SJeremy L. Thompson /**
4917a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
4927a982d89SJeremy L. Thompson 
4937a982d89SJeremy L. Thompson   @param[out] qf  CeedQFunction
4947a982d89SJeremy L. Thompson   @param data     Data to set
4957a982d89SJeremy L. Thompson 
4967a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4977a982d89SJeremy L. Thompson 
4987a982d89SJeremy L. Thompson   @ref Backend
4997a982d89SJeremy L. Thompson **/
500777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
501777ff853SJeremy L Thompson   qf->data = data;
502e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5037a982d89SJeremy L. Thompson }
5047a982d89SJeremy L. Thompson 
5057a982d89SJeremy L. Thompson /**
50634359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
50734359f16Sjeremylt 
50834359f16Sjeremylt   @param qf  CeedQFunction to increment the reference counter
50934359f16Sjeremylt 
51034359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
51134359f16Sjeremylt 
51234359f16Sjeremylt   @ref Backend
51334359f16Sjeremylt **/
5149560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
51534359f16Sjeremylt   qf->ref_count++;
51634359f16Sjeremylt   return CEED_ERROR_SUCCESS;
51734359f16Sjeremylt }
51834359f16Sjeremylt 
5196e15d496SJeremy L Thompson /**
5206e15d496SJeremy L Thompson   @brief Estimate number of FLOPs per quadrature required to apply QFunction
5216e15d496SJeremy L Thompson 
5226e15d496SJeremy L Thompson   @param qf    QFunction to estimate FLOPs for
5236e15d496SJeremy L Thompson   @param flops Address of variable to hold FLOPs estimate
5246e15d496SJeremy L Thompson 
5256e15d496SJeremy L Thompson   @ref Backend
5266e15d496SJeremy L Thompson **/
5279d36ca50SJeremy L Thompson int CeedQFunctionGetFlopsEstimate(CeedQFunction qf, CeedSize *flops) {
5286e15d496SJeremy L Thompson   if (qf->user_flop_estimate == -1)
5296e15d496SJeremy L Thompson     // LCOV_EXCL_START
5306e15d496SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_INCOMPLETE,
5316e15d496SJeremy L Thompson                      "Must set FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
5326e15d496SJeremy L Thompson   // LCOV_EXCL_STOP
5336e15d496SJeremy L Thompson   *flops = qf->user_flop_estimate;
5346e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5356e15d496SJeremy L Thompson }
5366e15d496SJeremy L Thompson 
5377a982d89SJeremy L. Thompson /// @}
5387a982d89SJeremy L. Thompson 
5397a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5407a982d89SJeremy L. Thompson /// CeedQFunction Public API
5417a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5427a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
5437a982d89SJeremy L. Thompson /// @{
5447a982d89SJeremy L. Thompson 
5457a982d89SJeremy L. Thompson /**
5467a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
5477a982d89SJeremy L. Thompson 
5487a982d89SJeremy L. Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
549d1d35e2fSjeremylt   @param vec_length  Vector length. Caller must ensure that number of quadrature
550d1d35e2fSjeremylt                        points is a multiple of vec_length.
5517a982d89SJeremy L. Thompson   @param f           Function pointer to evaluate action at quadrature points.
5527a982d89SJeremy L. Thompson                        See \ref CeedQFunctionUser.
5537a982d89SJeremy L. Thompson   @param source      Absolute path to source of QFunction,
554c8d5b03cSJeremy L Thompson                        "\abs_path\file.h:function_name".
555c8d5b03cSJeremy L Thompson                        For support across all backends, this source must only
556c8d5b03cSJeremy L Thompson                        contain constructs supported by C99, C++11, and CUDA.
5577a982d89SJeremy L. Thompson   @param[out] qf     Address of the variable where the newly created
5587a982d89SJeremy L. Thompson                        CeedQFunction will be stored
5597a982d89SJeremy L. Thompson 
5607a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5617a982d89SJeremy L. Thompson 
5627a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
5637a982d89SJeremy L. Thompson     arguments.
5647a982d89SJeremy L. Thompson 
5657a982d89SJeremy L. Thompson   @ref User
5667a982d89SJeremy L. Thompson **/
567d1d35e2fSjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length,
568d1d35e2fSjeremylt                                 CeedQFunctionUser f,
5697a982d89SJeremy L. Thompson                                 const char *source, CeedQFunction *qf) {
5707a982d89SJeremy L. Thompson   int ierr;
57143e1b16fSJeremy L Thompson   char *source_copy, *kernel_name_copy;
5727a982d89SJeremy L. Thompson 
5737a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
5747a982d89SJeremy L. Thompson     Ceed delegate;
5757a982d89SJeremy L. Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
5767a982d89SJeremy L. Thompson 
5777a982d89SJeremy L. Thompson     if (!delegate)
5787a982d89SJeremy L. Thompson       // LCOV_EXCL_START
579e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
580e15f9bd0SJeremy L Thompson                        "Backend does not support QFunctionCreate");
5817a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
5827a982d89SJeremy L. Thompson 
583d1d35e2fSjeremylt     ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf);
5847a982d89SJeremy L. Thompson     CeedChk(ierr);
585e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
5867a982d89SJeremy L. Thompson   }
5877a982d89SJeremy L. Thompson 
588edfb5f23SJeremy L Thompson   if (strlen(source) && !strrchr(source, ':'))
58943e1b16fSJeremy L Thompson     // LCOV_EXCL_START
59043e1b16fSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPLETE,
59143e1b16fSJeremy L Thompson                      "Provided path to source does not include function name. "
59243e1b16fSJeremy L Thompson                      "Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"",
59343e1b16fSJeremy L Thompson                      source);
59443e1b16fSJeremy L Thompson   // LCOV_EXCL_STOP
59543e1b16fSJeremy L Thompson 
5967a982d89SJeremy L. Thompson   ierr = CeedCalloc(1, qf); CeedChk(ierr);
5977a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
5989560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
599d1d35e2fSjeremylt   (*qf)->ref_count = 1;
600d1d35e2fSjeremylt   (*qf)->vec_length = vec_length;
601f04ea552SJeremy L Thompson   (*qf)->is_identity = false;
602441428dfSJeremy L Thompson   (*qf)->is_context_writable = true;
6037a982d89SJeremy L. Thompson   (*qf)->function = f;
6046e15d496SJeremy L Thompson   (*qf)->user_flop_estimate = -1;
60543e1b16fSJeremy L Thompson   if (strlen(source)) {
606*ee5a26f2SJeremy L Thompson     bool is_absolute_path;
607*ee5a26f2SJeremy L Thompson     char *absolute_path;
608*ee5a26f2SJeremy L Thompson 
609*ee5a26f2SJeremy L Thompson     ierr = CeedCheckFilePath(ceed, source, &is_absolute_path); CeedChk(ierr);
610*ee5a26f2SJeremy L Thompson     if (is_absolute_path) {
611*ee5a26f2SJeremy L Thompson       absolute_path = (char *)source;
612*ee5a26f2SJeremy L Thompson     } else {
613*ee5a26f2SJeremy L Thompson       ierr = CeedGetJitAbsolutePath(ceed, source, &absolute_path); CeedChk(ierr);
614*ee5a26f2SJeremy L Thompson     }
615*ee5a26f2SJeremy L Thompson 
616*ee5a26f2SJeremy L Thompson     const char *kernel_name = strrchr(absolute_path, ':') + 1;
61743e1b16fSJeremy L Thompson     size_t kernel_name_len = strlen(kernel_name);
61843e1b16fSJeremy L Thompson     ierr = CeedCalloc(kernel_name_len + 1, &kernel_name_copy); CeedChk(ierr);
61943e1b16fSJeremy L Thompson     strncpy(kernel_name_copy, kernel_name, kernel_name_len);
62043e1b16fSJeremy L Thompson     (*qf)->kernel_name = kernel_name_copy;
62143e1b16fSJeremy L Thompson 
622*ee5a26f2SJeremy L Thompson     size_t source_len = strlen(absolute_path) - kernel_name_len - 1;
62343e1b16fSJeremy L Thompson     ierr = CeedCalloc(source_len + 1, &source_copy); CeedChk(ierr);
624*ee5a26f2SJeremy L Thompson     strncpy(source_copy, absolute_path, source_len);
625d1d35e2fSjeremylt     (*qf)->source_path = source_copy;
626*ee5a26f2SJeremy L Thompson 
627*ee5a26f2SJeremy L Thompson     if (!is_absolute_path) {
628*ee5a26f2SJeremy L Thompson       ierr = CeedFree(&absolute_path); CeedChk(ierr);
629*ee5a26f2SJeremy L Thompson     }
63043e1b16fSJeremy L Thompson   }
631bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields); CeedChk(ierr);
632bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields); CeedChk(ierr);
6337a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
634e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6357a982d89SJeremy L. Thompson }
6367a982d89SJeremy L. Thompson 
6377a982d89SJeremy L. Thompson /**
638288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
639288c0443SJeremy L Thompson 
640288c0443SJeremy L Thompson   @param ceed     A Ceed object where the CeedQFunction will be created
641288c0443SJeremy L Thompson   @param name     Name of QFunction to use from gallery
642288c0443SJeremy L Thompson   @param[out] qf  Address of the variable where the newly created
643288c0443SJeremy L Thompson                     CeedQFunction will be stored
644288c0443SJeremy L Thompson 
645288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
646288c0443SJeremy L Thompson 
6477a982d89SJeremy L. Thompson   @ref User
648288c0443SJeremy L Thompson **/
649288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
650288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
651288c0443SJeremy L Thompson   int ierr;
652f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
653288c0443SJeremy L Thompson 
6541d013790SJed Brown   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
655288c0443SJeremy L Thompson   // Find matching backend
656e15f9bd0SJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE,
657e15f9bd0SJeremy L Thompson                                 "No QFunction name provided");
658288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
659288c0443SJeremy L Thompson     size_t n;
660d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
661d1d35e2fSjeremylt     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {}
662d1d35e2fSjeremylt     if (n > match_len) {
663d1d35e2fSjeremylt       match_len = n;
664f7e22acaSJeremy L Thompson       match_index = i;
665288c0443SJeremy L Thompson     }
666288c0443SJeremy L Thompson   }
667d1d35e2fSjeremylt   if (!match_len)
668138d4072Sjeremylt     // LCOV_EXCL_START
669e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
670138d4072Sjeremylt   // LCOV_EXCL_STOP
671288c0443SJeremy L Thompson 
672288c0443SJeremy L Thompson   // Create QFunction
673d1d35e2fSjeremylt   ierr = CeedQFunctionCreateInterior(ceed,
674f7e22acaSJeremy L Thompson                                      gallery_qfunctions[match_index].vec_length,
675f7e22acaSJeremy L Thompson                                      gallery_qfunctions[match_index].f,
676*ee5a26f2SJeremy L Thompson                                      gallery_qfunctions[match_index].source, qf);
677288c0443SJeremy L Thompson   CeedChk(ierr);
678288c0443SJeremy L Thompson 
679288c0443SJeremy L Thompson   // QFunction specific setup
680f7e22acaSJeremy L Thompson   ierr = gallery_qfunctions[match_index].init(ceed, name, *qf); CeedChk(ierr);
681288c0443SJeremy L Thompson 
68275affc3bSjeremylt   // Copy name
683f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name); CeedChk(ierr);
68443e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
685e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
686288c0443SJeremy L Thompson }
687288c0443SJeremy L Thompson 
688288c0443SJeremy L Thompson /**
6890219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
6900219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
6910219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
6920219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
6930219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
6940219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
6950219ea01SJeremy L Thompson            location for both.
6960219ea01SJeremy L Thompson 
6970219ea01SJeremy L Thompson   @param ceed          A Ceed object where the CeedQFunction will be created
698d1d35e2fSjeremylt   @param[in] size      Size of the QFunction fields
699d1d35e2fSjeremylt   @param[in] in_mode   CeedEvalMode for input to CeedQFunction
700d1d35e2fSjeremylt   @param[in] out_mode  CeedEvalMode for output to CeedQFunction
7010219ea01SJeremy L Thompson   @param[out] qf       Address of the variable where the newly created
7020219ea01SJeremy L Thompson                          CeedQFunction will be stored
7030219ea01SJeremy L Thompson 
7040219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
7050219ea01SJeremy L Thompson 
7067a982d89SJeremy L. Thompson   @ref User
7070219ea01SJeremy L Thompson **/
708d1d35e2fSjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode,
709d1d35e2fSjeremylt                                 CeedEvalMode out_mode, CeedQFunction *qf) {
7100219ea01SJeremy L Thompson   int ierr;
7110219ea01SJeremy L Thompson 
7120219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
713d1d35e2fSjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr);
714d1d35e2fSjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr);
7150219ea01SJeremy L Thompson 
716f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
717547dbd6fSJeremy L Thompson 
718777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
7193668ca4bSJeremy L Thompson   CeedContextFieldLabel size_label;
720547dbd6fSJeremy L Thompson   ierr = CeedQFunctionGetContext(*qf, &ctx); CeedChk(ierr);
7213668ca4bSJeremy L Thompson   ierr = CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label);
7223668ca4bSJeremy L Thompson   CeedChk(ierr);
7237bfe0f0eSJeremy L Thompson   ierr = CeedQFunctionContextSetInt32(ctx, size_label, &size); CeedChk(ierr);
724547dbd6fSJeremy L Thompson 
725e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7260219ea01SJeremy L Thompson }
7270219ea01SJeremy L Thompson 
7280219ea01SJeremy L Thompson /**
7299560d06aSjeremylt   @brief Copy the pointer to a CeedQFunction. Both pointers should
7309560d06aSjeremylt            be destroyed with `CeedQFunctionDestroy()`;
7319560d06aSjeremylt            Note: If `*qf_copy` is non-NULL, then it is assumed that
7329560d06aSjeremylt            `*qf_copy` is a pointer to a CeedQFunction. This
7339560d06aSjeremylt            CeedQFunction will be destroyed if `*qf_copy` is the only
7349560d06aSjeremylt            reference to this CeedQFunction.
7359560d06aSjeremylt 
7369560d06aSjeremylt   @param qf            CeedQFunction to copy reference to
7379560d06aSjeremylt   @param[out] qf_copy  Variable to store copied reference
7389560d06aSjeremylt 
7399560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7409560d06aSjeremylt 
7419560d06aSjeremylt   @ref User
7429560d06aSjeremylt **/
7439560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
7449560d06aSjeremylt   int ierr;
7459560d06aSjeremylt 
7469560d06aSjeremylt   ierr = CeedQFunctionReference(qf); CeedChk(ierr);
7479560d06aSjeremylt   ierr = CeedQFunctionDestroy(qf_copy); CeedChk(ierr);
7489560d06aSjeremylt   *qf_copy = qf;
7499560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7509560d06aSjeremylt }
7519560d06aSjeremylt 
7529560d06aSjeremylt /**
753a0a97fcfSJed Brown   @brief Add a CeedQFunction input
754b11c1e72Sjeremylt 
755b11c1e72Sjeremylt   @param qf          CeedQFunction
756d1d35e2fSjeremylt   @param field_name  Name of QFunction field
757d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
758d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
759d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
760b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
761b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
762b11c1e72Sjeremylt 
763b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
764dfdf5a53Sjeremylt 
7657a982d89SJeremy L. Thompson   @ref User
766b11c1e72Sjeremylt **/
767d1d35e2fSjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name,
768d1d35e2fSjeremylt                           CeedInt size,
769d1d35e2fSjeremylt                           CeedEvalMode eval_mode) {
770f04ea552SJeremy L Thompson   if (qf->is_immutable)
771e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
772e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
773f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
774e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
775d1d35e2fSjeremylt   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1))
776e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
777e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
778e15f9bd0SJeremy L Thompson                      "CEED_EVAL_WEIGHT should have size 1");
779e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
780d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields],
781d1d35e2fSjeremylt                                    field_name, size, eval_mode);
782fe2413ffSjeremylt   CeedChk(ierr);
783d1d35e2fSjeremylt   qf->num_input_fields++;
784e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
785d7b241e6Sjeremylt }
786d7b241e6Sjeremylt 
787b11c1e72Sjeremylt /**
788a0a97fcfSJed Brown   @brief Add a CeedQFunction output
789b11c1e72Sjeremylt 
790b11c1e72Sjeremylt   @param qf          CeedQFunction
791d1d35e2fSjeremylt   @param field_name  Name of QFunction field
792d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
793d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
794d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
795b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
796b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
797b11c1e72Sjeremylt 
798b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
799dfdf5a53Sjeremylt 
8007a982d89SJeremy L. Thompson   @ref User
801b11c1e72Sjeremylt **/
802d1d35e2fSjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name,
803d1d35e2fSjeremylt                            CeedInt size, CeedEvalMode eval_mode) {
804f04ea552SJeremy L Thompson   if (qf->is_immutable)
805e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
806e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
807f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
808e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
809d1d35e2fSjeremylt   if (eval_mode == CEED_EVAL_WEIGHT)
810c042f62fSJeremy L Thompson     // LCOV_EXCL_START
811e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
812e15f9bd0SJeremy L Thompson                      "Cannot create QFunction output with "
8131d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
814c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
815d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields],
816d1d35e2fSjeremylt                                    field_name, size, eval_mode);
817fe2413ffSjeremylt   CeedChk(ierr);
818d1d35e2fSjeremylt   qf->num_output_fields++;
819e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
820d7b241e6Sjeremylt }
821d7b241e6Sjeremylt 
822dfdf5a53Sjeremylt /**
82343bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
82443bbe138SJeremy L Thompson 
825f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
826f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
827f04ea552SJeremy L Thompson 
82843bbe138SJeremy L Thompson   @param qf                      CeedQFunction
829f74ec584SJeremy L Thompson   @param[out] num_input_fields   Variable to store number of input fields
830f74ec584SJeremy L Thompson   @param[out] input_fields       Variable to store input fields
831f74ec584SJeremy L Thompson   @param[out] num_output_fields  Variable to store number of output fields
832f74ec584SJeremy L Thompson   @param[out] output_fields      Variable to store output fields
83343bbe138SJeremy L Thompson 
83443bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
83543bbe138SJeremy L Thompson 
836e9b533fbSJeremy L Thompson   @ref Advanced
83743bbe138SJeremy L Thompson **/
83843bbe138SJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields,
83943bbe138SJeremy L Thompson                            CeedQFunctionField **input_fields,
84043bbe138SJeremy L Thompson                            CeedInt *num_output_fields,
84143bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
842f04ea552SJeremy L Thompson   qf->is_immutable = true;
84343bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
84443bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
84543bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
84643bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
84743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
84843bbe138SJeremy L Thompson }
84943bbe138SJeremy L Thompson 
85043bbe138SJeremy L Thompson /**
85143bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
85243bbe138SJeremy L Thompson 
85343bbe138SJeremy L Thompson   @param qf_field         CeedQFunctionField
85443bbe138SJeremy L Thompson   @param[out] field_name  Variable to store the field name
85543bbe138SJeremy L Thompson 
85643bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
85743bbe138SJeremy L Thompson 
858e9b533fbSJeremy L Thompson   @ref Advanced
85943bbe138SJeremy L Thompson **/
86043bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
86143bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
86243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
86343bbe138SJeremy L Thompson }
86443bbe138SJeremy L Thompson 
86543bbe138SJeremy L Thompson /**
86643bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
86743bbe138SJeremy L Thompson 
86843bbe138SJeremy L Thompson   @param qf_field   CeedQFunctionField
86943bbe138SJeremy L Thompson   @param[out] size  Variable to store the size of the field
87043bbe138SJeremy L Thompson 
87143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
87243bbe138SJeremy L Thompson 
873e9b533fbSJeremy L Thompson   @ref Advanced
87443bbe138SJeremy L Thompson **/
87543bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
87643bbe138SJeremy L Thompson   *size = qf_field->size;
87743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
87843bbe138SJeremy L Thompson }
87943bbe138SJeremy L Thompson 
88043bbe138SJeremy L Thompson /**
88143bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
88243bbe138SJeremy L Thompson 
88343bbe138SJeremy L Thompson   @param qf_field        CeedQFunctionField
88443bbe138SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
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 CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field,
89143bbe138SJeremy L Thompson                                   CeedEvalMode *eval_mode) {
89243bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
89343bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
89443bbe138SJeremy L Thompson }
89543bbe138SJeremy L Thompson 
89643bbe138SJeremy L Thompson /**
8974ce2993fSjeremylt   @brief Set global context for a CeedQFunction
898b11c1e72Sjeremylt 
899b11c1e72Sjeremylt   @param qf   CeedQFunction
900b11c1e72Sjeremylt   @param ctx  Context data to set
901b11c1e72Sjeremylt 
902b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
903dfdf5a53Sjeremylt 
9047a982d89SJeremy L. Thompson   @ref User
905b11c1e72Sjeremylt **/
906777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
90734359f16Sjeremylt   int ierr;
9081389e7feSJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&qf->ctx); CeedChk(ierr);
909d7b241e6Sjeremylt   qf->ctx = ctx;
9109560d06aSjeremylt   ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr);
911e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
912d7b241e6Sjeremylt }
913d7b241e6Sjeremylt 
914b11c1e72Sjeremylt /**
915441428dfSJeremy L Thompson   @brief Set writability of CeedQFunctionContext when calling the `CeedQFunctionUser`.
916441428dfSJeremy L Thompson            The default value is 'is_writable == true'.
917441428dfSJeremy L Thompson 
918441428dfSJeremy L Thompson            Setting `is_writable == true` indicates the `CeedQFunctionUser` writes
919441428dfSJeremy L Thompson            into the CeedQFunctionContextData and requires memory syncronization
920441428dfSJeremy L Thompson            after calling `CeedQFunctionApply()`.
921441428dfSJeremy L Thompson 
922441428dfSJeremy L Thompson            Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not
923441428dfSJeremy L Thompson            modify the CeedQFunctionContextData. Violating this assertion may lead
924441428dfSJeremy L Thompson            to inconsistent data.
925441428dfSJeremy L Thompson 
926441428dfSJeremy L Thompson            Setting `is_writable == false` may offer a performance improvement on GPU backends.
927441428dfSJeremy L Thompson 
928441428dfSJeremy L Thompson   @param qf           CeedQFunction
929441428dfSJeremy L Thompson   @param is_writable  Writability status
930441428dfSJeremy L Thompson 
931441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
932441428dfSJeremy L Thompson 
933441428dfSJeremy L Thompson   @ref User
934441428dfSJeremy L Thompson **/
935441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
936441428dfSJeremy L Thompson   qf->is_context_writable = is_writable;
937441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
938441428dfSJeremy L Thompson }
939441428dfSJeremy L Thompson 
940441428dfSJeremy L Thompson /**
9416e15d496SJeremy L Thompson   @brief Set estimated number of FLOPs per quadrature required to apply QFunction
9426e15d496SJeremy L Thompson 
9436e15d496SJeremy L Thompson   @param qf    QFunction to estimate FLOPs for
9446e15d496SJeremy L Thompson   @param flops FLOPs per quadrature point estimate
9456e15d496SJeremy L Thompson 
9466e15d496SJeremy L Thompson   @ref Backend
9476e15d496SJeremy L Thompson **/
9489d36ca50SJeremy L Thompson int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
9496e15d496SJeremy L Thompson   if (flops < 0)
9506e15d496SJeremy L Thompson     // LCOV_EXCL_START
9516e15d496SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_INCOMPATIBLE,
9526e15d496SJeremy L Thompson                      "Must set non-negative FLOPs estimate");
9536e15d496SJeremy L Thompson   // LCOV_EXCL_STOP
9546e15d496SJeremy L Thompson   qf->user_flop_estimate = flops;
9556e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9566e15d496SJeremy L Thompson }
9576e15d496SJeremy L Thompson 
9586e15d496SJeremy L Thompson /**
95975affc3bSjeremylt   @brief View a CeedQFunction
96075affc3bSjeremylt 
96175affc3bSjeremylt   @param[in] qf      CeedQFunction to view
96275affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
96375affc3bSjeremylt 
96475affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
96575affc3bSjeremylt 
9667a982d89SJeremy L. Thompson   @ref User
96775affc3bSjeremylt **/
96875affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
96975affc3bSjeremylt   int ierr;
97075affc3bSjeremylt 
97175affc3bSjeremylt   fprintf(stream, "%sCeedQFunction %s\n",
97243e1b16fSJeremy L Thompson           qf->is_gallery ? "Gallery " : "User ",
97343e1b16fSJeremy L Thompson           qf->is_gallery ? qf->gallery_name : qf->kernel_name);
97475affc3bSjeremylt 
975d1d35e2fSjeremylt   fprintf(stream, "  %d Input Field%s:\n", qf->num_input_fields,
976d1d35e2fSjeremylt           qf->num_input_fields>1 ? "s" : "");
977d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_input_fields; i++) {
978d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream);
9792da88da5Sjeremylt     CeedChk(ierr);
98075affc3bSjeremylt   }
98175affc3bSjeremylt 
982d1d35e2fSjeremylt   fprintf(stream, "  %d Output Field%s:\n", qf->num_output_fields,
983d1d35e2fSjeremylt           qf->num_output_fields>1 ? "s" : "");
984d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_output_fields; i++) {
985d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream);
98675affc3bSjeremylt     CeedChk(ierr);
98775affc3bSjeremylt   }
988e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
98975affc3bSjeremylt }
99075affc3bSjeremylt 
99175affc3bSjeremylt /**
992b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunction
993b7c9bbdaSJeremy L Thompson 
994b7c9bbdaSJeremy L Thompson   @param qf              CeedQFunction
995b7c9bbdaSJeremy L Thompson   @param[out] ceed       Variable to store Ceed
996b7c9bbdaSJeremy L Thompson 
997b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
998b7c9bbdaSJeremy L Thompson 
999b7c9bbdaSJeremy L Thompson   @ref Advanced
1000b7c9bbdaSJeremy L Thompson **/
1001b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
1002b7c9bbdaSJeremy L Thompson   *ceed = qf->ceed;
1003b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1004b7c9bbdaSJeremy L Thompson }
1005b7c9bbdaSJeremy L Thompson 
1006b7c9bbdaSJeremy L Thompson /**
1007b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
1008b11c1e72Sjeremylt 
1009f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
1010f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
1011f04ea552SJeremy L Thompson 
1012b11c1e72Sjeremylt   @param qf      CeedQFunction
1013b11c1e72Sjeremylt   @param Q       Number of quadrature points
101434138859Sjeremylt   @param[in] u   Array of input CeedVectors
101534138859Sjeremylt   @param[out] v  Array of output CeedVectors
1016b11c1e72Sjeremylt 
1017b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1018dfdf5a53Sjeremylt 
10197a982d89SJeremy L. Thompson   @ref User
1020b11c1e72Sjeremylt **/
1021d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
1022aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
1023d7b241e6Sjeremylt   int ierr;
1024d7b241e6Sjeremylt   if (!qf->Apply)
1025c042f62fSJeremy L Thompson     // LCOV_EXCL_START
1026e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED,
1027e15f9bd0SJeremy L Thompson                      "Backend does not support QFunctionApply");
1028c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
1029d1d35e2fSjeremylt   if (Q % qf->vec_length)
1030c042f62fSJeremy L Thompson     // LCOV_EXCL_START
1031e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
1032e15f9bd0SJeremy L Thompson                      "Number of quadrature points %d must be a "
1033d1d35e2fSjeremylt                      "multiple of %d", Q, qf->vec_length);
1034c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
1035f04ea552SJeremy L Thompson   qf->is_immutable = true;
1036d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
1037e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1038d7b241e6Sjeremylt }
1039d7b241e6Sjeremylt 
1040b11c1e72Sjeremylt /**
1041b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
1042b11c1e72Sjeremylt 
1043b11c1e72Sjeremylt   @param qf  CeedQFunction to destroy
1044b11c1e72Sjeremylt 
1045b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1046dfdf5a53Sjeremylt 
10477a982d89SJeremy L. Thompson   @ref User
1048b11c1e72Sjeremylt **/
1049d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
1050d7b241e6Sjeremylt   int ierr;
1051d7b241e6Sjeremylt 
1052d1d35e2fSjeremylt   if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS;
1053fe2413ffSjeremylt   // Backend destroy
1054d7b241e6Sjeremylt   if ((*qf)->Destroy) {
1055d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
1056d7b241e6Sjeremylt   }
1057fe2413ffSjeremylt   // Free fields
1058d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_input_fields; i++) {
1059d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr);
1060d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr);
1061fe2413ffSjeremylt   }
1062d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_output_fields; i++) {
1063d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr);
1064d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr);
1065fe2413ffSjeremylt   }
1066d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr);
1067d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr);
1068777ff853SJeremy L Thompson 
1069777ff853SJeremy L Thompson   // User context data object
1070777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
1071fe2413ffSjeremylt 
1072d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr);
107343e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->gallery_name); CeedChk(ierr);
107443e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->kernel_name); CeedChk(ierr);
1075d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
1076d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
1077e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1078d7b241e6Sjeremylt }
1079d7b241e6Sjeremylt 
1080d7b241e6Sjeremylt /// @}
1081