Skip to content

Commit

Permalink
Merge pull request #2811 from AayushSabharwal/as/more-metadata
Browse files Browse the repository at this point in the history
feat: dump more variable metadata
  • Loading branch information
ChrisRackauckas authored Jul 1, 2024
2 parents a203b86 + 230eb16 commit 8c45817
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2625,12 +2625,22 @@ See also: [`ModelingToolkit.dump_variable_metadata`](@ref), [`ModelingToolkit.du
"""
function dump_parameters(sys::AbstractSystem)
defs = defaults(sys)
map(dump_variable_metadata.(parameters(sys))) do meta
pdeps = parameter_dependencies(sys)
metas = map(dump_variable_metadata.(parameters(sys))) do meta
if haskey(defs, meta.var)
meta = merge(meta, (; default = defs[meta.var]))
end
meta
end
pdep_metas = map(collect(keys(pdeps))) do sym
val = pdeps[sym]
meta = dump_variable_metadata(sym)
meta = merge(meta,
(; dependency = pdeps[sym],
default = symbolic_evaluate(pdeps[sym], merge(defs, pdeps))))
return meta
end
return vcat(metas, pdep_metas)
end

"""
Expand Down
4 changes: 3 additions & 1 deletion src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function dump_variable_metadata(var)
if desc == ""
desc = nothing
end
default = hasdefault(uvar) ? getdefault(uvar) : nothing
guess = getguess(uvar)
disturbance = isdisturbance(uvar) || nothing
tunable = istunable(uvar, isparameter(uvar))
Expand All @@ -72,7 +73,8 @@ function dump_variable_metadata(var)
disturbance,
tunable,
dist,
type
type,
default
)

return NamedTuple(k => v for (k, v) in pairs(meta) if v !== nothing)
Expand Down
18 changes: 18 additions & 0 deletions test/test_variable_metadata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ using ModelingToolkit
@test hasguess(y) === true
@test ModelingToolkit.dump_variable_metadata(y).guess == 0

# Default
@variables y = 0
@test ModelingToolkit.getdefault(y) === 0
@test ModelingToolkit.hasdefault(y) === true
@test ModelingToolkit.dump_variable_metadata(y).default == 0

# Issue#2653
@variables y[1:3] [guess = ones(3)]
@test getguess(y) == ones(3)
Expand Down Expand Up @@ -124,3 +130,15 @@ sp = Set(p)
@named sys = ODESystem([u ~ p], t)

@test_nowarn show(stdout, "text/plain", sys)

# Defaults overridden by system, parameter dependencies
@variables x(t) = 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
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
@test isequal(params_meta[:q].dependency, 2p)

0 comments on commit 8c45817

Please sign in to comment.