Commit 9c9918b7 authored by Markus Mirz's avatar Markus Mirz
Browse files

pybind: add export and log attr to simulation and rt sim


Signed-off-by: Markus Mirz's avatarMarkus Mirz <mmirz@eonerc.rwth-aachen.de>
parent 21aa7d4e
...@@ -41,6 +41,8 @@ namespace DPsim { ...@@ -41,6 +41,8 @@ namespace DPsim {
void run(const Timer::StartClock::duration &startIn = std::chrono::seconds(1)); void run(const Timer::StartClock::duration &startIn = std::chrono::seconds(1));
void run(const Timer::StartClock::time_point &startAt); void run(const Timer::StartClock::time_point &startAt);
void run(Int startIn) { run(std::chrono::seconds(startIn)); }
}; };
} }
...@@ -251,11 +251,14 @@ namespace DPsim { ...@@ -251,11 +251,14 @@ namespace DPsim {
std::vector<Real>& stepTimes() { return mStepTimes; } std::vector<Real>& stepTimes() { return mStepTimes; }
// #### Set component attributes during simulation #### // #### Set component attributes during simulation ####
void setAttribute(const String &comp, const String &attr, Real value); void setIdObjAttr(const String &comp, const String &attr, Real value);
void setAttribute(const String &comp, const String &attr, Complex value); void setIdObjAttr(const String &comp, const String &attr, Complex value);
// #### Get component attributes during simulation #### // #### Get component attributes during simulation ####
Real getRealAttribute(const String &comp, const String &attr); Real getRealIdObjAttr(const String &comp, const String &attr);
Complex getComplexAttribute(const String &comp, const String &attr); Complex getComplexIdObjAttr(const String &comp, const String &attr);
void exportIdObjAttr(const String &comp, const String &attr, UInt idx, CPS::AttributeBase::Modifier mod = CPS::AttributeBase::Modifier::real, UInt row = 0, UInt col = 0);
void logIdObjAttr(const String &comp, const String &attr);
}; };
} }
...@@ -479,7 +479,7 @@ void Simulation::logStepTimes(String logName) { ...@@ -479,7 +479,7 @@ void Simulation::logStepTimes(String logName) {
mLog->info("Average step time: {:.6f}", stepTimeSum / mStepTimes.size()); mLog->info("Average step time: {:.6f}", stepTimeSum / mStepTimes.size());
} }
void Simulation::setAttribute(const String &comp, const String &attr, Real value) { void Simulation::setIdObjAttr(const String &comp, const String &attr, Real value) {
IdentifiedObject::Ptr compObj = mSystem.component<IdentifiedObject>(comp); IdentifiedObject::Ptr compObj = mSystem.component<IdentifiedObject>(comp);
if (compObj) { if (compObj) {
try { try {
...@@ -492,7 +492,7 @@ void Simulation::setAttribute(const String &comp, const String &attr, Real value ...@@ -492,7 +492,7 @@ void Simulation::setAttribute(const String &comp, const String &attr, Real value
mLog->error("Component not found"); mLog->error("Component not found");
} }
void Simulation::setAttribute(const String &comp, const String &attr, Complex value){ void Simulation::setIdObjAttr(const String &comp, const String &attr, Complex value){
IdentifiedObject::Ptr compObj = mSystem.component<IdentifiedObject>(comp); IdentifiedObject::Ptr compObj = mSystem.component<IdentifiedObject>(comp);
if (compObj) { if (compObj) {
try { try {
...@@ -505,7 +505,7 @@ void Simulation::setAttribute(const String &comp, const String &attr, Complex va ...@@ -505,7 +505,7 @@ void Simulation::setAttribute(const String &comp, const String &attr, Complex va
mLog->error("Component not found"); mLog->error("Component not found");
} }
Real Simulation::getRealAttribute(const String &comp, const String &attr) { Real Simulation::getRealIdObjAttr(const String &comp, const String &attr) {
IdentifiedObject::Ptr compObj = mSystem.component<IdentifiedObject>(comp); IdentifiedObject::Ptr compObj = mSystem.component<IdentifiedObject>(comp);
if (compObj) { if (compObj) {
try { try {
...@@ -516,10 +516,10 @@ Real Simulation::getRealAttribute(const String &comp, const String &attr) { ...@@ -516,10 +516,10 @@ Real Simulation::getRealAttribute(const String &comp, const String &attr) {
} }
mLog->error("Component not found"); mLog->error("Component not found");
return 10; return 0;
} }
Complex Simulation::getComplexAttribute(const String &comp, const String &attr){ Complex Simulation::getComplexIdObjAttr(const String &comp, const String &attr) {
IdentifiedObject::Ptr compObj = mSystem.component<IdentifiedObject>(comp); IdentifiedObject::Ptr compObj = mSystem.component<IdentifiedObject>(comp);
if (compObj) { if (compObj) {
try { try {
...@@ -530,5 +530,101 @@ Complex Simulation::getComplexAttribute(const String &comp, const String &attr){ ...@@ -530,5 +530,101 @@ Complex Simulation::getComplexAttribute(const String &comp, const String &attr){
} }
mLog->error("Component not found"); mLog->error("Component not found");
return 10; return 0;
}
void Simulation::exportIdObjAttr(const String &comp, const String &attr, UInt idx, AttributeBase::Modifier mod, UInt row, UInt col) {
Bool found = false;
IdentifiedObject::Ptr compObj = mSystem.component<IdentifiedObject>(comp);
if (!compObj) compObj = mSystem.node<TopologicalNode>(comp);
if (compObj) {
try {
auto v = compObj->attribute<Real>(attr);
mInterfaces[0].interface->exportReal(v, idx);
found = true;
} catch (InvalidAttributeException &e) { }
try {
auto v = compObj->attributeComplex(attr);
switch(mod) {
case AttributeBase::Modifier::real :
mInterfaces[0].interface->exportReal(v->real(), idx);
found = true;
break;
case AttributeBase::Modifier::imag :
mInterfaces[0].interface->exportReal(v->imag(), idx);
found = true;
break;
case AttributeBase::Modifier::mag :
mInterfaces[0].interface->exportReal(v->mag(), idx);
found = true;
break;
case AttributeBase::Modifier::phase :
mInterfaces[0].interface->exportReal(v->phase(), idx);
found = true;
break;
}
} catch (InvalidAttributeException &e) { }
try {
auto v = compObj->attributeMatrixReal(attr)->coeff(row, col);
found = true;
} catch (InvalidAttributeException &e) { }
try {
auto v = compObj->attributeMatrixComp(attr);
switch(mod) {
case AttributeBase::Modifier::real :
mInterfaces[0].interface->exportReal(v->coeffReal(row, col), idx);
found = true;
break;
case AttributeBase::Modifier::imag :
mInterfaces[0].interface->exportReal(v->coeffImag(row, col), idx);
found = true;
break;
case AttributeBase::Modifier::mag :
mInterfaces[0].interface->exportReal(v->coeffMag(row, col), idx);
found = true;
break;
case AttributeBase::Modifier::phase :
mInterfaces[0].interface->exportReal(v->coeffPhase(row, col), idx);
found = true;
break;
}
} catch (InvalidAttributeException &e) { }
if (!found) mLog->error("Attribute not found");
}
else {
mLog->error("Component not found");
}
}
void Simulation::logIdObjAttr(const String &comp, const String &attr) {
IdentifiedObject::Ptr compObj = mSystem.component<IdentifiedObject>(comp);
IdentifiedObject::Ptr nodeObj = mSystem.node<TopologicalNode>(comp);
if (compObj) {
try {
auto name = compObj->name() + "." + attr;
auto v = compObj->attribute(attr);
mLoggers[0]->addAttribute(name, v);
} catch (InvalidAttributeException &e) {
mLog->error("Attribute not found");
}
} else if (nodeObj) {
try {
auto name = nodeObj->name() + "." + attr;
auto v = nodeObj->attribute(attr);
mLoggers[0]->addAttribute(name, v);
} catch (InvalidAttributeException &e) {
mLog->error("Attribute not found");
}
}
else {
mLog->error("Component not found");
}
} }
...@@ -43,22 +43,32 @@ PYBIND11_MODULE(dpsimpy, m) { ...@@ -43,22 +43,32 @@ PYBIND11_MODULE(dpsimpy, m) {
.def("set_domain", &DPsim::Simulation::setDomain) .def("set_domain", &DPsim::Simulation::setDomain)
.def("start", &DPsim::Simulation::start) .def("start", &DPsim::Simulation::start)
.def("next", &DPsim::Simulation::next) .def("next", &DPsim::Simulation::next)
.def("set_attribute", static_cast<void (DPsim::Simulation::*)(const std::string&, const std::string&, CPS::Real)>(&DPsim::Simulation::setAttribute)) .def("set_idobj_attr", static_cast<void (DPsim::Simulation::*)(const std::string&, const std::string&, CPS::Real)>(&DPsim::Simulation::setIdObjAttr))
.def("set_attribute", static_cast<void (DPsim::Simulation::*)(const std::string&, const std::string&, CPS::Complex)>(&DPsim::Simulation::setAttribute)) .def("set_idobj_attr", static_cast<void (DPsim::Simulation::*)(const std::string&, const std::string&, CPS::Complex)>(&DPsim::Simulation::setIdObjAttr))
.def("get_real_attribute", &DPsim::Simulation::getRealAttribute) .def("get_real_idobj_attr", &DPsim::Simulation::getRealIdObjAttr)
.def("get_complex_attribute", &DPsim::Simulation::getComplexAttribute) .def("get_comp_idobj_attr", &DPsim::Simulation::getComplexIdObjAttr)
.def("add_interface", &DPsim::Simulation::addInterface, py::arg("interface"), py::arg("syncStart") = true); .def("add_interface", &DPsim::Simulation::addInterface, py::arg("interface"), py::arg("syncStart") = false)
.def("export_attr", &DPsim::Simulation::exportIdObjAttr, py::arg("obj"), py::arg("attr"), py::arg("idx"), py::arg("modifier"), py::arg("row") = 0, py::arg("col") = 0)
py::class_<DPsim::RealTimeSimulation>(m, "RealTimeSimulation") .def("log_attr", &DPsim::Simulation::logIdObjAttr);
py::class_<DPsim::RealTimeSimulation, DPsim::Simulation>(m, "RealTimeSimulation")
.def(py::init<std::string>()) .def(py::init<std::string>())
.def("name", &DPsim::Simulation::name) .def("name", &DPsim::RealTimeSimulation::name)
.def("set_time_step", &DPsim::Simulation::setTimeStep) .def("set_time_step", &DPsim::RealTimeSimulation::setTimeStep)
.def("set_final_time", &DPsim::Simulation::setFinalTime) .def("set_final_time", &DPsim::RealTimeSimulation::setFinalTime)
.def("add_logger", &DPsim::Simulation::addLogger) .def("add_logger", &DPsim::RealTimeSimulation::addLogger)
.def("set_system", &DPsim::Simulation::setSystem) .def("set_system", &DPsim::RealTimeSimulation::setSystem)
.def("run", &DPsim::Simulation::run) .def("run", static_cast<void (DPsim::RealTimeSimulation::*)(CPS::Int startIn)>(&DPsim::RealTimeSimulation::run))
.def("set_solver", &DPsim::Simulation::setSolverType) .def("set_solver", &DPsim::RealTimeSimulation::setSolverType)
.def("set_domain", &DPsim::Simulation::setDomain); .def("set_domain", &DPsim::RealTimeSimulation::setDomain)
.def("set_idobj_attr", static_cast<void (DPsim::RealTimeSimulation::*)(const std::string&, const std::string&, CPS::Real)>(&DPsim::Simulation::setIdObjAttr))
.def("set_idobj_attr", static_cast<void (DPsim::RealTimeSimulation::*)(const std::string&, const std::string&, CPS::Complex)>(&DPsim::Simulation::setIdObjAttr))
.def("get_real_idobj_attr", &DPsim::RealTimeSimulation::getRealIdObjAttr)
.def("get_comp_idobj_attr", &DPsim::RealTimeSimulation::getComplexIdObjAttr)
.def("add_interface", &DPsim::RealTimeSimulation::addInterface, py::arg("interface"), py::arg("syncStart") = false)
.def("export_attr", &DPsim::RealTimeSimulation::exportIdObjAttr, py::arg("obj"), py::arg("attr"), py::arg("idx"), py::arg("modifier"), py::arg("row") = 0, py::arg("col") = 0)
.def("log_attr", &DPsim::RealTimeSimulation::logIdObjAttr);
py::class_<CPS::SystemTopology, std::shared_ptr<CPS::SystemTopology>>(m, "SystemTopology") py::class_<CPS::SystemTopology, std::shared_ptr<CPS::SystemTopology>>(m, "SystemTopology")
.def(py::init<CPS::Real, CPS::TopologicalNode::List, CPS::IdentifiedObject::List>()) .def(py::init<CPS::Real, CPS::TopologicalNode::List, CPS::IdentifiedObject::List>())
...@@ -66,12 +76,13 @@ PYBIND11_MODULE(dpsimpy, m) { ...@@ -66,12 +76,13 @@ PYBIND11_MODULE(dpsimpy, m) {
.def("add", &DPsim::SystemTopology::addComponent) .def("add", &DPsim::SystemTopology::addComponent)
.def("_repr_svg_", &DPsim::SystemTopology::render) .def("_repr_svg_", &DPsim::SystemTopology::render)
.def("render_to_file", &DPsim::SystemTopology::renderToFile) .def("render_to_file", &DPsim::SystemTopology::renderToFile)
.def_readwrite("nodes", &DPsim::SystemTopology::mNodes); .def_readwrite("nodes", &DPsim::SystemTopology::mNodes)
.def("list_idobjects", &DPsim::SystemTopology::listIdObjects);
py::class_<DPsim::Interface>(m, "Interface"); py::class_<DPsim::Interface>(m, "Interface");
py::class_<DPsim::InterfaceShmem, DPsim::Interface>(m, "InterfaceShmem") py::class_<DPsim::InterfaceShmem, DPsim::Interface>(m, "InterfaceShmem")
.def(py::init<const CPS::String &, const CPS::String &>()); .def(py::init<const CPS::String&, const CPS::String&>(), py::arg("shmwrite") = "/dpsim-villas", py::arg("shmread") = "/villas-dpsim");
py::class_<DPsim::DataLogger, std::shared_ptr<DPsim::DataLogger>>(m, "Logger") py::class_<DPsim::DataLogger, std::shared_ptr<DPsim::DataLogger>>(m, "Logger")
.def(py::init<std::string>()) .def(py::init<std::string>())
...@@ -80,6 +91,12 @@ PYBIND11_MODULE(dpsimpy, m) { ...@@ -80,6 +91,12 @@ PYBIND11_MODULE(dpsimpy, m) {
py::class_<CPS::IdentifiedObject, std::shared_ptr<CPS::IdentifiedObject>>(m, "IdentifiedObject") py::class_<CPS::IdentifiedObject, std::shared_ptr<CPS::IdentifiedObject>>(m, "IdentifiedObject")
.def("name", &CPS::IdentifiedObject::name); .def("name", &CPS::IdentifiedObject::name);
py::enum_<CPS::AttributeBase::Modifier>(m, "AttrModifier")
.value("real", CPS::AttributeBase::Modifier::real)
.value("imag", CPS::AttributeBase::Modifier::imag)
.value("mag", CPS::AttributeBase::Modifier::mag)
.value("phase", CPS::AttributeBase::Modifier::phase);
py::enum_<CPS::Domain>(m, "Domain") py::enum_<CPS::Domain>(m, "Domain")
.value("SP", CPS::Domain::SP) .value("SP", CPS::Domain::SP)
.value("DP", CPS::Domain::DP) .value("DP", CPS::Domain::DP)
......
...@@ -68,6 +68,8 @@ namespace Flags { ...@@ -68,6 +68,8 @@ namespace Flags {
typedef std::vector<Ptr> List; typedef std::vector<Ptr> List;
typedef std::map<String, Ptr> Map; typedef std::map<String, Ptr> Map;
enum class Modifier { real, imag, mag, phase };
virtual String toString() const = 0; virtual String toString() const = 0;
int flags() const { int flags() const {
......
...@@ -132,17 +132,6 @@ namespace CPS { ...@@ -132,17 +132,6 @@ namespace CPS {
mComponents.push_back(component); mComponents.push_back(component);
} }
/// Adds component and initializes frequencies
void addresistor(std::shared_ptr<DP::Ph1::Resistor> component) {
auto powerCompComplex = std::dynamic_pointer_cast<SimPowerComp<Complex>>(component);
if (powerCompComplex) powerCompComplex->initialize(mFrequencies);
auto powerCompReal = std::dynamic_pointer_cast<SimPowerComp<Real>>(component);
if (powerCompReal) powerCompReal->initialize(mFrequencies);
mComponents.push_back(component);
}
/// Connect component to simNodes /// Connect component to simNodes
template <typename VarType> template <typename VarType>
void connectComponentToNodes(typename SimPowerComp<VarType>::Ptr component, typename SimNode<VarType>::List simNodes) { void connectComponentToNodes(typename SimPowerComp<VarType>::Ptr component, typename SimNode<VarType>::List simNodes) {
...@@ -228,6 +217,18 @@ namespace CPS { ...@@ -228,6 +217,18 @@ namespace CPS {
return nullptr; return nullptr;
} }
std::map<String, String> listIdObjects() {
std::map<String, String> objTypeMap;
for (auto node : mNodes) {
objTypeMap[node->name()] = node->type();
}
for (auto comp : mComponents) {
objTypeMap[comp->name()] = comp->type();
}
return objTypeMap;
}
// #### Operations on the SystemTopology #### // #### Operations on the SystemTopology ####
/// Copy the whole topology the given number of times and add the resulting components and nodes to the topology. /// Copy the whole topology the given number of times and add the resulting components and nodes to the topology.
......
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