Skip to content

Commit 7d3f1d5

Browse files
committed
add plot_analytical to plot recipes
1 parent c3fb20c commit 7d3f1d5

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

docs/src/basic_example.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,19 @@ nothing # hide
127127

128128
![shoaling solution](shoaling_solution.png)
129129

130-
By default, this will plot the bathymetry, but not the initial (analytical) solution.
130+
By default, this will plot the bathymetry, but neither the initial nor the analytical solution.
131131

132-
You can adjust this by passing the boolean values `plot_bathymetry` (if `true`, always plot bathymetry in the first subplot) and `plot_initial`. Note that `plot_initial = true` will evaluate and plot the initial condition function at the same time `t` as the numerical solution being displayed (the final time by default). This means if your initial condition function represents an analytical solution, setting `plot_initial = true` will plot the analytical solution at that specific time for comparison.
132+
You can adjust this by passing the boolean values `plot_bathymetry` (if `true`, always plot bathymetry in the first subplot),
133+
`plot_initial`, and `plot_analytical`. Note that `plot_analytical = true` will evaluate and plot the initial condition function
134+
at the same time `t` as the numerical solution being displayed (the final time by default). This means if your initial condition
135+
function represents an analytical solution, setting `plot_analytical = true` will plot the analytical solution at that specific
136+
time for comparison. On the other hand, `plot_initial = true` will always plot the initial condition at time `t = first(tspan)`.
133137

134138
Plotting an animation over time can, e.g., be done by the following command, which uses `step` to plot the solution at a specific time step. Here `conversion = waterheight_total` makes it so that we only look at the total water height ``\eta`` and not also the velocity ``v``. More on tutorials for plotting can be found in the chapter [Plotting Simulation Results](@ref plotting).
135139

136140
```@example overview
137141
anim = @animate for step in 1:length(sol.u)
138-
plot(semi => sol, plot_initial = true, conversion = waterheight_total, step = step, xlims = (-50, 20), ylims = (-0.8, 0.1),
142+
plot(semi => sol, plot_analytical = true, conversion = waterheight_total, step = step, xlims = (-50, 20), ylims = (-0.8, 0.1),
139143
plot_title = @sprintf "BBM-BBM equations at t = %.2f" sol.t[step])
140144
end
141145
gif(anim, "shoaling_solution.gif", fps = 25)

docs/src/plotting.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ nothing # hide
4747

4848
## Variable Conversion and Visualization Options
4949

50-
The plotting system supports different variable conversions and visualization options. By default, the physical variables (returned by [`prim2phys`](@ref)) are plotted. For hyperbolic approximations like [`HyperbolicSerreGreenNaghdiEquations1D`](@ref), this means auxiliary variables are not plotted by default - only the physical variables from the limit system.
50+
The plotting system supports different variable conversions and visualization options. By default, the physical variables (returned by [`prim2phys`](@ref)) are plotted. For hyperbolic approximations like [`HyperbolicSerreGreenNaghdiEquations1D`](@ref), this means auxiliary variables are not plotted by default only the physical variables from the limit system.
5151

5252
You can plot conservative variables, specific physical quantities, and control what additional information is displayed:
5353

@@ -59,8 +59,8 @@ using Plots
5959
6060
t = 13.37 # plot solution at (roughly) t = 13.37s
6161
step_idx = argmin(abs.(saveat .- t)) # get the closest point to 13.37
62-
p1 = plot(semi => sol, conversion = prim2prim, plot_bathymetry = false,
63-
suptitle = "Primitive Variables", step = step_idx)
62+
p1 = plot(semi => sol, conversion = prim2prim, plot_bathymetry = false, plot_analytical = true,
63+
suptitle = "Primitive Variables with Analytical Solution", step = step_idx)
6464
p2 = plot(semi => sol, conversion = prim2cons, plot_bathymetry = false,
6565
suptitle = "Conservative Variables", step = step_idx)
6666
p3 = plot(semi => sol, conversion = waterheight_total, plot_bathymetry = true,
@@ -75,8 +75,9 @@ nothing # hide
7575

7676
![variable conversions](variable_conversions.png)
7777

78-
Note that the argument `plot_initial = true` plots the initial condition evaluated at the selected time step, which means that the analytical solution is plotted if the initial condition
79-
function describes an exact solution that varies with time.
78+
Note that the argument `plot_analytical = true` plots the initial condition evaluated at the selected time step, which means
79+
that the analytical solution is plotted if the initial condition function describes an exact solution that varies with time.
80+
Similarly, `plot_initial = true` always plots the initial condition at time `t = first(tspan)`.
8081

8182
## Time Series Analysis at Spatial Points
8283

src/visualization.jl

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
struct PlotData{Conversion}
22
semisol::Pair{<:Semidiscretization, <:ODESolution}
33
plot_initial::Bool
4+
plot_analytical::Bool
45
plot_bathymetry::Bool
56
conversion::Conversion
67
step::Integer
@@ -13,7 +14,7 @@ struct PlotDataOverTime{RealT, Conversion}
1314
end
1415

1516
@recipe function f(plotdata::PlotData)
16-
@unpack semisol, plot_initial, plot_bathymetry, conversion, step = plotdata
17+
@unpack semisol, plot_initial, plot_analytical, plot_bathymetry, conversion, step = plotdata
1718
semi, sol = semisol
1819
equations = semi.equations
1920
names = varnames(conversion, equations)
@@ -47,7 +48,7 @@ end
4748
names[i] in ("D", "b") && continue
4849

4950
subplot += 1
50-
if plot_initial == true
51+
if plot_analytical == true
5152
q_exact = compute_coefficients(initial_condition, t, semi)
5253
data_exact = zeros(nvars, nnodes(semi))
5354
for j in eachnode(semi)
@@ -57,11 +58,27 @@ end
5758
@series begin
5859
subplot --> subplot
5960
linestyle --> :solid
60-
label --> "initial $(names[i])"
61+
label --> "analytical $(names[i])"
6162
grid(semi), data_exact[i, :]
6263
end
6364
end
6465

66+
if plot_initial == true
67+
t0 = sol.t[1]
68+
q_exact = compute_coefficients(initial_condition, t0, semi)
69+
data_initial = zeros(nvars, nnodes(semi))
70+
for j in eachnode(semi)
71+
data_initial[:, j] .= conversion(get_node_vars(q_exact, equations, j),
72+
equations)
73+
end
74+
@series begin
75+
subplot --> subplot
76+
linestyle --> :solid
77+
label --> "initial $(names[i])"
78+
grid(semi), data_initial[i, :]
79+
end
80+
end
81+
6582
@series begin
6683
subplot --> subplot
6784
label --> names[i]
@@ -139,13 +156,15 @@ end
139156
end
140157

141158
@recipe function f(semisol::Pair{<:Semidiscretization, <:ODESolution}; plot_initial = false,
142-
plot_bathymetry = true, conversion = prim2phys, step = -1)
143-
PlotData(semisol, plot_initial, plot_bathymetry, conversion, step)
159+
plot_analytical = false, plot_bathymetry = true, conversion = prim2phys,
160+
step = -1)
161+
PlotData(semisol, plot_initial, plot_analytical, plot_bathymetry, conversion, step)
144162
end
145163

146164
@recipe function f(semi::Semidiscretization, sol::ODESolution; plot_initial = false,
147-
plot_bathymetry = true, conversion = prim2phys, step = -1)
148-
PlotData(semi => sol, plot_initial, plot_bathymetry, conversion, step)
165+
plot_analytical = false, plot_bathymetry = true, conversion = prim2phys,
166+
step = -1)
167+
PlotData(semi => sol, plot_initial, plot_analytical, plot_bathymetry, conversion, step)
149168
end
150169

151170
@recipe function f(semisol::Pair{<:Semidiscretization, <:ODESolution}, x_value;

test/test_visualization.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
extra_analysis_integrals = (waterheight_total, custom_integral))
77
@test_nowarn plot(semi => sol)
88
@test_nowarn plot!(semi => sol, plot_initial = true)
9+
@test_nowarn plot!(semi => sol, plot_analytical = true)
910
@test_nowarn plot(semi, sol, conversion = prim2cons, plot_bathymetry = false)
1011
@test_nowarn plot(semi => sol, 0.0)
1112
@test_nowarn plot(semi, sol, 0.0, conversion = prim2cons)

0 commit comments

Comments
 (0)