107 raise ValueError(
"number of pieces must be >= 1")
109 reader = vtk.vtkXMLUnstructuredGridReader()
110 reader.SetFileName(input_path)
111 if not reader.CanReadFile(input_path):
112 raise RuntimeError(f
"cannot read {input_path}")
114 grid = reader.GetOutput()
116 number_of_cells = grid.GetNumberOfCells()
117 if number_of_cells == 0:
118 raise RuntimeError(f
"{input_path} has no cells")
119 pieces = min(pieces, number_of_cells)
121 output_dir = os.path.dirname(os.path.abspath(output_path))
122 os.makedirs(output_dir, exist_ok=
True)
123 stem = os.path.splitext(os.path.basename(output_path))[0]
126 for p
in range(pieces):
127 lo = number_of_cells * p // pieces
128 hi = number_of_cells * (p + 1) // pieces
130 extract = vtk.vtkExtractCells()
131 extract.SetInputData(grid)
132 extract.AddCellRange(lo, hi - 1)
135 piece = extract.GetOutput()
138 for name
in (
"vtkOriginalCellIds",):
139 if piece.GetCellData().HasArray(name):
140 piece.GetCellData().RemoveArray(name)
141 for name
in (
"vtkOriginalPointIds",):
142 if piece.GetPointData().HasArray(name):
143 piece.GetPointData().RemoveArray(name)
145 source = f
"{stem}_{p}.vtu"
146 sources.append(source)
147 writer = vtk.vtkXMLUnstructuredGridWriter()
148 writer.SetInputData(piece)
149 writer.SetFileName(os.path.join(output_dir, source))
152 writer.SetDataModeToBinary()
154 writer.SetCompressorTypeToZLib()
156 writer.SetCompressorTypeToNone()
158 print(f
"piece {p}: cells [{lo}, {hi}) -> {source}", flush=
True)
161 print(f
"wrote {output_path} with {pieces} pieces ({number_of_cells} cells total)")
165 parser = argparse.ArgumentParser(description=__doc__,
166 formatter_class=argparse.RawDescriptionHelpFormatter)
167 parser.add_argument(
"input", help=
"single-piece .vtu snapshot")
168 parser.add_argument(
"output", help=
"output .pvtu summary path")
169 parser.add_argument(
"-k",
"--pieces", type=int, default=8,
170 help=
"number of pieces to split into (default: 8)")
171 parser.add_argument(
"--no-compress", action=
"store_true",
172 help=
"write uncompressed piece files")
173 args = parser.parse_args(argv)
175 repartition(args.input, args.output, args.pieces, compress=
not args.no_compress)