Skip to content

Commit 449d368

Browse files
csimalDatseris
andauthored
Fix broken default constructors (#29)
* Add tests for default constructors * Add default value and fix docs * Add default value and fix broken tests * Add missing parameter value and fix typo * Update Project.toml --------- Co-authored-by: George Datseris <[email protected]>
1 parent 17a8979 commit 449d368

File tree

5 files changed

+131
-48
lines changed

5 files changed

+131
-48
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PredefinedDynamicalSystems"
22
uuid = "31e2f376-db9e-427a-b76e-a14f56347a14"
33
repo = "https://github.com/JuliaDynamics/PredefinedDynamicalSystems.jl.git"
4-
version = "1.2"
4+
version = "1.3"
55

66
[deps]
77
DynamicalSystemsBase = "6e36e845-645a-534a-86f2-f5d4aa5a06b4"

src/continuous_famous_systems.jl

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ end
8585
return SVector{3}(du1, du2, du3)
8686
end
8787
function chua_jacob(u, p, t)
88-
return SMatrix{3,3}(-p[1]*(1 + chua_element_derivative(u[1], p[3], p[4])),1.0,0.0,p[1],-1.0,-p[2],0.0,1.0,0.0)
88+
return SMatrix{3,3}(-p[1]*(1 + chua_element_derivative(u[1], p[3], p[4])),1.0,0.0,p[1],-1.0,-p[2],0.0,1.0,0.0)
8989
end
9090
# Helper functions for Chua's circuit.
9191
function chua_element(x, m0, m1)
@@ -130,7 +130,7 @@ end
130130
return SVector{3}(du1, du2, du3)
131131
end
132132
function roessler_jacob(u, p, t)
133-
return SMatrix{3,3}(0.0,1.0,u[3],-1.0,p[1],0.0,-1.0,0.0,u[1]-p[3])
133+
return SMatrix{3,3}(0.0,1.0,u[3],-1.0,p[1],0.0,-1.0,0.0,u[1]-p[3])
134134
end
135135

136136
"""
@@ -170,7 +170,7 @@ end
170170
sin_ϕ = sin(φ); cos_ϕ = cos(φ)
171171
sin_θ₂ = sin(u[3]); sin_θ₁ = sin(u[1])
172172
Δ = (p[4] + p[5]) - p[5] * cos_ϕ^2
173-
173+
174174
du1 = u[2]
175175
du2 = (p[5] * (cos_ϕ * (p[2] * u[2]^2 * sin_ϕ + p[1] * sin_θ₂) +
176176
p[3] * u[4]^2 * sin_ϕ) -
@@ -295,7 +295,7 @@ end
295295
`N` is the chain length, `F` the forcing. Jacobian is created automatically.
296296
(parameter container only contains `F`)
297297
"""
298-
function lorenz96(N::Int, u0 = range(0; length = N, step = 0.1); F=0.01)
298+
function lorenz96(N::Int = 4, u0 = range(0; length = N, step = 0.1); F=0.01)
299299
@assert N 4 "`N` must be at least 4"
300300
lor96 = Lorenz96{N}() # create struct
301301
return CoupledODEs(lor96, u0, [F])
@@ -398,7 +398,7 @@ function gissinger_rule(u, p, t)
398398
return SVector{3}(du1, du2, du3)
399399
end
400400
function gissinger_jacob(u, p, t)
401-
μ, ν, Γ = p
401+
μ, ν, Γ = p
402402
return SMatrix{3,3}(μ,u[3],u[2],-u[3],-ν,u[1],-u[2],u[1],-1)
403403
end
404404

@@ -433,7 +433,7 @@ function rikitake_jacob(u, p, t)
433433
μ, α = p
434434
x,y,z = u
435435

436-
return SMatrix{3,3}(-μ,z-α,-y,z,-μ,-x,y,x,0)
436+
return SMatrix{3,3}(-μ,z-α,-y,z,-μ,-x,y,x,0)
437437
end
438438

439439
"""
@@ -472,8 +472,8 @@ function nosehoover_rule(u, p, t)
472472
end
473473
function nosehoover_jacob(u, p, t)
474474
x,y,z = u
475-
476-
return SMatrix{3,3}(0,-1,0,1,z,-2y,0,y,0)
475+
476+
return SMatrix{3,3}(0,-1,0,1,z,-2y,0,y,0)
477477
end
478478

479479
"""
@@ -613,7 +613,7 @@ end
613613
function ueda_jacob(u, p, t)
614614
x,y = u
615615
k, B = p
616-
return SMatrix{2,2}(0,-3*x^2,1,-k)
616+
return SMatrix{2,2}(0,-3*x^2,1,-k)
617617
end
618618

619619

@@ -778,9 +778,9 @@ function stommel_thermohaline_jacob(x, p, t)
778778
η1, η2, η3 = p
779779
q = abs(T-S)
780780
if T S
781-
return SMatrix{2,2}((-1 - 2T + S), -S,T,(-η3 - T + 2S))
781+
return SMatrix{2,2}((-1 - 2T + S), -S,T,(-η3 - T + 2S))
782782
else
783-
return SMatrix{2,2}((-1 + 2T - S), S,-T,(-η3 + T - 2S))
783+
return SMatrix{2,2}((-1 + 2T - S), S,-T,(-η3 + T - 2S))
784784
end
785785
end
786786

@@ -823,7 +823,7 @@ end
823823
end
824824
function lorenz84_rule_jacob(u, p, t)
825825
F, G, a, b = p
826-
x, y, z = u
826+
x, y, z = u
827827
return SMatrix{3,3}(-a,y-b*z,b*y+z,-2y,x-1,b*x,-2z,-b*x,x-1)
828828
end
829829

@@ -867,7 +867,7 @@ end
867867
return SVector{3}(dx, dy, dz)
868868
end
869869
function lorenzdl_rule_jacob(u, p, t)
870-
x, y, z = u
870+
x, y, z = u
871871
return SMatrix{3,3}(-1,-z,y,1,0,x,0,-x,0)
872872
end
873873

@@ -909,7 +909,7 @@ end
909909

910910

911911
"""
912-
kuramoto(D = 20, u0 = range(0, 2π; length = D);
912+
kuramoto(D = 25, u0 = range(0, 2π; length = D);
913913
K = 0.3, ω = range(-1, 1; length = D)
914914
)
915915
The Kuramoto model[^Kuramoto1975] of `D` coupled oscillators with equation
@@ -977,7 +977,7 @@ end
977977
function sprott_dissipative_conservative_jacob(u, p, t)
978978
a, b, c = p
979979
x, y, z = u
980-
980+
981981
return SMatrix{3,3}(a*y + z,-4x,c - 2x,1 + a*x,b*z,-2y,x,b*y,0)
982982
end
983983

@@ -1188,15 +1188,15 @@ function hindmarshrose_rule(u, p, t)
11881188
end
11891189
function hindmarshrose_jacob(u, p, t)
11901190
@inbounds begin
1191-
a,b,c,d,r,s, xr, I = p
1191+
a,b,c,d,r,s, xr, I = p
11921192
return SMatrix{3,3}(-3*a*u[1]^2 + 2*b*u[1],-2*d*u[1],r*s,1,-1,0,-1,0,-r)
11931193
end
11941194
end
11951195

11961196
"""
11971197
```julia
11981198
hindmarshrose_two_coupled(u0=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6];
1199-
a = 1.0, b = 3.0, d = 5.0, r = 0.001, s = 4.0, xr = -1.6, I = 4.0,
1199+
a = 1.0, b = 3.0, c=1.0, d = 5.0, r = 0.001, s = 4.0, xr = -1.6, I = 4.0,
12001200
k1 = -0.17, k2 = -0.17, k_el = 0.0, xv = 2.0)
12011201
```
12021202
```math
@@ -1214,7 +1214,7 @@ The default parameter values are taken from article "Dragon-king-like extreme ev
12141214
coupled bursting neurons", DOI:https://doi.org/10.1103/PhysRevE.97.062311.
12151215
"""
12161216
function hindmarshrose_two_coupled(u0=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6];
1217-
a = 1.0, b = 3.0, d = 5.0, r = 0.001, s = 4.0, xr = -1.6, I = 4.0,
1217+
a = 1.0, b = 3.0, c=1.0, d = 5.0, r = 0.001, s = 4.0, xr = -1.6, I = 4.0,
12181218
k1 = -0.17, k2 = -0.17, k_el = 0.0, xv = 2.0)
12191219
return CoupledODEs(hindmarshrose_coupled_rule, u0, [a, b, c, d, r, s, xr, I, k1, k2, k_el, xv])
12201220
end
@@ -1225,11 +1225,11 @@ function hindmarshrose_coupled_rule(u, p, t)
12251225
a, b, c, d, r, s, xr, I, k1, k2, k_el, xv = p
12261226
x1, y1, z1, x2, y2, z2 = u
12271227

1228-
du1 = y1 + b * x1 ^ 2 - a * x1 ^3 - z1 + I - k1 * ( x1 - vs ) * sigma(x2) + el_link * ( x2 - x1 )
1228+
du1 = y1 + b * x1 ^ 2 - a * x1 ^3 - z1 + I - k1 * ( x1 - xv ) * sigma(x2) + k_el * ( x2 - x1 )
12291229
du2 = c - d * x1 ^2 - y1
12301230
du3 = r * ( s * ( x1 - xr ) - z1 )
12311231

1232-
du4 = y2 + b * x2 ^ 2 - a * x2 ^3 - z2 + I - k2 * ( x2 - vs ) * sigma(x1) + el_link * ( x1 - x2 )
1232+
du4 = y2 + b * x2 ^ 2 - a * x2 ^3 - z2 + I - k2 * ( x2 - xv ) * sigma(x1) + k_el * ( x1 - x2 )
12331233
du5 = c - d * x2 ^2 - y2
12341234
du6 = r * ( s * ( x2 - xr ) - z2 )
12351235
return SVector(du1, du2, du3, du4, du5, du6)
@@ -1288,7 +1288,7 @@ end
12881288
function stuartlandau_jacob(u, p, t)
12891289
@inbounds begin
12901290
μ, ω, b = p
1291-
1291+
12921292
return SMatrix{2,2}- 3*u[1]^2 -u[2]^2 -2*b*u[1]*u[2],-2*u[1]*u[2] +ω +b*u[2]^2 +3*b*u[1]^2,
12931293
-2*u[1]*u[2] -ω -b*u[1]^2 -3*b*u[2]^2-u[1]^2 -3*u[2]^2 +2*b*u[1]*u[2])
12941294
end

src/discrete_famous_systems.jl

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ The first `M` entries of the state are the angles, the last `M` are the momenta.
111111
"""
112112
function coupledstandardmaps end
113113
using SparseArrays
114-
function coupledstandardmaps(M::Int, u0 = 0.001rand(2M);
114+
function coupledstandardmaps(M::Int = 2, u0 = 0.001rand(2M);
115115
ks = ones(M), Γ = 1.0)
116116

117117
SV = SVector{M, Int}
@@ -130,7 +130,7 @@ function coupledstandardmaps(M::Int, u0 = 0.001rand(2M);
130130
sparseJ = sparse(J)
131131
p = vcat(ks, Γ)
132132
csm(sparseJ, u0, p, 0)
133-
return DeterministicIteratedMap(csm, u0, p, csm, sparseJ)
133+
return DeterministicIteratedMap(csm, u0, p)
134134
end
135135
struct CoupledStandardMaps{N}
136136
idxs::SVector{N, Int}
@@ -148,7 +148,7 @@ function (f::CoupledStandardMaps{N})(xnew::AbstractVector, x, p, n) where {N}
148148

149149
xnew[i] = mod2pi(x[i] + xnew[i+N])
150150
end
151-
return xnew
151+
return nothing
152152
end
153153
function (f::CoupledStandardMaps{M})(
154154
J::AbstractMatrix, x, p, n) where {M}
@@ -250,23 +250,23 @@ x_n(1 + |2x_n|^{z-1}), & \\quad |x_n| \\le 0.5 \\\\
250250
[2] : Meyer et al., New. J. Phys **20** (2019)
251251
"""
252252
function pomeau_manneville(u0 = 0.2, z = 2.5)
253-
return DeterministicIteratedMap(pm_rule, u0, [z], pm_jac)
253+
return DeterministicIteratedMap(pm_rule, SVector{1}(u0), [z])
254254
end
255255
function pm_rule(x, p, n)
256-
if x < -0.5
257-
-4x - 3
258-
elseif -0.5 x 0.5
259-
@inbounds x*(1 + abs(2x)^(p[1]-1))
256+
if x[1] < -0.5
257+
SVector{1}(-4x[1] - 3)
258+
elseif -0.5 x[1] 0.5
259+
@inbounds SVector{1}(x[1]*(1 + abs(2x[1])^(p[1]-1)))
260260
else
261-
-4x + 3
261+
SVector{1}(-4x[1] + 3)
262262
end
263263
end
264264
function pomeau_manneville_jacob(x, p, n)
265-
if x < -0.5
265+
if x[1] < -0.5
266266
-4.0
267-
elseif -0.5 x 0.5
267+
elseif -0.5 x[1] 0.5
268268
@inbounds z = p[1]
269-
0.5(x^2 * 2^z * (z-1)*abs(x)^(z-3) + 2^z * abs(x)^(z-1) + 2)
269+
0.5(x[1]^2 * 2^z * (z-1)*abs(x[1])^(z-3) + 2^z * abs(x[1])^(z-1) + 2)
270270
else
271271
-4.0
272272
end
@@ -288,13 +288,13 @@ function's documentation string.
288288
[^Manneville1980]: Manneville, P. (1980). Intermittency, self-similarity and 1/f spectrum in dissipative dynamical systems. [Journal de Physique, 41(11), 1235–1243](https://doi.org/10.1051/jphys:0198000410110123500)
289289
"""
290290
function manneville_simple(x0=0.4; ε = 0.1)
291-
return DeterministicIteratedMap(manneville_f, x0, [ε], manneville_j)
291+
return DeterministicIteratedMap(manneville_f, SVector{1}(x0), [ε])
292292
end
293293

294294
function manneville_f(x, p, t)
295295
e = p[1]
296-
y = (1+e)*x + (1-e)*x*x
297-
return y%1
296+
y = (1+e)*x[1] + (1-e)*x[1]*x[1]
297+
return SVector{1}(y%1)
298298
end
299299
manneville_simple_jacob(x, p, n) = (1+p[1]) + (1-p[1])*2x
300300

@@ -411,15 +411,15 @@ function tentmap(u0 = 0.25, μ = 2.0)
411411
end
412412
function tentmap_rule(x, p, n)
413413
μ = p[1]
414-
if x < 0.5
415-
μ*x
414+
if x[1] < 0.5
415+
SVector{1}(μ*x[1])
416416
else
417-
μ*(1 - x)
417+
SVector{1}(μ*(1 - x[1]))
418418
end
419419
end
420420
function tentmap_jacob(x, p, n)
421421
μ = p[1]
422-
if x < -0.5
422+
if x[1] < -0.5
423423
μ
424424
else
425425
-μ
@@ -440,14 +440,14 @@ The parameter β controls the dynamics of the map. Its Lyapunov exponent can be
440440
At β=2, it becomes the dyadic transformation, also known as the bit shift map, the 2x mod 1 map, the Bernoulli map or the sawtooth map. The typical trajectory for this case is chaotic, though there are countably infinite periodic orbits [^Ott2002].
441441
"""
442442
function betatransformationmap(u0 = 0.25; β=2.0)
443-
return DeterministicIteratedMap(betatransformation_rule, u0, [β])
443+
return DeterministicIteratedMap(betatransformation_rule, SVector{1}(u0), [β])
444444
end
445445
function betatransformation_rule(x, p, n)
446446
@inbounds β = p[1]
447-
if 0 x < 1/β
448-
β*x
447+
if 0 x[1] < 1/β
448+
SVector{1}(β*x[1])
449449
else
450-
β*x - 1
450+
SVector{1}(β*x - 1)
451451
end
452452
end
453453
function betatransformation_jacob(x, p, n)
@@ -520,9 +520,9 @@ end
520520
a,b,c,d = p
521521
x,y = u
522522
t = c - d/(1 + x^2 + y^2)
523-
aux = 2*d/(1+x^2+y^2)
524-
return SMatrix{2,2}(b*(cos(t)-x^2*sin(t)*aux -x*y*cos(t)*aux), b*(sin(t) +x^2*cos(t)*aux)-x*y*sin(t)*aux,
525-
b*(-sin(t) -x*y*sin(t)*aux -y^2*cos(t)*aux), b*(cos(t) -x*y*cos(t)*aux -y^2*sin(t)*aux))
523+
aux = 2*d/(1+x^2+y^2)
524+
return SMatrix{2,2}(b*(cos(t)-x^2*sin(t)*aux -x*y*cos(t)*aux), b*(sin(t) +x^2*cos(t)*aux)-x*y*sin(t)*aux,
525+
b*(-sin(t) -x*y*sin(t)*aux -y^2*cos(t)*aux), b*(cos(t) -x*y*cos(t)*aux -y^2*sin(t)*aux))
526526
end
527527

528528

test/constructors.jl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
3+
@testset "Discrete Systems Default Constructors" begin
4+
systems = [
5+
:towel,
6+
:standardmap,
7+
:coupledstandardmaps,
8+
:henon,
9+
:logistic,
10+
:pomeau_manneville,
11+
:manneville_simple,
12+
:arnoldcat,
13+
:grebogi_map,
14+
:nld_coupled_logistic_maps,
15+
:tentmap,
16+
:betatransformationmap,
17+
:rulkovmap,
18+
:ikedamap,
19+
:ulam,
20+
]
21+
for system in systems
22+
@test @eval PredefinedDynamicalSystems.$system() isa DeterministicIteratedMap
23+
end
24+
end
25+
26+
@testset "Continous Systems Default Constructors" begin
27+
systems = [
28+
:lorenz,
29+
:chua,
30+
:roessler,
31+
:double_pendulum,
32+
:henonheiles,
33+
:qbh,
34+
:lorenz96,
35+
:duffing,
36+
:shinriki,
37+
:gissinger,
38+
:rikitake,
39+
:nosehoover,
40+
:antidots,
41+
:ueda,
42+
:magnetic_pendulum,
43+
:fitzhugh_nagumo,
44+
:more_chaos_example,
45+
:thomas_cyclical,
46+
:stommel_thermohaline,
47+
:lorenz84,
48+
:lorenzdl,
49+
:coupled_roessler,
50+
:kuramoto,
51+
:sprott_dissipative_conservative,
52+
:hodgkinhuxley,
53+
:vanderpol,
54+
:lotkavolterra,
55+
:hindmarshrose,
56+
:hindmarshrose_two_coupled,
57+
:stuartlandau_oscillator,
58+
:forced_pendulum,
59+
:riddled_basins,
60+
:morris_lecar,
61+
:sakarya,
62+
:lorenz_bounded,
63+
:swinging_atwood,
64+
:guckenheimer_holmes,
65+
:halvorsen,
66+
:multispecies_competition,
67+
:hyper_roessler,
68+
:hyper_lorenz,
69+
:hyper_qi,
70+
:hyper_jha,
71+
:hyper_wang,
72+
:hyper_xu,
73+
:hyper_bao,
74+
:hyper_cai,
75+
:hyper_lu,
76+
:hyper_pang,
77+
]
78+
for system in systems
79+
@test @eval PredefinedDynamicalSystems.$system() isa CoupledODEs
80+
end
81+
end

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ using PredefinedDynamicalSystems
22
using Test
33

44
@test PredefinedDynamicalSystems.henon() isa DeterministicIteratedMap
5+
6+
include("constructors.jl")

0 commit comments

Comments
 (0)