Skip to content

Commit

Permalink
implement print function for attribute lists
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Schroeder <jonas.schroeder1@rwth-aachen.de>
  • Loading branch information
JTS22 committed Jul 29, 2021
1 parent 88a5254 commit 6806cc3
Show file tree
Hide file tree
Showing 4 changed files with 332 additions and 1 deletion.
283 changes: 283 additions & 0 deletions Examples/Notebooks/Circuits/CS_R2CL print_attributes.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Circuit CS_R2CL"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import villas.dataprocessing.readtools as rt\n",
"import villas.dataprocessing.plottools as pt\n",
"from villas.dataprocessing.timeseries import TimeSeries as ts"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## DPsim EMT"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# DPsim EMT simulation\n",
"import dpsimpy\n",
"\n",
"name = 'EMT_CS_R2CL'\n",
"\n",
"# Nodes\n",
"gnd = dpsimpy.emt.SimNode.gnd\n",
"n1 = dpsimpy.emt.SimNode('n1')\n",
"n2 = dpsimpy.emt.SimNode('n2')\n",
"\n",
"# Components\n",
"cs = dpsimpy.emt.ph1.CurrentSource('cs')\n",
"cs.I_ref = complex(10, 0)\n",
"cs.f_src = 50\n",
"r1 = dpsimpy.emt.ph1.Resistor('r_1')\n",
"r1.R = 1\n",
"c1 = dpsimpy.emt.ph1.Capacitor('c_1')\n",
"c1.C = 0.001\n",
"l1 = dpsimpy.emt.ph1.Inductor('l_1')\n",
"l1.L = 0.001\n",
"r2 = dpsimpy.emt.ph1.Resistor('r_2')\n",
"r2.R = 1\n",
"\n",
"cs.print_attributes()\n",
"r1.print_attributes()\n",
"c1.print_attributes()\n",
"l1.print_attributes()\n",
"r2.print_attributes()\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",
"# Logging\n",
"logger = dpsimpy.Logger(name)\n",
"logger.log_attribute('n1.v', 'v', n1)\n",
"logger.log_attribute('n2.v', 'v', n2)\n",
"logger.log_attribute('cs.i_intf', 'i_intf', cs)\n",
"logger.log_attribute('c_1.i_intf', 'i_intf', c1)\n",
"\n",
"sim = dpsimpy.Simulation(name)\n",
"sim.set_domain(dpsimpy.Domain.EMT)\n",
"sim.set_system(system)\n",
"sim.set_time_step(0.0001)\n",
"sim.set_final_time(0.1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sim.add_logger(logger)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sim.run()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# read EMT results\n",
"work_dir = 'logs/'\n",
"log_name = 'EMT_CS_R2CL'\n",
"print(work_dir + log_name + '.csv')\n",
"\n",
"ts_dpsim_emt = rt.read_timeseries_dpsim(work_dir + log_name + '.csv')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"v1_emt = 'n1.v'\n",
"v2_emt = 'n2.v'\n",
"i01_emt = 'cs.i_intf'\n",
"i12_emt = 'c_1.i_intf'\n",
"\n",
"ts_dpsim_emt[v1_emt].label = 'v1 EMT'\n",
"ts_dpsim_emt[v2_emt].label = 'v2 EMT'\n",
"ts_dpsim_emt[i01_emt].label = 'i01 EMT'\n",
"ts_dpsim_emt[i12_emt].label = 'i12 EMT'\n",
"pt.plot_timeseries(1, ts_dpsim_emt[v1_emt])\n",
"pt.plot_timeseries(1, ts_dpsim_emt[v2_emt])\n",
"pt.plot_timeseries(2, ts_dpsim_emt[i01_emt])\n",
"pt.plot_timeseries(2, ts_dpsim_emt[i12_emt])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## DPsim DP"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# DPsim DP simulation\n",
"import dpsimpy\n",
"\n",
"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.I_ref = complex(10,0)\n",
"r1 = dpsimpy.dp.ph1.Resistor('r_1')\n",
"r1.R = 1\n",
"c1 = dpsimpy.dp.ph1.Capacitor('c_1')\n",
"c1.C = 0.001\n",
"l1 = dpsimpy.dp.ph1.Inductor('l_1')\n",
"l1.L = 0.001\n",
"r2 = dpsimpy.dp.ph1.Resistor('r_2')\n",
"r2.R = 1\n",
"\n",
"cs.print_attributes()\n",
"r1.print_attributes()\n",
"c1.print_attributes()\n",
"l1.print_attributes()\n",
"r2.print_attributes()\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",
"# Logging\n",
"logger = dpsimpy.Logger(name)\n",
"logger.log_attribute('n1.v', 'v', n1)\n",
"logger.log_attribute('n2.v', 'v', n2)\n",
"logger.log_attribute('cs.i_intf', 'i_intf', cs)\n",
"logger.log_attribute('c_1.i_intf', 'i_intf', c1)\n",
"\n",
"sim = dpsimpy.Simulation(name)\n",
"sim.set_system(system)\n",
"sim.set_domain(dpsimpy.Domain.DP)\n",
"sim.set_time_step(0.0001)\n",
"sim.set_final_time(0.1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sim.add_logger(logger)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sim.run()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# read DP results\n",
"work_dir = 'logs/'\n",
"log_name = 'DP_CS_R2CL'\n",
"print(work_dir + log_name + '.csv')\n",
"\n",
"ts_dpsim_dp = rt.read_timeseries_dpsim(work_dir + log_name + '.csv')\n",
"\n",
"#convert to emt\n",
"ts_dpsim_dp_emt = ts.frequency_shift_list(ts_dpsim_dp, 50)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"v1_dp = 'n1.v_shift'\n",
"v2_dp = 'n2.v_shift'\n",
"i01_dp = 'cs.i_intf_shift'\n",
"i12_dp = 'c_1.i_intf_shift'\n",
"\n",
"ts_dpsim_dp_emt[v1_dp].label = 'v1 DP'\n",
"ts_dpsim_dp_emt[v2_dp].label = 'v2 DP'\n",
"ts_dpsim_dp_emt[i01_dp].label = 'i01 DP'\n",
"ts_dpsim_dp_emt[i12_dp].label = 'i12 DP'\n",
"pt.plot_timeseries(1, ts_dpsim_dp_emt[v1_dp])\n",
"pt.plot_timeseries(1, ts_dpsim_dp_emt[v2_dp])\n",
"pt.plot_timeseries(2, ts_dpsim_dp_emt[i01_dp])\n",
"pt.plot_timeseries(2, ts_dpsim_dp_emt[i12_dp])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.7"
},
"orig_nbformat": 3
},
"nbformat": 4,
"nbformat_minor": 4
}
3 changes: 3 additions & 0 deletions Include/dpsim/pybind/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*********************************************************************************/
#pragma once

#include <iomanip>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/functional.h>
Expand All @@ -30,3 +31,5 @@ py::cpp_function createAttributeGetter(const std::string name) {
}

CPS::Matrix zeroMatrix(int dim);

void printAttributes(CPS::IdentifiedObject &obj);
41 changes: 41 additions & 0 deletions Source/pybind/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,44 @@
CPS::Matrix zeroMatrix(int dim) {
return CPS::Matrix::Zero(dim, dim);
}

void printAttributes(CPS::IdentifiedObject &obj) {
std::stringstream output;
output << std::setiosflags(std::ios::left);
output << "Attribute list for object with name " << obj.name() << ":" << std::endl;
output << std::setw(30) << "name" << std::setw(20) << "type" << std::setw(15) << "size" << std::setw(30) << "value" << std::endl;
output << std::setw(95) << std::setfill('-') << "" << std::setfill(' ') << std::endl;
for (auto attr : obj.attributes()) {
std::string name = attr.first;
std::string type;
std::string value;
std::string size;
if (auto tryReal = std::dynamic_pointer_cast<CPS::Attribute<CPS::Real>>(attr.second)) {
type = "Real";
value = std::to_string(tryReal->get());
size = "";
} else if (auto tryComplex = std::dynamic_pointer_cast<CPS::Attribute<CPS::Complex>>(attr.second)) {
type = "Complex";
value = std::to_string(abs(tryComplex->get())) + "<" + std::to_string(arg(tryComplex->get()));
size = "";
} else if (auto tryMatrixReal = std::dynamic_pointer_cast<CPS::Attribute<CPS::Matrix>>(attr.second)) {
type = "MatrixReal";
value = "[...]";
size = std::to_string(tryMatrixReal->get().rows()) + "x" + std::to_string(tryMatrixReal->get().cols());
} else if (auto tryMatrixComp = std::dynamic_pointer_cast<CPS::Attribute<CPS::MatrixComp>>(attr.second)) {
type = "MatrixComplex";
value = "[...]";
size = std::to_string(tryMatrixComp->get().rows()) + "x" + std::to_string(tryMatrixComp->get().cols());
} else if (auto tryString = std::dynamic_pointer_cast<CPS::Attribute<CPS::String>>(attr.second)) {
type = "String";
value = "\"" + tryString->get() + "\"";
size = "";
} else {
type = "Unknown";
value = "Unknown";
size = "Unknown";
}
output << std::setw(30) << name << std::setw(20) << type << std::setw(15) << size << std::setw(30) << value << std::endl;
}
py::print(output.str());
}
6 changes: 5 additions & 1 deletion Source/pybind/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*********************************************************************************/

#include <iomanip>

#include <pybind11/pybind11.h>
#include <pybind11/complex.h>
#include <pybind11/stl.h>
#include <pybind11/functional.h>
#include <pybind11/eigen.h>
#include <pybind11/iostream.h>

#include <dpsim/Simulation.h>
#include <dpsim/RealTimeSimulation.h>
Expand Down Expand Up @@ -124,7 +127,8 @@ PYBIND11_MODULE(dpsimpy, m) {
.def("log_attribute", (void (DPsim::DataLogger::*)(const std::vector<CPS::String> &, const CPS::String &, CPS::IdentifiedObject::Ptr)) &DPsim::DataLogger::addAttribute);

py::class_<CPS::IdentifiedObject, std::shared_ptr<CPS::IdentifiedObject>>(m, "IdentifiedObject")
.def("name", &CPS::IdentifiedObject::name);
.def("name", &CPS::IdentifiedObject::name)
.def("print_attributes", &printAttributes);

py::enum_<CPS::AttributeBase::Modifier>(m, "AttrModifier")
.value("real", CPS::AttributeBase::Modifier::real)
Expand Down

0 comments on commit 6806cc3

Please sign in to comment.