xref: /libCEED/examples/fluids/src/cloptions.c (revision 88626eed6564cd43033d3137230605fb5f962840)
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   PetscErrorCode   ierr;
19   PetscFunctionBeginUser;
20 
21   ierr = PetscFunctionListAdd(&app_ctx->problems, "density_current",
22                               NS_DENSITY_CURRENT); CHKERRQ(ierr);
23 
24   ierr = PetscFunctionListAdd(&app_ctx->problems, "euler_vortex",
25                               NS_EULER_VORTEX); CHKERRQ(ierr);
26 
27   ierr = PetscFunctionListAdd(&app_ctx->problems, "advection",
28                               NS_ADVECTION); CHKERRQ(ierr);
29 
30   ierr = PetscFunctionListAdd(&app_ctx->problems, "advection2d",
31                               NS_ADVECTION2D); CHKERRQ(ierr);
32 
33   ierr = PetscFunctionListAdd(&app_ctx->problems, "blasius",
34                               NS_BLASIUS); CHKERRQ(ierr);
35 
36   ierr = PetscFunctionListAdd(&app_ctx->problems, "channel",
37                               NS_CHANNEL); CHKERRQ(ierr);
38 
39   PetscFunctionReturn(0);
40 }
41 
42 // Process general command line options
43 PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx,
44     SimpleBC bc) {
45 
46   PetscBool ceed_flag = PETSC_FALSE;
47   PetscBool problem_flag = PETSC_FALSE;
48   PetscErrorCode ierr;
49   PetscFunctionBeginUser;
50 
51   PetscOptionsBegin(comm, NULL, "Navier-Stokes in PETSc with libCEED",
52                     NULL);
53 
54   ierr = PetscOptionsString("-ceed", "CEED resource specifier",
55                             NULL, app_ctx->ceed_resource, app_ctx->ceed_resource,
56                             sizeof(app_ctx->ceed_resource), &ceed_flag); CHKERRQ(ierr);
57 
58   app_ctx->test_mode = PETSC_FALSE;
59   ierr = PetscOptionsBool("-test", "Run in test mode",
60                           NULL, app_ctx->test_mode, &app_ctx->test_mode, NULL); CHKERRQ(ierr);
61 
62   app_ctx->test_tol = 1E-11;
63   ierr = PetscOptionsScalar("-compare_final_state_atol",
64                             "Test absolute tolerance",
65                             NULL, app_ctx->test_tol, &app_ctx->test_tol, NULL); CHKERRQ(ierr);
66 
67   ierr = PetscOptionsString("-compare_final_state_filename", "Test filename",
68                             NULL, app_ctx->file_path, app_ctx->file_path,
69                             sizeof(app_ctx->file_path), NULL); CHKERRQ(ierr);
70 
71   ierr = PetscOptionsFList("-problem", "Problem to solve", NULL,
72                            app_ctx->problems,
73                            app_ctx->problem_name, app_ctx->problem_name, sizeof(app_ctx->problem_name),
74                            &problem_flag); CHKERRQ(ierr);
75 
76   app_ctx->viz_refine = 0;
77   ierr = PetscOptionsInt("-viz_refine",
78                          "Regular refinement levels for visualization",
79                          NULL, app_ctx->viz_refine, &app_ctx->viz_refine, NULL); CHKERRQ(ierr);
80 
81   app_ctx->output_freq = 10;
82   ierr = PetscOptionsInt("-output_freq",
83                          "Frequency of output, in number of steps",
84                          NULL, app_ctx->output_freq, &app_ctx->output_freq, NULL); CHKERRQ(ierr);
85 
86   app_ctx->cont_steps = 0;
87   ierr = PetscOptionsInt("-continue", "Continue from previous solution",
88                          NULL, app_ctx->cont_steps, &app_ctx->cont_steps, NULL); CHKERRQ(ierr);
89 
90   app_ctx->degree = 1;
91   ierr = PetscOptionsInt("-degree", "Polynomial degree of finite elements",
92                          NULL, app_ctx->degree, &app_ctx->degree, NULL); CHKERRQ(ierr);
93 
94   app_ctx->q_extra = 2;
95   ierr = PetscOptionsInt("-q_extra", "Number of extra quadrature points",
96                          NULL, app_ctx->q_extra, &app_ctx->q_extra, NULL); CHKERRQ(ierr);
97 
98   ierr = PetscStrncpy(app_ctx->output_dir, ".", 2); CHKERRQ(ierr);
99   ierr = PetscOptionsString("-output_dir", "Output directory",
100                             NULL, app_ctx->output_dir, app_ctx->output_dir,
101                             sizeof(app_ctx->output_dir), NULL); CHKERRQ(ierr);
102 
103   // Provide default ceed resource if not specified
104   if (!ceed_flag) {
105     const char *ceed_resource = "/cpu/self";
106     strncpy(app_ctx->ceed_resource, ceed_resource, 10);
107   }
108 
109   // Provide default problem if not specified
110   if (!problem_flag) {
111     const char *problem_name = "density_current";
112     strncpy(app_ctx->problem_name, problem_name, 16);
113   }
114 
115   // Wall Boundary Conditions
116   bc->num_wall = 16;
117   PetscBool flg;
118   ierr = PetscOptionsIntArray("-bc_wall",
119                               "Face IDs to apply wall BC",
120                               NULL, bc->walls, &bc->num_wall, NULL); CHKERRQ(ierr);
121   bc->num_comps = 5;
122   ierr = PetscOptionsIntArray("-wall_comps",
123                               "An array of constrained component numbers",
124                               NULL, bc->wall_comps, &bc->num_comps, &flg); CHKERRQ(ierr);
125   // Slip Boundary Conditions
126   for (PetscInt j=0; j<3; j++) {
127     bc->num_slip[j] = 16;
128     PetscBool flg;
129     const char *flags[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"};
130     ierr = PetscOptionsIntArray(flags[j],
131                                 "Face IDs to apply slip BC",
132                                 NULL, bc->slips[j], &bc->num_slip[j], &flg); CHKERRQ(ierr);
133     if (flg) bc->user_bc = PETSC_TRUE;
134   }
135 
136   // Error if wall and slip BCs are set on the same face
137   if (bc->user_bc)
138     for (PetscInt c = 0; c < 3; c++)
139       for (PetscInt s = 0; s < bc->num_slip[c]; s++)
140         for (PetscInt w = 0; w < bc->num_wall; w++)
141           if (bc->slips[c][s] == bc->walls[w])
142             SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
143                     "Boundary condition already set on face %" PetscInt_FMT "!\n",
144                     bc->walls[w]);
145 
146   // Inflow BCs
147   bc->num_inflow = 16;
148   ierr = PetscOptionsIntArray("-bc_inflow",
149                               "Face IDs to apply inflow BC",
150                               NULL, bc->inflows, &bc->num_inflow, NULL); CHKERRQ(ierr);
151   // Outflow BCs
152   bc->num_outflow = 16;
153   ierr = PetscOptionsIntArray("-bc_outflow",
154                               "Face IDs to apply outflow BC",
155                               NULL, bc->outflows, &bc->num_outflow, NULL); CHKERRQ(ierr);
156 
157   PetscOptionsEnd();
158 
159   PetscFunctionReturn(0);
160 }
161