Skip to content

Commit e47fb18

Browse files
committed
Ad Hoc "fix"
1 parent 32206c9 commit e47fb18

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

src/systems/connectors.jl

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,18 @@ end
5656
Connection(syss) = Connection(inners=syss)
5757
get_systems(c::Connection) = c.inners
5858
function Base.in(e::Symbol, c::Connection)
59-
any(k->nameof(k) === e, c.inners) || any(k->nameof(k) === e, c.outers)
59+
(c.inners !== nothing && any(k->nameof(k) === e, c.inners)) ||
60+
(c.outers !== nothing && any(k->nameof(k) === e, c.outers))
61+
end
62+
63+
function renamespace(sym::Symbol, connection::Connection)
64+
inners = connection.inners === nothing ? [] : renamespace.(sym, connection.inners)
65+
if connection.outers !== nothing
66+
for o in connection.outers
67+
push!(inners, renamespace(sym, o))
68+
end
69+
end
70+
Connection(;inners=inners)
6071
end
6172

6273
const EMPTY_VEC = []
@@ -200,12 +211,14 @@ else
200211
end
201212
@register mydiv(n, d)
202213

203-
function expand_connections(sys::AbstractSystem; debug=false, tol=1e-10)
214+
function expand_connections(sys::AbstractSystem; debug=false, tol=1e-10,
215+
rename=Ref{Union{Nothing,Tuple{Symbol,Int}}}(nothing), stream_connects=[])
204216
subsys = get_systems(sys)
205217
isempty(subsys) && return sys
206218

207219
# post order traversal
208-
@set! sys.systems = map(s->expand_connections(s, debug=debug, tol=tol), subsys)
220+
@set! sys.systems = map(s->expand_connections(s, debug=debug, tol=tol,
221+
rename=rename, stream_connects=stream_connects), subsys)
209222

210223
outer_connectors = Symbol[]
211224
for s in subsys
@@ -306,7 +319,21 @@ function expand_connections(sys::AbstractSystem; debug=false, tol=1e-10)
306319
end
307320

308321
# stream variables
309-
stream_connects = filter(isstreamconnection, narg_connects)
322+
if rename[] !== nothing
323+
name, depth = rename[]
324+
nc = length(stream_connects)
325+
for i in nc-depth+1:nc
326+
stream_connects[i] = renamespace(:room, stream_connects[i])
327+
end
328+
end
329+
nsc = 0
330+
for c in narg_connects
331+
if isstreamconnection(c)
332+
push!(stream_connects, c)
333+
nsc += 1
334+
end
335+
end
336+
rename[] = nameof(sys), nsc
310337
instream_eqs, additional_eqs = expand_instream(instream_eqs, instream_exprs, stream_connects; debug=debug, tol=tol)
311338

312339
@set! sys.eqs = [eqs; instream_eqs; additional_eqs]
@@ -388,8 +415,8 @@ function expand_instream(instream_eqs, instream_exprs, connects; debug=false, to
388415
connectors = Iterators.flatten((connect.inners, connect.outers))
389416
# stream variable
390417
sv = getproperty(first(connectors), streamvar_name; namespace=false)
391-
inner_sc = connect.inners
392-
outer_sc = connect.outers
418+
inner_sc = something(connect.inners, EMPTY_VEC)
419+
outer_sc = something(connect.outers, EMPTY_VEC)
393420

394421
n_outers = length(outer_sc)
395422
n_inners = length(inner_sc)
@@ -463,7 +490,7 @@ function expand_instream(instream_eqs, instream_exprs, connects; debug=false, to
463490
# additional equations
464491
additional_eqs = Equation[]
465492
for c in connects
466-
outer_sc = c.outers
493+
outer_sc = something(c.outers, EMPTY_VEC)
467494
isempty(outer_sc) && continue
468495
inner_sc = c.inners
469496
n_outers = length(outer_sc)

0 commit comments

Comments
 (0)