xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision 34359f16efbc10f0e69405f262a23387c38de222)
1d7b241e6Sjeremylt // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2d7b241e6Sjeremylt // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3d7b241e6Sjeremylt // reserved. See files LICENSE and NOTICE for details.
4d7b241e6Sjeremylt //
5d7b241e6Sjeremylt // This file is part of CEED, a collection of benchmarks, miniapps, software
6d7b241e6Sjeremylt // libraries and APIs for efficient high-order finite element and spectral
7d7b241e6Sjeremylt // element discretizations for exascale applications. For more information and
8d7b241e6Sjeremylt // source code availability see http://github.com/ceed.
9d7b241e6Sjeremylt //
10d7b241e6Sjeremylt // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11d7b241e6Sjeremylt // a collaborative effort of two U.S. Department of Energy organizations (Office
12d7b241e6Sjeremylt // of Science and the National Nuclear Security Administration) responsible for
13d7b241e6Sjeremylt // the planning and preparation of a capable exascale ecosystem, including
14d7b241e6Sjeremylt // software, applications, hardware, advanced system engineering and early
15d7b241e6Sjeremylt // testbed platforms, in support of the nation's exascale computing imperative.
16d7b241e6Sjeremylt 
17ec3da8bcSJed Brown #include <ceed/ceed.h>
18ec3da8bcSJed Brown #include <ceed/backend.h>
193d576824SJeremy L Thompson #include <ceed-impl.h>
20288c0443SJeremy L Thompson #include <limits.h>
213d576824SJeremy L Thompson #include <stdbool.h>
223d576824SJeremy L Thompson #include <stdio.h>
233d576824SJeremy L Thompson #include <string.h>
24288c0443SJeremy L Thompson 
257a982d89SJeremy L. Thompson /// @file
267a982d89SJeremy L. Thompson /// Implementation of public CeedQFunction interfaces
277a982d89SJeremy L. Thompson 
28288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP
29442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none;
30442e7f0bSjeremylt /// @endcond
31442e7f0bSjeremylt 
327a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
337a982d89SJeremy L. Thompson /// @{
347a982d89SJeremy L. Thompson 
357a982d89SJeremy L. Thompson // Indicate that no QFunction is provided by the user
367a982d89SJeremy L. Thompson const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
377a982d89SJeremy L. Thompson 
387a982d89SJeremy L. Thompson /// @}
397a982d89SJeremy L. Thompson 
40442e7f0bSjeremylt /// @cond DOXYGEN_SKIP
41288c0443SJeremy L Thompson static struct {
42288c0443SJeremy L Thompson   char name[CEED_MAX_RESOURCE_LEN];
43288c0443SJeremy L Thompson   char source[CEED_MAX_RESOURCE_LEN];
44d1d35e2fSjeremylt   CeedInt vec_length;
45288c0443SJeremy L Thompson   CeedQFunctionUser f;
46288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
47d1d35e2fSjeremylt } gallery_qfunctions[1024];
48288c0443SJeremy L Thompson static size_t num_qfunctions;
49288c0443SJeremy L Thompson /// @endcond
50d7b241e6Sjeremylt 
51777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
52777ff853SJeremy L Thompson /// CeedQFunction Library Internal Functions
53777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
54777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper
55777ff853SJeremy L Thompson /// @{
56777ff853SJeremy L Thompson 
57b11c1e72Sjeremylt /**
58288c0443SJeremy L Thompson   @brief Register a gallery QFunction
59288c0443SJeremy L Thompson 
60288c0443SJeremy L Thompson   @param name        Name for this backend to respond to
611176cc3aSJeremy L Thompson   @param source      Absolute path to source of QFunction,
621176cc3aSJeremy L Thompson                        "\path\CEED_DIR\gallery\folder\file.h:function_name"
63d1d35e2fSjeremylt   @param vec_length  Vector length.  Caller must ensure that number of quadrature
64d1d35e2fSjeremylt                        points is a multiple of vec_length.
65288c0443SJeremy L Thompson   @param f           Function pointer to evaluate action at quadrature points.
66288c0443SJeremy L Thompson                        See \ref CeedQFunctionUser.
67288c0443SJeremy L Thompson   @param init        Initialization function called by CeedQFunctionInit() when the
68288c0443SJeremy L Thompson                        QFunction is selected.
69288c0443SJeremy L Thompson 
70288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
71288c0443SJeremy L Thompson 
727a982d89SJeremy L. Thompson   @ref Developer
73288c0443SJeremy L Thompson **/
74288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source,
75d1d35e2fSjeremylt                           CeedInt vec_length, CeedQFunctionUser f,
76288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
77d1d35e2fSjeremylt   if (num_qfunctions >= sizeof(gallery_qfunctions) / sizeof(
78d1d35e2fSjeremylt         gallery_qfunctions[0]))
79c042f62fSJeremy L Thompson     // LCOV_EXCL_START
80e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
81c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
82c042f62fSJeremy L Thompson 
83d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
84d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
85d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].source, source,
86d1d35e2fSjeremylt           CEED_MAX_RESOURCE_LEN);
87d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
88d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].vec_length = vec_length;
89d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].f = f;
90d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].init = init;
91288c0443SJeremy L Thompson   num_qfunctions++;
92e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
93288c0443SJeremy L Thompson }
94288c0443SJeremy L Thompson 
95288c0443SJeremy L Thompson /**
967a982d89SJeremy L. Thompson   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
977a982d89SJeremy L. Thompson 
987a982d89SJeremy L. Thompson   @param f           CeedQFunctionField
99d1d35e2fSjeremylt   @param field_name  Name of QFunction field
100d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
101d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT
102d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
1037a982d89SJeremy L. Thompson                        \ref CEED_EVAL_INTERP to use interpolated values,
1047a982d89SJeremy L. Thompson                        \ref CEED_EVAL_GRAD to use gradients,
1057a982d89SJeremy L. Thompson                        \ref CEED_EVAL_WEIGHT to use quadrature weights.
1067a982d89SJeremy L. Thompson 
1077a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1087a982d89SJeremy L. Thompson 
1097a982d89SJeremy L. Thompson   @ref Developer
1107a982d89SJeremy L. Thompson **/
111d1d35e2fSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *field_name,
112d1d35e2fSjeremylt                                  CeedInt size, CeedEvalMode eval_mode) {
113d1d35e2fSjeremylt   size_t len = strlen(field_name);
1147a982d89SJeremy L. Thompson   char *tmp;
1157a982d89SJeremy L. Thompson   int ierr;
1167a982d89SJeremy L. Thompson 
117e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, f); CeedChk(ierr);
1187a982d89SJeremy L. Thompson   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
119d1d35e2fSjeremylt   memcpy(tmp, field_name, len+1);
120d1d35e2fSjeremylt   (*f)->field_name = tmp;
1217a982d89SJeremy L. Thompson   (*f)->size = size;
122d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
123e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1247a982d89SJeremy L. Thompson }
1257a982d89SJeremy L. Thompson 
1267a982d89SJeremy L. Thompson /**
1277a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1287a982d89SJeremy L. Thompson 
1297a982d89SJeremy L. Thompson   @param[in] field         QFunction field to view
130d1d35e2fSjeremylt   @param[in] field_number  Number of field being viewed
1314c4400c7SValeria Barra   @param[in] in            true for input field, false for output
1327a982d89SJeremy L. Thompson   @param[in] stream        Stream to view to, e.g., stdout
1337a982d89SJeremy L. Thompson 
1347a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1357a982d89SJeremy L. Thompson 
1367a982d89SJeremy L. Thompson   @ref Utility
1377a982d89SJeremy L. Thompson **/
138d1d35e2fSjeremylt static int CeedQFunctionFieldView(CeedQFunctionField field,
139d1d35e2fSjeremylt                                   CeedInt field_number,
1407a982d89SJeremy L. Thompson                                   bool in, FILE *stream) {
1417a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1427a982d89SJeremy L. Thompson   fprintf(stream, "    %s Field [%d]:\n"
1437a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1447a982d89SJeremy L. Thompson           "      Size: %d\n"
1457a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
146d1d35e2fSjeremylt           inout, field_number, field->field_name, field->size,
147d1d35e2fSjeremylt           CeedEvalModes[field->eval_mode]);
148e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1497a982d89SJeremy L. Thompson }
1507a982d89SJeremy L. Thompson 
151777ff853SJeremy L Thompson /**
152777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
153777ff853SJeremy L Thompson 
154777ff853SJeremy L Thompson   @param qf      CeedQFunction
155777ff853SJeremy L Thompson   @param status  Boolean value to set as Fortran status
156777ff853SJeremy L Thompson 
157777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
158777ff853SJeremy L Thompson 
159777ff853SJeremy L Thompson   @ref Backend
160777ff853SJeremy L Thompson **/
161777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
162d1d35e2fSjeremylt   qf->fortran_status = status;
163e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
164777ff853SJeremy L Thompson }
165777ff853SJeremy L Thompson 
1667a982d89SJeremy L. Thompson /// @}
1677a982d89SJeremy L. Thompson 
1687a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1697a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1707a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1717a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1727a982d89SJeremy L. Thompson /// @{
1737a982d89SJeremy L. Thompson 
1747a982d89SJeremy L. Thompson /**
1757a982d89SJeremy L. Thompson   @brief Get the Ceed associated with a CeedQFunction
1767a982d89SJeremy L. Thompson 
1777a982d89SJeremy L. Thompson   @param qf              CeedQFunction
1787a982d89SJeremy L. Thompson   @param[out] ceed       Variable to store Ceed
1797a982d89SJeremy L. Thompson 
1807a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1817a982d89SJeremy L. Thompson 
1827a982d89SJeremy L. Thompson   @ref Backend
1837a982d89SJeremy L. Thompson **/
1847a982d89SJeremy L. Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
1857a982d89SJeremy L. Thompson   *ceed = qf->ceed;
186e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1877a982d89SJeremy L. Thompson }
1887a982d89SJeremy L. Thompson 
1897a982d89SJeremy L. Thompson /**
1907a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1917a982d89SJeremy L. Thompson 
1927a982d89SJeremy L. Thompson   @param qf               CeedQFunction
193d1d35e2fSjeremylt   @param[out] vec_length  Variable to store vector length
1947a982d89SJeremy L. Thompson 
1957a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1967a982d89SJeremy L. Thompson 
1977a982d89SJeremy L. Thompson   @ref Backend
1987a982d89SJeremy L. Thompson **/
199d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
200d1d35e2fSjeremylt   *vec_length = qf->vec_length;
201e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2027a982d89SJeremy L. Thompson }
2037a982d89SJeremy L. Thompson 
2047a982d89SJeremy L. Thompson /**
2057a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
2067a982d89SJeremy L. Thompson 
2077a982d89SJeremy L. Thompson   @param qf               CeedQFunction
208d1d35e2fSjeremylt   @param[out] num_input   Variable to store number of input fields
209d1d35e2fSjeremylt   @param[out] num_output  Variable to store number of output fields
2107a982d89SJeremy L. Thompson 
2117a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2127a982d89SJeremy L. Thompson 
2137a982d89SJeremy L. Thompson   @ref Backend
2147a982d89SJeremy L. Thompson **/
215d1d35e2fSjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input,
216d1d35e2fSjeremylt                             CeedInt *num_output) {
217d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
218d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
219e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2207a982d89SJeremy L. Thompson }
2217a982d89SJeremy L. Thompson 
2227a982d89SJeremy L. Thompson /**
2237a982d89SJeremy L. Thompson   @brief Get the source path string for a CeedQFunction
2247a982d89SJeremy L. Thompson 
2257a982d89SJeremy L. Thompson   @param qf           CeedQFunction
2267a982d89SJeremy L. Thompson   @param[out] source  Variable to store source path string
2277a982d89SJeremy L. Thompson 
2287a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2297a982d89SJeremy L. Thompson 
2307a982d89SJeremy L. Thompson   @ref Backend
2317a982d89SJeremy L. Thompson **/
2327a982d89SJeremy L. Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source) {
233d1d35e2fSjeremylt   *source = (char *) qf->source_path;
234e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2357a982d89SJeremy L. Thompson }
2367a982d89SJeremy L. Thompson 
2377a982d89SJeremy L. Thompson /**
2387a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2397a982d89SJeremy L. Thompson 
2407a982d89SJeremy L. Thompson   @param qf      CeedQFunction
2417a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
2427a982d89SJeremy L. Thompson 
2437a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2447a982d89SJeremy L. Thompson 
2457a982d89SJeremy L. Thompson   @ref Backend
2467a982d89SJeremy L. Thompson **/
2477a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
2487a982d89SJeremy L. Thompson   *f = qf->function;
249e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2507a982d89SJeremy L. Thompson }
2517a982d89SJeremy L. Thompson 
2527a982d89SJeremy L. Thompson /**
253777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
254777ff853SJeremy L Thompson            Note: For QFunctions from the Fortran interface, this
255777ff853SJeremy L Thompson              function will return the Fortran context
256777ff853SJeremy L Thompson              CeedQFunctionContext.
2577a982d89SJeremy L. Thompson 
2587a982d89SJeremy L. Thompson   @param qf        CeedQFunction
259777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2607a982d89SJeremy L. Thompson 
2617a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2627a982d89SJeremy L. Thompson 
2637a982d89SJeremy L. Thompson   @ref Backend
2647a982d89SJeremy L. Thompson **/
265777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
2667a982d89SJeremy L. Thompson   *ctx = qf->ctx;
267e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2687a982d89SJeremy L. Thompson }
2697a982d89SJeremy L. Thompson 
2707a982d89SJeremy L. Thompson /**
2717a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
272777ff853SJeremy L Thompson            Note: For all QFunctions this function will return the user
273777ff853SJeremy L Thompson              CeedQFunctionContext and not interface context
274777ff853SJeremy L Thompson              CeedQFunctionContext, if any such object exists.
2757a982d89SJeremy L. Thompson 
2767a982d89SJeremy L. Thompson   @param qf        CeedQFunction
277777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2787a982d89SJeremy L. Thompson 
2797a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2807a982d89SJeremy L. Thompson   @ref Backend
2817a982d89SJeremy L. Thompson **/
282777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
283777ff853SJeremy L Thompson   int ierr;
284d1d35e2fSjeremylt   if (qf->fortran_status) {
285d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
286d1d35e2fSjeremylt     ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx);
287777ff853SJeremy L Thompson     CeedChk(ierr);
288d1d35e2fSjeremylt     *ctx = fortran_ctx->innerctx;
289d1d35e2fSjeremylt     ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx);
290d1d35e2fSjeremylt     CeedChk(ierr);
2917a982d89SJeremy L. Thompson   } else {
2927a982d89SJeremy L. Thompson     *ctx = qf->ctx;
2937a982d89SJeremy L. Thompson   }
294e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2957a982d89SJeremy L. Thompson }
2967a982d89SJeremy L. Thompson 
2977a982d89SJeremy L. Thompson /**
2987a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
2997a982d89SJeremy L. Thompson 
3007a982d89SJeremy L. Thompson   @param qf                CeedQFunction
301d1d35e2fSjeremylt   @param[out] is_identity  Variable to store identity status
3027a982d89SJeremy L. Thompson 
3037a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3047a982d89SJeremy L. Thompson 
3057a982d89SJeremy L. Thompson   @ref Backend
3067a982d89SJeremy L. Thompson **/
307d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
308d1d35e2fSjeremylt   *is_identity = qf->identity;
309e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3107a982d89SJeremy L. Thompson }
3117a982d89SJeremy L. Thompson 
3127a982d89SJeremy L. Thompson /**
3137a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
3147a982d89SJeremy L. Thompson 
3157a982d89SJeremy L. Thompson   @param qf         CeedQFunction
3167a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
3177a982d89SJeremy L. Thompson 
3187a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3197a982d89SJeremy L. Thompson 
3207a982d89SJeremy L. Thompson   @ref Backend
3217a982d89SJeremy L. Thompson **/
322777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
323777ff853SJeremy L Thompson   *(void **)data = qf->data;
324e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3257a982d89SJeremy L. Thompson }
3267a982d89SJeremy L. Thompson 
3277a982d89SJeremy L. Thompson /**
3287a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
3297a982d89SJeremy L. Thompson 
3307a982d89SJeremy L. Thompson   @param[out] qf  CeedQFunction
3317a982d89SJeremy L. Thompson   @param data     Data to set
3327a982d89SJeremy L. Thompson 
3337a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3347a982d89SJeremy L. Thompson 
3357a982d89SJeremy L. Thompson   @ref Backend
3367a982d89SJeremy L. Thompson **/
337777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
338777ff853SJeremy L Thompson   qf->data = data;
339e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3407a982d89SJeremy L. Thompson }
3417a982d89SJeremy L. Thompson 
3427a982d89SJeremy L. Thompson /**
343*34359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
344*34359f16Sjeremylt 
345*34359f16Sjeremylt   @param qf  CeedQFunction to increment the reference counter
346*34359f16Sjeremylt 
347*34359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
348*34359f16Sjeremylt 
349*34359f16Sjeremylt   @ref Backend
350*34359f16Sjeremylt **/
351*34359f16Sjeremylt int CeedQFunctionIncrementRefCounter(CeedQFunction qf) {
352*34359f16Sjeremylt   qf->ref_count++;
353*34359f16Sjeremylt   return CEED_ERROR_SUCCESS;
354*34359f16Sjeremylt }
355*34359f16Sjeremylt 
356*34359f16Sjeremylt /**
3577a982d89SJeremy L. Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
3587a982d89SJeremy L. Thompson 
3597a982d89SJeremy L. Thompson   @param qf                  CeedQFunction
360d1d35e2fSjeremylt   @param[out] input_fields   Variable to store input_fields
361d1d35e2fSjeremylt   @param[out] output_fields  Variable to store output_fields
3627a982d89SJeremy L. Thompson 
3637a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3647a982d89SJeremy L. Thompson 
3657a982d89SJeremy L. Thompson   @ref Backend
3667a982d89SJeremy L. Thompson **/
367d1d35e2fSjeremylt int CeedQFunctionGetFields(CeedQFunction qf, CeedQFunctionField **input_fields,
368d1d35e2fSjeremylt                            CeedQFunctionField **output_fields) {
369d1d35e2fSjeremylt   if (input_fields) *input_fields = qf->input_fields;
370d1d35e2fSjeremylt   if (output_fields) *output_fields = qf->output_fields;
371e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3727a982d89SJeremy L. Thompson }
3737a982d89SJeremy L. Thompson 
3747a982d89SJeremy L. Thompson /**
3757a982d89SJeremy L. Thompson   @brief Get the name of a CeedQFunctionField
3767a982d89SJeremy L. Thompson 
377d1d35e2fSjeremylt   @param qf_field         CeedQFunctionField
378d1d35e2fSjeremylt   @param[out] field_name  Variable to store the field name
3797a982d89SJeremy L. Thompson 
3807a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3817a982d89SJeremy L. Thompson 
3827a982d89SJeremy L. Thompson   @ref Backend
3837a982d89SJeremy L. Thompson **/
384d1d35e2fSjeremylt int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
385d1d35e2fSjeremylt   *field_name = (char *)qf_field->field_name;
386e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3877a982d89SJeremy L. Thompson }
3887a982d89SJeremy L. Thompson 
3897a982d89SJeremy L. Thompson /**
3907a982d89SJeremy L. Thompson   @brief Get the number of components of a CeedQFunctionField
3917a982d89SJeremy L. Thompson 
392d1d35e2fSjeremylt   @param qf_field   CeedQFunctionField
3937a982d89SJeremy L. Thompson   @param[out] size  Variable to store the size of the field
3947a982d89SJeremy L. Thompson 
3957a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3967a982d89SJeremy L. Thompson 
3977a982d89SJeremy L. Thompson   @ref Backend
3987a982d89SJeremy L. Thompson **/
399d1d35e2fSjeremylt int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
400d1d35e2fSjeremylt   *size = qf_field->size;
401e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4027a982d89SJeremy L. Thompson }
4037a982d89SJeremy L. Thompson 
4047a982d89SJeremy L. Thompson /**
4057a982d89SJeremy L. Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
4067a982d89SJeremy L. Thompson 
407d1d35e2fSjeremylt   @param qf_field        CeedQFunctionField
408d1d35e2fSjeremylt   @param[out] eval_mode  Variable to store the field evaluation mode
4097a982d89SJeremy L. Thompson 
4107a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4117a982d89SJeremy L. Thompson 
4127a982d89SJeremy L. Thompson   @ref Backend
4137a982d89SJeremy L. Thompson **/
414d1d35e2fSjeremylt int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field,
415d1d35e2fSjeremylt                                   CeedEvalMode *eval_mode) {
416d1d35e2fSjeremylt   *eval_mode = qf_field->eval_mode;
417e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4187a982d89SJeremy L. Thompson }
4197a982d89SJeremy L. Thompson 
4207a982d89SJeremy L. Thompson /// @}
4217a982d89SJeremy L. Thompson 
4227a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4237a982d89SJeremy L. Thompson /// CeedQFunction Public API
4247a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4257a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
4267a982d89SJeremy L. Thompson /// @{
4277a982d89SJeremy L. Thompson 
4287a982d89SJeremy L. Thompson /**
4297a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
4307a982d89SJeremy L. Thompson 
4317a982d89SJeremy L. Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
432d1d35e2fSjeremylt   @param vec_length  Vector length. Caller must ensure that number of quadrature
433d1d35e2fSjeremylt                        points is a multiple of vec_length.
4347a982d89SJeremy L. Thompson   @param f           Function pointer to evaluate action at quadrature points.
4357a982d89SJeremy L. Thompson                        See \ref CeedQFunctionUser.
4367a982d89SJeremy L. Thompson   @param source      Absolute path to source of QFunction,
437c8d5b03cSJeremy L Thompson                        "\abs_path\file.h:function_name".
438c8d5b03cSJeremy L Thompson                        For support across all backends, this source must only
439c8d5b03cSJeremy L Thompson                        contain constructs supported by C99, C++11, and CUDA.
4407a982d89SJeremy L. Thompson   @param[out] qf     Address of the variable where the newly created
4417a982d89SJeremy L. Thompson                        CeedQFunction will be stored
4427a982d89SJeremy L. Thompson 
4437a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4447a982d89SJeremy L. Thompson 
4457a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
4467a982d89SJeremy L. Thompson     arguments.
4477a982d89SJeremy L. Thompson 
4487a982d89SJeremy L. Thompson   @ref User
4497a982d89SJeremy L. Thompson **/
450d1d35e2fSjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length,
451d1d35e2fSjeremylt                                 CeedQFunctionUser f,
4527a982d89SJeremy L. Thompson                                 const char *source, CeedQFunction *qf) {
4537a982d89SJeremy L. Thompson   int ierr;
4547a982d89SJeremy L. Thompson   char *source_copy;
4557a982d89SJeremy L. Thompson 
4567a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
4577a982d89SJeremy L. Thompson     Ceed delegate;
4587a982d89SJeremy L. Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
4597a982d89SJeremy L. Thompson 
4607a982d89SJeremy L. Thompson     if (!delegate)
4617a982d89SJeremy L. Thompson       // LCOV_EXCL_START
462e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
463e15f9bd0SJeremy L Thompson                        "Backend does not support QFunctionCreate");
4647a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
4657a982d89SJeremy L. Thompson 
466d1d35e2fSjeremylt     ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf);
4677a982d89SJeremy L. Thompson     CeedChk(ierr);
468e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
4697a982d89SJeremy L. Thompson   }
4707a982d89SJeremy L. Thompson 
4717a982d89SJeremy L. Thompson   ierr = CeedCalloc(1, qf); CeedChk(ierr);
4727a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
473*34359f16Sjeremylt   ierr = CeedIncrementRefCounter(ceed); CeedChk(ierr);
474d1d35e2fSjeremylt   (*qf)->ref_count = 1;
475d1d35e2fSjeremylt   (*qf)->vec_length = vec_length;
4767a982d89SJeremy L. Thompson   (*qf)->identity = 0;
4777a982d89SJeremy L. Thompson   (*qf)->function = f;
4787a982d89SJeremy L. Thompson   size_t slen = strlen(source) + 1;
4797a982d89SJeremy L. Thompson   ierr = CeedMalloc(slen, &source_copy); CeedChk(ierr);
4807a982d89SJeremy L. Thompson   memcpy(source_copy, source, slen);
481d1d35e2fSjeremylt   (*qf)->source_path = source_copy;
482d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->input_fields); CeedChk(ierr);
483d1d35e2fSjeremylt   ierr = CeedCalloc(16, &(*qf)->output_fields); CeedChk(ierr);
4847a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
485e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4867a982d89SJeremy L. Thompson }
4877a982d89SJeremy L. Thompson 
4887a982d89SJeremy L. Thompson /**
489288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
490288c0443SJeremy L Thompson 
491288c0443SJeremy L Thompson   @param ceed     A Ceed object where the CeedQFunction will be created
492288c0443SJeremy L Thompson   @param name     Name of QFunction to use from gallery
493288c0443SJeremy L Thompson   @param[out] qf  Address of the variable where the newly created
494288c0443SJeremy L Thompson                     CeedQFunction will be stored
495288c0443SJeremy L Thompson 
496288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
497288c0443SJeremy L Thompson 
4987a982d89SJeremy L. Thompson   @ref User
499288c0443SJeremy L Thompson **/
500288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
501288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
502288c0443SJeremy L Thompson   int ierr;
503d1d35e2fSjeremylt   size_t match_len = 0, match_idx = UINT_MAX;
50475affc3bSjeremylt   char *name_copy;
505288c0443SJeremy L Thompson 
5061d013790SJed Brown   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
507288c0443SJeremy L Thompson   // Find matching backend
508e15f9bd0SJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE,
509e15f9bd0SJeremy L Thompson                                 "No QFunction name provided");
510288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
511288c0443SJeremy L Thompson     size_t n;
512d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
513d1d35e2fSjeremylt     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {}
514d1d35e2fSjeremylt     if (n > match_len) {
515d1d35e2fSjeremylt       match_len = n;
516d1d35e2fSjeremylt       match_idx = i;
517288c0443SJeremy L Thompson     }
518288c0443SJeremy L Thompson   }
519d1d35e2fSjeremylt   if (!match_len)
520138d4072Sjeremylt     // LCOV_EXCL_START
521e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
522138d4072Sjeremylt   // LCOV_EXCL_STOP
523288c0443SJeremy L Thompson 
524288c0443SJeremy L Thompson   // Create QFunction
525d1d35e2fSjeremylt   ierr = CeedQFunctionCreateInterior(ceed,
526d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].vec_length,
527d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].f,
528d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].source, qf);
529288c0443SJeremy L Thompson   CeedChk(ierr);
530288c0443SJeremy L Thompson 
531288c0443SJeremy L Thompson   // QFunction specific setup
532d1d35e2fSjeremylt   ierr = gallery_qfunctions[match_idx].init(ceed, name, *qf); CeedChk(ierr);
533288c0443SJeremy L Thompson 
53475affc3bSjeremylt   // Copy name
53575affc3bSjeremylt   size_t slen = strlen(name) + 1;
53675affc3bSjeremylt   ierr = CeedMalloc(slen, &name_copy); CeedChk(ierr);
53775affc3bSjeremylt   memcpy(name_copy, name, slen);
538d1d35e2fSjeremylt   (*qf)->qf_name = name_copy;
539e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
540288c0443SJeremy L Thompson }
541288c0443SJeremy L Thompson 
542288c0443SJeremy L Thompson /**
5430219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
5440219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
5450219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
5460219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
5470219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
5480219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
5490219ea01SJeremy L Thompson            location for both.
5500219ea01SJeremy L Thompson 
5510219ea01SJeremy L Thompson   @param ceed          A Ceed object where the CeedQFunction will be created
552d1d35e2fSjeremylt   @param[in] size      Size of the QFunction fields
553d1d35e2fSjeremylt   @param[in] in_mode   CeedEvalMode for input to CeedQFunction
554d1d35e2fSjeremylt   @param[in] out_mode  CeedEvalMode for output to CeedQFunction
5550219ea01SJeremy L Thompson   @param[out] qf       Address of the variable where the newly created
5560219ea01SJeremy L Thompson                          CeedQFunction will be stored
5570219ea01SJeremy L Thompson 
5580219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5590219ea01SJeremy L Thompson 
5607a982d89SJeremy L. Thompson   @ref User
5610219ea01SJeremy L Thompson **/
562d1d35e2fSjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode,
563d1d35e2fSjeremylt                                 CeedEvalMode out_mode, CeedQFunction *qf) {
5640219ea01SJeremy L Thompson   int ierr;
5650219ea01SJeremy L Thompson 
566d1d35e2fSjeremylt   if (in_mode == CEED_EVAL_NONE && out_mode == CEED_EVAL_NONE)
56760f77c51Sjeremylt     // LCOV_EXCL_START
568e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
569e15f9bd0SJeremy L Thompson                      "CEED_EVAL_NONE for a both the input and "
57060f77c51Sjeremylt                      "output does not make sense with an identity QFunction");
57160f77c51Sjeremylt   // LCOV_EXCL_STOP
57260f77c51Sjeremylt 
5730219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
574d1d35e2fSjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr);
575d1d35e2fSjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr);
5760219ea01SJeremy L Thompson 
5770219ea01SJeremy L Thompson   (*qf)->identity = 1;
578d1d35e2fSjeremylt   CeedInt *size_data;
579d1d35e2fSjeremylt   ierr = CeedCalloc(1, &size_data); CeedChk(ierr);
580d1d35e2fSjeremylt   size_data[0] = size;
581777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
582777ff853SJeremy L Thompson   ierr = CeedQFunctionContextCreate(ceed, &ctx); CeedChk(ierr);
583777ff853SJeremy L Thompson   ierr = CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_OWN_POINTER,
584d1d35e2fSjeremylt                                      sizeof(*size_data), (void *)size_data);
585777ff853SJeremy L Thompson   CeedChk(ierr);
586777ff853SJeremy L Thompson   ierr = CeedQFunctionSetContext(*qf, ctx); CeedChk(ierr);
587777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&ctx); CeedChk(ierr);
588e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5890219ea01SJeremy L Thompson }
5900219ea01SJeremy L Thompson 
5910219ea01SJeremy L Thompson /**
592a0a97fcfSJed Brown   @brief Add a CeedQFunction input
593b11c1e72Sjeremylt 
594b11c1e72Sjeremylt   @param qf          CeedQFunction
595d1d35e2fSjeremylt   @param field_name  Name of QFunction field
596d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
597d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
598d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
599b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
600b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
601b11c1e72Sjeremylt 
602b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
603dfdf5a53Sjeremylt 
6047a982d89SJeremy L. Thompson   @ref User
605b11c1e72Sjeremylt **/
606d1d35e2fSjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name,
607d1d35e2fSjeremylt                           CeedInt size,
608d1d35e2fSjeremylt                           CeedEvalMode eval_mode) {
609d1d35e2fSjeremylt   if (qf->operators_set)
610e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
611e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
612e15f9bd0SJeremy L Thompson                      "QFunction cannot be changed when in use by an operator");
613e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
614d1d35e2fSjeremylt   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1))
615e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
616e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
617e15f9bd0SJeremy L Thompson                      "CEED_EVAL_WEIGHT should have size 1");
618e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
619d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields],
620d1d35e2fSjeremylt                                    field_name, size, eval_mode);
621fe2413ffSjeremylt   CeedChk(ierr);
622d1d35e2fSjeremylt   qf->num_input_fields++;
623e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
624d7b241e6Sjeremylt }
625d7b241e6Sjeremylt 
626b11c1e72Sjeremylt /**
627a0a97fcfSJed Brown   @brief Add a CeedQFunction output
628b11c1e72Sjeremylt 
629b11c1e72Sjeremylt   @param qf          CeedQFunction
630d1d35e2fSjeremylt   @param field_name  Name of QFunction field
631d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
632d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
633d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
634b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
635b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
636b11c1e72Sjeremylt 
637b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
638dfdf5a53Sjeremylt 
6397a982d89SJeremy L. Thompson   @ref User
640b11c1e72Sjeremylt **/
641d1d35e2fSjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name,
642d1d35e2fSjeremylt                            CeedInt size, CeedEvalMode eval_mode) {
643d1d35e2fSjeremylt   if (qf->operators_set)
644e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
645e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
646e15f9bd0SJeremy L Thompson                      "QFunction cannot be changed when in use by an operator");
647e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
648d1d35e2fSjeremylt   if (eval_mode == CEED_EVAL_WEIGHT)
649c042f62fSJeremy L Thompson     // LCOV_EXCL_START
650e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
651e15f9bd0SJeremy L Thompson                      "Cannot create QFunction output with "
6521d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
653c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
654d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields],
655d1d35e2fSjeremylt                                    field_name, size, eval_mode);
656fe2413ffSjeremylt   CeedChk(ierr);
657d1d35e2fSjeremylt   qf->num_output_fields++;
658e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
659d7b241e6Sjeremylt }
660d7b241e6Sjeremylt 
661dfdf5a53Sjeremylt /**
6624ce2993fSjeremylt   @brief Set global context for a CeedQFunction
663b11c1e72Sjeremylt 
664b11c1e72Sjeremylt   @param qf   CeedQFunction
665b11c1e72Sjeremylt   @param ctx  Context data to set
666b11c1e72Sjeremylt 
667b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
668dfdf5a53Sjeremylt 
6697a982d89SJeremy L. Thompson   @ref User
670b11c1e72Sjeremylt **/
671777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
672*34359f16Sjeremylt   int ierr;
673d7b241e6Sjeremylt   qf->ctx = ctx;
674*34359f16Sjeremylt   ierr = CeedQFunctionContextIncrementRefCounter(ctx); CeedChk(ierr);
675e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
676d7b241e6Sjeremylt }
677d7b241e6Sjeremylt 
678b11c1e72Sjeremylt /**
67975affc3bSjeremylt   @brief View a CeedQFunction
68075affc3bSjeremylt 
68175affc3bSjeremylt   @param[in] qf      CeedQFunction to view
68275affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
68375affc3bSjeremylt 
68475affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
68575affc3bSjeremylt 
6867a982d89SJeremy L. Thompson   @ref User
68775affc3bSjeremylt **/
68875affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
68975affc3bSjeremylt   int ierr;
69075affc3bSjeremylt 
69175affc3bSjeremylt   fprintf(stream, "%sCeedQFunction %s\n",
692d1d35e2fSjeremylt           qf->qf_name ? "Gallery " : "User ", qf->qf_name ? qf->qf_name : "");
69375affc3bSjeremylt 
694d1d35e2fSjeremylt   fprintf(stream, "  %d Input Field%s:\n", qf->num_input_fields,
695d1d35e2fSjeremylt           qf->num_input_fields>1 ? "s" : "");
696d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_input_fields; i++) {
697d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream);
6982da88da5Sjeremylt     CeedChk(ierr);
69975affc3bSjeremylt   }
70075affc3bSjeremylt 
701d1d35e2fSjeremylt   fprintf(stream, "  %d Output Field%s:\n", qf->num_output_fields,
702d1d35e2fSjeremylt           qf->num_output_fields>1 ? "s" : "");
703d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_output_fields; i++) {
704d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream);
70575affc3bSjeremylt     CeedChk(ierr);
70675affc3bSjeremylt   }
707e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
70875affc3bSjeremylt }
70975affc3bSjeremylt 
71075affc3bSjeremylt /**
711b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
712b11c1e72Sjeremylt 
713b11c1e72Sjeremylt   @param qf      CeedQFunction
714b11c1e72Sjeremylt   @param Q       Number of quadrature points
71534138859Sjeremylt   @param[in] u   Array of input CeedVectors
71634138859Sjeremylt   @param[out] v  Array of output CeedVectors
717b11c1e72Sjeremylt 
718b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
719dfdf5a53Sjeremylt 
7207a982d89SJeremy L. Thompson   @ref User
721b11c1e72Sjeremylt **/
722d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
723aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
724d7b241e6Sjeremylt   int ierr;
725d7b241e6Sjeremylt   if (!qf->Apply)
726c042f62fSJeremy L Thompson     // LCOV_EXCL_START
727e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED,
728e15f9bd0SJeremy L Thompson                      "Backend does not support QFunctionApply");
729c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
730d1d35e2fSjeremylt   if (Q % qf->vec_length)
731c042f62fSJeremy L Thompson     // LCOV_EXCL_START
732e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
733e15f9bd0SJeremy L Thompson                      "Number of quadrature points %d must be a "
734d1d35e2fSjeremylt                      "multiple of %d", Q, qf->vec_length);
735c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
736d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
737e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
738d7b241e6Sjeremylt }
739d7b241e6Sjeremylt 
740b11c1e72Sjeremylt /**
741b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
742b11c1e72Sjeremylt 
743b11c1e72Sjeremylt   @param qf  CeedQFunction to destroy
744b11c1e72Sjeremylt 
745b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
746dfdf5a53Sjeremylt 
7477a982d89SJeremy L. Thompson   @ref User
748b11c1e72Sjeremylt **/
749d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
750d7b241e6Sjeremylt   int ierr;
751d7b241e6Sjeremylt 
752d1d35e2fSjeremylt   if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS;
753fe2413ffSjeremylt   // Backend destroy
754d7b241e6Sjeremylt   if ((*qf)->Destroy) {
755d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
756d7b241e6Sjeremylt   }
757fe2413ffSjeremylt   // Free fields
758d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_input_fields; i++) {
759d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr);
760d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr);
761fe2413ffSjeremylt   }
762d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_output_fields; i++) {
763d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr);
764d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr);
765fe2413ffSjeremylt   }
766d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr);
767d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr);
768777ff853SJeremy L Thompson 
769777ff853SJeremy L Thompson   // User context data object
770777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
771fe2413ffSjeremylt 
772d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr);
773d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->qf_name); CeedChk(ierr);
774d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
775d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
776e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
777d7b241e6Sjeremylt }
778d7b241e6Sjeremylt 
779d7b241e6Sjeremylt /// @}
780