Skip to content

Commit 21d1ce7

Browse files
fix: construct initializeprob if initial value is symbolic
1 parent be393fd commit 21d1ce7

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/systems/problem_utils.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,14 @@ function process_SciMLProblem(
433433
solvablepars = [p
434434
for p in parameters(sys)
435435
if is_parameter_solvable(p, pmap, defs, guesses)]
436+
has_dependent_unknowns = any(unknowns(sys)) do sym
437+
val = get(op, sym, nothing)
438+
val === nothing && return false
439+
return symbolic_type(val) != NotSymbolic() || is_array_of_symbolics(val)
440+
end
436441
if build_initializeprob &&
437442
(((implicit_dae || has_observed_u0s || !isempty(missing_unknowns) ||
438-
!isempty(solvablepars)) &&
443+
!isempty(solvablepars) || has_dependent_unknowns) &&
439444
get_tearing_state(sys) !== nothing) ||
440445
!isempty(initialization_equations(sys))) && t !== nothing
441446
initializeprob = ModelingToolkit.InitializationProblem(

test/initializationsystem.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,3 +844,19 @@ end
844844
isys = ModelingToolkit.generate_initializesystem(sys)
845845
@test isequal(defaults(isys)[y], 2x + 1)
846846
end
847+
848+
@testset "Create initializeprob when unknown has dependent value" begin
849+
@variables x(t) y(t)
850+
@mtkbuild sys = ODESystem([D(x) ~ x, D(y) ~ t * y], t; defaults = [x => 2y])
851+
prob = ODEProblem(sys, [y => 1.0], (0.0, 1.0))
852+
@test prob.f.initializeprob !== nothing
853+
integ = init(prob)
854+
@test integ[x] 2.0
855+
856+
@variables x(t)[1:2] y(t)
857+
@mtkbuild sys = ODESystem([D(x) ~ x, D(y) ~ t], t; defaults = [x => [y, 3.0]])
858+
prob = ODEProblem(sys, [y => 1.0], (0.0, 1.0))
859+
@test prob.f.initializeprob !== nothing
860+
integ = init(prob)
861+
@test integ[x] [1.0, 3.0]
862+
end

0 commit comments

Comments
 (0)