xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision b7c9bbdafc9b91976b65f80ebb2c5357c2efd8bc)
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) {
168f04ea552SJeremy L Thompson   qf->is_fortran = 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 vector length of a CeedQFunction
1827a982d89SJeremy L. Thompson 
1837a982d89SJeremy L. Thompson   @param qf               CeedQFunction
184d1d35e2fSjeremylt   @param[out] vec_length  Variable to store vector length
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 **/
190d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
191d1d35e2fSjeremylt   *vec_length = qf->vec_length;
192e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1937a982d89SJeremy L. Thompson }
1947a982d89SJeremy L. Thompson 
1957a982d89SJeremy L. Thompson /**
1967a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
1977a982d89SJeremy L. Thompson 
1987a982d89SJeremy L. Thompson   @param qf               CeedQFunction
199d1d35e2fSjeremylt   @param[out] num_input   Variable to store number of input fields
200d1d35e2fSjeremylt   @param[out] num_output  Variable to store number of output fields
2017a982d89SJeremy L. Thompson 
2027a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2037a982d89SJeremy L. Thompson 
2047a982d89SJeremy L. Thompson   @ref Backend
2057a982d89SJeremy L. Thompson **/
206d1d35e2fSjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input,
207d1d35e2fSjeremylt                             CeedInt *num_output) {
208d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
209d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
210e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2117a982d89SJeremy L. Thompson }
2127a982d89SJeremy L. Thompson 
2137a982d89SJeremy L. Thompson /**
2147a982d89SJeremy L. Thompson   @brief Get the source path string for a CeedQFunction
2157a982d89SJeremy L. Thompson 
2167a982d89SJeremy L. Thompson   @param qf           CeedQFunction
2177a982d89SJeremy L. Thompson   @param[out] source  Variable to store source path string
2187a982d89SJeremy L. Thompson 
2197a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2207a982d89SJeremy L. Thompson 
2217a982d89SJeremy L. Thompson   @ref Backend
2227a982d89SJeremy L. Thompson **/
2237a982d89SJeremy L. Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source) {
224d1d35e2fSjeremylt   *source = (char *) qf->source_path;
225e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2267a982d89SJeremy L. Thompson }
2277a982d89SJeremy L. Thompson 
2287a982d89SJeremy L. Thompson /**
2297a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2307a982d89SJeremy L. Thompson 
2317a982d89SJeremy L. Thompson   @param qf      CeedQFunction
2327a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
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 CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
2397a982d89SJeremy L. Thompson   *f = qf->function;
240e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2417a982d89SJeremy L. Thompson }
2427a982d89SJeremy L. Thompson 
2437a982d89SJeremy L. Thompson /**
244777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
245777ff853SJeremy L Thompson            Note: For QFunctions from the Fortran interface, this
246777ff853SJeremy L Thompson              function will return the Fortran context
247777ff853SJeremy L Thompson              CeedQFunctionContext.
2487a982d89SJeremy L. Thompson 
2497a982d89SJeremy L. Thompson   @param qf        CeedQFunction
250777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2517a982d89SJeremy L. Thompson 
2527a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2537a982d89SJeremy L. Thompson 
2547a982d89SJeremy L. Thompson   @ref Backend
2557a982d89SJeremy L. Thompson **/
256777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
2577a982d89SJeremy L. Thompson   *ctx = qf->ctx;
258e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2597a982d89SJeremy L. Thompson }
2607a982d89SJeremy L. Thompson 
2617a982d89SJeremy L. Thompson /**
2627a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
263777ff853SJeremy L Thompson            Note: For all QFunctions this function will return the user
264777ff853SJeremy L Thompson              CeedQFunctionContext and not interface context
265777ff853SJeremy L Thompson              CeedQFunctionContext, if any such object exists.
2667a982d89SJeremy L. Thompson 
2677a982d89SJeremy L. Thompson   @param qf        CeedQFunction
268777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2697a982d89SJeremy L. Thompson 
2707a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2717a982d89SJeremy L. Thompson   @ref Backend
2727a982d89SJeremy L. Thompson **/
273777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
274777ff853SJeremy L Thompson   int ierr;
275f04ea552SJeremy L Thompson   if (qf->is_fortran) {
276d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
277d1d35e2fSjeremylt     ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx);
278777ff853SJeremy L Thompson     CeedChk(ierr);
279d1d35e2fSjeremylt     *ctx = fortran_ctx->innerctx;
280d1d35e2fSjeremylt     ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx);
281d1d35e2fSjeremylt     CeedChk(ierr);
2827a982d89SJeremy L. Thompson   } else {
2837a982d89SJeremy L. Thompson     *ctx = qf->ctx;
2847a982d89SJeremy L. Thompson   }
285e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2867a982d89SJeremy L. Thompson }
2877a982d89SJeremy L. Thompson 
2887a982d89SJeremy L. Thompson /**
2897a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
2907a982d89SJeremy L. Thompson 
2917a982d89SJeremy L. Thompson   @param qf                CeedQFunction
292d1d35e2fSjeremylt   @param[out] is_identity  Variable to store identity status
2937a982d89SJeremy L. Thompson 
2947a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2957a982d89SJeremy L. Thompson 
2967a982d89SJeremy L. Thompson   @ref Backend
2977a982d89SJeremy L. Thompson **/
298d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
299f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
300e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3017a982d89SJeremy L. Thompson }
3027a982d89SJeremy L. Thompson 
3037a982d89SJeremy L. Thompson /**
3047a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
3057a982d89SJeremy L. Thompson 
3067a982d89SJeremy L. Thompson   @param qf         CeedQFunction
3077a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
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 **/
313777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
314777ff853SJeremy L Thompson   *(void **)data = qf->data;
315e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3167a982d89SJeremy L. Thompson }
3177a982d89SJeremy L. Thompson 
3187a982d89SJeremy L. Thompson /**
3197a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
3207a982d89SJeremy L. Thompson 
3217a982d89SJeremy L. Thompson   @param[out] qf  CeedQFunction
3227a982d89SJeremy L. Thompson   @param data     Data to set
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 CeedQFunctionSetData(CeedQFunction qf, void *data) {
329777ff853SJeremy L Thompson   qf->data = data;
330e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3317a982d89SJeremy L. Thompson }
3327a982d89SJeremy L. Thompson 
3337a982d89SJeremy L. Thompson /**
33434359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
33534359f16Sjeremylt 
33634359f16Sjeremylt   @param qf  CeedQFunction to increment the reference counter
33734359f16Sjeremylt 
33834359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
33934359f16Sjeremylt 
34034359f16Sjeremylt   @ref Backend
34134359f16Sjeremylt **/
3429560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
34334359f16Sjeremylt   qf->ref_count++;
34434359f16Sjeremylt   return CEED_ERROR_SUCCESS;
34534359f16Sjeremylt }
34634359f16Sjeremylt 
3477a982d89SJeremy L. Thompson /// @}
3487a982d89SJeremy L. Thompson 
3497a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3507a982d89SJeremy L. Thompson /// CeedQFunction Public API
3517a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3527a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
3537a982d89SJeremy L. Thompson /// @{
3547a982d89SJeremy L. Thompson 
3557a982d89SJeremy L. Thompson /**
3567a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
3577a982d89SJeremy L. Thompson 
3587a982d89SJeremy L. Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
359d1d35e2fSjeremylt   @param vec_length  Vector length. Caller must ensure that number of quadrature
360d1d35e2fSjeremylt                        points is a multiple of vec_length.
3617a982d89SJeremy L. Thompson   @param f           Function pointer to evaluate action at quadrature points.
3627a982d89SJeremy L. Thompson                        See \ref CeedQFunctionUser.
3637a982d89SJeremy L. Thompson   @param source      Absolute path to source of QFunction,
364c8d5b03cSJeremy L Thompson                        "\abs_path\file.h:function_name".
365c8d5b03cSJeremy L Thompson                        For support across all backends, this source must only
366c8d5b03cSJeremy L Thompson                        contain constructs supported by C99, C++11, and CUDA.
3677a982d89SJeremy L. Thompson   @param[out] qf     Address of the variable where the newly created
3687a982d89SJeremy L. Thompson                        CeedQFunction will be stored
3697a982d89SJeremy L. Thompson 
3707a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3717a982d89SJeremy L. Thompson 
3727a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
3737a982d89SJeremy L. Thompson     arguments.
3747a982d89SJeremy L. Thompson 
3757a982d89SJeremy L. Thompson   @ref User
3767a982d89SJeremy L. Thompson **/
377d1d35e2fSjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length,
378d1d35e2fSjeremylt                                 CeedQFunctionUser f,
3797a982d89SJeremy L. Thompson                                 const char *source, CeedQFunction *qf) {
3807a982d89SJeremy L. Thompson   int ierr;
3817a982d89SJeremy L. Thompson   char *source_copy;
3827a982d89SJeremy L. Thompson 
3837a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
3847a982d89SJeremy L. Thompson     Ceed delegate;
3857a982d89SJeremy L. Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
3867a982d89SJeremy L. Thompson 
3877a982d89SJeremy L. Thompson     if (!delegate)
3887a982d89SJeremy L. Thompson       // LCOV_EXCL_START
389e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
390e15f9bd0SJeremy L Thompson                        "Backend does not support QFunctionCreate");
3917a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
3927a982d89SJeremy L. Thompson 
393d1d35e2fSjeremylt     ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf);
3947a982d89SJeremy L. Thompson     CeedChk(ierr);
395e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
3967a982d89SJeremy L. Thompson   }
3977a982d89SJeremy L. Thompson 
3987a982d89SJeremy L. Thompson   ierr = CeedCalloc(1, qf); CeedChk(ierr);
3997a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
4009560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
401d1d35e2fSjeremylt   (*qf)->ref_count = 1;
402d1d35e2fSjeremylt   (*qf)->vec_length = vec_length;
403f04ea552SJeremy L Thompson   (*qf)->is_identity = false;
4047a982d89SJeremy L. Thompson   (*qf)->function = f;
4057a982d89SJeremy L. Thompson   size_t slen = strlen(source) + 1;
4067a982d89SJeremy L. Thompson   ierr = CeedMalloc(slen, &source_copy); CeedChk(ierr);
4077a982d89SJeremy L. Thompson   memcpy(source_copy, source, slen);
408d1d35e2fSjeremylt   (*qf)->source_path = source_copy;
409d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->input_fields); CeedChk(ierr);
410d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->output_fields); CeedChk(ierr);
4117a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
412e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4137a982d89SJeremy L. Thompson }
4147a982d89SJeremy L. Thompson 
4157a982d89SJeremy L. Thompson /**
416288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
417288c0443SJeremy L Thompson 
418288c0443SJeremy L Thompson   @param ceed     A Ceed object where the CeedQFunction will be created
419288c0443SJeremy L Thompson   @param name     Name of QFunction to use from gallery
420288c0443SJeremy L Thompson   @param[out] qf  Address of the variable where the newly created
421288c0443SJeremy L Thompson                     CeedQFunction will be stored
422288c0443SJeremy L Thompson 
423288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
424288c0443SJeremy L Thompson 
4257a982d89SJeremy L. Thompson   @ref User
426288c0443SJeremy L Thompson **/
427288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
428288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
429288c0443SJeremy L Thompson   int ierr;
430d1d35e2fSjeremylt   size_t match_len = 0, match_idx = UINT_MAX;
43175affc3bSjeremylt   char *name_copy;
432288c0443SJeremy L Thompson 
4331d013790SJed Brown   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
434288c0443SJeremy L Thompson   // Find matching backend
435e15f9bd0SJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE,
436e15f9bd0SJeremy L Thompson                                 "No QFunction name provided");
437288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
438288c0443SJeremy L Thompson     size_t n;
439d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
440d1d35e2fSjeremylt     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {}
441d1d35e2fSjeremylt     if (n > match_len) {
442d1d35e2fSjeremylt       match_len = n;
443d1d35e2fSjeremylt       match_idx = i;
444288c0443SJeremy L Thompson     }
445288c0443SJeremy L Thompson   }
446d1d35e2fSjeremylt   if (!match_len)
447138d4072Sjeremylt     // LCOV_EXCL_START
448e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
449138d4072Sjeremylt   // LCOV_EXCL_STOP
450288c0443SJeremy L Thompson 
451288c0443SJeremy L Thompson   // Create QFunction
452d1d35e2fSjeremylt   ierr = CeedQFunctionCreateInterior(ceed,
453d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].vec_length,
454d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].f,
455d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].source, qf);
456288c0443SJeremy L Thompson   CeedChk(ierr);
457288c0443SJeremy L Thompson 
458288c0443SJeremy L Thompson   // QFunction specific setup
459d1d35e2fSjeremylt   ierr = gallery_qfunctions[match_idx].init(ceed, name, *qf); CeedChk(ierr);
460288c0443SJeremy L Thompson 
46175affc3bSjeremylt   // Copy name
46275affc3bSjeremylt   size_t slen = strlen(name) + 1;
46375affc3bSjeremylt   ierr = CeedMalloc(slen, &name_copy); CeedChk(ierr);
46475affc3bSjeremylt   memcpy(name_copy, name, slen);
465d1d35e2fSjeremylt   (*qf)->qf_name = name_copy;
466e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
467288c0443SJeremy L Thompson }
468288c0443SJeremy L Thompson 
469288c0443SJeremy L Thompson /**
4700219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
4710219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
4720219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
4730219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
4740219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
4750219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
4760219ea01SJeremy L Thompson            location for both.
4770219ea01SJeremy L Thompson 
4780219ea01SJeremy L Thompson   @param ceed          A Ceed object where the CeedQFunction will be created
479d1d35e2fSjeremylt   @param[in] size      Size of the QFunction fields
480d1d35e2fSjeremylt   @param[in] in_mode   CeedEvalMode for input to CeedQFunction
481d1d35e2fSjeremylt   @param[in] out_mode  CeedEvalMode for output to CeedQFunction
4820219ea01SJeremy L Thompson   @param[out] qf       Address of the variable where the newly created
4830219ea01SJeremy L Thompson                          CeedQFunction will be stored
4840219ea01SJeremy L Thompson 
4850219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4860219ea01SJeremy L Thompson 
4877a982d89SJeremy L. Thompson   @ref User
4880219ea01SJeremy L Thompson **/
489d1d35e2fSjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode,
490d1d35e2fSjeremylt                                 CeedEvalMode out_mode, CeedQFunction *qf) {
4910219ea01SJeremy L Thompson   int ierr;
4920219ea01SJeremy L Thompson 
4930219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
494d1d35e2fSjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr);
495d1d35e2fSjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr);
4960219ea01SJeremy L Thompson 
497f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
498d1d35e2fSjeremylt   CeedInt *size_data;
499d1d35e2fSjeremylt   ierr = CeedCalloc(1, &size_data); CeedChk(ierr);
500d1d35e2fSjeremylt   size_data[0] = size;
501777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
502777ff853SJeremy L Thompson   ierr = CeedQFunctionContextCreate(ceed, &ctx); CeedChk(ierr);
503777ff853SJeremy L Thompson   ierr = CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_OWN_POINTER,
504d1d35e2fSjeremylt                                      sizeof(*size_data), (void *)size_data);
505777ff853SJeremy L Thompson   CeedChk(ierr);
506777ff853SJeremy L Thompson   ierr = CeedQFunctionSetContext(*qf, ctx); CeedChk(ierr);
507777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&ctx); CeedChk(ierr);
508e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5090219ea01SJeremy L Thompson }
5100219ea01SJeremy L Thompson 
5110219ea01SJeremy L Thompson /**
5129560d06aSjeremylt   @brief Copy the pointer to a CeedQFunction. Both pointers should
5139560d06aSjeremylt            be destroyed with `CeedQFunctionDestroy()`;
5149560d06aSjeremylt            Note: If `*qf_copy` is non-NULL, then it is assumed that
5159560d06aSjeremylt            `*qf_copy` is a pointer to a CeedQFunction. This
5169560d06aSjeremylt            CeedQFunction will be destroyed if `*qf_copy` is the only
5179560d06aSjeremylt            reference to this CeedQFunction.
5189560d06aSjeremylt 
5199560d06aSjeremylt   @param qf            CeedQFunction to copy reference to
5209560d06aSjeremylt   @param[out] qf_copy  Variable to store copied reference
5219560d06aSjeremylt 
5229560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
5239560d06aSjeremylt 
5249560d06aSjeremylt   @ref User
5259560d06aSjeremylt **/
5269560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
5279560d06aSjeremylt   int ierr;
5289560d06aSjeremylt 
5299560d06aSjeremylt   ierr = CeedQFunctionReference(qf); CeedChk(ierr);
5309560d06aSjeremylt   ierr = CeedQFunctionDestroy(qf_copy); CeedChk(ierr);
5319560d06aSjeremylt   *qf_copy = qf;
5329560d06aSjeremylt   return CEED_ERROR_SUCCESS;
5339560d06aSjeremylt }
5349560d06aSjeremylt 
5359560d06aSjeremylt /**
536a0a97fcfSJed Brown   @brief Add a CeedQFunction input
537b11c1e72Sjeremylt 
538b11c1e72Sjeremylt   @param qf          CeedQFunction
539d1d35e2fSjeremylt   @param field_name  Name of QFunction field
540d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
541d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
542d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
543b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
544b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
545b11c1e72Sjeremylt 
546b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
547dfdf5a53Sjeremylt 
5487a982d89SJeremy L. Thompson   @ref User
549b11c1e72Sjeremylt **/
550d1d35e2fSjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name,
551d1d35e2fSjeremylt                           CeedInt size,
552d1d35e2fSjeremylt                           CeedEvalMode eval_mode) {
553f04ea552SJeremy L Thompson   if (qf->is_immutable)
554e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
555e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
556f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
557e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
558d1d35e2fSjeremylt   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1))
559e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
560e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
561e15f9bd0SJeremy L Thompson                      "CEED_EVAL_WEIGHT should have size 1");
562e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
563d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields],
564d1d35e2fSjeremylt                                    field_name, size, eval_mode);
565fe2413ffSjeremylt   CeedChk(ierr);
566d1d35e2fSjeremylt   qf->num_input_fields++;
567e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
568d7b241e6Sjeremylt }
569d7b241e6Sjeremylt 
570b11c1e72Sjeremylt /**
571a0a97fcfSJed Brown   @brief Add a CeedQFunction output
572b11c1e72Sjeremylt 
573b11c1e72Sjeremylt   @param qf          CeedQFunction
574d1d35e2fSjeremylt   @param field_name  Name of QFunction field
575d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
576d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
577d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
578b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
579b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
580b11c1e72Sjeremylt 
581b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
582dfdf5a53Sjeremylt 
5837a982d89SJeremy L. Thompson   @ref User
584b11c1e72Sjeremylt **/
585d1d35e2fSjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name,
586d1d35e2fSjeremylt                            CeedInt size, CeedEvalMode eval_mode) {
587f04ea552SJeremy L Thompson   if (qf->is_immutable)
588e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
589e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
590f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
591e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
592d1d35e2fSjeremylt   if (eval_mode == CEED_EVAL_WEIGHT)
593c042f62fSJeremy L Thompson     // LCOV_EXCL_START
594e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
595e15f9bd0SJeremy L Thompson                      "Cannot create QFunction output with "
5961d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
597c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
598d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields],
599d1d35e2fSjeremylt                                    field_name, size, eval_mode);
600fe2413ffSjeremylt   CeedChk(ierr);
601d1d35e2fSjeremylt   qf->num_output_fields++;
602e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
603d7b241e6Sjeremylt }
604d7b241e6Sjeremylt 
605dfdf5a53Sjeremylt /**
60643bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
60743bbe138SJeremy L Thompson 
608f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
609f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
610f04ea552SJeremy L Thompson 
61143bbe138SJeremy L Thompson   @param qf                  CeedQFunction
61243bbe138SJeremy L Thompson   @param[out] input_fields   Variable to store input_fields
61343bbe138SJeremy L Thompson   @param[out] output_fields  Variable to store output_fields
61443bbe138SJeremy L Thompson 
61543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
61643bbe138SJeremy L Thompson 
617e9b533fbSJeremy L Thompson   @ref Advanced
61843bbe138SJeremy L Thompson **/
61943bbe138SJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields,
62043bbe138SJeremy L Thompson                            CeedQFunctionField **input_fields,
62143bbe138SJeremy L Thompson                            CeedInt *num_output_fields,
62243bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
623f04ea552SJeremy L Thompson   qf->is_immutable = true;
62443bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
62543bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
62643bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
62743bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
62843bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
62943bbe138SJeremy L Thompson }
63043bbe138SJeremy L Thompson 
63143bbe138SJeremy L Thompson /**
63243bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
63343bbe138SJeremy L Thompson 
63443bbe138SJeremy L Thompson   @param qf_field         CeedQFunctionField
63543bbe138SJeremy L Thompson   @param[out] field_name  Variable to store the field name
63643bbe138SJeremy L Thompson 
63743bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
63843bbe138SJeremy L Thompson 
639e9b533fbSJeremy L Thompson   @ref Advanced
64043bbe138SJeremy L Thompson **/
64143bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
64243bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
64343bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
64443bbe138SJeremy L Thompson }
64543bbe138SJeremy L Thompson 
64643bbe138SJeremy L Thompson /**
64743bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
64843bbe138SJeremy L Thompson 
64943bbe138SJeremy L Thompson   @param qf_field   CeedQFunctionField
65043bbe138SJeremy L Thompson   @param[out] size  Variable to store the size of the field
65143bbe138SJeremy L Thompson 
65243bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
65343bbe138SJeremy L Thompson 
654e9b533fbSJeremy L Thompson   @ref Advanced
65543bbe138SJeremy L Thompson **/
65643bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
65743bbe138SJeremy L Thompson   *size = qf_field->size;
65843bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
65943bbe138SJeremy L Thompson }
66043bbe138SJeremy L Thompson 
66143bbe138SJeremy L Thompson /**
66243bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
66343bbe138SJeremy L Thompson 
66443bbe138SJeremy L Thompson   @param qf_field        CeedQFunctionField
66543bbe138SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
66643bbe138SJeremy L Thompson 
66743bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
66843bbe138SJeremy L Thompson 
669e9b533fbSJeremy L Thompson   @ref Advanced
67043bbe138SJeremy L Thompson **/
67143bbe138SJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field,
67243bbe138SJeremy L Thompson                                   CeedEvalMode *eval_mode) {
67343bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
67443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
67543bbe138SJeremy L Thompson }
67643bbe138SJeremy L Thompson 
67743bbe138SJeremy L Thompson /**
6784ce2993fSjeremylt   @brief Set global context for a CeedQFunction
679b11c1e72Sjeremylt 
680b11c1e72Sjeremylt   @param qf   CeedQFunction
681b11c1e72Sjeremylt   @param ctx  Context data to set
682b11c1e72Sjeremylt 
683b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
684dfdf5a53Sjeremylt 
6857a982d89SJeremy L. Thompson   @ref User
686b11c1e72Sjeremylt **/
687777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
68834359f16Sjeremylt   int ierr;
689d7b241e6Sjeremylt   qf->ctx = ctx;
6909560d06aSjeremylt   ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr);
691e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
692d7b241e6Sjeremylt }
693d7b241e6Sjeremylt 
694b11c1e72Sjeremylt /**
69575affc3bSjeremylt   @brief View a CeedQFunction
69675affc3bSjeremylt 
69775affc3bSjeremylt   @param[in] qf      CeedQFunction to view
69875affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
69975affc3bSjeremylt 
70075affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
70175affc3bSjeremylt 
7027a982d89SJeremy L. Thompson   @ref User
70375affc3bSjeremylt **/
70475affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
70575affc3bSjeremylt   int ierr;
70675affc3bSjeremylt 
70775affc3bSjeremylt   fprintf(stream, "%sCeedQFunction %s\n",
708d1d35e2fSjeremylt           qf->qf_name ? "Gallery " : "User ", qf->qf_name ? qf->qf_name : "");
70975affc3bSjeremylt 
710d1d35e2fSjeremylt   fprintf(stream, "  %d Input Field%s:\n", qf->num_input_fields,
711d1d35e2fSjeremylt           qf->num_input_fields>1 ? "s" : "");
712d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_input_fields; i++) {
713d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream);
7142da88da5Sjeremylt     CeedChk(ierr);
71575affc3bSjeremylt   }
71675affc3bSjeremylt 
717d1d35e2fSjeremylt   fprintf(stream, "  %d Output Field%s:\n", qf->num_output_fields,
718d1d35e2fSjeremylt           qf->num_output_fields>1 ? "s" : "");
719d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_output_fields; i++) {
720d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream);
72175affc3bSjeremylt     CeedChk(ierr);
72275affc3bSjeremylt   }
723e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
72475affc3bSjeremylt }
72575affc3bSjeremylt 
72675affc3bSjeremylt /**
727*b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunction
728*b7c9bbdaSJeremy L Thompson 
729*b7c9bbdaSJeremy L Thompson   @param qf              CeedQFunction
730*b7c9bbdaSJeremy L Thompson   @param[out] ceed       Variable to store Ceed
731*b7c9bbdaSJeremy L Thompson 
732*b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
733*b7c9bbdaSJeremy L Thompson 
734*b7c9bbdaSJeremy L Thompson   @ref Advanced
735*b7c9bbdaSJeremy L Thompson **/
736*b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
737*b7c9bbdaSJeremy L Thompson   *ceed = qf->ceed;
738*b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
739*b7c9bbdaSJeremy L Thompson }
740*b7c9bbdaSJeremy L Thompson 
741*b7c9bbdaSJeremy L Thompson /**
742b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
743b11c1e72Sjeremylt 
744f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
745f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
746f04ea552SJeremy L Thompson 
747b11c1e72Sjeremylt   @param qf      CeedQFunction
748b11c1e72Sjeremylt   @param Q       Number of quadrature points
74934138859Sjeremylt   @param[in] u   Array of input CeedVectors
75034138859Sjeremylt   @param[out] v  Array of output CeedVectors
751b11c1e72Sjeremylt 
752b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
753dfdf5a53Sjeremylt 
7547a982d89SJeremy L. Thompson   @ref User
755b11c1e72Sjeremylt **/
756d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
757aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
758d7b241e6Sjeremylt   int ierr;
759d7b241e6Sjeremylt   if (!qf->Apply)
760c042f62fSJeremy L Thompson     // LCOV_EXCL_START
761e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED,
762e15f9bd0SJeremy L Thompson                      "Backend does not support QFunctionApply");
763c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
764d1d35e2fSjeremylt   if (Q % qf->vec_length)
765c042f62fSJeremy L Thompson     // LCOV_EXCL_START
766e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
767e15f9bd0SJeremy L Thompson                      "Number of quadrature points %d must be a "
768d1d35e2fSjeremylt                      "multiple of %d", Q, qf->vec_length);
769c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
770f04ea552SJeremy L Thompson   qf->is_immutable = true;
771d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
772e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
773d7b241e6Sjeremylt }
774d7b241e6Sjeremylt 
775b11c1e72Sjeremylt /**
776b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
777b11c1e72Sjeremylt 
778b11c1e72Sjeremylt   @param qf  CeedQFunction to destroy
779b11c1e72Sjeremylt 
780b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
781dfdf5a53Sjeremylt 
7827a982d89SJeremy L. Thompson   @ref User
783b11c1e72Sjeremylt **/
784d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
785d7b241e6Sjeremylt   int ierr;
786d7b241e6Sjeremylt 
787d1d35e2fSjeremylt   if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS;
788fe2413ffSjeremylt   // Backend destroy
789d7b241e6Sjeremylt   if ((*qf)->Destroy) {
790d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
791d7b241e6Sjeremylt   }
792fe2413ffSjeremylt   // Free fields
793d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_input_fields; i++) {
794d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr);
795d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr);
796fe2413ffSjeremylt   }
797d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_output_fields; i++) {
798d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr);
799d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr);
800fe2413ffSjeremylt   }
801d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr);
802d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr);
803777ff853SJeremy L Thompson 
804777ff853SJeremy L Thompson   // User context data object
805777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
806fe2413ffSjeremylt 
807d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr);
808d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->qf_name); CeedChk(ierr);
809d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
810d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
811e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
812d7b241e6Sjeremylt }
813d7b241e6Sjeremylt 
814d7b241e6Sjeremylt /// @}
815