From 6d0c202476b7c3fa91f65cd9d2fd62ea4bd703dd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Jun 2024 15:14:44 +0000 Subject: [PATCH 1/3] fix: fix edge cases with metadata dumping, add tests --- src/systems/abstractsystem.jl | 6 +++++- src/variables.jl | 4 ++++ test/test_variable_metadata.jl | 12 +++++++----- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 82c7fc0195..97d5dfc970 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2776,11 +2776,15 @@ ModelingToolkit.dump_unknowns(sys) See also: [`ModelingToolkit.dump_variable_metadata`](@ref), [`ModelingToolkit.dump_parameters`](@ref) """ function dump_unknowns(sys::AbstractSystem) - defs = defaults(sys) + defs = varmap_with_toterm(defaults(sys)) + gs = varmap_with_toterm(guesses(sys)) map(dump_variable_metadata.(unknowns(sys))) do meta if haskey(defs, meta.var) meta = merge(meta, (; default = defs[meta.var])) end + if haskey(gs, meta.var) + meta = merge(meta, (; guess = gs[meta.var])) + end meta end end diff --git a/src/variables.jl b/src/variables.jl index d9c5f71cab..dc52269bff 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -208,6 +208,10 @@ function _varmap_to_vars(varmap::Dict, varlist; defaults = Dict(), check = false return [values[unwrap(var)] for var in varlist] end +function varmap_with_toterm(varmap; toterm = Symbolics.diff2term) + return merge(todict(varmap), Dict(toterm(unwrap(k)) => v for (k, v) in varmap)) +end + function canonicalize_varmap(varmap; toterm = Symbolics.diff2term) new_varmap = Dict() for (k, v) in varmap diff --git a/test/test_variable_metadata.jl b/test/test_variable_metadata.jl index 22832fed98..ddcd30f6b2 100644 --- a/test/test_variable_metadata.jl +++ b/test/test_variable_metadata.jl @@ -131,13 +131,15 @@ sp = Set(p) @test_nowarn show(stdout, "text/plain", sys) -# Defaults overridden by system, parameter dependencies -@variables x(t) = 1.0 +# Defaults, guesses overridden by system, parameter dependencies +@variables x(t)=1.0 y(t) [guess = 1.0] @parameters p=2.0 q @named sys = ODESystem(Equation[], t, [x], [p]; defaults = Dict(x => 2.0, p => 3.0), - parameter_dependencies = [q => 2p]) -x_meta = ModelingToolkit.dump_unknowns(sys)[] -@test x_meta.default == 2.0 + guesses = Dict(y => 2.0), parameter_dependencies = [q => 2p]) +unks_meta = ModelingToolkit.dump_unknowns(sys) +unks_meta = Dict([ModelingToolkit.getname(meta.var) => meta for meta in unks_meta]) +@test unks_meta[:x].default == 2.0 +@test unks_meta[:y].guess == 2.0 params_meta = ModelingToolkit.dump_parameters(sys) params_meta = Dict([ModelingToolkit.getname(meta.var) => meta for meta in params_meta]) @test params_meta[:p].default == 3.0 From 691c0ccdcac93d840cf676833592180caae2ec41 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 23 Jul 2024 17:34:36 +0530 Subject: [PATCH 2/3] fixup! fix: fix edge cases with metadata dumping, add tests --- test/test_variable_metadata.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_variable_metadata.jl b/test/test_variable_metadata.jl index ddcd30f6b2..7f9799edb3 100644 --- a/test/test_variable_metadata.jl +++ b/test/test_variable_metadata.jl @@ -134,7 +134,7 @@ sp = Set(p) # Defaults, guesses overridden by system, parameter dependencies @variables x(t)=1.0 y(t) [guess = 1.0] @parameters p=2.0 q -@named sys = ODESystem(Equation[], t, [x], [p]; defaults = Dict(x => 2.0, p => 3.0), +@named sys = ODESystem(Equation[], t, [x, y], [p]; defaults = Dict(x => 2.0, p => 3.0), guesses = Dict(y => 2.0), parameter_dependencies = [q => 2p]) unks_meta = ModelingToolkit.dump_unknowns(sys) unks_meta = Dict([ModelingToolkit.getname(meta.var) => meta for meta in unks_meta]) From 105bdbef84e84812e61423196c3cb8f52e5acd72 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 23 Jul 2024 17:35:13 +0530 Subject: [PATCH 3/3] fix: fix dumping of non-numeric variable metadata --- src/variables.jl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/variables.jl b/src/variables.jl index dc52269bff..6177748d9b 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -30,8 +30,13 @@ ModelingToolkit.dump_variable_metadata(p) function dump_variable_metadata(var) uvar = unwrap(var) vartype, name = get(uvar.metadata, VariableSource, (:unknown, :unknown)) - shape = Symbolics.shape(var) - if shape == () + type = symtype(uvar) + if type <: AbstractArray + shape = Symbolics.shape(var) + if shape == () + shape = nothing + end + else shape = nothing end unit = get(uvar.metadata, VariableUnit, nothing)