2016-07-18 23:05:35 +05:30
|
|
|
#!/usr/bin/env python2.7
|
2015-06-15 23:42:38 +05:30
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
import os
|
|
|
|
import sys
|
2015-06-15 23:42:38 +05:30
|
|
|
from optparse import OptionParser
|
2019-06-14 16:33:30 +05:30
|
|
|
|
|
|
|
import numpy as np
|
2015-06-15 23:42:38 +05:30
|
|
|
from PIL import Image, ImageDraw
|
2019-06-14 16:33:30 +05:30
|
|
|
|
2015-06-15 23:42:38 +05:30
|
|
|
import damask
|
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2015-06-15 23:42:38 +05:30
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
|
2015-06-15 23:42:38 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
|
|
|
Generate PNG image from scalar data on grid deformed by (periodic) deformation gradient.
|
|
|
|
|
|
|
|
""", version = scriptID)
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-l','--label',
|
|
|
|
dest = 'label',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'column containing data [all]')
|
|
|
|
parser.add_option('-r','--range',
|
|
|
|
dest = 'range',
|
|
|
|
type = 'float', nargs = 2, metavar = 'float float',
|
|
|
|
help = 'data range (min max) [auto]')
|
|
|
|
parser.add_option('--gap', '--transparent',
|
|
|
|
dest = 'gap',
|
|
|
|
type = 'float', metavar = 'float',
|
|
|
|
help = 'value to treat as transparent [%default]')
|
|
|
|
parser.add_option('-d','--dimension',
|
|
|
|
dest = 'dimension',
|
|
|
|
type = 'int', nargs = 3, metavar = ' '.join(['int']*3),
|
|
|
|
help = 'data dimension (x/y/z)')
|
|
|
|
parser.add_option('-s','--size',
|
|
|
|
dest = 'size',
|
|
|
|
type = 'float', nargs = 3, metavar = ' '.join(['float']*3),
|
|
|
|
help = 'box size (x/y/z)')
|
|
|
|
parser.add_option('-f','--defgrad',
|
|
|
|
dest = 'defgrad', metavar = 'string',
|
|
|
|
help = 'column label of deformation gradient [%default]')
|
|
|
|
parser.add_option('--scaling',
|
|
|
|
dest = 'scaling',
|
|
|
|
type = 'float', nargs = 3, metavar = ' '.join(['float']*3),
|
|
|
|
help = 'x/y/z scaling of displacement fluctuation [%default]')
|
|
|
|
parser.add_option('-z','--layer',
|
|
|
|
dest = 'z',
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
help = 'index of z plane to plot [%default]')
|
|
|
|
parser.add_option('--color',
|
|
|
|
dest = 'color',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'color scheme')
|
|
|
|
parser.add_option('--invert',
|
|
|
|
dest = 'invert',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'invert color scheme')
|
|
|
|
parser.add_option('--abs',
|
|
|
|
dest = 'abs',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'magnitude of values')
|
|
|
|
parser.add_option('--log',
|
|
|
|
dest = 'log',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'log10 of values')
|
|
|
|
parser.add_option('-N','--pixelsize',
|
|
|
|
dest = 'pixelsize',
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
help = 'pixels per cell edge')
|
|
|
|
parser.add_option('--show',
|
|
|
|
dest = 'show',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'show resulting image')
|
2015-06-15 23:42:38 +05:30
|
|
|
|
|
|
|
parser.set_defaults(label = None,
|
|
|
|
range = [0.0,0.0],
|
|
|
|
dimension = [],
|
|
|
|
size = [],
|
|
|
|
z = 1,
|
|
|
|
abs = False,
|
|
|
|
log = False,
|
|
|
|
defgrad = 'f',
|
|
|
|
scaling = [1.,1.,1.],
|
|
|
|
color = "gray",
|
|
|
|
invert = False,
|
|
|
|
pixelsize = 1,
|
|
|
|
show = False,
|
|
|
|
)
|
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
options.size = np.array(options.size)
|
|
|
|
options.dimension = np.array(options.dimension)
|
|
|
|
options.range = np.array(options.range)
|
|
|
|
|
|
|
|
if options.z > 0: options.z -= 1 # adjust to 0-based indexing
|
|
|
|
|
|
|
|
# --- color palette ---------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
theMap = damask.Colormap(predefined=options.color)
|
|
|
|
if options.invert: theMap = theMap.invert()
|
|
|
|
theColors = np.uint8(np.array(theMap.export(format='list',steps=256))*255)
|
|
|
|
|
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-06-15 23:42:38 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2015-08-21 01:12:05 +05:30
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False,
|
2016-03-02 02:05:59 +05:30
|
|
|
labeled = options.label is not None,
|
2015-08-21 01:12:05 +05:30
|
|
|
readonly = True)
|
|
|
|
except: continue
|
2015-09-11 18:25:43 +05:30
|
|
|
table.report_name(scriptName,name)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ read header ------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
|
2015-06-15 23:42:38 +05:30
|
|
|
# --------------- figure out columns to process ---------------------------------------------------
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
if table.label_dimension(options.label) != 1:
|
2015-08-08 00:33:26 +05:30
|
|
|
errors.append('no scalar data ({}) found.'.format(options.label))
|
2015-06-15 23:42:38 +05:30
|
|
|
if table.label_dimension(options.defgrad) != 9:
|
2015-08-08 00:33:26 +05:30
|
|
|
errors.append('no deformation gradient tensor (1..9_{}) found.'.format(options.defgrad))
|
2015-06-15 23:42:38 +05:30
|
|
|
|
|
|
|
if errors != []:
|
2015-08-18 10:11:36 +05:30
|
|
|
table.croak('\n'.join(errors)+'\n')
|
2015-06-15 23:42:38 +05:30
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
|
|
|
|
|
|
|
table.data_readArray([options.label,options.defgrad])
|
|
|
|
|
|
|
|
data = table.data[:,0 ].transpose().reshape( list(options.dimension),order='F')
|
2015-08-08 00:33:26 +05:30
|
|
|
F = table.data[:,1:10].transpose().reshape([3,3]+list(options.dimension),order='F')
|
2015-06-15 23:42:38 +05:30
|
|
|
|
|
|
|
if options.abs: data = np.abs(data)
|
|
|
|
if options.log: data = np.log10(data)
|
|
|
|
if np.all(options.range == 0.0): options.range = np.array([data.min(),data.max()])
|
|
|
|
elif options.log: options.range = np.log10(options.range)
|
|
|
|
|
|
|
|
data = ( data - options.range.min()) / \
|
|
|
|
(options.range.max() - options.range.min()) # data scaled to fraction of range
|
|
|
|
|
|
|
|
data = np.clip(data,0.0,1.0) # cut off outliers (should be none)
|
|
|
|
|
|
|
|
# ---------------- calculate coordinates -----------------------------------------------------------
|
|
|
|
|
|
|
|
Favg = damask.core.math.tensorAvg(F)
|
|
|
|
centroids = damask.core.mesh.deformedCoordsFFT(options.size,F,Favg,options.scaling)
|
|
|
|
nodes = damask.core.mesh.nodesAroundCentres(options.size,Favg,centroids)
|
|
|
|
|
|
|
|
boundingBox = np.array([ \
|
|
|
|
[np.amin(nodes[0,:,:,options.z]),np.amin(nodes[1,:,:,options.z]),np.amin(nodes[2,:,:,options.z])],
|
|
|
|
[np.amax(nodes[0,:,:,options.z]),np.amax(nodes[1,:,:,options.z]),np.amax(nodes[2,:,:,options.z])],
|
|
|
|
]) # find x-y bounding box for given z layer
|
|
|
|
|
|
|
|
nodes -= boundingBox[0].repeat(np.prod(options.dimension+1)).reshape([3]+list(options.dimension+1))
|
2016-03-02 02:29:12 +05:30
|
|
|
nodes *= (options.pixelsize*options.dimension/options.size).repeat(np.prod(options.dimension+1)).\
|
|
|
|
reshape([3]+list(options.dimension+1))
|
2016-03-02 02:05:59 +05:30
|
|
|
imagesize = (options.pixelsize*(boundingBox[1]-boundingBox[0])* # determine image size from number of
|
|
|
|
options.dimension/options.size)[:2].astype('i') # cells in overall bounding box
|
2015-06-15 23:42:38 +05:30
|
|
|
im = Image.new('RGBA',imagesize)
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
2016-10-25 00:46:29 +05:30
|
|
|
for y in range(options.dimension[1]):
|
|
|
|
for x in range(options.dimension[0]):
|
2015-06-15 23:42:38 +05:30
|
|
|
draw.polygon([nodes[0,x ,y ,options.z],
|
|
|
|
nodes[1,x ,y ,options.z],
|
|
|
|
nodes[0,x+1,y ,options.z],
|
|
|
|
nodes[1,x+1,y ,options.z],
|
|
|
|
nodes[0,x+1,y+1,options.z],
|
|
|
|
nodes[1,x+1,y+1,options.z],
|
|
|
|
nodes[0,x ,y+1,options.z],
|
|
|
|
nodes[1,x ,y+1,options.z],
|
|
|
|
],
|
2015-08-08 00:33:26 +05:30
|
|
|
fill = tuple(theColors[int(255*data[x,y,options.z])],
|
|
|
|
0 if data[x,y,options.z] == options.gap else 255),
|
2015-06-15 23:42:38 +05:30
|
|
|
outline = None)
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ output result -----------------------------------------
|
2015-06-15 23:42:38 +05:30
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
im.save(os.path.splitext(name)[0]+ \
|
|
|
|
('_'+options.label if options.label else '')+ \
|
|
|
|
'.png' if name else sys.stdout,
|
2015-08-08 00:33:26 +05:30
|
|
|
format = "PNG")
|
2015-06-15 23:42:38 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close() # close ASCII table
|
2015-06-15 23:42:38 +05:30
|
|
|
if options.show: im.show()
|