# Evolving a DynamicalSystem until it is close to an attractor

**URL:** https://discourse.julialang.org/t/evolving-a-dynamicalsystem-until-it-is-close-to-an-attractor/121294
**Category:** Modelling & Simulations
**Tags:** dynamical-systems
**Created:** [October 14, 2024, 4:11pm UTC](https://discourse.julialang.org/t/evolving-a-dynamicalsystem-until-it-is-close-to-an-attractor/121294 "2024-10-14T16:11:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![andreasmorr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasmorr/32/212240_2.png) [@andreasmorr](https://discourse.julialang.org/u/andreasmorr)
#### Post date: [October 14, 2024, 4:11pm UTC](https://discourse.julialang.org/t/evolving-a-dynamicalsystem-until-it-is-close-to-an-attractor/121294/1 "2024-10-14T16:11:28Z")

</div>

Hello! I’ve been working with DynamicalSystems.jl and Attractors.jl and I’m wondering if there exists the following functionality:

I would like to obtain a trajectory of a given DynamicalSystem and initial condition, which terminates once it has reached a certain proximity to a given attractor of the system.

A slightly different result which I would be equally satisfied with is this:  
Say I give a DynamicalSystem and a grid to Attractors.AttractorsViaRecurrences and extract the attractors. Then for each grid cell as an initial condition, a trajectory is evolved until certain recurrence conditions are met. How can I get access to all of these internally generated trajectories on the grid? Or how might I adopt the code most efficiently to generate them myself?

Thanks for any hints!

---

<div class="post-metadata">

### Author: ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)
#### Post date: [October 14, 2024, 7:01pm UTC](https://discourse.julialang.org/t/evolving-a-dynamicalsystem-until-it-is-close-to-an-attractor/121294/2 "2024-10-14T19:01:30Z")

</div>

Hi @andreasmorr , welcome.

> [@andreasmorr](#):
>
> I would like to obtain a trajectory of a given DynamicalSystem and initial condition, which terminates once it has reached a certain proximity to a given attractor of the system.

To me this sounds like it is most fitting for the [AttractorsViaProximity](https://juliadynamics.github.io/DynamicalSystemsDocs.jl/attractors/stable/api/#Attractors.AttractorsViaProximity) (which also recently got a good update).

I would use the function [`convergence_time`](https://juliadynamics.github.io/DynamicalSystemsDocs.jl/attractors/stable/api/#Attractors.convergence_time) like so:

```julia
mapper = AttractorsViaProximity(ds, attractors, ε)
u0 = whatever
id = mapper(u0)
t = convergence_time(mapper)

```

In this scenario `t` would be the time until the trajectory “terminates”, which would be when it comes `ε` close to any of the attractors. So you could also obtain the full trajectory with `X, tvec = trajectory(ds, t, u0)`.

* * *

> [@andreasmorr](#):
>
> Then for each grid cell as an initial condition, a trajectory is evolved until certain recurrence conditions are met. How can I get access to all of these internally generated trajectories on the grid?

It is not possible to use the same recurrences mapper and reach a different recurrence condition for different initial conditions. You would need a dedicated mapper for each different recurrence threshold.

For the second half of your question, internally in the source code of the recurrences mapper the recurrences are counted and that’s easy to find in the source code. As a user it isn’t easy to access it… However, we do access this information in this example animation script, that uses the internals of the recurrences mapper:

> **[Animation illustrating AttractorsViaRecurrences · Attractors.jl](https://juliadynamics.github.io/DynamicalSystemsDocs.jl/attractors/stable/recurrences_animation/#recurrences_animation)**
>
> Documentation for Attractors.jl.

the code in this animation script counts the recurrences explicitly for each initial condition. You can adjust it to your needs I guess?

---

<div class="post-metadata">

### Author: ![andreasmorr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasmorr/32/212240_2.png) [@andreasmorr](https://discourse.julialang.org/u/andreasmorr)
#### Post date: [October 15, 2024, 11:16am UTC](https://discourse.julialang.org/t/evolving-a-dynamicalsystem-until-it-is-close-to-an-attractor/121294/3 "2024-10-15T11:16:34Z")

</div>

Hey @Datseris, thanks for your quick response! Running `trajectory` up to the `convergence_time` corresponding to the respective initial condition is a very elegant solution. Works perfect for me!
