From 547de79e7c8d4048f1a4a42b628b5eba5b3ca115 Mon Sep 17 00:00:00 2001 From: Joshua Lampert <51029046+JoshuaLampert@users.noreply.github.com> Date: Mon, 25 Sep 2023 00:03:55 +0200 Subject: [PATCH] Add the opportunity to plot the errors of AnalysisCallback (#42) * add the opportunity to plot the errors of AnalysisCallback * format --- src/callbacks_step/analysis.jl | 5 ++-- src/visualization.jl | 44 ++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/callbacks_step/analysis.jl b/src/callbacks_step/analysis.jl index 5c709ec8..93806d37 100644 --- a/src/callbacks_step/analysis.jl +++ b/src/callbacks_step/analysis.jl @@ -190,6 +190,7 @@ function (analysis_callback::AnalysisCallback)(u_ode, integrator, semi) @unpack analysis_errors, analysis_integrals, tstops, errors, integrals = analysis_callback @unpack t, dt = integrator push!(tstops, t) + t_final = integrator.sol.prob.tspan[2] iter = integrator.stats.naccept # Compute the total runtime since the analysis callback has been initialized, in seconds @@ -204,7 +205,7 @@ function (analysis_callback::AnalysisCallback)(u_ode, integrator, semi) gc_time_absolute = 1.0e-9 * (Base.gc_time_ns() - analysis_callback.start_gc_time) # Compute the percentage of total time that was spent in garbage collection - gc_time_percentage = gc_time_absolute / runtime_absolute * 10 + gc_time_percentage = gc_time_absolute / runtime_absolute * 100 # Obtain the current memory usage of the Julia garbage collector, in MiB, i.e., the total size of # objects in memory that have been allocated by the JIT compiler or the user code. @@ -226,7 +227,7 @@ function (analysis_callback::AnalysisCallback)(u_ode, integrator, semi) " " * " └── GC time: " * @sprintf("%10.8e s (%5.3f%%)", gc_time_absolute, gc_time_percentage)) - println(" sim. time: " * @sprintf("%10.8e", t)) + println(" sim. time: " * @sprintf("%10.8e (%5.3f%%)", t, t / t_final*100)) println(" #DOF: " * @sprintf("% 14d", nnodes(semi)) * " " * " alloc'd memory: " * @sprintf("%14.3f MiB", memory_use)) diff --git a/src/visualization.jl b/src/visualization.jl index 31712d54..318be639 100644 --- a/src/visualization.jl +++ b/src/visualization.jl @@ -86,10 +86,11 @@ end end end - # Plot the bathymetry if primitive variables are plotted + # Plot the bathymetry if plot_bathymetry == true @series begin subplot := 1 + linestyle := :solid label := "bathymetry" xguide := "x" yguide := names[1] @@ -172,19 +173,36 @@ end PlotDataOverTime(semi => sol, x_value, conversion, yli) end -# TODO: Only plot change in invariants for now, also plot errors? -@recipe function f(cb::DiscreteCallback{Condition, Affect!}) where {Condition, - Affect! <: - AnalysisCallback} +@recipe function f(cb::DiscreteCallback{Condition, Affect!}; what = (:integrals,), + label_extension = "") where {Condition, Affect! <: AnalysisCallback} t = tstops(cb) - ints = integrals(cb) - plot_title --> "change of invariants" - for (name, integral) in pairs(ints) - @series begin - label := string(name) - xguide --> "t" - yguide --> "change of invariants" - t, integral .- integral[1] + subplot = 1 + layout := length(what) + if :integrals in what + ints = integrals(cb) + plot_title --> "change of invariants" + for (name, integral) in pairs(ints) + @series begin + subplot := subplot + label := string(name) * " " * label_extension + xguide --> "t" + yguide --> "change of invariants" + t, integral .- integral[1] + end + end + subplot += 1 + end + if :errors in what + errs = errors(cb) + plot_title --> "errors" + for (name, err) in pairs(errs) + @series begin + subplot := subplot + label := string(name) * " " * label_extension + xguide --> "t" + yguide --> "sum of errors" + t, dropdims(sum(err, dims = 1), dims = 1) + end end end end