@@ -924,18 +924,18 @@ function promote_connect_type(T, S)
924
924
error (" Don't know how to connect systems of type $S and $T " )
925
925
end
926
926
927
- Base. @kwdef struct Connect
927
+ Base. @kwdef struct Connection
928
928
inners = nothing
929
929
outers = nothing
930
930
end
931
931
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
934
934
935
- function Base. show (io:: IO , c:: Connect )
935
+ function Base. show (io:: IO , c:: Connection )
936
936
@unpack outers, inners = c
937
937
if outers === nothing && inners === nothing
938
- print (io, " <Connect >" )
938
+ print (io, " <Connection >" )
939
939
else
940
940
inner_str = join ((string (nameof (s)) * " ::inner" for s in inners), " , " )
941
941
outer_str = join ((string (nameof (s)) * " ::outer" for s in outers), " , " )
@@ -947,18 +947,15 @@ end
947
947
function connect (syss... )
948
948
length (syss) >= 2 || error (" connect takes at least two systems!" )
949
949
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
951
951
end
952
952
953
- # fallback
954
- connect (T:: Type , c:: Connect ) = connect (T, c. outers... , c. inners... )
955
-
956
953
function expand_connections (sys:: AbstractSystem ; debug= false )
957
954
subsys = get_systems (sys)
958
955
isempty (subsys) && return sys
959
956
960
957
# 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)
962
959
963
960
964
961
# Note that subconnectors in outer connectors are still outer connectors.
@@ -968,19 +965,19 @@ function expand_connections(sys::AbstractSystem; debug=false)
968
965
s = string (nameof (sys))
969
966
idx = findfirst (isequal (' ₊' ), s)
970
967
parent_name = idx === nothing ? s : s[1 : idx]
971
- parent_name in isouter
968
+ parent_name in outer_connectors
972
969
end
973
970
end
974
971
975
- eqs′ = equations (sys)
972
+ eqs′ = get_eqs (sys)
976
973
eqs = Equation[]
977
974
cts = [] # connections
978
975
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
980
977
end
981
978
982
979
sys2idx = Dict {Symbol,Int} () # system (name) to n-th connect statement
983
- narg_connects = Vector{Connect} []
980
+ narg_connects = Connection []
984
981
for (i, syss) in enumerate (cts)
985
982
# find intersecting connections
986
983
exclude = 0 # exclude the intersecting system
@@ -997,7 +994,7 @@ function expand_connections(sys::AbstractSystem; debug=false)
997
994
for s in syss
998
995
isouter (s) ? push! (outers, s) : push! (inners, s)
999
996
end
1000
- push! (narg_connects, Connect (outers= outers, inners= inners))
997
+ push! (narg_connects, Connection (outers= outers, inners= inners))
1001
998
for s in syss
1002
999
sys2idx[nameof (s)] = length (narg_connects)
1003
1000
end
@@ -1013,15 +1010,15 @@ function expand_connections(sys::AbstractSystem; debug=false)
1013
1010
1014
1011
# Bad things happen when there are more than one intersections
1015
1012
for c in narg_connects
1016
- @unpack outer, inner = c
1013
+ @unpack outers, inners = c
1017
1014
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" )
1019
1016
end
1020
1017
1021
1018
if debug
1022
1019
println (" Connections:" )
1023
1020
print_with_indent (x) = println (" " ^ 4 , x)
1024
- foreach (print_with_indent ∘ Connect , narg_connects)
1021
+ foreach (print_with_indent, narg_connects)
1025
1022
end
1026
1023
1027
1024
for c in narg_connects
@@ -1034,66 +1031,6 @@ function expand_connections(sys::AbstractSystem; debug=false)
1034
1031
return sys
1035
1032
end
1036
1033
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
-
1097
1034
# ##
1098
1035
# ## Inheritance & composition
1099
1036
# ##
0 commit comments