From ce7034e90b55dde579eacf87544befe98c366783 Mon Sep 17 00:00:00 2001 From: Daniel_Doehring Date: Tue, 12 Nov 2024 15:51:05 +0100 Subject: [PATCH 1/6] Restart HyperbolicParabolic with SplitODEProblem --- .../elixir_advection_diffusion.jl | 6 ++- .../elixir_advection_diffusion_restart.jl | 30 +++++++++++++ src/semidiscretization/semidiscretization.jl | 19 ++++---- ...semidiscretization_hyperbolic_parabolic.jl | 44 +++++++++++++++++-- test/test_parabolic_1d.jl | 15 +++++++ 5 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 examples/tree_1d_dgsem/elixir_advection_diffusion_restart.jl diff --git a/examples/tree_1d_dgsem/elixir_advection_diffusion.jl b/examples/tree_1d_dgsem/elixir_advection_diffusion.jl index 9182fd22d0e..c557065308f 100644 --- a/examples/tree_1d_dgsem/elixir_advection_diffusion.jl +++ b/examples/tree_1d_dgsem/elixir_advection_diffusion.jl @@ -75,8 +75,12 @@ analysis_callback = AnalysisCallback(semi, interval = 100) # The AliveCallback prints short status information in regular intervals alive_callback = AliveCallback(analysis_interval = 100) +# The SaveRestartCallback allows to save a file from which a Trixi.jl simulation can be restarted +save_restart = SaveRestartCallback(interval = 100, + save_final_restart = true) + # Create a CallbackSet to collect all callbacks such that they can be passed to the ODE solver -callbacks = CallbackSet(summary_callback, analysis_callback, alive_callback) +callbacks = CallbackSet(summary_callback, analysis_callback, alive_callback, save_restart) ############################################################################### # run the simulation diff --git a/examples/tree_1d_dgsem/elixir_advection_diffusion_restart.jl b/examples/tree_1d_dgsem/elixir_advection_diffusion_restart.jl new file mode 100644 index 00000000000..2b8ad1d5fc3 --- /dev/null +++ b/examples/tree_1d_dgsem/elixir_advection_diffusion_restart.jl @@ -0,0 +1,30 @@ + +using OrdinaryDiffEq +using Trixi + +############################################################################### +# create a restart file + +elixir_file = "elixir_advection_diffusion.jl" +trixi_include(@__MODULE__, joinpath(@__DIR__, elixir_file)) + +############################################################################### +# initialize the ODE + +restart_file = "restart_000000018.h5" +restart_filename = joinpath("out", restart_file) +tspan = (load_time(restart_filename), 2.0) + +ode = semidiscretize(semi, tspan, restart_filename); + +# Do not save restart files here +callbacks = CallbackSet(summary_callback, analysis_callback, alive_callback) + +############################################################################### +# run the simulation + +sol = solve(ode, KenCarp4(autodiff = false), abstol = time_abs_tol, reltol = time_int_tol, + save_everystep = false, callback = callbacks) + +# Print the timer summary +summary_callback() diff --git a/src/semidiscretization/semidiscretization.jl b/src/semidiscretization/semidiscretization.jl index 5f886b350cd..b6abd4aa6ef 100644 --- a/src/semidiscretization/semidiscretization.jl +++ b/src/semidiscretization/semidiscretization.jl @@ -78,16 +78,17 @@ function calc_error_norms(u_ode, t, analyzer, semi::AbstractSemidiscretization, end """ - semidiscretize(semi::AbstractSemidiscretization, tspan) + semidiscretize(semi::AbstractSemidiscretization, tspan; reset_threads=true) Wrap the semidiscretization `semi` as an ODE problem in the time interval `tspan` that can be passed to `solve` from the [SciML ecosystem](https://diffeq.sciml.ai/latest/). + +Optionally reset Polyester.jl threads. See +https://github.com/trixi-framework/Trixi.jl/issues/1583 +https://github.com/JuliaSIMD/Polyester.jl/issues/30 """ function semidiscretize(semi::AbstractSemidiscretization, tspan; reset_threads = true) - # Optionally reset Polyester.jl threads. See - # https://github.com/trixi-framework/Trixi.jl/issues/1583 - # https://github.com/JuliaSIMD/Polyester.jl/issues/30 if reset_threads Polyester.reset_threads!() end @@ -102,18 +103,20 @@ function semidiscretize(semi::AbstractSemidiscretization, tspan; end """ - semidiscretize(semi::AbstractSemidiscretization, tspan, restart_file::AbstractString) + semidiscretize(semi::AbstractSemidiscretization, tspan, restart_file::AbstractString; + reset_threads=true) Wrap the semidiscretization `semi` as an ODE problem in the time interval `tspan` that can be passed to `solve` from the [SciML ecosystem](https://diffeq.sciml.ai/latest/). The initial condition etc. is taken from the `restart_file`. + +Optionally reset Polyester.jl threads. See +https://github.com/trixi-framework/Trixi.jl/issues/1583 +https://github.com/JuliaSIMD/Polyester.jl/issues/30 """ function semidiscretize(semi::AbstractSemidiscretization, tspan, restart_file::AbstractString; reset_threads = true) - # Optionally reset Polyester.jl threads. See - # https://github.com/trixi-framework/Trixi.jl/issues/1583 - # https://github.com/JuliaSIMD/Polyester.jl/issues/30 if reset_threads Polyester.reset_threads!() end diff --git a/src/semidiscretization/semidiscretization_hyperbolic_parabolic.jl b/src/semidiscretization/semidiscretization_hyperbolic_parabolic.jl index 87204843ed9..cebc6ecc361 100644 --- a/src/semidiscretization/semidiscretization_hyperbolic_parabolic.jl +++ b/src/semidiscretization/semidiscretization_hyperbolic_parabolic.jl @@ -267,19 +267,21 @@ function compute_coefficients!(u_ode, t, semi::SemidiscretizationHyperbolicParab end """ - semidiscretize(semi::SemidiscretizationHyperbolicParabolic, tspan) + semidiscretize(semi::SemidiscretizationHyperbolicParabolic, tspan; + reset_threads=true) Wrap the semidiscretization `semi` as a split ODE problem in the time interval `tspan` that can be passed to `solve` from the [SciML ecosystem](https://diffeq.sciml.ai/latest/). The parabolic right-hand side is the first function of the split ODE problem and will be used by default by the implicit part of IMEX methods from the SciML ecosystem. + +Optionally reset Polyester.jl threads. See +https://github.com/trixi-framework/Trixi.jl/issues/1583 +https://github.com/JuliaSIMD/Polyester.jl/issues/30 """ function semidiscretize(semi::SemidiscretizationHyperbolicParabolic, tspan; reset_threads = true) - # Optionally reset Polyester.jl threads. See - # https://github.com/trixi-framework/Trixi.jl/issues/1583 - # https://github.com/JuliaSIMD/Polyester.jl/issues/30 if reset_threads Polyester.reset_threads!() end @@ -295,6 +297,40 @@ function semidiscretize(semi::SemidiscretizationHyperbolicParabolic, tspan; return SplitODEProblem{iip}(rhs_parabolic!, rhs!, u0_ode, tspan, semi) end +""" + semidiscretize(semi::SemidiscretizationHyperbolicParabolic, tspan, + restart_file::AbstractString; reset_threads=true) + +Wrap the semidiscretization `semi` as a split ODE problem in the time interval `tspan` +that can be passed to `solve` from the [SciML ecosystem](https://diffeq.sciml.ai/latest/). +The parabolic right-hand side is the first function of the split ODE problem and +will be used by default by the implicit part of IMEX methods from the +SciML ecosystem. + +The initial condition etc. is taken from the `restart_file`. + +Optionally reset Polyester.jl threads. See +https://github.com/trixi-framework/Trixi.jl/issues/1583 +https://github.com/JuliaSIMD/Polyester.jl/issues/30 +""" +function semidiscretize(semi::SemidiscretizationHyperbolicParabolic, tspan, + restart_file::AbstractString; + reset_threads = true, split_form = true) + if reset_threads + Polyester.reset_threads!() + end + + u0_ode = load_restart_file(semi, restart_file) + # TODO: MPI, do we want to synchronize loading and print debug statements, e.g. using + # mpi_isparallel() && MPI.Barrier(mpi_comm()) + # See https://github.com/trixi-framework/Trixi.jl/issues/328 + iip = true # is-inplace, i.e., we modify a vector when calling rhs_parabolic!, rhs! + # Note that the IMEX time integration methods of OrdinaryDiffEq.jl treat the + # first function implicitly and the second one explicitly. Thus, we pass the + # stiffer parabolic function first. + return SplitODEProblem{iip}(rhs_parabolic!, rhs!, u0_ode, tspan, semi) +end + function rhs!(du_ode, u_ode, semi::SemidiscretizationHyperbolicParabolic, t) @unpack mesh, equations, boundary_conditions, source_terms, solver, cache = semi diff --git a/test/test_parabolic_1d.jl b/test/test_parabolic_1d.jl index 062e6363a2f..48b2b9ce4d6 100644 --- a/test/test_parabolic_1d.jl +++ b/test/test_parabolic_1d.jl @@ -28,6 +28,21 @@ isdir(outdir) && rm(outdir, recursive = true) end end +@trixi_testset "TreeMesh1D: elixir_advection_diffusion_restart.jl" begin + @test_trixi_include(joinpath(examples_dir(), "tree_1d_dgsem", + "elixir_advection_diffusion_restart.jl"), + l2=[1.0671615777620987e-5], + linf=[3.861509422325993e-5]) + # 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)) < 1000 + end +end + @trixi_testset "TreeMesh1D: elixir_advection_diffusion.jl (AMR)" begin @test_trixi_include(joinpath(examples_dir(), "tree_1d_dgsem", "elixir_advection_diffusion.jl"), From 3eff27df73706152361ded157e308ee925938249 Mon Sep 17 00:00:00 2001 From: Daniel_Doehring Date: Tue, 12 Nov 2024 15:58:13 +0100 Subject: [PATCH 2/6] cons docstrings --- src/semidiscretization/semidiscretization.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/semidiscretization/semidiscretization.jl b/src/semidiscretization/semidiscretization.jl index b6abd4aa6ef..5a6bfca59b8 100644 --- a/src/semidiscretization/semidiscretization.jl +++ b/src/semidiscretization/semidiscretization.jl @@ -78,7 +78,8 @@ function calc_error_norms(u_ode, t, analyzer, semi::AbstractSemidiscretization, end """ - semidiscretize(semi::AbstractSemidiscretization, tspan; reset_threads=true) + semidiscretize(semi::AbstractSemidiscretization, tspan; + reset_threads=true) Wrap the semidiscretization `semi` as an ODE problem in the time interval `tspan` that can be passed to `solve` from the [SciML ecosystem](https://diffeq.sciml.ai/latest/). From f7366cc086f0eae81b7a58a9feacc2d5ca219b00 Mon Sep 17 00:00:00 2001 From: Daniel_Doehring Date: Tue, 12 Nov 2024 16:19:06 +0100 Subject: [PATCH 3/6] remove split_form flag --- .../semidiscretization_hyperbolic_parabolic.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/semidiscretization/semidiscretization_hyperbolic_parabolic.jl b/src/semidiscretization/semidiscretization_hyperbolic_parabolic.jl index cebc6ecc361..7efaf59fc5b 100644 --- a/src/semidiscretization/semidiscretization_hyperbolic_parabolic.jl +++ b/src/semidiscretization/semidiscretization_hyperbolic_parabolic.jl @@ -315,7 +315,7 @@ https://github.com/JuliaSIMD/Polyester.jl/issues/30 """ function semidiscretize(semi::SemidiscretizationHyperbolicParabolic, tspan, restart_file::AbstractString; - reset_threads = true, split_form = true) + reset_threads = true) if reset_threads Polyester.reset_threads!() end From 58889fdb101b2f86245a57c595e73bb93c74118d Mon Sep 17 00:00:00 2001 From: Daniel_Doehring Date: Tue, 12 Nov 2024 16:55:11 +0100 Subject: [PATCH 4/6] cons format --- src/semidiscretization/semidiscretization.jl | 3 ++- .../semidiscretization_hyperbolic_parabolic.jl | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/semidiscretization/semidiscretization.jl b/src/semidiscretization/semidiscretization.jl index 5a6bfca59b8..6e980ec237e 100644 --- a/src/semidiscretization/semidiscretization.jl +++ b/src/semidiscretization/semidiscretization.jl @@ -104,7 +104,8 @@ function semidiscretize(semi::AbstractSemidiscretization, tspan; end """ - semidiscretize(semi::AbstractSemidiscretization, tspan, restart_file::AbstractString; + semidiscretize(semi::AbstractSemidiscretization, tspan, + restart_file::AbstractString; reset_threads=true) Wrap the semidiscretization `semi` as an ODE problem in the time interval `tspan` diff --git a/src/semidiscretization/semidiscretization_hyperbolic_parabolic.jl b/src/semidiscretization/semidiscretization_hyperbolic_parabolic.jl index 7efaf59fc5b..2c70786abc5 100644 --- a/src/semidiscretization/semidiscretization_hyperbolic_parabolic.jl +++ b/src/semidiscretization/semidiscretization_hyperbolic_parabolic.jl @@ -299,7 +299,8 @@ end """ semidiscretize(semi::SemidiscretizationHyperbolicParabolic, tspan, - restart_file::AbstractString; reset_threads=true) + restart_file::AbstractString; + reset_threads=true) Wrap the semidiscretization `semi` as a split ODE problem in the time interval `tspan` that can be passed to `solve` from the [SciML ecosystem](https://diffeq.sciml.ai/latest/). From 31b6f97865f264f692c4d791d110dde966896e65 Mon Sep 17 00:00:00 2001 From: Daniel_Doehring Date: Tue, 12 Nov 2024 18:22:52 +0100 Subject: [PATCH 5/6] revert doc --- src/semidiscretization/semidiscretization.jl | 17 +++++++--------- ...semidiscretization_hyperbolic_parabolic.jl | 20 ++++++++----------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/semidiscretization/semidiscretization.jl b/src/semidiscretization/semidiscretization.jl index 6e980ec237e..873bfdd9893 100644 --- a/src/semidiscretization/semidiscretization.jl +++ b/src/semidiscretization/semidiscretization.jl @@ -83,13 +83,12 @@ end Wrap the semidiscretization `semi` as an ODE problem in the time interval `tspan` that can be passed to `solve` from the [SciML ecosystem](https://diffeq.sciml.ai/latest/). - -Optionally reset Polyester.jl threads. See -https://github.com/trixi-framework/Trixi.jl/issues/1583 -https://github.com/JuliaSIMD/Polyester.jl/issues/30 """ function semidiscretize(semi::AbstractSemidiscretization, tspan; reset_threads = true) + # Optionally reset Polyester.jl threads. See + # https://github.com/trixi-framework/Trixi.jl/issues/1583 + # https://github.com/JuliaSIMD/Polyester.jl/issues/30 if reset_threads Polyester.reset_threads!() end @@ -105,20 +104,18 @@ end """ semidiscretize(semi::AbstractSemidiscretization, tspan, - restart_file::AbstractString; - reset_threads=true) + restart_file::AbstractString) Wrap the semidiscretization `semi` as an ODE problem in the time interval `tspan` that can be passed to `solve` from the [SciML ecosystem](https://diffeq.sciml.ai/latest/). The initial condition etc. is taken from the `restart_file`. - -Optionally reset Polyester.jl threads. See -https://github.com/trixi-framework/Trixi.jl/issues/1583 -https://github.com/JuliaSIMD/Polyester.jl/issues/30 """ function semidiscretize(semi::AbstractSemidiscretization, tspan, restart_file::AbstractString; reset_threads = true) + # Optionally reset Polyester.jl threads. See + # https://github.com/trixi-framework/Trixi.jl/issues/1583 + # https://github.com/JuliaSIMD/Polyester.jl/issues/30 if reset_threads Polyester.reset_threads!() end diff --git a/src/semidiscretization/semidiscretization_hyperbolic_parabolic.jl b/src/semidiscretization/semidiscretization_hyperbolic_parabolic.jl index 2c70786abc5..1af65995e79 100644 --- a/src/semidiscretization/semidiscretization_hyperbolic_parabolic.jl +++ b/src/semidiscretization/semidiscretization_hyperbolic_parabolic.jl @@ -267,21 +267,19 @@ function compute_coefficients!(u_ode, t, semi::SemidiscretizationHyperbolicParab end """ - semidiscretize(semi::SemidiscretizationHyperbolicParabolic, tspan; - reset_threads=true) + semidiscretize(semi::SemidiscretizationHyperbolicParabolic, tspan) Wrap the semidiscretization `semi` as a split ODE problem in the time interval `tspan` that can be passed to `solve` from the [SciML ecosystem](https://diffeq.sciml.ai/latest/). The parabolic right-hand side is the first function of the split ODE problem and will be used by default by the implicit part of IMEX methods from the SciML ecosystem. - -Optionally reset Polyester.jl threads. See -https://github.com/trixi-framework/Trixi.jl/issues/1583 -https://github.com/JuliaSIMD/Polyester.jl/issues/30 """ function semidiscretize(semi::SemidiscretizationHyperbolicParabolic, tspan; reset_threads = true) + # Optionally reset Polyester.jl threads. See + # https://github.com/trixi-framework/Trixi.jl/issues/1583 + # https://github.com/JuliaSIMD/Polyester.jl/issues/30 if reset_threads Polyester.reset_threads!() end @@ -299,8 +297,7 @@ end """ semidiscretize(semi::SemidiscretizationHyperbolicParabolic, tspan, - restart_file::AbstractString; - reset_threads=true) + restart_file::AbstractString) Wrap the semidiscretization `semi` as a split ODE problem in the time interval `tspan` that can be passed to `solve` from the [SciML ecosystem](https://diffeq.sciml.ai/latest/). @@ -309,14 +306,13 @@ will be used by default by the implicit part of IMEX methods from the SciML ecosystem. The initial condition etc. is taken from the `restart_file`. - -Optionally reset Polyester.jl threads. See -https://github.com/trixi-framework/Trixi.jl/issues/1583 -https://github.com/JuliaSIMD/Polyester.jl/issues/30 """ function semidiscretize(semi::SemidiscretizationHyperbolicParabolic, tspan, restart_file::AbstractString; reset_threads = true) + # Optionally reset Polyester.jl threads. See + # https://github.com/trixi-framework/Trixi.jl/issues/1583 + # https://github.com/JuliaSIMD/Polyester.jl/issues/30 if reset_threads Polyester.reset_threads!() end From b446b9d948de9f46d5352b9ba14e9544d14b017f Mon Sep 17 00:00:00 2001 From: Daniel_Doehring Date: Tue, 12 Nov 2024 18:23:34 +0100 Subject: [PATCH 6/6] docs --- src/semidiscretization/semidiscretization.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/semidiscretization/semidiscretization.jl b/src/semidiscretization/semidiscretization.jl index 873bfdd9893..dd5c3c4791d 100644 --- a/src/semidiscretization/semidiscretization.jl +++ b/src/semidiscretization/semidiscretization.jl @@ -78,8 +78,7 @@ function calc_error_norms(u_ode, t, analyzer, semi::AbstractSemidiscretization, end """ - semidiscretize(semi::AbstractSemidiscretization, tspan; - reset_threads=true) + semidiscretize(semi::AbstractSemidiscretization, tspan) Wrap the semidiscretization `semi` as an ODE problem in the time interval `tspan` that can be passed to `solve` from the [SciML ecosystem](https://diffeq.sciml.ai/latest/).