def test_coord_trans(): natoms = 10 cell = np.array([[3, 0, 0], [1.1, 5, -0.04], [-0.33, 1.5, 7]]) cryst_const = crys.cell2cc(cell) coords_frac = rand(natoms, 3) coords = crys.coord_trans(coords=coords_frac, old=cell, new=np.identity(3)) st = crys.Structure(coords=coords, cell=cell) assert np.allclose(coords_frac, st.coords_frac) st = crys.Structure(coords_frac=coords_frac, cell=cell) assert np.allclose(coords, st.coords) st = crys.Structure(coords=coords, cell=cell) assert np.allclose(cryst_const, st.cryst_const)
def test_get_ase_atoms(): natoms = 10 st = crys.Structure(coords_frac=rand(natoms, 3), symbols=['H'] * 10, cell=rand(3, 3)) try: import ase st2 = crys.atoms2struct(crys.struct2atoms(st)) keys = [ 'natoms', 'coords', 'coords_frac', 'symbols', 'cryst_const', 'cell', 'volume', 'mass' ] tools.assert_dict_with_all_types_almost_equal(\ st.__dict__, st2.__dict__, keys=keys, strict=True) # in case the test fails, use this to find out which key failed ## for kk in keys: ## print("testing: %s ..." %kk) ## tools.assert_all_types_almost_equal(st.__dict__[kk], st2.__dict__[kk]) for pbc in [True, False]: at = st.get_ase_atoms(pbc=pbc) assert (at.pbc == np.array([pbc] * 3)).all() at = crys.struct2atoms(st, pbc=pbc) assert (at.pbc == np.array([pbc] * 3)).all() except ImportError: tools.skip("cannot import ase, skipping test get_ase_atoms()")
def test_methods(): tr = crys.Trajectory(coords=rand(100, 10, 3), cell=rand(100, 3, 3), symbols=['H'] * 10) st = crys.Structure(coords=rand(10, 3), cell=rand(3, 3), symbols=['H'] * 10) for obj, indices in [(st, [0, 1, 2]), (tr, [0, 0, 1, 2])]: if obj.is_traj: v0 = obj.coords[indices[0], indices[1], ...] v1 = obj.coords[indices[0], indices[2], ...] v2 = obj.coords[indices[0], indices[3], ...] else: v0 = obj.coords[indices[0], ...] v1 = obj.coords[indices[1], ...] v2 = obj.coords[indices[2], ...] # use eps=0 since the new system is not orthogonal, only test API o1 = crys.align_cart(obj, x=v1 - v0, y=v2 - v0, eps=0) o2 = crys.align_cart(obj, vecs=np.array([v0, v1, v2]), eps=0) o3 = crys.align_cart(obj, indices=indices, eps=0) tools.assert_dict_with_all_types_almost_equal(o1.__dict__, o2.__dict__, keys=o1.attr_lst) tools.assert_dict_with_all_types_almost_equal(o1.__dict__, o3.__dict__, keys=o1.attr_lst)
def test_pbc_wrap(): coords_frac_orig = np.array([[1.1, 0.9, -0.1], [-0.8, 0.5, 0.0]]) st = crys.Structure(coords_frac=coords_frac_orig, cell=np.identity(3) * 2, symbols=['H'] * 2) st_wrap = crys.pbc_wrap(st) # pbc_wrap() makes a copy by default, make sure the original st is unchanged assert (st.coords_frac == coords_frac_orig).all() assert (st.coords == coords_frac_orig * 2.0).all() coords_frac_wrap = np.array([[0.1, 0.9, 0.9], [0.2, 0.5, 0.]]) assert np.allclose(coords_frac_wrap, st_wrap.coords_frac) assert np.allclose(coords_frac_wrap, st_wrap.coords / 2.0) coords_frac_wrap = np.random.rand(20, 100, 3) plus = np.random.randint(-1, 1, coords_frac_wrap.shape) coords_frac_orig = coords_frac_wrap + plus tr = crys.Trajectory(coords_frac=coords_frac_orig, cell=np.identity(3) * 2.0, symbols=['H'] * 100) tr_wrap = crys.pbc_wrap(tr, xyz_axis=-1) assert (tr.coords_frac == coords_frac_orig).all() assert (tr.coords == coords_frac_orig * 2.0).all() assert np.allclose(coords_frac_orig[plus == 0], tr_wrap.coords_frac[plus == 0]) assert np.allclose(coords_frac_orig[plus == -1] + 1, tr_wrap.coords_frac[plus == -1]) assert np.allclose(coords_frac_orig[plus == 1] - 1, tr_wrap.coords_frac[plus == 1]) assert np.allclose(coords_frac_wrap, tr_wrap.coords_frac) assert np.allclose(coords_frac_wrap, tr_wrap.coords / 2.0) tr_wrap = crys.pbc_wrap(tr, mask=[True, True, False], xyz_axis=-1) assert np.allclose(tr.coords_frac[..., 2], tr_wrap.coords_frac[..., 2])
def test_save_mkdir(): path = os.path.join(testdir, 'foo', 'bar', 'baz') assert not os.path.exists(path) fn = os.path.join(path, 'grr.pk') st = crys.Structure(coords=rand(10,3), cell=rand(3,3), symbols=['H']*10) st.dump(fn, mkdir=True) io.read_pickle(fn)
def test_write_lammps(): st = crys.Structure(coords_frac=rand(20, 3), cell=rand(3, 3), symbols=['Al'] * 10 + ['N'] * 10) # align cell to lammps standard [[x,0,0],...] st.coords = None st.cell = None st.set_all() st_fn = common.pj(testdir, 'lmp.struct') io.write_lammps(st_fn, st) symbols = common.file_read(st_fn + '.symbols').split() assert st.symbols == symbols cmd = r"grep -A22 Atoms %s | grep '^[0-9]'" % st_fn arr = parse.arr2d_from_txt(common.backtick(cmd)) assert arr.shape == (20, 5) assert np.allclose(st.coords, arr[:, 2:])
def test_fdist(): natoms = 5 coords_frac = np.random.rand(natoms,3) cell = np.random.rand(3,3)*3 struct = crys.Structure(coords_frac=coords_frac, cell=cell) for pbc in [0,1]: print("pbc:", pbc) pyret = pydist(coords_frac, cell, pbc) # uses _flib.distsq_frac() pyret2 = crys.distances(struct, pbc=pbc, squared=True, fullout=True) for ii in [0,1,2]: print(ii) assert np.allclose(pyret[ii], pyret2[ii])
def test_correct(): coords = rand(20, 3) cell = rand(3, 3) st = crys.Structure(coords=coords, cell=cell, symbols=['H']) # new cartesian coord sys, created by rotating the old E=identity(3) by 3 # random angles angles = [rand() * 360, rand() * 180, rand() * 360] rmat = euler_rotation(*tuple(angles)) newcoords = np.array([np.dot(rmat, x) for x in st.coords]) newcell = np.array([np.dot(rmat, x) for x in st.cell]) st2 = crys.align_cart(st, cart=rmat, eps=1e-3) assert np.allclose(newcoords, np.dot(st.coords, rmat.T)) assert np.allclose( newcoords, crys.coord_trans(st.coords, old=np.identity(3), new=rmat)) assert np.allclose(newcell, np.dot(st.cell, rmat.T)) assert np.allclose(newcell, crys.coord_trans(st.cell, old=np.identity(3), new=rmat)) assert np.allclose(st2.coords, newcoords)
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)
{ensemble} timestep 0.5e-3 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',
# coords_frac = \ 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