direct support for vtk output

- geom_check can now handle multiple files
- microstructure index is stored as integer in vtk file
This commit is contained in:
Martin Diehl 2019-05-30 15:35:45 +02:00
parent 08052737cf
commit 46f0ad052e
3 changed files with 98 additions and 25 deletions

39
processing/pre/geom_check.py Executable file
View File

@ -0,0 +1,39 @@
#!/usr/bin/env python3
import os
import sys
from io import StringIO
from optparse import OptionParser
import damask
scriptName = os.path.splitext(os.path.basename(__file__))[0]
scriptID = ' '.join([scriptName,damask.version])
#--------------------------------------------------------------------------------------------------
# MAIN
#--------------------------------------------------------------------------------------------------
parser = OptionParser(option_class=damask.extendableOption, usage='%prog [geomfile(s)]', description = """
Writes vtk file for visualization.
""", version = scriptID)
(options, filenames) = parser.parse_args()
if filenames == []: filenames = [None]
for name in filenames:
damask.util.report(scriptName,name)
geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name)
damask.util.croak(geom)
if name is None:
sys.stdout.write(geom.to_vtk())
else:
geom.to_vtk(os.path.splitext(name)[0])

View File

@ -1,25 +0,0 @@
#!/usr/bin/env bash
if [[ "$1" == "-f" || "$1" == "--float" ]]; then
shift
arg='--float'
else
arg=''
fi
for geom in "$@"
do
geom_toTable $arg \
< $geom \
| \
vtk_rectilinearGrid > ${geom%.*}.vtk
geom_toTable $arg \
< $geom \
| \
vtk_addRectilinearGridData \
--vtk ${geom%.*}.vtk \
--data microstructure \
rm ${geom%.*}.vtk
done

View File

@ -1,8 +1,12 @@
import os
from io import StringIO from io import StringIO
import numpy as np import numpy as np
import vtk
from vtk.util import numpy_support
from . import util from . import util
from . import version
class Geom(): class Geom():
@ -198,6 +202,61 @@ class Geom():
self.microstructure.reshape([grid[0],np.prod(grid[1:])],order='F').T, self.microstructure.reshape([grid[0],np.prod(grid[1:])],order='F').T,
header='\n'.join(header), fmt=format_string, comments='') header='\n'.join(header), fmt=format_string, comments='')
def to_vtk(self,fname=None):
"""Generates vtk file. If file name is given, stored in file otherwise returned as string"""
grid = self.get_grid() + np.ones(3,dtype=int)
size = self.get_size()
origin = self.get_origin()
coords = [
np.linspace(0,size[0],grid[0]) - origin[0],
np.linspace(0,size[1],grid[1]) - origin[1],
np.linspace(0,size[2],grid[2]) - origin[2]
]
rGrid = vtk.vtkRectilinearGrid()
coordArray = [vtk.vtkDoubleArray(),vtk.vtkDoubleArray(),vtk.vtkDoubleArray()]
rGrid.SetDimensions(*grid)
for d,coord in enumerate(coords):
for c in coord:
coordArray[d].InsertNextValue(c)
rGrid.SetXCoordinates(coordArray[0])
rGrid.SetYCoordinates(coordArray[1])
rGrid.SetZCoordinates(coordArray[2])
ms = numpy_support.numpy_to_vtk(num_array=self.microstructure.flatten(order='F'),array_type=vtk.VTK_INT)
ms.SetName('microstructure')
rGrid.GetCellData().AddArray(ms)
if fname is not None:
writer = vtk.vtkXMLRectilinearGridWriter()
writer.SetCompressorTypeToZLib()
writer.SetDataModeToBinary()
ext = os.path.splitext(fname)[1]
if ext == '':
name = fname + '.' + writer.GetDefaultFileExtension()
elif ext == writer.GetDefaultFileExtension():
name = fname
else:
raise ValueError("unknown extension {}".format(ext))
writer.SetFileName(name)
else:
writer = vtk.vtkDataSetWriter()
writer.SetHeader('damask.Geom '+version)
writer.WriteToOutputStringOn()
writer.SetInputData(rGrid)
writer.Write()
if fname is None: return writer.GetOutputString()
def show(self): def show(self):
"""Show raw content (as in file)""" """Show raw content (as in file)"""
f=StringIO() f=StringIO()