예제 #1
0
파일: demo.py 프로젝트: yumengch/DAPPER
def demo(u, s0, N, as_points, ends):
    core.u = u

    # Simulation length
    K = 10**5 // N

    # Computations
    simulator = modelling.with_recursion(core.step, prog="Simulating")

    # Initial ensemble
    E0 = core.x0 + s0 * np.random.randn(N, 2)

    # Simulate
    EE = simulator(E0, K, 0, 1)

    # Plot
    fig, ax = plt.subplots()
    fig.suptitle('Phase space' + f"\nu={core.u}, N={N}, $σ_0$={s0}")
    ax.set(xlabel="x", ylabel="y")

    # Re-order axes for plotting
    tail_length = 0  # 0 => infinite
    ET = EE[-tail_length:].transpose((2, 0, 1))

    if as_points:
        ax.plot(*ET, "b.", ms=1.0, alpha=1.0)
    else:
        ax.plot(*ET, "b-", lw=.02, alpha=0.1)

    if ends:
        ax.plot(*EE[0].T, '*g', ms=7)
        ax.plot(*EE[-1].T, '*r', ms=7)
예제 #2
0
def gen_ensemble_sample(model, nSamples, nEnsemble, SpinUp, Spacing):
    simulator = modelling.with_recursion(model.step, prog="Simulating")
    K = SpinUp + nSamples * Spacing
    Nx = np.prod(shape)  # total state length
    init = np.random.normal(loc=0.0, scale=0.1, size=[nEnsemble, Nx])
    sample = simulator(init, K, 0.0, model.prms["dtout"])
    return sample[SpinUp::Spacing, :, :]
예제 #3
0
파일: demo.py 프로젝트: yumengch/DAPPER
"""Demonstrate the Lotka-Volterra model."""

import numpy as np
from matplotlib import pyplot as plt

import dapper.mods as modelling
from dapper.mods.LotkaVolterra import step, x0

simulator = modelling.with_recursion(step, prog="Simulating")

dt = 0.7
K = int(1 * 10**3 / dt)
xx = simulator(x0, K, t0=0, dt=dt)

fig, ax = plt.subplots(figsize=(9, 6))
ax.plot(np.linspace(0, K * dt, K + 1), xx)

plt.show()
예제 #4
0
파일: demo.py 프로젝트: yumengch/DAPPER
"""Demonstrate the Double-Pendulum model."""
# https://en.wikipedia.org/wiki/Double_pendulum

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
from numpy import cos, sin

import dapper.mods as modelling
from dapper.mods.DoublePendulum import L1, L2, step, x0

E0 = x0 + 0.01 * np.random.randn(3, 4)
simulator = modelling.with_recursion(step)
dt = 0.01
EE = simulator(E0, k=10**4, t0=0, dt=dt)

# Energy evolution. Should be constant, but for numerical errors:
# plt.plot(energy(EE).T)

fig = plt.figure()
ax = fig.add_subplot(111, autoscale_on=False, xlim=(-2, 2), ylim=(-2, 2))

lines = []
for _ in E0:
    lines += [ax.plot([], [], 'o-', lw=2)[0]]

# Need to hide text handle among "lines"
time_template = 'time = %.1fs'
lines += [ax.text(0.05, 0.9, '', transform=ax.transAxes)]

예제 #5
0
    from matplotlib import pyplot as plt

    import dapper.mods as modelling
    from dapper.mods.LorenzUV.lorenz96 import LUV

    # Setup
    sd0 = modelling.set_seed(4)
    # from dapper.mods.LorenzUV.wilks05 import LUV
    nU, J = LUV.nU, LUV.J

    dt = 0.005
    t0 = np.nan
    K  = int(10/dt)

    step_1 = modelling.with_rk4(LUV.dxdt, autonom=True)
    step_K = modelling.with_recursion(step_1, prog=1)

    x0 = 0.01*np.random.randn(LUV.M)
    x0 = step_K(x0, int(2/dt), t0, dt)[-1]  # BurnIn
    xx = step_K(x0, K, t0, dt)

    # Grab parts of state vector
    ii = np.arange(nU+1)
    jj = np.arange(nU*J+1)
    circU = np.mod(ii, nU)
    circV = np.mod(jj, nU*J) + nU
    iU = np.hstack([0, 0.5+np.arange(nU), nU])

    def Ui(xx):
        interp = (xx[0]+xx[-1])/2
        return np.hstack([interp, xx, interp])
예제 #6
0
    ii    = np.random.choice(np.arange(Nx), 100, False)
    T     = 1000.0
    dt    = model.prms['dtout']
    x0    = np.load(sample_filename)['sample'][-1]
    eps   = 0.01  # ensemble rescaling
    N     = 300


########################
# Reference trajectory
########################
# NB: Arbitrary, coz models are autonom. But dont use nan coz QG doesn't like it.
t0 = 0.0
K  = int(round(T/dt))       # Num of time steps.
tt = np.linspace(dt, T, K)  # Time seq.
x  = with_recursion(step, prog="BurnIn")(x0, int(10/dt), t0, dt)[-1]
xx = with_recursion(step, prog="Reference")(x, K, t0, dt)


########################
# ACF
########################
# NB: Won't work with QG (too big, and BCs==0).
fig, ax = freshfig(4)
if "ii" not in locals():
    ii = np.arange(min(100, Nx))
if "nlags" not in locals():
    nlags = min(100, K-1)
ax.plot(tt[:nlags], np.nanmean(
    series.auto_cov(xx[:nlags, ii], nlags=nlags-1, corr=1),
    axis=1))
예제 #7
0
    if PRMS == 'Wilks':
        from dapper.mods.LorenzUV.wilks05 import LUV
    else:
        from dapper.mods.LorenzUV.lorenz96 import LUV
    nU = LUV.nU

    K = 400
    dt = 0.005
    t0 = np.nan

    modelling.set_seed(30)  # 3 5 7 13 15 30
    x0 = np.random.randn(LUV.M)

    true_step = with_rk4(LUV.dxdt, autonom=True)
    model_step = with_rk4(LUV.dxdt_trunc, autonom=True)
    true_K = with_recursion(true_step, prog=1)

    ###########################
    # Compute truth trajectory
    ###########################
    x0 = true_K(x0, int(2 / dt), t0, dt)[-1]  # BurnIn
    xx = true_K(x0, K, t0, dt)

    ###########################
    # Compute unresovled scales
    ###########################
    gg = np.zeros((K, nU))  # "Unresolved tendency"
    for k, x in enumerate(xx[:-1]):
        X = x[:nU]
        Z = model_step(X, t0, dt)
        D = Z - xx[k + 1, :nU]
예제 #8
0
def gen_sample(model, nSamples, SpinUp, Spacing):
    simulator = modelling.with_recursion(model.step, prog="Simulating")
    K         = SpinUp + nSamples*Spacing
    Nx        = np.prod(shape)  # total state length
    sample    = simulator(np.zeros(Nx), K, 0.0, model.prms["dtout"])
    return sample[SpinUp::Spacing]
예제 #9
0
import numpy as np
from matplotlib import pyplot as plt

import dapper.mods as modelling
from dapper.mods.LorenzUV.lorenz96 import LUV
from dapper.tools.progressbar import progbar
from dapper.tools.viz import setup_wrapping

nU, J = LUV.nU, LUV.J

dt = 0.005
K = int(4 / dt)

step_1 = modelling.with_rk4(LUV.dxdt, autonom=True)
step_K = modelling.with_recursion(step_1, prog='Simulating')

xx = step_K(LUV.x0, K, np.nan, dt)

# Grab parts of state vector
ii, wrapU = setup_wrapping(nU)
jj, wrapV = setup_wrapping(nU * J)

# Animate linear
plt.figure()
lhU = plt.plot(ii, wrapU(xx[-1, :nU]), 'b', lw=3)[0]
lhV = plt.plot(jj / J, wrapV(xx[-1, nU:]), 'g', lw=2)[0]
for k in progbar(range(K), 'Plotting'):
    lhU.set_ydata(wrapU(xx[k, :nU]))
    lhV.set_ydata(wrapV(xx[k, nU:]))
    plt.pause(0.001)