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 18 #include "qfunctions/newtonian_types.h" 19 #include "qfunctions/stabilization_types.h" 20 21 // ----------------------------------------------------------------------------- 22 // PETSc Version 23 // ----------------------------------------------------------------------------- 24 #if PETSC_VERSION_LT(3, 17, 0) 25 #error "PETSc v3.17 or later is required" 26 #endif 27 28 // ----------------------------------------------------------------------------- 29 // Enums 30 // ----------------------------------------------------------------------------- 31 // Translate PetscMemType to CeedMemType 32 static inline CeedMemType MemTypeP2C(PetscMemType mem_type) { return PetscMemTypeDevice(mem_type) ? CEED_MEM_DEVICE : CEED_MEM_HOST; } 33 34 // Advection - Wind Options 35 typedef enum { 36 WIND_ROTATION = 0, 37 WIND_TRANSLATION = 1, 38 } WindType; 39 static const char *const WindTypes[] = {"rotation", "translation", "WindType", "WIND_", NULL}; 40 41 // Advection - Bubble Types 42 typedef enum { 43 BUBBLE_SPHERE = 0, // dim=3 44 BUBBLE_CYLINDER = 1, // dim=2 45 } BubbleType; 46 static const char *const BubbleTypes[] = {"sphere", "cylinder", "BubbleType", "BUBBLE_", NULL}; 47 48 // Advection - Bubble Continuity Types 49 typedef enum { 50 BUBBLE_CONTINUITY_SMOOTH = 0, // Original continuous, smooth shape 51 BUBBLE_CONTINUITY_BACK_SHARP = 1, // Discontinuous, sharp back half shape 52 BUBBLE_CONTINUITY_THICK = 2, // Define a finite thickness 53 } BubbleContinuityType; 54 static const char *const BubbleContinuityTypes[] = {"smooth", "back_sharp", "thick", "BubbleContinuityType", "BUBBLE_CONTINUITY_", NULL}; 55 56 // Euler - test cases 57 typedef enum { 58 EULER_TEST_ISENTROPIC_VORTEX = 0, 59 EULER_TEST_1 = 1, 60 EULER_TEST_2 = 2, 61 EULER_TEST_3 = 3, 62 EULER_TEST_4 = 4, 63 EULER_TEST_5 = 5, 64 } EulerTestType; 65 static const char *const EulerTestTypes[] = {"isentropic_vortex", "test_1", "test_2", "test_3", "test_4", "test_5", 66 "EulerTestType", "EULER_TEST_", NULL}; 67 68 // Stabilization methods 69 static const char *const StabilizationTypes[] = {"none", "SU", "SUPG", "StabilizationType", "STAB_", NULL}; 70 71 // ----------------------------------------------------------------------------- 72 // Structs 73 // ----------------------------------------------------------------------------- 74 // Structs declarations 75 typedef struct AppCtx_private *AppCtx; 76 typedef struct CeedData_private *CeedData; 77 typedef struct User_private *User; 78 typedef struct Units_private *Units; 79 typedef struct SimpleBC_private *SimpleBC; 80 typedef struct Physics_private *Physics; 81 82 // Application context from user command line options 83 struct AppCtx_private { 84 // libCEED arguments 85 char ceed_resource[PETSC_MAX_PATH_LEN]; // libCEED backend 86 PetscInt degree; 87 PetscInt q_extra; 88 // Solver arguments 89 MatType amat_type; 90 PetscBool pmat_pbdiagonal; 91 // Post-processing arguments 92 PetscInt checkpoint_interval; 93 PetscInt viz_refine; 94 PetscInt cont_steps; 95 PetscReal cont_time; 96 char cont_file[PETSC_MAX_PATH_LEN]; 97 char cont_time_file[PETSC_MAX_PATH_LEN]; 98 char output_dir[PETSC_MAX_PATH_LEN]; 99 PetscBool add_stepnum2bin; 100 PetscBool checkpoint_vtk; 101 // Problem type arguments 102 PetscFunctionList problems; 103 char problem_name[PETSC_MAX_PATH_LEN]; 104 // Test mode arguments 105 PetscBool test_mode; 106 PetscScalar test_tol; 107 char file_path[PETSC_MAX_PATH_LEN]; 108 }; 109 110 // libCEED data struct 111 struct CeedData_private { 112 CeedVector x_coord, q_data; 113 CeedBasis basis_x, basis_xc, basis_q, basis_x_sur, basis_q_sur, basis_xc_sur; 114 CeedElemRestriction elem_restr_x, elem_restr_q, elem_restr_qd_i; 115 CeedOperator op_setup_vol, op_ics; 116 CeedQFunction qf_setup_vol, qf_ics, qf_rhs_vol, qf_ifunction_vol, qf_setup_sur, qf_apply_inflow, qf_apply_inflow_jacobian, qf_apply_outflow, 117 qf_apply_outflow_jacobian, qf_apply_freestream, qf_apply_freestream_jacobian; 118 struct { 119 CeedElemRestriction elem_restr_parent_x, elem_restr_parent_stats, elem_restr_parent_qd; 120 CeedBasis basis_x, basis_stats; 121 CeedVector x_coord; 122 } spanstats; 123 }; 124 125 typedef struct { 126 DM dm; 127 PetscSF sf; 128 CeedOperator op_stats; 129 PetscInt num_comp_stats; 130 } Span_Stats; 131 132 // PETSc user data 133 struct User_private { 134 MPI_Comm comm; 135 DM dm; 136 DM dm_viz; 137 Mat interp_viz; 138 Ceed ceed; 139 Units units; 140 Vec M, Q_loc, Q_dot_loc; 141 Physics phys; 142 AppCtx app_ctx; 143 CeedVector q_ceed, q_dot_ceed, g_ceed, coo_values_amat, coo_values_pmat, x_ceed; 144 CeedOperator op_rhs_vol, op_rhs, op_ifunction_vol, op_ifunction, op_ijacobian, op_dirichlet; 145 bool matrices_set_up; 146 CeedScalar time, dt; 147 Span_Stats spanstats; 148 }; 149 150 // Units 151 struct Units_private { 152 // fundamental units 153 PetscScalar meter; 154 PetscScalar kilogram; 155 PetscScalar second; 156 PetscScalar Kelvin; 157 // derived units 158 PetscScalar Pascal; 159 PetscScalar J_per_kg_K; 160 PetscScalar m_per_squared_s; 161 PetscScalar W_per_m_K; 162 PetscScalar Joule; 163 }; 164 165 // Boundary conditions 166 struct SimpleBC_private { 167 PetscInt num_wall, // Number of faces with wall BCs 168 wall_comps[5], // An array of constrained component numbers 169 num_comps, 170 num_slip[3], // Number of faces with slip BCs 171 num_inflow, num_outflow, num_freestream; 172 PetscInt walls[16], slips[3][16], inflows[16], outflows[16], freestreams[16]; 173 PetscBool user_bc; 174 }; 175 176 // Struct that contains all enums and structs used for the physics of all problems 177 struct Physics_private { 178 WindType wind_type; 179 BubbleType bubble_type; 180 BubbleContinuityType bubble_continuity_type; 181 EulerTestType euler_test; 182 StabilizationType stab; 183 PetscBool implicit; 184 StateVariable state_var; 185 PetscBool has_curr_time; 186 PetscBool has_neumann; 187 CeedContextFieldLabel solution_time_label; 188 CeedContextFieldLabel stg_solution_time_label; 189 CeedContextFieldLabel timestep_size_label; 190 CeedContextFieldLabel ics_time_label; 191 CeedContextFieldLabel ijacobian_time_shift_label; 192 }; 193 194 typedef struct { 195 CeedQFunctionUser qfunction; 196 const char *qfunction_loc; 197 CeedQFunctionContext qfunction_context; 198 } ProblemQFunctionSpec; 199 200 // Problem specific data 201 typedef struct ProblemData_private ProblemData; 202 struct ProblemData_private { 203 CeedInt dim, q_data_size_vol, q_data_size_sur, jac_data_size_sur; 204 CeedScalar dm_scale; 205 ProblemQFunctionSpec setup_vol, setup_sur, ics, apply_vol_rhs, apply_vol_ifunction, apply_vol_ijacobian, apply_inflow, apply_outflow, 206 apply_freestream, apply_inflow_jacobian, apply_outflow_jacobian, apply_freestream_jacobian; 207 bool non_zero_time; 208 PetscErrorCode (*bc)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *); 209 void *bc_ctx; 210 PetscBool bc_from_ics, use_dirichlet_ceed; 211 PetscErrorCode (*print_info)(ProblemData *, AppCtx); 212 }; 213 214 extern int FreeContextPetsc(void *); 215 216 // ----------------------------------------------------------------------------- 217 // Set up problems 218 // ----------------------------------------------------------------------------- 219 // Set up function for each problem 220 extern PetscErrorCode NS_NEWTONIAN_WAVE(ProblemData *problem, DM dm, void *ctx, SimpleBC bc); 221 extern PetscErrorCode NS_CHANNEL(ProblemData *problem, DM dm, void *ctx, SimpleBC bc); 222 extern PetscErrorCode NS_BLASIUS(ProblemData *problem, DM dm, void *ctx, SimpleBC bc); 223 extern PetscErrorCode NS_NEWTONIAN_IG(ProblemData *problem, DM dm, void *ctx, SimpleBC bc); 224 extern PetscErrorCode NS_DENSITY_CURRENT(ProblemData *problem, DM dm, void *ctx, SimpleBC bc); 225 extern PetscErrorCode NS_EULER_VORTEX(ProblemData *problem, DM dm, void *ctx, SimpleBC bc); 226 extern PetscErrorCode NS_SHOCKTUBE(ProblemData *problem, DM dm, void *ctx, SimpleBC bc); 227 extern PetscErrorCode NS_ADVECTION(ProblemData *problem, DM dm, void *ctx, SimpleBC bc); 228 extern PetscErrorCode NS_ADVECTION2D(ProblemData *problem, DM dm, void *ctx, SimpleBC bc); 229 230 // Print function for each problem 231 extern PetscErrorCode PRINT_NEWTONIAN(ProblemData *problem, AppCtx app_ctx); 232 233 extern PetscErrorCode PRINT_EULER_VORTEX(ProblemData *problem, AppCtx app_ctx); 234 235 extern PetscErrorCode PRINT_SHOCKTUBE(ProblemData *problem, AppCtx app_ctx); 236 237 extern PetscErrorCode PRINT_ADVECTION(ProblemData *problem, AppCtx app_ctx); 238 239 extern PetscErrorCode PRINT_ADVECTION2D(ProblemData *problem, AppCtx app_ctx); 240 241 // ----------------------------------------------------------------------------- 242 // libCEED functions 243 // ----------------------------------------------------------------------------- 244 // Utility function - essential BC dofs are encoded in closure indices as -(i+1). 245 PetscInt Involute(PetscInt i); 246 247 // Utility function to create local CEED restriction 248 PetscErrorCode CreateRestrictionFromPlex(Ceed ceed, DM dm, CeedInt height, DMLabel domain_label, CeedInt value, CeedElemRestriction *elem_restr); 249 250 // Utility function to get Ceed Restriction for each domain 251 PetscErrorCode GetRestrictionForDomain(Ceed ceed, DM dm, CeedInt height, DMLabel domain_label, PetscInt value, CeedInt Q, CeedInt q_data_size, 252 CeedElemRestriction *elem_restr_q, CeedElemRestriction *elem_restr_x, CeedElemRestriction *elem_restr_qd_i); 253 254 // Utility function to create CEED Composite Operator for the entire domain 255 PetscErrorCode CreateOperatorForDomain(Ceed ceed, DM dm, SimpleBC bc, CeedData ceed_data, Physics phys, CeedOperator op_apply_vol, 256 CeedOperator op_apply_ijacobian_vol, CeedInt height, CeedInt P_sur, CeedInt Q_sur, CeedInt q_data_size_sur, 257 CeedInt jac_data_size_sur, CeedOperator *op_apply, CeedOperator *op_apply_ijacobian); 258 259 PetscErrorCode SetupLibceed(Ceed ceed, CeedData ceed_data, DM dm, User user, AppCtx app_ctx, ProblemData *problem, SimpleBC bc); 260 261 // ----------------------------------------------------------------------------- 262 // Time-stepping functions 263 // ----------------------------------------------------------------------------- 264 // Compute mass matrix for explicit scheme 265 PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm, CeedData ceed_data, Vec M); 266 267 // RHS (Explicit time-stepper) function setup 268 PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data); 269 270 // Implicit time-stepper function setup 271 PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, void *user_data); 272 273 // User provided TS Monitor 274 PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx); 275 276 // TS: Create, setup, and solve 277 PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, Vec *Q, PetscScalar *f_time, TS *ts); 278 279 // ----------------------------------------------------------------------------- 280 // Setup DM 281 // ----------------------------------------------------------------------------- 282 // Create mesh 283 PetscErrorCode CreateDM(MPI_Comm comm, ProblemData *problem, MatType, VecType, DM *dm); 284 285 // Set up DM 286 PetscErrorCode SetUpDM(DM dm, ProblemData *problem, PetscInt degree, SimpleBC bc, Physics phys); 287 288 // Refine DM for high-order viz 289 PetscErrorCode VizRefineDM(DM dm, User user, ProblemData *problem, SimpleBC bc, Physics phys); 290 291 // ----------------------------------------------------------------------------- 292 // Process command line options 293 // ----------------------------------------------------------------------------- 294 // Register problems to be available on the command line 295 PetscErrorCode RegisterProblems_NS(AppCtx app_ctx); 296 297 // Process general command line options 298 PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx, SimpleBC bc); 299 300 // ----------------------------------------------------------------------------- 301 // Miscellaneous utility functions 302 // ----------------------------------------------------------------------------- 303 PetscErrorCode ICs_FixMultiplicity(DM dm, CeedData ceed_data, User user, Vec Q_loc, Vec Q, CeedScalar time); 304 305 PetscErrorCode DMPlexInsertBoundaryValues_NS(DM dm, PetscBool insert_essential, Vec Q_loc, PetscReal time, Vec face_geom_FVM, Vec cell_geom_FVM, 306 Vec grad_FVM); 307 308 // Compare reference solution values with current test run for CI 309 PetscErrorCode RegressionTests_NS(AppCtx app_ctx, Vec Q); 310 311 // Get error for problems with exact solutions 312 PetscErrorCode GetError_NS(CeedData ceed_data, DM dm, User user, Vec Q, PetscScalar final_time); 313 314 // Post-processing 315 PetscErrorCode PostProcess_NS(TS ts, CeedData ceed_data, DM dm, ProblemData *problem, User user, Vec Q, PetscScalar final_time); 316 317 // -- Gather initial Q values in case of continuation of simulation 318 PetscErrorCode SetupICsFromBinary(MPI_Comm comm, AppCtx app_ctx, Vec Q); 319 320 // Record boundary values from initial condition 321 PetscErrorCode SetBCsFromICs_NS(DM dm, Vec Q, Vec Q_loc); 322 323 // Versioning token for binary checkpoints 324 extern const PetscInt FLUIDS_FILE_TOKEN; 325 326 // Create appropriate mass qfunction based on number of components N 327 PetscErrorCode CreateMassQFunction(Ceed ceed, CeedInt N, CeedInt q_data_size, CeedQFunction *qf); 328 329 PetscErrorCode CreateStatsDM(User user, ProblemData *problem, PetscInt degree, SimpleBC bc); 330 331 PetscErrorCode SetupStatsCollection(Ceed ceed, User user, CeedData ceed_data, ProblemData *problem); 332 333 // ----------------------------------------------------------------------------- 334 // Boundary Condition Related Functions 335 // ----------------------------------------------------------------------------- 336 337 // Setup StrongBCs that use QFunctions 338 PetscErrorCode SetupStrongBC_Ceed(Ceed ceed, CeedData ceed_data, DM dm, User user, AppCtx app_ctx, ProblemData *problem, SimpleBC bc, CeedInt Q_sur, 339 CeedInt q_data_size_sur); 340 341 PetscErrorCode FreestreamBCSetup(ProblemData *problem, DM dm, void *ctx, NewtonianIdealGasContext newtonian_ig_ctx, const StatePrimitive *reference); 342 PetscErrorCode OutflowBCSetup(ProblemData *problem, DM dm, void *ctx, NewtonianIdealGasContext newtonian_ig_ctx, const StatePrimitive *reference); 343 344 #endif // libceed_fluids_examples_navier_stokes_h 345