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 /// @file 9 /// Command line option processing for Navier-Stokes example using PETSc 10 11 #include <petscdevice.h> 12 #include <petscsys.h> 13 14 #include "../navierstokes.h" 15 16 // Register problems to be available on the command line 17 PetscErrorCode RegisterProblems_NS(AppCtx app_ctx) { 18 app_ctx->problems = NULL; 19 20 PetscFunctionBeginUser; 21 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "density_current", NS_DENSITY_CURRENT)); 22 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "euler_vortex", NS_EULER_VORTEX)); 23 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "shocktube", NS_SHOCKTUBE)); 24 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "advection", NS_ADVECTION)); 25 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "blasius", NS_BLASIUS)); 26 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "channel", NS_CHANNEL)); 27 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "gaussian_wave", NS_GAUSSIAN_WAVE)); 28 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "newtonian", NS_NEWTONIAN_IG)); 29 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "taylor_green", NS_TAYLOR_GREEN)); 30 PetscFunctionReturn(PETSC_SUCCESS); 31 } 32 33 // Process general command line options 34 PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx, SimpleBC bc) { 35 PetscBool ceed_flag = PETSC_FALSE; 36 PetscBool problem_flag = PETSC_FALSE; 37 PetscBool option_set = PETSC_FALSE; 38 39 PetscFunctionBeginUser; 40 PetscOptionsBegin(comm, NULL, "Navier-Stokes in PETSc with libCEED", NULL); 41 42 PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, app_ctx->ceed_resource, app_ctx->ceed_resource, 43 sizeof(app_ctx->ceed_resource), &ceed_flag)); 44 45 app_ctx->test_type = TESTTYPE_NONE; 46 PetscCall(PetscOptionsEnum("-test_type", "Type of test to run", NULL, TestTypes, (PetscEnum)(app_ctx->test_type), (PetscEnum *)&app_ctx->test_type, 47 NULL)); 48 49 app_ctx->test_tol = 1E-11; 50 PetscCall(PetscOptionsScalar("-compare_final_state_atol", "Test absolute tolerance", NULL, app_ctx->test_tol, &app_ctx->test_tol, NULL)); 51 52 PetscCall(PetscOptionsString("-compare_final_state_filename", "Test filename", NULL, app_ctx->test_file_path, app_ctx->test_file_path, 53 sizeof(app_ctx->test_file_path), NULL)); 54 55 PetscCall(PetscOptionsFList("-problem", "Problem to solve", NULL, app_ctx->problems, app_ctx->problem_name, app_ctx->problem_name, 56 sizeof(app_ctx->problem_name), &problem_flag)); 57 58 app_ctx->viz_refine = 0; 59 PetscCall(PetscOptionsInt("-viz_refine", "Regular refinement levels for visualization", NULL, app_ctx->viz_refine, &app_ctx->viz_refine, NULL)); 60 61 app_ctx->checkpoint_interval = 10; 62 app_ctx->checkpoint_vtk = PETSC_FALSE; 63 PetscCall(PetscOptionsDeprecated("-output_freq", "-checkpoint_interval", "libCEED 0.11.1", "Use -checkpoint_vtk true to include VTK output")); 64 PetscCall(PetscOptionsInt("-output_freq", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval, 65 &app_ctx->checkpoint_interval, &option_set)); 66 if (option_set) app_ctx->checkpoint_vtk = PETSC_TRUE; 67 PetscCall(PetscOptionsInt("-checkpoint_interval", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval, 68 &app_ctx->checkpoint_interval, NULL)); 69 PetscCall(PetscOptionsBool("-checkpoint_vtk", "Include VTK (*.vtu) output at each binary checkpoint", NULL, app_ctx->checkpoint_vtk, 70 &app_ctx->checkpoint_vtk, NULL)); 71 72 PetscCall(PetscOptionsBool("-output_add_stepnum2bin", "Add step number to the binary outputs", NULL, app_ctx->add_stepnum2bin, 73 &app_ctx->add_stepnum2bin, NULL)); 74 75 PetscCall(PetscStrncpy(app_ctx->output_dir, ".", 2)); 76 PetscCall(PetscOptionsString("-output_dir", "Output directory", NULL, app_ctx->output_dir, app_ctx->output_dir, sizeof(app_ctx->output_dir), NULL)); 77 78 app_ctx->cont_steps = 0; 79 PetscCall(PetscOptionsInt("-continue", "Continue from previous solution", NULL, app_ctx->cont_steps, &app_ctx->cont_steps, NULL)); 80 81 PetscCall(PetscStrcpy(app_ctx->cont_file, "[output_dir]/ns-solution.bin")); 82 PetscCall(PetscOptionsString("-continue_filename", "Filename to get initial condition from", NULL, app_ctx->cont_file, app_ctx->cont_file, 83 sizeof(app_ctx->cont_file), &option_set)); 84 if (!option_set) PetscCall(PetscSNPrintf(app_ctx->cont_file, sizeof app_ctx->cont_file, "%s/ns-solution.bin", app_ctx->output_dir)); 85 if (option_set && app_ctx->cont_steps == 0) app_ctx->cont_steps = -1; // Read time from file 86 87 PetscCall(PetscStrcpy(app_ctx->cont_time_file, "[output_dir]/ns-time.bin")); 88 PetscCall(PetscOptionsString("-continue_time_filename", "Filename to get initial condition time from", NULL, app_ctx->cont_time_file, 89 app_ctx->cont_time_file, sizeof(app_ctx->cont_time_file), &option_set)); 90 if (!option_set) PetscCall(PetscSNPrintf(app_ctx->cont_time_file, sizeof app_ctx->cont_time_file, "%s/ns-time.bin", app_ctx->output_dir)); 91 92 app_ctx->degree = 1; 93 PetscCall(PetscOptionsInt("-degree", "Polynomial degree of finite elements", NULL, app_ctx->degree, &app_ctx->degree, NULL)); 94 95 app_ctx->q_extra = 0; 96 PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, app_ctx->q_extra, &app_ctx->q_extra, NULL)); 97 98 { 99 PetscBool option_set; 100 char amat_type[256] = ""; 101 PetscCall(PetscOptionsFList("-amat_type", "Set the type of Amat distinct from Pmat (-dm_mat_type)", NULL, MatList, amat_type, amat_type, 102 sizeof(amat_type), &option_set)); 103 if (option_set) PetscCall(PetscStrallocpy(amat_type, (char **)&app_ctx->amat_type)); 104 } 105 106 // Provide default ceed resource if not specified 107 if (!ceed_flag) { 108 const char *ceed_resource = "/cpu/self"; 109 strncpy(app_ctx->ceed_resource, ceed_resource, 10); 110 } 111 // If we request a GPU, make sure PETSc has initialized its device (which is 112 // MPI-aware in case multiple devices are available) before CeedInit so that 113 // PETSc and libCEED agree about which device to use. 114 if (strncmp(app_ctx->ceed_resource, "/gpu", 4) == 0) PetscCall(PetscDeviceInitialize(PETSC_DEVICE_DEFAULT())); 115 116 // Provide default problem if not specified 117 if (!problem_flag) { 118 const char *problem_name = "density_current"; 119 strncpy(app_ctx->problem_name, problem_name, 16); 120 } 121 122 // Wall Boundary Conditions 123 bc->num_wall = 16; 124 PetscBool flg; 125 PetscCall(PetscOptionsIntArray("-bc_wall", "Face IDs to apply wall BC", NULL, bc->walls, &bc->num_wall, NULL)); 126 bc->num_comps = 5; 127 PetscCall(PetscOptionsIntArray("-wall_comps", "An array of constrained component numbers", NULL, bc->wall_comps, &bc->num_comps, &flg)); 128 129 { // Symmetry Boundary Conditions 130 const char *deprecated[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"}; 131 const char *flags[3] = {"-bc_symmetry_x", "-bc_symmetry_y", "-bc_symmetry_z"}; 132 PetscBool flg, has_symmetry = PETSC_FALSE; 133 134 for (PetscInt j = 0; j < 3; j++) { 135 bc->num_symmetry[j] = 16; 136 PetscCall(PetscOptionsDeprecated(deprecated[j], flags[j], "libCEED 0.12.0", 137 "Use -bc_symmetry_[x,y,z] for direct equivalency, or -bc_slip for weak, Riemann-based, direction-invariant " 138 "slip/no-penatration boundary conditions")); 139 PetscCall(PetscOptionsIntArray(flags[j], "Face IDs to apply symmetry BC", NULL, bc->symmetries[j], &bc->num_symmetry[j], &flg)); 140 if (!flg) { 141 bc->num_symmetry[j] = 16; 142 PetscCall(PetscOptionsIntArray(deprecated[j], "Face IDs to apply slip BC", NULL, bc->symmetries[j], &bc->num_symmetry[j], &flg)); 143 } 144 if (bc->num_symmetry[j] > 0) has_symmetry = PETSC_TRUE; 145 } 146 147 // Error if wall and symmetry BCs are set on the same face 148 if (has_symmetry) { 149 for (PetscInt c = 0; c < 3; c++) { 150 for (PetscInt s = 0; s < bc->num_symmetry[c]; s++) { 151 for (PetscInt w = 0; w < bc->num_wall; w++) { 152 PetscCheck(bc->symmetries[c][s] != bc->walls[w], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, 153 "Boundary condition already set on face %" PetscInt_FMT "!\n", bc->walls[w]); 154 } 155 } 156 } 157 } 158 } 159 app_ctx->wall_forces.num_wall = bc->num_wall; 160 PetscMalloc1(bc->num_wall, &app_ctx->wall_forces.walls); 161 PetscCall(PetscArraycpy(app_ctx->wall_forces.walls, bc->walls, bc->num_wall)); 162 163 // Inflow BCs 164 bc->num_inflow = 16; 165 PetscCall(PetscOptionsIntArray("-bc_inflow", "Face IDs to apply inflow BC", NULL, bc->inflows, &bc->num_inflow, NULL)); 166 // Outflow BCs 167 bc->num_outflow = 16; 168 PetscCall(PetscOptionsIntArray("-bc_outflow", "Face IDs to apply outflow BC", NULL, bc->outflows, &bc->num_outflow, NULL)); 169 // Freestream BCs 170 bc->num_freestream = 16; 171 PetscCall(PetscOptionsIntArray("-bc_freestream", "Face IDs to apply freestream BC", NULL, bc->freestreams, &bc->num_freestream, NULL)); 172 173 bc->num_slip = 16; 174 PetscCall(PetscOptionsIntArray("-bc_slip", "Face IDs to apply slip BC", NULL, bc->slips, &bc->num_slip, NULL)); 175 176 // Statistics Options 177 app_ctx->turb_spanstats_collect_interval = 1; 178 PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_collect_interval", "Number of timesteps between statistics collection", NULL, 179 app_ctx->turb_spanstats_collect_interval, &app_ctx->turb_spanstats_collect_interval, NULL)); 180 181 app_ctx->turb_spanstats_viewer_interval = -1; 182 PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_viewer_interval", "Number of timesteps between statistics viewer writing", NULL, 183 app_ctx->turb_spanstats_viewer_interval, &app_ctx->turb_spanstats_viewer_interval, NULL)); 184 185 PetscCall(PetscOptionsViewer("-ts_monitor_turbulence_spanstats_viewer", "Viewer for the statistics", NULL, &app_ctx->turb_spanstats_viewer, 186 &app_ctx->turb_spanstats_viewer_format, &app_ctx->turb_spanstats_enable)); 187 188 PetscCall(PetscOptionsViewer("-ts_monitor_wall_force", "Viewer for force on each (no-slip) wall", NULL, &app_ctx->wall_forces.viewer, 189 &app_ctx->wall_forces.viewer_format, NULL)); 190 191 // SGS Model Options 192 app_ctx->sgs_model_type = SGS_MODEL_NONE; 193 PetscCall(PetscOptionsEnum("-sgs_model_type", "Subgrid Stress Model type", NULL, SGSModelTypes, (PetscEnum)app_ctx->sgs_model_type, 194 (PetscEnum *)&app_ctx->sgs_model_type, NULL)); 195 196 PetscCall(PetscOptionsBool("-diff_filter_monitor", "Enable differential filtering TSMonitor", NULL, app_ctx->diff_filter_monitor, 197 &app_ctx->diff_filter_monitor, NULL)); 198 199 // Mesh Transformation Options 200 app_ctx->mesh_transform_type = MESH_TRANSFORM_NONE; 201 PetscCall(PetscOptionsEnum("-mesh_transform", "Mesh transform to perform", NULL, MeshTransformTypes, (PetscEnum)app_ctx->mesh_transform_type, 202 (PetscEnum *)&app_ctx->mesh_transform_type, NULL)); 203 204 PetscCall( 205 PetscOptionsBool("-sgs_train_enable", "Enable Data-Driven SGS training", NULL, app_ctx->sgs_train_enable, &app_ctx->sgs_train_enable, NULL)); 206 207 PetscOptionsEnd(); 208 PetscFunctionReturn(PETSC_SUCCESS); 209 } 210