diffrax icon indicating copy to clipboard operation
diffrax copied to clipboard

ODE solver: some time period has algebraic solution

Open timnotavailable opened this issue 5 months ago • 4 comments

Hello Patrick, I met a problem on realizing such function:

Suppose I have such ODE function: From t0 to t1 I have dy/dx=f(t,y), in this period of time I have to use numerical ODE solver to solve until I get y(t1), from t1 to t2 I have algebraic formula to compute any t between t1 and t2, so I could easily use the formula to get y(t2). From t2 to t3 again I have to use ODE solver numerically.......

Do you have any idea on how to code with the diffrax? And another point , I also want to realize that one can calculate the sensitivities, how should I do it?

timnotavailable avatar Aug 03 '25 16:08 timnotavailable

I think you should be able to do this by just wrapping a diffeqsolve + algebraic solution in a lax.scan. :)

patrick-kidger avatar Aug 03 '25 19:08 patrick-kidger

hi, I guess you are referring to this logic: (for simplicity I use loop to substitute lax.scan): loop the following: sol1=diffeqsolve (term,t0,t1,y0,solver,args,saveat) ## t0 to t1 ## numerical solve sol2=algebra_fun (t1,t2,sol1.solver_state,args,saveat) ## t1 to t2 ## algebraic solution sol3=diffeqsolve (term,t2,t3,sol2.solver_state,solver,args,saveat) ## t2 to t3 ## numerical solve .......

and i could wrap the whole process in a outer_func with lax.scan. And this outer_func can be traced to get sensitivities. Did I understand correctly?

What's more , I saw in the Saveat I could configurate fn , now I only want to save at ts and each period of t1, so how should i define saveat fn to make it memory-efficient(only saving ts and each t1 but not other irrelevant infos)?

timnotavailable avatar Aug 03 '25 21:08 timnotavailable

Yup, exactly.

I'm not sure I understand your question about SaveAt, I'm afraid. If you'd like to save the full solution at specific times ts and at at1 then you can do so via SaveAt(ts=[t_a, t_b, t_c, t_d, ...etc...], t1=True). If you'd like to save just part of the solution then you can define a function fn=lambda t, y, args: ... specifying what to save.

patrick-kidger avatar Aug 04 '25 18:08 patrick-kidger

Hi thanks! That's enough clear!

timnotavailable avatar Aug 04 '25 19:08 timnotavailable