Gekko and CoolProp

ぐ巨炮叔叔 提交于 2020-08-08 06:03:34

问题


I'm using GEKKO and CoolProp for thermal systems simulation. When trying to use CoolProp's functions inside the model equations (as shown below for an isentropic expansion) I'm getting an error message regarding the variable type: "must be real number, not GKVariable". Could someone please help me with this issue?

from gekko import GEKKO
import CoolProp.CoolProp as CP 
#
p1 = 2e5
T1 = 300.0 + 273.15
p2 = 1e5
eta = 0.80
fluid = 'H2O'
#
h1 = CP.PropsSI('H','T',T1,'P',p1,fluid)
s1 = CP.PropsSI('S','T',T1,'P',p1,fluid)
#
m = GEKKO()
h2 = m.Var()
h2s = m.Var()
T2 = m.Var()
#
m.Equation(eta * (h1 - h2) - (h1 - h2s) == 0)
m.Equation(h2s - CP.PropsSI('H','S',s1,'P',p2,fluid) == 0)
m.Equation(h2 - CP.PropsSI('H','T',T2,'P',p2,fluid) == 0)
#
m.options.IMODE = 1  #Steady state
m.options.SOLVER = 3 # solver (IPOPT) 
m.solve(disp=False)
#
print(T2.value[0])

Thanks in advance.


回答1:


Gekko needs to have the CoolProp equations to perform automatic differentiation. If it doesn't have the equation then you could use a cspline (cubic spline documentation).

from gekko import GEKKO
import CoolProp.CoolProp as CP 
import numpy as np
#
p1 = 2e5
T1 = 300.0 + 273.15
p2 = 1e5
eta = 0.80
fluid = 'H2O'
# constants
h1 = CP.PropsSI('H','T',T1,'P',p1,fluid)
s1 = CP.PropsSI('S','T',T1,'P',p1,fluid)
c2 = CP.PropsSI('H','S',s1,'P',p2,fluid)
# gekko model
m = GEKKO(remote=False)
h2 = m.Var()
h2s = m.Var()
T2 = m.Var()
# build cubic spline
n = 100
T = np.linspace(373,1000,100)
h = [CP.PropsSI('H','T',Ti,'P',p1,fluid) for Ti in T]
m.cspline(T2,h2,T,h)
#
m.Equation(eta * (h1 - h2) - (h1 - h2s) == 0)
m.Equation(h2s - c2 == 0)
#
m.options.IMODE = 1  #Steady state
m.options.SOLVER = 3 # solver (IPOPT) 
m.solve(disp=False)
#
print(T2.value[0])

This produces the solution:

T2 = 468.52459939


来源:https://stackoverflow.com/questions/62832981/gekko-and-coolprop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!