xref: /honee/src/cloptions.c (revision a467869f826f0b54b19610fc24a2b0811764b28a)
1727da7e7SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2727da7e7SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3a515125bSLeila Ghaffari //
4727da7e7SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5a515125bSLeila Ghaffari //
6727da7e7SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7a515125bSLeila Ghaffari 
8a515125bSLeila Ghaffari /// @file
9a515125bSLeila Ghaffari /// Command line option processing for Navier-Stokes example using PETSc
10a515125bSLeila Ghaffari 
11*a467869fSJed Brown #include <petscdevice.h>
12*a467869fSJed Brown 
13a515125bSLeila Ghaffari #include "../navierstokes.h"
14a515125bSLeila Ghaffari 
15a515125bSLeila Ghaffari // Register problems to be available on the command line
16a515125bSLeila Ghaffari PetscErrorCode RegisterProblems_NS(AppCtx app_ctx) {
17a515125bSLeila Ghaffari   app_ctx->problems = NULL;
18a515125bSLeila Ghaffari   PetscFunctionBeginUser;
19a515125bSLeila Ghaffari 
202b916ea7SJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "density_current", NS_DENSITY_CURRENT));
212b916ea7SJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "euler_vortex", NS_EULER_VORTEX));
222b916ea7SJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "shocktube", NS_SHOCKTUBE));
232b916ea7SJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "advection", NS_ADVECTION));
242b916ea7SJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "advection2d", NS_ADVECTION2D));
252b916ea7SJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "blasius", NS_BLASIUS));
262b916ea7SJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "channel", NS_CHANNEL));
272b916ea7SJeremy L Thompson   PetscCall(PetscFunctionListAdd(&app_ctx->problems, "newtonian_wave", NS_NEWTONIAN_WAVE));
28bb8a0c61SJames Wright 
29a515125bSLeila Ghaffari   PetscFunctionReturn(0);
30a515125bSLeila Ghaffari }
31a515125bSLeila Ghaffari 
32a515125bSLeila Ghaffari // Process general command line options
332b916ea7SJeremy L Thompson PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx, SimpleBC bc) {
34a515125bSLeila Ghaffari   PetscBool ceed_flag    = PETSC_FALSE;
35a515125bSLeila Ghaffari   PetscBool problem_flag = PETSC_FALSE;
3691a36801SJames Wright   PetscBool option_set   = PETSC_FALSE;
372b916ea7SJeremy L Thompson 
38a515125bSLeila Ghaffari   PetscFunctionBeginUser;
39a515125bSLeila Ghaffari 
402b916ea7SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Navier-Stokes in PETSc with libCEED", NULL);
41a515125bSLeila Ghaffari 
422b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, app_ctx->ceed_resource, app_ctx->ceed_resource,
432b916ea7SJeremy L Thompson                                sizeof(app_ctx->ceed_resource), &ceed_flag));
44a515125bSLeila Ghaffari 
45a515125bSLeila Ghaffari   app_ctx->test_mode = PETSC_FALSE;
462b916ea7SJeremy L Thompson   PetscCall(PetscOptionsBool("-test", "Run in test mode", NULL, app_ctx->test_mode, &app_ctx->test_mode, NULL));
47a515125bSLeila Ghaffari 
48a515125bSLeila Ghaffari   app_ctx->test_tol = 1E-11;
492b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-compare_final_state_atol", "Test absolute tolerance", NULL, app_ctx->test_tol, &app_ctx->test_tol, NULL));
50a515125bSLeila Ghaffari 
512b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-compare_final_state_filename", "Test filename", NULL, app_ctx->file_path, app_ctx->file_path,
522b916ea7SJeremy L Thompson                                sizeof(app_ctx->file_path), NULL));
53a515125bSLeila Ghaffari 
542b916ea7SJeremy L Thompson   PetscCall(PetscOptionsFList("-problem", "Problem to solve", NULL, app_ctx->problems, app_ctx->problem_name, app_ctx->problem_name,
552b916ea7SJeremy L Thompson                               sizeof(app_ctx->problem_name), &problem_flag));
56a515125bSLeila Ghaffari 
57a515125bSLeila Ghaffari   app_ctx->viz_refine = 0;
582b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-viz_refine", "Regular refinement levels for visualization", NULL, app_ctx->viz_refine, &app_ctx->viz_refine, NULL));
59a515125bSLeila Ghaffari 
60a515125bSLeila Ghaffari   app_ctx->output_freq = 10;
612b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-output_freq", "Frequency of output, in number of steps", NULL, app_ctx->output_freq, &app_ctx->output_freq, NULL));
62a515125bSLeila Ghaffari 
632b916ea7SJeremy L Thompson   PetscCall(PetscOptionsBool("-output_add_stepnum2bin", "Add step number to the binary outputs", NULL, app_ctx->add_stepnum2bin,
642b916ea7SJeremy L Thompson                              &app_ctx->add_stepnum2bin, NULL));
6591a36801SJames Wright 
6691a36801SJames Wright   PetscCall(PetscStrncpy(app_ctx->output_dir, ".", 2));
672b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-output_dir", "Output directory", NULL, app_ctx->output_dir, app_ctx->output_dir, sizeof(app_ctx->output_dir), NULL));
6891a36801SJames Wright 
69a515125bSLeila Ghaffari   app_ctx->cont_steps = 0;
702b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-continue", "Continue from previous solution", NULL, app_ctx->cont_steps, &app_ctx->cont_steps, NULL));
71a515125bSLeila Ghaffari 
7291a36801SJames Wright   PetscCall(PetscStrcpy(app_ctx->cont_file, "[output_dir]/ns-solution.bin"));
732b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-continue_filename", "Filename to get initial condition from", NULL, app_ctx->cont_file, app_ctx->cont_file,
7491a36801SJames Wright                                sizeof(app_ctx->cont_file), &option_set));
752b916ea7SJeremy L Thompson   if (!option_set) PetscCall(PetscSNPrintf(app_ctx->cont_file, sizeof app_ctx->cont_file, "%s/ns-solution.bin", app_ctx->output_dir));
7691a36801SJames Wright 
7791a36801SJames Wright   PetscCall(PetscStrcpy(app_ctx->cont_time_file, "[output_dir]/ns-time.bin"));
782b916ea7SJeremy L Thompson   PetscCall(PetscOptionsString("-continue_time_filename", "Filename to get initial condition time from", NULL, app_ctx->cont_time_file,
792b916ea7SJeremy L Thompson                                app_ctx->cont_time_file, sizeof(app_ctx->cont_time_file), &option_set));
802b916ea7SJeremy 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));
8191a36801SJames Wright 
82a515125bSLeila Ghaffari   app_ctx->degree = 1;
832b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-degree", "Polynomial degree of finite elements", NULL, app_ctx->degree, &app_ctx->degree, NULL));
84a515125bSLeila Ghaffari 
85a515125bSLeila Ghaffari   app_ctx->q_extra = 2;
862b916ea7SJeremy L Thompson   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, app_ctx->q_extra, &app_ctx->q_extra, NULL));
87a515125bSLeila Ghaffari 
88b107fddaSJed Brown   {
89b107fddaSJed Brown     PetscBool option_set;
90b107fddaSJed Brown     char      amat_type[256] = "";
912b916ea7SJeremy L Thompson     PetscCall(PetscOptionsFList("-amat_type", "Set the type of Amat distinct from Pmat (-dm_mat_type)", NULL, MatList, amat_type, amat_type,
922b916ea7SJeremy L Thompson                                 sizeof(amat_type), &option_set));
932b916ea7SJeremy L Thompson     if (option_set) PetscCall(PetscStrallocpy(amat_type, (char **)&app_ctx->amat_type));
94b107fddaSJed Brown   }
952b916ea7SJeremy L Thompson   PetscCall(PetscOptionsBool("-pmat_pbdiagonal", "Assemble only point-block diagonal for Pmat", NULL, app_ctx->pmat_pbdiagonal,
96b107fddaSJed Brown                              &app_ctx->pmat_pbdiagonal, NULL));
97b107fddaSJed Brown 
98a515125bSLeila Ghaffari   // Provide default ceed resource if not specified
99a515125bSLeila Ghaffari   if (!ceed_flag) {
100a515125bSLeila Ghaffari     const char *ceed_resource = "/cpu/self";
101a515125bSLeila Ghaffari     strncpy(app_ctx->ceed_resource, ceed_resource, 10);
102a515125bSLeila Ghaffari   }
103*a467869fSJed Brown   // If we request a GPU, make sure PETSc has initialized its device (which is
104*a467869fSJed Brown   // MPI-aware in case multiple devices are available) before CeedInit so that
105*a467869fSJed Brown   // PETSc and libCEED agree about which device to use.
106*a467869fSJed Brown   if (strncmp(app_ctx->ceed_resource, "/gpu", 4) == 0) PetscCall(PetscDeviceInitialize(PETSC_DEVICE_DEFAULT()));
107a515125bSLeila Ghaffari 
108a515125bSLeila Ghaffari   // Provide default problem if not specified
109a515125bSLeila Ghaffari   if (!problem_flag) {
110a515125bSLeila Ghaffari     const char *problem_name = "density_current";
111a515125bSLeila Ghaffari     strncpy(app_ctx->problem_name, problem_name, 16);
112a515125bSLeila Ghaffari   }
113a515125bSLeila Ghaffari 
114002797a3SLeila Ghaffari   // Wall Boundary Conditions
115002797a3SLeila Ghaffari   bc->num_wall = 16;
116002797a3SLeila Ghaffari   PetscBool flg;
1172b916ea7SJeremy L Thompson   PetscCall(PetscOptionsIntArray("-bc_wall", "Face IDs to apply wall BC", NULL, bc->walls, &bc->num_wall, NULL));
118002797a3SLeila Ghaffari   bc->num_comps = 5;
1192b916ea7SJeremy L Thompson   PetscCall(PetscOptionsIntArray("-wall_comps", "An array of constrained component numbers", NULL, bc->wall_comps, &bc->num_comps, &flg));
120002797a3SLeila Ghaffari   // Slip Boundary Conditions
121002797a3SLeila Ghaffari   for (PetscInt j = 0; j < 3; j++) {
122002797a3SLeila Ghaffari     bc->num_slip[j] = 16;
123002797a3SLeila Ghaffari     PetscBool   flg;
124002797a3SLeila Ghaffari     const char *flags[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"};
1252b916ea7SJeremy L Thompson     PetscCall(PetscOptionsIntArray(flags[j], "Face IDs to apply slip BC", NULL, bc->slips[j], &bc->num_slip[j], &flg));
126002797a3SLeila Ghaffari     if (flg) bc->user_bc = PETSC_TRUE;
127002797a3SLeila Ghaffari   }
128002797a3SLeila Ghaffari 
129002797a3SLeila Ghaffari   // Error if wall and slip BCs are set on the same face
130002797a3SLeila Ghaffari   if (bc->user_bc)
131002797a3SLeila Ghaffari     for (PetscInt c = 0; c < 3; c++)
132002797a3SLeila Ghaffari       for (PetscInt s = 0; s < bc->num_slip[c]; s++)
133002797a3SLeila Ghaffari         for (PetscInt w = 0; w < bc->num_wall; w++)
134002797a3SLeila Ghaffari           if (bc->slips[c][s] == bc->walls[w])
1352b916ea7SJeremy L Thompson             SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Boundary condition already set on face %" PetscInt_FMT "!\n", bc->walls[w]);
136002797a3SLeila Ghaffari 
137002797a3SLeila Ghaffari   // Inflow BCs
138002797a3SLeila Ghaffari   bc->num_inflow = 16;
1392b916ea7SJeremy L Thompson   PetscCall(PetscOptionsIntArray("-bc_inflow", "Face IDs to apply inflow BC", NULL, bc->inflows, &bc->num_inflow, NULL));
140002797a3SLeila Ghaffari   // Outflow BCs
141002797a3SLeila Ghaffari   bc->num_outflow = 16;
1422b916ea7SJeremy L Thompson   PetscCall(PetscOptionsIntArray("-bc_outflow", "Face IDs to apply outflow BC", NULL, bc->outflows, &bc->num_outflow, NULL));
143df55ba5fSJames Wright   // Freestream BCs
144df55ba5fSJames Wright   bc->num_freestream = 16;
1452b916ea7SJeremy L Thompson   PetscCall(PetscOptionsIntArray("-bc_freestream", "Face IDs to apply freestream BC", NULL, bc->freestreams, &bc->num_freestream, NULL));
146002797a3SLeila Ghaffari 
1471485969bSJeremy L Thompson   PetscOptionsEnd();
148a515125bSLeila Ghaffari 
149a515125bSLeila Ghaffari   PetscFunctionReturn(0);
150a515125bSLeila Ghaffari }
151