Skip to content

Commit

Permalink
Fix intern time integration method with fixed time step (#1919)
Browse files Browse the repository at this point in the history
* Take minimum with current time step

* Fix bug using `dtcache`

* Reset not needed change

* Adjust comment on `dtcache` [skip ci]

* Modify time step before checking for end time

* Add test for SSP method with fixed time step and tstops

* fmt

* Add alive callback

* Move solution callback into test

* Adapt fixed step size to exactly hit 0.1
  • Loading branch information
bennibolm authored May 6, 2024
1 parent cd097fc commit 4fe2824
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/time_integration/methods_SSP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ mutable struct SimpleIntegratorSSP{RealT <: Real, uType, Params, Sol, F, Alg,
t::RealT
tdir::RealT
dt::RealT # current time step
dtcache::RealT # ignored
dtcache::RealT # manually set time step
iter::Int # current number of time steps (iteration)
p::Params # will be the semidiscretization from Trixi
sol::Sol # faked
Expand All @@ -102,7 +102,7 @@ end

"""
add_tstop!(integrator::SimpleIntegratorSSP, t)
Add a time stop during the time integration process.
Add a time stop during the time integration process.
This function is called after the periodic SaveSolutionCallback to specify the next stop to save the solution.
"""
function add_tstop!(integrator::SimpleIntegratorSSP, t)
Expand Down Expand Up @@ -145,7 +145,7 @@ function solve(ode::ODEProblem, alg = SimpleSSPRK33()::SimpleAlgorithmSSP;
t = first(ode.tspan)
tdir = sign(ode.tspan[end] - ode.tspan[1])
iter = 0
integrator = SimpleIntegratorSSP(u, du, r0, t, tdir, dt, zero(dt), iter, ode.p,
integrator = SimpleIntegratorSSP(u, du, r0, t, tdir, dt, dt, iter, ode.p,
(prob = ode,), ode.f, alg,
SimpleIntegratorSSPOptions(callback, ode.tspan;
kwargs...),
Expand Down Expand Up @@ -185,15 +185,15 @@ function solve!(integrator::SimpleIntegratorSSP)
error("time step size `dt` is NaN")
end

modify_dt_for_tstops!(integrator)

# if the next iteration would push the simulation beyond the end time, set dt accordingly
if integrator.t + integrator.dt > t_end ||
isapprox(integrator.t + integrator.dt, t_end)
integrator.dt = t_end - integrator.t
terminate!(integrator)
end

modify_dt_for_tstops!(integrator)

@. integrator.r0 = integrator.u
for stage in eachindex(alg.c)
t_stage = integrator.t + integrator.dt * alg.c[stage]
Expand Down Expand Up @@ -232,7 +232,7 @@ function solve!(integrator::SimpleIntegratorSSP)
end
end

# Empty the tstops array.
# Empty the tstops array.
# This cannot be done in terminate!(integrator::SimpleIntegratorSSP) because DiffEqCallbacks.PeriodicCallbackAffect would return at error.
extract_all!(integrator.opts.tstops)

Expand All @@ -253,12 +253,12 @@ u_modified!(integrator::SimpleIntegratorSSP, ::Bool) = false

# used by adaptive timestepping algorithms in DiffEq
function set_proposed_dt!(integrator::SimpleIntegratorSSP, dt)
integrator.dt = dt
(integrator.dt = dt; integrator.dtcache = dt)
end

# used by adaptive timestepping algorithms in DiffEq
function get_proposed_dt(integrator::SimpleIntegratorSSP)
return integrator.dt
return ifelse(integrator.opts.adaptive, integrator.dt, integrator.dtcache)
end

# stop the time integration
Expand Down
32 changes: 32 additions & 0 deletions test/test_tree_2d_euler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,38 @@ end
end
end

@trixi_testset "elixir_euler_shockcapturing_subcell.jl (fixed time step)" begin
# Testing local SSP method without stepsize callback
# Additionally, tests combination with SaveSolutionCallback using time interval
@test_trixi_include(joinpath(EXAMPLES_DIR,
"elixir_euler_shockcapturing_subcell.jl"),
dt=2.0e-3,
tspan=(0.0, 0.25),
save_solution=SaveSolutionCallback(dt = 0.1 + 1.0e-8),
callbacks=CallbackSet(summary_callback, save_solution,
analysis_callback, alive_callback),
l2=[
0.05624855363458103,
0.06931288786158463,
0.06931283188960778,
0.6200535829842072,
],
linf=[
0.29029967648805566,
0.6494728865862608,
0.6494729363533714,
3.0949621505674787,
])
# Ensure that we do not have excessive memory allocations
# (e.g., from type instabilities)
let
t = sol.t[end]
u_ode = sol.u[end]
du_ode = similar(u_ode)
@test (@allocated Trixi.rhs!(du_ode, u_ode, semi, t)) < 15000
end
end

@trixi_testset "elixir_euler_blast_wave.jl" begin
@test_trixi_include(joinpath(EXAMPLES_DIR, "elixir_euler_blast_wave.jl"),
l2=[
Expand Down

0 comments on commit 4fe2824

Please sign in to comment.