|
1 | 1 | using BifurcationKit, ModelingToolkit, Test
|
2 | 2 |
|
3 |
| -# Checks pitchfork diagram and that there are the correct number of branches (a main one and two children) |
| 3 | +# Simple pitchfork diagram, compares solution to native BifurcationKit, checks they are identical. |
| 4 | +# Checks using `jac=false` option. |
4 | 5 | let
|
| 6 | + # Creates model. |
5 | 7 | @variables t x(t) y(t)
|
6 | 8 | @parameters μ α
|
7 | 9 | eqs = [0 ~ μ * x - x^3 + α * y,
|
8 | 10 | 0 ~ -y]
|
9 | 11 | @named nsys = NonlinearSystem(eqs, [x, y], [μ, α])
|
10 | 12 |
|
| 13 | + # Creates BifurcationProblem |
11 | 14 | bif_par = μ
|
12 | 15 | p_start = [μ => -1.0, α => 1.0]
|
13 | 16 | u0_guess = [x => 1.0, y => 1.0]
|
14 | 17 | plot_var = x
|
15 |
| - |
16 |
| - using BifurcationKit |
17 | 18 | bprob = BifurcationProblem(nsys,
|
18 | 19 | u0_guess,
|
19 | 20 | p_start,
|
20 | 21 | bif_par;
|
21 | 22 | plot_var = plot_var,
|
22 | 23 | jac = false)
|
23 | 24 |
|
| 25 | + # Conputes bifurcation diagram. |
24 | 26 | p_span = (-4.0, 6.0)
|
| 27 | + opts_br = ContinuationPar(max_steps = 500, p_min = p_span[1], p_max = p_span[2]) |
| 28 | + bif_dia = bifurcationdiagram(bprob, PALC(), 2, (args...) -> opts_br; bothside = true) |
| 29 | + |
| 30 | + # Computes bifurcation diagram using BifurcationKit directly (without going through MTK). |
| 31 | + function f_BK(u, p) |
| 32 | + x, y = u |
| 33 | + μ, α = p |
| 34 | + return [μ * x - x^3 + α * y, -y] |
| 35 | + end |
| 36 | + bprob_BK = BifurcationProblem(f_BK, |
| 37 | + [1.0, 1.0], |
| 38 | + [-1.0, 1.0], |
| 39 | + (@lens _[1]); |
| 40 | + record_from_solution = (x, p) -> x[1]) |
| 41 | + bif_dia_BK = bifurcationdiagram(bprob_BK, |
| 42 | + PALC(), |
| 43 | + 2, |
| 44 | + (args...) -> opts_br; |
| 45 | + bothside = true) |
| 46 | + |
| 47 | + # Compares results. |
| 48 | + @test getfield.(bif_dia.γ.branch, :x) ≈ getfield.(bif_dia_BK.γ.branch, :x) |
| 49 | + @test getfield.(bif_dia.γ.branch, :param) ≈ getfield.(bif_dia_BK.γ.branch, :param) |
| 50 | + @test bif_dia.γ.specialpoint[1].x == bif_dia_BK.γ.specialpoint[1].x |
| 51 | + @test bif_dia.γ.specialpoint[1].param == bif_dia_BK.γ.specialpoint[1].param |
| 52 | + @test bif_dia.γ.specialpoint[1].type == bif_dia_BK.γ.specialpoint[1].type |
| 53 | +end |
| 54 | + |
| 55 | +# Lotka–Volterra model, checks exact position of bifurcation variable and bifurcation points. |
| 56 | +# Checks using ODESystem input. |
| 57 | +let |
| 58 | + # Creates a Lotka–Volterra model. |
| 59 | + @parameters α a b |
| 60 | + @variables t x(t) y(t) z(t) |
| 61 | + D = Differential(t) |
| 62 | + eqs = [D(x) ~ -x + a * y + x^2 * y, |
| 63 | + D(y) ~ b - a * y - x^2 * y] |
| 64 | + @named sys = ODESystem(eqs) |
| 65 | + |
| 66 | + # Creates BifurcationProblem |
| 67 | + bprob = BifurcationProblem(sys, |
| 68 | + [x => 1.5, y => 1.0], |
| 69 | + [a => 0.1, b => 0.5], |
| 70 | + b; |
| 71 | + plot_var = x) |
| 72 | + |
| 73 | + # Computes bifurcation diagram. |
| 74 | + p_span = (0.0, 2.0) |
| 75 | + opt_newton = NewtonPar(tol = 1e-9, max_iterations = 2000) |
| 76 | + opts_br = ContinuationPar(dsmax = 0.05, |
| 77 | + max_steps = 500, |
| 78 | + newton_options = opt_newton, |
| 79 | + p_min = p_span[1], |
| 80 | + p_max = p_span[2], |
| 81 | + n_inversion = 4) |
| 82 | + bif_dia = bifurcationdiagram(bprob, PALC(), 2, (args...) -> opts_br; bothside = true) |
| 83 | + |
| 84 | + # Tests that the diagram has the correct values (x = b) |
| 85 | + all([b.x ≈ b.param for b in bif_dia.γ.branch]) |
| 86 | + |
| 87 | + # Tests that we get two Hopf bifurcations at the correct positions. |
| 88 | + hopf_points = sort(getfield.(filter(sp -> sp.type == :hopf, bif_dia.γ.specialpoint), |
| 89 | + :x); |
| 90 | + by = x -> x[1]) |
| 91 | + @test length(hopf_points) == 2 |
| 92 | + @test hopf_points[1] ≈ [0.41998733080424205, 1.5195495712453098] |
| 93 | + @test hopf_points[2] ≈ [0.7899715592573977, 1.0910379583813192] |
| 94 | +end |
| 95 | + |
| 96 | +# Simple fold bifurcation model, checks exact position of bifurcation variable and bifurcation points. |
| 97 | +# Checks that default parameter values are accounted for. |
| 98 | +# Checks that observables (that depend on other observables, as in this case) are accounted for. |
| 99 | +let |
| 100 | + # Creates model, and uses `structural_simplify` to generate observables. |
| 101 | + @parameters μ p=2 |
| 102 | + @variables t x(t) y(t) z(t) |
| 103 | + D = Differential(t) |
| 104 | + eqs = [0 ~ μ - x^3 + 2x^2, |
| 105 | + 0 ~ p * μ - y, |
| 106 | + 0 ~ y - z] |
| 107 | + @named nsys = NonlinearSystem(eqs, [x, y, z], [μ, p]) |
| 108 | + nsys = structural_simplify(nsys) |
| 109 | + |
| 110 | + # Creates BifurcationProblem. |
| 111 | + bif_par = μ |
| 112 | + p_start = [μ => 1.0] |
| 113 | + u0_guess = [x => 1.0, y => 0.1, z => 0.1] |
| 114 | + plot_var = x |
| 115 | + bprob = BifurcationProblem(nsys, u0_guess, p_start, bif_par; plot_var = plot_var) |
| 116 | + |
| 117 | + # Computes bifurcation diagram. |
| 118 | + p_span = (-4.3, 12.0) |
25 | 119 | opt_newton = NewtonPar(tol = 1e-9, max_iterations = 20)
|
26 |
| - opts_br = ContinuationPar(dsmin = 0.001, dsmax = 0.05, ds = 0.01, |
27 |
| - max_steps = 100, nev = 2, newton_options = opt_newton, |
28 |
| - p_min = p_span[1], p_max = p_span[2], |
29 |
| - detect_bifurcation = 3, n_inversion = 4, tol_bisection_eigenvalue = 1e-8, |
30 |
| - dsmin_bisection = 1e-9) |
| 120 | + opts_br = ContinuationPar(dsmax = 0.05, |
| 121 | + max_steps = 500, |
| 122 | + newton_options = opt_newton, |
| 123 | + p_min = p_span[1], |
| 124 | + p_max = p_span[2], |
| 125 | + n_inversion = 4) |
| 126 | + bif_dia = bifurcationdiagram(bprob, PALC(), 2, (args...) -> opts_br; bothside = true) |
31 | 127 |
|
32 |
| - bf = bifurcationdiagram(bprob, PALC(), 2, (args...) -> opts_br; bothside = true) |
| 128 | + # Tests that the diagram has the correct values (x = b) |
| 129 | + all([b.x ≈ 2 * b.param for b in bif_dia.γ.branch]) |
33 | 130 |
|
34 |
| - @test length(bf.child) == 2 |
| 131 | + # Tests that we get two fold bifurcations at the correct positions. |
| 132 | + fold_points = sort(getfield.(filter(sp -> sp.type == :bp, bif_dia.γ.specialpoint), |
| 133 | + :param)) |
| 134 | + @test length(fold_points) == 2 |
| 135 | + @test fold_points ≈ [-1.1851851706940317, -5.6734983580551894e-6] # test that they occur at the correct parameter values). |
35 | 136 | end
|
0 commit comments