def read_dynmat(path='.', natoms=None, filename='dynmat.out', axsf='dynmat.axsf'): """Read ``dynmat.x`` output. `freqs` are parsed from `filename` and `vecs` from `axsf`. `qpoints` is alawys Gamma, i.e. [0,0,0]. Output format is the same as in :func:`read_dyn`. Parameters ---------- path : str path where output files are natoms : int filename : str Text output from dynmat.x, where the frequencies are printed, relative to `path`. axsf : str AXSF file (``filxsf`` in input) with mode vectors as forces. Returns ------- qpoints, freqs, vecs qpoints : 1d array (3,) The qpoint, which is Gamma, i.e. [0,0,0] freqs : 1d array, (nmodes,) where nmodes = 3*natoms 3*natoms phonon frequencies in [cm^-1] at the q-point. vecs : 3d real array (nmodes, natoms, 3) Real parts (???) if the eigenvectors of the dynamical matrix for the q-point. Notes ----- We assume the output to be generated with ``dynmat.x < dynmat.in > dynmat.out``. """ assert natoms is not None, ("natoms is None") nmodes = 3*natoms out_fn = pj(path, filename) axsf_fn = pj(path, axsf) cmd = "grep -A{0} PRIMCO {1} | sed -re '/PRIMCO.*/{{N;d;}}' | \ awk '{{print $5\" \"$6\" \"$7}}'".format(natoms+1, axsf_fn) qpoints = np.zeros((3,)) vecs = np.fromstring(common.backtick(cmd), sep=' ').reshape(nmodes,natoms,3) cmd = "grep -A{0} 'mode.*cm-1' {1} | grep -v mode | \ awk '{{print $2}}'".format(nmodes, out_fn) freqs = np.fromstring(common.backtick(cmd), sep=' ') return qpoints,freqs,vecs
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_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_dcd(): # nstep = 101 # natoms = 16 dir_lmp = tools.unpack_compressed('files/lammps/md-npt.tgz') fn_lmp = pj(dir_lmp, 'lmp.out.dcd') # nstep = 16 # natoms = 57 dir_cp2k = tools.unpack_compressed('files/cp2k/dcd/npt_dcd.tgz') fn_cp2k = pj(dir_cp2k, 'PROJECT-pos-1.dcd') # read headers hdr_lmp = dcd.read_dcd_header(fn_lmp) hdr_cp2k = dcd.read_dcd_header(fn_cp2k) print(">>> comparing headers") for ref, dct in [(hdr_cp2k_ref, hdr_cp2k), (hdr_lmp_ref, hdr_lmp)]: tools.assert_dict_with_all_types_equal(ref, dct, keys=list(ref.keys()), strict=True) # compare data read by python (dcd.py) and fortran (dcd.f90, _dcd) # implementations # cc = cryst_const # co = coords print(">>> comparing data") for fn, convang, nstephdr, nstep, natoms in [ (fn_lmp, True, True, 101, 16), (fn_lmp, True, False, 101, 16), (fn_cp2k, False, False, 16, 57) ]: cc_py, co_py = dcd.read_dcd_data(fn, convang=convang) cc_f, co_f = dcd.read_dcd_data_f(fn, convang=convang, nstephdr=nstephdr) print(">>> ... cryst_const") tools.assert_array_equal(cc_py, cc_f) print(">>> ... coords") tools.assert_array_equal(co_py, co_f) print(">>> ... shapes") assert cc_f.shape == (nstep, 6) assert co_f.shape == (nstep, natoms, 3) # lmp angles are around 60, cp2k around 90 degree, cosines are between # -1 and 1, make sure the angle conversion works print(">>> ... angles") assert (cc_py[:, 3:] > 50).all() # compare slow and fast python versions # cc = cryst_const # co = coords print(">>> comparing data") for fn, convang, nstep, natoms in [(fn_lmp, True, 101, 16), (fn_lmp, True, 101, 16), (fn_cp2k, False, 16, 57)]: cc_py_fast, co_py_fast = dcd.read_dcd_data(fn, convang=convang) cc_py_ref, co_py_ref = dcd.read_dcd_data_ref(fn, convang=convang) print(">>> ... cryst_const") tools.assert_array_equal(cc_py_fast, cc_py_ref) print(">>> ... coords") tools.assert_array_equal(co_py_fast, co_py_ref) print(">>> ... shapes") assert cc_py_ref.shape == (nstep, 6) assert co_py_ref.shape == (nstep, natoms, 3) # lmp angles are around 60, cp2k around 90 degree, cosines are between # -1 and 1, make sure the angle conversion works print(">>> ... angles") assert (cc_py_fast[:, 3:] > 50).all()
def test_dcd(): # nstep = 101 # natoms = 16 dir_lmp = tools.unpack_compressed('files/lammps/md-npt.tgz') fn_lmp = pj(dir_lmp, 'lmp.out.dcd') # nstep = 16 # natoms = 57 dir_cp2k = tools.unpack_compressed('files/cp2k/dcd/npt_dcd.tgz') fn_cp2k = pj(dir_cp2k, 'PROJECT-pos-1.dcd') # read headers hdr_lmp = dcd.read_dcd_header(fn_lmp) hdr_cp2k = dcd.read_dcd_header(fn_cp2k) print ">>> comparing headers" for ref, dct in [(hdr_cp2k_ref, hdr_cp2k), (hdr_lmp_ref, hdr_lmp)]: tools.assert_dict_with_all_types_equal(ref, dct, keys=ref.keys(), strict=True) # compare data read by python (dcd.py) and fortran (dcd.f90, _dcd) # implementations # cc = cryst_const # co = coords print ">>> comparing data" for fn,convang,nstephdr,nstep,natoms in [(fn_lmp,True,True,101,16), (fn_lmp,True,False,101,16), (fn_cp2k,False,False,16,57)]: cc_py, co_py = dcd.read_dcd_data(fn, convang=convang) cc_f, co_f = dcd.read_dcd_data_f(fn, convang=convang, nstephdr=nstephdr) print ">>> ... cryst_const" tools.assert_array_equal(cc_py, cc_f) print ">>> ... coords" tools.assert_array_equal(co_py, co_f) print ">>> ... shapes" assert cc_f.shape == (nstep,6) assert co_f.shape == (nstep,natoms,3) # lmp angles are around 60, cp2k around 90 degree, cosines are between # -1 and 1, make sure the angle conversion works print ">>> ... angles" assert (cc_py[:,3:] > 50).all() # compare slow and fast python versions # cc = cryst_const # co = coords print ">>> comparing data" for fn,convang,nstep,natoms in [(fn_lmp,True,101,16), (fn_lmp,True,101,16), (fn_cp2k,False,16,57)]: cc_py_fast, co_py_fast = dcd.read_dcd_data(fn, convang=convang) cc_py_ref, co_py_ref = dcd.read_dcd_data_ref(fn, convang=convang) print ">>> ... cryst_const" tools.assert_array_equal(cc_py_fast, cc_py_ref) print ">>> ... coords" tools.assert_array_equal(co_py_fast, co_py_ref) print ">>> ... shapes" assert cc_py_ref.shape == (nstep,6) assert co_py_ref.shape == (nstep,natoms,3) # lmp angles are around 60, cp2k around 90 degree, cosines are between # -1 and 1, make sure the angle conversion works print ">>> ... angles" assert (cc_py_fast[:,3:] > 50).all()