Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Jan 30, 2024
1 parent 197da7c commit 3256ae2
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 46 deletions.
2 changes: 1 addition & 1 deletion test/constants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ UMT = ModelingToolkit.UnitfulUnitCheck
D = Differential(t)
eqs = [D(x) ~ a]
@named sys = ODESystem(eqs)
prob = ODEProblem(sys, [0], [0.0, 1.0], [])
prob = ODEProblem(complete(sys), [0], [0.0, 1.0], [])
sol = solve(prob, Tsit5())

newsys = MT.eliminate_constants(sys)
Expand Down
2 changes: 1 addition & 1 deletion test/dae_jacobian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ du0 = [0.5, -2.0]
p = [p1 => 1.5,
p2 => 3.0]

prob = DAEProblem(sys, du0, u0, tspan, p, jac = true, sparse = true)
prob = DAEProblem(complete(sys), du0, u0, tspan, p, jac = true, sparse = true)
sol = solve(prob, IDA(linear_solver = :KLU))

@test maximum(sol - sol1) < 1e-12
3 changes: 3 additions & 0 deletions test/discretesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ end
u0 = [S => 990.0, I => 10.0, R => 0.0]
p ==> 0.05, c => 10.0, γ => 0.25, δt => 0.1, nsteps => 400]
tspan = (0.0, ModelingToolkit.value(substitute(nsteps, p))) # value function (from Symbolics) is used to convert a Num to Float64
sys = complete(sys)
prob_map = DiscreteProblem(sys, u0, tspan, p)
@test prob_map.f.sys === sys

Expand All @@ -70,6 +71,7 @@ eqs2 = [D(S) ~ S - infection2,
D(R) ~ R + recovery2]

@named sys = DiscreteSystem(eqs2; controls = [β, γ], tspan)
sys = complete(sys)
@test ModelingToolkit.defaults(sys) != Dict()

prob_map2 = DiscreteProblem(sys)
Expand Down Expand Up @@ -193,6 +195,7 @@ RHS2 = RHS
end

@named sys = DiscreteSystem(eqs, t, us, ps; defaults = defs, preface = preface)
sys = complete(sys)
prob = DiscreteProblem(sys, [], (0.0, 1.0))
sol = solve(prob, FunctionMap(); dt = dt)
@test c[1] + 1 == length(sol)
Expand Down
4 changes: 2 additions & 2 deletions test/extensions/bifurcationkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let
eqs = [0 ~ μ * x - x^3 + α * y,
0 ~ -y]
@named nsys = NonlinearSystem(eqs, [x, y], [μ, α])

nsys = complete(nsys)
# Creates BifurcationProblem
bif_par = μ
p_start ==> -1.0, α => 1.0]
Expand Down Expand Up @@ -62,7 +62,7 @@ let
eqs = [D(x) ~ -x + a * y + x^2 * y,
D(y) ~ b - a * y - x^2 * y]
@named sys = ODESystem(eqs)

sys = complete(sys)
# Creates BifurcationProblem
bprob = BifurcationProblem(sys,
[x => 1.5, y => 1.0],
Expand Down
10 changes: 5 additions & 5 deletions test/funcaffect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ affect1!(integ, u, p, ctx) = integ.u[u.u] += 10

@named sys = ODESystem(eqs, t, [u], [],
discrete_events = [[4.0] => (affect1!, [u], [], nothing)])
prob = ODEProblem(sys, [u => 10.0], (0, 10.0))
prob = ODEProblem(complete(sys), [u => 10.0], (0, 10.0))
sol = solve(prob, Tsit5())
i4 = findfirst(==(4.0), sol[:t])
@test sol.u[i4 + 1][1] > 10.0
Expand Down Expand Up @@ -62,7 +62,7 @@ end
ctx1 = [10.0]
@named sys = ODESystem(eqs, t, [u], [],
discrete_events = [[4.0, 8.0] => (affect2!, [u], [], ctx1)])
prob = ODEProblem(sys, [u => 10.0], (0, 10.0))
prob = ODEProblem(complete(sys), [u => 10.0], (0, 10.0))
sol = solve(prob, Tsit5())
i4 = findfirst(==(4.0), sol[:t])
@test sol.u[i4 + 1][1] > 10.0
Expand All @@ -79,7 +79,7 @@ end
@parameters a = 10.0
@named sys = ODESystem(eqs, t, [u], [a],
discrete_events = [[4.0, 8.0] => (affect3!, [u], [a], nothing)])
prob = ODEProblem(sys, [u => 10.0], (0, 10.0))
prob = ODEProblem(complete(sys), [u => 10.0], (0, 10.0))

sol = solve(prob, Tsit5())
i4 = findfirst(==(4.0), sol[:t])
Expand All @@ -97,7 +97,7 @@ end
discrete_events = [
[4.0, 8.0] => (affect3!, [u], [a => :b], nothing),
])
prob = ODEProblem(sys, [u => 10.0], (0, 10.0))
prob = ODEProblem(complete(sys), [u => 10.0], (0, 10.0))

sol = solve(prob, Tsit5())
i4 = findfirst(==(4.0), sol[:t])
Expand Down Expand Up @@ -225,7 +225,7 @@ balls = compose(balls, [ball1, ball2])
@test ModelingToolkit.has_discrete_events(balls)
@test length(ModelingToolkit.affects(ModelingToolkit.discrete_events(balls))) == 2

prob = ODEProblem(balls, [ball1.x => 10.0, ball1.v => 0, ball2.x => 10.0, ball2.v => 0],
prob = ODEProblem(complete(balls), [ball1.x => 10.0, ball1.v => 0, ball2.x => 10.0, ball2.v => 0],
(0, 3.0))
sol = solve(prob, Tsit5())

Expand Down
4 changes: 2 additions & 2 deletions test/jacobiansparsity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ end
u0 = init_brusselator_2d(xyd_brusselator)
prob_ode_brusselator_2d = ODEProblem(brusselator_2d_loop,
u0, (0.0, 11.5), p)
sys = modelingtoolkitize(prob_ode_brusselator_2d)
sys = complete(modelingtoolkitize(prob_ode_brusselator_2d))

# test sparse jacobian pattern only.
prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = false)
Expand Down Expand Up @@ -74,7 +74,7 @@ f = DiffEqBase.ODEFunction(sys, u0 = nothing, sparse = true, jac = false)
u0 = similar(init_brusselator_2d(xyd_brusselator), Float32)
prob_ode_brusselator_2d = ODEProblem(brusselator_2d_loop,
u0, (0.0, 11.5), p)
sys = modelingtoolkitize(prob_ode_brusselator_2d)
sys = complete(modelingtoolkitize(prob_ode_brusselator_2d))

prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = false)
@test eltype(prob.f.jac_prototype) == Float32
Expand Down
7 changes: 7 additions & 0 deletions test/jumpsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ rate₃ = γ * I * h
affect₃ = [I ~ I * h - 1, R ~ R + 1]
j₃ = ConstantRateJump(rate₃, affect₃)
@named js2 = JumpSystem([j₁, j₃], t, [S, I, R], [β, γ])
js2 = complete(js2)
u₀ = [999, 1, 0];
p = (0.1 / 1000, 0.01);
tspan = (0.0, 250.0);
Expand All @@ -80,6 +81,7 @@ m = getmean(jprob, Nsims)
@variables S2(t)
obs = [S2 ~ 2 * S]
@named js2b = JumpSystem([j₁, j₃], t, [S, I, R], [β, γ], observed = obs)
js2b = complete(js2b)
dprob = DiscreteProblem(js2b, u₀map, tspan, parammap)
jprob = JumpProblem(js2b, dprob, Direct(), save_positions = (false, false), rng = rng)
sol = solve(jprob, SSAStepper(), saveat = tspan[2] / 10)
Expand Down Expand Up @@ -132,13 +134,15 @@ m2 = getmean(jprob, Nsims)
maj1 = MassActionJump(2 * β / 2, [S => 1, I => 1], [S => -1, I => 1])
maj2 = MassActionJump(γ, [I => 1], [I => -1, R => 1])
@named js3 = JumpSystem([maj1, maj2], t, [S, I, R], [β, γ])
js3 = complete(js3)
dprob = DiscreteProblem(js3, u₀map, tspan, parammap)
jprob = JumpProblem(js3, dprob, Direct(), rng = rng)
m3 = getmean(jprob, Nsims)
@test abs(m - m3) / m < 0.01

# maj jump test with various dep graphs
@named js3b = JumpSystem([maj1, maj2], t, [S, I, R], [β, γ])
js3b = complete(js3b)
jprobb = JumpProblem(js3b, dprob, NRM(), rng = rng)
m4 = getmean(jprobb, Nsims)
@test abs(m - m4) / m < 0.01
Expand All @@ -150,6 +154,7 @@ m4 = getmean(jprobc, Nsims)
maj1 = MassActionJump(2.0, [0 => 1], [S => 1])
maj2 = MassActionJump(γ, [S => 1], [S => -1])
@named js4 = JumpSystem([maj1, maj2], t, [S], [β, γ])
js4 = complete(js4)
dprob = DiscreteProblem(js4, [S => 999], (0, 1000.0), [β => 100.0, γ => 0.01])
jprob = JumpProblem(js4, dprob, Direct(), rng = rng)
m4 = getmean(jprob, Nsims)
Expand All @@ -159,6 +164,7 @@ m4 = getmean(jprob, Nsims)
maj1 = MassActionJump(2.0, [0 => 1], [S => 1])
maj2 = MassActionJump(γ, [S => 2], [S => -1])
@named js4 = JumpSystem([maj1, maj2], t, [S], [β, γ])
js4 = complete(js4)
dprob = DiscreteProblem(js4, [S => 999], (0, 1000.0), [β => 100.0, γ => 0.01])
jprob = JumpProblem(js4, dprob, Direct(), rng = rng)
sol = solve(jprob, SSAStepper());
Expand All @@ -177,6 +183,7 @@ end
maj1 = MassActionJump(k1 * k3, [0 => 1], [A => -1, B => 1])
maj2 = MassActionJump(k2, [B => 1], [A => 1, B => -1])
@named js5 = JumpSystem([maj1, maj2], t, [A, B], [k1, k2, k3])
js5 = complete(js5)
p = [k1 => 2.0, k2 => 0.0, k3 => 0.5]
u₀ = [A => 100, B => 0]
tspan = (0.0, 2000.0)
Expand Down
4 changes: 3 additions & 1 deletion test/lowering_solving.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ p = [σ => 28.0,
β => 8 / 3]

tspan = (0.0, 100.0)

sys = complete(sys)
prob = ODEProblem(sys, u0, tspan, p, jac = true)
probexpr = ODEProblemExpr(sys, u0, tspan, p, jac = true)
sol = solve(prob, Tsit5())
Expand All @@ -52,7 +54,7 @@ lorenz2 = ODESystem(eqs, name = :lorenz2)
@parameters γ
connections = [0 ~ lorenz1.x + lorenz2.y + α * γ]
@named connected = ODESystem(connections, t, [α], [γ], systems = [lorenz1, lorenz2])

connected = complete(connected)
u0 = [lorenz1.x => 1.0,
lorenz1.y => 0.0,
lorenz1.z => 0.0,
Expand Down
10 changes: 5 additions & 5 deletions test/modelingtoolkitize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ x0 = zeros(2)
p = [1.0, 100.0]

prob = OptimizationProblem(rosenbrock, x0, p)
sys = modelingtoolkitize(prob) # symbolicitize me captain!
sys = complete(modelingtoolkitize(prob)) # symbolicitize me captain!

prob = OptimizationProblem(sys, x0, p, grad = true, hess = true)
sol = solve(prob, NelderMead())
Expand Down Expand Up @@ -141,7 +141,7 @@ problem = ODEProblem(SIRD_ac!, ℬ, 𝒯, 𝒫)
@time solution = solve(problem, Tsit5(), saveat = 1:final_time);

problem = ODEProblem(SIRD_ac!, ℬ, 𝒯, 𝒫)
sys = modelingtoolkitize(problem)
sys = complete(modelingtoolkitize(problem))
fast_problem = ODEProblem(sys, ℬ, 𝒯, 𝒫)
@time solution = solve(fast_problem, Tsit5(), saveat = 1:final_time)

Expand Down Expand Up @@ -179,7 +179,7 @@ u0 = [1.0, 0, 0, 0, 0]
p = [9.8, 1]
tspan = (0, 10.0)
pendulum_prob = ODEProblem(pendulum_fun!, u0, tspan, p)
pendulum_sys_org = modelingtoolkitize(pendulum_prob)
pendulum_sys_org = complete(modelingtoolkitize(pendulum_prob))
sts = states(pendulum_sys_org)
pendulum_sys = dae_index_lowering(pendulum_sys_org)
prob = ODEProblem(pendulum_sys, Pair[], tspan)
Expand Down Expand Up @@ -250,7 +250,7 @@ u0 = @LArray [9998.0, 1.0, 1.0, 1.0] (:S, :I, :R, :C)

# Initiate ODE problem
problem = ODEProblem(SIR!, u0, tspan, p)
sys = modelingtoolkitize(problem)
sys = complete(modelingtoolkitize(problem))

@parameters t
@test all(isequal.(parameters(sys), getproperty.(@variables(β, η, ω, φ, σ, μ), :val)))
Expand Down Expand Up @@ -304,6 +304,6 @@ noiseeqs = [0.1 * x,
0.1 * z]

@named sys = SDESystem(eqs, noiseeqs, t, [x, y, z], [sig, rho, beta]; tspan = (0, 1000.0))
prob = SDEProblem(sys)
prob = SDEProblem(complete(sys))
sys = modelingtoolkitize(prob)
@test ModelingToolkit.has_tspan(sys)
7 changes: 5 additions & 2 deletions test/nonlinearsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ eqs = [0 ~ σ * a * h,
0 ~ x *- z) - y,
0 ~ x * y - β * z]
@named ns = NonlinearSystem(eqs, [x, y, z], [σ, ρ, β])
ns = complete(ns)
nlsys_func = generate_function(ns, [x, y, z], [σ, ρ, β])
nf = NonlinearFunction(ns)
jac = calculate_jacobian(ns)
Expand Down Expand Up @@ -98,7 +99,7 @@ eqs1 = [

lorenz = name -> NonlinearSystem(eqs1, [x, y, z, u, F], [σ, ρ, β], name = name)
lorenz1 = lorenz(:lorenz1)
@test_throws ArgumentError NonlinearProblem(lorenz1, zeros(5))
@test_throws ArgumentError NonlinearProblem(complete(lorenz1), zeros(5))
lorenz2 = lorenz(:lorenz2)
@named connected = NonlinearSystem([s ~ a + lorenz1.x
lorenz2.y ~ s * h
Expand Down Expand Up @@ -128,7 +129,7 @@ eqs = [0 ~ σ * (y - x),
0 ~ x *- z) - y,
0 ~ x * y - β * z * h]
@named ns = NonlinearSystem(eqs, [x, y, z], [σ, ρ, β])
np = NonlinearProblem(ns, [0, 0, 0], [1, 2, 3], jac = true, sparse = true)
np = NonlinearProblem(complete(ns), [0, 0, 0], [1, 2, 3], jac = true, sparse = true)
@test calculate_jacobian(ns, sparse = true) isa SparseMatrixCSC

# issue #819
Expand Down Expand Up @@ -199,6 +200,7 @@ eq = [v1 ~ sin(2pi * t * h)
u[4] ~ h]

sys = NonlinearSystem(eqs, collect(u[1:4]), Num[], defaults = Dict([]), name = :test)
sys = complete(sys)
prob = NonlinearProblem(sys, ones(length(states(sys))))

sol = NonlinearSolve.solve(prob, NewtonRaphson())
Expand All @@ -223,6 +225,7 @@ testdict = Dict([:test => 1])
0 ~ x * (b - z) - y,
0 ~ x * y - c * z]
@named sys = NonlinearSystem(eqs, [x, y, z], [a, b, c], defaults = Dict(x => 2.0))
sys = complete(sys)
prob = NonlinearProblem(sys, ones(length(states(sys))))

prob_ = remake(prob, u0 = [1.0, 2.0, 3.0], p = [1.1, 1.2, 1.3])
Expand Down
12 changes: 10 additions & 2 deletions test/odesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ eqs = [D(y₁) ~ -k₁ * y₁ + k₃ * y₂ * y₃,
0 ~ y₁ + y₂ + y₃ - 1,
D(y₂) ~ k₁ * y₁ - k₂ * y₂^2 - k₃ * y₂ * y₃ * κ]
@named sys = ODESystem(eqs, defaults = [k₁ => 100, k₂ => 3e7, y₁ => 1.0])
sys = complete(sys)
u0 = Pair[]
push!(u0, y₂ => 0.0)
push!(u0, y₃ => 0.0)
Expand Down Expand Up @@ -279,7 +280,7 @@ sol_dpmap = solve(prob_dpmap, Rodas5())
sys1 = makesys(:sys1)
sys2 = makesys(:sys2)
@parameters t b=1.0
ODESystem(Equation[], t, [], [b]; systems = [sys1, sys2], name = :foo)
complete(ODESystem(Equation[], t, [], [b]; systems = [sys1, sys2], name = :foo))
end

sys = makecombinedsys()
Expand Down Expand Up @@ -435,6 +436,7 @@ default_u0 = [D(x) => 0.0, x => 10.0]
default_p = [M => 1.0, b => 1.0, k => 1.0]
@named sys = ODESystem(eqs, t, [x], ps; defaults = [default_u0; default_p], tspan)
sys = ode_order_lowering(sys)
sys = complete(sys)
prob = ODEProblem(sys)
sol = solve(prob, Tsit5())
@test sol.t[end] == tspan[end]
Expand All @@ -448,6 +450,7 @@ prob = ODEProblem{false}(sys; u0_constructor = x -> SVector(x...))
D = Differential(t)
eqs = [D(x1) ~ -x1]
@named sys = ODESystem(eqs, t, [x1, x2], [])
sys = complete(sys)
@test_throws ArgumentError ODEProblem(sys, [1.0, 1.0], (0.0, 1.0))
@test_nowarn ODEProblem(sys, [1.0, 1.0], (0.0, 1.0), check_length = false)

Expand Down Expand Up @@ -512,6 +515,7 @@ eqs = [δ(x) ~ a * x - b * x * y
Δ(x) ~ y
U(y) ~ x + 1]
@named de = ODESystem(eqs, t, [x, y], [a, b, c, d])
de = complete(de)
@test generate_difference_cb(de) isa ModelingToolkit.DiffEqCallbacks.DiscreteCallback

# doesn't work with ODEFunction
Expand Down Expand Up @@ -589,6 +593,7 @@ eqs = [D(x) ~ foo(x, ms); D.(ms) .~ 1]
@named sys = ODESystem(eqs, t, [x; ms], [])
@named emptysys = ODESystem(Equation[], t)
@named outersys = compose(emptysys, sys)
outersys = complete(outersys)
prob = ODEProblem(outersys, [sys.x => 1.0; collect(sys.ms) .=> 1:3], (0, 1.0))
@test_nowarn solve(prob, Tsit5())

Expand Down Expand Up @@ -675,6 +680,7 @@ eqs[end] = D(D(z)) ~ α * x - β * y
end

@named sys = ODESystem(eqs, t, us, ps; defaults = defs, preface = preface)
sys = complete(sys)
prob = ODEProblem(sys, [], (0.0, 1.0))
sol = solve(prob, Euler(); dt = 0.1)

Expand Down Expand Up @@ -734,6 +740,7 @@ let
D = Differential(t)
eqs = [D(A) ~ -k1 * k2 * A]
@named sys = ODESystem(eqs, t)
sys = complete(sys)
u0map = [A => 1.0]
pmap = (k1 => 1.0, k2 => 1)
tspan = (0.0, 1.0)
Expand Down Expand Up @@ -887,6 +894,7 @@ let
D = Differential(t)
eqs = [D(A) ~ -k * A]
@named osys = ODESystem(eqs, t)
osys = complete(osys)
oprob = ODEProblem(osys, [A => 1.0], (0.0, 10.0), [k => 1.0]; check_length = false)
@test_nowarn sol = solve(oprob, Tsit5())
end
Expand Down Expand Up @@ -1002,7 +1010,7 @@ testdict = Dict([:name => "test"])
eqs = [∂t(Q) ~ 1 / sin(P)
∂t(P) ~ log(-cos(Q))]
@named sys = ODESystem(eqs, t, [P, Q], [])
sys = debug_system(sys);
sys = complete(debug_system(sys));
prob = ODEProblem(sys, [], (0, 1.0));
du = zero(prob.u0);
if VERSION < v"1.8"
Expand Down
Loading

0 comments on commit 3256ae2

Please sign in to comment.