xref: /honee/src/cloptions.c (revision 8b774af87fdb8a3f86da550a60bc10e487d29d78)
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 
29a515125bSLeila Ghaffari // Process general command line options
302b916ea7SJeremy L Thompson PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx, SimpleBC bc) {
31a515125bSLeila Ghaffari   PetscBool ceed_flag    = PETSC_FALSE;
32a515125bSLeila Ghaffari   PetscBool problem_flag = PETSC_FALSE;
3391a36801SJames Wright   PetscBool option_set   = PETSC_FALSE;
342b916ea7SJeremy L Thompson 
35a515125bSLeila Ghaffari   PetscFunctionBeginUser;
36b1489633SJames Wright   {
37b1489633SJames Wright     PetscInt num_options;
38b1489633SJames Wright 
39b1489633SJames Wright     PetscCall(PetscOptionsLeftGet(NULL, &num_options, NULL, NULL));
40b1489633SJames Wright     PetscCheck(num_options > 0, comm, PETSC_ERR_USER_INPUT,
41b1489633SJames Wright                "Command line options required."
42b1489633SJames Wright                " Please consult the documentation to see which options are required.");
43b1489633SJames Wright     PetscCall(PetscOptionsLeftRestore(NULL, &num_options, NULL, NULL));
44b1489633SJames Wright   }
45b1489633SJames Wright 
465907cb7eSJames Wright   PetscCall(RegisterProblems_NS(app_ctx));
4715747f0fSJames Wright   PetscOptionsBegin(comm, NULL, "HONEE - High-Order Navier-stokes Equation Evaluator", NULL);
48a515125bSLeila Ghaffari 
492b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, app_ctx->ceed_resource, app_ctx->ceed_resource,
502b916ea7SJeremy L Thompson                                sizeof(app_ctx->ceed_resource), &ceed_flag));
51a515125bSLeila Ghaffari 
520e1e9333SJames Wright   app_ctx->test_type = TESTTYPE_NONE;
530e1e9333SJames Wright   PetscCall(PetscOptionsEnum("-test_type", "Type of test to run", NULL, TestTypes, (PetscEnum)(app_ctx->test_type), (PetscEnum *)&app_ctx->test_type,
540e1e9333SJames Wright                              NULL));
55a515125bSLeila Ghaffari 
56a515125bSLeila Ghaffari   app_ctx->test_tol = 1E-11;
572b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-compare_final_state_atol", "Test absolute tolerance", NULL, app_ctx->test_tol, &app_ctx->test_tol, NULL));
58a515125bSLeila Ghaffari 
59c931fa59SJames Wright   PetscCall(PetscOptionsString("-compare_final_state_filename", "Test filename", NULL, app_ctx->test_file_path, app_ctx->test_file_path,
60c931fa59SJames Wright                                sizeof(app_ctx->test_file_path), NULL));
61a515125bSLeila Ghaffari 
622b916ea7SJeremy L Thompson   PetscCall(PetscOptionsFList("-problem", "Problem to solve", NULL, app_ctx->problems, app_ctx->problem_name, app_ctx->problem_name,
632b916ea7SJeremy L Thompson                               sizeof(app_ctx->problem_name), &problem_flag));
64a515125bSLeila Ghaffari 
65a515125bSLeila Ghaffari   app_ctx->viz_refine = 0;
662b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-viz_refine", "Regular refinement levels for visualization", NULL, app_ctx->viz_refine, &app_ctx->viz_refine, NULL));
67a515125bSLeila Ghaffari 
68852e5969SJed Brown   app_ctx->checkpoint_interval = 10;
69852e5969SJed Brown   app_ctx->checkpoint_vtk      = PETSC_FALSE;
70852e5969SJed Brown   PetscCall(PetscOptionsDeprecated("-output_freq", "-checkpoint_interval", "libCEED 0.11.1", "Use -checkpoint_vtk true to include VTK output"));
71852e5969SJed Brown   PetscCall(PetscOptionsInt("-output_freq", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
72852e5969SJed Brown                             &app_ctx->checkpoint_interval, &option_set));
73852e5969SJed Brown   if (option_set) app_ctx->checkpoint_vtk = PETSC_TRUE;
74852e5969SJed Brown   PetscCall(PetscOptionsInt("-checkpoint_interval", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
75852e5969SJed Brown                             &app_ctx->checkpoint_interval, NULL));
76852e5969SJed Brown   PetscCall(PetscOptionsBool("-checkpoint_vtk", "Include VTK (*.vtu) output at each binary checkpoint", NULL, app_ctx->checkpoint_vtk,
77852e5969SJed Brown                              &app_ctx->checkpoint_vtk, NULL));
78a515125bSLeila Ghaffari 
792b916ea7SJeremy L Thompson   PetscCall(PetscOptionsBool("-output_add_stepnum2bin", "Add step number to the binary outputs", NULL, app_ctx->add_stepnum2bin,
802b916ea7SJeremy L Thompson                              &app_ctx->add_stepnum2bin, NULL));
8191a36801SJames Wright 
8291a36801SJames Wright   PetscCall(PetscStrncpy(app_ctx->output_dir, ".", 2));
832b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-output_dir", "Output directory", NULL, app_ctx->output_dir, app_ctx->output_dir, sizeof(app_ctx->output_dir), NULL));
84c500636fSJames Wright   PetscMPIInt rank;
85c500636fSJames Wright   MPI_Comm_rank(comm, &rank);
86c500636fSJames Wright   if (!rank) PetscCall(PetscMkdir(app_ctx->output_dir));
8791a36801SJames Wright 
882b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-continue_filename", "Filename to get initial condition from", NULL, app_ctx->cont_file, app_ctx->cont_file,
89481d14cbSJames Wright                                sizeof(app_ctx->cont_file), NULL));
90481d14cbSJames Wright   if (app_ctx->cont_file[0] != '\0') app_ctx->use_continue_file = PETSC_TRUE;
9191a36801SJames Wright 
92ea2beb2dSJames Wright   PetscCall(
93481d14cbSJames Wright       PetscOptionsDeprecated("-continue", NULL, "HONEE 0.0.0", "Set -continue_filename to non-empty string to continue from previous solution"));
94481d14cbSJames Wright   PetscCall(
95ea2beb2dSJames Wright       PetscOptionsDeprecated("-continue_time_filename", NULL, "HONEE 0.0.0", "HONEE no longer supports reading in solution times from binary file"));
9691a36801SJames Wright 
97a515125bSLeila Ghaffari   app_ctx->degree = 1;
982b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-degree", "Polynomial degree of finite elements", NULL, app_ctx->degree, &app_ctx->degree, NULL));
99a515125bSLeila Ghaffari 
1001219168aSLeila Ghaffari   app_ctx->q_extra = 0;
1012b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, app_ctx->q_extra, &app_ctx->q_extra, NULL));
102a515125bSLeila Ghaffari 
103b107fddaSJed Brown   {
104b107fddaSJed Brown     PetscBool option_set;
105b107fddaSJed Brown     char      amat_type[256] = "";
1062b916ea7SJeremy L Thompson     PetscCall(PetscOptionsFList("-amat_type", "Set the type of Amat distinct from Pmat (-dm_mat_type)", NULL, MatList, amat_type, amat_type,
1072b916ea7SJeremy L Thompson                                 sizeof(amat_type), &option_set));
1082b916ea7SJeremy L Thompson     if (option_set) PetscCall(PetscStrallocpy(amat_type, (char **)&app_ctx->amat_type));
109b107fddaSJed Brown   }
1101024319bSJames Wright   {
1111024319bSJames Wright     PetscBool option_set;
1121024319bSJames Wright     PetscCall(PetscOptionsHasName(NULL, NULL, "-pmat_pbdiagonal", &option_set));
1131024319bSJames Wright     if (option_set) PetscCall(PetscPrintf(comm, "Warning! -pmat_pbdiagonal no longer used. Pmat assembly determined from -pc_type setting\n"));
1141024319bSJames Wright   }
115b107fddaSJed Brown 
116a515125bSLeila Ghaffari   // Provide default ceed resource if not specified
117a515125bSLeila Ghaffari   if (!ceed_flag) {
118a515125bSLeila Ghaffari     const char *ceed_resource = "/cpu/self";
119a515125bSLeila Ghaffari     strncpy(app_ctx->ceed_resource, ceed_resource, 10);
120a515125bSLeila Ghaffari   }
121a467869fSJed Brown   // If we request a GPU, make sure PETSc has initialized its device (which is
122a467869fSJed Brown   // MPI-aware in case multiple devices are available) before CeedInit so that
123a467869fSJed Brown   // PETSc and libCEED agree about which device to use.
124a467869fSJed Brown   if (strncmp(app_ctx->ceed_resource, "/gpu", 4) == 0) PetscCall(PetscDeviceInitialize(PETSC_DEVICE_DEFAULT()));
125a515125bSLeila Ghaffari 
126a515125bSLeila Ghaffari   // Provide default problem if not specified
127a515125bSLeila Ghaffari   if (!problem_flag) {
128a515125bSLeila Ghaffari     const char *problem_name = "density_current";
129a515125bSLeila Ghaffari     strncpy(app_ctx->problem_name, problem_name, 16);
130a515125bSLeila Ghaffari   }
131a515125bSLeila Ghaffari 
132b0488d1fSJames Wright   // Statistics Options
133c931fa59SJames Wright   app_ctx->turb_spanstats_collect_interval = 1;
134c931fa59SJames Wright   PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_collect_interval", "Number of timesteps between statistics collection", NULL,
135c931fa59SJames Wright                             app_ctx->turb_spanstats_collect_interval, &app_ctx->turb_spanstats_collect_interval, NULL));
136b0488d1fSJames Wright 
137c931fa59SJames Wright   app_ctx->turb_spanstats_viewer_interval = -1;
138c931fa59SJames Wright   PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_viewer_interval", "Number of timesteps between statistics viewer writing", NULL,
139c931fa59SJames Wright                             app_ctx->turb_spanstats_viewer_interval, &app_ctx->turb_spanstats_viewer_interval, NULL));
140b0488d1fSJames Wright 
141c931fa59SJames Wright   PetscCall(PetscOptionsViewer("-ts_monitor_turbulence_spanstats_viewer", "Viewer for the statistics", NULL, &app_ctx->turb_spanstats_viewer,
142c931fa59SJames Wright                                &app_ctx->turb_spanstats_viewer_format, &app_ctx->turb_spanstats_enable));
143b0488d1fSJames Wright 
144c5e9980aSAdeleke O. Bankole   PetscCall(PetscOptionsViewer("-ts_monitor_wall_force", "Viewer for force on each (no-slip) wall", NULL, &app_ctx->wall_forces.viewer,
145c5e9980aSAdeleke O. Bankole                                &app_ctx->wall_forces.viewer_format, NULL));
146c5e9980aSAdeleke O. Bankole 
14701ab89c1SJames Wright   // SGS Model Options
14801ab89c1SJames Wright   app_ctx->sgs_model_type = SGS_MODEL_NONE;
14901ab89c1SJames Wright   PetscCall(PetscOptionsEnum("-sgs_model_type", "Subgrid Stress Model type", NULL, SGSModelTypes, (PetscEnum)app_ctx->sgs_model_type,
15001ab89c1SJames Wright                              (PetscEnum *)&app_ctx->sgs_model_type, NULL));
15101ab89c1SJames Wright 
15288b07121SJames Wright   PetscCall(PetscOptionsBool("-diff_filter_monitor", "Enable differential filtering TSMonitor", NULL, app_ctx->diff_filter_monitor,
15388b07121SJames Wright                              &app_ctx->diff_filter_monitor, NULL));
15488b07121SJames Wright 
155f31f4833SJames Wright   // Mesh Transformation Options
156f31f4833SJames Wright   app_ctx->mesh_transform_type = MESH_TRANSFORM_NONE;
157f31f4833SJames Wright   PetscCall(PetscOptionsEnum("-mesh_transform", "Mesh transform to perform", NULL, MeshTransformTypes, (PetscEnum)app_ctx->mesh_transform_type,
158f31f4833SJames Wright                              (PetscEnum *)&app_ctx->mesh_transform_type, NULL));
159f31f4833SJames Wright 
1601c17f66aSJames Wright   PetscCall(
1611c17f66aSJames Wright       PetscOptionsBool("-sgs_train_enable", "Enable Data-Driven SGS training", NULL, app_ctx->sgs_train_enable, &app_ctx->sgs_train_enable, NULL));
1621c17f66aSJames Wright 
1638c85b835SJames Wright   PetscCall(PetscOptionsEnum("-div_diff_flux_projection_method", "Method of divergence of diffusive flux projection", NULL,
1648c85b835SJames Wright                              DivDiffFluxProjectionMethods, (PetscEnum)(app_ctx->divFdiffproj_method), (PetscEnum *)&app_ctx->divFdiffproj_method,
1658c85b835SJames Wright                              NULL));
1668c85b835SJames Wright 
167*8b774af8SJames Wright   app_ctx->check_step_interval = -1;
168*8b774af8SJames Wright   PetscCall(PetscOptionsDeprecated("-ts_monitor_nan_interval", "-honee_check_step_interval", "HONEE 0.0", NULL));
169*8b774af8SJames Wright   PetscCall(PetscOptionsInt("-honee_check_step_interval", "Number of timesteps between verifying the validity of the solution", NULL,
170*8b774af8SJames Wright                             app_ctx->check_step_interval, &app_ctx->check_step_interval, NULL));
171*8b774af8SJames Wright 
1721485969bSJeremy L Thompson   PetscOptionsEnd();
173d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
174a515125bSLeila Ghaffari }
175