Skip to content

Commit 741ade3

Browse files
Merge pull request #3028 from hersle/fix_ics_unknowns
Override defaults when setting initial conditions with mapped unknowns(sys)
2 parents 76bff8f + 937ef53 commit 741ade3

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/systems/nonlinear/initializesystem.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,24 @@ function generate_initializesystem(sys::ODESystem;
4444
y = get(schedule.dummy_sub, x[1], x[1])
4545
y = ModelingToolkit.fixpoint_sub(y, full_diffmap)
4646

47-
if y isa Symbolics.Arr
47+
if y set_full_states
48+
# defer initialization until defaults are merged below
49+
push!(filtered_u0, y => x[2])
50+
elseif y isa Symbolics.Arr
51+
# scalarize array # TODO: don't scalarize arrays
4852
_y = collect(y)
49-
50-
# TODO: Don't scalarize arrays
51-
for i in 1:length(_y)
53+
for i in eachindex(_y)
5254
push!(filtered_u0, _y[i] => x[2][i])
5355
end
54-
elseif y isa ModelingToolkit.BasicSymbolic
56+
elseif y isa Symbolics.BasicSymbolic
5557
# y is a derivative expression expanded
5658
# add to the initialization equations
5759
push!(eqs_ics, y ~ x[2])
58-
elseif y set_full_states
59-
push!(filtered_u0, y => x[2])
6060
else
6161
error("Initialization expression $y is currently not supported. If its a higher order derivative expression, then only the dummy derivative expressions are supported.")
6262
end
6363
end
64-
filtered_u0 = filtered_u0 isa Pair ? todict([filtered_u0]) : todict(filtered_u0)
64+
filtered_u0 = todict(filtered_u0)
6565
end
6666
else
6767
dd_guess = Dict()

test/initializationsystem.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,25 @@ sys = extend(sysx, sysy)
486486
ssys = structural_simplify(sys)
487487
@test_throws ArgumentError ODEProblem(ssys, [x => 3], (0, 1), []) # y should have a guess
488488
end
489+
490+
# https://github.com/SciML/ModelingToolkit.jl/issues/3025
491+
@testset "Override defaults when setting initial conditions with unknowns(sys) or similar" begin
492+
@variables x(t) y(t)
493+
494+
# system 1 should solve to x = 1
495+
ics1 = [x => 1]
496+
sys1 = ODESystem([D(x) ~ 0], t; defaults = ics1, name = :sys1) |> structural_simplify
497+
prob1 = ODEProblem(sys1, [], (0.0, 1.0), [])
498+
sol1 = solve(prob1, Tsit5())
499+
@test all(sol1[x] .== 1)
500+
501+
# system 2 should solve to x = y = 2
502+
sys2 = extend(
503+
sys1,
504+
ODESystem([D(y) ~ 0], t; initialization_eqs = [y ~ 2], name = :sys2)
505+
) |> structural_simplify
506+
ics2 = unknowns(sys1) .=> 2 # should be equivalent to "ics2 = [x => 2]"
507+
prob2 = ODEProblem(sys2, ics2, (0.0, 1.0), []; fully_determined = true)
508+
sol2 = solve(prob2, Tsit5())
509+
@test all(sol2[x] .== 2) && all(sol2[y] .== 2)
510+
end

0 commit comments

Comments
 (0)