def test_direc_and_neg_dims(): for dims in [(2, 3, 4), (-2, -3, -4), (-2, 3, 4), (2, -3, 4), (2, 3, -4)]: m1 = crys.scell_mask(*dims, direc=1)[::-1, :] m2 = crys.scell_mask(*dims, direc=-1) assert (m1 == m2).all() for direc in [1, -1]: ref = crys.scell_mask(2, 3, 4, direc=direc) assert ((ref + crys.scell_mask(-2, -3, -4, direc=direc)) == 0.0).all() now = crys.scell_mask(-2, 3, 4, direc=direc) assert ((ref[:, 0] + now[:, 0]) == 0.0).all() assert (ref[:, 1:] == now[:, 1:]).all() now = crys.scell_mask(2, -3, 4, direc=direc) assert ((ref[:, 1] + now[:, 1]) == 0.0).all() assert (ref[:, (0, 2)] == now[:, (0, 2)]).all() now = crys.scell_mask(2, 3, -4, direc=direc) assert ((ref[:, 2] + now[:, 2]) == 0.0).all() assert (ref[:, :2] == now[:, :2]).all() natoms = 20 coords_frac = rand(natoms, 3) cell = rand(3, 3) dims = (2, 3, 4) symbols = [next(syms) for ii in range(natoms)] struct = Structure(coords_frac=coords_frac, cell=cell, symbols=symbols) # coords are offset by a constant shift if all dims and direc are < 0 s1 = crys.scell(struct, (2, 3, 4), direc=1) s2 = crys.scell(struct, (-2, -3, -4), direc=-1) d = s1.coords - s2.coords assert np.abs(d - d[0, :][None, :]).sum() < 1e-12
def test_direc_and_neg_dims(): for dims in [(2,3,4), (-2,-3,-4), (-2,3,4), (2,-3,4), (2,3,-4)]: m1 = crys.scell_mask(*dims, direc=1)[::-1,:] m2 = crys.scell_mask(*dims, direc=-1) assert (m1==m2).all() for direc in [1,-1]: ref = crys.scell_mask( 2, 3, 4, direc=direc) assert ((ref + crys.scell_mask(-2,-3,-4, direc=direc)) == 0.0).all() now = crys.scell_mask(-2, 3, 4, direc=direc) assert ((ref[:,0] + now[:,0]) == 0.0).all() assert (ref[:,1:] == now[:,1:]).all() now = crys.scell_mask(2, -3, 4, direc=direc) assert ((ref[:,1] + now[:,1]) == 0.0).all() assert (ref[:,(0,2)] == now[:,(0,2)]).all() now = crys.scell_mask(2, 3, -4, direc=direc) assert ((ref[:,2] + now[:,2]) == 0.0).all() assert (ref[:,:2] == now[:,:2]).all() natoms = 20 coords_frac = rand(natoms,3) cell = rand(3,3) dims = (2,3,4) symbols = [syms.next() for ii in range(natoms)] struct = Structure(coords_frac=coords_frac, cell=cell, symbols=symbols) # coords are offset by a constant shift if all dims and direc are < 0 s1 = crys.scell(struct, (2,3,4), direc=1) s2 = crys.scell(struct, (-2,-3,-4), direc=-1) d = s1.coords - s2.coords assert np.abs(d - d[0,:][None,:]).sum() < 1e-12
def test_symmetry(): tools.skip_if_pkg_missing('pyspglib') st_prim = crys.Structure(coords_frac=np.array([[0] * 3, [.5] * 3]), cryst_const=np.array([3.5] * 3 + [60] * 3), symbols=['Al', 'N']) st_sc = crys.scell(st_prim, (2, 3, 4)) st_prim2 = symmetry.spglib_get_primitive(st_sc, symprec=1e-2) # irreducible structs assert symmetry.spglib_get_primitive(st_prim, symprec=1e-2) is None assert symmetry.spglib_get_primitive(st_prim2, symprec=1e-2) is None for st in [st_prim, st_sc, st_prim2]: assert symmetry.spglib_get_spacegroup(st_prim, symprec=1e-2) == (225, 'Fm-3m') # this is redundant since we have is_same_struct(), but keep it anyway tools.assert_dict_with_all_types_almost_equal( st_prim.__dict__, st_prim2.__dict__, keys=['natoms', 'symbols', 'volume', 'cryst_const']) assert symmetry.is_same_struct(st_prim, st_prim2)
def test_angle(): # CaCl struct, the supercell will have 0 and 180 degrees -> check corner # cases st = io.read_cif('files/angle/rs.cif') st = crys.scell(st, (2,1,1)) nang = st.natoms*(st.natoms-1)*(st.natoms-2) mask_val = 999.0 for deg in [True,False]: for pbc in [True,False]: agf = crys.angles(st, pbc=pbc, mask_val=mask_val) agpy, aipy = angles(st, pbc=pbc, mask_val=mask_val) eps = np.finfo(float).eps*5 assert np.allclose(agf, agpy) assert aipy.shape[0] == nang assert len((agf != mask_val).nonzero()[0]) == nang angleidx = np.array(zip(*(agf != mask_val).nonzero())) assert (angleidx == aipy).all() assert not np.isnan(agpy).any(), "python angle nan" assert not np.isnan(agf).any(), "fortran angle nan" # do we have 0 and 180 degrees? assert (agf < eps).any(), "no zero degree cases" assert (agf - 180.0 < eps).any(), "no 180 degree cases" assert (agf >= 0.0).all(), "negative angles"
def test_angle(): # CaCl struct, the supercell will have 0 and 180 degrees -> check corner # cases tools.skip_if_pkg_missing('CifFile') st = io.read_cif('files/angle/rs.cif') st = crys.scell(st, (2, 1, 1)) nang = st.natoms * (st.natoms - 1) * (st.natoms - 2) mask_val = 999.0 for deg in [True, False]: for pbc in [True, False]: agf = crys.angles(st, pbc=pbc, mask_val=mask_val) agpy, aipy = angles(st, pbc=pbc, mask_val=mask_val) eps = np.finfo(float).eps * 5 assert np.allclose(agf, agpy) assert aipy.shape[0] == nang assert len((agf != mask_val).nonzero()[0]) == nang angleidx = np.array(list(zip(*(agf != mask_val).nonzero()))) assert (angleidx == aipy).all() assert not np.isnan(agpy).any(), "python angle nan" assert not np.isnan(agf).any(), "fortran angle nan" # do we have 0 and 180 degrees? assert (agf < eps).any(), "no zero degree cases" assert (agf - 180.0 < eps).any(), "no 180 degree cases" assert (agf >= 0.0).all(), "negative angles"
run 1000 """ assert len(sys.argv) == 2, "need one input arg: nvt or npt" if sys.argv[1] == 'npt': ens_txt = npt_txt elif sys.argv[1] == 'nvt': ens_txt = nvt_txt else: raise StandardError("only nvt / npt allowed") # create structure file st = crys.Structure(coords_frac=np.array([[0.0]*3, [.5]*3]), cryst_const=np.array([2.85]*3 + [60]*3), symbols=['Al','N']) io.write_lammps('lmp.struct', crys.scell(st,(3,3,3))) # write lmp.in for nvt or npt common.file_write('lmp.in', lmp_in_templ.format(ensemble=ens_txt)) # run lammps common.system("mpirun -np 2 lammps < lmp.in", wait=True) # read trajectory trtxt_orig = io.read_lammps_md_txt('log.lammps') trdcd = io.read_lammps_md_dcd('log.lammps') # plotting plots = mpl.prepare_plots(['coords', 'coords_frac', 'velocity', 'cryst_const', 'cell']) for name,pl in plots.iteritems():
run 1000 """ assert len(sys.argv) == 2, "need one input arg: nvt or npt" if sys.argv[1] == 'npt': ens_txt = npt_txt elif sys.argv[1] == 'nvt': ens_txt = nvt_txt else: raise Exception("only nvt / npt allowed") # create structure file st = crys.Structure(coords_frac=np.array([[0.0]*3, [.5]*3]), cryst_const=np.array([2.85]*3 + [60]*3), symbols=['Al','N']) io.write_lammps('lmp.struct', crys.scell(st,(3,3,3))) # write lmp.in for nvt or npt common.file_write('lmp.in', lmp_in_templ.format(ensemble=ens_txt)) # run lammps common.system("mpirun -np 2 lammps < lmp.in", wait=True) # read trajectory trtxt_orig = io.read_lammps_md_txt('log.lammps') trdcd = io.read_lammps_md_dcd('log.lammps') # plotting plots = mpl.prepare_plots(['coords', 'coords_frac', 'velocity', 'cryst_const', 'cell']) for name,pl in plots.items():
np.array([[0.0, 0.0, 0.0], [0.5, 0.0, 0.0], [0.0, 0.5, 0.0], [0.5, 0.5, 0.0], [0.0, 0.0, 0.5], [0.5, 0.0, 0.5], [0.0, 0.5, 0.5], [0.5, 0.5, 0.5], ]) symbols = ['Al', 'N', 'N', 'Al', 'N', 'Al', 'Al', 'N'] alat = 5.0 cell = np.identity(3) * alat struct = crys.Structure(coords_frac=coords_frac, symbols=symbols, cell=cell) sc = crys.scell(struct, (2,2,2)) name = 'aln_ibrav0_sc' structs[name] = Struct(coords_frac=sc.coords_frac, cell=sc.cell, symbols=sc.symbols, fnbase=name, tgtdir=tgtdir) print(structs[name].cryst_const) ## structs[name].write_cif() ## structs[name].write_axsf() ## structs[name].savetxt() # AlN ibrav=2 # ----------- #
def test_scell(): cell = np.identity(3) coords_frac = np.array([[0.5, 0.5, 0.5], [1,1,1]]) symbols = ['Al', 'N'] sc = crys.scell(Structure(coords_frac=coords_frac, cell=cell, symbols=symbols), (2,2,2)) sc_coords_frac = \ np.array([[ 0.25, 0.25, 0.25], [ 0.25, 0.25, 0.75], [ 0.25, 0.75, 0.25], [ 0.25, 0.75, 0.75], [ 0.75, 0.25, 0.25], [ 0.75, 0.25, 0.75], [ 0.75, 0.75, 0.25], [ 0.75, 0.75, 0.75], [ 0.5 , 0.5 , 0.5 ], [ 0.5 , 0.5 , 1. ], [ 0.5 , 1. , 0.5 ], [ 0.5 , 1. , 1. ], [ 1. , 0.5 , 0.5 ], [ 1. , 0.5 , 1. ], [ 1. , 1. , 0.5 ], [ 1. , 1. , 1. ]]) sc_symbols = ['Al']*8 + ['N']*8 sc_cell = \ np.array([[ 2., 0., 0.], [ 0., 2., 0.], [ 0., 0., 2.]]) assert sc.symbols == sc_symbols np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac) np.testing.assert_array_almost_equal(sc.cell, sc_cell) # non-orthorhombic cell cell = \ np.array([[ 1., 0.5, 0.5], [ 0.25, 1., 0.2], [ 0.2, 0.5, 1.]]) sc = crys.scell(Structure(coords_frac=coords_frac, cell=cell, symbols=symbols), (2,2,2)) sc_cell = \ np.array([[ 2. , 1. , 1. ], [ 0.5, 2. , 0.4], [ 0.4, 1. , 2. ]]) np.testing.assert_array_almost_equal(sc.cell, sc_cell) # crystal coords are cell-independent np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac) # slab # # Test if old and new implementation behave for a tricky case: natoms == 2 # mask.shape[0], i.e. if reshape() behaves correctly. # Reference generated with old implementation. Default is new. cell = np.identity(3) coords_frac = np.array([[0.5, 0.5, 0.5], [1,1,1]]) symbols = ['Al', 'N'] sc = crys.scell(Structure(coords_frac=coords_frac, cell=cell, symbols=symbols), (1,1,2)) sc_coords_frac = \ np.array([[ 0.5 , 0.5 , 0.25], [ 0.5 , 0.5 , 0.75], [ 1. , 1. , 0.5 ], [ 1. , 1. , 1. ]]) sc_cell = \ np.array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 2.]]) sc_symbols = ['Al', 'Al', 'N', 'N'] assert sc.symbols == sc_symbols np.testing.assert_array_almost_equal(sc.cell, sc_cell) np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac) sc = crys.scell(Structure(coords_frac=coords_frac, cell=cell, symbols=symbols), (1,2,1)) sc_coords_frac = \ np.array([[ 0.5 , 0.25, 0.5 ], [ 0.5 , 0.75, 0.5 ], [ 1. , 0.5 , 1. ], [ 1. , 1. , 1. ]]) sc_cell = \ np.array([[ 1., 0., 0.], [ 0., 2., 0.], [ 0., 0., 1.]]) assert sc.symbols == sc_symbols np.testing.assert_array_almost_equal(sc.cell, sc_cell) np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac) sc = crys.scell(Structure(coords_frac=coords_frac, cell=cell, symbols=symbols), (2,1,1)) sc_coords_frac = \ np.array([[ 0.25, 0.5 , 0.5 ], [ 0.75, 0.5 , 0.5 ], [ 0.5 , 1. , 1. ], [ 1. , 1. , 1. ]]) sc_cell = \ np.array([[ 2., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]]) assert sc.symbols == sc_symbols np.testing.assert_array_almost_equal(sc.cell, sc_cell) np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac) # symbols = None sc = crys.scell(Structure(coords_frac=coords_frac, cell=cell, symbols=None), (2,2,2)) assert sc.symbols is None # Trajectory natoms = 4 nstep = 100 symbols = [syms.next() for ii in range(natoms)] # cell 2d coords_frac = rand(nstep,natoms,3) cell = rand(3,3) dims = (2,3,4) nmask = np.prod(dims) sc = crys.scell(Trajectory(coords_frac=coords_frac, cell=cell, symbols=symbols), dims=dims) assert sc.coords_frac.shape == (nstep, nmask*natoms, 3) assert sc.symbols == common.flatten([[sym]*nmask for sym in symbols]) assert sc.cell.shape == (nstep,3,3) np.testing.assert_array_almost_equal(sc.cell, num.extend_array(cell * np.asarray(dims)[:,None], sc.nstep, axis=0)) # cell 3d cell = rand(nstep,3,3) sc = crys.scell(Trajectory(coords_frac=coords_frac, cell=cell, symbols=symbols), dims=dims) assert sc.coords_frac.shape == (nstep, nmask*natoms, 3) coords_frac2 = np.array([crys.scell(Structure(coords_frac=coords_frac[ii,...,], cell=cell[ii,...], symbols=symbols), dims=dims).coords_frac \ for ii in range(nstep)]) np.testing.assert_array_almost_equal(sc.coords_frac, coords_frac2) assert sc.symbols == common.flatten([[sym]*nmask for sym in symbols]) assert sc.cell.shape == (nstep,3,3) np.testing.assert_array_almost_equal(sc.cell, cell * np.asarray(dims)[None,:,None]) # methods natoms = 20 coords_frac = rand(natoms,3) cell = rand(3,3) dims = (2,3,4) symbols = [syms.next() for ii in range(natoms)] struct = Structure(coords_frac=coords_frac, cell=cell, symbols=symbols) sc1 = crys.scell(struct, dims=dims, method=1) sc2 = crys.scell(struct, dims=dims, method=2) d1 = dict([(key, getattr(sc1, key)) for key in sc1.attr_lst]) d2 = dict([(key, getattr(sc2, key)) for key in sc2.attr_lst]) tools.assert_dict_with_all_types_almost_equal(d1, d2)
pair_coeff * * ../AlN.tersoff Al N ### IO dump dump_txt all custom 1 lmp.out.dump id type xu yu zu fx fy fz & vx vy vz xsu ysu zsu dump_modify dump_txt sort id dump dump_dcd all dcd 1 lmp.out.dcd dump_modify dump_dcd sort id unwrap yes thermo_style custom step temp vol cella cellb cellc cellalpha cellbeta cellgamma & ke pe etotal & press pxx pyy pzz pxy pxz pyz cpu press thermo_modify flush yes thermo 1 fix 1 all box/relax tri 0.0 minimize 1e-8 1e-8 5000 10000 """ st = crys.Structure(coords_frac=np.array([[0.0]*3, [.5]*3]), cryst_const=np.array([2.85]*3 + [60]*3), symbols=['Al','N']) for dr in ['md-nvt', 'md-npt', 'vc-relax']: common.system("rm -rfv {dr}; mkdir -v {dr}".format(dr=dr)) io.write_lammps('vc-relax/lmp.struct', st) io.write_lammps('md-nvt/lmp.struct', crys.scell(st,(2,2,2))) io.write_lammps('md-npt/lmp.struct', crys.scell(st,(2,2,2))) for dr,txt in lmp_in.iteritems(): common.file_write('%s/lmp.in' %dr, txt)
def test_scell(): cell = np.identity(3) coords_frac = np.array([[0.5, 0.5, 0.5], [1, 1, 1]]) symbols = ['Al', 'N'] sc = crys.scell( Structure(coords_frac=coords_frac, cell=cell, symbols=symbols), (2, 2, 2)) sc_coords_frac = \ np.array([[ 0.25, 0.25, 0.25], [ 0.25, 0.25, 0.75], [ 0.25, 0.75, 0.25], [ 0.25, 0.75, 0.75], [ 0.75, 0.25, 0.25], [ 0.75, 0.25, 0.75], [ 0.75, 0.75, 0.25], [ 0.75, 0.75, 0.75], [ 0.5 , 0.5 , 0.5 ], [ 0.5 , 0.5 , 1. ], [ 0.5 , 1. , 0.5 ], [ 0.5 , 1. , 1. ], [ 1. , 0.5 , 0.5 ], [ 1. , 0.5 , 1. ], [ 1. , 1. , 0.5 ], [ 1. , 1. , 1. ]]) sc_symbols = ['Al'] * 8 + ['N'] * 8 sc_cell = \ np.array([[ 2., 0., 0.], [ 0., 2., 0.], [ 0., 0., 2.]]) assert sc.symbols == sc_symbols np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac) np.testing.assert_array_almost_equal(sc.cell, sc_cell) # non-orthorhombic cell cell = \ np.array([[ 1., 0.5, 0.5], [ 0.25, 1., 0.2], [ 0.2, 0.5, 1.]]) sc = crys.scell( Structure(coords_frac=coords_frac, cell=cell, symbols=symbols), (2, 2, 2)) sc_cell = \ np.array([[ 2. , 1. , 1. ], [ 0.5, 2. , 0.4], [ 0.4, 1. , 2. ]]) np.testing.assert_array_almost_equal(sc.cell, sc_cell) # crystal coords are cell-independent np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac) # slab # # Test if old and new implementation behave for a tricky case: natoms == 2 # mask.shape[0], i.e. if reshape() behaves correctly. # Reference generated with old implementation. Default is new. cell = np.identity(3) coords_frac = np.array([[0.5, 0.5, 0.5], [1, 1, 1]]) symbols = ['Al', 'N'] sc = crys.scell( Structure(coords_frac=coords_frac, cell=cell, symbols=symbols), (1, 1, 2)) sc_coords_frac = \ np.array([[ 0.5 , 0.5 , 0.25], [ 0.5 , 0.5 , 0.75], [ 1. , 1. , 0.5 ], [ 1. , 1. , 1. ]]) sc_cell = \ np.array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 2.]]) sc_symbols = ['Al', 'Al', 'N', 'N'] assert sc.symbols == sc_symbols np.testing.assert_array_almost_equal(sc.cell, sc_cell) np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac) sc = crys.scell( Structure(coords_frac=coords_frac, cell=cell, symbols=symbols), (1, 2, 1)) sc_coords_frac = \ np.array([[ 0.5 , 0.25, 0.5 ], [ 0.5 , 0.75, 0.5 ], [ 1. , 0.5 , 1. ], [ 1. , 1. , 1. ]]) sc_cell = \ np.array([[ 1., 0., 0.], [ 0., 2., 0.], [ 0., 0., 1.]]) assert sc.symbols == sc_symbols np.testing.assert_array_almost_equal(sc.cell, sc_cell) np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac) sc = crys.scell( Structure(coords_frac=coords_frac, cell=cell, symbols=symbols), (2, 1, 1)) sc_coords_frac = \ np.array([[ 0.25, 0.5 , 0.5 ], [ 0.75, 0.5 , 0.5 ], [ 0.5 , 1. , 1. ], [ 1. , 1. , 1. ]]) sc_cell = \ np.array([[ 2., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]]) assert sc.symbols == sc_symbols np.testing.assert_array_almost_equal(sc.cell, sc_cell) np.testing.assert_array_almost_equal(sc.coords_frac, sc_coords_frac) # symbols = None sc = crys.scell( Structure(coords_frac=coords_frac, cell=cell, symbols=None), (2, 2, 2)) assert sc.symbols is None # Trajectory natoms = 4 nstep = 100 symbols = [next(syms) for ii in range(natoms)] # cell 2d coords_frac = rand(nstep, natoms, 3) cell = rand(3, 3) dims = (2, 3, 4) nmask = np.prod(dims) sc = crys.scell(Trajectory(coords_frac=coords_frac, cell=cell, symbols=symbols), dims=dims) assert sc.coords_frac.shape == (nstep, nmask * natoms, 3) assert sc.symbols == common.flatten([[sym] * nmask for sym in symbols]) assert sc.cell.shape == (nstep, 3, 3) np.testing.assert_array_almost_equal( sc.cell, num.extend_array(cell * np.asarray(dims)[:, None], sc.nstep, axis=0)) # cell 3d cell = rand(nstep, 3, 3) sc = crys.scell(Trajectory(coords_frac=coords_frac, cell=cell, symbols=symbols), dims=dims) assert sc.coords_frac.shape == (nstep, nmask * natoms, 3) coords_frac2 = np.array([crys.scell(Structure(coords_frac=coords_frac[ii,...,], cell=cell[ii,...], symbols=symbols), dims=dims).coords_frac \ for ii in range(nstep)]) np.testing.assert_array_almost_equal(sc.coords_frac, coords_frac2) assert sc.symbols == common.flatten([[sym] * nmask for sym in symbols]) assert sc.cell.shape == (nstep, 3, 3) np.testing.assert_array_almost_equal( sc.cell, cell * np.asarray(dims)[None, :, None]) # methods natoms = 20 coords_frac = rand(natoms, 3) cell = rand(3, 3) dims = (2, 3, 4) symbols = [next(syms) for ii in range(natoms)] struct = Structure(coords_frac=coords_frac, cell=cell, symbols=symbols) sc1 = crys.scell(struct, dims=dims, method=1) sc2 = crys.scell(struct, dims=dims, method=2) d1 = dict([(key, getattr(sc1, key)) for key in sc1.attr_lst]) d2 = dict([(key, getattr(sc2, key)) for key in sc2.attr_lst]) tools.assert_dict_with_all_types_almost_equal(d1, d2)
np.array([[0.0, 0.0, 0.0], [0.5, 0.0, 0.0], [0.0, 0.5, 0.0], [0.5, 0.5, 0.0], [0.0, 0.0, 0.5], [0.5, 0.0, 0.5], [0.0, 0.5, 0.5], [0.5, 0.5, 0.5], ]) symbols = ['Al', 'N', 'N', 'Al', 'N', 'Al', 'Al', 'N'] alat = 5.0 cell = np.identity(3) * alat struct = crys.Structure(coords_frac=coords_frac, symbols=symbols, cell=cell) sc = crys.scell(struct, (2,2,2)) name = 'aln_ibrav0_sc' structs[name] = Struct(coords_frac=sc.coords_frac, cell=sc.cell, symbols=sc.symbols, fnbase=name, tgtdir=tgtdir) print structs[name].cryst_const ## structs[name].write_cif() ## structs[name].write_axsf() ## structs[name].savetxt() # AlN ibrav=2 # ----------- #