Skip to content

test, docs: fix tests and docs for MTKv9 #634

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

Merged
merged 3 commits into from
Mar 7, 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ LinearAlgebra = "1.10"
Logging = "1.10"
Makie = "0.20"
Markdown = "1.10"
ModelingToolkit = "8.75"
ModelingToolkit = "8.75, 9"
PartialFunctions = "1.1"
PrecompileTools = "1.2"
Preferences = "1.3"
Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"

[compat]
Documenter = "1"
ModelingToolkit = "8.35"
ModelingToolkit = "8.35, 9"
SciMLBase = "1.74, 2"
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -3897,7 +3897,7 @@ end
function sys_or_symbolcache(sys, syms, paramsyms, indepsym = nothing)
if sys === nothing &&
(syms !== nothing || paramsyms !== nothing || indepsym !== nothing)
error("The use of keyword arguments `syms`, `paramsyms` and `indepsym` for `SciMLFunction`s is deprecated. Pass `sys = SymbolCache(syms, paramsyms, indepsym)` instead.")
Base.depwarn("The use of keyword arguments `syms`, `paramsyms` and `indepsym` for `SciMLFunction`s is deprecated. Pass `sys = SymbolCache(syms, paramsyms, indepsym)` instead.", :syms)
sys = SymbolCache(syms, paramsyms, indepsym)
end
return sys
Expand Down
3 changes: 3 additions & 0 deletions test/downstream/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ 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"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1"
SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226"
Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4"
SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
Expand All @@ -25,6 +27,7 @@ OrdinaryDiffEq = "6.33"
RecursiveArrayTools = "3"
SciMLBase = "2"
SciMLSensitivity = "7.11"
SciMLStructures = "1.1"
Sundials = "4.11"
SymbolicIndexingInterface = "0.3"
Unitful = "1.12"
Expand Down
24 changes: 12 additions & 12 deletions test/downstream/integrator_indexing.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ModelingToolkit, OrdinaryDiffEq, RecursiveArrayTools, StochasticDiffEq,
SymbolicIndexingInterface, Test
using ModelingToolkit: t_nounits as t, D_nounits as D
using SciMLStructures: canonicalize, Tunable
### Tests on non-layered model (everything should work). ###

@parameters a b c d
Expand All @@ -9,8 +10,8 @@ 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 pop_model = ODESystem(eqs, t)
population_model = complete(pop_model)
# Tests on ODEProblem.
u0 = [s1 => 2.0, s2 => 1.0]
p = [a => 2.0, b => 1.0, c => 1.0, d => 1.0]
Expand Down Expand Up @@ -81,7 +82,8 @@ integrator[:s1] = 1.0
# Tests on SDEProblem
noiseeqs = [0.1 * s1,
0.1 * s2]
@named noisy_population_model = SDESystem(population_model, noiseeqs)
@named noisy_population_model = SDESystem(pop_model, noiseeqs)
noisy_population_model = complete(noisy_population_model)
sprob = SDEProblem(noisy_population_model, u0, (0.0, 100.0), p)
integrator = init(sprob, ImplicitEM())

Expand Down Expand Up @@ -120,7 +122,6 @@ integrator[:s1] = 1.0

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

eqs = [D(x) ~ σ * (y - x),
D(y) ~ x * (ρ - z) - y,
Expand All @@ -133,9 +134,8 @@ eqs = [D(x) ~ σ * (y - x),
@variables a(t) α(t)
connections = [0 ~ lorenz1.x + lorenz2.y + a * γ,
α ~ 2lorenz1.x + a * γ]
@mtkbuild sys_simplified = ODESystem(
@mtkbuild sys = ODESystem(
connections, t, [a, α], [γ], systems = [lorenz1, lorenz2])
sys_simplified = complete(structural_simplify(sys))

u0 = [lorenz1.x => 1.0,
lorenz1.y => 0.0,
Expand All @@ -154,7 +154,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)
integrator = init(prob, Rodas4())
step!(integrator, 100.0, true)

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

@test integrator2[q] isa Vector{Float64}
Expand Down Expand Up @@ -215,8 +215,9 @@ end

# Tests various interface methods:
@test_throws Any getp(sys, σ)(integrator)
@test in(getp(sys, lorenz1.σ)(integrator), integrator.p)
@test in(getp(sys, lorenz2.σ)(integrator), integrator.p)
tunable, _, _ = canonicalize(Tunable(), integrator.p)
@test in(getp(sys, lorenz1.σ)(integrator), tunable)
@test in(getp(sys, lorenz2.σ)(integrator), tunable)
@test_throws Any getp(sol, :σ)(sol)

@test_throws Any integrator[x]
Expand Down Expand Up @@ -329,7 +330,6 @@ plot(sol,idxs=(t,α))
using LinearAlgebra
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]]
@mtkbuild sys = ODESystem(eqs, t, [sts...;], [ps...;])
Expand Down
2 changes: 1 addition & 1 deletion test/downstream/problem_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ noiseeqs = [0.1 * x,
0.1 * y,
0.1 * z]
@named noise_sys = SDESystem(sys, noiseeqs)
noise_sys = complete(noise_sys)
sprob = SDEProblem(noise_sys, u0, (0.0, 100.0), p)
u0

Expand Down Expand Up @@ -181,7 +182,6 @@ set_tuple!(sprob, [10.0, 10.0])
using LinearAlgebra
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]]
@mtkbuild sys = ODESystem(eqs, t, [sts...;], [ps...;])
Expand Down
1 change: 0 additions & 1 deletion test/downstream/remake_autodiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ using OrdinaryDiffEq, ModelingToolkit, Zygote, SciMLSensitivity
using ModelingToolkit: t_nounits as t, D_nounits as D

@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)
params = @parameters p1=1.5 p2=1.0 p3=3.0 p4=1.0
Expand Down
7 changes: 4 additions & 3 deletions test/downstream/solution_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ sol = solve(oprob, Rodas4())
noiseeqs = [0.1 * s1,
0.1 * s2]
@named noisy_population_model = SDESystem(population_model, noiseeqs)
sprob = SDEProblem(complete(noisy_population_model), u0, (0.0, 100.0), p)
noisy_population_model = complete(noisy_population_model)
sprob = SDEProblem(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 @@ -46,8 +47,8 @@ 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)
Expand Down
14 changes: 7 additions & 7 deletions test/downstream/symbol_indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ using ModelingToolkit: t_nounits as t, D_nounits as D

@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 @@ -103,7 +102,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 @@ -217,10 +216,10 @@ plot(sol,idxs=(t,α))
using LinearAlgebra
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 @@ -340,9 +339,9 @@ end
# accessing parameters
@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 @@ -355,6 +354,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
7 changes: 4 additions & 3 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,10 +10,10 @@ 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)
@named sys = ODESystem(eqs, t)
sys = complete(sys)
prob = ODEProblem(sys)
sol = solve(prob, Tsit5(), tspan = (0.0, 1.0))
df = DataFrame(sol)
Expand Down