Skip to content

contextual variable metadata (like units) is lost when writing System to file  #1123

@anandijain

Description

@anandijain
using ModelingToolkit, OrdinaryDiffEq, Unitful

@parameters t σ ρ β
@variables x(t) y(t) z(t)
x = setmetadata(x, Symbolics.option_to_metadata_type(Val(:unit)), u"m/s")
y = setmetadata(y, Symbolics.option_to_metadata_type(Val(:unit)), u"m/s")
z = setmetadata(z, Symbolics.option_to_metadata_type(Val(:unit)), u"m/s")
D = Differential(t)

eqs = [D(x) ~ σ*(y-x),
       D(y) ~ x*-z)-y,
       D(z) ~ x*y - β*z]

sys = ODESystem(eqs)
open("lorenz_units.jl", "w") do io
    write(io, sys)
end

gives

begin
    var"##iv#264" = (@variables(t))[1]
    var"##sts#265" = (collect)(@variables(x(t), y(t), z(t)))
    var"##ps#266" = (collect)(@parameters(σ, ρ, β))
    var"##eqs#267" = [
        (Differential(t))(x) ~ σ * (y + -1x)
        (Differential(t))(y) ~ x *+ -1z) + -1y
        (Differential(t))(z) ~ x * y + -1 * β * z
    ]
    var"##defs#268" = (Dict)()
    (ODESystem)(
        var"##eqs#267",
        var"##iv#264",
        var"##sts#265",
        var"##ps#266";
        defaults = var"##defs#268",
    )
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions