xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision 3d3250a08f026bd5a3ff436a992143d30c7d6e16)
1d7b241e6Sjeremylt // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2d7b241e6Sjeremylt // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3d7b241e6Sjeremylt // reserved. See files LICENSE and NOTICE for details.
4d7b241e6Sjeremylt //
5d7b241e6Sjeremylt // This file is part of CEED, a collection of benchmarks, miniapps, software
6d7b241e6Sjeremylt // libraries and APIs for efficient high-order finite element and spectral
7d7b241e6Sjeremylt // element discretizations for exascale applications. For more information and
8d7b241e6Sjeremylt // source code availability see http://github.com/ceed.
9d7b241e6Sjeremylt //
10d7b241e6Sjeremylt // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11d7b241e6Sjeremylt // a collaborative effort of two U.S. Department of Energy organizations (Office
12d7b241e6Sjeremylt // of Science and the National Nuclear Security Administration) responsible for
13d7b241e6Sjeremylt // the planning and preparation of a capable exascale ecosystem, including
14d7b241e6Sjeremylt // software, applications, hardware, advanced system engineering and early
15d7b241e6Sjeremylt // testbed platforms, in support of the nation's exascale computing imperative.
16d7b241e6Sjeremylt 
17ec3da8bcSJed Brown #include <ceed/ceed.h>
18ec3da8bcSJed Brown #include <ceed/backend.h>
19*3d3250a0SJeremy L Thompson #include <ceed/jit-tools.h>
203d576824SJeremy L Thompson #include <ceed-impl.h>
21288c0443SJeremy L Thompson #include <limits.h>
223d576824SJeremy L Thompson #include <stdbool.h>
233d576824SJeremy L Thompson #include <stdio.h>
2443e1b16fSJeremy L Thompson #include <stdlib.h>
253d576824SJeremy L Thompson #include <string.h>
26288c0443SJeremy L Thompson 
277a982d89SJeremy L. Thompson /// @file
287a982d89SJeremy L. Thompson /// Implementation of public CeedQFunction interfaces
297a982d89SJeremy L. Thompson 
30288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP
31442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none;
32442e7f0bSjeremylt /// @endcond
33442e7f0bSjeremylt 
347a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
357a982d89SJeremy L. Thompson /// @{
367a982d89SJeremy L. Thompson 
377a982d89SJeremy L. Thompson // Indicate that no QFunction is provided by the user
387a982d89SJeremy L. Thompson const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
397a982d89SJeremy L. Thompson 
407a982d89SJeremy L. Thompson /// @}
417a982d89SJeremy L. Thompson 
42442e7f0bSjeremylt /// @cond DOXYGEN_SKIP
43288c0443SJeremy L Thompson static struct {
44288c0443SJeremy L Thompson   char name[CEED_MAX_RESOURCE_LEN];
45288c0443SJeremy L Thompson   char source[CEED_MAX_RESOURCE_LEN];
46d1d35e2fSjeremylt   CeedInt vec_length;
47288c0443SJeremy L Thompson   CeedQFunctionUser f;
48288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
49d1d35e2fSjeremylt } gallery_qfunctions[1024];
50288c0443SJeremy L Thompson static size_t num_qfunctions;
51288c0443SJeremy L Thompson /// @endcond
52d7b241e6Sjeremylt 
53777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
54777ff853SJeremy L Thompson /// CeedQFunction Library Internal Functions
55777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
56777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper
57777ff853SJeremy L Thompson /// @{
58777ff853SJeremy L Thompson 
59b11c1e72Sjeremylt /**
60288c0443SJeremy L Thompson   @brief Register a gallery QFunction
61288c0443SJeremy L Thompson 
62288c0443SJeremy L Thompson   @param name        Name for this backend to respond to
631176cc3aSJeremy L Thompson   @param source      Absolute path to source of QFunction,
641176cc3aSJeremy L Thompson                        "\path\CEED_DIR\gallery\folder\file.h:function_name"
65d1d35e2fSjeremylt   @param vec_length  Vector length.  Caller must ensure that number of quadrature
66d1d35e2fSjeremylt                        points is a multiple of vec_length.
67288c0443SJeremy L Thompson   @param f           Function pointer to evaluate action at quadrature points.
68288c0443SJeremy L Thompson                        See \ref CeedQFunctionUser.
69288c0443SJeremy L Thompson   @param init        Initialization function called by CeedQFunctionInit() when the
70288c0443SJeremy L Thompson                        QFunction is selected.
71288c0443SJeremy L Thompson 
72288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
73288c0443SJeremy L Thompson 
747a982d89SJeremy L. Thompson   @ref Developer
75288c0443SJeremy L Thompson **/
76288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source,
77d1d35e2fSjeremylt                           CeedInt vec_length, CeedQFunctionUser f,
78288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
79d1d35e2fSjeremylt   if (num_qfunctions >= sizeof(gallery_qfunctions) / sizeof(
80d1d35e2fSjeremylt         gallery_qfunctions[0]))
81c042f62fSJeremy L Thompson     // LCOV_EXCL_START
82e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
83c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
84c042f62fSJeremy L Thompson 
85d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
86d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
87d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].source, source,
88d1d35e2fSjeremylt           CEED_MAX_RESOURCE_LEN);
89d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
90d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].vec_length = vec_length;
91d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].f = f;
92d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].init = init;
93288c0443SJeremy L Thompson   num_qfunctions++;
94e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
95288c0443SJeremy L Thompson }
96288c0443SJeremy L Thompson 
97288c0443SJeremy L Thompson /**
987a982d89SJeremy L. Thompson   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
997a982d89SJeremy L. Thompson 
1007a982d89SJeremy L. Thompson   @param f           CeedQFunctionField
101d1d35e2fSjeremylt   @param field_name  Name of QFunction field
102d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
103d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT
104d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
1057a982d89SJeremy L. Thompson                        \ref CEED_EVAL_INTERP to use interpolated values,
1067a982d89SJeremy L. Thompson                        \ref CEED_EVAL_GRAD to use gradients,
1077a982d89SJeremy L. Thompson                        \ref CEED_EVAL_WEIGHT to use quadrature weights.
1087a982d89SJeremy L. Thompson 
1097a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1107a982d89SJeremy L. Thompson 
1117a982d89SJeremy L. Thompson   @ref Developer
1127a982d89SJeremy L. Thompson **/
113d1d35e2fSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *field_name,
114d1d35e2fSjeremylt                                  CeedInt size, CeedEvalMode eval_mode) {
115d1d35e2fSjeremylt   size_t len = strlen(field_name);
1167a982d89SJeremy L. Thompson   char *tmp;
1177a982d89SJeremy L. Thompson   int ierr;
1187a982d89SJeremy L. Thompson 
119e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, f); CeedChk(ierr);
1207a982d89SJeremy L. Thompson   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
121d1d35e2fSjeremylt   memcpy(tmp, field_name, len+1);
122d1d35e2fSjeremylt   (*f)->field_name = tmp;
1237a982d89SJeremy L. Thompson   (*f)->size = size;
124d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
125e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1267a982d89SJeremy L. Thompson }
1277a982d89SJeremy L. Thompson 
1287a982d89SJeremy L. Thompson /**
1297a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1307a982d89SJeremy L. Thompson 
1317a982d89SJeremy L. Thompson   @param[in] field         QFunction field to view
132d1d35e2fSjeremylt   @param[in] field_number  Number of field being viewed
1334c4400c7SValeria Barra   @param[in] in            true for input field, false for output
1347a982d89SJeremy L. Thompson   @param[in] stream        Stream to view to, e.g., stdout
1357a982d89SJeremy L. Thompson 
1367a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1377a982d89SJeremy L. Thompson 
1387a982d89SJeremy L. Thompson   @ref Utility
1397a982d89SJeremy L. Thompson **/
140d1d35e2fSjeremylt static int CeedQFunctionFieldView(CeedQFunctionField field,
141d1d35e2fSjeremylt                                   CeedInt field_number,
1427a982d89SJeremy L. Thompson                                   bool in, FILE *stream) {
1438229195eSjeremylt   int ierr;
1447a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1458229195eSjeremylt   char *field_name;
1468229195eSjeremylt   ierr = CeedQFunctionFieldGetName(field, &field_name); CeedChk(ierr);
1478229195eSjeremylt   CeedInt size;
1488229195eSjeremylt   ierr = CeedQFunctionFieldGetSize(field, &size); CeedChk(ierr);
1498229195eSjeremylt   CeedEvalMode eval_mode;
1508229195eSjeremylt   ierr = CeedQFunctionFieldGetEvalMode(field, &eval_mode); CeedChk(ierr);
1517a982d89SJeremy L. Thompson   fprintf(stream, "    %s Field [%d]:\n"
1527a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1537a982d89SJeremy L. Thompson           "      Size: %d\n"
1547a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1558229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
156e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1577a982d89SJeremy L. Thompson }
1587a982d89SJeremy L. Thompson 
159777ff853SJeremy L Thompson /**
160777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
161777ff853SJeremy L Thompson 
162777ff853SJeremy L Thompson   @param qf      CeedQFunction
163777ff853SJeremy L Thompson   @param status  Boolean value to set as Fortran status
164777ff853SJeremy L Thompson 
165777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
166777ff853SJeremy L Thompson 
167777ff853SJeremy L Thompson   @ref Backend
168777ff853SJeremy L Thompson **/
169777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
170f04ea552SJeremy L Thompson   qf->is_fortran = status;
171e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
172777ff853SJeremy L Thompson }
173777ff853SJeremy L Thompson 
1747a982d89SJeremy L. Thompson /// @}
1757a982d89SJeremy L. Thompson 
1767a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1777a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1787a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1797a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1807a982d89SJeremy L. Thompson /// @{
1817a982d89SJeremy L. Thompson 
1827a982d89SJeremy L. Thompson /**
1837a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1847a982d89SJeremy L. Thompson 
1857a982d89SJeremy L. Thompson   @param qf               CeedQFunction
186d1d35e2fSjeremylt   @param[out] vec_length  Variable to store vector length
1877a982d89SJeremy L. Thompson 
1887a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1897a982d89SJeremy L. Thompson 
1907a982d89SJeremy L. Thompson   @ref Backend
1917a982d89SJeremy L. Thompson **/
192d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
193d1d35e2fSjeremylt   *vec_length = qf->vec_length;
194e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1957a982d89SJeremy L. Thompson }
1967a982d89SJeremy L. Thompson 
1977a982d89SJeremy L. Thompson /**
1987a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
1997a982d89SJeremy L. Thompson 
2007a982d89SJeremy L. Thompson   @param qf               CeedQFunction
201d1d35e2fSjeremylt   @param[out] num_input   Variable to store number of input fields
202d1d35e2fSjeremylt   @param[out] num_output  Variable to store number of output fields
2037a982d89SJeremy L. Thompson 
2047a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2057a982d89SJeremy L. Thompson 
2067a982d89SJeremy L. Thompson   @ref Backend
2077a982d89SJeremy L. Thompson **/
208d1d35e2fSjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input,
209d1d35e2fSjeremylt                             CeedInt *num_output) {
210d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
211d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
212e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2137a982d89SJeremy L. Thompson }
2147a982d89SJeremy L. Thompson 
2157a982d89SJeremy L. Thompson /**
21643e1b16fSJeremy L Thompson   @brief Get the name of the user function for a CeedQFunction
2177a982d89SJeremy L. Thompson 
2187a982d89SJeremy L. Thompson   @param qf                CeedQFunction
21943e1b16fSJeremy L Thompson   @param[out] kernel_name  Variable to store source path string
2207a982d89SJeremy L. Thompson 
2217a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2227a982d89SJeremy L. Thompson 
2237a982d89SJeremy L. Thompson   @ref Backend
2247a982d89SJeremy L. Thompson **/
22543e1b16fSJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, char **kernel_name) {
22643e1b16fSJeremy L Thompson   *kernel_name = (char *) qf->kernel_name;
22743e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
22843e1b16fSJeremy L Thompson }
22943e1b16fSJeremy L Thompson 
23043e1b16fSJeremy L Thompson /**
23143e1b16fSJeremy L Thompson   @brief Get the source path string for a CeedQFunction
23243e1b16fSJeremy L Thompson 
23343e1b16fSJeremy L Thompson   @param qf                CeedQFunction
23443e1b16fSJeremy L Thompson   @param[out] source_path  Variable to store source path string
23543e1b16fSJeremy L Thompson 
23643e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
23743e1b16fSJeremy L Thompson 
23843e1b16fSJeremy L Thompson   @ref Backend
23943e1b16fSJeremy L Thompson **/
24043e1b16fSJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) {
24143e1b16fSJeremy L Thompson   *source_path = (char *) qf->source_path;
242e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2437a982d89SJeremy L. Thompson }
2447a982d89SJeremy L. Thompson 
2457a982d89SJeremy L. Thompson /**
246*3d3250a0SJeremy L Thompson   @brief Initalize and load QFunction source file into string buffer, including
247*3d3250a0SJeremy L Thompson            full text of local files in place of `#include "local.h"`.
248*3d3250a0SJeremy L Thompson            The `buffer` is set to `NULL` if there is no QFunction source file.
249*3d3250a0SJeremy L Thompson          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
250*3d3250a0SJeremy L Thompson 
251*3d3250a0SJeremy L Thompson   @param qf                     CeedQFunction
252*3d3250a0SJeremy L Thompson   @param[out] buffer            String buffer for source file contents
253*3d3250a0SJeremy L Thompson 
254*3d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
255*3d3250a0SJeremy L Thompson 
256*3d3250a0SJeremy L Thompson   @ref Backend
257*3d3250a0SJeremy L Thompson **/
258*3d3250a0SJeremy L Thompson int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, char **source_buffer) {
259*3d3250a0SJeremy L Thompson   int ierr;
260*3d3250a0SJeremy L Thompson   char *source_path;
261*3d3250a0SJeremy L Thompson 
262*3d3250a0SJeremy L Thompson   ierr = CeedQFunctionGetSourcePath(qf, &source_path); CeedChk(ierr);
263*3d3250a0SJeremy L Thompson   *source_buffer = NULL;
264*3d3250a0SJeremy L Thompson   if (source_path) {
265*3d3250a0SJeremy L Thompson     ierr = CeedLoadSourceToBuffer(qf->ceed, source_path, source_buffer);
266*3d3250a0SJeremy L Thompson     CeedChk(ierr);
267*3d3250a0SJeremy L Thompson   }
268*3d3250a0SJeremy L Thompson 
269*3d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
270*3d3250a0SJeremy L Thompson }
271*3d3250a0SJeremy L Thompson 
272*3d3250a0SJeremy L Thompson /**
2737a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2747a982d89SJeremy L. Thompson 
2757a982d89SJeremy L. Thompson   @param qf      CeedQFunction
2767a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
2777a982d89SJeremy L. Thompson 
2787a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2797a982d89SJeremy L. Thompson 
2807a982d89SJeremy L. Thompson   @ref Backend
2817a982d89SJeremy L. Thompson **/
2827a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
2837a982d89SJeremy L. Thompson   *f = qf->function;
284e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2857a982d89SJeremy L. Thompson }
2867a982d89SJeremy L. Thompson 
2877a982d89SJeremy L. Thompson /**
288777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
289777ff853SJeremy L Thompson            Note: For QFunctions from the Fortran interface, this
290777ff853SJeremy L Thompson              function will return the Fortran context
291777ff853SJeremy L Thompson              CeedQFunctionContext.
2927a982d89SJeremy L. Thompson 
2937a982d89SJeremy L. Thompson   @param qf        CeedQFunction
294777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2957a982d89SJeremy L. Thompson 
2967a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2977a982d89SJeremy L. Thompson 
2987a982d89SJeremy L. Thompson   @ref Backend
2997a982d89SJeremy L. Thompson **/
300777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
3017a982d89SJeremy L. Thompson   *ctx = qf->ctx;
302e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3037a982d89SJeremy L. Thompson }
3047a982d89SJeremy L. Thompson 
3057a982d89SJeremy L. Thompson /**
3067a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
307777ff853SJeremy L Thompson            Note: For all QFunctions this function will return the user
308777ff853SJeremy L Thompson              CeedQFunctionContext and not interface context
309777ff853SJeremy L Thompson              CeedQFunctionContext, if any such object exists.
3107a982d89SJeremy L. Thompson 
3117a982d89SJeremy L. Thompson   @param qf        CeedQFunction
312777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
3137a982d89SJeremy L. Thompson 
3147a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3157a982d89SJeremy L. Thompson   @ref Backend
3167a982d89SJeremy L. Thompson **/
317777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
318777ff853SJeremy L Thompson   int ierr;
319f04ea552SJeremy L Thompson   if (qf->is_fortran) {
320d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
321d1d35e2fSjeremylt     ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx);
322777ff853SJeremy L Thompson     CeedChk(ierr);
323d1d35e2fSjeremylt     *ctx = fortran_ctx->innerctx;
324d1d35e2fSjeremylt     ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx);
325d1d35e2fSjeremylt     CeedChk(ierr);
3267a982d89SJeremy L. Thompson   } else {
3277a982d89SJeremy L. Thompson     *ctx = qf->ctx;
3287a982d89SJeremy L. Thompson   }
329e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3307a982d89SJeremy L. Thompson }
3317a982d89SJeremy L. Thompson 
3327a982d89SJeremy L. Thompson /**
3337a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
3347a982d89SJeremy L. Thompson 
3357a982d89SJeremy L. Thompson   @param qf                CeedQFunction
336d1d35e2fSjeremylt   @param[out] is_identity  Variable to store identity status
3377a982d89SJeremy L. Thompson 
3387a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3397a982d89SJeremy L. Thompson 
3407a982d89SJeremy L. Thompson   @ref Backend
3417a982d89SJeremy L. Thompson **/
342d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
343f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
344e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3457a982d89SJeremy L. Thompson }
3467a982d89SJeremy L. Thompson 
3477a982d89SJeremy L. Thompson /**
3487a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
3497a982d89SJeremy L. Thompson 
3507a982d89SJeremy L. Thompson   @param qf         CeedQFunction
3517a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
3527a982d89SJeremy L. Thompson 
3537a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3547a982d89SJeremy L. Thompson 
3557a982d89SJeremy L. Thompson   @ref Backend
3567a982d89SJeremy L. Thompson **/
357777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
358777ff853SJeremy L Thompson   *(void **)data = qf->data;
359e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3607a982d89SJeremy L. Thompson }
3617a982d89SJeremy L. Thompson 
3627a982d89SJeremy L. Thompson /**
3637a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
3647a982d89SJeremy L. Thompson 
3657a982d89SJeremy L. Thompson   @param[out] qf  CeedQFunction
3667a982d89SJeremy L. Thompson   @param data     Data to set
3677a982d89SJeremy L. Thompson 
3687a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3697a982d89SJeremy L. Thompson 
3707a982d89SJeremy L. Thompson   @ref Backend
3717a982d89SJeremy L. Thompson **/
372777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
373777ff853SJeremy L Thompson   qf->data = data;
374e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3757a982d89SJeremy L. Thompson }
3767a982d89SJeremy L. Thompson 
3777a982d89SJeremy L. Thompson /**
37834359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
37934359f16Sjeremylt 
38034359f16Sjeremylt   @param qf  CeedQFunction to increment the reference counter
38134359f16Sjeremylt 
38234359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
38334359f16Sjeremylt 
38434359f16Sjeremylt   @ref Backend
38534359f16Sjeremylt **/
3869560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
38734359f16Sjeremylt   qf->ref_count++;
38834359f16Sjeremylt   return CEED_ERROR_SUCCESS;
38934359f16Sjeremylt }
39034359f16Sjeremylt 
3917a982d89SJeremy L. Thompson /// @}
3927a982d89SJeremy L. Thompson 
3937a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3947a982d89SJeremy L. Thompson /// CeedQFunction Public API
3957a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3967a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
3977a982d89SJeremy L. Thompson /// @{
3987a982d89SJeremy L. Thompson 
3997a982d89SJeremy L. Thompson /**
4007a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
4017a982d89SJeremy L. Thompson 
4027a982d89SJeremy L. Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
403d1d35e2fSjeremylt   @param vec_length  Vector length. Caller must ensure that number of quadrature
404d1d35e2fSjeremylt                        points is a multiple of vec_length.
4057a982d89SJeremy L. Thompson   @param f           Function pointer to evaluate action at quadrature points.
4067a982d89SJeremy L. Thompson                        See \ref CeedQFunctionUser.
4077a982d89SJeremy L. Thompson   @param source      Absolute path to source of QFunction,
408c8d5b03cSJeremy L Thompson                        "\abs_path\file.h:function_name".
409c8d5b03cSJeremy L Thompson                        For support across all backends, this source must only
410c8d5b03cSJeremy L Thompson                        contain constructs supported by C99, C++11, and CUDA.
4117a982d89SJeremy L. Thompson   @param[out] qf     Address of the variable where the newly created
4127a982d89SJeremy L. Thompson                        CeedQFunction will be stored
4137a982d89SJeremy L. Thompson 
4147a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4157a982d89SJeremy L. Thompson 
4167a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
4177a982d89SJeremy L. Thompson     arguments.
4187a982d89SJeremy L. Thompson 
4197a982d89SJeremy L. Thompson   @ref User
4207a982d89SJeremy L. Thompson **/
421d1d35e2fSjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length,
422d1d35e2fSjeremylt                                 CeedQFunctionUser f,
4237a982d89SJeremy L. Thompson                                 const char *source, CeedQFunction *qf) {
4247a982d89SJeremy L. Thompson   int ierr;
42543e1b16fSJeremy L Thompson   char *source_copy, *kernel_name_copy;
4267a982d89SJeremy L. Thompson 
4277a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
4287a982d89SJeremy L. Thompson     Ceed delegate;
4297a982d89SJeremy L. Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
4307a982d89SJeremy L. Thompson 
4317a982d89SJeremy L. Thompson     if (!delegate)
4327a982d89SJeremy L. Thompson       // LCOV_EXCL_START
433e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
434e15f9bd0SJeremy L Thompson                        "Backend does not support QFunctionCreate");
4357a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
4367a982d89SJeremy L. Thompson 
437d1d35e2fSjeremylt     ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf);
4387a982d89SJeremy L. Thompson     CeedChk(ierr);
439e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
4407a982d89SJeremy L. Thompson   }
4417a982d89SJeremy L. Thompson 
44243e1b16fSJeremy L Thompson   if (strlen(source) && !strrchr(source, ':'))\
44343e1b16fSJeremy L Thompson     // LCOV_EXCL_START
44443e1b16fSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPLETE,
44543e1b16fSJeremy L Thompson                      "Provided path to source does not include function name. "
44643e1b16fSJeremy L Thompson                      "Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"",
44743e1b16fSJeremy L Thompson                      source);
44843e1b16fSJeremy L Thompson   // LCOV_EXCL_STOP
44943e1b16fSJeremy L Thompson 
4507a982d89SJeremy L. Thompson   ierr = CeedCalloc(1, qf); CeedChk(ierr);
4517a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
4529560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
453d1d35e2fSjeremylt   (*qf)->ref_count = 1;
454d1d35e2fSjeremylt   (*qf)->vec_length = vec_length;
455f04ea552SJeremy L Thompson   (*qf)->is_identity = false;
4567a982d89SJeremy L. Thompson   (*qf)->function = f;
45743e1b16fSJeremy L Thompson   if (strlen(source)) {
45843e1b16fSJeremy L Thompson     const char *kernel_name = strrchr(source, ':') + 1;
45943e1b16fSJeremy L Thompson     size_t kernel_name_len = strlen(kernel_name);
46043e1b16fSJeremy L Thompson     ierr = CeedCalloc(kernel_name_len + 1, &kernel_name_copy); CeedChk(ierr);
46143e1b16fSJeremy L Thompson     strncpy(kernel_name_copy, kernel_name, kernel_name_len);
46243e1b16fSJeremy L Thompson     (*qf)->kernel_name = kernel_name_copy;
46343e1b16fSJeremy L Thompson 
46443e1b16fSJeremy L Thompson     size_t source_len = strlen(source) - kernel_name_len - 1;
46543e1b16fSJeremy L Thompson     ierr = CeedCalloc(source_len + 1, &source_copy); CeedChk(ierr);
46643e1b16fSJeremy L Thompson     strncpy(source_copy, source, source_len);
467d1d35e2fSjeremylt     (*qf)->source_path = source_copy;
46843e1b16fSJeremy L Thompson   }
469d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->input_fields); CeedChk(ierr);
470d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->output_fields); CeedChk(ierr);
4717a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
472e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4737a982d89SJeremy L. Thompson }
4747a982d89SJeremy L. Thompson 
4757a982d89SJeremy L. Thompson /**
476288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
477288c0443SJeremy L Thompson 
478288c0443SJeremy L Thompson   @param ceed     A Ceed object where the CeedQFunction will be created
479288c0443SJeremy L Thompson   @param name     Name of QFunction to use from gallery
480288c0443SJeremy L Thompson   @param[out] qf  Address of the variable where the newly created
481288c0443SJeremy L Thompson                     CeedQFunction will be stored
482288c0443SJeremy L Thompson 
483288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
484288c0443SJeremy L Thompson 
4857a982d89SJeremy L. Thompson   @ref User
486288c0443SJeremy L Thompson **/
487288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
488288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
489288c0443SJeremy L Thompson   int ierr;
490d1d35e2fSjeremylt   size_t match_len = 0, match_idx = UINT_MAX;
49175affc3bSjeremylt   char *name_copy;
492288c0443SJeremy L Thompson 
4931d013790SJed Brown   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
494288c0443SJeremy L Thompson   // Find matching backend
495e15f9bd0SJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE,
496e15f9bd0SJeremy L Thompson                                 "No QFunction name provided");
497288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
498288c0443SJeremy L Thompson     size_t n;
499d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
500d1d35e2fSjeremylt     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {}
501d1d35e2fSjeremylt     if (n > match_len) {
502d1d35e2fSjeremylt       match_len = n;
503d1d35e2fSjeremylt       match_idx = i;
504288c0443SJeremy L Thompson     }
505288c0443SJeremy L Thompson   }
506d1d35e2fSjeremylt   if (!match_len)
507138d4072Sjeremylt     // LCOV_EXCL_START
508e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
509138d4072Sjeremylt   // LCOV_EXCL_STOP
510288c0443SJeremy L Thompson 
511288c0443SJeremy L Thompson   // Create QFunction
512d1d35e2fSjeremylt   ierr = CeedQFunctionCreateInterior(ceed,
513d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].vec_length,
514d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].f,
515d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].source, qf);
516288c0443SJeremy L Thompson   CeedChk(ierr);
517288c0443SJeremy L Thompson 
518288c0443SJeremy L Thompson   // QFunction specific setup
519d1d35e2fSjeremylt   ierr = gallery_qfunctions[match_idx].init(ceed, name, *qf); CeedChk(ierr);
520288c0443SJeremy L Thompson 
52175affc3bSjeremylt   // Copy name
52275affc3bSjeremylt   size_t slen = strlen(name) + 1;
52375affc3bSjeremylt   ierr = CeedMalloc(slen, &name_copy); CeedChk(ierr);
52475affc3bSjeremylt   memcpy(name_copy, name, slen);
52543e1b16fSJeremy L Thompson   (*qf)->gallery_name = name_copy;
52643e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
527e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
528288c0443SJeremy L Thompson }
529288c0443SJeremy L Thompson 
530288c0443SJeremy L Thompson /**
5310219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
5320219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
5330219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
5340219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
5350219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
5360219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
5370219ea01SJeremy L Thompson            location for both.
5380219ea01SJeremy L Thompson 
5390219ea01SJeremy L Thompson   @param ceed          A Ceed object where the CeedQFunction will be created
540d1d35e2fSjeremylt   @param[in] size      Size of the QFunction fields
541d1d35e2fSjeremylt   @param[in] in_mode   CeedEvalMode for input to CeedQFunction
542d1d35e2fSjeremylt   @param[in] out_mode  CeedEvalMode for output to CeedQFunction
5430219ea01SJeremy L Thompson   @param[out] qf       Address of the variable where the newly created
5440219ea01SJeremy L Thompson                          CeedQFunction will be stored
5450219ea01SJeremy L Thompson 
5460219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5470219ea01SJeremy L Thompson 
5487a982d89SJeremy L. Thompson   @ref User
5490219ea01SJeremy L Thompson **/
550d1d35e2fSjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode,
551d1d35e2fSjeremylt                                 CeedEvalMode out_mode, CeedQFunction *qf) {
5520219ea01SJeremy L Thompson   int ierr;
5530219ea01SJeremy L Thompson 
5540219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
555d1d35e2fSjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr);
556d1d35e2fSjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr);
5570219ea01SJeremy L Thompson 
558f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
559d1d35e2fSjeremylt   CeedInt *size_data;
560d1d35e2fSjeremylt   ierr = CeedCalloc(1, &size_data); CeedChk(ierr);
561d1d35e2fSjeremylt   size_data[0] = size;
562777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
563777ff853SJeremy L Thompson   ierr = CeedQFunctionContextCreate(ceed, &ctx); CeedChk(ierr);
564777ff853SJeremy L Thompson   ierr = CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_OWN_POINTER,
565d1d35e2fSjeremylt                                      sizeof(*size_data), (void *)size_data);
566777ff853SJeremy L Thompson   CeedChk(ierr);
567777ff853SJeremy L Thompson   ierr = CeedQFunctionSetContext(*qf, ctx); CeedChk(ierr);
568777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&ctx); CeedChk(ierr);
569e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5700219ea01SJeremy L Thompson }
5710219ea01SJeremy L Thompson 
5720219ea01SJeremy L Thompson /**
5739560d06aSjeremylt   @brief Copy the pointer to a CeedQFunction. Both pointers should
5749560d06aSjeremylt            be destroyed with `CeedQFunctionDestroy()`;
5759560d06aSjeremylt            Note: If `*qf_copy` is non-NULL, then it is assumed that
5769560d06aSjeremylt            `*qf_copy` is a pointer to a CeedQFunction. This
5779560d06aSjeremylt            CeedQFunction will be destroyed if `*qf_copy` is the only
5789560d06aSjeremylt            reference to this CeedQFunction.
5799560d06aSjeremylt 
5809560d06aSjeremylt   @param qf            CeedQFunction to copy reference to
5819560d06aSjeremylt   @param[out] qf_copy  Variable to store copied reference
5829560d06aSjeremylt 
5839560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
5849560d06aSjeremylt 
5859560d06aSjeremylt   @ref User
5869560d06aSjeremylt **/
5879560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
5889560d06aSjeremylt   int ierr;
5899560d06aSjeremylt 
5909560d06aSjeremylt   ierr = CeedQFunctionReference(qf); CeedChk(ierr);
5919560d06aSjeremylt   ierr = CeedQFunctionDestroy(qf_copy); CeedChk(ierr);
5929560d06aSjeremylt   *qf_copy = qf;
5939560d06aSjeremylt   return CEED_ERROR_SUCCESS;
5949560d06aSjeremylt }
5959560d06aSjeremylt 
5969560d06aSjeremylt /**
597a0a97fcfSJed Brown   @brief Add a CeedQFunction input
598b11c1e72Sjeremylt 
599b11c1e72Sjeremylt   @param qf          CeedQFunction
600d1d35e2fSjeremylt   @param field_name  Name of QFunction field
601d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
602d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
603d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
604b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
605b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
606b11c1e72Sjeremylt 
607b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
608dfdf5a53Sjeremylt 
6097a982d89SJeremy L. Thompson   @ref User
610b11c1e72Sjeremylt **/
611d1d35e2fSjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name,
612d1d35e2fSjeremylt                           CeedInt size,
613d1d35e2fSjeremylt                           CeedEvalMode eval_mode) {
614f04ea552SJeremy L Thompson   if (qf->is_immutable)
615e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
616e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
617f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
618e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
619d1d35e2fSjeremylt   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1))
620e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
621e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
622e15f9bd0SJeremy L Thompson                      "CEED_EVAL_WEIGHT should have size 1");
623e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
624d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields],
625d1d35e2fSjeremylt                                    field_name, size, eval_mode);
626fe2413ffSjeremylt   CeedChk(ierr);
627d1d35e2fSjeremylt   qf->num_input_fields++;
628e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
629d7b241e6Sjeremylt }
630d7b241e6Sjeremylt 
631b11c1e72Sjeremylt /**
632a0a97fcfSJed Brown   @brief Add a CeedQFunction output
633b11c1e72Sjeremylt 
634b11c1e72Sjeremylt   @param qf          CeedQFunction
635d1d35e2fSjeremylt   @param field_name  Name of QFunction field
636d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
637d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
638d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
639b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
640b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
641b11c1e72Sjeremylt 
642b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
643dfdf5a53Sjeremylt 
6447a982d89SJeremy L. Thompson   @ref User
645b11c1e72Sjeremylt **/
646d1d35e2fSjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name,
647d1d35e2fSjeremylt                            CeedInt size, CeedEvalMode eval_mode) {
648f04ea552SJeremy L Thompson   if (qf->is_immutable)
649e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
650e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
651f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
652e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
653d1d35e2fSjeremylt   if (eval_mode == CEED_EVAL_WEIGHT)
654c042f62fSJeremy L Thompson     // LCOV_EXCL_START
655e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
656e15f9bd0SJeremy L Thompson                      "Cannot create QFunction output with "
6571d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
658c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
659d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields],
660d1d35e2fSjeremylt                                    field_name, size, eval_mode);
661fe2413ffSjeremylt   CeedChk(ierr);
662d1d35e2fSjeremylt   qf->num_output_fields++;
663e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
664d7b241e6Sjeremylt }
665d7b241e6Sjeremylt 
666dfdf5a53Sjeremylt /**
66743bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
66843bbe138SJeremy L Thompson 
669f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
670f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
671f04ea552SJeremy L Thompson 
67243bbe138SJeremy L Thompson   @param qf                  CeedQFunction
67343bbe138SJeremy L Thompson   @param[out] input_fields   Variable to store input_fields
67443bbe138SJeremy L Thompson   @param[out] output_fields  Variable to store output_fields
67543bbe138SJeremy L Thompson 
67643bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
67743bbe138SJeremy L Thompson 
678e9b533fbSJeremy L Thompson   @ref Advanced
67943bbe138SJeremy L Thompson **/
68043bbe138SJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields,
68143bbe138SJeremy L Thompson                            CeedQFunctionField **input_fields,
68243bbe138SJeremy L Thompson                            CeedInt *num_output_fields,
68343bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
684f04ea552SJeremy L Thompson   qf->is_immutable = true;
68543bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
68643bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
68743bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
68843bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
68943bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
69043bbe138SJeremy L Thompson }
69143bbe138SJeremy L Thompson 
69243bbe138SJeremy L Thompson /**
69343bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
69443bbe138SJeremy L Thompson 
69543bbe138SJeremy L Thompson   @param qf_field         CeedQFunctionField
69643bbe138SJeremy L Thompson   @param[out] field_name  Variable to store the field name
69743bbe138SJeremy L Thompson 
69843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
69943bbe138SJeremy L Thompson 
700e9b533fbSJeremy L Thompson   @ref Advanced
70143bbe138SJeremy L Thompson **/
70243bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
70343bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
70443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
70543bbe138SJeremy L Thompson }
70643bbe138SJeremy L Thompson 
70743bbe138SJeremy L Thompson /**
70843bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
70943bbe138SJeremy L Thompson 
71043bbe138SJeremy L Thompson   @param qf_field   CeedQFunctionField
71143bbe138SJeremy L Thompson   @param[out] size  Variable to store the size of the field
71243bbe138SJeremy L Thompson 
71343bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
71443bbe138SJeremy L Thompson 
715e9b533fbSJeremy L Thompson   @ref Advanced
71643bbe138SJeremy L Thompson **/
71743bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
71843bbe138SJeremy L Thompson   *size = qf_field->size;
71943bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
72043bbe138SJeremy L Thompson }
72143bbe138SJeremy L Thompson 
72243bbe138SJeremy L Thompson /**
72343bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
72443bbe138SJeremy L Thompson 
72543bbe138SJeremy L Thompson   @param qf_field        CeedQFunctionField
72643bbe138SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
72743bbe138SJeremy L Thompson 
72843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
72943bbe138SJeremy L Thompson 
730e9b533fbSJeremy L Thompson   @ref Advanced
73143bbe138SJeremy L Thompson **/
73243bbe138SJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field,
73343bbe138SJeremy L Thompson                                   CeedEvalMode *eval_mode) {
73443bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
73543bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
73643bbe138SJeremy L Thompson }
73743bbe138SJeremy L Thompson 
73843bbe138SJeremy L Thompson /**
7394ce2993fSjeremylt   @brief Set global context for a CeedQFunction
740b11c1e72Sjeremylt 
741b11c1e72Sjeremylt   @param qf   CeedQFunction
742b11c1e72Sjeremylt   @param ctx  Context data to set
743b11c1e72Sjeremylt 
744b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
745dfdf5a53Sjeremylt 
7467a982d89SJeremy L. Thompson   @ref User
747b11c1e72Sjeremylt **/
748777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
74934359f16Sjeremylt   int ierr;
750d7b241e6Sjeremylt   qf->ctx = ctx;
7519560d06aSjeremylt   ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr);
752e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
753d7b241e6Sjeremylt }
754d7b241e6Sjeremylt 
755b11c1e72Sjeremylt /**
75675affc3bSjeremylt   @brief View a CeedQFunction
75775affc3bSjeremylt 
75875affc3bSjeremylt   @param[in] qf      CeedQFunction to view
75975affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
76075affc3bSjeremylt 
76175affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
76275affc3bSjeremylt 
7637a982d89SJeremy L. Thompson   @ref User
76475affc3bSjeremylt **/
76575affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
76675affc3bSjeremylt   int ierr;
76775affc3bSjeremylt 
76875affc3bSjeremylt   fprintf(stream, "%sCeedQFunction %s\n",
76943e1b16fSJeremy L Thompson           qf->is_gallery ? "Gallery " : "User ",
77043e1b16fSJeremy L Thompson           qf->is_gallery ? qf->gallery_name : qf->kernel_name);
77175affc3bSjeremylt 
772d1d35e2fSjeremylt   fprintf(stream, "  %d Input Field%s:\n", qf->num_input_fields,
773d1d35e2fSjeremylt           qf->num_input_fields>1 ? "s" : "");
774d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_input_fields; i++) {
775d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream);
7762da88da5Sjeremylt     CeedChk(ierr);
77775affc3bSjeremylt   }
77875affc3bSjeremylt 
779d1d35e2fSjeremylt   fprintf(stream, "  %d Output Field%s:\n", qf->num_output_fields,
780d1d35e2fSjeremylt           qf->num_output_fields>1 ? "s" : "");
781d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_output_fields; i++) {
782d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream);
78375affc3bSjeremylt     CeedChk(ierr);
78475affc3bSjeremylt   }
785e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
78675affc3bSjeremylt }
78775affc3bSjeremylt 
78875affc3bSjeremylt /**
789b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunction
790b7c9bbdaSJeremy L Thompson 
791b7c9bbdaSJeremy L Thompson   @param qf              CeedQFunction
792b7c9bbdaSJeremy L Thompson   @param[out] ceed       Variable to store Ceed
793b7c9bbdaSJeremy L Thompson 
794b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
795b7c9bbdaSJeremy L Thompson 
796b7c9bbdaSJeremy L Thompson   @ref Advanced
797b7c9bbdaSJeremy L Thompson **/
798b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
799b7c9bbdaSJeremy L Thompson   *ceed = qf->ceed;
800b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
801b7c9bbdaSJeremy L Thompson }
802b7c9bbdaSJeremy L Thompson 
803b7c9bbdaSJeremy L Thompson /**
804b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
805b11c1e72Sjeremylt 
806f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
807f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
808f04ea552SJeremy L Thompson 
809b11c1e72Sjeremylt   @param qf      CeedQFunction
810b11c1e72Sjeremylt   @param Q       Number of quadrature points
81134138859Sjeremylt   @param[in] u   Array of input CeedVectors
81234138859Sjeremylt   @param[out] v  Array of output CeedVectors
813b11c1e72Sjeremylt 
814b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
815dfdf5a53Sjeremylt 
8167a982d89SJeremy L. Thompson   @ref User
817b11c1e72Sjeremylt **/
818d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
819aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
820d7b241e6Sjeremylt   int ierr;
821d7b241e6Sjeremylt   if (!qf->Apply)
822c042f62fSJeremy L Thompson     // LCOV_EXCL_START
823e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED,
824e15f9bd0SJeremy L Thompson                      "Backend does not support QFunctionApply");
825c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
826d1d35e2fSjeremylt   if (Q % qf->vec_length)
827c042f62fSJeremy L Thompson     // LCOV_EXCL_START
828e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
829e15f9bd0SJeremy L Thompson                      "Number of quadrature points %d must be a "
830d1d35e2fSjeremylt                      "multiple of %d", Q, qf->vec_length);
831c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
832f04ea552SJeremy L Thompson   qf->is_immutable = true;
833d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
834e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
835d7b241e6Sjeremylt }
836d7b241e6Sjeremylt 
837b11c1e72Sjeremylt /**
838b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
839b11c1e72Sjeremylt 
840b11c1e72Sjeremylt   @param qf  CeedQFunction to destroy
841b11c1e72Sjeremylt 
842b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
843dfdf5a53Sjeremylt 
8447a982d89SJeremy L. Thompson   @ref User
845b11c1e72Sjeremylt **/
846d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
847d7b241e6Sjeremylt   int ierr;
848d7b241e6Sjeremylt 
849d1d35e2fSjeremylt   if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS;
850fe2413ffSjeremylt   // Backend destroy
851d7b241e6Sjeremylt   if ((*qf)->Destroy) {
852d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
853d7b241e6Sjeremylt   }
854fe2413ffSjeremylt   // Free fields
855d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_input_fields; i++) {
856d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr);
857d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr);
858fe2413ffSjeremylt   }
859d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_output_fields; i++) {
860d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr);
861d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr);
862fe2413ffSjeremylt   }
863d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr);
864d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr);
865777ff853SJeremy L Thompson 
866777ff853SJeremy L Thompson   // User context data object
867777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
868fe2413ffSjeremylt 
869d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr);
87043e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->gallery_name); CeedChk(ierr);
87143e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->kernel_name); CeedChk(ierr);
872d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
873d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
874e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
875d7b241e6Sjeremylt }
876d7b241e6Sjeremylt 
877d7b241e6Sjeremylt /// @}
878