def test_div(self, field_def, div_def): size = np.random.random(3) + 1.0 cells = np.random.randint(8, 32, (3)) nodes = grid_filters.coordinates0_point(cells, size) my_locals = locals() # needed for list comprehension field = np.stack([ np.broadcast_to(eval(f, globals(), my_locals), cells) for f in field_def ], axis=-1) field = field.reshape( tuple(cells) + ((3, 3) if len(field_def) == 9 else (3, ))) div = np.stack([ np.broadcast_to(eval(c, globals(), my_locals), cells) for c in div_def ], axis=-1) if len(div_def) == 3: div = div.reshape(tuple(cells) + ((3, ))) else: div = div.reshape(tuple(cells)) assert np.allclose(div, grid_filters.divergence(size, field))
def test_from_table_recover(self,tmp_path): cells = np.random.randint(60,100,3) size = np.ones(3)+np.random.rand(3) s = seeds.from_random(size,np.random.randint(60,100)) grid = Grid.from_Voronoi_tessellation(cells,size,s) coords = grid_filters.coordinates0_point(cells,size) t = Table(np.column_stack((coords.reshape(-1,3,order='F'),grid.material.flatten(order='F'))),{'c':3,'m':1}) assert grid_equal(grid.sort().renumber(),Grid.from_table(t,'c',['m']))
def test_from_table(self): cells = np.random.randint(60,100,3) size = np.ones(3)+np.random.rand(3) coords = grid_filters.coordinates0_point(cells,size).reshape(-1,3,order='F') z=np.ones(cells.prod()) z[cells[:2].prod()*int(cells[2]/2):]=0 t = Table(np.column_stack((coords,z)),{'coords':3,'z':1}) t = t.add('indicator',t.get('coords')[:,0]) g = Grid.from_table(t,'coords',['indicator','z']) assert g.N_materials == g.cells[0]*2 and (g.material[:,:,-1]-g.material[:,:,0] == cells[0]).all()
def test_from_grid_grid(self, periodic, average): 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) 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()
def test_curl(self,field_def,curl_def): size = np.random.random(3)+1.0 cells = np.random.randint(8,32,(3)) nodes = grid_filters.coordinates0_point(cells,size) my_locals = locals() # needed for list comprehension field = np.stack([np.broadcast_to(eval(f,globals(),my_locals),cells) for f in field_def],axis=-1) field = field.reshape(tuple(cells) + ((3,3) if len(field_def)==9 else (3,))) curl = np.stack([np.broadcast_to(eval(c,globals(),my_locals),cells) for c in curl_def], axis=-1) curl = curl.reshape(tuple(cells) + ((3,3) if len(curl_def)==9 else (3,))) assert np.allclose(curl,grid_filters.curl(size,field))
def test_coordinates(self, default, mode): if mode == 'cell': a = grid_filters.coordinates0_point(default.cells, default.size, default.origin) b = default.coordinates0_point.reshape(tuple(default.cells) + (3, ), order='F') elif mode == 'node': a = grid_filters.coordinates0_node(default.cells, default.size, default.origin) b = default.coordinates0_node.reshape(tuple(default.cells + 1) + (3, ), order='F') assert np.allclose(a, b)
def test_compare_reference_rectilinearGrid(self, update, ref_path, tmp_path): cells = np.array([5, 6, 7], int) size = np.array([.6, 1., .5]) rectilinearGrid = VTK.from_rectilinear_grid(cells, size) c = grid_filters.coordinates0_point(cells, size).reshape(-1, 3, order='F') n = grid_filters.coordinates0_node(cells, size).reshape(-1, 3, order='F') rectilinearGrid.add(c, 'cell') rectilinearGrid.add(n, 'node') if update: rectilinearGrid.save(ref_path / 'rectilinearGrid') else: reference = VTK.load(ref_path / 'rectilinearGrid.vtr') assert rectilinearGrid.__repr__() == reference.__repr__() and \ np.allclose(rectilinearGrid.get('cell'),c)
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, )
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)