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?

5 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.

I too have the same problem while implementing the examples given in the documentation such as

using OrdinaryDiffEq, ProgressLogging
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, 1000000.0)
prob = ODEProblem(lorenz!, u0, tspan)
sol = solve(prob, Tsit5(), progress = true)

There is no progress bar shown in VSCode and I do not know a workaround to it.

On Julia v1.10? Share ]st.

I have the same issue.

Status `~/Code/SciMLExpectations/Project.toml`
  [31c24e10] Distributions v0.25.108
  [19dc6840] HCubature v1.6.0
  [de52edbc] Integrals v4.4.1
  [b964fa9f] LaTeXStrings v1.3.1
  [1dea7af3] OrdinaryDiffEq v6.78.0
  [8314cec4] PGFPlotsX v1.6.1
  [91a5bcdd] Plots v1.40.4
  [33c8b6b6] ProgressLogging v0.1.4
  [afe9f18d] SciMLExpectations v2.2.0
  [f3b207a7] StatsPlots v0.15.7
  [5d786b92] TerminalLoggers v0.1.7

add this:


using Logging: global_logger
using TerminalLoggers: TerminalLogger
global_logger(TerminalLogger())

That’s for logging into the terminal though.

@pfitzseb has something changed in the VS Code API?

No. The above example works fine for me:
image

it works in VS code, on the bottom bar