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 "Utils.H"
34#include "Version.H"
35
36ArgumentParser::ArgumentParser(int argc, char* argv[]) {
37 parse_args(argc, argv);
38}
39
40void ArgumentParser::parse_args(int argc, char* argv[]) {
41 bool methodFound = false;
42 bool inputFileFound = false;
43
44 for (int i = 1; i < argc; ++i) {
45 std::string arg = argv[i];
46
47 if ((arg == "--job" || arg == "-j") && i + 1 < argc) {
48 this->jobName = argv[++i];
49 }
50 else if ((arg == "--tolerance" || arg == "-t") && i + 1 < argc) {
51 this->tolerance = std::stod(argv[++i]);
52 }
53 else if ((arg == "--max-iterations" || arg == "-m") && i + 1 < argc) {
54 this->maxIterations = std::stoi(argv[++i]);
55 }
56 else if ((arg == "--relaxation" || arg == "-r") && i + 1 < argc) {
57 this->relaxation = std::stod(argv[++i]);
58 }
59 else if (arg == "--version" || arg == "-v") {
60 std::exit(0);
61 }
62 else if (arg == "--help" || arg == "-h") {
63 help();
64 std::exit(0);
65 }
66 else if (!inputFileFound) {
67 this->inputFile = arg;
68
71 }
72 else if (Utilities::isRawFormat(arg))
73 {
75 }
76 else {
77 LOG_MESSAGE("ERROR: Invalid format '{}'", arg);
78 help();
79 std::exit(1);
80 }
81
82 inputFileFound = true;
83 }
84 else if (!methodFound) {
85 if (arg == "GAUSS") {
87 methodFound = true;
88 }
89 else if (arg == "NEWTON") {
91 methodFound = true;
92 }
93 else {
94 LOG_MESSAGE("ERROR: Invalid method '{}'", arg);
95 help();
96 std::exit(1);
97 }
98 }
99 else {
100 LOG_MESSAGE("ERROR: Unexpected argument '{}'", arg);
101 help();
102 std::exit(1);
103 }
104 }
105
106 if (!inputFileFound) {
107 LOG_MESSAGE("ERROR: Input CDF file (.txt or .cdf) is required.");
108 help();
109 std::exit(1);
110 }
111
112 if (!methodFound) {
113 LOG_MESSAGE("ERROR: Missing required solver argument (GAUSS or NEWTON).");
114 help();
115 std::exit(1);
116 }
117
118 if (jobName.empty()) {
119 jobName = std::filesystem::path(inputFile).stem().string();
120 }
121
123 LOG_MESSAGE("Warning: Relaxation coefficient ignored for method 'NEWTON'");
124 }
125
126 LOG_DEBUG("deltaFlow v{}", deltaFlow_VERSION);
127 LOG_DEBUG("CMake v{}, GCC v{}", CMake_VERSION, gcc_VERSION);
128}
129
130std::string ArgumentParser::getInputFile() const noexcept {
131 return this->inputFile;
132}
133
134std::string ArgumentParser::getJobName() const noexcept {
135 return this->jobName;
136}
137
138double ArgumentParser::getTolerance() const noexcept {
139 return this->tolerance;
140}
141
143 return this->maxIterations;
144}
145
147 return this->relaxation;
148}
149
151 return this->method;
152}
153
155 return this->format;
156}
157
158void ArgumentParser::help() const noexcept {
159 LOG_MESSAGE(R"(
160Usage:
161 deltaFlow [OPTIONS] <input-file> <solver>
162
163Required:
164 <input-file> Path to input file (.cdf, .txt or .raw)
165 <solver> Solver method: GAUSS | NEWTON
166
167Options:
168 -j, --job <name> Job name
169 -t, --tolerance <value> Convergence tolerance (default: 1E-8)
170 -m, --max-iterations <int> Maximum number of iterations (default: 1024)
171 -h, --help Display help message
172 -v, --version Show program version and exit
173
174Solvers:
175 GAUSS Gauss-Seidel Method
176 -r, --relaxation <value> Relaxation coefficient (default: 1.0)
177
178 NEWTON Newton-Raphson Method
179)");
180}
Command-line argument parsing utilities for deltaFlow.
InputFormat
Supported input file formats.
Definition Argparse.H:50
@ IEEE
IEEE Common Data Format (.cdf, .txt)
@ PSSE
PSS/E Raw Data Format (.raw)
SolverType
Types of solvers supported by deltaFlow.
Definition Argparse.H:41
@ 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
Utility functions and helpers for deltaFlow.
InputFormat getInputFormat() const noexcept
Get the input file format.
Definition Argparse.C:154
double relaxation
Relaxation coefficient ($$ \omega $$)
Definition Argparse.H:129
std::string inputFile
Path to input CDF file.
Definition Argparse.H:125
std::string jobName
Job name (defaults to input filename)
Definition Argparse.H:126
std::string getJobName() const noexcept
Get the job name.
Definition Argparse.C:134
double getTolerance() const noexcept
Get the convergence tolerance ($$ \epsilon $$).
Definition Argparse.C:138
void parse_args(int argc, char *argv[])
Parse the provided arguments.
Definition Argparse.C:40
void help() const noexcept
Print help message to stdout.
Definition Argparse.C:158
ArgumentParser(int argc, char *argv[])
Constructor: parses command-line arguments.
Definition Argparse.C:36
double tolerance
Convergence tolerance ($$ \epsilon $$)
Definition Argparse.H:127
SolverType getSolverType() const noexcept
Get the solver type.
Definition Argparse.C:150
std::string getInputFile() const noexcept
Get the input CDF file path.
Definition Argparse.C:130
double getRelaxationCoefficient() const noexcept
Get the relaxation coefficient ($$ \omega $$).
Definition Argparse.C:146
SolverType method
Solver type.
Definition Argparse.H:130
int maxIterations
Maximum number of iterations ($$ N_{max} $$)
Definition Argparse.H:128
int getMaxIterations() const noexcept
Get the maximum number of iterations ($$ N_{max} $$).
Definition Argparse.C:142
InputFormat format
Input file format.
Definition Argparse.H:131
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