xref: /libCEED/interface/ceed-qfunction.c (revision d1d35e2f02dc969aee8debf3fd943dd784aa847a)
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];
44*d1d35e2fSjeremylt   CeedInt vec_length;
45288c0443SJeremy L Thompson   CeedQFunctionUser f;
46288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
47*d1d35e2fSjeremylt } 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"
63*d1d35e2fSjeremylt   @param vec_length  Vector length.  Caller must ensure that number of quadrature
64*d1d35e2fSjeremylt                        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,
75*d1d35e2fSjeremylt                           CeedInt vec_length, CeedQFunctionUser f,
76288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
77*d1d35e2fSjeremylt   if (num_qfunctions >= sizeof(gallery_qfunctions) / sizeof(
78*d1d35e2fSjeremylt         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 
83*d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
84*d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
85*d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].source, source,
86*d1d35e2fSjeremylt           CEED_MAX_RESOURCE_LEN);
87*d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
88*d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].vec_length = vec_length;
89*d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].f = f;
90*d1d35e2fSjeremylt   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
99*d1d35e2fSjeremylt   @param field_name  Name of QFunction field
100*d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
101*d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT
102*d1d35e2fSjeremylt   @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 **/
111*d1d35e2fSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *field_name,
112*d1d35e2fSjeremylt                                  CeedInt size, CeedEvalMode eval_mode) {
113*d1d35e2fSjeremylt   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);
119*d1d35e2fSjeremylt   memcpy(tmp, field_name, len+1);
120*d1d35e2fSjeremylt   (*f)->field_name = tmp;
1217a982d89SJeremy L. Thompson   (*f)->size = size;
122*d1d35e2fSjeremylt   (*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
130*d1d35e2fSjeremylt   @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 **/
138*d1d35e2fSjeremylt static int CeedQFunctionFieldView(CeedQFunctionField field,
139*d1d35e2fSjeremylt                                   CeedInt field_number,
1407a982d89SJeremy L. Thompson                                   bool in, FILE *stream) {
1417a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1427a982d89SJeremy L. Thompson   fprintf(stream, "    %s Field [%d]:\n"
1437a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1447a982d89SJeremy L. Thompson           "      Size: %d\n"
1457a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
146*d1d35e2fSjeremylt           inout, field_number, field->field_name, field->size,
147*d1d35e2fSjeremylt           CeedEvalModes[field->eval_mode]);
148e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1497a982d89SJeremy L. Thompson }
1507a982d89SJeremy L. Thompson 
151777ff853SJeremy L Thompson /**
152777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
153777ff853SJeremy L Thompson 
154777ff853SJeremy L Thompson   @param qf      CeedQFunction
155777ff853SJeremy L Thompson   @param status  Boolean value to set as Fortran status
156777ff853SJeremy L Thompson 
157777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
158777ff853SJeremy L Thompson 
159777ff853SJeremy L Thompson   @ref Backend
160777ff853SJeremy L Thompson **/
161777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
162*d1d35e2fSjeremylt   qf->fortran_status = status;
163e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
164777ff853SJeremy L Thompson }
165777ff853SJeremy L Thompson 
1667a982d89SJeremy L. Thompson /// @}
1677a982d89SJeremy L. Thompson 
1687a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1697a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1707a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1717a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1727a982d89SJeremy L. Thompson /// @{
1737a982d89SJeremy L. Thompson 
1747a982d89SJeremy L. Thompson /**
1757a982d89SJeremy L. Thompson   @brief Get the Ceed associated with a CeedQFunction
1767a982d89SJeremy L. Thompson 
1777a982d89SJeremy L. Thompson   @param qf              CeedQFunction
1787a982d89SJeremy L. Thompson   @param[out] ceed       Variable to store Ceed
1797a982d89SJeremy L. Thompson 
1807a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1817a982d89SJeremy L. Thompson 
1827a982d89SJeremy L. Thompson   @ref Backend
1837a982d89SJeremy L. Thompson **/
1847a982d89SJeremy L. Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
1857a982d89SJeremy L. Thompson   *ceed = qf->ceed;
186e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1877a982d89SJeremy L. Thompson }
1887a982d89SJeremy L. Thompson 
1897a982d89SJeremy L. Thompson /**
1907a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1917a982d89SJeremy L. Thompson 
1927a982d89SJeremy L. Thompson   @param qf               CeedQFunction
193*d1d35e2fSjeremylt   @param[out] vec_length  Variable to store vector length
1947a982d89SJeremy L. Thompson 
1957a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1967a982d89SJeremy L. Thompson 
1977a982d89SJeremy L. Thompson   @ref Backend
1987a982d89SJeremy L. Thompson **/
199*d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
200*d1d35e2fSjeremylt   *vec_length = qf->vec_length;
201e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2027a982d89SJeremy L. Thompson }
2037a982d89SJeremy L. Thompson 
2047a982d89SJeremy L. Thompson /**
2057a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
2067a982d89SJeremy L. Thompson 
2077a982d89SJeremy L. Thompson   @param qf               CeedQFunction
208*d1d35e2fSjeremylt   @param[out] num_input   Variable to store number of input fields
209*d1d35e2fSjeremylt   @param[out] num_output  Variable to store number of output fields
2107a982d89SJeremy L. Thompson 
2117a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2127a982d89SJeremy L. Thompson 
2137a982d89SJeremy L. Thompson   @ref Backend
2147a982d89SJeremy L. Thompson **/
215*d1d35e2fSjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input,
216*d1d35e2fSjeremylt                             CeedInt *num_output) {
217*d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
218*d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
219e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2207a982d89SJeremy L. Thompson }
2217a982d89SJeremy L. Thompson 
2227a982d89SJeremy L. Thompson /**
2237a982d89SJeremy L. Thompson   @brief Get the source path string for a CeedQFunction
2247a982d89SJeremy L. Thompson 
2257a982d89SJeremy L. Thompson   @param qf           CeedQFunction
2267a982d89SJeremy L. Thompson   @param[out] source  Variable to store source path string
2277a982d89SJeremy L. Thompson 
2287a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2297a982d89SJeremy L. Thompson 
2307a982d89SJeremy L. Thompson   @ref Backend
2317a982d89SJeremy L. Thompson **/
2327a982d89SJeremy L. Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source) {
233*d1d35e2fSjeremylt   *source = (char *) qf->source_path;
234e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2357a982d89SJeremy L. Thompson }
2367a982d89SJeremy L. Thompson 
2377a982d89SJeremy L. Thompson /**
2387a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2397a982d89SJeremy L. Thompson 
2407a982d89SJeremy L. Thompson   @param qf      CeedQFunction
2417a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
2427a982d89SJeremy L. Thompson 
2437a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2447a982d89SJeremy L. Thompson 
2457a982d89SJeremy L. Thompson   @ref Backend
2467a982d89SJeremy L. Thompson **/
2477a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
2487a982d89SJeremy L. Thompson   *f = qf->function;
249e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2507a982d89SJeremy L. Thompson }
2517a982d89SJeremy L. Thompson 
2527a982d89SJeremy L. Thompson /**
253777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
254777ff853SJeremy L Thompson            Note: For QFunctions from the Fortran interface, this
255777ff853SJeremy L Thompson              function will return the Fortran context
256777ff853SJeremy L Thompson              CeedQFunctionContext.
2577a982d89SJeremy L. Thompson 
2587a982d89SJeremy L. Thompson   @param qf        CeedQFunction
259777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2607a982d89SJeremy L. Thompson 
2617a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2627a982d89SJeremy L. Thompson 
2637a982d89SJeremy L. Thompson   @ref Backend
2647a982d89SJeremy L. Thompson **/
265777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
2667a982d89SJeremy L. Thompson   *ctx = qf->ctx;
267e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2687a982d89SJeremy L. Thompson }
2697a982d89SJeremy L. Thompson 
2707a982d89SJeremy L. Thompson /**
2717a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
272777ff853SJeremy L Thompson            Note: For all QFunctions this function will return the user
273777ff853SJeremy L Thompson              CeedQFunctionContext and not interface context
274777ff853SJeremy L Thompson              CeedQFunctionContext, if any such object exists.
2757a982d89SJeremy L. Thompson 
2767a982d89SJeremy L. Thompson   @param qf        CeedQFunction
277777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2787a982d89SJeremy L. Thompson 
2797a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2807a982d89SJeremy L. Thompson   @ref Backend
2817a982d89SJeremy L. Thompson **/
282777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
283777ff853SJeremy L Thompson   int ierr;
284*d1d35e2fSjeremylt   if (qf->fortran_status) {
285*d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
286*d1d35e2fSjeremylt     ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx);
287777ff853SJeremy L Thompson     CeedChk(ierr);
288*d1d35e2fSjeremylt     *ctx = fortran_ctx->innerctx;
289*d1d35e2fSjeremylt     ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx);
290*d1d35e2fSjeremylt     CeedChk(ierr);
2917a982d89SJeremy L. Thompson   } else {
2927a982d89SJeremy L. Thompson     *ctx = qf->ctx;
2937a982d89SJeremy L. Thompson   }
294e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2957a982d89SJeremy L. Thompson }
2967a982d89SJeremy L. Thompson 
2977a982d89SJeremy L. Thompson /**
2987a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
2997a982d89SJeremy L. Thompson 
3007a982d89SJeremy L. Thompson   @param qf                CeedQFunction
301*d1d35e2fSjeremylt   @param[out] is_identity  Variable to store identity status
3027a982d89SJeremy L. Thompson 
3037a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3047a982d89SJeremy L. Thompson 
3057a982d89SJeremy L. Thompson   @ref Backend
3067a982d89SJeremy L. Thompson **/
307*d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
308*d1d35e2fSjeremylt   *is_identity = qf->identity;
309e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3107a982d89SJeremy L. Thompson }
3117a982d89SJeremy L. Thompson 
3127a982d89SJeremy L. Thompson /**
3137a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
3147a982d89SJeremy L. Thompson 
3157a982d89SJeremy L. Thompson   @param qf         CeedQFunction
3167a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
3177a982d89SJeremy L. Thompson 
3187a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3197a982d89SJeremy L. Thompson 
3207a982d89SJeremy L. Thompson   @ref Backend
3217a982d89SJeremy L. Thompson **/
322777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
323777ff853SJeremy L Thompson   *(void **)data = qf->data;
324e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3257a982d89SJeremy L. Thompson }
3267a982d89SJeremy L. Thompson 
3277a982d89SJeremy L. Thompson /**
3287a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
3297a982d89SJeremy L. Thompson 
3307a982d89SJeremy L. Thompson   @param[out] qf  CeedQFunction
3317a982d89SJeremy L. Thompson   @param data     Data to set
3327a982d89SJeremy L. Thompson 
3337a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3347a982d89SJeremy L. Thompson 
3357a982d89SJeremy L. Thompson   @ref Backend
3367a982d89SJeremy L. Thompson **/
337777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
338777ff853SJeremy L Thompson   qf->data = data;
339e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3407a982d89SJeremy L. Thompson }
3417a982d89SJeremy L. Thompson 
3427a982d89SJeremy L. Thompson /**
3437a982d89SJeremy L. Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
3447a982d89SJeremy L. Thompson 
3457a982d89SJeremy L. Thompson   @param qf                  CeedQFunction
346*d1d35e2fSjeremylt   @param[out] input_fields   Variable to store input_fields
347*d1d35e2fSjeremylt   @param[out] output_fields  Variable to store output_fields
3487a982d89SJeremy L. Thompson 
3497a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3507a982d89SJeremy L. Thompson 
3517a982d89SJeremy L. Thompson   @ref Backend
3527a982d89SJeremy L. Thompson **/
353*d1d35e2fSjeremylt int CeedQFunctionGetFields(CeedQFunction qf, CeedQFunctionField **input_fields,
354*d1d35e2fSjeremylt                            CeedQFunctionField **output_fields) {
355*d1d35e2fSjeremylt   if (input_fields) *input_fields = qf->input_fields;
356*d1d35e2fSjeremylt   if (output_fields) *output_fields = qf->output_fields;
357e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3587a982d89SJeremy L. Thompson }
3597a982d89SJeremy L. Thompson 
3607a982d89SJeremy L. Thompson /**
3617a982d89SJeremy L. Thompson   @brief Get the name of a CeedQFunctionField
3627a982d89SJeremy L. Thompson 
363*d1d35e2fSjeremylt   @param qf_field         CeedQFunctionField
364*d1d35e2fSjeremylt   @param[out] field_name  Variable to store the field name
3657a982d89SJeremy L. Thompson 
3667a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3677a982d89SJeremy L. Thompson 
3687a982d89SJeremy L. Thompson   @ref Backend
3697a982d89SJeremy L. Thompson **/
370*d1d35e2fSjeremylt int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
371*d1d35e2fSjeremylt   *field_name = (char *)qf_field->field_name;
372e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3737a982d89SJeremy L. Thompson }
3747a982d89SJeremy L. Thompson 
3757a982d89SJeremy L. Thompson /**
3767a982d89SJeremy L. Thompson   @brief Get the number of components of a CeedQFunctionField
3777a982d89SJeremy L. Thompson 
378*d1d35e2fSjeremylt   @param qf_field   CeedQFunctionField
3797a982d89SJeremy L. Thompson   @param[out] size  Variable to store the size of the field
3807a982d89SJeremy L. Thompson 
3817a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3827a982d89SJeremy L. Thompson 
3837a982d89SJeremy L. Thompson   @ref Backend
3847a982d89SJeremy L. Thompson **/
385*d1d35e2fSjeremylt int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
386*d1d35e2fSjeremylt   *size = qf_field->size;
387e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3887a982d89SJeremy L. Thompson }
3897a982d89SJeremy L. Thompson 
3907a982d89SJeremy L. Thompson /**
3917a982d89SJeremy L. Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
3927a982d89SJeremy L. Thompson 
393*d1d35e2fSjeremylt   @param qf_field        CeedQFunctionField
394*d1d35e2fSjeremylt   @param[out] eval_mode  Variable to store the field evaluation mode
3957a982d89SJeremy L. Thompson 
3967a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3977a982d89SJeremy L. Thompson 
3987a982d89SJeremy L. Thompson   @ref Backend
3997a982d89SJeremy L. Thompson **/
400*d1d35e2fSjeremylt int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field,
401*d1d35e2fSjeremylt                                   CeedEvalMode *eval_mode) {
402*d1d35e2fSjeremylt   *eval_mode = qf_field->eval_mode;
403e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4047a982d89SJeremy L. Thompson }
4057a982d89SJeremy L. Thompson 
4067a982d89SJeremy L. Thompson /// @}
4077a982d89SJeremy L. Thompson 
4087a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4097a982d89SJeremy L. Thompson /// CeedQFunction Public API
4107a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4117a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
4127a982d89SJeremy L. Thompson /// @{
4137a982d89SJeremy L. Thompson 
4147a982d89SJeremy L. Thompson /**
4157a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
4167a982d89SJeremy L. Thompson 
4177a982d89SJeremy L. Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
418*d1d35e2fSjeremylt   @param vec_length  Vector length. Caller must ensure that number of quadrature
419*d1d35e2fSjeremylt                        points is a multiple of vec_length.
4207a982d89SJeremy L. Thompson   @param f           Function pointer to evaluate action at quadrature points.
4217a982d89SJeremy L. Thompson                        See \ref CeedQFunctionUser.
4227a982d89SJeremy L. Thompson   @param source      Absolute path to source of QFunction,
423c8d5b03cSJeremy L Thompson                        "\abs_path\file.h:function_name".
424c8d5b03cSJeremy L Thompson                        For support across all backends, this source must only
425c8d5b03cSJeremy L Thompson                        contain constructs supported by C99, C++11, and CUDA.
4267a982d89SJeremy L. Thompson   @param[out] qf     Address of the variable where the newly created
4277a982d89SJeremy L. Thompson                        CeedQFunction will be stored
4287a982d89SJeremy L. Thompson 
4297a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4307a982d89SJeremy L. Thompson 
4317a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
4327a982d89SJeremy L. Thompson     arguments.
4337a982d89SJeremy L. Thompson 
4347a982d89SJeremy L. Thompson   @ref User
4357a982d89SJeremy L. Thompson **/
436*d1d35e2fSjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length,
437*d1d35e2fSjeremylt                                 CeedQFunctionUser f,
4387a982d89SJeremy L. Thompson                                 const char *source, CeedQFunction *qf) {
4397a982d89SJeremy L. Thompson   int ierr;
4407a982d89SJeremy L. Thompson   char *source_copy;
4417a982d89SJeremy L. Thompson 
4427a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
4437a982d89SJeremy L. Thompson     Ceed delegate;
4447a982d89SJeremy L. Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
4457a982d89SJeremy L. Thompson 
4467a982d89SJeremy L. Thompson     if (!delegate)
4477a982d89SJeremy L. Thompson       // LCOV_EXCL_START
448e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
449e15f9bd0SJeremy L Thompson                        "Backend does not support QFunctionCreate");
4507a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
4517a982d89SJeremy L. Thompson 
452*d1d35e2fSjeremylt     ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf);
4537a982d89SJeremy L. Thompson     CeedChk(ierr);
454e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
4557a982d89SJeremy L. Thompson   }
4567a982d89SJeremy L. Thompson 
4577a982d89SJeremy L. Thompson   ierr = CeedCalloc(1, qf); CeedChk(ierr);
4587a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
459*d1d35e2fSjeremylt   ceed->ref_count++;
460*d1d35e2fSjeremylt   (*qf)->ref_count = 1;
461*d1d35e2fSjeremylt   (*qf)->vec_length = vec_length;
4627a982d89SJeremy L. Thompson   (*qf)->identity = 0;
4637a982d89SJeremy L. Thompson   (*qf)->function = f;
4647a982d89SJeremy L. Thompson   size_t slen = strlen(source) + 1;
4657a982d89SJeremy L. Thompson   ierr = CeedMalloc(slen, &source_copy); CeedChk(ierr);
4667a982d89SJeremy L. Thompson   memcpy(source_copy, source, slen);
467*d1d35e2fSjeremylt   (*qf)->source_path = source_copy;
468*d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->input_fields); CeedChk(ierr);
469*d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->output_fields); CeedChk(ierr);
4707a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
471e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4727a982d89SJeremy L. Thompson }
4737a982d89SJeremy L. Thompson 
4747a982d89SJeremy L. Thompson /**
475288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
476288c0443SJeremy L Thompson 
477288c0443SJeremy L Thompson   @param ceed     A Ceed object where the CeedQFunction will be created
478288c0443SJeremy L Thompson   @param name     Name of QFunction to use from gallery
479288c0443SJeremy L Thompson   @param[out] qf  Address of the variable where the newly created
480288c0443SJeremy L Thompson                     CeedQFunction will be stored
481288c0443SJeremy L Thompson 
482288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
483288c0443SJeremy L Thompson 
4847a982d89SJeremy L. Thompson   @ref User
485288c0443SJeremy L Thompson **/
486288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
487288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
488288c0443SJeremy L Thompson   int ierr;
489*d1d35e2fSjeremylt   size_t match_len = 0, match_idx = UINT_MAX;
49075affc3bSjeremylt   char *name_copy;
491288c0443SJeremy L Thompson 
4921d013790SJed Brown   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
493288c0443SJeremy L Thompson   // Find matching backend
494e15f9bd0SJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE,
495e15f9bd0SJeremy L Thompson                                 "No QFunction name provided");
496288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
497288c0443SJeremy L Thompson     size_t n;
498*d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
499*d1d35e2fSjeremylt     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {}
500*d1d35e2fSjeremylt     if (n > match_len) {
501*d1d35e2fSjeremylt       match_len = n;
502*d1d35e2fSjeremylt       match_idx = i;
503288c0443SJeremy L Thompson     }
504288c0443SJeremy L Thompson   }
505*d1d35e2fSjeremylt   if (!match_len)
506138d4072Sjeremylt     // LCOV_EXCL_START
507e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
508138d4072Sjeremylt   // LCOV_EXCL_STOP
509288c0443SJeremy L Thompson 
510288c0443SJeremy L Thompson   // Create QFunction
511*d1d35e2fSjeremylt   ierr = CeedQFunctionCreateInterior(ceed,
512*d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].vec_length,
513*d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].f,
514*d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].source, qf);
515288c0443SJeremy L Thompson   CeedChk(ierr);
516288c0443SJeremy L Thompson 
517288c0443SJeremy L Thompson   // QFunction specific setup
518*d1d35e2fSjeremylt   ierr = gallery_qfunctions[match_idx].init(ceed, name, *qf); CeedChk(ierr);
519288c0443SJeremy L Thompson 
52075affc3bSjeremylt   // Copy name
52175affc3bSjeremylt   size_t slen = strlen(name) + 1;
52275affc3bSjeremylt   ierr = CeedMalloc(slen, &name_copy); CeedChk(ierr);
52375affc3bSjeremylt   memcpy(name_copy, name, slen);
524*d1d35e2fSjeremylt   (*qf)->qf_name = name_copy;
525e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
526288c0443SJeremy L Thompson }
527288c0443SJeremy L Thompson 
528288c0443SJeremy L Thompson /**
5290219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
5300219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
5310219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
5320219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
5330219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
5340219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
5350219ea01SJeremy L Thompson            location for both.
5360219ea01SJeremy L Thompson 
5370219ea01SJeremy L Thompson   @param ceed          A Ceed object where the CeedQFunction will be created
538*d1d35e2fSjeremylt   @param[in] size      Size of the QFunction fields
539*d1d35e2fSjeremylt   @param[in] in_mode   CeedEvalMode for input to CeedQFunction
540*d1d35e2fSjeremylt   @param[in] out_mode  CeedEvalMode for output to CeedQFunction
5410219ea01SJeremy L Thompson   @param[out] qf       Address of the variable where the newly created
5420219ea01SJeremy L Thompson                          CeedQFunction will be stored
5430219ea01SJeremy L Thompson 
5440219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5450219ea01SJeremy L Thompson 
5467a982d89SJeremy L. Thompson   @ref User
5470219ea01SJeremy L Thompson **/
548*d1d35e2fSjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode,
549*d1d35e2fSjeremylt                                 CeedEvalMode out_mode, CeedQFunction *qf) {
5500219ea01SJeremy L Thompson   int ierr;
5510219ea01SJeremy L Thompson 
552*d1d35e2fSjeremylt   if (in_mode == CEED_EVAL_NONE && out_mode == CEED_EVAL_NONE)
55360f77c51Sjeremylt     // LCOV_EXCL_START
554e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
555e15f9bd0SJeremy L Thompson                      "CEED_EVAL_NONE for a both the input and "
55660f77c51Sjeremylt                      "output does not make sense with an identity QFunction");
55760f77c51Sjeremylt   // LCOV_EXCL_STOP
55860f77c51Sjeremylt 
5590219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
560*d1d35e2fSjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr);
561*d1d35e2fSjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr);
5620219ea01SJeremy L Thompson 
5630219ea01SJeremy L Thompson   (*qf)->identity = 1;
564*d1d35e2fSjeremylt   CeedInt *size_data;
565*d1d35e2fSjeremylt   ierr = CeedCalloc(1, &size_data); CeedChk(ierr);
566*d1d35e2fSjeremylt   size_data[0] = size;
567777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
568777ff853SJeremy L Thompson   ierr = CeedQFunctionContextCreate(ceed, &ctx); CeedChk(ierr);
569777ff853SJeremy L Thompson   ierr = CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_OWN_POINTER,
570*d1d35e2fSjeremylt                                      sizeof(*size_data), (void *)size_data);
571777ff853SJeremy L Thompson   CeedChk(ierr);
572777ff853SJeremy L Thompson   ierr = CeedQFunctionSetContext(*qf, ctx); CeedChk(ierr);
573777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&ctx); CeedChk(ierr);
574e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5750219ea01SJeremy L Thompson }
5760219ea01SJeremy L Thompson 
5770219ea01SJeremy L Thompson /**
578a0a97fcfSJed Brown   @brief Add a CeedQFunction input
579b11c1e72Sjeremylt 
580b11c1e72Sjeremylt   @param qf          CeedQFunction
581*d1d35e2fSjeremylt   @param field_name  Name of QFunction field
582*d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
583*d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
584*d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
585b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
586b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
587b11c1e72Sjeremylt 
588b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
589dfdf5a53Sjeremylt 
5907a982d89SJeremy L. Thompson   @ref User
591b11c1e72Sjeremylt **/
592*d1d35e2fSjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name,
593*d1d35e2fSjeremylt                           CeedInt size,
594*d1d35e2fSjeremylt                           CeedEvalMode eval_mode) {
595*d1d35e2fSjeremylt   if (qf->operators_set)
596e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
597e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
598e15f9bd0SJeremy L Thompson                      "QFunction cannot be changed when in use by an operator");
599e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
600*d1d35e2fSjeremylt   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1))
601e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
602e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
603e15f9bd0SJeremy L Thompson                      "CEED_EVAL_WEIGHT should have size 1");
604e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
605*d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields],
606*d1d35e2fSjeremylt                                    field_name, size, eval_mode);
607fe2413ffSjeremylt   CeedChk(ierr);
608*d1d35e2fSjeremylt   qf->num_input_fields++;
609e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
610d7b241e6Sjeremylt }
611d7b241e6Sjeremylt 
612b11c1e72Sjeremylt /**
613a0a97fcfSJed Brown   @brief Add a CeedQFunction output
614b11c1e72Sjeremylt 
615b11c1e72Sjeremylt   @param qf          CeedQFunction
616*d1d35e2fSjeremylt   @param field_name  Name of QFunction field
617*d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
618*d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
619*d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
620b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
621b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
622b11c1e72Sjeremylt 
623b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
624dfdf5a53Sjeremylt 
6257a982d89SJeremy L. Thompson   @ref User
626b11c1e72Sjeremylt **/
627*d1d35e2fSjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name,
628*d1d35e2fSjeremylt                            CeedInt size, CeedEvalMode eval_mode) {
629*d1d35e2fSjeremylt   if (qf->operators_set)
630e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
631e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
632e15f9bd0SJeremy L Thompson                      "QFunction cannot be changed when in use by an operator");
633e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
634*d1d35e2fSjeremylt   if (eval_mode == CEED_EVAL_WEIGHT)
635c042f62fSJeremy L Thompson     // LCOV_EXCL_START
636e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
637e15f9bd0SJeremy L Thompson                      "Cannot create QFunction output with "
6381d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
639c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
640*d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields],
641*d1d35e2fSjeremylt                                    field_name, size, eval_mode);
642fe2413ffSjeremylt   CeedChk(ierr);
643*d1d35e2fSjeremylt   qf->num_output_fields++;
644e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
645d7b241e6Sjeremylt }
646d7b241e6Sjeremylt 
647dfdf5a53Sjeremylt /**
6484ce2993fSjeremylt   @brief Set global context for a CeedQFunction
649b11c1e72Sjeremylt 
650b11c1e72Sjeremylt   @param qf   CeedQFunction
651b11c1e72Sjeremylt   @param ctx  Context data to set
652b11c1e72Sjeremylt 
653b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
654dfdf5a53Sjeremylt 
6557a982d89SJeremy L. Thompson   @ref User
656b11c1e72Sjeremylt **/
657777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
658d7b241e6Sjeremylt   qf->ctx = ctx;
659*d1d35e2fSjeremylt   ctx->ref_count++;
660e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
661d7b241e6Sjeremylt }
662d7b241e6Sjeremylt 
663b11c1e72Sjeremylt /**
66475affc3bSjeremylt   @brief View a CeedQFunction
66575affc3bSjeremylt 
66675affc3bSjeremylt   @param[in] qf      CeedQFunction to view
66775affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
66875affc3bSjeremylt 
66975affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
67075affc3bSjeremylt 
6717a982d89SJeremy L. Thompson   @ref User
67275affc3bSjeremylt **/
67375affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
67475affc3bSjeremylt   int ierr;
67575affc3bSjeremylt 
67675affc3bSjeremylt   fprintf(stream, "%sCeedQFunction %s\n",
677*d1d35e2fSjeremylt           qf->qf_name ? "Gallery " : "User ", qf->qf_name ? qf->qf_name : "");
67875affc3bSjeremylt 
679*d1d35e2fSjeremylt   fprintf(stream, "  %d Input Field%s:\n", qf->num_input_fields,
680*d1d35e2fSjeremylt           qf->num_input_fields>1 ? "s" : "");
681*d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_input_fields; i++) {
682*d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream);
6832da88da5Sjeremylt     CeedChk(ierr);
68475affc3bSjeremylt   }
68575affc3bSjeremylt 
686*d1d35e2fSjeremylt   fprintf(stream, "  %d Output Field%s:\n", qf->num_output_fields,
687*d1d35e2fSjeremylt           qf->num_output_fields>1 ? "s" : "");
688*d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_output_fields; i++) {
689*d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream);
69075affc3bSjeremylt     CeedChk(ierr);
69175affc3bSjeremylt   }
692e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
69375affc3bSjeremylt }
69475affc3bSjeremylt 
69575affc3bSjeremylt /**
696b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
697b11c1e72Sjeremylt 
698b11c1e72Sjeremylt   @param qf      CeedQFunction
699b11c1e72Sjeremylt   @param Q       Number of quadrature points
70034138859Sjeremylt   @param[in] u   Array of input CeedVectors
70134138859Sjeremylt   @param[out] v  Array of output CeedVectors
702b11c1e72Sjeremylt 
703b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
704dfdf5a53Sjeremylt 
7057a982d89SJeremy L. Thompson   @ref User
706b11c1e72Sjeremylt **/
707d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
708aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
709d7b241e6Sjeremylt   int ierr;
710d7b241e6Sjeremylt   if (!qf->Apply)
711c042f62fSJeremy L Thompson     // LCOV_EXCL_START
712e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED,
713e15f9bd0SJeremy L Thompson                      "Backend does not support QFunctionApply");
714c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
715*d1d35e2fSjeremylt   if (Q % qf->vec_length)
716c042f62fSJeremy L Thompson     // LCOV_EXCL_START
717e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
718e15f9bd0SJeremy L Thompson                      "Number of quadrature points %d must be a "
719*d1d35e2fSjeremylt                      "multiple of %d", Q, qf->vec_length);
720c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
721d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
722e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
723d7b241e6Sjeremylt }
724d7b241e6Sjeremylt 
725b11c1e72Sjeremylt /**
726b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
727b11c1e72Sjeremylt 
728b11c1e72Sjeremylt   @param qf  CeedQFunction to destroy
729b11c1e72Sjeremylt 
730b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
731dfdf5a53Sjeremylt 
7327a982d89SJeremy L. Thompson   @ref User
733b11c1e72Sjeremylt **/
734d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
735d7b241e6Sjeremylt   int ierr;
736d7b241e6Sjeremylt 
737*d1d35e2fSjeremylt   if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS;
738fe2413ffSjeremylt   // Backend destroy
739d7b241e6Sjeremylt   if ((*qf)->Destroy) {
740d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
741d7b241e6Sjeremylt   }
742fe2413ffSjeremylt   // Free fields
743*d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_input_fields; i++) {
744*d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr);
745*d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr);
746fe2413ffSjeremylt   }
747*d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_output_fields; i++) {
748*d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr);
749*d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr);
750fe2413ffSjeremylt   }
751*d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr);
752*d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr);
753777ff853SJeremy L Thompson 
754777ff853SJeremy L Thompson   // User context data object
755777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
756fe2413ffSjeremylt 
757*d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr);
758*d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->qf_name); CeedChk(ierr);
759d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
760d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
761e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
762d7b241e6Sjeremylt }
763d7b241e6Sjeremylt 
764d7b241e6Sjeremylt /// @}
765