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