Skip to content

Commit

Permalink
correct mistake in cache for BVP Collocation
Browse files Browse the repository at this point in the history
increase compats
  • Loading branch information
rveltz committed Jul 8, 2024
1 parent 44979ea commit f6f3f06
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 87 deletions.
10 changes: 5 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"

[compat]
BifurcationKit = "0.3"
BifurcationKit = "0.3.6"
DocStringExtensions = "0.9"
ForwardDiff = "^0.10"
Parameters = "0.12"
RecursiveArrayTools = "2.38"
SciMLBase = "1.98"
Parameters = "^0.12"
RecursiveArrayTools = "^2.3, ^2.4, ^2.8, ^2.9, ^3"
SciMLBase = "^2"
Setfield = "1"
julia = "1"
julia = "1.5"

[extras]
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Assuming that you already have Julia correctly installed, it suffices to import
`HclinicBifurcationKit.jl` in the standard way:

```julia
import Pkg; Pkg.add("https://github.com/bifurcationkit/HclinicBifurcationKit.jl")
] add HclinicBifurcationKit
```

## Support and citation
Expand Down
48 changes: 25 additions & 23 deletions docs/src/tutorials/ode/OPL.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ It is easy to encode the ODE as follows

```@example TUTOPL
using Revise, Plots
using Parameters, Setfield, LinearAlgebra, Test, ForwardDiff
using Setfield, LinearAlgebra, Test, ForwardDiff
using BifurcationKit, Test
using HclinicBifurcationKit
const BK = BifurcationKit
recordFromSolution(x, p) = (D₂₃ = x[6], β = x[1],)
####################################################################################################
function OPL!(dz, u, p, t)
@unpack b, σ, g, a, D₂₁⁰, D₂₃⁰ = p
(;b, σ, g, a, D₂₁⁰, D₂₃⁰) = p
β, p₂₁, p₂₃, p₃₁, D₂₁, D₂₃ = u
dz[1] = -σ * β + g * p₂₃
dz[2] = -p₂₁ - β * p₃₁ + a * D₂₁
Expand Down Expand Up @@ -140,31 +140,33 @@ br_coll = continuation(
true
end,
normC = norminf)
_sol = get_periodic_orbit(br_coll, length(br_coll))
BK.plot(_sol.t, _sol.u'; marker = :d, markersize = 1,title = "Last periodic orbit on branch")
```

```@example TUTOPL
probhom, solh = generate_hom_problem(
setproperties(br_coll.prob.prob, meshadapt=true, K = 100),
br_coll.sol[end].x,
BK.setparam(br_coll, br_coll.sol[end].p),
BK.getlens(br_coll);
update_every_step = 4,
verbose = true,
# ϵ0 = 1e-7, ϵ1 = 1e-7, # WORK BEST
# ϵ0 = 1e-8, ϵ1 = 1e-5, # maxT = 70,
t0 = 0., t1 = 120.,
# freeparams = ((@lens _.T), (@lens _.ϵ1),)
# freeparams = ((@lens _.T), (@lens _.ϵ0)),
freeparams = ((@lens _.ϵ0), (@lens _.ϵ1)), # WORK BEST
# freeparams = ((@lens _.T),),
testOrbitFlip = false,
testInclinationFlip = false
)
setproperties(br_coll.prob.prob, meshadapt=true, K = 100),
br_coll.sol[end].x.sol,
BK.setparam(br_coll, br_coll.sol[end].p),
BK.getlens(br_coll);
update_every_step = 4,
verbose = true,
# ϵ0 = 1e-7, ϵ1 = 1e-7, # WORK BEST
# ϵ0 = 1e-8, ϵ1 = 1e-5, # maxT = 70,
t0 = 0., t1 = 120.,
# freeparams = ((@lens _.T), (@lens _.ϵ1),)
# freeparams = ((@lens _.T), (@lens _.ϵ0)),
freeparams = ((@lens _.ϵ0), (@lens _.ϵ1)), # WORK BEST
# freeparams = ((@lens _.T),),
testOrbitFlip = false,
testInclinationFlip = false
)
#####
_sol = get_homoclinic_orbit(probhom, solh, BK.getparams(probhom);)
scene = plot(_sol.t, _sol[:,:]', marker = :d, markersize = 1)
BK.plot(_sol.t, _sol.u'; marker = :d, markersize = 1,title = "Guess for homoclinic orbit")
```

```@example TUTOPL
Expand Down Expand Up @@ -253,7 +255,7 @@ probhom, solh = generate_hom_problem(
)
_sol = get_homoclinic_orbit(probhom, solh, BK.getparams(probhom); saveat=.1)
plot(plot(_sol[1,:], _sol[2,:]), plot(_sol.t, _sol[:,:]'))
plot(plot(_sol[1,:], _sol[2,:]), plot(_sol.t, _sol[1:4,:]'))
optn_hom = NewtonPar(verbose = true, tol = 1e-9, max_iterations = 7)
optc_hom = ContinuationPar(newton_options = optn_hom, ds = -1e-4, dsmin = 1e-6, dsmax = 1e-3, plot_every_step = 1, max_steps = 10, detect_bifurcation = 0, save_sol_every_step = 1)
Expand All @@ -271,12 +273,12 @@ br_hom_sh = continuation(
par0 = set(par0, (@lens _.b), p.p)
sol = get_homoclinic_orbit(𝐇𝐨𝐦, x, par0)
m = (𝐇𝐨𝐦.bvp isa PeriodicOrbitOCollProblem && 𝐇𝐨𝐦.bvp.meshadapt) ? :d : :none
plot!(sol.t, sol[:,:]',subplot=3, markersize = 1, marker=m)
plot!(sol.t, sol[1:6,:]',subplot=3, markersize = 1, marker=m)
end,
)
_sol = get_homoclinic_orbit(probhom, br_hom_sh.sol[end].x, BK.setparam(br_hom_sh, br_hom_sh.sol[end].p); saveat=.1)
plot(_sol.t, _sol[:,:]')
plot(_sol.t, _sol[1:6,:]')
```

```@example TUTOPL
Expand Down
6 changes: 3 additions & 3 deletions docs/src/tutorials/ode/tutorialsFreire.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ It is easy to encode the ODE as follows

```@example TUTFREIRE
using Revise, Plots
using Parameters, Setfield, LinearAlgebra, Test, ForwardDiff
using Setfield, LinearAlgebra, Test, ForwardDiff
using BifurcationKit, Test
using HclinicBifurcationKit
const BK = BifurcationKit
recordFromSolution(x, p) = (x = x[1], y = x[2])
function freire!(dz, u, p, t)
@unpack ν, β, A₃, B₃, r, ϵ = p
(;ν, β, A₃, B₃, r, ϵ) = p
x, y, z = u
dz[1] = (-ν*x + β*(y-x) - A₃*x^3 + B₃*(y-x)^3 + ϵ)/r
dz[2] = -β*(y-x) - z - B₃*(y-x)^3
Expand Down Expand Up @@ -92,7 +92,7 @@ function plotHom(x,p;k...)
par0 = set(par0, p.lens, p.p)
sol = get_homoclinic_orbit(𝐇𝐨𝐦, x, par0)
m = (𝐇𝐨𝐦.bvp isa PeriodicOrbitOCollProblem && 𝐇𝐨𝐦.bvp.meshadapt) ? :d : :none
plot!(sol.t, sol[:,:]',subplot=3, markersize = 1, marker=m)
plot!(sol.t, sol[1:3,:]',subplot=3, markersize = 1, marker=m)
end
```

Expand Down
37 changes: 18 additions & 19 deletions examples/OPL.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Revise, Plots
using Parameters, Setfield, LinearAlgebra, Test, ForwardDiff
using LinearAlgebra, Test, ForwardDiff
using BifurcationKit, Test
using HclinicBifurcationKit
const BK = BifurcationKit

recordFromSolution(x, p) = (D₂₃ = x[6], β = x[1],)
####################################################################################################
function OPL!(dz, u, p, t)
@unpack b, σ, g, a, D₂₁⁰, D₂₃⁰ = p
(;b, σ, g, a, D₂₁⁰, D₂₃⁰) = p
β, p₂₁, p₂₃, p₃₁, D₂₁, D₂₃ = u
dz[1] = -σ * β + g * p₂₃
dz[2] = -p₂₁ - β * p₃₁ + a * D₂₁
Expand All @@ -24,13 +24,13 @@ z0 = zeros(6)
prob = BK.BifurcationProblem(OPL, z0, par_OPL, (@lens _.a); record_from_solution = recordFromSolution)

opts_br = ContinuationPar(p_min = -1., p_max = 8., ds = 0.001, dsmax = 0.06, n_inversion = 6, detect_bifurcation = 3, max_bisection_steps = 25, nev = 6, plot_every_step = 20, max_steps = 100, save_sol_every_step = 1, detect_fold = true)
opts_br = @set opts_br.newton_options.verbose = false
br = continuation(prob, PALC(tangent = Secant()), opts_br;
br = continuation(prob, PALC(tangent = Secant()), opts_br;
bothside = false, normC = norminf)

plot(br, plotfold=true)

br2 = continuation(re_make(prob; u0 = [1.6931472491037485, -0.17634826359471437, 0.06772588996414994, -0.23085768742546342, -0.5672243219935907, -0.09634826359471438]), PALC(tangent = Secant()), opts_br; verbosity = 0, bothside = false, normC = norminf)
br2 = continuation(br, 1)

plot(br, br2)
####################################################################################################
sn_br = continuation(br, 1, (@lens _.b), ContinuationPar(opts_br, detect_bifurcation = 1, save_sol_every_step = 1, max_steps = 80) ;
Expand Down Expand Up @@ -90,7 +90,7 @@ br_coll = continuation(
# br, 2,
br2, 1,
opts_po_cont,
PeriodicOrbitOCollProblem(20, 4; meshadapt = true, update_section_every_step = 2);
PeriodicOrbitOCollProblem(40, 4; meshadapt = true, update_section_every_step = 2);
ampfactor = 1., δp = 0.0015,
verbosity = 2, plot = true,
alg = PALC(tangent = Bordered()),
Expand All @@ -108,14 +108,13 @@ br_coll = continuation(
end,
normC = norminf)

_sol = get_periodic_orbit(br_coll.prob.prob, br_coll.sol[end].x, nothing)
plot(_sol.t, _sol[:,:]', marker = :d, markersize = 1)

_sol = get_periodic_orbit(br_coll, length(br_coll))
BK.plot(_sol.t, _sol.u'; marker = :d, markersize = 1, title = "Last periodic orbit on branch")
####################################################################################################
# homoclinic
probhom, solh = generate_hom_problem(
setproperties(br_coll.prob.prob, meshadapt=true, K = 100),
br_coll.sol[end].x,
br_coll.sol[end].x.sol,
BK.setparam(br_coll, br_coll.sol[end].p),
BK.getlens(br_coll);
update_every_step = 4,
Expand All @@ -134,7 +133,7 @@ probhom, solh = generate_hom_problem(
#####

_sol = get_homoclinic_orbit(probhom, solh, BK.getparams(probhom);)
plot(_sol.t, _sol[:,:]', marker = :d, markersize = 1)
plot(_sol.t, _sol[:,:]', marker = :d, markersize = 1, title = "Initial guess for homoclinic orbit")

optn_hom = NewtonPar(verbose = true, tol = 1e-10, max_iterations = 5)
optc_hom = ContinuationPar(newton_options = optn_hom, ds = -0.0001, dsmin = 1e-5, plot_every_step = 10, max_steps = 100, detect_bifurcation = 0, detect_event = 2, save_sol_every_step = 1, p_min = -1.01)
Expand All @@ -159,8 +158,8 @@ br_hom_c = continuation(
end,
)

using PrettyTables
br_hom_c.branch |> pretty_table

br_hom_c.branch |> vscodedisplay

plot(sn_br, vars = (:a, :b), branchlabel = "SN", )
plot!(hopf_br, branchlabel = "AH₀", vars = (:a, :b))
Expand All @@ -169,7 +168,7 @@ plot!(br_hom_c, branchlabel = "H₀", vars = (:a, :b))
ylims!(0,1.5)
####################################################################################################
# same with shooting
using DifferentialEquations
using OrdinaryDiffEq
probsh = ODEProblem(OPL!, copy(z0), (0., 1000.), par_OPL; abstol = 1e-12, reltol = 1e-10)

# newton parameters
Expand Down Expand Up @@ -200,8 +199,8 @@ br_sh = continuation(
end,
normC = norminf)

_sol = get_periodic_orbit(br_sh.prob.prob, br_sh.sol[end].x, br_sh.sol[end].p)
plot(_sol.t, _sol[:,:]')
_sol = get_periodic_orbit(br_sh, length(br_sh))
plot(_sol)

#######################################
# homoclinic
Expand All @@ -220,7 +219,7 @@ probhom, solh = generate_hom_problem(
)

_sol = get_homoclinic_orbit(probhom, solh, BK.getparams(probhom); saveat=.1)
plot(plot(_sol[1,:], _sol[2,:]), plot(_sol.t, _sol[:,:]'))
plot(plot(_sol[1,:], _sol[2,:]), plot(_sol.t, _sol[1:4,:]'))

optn_hom = NewtonPar(verbose = true, tol = 1e-9, max_iterations = 7)
optc_hom = ContinuationPar(newton_options = optn_hom, ds = -1e-4, dsmin = 1e-6, dsmax = 1e-3, plot_every_step = 1, max_steps = 10, detect_bifurcation = 0, save_sol_every_step = 1)
Expand All @@ -241,12 +240,12 @@ br_hom_sh = continuation(
par0 = set(par0, (@lens _.b), p.p)
sol = get_homoclinic_orbit(𝐇𝐨𝐦, x, par0)
m = (𝐇𝐨𝐦.bvp isa PeriodicOrbitOCollProblem && 𝐇𝐨𝐦.bvp.meshadapt) ? :d : :none
plot!(sol.t, sol[:,:]',subplot=3, markersize = 1, marker=m)
plot!(sol.t, sol[1:6,:]',subplot=3, markersize = 1, marker=m)
end,
)

_sol = get_homoclinic_orbit(probhom, br_hom_sh.sol[end].x, BK.setparam(br_hom_sh, br_hom_sh.sol[end].p); saveat=.1)
plot(_sol.t, _sol[:,:]')
plot(_sol.t, _sol[1:6,:]')


plot(sn_br, vars = (:a, :b), branchlabel = "SN", )
Expand Down
Loading

0 comments on commit f6f3f06

Please sign in to comment.