Commit 4f7c2759 authored by Markus Mirz's avatar Markus Mirz
Browse files

add pybind interface for basic circuits and powerflow

parent 531847a8
......@@ -95,15 +95,16 @@ find_package(OpenMP)
find_package(CUDA)
find_package(GSL)
find_package(Graphviz)
find_package(PythonInterp 3.6)
find_package(PythonLibs 3.6)
find_package(NumPy)
find_package(VILLASnode)
if(PythonInterp_FOUND AND PythonLibs_FOUND)
set(Python_FOUND ON)
if(NOT PYBIND)
find_package(PythonInterp 3.6)
find_package(PythonLibs 3.6)
find_package(NumPy)
if(PythonInterp_FOUND AND PythonLibs_FOUND)
set(Python_FOUND ON)
endif()
endif()
if("${CMAKE_SYSTEM}" MATCHES "Linux")
......@@ -116,6 +117,11 @@ option(BUILD_SHARED_LIBS "Build shared library" OFF)
option(BUILD_EXAMPLES "Build C++ examples" ON)
option(GET_GRID_DATA "Download grid data" ON)
option(PYBIND "Build pybind module" OFF)
if(PYBIND)
set(BUILD_SHARED_LIBS ON)
endif()
include(CMakeDependentOption)
cmake_dependent_option(WITH_GSL "Enable GSL" ON "GSL_FOUND" OFF)
cmake_dependent_option(WITH_SUNDIALS "Enable sundials solver suite" ON "Sundials_FOUND" OFF)
......@@ -176,9 +182,17 @@ if(BUILD_EXAMPLES)
add_subdirectory(Examples)
endif(BUILD_EXAMPLES)
#add_subdirectory(Dependencies/pybind11)
#pybind11_add_module(dpsimpy Source/pybind/main.cpp)
#target_link_libraries(dpsimpy PRIVATE dpsim)
if(PYBIND)
if (WITH_PYBIND_SUBMODULE OR WIN32)
include(cmake/GetPybindSubmodule.cmake)
else()
find_package(pybind11)
endif()
set(PYBIND11_CPP_STANDARD -std=c++11)
pybind11_add_module(dpsimpy Source/pybind/main.cpp)
target_link_libraries(dpsimpy PRIVATE dpsim)
endif()
include(cmake/GetGridData.cmake)
......
%% Cell type:markdown id: tags:
# Circuit CS_R2CL
%% Cell type:code id: tags:
``` python
import villas.dataprocessing.readtools as rt
from villas.dataprocessing.timeseries import TimeSeries as ts
```
%% Cell type:markdown id: tags:
## DPsim EMT
%% Cell type:code id: tags:
``` python
# DPsim EMT simulation
import dpsimpy
name = 'EMT_CS_R2CL'
# Nodes
gnd = dpsim.emt.Node.GND()
n1 = dpsim.emt.Node('n1')
n2 = dpsim.emt.Node('n2')
# Components
cs = dpsim.emt.ph1.CurrentSource('cs')
cs.I_ref = complex(10,0)
cs.f_src = 50
r1 = dpsim.emt.ph1.Resistor('r_1');
r1.R = 1;
c1 = dpsim.emt.ph1.Capacitor('c_1');
c1.C = 0.001;
l1 = dpsim.emt.ph1.Inductor('l_1');
l1.L = 0.001;
r2 = dpsim.emt.ph1.Resistor('r_2');
r2.R = 1;
# Connections
cs.connect([gnd, n1])
r1.connect([n1, gnd])
c1.connect([n1, n2]);
l1.connect([n2, gnd]);
r2.connect([n2, gnd]);
# Define system topology
system = dpsim.SystemTopology(50, [gnd, n1, n2], [cs, r1, c1, l1, r2]);
# Logging
logger = dpsim.Logger(name)
logger.log_attribute(n1, 'v');
logger.log_attribute(n2, 'v');
logger.log_attribute(cs, 'i_intf');
logger.log_attribute(c1, 'i_intf');
sim = dpsim.Simulation(name, system, timestep=0.0001, duration=0.1, pbar=True, sim_type=1)
sim.add_logger(logger)
await sim.simulate()
```
%% Cell type:code id: tags:
``` python
# read EMT results
work_dir = 'logs/'
log_name = 'EMT_CS_R2CL'
print(work_dir + log_name + '.csv')
ts_dpsim_emt = rt.read_timeseries_dpsim(work_dir + log_name + '.csv')
```
%% Cell type:code id: tags:
``` python
v1_emt = 'n1.v'
v2_emt = 'n2.v'
i01_emt = 'cs.i_intf'
i12_emt = 'c_1.i_intf'
ts_dpsim_emt[v1_emt].label = 'v1 EMT'
ts_dpsim_emt[v2_emt].label = 'v2 EMT'
ts_dpsim_emt[i01_emt].label = 'i01 EMT'
ts_dpsim_emt[i12_emt].label = 'i12 EMT'
pt.plot_timeseries(1, ts_dpsim_emt[v1_emt])
pt.plot_timeseries(1, ts_dpsim_emt[v2_emt])
pt.plot_timeseries(2, ts_dpsim_emt[i01_emt])
pt.plot_timeseries(2, ts_dpsim_emt[i12_emt])
```
%% Cell type:markdown id: tags:
## DPsim DP
%% Cell type:code id: tags:
``` python
# DPsim DP simulation
import dpsimpy
name = 'DP_CS_R2CL'
# Nodes
gnd = dpsimpy.dp.SimNode.gnd
n1 = dpsimpy.dp.SimNode('n1')
n2 = dpsimpy.dp.SimNode('n2')
# Components
cs = dpsimpy.dp.ph1.CurrentSource('cs')
cs.set_parameters(complex(10,0))
r1 = dpsimpy.dp.ph1.Resistor('r_1');
r1.set_parameters(1);
c1 = dpsimpy.dp.ph1.Capacitor('c_1');
c1.set_parameters(0.001);
l1 = dpsimpy.dp.ph1.Inductor('l_1');
l1.set_parameters(0.001);
r2 = dpsimpy.dp.ph1.Resistor('r_2');
r2.set_parameters(1);
# Connections
cs.connect([gnd, n1])
r1.connect([n1, gnd])
c1.connect([n1, n2]);
l1.connect([n2, gnd]);
r2.connect([n2, gnd]);
# Define system topology
system = dpsimpy.SystemTopology(50, [gnd, n1, n2], [cs, r1, c1, l1, r2]);
# Logging
logger = dpsimpy.Logger(name)
logger.log_attribute('v1', 'v', n1);
logger.log_attribute('v2', 'v', n1);
#logger.log_attribute(n1, 'v');
#logger.log_attribute(n2, 'v');
#logger.log_attribute(cs, 'i_intf');
#logger.log_attribute(c1, 'i_intf');
sim = dpsimpy.Simulation(name)
sim.set_system(system)
sim.set_time_step(0.0001)
sim.set_final_time(0.1)
```
%% Cell type:code id: tags:
``` python
sim.add_logger(logger)
```
%% Cell type:code id: tags:
``` python
sim.run()
```
%% Cell type:code id: tags:
``` python
# read DP results
work_dir = 'logs/'
log_name = 'DP_CS_R2CL'
print(work_dir + log_name + '.csv')
ts_dpsim_dp = rt.read_timeseries_dpsim(work_dir + log_name + '.csv')
#convert to emt
ts_dpsim_dp_emt = ts.frequency_shift_list(ts_dpsim_dp, 50)
```
%% Cell type:code id: tags:
``` python
v1_dp = 'n1.v_shift'
v2_dp = 'n2.v_shift'
i01_dp = 'cs.i_intf_shift'
i12_dp = 'c_1.i_intf_shift'
ts_dpsim_dp_emt[v1_dp].label = 'v1 DP'
ts_dpsim_dp_emt[v2_dp].label = 'v2 DP'
ts_dpsim_dp_emt[i01_dp].label = 'i01 DP'
ts_dpsim_dp_emt[i12_dp].label = 'i12 DP'
pt.plot_timeseries(1, ts_dpsim_dp_emt[v1_dp])
pt.plot_timeseries(1, ts_dpsim_dp_emt[v2_dp])
pt.plot_timeseries(2, ts_dpsim_dp_emt[i01_dp])
pt.plot_timeseries(2, ts_dpsim_dp_emt[i12_dp])
```
%% Cell type:markdown id: tags:
## Simulink reference results
%% Cell type:code id: tags:
``` python
# read Simulink log file
import os
import urllib.request
if not os.path.exists('reference-results'):
os.mkdir('reference-results')
url = 'https://git.rwth-aachen.de/acs/public/simulation/reference-results/raw/master/Simulink/Circuits/SL_CS_R2CL.csv'
local_file = 'reference-results/SL_CS_R2CL.csv'
urllib.request.urlretrieve(url, local_file)
ts_sl = rt.read_timeseries_simulink(local_file)
```
%% Cell type:code id: tags:
``` python
v1_sl = 'v1'
v2_sl = 'v2'