xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision f7e22aca11115b09245aceab63a42f8c78198ce8)
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>
193d3250a0SJeremy L Thompson #include <ceed/jit-tools.h>
203d576824SJeremy L Thompson #include <ceed-impl.h>
21288c0443SJeremy L Thompson #include <limits.h>
223d576824SJeremy L Thompson #include <stdbool.h>
233d576824SJeremy L Thompson #include <stdio.h>
2443e1b16fSJeremy L Thompson #include <stdlib.h>
253d576824SJeremy L Thompson #include <string.h>
26288c0443SJeremy L Thompson 
277a982d89SJeremy L. Thompson /// @file
287a982d89SJeremy L. Thompson /// Implementation of public CeedQFunction interfaces
297a982d89SJeremy L. Thompson 
30288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP
31442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none;
32442e7f0bSjeremylt /// @endcond
33442e7f0bSjeremylt 
347a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
357a982d89SJeremy L. Thompson /// @{
367a982d89SJeremy L. Thompson 
377a982d89SJeremy L. Thompson // Indicate that no QFunction is provided by the user
387a982d89SJeremy L. Thompson const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
397a982d89SJeremy L. Thompson 
407a982d89SJeremy L. Thompson /// @}
417a982d89SJeremy L. Thompson 
42442e7f0bSjeremylt /// @cond DOXYGEN_SKIP
43288c0443SJeremy L Thompson static struct {
44288c0443SJeremy L Thompson   char name[CEED_MAX_RESOURCE_LEN];
45288c0443SJeremy L Thompson   char source[CEED_MAX_RESOURCE_LEN];
46d1d35e2fSjeremylt   CeedInt vec_length;
47288c0443SJeremy L Thompson   CeedQFunctionUser f;
48288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
49d1d35e2fSjeremylt } gallery_qfunctions[1024];
50288c0443SJeremy L Thompson static size_t num_qfunctions;
51288c0443SJeremy L Thompson /// @endcond
52d7b241e6Sjeremylt 
53777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
54777ff853SJeremy L Thompson /// CeedQFunction Library Internal Functions
55777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
56777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper
57777ff853SJeremy L Thompson /// @{
58777ff853SJeremy L Thompson 
59b11c1e72Sjeremylt /**
60288c0443SJeremy L Thompson   @brief Register a gallery QFunction
61288c0443SJeremy L Thompson 
62288c0443SJeremy L Thompson   @param name        Name for this backend to respond to
631176cc3aSJeremy L Thompson   @param source      Absolute path to source of QFunction,
641176cc3aSJeremy L Thompson                        "\path\CEED_DIR\gallery\folder\file.h:function_name"
65d1d35e2fSjeremylt   @param vec_length  Vector length.  Caller must ensure that number of quadrature
66d1d35e2fSjeremylt                        points is a multiple of vec_length.
67288c0443SJeremy L Thompson   @param f           Function pointer to evaluate action at quadrature points.
68288c0443SJeremy L Thompson                        See \ref CeedQFunctionUser.
69288c0443SJeremy L Thompson   @param init        Initialization function called by CeedQFunctionInit() when the
70288c0443SJeremy L Thompson                        QFunction is selected.
71288c0443SJeremy L Thompson 
72288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
73288c0443SJeremy L Thompson 
747a982d89SJeremy L. Thompson   @ref Developer
75288c0443SJeremy L Thompson **/
76288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source,
77d1d35e2fSjeremylt                           CeedInt vec_length, CeedQFunctionUser f,
78288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
79d1d35e2fSjeremylt   if (num_qfunctions >= sizeof(gallery_qfunctions) / sizeof(
80d1d35e2fSjeremylt         gallery_qfunctions[0]))
81c042f62fSJeremy L Thompson     // LCOV_EXCL_START
82e15f9bd0SJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
83c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
84c042f62fSJeremy L Thompson 
858ccf1006SJeremy L Thompson   CeedDebugEnv("Gallery Register: %s", name);
868ccf1006SJeremy L Thompson 
87d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
88d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
89d1d35e2fSjeremylt   strncpy(gallery_qfunctions[num_qfunctions].source, source,
90d1d35e2fSjeremylt           CEED_MAX_RESOURCE_LEN);
91d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
92d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].vec_length = vec_length;
93d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].f = f;
94d1d35e2fSjeremylt   gallery_qfunctions[num_qfunctions].init = init;
95288c0443SJeremy L Thompson   num_qfunctions++;
96e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
97288c0443SJeremy L Thompson }
98288c0443SJeremy L Thompson 
99288c0443SJeremy L Thompson /**
1007a982d89SJeremy L. Thompson   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
1017a982d89SJeremy L. Thompson 
1027a982d89SJeremy L. Thompson   @param f           CeedQFunctionField
103d1d35e2fSjeremylt   @param field_name  Name of QFunction field
104d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
105d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT
106d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
1077a982d89SJeremy L. Thompson                        \ref CEED_EVAL_INTERP to use interpolated values,
1087a982d89SJeremy L. Thompson                        \ref CEED_EVAL_GRAD to use gradients,
1097a982d89SJeremy L. Thompson                        \ref CEED_EVAL_WEIGHT to use quadrature weights.
1107a982d89SJeremy L. Thompson 
1117a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1127a982d89SJeremy L. Thompson 
1137a982d89SJeremy L. Thompson   @ref Developer
1147a982d89SJeremy L. Thompson **/
115d1d35e2fSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name,
116d1d35e2fSjeremylt                                  CeedInt size, CeedEvalMode eval_mode) {
117cdf32b93SJeremy L Thompson   int ierr;
1187a982d89SJeremy L. Thompson 
119e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, f); CeedChk(ierr);
120*f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(field_name, (char **)&(*f)->field_name);
121*f7e22acaSJeremy L Thompson   CeedChk(ierr);
1227a982d89SJeremy L. Thompson   (*f)->size = size;
123d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
124e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1257a982d89SJeremy L. Thompson }
1267a982d89SJeremy L. Thompson 
1277a982d89SJeremy L. Thompson /**
1287a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1297a982d89SJeremy L. Thompson 
1307a982d89SJeremy L. Thompson   @param[in] field         QFunction field to view
131d1d35e2fSjeremylt   @param[in] field_number  Number of field being viewed
1324c4400c7SValeria Barra   @param[in] in            true for input field, false for output
1337a982d89SJeremy L. Thompson   @param[in] stream        Stream to view to, e.g., stdout
1347a982d89SJeremy L. Thompson 
1357a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1367a982d89SJeremy L. Thompson 
1377a982d89SJeremy L. Thompson   @ref Utility
1387a982d89SJeremy L. Thompson **/
139d1d35e2fSjeremylt static int CeedQFunctionFieldView(CeedQFunctionField field,
140d1d35e2fSjeremylt                                   CeedInt field_number,
1417a982d89SJeremy L. Thompson                                   bool in, FILE *stream) {
1428229195eSjeremylt   int ierr;
1437a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1448229195eSjeremylt   char *field_name;
1458229195eSjeremylt   ierr = CeedQFunctionFieldGetName(field, &field_name); CeedChk(ierr);
1468229195eSjeremylt   CeedInt size;
1478229195eSjeremylt   ierr = CeedQFunctionFieldGetSize(field, &size); CeedChk(ierr);
1488229195eSjeremylt   CeedEvalMode eval_mode;
1498229195eSjeremylt   ierr = CeedQFunctionFieldGetEvalMode(field, &eval_mode); CeedChk(ierr);
1507a982d89SJeremy L. Thompson   fprintf(stream, "    %s Field [%d]:\n"
1517a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1527a982d89SJeremy L. Thompson           "      Size: %d\n"
1537a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1548229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
155e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1567a982d89SJeremy L. Thompson }
1577a982d89SJeremy L. Thompson 
158777ff853SJeremy L Thompson /**
159777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
160777ff853SJeremy L Thompson 
161777ff853SJeremy L Thompson   @param qf      CeedQFunction
162777ff853SJeremy L Thompson   @param status  Boolean value to set as Fortran status
163777ff853SJeremy L Thompson 
164777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
165777ff853SJeremy L Thompson 
166777ff853SJeremy L Thompson   @ref Backend
167777ff853SJeremy L Thompson **/
168777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
169f04ea552SJeremy L Thompson   qf->is_fortran = status;
170e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
171777ff853SJeremy L Thompson }
172777ff853SJeremy L Thompson 
1737a982d89SJeremy L. Thompson /// @}
1747a982d89SJeremy L. Thompson 
1757a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1767a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1777a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1787a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1797a982d89SJeremy L. Thompson /// @{
1807a982d89SJeremy L. Thompson 
1817a982d89SJeremy L. Thompson /**
1827a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1837a982d89SJeremy L. Thompson 
1847a982d89SJeremy L. Thompson   @param qf               CeedQFunction
185d1d35e2fSjeremylt   @param[out] vec_length  Variable to store vector length
1867a982d89SJeremy L. Thompson 
1877a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1887a982d89SJeremy L. Thompson 
1897a982d89SJeremy L. Thompson   @ref Backend
1907a982d89SJeremy L. Thompson **/
191d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
192d1d35e2fSjeremylt   *vec_length = qf->vec_length;
193e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1947a982d89SJeremy L. Thompson }
1957a982d89SJeremy L. Thompson 
1967a982d89SJeremy L. Thompson /**
1977a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
1987a982d89SJeremy L. Thompson 
1997a982d89SJeremy L. Thompson   @param qf               CeedQFunction
200d1d35e2fSjeremylt   @param[out] num_input   Variable to store number of input fields
201d1d35e2fSjeremylt   @param[out] num_output  Variable to store number of output fields
2027a982d89SJeremy L. Thompson 
2037a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2047a982d89SJeremy L. Thompson 
2057a982d89SJeremy L. Thompson   @ref Backend
2067a982d89SJeremy L. Thompson **/
207d1d35e2fSjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input,
208d1d35e2fSjeremylt                             CeedInt *num_output) {
209d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
210d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
211e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2127a982d89SJeremy L. Thompson }
2137a982d89SJeremy L. Thompson 
2147a982d89SJeremy L. Thompson /**
21543e1b16fSJeremy L Thompson   @brief Get the name of the user function for a CeedQFunction
2167a982d89SJeremy L. Thompson 
2177a982d89SJeremy L. Thompson   @param qf                CeedQFunction
21843e1b16fSJeremy L Thompson   @param[out] kernel_name  Variable to store source path string
2197a982d89SJeremy L. Thompson 
2207a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2217a982d89SJeremy L. Thompson 
2227a982d89SJeremy L. Thompson   @ref Backend
2237a982d89SJeremy L. Thompson **/
22443e1b16fSJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, char **kernel_name) {
22543e1b16fSJeremy L Thompson   *kernel_name = (char *) qf->kernel_name;
22643e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
22743e1b16fSJeremy L Thompson }
22843e1b16fSJeremy L Thompson 
22943e1b16fSJeremy L Thompson /**
23043e1b16fSJeremy L Thompson   @brief Get the source path string for a CeedQFunction
23143e1b16fSJeremy L Thompson 
23243e1b16fSJeremy L Thompson   @param qf                CeedQFunction
23343e1b16fSJeremy L Thompson   @param[out] source_path  Variable to store source path string
23443e1b16fSJeremy L Thompson 
23543e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
23643e1b16fSJeremy L Thompson 
23743e1b16fSJeremy L Thompson   @ref Backend
23843e1b16fSJeremy L Thompson **/
23943e1b16fSJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) {
24043e1b16fSJeremy L Thompson   *source_path = (char *) qf->source_path;
241e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2427a982d89SJeremy L. Thompson }
2437a982d89SJeremy L. Thompson 
2447a982d89SJeremy L. Thompson /**
2453d3250a0SJeremy L Thompson   @brief Initalize and load QFunction source file into string buffer, including
2463d3250a0SJeremy L Thompson            full text of local files in place of `#include "local.h"`.
2473d3250a0SJeremy L Thompson            The `buffer` is set to `NULL` if there is no QFunction source file.
2483d3250a0SJeremy L Thompson          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
2493d3250a0SJeremy L Thompson 
2503d3250a0SJeremy L Thompson   @param qf                  CeedQFunction
251f74ec584SJeremy L Thompson   @param[out] source_buffer  String buffer for source file contents
2523d3250a0SJeremy L Thompson 
2533d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2543d3250a0SJeremy L Thompson 
2553d3250a0SJeremy L Thompson   @ref Backend
2563d3250a0SJeremy L Thompson **/
2573d3250a0SJeremy L Thompson int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, char **source_buffer) {
2583d3250a0SJeremy L Thompson   int ierr;
2593d3250a0SJeremy L Thompson   char *source_path;
2603d3250a0SJeremy L Thompson 
2613d3250a0SJeremy L Thompson   ierr = CeedQFunctionGetSourcePath(qf, &source_path); CeedChk(ierr);
2623d3250a0SJeremy L Thompson   *source_buffer = NULL;
2633d3250a0SJeremy L Thompson   if (source_path) {
2643d3250a0SJeremy L Thompson     ierr = CeedLoadSourceToBuffer(qf->ceed, source_path, source_buffer);
2653d3250a0SJeremy L Thompson     CeedChk(ierr);
2663d3250a0SJeremy L Thompson   }
2673d3250a0SJeremy L Thompson 
2683d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2693d3250a0SJeremy L Thompson }
2703d3250a0SJeremy L Thompson 
2713d3250a0SJeremy L Thompson /**
2727a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2737a982d89SJeremy L. Thompson 
2747a982d89SJeremy L. Thompson   @param qf      CeedQFunction
2757a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
2767a982d89SJeremy L. Thompson 
2777a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2787a982d89SJeremy L. Thompson 
2797a982d89SJeremy L. Thompson   @ref Backend
2807a982d89SJeremy L. Thompson **/
2817a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
2827a982d89SJeremy L. Thompson   *f = qf->function;
283e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2847a982d89SJeremy L. Thompson }
2857a982d89SJeremy L. Thompson 
2867a982d89SJeremy L. Thompson /**
287777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
288777ff853SJeremy L Thompson            Note: For QFunctions from the Fortran interface, this
289777ff853SJeremy L Thompson              function will return the Fortran context
290777ff853SJeremy L Thompson              CeedQFunctionContext.
2917a982d89SJeremy L. Thompson 
2927a982d89SJeremy L. Thompson   @param qf        CeedQFunction
293777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2947a982d89SJeremy L. Thompson 
2957a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2967a982d89SJeremy L. Thompson 
2977a982d89SJeremy L. Thompson   @ref Backend
2987a982d89SJeremy L. Thompson **/
299777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
3007a982d89SJeremy L. Thompson   *ctx = qf->ctx;
301e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3027a982d89SJeremy L. Thompson }
3037a982d89SJeremy L. Thompson 
3047a982d89SJeremy L. Thompson /**
3057a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
306777ff853SJeremy L Thompson            Note: For all QFunctions this function will return the user
307777ff853SJeremy L Thompson              CeedQFunctionContext and not interface context
308777ff853SJeremy L Thompson              CeedQFunctionContext, if any such object exists.
3097a982d89SJeremy L. Thompson 
3107a982d89SJeremy L. Thompson   @param qf        CeedQFunction
311777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
3127a982d89SJeremy L. Thompson 
3137a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3147a982d89SJeremy L. Thompson   @ref Backend
3157a982d89SJeremy L. Thompson **/
316777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
317777ff853SJeremy L Thompson   int ierr;
318f04ea552SJeremy L Thompson   if (qf->is_fortran) {
319d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
320d1d35e2fSjeremylt     ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx);
321777ff853SJeremy L Thompson     CeedChk(ierr);
3228cb0412aSJeremy L Thompson     *ctx = fortran_ctx->inner_ctx;
323d1d35e2fSjeremylt     ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx);
324d1d35e2fSjeremylt     CeedChk(ierr);
3257a982d89SJeremy L. Thompson   } else {
3267a982d89SJeremy L. Thompson     *ctx = qf->ctx;
3277a982d89SJeremy L. Thompson   }
328e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3297a982d89SJeremy L. Thompson }
3307a982d89SJeremy L. Thompson 
3317a982d89SJeremy L. Thompson /**
3327a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
3337a982d89SJeremy L. Thompson 
3347a982d89SJeremy L. Thompson   @param qf                CeedQFunction
335d1d35e2fSjeremylt   @param[out] is_identity  Variable to store identity status
3367a982d89SJeremy L. Thompson 
3377a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3387a982d89SJeremy L. Thompson 
3397a982d89SJeremy L. Thompson   @ref Backend
3407a982d89SJeremy L. Thompson **/
341d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
342f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
343e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3447a982d89SJeremy L. Thompson }
3457a982d89SJeremy L. Thompson 
3467a982d89SJeremy L. Thompson /**
3477a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
3487a982d89SJeremy L. Thompson 
3497a982d89SJeremy L. Thompson   @param qf         CeedQFunction
3507a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
3517a982d89SJeremy L. Thompson 
3527a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3537a982d89SJeremy L. Thompson 
3547a982d89SJeremy L. Thompson   @ref Backend
3557a982d89SJeremy L. Thompson **/
356777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
357777ff853SJeremy L Thompson   *(void **)data = qf->data;
358e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3597a982d89SJeremy L. Thompson }
3607a982d89SJeremy L. Thompson 
3617a982d89SJeremy L. Thompson /**
3627a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
3637a982d89SJeremy L. Thompson 
3647a982d89SJeremy L. Thompson   @param[out] qf  CeedQFunction
3657a982d89SJeremy L. Thompson   @param data     Data to set
3667a982d89SJeremy L. Thompson 
3677a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3687a982d89SJeremy L. Thompson 
3697a982d89SJeremy L. Thompson   @ref Backend
3707a982d89SJeremy L. Thompson **/
371777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
372777ff853SJeremy L Thompson   qf->data = data;
373e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3747a982d89SJeremy L. Thompson }
3757a982d89SJeremy L. Thompson 
3767a982d89SJeremy L. Thompson /**
37734359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
37834359f16Sjeremylt 
37934359f16Sjeremylt   @param qf  CeedQFunction to increment the reference counter
38034359f16Sjeremylt 
38134359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
38234359f16Sjeremylt 
38334359f16Sjeremylt   @ref Backend
38434359f16Sjeremylt **/
3859560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
38634359f16Sjeremylt   qf->ref_count++;
38734359f16Sjeremylt   return CEED_ERROR_SUCCESS;
38834359f16Sjeremylt }
38934359f16Sjeremylt 
3907a982d89SJeremy L. Thompson /// @}
3917a982d89SJeremy L. Thompson 
3927a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3937a982d89SJeremy L. Thompson /// CeedQFunction Public API
3947a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3957a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
3967a982d89SJeremy L. Thompson /// @{
3977a982d89SJeremy L. Thompson 
3987a982d89SJeremy L. Thompson /**
3997a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
4007a982d89SJeremy L. Thompson 
4017a982d89SJeremy L. Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
402d1d35e2fSjeremylt   @param vec_length  Vector length. Caller must ensure that number of quadrature
403d1d35e2fSjeremylt                        points is a multiple of vec_length.
4047a982d89SJeremy L. Thompson   @param f           Function pointer to evaluate action at quadrature points.
4057a982d89SJeremy L. Thompson                        See \ref CeedQFunctionUser.
4067a982d89SJeremy L. Thompson   @param source      Absolute path to source of QFunction,
407c8d5b03cSJeremy L Thompson                        "\abs_path\file.h:function_name".
408c8d5b03cSJeremy L Thompson                        For support across all backends, this source must only
409c8d5b03cSJeremy L Thompson                        contain constructs supported by C99, C++11, and CUDA.
4107a982d89SJeremy L. Thompson   @param[out] qf     Address of the variable where the newly created
4117a982d89SJeremy L. Thompson                        CeedQFunction will be stored
4127a982d89SJeremy L. Thompson 
4137a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4147a982d89SJeremy L. Thompson 
4157a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
4167a982d89SJeremy L. Thompson     arguments.
4177a982d89SJeremy L. Thompson 
4187a982d89SJeremy L. Thompson   @ref User
4197a982d89SJeremy L. Thompson **/
420d1d35e2fSjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length,
421d1d35e2fSjeremylt                                 CeedQFunctionUser f,
4227a982d89SJeremy L. Thompson                                 const char *source, CeedQFunction *qf) {
4237a982d89SJeremy L. Thompson   int ierr;
42443e1b16fSJeremy L Thompson   char *source_copy, *kernel_name_copy;
4257a982d89SJeremy L. Thompson 
4267a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
4277a982d89SJeremy L. Thompson     Ceed delegate;
4287a982d89SJeremy L. Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
4297a982d89SJeremy L. Thompson 
4307a982d89SJeremy L. Thompson     if (!delegate)
4317a982d89SJeremy L. Thompson       // LCOV_EXCL_START
432e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
433e15f9bd0SJeremy L Thompson                        "Backend does not support QFunctionCreate");
4347a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
4357a982d89SJeremy L. Thompson 
436d1d35e2fSjeremylt     ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf);
4377a982d89SJeremy L. Thompson     CeedChk(ierr);
438e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
4397a982d89SJeremy L. Thompson   }
4407a982d89SJeremy L. Thompson 
44143e1b16fSJeremy L Thompson   if (strlen(source) && !strrchr(source, ':'))\
44243e1b16fSJeremy L Thompson     // LCOV_EXCL_START
44343e1b16fSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPLETE,
44443e1b16fSJeremy L Thompson                      "Provided path to source does not include function name. "
44543e1b16fSJeremy L Thompson                      "Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"",
44643e1b16fSJeremy L Thompson                      source);
44743e1b16fSJeremy L Thompson   // LCOV_EXCL_STOP
44843e1b16fSJeremy L Thompson 
4497a982d89SJeremy L. Thompson   ierr = CeedCalloc(1, qf); CeedChk(ierr);
4507a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
4519560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
452d1d35e2fSjeremylt   (*qf)->ref_count = 1;
453d1d35e2fSjeremylt   (*qf)->vec_length = vec_length;
454f04ea552SJeremy L Thompson   (*qf)->is_identity = false;
4557a982d89SJeremy L. Thompson   (*qf)->function = f;
45643e1b16fSJeremy L Thompson   if (strlen(source)) {
45743e1b16fSJeremy L Thompson     const char *kernel_name = strrchr(source, ':') + 1;
45843e1b16fSJeremy L Thompson     size_t kernel_name_len = strlen(kernel_name);
45943e1b16fSJeremy L Thompson     ierr = CeedCalloc(kernel_name_len + 1, &kernel_name_copy); CeedChk(ierr);
46043e1b16fSJeremy L Thompson     strncpy(kernel_name_copy, kernel_name, kernel_name_len);
46143e1b16fSJeremy L Thompson     (*qf)->kernel_name = kernel_name_copy;
46243e1b16fSJeremy L Thompson 
46343e1b16fSJeremy L Thompson     size_t source_len = strlen(source) - kernel_name_len - 1;
46443e1b16fSJeremy L Thompson     ierr = CeedCalloc(source_len + 1, &source_copy); CeedChk(ierr);
46543e1b16fSJeremy L Thompson     strncpy(source_copy, source, source_len);
466d1d35e2fSjeremylt     (*qf)->source_path = source_copy;
46743e1b16fSJeremy L Thompson   }
468bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields); CeedChk(ierr);
469bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields); CeedChk(ierr);
4707a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
471e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4727a982d89SJeremy L. Thompson }
4737a982d89SJeremy L. Thompson 
4747a982d89SJeremy L. Thompson /**
475288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
476288c0443SJeremy L Thompson 
477288c0443SJeremy L Thompson   @param ceed     A Ceed object where the CeedQFunction will be created
478288c0443SJeremy L Thompson   @param name     Name of QFunction to use from gallery
479288c0443SJeremy L Thompson   @param[out] qf  Address of the variable where the newly created
480288c0443SJeremy L Thompson                     CeedQFunction will be stored
481288c0443SJeremy L Thompson 
482288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
483288c0443SJeremy L Thompson 
4847a982d89SJeremy L. Thompson   @ref User
485288c0443SJeremy L Thompson **/
486288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
487288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
488288c0443SJeremy L Thompson   int ierr;
489*f7e22acaSJeremy L Thompson   size_t match_len = 0, match_index = UINT_MAX;
490288c0443SJeremy L Thompson 
4911d013790SJed Brown   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
492288c0443SJeremy L Thompson   // Find matching backend
493e15f9bd0SJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE,
494e15f9bd0SJeremy L Thompson                                 "No QFunction name provided");
495288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
496288c0443SJeremy L Thompson     size_t n;
497d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
498d1d35e2fSjeremylt     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {}
499d1d35e2fSjeremylt     if (n > match_len) {
500d1d35e2fSjeremylt       match_len = n;
501*f7e22acaSJeremy L Thompson       match_index = i;
502288c0443SJeremy L Thompson     }
503288c0443SJeremy L Thompson   }
504d1d35e2fSjeremylt   if (!match_len)
505138d4072Sjeremylt     // LCOV_EXCL_START
506e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
507138d4072Sjeremylt   // LCOV_EXCL_STOP
508288c0443SJeremy L Thompson 
509288c0443SJeremy L Thompson   // Create QFunction
510d1d35e2fSjeremylt   ierr = CeedQFunctionCreateInterior(ceed,
511*f7e22acaSJeremy L Thompson                                      gallery_qfunctions[match_index].vec_length,
512*f7e22acaSJeremy L Thompson                                      gallery_qfunctions[match_index].f,
513*f7e22acaSJeremy L Thompson                                      gallery_qfunctions[match_index].source, qf);
514288c0443SJeremy L Thompson   CeedChk(ierr);
515288c0443SJeremy L Thompson 
516288c0443SJeremy L Thompson   // QFunction specific setup
517*f7e22acaSJeremy L Thompson   ierr = gallery_qfunctions[match_index].init(ceed, name, *qf); CeedChk(ierr);
518288c0443SJeremy L Thompson 
51975affc3bSjeremylt   // Copy name
520*f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name); CeedChk(ierr);
52143e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
522e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
523288c0443SJeremy L Thompson }
524288c0443SJeremy L Thompson 
525288c0443SJeremy L Thompson /**
5260219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
5270219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
5280219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
5290219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
5300219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
5310219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
5320219ea01SJeremy L Thompson            location for both.
5330219ea01SJeremy L Thompson 
5340219ea01SJeremy L Thompson   @param ceed          A Ceed object where the CeedQFunction will be created
535d1d35e2fSjeremylt   @param[in] size      Size of the QFunction fields
536d1d35e2fSjeremylt   @param[in] in_mode   CeedEvalMode for input to CeedQFunction
537d1d35e2fSjeremylt   @param[in] out_mode  CeedEvalMode for output to CeedQFunction
5380219ea01SJeremy L Thompson   @param[out] qf       Address of the variable where the newly created
5390219ea01SJeremy L Thompson                          CeedQFunction will be stored
5400219ea01SJeremy L Thompson 
5410219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5420219ea01SJeremy L Thompson 
5437a982d89SJeremy L. Thompson   @ref User
5440219ea01SJeremy L Thompson **/
545d1d35e2fSjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode,
546d1d35e2fSjeremylt                                 CeedEvalMode out_mode, CeedQFunction *qf) {
5470219ea01SJeremy L Thompson   int ierr;
5480219ea01SJeremy L Thompson 
5490219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
550d1d35e2fSjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr);
551d1d35e2fSjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr);
5520219ea01SJeremy L Thompson 
553f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
554547dbd6fSJeremy L Thompson 
555777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
556547dbd6fSJeremy L Thompson   ierr = CeedQFunctionGetContext(*qf, &ctx); CeedChk(ierr);
557547dbd6fSJeremy L Thompson   ierr = CeedQFunctionContextSetInt32(ctx, "size", size); CeedChk(ierr);
558547dbd6fSJeremy L Thompson 
559e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5600219ea01SJeremy L Thompson }
5610219ea01SJeremy L Thompson 
5620219ea01SJeremy L Thompson /**
5639560d06aSjeremylt   @brief Copy the pointer to a CeedQFunction. Both pointers should
5649560d06aSjeremylt            be destroyed with `CeedQFunctionDestroy()`;
5659560d06aSjeremylt            Note: If `*qf_copy` is non-NULL, then it is assumed that
5669560d06aSjeremylt            `*qf_copy` is a pointer to a CeedQFunction. This
5679560d06aSjeremylt            CeedQFunction will be destroyed if `*qf_copy` is the only
5689560d06aSjeremylt            reference to this CeedQFunction.
5699560d06aSjeremylt 
5709560d06aSjeremylt   @param qf            CeedQFunction to copy reference to
5719560d06aSjeremylt   @param[out] qf_copy  Variable to store copied reference
5729560d06aSjeremylt 
5739560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
5749560d06aSjeremylt 
5759560d06aSjeremylt   @ref User
5769560d06aSjeremylt **/
5779560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
5789560d06aSjeremylt   int ierr;
5799560d06aSjeremylt 
5809560d06aSjeremylt   ierr = CeedQFunctionReference(qf); CeedChk(ierr);
5819560d06aSjeremylt   ierr = CeedQFunctionDestroy(qf_copy); CeedChk(ierr);
5829560d06aSjeremylt   *qf_copy = qf;
5839560d06aSjeremylt   return CEED_ERROR_SUCCESS;
5849560d06aSjeremylt }
5859560d06aSjeremylt 
5869560d06aSjeremylt /**
587a0a97fcfSJed Brown   @brief Add a CeedQFunction input
588b11c1e72Sjeremylt 
589b11c1e72Sjeremylt   @param qf          CeedQFunction
590d1d35e2fSjeremylt   @param field_name  Name of QFunction field
591d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
592d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
593d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
594b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
595b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
596b11c1e72Sjeremylt 
597b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
598dfdf5a53Sjeremylt 
5997a982d89SJeremy L. Thompson   @ref User
600b11c1e72Sjeremylt **/
601d1d35e2fSjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name,
602d1d35e2fSjeremylt                           CeedInt size,
603d1d35e2fSjeremylt                           CeedEvalMode eval_mode) {
604f04ea552SJeremy L Thompson   if (qf->is_immutable)
605e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
606e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
607f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
608e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
609d1d35e2fSjeremylt   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1))
610e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
611e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
612e15f9bd0SJeremy L Thompson                      "CEED_EVAL_WEIGHT should have size 1");
613e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
614d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields],
615d1d35e2fSjeremylt                                    field_name, size, eval_mode);
616fe2413ffSjeremylt   CeedChk(ierr);
617d1d35e2fSjeremylt   qf->num_input_fields++;
618e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
619d7b241e6Sjeremylt }
620d7b241e6Sjeremylt 
621b11c1e72Sjeremylt /**
622a0a97fcfSJed Brown   @brief Add a CeedQFunction output
623b11c1e72Sjeremylt 
624b11c1e72Sjeremylt   @param qf          CeedQFunction
625d1d35e2fSjeremylt   @param field_name  Name of QFunction field
626d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
627d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
628d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
629b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
630b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
631b11c1e72Sjeremylt 
632b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
633dfdf5a53Sjeremylt 
6347a982d89SJeremy L. Thompson   @ref User
635b11c1e72Sjeremylt **/
636d1d35e2fSjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name,
637d1d35e2fSjeremylt                            CeedInt size, CeedEvalMode eval_mode) {
638f04ea552SJeremy L Thompson   if (qf->is_immutable)
639e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
640e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
641f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
642e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
643d1d35e2fSjeremylt   if (eval_mode == CEED_EVAL_WEIGHT)
644c042f62fSJeremy L Thompson     // LCOV_EXCL_START
645e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
646e15f9bd0SJeremy L Thompson                      "Cannot create QFunction output with "
6471d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
648c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
649d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields],
650d1d35e2fSjeremylt                                    field_name, size, eval_mode);
651fe2413ffSjeremylt   CeedChk(ierr);
652d1d35e2fSjeremylt   qf->num_output_fields++;
653e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
654d7b241e6Sjeremylt }
655d7b241e6Sjeremylt 
656dfdf5a53Sjeremylt /**
65743bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
65843bbe138SJeremy L Thompson 
659f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
660f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
661f04ea552SJeremy L Thompson 
66243bbe138SJeremy L Thompson   @param qf                      CeedQFunction
663f74ec584SJeremy L Thompson   @param[out] num_input_fields   Variable to store number of input fields
664f74ec584SJeremy L Thompson   @param[out] input_fields       Variable to store input fields
665f74ec584SJeremy L Thompson   @param[out] num_output_fields  Variable to store number of output fields
666f74ec584SJeremy L Thompson   @param[out] output_fields      Variable to store output fields
66743bbe138SJeremy L Thompson 
66843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
66943bbe138SJeremy L Thompson 
670e9b533fbSJeremy L Thompson   @ref Advanced
67143bbe138SJeremy L Thompson **/
67243bbe138SJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields,
67343bbe138SJeremy L Thompson                            CeedQFunctionField **input_fields,
67443bbe138SJeremy L Thompson                            CeedInt *num_output_fields,
67543bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
676f04ea552SJeremy L Thompson   qf->is_immutable = true;
67743bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
67843bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
67943bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
68043bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
68143bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
68243bbe138SJeremy L Thompson }
68343bbe138SJeremy L Thompson 
68443bbe138SJeremy L Thompson /**
68543bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
68643bbe138SJeremy L Thompson 
68743bbe138SJeremy L Thompson   @param qf_field         CeedQFunctionField
68843bbe138SJeremy L Thompson   @param[out] field_name  Variable to store the field name
68943bbe138SJeremy L Thompson 
69043bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
69143bbe138SJeremy L Thompson 
692e9b533fbSJeremy L Thompson   @ref Advanced
69343bbe138SJeremy L Thompson **/
69443bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
69543bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
69643bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
69743bbe138SJeremy L Thompson }
69843bbe138SJeremy L Thompson 
69943bbe138SJeremy L Thompson /**
70043bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
70143bbe138SJeremy L Thompson 
70243bbe138SJeremy L Thompson   @param qf_field   CeedQFunctionField
70343bbe138SJeremy L Thompson   @param[out] size  Variable to store the size of the field
70443bbe138SJeremy L Thompson 
70543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
70643bbe138SJeremy L Thompson 
707e9b533fbSJeremy L Thompson   @ref Advanced
70843bbe138SJeremy L Thompson **/
70943bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
71043bbe138SJeremy L Thompson   *size = qf_field->size;
71143bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
71243bbe138SJeremy L Thompson }
71343bbe138SJeremy L Thompson 
71443bbe138SJeremy L Thompson /**
71543bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
71643bbe138SJeremy L Thompson 
71743bbe138SJeremy L Thompson   @param qf_field        CeedQFunctionField
71843bbe138SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
71943bbe138SJeremy L Thompson 
72043bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
72143bbe138SJeremy L Thompson 
722e9b533fbSJeremy L Thompson   @ref Advanced
72343bbe138SJeremy L Thompson **/
72443bbe138SJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field,
72543bbe138SJeremy L Thompson                                   CeedEvalMode *eval_mode) {
72643bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
72743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
72843bbe138SJeremy L Thompson }
72943bbe138SJeremy L Thompson 
73043bbe138SJeremy L Thompson /**
7314ce2993fSjeremylt   @brief Set global context for a CeedQFunction
732b11c1e72Sjeremylt 
733b11c1e72Sjeremylt   @param qf   CeedQFunction
734b11c1e72Sjeremylt   @param ctx  Context data to set
735b11c1e72Sjeremylt 
736b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
737dfdf5a53Sjeremylt 
7387a982d89SJeremy L. Thompson   @ref User
739b11c1e72Sjeremylt **/
740777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
74134359f16Sjeremylt   int ierr;
742d7b241e6Sjeremylt   qf->ctx = ctx;
7439560d06aSjeremylt   ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr);
744e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
745d7b241e6Sjeremylt }
746d7b241e6Sjeremylt 
747b11c1e72Sjeremylt /**
74875affc3bSjeremylt   @brief View a CeedQFunction
74975affc3bSjeremylt 
75075affc3bSjeremylt   @param[in] qf      CeedQFunction to view
75175affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
75275affc3bSjeremylt 
75375affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
75475affc3bSjeremylt 
7557a982d89SJeremy L. Thompson   @ref User
75675affc3bSjeremylt **/
75775affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
75875affc3bSjeremylt   int ierr;
75975affc3bSjeremylt 
76075affc3bSjeremylt   fprintf(stream, "%sCeedQFunction %s\n",
76143e1b16fSJeremy L Thompson           qf->is_gallery ? "Gallery " : "User ",
76243e1b16fSJeremy L Thompson           qf->is_gallery ? qf->gallery_name : qf->kernel_name);
76375affc3bSjeremylt 
764d1d35e2fSjeremylt   fprintf(stream, "  %d Input Field%s:\n", qf->num_input_fields,
765d1d35e2fSjeremylt           qf->num_input_fields>1 ? "s" : "");
766d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_input_fields; i++) {
767d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream);
7682da88da5Sjeremylt     CeedChk(ierr);
76975affc3bSjeremylt   }
77075affc3bSjeremylt 
771d1d35e2fSjeremylt   fprintf(stream, "  %d Output Field%s:\n", qf->num_output_fields,
772d1d35e2fSjeremylt           qf->num_output_fields>1 ? "s" : "");
773d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_output_fields; i++) {
774d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream);
77575affc3bSjeremylt     CeedChk(ierr);
77675affc3bSjeremylt   }
777e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
77875affc3bSjeremylt }
77975affc3bSjeremylt 
78075affc3bSjeremylt /**
781b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunction
782b7c9bbdaSJeremy L Thompson 
783b7c9bbdaSJeremy L Thompson   @param qf              CeedQFunction
784b7c9bbdaSJeremy L Thompson   @param[out] ceed       Variable to store Ceed
785b7c9bbdaSJeremy L Thompson 
786b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
787b7c9bbdaSJeremy L Thompson 
788b7c9bbdaSJeremy L Thompson   @ref Advanced
789b7c9bbdaSJeremy L Thompson **/
790b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
791b7c9bbdaSJeremy L Thompson   *ceed = qf->ceed;
792b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
793b7c9bbdaSJeremy L Thompson }
794b7c9bbdaSJeremy L Thompson 
795b7c9bbdaSJeremy L Thompson /**
796b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
797b11c1e72Sjeremylt 
798f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
799f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
800f04ea552SJeremy L Thompson 
801b11c1e72Sjeremylt   @param qf      CeedQFunction
802b11c1e72Sjeremylt   @param Q       Number of quadrature points
80334138859Sjeremylt   @param[in] u   Array of input CeedVectors
80434138859Sjeremylt   @param[out] v  Array of output CeedVectors
805b11c1e72Sjeremylt 
806b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
807dfdf5a53Sjeremylt 
8087a982d89SJeremy L. Thompson   @ref User
809b11c1e72Sjeremylt **/
810d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
811aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
812d7b241e6Sjeremylt   int ierr;
813d7b241e6Sjeremylt   if (!qf->Apply)
814c042f62fSJeremy L Thompson     // LCOV_EXCL_START
815e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED,
816e15f9bd0SJeremy L Thompson                      "Backend does not support QFunctionApply");
817c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
818d1d35e2fSjeremylt   if (Q % qf->vec_length)
819c042f62fSJeremy L Thompson     // LCOV_EXCL_START
820e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
821e15f9bd0SJeremy L Thompson                      "Number of quadrature points %d must be a "
822d1d35e2fSjeremylt                      "multiple of %d", Q, qf->vec_length);
823c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
824f04ea552SJeremy L Thompson   qf->is_immutable = true;
825d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
826e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
827d7b241e6Sjeremylt }
828d7b241e6Sjeremylt 
829b11c1e72Sjeremylt /**
830b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
831b11c1e72Sjeremylt 
832b11c1e72Sjeremylt   @param qf  CeedQFunction to destroy
833b11c1e72Sjeremylt 
834b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
835dfdf5a53Sjeremylt 
8367a982d89SJeremy L. Thompson   @ref User
837b11c1e72Sjeremylt **/
838d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
839d7b241e6Sjeremylt   int ierr;
840d7b241e6Sjeremylt 
841d1d35e2fSjeremylt   if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS;
842fe2413ffSjeremylt   // Backend destroy
843d7b241e6Sjeremylt   if ((*qf)->Destroy) {
844d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
845d7b241e6Sjeremylt   }
846fe2413ffSjeremylt   // Free fields
847d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_input_fields; i++) {
848d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr);
849d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr);
850fe2413ffSjeremylt   }
851d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_output_fields; i++) {
852d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr);
853d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr);
854fe2413ffSjeremylt   }
855d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr);
856d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr);
857777ff853SJeremy L Thompson 
858777ff853SJeremy L Thompson   // User context data object
859777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
860fe2413ffSjeremylt 
861d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr);
86243e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->gallery_name); CeedChk(ierr);
86343e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->kernel_name); CeedChk(ierr);
864d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
865d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
866e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
867d7b241e6Sjeremylt }
868d7b241e6Sjeremylt 
869d7b241e6Sjeremylt /// @}
870