def _get_writer(filetype, filename): import vtk if filetype in "vtk-ascii": logging.warning("VTK ASCII files are only meant for debugging.") writer = vtk.vtkUnstructuredGridWriter() writer.SetFileTypeToASCII() elif filetype == "vtk-binary": writer = vtk.vtkUnstructuredGridWriter() writer.SetFileTypeToBinary() elif filetype == "vtu-ascii": logging.warning("VTU ASCII files are only meant for debugging.") writer = vtk.vtkXMLUnstructuredGridWriter() writer.SetDataModeToAscii() elif filetype == "vtu-binary": writer = vtk.vtkXMLUnstructuredGridWriter() writer.SetDataModeToBinary() elif filetype == "xdmf2": writer = vtk.vtkXdmfWriter() elif filetype == "xdmf3": writer = vtk.vtkXdmf3Writer() else: assert filetype == "exodus", "Unknown file type '{}'.".format(filename) writer = vtk.vtkExodusIIWriter() # if the mesh contains vtkmodeldata information, make use of it # and write out all time steps. writer.WriteAllTimeStepsOn() return writer
def write( filename, vtk_mesh, point_data = None, cell_data = None, field_data = None ): '''Writes mesh together with data to a file. :params filename: File to write to. :type filename: str :params point_data: Named additional point data to write to the file. :type point_data: dict ''' import os extension = os.path.splitext(filename)[1] # add point data is_exodus_format = extension in [ '.ex2', '.exo', '.e' ] if point_data: for name, data in point_data.iteritems(): new_name = name # There is a naming inconsistency in VTK when it comes to # multivectors in Exodus files: # If a vector 'v' has two components, they are called 'v_r', # 'v_z' (note the underscore), if it has three, then they are # called 'vx', 'vy', 'vz'. # Make this consistent by appending an underscore if needed. # Note that for VTK files, this problem does not occur since # the label of a vector is always stored as a string. is_3d_vector = len(data.shape) == 2 and data.shape[1] == 3 if is_exodus_format and is_3d_vector and name[-1] != '_': new_name += '_' vtk_mesh.GetPointData() \ .AddArray(_create_vtkarray(data, new_name)) # add cell data if cell_data: for key, value in cell_data.iteritems(): vtk_mesh.GetCellData() \ .AddArray(_create_vtkarray(value, key)) # add field data if field_data: for key, value in field_data.iteritems(): vtk_mesh.GetFieldData() \ .AddArray(_create_vtkarray(value, key)) import re extension = os.path.splitext(filename)[1] if extension == '.vtu': # VTK XML format from vtk import vtkXMLUnstructuredGridWriter writer = vtkXMLUnstructuredGridWriter() elif extension == '.pvtu': # parallel VTK XML format from vtk import vtkXMLPUnstructuredGridWriter writer = vtkXMLPUnstructuredGridWriter() elif extension == '.vtk': # classical VTK format from vtk import vtkUnstructuredGridWriter writer = vtkUnstructuredGridWriter() writer.SetFileTypeToASCII() elif extension == '.xmf': # XDMF format from vtk import vtkXdmfWriter writer = vtkXdmfWriter() elif extension in [ '.ex2', '.exo', '.e' ]: # Exodus II format from vtk import vtkExodusIIWriter writer = vtkExodusIIWriter() # If the mesh contains vtkModelData information, make use of it # and write out all time steps. writer.WriteAllTimeStepsOn() elif re.match('[^\.]*\.e\.\d+\.\d+', filename): # TODO handle parallel I/O with vtkPExodusIIWriter from vtk import vtkExodusIIWriter writer = vtkExodusIIWriter() # If the mesh contains vtkModelData information, make use of it # and write out all time steps. writer.WriteAllTimeStepsOn() else: raise IOError( 'Unknown file type \'%s\'.' % filename ) writer.SetFileName( filename ) writer.SetInputData( vtk_mesh ) writer.Write() return
def write(filetype, filename, points, cells, point_data=None, cell_data=None, field_data=None): import vtk from vtk.util import numpy_support def _create_vtkarray(X, name): array = vtk.util.numpy_support.numpy_to_vtk(X, deep=1) array.SetName(name) return array if point_data is None: point_data = {} if cell_data is None: cell_data = {} if field_data is None: field_data = {} vtk_mesh = _generate_vtk_mesh(points, cells) # add point data pd = vtk_mesh.GetPointData() for name, X in point_data.items(): # There is a naming inconsistency in VTK when it comes to multivectors # in Exodus files: # If a vector 'v' has two components, they are called 'v_x', 'v_y' # (note the underscore), if it has three, then they are called 'vx', # 'vy', 'vz'. See bug <http://www.vtk.org/Bug/view.php?id=15894>. # For VT{K,U} files, no underscore is ever added. pd.AddArray(_create_vtkarray(X, name)) # Add cell data. # The cell_data is structured like # # cell_type -> # key -> array # key -> array # [...] # cell_type -> # key -> array # key -> array # [...] # [...] # # VTK expects one array for each `key`, so assemble the keys across all # mesh_types. This requires each key to be present for each mesh_type, of # course. all_keys = [] for cell_type in cell_data: all_keys += cell_data[cell_type].keys() # create unified cell data for key in all_keys: for cell_type in cell_data: assert key in cell_data[cell_type] unified_cell_data = { key: numpy.concatenate( [cell_data[cell_type][key] for cell_type in cell_data]) for key in all_keys } # add the array data to the mesh cd = vtk_mesh.GetCellData() for name, array in unified_cell_data.items(): cd.AddArray(_create_vtkarray(array, name)) # add field data fd = vtk_mesh.GetFieldData() for key, value in field_data.items(): fd.AddArray(_create_vtkarray(value, key)) if filetype in 'vtk-ascii': writer = vtk.vtkUnstructuredGridWriter() writer.SetFileTypeToASCII() elif filetype == 'vtk-binary': writer = vtk.vtkUnstructuredGridWriter() writer.SetFileTypeToBinary() elif filetype == 'vtu': # vtk xml format writer = vtk.vtkXMLUnstructuredGridWriter() elif filetype == 'xdmf': writer = vtk.vtkXdmfWriter() elif filetype == 'exodus': # exodus ii format writer = vtk.vtkExodusIIWriter() # if the mesh contains vtkmodeldata information, make use of it # and write out all time steps. writer.WriteAllTimeStepsOn() else: raise RuntimeError('unknown file type \'%s\'.' % filetype) writer.SetFileName(filename) try: writer.SetInput(vtk_mesh) except AttributeError: writer.SetInputData(vtk_mesh) writer.Write() return
def write(filetype, filename, points, cells, point_data=None, cell_data=None, field_data=None ): import vtk from vtk.util import numpy_support def _create_vtkarray(X, name): array = vtk.util.numpy_support.numpy_to_vtk(X, deep=1) array.SetName(name) return array if point_data is None: point_data = {} if cell_data is None: cell_data = {} if field_data is None: field_data = {} vtk_mesh = _generate_vtk_mesh(points, cells) # add point data pd = vtk_mesh.GetPointData() for name, X in point_data.iteritems(): # There is a naming inconsistency in VTK when it comes to multivectors # in Exodus files: # If a vector 'v' has two components, they are called 'v_x', 'v_y' # (note the underscore), if it has three, then they are called 'vx', # 'vy', 'vz'. See bug <http://www.vtk.org/Bug/view.php?id=15894>. # For VT{K,U} files, no underscore is ever added. pd.AddArray(_create_vtkarray(X, name)) # add cell data cd = vtk_mesh.GetCellData() for key, value in cell_data.iteritems(): cd.AddArray(_create_vtkarray(value, key)) # add field data fd = vtk_mesh.GetFieldData() for key, value in field_data.iteritems(): fd.AddArray(_create_vtkarray(value, key)) if filetype in 'vtk-ascii': writer = vtk.vtkUnstructuredGridWriter() writer.SetFileTypeToASCII() elif filetype == 'vtk-binary': writer = vtk.vtkUnstructuredGridWriter() writer.SetFileTypeToBinary() elif filetype == 'vtu': # vtk xml format writer = vtk.vtkXMLUnstructuredGridWriter() elif filetype == 'xdmf': writer = vtk.vtkXdmfWriter() elif filetype == 'exodus': # exodus ii format writer = vtk.vtkExodusIIWriter() # if the mesh contains vtkmodeldata information, make use of it # and write out all time steps. writer.WriteAllTimeStepsOn() else: raise RuntimeError('unknown file type \'%s\'.' % filetype) writer.SetFileName(filename) try: writer.SetInput(vtk_mesh) except AttributeError: writer.SetInputData(vtk_mesh) writer.Write() return
def write(filetype, filename, points, cells, point_data=None, cell_data=None, field_data=None): # pylint: disable=import-error import vtk def _create_vtkarray(X, name): array = vtk.util.numpy_support.numpy_to_vtk(X, deep=1) array.SetName(name) return array point_data = {} if point_data is None else point_data cell_data = {} if cell_data is None else cell_data field_data = {} if field_data is None else field_data # assert data integrity for key in point_data: assert len(point_data[key]) == len(points), \ 'Point data mismatch.' for key in cell_data: assert key in cells, 'Cell data without cell' for key2 in cell_data[key]: assert len(cell_data[key][key2]) == len(cells[key]), \ 'Cell data mismatch.' vtk_mesh = _generate_vtk_mesh(points, cells) # add point data pd = vtk_mesh.GetPointData() for name, X in point_data.items(): # There is a naming inconsistency in VTK when it comes to multivectors # in Exodus files: # If a vector 'v' has two components, they are called 'v_x', 'v_y' # (note the underscore), if it has three, then they are called 'vx', # 'vy', 'vz'. See bug <http://www.vtk.org/Bug/view.php?id=15894>. # For VT{K,U} files, no underscore is ever added. pd.AddArray(_create_vtkarray(X, name)) # Add cell data. # The cell_data is structured like # # cell_type -> # key -> array # key -> array # [...] # cell_type -> # key -> array # key -> array # [...] # [...] # # VTK expects one array for each `key`, so assemble the keys across all # mesh_types. This requires each key to be present for each mesh_type, of # course. all_keys = [] for cell_type in cell_data: all_keys += cell_data[cell_type].keys() # create unified cell data for key in all_keys: for cell_type in cell_data: assert key in cell_data[cell_type] unified_cell_data = { key: numpy.concatenate([ cell_data[cell_type][key] for cell_type in cell_data ]) for key in all_keys } # add the array data to the mesh cd = vtk_mesh.GetCellData() for name, array in unified_cell_data.items(): cd.AddArray(_create_vtkarray(array, name)) # add field data fd = vtk_mesh.GetFieldData() for key, value in field_data.items(): fd.AddArray(_create_vtkarray(value, key)) if filetype in 'vtk-ascii': logging.warning('VTK ASCII files are only meant for debugging.') writer = vtk.vtkUnstructuredGridWriter() writer.SetFileTypeToASCII() elif filetype == 'vtk-binary': writer = vtk.vtkUnstructuredGridWriter() writer.SetFileTypeToBinary() elif filetype == 'vtu-ascii': logging.warning('VTU ASCII files are only meant for debugging.') writer = vtk.vtkXMLUnstructuredGridWriter() writer.SetDataModeToAscii() elif filetype == 'vtu-binary': writer = vtk.vtkXMLUnstructuredGridWriter() writer.SetDataModeToBinary() elif filetype == 'xdmf2': writer = vtk.vtkXdmfWriter() elif filetype == 'xdmf3': writer = vtk.vtkXdmf3Writer() else: assert filetype == 'exodus', \ 'Unknown file type \'{}\'.'.format(filename) writer = vtk.vtkExodusIIWriter() # if the mesh contains vtkmodeldata information, make use of it # and write out all time steps. writer.WriteAllTimeStepsOn() writer.SetFileName(filename) try: writer.SetInput(vtk_mesh) except AttributeError: writer.SetInputData(vtk_mesh) writer.Write() return
def write(filetype, filename, points, cells, point_data=None, cell_data=None, field_data=None): import vtk from vtk.util import numpy_support def _create_vtkarray(X, name): array = vtk.util.numpy_support.numpy_to_vtk(X, deep=1) array.SetName(name) return array if point_data is None: point_data = {} if cell_data is None: cell_data = {} if field_data is None: field_data = {} vtk_mesh = _generate_vtk_mesh(points, cells) # add point data pd = vtk_mesh.GetPointData() for name, X in point_data.iteritems(): # There is a naming inconsistency in VTK when it comes to multivectors # in Exodus files: # If a vector 'v' has two components, they are called 'v_x', 'v_y' # (note the underscore), if it has three, then they are called 'vx', # 'vy', 'vz'. See bug <http://www.vtk.org/Bug/view.php?id=15894>. # For VT{K,U} files, no underscore is ever added. pd.AddArray(_create_vtkarray(X, name)) # add cell data cd = vtk_mesh.GetCellData() for key, value in cell_data.iteritems(): cd.AddArray(_create_vtkarray(value, key)) # add field data fd = vtk_mesh.GetFieldData() for key, value in field_data.iteritems(): fd.AddArray(_create_vtkarray(value, key)) if filetype in 'vtk-ascii': writer = vtk.vtkUnstructuredGridWriter() writer.SetFileTypeToASCII() elif filetype == 'vtk-binary': writer = vtk.vtkUnstructuredGridWriter() writer.SetFileTypeToBinary() elif filetype == 'vtu': # vtk xml format writer = vtk.vtkXMLUnstructuredGridWriter() elif filetype == 'xdmf': writer = vtk.vtkXdmfWriter() elif filetype == 'exodus': # exodus ii format writer = vtk.vtkExodusIIWriter() # if the mesh contains vtkmodeldata information, make use of it # and write out all time steps. writer.WriteAllTimeStepsOn() else: raise RuntimeError('unknown file type \'%s\'.' % filetype) writer.SetFileName(filename) try: writer.SetInput(vtk_mesh) except AttributeError: writer.SetInputData(vtk_mesh) writer.Write() return