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 CeedScalar dm_scale; 280 CeedQFunctionUser setup_vol, setup_sur, ics, apply_vol_rhs, apply_vol_ifunction, 281 apply_sur; 282 const char *setup_vol_loc, *setup_sur_loc, *ics_loc, 283 *apply_vol_rhs_loc, *apply_vol_ifunction_loc, *apply_sur_loc; 284 bool non_zero_time; 285 PetscErrorCode (*bc)(PetscInt, PetscReal, const PetscReal[], PetscInt, 286 PetscScalar[], void *); 287 PetscErrorCode (*setup_ctx)(Ceed, CeedData, AppCtx, SetupContext, Physics); 288 PetscErrorCode (*bc_func)(DM, SimpleBC, Physics, void *); 289 PetscErrorCode (*print_info)(Physics, SetupContext, AppCtx); 290 } ProblemData; 291 // *INDENT-ON* 292 293 // ----------------------------------------------------------------------------- 294 // Set up problems 295 // ----------------------------------------------------------------------------- 296 // Set up function for each problem 297 extern PetscErrorCode NS_DENSITY_CURRENT(ProblemData *problem, DM dm, 298 void *setup_ctx, void *ctx); 299 extern PetscErrorCode NS_EULER_VORTEX(ProblemData *problem, DM dm, 300 void *setup_ctx, void *ctx); 301 extern PetscErrorCode NS_ADVECTION(ProblemData *problem, DM dm, void *setup_ctx, 302 void *ctx); 303 extern PetscErrorCode NS_ADVECTION2D(ProblemData *problem, DM dm, 304 void *setup_ctx, void *ctx); 305 306 // Set up context for each problem 307 extern PetscErrorCode SetupContext_DENSITY_CURRENT(Ceed ceed, 308 CeedData ceed_data, AppCtx app_ctx, SetupContext setup_ctx, Physics phys); 309 310 extern PetscErrorCode SetupContext_EULER_VORTEX(Ceed ceed, CeedData ceed_data, 311 AppCtx app_ctx, SetupContext setup_ctx, Physics phys); 312 313 extern PetscErrorCode SetupContext_ADVECTION(Ceed ceed, CeedData ceed_data, 314 AppCtx app_ctx, SetupContext setup_ctx, Physics phys); 315 316 extern PetscErrorCode SetupContext_ADVECTION2D(Ceed ceed, CeedData ceed_data, 317 AppCtx app_ctx, SetupContext setup_ctx, Physics phys); 318 319 // Boundary condition function for each problem 320 extern PetscErrorCode BC_DENSITY_CURRENT(DM dm, SimpleBC bc, Physics phys, 321 void *setup_ctx); 322 323 extern PetscErrorCode BC_EULER_VORTEX(DM dm, SimpleBC bc, Physics phys, 324 void *setup_ctx); 325 326 extern PetscErrorCode BC_ADVECTION(DM dm, SimpleBC bc, Physics phys, 327 void *setup_ctx); 328 329 extern PetscErrorCode BC_ADVECTION2D(DM dm, SimpleBC bc, Physics phys, 330 void *setup_ctx); 331 332 // Print function for each problem 333 extern PetscErrorCode PRINT_DENSITY_CURRENT(Physics phys, 334 SetupContext setup_ctx, AppCtx app_ctx); 335 336 extern PetscErrorCode PRINT_EULER_VORTEX(Physics phys, SetupContext setup_ctx, 337 AppCtx app_ctx); 338 339 extern PetscErrorCode PRINT_ADVECTION(Physics phys, SetupContext setup_ctx, 340 AppCtx app_ctx); 341 342 extern PetscErrorCode PRINT_ADVECTION2D(Physics phys, SetupContext setup_ctx, 343 AppCtx app_ctx); 344 345 // ----------------------------------------------------------------------------- 346 // libCEED functions 347 // ----------------------------------------------------------------------------- 348 // Utility function - essential BC dofs are encoded in closure indices as -(i+1). 349 PetscInt Involute(PetscInt i); 350 351 // Utility function to create local CEED restriction 352 PetscErrorCode CreateRestrictionFromPlex(Ceed ceed, DM dm, CeedInt height, 353 DMLabel domain_label, CeedInt value, CeedElemRestriction *elem_restr); 354 355 // Utility function to get Ceed Restriction for each domain 356 PetscErrorCode GetRestrictionForDomain(Ceed ceed, DM dm, CeedInt height, 357 DMLabel domain_label, PetscInt value, 358 CeedInt Q, CeedInt q_data_size, 359 CeedElemRestriction *elem_restr_q, 360 CeedElemRestriction *elem_restr_x, 361 CeedElemRestriction *elem_restr_qd_i); 362 363 // Utility function to create CEED Composite Operator for the entire domain 364 PetscErrorCode CreateOperatorForDomain(Ceed ceed, DM dm, SimpleBC bc, 365 CeedData ceed_data, Physics phys, 366 CeedOperator op_apply_vol, CeedInt height, 367 CeedInt P_sur, CeedInt Q_sur, CeedInt q_data_size_sur, 368 CeedOperator *op_apply); 369 370 PetscErrorCode SetupLibceed(Ceed ceed, CeedData ceed_data, DM dm, User user, 371 AppCtx app_ctx, ProblemData *problem, SimpleBC bc); 372 373 // ----------------------------------------------------------------------------- 374 // Time-stepping functions 375 // ----------------------------------------------------------------------------- 376 // Compute mass matrix for explicit scheme 377 PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm, CeedData ceed_data, 378 Vec M); 379 380 // RHS (Explicit time-stepper) function setup 381 PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data); 382 383 // Implicit time-stepper function setup 384 PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, 385 void *user_data); 386 387 // User provided TS Monitor 388 PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, 389 void *ctx); 390 391 // TS: Create, setup, and solve 392 PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, 393 Vec *Q, PetscScalar *f_time, TS *ts); 394 395 // ----------------------------------------------------------------------------- 396 // Setup DM 397 // ----------------------------------------------------------------------------- 398 // Create mesh 399 PetscErrorCode CreateDM(MPI_Comm comm, ProblemData *problem, DM *dm); 400 401 // Set up DM 402 PetscErrorCode SetUpDM(DM dm, ProblemData *problem, PetscInt degree, 403 SimpleBC bc, Physics phys, void *setup_ctx); 404 405 // Refine DM for high-order viz 406 PetscErrorCode VizRefineDM(DM dm, User user, ProblemData *problem, 407 SimpleBC bc, Physics phys, void *setup_ctx); 408 409 // ----------------------------------------------------------------------------- 410 // Process command line options 411 // ----------------------------------------------------------------------------- 412 // Register problems to be available on the command line 413 PetscErrorCode RegisterProblems_NS(AppCtx app_ctx); 414 415 // Process general command line options 416 PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx); 417 418 // ----------------------------------------------------------------------------- 419 // Miscellaneous utility functions 420 // ----------------------------------------------------------------------------- 421 PetscErrorCode ICs_FixMultiplicity(DM dm, CeedData ceed_data, Vec Q_loc, Vec Q, 422 CeedScalar time); 423 424 PetscErrorCode DMPlexInsertBoundaryValues_NS(DM dm, 425 PetscBool insert_essential, Vec Q_loc, PetscReal time, Vec face_geom_FVM, 426 Vec cell_geom_FVM, Vec grad_FVM); 427 428 // Compare reference solution values with current test run for CI 429 PetscErrorCode RegressionTests_NS(AppCtx app_ctx, Vec Q); 430 431 // Get error for problems with exact solutions 432 PetscErrorCode GetError_NS(CeedData ceed_data, DM dm, AppCtx app_ctx, Vec Q, 433 PetscScalar final_time); 434 435 // Post-processing 436 PetscErrorCode PostProcess_NS(TS ts, CeedData ceed_data, DM dm, 437 ProblemData *problem, AppCtx app_ctx, 438 Vec Q, PetscScalar final_time); 439 440 // -- Gather initial Q values in case of continuation of simulation 441 PetscErrorCode SetupICsFromBinary(MPI_Comm comm, AppCtx app_ctx, Vec Q); 442 443 // Record boundary values from initial condition 444 PetscErrorCode SetBCsFromICs_NS(DM dm, Vec Q, Vec Q_loc); 445 446 // ----------------------------------------------------------------------------- 447 448 #endif // libceed_fluids_examples_navier_stokes_h 449