Skip to content
Snippets Groups Projects
Commit 2da72cdb authored by Jan Dinkelbach's avatar Jan Dinkelbach
Browse files

minor update on lecture 2 notebooks

parent cbf058c5
No related branches found
No related tags found
1 merge request!1Jdi gna
%% Cell type:markdown id: tags:
# Modeling and Simulation of Complex Power Systems
## Simulation Examples
The following notebooks are currently available:
Lecture Examples:
- [Lecture 2 - Modified Nodal Analysis - with DPsim solver](./lectures/02_NA_MNA/VS_CS_R4.ipynb)
- [Lecture 2 - Modified Nodal Analysis - without DPsim solver](./lectures/02_NA_MNA/VS_CS_R4_hard-coded.ipynb)
- [Lecture 2 - Modified Nodal Analysis - without DPsim](./lectures/02_NA_MNA/VS_CS_R4_hard-coded.ipynb)
- [Lecture 2 - Modified Nodal Analysis - with DPsim](./lectures/02_NA_MNA/VS_CS_R4.ipynb)
- [Lecture 3 - Resistive Companion](./lectures/03_ResistiveCompanion/VS_R2L3.ipynb)
- [Lecture 4 - Nonlinear Resistive Companion](./lectures/04_NLResistiveCompanion/NL_RC.ipynb)
- [Lecture 5 - State Space Equations](./lectures/05_StateSpace/StateEq_ASMG.ipynb)
- [Lecture 8 - Dynamic Phasors](./lectures/08_DecoupledELMESim/VS_RL1.ipynb)
- [Lecture 9 - Diakoptics](./lectures/09_Diakoptics/Diakoptics.ipynb)
- [Lecture 9 - LIM](./lectures/09_LIM/LIM.ipynb)
- [Lecture 11 - Uncertain System Analysis](./lectures/11_UncertainSystemAnalysis/UncertainSystemAnalysis.ipynb)
Exercise Examples:
- [Exercice 3 - ResistiveCompanion -Task 1](./exercises/03_ResistiveCompanion/CS_R1L1.ipynb)
- [Exercise 6 - Diakoptics - Extra Task](./exercises/06_Diakoptics_ExtraTask/Diakoptics_ExtraTask.ipynb)
- [Exercise 7 - LIM - Extra Task](./exercises/07_LIM_ExtraTask/LIM_Exercise_ExtraTask.ipynb)
- [Exercise 8 - Numerical Integration](./exercises/08_NumInt/NumInt_example.ipynb)
## Helping Material
### Notebooks
Learn how to work with the notebooks by accessing the extensive information under **Help** in the navigation bar.
As a first starting point you might refer to **Help>Notebook Reference**, where you can find **Notebook Examples** in the user documentation. Important basics are covered in the following sections:
- Notebook Basics
- Running Code
- Markdown Cells
### Python
If you need an introduction to the Python language, the **Beginners Guide** under **Help>Python Reference** links to lots of tutorials depending on your previous knowledge.
### IPython
As a more advanced user, you can benefit from the fact that Jupyter Notebooks run an **IPython** kernel, which comes with additional features with respect to the standard **Python** kernel. To learn more about these features, you can follow under **Help>IPython Reference** the link **IPython documentation** and have a look into **Tutorial>Introducing IPython**.
### Contact
Any questions can be adressed to
[acs-teaching-mscps@eonerc.rwth-aachen.de](mailto:acs-teaching-mscps@eonerc.rwth-aachen.de)
%% Cell type:code id: tags:
``` python
```
......
%% Cell type:markdown id: tags:
# MSP Simulation Example - Modified Nodal Analysis
# MSP Simulation Example - Modified Nodal Analysis - with DPsim
%% Cell type:markdown id: tags:
## Sample Circuit
%% Cell type:markdown id: tags:
<img src="VS_CS_R4.png" width="500" align="left">
%% Cell type:markdown id: tags:
$R_1$: $1 \Omega$, $R_2$: $1 \Omega$, $R_3$: $10 \Omega$, $R_4$: $5 \Omega$
$I_1$: $1 A$, $V_{in}$: $1 V$
%% Cell type:markdown id: tags:
## Run simulation
%% Cell type:markdown id: tags:
Using the MNA-based C++ solver [DPsim](https://www.fein-aachen.org/en/projects/dpsim/) for power system simulation
%% Cell type:code id: tags:
``` python
import dpsim
model_name = 'EMT_VS_CS_R4_AC'
final_time = 0.0002
time_step = 0.0001
# Nodes
gnd = dpsim.emt.Node.GND()
n1 = dpsim.emt.Node('n1')
n2 = dpsim.emt.Node('n2')
n3 = dpsim.emt.Node('n3')
# Components
vs = dpsim.emt.ph1.VoltageSource('vs')
vs.V_ref = complex(10,0)
vs.f_src = 0
r1 = dpsim.emt.ph1.Resistor('r1')
r1.R = 1
r2 = dpsim.emt.ph1.Resistor('r2')
r2.R = 1
r3 = dpsim.emt.ph1.Resistor('r3')
r3.R = 10
r4 = dpsim.emt.ph1.Resistor('r4')
r4.R = 5
cs = dpsim.emt.ph1.CurrentSource('cs')
cs.I_ref = complex(1,0)
cs.f_src = 0
vs.connect([gnd, n1])
r1.connect([n1, n2])
r2.connect([n2, gnd])
r3.connect([n2, n3])
r4.connect([n3, gnd])
cs.connect([gnd, n3])
system = dpsim.SystemTopology(0, [gnd, n1, n2, n3], [vs, r1, r2, r3, r4, cs])
logger = dpsim.Logger(model_name)
logger.log_attribute(n1, 'v');
logger.log_attribute(n2, 'v');
logger.log_attribute(n3, 'v');
logger.log_attribute(r1, 'i_intf');
logger.log_attribute(r3, 'i_intf');
sim = dpsim.Simulation(model_name, system, timestep=time_step, duration=final_time, pbar=True, sim_type=1, log_level=4)
sim.add_logger(logger)
sim.start()
```
%% Output
%% Cell type:markdown id: tags:
## Analyze simulation log
%% Cell type:markdown id: tags:
### Read log
%% Cell type:code id: tags:
``` python
from villas.dataprocessing.readtools import *
from villas.dataprocessing.timeseries import *
import re
work_dir = 'Logs/'
log_path = work_dir + model_name + '_MNA.log'
log_lines, log_sections = read_dpsim_log(log_path)
```
%% Cell type:markdown id: tags:
### Show node detection log
%% Cell type:code id: tags:
``` python
for line_pos in log_sections['init']:
print(log_lines[line_pos])
```
%% Output
INFO: #### Start Initialization ####
INFO: Found node n1
INFO: Found node n2
INFO: Found node n3
INFO: Created virtual node0 = 3 for vs
INFO: Number of network nodes: 3
INFO: Number of nodes: 4
%% Cell type:markdown id: tags:
### Show matrix stamping log
%% Cell type:code id: tags:
``` python
for line_pos in log_sections['sysmat_stamp']:
print(log_lines[line_pos])
```
%% Output
DEBUG: Stamping EMT::Ph1::VoltageSource vs into system matrix:
0 0 0 1
0 0 0 0
0 0 0 0
1 0 0 0
DEBUG: Stamping EMT::Ph1::Resistor r1 into system matrix:
1 -1 0 1
-1 1 0 0
0 0 0 0
1 0 0 0
DEBUG: Stamping EMT::Ph1::Resistor r2 into system matrix:
1 -1 0 1
-1 2 0 0
0 0 0 0
1 0 0 0
DEBUG: Stamping EMT::Ph1::Resistor r3 into system matrix:
1 -1 0 1
-1 2.1 -0.1 0
0 -0.1 0.1 0
1 0 0 0
DEBUG: Stamping EMT::Ph1::Resistor r4 into system matrix:
1 -1 0 1
-1 2.1 -0.1 0
0 -0.1 0.3 0
1 0 0 0
DEBUG: Stamping EMT::Ph1::CurrentSource cs into system matrix:
1 -1 0 1
-1 2.1 -0.1 0
0 -0.1 0.3 0
1 0 0 0
%% Cell type:markdown id: tags:
### Show source vector log
%% Cell type:code id: tags:
``` python
for line_pos in log_sections['sourcevec_stamp']:
print(log_lines[line_pos])
```
%% Output
DEBUG: Stamping EMT::Ph1::VoltageSource vs into source vector:
0
0
0
10
DEBUG: Stamping EMT::Ph1::Resistor r1 into source vector:
0
0
0
10
DEBUG: Stamping EMT::Ph1::Resistor r2 into source vector:
0
0
0
10
DEBUG: Stamping EMT::Ph1::Resistor r3 into source vector:
0
0
0
10
DEBUG: Stamping EMT::Ph1::Resistor r4 into source vector:
0
0
0
10
DEBUG: Stamping EMT::Ph1::CurrentSource cs into source vector:
0
0
1
10
%% Cell type:markdown id: tags:
### Show LU decomposition of final system matrix
%% Cell type:code id: tags:
``` python
for line_pos in log_sections['sysmat_final']:
print(log_lines[line_pos])
for line_pos in log_sections['ludecomp']:
print(log_lines[line_pos])
```
%% Output
INFO: System matrix:
1 -1 0 1
-1 2.1 -0.1 0
0 -0.1 0.3 0
1 0 0 0
INFO: LU decomposition:
1 -1 0 1
-1 1.1 -0.1 1
0 -0.0909091 0.290909 0.0909091
1 0.909091 0.3125 -1.9375
%% Cell type:markdown id: tags:
## Read and show solution log
%% Cell type:code id: tags:
``` python
work_dir = 'Logs/'
model_name = 'EMT_VS_CS_R4_AC'
log_path = work_dir + model_name + '.csv'
ts_dpsim_emt = read_timeseries_dpsim(log_path, print_status=False)
for key, val in ts_dpsim_emt.items():
print(key + ': ' + str(val.values[0]))
```
%% Output
n1.v: 10.0
n2.v: 5.0
n3.v: 5.0
r1.i_intf: -5.0
r3.i_intf: -0.0
......
%% Cell type:markdown id: tags:
# MSP Simulation Example - Modified Nodal Analysis - without the DPsim solver
# MSP Simulation Example - Modified Nodal Analysis - without DPsim
%% Cell type:markdown id: tags:
## Sample Circuit
%% Cell type:markdown id: tags:
<img src="VS_CS_R4_HC.png" width="500" align="left">
%% Cell type:markdown id: tags:
$R_1$: $1 \Omega$, $R_2$: $1 \Omega$, $R_3$: $10 \Omega$, $R_4$: $5 \Omega$
$I_1$: $1 A$, $V_{in}$: $10 V$
$I_1$: $1 A$, $V_{0}$: $10 V$
%% Cell type:markdown id: tags:
## Circuit and Simulation Setup
%% Cell type:code id: tags:
``` python
import numpy as np
np.set_printoptions(sign=' ')
#Components
R1= 1.0
R2= 1.0
R3= 10.0
R4= 5.0
I_src= 1.0
V_src= 10.0
I_1= 1.0
V_0= 10.0
```
%% Cell type:markdown id: tags:
Derive the admittance matrix G and the source vector A applied in nodal analysis using the matrix stamp approach. Note here that there are two approaches to stamp the voltage source:
1. **NA**: Stamp the voltage source as **real** voltage source with internal resistance R1 and transform it into a current source using Norton transformation --> 2 nodes + 1 reference node
2. **MNA**: Stamp the voltage source as **ideal** voltage source. --> 3 nodes + 1 reference node
Derive the admittance matrix G and the source vector A applied in nodal analysis using the matrix stamp approach. Note here, that there are in general two approaches to stamp a voltage source connected in series with a resistor:
1. **NA**: Stamp the voltage source as **real** voltage source with the resistor as internal resistance (here R1) and transform it into a current source using Norton transformation (2 nodes involved).
2. **MNA**: Stamp the voltage source as **ideal** voltage source (3 nodes involved)
In the following, we apply the second option.
%% Cell type:markdown id: tags:
## Modified Nodal Analysis
%% Cell type:code id: tags:
``` python
#Building source vector
A_src = np.array([[0] ,
[0] ,
[I_src] ,
[V_src]])
[I_1] ,
[V_0]])
#Building admitance matrix
G = np.array([[1/R1, -1/R1 , 0 , 1],
[-1/R1, (1/R1 + 1/R2 + 1/R3) , -1/R3 , 0],
[0 , -1/R3, (1/R3 + 1/R4), 0],
[1, 0, 0, 0]])
#System solution
e_nodes = np.matmul(np.linalg.inv(G),A_src)
```
%% Cell type:markdown id: tags:
### Problem formulation
%% Cell type:code id: tags:
``` python
print('Admitance matrix: \n' + str(G))
print('\nProblem size: 4x4')
print('\nNode Voltages: \n' + str(e_nodes[0:3 , :]))
print('\nCurrent i_10 through the voltage source: \n'+ str(-e_nodes[1 , :]))
print('\nSource vector: \n' + str(A_src))
print('\nSize: 4x4')
```
%% Output
Admitance matrix:
[[ 1. -1. 0. 1. ]
[-1. 2.1 -0.1 0. ]
[ 0. -0.1 0.3 0. ]
[ 1. 0. 0. 0. ]]
Problem size: 4x4
Source vector:
[[ 0.]
[ 0.]
[ 1.]
[ 10.]]
Size: 4x4
%% Cell type:markdown id: tags:
### Problem solution
%% Cell type:code id: tags:
``` python
print('Node Voltages: \n' + str(e_nodes[0:3 , :]))
print('\nCurrent i_10 through the voltage source: \n'+ str(-e_nodes[1 , :]))
```
%% Output
Node Voltages:
[[ 10.]
[ 5.]
[ 5.]]
Current i_10 through the voltage source:
[-5.]
%% Cell type:markdown id: tags:
## LU decomposition of final system matrix
%% Cell type:code id: tags:
``` python
import scipy.linalg as linalg
import ipywidgets as widgets
P, L, U = linalg.lu(G, permute_l=False, overwrite_a=False, check_finite=True)
print('Admitance matrix: \n' + str(G))
print('\nLower triangular matrix: \n' + str(L))
print('\nUpper triangular matrix: \n'+ str(U))
```
%% Output
Admitance matrix:
[[ 1. -1. 0. 1. ]
[-1. 2.1 -0.1 0. ]
[ 0. -0.1 0.3 0. ]
[ 1. 0. 0. 0. ]]
Lower triangular matrix:
[[ 1. 0. 0. 0. ]
[-1. 1. 0. 0. ]
[ 0. -0.09090909 1. 0. ]
[ 1. 0.90909091 0.3125 1. ]]
Upper triangular matrix:
[[ 1. -1. 0. 1. ]
[ 0. 1.1 -0.1 1. ]
[ 0. 0. 0.29090909 0.09090909]
[ 0. 0. 0. -1.9375 ]]
%% Cell type:code id: tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment