2016-07-18 23:05:35 +05:30
|
|
|
#!/usr/bin/env python2.7
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2016-03-01 22:55:14 +05:30
|
|
|
import os,sys,math
|
2014-07-16 22:11:04 +05:30
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2014-07-25 00:17:09 +05:30
|
|
|
|
2015-04-24 13:37:13 +05:30
|
|
|
def divFFT(geomdim,field):
|
2016-04-11 23:55:24 +05:30
|
|
|
shapeFFT = np.array(np.shape(field))[0:3]
|
2016-03-17 00:25:56 +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
|
2015-04-24 13:37:13 +05:30
|
|
|
|
2016-11-07 13:36:35 +05:30
|
|
|
if n == 3: dataType = 'vector'
|
|
|
|
elif n == 9: dataType = 'tensor'
|
|
|
|
|
2016-06-29 14:28:15 +05:30
|
|
|
field_fourier = np.fft.rfftn(field,axes=(0,1,2),s=shapeFFT)
|
2016-04-11 23:55:24 +05:30
|
|
|
div_fourier = np.empty(field_fourier.shape[0:len(np.shape(field))-1],'c16') # size depents on whether tensor or vector
|
2015-04-24 13:37:13 +05:30
|
|
|
|
|
|
|
# differentiation in Fourier space
|
2016-03-17 00:25:56 +05:30
|
|
|
TWOPIIMG = 2.0j*math.pi
|
2016-11-04 23:20:39 +05:30
|
|
|
k_sk = np.where(np.arange(grid[2])>grid[2]//2,np.arange(grid[2])-grid[2],np.arange(grid[2]))/geomdim[0]
|
|
|
|
if grid[2]%2 == 0: k_sk[grid[2]//2] = 0 # for even grid, set Nyquist freq to 0 (Johnson, MIT, 2011)
|
|
|
|
|
|
|
|
k_sj = np.where(np.arange(grid[1])>grid[1]//2,np.arange(grid[1])-grid[1],np.arange(grid[1]))/geomdim[1]
|
|
|
|
if grid[1]%2 == 0: k_sj[grid[1]//2] = 0 # for even grid, set Nyquist freq to 0 (Johnson, MIT, 2011)
|
|
|
|
|
|
|
|
k_si = np.arange(grid[0]//2+1)/geomdim[2]
|
|
|
|
|
|
|
|
kk, kj, ki = np.meshgrid(k_sk,k_sj,k_si,indexing = 'ij')
|
|
|
|
k_s = np.concatenate((ki[:,:,:,None],kj[:,:,:,None],kk[:,:,:,None]),axis = 3).astype('c16')
|
2016-11-07 13:36:35 +05:30
|
|
|
if dataType == 'tensor': # tensor, 3x3 -> 3
|
2016-11-04 23:20:39 +05:30
|
|
|
div_fourier = np.einsum('ijklm,ijkm->ijkl',field_fourier,k_s)*TWOPIIMG
|
2016-11-07 13:36:35 +05:30
|
|
|
elif dataType == 'vector': # vector, 3 -> 1
|
2016-11-04 23:20:39 +05:30
|
|
|
div_fourier = np.einsum('ijkl,ijkl->ijk',field_fourier,k_s)*TWOPIIMG
|
|
|
|
|
2016-06-29 14:28:15 +05:30
|
|
|
return np.fft.irfftn(div_fourier,axes=(0,1,2),s=shapeFFT).reshape([N,n/3])
|
2015-04-24 13:37:13 +05:30
|
|
|
|
|
|
|
|
2011-06-21 21:55:48 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2016-04-25 16:27:38 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog option(s) [ASCIItable(s)]', description = """
|
2011-06-21 21:55:48 +05:30
|
|
|
Add column(s) containing divergence 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-06-21 21:55:48 +05:30
|
|
|
|
2016-04-25 16:52:34 +05:30
|
|
|
parser.add_option('-p','--pos','--periodiccellcenter',
|
2016-04-27 02:19:58 +05:30
|
|
|
dest = 'pos',
|
2015-08-08 00:33:26 +05:30
|
|
|
type = 'string', metavar = 'string',
|
2016-04-25 16:27:38 +05:30
|
|
|
help = 'label of coordinates [%default]')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-v','--vector',
|
|
|
|
dest = 'vector',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
2016-04-25 16:27:38 +05:30
|
|
|
help = 'label(s) of vector field values')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-t','--tensor',
|
|
|
|
dest = 'tensor',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
2016-04-25 16:27:38 +05:30
|
|
|
help = 'label(s) of tensor field values')
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2016-04-27 02:19:58 +05:30
|
|
|
parser.set_defaults(pos = 'pos',
|
2015-08-08 00:33:26 +05:30
|
|
|
)
|
2011-06-21 21:55:48 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2016-03-02 01:14:43 +05:30
|
|
|
if options.vector is None and options.tensor is None:
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.error('no data column specified.')
|
|
|
|
|
2016-03-17 00:42:53 +05:30
|
|
|
# --- loop over input files ------------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-08-13 14:02:09 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2016-03-17 00:25:56 +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-05-11 02:29:23 +05:30
|
|
|
|
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 = {}
|
|
|
|
|
2016-04-27 02:19:58 +05:30
|
|
|
if table.label_dimension(options.pos) != 3: errors.append('coordinates {} are not a vector.'.format(options.pos))
|
|
|
|
else: colCoord = table.label_index(options.pos)
|
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-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-11 02:29:23 +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(['divFFT({})'.format(label) if type == 'vector' else
|
2016-10-25 00:46:29 +05:30
|
|
|
'{}_divFFT({})'.format(i+1,label) for i in range(data['dim']//3)]) # 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()
|
|
|
|
|
2016-10-25 00:46:29 +05:30
|
|
|
coords = [np.unique(table.data[:,colCoord+i]) for i in range(3)]
|
2015-12-15 11:46:47 +05:30
|
|
|
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)
|
2016-03-17 00:42:53 +05:30
|
|
|
size = np.where(grid > 1, size, min(size[grid > 1]/grid[grid > 1])) # spacing for grid==1 equal to smallest among other ones
|
2014-07-16 22:11:04 +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']):
|
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(divFFT(size[::-1],
|
2016-03-17 00:25:56 +05:30
|
|
|
table.data[:,data['column'][i]:data['column'][i]+data['dim']].
|
2016-06-29 14:28:15 +05:30
|
|
|
reshape(grid[::-1].tolist()+data['shape'])))
|
2015-05-11 02:29:23 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ output result -----------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
if len(stack) > 1: table.data = np.hstack(tuple(stack))
|
2015-05-11 02:29:23 +05:30
|
|
|
table.data_writeArray('%.12g')
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
|
|
|
|
2016-03-17 00:25:56 +05:30
|
|
|
table.close() # close input ASCII table (works for stdin)
|