def test_div(self, field_def, div_def): size = np.random.random(3) + 1.0 grid = np.random.randint(8, 32, (3)) nodes = grid_filters.cell_coord0(grid, size) my_locals = locals() # needed for list comprehension field = np.stack([ np.broadcast_to(eval(f, globals(), my_locals), grid) for f in field_def ], axis=-1) field = field.reshape( tuple(grid) + ((3, 3) if len(field_def) == 9 else (3, ))) div = np.stack([ np.broadcast_to(eval(c, globals(), my_locals), grid) for c in div_def ], axis=-1) if len(div_def) == 3: div = div.reshape(tuple(grid) + ((3, ))) else: div = div.reshape(tuple(grid)) assert np.allclose(div, grid_filters.divergence(size, field))
def test_from_table_recover(self,tmp_path): grid = np.random.randint(60,100,3) size = np.ones(3)+np.random.rand(3) s = seeds.from_random(size,np.random.randint(60,100)) geom = Geom.from_Voronoi_tessellation(grid,size,s) coords = grid_filters.cell_coord0(grid,size) t = Table(np.column_stack((coords.reshape(-1,3,order='F'),geom.material.flatten(order='F'))),{'c':3,'m':1}) assert geom_equal(geom.sort().renumber(),Geom.from_table(t,'c',['m']))
def test_from_table(self): grid = np.random.randint(60,100,3) size = np.ones(3)+np.random.rand(3) coords = grid_filters.cell_coord0(grid,size).reshape(-1,3,order='F') z=np.ones(grid.prod()) z[grid[:2].prod()*int(grid[2]/2):]=0 t = Table(np.column_stack((coords,z)),{'coords':3,'z':1}) g = Geom.from_table(t,'coords',['1_coords','z']) assert g.N_materials == g.grid[0]*2 and (g.material[:,:,-1]-g.material[:,:,0] == grid[0]).all()
def test_from_geom_grid(self, periodic, average): grid = np.random.randint(10, 20, 3) size = np.ones(3) + np.random.random(3) coords = grid_filters.cell_coord0(grid, size).reshape(-1, 3) np.random.shuffle(coords) geom_1 = Geom.from_Voronoi_tessellation(grid, size, coords) coords, material = seeds.from_geom(geom_1, average=average, periodic=periodic) geom_2 = Geom.from_Voronoi_tessellation(grid, size, coords, material) assert (geom_2.material == geom_1.material).all()
def test_curl(self,field_def,curl_def): size = np.random.random(3)+1.0 grid = np.random.randint(8,32,(3)) nodes = grid_filters.cell_coord0(grid,size) my_locals = locals() # needed for list comprehension field = np.stack([np.broadcast_to(eval(f,globals(),my_locals),grid) for f in field_def],axis=-1) field = field.reshape(tuple(grid) + ((3,3) if len(field_def)==9 else (3,))) curl = np.stack([np.broadcast_to(eval(c,globals(),my_locals),grid) for c in curl_def], axis=-1) curl = curl.reshape(tuple(grid) + ((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.cell_coord0(default.grid, default.size, default.origin) b = default.cell_coordinates.reshape(tuple(default.grid) + (3, ), order='F') elif mode == 'node': a = grid_filters.node_coord0(default.grid, default.size, default.origin) b = default.node_coordinates.reshape(tuple(default.grid + 1) + (3, ), order='F') assert np.allclose(a, b)
def test_compare_reference_rectilinearGrid(self, update, reference_dir, tmp_path): grid = np.array([5, 6, 7], int) size = np.array([.6, 1., .5]) rectilinearGrid = VTK.from_rectilinear_grid(grid, size) c = grid_filters.cell_coord0(grid, size).reshape(-1, 3, order='F') n = grid_filters.node_coord0(grid, size).reshape(-1, 3, order='F') rectilinearGrid.add(c, 'cell') rectilinearGrid.add(n, 'node') if update: rectilinearGrid.save(reference_dir / 'rectilinearGrid') else: reference = VTK.load(reference_dir / 'rectilinearGrid.vtr') assert rectilinearGrid.__repr__() == reference.__repr__() and \ np.allclose(rectilinearGrid.get('cell'),c)
def test_ODF_cell(self, reference_dir, fractions, degrees, N): steps = np.array([144, 36, 36]) limits = np.array([360., 90., 90.]) rng = tuple(zip(np.zeros(3), limits)) weights = Table.load( reference_dir / 'ODF_experimental_cell.txt').get('intensity').flatten() Eulers = grid_filters.cell_coord0(steps, limits) Eulers = np.radians(Eulers) if not degrees else Eulers Eulers_r = Rotation.from_ODF(weights, Eulers.reshape(-1, 3, order='F'), N, degrees, fractions).as_Eulers(True) weights_r = np.histogramdd( Eulers_r, steps, rng)[0].flatten(order='F') / N * np.sum(weights) if fractions: assert np.sqrt(((weights_r - weights)**2).mean()) < 4
def test_cell_coord0(self): size = np.random.random(3) grid = np.random.randint(8,32,(3)) coord = grid_filters.cell_coord0(grid,size) assert np.allclose(coord[0,0,0],size/grid*.5) and coord.shape == tuple(grid) + (3,)
def test_coord0(self): size = np.random.random(3) grid = np.random.randint(8,32,(3)) c = grid_filters.cell_coord0(grid+1,size+size/grid) n = grid_filters.node_coord0(grid,size) + size/grid*.5 assert np.allclose(c,n)