xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision e9b533fb9793b92fee16c1660fbf6414c678b8a7)
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>
193d576824SJeremy L Thompson #include <ceed-impl.h>
20288c0443SJeremy L Thompson #include <limits.h>
213d576824SJeremy L Thompson #include <stdbool.h>
223d576824SJeremy L Thompson #include <stdio.h>
233d576824SJeremy L Thompson #include <string.h>
24288c0443SJeremy L Thompson 
257a982d89SJeremy L. Thompson /// @file
267a982d89SJeremy L. Thompson /// Implementation of public CeedQFunction interfaces
277a982d89SJeremy L. Thompson 
28288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP
29442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none;
30442e7f0bSjeremylt /// @endcond
31442e7f0bSjeremylt 
327a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
337a982d89SJeremy L. Thompson /// @{
347a982d89SJeremy L. Thompson 
357a982d89SJeremy L. Thompson // Indicate that no QFunction is provided by the user
367a982d89SJeremy L. Thompson const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
377a982d89SJeremy L. Thompson 
387a982d89SJeremy L. Thompson /// @}
397a982d89SJeremy L. Thompson 
40442e7f0bSjeremylt /// @cond DOXYGEN_SKIP
41288c0443SJeremy L Thompson static struct {
42288c0443SJeremy L Thompson   char name[CEED_MAX_RESOURCE_LEN];
43288c0443SJeremy L Thompson   char source[CEED_MAX_RESOURCE_LEN];
44d1d35e2fSjeremylt   CeedInt vec_length;
45288c0443SJeremy L Thompson   CeedQFunctionUser f;
46288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
47d1d35e2fSjeremylt } gallery_qfunctions[1024];
48288c0443SJeremy L Thompson static size_t num_qfunctions;
49288c0443SJeremy L Thompson /// @endcond
50d7b241e6Sjeremylt 
51777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
52777ff853SJeremy L Thompson /// CeedQFunction Library Internal Functions
53777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
54777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper
55777ff853SJeremy L Thompson /// @{
56777ff853SJeremy L Thompson 
57b11c1e72Sjeremylt /**
58288c0443SJeremy L Thompson   @brief Register a gallery QFunction
59288c0443SJeremy L Thompson 
60288c0443SJeremy L Thompson   @param name        Name for this backend to respond to
611176cc3aSJeremy L Thompson   @param source      Absolute path to source of QFunction,
621176cc3aSJeremy L Thompson                        "\path\CEED_DIR\gallery\folder\file.h:function_name"
63d1d35e2fSjeremylt   @param vec_length  Vector length.  Caller must ensure that number of quadrature
64d1d35e2fSjeremylt                        points is a multiple of vec_length.
65288c0443SJeremy L Thompson   @param f           Function pointer to evaluate action at quadrature points.
66288c0443SJeremy L Thompson                        See \ref CeedQFunctionUser.
67288c0443SJeremy L Thompson   @param init        Initialization function called by CeedQFunctionInit() when the
68288c0443SJeremy L Thompson                        QFunction is selected.
69288c0443SJeremy L Thompson 
70288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
71288c0443SJeremy L Thompson 
727a982d89SJeremy L. Thompson   @ref Developer
73288c0443SJeremy L Thompson **/
74288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source,
75d1d35e2fSjeremylt                           CeedInt vec_length, CeedQFunctionUser f,
76288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
77d1d35e2fSjeremylt   if (num_qfunctions >= sizeof(gallery_qfunctions) / sizeof(
78d1d35e2fSjeremylt         gallery_qfunctions[0]))
79c042f62fSJeremy L Thompson     // LCOV_EXCL_START
80e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
81c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
82c042f62fSJeremy 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;
85d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].source, source,
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) {
113d1d35e2fSjeremylt   size_t len = strlen(field_name);
1147a982d89SJeremy L. Thompson   char *tmp;
1157a982d89SJeremy L. Thompson   int ierr;
1167a982d89SJeremy L. Thompson 
117e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, f); CeedChk(ierr);
1187a982d89SJeremy L. Thompson   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
119d1d35e2fSjeremylt   memcpy(tmp, field_name, len+1);
120d1d35e2fSjeremylt   (*f)->field_name = tmp;
1217a982d89SJeremy L. Thompson   (*f)->size = size;
122d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
123e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1247a982d89SJeremy L. Thompson }
1257a982d89SJeremy L. Thompson 
1267a982d89SJeremy L. Thompson /**
1277a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1287a982d89SJeremy L. Thompson 
1297a982d89SJeremy L. Thompson   @param[in] field         QFunction field to view
130d1d35e2fSjeremylt   @param[in] field_number  Number of field being viewed
1314c4400c7SValeria Barra   @param[in] in            true for input field, false for output
1327a982d89SJeremy L. Thompson   @param[in] stream        Stream to view to, e.g., stdout
1337a982d89SJeremy L. Thompson 
1347a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1357a982d89SJeremy L. Thompson 
1367a982d89SJeremy L. Thompson   @ref Utility
1377a982d89SJeremy L. Thompson **/
138d1d35e2fSjeremylt static int CeedQFunctionFieldView(CeedQFunctionField field,
139d1d35e2fSjeremylt                                   CeedInt field_number,
1407a982d89SJeremy L. Thompson                                   bool in, FILE *stream) {
1418229195eSjeremylt   int ierr;
1427a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1438229195eSjeremylt   char *field_name;
1448229195eSjeremylt   ierr = CeedQFunctionFieldGetName(field, &field_name); CeedChk(ierr);
1458229195eSjeremylt   CeedInt size;
1468229195eSjeremylt   ierr = CeedQFunctionFieldGetSize(field, &size); CeedChk(ierr);
1478229195eSjeremylt   CeedEvalMode eval_mode;
1488229195eSjeremylt   ierr = CeedQFunctionFieldGetEvalMode(field, &eval_mode); CeedChk(ierr);
1497a982d89SJeremy L. Thompson   fprintf(stream, "    %s Field [%d]:\n"
1507a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1517a982d89SJeremy L. Thompson           "      Size: %d\n"
1527a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1538229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
154e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1557a982d89SJeremy L. Thompson }
1567a982d89SJeremy L. Thompson 
157777ff853SJeremy L Thompson /**
158777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
159777ff853SJeremy L Thompson 
160777ff853SJeremy L Thompson   @param qf      CeedQFunction
161777ff853SJeremy L Thompson   @param status  Boolean value to set as Fortran status
162777ff853SJeremy L Thompson 
163777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
164777ff853SJeremy L Thompson 
165777ff853SJeremy L Thompson   @ref Backend
166777ff853SJeremy L Thompson **/
167777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
168f04ea552SJeremy L Thompson   qf->is_fortran = status;
169e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
170777ff853SJeremy L Thompson }
171777ff853SJeremy L Thompson 
1727a982d89SJeremy L. Thompson /// @}
1737a982d89SJeremy L. Thompson 
1747a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1757a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1767a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1777a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1787a982d89SJeremy L. Thompson /// @{
1797a982d89SJeremy L. Thompson 
1807a982d89SJeremy L. Thompson /**
1817a982d89SJeremy L. Thompson   @brief Get the Ceed associated with a CeedQFunction
1827a982d89SJeremy L. Thompson 
1837a982d89SJeremy L. Thompson   @param qf              CeedQFunction
1847a982d89SJeremy L. Thompson   @param[out] ceed       Variable to store Ceed
1857a982d89SJeremy L. Thompson 
1867a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1877a982d89SJeremy L. Thompson 
1887a982d89SJeremy L. Thompson   @ref Backend
1897a982d89SJeremy L. Thompson **/
1907a982d89SJeremy L. Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
1917a982d89SJeremy L. Thompson   *ceed = qf->ceed;
192e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1937a982d89SJeremy L. Thompson }
1947a982d89SJeremy L. Thompson 
1957a982d89SJeremy L. Thompson /**
1967a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1977a982d89SJeremy L. Thompson 
1987a982d89SJeremy L. Thompson   @param qf               CeedQFunction
199d1d35e2fSjeremylt   @param[out] vec_length  Variable to store vector length
2007a982d89SJeremy L. Thompson 
2017a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2027a982d89SJeremy L. Thompson 
2037a982d89SJeremy L. Thompson   @ref Backend
2047a982d89SJeremy L. Thompson **/
205d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
206d1d35e2fSjeremylt   *vec_length = qf->vec_length;
207e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2087a982d89SJeremy L. Thompson }
2097a982d89SJeremy L. Thompson 
2107a982d89SJeremy L. Thompson /**
2117a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
2127a982d89SJeremy L. Thompson 
2137a982d89SJeremy L. Thompson   @param qf               CeedQFunction
214d1d35e2fSjeremylt   @param[out] num_input   Variable to store number of input fields
215d1d35e2fSjeremylt   @param[out] num_output  Variable to store number of output fields
2167a982d89SJeremy L. Thompson 
2177a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2187a982d89SJeremy L. Thompson 
2197a982d89SJeremy L. Thompson   @ref Backend
2207a982d89SJeremy L. Thompson **/
221d1d35e2fSjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input,
222d1d35e2fSjeremylt                             CeedInt *num_output) {
223d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
224d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
225e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2267a982d89SJeremy L. Thompson }
2277a982d89SJeremy L. Thompson 
2287a982d89SJeremy L. Thompson /**
2297a982d89SJeremy L. Thompson   @brief Get the source path string for a CeedQFunction
2307a982d89SJeremy L. Thompson 
2317a982d89SJeremy L. Thompson   @param qf           CeedQFunction
2327a982d89SJeremy L. Thompson   @param[out] source  Variable to store source path string
2337a982d89SJeremy L. Thompson 
2347a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2357a982d89SJeremy L. Thompson 
2367a982d89SJeremy L. Thompson   @ref Backend
2377a982d89SJeremy L. Thompson **/
2387a982d89SJeremy L. Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source) {
239d1d35e2fSjeremylt   *source = (char *) qf->source_path;
240e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2417a982d89SJeremy L. Thompson }
2427a982d89SJeremy L. Thompson 
2437a982d89SJeremy L. Thompson /**
2447a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2457a982d89SJeremy L. Thompson 
2467a982d89SJeremy L. Thompson   @param qf      CeedQFunction
2477a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
2487a982d89SJeremy L. Thompson 
2497a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2507a982d89SJeremy L. Thompson 
2517a982d89SJeremy L. Thompson   @ref Backend
2527a982d89SJeremy L. Thompson **/
2537a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
2547a982d89SJeremy L. Thompson   *f = qf->function;
255e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2567a982d89SJeremy L. Thompson }
2577a982d89SJeremy L. Thompson 
2587a982d89SJeremy L. Thompson /**
259777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
260777ff853SJeremy L Thompson            Note: For QFunctions from the Fortran interface, this
261777ff853SJeremy L Thompson              function will return the Fortran context
262777ff853SJeremy L Thompson              CeedQFunctionContext.
2637a982d89SJeremy L. Thompson 
2647a982d89SJeremy L. Thompson   @param qf        CeedQFunction
265777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2667a982d89SJeremy L. Thompson 
2677a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2687a982d89SJeremy L. Thompson 
2697a982d89SJeremy L. Thompson   @ref Backend
2707a982d89SJeremy L. Thompson **/
271777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
2727a982d89SJeremy L. Thompson   *ctx = qf->ctx;
273e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2747a982d89SJeremy L. Thompson }
2757a982d89SJeremy L. Thompson 
2767a982d89SJeremy L. Thompson /**
2777a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
278777ff853SJeremy L Thompson            Note: For all QFunctions this function will return the user
279777ff853SJeremy L Thompson              CeedQFunctionContext and not interface context
280777ff853SJeremy L Thompson              CeedQFunctionContext, if any such object exists.
2817a982d89SJeremy L. Thompson 
2827a982d89SJeremy L. Thompson   @param qf        CeedQFunction
283777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2847a982d89SJeremy L. Thompson 
2857a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2867a982d89SJeremy L. Thompson   @ref Backend
2877a982d89SJeremy L. Thompson **/
288777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
289777ff853SJeremy L Thompson   int ierr;
290f04ea552SJeremy L Thompson   if (qf->is_fortran) {
291d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
292d1d35e2fSjeremylt     ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx);
293777ff853SJeremy L Thompson     CeedChk(ierr);
294d1d35e2fSjeremylt     *ctx = fortran_ctx->innerctx;
295d1d35e2fSjeremylt     ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx);
296d1d35e2fSjeremylt     CeedChk(ierr);
2977a982d89SJeremy L. Thompson   } else {
2987a982d89SJeremy L. Thompson     *ctx = qf->ctx;
2997a982d89SJeremy L. Thompson   }
300e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3017a982d89SJeremy L. Thompson }
3027a982d89SJeremy L. Thompson 
3037a982d89SJeremy L. Thompson /**
3047a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
3057a982d89SJeremy L. Thompson 
3067a982d89SJeremy L. Thompson   @param qf                CeedQFunction
307d1d35e2fSjeremylt   @param[out] is_identity  Variable to store identity status
3087a982d89SJeremy L. Thompson 
3097a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3107a982d89SJeremy L. Thompson 
3117a982d89SJeremy L. Thompson   @ref Backend
3127a982d89SJeremy L. Thompson **/
313d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
314f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
315e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3167a982d89SJeremy L. Thompson }
3177a982d89SJeremy L. Thompson 
3187a982d89SJeremy L. Thompson /**
3197a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
3207a982d89SJeremy L. Thompson 
3217a982d89SJeremy L. Thompson   @param qf         CeedQFunction
3227a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
3237a982d89SJeremy L. Thompson 
3247a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3257a982d89SJeremy L. Thompson 
3267a982d89SJeremy L. Thompson   @ref Backend
3277a982d89SJeremy L. Thompson **/
328777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
329777ff853SJeremy L Thompson   *(void **)data = qf->data;
330e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3317a982d89SJeremy L. Thompson }
3327a982d89SJeremy L. Thompson 
3337a982d89SJeremy L. Thompson /**
3347a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
3357a982d89SJeremy L. Thompson 
3367a982d89SJeremy L. Thompson   @param[out] qf  CeedQFunction
3377a982d89SJeremy L. Thompson   @param data     Data to set
3387a982d89SJeremy L. Thompson 
3397a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3407a982d89SJeremy L. Thompson 
3417a982d89SJeremy L. Thompson   @ref Backend
3427a982d89SJeremy L. Thompson **/
343777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
344777ff853SJeremy L Thompson   qf->data = data;
345e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3467a982d89SJeremy L. Thompson }
3477a982d89SJeremy L. Thompson 
3487a982d89SJeremy L. Thompson /**
34934359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
35034359f16Sjeremylt 
35134359f16Sjeremylt   @param qf  CeedQFunction to increment the reference counter
35234359f16Sjeremylt 
35334359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
35434359f16Sjeremylt 
35534359f16Sjeremylt   @ref Backend
35634359f16Sjeremylt **/
3579560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
35834359f16Sjeremylt   qf->ref_count++;
35934359f16Sjeremylt   return CEED_ERROR_SUCCESS;
36034359f16Sjeremylt }
36134359f16Sjeremylt 
3627a982d89SJeremy L. Thompson /// @}
3637a982d89SJeremy L. Thompson 
3647a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3657a982d89SJeremy L. Thompson /// CeedQFunction Public API
3667a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3677a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
3687a982d89SJeremy L. Thompson /// @{
3697a982d89SJeremy L. Thompson 
3707a982d89SJeremy L. Thompson /**
3717a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
3727a982d89SJeremy L. Thompson 
3737a982d89SJeremy L. Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
374d1d35e2fSjeremylt   @param vec_length  Vector length. Caller must ensure that number of quadrature
375d1d35e2fSjeremylt                        points is a multiple of vec_length.
3767a982d89SJeremy L. Thompson   @param f           Function pointer to evaluate action at quadrature points.
3777a982d89SJeremy L. Thompson                        See \ref CeedQFunctionUser.
3787a982d89SJeremy L. Thompson   @param source      Absolute path to source of QFunction,
379c8d5b03cSJeremy L Thompson                        "\abs_path\file.h:function_name".
380c8d5b03cSJeremy L Thompson                        For support across all backends, this source must only
381c8d5b03cSJeremy L Thompson                        contain constructs supported by C99, C++11, and CUDA.
3827a982d89SJeremy L. Thompson   @param[out] qf     Address of the variable where the newly created
3837a982d89SJeremy L. Thompson                        CeedQFunction will be stored
3847a982d89SJeremy L. Thompson 
3857a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3867a982d89SJeremy L. Thompson 
3877a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
3887a982d89SJeremy L. Thompson     arguments.
3897a982d89SJeremy L. Thompson 
3907a982d89SJeremy L. Thompson   @ref User
3917a982d89SJeremy L. Thompson **/
392d1d35e2fSjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length,
393d1d35e2fSjeremylt                                 CeedQFunctionUser f,
3947a982d89SJeremy L. Thompson                                 const char *source, CeedQFunction *qf) {
3957a982d89SJeremy L. Thompson   int ierr;
3967a982d89SJeremy L. Thompson   char *source_copy;
3977a982d89SJeremy L. Thompson 
3987a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
3997a982d89SJeremy L. Thompson     Ceed delegate;
4007a982d89SJeremy L. Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
4017a982d89SJeremy L. Thompson 
4027a982d89SJeremy L. Thompson     if (!delegate)
4037a982d89SJeremy L. Thompson       // LCOV_EXCL_START
404e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
405e15f9bd0SJeremy L Thompson                        "Backend does not support QFunctionCreate");
4067a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
4077a982d89SJeremy L. Thompson 
408d1d35e2fSjeremylt     ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf);
4097a982d89SJeremy L. Thompson     CeedChk(ierr);
410e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
4117a982d89SJeremy L. Thompson   }
4127a982d89SJeremy L. Thompson 
4137a982d89SJeremy L. Thompson   ierr = CeedCalloc(1, qf); CeedChk(ierr);
4147a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
4159560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
416d1d35e2fSjeremylt   (*qf)->ref_count = 1;
417d1d35e2fSjeremylt   (*qf)->vec_length = vec_length;
418f04ea552SJeremy L Thompson   (*qf)->is_identity = false;
4197a982d89SJeremy L. Thompson   (*qf)->function = f;
4207a982d89SJeremy L. Thompson   size_t slen = strlen(source) + 1;
4217a982d89SJeremy L. Thompson   ierr = CeedMalloc(slen, &source_copy); CeedChk(ierr);
4227a982d89SJeremy L. Thompson   memcpy(source_copy, source, slen);
423d1d35e2fSjeremylt   (*qf)->source_path = source_copy;
424d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->input_fields); CeedChk(ierr);
425d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->output_fields); CeedChk(ierr);
4267a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
427e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4287a982d89SJeremy L. Thompson }
4297a982d89SJeremy L. Thompson 
4307a982d89SJeremy L. Thompson /**
431288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
432288c0443SJeremy L Thompson 
433288c0443SJeremy L Thompson   @param ceed     A Ceed object where the CeedQFunction will be created
434288c0443SJeremy L Thompson   @param name     Name of QFunction to use from gallery
435288c0443SJeremy L Thompson   @param[out] qf  Address of the variable where the newly created
436288c0443SJeremy L Thompson                     CeedQFunction will be stored
437288c0443SJeremy L Thompson 
438288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
439288c0443SJeremy L Thompson 
4407a982d89SJeremy L. Thompson   @ref User
441288c0443SJeremy L Thompson **/
442288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
443288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
444288c0443SJeremy L Thompson   int ierr;
445d1d35e2fSjeremylt   size_t match_len = 0, match_idx = UINT_MAX;
44675affc3bSjeremylt   char *name_copy;
447288c0443SJeremy L Thompson 
4481d013790SJed Brown   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
449288c0443SJeremy L Thompson   // Find matching backend
450e15f9bd0SJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE,
451e15f9bd0SJeremy L Thompson                                 "No QFunction name provided");
452288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
453288c0443SJeremy L Thompson     size_t n;
454d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
455d1d35e2fSjeremylt     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {}
456d1d35e2fSjeremylt     if (n > match_len) {
457d1d35e2fSjeremylt       match_len = n;
458d1d35e2fSjeremylt       match_idx = i;
459288c0443SJeremy L Thompson     }
460288c0443SJeremy L Thompson   }
461d1d35e2fSjeremylt   if (!match_len)
462138d4072Sjeremylt     // LCOV_EXCL_START
463e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
464138d4072Sjeremylt   // LCOV_EXCL_STOP
465288c0443SJeremy L Thompson 
466288c0443SJeremy L Thompson   // Create QFunction
467d1d35e2fSjeremylt   ierr = CeedQFunctionCreateInterior(ceed,
468d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].vec_length,
469d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].f,
470d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].source, qf);
471288c0443SJeremy L Thompson   CeedChk(ierr);
472288c0443SJeremy L Thompson 
473288c0443SJeremy L Thompson   // QFunction specific setup
474d1d35e2fSjeremylt   ierr = gallery_qfunctions[match_idx].init(ceed, name, *qf); CeedChk(ierr);
475288c0443SJeremy L Thompson 
47675affc3bSjeremylt   // Copy name
47775affc3bSjeremylt   size_t slen = strlen(name) + 1;
47875affc3bSjeremylt   ierr = CeedMalloc(slen, &name_copy); CeedChk(ierr);
47975affc3bSjeremylt   memcpy(name_copy, name, slen);
480d1d35e2fSjeremylt   (*qf)->qf_name = name_copy;
481e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
482288c0443SJeremy L Thompson }
483288c0443SJeremy L Thompson 
484288c0443SJeremy L Thompson /**
4850219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
4860219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
4870219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
4880219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
4890219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
4900219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
4910219ea01SJeremy L Thompson            location for both.
4920219ea01SJeremy L Thompson 
4930219ea01SJeremy L Thompson   @param ceed          A Ceed object where the CeedQFunction will be created
494d1d35e2fSjeremylt   @param[in] size      Size of the QFunction fields
495d1d35e2fSjeremylt   @param[in] in_mode   CeedEvalMode for input to CeedQFunction
496d1d35e2fSjeremylt   @param[in] out_mode  CeedEvalMode for output to CeedQFunction
4970219ea01SJeremy L Thompson   @param[out] qf       Address of the variable where the newly created
4980219ea01SJeremy L Thompson                          CeedQFunction will be stored
4990219ea01SJeremy L Thompson 
5000219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5010219ea01SJeremy L Thompson 
5027a982d89SJeremy L. Thompson   @ref User
5030219ea01SJeremy L Thompson **/
504d1d35e2fSjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode,
505d1d35e2fSjeremylt                                 CeedEvalMode out_mode, CeedQFunction *qf) {
5060219ea01SJeremy L Thompson   int ierr;
5070219ea01SJeremy L Thompson 
5080219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
509d1d35e2fSjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr);
510d1d35e2fSjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr);
5110219ea01SJeremy L Thompson 
512f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
513d1d35e2fSjeremylt   CeedInt *size_data;
514d1d35e2fSjeremylt   ierr = CeedCalloc(1, &size_data); CeedChk(ierr);
515d1d35e2fSjeremylt   size_data[0] = size;
516777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
517777ff853SJeremy L Thompson   ierr = CeedQFunctionContextCreate(ceed, &ctx); CeedChk(ierr);
518777ff853SJeremy L Thompson   ierr = CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_OWN_POINTER,
519d1d35e2fSjeremylt                                      sizeof(*size_data), (void *)size_data);
520777ff853SJeremy L Thompson   CeedChk(ierr);
521777ff853SJeremy L Thompson   ierr = CeedQFunctionSetContext(*qf, ctx); CeedChk(ierr);
522777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&ctx); CeedChk(ierr);
523e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5240219ea01SJeremy L Thompson }
5250219ea01SJeremy L Thompson 
5260219ea01SJeremy L Thompson /**
5279560d06aSjeremylt   @brief Copy the pointer to a CeedQFunction. Both pointers should
5289560d06aSjeremylt            be destroyed with `CeedQFunctionDestroy()`;
5299560d06aSjeremylt            Note: If `*qf_copy` is non-NULL, then it is assumed that
5309560d06aSjeremylt            `*qf_copy` is a pointer to a CeedQFunction. This
5319560d06aSjeremylt            CeedQFunction will be destroyed if `*qf_copy` is the only
5329560d06aSjeremylt            reference to this CeedQFunction.
5339560d06aSjeremylt 
5349560d06aSjeremylt   @param qf            CeedQFunction to copy reference to
5359560d06aSjeremylt   @param[out] qf_copy  Variable to store copied reference
5369560d06aSjeremylt 
5379560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
5389560d06aSjeremylt 
5399560d06aSjeremylt   @ref User
5409560d06aSjeremylt **/
5419560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
5429560d06aSjeremylt   int ierr;
5439560d06aSjeremylt 
5449560d06aSjeremylt   ierr = CeedQFunctionReference(qf); CeedChk(ierr);
5459560d06aSjeremylt   ierr = CeedQFunctionDestroy(qf_copy); CeedChk(ierr);
5469560d06aSjeremylt   *qf_copy = qf;
5479560d06aSjeremylt   return CEED_ERROR_SUCCESS;
5489560d06aSjeremylt }
5499560d06aSjeremylt 
5509560d06aSjeremylt /**
551a0a97fcfSJed Brown   @brief Add a CeedQFunction input
552b11c1e72Sjeremylt 
553b11c1e72Sjeremylt   @param qf          CeedQFunction
554d1d35e2fSjeremylt   @param field_name  Name of QFunction field
555d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
556d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
557d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
558b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
559b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
560b11c1e72Sjeremylt 
561b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
562dfdf5a53Sjeremylt 
5637a982d89SJeremy L. Thompson   @ref User
564b11c1e72Sjeremylt **/
565d1d35e2fSjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name,
566d1d35e2fSjeremylt                           CeedInt size,
567d1d35e2fSjeremylt                           CeedEvalMode eval_mode) {
568f04ea552SJeremy L Thompson   if (qf->is_immutable)
569e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
570e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
571f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
572e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
573d1d35e2fSjeremylt   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1))
574e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
575e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
576e15f9bd0SJeremy L Thompson                      "CEED_EVAL_WEIGHT should have size 1");
577e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
578d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields],
579d1d35e2fSjeremylt                                    field_name, size, eval_mode);
580fe2413ffSjeremylt   CeedChk(ierr);
581d1d35e2fSjeremylt   qf->num_input_fields++;
582e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
583d7b241e6Sjeremylt }
584d7b241e6Sjeremylt 
585b11c1e72Sjeremylt /**
586a0a97fcfSJed Brown   @brief Add a CeedQFunction output
587b11c1e72Sjeremylt 
588b11c1e72Sjeremylt   @param qf          CeedQFunction
589d1d35e2fSjeremylt   @param field_name  Name of QFunction field
590d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
591d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
592d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
593b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
594b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
595b11c1e72Sjeremylt 
596b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
597dfdf5a53Sjeremylt 
5987a982d89SJeremy L. Thompson   @ref User
599b11c1e72Sjeremylt **/
600d1d35e2fSjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name,
601d1d35e2fSjeremylt                            CeedInt size, CeedEvalMode eval_mode) {
602f04ea552SJeremy L Thompson   if (qf->is_immutable)
603e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
604e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
605f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
606e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
607d1d35e2fSjeremylt   if (eval_mode == CEED_EVAL_WEIGHT)
608c042f62fSJeremy L Thompson     // LCOV_EXCL_START
609e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
610e15f9bd0SJeremy L Thompson                      "Cannot create QFunction output with "
6111d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
612c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
613d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields],
614d1d35e2fSjeremylt                                    field_name, size, eval_mode);
615fe2413ffSjeremylt   CeedChk(ierr);
616d1d35e2fSjeremylt   qf->num_output_fields++;
617e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
618d7b241e6Sjeremylt }
619d7b241e6Sjeremylt 
620dfdf5a53Sjeremylt /**
62143bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
62243bbe138SJeremy L Thompson 
623f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
624f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
625f04ea552SJeremy L Thompson 
62643bbe138SJeremy L Thompson   @param qf                  CeedQFunction
62743bbe138SJeremy L Thompson   @param[out] input_fields   Variable to store input_fields
62843bbe138SJeremy L Thompson   @param[out] output_fields  Variable to store output_fields
62943bbe138SJeremy L Thompson 
63043bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
63143bbe138SJeremy L Thompson 
632*e9b533fbSJeremy L Thompson   @ref Advanced
63343bbe138SJeremy L Thompson **/
63443bbe138SJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields,
63543bbe138SJeremy L Thompson                            CeedQFunctionField **input_fields,
63643bbe138SJeremy L Thompson                            CeedInt *num_output_fields,
63743bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
638f04ea552SJeremy L Thompson   qf->is_immutable = true;
63943bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
64043bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
64143bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
64243bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
64343bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
64443bbe138SJeremy L Thompson }
64543bbe138SJeremy L Thompson 
64643bbe138SJeremy L Thompson /**
64743bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
64843bbe138SJeremy L Thompson 
64943bbe138SJeremy L Thompson   @param qf_field         CeedQFunctionField
65043bbe138SJeremy L Thompson   @param[out] field_name  Variable to store the field name
65143bbe138SJeremy L Thompson 
65243bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
65343bbe138SJeremy L Thompson 
654*e9b533fbSJeremy L Thompson   @ref Advanced
65543bbe138SJeremy L Thompson **/
65643bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
65743bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
65843bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
65943bbe138SJeremy L Thompson }
66043bbe138SJeremy L Thompson 
66143bbe138SJeremy L Thompson /**
66243bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
66343bbe138SJeremy L Thompson 
66443bbe138SJeremy L Thompson   @param qf_field   CeedQFunctionField
66543bbe138SJeremy L Thompson   @param[out] size  Variable to store the size of the field
66643bbe138SJeremy L Thompson 
66743bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
66843bbe138SJeremy L Thompson 
669*e9b533fbSJeremy L Thompson   @ref Advanced
67043bbe138SJeremy L Thompson **/
67143bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
67243bbe138SJeremy L Thompson   *size = qf_field->size;
67343bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
67443bbe138SJeremy L Thompson }
67543bbe138SJeremy L Thompson 
67643bbe138SJeremy L Thompson /**
67743bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
67843bbe138SJeremy L Thompson 
67943bbe138SJeremy L Thompson   @param qf_field        CeedQFunctionField
68043bbe138SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
68143bbe138SJeremy L Thompson 
68243bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
68343bbe138SJeremy L Thompson 
684*e9b533fbSJeremy L Thompson   @ref Advanced
68543bbe138SJeremy L Thompson **/
68643bbe138SJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field,
68743bbe138SJeremy L Thompson                                   CeedEvalMode *eval_mode) {
68843bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
68943bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
69043bbe138SJeremy L Thompson }
69143bbe138SJeremy L Thompson 
69243bbe138SJeremy L Thompson /**
6934ce2993fSjeremylt   @brief Set global context for a CeedQFunction
694b11c1e72Sjeremylt 
695b11c1e72Sjeremylt   @param qf   CeedQFunction
696b11c1e72Sjeremylt   @param ctx  Context data to set
697b11c1e72Sjeremylt 
698b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
699dfdf5a53Sjeremylt 
7007a982d89SJeremy L. Thompson   @ref User
701b11c1e72Sjeremylt **/
702777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
70334359f16Sjeremylt   int ierr;
704d7b241e6Sjeremylt   qf->ctx = ctx;
7059560d06aSjeremylt   ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr);
706e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
707d7b241e6Sjeremylt }
708d7b241e6Sjeremylt 
709b11c1e72Sjeremylt /**
71075affc3bSjeremylt   @brief View a CeedQFunction
71175affc3bSjeremylt 
71275affc3bSjeremylt   @param[in] qf      CeedQFunction to view
71375affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
71475affc3bSjeremylt 
71575affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
71675affc3bSjeremylt 
7177a982d89SJeremy L. Thompson   @ref User
71875affc3bSjeremylt **/
71975affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
72075affc3bSjeremylt   int ierr;
72175affc3bSjeremylt 
72275affc3bSjeremylt   fprintf(stream, "%sCeedQFunction %s\n",
723d1d35e2fSjeremylt           qf->qf_name ? "Gallery " : "User ", qf->qf_name ? qf->qf_name : "");
72475affc3bSjeremylt 
725d1d35e2fSjeremylt   fprintf(stream, "  %d Input Field%s:\n", qf->num_input_fields,
726d1d35e2fSjeremylt           qf->num_input_fields>1 ? "s" : "");
727d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_input_fields; i++) {
728d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream);
7292da88da5Sjeremylt     CeedChk(ierr);
73075affc3bSjeremylt   }
73175affc3bSjeremylt 
732d1d35e2fSjeremylt   fprintf(stream, "  %d Output Field%s:\n", qf->num_output_fields,
733d1d35e2fSjeremylt           qf->num_output_fields>1 ? "s" : "");
734d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_output_fields; i++) {
735d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream);
73675affc3bSjeremylt     CeedChk(ierr);
73775affc3bSjeremylt   }
738e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
73975affc3bSjeremylt }
74075affc3bSjeremylt 
74175affc3bSjeremylt /**
742b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
743b11c1e72Sjeremylt 
744f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
745f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
746f04ea552SJeremy L Thompson 
747b11c1e72Sjeremylt   @param qf      CeedQFunction
748b11c1e72Sjeremylt   @param Q       Number of quadrature points
74934138859Sjeremylt   @param[in] u   Array of input CeedVectors
75034138859Sjeremylt   @param[out] v  Array of output CeedVectors
751b11c1e72Sjeremylt 
752b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
753dfdf5a53Sjeremylt 
7547a982d89SJeremy L. Thompson   @ref User
755b11c1e72Sjeremylt **/
756d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
757aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
758d7b241e6Sjeremylt   int ierr;
759d7b241e6Sjeremylt   if (!qf->Apply)
760c042f62fSJeremy L Thompson     // LCOV_EXCL_START
761e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED,
762e15f9bd0SJeremy L Thompson                      "Backend does not support QFunctionApply");
763c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
764d1d35e2fSjeremylt   if (Q % qf->vec_length)
765c042f62fSJeremy L Thompson     // LCOV_EXCL_START
766e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
767e15f9bd0SJeremy L Thompson                      "Number of quadrature points %d must be a "
768d1d35e2fSjeremylt                      "multiple of %d", Q, qf->vec_length);
769c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
770f04ea552SJeremy L Thompson   qf->is_immutable = true;
771d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
772e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
773d7b241e6Sjeremylt }
774d7b241e6Sjeremylt 
775b11c1e72Sjeremylt /**
776b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
777b11c1e72Sjeremylt 
778b11c1e72Sjeremylt   @param qf  CeedQFunction to destroy
779b11c1e72Sjeremylt 
780b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
781dfdf5a53Sjeremylt 
7827a982d89SJeremy L. Thompson   @ref User
783b11c1e72Sjeremylt **/
784d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
785d7b241e6Sjeremylt   int ierr;
786d7b241e6Sjeremylt 
787d1d35e2fSjeremylt   if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS;
788fe2413ffSjeremylt   // Backend destroy
789d7b241e6Sjeremylt   if ((*qf)->Destroy) {
790d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
791d7b241e6Sjeremylt   }
792fe2413ffSjeremylt   // Free fields
793d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_input_fields; i++) {
794d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr);
795d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr);
796fe2413ffSjeremylt   }
797d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_output_fields; i++) {
798d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr);
799d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr);
800fe2413ffSjeremylt   }
801d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr);
802d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr);
803777ff853SJeremy L Thompson 
804777ff853SJeremy L Thompson   // User context data object
805777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
806fe2413ffSjeremylt 
807d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr);
808d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->qf_name); CeedChk(ierr);
809d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
810d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
811e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
812d7b241e6Sjeremylt }
813d7b241e6Sjeremylt 
814d7b241e6Sjeremylt /// @}
815