2012-04-18 15:28:59 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2012-04-18 15:28:59 +05:30
|
|
|
|
2016-03-01 22:55:14 +05:30
|
|
|
import os,sys,math
|
2014-07-22 01:25:05 +05:30
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2012-04-18 15:28:59 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2012-04-18 15:28:59 +05:30
|
|
|
|
2015-12-03 02:23:02 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
def deformedCoordsFFT(F,undeformed=False):
|
2016-03-02 01:14:43 +05:30
|
|
|
|
2015-12-03 02:23:02 +05:30
|
|
|
wgt = 1.0/grid.prod()
|
2015-12-06 03:04:39 +05:30
|
|
|
integrator = np.array([0.+1.j,0.+1.j,0.+1.j],'c16') * size/ 2.0 / math.pi
|
|
|
|
step = size/grid
|
|
|
|
|
|
|
|
F_fourier = np.fft.rfftn(F,axes=(0,1,2))
|
|
|
|
coords_fourier = np.zeros(F_fourier.shape[0:4],'c16')
|
2015-12-03 02:23:02 +05:30
|
|
|
|
|
|
|
if undeformed:
|
|
|
|
Favg=np.eye(3)
|
|
|
|
else:
|
|
|
|
Favg=np.real(F_fourier[0,0,0,:,:])*wgt
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
# integration in Fourier space
|
|
|
|
k_s = np.zeros([3],'i')
|
2015-12-06 03:04:39 +05:30
|
|
|
for i in xrange(grid[2]):
|
|
|
|
k_s[2] = i
|
|
|
|
if(i > grid[2]//2 ): k_s[2] = k_s[2] - grid[2]
|
2015-12-03 02:23:02 +05:30
|
|
|
for j in xrange(grid[1]):
|
|
|
|
k_s[1] = j
|
2015-12-06 03:04:39 +05:30
|
|
|
if(j > grid[1]//2 ): k_s[1] = k_s[1] - grid[1]
|
|
|
|
for k in xrange(grid[0]//2+1):
|
|
|
|
k_s[0] = k
|
2015-12-03 02:23:02 +05:30
|
|
|
for m in xrange(3):
|
2015-12-06 03:04:39 +05:30
|
|
|
coords_fourier[i,j,k,m] = sum(F_fourier[i,j,k,m,0:3]*k_s*integrator)
|
2015-12-03 02:23:02 +05:30
|
|
|
if (any(k_s != 0)):
|
2015-12-06 03:04:39 +05:30
|
|
|
coords_fourier[i,j,k,0:3] /= -sum(k_s*k_s)
|
|
|
|
|
2015-12-03 02:23:02 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
# add average to scaled fluctuation and put (0,0,0) on (0,0,0)
|
2015-12-06 03:04:39 +05:30
|
|
|
coords = np.fft.irfftn(coords_fourier,F.shape[0:3],axes=(0,1,2))
|
|
|
|
|
|
|
|
offset_coords = np.dot(F[0,0,0,:,:],step/2.0) - scaling*coords[0,0,0,0:3]
|
|
|
|
for z in xrange(grid[2]):
|
|
|
|
for y in xrange(grid[1]):
|
|
|
|
for x in xrange(grid[0]):
|
|
|
|
coords[z,y,x,0:3] = scaling*coords[z,y,x,0:3] \
|
2015-12-03 02:23:02 +05:30
|
|
|
+ offset_coords \
|
2015-12-06 03:04:39 +05:30
|
|
|
+ np.dot(Favg,step*np.array([x,y,z]))
|
|
|
|
|
2015-12-03 02:23:02 +05:30
|
|
|
return coords
|
|
|
|
|
2012-04-18 15:28:59 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2014-07-22 01:25:05 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options file[s]', description = """
|
2015-06-29 15:14:49 +05:30
|
|
|
Add deformed configuration of given initial coordinates.
|
2015-08-08 00:33:26 +05:30
|
|
|
Operates on periodic three-dimensional x,y,z-ordered data sets.
|
2012-04-18 15:28:59 +05:30
|
|
|
|
2014-08-06 18:57:09 +05:30
|
|
|
""", version = scriptID)
|
2012-04-18 15:28:59 +05:30
|
|
|
|
2015-12-03 02:23:02 +05:30
|
|
|
parser.add_option('-f', '--defgrad',dest='defgrad', metavar = 'string',
|
|
|
|
help='heading of deformation gradient columns [%default]')
|
|
|
|
parser.add_option('--reference', dest='undeformed', action='store_true',
|
|
|
|
help='map results to reference (undeformed) average configuration [%default]')
|
|
|
|
parser.add_option('--scaling', dest='scaling', action='extend', metavar = '<float LIST>',
|
|
|
|
help='scaling of fluctuation')
|
|
|
|
parser.add_option('-u', '--unitlength', dest='unitlength', type='float', metavar = 'float',
|
|
|
|
help='set unit length for 2D model [%default]')
|
|
|
|
parser.add_option('--coordinates', dest='coords', metavar='string',
|
|
|
|
help='column heading for coordinates [%default]')
|
|
|
|
|
|
|
|
parser.set_defaults(defgrad = 'f')
|
|
|
|
parser.set_defaults(coords = 'ipinitialcoord')
|
|
|
|
parser.set_defaults(scaling = [])
|
|
|
|
parser.set_defaults(undeformed = False)
|
|
|
|
parser.set_defaults(unitlength = 0.0)
|
2012-04-18 15:28:59 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2015-12-03 02:23:02 +05:30
|
|
|
options.scaling += [1.0 for i in xrange(max(0,3-len(options.scaling)))]
|
|
|
|
scaling = map(float, options.scaling)
|
|
|
|
|
|
|
|
|
2015-06-08 10:37:14 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2012-04-18 15:28:59 +05:30
|
|
|
|
2014-07-22 01:25:05 +05:30
|
|
|
for name in filenames:
|
2015-08-21 01:12:05 +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)
|
2012-04-18 15:28:59 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ read header ------------------------------------------
|
2015-06-08 10:37:14 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.head_read()
|
|
|
|
|
|
|
|
# ------------------------------------------ sanity checks ----------------------------------------
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
remarks = []
|
|
|
|
|
|
|
|
if table.label_dimension(options.coords) != 3: errors.append('coordinates {} are not a vector.'.format(options.coords))
|
|
|
|
else: colCoord = table.label_index(options.coords)
|
|
|
|
|
|
|
|
if table.label_dimension(options.defgrad) != 9: errors.append('deformation gradient {} is not a tensor.'.format(options.defgrad))
|
|
|
|
else: colF = table.label_index(options.defgrad)
|
|
|
|
|
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)
|
2015-06-08 10:37:14 +05:30
|
|
|
continue
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# --------------- figure out size and grid ---------------------------------------------------------
|
2015-06-08 10:37:14 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.data_readArray()
|
2012-04-18 15:28:59 +05:30
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
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)
|
2016-03-02 01:14:43 +05:30
|
|
|
size = np.where(grid > 1, size, min(size[grid > 1]/grid[grid > 1])) # spacing for grid==1 set to smallest among other spacings
|
2012-04-18 15:28:59 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
N = grid.prod()
|
2015-12-03 02:23:02 +05:30
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
if N != len(table.data): errors.append('data count {} does not match grid {}x{}x{}.'.format(N,*grid))
|
|
|
|
if errors != []:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak(errors)
|
2015-08-21 01:12:05 +05:30
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2015-09-01 02:52:44 +05:30
|
|
|
for coord in xrange(3):
|
2015-12-03 02:23:02 +05:30
|
|
|
label = '{}_{}_{}'.format(coord+1,options.defgrad,options.coords)
|
|
|
|
if np.any(scaling) != 1.0: label+='_{}_{}_{}'.format(scaling)
|
|
|
|
if options.undeformed: label+='_undeformed'
|
|
|
|
table.labels_append([label]) # extend ASCII header with new labels
|
2012-04-18 15:28:59 +05:30
|
|
|
table.head_write()
|
|
|
|
|
2015-09-01 02:52:44 +05:30
|
|
|
# ------------------------------------------ read deformation gradient field -----------------------
|
2015-12-06 03:04:39 +05:30
|
|
|
centroids = deformedCoordsFFT(table.data[:,colF:colF+9].reshape(grid[2],grid[1],grid[0],3,3),
|
2015-12-03 02:23:02 +05:30
|
|
|
options.undeformed)
|
2015-09-01 02:52:44 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
|
|
|
table.data_rewind()
|
2015-12-06 03:04:39 +05:30
|
|
|
for z in xrange(grid[2]):
|
|
|
|
for y in xrange(grid[1]):
|
|
|
|
for x in xrange(grid[0]):
|
|
|
|
table.data_read()
|
|
|
|
table.data_append(list(centroids[z,y,x,:]))
|
|
|
|
table.data_write()
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
|
|
|
|
2016-03-02 01:14:43 +05:30
|
|
|
table.close() # close ASCII tables
|