From 137c4e3b0358b67bd8ad05afe9f8543c663460f7 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Mon, 12 Jun 2023 20:38:14 +0200 Subject: [PATCH] easier to understand --- python/tests/test_grid_filters.py | 130 +++++++++++++++--------------- 1 file changed, 66 insertions(+), 64 deletions(-) diff --git a/python/tests/test_grid_filters.py b/python/tests/test_grid_filters.py index 2ca239b84..20575f2bd 100644 --- a/python/tests/test_grid_filters.py +++ b/python/tests/test_grid_filters.py @@ -9,95 +9,89 @@ from damask import seeds class TestGridFilters: def test_coordinates0_point(self): - size = np.random.random(3) # noqa - cells = np.random.randint(8,32,(3)) - coord = grid_filters.coordinates0_point(cells,size) - assert np.allclose(coord[0,0,0],size/cells*.5) and coord.shape == tuple(cells) + (3,) + size = np.random.random(3) # noqa + cells = np.random.randint(8,32,(3)) + coord = grid_filters.coordinates0_point(cells,size) + assert np.allclose(coord[0,0,0],size/cells*.5) and coord.shape == tuple(cells) + (3,) def test_coordinates0_node(self): - size = np.random.random(3) # noqa - cells = np.random.randint(8,32,(3)) - coord = grid_filters.coordinates0_node(cells,size) - assert np.allclose(coord[-1,-1,-1],size) and coord.shape == tuple(cells+1) + (3,) + size = np.random.random(3) # noqa + cells = np.random.randint(8,32,(3)) + coord = grid_filters.coordinates0_node(cells,size) + assert np.allclose(coord[-1,-1,-1],size) and coord.shape == tuple(cells+1) + (3,) def test_coord0(self): - size = np.random.random(3) # noqa - cells = np.random.randint(8,32,(3)) - c = grid_filters.coordinates0_point(cells+1,size+size/cells) - n = grid_filters.coordinates0_node(cells,size) + size/cells*.5 - assert np.allclose(c,n) + size = np.random.random(3) # noqa + cells = np.random.randint(8,32,(3)) + c = grid_filters.coordinates0_point(cells+1,size+size/cells) + n = grid_filters.coordinates0_node(cells,size) + size/cells*.5 + assert np.allclose(c,n) @pytest.mark.parametrize('mode',['point','node']) def test_grid_DNA(self,mode): - """Ensure that cellsSizeOrigin_coordinates0_xx is the inverse of coordinates0_xx.""" # noqa - cells = np.random.randint(8,32,(3)) - size = np.random.random(3) - origin = np.random.random(3) - 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"))') - assert np.allclose(cells,_cells) and np.allclose(size,_size) and np.allclose(origin,_origin) + """Ensure that cellsSizeOrigin_coordinates0_xx is the inverse of coordinates0_xx.""" # noqa + cells = np.random.randint(8,32,(3)) + size = np.random.random(3) + origin = np.random.random(3) + 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"))') + assert np.allclose(cells,_cells) and np.allclose(size,_size) and np.allclose(origin,_origin) def test_displacement_fluct_periodic(self): - """Ensure that fluctuations are periodic.""" # noqa - size = np.random.random(3) - cells = np.random.randint(8,32,(3)) - F = np.random.random(tuple(cells)+(3,3)) - assert np.allclose(grid_filters.displacement_fluct_node(size,F), - grid_filters.point_to_node(grid_filters.displacement_fluct_point(size,F))) + """Ensure that fluctuations are periodic.""" # noqa + size = np.random.random(3) + cells = np.random.randint(8,32,(3)) + F = np.random.random(tuple(cells)+(3,3)) + assert np.allclose(grid_filters.displacement_fluct_node(size,F), + grid_filters.point_to_node(grid_filters.displacement_fluct_point(size,F))) def test_interpolation_to_node(self): - size = np.random.random(3) # noqa - cells = np.random.randint(8,32,(3)) - F = np.random.random(tuple(cells)+(3,3)) - 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]) + size = np.random.random(3) # noqa + cells = np.random.randint(8,32,(3)) + F = np.random.random(tuple(cells)+(3,3)) + 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]) 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) - node_field_x = np.cos(coordinates_node_x) - node_field = np.broadcast_to(node_field_x.reshape(-1,1,1),cells+1) + coordinates_node_x = np.linspace(0,np.pi*2,num=cells[0]+1) + node_field_x = np.cos(coordinates_node_x) + 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 - 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) + 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 = 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']) def test_coordinates0_origin(self,mode): - origin= np.random.random(3) # noqa - size = np.random.random(3) # noqa - cells = np.random.randint(8,32,(3)) - shifted = eval(f'grid_filters.coordinates0_{mode}(cells,size,origin)') - unshifted = eval(f'grid_filters.coordinates0_{mode}(cells,size)') - if mode == 'cell': + origin= np.random.random(3) # noqa + size = np.random.random(3) # noqa + cells = np.random.randint(8,32,(3)) + shifted = eval(f'grid_filters.coordinates0_{mode}(cells,size,origin)') + unshifted = eval(f'grid_filters.coordinates0_{mode}(cells,size)') + if mode == 'cell': 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,))) @pytest.mark.parametrize('function',[grid_filters.displacement_avg_point, grid_filters.displacement_avg_node]) def test_displacement_avg_vanishes(self,function): - """Ensure that random fluctuations in F do not result in average displacement.""" # noqa - size = np.random.random(3) - cells = np.random.randint(8,32,(3)) - F = np.random.random(tuple(cells)+(3,3)) - F += np.eye(3) - np.average(F,axis=(0,1,2)) - assert np.allclose(function(size,F),0.0) + """Ensure that random fluctuations in F do not result in average displacement.""" # noqa + size = np.random.random(3) + 1.0 + cells = np.random.randint(8,32,(3)) + F = np.random.random(tuple(cells)+(3,3)) + F += np.eye(3) - np.average(F,axis=(0,1,2)) + assert np.allclose(function(size,F),0.0) - @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) - - def test_displacement_fluct_vanishes_plus_minus(self): + @pytest.mark.parametrize('function',[grid_filters.displacement_avg_point, + grid_filters.displacement_avg_node]) + def test_displacement_avg_vanishes_simple(self,function): F = np.eye(3) + size = np.random.random(3) + 1.0 F_c = F.copy() F_t = F.copy() @@ -105,8 +99,16 @@ class TestGridFilters: 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]]) - assert np.allclose(grid_filters.displacement_point([1,1,1],F_no_avg), - grid_filters.displacement_fluct_point([1,1,1],F_no_avg)) + assert np.allclose(function(size,F_no_avg),0.0) + + @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 = [ (['np.sin(np.pi*2*nodes[...,0]/size[0])', '0.0', '0.0',