Bound an odeint variable

限于喜欢 提交于 2019-12-01 07:10:32

问题


I'm using odeint to simulate a system wherein there are several variables which should not go less than zero.

Is there an appropriate way to bound a variable in odeint to a particular range?


回答1:


There is no such possibility in odeint. And I guess there are no algorithms which could do that. You must somehow encode the bound in your ODE.

If you only want to find a bound during the evolution of your system use a loop like

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

Two side nodes, maybe this is the case for your problem:

  1. There are special algorithms for ODEs with have conservation laws where the algorithm ensures that the conservation law holds, see the symplectic solvers for example.

  2. If you bound is already encode in some way in your ODE and the bound is anyhow reached you must shorten the stepsize of the solver.




回答2:


What you need is sometimes called a "saturation" constraint, a common issue in dynamic systems modeling. You can easily code it inside your equation:

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] = ....
    ...
}


来源:https://stackoverflow.com/questions/14586059/bound-an-odeint-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!