Exemple #1
0
def calculate_region_rectangle(x, y, corner0, corner1):
    # Return boolean array of same shape as x and y.
    xy = np.stack((x.ravel(), y.ravel()), axis=1)  # Shape (npoints, 2)
    rectangle = Rectangle(corner0,
                          width=corner1[0] - corner0[0],
                          height=corner1[1] - corner0[1])
    region = rectangle.contains_points(xy)  # Shape (2*npoints)
    region.shape = x.shape  # Shape (npoints, 2)
    return region
def rotate_xyz(file,nx,nz,Nx=40,Nz=5,name='',opt='',**kwargs):
    '''Generates .xyz files corresponding to individual rotation
    - file : path to cif file (.xyz files will be put in the same folder )
    - Nrot : number of rotation to perform within 0,pi/2
    '''
    crys = file
    if isinstance(file,str):
        crys = Crystal.from_cif(file)
        if not name : name = file.replace('.cif','')

    a1,a2,a3    = np.array(crys.lattice_vectors)
    ax1,by2,cz3 = np.array(crys.lattice_parameters[:3])
    nxz   = int(cz3/ax1)
    #int(nx.max()+nxz*nz.max()), int(nx.max()/nxz+nz.max())
    xy0   = (0,(Nz-nz.max())*cz3)
    print('super cell calculation ...')
    crys0   = crys.supercell(Nx,1,Nz)
    pattern = np.array([
        [a.atomic_number]+list(a.coords_cartesian)+[a.occupancy,wobbles[a.atomic_number]] for a in crys0.atoms])
    coords0 = np.array([a.coords_cartesian for a in crys0.atoms])

    files = []
    Cs = [cs[E] for E in pattern[:,0]]
    for i1,i2 in zip(nx,nz):
        # i1,i2 = i,Nrot-i; n0 = gcd(i1,i2)
        # i1,i2 = i1/n0,i2/n0;#print(i1,i2)
        cz = i1*a1+i2*a3; #print(cz)
        ax = np.array([cz[1],-cz[0]]); #print(cz)
        c,angle = np.linalg.norm(cz),np.arctan(cz[0]/cz[2])
        if not i1%i2*nxz:
            n0 = i1/gcd(i1,i2*nxz)
            if not n0%2 : n0/=2
            a = np.linalg.norm([i2*nxz/i1,1])*cz3*n0
        else:
            a = np.linalg.norm([i2*nxz,i1])*cz3
        # a = np.linalg.norm([i2*nxz,i1])*cz3

        print(green+'i1=%d,i2=%d,angle=%.2f' %(i1,i2,angle*180/np.pi)+black)
        rect=Rectangle(xy0,1.0*a,1.0*c,angle=-angle*180/np.pi,edgecolor='k',linewidth=2,alpha=0.1)
        print('finding points ...')
        idc = rect.contains_points(coords0[:,[0,2]],radius=0.001)
        print('done')
        n   = (i1,0,i2)
        xyz = name+'%d%d%d.xyz' %n
        if opt:
            path = dsp.dirname(name)

            x,z = coords0[:,[0,2]].T;
            x0,z0 = (coords0[idc,:])[:,[0,2]].T
            X,Z = np.hstack([x,x0]),np.hstack([z,z0])
            S = np.array([10]*x.size+[30]*x0.size)
            C = Cs+list(np.array(Cs)[idc,:])
            dsp.stddisp(scat=[X,Z,S,C],patches=[rect],equal=1,figsize='12',
                xyTicks=[ax1,cz3],pOpt='tGeX',xylims=[0,40*ax1,-10*cz3,10*cz3],
                opt=opt,name=path+'figures/'+dsp.basename(xyz).replace('xyz','png'))
            plt.show()

        lat_vec = [a,a2[1],c]#np.array([Nrot*(a3+a1),a2,Nrot*(a3-a1)])#
        cc = coords0[idc,:]
        cc[:,2]-=xy0[1]
        coords  = Rot(-angle).dot(cc.T).T
        pattern[idc,1:4] = coords
        # make_xyz(xyz,pattern[idc,:],np.diag(lat_vec),n,fmt='%.4f',dopt='s')
        files+=[xyz] #dsp.basename(xyz)]
    return files