Commit 0e61bf84 authored by Markus Mirz's avatar Markus Mirz

pybind: extend interface and circuit example

parent 8095f9be
......@@ -15,8 +15,43 @@
"metadata": {},
"outputs": [],
"source": [
"sim = dpsimpy.Simulation('test')\n",
"sim.name()"
"name = 'DP_CS_R2CL'\n",
"\n",
"# Nodes\n",
"gnd = dpsimpy.dp.SimNode.gnd\n",
"n1 = dpsimpy.dp.SimNode('n1')\n",
"n2 = dpsimpy.dp.SimNode('n2')\n",
"\n",
"# Components\n",
"cs = dpsimpy.dp.ph1.CurrentSource('cs')\n",
"cs.set_parameters(complex(10,0))\n",
"r1 = dpsimpy.dp.ph1.Resistor('r_1');\n",
"r1.set_parameters(1);\n",
"c1 = dpsimpy.dp.ph1.Capacitor('c_1');\n",
"c1.set_parameters(0.001);\n",
"l1 = dpsimpy.dp.ph1.Inductor('l_1');\n",
"l1.set_parameters(0.001);\n",
"r2 = dpsimpy.dp.ph1.Resistor('r_2');\n",
"r2.set_parameters(1);\n",
"\n",
"# Connections\n",
"cs.connect([gnd, n1])\n",
"r1.connect([n1, gnd])\n",
"c1.connect([n1, n2]);\n",
"l1.connect([n2, gnd]);\n",
"r2.connect([n2, gnd]);\n",
"\n",
"# Define system topology\n",
"system = dpsimpy.SystemTopology(50, [gnd, n1, n2], [cs, r1, c1, l1, r2]);\n",
"\n",
"logger = dpsimpy.Logger(name)\n",
"logger.log_attribute('v1', 'v', n1);\n",
"logger.log_attribute('v2', 'v', n1);\n",
"\n",
"sim = dpsimpy.Simulation(name)\n",
"sim.set_system(system)\n",
"sim.set_time_step(0.0001)\n",
"sim.set_final_time(0.1)"
]
},
{
......@@ -25,18 +60,7 @@
"metadata": {},
"outputs": [],
"source": [
"node = dpsimpy.dp.SimNode('dpnode')\n",
"node.name()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gndnode = dpsimpy.dp.SimNode.gnd\n",
"gndnode.name()"
"sim.run()"
]
},
{
......
......@@ -63,6 +63,7 @@ namespace DPsim {
void addAttribute(const String &name, CPS::Attribute<Complex>::Ptr attr);
void addAttribute(const String &name, CPS::MatrixRealAttribute::Ptr attr);
void addAttribute(const String &name, CPS::MatrixCompAttribute::Ptr attr, UInt rowsMax = 0, UInt colsMax = 0);
void addAttribute(const String &name, const String &attr, CPS::IdentifiedObject::Ptr obj);
template<typename VarType>
void addNode(typename CPS::SimNode<VarType>::Ptr node) {
......
......@@ -160,6 +160,10 @@ void DataLogger::addAttribute(const String &name, CPS::Attribute<Real>::Ptr attr
mAttributes[name] = attr;
}
void DataLogger::addAttribute(const String &name, const String &attr, CPS::IdentifiedObject::Ptr obj) {
mAttributes[name] = obj->attribute(attr);
}
void DataLogger::addAttribute(const String &name, CPS::Attribute<Complex>::Ptr attr) {
auto attrComp = std::static_pointer_cast<CPS::ComplexAttribute>(attr);
......
......@@ -7,7 +7,12 @@
*********************************************************************************/
#include <pybind11/pybind11.h>
#include <pybind11/complex.h>
#include <pybind11/stl.h>
#include <dpsim/Simulation.h>
#include <cps/IdentifiedObject.h>
#include <DPsim.h>
namespace py = pybind11;
......@@ -22,20 +27,57 @@ PYBIND11_MODULE(dpsimpy, m) {
py::class_<DPsim::Simulation>(m, "Simulation")
.def(py::init<std::string>())
.def("name", &DPsim::Simulation::name);
.def("name", &DPsim::Simulation::name)
.def("set_time_step", &DPsim::Simulation::setTimeStep)
.def("set_final_time", &DPsim::Simulation::setFinalTime)
.def("add_logger", &DPsim::Simulation::addLogger)
.def("set_system", &DPsim::Simulation::setSystem)
.def("run", &DPsim::Simulation::run);
py::class_<CPS::SystemTopology, std::shared_ptr<CPS::SystemTopology>>(m, "SystemTopology")
.def(py::init<CPS::Real, CPS::TopologicalNode::List, CPS::IdentifiedObject::List>());
py::class_<DPsim::DataLogger, std::shared_ptr<DPsim::DataLogger>>(m, "Logger")
.def(py::init<std::string>())
.def("log_attribute", (void (DPsim::DataLogger::*)(const CPS::String &, const CPS::String &, CPS::IdentifiedObject::Ptr)) &DPsim::DataLogger::addAttribute);
py::class_<CPS::IdentifiedObject, std::shared_ptr<CPS::IdentifiedObject>>(m, "IdentifiedObject");
py::class_<CPS::TopologicalNode, CPS::IdentifiedObject, std::shared_ptr<CPS::TopologicalNode>>(m, "TopologicalNode");
py::module mDP = m.def_submodule("dp", "dynamic phasor models");
py::module mDPPh1 = mDP.def_submodule("ph1", "single phase dynamic phasor models");
py::module mEMT = m.def_submodule("emt", "electromagnetic-transient models");
py::module mEMTPh1 = mEMT.def_submodule("ph1", "single phase electromagnetic-transient models");
py::class_<CPS::DP::SimNode, std::shared_ptr<CPS::DP::SimNode>>(mDP, "SimNode")
py::class_<CPS::DP::SimNode, CPS::TopologicalNode, std::shared_ptr<CPS::DP::SimNode>>(mDP, "SimNode")
.def(py::init<std::string>())
.def_readonly_static("gnd", &CPS::DP::SimNode::GND)
.def("name", &CPS::DP::SimNode::name);
py::class_<CPS::EMT::SimNode, std::shared_ptr<CPS::EMT::SimNode>>(mEMT, "SimNode")
py::class_<CPS::EMT::SimNode, CPS::TopologicalNode, std::shared_ptr<CPS::EMT::SimNode>>(mEMT, "SimNode")
.def(py::init<std::string>())
.def("name", &CPS::EMT::SimNode::name);
py::class_<CPS::DP::Ph1::CurrentSource, CPS::IdentifiedObject, std::shared_ptr<CPS::DP::Ph1::CurrentSource>>(mDPPh1, "CurrentSource")
.def(py::init<std::string>())
.def("set_parameters", &CPS::DP::Ph1::CurrentSource::setParameters)
.def("connect", &CPS::DP::Ph1::CurrentSource::connect);
py::class_<CPS::DP::Ph1::Resistor, CPS::IdentifiedObject, std::shared_ptr<CPS::DP::Ph1::Resistor>>(mDPPh1, "Resistor")
.def(py::init<std::string>())
.def("set_parameters", &CPS::DP::Ph1::Resistor::setParameters)
.def("connect", &CPS::DP::Ph1::Resistor::connect);
py::class_<CPS::DP::Ph1::Capacitor, CPS::IdentifiedObject, std::shared_ptr<CPS::DP::Ph1::Capacitor>>(mDPPh1, "Capacitor")
.def(py::init<std::string>())
.def("set_parameters", &CPS::DP::Ph1::Capacitor::setParameters)
.def("connect", &CPS::DP::Ph1::Capacitor::connect);
py::class_<CPS::DP::Ph1::Inductor, CPS::IdentifiedObject, std::shared_ptr<CPS::DP::Ph1::Inductor>>(mDPPh1, "Inductor")
.def(py::init<std::string>())
.def("set_parameters", &CPS::DP::Ph1::Inductor::setParameters)
.def("connect", &CPS::DP::Ph1::Inductor::connect);
#ifdef VERSION_INFO
m.attr("__version__") = VERSION_INFO;
#else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment