12#include <dpsim/Config.h>
38 "Simulation time-step"},
40 "Simulation duration"},
46 "Start time of real-time simulation"},
53 "(DenseLU|SparseLU|KLU|CUDADense|CUDASparse)",
54 "Type of direct linear solver implementation"},
56 "User-definable options"},
59 "Json file containing parametrization"},
61 timeStep(dt), duration(d), sysFreq(sf), scenario(s), logLevel(ll),
62 cliLogLevel(clill), name(nm), params(ps), startSynch(ss), blocking(b),
63 steadyInit(si), solver{sd, st}, directImpl(mi), solverPluginName(spn) {
64 parseArguments(argc, argv);
79 "Simulation time-step"},
81 "Simulation duration"},
87 "Start time of real-time simulation"},
94 "(DenseLU|SparseLU|KLU|CUDADense|CUDASparse)",
95 "Type of direct linear solver implementation"},
97 "User-definable options"},
100 timeStep(dt), duration(d), sysFreq(sf), scenario(s), logLevel(ll),
101 cliLogLevel(clill), name(nm), startSynch(ss), blocking(b), steadyInit(si),
102 solver{sd, st}, directImpl(mi), solverPluginName(spn) {}
106 std::vector<option> long_options;
108 long_options.push_back({a.name, a.has_arg, a.flag, a.val});
113 int option_index = 0;
116 "ht:d:s:l:a:i:f:D:P:T:U:o:Sbn:", long_options.data(),
155 auto p = arg.find(
"=");
156 auto key = arg.substr(0, p);
157 auto value = arg.substr(p + 1);
158 if (p != String::npos)
169 else if (arg ==
"INFO")
171 else if (arg ==
"ERR")
173 else if (arg ==
"WARN")
175 else if (arg ==
"NONE")
178 throw std::invalid_argument(
"Invalid value for --log-level: must be a "
179 "string of DEBUG, INFO, ERR, WARN or NONE");
188 else if (arg ==
"EMT")
190 else if (arg ==
"SP")
193 throw std::invalid_argument(
"Invalid value for --solver-domain: must "
194 "be a string of SP, DP, EMT");
203 else if (arg ==
"NRP")
206 throw std::invalid_argument(
207 "Invalid value for --solver-type: must be a string of NRP or MNA");
212 if (arg ==
"DenseLU") {
214 }
else if (arg ==
"SparseLU") {
216 }
else if (arg ==
"KLU") {
218 }
else if (arg ==
"CUDADense") {
220 }
else if (arg ==
"CUDASparse") {
222 }
else if (arg ==
"CUDAMagma") {
224 }
else if (arg ==
"Plugin") {
227 throw std::invalid_argument(
"Invalid value for --solver-mna-impl");
237 double deltaT = std::stod(
optarg);
240 std::chrono::milliseconds(
static_cast<int>(deltaT * 1e3));
247 std::istringstream ss(
optarg);
249 ss >> std::get_time(&t,
"%Y%m%dT%H%M%S");
252 throw std::invalid_argument(
253 "Invalid value for --start-at: must be a ISO8601 date");
255 std::time_t tt = std::mktime(&t);
257 startTime = Timer::StartClock::from_time_t(tt);
287 std::cout <<
"Usage: " <<
mProgramName <<
" [OPTIONS] [FILES]" << std::endl;
288 std::cout << std::endl;
289 std::cout <<
" Available options:" << std::endl;
295 std::cout <<
" -" <<
static_cast<char>(a.val) <<
", --" << a.name;
298 std::cout <<
" " << a.valdesc;
301 std::cout <<
" " << a.desc;
303 std::cout << std::endl;
306 std::cout << std::endl;
310 std::list<fs::path> paths;
313 paths.emplace_back(p);
321 buffer.reserve(
data.size());
322 for (
size_t pos = 0; pos !=
data.size(); ++pos) {
325 buffer.append(
"&");
328 buffer.append(
""");
331 buffer.append(
"'");
334 buffer.append(
"<");
337 buffer.append(
">");
340 buffer.append(&
data[pos], 1);
349 std::vector<std::string> tokens;
354 while ((curentPos = s.find(delimiter, lastPos)) != std::string::npos) {
355 const size_t tokenLength = curentPos - lastPos;
356 tokens.push_back(s.substr(lastPos, tokenLength));
359 lastPos = curentPos + 1;
363 if (lastPos != s.length()) {
364 const size_t lastTokenLength = s.length() - lastPos;
365 tokens.push_back(s.substr(lastPos, lastTokenLength));
372 const std::string &useEnv) {
379 std::vector<fs::path> searchPaths = {fs::current_path()};
382 searchPaths.push_back(hint);
385 if (!useEnv.empty() && getenv(useEnv.c_str())) {
386 std::vector<std::string> envPaths =
tokenize(getenv(useEnv.c_str()), sep);
388 for (std::string envPath : envPaths) {
389 searchPaths.emplace_back(envPath);
393 for (
auto searchPath : searchPaths) {
396 if (searchPath.is_relative())
397 fullPath /= fs::current_path();
399 fullPath /= searchPath;
402 if (fs::exists(fullPath)) {
403 return fs::absolute(fullPath);
408 for (
auto searchPath : searchPaths)
409 searchPathsString.append(searchPath.string().append(
"\n"));
411 throw std::runtime_error(fmt::format(
"File not found: {}\nSearch paths:\n{}",
412 name.string(), searchPathsString));
416 const fs::path &hint,
417 const std::string &useEnv) {
419 std::list<fs::path> foundnames;
421 for (
auto filename : filennames) {
422 auto foundname =
findFile(filename, hint, useEnv);
424 foundnames.emplace_back(foundname);
431void DPsim::Utils::applySimulationParametersFromJson(
const json config,
433 if (config.contains(
"timestep"))
435 if (config.contains(
"duration"))
439void DPsim::Utils::applySynchronousGeneratorParametersFromJson(
441 std::shared_ptr<CPS::EMT::Ph3::SynchronGeneratorDQ> syngen) {
442 if (config.contains(
"options")) {
443 Bool containsSyngenOptions =
false;
444 for (
String attrName : syngen->attrParamNames) {
445 if (config[
"options"].contains(attrName)) {
446 syngen->attributeTyped<
Real>(attrName)->set(
447 config[
"options"][attrName].get<double>());
448 containsSyngenOptions =
true;
451 if (containsSyngenOptions)
452 syngen->applyParametersOperationalPerUnit();
spdlog::level::level_enum Level
std::list< String > positional
void parseArguments(int argc, char *argv[])
DPsim::DirectLinearSolverImpl directImpl
std::list< fs::path > positionalPaths() const
CommandLineArgs(int argc, char *argv[], String name="dpsim", Real dt=0.001, Real d=1, Real sf=50, Int s=-1, CPS::Logger::Level ll=CPS::Logger::Level::info, CPS::Logger::Level clill=CPS::Logger::Level::off, Bool ss=false, Bool b=false, Bool si=false, CPS::Domain sd=CPS::Domain::DP, Solver::Type st=Solver::Type::MNA, DirectLinearSolverImpl mi=DirectLinearSolverImpl::KLU, String spn="plugin.so", String params="default.json")
struct DPsim::CommandLineArgs::@006274247107050243354147375320272025065223106110 solver
std::map< String, String > options
std::vector< Argument > mArguments
DPsim::Timer::StartClock::time_point startTime
CPS::Logger::Level logLevel
The Simulation holds a SystemTopology and a Solver.
void setFinalTime(Real finalTime)
void setTimeStep(Real timeStep)
int getopt_long(int nargc, char *const *nargv, const char *options, const struct option *long_options, int *idx)
struct dps_magma_data data
std::list< fs::path > findFiles(std::list< fs::path > filennames, const fs::path &hint, const std::string &useEnv=std::string())
String encodeXml(String &data)
std::vector< std::string > tokenize(std::string s, char delimiter)
fs::path findFile(const fs::path &name, const fs::path &hint=fs::path(), const std::string &useEnv=std::string())