xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunction.c (revision 14922b2aa5b06c5d10198c874ef2e7680888d59b)
1 // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3 // reserved. See files LICENSE and NOTICE for details.
4 //
5 // This file is part of CEED, a collection of benchmarks, miniapps, software
6 // libraries and APIs for efficient high-order finite element and spectral
7 // element discretizations for exascale applications. For more information and
8 // source code availability see http://github.com/ceed.
9 //
10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11 // a collaborative effort of two U.S. Department of Energy organizations (Office
12 // of Science and the National Nuclear Security Administration) responsible for
13 // the planning and preparation of a capable exascale ecosystem, including
14 // software, applications, hardware, advanced system engineering and early
15 // testbed platforms, in support of the nation's exascale computing imperative.
16 
17 #include <ceed-impl.h>
18 #include <ceed-backend.h>
19 #include <string.h>
20 
21 /// @file
22 /// Implementation of public CeedQFunction interfaces
23 ///
24 /// @addtogroup CeedQFunction
25 /// @{
26 
27 /**
28   @brief Create a CeedQFunction for evaluating interior (volumetric) terms.
29 
30   @param ceed       A Ceed object where the CeedQFunction will be created
31   @param vlength    Vector length.  Caller must ensure that number of quadrature
32                     points is a multiple of vlength.
33   @param f          Function pointer to evaluate action at quadrature points.
34                     See below.
35   @param focca      OCCA identifier "file.c:function_name" for definition of `f`
36   @param[out] qf    Address of the variable where the newly created
37                      CeedQFunction will be stored
38 
39   @return An error code: 0 - success, otherwise - failure
40 
41   The arguments of the call-back 'function' are:
42 
43    1. [void *ctx][in/out] - user data, this is the 'ctx' pointer stored in
44               the CeedQFunction, set by calling CeedQFunctionSetContext
45 
46    2. [CeedInt nq][in] - number of quadrature points to process
47 
48    3. [const CeedScalar *const *u][in] - input fields data at quadrature pts, listed in the order given by the user
49 
50    4. [CeedScalar *const *v][out] - output fields data at quadrature points, again listed in order given by the user
51 
52   @ref Basic
53 **/
54 int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength,
55                                 int (*f)(void*, CeedInt, const CeedScalar *const*, CeedScalar *const*),
56                                 const char *focca, CeedQFunction *qf) {
57   int ierr;
58   char *focca_copy;
59 
60   if (!ceed->QFunctionCreate) {
61     Ceed delegate;
62     ierr = CeedGetDelegate(ceed, &delegate); CeedChk(ierr);
63 
64     if (!delegate)
65       return CeedError(ceed, 1, "Backend does not support QFunctionCreate");
66 
67     ierr = CeedQFunctionCreateInterior(delegate, vlength, f, focca, qf);
68     CeedChk(ierr);
69     return 0;
70   }
71 
72   ierr = CeedCalloc(1,qf); CeedChk(ierr);
73   (*qf)->ceed = ceed;
74   ceed->refcount++;
75   (*qf)->refcount = 1;
76   (*qf)->vlength = vlength;
77   (*qf)->function = f;
78   ierr = CeedCalloc(strlen(focca)+1, &focca_copy); CeedChk(ierr);
79   strcpy(focca_copy, focca);
80   (*qf)->focca = focca_copy;
81   ierr = CeedCalloc(16, &(*qf)->inputfields); CeedChk(ierr);
82   ierr = CeedCalloc(16, &(*qf)->outputfields); CeedChk(ierr);
83   ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr);
84   return 0;
85 }
86 
87 /**
88   @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output
89 
90   @param f          CeedQFunctionField
91   @param fieldname  Name of QFunction field
92   @param ncomp      Number of components per quadrature node
93   @param emode      \ref CEED_EVAL_NONE to use values directly,
94                       \ref CEED_EVAL_INTERP to use interpolated values,
95                       \ref CEED_EVAL_GRAD to use gradients.
96 
97   @return An error code: 0 - success, otherwise - failure
98 
99   @ref Developer
100 **/
101 static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *fieldname,
102                                  CeedInt ncomp, CeedEvalMode emode) {
103   size_t len = strlen(fieldname);
104   char *tmp;
105   int ierr;
106   ierr = CeedCalloc(1,f); CeedChk(ierr);
107 
108   ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr);
109   memcpy(tmp, fieldname, len+1);
110   (*f)->fieldname = tmp;
111   (*f)->ncomp = ncomp;
112   (*f)->emode = emode;
113   return 0;
114 }
115 
116 /**
117   @brief Add a CeedQFunction input
118 
119   @param qf         CeedQFunction
120   @param fieldname  Name of QFunction field
121   @param ncomp      Number of components per quadrature node
122   @param emode      \ref CEED_EVAL_NONE to use values directly,
123                       \ref CEED_EVAL_INTERP to use interpolated values,
124                       \ref CEED_EVAL_GRAD to use gradients.
125 
126   @return An error code: 0 - success, otherwise - failure
127 
128   @ref Basic
129 **/
130 int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname,
131                           CeedInt ncomp, CeedEvalMode emode) {
132   int ierr = CeedQFunctionFieldSet(&qf->inputfields[qf->numinputfields],
133                                    fieldname, ncomp, emode);
134   CeedChk(ierr);
135   qf->numinputfields++;
136   return 0;
137 }
138 
139 /**
140   @brief Add a CeedQFunction output
141 
142   @param qf         CeedQFunction
143   @param fieldname  Name of QFunction field
144   @param ncomp      Number of components per quadrature node
145   @param emode      \ref CEED_EVAL_NONE to use values directly,
146                       \ref CEED_EVAL_INTERP to use interpolated values,
147                       \ref CEED_EVAL_GRAD to use gradients.
148 
149   @return An error code: 0 - success, otherwise - failure
150 
151   @ref Basic
152 **/
153 int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
154                            CeedInt ncomp, CeedEvalMode emode) {
155   if (emode == CEED_EVAL_WEIGHT)
156     return CeedError(qf->ceed, 1,
157                      "Cannot create qfunction output with CEED_EVAL_WEIGHT");
158   int ierr = CeedQFunctionFieldSet(&qf->outputfields[qf->numoutputfields],
159                                    fieldname, ncomp, emode);
160   CeedChk(ierr);
161   qf->numoutputfields++;
162   return 0;
163 }
164 
165 /**
166   @brief Get the Ceed associated with a CeedQFunction
167 
168   @param qf              CeedQFunction
169   @param[out] ceed       Variable to store Ceed
170 
171   @return An error code: 0 - success, otherwise - failure
172 
173   @ref Advanced
174 **/
175 
176 int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
177   *ceed = qf->ceed;
178   return 0;
179 }
180 
181 /**
182   @brief Get the vector length of a CeedQFunction
183 
184   @param qf              CeedQFunction
185   @param[out] veclength  Variable to store vector length
186 
187   @return An error code: 0 - success, otherwise - failure
188 
189   @ref Advanced
190 **/
191 
192 int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength) {
193   *vlength = qf->vlength;
194   return 0;
195 }
196 
197 /**
198   @brief Get the number of inputs and outputs to a CeedQFunction
199 
200   @param qf              CeedQFunction
201   @param[out] numinput   Variable to store number of input fields
202   @param[out] numoutput  Variable to store number of output fields
203 
204   @return An error code: 0 - success, otherwise - failure
205 
206   @ref Advanced
207 **/
208 
209 int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput,
210                             CeedInt *numoutput) {
211   if (numinput) *numinput = qf->numinputfields;
212   if (numoutput) *numoutput = qf->numoutputfields;
213   return 0;
214 }
215 
216 /**
217   @brief Get the FOCCA string for a CeedQFunction
218 
219   @param qf              CeedQFunction
220   @param[out] focca      Variable to store focca string
221 
222   @return An error code: 0 - success, otherwise - failure
223 
224   @ref Advanced
225 **/
226 
227 int CeedQFunctionGetFOCCA(CeedQFunction qf, char* *focca) {
228   *focca = (char*) qf->focca;
229   return 0;
230 }
231 
232 /**
233   @brief Get the User Function for a CeedQFunction
234 
235   @param qf              CeedQFunction
236   @param[out] f          Variable to store user function
237 
238   @return An error code: 0 - success, otherwise - failure
239 
240   @ref Advanced
241 **/
242 
243 int CeedQFunctionGetUserFunction(CeedQFunction qf, int (**f)()) {
244   *f = (int (*)())qf->function;
245   return 0;
246 }
247 
248 /**
249   @brief Get global context size for a CeedQFunction
250 
251   @param qf              CeedQFunction
252   @param[out] ctxsize    Variable to store size of context data values
253 
254   @return An error code: 0 - success, otherwise - failure
255 
256   @ref Advanced
257 **/
258 
259 int CeedQFunctionGetContextSize(CeedQFunction qf, size_t *ctxsize) {
260   if (qf->fortranstatus) {
261     fContext *fctx = qf->ctx;
262     *ctxsize = fctx->innerctxsize;
263   } else {
264     *ctxsize = qf->ctxsize;
265   }
266   return 0;
267 }
268 
269 /**
270   @brief Get global context for a CeedQFunction
271 
272   @param qf              CeedQFunction
273   @param[out] ctx        Variable to store context data values
274 
275   @return An error code: 0 - success, otherwise - failure
276 
277   @ref Advanced
278 **/
279 
280 int CeedQFunctionGetContext(CeedQFunction qf, void* *ctx) {
281   *ctx = qf->ctx;
282   return 0;
283 }
284 
285 /**
286   @brief Determine if Fortran interface was used
287 
288   @param qf                  CeedQFunction
289   @param[out] fortranstatus  Variable to store Fortran status
290 
291   @return An error code: 0 - success, otherwise - failure
292 
293   @ref Advanced
294 **/
295 
296 int CeedQFunctionGetFortranStatus(CeedQFunction qf, bool *fortranstatus) {
297   *fortranstatus = qf->fortranstatus;
298   return 0;
299 }
300 
301 /**
302   @brief Get true user context for a CeedQFunction
303 
304   @param qf              CeedQFunction
305   @param[out] ctx        Variable to store context data values
306 
307   @return An error code: 0 - success, otherwise - failure
308 
309   @ref Advanced
310 **/
311 
312 int CeedQFunctionGetInnerContext(CeedQFunction qf, void* *ctx) {
313   if (qf->fortranstatus) {
314     fContext *fctx = qf->ctx;
315     *ctx = fctx->innerctx;
316   } else {
317     *ctx = qf->ctx;
318   }
319 
320   return 0;
321 }
322 
323 /**
324   @brief Get backend data of a CeedQFunction
325 
326   @param qf              CeedQFunction
327   @param[out] data       Variable to store data
328 
329   @return An error code: 0 - success, otherwise - failure
330 
331   @ref Advanced
332 **/
333 
334 int CeedQFunctionGetData(CeedQFunction qf, void* *data) {
335   *data = qf->data;
336   return 0;
337 }
338 
339 /**
340   @brief Set backend data of a CeedQFunction
341 
342   @param[out] qf         CeedQFunction
343   @param data            Data to set
344 
345   @return An error code: 0 - success, otherwise - failure
346 
347   @ref Advanced
348 **/
349 
350 int CeedQFunctionSetData(CeedQFunction qf, void* *data) {
351   qf->data = *data;
352   return 0;
353 }
354 
355 /**
356   @brief Set global context for a CeedQFunction
357 
358   @param qf       CeedQFunction
359   @param ctx      Context data to set
360   @param ctxsize  Size of context data values
361 
362   @return An error code: 0 - success, otherwise - failure
363 
364   @ref Basic
365 **/
366 int CeedQFunctionSetContext(CeedQFunction qf, void *ctx, size_t ctxsize) {
367   qf->ctx = ctx;
368   qf->ctxsize = ctxsize;
369   return 0;
370 }
371 
372 /**
373   @brief Apply the action of a CeedQFunction
374 
375   @param qf      CeedQFunction
376   @param Q       Number of quadrature points
377   @param[in] u   Array of input data arrays
378   @param[out] v  Array of output data arrays
379 
380   @return An error code: 0 - success, otherwise - failure
381 
382   @ref Advanced
383 **/
384 int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
385                        CeedVector* u, CeedVector* v) {
386   int ierr;
387   if (!qf->Apply)
388     return CeedError(qf->ceed, 1, "Backend does not support QFunctionApply");
389   if (Q % qf->vlength)
390     return CeedError(qf->ceed, 2,
391                      "Number of quadrature points %d must be a multiple of %d",
392                      Q, qf->vlength);
393   ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr);
394   return 0;
395 }
396 
397 /**
398   @brief Get the CeedQFunctionFields of a CeedQFunction
399 
400   @param qf                 CeedQFunction
401   @param[out] inputfields   Variable to store inputfields
402   @param[out] outputfields  Variable to store outputfields
403 
404   @return An error code: 0 - success, otherwise - failure
405 
406   @ref Advanced
407 **/
408 
409 int CeedQFunctionGetFields(CeedQFunction qf,
410                            CeedQFunctionField* *inputfields,
411                            CeedQFunctionField* *outputfields) {
412   if (inputfields) *inputfields = qf->inputfields;
413   if (outputfields) *outputfields = qf->outputfields;
414   return 0;
415 }
416 
417 /**
418   @brief Get the name of a CeedQFunctionField
419 
420   @param qffield         CeedQFunctionField
421   @param[out] fieldname  Variable to store the field name
422 
423   @return An error code: 0 - success, otherwise - failure
424 
425   @ref Advanced
426 **/
427 
428 int CeedQFunctionFieldGetName(CeedQFunctionField qffield,
429                               char* *fieldname) {
430   *fieldname = (char *)qffield->fieldname;
431   return 0;
432 }
433 
434 /**
435   @brief Get the number of components of a CeedQFunctionField
436 
437   @param qffield         CeedQFunctionField
438   @param[out] numcomp    Variable to store the number of components
439 
440   @return An error code: 0 - success, otherwise - failure
441 
442   @ref Advanced
443 **/
444 
445 int CeedQFunctionFieldGetNumComponents(CeedQFunctionField qffield,
446                                        CeedInt *numcomp) {
447   *numcomp = qffield->ncomp;
448   return 0;
449 }
450 
451 /**
452   @brief Get the CeedEvalMode of a CeedQFunctionField
453 
454   @param qffield         CeedQFunctionField
455   @param[out] vec        Variable to store the number of components
456 
457   @return An error code: 0 - success, otherwise - failure
458 
459   @ref Advanced
460 **/
461 
462 int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qffield,
463                                   CeedEvalMode *emode) {
464   *emode = qffield->emode;
465   return 0;
466 }
467 
468 /**
469   @brief Destroy a CeedQFunction
470 
471   @param qf CeedQFunction to destroy
472 
473   @return An error code: 0 - success, otherwise - failure
474 
475   @ref Basic
476 **/
477 int CeedQFunctionDestroy(CeedQFunction *qf) {
478   int ierr;
479 
480   if (!*qf || --(*qf)->refcount > 0) return 0;
481   // Backend destroy
482   if ((*qf)->Destroy) {
483     ierr = (*qf)->Destroy(*qf); CeedChk(ierr);
484   }
485   // Free fields
486   for (int i=0; i<(*qf)->numinputfields; i++) {
487     ierr = CeedFree(&(*(*qf)->inputfields[i]).fieldname); CeedChk(ierr);
488     ierr = CeedFree(&(*qf)->inputfields[i]); CeedChk(ierr);
489   }
490   for (int i=0; i<(*qf)->numoutputfields; i++) {
491     ierr = CeedFree(&(*(*qf)->outputfields[i]).fieldname); CeedChk(ierr);
492     ierr = CeedFree(&(*qf)->outputfields[i]); CeedChk(ierr);
493   }
494   ierr = CeedFree(&(*qf)->inputfields); CeedChk(ierr);
495   ierr = CeedFree(&(*qf)->outputfields); CeedChk(ierr);
496 
497   ierr = CeedFree(&(*qf)->focca); CeedChk(ierr);
498   ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr);
499   ierr = CeedFree(qf); CeedChk(ierr);
500   return 0;
501 }
502 
503 /// @}
504