Event Handling

BifurcationKit.jl allows the detection of events along the branch of solutions. Its main use consists in detecting bifurcation points but they can be used and combined together by the user too.

The events are detected during a call to br = continuation(prob, alg, contParams::ContinuationPar;kwargs...) by turning on the following flag(s):

  • contParams.detect_event = 1

The event points are located by looking at the function defining the event (see below). The located event points are then returned in br.specialpoint.

Precise detection of event points using Bisection

Note that the event points detected when detect_event = 1 are only approximate event points. Indeed, we only signal that, in between two continuation steps which can be large, a (several) event point has been detected. Hence, we only have a rough idea of where the event is located, unless your ContinuationPar().dsmax is very small... This can be improved as follows.

If you choose detect_event = 2, a bisection algorithm is used to locate the event points more precisely. It means that we recursively track down the event. Some options in ContinuationPar control this behavior:

  • n_inversion: number of sign inversions in the bisection algorithm
  • max_bisection_steps maximum number of bisection steps
  • tol_param_bisection_event tolerance on parameter to locate event

Different event types

The set of possible events DiscreteEvent, ContinuousEvent, SetOfEvents, PairOfEvents is detailed in the Library.

Built-in events

BifurcationKit.SaveAtEventFunction
`SaveAtEvent(positions::Tuple)`

This event implements the detection of when the parameter values, used during continuation, equals one of the values in positions. This state is then saved in the branch.

For example, you can use it like continuation(args...; event = SaveAtEvent((1., 2., -3.)))

BifurcationKit.FoldDetectEventConstant
`FoldDetectEvent`

This event implements the detection of Fold points based on the p-component of the tangent vector to the continuation curve. It is designed to work with PALC(tangent=Bordered()) as continuation algorithm. To use it, pass event = FoldDetectEvent to continuation.

Examples

We show how to use the different events. We first set up a problem as usual.

using Revise, BifurcationKit, Setfield, ForwardDiff
using Plots
const BK = BifurcationKit
####################################################################################################
# test vector field for event detection
function Feve(X, p)
	p1, p2, k = p
	x, y = X
	out = similar(X)
	out[1] = p1 + x - y - x^k/k
	out[2] = p1 + y + x - 2y^k/k
	out
end

# parameters for the vector field
par = (p1 = -3., p2=-3., k=3)

# bifurcation problem
prob = BifurcationProblem(Feve, -2ones(2), par, (@lens _.p1);
	record_from_solution = (x,p) -> x[1])

# parameters for the continuation
opts = ContinuationPar(dsmax = 0.1, ds = 0.001, max_steps = 128, p_min = -3., p_max = 4.0, plot_every_step = 10,
     newton_options = NewtonPar(tol = 1e-10, verbose = false, max_iterations = 5),
     # parameters specific to event detection
     detect_bifurcation = 0, detect_event = 2, n_inversion = 6, dsmin_bisection = 1e-9,
     max_bisection_steps = 15, detect_fold=false)

# arguments for continuation
args = (prob, PALC(bls = MatrixBLS()), opts)
kwargs = (plot = false, verbosity = 0,)

Example of continuous event

In this first example, we build an event to detect when the parameter value is -2 or when the first component of the solution is 1.

br = continuation(args...; kwargs...,
	event = BK.ContinuousEvent(2,
		(iter, state) -> (getp(state)+2, getx(state)[1]-1)),)
nothing #hide

gives

br
 ┌─ Curve type: EquilibriumCont
 ├─ Number of points: 129
 ├─ Type of vectors: Vector{Float64}
 ├─ Parameter p1 starts at -3.0, ends at 4.0
 ├─ Algo: PALC
 └─ Special points:

If `br` is the name of the branch,
ind_ev = index of the bifurcating eigenvalue e.g. `br.eig[idx].eigenvals[ind_ev]`

- #  1,  userC-1 at p1 ≈ -1.99998635 ∈ (-2.00000275, -1.99998635), |δp|=2e-05, [converged], δ = ( 0,  0), step =  25, eigenelements in eig[ 26], ind_ev =   0
- #  2,  userC-1 at p1 ≈ -2.00000133 ∈ (-2.00000133, -1.99999793), |δp|=3e-06, [   guessL], δ = ( 0,  0), step =  42, eigenelements in eig[ 43], ind_ev =   0
- #  3,  userC-2 at p1 ≈ -2.30843868 ∈ (-2.30845151, -2.30843868), |δp|=1e-05, [converged], δ = ( 0,  0), step =  47, eigenelements in eig[ 48], ind_ev =   0
- #  4,  userC-1 at p1 ≈ -1.99999219 ∈ (-2.00002233, -1.99999219), |δp|=3e-05, [converged], δ = ( 0,  0), step =  50, eigenelements in eig[ 51], ind_ev =   0
- #  5,  userC-2 at p1 ≈ -0.83492016 ∈ (-0.83492251, -0.83492016), |δp|=2e-06, [   guessL], δ = ( 0,  0), step =  62, eigenelements in eig[ 63], ind_ev =   0
- #  6,  userC-2 at p1 ≈ +1.14337663 ∈ (+1.14335658, +1.14337663), |δp|=2e-05, [converged], δ = ( 0,  0), step = 106, eigenelements in eig[107], ind_ev =   0
- #  7, endpoint at p1 ≈ +4.00000000,                                                                     step = 128

This shows for example that the first component of the event was detected userC-1 first. This yields

plot(br)

You can also name the events as follows

 br = continuation(args...; kwargs...,
 	event = BK.ContinuousEvent(2,
 		(iter, state) -> (getp(state)+2, getx(state)[1]-1),
 		("event1", "event2")))
nothing #hide

And get:

br
 ┌─ Curve type: EquilibriumCont
 ├─ Number of points: 129
 ├─ Type of vectors: Vector{Float64}
 ├─ Parameter p1 starts at -3.0, ends at 4.0
 ├─ Algo: PALC
 └─ Special points:

If `br` is the name of the branch,
ind_ev = index of the bifurcating eigenvalue e.g. `br.eig[idx].eigenvals[ind_ev]`

- #  1,   event1 at p1 ≈ -1.99998635 ∈ (-2.00000275, -1.99998635), |δp|=2e-05, [converged], δ = ( 0,  0), step =  25, eigenelements in eig[ 26], ind_ev =   0
- #  2,   event1 at p1 ≈ -2.00000133 ∈ (-2.00000133, -1.99999793), |δp|=3e-06, [   guessL], δ = ( 0,  0), step =  42, eigenelements in eig[ 43], ind_ev =   0
- #  3,   event2 at p1 ≈ -2.30843868 ∈ (-2.30845151, -2.30843868), |δp|=1e-05, [converged], δ = ( 0,  0), step =  47, eigenelements in eig[ 48], ind_ev =   0
- #  4,   event1 at p1 ≈ -1.99999219 ∈ (-2.00002233, -1.99999219), |δp|=3e-05, [converged], δ = ( 0,  0), step =  50, eigenelements in eig[ 51], ind_ev =   0
- #  5,   event2 at p1 ≈ -0.83492016 ∈ (-0.83492251, -0.83492016), |δp|=2e-06, [   guessL], δ = ( 0,  0), step =  62, eigenelements in eig[ 63], ind_ev =   0
- #  6,   event2 at p1 ≈ +1.14337663 ∈ (+1.14335658, +1.14337663), |δp|=2e-05, [converged], δ = ( 0,  0), step = 106, eigenelements in eig[107], ind_ev =   0
- #  7, endpoint at p1 ≈ +4.00000000,                                                                     step = 128
plot(br)

Example of discrete event

You can also use discrete events to detect a change. For example, the following detect when the parameter value equals -2:

br = continuation(args...; kwargs...,
	event = BK.DiscreteEvent(1,
		(iter, state) -> getp(state)>-2))
 ┌─ Curve type: EquilibriumCont
 ├─ Number of points: 128
 ├─ Type of vectors: Vector{Float64}
 ├─ Parameter p1 starts at -3.0, ends at 4.0
 ├─ Algo: PALC
 └─ Special points:

If `br` is the name of the branch,
ind_ev = index of the bifurcating eigenvalue e.g. `br.eig[idx].eigenvals[ind_ev]`

- #  1,    userD at p1 ≈ -1.99998635 ∈ (-2.00000275, -1.99998635), |δp|=2e-05, [converged], δ = ( 0,  0), step =  25, eigenelements in eig[ 26], ind_ev =   0
- #  2,    userD at p1 ≈ -2.00000133 ∈ (-2.00000133, -1.99999793), |δp|=3e-06, [   guessL], δ = ( 0,  0), step =  42, eigenelements in eig[ 43], ind_ev =   0
- #  3,    userD at p1 ≈ -1.99999434 ∈ (-2.00000187, -1.99999434), |δp|=8e-06, [converged], δ = ( 0,  0), step =  49, eigenelements in eig[ 50], ind_ev =   0
- #  4, endpoint at p1 ≈ +4.00000000,                                                                     step = 127

Example of PairOfEvents event

Let us be a bit more creative and combine a continuous event with a discrete one:

br = continuation(args...; kwargs...,
	event = BK.PairOfEvents(
		BK.ContinuousEvent(1, (iter, state) -> getp(state)),
		BK.DiscreteEvent(1, (iter, state) -> getp(state)>-2)))
 ┌─ Curve type: EquilibriumCont
 ├─ Number of points: 128
 ├─ Type of vectors: Vector{Float64}
 ├─ Parameter p1 starts at -3.0, ends at 4.0
 ├─ Algo: PALC
 └─ Special points:

If `br` is the name of the branch,
ind_ev = index of the bifurcating eigenvalue e.g. `br.eig[idx].eigenvals[ind_ev]`

- #  1,  userD-1 at p1 ≈ -1.99998635 ∈ (-2.00000275, -1.99998635), |δp|=2e-05, [converged], δ = ( 0,  0), step =  25, eigenelements in eig[ 26], ind_ev =   0
- #  2,  userD-1 at p1 ≈ -2.00000133 ∈ (-2.00000133, -1.99999793), |δp|=3e-06, [   guessL], δ = ( 0,  0), step =  42, eigenelements in eig[ 43], ind_ev =   0
- #  3,  userD-1 at p1 ≈ -1.99999434 ∈ (-2.00000187, -1.99999434), |δp|=8e-06, [converged], δ = ( 0,  0), step =  49, eigenelements in eig[ 50], ind_ev =   0
- #  4,  userC-1 at p1 ≈ +0.00000326 ∈ (-0.00000027, +0.00000326), |δp|=4e-06, [    guess], δ = ( 0,  0), step =  69, eigenelements in eig[ 70], ind_ev =   0
- #  5, endpoint at p1 ≈ +4.00000000,                                                                     step = 127

Here userD-1 means that the first component of the discrete event was detected. Of course, you can name the event like done above.

Example of set of events

We can combine more events and chain them like we want using SetOfEvents. In this example, we show how to do bifurcation detection and event location altogether:

ev1 = BK.ContinuousEvent(1, (iter, state) -> getp(state)-1)
ev2 = BK.ContinuousEvent(2, (iter, state) -> (getp(state)-2, getp(state)-2.5))
# event to detect bifurcation
ev3 = BK.BifDetectEvent
# we combine the events together
eve = BK.SetOfEvents(ev1, ev2, ev3)

br = continuation(args...; kwargs...,
		event = eve)
 ┌─ Curve type: EquilibriumCont
 ├─ Number of points: 129
 ├─ Type of vectors: Vector{Float64}
 ├─ Parameter p1 starts at -3.0, ends at 3.60609443804766
 ├─ Algo: PALC
 └─ Special points:

If `br` is the name of the branch,
ind_ev = index of the bifurcating eigenvalue e.g. `br.eig[idx].eigenvals[ind_ev]`

- #  1,       bp at p1 ≈ -1.13352254 ∈ (-1.13352254, -1.13351184), |δp|=1e-05, [    guess], δ = ( 1,  0), step =  33, eigenelements in eig[ 34], ind_ev =   1
- #  2,       bp at p1 ≈ -2.32505862 ∈ (-2.32505862, -2.32505849), |δp|=1e-07, [converged], δ = (-1,  0), step =  46, eigenelements in eig[ 47], ind_ev =   1
- #  3,     hopf at p1 ≈ -0.95384426 ∈ (-0.95385423, -0.95384426), |δp|=1e-05, [converged], δ = ( 2,  2), step =  60, eigenelements in eig[ 61], ind_ev =   2
- #  4,     hopf at p1 ≈ +0.95385225 ∈ (+0.95384228, +0.95385225), |δp|=1e-05, [converged], δ = (-2, -2), step =  80, eigenelements in eig[ 81], ind_ev =   2
- #  5,   userC1 at p1 ≈ +1.00000070 ∈ (+0.99999933, +1.00000070), |δp|=1e-06, [    guess], δ = ( 0,  0), step =  82, eigenelements in eig[ 83], ind_ev =   0
- #  6, userC2-1 at p1 ≈ +2.00011806 ∈ (+1.99963580, +2.00011806), |δp|=5e-04, [converged], δ = ( 0,  0), step =  91, eigenelements in eig[ 92], ind_ev =   0
- #  7,       bp at p1 ≈ +2.32505862 ∈ (+2.32505862, +2.32505862), |δp|=2e-10, [    guess], δ = ( 1,  0), step =  95, eigenelements in eig[ 96], ind_ev =   1
- #  8, userC2-1 at p1 ≈ +1.99990023 ∈ (+1.99990023, +2.00011780), |δp|=2e-04, [converged], δ = ( 0,  0), step =  99, eigenelements in eig[100], ind_ev =   0
- #  9,       bp at p1 ≈ +1.13286430 ∈ (+1.13286415, +1.13286430), |δp|=2e-07, [converged], δ = (-1,  0), step = 108, eigenelements in eig[109], ind_ev =   1
- # 10, userC2-1 at p1 ≈ +2.00003159 ∈ (+1.99996599, +2.00003159), |δp|=7e-05, [converged], δ = ( 0,  0), step = 116, eigenelements in eig[117], ind_ev =   0
- # 11, userC2-2 at p1 ≈ +2.50049932 ∈ (+2.49996432, +2.50049932), |δp|=5e-04, [converged], δ = ( 0,  0), step = 120, eigenelements in eig[121], ind_ev =   0
- # 12, endpoint at p1 ≈ +3.74519842,                                                                     step = 129

Which gives

plot(br)