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