|
56 | 56 | Connection(syss) = Connection(inners=syss)
|
57 | 57 | get_systems(c::Connection) = c.inners
|
58 | 58 | 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) |
60 | 71 | end
|
61 | 72 |
|
62 | 73 | const EMPTY_VEC = []
|
@@ -200,12 +211,14 @@ else
|
200 | 211 | end
|
201 | 212 | @register mydiv(n, d)
|
202 | 213 |
|
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=[]) |
204 | 216 | subsys = get_systems(sys)
|
205 | 217 | isempty(subsys) && return sys
|
206 | 218 |
|
207 | 219 | # 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) |
209 | 222 |
|
210 | 223 | outer_connectors = Symbol[]
|
211 | 224 | for s in subsys
|
@@ -306,7 +319,21 @@ function expand_connections(sys::AbstractSystem; debug=false, tol=1e-10)
|
306 | 319 | end
|
307 | 320 |
|
308 | 321 | # 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 |
310 | 337 | instream_eqs, additional_eqs = expand_instream(instream_eqs, instream_exprs, stream_connects; debug=debug, tol=tol)
|
311 | 338 |
|
312 | 339 | @set! sys.eqs = [eqs; instream_eqs; additional_eqs]
|
@@ -388,8 +415,8 @@ function expand_instream(instream_eqs, instream_exprs, connects; debug=false, to
|
388 | 415 | connectors = Iterators.flatten((connect.inners, connect.outers))
|
389 | 416 | # stream variable
|
390 | 417 | 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) |
393 | 420 |
|
394 | 421 | n_outers = length(outer_sc)
|
395 | 422 | n_inners = length(inner_sc)
|
@@ -463,7 +490,7 @@ function expand_instream(instream_eqs, instream_exprs, connects; debug=false, to
|
463 | 490 | # additional equations
|
464 | 491 | additional_eqs = Equation[]
|
465 | 492 | for c in connects
|
466 |
| - outer_sc = c.outers |
| 493 | + outer_sc = something(c.outers, EMPTY_VEC) |
467 | 494 | isempty(outer_sc) && continue
|
468 | 495 | inner_sc = c.inners
|
469 | 496 | n_outers = length(outer_sc)
|
|
0 commit comments