1727da7e7SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2727da7e7SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3a515125bSLeila Ghaffari // 4727da7e7SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5a515125bSLeila Ghaffari // 6727da7e7SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7a515125bSLeila Ghaffari 8a515125bSLeila Ghaffari /// @file 9a515125bSLeila Ghaffari /// Command line option processing for Navier-Stokes example using PETSc 10a515125bSLeila Ghaffari 11a467869fSJed Brown #include <petscdevice.h> 12e419654dSJeremy L Thompson #include <petscsys.h> 13a467869fSJed Brown 14a515125bSLeila Ghaffari #include "../navierstokes.h" 15a515125bSLeila Ghaffari 16a515125bSLeila Ghaffari // Register problems to be available on the command line 17a515125bSLeila Ghaffari PetscErrorCode RegisterProblems_NS(AppCtx app_ctx) { 18a515125bSLeila Ghaffari app_ctx->problems = NULL; 19a515125bSLeila Ghaffari 2006f41313SJames Wright PetscFunctionBeginUser; 212b916ea7SJeremy L Thompson PetscCall(PetscFunctionListAdd(&app_ctx->problems, "density_current", NS_DENSITY_CURRENT)); 222b916ea7SJeremy L Thompson PetscCall(PetscFunctionListAdd(&app_ctx->problems, "euler_vortex", NS_EULER_VORTEX)); 232b916ea7SJeremy L Thompson PetscCall(PetscFunctionListAdd(&app_ctx->problems, "shocktube", NS_SHOCKTUBE)); 242b916ea7SJeremy L Thompson PetscCall(PetscFunctionListAdd(&app_ctx->problems, "advection", NS_ADVECTION)); 252b916ea7SJeremy L Thompson PetscCall(PetscFunctionListAdd(&app_ctx->problems, "blasius", NS_BLASIUS)); 262b916ea7SJeremy L Thompson PetscCall(PetscFunctionListAdd(&app_ctx->problems, "channel", NS_CHANNEL)); 27e7754af5SKenneth E. Jansen PetscCall(PetscFunctionListAdd(&app_ctx->problems, "gaussian_wave", NS_GAUSSIAN_WAVE)); 28b8fb7609SAdeleke O. Bankole PetscCall(PetscFunctionListAdd(&app_ctx->problems, "newtonian", NS_NEWTONIAN_IG)); 29692bc0d9SJames Wright PetscCall(PetscFunctionListAdd(&app_ctx->problems, "taylor_green", NS_TAYLOR_GREEN)); 30d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 31a515125bSLeila Ghaffari } 32a515125bSLeila Ghaffari 33a515125bSLeila Ghaffari // Process general command line options 342b916ea7SJeremy L Thompson PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx, SimpleBC bc) { 35a515125bSLeila Ghaffari PetscBool ceed_flag = PETSC_FALSE; 36a515125bSLeila Ghaffari PetscBool problem_flag = PETSC_FALSE; 3791a36801SJames Wright PetscBool option_set = PETSC_FALSE; 382b916ea7SJeremy L Thompson 39a515125bSLeila Ghaffari PetscFunctionBeginUser; 40*b1489633SJames Wright { 41*b1489633SJames Wright PetscInt num_options; 42*b1489633SJames Wright 43*b1489633SJames Wright PetscCall(PetscOptionsLeftGet(NULL, &num_options, NULL, NULL)); 44*b1489633SJames Wright PetscCheck(num_options > 0, comm, PETSC_ERR_USER_INPUT, 45*b1489633SJames Wright "Command line options required." 46*b1489633SJames Wright " Please consult the documentation to see which options are required."); 47*b1489633SJames Wright PetscCall(PetscOptionsLeftRestore(NULL, &num_options, NULL, NULL)); 48*b1489633SJames Wright } 49*b1489633SJames Wright 502b916ea7SJeremy L Thompson PetscOptionsBegin(comm, NULL, "Navier-Stokes in PETSc with libCEED", NULL); 51a515125bSLeila Ghaffari 522b916ea7SJeremy L Thompson PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, app_ctx->ceed_resource, app_ctx->ceed_resource, 532b916ea7SJeremy L Thompson sizeof(app_ctx->ceed_resource), &ceed_flag)); 54a515125bSLeila Ghaffari 550e1e9333SJames Wright app_ctx->test_type = TESTTYPE_NONE; 560e1e9333SJames Wright PetscCall(PetscOptionsEnum("-test_type", "Type of test to run", NULL, TestTypes, (PetscEnum)(app_ctx->test_type), (PetscEnum *)&app_ctx->test_type, 570e1e9333SJames Wright NULL)); 58a515125bSLeila Ghaffari 59a515125bSLeila Ghaffari app_ctx->test_tol = 1E-11; 602b916ea7SJeremy L Thompson PetscCall(PetscOptionsScalar("-compare_final_state_atol", "Test absolute tolerance", NULL, app_ctx->test_tol, &app_ctx->test_tol, NULL)); 61a515125bSLeila Ghaffari 62c931fa59SJames Wright PetscCall(PetscOptionsString("-compare_final_state_filename", "Test filename", NULL, app_ctx->test_file_path, app_ctx->test_file_path, 63c931fa59SJames Wright sizeof(app_ctx->test_file_path), NULL)); 64a515125bSLeila Ghaffari 652b916ea7SJeremy L Thompson PetscCall(PetscOptionsFList("-problem", "Problem to solve", NULL, app_ctx->problems, app_ctx->problem_name, app_ctx->problem_name, 662b916ea7SJeremy L Thompson sizeof(app_ctx->problem_name), &problem_flag)); 67a515125bSLeila Ghaffari 68a515125bSLeila Ghaffari app_ctx->viz_refine = 0; 692b916ea7SJeremy L Thompson PetscCall(PetscOptionsInt("-viz_refine", "Regular refinement levels for visualization", NULL, app_ctx->viz_refine, &app_ctx->viz_refine, NULL)); 70a515125bSLeila Ghaffari 71852e5969SJed Brown app_ctx->checkpoint_interval = 10; 72852e5969SJed Brown app_ctx->checkpoint_vtk = PETSC_FALSE; 73852e5969SJed Brown PetscCall(PetscOptionsDeprecated("-output_freq", "-checkpoint_interval", "libCEED 0.11.1", "Use -checkpoint_vtk true to include VTK output")); 74852e5969SJed Brown PetscCall(PetscOptionsInt("-output_freq", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval, 75852e5969SJed Brown &app_ctx->checkpoint_interval, &option_set)); 76852e5969SJed Brown if (option_set) app_ctx->checkpoint_vtk = PETSC_TRUE; 77852e5969SJed Brown PetscCall(PetscOptionsInt("-checkpoint_interval", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval, 78852e5969SJed Brown &app_ctx->checkpoint_interval, NULL)); 79852e5969SJed Brown PetscCall(PetscOptionsBool("-checkpoint_vtk", "Include VTK (*.vtu) output at each binary checkpoint", NULL, app_ctx->checkpoint_vtk, 80852e5969SJed Brown &app_ctx->checkpoint_vtk, NULL)); 81a515125bSLeila Ghaffari 822b916ea7SJeremy L Thompson PetscCall(PetscOptionsBool("-output_add_stepnum2bin", "Add step number to the binary outputs", NULL, app_ctx->add_stepnum2bin, 832b916ea7SJeremy L Thompson &app_ctx->add_stepnum2bin, NULL)); 8491a36801SJames Wright 8591a36801SJames Wright PetscCall(PetscStrncpy(app_ctx->output_dir, ".", 2)); 862b916ea7SJeremy L Thompson PetscCall(PetscOptionsString("-output_dir", "Output directory", NULL, app_ctx->output_dir, app_ctx->output_dir, sizeof(app_ctx->output_dir), NULL)); 8791a36801SJames Wright 88a515125bSLeila Ghaffari app_ctx->cont_steps = 0; 892b916ea7SJeremy L Thompson PetscCall(PetscOptionsInt("-continue", "Continue from previous solution", NULL, app_ctx->cont_steps, &app_ctx->cont_steps, NULL)); 90a515125bSLeila Ghaffari 9191a36801SJames Wright PetscCall(PetscStrcpy(app_ctx->cont_file, "[output_dir]/ns-solution.bin")); 922b916ea7SJeremy L Thompson PetscCall(PetscOptionsString("-continue_filename", "Filename to get initial condition from", NULL, app_ctx->cont_file, app_ctx->cont_file, 9391a36801SJames Wright sizeof(app_ctx->cont_file), &option_set)); 942b916ea7SJeremy L Thompson if (!option_set) PetscCall(PetscSNPrintf(app_ctx->cont_file, sizeof app_ctx->cont_file, "%s/ns-solution.bin", app_ctx->output_dir)); 959293eaa1SJed Brown if (option_set && app_ctx->cont_steps == 0) app_ctx->cont_steps = -1; // Read time from file 9691a36801SJames Wright 9791a36801SJames Wright PetscCall(PetscStrcpy(app_ctx->cont_time_file, "[output_dir]/ns-time.bin")); 982b916ea7SJeremy L Thompson PetscCall(PetscOptionsString("-continue_time_filename", "Filename to get initial condition time from", NULL, app_ctx->cont_time_file, 992b916ea7SJeremy L Thompson app_ctx->cont_time_file, sizeof(app_ctx->cont_time_file), &option_set)); 1002b916ea7SJeremy L Thompson if (!option_set) PetscCall(PetscSNPrintf(app_ctx->cont_time_file, sizeof app_ctx->cont_time_file, "%s/ns-time.bin", app_ctx->output_dir)); 10191a36801SJames Wright 102a515125bSLeila Ghaffari app_ctx->degree = 1; 1032b916ea7SJeremy L Thompson PetscCall(PetscOptionsInt("-degree", "Polynomial degree of finite elements", NULL, app_ctx->degree, &app_ctx->degree, NULL)); 104a515125bSLeila Ghaffari 1051219168aSLeila Ghaffari app_ctx->q_extra = 0; 1062b916ea7SJeremy L Thompson PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, app_ctx->q_extra, &app_ctx->q_extra, NULL)); 107a515125bSLeila Ghaffari 108b107fddaSJed Brown { 109b107fddaSJed Brown PetscBool option_set; 110b107fddaSJed Brown char amat_type[256] = ""; 1112b916ea7SJeremy L Thompson PetscCall(PetscOptionsFList("-amat_type", "Set the type of Amat distinct from Pmat (-dm_mat_type)", NULL, MatList, amat_type, amat_type, 1122b916ea7SJeremy L Thompson sizeof(amat_type), &option_set)); 1132b916ea7SJeremy L Thompson if (option_set) PetscCall(PetscStrallocpy(amat_type, (char **)&app_ctx->amat_type)); 114b107fddaSJed Brown } 1151024319bSJames Wright { 1161024319bSJames Wright PetscBool option_set; 1171024319bSJames Wright PetscCall(PetscOptionsHasName(NULL, NULL, "-pmat_pbdiagonal", &option_set)); 1181024319bSJames Wright if (option_set) PetscCall(PetscPrintf(comm, "Warning! -pmat_pbdiagonal no longer used. Pmat assembly determined from -pc_type setting\n")); 1191024319bSJames Wright } 120b107fddaSJed Brown 121a515125bSLeila Ghaffari // Provide default ceed resource if not specified 122a515125bSLeila Ghaffari if (!ceed_flag) { 123a515125bSLeila Ghaffari const char *ceed_resource = "/cpu/self"; 124a515125bSLeila Ghaffari strncpy(app_ctx->ceed_resource, ceed_resource, 10); 125a515125bSLeila Ghaffari } 126a467869fSJed Brown // If we request a GPU, make sure PETSc has initialized its device (which is 127a467869fSJed Brown // MPI-aware in case multiple devices are available) before CeedInit so that 128a467869fSJed Brown // PETSc and libCEED agree about which device to use. 129a467869fSJed Brown if (strncmp(app_ctx->ceed_resource, "/gpu", 4) == 0) PetscCall(PetscDeviceInitialize(PETSC_DEVICE_DEFAULT())); 130a515125bSLeila Ghaffari 131a515125bSLeila Ghaffari // Provide default problem if not specified 132a515125bSLeila Ghaffari if (!problem_flag) { 133a515125bSLeila Ghaffari const char *problem_name = "density_current"; 134a515125bSLeila Ghaffari strncpy(app_ctx->problem_name, problem_name, 16); 135a515125bSLeila Ghaffari } 136a515125bSLeila Ghaffari 137002797a3SLeila Ghaffari // Wall Boundary Conditions 138002797a3SLeila Ghaffari bc->num_wall = 16; 139002797a3SLeila Ghaffari PetscBool flg; 1402b916ea7SJeremy L Thompson PetscCall(PetscOptionsIntArray("-bc_wall", "Face IDs to apply wall BC", NULL, bc->walls, &bc->num_wall, NULL)); 141002797a3SLeila Ghaffari bc->num_comps = 5; 1422b916ea7SJeremy L Thompson PetscCall(PetscOptionsIntArray("-wall_comps", "An array of constrained component numbers", NULL, bc->wall_comps, &bc->num_comps, &flg)); 143fce2147eSJames Wright 144fce2147eSJames Wright { // Symmetry Boundary Conditions 145fce2147eSJames Wright const char *deprecated[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"}; 146fce2147eSJames Wright const char *flags[3] = {"-bc_symmetry_x", "-bc_symmetry_y", "-bc_symmetry_z"}; 147fce2147eSJames Wright PetscBool flg, has_symmetry = PETSC_FALSE; 148fce2147eSJames Wright 149002797a3SLeila Ghaffari for (PetscInt j = 0; j < 3; j++) { 150fce2147eSJames Wright bc->num_symmetry[j] = 16; 151fce2147eSJames Wright PetscCall(PetscOptionsDeprecated(deprecated[j], flags[j], "libCEED 0.12.0", 152fce2147eSJames Wright "Use -bc_symmetry_[x,y,z] for direct equivalency, or -bc_slip for weak, Riemann-based, direction-invariant " 153fce2147eSJames Wright "slip/no-penatration boundary conditions")); 154fce2147eSJames Wright PetscCall(PetscOptionsIntArray(flags[j], "Face IDs to apply symmetry BC", NULL, bc->symmetries[j], &bc->num_symmetry[j], &flg)); 155fce2147eSJames Wright if (!flg) { 156fce2147eSJames Wright bc->num_symmetry[j] = 16; 157fce2147eSJames Wright PetscCall(PetscOptionsIntArray(deprecated[j], "Face IDs to apply slip BC", NULL, bc->symmetries[j], &bc->num_symmetry[j], &flg)); 158fce2147eSJames Wright } 159fce2147eSJames Wright if (bc->num_symmetry[j] > 0) has_symmetry = PETSC_TRUE; 160002797a3SLeila Ghaffari } 161002797a3SLeila Ghaffari 162fce2147eSJames Wright // Error if wall and symmetry BCs are set on the same face 163fce2147eSJames Wright if (has_symmetry) { 164e419654dSJeremy L Thompson for (PetscInt c = 0; c < 3; c++) { 165fce2147eSJames Wright for (PetscInt s = 0; s < bc->num_symmetry[c]; s++) { 166e419654dSJeremy L Thompson for (PetscInt w = 0; w < bc->num_wall; w++) { 167fce2147eSJames Wright PetscCheck(bc->symmetries[c][s] != bc->walls[w], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, 1685d28dccaSJames Wright "Boundary condition already set on face %" PetscInt_FMT "!\n", bc->walls[w]); 169e419654dSJeremy L Thompson } 170e419654dSJeremy L Thompson } 171e419654dSJeremy L Thompson } 172e419654dSJeremy L Thompson } 173fce2147eSJames Wright } 17491933550SJames Wright app_ctx->wall_forces.num_wall = bc->num_wall; 17591933550SJames Wright PetscMalloc1(bc->num_wall, &app_ctx->wall_forces.walls); 17691933550SJames Wright PetscCall(PetscArraycpy(app_ctx->wall_forces.walls, bc->walls, bc->num_wall)); 177002797a3SLeila Ghaffari 178002797a3SLeila Ghaffari // Inflow BCs 179002797a3SLeila Ghaffari bc->num_inflow = 16; 1802b916ea7SJeremy L Thompson PetscCall(PetscOptionsIntArray("-bc_inflow", "Face IDs to apply inflow BC", NULL, bc->inflows, &bc->num_inflow, NULL)); 181002797a3SLeila Ghaffari // Outflow BCs 182002797a3SLeila Ghaffari bc->num_outflow = 16; 1832b916ea7SJeremy L Thompson PetscCall(PetscOptionsIntArray("-bc_outflow", "Face IDs to apply outflow BC", NULL, bc->outflows, &bc->num_outflow, NULL)); 184df55ba5fSJames Wright // Freestream BCs 185df55ba5fSJames Wright bc->num_freestream = 16; 1862b916ea7SJeremy L Thompson PetscCall(PetscOptionsIntArray("-bc_freestream", "Face IDs to apply freestream BC", NULL, bc->freestreams, &bc->num_freestream, NULL)); 187002797a3SLeila Ghaffari 1889ed3d70dSJames Wright bc->num_slip = 16; 1899ed3d70dSJames Wright PetscCall(PetscOptionsIntArray("-bc_slip", "Face IDs to apply slip BC", NULL, bc->slips, &bc->num_slip, NULL)); 1909ed3d70dSJames Wright 191b0488d1fSJames Wright // Statistics Options 192c931fa59SJames Wright app_ctx->turb_spanstats_collect_interval = 1; 193c931fa59SJames Wright PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_collect_interval", "Number of timesteps between statistics collection", NULL, 194c931fa59SJames Wright app_ctx->turb_spanstats_collect_interval, &app_ctx->turb_spanstats_collect_interval, NULL)); 195b0488d1fSJames Wright 196c931fa59SJames Wright app_ctx->turb_spanstats_viewer_interval = -1; 197c931fa59SJames Wright PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_viewer_interval", "Number of timesteps between statistics viewer writing", NULL, 198c931fa59SJames Wright app_ctx->turb_spanstats_viewer_interval, &app_ctx->turb_spanstats_viewer_interval, NULL)); 199b0488d1fSJames Wright 200c931fa59SJames Wright PetscCall(PetscOptionsViewer("-ts_monitor_turbulence_spanstats_viewer", "Viewer for the statistics", NULL, &app_ctx->turb_spanstats_viewer, 201c931fa59SJames Wright &app_ctx->turb_spanstats_viewer_format, &app_ctx->turb_spanstats_enable)); 202b0488d1fSJames Wright 203c5e9980aSAdeleke O. Bankole PetscCall(PetscOptionsViewer("-ts_monitor_wall_force", "Viewer for force on each (no-slip) wall", NULL, &app_ctx->wall_forces.viewer, 204c5e9980aSAdeleke O. Bankole &app_ctx->wall_forces.viewer_format, NULL)); 205c5e9980aSAdeleke O. Bankole 20601ab89c1SJames Wright // SGS Model Options 20701ab89c1SJames Wright app_ctx->sgs_model_type = SGS_MODEL_NONE; 20801ab89c1SJames Wright PetscCall(PetscOptionsEnum("-sgs_model_type", "Subgrid Stress Model type", NULL, SGSModelTypes, (PetscEnum)app_ctx->sgs_model_type, 20901ab89c1SJames Wright (PetscEnum *)&app_ctx->sgs_model_type, NULL)); 21001ab89c1SJames Wright 21188b07121SJames Wright PetscCall(PetscOptionsBool("-diff_filter_monitor", "Enable differential filtering TSMonitor", NULL, app_ctx->diff_filter_monitor, 21288b07121SJames Wright &app_ctx->diff_filter_monitor, NULL)); 21388b07121SJames Wright 214f31f4833SJames Wright // Mesh Transformation Options 215f31f4833SJames Wright app_ctx->mesh_transform_type = MESH_TRANSFORM_NONE; 216f31f4833SJames Wright PetscCall(PetscOptionsEnum("-mesh_transform", "Mesh transform to perform", NULL, MeshTransformTypes, (PetscEnum)app_ctx->mesh_transform_type, 217f31f4833SJames Wright (PetscEnum *)&app_ctx->mesh_transform_type, NULL)); 218f31f4833SJames Wright 2191c17f66aSJames Wright PetscCall( 2201c17f66aSJames Wright PetscOptionsBool("-sgs_train_enable", "Enable Data-Driven SGS training", NULL, app_ctx->sgs_train_enable, &app_ctx->sgs_train_enable, NULL)); 2211c17f66aSJames Wright 2221485969bSJeremy L Thompson PetscOptionsEnd(); 223d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 224a515125bSLeila Ghaffari } 225