Skip to content

Commit 6db8466

Browse files
animations in plot recipe
1 parent 4373c30 commit 6db8466

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

src/integrator_interface.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ intervals(integrator::DEIntegrator) = IntegratorIntervals(integrator)
9595
for x in vars
9696
for j in 1:dims
9797
if denseplot
98-
push!(plot_vecs[j], u_n(plot_timeseries, x[j],integrator.sol,plott,plot_timeseries))
98+
push!(plot_vecs[j], u_n(plot_timeseries, x[j],integrator.sol,plott,plot_timeseries,0))
9999
else # just get values
100100
if x[j] == 0
101101
push!(plot_vecs[j], integrator.t)
@@ -113,7 +113,7 @@ intervals(integrator::DEIntegrator) = IntegratorIntervals(integrator)
113113
for x in vars
114114
for j in 1:dims
115115
if denseplot
116-
push!(plot_vecs[j], u_n(plot_timeseries, x[j],sol,plott,plot_timeseries))
116+
push!(plot_vecs[j], u_n(plot_timeseries, x[j],sol,plott,plot_timeseries,0))
117117
else # Just get values
118118
if x[j] == 0
119119
push!(plot_vecs[j], integrator.t)

src/solutions/solution_interface.jl

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,38 @@ end
5555
@recipe function f(sol::DESolution;
5656
plot_analytic=false,
5757
denseplot = (sol.dense || typeof(sol.prob) <: AbstractDiscreteProblem) && !(typeof(sol) <: AbstractSDESolution),
58-
plotdensity=typeof(sol.prob) <: AbstractDiscreteProblem ? 100*length(sol) : 10*length(sol),vars=nothing)
58+
plotdensity = sol.tslocation==0 ? (typeof(sol.prob) <: AbstractDiscreteProblem ? 100*length(sol) : 10*length(sol)) : 100*sol.tslocation,
59+
vars=nothing)
5960

6061
int_vars = interpret_vars(vars,sol)
6162

63+
if sol.tslocation == 0
64+
end_idx = length(sol)
65+
else
66+
end_idx = sol.tslocation
67+
end
68+
6269
if denseplot
6370
# Generate the points from the plot from dense function
64-
plott = collect(Ranges.linspace(sol.t[1],sol.t[end],plotdensity))
71+
plott = collect(Ranges.linspace(sol.t[1],sol.t[end_idx],plotdensity))
6572
plot_timeseries = sol(plott)
6673
if plot_analytic
6774
plot_analytic_timeseries = [sol.prob.analytic(t,sol.prob.u0) for t in plott]
6875
end
6976
else
7077
# Plot for sparse output: use the timeseries itself
71-
plott = sol.t
72-
plot_timeseries = sol.u
73-
if plot_analytic
74-
plot_analytic_timeseries = sol.u_analytic
78+
if end_idx == 0
79+
plott = sol.t
80+
plot_timeseries = sol.u
81+
if plot_analytic
82+
plot_analytic_timeseries = sol.u_analytic
83+
end
84+
else
85+
plott = sol.t[1:end_idx]
86+
plot_timeseries = sol.u[1:end_idx]
87+
if plot_analytic
88+
plot_analytic_timeseries = sol.u_analytic[1:end_idx]
89+
end
7590
end
7691
end
7792

@@ -80,7 +95,7 @@ end
8095
@assert length(var) == dims
8196
end
8297
# Should check that all have the same dims!
83-
plot_vecs,labels = solplot_vecs_and_labels(dims,int_vars,plot_timeseries,plott,sol,plot_analytic)
98+
plot_vecs,labels = solplot_vecs_and_labels(dims,int_vars,plot_timeseries,plott,sol,plot_analytic,end_idx)
8499

85100
tdir = sign(sol.t[end]-sol.t[1])
86101
xflip --> tdir < 0
@@ -222,38 +237,46 @@ function add_analytic_labels!(labels,x,dims,sol)
222237
end
223238
end
224239

225-
function u_n(timeseries::AbstractArray, n::Int,sol,plott,plot_timeseries)
240+
function u_n(timeseries::AbstractArray, n::Int,sol,plott,plot_timeseries,end_idx)
226241
# Returns the nth variable from the timeseries, t if n == 0
227242
if n == 0
228-
plott
243+
if end_idx == 0
244+
return plott
245+
else
246+
return plott[1:end_idx]
247+
end
229248
elseif n == 1 && !(typeof(sol[1]) <: AbstractArray)
230-
timeseries
249+
if end_idx == 0
250+
return timeseries
251+
else
252+
return timeseries[1:end_idx]
253+
end
231254
else
232255
tmp = Vector{eltype(sol[1])}(length(plot_timeseries))
233256
for j in 1:length(plot_timeseries)
234257
tmp[j] = plot_timeseries[j][n]
235258
end
236-
tmp
259+
return tmp
237260
end
238261
end
239262

240-
function solplot_vecs_and_labels(dims,vars,plot_timeseries,plott,sol,plot_analytic)
263+
function solplot_vecs_and_labels(dims,vars,plot_timeseries,plott,sol,plot_analytic,end_idx)
241264
plot_vecs = []
242265
for i in 1:dims
243266
push!(plot_vecs,[])
244267
end
245268
labels = String[]# Array{String, 2}(1, length(vars)*(1+plot_analytic))
246269
for x in vars
247270
for j in 1:dims
248-
push!(plot_vecs[j], u_n(plot_timeseries, x[j],sol,plott,plot_timeseries))
271+
push!(plot_vecs[j], u_n(plot_timeseries, x[j],sol,plott,plot_timeseries,end_idx))
249272
end
250273
add_labels!(labels,x,dims,sol)
251274
end
252275

253276
if plot_analytic
254277
for x in vars
255278
for j in 1:dims
256-
push!(plot_vecs[j], u_n(plot_timeseries, x[j],sol,plott,plot_timeseries))
279+
push!(plot_vecs[j], u_n(plot_timeseries, x[j],sol,plott,plot_timeseries,end_idx))
257280
end
258281
add_analytic_labels!(labels,x,dims,sol)
259282
end

0 commit comments

Comments
 (0)