diff --git a/src/SymbolicIndexingInterface.jl b/src/SymbolicIndexingInterface.jl index 93c5398..04db912 100644 --- a/src/SymbolicIndexingInterface.jl +++ b/src/SymbolicIndexingInterface.jl @@ -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, @@ -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 diff --git a/src/index_provider_interface.jl b/src/index_provider_interface.jl index a114450..bd3ff90 100644 --- a/src/index_provider_interface.jl +++ b/src/index_provider_interface.jl @@ -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) diff --git a/test/downstream/interface_tests.jl b/test/downstream/interface_tests.jl new file mode 100644 index 0000000..ce728f6 --- /dev/null +++ b/test/downstream/interface_tests.jl @@ -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) diff --git a/test/example_test.jl b/test/example_test.jl index 521de32..ac1ee19 100644 --- a/test/example_test.jl +++ b/test/example_test.jl @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index b91706c..6b9b7cd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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