Ejemplo n.º 1
0
def demo(u, s0, N, as_points, ends):
    core.u = u

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

    # Computations
    simulator = dpr.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)
Ejemplo n.º 2
0
"""Demonstrate the Lorenz-96 model."""

# For a deeper introduction, see
# "DA-tutorials/T4 - Dynamical systems, chaos, Lorenz.ipynb"

from matplotlib import pyplot as plt
from numpy import eye

import dapper as dpr
from dapper.mods.Lorenz96 import step, x0
from dapper.tools.viz import amplitude_animation

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

x0 = x0(40)
E0 = x0 + 1e-3*eye(len(x0))[:3]

dt = 0.05
xx = simulator(E0, k=500, t=0, dt=dt)

ani = amplitude_animation(xx, dt=dt, interval=70)

plt.show()
Ejemplo n.º 3
0
def gen_sample(model, nSamples, SpinUp, Spacing):
    simulator = dpr.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]
Ejemplo n.º 4
0
import numpy as np
from matplotlib import pyplot as plt

import dapper as dpr
from dapper.mods.LorenzUV.lorenz96 import LUV
from dapper.tools.utils import progbar
from dapper.tools.viz import setup_wrapping

nU, J = LUV.nU, LUV.J

dt = 0.005
K  = int(4/dt)

step_1 = dpr.with_rk4(LUV.dxdt, autonom=True)
step_K = dpr.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)
Ejemplo n.º 5
0
import dapper as dpr
from dapper.mods.LorenzUV.lorenz96 import LUV
from dapper.tools.math import ccat

# Setup
sd0 = dpr.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 = dpr.with_rk4(LUV.dxdt, autonom=True)
step_K = dpr.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
Ejemplo n.º 6
0
"""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 as dpr
from dapper.mods.DoublePendulum import L1, L2, step, x0

E0 = x0 + 0.01 * np.random.randn(3, 4)
simulator = dpr.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 x 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)]

Ejemplo n.º 7
0
    print('--> use noisy/sparse observations')

#List of values for the used paramters (should in a list)
lparam = {k: params.get(k, [default_param[k]]) for k in used_paramter}

#Sequence of all the combination of the parameters
seq_param = ParameterGrid(lparam)

#Load the model
dt = model['dt']
model_module = __import__(model['model_module'])

if model['type'] == 'physical':

    HMM_trunc = getattr(model_module, model['model_name'])
    trunc_model = with_recursion(HMM_trunc.Dyn.model)

else:
    raise NotImplementedError("Only physical is available as model type")

nn = len(seq_param)
for i, dparam in enumerate(seq_param):
    print('Experiment {}/{}:'.format(i + 1, nn), dparam)
    #Load True simulation
    data_truth = np.load(file_truth.format(**dparam))
    xx = data_truth['xx']
    dt_full = data_truth['dt']
    T = data_truth['T']
    dtObs = dparam['dtObs']

    #Modify chrono