Commit d06c9ccc authored by Jan Dinkelbach's avatar Jan Dinkelbach Committed by Markus Mirz
Browse files

add slack validation for sp1ph

parent 61a59f64
......@@ -21,7 +21,7 @@ set(CIRCUIT_SOURCES
Circuits/DP_Slack_PiLine_PQLoad_with_PF_Init.cpp
Circuits/DP_Slack_PiLine_VSI_with_PF_Init.cpp
# Combined EMT and DP examples
Circuits/EMT_DP_Slack.cpp
Circuits/EMT_DP_SP_Slack.cpp
Circuits/EMT_DP_VS_Init.cpp
Circuits/EMT_DP_VS_RLC_Circuits.cpp
......@@ -44,6 +44,7 @@ set(CIRCUIT_SOURCES
# SP examples
Circuits/SP_Circuits.cpp
Circuits/SP_PiLine.cpp
# EMT and DP studies
Circuits/DP_EMT_RL_SourceStep.cpp
......
......@@ -20,6 +20,82 @@ using namespace DPsim;
using namespace CPS;
using namespace CPS::CIM;
void simElementsSP1ph() {
Real timeStep = 0.00005;
Real finalTime = 1;
String simName = "SP_Slack_Elements";
Logger::setLogDir("logs/"+simName);
// Nodes
auto n1 = SimNode<Complex>::SimNode::make("n1");
// Components
auto vs = SP::Ph1::VoltageSource::make("v_1");
vs->setParameters(CPS::Math::polar(100000, 0));
auto load = SP::Ph1::Resistor::make("R_load");
load->setParameters(10000);
// Topology
vs->connect({ SimNode<Complex>::GND, n1 });
load->connect({ n1, SimNode<Complex>::GND });
auto sys = SystemTopology(50,
SystemNodeList{n1},
SystemComponentList{vs, load});
// Logging
auto logger = DataLogger::make(simName);
logger->addAttribute("v1", n1->attribute("v"));
logger->addAttribute("i1", vs->attribute("i_intf"));
Simulation sim(simName);
sim.setSystem(sys);
sim.setTimeStep(timeStep);
sim.setFinalTime(finalTime);
sim.addLogger(logger);
sim.run();
}
void simComponentSP1ph() {
Real timeStep = 0.00005;
Real finalTime = 1;
String simName = "SP_Slack_Component";
Logger::setLogDir("logs/"+simName);
// Nodes
auto n1 = SimNode<Complex>::make("n1");
// Components
auto sl = SP::Ph1::NetworkInjection::make("v_1");
sl->setParameters(CPS::Math::polar(100000, 0));
auto load = SP::Ph1::Resistor::make("R_load");
load->setParameters(10000);
// Topology
sl->connect({ n1 });
load->connect({ n1, SimNode<Complex>::GND });
auto sys = SystemTopology(50,
SystemNodeList{n1},
SystemComponentList{sl, load});
// Logging
auto logger = DataLogger::make(simName);
logger->addAttribute("v1", n1->attribute("v"));
logger->addAttribute("i1", sl->attribute("i_intf"));
Simulation sim(simName);
sim.setSystem(sys);
sim.setTimeStep(timeStep);
sim.setFinalTime(finalTime);
sim.addLogger(logger);
sim.run();
}
void simElementsDP1ph() {
Real timeStep = 0.00005;
Real finalTime = 1;
......@@ -175,6 +251,9 @@ void simComponentEMT3ph() {
}
int main(int argc, char* argv[]) {
simElementsSP1ph();
simComponentSP1ph();
simElementsDP1ph();
simComponentDP1ph();
......
......@@ -8,11 +8,117 @@
import villas.dataprocessing.readtools as rt
from villas.dataprocessing.timeseries import TimeSeries as ts
import matplotlib.pyplot as plt
import numpy as np
#%matplotlib widget
# %matplotlib widget
epsilon = 1e-12
```
%% Cell type:markdown id: tags:
## SP Slack with voltage source element
%% Cell type:code id: tags:
``` python
%%bash
TOP=${TOP:-$(git rev-parse --show-toplevel)}
PATH=${TOP}/build/Examples/Cxx
EMT_DP_SP_Slack
```
%% Cell type:code id: tags:
``` python
work_dir = 'logs/SP_Slack_Elements/'
log_name = 'SP_Slack_Elements'
print(work_dir + log_name + '.csv')
slack_elements = rt.read_timeseries_dpsim(work_dir + log_name + '.csv')
slack_elements_sp_shifted = ts.frequency_shift_list(slack_elements, 50)
```
%% Cell type:code id: tags:
``` python
plt.figure()
plt.plot(slack_elements_sp_shifted['v1_shift'].time, slack_elements_sp_shifted['v1_shift'].values, label='v1_shift')
plt.legend()
```
%% Cell type:code id: tags:
``` python
plt.figure()
plt.plot(slack_elements_sp_shifted['i1_shift'].time, slack_elements_sp_shifted['i1_shift'].values, label='i1_shift')
plt.legend()
```
%% Cell type:markdown id: tags:
## SP Slack composite model
%% Cell type:code id: tags:
``` python
work_dir = 'logs/SP_Slack_Component/'
log_name = 'SP_Slack_Component'
print(work_dir + log_name + '.csv')
slack_component = rt.read_timeseries_dpsim(work_dir + log_name + '.csv')
slack_component_sp_shifted = ts.frequency_shift_list(slack_component, 50)
```
%% Cell type:code id: tags:
``` python
plt.figure()
plt.plot(slack_component_sp_shifted['v1_shift'].time, slack_component_sp_shifted['v1_shift'].values, label='v1_shift')
plt.legend()
```
%% Cell type:code id: tags:
``` python
plt.figure()
plt.plot(slack_component_sp_shifted['i1_shift'].time, slack_component_sp_shifted['i1_shift'].values, label='i1_shift')
plt.legend()
```
%% Cell type:markdown id: tags:
## Error for SP Slack
%% Cell type:code id: tags:
``` python
plt.figure()
plt.plot(slack_component_sp_shifted['v1_shift'].time, slack_elements_sp_shifted['v1_shift'].values - slack_component_sp_shifted['v1_shift'].values, label='v1_shift_error')
plt.legend()
```
%% Cell type:code id: tags:
``` python
plt.figure()
plt.plot(slack_component_sp_shifted['i1_shift'].time, slack_elements_sp_shifted['i1_shift'].values - slack_component_sp_shifted['i1_shift'].values, label='i1_shift_error')
plt.legend()
```
%% Cell type:markdown id: tags:
## Assertion for SP Slack
%% Cell type:code id: tags:
``` python
errors_sp_shifted = []
for name in ['v1_shift', 'i1_shift']:
errors_sp_shifted.append(np.absolute(slack_elements_sp_shifted[name].values - slack_component_sp_shifted[name].values).max())
print(name + ': ' + str(errors_sp_shifted[-1]))
assert np.max(errors_sp_shifted) < epsilon
```
%% Cell type:markdown id: tags:
## DP Slack with voltage source element
......@@ -22,11 +128,11 @@
``` python
%%bash
TOP=${TOP:-$(git rev-parse --show-toplevel)}
PATH=${TOP}/build/Examples/Cxx
EMT_DP_Slack
EMT_DP_SP_Slack
```
%% Cell type:code id: tags:
``` python
......@@ -221,10 +327,50 @@
assert np.max(errors_emt) < 1e-3
```
%% Cell type:markdown id: tags:
### Comparison SP vs. DP
%% Cell type:code id: tags:
``` python
plt.figure()
for name in [('v1_shift', 'v1_shift')]:
plt.plot(slack_component_sp_shifted[name[0]].time, slack_component_sp_shifted[name[0]].values - slack_component_dp_shifted[name[1]].values, label=name[0]+' (SP) vs. '+name[1]+' (DP)')
plt.legend()
```
%% Cell type:code id: tags:
``` python
plt.figure()
for name in [('i1_shift', 'i1_shift')]:
plt.plot(slack_component_sp_shifted[name[0]].time, slack_component_sp_shifted[name[0]].values - slack_component_dp_shifted[name[1]].values, label=name[0]+' (SP) vs. '+name[1]+' (DP)')
plt.legend()
```
%% Cell type:markdown id: tags:
### Assertion SP vs. DP
%% Cell type:code id: tags:
``` python
compare_errors_abs = []
compare_errors_rel = []
for name in [('v1_shift', 'v1_shift'), ('i1_shift', 'i1_shift')]:
compare_errors_abs.append(np.absolute(slack_component_sp_shifted[name[0]].values - slack_component_dp_shifted[name[1]].values).max())
compare_errors_rel.append(np.absolute(slack_component_sp_shifted[name[0]].values - slack_component_dp_shifted[name[1]].values).max()/slack_component_dp_shifted[name[1]].values.max())
print(name[0]+' vs. '+name[1] + ' (abs): ' + str(compare_errors_abs[-1]))
print(name[0]+' vs. '+name[1] + ' (rel): ' + str(compare_errors_rel[-1]))
print('Max rel error: '+ '{:.2}'.format(np.max(compare_errors_rel)*100) +'%')
assert np.max(compare_errors_rel) < epsilon
```
%% Cell type:markdown id: tags:
### Comparison DP vs. EMT
%% Cell type:code id: tags:
``` python
......
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