1ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors. 2ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause 3a515125bSLeila Ghaffari 4a515125bSLeila Ghaffari /// @file 5ea615d4cSJames Wright /// Command line option processing for HONEE 6a515125bSLeila Ghaffari 7354560d1SJames Wright #include <ctype.h> 8a467869fSJed Brown #include <petscdevice.h> 9e419654dSJeremy L Thompson #include <petscsys.h> 10a467869fSJed Brown 11149fb536SJames Wright #include <navierstokes.h> 12a515125bSLeila Ghaffari 13a515125bSLeila Ghaffari // Register problems to be available on the command line 145907cb7eSJames Wright static PetscErrorCode RegisterProblems_NS(AppCtx app_ctx) { 15a515125bSLeila Ghaffari app_ctx->problems = NULL; 16a515125bSLeila Ghaffari 1706f41313SJames Wright PetscFunctionBeginUser; 182b916ea7SJeremy L Thompson PetscCall(PetscFunctionListAdd(&app_ctx->problems, "density_current", NS_DENSITY_CURRENT)); 192b916ea7SJeremy L Thompson PetscCall(PetscFunctionListAdd(&app_ctx->problems, "euler_vortex", NS_EULER_VORTEX)); 202b916ea7SJeremy L Thompson PetscCall(PetscFunctionListAdd(&app_ctx->problems, "shocktube", NS_SHOCKTUBE)); 212b916ea7SJeremy L Thompson PetscCall(PetscFunctionListAdd(&app_ctx->problems, "advection", NS_ADVECTION)); 222b916ea7SJeremy L Thompson PetscCall(PetscFunctionListAdd(&app_ctx->problems, "blasius", NS_BLASIUS)); 232b916ea7SJeremy L Thompson PetscCall(PetscFunctionListAdd(&app_ctx->problems, "channel", NS_CHANNEL)); 24e7754af5SKenneth E. Jansen PetscCall(PetscFunctionListAdd(&app_ctx->problems, "gaussian_wave", NS_GAUSSIAN_WAVE)); 25b8fb7609SAdeleke O. Bankole PetscCall(PetscFunctionListAdd(&app_ctx->problems, "newtonian", NS_NEWTONIAN_IG)); 26692bc0d9SJames Wright PetscCall(PetscFunctionListAdd(&app_ctx->problems, "taylor_green", NS_TAYLOR_GREEN)); 27d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 28a515125bSLeila Ghaffari } 29a515125bSLeila Ghaffari 30ad676c0bSJames Wright /** 31ad676c0bSJames Wright @brief Convert ISO 8601 time string to duration in seconds 32ad676c0bSJames Wright 33ad676c0bSJames Wright Accepted formats are 'hh', 'hh:mm', and 'hh:mm:ss'. 34ad676c0bSJames Wright 35ad676c0bSJames Wright @param[in] comm MPI_Comm for error handling 36ad676c0bSJames Wright @param[in] string string of the ISO 8601 duration 37ad676c0bSJames Wright @param[out] duration Duration in number of seconds 38ad676c0bSJames Wright **/ 39ad676c0bSJames Wright PetscErrorCode ISO8601TimeDurationToSeconds(MPI_Comm comm, const char *string, time_t *duration) { 40ad676c0bSJames Wright int num_items; 41ad676c0bSJames Wright char **entries; 42ad676c0bSJames Wright 43ad676c0bSJames Wright PetscFunctionBeginUser; 44ad676c0bSJames Wright if (string[0] == '\0') { 45ad676c0bSJames Wright *duration = 0; 46ad676c0bSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 47ad676c0bSJames Wright } 48ad676c0bSJames Wright for (PetscInt i = 0; i < strlen(string); i++) { 49ad676c0bSJames Wright PetscCheck(isdigit(string[i]) || string[i] == ':', comm, PETSC_ERR_SUP, "Time duration may only include digits and ':' separator, found '%c'", 50ad676c0bSJames Wright string[i]); 51ad676c0bSJames Wright } 52ad676c0bSJames Wright PetscCall(PetscStrToArray(string, ':', &num_items, &entries)); 53ad676c0bSJames Wright switch (num_items) { 54ad676c0bSJames Wright case 1: // Only hours 55ad676c0bSJames Wright *duration = 60 * 60 * atoi(entries[0]); 56ad676c0bSJames Wright break; 57ad676c0bSJames Wright case 2: // Hours and Minutes 58ad676c0bSJames Wright *duration = 60 * 60 * atoi(entries[0]) + 60 * atoi(entries[1]); 59ad676c0bSJames Wright break; 60ad676c0bSJames Wright case 3: // Hours, Minutes, and Seconds 61ad676c0bSJames Wright *duration = 60 * 60 * atoi(entries[0]) + 60 * atoi(entries[1]) + atoi(entries[2]); 62ad676c0bSJames Wright break; 63ad676c0bSJames Wright default: 64ad676c0bSJames Wright SETERRQ(comm, PETSC_ERR_SUP, "Recieved %d ':' delimited entries, expect either 1, 2, or 3", num_items); 65ad676c0bSJames Wright } 66ad676c0bSJames Wright PetscCall(PetscStrToArrayDestroy(num_items, entries)); 67ad676c0bSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 68ad676c0bSJames Wright } 69ad676c0bSJames Wright 70a515125bSLeila Ghaffari // Process general command line options 71d3c60affSJames Wright PetscErrorCode ProcessCommandLineOptions(Honee honee) { 7273170398SJames Wright MPI_Comm comm = honee->comm; 7373170398SJames Wright AppCtx app_ctx = honee->app_ctx; 74a515125bSLeila Ghaffari PetscBool ceed_flag = PETSC_FALSE; 75a515125bSLeila Ghaffari PetscBool problem_flag = PETSC_FALSE; 7691a36801SJames Wright PetscBool option_set = PETSC_FALSE; 772b916ea7SJeremy L Thompson 78a515125bSLeila Ghaffari PetscFunctionBeginUser; 79b1489633SJames Wright { 80b1489633SJames Wright PetscInt num_options; 81dba12ed7SJames Wright PetscBool help_set; 82b1489633SJames Wright 83dba12ed7SJames Wright PetscCall(PetscOptionsHasHelp(NULL, &help_set)); 84dba12ed7SJames Wright if (help_set) { 85dba12ed7SJames Wright PetscCall(PetscOptionsSetValue(NULL, "-ts_max_steps", "0")); 86dba12ed7SJames Wright } else { 87b1489633SJames Wright PetscCall(PetscOptionsLeftGet(NULL, &num_options, NULL, NULL)); 88b1489633SJames Wright PetscCheck(num_options > 0, comm, PETSC_ERR_USER_INPUT, 89b1489633SJames Wright "Command line options required." 90b1489633SJames Wright " Please consult the documentation to see which options are required."); 91b1489633SJames Wright PetscCall(PetscOptionsLeftRestore(NULL, &num_options, NULL, NULL)); 92b1489633SJames Wright } 93dba12ed7SJames Wright } 94b1489633SJames Wright 955907cb7eSJames Wright PetscCall(RegisterProblems_NS(app_ctx)); 9615747f0fSJames Wright PetscOptionsBegin(comm, NULL, "HONEE - High-Order Navier-stokes Equation Evaluator", NULL); 97a515125bSLeila Ghaffari 982b916ea7SJeremy L Thompson PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, app_ctx->ceed_resource, app_ctx->ceed_resource, 992b916ea7SJeremy L Thompson sizeof(app_ctx->ceed_resource), &ceed_flag)); 100a515125bSLeila Ghaffari 1010e1e9333SJames Wright app_ctx->test_type = TESTTYPE_NONE; 102*14bd2a07SJames Wright PetscCall( 103*14bd2a07SJames Wright PetscOptionsEnum("-test_type", "Type of test to run", NULL, TestTypes, (PetscEnum)app_ctx->test_type, (PetscEnum *)&app_ctx->test_type, NULL)); 104a515125bSLeila Ghaffari 105a515125bSLeila Ghaffari app_ctx->test_tol = 1E-11; 1062b916ea7SJeremy L Thompson PetscCall(PetscOptionsScalar("-compare_final_state_atol", "Test absolute tolerance", NULL, app_ctx->test_tol, &app_ctx->test_tol, NULL)); 107a515125bSLeila Ghaffari 108c931fa59SJames Wright PetscCall(PetscOptionsString("-compare_final_state_filename", "Test filename", NULL, app_ctx->test_file_path, app_ctx->test_file_path, 109c931fa59SJames Wright sizeof(app_ctx->test_file_path), NULL)); 110a515125bSLeila Ghaffari 1112b916ea7SJeremy L Thompson PetscCall(PetscOptionsFList("-problem", "Problem to solve", NULL, app_ctx->problems, app_ctx->problem_name, app_ctx->problem_name, 1122b916ea7SJeremy L Thompson sizeof(app_ctx->problem_name), &problem_flag)); 113a515125bSLeila Ghaffari 114a515125bSLeila Ghaffari app_ctx->viz_refine = 0; 1152b916ea7SJeremy L Thompson PetscCall(PetscOptionsInt("-viz_refine", "Regular refinement levels for visualization", NULL, app_ctx->viz_refine, &app_ctx->viz_refine, NULL)); 116a515125bSLeila Ghaffari 117ef55efadSJames Wright app_ctx->checkpoint_interval = 0; 118852e5969SJed Brown app_ctx->checkpoint_vtk = PETSC_FALSE; 119852e5969SJed Brown PetscCall(PetscOptionsDeprecated("-output_freq", "-checkpoint_interval", "libCEED 0.11.1", "Use -checkpoint_vtk true to include VTK output")); 120852e5969SJed Brown PetscCall(PetscOptionsInt("-output_freq", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval, 121852e5969SJed Brown &app_ctx->checkpoint_interval, &option_set)); 122852e5969SJed Brown if (option_set) app_ctx->checkpoint_vtk = PETSC_TRUE; 123852e5969SJed Brown PetscCall(PetscOptionsInt("-checkpoint_interval", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval, 124852e5969SJed Brown &app_ctx->checkpoint_interval, NULL)); 125852e5969SJed Brown PetscCall(PetscOptionsBool("-checkpoint_vtk", "Include VTK (*.vtu) output at each binary checkpoint", NULL, app_ctx->checkpoint_vtk, 126852e5969SJed Brown &app_ctx->checkpoint_vtk, NULL)); 127a515125bSLeila Ghaffari 1282b916ea7SJeremy L Thompson PetscCall(PetscOptionsBool("-output_add_stepnum2bin", "Add step number to the binary outputs", NULL, app_ctx->add_stepnum2bin, 1292b916ea7SJeremy L Thompson &app_ctx->add_stepnum2bin, NULL)); 13091a36801SJames Wright 13191a36801SJames Wright PetscCall(PetscStrncpy(app_ctx->output_dir, ".", 2)); 1322b916ea7SJeremy L Thompson PetscCall(PetscOptionsString("-output_dir", "Output directory", NULL, app_ctx->output_dir, app_ctx->output_dir, sizeof(app_ctx->output_dir), NULL)); 133c500636fSJames Wright PetscMPIInt rank; 134c500636fSJames Wright MPI_Comm_rank(comm, &rank); 135c500636fSJames Wright if (!rank) PetscCall(PetscMkdir(app_ctx->output_dir)); 13691a36801SJames Wright 1372b916ea7SJeremy L Thompson PetscCall(PetscOptionsString("-continue_filename", "Filename to get initial condition from", NULL, app_ctx->cont_file, app_ctx->cont_file, 138481d14cbSJames Wright sizeof(app_ctx->cont_file), NULL)); 139481d14cbSJames Wright if (app_ctx->cont_file[0] != '\0') app_ctx->use_continue_file = PETSC_TRUE; 14091a36801SJames Wright 141ea2beb2dSJames Wright PetscCall( 142481d14cbSJames Wright PetscOptionsDeprecated("-continue", NULL, "HONEE 0.0.0", "Set -continue_filename to non-empty string to continue from previous solution")); 143481d14cbSJames Wright PetscCall( 144ea2beb2dSJames Wright PetscOptionsDeprecated("-continue_time_filename", NULL, "HONEE 0.0.0", "HONEE no longer supports reading in solution times from binary file")); 14591a36801SJames Wright 146a515125bSLeila Ghaffari app_ctx->degree = 1; 1472b916ea7SJeremy L Thompson PetscCall(PetscOptionsInt("-degree", "Polynomial degree of finite elements", NULL, app_ctx->degree, &app_ctx->degree, NULL)); 148a515125bSLeila Ghaffari 1491219168aSLeila Ghaffari app_ctx->q_extra = 0; 1502b916ea7SJeremy L Thompson PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, app_ctx->q_extra, &app_ctx->q_extra, NULL)); 151a515125bSLeila Ghaffari 152b107fddaSJed Brown { 153b107fddaSJed Brown PetscBool option_set; 154b107fddaSJed Brown char amat_type[256] = ""; 1552b916ea7SJeremy L Thompson PetscCall(PetscOptionsFList("-amat_type", "Set the type of Amat distinct from Pmat (-dm_mat_type)", NULL, MatList, amat_type, amat_type, 1562b916ea7SJeremy L Thompson sizeof(amat_type), &option_set)); 1572b916ea7SJeremy L Thompson if (option_set) PetscCall(PetscStrallocpy(amat_type, (char **)&app_ctx->amat_type)); 158b107fddaSJed Brown } 1591024319bSJames Wright { 1601024319bSJames Wright PetscBool option_set; 1611024319bSJames Wright PetscCall(PetscOptionsHasName(NULL, NULL, "-pmat_pbdiagonal", &option_set)); 1621024319bSJames Wright if (option_set) PetscCall(PetscPrintf(comm, "Warning! -pmat_pbdiagonal no longer used. Pmat assembly determined from -pc_type setting\n")); 1631024319bSJames Wright } 164b107fddaSJed Brown 165a515125bSLeila Ghaffari // Provide default ceed resource if not specified 166a515125bSLeila Ghaffari if (!ceed_flag) { 167a515125bSLeila Ghaffari const char *ceed_resource = "/cpu/self"; 168a515125bSLeila Ghaffari strncpy(app_ctx->ceed_resource, ceed_resource, 10); 169a515125bSLeila Ghaffari } 170a467869fSJed Brown // If we request a GPU, make sure PETSc has initialized its device (which is 171a467869fSJed Brown // MPI-aware in case multiple devices are available) before CeedInit so that 172a467869fSJed Brown // PETSc and libCEED agree about which device to use. 173a467869fSJed Brown if (strncmp(app_ctx->ceed_resource, "/gpu", 4) == 0) PetscCall(PetscDeviceInitialize(PETSC_DEVICE_DEFAULT())); 174a515125bSLeila Ghaffari 175a515125bSLeila Ghaffari // Provide default problem if not specified 176a515125bSLeila Ghaffari if (!problem_flag) { 177a515125bSLeila Ghaffari const char *problem_name = "density_current"; 178a515125bSLeila Ghaffari strncpy(app_ctx->problem_name, problem_name, 16); 179a515125bSLeila Ghaffari } 180a515125bSLeila Ghaffari 181c5e9980aSAdeleke O. Bankole PetscCall(PetscOptionsViewer("-ts_monitor_wall_force", "Viewer for force on each (no-slip) wall", NULL, &app_ctx->wall_forces.viewer, 182c5e9980aSAdeleke O. Bankole &app_ctx->wall_forces.viewer_format, NULL)); 183c5e9980aSAdeleke O. Bankole 18401ab89c1SJames Wright // SGS Model Options 18501ab89c1SJames Wright app_ctx->sgs_model_type = SGS_MODEL_NONE; 18601ab89c1SJames Wright PetscCall(PetscOptionsEnum("-sgs_model_type", "Subgrid Stress Model type", NULL, SGSModelTypes, (PetscEnum)app_ctx->sgs_model_type, 18701ab89c1SJames Wright (PetscEnum *)&app_ctx->sgs_model_type, NULL)); 18801ab89c1SJames Wright 189f31f4833SJames Wright // Mesh Transformation Options 190f31f4833SJames Wright app_ctx->mesh_transform_type = MESH_TRANSFORM_NONE; 191f31f4833SJames Wright PetscCall(PetscOptionsEnum("-mesh_transform", "Mesh transform to perform", NULL, MeshTransformTypes, (PetscEnum)app_ctx->mesh_transform_type, 192f31f4833SJames Wright (PetscEnum *)&app_ctx->mesh_transform_type, NULL)); 193f31f4833SJames Wright 1941c17f66aSJames Wright PetscCall( 1951c17f66aSJames Wright PetscOptionsBool("-sgs_train_enable", "Enable Data-Driven SGS training", NULL, app_ctx->sgs_train_enable, &app_ctx->sgs_train_enable, NULL)); 1964c6ae86eSJames Wright if (app_ctx->sgs_train_enable) honee->set_poststep = PETSC_TRUE; 1971c17f66aSJames Wright 1988c85b835SJames Wright PetscCall(PetscOptionsEnum("-div_diff_flux_projection_method", "Method of divergence of diffusive flux projection", NULL, 199*14bd2a07SJames Wright DivDiffFluxProjectionMethods, (PetscEnum)app_ctx->divFdiffproj_method, (PetscEnum *)&app_ctx->divFdiffproj_method, 2008c85b835SJames Wright NULL)); 2018c85b835SJames Wright 2028b774af8SJames Wright app_ctx->check_step_interval = -1; 2038b774af8SJames Wright PetscCall(PetscOptionsDeprecated("-ts_monitor_nan_interval", "-honee_check_step_interval", "HONEE 0.0", NULL)); 2048b774af8SJames Wright PetscCall(PetscOptionsInt("-honee_check_step_interval", "Number of timesteps between verifying the validity of the solution", NULL, 2058b774af8SJames Wright app_ctx->check_step_interval, &app_ctx->check_step_interval, NULL)); 2064c6ae86eSJames Wright if (app_ctx->check_step_interval > 0) honee->set_poststep = PETSC_TRUE; 2078b774af8SJames Wright 208354560d1SJames Wright { 209354560d1SJames Wright char buffer[2048] = "0"; 210354560d1SJames Wright time_t max_wall_time_buffer, max_wall_time_duration; 211354560d1SJames Wright PetscCall(PetscOptionsString("-honee_max_wall_time_duration", "Maximum wall time duration HONEE should wait before stopping TSSolve", NULL, 212354560d1SJames Wright buffer, buffer, sizeof(buffer), NULL)); 213354560d1SJames Wright PetscCall(ISO8601TimeDurationToSeconds(comm, buffer, &max_wall_time_duration)); 214354560d1SJames Wright PetscCall(PetscStrncpy(buffer, "0:1", sizeof(buffer))); // Default 1 minute buffer 215354560d1SJames Wright PetscCall(PetscOptionsString("-honee_max_wall_time_buffer", 216354560d1SJames Wright "Time before max_wall_time_duration when TSSolve should be stopped and checkpoint files written", NULL, buffer, 217354560d1SJames Wright buffer, sizeof(buffer), NULL)); 218354560d1SJames Wright PetscCall(ISO8601TimeDurationToSeconds(comm, buffer, &max_wall_time_buffer)); 219354560d1SJames Wright if (max_wall_time_duration == 0) honee->max_wall_time = -1; 2204c6ae86eSJames Wright else { 2214c6ae86eSJames Wright honee->set_poststep = PETSC_TRUE; 2224c6ae86eSJames Wright honee->max_wall_time = honee->start_time + max_wall_time_duration - max_wall_time_buffer; 2234c6ae86eSJames Wright } 224354560d1SJames Wright 225354560d1SJames Wright honee->max_wall_time_interval = 1; 226354560d1SJames Wright PetscCall(PetscOptionsInt("-honee_max_wall_time_interval", 227354560d1SJames Wright "Number of timesteps between checking whether TSSolve should be stopped due to max_wall_time", NULL, 228354560d1SJames Wright honee->max_wall_time_interval, &honee->max_wall_time_interval, NULL)); 229354560d1SJames Wright } 230c9f37605SMohammed Amin { 231c9f37605SMohammed Amin PetscScalar meter = 1.0; 232c9f37605SMohammed Amin PetscScalar second = 1.0; 233c9f37605SMohammed Amin PetscScalar kilogram = 1.0; 234c9f37605SMohammed Amin PetscScalar Kelvin = 1.0; 235354560d1SJames Wright 236c9f37605SMohammed Amin PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL)); 237c9f37605SMohammed Amin PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL)); 238c9f37605SMohammed Amin PetscCall(PetscOptionsScalar("-units_kilogram", "1 kilogram in scaled mass units", NULL, kilogram, &kilogram, NULL)); 239c9f37605SMohammed Amin PetscCall(PetscOptionsScalar("-units_kelvin", "1 Kelvin in scaled temperature units", NULL, Kelvin, &Kelvin, NULL)); 240c9f37605SMohammed Amin 241c9f37605SMohammed Amin Units units = honee->units; 242c9f37605SMohammed Amin units->meter = meter; 243c9f37605SMohammed Amin units->second = second; 244c9f37605SMohammed Amin units->kilogram = kilogram; 245c9f37605SMohammed Amin units->Kelvin = Kelvin; 246c9f37605SMohammed Amin 247c9f37605SMohammed Amin units->Pascal = kilogram / (meter * PetscSqr(second)); 248c9f37605SMohammed Amin units->Joule = kilogram * PetscSqr(meter) / PetscSqr(second); 249c9f37605SMohammed Amin units->J_per_kg_K = units->Joule / (kilogram * Kelvin); 250c9f37605SMohammed Amin units->m_per_squared_s = meter / PetscSqr(second); 251c9f37605SMohammed Amin units->W_per_m_K = units->Joule / (second * meter * Kelvin); 252c9f37605SMohammed Amin } 2531485969bSJeremy L Thompson PetscOptionsEnd(); 254d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 255a515125bSLeila Ghaffari } 256