diff --git a/Project.toml b/Project.toml index b46f583edc7..214fc7aec10 100644 --- a/Project.toml +++ b/Project.toml @@ -33,7 +33,6 @@ Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01" RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" -Requires = "ae029012-a4dd-5104-9daa-d747884805df" SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462" SimpleUnPack = "ce78b400-467f-4804-87d8-8f486da07d0a" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" @@ -59,6 +58,7 @@ Convex = "f65535da-76fb-5f13-bab9-19810c17039a" ECOS = "e2685f51-7e38-5353-a97d-a921fd2c8199" Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" NLsolve = "2774e3e8-f4cf-5e23-947b-6d7e65073b56" +Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" SparseConnectivityTracer = "9f842d2f-2579-4b1d-911e-f412cf18a3f5" [extensions] @@ -66,6 +66,7 @@ TrixiCUDAExt = "CUDA" TrixiConvexECOSExt = ["Convex", "ECOS"] TrixiMakieExt = "Makie" TrixiNLsolveExt = "NLsolve" +TrixiPlotsExt = "Plots" TrixiSparseConnectivityTracerExt = "SparseConnectivityTracer" [compat] @@ -96,6 +97,7 @@ NLsolve = "4.5.1" Octavian = "0.3.28" OffsetArrays = "1.13" P4est = "0.4.12" +Plots = "1.38.13" Polyester = "=0.7.16, 0.7.18" PrecompileTools = "1.2.1" Preferences = "1.5" @@ -103,7 +105,6 @@ Printf = "1" RecipesBase = "1.3.4" RecursiveArrayTools = "3.37" Reexport = "1.2.2" -Requires = "1.3" SciMLBase = "2.141.0" SimpleUnPack = "1.1" SparseArrays = "1" diff --git a/ext/TrixiPlotsExt.jl b/ext/TrixiPlotsExt.jl new file mode 100644 index 00000000000..18cfc87d3c1 --- /dev/null +++ b/ext/TrixiPlotsExt.jl @@ -0,0 +1,72 @@ +module TrixiPlotsExt + +# Load the required packages +using Plots: Plots +using Trixi: Trixi, getmesh +using MuladdMacro: @muladd +using Printf: @sprintf + +@muladd begin +#! format: noindent + +function Trixi.show_plot(plot_data, variable_names; + show_mesh = true, plot_arguments = Dict{Symbol, Any}(), + time = nothing, timestep = nothing) + # Gather subplots + plots = [] + for v in variable_names + push!(plots, Plots.plot(plot_data[v]; plot_arguments...)) + end + if show_mesh + push!(plots, Plots.plot(getmesh(plot_data); plot_arguments...)) + end + + # Note, for the visualization callback to work for general equation systems + # this layout construction would need to use the if-logic below. + # Currently, there is no use case for this so it is left here as a note. + # + # Determine layout + # if length(plots) <= 3 + # cols = length(plots) + # rows = 1 + # else + # cols = ceil(Int, sqrt(length(plots))) + # rows = div(length(plots), cols, RoundUp) + # end + # layout = (rows, cols) + + # Determine layout + cols = ceil(Int, sqrt(length(plots))) + rows = div(length(plots), cols, RoundUp) + layout = (rows, cols) + + # Show plot + return display(Plots.plot(plots..., layout = layout)) +end + +function Trixi.save_plot(plot_data, variable_names; + show_mesh = true, plot_arguments = Dict{Symbol, Any}(), + time = nothing, timestep = nothing) + # Gather subplots + plots = [] + for v in variable_names + push!(plots, Plots.plot(plot_data[v]; plot_arguments...)) + end + if show_mesh + push!(plots, Plots.plot(getmesh(plot_data); plot_arguments...)) + end + + # Determine layout + cols = ceil(Int, sqrt(length(plots))) + rows = div(length(plots), cols, RoundUp) + layout = (rows, cols) + + # Create plot + Plots.plot(plots..., layout = layout) + + # Determine filename and save plot + filename = joinpath("out", @sprintf("solution_%09d.png", timestep)) + return Plots.savefig(filename) +end +end # @muladd +end # module TrixiPlotsExt diff --git a/src/Trixi.jl b/src/Trixi.jl index 57eb76ef2f6..b005e2a2302 100644 --- a/src/Trixi.jl +++ b/src/Trixi.jl @@ -78,7 +78,6 @@ using P4est using T8code using RecipesBase: RecipesBase using RecursiveArrayTools: VectorOfArray -using Requires: @require using Static: Static, One, True, False @reexport using StaticArrays: SVector using StaticArrays: StaticArrays, MVector, MArray, SMatrix, @SMatrix @@ -351,11 +350,6 @@ function __init__() init_t8code() register_error_hints() - - # Enable features that depend on the availability of the Plots package - @require Plots="91a5bcdd-55d7-5caf-9e0b-520d859cae80" begin - using .Plots: Plots - end end include("auxiliary/precompile.jl") diff --git a/src/callbacks_step/visualization.jl b/src/callbacks_step/visualization.jl index dc7dca055ce..a7d6a31e4e6 100644 --- a/src/callbacks_step/visualization.jl +++ b/src/callbacks_step/visualization.jl @@ -110,19 +110,6 @@ function VisualizationCallback(semi, plot_data_creator = nothing; plot_creator, Dict{Symbol, Any}(plot_arguments)) - # Warn users if they create a visualization callback without having loaded the Plots package - # - # Note: This warning is added for convenience, as Plots is the only "officially" supported - # visualization package right now. However, in general nothing prevents anyone from using - # other packages such as Makie, Gadfly etc., given that appropriate `plot_creator`s are - # passed. This is also the reason why the visualization callback is not included via - # Requires.jl only when Plots is present. - # In the future, we should update/remove this warning if other plotting packages are - # starting to be used. - if !(:Plots in names(@__MODULE__, all = true)) - @warn "Package `Plots` not loaded but required by `VisualizationCallback` to visualize results" - end - return DiscreteCallback(visualization_callback, visualization_callback, # the first one is the condition, the second the affect! save_positions = (false, false), initialize = initialize!) @@ -186,42 +173,12 @@ variables in `variable_names` and, optionally, the mesh (if `show_mesh` is `true This function is the default `plot_creator` argument for the [`VisualizationCallback`](@ref). `time` and `timestep` are currently unused by this function. +!!! note + This requires loading [Plots.jl](https://github.com/JuliaPlots/Plots.jl), e.g., via `using Plots`. + See also: [`VisualizationCallback`](@ref), [`save_plot`](@ref) """ -function show_plot(plot_data, variable_names; - show_mesh = true, plot_arguments = Dict{Symbol, Any}(), - time = nothing, timestep = nothing) - # Gather subplots - plots = [] - for v in variable_names - push!(plots, Plots.plot(plot_data[v]; plot_arguments...)) - end - if show_mesh - push!(plots, Plots.plot(getmesh(plot_data); plot_arguments...)) - end - - # Note, for the visualization callback to work for general equation systems - # this layout construction would need to use the if-logic below. - # Currently, there is no use case for this so it is left here as a note. - # - # Determine layout - # if length(plots) <= 3 - # cols = length(plots) - # rows = 1 - # else - # cols = ceil(Int, sqrt(length(plots))) - # rows = div(length(plots), cols, RoundUp) - # end - # layout = (rows, cols) - - # Determine layout - cols = ceil(Int, sqrt(length(plots))) - rows = div(length(plots), cols, RoundUp) - layout = (rows, cols) - - # Show plot - return display(Plots.plot(plots..., layout = layout)) -end +function show_plot end """ save_plot(plot_data, variable_names; @@ -235,30 +192,10 @@ is `true`). Additionally, `plot_arguments` will be unpacked and passed as keywo The `timestep` is used in the filename. `time` is currently unused by this function. +!!! note + This requires loading [Plots.jl](https://github.com/JuliaPlots/Plots.jl), e.g., via `using Plots`. + See also: [`VisualizationCallback`](@ref), [`show_plot`](@ref) """ -function save_plot(plot_data, variable_names; - show_mesh = true, plot_arguments = Dict{Symbol, Any}(), - time = nothing, timestep = nothing) - # Gather subplots - plots = [] - for v in variable_names - push!(plots, Plots.plot(plot_data[v]; plot_arguments...)) - end - if show_mesh - push!(plots, Plots.plot(getmesh(plot_data); plot_arguments...)) - end - - # Determine layout - cols = ceil(Int, sqrt(length(plots))) - rows = div(length(plots), cols, RoundUp) - layout = (rows, cols) - - # Create plot - Plots.plot(plots..., layout = layout) - - # Determine filename and save plot - filename = joinpath("out", @sprintf("solution_%09d.png", timestep)) - return Plots.savefig(filename) -end +function save_plot end end # @muladd