From dd318c8d1d3af3af6fd5a4c8c6d313499045d0c0 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sat, 21 Dec 2019 15:12:01 +0100 Subject: [PATCH] Table class replaces ASCIItable class --- processing/post/addCompatibilityMismatch.py | 111 +++----------------- processing/post/addIPFcolor.py | 55 +++------- processing/post/addNorm.py | 71 +++---------- processing/post/addPole.py | 57 +++------- 4 files changed, 60 insertions(+), 234 deletions(-) diff --git a/processing/post/addCompatibilityMismatch.py b/processing/post/addCompatibilityMismatch.py index 7556cb863..e4b6d940d 100755 --- a/processing/post/addCompatibilityMismatch.py +++ b/processing/post/addCompatibilityMismatch.py @@ -2,10 +2,10 @@ import os import math +import sys from optparse import OptionParser import numpy as np -import scipy.ndimage import damask @@ -13,26 +13,6 @@ import damask scriptName = os.path.splitext(os.path.basename(__file__))[0] scriptID = ' '.join([scriptName,damask.version]) -#-------------------------------------------------------------------------------------------------- -def cell2node(cellData,grid): - - nodeData = 0.0 - datalen = np.array(cellData.shape[3:]).prod() - - for i in range(datalen): - node = scipy.ndimage.convolve(cellData.reshape(tuple(grid[::-1])+(datalen,))[...,i], - np.ones((2,2,2))/8., # 2x2x2 neighborhood of cells - mode = 'wrap', - origin = -1, # offset to have cell origin as center - ) # now averaged at cell origins - node = np.append(node,node[np.newaxis,0,:,:,...],axis=0) # wrap along z - node = np.append(node,node[:,0,np.newaxis,:,...],axis=1) # wrap along y - node = np.append(node,node[:,:,0,np.newaxis,...],axis=2) # wrap along x - - nodeData = node[...,np.newaxis] if i==0 else np.concatenate((nodeData,node[...,np.newaxis]),axis=-1) - - return nodeData - #-------------------------------------------------------------------------------------------------- def deformationAvgFFT(F,grid,size,nodal=False,transformed=False): """Calculate average cell center (or nodal) deformation for deformation gradient field specified in each grid cell""" @@ -82,7 +62,7 @@ def displacementFluctFFT(F,grid,size,nodal=False,transformed=False): displacement = np.fft.irfftn(displacement_fourier,grid[::-1],axes=(0,1,2)) - return cell2node(displacement,grid) if nodal else displacement + return damask.grid_filters.cell_2_node(displacement) if nodal else displacement def volTetrahedron(coords): @@ -241,92 +221,33 @@ parser.set_defaults(pos = 'pos', ) (options,filenames) = parser.parse_args() - -# --- loop over input files ------------------------------------------------------------------------- - if filenames == []: filenames = [None] + for name in filenames: - try: - table = damask.ASCIItable(name = name, - buffered = False) - except: continue damask.util.report(scriptName,name) - -# ------------------------------------------ read header ------------------------------------------ - - table.head_read() - -# ------------------------------------------ sanity checks ---------------------------------------- - - errors = [] - remarks = [] - if table.label_dimension(options.defgrad) != 9: - errors.append('deformation gradient "{}" is not a 3x3 tensor.'.format(options.defgrad)) + table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name) + grid,size,origin = damask.grid_filters.cell_coord0_2_DNA(table.get(options.pos)) - coordDim = table.label_dimension(options.pos) - if not 3 >= coordDim >= 1: - errors.append('coordinates "{}" need to have one, two, or three dimensions.'.format(options.pos)) - elif coordDim < 3: - remarks.append('appending {} dimension{} to coordinates "{}"...'.format(3-coordDim, - 's' if coordDim < 2 else '', - options.pos)) - - if remarks != []: damask.util.croak(remarks) - if errors != []: - damask.util.croak(errors) - table.close(dismiss=True) - continue - -# --------------- figure out size and grid --------------------------------------------------------- - - table.data_readArray([options.defgrad,options.pos]) - table.data_rewind() - - if table.data[:,9:].shape[1] < 3: - table.data = np.hstack((table.data, - np.zeros((table.data.shape[0], - 3-table.data[:,9:].shape[1]),dtype='f'))) # fill coords up to 3D with zeros - - grid,size = damask.util.coordGridAndSize(table.data[:,9:12]) N = grid.prod() - if N != len(table.data): errors.append('data count {} does not match grid {}x{}x{}.'.format(N,*grid)) - if errors != []: - damask.util.croak(errors) - table.close(dismiss = True) - continue - -# -----------------------------process data and assemble header ------------------------------------- - - F_fourier = np.fft.rfftn(table.data[:,:9].reshape(grid[2],grid[1],grid[0],3,3),axes=(0,1,2)) # perform transform only once... + F_fourier = np.fft.rfftn(table.get(options.defgrad).reshape(grid[2],grid[1],grid[0],3,3),axes=(0,1,2)) # perform transform only once... nodes = displacementFluctFFT(F_fourier,grid,size,True,transformed=True)\ + deformationAvgFFT (F_fourier,grid,size,True,transformed=True) if options.shape: - table.labels_append(['shapeMismatch({})'.format(options.defgrad)]) centres = displacementFluctFFT(F_fourier,grid,size,False,transformed=True)\ + deformationAvgFFT (F_fourier,grid,size,False,transformed=True) - + shapeMismatch = shapeMismatch( size,table.get(options.defgrad).reshape(grid[2],grid[1],grid[0],3,3),nodes,centres) + table.add('shapeMismatch(({}))'.format(options.defgrad), + shapeMismatch.reshape((-1,1)), + scriptID+' '+' '.join(sys.argv[1:])) + if options.volume: - table.labels_append(['volMismatch({})'.format(options.defgrad)]) + volumeMismatch = volumeMismatch(size,table.get(options.defgrad).reshape(grid[2],grid[1],grid[0],3,3),nodes) + table.add('volMismatch(({}))'.format(options.defgrad), + volumeMismatch.reshape((-1,1)), + scriptID+' '+' '.join(sys.argv[1:])) - table.head_write() - if options.shape: - shapeMismatch = shapeMismatch( size,table.data[:,:9].reshape(grid[2],grid[1],grid[0],3,3),nodes,centres) - if options.volume: - volumeMismatch = volumeMismatch(size,table.data[:,:9].reshape(grid[2],grid[1],grid[0],3,3),nodes) - -# ------------------------------------------ output data ------------------------------------------- - for i in range(grid[2]): - for j in range(grid[1]): - for k in range(grid[0]): - table.data_read() - if options.shape: table.data_append(shapeMismatch[i,j,k]) - if options.volume: table.data_append(volumeMismatch[i,j,k]) - table.data_write() - -# ------------------------------------------ output finalization ----------------------------------- - - table.close() # close ASCII tables + table.to_ASCII(sys.stdout if name is None else name) diff --git a/processing/post/addIPFcolor.py b/processing/post/addIPFcolor.py index 0149dd078..5114b6a91 100755 --- a/processing/post/addIPFcolor.py +++ b/processing/post/addIPFcolor.py @@ -43,54 +43,25 @@ parser.set_defaults(pole = (0.0,0.0,1.0), ) (options, filenames) = parser.parse_args() +if filenames == []: filenames = [None] # damask.Orientation requires Bravais lattice, but we are only interested in symmetry -symmetry2lattice={'cubic':'bcc','hexagonal':'hex','tetragonal':'bct'} +symmetry2lattice={'cubic':'fcc','hexagonal':'hex','tetragonal':'bct'} lattice = symmetry2lattice[options.symmetry] pole = np.array(options.pole) pole /= np.linalg.norm(pole) -# --- loop over input files ------------------------------------------------------------------------ - -if filenames == []: filenames = [None] - for name in filenames: - try: - table = damask.ASCIItable(name = name, - buffered = False) - except: continue - damask.util.report(scriptName,name) + damask.util.report(scriptName,name) -# ------------------------------------------ read header ------------------------------------------ - - table.head_read() - -# ------------------------------------------ sanity checks ---------------------------------------- - - if not table.label_dimension(options.quaternion) == 4: - damask.util.croak('input {} does not have dimension 4.'.format(options.quaternion)) - table.close(dismiss = True) # close ASCIItable and remove empty file - continue - - column = table.label_index(options.quaternion) - -# ------------------------------------------ assemble header --------------------------------------- - - table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:])) - table.labels_append(['{}_IPF_{:g}{:g}{:g}_{sym}'.format(i+1,*options.pole,sym = options.symmetry.lower()) for i in range(3)]) - table.head_write() - -# ------------------------------------------ process data ------------------------------------------ - - outputAlive = True - while outputAlive and table.data_read(): # read next data line of ASCII table - o = damask.Orientation(np.array(list(map(float,table.data[column:column+4]))), - lattice = lattice).reduced() - - table.data_append(o.IPFcolor(pole)) - outputAlive = table.data_write() # output processed line - -# ------------------------------------------ output finalization ----------------------------------- - - table.close() # close ASCII tables + table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name) + orientation = table.get(options.quaternion) + color = np.empty((orientation.shape[0],3)) + for i,o in enumerate(orientation): + color[i] = damask.Orientation(o,lattice = lattice).IPFcolor(pole) + + table.add('IPF_{:g}{:g}{:g}_{sym}'.format(*options.pole,sym = options.symmetry.lower()), + color, + scriptID+' '+' '.join(sys.argv[1:])) + table.to_ASCII(sys.stdout if name is None else name) diff --git a/processing/post/addNorm.py b/processing/post/addNorm.py index c8c0b05bf..e58947de0 100755 --- a/processing/post/addNorm.py +++ b/processing/post/addNorm.py @@ -42,7 +42,7 @@ parser.add_option('-n','--norm', type = 'choice', choices = normChoices, metavar='string', help = 'type of element-wise p-norm [frobenius] {%s}'%(','.join(map(str,normChoices)))) parser.add_option('-l','--label', - dest = 'label', + dest = 'labels', action = 'extend', metavar = '', help = 'heading of column(s) to calculate norm of') @@ -50,62 +50,25 @@ parser.set_defaults(norm = 'frobenius', ) (options,filenames) = parser.parse_args() - -if options.norm.lower() not in normChoices: - parser.error('invalid norm ({}) specified.'.format(options.norm)) -if options.label is None: - parser.error('no data column specified.') - -# --- loop over input files ------------------------------------------------------------------------- - if filenames == []: filenames = [None] +if options.norm.lower() not in normChoices: + parser.error('invalid norm ({}) specified.'.format(options.norm)) +if options.labels is None: + parser.error('no data column specified.') + for name in filenames: - try: - table = damask.ASCIItable(name = name, - buffered = False) - except: continue - damask.util.report(scriptName,name) + damask.util.report(scriptName,name) -# ------------------------------------------ read header ------------------------------------------ + table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name) + for label in options.labels: + data = table.get(label) + data_norm = np.empty((data.shape[0],1)) + for i,d in enumerate(data): + data_norm[i] = norm(options.norm.capitalize(),d) - table.head_read() + table.add('norm{}({})'.format(options.norm.capitalize(),label), + data_norm, + scriptID+' '+' '.join(sys.argv[1:])) -# ------------------------------------------ sanity checks ---------------------------------------- - - errors = [] - remarks = [] - columns = [] - dims = [] - - for what in options.label: - dim = table.label_dimension(what) - if dim < 0: remarks.append('column {} not found...'.format(what)) - else: - dims.append(dim) - columns.append(table.label_index(what)) - table.labels_append('norm{}({})'.format(options.norm.capitalize(),what)) # extend ASCII header with new labels - - if remarks != []: damask.util.croak(remarks) - if errors != []: - damask.util.croak(errors) - table.close(dismiss = True) - continue - -# ------------------------------------------ assemble header -------------------------------------- - - table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:])) - table.head_write() - -# ------------------------------------------ process data ------------------------------------------ - - outputAlive = True - while outputAlive and table.data_read(): # read next data line of ASCII table - for column,dim in zip(columns,dims): - table.data_append(norm(options.norm.capitalize(), - map(float,table.data[column:column+dim]))) - outputAlive = table.data_write() # output processed line - -# ------------------------------------------ output finalization ----------------------------------- - - table.close() # close input ASCII table (works for stdin) + table.to_ASCII(sys.stdout if name is None else name) diff --git a/processing/post/addPole.py b/processing/post/addPole.py index c8b83b106..9f4982ff5 100755 --- a/processing/post/addPole.py +++ b/processing/post/addPole.py @@ -42,52 +42,23 @@ parser.set_defaults(pole = (1.0,0.0,0.0), ) (options, filenames) = parser.parse_args() +if filenames == []: filenames = [None] pole = np.array(options.pole) pole /= np.linalg.norm(pole) -# --- loop over input files ------------------------------------------------------------------------- - -if filenames == []: filenames = [None] - for name in filenames: - try: - table = damask.ASCIItable(name = name, - buffered = False) - except: continue - damask.util.report(scriptName,name) + damask.util.report(scriptName,name) -# ------------------------------------------ read header ------------------------------------------ - - table.head_read() - -# ------------------------------------------ sanity checks ---------------------------------------- - - if not table.label_dimension(options.quaternion) == 4: - damask.util.croak('input {} does not have dimension 4.'.format(options.quaternion)) - table.close(dismiss = True) # close ASCIItable and remove empty file - continue - - column = table.label_index(options.quaternion) - -# ------------------------------------------ assemble header --------------------------------------- - - table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:])) - table.labels_append(['{}_pole_{}{}{}'.format(i+1,*options.pole) for i in range(2)]) - table.head_write() - -# ------------------------------------------ process data ------------------------------------------ - outputAlive = True - while outputAlive and table.data_read(): # read next data line of ASCII table - o = damask.Rotation(np.array(list(map(float,table.data[column:column+4])))) - - rotatedPole = o*pole # rotate pole according to crystal orientation - (x,y) = rotatedPole[0:2]/(1.+abs(pole[2])) # stereographic projection - - table.data_append([np.sqrt(x*x+y*y),np.arctan2(y,x)] if options.polar else [x,y]) # cartesian coordinates - - outputAlive = table.data_write() # output processed line - -# ------------------------------------------ output finalization ----------------------------------- - - table.close() # close ASCII tables + table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name) + orientation = table.get(options.quaternion) + poles = np.empty((orientation.shape[0],2)) + for i,o in enumerate(orientation): + rotatedPole = damask.Rotation(o)*pole # rotate pole according to crystal orientation + (x,y) = rotatedPole[0:2]/(1.+abs(pole[2])) # stereographic projection + poles[i] = [np.sqrt(x*x+y*y),np.arctan2(y,x)] if options.polar else [x,y] # cartesian coordinates + + table.add('pole_{}{}{}'.format(*options.pole), + poles, + scriptID+' '+' '.join(sys.argv[1:])) + table.to_ASCII(sys.stdout if name is None else name)