2018-11-17 12:42:12 +05:30
|
|
|
#!/usr/bin/env python3
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2011-12-21 22:55:31 +05:30
|
|
|
|
2016-03-01 22:55:14 +05:30
|
|
|
import os,sys,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
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2011-12-21 22:55:31 +05:30
|
|
|
|
2018-01-30 00:42:19 +05:30
|
|
|
def merge_dicts(*dict_args):
|
2018-01-30 01:00:26 +05:30
|
|
|
"""Given any number of dicts, shallow copy and merge into a new dict, with precedence going to key value pairs in latter dicts."""
|
|
|
|
result = {}
|
|
|
|
for dictionary in dict_args:
|
|
|
|
result.update(dictionary)
|
|
|
|
return result
|
2018-01-30 00:42:19 +05:30
|
|
|
|
2015-04-24 13:37:13 +05:30
|
|
|
def curlFFT(geomdim,field):
|
2018-01-30 01:00:26 +05:30
|
|
|
"""Calculate curl of a vector or tensor field by transforming into Fourier space."""
|
|
|
|
shapeFFT = np.array(np.shape(field))[0:3]
|
|
|
|
grid = np.array(np.shape(field)[2::-1])
|
|
|
|
N = grid.prod() # field size
|
|
|
|
n = np.array(np.shape(field)[3:]).prod() # data size
|
|
|
|
|
|
|
|
field_fourier = np.fft.rfftn(field,axes=(0,1,2),s=shapeFFT)
|
|
|
|
curl_fourier = np.empty(field_fourier.shape,'c16')
|
|
|
|
|
|
|
|
# differentiation in Fourier space
|
|
|
|
TWOPIIMG = 2.0j*math.pi
|
|
|
|
einsums = {
|
|
|
|
3:'slm,ijkl,ijkm->ijks', # vector, 3 -> 3
|
|
|
|
9:'slm,ijkl,ijknm->ijksn', # tensor, 3x3 -> 3x3
|
|
|
|
}
|
|
|
|
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 # Nyquist freq=0 for even grid (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 # Nyquist freq=0 for even grid (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')
|
|
|
|
|
|
|
|
e = np.zeros((3, 3, 3))
|
|
|
|
e[0, 1, 2] = e[1, 2, 0] = e[2, 0, 1] = 1.0 # Levi-Civita symbols
|
|
|
|
e[0, 2, 1] = e[2, 1, 0] = e[1, 0, 2] = -1.0
|
|
|
|
|
|
|
|
curl_fourier = np.einsum(einsums[n],e,k_s,field_fourier)*TWOPIIMG
|
|
|
|
|
2019-02-15 23:22:47 +05:30
|
|
|
return np.fft.irfftn(curl_fourier,axes=(0,1,2),s=shapeFFT).reshape([N,n])
|
2015-04-24 13:37:13 +05:30
|
|
|
|
|
|
|
|
2011-12-21 22:55:31 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2019-02-16 22:11:56 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """
|
2011-12-21 22:55:31 +05:30
|
|
|
Add column(s) containing curl of requested column(s).
|
2018-01-29 04:55:50 +05:30
|
|
|
Operates on periodic ordered three-dimensional data sets
|
|
|
|
of vector and tensor fields.
|
2014-08-06 18:57:09 +05:30
|
|
|
""", version = scriptID)
|
2011-12-21 22:55:31 +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',
|
2016-04-16 03:57:23 +05:30
|
|
|
type = 'string', metavar = 'string',
|
2016-04-25 16:27:38 +05:30
|
|
|
help = 'label of coordinates [%default]')
|
2018-01-30 07:57:05 +05:30
|
|
|
parser.add_option('-l','--label',
|
2018-01-29 04:55:50 +05:30
|
|
|
dest = 'data',
|
2015-08-08 00:33:26 +05:30
|
|
|
action = 'extend', metavar = '<string LIST>',
|
2018-01-29 04:55:50 +05:30
|
|
|
help = 'label(s) of 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-12-21 22:55:31 +05:30
|
|
|
|
2018-01-30 00:42:19 +05:30
|
|
|
|
2011-12-21 22:55:31 +05:30
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2018-01-29 04:55:50 +05:30
|
|
|
if options.data is None: parser.error('no data column specified.')
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2018-01-30 00:42:19 +05:30
|
|
|
# --- define possible data types -------------------------------------------------------------------
|
|
|
|
|
|
|
|
datatypes = {
|
|
|
|
3: {'name': 'vector',
|
|
|
|
'shape': [3],
|
|
|
|
},
|
|
|
|
9: {'name': 'tensor',
|
|
|
|
'shape': [3,3],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:27:38 +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-08-08 00:33:26 +05:30
|
|
|
|
2018-01-29 04:55:50 +05:30
|
|
|
# --- interpret header ----------------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
|
|
|
|
remarks = []
|
2018-01-29 04:55:50 +05:30
|
|
|
errors = []
|
2018-01-30 00:42:19 +05:30
|
|
|
active = []
|
2018-01-29 04:55:50 +05:30
|
|
|
|
|
|
|
coordDim = table.label_dimension(options.pos)
|
|
|
|
if coordDim != 3:
|
|
|
|
errors.append('coordinates "{}" must be three-dimensional.'.format(options.pos))
|
|
|
|
else: coordCol = table.label_index(options.pos)
|
|
|
|
|
2018-01-30 00:42:19 +05:30
|
|
|
for me in options.data:
|
|
|
|
dim = table.label_dimension(me)
|
|
|
|
if dim in datatypes:
|
|
|
|
active.append(merge_dicts({'label':me},datatypes[dim]))
|
|
|
|
remarks.append('differentiating {} "{}"...'.format(datatypes[dim]['name'],me))
|
2018-01-29 04:55:50 +05:30
|
|
|
else:
|
2018-01-30 00:42:19 +05:30
|
|
|
remarks.append('skipping "{}" of dimension {}...'.format(me,dim) if dim != -1 else \
|
|
|
|
'"{}" not found...'.format(me) )
|
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:]))
|
2018-01-30 00:42:19 +05:30
|
|
|
for data in active:
|
2018-01-30 01:00:26 +05:30
|
|
|
table.labels_append(['{}_curlFFT({})'.format(i+1,data['label'])
|
|
|
|
for i in range(np.prod(np.array(data['shape'])))]) # 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()
|
2018-01-30 23:28:43 +05:30
|
|
|
grid,size = damask.util.coordGridAndSize(table.data[:,table.label_indexrange(options.pos)])
|
2014-07-10 14:57:51 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ process value field -----------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
stack = [table.data]
|
2018-01-30 00:42:19 +05:30
|
|
|
for data in active:
|
|
|
|
# we need to reverse order here, because x is fastest,ie rightmost, but leftmost in our x,y,z notation
|
|
|
|
stack.append(curlFFT(size[::-1],
|
|
|
|
table.data[:,table.label_indexrange(data['label'])].
|
|
|
|
reshape(grid[::-1].tolist()+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)
|