Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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] = ....
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top