# LDDS

## Tutorial 4: Lagrangian descriptors for a user-defined 1 DoF dynamical system

In this notebook we will show how to calculate Lagrangian descriptors for a 1 DoF dynamical system defined by the user.

We begin by importing the LDDS module, and from it, all the functions necessary to calculate and plot LDs.


```python
import os, sys
sys.path.insert(1, os.pardir)
import ldds
from ldds.base import compute_lagrangian_descriptor
from ldds.tools import draw_all_lds
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
```

#### The Morse oscillator

The Hamiltonian function that describes the Morse oscillator is:

\begin{equation*}
 H(x,p_x) = \dfrac{1}{2m}p_x^2 + D\left(1 - e^{-\alpha x}\right)^2
\end{equation*}

where $m$ is the mass, $D$ represents the well depth and $\alpha$ controls the well width. The vector field that defines the dynamical system is given by:

\begin{align}
   \dot{x} & = \dfrac{\partial H}{\partial p_x} = f_1(x,p_x) = \dfrac{p_x}{m} \\[.2cm]
   \dot{p}_x & = -\dfrac{\partial H}{\partial x} = f_2(x,p_x) = -2 D\,\alpha \left(e^{-\alpha x} - e^{-2\alpha x}\right)
\end{align}

Define the `Morse1D` function containing the two-dimensional dynamical system


```python
def Morse1D(t, u, PARAMETERS = [1, 2, 1]):
    """
    Returns a user-defined 1-DoF vector field at time t, for an array of points in phase-space.
    Number of model parameters: 1 . PARAMETERS = [m, D, alpha]
    Functional form: v = ((1/m)*y, -2*D*alpha*(exp(-alpha*x) - exp(-2*alpha*x))), with u = (x, y)
    
    Parameters
    ----------
    t : float
        fixed time-point of vector field, for all points in phase-space.
        
    u : array_like, shape(n,)
        points in phase-space to determine vector field at common time t.
    
    PARAMETERS : list of floats
        vector field parameters
    
    Returns
    -------
    v : array_like, shape(n,)
        vector field corresponding to points u, in phase-space at time t
    """
    x, y = u.T
    ##############################################
    #
    # CHANGE STUFF BELOW ONLY
    #
    ##############################################
    # Hamiltonian Model Parameter
    m, D, alpha = PARAMETERS
    v = np.column_stack([ (1/m) * y, -2 * D * alpha * (np.exp(-alpha * x) - np.exp(-2 * alpha * x))])
    ##############################################
    #
    # CHANGE STUFF ABOVE ONLY
    #
    ##############################################    
    return v
```

### Define 2D grid to compute LDs


```python
# Mesh parameters to calculate LDs

# 2D Domain
x_min,x_max = [-1, 7]
y_min,y_max = [-2.5, 2.5]

# Grid size
Nx, Ny = [500, 500]

grid_parameters = [(x_min, x_max, Nx), (y_min, y_max, Ny)]
```

### Setup vector field and model parameters


```python
# Morse oscillator parameters
m, D, alpha = [1, 2, 1]

vector_field = lambda t,u: Morse1D(t, u, PARAMETERS = [m, D, alpha])
```

### Compute and draw LDs at time $t = t_0$


```python
# Integration parameters (Calculate LDs at time t = t0 by integrating in the interval [t0-tau,t0+tau])
t0 = 0
tau = 15

# LDs definition (we will use the p-value seminorm)
p_value = 0.5

LD_forward = compute_lagrangian_descriptor(grid_parameters, vector_field, tau, p_value)
LD_backward = compute_lagrangian_descriptor(grid_parameters, vector_field, -tau, p_value)

figs = draw_all_lds(LD_forward, LD_backward, grid_parameters, tau, p_value)
```

We change next the model parameters:


```python
# Morse oscillator parameters
m, D, alpha = [1, 1, 1]

vector_field = lambda t,u: Morse1D(t, u, PARAMETERS = [m, D, alpha])
```

### Compute and draw LDs at time $t = t_0$


```python
# Integration parameters (Calculate LDs at time t = t0 by integrating in the interval [t0-tau,t0+tau])
t0 = 0
tau = 15

# LDs definition (we will use the arclength)
p_value = 2

LD_forward = compute_lagrangian_descriptor(grid_parameters, vector_field, tau, p_value)
LD_backward = compute_lagrangian_descriptor(grid_parameters, vector_field, -tau, p_value)

figs = draw_all_lds(LD_forward, LD_backward, grid_parameters, tau, p_value)
```


```python

```

f9c3e957642d9113dc06aadb8314db1e81c18f1c
ipynb
Jupyter Notebook
tutorials/tutorial-4.ipynb
champsproject/ldds
dc6a94e52b3941935d8e3ed22e3c083cd8e7bf31
BSD-3-Clause
2021-04-09T09:16:14.000Z
2021-12-27T08:43:28.000Z
tutorials/tutorial-4.ipynb
champsproject/ldds
dc6a94e52b3941935d8e3ed22e3c083cd8e7bf31
BSD-3-Clause
2020-10-21T14:28:29.000Z
2021-07-26T16:33:18.000Z
tutorials/tutorial-4.ipynb
champsproject/ldds
dc6a94e52b3941935d8e3ed22e3c083cd8e7bf31
BSD-3-Clause
2020-05-19T13:16:50.000Z
2020-08-03T14:34:42.000Z
Qwen/Qwen-72B
1. YES
2. YES