Skip to content

Commit 8335980

Browse files
authored
Add contribution of multi/nonlocal stability by Attractors.jl
We have discussed this on Slack some time ago!
1 parent 44dff8e commit 8335980

File tree

1 file changed

+218
-0
lines changed

1 file changed

+218
-0
lines changed

docs/src/tutorials/attractors.md

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
# [Multi- and Nonlocal- Continuation](@id attractors)
2+
3+
In the tutorial on [Bifurcation Diagrams](@ref bifurcation_diagrams) we saw how one can create them by integrating ModelingToolkit.jl with BifurcationKit.jl.
4+
This approach is also often called _continuation_ in the broader literature,
5+
because in essence we are "continuing" the location of individual un/stable fixed points or limit cycles in a dynamical system across a parameter axis.
6+
7+
Recently, an alternative continuation framework was proposed that takes a fundamentally different approach to continuation that is particularly suitable for complex systems. This framework is implemented in [Attractors.jl](https://juliadynamics.github.io/DynamicalSystemsDocs.jl/attractors/stable/) as part of the DynamicalSystems.jl software library.
8+
This new continuation is called _global_ continuation, while the one of BifurcationKit.jl is called _local_ continuation.
9+
10+
Instead of continuing an individual fixed point or limit cycle, the global continuation finds all attractors of the dynamical system and continues all of them, in parallel, in a single continuation. It distinguishes and labels automatically the different attractors.
11+
Hence "multi-" for multiple attractors.
12+
Another key difference is that instead of estimating the local (or linear, or Jacobian) stability of the attractors, it estimates various measures of _nonlocal_ stability (e.g, related with the size of the basins of attraction, or the size of a perturbation that would make the dynamical system state converge to an alternative attractor).
13+
Hence the "nonlocal-" component.
14+
More differences and pros & cons are discussed in the documentation of Attractors.jl.
15+
16+
!!! note "Attractors and basins"
17+
This tutorial assumes that you have some familiarity with dynamical systems,
18+
specifically what are attractors and basins of attraction. If you don't have
19+
this yet, we recommend Chapter 1 of the textbook
20+
[Nonlinear Dynamics](https://link.springer.com/book/10.1007/978-3-030-91032-7).
21+
22+
## Creating the `DynamicalSystem` via MTK
23+
24+
Let's showcase this framework by modelling a chaotic bistable dynamical system that we define via ModelingToolkit.jl, which will the be casted into a `DynamicalSystem` type for the DynamicalSystems.jl library. The equations of our system are
25+
26+
```@example Attractors
27+
using ModelingToolkit
28+
using ModelingToolkit: t_nounits as t, D_nounits as D
29+
30+
@variables x(t) = -4.0 y(t) = 5.0 z(t) = 0.0
31+
@parameters a = 5.0 b = 0.1
32+
33+
eqs = [
34+
D(x) ~ y - x,
35+
D(y) ~ - x*z + b*abs(z),
36+
D(z) ~ x*y - a,
37+
]
38+
```
39+
40+
Because our dynamical system is super simple, we will directly make an `ODESystem` and cast it in an `ODEProblem` as in the [`ODESystems` tutorial](@ref programmatically). Since all state variables and parameters have a default value we can immediately write
41+
42+
```@example Attractors
43+
@named modlorenz = ODESystem(eqs, t)
44+
ssys = structural_simplify(modlorenz)
45+
# The timespan given to the problem is irrelevant for DynamicalSystems.jl
46+
prob = ODEProblem(ssys, [], (0.0, 1.0), [])
47+
```
48+
49+
This `prob` can be turned to a dynamical system as simply as
50+
51+
```@example Attractors
52+
using Attractors # or `DynamicalSystems`
53+
ds = CoupledODEs(prob)
54+
```
55+
56+
DynamicalSystems.jl integrates fully with ModelingToolkit.jl out of the box and understands whether a given problem has been created via ModelingToolkit.jl. For example you can use the symbolic variables, or their `Symbol` representations, to access a system state or parameter
57+
58+
```@example Attractors
59+
observe_state(ds, x)
60+
```
61+
62+
```@example Attractors
63+
current_parameter(ds, :a) # or `a` directly
64+
```
65+
66+
## Finding all attractors in the state space
67+
68+
Attractors.jl provides an extensive interface for finding all (within a state space region and numerical accuracy) attractors of a dynamical system.
69+
This interface is structured around the type `AttractorMapper` and is discussed in the Attractors.jl documentation in detail. Here we will briefly mention one of the possible approaches, the recurrences-based algorithm. It finds attractors by finding locations in the state space where the trajectory returns again and again.
70+
71+
To use this technique, we first need to create a tessellation of the state space
72+
73+
```@example Attractors
74+
grid = (
75+
range(-15.0, 15.0; length = 150), # x
76+
range(-20.0, 20.0; length = 150), # y
77+
range(-20.0, 20.0; length = 150), # z
78+
)
79+
```
80+
81+
which we then give as input to the `AttractorsViaRecurrences` mapper along with the dynamical system
82+
83+
```@example Attractors
84+
mapper = AttractorsViaRecurrences(ds, grid;
85+
consecutive_recurrences = 1000,
86+
consecutive_lost_steps = 100,
87+
)
88+
```
89+
to learn about the metaparameters of the algorithm visit the documentation of Attractors.jl.
90+
91+
This `mapper` object is incredibly powerful! It can be used to map initial conditions to attractor they converge to, while ensuring that initial conditions that converge to the same attractor are given the same label.
92+
For example, if we use the `mapper` as a function and give it an initial condition we get
93+
94+
```@example Attractors
95+
mapper([-4.0, 5, 0])
96+
```
97+
98+
```@example Attractors
99+
mapper([4.0, 2, 0])
100+
```
101+
102+
```@example Attractors
103+
mapper([1.0, 3, 2])
104+
```
105+
106+
The numbers returned are simply the unique identifiers of the attractors the initial conditions converged to.
107+
108+
DynamicalSystems.jl library is the only dynamical systems software (in any language) that provides such an infrastructure for mapping initial conditions of any arbitrary dynamical system to its unique attractors. And this is only the tip of this iceberg! The rest of the functionality of Attractors.jl is all full of brand new cutting edge progress in dynamical systems research.
109+
110+
The found attractors are stored in the mapper internally, to obtain them we
111+
use the function
112+
113+
```@example Attractors
114+
attractors = extract_attractors(mapper)
115+
```
116+
117+
This is a dictionary that maps attractor IDs to the attractor sets themselves.
118+
`StateSpaceSet` is a wrapper of a vector of points and behaves exactly like a vector of points. We can plot them easily like
119+
120+
```@example Attractors
121+
using CairoMakie
122+
fig = Figure()
123+
ax = Axis(fig[1,1])
124+
colors = ["#7143E0", "#191E44"]
125+
for (id, A) in attractors
126+
scatter!(ax, A[:, [1, 3]]; color = colors[id])
127+
end
128+
fig
129+
```
130+
131+
## Basins of attraction
132+
133+
Estimating the basins of attraction of these attractor is a matter of a couple lines of code. First we define the state space are to estimate the basins for. Here we can re-use the `grid` we defined above. Then we only have to call
134+
135+
```julia
136+
basins = basins_of_attraction(mapper, grid)
137+
```
138+
139+
We won't run this in this tutorial because it is a length computation (150×150×150).
140+
We will however estimate a slice of the 3D basins of attraction.
141+
DynamicalSystems.jl allows for a rather straightforward setting of initial conditions:
142+
```@example Attractors
143+
ics = [Dict(:x => x, :y => 0, :z=>z) for x in grid[1] for z in grid[3]]
144+
```
145+
146+
now we can estimate the basins of attraction on a slice on the x-z grid
147+
148+
```@example Attractors
149+
fs, labels = basins_fractions(mapper, ics)
150+
labels = reshape(labels, (length(grid[1]), length(grid[3])))
151+
```
152+
153+
and visualize them
154+
155+
```@example Attractors
156+
heatmap(grid[1], grid[3], labels; colormap = colors)
157+
```
158+
159+
## Global continuation
160+
161+
We've already outlined the principles of the global continuation, so let's just do it here!
162+
We first have to define a global continuation algorithm, which for this tutorial,
163+
it is just a wrapper of the existing `mapper`
164+
165+
```@example Attractors
166+
ascm = AttractorSeedContinueMatch(mapper);
167+
```
168+
169+
we need two more ingredients to perform the global continuation.
170+
One is a sampler of initial conditions in the state space.
171+
Here we'll uniformly sample initial conditions within this grid we have already defined
172+
173+
```@example Attractors
174+
sampler, = statespace_sampler(grid);
175+
```
176+
177+
the last ingredient is what parameter(s) to perform the continuation over.
178+
In contrast to local continuation, where we can only specify a parameter range, in global continuation one can specify an exact parameter curve to continue over.
179+
This curve can span any-dimensional parameter space, in contrast to the 1D or 2D parameter spaces supported in local continuation.
180+
Here we will use the curve
181+
182+
```@example Attractors
183+
params(θ) = [:a => 5 + 0.5cos(θ), :b => 0.1 + 0.01sin(θ)]
184+
θs = range(0, 2π; length = 101)
185+
pcurve = params.(θs)
186+
```
187+
which makes an ellipsis over the parameter space.
188+
189+
We put these three ingredients together to call the global continuation
190+
191+
```@example Attractors
192+
fractions_cont, attractors_cont = global_continuation(ascm, pcurve, sampler);
193+
```
194+
195+
The output of the continuation is how the attractors and their basins fractions change over this parameter curve. We can visualize this directly using a convenience function
196+
197+
```@example Attractors
198+
fig = plot_basins_attractors_curves(
199+
fractions_cont, attractors_cont, A -> minimum(A[:, 1]), θs,
200+
)
201+
```
202+
203+
The top panel shows the relative basins of attractions of the attractors and the bottom panel shows their minimum x-position. The colors correspond to unique attractors. Perhaps making a video is easier to understand:
204+
205+
```@example Attractors
206+
animate_attractors_continuation(
207+
ds, attractors_cont, fractions_cont, pcurve;
208+
savename = "curvecont.mp4"
209+
);
210+
```
211+
212+
```@raw html
213+
<video width="auto" controls loop>
214+
<source src="../curvecont.mp4" type="video/mp4">
215+
</video>
216+
```
217+
218+
To learn more about this global continuation and its various options, and more details about how it compares with local continuation, visit the documentation of [Attractors.jl](https://juliadynamics.github.io/DynamicalSystemsDocs.jl/attractors/stable/).

0 commit comments

Comments
 (0)