-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ea5b75
commit 336c932
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters