Skip to content

Commit

Permalink
Fix downstream tests for MTK v9
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisRackauckas committed Feb 27, 2024
1 parent 8a63727 commit 14b8690
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 34 deletions.
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
21 changes: 10 additions & 11 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 @@ -127,15 +126,15 @@ 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 +184,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 +197,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 @@ -333,7 +332,7 @@ 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
10 changes: 4 additions & 6 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 @@ -187,7 +185,7 @@ 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
13 changes: 5 additions & 8 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 @@ -58,7 +55,7 @@ eqs = [D(x) ~ σ * (y - x),
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

0 comments on commit 14b8690

Please sign in to comment.