small polishing

This commit is contained in:
Martin Diehl 2020-09-18 16:32:08 +02:00
parent f23b89f055
commit c42511f101
3 changed files with 20 additions and 30 deletions

View File

@ -1,7 +1,5 @@
import sys
import copy import copy
import multiprocessing import multiprocessing as mp
from io import StringIO
from functools import partial from functools import partial
import numpy as np import numpy as np
@ -404,7 +402,7 @@ class Geom:
seeds_p = seeds seeds_p = seeds
coords = grid_filters.cell_coord0(grid,size).reshape(-1,3) 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]) result = pool.map_async(partial(Geom._find_closest_seed,seeds_p,weights_p), [coord for coord in coords])
pool.close() pool.close()
pool.join() 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. Writes a geom file.
@ -455,7 +453,7 @@ class Geom:
---------- ----------
fname : str or file handle fname : str or file handle
Geometry file to write with extension '.geom'. Geometry file to write with extension '.geom'.
pack : bool, optional compress : bool, optional
Compress geometry with 'x of y' and 'a to b'. Compress geometry with 'x of y' and 'a to b'.
""" """
@ -467,10 +465,10 @@ class Geom:
grid = self.get_grid() grid = self.get_grid()
if pack is None: if compress is None:
plain = grid.prod()/self.N_microstructure < 250 plain = grid.prod()/self.N_microstructure < 250
else: else:
plain = not pack plain = not compress
if plain: if plain:
format_string = '%g' if self.microstructure.dtype in np.sctypes['float'] else \ 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') f.write(f'{reps} of {former}\n')
def save_vtr(self,fname=None): def save_vtr(self,fname,compress=True):
""" """
Generates vtk rectilinear grid. Generates vtk rectilinear grid.
@ -527,24 +525,21 @@ class Geom:
fname : str, optional fname : str, optional
Filename to write. If no file is given, a string is returned. Filename to write. If no file is given, a string is returned.
Valid extension is .vtr, it will be appended if not given. 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 = VTK.from_rectilinearGrid(self.grid,self.size,self.origin)
v.add(self.microstructure.flatten(order='F'),'materialpoint') v.add(self.microstructure.flatten(order='F'),'materialpoint')
v.add_comments(self.comments) v.add_comments(self.comments)
if fname: v.save(fname if str(fname).endswith('.vtr') else str(fname)+'.vtr',parallel=False,compress=compress)
v.save(fname if str(fname).endswith('.vtr') else str(fname)+'.vtr')
else:
sys.stdout.write(v.__repr__())
def as_ASCII(self,pack=False): def show(self):
"""Format geometry as human-readable ASCII.""" """Show on screen."""
f = StringIO() v = VTK.from_rectilinearGrid(self.grid,self.size,self.origin)
self.save_ASCII(f,pack) v.show()
f.seek(0)
return ''.join(f.readlines())
def add_primitive(self,dimension,center,exponent, def add_primitive(self,dimension,center,exponent,

View File

@ -178,6 +178,8 @@ class VTK:
Filename for writing. Filename for writing.
parallel : boolean, optional parallel : boolean, optional
Write data in parallel background process. Defaults to True. 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): if isinstance(self.vtk_data,vtk.vtkRectilinearGrid):

View File

@ -69,18 +69,11 @@ class TestGeom:
def test_write_read_file(self,default,tmpdir): def test_write_read_file(self,default,tmpdir):
with open(tmpdir/'default.geom','w') as f: 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: with open(tmpdir/'default.geom') as f:
new = Geom.load_ASCII(f) new = Geom.load_ASCII(f)
assert geom_equal(default,new) 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): def test_read_write_vtr(self,default,tmpdir):
default.save_vtr(tmpdir/'default') default.save_vtr(tmpdir/'default')
for _ in range(10): for _ in range(10):
@ -107,9 +100,9 @@ class TestGeom:
Geom.load_vtr(tmpdir/'no_materialpoint.vtr') Geom.load_vtr(tmpdir/'no_materialpoint.vtr')
@pytest.mark.parametrize('pack',[True,False]) @pytest.mark.parametrize('compress',[True,False])
def test_pack(self,default,tmpdir,pack): def test_compress(self,default,tmpdir,compress):
default.save_ASCII(tmpdir/'default.geom',pack=pack) default.save_ASCII(tmpdir/'default.geom',compress=compress)
new = Geom.load_ASCII(tmpdir/'default.geom') new = Geom.load_ASCII(tmpdir/'default.geom')
assert geom_equal(new,default) assert geom_equal(new,default)