DPsim
Loading...
Searching...
No Matches
main.cpp
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#include <iomanip>
10
11#include <pybind11/complex.h>
12#include <pybind11/eigen.h>
13#include <pybind11/functional.h>
14#include <pybind11/iostream.h>
15#include <pybind11/pybind11.h>
16#include <pybind11/stl.h>
17
18#include <DPsim.h>
19
20#include <DPsimPy.h>
21
22PYBIND11_DECLARE_HOLDER_TYPE(T, CPS::AttributePointer<T>);
23
24namespace py = pybind11;
25using namespace pybind11::literals;
26
27PYBIND11_MODULE(dpsimpy, m) {
28 m.doc() = R"pbdoc(
29 DPsim Python bindings
30 -----------------------
31 The Python bindings provide access to most of the DPsim features implemented in C++.
32 It is possible to run powerflow, quasi-static, dynamic phasor and electromagnetic transient simulations
33 and to parameterize all components of the network from Python.
34 )pbdoc";
35
36 //Enums
37 py::enum_<CPS::Logger::Level>(m, "LogLevel")
38 .value("trace", CPS::Logger::Level::trace)
39 .value("debug", CPS::Logger::Level::debug)
40 .value("info", CPS::Logger::Level::info)
41 .value("warn", CPS::Logger::Level::warn)
42 .value("err", CPS::Logger::Level::err)
43 .value("critical", CPS::Logger::Level::critical)
44 .value("off", CPS::Logger::Level::off);
45
46 py::class_<CPS::Math>(m, "Math")
47 .def_static("single_phase_variable_to_three_phase",
49 .def_static("single_phase_parameter_to_three_phase",
51 .def_static("single_phase_power_to_three_phase",
53
54 py::enum_<DPsim::Solver::Behaviour>(m, "SolverBehaviour")
55 .value("Initialization", DPsim::Solver::Behaviour::Initialization)
56 .value("Simulation", DPsim::Solver::Behaviour::Simulation);
57
58 py::enum_<CPS::Domain>(m, "Domain")
59 .value("SP", CPS::Domain::SP)
60 .value("DP", CPS::Domain::DP)
61 .value("EMT", CPS::Domain::EMT);
62
63 py::enum_<CPS::PhaseType>(m, "PhaseType")
64 .value("A", CPS::PhaseType::A)
65 .value("B", CPS::PhaseType::B)
66 .value("C", CPS::PhaseType::C)
67 .value("ABC", CPS::PhaseType::ABC)
68 .value("Single", CPS::PhaseType::Single);
69
70 py::enum_<CPS::PowerflowBusType>(m, "PowerflowBusType")
71 .value("PV", CPS::PowerflowBusType::PV)
72 .value("PQ", CPS::PowerflowBusType::PQ)
73 .value("VD", CPS::PowerflowBusType::VD)
74 .value("None", CPS::PowerflowBusType::None);
75
76 py::enum_<CPS::GeneratorType>(m, "GeneratorType")
77 .value("PVNode", CPS::GeneratorType::PVNode)
78 .value("TransientStability", CPS::GeneratorType::TransientStability)
79 .value("IdealVoltageSource", CPS::GeneratorType::IdealVoltageSource)
80 .value("SG3OrderVBR", CPS::GeneratorType::SG3OrderVBR)
81 .value("SG4OrderVBR", CPS::GeneratorType::SG4OrderVBR)
82 .value("SG5OrderVBR", CPS::GeneratorType::SG5OrderVBR)
83 .value("SG6aOrderVBR", CPS::GeneratorType::SG6aOrderVBR)
84 .value("SG6bOrderVBR", CPS::GeneratorType::SG6bOrderVBR)
85 .value("FullOrderVBR", CPS::GeneratorType::FullOrderVBR)
86 .value("FullOrder", CPS::GeneratorType::FullOrder)
87 .value("NONE", CPS::GeneratorType::None);
88
89 py::enum_<CPS::CouplingMethod>(m, "CouplingMethod")
90 .value("DELAY", CPS::CouplingMethod::DELAY)
91 .value("EXTRAPOLATION_ZOH", CPS::CouplingMethod::EXTRAPOLATION_ZOH)
92 .value("EXTRAPOLATION_LINEAR", CPS::CouplingMethod::EXTRAPOLATION_LINEAR);
93
94 py::enum_<DPsim::Solver::Type>(m, "Solver")
95 .value("MNA", DPsim::Solver::Type::MNA)
96 .value("DAE", DPsim::Solver::Type::DAE)
97 .value("NRP", DPsim::Solver::Type::NRP);
98
99 py::enum_<DPsim::DirectLinearSolverImpl>(m, "DirectLinearSolverImpl")
100 .value("Undef", DPsim::DirectLinearSolverImpl::Undef)
101 .value("DenseLU", DPsim::DirectLinearSolverImpl::DenseLU)
102 .value("SparseLU", DPsim::DirectLinearSolverImpl::SparseLU)
103 .value("KLU", DPsim::DirectLinearSolverImpl::KLU)
104 .value("CUDADense", DPsim::DirectLinearSolverImpl::CUDADense)
105 .value("CUDASparse", DPsim::DirectLinearSolverImpl::CUDASparse)
106 .value("CUDAMagma", DPsim::DirectLinearSolverImpl::CUDAMagma);
107
108 py::enum_<DPsim::SCALING_METHOD>(m, "scaling_method")
109 .value("no_scaling", DPsim::SCALING_METHOD::NO_SCALING)
110 .value("sum_scaling", DPsim::SCALING_METHOD::SUM_SCALING)
111 .value("max_scaling", DPsim::SCALING_METHOD::MAX_SCALING);
112
113 py::enum_<DPsim::FILL_IN_REDUCTION_METHOD>(m, "fill_in_reduction_method")
114 .value("amd", DPsim::FILL_IN_REDUCTION_METHOD::AMD)
115 .value("amd_nv", DPsim::FILL_IN_REDUCTION_METHOD::AMD_NV)
116 .value("amd_ra", DPsim::FILL_IN_REDUCTION_METHOD::AMD_RA)
117 .value("colamd", DPsim::FILL_IN_REDUCTION_METHOD::COLAMD);
118
119 py::enum_<DPsim::PARTIAL_REFACTORIZATION_METHOD>(
120 m, "partial_refactorization_method")
121 .value("no_partial_refactorization",
122 DPsim::PARTIAL_REFACTORIZATION_METHOD::NO_PARTIAL_REFACTORIZATION)
123 .value("factorization_path",
124 DPsim::PARTIAL_REFACTORIZATION_METHOD::FACTORIZATION_PATH)
125 .value("refactorization_restart",
126 DPsim::PARTIAL_REFACTORIZATION_METHOD::REFACTORIZATION_RESTART);
127
128 py::enum_<DPsim::USE_BTF>(m, "use_btf")
129 .value("no_btf", DPsim::USE_BTF::NO_BTF)
130 .value("do_btf", DPsim::USE_BTF::DO_BTF);
131
132 py::enum_<CPS::CSVReader::Mode>(m, "CSVReaderMode")
133 .value("AUTO", CPS::CSVReader::Mode::AUTO)
134 .value("MANUAL", CPS::CSVReader::Mode::MANUAL);
135
136 py::enum_<CPS::CSVReader::DataFormat>(m, "CSVReaderFormat")
137 .value("HHMMSS", CPS::CSVReader::DataFormat::HHMMSS)
138 .value("SECONDS", CPS::CSVReader::DataFormat::SECONDS)
139 .value("HOURS", CPS::CSVReader::DataFormat::HOURS)
140 .value("MINUTES", CPS::CSVReader::DataFormat::MINUTES);
141
142 py::enum_<DPsim::StateSpaceAnalysisFrame>(m, "StateSpaceAnalysisFrame")
143 .value("Native", DPsim::StateSpaceAnalysisFrame::Native)
144 .value("GlobalDQ0", DPsim::StateSpaceAnalysisFrame::GlobalDQ0);
145
146 m.attr("RMS3PH_TO_PEAK1PH") = RMS3PH_TO_PEAK1PH;
147 m.attr("PEAK1PH_TO_RMS3PH") = PEAK1PH_TO_RMS3PH;
148 m.attr("P_SNUB_TRANSFORMER") = P_SNUB_TRANSFORMER;
149 m.attr("Q_SNUB_TRANSFORMER") = Q_SNUB_TRANSFORMER;
150
151 addAttributes(m);
152
153 py::class_<DPsim::DirectLinearSolverConfiguration>(
154 m, "DirectLinearSolverConfiguration")
155 .def(py::init<>())
156 .def("set_fill_in_reduction_method",
157 &DPsim::DirectLinearSolverConfiguration::setFillInReductionMethod)
158 .def("set_scaling_method",
159 &DPsim::DirectLinearSolverConfiguration::setScalingMethod)
160 .def("set_partial_refactorization_method",
162 setPartialRefactorizationMethod)
163 .def("set_btf", &DPsim::DirectLinearSolverConfiguration::setBTF)
164 .def("get_scaling_method",
165 &DPsim::DirectLinearSolverConfiguration::getScalingMethod)
166 .def("get_fill_in_reduction_method",
167 &DPsim::DirectLinearSolverConfiguration::getFillInReductionMethod)
168 .def("get_partial_refactorization_method",
170 getPartialRefactorizationMethod)
171 .def("get_btf", &DPsim::DirectLinearSolverConfiguration::getBTF);
172
173 py::class_<DPsim::MNAStateSpaceExtractor>(m, "MNAStateSpaceExtractor")
174 .def("is_initialized", &DPsim::MNAStateSpaceExtractor::isInitialized)
175 .def("get_state_count", &DPsim::MNAStateSpaceExtractor::getStateCount)
176 .def("get_time_step", &DPsim::MNAStateSpaceExtractor::getTimeStep)
177 .def("get_discrete_state_matrix",
178 &DPsim::MNAStateSpaceExtractor::getDiscreteStateMatrix,
179 py::return_value_policy::reference_internal);
180
181 py::class_<DPsim::StateSpaceModalAnalysis>(m, "StateSpaceModalAnalysis")
182 .def(py::init<const DPsim::MNAStateSpaceExtractor &>(),
183 py::keep_alive<1, 2>())
184 .def("set_analysis_frame",
186 .def("set_global_dq0_frame",
188 "theta0"_a = 0.0)
190 .def("get_discrete_eigenvalues",
192 py::return_value_policy::reference_internal)
193 .def("get_continuous_eigenvalues",
195 py::return_value_policy::reference_internal)
196 .def("get_right_eigenvectors",
198 py::return_value_policy::reference_internal)
199 .def("get_left_eigenvectors",
201 py::return_value_policy::reference_internal)
202 .def("get_participation_factors",
204 py::return_value_policy::reference_internal)
205 .def("get_state_names", &DPsim::StateSpaceModalAnalysis::getStateNames,
206 py::return_value_policy::reference_internal);
207
208 py::class_<DPsim::Simulation>(m, "Simulation")
209 .def(py::init<std::string, CPS::Logger::Level>(), "name"_a,
210 "loglevel"_a = CPS::Logger::Level::off)
211 .def("name", &DPsim::Simulation::name)
212 .def("set_time_step", &DPsim::Simulation::setTimeStep)
213 .def("set_final_time", &DPsim::Simulation::setFinalTime)
214 .def("add_logger", &DPsim::Simulation::addLogger)
215 .def("set_system", &DPsim::Simulation::setSystem)
216 .def("run", &DPsim::Simulation::run)
217 .def("set_solver", &DPsim::Simulation::setSolverType)
218 .def("set_pf_keep_last_solution",
219 &DPsim::Simulation::setPFKeepLastSolution)
220 .def("get_pf_keep_last_solution",
221 &DPsim::Simulation::getPFKeepLastSolution)
222 .def("set_pf_base_apparent_power_fallback",
223 &DPsim::Simulation::setPFBaseApparentPowerFallback)
224 .def("get_pf_base_apparent_power_fallback",
225 &DPsim::Simulation::getPFBaseApparentPowerFallback)
226 .def("set_pf_max_iterations", &DPsim::Simulation::setPFMaxIterations)
227 .def("get_pf_max_iterations", &DPsim::Simulation::getPFMaxIterations)
228 .def("set_pf_solver_use_sparse", &DPsim::Simulation::setPFSolverUseSparse)
229 .def("get_pf_solver_use_sparse", &DPsim::Simulation::getPFSolverUseSparse)
230 .def("set_pf_solver_enforce_q_limits",
231 &DPsim::Simulation::setPFSolverEnforceReactiveLimits)
232 .def("get_pf_solver_enforce_q_limits",
233 &DPsim::Simulation::getPFSolverEnforceReactiveLimits)
234 .def("set_pf_solver_base_voltage_loose_tolerance",
235 &DPsim::Simulation::setPFSolverBaseVoltageLooseTolerance)
236 .def("get_pf_solver_base_voltage_loose_tolerance",
237 &DPsim::Simulation::getPFSolverBaseVoltageLooseTolerance)
238 .def("set_pf_solver_base_voltage_strict_tolerance",
239 &DPsim::Simulation::setPFSolverBaseVoltageStrictTolerance)
240 .def("get_pf_solver_base_voltage_strict_tolerance",
241 &DPsim::Simulation::getPFSolverBaseVoltageStrictTolerance)
242 .def("set_domain", &DPsim::Simulation::setDomain)
243 .def("start", &DPsim::Simulation::start)
244 .def("next", &DPsim::Simulation::next)
245 .def("stop", &DPsim::Simulation::stop)
246 .def("get_idobj_attr", &DPsim::Simulation::getIdObjAttribute, "comp"_a,
247 "attr"_a)
248 .def("add_interface", &DPsim::Simulation::addInterface, "interface"_a)
249 .def("log_idobj_attribute", &DPsim::Simulation::logIdObjAttribute,
250 "comp"_a, "attr"_a)
251 .def("log_attribute", &DPsim::Simulation::logAttribute, "name"_a,
252 "attr"_a)
253 .def("do_init_from_nodes_and_terminals",
254 &DPsim::Simulation::doInitFromNodesAndTerminals)
255 .def("do_system_matrix_recomputation",
256 &DPsim::Simulation::doSystemMatrixRecomputation)
257 .def("do_state_space_extraction",
259 py::arg_v("value", true, "True"))
260 .def("get_state_space_extractor",
261 &DPsim::Simulation::getStateSpaceExtractor, "solver_index"_a = 0,
262 py::return_value_policy::reference_internal)
263 .def("do_steady_state_init", &DPsim::Simulation::doSteadyStateInit)
264 .def("do_frequency_parallelization",
266 .def("do_split_subnets", &DPsim::Simulation::doSplitSubnets)
267 .def("set_tearing_components", &DPsim::Simulation::setTearingComponents)
268 .def("add_event", &DPsim::Simulation::addEvent)
269 .def("set_solver_component_behaviour",
271 .def("set_direct_solver_implementation",
272 &DPsim::Simulation::setDirectLinearSolverImplementation)
273 .def("set_direct_linear_solver_configuration",
274 &DPsim::Simulation::setDirectLinearSolverConfiguration)
275 .def("log_lu_times", &DPsim::Simulation::logLUTimes);
276
277#ifdef WITH_RT
278 py::class_<DPsim::RealTimeSimulation, DPsim::Simulation>(m,
279 "RealTimeSimulation")
280 .def(py::init<std::string, CPS::Logger::Level>(), "name"_a,
281 "loglevel"_a = CPS::Logger::Level::info)
282 .def("name", &DPsim::RealTimeSimulation::name)
283 .def("set_time_step", &DPsim::RealTimeSimulation::setTimeStep)
284 .def("set_final_time", &DPsim::RealTimeSimulation::setFinalTime)
285 .def("add_logger", &DPsim::RealTimeSimulation::addLogger)
286 .def("set_system", &DPsim::RealTimeSimulation::setSystem)
287 .def("run",
288 static_cast<void (DPsim::RealTimeSimulation::*)(CPS::Int startIn)>(
290 .def("set_solver", &DPsim::RealTimeSimulation::setSolverType)
291 .def("set_domain", &DPsim::RealTimeSimulation::setDomain);
292#endif
293
294 py::class_<CPS::SystemTopology, std::shared_ptr<CPS::SystemTopology>>(
295 m, "SystemTopology")
296 .def(py::init<CPS::Real, CPS::TopologicalNode::List,
297 CPS::IdentifiedObject::List>())
298 .def(py::init<CPS::Real, CPS::Matrix, CPS::TopologicalNode::List,
299 CPS::IdentifiedObject::List>())
300 .def(py::init<CPS::Real>())
301 .def("add_component", &DPsim::SystemTopology::addComponent)
302 .def("add_component", &DPsim::SystemTopology::addComponents)
303 .def("add_node", &DPsim::SystemTopology::addNode)
304 .def("node", py::overload_cast<std::string_view>(
306 .def("node", py::overload_cast<CPS::UInt>(
308 .def("connect_component",
309 py::overload_cast<CPS::SimPowerComp<CPS::Real>::Ptr,
310 CPS::SimNode<CPS::Real>::List>(
312 .def("connect_component",
313 py::overload_cast<CPS::SimPowerComp<CPS::Complex>::Ptr,
314 CPS::SimNode<CPS::Complex>::List>(
316 .def("component",
318 .def("add_tear_component", &DPsim::SystemTopology::addTearComponent)
319#ifdef WITH_GRAPHVIZ
320 .def("_repr_svg_", &DPsim::SystemTopology::render)
321 .def("render_to_file", &DPsim::SystemTopology::renderToFile)
322#endif
323 .def_readwrite("nodes", &DPsim::SystemTopology::mNodes)
324 .def_readwrite("components", &DPsim::SystemTopology::mComponents)
325 .def_readwrite("components_at_node",
327 .def_readonly("tear_components", &DPsim::SystemTopology::mTearComponents)
328 .def("list_idobjects", &DPsim::SystemTopology::listIdObjects)
329 .def("init_with_powerflow", &DPsim::SystemTopology::initWithPowerflow,
330 "systemPF"_a, "domain"_a)
331 .def("add_component", &DPsim::SystemTopology::addComponent)
332 .def("add_components", &DPsim::SystemTopology::addComponents)
333 .def("remove_component", &DPsim::SystemTopology::removeComponent);
334
335 py::class_<DPsim::Interface, std::shared_ptr<DPsim::Interface>>(m,
336 "Interface");
337
339 std::shared_ptr<DPsim::DataLoggerInterface>>(m,
340 "DataLoggerInterface")
341 .def("log_attribute",
342 py::overload_cast<const CPS::String &, CPS::AttributeBase::Ptr,
343 CPS::UInt, CPS::UInt>(
344 &DPsim::DataLoggerInterface::logAttribute),
345 "name"_a, "attr"_a, "max_cols"_a = 0, "max_rows"_a = 0)
347 .def("log_attribute",
348 py::overload_cast<const std::vector<CPS::String> &,
349 CPS::AttributeBase::Ptr>(
350 &DPsim::DataLoggerInterface::logAttribute),
351 "names"_a, "attr"_a)
353 .def("log_attribute",
355 const std::vector<CPS::String> &names, const CPS::String &attr,
356 const CPS::IdentifiedObject &comp) {
357 logger.logAttribute(names, comp.attribute(attr));
358 });
359
361 std::shared_ptr<DPsim::DataLogger>>(m, "Logger")
362 .def(py::init<std::string>())
363 .def_static("set_log_dir", &CPS::Logger::setLogDir)
364 .def_static("get_log_dir", &CPS::Logger::logDir)
365 .def("log_attribute",
366 py::overload_cast<const CPS::String &, CPS::AttributeBase::Ptr,
367 CPS::UInt, CPS::UInt>(
368 &DPsim::DataLogger::logAttribute),
369 "name"_a, "attr"_a, "max_cols"_a = 0, "max_rows"_a = 0)
371 .def("log_attribute",
372 py::overload_cast<const std::vector<CPS::String> &,
373 CPS::AttributeBase::Ptr>(
374 &DPsim::DataLogger::logAttribute),
375 "names"_a, "attr"_a)
377 .def(
378 "log_attribute",
379 [](DPsim::DataLogger &logger, const CPS::String &name,
380 const CPS::String &attr, const CPS::IdentifiedObject &comp,
381 CPS::UInt rowsMax, CPS::UInt colsMax) {
382 logger.logAttribute(name, comp.attribute(attr), rowsMax, colsMax);
383 },
384 "name"_a, "attr"_a, "comp"_a, "rows_max"_a = 0, "cols_max"_a = 0)
386 .def("log_attribute",
387 [](DPsim::DataLogger &logger, const std::vector<CPS::String> &names,
388 const CPS::String &attr, const CPS::IdentifiedObject &comp) {
389 logger.logAttribute(names, comp.attribute(attr));
390 });
391#ifdef WITH_RT
393 std::shared_ptr<DPsim::RealTimeDataLogger>>(m,
394 "RealTimeDataLogger")
395
396 .def(py::init([](py::object filename, DPsim::Real final_time,
397 DPsim::Real time_step) {
398 py::object fspath =
399 py::module_::import("os").attr("fspath")(filename);
400 std::string s = py::cast<std::string>(fspath);
401 std::filesystem::path p(s);
402 return std::make_shared<DPsim::RealTimeDataLogger>(p, final_time,
403 time_step);
404 }),
405 "filename"_a, "final_time"_a, "time_step"_a)
406
407 .def(py::init([](py::object filename, std::size_t row_number) {
408 py::object fspath =
409 py::module_::import("os").attr("fspath")(filename);
410 std::string s = py::cast<std::string>(fspath);
411 std::filesystem::path p(s);
412 return std::make_shared<DPsim::RealTimeDataLogger>(p, row_number);
413 }),
414 "filename"_a, "row_number"_a)
415
416 .def("log_attribute",
417 py::overload_cast<const CPS::String &, CPS::AttributeBase::Ptr,
418 CPS::UInt, CPS::UInt>(
419 &DPsim::DataLoggerInterface::logAttribute),
420 "name"_a, "attr"_a, "max_cols"_a = 0, "max_rows"_a = 0)
421
422 .def("log_attribute",
423 py::overload_cast<const std::vector<CPS::String> &,
424 CPS::AttributeBase::Ptr>(
425 &DPsim::DataLoggerInterface::logAttribute),
426 "names"_a, "attr"_a)
427
428 .def(
429 "log_attribute",
430 [](DPsim::RealTimeDataLogger &logger, const CPS::String &name,
431 const CPS::String &attr, const CPS::IdentifiedObject &comp,
432 CPS::UInt rowsMax, CPS::UInt colsMax) {
433 logger.logAttribute(name, comp.attribute(attr), rowsMax, colsMax);
434 },
435 "name"_a, "attr"_a, "comp"_a, "rows_max"_a = 0, "cols_max"_a = 0)
436
437 .def(
438 "log_attribute",
439 [](DPsim::RealTimeDataLogger &logger,
440 const std::vector<CPS::String> &names, const CPS::String &attr,
441 const CPS::IdentifiedObject &comp) {
442 logger.logAttribute(names, comp.attribute(attr));
443 },
444 "names"_a, "attr"_a, "comp"_a);
445#endif
446
447 py::class_<CPS::IdentifiedObject, std::shared_ptr<CPS::IdentifiedObject>>(
448 m, "IdentifiedObject")
449 .def("name", &CPS::IdentifiedObject::name)
453 .def("attr", &CPS::IdentifiedObject::attribute, "name"_a)
454 .def("print_attribute_list", &printAttributes)
455 .def("print_attribute", &printAttribute, "attribute_name"_a)
456 .def("__str__", &getAttributeList);
457
458#ifdef WITH_CIM
459 py::enum_<CPS::CIM::Reader::VoltageTargetUnit>(m,
460 "CIMReaderVoltageTargetUnit")
461 .value("Auto", CPS::CIM::Reader::VoltageTargetUnit::Auto)
462 .value("PerUnit", CPS::CIM::Reader::VoltageTargetUnit::PerUnit)
463 .value("Absolute", CPS::CIM::Reader::VoltageTargetUnit::Absolute);
464
465 py::class_<CPS::CIM::Reader>(m, "CIMReader")
466 .def(py::init<std::string, CPS::Logger::Level, CPS::Logger::Level>(),
467 "name"_a, "loglevel"_a = CPS::Logger::Level::info,
468 "comploglevel"_a = CPS::Logger::Level::off)
469 .def("loadCIM", (CPS::SystemTopology(CPS::CIM::Reader::*)(
470 CPS::Real, const std::list<CPS::String> &,
471 CPS::Domain, CPS::PhaseType, CPS::GeneratorType)) &
473 .def("set_extnet_voltage_target_unit",
474 &CPS::CIM::Reader::setExtnetVoltageTargetUnit, "unit"_a);
475#endif
476
477 py::class_<CPS::CSVReader>(m, "CSVReader")
478 .def(py::init<std::string, const std::string &,
479 std::map<std::string, std::string> &, CPS::Logger::Level>())
480 .def("assignLoadProfile", &CPS::CSVReader::assignLoadProfile);
481
482 //Base Classes
483
484 py::class_<CPS::TopologicalPowerComp,
485 std::shared_ptr<CPS::TopologicalPowerComp>, CPS::IdentifiedObject>(
486 m, "TopologicalPowerComp");
487 py::class_<CPS::SimPowerComp<CPS::Complex>,
488 std::shared_ptr<CPS::SimPowerComp<CPS::Complex>>,
489 CPS::TopologicalPowerComp>(m, "SimPowerCompComplex")
491 .def("set_intf_current", &CPS::SimPowerComp<CPS::Complex>::setIntfCurrent)
492 .def("set_intf_voltage", &CPS::SimPowerComp<CPS::Complex>::setIntfVoltage)
493 .def("get_terminal", &CPS::SimPowerComp<CPS::Complex>::terminal,
494 "index"_a);
495 py::class_<CPS::SimPowerComp<CPS::Real>,
496 std::shared_ptr<CPS::SimPowerComp<CPS::Real>>,
497 CPS::TopologicalPowerComp>(m, "SimPowerCompReal")
499 .def("set_intf_current", &CPS::SimPowerComp<CPS::Real>::setIntfCurrent)
500 .def("set_intf_voltage", &CPS::SimPowerComp<CPS::Real>::setIntfVoltage)
501 .def("get_terminal", &CPS::SimPowerComp<CPS::Real>::terminal, "index"_a);
502 py::class_<CPS::TopologicalNode, std::shared_ptr<CPS::TopologicalNode>,
503 CPS::IdentifiedObject>(m, "TopologicalNode")
504 .def("initial_single_voltage",
505 &CPS::TopologicalNode::initialSingleVoltage,
506 "phase_type"_a = CPS::PhaseType::Single);
507
508 py::class_<CPS::TopologicalTerminal,
509 std::shared_ptr<CPS::TopologicalTerminal>, CPS::IdentifiedObject>(
510 m, "TopologicalTerminal")
511 .def("set_power",
512 py::overload_cast<CPS::Complex>(&CPS::TopologicalTerminal::setPower))
513 .def("set_power", py::overload_cast<CPS::MatrixComp>(
514 &CPS::TopologicalTerminal::setPower));
515
516 py::class_<CPS::SimTerminal<CPS::Complex>,
517 std::shared_ptr<CPS::SimTerminal<CPS::Complex>>,
518 CPS::TopologicalTerminal>(m, "SimTerminalComplex");
519 py::class_<CPS::SimTerminal<CPS::Real>,
520 std::shared_ptr<CPS::SimTerminal<CPS::Real>>,
521 CPS::TopologicalTerminal>(m, "SimTerminalReal");
522
523 //Events
524 py::module mEvent = m.def_submodule("event", "events");
525 py::class_<DPsim::Event, std::shared_ptr<DPsim::Event>>(mEvent, "Event");
526 py::class_<DPsim::SwitchEvent, std::shared_ptr<DPsim::SwitchEvent>,
527 DPsim::Event>(mEvent, "SwitchEvent", py::multiple_inheritance())
528 .def(py::init<CPS::Real, const std::shared_ptr<CPS::Base::Ph1::Switch>,
529 CPS::Bool>());
530 py::class_<DPsim::SwitchEvent3Ph, std::shared_ptr<DPsim::SwitchEvent3Ph>,
531 DPsim::Event>(mEvent, "SwitchEvent3Ph", py::multiple_inheritance())
532 .def(py::init<CPS::Real, const std::shared_ptr<CPS::Base::Ph3::Switch>,
533 CPS::Bool>());
534
535 //Components
536 py::module mBase = m.def_submodule("base", "base models");
537 addBaseComponents(mBase);
538
539 py::module mDP = m.def_submodule("dp", "dynamic phasor models");
540 addDPComponents(mDP);
541
542 py::module mEMT = m.def_submodule("emt", "electromagnetic-transient models");
543 addEMTComponents(mEMT);
544
545 py::module mSP = m.def_submodule("sp", "static phasor models");
546 mSP.attr("SimNode") = mDP.attr("SimNode");
547 addSPComponents(mSP);
548
549 py::module mSignal = m.def_submodule("signal", "signal models");
550 addSignalComponents(mSignal);
551
552#ifdef VERSION_INFO
553 m.attr("__version__") = VERSION_INFO;
554#else
555 m.attr("__version__") = "dev";
556#endif
557}
SystemTopology loadCIM(Real systemFrequency, const fs::path &filename, Domain domain=Domain::DP, PhaseType phase=PhaseType::Single, GeneratorType genType=GeneratorType::None)
Parses data from CIM files into the CPS data structure.
Definition Reader.cpp:249
void assignLoadProfile(SystemTopology &sys, Real start_time=-1, Real time_step=1, Real end_time=-1, CSVReader::Mode mode=CSVReader::Mode::AUTO, CSVReader::DataFormat format=CSVReader::DataFormat::SECONDS)
assign load profile to corresponding load object
AttributeBase::Ptr attribute(const String &name) const
Return pointer to an attribute.
static void setLogDir(String path)
Set env variable CPS_LOG_DIR and overwrite.
Definition Logger.cpp:88
static Matrix singlePhasePowerToThreePhase(Real power)
To convert single phase power to symmetrical three phase.
static Matrix singlePhaseParameterToThreePhase(Real parameter)
To convert single phase parameters to symmetrical three phase ones.
static MatrixComp singlePhaseVariableToThreePhase(Complex var_1ph)
To convert single phase complex variables (voltages, currents) to symmetrical three phase ones.
SimTerminal< VarType >::Ptr terminal(UInt index)
Get pointer to Terminal.
void connect(typename SimNode< VarType >::List nodes)
Sets all nodes and checks for nominal number of Nodes for this Component.
IdentifiedObject::List mComponents
List of network components.
std::shared_ptr< Type > node(UInt index)
Returns TopologicalNode by index in node list.
void initWithPowerflow(const SystemTopology &systemPF, CPS::Domain domain)
Initialize nodes and SG power from PowerFlow.
void removeComponent(const String &name)
Remove system component.
void addNode(TopologicalNode::Ptr topNode)
Adds node and initializes frequencies.
void addTearComponent(IdentifiedObject::Ptr component)
Adds component and initializes frequencies.
std::shared_ptr< Type > component(const String &name)
Returns Component by name.
TopologicalNode::List mNodes
List of network nodes.
void addComponents(const IdentifiedObject::List &components)
Add multiple components.
void connectComponentToNodes(typename SimPowerComp< VarType >::Ptr component, typename SimNode< VarType >::List simNodes)
Connect component to simNodes.
void addComponent(IdentifiedObject::Ptr component)
Adds component and initializes frequencies.
IdentifiedObject::List mTearComponents
std::map< TopologicalNode::Ptr, TopologicalPowerComp::List > mComponentsAtNode
Map of network components connected to network nodes.
Extending Simulation class by real-time functionality.
void run(const Timer::StartClock::duration &startIn=std::chrono::seconds(1))
void doFrequencyParallelization(Bool value)
Compute phasors of different frequencies in parallel.
Definition Simulation.h:237
void logLUTimes()
Write LU decomposition times measurements to log file.
Real next()
Run until next time step.
void addLogger(DataLoggerInterface::Ptr logger)
Add a new data logger.
Definition Simulation.h:307
void setSolverAndComponentBehaviour(Solver::Behaviour behaviour)
set solver and component to initialization or simulation behaviour
Definition Simulation.h:203
CPS::AttributeBase::Ptr getIdObjAttribute(const String &comp, const String &attr)
CHECK: Can these be deleted? getIdObjAttribute + "**attr =" should suffice.
void start()
Start simulation without advancing in time.
void doSteadyStateInit(Bool f)
activate steady state initialization
Definition Simulation.h:252
void addEvent(Event::Ptr e)
Schedule an event in the simulation.
Definition Simulation.h:305
void doStateSpaceExtraction(Bool value=true)
Enable extraction of the MNA-coupled discrete-time state matrix.
Definition Simulation.h:243
void run()
Run simulation until total time is elapsed.
void logAttribute(String name, CPS::AttributeBase::Ptr attr)
CHECK: Can we store the attribute name / UID intrinsically inside the attribute?
const MNAStateSpaceExtractor & getStateSpaceExtractor(UInt solverIndex=0) const
void stop()
Stop simulation including scheduler and interfaces.
void setAnalysisFrame(StateSpaceAnalysisFrame frame)
const std::vector< String > & getStateNames() const
const CPS::VectorComp & getContinuousEigenvalues() const
Continuous-time equivalent eigenvalues reconstructed from discrete eigenvalues.
const CPS::VectorComp & getDiscreteEigenvalues() const
Eigenvalues of the extracted discrete-time state matrix in the selected analysis frame.
void update()
Update modal quantities from the current extracted state matrix.
void setGlobalDq0Frame(Real omega, Real theta0=0.0)
const CPS::MatrixComp & getLeftEigenvectors() const
const CPS::MatrixComp & getRightEigenvectors() const
const CPS::MatrixComp & getParticipationFactors() const