2011-06-01 14:14:14 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2016-03-02 02:42:04 +05:30
|
|
|
import os,sys,vtk
|
2014-11-06 03:34:59 +05:30
|
|
|
import numpy as np
|
2014-11-07 16:37:03 +05:30
|
|
|
from optparse import OptionParser
|
2014-11-06 03:34:59 +05:30
|
|
|
import damask
|
2011-06-01 14:14:14 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2014-11-07 16:37:03 +05:30
|
|
|
|
2013-05-15 02:39:37 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
2011-06-01 14:14:14 +05:30
|
|
|
|
2014-11-07 16:37:03 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog [file[s]]', description = """
|
2011-11-03 17:37:41 +05:30
|
|
|
Produce VTK rectilinear mesh of structure data from geom description
|
2011-06-01 14:14:14 +05:30
|
|
|
|
2014-11-07 16:37:03 +05:30
|
|
|
""", version = scriptID)
|
2011-06-01 14:14:14 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-m','--nodata',
|
|
|
|
dest = 'data',
|
|
|
|
action = 'store_false',
|
|
|
|
help = 'generate mesh without microstructure index data')
|
2014-08-21 21:31:11 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.set_defaults(data = True,
|
|
|
|
)
|
2014-08-21 21:31:11 +05:30
|
|
|
|
2011-10-11 22:55:22 +05:30
|
|
|
(options, filenames) = parser.parse_args()
|
2011-06-01 14:14:14 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
2015-08-13 00:26:40 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2015-08-13 00:26:40 +05:30
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False, labeled = False, readonly = True)
|
|
|
|
except: continue
|
2015-10-09 03:00:33 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# --- interpret header ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
info,extra_header = table.head_getGeom()
|
|
|
|
|
2015-10-09 03:00:33 +05:30
|
|
|
damask.util.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))),
|
2015-08-08 00:33:26 +05:30
|
|
|
'size x y z: %s'%(' x '.join(map(str,info['size']))),
|
|
|
|
'origin x y z: %s'%(' : '.join(map(str,info['origin']))),
|
|
|
|
'homogenization: %i'%info['homogenization'],
|
|
|
|
'microstructures: %i'%info['microstructures'],
|
|
|
|
])
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
if np.any(info['grid'] < 1): errors.append('invalid grid a b c.')
|
|
|
|
if np.any(info['size'] <= 0.0): errors.append('invalid size x y z.')
|
2016-03-21 02:16:35 +05:30
|
|
|
|
|
|
|
#--- read microstructure information --------------------------------------------------------------
|
|
|
|
|
|
|
|
if options.data:
|
|
|
|
microstructure,ok = table.microstructure_read(info['grid'],strict = True) # read microstructure
|
|
|
|
|
|
|
|
if ok:
|
|
|
|
structure = vtk.vtkIntArray()
|
|
|
|
structure.SetName('Microstructures')
|
|
|
|
for idx in microstructure: structure.InsertNextValue(idx)
|
|
|
|
|
|
|
|
else: errors.append('mismatch between data and grid dimension.')
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if errors != []:
|
2015-10-09 03:00:33 +05:30
|
|
|
damask.util.croak(errors)
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close(dismiss = True)
|
2013-06-30 06:01:44 +05:30
|
|
|
continue
|
2013-05-15 02:39:37 +05:30
|
|
|
|
2016-03-21 02:16:35 +05:30
|
|
|
# --- generate VTK rectilinear grid ---------------------------------------------------------------
|
2013-05-15 02:39:37 +05:30
|
|
|
|
|
|
|
grid = vtk.vtkRectilinearGrid()
|
|
|
|
grid.SetDimensions([x+1 for x in info['grid']])
|
|
|
|
for i in xrange(3):
|
2013-06-30 06:01:44 +05:30
|
|
|
temp = vtk.vtkDoubleArray()
|
|
|
|
temp.SetNumberOfTuples(info['grid'][i]+1)
|
|
|
|
for j in xrange(info['grid'][i]+1):
|
|
|
|
temp.InsertTuple1(j,j*info['size'][i]/info['grid'][i]+info['origin'][i])
|
2014-08-21 21:31:11 +05:30
|
|
|
if i == 0: grid.SetXCoordinates(temp)
|
|
|
|
elif i == 1: grid.SetYCoordinates(temp)
|
|
|
|
elif i == 2: grid.SetZCoordinates(temp)
|
2013-05-15 02:39:37 +05:30
|
|
|
|
|
|
|
|
2016-03-21 02:16:35 +05:30
|
|
|
if options.data: grid.GetCellData().AddArray(structure)
|
2013-05-15 02:39:37 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- write data -----------------------------------------------------------------------------------
|
2015-08-13 00:26:40 +05:30
|
|
|
if name:
|
2013-12-09 21:11:29 +05:30
|
|
|
writer = vtk.vtkXMLRectilinearGridWriter()
|
2015-10-09 11:56:18 +05:30
|
|
|
(directory,filename) = os.path.split(name)
|
2013-12-09 21:11:29 +05:30
|
|
|
writer.SetDataModeToBinary()
|
|
|
|
writer.SetCompressorTypeToZLib()
|
2016-03-02 02:42:04 +05:30
|
|
|
writer.SetFileName(os.path.join(directory,os.path.splitext(filename)[0]+'.'+writer.GetDefaultFileExtension()))
|
2015-08-13 00:26:40 +05:30
|
|
|
else:
|
2015-10-09 03:00:33 +05:30
|
|
|
writer = vtk.vtkDataSetWriter()
|
2015-08-13 00:26:40 +05:30
|
|
|
writer.WriteToOutputStringOn()
|
|
|
|
writer.SetHeader('# powered by '+scriptID)
|
2015-10-09 03:00:33 +05:30
|
|
|
|
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(grid)
|
|
|
|
else: writer.SetInputData(grid)
|
|
|
|
writer.Write()
|
2016-03-02 02:42:04 +05:30
|
|
|
if name is None: sys.stdout.write(writer.GetOutputString()[0:writer.GetOutputStringLength()])
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
table.close()
|