Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add name_to_symbolic #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading