-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathEx_CST.py
358 lines (288 loc) · 10.3 KB
/
Ex_CST.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# -*- coding: utf-8 -*-
"""
Created on Mon May 28 13:03:03 2018
@author: Riccardo Bacci di Capaci
CST example
A Continuous Stirred Tank to be identified from input-output data
"""
# import package
from __future__ import division # compatibility layer between Python 2 and Python 3
from past.utils import old_div
from sippy import functionset as fset
from sippy import functionsetSIM as fsetSIM
from sippy import functionset_OPT as fset_OPT
from sippy import *
#
#
import numpy as np
import control.matlab as cnt
import matplotlib.pyplot as plt
# sampling time
ts = 1. # [min]
# time settings (t final, samples number, samples vector)
tfin = 1000
npts = int(old_div(tfin,ts)) + 1
Time = np.linspace(0, tfin, npts)
# Data
V = 10.0 # tank volume [m^3] --> assumed to be constant
ro = 1100.0 # solution density [kg/m^3] --> assumed to be constant
cp = 4.180 # specific heat [kJ/kg*K] --> assumed to be constant
Lam = 2272.0 # latent heat [kJ/kg] --> assumed to be constant (Tvap = 100°C, Pvap = 1atm)
# initial conditions
# Ca_0
# Tin_0
# VARIABLES
# 4 Inputs
# - as v. manipulated
# Input Flow rate Fin [m^3/min]
# Steam Flow rate W [kg/min]
# - as disturbances
# Input Concentration Ca_in [kg salt/m^3 solution]
# Input Temperature T_in [°C]
# U = [F, W, Ca_in, T_in]
m = 4
# 2 Outputs
# Output Concentration Ca [kg salt/m^3 solution] (Ca = Ca_out)
# Output Temperature T [°C] (T = T_out)
# X = [Ca, T]
p = 2
# Function with Nonlinear System Dynamics
def Fdyn(X,U):
# Balances
# V is constant ---> perfect Level Control
# ro*F_in = ro*F_out = ro*F --> F = F_in = F_out at each instant
# Mass Balance on A
# Ca_in*F - Ca*F = V*dCA/dt
#
dx_0 = (U[2]*U[0] - X[0]*U[0])/V
# Energy Balance
# ro*cp*F*T_in - ro*cp*F*T + W*Lam = (V*ro*cp)*dT/dt
#
dx_1 = (ro*cp*U[0]*U[3] - ro*cp*U[0]*X[1] + U[1]*Lam)/(V*ro*cp)
fx = np.append(dx_0,dx_1)
return fx
# Build input sequences
U = np.zeros((m,npts))
# manipulated inputs as GBN
# Input Flow rate Fin = F = U[0] [m^3/min]
prob_switch_1 = 0.05
F_min = 0.4
F_max = 0.6
Range_GBN_1 = [F_min,F_max]
[U[0,:],_,_] = fset.GBN_seq(npts, prob_switch_1, Range = Range_GBN_1)
# Steam Flow rate W = U[1] [kg/min]
prob_switch_2 = 0.05
W_min = 20
W_max = 40
Range_GBN_2 = [W_min,W_max]
[U[1,:],_,_] = fset.GBN_seq(npts, prob_switch_2, Range = Range_GBN_2)
# disturbance inputs as RW (random-walk)
# Input Concentration Ca_in = U[2] [kg salt/m^3 solution]
Ca_0 = 10.0 # initial condition
sigma_Ca = 0.01 # variation
U[2,:] = fset.RW_seq(npts, Ca_0, sigma = sigma_Ca)
# Input Temperature T_in [°C]
Tin_0 = 25.0 # initial condition
sigma_T = 0.01 # variation
U[3,:] = fset.RW_seq(npts, Tin_0, sigma = sigma_T)
##### COLLECT DATA
# Output Initial conditions
Caout_0 = Ca_0
Tout_0 = (ro*cp*U[0,0]*Tin_0 + U[1,0]*Lam)/(ro*cp*U[0,0])
Xo1 = Caout_0*np.ones((1,npts))
Xo2 = Tout_0*np.ones((1,npts))
X = np.vstack((Xo1,Xo2))
# Run Simulation
for j in range(npts-1):
# Explicit Runge-Kutta 4 (TC dynamics is integrateed by hand)
Mx = 5 # Number of elements in each time step
dt = ts/Mx # integration step
# Output & Input
X0k = X[:,j]
Uk = U[:,j]
# Integrate the model
for i in range(Mx):
k1 = Fdyn(X0k, Uk)
k2 = Fdyn(X0k + dt/2.0*k1, Uk)
k3 = Fdyn(X0k + dt/2.0*k2, Uk)
k4 = Fdyn(X0k + dt*k3, Uk)
Xk_1 = X0k + (dt/6.0)*(k1 + 2.0*k2 + 2.0*k3 + k4)
X[:,j+1] = Xk_1
# Add noise (with assigned variances)
var = [0.001, 0.001]
noise = fset.white_noise_var(npts,var)
# Build Output
Y = X + noise
#### IDENTIFICATION STAGE (Linear Models)
# Orders
na_ords = [2,2]
nb_ords = [[1,1,1,1], [1,1,1,1]]
nc_ords = [1,1]
nd_ords = [1,1]
nf_ords = [2,2]
theta = [[1,1,1,1], [1,1,1,1]]
# Number of iterations
n_iter = 300
# IN-OUT Models: ARX - ARMAX - OE - BJ - GEN
Id_ARX = system_identification(Y, U, 'ARX', centering = 'MeanVal', ARX_orders = [na_ords, nb_ords, theta])
Id_ARMAX = system_identification(Y, U, 'ARMAX', centering = 'MeanVal',
ARMAX_orders = [na_ords, nb_ords, nc_ords, theta], max_iterations = n_iter, ARMAX_mod = 'OPT')
Id_OE = system_identification(Y, U, 'OE', centering = 'MeanVal', OE_orders = [nb_ords, nf_ords, theta], max_iterations = n_iter)
Id_BJ = system_identification(Y, U, 'BJ', centering = 'MeanVal',
BJ_orders = [nb_ords, nc_ords, nd_ords, nf_ords, theta], max_iterations = n_iter, stab_cons = True)
Id_GEN = system_identification(Y, U, 'GEN', centering = 'MeanVal',
GEN_orders = [na_ords, nb_ords, nc_ords, nd_ords, nf_ords, theta],
max_iterations = n_iter, stab_cons = True, stab_marg = 0.98)
# SS - mimo
# choose method
method = 'PARSIM-K'
SS_ord = 2
Id_SS = system_identification(Y, U, method, SS_fixed_order = SS_ord)
# GETTING RESULTS (Y_id)
# IN-OUT
Y_arx = Id_ARX.Yid
Y_armax = Id_ARMAX.Yid
Y_oe = Id_OE.Yid
Y_bj = Id_BJ.Yid
Y_gen = Id_GEN.Yid
# SS
x_ss, Y_ss = fsetSIM.SS_lsim_process_form(Id_SS.A,Id_SS.B,Id_SS.C,Id_SS.D,U,Id_SS.x0)
##### PLOTS
# Input
plt.close('all')
plt.figure(1)
str_input = ['F [m$^3$/min]', 'W [kg/min]', 'Ca$_{in}$ [kg/m$^3$]', 'T$_{in}$ [$^o$C]']
for i in range(m):
plt.subplot(m,1,i+1)
plt.plot(Time,U[i,:])
plt.ylabel("Input " + str(i+1))
plt.ylabel(str_input[i])
plt.grid()
plt.xlabel("Time")
plt.axis([0, tfin, 0.95*np.amin(U[i,:]), 1.05*np.amax(U[i,:])])
if i == 0:
plt.title('identification')
# Output
plt.figure(2)
str_output = ['Ca [kg/m$^3$]', 'T [$^o$C]']
for i in range(p):
plt.subplot(p,1,i+1)
plt.plot(Time,Y[i,:])
plt.plot(Time,Y_arx[i,:])
#plt.plot(Time,Y_arma[i,:])
plt.plot(Time,Y_armax[i,:])
# plt.plot(Time,Y_ararx[i,:])
# plt.plot(Time,Y_ararmax[i,:])
plt.plot(Time,Y_oe[i,:])
plt.plot(Time,Y_bj[i,:])
plt.plot(Time,Y_gen[i,:])
plt.plot(Time,Y_ss[i,:])
plt.ylabel("Output " + str(i+1))
plt.ylabel(str_output[i])
plt.legend(['Data','ARX','ARMAX','OE','BJ', 'GEN','SS'])
plt.grid()
plt.xlabel("Time")
if i == 0:
plt.title('identification')
#### VALIDATION STAGE
# Build new input sequences
U_val = np.zeros((m,npts))
# U_val = U.copy()
# manipulated inputs as GBN
# Input Flow rate Fin = F = U[0] [m^3/min]
prob_switch_1 = 0.05
F_min = 0.4
F_max = 0.6
Range_GBN_1 = [F_min,F_max]
[U_val[0,:],_,_] = fset.GBN_seq(npts, prob_switch_1, Range = Range_GBN_1)
# Steam Flow rate W = U[1] [kg/min]
prob_switch_2 = 0.05
W_min = 20
W_max = 40
Range_GBN_2 = [W_min,W_max]
[U_val[1,:],_,_] = fset.GBN_seq(npts, prob_switch_2, Range = Range_GBN_2)
# disturbance inputs as RW (random-walk)
# Input Concentration Ca_in = U[2] [kg salt/m^3 solution]
Ca_0 = 10.0 # initial condition
sigma_Ca = 0.02 # variation
U_val[2,:] = fset.RW_seq(npts, Ca_0, sigma = sigma_Ca)
# Input Temperature T_in [°C]
Tin_0 = 25.0 # initial condition
sigma_T = 0.1 # variation
U_val[3,:] = fset.RW_seq(npts, Tin_0, sigma = sigma_T)
#### COLLECT DATA
# Output Initial conditions
Caout_0 = Ca_0
Tout_0 = (ro*cp*U[0,0]*Tin_0 + U[1,0]*Lam)/(ro*cp*U[0,0])
Xo1 = Caout_0*np.ones((1,npts))
Xo2 = Tout_0*np.ones((1,npts))
X_val = np.vstack((Xo1,Xo2))
# Run Simulation
for j in range(npts-1):
# Explicit Runge-Kutta 4 (TC dynamics is integrateed by hand)
Mx = 5 # Number of elements in each time step
dt = ts/Mx # integration step
# Output & Input
X0k = X_val[:,j]
Uk = U_val[:,j]
# Integrate the model
for i in range(Mx):
k1 = Fdyn(X0k, Uk)
k2 = Fdyn(X0k + dt/2.0*k1, Uk)
k3 = Fdyn(X0k + dt/2.0*k2, Uk)
k4 = Fdyn(X0k + dt*k3, Uk)
Xk_1 = X0k + (dt/6.0)*(k1 + 2.0*k2 + 2.0*k3 + k4)
X_val[:,j+1] = Xk_1
# Add noise (with assigned variances)
var = [0.01, 0.05]
noise_val = fset.white_noise_var(npts,var)
# Build Output
Y_val = X_val + noise_val
# MODEL VALIDATION
# IN-OUT Models: ARX - ARMAX - OE - BJ
Yv_arx = fset.validation(Id_ARX,U_val,Y_val,Time, centering = 'MeanVal')
Yv_armax = fset.validation(Id_ARMAX,U_val,Y_val,Time,centering = 'MeanVal')
Yv_oe = fset.validation(Id_OE,U_val,Y_val,Time,centering = 'MeanVal')
Yv_bj = fset.validation(Id_BJ,U_val,Y_val,Time,centering = 'MeanVal')
Yv_gen = fset.validation(Id_GEN,U_val,Y_val,Time, centering = 'MeanVal')
# SS
x_ss, Yv_ss = fsetSIM.SS_lsim_process_form(Id_SS.A,Id_SS.B,Id_SS.C,Id_SS.D,U_val,Id_SS.x0)
##### PLOTS
# Input
plt.figure(3)
str_input = ['F [m$^3$/min]', 'W [kg/min]', 'Ca$_{in}$ [kg/m$^3$]', 'T$_{in}$ [$^o$C]']
for i in range(m):
plt.subplot(m,1,i+1)
plt.plot(Time,U_val[i,:])
# plt.ylabel("Input " + str(i+1))
plt.ylabel(str_input[i])
plt.grid()
plt.xlabel("Time")
plt.axis([0, tfin, 0.95*np.amin(U_val[i,:]), 1.05*np.amax(U_val[i,:])])
if i == 0:
plt.title('validation')
# Output
plt.figure(4)
str_output = ['Ca [kg/m$^3$]', 'T [$^o$C]']
for i in range(p):
plt.subplot(p,1,i+1)
plt.plot(Time,Y_val[i,:])
#plt.plot(Time,Yv_fir[i,:])
plt.plot(Time,Yv_arx[i,:])
# plt.plot(Time,Yv_arma[i,:])
plt.plot(Time,Yv_armax[i,:])
# plt.plot(Time,Yv_ararx[i,:])
# plt.plot(Time,Yv_ararmax[i,:])
plt.plot(Time,Yv_oe[i,:])
plt.plot(Time,Yv_bj[i,:])
plt.plot(Time,Yv_gen[i,:])
plt.plot(Time,Yv_ss[i,:])
# plt.ylabel("Output " + str(i+1))
plt.ylabel(str_output[i])
plt.legend(['Data','ARX','ARMAX','OE','BJ','GEN','SS'])
# plt.legend(['Data','ARX','ARMAX','GEN','SS'])
#plt.legend(['Data','ARMAX'])
plt.grid()
plt.xlabel("Time")
if i == 0:
plt.title('validation')