Skip to content

Commit 172fe16

Browse files
Merge pull request #478 from SciML/plots
Improve plotting for ModelingToolkit changes
2 parents 3b36d39 + f013949 commit 172fe16

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

src/solutions/solution_interface.jl

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,23 @@ DEFAULT_PLOT_FUNC(x,y,z) = (x,y,z) # For v0.5.2 bug
7171

7272
syms = getsyms(sol)
7373
int_vars = interpret_vars(vars,sol,syms)
74+
strs = cleansyms(syms)
75+
7476
tscale = get(plotattributes, :xscale, :identity)
7577
plot_vecs,labels = diffeq_to_arrays(sol,plot_analytic,denseplot,
7678
plotdensity,tspan,axis_safety,
77-
vars,int_vars,tscale,syms)
79+
vars,int_vars,tscale,strs)
7880

7981
tdir = sign(sol.t[end]-sol.t[1])
8082
xflip --> tdir < 0
8183
seriestype --> :path
8284

8385
# Special case labels when vars = (:x,:y,:z) or (:x) or [:x,:y] ...
84-
if typeof(vars) <: Tuple && (typeof(vars[1]) == Symbol && typeof(vars[2]) == Symbol)
85-
xguide --> vars[1]
86-
yguide --> vars[2]
86+
if typeof(vars) <: Tuple && (issymbollike(vars[1]) && issymbollike(vars[2]))
87+
xguide --> strs[int_vars[1][2]]
88+
yguide --> strs[int_vars[1][3]]
8789
if length(vars) > 2
88-
zguide --> vars[3]
90+
zguide --> strs[int_vars[1][4]]
8991
end
9092
end
9193
if getindex.(int_vars,1) == zeros(length(int_vars)) || getindex.(int_vars,2) == zeros(length(int_vars))
@@ -157,7 +159,15 @@ function getsyms(sol)
157159
end
158160
end
159161

160-
function diffeq_to_arrays(sol,plot_analytic,denseplot,plotdensity,tspan,axis_safety,vars,int_vars,tscale,syms)
162+
cleansyms(syms::Nothing) = nothing
163+
cleansyms(syms::Vector{Symbol}) = cleansym.(syms)
164+
function cleansym(sym::Symbol)
165+
str = String(sym)
166+
replace(str,""=>".") # Fix MTK component syntax
167+
end
168+
issymbollike(x) = typeof(x) <: Symbol || Symbol(typeof(x)) == :Operation || Symbol(typeof(x)) == :Variable
169+
170+
function diffeq_to_arrays(sol,plot_analytic,denseplot,plotdensity,tspan,axis_safety,vars,int_vars,tscale,strs)
161171
if tspan === nothing
162172
if sol.tslocation == 0
163173
end_idx = length(sol)
@@ -228,21 +238,19 @@ function diffeq_to_arrays(sol,plot_analytic,denseplot,plotdensity,tspan,axis_saf
228238
@assert length(var) == dims
229239
end
230240
# Should check that all have the same dims!
231-
plot_vecs,labels = solplot_vecs_and_labels(dims,int_vars,plot_timeseries,plott,sol,plot_analytic,plot_analytic_timeseries,syms)
241+
plot_vecs,labels = solplot_vecs_and_labels(dims,int_vars,plot_timeseries,plott,sol,plot_analytic,plot_analytic_timeseries,strs)
232242
end
233243

234244
function interpret_vars(vars,sol,syms)
235245
if vars !== nothing && syms !== nothing
236246
# Do syms conversion
237247
tmp_vars = []
238248
for var in vars
239-
if typeof(var) <: Symbol
240-
var_int = something(findfirst(isequal(var), syms), 0)
241-
elseif typeof(var) <: Union{Tuple,AbstractArray} #eltype(var) <: Symbol # Some kind of iterable
249+
if typeof(var) <: Union{Tuple,AbstractArray} #eltype(var) <: Symbol # Some kind of iterable
242250
tmp = []
243251
for x in var
244-
if typeof(x) <: Symbol
245-
push!(tmp,something(findfirst(isequal(x), syms), 0))
252+
if issymbollike(x)
253+
push!(tmp,something(findfirst(isequal(Symbol(x)), syms), 0))
246254
else
247255
push!(tmp,x)
248256
end
@@ -252,6 +260,8 @@ function interpret_vars(vars,sol,syms)
252260
else
253261
var_int = tmp
254262
end
263+
elseif issymbollike(var)
264+
var_int = something(findfirst(isequal(Symbol(var)), syms), 0)
255265
else
256266
var_int = var
257267
end
@@ -325,14 +335,14 @@ function interpret_vars(vars,sol,syms)
325335
vars
326336
end
327337

328-
function add_labels!(labels,x,dims,sol,syms)
338+
function add_labels!(labels,x,dims,sol,strs)
329339
lys = []
330340
for j in 3:dims
331341
if x[j] == 0
332342
push!(lys,"t,")
333343
else
334-
if syms !== nothing
335-
push!(lys,"$(syms[x[j]]),")
344+
if strs !== nothing
345+
push!(lys,"$(strs[x[j]]),")
336346
else
337347
push!(lys,"u$(x[j]),")
338348
end
@@ -342,8 +352,8 @@ function add_labels!(labels,x,dims,sol,syms)
342352
if x[2] == 0 && dims == 3
343353
tmp_lab = "$(lys...)(t)"
344354
else
345-
if syms !== nothing && x[2] != 0
346-
tmp = syms[x[2]]
355+
if strs !== nothing && x[2] != 0
356+
tmp = strs[x[2]]
347357
tmp_lab = "($tmp,$(lys...))"
348358
else
349359
if x[2] == 0
@@ -361,14 +371,14 @@ function add_labels!(labels,x,dims,sol,syms)
361371
labels
362372
end
363373

364-
function add_analytic_labels!(labels,x,dims,sol,syms)
374+
function add_analytic_labels!(labels,x,dims,sol,strs)
365375
lys = []
366376
for j in 3:dims
367377
if x[j] == 0 && dims == 3
368378
push!(lys,"t,")
369379
else
370-
if syms !== nothing
371-
push!(lys,string("True ",syms[x[j]],","))
380+
if strs !== nothing
381+
push!(lys,string("True ",strs[x[j]],","))
372382
else
373383
push!(lys,"True u$(x[j]),")
374384
end
@@ -378,8 +388,8 @@ function add_analytic_labels!(labels,x,dims,sol,syms)
378388
if x[2] == 0
379389
tmp_lab = "$(lys...)(t)"
380390
else
381-
if syms !== nothing
382-
tmp = string("True ",syms[x[2]])
391+
if strs !== nothing
392+
tmp = string("True ",strs[x[2]])
383393
tmp_lab = "($tmp,$(lys...))"
384394
else
385395
tmp_lab = "(True u$(x[2]),$(lys...))"
@@ -407,7 +417,7 @@ function u_n(timeseries::AbstractArray, n::Int,sol,plott,plot_timeseries)
407417
end
408418
end
409419

410-
function solplot_vecs_and_labels(dims,vars,plot_timeseries,plott,sol,plot_analytic,plot_analytic_timeseries,syms)
420+
function solplot_vecs_and_labels(dims,vars,plot_timeseries,plott,sol,plot_analytic,plot_analytic_timeseries,strs)
411421
plot_vecs = []
412422
labels = String[]
413423
for x in vars
@@ -427,7 +437,7 @@ function solplot_vecs_and_labels(dims,vars,plot_timeseries,plott,sol,plot_analyt
427437
end
428438
push!(plot_vecs[i],tmp[i])
429439
end
430-
add_labels!(labels,x,dims,sol,syms)
440+
add_labels!(labels,x,dims,sol,strs)
431441
end
432442

433443

@@ -445,7 +455,7 @@ function solplot_vecs_and_labels(dims,vars,plot_timeseries,plott,sol,plot_analyt
445455
for i in eachindex(tmp)
446456
push!(plot_vecs[i],tmp[i])
447457
end
448-
add_analytic_labels!(labels,x,dims,sol,syms)
458+
add_analytic_labels!(labels,x,dims,sol,strs)
449459
end
450460
end
451461
plot_vecs = [hcat(x...) for x in plot_vecs]

0 commit comments

Comments
 (0)