xref: /honee/src/cloptions.c (revision 354560d100c3151a85e6abf4f71c41dd4f95bc3d)
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
5a515125bSLeila Ghaffari /// Command line option processing for Navier-Stokes example using PETSc
6a515125bSLeila Ghaffari 
7*354560d1SJames 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
7173170398SJames Wright PetscErrorCode ProcessCommandLineOptions(Honee honee, SimpleBC bc) {
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;
81b1489633SJames Wright 
82b1489633SJames Wright     PetscCall(PetscOptionsLeftGet(NULL, &num_options, NULL, NULL));
83b1489633SJames Wright     PetscCheck(num_options > 0, comm, PETSC_ERR_USER_INPUT,
84b1489633SJames Wright                "Command line options required."
85b1489633SJames Wright                " Please consult the documentation to see which options are required.");
86b1489633SJames Wright     PetscCall(PetscOptionsLeftRestore(NULL, &num_options, NULL, NULL));
87b1489633SJames Wright   }
88b1489633SJames Wright 
895907cb7eSJames Wright   PetscCall(RegisterProblems_NS(app_ctx));
9015747f0fSJames Wright   PetscOptionsBegin(comm, NULL, "HONEE - High-Order Navier-stokes Equation Evaluator", NULL);
91a515125bSLeila Ghaffari 
922b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, app_ctx->ceed_resource, app_ctx->ceed_resource,
932b916ea7SJeremy L Thompson                                sizeof(app_ctx->ceed_resource), &ceed_flag));
94a515125bSLeila Ghaffari 
950e1e9333SJames Wright   app_ctx->test_type = TESTTYPE_NONE;
960e1e9333SJames Wright   PetscCall(PetscOptionsEnum("-test_type", "Type of test to run", NULL, TestTypes, (PetscEnum)(app_ctx->test_type), (PetscEnum *)&app_ctx->test_type,
970e1e9333SJames Wright                              NULL));
98a515125bSLeila Ghaffari 
99a515125bSLeila Ghaffari   app_ctx->test_tol = 1E-11;
1002b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-compare_final_state_atol", "Test absolute tolerance", NULL, app_ctx->test_tol, &app_ctx->test_tol, NULL));
101a515125bSLeila Ghaffari 
102c931fa59SJames Wright   PetscCall(PetscOptionsString("-compare_final_state_filename", "Test filename", NULL, app_ctx->test_file_path, app_ctx->test_file_path,
103c931fa59SJames Wright                                sizeof(app_ctx->test_file_path), NULL));
104a515125bSLeila Ghaffari 
1052b916ea7SJeremy L Thompson   PetscCall(PetscOptionsFList("-problem", "Problem to solve", NULL, app_ctx->problems, app_ctx->problem_name, app_ctx->problem_name,
1062b916ea7SJeremy L Thompson                               sizeof(app_ctx->problem_name), &problem_flag));
107a515125bSLeila Ghaffari 
108a515125bSLeila Ghaffari   app_ctx->viz_refine = 0;
1092b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-viz_refine", "Regular refinement levels for visualization", NULL, app_ctx->viz_refine, &app_ctx->viz_refine, NULL));
110a515125bSLeila Ghaffari 
111852e5969SJed Brown   app_ctx->checkpoint_interval = 10;
112852e5969SJed Brown   app_ctx->checkpoint_vtk      = PETSC_FALSE;
113852e5969SJed Brown   PetscCall(PetscOptionsDeprecated("-output_freq", "-checkpoint_interval", "libCEED 0.11.1", "Use -checkpoint_vtk true to include VTK output"));
114852e5969SJed Brown   PetscCall(PetscOptionsInt("-output_freq", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
115852e5969SJed Brown                             &app_ctx->checkpoint_interval, &option_set));
116852e5969SJed Brown   if (option_set) app_ctx->checkpoint_vtk = PETSC_TRUE;
117852e5969SJed Brown   PetscCall(PetscOptionsInt("-checkpoint_interval", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
118852e5969SJed Brown                             &app_ctx->checkpoint_interval, NULL));
119852e5969SJed Brown   PetscCall(PetscOptionsBool("-checkpoint_vtk", "Include VTK (*.vtu) output at each binary checkpoint", NULL, app_ctx->checkpoint_vtk,
120852e5969SJed Brown                              &app_ctx->checkpoint_vtk, NULL));
121a515125bSLeila Ghaffari 
1222b916ea7SJeremy L Thompson   PetscCall(PetscOptionsBool("-output_add_stepnum2bin", "Add step number to the binary outputs", NULL, app_ctx->add_stepnum2bin,
1232b916ea7SJeremy L Thompson                              &app_ctx->add_stepnum2bin, NULL));
12491a36801SJames Wright 
12591a36801SJames Wright   PetscCall(PetscStrncpy(app_ctx->output_dir, ".", 2));
1262b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-output_dir", "Output directory", NULL, app_ctx->output_dir, app_ctx->output_dir, sizeof(app_ctx->output_dir), NULL));
127c500636fSJames Wright   PetscMPIInt rank;
128c500636fSJames Wright   MPI_Comm_rank(comm, &rank);
129c500636fSJames Wright   if (!rank) PetscCall(PetscMkdir(app_ctx->output_dir));
13091a36801SJames Wright 
1312b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-continue_filename", "Filename to get initial condition from", NULL, app_ctx->cont_file, app_ctx->cont_file,
132481d14cbSJames Wright                                sizeof(app_ctx->cont_file), NULL));
133481d14cbSJames Wright   if (app_ctx->cont_file[0] != '\0') app_ctx->use_continue_file = PETSC_TRUE;
13491a36801SJames Wright 
135ea2beb2dSJames Wright   PetscCall(
136481d14cbSJames Wright       PetscOptionsDeprecated("-continue", NULL, "HONEE 0.0.0", "Set -continue_filename to non-empty string to continue from previous solution"));
137481d14cbSJames Wright   PetscCall(
138ea2beb2dSJames Wright       PetscOptionsDeprecated("-continue_time_filename", NULL, "HONEE 0.0.0", "HONEE no longer supports reading in solution times from binary file"));
13991a36801SJames Wright 
140a515125bSLeila Ghaffari   app_ctx->degree = 1;
1412b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-degree", "Polynomial degree of finite elements", NULL, app_ctx->degree, &app_ctx->degree, NULL));
142a515125bSLeila Ghaffari 
1431219168aSLeila Ghaffari   app_ctx->q_extra = 0;
1442b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, app_ctx->q_extra, &app_ctx->q_extra, NULL));
145a515125bSLeila Ghaffari 
146b107fddaSJed Brown   {
147b107fddaSJed Brown     PetscBool option_set;
148b107fddaSJed Brown     char      amat_type[256] = "";
1492b916ea7SJeremy L Thompson     PetscCall(PetscOptionsFList("-amat_type", "Set the type of Amat distinct from Pmat (-dm_mat_type)", NULL, MatList, amat_type, amat_type,
1502b916ea7SJeremy L Thompson                                 sizeof(amat_type), &option_set));
1512b916ea7SJeremy L Thompson     if (option_set) PetscCall(PetscStrallocpy(amat_type, (char **)&app_ctx->amat_type));
152b107fddaSJed Brown   }
1531024319bSJames Wright   {
1541024319bSJames Wright     PetscBool option_set;
1551024319bSJames Wright     PetscCall(PetscOptionsHasName(NULL, NULL, "-pmat_pbdiagonal", &option_set));
1561024319bSJames Wright     if (option_set) PetscCall(PetscPrintf(comm, "Warning! -pmat_pbdiagonal no longer used. Pmat assembly determined from -pc_type setting\n"));
1571024319bSJames Wright   }
158b107fddaSJed Brown 
159a515125bSLeila Ghaffari   // Provide default ceed resource if not specified
160a515125bSLeila Ghaffari   if (!ceed_flag) {
161a515125bSLeila Ghaffari     const char *ceed_resource = "/cpu/self";
162a515125bSLeila Ghaffari     strncpy(app_ctx->ceed_resource, ceed_resource, 10);
163a515125bSLeila Ghaffari   }
164a467869fSJed Brown   // If we request a GPU, make sure PETSc has initialized its device (which is
165a467869fSJed Brown   // MPI-aware in case multiple devices are available) before CeedInit so that
166a467869fSJed Brown   // PETSc and libCEED agree about which device to use.
167a467869fSJed Brown   if (strncmp(app_ctx->ceed_resource, "/gpu", 4) == 0) PetscCall(PetscDeviceInitialize(PETSC_DEVICE_DEFAULT()));
168a515125bSLeila Ghaffari 
169a515125bSLeila Ghaffari   // Provide default problem if not specified
170a515125bSLeila Ghaffari   if (!problem_flag) {
171a515125bSLeila Ghaffari     const char *problem_name = "density_current";
172a515125bSLeila Ghaffari     strncpy(app_ctx->problem_name, problem_name, 16);
173a515125bSLeila Ghaffari   }
174a515125bSLeila Ghaffari 
175b0488d1fSJames Wright   // Statistics Options
176c931fa59SJames Wright   app_ctx->turb_spanstats_collect_interval = 1;
177c931fa59SJames Wright   PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_collect_interval", "Number of timesteps between statistics collection", NULL,
178c931fa59SJames Wright                             app_ctx->turb_spanstats_collect_interval, &app_ctx->turb_spanstats_collect_interval, NULL));
179b0488d1fSJames Wright 
180c931fa59SJames Wright   app_ctx->turb_spanstats_viewer_interval = -1;
181c931fa59SJames Wright   PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_viewer_interval", "Number of timesteps between statistics viewer writing", NULL,
182c931fa59SJames Wright                             app_ctx->turb_spanstats_viewer_interval, &app_ctx->turb_spanstats_viewer_interval, NULL));
183b0488d1fSJames Wright 
184c931fa59SJames Wright   PetscCall(PetscOptionsViewer("-ts_monitor_turbulence_spanstats_viewer", "Viewer for the statistics", NULL, &app_ctx->turb_spanstats_viewer,
185c931fa59SJames Wright                                &app_ctx->turb_spanstats_viewer_format, &app_ctx->turb_spanstats_enable));
186b0488d1fSJames Wright 
187c5e9980aSAdeleke O. Bankole   PetscCall(PetscOptionsViewer("-ts_monitor_wall_force", "Viewer for force on each (no-slip) wall", NULL, &app_ctx->wall_forces.viewer,
188c5e9980aSAdeleke O. Bankole                                &app_ctx->wall_forces.viewer_format, NULL));
189c5e9980aSAdeleke O. Bankole 
19001ab89c1SJames Wright   // SGS Model Options
19101ab89c1SJames Wright   app_ctx->sgs_model_type = SGS_MODEL_NONE;
19201ab89c1SJames Wright   PetscCall(PetscOptionsEnum("-sgs_model_type", "Subgrid Stress Model type", NULL, SGSModelTypes, (PetscEnum)app_ctx->sgs_model_type,
19301ab89c1SJames Wright                              (PetscEnum *)&app_ctx->sgs_model_type, NULL));
19401ab89c1SJames Wright 
19588b07121SJames Wright   PetscCall(PetscOptionsBool("-diff_filter_monitor", "Enable differential filtering TSMonitor", NULL, app_ctx->diff_filter_monitor,
19688b07121SJames Wright                              &app_ctx->diff_filter_monitor, NULL));
19788b07121SJames Wright 
198f31f4833SJames Wright   // Mesh Transformation Options
199f31f4833SJames Wright   app_ctx->mesh_transform_type = MESH_TRANSFORM_NONE;
200f31f4833SJames Wright   PetscCall(PetscOptionsEnum("-mesh_transform", "Mesh transform to perform", NULL, MeshTransformTypes, (PetscEnum)app_ctx->mesh_transform_type,
201f31f4833SJames Wright                              (PetscEnum *)&app_ctx->mesh_transform_type, NULL));
202f31f4833SJames Wright 
2031c17f66aSJames Wright   PetscCall(
2041c17f66aSJames Wright       PetscOptionsBool("-sgs_train_enable", "Enable Data-Driven SGS training", NULL, app_ctx->sgs_train_enable, &app_ctx->sgs_train_enable, NULL));
2051c17f66aSJames Wright 
2068c85b835SJames Wright   PetscCall(PetscOptionsEnum("-div_diff_flux_projection_method", "Method of divergence of diffusive flux projection", NULL,
2078c85b835SJames Wright                              DivDiffFluxProjectionMethods, (PetscEnum)(app_ctx->divFdiffproj_method), (PetscEnum *)&app_ctx->divFdiffproj_method,
2088c85b835SJames Wright                              NULL));
2098c85b835SJames Wright 
2108b774af8SJames Wright   app_ctx->check_step_interval = -1;
2118b774af8SJames Wright   PetscCall(PetscOptionsDeprecated("-ts_monitor_nan_interval", "-honee_check_step_interval", "HONEE 0.0", NULL));
2128b774af8SJames Wright   PetscCall(PetscOptionsInt("-honee_check_step_interval", "Number of timesteps between verifying the validity of the solution", NULL,
2138b774af8SJames Wright                             app_ctx->check_step_interval, &app_ctx->check_step_interval, NULL));
2148b774af8SJames Wright 
215*354560d1SJames Wright   {
216*354560d1SJames Wright     char   buffer[2048] = "0";
217*354560d1SJames Wright     time_t max_wall_time_buffer, max_wall_time_duration;
218*354560d1SJames Wright     PetscCall(PetscOptionsString("-honee_max_wall_time_duration", "Maximum wall time duration HONEE should wait before stopping TSSolve", NULL,
219*354560d1SJames Wright                                  buffer, buffer, sizeof(buffer), NULL));
220*354560d1SJames Wright     PetscCall(ISO8601TimeDurationToSeconds(comm, buffer, &max_wall_time_duration));
221*354560d1SJames Wright     PetscCall(PetscStrncpy(buffer, "0:1", sizeof(buffer)));  // Default 1 minute buffer
222*354560d1SJames Wright     PetscCall(PetscOptionsString("-honee_max_wall_time_buffer",
223*354560d1SJames Wright                                  "Time before max_wall_time_duration when TSSolve should be stopped and checkpoint files written", NULL, buffer,
224*354560d1SJames Wright                                  buffer, sizeof(buffer), NULL));
225*354560d1SJames Wright     PetscCall(ISO8601TimeDurationToSeconds(comm, buffer, &max_wall_time_buffer));
226*354560d1SJames Wright     if (max_wall_time_duration == 0) honee->max_wall_time = -1;
227*354560d1SJames Wright     else honee->max_wall_time = honee->start_time + max_wall_time_duration - max_wall_time_buffer;
228*354560d1SJames Wright 
229*354560d1SJames Wright     honee->max_wall_time_interval = 1;
230*354560d1SJames Wright     PetscCall(PetscOptionsInt("-honee_max_wall_time_interval",
231*354560d1SJames Wright                               "Number of timesteps between checking whether TSSolve should be stopped due to max_wall_time", NULL,
232*354560d1SJames Wright                               honee->max_wall_time_interval, &honee->max_wall_time_interval, NULL));
233*354560d1SJames Wright   }
234*354560d1SJames Wright 
2351485969bSJeremy L Thompson   PetscOptionsEnd();
236d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
237a515125bSLeila Ghaffari }
238