2013-02-25 22:42:13 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
|
|
|
import os,sys,string,math,numpy,time
|
|
|
|
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
|
|
|
|
|
2013-07-18 18:58:54 +05:30
|
|
|
scriptID = '$Id$'
|
|
|
|
scriptName = scriptID.split()[1]
|
|
|
|
|
2013-05-14 23:21:53 +05:30
|
|
|
#------------------------------------------------------------------------------------------------
|
2013-02-25 22:42:13 +05:30
|
|
|
class extendedOption(Option):
|
2013-05-14 23:21:53 +05:30
|
|
|
#------------------------------------------------------------------------------------------------
|
2013-02-25 22:42:13 +05:30
|
|
|
# used for definition of new option parser action 'extend', which enables to take multiple option arguments
|
|
|
|
# taken from online tutorial http://docs.python.org/library/optparse.html
|
|
|
|
|
|
|
|
ACTIONS = Option.ACTIONS + ("extend",)
|
|
|
|
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
|
|
|
|
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
|
|
|
|
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
|
|
|
|
|
|
|
|
def take_action(self, action, dest, opt, value, values, parser):
|
|
|
|
if action == "extend":
|
|
|
|
lvalue = value.split(",")
|
|
|
|
values.ensure_value(dest, []).extend(lvalue)
|
|
|
|
else:
|
|
|
|
Option.take_action(self, action, dest, opt, value, values, parser)
|
|
|
|
|
|
|
|
|
2013-05-14 23:21:53 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
2013-02-25 22:42:13 +05:30
|
|
|
parser = OptionParser(option_class=extendedOption, usage='%prog', description = """
|
|
|
|
Generate a geometry file of an osteon enclosing the Harvesian canal and separated by interstitial tissue.
|
|
|
|
The osteon phase is lamellar with a twisted plywood structure.
|
2013-02-27 16:26:30 +05:30
|
|
|
Its fiber orientation is oscillating by +/- amplitude within one period.
|
2013-07-18 18:58:54 +05:30
|
|
|
""" + string.replace(scriptID,'\n','\\n')
|
2013-02-25 22:42:13 +05:30
|
|
|
)
|
|
|
|
|
2013-04-12 18:18:39 +05:30
|
|
|
parser.add_option('-g', '--grid', dest='grid', type='int', nargs=2, \
|
|
|
|
help='a,b grid of hexahedral box %default')
|
|
|
|
parser.add_option('-s', '--size', dest='size', type='float', nargs=2, \
|
|
|
|
help='x,y size of hexahedral box %default')
|
2013-02-25 22:42:13 +05:30
|
|
|
parser.add_option('-c', '--canal', dest='canal', type='float', \
|
2013-04-12 18:18:39 +05:30
|
|
|
help='Haversian canal radius [%default]')
|
2013-02-25 22:42:13 +05:30
|
|
|
parser.add_option('-o', '--osteon', dest='osteon', type='float', \
|
2013-04-12 18:18:39 +05:30
|
|
|
help='osteon radius (horizontal) [%default]')
|
2013-02-28 00:13:53 +05:30
|
|
|
parser.add_option('-l', '--lamella', dest='period', type='float', \
|
2013-04-12 18:18:39 +05:30
|
|
|
help='lamella width [%default]')
|
2013-02-28 00:13:53 +05:30
|
|
|
parser.add_option('-a', '--amplitude', dest='amplitude', type='float', \
|
2013-04-12 18:18:39 +05:30
|
|
|
help='amplitude of twisted plywood wiggle in deg [%default]')
|
2013-02-28 00:13:53 +05:30
|
|
|
parser.add_option( '--aspect', dest='aspect', type='float', \
|
2013-04-12 18:18:39 +05:30
|
|
|
help='osteon aspect ratio (vert/horiz) [%default]')
|
2013-02-27 16:26:30 +05:30
|
|
|
parser.add_option('-w', '--omega', dest='omega', type='float', \
|
2013-04-12 18:18:39 +05:30
|
|
|
help='rotation angle (around normal) of osteon [%default]')
|
2013-02-25 22:42:13 +05:30
|
|
|
parser.add_option('--homogenization', dest='homogenization', type='int', \
|
2013-04-12 18:18:39 +05:30
|
|
|
help='homogenization index to be used [%default]')
|
2013-02-27 16:26:30 +05:30
|
|
|
parser.add_option('--crystallite', dest='crystallite', type='int', \
|
2013-04-12 18:18:39 +05:30
|
|
|
help='crystallite index to be used [%default]')
|
2013-02-25 22:42:13 +05:30
|
|
|
parser.add_option('--configuration', dest='config', action='store_true', \
|
2013-04-12 18:18:39 +05:30
|
|
|
help='output material configuration [%default]')
|
2013-02-25 22:42:13 +05:30
|
|
|
parser.add_option('-2', '--twodimensional', dest='twoD', action='store_true', \
|
2013-04-12 18:18:39 +05:30
|
|
|
help='use two-dimensional geom data arrangement [%default]')
|
2013-02-25 22:42:13 +05:30
|
|
|
|
|
|
|
parser.set_defaults(canal = 25e-6)
|
2013-02-28 00:13:53 +05:30
|
|
|
parser.set_defaults(osteon = 100e-6)
|
2013-02-25 22:42:13 +05:30
|
|
|
parser.set_defaults(aspect = 1.0)
|
2013-02-27 16:26:30 +05:30
|
|
|
parser.set_defaults(omega = 0.0)
|
2013-02-25 22:42:13 +05:30
|
|
|
parser.set_defaults(period = 5e-6)
|
|
|
|
parser.set_defaults(amplitude = 60)
|
2013-04-12 18:18:39 +05:30
|
|
|
parser.set_defaults(size = numpy.array([300e-6,300e-6],'d'))
|
|
|
|
parser.set_defaults(grid = numpy.array([512,512],'i'))
|
2013-02-25 22:42:13 +05:30
|
|
|
parser.set_defaults(homogenization = 1)
|
|
|
|
parser.set_defaults(crystallite = 1)
|
|
|
|
parser.set_defaults(config = False)
|
|
|
|
parser.set_defaults(twoD = False)
|
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2013-05-14 23:21:53 +05:30
|
|
|
#--- setup file handles ---------------------------------------------------------------------------
|
2013-02-25 22:42:13 +05:30
|
|
|
file = {'name':'STDIN',
|
|
|
|
'input':sys.stdin,
|
|
|
|
'output':sys.stdout,
|
|
|
|
'croak':sys.stderr,
|
|
|
|
}
|
|
|
|
|
2013-04-12 18:18:39 +05:30
|
|
|
if numpy.any(options.grid < 2):
|
2013-05-15 02:39:37 +05:30
|
|
|
file['croak'].write('grid too small...\n')
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
if numpy.any(options.size <= 0.0):
|
|
|
|
file['croak'].write('size too small...\n')
|
2013-02-25 22:42:13 +05:30
|
|
|
sys.exit()
|
|
|
|
|
2013-05-14 23:21:53 +05:30
|
|
|
options.omega *= math.pi/180.0 # rescale ro radians
|
2013-02-28 00:13:53 +05:30
|
|
|
rotation = numpy.array([[ math.cos(options.omega),math.sin(options.omega),],
|
|
|
|
[-math.sin(options.omega),math.cos(options.omega),]],'d')
|
2013-02-25 22:42:13 +05:30
|
|
|
|
2013-02-28 00:13:53 +05:30
|
|
|
box = numpy.dot(numpy.array([[options.canal,0.],[0.,options.aspect*options.canal]]).transpose(),rotation)
|
2013-02-25 22:42:13 +05:30
|
|
|
|
2013-04-12 18:18:39 +05:30
|
|
|
|
|
|
|
info = {
|
|
|
|
'grid': numpy.ones(3,'i'),
|
|
|
|
'size': numpy.ones(3,'d'),
|
|
|
|
'origin': numpy.zeros(3,'d'),
|
2013-05-14 23:21:53 +05:30
|
|
|
'microstructures': 3,
|
2013-04-12 18:18:39 +05:30
|
|
|
'homogenization': options.homogenization,
|
2013-02-25 22:42:13 +05:30
|
|
|
}
|
|
|
|
|
2013-04-12 18:18:39 +05:30
|
|
|
info['grid'][:2] = options.grid
|
|
|
|
info['size'][:2] = options.size
|
|
|
|
info['size'][2] = min(info['size'][0]/info['grid'][0],info['size'][1]/info['grid'][1])
|
|
|
|
info['origin'] = -info['size']/2.0
|
2013-02-27 16:26:30 +05:30
|
|
|
|
2013-04-12 18:18:39 +05:30
|
|
|
X0 = info['size'][0]/info['grid'][0]*\
|
|
|
|
(numpy.tile(numpy.arange(info['grid'][0]),(info['grid'][1],1)) - info['grid'][0]/2 + 0.5)
|
|
|
|
Y0 = info['size'][1]/info['grid'][1]*\
|
|
|
|
(numpy.tile(numpy.arange(info['grid'][1]),(info['grid'][0],1)).transpose() - info['grid'][1]/2 + 0.5)
|
2013-02-25 22:42:13 +05:30
|
|
|
|
2013-05-14 23:21:53 +05:30
|
|
|
X = X0*rotation[0,0] + Y0*rotation[0,1] # rotate by omega
|
|
|
|
Y = X0*rotation[1,0] + Y0*rotation[1,1] # rotate by omega
|
2013-02-25 22:42:13 +05:30
|
|
|
|
2013-02-28 00:13:53 +05:30
|
|
|
radius = numpy.sqrt(X*X + Y*Y/options.aspect/options.aspect)
|
|
|
|
alpha = numpy.degrees(numpy.arctan2(Y/options.aspect,X))
|
|
|
|
beta = options.amplitude*numpy.sin(2.0*math.pi*(radius-options.canal)/options.period)
|
2013-02-25 22:42:13 +05:30
|
|
|
|
|
|
|
microstructure = numpy.where(radius < float(options.canal),1,0) + numpy.where(radius > float(options.osteon),2,0)
|
|
|
|
|
2013-04-12 18:18:39 +05:30
|
|
|
alphaOfGrain = numpy.zeros(info['grid'][0]*info['grid'][1],'d')
|
|
|
|
betaOfGrain = numpy.zeros(info['grid'][0]*info['grid'][1],'d')
|
|
|
|
for y in xrange(info['grid'][1]):
|
|
|
|
for x in xrange(info['grid'][0]):
|
2013-02-28 00:13:53 +05:30
|
|
|
if microstructure[y,x] == 0:
|
2013-04-12 18:18:39 +05:30
|
|
|
microstructure[y,x] = info['microstructures']
|
|
|
|
alphaOfGrain[info['microstructures']] = alpha[y,x]
|
|
|
|
betaOfGrain[ info['microstructures']] = beta[y,x]
|
|
|
|
info['microstructures'] += 1
|
2013-07-18 18:58:54 +05:30
|
|
|
#--- report ---------------------------------------------------------------------------------------
|
|
|
|
else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
|
2013-04-12 18:18:39 +05:30
|
|
|
file['croak'].write('grid a b c: %s\n'%(' x '.join(map(str,info['grid']))) + \
|
|
|
|
'size x y z: %s\n'%(' x '.join(map(str,info['size']))) + \
|
|
|
|
'origin x y z: %s\n'%(' : '.join(map(str,info['origin']))) + \
|
|
|
|
'microstructures: %i\n'%info['microstructures'] + \
|
|
|
|
'homogenization: %i\n'%info['homogenization'])
|
|
|
|
file['croak'].write("bounding box: %s\n"%(numpy.sqrt(numpy.sum(box*box,0))))
|
2013-02-25 22:42:13 +05:30
|
|
|
|
2013-05-15 02:39:37 +05:30
|
|
|
if numpy.any(info['grid'] < 1):
|
2013-05-15 21:32:38 +05:30
|
|
|
file['croak'].write('invalid grid a b c.\n')
|
2013-05-15 02:39:37 +05:30
|
|
|
sys.exit()
|
|
|
|
if numpy.any(info['size'] <= 0.0):
|
2013-05-15 21:32:38 +05:30
|
|
|
file['croak'].write('invalid size x y z.\n')
|
2013-05-15 02:39:37 +05:30
|
|
|
sys.exit()
|
|
|
|
|
2013-02-25 22:42:13 +05:30
|
|
|
# -------------------------------------- switch according to task ----------------------------------
|
2013-04-12 18:18:39 +05:30
|
|
|
formatwidth = 1+int(math.floor(math.log10(info['microstructures']-1)))
|
2013-02-25 22:42:13 +05:30
|
|
|
if options.config:
|
|
|
|
file['output'].write('<microstructure>\n')
|
|
|
|
file['output'].write('\n[canal]\n' + \
|
|
|
|
'crystallite %i\n'%options.crystallite + \
|
|
|
|
'(constituent)\tphase 1\ttexture 1\tfraction 1.0\n')
|
|
|
|
file['output'].write('\n[interstitial]\n' + \
|
|
|
|
'crystallite %i\n'%options.crystallite + \
|
|
|
|
'(constituent)\tphase 2\ttexture 2\tfraction 1.0\n')
|
2013-04-18 19:23:13 +05:30
|
|
|
for i in xrange(3,info['microstructures']):
|
2013-02-27 16:26:30 +05:30
|
|
|
file['output'].write('\n[Grain%s]\n'%(str(i).zfill(formatwidth)) + \
|
2013-02-25 22:42:13 +05:30
|
|
|
'crystallite %i\n'%options.crystallite + \
|
2013-02-27 16:26:30 +05:30
|
|
|
'(constituent)\tphase 3\ttexture %s\tfraction 1.0\n'%(str(i).rjust(formatwidth)))
|
2013-02-25 22:42:13 +05:30
|
|
|
|
|
|
|
file['output'].write('\n<texture>\n')
|
|
|
|
file['output'].write('\n[canal]\n')
|
|
|
|
file['output'].write('\n[interstitial]\n')
|
2013-04-18 19:23:13 +05:30
|
|
|
for i in xrange(3,info['microstructures']):
|
2013-02-27 16:26:30 +05:30
|
|
|
file['output'].write('\n[Grain%s]\n'%(str(i).zfill(formatwidth)) + \
|
2013-02-25 22:42:13 +05:30
|
|
|
'(gauss)\tphi1 %g\tPhi %g\tphi2 0\tscatter 0.0\tfraction 1.0\n'%(\
|
|
|
|
alphaOfGrain[i],\
|
|
|
|
betaOfGrain[i]))
|
|
|
|
|
|
|
|
else:
|
2013-07-18 18:58:54 +05:30
|
|
|
header = [scriptID+'\n']
|
2013-05-14 23:21:53 +05:30
|
|
|
header.append("grid\ta %i\tb %i\tc %i\n"%(info['grid'][0],info['grid'][1],info['grid'][2],))
|
|
|
|
header.append("size\tx %f\ty %f\tz %f\n"%(info['size'][0],info['size'][1],info['size'][2],))
|
|
|
|
header.append("origin\tx %f\ty %f\tz %f\n"%(info['origin'][0],info['origin'][1],info['origin'][2],))
|
|
|
|
header.append("microstructures\t%i\n"%info['microstructures'])
|
|
|
|
header.append("homogenization\t%i\n"%info['homogenization'])
|
|
|
|
file['output'].write('%i\theader\n'%(len(header))+''.join(header))
|
2013-02-25 22:42:13 +05:30
|
|
|
|
2013-04-12 18:18:39 +05:30
|
|
|
for y in xrange(info['grid'][1]):
|
|
|
|
for x in xrange(info['grid'][0]):
|
2013-02-25 22:42:13 +05:30
|
|
|
file['output'].write(\
|
2013-02-28 00:13:53 +05:30
|
|
|
str(microstructure[y,x]).rjust(formatwidth) + \
|
2013-02-25 22:42:13 +05:30
|
|
|
{True:' ',False:'\n'}[options.twoD] )
|
|
|
|
file['output'].write({True:'\n',False:''}[options.twoD])
|
|
|
|
|
|
|
|
|
2013-05-14 23:21:53 +05:30
|
|
|
#--- output finalization --------------------------------------------------------------------------
|
2013-02-25 22:42:13 +05:30
|
|
|
file['output'].close()
|