From 3256ae289a706e84958c2646192d7b2b09c14daa Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 30 Jan 2024 19:26:49 +0530 Subject: [PATCH] test: fix tests --- test/constants.jl | 2 +- test/dae_jacobian.jl | 2 +- test/discretesystem.jl | 3 +++ test/extensions/bifurcationkit.jl | 4 +-- test/funcaffect.jl | 10 ++++---- test/jacobiansparsity.jl | 4 +-- test/jumpsystem.jl | 7 ++++++ test/lowering_solving.jl | 4 ++- test/modelingtoolkitize.jl | 10 ++++---- test/nonlinearsystem.jl | 7 ++++-- test/odesystem.jl | 12 +++++++-- test/optimizationsystem.jl | 25 ++++++++++--------- test/sdesystem.jl | 12 ++++++--- test/serialization.jl | 5 ++-- test/steadystatesystems.jl | 1 + .../index_reduction.jl | 4 +-- test/structural_transformation/tearing.jl | 2 +- test/symbolic_events.jl | 10 +++++--- test/symbolic_parameters.jl | 4 ++- 19 files changed, 82 insertions(+), 46 deletions(-) diff --git a/test/constants.jl b/test/constants.jl index ee9c9fa618..373c37464a 100644 --- a/test/constants.jl +++ b/test/constants.jl @@ -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) diff --git a/test/dae_jacobian.jl b/test/dae_jacobian.jl index 9047a91bc2..4ca6ce865c 100644 --- a/test/dae_jacobian.jl +++ b/test/dae_jacobian.jl @@ -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 diff --git a/test/discretesystem.jl b/test/discretesystem.jl index e8b2a0114d..07b409c5ff 100644 --- a/test/discretesystem.jl +++ b/test/discretesystem.jl @@ -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 @@ -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) @@ -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) diff --git a/test/extensions/bifurcationkit.jl b/test/extensions/bifurcationkit.jl index 53c7369485..a3ed2bb813 100644 --- a/test/extensions/bifurcationkit.jl +++ b/test/extensions/bifurcationkit.jl @@ -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] @@ -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], diff --git a/test/funcaffect.jl b/test/funcaffect.jl index bb953988b5..782f98a62d 100644 --- a/test/funcaffect.jl +++ b/test/funcaffect.jl @@ -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 @@ -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 @@ -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]) @@ -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]) @@ -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()) diff --git a/test/jacobiansparsity.jl b/test/jacobiansparsity.jl index e5738db218..7521444f2a 100644 --- a/test/jacobiansparsity.jl +++ b/test/jacobiansparsity.jl @@ -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) @@ -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 diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 69ac24f9af..8ceb210982 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -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); @@ -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) @@ -132,6 +134,7 @@ 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) @@ -139,6 +142,7 @@ m3 = getmean(jprob, Nsims) # 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 @@ -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) @@ -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()); @@ -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) diff --git a/test/lowering_solving.jl b/test/lowering_solving.jl index 712a2a3595..58dca5e2b1 100644 --- a/test/lowering_solving.jl +++ b/test/lowering_solving.jl @@ -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()) @@ -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, diff --git a/test/modelingtoolkitize.jl b/test/modelingtoolkitize.jl index 517977f99f..6a690fcd28 100644 --- a/test/modelingtoolkitize.jl +++ b/test/modelingtoolkitize.jl @@ -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()) @@ -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) @@ -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) @@ -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))) @@ -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) diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index 0d1fcf90da..09879f24b6 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -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) @@ -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 @@ -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 @@ -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()) @@ -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]) diff --git a/test/odesystem.jl b/test/odesystem.jl index 9bcbbfc130..bf65ca8220 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -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) @@ -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() @@ -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] @@ -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) @@ -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 @@ -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()) @@ -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) @@ -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) @@ -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 @@ -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" diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index 2dbf9f08ed..68d79ad927 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -14,8 +14,8 @@ using ModelingToolkit: get_metadata @variables z @parameters β loss2 = sys1.x - sys2.y + z * β - combinedsys = OptimizationSystem(loss2, [z], [β], systems = [sys1, sys2], - name = :combinedsys) + combinedsys = complete(OptimizationSystem(loss2, [z], [β], systems = [sys1, sys2], + name = :combinedsys)) equations(combinedsys) states(combinedsys) @@ -27,7 +27,7 @@ using ModelingToolkit: get_metadata generate_gradient(combinedsys) generate_hessian(combinedsys) hess_sparsity = ModelingToolkit.hessian_sparsity(sys1) - sparse_prob = OptimizationProblem(sys1, [x, y], [a, b], grad = true, sparse = true) + sparse_prob = OptimizationProblem(complete(sys1), [x, y], [a, b], grad = true, sparse = true) @test sparse_prob.f.hess_prototype.rowval == hess_sparsity.rowval @test sparse_prob.f.hess_prototype.colptr == hess_sparsity.colptr @@ -57,7 +57,7 @@ end x^2 + y^2 ≲ 1.0, ] @named sys = OptimizationSystem(loss, [x, y], [a, b], constraints = cons) - + sys = complete(sys) prob = OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 1.0], grad = true, hess = true, cons_j = true, cons_h = true) @test prob.f.sys === sys @@ -149,8 +149,8 @@ end ] sys1 = OptimizationSystem(o1, [x], [a], name = :sys1, constraints = c1) sys2 = OptimizationSystem(o2, [y], [], name = :sys2, constraints = c2) - sys = OptimizationSystem(0, [], []; name = :sys, systems = [sys1, sys2], - constraints = [sys1.x + sys2.y ~ 2], checks = false) + sys = complete(OptimizationSystem(0, [], []; name = :sys, systems = [sys1, sys2], + constraints = [sys1.x + sys2.y ~ 2], checks = false)) prob = OptimizationProblem(sys, [0.0, 0.0]) @test isequal(constraints(sys), vcat(sys1.x + sys2.y ~ 2, sys1.x ~ 1, sys2.y ~ 1)) @test isequal(equations(sys), (sys1.x - sys1.a)^2 + (sys2.y - 1 / 2)^2) @@ -190,8 +190,8 @@ end @variables z @parameters β loss2 = sys1.x - sys2.y + z * β - combinedsys = OptimizationSystem(loss2, [z], [β], systems = [sys1, sys2], - name = :combinedsys) + combinedsys = complete(OptimizationSystem(loss2, [z], [β], systems = [sys1, sys2], + name = :combinedsys)) u0 = [sys1.x => 1.0 sys1.y => 2.0 @@ -243,7 +243,7 @@ end x[1]^2 + x[2]^2 ≲ 2.0, ]) - prob = OptimizationProblem(sys, [x[1] => 2.0, x[2] => 0.0], [], grad = true, + prob = OptimizationProblem(complete(sys), [x[1] => 2.0, x[2] => 0.0], [], grad = true, hess = true, cons_j = true, cons_h = true) sol = Optimization.solve(prob, Ipopt.Optimizer(); print_level = 0) @test sol.u ≈ [1, 0] @@ -257,7 +257,7 @@ end @parameters a b loss = (a - x)^2 + b * (y - x^2)^2 @named sys = OptimizationSystem(loss, [x, y], [a, b, c]) - prob = OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0]) + prob = OptimizationProblem(complete(sys), [x => 0.0, y => 0.0], [a => 1.0, b => 100.0]) @test prob.lb == [-Inf, 0.0] @test prob.ub == [Inf, Inf] end @@ -269,12 +269,12 @@ end cons = [ x₁^2 + x₂^2 ≲ 1.0, ] - sys1 = OptimizationSystem(loss, [x₁, x₂], [α₁, α₂], name = :sys1, constraints = cons) + sys1 = complete(OptimizationSystem(loss, [x₁, x₂], [α₁, α₂], name = :sys1, constraints = cons)) prob1 = OptimizationProblem(sys1, [x₁ => 0.0, x₂ => 0.0], [α₁ => 1.0, α₂ => 100.0], grad = true, hess = true, cons_j = true, cons_h = true) - sys2 = modelingtoolkitize(prob1) + sys2 = complete(modelingtoolkitize(prob1)) prob2 = OptimizationProblem(sys2, [x₁ => 0.0, x₂ => 0.0], [α₁ => 1.0, α₂ => 100.0], grad = true, hess = true, cons_j = true, cons_h = true) @@ -289,6 +289,7 @@ end @parameters a b loss = (a - x)^2 + b * (y - x^2)^2 @named sys = OptimizationSystem(loss, [x, y], [a, b], constraints = [x^2 + y^2 ≲ 0.0]) + sys = complete(sys) @test_throws ArgumentError OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0], diff --git a/test/sdesystem.jl b/test/sdesystem.jl index a520a9d58e..6c92fb0cb5 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -21,6 +21,7 @@ noiseeqs = [0.1 * x, @test SDESystem(sys, noiseeqs, name = :foo) isa SDESystem @named de = SDESystem(eqs, noiseeqs, t, [x, y, z], [σ, ρ, β], tspan = (0.0, 10.0)) +de = complete(de) f = eval(generate_diffusion_function(de)[1]) @test f(ones(3), rand(3), nothing) == 0.1ones(3) @@ -42,6 +43,7 @@ noiseeqs_nd = [0.01*x 0.01*x*y 0.02*x*z σ 0.01*y 0.02*x*z ρ β 0.01*z] @named de = SDESystem(eqs, noiseeqs_nd, t, [x, y, z], [σ, ρ, β]) +de = complete(de) f = eval(generate_diffusion_function(de)[1]) @test f([1, 2, 3.0], [0.1, 0.2, 0.3], nothing) == [0.01*1 0.01*1*2 0.02*1*3 0.1 0.01*2 0.02*1*3 @@ -504,13 +506,14 @@ noiseeqs = [0.1 * x] ] @named de = SDESystem(eqs, noiseeqs, t, [x], [α, β], observed = [weight ~ x * 10]) - + de = complete(de) prob = SDEProblem(de, u0map, (0.0, 1.0), parammap) sol = solve(prob, EM(), dt = dt) @test observed(de) == [weight ~ x * 10] @test sol[weight] == 10 * sol[x] @named ode = ODESystem(eqs, t, [x], [α, β], observed = [weight ~ x * 10]) + ode = complete(ode) odeprob = ODEProblem(ode, u0map, (0.0, 1.0), parammap) solode = solve(odeprob, Tsit5()) @test observed(ode) == [weight ~ x * 10] @@ -528,7 +531,7 @@ end noiseeqs = [β * x] @named de = SDESystem(eqs, noiseeqs, t, [x], [α, β]) - + de = complete(de) g(x) = x[1]^2 dt = 1 // 2^(7) x0 = 0.1 @@ -555,7 +558,7 @@ end seeds = rand(UInt, numtraj) ensemble_prob = EnsembleProblem(prob; - output_func = (sol, i) -> (g(sol[end]), false), + output_func = (sol, i) -> (g(sol.u[end]), false), prob_func = prob_func) sim = solve(ensemble_prob, EM(), dt = dt, trajectories = numtraj) @@ -564,7 +567,7 @@ end ## Variance reduction method u = x - demod = ModelingToolkit.Girsanov_transform(de, u; θ0 = 0.1) + demod = complete(ModelingToolkit.Girsanov_transform(de, u; θ0 = 0.1)) probmod = SDEProblem(demod, u0map, (0.0, 1.0), parammap) @@ -609,6 +612,7 @@ diffusion_eqs = [s*x 0 (s * x * z)-s * z 0] sys2 = SDESystem(drift_eqs, diffusion_eqs, t, sts, ps, name = :sys1) +sys2 = complete(sys2) @test sys1 == sys2 prob = SDEProblem(sys1, sts .=> [1.0, 0.0, 0.0], diff --git a/test/serialization.jl b/test/serialization.jl index 79f6f34bdf..430a8765d1 100644 --- a/test/serialization.jl +++ b/test/serialization.jl @@ -5,6 +5,7 @@ using ModelingToolkit, SciMLBase, Serialization, OrdinaryDiffEq D = Differential(t) @named sys = ODESystem([D(x) ~ -0.5 * x], defaults = Dict(x => 1.0)) +sys = complete(sys) for prob in [ eval(ModelingToolkit.ODEProblem{false}(sys, nothing, nothing, SciMLBase.NullParameters())), @@ -37,7 +38,7 @@ sol = solve(prob, ImplicitEuler()) ## Check ODESystem with Observables ---------- ss_exp = ModelingToolkit.toexpr(ss) -ss_ = eval(ss_exp) +ss_ = complete(eval(ss_exp)) prob_ = ODEProblem(ss_, [], (0, 0.1)) sol_ = solve(prob_, ImplicitEuler()) @test sol[all_obs] == sol_[all_obs] @@ -64,5 +65,5 @@ end) probexpr = ODEProblemExpr{true}(ss, [], (0, 0.1); observedfun_exp); prob_obs = eval(probexpr) sol_obs = solve(prob_obs, ImplicitEuler()) - +@show all_obs @test sol_obs[all_obs] == sol[all_obs] diff --git a/test/steadystatesystems.jl b/test/steadystatesystems.jl index 9d31844b23..9b5a6c603f 100644 --- a/test/steadystatesystems.jl +++ b/test/steadystatesystems.jl @@ -7,6 +7,7 @@ using Test D = Differential(t) eqs = [D(x) ~ x^2 - r] @named de = ODESystem(eqs) +de = complete(de) for factor in [1e-1, 1e0, 1e10], u0_p in [(2.34, 2.676), (22.34, 1.632), (0.3, 15.676), (0.3, 0.006)] diff --git a/test/structural_transformation/index_reduction.jl b/test/structural_transformation/index_reduction.jl index e6f7534e58..5effe83dcb 100644 --- a/test/structural_transformation/index_reduction.jl +++ b/test/structural_transformation/index_reduction.jl @@ -71,7 +71,7 @@ prob = ODEProblem(ODEFunction(first_order_idx1_pendulum), sol = solve(prob, Rodas5()); #plot(sol, idxs=(1, 2)) -new_sys = dae_index_lowering(ModelingToolkit.ode_order_lowering(pendulum2)) +new_sys = complete(dae_index_lowering(ModelingToolkit.ode_order_lowering(pendulum2))) prob_auto = ODEProblem(new_sys, [D(x) => 0, @@ -98,7 +98,7 @@ pendulum2 = ODESystem(eqs2, t, [x, y, T], [L, g], name = :pendulum) first_order_sys = ModelingToolkit.ode_order_lowering(pendulum2) # Perform index reduction to get an Index 1 DAE -new_sys = dae_index_lowering(first_order_sys) +new_sys = complete(dae_index_lowering(first_order_sys)) u0 = [ D(x) => 0.0, diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index b2e4b846d6..e1605d6ed8 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -119,7 +119,7 @@ end # unknowns: u5 # solve for # 0 = u5 - hypot(sin(u5), hypot(cos(sin(u5)), hypot(sin(u5), cos(sin(u5))))) -tornsys = tearing(sys) +tornsys = complete(tearing(sys)) @test isequal(equations(tornsys), [0 ~ u5 - hypot(u4, u1)]) prob = NonlinearProblem(tornsys, ones(1)) sol = solve(prob, NewtonRaphson()) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 4620fbaddb..9579deaa61 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -136,6 +136,8 @@ fsys = flatten(sys) @test isequal(ModelingToolkit.continuous_events(sys2)[1].eqs[], x ~ 2) @test isequal(ModelingToolkit.continuous_events(sys2)[2].eqs[], sys.x ~ 1) +sys = complete(sys) +sys2 = complete(sys2) # Functions should be generated for root-finding equations prob = ODEProblem(sys, Pair[], (0.0, 2.0)) p0 = 0 @@ -190,6 +192,7 @@ sol = solve(prob, Tsit5()) @test minimum(t -> abs(t - 2), sol.t) < 1e-10 # test that the solver stepped at the second root @named sys = ODESystem(eqs, continuous_events = [x ~ 1, x ~ 2]) # two root eqs using the same state +sys = complete(sys) prob = ODEProblem(sys, Pair[], (0.0, 3.0)) @test get_callback(prob) isa ModelingToolkit.DiffEqCallbacks.VectorContinuousCallback sol = solve(prob, Tsit5()) @@ -341,7 +344,7 @@ sys = structural_simplify(model) let function testsol(osys, u0, p, tspan; tstops = Float64[], skipparamtest = false, kwargs...) - oprob = ODEProblem(osys, u0, tspan, p; kwargs...) + oprob = ODEProblem(complete(osys), u0, tspan, p; kwargs...) sol = solve(oprob, Tsit5(); tstops = tstops, abstol = 1e-10, reltol = 1e-10) @test isapprox(sol(1.0000000001)[1] - sol(0.999999999)[1], 1.0; rtol = 1e-6) !skipparamtest && (@test oprob.p[1] == 1.0) @@ -392,7 +395,7 @@ let end cb2‵‵ = [2.0] => (affect!, [], [k], nothing) @named osys4 = ODESystem(eqs, t, [A], [k, t1], discrete_events = [cb1, cb2‵‵]) - oprob4 = ODEProblem(osys4, u0, tspan, p) + oprob4 = ODEProblem(complete(osys4), u0, tspan, p) testsol(osys4, u0, p, tspan; tstops = [1.0]) # mixing with symbolic condition in the func affect @@ -415,7 +418,7 @@ end let function testsol(ssys, u0, p, tspan; tstops = Float64[], skipparamtest = false, kwargs...) - sprob = SDEProblem(ssys, u0, tspan, p; kwargs...) + sprob = SDEProblem(complete(ssys), u0, tspan, p; kwargs...) sol = solve(sprob, RI5(); tstops = tstops, abstol = 1e-10, reltol = 1e-10) @test isapprox(sol(1.0000000001)[1] - sol(0.999999999)[1], 1.0; rtol = 1e-4) !skipparamtest && (@test sprob.p[1] == 1.0) @@ -495,6 +498,7 @@ end let rng = rng function testsol(jsys, u0, p, tspan; tstops = Float64[], skipparamtest = false, N = 40000, kwargs...) + jsys = complete(jsys) dprob = DiscreteProblem(jsys, u0, tspan, p) jprob = JumpProblem(jsys, dprob, Direct(); kwargs...) sol = solve(jprob, SSAStepper(); tstops = tstops) diff --git a/test/symbolic_parameters.jl b/test/symbolic_parameters.jl index 1df6782faf..3417f5b15a 100644 --- a/test/symbolic_parameters.jl +++ b/test/symbolic_parameters.jl @@ -25,7 +25,7 @@ resolved = ModelingToolkit.varmap_to_vars(Dict(), parameters(ns), defaults = ModelingToolkit.defaults(ns)) @test resolved == [1, 0.1 + 1, (0.1 + 1) * 1.1] -prob = NonlinearProblem(ns, [u => 1.0], Pair[]) +prob = NonlinearProblem(complete(ns), [u => 1.0], Pair[]) @test prob.u0 == [1.0, 1.1, 0.9] @show sol = solve(prob, NewtonRaphson()) @@ -39,6 +39,7 @@ res = ModelingToolkit.varmap_to_vars(Dict(), parameters(top), defaults = ModelingToolkit.defaults(top)) @test res == [0.5, 1, 0.1 + 1, (0.1 + 1) * 1.1] +top = complete(top) prob = NonlinearProblem(top, [states(ns, u) => 1.0, a => 1.0], []) @test prob.u0 == [1.0, 0.5, 1.1, 0.9] @show sol = solve(prob, NewtonRaphson()) @@ -59,6 +60,7 @@ end) der = Differential(t) eqs = [der(x) ~ x] @named sys = ODESystem(eqs, t, vars, [x0]) +sys = complete(sys) pars = [ x0 => 10.0, ]