-
-
Notifications
You must be signed in to change notification settings - Fork 228
Fix unit validation for dimensionally compatible quantities (fixes #3787) #3838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
) This commit fixes the unit validation system to properly handle dimensionally compatible quantities with different scales (e.g., MW vs kW, V vs mV). The issue was that the `equivalent()` function used exact equality (`isequal`) instead of dimensional compatibility checking. This caused validation to fail for physically meaningful equations like `D(E) ~ P - E/τ` where E is in kJ, P is in MW, and τ is in ms, even though kJ/ms = kW and MW and kW are dimensionally compatible (both power units). Changes: - Updated `equivalent()` function to check dimensional compatibility using `DQ.dimension()` for DynamicQuantities instead of exact equality - Added `equivalent_strict()` function for connection validation that maintains exact equality requirements for electrical connector safety - Updated connection validation to use strict equivalence to prevent connecting V and mV connectors (different voltage scales) - Added comprehensive tests including the original bug case and edge cases The fix allows the example from issue #3787 to validate correctly while maintaining safety for electrical connections. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This is unsafe. |
Benchmark Results (Julia v1)Time benchmarks
Memory benchmarks
|
Benchmark Results (Julia vlts)Time benchmarks
Memory benchmarks
|
q = x isa DQ.AbstractQuantity ? x : y | ||
non_q = x isa DQ.AbstractQuantity ? y : x | ||
# Only consider equivalent if the quantity is dimensionless and other is zero/number | ||
return DQ.dimension(q) == DQ.dimension(DQ.Quantity(1.0)) && (isequal(non_q, 0) || non_q isa Number) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶
return DQ.dimension(q) == DQ.dimension(DQ.Quantity(1.0)) && (isequal(non_q, 0) || non_q isa Number) | |
return DQ.dimension(q) == DQ.dimension(DQ.Quantity(1.0)) && | |
(isequal(non_q, 0) || non_q isa Number) |
@parameters τ_ms [unit = u"ms"] | ||
@variables t_ms [unit = u"ms"] E_kJ(t_ms) [unit = u"kJ"] P_MW(t_ms) [unit = u"MW"] | ||
D_ms = Differential(t_ms) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶
|
||
# This should pass: kJ/ms = kW, and MW and kW are dimensionally compatible (both power) | ||
eqs_3787 = [D_ms(E_kJ) ~ P_MW - E_kJ / τ_ms, | ||
0 ~ P_MW] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶
0 ~ P_MW] | |
0 ~ P_MW] |
eqs_3787 = [D_ms(E_kJ) ~ P_MW - E_kJ / τ_ms, | ||
0 ~ P_MW] | ||
@test MT.validate(eqs_3787) == true | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶
@variables x_m [unit = u"m"] y_km [unit = u"km"] | ||
eq_scales = [x_m ~ y_km] | ||
@test MT.validate(eq_scales) == true | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶
Summary
This PR fixes the unit validation system to properly handle dimensionally compatible quantities with different scales, resolving issue #3787.
Problem
The unit validation was failing for physically meaningful equations like:
The issue was that
kJ/ms = kW
andMW
andkW
are dimensionally compatible (both power units), but the validation used exact equality instead of dimensional compatibility.Root Cause
The
equivalent(x, y)
function insrc/systems/unit_check.jl
was defined as simplyisequal(x, y)
, which meant:1.0e6 m² kg s⁻³
(MW) and1.0e3 m² kg s⁻³
(kW) were not considered equivalentSolution
equivalent()
function: Now checks dimensional compatibility usingDQ.dimension()
for DynamicQuantities instead of exact equalityequivalent_strict()
function: Maintains exact equality for connection validation to ensure electrical safetyChanges
Test Cases
✅ Issue #3787 case:
MW
andkJ/ms
(= kW) now validate as compatible✅ Scale compatibility:
m
andkm
validate as compatible✅ Connection safety:
V
andmV
connectors still fail validation✅ Incompatible dimensions:
m
ands
still fail validationExample Usage
Closes #3787
🤖 Generated with Claude Code