xref: /libCEED/examples/fluids/src/cloptions.c (revision 12d74c386ca5dafc3459a11ee0c74b64b71b2bd0)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
377841947SLeila Ghaffari //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
577841947SLeila Ghaffari //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
777841947SLeila Ghaffari 
877841947SLeila Ghaffari /// @file
977841947SLeila Ghaffari /// Command line option processing for Navier-Stokes example using PETSc
1077841947SLeila Ghaffari 
110992c3d1SJed Brown #include <petscdevice.h>
1249aac155SJeremy L Thompson #include <petscsys.h>
130992c3d1SJed Brown 
1477841947SLeila Ghaffari #include "../navierstokes.h"
1577841947SLeila Ghaffari 
1677841947SLeila Ghaffari // Register problems to be available on the command line
1777841947SLeila Ghaffari PetscErrorCode RegisterProblems_NS(AppCtx app_ctx) {
1877841947SLeila Ghaffari   app_ctx->problems = NULL;
1977841947SLeila Ghaffari 
20f17d818dSJames Wright   PetscFunctionBeginUser;
212b730f8bSJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "density_current", NS_DENSITY_CURRENT));
222b730f8bSJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "euler_vortex", NS_EULER_VORTEX));
232b730f8bSJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "shocktube", NS_SHOCKTUBE));
242b730f8bSJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "advection", NS_ADVECTION));
252b730f8bSJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "blasius", NS_BLASIUS));
262b730f8bSJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "channel", NS_CHANNEL));
27530ad8c4SKenneth E. Jansen   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "gaussian_wave", NS_GAUSSIAN_WAVE));
28d310b3d3SAdeleke O. Bankole   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "newtonian", NS_NEWTONIAN_IG));
2914712a6bSJames Wright   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "taylor_green", NS_TAYLOR_GREEN));
30ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
3177841947SLeila Ghaffari }
3277841947SLeila Ghaffari 
3377841947SLeila Ghaffari // Process general command line options
342b730f8bSJeremy L Thompson PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx, SimpleBC bc) {
3577841947SLeila Ghaffari   PetscBool ceed_flag    = PETSC_FALSE;
3677841947SLeila Ghaffari   PetscBool problem_flag = PETSC_FALSE;
3769293791SJames Wright   PetscBool option_set   = PETSC_FALSE;
382b730f8bSJeremy L Thompson 
3977841947SLeila Ghaffari   PetscFunctionBeginUser;
40*12d74c38SJames Wright   {
41*12d74c38SJames Wright     PetscInt num_options;
42*12d74c38SJames Wright 
43*12d74c38SJames Wright     PetscCall(PetscOptionsLeftGet(NULL, &num_options, NULL, NULL));
44*12d74c38SJames Wright     PetscCheck(num_options > 0, comm, PETSC_ERR_USER_INPUT,
45*12d74c38SJames Wright                "Command line options required."
46*12d74c38SJames Wright                " Please consult the documentation to see which options are required.");
47*12d74c38SJames Wright     PetscCall(PetscOptionsLeftRestore(NULL, &num_options, NULL, NULL));
48*12d74c38SJames Wright   }
49*12d74c38SJames Wright 
502b730f8bSJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Navier-Stokes in PETSc with libCEED", NULL);
5177841947SLeila Ghaffari 
522b730f8bSJeremy L Thompson   PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, app_ctx->ceed_resource, app_ctx->ceed_resource,
532b730f8bSJeremy L Thompson                                sizeof(app_ctx->ceed_resource), &ceed_flag));
5477841947SLeila Ghaffari 
558fb33541SJames Wright   app_ctx->test_type = TESTTYPE_NONE;
568fb33541SJames Wright   PetscCall(PetscOptionsEnum("-test_type", "Type of test to run", NULL, TestTypes, (PetscEnum)(app_ctx->test_type), (PetscEnum *)&app_ctx->test_type,
578fb33541SJames Wright                              NULL));
5877841947SLeila Ghaffari 
5977841947SLeila Ghaffari   app_ctx->test_tol = 1E-11;
602b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-compare_final_state_atol", "Test absolute tolerance", NULL, app_ctx->test_tol, &app_ctx->test_tol, NULL));
6177841947SLeila Ghaffari 
62b7d66439SJames Wright   PetscCall(PetscOptionsString("-compare_final_state_filename", "Test filename", NULL, app_ctx->test_file_path, app_ctx->test_file_path,
63b7d66439SJames Wright                                sizeof(app_ctx->test_file_path), NULL));
6477841947SLeila Ghaffari 
652b730f8bSJeremy L Thompson   PetscCall(PetscOptionsFList("-problem", "Problem to solve", NULL, app_ctx->problems, app_ctx->problem_name, app_ctx->problem_name,
662b730f8bSJeremy L Thompson                               sizeof(app_ctx->problem_name), &problem_flag));
6777841947SLeila Ghaffari 
6877841947SLeila Ghaffari   app_ctx->viz_refine = 0;
692b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-viz_refine", "Regular refinement levels for visualization", NULL, app_ctx->viz_refine, &app_ctx->viz_refine, NULL));
7077841947SLeila Ghaffari 
7137cbb16aSJed Brown   app_ctx->checkpoint_interval = 10;
7237cbb16aSJed Brown   app_ctx->checkpoint_vtk      = PETSC_FALSE;
7337cbb16aSJed Brown   PetscCall(PetscOptionsDeprecated("-output_freq", "-checkpoint_interval", "libCEED 0.11.1", "Use -checkpoint_vtk true to include VTK output"));
7437cbb16aSJed Brown   PetscCall(PetscOptionsInt("-output_freq", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
7537cbb16aSJed Brown                             &app_ctx->checkpoint_interval, &option_set));
7637cbb16aSJed Brown   if (option_set) app_ctx->checkpoint_vtk = PETSC_TRUE;
7737cbb16aSJed Brown   PetscCall(PetscOptionsInt("-checkpoint_interval", "Frequency of output, in number of steps", NULL, app_ctx->checkpoint_interval,
7837cbb16aSJed Brown                             &app_ctx->checkpoint_interval, NULL));
7937cbb16aSJed Brown   PetscCall(PetscOptionsBool("-checkpoint_vtk", "Include VTK (*.vtu) output at each binary checkpoint", NULL, app_ctx->checkpoint_vtk,
8037cbb16aSJed Brown                              &app_ctx->checkpoint_vtk, NULL));
8177841947SLeila Ghaffari 
822b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-output_add_stepnum2bin", "Add step number to the binary outputs", NULL, app_ctx->add_stepnum2bin,
832b730f8bSJeremy L Thompson                              &app_ctx->add_stepnum2bin, NULL));
8469293791SJames Wright 
8569293791SJames Wright   PetscCall(PetscStrncpy(app_ctx->output_dir, ".", 2));
862b730f8bSJeremy L Thompson   PetscCall(PetscOptionsString("-output_dir", "Output directory", NULL, app_ctx->output_dir, app_ctx->output_dir, sizeof(app_ctx->output_dir), NULL));
8769293791SJames Wright 
8877841947SLeila Ghaffari   app_ctx->cont_steps = 0;
892b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-continue", "Continue from previous solution", NULL, app_ctx->cont_steps, &app_ctx->cont_steps, NULL));
9077841947SLeila Ghaffari 
9169293791SJames Wright   PetscCall(PetscStrcpy(app_ctx->cont_file, "[output_dir]/ns-solution.bin"));
922b730f8bSJeremy L Thompson   PetscCall(PetscOptionsString("-continue_filename", "Filename to get initial condition from", NULL, app_ctx->cont_file, app_ctx->cont_file,
9369293791SJames Wright                                sizeof(app_ctx->cont_file), &option_set));
942b730f8bSJeremy L Thompson   if (!option_set) PetscCall(PetscSNPrintf(app_ctx->cont_file, sizeof app_ctx->cont_file, "%s/ns-solution.bin", app_ctx->output_dir));
954de8550aSJed Brown   if (option_set && app_ctx->cont_steps == 0) app_ctx->cont_steps = -1;  // Read time from file
9669293791SJames Wright 
9769293791SJames Wright   PetscCall(PetscStrcpy(app_ctx->cont_time_file, "[output_dir]/ns-time.bin"));
982b730f8bSJeremy L Thompson   PetscCall(PetscOptionsString("-continue_time_filename", "Filename to get initial condition time from", NULL, app_ctx->cont_time_file,
992b730f8bSJeremy L Thompson                                app_ctx->cont_time_file, sizeof(app_ctx->cont_time_file), &option_set));
1002b730f8bSJeremy L Thompson   if (!option_set) PetscCall(PetscSNPrintf(app_ctx->cont_time_file, sizeof app_ctx->cont_time_file, "%s/ns-time.bin", app_ctx->output_dir));
10169293791SJames Wright 
10277841947SLeila Ghaffari   app_ctx->degree = 1;
1032b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-degree", "Polynomial degree of finite elements", NULL, app_ctx->degree, &app_ctx->degree, NULL));
10477841947SLeila Ghaffari 
105fc14f3f6SLeila Ghaffari   app_ctx->q_extra = 0;
1062b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, app_ctx->q_extra, &app_ctx->q_extra, NULL));
10777841947SLeila Ghaffari 
108544be873SJed Brown   {
109544be873SJed Brown     PetscBool option_set;
110544be873SJed Brown     char      amat_type[256] = "";
1112b730f8bSJeremy L Thompson     PetscCall(PetscOptionsFList("-amat_type", "Set the type of Amat distinct from Pmat (-dm_mat_type)", NULL, MatList, amat_type, amat_type,
1122b730f8bSJeremy L Thompson                                 sizeof(amat_type), &option_set));
1132b730f8bSJeremy L Thompson     if (option_set) PetscCall(PetscStrallocpy(amat_type, (char **)&app_ctx->amat_type));
114544be873SJed Brown   }
115d231d939SJames Wright   {
116d231d939SJames Wright     PetscBool option_set;
117d231d939SJames Wright     PetscCall(PetscOptionsHasName(NULL, NULL, "-pmat_pbdiagonal", &option_set));
118d231d939SJames Wright     if (option_set) PetscCall(PetscPrintf(comm, "Warning! -pmat_pbdiagonal no longer used. Pmat assembly determined from -pc_type setting\n"));
119d231d939SJames Wright   }
120544be873SJed Brown 
12177841947SLeila Ghaffari   // Provide default ceed resource if not specified
12277841947SLeila Ghaffari   if (!ceed_flag) {
12377841947SLeila Ghaffari     const char *ceed_resource = "/cpu/self";
12477841947SLeila Ghaffari     strncpy(app_ctx->ceed_resource, ceed_resource, 10);
12577841947SLeila Ghaffari   }
1260992c3d1SJed Brown   // If we request a GPU, make sure PETSc has initialized its device (which is
1270992c3d1SJed Brown   // MPI-aware in case multiple devices are available) before CeedInit so that
1280992c3d1SJed Brown   // PETSc and libCEED agree about which device to use.
1290992c3d1SJed Brown   if (strncmp(app_ctx->ceed_resource, "/gpu", 4) == 0) PetscCall(PetscDeviceInitialize(PETSC_DEVICE_DEFAULT()));
13077841947SLeila Ghaffari 
13177841947SLeila Ghaffari   // Provide default problem if not specified
13277841947SLeila Ghaffari   if (!problem_flag) {
13377841947SLeila Ghaffari     const char *problem_name = "density_current";
13477841947SLeila Ghaffari     strncpy(app_ctx->problem_name, problem_name, 16);
13577841947SLeila Ghaffari   }
13677841947SLeila Ghaffari 
1372fe7aee7SLeila Ghaffari   // Wall Boundary Conditions
1382fe7aee7SLeila Ghaffari   bc->num_wall = 16;
1392fe7aee7SLeila Ghaffari   PetscBool flg;
1402b730f8bSJeremy L Thompson   PetscCall(PetscOptionsIntArray("-bc_wall", "Face IDs to apply wall BC", NULL, bc->walls, &bc->num_wall, NULL));
1412fe7aee7SLeila Ghaffari   bc->num_comps = 5;
1422b730f8bSJeremy L Thompson   PetscCall(PetscOptionsIntArray("-wall_comps", "An array of constrained component numbers", NULL, bc->wall_comps, &bc->num_comps, &flg));
1437c5bba50SJames Wright 
1447c5bba50SJames Wright   {  // Symmetry Boundary Conditions
1457c5bba50SJames Wright     const char *deprecated[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"};
1467c5bba50SJames Wright     const char *flags[3]      = {"-bc_symmetry_x", "-bc_symmetry_y", "-bc_symmetry_z"};
1477c5bba50SJames Wright     PetscBool   flg, has_symmetry = PETSC_FALSE;
1487c5bba50SJames Wright 
1492fe7aee7SLeila Ghaffari     for (PetscInt j = 0; j < 3; j++) {
1507c5bba50SJames Wright       bc->num_symmetry[j] = 16;
1517c5bba50SJames Wright       PetscCall(PetscOptionsDeprecated(deprecated[j], flags[j], "libCEED 0.12.0",
1527c5bba50SJames Wright                                        "Use -bc_symmetry_[x,y,z] for direct equivalency, or -bc_slip for weak, Riemann-based, direction-invariant "
1537c5bba50SJames Wright                                        "slip/no-penatration boundary conditions"));
1547c5bba50SJames Wright       PetscCall(PetscOptionsIntArray(flags[j], "Face IDs to apply symmetry BC", NULL, bc->symmetries[j], &bc->num_symmetry[j], &flg));
1557c5bba50SJames Wright       if (!flg) {
1567c5bba50SJames Wright         bc->num_symmetry[j] = 16;
1577c5bba50SJames Wright         PetscCall(PetscOptionsIntArray(deprecated[j], "Face IDs to apply slip BC", NULL, bc->symmetries[j], &bc->num_symmetry[j], &flg));
1587c5bba50SJames Wright       }
1597c5bba50SJames Wright       if (bc->num_symmetry[j] > 0) has_symmetry = PETSC_TRUE;
1602fe7aee7SLeila Ghaffari     }
1612fe7aee7SLeila Ghaffari 
1627c5bba50SJames Wright     // Error if wall and symmetry BCs are set on the same face
1637c5bba50SJames Wright     if (has_symmetry) {
16449aac155SJeremy L Thompson       for (PetscInt c = 0; c < 3; c++) {
1657c5bba50SJames Wright         for (PetscInt s = 0; s < bc->num_symmetry[c]; s++) {
16649aac155SJeremy L Thompson           for (PetscInt w = 0; w < bc->num_wall; w++) {
1677c5bba50SJames Wright             PetscCheck(bc->symmetries[c][s] != bc->walls[w], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
1680e654f56SJames Wright                        "Boundary condition already set on face %" PetscInt_FMT "!\n", bc->walls[w]);
16949aac155SJeremy L Thompson           }
17049aac155SJeremy L Thompson         }
17149aac155SJeremy L Thompson       }
17249aac155SJeremy L Thompson     }
1737c5bba50SJames Wright   }
174f5452247SJames Wright   app_ctx->wall_forces.num_wall = bc->num_wall;
175f5452247SJames Wright   PetscMalloc1(bc->num_wall, &app_ctx->wall_forces.walls);
176f5452247SJames Wright   PetscCall(PetscArraycpy(app_ctx->wall_forces.walls, bc->walls, bc->num_wall));
1772fe7aee7SLeila Ghaffari 
1782fe7aee7SLeila Ghaffari   // Inflow BCs
1792fe7aee7SLeila Ghaffari   bc->num_inflow = 16;
1802b730f8bSJeremy L Thompson   PetscCall(PetscOptionsIntArray("-bc_inflow", "Face IDs to apply inflow BC", NULL, bc->inflows, &bc->num_inflow, NULL));
1812fe7aee7SLeila Ghaffari   // Outflow BCs
1822fe7aee7SLeila Ghaffari   bc->num_outflow = 16;
1832b730f8bSJeremy L Thompson   PetscCall(PetscOptionsIntArray("-bc_outflow", "Face IDs to apply outflow BC", NULL, bc->outflows, &bc->num_outflow, NULL));
184f4ca79c2SJames Wright   // Freestream BCs
185f4ca79c2SJames Wright   bc->num_freestream = 16;
1862b730f8bSJeremy L Thompson   PetscCall(PetscOptionsIntArray("-bc_freestream", "Face IDs to apply freestream BC", NULL, bc->freestreams, &bc->num_freestream, NULL));
1872fe7aee7SLeila Ghaffari 
1889f844368SJames Wright   bc->num_slip = 16;
1899f844368SJames Wright   PetscCall(PetscOptionsIntArray("-bc_slip", "Face IDs to apply slip BC", NULL, bc->slips, &bc->num_slip, NULL));
1909f844368SJames Wright 
191a175e481SJames Wright   // Statistics Options
192b7d66439SJames Wright   app_ctx->turb_spanstats_collect_interval = 1;
193b7d66439SJames Wright   PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_collect_interval", "Number of timesteps between statistics collection", NULL,
194b7d66439SJames Wright                             app_ctx->turb_spanstats_collect_interval, &app_ctx->turb_spanstats_collect_interval, NULL));
195a175e481SJames Wright 
196b7d66439SJames Wright   app_ctx->turb_spanstats_viewer_interval = -1;
197b7d66439SJames Wright   PetscCall(PetscOptionsInt("-ts_monitor_turbulence_spanstats_viewer_interval", "Number of timesteps between statistics viewer writing", NULL,
198b7d66439SJames Wright                             app_ctx->turb_spanstats_viewer_interval, &app_ctx->turb_spanstats_viewer_interval, NULL));
199a175e481SJames Wright 
200b7d66439SJames Wright   PetscCall(PetscOptionsViewer("-ts_monitor_turbulence_spanstats_viewer", "Viewer for the statistics", NULL, &app_ctx->turb_spanstats_viewer,
201b7d66439SJames Wright                                &app_ctx->turb_spanstats_viewer_format, &app_ctx->turb_spanstats_enable));
202a175e481SJames Wright 
203ca69d878SAdeleke O. Bankole   PetscCall(PetscOptionsViewer("-ts_monitor_wall_force", "Viewer for force on each (no-slip) wall", NULL, &app_ctx->wall_forces.viewer,
204ca69d878SAdeleke O. Bankole                                &app_ctx->wall_forces.viewer_format, NULL));
205ca69d878SAdeleke O. Bankole 
20619ffbc25SJames Wright   // SGS Model Options
20719ffbc25SJames Wright   app_ctx->sgs_model_type = SGS_MODEL_NONE;
20819ffbc25SJames Wright   PetscCall(PetscOptionsEnum("-sgs_model_type", "Subgrid Stress Model type", NULL, SGSModelTypes, (PetscEnum)app_ctx->sgs_model_type,
20919ffbc25SJames Wright                              (PetscEnum *)&app_ctx->sgs_model_type, NULL));
21019ffbc25SJames Wright 
2114e9802d1SJames Wright   PetscCall(PetscOptionsBool("-diff_filter_monitor", "Enable differential filtering TSMonitor", NULL, app_ctx->diff_filter_monitor,
2124e9802d1SJames Wright                              &app_ctx->diff_filter_monitor, NULL));
2134e9802d1SJames Wright 
2142526956eSJames Wright   // Mesh Transformation Options
2152526956eSJames Wright   app_ctx->mesh_transform_type = MESH_TRANSFORM_NONE;
2162526956eSJames Wright   PetscCall(PetscOptionsEnum("-mesh_transform", "Mesh transform to perform", NULL, MeshTransformTypes, (PetscEnum)app_ctx->mesh_transform_type,
2172526956eSJames Wright                              (PetscEnum *)&app_ctx->mesh_transform_type, NULL));
2182526956eSJames Wright 
21928134cfaSJames Wright   PetscCall(
22028134cfaSJames Wright       PetscOptionsBool("-sgs_train_enable", "Enable Data-Driven SGS training", NULL, app_ctx->sgs_train_enable, &app_ctx->sgs_train_enable, NULL));
22128134cfaSJames Wright 
22267490bc6SJeremy L Thompson   PetscOptionsEnd();
223ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
22477841947SLeila Ghaffari }
225