Equation handling in a MTK

Hello,
I am playing with a toy problem. I apologies in advance if it is a trivial question.
I am curious on how the equations of a model are handled.
For example the following code gives expected results:

using ModelingToolkit
using DifferentialEquations: solve
@variables t
@mtkmodel LinearEq begin
	@parameters begin
        
     end
  @variables begin
    x(t)
    y(t)
  end
  @equations begin
    10 ~ x + y
    15 ~ x
  end
end

@mtkbuild eq = LinearEq()
prob = ODEProblem(eq,[],(0,1),[])
sol = solve(prob)

while the following give error:

using ModelingToolkit
using DifferentialEquations: solve
@variables t
@mtkmodel LinearEq begin
	@parameters begin
        
     end
  @variables begin
    x(t)
    y(t)
  end
  @equations begin
    10 ~ x + y
    15 ~ x - y
  end
end

@mtkbuild eq = LinearEq()
prob = ODEProblem(eq,[],(0,1),[])
sol = solve(prob)

the error message being:

ERROR: LoadError: ArgumentError: SymbolicUtils.BasicSymbolic{Real}[y(t)] are missing from the variable map.

So in the first case the solver identified that the actual equation to solve was only for y by substituting the value of x. While in the second case it cannot exactly identify that it is a linear system which can be solved by substitutions.

It is possible to explain why the second case does not work while the first one works? And is there a way for the second case to work?

Thank you

This is not on v9?

I was using 8.75
I have now updated to 9.1.0 and I have the same issue.

The new initialization was added in 9.3. I would upgrade to that and follow the tutorial

Hello,
I get the same issue. I have tried 9.3.0 and 9.4.0

You should have a new error now that mentions that you’re missing a guess on y?

I still get the same error. The version I am using now is v9.3.0

ERROR: LoadError: ArgumentError: SymbolicUtils.BasicSymbolic{Real}[y(t)] are missing from the variable map.

I’ll look into why this isn’t giving the better error message. The issue of course is that you’re missing guesses for the nonlinear solver:

using ModelingToolkit
using DifferentialEquations: solve
@variables t
@mtkmodel LinearEq begin
	@parameters begin
        
     end
  @variables begin
    x(t)
    y(t)
  end
  @equations begin
    10 ~ x + y
    15 ~ x - y
  end
end

@mtkbuild eq = LinearEq()
prob = ODEProblem(eq,[],(0,1),[], guesses = [eq.x => 0.0, eq.y => 0.0])
sol = solve(prob)

But why it gives the less informative error is worth investigating. This is the expected error:

julia> prob = ODEProblem(eq,[],(0,1),[], guesses = [eq.x => 0.0])
ERROR: Initial condition underdefined. Some are missing from the variable map.
Please provide a default (`u0`), initialization equation, or guess
for the following variables:

SymbolicUtils.BasicSymbolic{Real}[y(t)]

Stacktrace:
  [1] _varmap_to_vars(varmap::Dict{…}, varlist::Vector{…}; defaults::Dict{…}, check::Bool, toterm::typeof(ModelingToolkit.default_toterm), initialization_phase::Bool)
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/variables.jl:198
  [2] varmap_to_vars(varmap::Vector{…}, varlist::Vector{…}; defaults::Dict{…}, check::Bool, toterm::Function, promotetoconcrete::Nothing, tofloat::Bool, use_union::Bool)
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/variables.jl:152
  [3] get_u0(sys::NonlinearSystem, u0map::Vector{Pair{Num, Float64}}, parammap::Vector{Any}; symbolic_u0::Bool)
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:0
  [4] get_u0
    @ ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:815 [inlined]
  [5] process_NonlinearProblem(constructor::Type, sys::NonlinearSystem, u0map::Vector{…}, parammap::Vector{…}; version::Nothing, jac::Bool, checkbounds::Bool, sparse::Bool, simplify::Bool, linenumbers::Bool, parallel::Symbolics.SerialForm, eval_expression::Bool, use_union::Bool, tofloat::Bool, kwargs::@Kwargs{…})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/nonlinear/nonlinearsystem.jl:365
  [6] process_NonlinearProblem
    @ ~/.julia/dev/ModelingToolkit/src/systems/nonlinear/nonlinearsystem.jl:350 [inlined]
  [7] (NonlinearProblem{…})(sys::NonlinearSystem, u0map::Vector{…}, parammap::Vector{…}; check_length::Bool, kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/nonlinear/nonlinearsystem.jl:401
  [8] (NonlinearProblem{true})(sys::NonlinearSystem, u0map::Vector{Pair{Num, Float64}}, parammap::Vector{Any})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/nonlinear/nonlinearsystem.jl:395
  [9] NonlinearProblem(::NonlinearSystem, ::Vector{Pair{Num, Float64}}, ::Vararg{Any}; kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/nonlinear/nonlinearsystem.jl:392
 [10] NonlinearProblem(::NonlinearSystem, ::Vector{Pair{Num, Float64}}, ::Vararg{Any})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/nonlinear/nonlinearsystem.jl:391
 [11] ModelingToolkit.InitializationProblem{…}(sys::ODESystem, t::Int64, u0map::Vector{…}, parammap::Vector{…}; guesses::Vector{…}, check_length::Bool, warn_initialize_determined::Bool, kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1601
 [12] kwcall(::NamedTuple, ::Type{…}, sys::ModelingToolkit.AbstractODESystem, args::Vararg{…})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1558 [inlined]
 [13] #_#762
    @ ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1536 [inlined]
 [14] kwcall(::NamedTuple, ::Type{…}, sys::ModelingToolkit.AbstractODESystem, args::Vararg{…})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1535 [inlined]
 [15] #InitializationProblem#760
    @ ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1524 [inlined]
 [16] kwcall(::NamedTuple, ::Type{…}, sys::ModelingToolkit.AbstractODESystem, args::Vararg{…})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1523 [inlined]
 [17] process_DEProblem(constructor::Type, sys::ODESystem, u0map::Vector{…}, parammap::Vector{…}; implicit_dae::Bool, du0map::Nothing, version::Nothing, tgrad::Bool, jac::Bool, checkbounds::Bool, sparse::Bool, simplify::Bool, linenumbers::Bool, parallel::Symbolics.SerialForm, eval_expression::Bool, use_union::Bool, tofloat::Bool, symbolic_u0::Bool, u0_constructor::typeof(identity), guesses::Vector{…}, t::Int64, warn_initialize_determined::Bool, kwargs::@Kwargs{…})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:886
 [18] process_DEProblem
    @ ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:844 [inlined]
 [19] (ODEProblem{…})(sys::ODESystem, u0map::Vector{…}, tspan::Tuple{…}, parammap::Vector{…}; callback::Nothing, check_length::Bool, warn_initialize_determined::Bool, kwargs::@Kwargs{…})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1046
 [20] (ODEProblem{true})(::ODESystem, ::Vector{Any}, ::Vararg{Any}; kwargs::@Kwargs{guesses::Vector{Pair{Num, Float64}}})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1023
 [21] ODEProblem(::ODESystem, ::Vector{Any}, ::Vararg{Any}; kwargs::@Kwargs{guesses::Vector{Pair{Num, Float64}}})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1012
 [22] top-level scope
    @ REPL[2]:1
Some type information was truncated. Use `show(err)` to see complete types.
1 Like

The first one works because it analytically solves the equation, while the second one hits the nonlinear solver.