I am trying to use a cspine interpolation in my gekko model. In this problem there is a power plant, a steam turbine, and a grid. The turbine will have different efficiencies based on the amount of it's capacity being used to meet the grid. I have tried to implement a gekko cspline and then have the model call that to give the efficiency for each time point based on the power production. I haven't been able to get this to work. Is this possible in Gekko?
from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
# Grid demand
t = np.linspace(0, 24, 24)
e_grid = 2.5*np.sin(t/24*(2*np.pi)) + 5
# Turbine Efficiency curve based on turbine capacity
pcap = np.linspace(.1, 1, 10) # %capacity
cap = 10*pcap
turb_eff = .75*np.sin(cap/11*np.pi)
# build model
m = GEKKO(remote=True)
m.time = t
Econs = m.Param(e_grid)
Egen = m.MV(value=5, lb=0, ub=10) # steam production
x = m.Param(value=cap)
y = m.Var()
Turb_spline = m.cspline(x, y, cap, turb_eff)
turb_out = m.Intermediate(Egen*Turb_spline)
m.Equation(Econs == turb_out)
m.Obj(Egen)
m.options.IMODE = 5
m.options.SOLVER = 3
m.solve()
plt.plot(t, Egen.value, label='gen')
plt.plot(t, Econs.value, label='cons')
plt.xlabel('time')
plt.ylabel('Energy')
plt.legend()
I was able to get this to work using np.polyfit. I then was able to add a Polynomial into my gekko model and run it to get the correct adjusting of the efficiencies. I used the following code instead of a cspline.
Ecap = m.Intermediate(Egen/cap)
m.Equation(turb_eff == p[0]*Ecap**5 + p[1]*Ecap**4 + p[2]*Ecap**3 + p[3]*Ecap**2 + p[4]*Ecap + p[5])
turb_out = m.Intermediate(Egen*turb_eff)
m.Equation(turb_out == Econs)
I would still like know how to use a cspline so that I can fit more complicated models that a polynomial is unable to capture.


