code cleanup:

consistenly use getter and setter methods
commenting etc
This commit is contained in:
Brantegger Georg
2022-07-27 11:40:58 +02:00
parent ac8bfdb7c6
commit d1c15090dc
13 changed files with 956 additions and 584 deletions

View File

@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
@@ -23,15 +23,16 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#define constants\n",
"\n",
"#Turbine\n",
"Q_nenn = 0.85\n",
"p_nenn,_ = pressure_conversion(10.6,'bar','Pa')\n",
"Q_nenn = 0.85 # m³/s\n",
"p_nenn = pressure_conversion(10.6,'bar','Pa')\n",
"closing_time = 480. #s\n",
"\n",
"# physics\n",
"g = 9.81 # gravitational acceleration [m/s²]\n",
@@ -39,8 +40,8 @@
"\n",
"# define controller constants\n",
"target_level = 8. # m\n",
"Kp = 0.1\n",
"Ti = 100.\n",
"Kp = 0.01\n",
"Ti = 3600.\n",
"deadband_range = 0.05 # m\n",
"\n",
"# reservoir\n",
@@ -59,9 +60,9 @@
"h_fict = 100\n",
"offset_pressure = rho*g*h_fict\n",
"\n",
"t_max = 1e3 #s\n",
"nt = int(1e6) # number of simulation steps of reservoir in between timesteps of pipeline \n",
"dt = t_max/nt\n",
"t_max = 1e4 #s\n",
"dt = 1e-2 # simulation timestep\n",
"nt = int(t_max//dt) # number of simulation steps of reservoir in between timesteps of pipeline \n",
"\n",
"t_vec = np.arange(0,nt+1,1)*dt\n",
"\n"
@@ -69,25 +70,24 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# create objects\n",
"\n",
"V = Ausgleichsbecken_class(area_base,area_outflux,critical_level_low,critical_level_high,dt)\n",
"V.set_steady_state(initial_influx,initial_level,initial_pressure_unit,conversion_pressure_unit)\n",
"V.set_steady_state(initial_influx,initial_level,conversion_pressure_unit)\n",
"\n",
"T1 = Francis_Turbine(Q_nenn,p_nenn)\n",
"T1 = Francis_Turbine(Q_nenn,p_nenn,closing_time,dt)\n",
"T1.set_steady_state(initial_influx,p0+offset_pressure)\n",
"T1.set_closing_time(500)\n",
"\n",
"Pegelregler = PI_controller_class(target_level,deadband_range,Kp,Ti,dt)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
@@ -96,12 +96,12 @@
"LA_soll_vec = np.full(nt+1,T1.LA)\n",
"Q_vec = np.full(nt+1,initial_influx)\n",
"\n",
"Pegelregler.control_variable = T1.LA"
"Pegelregler.control_variable = T1.get_current_LA()"
]
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -109,106 +109,105 @@
"output_type": "stream",
"text": [
"0.0\n",
"10.0\n",
"20.0\n",
"30.0\n",
"40.0\n",
"50.0\n",
"60.0\n",
"70.0\n",
"80.0\n",
"90.0\n",
"100.0\n",
"110.0\n",
"120.0\n",
"130.0\n",
"140.0\n",
"150.0\n",
"160.0\n",
"170.0\n",
"180.0\n",
"190.0\n",
"200.0\n",
"210.0\n",
"220.0\n",
"230.0\n",
"240.0\n",
"250.0\n",
"260.0\n",
"270.0\n",
"280.0\n",
"290.0\n",
"300.0\n",
"310.0\n",
"320.0\n",
"330.0\n",
"340.0\n",
"350.0\n",
"360.0\n",
"370.0\n",
"380.0\n",
"390.0\n",
"400.0\n",
"410.0\n",
"420.0\n",
"430.0\n",
"440.0\n",
"450.0\n",
"460.0\n",
"470.0\n",
"480.0\n",
"490.0\n",
"500.0\n",
"510.0\n",
"520.0\n",
"530.0\n",
"540.0\n",
"550.0\n",
"560.0\n",
"570.0\n",
"580.0\n",
"590.0\n",
"600.0\n",
"610.0\n",
"620.0\n",
"630.0\n",
"640.0\n",
"650.0\n",
"660.0\n",
"670.0\n",
"680.0\n",
"690.0\n",
"700.0\n",
"710.0\n",
"720.0\n",
"730.0\n",
"740.0\n",
"750.0\n",
"760.0\n",
"770.0\n",
"780.0\n",
"790.0\n",
"800.0\n",
"810.0\n",
"820.0\n",
"830.0\n",
"840.0\n",
"850.0\n",
"860.0\n",
"870.0\n",
"880.0\n",
"890.0\n",
"900.0\n",
"910.0\n",
"920.0\n",
"930.0\n",
"940.0\n",
"950.0\n",
"960.0\n",
"970.0\n",
"980.0\n",
"990.0\n",
"1000.0\n"
"1000.0\n",
"1100.0\n",
"1200.0\n",
"1300.0\n",
"1400.0\n",
"1500.0\n",
"1600.0\n",
"1700.0\n",
"1800.0\n",
"1900.0\n",
"2000.0\n",
"2100.0\n",
"2200.0\n",
"2300.0\n",
"2400.0\n",
"2500.0\n",
"2600.0\n",
"2700.0\n",
"2800.0\n",
"2900.0\n",
"3000.0\n",
"3100.0\n",
"3200.0\n",
"3300.0\n",
"3400.0\n",
"3500.0\n",
"3600.0\n",
"3700.0\n",
"3800.0\n",
"3900.0\n",
"4000.0\n",
"4100.0\n",
"4200.0\n",
"4300.0\n",
"4400.0\n",
"4500.0\n",
"4600.0\n",
"4700.0\n",
"4800.0\n",
"4900.0\n",
"5000.0\n",
"5100.0\n",
"5200.0\n",
"5300.0\n",
"5400.0\n",
"5500.0\n",
"5600.0\n",
"5700.0\n",
"5800.0\n",
"5900.0\n",
"6000.0\n",
"6100.0\n",
"6200.0\n",
"6300.0\n",
"6400.0\n",
"6500.0\n",
"6600.0\n",
"6700.0\n",
"6800.0\n",
"6900.0\n",
"7000.0\n",
"7100.0\n",
"7200.0\n",
"7300.0\n",
"7400.0\n",
"7500.0\n",
"7600.0\n",
"7700.0\n",
"7800.0\n",
"7900.0\n",
"8000.0\n",
"8100.0\n",
"8200.0\n",
"8300.0\n",
"8400.0\n",
"8500.0\n",
"8600.0\n",
"8700.0\n",
"8800.0\n",
"8900.0\n",
"9000.0\n",
"9100.0\n",
"9200.0\n",
"9300.0\n",
"9400.0\n",
"9500.0\n",
"9600.0\n",
"9700.0\n",
"9800.0\n",
"9900.0\n"
]
}
],
@@ -220,31 +219,31 @@
" if np.mod(i,1e4) == 0:\n",
" print(t_vec[i])\n",
"\n",
" if t_vec[i] == 0.4*np.max(t_vec):\n",
" V.influx = 0\n",
" if i == 0.4*(nt+1):\n",
" V.set_influx(0.)\n",
"\n",
" p = rho*g*V.level-0.5*rho*(V.outflux_vel)**2\n",
"\n",
" LA_soll = Pegelregler.get_control_variable(V.level)\n",
" T1.change_LA(LA_soll,dt)\n",
" p = V.get_current_pressure()\n",
" Pegelregler.update_control_variable(V.level)\n",
" LA_soll = Pegelregler.get_current_control_variable()\n",
" T1.update_LA(LA_soll)\n",
" T1.set_pressure(p+offset_pressure)\n",
" LA_soll_vec[i] = LA_soll\n",
" LA_ist_vec[i] = T1.LA\n",
" Q_vec[i] = T1.get_Q(p+offset_pressure)\n",
" LA_ist_vec[i] = T1.get_current_LA()\n",
" Q_vec[i] = T1.get_current_Q()\n",
"\n",
" V.pressure = p\n",
" V.outflux_vel = 1/V.area_outflux*Q_vec[i]\n",
" \n",
" V.set_outflux(Q_vec[i])\n",
"\n",
" V.e_RK_4() \n",
" V.level = V.update_level(V.timestep) \n",
" V.set_volume() \n",
" level_vec[i] = V.level \n",
" V.timestep_reservoir_evolution() \n",
" \n",
" level_vec[i] = V.get_current_level()\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
@@ -257,7 +256,7 @@
"axs1[0].set_xlabel(r'$t$ [$\\mathrm{s}$]')\n",
"axs1[0].set_ylabel(r'$h$ [$\\mathrm{m}$]')\n",
"axs1[0].plot(t_vec,level_vec)\n",
"axs1[0].set_ylim([0.85*initial_level,1.05*initial_level])\n",
"axs1[0].set_ylim([0*initial_level,1.5*initial_level])\n",
"axs1[1].set_title('Flux')\n",
"axs1[1].set_xlabel(r'$t$ [$\\mathrm{s}$]')\n",
"axs1[1].set_ylabel(r'$Q$ [$\\mathrm{m} / \\mathrm{s}^3$]')\n",
@@ -265,51 +264,33 @@
"axs1[1].set_ylim([0,2*initial_influx])\n",
"axs1[2].set_title('LA')\n",
"axs1[2].set_xlabel(r'$t$ [$\\mathrm{s}$]')\n",
"axs1[2].set_ylabel(r'$LA$ [\\%]')\n",
"axs1[2].set_ylabel(r'$LA$ [%]')\n",
"axs1[2].plot(t_vec,LA_soll_vec)\n",
"axs1[2].plot(t_vec,LA_ist_vec)\n",
"axs1[2].set_ylim([0,1])\n",
"fig1.tight_layout()\n",
"fig1.show()\n",
"plt.pause(1)"
"fig1.show()\n"
]
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[<matplotlib.lines.Line2D at 0x26263b78be0>]"
"[<matplotlib.lines.Line2D at 0x1caf15caca0>]"
]
},
"execution_count": 40,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fig2 = plt.figure()\n",
"plt.plot(t_vec,Pegelregler.error_history[1:])"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[8. 8. 8. ... 7.21126138 7.21126138 7.21126138]\n"
]
}
],
"source": [
"print(level_vec[:])"
"plt.plot(t_vec,Pegelregler.get_error_history())"
]
}
],

View File

@@ -2,13 +2,13 @@ import numpy as np
#based on https://en.wikipedia.org/wiki/PID_controller#Discrete_implementation
def trap_int(vec,timestep):
# numerical integration via the trapeziod rule to calculate the performance parameters
l = np.size(vec)
int = 0
for i in range(l-1):
int = int + (vec[i]+vec[i+1])/2*timestep
return int
def ISE_fun(error_history,timestep):
# calcuate the integral of square error
e = np.array(error_history)
@@ -74,61 +74,50 @@ class P_controller_class:
class PI_controller_class:
def __init__(self,setpoint,deadband,proportionality_constant,Ti, timestep):
# init
def __init__(self,setpoint,deadband,proportionality_constant,Ti,timestep,lower_limit=0.,upper_limit=1.):
self.SP = setpoint
self.db = deadband
self.Kp = proportionality_constant
self.Ti = Ti
self.Ti = Ti # integration time
self.dt = timestep
self.error_history = [0]
# use a list to be able to append more easily - will get converted to np.array when needed
self.error_history = [0]
self.cv_lower_limit = 0 # default
self.cv_upper_limit = +1 # default
self.control_variable = -99
self.cv_lower_limit = lower_limit # limits for the controll variable
self.cv_upper_limit = upper_limit # limits for the controll variable
def set_control_variable_limits(self,lower_limit,upper_limit):
self.cv_lower_limit = lower_limit
self.cv_upper_limit = upper_limit
# setter
def set_setpoint(self,setpoint):
self.SP = setpoint
def calculate_error(self,process_variable):
self.error = process_variable-self.SP
self.error_history.append(self.error)
def set_control_variable(self,control_variable, display_warning=True):
if display_warning == True and self.control_variable != -99:
print('WARNING! You are setting the control variable of the PI controller manually \
and are not using the .update_controll_variable() method')
self.control_variable = control_variable
def get_control_variable(self,process_variable):
self.calculate_error(process_variable)
cv = self.control_variable
Kp = self.Kp
Ti = self.Ti
dt = self.dt
e0 = self.error_history[-1]
e1 = self.error_history[-2]
if abs(self.error) > self.db:
new_control = cv+Kp*(e0-e1)+dt/Ti*e0
else:
new_control = cv
if new_control < self.cv_lower_limit:
new_control = self.cv_lower_limit
if new_control > self.cv_upper_limit:
new_control = self.cv_upper_limit
self.control_variable = new_control
# getter
def get_current_control_variable(self):
return self.control_variable
def get_error_history(self):
return self.error_history[1:]
def get_performance_indicators(self,ISE=True,IAE=True,ITSE=True,ITAE=True):
# calculate and return the performance indicators of the error history
ise = np.nan
iae = np.nan
itse = np.nan
itae = np.nan
# self.error_history[1:] because the first value of the error history is set to [0]
# to avoid special case handling in the calculation of the controll variable
if ISE == True:
# to avoid special case handling in the calculation of the control variable
if ISE == True:
ise = ISE_fun(self.error_history[1:],self.dt)
if IAE == True:
if IAE == True:
iae = IAE_fun(self.error_history[1:],self.dt)
if ITSE == True:
itse = ITSE_fun(self.error_history[1:],self.dt)
@@ -137,4 +126,58 @@ class PI_controller_class:
return ise,iae,itse,itae
def get_info(self):
new_line = '\n'
# :<10 pads the self.value to be 10 characters wide
print_str = (f"Turbine has the following attributes: {new_line}"
f"----------------------------- {new_line}"
f"Type = PI Controller {new_line}"
f"Setpoint = {self.SP:<10} {new_line}"
f"Deadband = {self.db:<10} {new_line}"
f"Proportionality constant = {self.Kp:<10} {new_line}"
f"Integration time = {self.Ti:<10} [s] {new_line}"
f"Current control variable = {round(self.control_variable,3):<10} {new_line}"
f"Lower limit CV = {self.cv_lower_limit:<10} {new_line}"
f"Upper limit CV = {self.cv_upper_limit:<10} {new_line}"
f"Simulation timestep = {self.dt:<10} [s] {new_line}"
f"----------------------------- {new_line}")
print(print_str)
# methods
def calculate_error(self,process_variable):
# calculate the error and expand the err history
self.error = process_variable-self.SP
self.error_history.append(self.error)
def update_control_variable(self,process_variable):
# calculate the current control variable and make sure it does not exceed the limits
self.calculate_error(process_variable)
# initialize some variables
cv = self.control_variable
Kp = self.Kp
Ti = self.Ti
dt = self.dt
e0 = self.error_history[-1]
e1 = self.error_history[-2]
# test if the error exceeds the deadband range
# only if that is the case, change control variable
if abs(self.error) > self.db:
new_control = cv+Kp*(e0-e1)+dt/Ti*e0
else:
new_control = cv
# ensure that the controll variable stays within the predefined limits
if new_control < self.cv_lower_limit:
new_control = self.cv_lower_limit
if new_control > self.cv_upper_limit:
new_control = self.cv_upper_limit
# set the control variable attribute
self.set_control_variable(new_control,display_warning=False)