Skip to content

Commit

Permalink
test: update several tests to MTKv9, fix minor bug
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Feb 29, 2024
1 parent 579b5ba commit 4f68c98
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/scimlfunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2293,7 +2293,7 @@ function ODEFunction{iip, specialize}(f;

_f = prepare_function(f)

sys = something(sys, SymbolCache(syms, paramsyms, indepsym))
sys = sys_or_symbolcache(sys, syms, paramsyms, indepsym)

@assert typeof(initializeprob) <:
Union{Nothing, NonlinearProblem, NonlinearLeastSquaresProblem}
Expand Down
1 change: 1 addition & 0 deletions test/downstream/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
OptimizationMOI = "fd9f6733-72f4-499f-8506-86b2bdd0dea1"
OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
Expand Down
16 changes: 8 additions & 8 deletions test/downstream/ensemble_multi_prob.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
using ModelingToolkit, OrdinaryDiffEq, Test
using ModelingToolkit: t_nounits as t, D_nounits as D

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

@named sys1 = ODESystem([D(x) ~ x,
D(y) ~ -y])
D(y) ~ -y], t)
@named sys2 = ODESystem([D(x) ~ 2x,
D(y) ~ -2y])
D(y) ~ -2y], t)
@named sys3 = ODESystem([D(x) ~ 3x,
D(y) ~ -3y])
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))
prob3 = ODEProblem(sys3, [3.0, 3.0], (0.0, 1.0))
prob1 = ODEProblem(complete(sys1), [1.0, 1.0], (0.0, 1.0))
prob2 = ODEProblem(complete(sys2), [2.0, 2.0], (0.0, 1.0))
prob3 = ODEProblem(complete(sys3), [3.0, 3.0], (0.0, 1.0))

# test that when passing a vector of problems, trajectories and the prob_func are chosen appropriately
ensemble_prob = EnsembleProblem([prob1, prob2, prob3])
Expand Down
9 changes: 3 additions & 6 deletions test/downstream/integrator_indexing.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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]
Expand Down Expand Up @@ -119,9 +119,8 @@ 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,
Expand Down Expand Up @@ -327,10 +326,8 @@ 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...;])
Expand Down
8 changes: 3 additions & 5 deletions test/downstream/problem_interface.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using ModelingToolkit, OrdinaryDiffEq, Test
using SymbolicIndexingInterface
using ModelingToolkit: t_nounits as t, D_nounits as D

@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)
@named sys = ODESystem(eqs, t)

sys = structural_simplify(sys)

Expand Down Expand Up @@ -181,10 +181,8 @@ 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...;])
Expand Down
4 changes: 2 additions & 2 deletions test/downstream/remake_autodiff.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using OrdinaryDiffEq, ModelingToolkit, Zygote, SciMLSensitivity
using ModelingToolkit: t_nounits as t, D_nounits as D

@variables t x(t) o(t)
D = Differential(t)
@variables x(t) o(t)
function lotka_volterra(; name = name)
unknowns = @variables x(t)=1.0 y(t)=1.0 o(t)
params = @parameters p1=1.5 p2=1.0 p3=3.0 p4=1.0
Expand Down
17 changes: 7 additions & 10 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,6 +29,7 @@ sol = solve(oprob, Rodas4())
noiseeqs = [0.1 * s1,
0.1 * s2]
@named noisy_population_model = SDESystem(population_model, noiseeqs)
noisy_population_model = complete(noisy_population_model)
sprob = SDEProblem(noisy_population_model, u0, (0.0, 100.0), p)
sol = solve(sprob, ImplicitEM())

Expand All @@ -42,16 +40,15 @@ 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)
Expand Down
23 changes: 10 additions & 13 deletions test/downstream/symbol_indexing.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
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)

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)
Expand Down Expand Up @@ -106,7 +103,7 @@ eqs = [D(q[1]) ~ 2q[1]
D(q[2]) ~ 2.0]
@named sys2 = ODESystem(eqs, t, [q...], [])
sys2_simplified = structural_simplify(sys2)
prob2 = ODEProblem(sys2, [], (0.0, 5.0))
prob2 = ODEProblem(sys2_simplified, [], (0.0, 5.0))
sol2 = solve(prob2, Tsit5())

@test sol2[q] isa Vector{Vector{Float64}}
Expand Down Expand Up @@ -218,13 +215,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...;])
sys = complete(sys)
prob = ODEProblem(sys, [], (0, 1.0))
sol = solve(prob, Tsit5())
@test sol[x] isa Vector{<:Vector}
Expand Down Expand Up @@ -342,11 +338,11 @@ for (sym, oldval, newval, check_inference) in [
end

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

@named fol = ODESystem([D(x) ~ (1 - x) / tau])
@named fol = ODESystem([D(x) ~ (1 - x) / tau], t)
fol = complete(fol)
prob = ODEProblem(fol, [x => 0.0], (0.0, 10.0), [tau => 3.0])
sol = solve(prob, Tsit5())
@test getp(fol, tau)(sol) == 3
Expand All @@ -359,6 +355,7 @@ sol = solve(prob, Tsit5())
@parameters a=1 b=1
loss = (a - x)^2 + b * (y - x^2)^2
@named sys = OptimizationSystem(loss, [x, y], [a, b])
sys = complete(sys)
u0 = [x => 1.0
y => 2.0]
p = [a => 1.0
Expand Down
4 changes: 2 additions & 2 deletions test/traits.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using SciMLBase, Test
using ModelingToolkit, OrdinaryDiffEq, DataFrames
using ModelingToolkit: t_nounits as t, D_nounits as D

@test SciMLBase.Tables.isrowtable(ODESolution)
@test SciMLBase.Tables.isrowtable(RODESolution)
Expand All @@ -9,8 +10,7 @@ using ModelingToolkit, OrdinaryDiffEq, DataFrames
@test !SciMLBase.Tables.isrowtable(SciMLBase.QuadratureSolution)
@test !SciMLBase.Tables.isrowtable(SciMLBase.OptimizationSolution)

@variables t x(t)=1
D = Differential(t)
@variables x(t)=1
eqs = [D(x) ~ -x]
@named sys = ODESystem(eqs, t)
sys = complete(sys)
Expand Down

0 comments on commit 4f68c98

Please sign in to comment.