xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision 9560d06a92c065fb7d600a8c20ade8d9a4cda324)
1d7b241e6Sjeremylt // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2d7b241e6Sjeremylt // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3d7b241e6Sjeremylt // reserved. See files LICENSE and NOTICE for details.
4d7b241e6Sjeremylt //
5d7b241e6Sjeremylt // This file is part of CEED, a collection of benchmarks, miniapps, software
6d7b241e6Sjeremylt // libraries and APIs for efficient high-order finite element and spectral
7d7b241e6Sjeremylt // element discretizations for exascale applications. For more information and
8d7b241e6Sjeremylt // source code availability see http://github.com/ceed.
9d7b241e6Sjeremylt //
10d7b241e6Sjeremylt // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11d7b241e6Sjeremylt // a collaborative effort of two U.S. Department of Energy organizations (Office
12d7b241e6Sjeremylt // of Science and the National Nuclear Security Administration) responsible for
13d7b241e6Sjeremylt // the planning and preparation of a capable exascale ecosystem, including
14d7b241e6Sjeremylt // software, applications, hardware, advanced system engineering and early
15d7b241e6Sjeremylt // testbed platforms, in support of the nation's exascale computing imperative.
16d7b241e6Sjeremylt 
17ec3da8bcSJed Brown #include <ceed/ceed.h>
18ec3da8bcSJed Brown #include <ceed/backend.h>
193d576824SJeremy L Thompson #include <ceed-impl.h>
20288c0443SJeremy L Thompson #include <limits.h>
213d576824SJeremy L Thompson #include <stdbool.h>
223d576824SJeremy L Thompson #include <stdio.h>
233d576824SJeremy L Thompson #include <string.h>
24288c0443SJeremy L Thompson 
257a982d89SJeremy L. Thompson /// @file
267a982d89SJeremy L. Thompson /// Implementation of public CeedQFunction interfaces
277a982d89SJeremy L. Thompson 
28288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP
29442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none;
30442e7f0bSjeremylt /// @endcond
31442e7f0bSjeremylt 
327a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
337a982d89SJeremy L. Thompson /// @{
347a982d89SJeremy L. Thompson 
357a982d89SJeremy L. Thompson // Indicate that no QFunction is provided by the user
367a982d89SJeremy L. Thompson const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
377a982d89SJeremy L. Thompson 
387a982d89SJeremy L. Thompson /// @}
397a982d89SJeremy L. Thompson 
40442e7f0bSjeremylt /// @cond DOXYGEN_SKIP
41288c0443SJeremy L Thompson static struct {
42288c0443SJeremy L Thompson   char name[CEED_MAX_RESOURCE_LEN];
43288c0443SJeremy L Thompson   char source[CEED_MAX_RESOURCE_LEN];
44d1d35e2fSjeremylt   CeedInt vec_length;
45288c0443SJeremy L Thompson   CeedQFunctionUser f;
46288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
47d1d35e2fSjeremylt } gallery_qfunctions[1024];
48288c0443SJeremy L Thompson static size_t num_qfunctions;
49288c0443SJeremy L Thompson /// @endcond
50d7b241e6Sjeremylt 
51777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
52777ff853SJeremy L Thompson /// CeedQFunction Library Internal Functions
53777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
54777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper
55777ff853SJeremy L Thompson /// @{
56777ff853SJeremy L Thompson 
57b11c1e72Sjeremylt /**
58288c0443SJeremy L Thompson   @brief Register a gallery QFunction
59288c0443SJeremy L Thompson 
60288c0443SJeremy L Thompson   @param name        Name for this backend to respond to
611176cc3aSJeremy L Thompson   @param source      Absolute path to source of QFunction,
621176cc3aSJeremy L Thompson                        "\path\CEED_DIR\gallery\folder\file.h:function_name"
63d1d35e2fSjeremylt   @param vec_length  Vector length.  Caller must ensure that number of quadrature
64d1d35e2fSjeremylt                        points is a multiple of vec_length.
65288c0443SJeremy L Thompson   @param f           Function pointer to evaluate action at quadrature points.
66288c0443SJeremy L Thompson                        See \ref CeedQFunctionUser.
67288c0443SJeremy L Thompson   @param init        Initialization function called by CeedQFunctionInit() when the
68288c0443SJeremy L Thompson                        QFunction is selected.
69288c0443SJeremy L Thompson 
70288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
71288c0443SJeremy L Thompson 
727a982d89SJeremy L. Thompson   @ref Developer
73288c0443SJeremy L Thompson **/
74288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source,
75d1d35e2fSjeremylt                           CeedInt vec_length, CeedQFunctionUser f,
76288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
77d1d35e2fSjeremylt   if (num_qfunctions >= sizeof(gallery_qfunctions) / sizeof(
78d1d35e2fSjeremylt         gallery_qfunctions[0]))
79c042f62fSJeremy L Thompson     // LCOV_EXCL_START
80e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
81c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
82c042f62fSJeremy L Thompson 
83d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
84d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
85d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].source, source,
86d1d35e2fSjeremylt           CEED_MAX_RESOURCE_LEN);
87d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
88d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].vec_length = vec_length;
89d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].f = f;
90d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].init = init;
91288c0443SJeremy L Thompson   num_qfunctions++;
92e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
93288c0443SJeremy L Thompson }
94288c0443SJeremy L Thompson 
95288c0443SJeremy L Thompson /**
967a982d89SJeremy L. Thompson   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
977a982d89SJeremy L. Thompson 
987a982d89SJeremy L. Thompson   @param f           CeedQFunctionField
99d1d35e2fSjeremylt   @param field_name  Name of QFunction field
100d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
101d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT
102d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
1037a982d89SJeremy L. Thompson                        \ref CEED_EVAL_INTERP to use interpolated values,
1047a982d89SJeremy L. Thompson                        \ref CEED_EVAL_GRAD to use gradients,
1057a982d89SJeremy L. Thompson                        \ref CEED_EVAL_WEIGHT to use quadrature weights.
1067a982d89SJeremy L. Thompson 
1077a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1087a982d89SJeremy L. Thompson 
1097a982d89SJeremy L. Thompson   @ref Developer
1107a982d89SJeremy L. Thompson **/
111d1d35e2fSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *field_name,
112d1d35e2fSjeremylt                                  CeedInt size, CeedEvalMode eval_mode) {
113d1d35e2fSjeremylt   size_t len = strlen(field_name);
1147a982d89SJeremy L. Thompson   char *tmp;
1157a982d89SJeremy L. Thompson   int ierr;
1167a982d89SJeremy L. Thompson 
117e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, f); CeedChk(ierr);
1187a982d89SJeremy L. Thompson   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
119d1d35e2fSjeremylt   memcpy(tmp, field_name, len+1);
120d1d35e2fSjeremylt   (*f)->field_name = tmp;
1217a982d89SJeremy L. Thompson   (*f)->size = size;
122d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
123e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1247a982d89SJeremy L. Thompson }
1257a982d89SJeremy L. Thompson 
1267a982d89SJeremy L. Thompson /**
1277a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1287a982d89SJeremy L. Thompson 
1297a982d89SJeremy L. Thompson   @param[in] field         QFunction field to view
130d1d35e2fSjeremylt   @param[in] field_number  Number of field being viewed
1314c4400c7SValeria Barra   @param[in] in            true for input field, false for output
1327a982d89SJeremy L. Thompson   @param[in] stream        Stream to view to, e.g., stdout
1337a982d89SJeremy L. Thompson 
1347a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1357a982d89SJeremy L. Thompson 
1367a982d89SJeremy L. Thompson   @ref Utility
1377a982d89SJeremy L. Thompson **/
138d1d35e2fSjeremylt static int CeedQFunctionFieldView(CeedQFunctionField field,
139d1d35e2fSjeremylt                                   CeedInt field_number,
1407a982d89SJeremy L. Thompson                                   bool in, FILE *stream) {
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",
146d1d35e2fSjeremylt           inout, field_number, field->field_name, field->size,
147d1d35e2fSjeremylt           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) {
162d1d35e2fSjeremylt   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
193d1d35e2fSjeremylt   @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 **/
199d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
200d1d35e2fSjeremylt   *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
208d1d35e2fSjeremylt   @param[out] num_input   Variable to store number of input fields
209d1d35e2fSjeremylt   @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 **/
215d1d35e2fSjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input,
216d1d35e2fSjeremylt                             CeedInt *num_output) {
217d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
218d1d35e2fSjeremylt   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) {
233d1d35e2fSjeremylt   *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;
284d1d35e2fSjeremylt   if (qf->fortran_status) {
285d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
286d1d35e2fSjeremylt     ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx);
287777ff853SJeremy L Thompson     CeedChk(ierr);
288d1d35e2fSjeremylt     *ctx = fortran_ctx->innerctx;
289d1d35e2fSjeremylt     ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx);
290d1d35e2fSjeremylt     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
301d1d35e2fSjeremylt   @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 **/
307d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
308d1d35e2fSjeremylt   *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 /**
34334359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
34434359f16Sjeremylt 
34534359f16Sjeremylt   @param qf  CeedQFunction to increment the reference counter
34634359f16Sjeremylt 
34734359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
34834359f16Sjeremylt 
34934359f16Sjeremylt   @ref Backend
35034359f16Sjeremylt **/
351*9560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
35234359f16Sjeremylt   qf->ref_count++;
35334359f16Sjeremylt   return CEED_ERROR_SUCCESS;
35434359f16Sjeremylt }
35534359f16Sjeremylt 
35634359f16Sjeremylt /**
3577a982d89SJeremy L. Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
3587a982d89SJeremy L. Thompson 
3597a982d89SJeremy L. Thompson   @param qf                  CeedQFunction
360d1d35e2fSjeremylt   @param[out] input_fields   Variable to store input_fields
361d1d35e2fSjeremylt   @param[out] output_fields  Variable to store output_fields
3627a982d89SJeremy L. Thompson 
3637a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3647a982d89SJeremy L. Thompson 
3657a982d89SJeremy L. Thompson   @ref Backend
3667a982d89SJeremy L. Thompson **/
367d1d35e2fSjeremylt int CeedQFunctionGetFields(CeedQFunction qf, CeedQFunctionField **input_fields,
368d1d35e2fSjeremylt                            CeedQFunctionField **output_fields) {
369d1d35e2fSjeremylt   if (input_fields) *input_fields = qf->input_fields;
370d1d35e2fSjeremylt   if (output_fields) *output_fields = qf->output_fields;
371e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3727a982d89SJeremy L. Thompson }
3737a982d89SJeremy L. Thompson 
3747a982d89SJeremy L. Thompson /**
3757a982d89SJeremy L. Thompson   @brief Get the name of a CeedQFunctionField
3767a982d89SJeremy L. Thompson 
377d1d35e2fSjeremylt   @param qf_field         CeedQFunctionField
378d1d35e2fSjeremylt   @param[out] field_name  Variable to store the field name
3797a982d89SJeremy L. Thompson 
3807a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3817a982d89SJeremy L. Thompson 
3827a982d89SJeremy L. Thompson   @ref Backend
3837a982d89SJeremy L. Thompson **/
384d1d35e2fSjeremylt int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
385d1d35e2fSjeremylt   *field_name = (char *)qf_field->field_name;
386e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3877a982d89SJeremy L. Thompson }
3887a982d89SJeremy L. Thompson 
3897a982d89SJeremy L. Thompson /**
3907a982d89SJeremy L. Thompson   @brief Get the number of components of a CeedQFunctionField
3917a982d89SJeremy L. Thompson 
392d1d35e2fSjeremylt   @param qf_field   CeedQFunctionField
3937a982d89SJeremy L. Thompson   @param[out] size  Variable to store the size of the field
3947a982d89SJeremy L. Thompson 
3957a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3967a982d89SJeremy L. Thompson 
3977a982d89SJeremy L. Thompson   @ref Backend
3987a982d89SJeremy L. Thompson **/
399d1d35e2fSjeremylt int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
400d1d35e2fSjeremylt   *size = qf_field->size;
401e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4027a982d89SJeremy L. Thompson }
4037a982d89SJeremy L. Thompson 
4047a982d89SJeremy L. Thompson /**
4057a982d89SJeremy L. Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
4067a982d89SJeremy L. Thompson 
407d1d35e2fSjeremylt   @param qf_field        CeedQFunctionField
408d1d35e2fSjeremylt   @param[out] eval_mode  Variable to store the field evaluation mode
4097a982d89SJeremy L. Thompson 
4107a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4117a982d89SJeremy L. Thompson 
4127a982d89SJeremy L. Thompson   @ref Backend
4137a982d89SJeremy L. Thompson **/
414d1d35e2fSjeremylt int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field,
415d1d35e2fSjeremylt                                   CeedEvalMode *eval_mode) {
416d1d35e2fSjeremylt   *eval_mode = qf_field->eval_mode;
417e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4187a982d89SJeremy L. Thompson }
4197a982d89SJeremy L. Thompson 
4207a982d89SJeremy L. Thompson /// @}
4217a982d89SJeremy L. Thompson 
4227a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4237a982d89SJeremy L. Thompson /// CeedQFunction Public API
4247a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4257a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
4267a982d89SJeremy L. Thompson /// @{
4277a982d89SJeremy L. Thompson 
4287a982d89SJeremy L. Thompson /**
4297a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
4307a982d89SJeremy L. Thompson 
4317a982d89SJeremy L. Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
432d1d35e2fSjeremylt   @param vec_length  Vector length. Caller must ensure that number of quadrature
433d1d35e2fSjeremylt                        points is a multiple of vec_length.
4347a982d89SJeremy L. Thompson   @param f           Function pointer to evaluate action at quadrature points.
4357a982d89SJeremy L. Thompson                        See \ref CeedQFunctionUser.
4367a982d89SJeremy L. Thompson   @param source      Absolute path to source of QFunction,
437c8d5b03cSJeremy L Thompson                        "\abs_path\file.h:function_name".
438c8d5b03cSJeremy L Thompson                        For support across all backends, this source must only
439c8d5b03cSJeremy L Thompson                        contain constructs supported by C99, C++11, and CUDA.
4407a982d89SJeremy L. Thompson   @param[out] qf     Address of the variable where the newly created
4417a982d89SJeremy L. Thompson                        CeedQFunction will be stored
4427a982d89SJeremy L. Thompson 
4437a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4447a982d89SJeremy L. Thompson 
4457a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
4467a982d89SJeremy L. Thompson     arguments.
4477a982d89SJeremy L. Thompson 
4487a982d89SJeremy L. Thompson   @ref User
4497a982d89SJeremy L. Thompson **/
450d1d35e2fSjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length,
451d1d35e2fSjeremylt                                 CeedQFunctionUser f,
4527a982d89SJeremy L. Thompson                                 const char *source, CeedQFunction *qf) {
4537a982d89SJeremy L. Thompson   int ierr;
4547a982d89SJeremy L. Thompson   char *source_copy;
4557a982d89SJeremy L. Thompson 
4567a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
4577a982d89SJeremy L. Thompson     Ceed delegate;
4587a982d89SJeremy L. Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
4597a982d89SJeremy L. Thompson 
4607a982d89SJeremy L. Thompson     if (!delegate)
4617a982d89SJeremy L. Thompson       // LCOV_EXCL_START
462e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
463e15f9bd0SJeremy L Thompson                        "Backend does not support QFunctionCreate");
4647a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
4657a982d89SJeremy L. Thompson 
466d1d35e2fSjeremylt     ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf);
4677a982d89SJeremy L. Thompson     CeedChk(ierr);
468e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
4697a982d89SJeremy L. Thompson   }
4707a982d89SJeremy L. Thompson 
4717a982d89SJeremy L. Thompson   ierr = CeedCalloc(1, qf); CeedChk(ierr);
4727a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
473*9560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
474d1d35e2fSjeremylt   (*qf)->ref_count = 1;
475d1d35e2fSjeremylt   (*qf)->vec_length = vec_length;
4767a982d89SJeremy L. Thompson   (*qf)->identity = 0;
4777a982d89SJeremy L. Thompson   (*qf)->function = f;
4787a982d89SJeremy L. Thompson   size_t slen = strlen(source) + 1;
4797a982d89SJeremy L. Thompson   ierr = CeedMalloc(slen, &source_copy); CeedChk(ierr);
4807a982d89SJeremy L. Thompson   memcpy(source_copy, source, slen);
481d1d35e2fSjeremylt   (*qf)->source_path = source_copy;
482d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->input_fields); CeedChk(ierr);
483d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->output_fields); CeedChk(ierr);
4847a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
485e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4867a982d89SJeremy L. Thompson }
4877a982d89SJeremy L. Thompson 
4887a982d89SJeremy L. Thompson /**
489288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
490288c0443SJeremy L Thompson 
491288c0443SJeremy L Thompson   @param ceed     A Ceed object where the CeedQFunction will be created
492288c0443SJeremy L Thompson   @param name     Name of QFunction to use from gallery
493288c0443SJeremy L Thompson   @param[out] qf  Address of the variable where the newly created
494288c0443SJeremy L Thompson                     CeedQFunction will be stored
495288c0443SJeremy L Thompson 
496288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
497288c0443SJeremy L Thompson 
4987a982d89SJeremy L. Thompson   @ref User
499288c0443SJeremy L Thompson **/
500288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
501288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
502288c0443SJeremy L Thompson   int ierr;
503d1d35e2fSjeremylt   size_t match_len = 0, match_idx = UINT_MAX;
50475affc3bSjeremylt   char *name_copy;
505288c0443SJeremy L Thompson 
5061d013790SJed Brown   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
507288c0443SJeremy L Thompson   // Find matching backend
508e15f9bd0SJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE,
509e15f9bd0SJeremy L Thompson                                 "No QFunction name provided");
510288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
511288c0443SJeremy L Thompson     size_t n;
512d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
513d1d35e2fSjeremylt     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {}
514d1d35e2fSjeremylt     if (n > match_len) {
515d1d35e2fSjeremylt       match_len = n;
516d1d35e2fSjeremylt       match_idx = i;
517288c0443SJeremy L Thompson     }
518288c0443SJeremy L Thompson   }
519d1d35e2fSjeremylt   if (!match_len)
520138d4072Sjeremylt     // LCOV_EXCL_START
521e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
522138d4072Sjeremylt   // LCOV_EXCL_STOP
523288c0443SJeremy L Thompson 
524288c0443SJeremy L Thompson   // Create QFunction
525d1d35e2fSjeremylt   ierr = CeedQFunctionCreateInterior(ceed,
526d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].vec_length,
527d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].f,
528d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].source, qf);
529288c0443SJeremy L Thompson   CeedChk(ierr);
530288c0443SJeremy L Thompson 
531288c0443SJeremy L Thompson   // QFunction specific setup
532d1d35e2fSjeremylt   ierr = gallery_qfunctions[match_idx].init(ceed, name, *qf); CeedChk(ierr);
533288c0443SJeremy L Thompson 
53475affc3bSjeremylt   // Copy name
53575affc3bSjeremylt   size_t slen = strlen(name) + 1;
53675affc3bSjeremylt   ierr = CeedMalloc(slen, &name_copy); CeedChk(ierr);
53775affc3bSjeremylt   memcpy(name_copy, name, slen);
538d1d35e2fSjeremylt   (*qf)->qf_name = name_copy;
539e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
540288c0443SJeremy L Thompson }
541288c0443SJeremy L Thompson 
542288c0443SJeremy L Thompson /**
5430219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
5440219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
5450219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
5460219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
5470219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
5480219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
5490219ea01SJeremy L Thompson            location for both.
5500219ea01SJeremy L Thompson 
5510219ea01SJeremy L Thompson   @param ceed          A Ceed object where the CeedQFunction will be created
552d1d35e2fSjeremylt   @param[in] size      Size of the QFunction fields
553d1d35e2fSjeremylt   @param[in] in_mode   CeedEvalMode for input to CeedQFunction
554d1d35e2fSjeremylt   @param[in] out_mode  CeedEvalMode for output to CeedQFunction
5550219ea01SJeremy L Thompson   @param[out] qf       Address of the variable where the newly created
5560219ea01SJeremy L Thompson                          CeedQFunction will be stored
5570219ea01SJeremy L Thompson 
5580219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5590219ea01SJeremy L Thompson 
5607a982d89SJeremy L. Thompson   @ref User
5610219ea01SJeremy L Thompson **/
562d1d35e2fSjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode,
563d1d35e2fSjeremylt                                 CeedEvalMode out_mode, CeedQFunction *qf) {
5640219ea01SJeremy L Thompson   int ierr;
5650219ea01SJeremy L Thompson 
566d1d35e2fSjeremylt   if (in_mode == CEED_EVAL_NONE && out_mode == CEED_EVAL_NONE)
56760f77c51Sjeremylt     // LCOV_EXCL_START
568e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
569e15f9bd0SJeremy L Thompson                      "CEED_EVAL_NONE for a both the input and "
57060f77c51Sjeremylt                      "output does not make sense with an identity QFunction");
57160f77c51Sjeremylt   // LCOV_EXCL_STOP
57260f77c51Sjeremylt 
5730219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
574d1d35e2fSjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr);
575d1d35e2fSjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr);
5760219ea01SJeremy L Thompson 
5770219ea01SJeremy L Thompson   (*qf)->identity = 1;
578d1d35e2fSjeremylt   CeedInt *size_data;
579d1d35e2fSjeremylt   ierr = CeedCalloc(1, &size_data); CeedChk(ierr);
580d1d35e2fSjeremylt   size_data[0] = size;
581777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
582777ff853SJeremy L Thompson   ierr = CeedQFunctionContextCreate(ceed, &ctx); CeedChk(ierr);
583777ff853SJeremy L Thompson   ierr = CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_OWN_POINTER,
584d1d35e2fSjeremylt                                      sizeof(*size_data), (void *)size_data);
585777ff853SJeremy L Thompson   CeedChk(ierr);
586777ff853SJeremy L Thompson   ierr = CeedQFunctionSetContext(*qf, ctx); CeedChk(ierr);
587777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&ctx); CeedChk(ierr);
588e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5890219ea01SJeremy L Thompson }
5900219ea01SJeremy L Thompson 
5910219ea01SJeremy L Thompson /**
592*9560d06aSjeremylt   @brief Copy the pointer to a CeedQFunction. Both pointers should
593*9560d06aSjeremylt            be destroyed with `CeedQFunctionDestroy()`;
594*9560d06aSjeremylt            Note: If `*qf_copy` is non-NULL, then it is assumed that
595*9560d06aSjeremylt            `*qf_copy` is a pointer to a CeedQFunction. This
596*9560d06aSjeremylt            CeedQFunction will be destroyed if `*qf_copy` is the only
597*9560d06aSjeremylt            reference to this CeedQFunction.
598*9560d06aSjeremylt 
599*9560d06aSjeremylt   @param qf            CeedQFunction to copy reference to
600*9560d06aSjeremylt   @param[out] qf_copy  Variable to store copied reference
601*9560d06aSjeremylt 
602*9560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
603*9560d06aSjeremylt 
604*9560d06aSjeremylt   @ref User
605*9560d06aSjeremylt **/
606*9560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
607*9560d06aSjeremylt   int ierr;
608*9560d06aSjeremylt 
609*9560d06aSjeremylt   ierr = CeedQFunctionReference(qf); CeedChk(ierr);
610*9560d06aSjeremylt   ierr = CeedQFunctionDestroy(qf_copy); CeedChk(ierr);
611*9560d06aSjeremylt   *qf_copy = qf;
612*9560d06aSjeremylt   return CEED_ERROR_SUCCESS;
613*9560d06aSjeremylt }
614*9560d06aSjeremylt 
615*9560d06aSjeremylt /**
616a0a97fcfSJed Brown   @brief Add a CeedQFunction input
617b11c1e72Sjeremylt 
618b11c1e72Sjeremylt   @param qf          CeedQFunction
619d1d35e2fSjeremylt   @param field_name  Name of QFunction field
620d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
621d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
622d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
623b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
624b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
625b11c1e72Sjeremylt 
626b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
627dfdf5a53Sjeremylt 
6287a982d89SJeremy L. Thompson   @ref User
629b11c1e72Sjeremylt **/
630d1d35e2fSjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name,
631d1d35e2fSjeremylt                           CeedInt size,
632d1d35e2fSjeremylt                           CeedEvalMode eval_mode) {
633d1d35e2fSjeremylt   if (qf->operators_set)
634e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
635e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
636e15f9bd0SJeremy L Thompson                      "QFunction cannot be changed when in use by an operator");
637e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
638d1d35e2fSjeremylt   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1))
639e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
640e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
641e15f9bd0SJeremy L Thompson                      "CEED_EVAL_WEIGHT should have size 1");
642e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
643d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields],
644d1d35e2fSjeremylt                                    field_name, size, eval_mode);
645fe2413ffSjeremylt   CeedChk(ierr);
646d1d35e2fSjeremylt   qf->num_input_fields++;
647e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
648d7b241e6Sjeremylt }
649d7b241e6Sjeremylt 
650b11c1e72Sjeremylt /**
651a0a97fcfSJed Brown   @brief Add a CeedQFunction output
652b11c1e72Sjeremylt 
653b11c1e72Sjeremylt   @param qf          CeedQFunction
654d1d35e2fSjeremylt   @param field_name  Name of QFunction field
655d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
656d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
657d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
658b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
659b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
660b11c1e72Sjeremylt 
661b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
662dfdf5a53Sjeremylt 
6637a982d89SJeremy L. Thompson   @ref User
664b11c1e72Sjeremylt **/
665d1d35e2fSjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name,
666d1d35e2fSjeremylt                            CeedInt size, CeedEvalMode eval_mode) {
667d1d35e2fSjeremylt   if (qf->operators_set)
668e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
669e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
670e15f9bd0SJeremy L Thompson                      "QFunction cannot be changed when in use by an operator");
671e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
672d1d35e2fSjeremylt   if (eval_mode == CEED_EVAL_WEIGHT)
673c042f62fSJeremy L Thompson     // LCOV_EXCL_START
674e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
675e15f9bd0SJeremy L Thompson                      "Cannot create QFunction output with "
6761d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
677c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
678d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields],
679d1d35e2fSjeremylt                                    field_name, size, eval_mode);
680fe2413ffSjeremylt   CeedChk(ierr);
681d1d35e2fSjeremylt   qf->num_output_fields++;
682e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
683d7b241e6Sjeremylt }
684d7b241e6Sjeremylt 
685dfdf5a53Sjeremylt /**
6864ce2993fSjeremylt   @brief Set global context for a CeedQFunction
687b11c1e72Sjeremylt 
688b11c1e72Sjeremylt   @param qf   CeedQFunction
689b11c1e72Sjeremylt   @param ctx  Context data to set
690b11c1e72Sjeremylt 
691b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
692dfdf5a53Sjeremylt 
6937a982d89SJeremy L. Thompson   @ref User
694b11c1e72Sjeremylt **/
695777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
69634359f16Sjeremylt   int ierr;
697d7b241e6Sjeremylt   qf->ctx = ctx;
698*9560d06aSjeremylt   ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr);
699e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
700d7b241e6Sjeremylt }
701d7b241e6Sjeremylt 
702b11c1e72Sjeremylt /**
70375affc3bSjeremylt   @brief View a CeedQFunction
70475affc3bSjeremylt 
70575affc3bSjeremylt   @param[in] qf      CeedQFunction to view
70675affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
70775affc3bSjeremylt 
70875affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
70975affc3bSjeremylt 
7107a982d89SJeremy L. Thompson   @ref User
71175affc3bSjeremylt **/
71275affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
71375affc3bSjeremylt   int ierr;
71475affc3bSjeremylt 
71575affc3bSjeremylt   fprintf(stream, "%sCeedQFunction %s\n",
716d1d35e2fSjeremylt           qf->qf_name ? "Gallery " : "User ", qf->qf_name ? qf->qf_name : "");
71775affc3bSjeremylt 
718d1d35e2fSjeremylt   fprintf(stream, "  %d Input Field%s:\n", qf->num_input_fields,
719d1d35e2fSjeremylt           qf->num_input_fields>1 ? "s" : "");
720d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_input_fields; i++) {
721d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream);
7222da88da5Sjeremylt     CeedChk(ierr);
72375affc3bSjeremylt   }
72475affc3bSjeremylt 
725d1d35e2fSjeremylt   fprintf(stream, "  %d Output Field%s:\n", qf->num_output_fields,
726d1d35e2fSjeremylt           qf->num_output_fields>1 ? "s" : "");
727d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_output_fields; i++) {
728d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream);
72975affc3bSjeremylt     CeedChk(ierr);
73075affc3bSjeremylt   }
731e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
73275affc3bSjeremylt }
73375affc3bSjeremylt 
73475affc3bSjeremylt /**
735b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
736b11c1e72Sjeremylt 
737b11c1e72Sjeremylt   @param qf      CeedQFunction
738b11c1e72Sjeremylt   @param Q       Number of quadrature points
73934138859Sjeremylt   @param[in] u   Array of input CeedVectors
74034138859Sjeremylt   @param[out] v  Array of output CeedVectors
741b11c1e72Sjeremylt 
742b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
743dfdf5a53Sjeremylt 
7447a982d89SJeremy L. Thompson   @ref User
745b11c1e72Sjeremylt **/
746d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
747aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
748d7b241e6Sjeremylt   int ierr;
749d7b241e6Sjeremylt   if (!qf->Apply)
750c042f62fSJeremy L Thompson     // LCOV_EXCL_START
751e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED,
752e15f9bd0SJeremy L Thompson                      "Backend does not support QFunctionApply");
753c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
754d1d35e2fSjeremylt   if (Q % qf->vec_length)
755c042f62fSJeremy L Thompson     // LCOV_EXCL_START
756e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
757e15f9bd0SJeremy L Thompson                      "Number of quadrature points %d must be a "
758d1d35e2fSjeremylt                      "multiple of %d", Q, qf->vec_length);
759c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
760d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
761e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
762d7b241e6Sjeremylt }
763d7b241e6Sjeremylt 
764b11c1e72Sjeremylt /**
765b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
766b11c1e72Sjeremylt 
767b11c1e72Sjeremylt   @param qf  CeedQFunction to destroy
768b11c1e72Sjeremylt 
769b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
770dfdf5a53Sjeremylt 
7717a982d89SJeremy L. Thompson   @ref User
772b11c1e72Sjeremylt **/
773d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
774d7b241e6Sjeremylt   int ierr;
775d7b241e6Sjeremylt 
776d1d35e2fSjeremylt   if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS;
777fe2413ffSjeremylt   // Backend destroy
778d7b241e6Sjeremylt   if ((*qf)->Destroy) {
779d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
780d7b241e6Sjeremylt   }
781fe2413ffSjeremylt   // Free fields
782d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_input_fields; i++) {
783d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr);
784d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr);
785fe2413ffSjeremylt   }
786d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_output_fields; i++) {
787d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr);
788d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr);
789fe2413ffSjeremylt   }
790d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr);
791d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr);
792777ff853SJeremy L Thompson 
793777ff853SJeremy L Thompson   // User context data object
794777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
795fe2413ffSjeremylt 
796d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr);
797d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->qf_name); CeedChk(ierr);
798d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
799d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
800e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
801d7b241e6Sjeremylt }
802d7b241e6Sjeremylt 
803d7b241e6Sjeremylt /// @}
804