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

first revision of notebook exercise 3 tasks 1

parent 03cebc80
No related branches found
No related tags found
1 merge request!1Jdi gna
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Modeling and Simulation of Complex Power Systems # Modeling and Simulation of Complex Power Systems
## Simulation Examples ## Simulation Examples
The following notebooks are currently available: The following notebooks are currently available:
Lecture Examples: Lecture Examples:
- [Lecture 2 - Modified Nodal Analysis - without DPsim](./lectures/02_NA_MNA/Lecture_SimExample_MNA_withoutDPsim.ipynb) - [Lecture 2 - Modified Nodal Analysis - without DPsim](./lectures/02_NA_MNA/Lecture_SimExample_MNA_withoutDPsim.ipynb)
- [Lecture 2 - Modified Nodal Analysis - with DPsim](./lectures/02_NA_MNA/Lecture_SimExample_MNA_DPsim.ipynb) - [Lecture 2 - Modified Nodal Analysis - with DPsim](./lectures/02_NA_MNA/Lecture_SimExample_MNA_DPsim.ipynb)
- [Lecture 3 - Resistive Companion](./lectures/03_ResistiveCompanion/Lecture_SimExample_ResistiveCompanion.ipynb) - [Lecture 3 - Resistive Companion](./lectures/03_ResistiveCompanion/Lecture_SimExample_ResistiveCompanion.ipynb)
- [Lecture 3 - Resistive Companion - automated](./lectures/03_ResistiveCompanion/Lecture_SimExample_ResistiveCompanion_automated.ipynb) - [Lecture 3 - Resistive Companion - automated](./lectures/03_ResistiveCompanion/Lecture_SimExample_ResistiveCompanion_automated.ipynb)
- [Lecture 4 - Nonlinear Resistive Companion](./lectures/04_NLResistiveCompanion/Lecture_SimExample_NLResistiveCompanion.ipynb) - [Lecture 4 - Nonlinear Resistive Companion](./lectures/04_NLResistiveCompanion/Lecture_SimExample_NLResistiveCompanion.ipynb)
- [Lecture 5 - State Space Equations](./lectures/05_StateSpace/Lecture_SimExample_StateSpace.ipynb) - [Lecture 5 - State Space Equations](./lectures/05_StateSpace/Lecture_SimExample_StateSpace.ipynb)
- [Lecture 8 - Dynamic Phasors](./lectures/08_DecoupledELMESim/Lecture_SimExample_DecoupledELMESim.ipynb) - [Lecture 8 - Dynamic Phasors](./lectures/08_DecoupledELMESim/Lecture_SimExample_DecoupledELMESim.ipynb)
- [Lecture 9 - Diakoptics](./lectures/09_Diakoptics/Lecture_SimExample_Diakoptics.ipynb) - [Lecture 9 - Diakoptics](./lectures/09_Diakoptics/Lecture_SimExample_Diakoptics.ipynb)
- [Lecture 9 - LIM](./lectures/09_LIM/Lecture_SimExample_LIM.ipynb) - [Lecture 9 - LIM](./lectures/09_LIM/Lecture_SimExample_LIM.ipynb)
- [Lecture 11 - Uncertain System Analysis](./lectures/11_UncertainSystemAnalysis/Lecture_SimExample_UncertainSystemAnalysis.ipynb) - [Lecture 11 - Uncertain System Analysis](./lectures/11_UncertainSystemAnalysis/Lecture_SimExample_UncertainSystemAnalysis.ipynb)
Exercise Examples: Exercise Examples:
- [Exercice 3 - ResistiveCompanion -Task 1](./exercises/03_ResistiveCompanion/Exercise_SimExample_ResistiveCompanion_Task1.ipynb) - [Exercise 3 - Resistive Companion - Task 1](./exercises/03_ResistiveCompanion/Exercise_SimExample_ResistiveCompanion_Task1.ipynb)
- [Exercise 6 - Diakoptics - Extra Task](./exercises/06_Diakoptics_ExtraTask/Exercise_SimExample_Diakoptics_ExtraTask.ipynb) - [Exercise 6 - Diakoptics - Extra Task](./exercises/06_Diakoptics_ExtraTask/Exercise_SimExample_Diakoptics_ExtraTask.ipynb)
- [Exercise 7 - LIM - Extra Task](./exercises/07_LIM_ExtraTask/Exercise_SimExample_LIM_Exercise_ExtraTask.ipynb) - [Exercise 7 - LIM - Extra Task](./exercises/07_LIM_ExtraTask/Exercise_SimExample_LIM_Exercise_ExtraTask.ipynb)
- [Exercise 8 - Numerical Integration](./exercises/08_NumInt/Exercise_SimExample_NumInt_example.ipynb) - [Exercise 8 - Numerical Integration](./exercises/08_NumInt/Exercise_SimExample_NumInt_example.ipynb)
## Helping Material ## Helping Material
### Notebooks ### Notebooks
Learn how to work with the notebooks by accessing the extensive information under **Help** in the navigation bar. 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: 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 - Notebook Basics
- Running Code - Running Code
- Markdown Cells - Markdown Cells
### Python ### 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. 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 ### 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**. 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 ### Contact
Any questions can be adressed to Any questions can be adressed to
[acs-teaching-mscps@eonerc.rwth-aachen.de](mailto:acs-teaching-mscps@eonerc.rwth-aachen.de) [acs-teaching-mscps@eonerc.rwth-aachen.de](mailto:acs-teaching-mscps@eonerc.rwth-aachen.de)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
......
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# MSP Simulation Example - Resistive Companion - # MSP Simulation Example - Resistive Companion - Task 1
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Sample Circuit ## Sample Circuit
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
<img src="Circuit_Exercise_Task1.png" width="400" align="left"> <img src="Circuit_Exercise_Task1.png" width="400" align="left">
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
$R_1$ $=0.2 \Omega$ <br> $R$ $=0.2 \Omega$ <br>
$L_1$ $=1mH$ <br> $L$ $=1mH$ <br>
$I_1$$=10A$ <br> $I$$=10A$ <br>
$i_L(0)=0 $ <br> $i_L(0)=0 $ <br>
$\Delta t = 0.1ms$ <br>
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Circuit and Simulation Setup ## Circuit and Simulation Setup
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import numpy as np import numpy as np
import ipywidgets as widget import ipywidgets as widget
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
np.set_printoptions(sign=' ') np.set_printoptions(sign=' ')
# Circuit parameters # Parameters
R1 = 0.2 R1 = 0.2
L1 = 1e-3 L1 = 1e-3
I_src= 10 I_src= 10
time_step = 1e-4
G1 = 1/R1 G1 = 1/R1
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## DC equivalent of Inductor ## DC equivalent of Inductor
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Equation of the inductor: ### Equation of the inductor:
$v_L(t) =L\cdot\frac{di_L}{dt}$ $v_L(t) =L\cdot\frac{di_L}{dt}$
### Integration: ### Integration:
$i_L(t+\Delta{t}) =i_L(t) + \frac{1}{L} \int_{t}^{t+\Delta{t}} \ v_L(\tau) d\tau$ $i_L(t+\Delta{t}) =i_L(t) + \frac{1}{L} \int_{t}^{t+\Delta{t}} \ v_L(\tau) d\tau$
### Discretization: ### Discretization:
$i_L(k+1)= i_L(k) + \frac{1}{L} \int_{t_k}^{t_k+\Delta{t}} \ v_L(\tau) d\tau$ $i_L(k+1)= i_L(k) + \frac{1}{L} \int_{t_k}^{t_k+\Delta{t}} \ v_L(\tau) d\tau$
### Application of trapezoidal rule: ### Application of trapezoidal rule:
$i_L(k)= i_L(k) + \frac{1}{L} \frac{v_L(k)+v_L(k+1)}{2} \Delta{t} $ $i_L(k+1)= i_L(k) + \frac{1}{L} \frac{v_L(k)+v_L(k+1)}{2} \Delta{t} $
$i_L(k+1)=\frac{\Delta{t}}{2L} v_L(k+1) + (i_L(k) + \frac{\Delta{t}}{2L} v_L(k)) $ $i_L(k+1)=\frac{\Delta{t}}{2L} v_L(k+1) + (i_L(k) + \frac{\Delta{t}}{2L} v_L(k)) $
The inductor in the calculation step (k + 1) can be substituted with an inductance $G_L = \frac{\Delta{t}}{2L}$ in parallel with a current source $A_L(k) = i_L(k) + \frac{\Delta{t}}{2L} v_L(k) $ The inductor in the calculation step (k + 1) can be substituted with an inductance $G_L = \frac{\Delta{t}}{2L}$ in parallel with a current source $A_L(k) = i_L(k) + \frac{\Delta{t}}{2L} v_L(k) $
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
<img src="Circuit_Exercise_Task1_DCequivalents.png" width="400" align="left"> <img src="Circuit_Exercise_Task1_DCequivalents.png" width="400" align="left">
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Run Simulation ## Run Simulation
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
#Perform simulation with dpsim #Perform simulation with dpsim
import dpsim import dpsim
#Assign slider values to simulation parameters
model_name = 'CS_R1L1' model_name = 'CS_R1L1'
time_step = 1e-4
#Set final time to calculate first two simulation steps #Set final time to calculate first two simulation steps
final_time = 2e-4 final_time = 2e-4
#Number of simulation steps #Number of simulation steps
npoint = int(np.round(final_time/time_step)) npoint = int(np.round(final_time/time_step))
# Nodes # Nodes
gnd = dpsim.emt.Node.GND() gnd = dpsim.emt.Node.GND()
n1 = dpsim.emt.Node('n1') n1 = dpsim.emt.Node('n1')
# Components # Components
r1 = dpsim.emt.ph1.Resistor('r1') r1 = dpsim.emt.ph1.Resistor('r1')
r1.R = 0.2 r1.R = 0.2
l1 = dpsim.emt.ph1.Inductor('l1') l1 = dpsim.emt.ph1.Inductor('l1')
l1.L = 1e-3 l1.L = 1e-3
cs = dpsim.emt.ph1.CurrentSource('cs') cs = dpsim.emt.ph1.CurrentSource('cs')
cs.I_ref = complex(10,0) cs.I_ref = complex(10,0)
cs.f_src = 0 cs.f_src = 0
cs.connect([gnd, n1]) cs.connect([gnd, n1])
r1.connect([n1, gnd]) r1.connect([n1, gnd])
l1.connect([n1, gnd]) l1.connect([n1, gnd])
system = dpsim.SystemTopology(0, [gnd, n1], [cs, r1, l1]) system = dpsim.SystemTopology(0, [gnd, n1], [cs, r1, l1])
logger = dpsim.Logger(model_name) logger = dpsim.Logger(model_name)
logger.log_attribute(n1, 'v'); logger.log_attribute(n1, 'v');
logger.log_attribute(r1, 'i_intf'); logger.log_attribute(r1, 'i_intf');
logger.log_attribute(l1, 'i_intf'); logger.log_attribute(l1, 'i_intf');
sim = dpsim.Simulation(model_name, system, timestep=time_step, duration=final_time, pbar=True, sim_type=1, solver_type=0, log_level=4) sim = dpsim.Simulation(model_name, system, timestep=time_step, duration=final_time, pbar=True, sim_type=1, solver_type=0, log_level=4)
sim.add_logger(logger) sim.add_logger(logger)
sim.start() sim.start()
``` ```
%% Output %% Output
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from villas.dataprocessing.readtools import * from villas.dataprocessing.readtools import *
from villas.dataprocessing.timeseries import * from villas.dataprocessing.timeseries import *
import re import re
work_dir = 'logs/' work_dir = 'logs/'
log_path = work_dir + model_name + '_MNA.log' log_path = work_dir + model_name + '_MNA.log'
log_lines, log_sections = read_dpsim_log(log_path) log_lines, log_sections = read_dpsim_log(log_path)
for line_pos in log_sections['sysmat_stamp']: for line_pos in log_sections['sysmat_stamp']:
print(log_lines[line_pos]) print(log_lines[line_pos])
for line_pos in log_sections['sourcevec_stamp']: for line_pos in log_sections['sourcevec_stamp']:
print(log_lines[line_pos]) print(log_lines[line_pos])
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Read and show solution log ## Read and show solution log
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from villas.dataprocessing.readtools import * from villas.dataprocessing.readtools import *
from villas.dataprocessing.timeseries import * from villas.dataprocessing.timeseries import *
import re import re
work_dir = 'logs/' work_dir = 'logs/'
model_name = 'CS_R1L1' model_name = 'CS_R1L1'
log_path = work_dir + model_name + '.csv' log_path = work_dir + model_name + '.csv'
ts_dpsim_emt = read_timeseries_dpsim(log_path, print_status=False) ts_dpsim_emt = read_timeseries_dpsim(log_path, print_status=False)
for key, val in ts_dpsim_emt.items(): for key, val in ts_dpsim_emt.items():
print(key + ': ' + str(val.values)) print(key + ': ' + str(val.values))
``` ```
%% Output %% Output
l1.i_intf: [-0.09901 -0.295069] l1.i_intf: [-0.09901 -0.295069]
n1.v: [ 1.980198 1.940986] n1.v: [ 1.980198 1.940986]
r1.i_intf: [-9.90099 -9.704931] r1.i_intf: [-9.90099 -9.704931]
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Plot results ## Plot results
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
#Extract plot data #Extract plot data
plot_data=ts_dpsim_emt["n1.v"] plot_data=ts_dpsim_emt["n1.v"]
y_values= np.asarray(plot_data.values) y_values= np.asarray(plot_data.values)
#Add initial value #Add initial value
y_values= np.insert(y_values, 0, I_src*R1) y_values= np.insert(y_values, 0, I_src*R1)
t = np.arange(npoint+1)*time_step t = np.arange(npoint+1)*time_step
plt.figure(figsize=(8,6)) plt.figure(figsize=(8,6))
plt.xlabel('Time [ms]') plt.xlabel('Time [ms]')
plt.ylabel('Voltage [V]') plt.ylabel('Voltage [V]')
plt.axis([0, 4*time_step, 1.9, 2.05]) plt.axis([0, 4*time_step, 1.9, 2.05])
plt.scatter(t,y_values, label='$e_{1}$(t)') plt.scatter(t,y_values, label='$e_{1}$(t)')
#show corresponding values on the plot #show corresponding values on the plot
for i in np.arange(npoint+1): for i in np.arange(npoint+1):
plt.annotate(' ' + str(np.round(y_values[i], 2)), (t[i], y_values[i])) plt.annotate(' ' + str(np.round(y_values[i], 2)), (t[i], y_values[i]))
plt.legend(loc='upper right') plt.legend(loc='upper right')
plt.show() plt.show()
``` ```
%% Output %% Output
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment