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_sur; 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, num_slip[3]; 185 PetscInt walls[6], slips[3][6]; 186 PetscBool user_bc; 187 }; 188 189 // Initial conditions 190 #ifndef setup_context_struct 191 #define setup_context_struct 192 typedef struct SetupContext_ *SetupContext; 193 struct SetupContext_ { 194 CeedScalar theta0; 195 CeedScalar thetaC; 196 CeedScalar P0; 197 CeedScalar N; 198 CeedScalar cv; 199 CeedScalar cp; 200 CeedScalar g; 201 CeedScalar rc; 202 CeedScalar lx; 203 CeedScalar ly; 204 CeedScalar lz; 205 CeedScalar center[3]; 206 CeedScalar dc_axis[3]; 207 CeedScalar wind[3]; 208 CeedScalar time; 209 int wind_type; // See WindType: 0=ROTATION, 1=TRANSLATION 210 int bubble_type; // See BubbleType: 0=SPHERE, 1=CYLINDER 211 int bubble_continuity_type; // See BubbleContinuityType: 0=SMOOTH, 1=BACK_SHARP 2=THICK 212 }; 213 #endif 214 215 // DENSITY_CURRENT 216 #ifndef dc_context_struct 217 #define dc_context_struct 218 typedef struct DCContext_ *DCContext; 219 struct DCContext_ { 220 CeedScalar lambda; 221 CeedScalar mu; 222 CeedScalar k; 223 CeedScalar cv; 224 CeedScalar cp; 225 CeedScalar g; 226 CeedScalar c_tau; 227 int stabilization; // See StabilizationType: 0=none, 1=SU, 2=SUPG 228 }; 229 #endif 230 231 // EULER_VORTEX 232 #ifndef euler_context_struct 233 #define euler_context_struct 234 typedef struct EulerContext_ *EulerContext; 235 struct EulerContext_ { 236 CeedScalar center[3]; 237 CeedScalar curr_time; 238 CeedScalar vortex_strength; 239 CeedScalar c_tau; 240 CeedScalar mean_velocity[3]; 241 bool implicit; 242 int euler_test; 243 int stabilization; // See StabilizationType: 0=none, 1=SU, 2=SUPG 244 }; 245 #endif 246 247 // ADVECTION and ADVECTION2D 248 #ifndef advection_context_struct 249 #define advection_context_struct 250 typedef struct AdvectionContext_ *AdvectionContext; 251 struct AdvectionContext_ { 252 CeedScalar CtauS; 253 CeedScalar strong_form; 254 CeedScalar E_wind; 255 bool implicit; 256 int stabilization; // See StabilizationType: 0=none, 1=SU, 2=SUPG 257 }; 258 #endif 259 260 // Struct that contains all enums and structs used for the physics of all problems 261 struct Physics_private { 262 DCContext dc_ctx; 263 EulerContext euler_ctx; 264 AdvectionContext advection_ctx; 265 WindType wind_type; 266 BubbleType bubble_type; 267 BubbleContinuityType bubble_continuity_type; 268 EulerTestType euler_test; 269 StabilizationType stab; 270 PetscBool implicit; 271 PetscBool has_curr_time; 272 PetscBool has_neumann; 273 }; 274 275 // Problem specific data 276 // *INDENT-OFF* 277 typedef struct { 278 CeedInt dim, q_data_size_vol, q_data_size_sur; 279 CeedQFunctionUser setup_vol, setup_sur, ics, apply_vol_rhs, apply_vol_ifunction, 280 apply_sur; 281 const char *setup_vol_loc, *setup_sur_loc, *ics_loc, 282 *apply_vol_rhs_loc, *apply_vol_ifunction_loc, *apply_sur_loc; 283 bool non_zero_time; 284 PetscErrorCode (*bc)(PetscInt, PetscReal, const PetscReal[], PetscInt, 285 PetscScalar[], void *); 286 PetscErrorCode (*setup_ctx)(Ceed, CeedData, AppCtx, SetupContext, Physics); 287 PetscErrorCode (*bc_func)(DM, SimpleBC, Physics, void *); 288 PetscErrorCode (*print_info)(Physics, SetupContext, AppCtx); 289 } ProblemData; 290 // *INDENT-ON* 291 292 // ----------------------------------------------------------------------------- 293 // Set up problems 294 // ----------------------------------------------------------------------------- 295 // Set up function for each problem 296 extern PetscErrorCode NS_DENSITY_CURRENT(ProblemData *problem, void *setup_ctx, 297 void *ctx); 298 299 extern PetscErrorCode NS_EULER_VORTEX(ProblemData *problem, void *setup_ctx, 300 void *ctx); 301 302 extern PetscErrorCode NS_ADVECTION(ProblemData *problem, void *setup_ctx, 303 void *ctx); 304 305 extern PetscErrorCode NS_ADVECTION2D(ProblemData *problem, void *setup_ctx, 306 void *ctx); 307 308 // Set up context for each problem 309 extern PetscErrorCode SetupContext_DENSITY_CURRENT(Ceed ceed, 310 CeedData ceed_data, AppCtx app_ctx, SetupContext setup_ctx, Physics phys); 311 312 extern PetscErrorCode SetupContext_EULER_VORTEX(Ceed ceed, CeedData ceed_data, 313 AppCtx app_ctx, SetupContext setup_ctx, Physics phys); 314 315 extern PetscErrorCode SetupContext_ADVECTION(Ceed ceed, CeedData ceed_data, 316 AppCtx app_ctx, SetupContext setup_ctx, Physics phys); 317 318 extern PetscErrorCode SetupContext_ADVECTION2D(Ceed ceed, CeedData ceed_data, 319 AppCtx app_ctx, SetupContext setup_ctx, Physics phys); 320 321 // Boundary condition function for each problem 322 extern PetscErrorCode BC_DENSITY_CURRENT(DM dm, SimpleBC bc, Physics phys, 323 void *setup_ctx); 324 325 extern PetscErrorCode BC_EULER_VORTEX(DM dm, SimpleBC bc, Physics phys, 326 void *setup_ctx); 327 328 extern PetscErrorCode BC_ADVECTION(DM dm, SimpleBC bc, Physics phys, 329 void *setup_ctx); 330 331 extern PetscErrorCode BC_ADVECTION2D(DM dm, SimpleBC bc, Physics phys, 332 void *setup_ctx); 333 334 // Print function for each problem 335 extern PetscErrorCode PRINT_DENSITY_CURRENT(Physics phys, 336 SetupContext setup_ctx, AppCtx app_ctx); 337 338 extern PetscErrorCode PRINT_EULER_VORTEX(Physics phys, SetupContext setup_ctx, 339 AppCtx app_ctx); 340 341 extern PetscErrorCode PRINT_ADVECTION(Physics phys, SetupContext setup_ctx, 342 AppCtx app_ctx); 343 344 extern PetscErrorCode PRINT_ADVECTION2D(Physics phys, SetupContext setup_ctx, 345 AppCtx app_ctx); 346 347 // ----------------------------------------------------------------------------- 348 // libCEED functions 349 // ----------------------------------------------------------------------------- 350 // Utility function - essential BC dofs are encoded in closure indices as -(i+1). 351 PetscInt Involute(PetscInt i); 352 353 // Utility function to create local CEED restriction 354 PetscErrorCode CreateRestrictionFromPlex(Ceed ceed, DM dm, CeedInt height, 355 DMLabel domain_label, CeedInt value, CeedElemRestriction *elem_restr); 356 357 // Utility function to get Ceed Restriction for each domain 358 PetscErrorCode GetRestrictionForDomain(Ceed ceed, DM dm, CeedInt height, 359 DMLabel domain_label, PetscInt value, 360 CeedInt Q, CeedInt q_data_size, 361 CeedElemRestriction *elem_restr_q, 362 CeedElemRestriction *elem_restr_x, 363 CeedElemRestriction *elem_restr_qd_i); 364 365 // Utility function to create CEED Composite Operator for the entire domain 366 PetscErrorCode CreateOperatorForDomain(Ceed ceed, DM dm, SimpleBC bc, 367 CeedData ceed_data, Physics phys, 368 CeedOperator op_apply_vol, CeedInt height, 369 CeedInt P_sur, CeedInt Q_sur, CeedInt q_data_size_sur, 370 CeedOperator *op_apply); 371 372 PetscErrorCode SetupLibceed(Ceed ceed, CeedData ceed_data, DM dm, User user, 373 AppCtx app_ctx, ProblemData *problem, SimpleBC bc); 374 375 // ----------------------------------------------------------------------------- 376 // Time-stepping functions 377 // ----------------------------------------------------------------------------- 378 // Compute mass matrix for explicit scheme 379 PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm, CeedData ceed_data, 380 Vec M); 381 382 // RHS (Explicit time-stepper) function setup 383 PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data); 384 385 // Implicit time-stepper function setup 386 PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, 387 void *user_data); 388 389 // User provided TS Monitor 390 PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, 391 void *ctx); 392 393 // TS: Create, setup, and solve 394 PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, 395 Vec *Q, PetscScalar *f_time, TS *ts); 396 397 // ----------------------------------------------------------------------------- 398 // Setup DM 399 // ----------------------------------------------------------------------------- 400 // Read mesh and distribute DM in parallel 401 PetscErrorCode CreateDistributedDM(MPI_Comm comm, ProblemData *problem, 402 SetupContext setup_ctx, DM *dm); 403 404 // Set up DM 405 PetscErrorCode SetUpDM(DM dm, ProblemData *problem, PetscInt degree, 406 SimpleBC bc, Physics phys, void *setup_ctx); 407 408 // Refine DM for high-order viz 409 PetscErrorCode VizRefineDM(DM dm, User user, ProblemData *problem, 410 SimpleBC bc, Physics phys, void *setup_ctx); 411 412 // ----------------------------------------------------------------------------- 413 // Process command line options 414 // ----------------------------------------------------------------------------- 415 // Register problems to be available on the command line 416 PetscErrorCode RegisterProblems_NS(AppCtx app_ctx); 417 418 // Process general command line options 419 PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx); 420 421 // ----------------------------------------------------------------------------- 422 // Miscellaneous utility functions 423 // ----------------------------------------------------------------------------- 424 PetscErrorCode ICs_FixMultiplicity(DM dm, CeedData ceed_data, Vec Q_loc, Vec Q, 425 CeedScalar time); 426 427 PetscErrorCode DMPlexInsertBoundaryValues_NS(DM dm, 428 PetscBool insert_essential, Vec Q_loc, PetscReal time, Vec face_geom_FVM, 429 Vec cell_geom_FVM, Vec grad_FVM); 430 431 // Compare reference solution values with current test run for CI 432 PetscErrorCode RegressionTests_NS(AppCtx app_ctx, Vec Q); 433 434 // Get error for problems with exact solutions 435 PetscErrorCode GetError_NS(CeedData ceed_data, DM dm, AppCtx app_ctx, Vec Q, 436 PetscScalar final_time); 437 438 // Post-processing 439 PetscErrorCode PostProcess_NS(TS ts, CeedData ceed_data, DM dm, 440 ProblemData *problem, AppCtx app_ctx, 441 Vec Q, PetscScalar final_time); 442 443 // -- Gather initial Q values in case of continuation of simulation 444 PetscErrorCode SetupICsFromBinary(MPI_Comm comm, AppCtx app_ctx, Vec Q); 445 446 // Record boundary values from initial condition 447 PetscErrorCode SetBCsFromICs_NS(DM dm, Vec Q, Vec Q_loc); 448 449 // ----------------------------------------------------------------------------- 450 451 #endif // libceed_fluids_examples_navier_stokes_h 452