@@ -48,14 +48,11 @@ function screen_unit(result)
4848 if result isa DQ. AbstractQuantity
4949 d = DQ. dimension (result)
5050 if d isa DQ. Dimensions
51- if result != oneunit (result)
52- throw (ValidationError (" $result uses non SI unit. Please use SI unit only." ))
53- end
5451 return result
5552 elseif d isa DQ. SymbolicDimensions
56- throw ( ValidationError ( " $ result uses SymbolicDimensions, please use `u \" m \" ` to instantiate SI unit only. " ))
53+ return DQ . uexpand ( oneunit ( result))
5754 else
58- throw (ValidationError (" $result doesn't use SI unit, please use `u \" m \" ` to instantiate SI unit only. " ))
55+ throw (ValidationError (" $result doesn't have a recognized unit" ))
5956 end
6057 else
6158 throw (ValidationError (" $result doesn't have any unit." ))
@@ -69,7 +66,7 @@ get_literal_unit(x) = screen_unit(something(__get_literal_unit(x), unitless))
6966Find the unit of a symbolic item.
7067"""
7168get_unit (x:: Real ) = unitless
72- get_unit (x:: DQ.AbstractQuantity ) = screen_unit (oneunit (x) )
69+ get_unit (x:: DQ.AbstractQuantity ) = screen_unit (x )
7370get_unit (x:: AbstractArray ) = map (get_unit, x)
7471get_unit (x:: Num ) = get_unit (unwrap (x))
7572get_unit (op:: Differential , args) = get_unit (args[1 ]) / get_unit (op. x)
@@ -81,12 +78,19 @@ get_unit(op::typeof(instream), args) = get_unit(args[1])
8178function get_unit (op, args) # Fallback
8279 result = op (get_unit .(args)... )
8380 try
84- oneunit ( result)
81+ result
8582 catch
8683 throw (ValidationError (" Unable to get unit for operation $op with arguments $args ." ))
8784 end
8885end
8986
87+ function get_unit (:: Union{typeof(+), typeof(-)} , args)
88+ u = get_unit (args[1 ])
89+ if all (i -> get_unit (args[i]) == u, 2 : length (args))
90+ return u
91+ end
92+ end
93+
9094function get_unit (op:: Integral , args)
9195 unit = 1
9296 if op. domain. variables isa Vector
@@ -96,7 +100,7 @@ function get_unit(op::Integral, args)
96100 else
97101 unit *= get_unit (op. domain. variables)
98102 end
99- return oneunit ( get_unit (args[1 ]) * unit)
103+ return get_unit (args[1 ]) * unit
100104end
101105
102106equivalent (x, y) = isequal (x, y)
@@ -197,7 +201,11 @@ function _validate(terms::Vector, labels::Vector{String}; info::String = "")
197201 first_label = label
198202 elseif ! equivalent (first_unit, equnit)
199203 valid = false
200- @warn (" $info : units [$(first_unit) ] for $(first_label) and [$(equnit) ] for $(label) do not match." )
204+ str = " $info : units [$(first_unit) ] for $(first_label) and [$(equnit) ] for $(label) do not match."
205+ if oneunit (first_unit) == oneunit (equnit)
206+ str *= " If there are non-SI units in the system, please use symbolic units like `us\" ms\" `"
207+ end
208+ @warn (str)
201209 end
202210 end
203211 end
@@ -227,7 +235,11 @@ function _validate(conn::Connection; info::String = "")
227235 bunit = safe_get_unit (sst[j], info * string (nameof (s)) * " #$j " )
228236 if ! equivalent (aunit, bunit)
229237 valid = false
230- @warn (" $info : connected system unknowns $x and $(sst[j]) have mismatched units." )
238+ str = " $info : connected system unknowns $x ($aunit ) and $(sst[j]) ($bunit ) have mismatched units."
239+ if oneunit (aunit) == oneunit (bunit)
240+ str *= " If there are non-SI units in the system, please use symbolic units like `us\" ms\" `"
241+ end
242+ @warn (str)
231243 end
232244 end
233245 end
0 commit comments