2018-11-17 12:42:12 +05:30
|
|
|
#!/usr/bin/env python3
|
2014-08-29 00:20:48 +05:30
|
|
|
|
2019-05-25 14:15:58 +05:30
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from io import StringIO
|
|
|
|
from optparse import OptionParser
|
2014-08-29 00:20:48 +05:30
|
|
|
|
2019-05-27 00:06:41 +05:30
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
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-08-29 00:20:48 +05:30
|
|
|
|
2019-05-27 00:06:41 +05:30
|
|
|
|
2014-08-29 00:20:48 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
|
2019-05-27 00:06:41 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [geomfile(s)]', description = """
|
|
|
|
Inserts a primitive geometric object at a given position.
|
2014-08-29 00:20:48 +05:30
|
|
|
Depending on the sign of the dimension parameters, these objects can be boxes, cylinders, or ellipsoids.
|
2014-11-07 16:43:14 +05:30
|
|
|
|
|
|
|
""", version = scriptID)
|
2014-08-29 00:20:48 +05:30
|
|
|
|
2019-05-27 00:06:41 +05:30
|
|
|
parser.add_option('-c', '--center',
|
|
|
|
dest='center',
|
2019-03-10 00:48:28 +05:30
|
|
|
type='float', nargs = 3, metavar=' '.join(['float']*3),
|
2014-08-29 00:20:48 +05:30
|
|
|
help='a,b,c origin of primitive %default')
|
2019-05-27 00:06:41 +05:30
|
|
|
parser.add_option('-d', '--dimension',
|
|
|
|
dest='dimension',
|
2019-03-10 00:48:28 +05:30
|
|
|
type='float', nargs = 3, metavar=' '.join(['float']*3),
|
2019-05-27 00:06:41 +05:30
|
|
|
help='a,b,c extension of hexahedral box')
|
|
|
|
parser.add_option('-e', '--exponent',
|
|
|
|
dest='exponent',
|
2019-03-10 00:48:28 +05:30
|
|
|
type='float', nargs = 3, metavar=' '.join(['float']*3),
|
2017-08-03 21:38:51 +05:30
|
|
|
help='i,j,k exponents for axes - 0 gives octahedron (|x|^(2^0) + |y|^(2^0) + |z|^(2^0) < 1), \
|
|
|
|
1 gives a sphere (|x|^(2^1) + |y|^(2^1) + |z|^(2^1) < 1), \
|
2019-05-27 00:06:41 +05:30
|
|
|
large values produce boxes, negative turn concave.')
|
|
|
|
parser.add_option('-f', '--fill',
|
|
|
|
dest='fill',
|
|
|
|
type='float', metavar = 'int',
|
|
|
|
help='microstructure index to fill primitive, defaults to max microstructure index + 1')
|
|
|
|
parser.add_option('-q', '--quaternion',
|
|
|
|
dest='quaternion',
|
2019-03-10 00:48:28 +05:30
|
|
|
type='float', nargs = 4, metavar=' '.join(['float']*4),
|
2014-08-29 00:20:48 +05:30
|
|
|
help = 'rotation of primitive as quaternion')
|
2019-05-27 00:06:41 +05:30
|
|
|
parser.add_option('-a', '--angleaxis',
|
|
|
|
dest='angleaxis',
|
|
|
|
type=float, nargs = 4, metavar=' '.join(['float']*4),
|
2019-03-10 00:48:28 +05:30
|
|
|
help = 'axis and angle to rotate primitive')
|
2019-05-27 00:06:41 +05:30
|
|
|
parser.add_option( '--degrees',
|
|
|
|
dest='degrees',
|
2019-03-10 00:48:28 +05:30
|
|
|
action='store_true',
|
2019-05-27 00:06:41 +05:30
|
|
|
help = 'angle is given in degrees')
|
|
|
|
parser.add_option( '--nonperiodic',
|
|
|
|
dest='periodic',
|
2019-03-10 00:48:28 +05:30
|
|
|
action='store_false',
|
2019-05-27 00:06:41 +05:30
|
|
|
help = 'wrap around edges')
|
|
|
|
parser.add_option( '--realspace',
|
|
|
|
dest='realspace',
|
2019-03-10 00:48:28 +05:30
|
|
|
action='store_true',
|
2017-08-03 21:38:51 +05:30
|
|
|
help = '-c and -d span [origin,origin+size] instead of [0,grid] coordinates')
|
2019-05-27 00:06:41 +05:30
|
|
|
parser.add_option( '--invert',
|
|
|
|
dest='inside',
|
2019-05-23 21:33:54 +05:30
|
|
|
action='store_false',
|
|
|
|
help = 'invert the volume filled by the primitive (inside/outside)')
|
2019-05-27 00:06:41 +05:30
|
|
|
|
2017-03-08 23:16:53 +05:30
|
|
|
parser.set_defaults(center = (.0,.0,.0),
|
2014-08-29 00:20:48 +05:30
|
|
|
degrees = False,
|
2017-08-03 21:42:47 +05:30
|
|
|
exponent = (20,20,20), # box shape by default
|
2017-03-08 07:07:30 +05:30
|
|
|
periodic = True,
|
2017-08-03 21:38:51 +05:30
|
|
|
realspace = False,
|
2019-05-23 21:33:54 +05:30
|
|
|
inside = True,
|
2014-08-29 00:20:48 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
2019-05-23 21:33:54 +05:30
|
|
|
|
2017-03-08 23:16:53 +05:30
|
|
|
if options.dimension is None:
|
2019-05-27 00:06:41 +05:30
|
|
|
parser.error('no dimension specified.')
|
|
|
|
if [options.angleaxis,options.quaternion].count(None) == 2:
|
|
|
|
parser.error('more than one rotation specified.')
|
|
|
|
|
2017-03-08 23:16:53 +05:30
|
|
|
if options.angleaxis is not None:
|
2019-03-10 12:46:01 +05:30
|
|
|
rotation = damask.Rotation.fromAxisAngle(np.array(options.angleaxis),options.degrees,normalise=True)
|
2017-03-08 23:16:53 +05:30
|
|
|
elif options.quaternion is not None:
|
2019-02-12 12:25:54 +05:30
|
|
|
rotation = damask.Rotation.fromQuaternion(options.quaternion)
|
2014-08-29 00:20:48 +05:30
|
|
|
else:
|
2019-02-12 04:41:22 +05:30
|
|
|
rotation = damask.Rotation()
|
2014-08-29 00:20:48 +05:30
|
|
|
|
2019-05-27 00:06:41 +05:30
|
|
|
options.center = np.array(options.center)
|
2017-03-08 07:07:30 +05:30
|
|
|
options.dimension = np.array(options.dimension)
|
2017-08-03 21:42:47 +05:30
|
|
|
# undo logarithmic sense of exponent and generate ellipsoids for negative dimensions (backward compatibility)
|
2019-05-27 00:06:41 +05:30
|
|
|
options.exponent = np.where(np.array(options.dimension) > 0, np.power(2,options.exponent), 2)
|
2015-08-18 10:02:19 +05:30
|
|
|
|
|
|
|
|
2019-05-25 14:15:58 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2014-08-29 00:20:48 +05:30
|
|
|
|
2019-05-25 14:15:58 +05:30
|
|
|
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)
|
2019-05-27 00:06:41 +05:30
|
|
|
damask.util.croak(geom)
|
|
|
|
microstructure = geom.get_microstructure()
|
2014-08-29 00:20:48 +05:30
|
|
|
|
2019-05-27 00:06:41 +05:30
|
|
|
fill = options.fill if options.fill is not None else np.nanmax(microstructure)+1
|
Improve performance and generalize primitive shape
- Behavior is mostly unchanged, but the primitive may be shifted by a voxel when compared to the previous version, which had rounding issues near the edge of the primitive.
- exponent flag specifies the exponents that satisfy the equation x^e1 + y^e2 + z^e3 < 1. (1,1,1) gives an octahedron, (2,2,2) a sphere, and large values (1e10, 1e10, 1e10) gives a hexahedral box for any reasonable resolution. Mixing the two can produce a cylinder, (1e10, 2, 2) gives one with rotational symmetry about the x-axis.
2017-02-24 11:03:47 +05:30
|
|
|
|
2019-05-25 14:15:58 +05:30
|
|
|
origin = np.zeros(3)
|
|
|
|
for i,line in enumerate(geom.comments):
|
|
|
|
if line.lower().strip().startswith('origin'):
|
|
|
|
origin= np.array([float(line.split()[j]) for j in [2,4,6]]) # assume correct order (x,y,z)
|
Improve performance and generalize primitive shape
- Behavior is mostly unchanged, but the primitive may be shifted by a voxel when compared to the previous version, which had rounding issues near the edge of the primitive.
- exponent flag specifies the exponents that satisfy the equation x^e1 + y^e2 + z^e3 < 1. (1,1,1) gives an octahedron, (2,2,2) a sphere, and large values (1e10, 1e10, 1e10) gives a hexahedral box for any reasonable resolution. Mixing the two can produce a cylinder, (1e10, 2, 2) gives one with rotational symmetry about the x-axis.
2017-02-24 11:03:47 +05:30
|
|
|
|
2017-03-08 07:07:30 +05:30
|
|
|
# coordinates given in real space (default) vs voxel space
|
2017-08-03 21:38:51 +05:30
|
|
|
if options.realspace:
|
2019-05-25 14:15:58 +05:30
|
|
|
options.center -= origin
|
|
|
|
options.center *= geom.get_grid() / geom.get_size()
|
|
|
|
options.dimension *= geom.get_grid() / geom.get_size()
|
2017-03-08 07:07:30 +05:30
|
|
|
|
2017-08-03 21:38:51 +05:30
|
|
|
grid = microstructure.shape
|
2017-03-05 22:20:17 +05:30
|
|
|
|
|
|
|
# change to coordinate space where the primitive is the unit sphere/cube/etc
|
Improve performance and generalize primitive shape
- Behavior is mostly unchanged, but the primitive may be shifted by a voxel when compared to the previous version, which had rounding issues near the edge of the primitive.
- exponent flag specifies the exponents that satisfy the equation x^e1 + y^e2 + z^e3 < 1. (1,1,1) gives an octahedron, (2,2,2) a sphere, and large values (1e10, 1e10, 1e10) gives a hexahedral box for any reasonable resolution. Mixing the two can produce a cylinder, (1e10, 2, 2) gives one with rotational symmetry about the x-axis.
2017-02-24 11:03:47 +05:30
|
|
|
if options.periodic: # use padding to achieve periodicity
|
2017-08-03 21:38:51 +05:30
|
|
|
(X, Y, Z) = np.meshgrid(np.arange(-grid[0]/2, (3*grid[0])/2, dtype=np.float32), # 50% padding on each side
|
|
|
|
np.arange(-grid[1]/2, (3*grid[1])/2, dtype=np.float32),
|
|
|
|
np.arange(-grid[2]/2, (3*grid[2])/2, dtype=np.float32),
|
2017-02-25 09:00:04 +05:30
|
|
|
indexing='ij')
|
|
|
|
# Padding handling
|
|
|
|
X = np.roll(np.roll(np.roll(X,
|
2018-11-16 13:32:31 +05:30
|
|
|
-grid[0]//2, axis=0),
|
|
|
|
-grid[1]//2, axis=1),
|
|
|
|
-grid[2]//2, axis=2)
|
2017-02-25 09:00:04 +05:30
|
|
|
Y = np.roll(np.roll(np.roll(Y,
|
2018-11-16 13:32:31 +05:30
|
|
|
-grid[0]//2, axis=0),
|
|
|
|
-grid[1]//2, axis=1),
|
|
|
|
-grid[2]//2, axis=2)
|
2017-02-25 09:00:04 +05:30
|
|
|
Z = np.roll(np.roll(np.roll(Z,
|
2018-11-16 13:32:31 +05:30
|
|
|
-grid[0]//2, axis=0),
|
|
|
|
-grid[1]//2, axis=1),
|
|
|
|
-grid[2]//2, axis=2)
|
2017-03-05 22:20:17 +05:30
|
|
|
else: # nonperiodic, much lighter on resources
|
|
|
|
# change to coordinate space where the primitive is the unit sphere/cube/etc
|
2017-08-03 21:38:51 +05:30
|
|
|
(X, Y, Z) = np.meshgrid(np.arange(0, grid[0], dtype=np.float32),
|
|
|
|
np.arange(0, grid[1], dtype=np.float32),
|
|
|
|
np.arange(0, grid[2], dtype=np.float32),
|
2017-03-05 22:20:17 +05:30
|
|
|
indexing='ij')
|
2017-02-25 09:00:04 +05:30
|
|
|
|
2017-03-05 22:20:17 +05:30
|
|
|
# first by translating the center onto 0, 0.5 shifts the voxel origin onto the center of the voxel
|
|
|
|
X -= options.center[0] - 0.5
|
|
|
|
Y -= options.center[1] - 0.5
|
|
|
|
Z -= options.center[2] - 0.5
|
2019-02-12 12:12:46 +05:30
|
|
|
# and then by applying the rotation
|
2017-03-05 22:20:17 +05:30
|
|
|
(X, Y, Z) = rotation * (X, Y, Z)
|
|
|
|
# and finally by scaling (we don't worry about options.dimension being negative, np.abs occurs on the microstructure = np.where... line)
|
|
|
|
X /= options.dimension[0] * 0.5
|
|
|
|
Y /= options.dimension[1] * 0.5
|
|
|
|
Z /= options.dimension[2] * 0.5
|
Improve performance and generalize primitive shape
- Behavior is mostly unchanged, but the primitive may be shifted by a voxel when compared to the previous version, which had rounding issues near the edge of the primitive.
- exponent flag specifies the exponents that satisfy the equation x^e1 + y^e2 + z^e3 < 1. (1,1,1) gives an octahedron, (2,2,2) a sphere, and large values (1e10, 1e10, 1e10) gives a hexahedral box for any reasonable resolution. Mixing the two can produce a cylinder, (1e10, 2, 2) gives one with rotational symmetry about the x-axis.
2017-02-24 11:03:47 +05:30
|
|
|
|
2017-03-05 22:20:17 +05:30
|
|
|
|
|
|
|
# High exponents can cause underflow & overflow - loss of precision is okay here, we just compare it to 1, so +infinity and 0 are fine
|
|
|
|
old_settings = np.seterr()
|
|
|
|
np.seterr(over='ignore', under='ignore')
|
|
|
|
|
|
|
|
if options.periodic: # use padding to achieve periodicity
|
2017-08-03 21:38:51 +05:30
|
|
|
inside = np.zeros(grid, dtype=bool)
|
2017-02-25 09:00:04 +05:30
|
|
|
for i in range(2):
|
|
|
|
for j in range(2):
|
|
|
|
for k in range(2):
|
Improve performance and generalize primitive shape
- Behavior is mostly unchanged, but the primitive may be shifted by a voxel when compared to the previous version, which had rounding issues near the edge of the primitive.
- exponent flag specifies the exponents that satisfy the equation x^e1 + y^e2 + z^e3 < 1. (1,1,1) gives an octahedron, (2,2,2) a sphere, and large values (1e10, 1e10, 1e10) gives a hexahedral box for any reasonable resolution. Mixing the two can produce a cylinder, (1e10, 2, 2) gives one with rotational symmetry about the x-axis.
2017-02-24 11:03:47 +05:30
|
|
|
inside = inside | ( # Most of this is handling the padding
|
2017-08-03 21:38:51 +05:30
|
|
|
np.abs(X[grid[0] * i : grid[0] * (i+1),
|
|
|
|
grid[1] * j : grid[1] * (j+1),
|
|
|
|
grid[2] * k : grid[2] * (k+1)])**options.exponent[0] +
|
|
|
|
np.abs(Y[grid[0] * i : grid[0] * (i+1),
|
|
|
|
grid[1] * j : grid[1] * (j+1),
|
|
|
|
grid[2] * k : grid[2] * (k+1)])**options.exponent[1] +
|
|
|
|
np.abs(Z[grid[0] * i : grid[0] * (i+1),
|
|
|
|
grid[1] * j : grid[1] * (j+1),
|
|
|
|
grid[2] * k : grid[2] * (k+1)])**options.exponent[2] <= 1.0)
|
Improve performance and generalize primitive shape
- Behavior is mostly unchanged, but the primitive may be shifted by a voxel when compared to the previous version, which had rounding issues near the edge of the primitive.
- exponent flag specifies the exponents that satisfy the equation x^e1 + y^e2 + z^e3 < 1. (1,1,1) gives an octahedron, (2,2,2) a sphere, and large values (1e10, 1e10, 1e10) gives a hexahedral box for any reasonable resolution. Mixing the two can produce a cylinder, (1e10, 2, 2) gives one with rotational symmetry about the x-axis.
2017-02-24 11:03:47 +05:30
|
|
|
|
2019-05-23 21:33:54 +05:30
|
|
|
microstructure = np.where(inside,
|
2019-05-27 00:06:41 +05:30
|
|
|
fill if options.inside else microstructure,
|
|
|
|
microstructure if options.inside else fill)
|
2017-03-05 22:20:17 +05:30
|
|
|
|
Improve performance and generalize primitive shape
- Behavior is mostly unchanged, but the primitive may be shifted by a voxel when compared to the previous version, which had rounding issues near the edge of the primitive.
- exponent flag specifies the exponents that satisfy the equation x^e1 + y^e2 + z^e3 < 1. (1,1,1) gives an octahedron, (2,2,2) a sphere, and large values (1e10, 1e10, 1e10) gives a hexahedral box for any reasonable resolution. Mixing the two can produce a cylinder, (1e10, 2, 2) gives one with rotational symmetry about the x-axis.
2017-02-24 11:03:47 +05:30
|
|
|
else: # nonperiodic, much lighter on resources
|
|
|
|
microstructure = np.where(np.abs(X)**options.exponent[0] +
|
|
|
|
np.abs(Y)**options.exponent[1] +
|
2019-05-23 21:33:54 +05:30
|
|
|
np.abs(Z)**options.exponent[2] <= 1.0,
|
2019-05-27 00:06:41 +05:30
|
|
|
fill if options.inside else microstructure,
|
|
|
|
microstructure if options.inside else fill)
|
2014-08-29 00:20:48 +05:30
|
|
|
|
2019-05-27 00:06:41 +05:30
|
|
|
damask.util.croak(geom.update(microstructure))
|
2019-05-25 14:15:58 +05:30
|
|
|
geom.add_comment(scriptID + ' ' + ' '.join(sys.argv[1:]))
|
|
|
|
|
|
|
|
if name is None:
|
|
|
|
sys.stdout.write(str(geom.show()))
|
|
|
|
else:
|
|
|
|
geom.to_file(name)
|