xref: /honee/src/cloptions.c (revision 0d234a2c256ae48105718f4ac980234389e1f4a0)
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
RegisterProblems_NS(AppCtx app_ctx)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 **/
ISO8601TimeDurationToSeconds(MPI_Comm comm,const char * string,time_t * duration)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
ProcessCommandLineOptions(Honee honee)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"));
86*366f3756SJames Wright       PetscCall(PetscPrintf(comm, "\n########################################\n\n"));
87*366f3756SJames Wright       PetscCall(PetscPrintf(comm, "HONEE documentation may be found at https://honee.phypid.org\n\n"));
88*366f3756SJames Wright       PetscCall(PetscPrintf(comm, "########################################\n\n"));
89dba12ed7SJames Wright     } else {
90b1489633SJames Wright       PetscCall(PetscOptionsLeftGet(NULL, &num_options, NULL, NULL));
91b1489633SJames Wright       PetscCheck(num_options > 0, comm, PETSC_ERR_USER_INPUT,
92b1489633SJames Wright                  "Command line options required."
93*366f3756SJames Wright                  " Please consult the documentation to see which options are required."
94*366f3756SJames Wright                  " HONEE documentation may be found at https://honee.phypid.org\n");
95b1489633SJames Wright       PetscCall(PetscOptionsLeftRestore(NULL, &num_options, NULL, NULL));
96b1489633SJames Wright     }
97dba12ed7SJames Wright   }
98b1489633SJames Wright 
995907cb7eSJames Wright   PetscCall(RegisterProblems_NS(app_ctx));
10015747f0fSJames Wright   PetscOptionsBegin(comm, NULL, "HONEE - High-Order Navier-stokes Equation Evaluator", NULL);
101a515125bSLeila Ghaffari 
1022b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, app_ctx->ceed_resource, app_ctx->ceed_resource,
1032b916ea7SJeremy L Thompson                                sizeof(app_ctx->ceed_resource), &ceed_flag));
104a515125bSLeila Ghaffari 
1050e1e9333SJames Wright   app_ctx->test_type = TESTTYPE_NONE;
1069eadbee4SJames Wright   PetscCall(PetscOptionsEnum("-test_type", "Type of test to run", NULL, TestTypes, (PetscEnum)app_ctx->test_type, (PetscEnum *)&app_ctx->test_type,
1079eadbee4SJames Wright                              NULL));
108a515125bSLeila Ghaffari 
109a515125bSLeila Ghaffari   app_ctx->test_tol = 1E-11;
1102b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-compare_final_state_atol", "Test absolute tolerance", NULL, app_ctx->test_tol, &app_ctx->test_tol, NULL));
111a515125bSLeila Ghaffari 
112c931fa59SJames Wright   PetscCall(PetscOptionsString("-compare_final_state_filename", "Test filename", NULL, app_ctx->test_file_path, app_ctx->test_file_path,
113c931fa59SJames Wright                                sizeof(app_ctx->test_file_path), NULL));
114a515125bSLeila Ghaffari 
1152b916ea7SJeremy L Thompson   PetscCall(PetscOptionsFList("-problem", "Problem to solve", NULL, app_ctx->problems, app_ctx->problem_name, app_ctx->problem_name,
1162b916ea7SJeremy L Thompson                               sizeof(app_ctx->problem_name), &problem_flag));
117a515125bSLeila Ghaffari 
118a515125bSLeila Ghaffari   app_ctx->viz_refine = 0;
1192b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-viz_refine", "Regular refinement levels for visualization", NULL, app_ctx->viz_refine, &app_ctx->viz_refine, NULL));
120a515125bSLeila Ghaffari 
121ef55efadSJames Wright   app_ctx->checkpoint_interval = 0;
122852e5969SJed Brown   app_ctx->checkpoint_vtk      = PETSC_FALSE;
123852e5969SJed Brown   PetscCall(PetscOptionsDeprecated("-output_freq", "-checkpoint_interval", "libCEED 0.11.1", "Use -checkpoint_vtk true to include VTK output"));
124852e5969SJed Brown   PetscCall(PetscOptionsInt("-output_freq", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
125852e5969SJed Brown                             &app_ctx->checkpoint_interval, &option_set));
126852e5969SJed Brown   if (option_set) app_ctx->checkpoint_vtk = PETSC_TRUE;
127852e5969SJed Brown   PetscCall(PetscOptionsInt("-checkpoint_interval", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
128852e5969SJed Brown                             &app_ctx->checkpoint_interval, NULL));
129852e5969SJed Brown   PetscCall(PetscOptionsBool("-checkpoint_vtk", "Include VTK (*.vtu) output at each binary checkpoint", NULL, app_ctx->checkpoint_vtk,
130852e5969SJed Brown                              &app_ctx->checkpoint_vtk, NULL));
131a515125bSLeila Ghaffari 
1322b916ea7SJeremy L Thompson   PetscCall(PetscOptionsBool("-output_add_stepnum2bin", "Add step number to the binary outputs", NULL, app_ctx->add_stepnum2bin,
1332b916ea7SJeremy L Thompson                              &app_ctx->add_stepnum2bin, NULL));
13491a36801SJames Wright 
13591a36801SJames Wright   PetscCall(PetscStrncpy(app_ctx->output_dir, ".", 2));
1362b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-output_dir", "Output directory", NULL, app_ctx->output_dir, app_ctx->output_dir, sizeof(app_ctx->output_dir), NULL));
137c500636fSJames Wright   PetscMPIInt rank;
138c500636fSJames Wright   MPI_Comm_rank(comm, &rank);
139c500636fSJames Wright   if (!rank) PetscCall(PetscMkdir(app_ctx->output_dir));
14091a36801SJames Wright 
1412b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-continue_filename", "Filename to get initial condition from", NULL, app_ctx->cont_file, app_ctx->cont_file,
142481d14cbSJames Wright                                sizeof(app_ctx->cont_file), NULL));
143481d14cbSJames Wright   if (app_ctx->cont_file[0] != '\0') app_ctx->use_continue_file = PETSC_TRUE;
14491a36801SJames Wright 
1459eadbee4SJames Wright   PetscCall(PetscOptionsDeprecated("-continue", NULL, "HONEE 0.0.0",
1469eadbee4SJames Wright                                    "Set -continue_filename to non-empty string to continue from previous solution"));
1479eadbee4SJames Wright   PetscCall(PetscOptionsDeprecated("-continue_time_filename", NULL, "HONEE 0.0.0",
1489eadbee4SJames Wright                                    "HONEE no longer supports reading in solution times from binary file"));
14991a36801SJames Wright 
150a515125bSLeila Ghaffari   app_ctx->degree = 1;
1512b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-degree", "Polynomial degree of finite elements", NULL, app_ctx->degree, &app_ctx->degree, NULL));
152a515125bSLeila Ghaffari 
1531219168aSLeila Ghaffari   app_ctx->q_extra = 0;
1542b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, app_ctx->q_extra, &app_ctx->q_extra, NULL));
155a515125bSLeila Ghaffari 
156b107fddaSJed Brown   {
157b107fddaSJed Brown     PetscBool option_set;
158b107fddaSJed Brown     char      amat_type[256] = "";
1592b916ea7SJeremy L Thompson     PetscCall(PetscOptionsFList("-amat_type", "Set the type of Amat distinct from Pmat (-dm_mat_type)", NULL, MatList, amat_type, amat_type,
1602b916ea7SJeremy L Thompson                                 sizeof(amat_type), &option_set));
1612b916ea7SJeremy L Thompson     if (option_set) PetscCall(PetscStrallocpy(amat_type, (char **)&app_ctx->amat_type));
162b107fddaSJed Brown   }
1631024319bSJames Wright   {
1641024319bSJames Wright     PetscBool option_set;
1651024319bSJames Wright     PetscCall(PetscOptionsHasName(NULL, NULL, "-pmat_pbdiagonal", &option_set));
1661024319bSJames Wright     if (option_set) PetscCall(PetscPrintf(comm, "Warning! -pmat_pbdiagonal no longer used. Pmat assembly determined from -pc_type setting\n"));
1671024319bSJames Wright   }
168b107fddaSJed Brown 
169a515125bSLeila Ghaffari   // Provide default ceed resource if not specified
170a515125bSLeila Ghaffari   if (!ceed_flag) {
171a515125bSLeila Ghaffari     const char *ceed_resource = "/cpu/self";
172a515125bSLeila Ghaffari     strncpy(app_ctx->ceed_resource, ceed_resource, 10);
173a515125bSLeila Ghaffari   }
174a467869fSJed Brown   // If we request a GPU, make sure PETSc has initialized its device (which is
175a467869fSJed Brown   // MPI-aware in case multiple devices are available) before CeedInit so that
176a467869fSJed Brown   // PETSc and libCEED agree about which device to use.
177a467869fSJed Brown   if (strncmp(app_ctx->ceed_resource, "/gpu", 4) == 0) PetscCall(PetscDeviceInitialize(PETSC_DEVICE_DEFAULT()));
178a515125bSLeila Ghaffari 
179a515125bSLeila Ghaffari   // Provide default problem if not specified
180a515125bSLeila Ghaffari   if (!problem_flag) {
181a515125bSLeila Ghaffari     const char *problem_name = "density_current";
182a515125bSLeila Ghaffari     strncpy(app_ctx->problem_name, problem_name, 16);
183a515125bSLeila Ghaffari   }
184a515125bSLeila Ghaffari 
185c5e9980aSAdeleke O. Bankole   PetscCall(PetscOptionsViewer("-ts_monitor_wall_force", "Viewer for force on each (no-slip) wall", NULL, &app_ctx->wall_forces.viewer,
186c5e9980aSAdeleke O. Bankole                                &app_ctx->wall_forces.viewer_format, NULL));
187c5e9980aSAdeleke O. Bankole 
18801ab89c1SJames Wright   // SGS Model Options
18901ab89c1SJames Wright   app_ctx->sgs_model_type = SGS_MODEL_NONE;
19001ab89c1SJames Wright   PetscCall(PetscOptionsEnum("-sgs_model_type", "Subgrid Stress Model type", NULL, SGSModelTypes, (PetscEnum)app_ctx->sgs_model_type,
19101ab89c1SJames Wright                              (PetscEnum *)&app_ctx->sgs_model_type, NULL));
19201ab89c1SJames Wright 
1939eadbee4SJames Wright   PetscCall(PetscOptionsBool("-sgs_train_enable", "Enable Data-Driven SGS training", NULL, app_ctx->sgs_train_enable, &app_ctx->sgs_train_enable,
1949eadbee4SJames Wright                              NULL));
1954c6ae86eSJames Wright   if (app_ctx->sgs_train_enable) honee->set_poststep = PETSC_TRUE;
1961c17f66aSJames Wright 
1978c85b835SJames Wright   PetscCall(PetscOptionsEnum("-div_diff_flux_projection_method", "Method of divergence of diffusive flux projection", NULL,
19814bd2a07SJames Wright                              DivDiffFluxProjectionMethods, (PetscEnum)app_ctx->divFdiffproj_method, (PetscEnum *)&app_ctx->divFdiffproj_method,
1998c85b835SJames Wright                              NULL));
2008c85b835SJames Wright 
2018b774af8SJames Wright   app_ctx->check_step_interval = -1;
2028b774af8SJames Wright   PetscCall(PetscOptionsDeprecated("-ts_monitor_nan_interval", "-honee_check_step_interval", "HONEE 0.0", NULL));
2038b774af8SJames Wright   PetscCall(PetscOptionsInt("-honee_check_step_interval", "Number of timesteps between verifying the validity of the solution", NULL,
2048b774af8SJames Wright                             app_ctx->check_step_interval, &app_ctx->check_step_interval, NULL));
2054c6ae86eSJames Wright   if (app_ctx->check_step_interval > 0) honee->set_poststep = PETSC_TRUE;
2068b774af8SJames Wright 
207354560d1SJames Wright   {
208354560d1SJames Wright     char   buffer[2048] = "0";
209354560d1SJames Wright     time_t max_wall_time_buffer, max_wall_time_duration;
210354560d1SJames Wright     PetscCall(PetscOptionsString("-honee_max_wall_time_duration", "Maximum wall time duration HONEE should wait before stopping TSSolve", NULL,
211354560d1SJames Wright                                  buffer, buffer, sizeof(buffer), NULL));
212354560d1SJames Wright     PetscCall(ISO8601TimeDurationToSeconds(comm, buffer, &max_wall_time_duration));
213354560d1SJames Wright     PetscCall(PetscStrncpy(buffer, "0:1", sizeof(buffer)));  // Default 1 minute buffer
214354560d1SJames Wright     PetscCall(PetscOptionsString("-honee_max_wall_time_buffer",
215354560d1SJames Wright                                  "Time before max_wall_time_duration when TSSolve should be stopped and checkpoint files written", NULL, buffer,
216354560d1SJames Wright                                  buffer, sizeof(buffer), NULL));
217354560d1SJames Wright     PetscCall(ISO8601TimeDurationToSeconds(comm, buffer, &max_wall_time_buffer));
218354560d1SJames Wright     if (max_wall_time_duration == 0) honee->max_wall_time = -1;
2194c6ae86eSJames Wright     else {
2204c6ae86eSJames Wright       honee->set_poststep  = PETSC_TRUE;
2214c6ae86eSJames Wright       honee->max_wall_time = honee->start_time + max_wall_time_duration - max_wall_time_buffer;
2224c6ae86eSJames Wright     }
223354560d1SJames Wright 
224354560d1SJames Wright     honee->max_wall_time_interval = 1;
225354560d1SJames Wright     PetscCall(PetscOptionsInt("-honee_max_wall_time_interval",
226354560d1SJames Wright                               "Number of timesteps between checking whether TSSolve should be stopped due to max_wall_time", NULL,
227354560d1SJames Wright                               honee->max_wall_time_interval, &honee->max_wall_time_interval, NULL));
228354560d1SJames Wright   }
2295dd063edSJames Wright 
230c9f37605SMohammed Amin   {
231c9f37605SMohammed Amin     Units units = honee->units;
2325dd063edSJames Wright     *units      = (struct Units_private){
2335dd063edSJames Wright              .meter    = 1.0,
2345dd063edSJames Wright              .second   = 1.0,
2355dd063edSJames Wright              .kilogram = 1.0,
2365dd063edSJames Wright              .Kelvin   = 1.0,
2375dd063edSJames Wright     };
238c9f37605SMohammed Amin 
2395dd063edSJames Wright     PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, units->meter, &units->meter, NULL));
2405dd063edSJames Wright     PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, units->second, &units->second, NULL));
2415dd063edSJames Wright     PetscCall(PetscOptionsScalar("-units_kilogram", "1 kilogram in scaled mass units", NULL, units->kilogram, &units->kilogram, NULL));
2425dd063edSJames Wright     PetscCall(PetscOptionsScalar("-units_kelvin", "1 Kelvin in scaled temperature units", NULL, units->Kelvin, &units->Kelvin, NULL));
2435dd063edSJames Wright 
2445dd063edSJames Wright     units->Pascal          = units->kilogram / (units->meter * PetscSqr(units->second));
2455dd063edSJames Wright     units->Joule           = units->kilogram * PetscSqr(units->meter) / PetscSqr(units->second);
2465dd063edSJames Wright     units->J_per_kg_K      = units->Joule / (units->kilogram * units->Kelvin);
2475dd063edSJames Wright     units->m_per_squared_s = units->meter / PetscSqr(units->second);
2485dd063edSJames Wright     units->W_per_m_K       = units->Joule / (units->second * units->meter * units->Kelvin);
249c9f37605SMohammed Amin   }
2501485969bSJeremy L Thompson   PetscOptionsEnd();
251d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
252a515125bSLeila Ghaffari }
253