diff --git a/python/damask/_geom.py b/python/damask/_geom.py index 7ee923ad8..144a80b70 100644 --- a/python/damask/_geom.py +++ b/python/damask/_geom.py @@ -6,7 +6,7 @@ from functools import partial import numpy as np from scipy import ndimage,spatial -import damask +from . import environment from . import VTK from . import util from . import grid_filters @@ -143,20 +143,28 @@ class Geom: """ 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 ---------- - microstructure : numpy.ndarray - microstructure array (3D). + microstructure : numpy.ndarray or numpy.ma.core.MaskedArray of shape (:,:,:) + Microstructure indices. """ if microstructure is not None: - if len(microstructure.shape) != 3: - raise ValueError(f'Invalid microstructure shape {microstructure.shape}') - elif microstructure.dtype not in np.sctypes['float'] + np.sctypes['int']: - raise TypeError(f'Invalid microstructue data type {microstructure.dtype}') + if isinstance(microstructure,np.ma.core.MaskedArray): + self.microstructure = np.where(microstructure.mask, + self.microstructure,microstructure.data) else: 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): """ @@ -359,7 +367,7 @@ class Geom: seeds_p = seeds 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]) pool.close() pool.join() diff --git a/python/tests/test_Geom.py b/python/tests/test_Geom.py index 155178322..f5ffbd6bf 100644 --- a/python/tests/test_Geom.py +++ b/python/tests/test_Geom.py @@ -40,6 +40,16 @@ class TestGeom: print(modified) 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): default.to_file(str(tmpdir.join('default.geom')))