Ejemplo n.º 1
0
def test_jac_simpl():
    model = Model("dxxU", "U")
    model_simp = Model("dxxU", "U", simplify=True)
    x = np.linspace(0, 2 * np.pi, 50, endpoint=False)
    U = np.cos(x)
    assert np.isclose(
        model.J(model.fields_template(x=x, U=U), dict(periodic=True)).todense(),
        model.J(model_simp.fields_template(x=x, U=U), dict(periodic=True)).todense(),
    ).all()
Ejemplo n.º 2
0
def test_jac_simpl():
    model = Model("dxxU", "U")
    model_simp = Model("dxxU", "U", simplify=True)
    x = np.linspace(0, 2 * np.pi, 50, endpoint=False)
    U = np.cos(x)
    assert np.isclose(
        model.J(model.fields_template(x=x, U=U),
                dict(periodic=True)).todense(),
        model.J(model_simp.fields_template(x=x, U=U),
                dict(periodic=True)).todense(),
    ).all()
Ejemplo n.º 3
0
def test_routines_api():
    x = np.linspace(0, 10, 100)
    U = np.cos(x * 2 * np.pi / 10)
    model = Model(differential_equations="dxxU",
                  dependent_variables="U")
    fields = model.fields_template(x=x, U=U)
    F = model.F
    print(F)
    F(fields, dict(periodic=True))
    F.diff_approx(fields, dict(periodic=True))
    J = model.J
    print(J)
    J(fields, dict(periodic=True))
Ejemplo n.º 4
0
def test_upwind(compiler, uporder, vel, periodic):
    model = Model(
        differential_equations=["upwind(%s, U, %i)" % (vel, uporder)],
        dependent_variables="U",
        parameters="k",
        help_functions="s",
        compiler=compiler,
    )

    x, dx = np.linspace(0, 10, 100, retstep=True, endpoint=False)
    U = np.cos(x * 2 * np.pi / 10)
    s = np.zeros_like(x)
    fields = model.fields_template(x=x, U=U, s=s)
    parameters = dict(periodic=periodic, k=1)
    model.F(fields, parameters)
    model.J(fields, parameters)
Ejemplo n.º 5
0
def test_upwind(compiler, uporder, vel, periodic):
    model = Model(
        differential_equations=["upwind(%s, U, %i)" % (vel, uporder)],
        dependent_variables="U",
        parameters="k",
        help_functions="s",
        compiler=compiler,
    )

    x, dx = np.linspace(0, 10, 100, retstep=True, endpoint=False)
    U = np.cos(x * 2 * np.pi / 10)
    s = np.zeros_like(x)
    fields = model.fields_template(x=x, U=U, s=s)
    parameters = dict(periodic=periodic, k=1)
    model.F(fields, parameters)
    model.J(fields, parameters)
Ejemplo n.º 6
0
def test_fields_api():
    model = Model(differential_equations=["dxxU1", "dxxU2"],
                  dependent_variables=["U1", "U2"],
                  help_functions='s')
    x = np.linspace(0, 1, 100)
    U1 = np.cos(x)
    U2 = np.sin(x)
    s = np.sin(x)
    fields = model.fields_template(x=x, U1=U1, U2=U2, s=s)
    print(fields)
    assert np.isclose(fields.uflat, np.vstack([U1, U2]).flatten('F')).all()
    assert np.isclose(fields.uarray.to_array(), np.vstack([U1, U2])).all()
    assert np.isclose(fields["x"], x).all()
    assert np.isclose(fields["U1"], U1).all()
    assert np.isclose(fields["U2"], U2).all()
    assert np.isclose(fields["s"], s).all()
    assert fields.size == x.size
Ejemplo n.º 7
0
def test_fields_api():
    model = Model(differential_equations=["dxxU1", "dxxU2"],
                  dependent_variables=["U1", "U2"], help_functions='s')
    x = np.linspace(0, 1, 100)
    U1 = np.cos(x)
    U2 = np.sin(x)
    s = np.sin(x)
    fields = model.fields_template(x=x, U1=U1, U2=U2, s=s)
    print(fields)
    assert np.isclose(fields.uflat,
                      np.vstack([U1, U2]).flatten('F')).all()
    assert np.isclose(fields.uarray.to_array(),
                      np.vstack([U1, U2])).all()
    assert np.isclose(fields["x"], x).all()
    assert np.isclose(fields["U1"], U1).all()
    assert np.isclose(fields["U2"], U2).all()
    assert np.isclose(fields["s"], s).all()
    assert fields.size == x.size
Ejemplo n.º 8
0
def test_model_monovariate(func, var, par, k, compiler):
    model = Model(func, var, par, compiler=compiler)
    x, dx = np.linspace(0, 10, 100, retstep=True, endpoint=False)
    U = np.cos(x * 2 * np.pi / 10)
    fields = model.fields_template(x=x, U=U)
    parameters = dict(periodic=True, k=k)
    F = model.F(fields, parameters)
    J_sparse = model.J(fields, parameters)
    J_dense = model.J(fields, parameters, sparse=False)
    J_approx = model.F.diff_approx(fields, parameters)

    dxU = np.gradient(np.pad(U, 2, mode="wrap")) / dx
    dxxU = np.gradient(dxU) / dx

    dxU = dxU[2:-2]
    dxxU = dxxU[2:-2]

    assert np.isclose(F, k * dxxU, rtol=1E-2).all()
    assert np.isclose(J_approx, J_sparse.todense(), rtol=1E-2).all()
    assert np.isclose(J_approx, J_dense, rtol=1E-2).all()
Ejemplo n.º 9
0
def test_model_monovariate(func, var, par, k, compiler):
    model = Model(func, var, par, compiler=compiler)
    x, dx = np.linspace(0, 10, 100, retstep=True, endpoint=False)
    U = np.cos(x * 2 * np.pi / 10)
    fields = model.fields_template(x=x, U=U)
    parameters = dict(periodic=True, k=k)
    F = model.F(fields, parameters)
    J_sparse = model.J(fields, parameters)
    J_dense = model.J(fields, parameters, sparse=False)
    J_approx = model.F.diff_approx(fields, parameters)

    dxU = np.gradient(np.pad(U, 2, mode="wrap")) / dx
    dxxU = np.gradient(dxU) / dx

    dxU = dxU[2:-2]
    dxxU = dxxU[2:-2]

    assert np.isclose(F, k * dxxU, rtol=1E-2).all()
    assert np.isclose(J_approx, J_sparse.todense(), rtol=1E-2).all()
    assert np.isclose(J_approx, J_dense, rtol=1E-2).all()
Ejemplo n.º 10
0
def test_model_api(compiler, periodic):
    model = Model(
        differential_equations=["k * dxxU + s"],
        dependent_variables="U",
        parameters="k",
        help_functions="s",
        compiler=compiler,
    )
    assert set(model._args) == set(
        ["x", "U_m1", "U", "U_p1", "s_m1", "s", "s_p1", "k", "dx"])
    with pytest.raises(NotImplementedError):
        Model("dxxxxxU", "U")
    with pytest.raises(ValueError):
        Model("dxxx(dx)", "U")
    x, dx = np.linspace(0, 10, 100, retstep=True, endpoint=False)
    U = np.cos(x * 2 * np.pi / 10)
    s = np.zeros_like(x)
    fields = model.fields_template(x=x, U=U, s=s)
    parameters = dict(periodic=periodic, k=1)
    model.F(fields, parameters)
    model.J(fields, parameters)
Ejemplo n.º 11
0
def test_model_api(compiler, periodic):
    model = Model(
        differential_equations=["k * dxxU + s"],
        dependent_variables="U",
        parameters="k",
        help_functions="s",
        compiler=compiler,
    )
    assert set(model._args) == set(
        ["x", "U_m1", "U", "U_p1", "s_m1", "s", "s_p1", "k", "dx"]
    )
    with pytest.raises(NotImplementedError):
        Model("dxxxxxU", "U")
    with pytest.raises(ValueError):
        Model("dxxx(dx)", "U")
    x, dx = np.linspace(0, 10, 100, retstep=True, endpoint=False)
    U = np.cos(x * 2 * np.pi / 10)
    s = np.zeros_like(x)
    fields = model.fields_template(x=x, U=U, s=s)
    parameters = dict(periodic=periodic, k=1)
    model.F(fields, parameters)
    model.J(fields, parameters)
Ejemplo n.º 12
0
def test_model_bivariate():
    model = Model(["k1 * dxx(v)", "k2 * dxx(u)"], ["u", "v"], ["k1", "k2"])
    x, dx = np.linspace(0, 10, 50, retstep=True, endpoint=False)
    u = np.cos(x * 2 * np.pi / 10)
    v = np.sin(x * 2 * np.pi / 10)
    fields = model.fields_template(x=x, u=u, v=v)
    parameters = dict(periodic=True, k1=1, k2=1)
    F = model.F(fields, parameters)
    J_sparse = model.J(fields, parameters)
    J_dense = model.J(fields, parameters, sparse=False)
    J_approx = model.F.diff_approx(fields, parameters, eps=1E-3)

    dxu = np.gradient(np.pad(u, 2, mode="wrap")) / dx
    dxxu = np.gradient(dxu) / dx
    dxu = dxu[2:-2]
    dxxu = dxxu[2:-2]

    dxv = np.gradient(np.pad(v, 2, mode="wrap")) / dx
    dxxv = np.gradient(dxv) / dx
    dxv = dxv[2:-2]
    dxxv = dxxv[2:-2]
    assert np.isclose(F, np.vstack([dxxv, dxxu]).flatten("F"), rtol=1E-2).all()
    assert np.isclose(J_approx, J_sparse.todense(), rtol=1E-4).all()
    assert np.isclose(J_approx, J_dense, rtol=1E-4).all()
Ejemplo n.º 13
0
def test_model_bivariate():
    model = Model(["k1 * dxx(v)", "k2 * dxx(u)"], ["u", "v"], ["k1", "k2"])
    x, dx = np.linspace(0, 10, 50, retstep=True, endpoint=False)
    u = np.cos(x * 2 * np.pi / 10)
    v = np.sin(x * 2 * np.pi / 10)
    fields = model.fields_template(x=x, u=u, v=v)
    parameters = dict(periodic=True, k1=1, k2=1)
    F = model.F(fields, parameters)
    J_sparse = model.J(fields, parameters)
    J_dense = model.J(fields, parameters, sparse=False)
    J_approx = model.F.diff_approx(fields, parameters, eps=1E-3)

    dxu = np.gradient(np.pad(u, 2, mode="wrap")) / dx
    dxxu = np.gradient(dxu) / dx
    dxu = dxu[2:-2]
    dxxu = dxxu[2:-2]

    dxv = np.gradient(np.pad(v, 2, mode="wrap")) / dx
    dxxv = np.gradient(dxv) / dx
    dxv = dxv[2:-2]
    dxxv = dxxv[2:-2]
    assert np.isclose(F, np.vstack([dxxv, dxxu]).flatten("F"), rtol=1E-2).all()
    assert np.isclose(J_approx, J_sparse.todense(), rtol=1E-4).all()
    assert np.isclose(J_approx, J_dense, rtol=1E-4).all()
Ejemplo n.º 14
0
import numpy as np
import pylab as pl

from triflow import Model, Simulation

model = Model("k * dxxU - c * dxU", "U", ["k", "c"])

x, dx = np.linspace(0, 1, 200, retstep=True)
U = np.cos(2 * np.pi * x * 5)
fields = model.fields_template(x=x, U=U)

parameters = dict(c=.03, k=.001, dx=dx, periodic=False)

t = 0
dt = 5E-1
tmax = 2.5

pl.plot(fields.x, fields.U, label='t: %g' % t)


def dirichlet_condition(t, fields, pars):
    fields.U[0] = 1
    fields.U[-1] = 0
    return fields, pars


simul = Simulation(model,
                   fields,
                   parameters,
                   dt,
                   hook=dirichlet_condition,
Ejemplo n.º 15
0
import numpy as np
import pylab as pl

from triflow import Model, schemes

model = Model("k * dxxU - c * dxU",
              "U", ["k", "c"])

x, dx = np.linspace(0, 1, 200, retstep=True)
U = np.cos(2 * np.pi * x * 5)
fields = model.fields_template(x=x, U=U)

pl.plot(fields.x, fields.U)

parameters = dict(c=.03, k=.001, dx=dx, periodic=True)

t = 0
dt = 1E-1

scheme = schemes.Theta(model, theta=.5)  # Crank-Nicolson scheme

new_t, new_fields = scheme(t, fields, dt, parameters)

pl.plot(new_fields.x, new_fields.U)
pl.xlim(0, 1)
pl.show()