xref: /libCEED/interface/ceed-qfunction.c (revision 547dbd6f5071ad3a6569362aeaf8c427ad429b62)
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;
118d1d35e2fSjeremylt   size_t len = strlen(field_name);
1197a982d89SJeremy L. Thompson   char *tmp;
1207a982d89SJeremy L. Thompson 
121e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, f); CeedChk(ierr);
1227a982d89SJeremy L. Thompson   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
123d1d35e2fSjeremylt   memcpy(tmp, field_name, len+1);
124d1d35e2fSjeremylt   (*f)->field_name = tmp;
1257a982d89SJeremy L. Thompson   (*f)->size = size;
126d1d35e2fSjeremylt   (*f)->eval_mode = eval_mode;
127e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1287a982d89SJeremy L. Thompson }
1297a982d89SJeremy L. Thompson 
1307a982d89SJeremy L. Thompson /**
1317a982d89SJeremy L. Thompson   @brief View a field of a CeedQFunction
1327a982d89SJeremy L. Thompson 
1337a982d89SJeremy L. Thompson   @param[in] field         QFunction field to view
134d1d35e2fSjeremylt   @param[in] field_number  Number of field being viewed
1354c4400c7SValeria Barra   @param[in] in            true for input field, false for output
1367a982d89SJeremy L. Thompson   @param[in] stream        Stream to view to, e.g., stdout
1377a982d89SJeremy L. Thompson 
1387a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1397a982d89SJeremy L. Thompson 
1407a982d89SJeremy L. Thompson   @ref Utility
1417a982d89SJeremy L. Thompson **/
142d1d35e2fSjeremylt static int CeedQFunctionFieldView(CeedQFunctionField field,
143d1d35e2fSjeremylt                                   CeedInt field_number,
1447a982d89SJeremy L. Thompson                                   bool in, FILE *stream) {
1458229195eSjeremylt   int ierr;
1467a982d89SJeremy L. Thompson   const char *inout = in ? "Input" : "Output";
1478229195eSjeremylt   char *field_name;
1488229195eSjeremylt   ierr = CeedQFunctionFieldGetName(field, &field_name); CeedChk(ierr);
1498229195eSjeremylt   CeedInt size;
1508229195eSjeremylt   ierr = CeedQFunctionFieldGetSize(field, &size); CeedChk(ierr);
1518229195eSjeremylt   CeedEvalMode eval_mode;
1528229195eSjeremylt   ierr = CeedQFunctionFieldGetEvalMode(field, &eval_mode); CeedChk(ierr);
1537a982d89SJeremy L. Thompson   fprintf(stream, "    %s Field [%d]:\n"
1547a982d89SJeremy L. Thompson           "      Name: \"%s\"\n"
1557a982d89SJeremy L. Thompson           "      Size: %d\n"
1567a982d89SJeremy L. Thompson           "      EvalMode: \"%s\"\n",
1578229195eSjeremylt           inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
158e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1597a982d89SJeremy L. Thompson }
1607a982d89SJeremy L. Thompson 
161777ff853SJeremy L Thompson /**
162777ff853SJeremy L Thompson   @brief Set flag to determine if Fortran interface is used
163777ff853SJeremy L Thompson 
164777ff853SJeremy L Thompson   @param qf      CeedQFunction
165777ff853SJeremy L Thompson   @param status  Boolean value to set as Fortran status
166777ff853SJeremy L Thompson 
167777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
168777ff853SJeremy L Thompson 
169777ff853SJeremy L Thompson   @ref Backend
170777ff853SJeremy L Thompson **/
171777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
172f04ea552SJeremy L Thompson   qf->is_fortran = status;
173e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
174777ff853SJeremy L Thompson }
175777ff853SJeremy L Thompson 
1767a982d89SJeremy L. Thompson /// @}
1777a982d89SJeremy L. Thompson 
1787a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1797a982d89SJeremy L. Thompson /// CeedQFunction Backend API
1807a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1817a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend
1827a982d89SJeremy L. Thompson /// @{
1837a982d89SJeremy L. Thompson 
1847a982d89SJeremy L. Thompson /**
1857a982d89SJeremy L. Thompson   @brief Get the vector length of a CeedQFunction
1867a982d89SJeremy L. Thompson 
1877a982d89SJeremy L. Thompson   @param qf               CeedQFunction
188d1d35e2fSjeremylt   @param[out] vec_length  Variable to store vector length
1897a982d89SJeremy L. Thompson 
1907a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1917a982d89SJeremy L. Thompson 
1927a982d89SJeremy L. Thompson   @ref Backend
1937a982d89SJeremy L. Thompson **/
194d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
195d1d35e2fSjeremylt   *vec_length = qf->vec_length;
196e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1977a982d89SJeremy L. Thompson }
1987a982d89SJeremy L. Thompson 
1997a982d89SJeremy L. Thompson /**
2007a982d89SJeremy L. Thompson   @brief Get the number of inputs and outputs to a CeedQFunction
2017a982d89SJeremy L. Thompson 
2027a982d89SJeremy L. Thompson   @param qf               CeedQFunction
203d1d35e2fSjeremylt   @param[out] num_input   Variable to store number of input fields
204d1d35e2fSjeremylt   @param[out] num_output  Variable to store number of output fields
2057a982d89SJeremy L. Thompson 
2067a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2077a982d89SJeremy L. Thompson 
2087a982d89SJeremy L. Thompson   @ref Backend
2097a982d89SJeremy L. Thompson **/
210d1d35e2fSjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input,
211d1d35e2fSjeremylt                             CeedInt *num_output) {
212d1d35e2fSjeremylt   if (num_input) *num_input = qf->num_input_fields;
213d1d35e2fSjeremylt   if (num_output) *num_output = qf->num_output_fields;
214e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2157a982d89SJeremy L. Thompson }
2167a982d89SJeremy L. Thompson 
2177a982d89SJeremy L. Thompson /**
21843e1b16fSJeremy L Thompson   @brief Get the name of the user function for a CeedQFunction
2197a982d89SJeremy L. Thompson 
2207a982d89SJeremy L. Thompson   @param qf                CeedQFunction
22143e1b16fSJeremy L Thompson   @param[out] kernel_name  Variable to store source path string
2227a982d89SJeremy L. Thompson 
2237a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2247a982d89SJeremy L. Thompson 
2257a982d89SJeremy L. Thompson   @ref Backend
2267a982d89SJeremy L. Thompson **/
22743e1b16fSJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, char **kernel_name) {
22843e1b16fSJeremy L Thompson   *kernel_name = (char *) qf->kernel_name;
22943e1b16fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
23043e1b16fSJeremy L Thompson }
23143e1b16fSJeremy L Thompson 
23243e1b16fSJeremy L Thompson /**
23343e1b16fSJeremy L Thompson   @brief Get the source path string for a CeedQFunction
23443e1b16fSJeremy L Thompson 
23543e1b16fSJeremy L Thompson   @param qf                CeedQFunction
23643e1b16fSJeremy L Thompson   @param[out] source_path  Variable to store source path string
23743e1b16fSJeremy L Thompson 
23843e1b16fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
23943e1b16fSJeremy L Thompson 
24043e1b16fSJeremy L Thompson   @ref Backend
24143e1b16fSJeremy L Thompson **/
24243e1b16fSJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) {
24343e1b16fSJeremy L Thompson   *source_path = (char *) qf->source_path;
244e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2457a982d89SJeremy L. Thompson }
2467a982d89SJeremy L. Thompson 
2477a982d89SJeremy L. Thompson /**
2483d3250a0SJeremy L Thompson   @brief Initalize and load QFunction source file into string buffer, including
2493d3250a0SJeremy L Thompson            full text of local files in place of `#include "local.h"`.
2503d3250a0SJeremy L Thompson            The `buffer` is set to `NULL` if there is no QFunction source file.
2513d3250a0SJeremy L Thompson          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
2523d3250a0SJeremy L Thompson 
2533d3250a0SJeremy L Thompson   @param qf                  CeedQFunction
254f74ec584SJeremy L Thompson   @param[out] source_buffer  String buffer for source file contents
2553d3250a0SJeremy L Thompson 
2563d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2573d3250a0SJeremy L Thompson 
2583d3250a0SJeremy L Thompson   @ref Backend
2593d3250a0SJeremy L Thompson **/
2603d3250a0SJeremy L Thompson int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, char **source_buffer) {
2613d3250a0SJeremy L Thompson   int ierr;
2623d3250a0SJeremy L Thompson   char *source_path;
2633d3250a0SJeremy L Thompson 
2643d3250a0SJeremy L Thompson   ierr = CeedQFunctionGetSourcePath(qf, &source_path); CeedChk(ierr);
2653d3250a0SJeremy L Thompson   *source_buffer = NULL;
2663d3250a0SJeremy L Thompson   if (source_path) {
2673d3250a0SJeremy L Thompson     ierr = CeedLoadSourceToBuffer(qf->ceed, source_path, source_buffer);
2683d3250a0SJeremy L Thompson     CeedChk(ierr);
2693d3250a0SJeremy L Thompson   }
2703d3250a0SJeremy L Thompson 
2713d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2723d3250a0SJeremy L Thompson }
2733d3250a0SJeremy L Thompson 
2743d3250a0SJeremy L Thompson /**
2757a982d89SJeremy L. Thompson   @brief Get the User Function for a CeedQFunction
2767a982d89SJeremy L. Thompson 
2777a982d89SJeremy L. Thompson   @param qf      CeedQFunction
2787a982d89SJeremy L. Thompson   @param[out] f  Variable to store user function
2797a982d89SJeremy L. Thompson 
2807a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2817a982d89SJeremy L. Thompson 
2827a982d89SJeremy L. Thompson   @ref Backend
2837a982d89SJeremy L. Thompson **/
2847a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
2857a982d89SJeremy L. Thompson   *f = qf->function;
286e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2877a982d89SJeremy L. Thompson }
2887a982d89SJeremy L. Thompson 
2897a982d89SJeremy L. Thompson /**
290777ff853SJeremy L Thompson   @brief Get global context for a CeedQFunction.
291777ff853SJeremy L Thompson            Note: For QFunctions from the Fortran interface, this
292777ff853SJeremy L Thompson              function will return the Fortran context
293777ff853SJeremy L Thompson              CeedQFunctionContext.
2947a982d89SJeremy L. Thompson 
2957a982d89SJeremy L. Thompson   @param qf        CeedQFunction
296777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
2977a982d89SJeremy L. Thompson 
2987a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2997a982d89SJeremy L. Thompson 
3007a982d89SJeremy L. Thompson   @ref Backend
3017a982d89SJeremy L. Thompson **/
302777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
3037a982d89SJeremy L. Thompson   *ctx = qf->ctx;
304e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3057a982d89SJeremy L. Thompson }
3067a982d89SJeremy L. Thompson 
3077a982d89SJeremy L. Thompson /**
3087a982d89SJeremy L. Thompson   @brief Get true user context for a CeedQFunction
309777ff853SJeremy L Thompson            Note: For all QFunctions this function will return the user
310777ff853SJeremy L Thompson              CeedQFunctionContext and not interface context
311777ff853SJeremy L Thompson              CeedQFunctionContext, if any such object exists.
3127a982d89SJeremy L. Thompson 
3137a982d89SJeremy L. Thompson   @param qf        CeedQFunction
314777ff853SJeremy L Thompson   @param[out] ctx  Variable to store CeedQFunctionContext
3157a982d89SJeremy L. Thompson 
3167a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3177a982d89SJeremy L. Thompson   @ref Backend
3187a982d89SJeremy L. Thompson **/
319777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
320777ff853SJeremy L Thompson   int ierr;
321f04ea552SJeremy L Thompson   if (qf->is_fortran) {
322d1d35e2fSjeremylt     CeedFortranContext fortran_ctx = NULL;
323d1d35e2fSjeremylt     ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx);
324777ff853SJeremy L Thompson     CeedChk(ierr);
325d1d35e2fSjeremylt     *ctx = fortran_ctx->innerctx;
326d1d35e2fSjeremylt     ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx);
327d1d35e2fSjeremylt     CeedChk(ierr);
3287a982d89SJeremy L. Thompson   } else {
3297a982d89SJeremy L. Thompson     *ctx = qf->ctx;
3307a982d89SJeremy L. Thompson   }
331e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3327a982d89SJeremy L. Thompson }
3337a982d89SJeremy L. Thompson 
3347a982d89SJeremy L. Thompson /**
3357a982d89SJeremy L. Thompson   @brief Determine if QFunction is identity
3367a982d89SJeremy L. Thompson 
3377a982d89SJeremy L. Thompson   @param qf                CeedQFunction
338d1d35e2fSjeremylt   @param[out] is_identity  Variable to store identity status
3397a982d89SJeremy L. Thompson 
3407a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3417a982d89SJeremy L. Thompson 
3427a982d89SJeremy L. Thompson   @ref Backend
3437a982d89SJeremy L. Thompson **/
344d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
345f04ea552SJeremy L Thompson   *is_identity = qf->is_identity;
346e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3477a982d89SJeremy L. Thompson }
3487a982d89SJeremy L. Thompson 
3497a982d89SJeremy L. Thompson /**
3507a982d89SJeremy L. Thompson   @brief Get backend data of a CeedQFunction
3517a982d89SJeremy L. Thompson 
3527a982d89SJeremy L. Thompson   @param qf         CeedQFunction
3537a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
3547a982d89SJeremy L. Thompson 
3557a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3567a982d89SJeremy L. Thompson 
3577a982d89SJeremy L. Thompson   @ref Backend
3587a982d89SJeremy L. Thompson **/
359777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) {
360777ff853SJeremy L Thompson   *(void **)data = qf->data;
361e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3627a982d89SJeremy L. Thompson }
3637a982d89SJeremy L. Thompson 
3647a982d89SJeremy L. Thompson /**
3657a982d89SJeremy L. Thompson   @brief Set backend data of a CeedQFunction
3667a982d89SJeremy L. Thompson 
3677a982d89SJeremy L. Thompson   @param[out] qf  CeedQFunction
3687a982d89SJeremy L. Thompson   @param data     Data to set
3697a982d89SJeremy L. Thompson 
3707a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3717a982d89SJeremy L. Thompson 
3727a982d89SJeremy L. Thompson   @ref Backend
3737a982d89SJeremy L. Thompson **/
374777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) {
375777ff853SJeremy L Thompson   qf->data = data;
376e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3777a982d89SJeremy L. Thompson }
3787a982d89SJeremy L. Thompson 
3797a982d89SJeremy L. Thompson /**
38034359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunction
38134359f16Sjeremylt 
38234359f16Sjeremylt   @param qf  CeedQFunction to increment the reference counter
38334359f16Sjeremylt 
38434359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
38534359f16Sjeremylt 
38634359f16Sjeremylt   @ref Backend
38734359f16Sjeremylt **/
3889560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) {
38934359f16Sjeremylt   qf->ref_count++;
39034359f16Sjeremylt   return CEED_ERROR_SUCCESS;
39134359f16Sjeremylt }
39234359f16Sjeremylt 
3937a982d89SJeremy L. Thompson /// @}
3947a982d89SJeremy L. Thompson 
3957a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3967a982d89SJeremy L. Thompson /// CeedQFunction Public API
3977a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3987a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser
3997a982d89SJeremy L. Thompson /// @{
4007a982d89SJeremy L. Thompson 
4017a982d89SJeremy L. Thompson /**
4027a982d89SJeremy L. Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
4037a982d89SJeremy L. Thompson 
4047a982d89SJeremy L. Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
405d1d35e2fSjeremylt   @param vec_length  Vector length. Caller must ensure that number of quadrature
406d1d35e2fSjeremylt                        points is a multiple of vec_length.
4077a982d89SJeremy L. Thompson   @param f           Function pointer to evaluate action at quadrature points.
4087a982d89SJeremy L. Thompson                        See \ref CeedQFunctionUser.
4097a982d89SJeremy L. Thompson   @param source      Absolute path to source of QFunction,
410c8d5b03cSJeremy L Thompson                        "\abs_path\file.h:function_name".
411c8d5b03cSJeremy L Thompson                        For support across all backends, this source must only
412c8d5b03cSJeremy L Thompson                        contain constructs supported by C99, C++11, and CUDA.
4137a982d89SJeremy L. Thompson   @param[out] qf     Address of the variable where the newly created
4147a982d89SJeremy L. Thompson                        CeedQFunction will be stored
4157a982d89SJeremy L. Thompson 
4167a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4177a982d89SJeremy L. Thompson 
4187a982d89SJeremy L. Thompson   See \ref CeedQFunctionUser for details on the call-back function @a f's
4197a982d89SJeremy L. Thompson     arguments.
4207a982d89SJeremy L. Thompson 
4217a982d89SJeremy L. Thompson   @ref User
4227a982d89SJeremy L. Thompson **/
423d1d35e2fSjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length,
424d1d35e2fSjeremylt                                 CeedQFunctionUser f,
4257a982d89SJeremy L. Thompson                                 const char *source, CeedQFunction *qf) {
4267a982d89SJeremy L. Thompson   int ierr;
42743e1b16fSJeremy L Thompson   char *source_copy, *kernel_name_copy;
4287a982d89SJeremy L. Thompson 
4297a982d89SJeremy L. Thompson   if (!ceed->QFunctionCreate) {
4307a982d89SJeremy L. Thompson     Ceed delegate;
4317a982d89SJeremy L. Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
4327a982d89SJeremy L. Thompson 
4337a982d89SJeremy L. Thompson     if (!delegate)
4347a982d89SJeremy L. Thompson       // LCOV_EXCL_START
435e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
436e15f9bd0SJeremy L Thompson                        "Backend does not support QFunctionCreate");
4377a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
4387a982d89SJeremy L. Thompson 
439d1d35e2fSjeremylt     ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf);
4407a982d89SJeremy L. Thompson     CeedChk(ierr);
441e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
4427a982d89SJeremy L. Thompson   }
4437a982d89SJeremy L. Thompson 
44443e1b16fSJeremy L Thompson   if (strlen(source) && !strrchr(source, ':'))\
44543e1b16fSJeremy L Thompson     // LCOV_EXCL_START
44643e1b16fSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPLETE,
44743e1b16fSJeremy L Thompson                      "Provided path to source does not include function name. "
44843e1b16fSJeremy L Thompson                      "Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"",
44943e1b16fSJeremy L Thompson                      source);
45043e1b16fSJeremy L Thompson   // LCOV_EXCL_STOP
45143e1b16fSJeremy L Thompson 
4527a982d89SJeremy L. Thompson   ierr = CeedCalloc(1, qf); CeedChk(ierr);
4537a982d89SJeremy L. Thompson   (*qf)->ceed = ceed;
4549560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
455d1d35e2fSjeremylt   (*qf)->ref_count = 1;
456d1d35e2fSjeremylt   (*qf)->vec_length = vec_length;
457f04ea552SJeremy L Thompson   (*qf)->is_identity = false;
4587a982d89SJeremy L. Thompson   (*qf)->function = f;
45943e1b16fSJeremy L Thompson   if (strlen(source)) {
46043e1b16fSJeremy L Thompson     const char *kernel_name = strrchr(source, ':') + 1;
46143e1b16fSJeremy L Thompson     size_t kernel_name_len = strlen(kernel_name);
46243e1b16fSJeremy L Thompson     ierr = CeedCalloc(kernel_name_len + 1, &kernel_name_copy); CeedChk(ierr);
46343e1b16fSJeremy L Thompson     strncpy(kernel_name_copy, kernel_name, kernel_name_len);
46443e1b16fSJeremy L Thompson     (*qf)->kernel_name = kernel_name_copy;
46543e1b16fSJeremy L Thompson 
46643e1b16fSJeremy L Thompson     size_t source_len = strlen(source) - kernel_name_len - 1;
46743e1b16fSJeremy L Thompson     ierr = CeedCalloc(source_len + 1, &source_copy); CeedChk(ierr);
46843e1b16fSJeremy L Thompson     strncpy(source_copy, source, source_len);
469d1d35e2fSjeremylt     (*qf)->source_path = source_copy;
47043e1b16fSJeremy L Thompson   }
471bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields); CeedChk(ierr);
472bf4cb664SJeremy L Thompson   ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields); CeedChk(ierr);
4737a982d89SJeremy L. Thompson   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
474e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4757a982d89SJeremy L. Thompson }
4767a982d89SJeremy L. Thompson 
4777a982d89SJeremy L. Thompson /**
478288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
479288c0443SJeremy L Thompson 
480288c0443SJeremy L Thompson   @param ceed     A Ceed object where the CeedQFunction will be created
481288c0443SJeremy L Thompson   @param name     Name of QFunction to use from gallery
482288c0443SJeremy L Thompson   @param[out] qf  Address of the variable where the newly created
483288c0443SJeremy L Thompson                     CeedQFunction will be stored
484288c0443SJeremy L Thompson 
485288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
486288c0443SJeremy L Thompson 
4877a982d89SJeremy L. Thompson   @ref User
488288c0443SJeremy L Thompson **/
489288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
490288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
491288c0443SJeremy L Thompson   int ierr;
492d1d35e2fSjeremylt   size_t match_len = 0, match_idx = UINT_MAX;
49375affc3bSjeremylt   char *name_copy;
494288c0443SJeremy L Thompson 
4951d013790SJed Brown   ierr = CeedQFunctionRegisterAll(); CeedChk(ierr);
496288c0443SJeremy L Thompson   // Find matching backend
497e15f9bd0SJeremy L Thompson   if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE,
498e15f9bd0SJeremy L Thompson                                 "No QFunction name provided");
499288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
500288c0443SJeremy L Thompson     size_t n;
501d1d35e2fSjeremylt     const char *curr_name = gallery_qfunctions[i].name;
502d1d35e2fSjeremylt     for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {}
503d1d35e2fSjeremylt     if (n > match_len) {
504d1d35e2fSjeremylt       match_len = n;
505d1d35e2fSjeremylt       match_idx = i;
506288c0443SJeremy L Thompson     }
507288c0443SJeremy L Thompson   }
508d1d35e2fSjeremylt   if (!match_len)
509138d4072Sjeremylt     // LCOV_EXCL_START
510e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
511138d4072Sjeremylt   // LCOV_EXCL_STOP
512288c0443SJeremy L Thompson 
513288c0443SJeremy L Thompson   // Create QFunction
514d1d35e2fSjeremylt   ierr = CeedQFunctionCreateInterior(ceed,
515d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].vec_length,
516d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].f,
517d1d35e2fSjeremylt                                      gallery_qfunctions[match_idx].source, qf);
518288c0443SJeremy L Thompson   CeedChk(ierr);
519288c0443SJeremy L Thompson 
520288c0443SJeremy L Thompson   // QFunction specific setup
521d1d35e2fSjeremylt   ierr = gallery_qfunctions[match_idx].init(ceed, name, *qf); CeedChk(ierr);
522288c0443SJeremy L Thompson 
52375affc3bSjeremylt   // Copy name
52475affc3bSjeremylt   size_t slen = strlen(name) + 1;
52575affc3bSjeremylt   ierr = CeedMalloc(slen, &name_copy); CeedChk(ierr);
52675affc3bSjeremylt   memcpy(name_copy, name, slen);
52743e1b16fSJeremy L Thompson   (*qf)->gallery_name = name_copy;
52843e1b16fSJeremy L Thompson   (*qf)->is_gallery = true;
529e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
530288c0443SJeremy L Thompson }
531288c0443SJeremy L Thompson 
532288c0443SJeremy L Thompson /**
5330219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
5340219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
5350219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
5360219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
5370219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
5380219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
5390219ea01SJeremy L Thompson            location for both.
5400219ea01SJeremy L Thompson 
5410219ea01SJeremy L Thompson   @param ceed          A Ceed object where the CeedQFunction will be created
542d1d35e2fSjeremylt   @param[in] size      Size of the QFunction fields
543d1d35e2fSjeremylt   @param[in] in_mode   CeedEvalMode for input to CeedQFunction
544d1d35e2fSjeremylt   @param[in] out_mode  CeedEvalMode for output to CeedQFunction
5450219ea01SJeremy L Thompson   @param[out] qf       Address of the variable where the newly created
5460219ea01SJeremy L Thompson                          CeedQFunction will be stored
5470219ea01SJeremy L Thompson 
5480219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5490219ea01SJeremy L Thompson 
5507a982d89SJeremy L. Thompson   @ref User
5510219ea01SJeremy L Thompson **/
552d1d35e2fSjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode,
553d1d35e2fSjeremylt                                 CeedEvalMode out_mode, CeedQFunction *qf) {
5540219ea01SJeremy L Thompson   int ierr;
5550219ea01SJeremy L Thompson 
5560219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
557d1d35e2fSjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr);
558d1d35e2fSjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr);
5590219ea01SJeremy L Thompson 
560f04ea552SJeremy L Thompson   (*qf)->is_identity = true;
561*547dbd6fSJeremy L Thompson 
562777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
563*547dbd6fSJeremy L Thompson   ierr = CeedQFunctionGetContext(*qf, &ctx); CeedChk(ierr);
564*547dbd6fSJeremy L Thompson   ierr = CeedQFunctionContextSetInt32(ctx, "size", size); CeedChk(ierr);
565*547dbd6fSJeremy L Thompson 
566e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5670219ea01SJeremy L Thompson }
5680219ea01SJeremy L Thompson 
5690219ea01SJeremy L Thompson /**
5709560d06aSjeremylt   @brief Copy the pointer to a CeedQFunction. Both pointers should
5719560d06aSjeremylt            be destroyed with `CeedQFunctionDestroy()`;
5729560d06aSjeremylt            Note: If `*qf_copy` is non-NULL, then it is assumed that
5739560d06aSjeremylt            `*qf_copy` is a pointer to a CeedQFunction. This
5749560d06aSjeremylt            CeedQFunction will be destroyed if `*qf_copy` is the only
5759560d06aSjeremylt            reference to this CeedQFunction.
5769560d06aSjeremylt 
5779560d06aSjeremylt   @param qf            CeedQFunction to copy reference to
5789560d06aSjeremylt   @param[out] qf_copy  Variable to store copied reference
5799560d06aSjeremylt 
5809560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
5819560d06aSjeremylt 
5829560d06aSjeremylt   @ref User
5839560d06aSjeremylt **/
5849560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
5859560d06aSjeremylt   int ierr;
5869560d06aSjeremylt 
5879560d06aSjeremylt   ierr = CeedQFunctionReference(qf); CeedChk(ierr);
5889560d06aSjeremylt   ierr = CeedQFunctionDestroy(qf_copy); CeedChk(ierr);
5899560d06aSjeremylt   *qf_copy = qf;
5909560d06aSjeremylt   return CEED_ERROR_SUCCESS;
5919560d06aSjeremylt }
5929560d06aSjeremylt 
5939560d06aSjeremylt /**
594a0a97fcfSJed Brown   @brief Add a CeedQFunction input
595b11c1e72Sjeremylt 
596b11c1e72Sjeremylt   @param qf          CeedQFunction
597d1d35e2fSjeremylt   @param field_name  Name of QFunction field
598d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
599d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
600d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
601b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
602b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
603b11c1e72Sjeremylt 
604b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
605dfdf5a53Sjeremylt 
6067a982d89SJeremy L. Thompson   @ref User
607b11c1e72Sjeremylt **/
608d1d35e2fSjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name,
609d1d35e2fSjeremylt                           CeedInt size,
610d1d35e2fSjeremylt                           CeedEvalMode eval_mode) {
611f04ea552SJeremy L Thompson   if (qf->is_immutable)
612e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
613e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
614f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
615e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
616d1d35e2fSjeremylt   if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1))
617e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
618e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
619e15f9bd0SJeremy L Thompson                      "CEED_EVAL_WEIGHT should have size 1");
620e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
621d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields],
622d1d35e2fSjeremylt                                    field_name, size, eval_mode);
623fe2413ffSjeremylt   CeedChk(ierr);
624d1d35e2fSjeremylt   qf->num_input_fields++;
625e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
626d7b241e6Sjeremylt }
627d7b241e6Sjeremylt 
628b11c1e72Sjeremylt /**
629a0a97fcfSJed Brown   @brief Add a CeedQFunction output
630b11c1e72Sjeremylt 
631b11c1e72Sjeremylt   @param qf          CeedQFunction
632d1d35e2fSjeremylt   @param field_name  Name of QFunction field
633d1d35e2fSjeremylt   @param size        Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or
634d1d35e2fSjeremylt                        (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP
635d1d35e2fSjeremylt   @param eval_mode   \ref CEED_EVAL_NONE to use values directly,
636b11c1e72Sjeremylt                        \ref CEED_EVAL_INTERP to use interpolated values,
637b11c1e72Sjeremylt                        \ref CEED_EVAL_GRAD to use gradients.
638b11c1e72Sjeremylt 
639b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
640dfdf5a53Sjeremylt 
6417a982d89SJeremy L. Thompson   @ref User
642b11c1e72Sjeremylt **/
643d1d35e2fSjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name,
644d1d35e2fSjeremylt                            CeedInt size, CeedEvalMode eval_mode) {
645f04ea552SJeremy L Thompson   if (qf->is_immutable)
646e15f9bd0SJeremy L Thompson     // LCOV_EXCL_START
647e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_MAJOR,
648f04ea552SJeremy L Thompson                      "QFunction cannot be changed after set as immutable");
649e15f9bd0SJeremy L Thompson   // LCOV_EXCL_STOP
650d1d35e2fSjeremylt   if (eval_mode == CEED_EVAL_WEIGHT)
651c042f62fSJeremy L Thompson     // LCOV_EXCL_START
652e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
653e15f9bd0SJeremy L Thompson                      "Cannot create QFunction output with "
6541d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
655c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
656d1d35e2fSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields],
657d1d35e2fSjeremylt                                    field_name, size, eval_mode);
658fe2413ffSjeremylt   CeedChk(ierr);
659d1d35e2fSjeremylt   qf->num_output_fields++;
660e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
661d7b241e6Sjeremylt }
662d7b241e6Sjeremylt 
663dfdf5a53Sjeremylt /**
66443bbe138SJeremy L Thompson   @brief Get the CeedQFunctionFields of a CeedQFunction
66543bbe138SJeremy L Thompson 
666f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
667f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
668f04ea552SJeremy L Thompson 
66943bbe138SJeremy L Thompson   @param qf                      CeedQFunction
670f74ec584SJeremy L Thompson   @param[out] num_input_fields   Variable to store number of input fields
671f74ec584SJeremy L Thompson   @param[out] input_fields       Variable to store input fields
672f74ec584SJeremy L Thompson   @param[out] num_output_fields  Variable to store number of output fields
673f74ec584SJeremy L Thompson   @param[out] output_fields      Variable to store output fields
67443bbe138SJeremy L Thompson 
67543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
67643bbe138SJeremy L Thompson 
677e9b533fbSJeremy L Thompson   @ref Advanced
67843bbe138SJeremy L Thompson **/
67943bbe138SJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields,
68043bbe138SJeremy L Thompson                            CeedQFunctionField **input_fields,
68143bbe138SJeremy L Thompson                            CeedInt *num_output_fields,
68243bbe138SJeremy L Thompson                            CeedQFunctionField **output_fields) {
683f04ea552SJeremy L Thompson   qf->is_immutable = true;
68443bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = qf->num_input_fields;
68543bbe138SJeremy L Thompson   if (input_fields) *input_fields = qf->input_fields;
68643bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = qf->num_output_fields;
68743bbe138SJeremy L Thompson   if (output_fields) *output_fields = qf->output_fields;
68843bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
68943bbe138SJeremy L Thompson }
69043bbe138SJeremy L Thompson 
69143bbe138SJeremy L Thompson /**
69243bbe138SJeremy L Thompson   @brief Get the name of a CeedQFunctionField
69343bbe138SJeremy L Thompson 
69443bbe138SJeremy L Thompson   @param qf_field         CeedQFunctionField
69543bbe138SJeremy L Thompson   @param[out] field_name  Variable to store the field name
69643bbe138SJeremy L Thompson 
69743bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
69843bbe138SJeremy L Thompson 
699e9b533fbSJeremy L Thompson   @ref Advanced
70043bbe138SJeremy L Thompson **/
70143bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
70243bbe138SJeremy L Thompson   *field_name = (char *)qf_field->field_name;
70343bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
70443bbe138SJeremy L Thompson }
70543bbe138SJeremy L Thompson 
70643bbe138SJeremy L Thompson /**
70743bbe138SJeremy L Thompson   @brief Get the number of components of a CeedQFunctionField
70843bbe138SJeremy L Thompson 
70943bbe138SJeremy L Thompson   @param qf_field   CeedQFunctionField
71043bbe138SJeremy L Thompson   @param[out] size  Variable to store the size of the field
71143bbe138SJeremy L Thompson 
71243bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
71343bbe138SJeremy L Thompson 
714e9b533fbSJeremy L Thompson   @ref Advanced
71543bbe138SJeremy L Thompson **/
71643bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
71743bbe138SJeremy L Thompson   *size = qf_field->size;
71843bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
71943bbe138SJeremy L Thompson }
72043bbe138SJeremy L Thompson 
72143bbe138SJeremy L Thompson /**
72243bbe138SJeremy L Thompson   @brief Get the CeedEvalMode of a CeedQFunctionField
72343bbe138SJeremy L Thompson 
72443bbe138SJeremy L Thompson   @param qf_field        CeedQFunctionField
72543bbe138SJeremy L Thompson   @param[out] eval_mode  Variable to store the field evaluation mode
72643bbe138SJeremy L Thompson 
72743bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
72843bbe138SJeremy L Thompson 
729e9b533fbSJeremy L Thompson   @ref Advanced
73043bbe138SJeremy L Thompson **/
73143bbe138SJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field,
73243bbe138SJeremy L Thompson                                   CeedEvalMode *eval_mode) {
73343bbe138SJeremy L Thompson   *eval_mode = qf_field->eval_mode;
73443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
73543bbe138SJeremy L Thompson }
73643bbe138SJeremy L Thompson 
73743bbe138SJeremy L Thompson /**
7384ce2993fSjeremylt   @brief Set global context for a CeedQFunction
739b11c1e72Sjeremylt 
740b11c1e72Sjeremylt   @param qf   CeedQFunction
741b11c1e72Sjeremylt   @param ctx  Context data to set
742b11c1e72Sjeremylt 
743b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
744dfdf5a53Sjeremylt 
7457a982d89SJeremy L. Thompson   @ref User
746b11c1e72Sjeremylt **/
747777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
74834359f16Sjeremylt   int ierr;
749d7b241e6Sjeremylt   qf->ctx = ctx;
7509560d06aSjeremylt   ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr);
751e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
752d7b241e6Sjeremylt }
753d7b241e6Sjeremylt 
754b11c1e72Sjeremylt /**
75575affc3bSjeremylt   @brief View a CeedQFunction
75675affc3bSjeremylt 
75775affc3bSjeremylt   @param[in] qf      CeedQFunction to view
75875affc3bSjeremylt   @param[in] stream  Stream to write; typically stdout/stderr or a file
75975affc3bSjeremylt 
76075affc3bSjeremylt   @return Error code: 0 - success, otherwise - failure
76175affc3bSjeremylt 
7627a982d89SJeremy L. Thompson   @ref User
76375affc3bSjeremylt **/
76475affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
76575affc3bSjeremylt   int ierr;
76675affc3bSjeremylt 
76775affc3bSjeremylt   fprintf(stream, "%sCeedQFunction %s\n",
76843e1b16fSJeremy L Thompson           qf->is_gallery ? "Gallery " : "User ",
76943e1b16fSJeremy L Thompson           qf->is_gallery ? qf->gallery_name : qf->kernel_name);
77075affc3bSjeremylt 
771d1d35e2fSjeremylt   fprintf(stream, "  %d Input Field%s:\n", qf->num_input_fields,
772d1d35e2fSjeremylt           qf->num_input_fields>1 ? "s" : "");
773d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_input_fields; i++) {
774d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream);
7752da88da5Sjeremylt     CeedChk(ierr);
77675affc3bSjeremylt   }
77775affc3bSjeremylt 
778d1d35e2fSjeremylt   fprintf(stream, "  %d Output Field%s:\n", qf->num_output_fields,
779d1d35e2fSjeremylt           qf->num_output_fields>1 ? "s" : "");
780d1d35e2fSjeremylt   for (CeedInt i=0; i<qf->num_output_fields; i++) {
781d1d35e2fSjeremylt     ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream);
78275affc3bSjeremylt     CeedChk(ierr);
78375affc3bSjeremylt   }
784e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
78575affc3bSjeremylt }
78675affc3bSjeremylt 
78775affc3bSjeremylt /**
788b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunction
789b7c9bbdaSJeremy L Thompson 
790b7c9bbdaSJeremy L Thompson   @param qf              CeedQFunction
791b7c9bbdaSJeremy L Thompson   @param[out] ceed       Variable to store Ceed
792b7c9bbdaSJeremy L Thompson 
793b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
794b7c9bbdaSJeremy L Thompson 
795b7c9bbdaSJeremy L Thompson   @ref Advanced
796b7c9bbdaSJeremy L Thompson **/
797b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
798b7c9bbdaSJeremy L Thompson   *ceed = qf->ceed;
799b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
800b7c9bbdaSJeremy L Thompson }
801b7c9bbdaSJeremy L Thompson 
802b7c9bbdaSJeremy L Thompson /**
803b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
804b11c1e72Sjeremylt 
805f04ea552SJeremy L Thompson   Note: Calling this function asserts that setup is complete
806f04ea552SJeremy L Thompson           and sets the CeedQFunction as immutable.
807f04ea552SJeremy L Thompson 
808b11c1e72Sjeremylt   @param qf      CeedQFunction
809b11c1e72Sjeremylt   @param Q       Number of quadrature points
81034138859Sjeremylt   @param[in] u   Array of input CeedVectors
81134138859Sjeremylt   @param[out] v  Array of output CeedVectors
812b11c1e72Sjeremylt 
813b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
814dfdf5a53Sjeremylt 
8157a982d89SJeremy L. Thompson   @ref User
816b11c1e72Sjeremylt **/
817d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
818aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
819d7b241e6Sjeremylt   int ierr;
820d7b241e6Sjeremylt   if (!qf->Apply)
821c042f62fSJeremy L Thompson     // LCOV_EXCL_START
822e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED,
823e15f9bd0SJeremy L Thompson                      "Backend does not support QFunctionApply");
824c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
825d1d35e2fSjeremylt   if (Q % qf->vec_length)
826c042f62fSJeremy L Thompson     // LCOV_EXCL_START
827e15f9bd0SJeremy L Thompson     return CeedError(qf->ceed, CEED_ERROR_DIMENSION,
828e15f9bd0SJeremy L Thompson                      "Number of quadrature points %d must be a "
829d1d35e2fSjeremylt                      "multiple of %d", Q, qf->vec_length);
830c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
831f04ea552SJeremy L Thompson   qf->is_immutable = true;
832d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
833e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
834d7b241e6Sjeremylt }
835d7b241e6Sjeremylt 
836b11c1e72Sjeremylt /**
837b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
838b11c1e72Sjeremylt 
839b11c1e72Sjeremylt   @param qf  CeedQFunction to destroy
840b11c1e72Sjeremylt 
841b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
842dfdf5a53Sjeremylt 
8437a982d89SJeremy L. Thompson   @ref User
844b11c1e72Sjeremylt **/
845d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
846d7b241e6Sjeremylt   int ierr;
847d7b241e6Sjeremylt 
848d1d35e2fSjeremylt   if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS;
849fe2413ffSjeremylt   // Backend destroy
850d7b241e6Sjeremylt   if ((*qf)->Destroy) {
851d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
852d7b241e6Sjeremylt   }
853fe2413ffSjeremylt   // Free fields
854d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_input_fields; i++) {
855d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr);
856d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr);
857fe2413ffSjeremylt   }
858d1d35e2fSjeremylt   for (int i=0; i<(*qf)->num_output_fields; i++) {
859d1d35e2fSjeremylt     ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr);
860d1d35e2fSjeremylt     ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr);
861fe2413ffSjeremylt   }
862d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr);
863d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr);
864777ff853SJeremy L Thompson 
865777ff853SJeremy L Thompson   // User context data object
866777ff853SJeremy L Thompson   ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr);
867fe2413ffSjeremylt 
868d1d35e2fSjeremylt   ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr);
86943e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->gallery_name); CeedChk(ierr);
87043e1b16fSJeremy L Thompson   ierr = CeedFree(&(*qf)->kernel_name); CeedChk(ierr);
871d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
872d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
873e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
874d7b241e6Sjeremylt }
875d7b241e6Sjeremylt 
876d7b241e6Sjeremylt /// @}
877