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

Fix downstream tests for MTK v9 #640

Merged
merged 6 commits into from
Mar 1, 2024
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
17 changes: 8 additions & 9 deletions test/downstream/ensemble_multi_prob.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using ModelingToolkit, OrdinaryDiffEq, Test
using ModelingToolkit: t_nounits as t, D_nounits as D
@variables x(t), y(t)

@variables t, x(t), y(t)
D = Differential(t)

@named sys1 = ODESystem([D(x) ~ x,
D(y) ~ -y])
@named sys2 = ODESystem([D(x) ~ 2x,
D(y) ~ -2y])
@named sys3 = ODESystem([D(x) ~ 3x,
D(y) ~ -3y])
@mtkbuild sys1 = ODESystem([D(x) ~ x,
D(y) ~ -y], t)
@mtkbuild sys2 = ODESystem([D(x) ~ 2x,
D(y) ~ -2y], t)
@mtkbuild sys3 = ODESystem([D(x) ~ 3x,
D(y) ~ -3y], t)

prob1 = ODEProblem(sys1, [1.0, 1.0], (0.0, 1.0))
prob2 = ODEProblem(sys2, [2.0, 2.0], (0.0, 1.0))
Expand Down
25 changes: 12 additions & 13 deletions test/downstream/integrator_indexing.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using ModelingToolkit, OrdinaryDiffEq, RecursiveArrayTools, StochasticDiffEq,
SymbolicIndexingInterface, Test

using ModelingToolkit: t_nounits as t, D_nounits as D
### Tests on non-layered model (everything should work). ###

@parameters t a b c d
@parameters a b c d
@variables s1(t) s2(t)
D = Differential(t)

eqs = [D(s1) ~ a * s1 / (1 + s1 + s2) - b * s1,
D(s2) ~ +c * s2 / (1 + s1 + s2) - d * s2]

@named population_model = ODESystem(eqs)
@named population_model = ODESystem(eqs, t)

# Tests on ODEProblem.
u0 = [s1 => 2.0, s2 => 1.0]
Expand Down Expand Up @@ -119,23 +118,24 @@ integrator[noisy_population_model.s2] = 10.0
integrator[:s1] = 1.0
@test integrator[s1] == integrator[noisy_population_model.s1] == integrator[:s1] == 1.0

@parameters t σ ρ β
@parameters σ ρ β
@variables x(t) y(t) z(t)
D = Differential(t)

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

@named lorenz1 = ODESystem(eqs)
@named lorenz2 = ODESystem(eqs)
@named lorenz1 = ODESystem(eqs, t)
@named lorenz2 = ODESystem(eqs, t)

@parameters γ
@variables a(t) α(t)
connections = [0 ~ lorenz1.x + lorenz2.y + a * γ,
α ~ 2lorenz1.x + a * γ]
@named sys = ODESystem(connections, t, [a, α], [γ], systems = [lorenz1, lorenz2])
sys_simplified = structural_simplify(sys)
@mtkbuild sys_simplified = ODESystem(
connections, t, [a, α], [γ], systems = [lorenz1, lorenz2])
sys_simplified = complete(structural_simplify(sys))

u0 = [lorenz1.x => 1.0,
lorenz1.y => 0.0,
Expand Down Expand Up @@ -185,7 +185,7 @@ step!(integrator, 100.0, true)
eqs = [D(q[1]) ~ 2q[1]
D(q[2]) ~ 2.0]
@named sys2 = ODESystem(eqs, t, [q...], [])
sys2_simplified = structural_simplify(sys2)
sys2_simplified = complete(structural_simplify(sys2))
prob2 = ODEProblem(sys2, [], (0.0, 5.0))
integrator2 = init(prob2, Tsit5())

Expand All @@ -198,7 +198,7 @@ integrator2 = init(prob2, Tsit5())
@variables u(t)
eqs = [D(u) ~ u]

@named sys2 = ODESystem(eqs)
@mtkbuild sys2 = ODESystem(eqs, t)

tspan = (0.0, 5.0)

Expand Down Expand Up @@ -327,13 +327,12 @@ plot(sol,idxs=(t,α))
=#

using LinearAlgebra
@variables t
sts = @variables x(t)[1:3]=[1, 2, 3.0] y(t)=1.0
ps = @parameters p[1:3] = [1, 2, 3]
D = Differential(t)
eqs = [collect(D.(x) .~ x)
D(y) ~ norm(x) * y - x[1]]
@named sys = ODESystem(eqs, t, [sts...;], [ps...;])
@mtkbuild sys = ODESystem(eqs, t, [sts...;], [ps...;])
prob = ODEProblem(sys, [], (0, 1.0))
integrator = init(prob, Tsit5(), save_everystep = false)
@test integrator[x] isa Vector{Float64}
Expand Down
11 changes: 4 additions & 7 deletions test/downstream/problem_interface.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using ModelingToolkit, OrdinaryDiffEq, Test
using ModelingToolkit: t_nounits as t, D_nounits as D
using SymbolicIndexingInterface

@parameters σ ρ β
@variables t x(t) y(t) z(t)
D = Differential(t)
@variables x(t) y(t) z(t)

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

@named sys = ODESystem(eqs)

sys = structural_simplify(sys)
@mtkbuild sys = ODESystem(eqs, t)

u0 = [D(x) => 2.0,
x => 1.0,
Expand Down Expand Up @@ -181,13 +179,12 @@ set_tuple!(sprob, [10.0, 10.0])
@test get_tuple(sprob) == (10.0, 10.0)

using LinearAlgebra
@variables t
sts = @variables x(t)[1:3]=[1, 2, 3.0] y(t)=1.0
ps = @parameters p[1:3] = [1, 2, 3]
D = Differential(t)
eqs = [collect(D.(x) .~ x)
D(y) ~ norm(x) * y - x[1]]
@named sys = ODESystem(eqs, t, [sts...;], [ps...;])
@mtkbuild sys = ODESystem(eqs, t, [sts...;], [ps...;])
prob = ODEProblem(sys, [], (0, 1.0))
@test getp(sys, p)(prob) == prob.ps[p] == [1, 2, 3]
setp(sys, p)(prob, [4, 5, 6])
Expand Down
3 changes: 2 additions & 1 deletion test/downstream/remake_autodiff.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using OrdinaryDiffEq, ModelingToolkit, Zygote, SciMLSensitivity
using ModelingToolkit: t_nounits as t, D_nounits as D

@variables t x(t) o(t)
@variables x(t) o(t)
D = Differential(t)
function lotka_volterra(; name = name)
unknowns = @variables x(t)=1.0 y(t)=1.0 o(t)
Expand Down
20 changes: 8 additions & 12 deletions test/downstream/solution_interface.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
using ModelingToolkit, OrdinaryDiffEq, RecursiveArrayTools, StochasticDiffEq, Test
# compat for MTKv8 and v9
unknowns = isdefined(ModelingToolkit, :states) ? ModelingToolkit.states :
ModelingToolkit.unknowns
using ModelingToolkit: t_nounits as t, D_nounits as D

### Tests on non-layered model (everything should work). ###

@parameters t a b c d
@parameters a b c d
@variables s1(t) s2(t)
D = Differential(t)

eqs = [D(s1) ~ a * s1 / (1 + s1 + s2) - b * s1,
D(s2) ~ +c * s2 / (1 + s1 + s2) - d * s2]

@named population_model = ODESystem(eqs)
@mtkbuild population_model = ODESystem(eqs, t)

# Tests on ODEProblem.
u0 = [s1 => 2.0, s2 => 1.0]
Expand All @@ -32,7 +29,7 @@ sol = solve(oprob, Rodas4())
noiseeqs = [0.1 * s1,
0.1 * s2]
@named noisy_population_model = SDESystem(population_model, noiseeqs)
sprob = SDEProblem(noisy_population_model, u0, (0.0, 100.0), p)
sprob = SDEProblem(complete(noisy_population_model), u0, (0.0, 100.0), p)
sol = solve(sprob, ImplicitEM())

@test sol[s1] == sol[noisy_population_model.s1] == sol[:s1]
Expand All @@ -42,23 +39,22 @@ sol = solve(sprob, ImplicitEM())
@test_throws Exception sol[:a]
### Tests on layered model (some things should not work). ###

@parameters t σ ρ β
@parameters σ ρ β
@variables x(t) y(t) z(t)
D = Differential(t)

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

@named lorenz1 = ODESystem(eqs)
@named lorenz2 = ODESystem(eqs)
@named lorenz1 = ODESystem(eqs,t)
@named lorenz2 = ODESystem(eqs,t)

@parameters γ
@variables a(t) α(t)
connections = [0 ~ lorenz1.x + lorenz2.y + a * γ,
α ~ 2lorenz1.x + a * γ]
@named sys = ODESystem(connections, t, [a, α], [γ], systems = [lorenz1, lorenz2])
sys_simplified = structural_simplify(sys)
sys_simplified = complete(structural_simplify(sys))

u0 = [lorenz1.x => 1.0,
lorenz1.y => 0.0,
Expand Down
22 changes: 9 additions & 13 deletions test/downstream/symbol_indexing.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using ModelingToolkit, OrdinaryDiffEq, RecursiveArrayTools, SymbolicIndexingInterface, Test
using Optimization, OptimizationOptimJL
# compat for MTKv8 and v9
unknowns = isdefined(ModelingToolkit, :states) ? ModelingToolkit.states :
ModelingToolkit.unknowns
using ModelingToolkit: t_nounits as t, D_nounits as D

@parameters t σ ρ β
@parameters σ ρ β
@variables x(t) y(t) z(t)
D = Differential(t)

Expand All @@ -19,8 +17,7 @@ eqs = [D(x) ~ σ * (y - x),
@variables a(t) α(t)
connections = [0 ~ lorenz1.x + lorenz2.y + a * γ,
α ~ 2lorenz1.x + a * γ]
@named sys = ODESystem(connections, t, [a, α], [γ], systems = [lorenz1, lorenz2])
sys_simplified = structural_simplify(sys)
@mtkbuild sys = ODESystem(connections, t, [a, α], [γ], systems = [lorenz1, lorenz2])

u0 = [lorenz1.x => 1.0,
lorenz1.y => 0.0,
Expand All @@ -39,7 +36,7 @@ p = [lorenz1.σ => 10.0,
γ => 2.0]

tspan = (0.0, 100.0)
prob = ODEProblem(sys_simplified, u0, tspan, p)
prob = ODEProblem(sys, u0, tspan, p)
integ = init(prob, Rodas4())
sol = solve(prob, Rodas4())

Expand Down Expand Up @@ -135,7 +132,7 @@ sol1 = sol(0.0:1.0:10.0)

sol2 = sol(0.1)
@test sol2 isa Vector
@test length(sol2) == length(unknowns(sys_simplified))
@test length(sol2) == length(unknowns(sys))
@test first(sol2) isa Real

sol3 = sol(0.0:1.0:10.0, idxs = [lorenz1.x, lorenz2.x])
Expand Down Expand Up @@ -191,9 +188,9 @@ sol10 = sol(0.1, idxs = 2)
@test sol10 isa Real

@test is_timeseries(sol) == Timeseries()
getx = getu(sys_simplified, lorenz1.x)
get_arr = getu(sys_simplified, [lorenz1.x, lorenz2.x])
get_tuple = getu(sys_simplified, (lorenz1.x, lorenz2.x))
getx = getu(sys, lorenz1.x)
get_arr = getu(sys, [lorenz1.x, lorenz2.x])
get_tuple = getu(sys, (lorenz1.x, lorenz2.x))
get_obs = getu(sol, lorenz1.x + lorenz2.x) # can't use sys for observed
get_obs_arr = getu(sol, [lorenz1.x + lorenz2.x, lorenz1.y + lorenz2.y])
l1x_idx = variable_index(sol, lorenz1.x)
Expand All @@ -218,7 +215,6 @@ plot(sol,idxs=(t,α))
=#

using LinearAlgebra
@variables t
sts = @variables x(t)[1:3]=[1, 2, 3.0] y(t)=1.0
ps = @parameters p[1:3] = [1, 2, 3]
D = Differential(t)
Expand Down Expand Up @@ -342,7 +338,7 @@ for (sym, oldval, newval, check_inference) in [
end

# accessing parameters
@variables t x(t)
@variables x(t)
@parameters tau
D = Differential(t)

Expand Down
Loading