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