Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: format #3361

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/systems/diffeqs/odesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ function Base.:(==)(sys1::ODESystem, sys2::ODESystem)
_eq_unordered(get_unknowns(sys1), get_unknowns(sys2)) &&
_eq_unordered(get_ps(sys1), get_ps(sys2)) &&
_eq_unordered(continuous_events(sys1), continuous_events(sys2)) &&
_eq_unordered(discrete_events(sys1), discrete_events(sys2)) &&
_eq_unordered(discrete_events(sys1), discrete_events(sys2)) &&
all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2)))
end

Expand Down Expand Up @@ -631,7 +631,8 @@ function build_explicit_observed_function(sys, ts;
oop_fn = Func(args, [],
pre(Let(obsexprs,
return_value,
false)), [Expr(:meta, :propagate_inbounds)]) |> array_wrapper[1] |> oop_mtkp_wrapper |> toexpr
false)), [Expr(:meta, :propagate_inbounds)]) |> array_wrapper[1] |>
oop_mtkp_wrapper |> toexpr

if !checkbounds
oop_fn.args[end] = quote
Expand Down
3 changes: 1 addition & 2 deletions src/systems/diffeqs/sdesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ function Base.:(==)(sys1::SDESystem, sys2::SDESystem)
_eq_unordered(get_unknowns(sys1), get_unknowns(sys2)) &&
_eq_unordered(get_ps(sys1), get_ps(sys2)) &&
_eq_unordered(continuous_events(sys1), continuous_events(sys2)) &&
_eq_unordered(discrete_events(sys1), discrete_events(sys2)) &&
_eq_unordered(discrete_events(sys1), discrete_events(sys2)) &&
all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2)))
end

Expand Down Expand Up @@ -771,7 +771,6 @@ function DiffEqBase.SDEProblem{iip, specialize}(
end

function DiffEqBase.SDEProblem(sys::ODESystem, args...; kwargs...)

if any(ModelingToolkit.isbrownian, unknowns(sys))
error("SDESystem constructed by defining Brownian variables with @brownian must be simplified by calling `structural_simplify` before a SDEProblem can be constructed.")
else
Expand Down
6 changes: 4 additions & 2 deletions src/systems/nonlinear/nonlinearsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,8 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map,
# precomputed subexpressions should not contain `banned_vars`
banned_vars = Set{Any}(vcat(_dvs, getproperty.(_obs, (:lhs,))))
filter!(banned_vars) do var
symbolic_type(var) != ArraySymbolic() || all(j -> var[j] in banned_vars, eachindex(var))
symbolic_type(var) != ArraySymbolic() ||
all(j -> var[j] in banned_vars, eachindex(var))
end
state = Dict()
for i in eachindex(_obs)
Expand Down Expand Up @@ -772,7 +773,8 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map,
_obs = scc_obs[i]
cachevars = scc_cachevars[i]
cacheexprs = scc_cacheexprs[i]
_prevobsidxs = vcat(_prevobsidxs, observed_equations_used_by(sys, reduce(vcat, values(cacheexprs); init = [])))
_prevobsidxs = vcat(_prevobsidxs,
observed_equations_used_by(sys, reduce(vcat, values(cacheexprs); init = [])))

if isempty(cachevars)
push!(explicitfuns, Returns(nothing))
Expand Down
5 changes: 4 additions & 1 deletion test/fmi/fmi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,10 @@ end
@named adder2 = MTK.FMIComponent(
Val(2); fmu, type = :CS, communication_step_size = 1e-3)
sys, prob = build_looped_adders(adder1, adder2)
sol = solve(prob, Tsit5(); reltol = 1e-8, initializealg = SciMLBase.OverrideInit(nlsolve = FastShortcutNLLSPolyalg(autodiff = AutoFiniteDiff())))
sol = solve(prob,
Tsit5();
reltol = 1e-8,
initializealg = SciMLBase.OverrideInit(nlsolve = FastShortcutNLLSPolyalg(autodiff = AutoFiniteDiff())))
@test truesol(sol.t;
idxs = [truesys.adder1.c, truesys.adder2.c]).u≈sol(
sol.t; idxs = [sys.adder1.c, sys.adder2.c]).u rtol=1e-3
Expand Down
3 changes: 2 additions & 1 deletion test/initial_values.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,6 @@ end
@parameters p[1:2, 1:2]
@mtkbuild sys = ODESystem(D(x) ~ p * x, t)
# used to throw a `MethodError` complaining about `getindex(::Nothing, ::CartesianIndex{2})`
@test_throws ModelingToolkit.MissingParametersError ODEProblem(sys, [x => ones(2)], (0.0, 1.0))
@test_throws ModelingToolkit.MissingParametersError ODEProblem(
sys, [x => ones(2)], (0.0, 1.0))
end
20 changes: 10 additions & 10 deletions test/odesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ end
@testset "`isequal`" begin
@variables X(t)
@parameters p d
eq = D(X) ~ p - d*X
eq = D(X) ~ p - d * X

osys1 = complete(ODESystem([eq], t; name = :osys))
osys2 = complete(ODESystem([eq], t; name = :osys))
Expand All @@ -1559,36 +1559,36 @@ end

osys1 = complete(ODESystem([eq], t; name = :osys, continuous_events))
osys2 = complete(ODESystem([eq], t; name = :osys))
@test osys1 !== osys2
@test osys1 !== osys2

osys1 = complete(ODESystem([eq], t; name = :osys, discrete_events))
osys2 = complete(ODESystem([eq], t; name = :osys))
@test osys1 !== osys2

osys1 = complete(ODESystem([eq], t; name = :osys, continuous_events))
osys2 = complete(ODESystem([eq], t; name = :osys, discrete_events))
@test osys1 !== osys2
@test osys1 !== osys2
end

@testset "dae_order_lowering basic test" begin
@parameters a
@variables x(t) y(t) z(t)
@named dae_sys = ODESystem([
D(x) ~ y,
0 ~ x + z,
0 ~ x - y + z
], t, [z, y, x], [])
D(x) ~ y,
0 ~ x + z,
0 ~ x - y + z
], t, [z, y, x], [])

lowered_dae_sys = dae_order_lowering(dae_sys)
@variables x1(t) y1(t) z1(t)
@variables x1(t) y1(t) z1(t)
expected_eqs = [
0 ~ x + z,
0 ~ x - y + z,
Differential(t)(x) ~ y
]
lowered_eqs = equations(lowered_dae_sys)
sorted_lowered_eqs = sort(lowered_eqs, by=string)
sorted_expected_eqs = sort(expected_eqs, by=string)
sorted_lowered_eqs = sort(lowered_eqs, by = string)
sorted_expected_eqs = sort(expected_eqs, by = string)
@test sorted_lowered_eqs == sorted_expected_eqs

expected_vars = Set([z, y, x])
Expand Down
15 changes: 8 additions & 7 deletions test/sdesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -882,10 +882,10 @@ end
0.1 * z]

@named sys = ODESystem(eqs, tt, [x, y, z], [σ, ρ, β])

@named de = SDESystem(eqs, noiseeqs, tt, [x, y, z], [σ, ρ, β], tspan = (0.0, 10.0))
de = complete(de)

f = SDEFunctionExpr(de)
@test f isa Expr

Expand Down Expand Up @@ -915,7 +915,7 @@ end
@variables X(t)
@parameters p d
@brownian a
seq = D(X) ~ p - d*X + a
seq = D(X) ~ p - d * X + a
@mtkbuild ssys1 = System([seq], t; name = :ssys)
@mtkbuild ssys2 = System([seq], t; name = :ssys)
@test ssys1 == ssys2 # true
Expand All @@ -925,15 +925,15 @@ end

@mtkbuild ssys1 = System([seq], t; name = :ssys, continuous_events)
@mtkbuild ssys2 = System([seq], t; name = :ssys)
@test ssys1 !== ssys2
@test ssys1 !== ssys2

@mtkbuild ssys1 = System([seq], t; name = :ssys, discrete_events)
@mtkbuild ssys2 = System([seq], t; name = :ssys)
@test ssys1 !== ssys2

@mtkbuild ssys1 = System([seq], t; name = :ssys, continuous_events)
@mtkbuild ssys2 = System([seq], t; name = :ssys, discrete_events)
@test ssys1 !== ssys2
@test ssys1 !== ssys2
end

@testset "Error when constructing SDESystem without `structural_simplify`" begin
Expand All @@ -950,7 +950,8 @@ end
u0map = [x => 1.0, y => 0.0, z => 0.0]
parammap = [σ => 10.0, β => 26.0, ρ => 2.33]

@test_throws ErrorException("SDESystem constructed by defining Brownian variables with @brownian must be simplified by calling `structural_simplify` before a SDEProblem can be constructed.") SDEProblem(de, u0map, (0.0, 100.0), parammap)
@test_throws ErrorException("SDESystem constructed by defining Brownian variables with @brownian must be simplified by calling `structural_simplify` before a SDEProblem can be constructed.") SDEProblem(
de, u0map, (0.0, 100.0), parammap)
de = structural_simplify(de)
@test SDEProblem(de, u0map, (0.0, 100.0), parammap) isa SDEProblem
end
end
Loading