def test_scale(self,default,update,ref_path,cells): modified = default.scale(cells) tag = f'grid_{util.srepr(cells,"-")}' reference = ref_path/f'scale_{tag}.vtr' if update: modified.save(reference) assert grid_equal(Grid.load(reference), modified)
def test_flip(self,default,update,ref_path,directions): modified = default.flip(directions) tag = f'directions_{"-".join(directions)}' reference = ref_path/f'flip_{tag}.vtr' if update: modified.save(reference) assert grid_equal(Grid.load(reference), modified)
def test_minimal_surface_volume(self, surface, threshold): cells = np.ones(3, dtype=int) * 64 grid = Grid.from_minimal_surface(cells, np.ones(3), surface, threshold) assert np.isclose(np.count_nonzero(grid.material == 1) / np.prod(grid.cells), .5, rtol=1e-3)
def test_mirror(self,default,update,ref_path,directions,reflect): modified = default.mirror(directions,reflect) tag = f'directions_{"-".join(directions)}+reflect_{reflect}' reference = ref_path/f'mirror_{tag}.vtr' if update: modified.save(reference) assert grid_equal(Grid.load(reference), modified)
def test_rotate(self,default,update,ref_path,Eulers): modified = default.rotate(Rotation.from_Euler_angles(Eulers,degrees=True)) tag = f'Eulers_{util.srepr(Eulers,"-")}' reference = ref_path/f'rotate_{tag}.vtr' if update: modified.save(reference) assert grid_equal(Grid.load(reference), modified)
def default(): """Simple geometry.""" x=np.concatenate((np.ones(40,dtype=int), np.arange(2,42), np.ones(40,dtype=int)*2, np.arange(1,41))).reshape(8,5,4,order='F') return Grid(x,[8e-6,5e-6,4e-6])
def test_clean(self,default,update,ref_path,stencil,selection,periodic): current = default.clean(stencil,selection,periodic) reference = ref_path/f'clean_{stencil}_{"+".join(map(str,[None] if selection is None else selection))}_{periodic}' if update and stencil > 1: current.save(reference) assert grid_equal(Grid.load(reference) if stencil > 1 else default, current )
def test_minimal_surface_basic_properties(self,surface): cells = np.random.randint(60,100,3) size = np.ones(3)+np.random.rand(3) threshold = 2*np.random.rand()-1. periods = np.random.randint(2)+1 materials = np.random.randint(0,40,2) grid = Grid.from_minimal_surface(cells,size,surface,threshold,periods,materials) assert set(grid.material.flatten()) | set(materials) == set(materials) \ and (grid.size == size).all() and (grid.cells == cells).all()
def test_regrid_double_cells(self): size = np.random.random(3) # noqa cells = np.random.randint(8, 32, (3)) g = Grid.from_Voronoi_tessellation(cells, size, seeds.from_random(size, 10)) F = np.broadcast_to(np.eye(3), tuple(cells) + (3, 3)) assert all( g.scale(cells * 2).material.flatten() == g.material.flatten()[ grid_filters.regrid(size, F, cells * 2)])
def test_load_DREAM3D(self,ref_path,cell_ensemble_data): grain_c = ConfigMaterial.load_DREAM3D(ref_path/'2phase_irregularGrid.dream3d','Grain Data', cell_ensemble_data = cell_ensemble_data) point_c = ConfigMaterial.load_DREAM3D(ref_path/'2phase_irregularGrid.dream3d', cell_ensemble_data = cell_ensemble_data) assert point_c.is_valid and grain_c.is_valid and \ len(point_c['material'])+1 == len(grain_c['material']) grain_m = Grid.load_DREAM3D(ref_path/'2phase_irregularGrid.dream3d','FeatureIds').material.flatten() point_m = Grid.load_DREAM3D(ref_path/'2phase_irregularGrid.dream3d').material.flatten() for i in np.unique(point_m): j = int(grain_m[(point_m==i).nonzero()[0][0]]) assert np.allclose(point_c['material'][i]['constituents'][0]['O'], grain_c['material'][j]['constituents'][0]['O']) assert point_c['material'][i]['constituents'][0]['phase'] == \ grain_c['material'][j]['constituents'][0]['phase']
def test_tessellate_bicrystal(self, approach): cells = np.random.randint(5, 10, 3) * 2 size = cells.astype(float) seeds = np.vstack((size * np.array([0.5, 0.25, 0.5]), size * np.array([0.5, 0.75, 0.5]))) material = np.zeros(cells) material[:, cells[1] // 2:, :] = 1 if approach == 'Laguerre': grid = Grid.from_Laguerre_tessellation( cells, size, seeds, np.ones(2), periodic=np.random.random() > 0.5) elif approach == 'Voronoi': grid = Grid.from_Voronoi_tessellation( cells, size, seeds, periodic=np.random.random() > 0.5) assert np.all(grid.material == material)
def test_Laguerre_weights(self): cells = np.random.randint(10,20,3) size = np.random.random(3) + 1.0 N_seeds= np.random.randint(10,30) seeds = np.random.rand(N_seeds,3) * np.broadcast_to(size,(N_seeds,3)) weights= np.full((N_seeds),-np.inf) ms = np.random.randint(N_seeds) weights[ms] = np.random.random() Laguerre = Grid.from_Laguerre_tessellation(cells,size,seeds,weights,periodic=np.random.random()>0.5) assert np.all(Laguerre.material == ms)
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_add_primitive_rotation(self, center, inverse, periodic): """Rotation should not change result for sphere.""" g = np.random.randint(8, 32, (3)) s = np.random.random(3) + .5 fill = np.random.randint(10) + 2 G_1 = Grid(np.ones(g, 'i'), s).add_primitive(.3, center, 1, fill, inverse=inverse, periodic=periodic) G_2 = Grid(np.ones(g, 'i'), s).add_primitive(.3, center, 1, fill, Rotation.from_random(), inverse, periodic=periodic) assert grid_equal(G_1, G_2)
def visualise_volume_element(volume_element): try: from damask import Geom geom_obj = Geom.from_file('geom.geom') geom_obj.to_vtr('geom.vtr') except ImportError: from damask import Grid grid_obj = Grid.load_ASCII('geom.geom') grid_obj.save('geom.vtr')
def test_get_grain_boundaries(self, update, ref_path, periodic, direction): grid = Grid.load(ref_path / 'get_grain_boundaries_8g12x15x20.vti') current = grid.get_grain_boundaries(periodic, direction) if update: current.save( ref_path / f'get_grain_boundaries_8g12x15x20_{direction}_{periodic}.vtu', parallel=False) reference = VTK.load( ref_path / f'get_grain_boundaries_8g12x15x20_{"".join(direction)}_{periodic}.vtu' ) assert current.__repr__() == reference.__repr__()
def test_from_grid_selection(self, periodic, average, invert): cells = np.random.randint(10, 20, 3) N_seeds = np.random.randint(30, 300) size = np.ones(3) + np.random.random(3) coords = seeds.from_random(size, N_seeds, cells) grid = Grid.from_Voronoi_tessellation(cells, size, coords) selection = np.random.randint(N_seeds) + 1 coords, material = seeds.from_grid(grid, average=average, periodic=periodic, invert=invert, selection=[selection]) assert selection not in material if invert else (selection == material).all()
def test_vicinity_offset(self,trigger): offset = np.random.randint(2,4) vicinity = np.random.randint(2,4) g = np.random.randint(28,40,(3)) m = np.ones(g,'i') x = (g*np.random.permutation(np.array([.5,1,1]))).astype('i') m[slice(0,x[0]),slice(0,x[1]),slice(0,x[2])] = 2 m2 = m.copy() for i in [0,1,2]: m2[(np.roll(m,+vicinity,i)-m)!=0] += offset m2[(np.roll(m,-vicinity,i)-m)!=0] += offset if len(trigger) > 0: m2[m==1] = 1 grid = Grid(m,np.random.rand(3)).vicinity_offset(vicinity,offset,trigger=trigger) assert np.all(m2==grid.material)
def test_invalid_materials_shape(self,default): material = np.ones((3,3)) with pytest.raises(ValueError): Grid(material, size=np.ones(3))
def test_invalid_origin(self,default): with pytest.raises(ValueError): Grid(default.material[1:,1:,1:], size=np.ones(3), origin=np.ones(4))
def test_save_load_ASCII(self,default,tmp_path): default.save_ASCII(tmp_path/'ASCII') default.material -= 1 assert grid_equal(Grid.load_ASCII(tmp_path/'ASCII'),default)
def test_invalid_size(self,default): with pytest.raises(ValueError): Grid(default.material[1:,1:,1:], size=np.ones(2))
def test_cast_to_int(self): g = Grid(np.zeros((3,3,3)),np.ones(3)) assert g.material.dtype in np.sctypes['int']
def test_invalid_material_type(self): with pytest.raises(TypeError): Grid(np.zeros((3,3,3),dtype='complex'),np.ones(3))
def test_invalid_no_material(self,tmp_path): v = VTK.from_rectilinear_grid(np.random.randint(5,10,3)*2,np.random.random(3) + 1.0) v.save(tmp_path/'no_materialpoint.vtr',parallel=False) with pytest.raises(ValueError): Grid.load(tmp_path/'no_materialpoint.vtr')
def test_read_write_vtr(self,default,tmp_path): default.save(tmp_path/'default') new = Grid.load(tmp_path/'default.vtr') assert grid_equal(new,default)
def test_invalid_materials_type(self,default): material = np.random.randint(1,300,(3,4,5))==1 with pytest.raises(TypeError): Grid(material)
def test_canvas_cells(self, default): g = Grid(np.zeros(np.random.randint(3, 30, (3)), int), np.ones(3)) cells = np.random.randint(1, 30, (3)) offset = np.random.randint(-30, 30, (3)) assert np.all(g.canvas(cells, offset).cells == cells)
def test_sort(self): cells = np.random.randint(5,20,3) m = Grid(np.random.randint(1,20,cells)*3,np.ones(3)).sort().material.flatten(order='F') for i,v in enumerate(m): assert i==0 or v > m[:i].max() or v in m[:i]