xref: /libCEED/interface/ceed-qfunction.c (revision 43e1b16f0c7c5dbfae04543c0675e255f265ff67)
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>
23*43e1b16fSJeremy L Thompson #include <stdlib.h>
243d576824SJeremy L Thompson #include <string.h>
25288c0443SJeremy L Thompson 
267a982d89SJeremy L. Thompson /// @file
277a982d89SJeremy L. Thompson /// Implementation of public CeedQFunction interfaces
287a982d89SJeremy L. Thompson 
29288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP
30442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none;
31442e7f0bSjeremylt /// @endcond
32442e7f0bSjeremylt 
337a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
347a982d89SJeremy L. Thompson /// @{
357a982d89SJeremy L. Thompson 
367a982d89SJeremy L. Thompson // Indicate that no QFunction is provided by the user
377a982d89SJeremy L. Thompson const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
387a982d89SJeremy L. Thompson 
397a982d89SJeremy L. Thompson /// @}
407a982d89SJeremy L. Thompson 
41442e7f0bSjeremylt /// @cond DOXYGEN_SKIP
42288c0443SJeremy L Thompson static struct {
43288c0443SJeremy L Thompson   char name[CEED_MAX_RESOURCE_LEN];
44288c0443SJeremy L Thompson   char source[CEED_MAX_RESOURCE_LEN];
45d1d35e2fSjeremylt   CeedInt vec_length;
46288c0443SJeremy L Thompson   CeedQFunctionUser f;
47288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
48d1d35e2fSjeremylt } gallery_qfunctions[1024];
49288c0443SJeremy L Thompson static size_t num_qfunctions;
50288c0443SJeremy L Thompson /// @endcond
51d7b241e6Sjeremylt 
52777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
53777ff853SJeremy L Thompson /// CeedQFunction Library Internal Functions
54777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
55777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper
56777ff853SJeremy L Thompson /// @{
57777ff853SJeremy L Thompson 
58b11c1e72Sjeremylt /**
59288c0443SJeremy L Thompson   @brief Register a gallery QFunction
60288c0443SJeremy L Thompson 
61288c0443SJeremy L Thompson   @param name        Name for this backend to respond to
621176cc3aSJeremy L Thompson   @param source      Absolute path to source of QFunction,
631176cc3aSJeremy L Thompson                        "\path\CEED_DIR\gallery\folder\file.h:function_name"
64d1d35e2fSjeremylt   @param vec_length  Vector length.  Caller must ensure that number of quadrature
65d1d35e2fSjeremylt                        points is a multiple of vec_length.
66288c0443SJeremy L Thompson   @param f           Function pointer to evaluate action at quadrature points.
67288c0443SJeremy L Thompson                        See \ref CeedQFunctionUser.
68288c0443SJeremy L Thompson   @param init        Initialization function called by CeedQFunctionInit() when the
69288c0443SJeremy L Thompson                        QFunction is selected.
70288c0443SJeremy L Thompson 
71288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
72288c0443SJeremy L Thompson 
737a982d89SJeremy L. Thompson   @ref Developer
74288c0443SJeremy L Thompson **/
75288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source,
76d1d35e2fSjeremylt                           CeedInt vec_length, CeedQFunctionUser f,
77288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
78d1d35e2fSjeremylt   if (num_qfunctions >= sizeof(gallery_qfunctions) / sizeof(
79d1d35e2fSjeremylt         gallery_qfunctions[0]))
80c042f62fSJeremy L Thompson     // LCOV_EXCL_START
81e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
82c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
83c042f62fSJeremy L Thompson 
84d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
85d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
86d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].source, source,
87d1d35e2fSjeremylt           CEED_MAX_RESOURCE_LEN);
88d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
89d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].vec_length = vec_length;
90d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].f = f;
91d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].init = init;
92288c0443SJeremy L Thompson   num_qfunctions++;
93e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
94288c0443SJeremy L Thompson }
95288c0443SJeremy L Thompson 
96288c0443SJeremy L Thompson /**
977a982d89SJeremy L. Thompson   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
987a982d89SJeremy L. Thompson 
997a982d89SJeremy L. Thompson   @param f           CeedQFunctionField
100d1d35e2fSjeremylt   @param field_name  Name of QFunction field
101d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
102d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT
103d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
1047a982d89SJeremy L. Thompson                        \ref CEED_EVAL_INTERP to use interpolated values,
1057a982d89SJeremy L. Thompson                        \ref CEED_EVAL_GRAD to use gradients,
1067a982d89SJeremy L. Thompson                        \ref CEED_EVAL_WEIGHT to use quadrature weights.
1077a982d89SJeremy L. Thompson 
1087a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1097a982d89SJeremy L. Thompson 
1107a982d89SJeremy L. Thompson   @ref Developer
1117a982d89SJeremy L. Thompson **/
112d1d35e2fSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *field_name,
113d1d35e2fSjeremylt                                  CeedInt size, CeedEvalMode eval_mode) {
114d1d35e2fSjeremylt   size_t len = strlen(field_name);
1157a982d89SJeremy L. Thompson   char *tmp;
1167a982d89SJeremy L. Thompson   int ierr;
1177a982d89SJeremy L. Thompson 
118e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, f); CeedChk(ierr);
1197a982d89SJeremy L. Thompson   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
120d1d35e2fSjeremylt   memcpy(tmp, field_name, len+1);
121d1d35e2fSjeremylt   (*f)->field_name = tmp;
1227a982d89SJeremy L. Thompson   (*f)->size = size;
123d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
124e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1257a982d89SJeremy L. Thompson }
1267a982d89SJeremy L. Thompson 
1277a982d89SJeremy L. Thompson /**
1287a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1297a982d89SJeremy L. Thompson 
1307a982d89SJeremy L. Thompson   @param[in] field         QFunction field to view
131d1d35e2fSjeremylt   @param[in] field_number  Number of field being viewed
1324c4400c7SValeria Barra   @param[in] in            true for input field, false for output
1337a982d89SJeremy L. Thompson   @param[in] stream        Stream to view to, e.g., stdout
1347a982d89SJeremy L. Thompson 
1357a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1367a982d89SJeremy L. Thompson 
1377a982d89SJeremy L. Thompson   @ref Utility
1387a982d89SJeremy L. Thompson **/
139d1d35e2fSjeremylt static int CeedQFunctionFieldView(CeedQFunctionField field,
140d1d35e2fSjeremylt                                   CeedInt field_number,
1417a982d89SJeremy L. Thompson                                   bool in, FILE *stream) {
1428229195eSjeremylt   int ierr;
1437a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1448229195eSjeremylt   char *field_name;
1458229195eSjeremylt   ierr = CeedQFunctionFieldGetName(field, &field_name); CeedChk(ierr);
1468229195eSjeremylt   CeedInt size;
1478229195eSjeremylt   ierr = CeedQFunctionFieldGetSize(field, &size); CeedChk(ierr);
1488229195eSjeremylt   CeedEvalMode eval_mode;
1498229195eSjeremylt   ierr = CeedQFunctionFieldGetEvalMode(field, &eval_mode); CeedChk(ierr);
1507a982d89SJeremy L. Thompson   fprintf(stream, "    %s Field [%d]:\n"
1517a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1527a982d89SJeremy L. Thompson           "      Size: %d\n"
1537a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1548229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
155e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1567a982d89SJeremy L. Thompson }
1577a982d89SJeremy L. Thompson 
158777ff853SJeremy L Thompson /**
159777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
160777ff853SJeremy L Thompson 
161777ff853SJeremy L Thompson   @param qf      CeedQFunction
162777ff853SJeremy L Thompson   @param status  Boolean value to set as Fortran status
163777ff853SJeremy L Thompson 
164777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
165777ff853SJeremy L Thompson 
166777ff853SJeremy L Thompson   @ref Backend
167777ff853SJeremy L Thompson **/
168777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
169f04ea552SJeremy L Thompson   qf->is_fortran = status;
170e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
171777ff853SJeremy L Thompson }
172777ff853SJeremy L Thompson 
1737a982d89SJeremy L. Thompson /// @}
1747a982d89SJeremy L. Thompson 
1757a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1767a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1777a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1787a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1797a982d89SJeremy L. Thompson /// @{
1807a982d89SJeremy L. Thompson 
1817a982d89SJeremy L. Thompson /**
1827a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1837a982d89SJeremy L. Thompson 
1847a982d89SJeremy L. Thompson   @param qf               CeedQFunction
185d1d35e2fSjeremylt   @param[out] vec_length  Variable to store vector length
1867a982d89SJeremy L. Thompson 
1877a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1887a982d89SJeremy L. Thompson 
1897a982d89SJeremy L. Thompson   @ref Backend
1907a982d89SJeremy L. Thompson **/
191d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
192d1d35e2fSjeremylt   *vec_length = qf->vec_length;
193e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1947a982d89SJeremy L. Thompson }
1957a982d89SJeremy L. Thompson 
1967a982d89SJeremy L. Thompson /**
1977a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
1987a982d89SJeremy L. Thompson 
1997a982d89SJeremy L. Thompson   @param qf               CeedQFunction
200d1d35e2fSjeremylt   @param[out] num_input   Variable to store number of input fields
201d1d35e2fSjeremylt   @param[out] num_output  Variable to store number of output fields
2027a982d89SJeremy L. Thompson 
2037a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2047a982d89SJeremy L. Thompson 
2057a982d89SJeremy L. Thompson   @ref Backend
2067a982d89SJeremy L. Thompson **/
207d1d35e2fSjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input,
208d1d35e2fSjeremylt                             CeedInt *num_output) {
209d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
210d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
211e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2127a982d89SJeremy L. Thompson }
2137a982d89SJeremy L. Thompson 
2147a982d89SJeremy L. Thompson /**
215*43e1b16fSJeremy L Thompson   @brief Get the name of the user function for a CeedQFunction
2167a982d89SJeremy L. Thompson 
2177a982d89SJeremy L. Thompson   @param qf                CeedQFunction
218*43e1b16fSJeremy L Thompson   @param[out] kernel_name  Variable to store source path string
2197a982d89SJeremy L. Thompson 
2207a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2217a982d89SJeremy L. Thompson 
2227a982d89SJeremy L. Thompson   @ref Backend
2237a982d89SJeremy L. Thompson **/
224*43e1b16fSJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, char **kernel_name) {
225*43e1b16fSJeremy L Thompson   *kernel_name = (char *) qf->kernel_name;
226*43e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
227*43e1b16fSJeremy L Thompson }
228*43e1b16fSJeremy L Thompson 
229*43e1b16fSJeremy L Thompson /**
230*43e1b16fSJeremy L Thompson   @brief Get the source path string for a CeedQFunction
231*43e1b16fSJeremy L Thompson 
232*43e1b16fSJeremy L Thompson   @param qf                CeedQFunction
233*43e1b16fSJeremy L Thompson   @param[out] source_path  Variable to store source path string
234*43e1b16fSJeremy L Thompson 
235*43e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
236*43e1b16fSJeremy L Thompson 
237*43e1b16fSJeremy L Thompson   @ref Backend
238*43e1b16fSJeremy L Thompson **/
239*43e1b16fSJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) {
240*43e1b16fSJeremy L Thompson   *source_path = (char *) qf->source_path;
241e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2427a982d89SJeremy L. Thompson }
2437a982d89SJeremy L. Thompson 
2447a982d89SJeremy L. Thompson /**
2457a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2467a982d89SJeremy L. Thompson 
2477a982d89SJeremy L. Thompson   @param qf      CeedQFunction
2487a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
2497a982d89SJeremy L. Thompson 
2507a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2517a982d89SJeremy L. Thompson 
2527a982d89SJeremy L. Thompson   @ref Backend
2537a982d89SJeremy L. Thompson **/
2547a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
2557a982d89SJeremy L. Thompson   *f = qf->function;
256e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2577a982d89SJeremy L. Thompson }
2587a982d89SJeremy L. Thompson 
2597a982d89SJeremy L. Thompson /**
260777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
261777ff853SJeremy L Thompson            Note: For QFunctions from the Fortran interface, this
262777ff853SJeremy L Thompson              function will return the Fortran context
263777ff853SJeremy L Thompson              CeedQFunctionContext.
2647a982d89SJeremy L. Thompson 
2657a982d89SJeremy L. Thompson   @param qf        CeedQFunction
266777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2677a982d89SJeremy L. Thompson 
2687a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2697a982d89SJeremy L. Thompson 
2707a982d89SJeremy L. Thompson   @ref Backend
2717a982d89SJeremy L. Thompson **/
272777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
2737a982d89SJeremy L. Thompson   *ctx = qf->ctx;
274e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2757a982d89SJeremy L. Thompson }
2767a982d89SJeremy L. Thompson 
2777a982d89SJeremy L. Thompson /**
2787a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
279777ff853SJeremy L Thompson            Note: For all QFunctions this function will return the user
280777ff853SJeremy L Thompson              CeedQFunctionContext and not interface context
281777ff853SJeremy L Thompson              CeedQFunctionContext, if any such object exists.
2827a982d89SJeremy L. Thompson 
2837a982d89SJeremy L. Thompson   @param qf        CeedQFunction
284777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2857a982d89SJeremy L. Thompson 
2867a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2877a982d89SJeremy L. Thompson   @ref Backend
2887a982d89SJeremy L. Thompson **/
289777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
290777ff853SJeremy L Thompson   int ierr;
291f04ea552SJeremy L Thompson   if (qf->is_fortran) {
292d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
293d1d35e2fSjeremylt     ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx);
294777ff853SJeremy L Thompson     CeedChk(ierr);
295d1d35e2fSjeremylt     *ctx = fortran_ctx->innerctx;
296d1d35e2fSjeremylt     ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx);
297d1d35e2fSjeremylt     CeedChk(ierr);
2987a982d89SJeremy L. Thompson   } else {
2997a982d89SJeremy L. Thompson     *ctx = qf->ctx;
3007a982d89SJeremy L. Thompson   }
301e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3027a982d89SJeremy L. Thompson }
3037a982d89SJeremy L. Thompson 
3047a982d89SJeremy L. Thompson /**
3057a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
3067a982d89SJeremy L. Thompson 
3077a982d89SJeremy L. Thompson   @param qf                CeedQFunction
308d1d35e2fSjeremylt   @param[out] is_identity  Variable to store identity status
3097a982d89SJeremy L. Thompson 
3107a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3117a982d89SJeremy L. Thompson 
3127a982d89SJeremy L. Thompson   @ref Backend
3137a982d89SJeremy L. Thompson **/
314d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
315f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
316e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3177a982d89SJeremy L. Thompson }
3187a982d89SJeremy L. Thompson 
3197a982d89SJeremy L. Thompson /**
3207a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
3217a982d89SJeremy L. Thompson 
3227a982d89SJeremy L. Thompson   @param qf         CeedQFunction
3237a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
3247a982d89SJeremy L. Thompson 
3257a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3267a982d89SJeremy L. Thompson 
3277a982d89SJeremy L. Thompson   @ref Backend
3287a982d89SJeremy L. Thompson **/
329777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
330777ff853SJeremy L Thompson   *(void **)data = qf->data;
331e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3327a982d89SJeremy L. Thompson }
3337a982d89SJeremy L. Thompson 
3347a982d89SJeremy L. Thompson /**
3357a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
3367a982d89SJeremy L. Thompson 
3377a982d89SJeremy L. Thompson   @param[out] qf  CeedQFunction
3387a982d89SJeremy L. Thompson   @param data     Data to set
3397a982d89SJeremy L. Thompson 
3407a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3417a982d89SJeremy L. Thompson 
3427a982d89SJeremy L. Thompson   @ref Backend
3437a982d89SJeremy L. Thompson **/
344777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
345777ff853SJeremy L Thompson   qf->data = data;
346e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3477a982d89SJeremy L. Thompson }
3487a982d89SJeremy L. Thompson 
3497a982d89SJeremy L. Thompson /**
35034359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
35134359f16Sjeremylt 
35234359f16Sjeremylt   @param qf  CeedQFunction to increment the reference counter
35334359f16Sjeremylt 
35434359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
35534359f16Sjeremylt 
35634359f16Sjeremylt   @ref Backend
35734359f16Sjeremylt **/
3589560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
35934359f16Sjeremylt   qf->ref_count++;
36034359f16Sjeremylt   return CEED_ERROR_SUCCESS;
36134359f16Sjeremylt }
36234359f16Sjeremylt 
3637a982d89SJeremy L. Thompson /// @}
3647a982d89SJeremy L. Thompson 
3657a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3667a982d89SJeremy L. Thompson /// CeedQFunction Public API
3677a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3687a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
3697a982d89SJeremy L. Thompson /// @{
3707a982d89SJeremy L. Thompson 
3717a982d89SJeremy L. Thompson /**
3727a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
3737a982d89SJeremy L. Thompson 
3747a982d89SJeremy L. Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
375d1d35e2fSjeremylt   @param vec_length  Vector length. Caller must ensure that number of quadrature
376d1d35e2fSjeremylt                        points is a multiple of vec_length.
3777a982d89SJeremy L. Thompson   @param f           Function pointer to evaluate action at quadrature points.
3787a982d89SJeremy L. Thompson                        See \ref CeedQFunctionUser.
3797a982d89SJeremy L. Thompson   @param source      Absolute path to source of QFunction,
380c8d5b03cSJeremy L Thompson                        "\abs_path\file.h:function_name".
381c8d5b03cSJeremy L Thompson                        For support across all backends, this source must only
382c8d5b03cSJeremy L Thompson                        contain constructs supported by C99, C++11, and CUDA.
3837a982d89SJeremy L. Thompson   @param[out] qf     Address of the variable where the newly created
3847a982d89SJeremy L. Thompson                        CeedQFunction will be stored
3857a982d89SJeremy L. Thompson 
3867a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3877a982d89SJeremy L. Thompson 
3887a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
3897a982d89SJeremy L. Thompson     arguments.
3907a982d89SJeremy L. Thompson 
3917a982d89SJeremy L. Thompson   @ref User
3927a982d89SJeremy L. Thompson **/
393d1d35e2fSjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length,
394d1d35e2fSjeremylt                                 CeedQFunctionUser f,
3957a982d89SJeremy L. Thompson                                 const char *source, CeedQFunction *qf) {
3967a982d89SJeremy L. Thompson   int ierr;
397*43e1b16fSJeremy L Thompson   char *source_copy, *kernel_name_copy;
3987a982d89SJeremy L. Thompson 
3997a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
4007a982d89SJeremy L. Thompson     Ceed delegate;
4017a982d89SJeremy L. Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
4027a982d89SJeremy L. Thompson 
4037a982d89SJeremy L. Thompson     if (!delegate)
4047a982d89SJeremy L. Thompson       // LCOV_EXCL_START
405e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
406e15f9bd0SJeremy L Thompson                        "Backend does not support QFunctionCreate");
4077a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
4087a982d89SJeremy L. Thompson 
409d1d35e2fSjeremylt     ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf);
4107a982d89SJeremy L. Thompson     CeedChk(ierr);
411e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
4127a982d89SJeremy L. Thompson   }
4137a982d89SJeremy L. Thompson 
414*43e1b16fSJeremy L Thompson   if (strlen(source) && !strrchr(source, ':'))\
415*43e1b16fSJeremy L Thompson     // LCOV_EXCL_START
416*43e1b16fSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPLETE,
417*43e1b16fSJeremy L Thompson                      "Provided path to source does not include function name. "
418*43e1b16fSJeremy L Thompson                      "Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"",
419*43e1b16fSJeremy L Thompson                      source);
420*43e1b16fSJeremy L Thompson   // LCOV_EXCL_STOP
421*43e1b16fSJeremy L Thompson 
4227a982d89SJeremy L. Thompson   ierr = CeedCalloc(1, qf); CeedChk(ierr);
4237a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
4249560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
425d1d35e2fSjeremylt   (*qf)->ref_count = 1;
426d1d35e2fSjeremylt   (*qf)->vec_length = vec_length;
427f04ea552SJeremy L Thompson   (*qf)->is_identity = false;
4287a982d89SJeremy L. Thompson   (*qf)->function = f;
429*43e1b16fSJeremy L Thompson   if (strlen(source)) {
430*43e1b16fSJeremy L Thompson     const char *kernel_name = strrchr(source, ':') + 1;
431*43e1b16fSJeremy L Thompson     size_t kernel_name_len = strlen(kernel_name);
432*43e1b16fSJeremy L Thompson     ierr = CeedCalloc(kernel_name_len + 1, &kernel_name_copy); CeedChk(ierr);
433*43e1b16fSJeremy L Thompson     strncpy(kernel_name_copy, kernel_name, kernel_name_len);
434*43e1b16fSJeremy L Thompson     (*qf)->kernel_name = kernel_name_copy;
435*43e1b16fSJeremy L Thompson 
436*43e1b16fSJeremy L Thompson     size_t source_len = strlen(source) - kernel_name_len - 1;
437*43e1b16fSJeremy L Thompson     ierr = CeedCalloc(source_len + 1, &source_copy); CeedChk(ierr);
438*43e1b16fSJeremy L Thompson     strncpy(source_copy, source, source_len);
439d1d35e2fSjeremylt     (*qf)->source_path = source_copy;
440*43e1b16fSJeremy L Thompson   }
441d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->input_fields); CeedChk(ierr);
442d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->output_fields); CeedChk(ierr);
4437a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
444e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4457a982d89SJeremy L. Thompson }
4467a982d89SJeremy L. Thompson 
4477a982d89SJeremy L. Thompson /**
448288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
449288c0443SJeremy L Thompson 
450288c0443SJeremy L Thompson   @param ceed     A Ceed object where the CeedQFunction will be created
451288c0443SJeremy L Thompson   @param name     Name of QFunction to use from gallery
452288c0443SJeremy L Thompson   @param[out] qf  Address of the variable where the newly created
453288c0443SJeremy L Thompson                     CeedQFunction will be stored
454288c0443SJeremy L Thompson 
455288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
456288c0443SJeremy L Thompson 
4577a982d89SJeremy L. Thompson   @ref User
458288c0443SJeremy L Thompson **/
459288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
460288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
461288c0443SJeremy L Thompson   int ierr;
462d1d35e2fSjeremylt   size_t match_len = 0, match_idx = UINT_MAX;
46375affc3bSjeremylt   char *name_copy;
464288c0443SJeremy L Thompson 
4651d013790SJed Brown   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
466288c0443SJeremy L Thompson   // Find matching backend
467e15f9bd0SJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE,
468e15f9bd0SJeremy L Thompson                                 "No QFunction name provided");
469288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
470288c0443SJeremy L Thompson     size_t n;
471d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
472d1d35e2fSjeremylt     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {}
473d1d35e2fSjeremylt     if (n > match_len) {
474d1d35e2fSjeremylt       match_len = n;
475d1d35e2fSjeremylt       match_idx = i;
476288c0443SJeremy L Thompson     }
477288c0443SJeremy L Thompson   }
478d1d35e2fSjeremylt   if (!match_len)
479138d4072Sjeremylt     // LCOV_EXCL_START
480e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
481138d4072Sjeremylt   // LCOV_EXCL_STOP
482288c0443SJeremy L Thompson 
483288c0443SJeremy L Thompson   // Create QFunction
484d1d35e2fSjeremylt   ierr = CeedQFunctionCreateInterior(ceed,
485d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].vec_length,
486d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].f,
487d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].source, qf);
488288c0443SJeremy L Thompson   CeedChk(ierr);
489288c0443SJeremy L Thompson 
490288c0443SJeremy L Thompson   // QFunction specific setup
491d1d35e2fSjeremylt   ierr = gallery_qfunctions[match_idx].init(ceed, name, *qf); CeedChk(ierr);
492288c0443SJeremy L Thompson 
49375affc3bSjeremylt   // Copy name
49475affc3bSjeremylt   size_t slen = strlen(name) + 1;
49575affc3bSjeremylt   ierr = CeedMalloc(slen, &name_copy); CeedChk(ierr);
49675affc3bSjeremylt   memcpy(name_copy, name, slen);
497*43e1b16fSJeremy L Thompson   (*qf)->gallery_name = name_copy;
498*43e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
499e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
500288c0443SJeremy L Thompson }
501288c0443SJeremy L Thompson 
502288c0443SJeremy L Thompson /**
5030219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
5040219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
5050219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
5060219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
5070219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
5080219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
5090219ea01SJeremy L Thompson            location for both.
5100219ea01SJeremy L Thompson 
5110219ea01SJeremy L Thompson   @param ceed          A Ceed object where the CeedQFunction will be created
512d1d35e2fSjeremylt   @param[in] size      Size of the QFunction fields
513d1d35e2fSjeremylt   @param[in] in_mode   CeedEvalMode for input to CeedQFunction
514d1d35e2fSjeremylt   @param[in] out_mode  CeedEvalMode for output to CeedQFunction
5150219ea01SJeremy L Thompson   @param[out] qf       Address of the variable where the newly created
5160219ea01SJeremy L Thompson                          CeedQFunction will be stored
5170219ea01SJeremy L Thompson 
5180219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5190219ea01SJeremy L Thompson 
5207a982d89SJeremy L. Thompson   @ref User
5210219ea01SJeremy L Thompson **/
522d1d35e2fSjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode,
523d1d35e2fSjeremylt                                 CeedEvalMode out_mode, CeedQFunction *qf) {
5240219ea01SJeremy L Thompson   int ierr;
5250219ea01SJeremy L Thompson 
5260219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
527d1d35e2fSjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr);
528d1d35e2fSjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr);
5290219ea01SJeremy L Thompson 
530f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
531d1d35e2fSjeremylt   CeedInt *size_data;
532d1d35e2fSjeremylt   ierr = CeedCalloc(1, &size_data); CeedChk(ierr);
533d1d35e2fSjeremylt   size_data[0] = size;
534777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
535777ff853SJeremy L Thompson   ierr = CeedQFunctionContextCreate(ceed, &ctx); CeedChk(ierr);
536777ff853SJeremy L Thompson   ierr = CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_OWN_POINTER,
537d1d35e2fSjeremylt                                      sizeof(*size_data), (void *)size_data);
538777ff853SJeremy L Thompson   CeedChk(ierr);
539777ff853SJeremy L Thompson   ierr = CeedQFunctionSetContext(*qf, ctx); CeedChk(ierr);
540777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&ctx); CeedChk(ierr);
541e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5420219ea01SJeremy L Thompson }
5430219ea01SJeremy L Thompson 
5440219ea01SJeremy L Thompson /**
5459560d06aSjeremylt   @brief Copy the pointer to a CeedQFunction. Both pointers should
5469560d06aSjeremylt            be destroyed with `CeedQFunctionDestroy()`;
5479560d06aSjeremylt            Note: If `*qf_copy` is non-NULL, then it is assumed that
5489560d06aSjeremylt            `*qf_copy` is a pointer to a CeedQFunction. This
5499560d06aSjeremylt            CeedQFunction will be destroyed if `*qf_copy` is the only
5509560d06aSjeremylt            reference to this CeedQFunction.
5519560d06aSjeremylt 
5529560d06aSjeremylt   @param qf            CeedQFunction to copy reference to
5539560d06aSjeremylt   @param[out] qf_copy  Variable to store copied reference
5549560d06aSjeremylt 
5559560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
5569560d06aSjeremylt 
5579560d06aSjeremylt   @ref User
5589560d06aSjeremylt **/
5599560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
5609560d06aSjeremylt   int ierr;
5619560d06aSjeremylt 
5629560d06aSjeremylt   ierr = CeedQFunctionReference(qf); CeedChk(ierr);
5639560d06aSjeremylt   ierr = CeedQFunctionDestroy(qf_copy); CeedChk(ierr);
5649560d06aSjeremylt   *qf_copy = qf;
5659560d06aSjeremylt   return CEED_ERROR_SUCCESS;
5669560d06aSjeremylt }
5679560d06aSjeremylt 
5689560d06aSjeremylt /**
569a0a97fcfSJed Brown   @brief Add a CeedQFunction input
570b11c1e72Sjeremylt 
571b11c1e72Sjeremylt   @param qf          CeedQFunction
572d1d35e2fSjeremylt   @param field_name  Name of QFunction field
573d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
574d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
575d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
576b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
577b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
578b11c1e72Sjeremylt 
579b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
580dfdf5a53Sjeremylt 
5817a982d89SJeremy L. Thompson   @ref User
582b11c1e72Sjeremylt **/
583d1d35e2fSjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name,
584d1d35e2fSjeremylt                           CeedInt size,
585d1d35e2fSjeremylt                           CeedEvalMode eval_mode) {
586f04ea552SJeremy L Thompson   if (qf->is_immutable)
587e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
588e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
589f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
590e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
591d1d35e2fSjeremylt   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1))
592e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
593e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
594e15f9bd0SJeremy L Thompson                      "CEED_EVAL_WEIGHT should have size 1");
595e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
596d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields],
597d1d35e2fSjeremylt                                    field_name, size, eval_mode);
598fe2413ffSjeremylt   CeedChk(ierr);
599d1d35e2fSjeremylt   qf->num_input_fields++;
600e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
601d7b241e6Sjeremylt }
602d7b241e6Sjeremylt 
603b11c1e72Sjeremylt /**
604a0a97fcfSJed Brown   @brief Add a CeedQFunction output
605b11c1e72Sjeremylt 
606b11c1e72Sjeremylt   @param qf          CeedQFunction
607d1d35e2fSjeremylt   @param field_name  Name of QFunction field
608d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
609d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
610d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
611b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
612b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
613b11c1e72Sjeremylt 
614b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
615dfdf5a53Sjeremylt 
6167a982d89SJeremy L. Thompson   @ref User
617b11c1e72Sjeremylt **/
618d1d35e2fSjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name,
619d1d35e2fSjeremylt                            CeedInt size, CeedEvalMode eval_mode) {
620f04ea552SJeremy L Thompson   if (qf->is_immutable)
621e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
622e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
623f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
624e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
625d1d35e2fSjeremylt   if (eval_mode == CEED_EVAL_WEIGHT)
626c042f62fSJeremy L Thompson     // LCOV_EXCL_START
627e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
628e15f9bd0SJeremy L Thompson                      "Cannot create QFunction output with "
6291d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
630c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
631d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields],
632d1d35e2fSjeremylt                                    field_name, size, eval_mode);
633fe2413ffSjeremylt   CeedChk(ierr);
634d1d35e2fSjeremylt   qf->num_output_fields++;
635e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
636d7b241e6Sjeremylt }
637d7b241e6Sjeremylt 
638dfdf5a53Sjeremylt /**
63943bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
64043bbe138SJeremy L Thompson 
641f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
642f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
643f04ea552SJeremy L Thompson 
64443bbe138SJeremy L Thompson   @param qf                  CeedQFunction
64543bbe138SJeremy L Thompson   @param[out] input_fields   Variable to store input_fields
64643bbe138SJeremy L Thompson   @param[out] output_fields  Variable to store output_fields
64743bbe138SJeremy L Thompson 
64843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
64943bbe138SJeremy L Thompson 
650e9b533fbSJeremy L Thompson   @ref Advanced
65143bbe138SJeremy L Thompson **/
65243bbe138SJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields,
65343bbe138SJeremy L Thompson                            CeedQFunctionField **input_fields,
65443bbe138SJeremy L Thompson                            CeedInt *num_output_fields,
65543bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
656f04ea552SJeremy L Thompson   qf->is_immutable = true;
65743bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
65843bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
65943bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
66043bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
66143bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
66243bbe138SJeremy L Thompson }
66343bbe138SJeremy L Thompson 
66443bbe138SJeremy L Thompson /**
66543bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
66643bbe138SJeremy L Thompson 
66743bbe138SJeremy L Thompson   @param qf_field         CeedQFunctionField
66843bbe138SJeremy L Thompson   @param[out] field_name  Variable to store the field name
66943bbe138SJeremy L Thompson 
67043bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
67143bbe138SJeremy L Thompson 
672e9b533fbSJeremy L Thompson   @ref Advanced
67343bbe138SJeremy L Thompson **/
67443bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
67543bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
67643bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
67743bbe138SJeremy L Thompson }
67843bbe138SJeremy L Thompson 
67943bbe138SJeremy L Thompson /**
68043bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
68143bbe138SJeremy L Thompson 
68243bbe138SJeremy L Thompson   @param qf_field   CeedQFunctionField
68343bbe138SJeremy L Thompson   @param[out] size  Variable to store the size of the field
68443bbe138SJeremy L Thompson 
68543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
68643bbe138SJeremy L Thompson 
687e9b533fbSJeremy L Thompson   @ref Advanced
68843bbe138SJeremy L Thompson **/
68943bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
69043bbe138SJeremy L Thompson   *size = qf_field->size;
69143bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
69243bbe138SJeremy L Thompson }
69343bbe138SJeremy L Thompson 
69443bbe138SJeremy L Thompson /**
69543bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
69643bbe138SJeremy L Thompson 
69743bbe138SJeremy L Thompson   @param qf_field        CeedQFunctionField
69843bbe138SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
69943bbe138SJeremy L Thompson 
70043bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
70143bbe138SJeremy L Thompson 
702e9b533fbSJeremy L Thompson   @ref Advanced
70343bbe138SJeremy L Thompson **/
70443bbe138SJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field,
70543bbe138SJeremy L Thompson                                   CeedEvalMode *eval_mode) {
70643bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
70743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
70843bbe138SJeremy L Thompson }
70943bbe138SJeremy L Thompson 
71043bbe138SJeremy L Thompson /**
7114ce2993fSjeremylt   @brief Set global context for a CeedQFunction
712b11c1e72Sjeremylt 
713b11c1e72Sjeremylt   @param qf   CeedQFunction
714b11c1e72Sjeremylt   @param ctx  Context data to set
715b11c1e72Sjeremylt 
716b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
717dfdf5a53Sjeremylt 
7187a982d89SJeremy L. Thompson   @ref User
719b11c1e72Sjeremylt **/
720777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
72134359f16Sjeremylt   int ierr;
722d7b241e6Sjeremylt   qf->ctx = ctx;
7239560d06aSjeremylt   ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr);
724e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
725d7b241e6Sjeremylt }
726d7b241e6Sjeremylt 
727b11c1e72Sjeremylt /**
72875affc3bSjeremylt   @brief View a CeedQFunction
72975affc3bSjeremylt 
73075affc3bSjeremylt   @param[in] qf      CeedQFunction to view
73175affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
73275affc3bSjeremylt 
73375affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
73475affc3bSjeremylt 
7357a982d89SJeremy L. Thompson   @ref User
73675affc3bSjeremylt **/
73775affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
73875affc3bSjeremylt   int ierr;
73975affc3bSjeremylt 
74075affc3bSjeremylt   fprintf(stream, "%sCeedQFunction %s\n",
741*43e1b16fSJeremy L Thompson           qf->is_gallery ? "Gallery " : "User ",
742*43e1b16fSJeremy L Thompson           qf->is_gallery ? qf->gallery_name : qf->kernel_name);
74375affc3bSjeremylt 
744d1d35e2fSjeremylt   fprintf(stream, "  %d Input Field%s:\n", qf->num_input_fields,
745d1d35e2fSjeremylt           qf->num_input_fields>1 ? "s" : "");
746d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_input_fields; i++) {
747d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream);
7482da88da5Sjeremylt     CeedChk(ierr);
74975affc3bSjeremylt   }
75075affc3bSjeremylt 
751d1d35e2fSjeremylt   fprintf(stream, "  %d Output Field%s:\n", qf->num_output_fields,
752d1d35e2fSjeremylt           qf->num_output_fields>1 ? "s" : "");
753d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_output_fields; i++) {
754d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream);
75575affc3bSjeremylt     CeedChk(ierr);
75675affc3bSjeremylt   }
757e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
75875affc3bSjeremylt }
75975affc3bSjeremylt 
76075affc3bSjeremylt /**
761b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunction
762b7c9bbdaSJeremy L Thompson 
763b7c9bbdaSJeremy L Thompson   @param qf              CeedQFunction
764b7c9bbdaSJeremy L Thompson   @param[out] ceed       Variable to store Ceed
765b7c9bbdaSJeremy L Thompson 
766b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
767b7c9bbdaSJeremy L Thompson 
768b7c9bbdaSJeremy L Thompson   @ref Advanced
769b7c9bbdaSJeremy L Thompson **/
770b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
771b7c9bbdaSJeremy L Thompson   *ceed = qf->ceed;
772b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
773b7c9bbdaSJeremy L Thompson }
774b7c9bbdaSJeremy L Thompson 
775b7c9bbdaSJeremy L Thompson /**
776b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
777b11c1e72Sjeremylt 
778f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
779f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
780f04ea552SJeremy L Thompson 
781b11c1e72Sjeremylt   @param qf      CeedQFunction
782b11c1e72Sjeremylt   @param Q       Number of quadrature points
78334138859Sjeremylt   @param[in] u   Array of input CeedVectors
78434138859Sjeremylt   @param[out] v  Array of output CeedVectors
785b11c1e72Sjeremylt 
786b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
787dfdf5a53Sjeremylt 
7887a982d89SJeremy L. Thompson   @ref User
789b11c1e72Sjeremylt **/
790d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
791aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
792d7b241e6Sjeremylt   int ierr;
793d7b241e6Sjeremylt   if (!qf->Apply)
794c042f62fSJeremy L Thompson     // LCOV_EXCL_START
795e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED,
796e15f9bd0SJeremy L Thompson                      "Backend does not support QFunctionApply");
797c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
798d1d35e2fSjeremylt   if (Q % qf->vec_length)
799c042f62fSJeremy L Thompson     // LCOV_EXCL_START
800e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
801e15f9bd0SJeremy L Thompson                      "Number of quadrature points %d must be a "
802d1d35e2fSjeremylt                      "multiple of %d", Q, qf->vec_length);
803c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
804f04ea552SJeremy L Thompson   qf->is_immutable = true;
805d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
806e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
807d7b241e6Sjeremylt }
808d7b241e6Sjeremylt 
809b11c1e72Sjeremylt /**
810b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
811b11c1e72Sjeremylt 
812b11c1e72Sjeremylt   @param qf  CeedQFunction to destroy
813b11c1e72Sjeremylt 
814b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
815dfdf5a53Sjeremylt 
8167a982d89SJeremy L. Thompson   @ref User
817b11c1e72Sjeremylt **/
818d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
819d7b241e6Sjeremylt   int ierr;
820d7b241e6Sjeremylt 
821d1d35e2fSjeremylt   if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS;
822fe2413ffSjeremylt   // Backend destroy
823d7b241e6Sjeremylt   if ((*qf)->Destroy) {
824d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
825d7b241e6Sjeremylt   }
826fe2413ffSjeremylt   // Free fields
827d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_input_fields; i++) {
828d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr);
829d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr);
830fe2413ffSjeremylt   }
831d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_output_fields; i++) {
832d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr);
833d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr);
834fe2413ffSjeremylt   }
835d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr);
836d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr);
837777ff853SJeremy L Thompson 
838777ff853SJeremy L Thompson   // User context data object
839777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
840fe2413ffSjeremylt 
841d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr);
842*43e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->gallery_name); CeedChk(ierr);
843*43e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->kernel_name); CeedChk(ierr);
844d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
845d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
846e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
847d7b241e6Sjeremylt }
848d7b241e6Sjeremylt 
849d7b241e6Sjeremylt /// @}
850