DPsim
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 <nlohmann/json.hpp>
21 #include <vector>
22 
23 #include <dpsim-models/Components.h>
24 #include <dpsim-models/Filesystem.h>
25 #include <dpsim-models/Logger.h>
26 #include <dpsim/MNASolverFactory.h>
27 #include <dpsim/Simulation.h>
28 #include <dpsim/Solver.h>
29 #include <dpsim/Timer.h>
30 
31 using json = nlohmann::json;
32 
33 namespace DPsim {
34 
36 
37 protected:
38  struct Argument {
39  const char *name;
40  int has_arg;
41  int *flag;
42  int val;
43  const char *valdesc;
44  const char *desc;
45  };
46 
47  String mProgramName;
48  std::vector<Argument> mArguments;
49 
50 public:
51  CommandLineArgs(int argc, char *argv[],
52  /* Default settings */
53  String name = "dpsim", Real dt = 0.001, Real d = 1,
54  Real sf = 50, Int s = -1,
55  CPS::Logger::Level ll = CPS::Logger::Level::info,
56  CPS::Logger::Level clill = CPS::Logger::Level::off,
57  Bool ss = false, Bool b = false, Bool si = false,
58  CPS::Domain sd = CPS::Domain::DP,
59  Solver::Type st = Solver::Type::MNA,
60  DirectLinearSolverImpl mi = DirectLinearSolverImpl::KLU,
61  String spn = "plugin.so", String params = "default.json");
63  /* Default settings */
64  String name = "dpsim", Real dt = 0.001, Real d = 1, Real sf = 50,
65  Int s = -1, CPS::Logger::Level ll = CPS::Logger::Level::info,
66  CPS::Logger::Level clill = CPS::Logger::Level::off, Bool ss = false,
67  Bool b = false, Bool si = false, CPS::Domain sd = CPS::Domain::DP,
68  Solver::Type st = Solver::Type::MNA,
69  DirectLinearSolverImpl mi = DirectLinearSolverImpl::KLU,
70  String spn = "plugin.so");
71 
72  void parseArguments(int argc, char *argv[]);
73  void showUsage();
74 
75  double timeStep;
76  double duration;
77  double sysFreq;
78  int scenario;
79 
80  CPS::Logger::Level logLevel;
81  CPS::Logger::Level cliLogLevel;
82  String name;
83  String params;
84 
85  bool startSynch;
86  bool blocking;
87  bool steadyInit;
88 
89  struct {
90  CPS::Domain domain;
91  Solver::Type type;
92  } solver;
93  DPsim::DirectLinearSolverImpl directImpl;
94  String solverPluginName;
95 
96  DPsim::Timer::StartClock::time_point startTime;
97 
98  std::list<String> positional;
99  std::list<fs::path> positionalPaths() const;
100 
101  std::map<String, String> options;
102 
103  Int getOptionInt(String optionName) {
104  // try to convert to integer number
105  try {
106  return std::stoi(options[optionName]);
107  } catch (...) {
108  throw CPS::TypeException();
109  }
110  }
111 
112  Real getOptionReal(String optionName) {
113  // try to convert to real number
114  try {
115  return std::stod(options[optionName]);
116  } catch (...) {
117  throw CPS::TypeException();
118  }
119  }
120 
121  Bool getOptionBool(String optionName) {
122  // try to convert to boolean
123  if (options[optionName] == "true")
124  return true;
125  else if (options[optionName] == "false")
126  return false;
127  else
128  throw CPS::TypeException();
129  }
130 
131  String getOptionString(String optionName) { return options[optionName]; }
132 };
133 
134 namespace Utils {
135 
136 void applySimulationParametersFromJson(const json config, Simulation &sim);
137 void applySynchronousGeneratorParametersFromJson(
138  const json config,
139  std::shared_ptr<CPS::EMT::Ph3::SynchronGeneratorDQ> syngen);
140 
141 String encodeXml(String &data);
142 
143 template <typename T>
144 static CPS::String type(const CPS::String &stripPrefix = "CPS::") {
145  Int status = 1;
146  const char *mangled, *unmangled;
147 
148  mangled = typeid(T).name();
149 
150 #ifdef _MSC_VER
151  return CPS::String(mangled);
152 #else
153  unmangled = abi::__cxa_demangle(mangled, NULL, NULL, &status);
154 
155  if (status)
156  return mangled;
157  else {
158  CPS::String type = unmangled;
159 
160  delete unmangled;
161 
162  if (type.find(stripPrefix) == 0)
163  type = type.substr(stripPrefix.size());
164 
165  return type;
166  }
167 #endif
168 }
169 
170 std::vector<std::string> tokenize(std::string s, char delimiter);
171 
172 fs::path findFile(const fs::path &name, const fs::path &hint = fs::path(),
173  const std::string &useEnv = std::string());
174 
175 std::list<fs::path> findFiles(std::list<fs::path> filennames,
176  const fs::path &hint,
177  const std::string &useEnv = std::string());
178 
179 } // namespace Utils
180 } // namespace DPsim