def test_add_primitive_rotation(self,center,inverse,periodic): """Rotation should not change result for sphere (except for discretization errors).""" g = np.array([32,32,32]) fill = np.random.randint(10)+2 eu=np.array([np.random.randint(4),np.random.randint(2),np.random.randint(4)])*.5*np.pi G_1 = Geom(np.ones(g,'i'),[1.,1.,1.]).add_primitive(.3,center,1,fill,inverse=inverse,periodic=periodic) G_2 = Geom(np.ones(g,'i'),[1.,1.,1.]).add_primitive(.3,center,1,fill,Rotation.from_Eulers(eu),inverse,periodic=periodic) assert geom_equal(G_1,G_2)
def test_add_primitive_shift(self,center1,center2,diameter,exponent): """Same volume fraction for periodic geometries and different center.""" o = np.random.random(3)-.5 g = np.random.randint(8,32,(3)) s = np.random.random(3)+.5 G_1 = Geom(np.ones(g,'i'),s,o).add_primitive(diameter,center1,exponent) G_2 = Geom(np.ones(g,'i'),s,o).add_primitive(diameter,center2,exponent) assert np.count_nonzero(G_1.material!=2) == np.count_nonzero(G_2.material!=2)
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_tessellation_approaches(self,periodic): grid = 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)) Voronoi = Geom.from_Voronoi_tessellation( grid,size,seeds, np.arange(N_seeds)+5,periodic) Laguerre = Geom.from_Laguerre_tessellation(grid,size,seeds,np.ones(N_seeds),np.arange(N_seeds)+5,periodic) assert geom_equal(Laguerre,Voronoi)
def test_substitute(self,default): offset = np.random.randint(1,500) modified = Geom(default.material + offset, default.size, default.origin) assert not geom_equal(modified,default) assert geom_equal(default, modified.substitute(np.arange(default.material.max())+1+offset, np.arange(default.material.max())+1))
def test_from_geom_reconstruct(self): grid = 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, grid) geom_1 = Geom.from_Voronoi_tessellation(grid, size, coords) coords, material = seeds.from_geom(geom_1) geom_2 = Geom.from_Voronoi_tessellation(grid, size, coords, material) assert (geom_2.material == geom_1.material).all()
def test_renumber(self,default): material = default.material.copy() for m in np.unique(material): material[material==m] = material.max() + np.random.randint(1,30) default.material -= 1 modified = Geom(material, default.size, default.origin) assert not geom_equal(modified,default) assert geom_equal(default, modified.renumber())
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_tessellate_bicrystal(self,approach): grid = np.random.randint(5,10,3)*2 size = grid.astype(np.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(grid) material[:,grid[1]//2:,:] = 1 if approach == 'Laguerre': geom = Geom.from_Laguerre_tessellation(grid,size,seeds,np.ones(2),periodic=np.random.random()>0.5) elif approach == 'Voronoi': geom = Geom.from_Voronoi_tessellation(grid,size,seeds, periodic=np.random.random()>0.5) assert np.all(geom.material == material)
def test_rotate(self,default,update,reference_dir,Eulers): modified = default.rotate(Rotation.from_Eulers(Eulers,degrees=True)) tag = f'Eulers_{util.srepr(Eulers,"-")}' reference = reference_dir/f'rotate_{tag}.vtr' if update: modified.save(reference) assert geom_equal(Geom.load(reference), modified)
def test_rotate(self,default,update,reference_dir,Eulers): modified = copy.deepcopy(default) modified.rotate(Rotation.from_Eulers(Eulers,degrees=True)) tag = 'Eulers={}-{}-{}'.format(*Eulers) reference = os.path.join(reference_dir,'rotate_{}.geom'.format(tag)) if update: modified.to_file(reference) assert geom_equal(modified,Geom.from_file(reference))
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 Geom(x,[8e-6,5e-6,4e-6])
def test_scale(self,default,update,reference_dir,grid): modified = default.scale(grid) tag = f'grid_{util.srepr(grid,"-")}' reference = reference_dir/f'scale_{tag}.vtr' if update: modified.save(reference) assert geom_equal(Geom.load(reference), modified)
def test_mirror(self,default,update,reference_dir,directions,reflect): modified = copy.deepcopy(default) modified.mirror(directions,reflect) tag = 'directions={}_reflect={}'.format('-'.join(directions),reflect) reference = os.path.join(reference_dir,'mirror_{}.geom'.format(tag)) if update: modified.to_file(reference) assert geom_equal(modified,Geom.from_file(reference))
def test_flip(self,default,update,reference_dir,directions): modified = default.flip(directions) tag = f'directions_{"-".join(directions)}' reference = reference_dir/f'flip_{tag}.vtr' if update: modified.save(reference) assert geom_equal(Geom.load(reference), modified)
def test_mirror(self,default,update,reference_dir,directions,reflect): modified = default.mirror(directions,reflect) tag = f'directions_{"-".join(directions)}+reflect_{reflect}' reference = reference_dir/f'mirror_{tag}.vtr' if update: modified.save(reference) assert geom_equal(Geom.load(reference), modified)
def test_clean(self,default,update,reference_dir,stencil): modified = copy.deepcopy(default) modified.clean(stencil) tag = 'stencil={}'.format(stencil) reference = os.path.join(reference_dir,'clean_{}.geom'.format(tag)) if update: modified.to_file(reference) assert geom_equal(modified,Geom.from_file(reference))
def test_scale(self,default,update,reference_dir,grid): modified = copy.deepcopy(default) modified.scale(grid) tag = 'grid={}'.format('-'.join([str(x) for x in grid])) reference = os.path.join(reference_dir,'scale_{}.geom'.format(tag)) if update: modified.to_file(reference) assert geom_equal(modified,Geom.from_file(reference))
def test_clean(self,default,update,reference_dir,stencil,selection,periodic): current = default.clean(stencil,selection,periodic) reference = reference_dir/f'clean_{stencil}_{"+".join(map(str,[None] if selection is None else selection))}_{periodic}' if update and stencil > 1: current.save(reference) assert geom_equal(Geom.load(reference) if stencil > 1 else default, current )
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_minimal_surface_basic_properties(self,surface): grid = 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) geom = Geom.from_minimal_surface(grid,size,surface,threshold,periods,materials) assert set(geom.material.flatten()) | set(materials) == set(materials) \ and (geom.size == size).all() and (geom.grid == grid).all()
def test_Laguerre_weights(self): grid = 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(1, N_seeds+1) weights[ms-1] = np.random.random() Laguerre = Geom.from_Laguerre_tessellation(grid,size,seeds,weights,np.random.random()>0.5) assert np.all(Laguerre.microstructure == ms)
def test_Laguerre_weights(self): grid = 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 = Geom.from_Laguerre_tessellation(grid,size,seeds,weights,periodic=np.random.random()>0.5) assert np.all(Laguerre.material == ms)
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_from_geom_selection(self, periodic, average, invert): grid = 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, grid) geom = Geom.from_Voronoi_tessellation(grid, size, coords) selection = np.random.randint(N_seeds) + 1 coords, material = seeds.from_geom(geom, 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 geom = Geom(m,np.random.rand(3)).vicinity_offset(vicinity,offset,trigger=trigger) assert np.all(m2==geom.material)
def test_invalid_materials_type(self,default): material = np.random.randint(1,300,(3,4,5))==1 with pytest.raises(TypeError): Geom(material)
def test_invalid_materials_shape(self,default): material = np.ones((3,3)) with pytest.raises(ValueError): Geom(material, size=np.ones(3))
def test_invalid_origin(self,default): with pytest.raises(ValueError): Geom(default.material[1:,1:,1:], size=np.ones(3), origin=np.ones(4))
def test_invalid_size(self,default): with pytest.raises(ValueError): Geom(default.material[1:,1:,1:], size=np.ones(2))