예제 #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()
예제 #2
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()
예제 #3
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()
예제 #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)
예제 #5
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)