From e75c0cc8b5cf01c1f628ddc5fd295a6d7f423c7c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 16 Feb 2024 12:35:01 +0530 Subject: [PATCH] feat: add `default_values` to interface --- docs/src/complete_sii.md | 5 +++++ src/SymbolicIndexingInterface.jl | 2 +- src/interface.jl | 8 ++++++++ src/symbol_cache.jl | 1 + test/symbol_cache_test.jl | 3 +++ 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/src/complete_sii.md b/docs/src/complete_sii.md index e5d592a..0b795df 100644 --- a/docs/src/complete_sii.md +++ b/docs/src/complete_sii.md @@ -8,6 +8,7 @@ struct ExampleSystem state_index::Dict{Symbol,Int} parameter_index::Dict{Symbol,Int} independent_variable::Union{Symbol,Nothing} + defaults::Dict{Symbol, Float64} # mapping from observed variable to Expr to calculate its value observed::Dict{Symbol,Expr} end @@ -77,6 +78,10 @@ function SymbolicIndexingInterface.all_symbols(sys::ExampleSystem) sys.independent_variable === nothing ? Symbol[] : sys.independent_variable ) end + +function SymbolicIndexingInterface.default_values(sys::ExampleSystem) + return sys.defaults +end ``` ### Observed Equation Handling diff --git a/src/SymbolicIndexingInterface.jl b/src/SymbolicIndexingInterface.jl index 4edcb21..235cd85 100644 --- a/src/SymbolicIndexingInterface.jl +++ b/src/SymbolicIndexingInterface.jl @@ -8,7 +8,7 @@ export is_variable, variable_index, variable_symbols, is_parameter, parameter_in is_observed, observed, is_time_dependent, constant_structure, symbolic_container, all_variable_symbols, - all_symbols, solvedvariables, allvariables + all_symbols, solvedvariables, allvariables, default_values include("interface.jl") export SymbolCache diff --git a/src/interface.jl b/src/interface.jl index 36dac86..e7605cd 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -134,6 +134,14 @@ variables. """ all_symbols(sys) = all_symbols(symbolic_container(sys)) +""" + default_values(sys) + +Return a dictionary mapping symbols in the system to their default value, if any. This includes +parameter symbols. +""" +default_values(sys) = default_values(symbolic_container(sys)) + struct SolvedVariables end """ diff --git a/src/symbol_cache.jl b/src/symbol_cache.jl index 5a2a81c..b923e1a 100644 --- a/src/symbol_cache.jl +++ b/src/symbol_cache.jl @@ -75,6 +75,7 @@ all_variable_symbols(sc::SymbolCache) = variable_symbols(sc) function all_symbols(sc::SymbolCache) vcat(variable_symbols(sc), parameter_symbols(sc), independent_variable_symbols(sc)) end +default_values(::SymbolCache) = Dict() function Base.copy(sc::SymbolCache) return SymbolCache(sc.variables === nothing ? nothing : copy(sc.variables), diff --git a/test/symbol_cache_test.jl b/test/symbol_cache_test.jl index 5fb1b03..5f57b12 100644 --- a/test/symbol_cache_test.jl +++ b/test/symbol_cache_test.jl @@ -19,6 +19,7 @@ sc = SymbolCache([:x, :y, :z], [:a, :b], [:t]) @test independent_variable_symbols(sc) == [:t] @test all_variable_symbols(sc) == [:x, :y, :z] @test sort(all_symbols(sc)) == [:a, :b, :t, :x, :y, :z] +@test isempty(default_values(sc)) sc = SymbolCache([:x, :y], [:a, :b]) @test !is_time_dependent(sc) @@ -38,6 +39,7 @@ sc = SymbolCache() @test !is_time_dependent(sc) @test all_variable_symbols(sc) == [] @test all_symbols(sc) == [] +@test isempty(default_values(sc)) sc = SymbolCache(nothing, nothing, :t) @test all(.!is_independent_variable.((sc,), [:x, :y, :a, :b])) @@ -46,6 +48,7 @@ sc = SymbolCache(nothing, nothing, :t) @test is_time_dependent(sc) @test all_variable_symbols(sc) == [] @test all_symbols(sc) == [:t] +@test isempty(default_values(sc)) sc2 = copy(sc) @test sc.variables == sc2.variables