xref: /honee/src/cloptions.c (revision 9f354eca39917e84f8e341cd1623e3edff6c6745)
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 Navier-Stokes example using PETSc
6 
7 #include <petscdevice.h>
8 #include <petscsys.h>
9 
10 #include <navierstokes.h>
11 
12 // Register problems to be available on the command line
13 static PetscErrorCode RegisterProblems_NS(AppCtx app_ctx) {
14   app_ctx->problems = NULL;
15 
16   PetscFunctionBeginUser;
17   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "density_current", NS_DENSITY_CURRENT));
18   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "euler_vortex", NS_EULER_VORTEX));
19   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "shocktube", NS_SHOCKTUBE));
20   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "advection", NS_ADVECTION));
21   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "blasius", NS_BLASIUS));
22   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "channel", NS_CHANNEL));
23   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "gaussian_wave", NS_GAUSSIAN_WAVE));
24   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "newtonian", NS_NEWTONIAN_IG));
25   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "taylor_green", NS_TAYLOR_GREEN));
26   PetscFunctionReturn(PETSC_SUCCESS);
27 }
28 
29 // Process general command line options
30 PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx, SimpleBC bc) {
31   PetscBool ceed_flag    = PETSC_FALSE;
32   PetscBool problem_flag = PETSC_FALSE;
33   PetscBool option_set   = PETSC_FALSE;
34 
35   PetscFunctionBeginUser;
36   {
37     PetscInt num_options;
38 
39     PetscCall(PetscOptionsLeftGet(NULL, &num_options, NULL, NULL));
40     PetscCheck(num_options > 0, comm, PETSC_ERR_USER_INPUT,
41                "Command line options required."
42                " Please consult the documentation to see which options are required.");
43     PetscCall(PetscOptionsLeftRestore(NULL, &num_options, NULL, NULL));
44   }
45 
46   PetscCall(RegisterProblems_NS(app_ctx));
47   PetscOptionsBegin(comm, NULL, "HONEE - High-Order Navier-stokes Equation Evaluator", NULL);
48 
49   PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, app_ctx->ceed_resource, app_ctx->ceed_resource,
50                                sizeof(app_ctx->ceed_resource), &ceed_flag));
51 
52   app_ctx->test_type = TESTTYPE_NONE;
53   PetscCall(PetscOptionsEnum("-test_type", "Type of test to run", NULL, TestTypes, (PetscEnum)(app_ctx->test_type), (PetscEnum *)&app_ctx->test_type,
54                              NULL));
55 
56   app_ctx->test_tol = 1E-11;
57   PetscCall(PetscOptionsScalar("-compare_final_state_atol", "Test absolute tolerance", NULL, app_ctx->test_tol, &app_ctx->test_tol, NULL));
58 
59   PetscCall(PetscOptionsString("-compare_final_state_filename", "Test filename", NULL, app_ctx->test_file_path, app_ctx->test_file_path,
60                                sizeof(app_ctx->test_file_path), NULL));
61 
62   PetscCall(PetscOptionsFList("-problem", "Problem to solve", NULL, app_ctx->problems, app_ctx->problem_name, app_ctx->problem_name,
63                               sizeof(app_ctx->problem_name), &problem_flag));
64 
65   app_ctx->viz_refine = 0;
66   PetscCall(PetscOptionsInt("-viz_refine", "Regular refinement levels for visualization", NULL, app_ctx->viz_refine, &app_ctx->viz_refine, NULL));
67 
68   app_ctx->checkpoint_interval = 10;
69   app_ctx->checkpoint_vtk      = PETSC_FALSE;
70   PetscCall(PetscOptionsDeprecated("-output_freq", "-checkpoint_interval", "libCEED 0.11.1", "Use -checkpoint_vtk true to include VTK output"));
71   PetscCall(PetscOptionsInt("-output_freq", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
72                             &app_ctx->checkpoint_interval, &option_set));
73   if (option_set) app_ctx->checkpoint_vtk = PETSC_TRUE;
74   PetscCall(PetscOptionsInt("-checkpoint_interval", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
75                             &app_ctx->checkpoint_interval, NULL));
76   PetscCall(PetscOptionsBool("-checkpoint_vtk", "Include VTK (*.vtu) output at each binary checkpoint", NULL, app_ctx->checkpoint_vtk,
77                              &app_ctx->checkpoint_vtk, NULL));
78 
79   PetscCall(PetscOptionsBool("-output_add_stepnum2bin", "Add step number to the binary outputs", NULL, app_ctx->add_stepnum2bin,
80                              &app_ctx->add_stepnum2bin, NULL));
81 
82   PetscCall(PetscStrncpy(app_ctx->output_dir, ".", 2));
83   PetscCall(PetscOptionsString("-output_dir", "Output directory", NULL, app_ctx->output_dir, app_ctx->output_dir, sizeof(app_ctx->output_dir), NULL));
84   PetscMPIInt rank;
85   MPI_Comm_rank(comm, &rank);
86   if (!rank) PetscCall(PetscMkdir(app_ctx->output_dir));
87 
88   PetscCall(PetscOptionsString("-continue_filename", "Filename to get initial condition from", NULL, app_ctx->cont_file, app_ctx->cont_file,
89                                sizeof(app_ctx->cont_file), NULL));
90   if (app_ctx->cont_file[0] != '\0') app_ctx->use_continue_file = PETSC_TRUE;
91 
92   PetscCall(
93       PetscOptionsDeprecated("-continue", NULL, "HONEE 0.0.0", "Set -continue_filename to non-empty string to continue from previous solution"));
94   PetscCall(
95       PetscOptionsDeprecated("-continue_time_filename", NULL, "HONEE 0.0.0", "HONEE no longer supports reading in solution times from binary file"));
96 
97   app_ctx->degree = 1;
98   PetscCall(PetscOptionsInt("-degree", "Polynomial degree of finite elements", NULL, app_ctx->degree, &app_ctx->degree, NULL));
99 
100   app_ctx->q_extra = 0;
101   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, app_ctx->q_extra, &app_ctx->q_extra, NULL));
102 
103   {
104     PetscBool option_set;
105     char      amat_type[256] = "";
106     PetscCall(PetscOptionsFList("-amat_type", "Set the type of Amat distinct from Pmat (-dm_mat_type)", NULL, MatList, amat_type, amat_type,
107                                 sizeof(amat_type), &option_set));
108     if (option_set) PetscCall(PetscStrallocpy(amat_type, (char **)&app_ctx->amat_type));
109   }
110   {
111     PetscBool option_set;
112     PetscCall(PetscOptionsHasName(NULL, NULL, "-pmat_pbdiagonal", &option_set));
113     if (option_set) PetscCall(PetscPrintf(comm, "Warning! -pmat_pbdiagonal no longer used. Pmat assembly determined from -pc_type setting\n"));
114   }
115 
116   // Provide default ceed resource if not specified
117   if (!ceed_flag) {
118     const char *ceed_resource = "/cpu/self";
119     strncpy(app_ctx->ceed_resource, ceed_resource, 10);
120   }
121   // If we request a GPU, make sure PETSc has initialized its device (which is
122   // MPI-aware in case multiple devices are available) before CeedInit so that
123   // PETSc and libCEED agree about which device to use.
124   if (strncmp(app_ctx->ceed_resource, "/gpu", 4) == 0) PetscCall(PetscDeviceInitialize(PETSC_DEVICE_DEFAULT()));
125 
126   // Provide default problem if not specified
127   if (!problem_flag) {
128     const char *problem_name = "density_current";
129     strncpy(app_ctx->problem_name, problem_name, 16);
130   }
131 
132   // Statistics Options
133   app_ctx->turb_spanstats_collect_interval = 1;
134   PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_collect_interval", "Number of timesteps between statistics collection", NULL,
135                             app_ctx->turb_spanstats_collect_interval, &app_ctx->turb_spanstats_collect_interval, NULL));
136 
137   app_ctx->turb_spanstats_viewer_interval = -1;
138   PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_viewer_interval", "Number of timesteps between statistics viewer writing", NULL,
139                             app_ctx->turb_spanstats_viewer_interval, &app_ctx->turb_spanstats_viewer_interval, NULL));
140 
141   PetscCall(PetscOptionsViewer("-ts_monitor_turbulence_spanstats_viewer", "Viewer for the statistics", NULL, &app_ctx->turb_spanstats_viewer,
142                                &app_ctx->turb_spanstats_viewer_format, &app_ctx->turb_spanstats_enable));
143 
144   PetscCall(PetscOptionsViewer("-ts_monitor_wall_force", "Viewer for force on each (no-slip) wall", NULL, &app_ctx->wall_forces.viewer,
145                                &app_ctx->wall_forces.viewer_format, NULL));
146 
147   // SGS Model Options
148   app_ctx->sgs_model_type = SGS_MODEL_NONE;
149   PetscCall(PetscOptionsEnum("-sgs_model_type", "Subgrid Stress Model type", NULL, SGSModelTypes, (PetscEnum)app_ctx->sgs_model_type,
150                              (PetscEnum *)&app_ctx->sgs_model_type, NULL));
151 
152   PetscCall(PetscOptionsBool("-diff_filter_monitor", "Enable differential filtering TSMonitor", NULL, app_ctx->diff_filter_monitor,
153                              &app_ctx->diff_filter_monitor, NULL));
154 
155   // Mesh Transformation Options
156   app_ctx->mesh_transform_type = MESH_TRANSFORM_NONE;
157   PetscCall(PetscOptionsEnum("-mesh_transform", "Mesh transform to perform", NULL, MeshTransformTypes, (PetscEnum)app_ctx->mesh_transform_type,
158                              (PetscEnum *)&app_ctx->mesh_transform_type, NULL));
159 
160   PetscCall(
161       PetscOptionsBool("-sgs_train_enable", "Enable Data-Driven SGS training", NULL, app_ctx->sgs_train_enable, &app_ctx->sgs_train_enable, NULL));
162 
163   PetscCall(PetscOptionsEnum("-div_diff_flux_projection_method", "Method of divergence of diffusive flux projection", NULL,
164                              DivDiffFluxProjectionMethods, (PetscEnum)(app_ctx->divFdiffproj_method), (PetscEnum *)&app_ctx->divFdiffproj_method,
165                              NULL));
166 
167   PetscOptionsEnd();
168   PetscFunctionReturn(PETSC_SUCCESS);
169 }
170