From ff8ce3840b39eccd9718d4a85240897968ffac72 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Wed, 28 Apr 2021 07:57:20 +0200 Subject: [PATCH] handle infinite loop --- PRIVATE | 2 +- python/damask/seeds.py | 19 +++++++++++++------ python/setup.py | 2 +- python/tests/test_seeds.py | 9 +++++++++ 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/PRIVATE b/PRIVATE index 7f0594060..ab64793bb 160000 --- a/PRIVATE +++ b/PRIVATE @@ -1 +1 @@ -Subproject commit 7f0594060779d9a8a4e774d558134309ab77b96e +Subproject commit ab64793bb04c506d815ebc850672ed0f2d013e67 diff --git a/python/damask/seeds.py b/python/damask/seeds.py index aeab2e64a..02a050e66 100644 --- a/python/damask/seeds.py +++ b/python/damask/seeds.py @@ -71,18 +71,25 @@ def from_Poisson_disc(size,N_seeds,N_candidates,distance,periodic=True,rng_seed= coords = _np.empty((N_seeds,3)) coords[0] = rng.random(3) * size - i = 1 + s = 1 + i = 0 progress = util._ProgressBar(N_seeds+1,'',50) - while i < N_seeds: + while s < N_seeds: candidates = rng.random((N_candidates,3))*_np.broadcast_to(size,(N_candidates,3)) - tree = _spatial.cKDTree(coords[:i],boxsize=size) if periodic else \ - _spatial.cKDTree(coords[:i]) + tree = _spatial.cKDTree(coords[:s],boxsize=size) if periodic else \ + _spatial.cKDTree(coords[:s]) distances, dev_null = tree.query(candidates) best = distances.argmax() if distances[best] > distance: # require minimum separation - coords[i] = candidates[best] # maximum separation to existing point cloud + coords[s] = candidates[best] # maximum separation to existing point cloud + s += 1 + progress.update(s) + i = 0 + else: i += 1 - progress.update(i) + + if i == 100: + raise ValueError('Seeding not possible') return coords diff --git a/python/setup.py b/python/setup.py index f6ba83b7b..e590a1197 100644 --- a/python/setup.py +++ b/python/setup.py @@ -4,7 +4,7 @@ import re # https://www.python.org/dev/peps/pep-0440 with open(Path(__file__).parent/'damask/VERSION') as f: - version = re.sub(r'(-([^-]*)).*$',r'.\2',re.sub(r'^v(\d+\.\d+(\.\d+)?)',r'\1',f.readline().strip())) + version = re.sub(r'(-([^-]*)).*$',r'.\2',re.sub(r'^v(\d+\.\d+(\.\d+)?)',r'\1',f.readline().strip())) setuptools.setup( name='damask', diff --git a/python/tests/test_seeds.py b/python/tests/test_seeds.py index af27e74af..e68260aa4 100644 --- a/python/tests/test_seeds.py +++ b/python/tests/test_seeds.py @@ -26,6 +26,15 @@ class TestSeeds: cKDTree(coords).query(coords, 2) assert (0<= coords).all() and (coords=distance + @pytest.mark.parametrize('periodic',[True,False]) + def test_from_Poisson_disc_invalid(self,periodic): + N_seeds = np.random.randint(30,300) + N_candidates = N_seeds//15 + distance = np.random.random() + size = np.ones(3)*distance + with pytest.raises(ValueError): + seeds.from_Poisson_disc(size,N_seeds,N_candidates,distance,periodic=periodic) + def test_from_grid_reconstruct(self): cells = np.random.randint(10,20,3) N_seeds = np.random.randint(30,300)