xref: /honee/src/cloptions.c (revision 7317039853d1db79567c1299d410fce3ccb7649a)
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
30*73170398SJames Wright PetscErrorCode ProcessCommandLineOptions(Honee honee, SimpleBC bc) {
31*73170398SJames Wright   MPI_Comm  comm         = honee->comm;
32*73170398SJames Wright   AppCtx    app_ctx      = honee->app_ctx;
33a515125bSLeila Ghaffari   PetscBool ceed_flag    = PETSC_FALSE;
34a515125bSLeila Ghaffari   PetscBool problem_flag = PETSC_FALSE;
3591a36801SJames Wright   PetscBool option_set   = PETSC_FALSE;
362b916ea7SJeremy L Thompson 
37a515125bSLeila Ghaffari   PetscFunctionBeginUser;
38b1489633SJames Wright   {
39b1489633SJames Wright     PetscInt num_options;
40b1489633SJames Wright 
41b1489633SJames Wright     PetscCall(PetscOptionsLeftGet(NULL, &num_options, NULL, NULL));
42b1489633SJames Wright     PetscCheck(num_options > 0, comm, PETSC_ERR_USER_INPUT,
43b1489633SJames Wright                "Command line options required."
44b1489633SJames Wright                " Please consult the documentation to see which options are required.");
45b1489633SJames Wright     PetscCall(PetscOptionsLeftRestore(NULL, &num_options, NULL, NULL));
46b1489633SJames Wright   }
47b1489633SJames Wright 
485907cb7eSJames Wright   PetscCall(RegisterProblems_NS(app_ctx));
4915747f0fSJames Wright   PetscOptionsBegin(comm, NULL, "HONEE - High-Order Navier-stokes Equation Evaluator", NULL);
50a515125bSLeila Ghaffari 
512b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, app_ctx->ceed_resource, app_ctx->ceed_resource,
522b916ea7SJeremy L Thompson                                sizeof(app_ctx->ceed_resource), &ceed_flag));
53a515125bSLeila Ghaffari 
540e1e9333SJames Wright   app_ctx->test_type = TESTTYPE_NONE;
550e1e9333SJames Wright   PetscCall(PetscOptionsEnum("-test_type", "Type of test to run", NULL, TestTypes, (PetscEnum)(app_ctx->test_type), (PetscEnum *)&app_ctx->test_type,
560e1e9333SJames Wright                              NULL));
57a515125bSLeila Ghaffari 
58a515125bSLeila Ghaffari   app_ctx->test_tol = 1E-11;
592b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-compare_final_state_atol", "Test absolute tolerance", NULL, app_ctx->test_tol, &app_ctx->test_tol, NULL));
60a515125bSLeila Ghaffari 
61c931fa59SJames Wright   PetscCall(PetscOptionsString("-compare_final_state_filename", "Test filename", NULL, app_ctx->test_file_path, app_ctx->test_file_path,
62c931fa59SJames Wright                                sizeof(app_ctx->test_file_path), NULL));
63a515125bSLeila Ghaffari 
642b916ea7SJeremy L Thompson   PetscCall(PetscOptionsFList("-problem", "Problem to solve", NULL, app_ctx->problems, app_ctx->problem_name, app_ctx->problem_name,
652b916ea7SJeremy L Thompson                               sizeof(app_ctx->problem_name), &problem_flag));
66a515125bSLeila Ghaffari 
67a515125bSLeila Ghaffari   app_ctx->viz_refine = 0;
682b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-viz_refine", "Regular refinement levels for visualization", NULL, app_ctx->viz_refine, &app_ctx->viz_refine, NULL));
69a515125bSLeila Ghaffari 
70852e5969SJed Brown   app_ctx->checkpoint_interval = 10;
71852e5969SJed Brown   app_ctx->checkpoint_vtk      = PETSC_FALSE;
72852e5969SJed Brown   PetscCall(PetscOptionsDeprecated("-output_freq", "-checkpoint_interval", "libCEED 0.11.1", "Use -checkpoint_vtk true to include VTK output"));
73852e5969SJed Brown   PetscCall(PetscOptionsInt("-output_freq", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
74852e5969SJed Brown                             &app_ctx->checkpoint_interval, &option_set));
75852e5969SJed Brown   if (option_set) app_ctx->checkpoint_vtk = PETSC_TRUE;
76852e5969SJed Brown   PetscCall(PetscOptionsInt("-checkpoint_interval", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
77852e5969SJed Brown                             &app_ctx->checkpoint_interval, NULL));
78852e5969SJed Brown   PetscCall(PetscOptionsBool("-checkpoint_vtk", "Include VTK (*.vtu) output at each binary checkpoint", NULL, app_ctx->checkpoint_vtk,
79852e5969SJed Brown                              &app_ctx->checkpoint_vtk, NULL));
80a515125bSLeila Ghaffari 
812b916ea7SJeremy L Thompson   PetscCall(PetscOptionsBool("-output_add_stepnum2bin", "Add step number to the binary outputs", NULL, app_ctx->add_stepnum2bin,
822b916ea7SJeremy L Thompson                              &app_ctx->add_stepnum2bin, NULL));
8391a36801SJames Wright 
8491a36801SJames Wright   PetscCall(PetscStrncpy(app_ctx->output_dir, ".", 2));
852b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-output_dir", "Output directory", NULL, app_ctx->output_dir, app_ctx->output_dir, sizeof(app_ctx->output_dir), NULL));
86c500636fSJames Wright   PetscMPIInt rank;
87c500636fSJames Wright   MPI_Comm_rank(comm, &rank);
88c500636fSJames Wright   if (!rank) PetscCall(PetscMkdir(app_ctx->output_dir));
8991a36801SJames Wright 
902b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-continue_filename", "Filename to get initial condition from", NULL, app_ctx->cont_file, app_ctx->cont_file,
91481d14cbSJames Wright                                sizeof(app_ctx->cont_file), NULL));
92481d14cbSJames Wright   if (app_ctx->cont_file[0] != '\0') app_ctx->use_continue_file = PETSC_TRUE;
9391a36801SJames Wright 
94ea2beb2dSJames Wright   PetscCall(
95481d14cbSJames Wright       PetscOptionsDeprecated("-continue", NULL, "HONEE 0.0.0", "Set -continue_filename to non-empty string to continue from previous solution"));
96481d14cbSJames Wright   PetscCall(
97ea2beb2dSJames Wright       PetscOptionsDeprecated("-continue_time_filename", NULL, "HONEE 0.0.0", "HONEE no longer supports reading in solution times from binary file"));
9891a36801SJames Wright 
99a515125bSLeila Ghaffari   app_ctx->degree = 1;
1002b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-degree", "Polynomial degree of finite elements", NULL, app_ctx->degree, &app_ctx->degree, NULL));
101a515125bSLeila Ghaffari 
1021219168aSLeila Ghaffari   app_ctx->q_extra = 0;
1032b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, app_ctx->q_extra, &app_ctx->q_extra, NULL));
104a515125bSLeila Ghaffari 
105b107fddaSJed Brown   {
106b107fddaSJed Brown     PetscBool option_set;
107b107fddaSJed Brown     char      amat_type[256] = "";
1082b916ea7SJeremy L Thompson     PetscCall(PetscOptionsFList("-amat_type", "Set the type of Amat distinct from Pmat (-dm_mat_type)", NULL, MatList, amat_type, amat_type,
1092b916ea7SJeremy L Thompson                                 sizeof(amat_type), &option_set));
1102b916ea7SJeremy L Thompson     if (option_set) PetscCall(PetscStrallocpy(amat_type, (char **)&app_ctx->amat_type));
111b107fddaSJed Brown   }
1121024319bSJames Wright   {
1131024319bSJames Wright     PetscBool option_set;
1141024319bSJames Wright     PetscCall(PetscOptionsHasName(NULL, NULL, "-pmat_pbdiagonal", &option_set));
1151024319bSJames Wright     if (option_set) PetscCall(PetscPrintf(comm, "Warning! -pmat_pbdiagonal no longer used. Pmat assembly determined from -pc_type setting\n"));
1161024319bSJames Wright   }
117b107fddaSJed Brown 
118a515125bSLeila Ghaffari   // Provide default ceed resource if not specified
119a515125bSLeila Ghaffari   if (!ceed_flag) {
120a515125bSLeila Ghaffari     const char *ceed_resource = "/cpu/self";
121a515125bSLeila Ghaffari     strncpy(app_ctx->ceed_resource, ceed_resource, 10);
122a515125bSLeila Ghaffari   }
123a467869fSJed Brown   // If we request a GPU, make sure PETSc has initialized its device (which is
124a467869fSJed Brown   // MPI-aware in case multiple devices are available) before CeedInit so that
125a467869fSJed Brown   // PETSc and libCEED agree about which device to use.
126a467869fSJed Brown   if (strncmp(app_ctx->ceed_resource, "/gpu", 4) == 0) PetscCall(PetscDeviceInitialize(PETSC_DEVICE_DEFAULT()));
127a515125bSLeila Ghaffari 
128a515125bSLeila Ghaffari   // Provide default problem if not specified
129a515125bSLeila Ghaffari   if (!problem_flag) {
130a515125bSLeila Ghaffari     const char *problem_name = "density_current";
131a515125bSLeila Ghaffari     strncpy(app_ctx->problem_name, problem_name, 16);
132a515125bSLeila Ghaffari   }
133a515125bSLeila Ghaffari 
134b0488d1fSJames Wright   // Statistics Options
135c931fa59SJames Wright   app_ctx->turb_spanstats_collect_interval = 1;
136c931fa59SJames Wright   PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_collect_interval", "Number of timesteps between statistics collection", NULL,
137c931fa59SJames Wright                             app_ctx->turb_spanstats_collect_interval, &app_ctx->turb_spanstats_collect_interval, NULL));
138b0488d1fSJames Wright 
139c931fa59SJames Wright   app_ctx->turb_spanstats_viewer_interval = -1;
140c931fa59SJames Wright   PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_viewer_interval", "Number of timesteps between statistics viewer writing", NULL,
141c931fa59SJames Wright                             app_ctx->turb_spanstats_viewer_interval, &app_ctx->turb_spanstats_viewer_interval, NULL));
142b0488d1fSJames Wright 
143c931fa59SJames Wright   PetscCall(PetscOptionsViewer("-ts_monitor_turbulence_spanstats_viewer", "Viewer for the statistics", NULL, &app_ctx->turb_spanstats_viewer,
144c931fa59SJames Wright                                &app_ctx->turb_spanstats_viewer_format, &app_ctx->turb_spanstats_enable));
145b0488d1fSJames Wright 
146c5e9980aSAdeleke O. Bankole   PetscCall(PetscOptionsViewer("-ts_monitor_wall_force", "Viewer for force on each (no-slip) wall", NULL, &app_ctx->wall_forces.viewer,
147c5e9980aSAdeleke O. Bankole                                &app_ctx->wall_forces.viewer_format, NULL));
148c5e9980aSAdeleke O. Bankole 
14901ab89c1SJames Wright   // SGS Model Options
15001ab89c1SJames Wright   app_ctx->sgs_model_type = SGS_MODEL_NONE;
15101ab89c1SJames Wright   PetscCall(PetscOptionsEnum("-sgs_model_type", "Subgrid Stress Model type", NULL, SGSModelTypes, (PetscEnum)app_ctx->sgs_model_type,
15201ab89c1SJames Wright                              (PetscEnum *)&app_ctx->sgs_model_type, NULL));
15301ab89c1SJames Wright 
15488b07121SJames Wright   PetscCall(PetscOptionsBool("-diff_filter_monitor", "Enable differential filtering TSMonitor", NULL, app_ctx->diff_filter_monitor,
15588b07121SJames Wright                              &app_ctx->diff_filter_monitor, NULL));
15688b07121SJames Wright 
157f31f4833SJames Wright   // Mesh Transformation Options
158f31f4833SJames Wright   app_ctx->mesh_transform_type = MESH_TRANSFORM_NONE;
159f31f4833SJames Wright   PetscCall(PetscOptionsEnum("-mesh_transform", "Mesh transform to perform", NULL, MeshTransformTypes, (PetscEnum)app_ctx->mesh_transform_type,
160f31f4833SJames Wright                              (PetscEnum *)&app_ctx->mesh_transform_type, NULL));
161f31f4833SJames Wright 
1621c17f66aSJames Wright   PetscCall(
1631c17f66aSJames Wright       PetscOptionsBool("-sgs_train_enable", "Enable Data-Driven SGS training", NULL, app_ctx->sgs_train_enable, &app_ctx->sgs_train_enable, NULL));
1641c17f66aSJames Wright 
1658c85b835SJames Wright   PetscCall(PetscOptionsEnum("-div_diff_flux_projection_method", "Method of divergence of diffusive flux projection", NULL,
1668c85b835SJames Wright                              DivDiffFluxProjectionMethods, (PetscEnum)(app_ctx->divFdiffproj_method), (PetscEnum *)&app_ctx->divFdiffproj_method,
1678c85b835SJames Wright                              NULL));
1688c85b835SJames Wright 
1698b774af8SJames Wright   app_ctx->check_step_interval = -1;
1708b774af8SJames Wright   PetscCall(PetscOptionsDeprecated("-ts_monitor_nan_interval", "-honee_check_step_interval", "HONEE 0.0", NULL));
1718b774af8SJames Wright   PetscCall(PetscOptionsInt("-honee_check_step_interval", "Number of timesteps between verifying the validity of the solution", NULL,
1728b774af8SJames Wright                             app_ctx->check_step_interval, &app_ctx->check_step_interval, NULL));
1738b774af8SJames Wright 
1741485969bSJeremy L Thompson   PetscOptionsEnd();
175d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
176a515125bSLeila Ghaffari }
177