standardized and cleaned

-write to file if given
-type casting is done in the geom class
This commit is contained in:
Martin Diehl 2019-05-30 10:20:07 +02:00
parent 07276d511c
commit 931fa9d36a
2 changed files with 28 additions and 32 deletions

View File

@ -1,37 +1,36 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 no BOM -*-
import os
import sys
from optparse import OptionParser
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 = """
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [geomfile(s)]', description = """
Generate homogeneous geometry.
""", version = scriptID)
parser.add_option('-g',
'--grid',
parser.add_option('-g','--grid',
dest = 'grid',
type = 'string', nargs = 3, metavar = ' '.join(['string']*3),
type = 'int', nargs = 3, metavar = ' '.join(['int']*3),
help = 'a,b,c grid of hexahedral box %default')
parser.add_option('-s',
'--size',
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',
parser.add_option('-o','--origin',
dest = 'origin',
type = 'float', nargs = 3, metavar = ' '.join(['float']*3),
help = 'x,y,z of geometry origin %default')
@ -39,36 +38,33 @@ parser.add_option('--homogenization',
dest = 'homogenization',
type = 'int', metavar = 'int',
help = 'homogenization index [%default]')
parser.add_option('-f',
'--fill',
parser.add_option('-f','--fill',
dest = 'fill',
type = 'float', metavar = 'float',
type = 'float', metavar = 'int',
help = 'microstructure index [%default]')
parser.set_defaults(grid = [16,16,16],
size = [1.,1.,1.],
origin = [0.,0.,0.],
parser.set_defaults(grid = (16,16,16),
size = (1.,1.,1.),
origin = (0.,0.,0.),
homogenization = 1,
fill = 1,
)
(options, filenames) = parser.parse_args()
(options, filename) = parser.parse_args()
name = None if filename == [] else filename[0]
damask.util.report(scriptName,name)
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,
geom = damask.Geom(microstructure=np.full(options.grid,options.fill,dtype=dtype),
size=options.size,
origin=options.origin,
homogenization=options.homogenization,
comments=scriptID + ' ' + ' '.join(sys.argv[1:]))
damask.util.croak(geom)
if name is None:
sys.stdout.write(str(geom.show()))
else:
geom.to_file(name)

View File

@ -192,7 +192,7 @@ class Geom():
"""Writes to file"""
header = self.get_header()
grid = self.get_grid()
format_string = '%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.microstructure)-1)))) if self.microstructure.dtype == int \
format_string = '%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.microstructure))))) if self.microstructure.dtype == int \
else '%g'
np.savetxt(fname,
self.microstructure.reshape([grid[0],np.prod(grid[1:])],order='F').T,