Skip to content

Commit

Permalink
Refactoring (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhinkkan authored Aug 21, 2024
1 parent dac3d7e commit 147a38b
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 133 deletions.
14 changes: 6 additions & 8 deletions examples/grid_following/plot_gfl_10kva.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from motulator.common.utils import BaseValues, NominalValues
from motulator.grid import model
import motulator.grid.control.grid_following as control
from motulator.grid.utils import FilterPars, GridPars, plot_grid
from motulator.grid.utils import FilterPars, GridPars, plot
# from motulator.grid.utils import plot_voltage_vector
# from motulator.common.model import CarrierComparison
# import numpy as np
Expand All @@ -38,8 +38,7 @@
ac_filter = model.ACFilter(filter_par, grid_par)

# AC grid model with constant voltage magnitude and frequency
grid_model = model.ThreePhaseVoltageSource(
w_g=grid_par.w_gN, abs_e_g=grid_par.u_gN)
grid_model = model.ThreePhaseVoltageSource(w_g=base.w, abs_e_g=base.u)

# Inverter with constant DC voltage
converter = VoltageSourceConverter(u_dc=650)
Expand All @@ -54,8 +53,7 @@
# Configure the control system.

# Control configuration parameters
cfg = control.GFLControlCfg(
grid_par=grid_par, filter_par=filter_par, max_i=1.5*base.i)
cfg = control.GFLControlCfg(grid_par, filter_par, max_i=1.5*base.i)

# Create the control system
ctrl = control.GFLControl(cfg)
Expand All @@ -68,8 +66,8 @@
ctrl.ref.q_g = lambda t: (t > .04)*4e3

# Uncomment lines below to simulate a unbalanced fault (add negative sequence)
# mdl.grid_model.par.abs_e_g = 0.75*base.u
# mdl.grid_model.par.abs_e_g_neg = 0.25*base.u
# mdl.grid_model.par.abs_e_g = .75*base.u
# mdl.grid_model.par.abs_e_g_neg = .25*base.u
# mdl.grid_model.par.phi_neg = -np.pi/3

# %%
Expand All @@ -86,4 +84,4 @@

# Uncomment line below to plot locus of the grid voltage space vector
# plot_voltage_vector(sim, base)
plot_grid(sim, base, plot_pcc_voltage=True)
plot(sim, base, plot_pcc_voltage=False)
18 changes: 4 additions & 14 deletions examples/grid_following/plot_gfl_dc_bus_10kva.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from motulator.grid import model
import motulator.grid.control.grid_following as control
from motulator.grid.control import DCBusVoltageController
from motulator.grid.utils import FilterPars, GridPars, plot_grid
from motulator.grid.utils import FilterPars, GridPars, plot

# %%
# Compute base values based on the nominal values.
Expand All @@ -35,12 +35,8 @@
# Create AC filter with given parameters
ac_filter = model.ACFilter(filter_par, grid_par)

# AC-voltage magnitude (to simulate voltage dips or short-circuits)
abs_e_g_var = lambda t: base.u

# AC grid model with constant voltage magnitude and frequency
grid_model = model.ThreePhaseVoltageSource(
w_g=grid_par.w_gN, abs_e_g=abs_e_g_var)
grid_model = model.ThreePhaseVoltageSource(w_g=base.w, abs_e_g=base.u)

# Inverter model with DC-bus dynamics included
converter = VoltageSourceConverter(u_dc=600, C_dc=1e-3)
Expand All @@ -51,14 +47,8 @@
# %%
# Configure the control system.

# Control parameters
cfg = control.GFLControlCfg(
grid_par=grid_par,
C_dc=1e-3,
filter_par=filter_par,
max_i=1.5*base.i,
)
# Create the control system
cfg = control.GFLControlCfg(grid_par, filter_par, max_i=1.5*base.i, C_dc=1e-3)
ctrl = control.GFLControl(cfg)

# Add the DC-bus voltage controller to the control system
Expand Down Expand Up @@ -86,4 +76,4 @@
# By default results are plotted in per-unit values. By omitting the argument
# `base` you can plot the results in SI units.

plot_grid(sim=sim, base=base, plot_pcc_voltage=True)
plot(sim, base)
19 changes: 4 additions & 15 deletions examples/grid_following/plot_gfl_lcl_10kva.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from motulator.common.utils import BaseValues, NominalValues
from motulator.grid import model
import motulator.grid.control.grid_following as control
from motulator.grid.utils import FilterPars, GridPars, plot_grid
from motulator.grid.utils import FilterPars, GridPars, plot

# %%
# Compute base values based on the nominal values.
Expand All @@ -32,28 +32,20 @@
# DC-bus parameters
ac_filter = model.ACFilter(filter_par, grid_par)

# AC-voltage magnitude (to simulate voltage dips or short-circuits)
abs_e_g_var = lambda t: grid_par.u_gN

# AC grid model with constant voltage magnitude and frequency
grid_model = model.ThreePhaseVoltageSource(
w_g=grid_par.w_gN, abs_e_g=abs_e_g_var)
grid_model = model.ThreePhaseVoltageSource(w_g=base.w, abs_e_g=base.u)

# Inverter model with constant DC voltage
converter = VoltageSourceConverter(u_dc=650)

# Create system model
mdl = model.GridConverterSystem(converter, ac_filter, grid_model)

# Uncomment line below to enable the PWM model
#mdl.pwm = CarrierComparison()

# %%
# Configure the control system.

# Control parameters
cfg = control.GFLControlCfg(
grid_par=grid_par, filter_par=filter_par, max_i=1.5*base.i)
cfg = control.GFLControlCfg(grid_par, filter_par, max_i=1.5*base.i)

# Create the control system
ctrl = control.GFLControl(cfg)
Expand All @@ -74,7 +66,4 @@
# %%
# Plot the results.

# By default results are plotted in per-unit values. By omitting the argument
# `base` you can plot the results in SI units.

plot_grid(sim, base=base, plot_pcc_voltage=True)
plot(sim, base)
32 changes: 10 additions & 22 deletions examples/grid_forming/plot_gfm_obs_13kva.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from motulator.common.utils import BaseValues, NominalValues
from motulator.grid import model
import motulator.grid.control.grid_forming as control
from motulator.grid.utils import FilterPars, GridPars, plot_grid
from motulator.grid.utils import FilterPars, GridPars, plot
# from motulator.common.model import CarrierComparison

# %%
Expand All @@ -38,18 +38,14 @@
ac_filter = model.ACFilter(filter_par, grid_par)

# Grid voltage source with constant frequency and voltage magnitude
grid_model = model.ThreePhaseVoltageSource(
w_g=grid_par.w_gN, abs_e_g=grid_par.u_gN)
grid_model = model.ThreePhaseVoltageSource(w_g=base.w, abs_e_g=base.u)

# Inverter with constant DC voltage
converter = VoltageSourceConverter(u_dc=650)

# Create system model
mdl = model.GridConverterSystem(converter, ac_filter, grid_model)

# Uncomment the lines below to enable the PWM model
# mdl.pwm = CarrierComparison()

# %%
# Configure the control system.

Expand All @@ -58,11 +54,7 @@

# Set the configuration parameters
cfg = control.ObserverBasedGFMControlCfg(
grid_par=grid_par_est,
filter_par=filter_par,
T_s=100e-6,
max_i=1.3*base.i,
R_a=.2*base.Z)
grid_par_est, filter_par, max_i=1.3*base.i, T_s=100e-6, R_a=.2*base.Z)

# Create the control system
ctrl = control.ObserverBasedGFMControl(cfg)
Expand All @@ -71,19 +63,18 @@
# Set the references for converter output voltage magnitude and active power.

# Converter output voltage magnitude reference
ctrl.ref.v_c = lambda t: grid_par.u_gN
ctrl.ref.v_c = lambda t: base.u

# Active power reference
ctrl.ref.p_g = lambda t: ((t > .2)*(4.15e3) + (t > .5)*(4.15e3) + (t > .8)*
(4.2e3) - (t > 1.2)*(12.5e3))
ctrl.ref.p_g = lambda t: ((t > .2)/3 + (t > .5)/3 + (t > .8)/3 -
(t > 1.2))*nom.P

# Uncomment line below to simulate operation in rectifier mode
# ctrl.ref.p_g = lambda t: ((t > .2) - (t > .7)*2 + (t > 1.2))*12.5e3
# ctrl.ref.p_g = lambda t: ((t > .2) - (t > .7)*2 + (t > 1.2))*nom.P

# Uncomment lines below to simulate a grid voltage sag with constant ref.p_g
# mdl.grid_model.par.e_g_abs = lambda t: (
# 1 - (t > .2)*(0.8) + (t > 1)*(0.8))*grid_par.u_gN
# ctrl.ref.p_g = lambda t: 12.5e3
# mdl.grid_model.par.abs_e_g = lambda t: (1 - (t > .2)*.8 + (t > 1)*.8)*base.u
# ctrl.ref.p_g = lambda t: nom.P

# %%
# Create the simulation object and simulate it.
Expand All @@ -94,7 +85,4 @@
# %%
# Plot the results.

# By default results are plotted in per-unit values. By omitting the argument
# `base` you can plot the results in SI units.

plot_grid(sim=sim, base=base, plot_pcc_voltage=False)
plot(sim, base)
20 changes: 6 additions & 14 deletions examples/grid_forming/plot_gfm_rfpsc_13kva.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from motulator.common.utils import BaseValues, NominalValues
from motulator.grid import model
import motulator.grid.control.grid_forming as control
from motulator.grid.utils import FilterPars, GridPars, plot_grid
from motulator.grid.utils import FilterPars, GridPars, plot

# %%
# Compute base values based on the nominal values.
Expand All @@ -35,8 +35,7 @@
ac_filter = model.ACFilter(filter_par, grid_par)

# Grid voltage source with constant frequency and voltage magnitude
grid_model = model.ThreePhaseVoltageSource(
w_g=grid_par.w_gN, abs_e_g=grid_par.u_gN)
grid_model = model.ThreePhaseVoltageSource(w_g=base.w, abs_e_g=base.u)

# Inverter with constant DC voltage
converter = VoltageSourceConverter(u_dc=650)
Expand All @@ -49,11 +48,7 @@

# Control configuration parameters
cfg = control.RFPSCControlCfg(
grid_par=grid_par,
filter_par=filter_par,
T_s=100e-6,
max_i=1.3*base.i,
R_a=.2*base.Z)
grid_par, filter_par, max_i=1.3*base.i, T_s=100e-6, R_a=.2*base.Z)

# Create the control system
ctrl = control.RFPSCControl(cfg)
Expand All @@ -62,10 +57,10 @@
# Set the references for converter output voltage magnitude and active power.

# Converter output voltage magnitude reference
ctrl.ref.v = lambda t: grid_par.u_gN
ctrl.ref.v_c = lambda t: base.u

# Active power reference
ctrl.ref.p_g = lambda t: ((t > .2)*(1/3) + (t > .5)*(1/3) + (t > .8)*(1/3) -
ctrl.ref.p_g = lambda t: ((t > .2)/3 + (t > .5)/3 + (t > .8)/3 -
(t > 1.2))*nom.P

# %%
Expand All @@ -77,7 +72,4 @@
# %%
# Plot the results.

# By default results are plotted in per-unit values. By omitting the argument
# `base` you can plot the results in SI units.

plot_grid(sim, base=base, plot_pcc_voltage=True)
plot(sim, base)
8 changes: 2 additions & 6 deletions motulator/common/control/_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,8 @@ class ComplexPIController:
2DOF synchronous-frame complex-vector PI controller.
This implements a discrete-time 2DOF synchronous-frame complex-vector PI
controller, based on a disturbance observer structure. The complex-vector
gain selection is based on [#Bri2000]_. The addition of the feedforward
signal is based on [#Har2009]_. The continuous-time counterpart of
the controller is::
controller [#Bri2000]_. The continuous-time counterpart of the controller
is::
u = k_t*ref_i - k_p*i + (k_i + 1j*w*k_t)/s*(ref_i - i) + u_ff
Expand Down Expand Up @@ -330,11 +328,9 @@ class ComplexPIController:
"""

def __init__(self, k_p, k_i, k_t=None):
# Gains
self.k_p = k_p
self.k_t = k_t if k_t is not None else k_p
self.alpha_i = k_i/self.k_t # Inverse of the integration time T_i
# States
self.v, self.u_i = 0, 0

def output(self, ref_i, i, u_ff=0):
Expand Down
Loading

0 comments on commit 147a38b

Please sign in to comment.