Skip to content

Commit

Permalink
test: add remake tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Jan 2, 2024
1 parent 9ea5b75 commit 336c932
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
118 changes: 118 additions & 0 deletions test/remake.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using ModelingToolkit, SymbolicIndexingInterface

@parameters σ ρ β
@variables t x(t) y(t) z(t)
D = Differential(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)
u0 = [D(x) => 2.0,
x => 1.0,
y => 0.0,
z => 0.0]

p ==> 28.0,
ρ => 10.0,
β => 8 / 3]

tspan = (0.0, 100.0)
oprob = ODEProblem(sys, u0, tspan, p, jac = true)

oprob2 = remake(
oprob;
u0 = [x => 2.0, sys.y => 1.2, :z => 1.0],
p ==> 29.0, sys.ρ => 11.0, => 3.0]
)
@test oprob2.u0 isa Vector{<:Number}
@test oprob2.p isa Vector{<:Number}
@test oprob2[x] == oprob2[sys.x] == oprob2[:x] == 2.0
@test oprob2[y] == oprob2[sys.y] == oprob2[:y] == 1.2
@test oprob2[z] == oprob2[sys.z] == oprob2[:z] == 1.0
@test getp(sys, σ)(oprob2) == 29.0
@test getp(sys, sys.ρ)(oprob2) == 11.0
@test getp(sys, )(oprob2) == 3.0

oprob3 = remake(oprob; p ==> 30.0]) # partial update
@test getp(sys, σ)(oprob3) == 30.0

# SDEProblem.
noiseeqs = [0.1 * x,
0.1 * y,
0.1 * z]
@named noise_sys = SDESystem(sys, noiseeqs)
sprob = SDEProblem(noise_sys, u0, (0.0, 100.0), p)

sprob2 = remake(
sprob;
u0 = [x => 2.0, sys.y => 1.2, :z => 1.0],
p ==> 29.0, sys.ρ => 11.0, => 3.0]
)
@test sprob.u0 isa Vector{<:Number}
@test sprob.p isa Vector{<:Number}
@test sprob2[x] == sprob2[sys.x] == sprob2[:x] == 2.0
@test sprob2[y] == sprob2[sys.y] == sprob2[:y] == 1.2
@test sprob2[z] == sprob2[sys.z] == sprob2[:z] == 1.0
@test getp(sys, σ)(sprob2) == 29.0
@test getp(sys, sys.ρ)(sprob2) == 11.0
@test getp(sys, )(sprob2) == 3.0

sprob3 = remake(sprob; p ==> 30.0]) # partial update
@test getp(sys, σ)(sprob3) == 30.0

# DiscreteProblem
@named de = DiscreteSystem(
[D(x) ~ σ*(y-x),
D(y) ~ x*-z)-y,
D(z) ~ x*y - β*z],
t,
[x, y, z],
[σ, ρ, β],
)
dprob = DiscreteProblem(de, u0, tspan, p)
dprob2 = remake(
dprob;
u0 = [x => 2.0, sys.y => 1.2, :z => 1.0],
p ==> 29.0, sys.ρ => 11.0, => 3.0]
)
@test dprob.u0 isa Vector{<:Number}
@test dprob.p isa Vector{<:Number}
@test dprob2[x] == dprob2[sys.x] == dprob2[:x] == 2.0
@test dprob2[y] == dprob2[sys.y] == dprob2[:y] == 1.2
@test dprob2[z] == dprob2[sys.z] == dprob2[:z] == 1.0
@test getp(sys, σ)(dprob2) == 29.0
@test getp(sys, sys.ρ)(dprob2) == 11.0
@test getp(sys, )(dprob2) == 3.0

dprob3 = remake(dprob; p ==> 30.0]) # partial update
@test getp(sys, σ)(dprob3) == 30.0

# NonlinearProblem
@named ns = NonlinearSystem(
[0 ~ σ*(y-x),
0 ~ x*-z)-y,
0 ~ x*y - β*z],
[x,y,z],
[σ,ρ,β]
)
nlprob = NonlinearProblem(ns, u0, p)

nlprob2 = remake(
nlprob;
u0 = [x => 2.0, sys.y => 1.2, :z => 1.0],
p ==> 29.0, sys.ρ => 11.0, => 3.0]
)
@test nlprob.u0 isa Vector{<:Number}
@test nlprob.p isa Vector{<:Number}
@test nlprob2[x] == nlprob2[sys.x] == nlprob2[:x] == 2.0
@test nlprob2[y] == nlprob2[sys.y] == nlprob2[:y] == 1.2
@test nlprob2[z] == nlprob2[sys.z] == nlprob2[:z] == 1.0
@test getp(sys, σ)(nlprob2) == 29.0
@test getp(sys, sys.ρ)(nlprob2) == 11.0
@test getp(sys, )(nlprob2) == 3.0

nlprob3 = remake(nlprob; p ==> 30.0]) # partial update
@test getp(sys, σ)(nlprob3) == 30.0
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ end
@time @safetestset "Problem building tests" begin
include("problem_building_test.jl")
end
@time @safetestset "Remake tests" begin
include("remake.jl")
end
end

if !is_APPVEYOR && GROUP == "Downstream"
Expand Down

0 comments on commit 336c932

Please sign in to comment.