xref: /libCEED/include/ceed.h (revision 2774d5cba221882c6b4100a2009c80f26d4c6f5c)
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 user and utility components of 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: Types of Functions
30 /// @subsection Types of Functions
31 ///   libCEED provides three different header files depending upon the type of
32 ///   functions a user requires.
33 /// @section Utility Utility Functions
34 ///   These functions are intended general utilities that may be useful to
35 ///   libCEED developers and users. These functions can generally be found in "ceed.h".
36 /// @section Basic User Functions
37 ///   These functions are intended to be used by general users of the libCEED
38 ///   interface. These functions can generally be found in "ceed.h".
39 /// @section Advanced Backend Developer Functions
40 ///   These functions are intended to be used by backend developers of the
41 ///   libCEED interface. These functions can generally be found in "ceed-backend.h".
42 /// @section Developer Frontend Developer Functions
43 ///   These functions are intended to be used by frontend developers of the
44 ///   libCEED interface. These functions can generally be found in "ceed-impl.h".
45 
46 /**
47   CEED_EXTERN is used in this header to denote all publicly visible symbols.
48 
49   No other file should declare publicly visible symbols, thus it should never be
50   used outside ceed.h.
51  */
52 #ifdef __cplusplus
53 #  define CEED_EXTERN extern "C"
54 #else
55 #  define CEED_EXTERN extern
56 #endif
57 
58 #include <assert.h>
59 #include <stdint.h>
60 #include <stddef.h>
61 #include <stdarg.h>
62 #include <stdio.h>
63 #include <stdbool.h>
64 
65 // We can discuss ways to avoid forcing these to be compile-time decisions, but let's leave that for later.
66 /// Integer type, used for indexing
67 /// @ingroup Ceed
68 typedef int32_t CeedInt;
69 /// Scalar (floating point) type
70 /// @ingroup Ceed
71 typedef double CeedScalar;
72 
73 /// Library context created by CeedInit()
74 /// @ingroup Ceed
75 typedef struct Ceed_private *Ceed;
76 /// Non-blocking Ceed interfaces return a CeedRequest.
77 /// To perform an operation immediately, pass \ref CEED_REQUEST_IMMEDIATE instead.
78 /// @ingroup Ceed
79 typedef struct CeedRequest_private *CeedRequest;
80 /// Handle for vectors over the field \ref CeedScalar
81 /// @ingroup CeedVector
82 typedef struct CeedVector_private *CeedVector;
83 /// Handle for object describing restriction to elements
84 /// @ingroup CeedElemRestriction
85 typedef struct CeedElemRestriction_private *CeedElemRestriction;
86 /// Handle for object describing discrete finite element evaluations
87 /// @ingroup CeedBasis
88 typedef struct CeedBasis_private *CeedBasis;
89 /// Handle for object describing functions evaluated independently at quadrature points
90 /// @ingroup CeedQFunction
91 typedef struct CeedQFunction_private *CeedQFunction;
92 /// Handle for object describing FE-type operators acting on vectors
93 ///
94 /// Given an element restriction \f$E\f$, basis evaluator \f$B\f$, and quadrature function
95 /// \f$f\f$, a CeedOperator expresses operations of the form
96 ///   $$ E^T B^T f(B E u) $$
97 /// acting on the vector \f$u\f$.
98 /// @ingroup CeedOperator
99 typedef struct CeedOperator_private *CeedOperator;
100 
101 /// Handle for object describing CeedQFunction fields
102 /// @ingroup CeedQFunction
103 typedef struct CeedQFunctionField_private *CeedQFunctionField;
104 /// Handle for object describing CeedOperator fields
105 /// @ingroup CeedOperator
106 typedef struct CeedOperatorField_private *CeedOperatorField;
107 
108 CEED_EXTERN int CeedInit(const char *resource, Ceed *ceed);
109 CEED_EXTERN int CeedDestroy(Ceed *ceed);
110 
111 CEED_EXTERN int CeedErrorImpl(Ceed, const char *, int, const char *, int,
112                               const char *, ...);
113 /// Raise an error on ceed object
114 ///
115 /// @param ceed Ceed library context or NULL
116 /// @param ecode Error code (int)
117 /// @param ... printf-style format string followed by arguments as needed
118 ///
119 /// @ingroup Ceed
120 /// @sa CeedSetErrorHandler()
121 #define CeedError(ceed, ecode, ...)                                     \
122   CeedErrorImpl((ceed), __FILE__, __LINE__, __func__, (ecode), __VA_ARGS__)
123 
124 /// Specify memory type
125 ///
126 /// Many Ceed interfaces take or return pointers to memory.  This enum is used to
127 /// specify where the memory being provided or requested must reside.
128 /// @ingroup Ceed
129 typedef enum {
130   /// Memory resides on the host
131   CEED_MEM_HOST,
132   /// Memory resides on a device (corresponding to \ref Ceed resource)
133   CEED_MEM_DEVICE,
134 } CeedMemType;
135 
136 /// Conveys ownership status of arrays passed to Ceed interfaces.
137 /// @ingroup Ceed
138 typedef enum {
139   /// Implementation will copy the values and not store the passed pointer.
140   CEED_COPY_VALUES,
141   /// Implementation can use and modify the data provided by the user, but does
142   /// not take ownership.
143   CEED_USE_POINTER,
144   /// Implementation takes ownership of the pointer and will free using
145   /// CeedFree() when done using it.  The user should not assume that the
146   /// pointer remains valid after ownership has been transferred.  Note that
147   /// arrays allocated using C++ operator new or other allocators cannot
148   /// generally be freed using CeedFree().  CeedFree() is capable of freeing any
149   /// memory that can be freed using free(3).
150   CEED_OWN_POINTER,
151 } CeedCopyMode;
152 
153 CEED_EXTERN int CeedVectorCreate(Ceed ceed, CeedInt len, CeedVector *vec);
154 CEED_EXTERN int CeedVectorSetArray(CeedVector vec, CeedMemType mtype,
155                                    CeedCopyMode cmode, CeedScalar *array);
156 CEED_EXTERN int CeedVectorSetValue(CeedVector vec, CeedScalar value);
157 CEED_EXTERN int CeedVectorSyncArray(CeedVector vec, CeedMemType mtype);
158 CEED_EXTERN int CeedVectorGetArray(CeedVector vec, CeedMemType mtype,
159                                    CeedScalar **array);
160 CEED_EXTERN int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mtype,
161                                        const CeedScalar **array);
162 CEED_EXTERN int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array);
163 CEED_EXTERN int CeedVectorRestoreArrayRead(CeedVector vec,
164     const CeedScalar **array);
165 CEED_EXTERN int CeedVectorView(CeedVector vec, const char *fpfmt, FILE *stream);
166 CEED_EXTERN int CeedVectorGetLength(CeedVector vec, CeedInt *length);
167 CEED_EXTERN int CeedVectorDestroy(CeedVector *vec);
168 
169 CEED_EXTERN CeedRequest *const CEED_REQUEST_IMMEDIATE;
170 CEED_EXTERN CeedRequest *const CEED_REQUEST_ORDERED;
171 CEED_EXTERN int CeedRequestWait(CeedRequest *req);
172 
173 /// Argument for CeedOperatorSetField that vector is collocated with
174 /// quadrature points, used with qfunction eval mode CEED_EVAL_NONE
175 /// or CEED_EVAL_INTERP only, not with CEED_EVAL_GRAD, CEED_EVAL_DIV,
176 /// or CEED_EVAL_CURL
177 /// @ingroup CeedBasis
178 CEED_EXTERN CeedBasis CEED_BASIS_COLLOCATED;
179 
180 /// Argument for CeedOperatorSetField to use active input or output
181 /// @ingroup CeedVector
182 CEED_EXTERN CeedVector CEED_VECTOR_ACTIVE;
183 
184 /// Argument for CeedOperatorSetField to use no vector, used with
185 /// qfunction input with eval mode CEED_EVAL_WEIGHTS
186 /// @ingroup CeedVector
187 CEED_EXTERN CeedVector CEED_VECTOR_NONE;
188 
189 /// Denotes whether a linear transformation or its transpose should be applied
190 /// @ingroup CeedBasis
191 typedef enum {
192   /// Apply the linear transformation
193   CEED_NOTRANSPOSE,
194   /// Apply the transpose
195   CEED_TRANSPOSE
196 } CeedTransposeMode;
197 
198 CEED_EXTERN int CeedElemRestrictionCreate(Ceed ceed, CeedInt nelem,
199     CeedInt elemsize, CeedInt ndof, CeedInt ncomp, CeedMemType mtype,
200     CeedCopyMode cmode,
201     const CeedInt *indices, CeedElemRestriction *rstr);
202 CEED_EXTERN int CeedElemRestrictionCreateIdentity(Ceed ceed, CeedInt nelem,
203     CeedInt elemsize, CeedInt ndof, CeedInt ncomp, CeedElemRestriction *rstr);
204 CEED_EXTERN int CeedElemRestrictionCreateBlocked(Ceed ceed, CeedInt nelem,
205     CeedInt elemsize, CeedInt blksize, CeedInt ndof, CeedInt ncomp,
206     CeedMemType mtype,
207     CeedCopyMode cmode, const CeedInt *indices, CeedElemRestriction *rstr);
208 CEED_EXTERN int CeedElemRestrictionApply(CeedElemRestriction rstr,
209     CeedTransposeMode tmode, CeedTransposeMode lmode, CeedVector u,
210     CeedVector ru, CeedRequest *request);
211 CEED_EXTERN int CeedElemRestrictionDestroy(CeedElemRestriction *rstr);
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 CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P);
280 CEED_EXTERN int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q);
281 CEED_EXTERN int CeedBasisApply(CeedBasis basis, CeedInt nelem,
282                                CeedTransposeMode tmode,
283                                CeedEvalMode emode, CeedVector u, CeedVector v);
284 CEED_EXTERN int CeedBasisDestroy(CeedBasis *basis);
285 
286 CEED_EXTERN int CeedGaussQuadrature(CeedInt Q, CeedScalar *qref1d,
287                                     CeedScalar *qweight1d);
288 CEED_EXTERN int CeedLobattoQuadrature(CeedInt Q, CeedScalar *qref1d,
289                                       CeedScalar *qweight1d);
290 CEED_EXTERN int CeedQRFactorization(CeedScalar *mat, CeedScalar *tau, CeedInt m,
291                                     CeedInt n);
292 
293 /// Handle for the object describing the user CeedQFunction
294 ///
295 /// @param ctx - user-defined context set using CeedQFunctionSetContext() or NULL
296 ///
297 /// @param Q - number of quadrature points at which to evaluate
298 ///
299 /// @param in - array of pointers to each input argument in the order provided
300 ///             by the user in CeedQFunctionAddInput().  Each array has shape
301 ///             `[dim, ncomp, Q]` where `dim` is the geometric dimension for
302 ///             \ref CEED_EVAL_GRAD (`dim=1` for \ref CEED_EVAL_INTERP) and
303 ///             `ncomp` is the number of field components (`ncomp=1` for
304 ///             scalar fields).  This results in indexing the `i`th input at
305 ///             quadarture point `j` as `in[i][(d*ncomp + c)*Q + j]`.
306 ///
307 /// @param out - array of pointers to each output array in the order provided
308 ///              using CeedQFunctionAddOutput().  The shapes are as above for
309 ///              \a in.
310 ///
311 /// @return 0 on success, nonzero for failure.
312 ///
313 /// @ingroup CeedQFunction
314 typedef int (*CeedQFunctionUser)(void *ctx, const CeedInt Q,
315                                  const CeedScalar *const *in, CeedScalar *const *out);
316 
317 CEED_EXTERN int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength,
318     CeedQFunctionUser f, const char *focca, CeedQFunction *qf);
319 CEED_EXTERN int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname,
320                                       CeedInt ncomp, CeedEvalMode emode);
321 CEED_EXTERN int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname,
322                                        CeedInt ncomp, CeedEvalMode emode);
323 CEED_EXTERN int CeedQFunctionSetContext(CeedQFunction qf, void *ctx,
324                                         size_t ctxsize);
325 CEED_EXTERN int CeedQFunctionApply(CeedQFunction qf, CeedInt Q,
326                                    CeedVector *u, CeedVector *v);
327 CEED_EXTERN int CeedQFunctionDestroy(CeedQFunction *qf);
328 
329 CEED_EXTERN int CeedOperatorCreate(Ceed ceed, CeedQFunction qf,
330                                    CeedQFunction dqf, CeedQFunction dqfT,
331                                    CeedOperator *op);
332 CEED_EXTERN int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op);
333 CEED_EXTERN int CeedOperatorSetField(CeedOperator op, const char *fieldname,
334                                      CeedElemRestriction r,
335                                      CeedTransposeMode lmode, CeedBasis b,
336                                      CeedVector v);
337 CEED_EXTERN int CeedCompositeOperatorAddSub(CeedOperator compositeop,
338     CeedOperator subop);
339 CEED_EXTERN int CeedOperatorApply(CeedOperator op, CeedVector in,
340                                   CeedVector out, CeedRequest *request);
341 CEED_EXTERN int CeedOperatorDestroy(CeedOperator *op);
342 
343 /**
344   @brief Return integer power
345 
346   @param[in] base   The base to exponentiate
347   @param[in] power  The power to raise the base to
348 
349   @return base^power
350 
351   @ref Utility
352 **/
353 static inline CeedInt CeedIntPow(CeedInt base, CeedInt power) {
354   CeedInt result = 1;
355   while (power) {
356     if (power & 1) result *= base;
357     power >>= 1;
358     base *= base;
359   }
360   return result;
361 }
362 
363 /**
364   @brief Return mimimum of two integers
365 
366   @param[in] a  The first integer to compare
367   @param[in] b  The second integer to compare
368 
369   @return The minimum of the two integers
370 
371   @ref Utility
372 **/
373 static inline CeedInt CeedIntMin(CeedInt a, CeedInt b) { return a < b ? a : b; }
374 
375 #endif
376