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