2012-02-07 18:39:10 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2011-12-21 22:55:31 +05:30
|
|
|
|
2015-09-24 14:54:42 +05:30
|
|
|
import os,sys,string,math
|
2014-07-10 14:57:51 +05:30
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2011-12-21 22:55:31 +05:30
|
|
|
|
2014-08-06 18:57:09 +05:30
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
2014-12-19 00:56:52 +05:30
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
2011-12-21 22:55:31 +05:30
|
|
|
|
2015-04-24 13:37:13 +05:30
|
|
|
def curlFFT(geomdim,field):
|
|
|
|
grid = np.array(np.shape(field)[0:3])
|
2015-08-08 00:33:26 +05:30
|
|
|
N = grid.prod() # field size
|
|
|
|
n = np.array(np.shape(field)[3:]).prod() # data size
|
2015-04-24 13:37:13 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if n == 3:
|
2015-04-24 13:37:13 +05:30
|
|
|
dataType = 'vector'
|
2015-08-08 00:33:26 +05:30
|
|
|
elif n == 9:
|
2015-04-24 13:37:13 +05:30
|
|
|
dataType = 'tensor'
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
field_fourier = np.fft.fftpack.rfftn(field,axes=(0,1,2))
|
|
|
|
curl_fourier = np.zeros(field_fourier.shape,'c16')
|
2015-04-24 13:37:13 +05:30
|
|
|
|
|
|
|
# differentiation in Fourier space
|
2015-08-08 00:33:26 +05:30
|
|
|
k_s = np.zeros([3],'i')
|
2015-04-24 13:37:13 +05:30
|
|
|
TWOPIIMG = (0.0+2.0j*math.pi)
|
|
|
|
for i in xrange(grid[0]):
|
|
|
|
k_s[0] = i
|
2015-09-05 17:21:36 +05:30
|
|
|
if(grid[0]%2==0 and i == grid[0]//2): # for even grid, set Nyquist freq to 0 (Johnson, MIT, 2011)
|
|
|
|
k_s[0]=0
|
|
|
|
elif (i > grid[0]//2):
|
|
|
|
k_s[0] = k_s[0] - grid[0]
|
|
|
|
|
2015-04-24 13:37:13 +05:30
|
|
|
for j in xrange(grid[1]):
|
|
|
|
k_s[1] = j
|
2015-09-05 17:21:36 +05:30
|
|
|
if(grid[1]%2==0 and j == grid[1]//2): # for even grid, set Nyquist freq to 0 (Johnson, MIT, 2011)
|
|
|
|
k_s[1]=0
|
|
|
|
elif (j > grid[1]//2):
|
|
|
|
k_s[1] = k_s[1] - grid[1]
|
|
|
|
|
|
|
|
for k in xrange(grid[2]//2+1):
|
2015-04-24 13:37:13 +05:30
|
|
|
k_s[2] = k
|
2015-09-05 17:21:36 +05:30
|
|
|
if(grid[2]%2==0 and k == grid[2]//2): # for even grid, set Nyquist freq to 0 (Johnson, MIT, 2011)
|
|
|
|
k_s[2]=0
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
xi = np.array([k_s[2]/geomdim[2]+0.0j,k_s[1]/geomdim[1]+0.j,k_s[0]/geomdim[0]+0.j],'c16')
|
2015-04-24 13:37:13 +05:30
|
|
|
if dataType == 'tensor':
|
|
|
|
for l in xrange(3):
|
|
|
|
curl_fourier[i,j,k,0,l] = ( field_fourier[i,j,k,l,2]*xi[1]\
|
|
|
|
-field_fourier[i,j,k,l,1]*xi[2]) *TWOPIIMG
|
|
|
|
curl_fourier[i,j,k,1,l] = (-field_fourier[i,j,k,l,2]*xi[0]\
|
|
|
|
+field_fourier[i,j,k,l,0]*xi[2]) *TWOPIIMG
|
|
|
|
curl_fourier[i,j,k,2,l] = ( field_fourier[i,j,k,l,1]*xi[0]\
|
|
|
|
-field_fourier[i,j,k,l,0]*xi[1]) *TWOPIIMG
|
|
|
|
elif dataType == 'vector':
|
|
|
|
curl_fourier[i,j,k,0] = ( field_fourier[i,j,k,2]*xi[1]\
|
|
|
|
-field_fourier[i,j,k,1]*xi[2]) *TWOPIIMG
|
|
|
|
curl_fourier[i,j,k,1] = (-field_fourier[i,j,k,2]*xi[0]\
|
|
|
|
+field_fourier[i,j,k,0]*xi[2]) *TWOPIIMG
|
|
|
|
curl_fourier[i,j,k,2] = ( field_fourier[i,j,k,1]*xi[0]\
|
|
|
|
-field_fourier[i,j,k,0]*xi[1]) *TWOPIIMG
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
return np.fft.fftpack.irfftn(curl_fourier,axes=(0,1,2)).reshape([N,n])
|
2015-04-24 13:37:13 +05:30
|
|
|
|
|
|
|
|
2011-12-21 22:55:31 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2014-07-10 14:57:51 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2011-12-21 22:55:31 +05:30
|
|
|
Add column(s) containing curl of requested column(s).
|
|
|
|
Operates on periodic ordered three-dimensional data sets.
|
|
|
|
Deals with both vector- and tensor-valued fields.
|
|
|
|
|
2014-08-06 18:57:09 +05:30
|
|
|
""", version = scriptID)
|
2011-12-21 22:55:31 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-c','--coordinates',
|
|
|
|
dest = 'coords',
|
|
|
|
type = 'string', metavar='string',
|
|
|
|
help = 'column heading for coordinates [%default]')
|
|
|
|
parser.add_option('-v','--vector',
|
|
|
|
dest = 'vector',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'heading of columns containing vector field values')
|
|
|
|
parser.add_option('-t','--tensor',
|
|
|
|
dest = 'tensor',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'heading of columns containing tensor field values')
|
|
|
|
|
|
|
|
parser.set_defaults(coords = 'ipinitialcoord',
|
|
|
|
)
|
2011-12-21 22:55:31 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2015-05-10 16:59:11 +05:30
|
|
|
if options.vector == None and options.tensor == None:
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.error('no data column specified.')
|
|
|
|
|
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
2015-08-13 14:02:09 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2015-08-13 14:02:09 +05:30
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name,buffered = False)
|
|
|
|
except:
|
|
|
|
continue
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ read header ------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
|
|
|
|
# ------------------------------------------ sanity checks ----------------------------------------
|
|
|
|
|
|
|
|
items = {
|
|
|
|
'tensor': {'dim': 9, 'shape': [3,3], 'labels':options.tensor, 'active':[], 'column': []},
|
|
|
|
'vector': {'dim': 3, 'shape': [3], 'labels':options.vector, 'active':[], 'column': []},
|
|
|
|
}
|
|
|
|
errors = []
|
|
|
|
remarks = []
|
|
|
|
column = {}
|
|
|
|
|
|
|
|
if table.label_dimension(options.coords) != 3: errors.append('coordinates {} are not a vector.'.format(options.coords))
|
|
|
|
else: coordCol = table.label_index(options.coords)
|
2015-05-10 16:59:11 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
for type, data in items.iteritems():
|
2015-08-13 14:02:09 +05:30
|
|
|
for what in (data['labels'] if data['labels'] is not None else []):
|
2015-08-08 00:33:26 +05:30
|
|
|
dim = table.label_dimension(what)
|
|
|
|
if dim != data['dim']: remarks.append('column {} is not a {}.'.format(what,type))
|
2015-04-24 13:37:13 +05:30
|
|
|
else:
|
2015-08-08 00:33:26 +05:30
|
|
|
items[type]['active'].append(what)
|
|
|
|
items[type]['column'].append(table.label_index(what))
|
2015-04-24 13:37:13 +05:30
|
|
|
|
2015-09-24 14:54:42 +05:30
|
|
|
if remarks != []: damask.util.croak(remarks)
|
2015-08-08 00:33:26 +05:30
|
|
|
if errors != []:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak(errors)
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# ------------------------------------------ assemble header --------------------------------------
|
2015-04-24 13:37:13 +05:30
|
|
|
|
2015-05-10 16:59:11 +05:30
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2015-08-08 00:33:26 +05:30
|
|
|
for type, data in items.iteritems():
|
|
|
|
for label in data['active']:
|
|
|
|
table.labels_append(['{}_curlFFT({})'.format(i+1,label) for i in xrange(data['dim'])]) # extend ASCII header with new labels
|
2015-04-24 13:37:13 +05:30
|
|
|
table.head_write()
|
|
|
|
|
|
|
|
# --------------- figure out size and grid ---------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
table.data_readArray()
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
coords = [{},{},{}]
|
2015-08-08 00:33:26 +05:30
|
|
|
for i in xrange(len(table.data)):
|
2012-02-08 16:48:15 +05:30
|
|
|
for j in xrange(3):
|
2015-08-08 00:33:26 +05:30
|
|
|
coords[j][str(table.data[i,coordCol+j])] = True
|
2015-05-10 16:59:11 +05:30
|
|
|
grid = np.array(map(len,coords),'i')
|
2014-08-06 20:55:18 +05:30
|
|
|
size = grid/np.maximum(np.ones(3,'d'),grid-1.0)* \
|
|
|
|
np.array([max(map(float,coords[0].keys()))-min(map(float,coords[0].keys())),\
|
|
|
|
max(map(float,coords[1].keys()))-min(map(float,coords[1].keys())),\
|
|
|
|
max(map(float,coords[2].keys()))-min(map(float,coords[2].keys())),\
|
|
|
|
],'d') # size from bounding box, corrected for cell-centeredness
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
size = np.where(grid > 1, size, min(size[grid > 1]/grid[grid > 1])) # spacing for grid==1 equal to smallest among other spacings
|
2014-07-10 14:57:51 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ process value field -----------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
stack = [table.data]
|
|
|
|
for type, data in items.iteritems():
|
|
|
|
for i,label in enumerate(data['active']):
|
|
|
|
stack.append(curlFFT(size[::-1], # we need to reverse order here, because x is fastest,ie rightmost, but leftmost in our x,y,z notation
|
|
|
|
table.data[:,data['column'][i]:data['column'][i]+data['dim']].\
|
|
|
|
reshape([grid[2],grid[1],grid[0]]+data['shape'])))
|
2015-05-10 16:59:11 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ output result -----------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
if len(stack) > 1: table.data = np.hstack(tuple(stack))
|
2015-05-10 16:59:11 +05:30
|
|
|
table.data_writeArray('%.12g')
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
|
|
|
|
|
|
|
table.close() # close input ASCII table (works for stdin)
|