xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision 60f77c51b5a8877aa3ab66b685cde4f5f7c967bb)
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 
17d7b241e6Sjeremylt #include <ceed-impl.h>
18d863ab9bSjeremylt #include <ceed-backend.h>
19d7b241e6Sjeremylt #include <string.h>
20288c0443SJeremy L Thompson #include <limits.h>
21288c0443SJeremy L Thompson 
22288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP
23288c0443SJeremy L Thompson static struct {
24288c0443SJeremy L Thompson   char name[CEED_MAX_RESOURCE_LEN];
25288c0443SJeremy L Thompson   char source[CEED_MAX_RESOURCE_LEN];
26288c0443SJeremy L Thompson   CeedInt vlength;
27288c0443SJeremy L Thompson   CeedQFunctionUser f;
28288c0443SJeremy L Thompson   int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
29288c0443SJeremy L Thompson } qfunctions[1024];
30288c0443SJeremy L Thompson static size_t num_qfunctions;
31288c0443SJeremy L Thompson /// @endcond
32d7b241e6Sjeremylt 
33dfdf5a53Sjeremylt /// @file
34dfdf5a53Sjeremylt /// Implementation of public CeedQFunction interfaces
35dfdf5a53Sjeremylt ///
36dfdf5a53Sjeremylt /// @addtogroup CeedQFunction
37dfdf5a53Sjeremylt /// @{
38d7b241e6Sjeremylt 
39d7b241e6Sjeremylt /**
40d7b241e6Sjeremylt   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
41d7b241e6Sjeremylt 
42b11c1e72Sjeremylt   @param ceed       A Ceed object where the CeedQFunction will be created
43d7b241e6Sjeremylt   @param vlength    Vector length.  Caller must ensure that number of quadrature
44d7b241e6Sjeremylt                     points is a multiple of vlength.
45d7b241e6Sjeremylt   @param f          Function pointer to evaluate action at quadrature points.
469f0427d9SYohann                     See \ref CeedQFunctionUser.
471176cc3aSJeremy L Thompson   @param source     Absolute path to source of QFunction,
481176cc3aSJeremy L Thompson                       "\abs_path\file.h:function_name"
49b11c1e72Sjeremylt   @param[out] qf    Address of the variable where the newly created
50b11c1e72Sjeremylt                       CeedQFunction will be stored
51b11c1e72Sjeremylt 
52b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
53d7b241e6Sjeremylt 
548795c945Sjeremylt   See \ref CeedQFunctionUser for details on the call-back function @a f's
558795c945Sjeremylt     arguments.
56d7b241e6Sjeremylt 
57dfdf5a53Sjeremylt   @ref Basic
58dfdf5a53Sjeremylt **/
59692c2638Sjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength, CeedQFunctionUser f,
60288c0443SJeremy L Thompson                                 const char *source, CeedQFunction *qf) {
61d7b241e6Sjeremylt   int ierr;
62288c0443SJeremy L Thompson   char *source_copy;
63d7b241e6Sjeremylt 
645fe0d4faSjeremylt   if (!ceed->QFunctionCreate) {
655fe0d4faSjeremylt     Ceed delegate;
66aefd8378Sjeremylt     ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr);
675fe0d4faSjeremylt 
685fe0d4faSjeremylt     if (!delegate)
69c042f62fSJeremy L Thompson       // LCOV_EXCL_START
70d7b241e6Sjeremylt       return CeedError(ceed, 1, "Backend does not support QFunctionCreate");
71c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
725fe0d4faSjeremylt 
73288c0443SJeremy L Thompson     ierr = CeedQFunctionCreateInterior(delegate, vlength, f, source, qf);
741dfeef1dSjeremylt     CeedChk(ierr);
755fe0d4faSjeremylt     return 0;
765fe0d4faSjeremylt   }
775fe0d4faSjeremylt 
78d7b241e6Sjeremylt   ierr = CeedCalloc(1,qf); CeedChk(ierr);
79d7b241e6Sjeremylt   (*qf)->ceed = ceed;
80d7b241e6Sjeremylt   ceed->refcount++;
81d7b241e6Sjeremylt   (*qf)->refcount = 1;
82d7b241e6Sjeremylt   (*qf)->vlength = vlength;
830219ea01SJeremy L Thompson   (*qf)->identity = 0;
84d7b241e6Sjeremylt   (*qf)->function = f;
8507a02837SJed Brown   size_t slen = strlen(source) + 1;
8607a02837SJed Brown   ierr = CeedMalloc(slen, &source_copy); CeedChk(ierr);
8707a02837SJed Brown   memcpy(source_copy, source, slen);
88288c0443SJeremy L Thompson   (*qf)->sourcepath = source_copy;
89fe2413ffSjeremylt   ierr = CeedCalloc(16, &(*qf)->inputfields); CeedChk(ierr);
90fe2413ffSjeremylt   ierr = CeedCalloc(16, &(*qf)->outputfields); CeedChk(ierr);
91d7b241e6Sjeremylt   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
92d7b241e6Sjeremylt   return 0;
93d7b241e6Sjeremylt }
94d7b241e6Sjeremylt 
95b11c1e72Sjeremylt /**
96288c0443SJeremy L Thompson   @brief Register a gallery QFunction
97288c0443SJeremy L Thompson 
98288c0443SJeremy L Thompson   @param name    Name for this backend to respond to
991176cc3aSJeremy L Thompson   @param source  Absolute path to source of QFunction,
1001176cc3aSJeremy L Thompson                    "\path\CEED_DIR\gallery\folder\file.h:function_name"
101288c0443SJeremy L Thompson   @param vlength Vector length.  Caller must ensure that number of quadrature
102288c0443SJeremy L Thompson                    points is a multiple of vlength.
103288c0443SJeremy L Thompson   @param f       Function pointer to evaluate action at quadrature points.
104288c0443SJeremy L Thompson                    See \ref CeedQFunctionUser.
105288c0443SJeremy L Thompson   @param init    Initialization function called by CeedQFunctionInit() when the
106288c0443SJeremy L Thompson                    QFunction is selected.
107288c0443SJeremy L Thompson 
108288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
109288c0443SJeremy L Thompson 
110288c0443SJeremy L Thompson   @ref Advanced
111288c0443SJeremy L Thompson **/
112288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source,
113288c0443SJeremy L Thompson                           CeedInt vlength, CeedQFunctionUser f,
114288c0443SJeremy L Thompson                           int (*init)(Ceed, const char *, CeedQFunction)) {
115c042f62fSJeremy L Thompson   if (num_qfunctions >= sizeof(qfunctions) / sizeof(qfunctions[0]))
116c042f62fSJeremy L Thompson     // LCOV_EXCL_START
117288c0443SJeremy L Thompson     return CeedError(NULL, 1, "Too many gallery QFunctions");
118c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
119c042f62fSJeremy L Thompson 
120288c0443SJeremy L Thompson   strncpy(qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
121288c0443SJeremy L Thompson   qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0;
122288c0443SJeremy L Thompson   strncpy(qfunctions[num_qfunctions].source, source, CEED_MAX_RESOURCE_LEN);
123288c0443SJeremy L Thompson   qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0;
124288c0443SJeremy L Thompson   qfunctions[num_qfunctions].vlength = vlength;
125288c0443SJeremy L Thompson   qfunctions[num_qfunctions].f = f;
126288c0443SJeremy L Thompson   qfunctions[num_qfunctions].init = init;
127288c0443SJeremy L Thompson   num_qfunctions++;
128288c0443SJeremy L Thompson   return 0;
129288c0443SJeremy L Thompson }
130288c0443SJeremy L Thompson 
131288c0443SJeremy L Thompson /**
132288c0443SJeremy L Thompson   @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name.
133288c0443SJeremy L Thompson 
134288c0443SJeremy L Thompson   @param ceed       A Ceed object where the CeedQFunction will be created
135288c0443SJeremy L Thompson   @param name       Name of QFunction to use from gallery
136288c0443SJeremy L Thompson   @param[out] qf    Address of the variable where the newly created
137288c0443SJeremy L Thompson                       CeedQFunction will be stored
138288c0443SJeremy L Thompson 
139288c0443SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
140288c0443SJeremy L Thompson 
141288c0443SJeremy L Thompson   @ref Basic
142288c0443SJeremy L Thompson **/
143288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed,  const char *name,
144288c0443SJeremy L Thompson                                       CeedQFunction *qf) {
145288c0443SJeremy L Thompson   int ierr;
146288c0443SJeremy L Thompson   size_t matchlen = 0, matchidx = UINT_MAX;
147288c0443SJeremy L Thompson 
148288c0443SJeremy L Thompson   // Find matching backend
149288c0443SJeremy L Thompson   if (!name) return CeedError(NULL, 1, "No QFunction name provided");
150288c0443SJeremy L Thompson   for (size_t i=0; i<num_qfunctions; i++) {
151288c0443SJeremy L Thompson     size_t n;
152288c0443SJeremy L Thompson     const char *currname = qfunctions[i].name;
153288c0443SJeremy L Thompson     for (n = 0; currname[n] && currname[n] == name[n]; n++) {}
154288c0443SJeremy L Thompson     if (n > matchlen) {
155288c0443SJeremy L Thompson       matchlen = n;
156288c0443SJeremy L Thompson       matchidx = i;
157288c0443SJeremy L Thompson     }
158288c0443SJeremy L Thompson   }
1591d102b48SJeremy L Thompson   if (!matchlen)
160138d4072Sjeremylt     // LCOV_EXCL_START
1611d102b48SJeremy L Thompson     return CeedError(NULL, 1, "No suitable gallery QFunction");
162138d4072Sjeremylt   // LCOV_EXCL_STOP
163288c0443SJeremy L Thompson 
164288c0443SJeremy L Thompson   // Create QFunction
165288c0443SJeremy L Thompson   ierr = CeedQFunctionCreateInterior(ceed, qfunctions[matchidx].vlength,
166288c0443SJeremy L Thompson                                      qfunctions[matchidx].f,
167288c0443SJeremy L Thompson                                      qfunctions[matchidx].source, qf);
168288c0443SJeremy L Thompson   CeedChk(ierr);
169288c0443SJeremy L Thompson 
170288c0443SJeremy L Thompson   // QFunction specific setup
171288c0443SJeremy L Thompson   ierr = qfunctions[matchidx].init(ceed, name, *qf); CeedChk(ierr);
172288c0443SJeremy L Thompson 
173288c0443SJeremy L Thompson   return 0;
174288c0443SJeremy L Thompson }
175288c0443SJeremy L Thompson 
176288c0443SJeremy L Thompson /**
1770219ea01SJeremy L Thompson   @brief Create an identity CeedQFunction. Inputs are written into outputs in
1780219ea01SJeremy L Thompson            the order given. This is useful for CeedOperators that can be
1790219ea01SJeremy L Thompson            represented with only the action of a CeedRestriction and CeedBasis,
1800219ea01SJeremy L Thompson            such as restriction and prolongation operators for p-multigrid.
1810219ea01SJeremy L Thompson            Backends may optimize CeedOperators with this CeedQFunction to avoid
1820219ea01SJeremy L Thompson            the copy of input data to output fields by using the same memory
1830219ea01SJeremy L Thompson            location for both.
1840219ea01SJeremy L Thompson 
1850219ea01SJeremy L Thompson   @param ceed        A Ceed object where the CeedQFunction will be created
186*60f77c51Sjeremylt   @param[in] size    Size of the qfunction fields
187*60f77c51Sjeremylt   @param[in] inmode  CeedEvalMode for input to CeedQFunction
188*60f77c51Sjeremylt   @param[in] outmode CeedEvalMode for output to CeedQFunction
1890219ea01SJeremy L Thompson   @param[out] qf     Address of the variable where the newly created
1900219ea01SJeremy L Thompson                        CeedQFunction will be stored
1910219ea01SJeremy L Thompson 
1920219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1930219ea01SJeremy L Thompson 
1940219ea01SJeremy L Thompson   @ref Basic
1950219ea01SJeremy L Thompson **/
196*60f77c51Sjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode inmode,
197*60f77c51Sjeremylt                                 CeedEvalMode outmode, CeedQFunction *qf) {
1980219ea01SJeremy L Thompson   int ierr;
1990219ea01SJeremy L Thompson 
200*60f77c51Sjeremylt   if (inmode == CEED_EVAL_NONE && outmode == CEED_EVAL_NONE)
201*60f77c51Sjeremylt     // LCOV_EXCL_START
202*60f77c51Sjeremylt     return CeedError(ceed, 1, "CEED_EVAL_NONE for a both the input and "
203*60f77c51Sjeremylt                      "output does not make sense with an identity QFunction");
204*60f77c51Sjeremylt   // LCOV_EXCL_STOP
205*60f77c51Sjeremylt 
2060219ea01SJeremy L Thompson   ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr);
207*60f77c51Sjeremylt   ierr = CeedQFunctionAddInput(*qf, "input", 1, inmode); CeedChk(ierr);
208*60f77c51Sjeremylt   ierr = CeedQFunctionAddOutput(*qf, "output", 1, outmode); CeedChk(ierr);
2090219ea01SJeremy L Thompson 
2100219ea01SJeremy L Thompson   (*qf)->identity = 1;
2110219ea01SJeremy L Thompson   if (size > 1) {
2120219ea01SJeremy L Thompson     CeedInt *ctx;
2130219ea01SJeremy L Thompson     ierr = CeedCalloc(1, &ctx); CeedChk(ierr);
2140219ea01SJeremy L Thompson     ctx[0] = size;
2150219ea01SJeremy L Thompson     ierr = CeedQFunctionSetContext(*qf, ctx, sizeof(ctx)); CeedChk(ierr);
216c0fea178Sjeremylt     (*qf)->inputfields[0]->size = size;
217c0fea178Sjeremylt     (*qf)->outputfields[0]->size = size;
2180219ea01SJeremy L Thompson   }
2190219ea01SJeremy L Thompson 
2200219ea01SJeremy L Thompson   return 0;
2210219ea01SJeremy L Thompson }
2220219ea01SJeremy L Thompson 
2230219ea01SJeremy L Thompson /**
224a0a97fcfSJed Brown   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
225b11c1e72Sjeremylt 
226b11c1e72Sjeremylt   @param f          CeedQFunctionField
227b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
2284d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
22923e8ed12Sjeremylt                       1 for CEED_EVAL_NONE, CEED_EVAL_INTERP, and CEED_EVAL_WEIGHT)
230b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
231b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
23223e8ed12Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients,
23323e8ed12Sjeremylt                       \ref CEED_EVAL_WEIGHT to use quadrature weights.
234b11c1e72Sjeremylt 
235b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
236dfdf5a53Sjeremylt 
237dfdf5a53Sjeremylt   @ref Developer
238b11c1e72Sjeremylt **/
239fe2413ffSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *fieldname,
2404d537eeaSYohann                                  CeedInt size, CeedEvalMode emode) {
241d7b241e6Sjeremylt   size_t len = strlen(fieldname);
242d7b241e6Sjeremylt   char *tmp;
243fe2413ffSjeremylt   int ierr;
244fe2413ffSjeremylt   ierr = CeedCalloc(1,f); CeedChk(ierr);
245fe2413ffSjeremylt 
246fe2413ffSjeremylt   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
247d7b241e6Sjeremylt   memcpy(tmp, fieldname, len+1);
248fe2413ffSjeremylt   (*f)->fieldname = tmp;
2494d537eeaSYohann   (*f)->size = size;
250fe2413ffSjeremylt   (*f)->emode = emode;
251d7b241e6Sjeremylt   return 0;
252d7b241e6Sjeremylt }
253d7b241e6Sjeremylt 
254b11c1e72Sjeremylt /**
255a0a97fcfSJed Brown   @brief Add a CeedQFunction input
256b11c1e72Sjeremylt 
257b11c1e72Sjeremylt   @param qf         CeedQFunction
258b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
2594d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
2604d537eeaSYohann                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
261b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
262b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
263b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
264b11c1e72Sjeremylt 
265b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
266dfdf5a53Sjeremylt 
267dfdf5a53Sjeremylt   @ref Basic
268b11c1e72Sjeremylt **/
2691d102b48SJeremy L Thompson int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname, CeedInt size,
2701d102b48SJeremy L Thompson                           CeedEvalMode emode) {
271fe2413ffSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->inputfields[qf->numinputfields],
2724d537eeaSYohann                                    fieldname, size, emode);
273fe2413ffSjeremylt   CeedChk(ierr);
274fe2413ffSjeremylt   qf->numinputfields++;
275d7b241e6Sjeremylt   return 0;
276d7b241e6Sjeremylt }
277d7b241e6Sjeremylt 
278b11c1e72Sjeremylt /**
279a0a97fcfSJed Brown   @brief Add a CeedQFunction output
280b11c1e72Sjeremylt 
281b11c1e72Sjeremylt   @param qf         CeedQFunction
282b11c1e72Sjeremylt   @param fieldname  Name of QFunction field
2834d537eeaSYohann   @param size       Size of QFunction field, ncomp * (dim for CEED_EVAL_GRAD or
2844d537eeaSYohann                       1 for CEED_EVAL_NONE and CEED_EVAL_INTERP)
285b11c1e72Sjeremylt   @param emode      \ref CEED_EVAL_NONE to use values directly,
286b11c1e72Sjeremylt                       \ref CEED_EVAL_INTERP to use interpolated values,
287b11c1e72Sjeremylt                       \ref CEED_EVAL_GRAD to use gradients.
288b11c1e72Sjeremylt 
289b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
290dfdf5a53Sjeremylt 
291dfdf5a53Sjeremylt   @ref Basic
292b11c1e72Sjeremylt **/
293d7b241e6Sjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
2944d537eeaSYohann                            CeedInt size, CeedEvalMode emode) {
295d7b241e6Sjeremylt   if (emode == CEED_EVAL_WEIGHT)
296c042f62fSJeremy L Thompson     // LCOV_EXCL_START
2971d102b48SJeremy L Thompson     return CeedError(qf->ceed, 1, "Cannot create QFunction output with "
2981d102b48SJeremy L Thompson                      "CEED_EVAL_WEIGHT");
299c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
300fe2413ffSjeremylt   int ierr = CeedQFunctionFieldSet(&qf->outputfields[qf->numoutputfields],
3014d537eeaSYohann                                    fieldname, size, emode);
302fe2413ffSjeremylt   CeedChk(ierr);
303fe2413ffSjeremylt   qf->numoutputfields++;
304d7b241e6Sjeremylt   return 0;
305d7b241e6Sjeremylt }
306d7b241e6Sjeremylt 
307dfdf5a53Sjeremylt /**
3084ce2993fSjeremylt   @brief Get the Ceed associated with a CeedQFunction
3094ce2993fSjeremylt 
3104ce2993fSjeremylt   @param qf              CeedQFunction
3114ce2993fSjeremylt   @param[out] ceed       Variable to store Ceed
3124ce2993fSjeremylt 
3134ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3144ce2993fSjeremylt 
31523617272Sjeremylt   @ref Advanced
3164ce2993fSjeremylt **/
3174ce2993fSjeremylt 
3184ce2993fSjeremylt int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
3194ce2993fSjeremylt   *ceed = qf->ceed;
3204ce2993fSjeremylt   return 0;
3214ce2993fSjeremylt }
3224ce2993fSjeremylt 
3234ce2993fSjeremylt /**
3244ce2993fSjeremylt   @brief Get the vector length of a CeedQFunction
3254ce2993fSjeremylt 
3264ce2993fSjeremylt   @param qf            CeedQFunction
327288c0443SJeremy L Thompson   @param[out] vlength  Variable to store vector length
3284ce2993fSjeremylt 
3294ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3304ce2993fSjeremylt 
33123617272Sjeremylt   @ref Advanced
3324ce2993fSjeremylt **/
3334ce2993fSjeremylt 
3344ce2993fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength) {
3354ce2993fSjeremylt   *vlength = qf->vlength;
3364ce2993fSjeremylt   return 0;
3374ce2993fSjeremylt }
3384ce2993fSjeremylt 
3394ce2993fSjeremylt /**
340dfdf5a53Sjeremylt   @brief Get the number of inputs and outputs to a CeedQFunction
341dfdf5a53Sjeremylt 
342dfdf5a53Sjeremylt   @param qf              CeedQFunction
3434ce2993fSjeremylt   @param[out] numinput   Variable to store number of input fields
3444ce2993fSjeremylt   @param[out] numoutput  Variable to store number of output fields
345dfdf5a53Sjeremylt 
346dfdf5a53Sjeremylt   @return An error code: 0 - success, otherwise - failure
347dfdf5a53Sjeremylt 
34823617272Sjeremylt   @ref Advanced
349dfdf5a53Sjeremylt **/
350dfdf5a53Sjeremylt 
351d7b241e6Sjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput,
352d7b241e6Sjeremylt                             CeedInt *numoutput) {
3531a4ead9bSjeremylt   if (numinput) *numinput = qf->numinputfields;
3541a4ead9bSjeremylt   if (numoutput) *numoutput = qf->numoutputfields;
355d7b241e6Sjeremylt   return 0;
356d7b241e6Sjeremylt }
357d7b241e6Sjeremylt 
358d7b241e6Sjeremylt /**
359288c0443SJeremy L Thompson   @brief Get the source path string for a CeedQFunction
3604ce2993fSjeremylt 
3614ce2993fSjeremylt   @param qf              CeedQFunction
362288c0443SJeremy L Thompson   @param[out] source     Variable to store source path string
3634ce2993fSjeremylt 
3644ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3654ce2993fSjeremylt 
36623617272Sjeremylt   @ref Advanced
3674ce2993fSjeremylt **/
3684ce2993fSjeremylt 
369288c0443SJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source) {
370288c0443SJeremy L Thompson   *source = (char *) qf->sourcepath;
3714ce2993fSjeremylt   return 0;
3724ce2993fSjeremylt }
3734ce2993fSjeremylt 
3744ce2993fSjeremylt /**
375fe2413ffSjeremylt   @brief Get the User Function for a CeedQFunction
376fe2413ffSjeremylt 
377fe2413ffSjeremylt   @param qf              CeedQFunction
378fe2413ffSjeremylt   @param[out] f          Variable to store user function
379fe2413ffSjeremylt 
380fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
381fe2413ffSjeremylt 
382fe2413ffSjeremylt   @ref Advanced
383fe2413ffSjeremylt **/
384fe2413ffSjeremylt 
385d4f68153Sjeremylt int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
386d4f68153Sjeremylt   *f = qf->function;
387fe2413ffSjeremylt   return 0;
388fe2413ffSjeremylt }
389fe2413ffSjeremylt 
390fe2413ffSjeremylt /**
3914ce2993fSjeremylt   @brief Get global context size for a CeedQFunction
3924ce2993fSjeremylt 
3934ce2993fSjeremylt   @param qf              CeedQFunction
3944ce2993fSjeremylt   @param[out] ctxsize    Variable to store size of context data values
3954ce2993fSjeremylt 
3964ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
3974ce2993fSjeremylt 
39823617272Sjeremylt   @ref Advanced
3994ce2993fSjeremylt **/
4004ce2993fSjeremylt 
4014ce2993fSjeremylt int CeedQFunctionGetContextSize(CeedQFunction qf, size_t *ctxsize) {
402069aeabaSjeremylt   if (qf->fortranstatus) {
403069aeabaSjeremylt     fContext *fctx = qf->ctx;
404069aeabaSjeremylt     *ctxsize = fctx->innerctxsize;
405069aeabaSjeremylt   } else {
4064ce2993fSjeremylt     *ctxsize = qf->ctxsize;
407069aeabaSjeremylt   }
4084ce2993fSjeremylt   return 0;
4094ce2993fSjeremylt }
4104ce2993fSjeremylt 
4114ce2993fSjeremylt /**
4124ce2993fSjeremylt   @brief Get global context for a CeedQFunction
4134ce2993fSjeremylt 
4144ce2993fSjeremylt   @param qf              CeedQFunction
4154ce2993fSjeremylt   @param[out] ctx        Variable to store context data values
4164ce2993fSjeremylt 
4174ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
4184ce2993fSjeremylt 
41923617272Sjeremylt   @ref Advanced
4204ce2993fSjeremylt **/
4214ce2993fSjeremylt 
4224ce2993fSjeremylt int CeedQFunctionGetContext(CeedQFunction qf, void **ctx) {
4234ce2993fSjeremylt   *ctx = qf->ctx;
4244ce2993fSjeremylt   return 0;
4254ce2993fSjeremylt }
4264ce2993fSjeremylt 
4274ce2993fSjeremylt /**
428418fb8c2Sjeremylt   @brief Determine if Fortran interface was used
429418fb8c2Sjeremylt 
430418fb8c2Sjeremylt   @param qf                  CeedQFunction
431418fb8c2Sjeremylt   @param[out] fortranstatus  Variable to store Fortran status
432418fb8c2Sjeremylt 
433418fb8c2Sjeremylt   @return An error code: 0 - success, otherwise - failure
434418fb8c2Sjeremylt 
435418fb8c2Sjeremylt   @ref Advanced
436418fb8c2Sjeremylt **/
437418fb8c2Sjeremylt 
438418fb8c2Sjeremylt int CeedQFunctionGetFortranStatus(CeedQFunction qf, bool *fortranstatus) {
439418fb8c2Sjeremylt   *fortranstatus = qf->fortranstatus;
440418fb8c2Sjeremylt   return 0;
441418fb8c2Sjeremylt }
442418fb8c2Sjeremylt 
443418fb8c2Sjeremylt /**
4440219ea01SJeremy L Thompson   @brief Determine if QFunction is identity
4450219ea01SJeremy L Thompson 
4460219ea01SJeremy L Thompson   @param qf               CeedQFunction
4470219ea01SJeremy L Thompson   @param[out] identity    Variable to store identity status
4480219ea01SJeremy L Thompson 
4490219ea01SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4500219ea01SJeremy L Thompson 
4510219ea01SJeremy L Thompson   @ref Advanced
4520219ea01SJeremy L Thompson **/
4530219ea01SJeremy L Thompson 
4540219ea01SJeremy L Thompson int CeedQFunctionGetIdentityStatus(CeedQFunction qf, bool *identity) {
4550219ea01SJeremy L Thompson   *identity = qf->identity;
4560219ea01SJeremy L Thompson   return 0;
4570219ea01SJeremy L Thompson }
4580219ea01SJeremy L Thompson 
4590219ea01SJeremy L Thompson /**
46014922b2aSjeremylt   @brief Get true user context for a CeedQFunction
461069aeabaSjeremylt 
462069aeabaSjeremylt   @param qf              CeedQFunction
463069aeabaSjeremylt   @param[out] ctx        Variable to store context data values
464069aeabaSjeremylt 
465069aeabaSjeremylt   @return An error code: 0 - success, otherwise - failure
466069aeabaSjeremylt 
467069aeabaSjeremylt   @ref Advanced
468069aeabaSjeremylt **/
469069aeabaSjeremylt 
47014922b2aSjeremylt int CeedQFunctionGetInnerContext(CeedQFunction qf, void **ctx) {
47114922b2aSjeremylt   if (qf->fortranstatus) {
472069aeabaSjeremylt     fContext *fctx = qf->ctx;
473069aeabaSjeremylt     *ctx = fctx->innerctx;
47414922b2aSjeremylt   } else {
47514922b2aSjeremylt     *ctx = qf->ctx;
47614922b2aSjeremylt   }
47714922b2aSjeremylt 
4789f0427d9SYohann 
479069aeabaSjeremylt   return 0;
480069aeabaSjeremylt }
481069aeabaSjeremylt 
482069aeabaSjeremylt /**
4834ce2993fSjeremylt   @brief Get backend data of a CeedQFunction
4844ce2993fSjeremylt 
4854ce2993fSjeremylt   @param qf              CeedQFunction
4864ce2993fSjeremylt   @param[out] data       Variable to store data
4874ce2993fSjeremylt 
4884ce2993fSjeremylt   @return An error code: 0 - success, otherwise - failure
4894ce2993fSjeremylt 
49023617272Sjeremylt   @ref Advanced
4914ce2993fSjeremylt **/
4924ce2993fSjeremylt 
4934ce2993fSjeremylt int CeedQFunctionGetData(CeedQFunction qf, void **data) {
4944ce2993fSjeremylt   *data = qf->data;
4954ce2993fSjeremylt   return 0;
4964ce2993fSjeremylt }
4974ce2993fSjeremylt 
4984ce2993fSjeremylt /**
499fe2413ffSjeremylt   @brief Set backend data of a CeedQFunction
500fe2413ffSjeremylt 
501fe2413ffSjeremylt   @param[out] qf         CeedQFunction
502fe2413ffSjeremylt   @param data            Data to set
503fe2413ffSjeremylt 
504fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
505fe2413ffSjeremylt 
506fe2413ffSjeremylt   @ref Advanced
507fe2413ffSjeremylt **/
508fe2413ffSjeremylt 
509fe2413ffSjeremylt int CeedQFunctionSetData(CeedQFunction qf, void **data) {
510fe2413ffSjeremylt   qf->data = *data;
511fe2413ffSjeremylt   return 0;
512fe2413ffSjeremylt }
513fe2413ffSjeremylt 
514fe2413ffSjeremylt /**
5154ce2993fSjeremylt   @brief Set global context for a CeedQFunction
516b11c1e72Sjeremylt 
517b11c1e72Sjeremylt   @param qf       CeedQFunction
518b11c1e72Sjeremylt   @param ctx      Context data to set
519b11c1e72Sjeremylt   @param ctxsize  Size of context data values
520b11c1e72Sjeremylt 
521b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
522dfdf5a53Sjeremylt 
523dfdf5a53Sjeremylt   @ref Basic
524b11c1e72Sjeremylt **/
525d7b241e6Sjeremylt int CeedQFunctionSetContext(CeedQFunction qf, void *ctx, size_t ctxsize) {
526d7b241e6Sjeremylt   qf->ctx = ctx;
527d7b241e6Sjeremylt   qf->ctxsize = ctxsize;
528d7b241e6Sjeremylt   return 0;
529d7b241e6Sjeremylt }
530d7b241e6Sjeremylt 
531b11c1e72Sjeremylt /**
532b11c1e72Sjeremylt   @brief Apply the action of a CeedQFunction
533b11c1e72Sjeremylt 
534b11c1e72Sjeremylt   @param qf      CeedQFunction
535b11c1e72Sjeremylt   @param Q       Number of quadrature points
536b11c1e72Sjeremylt   @param[in] u   Array of input data arrays
537b11c1e72Sjeremylt   @param[out] v  Array of output data arrays
538b11c1e72Sjeremylt 
539b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
540dfdf5a53Sjeremylt 
541dfdf5a53Sjeremylt   @ref Advanced
542b11c1e72Sjeremylt **/
543d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
544aedaa0e5Sjeremylt                        CeedVector *u, CeedVector *v) {
545d7b241e6Sjeremylt   int ierr;
546d7b241e6Sjeremylt   if (!qf->Apply)
547c042f62fSJeremy L Thompson     // LCOV_EXCL_START
548d7b241e6Sjeremylt     return CeedError(qf->ceed, 1, "Backend does not support QFunctionApply");
549c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
550d7b241e6Sjeremylt   if (Q % qf->vlength)
551c042f62fSJeremy L Thompson     // LCOV_EXCL_START
5521d102b48SJeremy L Thompson     return CeedError(qf->ceed, 2, "Number of quadrature points %d must be a "
5531d102b48SJeremy L Thompson                      "multiple of %d", Q, qf->vlength);
554c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
555d7b241e6Sjeremylt   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
556d7b241e6Sjeremylt   return 0;
557d7b241e6Sjeremylt }
558d7b241e6Sjeremylt 
559b11c1e72Sjeremylt /**
560d1bcdac9Sjeremylt   @brief Get the CeedQFunctionFields of a CeedQFunction
561d1bcdac9Sjeremylt 
562d1bcdac9Sjeremylt   @param qf                 CeedQFunction
563d1bcdac9Sjeremylt   @param[out] inputfields   Variable to store inputfields
564d1bcdac9Sjeremylt   @param[out] outputfields  Variable to store outputfields
565d1bcdac9Sjeremylt 
566d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
567d1bcdac9Sjeremylt 
568d1bcdac9Sjeremylt   @ref Advanced
569d1bcdac9Sjeremylt **/
570d1bcdac9Sjeremylt 
571692c2638Sjeremylt int CeedQFunctionGetFields(CeedQFunction qf, CeedQFunctionField **inputfields,
572d1bcdac9Sjeremylt                            CeedQFunctionField **outputfields) {
5731d102b48SJeremy L Thompson   if (inputfields)
5741d102b48SJeremy L Thompson     *inputfields = qf->inputfields;
5751d102b48SJeremy L Thompson   if (outputfields)
5761d102b48SJeremy L Thompson     *outputfields = qf->outputfields;
577d1bcdac9Sjeremylt   return 0;
578d1bcdac9Sjeremylt }
579d1bcdac9Sjeremylt 
580d1bcdac9Sjeremylt /**
581fe2413ffSjeremylt   @brief Get the name of a CeedQFunctionField
582fe2413ffSjeremylt 
583fe2413ffSjeremylt   @param qffield         CeedQFunctionField
584fe2413ffSjeremylt   @param[out] fieldname  Variable to store the field name
585fe2413ffSjeremylt 
586fe2413ffSjeremylt   @return An error code: 0 - success, otherwise - failure
587fe2413ffSjeremylt 
588fe2413ffSjeremylt   @ref Advanced
589fe2413ffSjeremylt **/
590fe2413ffSjeremylt 
591692c2638Sjeremylt int CeedQFunctionFieldGetName(CeedQFunctionField qffield, char **fieldname) {
592fe2413ffSjeremylt   *fieldname = (char *)qffield->fieldname;
593fe2413ffSjeremylt   return 0;
594fe2413ffSjeremylt }
595fe2413ffSjeremylt 
596fe2413ffSjeremylt /**
597d1bcdac9Sjeremylt   @brief Get the number of components of a CeedQFunctionField
598d1bcdac9Sjeremylt 
599d1bcdac9Sjeremylt   @param qffield    CeedQFunctionField
6004d537eeaSYohann   @param[out] size  Variable to store the size of the field
601d1bcdac9Sjeremylt 
602d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
603d1bcdac9Sjeremylt 
604d1bcdac9Sjeremylt   @ref Advanced
605d1bcdac9Sjeremylt **/
606d1bcdac9Sjeremylt 
6074d537eeaSYohann int CeedQFunctionFieldGetSize(CeedQFunctionField qffield, CeedInt *size) {
6084d537eeaSYohann   *size = qffield->size;
609d1bcdac9Sjeremylt   return 0;
610d1bcdac9Sjeremylt }
611d1bcdac9Sjeremylt 
612d1bcdac9Sjeremylt /**
613d1bcdac9Sjeremylt   @brief Get the CeedEvalMode of a CeedQFunctionField
614d1bcdac9Sjeremylt 
615d1bcdac9Sjeremylt   @param qffield         CeedQFunctionField
616288c0443SJeremy L Thompson   @param[out] emode      Variable to store the field evaluation mode
617d1bcdac9Sjeremylt 
618d1bcdac9Sjeremylt   @return An error code: 0 - success, otherwise - failure
619d1bcdac9Sjeremylt 
620d1bcdac9Sjeremylt   @ref Advanced
621d1bcdac9Sjeremylt **/
622d1bcdac9Sjeremylt 
623d1bcdac9Sjeremylt int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qffield,
624d1bcdac9Sjeremylt                                   CeedEvalMode *emode) {
625fe2413ffSjeremylt   *emode = qffield->emode;
626d1bcdac9Sjeremylt   return 0;
627d1bcdac9Sjeremylt }
628d1bcdac9Sjeremylt 
629d1bcdac9Sjeremylt /**
630b11c1e72Sjeremylt   @brief Destroy a CeedQFunction
631b11c1e72Sjeremylt 
632b11c1e72Sjeremylt   @param qf CeedQFunction to destroy
633b11c1e72Sjeremylt 
634b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
635dfdf5a53Sjeremylt 
636dfdf5a53Sjeremylt   @ref Basic
637b11c1e72Sjeremylt **/
638d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) {
639d7b241e6Sjeremylt   int ierr;
640d7b241e6Sjeremylt 
6411d102b48SJeremy L Thompson   if (!*qf || --(*qf)->refcount > 0)
6421d102b48SJeremy L Thompson     return 0;
643fe2413ffSjeremylt   // Backend destroy
644d7b241e6Sjeremylt   if ((*qf)->Destroy) {
645d7b241e6Sjeremylt     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
646d7b241e6Sjeremylt   }
647fe2413ffSjeremylt   // Free fields
648fe2413ffSjeremylt   for (int i=0; i<(*qf)->numinputfields; i++) {
649fe2413ffSjeremylt     ierr = CeedFree(&(*(*qf)->inputfields[i]).fieldname); CeedChk(ierr);
650fe2413ffSjeremylt     ierr = CeedFree(&(*qf)->inputfields[i]); CeedChk(ierr);
651fe2413ffSjeremylt   }
652fe2413ffSjeremylt   for (int i=0; i<(*qf)->numoutputfields; i++) {
653fe2413ffSjeremylt     ierr = CeedFree(&(*(*qf)->outputfields[i]).fieldname); CeedChk(ierr);
654fe2413ffSjeremylt     ierr = CeedFree(&(*qf)->outputfields[i]); CeedChk(ierr);
655fe2413ffSjeremylt   }
656fe2413ffSjeremylt   ierr = CeedFree(&(*qf)->inputfields); CeedChk(ierr);
657fe2413ffSjeremylt   ierr = CeedFree(&(*qf)->outputfields); CeedChk(ierr);
6580219ea01SJeremy L Thompson   // Free ctx if identity
6590219ea01SJeremy L Thompson   if ((*qf)->identity) {
6600219ea01SJeremy L Thompson     ierr = CeedFree(&(*qf)->ctx); CeedChk(ierr);
6610219ea01SJeremy L Thompson   }
662fe2413ffSjeremylt 
663288c0443SJeremy L Thompson   ierr = CeedFree(&(*qf)->sourcepath); CeedChk(ierr);
664d7b241e6Sjeremylt   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
665d7b241e6Sjeremylt   ierr = CeedFree(qf); CeedChk(ierr);
666d7b241e6Sjeremylt   return 0;
667d7b241e6Sjeremylt }
668d7b241e6Sjeremylt 
669d7b241e6Sjeremylt /// @}
670