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