xref: /libCEED/interface/ceed-qfunction.c (revision 8229195e79c6b6b0e33df6e05b918f262f144d58)
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) {
141*8229195eSjeremylt   int ierr;
1427a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
143*8229195eSjeremylt   char *field_name;
144*8229195eSjeremylt   ierr = CeedQFunctionFieldGetName(field, &field_name); CeedChk(ierr);
145*8229195eSjeremylt   CeedInt size;
146*8229195eSjeremylt   ierr = CeedQFunctionFieldGetSize(field, &size); CeedChk(ierr);
147*8229195eSjeremylt   CeedEvalMode eval_mode;
148*8229195eSjeremylt   ierr = CeedQFunctionFieldGetEvalMode(field, &eval_mode); CeedChk(ierr);
1497a982d89SJeremy L. Thompson   fprintf(stream, "    %s Field [%d]:\n"
1507a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1517a982d89SJeremy L. Thompson           "      Size: %d\n"
1527a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
153*8229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
154e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1557a982d89SJeremy L. Thompson }
1567a982d89SJeremy L. Thompson 
157777ff853SJeremy L Thompson /**
158777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
159777ff853SJeremy L Thompson 
160777ff853SJeremy L Thompson   @param qf      CeedQFunction
161777ff853SJeremy L Thompson   @param status  Boolean value to set as Fortran status
162777ff853SJeremy L Thompson 
163777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
164777ff853SJeremy L Thompson 
165777ff853SJeremy L Thompson   @ref Backend
166777ff853SJeremy L Thompson **/
167777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
168d1d35e2fSjeremylt   qf->fortran_status = status;
169e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
170777ff853SJeremy L Thompson }
171777ff853SJeremy L Thompson 
1727a982d89SJeremy L. Thompson /// @}
1737a982d89SJeremy L. Thompson 
1747a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1757a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1767a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1777a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1787a982d89SJeremy L. Thompson /// @{
1797a982d89SJeremy L. Thompson 
1807a982d89SJeremy L. Thompson /**
1817a982d89SJeremy L. Thompson   @brief Get the Ceed associated with a CeedQFunction
1827a982d89SJeremy L. Thompson 
1837a982d89SJeremy L. Thompson   @param qf              CeedQFunction
1847a982d89SJeremy L. Thompson   @param[out] ceed       Variable to store Ceed
1857a982d89SJeremy L. Thompson 
1867a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1877a982d89SJeremy L. Thompson 
1887a982d89SJeremy L. Thompson   @ref Backend
1897a982d89SJeremy L. Thompson **/
1907a982d89SJeremy L. Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
1917a982d89SJeremy L. Thompson   *ceed = qf->ceed;
192e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1937a982d89SJeremy L. Thompson }
1947a982d89SJeremy L. Thompson 
1957a982d89SJeremy L. Thompson /**
1967a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1977a982d89SJeremy L. Thompson 
1987a982d89SJeremy L. Thompson   @param qf               CeedQFunction
199d1d35e2fSjeremylt   @param[out] vec_length  Variable to store vector length
2007a982d89SJeremy L. Thompson 
2017a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2027a982d89SJeremy L. Thompson 
2037a982d89SJeremy L. Thompson   @ref Backend
2047a982d89SJeremy L. Thompson **/
205d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
206d1d35e2fSjeremylt   *vec_length = qf->vec_length;
207e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2087a982d89SJeremy L. Thompson }
2097a982d89SJeremy L. Thompson 
2107a982d89SJeremy L. Thompson /**
2117a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
2127a982d89SJeremy L. Thompson 
2137a982d89SJeremy L. Thompson   @param qf               CeedQFunction
214d1d35e2fSjeremylt   @param[out] num_input   Variable to store number of input fields
215d1d35e2fSjeremylt   @param[out] num_output  Variable to store number of output fields
2167a982d89SJeremy L. Thompson 
2177a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2187a982d89SJeremy L. Thompson 
2197a982d89SJeremy L. Thompson   @ref Backend
2207a982d89SJeremy L. Thompson **/
221d1d35e2fSjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input,
222d1d35e2fSjeremylt                             CeedInt *num_output) {
223d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
224d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
225e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2267a982d89SJeremy L. Thompson }
2277a982d89SJeremy L. Thompson 
2287a982d89SJeremy L. Thompson /**
2297a982d89SJeremy L. Thompson   @brief Get the source path string for a CeedQFunction
2307a982d89SJeremy L. Thompson 
2317a982d89SJeremy L. Thompson   @param qf           CeedQFunction
2327a982d89SJeremy L. Thompson   @param[out] source  Variable to store source path string
2337a982d89SJeremy L. Thompson 
2347a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2357a982d89SJeremy L. Thompson 
2367a982d89SJeremy L. Thompson   @ref Backend
2377a982d89SJeremy L. Thompson **/
2387a982d89SJeremy L. Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source) {
239d1d35e2fSjeremylt   *source = (char *) qf->source_path;
240e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2417a982d89SJeremy L. Thompson }
2427a982d89SJeremy L. Thompson 
2437a982d89SJeremy L. Thompson /**
2447a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2457a982d89SJeremy L. Thompson 
2467a982d89SJeremy L. Thompson   @param qf      CeedQFunction
2477a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
2487a982d89SJeremy L. Thompson 
2497a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2507a982d89SJeremy L. Thompson 
2517a982d89SJeremy L. Thompson   @ref Backend
2527a982d89SJeremy L. Thompson **/
2537a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
2547a982d89SJeremy L. Thompson   *f = qf->function;
255e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2567a982d89SJeremy L. Thompson }
2577a982d89SJeremy L. Thompson 
2587a982d89SJeremy L. Thompson /**
259777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
260777ff853SJeremy L Thompson            Note: For QFunctions from the Fortran interface, this
261777ff853SJeremy L Thompson              function will return the Fortran context
262777ff853SJeremy L Thompson              CeedQFunctionContext.
2637a982d89SJeremy L. Thompson 
2647a982d89SJeremy L. Thompson   @param qf        CeedQFunction
265777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2667a982d89SJeremy L. Thompson 
2677a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2687a982d89SJeremy L. Thompson 
2697a982d89SJeremy L. Thompson   @ref Backend
2707a982d89SJeremy L. Thompson **/
271777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
2727a982d89SJeremy L. Thompson   *ctx = qf->ctx;
273e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2747a982d89SJeremy L. Thompson }
2757a982d89SJeremy L. Thompson 
2767a982d89SJeremy L. Thompson /**
2777a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
278777ff853SJeremy L Thompson            Note: For all QFunctions this function will return the user
279777ff853SJeremy L Thompson              CeedQFunctionContext and not interface context
280777ff853SJeremy L Thompson              CeedQFunctionContext, if any such object exists.
2817a982d89SJeremy L. Thompson 
2827a982d89SJeremy L. Thompson   @param qf        CeedQFunction
283777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2847a982d89SJeremy L. Thompson 
2857a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2867a982d89SJeremy L. Thompson   @ref Backend
2877a982d89SJeremy L. Thompson **/
288777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
289777ff853SJeremy L Thompson   int ierr;
290d1d35e2fSjeremylt   if (qf->fortran_status) {
291d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
292d1d35e2fSjeremylt     ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx);
293777ff853SJeremy L Thompson     CeedChk(ierr);
294d1d35e2fSjeremylt     *ctx = fortran_ctx->innerctx;
295d1d35e2fSjeremylt     ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx);
296d1d35e2fSjeremylt     CeedChk(ierr);
2977a982d89SJeremy L. Thompson   } else {
2987a982d89SJeremy L. Thompson     *ctx = qf->ctx;
2997a982d89SJeremy L. Thompson   }
300e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3017a982d89SJeremy L. Thompson }
3027a982d89SJeremy L. Thompson 
3037a982d89SJeremy L. Thompson /**
3047a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
3057a982d89SJeremy L. Thompson 
3067a982d89SJeremy L. Thompson   @param qf                CeedQFunction
307d1d35e2fSjeremylt   @param[out] is_identity  Variable to store identity status
3087a982d89SJeremy L. Thompson 
3097a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3107a982d89SJeremy L. Thompson 
3117a982d89SJeremy L. Thompson   @ref Backend
3127a982d89SJeremy L. Thompson **/
313d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
314d1d35e2fSjeremylt   *is_identity = qf->identity;
315e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3167a982d89SJeremy L. Thompson }
3177a982d89SJeremy L. Thompson 
3187a982d89SJeremy L. Thompson /**
3197a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
3207a982d89SJeremy L. Thompson 
3217a982d89SJeremy L. Thompson   @param qf         CeedQFunction
3227a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
3237a982d89SJeremy L. Thompson 
3247a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3257a982d89SJeremy L. Thompson 
3267a982d89SJeremy L. Thompson   @ref Backend
3277a982d89SJeremy L. Thompson **/
328777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
329777ff853SJeremy L Thompson   *(void **)data = qf->data;
330e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3317a982d89SJeremy L. Thompson }
3327a982d89SJeremy L. Thompson 
3337a982d89SJeremy L. Thompson /**
3347a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
3357a982d89SJeremy L. Thompson 
3367a982d89SJeremy L. Thompson   @param[out] qf  CeedQFunction
3377a982d89SJeremy L. Thompson   @param data     Data to set
3387a982d89SJeremy L. Thompson 
3397a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3407a982d89SJeremy L. Thompson 
3417a982d89SJeremy L. Thompson   @ref Backend
3427a982d89SJeremy L. Thompson **/
343777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
344777ff853SJeremy L Thompson   qf->data = data;
345e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3467a982d89SJeremy L. Thompson }
3477a982d89SJeremy L. Thompson 
3487a982d89SJeremy L. Thompson /**
34934359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
35034359f16Sjeremylt 
35134359f16Sjeremylt   @param qf  CeedQFunction to increment the reference counter
35234359f16Sjeremylt 
35334359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
35434359f16Sjeremylt 
35534359f16Sjeremylt   @ref Backend
35634359f16Sjeremylt **/
3579560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
35834359f16Sjeremylt   qf->ref_count++;
35934359f16Sjeremylt   return CEED_ERROR_SUCCESS;
36034359f16Sjeremylt }
36134359f16Sjeremylt 
36234359f16Sjeremylt /**
3637a982d89SJeremy L. Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
3647a982d89SJeremy L. Thompson 
3657a982d89SJeremy L. Thompson   @param qf                  CeedQFunction
366d1d35e2fSjeremylt   @param[out] input_fields   Variable to store input_fields
367d1d35e2fSjeremylt   @param[out] output_fields  Variable to store output_fields
3687a982d89SJeremy L. Thompson 
3697a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3707a982d89SJeremy L. Thompson 
3717a982d89SJeremy L. Thompson   @ref Backend
3727a982d89SJeremy L. Thompson **/
373d1d35e2fSjeremylt int CeedQFunctionGetFields(CeedQFunction qf, CeedQFunctionField **input_fields,
374d1d35e2fSjeremylt                            CeedQFunctionField **output_fields) {
375d1d35e2fSjeremylt   if (input_fields) *input_fields = qf->input_fields;
376d1d35e2fSjeremylt   if (output_fields) *output_fields = qf->output_fields;
377e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3787a982d89SJeremy L. Thompson }
3797a982d89SJeremy L. Thompson 
3807a982d89SJeremy L. Thompson /**
3817a982d89SJeremy L. Thompson   @brief Get the name of a CeedQFunctionField
3827a982d89SJeremy L. Thompson 
383d1d35e2fSjeremylt   @param qf_field         CeedQFunctionField
384d1d35e2fSjeremylt   @param[out] field_name  Variable to store the field name
3857a982d89SJeremy L. Thompson 
3867a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3877a982d89SJeremy L. Thompson 
3887a982d89SJeremy L. Thompson   @ref Backend
3897a982d89SJeremy L. Thompson **/
390d1d35e2fSjeremylt int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
391d1d35e2fSjeremylt   *field_name = (char *)qf_field->field_name;
392e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3937a982d89SJeremy L. Thompson }
3947a982d89SJeremy L. Thompson 
3957a982d89SJeremy L. Thompson /**
3967a982d89SJeremy L. Thompson   @brief Get the number of components of a CeedQFunctionField
3977a982d89SJeremy L. Thompson 
398d1d35e2fSjeremylt   @param qf_field   CeedQFunctionField
3997a982d89SJeremy L. Thompson   @param[out] size  Variable to store the size of the field
4007a982d89SJeremy L. Thompson 
4017a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4027a982d89SJeremy L. Thompson 
4037a982d89SJeremy L. Thompson   @ref Backend
4047a982d89SJeremy L. Thompson **/
405d1d35e2fSjeremylt int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
406d1d35e2fSjeremylt   *size = qf_field->size;
407e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4087a982d89SJeremy L. Thompson }
4097a982d89SJeremy L. Thompson 
4107a982d89SJeremy L. Thompson /**
4117a982d89SJeremy L. Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
4127a982d89SJeremy L. Thompson 
413d1d35e2fSjeremylt   @param qf_field        CeedQFunctionField
414d1d35e2fSjeremylt   @param[out] eval_mode  Variable to store the field evaluation mode
4157a982d89SJeremy L. Thompson 
4167a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4177a982d89SJeremy L. Thompson 
4187a982d89SJeremy L. Thompson   @ref Backend
4197a982d89SJeremy L. Thompson **/
420d1d35e2fSjeremylt int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field,
421d1d35e2fSjeremylt                                   CeedEvalMode *eval_mode) {
422d1d35e2fSjeremylt   *eval_mode = qf_field->eval_mode;
423e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4247a982d89SJeremy L. Thompson }
4257a982d89SJeremy L. Thompson 
4267a982d89SJeremy L. Thompson /// @}
4277a982d89SJeremy L. Thompson 
4287a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4297a982d89SJeremy L. Thompson /// CeedQFunction Public API
4307a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4317a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
4327a982d89SJeremy L. Thompson /// @{
4337a982d89SJeremy L. Thompson 
4347a982d89SJeremy L. Thompson /**
4357a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
4367a982d89SJeremy L. Thompson 
4377a982d89SJeremy L. Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
438d1d35e2fSjeremylt   @param vec_length  Vector length. Caller must ensure that number of quadrature
439d1d35e2fSjeremylt                        points is a multiple of vec_length.
4407a982d89SJeremy L. Thompson   @param f           Function pointer to evaluate action at quadrature points.
4417a982d89SJeremy L. Thompson                        See \ref CeedQFunctionUser.
4427a982d89SJeremy L. Thompson   @param source      Absolute path to source of QFunction,
443c8d5b03cSJeremy L Thompson                        "\abs_path\file.h:function_name".
444c8d5b03cSJeremy L Thompson                        For support across all backends, this source must only
445c8d5b03cSJeremy L Thompson                        contain constructs supported by C99, C++11, and CUDA.
4467a982d89SJeremy L. Thompson   @param[out] qf     Address of the variable where the newly created
4477a982d89SJeremy L. Thompson                        CeedQFunction will be stored
4487a982d89SJeremy L. Thompson 
4497a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4507a982d89SJeremy L. Thompson 
4517a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
4527a982d89SJeremy L. Thompson     arguments.
4537a982d89SJeremy L. Thompson 
4547a982d89SJeremy L. Thompson   @ref User
4557a982d89SJeremy L. Thompson **/
456d1d35e2fSjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length,
457d1d35e2fSjeremylt                                 CeedQFunctionUser f,
4587a982d89SJeremy L. Thompson                                 const char *source, CeedQFunction *qf) {
4597a982d89SJeremy L. Thompson   int ierr;
4607a982d89SJeremy L. Thompson   char *source_copy;
4617a982d89SJeremy L. Thompson 
4627a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
4637a982d89SJeremy L. Thompson     Ceed delegate;
4647a982d89SJeremy L. Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
4657a982d89SJeremy L. Thompson 
4667a982d89SJeremy L. Thompson     if (!delegate)
4677a982d89SJeremy L. Thompson       // LCOV_EXCL_START
468e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
469e15f9bd0SJeremy L Thompson                        "Backend does not support QFunctionCreate");
4707a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
4717a982d89SJeremy L. Thompson 
472d1d35e2fSjeremylt     ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf);
4737a982d89SJeremy L. Thompson     CeedChk(ierr);
474e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
4757a982d89SJeremy L. Thompson   }
4767a982d89SJeremy L. Thompson 
4777a982d89SJeremy L. Thompson   ierr = CeedCalloc(1, qf); CeedChk(ierr);
4787a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
4799560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
480d1d35e2fSjeremylt   (*qf)->ref_count = 1;
481d1d35e2fSjeremylt   (*qf)->vec_length = vec_length;
4827a982d89SJeremy L. Thompson   (*qf)->identity = 0;
4837a982d89SJeremy L. Thompson   (*qf)->function = f;
4847a982d89SJeremy L. Thompson   size_t slen = strlen(source) + 1;
4857a982d89SJeremy L. Thompson   ierr = CeedMalloc(slen, &source_copy); CeedChk(ierr);
4867a982d89SJeremy L. Thompson   memcpy(source_copy, source, slen);
487d1d35e2fSjeremylt   (*qf)->source_path = source_copy;
488d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->input_fields); CeedChk(ierr);
489d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->output_fields); CeedChk(ierr);
4907a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
491e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4927a982d89SJeremy L. Thompson }
4937a982d89SJeremy L. Thompson 
4947a982d89SJeremy L. Thompson /**
495288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
496288c0443SJeremy L Thompson 
497288c0443SJeremy L Thompson   @param ceed     A Ceed object where the CeedQFunction will be created
498288c0443SJeremy L Thompson   @param name     Name of QFunction to use from gallery
499288c0443SJeremy L Thompson   @param[out] qf  Address of the variable where the newly created
500288c0443SJeremy L Thompson                     CeedQFunction will be stored
501288c0443SJeremy L Thompson 
502288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
503288c0443SJeremy L Thompson 
5047a982d89SJeremy L. Thompson   @ref User
505288c0443SJeremy L Thompson **/
506288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
507288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
508288c0443SJeremy L Thompson   int ierr;
509d1d35e2fSjeremylt   size_t match_len = 0, match_idx = UINT_MAX;
51075affc3bSjeremylt   char *name_copy;
511288c0443SJeremy L Thompson 
5121d013790SJed Brown   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
513288c0443SJeremy L Thompson   // Find matching backend
514e15f9bd0SJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE,
515e15f9bd0SJeremy L Thompson                                 "No QFunction name provided");
516288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
517288c0443SJeremy L Thompson     size_t n;
518d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
519d1d35e2fSjeremylt     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {}
520d1d35e2fSjeremylt     if (n > match_len) {
521d1d35e2fSjeremylt       match_len = n;
522d1d35e2fSjeremylt       match_idx = i;
523288c0443SJeremy L Thompson     }
524288c0443SJeremy L Thompson   }
525d1d35e2fSjeremylt   if (!match_len)
526138d4072Sjeremylt     // LCOV_EXCL_START
527e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
528138d4072Sjeremylt   // LCOV_EXCL_STOP
529288c0443SJeremy L Thompson 
530288c0443SJeremy L Thompson   // Create QFunction
531d1d35e2fSjeremylt   ierr = CeedQFunctionCreateInterior(ceed,
532d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].vec_length,
533d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].f,
534d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].source, qf);
535288c0443SJeremy L Thompson   CeedChk(ierr);
536288c0443SJeremy L Thompson 
537288c0443SJeremy L Thompson   // QFunction specific setup
538d1d35e2fSjeremylt   ierr = gallery_qfunctions[match_idx].init(ceed, name, *qf); CeedChk(ierr);
539288c0443SJeremy L Thompson 
54075affc3bSjeremylt   // Copy name
54175affc3bSjeremylt   size_t slen = strlen(name) + 1;
54275affc3bSjeremylt   ierr = CeedMalloc(slen, &name_copy); CeedChk(ierr);
54375affc3bSjeremylt   memcpy(name_copy, name, slen);
544d1d35e2fSjeremylt   (*qf)->qf_name = name_copy;
545e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
546288c0443SJeremy L Thompson }
547288c0443SJeremy L Thompson 
548288c0443SJeremy L Thompson /**
5490219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
5500219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
5510219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
5520219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
5530219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
5540219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
5550219ea01SJeremy L Thompson            location for both.
5560219ea01SJeremy L Thompson 
5570219ea01SJeremy L Thompson   @param ceed          A Ceed object where the CeedQFunction will be created
558d1d35e2fSjeremylt   @param[in] size      Size of the QFunction fields
559d1d35e2fSjeremylt   @param[in] in_mode   CeedEvalMode for input to CeedQFunction
560d1d35e2fSjeremylt   @param[in] out_mode  CeedEvalMode for output to CeedQFunction
5610219ea01SJeremy L Thompson   @param[out] qf       Address of the variable where the newly created
5620219ea01SJeremy L Thompson                          CeedQFunction will be stored
5630219ea01SJeremy L Thompson 
5640219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5650219ea01SJeremy L Thompson 
5667a982d89SJeremy L. Thompson   @ref User
5670219ea01SJeremy L Thompson **/
568d1d35e2fSjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode,
569d1d35e2fSjeremylt                                 CeedEvalMode out_mode, CeedQFunction *qf) {
5700219ea01SJeremy L Thompson   int ierr;
5710219ea01SJeremy L Thompson 
572d1d35e2fSjeremylt   if (in_mode == CEED_EVAL_NONE && out_mode == CEED_EVAL_NONE)
57360f77c51Sjeremylt     // LCOV_EXCL_START
574e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
575e15f9bd0SJeremy L Thompson                      "CEED_EVAL_NONE for a both the input and "
57660f77c51Sjeremylt                      "output does not make sense with an identity QFunction");
57760f77c51Sjeremylt   // LCOV_EXCL_STOP
57860f77c51Sjeremylt 
5790219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
580d1d35e2fSjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr);
581d1d35e2fSjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr);
5820219ea01SJeremy L Thompson 
5830219ea01SJeremy L Thompson   (*qf)->identity = 1;
584d1d35e2fSjeremylt   CeedInt *size_data;
585d1d35e2fSjeremylt   ierr = CeedCalloc(1, &size_data); CeedChk(ierr);
586d1d35e2fSjeremylt   size_data[0] = size;
587777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
588777ff853SJeremy L Thompson   ierr = CeedQFunctionContextCreate(ceed, &ctx); CeedChk(ierr);
589777ff853SJeremy L Thompson   ierr = CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_OWN_POINTER,
590d1d35e2fSjeremylt                                      sizeof(*size_data), (void *)size_data);
591777ff853SJeremy L Thompson   CeedChk(ierr);
592777ff853SJeremy L Thompson   ierr = CeedQFunctionSetContext(*qf, ctx); CeedChk(ierr);
593777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&ctx); CeedChk(ierr);
594e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5950219ea01SJeremy L Thompson }
5960219ea01SJeremy L Thompson 
5970219ea01SJeremy L Thompson /**
5989560d06aSjeremylt   @brief Copy the pointer to a CeedQFunction. Both pointers should
5999560d06aSjeremylt            be destroyed with `CeedQFunctionDestroy()`;
6009560d06aSjeremylt            Note: If `*qf_copy` is non-NULL, then it is assumed that
6019560d06aSjeremylt            `*qf_copy` is a pointer to a CeedQFunction. This
6029560d06aSjeremylt            CeedQFunction will be destroyed if `*qf_copy` is the only
6039560d06aSjeremylt            reference to this CeedQFunction.
6049560d06aSjeremylt 
6059560d06aSjeremylt   @param qf            CeedQFunction to copy reference to
6069560d06aSjeremylt   @param[out] qf_copy  Variable to store copied reference
6079560d06aSjeremylt 
6089560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
6099560d06aSjeremylt 
6109560d06aSjeremylt   @ref User
6119560d06aSjeremylt **/
6129560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
6139560d06aSjeremylt   int ierr;
6149560d06aSjeremylt 
6159560d06aSjeremylt   ierr = CeedQFunctionReference(qf); CeedChk(ierr);
6169560d06aSjeremylt   ierr = CeedQFunctionDestroy(qf_copy); CeedChk(ierr);
6179560d06aSjeremylt   *qf_copy = qf;
6189560d06aSjeremylt   return CEED_ERROR_SUCCESS;
6199560d06aSjeremylt }
6209560d06aSjeremylt 
6219560d06aSjeremylt /**
622a0a97fcfSJed Brown   @brief Add a CeedQFunction input
623b11c1e72Sjeremylt 
624b11c1e72Sjeremylt   @param qf          CeedQFunction
625d1d35e2fSjeremylt   @param field_name  Name of QFunction field
626d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
627d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
628d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
629b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
630b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
631b11c1e72Sjeremylt 
632b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
633dfdf5a53Sjeremylt 
6347a982d89SJeremy L. Thompson   @ref User
635b11c1e72Sjeremylt **/
636d1d35e2fSjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name,
637d1d35e2fSjeremylt                           CeedInt size,
638d1d35e2fSjeremylt                           CeedEvalMode eval_mode) {
639d1d35e2fSjeremylt   if (qf->operators_set)
640e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
641e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
642e15f9bd0SJeremy L Thompson                      "QFunction cannot be changed when in use by an operator");
643e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
644d1d35e2fSjeremylt   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1))
645e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
646e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
647e15f9bd0SJeremy L Thompson                      "CEED_EVAL_WEIGHT should have size 1");
648e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
649d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields],
650d1d35e2fSjeremylt                                    field_name, size, eval_mode);
651fe2413ffSjeremylt   CeedChk(ierr);
652d1d35e2fSjeremylt   qf->num_input_fields++;
653e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
654d7b241e6Sjeremylt }
655d7b241e6Sjeremylt 
656b11c1e72Sjeremylt /**
657a0a97fcfSJed Brown   @brief Add a CeedQFunction output
658b11c1e72Sjeremylt 
659b11c1e72Sjeremylt   @param qf          CeedQFunction
660d1d35e2fSjeremylt   @param field_name  Name of QFunction field
661d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
662d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
663d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
664b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
665b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
666b11c1e72Sjeremylt 
667b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
668dfdf5a53Sjeremylt 
6697a982d89SJeremy L. Thompson   @ref User
670b11c1e72Sjeremylt **/
671d1d35e2fSjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name,
672d1d35e2fSjeremylt                            CeedInt size, CeedEvalMode eval_mode) {
673d1d35e2fSjeremylt   if (qf->operators_set)
674e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
675e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
676e15f9bd0SJeremy L Thompson                      "QFunction cannot be changed when in use by an operator");
677e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
678d1d35e2fSjeremylt   if (eval_mode == CEED_EVAL_WEIGHT)
679c042f62fSJeremy L Thompson     // LCOV_EXCL_START
680e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
681e15f9bd0SJeremy L Thompson                      "Cannot create QFunction output with "
6821d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
683c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
684d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields],
685d1d35e2fSjeremylt                                    field_name, size, eval_mode);
686fe2413ffSjeremylt   CeedChk(ierr);
687d1d35e2fSjeremylt   qf->num_output_fields++;
688e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
689d7b241e6Sjeremylt }
690d7b241e6Sjeremylt 
691dfdf5a53Sjeremylt /**
6924ce2993fSjeremylt   @brief Set global context for a CeedQFunction
693b11c1e72Sjeremylt 
694b11c1e72Sjeremylt   @param qf   CeedQFunction
695b11c1e72Sjeremylt   @param ctx  Context data to set
696b11c1e72Sjeremylt 
697b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
698dfdf5a53Sjeremylt 
6997a982d89SJeremy L. Thompson   @ref User
700b11c1e72Sjeremylt **/
701777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
70234359f16Sjeremylt   int ierr;
703d7b241e6Sjeremylt   qf->ctx = ctx;
7049560d06aSjeremylt   ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr);
705e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
706d7b241e6Sjeremylt }
707d7b241e6Sjeremylt 
708b11c1e72Sjeremylt /**
70975affc3bSjeremylt   @brief View a CeedQFunction
71075affc3bSjeremylt 
71175affc3bSjeremylt   @param[in] qf      CeedQFunction to view
71275affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
71375affc3bSjeremylt 
71475affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
71575affc3bSjeremylt 
7167a982d89SJeremy L. Thompson   @ref User
71775affc3bSjeremylt **/
71875affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
71975affc3bSjeremylt   int ierr;
72075affc3bSjeremylt 
72175affc3bSjeremylt   fprintf(stream, "%sCeedQFunction %s\n",
722d1d35e2fSjeremylt           qf->qf_name ? "Gallery " : "User ", qf->qf_name ? qf->qf_name : "");
72375affc3bSjeremylt 
724d1d35e2fSjeremylt   fprintf(stream, "  %d Input Field%s:\n", qf->num_input_fields,
725d1d35e2fSjeremylt           qf->num_input_fields>1 ? "s" : "");
726d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_input_fields; i++) {
727d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream);
7282da88da5Sjeremylt     CeedChk(ierr);
72975affc3bSjeremylt   }
73075affc3bSjeremylt 
731d1d35e2fSjeremylt   fprintf(stream, "  %d Output Field%s:\n", qf->num_output_fields,
732d1d35e2fSjeremylt           qf->num_output_fields>1 ? "s" : "");
733d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_output_fields; i++) {
734d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream);
73575affc3bSjeremylt     CeedChk(ierr);
73675affc3bSjeremylt   }
737e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
73875affc3bSjeremylt }
73975affc3bSjeremylt 
74075affc3bSjeremylt /**
741b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
742b11c1e72Sjeremylt 
743b11c1e72Sjeremylt   @param qf      CeedQFunction
744b11c1e72Sjeremylt   @param Q       Number of quadrature points
74534138859Sjeremylt   @param[in] u   Array of input CeedVectors
74634138859Sjeremylt   @param[out] v  Array of output CeedVectors
747b11c1e72Sjeremylt 
748b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
749dfdf5a53Sjeremylt 
7507a982d89SJeremy L. Thompson   @ref User
751b11c1e72Sjeremylt **/
752d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
753aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
754d7b241e6Sjeremylt   int ierr;
755d7b241e6Sjeremylt   if (!qf->Apply)
756c042f62fSJeremy L Thompson     // LCOV_EXCL_START
757e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED,
758e15f9bd0SJeremy L Thompson                      "Backend does not support QFunctionApply");
759c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
760d1d35e2fSjeremylt   if (Q % qf->vec_length)
761c042f62fSJeremy L Thompson     // LCOV_EXCL_START
762e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
763e15f9bd0SJeremy L Thompson                      "Number of quadrature points %d must be a "
764d1d35e2fSjeremylt                      "multiple of %d", Q, qf->vec_length);
765c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
766d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
767e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
768d7b241e6Sjeremylt }
769d7b241e6Sjeremylt 
770b11c1e72Sjeremylt /**
771b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
772b11c1e72Sjeremylt 
773b11c1e72Sjeremylt   @param qf  CeedQFunction to destroy
774b11c1e72Sjeremylt 
775b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
776dfdf5a53Sjeremylt 
7777a982d89SJeremy L. Thompson   @ref User
778b11c1e72Sjeremylt **/
779d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
780d7b241e6Sjeremylt   int ierr;
781d7b241e6Sjeremylt 
782d1d35e2fSjeremylt   if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS;
783fe2413ffSjeremylt   // Backend destroy
784d7b241e6Sjeremylt   if ((*qf)->Destroy) {
785d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
786d7b241e6Sjeremylt   }
787fe2413ffSjeremylt   // Free fields
788d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_input_fields; i++) {
789d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr);
790d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr);
791fe2413ffSjeremylt   }
792d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_output_fields; i++) {
793d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr);
794d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr);
795fe2413ffSjeremylt   }
796d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr);
797d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr);
798777ff853SJeremy L Thompson 
799777ff853SJeremy L Thompson   // User context data object
800777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
801fe2413ffSjeremylt 
802d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr);
803d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->qf_name); CeedChk(ierr);
804d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
805d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
806e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
807d7b241e6Sjeremylt }
808d7b241e6Sjeremylt 
809d7b241e6Sjeremylt /// @}
810