xref: /honee/src/cloptions.c (revision 91a368018eb14c2bc7a6cd3704e6f34e7aa15bb0)
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 "../navierstokes.h"
12 
13 // Register problems to be available on the command line
14 PetscErrorCode RegisterProblems_NS(AppCtx app_ctx) {
15 
16 
17   app_ctx->problems = NULL;
18   PetscFunctionBeginUser;
19 
20   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "density_current",
21                                  NS_DENSITY_CURRENT));
22 
23   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "euler_vortex",
24                                  NS_EULER_VORTEX));
25 
26   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "shocktube",
27                                  NS_SHOCKTUBE));
28 
29   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "advection",
30                                  NS_ADVECTION));
31 
32   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "advection2d",
33                                  NS_ADVECTION2D));
34 
35   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "blasius",
36                                  NS_BLASIUS));
37 
38   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "channel",
39                                  NS_CHANNEL));
40 
41   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "newtonian_wave",
42                                  NS_NEWTONIAN_WAVE));
43 
44   PetscFunctionReturn(0);
45 }
46 
47 // Process general command line options
48 PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx,
49     SimpleBC bc) {
50 
51   PetscBool ceed_flag    = PETSC_FALSE;
52   PetscBool problem_flag = PETSC_FALSE;
53   PetscBool option_set   = PETSC_FALSE;
54   PetscErrorCode ierr;
55   PetscFunctionBeginUser;
56 
57   PetscOptionsBegin(comm, NULL, "Navier-Stokes in PETSc with libCEED",
58                     NULL);
59 
60   ierr = PetscOptionsString("-ceed", "CEED resource specifier",
61                             NULL, app_ctx->ceed_resource, app_ctx->ceed_resource,
62                             sizeof(app_ctx->ceed_resource), &ceed_flag); CHKERRQ(ierr);
63 
64   app_ctx->test_mode = PETSC_FALSE;
65   ierr = PetscOptionsBool("-test", "Run in test mode",
66                           NULL, app_ctx->test_mode, &app_ctx->test_mode, NULL); CHKERRQ(ierr);
67 
68   app_ctx->test_tol = 1E-11;
69   ierr = PetscOptionsScalar("-compare_final_state_atol",
70                             "Test absolute tolerance",
71                             NULL, app_ctx->test_tol, &app_ctx->test_tol, NULL); CHKERRQ(ierr);
72 
73   ierr = PetscOptionsString("-compare_final_state_filename", "Test filename",
74                             NULL, app_ctx->file_path, app_ctx->file_path,
75                             sizeof(app_ctx->file_path), NULL); CHKERRQ(ierr);
76 
77   ierr = PetscOptionsFList("-problem", "Problem to solve", NULL,
78                            app_ctx->problems,
79                            app_ctx->problem_name, app_ctx->problem_name, sizeof(app_ctx->problem_name),
80                            &problem_flag); CHKERRQ(ierr);
81 
82   app_ctx->viz_refine = 0;
83   ierr = PetscOptionsInt("-viz_refine",
84                          "Regular refinement levels for visualization",
85                          NULL, app_ctx->viz_refine, &app_ctx->viz_refine, NULL); CHKERRQ(ierr);
86 
87   app_ctx->output_freq = 10;
88   ierr = PetscOptionsInt("-output_freq",
89                          "Frequency of output, in number of steps",
90                          NULL, app_ctx->output_freq, &app_ctx->output_freq, NULL); CHKERRQ(ierr);
91 
92   PetscCall(PetscOptionsBool("-output_add_stepnum2bin",
93                              "Add step number to the binary outputs",
94                              NULL, app_ctx->add_stepnum2bin, &app_ctx->add_stepnum2bin, NULL));
95 
96   PetscCall(PetscStrncpy(app_ctx->output_dir, ".", 2));
97   PetscCall(PetscOptionsString("-output_dir", "Output directory",
98                                NULL, app_ctx->output_dir, app_ctx->output_dir,
99                                sizeof(app_ctx->output_dir), NULL));
100 
101   app_ctx->cont_steps = 0;
102   ierr = PetscOptionsInt("-continue", "Continue from previous solution",
103                          NULL, app_ctx->cont_steps, &app_ctx->cont_steps, NULL); CHKERRQ(ierr);
104 
105   PetscCall(PetscStrcpy(app_ctx->cont_file, "[output_dir]/ns-solution.bin"));
106   PetscCall(PetscOptionsString("-continue_filename",
107                                "Filename to get initial condition from",
108                                NULL, app_ctx->cont_file, app_ctx->cont_file,
109                                sizeof(app_ctx->cont_file), &option_set));
110   if(!option_set) PetscCall(PetscSNPrintf(app_ctx->cont_file,
111                                             sizeof app_ctx->cont_file, "%s/ns-solution.bin",
112                                             app_ctx->output_dir));
113 
114   PetscCall(PetscStrcpy(app_ctx->cont_time_file, "[output_dir]/ns-time.bin"));
115   PetscCall(PetscOptionsString("-continue_time_filename",
116                                "Filename to get initial condition time from",
117                                NULL, app_ctx->cont_time_file, app_ctx->cont_time_file,
118                                sizeof(app_ctx->cont_time_file), &option_set));
119   if(!option_set) PetscCall(PetscSNPrintf(app_ctx->cont_time_file,
120                                             sizeof app_ctx->cont_time_file, "%s/ns-time.bin",
121                                             app_ctx->output_dir));
122 
123   app_ctx->degree = 1;
124   ierr = PetscOptionsInt("-degree", "Polynomial degree of finite elements",
125                          NULL, app_ctx->degree, &app_ctx->degree, NULL); CHKERRQ(ierr);
126 
127   app_ctx->q_extra = 2;
128   ierr = PetscOptionsInt("-q_extra", "Number of extra quadrature points",
129                          NULL, app_ctx->q_extra, &app_ctx->q_extra, NULL); CHKERRQ(ierr);
130 
131   {
132     PetscBool option_set;
133     char amat_type[256] = "";
134     PetscCall(PetscOptionsFList("-amat_type",
135                                 "Set the type of Amat distinct from Pmat (-dm_mat_type)",
136                                 NULL, MatList, amat_type, amat_type, sizeof(amat_type), &option_set));
137     if (option_set) PetscCall(PetscStrallocpy(amat_type,
138                                 (char **)&app_ctx->amat_type));
139   }
140   PetscCall(PetscOptionsBool("-pmat_pbdiagonal",
141                              "Assemble only point-block diagonal for Pmat", NULL, app_ctx->pmat_pbdiagonal,
142                              &app_ctx->pmat_pbdiagonal, NULL));
143 
144   // Provide default ceed resource if not specified
145   if (!ceed_flag) {
146     const char *ceed_resource = "/cpu/self";
147     strncpy(app_ctx->ceed_resource, ceed_resource, 10);
148   }
149 
150   // Provide default problem if not specified
151   if (!problem_flag) {
152     const char *problem_name = "density_current";
153     strncpy(app_ctx->problem_name, problem_name, 16);
154   }
155 
156   // Wall Boundary Conditions
157   bc->num_wall = 16;
158   PetscBool flg;
159   ierr = PetscOptionsIntArray("-bc_wall",
160                               "Face IDs to apply wall BC",
161                               NULL, bc->walls, &bc->num_wall, NULL); CHKERRQ(ierr);
162   bc->num_comps = 5;
163   ierr = PetscOptionsIntArray("-wall_comps",
164                               "An array of constrained component numbers",
165                               NULL, bc->wall_comps, &bc->num_comps, &flg); CHKERRQ(ierr);
166   // Slip Boundary Conditions
167   for (PetscInt j=0; j<3; j++) {
168     bc->num_slip[j] = 16;
169     PetscBool flg;
170     const char *flags[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"};
171     ierr = PetscOptionsIntArray(flags[j],
172                                 "Face IDs to apply slip BC",
173                                 NULL, bc->slips[j], &bc->num_slip[j], &flg); CHKERRQ(ierr);
174     if (flg) bc->user_bc = PETSC_TRUE;
175   }
176 
177   // Error if wall and slip BCs are set on the same face
178   if (bc->user_bc)
179     for (PetscInt c = 0; c < 3; c++)
180       for (PetscInt s = 0; s < bc->num_slip[c]; s++)
181         for (PetscInt w = 0; w < bc->num_wall; w++)
182           if (bc->slips[c][s] == bc->walls[w])
183             SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
184                     "Boundary condition already set on face %" PetscInt_FMT "!\n",
185                     bc->walls[w]);
186 
187   // Inflow BCs
188   bc->num_inflow = 16;
189   ierr = PetscOptionsIntArray("-bc_inflow",
190                               "Face IDs to apply inflow BC",
191                               NULL, bc->inflows, &bc->num_inflow, NULL); CHKERRQ(ierr);
192   // Outflow BCs
193   bc->num_outflow = 16;
194   ierr = PetscOptionsIntArray("-bc_outflow",
195                               "Face IDs to apply outflow BC",
196                               NULL, bc->outflows, &bc->num_outflow, NULL); CHKERRQ(ierr);
197   // Freestream BCs
198   bc->num_freestream = 16;
199   ierr = PetscOptionsIntArray("-bc_freestream",
200                               "Face IDs to apply freestream BC",
201                               NULL, bc->freestreams, &bc->num_freestream, NULL); CHKERRQ(ierr);
202 
203   PetscOptionsEnd();
204 
205   PetscFunctionReturn(0);
206 }
207