1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors. 2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause 3 4 /// @file 5 /// Command line option processing for Navier-Stokes example using PETSc 6 7 #include <petscdevice.h> 8 #include <petscsys.h> 9 10 #include <navierstokes.h> 11 12 // Register problems to be available on the command line 13 static PetscErrorCode RegisterProblems_NS(AppCtx app_ctx) { 14 app_ctx->problems = NULL; 15 16 PetscFunctionBeginUser; 17 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "density_current", NS_DENSITY_CURRENT)); 18 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "euler_vortex", NS_EULER_VORTEX)); 19 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "shocktube", NS_SHOCKTUBE)); 20 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "advection", NS_ADVECTION)); 21 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "blasius", NS_BLASIUS)); 22 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "channel", NS_CHANNEL)); 23 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "gaussian_wave", NS_GAUSSIAN_WAVE)); 24 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "newtonian", NS_NEWTONIAN_IG)); 25 PetscCall(PetscFunctionListAdd(&app_ctx->problems, "taylor_green", NS_TAYLOR_GREEN)); 26 PetscFunctionReturn(PETSC_SUCCESS); 27 } 28 29 /** 30 @brief Convert ISO 8601 time string to duration in seconds 31 32 Accepted formats are 'hh', 'hh:mm', and 'hh:mm:ss'. 33 34 @param[in] comm MPI_Comm for error handling 35 @param[in] string string of the ISO 8601 duration 36 @param[out] duration Duration in number of seconds 37 **/ 38 PetscErrorCode ISO8601TimeDurationToSeconds(MPI_Comm comm, const char *string, time_t *duration) { 39 int num_items; 40 char **entries; 41 42 PetscFunctionBeginUser; 43 if (string[0] == '\0') { 44 *duration = 0; 45 PetscFunctionReturn(PETSC_SUCCESS); 46 } 47 for (PetscInt i = 0; i < strlen(string); i++) { 48 PetscCheck(isdigit(string[i]) || string[i] == ':', comm, PETSC_ERR_SUP, "Time duration may only include digits and ':' separator, found '%c'", 49 string[i]); 50 } 51 PetscCall(PetscStrToArray(string, ':', &num_items, &entries)); 52 switch (num_items) { 53 case 1: // Only hours 54 *duration = 60 * 60 * atoi(entries[0]); 55 break; 56 case 2: // Hours and Minutes 57 *duration = 60 * 60 * atoi(entries[0]) + 60 * atoi(entries[1]); 58 break; 59 case 3: // Hours, Minutes, and Seconds 60 *duration = 60 * 60 * atoi(entries[0]) + 60 * atoi(entries[1]) + atoi(entries[2]); 61 break; 62 default: 63 SETERRQ(comm, PETSC_ERR_SUP, "Recieved %d ':' delimited entries, expect either 1, 2, or 3", num_items); 64 } 65 PetscCall(PetscStrToArrayDestroy(num_items, entries)); 66 PetscFunctionReturn(PETSC_SUCCESS); 67 } 68 69 // Process general command line options 70 PetscErrorCode ProcessCommandLineOptions(Honee honee, SimpleBC bc) { 71 MPI_Comm comm = honee->comm; 72 AppCtx app_ctx = honee->app_ctx; 73 PetscBool ceed_flag = PETSC_FALSE; 74 PetscBool problem_flag = PETSC_FALSE; 75 PetscBool option_set = PETSC_FALSE; 76 77 PetscFunctionBeginUser; 78 { 79 PetscInt num_options; 80 81 PetscCall(PetscOptionsLeftGet(NULL, &num_options, NULL, NULL)); 82 PetscCheck(num_options > 0, comm, PETSC_ERR_USER_INPUT, 83 "Command line options required." 84 " Please consult the documentation to see which options are required."); 85 PetscCall(PetscOptionsLeftRestore(NULL, &num_options, NULL, NULL)); 86 } 87 88 PetscCall(RegisterProblems_NS(app_ctx)); 89 PetscOptionsBegin(comm, NULL, "HONEE - High-Order Navier-stokes Equation Evaluator", NULL); 90 91 PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, app_ctx->ceed_resource, app_ctx->ceed_resource, 92 sizeof(app_ctx->ceed_resource), &ceed_flag)); 93 94 app_ctx->test_type = TESTTYPE_NONE; 95 PetscCall(PetscOptionsEnum("-test_type", "Type of test to run", NULL, TestTypes, (PetscEnum)(app_ctx->test_type), (PetscEnum *)&app_ctx->test_type, 96 NULL)); 97 98 app_ctx->test_tol = 1E-11; 99 PetscCall(PetscOptionsScalar("-compare_final_state_atol", "Test absolute tolerance", NULL, app_ctx->test_tol, &app_ctx->test_tol, NULL)); 100 101 PetscCall(PetscOptionsString("-compare_final_state_filename", "Test filename", NULL, app_ctx->test_file_path, app_ctx->test_file_path, 102 sizeof(app_ctx->test_file_path), NULL)); 103 104 PetscCall(PetscOptionsFList("-problem", "Problem to solve", NULL, app_ctx->problems, app_ctx->problem_name, app_ctx->problem_name, 105 sizeof(app_ctx->problem_name), &problem_flag)); 106 107 app_ctx->viz_refine = 0; 108 PetscCall(PetscOptionsInt("-viz_refine", "Regular refinement levels for visualization", NULL, app_ctx->viz_refine, &app_ctx->viz_refine, NULL)); 109 110 app_ctx->checkpoint_interval = 10; 111 app_ctx->checkpoint_vtk = PETSC_FALSE; 112 PetscCall(PetscOptionsDeprecated("-output_freq", "-checkpoint_interval", "libCEED 0.11.1", "Use -checkpoint_vtk true to include VTK output")); 113 PetscCall(PetscOptionsInt("-output_freq", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval, 114 &app_ctx->checkpoint_interval, &option_set)); 115 if (option_set) app_ctx->checkpoint_vtk = PETSC_TRUE; 116 PetscCall(PetscOptionsInt("-checkpoint_interval", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval, 117 &app_ctx->checkpoint_interval, NULL)); 118 PetscCall(PetscOptionsBool("-checkpoint_vtk", "Include VTK (*.vtu) output at each binary checkpoint", NULL, app_ctx->checkpoint_vtk, 119 &app_ctx->checkpoint_vtk, NULL)); 120 121 PetscCall(PetscOptionsBool("-output_add_stepnum2bin", "Add step number to the binary outputs", NULL, app_ctx->add_stepnum2bin, 122 &app_ctx->add_stepnum2bin, NULL)); 123 124 PetscCall(PetscStrncpy(app_ctx->output_dir, ".", 2)); 125 PetscCall(PetscOptionsString("-output_dir", "Output directory", NULL, app_ctx->output_dir, app_ctx->output_dir, sizeof(app_ctx->output_dir), NULL)); 126 PetscMPIInt rank; 127 MPI_Comm_rank(comm, &rank); 128 if (!rank) PetscCall(PetscMkdir(app_ctx->output_dir)); 129 130 PetscCall(PetscOptionsString("-continue_filename", "Filename to get initial condition from", NULL, app_ctx->cont_file, app_ctx->cont_file, 131 sizeof(app_ctx->cont_file), NULL)); 132 if (app_ctx->cont_file[0] != '\0') app_ctx->use_continue_file = PETSC_TRUE; 133 134 PetscCall( 135 PetscOptionsDeprecated("-continue", NULL, "HONEE 0.0.0", "Set -continue_filename to non-empty string to continue from previous solution")); 136 PetscCall( 137 PetscOptionsDeprecated("-continue_time_filename", NULL, "HONEE 0.0.0", "HONEE no longer supports reading in solution times from binary file")); 138 139 app_ctx->degree = 1; 140 PetscCall(PetscOptionsInt("-degree", "Polynomial degree of finite elements", NULL, app_ctx->degree, &app_ctx->degree, NULL)); 141 142 app_ctx->q_extra = 0; 143 PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, app_ctx->q_extra, &app_ctx->q_extra, NULL)); 144 145 { 146 PetscBool option_set; 147 char amat_type[256] = ""; 148 PetscCall(PetscOptionsFList("-amat_type", "Set the type of Amat distinct from Pmat (-dm_mat_type)", NULL, MatList, amat_type, amat_type, 149 sizeof(amat_type), &option_set)); 150 if (option_set) PetscCall(PetscStrallocpy(amat_type, (char **)&app_ctx->amat_type)); 151 } 152 { 153 PetscBool option_set; 154 PetscCall(PetscOptionsHasName(NULL, NULL, "-pmat_pbdiagonal", &option_set)); 155 if (option_set) PetscCall(PetscPrintf(comm, "Warning! -pmat_pbdiagonal no longer used. Pmat assembly determined from -pc_type setting\n")); 156 } 157 158 // Provide default ceed resource if not specified 159 if (!ceed_flag) { 160 const char *ceed_resource = "/cpu/self"; 161 strncpy(app_ctx->ceed_resource, ceed_resource, 10); 162 } 163 // If we request a GPU, make sure PETSc has initialized its device (which is 164 // MPI-aware in case multiple devices are available) before CeedInit so that 165 // PETSc and libCEED agree about which device to use. 166 if (strncmp(app_ctx->ceed_resource, "/gpu", 4) == 0) PetscCall(PetscDeviceInitialize(PETSC_DEVICE_DEFAULT())); 167 168 // Provide default problem if not specified 169 if (!problem_flag) { 170 const char *problem_name = "density_current"; 171 strncpy(app_ctx->problem_name, problem_name, 16); 172 } 173 174 // Statistics Options 175 app_ctx->turb_spanstats_collect_interval = 1; 176 PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_collect_interval", "Number of timesteps between statistics collection", NULL, 177 app_ctx->turb_spanstats_collect_interval, &app_ctx->turb_spanstats_collect_interval, NULL)); 178 179 app_ctx->turb_spanstats_viewer_interval = -1; 180 PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_viewer_interval", "Number of timesteps between statistics viewer writing", NULL, 181 app_ctx->turb_spanstats_viewer_interval, &app_ctx->turb_spanstats_viewer_interval, NULL)); 182 183 PetscCall(PetscOptionsViewer("-ts_monitor_turbulence_spanstats_viewer", "Viewer for the statistics", NULL, &app_ctx->turb_spanstats_viewer, 184 &app_ctx->turb_spanstats_viewer_format, &app_ctx->turb_spanstats_enable)); 185 186 PetscCall(PetscOptionsViewer("-ts_monitor_wall_force", "Viewer for force on each (no-slip) wall", NULL, &app_ctx->wall_forces.viewer, 187 &app_ctx->wall_forces.viewer_format, NULL)); 188 189 // SGS Model Options 190 app_ctx->sgs_model_type = SGS_MODEL_NONE; 191 PetscCall(PetscOptionsEnum("-sgs_model_type", "Subgrid Stress Model type", NULL, SGSModelTypes, (PetscEnum)app_ctx->sgs_model_type, 192 (PetscEnum *)&app_ctx->sgs_model_type, NULL)); 193 194 PetscCall(PetscOptionsBool("-diff_filter_monitor", "Enable differential filtering TSMonitor", NULL, app_ctx->diff_filter_monitor, 195 &app_ctx->diff_filter_monitor, NULL)); 196 197 // Mesh Transformation Options 198 app_ctx->mesh_transform_type = MESH_TRANSFORM_NONE; 199 PetscCall(PetscOptionsEnum("-mesh_transform", "Mesh transform to perform", NULL, MeshTransformTypes, (PetscEnum)app_ctx->mesh_transform_type, 200 (PetscEnum *)&app_ctx->mesh_transform_type, NULL)); 201 202 PetscCall( 203 PetscOptionsBool("-sgs_train_enable", "Enable Data-Driven SGS training", NULL, app_ctx->sgs_train_enable, &app_ctx->sgs_train_enable, NULL)); 204 205 PetscCall(PetscOptionsEnum("-div_diff_flux_projection_method", "Method of divergence of diffusive flux projection", NULL, 206 DivDiffFluxProjectionMethods, (PetscEnum)(app_ctx->divFdiffproj_method), (PetscEnum *)&app_ctx->divFdiffproj_method, 207 NULL)); 208 209 app_ctx->check_step_interval = -1; 210 PetscCall(PetscOptionsDeprecated("-ts_monitor_nan_interval", "-honee_check_step_interval", "HONEE 0.0", NULL)); 211 PetscCall(PetscOptionsInt("-honee_check_step_interval", "Number of timesteps between verifying the validity of the solution", NULL, 212 app_ctx->check_step_interval, &app_ctx->check_step_interval, NULL)); 213 214 PetscOptionsEnd(); 215 PetscFunctionReturn(PETSC_SUCCESS); 216 } 217