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
def write_time_series( filename, points, cells, point_data=None, cell_data=None, time_steps=None, ): """ Write time series given points and cells data. Parameters ---------- filename : str Output file name. points : ndarray Grid points array. cells : list of namedtuple (type, data) Grid cell data. point_data : list of dict or None, optional, default None Data associated to grid points for each time step. cell_data : list of dict or None, optional, default None Data associated to grid cells for each time step. time_steps : array_like, optional, default None Time step values. """ from ._common import get_meshio_version, get_old_meshio_cells if not isinstance(filename, str): raise TypeError() if point_data is not None and not isinstance(point_data, (list, tuple)): raise TypeError() if cell_data is not None and not isinstance(cell_data, (list, tuple)): raise TypeError() if time_steps is not None and not isinstance( time_steps, (list, tuple, numpy.ndarray) ): raise TypeError() if not (point_data or cell_data): raise ValueError("Provide at least point_data or cell_data.") else: nt = len(point_data) if point_data else len(cell_data) if point_data and len(point_data) != nt: raise ValueError("Inconsistent number of point data.") if cell_data and len(cell_data) != nt: raise ValueError("Inconsistent number of cell data.") if time_steps is not None and len(time_steps) != nt: raise ValueError("Inconsistent number of time steps.") point_data = point_data if point_data else [{}] * nt cell_data = cell_data if cell_data else [{}] * nt time_steps = time_steps if time_steps is not None else list(range(nt)) # Split cell data arrays sizes = numpy.cumsum([len(c.data) for c in cells[:-1]]) cell_data = [ {k: numpy.split(v, sizes) for k, v in cdata.items()} for cdata in cell_data ] # Sort data with time steps idx = numpy.argsort(time_steps) point_data = [point_data[i] for i in idx] cell_data = [cell_data[i] for i in idx] time_steps = [time_steps[i] for i in idx] # Write XDMF def write_data(writer, points, cells, point_data, cell_data, time_steps): writer.write_points_cells(points, cells) for tstep, pdata, cdata in zip(time_steps, point_data, cell_data): writer.write_data(tstep, point_data=pdata, cell_data=cdata) if get_meshio_version() < (3,): writer = meshio.XdmfTimeSeriesWriter(filename) tmp = [get_old_meshio_cells(cells, cdata) for cdata in cell_data] cells = tmp[0][0] cell_data = [cell[1] for cell in tmp] write_data(writer, points, cells, point_data, cell_data, time_steps) else: with meshio.xdmf.TimeSeriesWriter(filename) as writer: write_data(writer, points, cells, point_data, cell_data, time_steps)