Skip to content

Commit 0f0fcf6

Browse files
committed
WIP model balancing
1 parent 0f7ab52 commit 0f0fcf6

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

examples/electrical_components.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Test
22
using ModelingToolkit, OrdinaryDiffEq
33

4-
@parameters t
4+
@isdefined(t) || @parameters t
55
@connector function Pin(;name)
66
sts = @variables v(t)=1.0 i(t)=1.0 [connect = Flow]
77
ODESystem(Equation[], t, sts, []; name=name)

src/systems/abstractsystem.jl

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,10 +701,34 @@ function get_or_construct_tearing_state(sys)
701701
state
702702
end
703703

704+
# TODO: what about inputs?
705+
function count_unexpanded_flows(sys::AbstractSystem)
706+
nflows = 0
707+
for m in PreOrderDFS(Tree(sys))
708+
isconnector(m) || continue
709+
nflows += count(x->get_connection_type(x) === Flow, get_states(m))
710+
end
711+
nflows
712+
end
713+
714+
function n_extra_equations(sys::AbstractSystem)
715+
isconnector(sys) && return length(get_states(sys))
716+
nextras = count_unexpanded_flows(sys)
717+
end
718+
704719
function Base.show(io::IO, ::MIME"text/plain", sys::AbstractSystem)
705720
eqs = equations(sys)
721+
vars = states(sys); nvars = length(vars)
706722
if eqs isa AbstractArray
707-
Base.printstyled(io, "Model $(nameof(sys)) with $(length(eqs)) equations\n"; bold=true)
723+
neqs = count(eq->!(eq.lhs isa Connection), eqs)
724+
Base.printstyled(io, "Model $(nameof(sys)) with $neqs "; bold=true)
725+
nextras = n_extra_equations(sys)
726+
if nextras > 0
727+
Base.printstyled(io, "("; bold=true)
728+
Base.printstyled(io, neqs + nextras; bold=true, color=:magenta)
729+
Base.printstyled(io, ") "; bold=true)
730+
end
731+
Base.printstyled(io, "equations\n"; bold=true)
708732
else
709733
Base.printstyled(io, "Model $(nameof(sys))\n"; bold=true)
710734
end
@@ -716,7 +740,6 @@ function Base.show(io::IO, ::MIME"text/plain", sys::AbstractSystem)
716740
rows = first(displaysize(io)) ÷ 5
717741
limit = get(io, :limit, false)
718742

719-
vars = states(sys); nvars = length(vars)
720743
Base.printstyled(io, "States ($nvars):"; bold=true)
721744
nrows = min(nvars, limit ? rows : nvars)
722745
limited = nrows < length(vars)

0 commit comments

Comments
 (0)