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