Commit cbd2c837 authored by Markus Mirz's avatar Markus Mirz
Browse files

initialize frequencies in SystemTopology

parent 8fddab10
Subproject commit 2f2f246aee96a2f213a1ccceec3d392a3f0a06a0
Subproject commit c8daca4e5f2fbcbe4ca49c0917e975deacf7a43e
......@@ -46,7 +46,7 @@ int main(int argc, char* argv[]) {
auto n4 = Node::make("n4");
auto n5 = Node::make("n5");
Logger::Level level = Logger::Level::NONE;
Logger::Level level = Logger::Level::INFO;
// Components
auto inv = Inverter::make("inv", level);
......@@ -86,14 +86,15 @@ int main(int argc, char* argv[]) {
SystemNodeList{ n1, n2, n3, n4, n5 },
SystemComponentList{ inv, r1, l1, r2, l2, c1, rc, grid });
//Simulation sim(simName, sys, timeStep, finalTime, Domain::DP, Solver::Type::MNA, Logger::Level::INFO);
Simulation sim(simName, level);
sim.setSystem(sys);
sim.setTimeStep(timeStep);
sim.setFinalTime(finalTime);
sim.doHarmonicParallelization(false);
sim.initialize();
// Logging
auto logger = DataLogger::make(simName);
logger->addAttribute("v1", n1->attributeMatrixComp("v"), 1, 5);
......@@ -108,6 +109,7 @@ int main(int argc, char* argv[]) {
sim.run();
auto spdStepTimeLog = Logger::get("step_times", Logger::Level::INFO);
Logger::setLogPattern(spdStepTimeLog, "%v");
spdStepTimeLog->info("steptime_inv");
Real tot = 0;
......
%% Cell type:markdown id: tags:
# Inverter Grid Test
%% Cell type:code id: tags:
``` python
%%bash
TOP=${TOP:-$(git rev-parse --show-toplevel)}
PATH=${TOP}/build/Examples/Cxx
DP_Inverter_Grid_Test
```
%% Output
Scheduling tasks.
Start simulation.
Simulation finished.
0.000300565
0.000395416
%% Cell type:code id: tags:
``` python
import villas.dataprocessing.readtools as rt
import villas.dataprocessing.plottools as pt
from villas.dataprocessing.timeseries import TimeSeries
import matplotlib.pyplot as plt
```
%% Cell type:code id: tags:
``` python
# read results
work_dir = 'logs/DP_Inverter_Grid_Test/'
log_name = 'DP_Inverter_Grid_Test'
print(work_dir + log_name + '.csv')
ts_dpsim = rt.read_timeseries_dpsim(work_dir + log_name + '.csv')
```
%% Output
logs/DP_Inverter_Grid_Test/DP_Inverter_Grid_Test.csv
column number: 15
results length: 50000
real column names: []
complex column names: ['i12_0_0', 'i34_0_0', 'v1_0_0', 'v1_0_1', 'v1_0_2', 'v1_0_3', 'v1_0_4', 'v2_0_0', 'v3_0_0', 'v3_0_1', 'v3_0_2', 'v3_0_3', 'v3_0_4', 'v4_0_0', 'v5_0_0']
%% Cell type:code id: tags:
``` python
ts_list_v1 = [
ts_dpsim['v1_0_0'],
ts_dpsim['v1_0_1'],
ts_dpsim['v1_0_2'],
ts_dpsim['v1_0_3'],
ts_dpsim['v1_0_4']]
ts_list_v3 = [
ts_dpsim['v3_0_0'],
ts_dpsim['v3_0_1'],
ts_dpsim['v3_0_2'],
ts_dpsim['v3_0_3'],
ts_dpsim['v3_0_4']]
freq_list = [
50,
19850,
19950,
20050,
20150]
ts_dpsim_emt_v1 = TimeSeries.create_emt_from_dp(ts_list_v1, freq_list)
ts_dpsim_emt_v1.values
```
%% Output
array([ 0. , -0.07476615, -0.14131796, ..., -0.19165711,
-0.14131796, -0.07476615])
%% Cell type:code id: tags:
``` python
#%matplotlib qt
pt.plot_timeseries(1, ts_dpsim_emt_v1)
plt.xlim(0, 0.05)
```
%% Output
(0, 0.05)
%% Cell type:code id: tags:
``` python
ts_dpsim_emt_v3 = TimeSeries.create_emt_from_dp(ts_list_v3, freq_list)
pt.plot_timeseries(1, ts_dpsim_emt_v3)
plt.xlim(0, 0.05)
```
%% Output
(0, 0.05)
%% Cell type:code id: tags:
``` python
ts_dpsim_emt_v5 = ts_dpsim['v5_0_0'].frequency_shift(50)
#%matplotlib qt
pt.plot_timeseries(1, ts_dpsim_emt_v1)
pt.plot_timeseries(1, ts_dpsim_emt_v3)
pt.plot_timeseries(1, ts_dpsim_emt_v5)
plt.xlim(0, 0.05)
```
%% Output
(0, 0.05)
%% Cell type:code id: tags:
``` python
ts_dpsim_emt_v1 = ts_dpsim['v1_0_0'].frequency_shift(50)
pt.plot_timeseries(1, ts_dpsim_emt_v1)
plt.xlim(0, 0.05)
```
%% Output
(0, 0.05)
%% Cell type:markdown id: tags:
## Validation
%% 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/Inverter/ML_SP_Inverter_OL_v_i.csv'
local_file = 'reference-results/ML_SP_Inverter_OL_v_i.csv'
urllib.request.urlretrieve(url, local_file)
ts_ml_inverter = rt.read_timeseries_simulink(local_file)
```
%% Output
column number: 4
results length: 100001
real column names: ['v_inv', 'v_c', 'i1', 'i2']
complex column names: []
%% Cell type:code id: tags:
``` python
pt.plot_timeseries(1, ts_ml_inverter['v_inv'])
```
%% Output
%% Cell type:code id: tags:
``` python
#plt.figure(figsize=(20,10))
pt.plot_timeseries(1, ts_ml_inverter['v_c'])
pt.plot_timeseries(1, ts_dpsim_emt_v3)
plt.xlim(0.024, 0.026)
plt.ylim(300, 320)
```
%% Output
(300, 320)
%% Cell type:code id: tags:
``` python
import numpy as np
diff = ts_ml_inverter['v_c'].values[:5000] - np.interp(ts_ml_inverter['v_c'].time[:5000], ts_dpsim_emt_v3.time, ts_dpsim_emt_v3.values)
assert np.all(diff) < 10
```
%% 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/Inverter/SL_SP_Inverter_OL_v_i.csv'
local_file = 'reference-results/SL_SP_Inverter_OL_v_i.csv'
urllib.request.urlretrieve(url, local_file)
ts_sl_inverter = rt.read_timeseries_simulink(local_file)
```
%% Output
column number: 4
results length: 712639
real column names: ['v_inv', 'v_c', 'i1', 'i2']
complex column names: []
%% Cell type:code id: tags:
``` python
pt.plot_timeseries(1, ts_sl_inverter['v_inv'])
```
%% Output
%% Cell type:code id: tags:
``` python
#plt.figure(figsize=(20,10))
pt.plot_timeseries(1, ts_ml_inverter['v_c'])
pt.plot_timeseries(1, ts_sl_inverter['v_c'])
pt.plot_timeseries(1, ts_dpsim_emt_v3)
plt.xlim(0.024, 0.026)
plt.ylim(300, 320)
```
%% Output
(300, 320)
%% Cell type:code id: tags:
``` python
import numpy as np
ts_sl_inverter['v_c'].label = 'vc_sl'
ts_dpsim_emt_v3.label = 'vc_dpsim'
pt.plot_timeseries(1, ts_sl_inverter['v_c'])
pt.plot_timeseries(1, ts_dpsim_emt_v3)
plt.xlim(0.0245, 0.0255)
plt.ylim(306, 315)
plt.xlabel('time (s)')
plt.ylabel('voltage (V)')
ax = plt.gca()
ax.xaxis.set_major_locator(plt.MaxNLocator(5))
```
%% Output
%% Cell type:code id: tags:
``` python
```
......
......@@ -110,19 +110,17 @@ void MnaSolver<VarType>::initialize(CPS::SystemTopology system) {
template <>
void MnaSolver<Real>::initializeComponents() {
// TODO: Move to base solver class?
// This intialization according to power flow information is not MNA specific.
mSLog->info("-- Initialize components from power flow");
for (auto comp : mPowerComponents) {
auto pComp = std::dynamic_pointer_cast<PowerComponent<Complex>>(comp);
if (!pComp) continue;
pComp->initialize(mSystem.mFrequencies);
auto pComp = std::dynamic_pointer_cast<PowerComponent<Real>>(comp);
if (!pComp) continue;
pComp->initializeFromPowerflow(mSystem.mSystemFrequency);
}
// Initialize signal components.
for (auto comp : mSignalComponents)
comp->initialize(mSystem.mSystemOmega, mTimeStep);
// Initialize MNA specific parts of components.
for (auto comp : mPowerComponents) {
comp->mnaInitialize(mSystem.mSystemOmega, mTimeStep, attribute<Matrix>("left_vector"));
......@@ -137,19 +135,12 @@ void MnaSolver<Real>::initializeComponents() {
template <>
void MnaSolver<Complex>::initializeComponents() {
// TODO: Move to base solver class?
// This intialization according to power flow information is not MNA specific.
mSLog->info("-- Initialize components from power flow");
// Initialize nodes
for (UInt nodeIdx = 0; nodeIdx < mNodes.size(); nodeIdx++)
mNodes[nodeIdx]->initialize(mSystem.mFrequencies);
// Initialize power components with frequencies and from powerflow results
for (auto comp : mPowerComponents) {
auto pComp = std::dynamic_pointer_cast<PowerComponent<Complex>>(comp);
if (!pComp) continue;
pComp->initialize(mSystem.mFrequencies);
pComp->initializeFromPowerflow(mSystem.mSystemFrequency);
}
......
......@@ -56,6 +56,8 @@ Simulation::Simulation(String name, Logger::Level logLevel) :
mSLog = Logger::get(name);
mSLog->set_pattern("[%L] %v");
mSLog->set_level(Logger::cpsLogLevelToSpd(logLevel));
mInitialized = false;
}
Simulation::Simulation(String name, SystemTopology system,
......
Supports Markdown
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