因为这是稳态数据,所以不需要时间步长(m.time)。请提供有关如何将下面的模拟代码转换为“ Ca vs. T_reactor”的估算代码的建议。
import numpy as np
import matplotlib.pyplot as plt
from gekko import GEKKO
# Feed Temperature (K)
Tf = 350
# Feed Concentration (mol/m^3)
Caf = 1
# Steady State Initial Conditions for the States
Ca_ss = 1
T_ss = 304
#%% GEKKO
m = GEKKO(remote=True)
m.time = np.linspace(0, 25, 251)
# Volumetric Flowrate (m^3/sec)
q = 100
# Volume of CSTR (m^3)
V = 100
# Density of A-B Mixture (kg/m^3)
rho = 1000
# Heat capacity of A-B Mixture (J/kg-K)
Cp = 0.239
# Heat of reaction for A->B (J/mol)
mdelH = 5e4
# E - Activation energy in the Arrhenius Equation (J/mol)
# R - Universal Gas Constant = 8.31451 J/mol-K
EoverR = 8700
# Pre-exponential factor (1/sec)
k0 = 3.2e15
# U - Overall Heat Transfer Coefficient (W/m^2-K)
# A - Area - this value is specific for the U calculation (m^2)
UA = 5e4
# initial conditions = 280
T0 = 304
Ca0 = 1.0
T = m.MV(value=T_ss)
rA = m.Var(value=0)
Ca = m.CV(value=Ca_ss)
m.Equation(rA == k0*m.exp(-EoverR/T)*Ca)
m.Equation(Ca.dt() == q/V*(Caf - Ca) - rA)
m.options.IMODE = 1
m.options.SOLVER = 3
T_reactor = np.linspace(220, 260, 11)
Ca_results = np.zeros(np.size(T_reactor))
for i in range(np.size(T_reactor)):
T.Value = T_reactor[i]
m.solve(disp=True)
Ca_results[i] = Ca[-1]
Ca_data = -1.5*T_reactor/100 + 4.2 # for generating the operation data
# Plot the results
plt.plot(T_reactor,Ca_data,'bo',linewidth=3)
plt.plot(T_reactor,Ca_results,'r-',linewidth=3)
plt.ylabel('Ca (mol/L)')
plt.xlabel('Temperature (K)')
plt.legend(['Reactor Concentration'],loc='best')
plt.show()
答案 0 :(得分:2)
在Gekko中有一个steady state estimation mode(IMODE=2
)用于线性或非线性回归。两个示例是nonlinear regression和energy price regression。对于发布的问题,这里有一些建议:
m.FV()
类型,对于每个时间点具有不同值的参数应该是m.MV()
。Ca.FSTATUS=1
告诉求解器它应该尝试将Ca
的预测与Ca.value
中加载的数据进行匹配。kf.STATUS=1
告诉求解器,它是一个参数,应该对其进行调整以最大程度地减少Ca
误差。kf
(而不是k0
)设为可调参数,以改善问题的解决范围。较大的值(例如> 1e10或<-1e10)会给求解器带来问题(没有自动缩放),因为梯度很小。我创建了一个新参数kf
作为中间变量,也删除了一个附加方程。 import numpy as np
import matplotlib.pyplot as plt
from gekko import GEKKO
m = GEKKO(remote=True)
Tf = 350
Caf = 1
q = 100
V = 100
rho = 1000
Cp = 0.239
mdelH = 5e4
EoverR = 8700
k0 = 3.2e15
UA = 5e4
T = m.MV()
Ca = m.CV()
# new parameter to estimate
kf = m.FV(1,lb=0.5,ub=2.0)
kf.STATUS = 1
rA = m.Intermediate(kf*k0*m.exp(-EoverR/T)*Ca)
m.Equation(Ca.dt() == q/V*(Caf - Ca) - rA)
m.options.IMODE = 2
m.options.SOLVER = 3
# generate data
T_reactor = np.linspace(220, 260, 11)
Ca_data = -1.5*T_reactor/100 + 4.2
# insert data
T.value = T_reactor
Ca.value = Ca_data
Ca.FSTATUS = 1 # fit Ca
m.solve()
print('kf = ' + str(kf.value[0]))
print('k = ' + str(kf.value[0]*k0))
# Plot the results
plt.plot(T_reactor,Ca_data,'bo',linewidth=3)
plt.plot(T_reactor,Ca.value,'r-',linewidth=3)
plt.ylabel('Ca (mol/L)')
plt.xlabel('Temperature (K)')
plt.legend(['Reactor Concentration','Regression Fit'],loc='best')
plt.show()
您可以选择任意数量的参数进行估算以提高拟合度。它并不仅限于kf
。您的帖子中提到EoverR
是另一个可能估计的参数,但是由于k0
和EoverR
是共线性的,因此可能无法显着提高拟合度。可以增加或减少这两个参数,并给出几乎相同的解决方案。提醒一下,必须对温度进行重大估算才能估算出两者。