xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision 6eb0d8b4aff72517bac7a1ace48e04610a8fe084)
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)) {
70*6eb0d8b4SJeremy L Thompson   int ierr;
71*6eb0d8b4SJeremy 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 
80*6eb0d8b4SJeremy L Thompson   const char *relative_file_path;
81*6eb0d8b4SJeremy L Thompson   ierr = CeedGetJitRelativePath(source, &relative_file_path); CeedChk(ierr);
82*6eb0d8b4SJeremy 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;
85*6eb0d8b4SJeremy 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)) {
60643e1b16fSJeremy L Thompson     const char *kernel_name = strrchr(source, ':') + 1;
60743e1b16fSJeremy L Thompson     size_t kernel_name_len = strlen(kernel_name);
60843e1b16fSJeremy L Thompson     ierr = CeedCalloc(kernel_name_len + 1, &kernel_name_copy); CeedChk(ierr);
60943e1b16fSJeremy L Thompson     strncpy(kernel_name_copy, kernel_name, kernel_name_len);
61043e1b16fSJeremy L Thompson     (*qf)->kernel_name = kernel_name_copy;
61143e1b16fSJeremy L Thompson 
61243e1b16fSJeremy L Thompson     size_t source_len = strlen(source) - kernel_name_len - 1;
61343e1b16fSJeremy L Thompson     ierr = CeedCalloc(source_len + 1, &source_copy); CeedChk(ierr);
61443e1b16fSJeremy L Thompson     strncpy(source_copy, source, source_len);
615d1d35e2fSjeremylt     (*qf)->source_path = source_copy;
61643e1b16fSJeremy L Thompson   }
617bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields); CeedChk(ierr);
618bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields); CeedChk(ierr);
6197a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
620e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6217a982d89SJeremy L. Thompson }
6227a982d89SJeremy L. Thompson 
6237a982d89SJeremy L. Thompson /**
624288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
625288c0443SJeremy L Thompson 
626288c0443SJeremy L Thompson   @param ceed     A Ceed object where the CeedQFunction will be created
627288c0443SJeremy L Thompson   @param name     Name of QFunction to use from gallery
628288c0443SJeremy L Thompson   @param[out] qf  Address of the variable where the newly created
629288c0443SJeremy L Thompson                     CeedQFunction will be stored
630288c0443SJeremy L Thompson 
631288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
632288c0443SJeremy L Thompson 
6337a982d89SJeremy L. Thompson   @ref User
634288c0443SJeremy L Thompson **/
635288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
636288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
637288c0443SJeremy L Thompson   int ierr;
638f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
639288c0443SJeremy L Thompson 
6401d013790SJed Brown   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
641288c0443SJeremy L Thompson   // Find matching backend
642e15f9bd0SJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE,
643e15f9bd0SJeremy L Thompson                                 "No QFunction name provided");
644288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
645288c0443SJeremy L Thompson     size_t n;
646d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
647d1d35e2fSjeremylt     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {}
648d1d35e2fSjeremylt     if (n > match_len) {
649d1d35e2fSjeremylt       match_len = n;
650f7e22acaSJeremy L Thompson       match_index = i;
651288c0443SJeremy L Thompson     }
652288c0443SJeremy L Thompson   }
653d1d35e2fSjeremylt   if (!match_len)
654138d4072Sjeremylt     // LCOV_EXCL_START
655e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
656138d4072Sjeremylt   // LCOV_EXCL_STOP
657288c0443SJeremy L Thompson 
658*6eb0d8b4SJeremy L Thompson   // Build source path
659*6eb0d8b4SJeremy L Thompson   char *gallery_qfunction_source_path;
660*6eb0d8b4SJeremy L Thompson 
661288c0443SJeremy L Thompson   // Create QFunction
662*6eb0d8b4SJeremy L Thompson   ierr = CeedGetInstalledJitPath(ceed, gallery_qfunctions[match_index].source,
663*6eb0d8b4SJeremy L Thompson                                  &gallery_qfunction_source_path); CeedChk(ierr);
664d1d35e2fSjeremylt   ierr = CeedQFunctionCreateInterior(ceed,
665f7e22acaSJeremy L Thompson                                      gallery_qfunctions[match_index].vec_length,
666f7e22acaSJeremy L Thompson                                      gallery_qfunctions[match_index].f,
667*6eb0d8b4SJeremy L Thompson                                      gallery_qfunction_source_path, qf);
668288c0443SJeremy L Thompson   CeedChk(ierr);
669*6eb0d8b4SJeremy L Thompson   ierr = CeedFree(&gallery_qfunction_source_path); CeedChkBackend(ierr);
670288c0443SJeremy L Thompson 
671288c0443SJeremy L Thompson   // QFunction specific setup
672f7e22acaSJeremy L Thompson   ierr = gallery_qfunctions[match_index].init(ceed, name, *qf); CeedChk(ierr);
673288c0443SJeremy L Thompson 
67475affc3bSjeremylt   // Copy name
675f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name); CeedChk(ierr);
67643e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
677e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
678288c0443SJeremy L Thompson }
679288c0443SJeremy L Thompson 
680288c0443SJeremy L Thompson /**
6810219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
6820219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
6830219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
6840219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
6850219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
6860219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
6870219ea01SJeremy L Thompson            location for both.
6880219ea01SJeremy L Thompson 
6890219ea01SJeremy L Thompson   @param ceed          A Ceed object where the CeedQFunction will be created
690d1d35e2fSjeremylt   @param[in] size      Size of the QFunction fields
691d1d35e2fSjeremylt   @param[in] in_mode   CeedEvalMode for input to CeedQFunction
692d1d35e2fSjeremylt   @param[in] out_mode  CeedEvalMode for output to CeedQFunction
6930219ea01SJeremy L Thompson   @param[out] qf       Address of the variable where the newly created
6940219ea01SJeremy L Thompson                          CeedQFunction will be stored
6950219ea01SJeremy L Thompson 
6960219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
6970219ea01SJeremy L Thompson 
6987a982d89SJeremy L. Thompson   @ref User
6990219ea01SJeremy L Thompson **/
700d1d35e2fSjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode,
701d1d35e2fSjeremylt                                 CeedEvalMode out_mode, CeedQFunction *qf) {
7020219ea01SJeremy L Thompson   int ierr;
7030219ea01SJeremy L Thompson 
7040219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
705d1d35e2fSjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr);
706d1d35e2fSjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr);
7070219ea01SJeremy L Thompson 
708f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
709547dbd6fSJeremy L Thompson 
710777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
7113668ca4bSJeremy L Thompson   CeedContextFieldLabel size_label;
712547dbd6fSJeremy L Thompson   ierr = CeedQFunctionGetContext(*qf, &ctx); CeedChk(ierr);
7133668ca4bSJeremy L Thompson   ierr = CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label);
7143668ca4bSJeremy L Thompson   CeedChk(ierr);
7157bfe0f0eSJeremy L Thompson   ierr = CeedQFunctionContextSetInt32(ctx, size_label, &size); CeedChk(ierr);
716547dbd6fSJeremy L Thompson 
717e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7180219ea01SJeremy L Thompson }
7190219ea01SJeremy L Thompson 
7200219ea01SJeremy L Thompson /**
7219560d06aSjeremylt   @brief Copy the pointer to a CeedQFunction. Both pointers should
7229560d06aSjeremylt            be destroyed with `CeedQFunctionDestroy()`;
7239560d06aSjeremylt            Note: If `*qf_copy` is non-NULL, then it is assumed that
7249560d06aSjeremylt            `*qf_copy` is a pointer to a CeedQFunction. This
7259560d06aSjeremylt            CeedQFunction will be destroyed if `*qf_copy` is the only
7269560d06aSjeremylt            reference to this CeedQFunction.
7279560d06aSjeremylt 
7289560d06aSjeremylt   @param qf            CeedQFunction to copy reference to
7299560d06aSjeremylt   @param[out] qf_copy  Variable to store copied reference
7309560d06aSjeremylt 
7319560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7329560d06aSjeremylt 
7339560d06aSjeremylt   @ref User
7349560d06aSjeremylt **/
7359560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
7369560d06aSjeremylt   int ierr;
7379560d06aSjeremylt 
7389560d06aSjeremylt   ierr = CeedQFunctionReference(qf); CeedChk(ierr);
7399560d06aSjeremylt   ierr = CeedQFunctionDestroy(qf_copy); CeedChk(ierr);
7409560d06aSjeremylt   *qf_copy = qf;
7419560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7429560d06aSjeremylt }
7439560d06aSjeremylt 
7449560d06aSjeremylt /**
745a0a97fcfSJed Brown   @brief Add a CeedQFunction input
746b11c1e72Sjeremylt 
747b11c1e72Sjeremylt   @param qf          CeedQFunction
748d1d35e2fSjeremylt   @param field_name  Name of QFunction field
749d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
750d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
751d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
752b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
753b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
754b11c1e72Sjeremylt 
755b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
756dfdf5a53Sjeremylt 
7577a982d89SJeremy L. Thompson   @ref User
758b11c1e72Sjeremylt **/
759d1d35e2fSjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name,
760d1d35e2fSjeremylt                           CeedInt size,
761d1d35e2fSjeremylt                           CeedEvalMode eval_mode) {
762f04ea552SJeremy L Thompson   if (qf->is_immutable)
763e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
764e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
765f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
766e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
767d1d35e2fSjeremylt   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1))
768e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
769e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
770e15f9bd0SJeremy L Thompson                      "CEED_EVAL_WEIGHT should have size 1");
771e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
772d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields],
773d1d35e2fSjeremylt                                    field_name, size, eval_mode);
774fe2413ffSjeremylt   CeedChk(ierr);
775d1d35e2fSjeremylt   qf->num_input_fields++;
776e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
777d7b241e6Sjeremylt }
778d7b241e6Sjeremylt 
779b11c1e72Sjeremylt /**
780a0a97fcfSJed Brown   @brief Add a CeedQFunction output
781b11c1e72Sjeremylt 
782b11c1e72Sjeremylt   @param qf          CeedQFunction
783d1d35e2fSjeremylt   @param field_name  Name of QFunction field
784d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
785d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
786d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
787b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
788b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
789b11c1e72Sjeremylt 
790b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
791dfdf5a53Sjeremylt 
7927a982d89SJeremy L. Thompson   @ref User
793b11c1e72Sjeremylt **/
794d1d35e2fSjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name,
795d1d35e2fSjeremylt                            CeedInt size, CeedEvalMode eval_mode) {
796f04ea552SJeremy L Thompson   if (qf->is_immutable)
797e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
798e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
799f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
800e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
801d1d35e2fSjeremylt   if (eval_mode == CEED_EVAL_WEIGHT)
802c042f62fSJeremy L Thompson     // LCOV_EXCL_START
803e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
804e15f9bd0SJeremy L Thompson                      "Cannot create QFunction output with "
8051d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
806c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
807d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields],
808d1d35e2fSjeremylt                                    field_name, size, eval_mode);
809fe2413ffSjeremylt   CeedChk(ierr);
810d1d35e2fSjeremylt   qf->num_output_fields++;
811e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
812d7b241e6Sjeremylt }
813d7b241e6Sjeremylt 
814dfdf5a53Sjeremylt /**
81543bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
81643bbe138SJeremy L Thompson 
817f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
818f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
819f04ea552SJeremy L Thompson 
82043bbe138SJeremy L Thompson   @param qf                      CeedQFunction
821f74ec584SJeremy L Thompson   @param[out] num_input_fields   Variable to store number of input fields
822f74ec584SJeremy L Thompson   @param[out] input_fields       Variable to store input fields
823f74ec584SJeremy L Thompson   @param[out] num_output_fields  Variable to store number of output fields
824f74ec584SJeremy L Thompson   @param[out] output_fields      Variable to store output fields
82543bbe138SJeremy L Thompson 
82643bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
82743bbe138SJeremy L Thompson 
828e9b533fbSJeremy L Thompson   @ref Advanced
82943bbe138SJeremy L Thompson **/
83043bbe138SJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields,
83143bbe138SJeremy L Thompson                            CeedQFunctionField **input_fields,
83243bbe138SJeremy L Thompson                            CeedInt *num_output_fields,
83343bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
834f04ea552SJeremy L Thompson   qf->is_immutable = true;
83543bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
83643bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
83743bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
83843bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
83943bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
84043bbe138SJeremy L Thompson }
84143bbe138SJeremy L Thompson 
84243bbe138SJeremy L Thompson /**
84343bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
84443bbe138SJeremy L Thompson 
84543bbe138SJeremy L Thompson   @param qf_field         CeedQFunctionField
84643bbe138SJeremy L Thompson   @param[out] field_name  Variable to store the field name
84743bbe138SJeremy L Thompson 
84843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
84943bbe138SJeremy L Thompson 
850e9b533fbSJeremy L Thompson   @ref Advanced
85143bbe138SJeremy L Thompson **/
85243bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
85343bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
85443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
85543bbe138SJeremy L Thompson }
85643bbe138SJeremy L Thompson 
85743bbe138SJeremy L Thompson /**
85843bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
85943bbe138SJeremy L Thompson 
86043bbe138SJeremy L Thompson   @param qf_field   CeedQFunctionField
86143bbe138SJeremy L Thompson   @param[out] size  Variable to store the size of the field
86243bbe138SJeremy L Thompson 
86343bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
86443bbe138SJeremy L Thompson 
865e9b533fbSJeremy L Thompson   @ref Advanced
86643bbe138SJeremy L Thompson **/
86743bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
86843bbe138SJeremy L Thompson   *size = qf_field->size;
86943bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
87043bbe138SJeremy L Thompson }
87143bbe138SJeremy L Thompson 
87243bbe138SJeremy L Thompson /**
87343bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
87443bbe138SJeremy L Thompson 
87543bbe138SJeremy L Thompson   @param qf_field        CeedQFunctionField
87643bbe138SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
87743bbe138SJeremy L Thompson 
87843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
87943bbe138SJeremy L Thompson 
880e9b533fbSJeremy L Thompson   @ref Advanced
88143bbe138SJeremy L Thompson **/
88243bbe138SJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field,
88343bbe138SJeremy L Thompson                                   CeedEvalMode *eval_mode) {
88443bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
88543bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
88643bbe138SJeremy L Thompson }
88743bbe138SJeremy L Thompson 
88843bbe138SJeremy L Thompson /**
8894ce2993fSjeremylt   @brief Set global context for a CeedQFunction
890b11c1e72Sjeremylt 
891b11c1e72Sjeremylt   @param qf   CeedQFunction
892b11c1e72Sjeremylt   @param ctx  Context data to set
893b11c1e72Sjeremylt 
894b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
895dfdf5a53Sjeremylt 
8967a982d89SJeremy L. Thompson   @ref User
897b11c1e72Sjeremylt **/
898777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
89934359f16Sjeremylt   int ierr;
9001389e7feSJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&qf->ctx); CeedChk(ierr);
901d7b241e6Sjeremylt   qf->ctx = ctx;
9029560d06aSjeremylt   ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr);
903e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
904d7b241e6Sjeremylt }
905d7b241e6Sjeremylt 
906b11c1e72Sjeremylt /**
907441428dfSJeremy L Thompson   @brief Set writability of CeedQFunctionContext when calling the `CeedQFunctionUser`.
908441428dfSJeremy L Thompson            The default value is 'is_writable == true'.
909441428dfSJeremy L Thompson 
910441428dfSJeremy L Thompson            Setting `is_writable == true` indicates the `CeedQFunctionUser` writes
911441428dfSJeremy L Thompson            into the CeedQFunctionContextData and requires memory syncronization
912441428dfSJeremy L Thompson            after calling `CeedQFunctionApply()`.
913441428dfSJeremy L Thompson 
914441428dfSJeremy L Thompson            Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not
915441428dfSJeremy L Thompson            modify the CeedQFunctionContextData. Violating this assertion may lead
916441428dfSJeremy L Thompson            to inconsistent data.
917441428dfSJeremy L Thompson 
918441428dfSJeremy L Thompson            Setting `is_writable == false` may offer a performance improvement on GPU backends.
919441428dfSJeremy L Thompson 
920441428dfSJeremy L Thompson   @param qf           CeedQFunction
921441428dfSJeremy L Thompson   @param is_writable  Writability status
922441428dfSJeremy L Thompson 
923441428dfSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
924441428dfSJeremy L Thompson 
925441428dfSJeremy L Thompson   @ref User
926441428dfSJeremy L Thompson **/
927441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
928441428dfSJeremy L Thompson   qf->is_context_writable = is_writable;
929441428dfSJeremy L Thompson   return CEED_ERROR_SUCCESS;
930441428dfSJeremy L Thompson }
931441428dfSJeremy L Thompson 
932441428dfSJeremy L Thompson /**
9336e15d496SJeremy L Thompson   @brief Set estimated number of FLOPs per quadrature required to apply QFunction
9346e15d496SJeremy L Thompson 
9356e15d496SJeremy L Thompson   @param qf    QFunction to estimate FLOPs for
9366e15d496SJeremy L Thompson   @param flops FLOPs per quadrature point estimate
9376e15d496SJeremy L Thompson 
9386e15d496SJeremy L Thompson   @ref Backend
9396e15d496SJeremy L Thompson **/
9409d36ca50SJeremy L Thompson int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
9416e15d496SJeremy L Thompson   if (flops < 0)
9426e15d496SJeremy L Thompson     // LCOV_EXCL_START
9436e15d496SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_INCOMPATIBLE,
9446e15d496SJeremy L Thompson                      "Must set non-negative FLOPs estimate");
9456e15d496SJeremy L Thompson   // LCOV_EXCL_STOP
9466e15d496SJeremy L Thompson   qf->user_flop_estimate = flops;
9476e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9486e15d496SJeremy L Thompson }
9496e15d496SJeremy L Thompson 
9506e15d496SJeremy L Thompson /**
95175affc3bSjeremylt   @brief View a CeedQFunction
95275affc3bSjeremylt 
95375affc3bSjeremylt   @param[in] qf      CeedQFunction to view
95475affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
95575affc3bSjeremylt 
95675affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
95775affc3bSjeremylt 
9587a982d89SJeremy L. Thompson   @ref User
95975affc3bSjeremylt **/
96075affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
96175affc3bSjeremylt   int ierr;
96275affc3bSjeremylt 
96375affc3bSjeremylt   fprintf(stream, "%sCeedQFunction %s\n",
96443e1b16fSJeremy L Thompson           qf->is_gallery ? "Gallery " : "User ",
96543e1b16fSJeremy L Thompson           qf->is_gallery ? qf->gallery_name : qf->kernel_name);
96675affc3bSjeremylt 
967d1d35e2fSjeremylt   fprintf(stream, "  %d Input Field%s:\n", qf->num_input_fields,
968d1d35e2fSjeremylt           qf->num_input_fields>1 ? "s" : "");
969d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_input_fields; i++) {
970d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream);
9712da88da5Sjeremylt     CeedChk(ierr);
97275affc3bSjeremylt   }
97375affc3bSjeremylt 
974d1d35e2fSjeremylt   fprintf(stream, "  %d Output Field%s:\n", qf->num_output_fields,
975d1d35e2fSjeremylt           qf->num_output_fields>1 ? "s" : "");
976d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_output_fields; i++) {
977d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream);
97875affc3bSjeremylt     CeedChk(ierr);
97975affc3bSjeremylt   }
980e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
98175affc3bSjeremylt }
98275affc3bSjeremylt 
98375affc3bSjeremylt /**
984b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunction
985b7c9bbdaSJeremy L Thompson 
986b7c9bbdaSJeremy L Thompson   @param qf              CeedQFunction
987b7c9bbdaSJeremy L Thompson   @param[out] ceed       Variable to store Ceed
988b7c9bbdaSJeremy L Thompson 
989b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
990b7c9bbdaSJeremy L Thompson 
991b7c9bbdaSJeremy L Thompson   @ref Advanced
992b7c9bbdaSJeremy L Thompson **/
993b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
994b7c9bbdaSJeremy L Thompson   *ceed = qf->ceed;
995b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
996b7c9bbdaSJeremy L Thompson }
997b7c9bbdaSJeremy L Thompson 
998b7c9bbdaSJeremy L Thompson /**
999b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
1000b11c1e72Sjeremylt 
1001f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
1002f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
1003f04ea552SJeremy L Thompson 
1004b11c1e72Sjeremylt   @param qf      CeedQFunction
1005b11c1e72Sjeremylt   @param Q       Number of quadrature points
100634138859Sjeremylt   @param[in] u   Array of input CeedVectors
100734138859Sjeremylt   @param[out] v  Array of output CeedVectors
1008b11c1e72Sjeremylt 
1009b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1010dfdf5a53Sjeremylt 
10117a982d89SJeremy L. Thompson   @ref User
1012b11c1e72Sjeremylt **/
1013d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
1014aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
1015d7b241e6Sjeremylt   int ierr;
1016d7b241e6Sjeremylt   if (!qf->Apply)
1017c042f62fSJeremy L Thompson     // LCOV_EXCL_START
1018e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED,
1019e15f9bd0SJeremy L Thompson                      "Backend does not support QFunctionApply");
1020c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
1021d1d35e2fSjeremylt   if (Q % qf->vec_length)
1022c042f62fSJeremy L Thompson     // LCOV_EXCL_START
1023e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
1024e15f9bd0SJeremy L Thompson                      "Number of quadrature points %d must be a "
1025d1d35e2fSjeremylt                      "multiple of %d", Q, qf->vec_length);
1026c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
1027f04ea552SJeremy L Thompson   qf->is_immutable = true;
1028d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
1029e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1030d7b241e6Sjeremylt }
1031d7b241e6Sjeremylt 
1032b11c1e72Sjeremylt /**
1033b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
1034b11c1e72Sjeremylt 
1035b11c1e72Sjeremylt   @param qf  CeedQFunction to destroy
1036b11c1e72Sjeremylt 
1037b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1038dfdf5a53Sjeremylt 
10397a982d89SJeremy L. Thompson   @ref User
1040b11c1e72Sjeremylt **/
1041d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
1042d7b241e6Sjeremylt   int ierr;
1043d7b241e6Sjeremylt 
1044d1d35e2fSjeremylt   if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS;
1045fe2413ffSjeremylt   // Backend destroy
1046d7b241e6Sjeremylt   if ((*qf)->Destroy) {
1047d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
1048d7b241e6Sjeremylt   }
1049fe2413ffSjeremylt   // Free fields
1050d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_input_fields; i++) {
1051d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr);
1052d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr);
1053fe2413ffSjeremylt   }
1054d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_output_fields; i++) {
1055d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr);
1056d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr);
1057fe2413ffSjeremylt   }
1058d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr);
1059d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr);
1060777ff853SJeremy L Thompson 
1061777ff853SJeremy L Thompson   // User context data object
1062777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
1063fe2413ffSjeremylt 
1064d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr);
106543e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->gallery_name); CeedChk(ierr);
106643e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->kernel_name); CeedChk(ierr);
1067d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
1068d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
1069e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1070d7b241e6Sjeremylt }
1071d7b241e6Sjeremylt 
1072d7b241e6Sjeremylt /// @}
1073