replace geom_canvas --blank with geom_fromScratch

This commit is contained in:
Philip Eisenlohr 2019-05-29 17:53:45 -06:00
parent 17168525b6
commit d79f0c6290
3 changed files with 131 additions and 37 deletions

@ -1 +1 @@
Subproject commit d596982319b5e8bbded6c14a5724df6c9de2a0fd
Subproject commit 36b3a1b44a71574e2b5dce93ed25963e9fe3e74a

View File

@ -1,78 +1,98 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 no BOM -*-
import os
import sys
import numpy as np
import damask
from io import StringIO
from optparse import OptionParser
import numpy as np
import damask
scriptName = os.path.splitext(os.path.basename(__file__))[0]
scriptID = ' '.join([scriptName,damask.version])
# --------------------------------------------------------------------
# MAIN
# --------------------------------------------------------------------
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [geomfile(s)]', description = """
Increases or decreases the (three-dimensional) canvas.
parser = OptionParser(option_class=damask.extendableOption, usage='%prog option(s) [geomfile(s)]', description = """
Changes the (three-dimensional) canvas of a spectral geometry description.
Grid can be given as absolute or relative values, e.g. 16 16 16 or 2x 0.5x 32.
""", version = scriptID)
parser.add_option('-g','--grid',
parser.add_option('-g',
'--grid',
dest = 'grid',
type = 'string', nargs = 3, metavar = ' '.join(['string']*3),
help = 'a,b,c grid of hexahedral box')
parser.add_option('-o','--offset',
help = 'a,b,c grid of hexahedral box. [auto]')
parser.add_option('-o',
'--offset',
dest = 'offset',
type = 'int', nargs = 3, metavar = ' '.join(['int']*3),
help = 'a,b,c offset from old to new origin of grid [%default]')
parser.add_option('-f','--fill',
parser.add_option('-f',
'--fill',
dest = 'fill',
type = 'float', metavar = 'int',
help = 'background microstructure index, defaults to max microstructure index + 1')
type = 'float', metavar = 'float',
help = '(background) canvas grain index. "0" selects maximum microstructure index + 1 [%default]')
parser.set_defaults(offset = (0,0,0))
parser.set_defaults(grid = ['0','0','0'],
offset = (0,0,0),
)
(options, filenames) = parser.parse_args()
# --- loop over input files -------------------------------------------------------------------------
if filenames == []: filenames = [None]
for name in filenames:
damask.util.report(scriptName,name)
if name is None:
virt_file = StringIO(''.join(sys.stdin.read()))
geom = damask.Geom.from_file(virt_file)
else:
geom = damask.Geom.from_file(name)
geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name)
grid = geom.get_grid()
size = geom.get_size()
origin = geom.get_origin()
microstructure = geom.get_microstructure()
fill = np.nanmax(microstructure)+1 if options.fill is None else options.fill
dtype = float if np.isnan(fill) or int(fill) != fill or microstructure.dtype==np.float else int
if options.grid is not None:
for i,g in enumerate(options.grid):
grid[i] = int(round(grid[i]*float(g.lower().replace('x','')))) if g.lower().endswith('x') \
else int(options.grid[i])
# --- do work ------------------------------------------------------------------------------------
new_grid = np.array([int(o*float(n.lower().replace('x',''))) if n.lower().endswith('x') \
else int(n) for o,n in zip(grid,options.grid)],dtype=int)
new_grid = np.where(new_grid > 0, new_grid,grid)
new = np.full(grid,options.fill if options.fill is not None
else np.nanmax(geom.microstructure)+1,geom.microstructure.dtype)
microstructure_cropped = np.zeros(new_grid,dtype=dtype)
microstructure_cropped.fill(fill)
for x in range(geom.microstructure.shape[0]):
X = x + options.offset[0]
if not 0 <= X < new.shape[0]: continue
for y in range(geom.microstructure.shape[1]):
Y = y + options.offset[1]
if not 0 <= Y < new.shape[1]: continue
for z in range(geom.microstructure.shape[2]):
Z = z + options.offset[2]
if not 0 <= Z < new.shape[2]: continue
new[X,Y,Z] = geom.microstructure[x,y,z]
xindex = np.arange(max(options.offset[0],0),min(options.offset[0]+new_grid[0],grid[0]))
yindex = np.arange(max(options.offset[1],0),min(options.offset[1]+new_grid[1],grid[1]))
zindex = np.arange(max(options.offset[2],0),min(options.offset[2]+new_grid[2],grid[2]))
translate_x = [i - options.offset[0] for i in xindex]
translate_y = [i - options.offset[1] for i in yindex]
translate_z = [i - options.offset[2] for i in zindex]
if 0 in map(len,[xindex,yindex,zindex,translate_x,translate_y,translate_z]):
damask.util.croak('invaldid combination of grid and offset.')
continue
microstructure_cropped[min(translate_x):max(translate_x)+1,
min(translate_y):max(translate_y)+1,
min(translate_z):max(translate_z)+1] \
= microstructure[min(xindex):max(xindex)+1,
min(yindex):max(yindex)+1,
min(zindex):max(zindex)+1]
damask.util.croak(geom.update(new,rescale=True))
new_size = size/grid*new_grid if np.all(grid > 0) else new_grid
new_origin = origin + (size/grid if np.all(grid > 0) else new_size/new_grid) * options.offset
damask.util.croak(geom.update(microstructure=microstructure_cropped,
size=new_size,
origin=new_origin,
))
geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:]))
if name is None:

View File

@ -0,0 +1,74 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 no BOM -*-
import os
import sys
import numpy as np
import damask
from optparse import OptionParser
scriptName = os.path.splitext(os.path.basename(__file__))[0]
scriptID = ' '.join([scriptName,damask.version])
# --------------------------------------------------------------------
# MAIN
# --------------------------------------------------------------------
parser = OptionParser(option_class=damask.extendableOption, usage='%prog option(s) [geomfile(s)]', description = """
Generate homogeneous geometry.
""", version = scriptID)
parser.add_option('-g',
'--grid',
dest = 'grid',
type = 'string', nargs = 3, metavar = ' '.join(['string']*3),
help = 'a,b,c grid of hexahedral box %default')
parser.add_option('-s',
'--size',
dest = 'size',
type = 'float', nargs = 3, metavar = ' '.join(['float']*3),
help = 'x,y,z of geometry size %default')
parser.add_option('-o',
'--origin',
dest = 'origin',
type = 'float', nargs = 3, metavar = ' '.join(['float']*3),
help = 'x,y,z of geometry origin %default')
parser.add_option('--homogenization',
dest = 'homogenization',
type = 'int', metavar = 'int',
help = 'homogenization index [%default]')
parser.add_option('-f',
'--fill',
dest = 'fill',
type = 'float', metavar = 'float',
help = 'microstructure index [%default]')
parser.set_defaults(grid = [16,16,16],
size = [1.,1.,1.],
origin = [0.,0.,0.],
homogenization = 1,
fill = 1,
)
(options, filenames) = parser.parse_args()
dtype = float if np.isnan(options.fill) or int(options.fill) != options.fill else int
grid = list(map( int,options.grid))
size = list(map(float,options.size))
origin = list(map(float,options.origin))
homogenization = int(options.homogenization)
fill = float(options.fill)
damask.util.report(scriptName,'')
geom = damask.Geom(microstructure=(fill * np.ones(grid)).astype(dtype),
size=size,
origin=origin,
homogenization=homogenization,
comments=scriptID + ' ' + ' '.join(sys.argv[1:]))
damask.util.croak(geom)
sys.stdout.write(str(geom.show()))