diff --git a/processing/post/DADF5_vtk_cells.py b/processing/post/DADF5_vtk_cells.py new file mode 100755 index 000000000..008ac5f66 --- /dev/null +++ b/processing/post/DADF5_vtk_cells.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +# -*- coding: UTF-8 no BOM -*- + +import os,vtk +import numpy as np +import argparse +import damask + +scriptName = os.path.splitext(os.path.basename(__file__))[0] +scriptID = ' '.join([scriptName,damask.version]) + +# -------------------------------------------------------------------- +# MAIN +# -------------------------------------------------------------------- +parser = argparse.ArgumentParser() + +#ToDo: We need to decide on a way of handling arguments of variable lentght +#https://stackoverflow.com/questions/15459997/passing-integer-lists-to-python + +#parser.add_argument('--version', action='version', version='%(prog)s {}'.format(scriptID)) +parser.add_argument('filenames', nargs='+', + help='DADF5 files') + +options = parser.parse_args() + + +# --- loop over input files ------------------------------------------------------------------------ + +for filename in options.filenames: + data = damask.DADF5(filename) + + if data.structured: # for grid solvers use rectilinear grid + rGrid = vtk.vtkRectilinearGrid() + coordArray = [vtk.vtkDoubleArray(), + vtk.vtkDoubleArray(), + vtk.vtkDoubleArray(), + ] + + rGrid.SetDimensions(*data.grid) + for dim in [0,1,2]: + for c in np.linspace(0,data.size[dim],1+data.grid[dim]): + coordArray[dim].InsertNextValue(c) + + rGrid.SetXCoordinates(coordArray[0]) + rGrid.SetYCoordinates(coordArray[1]) + rGrid.SetZCoordinates(coordArray[2]) + + + for i,inc in enumerate(data.increments): + if not inc['active']: pass + + if data.structured: + writer = vtk.vtkXMLRectilinearGridWriter() + + writer.SetCompressorTypeToZLib() + writer.SetDataModeToBinary() + writer.SetFileName(os.path.join(os.path.split(filename)[0], + os.path.splitext(os.path.split(filename)[1])[0] + + '_inc{:04d}'.format(i) + # ToDo: adjust to lenght of increments + '.' + writer.GetDefaultFileExtension())) + if data.structured: + writer.SetInputData(rGrid) + + writer.Write() diff --git a/python/damask/__init__.py b/python/damask/__init__.py index d7ed4a9f9..2dfdac567 100644 --- a/python/damask/__init__.py +++ b/python/damask/__init__.py @@ -14,6 +14,7 @@ from .asciitable import ASCIItable # noqa from .config import Material # noqa from .colormaps import Colormap, Color # noqa from .orientation import Symmetry, Lattice, Rotation, Orientation # noqa +from .dadf5 import DADF5 # noqa #from .block import Block # only one class from .result import Result # noqa diff --git a/python/damask/dadf5.py b/python/damask/dadf5.py new file mode 100644 index 000000000..6342b18e7 --- /dev/null +++ b/python/damask/dadf5.py @@ -0,0 +1,38 @@ +# -*- coding: UTF-8 no BOM -*- +import h5py +import re + +# ------------------------------------------------------------------ +class DADF5(): + """Read and write to DADF5 files""" + +# ------------------------------------------------------------------ + def __init__(self, + filename, + mode = 'r', + ): + + if mode not in ['a','r']: + print('Invalid file access mode') + with h5py.File(filename,mode): + pass + + with h5py.File(filename,'r') as f: + + if f.attrs['DADF5-major'] != 0 or f.attrs['DADF5-minor'] != 1: + print('Unsupported DADF5 version {} '.format(f.attrs['DADF5-version'])) + + self.structured = 'grid' in f['mapping'].attrs.keys() + + if self.structured: + self.grid = f['mapping'].attrs['grid'] + self.size = f['mapping'].attrs['size'] + + r=re.compile('inc[0-9]+') + self.increments = [{'group': u, + 'time': f[u].attrs['time/s'], + 'active': True + } for u in f.keys() if r.match(u)] + + self.filename = filename + self.mode = mode