Esempio n. 1
0
def test_continuous_initial_condition_1d_pde():
    diff_eq = DiffusionEquation(1)
    mesh = Mesh([(0., 20.)], [.1])
    bcs = [(DirichletBoundaryCondition(lambda x, t: np.zeros((len(x), 1)),
                                       is_static=True),
            DirichletBoundaryCondition(lambda x, t: np.full((len(x), 1), 1.5),
                                       is_static=True))]
    cp = ConstrainedProblem(diff_eq, mesh, bcs)
    initial_condition = ContinuousInitialCondition(
        cp, lambda x: np.exp(-np.square(np.array(x) - 10.) / (2 * 5**2)))

    assert np.isclose(initial_condition.y_0(np.full((1, 1), 10.)), 1.)
    assert np.isclose(
        initial_condition.y_0(np.full((1, 1),
                                      np.sqrt(50) + 10.)), np.e**-1)
    assert np.allclose(initial_condition.y_0(np.full((5, 1), 10.)),
                       np.ones((5, 1)))

    y_0_vertices = initial_condition.discrete_y_0(True)
    assert y_0_vertices.shape == (201, 1)
    assert y_0_vertices[0, 0] == 0.
    assert y_0_vertices[-1, 0] == 1.5
    assert y_0_vertices[100, 0] == 1.
    assert np.all(0. < y_0_vertices[1:100, 0]) \
        and np.all(y_0_vertices[1:100, 0] < 1.)
    assert np.all(0. < y_0_vertices[101:-1, 0]) \
        and np.all(y_0_vertices[101:-1, 0] < 1.)

    y_0_cell_centers = initial_condition.discrete_y_0(False)
    assert y_0_cell_centers.shape == (200, 1)
    assert np.all(0. < y_0_cell_centers) and np.all(y_0_cell_centers < 1.)
Esempio n. 2
0
def test_continuous_initial_condition_ode():
    diff_eq = LotkaVolterraEquation()
    cp = ConstrainedProblem(diff_eq)
    initial_condition = ContinuousInitialCondition(
        cp, lambda _: np.array([10., 100.]))

    assert np.all(initial_condition.y_0(None) == [10., 100.])
    assert np.all(initial_condition.discrete_y_0() == [10., 100.])