xref: /libCEED/rust/libceed-sys/c-src/include/ceed.h (revision 4ce2993fee6e7bc9b86526ee90098d0dc489fc60)
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 /// @file
18 /// Public header for libCEED
19 #ifndef _ceed_h
20 #define _ceed_h
21 
22 /// @defgroup Ceed Ceed: core components
23 /// @defgroup CeedVector CeedVector: storing and manipulating vectors
24 /// @defgroup CeedElemRestriction CeedElemRestriction: restriction from vectors to elements
25 /// @defgroup CeedBasis CeedBasis: fully discrete finite element-like objects
26 /// @defgroup CeedQFunction CeedQFunction: independent operations at quadrature points
27 /// @defgroup CeedOperator CeedOperator: composed FE-type operations on vectors
28 ///
29 /// @page FunctionCategories libCEED Function Categories
30 /// @section Utility Utility Functions
31 ///   These functions are intended general utilities that may be useful to libCEED developers and users.
32 /// @section Basic User Functions
33 ///   These functions are intended to be used by general users of the libCEED interface.
34 /// @section Advanced Backend Developer Functions
35 ///   These functions are intended to be used by backend developers of the libCEED interface.
36 /// @section Developer Frontend Developer Functions
37 ///   These functions are intended to be used by frontend developers of the libCEED interface.
38 
39 /**
40   CEED_EXTERN is used in this header to denote all publicly visible symbols.
41 
42   No other file should declare publicly visible symbols, thus it should never be
43   used outside ceed.h.
44  */
45 #ifdef __cplusplus
46 #  define CEED_EXTERN extern "C"
47 #else
48 #  define CEED_EXTERN extern
49 #endif
50 
51 #include <assert.h>
52 #include <stdint.h>
53 #include <stddef.h>
54 #include <stdarg.h>
55 #include <stdio.h>
56 #include <stdbool.h>
57 
58 // We can discuss ways to avoid forcing these to be compile-time decisions, but let's leave that for later.
59 /// Integer type, used for indexing
60 /// @ingroup Ceed
61 typedef int32_t CeedInt;
62 /// Scalar (floating point) type
63 /// @ingroup Ceed
64 typedef double CeedScalar;
65 
66 /// Library context created by CeedInit()
67 /// @ingroup Ceed
68 typedef struct Ceed_private *Ceed;
69 /// Non-blocking Ceed interfaces return a CeedRequest.
70 /// To perform an operation immediately, pass \ref CEED_REQUEST_IMMEDIATE instead.
71 /// @ingroup Ceed
72 typedef struct CeedRequest_private *CeedRequest;
73 /// Handle for vectors over the field \ref CeedScalar
74 /// @ingroup CeedVector
75 typedef struct CeedVector_private *CeedVector;
76 /// Handle for object describing restriction to elements
77 /// @ingroup CeedElemRestriction
78 typedef struct CeedElemRestriction_private *CeedElemRestriction;
79 /// Handle for object describing discrete finite element evaluations
80 /// @ingroup CeedBasis
81 typedef struct CeedBasis_private *CeedBasis;
82 /// Handle for object describing functions evaluated independently at quadrature points
83 /// @ingroup CeedQFunction
84 typedef struct CeedQFunction_private *CeedQFunction;
85 /// Handle for object describing FE-type operators acting on vectors
86 ///
87 /// Given an element restriction \f$E\f$, basis evaluator \f$B\f$, and quadrature function
88 /// \f$f\f$, a CeedOperator expresses operations of the form
89 ///   $$ E^T B^T f(B E u) $$
90 /// acting on the vector \f$u\f$.
91 typedef struct CeedOperator_private *CeedOperator;
92 
93 CEED_EXTERN int CeedRegister(const char *prefix,
94                              int (*init)(const char *, Ceed), unsigned int priority);
95 
96 CEED_EXTERN int CeedInit(const char *resource, Ceed *ceed);
97 CEED_EXTERN int CeedGetDelegate(Ceed ceed, Ceed *delegate);
98 CEED_EXTERN int CeedSetDelegate(Ceed ceed, Ceed *delegate);
99 CEED_EXTERN int CeedErrorReturn(Ceed, const char *, int, const char *, int,
100                                 const char *, va_list);
101 CEED_EXTERN int CeedErrorAbort(Ceed, const char *, int, const char *, int,
102                                const char *, va_list);
103 CEED_EXTERN int CeedErrorExit(Ceed, const char *, int, const char *, int,
104                               const char *, va_list);
105 CEED_EXTERN int CeedSetErrorHandler(Ceed ceed,
106                                     int (eh)(Ceed, const char *, int, const char *,
107                                         int, const char *, va_list));
108 CEED_EXTERN int CeedErrorImpl(Ceed, const char *, int, const char *, int,
109                               const char *, ...);
110 /// Raise an error on ceed object
111 ///
112 /// @param ceed Ceed library context or NULL
113 /// @param ecode Error code (int)
114 /// @param ... printf-style format string followed by arguments as needed
115 ///
116 /// @ingroup Ceed
117 /// @sa CeedSetErrorHandler()
118 #define CeedError(ceed, ecode, ...)                                     \
119   CeedErrorImpl((ceed), __FILE__, __LINE__, __func__, (ecode), __VA_ARGS__)
120 CEED_EXTERN int CeedGetData(Ceed ceed, void* *data);
121 CEED_EXTERN int CeedDestroy(Ceed *ceed);
122 
123 /// Specify memory type
124 ///
125 /// Many Ceed interfaces take or return pointers to memory.  This enum is used to
126 /// specify where the memory being provided or requested must reside.
127 /// @ingroup Ceed
128 typedef enum {
129   /// Memory resides on the host
130   CEED_MEM_HOST,
131   /// Memory resides on a device (corresponding to \ref Ceed resource)
132   CEED_MEM_DEVICE,
133 } CeedMemType;
134 
135 /// Conveys ownership status of arrays passed to Ceed interfaces.
136 /// @ingroup Ceed
137 typedef enum {
138   /// Implementation will copy the values and not store the passed pointer.
139   CEED_COPY_VALUES,
140   /// Implementation can use and modify the data provided by the user, but does
141   /// not take ownership.
142   CEED_USE_POINTER,
143   /// Implementation takes ownership of the pointer and will free using
144   /// CeedFree() when done using it.  The user should not assume that the
145   /// pointer remains valid after ownership has been transferred.  Note that
146   /// arrays allocated using C++ operator new or other allocators cannot
147   /// generally be freed using CeedFree().  CeedFree() is capable of freeing any
148   /// memory that can be freed using free(3).
149   CEED_OWN_POINTER,
150 } CeedCopyMode;
151 
152 CEED_EXTERN int CeedVectorCreate(Ceed ceed, CeedInt len, CeedVector *vec);
153 CEED_EXTERN int CeedVectorSetArray(CeedVector vec, CeedMemType mtype,
154                                    CeedCopyMode cmode, CeedScalar *array);
155 CEED_EXTERN int CeedVectorSetValue(CeedVector vec, CeedScalar value);
156 CEED_EXTERN int CeedVectorGetArray(CeedVector vec, CeedMemType mtype,
157                                    CeedScalar **array);
158 CEED_EXTERN int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mtype,
159                                        const CeedScalar **array);
160 CEED_EXTERN int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array);
161 CEED_EXTERN int CeedVectorRestoreArrayRead(CeedVector vec,
162     const CeedScalar **array);
163 CEED_EXTERN int CeedVectorView(CeedVector vec, const char *fpfmt, FILE *stream);
164 CEED_EXTERN int CeedVectorGetCeed(CeedVector vec, Ceed *ceed);
165 CEED_EXTERN int CeedVectorGetLength(CeedVector vec, CeedInt *length);
166 CEED_EXTERN int CeedVectorGetState(CeedVector vec, uint64_t *state);
167 CEED_EXTERN int CeedVectorGetData(CeedVector vec, void* *data);
168 CEED_EXTERN int CeedVectorDestroy(CeedVector *vec);
169 
170 CEED_EXTERN CeedRequest *const CEED_REQUEST_IMMEDIATE;
171 CEED_EXTERN CeedRequest *const CEED_REQUEST_ORDERED;
172 CEED_EXTERN int CeedRequestWait(CeedRequest *req);
173 
174 /// Argument for CeedOperatorSetField that vector is collocated with
175 /// quadrature points, used with qfunction eval mode CEED_EVAL_NONE
176 /// or CEED_EVAL_INTERP only, not with CEED_EVAL_GRAD, CEED_EVAL_DIV,
177 /// or CEED_EVAL_CURL
178 /// @ingroup CeedBasis
179 CEED_EXTERN CeedBasis CEED_BASIS_COLLOCATED;
180 
181 /// Argument for CeedOperatorSetField to use active input or output
182 /// @ingroup CeedVector
183 CEED_EXTERN CeedVector CEED_VECTOR_ACTIVE;
184 
185 /// Argument for CeedOperatorSetField to use no vector, used with
186 /// qfunction input with eval mode CEED_EVAL_WEIGHTS
187 /// @ingroup CeedVector
188 CEED_EXTERN CeedVector CEED_VECTOR_NONE;
189 
190 /// Denotes whether a linear transformation or its transpose should be applied
191 /// @ingroup CeedBasis
192 typedef enum {
193   /// Apply the linear transformation
194   CEED_NOTRANSPOSE,
195   /// Apply the transpose
196   CEED_TRANSPOSE
197 } CeedTransposeMode;
198 
199 CEED_EXTERN int CeedElemRestrictionCreate(Ceed ceed, CeedInt nelem,
200     CeedInt elemsize, CeedInt ndof, CeedInt ncomp, CeedMemType mtype,
201     CeedCopyMode cmode,
202     const CeedInt *indices, CeedElemRestriction *rstr);
203 CEED_EXTERN int CeedElemRestrictionCreateIdentity(Ceed ceed, CeedInt nelem,
204     CeedInt elemsize, CeedInt ndof, CeedInt ncomp, CeedElemRestriction *rstr);
205 CEED_EXTERN int CeedElemRestrictionCreateBlocked(Ceed ceed, CeedInt nelem,
206     CeedInt elemsize, CeedInt blksize, CeedInt ndof, CeedInt ncomp,
207     CeedMemType mtype,
208     CeedCopyMode cmode, const CeedInt *indices, CeedElemRestriction *rstr);
209 CEED_EXTERN int CeedElemRestrictionCreateVector(CeedElemRestriction rstr,
210     CeedVector *lvec,
211     CeedVector *evec);
212 CEED_EXTERN int CeedElemRestrictionApply(CeedElemRestriction rstr,
213     CeedTransposeMode tmode, CeedTransposeMode lmode, CeedVector u,
214     CeedVector ru, CeedRequest *request);
215 CEED_EXTERN int CeedElemRestrictionGetCeed(CeedElemRestriction rstr, Ceed *ceed);
216 CEED_EXTERN int CeedElemRestrictionGetNumElements(CeedElemRestriction rstr,
217     CeedInt *numelem);
218 CEED_EXTERN int CeedElemRestrictionGetElementSize(CeedElemRestriction rstr,
219     CeedInt *elemsize);
220 CEED_EXTERN int CeedElemRestrictionGetNumDoF(CeedElemRestriction rstr,
221     CeedInt *numdof);
222 CEED_EXTERN int CeedElemRestrictionGetNumComponents(CeedElemRestriction rstr,
223     CeedInt *numcomp);
224 CEED_EXTERN int CeedElemRestrictionGetNumBlocks(CeedElemRestriction rstr,
225     CeedInt *numblk);
226 CEED_EXTERN int CeedElemRestrictionGetBlockSize(CeedElemRestriction rstr,
227     CeedInt *blksize);
228 CEED_EXTERN int CeedElemRestrictionGetData(CeedElemRestriction rstr,
229     void* *data);
230 CEED_EXTERN int CeedElemRestrictionDestroy(CeedElemRestriction *rstr);
231 
232 // The formalism here is that we have the structure
233 //   \int_\Omega v^T f_0(u, \nabla u, qdata) + (\nabla v)^T f_1(u, \nabla u, qdata)
234 // where gradients are with respect to the reference element.
235 
236 /// Basis evaluation mode
237 ///
238 /// Modes can be bitwise ORed when passing to most functions.
239 /// @ingroup CeedBasis
240 typedef enum {
241   /// Perform no evaluation (either because there is no data or it is already at
242   /// quadrature points)
243   CEED_EVAL_NONE   = 0,
244   /// Interpolate from nodes to quadrature points
245   CEED_EVAL_INTERP = 1,
246   /// Evaluate gradients at quadrature points from input in a nodal basis
247   CEED_EVAL_GRAD   = 2,
248   /// Evaluate divergence at quadrature points from input in a nodal basis
249   CEED_EVAL_DIV    = 4,
250   /// Evaluate curl at quadrature points from input in a nodal basis
251   CEED_EVAL_CURL   = 8,
252   /// Using no input, evaluate quadrature weights on the reference element
253   CEED_EVAL_WEIGHT = 16,
254 } CeedEvalMode;
255 
256 /// Type of quadrature; also used for location of nodes
257 /// @ingroup CeedBasis
258 typedef enum {
259   /// Gauss-Legendre quadrature
260   CEED_GAUSS = 0,
261   /// Gauss-Legendre-Lobatto quadrature
262   CEED_GAUSS_LOBATTO = 1,
263 } CeedQuadMode;
264 
265 /// Type of basis shape to create non-tensor H1 element basis
266 ///
267 /// Dimension can be extracted with bitwise AND
268 /// (CeedElemTopology & 2**(dim + 2)) == TRUE
269 /// @ingroup CeedBasis
270 typedef enum {
271   /// Line
272   CEED_LINE = 1 << 16 | 0,
273   /// Triangle - 2D shape
274   CEED_TRIANGLE = 2 << 16 | 1,
275   /// Quadralateral - 2D shape
276   CEED_QUAD = 2 << 16 | 2,
277   /// Tetrahedron - 3D shape
278   CEED_TET = 3 << 16 | 3,
279   /// Pyramid - 3D shape
280   CEED_PYRAMID = 3 << 16 | 4,
281   /// Prism - 3D shape
282   CEED_PRISM = 3 << 16 | 5,
283   /// Hexehedron - 3D shape
284   CEED_HEX = 3 << 16 | 6,
285 } CeedElemTopology;
286 
287 CEED_EXTERN int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim,
288     CeedInt ncomp, CeedInt P, CeedInt Q, CeedQuadMode qmode, CeedBasis *basis);
289 CEED_EXTERN int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt ncomp,
290                                         CeedInt P1d, CeedInt Q1d, const CeedScalar *interp1d, const CeedScalar *grad1d,
291                                         const CeedScalar *qref1d, const CeedScalar *qweight1d, CeedBasis *basis);
292 CEED_EXTERN int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo,
293                                   CeedInt ncomp,
294                                   CeedInt ndof, CeedInt nqpts,
295                                   const CeedScalar *interp, const CeedScalar *grad,
296                                   const CeedScalar *qref, const CeedScalar *qweight, CeedBasis *basis);
297 CEED_EXTERN int CeedBasisView(CeedBasis basis, FILE *stream);
298 CEED_EXTERN int CeedQRFactorization(CeedScalar *mat, CeedScalar *tau, CeedInt m,
299                                     CeedInt n);
300 CEED_EXTERN int CeedBasisGetCollocatedGrad(CeedBasis basis,
301     CeedScalar *colograd1d);
302 CEED_EXTERN int CeedBasisApply(CeedBasis basis, CeedInt nelem,
303                                CeedTransposeMode tmode,
304                                CeedEvalMode emode, const CeedScalar *u, CeedScalar *v);
305 CEED_EXTERN int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed);
306 CEED_EXTERN int CeedBasisGetTensorStatus(CeedBasis basis, bool *tensor);
307 CEED_EXTERN int CeedBasisGetDimension(CeedBasis basis, CeedInt *dim);
308 CEED_EXTERN int CeedBasisGetNumComponents(CeedBasis basis, CeedInt *numcomp);
309 CEED_EXTERN int CeedBasisGetNumNodes1D(CeedBasis basis, CeedInt *P1d);
310 CEED_EXTERN int CeedBasisGetNumQuadraturePoints1D(CeedBasis basis, CeedInt *Q1d);
311 CEED_EXTERN int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P);
312 CEED_EXTERN int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q);
313 CEED_EXTERN int CeedBasisGetQRef(CeedBasis basis, CeedScalar* *qref);
314 CEED_EXTERN int CeedBasisGetQWeights(CeedBasis basis, CeedScalar* *qweight);
315 CEED_EXTERN int CeedBasisGetInterp(CeedBasis basis, CeedScalar* *interp);
316 CEED_EXTERN int CeedBasisGetGrad(CeedBasis basis, CeedScalar* *grad);
317 CEED_EXTERN int CeedBasisGetData(CeedBasis basis, void* *data);
318 CEED_EXTERN int CeedBasisDestroy(CeedBasis *basis);
319 
320 CEED_EXTERN int CeedGaussQuadrature(CeedInt Q, CeedScalar *qref1d,
321                                     CeedScalar *qweight1d);
322 CEED_EXTERN int CeedLobattoQuadrature(CeedInt Q, CeedScalar *qref1d,
323                                       CeedScalar *qweight1d);
324 CEED_EXTERN int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim);
325 
326 CEED_EXTERN int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength,
327     int (*f)(void *ctx, CeedInt nq, const CeedScalar *const *u,
328              CeedScalar *const *v), const char *focca, CeedQFunction *qf);
329 CEED_EXTERN int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname,
330                                       CeedInt ncomp, CeedEvalMode emode);
331 CEED_EXTERN int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
332                                        CeedInt ncomp, CeedEvalMode emode);
333 CEED_EXTERN int CeedQFunctionSetContext(CeedQFunction qf, void *ctx,
334                                         size_t ctxsize);
335 CEED_EXTERN int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
336                                    const CeedScalar *const *u,
337                                    CeedScalar *const *v);
338 CEED_EXTERN int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed);
339 CEED_EXTERN int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength);
340 CEED_EXTERN int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinputfields,
341                                         CeedInt *numoutputfields);
342 CEED_EXTERN int CeedQFunctionGetFOCCA(CeedQFunction qf, char* *focca);
343 CEED_EXTERN int CeedQFunctionGetContextSize(CeedQFunction qf, size_t *ctxsize);
344 CEED_EXTERN int CeedQFunctionGetContext(CeedQFunction qf, void* *ctx);
345 CEED_EXTERN int CeedQFunctionGetData(CeedQFunction qf, void* *data);
346 CEED_EXTERN int CeedQFunctionDestroy(CeedQFunction *qf);
347 
348 CEED_EXTERN int CeedOperatorCreate(Ceed ceed, CeedQFunction qf,
349                                    CeedQFunction dqf, CeedQFunction dqfT,
350                                    CeedOperator *op);
351 CEED_EXTERN int CeedOperatorSetField(CeedOperator op, const char *fieldname,
352                                      CeedElemRestriction r, CeedBasis b,
353                                      CeedVector v);
354 CEED_EXTERN int CeedOperatorApply(CeedOperator op, CeedVector in,
355                                   CeedVector out, CeedRequest *request);
356 CEED_EXTERN int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed);
357 CEED_EXTERN int CeedOperatorGetNumElements(CeedOperator op, CeedInt *numelem);
358 CEED_EXTERN int CeedOperatorGetNumQuadraturePoints(CeedOperator op,
359                                                    CeedInt *numqpts);
360 CEED_EXTERN int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *numargs);
361 CEED_EXTERN int CeedOperatorGetSetupStatus(CeedOperator op, bool *setupdone);
362 CEED_EXTERN int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf);
363 CEED_EXTERN int CeedOperatorGetData(CeedOperator op, void* *data);
364 CEED_EXTERN int CeedOperatorSetSetupDone(CeedOperator op);
365 CEED_EXTERN int CeedOperatorDestroy(CeedOperator *op);
366 
367 /**
368   @brief Return integer power
369 
370   @param[in] base   The base to exponentiate
371   @param[in] power  The power to raise the base to
372 
373   @return base^power
374 
375   @ref Utility
376 **/
377 static inline CeedInt CeedIntPow(CeedInt base, CeedInt power) {
378   CeedInt result = 1;
379   while (power) {
380     if (power & 1) result *= base;
381     power >>= 1;
382     base *= base;
383   }
384   return result;
385 }
386 
387 /**
388   @brief Return mimimum of two integers
389 
390   @param[in] a  The first integer to compare
391   @param[in] b  The second integer to compare
392 
393   @return The minimum of the two integers
394 
395   @ref Utility
396 **/
397 static inline CeedInt CeedIntMin(CeedInt a, CeedInt b) { return a < b ? a : b; }
398 
399 #endif
400