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