1
+ get_connection_type (s) = getmetadata (unwrap (s), VariableConnectType, Equality)
2
+
1
3
function with_connector_type (expr)
2
4
@assert expr isa Expr && (expr. head == :function || (expr. head == :(= ) &&
3
5
expr. args[1 ] isa Expr &&
@@ -34,9 +36,12 @@ function connector_type(sys::AbstractSystem)
34
36
n_stream = 0
35
37
n_flow = 0
36
38
for s in sts
37
- vtype = getmetadata (s, ModelingToolkit. VariableConnectType, nothing )
38
- vtype === Stream && (n_stream += 1 )
39
- vtype === Flow && (n_flow += 1 )
39
+ vtype = get_connection_type (s)
40
+ if vtype === Stream
41
+ isarray (s) && error (" Array stream variables are not supported. Got $s ." )
42
+ n_stream += 1
43
+ end
44
+ vtype === Flow && (n_flow += 1 )
40
45
end
41
46
(n_stream > 0 && n_flow > 1 ) && error (" There are multiple flow variables in $(nameof (sys)) !" )
42
47
n_stream > 0 ? StreamConnector () : RegularConnector ()
@@ -84,6 +89,7 @@ function connect(c::Connection; check=true)
84
89
flow_eqs = Equation[]
85
90
other_eqs = Equation[]
86
91
92
+ ncnts = length (inners) + length (outers)
87
93
cnts = Iterators. flatten ((inners, outers))
88
94
fs, ss = Iterators. peel (cnts)
89
95
splitting_idx = length (inners) # anything after the splitting_idx is outer.
@@ -94,26 +100,47 @@ function connect(c::Connection; check=true)
94
100
Set (current_sts) == first_sts_set || error (" $(nameof (sys)) ($current_sts ) doesn't match the connection type of $(nameof (fs)) ($first_sts )." )
95
101
end
96
102
103
+ seen = Set ()
97
104
ceqs = Equation[]
98
105
for s in first_sts
99
106
name = getname (s)
100
- vtype = getmetadata (s, VariableConnectType, Equality)
107
+ fix_val = getproperty (fs, name) # representative
108
+ fix_val in seen && continue
109
+ push! (seen, fix_val)
110
+
111
+ vtype = get_connection_type (fix_val)
101
112
vtype === Stream && continue
102
- isflow = vtype === Flow
103
- rhs = 0 # only used for flow variables
104
- fix_val = getproperty (fs, name) # used for equality connections
105
- for (i, c) in enumerate (cnts)
106
- isinner = i <= splitting_idx
107
- # https://specification.modelica.org/v3.4/Ch15.html
108
- var = getproperty (c, name)
109
- if isflow
113
+
114
+ isarr = isarray (fix_val)
115
+
116
+ if vtype === Flow
117
+ rhs = isarr ? zeros (Int, ncnts) : 0
118
+ for (i, c) in enumerate (cnts)
119
+ isinner = i <= splitting_idx
120
+ # https://specification.modelica.org/v3.4/Ch15.html
121
+ var = scalarize (getproperty (c, name))
110
122
rhs += isinner ? var : - var
123
+ end
124
+ if isarr
125
+ for r in rhs
126
+ push! (ceqs, 0 ~ r)
127
+ end
111
128
else
112
- i == 1 && continue # skip the first iteration
113
- push! (ceqs, fix_val ~ getproperty (c, name))
129
+ push! (ceqs, 0 ~ rhs)
130
+ end
131
+ else # Equality
132
+ for c in ss
133
+ var = getproperty (c, name)
134
+ if isarr
135
+ vs = scalarize (var)
136
+ for (i, v) in enumerate (vs)
137
+ push! (ceqs, fix_val[i] ~ v)
138
+ end
139
+ else
140
+ push! (ceqs, fix_val ~ var)
141
+ end
114
142
end
115
143
end
116
- isflow && push! (ceqs, 0 ~ rhs)
117
144
end
118
145
119
146
return ceqs
144
171
function flowvar (sys:: AbstractSystem )
145
172
sts = get_states (sys)
146
173
for s in sts
147
- vtype = getmetadata (s, ModelingToolkit . VariableConnectType, nothing )
174
+ vtype = get_connection_type (s )
148
175
vtype === Flow && return s
149
176
end
150
177
error (" There in no flow variable in $(nameof (sys)) " )
@@ -365,7 +392,7 @@ function expand_instream(instream_eqs, instream_exprs, connects; debug=false, to
365
392
connector_representative = first (outer_sc)
366
393
fv = flowvar (connector_representative)
367
394
for sv in get_states (connector_representative)
368
- vtype = getmetadata (sv, ModelingToolkit . VariableConnectType, nothing )
395
+ vtype = get_connection_type (sv)
369
396
vtype === Stream || continue
370
397
if n_inners == 1 && n_outers == 1
371
398
innerstream = states (only (inner_sc), sv)
0 commit comments