xref: /honee/src/cloptions.c (revision ad676c0b70d502c61bb7e8086dfae500a5c2f968)
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 
7a467869fSJed Brown #include <petscdevice.h>
8e419654dSJeremy L Thompson #include <petscsys.h>
9a467869fSJed Brown 
10149fb536SJames Wright #include <navierstokes.h>
11a515125bSLeila Ghaffari 
12a515125bSLeila Ghaffari // Register problems to be available on the command line
135907cb7eSJames Wright static PetscErrorCode RegisterProblems_NS(AppCtx app_ctx) {
14a515125bSLeila Ghaffari   app_ctx->problems = NULL;
15a515125bSLeila Ghaffari 
1606f41313SJames Wright   PetscFunctionBeginUser;
172b916ea7SJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "density_current", NS_DENSITY_CURRENT));
182b916ea7SJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "euler_vortex", NS_EULER_VORTEX));
192b916ea7SJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "shocktube", NS_SHOCKTUBE));
202b916ea7SJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "advection", NS_ADVECTION));
212b916ea7SJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "blasius", NS_BLASIUS));
222b916ea7SJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "channel", NS_CHANNEL));
23e7754af5SKenneth E. Jansen   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "gaussian_wave", NS_GAUSSIAN_WAVE));
24b8fb7609SAdeleke O. Bankole   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "newtonian", NS_NEWTONIAN_IG));
25692bc0d9SJames Wright   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "taylor_green", NS_TAYLOR_GREEN));
26d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
27a515125bSLeila Ghaffari }
28a515125bSLeila Ghaffari 
29*ad676c0bSJames Wright /**
30*ad676c0bSJames Wright   @brief Convert ISO 8601 time string to duration in seconds
31*ad676c0bSJames Wright 
32*ad676c0bSJames Wright   Accepted formats are 'hh', 'hh:mm', and 'hh:mm:ss'.
33*ad676c0bSJames Wright 
34*ad676c0bSJames Wright   @param[in]  comm     MPI_Comm for error handling
35*ad676c0bSJames Wright   @param[in]  string   string of the ISO 8601 duration
36*ad676c0bSJames Wright   @param[out] duration Duration in number of seconds
37*ad676c0bSJames Wright **/
38*ad676c0bSJames Wright PetscErrorCode ISO8601TimeDurationToSeconds(MPI_Comm comm, const char *string, time_t *duration) {
39*ad676c0bSJames Wright   int    num_items;
40*ad676c0bSJames Wright   char **entries;
41*ad676c0bSJames Wright 
42*ad676c0bSJames Wright   PetscFunctionBeginUser;
43*ad676c0bSJames Wright   if (string[0] == '\0') {
44*ad676c0bSJames Wright     *duration = 0;
45*ad676c0bSJames Wright     PetscFunctionReturn(PETSC_SUCCESS);
46*ad676c0bSJames Wright   }
47*ad676c0bSJames Wright   for (PetscInt i = 0; i < strlen(string); i++) {
48*ad676c0bSJames Wright     PetscCheck(isdigit(string[i]) || string[i] == ':', comm, PETSC_ERR_SUP, "Time duration may only include digits and ':' separator, found '%c'",
49*ad676c0bSJames Wright                string[i]);
50*ad676c0bSJames Wright   }
51*ad676c0bSJames Wright   PetscCall(PetscStrToArray(string, ':', &num_items, &entries));
52*ad676c0bSJames Wright   switch (num_items) {
53*ad676c0bSJames Wright     case 1:  // Only hours
54*ad676c0bSJames Wright       *duration = 60 * 60 * atoi(entries[0]);
55*ad676c0bSJames Wright       break;
56*ad676c0bSJames Wright     case 2:  // Hours and Minutes
57*ad676c0bSJames Wright       *duration = 60 * 60 * atoi(entries[0]) + 60 * atoi(entries[1]);
58*ad676c0bSJames Wright       break;
59*ad676c0bSJames Wright     case 3:  // Hours, Minutes, and Seconds
60*ad676c0bSJames Wright       *duration = 60 * 60 * atoi(entries[0]) + 60 * atoi(entries[1]) + atoi(entries[2]);
61*ad676c0bSJames Wright       break;
62*ad676c0bSJames Wright     default:
63*ad676c0bSJames Wright       SETERRQ(comm, PETSC_ERR_SUP, "Recieved %d ':' delimited entries, expect either 1, 2, or 3", num_items);
64*ad676c0bSJames Wright   }
65*ad676c0bSJames Wright   PetscCall(PetscStrToArrayDestroy(num_items, entries));
66*ad676c0bSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
67*ad676c0bSJames Wright }
68*ad676c0bSJames Wright 
69a515125bSLeila Ghaffari // Process general command line options
7073170398SJames Wright PetscErrorCode ProcessCommandLineOptions(Honee honee, SimpleBC bc) {
7173170398SJames Wright   MPI_Comm  comm         = honee->comm;
7273170398SJames Wright   AppCtx    app_ctx      = honee->app_ctx;
73a515125bSLeila Ghaffari   PetscBool ceed_flag    = PETSC_FALSE;
74a515125bSLeila Ghaffari   PetscBool problem_flag = PETSC_FALSE;
7591a36801SJames Wright   PetscBool option_set   = PETSC_FALSE;
762b916ea7SJeremy L Thompson 
77a515125bSLeila Ghaffari   PetscFunctionBeginUser;
78b1489633SJames Wright   {
79b1489633SJames Wright     PetscInt num_options;
80b1489633SJames Wright 
81b1489633SJames Wright     PetscCall(PetscOptionsLeftGet(NULL, &num_options, NULL, NULL));
82b1489633SJames Wright     PetscCheck(num_options > 0, comm, PETSC_ERR_USER_INPUT,
83b1489633SJames Wright                "Command line options required."
84b1489633SJames Wright                " Please consult the documentation to see which options are required.");
85b1489633SJames Wright     PetscCall(PetscOptionsLeftRestore(NULL, &num_options, NULL, NULL));
86b1489633SJames Wright   }
87b1489633SJames Wright 
885907cb7eSJames Wright   PetscCall(RegisterProblems_NS(app_ctx));
8915747f0fSJames Wright   PetscOptionsBegin(comm, NULL, "HONEE - High-Order Navier-stokes Equation Evaluator", NULL);
90a515125bSLeila Ghaffari 
912b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, app_ctx->ceed_resource, app_ctx->ceed_resource,
922b916ea7SJeremy L Thompson                                sizeof(app_ctx->ceed_resource), &ceed_flag));
93a515125bSLeila Ghaffari 
940e1e9333SJames Wright   app_ctx->test_type = TESTTYPE_NONE;
950e1e9333SJames Wright   PetscCall(PetscOptionsEnum("-test_type", "Type of test to run", NULL, TestTypes, (PetscEnum)(app_ctx->test_type), (PetscEnum *)&app_ctx->test_type,
960e1e9333SJames Wright                              NULL));
97a515125bSLeila Ghaffari 
98a515125bSLeila Ghaffari   app_ctx->test_tol = 1E-11;
992b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-compare_final_state_atol", "Test absolute tolerance", NULL, app_ctx->test_tol, &app_ctx->test_tol, NULL));
100a515125bSLeila Ghaffari 
101c931fa59SJames Wright   PetscCall(PetscOptionsString("-compare_final_state_filename", "Test filename", NULL, app_ctx->test_file_path, app_ctx->test_file_path,
102c931fa59SJames Wright                                sizeof(app_ctx->test_file_path), NULL));
103a515125bSLeila Ghaffari 
1042b916ea7SJeremy L Thompson   PetscCall(PetscOptionsFList("-problem", "Problem to solve", NULL, app_ctx->problems, app_ctx->problem_name, app_ctx->problem_name,
1052b916ea7SJeremy L Thompson                               sizeof(app_ctx->problem_name), &problem_flag));
106a515125bSLeila Ghaffari 
107a515125bSLeila Ghaffari   app_ctx->viz_refine = 0;
1082b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-viz_refine", "Regular refinement levels for visualization", NULL, app_ctx->viz_refine, &app_ctx->viz_refine, NULL));
109a515125bSLeila Ghaffari 
110852e5969SJed Brown   app_ctx->checkpoint_interval = 10;
111852e5969SJed Brown   app_ctx->checkpoint_vtk      = PETSC_FALSE;
112852e5969SJed Brown   PetscCall(PetscOptionsDeprecated("-output_freq", "-checkpoint_interval", "libCEED 0.11.1", "Use -checkpoint_vtk true to include VTK output"));
113852e5969SJed Brown   PetscCall(PetscOptionsInt("-output_freq", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
114852e5969SJed Brown                             &app_ctx->checkpoint_interval, &option_set));
115852e5969SJed Brown   if (option_set) app_ctx->checkpoint_vtk = PETSC_TRUE;
116852e5969SJed Brown   PetscCall(PetscOptionsInt("-checkpoint_interval", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
117852e5969SJed Brown                             &app_ctx->checkpoint_interval, NULL));
118852e5969SJed Brown   PetscCall(PetscOptionsBool("-checkpoint_vtk", "Include VTK (*.vtu) output at each binary checkpoint", NULL, app_ctx->checkpoint_vtk,
119852e5969SJed Brown                              &app_ctx->checkpoint_vtk, NULL));
120a515125bSLeila Ghaffari 
1212b916ea7SJeremy L Thompson   PetscCall(PetscOptionsBool("-output_add_stepnum2bin", "Add step number to the binary outputs", NULL, app_ctx->add_stepnum2bin,
1222b916ea7SJeremy L Thompson                              &app_ctx->add_stepnum2bin, NULL));
12391a36801SJames Wright 
12491a36801SJames Wright   PetscCall(PetscStrncpy(app_ctx->output_dir, ".", 2));
1252b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-output_dir", "Output directory", NULL, app_ctx->output_dir, app_ctx->output_dir, sizeof(app_ctx->output_dir), NULL));
126c500636fSJames Wright   PetscMPIInt rank;
127c500636fSJames Wright   MPI_Comm_rank(comm, &rank);
128c500636fSJames Wright   if (!rank) PetscCall(PetscMkdir(app_ctx->output_dir));
12991a36801SJames Wright 
1302b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-continue_filename", "Filename to get initial condition from", NULL, app_ctx->cont_file, app_ctx->cont_file,
131481d14cbSJames Wright                                sizeof(app_ctx->cont_file), NULL));
132481d14cbSJames Wright   if (app_ctx->cont_file[0] != '\0') app_ctx->use_continue_file = PETSC_TRUE;
13391a36801SJames Wright 
134ea2beb2dSJames Wright   PetscCall(
135481d14cbSJames Wright       PetscOptionsDeprecated("-continue", NULL, "HONEE 0.0.0", "Set -continue_filename to non-empty string to continue from previous solution"));
136481d14cbSJames Wright   PetscCall(
137ea2beb2dSJames Wright       PetscOptionsDeprecated("-continue_time_filename", NULL, "HONEE 0.0.0", "HONEE no longer supports reading in solution times from binary file"));
13891a36801SJames Wright 
139a515125bSLeila Ghaffari   app_ctx->degree = 1;
1402b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-degree", "Polynomial degree of finite elements", NULL, app_ctx->degree, &app_ctx->degree, NULL));
141a515125bSLeila Ghaffari 
1421219168aSLeila Ghaffari   app_ctx->q_extra = 0;
1432b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, app_ctx->q_extra, &app_ctx->q_extra, NULL));
144a515125bSLeila Ghaffari 
145b107fddaSJed Brown   {
146b107fddaSJed Brown     PetscBool option_set;
147b107fddaSJed Brown     char      amat_type[256] = "";
1482b916ea7SJeremy L Thompson     PetscCall(PetscOptionsFList("-amat_type", "Set the type of Amat distinct from Pmat (-dm_mat_type)", NULL, MatList, amat_type, amat_type,
1492b916ea7SJeremy L Thompson                                 sizeof(amat_type), &option_set));
1502b916ea7SJeremy L Thompson     if (option_set) PetscCall(PetscStrallocpy(amat_type, (char **)&app_ctx->amat_type));
151b107fddaSJed Brown   }
1521024319bSJames Wright   {
1531024319bSJames Wright     PetscBool option_set;
1541024319bSJames Wright     PetscCall(PetscOptionsHasName(NULL, NULL, "-pmat_pbdiagonal", &option_set));
1551024319bSJames Wright     if (option_set) PetscCall(PetscPrintf(comm, "Warning! -pmat_pbdiagonal no longer used. Pmat assembly determined from -pc_type setting\n"));
1561024319bSJames Wright   }
157b107fddaSJed Brown 
158a515125bSLeila Ghaffari   // Provide default ceed resource if not specified
159a515125bSLeila Ghaffari   if (!ceed_flag) {
160a515125bSLeila Ghaffari     const char *ceed_resource = "/cpu/self";
161a515125bSLeila Ghaffari     strncpy(app_ctx->ceed_resource, ceed_resource, 10);
162a515125bSLeila Ghaffari   }
163a467869fSJed Brown   // If we request a GPU, make sure PETSc has initialized its device (which is
164a467869fSJed Brown   // MPI-aware in case multiple devices are available) before CeedInit so that
165a467869fSJed Brown   // PETSc and libCEED agree about which device to use.
166a467869fSJed Brown   if (strncmp(app_ctx->ceed_resource, "/gpu", 4) == 0) PetscCall(PetscDeviceInitialize(PETSC_DEVICE_DEFAULT()));
167a515125bSLeila Ghaffari 
168a515125bSLeila Ghaffari   // Provide default problem if not specified
169a515125bSLeila Ghaffari   if (!problem_flag) {
170a515125bSLeila Ghaffari     const char *problem_name = "density_current";
171a515125bSLeila Ghaffari     strncpy(app_ctx->problem_name, problem_name, 16);
172a515125bSLeila Ghaffari   }
173a515125bSLeila Ghaffari 
174b0488d1fSJames Wright   // Statistics Options
175c931fa59SJames Wright   app_ctx->turb_spanstats_collect_interval = 1;
176c931fa59SJames Wright   PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_collect_interval", "Number of timesteps between statistics collection", NULL,
177c931fa59SJames Wright                             app_ctx->turb_spanstats_collect_interval, &app_ctx->turb_spanstats_collect_interval, NULL));
178b0488d1fSJames Wright 
179c931fa59SJames Wright   app_ctx->turb_spanstats_viewer_interval = -1;
180c931fa59SJames Wright   PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_viewer_interval", "Number of timesteps between statistics viewer writing", NULL,
181c931fa59SJames Wright                             app_ctx->turb_spanstats_viewer_interval, &app_ctx->turb_spanstats_viewer_interval, NULL));
182b0488d1fSJames Wright 
183c931fa59SJames Wright   PetscCall(PetscOptionsViewer("-ts_monitor_turbulence_spanstats_viewer", "Viewer for the statistics", NULL, &app_ctx->turb_spanstats_viewer,
184c931fa59SJames Wright                                &app_ctx->turb_spanstats_viewer_format, &app_ctx->turb_spanstats_enable));
185b0488d1fSJames Wright 
186c5e9980aSAdeleke O. Bankole   PetscCall(PetscOptionsViewer("-ts_monitor_wall_force", "Viewer for force on each (no-slip) wall", NULL, &app_ctx->wall_forces.viewer,
187c5e9980aSAdeleke O. Bankole                                &app_ctx->wall_forces.viewer_format, NULL));
188c5e9980aSAdeleke O. Bankole 
18901ab89c1SJames Wright   // SGS Model Options
19001ab89c1SJames Wright   app_ctx->sgs_model_type = SGS_MODEL_NONE;
19101ab89c1SJames Wright   PetscCall(PetscOptionsEnum("-sgs_model_type", "Subgrid Stress Model type", NULL, SGSModelTypes, (PetscEnum)app_ctx->sgs_model_type,
19201ab89c1SJames Wright                              (PetscEnum *)&app_ctx->sgs_model_type, NULL));
19301ab89c1SJames Wright 
19488b07121SJames Wright   PetscCall(PetscOptionsBool("-diff_filter_monitor", "Enable differential filtering TSMonitor", NULL, app_ctx->diff_filter_monitor,
19588b07121SJames Wright                              &app_ctx->diff_filter_monitor, NULL));
19688b07121SJames Wright 
197f31f4833SJames Wright   // Mesh Transformation Options
198f31f4833SJames Wright   app_ctx->mesh_transform_type = MESH_TRANSFORM_NONE;
199f31f4833SJames Wright   PetscCall(PetscOptionsEnum("-mesh_transform", "Mesh transform to perform", NULL, MeshTransformTypes, (PetscEnum)app_ctx->mesh_transform_type,
200f31f4833SJames Wright                              (PetscEnum *)&app_ctx->mesh_transform_type, NULL));
201f31f4833SJames Wright 
2021c17f66aSJames Wright   PetscCall(
2031c17f66aSJames Wright       PetscOptionsBool("-sgs_train_enable", "Enable Data-Driven SGS training", NULL, app_ctx->sgs_train_enable, &app_ctx->sgs_train_enable, NULL));
2041c17f66aSJames Wright 
2058c85b835SJames Wright   PetscCall(PetscOptionsEnum("-div_diff_flux_projection_method", "Method of divergence of diffusive flux projection", NULL,
2068c85b835SJames Wright                              DivDiffFluxProjectionMethods, (PetscEnum)(app_ctx->divFdiffproj_method), (PetscEnum *)&app_ctx->divFdiffproj_method,
2078c85b835SJames Wright                              NULL));
2088c85b835SJames Wright 
2098b774af8SJames Wright   app_ctx->check_step_interval = -1;
2108b774af8SJames Wright   PetscCall(PetscOptionsDeprecated("-ts_monitor_nan_interval", "-honee_check_step_interval", "HONEE 0.0", NULL));
2118b774af8SJames Wright   PetscCall(PetscOptionsInt("-honee_check_step_interval", "Number of timesteps between verifying the validity of the solution", NULL,
2128b774af8SJames Wright                             app_ctx->check_step_interval, &app_ctx->check_step_interval, NULL));
2138b774af8SJames Wright 
2141485969bSJeremy L Thompson   PetscOptionsEnd();
215d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
216a515125bSLeila Ghaffari }
217