绑定一个 odeint 变量

Bound an odeint variable

本文关键字:一个 odeint 变量 绑定      更新时间:2023-10-16

我正在使用odeint来模拟一个系统,其中有几个变量不应该小于零。

有没有合适的方法将 odeint 中的变量绑定到特定范围?

在odeint中没有这种可能性。我想没有算法可以做到这一点。您必须以某种方式对 ODE 中的绑定进行编码。

如果您只想在系统演变过程中找到一个边界,请使用类似

while( t < tmax )
{
    stepper.do_step( ode , x , t , dt );
    t += dt;
    if( check_bound( x , t ) ) break;
}

两个侧节点,也许您的问题就是这种情况:

    对于
  1. 具有守恒定律的常微分方程,有一些特殊的算法,其中算法确保守恒定律成立,例如参见辛求解器。

  2. 如果绑定已经在 ODE 中以某种方式进行了编码,并且无论如何都达到了边界,则必须缩短求解器的步长。

您需要的有时称为"饱和"约束,这是动态系统建模中的常见问题。您可以轻松地在公式中对其进行编码:

void YourEquation::operator() (const state_type &x, state_type &dxdt, const time_type t)
{
    // suppose that x[0] is the variable that should always be greater/equal 0
    double x0 = x[0]; // or whatever data type you use
    dxdt[0] = .... // part of your equation here
    if (x0 <= 0 && dxdt[0] < 0)
    {
        x0 = 0;
        dxdt[0] = 0
    }
    // the rest of the system equations, use x0 instead of x[0] if necessary, actually it depends on the situation and physical interpretation
    dxdt[1] = ....
    dxdt[2] = ....
    ...
}