easier to understand

This commit is contained in:
Martin Diehl 2023-06-12 20:38:14 +02:00
parent 1a4c171109
commit 137c4e3b03
1 changed files with 66 additions and 64 deletions

View File

@ -9,95 +9,89 @@ from damask import seeds
class TestGridFilters: class TestGridFilters:
def test_coordinates0_point(self): def test_coordinates0_point(self):
size = np.random.random(3) # noqa size = np.random.random(3) # noqa
cells = np.random.randint(8,32,(3)) cells = np.random.randint(8,32,(3))
coord = grid_filters.coordinates0_point(cells,size) coord = grid_filters.coordinates0_point(cells,size)
assert np.allclose(coord[0,0,0],size/cells*.5) and coord.shape == tuple(cells) + (3,) assert np.allclose(coord[0,0,0],size/cells*.5) and coord.shape == tuple(cells) + (3,)
def test_coordinates0_node(self): def test_coordinates0_node(self):
size = np.random.random(3) # noqa size = np.random.random(3) # noqa
cells = np.random.randint(8,32,(3)) cells = np.random.randint(8,32,(3))
coord = grid_filters.coordinates0_node(cells,size) coord = grid_filters.coordinates0_node(cells,size)
assert np.allclose(coord[-1,-1,-1],size) and coord.shape == tuple(cells+1) + (3,) assert np.allclose(coord[-1,-1,-1],size) and coord.shape == tuple(cells+1) + (3,)
def test_coord0(self): def test_coord0(self):
size = np.random.random(3) # noqa size = np.random.random(3) # noqa
cells = np.random.randint(8,32,(3)) cells = np.random.randint(8,32,(3))
c = grid_filters.coordinates0_point(cells+1,size+size/cells) c = grid_filters.coordinates0_point(cells+1,size+size/cells)
n = grid_filters.coordinates0_node(cells,size) + size/cells*.5 n = grid_filters.coordinates0_node(cells,size) + size/cells*.5
assert np.allclose(c,n) assert np.allclose(c,n)
@pytest.mark.parametrize('mode',['point','node']) @pytest.mark.parametrize('mode',['point','node'])
def test_grid_DNA(self,mode): def test_grid_DNA(self,mode):
"""Ensure that cellsSizeOrigin_coordinates0_xx is the inverse of coordinates0_xx.""" # noqa """Ensure that cellsSizeOrigin_coordinates0_xx is the inverse of coordinates0_xx.""" # noqa
cells = np.random.randint(8,32,(3)) cells = np.random.randint(8,32,(3))
size = np.random.random(3) size = np.random.random(3)
origin = np.random.random(3) origin = np.random.random(3)
coord0 = eval(f'grid_filters.coordinates0_{mode}(cells,size,origin)') # noqa coord0 = eval(f'grid_filters.coordinates0_{mode}(cells,size,origin)') # noqa
_cells,_size,_origin = eval(f'grid_filters.cellsSizeOrigin_coordinates0_{mode}(coord0.reshape(-1,3,order="F"))') _cells,_size,_origin = eval(f'grid_filters.cellsSizeOrigin_coordinates0_{mode}(coord0.reshape(-1,3,order="F"))')
assert np.allclose(cells,_cells) and np.allclose(size,_size) and np.allclose(origin,_origin) assert np.allclose(cells,_cells) and np.allclose(size,_size) and np.allclose(origin,_origin)
def test_displacement_fluct_periodic(self): def test_displacement_fluct_periodic(self):
"""Ensure that fluctuations are periodic.""" # noqa """Ensure that fluctuations are periodic.""" # noqa
size = np.random.random(3) size = np.random.random(3)
cells = np.random.randint(8,32,(3)) cells = np.random.randint(8,32,(3))
F = np.random.random(tuple(cells)+(3,3)) F = np.random.random(tuple(cells)+(3,3))
assert np.allclose(grid_filters.displacement_fluct_node(size,F), assert np.allclose(grid_filters.displacement_fluct_node(size,F),
grid_filters.point_to_node(grid_filters.displacement_fluct_point(size,F))) grid_filters.point_to_node(grid_filters.displacement_fluct_point(size,F)))
def test_interpolation_to_node(self): def test_interpolation_to_node(self):
size = np.random.random(3) # noqa size = np.random.random(3) # noqa
cells = np.random.randint(8,32,(3)) cells = np.random.randint(8,32,(3))
F = np.random.random(tuple(cells)+(3,3)) F = np.random.random(tuple(cells)+(3,3))
assert np.allclose(grid_filters.coordinates_node(size,F) [1:-1,1:-1,1:-1], assert np.allclose(grid_filters.coordinates_node(size,F) [1:-1,1:-1,1:-1],
grid_filters.point_to_node(grid_filters.coordinates_point(size,F))[1:-1,1:-1,1:-1]) grid_filters.point_to_node(grid_filters.coordinates_point(size,F))[1:-1,1:-1,1:-1])
def test_interpolation_to_cell(self): def test_interpolation_to_cell(self):
cells = np.random.randint(1,30,(3)) # noqa cells = np.random.randint(1,30,(3)) # noqa
coordinates_node_x = np.linspace(0,np.pi*2,num=cells[0]+1) coordinates_node_x = np.linspace(0,np.pi*2,num=cells[0]+1)
node_field_x = np.cos(coordinates_node_x) node_field_x = np.cos(coordinates_node_x)
node_field = np.broadcast_to(node_field_x.reshape(-1,1,1),cells+1) node_field = np.broadcast_to(node_field_x.reshape(-1,1,1),cells+1)
coordinates0_point_x = coordinates_node_x[:-1]+coordinates_node_x[1]*.5 coordinates0_point_x = coordinates_node_x[:-1]+coordinates_node_x[1]*.5
cell_field_x = np.interp(coordinates0_point_x,coordinates_node_x,node_field_x,period=np.pi*2.) cell_field_x = np.interp(coordinates0_point_x,coordinates_node_x,node_field_x,period=np.pi*2.)
cell_field = np.broadcast_to(cell_field_x.reshape(-1,1,1),cells) cell_field = np.broadcast_to(cell_field_x.reshape(-1,1,1),cells)
assert np.allclose(cell_field,grid_filters.node_to_point(node_field)) assert np.allclose(cell_field,grid_filters.node_to_point(node_field))
@pytest.mark.parametrize('mode',['point','node']) @pytest.mark.parametrize('mode',['point','node'])
def test_coordinates0_origin(self,mode): def test_coordinates0_origin(self,mode):
origin= np.random.random(3) # noqa origin= np.random.random(3) # noqa
size = np.random.random(3) # noqa size = np.random.random(3) # noqa
cells = np.random.randint(8,32,(3)) cells = np.random.randint(8,32,(3))
shifted = eval(f'grid_filters.coordinates0_{mode}(cells,size,origin)') shifted = eval(f'grid_filters.coordinates0_{mode}(cells,size,origin)')
unshifted = eval(f'grid_filters.coordinates0_{mode}(cells,size)') unshifted = eval(f'grid_filters.coordinates0_{mode}(cells,size)')
if mode == 'cell': if mode == 'cell':
assert np.allclose(shifted,unshifted+np.broadcast_to(origin,tuple(cells) +(3,))) assert np.allclose(shifted,unshifted+np.broadcast_to(origin,tuple(cells) +(3,)))
elif mode == 'node': elif mode == 'node':
assert np.allclose(shifted,unshifted+np.broadcast_to(origin,tuple(cells+1)+(3,))) assert np.allclose(shifted,unshifted+np.broadcast_to(origin,tuple(cells+1)+(3,)))
@pytest.mark.parametrize('function',[grid_filters.displacement_avg_point, @pytest.mark.parametrize('function',[grid_filters.displacement_avg_point,
grid_filters.displacement_avg_node]) grid_filters.displacement_avg_node])
def test_displacement_avg_vanishes(self,function): def test_displacement_avg_vanishes(self,function):
"""Ensure that random fluctuations in F do not result in average displacement.""" # noqa """Ensure that random fluctuations in F do not result in average displacement.""" # noqa
size = np.random.random(3) size = np.random.random(3) + 1.0
cells = np.random.randint(8,32,(3)) cells = np.random.randint(8,32,(3))
F = np.random.random(tuple(cells)+(3,3)) F = np.random.random(tuple(cells)+(3,3))
F += np.eye(3) - np.average(F,axis=(0,1,2)) F += np.eye(3) - np.average(F,axis=(0,1,2))
assert np.allclose(function(size,F),0.0) assert np.allclose(function(size,F),0.0)
@pytest.mark.parametrize('function',[grid_filters.displacement_fluct_point, @pytest.mark.parametrize('function',[grid_filters.displacement_avg_point,
grid_filters.displacement_fluct_node]) grid_filters.displacement_avg_node])
def test_displacement_fluct_vanishes_avg(self,function): def test_displacement_avg_vanishes_simple(self,function):
"""Ensure that constant F does not result in fluctuating displacement.""" # noqa
size = np.random.random(3)
cells = np.random.randint(8,32,(3))
F = np.broadcast_to(np.random.random((3,3)), tuple(cells)+(3,3))
assert np.allclose(function(size,F),0.0)
def test_displacement_fluct_vanishes_plus_minus(self):
F = np.eye(3) F = np.eye(3)
size = np.random.random(3) + 1.0
F_c = F.copy() F_c = F.copy()
F_t = F.copy() F_t = F.copy()
@ -105,8 +99,16 @@ class TestGridFilters:
F_t[0,0] = 1.2 F_t[0,0] = 1.2
F_no_avg = np.concatenate([np.broadcast_to(_,(10,20,20,3,3)) for _ in [F_t,F_c]]) F_no_avg = np.concatenate([np.broadcast_to(_,(10,20,20,3,3)) for _ in [F_t,F_c]])
assert np.allclose(grid_filters.displacement_point([1,1,1],F_no_avg), assert np.allclose(function(size,F_no_avg),0.0)
grid_filters.displacement_fluct_point([1,1,1],F_no_avg))
@pytest.mark.parametrize('function',[grid_filters.displacement_fluct_point,
grid_filters.displacement_fluct_node])
def test_displacement_fluct_vanishes_avg(self,function):
"""Ensure that constant F does not result in fluctuating displacement.""" # noqa
size = np.random.random(3)
cells = np.random.randint(8,32,(3))
F = np.broadcast_to(np.random.random((3,3)), tuple(cells)+(3,3))
assert np.allclose(function(size,F),0.0)
displacement_fluct_test_data = [ displacement_fluct_test_data = [
(['np.sin(np.pi*2*nodes[...,0]/size[0])', '0.0', '0.0', (['np.sin(np.pi*2*nodes[...,0]/size[0])', '0.0', '0.0',