xref: /libCEED/examples/fluids/navierstokes.h (revision e8d902ca03f8f9018a8d626b82d32185d32586a7)
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 #ifndef navierstokes_h
18 #define navierstokes_h
19 
20 #include <ceed.h>
21 #include <petscdm.h>
22 #include <petscdmplex.h>
23 #include <petscsys.h>
24 #include <petscts.h>
25 #include <stdbool.h>
26 
27 // -----------------------------------------------------------------------------
28 // PETSc Macros
29 // -----------------------------------------------------------------------------
30 #if PETSC_VERSION_LT(3,14,0)
31 #  define DMPlexGetClosureIndices(a,b,c,d,e,f,g,h,i) DMPlexGetClosureIndices(a,b,c,d,f,g,i)
32 #  define DMPlexRestoreClosureIndices(a,b,c,d,e,f,g,h,i) DMPlexRestoreClosureIndices(a,b,c,d,f,g,i)
33 #endif
34 
35 #if PETSC_VERSION_LT(3,14,0)
36 #  define DMAddBoundary(a,b,c,d,e,f,g,h,i,j,k,l,m,n) DMAddBoundary(a,b,c,e,h,i,j,k,f,g,m)
37 #elif PETSC_VERSION_LT(3,16,0)
38 #  define DMAddBoundary(a,b,c,d,e,f,g,h,i,j,k,l,m,n) DMAddBoundary(a,b,c,e,h,i,j,k,l,f,g,m)
39 #else
40 #  define DMAddBoundary(a,b,c,d,e,f,g,h,i,j,k,l,m,n) DMAddBoundary(a,b,c,d,f,g,h,i,j,k,l,m,n)
41 #endif
42 
43 // -----------------------------------------------------------------------------
44 // Enums
45 // -----------------------------------------------------------------------------
46 // Translate PetscMemType to CeedMemType
47 static inline CeedMemType MemTypeP2C(PetscMemType mem_type) {
48   return PetscMemTypeDevice(mem_type) ? CEED_MEM_DEVICE : CEED_MEM_HOST;
49 }
50 
51 // Advection - Wind Options
52 typedef enum {
53   WIND_ROTATION    = 0,
54   WIND_TRANSLATION = 1,
55 } WindType;
56 static const char *const WindTypes[] = {
57   "rotation",
58   "translation",
59   "WindType", "WIND_", NULL
60 };
61 
62 // Advection - Bubble Types
63 typedef enum {
64   BUBBLE_SPHERE   = 0, // dim=3
65   BUBBLE_CYLINDER = 1, // dim=2
66 } BubbleType;
67 static const char *const BubbleTypes[] = {
68   "sphere",
69   "cylinder",
70   "BubbleType", "BUBBLE_", NULL
71 };
72 
73 // Advection - Bubble Continuity Types
74 typedef enum {
75   BUBBLE_CONTINUITY_SMOOTH     = 0,  // Original continuous, smooth shape
76   BUBBLE_CONTINUITY_BACK_SHARP = 1,  // Discontinuous, sharp back half shape
77   BUBBLE_CONTINUITY_THICK      = 2,  // Define a finite thickness
78 } BubbleContinuityType;
79 static const char *const BubbleContinuityTypes[] = {
80   "smooth",
81   "back_sharp",
82   "thick",
83   "BubbleContinuityType", "BUBBLE_CONTINUITY_", NULL
84 };
85 
86 // Euler - test cases
87 typedef enum {
88   EULER_TEST_ISENTROPIC_VORTEX = 0,
89   EULER_TEST_1 = 1,
90   EULER_TEST_2 = 2,
91   EULER_TEST_3 = 3,
92   EULER_TEST_4 = 4,
93 } EulerTestType;
94 static const char *const EulerTestTypes[] = {
95   "isentropic_vortex",
96   "test_1",
97   "test_2",
98   "test_3",
99   "test_4",
100   "EulerTestType", "EULER_TEST_", NULL
101 };
102 
103 // Stabilization methods
104 typedef enum {
105   STAB_NONE = 0,
106   STAB_SU   = 1, // Streamline Upwind
107   STAB_SUPG = 2, // Streamline Upwind Petrov-Galerkin
108 } StabilizationType;
109 static const char *const StabilizationTypes[] = {
110   "none",
111   "SU",
112   "SUPG",
113   "StabilizationType", "STAB_", NULL
114 };
115 
116 // -----------------------------------------------------------------------------
117 // Structs
118 // -----------------------------------------------------------------------------
119 // Structs declarations
120 typedef struct AppCtx_private    *AppCtx;
121 typedef struct CeedData_private  *CeedData;
122 typedef struct User_private      *User;
123 typedef struct Units_private     *Units;
124 typedef struct SimpleBC_private  *SimpleBC;
125 typedef struct SetupContext_     *SetupContext;
126 typedef struct Physics_private   *Physics;
127 
128 // Application context from user command line options
129 struct AppCtx_private {
130   // libCEED arguments
131   char              ceed_resource[PETSC_MAX_PATH_LEN]; // libCEED backend
132   PetscInt          degree;
133   PetscInt          q_extra;
134   // Post-processing arguments
135   PetscInt          output_freq;
136   PetscInt          viz_refine;
137   PetscInt          cont_steps;
138   char              output_dir[PETSC_MAX_PATH_LEN];
139   // Problem type arguments
140   PetscFunctionList problems;
141   char              problem_name[PETSC_MAX_PATH_LEN];
142   // Test mode arguments
143   PetscBool         test_mode;
144   PetscScalar       test_tol;
145   char              file_path[PETSC_MAX_PATH_LEN];
146 };
147 
148 // libCEED data struct
149 struct CeedData_private {
150   CeedVector           x_coord, q_data;
151   CeedQFunctionContext setup_context, dc_context, advection_context,
152                        euler_context;
153   CeedQFunction        qf_setup_vol, qf_ics, qf_rhs_vol, qf_ifunction_vol,
154                        qf_setup_sur, qf_apply_sur;
155   CeedBasis            basis_x, basis_xc, basis_q, basis_x_sur, basis_xc_sur,
156                        basis_q_sur;
157   CeedElemRestriction  elem_restr_x, elem_restr_q, elem_restr_qd_i;
158   CeedOperator         op_setup_vol, op_ics;
159 };
160 
161 // PETSc user data
162 struct User_private {
163   MPI_Comm     comm;
164   DM           dm;
165   DM           dm_viz;
166   Mat          interp_viz;
167   Ceed         ceed;
168   Units        units;
169   Vec          M;
170   Physics      phys;
171   AppCtx       app_ctx;
172   CeedVector   q_ceed, q_dot_ceed, g_ceed;
173   CeedOperator op_rhs_vol, op_rhs, op_ifunction_vol, op_ifunction;
174 };
175 
176 // Units
177 struct Units_private {
178   // fundamental units
179   PetscScalar meter;
180   PetscScalar kilogram;
181   PetscScalar second;
182   PetscScalar Kelvin;
183   // derived units
184   PetscScalar Pascal;
185   PetscScalar J_per_kg_K;
186   PetscScalar m_per_squared_s;
187   PetscScalar W_per_m_K;
188   PetscScalar Joule;
189 };
190 
191 // Boundary conditions
192 struct SimpleBC_private {
193   PetscInt  num_wall, num_slip[3];
194   PetscInt  walls[6], slips[3][6];
195   PetscBool user_bc;
196 };
197 
198 // Initial conditions
199 #ifndef setup_context_struct
200 #define setup_context_struct
201 typedef struct SetupContext_ *SetupContext;
202 struct SetupContext_ {
203   CeedScalar theta0;
204   CeedScalar thetaC;
205   CeedScalar P0;
206   CeedScalar N;
207   CeedScalar cv;
208   CeedScalar cp;
209   CeedScalar Rd;
210   CeedScalar g;
211   CeedScalar rc;
212   CeedScalar lx;
213   CeedScalar ly;
214   CeedScalar lz;
215   CeedScalar center[3];
216   CeedScalar dc_axis[3];
217   CeedScalar wind[3];
218   CeedScalar time;
219   int wind_type;              // See WindType: 0=ROTATION, 1=TRANSLATION
220   int bubble_type;            // See BubbleType: 0=SPHERE, 1=CYLINDER
221   int bubble_continuity_type; // See BubbleContinuityType: 0=SMOOTH, 1=BACK_SHARP 2=THICK
222 };
223 #endif
224 
225 // DENSITY_CURRENT
226 #ifndef dc_context_struct
227 #define dc_context_struct
228 typedef struct DCContext_ *DCContext;
229 struct DCContext_ {
230   CeedScalar lambda;
231   CeedScalar mu;
232   CeedScalar k;
233   CeedScalar cv;
234   CeedScalar cp;
235   CeedScalar g;
236   CeedScalar Rd;
237   int stabilization; // See StabilizationType: 0=none, 1=SU, 2=SUPG
238 };
239 #endif
240 
241 // EULER_VORTEX
242 #ifndef euler_context_struct
243 #define euler_context_struct
244 typedef struct EulerContext_ *EulerContext;
245 struct EulerContext_ {
246   CeedScalar center[3];
247   CeedScalar curr_time;
248   CeedScalar vortex_strength;
249   CeedScalar mean_velocity[3];
250   int euler_test;
251   bool implicit;
252 };
253 #endif
254 
255 // ADVECTION and ADVECTION2D
256 #ifndef advection_context_struct
257 #define advection_context_struct
258 typedef struct AdvectionContext_ *AdvectionContext;
259 struct AdvectionContext_ {
260   CeedScalar CtauS;
261   CeedScalar strong_form;
262   CeedScalar E_wind;
263   bool implicit;
264   int stabilization; // See StabilizationType: 0=none, 1=SU, 2=SUPG
265 };
266 #endif
267 
268 // Struct that contains all enums and structs used for the physics of all problems
269 struct Physics_private {
270   DCContext            dc_ctx;
271   EulerContext         euler_ctx;
272   AdvectionContext     advection_ctx;
273   WindType             wind_type;
274   BubbleType           bubble_type;
275   BubbleContinuityType bubble_continuity_type;
276   EulerTestType        euler_test;
277   StabilizationType    stab;
278   PetscBool            implicit;
279   PetscBool            has_curr_time;
280   PetscBool            has_neumann;
281 };
282 
283 // Problem specific data
284 // *INDENT-OFF*
285 typedef struct {
286   CeedInt           dim, q_data_size_vol, q_data_size_sur;
287   CeedQFunctionUser setup_vol, setup_sur, ics, apply_vol_rhs, apply_vol_ifunction,
288                     apply_sur;
289   const char        *setup_vol_loc, *setup_sur_loc, *ics_loc,
290                     *apply_vol_rhs_loc, *apply_vol_ifunction_loc, *apply_sur_loc;
291   bool              non_zero_time;
292   PetscErrorCode    (*bc)(PetscInt, PetscReal, const PetscReal[], PetscInt,
293                           PetscScalar[], void *);
294   PetscErrorCode    (*setup_ctx)(Ceed, CeedData, AppCtx, SetupContext, Physics);
295   PetscErrorCode    (*bc_func)(DM, SimpleBC, Physics, void *);
296   PetscErrorCode    (*print_info)(Physics, SetupContext, AppCtx);
297 } ProblemData;
298 // *INDENT-ON*
299 
300 // -----------------------------------------------------------------------------
301 // Set up problems
302 // -----------------------------------------------------------------------------
303 // Set up function for each problem
304 extern PetscErrorCode NS_DENSITY_CURRENT(ProblemData *problem, void *setup_ctx,
305     void *ctx);
306 
307 extern PetscErrorCode NS_EULER_VORTEX(ProblemData *problem, void *setup_ctx,
308                                       void *ctx);
309 
310 extern PetscErrorCode NS_ADVECTION(ProblemData *problem, void *setup_ctx,
311                                    void *ctx);
312 
313 extern PetscErrorCode NS_ADVECTION2D(ProblemData *problem, void *setup_ctx,
314                                      void *ctx);
315 
316 // Set up context for each problem
317 extern PetscErrorCode SetupContext_DENSITY_CURRENT(Ceed ceed,
318     CeedData ceed_data, AppCtx app_ctx, SetupContext setup_ctx, Physics phys);
319 
320 extern PetscErrorCode SetupContext_EULER_VORTEX(Ceed ceed, CeedData ceed_data,
321     AppCtx app_ctx, SetupContext setup_ctx, Physics phys);
322 
323 extern PetscErrorCode SetupContext_ADVECTION(Ceed ceed, CeedData ceed_data,
324     AppCtx app_ctx, SetupContext setup_ctx, Physics phys);
325 
326 extern PetscErrorCode SetupContext_ADVECTION2D(Ceed ceed, CeedData ceed_data,
327     AppCtx app_ctx, SetupContext setup_ctx, Physics phys);
328 
329 // Boundary condition function for each problem
330 extern PetscErrorCode BC_DENSITY_CURRENT(DM dm, SimpleBC bc, Physics phys,
331     void *setup_ctx);
332 
333 extern PetscErrorCode BC_EULER_VORTEX(DM dm, SimpleBC bc, Physics phys,
334                                       void *setup_ctx);
335 
336 extern PetscErrorCode BC_ADVECTION(DM dm, SimpleBC bc, Physics phys,
337                                    void *setup_ctx);
338 
339 extern PetscErrorCode BC_ADVECTION2D(DM dm, SimpleBC bc, Physics phys,
340                                      void *setup_ctx);
341 
342 // Print function for each problem
343 extern PetscErrorCode PRINT_DENSITY_CURRENT(Physics phys,
344     SetupContext setup_ctx, AppCtx app_ctx);
345 
346 extern PetscErrorCode PRINT_EULER_VORTEX(Physics phys, SetupContext setup_ctx,
347     AppCtx app_ctx);
348 
349 extern PetscErrorCode PRINT_ADVECTION(Physics phys, SetupContext setup_ctx,
350                                       AppCtx app_ctx);
351 
352 extern PetscErrorCode PRINT_ADVECTION2D(Physics phys, SetupContext setup_ctx,
353                                         AppCtx app_ctx);
354 
355 // -----------------------------------------------------------------------------
356 // libCEED functions
357 // -----------------------------------------------------------------------------
358 // Utility function - essential BC dofs are encoded in closure indices as -(i+1).
359 PetscInt Involute(PetscInt i);
360 
361 // Utility function to create local CEED restriction
362 PetscErrorCode CreateRestrictionFromPlex(Ceed ceed, DM dm, CeedInt P,
363     CeedInt height, DMLabel domain_label,
364     CeedInt value, CeedElemRestriction *elem_restr);
365 
366 // Utility function to get Ceed Restriction for each domain
367 PetscErrorCode GetRestrictionForDomain(Ceed ceed, DM dm, CeedInt height,
368                                        DMLabel domain_label, PetscInt value,
369                                        CeedInt P, CeedInt Q, CeedInt q_data_size,
370                                        CeedElemRestriction *elem_restr_q,
371                                        CeedElemRestriction *elem_restr_x,
372                                        CeedElemRestriction *elem_restr_qd_i);
373 
374 // Utility function to create CEED Composite Operator for the entire domain
375 PetscErrorCode CreateOperatorForDomain(Ceed ceed, DM dm, SimpleBC bc,
376                                        CeedData ceed_data, Physics phys,
377                                        CeedOperator op_apply_vol, CeedInt height,
378                                        CeedInt P_sur, CeedInt Q_sur, CeedInt q_data_size_sur,
379                                        CeedOperator *op_apply);
380 
381 PetscErrorCode SetupLibceed(Ceed ceed, CeedData ceed_data, DM dm, User user,
382                             AppCtx app_ctx, ProblemData *problem, SimpleBC bc);
383 
384 // -----------------------------------------------------------------------------
385 // Time-stepping functions
386 // -----------------------------------------------------------------------------
387 // Compute mass matrix for explicit scheme
388 PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm, CeedData ceed_data,
389                                        Vec M);
390 
391 // RHS (Explicit time-stepper) function setup
392 PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data);
393 
394 // Implicit time-stepper function setup
395 PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G,
396                             void *user_data);
397 
398 // User provided TS Monitor
399 PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q,
400                             void *ctx);
401 
402 // TS: Create, setup, and solve
403 PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys,
404                           Vec *Q, PetscScalar *f_time, TS *ts);
405 
406 // -----------------------------------------------------------------------------
407 // Setup DM
408 // -----------------------------------------------------------------------------
409 // Read mesh and distribute DM in parallel
410 PetscErrorCode CreateDistributedDM(MPI_Comm comm, ProblemData *problem,
411                                    SetupContext setup_ctx, DM *dm);
412 
413 // Set up DM
414 PetscErrorCode SetUpDM(DM dm, ProblemData *problem, PetscInt degree,
415                        SimpleBC bc, Physics phys, void *setup_ctx);
416 
417 // Refine DM for high-order viz
418 PetscErrorCode VizRefineDM(DM dm, User user, ProblemData *problem,
419                            SimpleBC bc, Physics phys, void *setup_ctx);
420 
421 // -----------------------------------------------------------------------------
422 // Process command line options
423 // -----------------------------------------------------------------------------
424 // Register problems to be available on the command line
425 PetscErrorCode RegisterProblems_NS(AppCtx app_ctx);
426 
427 // Process general command line options
428 PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx);
429 
430 // -----------------------------------------------------------------------------
431 // Miscellaneous utility functions
432 // -----------------------------------------------------------------------------
433 PetscErrorCode ICs_FixMultiplicity(DM dm, CeedData ceed_data, Vec Q_loc, Vec Q,
434                                    CeedScalar time);
435 
436 PetscErrorCode DMPlexInsertBoundaryValues_NS(DM dm,
437     PetscBool insert_essential, Vec Q_loc, PetscReal time, Vec face_geom_FVM,
438     Vec cell_geom_FVM, Vec grad_FVM);
439 
440 // Compare reference solution values with current test run for CI
441 PetscErrorCode RegressionTests_NS(AppCtx app_ctx, Vec Q);
442 
443 // Get error for problems with exact solutions
444 PetscErrorCode GetError_NS(CeedData ceed_data, DM dm, AppCtx app_ctx, Vec Q,
445                            PetscScalar final_time);
446 
447 // Post-processing
448 PetscErrorCode PostProcess_NS(TS ts, CeedData ceed_data, DM dm,
449                               ProblemData *problem, AppCtx app_ctx,
450                               Vec Q, PetscScalar final_time);
451 
452 // -- Gather initial Q values in case of continuation of simulation
453 PetscErrorCode SetupICsFromBinary(MPI_Comm comm, AppCtx app_ctx, Vec Q);
454 
455 // Record boundary values from initial condition
456 PetscErrorCode SetBCsFromICs_NS(DM dm, Vec Q, Vec Q_loc);
457 
458 // -----------------------------------------------------------------------------
459 #endif
460