Commit 55a1ba86 authored by Lukas Riedel's avatar Lukas Riedel

added _read_pvtu function to VTKFile to read parallel collection files

parent dc3672b9
......@@ -12,6 +12,22 @@ class BaseGrid(object):
_cellValues = () #: values of the cell variables
_dataDict = None #: dictionary holding variable data as key-value pairs
def merge(self, other):
"""
Merge two grids into a new grid instance
"""
if self.dim != other.dim:
raise RuntimeError("Grid dimensions do not match!")
new = self._merge_derived(other)
for var in self._cellVariables:
data = np.append(self.data()[var],other.data()[var],axis=0)
new.add_data(var,data)
return new
def cellCenters(self):
"""
:returns: The cell centers of the grid.
......@@ -56,6 +72,10 @@ class BaseGrid(object):
def _calcCellCenters(self):
return NotImplementedError
def _merge_derived(self,other):
return NotImplementedError
class RegularGrid(BaseGrid):
"""
Class implementing a grid interface for given cell centers on a regular grid.
......@@ -76,6 +96,12 @@ class RegularGrid(BaseGrid):
self._cellCenters = cellCenters
def _merge_derived(self,other):
"""
Merge this grid with another into a new regular grid instance
"""
cellCenters = np.append(self.cellCenters(),other.cellCenters(),axis=0)
return RegularGrid(cellCenters)
class UnstructuredVTKGrid(BaseGrid):
......@@ -124,6 +150,18 @@ class UnstructuredVTKGrid(BaseGrid):
self._numCells = int(len(self.connectivity) / self.ppc)
def _merge_derived(self, other):
"""
Merge this grid with another into a new unstructured grid instance
"""
if self.ppc != other.ppc:
raise RuntimeError("Using different numbers of points per grid cell")
points = np.append(self.points,other.points,axis=0)
connectivity = np.append(self.connectivity,other.connectivity,axis=0)
return UnstructuredVTKGrid(points,connectivity,self.ppc)
def triangulation(self):
"""
:returns: Cell point coordinates x, y, z and connectivity c as (x, y, z), c
......
......@@ -6,6 +6,7 @@ import numpy as np
import warnings
from dorie.utilities.grid import UnstructuredVTKGrid
from dorie.utilities.grid import BaseGrid
from dorie.utilities.warnings import VTKWarning
from dorie.utilities import check_path
......@@ -56,6 +57,8 @@ class VTKFile:
grid = self._read_vtr(xml_root, variables)
elif vtk_type == "UnstructuredGrid":
grid = self._read_vtu(xml_root, variables)
elif vtk_type == "PUnstructuredGrid":
grid = self._read_pvtu(xml_root, variables)
else:
raise RuntimeError("Problem reading {0}: XML root attribute 'type' must "
"either be 'StructuredGrid' or 'UnstructuredGrid' "
......@@ -64,6 +67,31 @@ class VTKFile:
return grid
def _read_pvtu(self, root, variables):
"""
Reads an merges the peace files given by a parallel collection .pvtu file.
This function assumes that the grids can simply be stacked.
Calls VTKFile.read() for any peace found.
"""
out = None
for grid in root:
for layer in grid:
if layer.tag == "Piece":
filename = layer.attrib["Source"]
if os.path.isfile(filename):
piece_file = VTKFile(filename)
if out == None:
out = piece_file.read(variables)
else:
out = out.merge(piece_file.read(variables))
else:
raise RuntimeError("Problem reading {}: Piece file {} cannot be found".format(self.path,filename))
return out
def _read_vtu(self, root, variables):
"""
Traverses the VTK XML tree root given in ``root``.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment