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 // Solver arguments 114 MatType amat_type; 115 PetscBool pmat_pbdiagonal; 116 // Post-processing arguments 117 PetscInt output_freq; 118 PetscInt viz_refine; 119 PetscInt cont_steps; 120 char output_dir[PETSC_MAX_PATH_LEN]; 121 // Problem type arguments 122 PetscFunctionList problems; 123 char problem_name[PETSC_MAX_PATH_LEN]; 124 // Test mode arguments 125 PetscBool test_mode; 126 PetscScalar test_tol; 127 char file_path[PETSC_MAX_PATH_LEN]; 128 }; 129 130 // libCEED data struct 131 struct CeedData_private { 132 CeedVector x_coord, q_data; 133 CeedQFunction qf_setup_vol, qf_ics, qf_rhs_vol, qf_ifunction_vol, 134 qf_setup_sur, 135 qf_apply_inflow, qf_apply_inflow_jacobian, 136 qf_apply_outflow, qf_apply_outflow_jacobian; 137 CeedBasis basis_x, basis_xc, basis_q, basis_x_sur, basis_q_sur, 138 basis_xc_sur; 139 CeedElemRestriction elem_restr_x, elem_restr_q, elem_restr_qd_i; 140 CeedOperator op_setup_vol, op_ics; 141 }; 142 143 // PETSc user data 144 struct User_private { 145 MPI_Comm comm; 146 DM dm; 147 DM dm_viz; 148 Mat interp_viz; 149 Ceed ceed; 150 Units units; 151 Vec M, Q_loc, Q_dot_loc; 152 Physics phys; 153 AppCtx app_ctx; 154 CeedVector q_ceed, q_dot_ceed, g_ceed, coo_values_amat, coo_values_pmat, 155 x_ceed; 156 CeedOperator op_rhs_vol, op_rhs, op_ifunction_vol, op_ifunction, op_ijacobian, 157 op_dirichlet; 158 bool matrices_set_up; 159 CeedScalar time, dt; 160 }; 161 162 // Units 163 struct Units_private { 164 // fundamental units 165 PetscScalar meter; 166 PetscScalar kilogram; 167 PetscScalar second; 168 PetscScalar Kelvin; 169 // derived units 170 PetscScalar Pascal; 171 PetscScalar J_per_kg_K; 172 PetscScalar m_per_squared_s; 173 PetscScalar W_per_m_K; 174 PetscScalar Joule; 175 }; 176 177 // Boundary conditions 178 struct SimpleBC_private { 179 PetscInt num_wall, // Number of faces with wall BCs 180 wall_comps[5], // An array of constrained component numbers 181 num_comps, 182 num_slip[3], // Number of faces with slip BCs 183 num_inflow, 184 num_outflow; 185 PetscInt walls[16], slips[3][16], inflows[16], outflows[16]; 186 PetscBool user_bc; 187 }; 188 189 // Struct that contains all enums and structs used for the physics of all problems 190 struct Physics_private { 191 WindType wind_type; 192 BubbleType bubble_type; 193 BubbleContinuityType bubble_continuity_type; 194 EulerTestType euler_test; 195 StabilizationType stab; 196 PetscBool implicit; 197 PetscBool primitive; 198 PetscBool has_curr_time; 199 PetscBool has_neumann; 200 CeedContextFieldLabel solution_time_label; 201 CeedContextFieldLabel timestep_size_label; 202 CeedContextFieldLabel ics_time_label; 203 CeedContextFieldLabel ijacobian_time_shift_label; 204 }; 205 206 typedef struct { 207 CeedQFunctionUser qfunction; 208 const char *qfunction_loc; 209 CeedQFunctionContext qfunction_context; 210 } ProblemQFunctionSpec; 211 212 // Problem specific data 213 // *INDENT-OFF* 214 typedef struct ProblemData_private ProblemData; 215 struct ProblemData_private { 216 CeedInt dim, q_data_size_vol, q_data_size_sur, jac_data_size_sur; 217 CeedScalar dm_scale; 218 ProblemQFunctionSpec setup_vol, setup_sur, ics, apply_vol_rhs, apply_vol_ifunction, 219 apply_vol_ijacobian, apply_inflow, apply_outflow, 220 apply_inflow_jacobian, apply_outflow_jacobian; 221 bool non_zero_time; 222 PetscErrorCode (*bc)(PetscInt, PetscReal, const PetscReal[], PetscInt, 223 PetscScalar[], void *); 224 void *bc_ctx; 225 PetscBool bc_from_ics, use_dirichlet_ceed; 226 PetscErrorCode (*print_info)(ProblemData*, AppCtx); 227 }; 228 // *INDENT-ON* 229 230 extern int FreeContextPetsc(void *); 231 232 // ----------------------------------------------------------------------------- 233 // Set up problems 234 // ----------------------------------------------------------------------------- 235 // Set up function for each problem 236 extern PetscErrorCode NS_CHANNEL(ProblemData *problem, DM dm, 237 void *ctx); 238 extern PetscErrorCode NS_BLASIUS(ProblemData *problem, DM dm, 239 void *ctx); 240 extern PetscErrorCode NS_NEWTONIAN_IG(ProblemData *problem, DM dm, 241 void *ctx); 242 extern PetscErrorCode NS_DENSITY_CURRENT(ProblemData *problem, DM dm, 243 void *ctx); 244 245 extern PetscErrorCode NS_EULER_VORTEX(ProblemData *problem, DM dm, 246 void *ctx); 247 extern PetscErrorCode NS_SHOCKTUBE(ProblemData *problem, DM dm, 248 void *ctx); 249 extern PetscErrorCode NS_ADVECTION(ProblemData *problem, DM dm, 250 void *ctx); 251 extern PetscErrorCode NS_ADVECTION2D(ProblemData *problem, DM dm, 252 void *ctx); 253 254 // Print function for each problem 255 extern PetscErrorCode PRINT_NEWTONIAN(ProblemData *problem, AppCtx app_ctx); 256 257 extern PetscErrorCode PRINT_EULER_VORTEX(ProblemData *problem, AppCtx app_ctx); 258 259 extern PetscErrorCode PRINT_SHOCKTUBE(ProblemData *problem, AppCtx app_ctx); 260 261 extern PetscErrorCode PRINT_ADVECTION(ProblemData *problem, AppCtx app_ctx); 262 263 extern PetscErrorCode PRINT_ADVECTION2D(ProblemData *problem, AppCtx app_ctx); 264 265 // ----------------------------------------------------------------------------- 266 // libCEED functions 267 // ----------------------------------------------------------------------------- 268 // Utility function - essential BC dofs are encoded in closure indices as -(i+1). 269 PetscInt Involute(PetscInt i); 270 271 // Utility function to create local CEED restriction 272 PetscErrorCode CreateRestrictionFromPlex(Ceed ceed, DM dm, CeedInt height, 273 DMLabel domain_label, CeedInt value, CeedElemRestriction *elem_restr); 274 275 // Utility function to get Ceed Restriction for each domain 276 PetscErrorCode GetRestrictionForDomain(Ceed ceed, DM dm, CeedInt height, 277 DMLabel domain_label, PetscInt value, 278 CeedInt Q, CeedInt q_data_size, 279 CeedElemRestriction *elem_restr_q, 280 CeedElemRestriction *elem_restr_x, 281 CeedElemRestriction *elem_restr_qd_i); 282 283 // Utility function to create CEED Composite Operator for the entire domain 284 PetscErrorCode CreateOperatorForDomain(Ceed ceed, DM dm, SimpleBC bc, 285 CeedData ceed_data, Physics phys, 286 CeedOperator op_apply_vol, 287 CeedOperator op_apply_ijacobian_vol, 288 CeedInt height, 289 CeedInt P_sur, CeedInt Q_sur, 290 CeedInt q_data_size_sur, CeedInt jac_data_size_sur, 291 CeedOperator *op_apply, CeedOperator *op_apply_ijacobian); 292 293 PetscErrorCode SetupLibceed(Ceed ceed, CeedData ceed_data, DM dm, User user, 294 AppCtx app_ctx, ProblemData *problem, SimpleBC bc); 295 296 // ----------------------------------------------------------------------------- 297 // Time-stepping functions 298 // ----------------------------------------------------------------------------- 299 // Compute mass matrix for explicit scheme 300 PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm, CeedData ceed_data, 301 Vec M); 302 303 // RHS (Explicit time-stepper) function setup 304 PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data); 305 306 // Implicit time-stepper function setup 307 PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, 308 void *user_data); 309 310 // User provided TS Monitor 311 PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, 312 void *ctx); 313 314 // TS: Create, setup, and solve 315 PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, 316 Vec *Q, PetscScalar *f_time, TS *ts); 317 318 // ----------------------------------------------------------------------------- 319 // Setup DM 320 // ----------------------------------------------------------------------------- 321 // Create mesh 322 PetscErrorCode CreateDM(MPI_Comm comm, ProblemData *problem, 323 MatType, VecType, DM *dm); 324 325 // Set up DM 326 PetscErrorCode SetUpDM(DM dm, ProblemData *problem, PetscInt degree, 327 SimpleBC bc, Physics phys); 328 329 // Refine DM for high-order viz 330 PetscErrorCode VizRefineDM(DM dm, User user, ProblemData *problem, 331 SimpleBC bc, Physics phys); 332 333 // ----------------------------------------------------------------------------- 334 // Process command line options 335 // ----------------------------------------------------------------------------- 336 // Register problems to be available on the command line 337 PetscErrorCode RegisterProblems_NS(AppCtx app_ctx); 338 339 // Process general command line options 340 PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx, 341 SimpleBC bc); 342 343 // ----------------------------------------------------------------------------- 344 // Miscellaneous utility functions 345 // ----------------------------------------------------------------------------- 346 PetscErrorCode ICs_FixMultiplicity(DM dm, CeedData ceed_data, User user, 347 Vec Q_loc, Vec Q, 348 CeedScalar time); 349 350 PetscErrorCode DMPlexInsertBoundaryValues_NS(DM dm, 351 PetscBool insert_essential, Vec Q_loc, PetscReal time, Vec face_geom_FVM, 352 Vec cell_geom_FVM, Vec grad_FVM); 353 354 // Compare reference solution values with current test run for CI 355 PetscErrorCode RegressionTests_NS(AppCtx app_ctx, Vec Q); 356 357 // Get error for problems with exact solutions 358 PetscErrorCode GetError_NS(CeedData ceed_data, DM dm, User user, Vec Q, 359 PetscScalar final_time); 360 361 // Post-processing 362 PetscErrorCode PostProcess_NS(TS ts, CeedData ceed_data, DM dm, 363 ProblemData *problem, User user, 364 Vec Q, PetscScalar final_time); 365 366 // -- Gather initial Q values in case of continuation of simulation 367 PetscErrorCode SetupICsFromBinary(MPI_Comm comm, AppCtx app_ctx, Vec Q); 368 369 // Record boundary values from initial condition 370 PetscErrorCode SetBCsFromICs_NS(DM dm, Vec Q, Vec Q_loc); 371 372 // ----------------------------------------------------------------------------- 373 // Boundary Condition Related Functions 374 // ----------------------------------------------------------------------------- 375 376 // Setup StrongBCs that use QFunctions 377 PetscErrorCode SetupStrongBC_Ceed(Ceed ceed, CeedData ceed_data, DM dm, 378 User user, AppCtx app_ctx, ProblemData *problem, 379 SimpleBC bc, CeedInt Q_sur, CeedInt q_data_size_sur); 380 381 #endif // libceed_fluids_examples_navier_stokes_h 382