@@ -118,6 +118,7 @@ function connect(c::Connection; check=true)
118
118
end
119
119
120
120
instream (a) = term (instream, unwrap (a), type= symtype (a))
121
+ SymbolicUtils. promote_symtype (:: typeof (instream), _) = Real
121
122
122
123
isconnector (s:: AbstractSystem ) = has_connector_type (s) && get_connector_type (s) != = nothing
123
124
isstreamconnector (s:: AbstractSystem ) = isconnector (s) && get_connector_type (s) isa StreamConnector
@@ -383,7 +384,7 @@ function expand_connections(sys::AbstractSystem; debug=false)
383
384
return sys
384
385
end
385
386
386
- function collect_connections (sys:: AbstractSystem ; debug= false )
387
+ function collect_connections (sys:: AbstractSystem ; debug= false , tol = 1e-10 )
387
388
subsys = get_systems (sys)
388
389
isempty (subsys) && return sys
389
390
@@ -494,13 +495,13 @@ function collect_connections(sys::AbstractSystem; debug=false)
494
495
# stream variables
495
496
stream_connects = filter (isstreamconnection, narg_connects)
496
497
@show length (stream_connects)
497
- instream_eqs, additional_eqs = expand_instream (instream_eqs, instream_exprs, stream_connects; debug= debug)
498
+ instream_eqs, additional_eqs = expand_instream (instream_eqs, instream_exprs, stream_connects; debug= debug, tol = tol )
498
499
499
500
@set! sys. eqs = [eqs; instream_eqs; additional_eqs]
500
501
return sys
501
502
end
502
503
503
- function expand_instream (instream_eqs, instream_exprs, connects; debug= false )
504
+ function expand_instream (instream_eqs, instream_exprs, connects; debug= false , tol )
504
505
sub = Dict ()
505
506
seen = Set ()
506
507
for ex in instream_exprs
@@ -535,7 +536,6 @@ function expand_instream(instream_eqs, instream_exprs, connects; debug=false)
535
536
connector_name === only (inner_names) || error (" $var is not in any stream connector of $(nameof (ogsys)) " )
536
537
sub[ex] = var
537
538
elseif n_inners == 2 && n_outers == 0
538
- @info connector_name collect (inner_names) length (inner_sc)
539
539
connector_name in inner_names || error (" $var is not in any stream connector of $(nameof (ogsys)) " )
540
540
idx = findfirst (c-> nameof (c) === connector_name, inner_sc)
541
541
other = idx == 1 ? 2 : 1
@@ -550,9 +550,9 @@ function expand_instream(instream_eqs, instream_exprs, connects; debug=false)
550
550
end
551
551
else
552
552
fv = flowvar (first (connectors))
553
- idx = findfirst (c-> nameof (c) === connector_name, inner_sc)
554
- if idx != = nothing
555
- si = sum (s-> max (states (s, fv), 0 ), outer_sc)
553
+ i = findfirst (c-> nameof (c) === connector_name, inner_sc)
554
+ if i != = nothing
555
+ si = isempty (outer_sc) ? 0 : sum (s-> max (states (s, fv), 0 ), outer_sc)
556
556
for j in 1 : n_inners; j == i && continue
557
557
f = states (inner_sc[j], fv)
558
558
si += max (- f, 0 )
@@ -586,7 +586,9 @@ function expand_instream(instream_eqs, instream_exprs, connects; debug=false)
586
586
inner_sc = c. inners
587
587
n_outers = length (outer_sc)
588
588
n_inners = length (inner_sc)
589
- for sv in get_states (first (outer_sc))
589
+ connector_representative = first (outer_sc)
590
+ fv = flowvar (connector_representative)
591
+ for sv in get_states (connector_representative)
590
592
vtype = getmetadata (sv, ModelingToolkit. VariableConnectType, nothing )
591
593
vtype === Stream || continue
592
594
if n_inners == 1 && n_outers == 1
@@ -600,11 +602,12 @@ function expand_instream(instream_eqs, instream_exprs, connects; debug=false)
600
602
push! (additional_eqs, v1 ~ instream (v2))
601
603
push! (additional_eqs, v2 ~ instream (v1))
602
604
else
605
+ sq = 0
603
606
for q in 1 : n_outers
604
607
sq += sum (s-> max (- states (s, fv), 0 ), inner_sc)
605
608
for k in 1 : n_outers; k == q && continue
606
- f = states (outer_sc[j ], fv)
607
- si += max (f, 0 )
609
+ f = states (outer_sc[k ], fv)
610
+ sq += max (f, 0 )
608
611
end
609
612
610
613
num = 0
@@ -629,6 +632,7 @@ function expand_instream(instream_eqs, instream_exprs, connects; debug=false)
629
632
630
633
instream_eqs = map (Base. Fix2 (substitute, sub), instream_eqs)
631
634
if debug
635
+ println (" ===========BEGIN=============" )
632
636
println (" Expanded equations:" )
633
637
for eq in instream_eqs
634
638
print_with_indent (4 , eq)
@@ -639,6 +643,7 @@ function expand_instream(instream_eqs, instream_exprs, connects; debug=false)
639
643
print_with_indent (4 , eq)
640
644
end
641
645
end
646
+ println (" ============END==============" )
642
647
end
643
648
return instream_eqs, additional_eqs
644
649
end
0 commit comments