avoid code duplication

This commit is contained in:
Martin Diehl 2019-12-13 14:36:52 +01:00
parent 7d849b639b
commit e5448fc381
1 changed files with 25 additions and 38 deletions

View File

@ -854,9 +854,9 @@ class DADF5():
---------- ----------
labels : list of str labels : list of str
Labels of the datasets to be exported. Labels of the datasets to be exported.
mode : str mode : str, either 'Cell' or 'Point'
Export in cell format or point format. Export in cell format or point format.
Default value is 'Cell' Default value is 'Cell'.
""" """
if mode=='Cell': if mode=='Cell':
@ -868,11 +868,11 @@ class DADF5():
for c in np.linspace(0,self.size[dim],1+self.grid[dim]): for c in np.linspace(0,self.size[dim],1+self.grid[dim]):
coordArray[dim].InsertNextValue(c) coordArray[dim].InsertNextValue(c)
grid = vtk.vtkRectilinearGrid() vtk_geom = vtk.vtkRectilinearGrid()
grid.SetDimensions(*(self.grid+1)) vtk_geom.SetDimensions(*(self.grid+1))
grid.SetXCoordinates(coordArray[0]) vtk_geom.SetXCoordinates(coordArray[0])
grid.SetYCoordinates(coordArray[1]) vtk_geom.SetYCoordinates(coordArray[1])
grid.SetZCoordinates(coordArray[2]) vtk_geom.SetZCoordinates(coordArray[2])
else: else:
@ -880,12 +880,12 @@ class DADF5():
with h5py.File(self.fname) as f: with h5py.File(self.fname) as f:
nodes.SetData(numpy_support.numpy_to_vtk(f['/geometry/x_n'][()],deep=True)) nodes.SetData(numpy_support.numpy_to_vtk(f['/geometry/x_n'][()],deep=True))
grid = vtk.vtkUnstructuredGrid() vtk_geom = vtk.vtkUnstructuredGrid()
grid.SetPoints(nodes) vtk_geom.SetPoints(nodes)
grid.Allocate(f['/geometry/T_c'].shape[0]) vtk_geom.Allocate(f['/geometry/T_c'].shape[0])
for i in f['/geometry/T_c']: for i in f['/geometry/T_c']:
grid.InsertNextCell(vtk.VTK_HEXAHEDRON,8,i-1) # not for all elements! vtk_geom.InsertNextCell(vtk.VTK_HEXAHEDRON,8,i-1) # not for all elements!
else: elif mode == 'Point':
Points = vtk.vtkPoints() Points = vtk.vtkPoints()
Vertices = vtk.vtkCellArray() Vertices = vtk.vtkCellArray()
for c in self.cell_coordinates(): for c in self.cell_coordinates():
@ -893,10 +893,10 @@ class DADF5():
Vertices.InsertNextCell(1) Vertices.InsertNextCell(1)
Vertices.InsertCellPoint(pointID) Vertices.InsertCellPoint(pointID)
Polydata = vtk.vtkPolyData() vtk_geom = vtk.vtkPolyData()
Polydata.SetPoints(Points) vtk_geom.SetPoints(Points)
Polydata.SetVerts(Vertices) vtk_geom.SetVerts(Vertices)
Polydata.Modified() vtk_geom.Modified()
N_digits = int(np.floor(np.log10(int(self.increments[-1][3:]))))+1 N_digits = int(np.floor(np.log10(int(self.increments[-1][3:]))))+1
@ -917,10 +917,8 @@ class DADF5():
vtk_data.append(numpy_support.numpy_to_vtk(num_array=array.reshape(shape), vtk_data.append(numpy_support.numpy_to_vtk(num_array=array.reshape(shape),
deep=True,array_type= vtk.VTK_DOUBLE)) deep=True,array_type= vtk.VTK_DOUBLE))
vtk_data[-1].SetName('1_'+x[0].split('/',1)[1]) #ToDo: hard coded 1! vtk_data[-1].SetName('1_'+x[0].split('/',1)[1]) #ToDo: hard coded 1!
if mode=='Cell': vtk_geom.GetCellData().AddArray(vtk_data[-1])
grid.GetCellData().AddArray(vtk_data[-1])
else:
Polydata.GetCellData().AddArray(vtk_data[-1])
else: else:
x = self.get_dataset_location(label) x = self.get_dataset_location(label)
if len(x) == 0: if len(x) == 0:
@ -932,10 +930,8 @@ class DADF5():
ph_name = re.compile(r'(\/[1-9])_([A-Z][a-z]*)_(([a-z]*)|([A-Z]*))') #looking for phase name in dataset name ph_name = re.compile(r'(\/[1-9])_([A-Z][a-z]*)_(([a-z]*)|([A-Z]*))') #looking for phase name in dataset name
dset_name = '1_' + re.sub(ph_name,r'',x[0].split('/',1)[1]) #removing phase name from generic dataset dset_name = '1_' + re.sub(ph_name,r'',x[0].split('/',1)[1]) #removing phase name from generic dataset
vtk_data[-1].SetName(dset_name) vtk_data[-1].SetName(dset_name)
if mode=='Cell': vtk_geom.GetCellData().AddArray(vtk_data[-1])
grid.GetCellData().AddArray(vtk_data[-1])
else:
Polydata.GetCellData().AddArray(vtk_data[-1])
self.set_visible('materialpoints',materialpoints_backup) self.set_visible('materialpoints',materialpoints_backup)
constituents_backup = self.visible['constituents'].copy() constituents_backup = self.visible['constituents'].copy()
@ -952,10 +948,7 @@ class DADF5():
vtk_data.append(numpy_support.numpy_to_vtk(num_array=array.reshape(shape), vtk_data.append(numpy_support.numpy_to_vtk(num_array=array.reshape(shape),
deep=True,array_type= vtk.VTK_DOUBLE)) deep=True,array_type= vtk.VTK_DOUBLE))
vtk_data[-1].SetName('1_'+x[0].split('/',1)[1]) #ToDo: why 1_? vtk_data[-1].SetName('1_'+x[0].split('/',1)[1]) #ToDo: why 1_?
if mode=='Cell': vtk_geom.GetCellData().AddArray(vtk_data[-1])
grid.GetCellData().AddArray(vtk_data[-1])
else:
Polydata.GetCellData().AddArray(vtk_data[-1])
else: else:
x = self.get_dataset_location(label) x = self.get_dataset_location(label)
if len(x) == 0: if len(x) == 0:
@ -965,10 +958,7 @@ class DADF5():
vtk_data.append(numpy_support.numpy_to_vtk(num_array=array.reshape(shape), vtk_data.append(numpy_support.numpy_to_vtk(num_array=array.reshape(shape),
deep=True,array_type= vtk.VTK_DOUBLE)) deep=True,array_type= vtk.VTK_DOUBLE))
vtk_data[-1].SetName('1_'+x[0].split('/',1)[1]) vtk_data[-1].SetName('1_'+x[0].split('/',1)[1])
if mode=='Cell': vtk_geom.GetCellData().AddArray(vtk_data[-1])
grid.GetCellData().AddArray(vtk_data[-1])
else:
Polydata.GetCellData().AddArray(vtk_data[-1])
self.set_visible('constituents',constituents_backup) self.set_visible('constituents',constituents_backup)
if mode=='Cell': if mode=='Cell':
@ -978,8 +968,8 @@ class DADF5():
vtk_data.append(numpy_support.numpy_to_vtk(num_array=self.read_dataset(x,0), vtk_data.append(numpy_support.numpy_to_vtk(num_array=self.read_dataset(x,0),
deep=True,array_type=vtk.VTK_DOUBLE)) deep=True,array_type=vtk.VTK_DOUBLE))
vtk_data[-1].SetName('u') vtk_data[-1].SetName('u')
grid.GetPointData().AddArray(vtk_data[-1]) vtk_geom.GetPointData().AddArray(vtk_data[-1])
else: elif mode == 'Point':
writer = vtk.vtkXMLPolyDataWriter() writer = vtk.vtkXMLPolyDataWriter()
@ -990,9 +980,6 @@ class DADF5():
writer.SetCompressorTypeToZLib() writer.SetCompressorTypeToZLib()
writer.SetDataModeToBinary() writer.SetDataModeToBinary()
writer.SetFileName(file_out) writer.SetFileName(file_out)
if mode=='Cell': writer.SetInputData(vtk_geom)
writer.SetInputData(grid)
else:
writer.SetInputData(Polydata)
writer.Write() writer.Write()