简体   繁体   English

在 Gekko 中求解 ODE 时获取以前的值

[英]Get previous values when solving an ODE in Gekko

I'm trying to solve a DAE in Gekko, where some of the components will depend upon the solution to a convolution integral我正在尝试解决 Gekko 中的 DAE,其中一些组件将取决于卷积积分的解决方案

不可缺少的

This requires a constant dt, but I'm sure that's somewhere in the options.这需要一个恒定的 dt,但我确信它在选项中的某个地方。 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.因此,我想要做的是使用 function 将 state 变量的当前值记录在数组中,并返回到该点的总和。 Here was my attempt using one of the simple ODE examples:这是我尝试使用一个简单的 ODE 示例:

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 af() 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.但是解决方案 1) 看起来不正确,并且 2) 我无法使用 af() function 和 3) 打印有关解决方案的任何信息,即使我将self.i数组的大小设置为 1,它也不会抛出边界错误,所以我希望它不会以我认为的方式被调用。 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.我还看到有人建议使用delay() function,但我不知道如何访问我当前所在的时间步长以及循环遍历之前的每个时间步长。

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.两个建议是 (1) 将 DAE 转换为离散形式(例如使用 有限元上的正交配置)或 (2) 使用卷积积分的连续积分形式。 The m.integral() function is available in Gekko to help with any integral expressions. Gekko 中提供了m.integral() function 以帮助处理任何积分表达式。 See How to solve integral in Python Gekko?请参阅如何解决 Python Gekko 中的积分问题? and Integral Objective (Luus) for two examples of solving problems with continuous integrals.Integral Objective (Luus)两个用连续积分解决问题的例子。

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() . Gekko 在构建 model 时仅调用一次函数。model 文件在m.path中作为gk0_model.apm (文本文件)或通过使用m.open_folder()打开文件夹找到。 The model must be expressed symbolically so that it can be compiled for automatic differentiation. model 必须用符号表示,这样才能编译自动微分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM