I am trying to solve a differential problem with Gekko for the first time. I have defined the parameters with CoolProp as suggested here: Gekko and CoolProp I would like to have a nice plot of T over time.
import CoolProp.CoolProp as CP
from gekko import GEKKO
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
import numpy as np
m = GEKKO(remote=False)
m.time = np.arange(0,100,1)
#variables
T = m.Var(value=78+273)
p = m.Var()
d = m.Var()
c = m.Var()
# build cubic spline
T_data = np.arange(77.5+273,98.5+273,1)
p_data = [CP.PropsSI('P','T',Ti,'Q',1,"Water") for Ti in T_data]
d_data = [CP.PropsSI('D','T',Ti,'Q',1,"Water") for Ti in T_data]
c_data = [CP.PropsSI('O','T',Ti,'Q',1,"Water") for Ti in T_data]
m.cspline(T,p,T_data,p_data)
m.cspline(T,d,T_data,d_data)
m.cspline(T,c,T_data,c_data)
#equations
def x(d,c,T):
return (50 + 0.1/d*T)/(1000*c)
m.Equation( T.dt() == x(d,c,T) )
m.options.IMODE = 4
m.options.SOLVER = 3 # solver (IPOPT)
#m.options.coldstart = 1
m.solve(disp=False)
fig, ax = plt.subplots()
ax.plot(m.time, y.value)
ax.set(xlabel='time (s)', ylabel='T',
title='gekko test')
ax.grid()
plt.show()
however, I get an error
Exception: @error: Equation Definition Equation without an equality (=) or inequality (>,<) (80.0/v21)(80.5/v21)(81.0/v21)(81.5/v21)(82.0/v21)(82.5/v21) STOPPING..
The same can be solved with scipy as follows:
def x(t,T):
p = CP.PropsSI('P','T',T,'Q',1,"Water")
d = CP.PropsSI('D','T',T,'Q',1,"Water")
c = CP.PropsSI('O','T',T,'Q',1,"Water")[1.0]).c
dTdt = (50 + 0.1/d*T)/(1000*c)
return dTdt
y0 = [78+273]
sol = solve_ivp(x, (0,100), y0, method='LSODA', rtol=1e-6)
fig, ax = plt.subplots()
ax.plot(sol.t, sol.y[0])
ax.set(xlabel='time (s)', ylabel='T',
title='scipy test')
ax.grid()
plt.show()
The infeasibilities.txt shows (I believe) that there is something wrong with the spline?
***** POSSIBLE INFEASBILE EQUATIONS ************
EQ Number | Lower | Residual | Upper | Infeas. | Name |
---|---|---|---|---|---|
1 | 0.0000E+00 | 8.0315E+02 | 0.0000E+00 | -8.0315E+02 | p(1).n(2).cspline1.cspline: y = p_0 + p_1 (x-x_i) + p_2 (x-x_i)^2 + p_3 (x-x_i)^3 |
Variable | Lower | Value | Upper | $Value | Name |
1 | -1.2346E+20 | 7.8000E+01 | 1.2346E+20 | 0.0000E+00 | p(1).n(2).v1 |
3 | -1.2346E+20 | 0.0000E+00 | 1.2346E+20 | 0.0000E+00 | p(1).n(2).v3 |
Any idea what causes the error and how I can get the model solved successfully?
Additionally, how do I include CoolProp data with two variables in an efficient and successful way? This doesn't seem to work properly:
T_data = np.arange(77.5+273,98.5+273,1)
D_data = np.arange(3,1004,1)
p_data=[]
for i in T_data:
row = []
for n in D_data:
row.append(CP.PropsSI('P','T',i,'D',n,"Water"))
p_data.append(row)
m.bspline(T1,D1,p1,T_data,D_data,p_data)
it results in en error
Exception: @error: Bspline Config bspline in bspline1_info.csv needs 3 lines: kx,ky,sf STOPPING...
It is a test run and familiarization with Gekko for the future more complex DAE problem, which cannot be solved with scipy. Any help is much appreciated.
EDIT 22.08 Thank you, Mr. Hedengren, for the answer below! Sadly, I still get an error when I try to do the same thing with other data, such as p or h. Simplified example:
m = GEKKO(remote=False)
u1 = m.Param(value=-81e3)
D1 = m.Param(value=808)
h1 = m.Var()
D_data = np.array([807.5, 808.0, 808.5, 809.0,809.5])
u_data = np.array([-121000, -111000, -101000, -91000,-81000])
h_data = []
for n in D_data:
row = []
for i in u_data:
row.append(CP.PropsSI('H','U',i,'D',n,"Nitrogen"))
h_data.append(row)
# Define the B-spline interpolation
m.bspline(D1, u1, h1, D_data, u_data, h_data)
# Set solver options
m.options.SOLVER = 3
m.options.DIAGLEVEL = 10
# Solve the optimization problem
m.solve()
Could someone perhaps help me understand why the solution doesn't work in this case?