trixi-framework / Trixi.jl

Trixi.jl: Adaptive high-order numerical simulations of conservation laws in Julia
https://trixi-framework.github.io/Trixi.jl
MIT License
505 stars 98 forks source link

Graceful exit on aborting a simulation with Ctrl-C #1876

Open sloede opened 3 months ago

sloede commented 3 months ago

Raise your hands if you haven't encountered this situation at least once before:

You tinker with a new feature implementation and/or simulation setup. Finally, you get it to run. For good measure, you wait a while to make sure it does not crash. Satisfied, you hit Ctrl-C to abort Now, you'd really like to look at the results using Plots.jl/Makie.jl.

But, alas, there is no way to visualize the result easily from within Julia because you do not have a sol object yet!

What I thus propose is to investigate possible ways to allow a "graceful abort" (an oxymoron if there ever was one) when running simulations from the REPL, discuss them, and then implement them for Trixi.jl. Ideally, I'd be looking at something like the following setup:

JoshuaLampert commented 3 months ago

I really like that idea!

ranocha commented 3 months ago

I think this may require at least a yield() statement after a time step. On Julia v1.10, I get

julia> using BenchmarkTools

julia> @benchmark yield()
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):  11.708 μs … 53.166 μs  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     12.500 μs              ┊ GC (median):    0.00%
 Time  (mean ± σ):   12.770 μs ±  1.046 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

    ▁▁▁▁▁▃▄█▄▂▁▁▁▁▁                                           ▁
  ▆██████████████████▇▇█▇▇▇██▇█▆▇██▇▇▇█▇█▇▇▇▇▆▇▆▆▆▅▅▄▅▅▅▆▅▃▄▄ █
  11.7 μs      Histogram: log(frequency) by time      16.5 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.
andrewwinters5000 commented 3 months ago

This is a nice idea! One follow-up question. Could this "graceful abort" also spit back the sol object if something like a negative density and/or pressure is encountered? This could help with debugging new shock capturing features. However, if it is too complicated or out of focus we can ignore this comment.

ranocha commented 3 months ago

This is a nice idea! One follow-up question. Could this "graceful abort" also spit back the sol object if something like a negative density and/or pressure is encountered? This could help with debugging new shock capturing features. However, if it is too complicated or out of focus we can ignore this comment.

This should work already with the recent version of Trixi.jl (thanks to the work of @DanielDoehring on NaN-based sqrt etc.):

julia> trixi_include("examples/tree_2d_dgsem/elixir_euler_kelvin_helmholtz_instability.jl",
                     tspan = (0.0, 5.0), alpha_max = 0.0)
...
#timesteps:    840 │ Δt: 7.2692e-04 │ sim. time: 3.6513e+00 (73.026%)  │ run time: 5.8007e+00 s
#timesteps:    850 │ Δt: 3.0837e-04 │ sim. time: 3.6562e+00 (73.123%)  │ run time: 5.8773e+00 s
#timesteps:    860 │ Δt: 2.3219e-05 │ sim. time: 3.6575e+00 (73.151%)  │ run time: 5.9528e+00 s
┌ Warning: Instability detected. Aborting
└ @ SciMLBase ~/.julia/packages/SciMLBase/8XHkk/src/integrator_interface.jl:606

julia> sol.t
2-element Vector{Float64}:
 0.0
 3.657545463670625
andrewwinters5000 commented 3 months ago

Aha, then disregard most of my comment above. We only need to focus on the Ctrl + c exiting.