How to show progress when using DifferentialEquations.jl?

The ODEs I’m solving typically takes an hour and I would like it to print the current time step for every, e.g. 10 time steps. I have tried the method shown on this page about progress bar, but it doesn’t work (i.e. nothing is displayed) on both VS Code and jupyter notebook. Then I tried using a discrete callback with the following codes:

function lorenz!(du, u, p, t)
    du[1] = 10.0 * (u[2] - u[1])
    du[2] = u[1] * (28.0 - u[3]) - u[2]
    du[3] = u[1] * u[2] - (8 / 3) * u[3]
end

u0 = [1.0; 0.0; 0.0]
tspan = (0.0, 100.0)
prob = ODEProblem(lorenz!, u0, tspan);

function do_nth!(integrator)
    println(@sprintf("no. of time steps : %4d, t = %.3e", length(integrator.sol.t), integrator.t))
    return nothing
end

condition1(u, t, integrator) = (mod(length(integrator.sol.t), 50) == 0)   # triggered every 50 time steps
cb1 = DiscreteCallback(condition1, do_nth!; save_positions=(false,false));

sol1 = solve(prob, FBDF(autodiff=true))
sol2 = solve(prob, FBDF(autodiff=true), callback=cb1)

I use FBDF(autodiff=true) in the above code because that’s the specific solver I will be using for my actual ODE. Now the problem is that sol1 and sol2 give different solution, and by comparing the second component of the solution to the example in the documentation, it looks like sol2 (the one with the callback) is wrong. So what else did I do wrong? What’s the best way to show the progress every X time steps?

Trixi.jl uses a simple AliveCallback to print the status of the solve at regular timestep intervals.

It’s pretty short so you can either just copy and paste it, or just load it from Trixi.jl if precompilation time isn’t an issue.

3 Likes

No, it’s just chaotic, so changing dt just a little bit gives an O(1) difference in the solution.

Set u_modified!(integrator,false) to tell it that no changes occurred in the equation and thus it’s fine to not reset dt and such after the callback. I.e.:

function do_nth!(integrator)
    println(@sprintf("no. of time steps : %4d, t = %.3e", length(integrator.sol.t), integrator.t))
    u_modified(integrator,false)
    return nothing
end

But as for using the progress bar, did you try enabling TerminalLoggers?

@pfitzseb did the logging information change for VS Code?

3 Likes

Nothing changed as far as I’m aware, no. Note that ProgressLogging.jl needs to be loaded for progress bars to work in VS Code though.