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