diff --git a/.gitignore b/.gitignore index 8d35cb3..2314fef 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ __pycache__ *.pyc +.cache +*.vt* diff --git a/test.py b/test.py deleted file mode 100644 index 22b0bb8..0000000 --- a/test.py +++ /dev/null @@ -1,28 +0,0 @@ -from writer import * -from data_array import * -from vtk_files import * -import numpy as np - -N = 10 - -x = np.linspace(0, 1, N*2) -y = np.linspace(0, 1, N+2) -z = np.linspace(0, 1, N) - -rect = RectilinearGrid('test.vtr', (x, y, z)) - -x, y, z = np.meshgrid(x, y, z, indexing='ij') -r = np.sqrt(x**2 + y**2 + z**2) -e_r = np.zeros(r.shape + (3,3)) -e_r[0, 0, 0, :, :] = np.array([[0, 1, 0], [1, 0, 0], [0, 1, 1]]) -e_r[-1, 0, 0, :, :] = np.eye(3) -print(e_r.shape) -rect.addPointData(DataArray(r, range(3), 'R')) -rect.addPointData(DataArray(r, range(3), 'R2')) -rect.addPointData(DataArray(e_r, range(3), 'e_r')) -rect.write() - -from evtk.hl import gridToVTK - -e_r = e_r.transpose(4, 3, 0, 1, 2).copy() -gridToVTK('grid', x, y, z, pointData={'R': r, 'R2':r, 'e_r': e_r}) diff --git a/tests/test_rectilinear_grid.py b/tests/test_rectilinear_grid.py new file mode 100644 index 0000000..5e4d74f --- /dev/null +++ b/tests/test_rectilinear_grid.py @@ -0,0 +1,22 @@ +from uvw import RectilinearGrid, DataArray +import numpy as np + +def test_rectilinear_grid(): + N = 10 + + x = np.linspace(0, 1, N*2) + y = np.linspace(0, 1, N+2) + z = np.linspace(0, 1, N) + + rect = RectilinearGrid('test.vtr', (x, y, z)) + + x, y, z = np.meshgrid(x, y, z, indexing='ij') + r = np.sqrt(x**2 + y**2 + z**2) + e_r = np.zeros(r.shape + (3,3)) + e_r[0, 0, 0, :, :] = np.array([[0, 1, 0], [1, 0, 0], [0, 1, 1]]) + e_r[-1, 0, 0, :, :] = np.eye(3) + print(e_r.shape) + rect.addPointData(DataArray(r, range(3), 'R')) + rect.addPointData(DataArray(r, range(3), 'R2')) + rect.addPointData(DataArray(e_r, range(3), 'e_r')) + rect.write() diff --git a/uvw/__init__.py b/uvw/__init__.py new file mode 100644 index 0000000..d9ecccd --- /dev/null +++ b/uvw/__init__.py @@ -0,0 +1,2 @@ +from .vtk_files import * +from .data_array import * diff --git a/data_array.py b/uvw/data_array.py similarity index 100% rename from data_array.py rename to uvw/data_array.py diff --git a/vtk_files.py b/uvw/vtk_files.py similarity index 91% rename from vtk_files.py rename to uvw/vtk_files.py index 17960cd..d155a24 100644 --- a/vtk_files.py +++ b/uvw/vtk_files.py @@ -1,65 +1,65 @@ -from writer import * -from data_array import * +from . import writer +from . import data_array import functools class VTKFile: """Generic VTK file""" def __init__(self, filename, filetype, rank=None): self.filename = filename self.rank = rank - self.writer = Writer(filetype) + self.writer = writer.Writer(filetype) def addPointData(self, data_array): self.point_data.registerDataArray(data_array) def addCellData(self, data_array): self.cell_data.registerDataArray(data_array) def write(self): self.writer.registerAppend() self.writer.write(self.filename) class RectilinearGrid(VTKFile): """VTK Rectilinear grid (coordinates are given by 3 seperate ranges)""" def __init__(self, filename, coordinates, rank=None): VTKFile.__init__(self, filename, 'RectilinearGrid', rank) # Checking that we actually have a list or tuple if type(coordinates).__name__ not in ('tuple', 'list'): coordinates = [coordinates] self.coordinates = list(coordinates) # Filling in missing coordinates for _ in range(len(coordinates), 3): self.coordinates.append(np.array([0.])) # Setting data extent extent = [] for coord in self.coordinates: if coord.ndim != 1: raise Exception('Coordinate array should have only one dimension') extent.append(coord.size-1) extent = functools.reduce(lambda x, y: x + "0 {} ".format(y), extent, "") self.writer.setDataNodeAttributes({ "WholeExtent": extent }) self.piece = self.writer.registerPiece({ "Extent": extent }) # Registering coordinates coordinate_component = self.piece.register('Coordinates') for coord, prefix in zip(self.coordinates, ('x', 'y', 'z')): - array = DataArray(coord, [0], prefix + '_coordinates') + array = data_array.DataArray(coord, [0], prefix + '_coordinates') coordinate_component.registerDataArray(array) # Registering data elements self.point_data = self.piece.register('PointData') self.cell_data = self.piece.register('CellData') diff --git a/writer.py b/uvw/writer.py similarity index 100% rename from writer.py rename to uvw/writer.py