Skip to content
Snippets Groups Projects
Commit 1b47cf86 authored by Ghassen Nakti's avatar Ghassen Nakti
Browse files

Update Resistive Companion Lecture example

parent 03cebc80
No related branches found
No related tags found
1 merge request!1Jdi gna
%% Cell type:markdown id: tags:
# MSP Simulation Example - Resistive Companion
%% Cell type:markdown id: tags:
## Sample Circuit
%% Cell type:markdown id: tags:
<img src="VS_R2L3.png" width="500" align="left">
%% Cell type:markdown id: tags:
$R_1$=$1 \Omega$, $R_2$=$2 \Omega$
$L_1$=$20 mH$, $L_2$=$100 mH$, $L_3$=$50 mH$
$V_{in}(t)$=$10 Vsin(\omega t)$
%% Cell type:markdown id: tags:
## Circuit and Simulation Setup
%% Cell type:code id: tags:
``` python
import numpy as np
import matplotlib.pyplot as plt
np.set_printoptions(sign=' ')
# Circuit parameters
R1 = 1
R2 = 2
L1 = 20e-3
L2 = 100e-3
L3 = 50e-3
Emax = 10
G1 = 1/R1
G2 = 1/R2
# Simulation parameters
# Note: Euler forward is numerically unstable for Ts>7.51e-6
T_total = 0.05
Ts = 1e-6
npoint = int(np.round(T_total/Ts))
print('Total simulation time: ' + str(T_total))
print('Simulation time step: ' + str(Ts))
```
%% Output
Total simulation time: 0.05
Simulation time step: 1e-06
%% Cell type:markdown id: tags:
## Euler Backward Integration Method
%% Cell type:code id: tags:
``` python
GL1_back = Ts/(L1)
GL2_back = Ts/(L2)
GL3_back = Ts/(L3)
# Conductance matrix
Gn = np.array([ [G1+GL1_back, -GL1_back, 0],
[-GL1_back, GL1_back+GL2_back+GL3_back, -GL3_back],
[0, -GL3_back, GL3_back+G2]])
# Node voltage vector
vn_back = np.zeros((3,npoint))
# Current vector in the 3 inductances
in_back = np.zeros((3,npoint))
# Intial conditions
# Voltage source at E(t=0)=0;
# i_L(t=0)=0 -> e1 = E(t=0) =0; e2 = 0; e3 = 0;
in_back[:,0] = np.zeros(3)
vn_back[:,0] = np.zeros(3)
# Enter Loop
# tic %Start a timer
for i in np.arange(1,npoint):
# Update source vector
AL1_back = in_back[0,i-1]
AL2_back = in_back[1,i-1]
AL3_back = in_back[2,i-1]
E = Emax*np.sin(2*np.pi*60*i*Ts)
Jn = np.array([E/R1-AL1_back, AL1_back-AL2_back-AL3_back, AL3_back])
# Matrix inversion and solution of the equation G*e=J;
vn_back[:,i] = np.linalg.solve(Gn, Jn)
# post step
in_back[0,i] = AL1_back+GL1_back*(vn_back[0,i]-vn_back[1,i])
in_back[1,i] = AL2_back+GL2_back*(vn_back[1,i])
in_back[2,i] = AL3_back+GL3_back*(vn_back[1,i]-vn_back[2,i])
# toc %stop the timer
```
%% Cell type:markdown id: tags:
## Euler Forward Integration Method
%% Cell type:code id: tags:
``` python
# Large resistor added in parallel with inductors (3 current sources coincide at a node)
Gadd = 1e-4
# Conductance matrix
Gn = np.array( [[G1+Gadd, -Gadd, 0],
[-Gadd, 3*Gadd, -Gadd],
[ 0, -Gadd, Gadd+G2]])
# Node voltage vector
vn_forw = np.zeros((3,npoint))
# Current vector in the 3 inductances
in_forw = np.zeros((3,npoint))
# Intial conditions
# Voltage source at E(t=0)=0;
# i_L(t=0)=0 -> e1 = E(t=0) =0; e2 = 0; e3 = 0;
in_forw[:,0] = np.zeros(3)
vn_forw[:,0] = np.zeros(3)
#Enter Loop
# tic % Start a timer
for i in np.arange(1,npoint):
# Update source vector
AL1_forw = in_forw[0,i-1]+(vn_forw[0,i-1]-vn_forw[1,i-1])*Ts/L1
AL2_forw = in_forw[1,i-1]+(vn_forw[1,i-1])*Ts/L2
AL3_forw = in_forw[2,i-1]+(vn_forw[1,i-1]-vn_forw[2,i-1])*Ts/L3
E = Emax*np.sin(2*np.pi*60*i*Ts)
Jn = np.array([E/R1-AL1_forw, AL1_forw-AL2_forw-AL3_forw, AL3_forw])
# Matrix inversion and solution of the equation G*e=J
vn_forw[:,i] = np.linalg.solve(Gn, Jn)
# post step
in_forw[0,i]= AL1_forw
in_forw[1,i]= AL2_forw
in_forw[2,i]= AL3_forw
```
%% Cell type:markdown id: tags:
## Trapezoidal Integration Method
%% Cell type:code id: tags:
``` python
GL1_trap = Ts/(2*L1)
GL2_trap = Ts/(2*L2)
GL3_trap = Ts/(2*L3)
# Conductance matrix
Gn = np.array( [[G1+GL1_trap, -GL1_trap, 0],
[-GL1_trap, GL1_trap+GL2_trap+GL3_trap, -GL3_trap],
[0, -GL3_trap, GL3_trap+G2]])
# Node voltage vector
vn_trap = np.zeros((3,npoint))
# Current vector in the 3 inductances
in_trap = np.zeros((3,npoint))
# Intial conditions
# Voltage source at E(t=0)=0
# i_L(t=0)=0 -> e1 = E(t=0) =0; e2 = 0; e3 = 0;
in_trap[:,0] = np.zeros(3)
vn_trap[:,0] = np.zeros(3)
#Enter Loop
#tic
for i in np.arange(1,npoint):
# Update source vector
AL1 = in_trap[0,i-1]+(vn_trap[0,i-1]-vn_trap[1,i-1])*Ts/(2*L1)
AL2 = in_trap[1,i-1]+(vn_trap[1,i-1])*Ts/(2*L2)
AL3 = in_trap[2,i-1]+(vn_trap[1,i-1]-vn_trap[2,i-1])*Ts/(2*L3)
E = Emax*np.sin(2*np.pi*60*i*Ts)
Jn = np.array([E/R1-AL1, AL1-AL2-AL3, AL3])
# Matrix inversion and solution of the equation G*e=J
vn_trap[:,i] = np.linalg.solve(Gn, Jn)
# post step
in_trap[0,i]= AL1+GL1_trap*(vn_trap[0,i]-vn_trap[1,i])
in_trap[1,i]= AL2+GL2_trap*(vn_trap[1,i])
in_trap[2,i]= AL3+GL3_trap*(vn_trap[1,i]-vn_trap[2,i])
#toc
```
%% Cell type:markdown id: tags:
## Result Comparison for different Integration Methods
%% Cell type:markdown id: tags:
Considering current through inductors $L_1$ and $L_2$
%% Cell type:markdown id: tags:
### Euler Backward vs Trapezoidal
%% Cell type:code id: tags:
``` python
t = np.arange(0, npoint)*Ts
plt.figure(figsize=(8,6))
plt.xlabel('Time [s]')
plt.ylabel('Current [A]')
plt.plot(t,in_trap[0,:], t, in_trap[2,:], linewidth=2)
plt.plot(t,in_back[0,:], ':', t, in_back[2,:],':', linewidth=2)
plt.xlim([0, (npoint-1)*Ts])
plt.legend(['Trapezoidal $I_{L1}$(t)', 'Trapezoidal $I_{L2}$(t)','E. Backward $I_{L1}$(t)', 'E. Backward $I_{L2}$(t)'])
plt.show()
```
%% Output
%% Cell type:markdown id: tags:
### Euler Forward vs Trapezoidal
%% Cell type:code id: tags:
``` python
plt.figure(figsize=(8,6))
plt.xlabel('Time [s]')
plt.ylabel('Current [A]')
plt.plot(t,in_trap[0,:], t, in_trap[2,:], linewidth=2)
plt.plot(t,in_forw[0,:], ':', t, in_forw[2,:],':', linewidth=2)
plt.xlim([0, (npoint-1)*Ts])
plt.legend(['Trapezoidal $I_{L1}$(t)', 'Trapezoidal $I_{L2}$(t)','E. Forward $I_{L1}$(t)', 'E. Forward $I_{L2}$(t)'])
plt.show()
```
%% Output
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# MSP Simulation Example - Resistive Companion - automated # MSP Simulation Example - Resistive Companion - automated
%% 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="VS_R2L3.png" width="500" align="left"> <img src="VS_R2L3.png" width="500" align="left">
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
$R_1$=$1 \Omega$, $R_2$=$2 \Omega$ $R_1$=$1 \Omega$, $R_2$=$2 \Omega$
$L_1$=$20 mH$, $L_2$=$100 mH$, $L_3$=$50 mH$ $L_1$=$20 mH$, $L_2$=$100 mH$, $L_3$=$50 mH$
$V_{in}(t)$=$10 V sin(\omega t)$ $V_{in}(t)$=$10 V sin(\omega t)$
%% 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
import warnings
warnings.filterwarnings('ignore')
%matplotlib inline
np.set_printoptions(sign=' ') np.set_printoptions(sign=' ')
# Circuit parameters # Circuit parameters
R1 = 1 R1 = 1
R2 = 2 R2 = 2
L1 = 20e-3 L1 = 20e-3
L2 = 100e-3 L2 = 100e-3
L3 = 50e-3 L3 = 50e-3
Emax = 10 Emax = 10
G1 = 1/R1 G1 = 1/R1
G2 = 1/R2 G2 = 1/R2
#default start values for Ts and T_total #default start values for Ts and T_total
Ts=1e-7 Ts=1e-7
T_total=1 T_total=0.05
```
%% Cell type:markdown id: tags:
## Euler Backward Integration Method
%% Cell type:code id: tags:
``` python
#function to perform the euler backward integration #function to perform the euler backward integration
def int_EB(Ts, T_total, npoint): def int_EB(Ts, T_total, npoint):
GL1_back = Ts/(L1) GL1_back = Ts/(L1)
GL2_back = Ts/(L2) GL2_back = Ts/(L2)
GL3_back = Ts/(L3) GL3_back = Ts/(L3)
# Conductance matrix # Conductance matrix
Gn = np.array([ [G1+GL1_back, -GL1_back, 0], Gn = np.array([ [G1+GL1_back, -GL1_back, 0],
[-GL1_back, GL1_back+GL2_back+GL3_back, -GL3_back], [-GL1_back, GL1_back+GL2_back+GL3_back, -GL3_back],
[0, -GL3_back, GL3_back+G2]]) [0, -GL3_back, GL3_back+G2]])
# Node voltage vector # Node voltage vector
vn_back = np.zeros((3,npoint)) vn_back = np.zeros((3,npoint))
# Current vector in the 3 inductances # Current vector in the 3 inductances
in_back = np.zeros((3,npoint)) in_back = np.zeros((3,npoint))
# Intial conditions # Intial conditions
# Voltage source at E(t=0)=0; # Voltage source at E(t=0)=0;
# i_L(t=0)=0 -> e1 = E(t=0) =0; e2 = 0; e3 = 0; # i_L(t=0)=0 -> e1 = E(t=0) =0; e2 = 0; e3 = 0;
in_back[:,0] = np.zeros(3) in_back[:,0] = np.zeros(3)
vn_back[:,0] = np.zeros(3) vn_back[:,0] = np.zeros(3)
# Enter Loop # Enter Loop
# tic %Start a timer # tic %Start a timer
for i in np.arange(1,npoint): for i in np.arange(1,npoint):
# Update source vector # Update source vector
AL1_back = in_back[0,i-1] AL1_back = in_back[0,i-1]
AL2_back = in_back[1,i-1] AL2_back = in_back[1,i-1]
AL3_back = in_back[2,i-1] AL3_back = in_back[2,i-1]
E = Emax*np.sin(2*np.pi*60*i*Ts) E = Emax*np.sin(2*np.pi*60*i*Ts)
Jn = np.array([E/R1-AL1_back, AL1_back-AL2_back-AL3_back, AL3_back]) Jn = np.array([E/R1-AL1_back, AL1_back-AL2_back-AL3_back, AL3_back])
# Matrix inversion and solution of the equation G*e=J; # Matrix inversion and solution of the equation G*e=J;
vn_back[:,i] = np.linalg.solve(Gn, Jn) vn_back[:,i] = np.linalg.solve(Gn, Jn)
# post step # post step
in_back[0,i] = AL1_back+GL1_back*(vn_back[0,i]-vn_back[1,i]) in_back[0,i] = AL1_back+GL1_back*(vn_back[0,i]-vn_back[1,i])
in_back[1,i] = AL2_back+GL2_back*(vn_back[1,i]) in_back[1,i] = AL2_back+GL2_back*(vn_back[1,i])
in_back[2,i] = AL3_back+GL3_back*(vn_back[1,i]-vn_back[2,i]) in_back[2,i] = AL3_back+GL3_back*(vn_back[1,i]-vn_back[2,i])
# toc %stop the timer # toc %stop the timer
#Plot results
plot_fun(in_back, Ts, npoint, 'E. Backward')
return in_back return in_back
```
%% Cell type:markdown id: tags:
## Euler Forward Integration Method
%% Cell type:code id: tags:
``` python
#function to perform the euler forward integration #function to perform the euler forward integration
def int_EF(Ts, T_total, npoint): def int_EF(Ts, T_total, npoint):
# Large resistor added in parallel with inductors (3 current sources coincide at a node) # Large resistor added in parallel with inductors (3 current sources coincide at a node)
Gadd = 1e-4 Gadd = 1e-4
# Conductance matrix # Conductance matrix
Gn = np.array( [[G1+Gadd, -Gadd, 0], Gn = np.array( [[G1+Gadd, -Gadd, 0],
[-Gadd, 3*Gadd, -Gadd], [-Gadd, 3*Gadd, -Gadd],
[ 0, -Gadd, Gadd+G2]]) [ 0, -Gadd, Gadd+G2]])
# Node voltage vector # Node voltage vector
vn_forw = np.zeros((3,npoint)) vn_forw = np.zeros((3,npoint))
# Current vector in the 3 inductances # Current vector in the 3 inductances
in_forw = np.zeros((3,npoint)) in_forw = np.zeros((3,npoint))
# Intial conditions # Intial conditions
# Voltage source at E(t=0)=0; # Voltage source at E(t=0)=0;
# i_L(t=0)=0 -> e1 = E(t=0) =0; e2 = 0; e3 = 0; # i_L(t=0)=0 -> e1 = E(t=0) =0; e2 = 0; e3 = 0;
in_forw[:,0] = np.zeros(3) in_forw[:,0] = np.zeros(3)
vn_forw[:,0] = np.zeros(3) vn_forw[:,0] = np.zeros(3)
#Enter Loop #Enter Loop
# tic % Start a timer # tic % Start a timer
for i in np.arange(1,npoint): for i in np.arange(1,npoint):
# Update source vector # Update source vector
AL1_forw = in_forw[0,i-1]+(vn_forw[0,i-1]-vn_forw[1,i-1])*Ts/L1 AL1_forw = in_forw[0,i-1]+(vn_forw[0,i-1]-vn_forw[1,i-1])*Ts/L1
AL2_forw = in_forw[1,i-1]+(vn_forw[1,i-1])*Ts/L2 AL2_forw = in_forw[1,i-1]+(vn_forw[1,i-1])*Ts/L2
AL3_forw = in_forw[2,i-1]+(vn_forw[1,i-1]-vn_forw[2,i-1])*Ts/L3 AL3_forw = in_forw[2,i-1]+(vn_forw[1,i-1]-vn_forw[2,i-1])*Ts/L3
E = Emax*np.sin(2*np.pi*60*i*Ts) E = Emax*np.sin(2*np.pi*60*i*Ts)
Jn = np.array([E/R1-AL1_forw, AL1_forw-AL2_forw-AL3_forw, AL3_forw]) Jn = np.array([E/R1-AL1_forw, AL1_forw-AL2_forw-AL3_forw, AL3_forw])
# Matrix inversion and solution of the equation G*e=J # Matrix inversion and solution of the equation G*e=J
vn_forw[:,i] = np.linalg.solve(Gn, Jn) vn_forw[:,i] = np.linalg.solve(Gn, Jn)
# post step # post step
in_forw[0,i]= AL1_forw in_forw[0,i]= AL1_forw
in_forw[1,i]= AL2_forw in_forw[1,i]= AL2_forw
in_forw[2,i]= AL3_forw in_forw[2,i]= AL3_forw
#Plot results
plot_fun(in_forw, Ts, npoint, 'E. Forward')
return in_forw return in_forw
```
%% Cell type:markdown id: tags:
## Trapezoidal Integration Method
%% Cell type:code id: tags:
``` python
#function to perform the trapezoidal integration #function to perform the trapezoidal integration
def int_TR(Ts, T_total, npoint): def int_TR(Ts, T_total, npoint):
GL1_trap = Ts/(2*L1) GL1_trap = Ts/(2*L1)
GL2_trap = Ts/(2*L2) GL2_trap = Ts/(2*L2)
GL3_trap = Ts/(2*L3) GL3_trap = Ts/(2*L3)
# Conductance matrix # Conductance matrix
Gn = np.array( [[G1+GL1_trap, -GL1_trap, 0], Gn = np.array( [[G1+GL1_trap, -GL1_trap, 0],
[-GL1_trap, GL1_trap+GL2_trap+GL3_trap, -GL3_trap], [-GL1_trap, GL1_trap+GL2_trap+GL3_trap, -GL3_trap],
[0, -GL3_trap, GL3_trap+G2]]) [0, -GL3_trap, GL3_trap+G2]])
# Node voltage vector # Node voltage vector
vn_trap = np.zeros((3,npoint)) vn_trap = np.zeros((3,npoint))
# Current vector in the 3 inductances # Current vector in the 3 inductances
in_trap = np.zeros((3,npoint)) in_trap = np.zeros((3,npoint))
# Intial conditions # Intial conditions
# Voltage source at E(t=0)=0 # Voltage source at E(t=0)=0
# i_L(t=0)=0 -> e1 = E(t=0) =0; e2 = 0; e3 = 0; # i_L(t=0)=0 -> e1 = E(t=0) =0; e2 = 0; e3 = 0;
in_trap[:,0] = np.zeros(3) in_trap[:,0] = np.zeros(3)
vn_trap[:,0] = np.zeros(3) vn_trap[:,0] = np.zeros(3)
#Enter Loop #Enter Loop
#tic #tic
for i in np.arange(1,npoint): for i in np.arange(1,npoint):
# Update source vector # Update source vector
AL1 = in_trap[0,i-1]+(vn_trap[0,i-1]-vn_trap[1,i-1])*Ts/(2*L1) AL1 = in_trap[0,i-1]+(vn_trap[0,i-1]-vn_trap[1,i-1])*Ts/(2*L1)
AL2 = in_trap[1,i-1]+(vn_trap[1,i-1])*Ts/(2*L2) AL2 = in_trap[1,i-1]+(vn_trap[1,i-1])*Ts/(2*L2)
AL3 = in_trap[2,i-1]+(vn_trap[1,i-1]-vn_trap[2,i-1])*Ts/(2*L3) AL3 = in_trap[2,i-1]+(vn_trap[1,i-1]-vn_trap[2,i-1])*Ts/(2*L3)
E = Emax*np.sin(2*np.pi*60*i*Ts) E = Emax*np.sin(2*np.pi*60*i*Ts)
Jn = np.array([E/R1-AL1, AL1-AL2-AL3, AL3]) Jn = np.array([E/R1-AL1, AL1-AL2-AL3, AL3])
# Matrix inversion and solution of the equation G*e=J # Matrix inversion and solution of the equation G*e=J
vn_trap[:,i] = np.linalg.solve(Gn, Jn) vn_trap[:,i] = np.linalg.solve(Gn, Jn)
# post step # post step
in_trap[0,i]= AL1+GL1_trap*(vn_trap[0,i]-vn_trap[1,i]) in_trap[0,i]= AL1+GL1_trap*(vn_trap[0,i]-vn_trap[1,i])
in_trap[1,i]= AL2+GL2_trap*(vn_trap[1,i]) in_trap[1,i]= AL2+GL2_trap*(vn_trap[1,i])
in_trap[2,i]= AL3+GL3_trap*(vn_trap[1,i]-vn_trap[2,i]) in_trap[2,i]= AL3+GL3_trap*(vn_trap[1,i]-vn_trap[2,i])
#toc #toc
#Plot results
plot_fun(in_trap, Ts, npoint, 'Trapezoidal')
return in_trap return in_trap
```
%% Cell type:markdown id: tags:
## Result Comparison for different Integration Methods
%% Cell type:code id: tags:
``` python
#Plot function #Plot function
def plot_fun(input_data, Ts, npoint, in_method): def plot_fun(time_step, npoint):
#Plot results
#Number of subplots (1*3) and size of the figure where the subplots will appear
fig, ax = plt.subplots(1,3, figsize=(16, 5))
#Calculate time values
t = np.arange(0, npoint)*Ts t = np.arange(0, npoint)*Ts
plt.figure(figsize=(4,3))
plt.xlabel('Time [s]') #Transform time values to milliseconds for better representation
plt.ylabel('Current [A]') t= np.multiply(t, 10e+3)
#plot current through L1 and L3
plt.plot(t,input_data[0,:], t, input_data[2,:], linewidth=2) #Plot results E. Backward
plt.xlim([0, (npoint-1)*Ts]) ax[0].set_title('E. Backward')
plt.legend([in_method +' $I_{L1}$(t)', in_method + ' $I_{L2}$(t)']) ax[0].set_xlim([0, (npoint-1)*Ts*10e+3])
plt.show() ax[0].set_xlabel('Time [ms]')
ax[0].set_ylabel('Current [A]')
ax[0].plot(t,in_back[0,:], 'r', t, in_back[2,:], 'r:')
leg = ax[0].legend(['$I_{L1}$(t)','$I_{L2}$(t)'])
#Plot results E. Forward
ax[1].set_title('E. Forward')
#Set axis limite and transform to milliseconds
ax[1].set_xlim([0, (npoint-1)*Ts*10e+3])
ax[1].set_xlabel('Time [ms]')
ax[1].set_ylabel('Current [A]')
ax[1].plot(t, in_forw[0,:] ,'g', t, in_forw[2,:], 'g:', linewidth=2)
leg = ax[1].legend(['$I_{L1}$(t)','$I_{L2}$(t)'])
#Plot results Trapezoidal
ax[2].set_title('Trapezoidal')
ax[2].set_xlim([0, (npoint-1)*Ts*10e+3])
ax[2].set_xlabel('Time [ms]')
ax[2].set_ylabel('Current [A]')
ax[2].plot(t,in_trap[0,:], 'b', t, in_trap[2,:], 'b:', linewidth=2)
leg = ax[2].legend(['$I_{L1}$(t)','$I_{L2}$(t)'])
#Slider function to forward the selected values to the simulation functions #Slider function to forward the selected values to the simulation functions
def slider_fun(time_step, total_time): def slider_fun(time_step):
global in_back , in_forw , in_trap, npoint global in_back , in_forw , in_trap, npoint
if time_step==0 or total_time==0: if time_step==0 or T_total==0:
print('\n \n \n' + 'Simulation time step is set to: ' + str(time_step) + ' seconds') print('\n \n \n' + 'Simulation time step is set to: ' + str(time_step) + ' seconds')
print('Total simulation time: ' + str(total_time) + ' seconds' + '\n \n') print('Total simulation time: ' + str(T_total) + ' seconds' + '\n \n')
else: else:
print('\n \n \n' + 'Simulation time step is set to: ' + str(time_step) + ' seconds') print('\n \n \n' + 'Simulation time step is set to: ' + str(time_step) + ' seconds')
print('Total simulation time: ' + str(total_time) + ' seconds' + '\n \n') print('Total simulation time: ' + str(T_total) + ' seconds' + '\n \n')
npoint = int(np.round(total_time/time_step)) npoint = int(np.round(T_total/time_step))
in_back=int_EB(time_step, total_time, npoint) in_back=int_EB(time_step, T_total, npoint)
in_forw=int_EF(time_step, total_time, npoint) in_forw=int_EF(time_step, T_total, npoint)
in_trap=int_TR(time_step, total_time, npoint) in_trap=int_TR(time_step, T_total, npoint)
return time_step, total_time plot_fun(time_step, npoint)
return time_step, T_total
#Values range for the simulation steps #Values range for the simulation steps
values=[round(i*10**-7, 7) for i in range(101)] values=[round(i*10**-7, 7) for i in range(101)]
#It is important to set the argument continous_update to 'false' so that the slider_fun is called after the user finishes dragging the slider. #It is important to set the argument continous_update to 'false' so that the slider_fun is called after the user finishes dragging the slider.
#Otherwise the function will be continuously called for several values in the dragging interval and the expected results will take alot of time to appear #Otherwise the function will be continuously called for several values in the dragging interval and the expected results will take alot of time to appear
slider= widget.interactive(slider_fun, time_step=widget.SelectionSlider(description="Time step", continuous_update=False, options=[("%g"%i,i) for i in values], layout=widget.Layout(width='50%', height='80px')), slider= widget.interactive(slider_fun, time_step=widget.SelectionSlider(description="Time step", continuous_update=False, options=[("%g"%i,i) for i in values], layout=widget.Layout( width='50%', height='50px',)));
total_time=widget.FloatSlider(description="Total time", continuous_update=False, min=0.0, max=0.1, step=0.001, layout=widget.Layout(width='50%', height='80px')));
display(slider) display(slider)
#inspect.getmembers(widget.SelectionSlider)
``` ```
%% Output %% Output
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Result Comparison for different Integration Methods ## Critical time step:
$7,6 \cdot 10^{-6} s < T_{critical} $
%% Cell type:markdown id: tags:
Considering current through inductors $L_1$ and $L_2$
%% Cell type:code id: tags:
``` python
#Assign slider values to simulation parameters
Ts, T_total = slider.result
npoint = int(np.round(T_total/Ts))
def plot_vs(Euler_Forward,Euler_Backward,Trapezoidal_Rule):
flags= [int(Euler_Forward), int(Euler_Backward) , int(Trapezoidal_Rule)]
graphs={'E. Forward ': in_forw*flags[0],
'E. Backward ': in_back*flags[1],
'Trapezoidal ': in_trap*flags[2]}
t = np.arange(0, npoint)*Ts
plt.figure(figsize=(8,6))
plt.xlabel('Time [s]')
plt.ylabel('Current [A]')
for key,val in graphs.items():
if np.all(val==0):
continue
plt.plot(t,val[0,:], label= key + '$I_{L1}$(t)')
plt.plot(t, val[2,:], label= key +'$I_{L2}$(t)')
plt.legend(loc='upper right')
plt.show()
print('Please select the integration methods you want to compare:')
widget.interact(plot_vs, Euler_Forward =False,
Euler_Backward=False,
Trapezoidal_Rule=False);
```
%% Output
Please select the integration methods you want to compare:
%% 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