using class

needs more memory, but should be faster and is better readable
This commit is contained in:
Martin Diehl 2019-05-26 21:30:38 +02:00
parent 7a500e77b1
commit af493cf9fd
3 changed files with 54 additions and 84 deletions

View File

@ -52,7 +52,7 @@ def gradFFT(geomdim,field):
# MAIN # MAIN
# -------------------------------------------------------------------- # --------------------------------------------------------------------
parser = OptionParser(option_class=damask.extendableOption, usage='%prog option [ASCIItable(s)]', description = """ parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """
Add column(s) containing gradient of requested column(s). Add column(s) containing gradient of requested column(s).
Operates on periodic ordered three-dimensional data sets Operates on periodic ordered three-dimensional data sets
of vector and scalar fields. of vector and scalar fields.

View File

@ -1,80 +1,47 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: UTF-8 no BOM -*-
import os,sys import os
import numpy as np import sys
from optparse import OptionParser from optparse import OptionParser
import damask import damask
scriptName = os.path.splitext(os.path.basename(__file__))[0] scriptName = os.path.splitext(os.path.basename(__file__))[0]
scriptID = ' '.join([scriptName,damask.version]) scriptID = ' '.join([scriptName,damask.version])
#-------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
# MAIN # MAIN
#-------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
parser = OptionParser(option_class=damask.extendableOption, usage='%prog [geomfile(s)]', description = """ parser = OptionParser(option_class=damask.extendableOption, usage='%prog [geomfile(s)]', description = """
compress geometry files with ranges "a to b" and/or multiples "n of x". Pack ranges to "a to b" and/or multiples to "n of x".
""", version = scriptID) """, version = scriptID)
(options, filenames) = parser.parse_args() (options, filenames) = parser.parse_args()
# --- loop over input files -------------------------------------------------------------------------
if filenames == []: filenames = [None] if filenames == []: filenames = [None]
for name in filenames: for name in filenames:
try:
table = damask.ASCIItable(name = name,
buffered = False, labeled = False)
except: continue
damask.util.report(scriptName,name) damask.util.report(scriptName,name)
# --- interpret header ---------------------------------------------------------------------------- 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)
damask.util.croak(geom)
microstructure = geom.get_microstructure().flatten('F')
table.head_read()
info,extra_header = table.head_getGeom()
damask.util.report_geom(info)
errors = [] compressType = None
if np.any(info['grid'] < 1): errors.append('invalid grid a b c.')
if np.any(info['size'] <= 0.0): errors.append('invalid size x y z.')
if errors != []:
damask.util.croak(errors)
table.close(dismiss = True)
continue
# --- write header ---------------------------------------------------------------------------------
table.labels_clear()
table.info_clear()
table.info_append(extra_header+[
scriptID + ' ' + ' '.join(sys.argv[1:]),
"grid\ta {grid[0]}\tb {grid[1]}\tc {grid[2]}".format(grid=info['grid']),
"size\tx {size[0]}\ty {size[1]}\tz {size[2]}".format(size=info['size']),
"origin\tx {origin[0]}\ty {origin[1]}\tz {origin[2]}".format(origin=info['origin']),
"homogenization\t{homog}".format(homog=info['homogenization']),
"microstructures\t{microstructures}".format(microstructures=info['microstructures']),
])
table.head_write()
# --- write packed microstructure information -----------------------------------------------------
compressType = ''
former = start = -1 former = start = -1
reps = 0 reps = 0
outputAlive = True for current in microstructure:
while outputAlive and table.data_read(): # read next data line of ASCII table
items = table.data
if len(items) > 2:
if items[1].lower() == 'of': items = [int(items[2])]*int(items[0])
elif items[1].lower() == 'to': items = range(int(items[0]),1+int(items[2]))
else: items = map(int,items)
else: items = map(int,items)
for current in items:
if abs(current - former) == 1 and (start - current) == reps*(former - current): if abs(current - former) == 1 and (start - current) == reps*(former - current):
compressType = 'to' compressType = 'to'
reps += 1 reps += 1
@ -82,16 +49,14 @@ for name in filenames:
compressType = 'of' compressType = 'of'
reps += 1 reps += 1
else: else:
if compressType == '': if compressType == None:
table.data = [] out = []
elif compressType == '.': elif compressType == '.':
table.data = [former] out.append('{}\n'.format(former))
elif compressType == 'to': elif compressType == 'to':
table.data = [start,'to',former] out.append('{} to {}\n'.format(start,former))
elif compressType == 'of': elif compressType == 'of':
table.data = [reps,'of',former] out.append('{} of {}\n'.format(reps,former))
outputAlive = table.data_write(delimiter = ' ') # output processed line
compressType = '.' compressType = '.'
start = current start = current
@ -99,14 +64,19 @@ for name in filenames:
former = current former = current
table.data = { if compressType == '.':
'.' : [former], out.append('{}\n'.format(former))
'to': [start,'to',former], elif compressType == 'to':
'of': [reps,'of',former], out.append('{} to {}\n'.format(start,former))
}[compressType] elif compressType == 'of':
out.append('{} of {}\n'.format(reps,former))
outputAlive = table.data_write(delimiter = ' ') # output processed line geom.add_comment(scriptID + ' ' + ' '.join(sys.argv[1:]))
comments = geom.get_comments()
# --- output finalization -------------------------------------------------------------------------- with open(name,'w') as f:
f.write('{} header\n'.format(3+len(comments)))
table.close() # close ASCII table f.writelines(["{}\n".format(comment) for comment in comments])
f.write('grid a {} b {} c {}\n'.format(*geom.get_grid()))
f.write('size x {} y {} z {}\n'.format(*geom.get_size()))
f.write('homogenization {}\n'.format(geom.get_homogenization()))
f.writelines(out)

View File

@ -34,10 +34,10 @@ for name in filenames:
geom = damask.Geom.from_file(virt_file) geom = damask.Geom.from_file(virt_file)
else: else:
geom = damask.Geom.from_file(name) geom = damask.Geom.from_file(name)
damask.util.croak(geom)
geom.add_comment(scriptID + ' ' + ' '.join(sys.argv[1:])) geom.add_comment(scriptID + ' ' + ' '.join(sys.argv[1:]))
damask.util.croak(geom)
if name is None: if name is None:
sys.stdout.write(str(geom.show())) sys.stdout.write(str(geom.show()))
else: else: