From 855bf124d397450e988c5e6a1704a5eec5290727 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Wed, 28 Oct 2020 14:39:41 +0100 Subject: [PATCH] faster https://stackoverflow.com/questions/16992713 --- python/damask/_geom.py | 11 +++++++---- python/tests/test_Geom.py | 7 +++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/python/damask/_geom.py b/python/damask/_geom.py index e0e6fcdc6..7dfca4cd1 100644 --- a/python/damask/_geom.py +++ b/python/damask/_geom.py @@ -787,11 +787,14 @@ class Geom: New material indices. """ - substituted = self.material.copy() - for from_ma,to_ma in zip(from_material,to_material): - substituted[self.material==from_ma] = to_ma + mapper = dict(zip(from_material,to_material)) + + def mp(entry): + return mapper[entry] if entry in mapper else entry + + mp = np.vectorize(mp) - return Geom(material = substituted, + return Geom(material = mp(self.material).reshape(self.grid), size = self.size, origin = self.origin, comments = self.comments+[util.execution_stamp('Geom','substitute')], diff --git a/python/tests/test_Geom.py b/python/tests/test_Geom.py index aa6cf4baf..73d157cfd 100644 --- a/python/tests/test_Geom.py +++ b/python/tests/test_Geom.py @@ -195,6 +195,13 @@ class TestGeom: modified.substitute(np.arange(default.material.max())+1+offset, np.arange(default.material.max())+1)) + def test_substitute_invariant(self,default): + f = np.unique(default.material.flatten())[:np.random.randint(1,default.material.max())] + t = np.random.permutation(f) + modified = default.substitute(f,t) + assert np.array_equiv(t,f) or (not geom_equal(modified,default)) + assert geom_equal(default, modified.substitute(t,f)) + @pytest.mark.parametrize('axis_angle',[np.array([1,0,0,86.7]), np.array([0,1,0,90.4]), np.array([0,0,1,90]), np.array([1,0,0,175]),np.array([0,-1,0,178]),np.array([0,0,1,180])])