@@ -29,12 +29,12 @@ MT = ModelingToolkit
29
29
```
30
30
"""
31
31
equivalent (x, y) = isequal (1 * x, 1 * y)
32
- unitless = Unitful. unit (1 )
32
+ const unitless = Unitful. unit (1 )
33
33
34
34
# For dispatching get_unit
35
- Literal = Union{Sym, Symbolics. ArrayOp, Symbolics. Arr, Symbolics. CallWithMetadata}
36
- Conditional = Union{typeof (ifelse), typeof (IfElse. ifelse)}
37
- Comparison = Union{typeof .([== , != , ≠ , < , <= , ≤ , > , >= , ≥ ])... }
35
+ const Literal = Union{Sym, Symbolics. ArrayOp, Symbolics. Arr, Symbolics. CallWithMetadata}
36
+ const Conditional = Union{typeof (ifelse), typeof (IfElse. ifelse)}
37
+ const Comparison = Union{typeof .([== , != , ≠ , < , <= , ≤ , > , >= , ≥ ])... }
38
38
39
39
" Find the unit of a symbolic item."
40
40
get_unit (x:: Real ) = unitless
@@ -170,6 +170,37 @@ function _validate(terms::Vector, labels::Vector{String}; info::String = "")
170
170
valid
171
171
end
172
172
173
+ function _validate (conn:: Connection ; info:: String = " " )
174
+ valid = true
175
+ syss = get_systems (conn)
176
+ sys = first (syss)
177
+ st = states (sys)
178
+ for i in 2 : length (syss)
179
+ s = syss[i]
180
+ sst = states (s)
181
+ if length (st) != length (sst)
182
+ valid = false
183
+ @warn (" $info : connected systems $(nameof (sys)) and $(nameof (s)) have $(length (st)) and $(length (sst)) states, cannor connect." )
184
+ continue
185
+ end
186
+ for (i, x) in enumerate (st)
187
+ j = findfirst (isequal (x), sst)
188
+ if j == nothing
189
+ valid = false
190
+ @warn (" $info : connected systems $(nameof (sys)) and $(nameof (s)) do not have the same states." )
191
+ else
192
+ aunit = safe_get_unit (x, info * string (nameof (sys)) * " #$i " )
193
+ bunit = safe_get_unit (sst[j], info * string (nameof (s)) * " #$j " )
194
+ if ! equivalent (aunit, bunit)
195
+ valid = false
196
+ @warn (" $info : connected system states $x and $(sst[j]) have mismatched units." )
197
+ end
198
+ end
199
+ end
200
+ end
201
+ valid
202
+ end
203
+
173
204
function validate (jump:: Union {ModelingToolkit. VariableRateJump,
174
205
ModelingToolkit. ConstantRateJump}, t:: Symbolic ;
175
206
info:: String = " " )
@@ -195,7 +226,11 @@ function validate(jumps::ArrayPartition{<:Union{Any, Vector{<:JumpType}}}, t::Sy
195
226
end
196
227
197
228
function validate (eq:: ModelingToolkit.Equation ; info:: String = " " )
198
- _validate ([eq. lhs, eq. rhs], [" left" , " right" ]; info)
229
+ if typeof (eq. lhs) == Connection
230
+ _validate (eq. rhs; info)
231
+ else
232
+ _validate ([eq. lhs, eq. rhs], [" left" , " right" ]; info)
233
+ end
199
234
end
200
235
function validate (eq:: ModelingToolkit.Equation ,
201
236
term:: Union{Symbolic, Unitful.Quantity, Num} ; info:: String = " " )
0 commit comments