first version of a library to parse HDF5

- preliminarly called DADF5 (DAMASK HDF5)
- script to write (empty undeformed) geometries is also added
This commit is contained in:
Martin Diehl 2019-04-13 11:11:32 +02:00
parent 89679147e8
commit 8eb1a35dfb
3 changed files with 103 additions and 0 deletions

View File

@ -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()

View File

@ -14,6 +14,7 @@ from .asciitable import ASCIItable # noqa
from .config import Material # noqa from .config import Material # noqa
from .colormaps import Colormap, Color # noqa from .colormaps import Colormap, Color # noqa
from .orientation import Symmetry, Lattice, Rotation, Orientation # noqa from .orientation import Symmetry, Lattice, Rotation, Orientation # noqa
from .dadf5 import DADF5 # noqa
#from .block import Block # only one class #from .block import Block # only one class
from .result import Result # noqa from .result import Result # noqa

38
python/damask/dadf5.py Normal file
View File

@ -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