xref: /honee/src/cloptions.c (revision ea2beb2d00ff99137ccb08857f9b9e2abb5f363f)
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
13a515125bSLeila Ghaffari 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 
462b916ea7SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Navier-Stokes in PETSc with libCEED", NULL);
47a515125bSLeila Ghaffari 
482b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, app_ctx->ceed_resource, app_ctx->ceed_resource,
492b916ea7SJeremy L Thompson                                sizeof(app_ctx->ceed_resource), &ceed_flag));
50a515125bSLeila Ghaffari 
510e1e9333SJames Wright   app_ctx->test_type = TESTTYPE_NONE;
520e1e9333SJames Wright   PetscCall(PetscOptionsEnum("-test_type", "Type of test to run", NULL, TestTypes, (PetscEnum)(app_ctx->test_type), (PetscEnum *)&app_ctx->test_type,
530e1e9333SJames Wright                              NULL));
54a515125bSLeila Ghaffari 
55a515125bSLeila Ghaffari   app_ctx->test_tol = 1E-11;
562b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-compare_final_state_atol", "Test absolute tolerance", NULL, app_ctx->test_tol, &app_ctx->test_tol, NULL));
57a515125bSLeila Ghaffari 
58c931fa59SJames Wright   PetscCall(PetscOptionsString("-compare_final_state_filename", "Test filename", NULL, app_ctx->test_file_path, app_ctx->test_file_path,
59c931fa59SJames Wright                                sizeof(app_ctx->test_file_path), NULL));
60a515125bSLeila Ghaffari 
612b916ea7SJeremy L Thompson   PetscCall(PetscOptionsFList("-problem", "Problem to solve", NULL, app_ctx->problems, app_ctx->problem_name, app_ctx->problem_name,
622b916ea7SJeremy L Thompson                               sizeof(app_ctx->problem_name), &problem_flag));
63a515125bSLeila Ghaffari 
64a515125bSLeila Ghaffari   app_ctx->viz_refine = 0;
652b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-viz_refine", "Regular refinement levels for visualization", NULL, app_ctx->viz_refine, &app_ctx->viz_refine, NULL));
66a515125bSLeila Ghaffari 
67852e5969SJed Brown   app_ctx->checkpoint_interval = 10;
68852e5969SJed Brown   app_ctx->checkpoint_vtk      = PETSC_FALSE;
69852e5969SJed Brown   PetscCall(PetscOptionsDeprecated("-output_freq", "-checkpoint_interval", "libCEED 0.11.1", "Use -checkpoint_vtk true to include VTK output"));
70852e5969SJed Brown   PetscCall(PetscOptionsInt("-output_freq", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
71852e5969SJed Brown                             &app_ctx->checkpoint_interval, &option_set));
72852e5969SJed Brown   if (option_set) app_ctx->checkpoint_vtk = PETSC_TRUE;
73852e5969SJed Brown   PetscCall(PetscOptionsInt("-checkpoint_interval", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
74852e5969SJed Brown                             &app_ctx->checkpoint_interval, NULL));
75852e5969SJed Brown   PetscCall(PetscOptionsBool("-checkpoint_vtk", "Include VTK (*.vtu) output at each binary checkpoint", NULL, app_ctx->checkpoint_vtk,
76852e5969SJed Brown                              &app_ctx->checkpoint_vtk, NULL));
77a515125bSLeila Ghaffari 
782b916ea7SJeremy L Thompson   PetscCall(PetscOptionsBool("-output_add_stepnum2bin", "Add step number to the binary outputs", NULL, app_ctx->add_stepnum2bin,
792b916ea7SJeremy L Thompson                              &app_ctx->add_stepnum2bin, NULL));
8091a36801SJames Wright 
8191a36801SJames Wright   PetscCall(PetscStrncpy(app_ctx->output_dir, ".", 2));
822b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-output_dir", "Output directory", NULL, app_ctx->output_dir, app_ctx->output_dir, sizeof(app_ctx->output_dir), NULL));
8391a36801SJames Wright 
84a515125bSLeila Ghaffari   app_ctx->cont_steps = 0;
852b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-continue", "Continue from previous solution", NULL, app_ctx->cont_steps, &app_ctx->cont_steps, NULL));
86a515125bSLeila Ghaffari 
8791a36801SJames Wright   PetscCall(PetscStrcpy(app_ctx->cont_file, "[output_dir]/ns-solution.bin"));
882b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-continue_filename", "Filename to get initial condition from", NULL, app_ctx->cont_file, app_ctx->cont_file,
8991a36801SJames Wright                                sizeof(app_ctx->cont_file), &option_set));
902b916ea7SJeremy L Thompson   if (!option_set) PetscCall(PetscSNPrintf(app_ctx->cont_file, sizeof app_ctx->cont_file, "%s/ns-solution.bin", app_ctx->output_dir));
919293eaa1SJed Brown   if (option_set && app_ctx->cont_steps == 0) app_ctx->cont_steps = -1;  // Read time from file
9291a36801SJames Wright 
93*ea2beb2dSJames Wright   PetscCall(
94*ea2beb2dSJames Wright       PetscOptionsDeprecated("-continue_time_filename", NULL, "HONEE 0.0.0", "HONEE no longer supports reading in solution times from binary file"));
9591a36801SJames Wright 
96a515125bSLeila Ghaffari   app_ctx->degree = 1;
972b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-degree", "Polynomial degree of finite elements", NULL, app_ctx->degree, &app_ctx->degree, NULL));
98a515125bSLeila Ghaffari 
991219168aSLeila Ghaffari   app_ctx->q_extra = 0;
1002b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, app_ctx->q_extra, &app_ctx->q_extra, NULL));
101a515125bSLeila Ghaffari 
102b107fddaSJed Brown   {
103b107fddaSJed Brown     PetscBool option_set;
104b107fddaSJed Brown     char      amat_type[256] = "";
1052b916ea7SJeremy L Thompson     PetscCall(PetscOptionsFList("-amat_type", "Set the type of Amat distinct from Pmat (-dm_mat_type)", NULL, MatList, amat_type, amat_type,
1062b916ea7SJeremy L Thompson                                 sizeof(amat_type), &option_set));
1072b916ea7SJeremy L Thompson     if (option_set) PetscCall(PetscStrallocpy(amat_type, (char **)&app_ctx->amat_type));
108b107fddaSJed Brown   }
1091024319bSJames Wright   {
1101024319bSJames Wright     PetscBool option_set;
1111024319bSJames Wright     PetscCall(PetscOptionsHasName(NULL, NULL, "-pmat_pbdiagonal", &option_set));
1121024319bSJames Wright     if (option_set) PetscCall(PetscPrintf(comm, "Warning! -pmat_pbdiagonal no longer used. Pmat assembly determined from -pc_type setting\n"));
1131024319bSJames Wright   }
114b107fddaSJed Brown 
115a515125bSLeila Ghaffari   // Provide default ceed resource if not specified
116a515125bSLeila Ghaffari   if (!ceed_flag) {
117a515125bSLeila Ghaffari     const char *ceed_resource = "/cpu/self";
118a515125bSLeila Ghaffari     strncpy(app_ctx->ceed_resource, ceed_resource, 10);
119a515125bSLeila Ghaffari   }
120a467869fSJed Brown   // If we request a GPU, make sure PETSc has initialized its device (which is
121a467869fSJed Brown   // MPI-aware in case multiple devices are available) before CeedInit so that
122a467869fSJed Brown   // PETSc and libCEED agree about which device to use.
123a467869fSJed Brown   if (strncmp(app_ctx->ceed_resource, "/gpu", 4) == 0) PetscCall(PetscDeviceInitialize(PETSC_DEVICE_DEFAULT()));
124a515125bSLeila Ghaffari 
125a515125bSLeila Ghaffari   // Provide default problem if not specified
126a515125bSLeila Ghaffari   if (!problem_flag) {
127a515125bSLeila Ghaffari     const char *problem_name = "density_current";
128a515125bSLeila Ghaffari     strncpy(app_ctx->problem_name, problem_name, 16);
129a515125bSLeila Ghaffari   }
130a515125bSLeila Ghaffari 
131b0488d1fSJames Wright   // Statistics Options
132c931fa59SJames Wright   app_ctx->turb_spanstats_collect_interval = 1;
133c931fa59SJames Wright   PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_collect_interval", "Number of timesteps between statistics collection", NULL,
134c931fa59SJames Wright                             app_ctx->turb_spanstats_collect_interval, &app_ctx->turb_spanstats_collect_interval, NULL));
135b0488d1fSJames Wright 
136c931fa59SJames Wright   app_ctx->turb_spanstats_viewer_interval = -1;
137c931fa59SJames Wright   PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_viewer_interval", "Number of timesteps between statistics viewer writing", NULL,
138c931fa59SJames Wright                             app_ctx->turb_spanstats_viewer_interval, &app_ctx->turb_spanstats_viewer_interval, NULL));
139b0488d1fSJames Wright 
140c931fa59SJames Wright   PetscCall(PetscOptionsViewer("-ts_monitor_turbulence_spanstats_viewer", "Viewer for the statistics", NULL, &app_ctx->turb_spanstats_viewer,
141c931fa59SJames Wright                                &app_ctx->turb_spanstats_viewer_format, &app_ctx->turb_spanstats_enable));
142b0488d1fSJames Wright 
143c5e9980aSAdeleke O. Bankole   PetscCall(PetscOptionsViewer("-ts_monitor_wall_force", "Viewer for force on each (no-slip) wall", NULL, &app_ctx->wall_forces.viewer,
144c5e9980aSAdeleke O. Bankole                                &app_ctx->wall_forces.viewer_format, NULL));
145c5e9980aSAdeleke O. Bankole 
14601ab89c1SJames Wright   // SGS Model Options
14701ab89c1SJames Wright   app_ctx->sgs_model_type = SGS_MODEL_NONE;
14801ab89c1SJames Wright   PetscCall(PetscOptionsEnum("-sgs_model_type", "Subgrid Stress Model type", NULL, SGSModelTypes, (PetscEnum)app_ctx->sgs_model_type,
14901ab89c1SJames Wright                              (PetscEnum *)&app_ctx->sgs_model_type, NULL));
15001ab89c1SJames Wright 
15188b07121SJames Wright   PetscCall(PetscOptionsBool("-diff_filter_monitor", "Enable differential filtering TSMonitor", NULL, app_ctx->diff_filter_monitor,
15288b07121SJames Wright                              &app_ctx->diff_filter_monitor, NULL));
15388b07121SJames Wright 
154f31f4833SJames Wright   // Mesh Transformation Options
155f31f4833SJames Wright   app_ctx->mesh_transform_type = MESH_TRANSFORM_NONE;
156f31f4833SJames Wright   PetscCall(PetscOptionsEnum("-mesh_transform", "Mesh transform to perform", NULL, MeshTransformTypes, (PetscEnum)app_ctx->mesh_transform_type,
157f31f4833SJames Wright                              (PetscEnum *)&app_ctx->mesh_transform_type, NULL));
158f31f4833SJames Wright 
1591c17f66aSJames Wright   PetscCall(
1601c17f66aSJames Wright       PetscOptionsBool("-sgs_train_enable", "Enable Data-Driven SGS training", NULL, app_ctx->sgs_train_enable, &app_ctx->sgs_train_enable, NULL));
1611c17f66aSJames Wright 
1628c85b835SJames Wright   PetscCall(PetscOptionsEnum("-div_diff_flux_projection_method", "Method of divergence of diffusive flux projection", NULL,
1638c85b835SJames Wright                              DivDiffFluxProjectionMethods, (PetscEnum)(app_ctx->divFdiffproj_method), (PetscEnum *)&app_ctx->divFdiffproj_method,
1648c85b835SJames Wright                              NULL));
1658c85b835SJames Wright 
1661485969bSJeremy L Thompson   PetscOptionsEnd();
167d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
168a515125bSLeila Ghaffari }
169