Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
trixi-framework
GitHub Repository: trixi-framework/Trixi.jl
Path: blob/main/ext/TrixiPlotsExt.jl
5582 views
1
module TrixiPlotsExt
2
3
# Load the required packages
4
using Plots: Plots
5
using Trixi: Trixi, getmesh
6
using MuladdMacro: @muladd
7
using Printf: @sprintf
8
9
@muladd begin
10
#! format: noindent
11
12
function Trixi.show_plot(plot_data, variable_names;
13
show_mesh = true, plot_arguments = Dict{Symbol, Any}(),
14
time = nothing, timestep = nothing)
15
# Gather subplots
16
plots = []
17
for v in variable_names
18
push!(plots, Plots.plot(plot_data[v]; plot_arguments...))
19
end
20
if show_mesh
21
push!(plots, Plots.plot(getmesh(plot_data); plot_arguments...))
22
end
23
24
# Note, for the visualization callback to work for general equation systems
25
# this layout construction would need to use the if-logic below.
26
# Currently, there is no use case for this so it is left here as a note.
27
#
28
# Determine layout
29
# if length(plots) <= 3
30
# cols = length(plots)
31
# rows = 1
32
# else
33
# cols = ceil(Int, sqrt(length(plots)))
34
# rows = div(length(plots), cols, RoundUp)
35
# end
36
# layout = (rows, cols)
37
38
# Determine layout
39
cols = ceil(Int, sqrt(length(plots)))
40
rows = div(length(plots), cols, RoundUp)
41
layout = (rows, cols)
42
43
# Show plot
44
return display(Plots.plot(plots..., layout = layout))
45
end
46
47
function Trixi.save_plot(plot_data, variable_names;
48
show_mesh = true, plot_arguments = Dict{Symbol, Any}(),
49
time = nothing, timestep = nothing)
50
# Gather subplots
51
plots = []
52
for v in variable_names
53
push!(plots, Plots.plot(plot_data[v]; plot_arguments...))
54
end
55
if show_mesh
56
push!(plots, Plots.plot(getmesh(plot_data); plot_arguments...))
57
end
58
59
# Determine layout
60
cols = ceil(Int, sqrt(length(plots)))
61
rows = div(length(plots), cols, RoundUp)
62
layout = (rows, cols)
63
64
# Create plot
65
Plots.plot(plots..., layout = layout)
66
67
# Determine filename and save plot
68
filename = joinpath("out", @sprintf("solution_%09d.png", timestep))
69
return Plots.savefig(filename)
70
end
71
end # @muladd
72
end # module TrixiPlotsExt
73
74