diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 74eb4c2809..5907ab5c09 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -836,6 +836,16 @@ function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap; merge(parammap, clockedparammap) end end + if eltype(u0map) <: Number + u0map = unknowns(sys) .=> u0map + end + if isempty(u0map) + u0map = Dict() + end + u0map = todict(u0map) + if t !== nothing + u0map = merge(u0map, Dict(iv => t)) + end # TODO: make it work with clocks # ModelingToolkit.get_tearing_state(sys) !== nothing => Requires structural_simplify first if sys isa ODESystem && build_initializeprob && @@ -843,12 +853,6 @@ function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap; all(isequal(Continuous()), ci.var_domain) && ModelingToolkit.get_tearing_state(sys) !== nothing) || !isempty(initialization_equations(sys))) && t !== nothing - if eltype(u0map) <: Number - u0map = unknowns(sys) .=> u0map - end - if isempty(u0map) - u0map = Dict() - end initializeprob = ModelingToolkit.InitializationProblem( sys, t, u0map, parammap; guesses, warn_initialize_determined, initialization_eqs, eval_expression, eval_module) @@ -871,7 +875,7 @@ function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap; parammap == SciMLBase.NullParameters() && isempty(defs) nothing else - MTKParameters(sys, parammap, trueinit; eval_expression, eval_module) + MTKParameters(sys, parammap, trueinit; t0 = t, eval_expression, eval_module) end else u0, p, defs = get_u0_p(sys, diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index e491344fcd..505caba32d 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -15,7 +15,7 @@ end function MTKParameters( sys::AbstractSystem, p, u0 = Dict(); tofloat = false, use_union = false, - eval_expression = false, eval_module = @__MODULE__) + t0 = nothing, eval_expression = false, eval_module = @__MODULE__) ic = if has_index_cache(sys) && get_index_cache(sys) !== nothing get_index_cache(sys) else @@ -43,6 +43,9 @@ function MTKParameters( defs = merge(defs, u0) defs = merge(Dict(eq.lhs => eq.rhs for eq in observed(sys)), defs) bigdefs = merge(defs, p) + if t0 !== nothing + bigdefs[get_iv(sys)] = t0 + end p = Dict() missing_params = Set() pdeps = has_parameter_dependencies(sys) ? parameter_dependencies(sys) : nothing diff --git a/test/initial_values.jl b/test/initial_values.jl index 3a7b48901d..0b72bb51fd 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -91,3 +91,12 @@ eqs = [D(D(z)) ~ ones(2, 2)] prob = ODEProblem(sys, [], (0.0, 1.0), [A1 => 0.3]) @test prob.ps[B1] == 0.3 @test prob.ps[B2] == 0.7 + +# Using indepvar in initialization +# Issue#2799 +@variables x(t) +@parameters p +@mtkbuild sys = ODESystem([D(x) ~ p], t; defaults = [x => t, p => 2t]) +prob = ODEProblem(structural_simplify(sys), [], (1.0, 2.0), []) +@test prob[x] == 1.0 +@test prob.ps[p] == 2.0