replace selectively

useful for vicinity_offset and add_primitive
This commit is contained in:
Martin Diehl 2020-08-08 18:41:47 +02:00
parent 56eb57d253
commit 5fcff876f9
2 changed files with 26 additions and 8 deletions

View File

@ -6,7 +6,7 @@ from functools import partial
import numpy as np import numpy as np
from scipy import ndimage,spatial from scipy import ndimage,spatial
import damask from . import environment
from . import VTK from . import VTK
from . import util from . import util
from . import grid_filters from . import grid_filters
@ -143,20 +143,28 @@ class Geom:
""" """
Replace the existing microstructure representation. Replace the existing microstructure representation.
The complete microstructure is replaced (indcluding grid definition),
unless a masked array is provided in which case the grid dimensions
need to match and masked entries are not replaced.
Parameters Parameters
---------- ----------
microstructure : numpy.ndarray microstructure : numpy.ndarray or numpy.ma.core.MaskedArray of shape (:,:,:)
microstructure array (3D). Microstructure indices.
""" """
if microstructure is not None: if microstructure is not None:
if len(microstructure.shape) != 3: if isinstance(microstructure,np.ma.core.MaskedArray):
raise ValueError(f'Invalid microstructure shape {microstructure.shape}') self.microstructure = np.where(microstructure.mask,
elif microstructure.dtype not in np.sctypes['float'] + np.sctypes['int']: self.microstructure,microstructure.data)
raise TypeError(f'Invalid microstructue data type {microstructure.dtype}')
else: else:
self.microstructure = np.copy(microstructure) self.microstructure = np.copy(microstructure)
if len(self.microstructure.shape) != 3:
raise ValueError(f'Invalid microstructure shape {microstructure.shape}')
elif self.microstructure.dtype not in np.sctypes['float'] + np.sctypes['int']:
raise TypeError(f'Invalid microstructue data type {microstructure.dtype}')
def set_size(self,size): def set_size(self,size):
""" """
@ -359,7 +367,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(damask.environment.options['DAMASK_NUM_THREADS'])) pool = multiprocessing.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()

View File

@ -40,6 +40,16 @@ class TestGeom:
print(modified) print(modified)
assert geom_equal(modified,default) assert geom_equal(modified,default)
@pytest.mark.parametrize('masked',[True,False])
def test_set_microstructure(self,default,masked):
old = default.get_microstructure()
new = np.random.randint(200,size=default.grid)
default.set_microstructure(np.ma.MaskedArray(new,np.full_like(new,masked)))
if masked:
assert np.all(default.microstructure==old)
else:
assert np.all(default.microstructure==new)
def test_write_read_str(self,default,tmpdir): def test_write_read_str(self,default,tmpdir):
default.to_file(str(tmpdir.join('default.geom'))) default.to_file(str(tmpdir.join('default.geom')))