ModelingToolkit.jl icon indicating copy to clipboard operation
ModelingToolkit.jl copied to clipboard

Validation of units broken

Open kraqbi opened this issue 8 months ago • 1 comments

Describe the bug 🐞 Hello! The example here : https://docs.sciml.ai/ModelingToolkit/stable/basics/Validation/#DynamicQuantities-Literals Does not work as intended. Its goal is to validate the dimensionalitty of the equations according to the units given.

Expected behavior

It should return "true" but does return "false".

Minimal Reproducible Example 👇

using ModelingToolkit, DynamicQuantities
@parameters τ [unit = u"ms"]
@variables t [unit = u"ms"] E(t) [unit = u"kJ"] P(t) [unit = u"MW"]
D = Differential(t)
eqs = eqs = [D(E) ~ P - E / τ,
    0 ~ P]
ModelingToolkit.validate(eqs)

Error & Stacktrace ⚠️

┌ Warning:  in eq. #1right, in sum P(t) + (-E(t)) / τ, units [1.0e6 m² kg s⁻³, 1.0 m² kg s⁻³] do not match.
└ @ ModelingToolkit ~/.julia/packages/ModelingToolkit/US9uJ/src/systems/unit_check.jl:181
false

Environment (please complete the following information):

  • Output of using Pkg; Pkg.status()
  [06fc5a27] DynamicQuantities v1.8.0
  [961ee093] ModelingToolkit v10.7.0
  • Output of versioninfo()
Julia Version 1.11.5
Commit 760b2e5b739 (2025-04-14 06:53 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 20 × 13th Gen Intel(R) Core(TM) i7-13800H
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, goldmont)
Threads: 1 default, 0 interactive, 1 GC (on 20 virtual cores)

Additional context

Question first raised here (https://discourse.julialang.org/t/unit-validation-and-inspection-with-mtk-error-in-the-documentation/112145) . Please tell me if I do not use the right form in that issue, this is my first time doing so.

kraqbi avatar Jul 01 '25 16:07 kraqbi

Describe the bug 🐞

This seems to be a similar bug with the one posted in this issue. When a constant is defined with a non-SI unit, and then involved in a multiplication (or possibly other operations) with a unitless constant, the resulting unit is incorrectly converted to SI. This results in a loss of the original unit and causes unit inconsistency issues in further computations. The values are correct, but won't pass the unit check.

Expected behavior

A constant assigned with non-SI unit is expected to keep its original unit after operation with a unitless constant. For example, the unit of a constant defined with a non-SI unit (e.g., cm^3*s^-1) should be preserved after multiplied with unitless constants (1.0, or exp(1.0/1.0)).

Minimal Reproducible Example 👇

#################################################################

    using ModelingToolkit
    using DynamicQuantities
    @constants T_0 = 298 [unit = u"K"]###using u to declare the units
    @constants b_0 = 1.0 [unit = u"cm^3*s^-1"] # cm^3/molec/s
    unit1 = ModelingToolkit.get_unit(b_0) #1.0000000000000002e-6 m³ s⁻¹
    unit2 = ModelingToolkit.get_unit(b_0* exp(T_0/T_0)) #outputs 1.0 m³ s⁻¹, but it should be 1.0000000000000002e-6 m³ s⁻¹
    unit3 = ModelingToolkit.get_unit(b_0* 10.0) #1.0 m³ s⁻¹
    unit1 == unit2 #false
    unit1 == unit3 #false
    val = substitute(b_0*exp(b_0/b_0), Dict(b_0 => 1u"cm^3*s^-1")) #2.7182818284590457e-6 m³ s⁻¹ #this is correct
#################################################################


#################################################################
    using ModelingToolkit
    using DynamicQuantities
    @constants T_0 = 298 [unit = us"K"]###using us to declare the units
    @constants b_0 = 1.0 [unit = us"cm^3*s^-1"] # cm^3/molec/s
    unit1 = ModelingToolkit.get_unit(b_0) #1.0000000000000002e-6 m³ s⁻¹
    unit2 = ModelingToolkit.get_unit(b_0* exp(T_0/T_0)) #outputs 1.0 m³ s⁻¹, but it should be 1.0000000000000002e-6 m³ s⁻¹
    unit3 = ModelingToolkit.get_unit(b_0* 10.0) #1.0 m³ s⁻¹
    unit1 == unit2 #false
    unit1 == unit3 #false
    val = substitute(b_0*exp(b_0/b_0), Dict(b_0 => 1u"cm^3*s^-1")) #2.7182818284590457e-6 m³ s⁻¹ #this is correct
#################################################################

xk-y avatar Jul 16 '25 13:07 xk-y