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,80 +54,54 @@ 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': config_header = ['<texture>']
for z in range(grid[2]): for i,data in enumerate(unique):
for y in range(grid[1]): ori = damask.Rotation(data[0:4])
for x in range(grid[0]): config_header += ['[Grain{}]'.format(i+1),
microstructure[x,y,z] = table.data[indices[i],3] '(gauss)\tphi1 {:.2f}\tPhi {:.2f}\tphi2 {:.2f}'.format(*ori.asEulers(degrees = True)),
i+=1 ]
if options.axes is not None: config_header += ['axes\t{} {} {}'.format(*options.axes)]
config_header = [] config_header += ['<microstructure>']
for i,data in enumerate(unique):
config_header += ['[Grain{}]'.format(i+1),
'(constituent)\tphase {}\ttexture {}\tfraction 1.0'.format(int(data[4]),i+1),
]
elif inputtype == 'quaternion': header = [scriptID + ' ' + ' '.join(sys.argv[1:])]\
unique,unique_inverse = np.unique(table.data[:,3:8],return_inverse=True,axis=0) + config_header
geom = damask.Geom(microstructure,size,origin,
homogenization=options.homogenization,comments=header)
damask.util.croak(geom)
for z in range(grid[2]): geom.to_file(sys.stdout if name is None else os.path.splitext(name)[0]+'.geom',pack=False)
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>']
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)),
]
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),
'(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)
damask.util.croak(geom)
geom.to_file(sys.stdout if name is None else os.path.splitext(name)[0]+'.geom',pack=False)