DAMASK_EICMD/python/tests/test_seeds.py

72 lines
3.3 KiB
Python
Raw Permalink Normal View History

2020-09-25 00:36:26 +05:30
import pytest
import numpy as np
from scipy.spatial import cKDTree
from damask import seeds
from damask import grid_filters
2020-12-04 11:42:18 +05:30
from damask import Grid
2020-09-25 00:36:26 +05:30
class TestSeeds:
2020-12-04 02:28:24 +05:30
@pytest.mark.parametrize('cells',[None,np.ones(3,dtype='i')*10])
def test_from_random(self,cells):
2020-09-25 00:36:26 +05:30
N_seeds = np.random.randint(30,300)
size = np.ones(3) + np.random.random(3)
2020-12-04 02:28:24 +05:30
coords = seeds.from_random(size,N_seeds,cells)
2020-09-25 00:36:26 +05:30
assert (0<=coords).all() and (coords<size).all()
2020-09-25 00:56:16 +05:30
@pytest.mark.parametrize('periodic',[True,False])
def test_from_Poisson_disc(self,periodic):
2020-09-25 00:36:26 +05:30
N_seeds = np.random.randint(30,300)
N_candidates = N_seeds//15
distance = np.random.random()
size = np.ones(3)*distance*N_seeds
2020-09-25 00:56:16 +05:30
coords = seeds.from_Poisson_disc(size,N_seeds,N_candidates,distance,periodic=periodic)
min_dists, _ = cKDTree(coords,boxsize=size).query(coords, 2) if periodic else \
cKDTree(coords).query(coords, 2)
2020-09-25 00:36:26 +05:30
assert (0<= coords).all() and (coords<size).all() and np.min(min_dists[:,1])>=distance
2020-09-25 01:13:04 +05:30
2021-04-28 11:27:20 +05:30
@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)
2020-12-04 11:42:18 +05:30
def test_from_grid_reconstruct(self):
2020-12-04 02:28:24 +05:30
cells = np.random.randint(10,20,3)
2020-09-25 01:13:04 +05:30
N_seeds = np.random.randint(30,300)
size = np.ones(3) + np.random.random(3)
2020-12-04 02:28:24 +05:30
coords = seeds.from_random(size,N_seeds,cells)
2020-12-04 11:42:18 +05:30
grid_1 = Grid.from_Voronoi_tessellation(cells,size,coords)
coords,material = seeds.from_grid(grid_1)
grid_2 = Grid.from_Voronoi_tessellation(cells,size,coords,material)
assert (grid_2.material==grid_1.material).all()
@pytest.mark.parametrize('periodic',[True,False])
@pytest.mark.parametrize('average',[True,False])
2020-12-04 11:42:18 +05:30
def test_from_grid_grid(self,periodic,average):
2020-12-04 02:28:24 +05:30
cells = np.random.randint(10,20,3)
size = np.ones(3) + np.random.random(3)
coords = grid_filters.coordinates0_point(cells,size).reshape(-1,3)
np.random.shuffle(coords)
2020-12-04 11:42:18 +05:30
grid_1 = Grid.from_Voronoi_tessellation(cells,size,coords)
coords,material = seeds.from_grid(grid_1,average=average,periodic=periodic)
grid_2 = Grid.from_Voronoi_tessellation(cells,size,coords,material)
assert (grid_2.material==grid_1.material).all()
@pytest.mark.parametrize('periodic',[True,False])
@pytest.mark.parametrize('average',[True,False])
@pytest.mark.parametrize('invert',[True,False])
2020-12-04 11:42:18 +05:30
def test_from_grid_selection(self,periodic,average,invert):
2020-12-04 02:28:24 +05:30
cells = np.random.randint(10,20,3)
N_seeds = np.random.randint(30,300)
size = np.ones(3) + np.random.random(3)
2020-12-04 02:28:24 +05:30
coords = seeds.from_random(size,N_seeds,cells)
2020-12-04 11:42:18 +05:30
grid = Grid.from_Voronoi_tessellation(cells,size,coords)
selection=np.random.randint(N_seeds)+1
2022-01-13 03:43:38 +05:30
coords,material = seeds.from_grid(grid,average=average,periodic=periodic,invert_selection=invert,selection=[selection])
assert selection not in material if invert else (selection==material).all()