问题
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