xref: /libCEED/rust/libceed-sys/c-src/include/ceed.h (revision 44951fc6642471e2b443ede8ee5bcff8115d816d)
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 /**
23   CEED_EXTERN is used in this header to denote all publicly visible symbols.
24 
25   No other file should declare publicly visible symbols, thus it should never be
26   used outside ceed.h.
27  */
28 #ifdef __cplusplus
29 #  define CEED_EXTERN extern "C"
30 #else
31 #  define CEED_EXTERN extern
32 #endif
33 
34 #include <assert.h>
35 #include <stdint.h>
36 #include <stddef.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 
40 // We can discuss ways to avoid forcing these to be compile-time decisions, but let's leave that for later.
41 /// Integer type, used for indexing
42 /// @ingroup Ceed
43 typedef int32_t CeedInt;
44 /// Scalar (floating point) type
45 /// @ingroup Ceed
46 typedef double CeedScalar;
47 
48 /// Library context created by CeedInit()
49 /// @ingroup Ceed
50 typedef struct Ceed_private *Ceed;
51 /// Non-blocking Ceed interfaces return a CeedRequest.
52 /// To perform an operation immediately, pass \ref CEED_REQUEST_IMMEDIATE instead.
53 /// @ingroup Ceed
54 typedef struct CeedRequest_private *CeedRequest;
55 /// Handle for vectors over the field \ref CeedScalar
56 /// @ingroup CeedVector
57 typedef struct CeedVector_private *CeedVector;
58 /// Handle for object describing restriction to elements
59 /// @ingroup CeedElemRestriction
60 typedef struct CeedElemRestriction_private *CeedElemRestriction;
61 /// Handle for object describing discrete finite element evaluations
62 /// @ingroup CeedBasis
63 typedef struct CeedBasis_private *CeedBasis;
64 /// Handle for object describing functions evaluated independently at quadrature points
65 /// @ingroup CeedQFunction
66 typedef struct CeedQFunction_private *CeedQFunction;
67 /// Handle for object describing FE-type operators acting on vectors
68 ///
69 /// Given an element restriction \f$E\f$, basis evaluator \f$B\f$, and quadrature function
70 /// \f$f\f$, a CeedOperator expresses operations of the form
71 ///   $$ E^T B^T f(B E u) $$
72 /// acting on the vector \f$u\f$.
73 typedef struct CeedOperator_private *CeedOperator;
74 
75 CEED_EXTERN int CeedRegister(const char *prefix,
76                              int (*init)(const char *, Ceed), unsigned int priority);
77 
78 CEED_EXTERN int CeedInit(const char *resource, Ceed *ceed);
79 CEED_EXTERN int CeedErrorReturn(Ceed, const char *, int, const char *, int,
80                                 const char *, va_list);
81 CEED_EXTERN int CeedErrorAbort(Ceed, const char *, int, const char *, int,
82                                const char *, va_list);
83 CEED_EXTERN int CeedSetErrorHandler(Ceed ceed,
84                                     int (eh)(Ceed, const char *, int, const char *,
85                                         int, const char *, va_list));
86 CEED_EXTERN int CeedErrorImpl(Ceed, const char *, int, const char *, int,
87                               const char *, ...);
88 /// Raise an error on ceed object
89 ///
90 /// @param ceed Ceed library context or NULL
91 /// @param ecode Error code (int)
92 /// @param ... printf-style format string followed by arguments as needed
93 ///
94 /// @ingroup Ceed
95 /// @sa CeedSetErrorHandler()
96 #define CeedError(ceed, ecode, ...)                                     \
97   CeedErrorImpl((ceed), __FILE__, __LINE__, __func__, (ecode), __VA_ARGS__)
98 CEED_EXTERN int CeedDestroy(Ceed *ceed);
99 
100 /// Specify memory type
101 ///
102 /// Many Ceed interfaces take or return pointers to memory.  This enum is used to
103 /// specify where the memory being provided or requested must reside.
104 /// @ingroup Ceed
105 typedef enum {
106   /// Memory resides on the host
107   CEED_MEM_HOST,
108   /// Memory resides on a device (corresponding to \ref Ceed resource)
109   CEED_MEM_DEVICE,
110 } CeedMemType;
111 
112 /// Conveys ownership status of arrays passed to Ceed interfaces.
113 /// @ingroup Ceed
114 typedef enum {
115   /// Implementation will copy the values and not store the passed pointer.
116   CEED_COPY_VALUES,
117   /// Implementation can use and modify the data provided by the user, but does
118   /// not take ownership.
119   CEED_USE_POINTER,
120   /// Implementation takes ownership of the pointer and will free using
121   /// CeedFree() when done using it.  The user should not assume that the
122   /// pointer remains valid after ownership has been transferred.  Note that
123   /// arrays allocated using C++ operator new or other allocators cannot
124   /// generally be freed using CeedFree().  CeedFree() is capable of freeing any
125   /// memory that can be freed using free(3).
126   CEED_OWN_POINTER,
127 } CeedCopyMode;
128 
129 CEED_EXTERN int CeedVectorCreate(Ceed ceed, CeedInt len, CeedVector *vec);
130 CEED_EXTERN int CeedVectorSetArray(CeedVector vec, CeedMemType mtype,
131                                    CeedCopyMode cmode, CeedScalar *array);
132 CEED_EXTERN int CeedVectorGetArray(CeedVector vec, CeedMemType mtype,
133                                    CeedScalar **array);
134 CEED_EXTERN int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mtype,
135                                        const CeedScalar **array);
136 CEED_EXTERN int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array);
137 CEED_EXTERN int CeedVectorRestoreArrayRead(CeedVector vec,
138     const CeedScalar **array);
139 CEED_EXTERN int CeedVectorView(CeedVector vec, const char *fpfmt, FILE *stream);
140 CEED_EXTERN int CeedVectorDestroy(CeedVector *vec);
141 
142 CEED_EXTERN CeedRequest *const CEED_REQUEST_IMMEDIATE;
143 CEED_EXTERN CeedRequest *const CEED_REQUEST_ORDERED;
144 CEED_EXTERN int CeedRequestWait(CeedRequest *req);
145 
146 /// Denotes whether a linear transformation or its transpose should be applied
147 typedef enum {
148   /// Apply the linear transformation
149   CEED_NOTRANSPOSE,
150   /// Apply the transpose
151   CEED_TRANSPOSE
152 } CeedTransposeMode;
153 
154 CEED_EXTERN int CeedElemRestrictionCreate(Ceed ceed, CeedInt nelements,
155     CeedInt esize, CeedInt ndof, CeedMemType mtype, CeedCopyMode cmode,
156     const CeedInt *indices, CeedElemRestriction *r);
157 
158 CEED_EXTERN int CeedElemRestrictionCreateBlocked(Ceed ceed, CeedInt nelements,
159     CeedInt esize, CeedInt blocksize, CeedMemType mtype, CeedCopyMode cmode,
160     CeedInt *blkindices, CeedElemRestriction *r);
161 CEED_EXTERN int CeedElemRestrictionApply(CeedElemRestriction r,
162     CeedTransposeMode tmode, CeedInt ncomp, CeedTransposeMode lmode, CeedVector u,
163     CeedVector ru, CeedRequest *request);
164 CEED_EXTERN int CeedElemRestrictionDestroy(CeedElemRestriction *r);
165 
166 // The formalism here is that we have the structure
167 //   \int_\Omega v^T f_0(u, \nabla u, qdata) + (\nabla v)^T f_1(u, \nabla u, qdata)
168 // where gradients are with respect to the reference element.
169 
170 /// Basis evaluation mode
171 ///
172 /// Modes can be bitwise ORed when passing to most functions.
173 /// @ingroup CeedBasis
174 typedef enum {
175   /// Perform no evaluation (either because there is no data or it is already at
176   /// quadrature points)
177   CEED_EVAL_NONE   = 0,
178   /// Interpolate from nodes to quadrature points
179   CEED_EVAL_INTERP = 1,
180   /// Evaluate gradients at quadrature points from input in a nodal basis
181   CEED_EVAL_GRAD   = 2,
182   /// Evaluate divergence at quadrature points from input in a nodal basis
183   CEED_EVAL_DIV    = 4,
184   /// Evaluate curl at quadrature points from input in a nodal basis
185   CEED_EVAL_CURL   = 8,
186   /// Using no input, evaluate quadrature weights on the reference element
187   CEED_EVAL_WEIGHT = 16,
188 } CeedEvalMode;
189 
190 /// Type of quadrature; also used for location of nodes
191 /// @ingroup CeedBasis
192 typedef enum {
193   /// Gauss-Legendre quadrature
194   CEED_GAUSS = 0,
195   /// Gauss-Legendre-Lobatto quadrature
196   CEED_GAUSS_LOBATTO = 1,
197 } CeedQuadMode;
198 
199 CEED_EXTERN int CeedBasisCreateTensorH1Lagrange(Ceed ceed, CeedInt dim,
200     CeedInt ndof, CeedInt P, CeedInt Q, CeedQuadMode qmode, CeedBasis *basis);
201 CEED_EXTERN int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt ndof,
202                                         CeedInt P1d, CeedInt Q1d, const CeedScalar *interp1d, const CeedScalar *grad1d,
203                                         const CeedScalar *qref1d, const CeedScalar *qweight1d, CeedBasis *basis);
204 CEED_EXTERN int CeedBasisView(CeedBasis basis, FILE *stream);
205 CEED_EXTERN int CeedQRFactorization(CeedScalar *mat, CeedScalar *tau, CeedInt m, CeedInt n);
206 CEED_EXTERN int CeedBasisGetColocatedGrad(CeedBasis basis, CeedScalar *colograd1d);
207 CEED_EXTERN int CeedBasisApply(CeedBasis basis, CeedTransposeMode tmode,
208                                CeedEvalMode emode, const CeedScalar *u, CeedScalar *v);
209 CEED_EXTERN int CeedBasisGetNumNodes(CeedBasis basis, CeedInt *P);
210 CEED_EXTERN int CeedBasisGetNumQuadraturePoints(CeedBasis basis, CeedInt *Q);
211 CEED_EXTERN int CeedBasisDestroy(CeedBasis *basis);
212 
213 CEED_EXTERN int CeedGaussQuadrature(CeedInt Q, CeedScalar *qref1d,
214                                     CeedScalar *qweight1d);
215 CEED_EXTERN int CeedLobattoQuadrature(CeedInt Q, CeedScalar *qref1d,
216                                       CeedScalar *qweight1d);
217 
218 CEED_EXTERN int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength,
219     CeedInt nfields, size_t qdatasize, CeedEvalMode inmode, CeedEvalMode outmode,
220     int (*f)(void *, void *, CeedInt, const CeedScalar *const *,
221              CeedScalar *const *),
222     const char *focca, CeedQFunction *qf);
223 CEED_EXTERN int CeedQFunctionSetContext(CeedQFunction qf, void *ctx,
224                                         size_t ctxsize);
225 CEED_EXTERN int CeedQFunctionApply(CeedQFunction qf, void *qdata, CeedInt Q,
226                                    const CeedScalar *const *u,
227                                    CeedScalar *const *v);
228 CEED_EXTERN int CeedQFunctionDestroy(CeedQFunction *qf);
229 
230 CEED_EXTERN int CeedOperatorCreate(Ceed ceed, CeedElemRestriction r,
231                                    CeedBasis b, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT,
232                                    CeedOperator *op);
233 CEED_EXTERN int CeedOperatorGetQData(CeedOperator op, CeedVector *qdata);
234 CEED_EXTERN int CeedOperatorApply(CeedOperator op, CeedVector qdata,
235                                   CeedVector ustate, CeedVector residual, CeedRequest *request);
236 CEED_EXTERN int CeedOperatorDestroy(CeedOperator *op);
237 
238 static inline CeedInt CeedPowInt(CeedInt base, CeedInt power) {
239   CeedInt result = 1;
240   while (power) {
241     if (power & 1) result *= base;
242     power >>= 1;
243     base *= base;
244   }
245   return result;
246 }
247 
248 #endif
249