Ejemplo n.º 1
0
def test_time_series():
    # write the data
    filename = "out.xdmf"

    writer = meshio.XdmfTimeSeriesWriter(filename)
    writer.write_points_cells(helpers.tri_mesh_2d.points,
                              helpers.tri_mesh_2d.cells)
    n = helpers.tri_mesh_2d.points.shape[0]

    times = numpy.linspace(0.0, 1.0, 5)
    point_data = [{"phi": numpy.full(n, t)} for t in times]
    for t, pd in zip(times, point_data):
        writer.write_data(t,
                          point_data=pd,
                          cell_data={"triangle": {
                              "a": [3.0, 4.2]
                          }})

    # read it back in
    reader = meshio.XdmfTimeSeriesReader(filename)
    points, cells = reader.read_points_cells()
    for k in range(reader.num_steps):
        t, pd, cd = reader.read_data(k)
        assert numpy.abs(times[k] - t) < 1.0e-12
        for key, value in pd.items():
            assert numpy.all(numpy.abs(value - point_data[k][key]) < 1.0e-12)

    return
Ejemplo n.º 2
0
def read_time_series(filename):
    """
    Read time series from XDMF file.

    Parameters
    ----------
    filename : str
        Input file name.

    Returns
    -------
    list of namedtuple (type, data)
        Grid cell data.
    list of dict
        Data associated to grid points for each time step.
    list of dict
        Data associated to grid cells for each time step.
    array_like
        Time step values.

    """
    from ._common import get_meshio_version, get_new_meshio_cells

    if not isinstance(filename, str):
        raise ValueError()

    point_data, cell_data, time_steps = [], [], []
    if get_meshio_version() < (3,):
        reader = meshio.XdmfTimeSeriesReader(filename)
        points, cells = reader.read_points_cells()

        for k in range(reader.num_steps):
            t, pdata, cdata = reader.read_data(k)

            _, cdata = get_new_meshio_cells(cells, cdata)
            point_data.append(pdata)
            cell_data.append(cdata)
            time_steps.append(t)

        cells = get_new_meshio_cells(cells)
    else:
        with meshio.xdmf.TimeSeriesReader(filename) as reader:
            points, cells = reader.read_points_cells()

            for k in range(reader.num_steps):
                t, pdata, cdata = reader.read_data(k)
                point_data.append(pdata)
                cell_data.append(cdata)
                time_steps.append(t)

    # Concatenate cell data arrays
    for cdata in cell_data:
        for k in cdata.keys():
            cdata[k] = numpy.concatenate(cdata[k])

    return points, cells, point_data, cell_data, time_steps