DAMASK_EICMD/processing/pre/geom_fromTable.py

156 lines
6.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2019-05-27 12:08:02 +05:30
import os
import sys
from optparse import OptionParser
2019-05-27 12:08:02 +05:30
import numpy as np
import damask
2019-05-27 12:08:02 +05:30
scriptName = os.path.splitext(os.path.basename(__file__))[0]
scriptID = ' '.join([scriptName,damask.version])
# --------------------------------------------------------------------
# MAIN
# --------------------------------------------------------------------
2019-05-27 12:08:02 +05:30
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """
2019-05-30 17:37:49 +05:30
Converts ASCII table. Input can be microstructure or orientation (as quaternion). For the latter,
2019-05-30 17:00:18 +05:30
phase information can be given additionally.
""", version = scriptID)
parser.add_option('--coordinates',
dest = 'pos',
type = 'string', metavar = 'string',
2017-05-29 14:02:22 +05:30
help = 'coordinates label (%default)')
parser.add_option('--phase',
dest = 'phase',
type = 'string', metavar = 'string',
help = 'phase label')
parser.add_option('--microstructure',
dest = 'microstructure',
type = 'string', metavar = 'string',
help = 'microstructure label')
parser.add_option('-q', '--quaternion',
dest = 'quaternion',
type = 'string', metavar='string',
help = 'quaternion label')
parser.add_option('--axes',
dest = 'axes',
type = 'string', nargs = 3, metavar = ' '.join(['string']*3),
help = 'orientation coordinate frame in terms of position coordinate frame [+x +y +z]')
parser.add_option('--homogenization',
dest = 'homogenization',
type = 'int', metavar = 'int',
help = 'homogenization index to be used [%default]')
parser.set_defaults(homogenization = 1,
2017-05-29 14:02:22 +05:30
pos = 'pos',
)
(options,filenames) = parser.parse_args()
input = [options.quaternion is not None,
2016-03-02 17:13:09 +05:30
options.microstructure is not None,
]
if np.sum(input) != 1:
parser.error('need either microstructure or quaternion (and optionally phase) as input.')
if options.microstructure is not None and options.phase is not None:
parser.error('need either microstructure or phase (and mandatory quaternion) as input.')
2016-03-02 17:13:09 +05:30
if options.axes is not None and not set(options.axes).issubset(set(['x','+x','-x','y','+y','-y','z','+z','-z'])):
parser.error('invalid axes {} {} {}.'.format(*options.axes))
(label,dim,inputtype) = [(options.quaternion,4,'quaternion'),
(options.microstructure,1,'microstructure'),
][np.where(input)[0][0]] # select input label that was requested
if filenames == []: filenames = [None]
for name in filenames:
damask.util.report(scriptName,name)
2019-05-27 12:08:02 +05:30
table = damask.ASCIItable(name = name,readonly=True)
table.head_read() # read ASCII header info
2019-05-30 17:37:49 +05:30
# ------------------------------------------ sanity checks ---------------------------------------
coordDim = table.label_dimension(options.pos)
errors = []
if not 3 >= coordDim >= 2:
errors.append('coordinates "{}" need to have two or three dimensions.'.format(options.pos))
if not np.all(table.label_dimension(label) == dim):
errors.append('input "{}" needs to have dimension {}.'.format(label,dim))
if options.phase and table.label_dimension(options.phase) != 1:
errors.append('phase column "{}" is not scalar.'.format(options.phase))
2019-05-30 17:37:49 +05:30
if errors != []:
damask.util.croak(errors)
continue
table.data_readArray([options.pos] \
2018-07-20 04:09:50 +05:30
+ (label if isinstance(label, list) else [label]) \
+ ([options.phase] if options.phase else []))
2019-05-30 17:37:49 +05:30
if coordDim == 2:
table.data = np.insert(table.data,2,np.zeros(len(table.data)),axis=1) # add zero z coordinate for two-dimensional input
2016-03-02 17:13:09 +05:30
if options.phase is None:
table.data = np.column_stack((table.data,np.ones(len(table.data)))) # add single phase if no phase column given
2019-05-27 12:08:02 +05:30
grid,size = damask.util.coordGridAndSize(table.data[:,0:3])
2016-10-25 00:46:29 +05:30
coords = [np.unique(table.data[:,i]) for i in range(3)]
2018-07-20 04:09:50 +05:30
mincorner = np.array(list(map(min,coords)))
2019-05-27 12:08:02 +05:30
origin = mincorner - 0.5*size/grid # shift from cell center to corner
indices = np.lexsort((table.data[:,0],table.data[:,1],table.data[:,2])) # indices of position when sorting x fast, z slow
microstructure = np.empty(grid,dtype = int) # initialize empty microstructure
i = 0
2019-05-30 17:37:49 +05:30
if inputtype == 'microstructure':
2016-10-25 00:46:29 +05:30
for z in range(grid[2]):
for y in range(grid[1]):
for x in range(grid[0]):
microstructure[x,y,z] = table.data[indices[i],3]
i+=1
config_header = []
elif inputtype == 'quaternion':
unique,unique_inverse = np.unique(table.data[:,3:8],return_inverse=True,axis=0)
2019-05-30 17:37:49 +05:30
for z in range(grid[2]):
for y in range(grid[1]):
for x in range(grid[0]):
microstructure[x,y,z] = unique_inverse[indices[i]]+1
i+=1
2019-05-30 17:37:49 +05:30
config_header = ['<texture>']
for i,data in enumerate(unique):
ori = damask.Rotation(data[0:4])
config_header += ['[Grain{}]'.format(i+1),
'(gauss)\tphi1 {:.2f}\tPhi {:.2f}\tphi2 {:.2f}'.format(*ori.asEulers(degrees = True)),
]
2019-05-30 17:37:49 +05:30
if options.axes is not None: config_header += ['axes\t{} {} {}'.format(*options.axes)]
config_header += ['<microstructure>']
for i,data in enumerate(unique):
config_header += ['[Grain{}]'.format(i+1),
'crystallite 1',
'(constituent)\tphase {}\ttexture {}\tfraction 1.0'.format(int(data[4]),i+1),
]
header = [scriptID + ' ' + ' '.join(sys.argv[1:])]\
+ config_header
geom = damask.Geom(microstructure,size,origin,
homogenization=options.homogenization,comments=header)
2019-05-27 12:08:02 +05:30
damask.util.croak(geom)
2019-05-30 17:37:49 +05:30
2019-11-24 23:32:19 +05:30
geom.to_file(sys.stdout if name is None else os.path.splitext(name)[0]+'.geom',pack=False)