Skip to content

Commit

Permalink
Restructure plot #20
Browse files Browse the repository at this point in the history
- plot(simres; kws...) show line plots, where kws can include x_indices = ..
- plot(simres, omega; seriestype=:surface, kws..) and plot(simres, omega; seriestype=:contour) show a surface and contour plot.
  • Loading branch information
arturgower committed Jun 5, 2018
1 parent 520e742 commit af1a810
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
40 changes: 24 additions & 16 deletions src/plot/plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ include("plot_domain.jl")
# include("plot_moments.jl")

# Plot the result in space (across all x) for a specific angular frequency
@recipe function plot(simres::FrequencySimulationResult, x_indices::Union{UnitRange{Int},Colon,AbstractVector{Int}}, ω_index::Int; field_apply = real, seriestype=:surface)
@recipe function plot(simres::SimulationResult, ω::AbstractFloat;
x_indices::Union{UnitRange{Int},Colon,AbstractVector{Int}} = Colon(),
ω_index = findmin(abs.(simres.ω .- ω))[2],
field_apply = real, seriestype=:surface)

x = [x[1] for x in simres.x[x_indices]]
y = [x[2] for x in simres.x[x_indices]]
Expand All @@ -18,37 +21,42 @@ include("plot_domain.jl")

fill --> true
fillcolor --> :pu_or

x, y, field_apply.(reshape(field(simres)[x_indices,ω_index],n_y,n_x))
title --> "Field at $(fieldnames(simres)[end])=$ωt"
seriestype := :contour
x, y, field_apply.(transpose(reshape(field(simres)[x_indices,ω_index],n_y,n_x)))

else

ω_or_t = getfield(simres, 3)[ω_index]
seriestype := :surface
fillcolor --> :pu_or
title --> "Field at ω=$(simres.ω[ω_index])"

title --> "Field at $(fieldnames(simres)[end])=$ω_or_t"
(x, y, field_apply.(field(simres)[x_indices,ω_index]))

end

end

# Plot the result across angular frequency for a specific position (x)
@recipe function plot(simres::FrequencySimulationResult, x_index::Int, ω_indices::Union{UnitRange{Int},Colon,AbstractVector{Int}}=Colon(); field_apply = real)
@recipe function plot(simres::SimulationResult;
x::Vector{AbstractVector} = simres.x,
x_indices::AbstractVector{Int} = [findmin(norm(z - y) for z in simres.x)[2] for y in x],
ω_indices::Union{UnitRange{Int},Colon,AbstractVector{Int}}=Colon())

x = simres.x[x_index]
complex_field = field(simres)[x_index, ω_indices]
for x_ind in x_indices

@series begin
label --> "Real x=$x"
(transpose(simres.ω[ω_indices]), real.(complex_field))
end
complex_field = field(simres)[x_ind, ω_indices]

@series begin
label --> "Imag x=$x"
(transpose(simres.ω[ω_indices]), imag.(complex_field))
end
@series begin
label --> "Real x=$(simres.x[x_ind])"
(transpose(getfield(simres, 3)[ω_indices]), real.(complex_field))
end

@series begin
label --> "Imag x=$(simres.x[x_ind])"
(transpose(getfield(simres, 3)[ω_indices]), imag.(complex_field))
end
end
end

"Plot just the particles"
Expand Down
8 changes: 4 additions & 4 deletions test/plot_field.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"Plot just the particles and source"
function plot_field(simres::SimulationResult; linetype = :line, field_apply = real)
if linetype == :line
function plot_field(simres::SimulationResult; seriestype = :line, field_apply = real)
if seriestype == :line
labs = [ "real x=$x" for x in simres.x];
labs = [labs ; [ "imag x=$x" for x in simres.x]]
ωt = getfield(simres, fieldnames(simres)[end])
plot(transpose(ωt), transpose([real.(field(simres)); imag.(field(simres))])
, labels = reshape(labs,1,length(labs))
)

elseif linetype == :contour || linetype == :field
elseif seriestype == :contour || seriestype == :field
ind_ωt = 1 # choose which time or frequency

x_pixels = union([x[1] for x in simres.x])
Expand All @@ -25,7 +25,7 @@ function plot_field(simres::SimulationResult; linetype = :line, field_apply = re
# title --> "Field at ω=$ω"

plot(x_pixels, y_pixels, field_apply.(field_mat)
, fill=true, aspect_ratio = 1.0, fillcolor = :pu_or, linetype = :contour)
, fill=true, aspect_ratio = 1.0, fillcolor = :pu_or, seriestype = :contour)
else error("Unknown linestyle = $linestyle for $res")
end
end
18 changes: 10 additions & 8 deletions test/plot_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,31 @@ import StaticArrays: SVector
using MultipleScattering

sound_p = Acoustic(1., 4. + 0.0im,2)
P=sound_p
particles = [Particle(sound_p,Circle([x,0.0], .5)) for x =0.:2.:10.]

sound_sim = Acoustic(1., 0.5 + 0.0im,2)
source = TwoDimAcousticPlanarSource(sound_sim, SVector(0.0,0.0), SVector(1.0,0.0), 1.)
sim = FrequencySimulation(sound_sim, particles, source)
sim = FrequencySimulation(sound_sim, source)

ω = 0.5

using Plots ; pyplot()
include("plot_field.jl")

plot(sim,ω)
plot!(sim)

bounds = Rectangle([-0.,-1.], [10.,1.])
simres = run(sim, bounds, [ω])
timres = run(sim, bounds, 0.:0.01:1; ts = [30.], result_in_time=true)
ωs = 0.:0.1:1.
simres = run(sim, bounds, ωs)
timres = run(sim, bounds, ωs; ts = [30.], result_in_time=true)

plot_field(simres, linetype=:contour)
plot_field(timres, linetype=:contour)
plot!(sim)
plot(simres,0.3)
plot(simres,0.3, seriestype=:contour)

# include("plot_field.jl")
# plot_field(simres, linetype=:contour)
# plot_field(timres, linetype=:contour)
# plot!(sim)

amplitude = 1.0
# Create new souce as a linear combination of two other sources
Expand Down

0 comments on commit af1a810

Please sign in to comment.