From 8186be6293de991047a9acfbbbc6ad20397f2d28 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 14:27:24 +0100 Subject: [PATCH] compress functionality should be part of the geom class automated decision is base on heuristic whether compression is memory efficient --- processing/pre/geom_clean.py | 5 +-- processing/pre/geom_mirror.py | 5 +-- processing/pre/geom_pack.py | 41 ++---------------------- processing/pre/geom_unpack.py | 5 +-- python/damask/geom.py | 60 +++++++++++++++++++++++++++++++---- 5 files changed, 59 insertions(+), 57 deletions(-) diff --git a/processing/pre/geom_clean.py b/processing/pre/geom_clean.py index 50f3657b2..65700ab61 100755 --- a/processing/pre/geom_clean.py +++ b/processing/pre/geom_clean.py @@ -42,7 +42,4 @@ for name in filenames: geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_mirror.py b/processing/pre/geom_mirror.py index 77ec1f4d7..f27b7eb66 100755 --- a/processing/pre/geom_mirror.py +++ b/processing/pre/geom_mirror.py @@ -46,7 +46,4 @@ for name in filenames: damask.util.croak(geom.mirror(options.directions,options.reflect)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_pack.py b/processing/pre/geom_pack.py index 786a40b95..e927c006f 100755 --- a/processing/pre/geom_pack.py +++ b/processing/pre/geom_pack.py @@ -33,42 +33,5 @@ for name in filenames: damask.util.croak(geom) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - - compressType = None - former = start = -1 - reps = 0 - - if name is None: - f = sys.stdout - else: - f= open(name,'w') - - for current in geom.microstructure.flatten('F'): - if abs(current - former) == 1 and (start - current) == reps*(former - current): - compressType = 'to' - reps += 1 - elif current == former and start == former: - compressType = 'of' - reps += 1 - else: - if compressType is None: - f.write('\n'.join(geom.get_header())+'\n') - elif compressType == '.': - f.write('{}\n'.format(former)) - elif compressType == 'to': - f.write('{} to {}\n'.format(start,former)) - elif compressType == 'of': - f.write('{} of {}\n'.format(reps,former)) - - compressType = '.' - start = current - reps = 1 - - former = current - - if compressType == '.': - f.write('{}\n'.format(former)) - elif compressType == 'to': - f.write('{} to {}\n'.format(start,former)) - elif compressType == 'of': - f.write('{} of {}\n'.format(reps,former)) + + geom.to_file(sys.stdout if name is None else name,pack=True) diff --git a/processing/pre/geom_unpack.py b/processing/pre/geom_unpack.py index 2a2d54130..58bd5de87 100755 --- a/processing/pre/geom_unpack.py +++ b/processing/pre/geom_unpack.py @@ -34,7 +34,4 @@ for name in filenames: damask.util.croak(geom) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/python/damask/geom.py b/python/damask/geom.py index 6dfcda013..61d7c64a4 100644 --- a/python/damask/geom.py +++ b/python/damask/geom.py @@ -299,7 +299,7 @@ class Geom(): return cls(microstructure.reshape(grid),size,origin,homogenization,comments) - def to_file(self,fname): + def to_file(self,fname,pack=None): """ Writes a geom file. @@ -307,15 +307,63 @@ class Geom(): ---------- fname : str or file handle geometry file to write. + pack : bool, optional + compress geometry with 'x of y' and 'a to b'. """ header = self.get_header() grid = self.get_grid() - format_string = '%g' if self.microstructure in np.sctypes['float'] else \ - '%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.microstructure))))) - np.savetxt(fname, - self.microstructure.reshape([grid[0],np.prod(grid[1:])],order='F').T, - header='\n'.join(header), fmt=format_string, comments='') + + if pack is None: + plain = grid.prod()/np.unique(self.microstructure).size < 250 + else: + plain = not pack + + if plain: + format_string = '%g' if self.microstructure in np.sctypes['float'] else \ + '%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.microstructure))))) + np.savetxt(fname, + self.microstructure.reshape([grid[0],np.prod(grid[1:])],order='F').T, + header='\n'.join(header), fmt=format_string, comments='') + else: + if isinstance(fname,str): + f = open(fname,'w') + else: + f = fname + + compressType = None + former = start = -1 + reps = 0 + for current in self.microstructure.flatten('F'): + if abs(current - former) == 1 and (start - current) == reps*(former - current): + compressType = 'to' + reps += 1 + elif current == former and start == former: + compressType = 'of' + reps += 1 + else: + if compressType is None: + f.write('\n'.join(self.get_header())+'\n') + elif compressType == '.': + f.write('{}\n'.format(former)) + elif compressType == 'to': + f.write('{} to {}\n'.format(start,former)) + elif compressType == 'of': + f.write('{} of {}\n'.format(reps,former)) + + compressType = '.' + start = current + reps = 1 + + former = current + + if compressType == '.': + f.write('{}\n'.format(former)) + elif compressType == 'to': + f.write('{} to {}\n'.format(start,former)) + elif compressType == 'of': + f.write('{} of {}\n'.format(reps,former)) + def to_vtk(self,fname=None): """