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>
24namespace py = pybind11;
25using namespace pybind11::literals;
27PYBIND11_MODULE(dpsimpy, m) {
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.
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);
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",
54 py::enum_<DPsim::Solver::Behaviour>(m,
"SolverBehaviour")
55 .value(
"Initialization", DPsim::Solver::Behaviour::Initialization)
56 .value(
"Simulation", DPsim::Solver::Behaviour::Simulation);
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);
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);
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);
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);
89 py::enum_<DPsim::Solver::Type>(m,
"Solver")
90 .value(
"MNA", DPsim::Solver::Type::MNA)
91 .value(
"DAE", DPsim::Solver::Type::DAE)
92 .value(
"NRP", DPsim::Solver::Type::NRP);
94 py::enum_<DPsim::DirectLinearSolverImpl>(m,
"DirectLinearSolverImpl")
95 .value(
"Undef", DPsim::DirectLinearSolverImpl::Undef)
96 .value(
"DenseLU", DPsim::DirectLinearSolverImpl::DenseLU)
97 .value(
"SparseLU", DPsim::DirectLinearSolverImpl::SparseLU)
98 .value(
"KLU", DPsim::DirectLinearSolverImpl::KLU)
99 .value(
"CUDADense", DPsim::DirectLinearSolverImpl::CUDADense)
100 .value(
"CUDASparse", DPsim::DirectLinearSolverImpl::CUDASparse)
101 .value(
"CUDAMagma", DPsim::DirectLinearSolverImpl::CUDAMagma);
103 py::enum_<DPsim::SCALING_METHOD>(m,
"scaling_method")
104 .value(
"no_scaling", DPsim::SCALING_METHOD::NO_SCALING)
105 .value(
"sum_scaling", DPsim::SCALING_METHOD::SUM_SCALING)
106 .value(
"max_scaling", DPsim::SCALING_METHOD::MAX_SCALING);
108 py::enum_<DPsim::FILL_IN_REDUCTION_METHOD>(m,
"fill_in_reduction_method")
109 .value(
"amd", DPsim::FILL_IN_REDUCTION_METHOD::AMD)
110 .value(
"amd_nv", DPsim::FILL_IN_REDUCTION_METHOD::AMD_NV)
111 .value(
"amd_ra", DPsim::FILL_IN_REDUCTION_METHOD::AMD_RA)
112 .value(
"colamd", DPsim::FILL_IN_REDUCTION_METHOD::COLAMD);
114 py::enum_<DPsim::PARTIAL_REFACTORIZATION_METHOD>(
115 m,
"partial_refactorization_method")
116 .value(
"no_partial_refactorization",
117 DPsim::PARTIAL_REFACTORIZATION_METHOD::NO_PARTIAL_REFACTORIZATION)
118 .value(
"factorization_path",
119 DPsim::PARTIAL_REFACTORIZATION_METHOD::FACTORIZATION_PATH)
120 .value(
"refactorization_restart",
121 DPsim::PARTIAL_REFACTORIZATION_METHOD::REFACTORIZATION_RESTART);
123 py::enum_<DPsim::USE_BTF>(m,
"use_btf")
124 .value(
"no_btf", DPsim::USE_BTF::NO_BTF)
125 .value(
"do_btf", DPsim::USE_BTF::DO_BTF);
127 py::enum_<CPS::CSVReader::Mode>(m,
"CSVReaderMode")
128 .value(
"AUTO", CPS::CSVReader::Mode::AUTO)
129 .value(
"MANUAL", CPS::CSVReader::Mode::MANUAL);
131 py::enum_<CPS::CSVReader::DataFormat>(m,
"CSVReaderFormat")
132 .value(
"HHMMSS", CPS::CSVReader::DataFormat::HHMMSS)
133 .value(
"SECONDS", CPS::CSVReader::DataFormat::SECONDS)
134 .value(
"HOURS", CPS::CSVReader::DataFormat::HOURS)
135 .value(
"MINUTES", CPS::CSVReader::DataFormat::MINUTES);
137 m.attr(
"RMS3PH_TO_PEAK1PH") = RMS3PH_TO_PEAK1PH;
138 m.attr(
"PEAK1PH_TO_RMS3PH") = PEAK1PH_TO_RMS3PH;
139 m.attr(
"P_SNUB_TRANSFORMER") = P_SNUB_TRANSFORMER;
140 m.attr(
"Q_SNUB_TRANSFORMER") = Q_SNUB_TRANSFORMER;
144 py::class_<DPsim::DirectLinearSolverConfiguration>(
145 m,
"DirectLinearSolverConfiguration")
147 .def(
"set_fill_in_reduction_method",
148 &DPsim::DirectLinearSolverConfiguration::setFillInReductionMethod)
149 .def(
"set_scaling_method",
150 &DPsim::DirectLinearSolverConfiguration::setScalingMethod)
151 .def(
"set_partial_refactorization_method",
153 setPartialRefactorizationMethod)
154 .def(
"set_btf", &DPsim::DirectLinearSolverConfiguration::setBTF)
155 .def(
"get_scaling_method",
156 &DPsim::DirectLinearSolverConfiguration::getScalingMethod)
157 .def(
"get_fill_in_reduction_method",
158 &DPsim::DirectLinearSolverConfiguration::getFillInReductionMethod)
159 .def(
"get_partial_refactorization_method",
161 getPartialRefactorizationMethod)
162 .def(
"get_btf", &DPsim::DirectLinearSolverConfiguration::getBTF);
164 py::class_<DPsim::Simulation>(m,
"Simulation")
165 .def(py::init<std::string, CPS::Logger::Level>(),
"name"_a,
166 "loglevel"_a = CPS::Logger::Level::off)
167 .def(
"name", &DPsim::Simulation::name)
168 .def(
"set_time_step", &DPsim::Simulation::setTimeStep)
169 .def(
"set_final_time", &DPsim::Simulation::setFinalTime)
171 .def(
"set_system", &DPsim::Simulation::setSystem)
173 .def(
"set_solver", &DPsim::Simulation::setSolverType)
174 .def(
"set_domain", &DPsim::Simulation::setDomain)
180 .def(
"add_interface", &DPsim::Simulation::addInterface,
"interface"_a)
181 .def(
"log_idobj_attribute", &DPsim::Simulation::logIdObjAttribute,
185 .def(
"do_init_from_nodes_and_terminals",
186 &DPsim::Simulation::doInitFromNodesAndTerminals)
187 .def(
"do_system_matrix_recomputation",
188 &DPsim::Simulation::doSystemMatrixRecomputation)
190 .def(
"do_frequency_parallelization",
192 .def(
"do_split_subnets", &DPsim::Simulation::doSplitSubnets)
193 .def(
"set_tearing_components", &DPsim::Simulation::setTearingComponents)
195 .def(
"set_solver_component_behaviour",
197 .def(
"set_direct_solver_implementation",
198 &DPsim::Simulation::setDirectLinearSolverImplementation)
199 .def(
"set_direct_linear_solver_configuration",
200 &DPsim::Simulation::setDirectLinearSolverConfiguration)
204 py::class_<DPsim::RealTimeSimulation, DPsim::Simulation>(m,
205 "RealTimeSimulation")
206 .def(py::init<std::string, CPS::Logger::Level>(),
"name"_a,
207 "loglevel"_a = CPS::Logger::Level::info)
208 .def(
"name", &DPsim::RealTimeSimulation::name)
209 .def(
"set_time_step", &DPsim::RealTimeSimulation::setTimeStep)
210 .def(
"set_final_time", &DPsim::RealTimeSimulation::setFinalTime)
212 .def(
"set_system", &DPsim::RealTimeSimulation::setSystem)
216 .def(
"set_solver", &DPsim::RealTimeSimulation::setSolverType)
217 .def(
"set_domain", &DPsim::RealTimeSimulation::setDomain);
220 py::class_<CPS::SystemTopology, std::shared_ptr<CPS::SystemTopology>>(
222 .def(py::init<CPS::Real, CPS::TopologicalNode::List,
223 CPS::IdentifiedObject::List>())
224 .def(py::init<CPS::Real, CPS::Matrix, CPS::TopologicalNode::List,
225 CPS::IdentifiedObject::List>())
226 .def(py::init<CPS::Real>())
229 .def(
"node", py::overload_cast<std::string_view>(
231 .def(
"node", py::overload_cast<CPS::UInt>(
233 .def(
"connect_component",
234 py::overload_cast<CPS::SimPowerComp<CPS::Real>::Ptr,
235 CPS::SimNode<CPS::Real>::List>(
237 .def(
"connect_component",
238 py::overload_cast<CPS::SimPowerComp<CPS::Complex>::Ptr,
239 CPS::SimNode<CPS::Complex>::List>(
245 .def(
"_repr_svg_", &DPsim::SystemTopology::render)
246 .def(
"render_to_file", &DPsim::SystemTopology::renderToFile)
250 .def_readwrite(
"components_at_node",
253 .def(
"list_idobjects", &DPsim::SystemTopology::listIdObjects)
255 "systemPF"_a,
"domain"_a)
260 py::class_<DPsim::Interface, std::shared_ptr<DPsim::Interface>>(m,
264 std::shared_ptr<DPsim::DataLoggerInterface>>(m,
265 "DataLoggerInterface")
266 .def(
"log_attribute",
267 py::overload_cast<
const CPS::String &, CPS::AttributeBase::Ptr,
268 CPS::UInt, CPS::UInt>(
269 &DPsim::DataLoggerInterface::logAttribute),
270 "name"_a,
"attr"_a,
"max_cols"_a = 0,
"max_rows"_a = 0)
272 .def(
"log_attribute",
273 py::overload_cast<
const std::vector<CPS::String> &,
274 CPS::AttributeBase::Ptr>(
275 &DPsim::DataLoggerInterface::logAttribute),
278 .def(
"log_attribute",
280 const std::vector<CPS::String> &names,
const CPS::String &attr,
282 logger.logAttribute(names, comp.
attribute(attr));
286 std::shared_ptr<DPsim::DataLogger>>(m,
"Logger")
287 .def(py::init<std::string>())
289 .def_static(
"get_log_dir", &CPS::Logger::logDir)
290 .def(
"log_attribute",
291 py::overload_cast<
const CPS::String &, CPS::AttributeBase::Ptr,
292 CPS::UInt, CPS::UInt>(
293 &DPsim::DataLogger::logAttribute),
294 "name"_a,
"attr"_a,
"max_cols"_a = 0,
"max_rows"_a = 0)
296 .def(
"log_attribute",
297 py::overload_cast<
const std::vector<CPS::String> &,
298 CPS::AttributeBase::Ptr>(
299 &DPsim::DataLogger::logAttribute),
306 CPS::UInt rowsMax, CPS::UInt colsMax) {
307 logger.logAttribute(name, comp.
attribute(attr), rowsMax, colsMax);
309 "name"_a,
"attr"_a,
"comp"_a,
"rows_max"_a = 0,
"cols_max"_a = 0)
311 .def(
"log_attribute",
314 logger.logAttribute(names, comp.
attribute(attr));
318 std::shared_ptr<DPsim::RealTimeDataLogger>>(m,
319 "RealTimeDataLogger")
321 .def(py::init([](py::object filename, DPsim::Real final_time,
322 DPsim::Real time_step) {
324 py::module_::import(
"os").attr(
"fspath")(filename);
325 std::string s = py::cast<std::string>(fspath);
326 std::filesystem::path p(s);
327 return std::make_shared<DPsim::RealTimeDataLogger>(p, final_time,
330 "filename"_a,
"final_time"_a,
"time_step"_a)
332 .def(py::init([](py::object filename, std::size_t row_number) {
334 py::module_::import(
"os").attr(
"fspath")(filename);
335 std::string s = py::cast<std::string>(fspath);
336 std::filesystem::path p(s);
337 return std::make_shared<DPsim::RealTimeDataLogger>(p, row_number);
339 "filename"_a,
"row_number"_a)
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),
357 CPS::UInt rowsMax, CPS::UInt colsMax) {
358 logger.logAttribute(name, comp.
attribute(attr), rowsMax, colsMax);
360 "name"_a,
"attr"_a,
"comp"_a,
"rows_max"_a = 0,
"cols_max"_a = 0)
365 const std::vector<CPS::String> &names,
const CPS::String &attr,
367 logger.logAttribute(names, comp.
attribute(attr));
369 "names"_a,
"attr"_a,
"comp"_a);
372 py::class_<CPS::IdentifiedObject, std::shared_ptr<CPS::IdentifiedObject>>(
373 m,
"IdentifiedObject")
374 .def(
"name", &CPS::IdentifiedObject::name)
379 .def(
"print_attribute_list", &printAttributes)
380 .def(
"print_attribute", &printAttribute,
"attribute_name"_a)
381 .def(
"__str__", &getAttributeList);
384 py::class_<CPS::CIM::Reader>(m,
"CIMReader")
385 .def(py::init<std::string, CPS::Logger::Level, CPS::Logger::Level>(),
386 "name"_a,
"loglevel"_a = CPS::Logger::Level::info,
387 "comploglevel"_a = CPS::Logger::Level::off)
389 CPS::Real,
const std::list<CPS::String> &,
390 CPS::Domain, CPS::PhaseType, CPS::GeneratorType)) &
394 py::class_<CPS::CSVReader>(m,
"CSVReader")
395 .def(py::init<std::string,
const std::string &,
396 std::map<std::string, std::string> &, CPS::Logger::Level>())
403 m,
"TopologicalPowerComp");
404 py::class_<CPS::SimPowerComp<CPS::Complex>,
405 std::shared_ptr<CPS::SimPowerComp<CPS::Complex>>,
408 .def(
"set_intf_current", &CPS::SimPowerComp<CPS::Complex>::setIntfCurrent)
409 .def(
"set_intf_voltage", &CPS::SimPowerComp<CPS::Complex>::setIntfVoltage)
412 py::class_<CPS::SimPowerComp<CPS::Real>,
413 std::shared_ptr<CPS::SimPowerComp<CPS::Real>>,
416 .def(
"set_intf_current", &CPS::SimPowerComp<CPS::Real>::setIntfCurrent)
417 .def(
"set_intf_voltage", &CPS::SimPowerComp<CPS::Real>::setIntfVoltage)
419 py::class_<CPS::TopologicalNode, std::shared_ptr<CPS::TopologicalNode>,
421 .def(
"initial_single_voltage",
422 &CPS::TopologicalNode::initialSingleVoltage,
423 "phase_type"_a = CPS::PhaseType::Single);
427 m,
"TopologicalTerminal")
429 py::overload_cast<CPS::Complex>(&CPS::TopologicalTerminal::setPower))
430 .def(
"set_power", py::overload_cast<CPS::MatrixComp>(
431 &CPS::TopologicalTerminal::setPower));
433 py::class_<CPS::SimTerminal<CPS::Complex>,
434 std::shared_ptr<CPS::SimTerminal<CPS::Complex>>,
436 py::class_<CPS::SimTerminal<CPS::Real>,
437 std::shared_ptr<CPS::SimTerminal<CPS::Real>>,
441 py::module mEvent = m.def_submodule(
"event",
"events");
442 py::class_<DPsim::Event, std::shared_ptr<DPsim::Event>>(mEvent,
"Event");
443 py::class_<DPsim::SwitchEvent, std::shared_ptr<DPsim::SwitchEvent>,
444 DPsim::Event>(mEvent,
"SwitchEvent", py::multiple_inheritance())
445 .def(py::init<CPS::Real,
const std::shared_ptr<CPS::Base::Ph1::Switch>,
447 py::class_<DPsim::SwitchEvent3Ph, std::shared_ptr<DPsim::SwitchEvent3Ph>,
448 DPsim::Event>(mEvent,
"SwitchEvent3Ph", py::multiple_inheritance())
449 .def(py::init<CPS::Real,
const std::shared_ptr<CPS::Base::Ph3::Switch>,
453 py::module mBase = m.def_submodule(
"base",
"base models");
454 addBaseComponents(mBase);
456 py::module mDP = m.def_submodule(
"dp",
"dynamic phasor models");
457 addDPComponents(mDP);
459 py::module mEMT = m.def_submodule(
"emt",
"electromagnetic-transient models");
460 addEMTComponents(mEMT);
462 py::module mSP = m.def_submodule(
"sp",
"static phasor models");
463 mSP.attr(
"SimNode") = mDP.attr(
"SimNode");
464 addSPComponents(mSP);
466 py::module mSignal = m.def_submodule(
"signal",
"signal models");
467 addSignalComponents(mSignal);
470 m.attr(
"__version__") = VERSION_INFO;
472 m.attr(
"__version__") =
"dev";
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.
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.
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 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.
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.
void setSolverAndComponentBehaviour(Solver::Behaviour behaviour)
set solver and component to initialization or simulation behaviour
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
void addEvent(Event::Ptr e)
Schedule an event in the simulation.
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?
void stop()
Stop simulation including scheduler and interfaces.