2016-03-17 00:32:38 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
|
|
|
import os,sys,math
|
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
|
|
|
|
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
|
|
|
|
2016-04-16 03:57:23 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
2016-03-17 00:32:38 +05:30
|
|
|
def gradFFT(geomdim,field):
|
2016-04-16 03:57:23 +05:30
|
|
|
|
2016-03-17 00:32:38 +05:30
|
|
|
grid = np.array(np.shape(field)[2::-1])
|
2016-03-17 00:42:53 +05:30
|
|
|
N = grid.prod() # field size
|
|
|
|
n = np.array(np.shape(field)[3:]).prod() # data size
|
2016-03-17 00:32:38 +05:30
|
|
|
if n == 3: dataType = 'vector'
|
|
|
|
elif n == 1: dataType = 'scalar'
|
|
|
|
|
|
|
|
field_fourier = np.fft.fftpack.rfftn(field,axes=(0,1,2))
|
2016-04-16 03:57:23 +05:30
|
|
|
grad_fourier = np.zeros(field_fourier.shape+(3,),'c16')
|
2016-03-17 00:32:38 +05:30
|
|
|
|
|
|
|
# differentiation in Fourier space
|
|
|
|
k_s = np.zeros([3],'i')
|
|
|
|
TWOPIIMG = 2.0j*math.pi
|
|
|
|
for i in xrange(grid[2]):
|
|
|
|
k_s[0] = i
|
2016-03-17 00:42:53 +05:30
|
|
|
if grid[2]%2 == 0 and i == grid[2]//2: k_s[0] = 0 # for even grid, set Nyquist freq to 0 (Johnson, MIT, 2011)
|
2016-03-17 00:32:38 +05:30
|
|
|
elif i > grid[2]//2: k_s[0] -= grid[2]
|
|
|
|
|
|
|
|
for j in xrange(grid[1]):
|
|
|
|
k_s[1] = j
|
2016-03-17 00:42:53 +05:30
|
|
|
if grid[1]%2 == 0 and j == grid[1]//2: k_s[1] = 0 # for even grid, set Nyquist freq to 0 (Johnson, MIT, 2011)
|
2016-03-17 00:32:38 +05:30
|
|
|
elif j > grid[1]//2: k_s[1] -= grid[1]
|
|
|
|
|
|
|
|
for k in xrange(grid[0]//2+1):
|
|
|
|
k_s[2] = k
|
2016-03-17 00:42:53 +05:30
|
|
|
if grid[0]%2 == 0 and k == grid[0]//2: k_s[2] = 0 # for even grid, set Nyquist freq to 0 (Johnson, MIT, 2011)
|
2016-03-17 00:32:38 +05:30
|
|
|
|
2016-03-17 00:42:53 +05:30
|
|
|
xi = (k_s/geomdim)[2::-1].astype('c16') # reversing the field order
|
2016-03-17 00:32:38 +05:30
|
|
|
|
2016-03-17 00:42:53 +05:30
|
|
|
grad_fourier[i,j,k,0,:] = field_fourier[i,j,k,0]*xi *TWOPIIMG # vector field from scalar data
|
2016-03-17 00:32:38 +05:30
|
|
|
|
|
|
|
if dataType == 'vector':
|
2016-03-17 00:42:53 +05:30
|
|
|
grad_fourier[i,j,k,1,:] = field_fourier[i,j,k,1]*xi *TWOPIIMG # tensor field from vector data
|
2016-03-17 00:32:38 +05:30
|
|
|
grad_fourier[i,j,k,2,:] = field_fourier[i,j,k,2]*xi *TWOPIIMG
|
|
|
|
|
|
|
|
return np.fft.fftpack.irfftn(grad_fourier,axes=(0,1,2)).reshape([N,3*n])
|
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
|
|
|
Add column(s) containing gradient of requested column(s).
|
|
|
|
Operates on periodic ordered three-dimensional data sets.
|
|
|
|
Deals with both vector- and scalar fields.
|
|
|
|
|
|
|
|
""", version = scriptID)
|
|
|
|
|
|
|
|
parser.add_option('-c','--coordinates',
|
|
|
|
dest = 'coords',
|
|
|
|
type = 'string', metavar='string',
|
2016-04-16 03:57:23 +05:30
|
|
|
help = 'column label of coordinates [%default]')
|
2016-03-17 00:32:38 +05:30
|
|
|
parser.add_option('-v','--vector',
|
|
|
|
dest = 'vector',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
2016-04-16 03:57:23 +05:30
|
|
|
help = 'column label(s) of vector field values')
|
2016-03-17 00:32:38 +05:30
|
|
|
parser.add_option('-s','--scalar',
|
|
|
|
dest = 'scalar',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
2016-04-16 03:57:23 +05:30
|
|
|
help = 'column label(s) of scalar field values')
|
2016-03-17 00:32:38 +05:30
|
|
|
|
2016-04-16 03:57:23 +05:30
|
|
|
parser.set_defaults(coords = 'pos',
|
2016-03-17 00:32:38 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
if options.vector is None and options.scalar is None:
|
|
|
|
parser.error('no data column specified.')
|
|
|
|
|
2016-03-17 00:42:53 +05:30
|
|
|
# --- loop over input files ------------------------------------------------------------------------
|
2016-03-17 00:32:38 +05:30
|
|
|
|
|
|
|
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 ----------------------------------------
|
|
|
|
|
|
|
|
items = {
|
|
|
|
'scalar': {'dim': 1, 'shape': [1], 'labels':options.scalar, 'active':[], 'column': []},
|
2016-04-16 03:57:23 +05:30
|
|
|
'vector': {'dim': 3, 'shape': [3], 'labels':options.vector, 'active':[], 'column': []},
|
2016-03-17 00:32:38 +05:30
|
|
|
}
|
|
|
|
errors = []
|
|
|
|
remarks = []
|
|
|
|
column = {}
|
|
|
|
|
|
|
|
if table.label_dimension(options.coords) != 3: errors.append('coordinates {} are not a vector.'.format(options.coords))
|
|
|
|
else: colCoord = table.label_index(options.coords)
|
|
|
|
|
|
|
|
for type, data in items.iteritems():
|
|
|
|
for what in (data['labels'] if data['labels'] is not None else []):
|
|
|
|
dim = table.label_dimension(what)
|
|
|
|
if dim != data['dim']: remarks.append('column {} is not a {}.'.format(what,type))
|
|
|
|
else:
|
|
|
|
items[type]['active'].append(what)
|
|
|
|
items[type]['column'].append(table.label_index(what))
|
|
|
|
|
|
|
|
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:]))
|
|
|
|
for type, data in items.iteritems():
|
|
|
|
for label in data['active']:
|
2016-03-17 00:42:53 +05:30
|
|
|
table.labels_append(['{}_gradFFT({})'.format(i+1,label) for i in xrange(3 * data['dim'])]) # extend ASCII header with new labels
|
2016-03-17 00:32:38 +05:30
|
|
|
table.head_write()
|
|
|
|
|
|
|
|
# --------------- figure out size and grid ---------------------------------------------------------
|
|
|
|
|
|
|
|
table.data_readArray()
|
|
|
|
|
|
|
|
coords = [np.unique(table.data[:,colCoord+i]) for i in xrange(3)]
|
|
|
|
mincorner = np.array(map(min,coords))
|
|
|
|
maxcorner = np.array(map(max,coords))
|
|
|
|
grid = np.array(map(len,coords),'i')
|
|
|
|
size = grid/np.maximum(np.ones(3,'d'), grid-1.0) * (maxcorner-mincorner) # size from edge to edge = dim * n/(n-1)
|
|
|
|
size = np.where(grid > 1, size, min(size[grid > 1]/grid[grid > 1]))
|
|
|
|
|
|
|
|
# ------------------------------------------ process value field -----------------------------------
|
|
|
|
|
|
|
|
stack = [table.data]
|
|
|
|
for type, data in items.iteritems():
|
|
|
|
for i,label in enumerate(data['active']):
|
2016-03-17 00:42:53 +05:30
|
|
|
# we need to reverse order here, because x is fastest,ie rightmost, but leftmost in our x,y,z notation
|
|
|
|
stack.append(gradFFT(size[::-1],
|
2016-03-17 00:32:38 +05:30
|
|
|
table.data[:,data['column'][i]:data['column'][i]+data['dim']].
|
|
|
|
reshape([grid[2],grid[1],grid[0]]+data['shape'])))
|
|
|
|
|
|
|
|
# ------------------------------------------ output result -----------------------------------------
|
|
|
|
|
|
|
|
if len(stack) > 1: table.data = np.hstack(tuple(stack))
|
|
|
|
table.data_writeArray('%.12g')
|
|
|
|
|
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
|
|
|
|
|
|
|
table.close() # close input ASCII table (works for stdin)
|