API

The typical use of the package consists of three steps:

lagprob = LagrangianDescriptorProblem(prob, M, uu0)  # 1. set up the problem
lagsol = solve(lagprob, alg)                        # 2. solve it
plot(lagsol)                                        # 3. visualize the descriptors

where prob is a differential equation problem, M is an infinitesimal Lagrangian descriptor, uu0 is a collection of initial conditions, and alg is an algorithm to solve prob. The Lagrangian descriptors at each initial condition are then given by lagsol(), or lagsol(:forward), lagsol(:backward), and so on.

Problems

LagrangianDescriptors.LagrangianDescriptorProblem — Type
LagrangianDescriptorProblem(prob, M, uu0; direction = :both)

A Lagrangian descriptor problem: the computation of the Lagrangian descriptors of the differential equation problem prob, given by the infinitesimal descriptor M, at each initial condition in the collection uu0. Solve it with solve to obtain a LagrangianDescriptorSolution.

Arguments

  • prob: an ODEProblem, a RODEProblem or an SDEProblem. Its time span (t0, tf) sets the time windows: [t0, tf] forward and [2t0 - tf, t0] backward. Its initial condition is replaced by the elements of uu0, and everything else, such as its parameters and keyword arguments, is kept.
  • M: the infinitesimal Lagrangian descriptor, integrated along the forward and backward solutions. Its signature depends on the problem:
    • ODEProblem: M(du, u, p, t), where du is the value of the right-hand side;
    • RODEProblem: M(du, u, p, t, W), where W is the value of the noise;
    • SDEProblem: M(du, u, p, t), where du is the value of the drift.
    Alternatively, an IncrementDescriptor, for the descriptor given by the increments of the solution.
  • uu0: a collection of initial conditions, such as a vector, or a matrix for a grid of initial conditions of a two-dimensional problem. Each element is used as an initial condition of prob, so it is a number for a scalar problem, or a vector otherwise.
  • direction: :both (default), :forward or :backward; the directions of time in which the Lagrangian descriptors are computed.

Fields

  • ensprob: the EnsembleProblem solved by solve with its default keyword arguments. Its trajectories solve the augmented problem built by augmentprob, and their outputs are the Lagrangian descriptors at the end of the time interval, along with the solver's return codes.
  • uu0: the given collection of initial conditions.
  • direction: the given direction.
  • prob and M: the given problem and descriptor, from which solve builds the ensemble problem for other keyword arguments.

Example

Lagrangian descriptors of a periodically forced Duffing equation, over a grid of initial conditions:

using LagrangianDescriptors, OrdinaryDiffEq, Plots
using LinearAlgebra: norm

function f!(du, u, p, t)
    x, y = u
    A, ω = p
    du[1] = y
    du[2] = x - x^3 + A * cos(ω * t)
end

u0 = [0.0, 0.0]
tspan = (0.0, 13.0)
A = 0.3; ω = π; p = (A, ω)

prob = ODEProblem(f!, u0, tspan, p)
M(du, u, p, t) = norm(du)
uu0 = [[x, y] for y in range(-1.0, 1.0, length = 301), x in range(-1.8, 1.8, length = 301)]

lagprob = LagrangianDescriptorProblem(prob, M, uu0)
lagsol = solve(lagprob, Tsit5())
plot(lagsol)
source

Lagrangian descriptors

The Lagrangian descriptors are given either by the integral of an infinitesimal descriptor M along the solutions, or by the sum of the increments of the solutions over a time grid, with an IncrementDescriptor.

The infinitesimal descriptor M is any function with the arguments below, where du is the value of the right-hand side, or drift, of the equation at the state u, with parameters p, at time t:

ProblemInfinitesimal descriptordu
ODEProblemM(du, u, p, t)f(u, p, t)
RODEProblemM(du, u, p, t, W)f(u, p, t, W), with the noise W
SDEProblemM(du, u, p, t)the drift f(u, p, t)

A common choice is the norm of the velocity, M(du, u, p, t) = norm(du), for which the Lagrangian descriptor is the length of the solution over the time interval.

LagrangianDescriptors.IncrementDescriptor — Type
IncrementDescriptor(; Δt, p = 0.5)

Lagrangian descriptor given by the sum of the p-th powers of the increments of the solution over a time grid with step Δt, as defined for stochastic differential equations in Balibrea-Iniesta, Lopesino, Wiggins and Mancho, Lagrangian Descriptors for Stochastic Differential Equations: A Tool for Revealing the Phase Portrait of Stochastic Dynamical Systems (2016).

It is passed in place of the infinitesimal Lagrangian descriptor M when building a LagrangianDescriptorProblem:

lagprob = LagrangianDescriptorProblem(prob, IncrementDescriptor(Δt = 0.05, p = 0.5), uu0)
lagsol = solve(lagprob, EM(), dt = 0.05)

With tspan = (t0, tf) and the grid t_j = t0 + j Δt, j = 0, …, N, with t_N = tf, the forward Lagrangian descriptor is

\[L_{\mathrm{fwd}} = \sum_{j=0}^{N-1} \sum_k \left| u^k(t_{j+1}) - u^k(t_j) \right|^p,\]

where $k$ runs over the components of the solution, and the backward one is the same sum along the backward solution, over the grid t0 - j Δt, j = 0, …, N. Their sum is the descriptor of the paper, over the time window [2t0 - tf, tf].

The time span tf - t0 must be a multiple of Δt. The solution is computed on the grid points exactly (they are passed to the solver as saveat and tstops), not interpolated, since interpolation would smooth out the noise. The paper uses the Euler–Maruyama method with step Δt, but any algorithm can be used.

For a stochastic equation, the descriptor depends on Δt and does not converge as Δt → 0, since the paths of a Wiener process have infinite length. The paper uses 0 < p ≤ 1. For p = 1 and a differential equation, it converges to the length of the solution as Δt → 0.

It also works for ODEProblems and RODEProblems. The mean over many realizations of the noise, also used in the paper, is obtained with the keyword realizations of solve.

source

Solving

CommonSolve.solve — Function
solve(prob::LagrangianDescriptorProblem, alg, args...; max_trajectory_value = Inf, realizations = 1, shared_noise = false, kwargs...)

Solve a LagrangianDescriptorProblem, returning a LagrangianDescriptorSolution.

Each initial condition in prob.uu0 is solved with the algorithm alg, as one trajectory of the ensemble problem prob.ensprob. The remaining positional arguments args and keyword arguments kwargs are passed on to the ensemble solve, and so to the solve of each trajectory. For instance, an ensemble algorithm, such as EnsembleThreads() (the default), EnsembleSerial() or EnsembleDistributed(), can be given after alg, and tolerances (abstol, reltol), a time step (dt), or a callback are given as keyword arguments. The number of trajectories is set from prob.uu0 and realizations.

Keyword arguments

  • max_trajectory_value::Real = Inf: if finite, the forward and the backward solutions are each stopped as soon as the absolute value of any of their components exceeds it. This is useful for systems whose solutions diverge, e.g. dissipative systems backward in time. The two directions are treated separately: a direction that is stopped keeps the Lagrangian descriptor accumulated up to then, while the other direction goes on. The stopped directions are marked in the fields terminated_forward and terminated_backward of the solution, and terminated marks the trajectories with any stopped direction.
  • realizations::Integer = 1: the number of times each initial condition is solved. For random or stochastic equations, each realization has different noise, and the solution gives the mean of the Lagrangian descriptors over the realizations, as in Balibrea-Iniesta, Lopesino, Wiggins and Mancho (2016). The return codes and the terminated flags of the solution are kept for each of the realizations * length(prob.uu0) solves.
  • shared_noise::Bool = false: for an SDEProblem or a RODEProblem, whether all initial conditions of a realization share the same noise path, as in the definition of the Lagrangian descriptors of stochastic equations of Balibrea-Iniesta, Lopesino, Wiggins and Mancho (2016), for a fixed realization of the noise. By default, each initial condition has independent noise. If the problem has a seed, the noise is reproducible in both cases.

Examples

lagsol = solve(lagprob, Tsit5())                          # an ODE problem
lagsol = solve(lagprob, Tsit5(), EnsembleSerial(); abstol = 1e-8, reltol = 1e-8)
lagsol = solve(lagprob, Tsit5(); max_trajectory_value = 1e3)
lagsol = solve(lagprob, RandomHeun(), dt = 1 / 100)       # a RODE problem
lagsol = solve(lagprob, SOSRI(); realizations = 50)       # an SDE problem
lagsol = solve(lagprob, EM(), dt = 0.01; shared_noise = true, realizations = 10)
source

Solutions

LagrangianDescriptors.LagrangianDescriptorSolution — Type
LagrangianDescriptorSolution

The solution of a LagrangianDescriptorProblem, returned by solve.

Lagrangian descriptors

The Lagrangian descriptors are obtained by calling the solution, lagsol(direction); see below. They are plotted with plot(lagsol); see the section on plotting.

Fields

  • enssol: the EnsembleSolution of the ensemble problem prob.ensprob. Each element of enssol.u is a NamedTuple with the Lagrangian descriptors lfwd and/or lbwd at the end of a trajectory, depending on the direction, and the solver's return code retcode.
  • uu0: the collection of initial conditions of the problem.
  • direction: the direction of the problem, :both, :forward or :backward.
  • terminated: a BitVector marking the trajectories with a direction stopped by the keyword max_trajectory_value of solve.
  • retcodes: the solver's return code (a SciMLBase.ReturnCode.T) of each trajectory. Trajectories that did not finish normally, e.g. because they reached maxiters or became unstable, are found with .!SciMLBase.successful_retcode.(lagsol.retcodes); their Lagrangian descriptors are not reliable.
  • terminated_forward and terminated_backward: BitVectors marking the trajectories whose forward or backward solution, respectively, was stopped by max_trajectory_value; their descriptors in that direction only measure the solution up to then.

When solved with several realizations (see solve), enssol.u and the flags and return codes have one entry per solve: those of all initial conditions of the first realization, then of the second one, and so on.

source

Plotting

A LagrangianDescriptorSolution can be plotted with Makie or Plots, e.g. with plot(lagsol) or plot(lagsol, direction); see Plotting.

Lower-level functions

The augmented problems solved for each initial condition, and the computation of the Lagrangian descriptors of a single solution of the original problem, by post-processing (see Alternative via post-processing).

LagrangianDescriptors.augmentprob — Function
augmentprob(prob::ODEProblem, M; direction::Symbol = :both, max_trajectory_value::Real = Inf)

Create an augmented ODEProblem by aggregating the forward and backward Lagrangian descriptors to the given ODEProblem.

More precisely, with given f = prob.f, u0 = prob.u0, and tspan = prob.tspan, an augmented ODEProblem is created that solves the forward and backward ODE and the forward and backward Lagrangian descriptors simultaneously.

With tspan = (t0, tf), the forward problem is solved over [t0, tf] and the backward problem over [2t0 - tf, t0], both starting from u0 at time t0. The backward problem is integrated via the change of variables t ↦ 2t0 - t, so that both run along the same time interval tspan.

The (global) Lagrangian descriptors are based on the provided local descriptor M=M(du, u, p, t). The (forward/backward) Lagrangian descriptor is the time-integration of the local descriptor along the (forward/backward) trajectory.

If direction=:forward or direction=:backward are given, then only the forward or backward problem is constructed in the augmented system. The default is direction=:both, where both directions are considered.

If max_trajectory_value is finite, each direction is frozen once the absolute value of a component of its solution exceeds it: its solution and its Lagrangian descriptor stop changing, while the other direction goes on.

source
augmentprob(prob::RODEProblem, M; direction::Symbol = :both, max_trajectory_value::Real = Inf)

Create an augmented RODEProblem by aggregating the forward and backward Lagrangian descriptors to the given RODEProblem.

More precisely, with given f = prob.f, u0 = prob.u0, W = prob.noise, and tspan = prob.tspan, an augmented RODEProblem is created that solves the forward and backward RODE and the forward and backward Lagrangian descriptors simultaneously.

With tspan = (t0, tf), the forward problem is solved over [t0, tf] and the backward problem over [2t0 - tf, t0], both starting from u0 at time t0. The backward problem is integrated via the change of variables t ↦ 2t0 - t, so that both run along the same time interval tspan.

Unless prob has a given noise process, the noise is the solver's default Wiener process starting at t0, with the shape of rand_prototype, or of u0 if rand_prototype is not given, as when solving prob directly. The forward and backward problems are then driven by independent Wiener processes, as for a two-sided Wiener process, whose paths over [2t0 - tf, t0] and over [t0, tf] are independent. In the augmented problem, the noise is a vector with a copy for each direction, and f and M receive the noise of their direction, with the shape of rand_prototype or u0 (a scalar for a scalar u0).

If prob has a given noise process, an independent copy of it is not available in general, so both directions are driven by that same process, evaluated at the forward time t.

The (global) Lagrangian descriptors are based on the provided local descriptor M=M(du, u, p, t, W). The (forward/backward) Lagrangian descriptor is the time-integration of the local descriptor along the (forward/backward) trajectory.

If direction=:forward or direction=:backward are given, then only the forward or backward problem is constructed in the augmented system. The default is direction=:both, where both directions are considered.

If max_trajectory_value is finite, each direction is frozen once the absolute value of a component of its solution exceeds it: its solution and its Lagrangian descriptor stop changing, while the other direction goes on.

source
augmentprob(prob::SDEProblem, M; direction::Symbol = :both, max_trajectory_value::Real = Inf)

Create an augmented SDEProblem by aggregating the forward and backward Lagrangian descriptors to the given SDEProblem.

More precisely, with given drift f = prob.f, diffusion g = prob.g, u0 = prob.u0, and tspan = prob.tspan, an augmented SDEProblem is created that solves the forward and backward SDE and the forward and backward Lagrangian descriptors simultaneously.

With tspan = (t0, tf), the forward problem is solved over [t0, tf] and the backward problem over [2t0 - tf, t0], both starting from u0 at time t0. The backward problem is integrated via the change of variables t ↦ 2t0 - t, so that both run along the same time interval tspan: its drift is -f and its diffusion is g, both evaluated at the time 2t0 - t.

The noise driving the backward problem is independent of the noise driving the forward problem, as for a two-sided Wiener process, whose increments over [2t0 - tf, t0] and over [t0, tf] are independent. With diagonal noise, each component of the augmented system has its own Wiener process. With non-diagonal noise, given by an n × m noise_rate_prototype, the augmented noise matrix has the diffusion of the forward problem and that of the backward problem in separate columns, so each has its own m noise channels.

The (global) Lagrangian descriptors are based on the provided local descriptor M=M(du, u, p, t), where du is the drift f(u, p, t). The (forward/backward) Lagrangian descriptor is the time-integration of the local descriptor along the (forward/backward) stochastic trajectory.

If direction=:forward or direction=:backward are given, then only the forward or backward problem is constructed in the augmented system. The default is direction=:both, where both directions are considered.

If max_trajectory_value is finite, each direction is frozen once the absolute value of a component of its solution exceeds it: its solution and its Lagrangian descriptor stop changing, while the other direction goes on.

Custom noise processes (the noise keyword of SDEProblem) are not supported, since the augmented system needs noise with a different dimension.

source
LagrangianDescriptors.lagrangian_descriptor — Function
lagrangian_descriptor(sol::ODESolution, M)

Computes the Lagrangian descriptor of a single trajectory of an ODEProblem.

It takes the provided infinitesimal Lagrangian descriptor of the form M=M(du, u, p, t), along with the time interval given in sol.prob.tspan, to compute

\[L = \int_{t_0}^{t_f} M(du(t), u(t), p, t) \;\mathrm{d}t,\]

where $t_0$ and $t_f$ are the initial and final times in sol.prob.tspan, u(t) = sol(t), p = sol.prob.p, and du(t) is computed from sol in a way dependent on whether sol.prob is in place or out of place.

The time integration is done via QuadGK.quadgk.

source
lagrangian_descriptor(sol::RODESolution, M)

Computes the Lagrangian descriptor of a single trajectory of a RODEProblem or of an SDEProblem (whose solutions are also RODESolutions).

It takes the provided infinitesimal Lagrangian descriptor in the form M=M(du, u, p, t, W), along with the time interval given in sol.prob.tspan, to compute

\[L = \int_{t_0}^{t_f} M(du(t), u(t), p, t, W(t)) \;\mathrm{d}t,\]

where $t_0$ and $t_f$ are the initial and final times in sol.prob.tspan, u(t) = sol(t), p = sol.prob.p, W(t) = first(sol.W(t)), and du(t) is either du = sol.prob.f(sol(t), sol.prob.p, t, first(sol.W(t))) or given by sol.prob.f(du, sol(t), sol.prob.p, t, first(sol.W(t))), depending on whether sol.prob is in place or out of place.

For an SDEProblem, the infinitesimal Lagrangian descriptor has the form M=M(du, u, p, t), where du is the drift sol.prob.f along the solution, without the noise argument, as in augmentprob for SDEProblems.

QuadGK.quadgk is an adaptive integration method and does not perform well on solutions of random or stochastic equations, so the integration, in these case, is performed via a simple trapezoidal rule on the mesh given by sol.t, which may be far com accurate depending on the mesh.

source

Index