xref: /honee/src/cloptions.c (revision 93a8a03ebfacd92b49e46e47da9f6eb46ee61821)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 /// @file
9 /// Command line option processing for Navier-Stokes example using PETSc
10 
11 #include <petscdevice.h>
12 #include <petscsys.h>
13 
14 #include "../navierstokes.h"
15 
16 // Register problems to be available on the command line
17 PetscErrorCode RegisterProblems_NS(AppCtx app_ctx) {
18   app_ctx->problems = NULL;
19   PetscFunctionBeginUser;
20 
21   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "density_current", NS_DENSITY_CURRENT));
22   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "euler_vortex", NS_EULER_VORTEX));
23   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "shocktube", NS_SHOCKTUBE));
24   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "advection", NS_ADVECTION));
25   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "advection2d", NS_ADVECTION2D));
26   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "blasius", NS_BLASIUS));
27   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "channel", NS_CHANNEL));
28   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "gaussian_wave", NS_GAUSSIAN_WAVE));
29   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "newtonian", NS_NEWTONIAN_IG));
30 
31   PetscFunctionReturn(PETSC_SUCCESS);
32 }
33 
34 // Process general command line options
35 PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx, SimpleBC bc) {
36   PetscBool ceed_flag    = PETSC_FALSE;
37   PetscBool problem_flag = PETSC_FALSE;
38   PetscBool option_set   = PETSC_FALSE;
39 
40   PetscFunctionBeginUser;
41 
42   PetscOptionsBegin(comm, NULL, "Navier-Stokes in PETSc with libCEED", NULL);
43 
44   PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, app_ctx->ceed_resource, app_ctx->ceed_resource,
45                                sizeof(app_ctx->ceed_resource), &ceed_flag));
46 
47   app_ctx->test_type = TESTTYPE_NONE;
48   PetscCall(PetscOptionsEnum("-test_type", "Type of test to run", NULL, TestTypes, (PetscEnum)(app_ctx->test_type), (PetscEnum *)&app_ctx->test_type,
49                              NULL));
50 
51   app_ctx->test_tol = 1E-11;
52   PetscCall(PetscOptionsScalar("-compare_final_state_atol", "Test absolute tolerance", NULL, app_ctx->test_tol, &app_ctx->test_tol, NULL));
53 
54   PetscCall(PetscOptionsString("-compare_final_state_filename", "Test filename", NULL, app_ctx->test_file_path, app_ctx->test_file_path,
55                                sizeof(app_ctx->test_file_path), NULL));
56 
57   PetscCall(PetscOptionsFList("-problem", "Problem to solve", NULL, app_ctx->problems, app_ctx->problem_name, app_ctx->problem_name,
58                               sizeof(app_ctx->problem_name), &problem_flag));
59 
60   app_ctx->viz_refine = 0;
61   PetscCall(PetscOptionsInt("-viz_refine", "Regular refinement levels for visualization", NULL, app_ctx->viz_refine, &app_ctx->viz_refine, NULL));
62 
63   app_ctx->checkpoint_interval = 10;
64   app_ctx->checkpoint_vtk      = PETSC_FALSE;
65   PetscCall(PetscOptionsDeprecated("-output_freq", "-checkpoint_interval", "libCEED 0.11.1", "Use -checkpoint_vtk true to include VTK output"));
66   PetscCall(PetscOptionsInt("-output_freq", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
67                             &app_ctx->checkpoint_interval, &option_set));
68   if (option_set) app_ctx->checkpoint_vtk = PETSC_TRUE;
69   PetscCall(PetscOptionsInt("-checkpoint_interval", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
70                             &app_ctx->checkpoint_interval, NULL));
71   PetscCall(PetscOptionsBool("-checkpoint_vtk", "Include VTK (*.vtu) output at each binary checkpoint", NULL, app_ctx->checkpoint_vtk,
72                              &app_ctx->checkpoint_vtk, NULL));
73 
74   PetscCall(PetscOptionsBool("-output_add_stepnum2bin", "Add step number to the binary outputs", NULL, app_ctx->add_stepnum2bin,
75                              &app_ctx->add_stepnum2bin, NULL));
76 
77   PetscCall(PetscStrncpy(app_ctx->output_dir, ".", 2));
78   PetscCall(PetscOptionsString("-output_dir", "Output directory", NULL, app_ctx->output_dir, app_ctx->output_dir, sizeof(app_ctx->output_dir), NULL));
79 
80   app_ctx->cont_steps = 0;
81   PetscCall(PetscOptionsInt("-continue", "Continue from previous solution", NULL, app_ctx->cont_steps, &app_ctx->cont_steps, NULL));
82 
83   PetscCall(PetscStrcpy(app_ctx->cont_file, "[output_dir]/ns-solution.bin"));
84   PetscCall(PetscOptionsString("-continue_filename", "Filename to get initial condition from", NULL, app_ctx->cont_file, app_ctx->cont_file,
85                                sizeof(app_ctx->cont_file), &option_set));
86   if (!option_set) PetscCall(PetscSNPrintf(app_ctx->cont_file, sizeof app_ctx->cont_file, "%s/ns-solution.bin", app_ctx->output_dir));
87   if (option_set && app_ctx->cont_steps == 0) app_ctx->cont_steps = -1;  // Read time from file
88 
89   PetscCall(PetscStrcpy(app_ctx->cont_time_file, "[output_dir]/ns-time.bin"));
90   PetscCall(PetscOptionsString("-continue_time_filename", "Filename to get initial condition time from", NULL, app_ctx->cont_time_file,
91                                app_ctx->cont_time_file, sizeof(app_ctx->cont_time_file), &option_set));
92   if (!option_set) PetscCall(PetscSNPrintf(app_ctx->cont_time_file, sizeof app_ctx->cont_time_file, "%s/ns-time.bin", app_ctx->output_dir));
93 
94   app_ctx->degree = 1;
95   PetscCall(PetscOptionsInt("-degree", "Polynomial degree of finite elements", NULL, app_ctx->degree, &app_ctx->degree, NULL));
96 
97   app_ctx->q_extra = 0;
98   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, app_ctx->q_extra, &app_ctx->q_extra, NULL));
99 
100   {
101     PetscBool option_set;
102     char      amat_type[256] = "";
103     PetscCall(PetscOptionsFList("-amat_type", "Set the type of Amat distinct from Pmat (-dm_mat_type)", NULL, MatList, amat_type, amat_type,
104                                 sizeof(amat_type), &option_set));
105     if (option_set) PetscCall(PetscStrallocpy(amat_type, (char **)&app_ctx->amat_type));
106   }
107   PetscCall(PetscOptionsBool("-pmat_pbdiagonal", "Assemble only point-block diagonal for Pmat", NULL, app_ctx->pmat_pbdiagonal,
108                              &app_ctx->pmat_pbdiagonal, NULL));
109 
110   // Provide default ceed resource if not specified
111   if (!ceed_flag) {
112     const char *ceed_resource = "/cpu/self";
113     strncpy(app_ctx->ceed_resource, ceed_resource, 10);
114   }
115   // If we request a GPU, make sure PETSc has initialized its device (which is
116   // MPI-aware in case multiple devices are available) before CeedInit so that
117   // PETSc and libCEED agree about which device to use.
118   if (strncmp(app_ctx->ceed_resource, "/gpu", 4) == 0) PetscCall(PetscDeviceInitialize(PETSC_DEVICE_DEFAULT()));
119 
120   // Provide default problem if not specified
121   if (!problem_flag) {
122     const char *problem_name = "density_current";
123     strncpy(app_ctx->problem_name, problem_name, 16);
124   }
125 
126   // Wall Boundary Conditions
127   bc->num_wall = 16;
128   PetscBool flg;
129   PetscCall(PetscOptionsIntArray("-bc_wall", "Face IDs to apply wall BC", NULL, bc->walls, &bc->num_wall, NULL));
130   bc->num_comps = 5;
131   PetscCall(PetscOptionsIntArray("-wall_comps", "An array of constrained component numbers", NULL, bc->wall_comps, &bc->num_comps, &flg));
132   // Slip Boundary Conditions
133   for (PetscInt j = 0; j < 3; j++) {
134     bc->num_slip[j] = 16;
135     PetscBool   flg;
136     const char *flags[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"};
137     PetscCall(PetscOptionsIntArray(flags[j], "Face IDs to apply slip BC", NULL, bc->slips[j], &bc->num_slip[j], &flg));
138     if (flg) bc->user_bc = PETSC_TRUE;
139   }
140 
141   // Error if wall and slip BCs are set on the same face
142   if (bc->user_bc) {
143     for (PetscInt c = 0; c < 3; c++) {
144       for (PetscInt s = 0; s < bc->num_slip[c]; s++) {
145         for (PetscInt w = 0; w < bc->num_wall; w++) {
146           PetscCheck(bc->slips[c][s] != bc->walls[w], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
147                      "Boundary condition already set on face %" PetscInt_FMT "!\n", bc->walls[w]);
148         }
149       }
150     }
151   }
152   app_ctx->wall_forces.num_wall = bc->num_wall;
153   PetscMalloc1(bc->num_wall, &app_ctx->wall_forces.walls);
154   PetscCall(PetscArraycpy(app_ctx->wall_forces.walls, bc->walls, bc->num_wall));
155 
156   // Inflow BCs
157   bc->num_inflow = 16;
158   PetscCall(PetscOptionsIntArray("-bc_inflow", "Face IDs to apply inflow BC", NULL, bc->inflows, &bc->num_inflow, NULL));
159   // Outflow BCs
160   bc->num_outflow = 16;
161   PetscCall(PetscOptionsIntArray("-bc_outflow", "Face IDs to apply outflow BC", NULL, bc->outflows, &bc->num_outflow, NULL));
162   // Freestream BCs
163   bc->num_freestream = 16;
164   PetscCall(PetscOptionsIntArray("-bc_freestream", "Face IDs to apply freestream BC", NULL, bc->freestreams, &bc->num_freestream, NULL));
165 
166   // Statistics Options
167   app_ctx->turb_spanstats_collect_interval = 1;
168   PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_collect_interval", "Number of timesteps between statistics collection", NULL,
169                             app_ctx->turb_spanstats_collect_interval, &app_ctx->turb_spanstats_collect_interval, NULL));
170 
171   app_ctx->turb_spanstats_viewer_interval = -1;
172   PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_viewer_interval", "Number of timesteps between statistics viewer writing", NULL,
173                             app_ctx->turb_spanstats_viewer_interval, &app_ctx->turb_spanstats_viewer_interval, NULL));
174 
175   PetscCall(PetscOptionsViewer("-ts_monitor_turbulence_spanstats_viewer", "Viewer for the statistics", NULL, &app_ctx->turb_spanstats_viewer,
176                                &app_ctx->turb_spanstats_viewer_format, &app_ctx->turb_spanstats_enable));
177 
178   PetscCall(PetscOptionsViewer("-ts_monitor_wall_force", "Viewer for force on each (no-slip) wall", NULL, &app_ctx->wall_forces.viewer,
179                                &app_ctx->wall_forces.viewer_format, NULL));
180 
181   // SGS Model Options
182   app_ctx->sgs_model_type = SGS_MODEL_NONE;
183   PetscCall(PetscOptionsEnum("-sgs_model_type", "Subgrid Stress Model type", NULL, SGSModelTypes, (PetscEnum)app_ctx->sgs_model_type,
184                              (PetscEnum *)&app_ctx->sgs_model_type, NULL));
185 
186   PetscCall(PetscOptionsBool("-diff_filter_monitor", "Enable differential filtering TSMonitor", NULL, app_ctx->diff_filter_monitor,
187                              &app_ctx->diff_filter_monitor, NULL));
188 
189   PetscOptionsEnd();
190 
191   PetscFunctionReturn(PETSC_SUCCESS);
192 }
193