From 14b869088108572b62544d32d207701e4aa9a0e2 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 27 Feb 2024 01:47:17 -0600 Subject: [PATCH 1/6] Fix downstream tests for MTK v9 --- test/downstream/ensemble_multi_prob.jl | 17 ++++++++--------- test/downstream/integrator_indexing.jl | 21 ++++++++++----------- test/downstream/problem_interface.jl | 10 ++++------ test/downstream/solution_interface.jl | 13 +++++-------- 4 files changed, 27 insertions(+), 34 deletions(-) diff --git a/test/downstream/ensemble_multi_prob.jl b/test/downstream/ensemble_multi_prob.jl index 9f09a58b2..de27acd84 100644 --- a/test/downstream/ensemble_multi_prob.jl +++ b/test/downstream/ensemble_multi_prob.jl @@ -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)) diff --git a/test/downstream/integrator_indexing.jl b/test/downstream/integrator_indexing.jl index 7f8795495..448bd19a0 100644 --- a/test/downstream/integrator_indexing.jl +++ b/test/downstream/integrator_indexing.jl @@ -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] @@ -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, @@ -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()) @@ -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) @@ -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} diff --git a/test/downstream/problem_interface.jl b/test/downstream/problem_interface.jl index c0ddce89b..1f245b3c7 100644 --- a/test/downstream/problem_interface.jl +++ b/test/downstream/problem_interface.jl @@ -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, @@ -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]) diff --git a/test/downstream/solution_interface.jl b/test/downstream/solution_interface.jl index ce38d354d..ea4eff21d 100644 --- a/test/downstream/solution_interface.jl +++ b/test/downstream/solution_interface.jl @@ -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] @@ -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] @@ -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, From b31928a3ffd836ad0a9df47b72bc39946081ecd2 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 27 Feb 2024 01:47:39 -0600 Subject: [PATCH 2/6] format --- test/downstream/ensemble_multi_prob.jl | 6 +++--- test/downstream/integrator_indexing.jl | 11 ++++++----- test/downstream/problem_interface.jl | 2 +- test/downstream/solution_interface.jl | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/test/downstream/ensemble_multi_prob.jl b/test/downstream/ensemble_multi_prob.jl index de27acd84..0ad6d49c3 100644 --- a/test/downstream/ensemble_multi_prob.jl +++ b/test/downstream/ensemble_multi_prob.jl @@ -3,11 +3,11 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables x(t), y(t) @mtkbuild sys1 = ODESystem([D(x) ~ x, - D(y) ~ -y],t) + D(y) ~ -y], t) @mtkbuild sys2 = ODESystem([D(x) ~ 2x, - D(y) ~ -2y],t) + D(y) ~ -2y], t) @mtkbuild sys3 = ODESystem([D(x) ~ 3x, - D(y) ~ -3y],t) + 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)) diff --git a/test/downstream/integrator_indexing.jl b/test/downstream/integrator_indexing.jl index 448bd19a0..9dce8332e 100644 --- a/test/downstream/integrator_indexing.jl +++ b/test/downstream/integrator_indexing.jl @@ -9,7 +9,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D eqs = [D(s1) ~ a * s1 / (1 + s1 + s2) - b * s1, D(s2) ~ +c * s2 / (1 + s1 + s2) - d * s2] -@named population_model = ODESystem(eqs,t) +@named population_model = ODESystem(eqs, t) # Tests on ODEProblem. u0 = [s1 => 2.0, s2 => 1.0] @@ -126,14 +126,15 @@ eqs = [D(x) ~ σ * (y - x), D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] -@named lorenz1 = ODESystem(eqs,t) -@named lorenz2 = ODESystem(eqs,t) +@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 * γ] -@mtkbuild sys_simplified = ODESystem(connections, t, [a, α], [γ], systems = [lorenz1, lorenz2]) +@mtkbuild sys_simplified = ODESystem( + connections, t, [a, α], [γ], systems = [lorenz1, lorenz2]) sys_simplified = complete(structural_simplify(sys)) u0 = [lorenz1.x => 1.0, @@ -197,7 +198,7 @@ integrator2 = init(prob2, Tsit5()) @variables u(t) eqs = [D(u) ~ u] - @mtkbuild sys2 = ODESystem(eqs,t) + @mtkbuild sys2 = ODESystem(eqs, t) tspan = (0.0, 5.0) diff --git a/test/downstream/problem_interface.jl b/test/downstream/problem_interface.jl index 1f245b3c7..bdb43ad37 100644 --- a/test/downstream/problem_interface.jl +++ b/test/downstream/problem_interface.jl @@ -9,7 +9,7 @@ eqs = [D(D(x)) ~ σ * (y - x), D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] -@mtkbuild sys = ODESystem(eqs,t) +@mtkbuild sys = ODESystem(eqs, t) u0 = [D(x) => 2.0, x => 1.0, diff --git a/test/downstream/solution_interface.jl b/test/downstream/solution_interface.jl index ea4eff21d..8e335a139 100644 --- a/test/downstream/solution_interface.jl +++ b/test/downstream/solution_interface.jl @@ -9,7 +9,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D eqs = [D(s1) ~ a * s1 / (1 + s1 + s2) - b * s1, D(s2) ~ +c * s2 / (1 + s1 + s2) - d * s2] -@mtkbuild population_model = ODESystem(eqs,t) +@mtkbuild population_model = ODESystem(eqs, t) # Tests on ODEProblem. u0 = [s1 => 2.0, s2 => 1.0] From 88aab2c31a7755a4d44b6bab6cdd4363190f56c6 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 27 Feb 2024 02:36:34 -0600 Subject: [PATCH 3/6] remove extra t D --- test/downstream/solution_interface.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/downstream/solution_interface.jl b/test/downstream/solution_interface.jl index 8e335a139..b7dd519b4 100644 --- a/test/downstream/solution_interface.jl +++ b/test/downstream/solution_interface.jl @@ -39,9 +39,8 @@ 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, From 0501bf5683b64cb337047c56875c74299eeb026d Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 27 Feb 2024 03:16:13 -0600 Subject: [PATCH 4/6] fix missing t --- test/downstream/solution_interface.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/downstream/solution_interface.jl b/test/downstream/solution_interface.jl index b7dd519b4..b74c7da89 100644 --- a/test/downstream/solution_interface.jl +++ b/test/downstream/solution_interface.jl @@ -46,8 +46,8 @@ 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) From 60cdf227e20d5486ed097c97b14bb2734a28f6bd Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 27 Feb 2024 07:35:44 -0600 Subject: [PATCH 5/6] forgot a file --- test/downstream/symbol_indexing.jl | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/test/downstream/symbol_indexing.jl b/test/downstream/symbol_indexing.jl index 17a24ee28..da18aa454 100644 --- a/test/downstream/symbol_indexing.jl +++ b/test/downstream/symbol_indexing.jl @@ -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) @@ -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, @@ -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()) @@ -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]) @@ -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) From 22fe5219c5c0bead6a30c6bc7db4cc558f8f2be1 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Fri, 1 Mar 2024 04:56:22 -0600 Subject: [PATCH 6/6] fix t imports --- test/downstream/integrator_indexing.jl | 3 +-- test/downstream/problem_interface.jl | 1 - test/downstream/remake_autodiff.jl | 3 ++- test/downstream/symbol_indexing.jl | 3 +-- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/test/downstream/integrator_indexing.jl b/test/downstream/integrator_indexing.jl index 9dce8332e..7a10954c0 100644 --- a/test/downstream/integrator_indexing.jl +++ b/test/downstream/integrator_indexing.jl @@ -118,7 +118,7 @@ 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) @@ -327,7 +327,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) diff --git a/test/downstream/problem_interface.jl b/test/downstream/problem_interface.jl index bdb43ad37..5c8c3ad6f 100644 --- a/test/downstream/problem_interface.jl +++ b/test/downstream/problem_interface.jl @@ -179,7 +179,6 @@ 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) diff --git a/test/downstream/remake_autodiff.jl b/test/downstream/remake_autodiff.jl index b012a4d15..98534a878 100644 --- a/test/downstream/remake_autodiff.jl +++ b/test/downstream/remake_autodiff.jl @@ -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) diff --git a/test/downstream/symbol_indexing.jl b/test/downstream/symbol_indexing.jl index da18aa454..766b0faf0 100644 --- a/test/downstream/symbol_indexing.jl +++ b/test/downstream/symbol_indexing.jl @@ -215,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) @@ -339,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)