I have a Symbolics.Equation
that I got from an external source. Now I want to use that equation in ModelingToolkit.jl, but I don’t know how to tell ModelingToolkit.jl which variables are parameters.
For example, here I get my equation from python, and SymPyPythonCall’s conversion from sympy variables to Symbolics.jl variables does not support provenance.
julia> using Symbolics, SymbolicUtils, PythonCall, SymPyPythonCall, ModelingToolkit
julia> pyexec("import sympy", Main)
julia> eq = pyconvert(Equation, pyeval("sympy.core.sympify('Eq(-x*k, diff(diff(f(x), x), x))')", Main))
Differential(x)(Differential(x)(f(x))) ~ -k*x
julia> ODESystem(eq, name="MyODESystem")
ERROR: ArgumentError: Variable k is not a function of independent variable x.
Stacktrace:
[1] check_variables(dvs::Vector{SymbolicUtils.BasicSymbolic{Real}}, iv::SymbolicUtils.BasicSymbolic{Real})
@ ModelingToolkit ~/.julia/packages/ModelingToolkit/brJDK/src/utils.jl:132
[2] ODESystem(tag::UInt64, deqs::Vector{Equation}, iv::SymbolicUtils.BasicSymbolic{Real}, dvs::Vector{SymbolicUtils.BasicSymbolic{Real}}, ps::Vector{Any}, tspan::Nothing, var_to_name::Dict{Any, Any}, ctrls::Vector{Any}, observed::Vector{Equation}, tgrad::Base.RefValue{Vector{Num}}, jac::Base.RefValue{Any}, ctrl_jac::Base.RefValue{Any}, Wfact::Base.RefValue{Matrix{Num}}, Wfact_t::Base.RefValue{Matrix{Num}}, name::String, systems::Vector{ODESystem}, defaults::Dict{Any, Any}, torn_matching::Nothing, connector_type::Nothing, preface::Nothing, cevents::Vector{ModelingToolkit.SymbolicContinuousCallback}, devents::Vector{ModelingToolkit.SymbolicDiscreteCallback}, metadata::Nothing, gui_metadata::Nothing, tearing_state::Nothing, substitutions::Nothing, complete::Bool, discrete_subsystems::Nothing, unknown_states::Nothing; checks::Bool)
@ ModelingToolkit ~/.julia/packages/ModelingToolkit/brJDK/src/systems/diffeqs/odesystem.jl:152
[3] ODESystem(deqs::Vector{Equation}, iv::SymbolicUtils.BasicSymbolic{Real}, dvs::Vector{Any}, ps::OrderedCollections.OrderedSet{Any}; controls::Vector{Num}, observed::Vector{Equation}, systems::Vector{ODESystem}, tspan::Nothing, name::String, default_u0::Dict{Any, Any}, default_p::Dict{Any, Any}, defaults::Dict{Any, Any}, connector_type::Nothing, preface::Nothing, continuous_events::Nothing, discrete_events::Nothing, checks::Bool, metadata::Nothing, gui_metadata::Nothing)
@ ModelingToolkit ~/.julia/packages/ModelingToolkit/brJDK/src/systems/diffeqs/odesystem.jl:218
[4] ODESystem(eqs::Vector{Equation}, iv::Nothing; kwargs::Base.Pairs{Symbol, String, Tuple{Symbol}, NamedTuple{(:name,), Tuple{String}}})
@ ModelingToolkit ~/.julia/packages/ModelingToolkit/brJDK/src/systems/diffeqs/odesystem.jl:268
[5] ODESystem(::Equation; kwargs::Base.Pairs{Symbol, String, Tuple{Symbol}, NamedTuple{(:name,), Tuple{String}}})
@ ModelingToolkit ~/.julia/packages/ModelingToolkit/brJDK/src/systems/diffeqs/odesystem.jl:303
[6] top-level scope
@ REPL[91]:1
julia> fx, k, x = get_variables(eq)
3-element Vector{Any}:
f(x)
k
x
julia> ODESystem(eq,x,[fx],[k], name="MyODESystem")
ERROR: ArgumentError: k is not a parameter.
Stacktrace:
[1] check_parameters(ps::Vector{SymbolicUtils.BasicSymbolic{Real}}, iv::SymbolicUtils.BasicSymbolic{Real})
@ ModelingToolkit ~/.julia/packages/ModelingToolkit/brJDK/src/utils.jl:108
[2] ODESystem(tag::UInt64, deqs::Vector{Equation}, iv::SymbolicUtils.BasicSymbolic{Real}, dvs::Vector{SymbolicUtils.BasicSymbolic{Real}}, ps::Vector{SymbolicUtils.BasicSymbolic{Real}}, tspan::Nothing, var_to_name::Dict{Any, Any}, ctrls::Vector{Any}, observed::Vector{Equation}, tgrad::Base.RefValue{Vector{Num}}, jac::Base.RefValue{Any}, ctrl_jac::Base.RefValue{Any}, Wfact::Base.RefValue{Matrix{Num}}, Wfact_t::Base.RefValue{Matrix{Num}}, name::String, systems::Vector{ODESystem}, defaults::Dict{Any, Any}, torn_matching::Nothing, connector_type::Nothing, preface::Nothing, cevents::Vector{ModelingToolkit.SymbolicContinuousCallback}, devents::Vector{ModelingToolkit.SymbolicDiscreteCallback}, metadata::Nothing, gui_metadata::Nothing, tearing_state::Nothing, substitutions::Nothing, complete::Bool, discrete_subsystems::Nothing, unknown_states::Nothing; checks::Bool)
@ ModelingToolkit ~/.julia/packages/ModelingToolkit/brJDK/src/systems/diffeqs/odesystem.jl:153
[3] ODESystem(deqs::Vector{Equation}, iv::SymbolicUtils.BasicSymbolic{Real}, dvs::Vector{SymbolicUtils.BasicSymbolic{Real}}, ps::Vector{SymbolicUtils.BasicSymbolic{Real}}; controls::Vector{Num}, observed::Vector{Equation}, systems::Vector{ODESystem}, tspan::Nothing, name::String, default_u0::Dict{Any, Any}, default_p::Dict{Any, Any}, defaults::Dict{Any, Any}, connector_type::Nothing, preface::Nothing, continuous_events::Nothing, discrete_events::Nothing, checks::Bool, metadata::Nothing, gui_metadata::Nothing)
@ ModelingToolkit ~/.julia/packages/ModelingToolkit/brJDK/src/systems/diffeqs/odesystem.jl:218
[4] ODESystem(::Equation, ::SymbolicUtils.BasicSymbolic{Real}, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, String, Tuple{Symbol}, NamedTuple{(:name,), Tuple{String}}})
@ ModelingToolkit ~/.julia/packages/ModelingToolkit/brJDK/src/systems/diffeqs/odesystem.jl:303
[5] top-level scope
@ REPL[93]:1
How should I tell ModelingToolkit.jl which variables are parameters and which are variables?