xref: /libCEED/include/ceed.h (revision e6e9a80e88c01b297acbea0e00348303555d9a96)
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 
57 // We can discuss ways to avoid forcing these to be compile-time decisions, but let's leave that for later.
58 /// Integer type, used for indexing
59 /// @ingroup Ceed
60 typedef int32_t CeedInt;
61 /// Scalar (floating point) type
62 /// @ingroup Ceed
63 typedef double CeedScalar;
64 
65 /// Library context created by CeedInit()
66 /// @ingroup Ceed
67 typedef struct Ceed_private *Ceed;
68 /// Non-blocking Ceed interfaces return a CeedRequest.
69 /// To perform an operation immediately, pass \ref CEED_REQUEST_IMMEDIATE instead.
70 /// @ingroup Ceed
71 typedef struct CeedRequest_private *CeedRequest;
72 /// Handle for vectors over the field \ref CeedScalar
73 /// @ingroup CeedVector
74 typedef struct CeedVector_private *CeedVector;
75 /// Handle for object describing restriction to elements
76 /// @ingroup CeedElemRestriction
77 typedef struct CeedElemRestriction_private *CeedElemRestriction;
78 /// Handle for object describing discrete finite element evaluations
79 /// @ingroup CeedBasis
80 typedef struct CeedBasis_private *CeedBasis;
81 /// Handle for object describing functions evaluated independently at quadrature points
82 /// @ingroup CeedQFunction
83 typedef struct CeedQFunction_private *CeedQFunction;
84 /// Handle for object describing FE-type operators acting on vectors
85 ///
86 /// Given an element restriction \f$E\f$, basis evaluator \f$B\f$, and quadrature function
87 /// \f$f\f$, a CeedOperator expresses operations of the form
88 ///   $$ E^T B^T f(B E u) $$
89 /// acting on the vector \f$u\f$.
90 typedef struct CeedOperator_private *CeedOperator;
91 
92 CEED_EXTERN int CeedRegister(const char *prefix,
93                              int (*init)(const char *, Ceed), unsigned int priority);
94 
95 CEED_EXTERN int CeedInit(const char *resource, Ceed *ceed);
96 CEED_EXTERN int CeedGetDelegate(Ceed ceed, Ceed *delegate);
97 CEED_EXTERN int CeedErrorReturn(Ceed, const char *, int, const char *, int,
98                                 const char *, va_list);
99 CEED_EXTERN int CeedErrorAbort(Ceed, const char *, int, const char *, int,
100                                const char *, va_list);
101 CEED_EXTERN int CeedErrorExit(Ceed, const char *, int, const char *, int,
102                               const char *, va_list);
103 CEED_EXTERN int CeedSetErrorHandler(Ceed ceed,
104                                     int (eh)(Ceed, const char *, int, const char *,
105                                         int, const char *, va_list));
106 CEED_EXTERN int CeedErrorImpl(Ceed, const char *, int, const char *, int,
107                               const char *, ...);
108 /// Raise an error on ceed object
109 ///
110 /// @param ceed Ceed library context or NULL
111 /// @param ecode Error code (int)
112 /// @param ... printf-style format string followed by arguments as needed
113 ///
114 /// @ingroup Ceed
115 /// @sa CeedSetErrorHandler()
116 #define CeedError(ceed, ecode, ...)                                     \
117   CeedErrorImpl((ceed), __FILE__, __LINE__, __func__, (ecode), __VA_ARGS__)
118 CEED_EXTERN int CeedDestroy(Ceed *ceed);
119 
120 /// Specify memory type
121 ///
122 /// Many Ceed interfaces take or return pointers to memory.  This enum is used to
123 /// specify where the memory being provided or requested must reside.
124 /// @ingroup Ceed
125 typedef enum {
126   /// Memory resides on the host
127   CEED_MEM_HOST,
128   /// Memory resides on a device (corresponding to \ref Ceed resource)
129   CEED_MEM_DEVICE,
130 } CeedMemType;
131 
132 /// Conveys ownership status of arrays passed to Ceed interfaces.
133 /// @ingroup Ceed
134 typedef enum {
135   /// Implementation will copy the values and not store the passed pointer.
136   CEED_COPY_VALUES,
137   /// Implementation can use and modify the data provided by the user, but does
138   /// not take ownership.
139   CEED_USE_POINTER,
140   /// Implementation takes ownership of the pointer and will free using
141   /// CeedFree() when done using it.  The user should not assume that the
142   /// pointer remains valid after ownership has been transferred.  Note that
143   /// arrays allocated using C++ operator new or other allocators cannot
144   /// generally be freed using CeedFree().  CeedFree() is capable of freeing any
145   /// memory that can be freed using free(3).
146   CEED_OWN_POINTER,
147 } CeedCopyMode;
148 
149 CEED_EXTERN int CeedVectorCreate(Ceed ceed, CeedInt len, CeedVector *vec);
150 CEED_EXTERN int CeedVectorSetArray(CeedVector vec, CeedMemType mtype,
151                                    CeedCopyMode cmode, CeedScalar *array);
152 CEED_EXTERN int CeedVectorSetValue(CeedVector vec, CeedScalar value);
153 CEED_EXTERN int CeedVectorGetArray(CeedVector vec, CeedMemType mtype,
154                                    CeedScalar **array);
155 CEED_EXTERN int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mtype,
156                                        const CeedScalar **array);
157 CEED_EXTERN int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array);
158 CEED_EXTERN int CeedVectorRestoreArrayRead(CeedVector vec,
159     const CeedScalar **array);
160 CEED_EXTERN int CeedVectorView(CeedVector vec, const char *fpfmt, FILE *stream);
161 CEED_EXTERN int CeedVectorGetLength(CeedVector vec, CeedInt *length);
162 CEED_EXTERN int CeedVectorDestroy(CeedVector *vec);
163 
164 CEED_EXTERN CeedRequest *const CEED_REQUEST_IMMEDIATE;
165 CEED_EXTERN CeedRequest *const CEED_REQUEST_ORDERED;
166 CEED_EXTERN int CeedRequestWait(CeedRequest *req);
167 
168 /// Argument for CeedOperatorSetField that vector is collocated with
169 /// quadrature points, used with qfunction eval mode CEED_EVAL_NONE
170 /// or CEED_EVAL_INTERP only, not with CEED_EVAL_GRAD, CEED_EVAL_DIV,
171 /// or CEED_EVAL_CURL
172 /// @ingroup CeedBasis
173 CEED_EXTERN CeedBasis CEED_BASIS_COLLOCATED;
174 
175 /// Argument for CeedOperatorSetField to use active input or output
176 /// @ingroup CeedVector
177 CEED_EXTERN CeedVector CEED_VECTOR_ACTIVE;
178 
179 /// Argument for CeedOperatorSetField to use no vector, used with
180 /// qfunction input with eval mode CEED_EVAL_WEIGHTS
181 /// @ingroup CeedVector
182 CEED_EXTERN CeedVector CEED_VECTOR_NONE;
183 
184 /// Denotes whether a linear transformation or its transpose should be applied
185 /// @ingroup CeedBasis
186 typedef enum {
187   /// Apply the linear transformation
188   CEED_NOTRANSPOSE,
189   /// Apply the transpose
190   CEED_TRANSPOSE
191 } CeedTransposeMode;
192 
193 CEED_EXTERN int CeedElemRestrictionCreate(Ceed ceed, CeedInt nelem,
194     CeedInt elemsize, CeedInt ndof, CeedInt ncomp, CeedMemType mtype,
195     CeedCopyMode cmode,
196     const CeedInt *indices, CeedElemRestriction *r);
197 CEED_EXTERN int CeedElemRestrictionCreateIdentity(Ceed ceed, CeedInt nelem,
198     CeedInt elemsize, CeedInt ndof, CeedInt ncomp, CeedElemRestriction *r);
199 CEED_EXTERN int CeedElemRestrictionCreateBlocked(Ceed ceed, CeedInt nelem,
200     CeedInt elemsize, CeedInt blksize, CeedInt ndof, CeedInt ncomp,
201     CeedMemType mtype,
202     CeedCopyMode cmode, const CeedInt *indices, CeedElemRestriction *r);
203 CEED_EXTERN int CeedElemRestrictionCreateVector(CeedElemRestriction r,
204     CeedVector *lvec,
205     CeedVector *evec);
206 CEED_EXTERN int CeedElemRestrictionGetNumElements(CeedElemRestriction r,
207     CeedInt *numelements);
208 CEED_EXTERN int CeedElemRestrictionApply(CeedElemRestriction r,
209     CeedTransposeMode tmode, CeedTransposeMode lmode, CeedVector u,
210     CeedVector ru, CeedRequest *request);
211 CEED_EXTERN int CeedElemRestrictionDestroy(CeedElemRestriction *r);
212 
213 // The formalism here is that we have the structure
214 //   \int_\Omega v^T f_0(u, \nabla u, qdata) + (\nabla v)^T f_1(u, \nabla u, qdata)
215 // where gradients are with respect to the reference element.
216 
217 /// Basis evaluation mode
218 ///
219 /// Modes can be bitwise ORed when passing to most functions.
220 /// @ingroup CeedBasis
221 typedef enum {
222   /// Perform no evaluation (either because there is no data or it is already at
223   /// quadrature points)
224   CEED_EVAL_NONE   = 0,
225   /// Interpolate from nodes to quadrature points
226   CEED_EVAL_INTERP = 1,
227   /// Evaluate gradients at quadrature points from input in a nodal basis
228   CEED_EVAL_GRAD   = 2,
229   /// Evaluate divergence at quadrature points from input in a nodal basis
230   CEED_EVAL_DIV    = 4,
231   /// Evaluate curl at quadrature points from input in a nodal basis
232   CEED_EVAL_CURL   = 8,
233   /// Using no input, evaluate quadrature weights on the reference element
234   CEED_EVAL_WEIGHT = 16,
235 } CeedEvalMode;
236 
237 /// Type of quadrature; also used for location of nodes
238 /// @ingroup CeedBasis
239 typedef enum {
240   /// Gauss-Legendre quadrature
241   CEED_GAUSS = 0,
242   /// Gauss-Legendre-Lobatto quadrature
243   CEED_GAUSS_LOBATTO = 1,
244 } CeedQuadMode;
245 
246 /// Type of basis shape to create non-tensor H1 element basis
247 ///
248 /// Dimension can be extracted with bitwise AND
249 /// (CeedElemTopology & 2**(dim + 2)) == TRUE
250 /// @ingroup CeedBasis
251 typedef enum {
252   /// Line
253   CEED_LINE = 1 << 16 | 0,
254   /// Triangle - 2D shape
255   CEED_TRIANGLE = 2 << 16 | 1,
256   /// Quadralateral - 2D shape
257   CEED_QUAD = 2 << 16 | 2,
258   /// Tetrahedron - 3D shape
259   CEED_TET = 3 << 16 | 3,
260   /// Pyramid - 3D shape
261   CEED_PYRAMID = 3 << 16 | 4,
262   /// Prism - 3D shape
263   CEED_PRISM = 3 << 16 | 5,
264   /// Hexehedron - 3D shape
265   CEED_HEX = 3 << 16 | 6,
266 } CeedElemTopology;
267 
268 CEED_EXTERN int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim,
269     CeedInt ncomp, CeedInt P, CeedInt Q, CeedQuadMode qmode, CeedBasis *basis);
270 CEED_EXTERN int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt ncomp,
271                                         CeedInt P1d, CeedInt Q1d, const CeedScalar *interp1d, const CeedScalar *grad1d,
272                                         const CeedScalar *qref1d, const CeedScalar *qweight1d, CeedBasis *basis);
273 CEED_EXTERN int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo,
274                                   CeedInt ncomp,
275                                   CeedInt ndof, CeedInt nqpts,
276                                   const CeedScalar *interp, const CeedScalar *grad,
277                                   const CeedScalar *qref, const CeedScalar *qweight, CeedBasis *basis);
278 CEED_EXTERN int CeedBasisView(CeedBasis basis, FILE *stream);
279 CEED_EXTERN int CeedQRFactorization(CeedScalar *mat, CeedScalar *tau, CeedInt m,
280                                     CeedInt n);
281 CEED_EXTERN int CeedBasisGetCollocatedGrad(CeedBasis basis,
282     CeedScalar *colograd1d);
283 CEED_EXTERN int CeedBasisApply(CeedBasis basis, CeedInt nelem,
284                                CeedTransposeMode tmode,
285                                CeedEvalMode emode, const CeedScalar *u, CeedScalar *v);
286 CEED_EXTERN int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P);
287 CEED_EXTERN int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q);
288 CEED_EXTERN int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim);
289 CEED_EXTERN int CeedBasisDestroy(CeedBasis *basis);
290 
291 CEED_EXTERN int CeedGaussQuadrature(CeedInt Q, CeedScalar *qref1d,
292                                     CeedScalar *qweight1d);
293 CEED_EXTERN int CeedLobattoQuadrature(CeedInt Q, CeedScalar *qref1d,
294                                       CeedScalar *qweight1d);
295 
296 CEED_EXTERN int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength,
297     int (*f)(void *ctx, CeedInt nq, const CeedScalar *const *u,
298              CeedScalar *const *v), const char *focca, CeedQFunction *qf);
299 CEED_EXTERN int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname,
300                                       CeedInt ncomp, CeedEvalMode emode);
301 CEED_EXTERN int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
302                                        CeedInt ncomp, CeedEvalMode emode);
303 CEED_EXTERN int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput,
304                                         CeedInt *numoutput);
305 CEED_EXTERN int CeedQFunctionSetContext(CeedQFunction qf, void *ctx,
306                                         size_t ctxsize);
307 CEED_EXTERN int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
308                                    const CeedScalar *const *u,
309                                    CeedScalar *const *v);
310 CEED_EXTERN int CeedQFunctionDestroy(CeedQFunction *qf);
311 
312 CEED_EXTERN int CeedOperatorCreate(Ceed ceed, CeedQFunction qf,
313                                    CeedQFunction dqf, CeedQFunction dqfT,
314                                    CeedOperator *op);
315 CEED_EXTERN int CeedOperatorSetField(CeedOperator op, const char *fieldname,
316                                      CeedElemRestriction r, CeedBasis b,
317                                      CeedVector v);
318 CEED_EXTERN int CeedOperatorApply(CeedOperator op, CeedVector in,
319                                   CeedVector out, CeedRequest *request);
320 CEED_EXTERN int CeedOperatorDestroy(CeedOperator *op);
321 
322 /**
323   @brief Return integer power
324 
325   @param[in] base   The base to exponentiate
326   @param[in] power  The power to raise the base to
327 
328   @return base^power
329 
330   @ref Utility
331 **/
332 static inline CeedInt CeedIntPow(CeedInt base, CeedInt power) {
333   CeedInt result = 1;
334   while (power) {
335     if (power & 1) result *= base;
336     power >>= 1;
337     base *= base;
338   }
339   return result;
340 }
341 
342 /**
343   @brief Return mimimum of two integers
344 
345   @param[in] a  The first integer to compare
346   @param[in] b  The second integer to compare
347 
348   @return The minimum of the two integers
349 
350   @ref Utility
351 **/
352 static inline CeedInt CeedIntMin(CeedInt a, CeedInt b) { return a < b ? a : b; }
353 
354 #endif
355