Skip to content

Commit

Permalink
fix: fix initialization with defaults dependent on indepvar
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Jul 1, 2024
1 parent c421522 commit bebb572
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
18 changes: 11 additions & 7 deletions src/systems/diffeqs/abstractodesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -836,19 +836,23 @@ 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 &&
(((implicit_dae || !isempty(missingvars)) &&
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)
Expand All @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion src/systems/parameter_buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/initial_values.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bebb572

Please sign in to comment.