deltaFlow
Argparse.C
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Saud Zahir
3 *
4 * This file is part of deltaFlow.
5 *
6 * deltaFlow is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * deltaFlow is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public
17 * License along with deltaFlow. If not, see
18 * <https://www.gnu.org/licenses/>.
19 */
20
26#include <cstdlib>
27#include <iostream>
28#include <stdexcept>
29
30#include "Argparse.H"
31#include "Display.H"
32#include "Logger.H"
33#include "PowerFlowOptions.H"
34#include "Utils.H"
35#include "Version.H"
36
37ArgumentParser::ArgumentParser(int argc, char* argv[]) {
38 parse_args(argc, argv);
39}
40
41void ArgumentParser::parse_args(int argc, char* argv[]) {
42 bool methodFound = false;
43 bool inputFileFound = false;
44
45 for (int i = 1; i < argc; ++i) {
46 std::string arg = argv[i];
47
48 if ((arg == "--job" || arg == "-j") && i + 1 < argc) {
49 this->jobName = argv[++i];
50 }
51 else if ((arg == "--tolerance" || arg == "-t") && i + 1 < argc) {
52 this->tolerance = std::stod(argv[++i]);
53 }
54 else if ((arg == "--max-iterations" || arg == "-m") && i + 1 < argc) {
55 this->maxIterations = std::stoi(argv[++i]);
56 }
57 else if ((arg == "--relaxation" || arg == "-r") && i + 1 < argc) {
58 this->relaxation = std::stod(argv[++i]);
59 }
60 else if (arg == "--version" || arg == "-v") {
61 std::exit(0);
62 }
63 else if (arg == "--help" || arg == "-h") {
64 help();
65 std::exit(0);
66 }
67 else if (!inputFileFound) {
68 this->inputFile = arg;
69
72 }
73 else if (Utilities::isRawFormat(arg))
74 {
76 }
77 else {
78 LOG_MESSAGE("ERROR: Invalid format '{}'", arg);
79 help();
80 std::exit(1);
81 }
82
83 inputFileFound = true;
84 }
85 else if (!methodFound) {
86 if (arg == "GAUSS") {
88 methodFound = true;
89 }
90 else if (arg == "NEWTON") {
92 methodFound = true;
93 }
94 else {
95 LOG_MESSAGE("ERROR: Invalid method '{}'", arg);
96 help();
97 std::exit(1);
98 }
99 }
100 else {
101 LOG_MESSAGE("ERROR: Unexpected argument '{}'", arg);
102 help();
103 std::exit(1);
104 }
105 }
106
107 if (!inputFileFound) {
108 LOG_MESSAGE("ERROR: Input CDF file (.txt or .cdf) is required.");
109 help();
110 std::exit(1);
111 }
112
113 if (!methodFound) {
114 LOG_MESSAGE("ERROR: Missing required solver argument (GAUSS or NEWTON).");
115 help();
116 std::exit(1);
117 }
118
119 if (jobName.empty()) {
120 jobName = std::filesystem::path(inputFile).stem().string();
121 }
122
124 LOG_MESSAGE("Warning: Relaxation coefficient ignored for method 'NEWTON'");
125 }
126
127 LOG_DEBUG("deltaFlow v{}", deltaFlow_VERSION);
128 LOG_DEBUG("CMake v{}, GCC v{}", CMake_VERSION, gcc_VERSION);
129}
130
131std::string ArgumentParser::getInputFile() const noexcept {
132 return this->inputFile;
133}
134
135std::string ArgumentParser::getJobName() const noexcept {
136 return this->jobName;
137}
138
139double ArgumentParser::getTolerance() const noexcept {
140 return this->tolerance;
141}
142
144 return this->maxIterations;
145}
146
148 return this->relaxation;
149}
150
152 return this->method;
153}
154
156 return this->format;
157}
158
160 PowerFlowOptions opts;
161 opts.inputFile = this->inputFile;
162 opts.jobName = this->jobName;
163 opts.tolerance = this->tolerance;
164 opts.maxIterations = this->maxIterations;
165 opts.relaxation = this->relaxation;
166 opts.solverType = this->method;
167 opts.inputFormat = this->format;
168 return opts;
169}
170
171void ArgumentParser::help() const noexcept {
172 LOG_MESSAGE(R"(
173Usage:
174 deltaFlow [OPTIONS] <input-file> <solver>
175
176Required:
177 <input-file> Path to input file (.cdf, .txt or .raw)
178 <solver> Solver method: GAUSS | NEWTON
179
180Options:
181 -j, --job <name> Job name
182 -t, --tolerance <value> Convergence tolerance (default: 1E-8)
183 -m, --max-iterations <int> Maximum number of iterations (default: 1024)
184 -h, --help Display help message
185 -v, --version Show program version and exit
186
187Solvers:
188 GAUSS Gauss-Seidel Method
189 -r, --relaxation <value> Relaxation coefficient (default: 1.0)
190
191 NEWTON Newton-Raphson Method
192)");
193}
Command-line argument parsing utilities for deltaFlow.
InputFormat
Supported input file formats.
Definition Argparse.H:52
@ IEEE
IEEE Common Data Format (.cdf, .txt)
@ PSSE
PSS/E Raw Data Format (.raw)
SolverType
Types of solvers supported by deltaFlow.
Definition Argparse.H:43
@ NewtonRaphson
Newton-Raphson iterative method.
@ GaussSeidel
Gauss-Seidel iterative method.
Display and formatting utilities for terminal and file output.
Logger utilities for deltaFlow, providing logging macros and a singleton Logger class.
#define LOG_DEBUG(msg,...)
Macro for logging a debug-level message.
Definition Logger.H:85
#define LOG_MESSAGE(msg,...)
Macro for printing messages to stdout.
Definition Logger.H:90
Plain parsed-options data, separated from ArgumentParser's parsing logic (ISP).
Utility functions and helpers for deltaFlow.
InputFormat getInputFormat() const noexcept
Get the input file format.
Definition Argparse.C:155
double relaxation
Relaxation coefficient ($$ \omega $$)
Definition Argparse.H:140
std::string inputFile
Path to input CDF file.
Definition Argparse.H:136
std::string jobName
Job name (defaults to input filename)
Definition Argparse.H:137
std::string getJobName() const noexcept
Get the job name.
Definition Argparse.C:135
PowerFlowOptions getOptions() const noexcept
Get all parsed options as a plain data struct (ISP).
Definition Argparse.C:159
double getTolerance() const noexcept
Get the convergence tolerance ($$ \epsilon $$).
Definition Argparse.C:139
void parse_args(int argc, char *argv[])
Parse the provided arguments.
Definition Argparse.C:41
void help() const noexcept
Print help message to stdout.
Definition Argparse.C:171
ArgumentParser(int argc, char *argv[])
Constructor: parses command-line arguments.
Definition Argparse.C:37
double tolerance
Convergence tolerance ($$ \epsilon $$)
Definition Argparse.H:138
SolverType getSolverType() const noexcept
Get the solver type.
Definition Argparse.C:151
std::string getInputFile() const noexcept
Get the input CDF file path.
Definition Argparse.C:131
double getRelaxationCoefficient() const noexcept
Get the relaxation coefficient ($$ \omega $$).
Definition Argparse.C:147
SolverType method
Solver type.
Definition Argparse.H:141
int maxIterations
Maximum number of iterations ($$ N_{max} $$)
Definition Argparse.H:139
int getMaxIterations() const noexcept
Get the maximum number of iterations ($$ N_{max} $$).
Definition Argparse.C:143
InputFormat format
Input file format.
Definition Argparse.H:142
bool isCommonDataFormat(const std::string &filePath)
Check if filepath is IEEE Common Data Format.
Definition Utils.H:43
bool isRawFormat(const std::string &filePath)
Check if filepath is PSS/E Raw format.
Definition Utils.H:53
Plain data holder for parsed command-line options.
std::string inputFile
Path to input file.
std::string jobName
Job name (defaults to input filename)
int maxIterations
Maximum number of iterations ($$ N_{max} $$)
double tolerance
Convergence tolerance ($$ \epsilon $$)
SolverType solverType
Solver type.
InputFormat inputFormat
Input file format.
double relaxation
Relaxation coefficient ($$ \omega $$)