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