From c42511f101c9607ffa2d80efee1c6dfadde548ec Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Fri, 18 Sep 2020 16:32:08 +0200 Subject: [PATCH] small polishing --- python/damask/_geom.py | 33 ++++++++++++++------------------- python/damask/_vtk.py | 2 ++ python/tests/test_Geom.py | 15 ++++----------- 3 files changed, 20 insertions(+), 30 deletions(-) diff --git a/python/damask/_geom.py b/python/damask/_geom.py index 60f7b90a5..be4f596c1 100644 --- a/python/damask/_geom.py +++ b/python/damask/_geom.py @@ -1,7 +1,5 @@ -import sys import copy -import multiprocessing -from io import StringIO +import multiprocessing as mp from functools import partial import numpy as np @@ -404,7 +402,7 @@ class Geom: seeds_p = seeds coords = grid_filters.cell_coord0(grid,size).reshape(-1,3) - pool = multiprocessing.Pool(processes = int(environment.options['DAMASK_NUM_THREADS'])) + pool = mp.Pool(processes = int(environment.options['DAMASK_NUM_THREADS'])) result = pool.map_async(partial(Geom._find_closest_seed,seeds_p,weights_p), [coord for coord in coords]) pool.close() pool.join() @@ -447,7 +445,7 @@ class Geom: ) - def save_ASCII(self,fname,pack=None): + def save_ASCII(self,fname,compress=None): """ Writes a geom file. @@ -455,7 +453,7 @@ class Geom: ---------- fname : str or file handle Geometry file to write with extension '.geom'. - pack : bool, optional + compress : bool, optional Compress geometry with 'x of y' and 'a to b'. """ @@ -467,10 +465,10 @@ class Geom: grid = self.get_grid() - if pack is None: + if compress is None: plain = grid.prod()/self.N_microstructure < 250 else: - plain = not pack + plain = not compress if plain: format_string = '%g' if self.microstructure.dtype in np.sctypes['float'] else \ @@ -518,7 +516,7 @@ class Geom: f.write(f'{reps} of {former}\n') - def save_vtr(self,fname=None): + def save_vtr(self,fname,compress=True): """ Generates vtk rectilinear grid. @@ -527,24 +525,21 @@ class Geom: fname : str, optional Filename to write. If no file is given, a string is returned. Valid extension is .vtr, it will be appended if not given. + compress : bool, optional + Compress with zlib algorithm. Defaults to True. """ v = VTK.from_rectilinearGrid(self.grid,self.size,self.origin) v.add(self.microstructure.flatten(order='F'),'materialpoint') v.add_comments(self.comments) - if fname: - v.save(fname if str(fname).endswith('.vtr') else str(fname)+'.vtr') - else: - sys.stdout.write(v.__repr__()) + v.save(fname if str(fname).endswith('.vtr') else str(fname)+'.vtr',parallel=False,compress=compress) - def as_ASCII(self,pack=False): - """Format geometry as human-readable ASCII.""" - f = StringIO() - self.save_ASCII(f,pack) - f.seek(0) - return ''.join(f.readlines()) + def show(self): + """Show on screen.""" + v = VTK.from_rectilinearGrid(self.grid,self.size,self.origin) + v.show() def add_primitive(self,dimension,center,exponent, diff --git a/python/damask/_vtk.py b/python/damask/_vtk.py index 2e424aefc..c1fe52f38 100644 --- a/python/damask/_vtk.py +++ b/python/damask/_vtk.py @@ -178,6 +178,8 @@ class VTK: Filename for writing. parallel : boolean, optional Write data in parallel background process. Defaults to True. + compress : bool, optional + Compress with zlib algorithm. Defaults to True. """ if isinstance(self.vtk_data,vtk.vtkRectilinearGrid): diff --git a/python/tests/test_Geom.py b/python/tests/test_Geom.py index 8c9e84c1f..12f50be96 100644 --- a/python/tests/test_Geom.py +++ b/python/tests/test_Geom.py @@ -69,18 +69,11 @@ class TestGeom: def test_write_read_file(self,default,tmpdir): with open(tmpdir/'default.geom','w') as f: - default.save_ASCII(f,pack=True) + default.save_ASCII(f,compress=True) with open(tmpdir/'default.geom') as f: new = Geom.load_ASCII(f) assert geom_equal(default,new) - def test_write_as_ASCII(self,default,tmpdir): - with open(tmpdir/'str.geom','w') as f: - f.write(default.as_ASCII()) - with open(tmpdir/'str.geom') as f: - new = Geom.load_ASCII(f) - assert geom_equal(default,new) - def test_read_write_vtr(self,default,tmpdir): default.save_vtr(tmpdir/'default') for _ in range(10): @@ -107,9 +100,9 @@ class TestGeom: Geom.load_vtr(tmpdir/'no_materialpoint.vtr') - @pytest.mark.parametrize('pack',[True,False]) - def test_pack(self,default,tmpdir,pack): - default.save_ASCII(tmpdir/'default.geom',pack=pack) + @pytest.mark.parametrize('compress',[True,False]) + def test_compress(self,default,tmpdir,compress): + default.save_ASCII(tmpdir/'default.geom',compress=compress) new = Geom.load_ASCII(tmpdir/'default.geom') assert geom_equal(new,default)