Skip to content

Commit

Permalink
feat: add default_values to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Feb 16, 2024
1 parent 46070b2 commit e75c0cc
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/src/complete_sii.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/SymbolicIndexingInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Check warning on line 143 in src/interface.jl

View check run for this annotation

Codecov / codecov/patch

src/interface.jl#L143

Added line #L143 was not covered by tests

struct SolvedVariables end

"""
Expand Down
1 change: 1 addition & 0 deletions src/symbol_cache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Check warning on line 78 in src/symbol_cache.jl

View check run for this annotation

Codecov / codecov/patch

src/symbol_cache.jl#L78

Added line #L78 was not covered by tests

function Base.copy(sc::SymbolCache)
return SymbolCache(sc.variables === nothing ? nothing : copy(sc.variables),
Expand Down
3 changes: 3 additions & 0 deletions test/symbol_cache_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]))
Expand All @@ -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
Expand Down

0 comments on commit e75c0cc

Please sign in to comment.