diff --git a/docs/make.jl b/docs/make.jl index 355914e5..b9d2e2e3 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -22,7 +22,7 @@ makedocs(; pages=Any[ "Home" => "index.md", "Quickstart" => "quickstart.md", - "Tutorial" => "tutorial.md", + "Tutorial" => "examples/tutorial.md", "Manual" => Any[ "Defining a StochSystem" => "man/stochsystem.md", "Stability analysis" => "man/systemanalysis.md", @@ -32,6 +32,9 @@ makedocs(; "Noise processes" => "man/noise.md", "Utilities" => "man/utils.md" ], + "Examples" => Any[ + "Instanton for the Maier-Stein system" => "examples/gMAM_Maierstein.md" + ], "Predefined systems" => "man/systems.md", "Development stage" => "man/dev.md" ], diff --git a/docs/src/examples/gMAM_Maierstein.md b/docs/src/examples/gMAM_Maierstein.md new file mode 100644 index 00000000..672eda32 --- /dev/null +++ b/docs/src/examples/gMAM_Maierstein.md @@ -0,0 +1,311 @@ + +# Minimal action path for the Maier-Stein model. + +Here we compute the maximum likelihood transition path (instanton) for the Maier-Stein model. + +```@example GMAM +using CriticalTransitions + +using CairoMakie +using CairoMakie.Makie.MathTeXEngine: get_font +font = (; + regular=get_font(:regular), bold=get_font(:bold), + italic=get_font(:italic), bold_italic=get_font(:bolditalic) +) +``` + +Let us explore the features of [CriticalTransitions.jl](https://github.com/JuliaDynamics/CriticalTransitions.jl) with Maier-Stein model. + +## Maier-stein model + +The [Maier-Stein model](https://link.springer.com/article/10.1007/BF02183736) (J. Stat. Phys 83, 3–4 (1996)) is commonly used in the field of nonlinear dynamics for benchmarking Large Deviation Theory (LDT) techniques, e.g., stoachastic transitions between different stable states. It is a simple model that describes the dynamics of a system with two degrees of freedom ``u`` and ``v``, and is given by the following set of ordinary differential equations: +```math +\begin{aligned} + \dot{u} &= u-u^3 - \beta*u*v^2\\ + \dot{v} &= -\alpha (1+u^2)*v +\end{aligned} +``` +The parameter ``\alpha>0`` controls the strength of the drift field and ``\beta>0`` represents the softening of that drift field. + +```@example GMAM +function meier_stein!(du, u, p, t) # in-place + x, y = u + du[1] = x - x^3 - 10 * x * y^2 + du[2] = -(1 + x^2) * y +end +function meier_stein(u, p, t) # out-of-place + x, y = u + dx = x - x^3 - 10 * x * y^2 + dy = -(1 + x^2) * y + SA[dx, dy] +end +σ = 0.25 +sys = StochSystem(meier_stein, [], zeros(2), σ, idfunc, nothing, I(2), "WhiteGauss") +``` + +A good reference to read about the large deviations methods is [this](https://homepages.warwick.ac.uk/staff/T.Grafke/simplified-geometric-minimum-action-method-for-the-computation-of-instantons.html) or [this]( https://homepages.warwick.ac.uk/staff/T.Grafke/simplified-geometric-minimum-action-method-for-the-computation-of-instantons.html) blog post by Tobias Grafke. + +## Attractors + +We start by investigating the deterministic dynamics of the Maier-Stein model. + +The function `fixed points` return the attractors, their eigenvalues and stability within the state space volume defined by `bmin` and `bmax`. + +```@example GMAM +u_min = -1.1; +u_max = 1.1; +v_min = -0.4; +v_max = 0.4; +bmin = [u_min, v_min]; +bmax = [u_max, v_max]; +fp, eig, stab = fixedpoints(sys, bmin, bmax) +stable_fp = fp[stab] +``` + +```@example GMAM +res = 100 +u_range = range(u_min, u_max, length=res) +v_range = range(v_min, v_max, length=res) + +du(u, v) = u - u^3 - 10 * u * v^2 +dv(u, v) = -(1 + u^2) * v +odeSol(u, v) = Point2f(du(u, v), dv(u, v)) + +z = [norm([du(x, y), dv(x, y)]) for x in u_range, y in v_range] +zmin, zmax = minimum(z), maximum(z) + +fig = Figure(size=(600, 400), fontsize=13) +ax = Axis(fig[1, 1], xlabel="u", ylabel="v", aspect=1.4, + xgridcolor=:transparent, ygridcolor=:transparent, + ylabelrotation=0) + +hm = heatmap!(ax, u_range, v_range, z, colormap=:Blues, colorrange=(zmin, zmax)) +Colorbar(fig[1, 2], hm; label="", width=15, ticksize=15, tickalign=1) +streamplot!(ax, odeSol, (u_min, u_max), (v_min, v_max); + gridsize=(20, 20), arrow_size=10, stepsize=0.01, + colormap=[:black, :black] +) +colgap!(fig.layout, 7) +limits!(u_min, u_max, v_min, v_max) +fig + +[scatter!(ax, Point(fp[i]), color=stab[i] > 0 ? :red : :dodgerblue, + markersize=10) for i in eachindex(fp)] +fig +``` + +We can simulate a stochastic trajectory using the function `simulate`. + +```@example GMAM +sol = simulate(sys, [1.0, 0.0], tmax=1000) + +fig = Figure(size=(1000, 400), fontsize=13) +ax1 = Axis(fig[1, 1], xlabel="t", ylabel="u", aspect=1.2, + xgridcolor=:transparent, ygridcolor=:transparent, + ylabelrotation=0) +ax2 = Axis(fig[1, 2], xlabel="u", ylabel="v", aspect=1.2, + xgridcolor=:transparent, ygridcolor=:transparent, + ylabelrotation=0) + +lines!(ax1, sol.t, first.(sol.u); linewidth=2, color=:black) + +hm = heatmap!(ax2, u_range, v_range, z, colormap=:Blues, colorrange=(zmin, zmax)) +Colorbar(fig[1, 3], hm; label="", width=15, ticksize=15, tickalign=1) +streamplot!(ax2, odeSol, (u_min, u_max), (v_min, v_max); + gridsize=(20, 20), arrow_size=10, stepsize=0.01, + colormap=[:white, :white] +) +colgap!(fig.layout, 7) +limits!(u_min, u_max, v_min, v_max) +fig + +[scatter!(ax2, Point(fp[i]), color=stab[i] > 0 ? :red : :dodgerblue, + markersize=10) for i in eachindex(fp)] + +lines!(ax2, reduce(hcat, sol.u), linewidth=1, color=(:black, 0.2)) +fig +``` + +## Basins of attraction + +Basins of attraction are the regions in the state space that lead to a particular attractor. We can find the basins of attraction using the function `basins`. + +@example GMAM +ba = basins(sys, [0.0, 0], [0.0, 1], [1.0, 0], intervals_to_box([-2, -2], [2, 2]), bstep=[0.01, 0.01], ϵ_mapper=0.001, Ttr=100) +Ur, Vr, atr, M = ba +heatmap(Ur, Vr, M) +``` + +The basin boundaries can be quickly extracted using the function `basin_boundaries`. + +@example GMAM +bb = basinboundary(ba) +``` + +## Edge tracking + +The edge tracking algorithm is a simple numerical method to find the edge state or (possibly chaotic) saddle on the boundary between two basins of attraction. It is first introduced by [Battelino et al. (1988)](https://doi.org/10.1016/0167-2789(88)90057-7) and further described by [Skufca et al. (2006)](https://doi.org/10.1103/PhysRevLett.96.174101). + +@example GMAM +edge = edgetracking(sys, [0.0, 0.0], [-1.0, -0.2], [stable_fp[1], stable_fp[2]]) +``` + +## Transitions + +We can quickly find a path which computes a transition from one attractor to another using the function `transition. + +@example GMAM +path, time, succes = transition(sys, fp[stab][1], fp[stab][2]) +``` + +@example GMAM +fig = Figure(size=(600, 400), fontsize=13) +ax = Axis(fig[1, 1], xlabel="u", ylabel="v", aspect=1.4, + xgridcolor=:transparent, ygridcolor=:transparent, + ylabelrotation=0) + +hm = heatmap!(ax, u_range, v_range, z, colormap=:Blues, colorrange=(zmin, zmax)) +Colorbar(fig[1, 2], hm; label="", width=15, ticksize=15, tickalign=1) +streamplot!(ax, odeSol, (u_min, u_max), (v_min, v_max); + gridsize=(20, 20), arrow_size=10, stepsize=0.01, + colormap=[:white, :white] +) +colgap!(fig.layout, 7) +limits!(u_min, u_max, v_min, v_max) +fig + +[scatter!(ax, Point(fp[i]), color=stab[i] > 0 ? :red : :dodgerblue, + markersize=10) for i in eachindex(fp)] +fig + +lines!(ax, path.u, lw=2, color=:black) +fig +``` + +If we want to compute many: `transitions` is the function to use. + +@example GMAM +tt = transitions(sys, fp[stab][1], fp[stab][2], 1, rad_i=0.1, rad_f=0.1, tmax=1e3); +``` + +@example GMAM +fig = Figure(size=(600, 400), fontsize=13) +ax = Axis(fig[1, 1], xlabel="u", ylabel="v", aspect=1.4, + xgridcolor=:transparent, ygridcolor=:transparent, + ylabelrotation=0) + +hm = heatmap!(ax, u_range, v_range, z, colormap=:Blues, colorrange=(zmin, zmax)) +Colorbar(fig[1, 2], hm; label="", width=15, ticksize=15, tickalign=1) +streamplot!(ax, odeSol, (u_min, u_max), (v_min, v_max); + gridsize=(20, 20), arrow_size=10, stepsize=0.01, + colormap=[:black, :black] +) +colgap!(fig.layout, 7) +limits!(u_min, u_max, v_min, v_max) +fig + +[scatter!(ax, Point(fp[i]), color=stab[i] > 0 ? :red : :dodgerblue, + markersize=10) for i in eachindex(fp)] + +for i in 1:3 + lines!(ax, tt[1][i].u) +end +fig +``` + +@example GMAM +# R, L = fp[stab] +# initial = reduce(hcat, path.u) +# lv = langevinmcmc(sys, initial; tmax =0.1) +``` + +## Large deviation theory + +In the context of nonlinear dynamics, Large Deviation Theory provides tools to quantify the probability of rare events that deviate significantly from the system's typical behavior. These rare events might be extreme values of a system's output, sudden transitions between different states, or other phenomena that occur with very low probability but can have significant implications for the system's overall behavior. + +Large deviation theory applies principles from probability theory and statistical mechanics to develop a rigorous mathematical description of these rare events. It uses the concept of a rate function, which measures the exponential decay rate of the probability of large deviations from the mean or typical behavior. This rate function plays a crucial role in quantifying the likelihood of rare events and understanding their impact on the system. + +For example, in a system exhibiting chaotic behavior, LDT can help quantify the probability of sudden large shifts in the system's trajectory. Similarly, in a system with multiple stable states, it can provide insight into the likelihood and pathways of transitions between these states under fluctuations. In the context of the Minimum Action Method (MAM) and the Geometric Minimum Action Method (gMAM), Large Deviation Theory is used to handle the large deviations action functional on the space of curves. This is a key part of how these methods analyze dynamical systems. + +The Maier-Stein model is a typical benchmark to test such LDT techniques. Let us try to reproduce the following figure from [Tobias Grafke's blog post](https://homepages.warwick.ac.uk/staff/T.Grafke/rogue-waves-and-large-deviations.html): + +![image-2.png](attachment:image-2.png) + +Let us first make an initial path: + +@example GMAM +xx = range(-1.0, 1.0, length=100) +yy = 0.3 .* (-xx .^ 2 .+ 1) +init = Matrix([xx yy]') +``` + +`min_action_method` runs the Minimum Action Method (MAM) to find the minimum action path (instanton) between an initial state x_i and final state x_f. This algorithm uses the minimizers of the [Optim.jl](https://julianlsolvers.github.io/Optim.jl/stable/#) package to minimize the [Freidlin-Wentzell action functional](https://en.wikipedia.org/wiki/Freidlin%E2%80%93Wentzell_theorem) (see `fw_action`) or [Onsager-Machlup action functional](https://en.wikipedia.org/wiki/Onsager%E2%80%93Machlup_function) (see `om_action`) for the given StochSystem `sys`. + +@example GMAM +mm = min_action_method(sys, init, 1, maxiter=200, save_info=false, verbose=false) +``` + +@example GMAM +fig = Figure(size=(600, 400), fontsize=13) +ax = Axis(fig[1, 1], xlabel="u", ylabel="v", aspect=1.4, + xgridcolor=:transparent, ygridcolor=:transparent, + ylabelrotation=0) + +hm = heatmap!(ax, u_range, v_range, z, colormap=:Blues, colorrange=(zmin, zmax)) +Colorbar(fig[1, 2], hm; label="", width=15, ticksize=15, tickalign=1) +streamplot!(ax, odeSol, (u_min, u_max), (v_min, v_max); + gridsize=(20, 20), arrow_size=10, stepsize=0.01, + colormap=[:black, :black] +) +colgap!(fig.layout, 7) +limits!(u_min, u_max, v_min, v_max) +fig + +[scatter!(ax, Point(fp[i]), color=stab[i] > 0 ? :red : :dodgerblue, + markersize=10) for i in eachindex(fp)] + +lines!(ax, init, linewidth=3, color=:black, linestyle=:dash) +lines!(ax, mm, linewidth=3, color=:orange) +fig +``` + +TODO: Why does MAM get the wrong results? + +`geometric_min_action_method` computes the minimizer of the Freidlin-Wentzell action using the geometric minimum action method (gMAM). The Minimum Action Method (MAM) is a more traditional approach, while the Geometric Minimum Action Method (gMAM) is a blend of the original MAM and the [string method](https://journals.aps.org/prb/abstract/10.1103/PhysRevB.66.052301). + +@example GMAM +gm = geometric_min_action_method(sys, init, converge=1e-7, maxiter=1000) +``` + +@example GMAM +fig = Figure(size=(600, 400), fontsize=13) +ax = Axis(fig[1, 1], xlabel="u", ylabel="v", aspect=1.4, + xgridcolor=:transparent, ygridcolor=:transparent, + ylabelrotation=0) + +hm = heatmap!(ax, u_range, v_range, z, colormap=:Blues, colorrange=(zmin, zmax)) +Colorbar(fig[1, 2], hm; label="", width=15, ticksize=15, tickalign=1) +streamplot!(ax, odeSol, (u_min, u_max), (v_min, v_max); + gridsize=(20, 20), arrow_size=10, stepsize=0.01, + colormap=[:black, :black] +) +colgap!(fig.layout, 7) +limits!(u_min, u_max, v_min, v_max) +fig + +[scatter!(ax, Point(fp[i]), color=stab[i] > 0 ? :red : :dodgerblue, + markersize=10) for i in eachindex(fp)] + +# len = length(gm[1]) +# for i in 2:(len-20) +# i & 10 == 0 && lines!(ax, gm[1][i], linewidth = 3, color = (:black, i/(3*len))) +# end +lines!(ax, gm[1][1], linewidth=3, color=:black, linestyle=:dash) +lines!(ax, gm[1][end], linewidth=3, color=:orange) +# lines!(ax, mm, linewidth = 3, color = :orange) +fig +``` + +--- +Author: Orjan Ameye (orjan.ameye@hotmail.com) +Date: 13 Feb 2024 \ No newline at end of file diff --git a/docs/src/tutorial.md b/docs/src/examples/tutorial.md similarity index 100% rename from docs/src/tutorial.md rename to docs/src/examples/tutorial.md diff --git a/examples/gMam_Maierstein.ipynb b/examples/gMam_Maierstein.ipynb index c7874d43..1b936fc1 100644 --- a/examples/gMam_Maierstein.ipynb +++ b/examples/gMam_Maierstein.ipynb @@ -23,8 +23,8 @@ "using CairoMakie\n", "using MathTeXEngine: get_font\n", "font = (;\n", - " regular = get_font(:regular), bold = get_font(:bold),\n", - " italic = get_font(:italic), bold_italic = get_font(:bolditalic)\n", + " regular=get_font(:regular), bold=get_font(:bold),\n", + " italic=get_font(:italic), bold_italic=get_font(:bolditalic)\n", ")\n", "using Colors, ColorSchemes" ] @@ -80,13 +80,13 @@ "source": [ "function meier_stein!(du, u, p, t) # in-place\n", " x, y = u\n", - " du[1] = x-x^3 -10*x*y^2\n", - " du[2] = -(1+x^2)*y\n", + " du[1] = x - x^3 - 10 * x * y^2\n", + " du[2] = -(1 + x^2) * y\n", "end\n", "function meier_stein(u, p, t) # out-of-place\n", " x, y = u\n", - " dx = x-x^3 -10*x*y^2\n", - " dy = -(1+x^2)*y\n", + " dx = x - x^3 - 10 * x * y^2\n", + " dy = -(1 + x^2) * y\n", " SA[dx, dy]\n", "end\n", "σ = 0.25\n", @@ -139,9 +139,12 @@ } ], "source": [ - "u_min = -1.1; u_max = 1.1;\n", - "v_min = -0.4; v_max = 0.4\n", - "bmin = [u_min, v_min]; bmax = [u_max, v_max]\n", + "u_min = -1.1;\n", + "u_max = 1.1;\n", + "v_min = -0.4;\n", + "v_max = 0.4;\n", + "bmin = [u_min, v_min];\n", + "bmax = [u_max, v_max];\n", "fp, eig, stab = fixedpoints(sys, bmin, bmax)\n", "stable_fp = fp[stab]" ] @@ -164,33 +167,33 @@ ], "source": [ "res = 100\n", - "u_range = range(u_min, u_max, length = res)\n", - "v_range = range(v_min, v_max, length = res)\n", + "u_range = range(u_min, u_max, length=res)\n", + "v_range = range(v_min, v_max, length=res)\n", "\n", - "du(u, v) = u-u^3 -10*u*v^2\n", - "dv(u, v) = -(1+u^2)*v\n", + "du(u, v) = u - u^3 - 10 * u * v^2\n", + "dv(u, v) = -(1 + u^2) * v\n", "odeSol(u, v) = Point2f(du(u, v), dv(u, v))\n", "\n", "z = [norm([du(x, y), dv(x, y)]) for x in u_range, y in v_range]\n", "zmin, zmax = minimum(z), maximum(z)\n", "\n", - "fig = Figure(size = (600, 400), fontsize =13)\n", - "ax = Axis(fig[1, 1], xlabel = \"u\", ylabel = \"v\", aspect=1.4,\n", - " xgridcolor = :transparent, ygridcolor = :transparent,\n", - " ylabelrotation = 0)\n", + "fig = Figure(size=(600, 400), fontsize=13)\n", + "ax = Axis(fig[1, 1], xlabel=\"u\", ylabel=\"v\", aspect=1.4,\n", + " xgridcolor=:transparent, ygridcolor=:transparent,\n", + " ylabelrotation=0)\n", "\n", - "hm = heatmap!(ax, u_range, v_range, z, colormap = :Blues, colorrange = (zmin, zmax))\n", - "Colorbar(fig[1, 2], hm; label = \"\", width = 15, ticksize = 15, tickalign = 1)\n", + "hm = heatmap!(ax, u_range, v_range, z, colormap=:Blues, colorrange=(zmin, zmax))\n", + "Colorbar(fig[1, 2], hm; label=\"\", width=15, ticksize=15, tickalign=1)\n", "streamplot!(ax, odeSol, (u_min, u_max), (v_min, v_max);\n", - " gridsize = (20, 20), arrow_size = 10, stepsize = 0.01,\n", - " colormap = [:black, :black]\n", - " )\n", + " gridsize=(20, 20), arrow_size=10, stepsize=0.01,\n", + " colormap=[:black, :black]\n", + ")\n", "colgap!(fig.layout, 7)\n", - "limits!(u_min,u_max,v_min,v_max)\n", + "limits!(u_min, u_max, v_min, v_max)\n", "fig\n", "\n", - "[scatter!(ax, Point(fp[i]), color = stab[i] > 0 ? :red : :dodgerblue,\n", - " markersize = 10) for i in eachindex(fp)]\n", + "[scatter!(ax, Point(fp[i]), color=stab[i] > 0 ? :red : :dodgerblue,\n", + " markersize=10) for i in eachindex(fp)]\n", "fig" ] }, @@ -218,32 +221,32 @@ } ], "source": [ - "sol = simulate(sys, [1.0, 0.0], tmax = 1000)\n", + "sol = simulate(sys, [1.0, 0.0], tmax=1000)\n", + "\n", + "fig = Figure(size=(1000, 400), fontsize=13)\n", + "ax1 = Axis(fig[1, 1], xlabel=\"t\", ylabel=\"u\", aspect=1.2,\n", + " xgridcolor=:transparent, ygridcolor=:transparent,\n", + " ylabelrotation=0)\n", + "ax2 = Axis(fig[1, 2], xlabel=\"u\", ylabel=\"v\", aspect=1.2,\n", + " xgridcolor=:transparent, ygridcolor=:transparent,\n", + " ylabelrotation=0)\n", "\n", - "fig = Figure(size = (1000, 400), fontsize =13)\n", - "ax1 = Axis(fig[1, 1], xlabel = \"t\", ylabel = \"u\", aspect=1.2,\n", - " xgridcolor = :transparent, ygridcolor = :transparent,\n", - " ylabelrotation = 0)\n", - "ax2 = Axis(fig[1, 2], xlabel = \"u\", ylabel = \"v\", aspect=1.2,\n", - " xgridcolor = :transparent, ygridcolor = :transparent,\n", - " ylabelrotation = 0)\n", - " \n", - "lines!(ax1, sol.t, first.(sol.u); linewidth = 2, color = :black)\n", + "lines!(ax1, sol.t, first.(sol.u); linewidth=2, color=:black)\n", "\n", - "hm = heatmap!(ax2, u_range, v_range, z, colormap = :Blues, colorrange = (zmin, zmax))\n", - "Colorbar(fig[1, 3], hm; label = \"\", width = 15, ticksize = 15, tickalign = 1)\n", + "hm = heatmap!(ax2, u_range, v_range, z, colormap=:Blues, colorrange=(zmin, zmax))\n", + "Colorbar(fig[1, 3], hm; label=\"\", width=15, ticksize=15, tickalign=1)\n", "streamplot!(ax2, odeSol, (u_min, u_max), (v_min, v_max);\n", - " gridsize = (20, 20), arrow_size = 10, stepsize = 0.01,\n", - " colormap = [:white, :white]\n", - " )\n", + " gridsize=(20, 20), arrow_size=10, stepsize=0.01,\n", + " colormap=[:white, :white]\n", + ")\n", "colgap!(fig.layout, 7)\n", - "limits!(u_min,u_max,v_min,v_max)\n", + "limits!(u_min, u_max, v_min, v_max)\n", "fig\n", "\n", - "[scatter!(ax2, Point(fp[i]), color = stab[i] > 0 ? :red : :dodgerblue,\n", - " markersize = 10) for i in eachindex(fp)]\n", + "[scatter!(ax2, Point(fp[i]), color=stab[i] > 0 ? :red : :dodgerblue,\n", + " markersize=10) for i in eachindex(fp)]\n", "\n", - "lines!(ax2, reduce(hcat,sol.u), linewidth = 1, color = (:black, 0.2))\n", + "lines!(ax2, reduce(hcat, sol.u), linewidth=1, color=(:black, 0.2))\n", "fig" ] }, @@ -295,7 +298,7 @@ } ], "source": [ - "ba = basins(sys, [0.,0], [0.,1], [1.,0], intervals_to_box([-2,-2],[2,2]), bstep=[0.01,0.01], ϵ_mapper=0.001, Ttr=100)\n", + "ba = basins(sys, [0.0, 0], [0.0, 1], [1.0, 0], intervals_to_box([-2, -2], [2, 2]), bstep=[0.01, 0.01], ϵ_mapper=0.001, Ttr=100)\n", "Ur, Vr, atr, M = ba\n", "heatmap(Ur, Vr, M)" ] @@ -370,7 +373,7 @@ } ], "source": [ - "edge = edgetracking(sys, [0.0, 0.0],[-1.0, -0.2], [stable_fp[1], stable_fp[2]])" + "edge = edgetracking(sys, [0.0, 0.0], [-1.0, -0.2], [stable_fp[1], stable_fp[2]])" ] }, { @@ -423,26 +426,26 @@ } ], "source": [ - "fig = Figure(size = (600, 400), fontsize =13)\n", - "ax = Axis(fig[1, 1], xlabel = \"u\", ylabel = \"v\", aspect=1.4,\n", - " xgridcolor = :transparent, ygridcolor = :transparent,\n", - " ylabelrotation = 0)\n", + "fig = Figure(size=(600, 400), fontsize=13)\n", + "ax = Axis(fig[1, 1], xlabel=\"u\", ylabel=\"v\", aspect=1.4,\n", + " xgridcolor=:transparent, ygridcolor=:transparent,\n", + " ylabelrotation=0)\n", "\n", - "hm = heatmap!(ax, u_range, v_range, z, colormap = :Blues, colorrange = (zmin, zmax))\n", - "Colorbar(fig[1, 2], hm; label = \"\", width = 15, ticksize = 15, tickalign = 1)\n", + "hm = heatmap!(ax, u_range, v_range, z, colormap=:Blues, colorrange=(zmin, zmax))\n", + "Colorbar(fig[1, 2], hm; label=\"\", width=15, ticksize=15, tickalign=1)\n", "streamplot!(ax, odeSol, (u_min, u_max), (v_min, v_max);\n", - " gridsize = (20, 20), arrow_size = 10, stepsize = 0.01,\n", - " colormap = [:white, :white]\n", - " )\n", + " gridsize=(20, 20), arrow_size=10, stepsize=0.01,\n", + " colormap=[:white, :white]\n", + ")\n", "colgap!(fig.layout, 7)\n", - "limits!(u_min,u_max,v_min,v_max)\n", + "limits!(u_min, u_max, v_min, v_max)\n", "fig\n", "\n", - "[scatter!(ax, Point(fp[i]), color = stab[i] > 0 ? :red : :dodgerblue,\n", - " markersize = 10) for i in eachindex(fp)]\n", + "[scatter!(ax, Point(fp[i]), color=stab[i] > 0 ? :red : :dodgerblue,\n", + " markersize=10) for i in eachindex(fp)]\n", "fig\n", "\n", - "lines!(ax, path.u, lw = 2, color = :black)\n", + "lines!(ax, path.u, lw=2, color=:black)\n", "fig" ] }, @@ -508,23 +511,23 @@ } ], "source": [ - "fig = Figure(size = (600, 400), fontsize =13)\n", - "ax = Axis(fig[1, 1], xlabel = \"u\", ylabel = \"v\", aspect=1.4,\n", - " xgridcolor = :transparent, ygridcolor = :transparent,\n", - " ylabelrotation = 0)\n", + "fig = Figure(size=(600, 400), fontsize=13)\n", + "ax = Axis(fig[1, 1], xlabel=\"u\", ylabel=\"v\", aspect=1.4,\n", + " xgridcolor=:transparent, ygridcolor=:transparent,\n", + " ylabelrotation=0)\n", "\n", - "hm = heatmap!(ax, u_range, v_range, z, colormap = :Blues, colorrange = (zmin, zmax))\n", - "Colorbar(fig[1, 2], hm; label = \"\", width = 15, ticksize = 15, tickalign = 1)\n", + "hm = heatmap!(ax, u_range, v_range, z, colormap=:Blues, colorrange=(zmin, zmax))\n", + "Colorbar(fig[1, 2], hm; label=\"\", width=15, ticksize=15, tickalign=1)\n", "streamplot!(ax, odeSol, (u_min, u_max), (v_min, v_max);\n", - " gridsize = (20, 20), arrow_size = 10, stepsize = 0.01,\n", - " colormap = [:black, :black]\n", - " )\n", + " gridsize=(20, 20), arrow_size=10, stepsize=0.01,\n", + " colormap=[:black, :black]\n", + ")\n", "colgap!(fig.layout, 7)\n", - "limits!(u_min,u_max,v_min,v_max)\n", + "limits!(u_min, u_max, v_min, v_max)\n", "fig\n", "\n", - "[scatter!(ax, Point(fp[i]), color = stab[i] > 0 ? :red : :dodgerblue,\n", - " markersize = 10) for i in eachindex(fp)]\n", + "[scatter!(ax, Point(fp[i]), color=stab[i] > 0 ? :red : :dodgerblue,\n", + " markersize=10) for i in eachindex(fp)]\n", "\n", "for i in 1:3\n", " lines!(ax, tt[1][i].u)\n", @@ -566,6 +569,7 @@ "metadata": {}, "source": [ "The Maier-Stein model is a typical benchmark to test such LDT techniques. Let us try to reproduce the following figure from [Tobias Grafke's blog post](https://homepages.warwick.ac.uk/staff/T.Grafke/rogue-waves-and-large-deviations.html):\n", + "\n", "![image-2.png](attachment:image-2.png)" ] }, @@ -594,8 +598,8 @@ } ], "source": [ - "xx = range(-1.0,1.0, length=100)\n", - "yy = 0.3 .* (- xx .^ 2 .+ 1)\n", + "xx = range(-1.0, 1.0, length=100)\n", + "yy = 0.3 .* (-xx .^ 2 .+ 1)\n", "init = Matrix([xx yy]')" ] }, @@ -651,26 +655,26 @@ } ], "source": [ - "fig = Figure(size = (600, 400), fontsize =13)\n", - "ax = Axis(fig[1, 1], xlabel = \"u\", ylabel = \"v\", aspect=1.4,\n", - " xgridcolor = :transparent, ygridcolor = :transparent,\n", - " ylabelrotation = 0)\n", + "fig = Figure(size=(600, 400), fontsize=13)\n", + "ax = Axis(fig[1, 1], xlabel=\"u\", ylabel=\"v\", aspect=1.4,\n", + " xgridcolor=:transparent, ygridcolor=:transparent,\n", + " ylabelrotation=0)\n", "\n", - "hm = heatmap!(ax, u_range, v_range, z, colormap = :Blues, colorrange = (zmin, zmax))\n", - "Colorbar(fig[1, 2], hm; label = \"\", width = 15, ticksize = 15, tickalign = 1)\n", + "hm = heatmap!(ax, u_range, v_range, z, colormap=:Blues, colorrange=(zmin, zmax))\n", + "Colorbar(fig[1, 2], hm; label=\"\", width=15, ticksize=15, tickalign=1)\n", "streamplot!(ax, odeSol, (u_min, u_max), (v_min, v_max);\n", - " gridsize = (20, 20), arrow_size = 10, stepsize = 0.01,\n", - " colormap = [:black, :black]\n", - " )\n", + " gridsize=(20, 20), arrow_size=10, stepsize=0.01,\n", + " colormap=[:black, :black]\n", + ")\n", "colgap!(fig.layout, 7)\n", - "limits!(u_min,u_max,v_min,v_max)\n", + "limits!(u_min, u_max, v_min, v_max)\n", "fig\n", "\n", - "[scatter!(ax, Point(fp[i]), color = stab[i] > 0 ? :red : :dodgerblue,\n", - " markersize = 10) for i in eachindex(fp)]\n", + "[scatter!(ax, Point(fp[i]), color=stab[i] > 0 ? :red : :dodgerblue,\n", + " markersize=10) for i in eachindex(fp)]\n", "\n", - "lines!(ax, init, linewidth = 3, color = :black, linestyle = :dash)\n", - "lines!(ax, mm, linewidth = 3, color = :orange)\n", + "lines!(ax, init, linewidth=3, color=:black, linestyle=:dash)\n", + "lines!(ax, mm, linewidth=3, color=:orange)\n", "fig" ] }, @@ -714,30 +718,30 @@ } ], "source": [ - "fig = Figure(size = (600, 400), fontsize =13)\n", - "ax = Axis(fig[1, 1], xlabel = \"u\", ylabel = \"v\", aspect=1.4,\n", - " xgridcolor = :transparent, ygridcolor = :transparent,\n", - " ylabelrotation = 0)\n", + "fig = Figure(size=(600, 400), fontsize=13)\n", + "ax = Axis(fig[1, 1], xlabel=\"u\", ylabel=\"v\", aspect=1.4,\n", + " xgridcolor=:transparent, ygridcolor=:transparent,\n", + " ylabelrotation=0)\n", "\n", - "hm = heatmap!(ax, u_range, v_range, z, colormap = :Blues, colorrange = (zmin, zmax))\n", - "Colorbar(fig[1, 2], hm; label = \"\", width = 15, ticksize = 15, tickalign = 1)\n", + "hm = heatmap!(ax, u_range, v_range, z, colormap=:Blues, colorrange=(zmin, zmax))\n", + "Colorbar(fig[1, 2], hm; label=\"\", width=15, ticksize=15, tickalign=1)\n", "streamplot!(ax, odeSol, (u_min, u_max), (v_min, v_max);\n", - " gridsize = (20, 20), arrow_size = 10, stepsize = 0.01,\n", - " colormap = [:black, :black]\n", - " )\n", + " gridsize=(20, 20), arrow_size=10, stepsize=0.01,\n", + " colormap=[:black, :black]\n", + ")\n", "colgap!(fig.layout, 7)\n", - "limits!(u_min,u_max,v_min,v_max)\n", + "limits!(u_min, u_max, v_min, v_max)\n", "fig\n", "\n", - "[scatter!(ax, Point(fp[i]), color = stab[i] > 0 ? :red : :dodgerblue,\n", - " markersize = 10) for i in eachindex(fp)]\n", + "[scatter!(ax, Point(fp[i]), color=stab[i] > 0 ? :red : :dodgerblue,\n", + " markersize=10) for i in eachindex(fp)]\n", "\n", "# len = length(gm[1])\n", "# for i in 2:(len-20)\n", "# i & 10 == 0 && lines!(ax, gm[1][i], linewidth = 3, color = (:black, i/(3*len)))\n", "# end\n", - "lines!(ax, gm[1][1], linewidth = 3, color = :black, linestyle = :dash)\n", - "lines!(ax, gm[1][end], linewidth = 3, color = :orange)\n", + "lines!(ax, gm[1][1], linewidth=3, color=:black, linestyle=:dash)\n", + "lines!(ax, gm[1][end], linewidth=3, color=:orange)\n", "# lines!(ax, mm, linewidth = 3, color = :orange)\n", "fig" ] diff --git a/src/trajectories/transition.jl b/src/trajectories/transition.jl index f1f0ed9e..5a80cc3f 100644 --- a/src/trajectories/transition.jl +++ b/src/trajectories/transition.jl @@ -101,7 +101,7 @@ The `savefile` keyword argument allows saving the data to a `.jld2` or `.h5` fil * `idx` (Array): list of sample indices i that produced a transition * `N_fail` (Int): number of samples that failed to produce a transition -> An example script using `transitions` is available [here](https://github.com/juliadynamics/CriticalTransitions.jl/blob/main/scripts/sample_transitions_h5.jl). +> An example script using `transitions` is available [here](https://github.com/juliadynamics/CriticalTransitions.jl/blob/main/examples/sample_transitions_h5.jl). """ function transitions(sys::StochSystem, x_i::State, x_f::State, N=1; rad_i=0.1,