Skip to content

Commit d26d308

Browse files
authored
Merge pull request #780 from SciML/myb/show
Fix ReactionSystem printing
2 parents 41d1789 + 9fb4759 commit d26d308

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

src/systems/abstractsystem.jl

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,12 @@ function structure(sys::AbstractSystem)
134134
s isa SystemStructure || throw(ArgumentError("SystemStructure is not yet initialized, please run `sys = initialize_system_structure(sys)` or `sys = alias_elimination(sys)`."))
135135
return s
136136
end
137-
138137
for prop in [:eqs, :iv, :states, :ps, :default_p, :default_u0, :observed, :tgrad, :jac, :Wfact, :Wfact_t, :systems, :structure]
139-
fname = Symbol(:get_, prop)
138+
fname1 = Symbol(:get_, prop)
139+
fname2 = Symbol(:has_, prop)
140140
@eval begin
141-
$fname(sys::AbstractSystem) = getfield(sys, $(QuoteNode(prop)))
141+
$fname1(sys::AbstractSystem) = getfield(sys, $(QuoteNode(prop)))
142+
$fname2(sys::AbstractSystem) = isdefined(sys, $(QuoteNode(prop)))
142143
end
143144
end
144145

@@ -162,15 +163,15 @@ function Base.getproperty(sys::AbstractSystem, name::Symbol)
162163
return rename(sts[i],renamespace(sysname,name))
163164
end
164165

165-
if isdefined(sys, :ps)
166+
if has_ps(sys)
166167
ps = get_ps(sys)
167168
i = findfirst(x->getname(x) == name,ps)
168169
if i !== nothing
169170
return rename(ps[i],renamespace(sysname,name))
170171
end
171172
end
172173

173-
if isdefined(sys, :observed)
174+
if has_observed(sys)
174175
obs = get_observed(sys)
175176
i = findfirst(x->getname(x.lhs)==name,obs)
176177
if i !== nothing
@@ -332,14 +333,16 @@ function Base.show(io::IO, sys::AbstractSystem)
332333
Base.printstyled(io, "States ($nvars):"; bold=true)
333334
nrows = min(nvars, limit ? rows : nvars)
334335
limited = nrows < length(vars)
335-
d_u0 = default_u0(sys)
336+
d_u0 = has_default_u0(sys) ? default_u0(sys) : nothing
336337
for i in 1:nrows
337338
s = vars[i]
338339
print(io, "\n ", s)
339340

340-
val = get(d_u0, s, nothing)
341-
if val !== nothing
342-
print(io, " [defaults to $val]")
341+
if d_u0 !== nothing
342+
val = get(d_u0, s, nothing)
343+
if val !== nothing
344+
print(io, " [defaults to $val]")
345+
end
343346
end
344347
end
345348
limited && print(io, "\n")
@@ -349,22 +352,26 @@ function Base.show(io::IO, sys::AbstractSystem)
349352
Base.printstyled(io, "Parameters ($nvars):"; bold=true)
350353
nrows = min(nvars, limit ? rows : nvars)
351354
limited = nrows < length(vars)
352-
d_p = default_p(sys)
355+
d_p = has_default_p(sys) ? default_p(sys) : nothing
353356
for i in 1:nrows
354357
s = vars[i]
355358
print(io, "\n ", s)
356359

357-
val = get(d_p, s, nothing)
358-
if val !== nothing
359-
print(io, " [defaults to $val]")
360+
if d_p !== nothing
361+
val = get(d_p, s, nothing)
362+
if val !== nothing
363+
print(io, " [defaults to $val]")
364+
end
360365
end
361366
end
362367
limited && print(io, "\n")
363368

364-
s = get_structure(sys)
365-
if s !== nothing
366-
Base.printstyled(io, "\nIncidence matrix:"; color=:magenta)
367-
show(io, incidence_matrix(s.graph, Num(Sym{Real}(:×))))
369+
if has_structure(sys)
370+
s = get_structure(sys)
371+
if s !== nothing
372+
Base.printstyled(io, "\nIncidence matrix:"; color=:magenta)
373+
show(io, incidence_matrix(s.graph, Num(Sym{Real}(:×))))
374+
end
368375
end
369376
return nothing
370377
end

test/reactionsystem.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ rs = ReactionSystem(rxs,t,[A,B,C,D],k)
2828
odesys = convert(ODESystem,rs)
2929
sdesys = convert(SDESystem,rs)
3030

31+
# test show
32+
io = IOBuffer()
33+
show(io, rs)
34+
str = String(take!(io))
35+
@test count(isequal('\n'), str) < 30
36+
3137
# hard coded ODE rhs
3238
function oderhs(u,k,t)
3339
A = u[1]; B = u[2]; C = u[3]; D = u[4];

0 commit comments

Comments
 (0)