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