DAMASK_EICMD/processing/pre/geom_toTable.py

68 lines
2.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2019-05-27 01:37:50 +05:30
import os
import sys
from optparse import OptionParser
2019-05-27 01:37:50 +05:30
from io import StringIO
import numpy as np
import damask
2019-05-27 01:37:50 +05:30
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 = """
2016-05-17 14:35:50 +05:30
Translate geom description into ASCIItable containing position and microstructure.
""", version = scriptID)
(options, filenames) = parser.parse_args()
if filenames == []: filenames = [None]
for name in filenames:
damask.util.report(scriptName,name)
2019-05-27 01:37:50 +05:30
if name is None:
virt_file = StringIO(''.join(sys.stdin.read()))
geom = damask.Geom.from_file(virt_file)
else:
geom = damask.Geom.from_file(name)
damask.util.croak(geom)
microstructure = geom.get_microstructure().flatten('F')
grid = geom.get_grid()
size = geom.get_size()
for i,line in enumerate(geom.get_comments()):
if line.lower().strip().startswith('origin'):
origin= np.array([float(line.split()[j]) for j in [2,4,6]]) # assume correct order (x,y,z)
2019-05-27 01:37:50 +05:30
#--- generate grid --------------------------------------------------------------------------------
2019-05-27 01:37:50 +05:30
x = (0.5 + np.arange(grid[0],dtype=float))/grid[0]*size[0]+origin[0]
y = (0.5 + np.arange(grid[1],dtype=float))/grid[1]*size[1]+origin[1]
z = (0.5 + np.arange(grid[2],dtype=float))/grid[2]*size[2]+origin[2]
2019-05-27 01:37:50 +05:30
xx = np.tile( x, grid[1]* grid[2])
yy = np.tile(np.repeat(y,grid[0] ),grid[2])
zz = np.repeat(z,grid[0]*grid[1])
2019-05-27 01:37:50 +05:30
# ------------------------------------------ finalize output ---------------------------------------
2019-05-28 01:30:26 +05:30
2019-05-27 01:37:50 +05:30
table = damask.ASCIItable(outname = os.path.splitext(name)[0]+'.txt' if name else name)
table.info_append([scriptID + '\t' + ' '.join(sys.argv[1:])] + geom.get_comments())
2016-10-25 00:46:29 +05:30
table.labels_append(['{}_{}'.format(1+i,'pos') for i in range(3)]+['microstructure'])
table.head_write()
table.output_flush()
2019-02-19 22:36:46 +05:30
table.data = np.squeeze(np.dstack((xx,yy,zz,microstructure)),axis=0)
table.data_writeArray()
table.close()