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|MetalDense|"
54 "MetalSparseBiCGSTAB)",
55 "Type of direct linear solver implementation"},
57 "User-definable options"},
60 "Json file containing parametrization"},
62 timeStep(dt), duration(d), sysFreq(sf), scenario(s), logLevel(ll),
63 cliLogLevel(clill), name(nm), params(ps), startSynch(ss), blocking(b),
64 steadyInit(si), solver{sd, st}, directImpl(mi), solverPluginName(spn) {
65 parseArguments(argc, argv);
80 "Simulation time-step"},
82 "Simulation duration"},
88 "Start time of real-time simulation"},
95 "(DenseLU|SparseLU|KLU|CUDADense|CUDASparse|MetalDense|"
96 "MetalSparseBiCGSTAB)",
97 "Type of direct linear solver implementation"},
99 "User-definable options"},
102 timeStep(dt), duration(d), sysFreq(sf), scenario(s), logLevel(ll),
103 cliLogLevel(clill), name(nm), startSynch(ss), blocking(b), steadyInit(si),
104 solver{sd, st}, directImpl(mi), solverPluginName(spn) {}
108 std::vector<option> long_options;
110 long_options.push_back({a.name, a.has_arg, a.flag, a.val});
115 int option_index = 0;
118 "ht:d:s:l:a:i:f:D:P:T:U:o:Sbn:", long_options.data(),
157 auto p = arg.find(
"=");
158 auto key = arg.substr(0, p);
159 auto value = arg.substr(p + 1);
160 if (p != String::npos)
171 else if (arg ==
"INFO")
173 else if (arg ==
"ERR")
175 else if (arg ==
"WARN")
177 else if (arg ==
"NONE")
180 throw std::invalid_argument(
"Invalid value for --log-level: must be a "
181 "string of DEBUG, INFO, ERR, WARN or NONE");
190 else if (arg ==
"EMT")
192 else if (arg ==
"SP")
195 throw std::invalid_argument(
"Invalid value for --solver-domain: must "
196 "be a string of SP, DP, EMT");
205 else if (arg ==
"NRP")
208 throw std::invalid_argument(
209 "Invalid value for --solver-type: must be a string of NRP or MNA");
214 if (arg ==
"DenseLU") {
216 }
else if (arg ==
"SparseLU") {
218 }
else if (arg ==
"KLU") {
220 }
else if (arg ==
"CUDADense") {
222 }
else if (arg ==
"CUDASparse") {
224 }
else if (arg ==
"CUDAMagma") {
226 }
else if (arg ==
"MetalDense") {
228 }
else if (arg ==
"MetalSparseBiCGSTAB") {
230 }
else if (arg ==
"HIPDense") {
232 }
else if (arg ==
"HIPSparseBiCGStab") {
234 }
else if (arg ==
"HIPSparseLURefactor") {
236 }
else if (arg ==
"HIPSparseQR") {
238 }
else if (arg ==
"Plugin") {
241 throw std::invalid_argument(
"Invalid value for --solver-mna-impl");
251 double deltaT = std::stod(
optarg);
254 std::chrono::milliseconds(
static_cast<int>(deltaT * 1e3));
261 std::istringstream ss(
optarg);
263 ss >> std::get_time(&t,
"%Y%m%dT%H%M%S");
266 throw std::invalid_argument(
267 "Invalid value for --start-at: must be a ISO8601 date");
269 std::time_t tt = std::mktime(&t);
271 startTime = Timer::StartClock::from_time_t(tt);
301 std::cout <<
"Usage: " <<
mProgramName <<
" [OPTIONS] [FILES]" << std::endl;
302 std::cout << std::endl;
303 std::cout <<
" Available options:" << std::endl;
309 std::cout <<
" -" <<
static_cast<char>(a.val) <<
", --" << a.name;
312 std::cout <<
" " << a.valdesc;
315 std::cout <<
" " << a.desc;
317 std::cout << std::endl;
320 std::cout << std::endl;
324 std::list<fs::path> paths;
327 paths.emplace_back(p);
335 buffer.reserve(
data.size());
336 for (
size_t pos = 0; pos !=
data.size(); ++pos) {
339 buffer.append(
"&");
342 buffer.append(
""");
345 buffer.append(
"'");
348 buffer.append(
"<");
351 buffer.append(
">");
354 buffer.append(&
data[pos], 1);
363 std::vector<std::string> tokens;
368 while ((curentPos = s.find(delimiter, lastPos)) != std::string::npos) {
369 const size_t tokenLength = curentPos - lastPos;
370 tokens.push_back(s.substr(lastPos, tokenLength));
373 lastPos = curentPos + 1;
377 if (lastPos != s.length()) {
378 const size_t lastTokenLength = s.length() - lastPos;
379 tokens.push_back(s.substr(lastPos, lastTokenLength));
386 const std::string &useEnv) {
393 std::vector<fs::path> searchPaths = {fs::current_path()};
396 searchPaths.push_back(hint);
399 if (!useEnv.empty() && getenv(useEnv.c_str())) {
400 std::vector<std::string> envPaths =
tokenize(getenv(useEnv.c_str()), sep);
402 for (std::string envPath : envPaths) {
403 searchPaths.emplace_back(envPath);
407 for (
auto searchPath : searchPaths) {
410 if (searchPath.is_relative())
411 fullPath /= fs::current_path();
413 fullPath /= searchPath;
416 if (fs::exists(fullPath)) {
417 return fs::absolute(fullPath);
422 for (
auto searchPath : searchPaths)
423 searchPathsString.append(searchPath.string().append(
"\n"));
425 throw std::runtime_error(fmt::format(
"File not found: {}\nSearch paths:\n{}",
426 name.string(), searchPathsString));
430 const fs::path &hint,
431 const std::string &useEnv) {
433 std::list<fs::path> foundnames;
435 for (
auto filename : filennames) {
436 auto foundname =
findFile(filename, hint, useEnv);
438 foundnames.emplace_back(foundname);
445void DPsim::Utils::applySimulationParametersFromJson(
const json config,
447 if (config.contains(
"timestep"))
449 if (config.contains(
"duration"))
453void DPsim::Utils::applySynchronousGeneratorParametersFromJson(
455 std::shared_ptr<CPS::EMT::Ph3::SynchronGeneratorDQ> syngen) {
456 if (config.contains(
"options")) {
457 Bool containsSyngenOptions =
false;
458 for (
String attrName : syngen->attrParamNames) {
459 if (config[
"options"].contains(attrName)) {
460 syngen->attributeTyped<
Real>(attrName)->set(
461 config[
"options"][attrName].get<double>());
462 containsSyngenOptions =
true;
465 if (containsSyngenOptions)
466 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())