예제 #1
0
def test_storage_persistence(compression, tmp_path):
    """test writing to persistent trackers"""
    dim = 5
    grid = UnitGrid([dim])
    field = ScalarField(grid)

    path = tmp_path / f"test_storage_persistence_{compression}.hdf5"

    # write some data
    for write_mode in ["append", "truncate_once", "truncate"]:
        storage = FileStorage(path,
                              info={"a": 1},
                              write_mode=write_mode,
                              compression=compression)

        # first batch
        storage.start_writing(field, info={"b": 2})
        field.data = np.arange(dim)
        storage.append(field, 0)
        field.data = np.arange(dim, 2 * dim)
        storage.append(field)
        storage.end_writing()

        # read first batch
        np.testing.assert_array_equal(storage.times, np.arange(2))
        np.testing.assert_array_equal(np.ravel(storage.data), np.arange(10))
        assert {"a": 1, "b": 2}.items() <= storage.info.items()

        # second batch
        storage.start_writing(field, info={"c": 3})
        field.data = np.arange(2 * dim, 3 * dim)
        storage.append(field, 2)
        storage.end_writing()

        storage.close()

        # read the data
        storage = FileStorage(path)
        if write_mode == "truncate":
            np.testing.assert_array_equal(storage.times, np.array([2]))
            np.testing.assert_array_equal(np.ravel(storage.data),
                                          np.arange(10, 15))
            assert storage.shape == (1, 5)
            info = {"c": 3}
            assert info.items() <= storage.info.items()
        else:
            np.testing.assert_array_equal(storage.times, np.arange(0, 3))
            np.testing.assert_array_equal(np.ravel(storage.data),
                                          np.arange(0, 15))
            assert storage.shape == (3, 5)
            info = {"a": 1, "b": 2, "c": 3}
            assert info.items() <= storage.info.items()
예제 #2
0
def test_pde_poisson_solver_1d():
    """test the poisson solver on 1d grids"""
    # solve Laplace's equation
    grid = UnitGrid([4])
    res = solve_laplace_equation(grid, bc=[{"value": -1}, {"value": 3}])
    np.testing.assert_allclose(res.data, grid.axes_coords[0] - 1)

    res = solve_laplace_equation(grid, bc=[{"value": -1}, {"derivative": 1}])
    np.testing.assert_allclose(res.data, grid.axes_coords[0] - 1)

    # test Poisson equation with 2nd Order BC
    res = solve_laplace_equation(grid, bc=[{"value": -1}, "extrapolate"])

    # solve Poisson's equation
    grid = CartesianGrid([[0, 1]], 4)
    field = ScalarField(grid, data=1)

    res = solve_poisson_equation(field, bc=[{"value": 1}, {"derivative": 1}])
    xs = grid.axes_coords[0]
    np.testing.assert_allclose(res.data, 1 + 0.5 * xs**2, rtol=1e-2)

    # test inconsistent problem
    field.data = 1
    with pytest.raises(RuntimeError, match="Neumann"):
        solve_poisson_equation(field, {"derivative": 0})
def test_collections_copy():
    """ test copying data of collections """
    grid = UnitGrid([2, 2])
    sf = ScalarField(grid, 0)
    vf = VectorField(grid, 1)
    fc = FieldCollection([sf, vf])

    data = np.r_[np.zeros(4), np.ones(8)]
    np.testing.assert_allclose(fc.data.flat, data)

    fc2 = fc.copy()
    assert fc.data is not fc2.data
    assert fc[0].data is not fc2[0].data
    assert fc[1].data is not fc2[1].data

    sf.data = 1
    np.testing.assert_allclose(fc.data.flat, np.ones(12))
    np.testing.assert_allclose(fc2.data.flat, data)

    # special case
    fc = FieldCollection([sf, sf])
    fc[0] = 2
    np.testing.assert_allclose(fc[0].data, 2)
    np.testing.assert_allclose(fc[1].data, 1)
예제 #4
0
def test_storage_persistence(collection, tmp_path):
    """test writing to persistent trackers"""
    dim = 5
    grid = UnitGrid([dim])
    scalar = ScalarField(grid)
    vector = pde.VectorField(grid)
    if collection:
        state = pde.FieldCollection([scalar, vector])
    else:
        state = scalar

    def assert_storage_content(storage, expect):
        """helper function testing storage content"""
        if collection:
            for i in range(2):
                field_data = storage.extract_field(i).data
                np.testing.assert_array_equal(np.ravel(field_data), expect)
        else:
            np.testing.assert_array_equal(np.ravel(storage.data), expect)

    path = tmp_path / f"test_storage_persistence_{collection}.hdf5"

    # write some data
    for write_mode in ["append", "truncate_once", "truncate"]:
        with FileStorage(path, info={"a": 1}, write_mode=write_mode) as writer:

            # first batch
            writer.start_writing(state, info={"b": 2})
            scalar.data = np.arange(dim)
            vector.data[:] = np.arange(dim)
            writer.append(state, 0)
            scalar.data = np.arange(dim, 2 * dim)
            vector.data[:] = np.arange(dim, 2 * dim)
            writer.append(state)
            writer.end_writing()

            # read first batch
            np.testing.assert_array_equal(writer.times, np.arange(2))
            assert_storage_content(writer, np.arange(10))
            assert {"a": 1, "b": 2}.items() <= writer.info.items()

            # second batch
            writer.start_writing(state, info={"c": 3})
            scalar.data = np.arange(2 * dim, 3 * dim)
            vector.data[:] = np.arange(2 * dim, 3 * dim)
            writer.append(state, 2)
            writer.end_writing()

        # read the data
        with FileStorage(path) as reader:
            if write_mode == "truncate":
                np.testing.assert_array_equal(reader.times, np.array([2]))
                assert_storage_content(reader, np.arange(10, 15))
                assert reader.shape == (1, 2, 5) if collection else (1, 5)
                info = {"c": 3}
                assert info.items() <= reader.info.items()

            else:
                np.testing.assert_array_equal(reader.times, np.arange(3))
                assert_storage_content(reader, np.arange(15))
                assert reader.shape == (3, 2, 5) if collection else (3, 5)
                info = {"a": 1, "b": 2, "c": 3}
                assert info.items() <= reader.info.items()