Skip to content
10 changes: 9 additions & 1 deletion src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,12 @@ function add_initialization_parameters(sys::AbstractSystem)
push!(all_initialvars, x)
end
end

# add derivatives of all variables for steady-state initial conditions
if is_time_dependent(sys) && !(sys isa AbstractDiscreteSystem)
D = Differential(get_iv(sys))
union!(all_initialvars, [D(v) for v in all_initialvars if iscall(v)])
end
for eq in parameter_dependencies(sys)
is_variable_floatingpoint(eq.lhs) || continue
push!(all_initialvars, eq.lhs)
Expand Down Expand Up @@ -1924,7 +1930,9 @@ function toexpr(sys::AbstractSystem)
end

eqs_name = push_eqs!(stmt, full_equations(sys), var2name)
defs_name = push_defaults!(stmt, defaults(sys), var2name)
filtered_defs = filter(
kvp -> !(iscall(kvp[1]) && operation(kvp[1]) isa Initial), defaults(sys))
defs_name = push_defaults!(stmt, filtered_defs, var2name)
obs_name = push_eqs!(stmt, obs, var2name)

if sys isa ODESystem
Expand Down
4 changes: 2 additions & 2 deletions src/systems/problem_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -623,12 +623,12 @@ function build_operating_point!(sys::AbstractSystem,
end

for k in keys(u0map)
v = fixpoint_sub(u0map[k], neithermap)
v = fixpoint_sub(u0map[k], neithermap; operator = Symbolics.Operator)
isequal(k, v) && continue
u0map[k] = v
end
for k in keys(pmap)
v = fixpoint_sub(pmap[k], neithermap)
v = fixpoint_sub(pmap[k], neithermap; operator = Symbolics.Operator)
isequal(k, v) && continue
pmap[k] = v
end
Expand Down
16 changes: 16 additions & 0 deletions test/initial_values.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,19 @@ end
prob2 = remake(prob; p = p_new)
@test prob2.ps[interp] == spline2
end

@testset "Issue#3523: don't substitute inside initial in `build_operating_point!`" begin
@variables (X(t))[1:2]
@parameters p[1:2]
eqs = [
0 ~ p[1] - X[1],
0 ~ p[2] - X[2]
]
@named nlsys = NonlinearSystem(eqs)
nlsys = complete(nlsys)

# Creates the `NonlinearProblem`.
u0 = [X => [1.0, 2.0]]
ps = [p => [4.0, 5.0]]
@test_nowarn NonlinearProblem(nlsys, u0, ps)
end
41 changes: 23 additions & 18 deletions test/initializationsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -406,30 +406,35 @@ sol = solve(prob, Tsit5())
@test sol.u[1] == [1.0]

# Steady state initialization
@testset "Steady state initialization" begin
@parameters σ ρ β
@variables x(t) y(t) z(t)

@parameters σ ρ β
@variables x(t) y(t) z(t)
eqs = [D(D(x)) ~ σ * (y - x),
D(y) ~ x * (ρ - z) - y,
D(z) ~ x * y - β * z]

eqs = [D(D(x)) ~ σ * (y - x),
D(y) ~ x * (ρ - z) - y,
D(z) ~ x * y - β * z]
@named sys = ODESystem(eqs, t)
sys = structural_simplify(sys)

@named sys = ODESystem(eqs, t)
sys = structural_simplify(sys)
u0 = [D(x) => 2.0,
x => 1.0,
D(y) => 0.0,
z => 0.0]

u0 = [D(x) => 2.0,
x => 1.0,
D(y) => 0.0,
z => 0.0]
p = [σ => 28.0,
ρ => 10.0,
β => 8 / 3]

p = [σ => 28.0,
ρ => 10.0,
β => 8 / 3]
tspan = (0.0, 0.2)
prob_mtk = ODEProblem(sys, u0, tspan, p)
sol = solve(prob_mtk, Tsit5())
@test sol[x * (ρ - z) - y][1] == 0.0

tspan = (0.0, 0.2)
prob_mtk = ODEProblem(sys, u0, tspan, p)
sol = solve(prob_mtk, Tsit5())
@test sol[x * (ρ - z) - y][1] == 0.0
prob_mtk.ps[Initial(D(y))] = 1.0
sol = solve(prob_mtk, Tsit5())
@test sol[x * (ρ - z) - y][1] == 1.0
end

@variables x(t) y(t) z(t)
@parameters α=1.5 β=1.0 γ=3.0 δ=1.0
Expand Down
2 changes: 1 addition & 1 deletion test/modelingtoolkitize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ sys = modelingtoolkitize(prob)
p = [10.0, 28.0, 2.66]
sprob = SDEProblem(sdef!, sdeg!, u0, tspan, p)
sys = complete(modelingtoolkitize(sprob))
@test length(ModelingToolkit.defaults(sys)) == 2length(u0) + length(p)
@test length(ModelingToolkit.defaults(sys)) == 3length(u0) + length(p)
sprob2 = SDEProblem(sys, [], tspan)

truevals = similar(u0)
Expand Down
3 changes: 2 additions & 1 deletion test/symbolic_indexing_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ using SciMLStructures: Tunable
(odesys,), [x, y, t, ParameterIndex(Tunable(), 1), :x, :y]) ==
[nothing, nothing, nothing, ParameterIndex(Tunable(), 1), nothing, nothing]
@test isequal(
Set(parameter_symbols(odesys)), Set([a, b, Initial(x), Initial(y), Initial(xy)]))
Set(parameter_symbols(odesys)), Set([a, b, Initial(x), Initial(y), Initial(xy),
Initial(D(x)), Initial(D(y)), Initial(D(xy))]))
@test all(is_independent_variable.((odesys,), [t, :t]))
@test all(.!is_independent_variable.((odesys,), [x, y, a, :x, :y, :a]))
@test isequal(independent_variable_symbols(odesys), [t])
Expand Down
Loading