Skip to content

Commit d6ea908

Browse files
committed
Update tests
1 parent fe6ea72 commit d6ea908

File tree

4 files changed

+23
-84
lines changed

4 files changed

+23
-84
lines changed

examples/electrical_components.jl

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ using ModelingToolkit, OrdinaryDiffEq
77
ODESystem(Equation[], t, sts, []; name=name)
88
end
99

10-
function ModelingToolkit.connect(::Type{Pin}, ps...)
11-
eqs = [
12-
0 ~ sum(p->p.i, ps) # KCL
13-
]
10+
function ModelingToolkit.connect(::Type{Pin}, c::Connection)
11+
@unpack outers, inners = c
12+
isum = isempty(inners) ? 0 : sum(p->p.i, inners)
13+
osum = isempty(outers) ? 0 : sum(p->p.i, outers)
14+
eqs = [0 ~ isum - osum] # KCL
15+
ps = [outers; inners]
1416
# KVL
1517
for i in 1:length(ps)-1
1618
push!(eqs, ps[i].v ~ ps[i+1].v)

src/ModelingToolkit.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export SteadyStateProblem, SteadyStateProblemExpr
165165
export JumpProblem, DiscreteProblem
166166
export NonlinearSystem, OptimizationSystem
167167
export ControlSystem
168-
export alias_elimination, flatten, connect, @connector
168+
export alias_elimination, flatten, connect, @connector, Connection
169169
export ode_order_lowering, liouville_transform
170170
export runge_kutta_discretize
171171
export PDESystem

src/systems/abstractsystem.jl

+15-78
Original file line numberDiff line numberDiff line change
@@ -924,18 +924,18 @@ function promote_connect_type(T, S)
924924
error("Don't know how to connect systems of type $S and $T")
925925
end
926926

927-
Base.@kwdef struct Connect
927+
Base.@kwdef struct Connection
928928
inners = nothing
929929
outers = nothing
930930
end
931931

932-
Connect(syss) = Connect(inners=syss)
933-
get_systems(c::Connect) = c.inners
932+
Connection(syss) = Connection(inners=syss)
933+
get_systems(c::Connection) = c.inners
934934

935-
function Base.show(io::IO, c::Connect)
935+
function Base.show(io::IO, c::Connection)
936936
@unpack outers, inners = c
937937
if outers === nothing && inners === nothing
938-
print(io, "<Connect>")
938+
print(io, "<Connection>")
939939
else
940940
inner_str = join((string(nameof(s)) * "::inner" for s in inners), ", ")
941941
outer_str = join((string(nameof(s)) * "::outer" for s in outers), ", ")
@@ -947,18 +947,15 @@ end
947947
function connect(syss...)
948948
length(syss) >= 2 || error("connect takes at least two systems!")
949949
length(unique(nameof, syss)) == length(syss) || error("connect takes distinct systems!")
950-
Equation(Connect(), Connect(syss)) # the RHS are connected systems
950+
Equation(Connection(), Connection(syss)) # the RHS are connected systems
951951
end
952952

953-
# fallback
954-
connect(T::Type, c::Connect) = connect(T, c.outers..., c.inners...)
955-
956953
function expand_connections(sys::AbstractSystem; debug=false)
957954
subsys = get_systems(sys)
958955
isempty(subsys) && return sys
959956

960957
# post order traversal
961-
@unpack sys.systems = map(s->expand_connections(s, debug=debug), subsys)
958+
@set sys.systems = map(s->expand_connections(s, debug=debug), subsys)
962959

963960

964961
# Note that subconnectors in outer connectors are still outer connectors.
@@ -968,19 +965,19 @@ function expand_connections(sys::AbstractSystem; debug=false)
968965
s = string(nameof(sys))
969966
idx = findfirst(isequal(''), s)
970967
parent_name = idx === nothing ? s : s[1:idx]
971-
parent_name in isouter
968+
parent_name in outer_connectors
972969
end
973970
end
974971

975-
eqs′ = equations(sys)
972+
eqs′ = get_eqs(sys)
976973
eqs = Equation[]
977974
cts = [] # connections
978975
for eq in eqs′
979-
eq.lhs isa Connect ? push!(cts, get_systems(eq.rhs)) : push!(eqs, eq) # split connections and equations
976+
eq.lhs isa Connection ? push!(cts, get_systems(eq.rhs)) : push!(eqs, eq) # split connections and equations
980977
end
981978

982979
sys2idx = Dict{Symbol,Int}() # system (name) to n-th connect statement
983-
narg_connects = Vector{Connect}[]
980+
narg_connects = Connection[]
984981
for (i, syss) in enumerate(cts)
985982
# find intersecting connections
986983
exclude = 0 # exclude the intersecting system
@@ -997,7 +994,7 @@ function expand_connections(sys::AbstractSystem; debug=false)
997994
for s in syss
998995
isouter(s) ? push!(outers, s) : push!(inners, s)
999996
end
1000-
push!(narg_connects, Connect(outers=outers, inners=inners))
997+
push!(narg_connects, Connection(outers=outers, inners=inners))
1001998
for s in syss
1002999
sys2idx[nameof(s)] = length(narg_connects)
10031000
end
@@ -1013,15 +1010,15 @@ function expand_connections(sys::AbstractSystem; debug=false)
10131010

10141011
# Bad things happen when there are more than one intersections
10151012
for c in narg_connects
1016-
@unpack outer, inner = c
1013+
@unpack outers, inners = c
10171014
len = length(outers) + length(inners)
1018-
length(unique(nameof, [outers; inners])) == len || error("$(Connect(syss)) has duplicated connections")
1015+
length(unique(nameof, [outers; inners])) == len || error("$(Connection(syss)) has duplicated connections")
10191016
end
10201017

10211018
if debug
10221019
println("Connections:")
10231020
print_with_indent(x) = println(" " ^ 4, x)
1024-
foreach(print_with_indent Connect, narg_connects)
1021+
foreach(print_with_indent, narg_connects)
10251022
end
10261023

10271024
for c in narg_connects
@@ -1034,66 +1031,6 @@ function expand_connections(sys::AbstractSystem; debug=false)
10341031
return sys
10351032
end
10361033

1037-
#=
1038-
function expand_connections(sys::AbstractSystem; debug=false)
1039-
sys = flatten(sys)
1040-
eqs′ = equations(sys)
1041-
eqs = Equation[]
1042-
cts = []
1043-
for eq in eqs′
1044-
eq.lhs isa Connect ? push!(cts, eq.rhs.syss) : push!(eqs, eq) # split connections and equations
1045-
end
1046-
1047-
# O(n) algorithm for connection fusing
1048-
sys2idx = Dict{Symbol,Int}() # system (name) to n-th connect statement
1049-
narg_connects = Vector{Any}[]
1050-
for (i, syss) in enumerate(cts)
1051-
# find intersecting connections
1052-
exclude = 0 # exclude the intersecting system
1053-
idx = 0 # idx of narg_connects
1054-
for (j, s) in enumerate(syss)
1055-
idx′ = get(sys2idx, nameof(s), nothing)
1056-
idx′ === nothing && continue
1057-
idx = idx′
1058-
exclude = j
1059-
end
1060-
if exclude == 0
1061-
push!(narg_connects, collect(syss))
1062-
for s in syss
1063-
sys2idx[nameof(s)] = length(narg_connects)
1064-
end
1065-
else
1066-
# fuse intersecting connections
1067-
for (j, s) in enumerate(syss); j == exclude && continue
1068-
sys2idx[nameof(s)] = idx
1069-
push!(narg_connects[idx], s)
1070-
end
1071-
end
1072-
end
1073-
1074-
# validation
1075-
for syss in narg_connects
1076-
length(unique(nameof, syss)) == length(syss) || error("$(Connect(syss)) has duplicated connections")
1077-
end
1078-
1079-
if debug
1080-
println("Connections:")
1081-
print_with_indent(x) = println(" " ^ 4, x)
1082-
foreach(print_with_indent ∘ Connect, narg_connects)
1083-
end
1084-
1085-
# generate connections
1086-
for syss in narg_connects
1087-
T = promote_connect_type(map(get_connection_type, syss)...)
1088-
ceqs = connect(T, syss...)
1089-
ceqs isa Equation ? push!(eqs, ceqs) : append!(eqs, ceqs)
1090-
end
1091-
1092-
@set! sys.eqs = eqs
1093-
return sys
1094-
end
1095-
=#
1096-
10971034
###
10981035
### Inheritance & composition
10991036
###

src/systems/diffeqs/odesystem.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ function ODESystem(eqs, iv=nothing; kwargs...)
169169
iv === nothing && throw(ArgumentError("Please pass in independent variables."))
170170
connecteqs = Equation[]
171171
for eq in eqs
172-
eq.lhs isa Connect && (push!(connecteqs, eq); continue)
172+
eq.lhs isa Connection && (push!(connecteqs, eq); continue)
173173
collect_vars!(allstates, ps, eq.lhs, iv)
174174
collect_vars!(allstates, ps, eq.rhs, iv)
175175
if isdiffeq(eq)

0 commit comments

Comments
 (0)