Skip to content

Commit

Permalink
feat: add name_to_symbolic
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Jun 13, 2024
1 parent 5d00f2f commit ccec0c3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/SymbolicIndexingInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ using Accessors: @reset
RuntimeGeneratedFunctions.init(@__MODULE__)

export ScalarSymbolic, ArraySymbolic, NotSymbolic, symbolic_type, hasname, getname,
Timeseries, NotTimeseries, is_timeseries, is_parameter_timeseries
Timeseries, NotTimeseries, is_timeseries, is_parameter_timeseries,
symbolic_evaluate
include("trait.jl")

export is_variable, variable_index, variable_symbols, is_parameter, parameter_index,
Expand All @@ -17,7 +18,7 @@ export is_variable, variable_index, variable_symbols, is_parameter, parameter_in
is_observed, observed, parameter_observed, ParameterObservedFunction,
is_time_dependent, constant_structure, symbolic_container,
all_variable_symbols, all_symbols, solvedvariables, allvariables, default_values,
symbolic_evaluate
name_to_symbolic
include("index_provider_interface.jl")

export SymbolCache
Expand Down
19 changes: 19 additions & 0 deletions src/index_provider_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,25 @@ independent variables.
"""
all_symbols(indp) = all_symbols(symbolic_container(indp))

"""
name_to_symbolic(indp, sym::Symbol)
Return the symbolic variable in `indp` with name `sym`. If there is no such variable,
return `nothing`. This roughly corresponds to the reverse operation of [`getname`](@ref),
in the context of index provider `indp`. By default, this method will search through
`all_symbols(indp)` for one with the required name.
See also: [`all_symbols`](@ref).
"""
function name_to_symbolic(indp, sym::Symbol)
for s in all_symbols(indp)
if hasname(s) && getname(s) == sym
return s
end
end
return nothing
end

"""
default_values(indp)
Expand Down
10 changes: 10 additions & 0 deletions test/downstream/interface_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using ModelingToolkit
using ModelingToolkit: t_nounits as t
using SymbolicIndexingInterface

@variables x(t)[1:2] y(t)
@named sys = ODESystem(Equation[], t, [x, y], [])
sys = complete(sys)

@test isequal(name_to_symbolic(sys, :x), x)
@test isequal(name_to_symbolic(sys, :y), y)
2 changes: 2 additions & 0 deletions test/example_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ sys = SystemMockup(true, [:x, :y, :z], [:a, :b, :c], :t)
@test independent_variable_symbols(sys) == [:t]
@test all_variable_symbols(sys) == [:x, :y, :z]
@test sort(all_symbols(sys)) == [:a, :b, :c, :t, :x, :y, :z]
@test name_to_symbolic.((sys,), all_symbols(sys)) == all_symbols(sys)
@test name_to_symbolic(sys, :q) === nothing
@test default_values(sys) == Dict() # fallback even if not implemented

sys = SystemMockup(true, [:x, :y, :z], [:a, :b, :c], nothing)
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,10 @@ if GROUP == "All" || GROUP == "Downstream"
@safetestset "BatchedInterface with array symbolics test" begin
@time include("downstream/batchedinterface_arrayvars.jl")
end
@safetestset "Remake with array symbolics test" begin
@time include("downstream/remake_arrayvars.jl")
end
@safetestset "MTK interface tests" begin
@time include("downstream/interface_tests.jl")
end
end

0 comments on commit ccec0c3

Please sign in to comment.