xref: /libCEED/interface/ceed-qfunction.c (revision 43bbe138142266535c178bdafdc3d62b34a54607)
1d7b241e6Sjeremylt // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2d7b241e6Sjeremylt // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3d7b241e6Sjeremylt // reserved. See files LICENSE and NOTICE for details.
4d7b241e6Sjeremylt //
5d7b241e6Sjeremylt // This file is part of CEED, a collection of benchmarks, miniapps, software
6d7b241e6Sjeremylt // libraries and APIs for efficient high-order finite element and spectral
7d7b241e6Sjeremylt // element discretizations for exascale applications. For more information and
8d7b241e6Sjeremylt // source code availability see http://github.com/ceed.
9d7b241e6Sjeremylt //
10d7b241e6Sjeremylt // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11d7b241e6Sjeremylt // a collaborative effort of two U.S. Department of Energy organizations (Office
12d7b241e6Sjeremylt // of Science and the National Nuclear Security Administration) responsible for
13d7b241e6Sjeremylt // the planning and preparation of a capable exascale ecosystem, including
14d7b241e6Sjeremylt // software, applications, hardware, advanced system engineering and early
15d7b241e6Sjeremylt // testbed platforms, in support of the nation's exascale computing imperative.
16d7b241e6Sjeremylt 
17ec3da8bcSJed Brown #include <ceed/ceed.h>
18ec3da8bcSJed Brown #include <ceed/backend.h>
193d576824SJeremy L Thompson #include <ceed-impl.h>
20288c0443SJeremy L Thompson #include <limits.h>
213d576824SJeremy L Thompson #include <stdbool.h>
223d576824SJeremy L Thompson #include <stdio.h>
233d576824SJeremy L Thompson #include <string.h>
24288c0443SJeremy L Thompson 
257a982d89SJeremy L. Thompson /// @file
267a982d89SJeremy L. Thompson /// Implementation of public CeedQFunction interfaces
277a982d89SJeremy L. Thompson 
28288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP
29442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none;
30442e7f0bSjeremylt /// @endcond
31442e7f0bSjeremylt 
327a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
337a982d89SJeremy L. Thompson /// @{
347a982d89SJeremy L. Thompson 
357a982d89SJeremy L. Thompson // Indicate that no QFunction is provided by the user
367a982d89SJeremy L. Thompson const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
377a982d89SJeremy L. Thompson 
387a982d89SJeremy L. Thompson /// @}
397a982d89SJeremy L. Thompson 
40442e7f0bSjeremylt /// @cond DOXYGEN_SKIP
41288c0443SJeremy L Thompson static struct {
42288c0443SJeremy L Thompson   char name[CEED_MAX_RESOURCE_LEN];
43288c0443SJeremy L Thompson   char source[CEED_MAX_RESOURCE_LEN];
44d1d35e2fSjeremylt   CeedInt vec_length;
45288c0443SJeremy L Thompson   CeedQFunctionUser f;
46288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
47d1d35e2fSjeremylt } gallery_qfunctions[1024];
48288c0443SJeremy L Thompson static size_t num_qfunctions;
49288c0443SJeremy L Thompson /// @endcond
50d7b241e6Sjeremylt 
51777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
52777ff853SJeremy L Thompson /// CeedQFunction Library Internal Functions
53777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
54777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper
55777ff853SJeremy L Thompson /// @{
56777ff853SJeremy L Thompson 
57b11c1e72Sjeremylt /**
58288c0443SJeremy L Thompson   @brief Register a gallery QFunction
59288c0443SJeremy L Thompson 
60288c0443SJeremy L Thompson   @param name        Name for this backend to respond to
611176cc3aSJeremy L Thompson   @param source      Absolute path to source of QFunction,
621176cc3aSJeremy L Thompson                        "\path\CEED_DIR\gallery\folder\file.h:function_name"
63d1d35e2fSjeremylt   @param vec_length  Vector length.  Caller must ensure that number of quadrature
64d1d35e2fSjeremylt                        points is a multiple of vec_length.
65288c0443SJeremy L Thompson   @param f           Function pointer to evaluate action at quadrature points.
66288c0443SJeremy L Thompson                        See \ref CeedQFunctionUser.
67288c0443SJeremy L Thompson   @param init        Initialization function called by CeedQFunctionInit() when the
68288c0443SJeremy L Thompson                        QFunction is selected.
69288c0443SJeremy L Thompson 
70288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
71288c0443SJeremy L Thompson 
727a982d89SJeremy L. Thompson   @ref Developer
73288c0443SJeremy L Thompson **/
74288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source,
75d1d35e2fSjeremylt                           CeedInt vec_length, CeedQFunctionUser f,
76288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
77d1d35e2fSjeremylt   if (num_qfunctions >= sizeof(gallery_qfunctions) / sizeof(
78d1d35e2fSjeremylt         gallery_qfunctions[0]))
79c042f62fSJeremy L Thompson     // LCOV_EXCL_START
80e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
81c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
82c042f62fSJeremy L Thompson 
83d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
84d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
85d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].source, source,
86d1d35e2fSjeremylt           CEED_MAX_RESOURCE_LEN);
87d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
88d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].vec_length = vec_length;
89d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].f = f;
90d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].init = init;
91288c0443SJeremy L Thompson   num_qfunctions++;
92e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
93288c0443SJeremy L Thompson }
94288c0443SJeremy L Thompson 
95288c0443SJeremy L Thompson /**
967a982d89SJeremy L. Thompson   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
977a982d89SJeremy L. Thompson 
987a982d89SJeremy L. Thompson   @param f           CeedQFunctionField
99d1d35e2fSjeremylt   @param field_name  Name of QFunction field
100d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
101d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT
102d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
1037a982d89SJeremy L. Thompson                        \ref CEED_EVAL_INTERP to use interpolated values,
1047a982d89SJeremy L. Thompson                        \ref CEED_EVAL_GRAD to use gradients,
1057a982d89SJeremy L. Thompson                        \ref CEED_EVAL_WEIGHT to use quadrature weights.
1067a982d89SJeremy L. Thompson 
1077a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1087a982d89SJeremy L. Thompson 
1097a982d89SJeremy L. Thompson   @ref Developer
1107a982d89SJeremy L. Thompson **/
111d1d35e2fSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *field_name,
112d1d35e2fSjeremylt                                  CeedInt size, CeedEvalMode eval_mode) {
113d1d35e2fSjeremylt   size_t len = strlen(field_name);
1147a982d89SJeremy L. Thompson   char *tmp;
1157a982d89SJeremy L. Thompson   int ierr;
1167a982d89SJeremy L. Thompson 
117e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, f); CeedChk(ierr);
1187a982d89SJeremy L. Thompson   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
119d1d35e2fSjeremylt   memcpy(tmp, field_name, len+1);
120d1d35e2fSjeremylt   (*f)->field_name = tmp;
1217a982d89SJeremy L. Thompson   (*f)->size = size;
122d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
123e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1247a982d89SJeremy L. Thompson }
1257a982d89SJeremy L. Thompson 
1267a982d89SJeremy L. Thompson /**
1277a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1287a982d89SJeremy L. Thompson 
1297a982d89SJeremy L. Thompson   @param[in] field         QFunction field to view
130d1d35e2fSjeremylt   @param[in] field_number  Number of field being viewed
1314c4400c7SValeria Barra   @param[in] in            true for input field, false for output
1327a982d89SJeremy L. Thompson   @param[in] stream        Stream to view to, e.g., stdout
1337a982d89SJeremy L. Thompson 
1347a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1357a982d89SJeremy L. Thompson 
1367a982d89SJeremy L. Thompson   @ref Utility
1377a982d89SJeremy L. Thompson **/
138d1d35e2fSjeremylt static int CeedQFunctionFieldView(CeedQFunctionField field,
139d1d35e2fSjeremylt                                   CeedInt field_number,
1407a982d89SJeremy L. Thompson                                   bool in, FILE *stream) {
1418229195eSjeremylt   int ierr;
1427a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1438229195eSjeremylt   char *field_name;
1448229195eSjeremylt   ierr = CeedQFunctionFieldGetName(field, &field_name); CeedChk(ierr);
1458229195eSjeremylt   CeedInt size;
1468229195eSjeremylt   ierr = CeedQFunctionFieldGetSize(field, &size); CeedChk(ierr);
1478229195eSjeremylt   CeedEvalMode eval_mode;
1488229195eSjeremylt   ierr = CeedQFunctionFieldGetEvalMode(field, &eval_mode); CeedChk(ierr);
1497a982d89SJeremy L. Thompson   fprintf(stream, "    %s Field [%d]:\n"
1507a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1517a982d89SJeremy L. Thompson           "      Size: %d\n"
1527a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1538229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
154e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1557a982d89SJeremy L. Thompson }
1567a982d89SJeremy L. Thompson 
157777ff853SJeremy L Thompson /**
158777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
159777ff853SJeremy L Thompson 
160777ff853SJeremy L Thompson   @param qf      CeedQFunction
161777ff853SJeremy L Thompson   @param status  Boolean value to set as Fortran status
162777ff853SJeremy L Thompson 
163777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
164777ff853SJeremy L Thompson 
165777ff853SJeremy L Thompson   @ref Backend
166777ff853SJeremy L Thompson **/
167777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
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 
3627a982d89SJeremy L. Thompson /// @}
3637a982d89SJeremy L. Thompson 
3647a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3657a982d89SJeremy L. Thompson /// CeedQFunction Public API
3667a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3677a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
3687a982d89SJeremy L. Thompson /// @{
3697a982d89SJeremy L. Thompson 
3707a982d89SJeremy L. Thompson /**
3717a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
3727a982d89SJeremy L. Thompson 
3737a982d89SJeremy L. Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
374d1d35e2fSjeremylt   @param vec_length  Vector length. Caller must ensure that number of quadrature
375d1d35e2fSjeremylt                        points is a multiple of vec_length.
3767a982d89SJeremy L. Thompson   @param f           Function pointer to evaluate action at quadrature points.
3777a982d89SJeremy L. Thompson                        See \ref CeedQFunctionUser.
3787a982d89SJeremy L. Thompson   @param source      Absolute path to source of QFunction,
379c8d5b03cSJeremy L Thompson                        "\abs_path\file.h:function_name".
380c8d5b03cSJeremy L Thompson                        For support across all backends, this source must only
381c8d5b03cSJeremy L Thompson                        contain constructs supported by C99, C++11, and CUDA.
3827a982d89SJeremy L. Thompson   @param[out] qf     Address of the variable where the newly created
3837a982d89SJeremy L. Thompson                        CeedQFunction will be stored
3847a982d89SJeremy L. Thompson 
3857a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3867a982d89SJeremy L. Thompson 
3877a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
3887a982d89SJeremy L. Thompson     arguments.
3897a982d89SJeremy L. Thompson 
3907a982d89SJeremy L. Thompson   @ref User
3917a982d89SJeremy L. Thompson **/
392d1d35e2fSjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length,
393d1d35e2fSjeremylt                                 CeedQFunctionUser f,
3947a982d89SJeremy L. Thompson                                 const char *source, CeedQFunction *qf) {
3957a982d89SJeremy L. Thompson   int ierr;
3967a982d89SJeremy L. Thompson   char *source_copy;
3977a982d89SJeremy L. Thompson 
3987a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
3997a982d89SJeremy L. Thompson     Ceed delegate;
4007a982d89SJeremy L. Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
4017a982d89SJeremy L. Thompson 
4027a982d89SJeremy L. Thompson     if (!delegate)
4037a982d89SJeremy L. Thompson       // LCOV_EXCL_START
404e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
405e15f9bd0SJeremy L Thompson                        "Backend does not support QFunctionCreate");
4067a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
4077a982d89SJeremy L. Thompson 
408d1d35e2fSjeremylt     ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf);
4097a982d89SJeremy L. Thompson     CeedChk(ierr);
410e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
4117a982d89SJeremy L. Thompson   }
4127a982d89SJeremy L. Thompson 
4137a982d89SJeremy L. Thompson   ierr = CeedCalloc(1, qf); CeedChk(ierr);
4147a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
4159560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
416d1d35e2fSjeremylt   (*qf)->ref_count = 1;
417d1d35e2fSjeremylt   (*qf)->vec_length = vec_length;
4187a982d89SJeremy L. Thompson   (*qf)->identity = 0;
4197a982d89SJeremy L. Thompson   (*qf)->function = f;
4207a982d89SJeremy L. Thompson   size_t slen = strlen(source) + 1;
4217a982d89SJeremy L. Thompson   ierr = CeedMalloc(slen, &source_copy); CeedChk(ierr);
4227a982d89SJeremy L. Thompson   memcpy(source_copy, source, slen);
423d1d35e2fSjeremylt   (*qf)->source_path = source_copy;
424d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->input_fields); CeedChk(ierr);
425d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->output_fields); CeedChk(ierr);
4267a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
427e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4287a982d89SJeremy L. Thompson }
4297a982d89SJeremy L. Thompson 
4307a982d89SJeremy L. Thompson /**
431288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
432288c0443SJeremy L Thompson 
433288c0443SJeremy L Thompson   @param ceed     A Ceed object where the CeedQFunction will be created
434288c0443SJeremy L Thompson   @param name     Name of QFunction to use from gallery
435288c0443SJeremy L Thompson   @param[out] qf  Address of the variable where the newly created
436288c0443SJeremy L Thompson                     CeedQFunction will be stored
437288c0443SJeremy L Thompson 
438288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
439288c0443SJeremy L Thompson 
4407a982d89SJeremy L. Thompson   @ref User
441288c0443SJeremy L Thompson **/
442288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
443288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
444288c0443SJeremy L Thompson   int ierr;
445d1d35e2fSjeremylt   size_t match_len = 0, match_idx = UINT_MAX;
44675affc3bSjeremylt   char *name_copy;
447288c0443SJeremy L Thompson 
4481d013790SJed Brown   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
449288c0443SJeremy L Thompson   // Find matching backend
450e15f9bd0SJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE,
451e15f9bd0SJeremy L Thompson                                 "No QFunction name provided");
452288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
453288c0443SJeremy L Thompson     size_t n;
454d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
455d1d35e2fSjeremylt     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {}
456d1d35e2fSjeremylt     if (n > match_len) {
457d1d35e2fSjeremylt       match_len = n;
458d1d35e2fSjeremylt       match_idx = i;
459288c0443SJeremy L Thompson     }
460288c0443SJeremy L Thompson   }
461d1d35e2fSjeremylt   if (!match_len)
462138d4072Sjeremylt     // LCOV_EXCL_START
463e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
464138d4072Sjeremylt   // LCOV_EXCL_STOP
465288c0443SJeremy L Thompson 
466288c0443SJeremy L Thompson   // Create QFunction
467d1d35e2fSjeremylt   ierr = CeedQFunctionCreateInterior(ceed,
468d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].vec_length,
469d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].f,
470d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].source, qf);
471288c0443SJeremy L Thompson   CeedChk(ierr);
472288c0443SJeremy L Thompson 
473288c0443SJeremy L Thompson   // QFunction specific setup
474d1d35e2fSjeremylt   ierr = gallery_qfunctions[match_idx].init(ceed, name, *qf); CeedChk(ierr);
475288c0443SJeremy L Thompson 
47675affc3bSjeremylt   // Copy name
47775affc3bSjeremylt   size_t slen = strlen(name) + 1;
47875affc3bSjeremylt   ierr = CeedMalloc(slen, &name_copy); CeedChk(ierr);
47975affc3bSjeremylt   memcpy(name_copy, name, slen);
480d1d35e2fSjeremylt   (*qf)->qf_name = name_copy;
481e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
482288c0443SJeremy L Thompson }
483288c0443SJeremy L Thompson 
484288c0443SJeremy L Thompson /**
4850219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
4860219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
4870219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
4880219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
4890219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
4900219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
4910219ea01SJeremy L Thompson            location for both.
4920219ea01SJeremy L Thompson 
4930219ea01SJeremy L Thompson   @param ceed          A Ceed object where the CeedQFunction will be created
494d1d35e2fSjeremylt   @param[in] size      Size of the QFunction fields
495d1d35e2fSjeremylt   @param[in] in_mode   CeedEvalMode for input to CeedQFunction
496d1d35e2fSjeremylt   @param[in] out_mode  CeedEvalMode for output to CeedQFunction
4970219ea01SJeremy L Thompson   @param[out] qf       Address of the variable where the newly created
4980219ea01SJeremy L Thompson                          CeedQFunction will be stored
4990219ea01SJeremy L Thompson 
5000219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5010219ea01SJeremy L Thompson 
5027a982d89SJeremy L. Thompson   @ref User
5030219ea01SJeremy L Thompson **/
504d1d35e2fSjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode,
505d1d35e2fSjeremylt                                 CeedEvalMode out_mode, CeedQFunction *qf) {
5060219ea01SJeremy L Thompson   int ierr;
5070219ea01SJeremy L Thompson 
5080219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
509d1d35e2fSjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr);
510d1d35e2fSjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr);
5110219ea01SJeremy L Thompson 
5120219ea01SJeremy L Thompson   (*qf)->identity = 1;
513d1d35e2fSjeremylt   CeedInt *size_data;
514d1d35e2fSjeremylt   ierr = CeedCalloc(1, &size_data); CeedChk(ierr);
515d1d35e2fSjeremylt   size_data[0] = size;
516777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
517777ff853SJeremy L Thompson   ierr = CeedQFunctionContextCreate(ceed, &ctx); CeedChk(ierr);
518777ff853SJeremy L Thompson   ierr = CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_OWN_POINTER,
519d1d35e2fSjeremylt                                      sizeof(*size_data), (void *)size_data);
520777ff853SJeremy L Thompson   CeedChk(ierr);
521777ff853SJeremy L Thompson   ierr = CeedQFunctionSetContext(*qf, ctx); CeedChk(ierr);
522777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&ctx); CeedChk(ierr);
523e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5240219ea01SJeremy L Thompson }
5250219ea01SJeremy L Thompson 
5260219ea01SJeremy L Thompson /**
5279560d06aSjeremylt   @brief Copy the pointer to a CeedQFunction. Both pointers should
5289560d06aSjeremylt            be destroyed with `CeedQFunctionDestroy()`;
5299560d06aSjeremylt            Note: If `*qf_copy` is non-NULL, then it is assumed that
5309560d06aSjeremylt            `*qf_copy` is a pointer to a CeedQFunction. This
5319560d06aSjeremylt            CeedQFunction will be destroyed if `*qf_copy` is the only
5329560d06aSjeremylt            reference to this CeedQFunction.
5339560d06aSjeremylt 
5349560d06aSjeremylt   @param qf            CeedQFunction to copy reference to
5359560d06aSjeremylt   @param[out] qf_copy  Variable to store copied reference
5369560d06aSjeremylt 
5379560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
5389560d06aSjeremylt 
5399560d06aSjeremylt   @ref User
5409560d06aSjeremylt **/
5419560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
5429560d06aSjeremylt   int ierr;
5439560d06aSjeremylt 
5449560d06aSjeremylt   ierr = CeedQFunctionReference(qf); CeedChk(ierr);
5459560d06aSjeremylt   ierr = CeedQFunctionDestroy(qf_copy); CeedChk(ierr);
5469560d06aSjeremylt   *qf_copy = qf;
5479560d06aSjeremylt   return CEED_ERROR_SUCCESS;
5489560d06aSjeremylt }
5499560d06aSjeremylt 
5509560d06aSjeremylt /**
551a0a97fcfSJed Brown   @brief Add a CeedQFunction input
552b11c1e72Sjeremylt 
553b11c1e72Sjeremylt   @param qf          CeedQFunction
554d1d35e2fSjeremylt   @param field_name  Name of QFunction field
555d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
556d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
557d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
558b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
559b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
560b11c1e72Sjeremylt 
561b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
562dfdf5a53Sjeremylt 
5637a982d89SJeremy L. Thompson   @ref User
564b11c1e72Sjeremylt **/
565d1d35e2fSjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name,
566d1d35e2fSjeremylt                           CeedInt size,
567d1d35e2fSjeremylt                           CeedEvalMode eval_mode) {
568d1d35e2fSjeremylt   if (qf->operators_set)
569e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
570e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
571e15f9bd0SJeremy L Thompson                      "QFunction cannot be changed when in use by an operator");
572e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
573d1d35e2fSjeremylt   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1))
574e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
575e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
576e15f9bd0SJeremy L Thompson                      "CEED_EVAL_WEIGHT should have size 1");
577e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
578d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields],
579d1d35e2fSjeremylt                                    field_name, size, eval_mode);
580fe2413ffSjeremylt   CeedChk(ierr);
581d1d35e2fSjeremylt   qf->num_input_fields++;
582e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
583d7b241e6Sjeremylt }
584d7b241e6Sjeremylt 
585b11c1e72Sjeremylt /**
586a0a97fcfSJed Brown   @brief Add a CeedQFunction output
587b11c1e72Sjeremylt 
588b11c1e72Sjeremylt   @param qf          CeedQFunction
589d1d35e2fSjeremylt   @param field_name  Name of QFunction field
590d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
591d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
592d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
593b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
594b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
595b11c1e72Sjeremylt 
596b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
597dfdf5a53Sjeremylt 
5987a982d89SJeremy L. Thompson   @ref User
599b11c1e72Sjeremylt **/
600d1d35e2fSjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name,
601d1d35e2fSjeremylt                            CeedInt size, CeedEvalMode eval_mode) {
602d1d35e2fSjeremylt   if (qf->operators_set)
603e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
604e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
605e15f9bd0SJeremy L Thompson                      "QFunction cannot be changed when in use by an operator");
606e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
607d1d35e2fSjeremylt   if (eval_mode == CEED_EVAL_WEIGHT)
608c042f62fSJeremy L Thompson     // LCOV_EXCL_START
609e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
610e15f9bd0SJeremy L Thompson                      "Cannot create QFunction output with "
6111d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
612c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
613d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields],
614d1d35e2fSjeremylt                                    field_name, size, eval_mode);
615fe2413ffSjeremylt   CeedChk(ierr);
616d1d35e2fSjeremylt   qf->num_output_fields++;
617e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
618d7b241e6Sjeremylt }
619d7b241e6Sjeremylt 
620dfdf5a53Sjeremylt /**
621*43bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
622*43bbe138SJeremy L Thompson 
623*43bbe138SJeremy L Thompson   @param qf                  CeedQFunction
624*43bbe138SJeremy L Thompson   @param[out] input_fields   Variable to store input_fields
625*43bbe138SJeremy L Thompson   @param[out] output_fields  Variable to store output_fields
626*43bbe138SJeremy L Thompson 
627*43bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
628*43bbe138SJeremy L Thompson 
629*43bbe138SJeremy L Thompson   @ref Backend
630*43bbe138SJeremy L Thompson **/
631*43bbe138SJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields,
632*43bbe138SJeremy L Thompson                            CeedQFunctionField **input_fields,
633*43bbe138SJeremy L Thompson                            CeedInt *num_output_fields,
634*43bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
635*43bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
636*43bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
637*43bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
638*43bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
639*43bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
640*43bbe138SJeremy L Thompson }
641*43bbe138SJeremy L Thompson 
642*43bbe138SJeremy L Thompson /**
643*43bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
644*43bbe138SJeremy L Thompson 
645*43bbe138SJeremy L Thompson   @param qf_field         CeedQFunctionField
646*43bbe138SJeremy L Thompson   @param[out] field_name  Variable to store the field name
647*43bbe138SJeremy L Thompson 
648*43bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
649*43bbe138SJeremy L Thompson 
650*43bbe138SJeremy L Thompson   @ref Backend
651*43bbe138SJeremy L Thompson **/
652*43bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
653*43bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
654*43bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
655*43bbe138SJeremy L Thompson }
656*43bbe138SJeremy L Thompson 
657*43bbe138SJeremy L Thompson /**
658*43bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
659*43bbe138SJeremy L Thompson 
660*43bbe138SJeremy L Thompson   @param qf_field   CeedQFunctionField
661*43bbe138SJeremy L Thompson   @param[out] size  Variable to store the size of the field
662*43bbe138SJeremy L Thompson 
663*43bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
664*43bbe138SJeremy L Thompson 
665*43bbe138SJeremy L Thompson   @ref Backend
666*43bbe138SJeremy L Thompson **/
667*43bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
668*43bbe138SJeremy L Thompson   *size = qf_field->size;
669*43bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
670*43bbe138SJeremy L Thompson }
671*43bbe138SJeremy L Thompson 
672*43bbe138SJeremy L Thompson /**
673*43bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
674*43bbe138SJeremy L Thompson 
675*43bbe138SJeremy L Thompson   @param qf_field        CeedQFunctionField
676*43bbe138SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
677*43bbe138SJeremy L Thompson 
678*43bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
679*43bbe138SJeremy L Thompson 
680*43bbe138SJeremy L Thompson   @ref Backend
681*43bbe138SJeremy L Thompson **/
682*43bbe138SJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field,
683*43bbe138SJeremy L Thompson                                   CeedEvalMode *eval_mode) {
684*43bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
685*43bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
686*43bbe138SJeremy L Thompson }
687*43bbe138SJeremy L Thompson 
688*43bbe138SJeremy L Thompson /**
6894ce2993fSjeremylt   @brief Set global context for a CeedQFunction
690b11c1e72Sjeremylt 
691b11c1e72Sjeremylt   @param qf   CeedQFunction
692b11c1e72Sjeremylt   @param ctx  Context data to set
693b11c1e72Sjeremylt 
694b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
695dfdf5a53Sjeremylt 
6967a982d89SJeremy L. Thompson   @ref User
697b11c1e72Sjeremylt **/
698777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
69934359f16Sjeremylt   int ierr;
700d7b241e6Sjeremylt   qf->ctx = ctx;
7019560d06aSjeremylt   ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr);
702e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
703d7b241e6Sjeremylt }
704d7b241e6Sjeremylt 
705b11c1e72Sjeremylt /**
70675affc3bSjeremylt   @brief View a CeedQFunction
70775affc3bSjeremylt 
70875affc3bSjeremylt   @param[in] qf      CeedQFunction to view
70975affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
71075affc3bSjeremylt 
71175affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
71275affc3bSjeremylt 
7137a982d89SJeremy L. Thompson   @ref User
71475affc3bSjeremylt **/
71575affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
71675affc3bSjeremylt   int ierr;
71775affc3bSjeremylt 
71875affc3bSjeremylt   fprintf(stream, "%sCeedQFunction %s\n",
719d1d35e2fSjeremylt           qf->qf_name ? "Gallery " : "User ", qf->qf_name ? qf->qf_name : "");
72075affc3bSjeremylt 
721d1d35e2fSjeremylt   fprintf(stream, "  %d Input Field%s:\n", qf->num_input_fields,
722d1d35e2fSjeremylt           qf->num_input_fields>1 ? "s" : "");
723d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_input_fields; i++) {
724d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream);
7252da88da5Sjeremylt     CeedChk(ierr);
72675affc3bSjeremylt   }
72775affc3bSjeremylt 
728d1d35e2fSjeremylt   fprintf(stream, "  %d Output Field%s:\n", qf->num_output_fields,
729d1d35e2fSjeremylt           qf->num_output_fields>1 ? "s" : "");
730d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_output_fields; i++) {
731d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream);
73275affc3bSjeremylt     CeedChk(ierr);
73375affc3bSjeremylt   }
734e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
73575affc3bSjeremylt }
73675affc3bSjeremylt 
73775affc3bSjeremylt /**
738b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
739b11c1e72Sjeremylt 
740b11c1e72Sjeremylt   @param qf      CeedQFunction
741b11c1e72Sjeremylt   @param Q       Number of quadrature points
74234138859Sjeremylt   @param[in] u   Array of input CeedVectors
74334138859Sjeremylt   @param[out] v  Array of output CeedVectors
744b11c1e72Sjeremylt 
745b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
746dfdf5a53Sjeremylt 
7477a982d89SJeremy L. Thompson   @ref User
748b11c1e72Sjeremylt **/
749d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
750aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
751d7b241e6Sjeremylt   int ierr;
752d7b241e6Sjeremylt   if (!qf->Apply)
753c042f62fSJeremy L Thompson     // LCOV_EXCL_START
754e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED,
755e15f9bd0SJeremy L Thompson                      "Backend does not support QFunctionApply");
756c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
757d1d35e2fSjeremylt   if (Q % qf->vec_length)
758c042f62fSJeremy L Thompson     // LCOV_EXCL_START
759e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
760e15f9bd0SJeremy L Thompson                      "Number of quadrature points %d must be a "
761d1d35e2fSjeremylt                      "multiple of %d", Q, qf->vec_length);
762c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
763d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
764e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
765d7b241e6Sjeremylt }
766d7b241e6Sjeremylt 
767b11c1e72Sjeremylt /**
768b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
769b11c1e72Sjeremylt 
770b11c1e72Sjeremylt   @param qf  CeedQFunction to destroy
771b11c1e72Sjeremylt 
772b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
773dfdf5a53Sjeremylt 
7747a982d89SJeremy L. Thompson   @ref User
775b11c1e72Sjeremylt **/
776d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
777d7b241e6Sjeremylt   int ierr;
778d7b241e6Sjeremylt 
779d1d35e2fSjeremylt   if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS;
780fe2413ffSjeremylt   // Backend destroy
781d7b241e6Sjeremylt   if ((*qf)->Destroy) {
782d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
783d7b241e6Sjeremylt   }
784fe2413ffSjeremylt   // Free fields
785d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_input_fields; i++) {
786d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr);
787d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr);
788fe2413ffSjeremylt   }
789d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_output_fields; i++) {
790d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr);
791d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr);
792fe2413ffSjeremylt   }
793d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr);
794d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr);
795777ff853SJeremy L Thompson 
796777ff853SJeremy L Thompson   // User context data object
797777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
798fe2413ffSjeremylt 
799d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr);
800d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->qf_name); CeedChk(ierr);
801d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
802d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
803e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
804d7b241e6Sjeremylt }
805d7b241e6Sjeremylt 
806d7b241e6Sjeremylt /// @}
807