less code, faster execution

This commit is contained in:
Martin Diehl 2020-01-15 14:09:26 +01:00
parent ce47e18e5c
commit d66b05134d
1 changed files with 34 additions and 60 deletions

View File

@ -54,61 +54,35 @@ parser.set_defaults(homogenization = 1,
) )
(options,filenames) = parser.parse_args() (options,filenames) = parser.parse_args()
if filenames == []: filenames = [None]
input = [options.quaternion is not None, if np.sum([options.quaternion is not None,
options.microstructure is not None, options.microstructure is not None]) != 1:
]
if np.sum(input) != 1:
parser.error('need either microstructure or quaternion (and optionally phase) as input.') parser.error('need either microstructure or quaternion (and optionally phase) as input.')
if options.microstructure is not None and options.phase is not None: if options.microstructure is not None and options.phase is not None:
parser.error('need either microstructure or phase (and mandatory quaternion) as input.') parser.error('need either microstructure or phase (and mandatory quaternion) as input.')
if options.axes is not None and not set(options.axes).issubset(set(['x','+x','-x','y','+y','-y','z','+z','-z'])): 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)) 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: for name in filenames:
damask.util.report(scriptName,name) damask.util.report(scriptName,name)
table = damask.ASCIItable(name = name,readonly=True)
table.head_read() # read ASCII header info
table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name)
table.sort_by(['{}_{}'.format(i,options.pos) for i in range(3,0,-1)]) # x fast, y slow
grid,size,origin = damask.grid_filters.cell_coord0_gridSizeOrigin(table.get(options.pos))
table.data_readArray([options.pos] \ config_header = table.comments
+ (label if isinstance(label, list) else [label]) \
+ ([options.phase] if options.phase else []))
if options.phase is None: if options.microstructure:
table.data = np.column_stack((table.data,np.ones(len(table.data)))) # add single phase if no phase column given microstructure = table.get(options.microstructure).reshape(grid,order='F')
grid,size,origin = damask.grid_filters.cell_coord0_gridSizeOrigin(table.data[:,0:3]) elif options.quaternion:
q = table.get(options.quaternion)
phase = table.get(options.phase).astype(int) if options.phase else \
np.ones((table.data.shape[0],1),dtype=int)
indices = np.lexsort((table.data[:,0],table.data[:,1],table.data[:,2])) # indices of position when sorting x fast, z slow unique,unique_inverse = np.unique(np.hstack((q,phase)),return_inverse=True,axis=0)
microstructure = np.empty(grid,dtype = int) # initialize empty microstructure microstructure = unique_inverse.reshape(grid,order='F') + 1
i = 0
if inputtype == 'microstructure':
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)
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
config_header = ['<texture>'] config_header = ['<texture>']
for i,data in enumerate(unique): for i,data in enumerate(unique):