Skip to content

Commit 1482786

Browse files
Merge pull request #2820 from AayushSabharwal/as/defaults-indepvar
fix: fix initialization with defaults dependent on indepvar
2 parents ba343fa + 64d2063 commit 1482786

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,13 +675,17 @@ Take dictionaries with initial conditions and parameters and convert them to num
675675
function get_u0_p(sys,
676676
u0map,
677677
parammap = nothing;
678+
t0 = nothing,
678679
use_union = true,
679680
tofloat = true,
680681
symbolic_u0 = false)
681682
dvs = unknowns(sys)
682683
ps = parameters(sys)
683684

684685
defs = defaults(sys)
686+
if t0 !== nothing
687+
defs[get_iv(sys)] = t0
688+
end
685689
if parammap !== nothing
686690
defs = mergedefaults(defs, parammap, ps)
687691
end
@@ -717,14 +721,19 @@ function get_u0_p(sys,
717721
end
718722
p = varmap_to_vars(parammap, ps; defaults = defs, tofloat, use_union)
719723
p = p === nothing ? SciMLBase.NullParameters() : p
724+
t0 !== nothing && delete!(defs, get_iv(sys))
720725
u0, p, defs
721726
end
722727

723728
function get_u0(
724-
sys, u0map, parammap = nothing; symbolic_u0 = false, toterm = default_toterm)
729+
sys, u0map, parammap = nothing; symbolic_u0 = false,
730+
toterm = default_toterm, t0 = nothing)
725731
dvs = unknowns(sys)
726732
ps = parameters(sys)
727733
defs = defaults(sys)
734+
if t0 !== nothing
735+
defs[get_iv(sys)] = t0
736+
end
728737
if parammap !== nothing
729738
defs = mergedefaults(defs, parammap, ps)
730739
end
@@ -745,6 +754,7 @@ function get_u0(
745754
else
746755
u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = true, toterm)
747756
end
757+
t0 !== nothing && delete!(defs, get_iv(sys))
748758
return u0, defs
749759
end
750760

@@ -819,20 +829,22 @@ function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap;
819829
end
820830

821831
if has_index_cache(sys) && get_index_cache(sys) !== nothing
822-
u0, defs = get_u0(sys, trueinit, parammap; symbolic_u0)
832+
u0, defs = get_u0(sys, trueinit, parammap; symbolic_u0,
833+
t0 = constructor <: Union{DDEFunction, SDDEFunction} ? nothing : t)
823834
check_eqs_u0(eqs, dvs, u0; kwargs...)
824835
p = if parammap === nothing ||
825836
parammap == SciMLBase.NullParameters() && isempty(defs)
826837
nothing
827838
else
828-
MTKParameters(sys, parammap, trueinit; eval_expression, eval_module)
839+
MTKParameters(sys, parammap, trueinit; t0 = t, eval_expression, eval_module)
829840
end
830841
else
831842
u0, p, defs = get_u0_p(sys,
832843
trueinit,
833844
parammap;
834845
tofloat,
835846
use_union,
847+
t0 = constructor <: Union{DDEFunction, SDDEFunction} ? nothing : t,
836848
symbolic_u0)
837849
p, split_idxs = split_parameters_by_type(p)
838850
if p isa Tuple

src/systems/parameter_buffer.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ end
1515

1616
function MTKParameters(
1717
sys::AbstractSystem, p, u0 = Dict(); tofloat = false, use_union = false,
18-
eval_expression = false, eval_module = @__MODULE__)
19-
ic::IndexCache = if has_index_cache(sys) && get_index_cache(sys) !== nothing
18+
t0 = nothing, eval_expression = false, eval_module = @__MODULE__)
19+
ic = if has_index_cache(sys) && get_index_cache(sys) !== nothing
2020
get_index_cache(sys)
2121
else
2222
error("Cannot create MTKParameters if system does not have index_cache")
@@ -43,6 +43,9 @@ function MTKParameters(
4343
defs = merge(defs, u0)
4444
defs = merge(Dict(eq.lhs => eq.rhs for eq in observed(sys)), defs)
4545
bigdefs = merge(defs, p)
46+
if t0 !== nothing
47+
bigdefs[get_iv(sys)] = t0
48+
end
4649
p = Dict()
4750
missing_params = Set()
4851
pdeps = has_parameter_dependencies(sys) ? parameter_dependencies(sys) : nothing

test/initial_values.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,12 @@ prob = ODEProblem(sys, [], (0.0, 1.0), [A1 => 0.3])
108108
@test isempty(ModelingToolkit.defaults(sys))
109109
end
110110
end
111+
112+
# Using indepvar in initialization
113+
# Issue#2799
114+
@variables x(t)
115+
@parameters p
116+
@mtkbuild sys = ODESystem([D(x) ~ p], t; defaults = [x => t, p => 2t])
117+
prob = ODEProblem(structural_simplify(sys), [], (1.0, 2.0), [])
118+
@test prob[x] == 1.0
119+
@test prob.ps[p] == 2.0

0 commit comments

Comments
 (0)