@@ -48,14 +48,11 @@ function screen_unit(result)
48
48
if result isa DQ. AbstractQuantity
49
49
d = DQ. dimension (result)
50
50
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
54
51
return result
55
52
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))
57
54
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" ))
59
56
end
60
57
else
61
58
throw (ValidationError (" $result doesn't have any unit." ))
@@ -69,7 +66,7 @@ get_literal_unit(x) = screen_unit(something(__get_literal_unit(x), unitless))
69
66
Find the unit of a symbolic item.
70
67
"""
71
68
get_unit (x:: Real ) = unitless
72
- get_unit (x:: DQ.AbstractQuantity ) = screen_unit (oneunit (x) )
69
+ get_unit (x:: DQ.AbstractQuantity ) = screen_unit (x )
73
70
get_unit (x:: AbstractArray ) = map (get_unit, x)
74
71
get_unit (x:: Num ) = get_unit (unwrap (x))
75
72
get_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])
81
78
function get_unit (op, args) # Fallback
82
79
result = op (get_unit .(args)... )
83
80
try
84
- oneunit ( result)
81
+ result
85
82
catch
86
83
throw (ValidationError (" Unable to get unit for operation $op with arguments $args ." ))
87
84
end
88
85
end
89
86
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
+
90
94
function get_unit (op:: Integral , args)
91
95
unit = 1
92
96
if op. domain. variables isa Vector
@@ -96,7 +100,7 @@ function get_unit(op::Integral, args)
96
100
else
97
101
unit *= get_unit (op. domain. variables)
98
102
end
99
- return oneunit ( get_unit (args[1 ]) * unit)
103
+ return get_unit (args[1 ]) * unit
100
104
end
101
105
102
106
equivalent (x, y) = isequal (x, y)
@@ -197,7 +201,11 @@ function _validate(terms::Vector, labels::Vector{String}; info::String = "")
197
201
first_label = label
198
202
elseif ! equivalent (first_unit, equnit)
199
203
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)
201
209
end
202
210
end
203
211
end
@@ -227,7 +235,11 @@ function _validate(conn::Connection; info::String = "")
227
235
bunit = safe_get_unit (sst[j], info * string (nameof (s)) * " #$j " )
228
236
if ! equivalent (aunit, bunit)
229
237
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)
231
243
end
232
244
end
233
245
end
0 commit comments