简体   繁体   中英

How to introduce abrupt jumps while solving coupled differential equations?

I have been trying to solve the following system of three coupled differential equations using odeint and solve_ivp. (This is not the exact system. I have modified it just to improve the readability of the question.)

def model(x,t):    # x is a list containing the values of u,v,w as they change with time t 
    u = x[0]
    v = x[1]
    w = x[2]
    
    dudt = u - u*v                           
    dvdt = -v + u*v - v*w**2          
    dwdt = -w + 2*v*w
    
    return [dudt, dvdt, dwdt]

This works fine. But now I want to modify this in the following way: whenever either of u,v or w goes below a threshold, it is reset to zero, and then let the system evolve. This should happen every time any of these three goes before the threshold automatically. The 'rules' of evolving the system remain the same.

I have tried modifying the code by doing this:

def model(x,t):    # x is a list containing the values of u,v,w as they change with time t 
    u = x[0]
    v = x[1]
    w = x[2]
     
    if u < u_threshold:
        x[0] = 0
    
    dudt = u - u*v                           
    dvdt = -v + u*v - vw**2          
    dwdt = -w + 2*v*w
    
    return [dudt, dvdt, dwdt]

I have shown it only for u, but you get the idea. This does not seem to work.

Please note that I cannot afford to stop the simulation every time any variable hits the threshold value, as this is only a toy-model. Later on, I will generalise this to systems of hundreds of coupled equations.

It does not work, as the state-vector x is read-only, there is no side-effect transmitting changes back to the state vector of the integrator.

And even if it did work, it would be a bad idea :

  • The ODE function model is mostly not called on points of the solution curve, and then in the higher order RK methods also not with increasing times.

  • Such a jump would make the ODE highly non-smooth destroying all the assumptions that are used in the time-step-size controller. The result of this can be varied, but all non-favorable towards a minimum-effort integration.

So yes, the best course is indeed to stop the simulation using the event mechanism of solve_ivp or to fashion your own time loop based on the stepper classes that are also behind solve_ivp .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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