PRISMS-PF Manual
Loading...
Searching...
No Matches
parse_cmd_options.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: © 2025 PRISMS Center at the University of Michigan
2// SPDX-License-Identifier: GNU Lesser General Public Version 2.1
3
4#pragma once
5
6#include <deal.II/base/mpi.h>
7
9
10#include <prismspf/config.h>
11
12#include <fstream>
13#include <string>
14#include <vector>
15
17
22{
23public:
24 // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
25
27 : argc(_argc)
28 {
29 for (int i = 1; i < argc; ++i)
30 {
31 tokens.emplace_back(argv[i]);
32 }
33 }
34
35 // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
36
37 std::string
39 {
40 // Allowed number of arguments
41#ifdef PRISMS_PF_WITH_CALIPER
42 const int n_args = 5;
43#else
44 const int n_args = 3;
45#endif
46
47 // Check that there aren't too many arguments
48 if (argc > n_args)
49 {
50 throw std::runtime_error(
51 "Too many command line arguments. The arguments should specify "
52 "the input file name and caliper configurations, if applicable. For example, "
53 "`./main -i parameters.prm`");
54 }
55
56 // Default filename
57 std::string parameters_filename = "parameters.prm";
58
59 if (argc >= n_args - 1 && !cmd_option_exists("-i"))
60 {
61 throw std::runtime_error(
62 "Invalid command line option. Use `-i` to specify the input file, "
63 "e.g., `./main -i parameters.prm`.");
64 }
65
66 // If -i is provided, check for an argument
67 if (cmd_option_exists("-i"))
68 {
70 }
71
72 // Validate the parameters file (i.e., it ends in .prm)
73 if (parameters_filename.size() < 4 ||
74 parameters_filename.substr(parameters_filename.size() - 4) != ".prm")
75 {
76 throw std::runtime_error("The input file must have the `.prm` extension. Please "
77 "rename the file accordingly.");
78 }
79
80 // Check if the file exists
81 const std::ifstream ifs_prm(parameters_filename);
82 if (!ifs_prm)
83 {
84 throw std::runtime_error("The specified parameters file `" + parameters_filename +
85 "` does not exist.");
86 }
87
88 // Log the filename being used
90 << "Using the input parameter file: " << parameters_filename << "\n";
91
93 }
94
95#ifdef PRISMS_PF_WITH_CALIPER
96 std::string
98 {
99 // Allowed number of arguments
100 const int n_args = 5;
101
102 // Check that there aren't too many arguments
103 if (argc > n_args)
104 {
105 throw std::runtime_error(
106 "Too many command line arguments. The arguments should specify "
107 "the input file name and caliper configurations, if applicable. For example, "
108 "`./main -i parameters.prm -P runtime-report`");
109 }
110
111 // Default config
112 std::string config = "runtime-report,mem.highwatermark";
113
114 if (argc >= n_args - 1 && !cmd_option_exists("-P"))
115 {
116 throw std::runtime_error(
117 "Invalid command line option. Use `-P` to specify caliper configurations, "
118 "e.g., `./main -P runtime-report`.");
119 }
120
121 // If -P is provided, check for an argument
122 if (cmd_option_exists("-P"))
123 {
124 config = get_cmd_option("-P");
125 }
126
127 // Log the filename being used
129 << "Using the caliper configuration: " << config << "\n";
130
131 return config;
132 }
133#endif
134
135private:
136 int argc;
137 std::vector<std::string> tokens;
138
139 [[nodiscard]] const std::string &
140 get_cmd_option(const std::string &option) const
141 {
142 auto itr = std::ranges::find(tokens, option);
143 if (itr != tokens.end())
144 {
145 ++itr; // Increment the iterator to get the value
146 if (itr != tokens.end())
147 {
148 return *itr;
149 }
150 }
151 static const std::string empty_string;
152 return empty_string;
153 }
154
155 [[nodiscard]] bool
156 cmd_option_exists(const std::string &option) const
157 {
158 return std::ranges::find(tokens, option) != tokens.end();
159 }
160};
161
static dealii::ConditionalOStream & pout_base()
Generic parallel output stream. Used for essential information in release and debug mode.
Definition conditional_ostreams.cc:43
Class to parse command line options.
Definition parse_cmd_options.h:22
std::vector< std::string > tokens
Definition parse_cmd_options.h:137
ParseCMDOptions(int &_argc, char **argv)
Definition parse_cmd_options.h:26
const std::string & get_cmd_option(const std::string &option) const
Definition parse_cmd_options.h:140
int argc
Definition parse_cmd_options.h:136
bool cmd_option_exists(const std::string &option) const
Definition parse_cmd_options.h:156
std::string get_parameters_filename()
Definition parse_cmd_options.h:38
@ Value
Use value of the variable as a criterion for refinement.
Definition grid_refiner_criterion.h:31
Definition conditional_ostreams.cc:20