2

I'm trying to solve a DAE in Gekko, where some of the components will depend upon the solution to a convolution integral

Integral

This requires a constant dt, but I'm sure that's somewhere in the options. Consequently, what I want to do is use a function to record the current value of a state variable in an array, and return the sum up to that point. Here was my attempt using one of the simple ODE examples:

import numpy as np
from gekko import GEKKO
import matplotlib.pyplot as plt

m = GEKKO()    
class adder:
    def __init__(self,):
        self.i=m.Array(m.Param, 10)
        self.count=0
    def f(self, y):
        self.i[self.count]=y
        self.count+=1
        return sum(self.i)
a=adder()
m.Equation(y.dt()==-y+1+a.f(y)) 
m.time = np.linspace(0,5) 
m.options.IMODE = 4
m.solve()

but the solution 1) looks incorrect and 2) I can't print anything about the solution using the a.f() function and 3) even when I set the size of the self.i array to 1, it doesn't throw an out of bounds error so I expect it's not being called in the way I think. I've also seen people suggest using the delay() function, but I don't know how to access which timestep I'm currently in and to loop over each previous timestep.

John Hedengren
  • 12,068
  • 1
  • 21
  • 25
HLL
  • 365
  • 2
  • 8

1 Answers1

0

Mixing continuous differential equations with discrete equations can be a challenge. Two recommendations are to either (1) convert the DAEs to a discrete form (such as with Orthogonal Collocation on Finite Elements) or (2) use a continuous integral form of the convolution integral. The m.integral() function is available in Gekko to help with any integral expressions. See How to solve integral in Python Gekko? and Integral Objective (Luus) for two examples of solving problems with continuous integrals.

Gekko calls functions only once when building the model. The model file is found in m.path as gk0_model.apm (text file) or by opening the folder with m.open_folder(). The model must be expressed symbolically so that it can be compiled for automatic differentiation.

John Hedengren
  • 12,068
  • 1
  • 21
  • 25