DPsim
Loading...
Searching...
No Matches
Utils.h
1/* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2 * EONERC, RWTH Aachen University
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7 *********************************************************************************/
8
9#pragma once
10
11#if defined(__clang__)
12#include <typeinfo>
13#endif
14#if defined(__GNUC__) || defined(__clang__)
15#include <cxxabi.h>
16#endif
17
18#include <cstdlib>
19#include <list>
20#include <vector>
21
22#include <dpsim-models/Components.h>
23#include <dpsim-models/Filesystem.h>
24#include <dpsim-models/Logger.h>
25#include <dpsim/MNASolverFactory.h>
26#include <dpsim/Simulation.h>
27#include <dpsim/Solver.h>
28#include <dpsim/Timer.h>
29
30#ifdef WITH_JSON
31#include <nlohmann/json.hpp>
32using json = nlohmann::json;
33#endif
34
35namespace DPsim {
36
37class CommandLineArgs {
38
39protected:
40 struct Argument {
41 const char *name;
42 int has_arg;
43 int *flag;
44 int val;
45 const char *valdesc;
46 const char *desc;
47 };
48
49 String mProgramName;
50 std::vector<Argument> mArguments;
51
52public:
53 CommandLineArgs(int argc, char *argv[],
54 /* Default settings */
55 String name = "dpsim", Real dt = 0.001, Real d = 1,
56 Real sf = 50, Int s = -1,
57 CPS::Logger::Level ll = CPS::Logger::Level::info,
58 CPS::Logger::Level clill = CPS::Logger::Level::off,
59 Bool ss = false, Bool b = false, Bool si = false,
60 CPS::Domain sd = CPS::Domain::DP,
61 Solver::Type st = Solver::Type::MNA,
62 DirectLinearSolverImpl mi = DirectLinearSolverImpl::KLU,
63 String spn = "plugin.so", String params = "default.json");
64 CommandLineArgs(
65 /* Default settings */
66 String name = "dpsim", Real dt = 0.001, Real d = 1, Real sf = 50,
67 Int s = -1, CPS::Logger::Level ll = CPS::Logger::Level::info,
68 CPS::Logger::Level clill = CPS::Logger::Level::off, Bool ss = false,
69 Bool b = false, Bool si = false, CPS::Domain sd = CPS::Domain::DP,
70 Solver::Type st = Solver::Type::MNA,
71 DirectLinearSolverImpl mi = DirectLinearSolverImpl::KLU,
72 String spn = "plugin.so");
73
74 void parseArguments(int argc, char *argv[]);
75 void showUsage();
76
77 double timeStep;
78 double duration;
79 double sysFreq;
80 int scenario;
81
82 CPS::Logger::Level logLevel;
83 CPS::Logger::Level cliLogLevel;
84 String name;
85 String params;
86
87 bool startSynch;
88 bool blocking;
89 bool steadyInit;
90
91 struct {
92 CPS::Domain domain;
93 Solver::Type type;
94 } solver;
95 DPsim::DirectLinearSolverImpl directImpl;
96 String solverPluginName;
97
98 DPsim::Timer::StartClock::time_point startTime;
99
100 std::list<String> positional;
101 std::list<fs::path> positionalPaths() const;
102
103 std::map<String, String> options;
104
105 Int getOptionInt(String optionName) {
106 // try to convert to integer number
107 try {
108 return std::stoi(options[optionName]);
109 } catch (...) {
110 throw CPS::TypeException();
111 }
112 }
113
114 Real getOptionReal(String optionName) {
115 // try to convert to real number
116 try {
117 return std::stod(options[optionName]);
118 } catch (...) {
119 throw CPS::TypeException();
120 }
121 }
122
123 Bool getOptionBool(String optionName) {
124 // try to convert to boolean
125 if (options[optionName] == "true")
126 return true;
127 else if (options[optionName] == "false")
128 return false;
129 else
130 throw CPS::TypeException();
131 }
132
133 String getOptionString(String optionName) { return options[optionName]; }
134};
135
136namespace Utils {
137
138#ifdef WITH_JSON
139void applySimulationParametersFromJson(const json config, Simulation &sim);
140void applySynchronousGeneratorParametersFromJson(
141 const json config,
142 std::shared_ptr<CPS::EMT::Ph3::SynchronGeneratorDQ> syngen);
143#endif
144
145String encodeXml(String &data);
146
147template <typename T>
148static CPS::String type(const CPS::String &stripPrefix = "CPS::") {
149 Int status = 1;
150 const char *mangled, *unmangled;
151
152 mangled = typeid(T).name();
153
154#ifdef _MSC_VER
155 return CPS::String(mangled);
156#else
157 unmangled = abi::__cxa_demangle(mangled, NULL, NULL, &status);
158
159 if (status)
160 return mangled;
161 else {
162 CPS::String type = unmangled;
163
164 delete unmangled;
165
166 if (type.find(stripPrefix) == 0)
167 type = type.substr(stripPrefix.size());
168
169 return type;
170 }
171#endif
172}
173
174std::vector<std::string> tokenize(std::string s, char delimiter);
175
176fs::path findFile(const fs::path &name, const fs::path &hint = fs::path(),
177 const std::string &useEnv = std::string());
178
179std::list<fs::path> findFiles(std::list<fs::path> filennames,
180 const fs::path &hint,
181 const std::string &useEnv = std::string());
182
183} // namespace Utils
184} // namespace DPsim