def test_pw_md_out(): filename = tools.unpack_compressed('files/pw.md.out.gz', prefix=__file__) alat = 5.9098 # Bohr pp1 = PwMDOutputFile(filename=filename, use_alat=True) pp1.parse() none_attrs = [\ 'coords_frac', ] assert_attrs_not_none(pp1, none_attrs=none_attrs) assert np.allclose(pp1.timestep, 150.0) # tryd traj1 = pp1.get_traj() assert_attrs_not_none(traj1) pp2 = PwMDOutputFile(filename=filename, use_alat=False, units={'length': alat*Bohr/Ang}) pp2.parse() assert np.allclose(pp2.timestep, 150.0) # tryd assert_attrs_not_none(pp2, none_attrs=none_attrs) # Skip coords and cell b/c they are modified by self.alat and # pp1.alat = 1.0, pp2.alat = 5.9098 attr_lst = common.pop_from_list(pp1.attr_lst, ['coords', 'cell']) adae(pp1.__dict__, pp2.__dict__, keys=attr_lst) traj2 = pp2.get_traj() adae(traj1.__dict__, traj2.__dict__, keys=traj1.attr_lst) pp3 = PwMDOutputFile(filename=filename) assert alat == pp3.get_alat() # self.use_alat=True default
def test_pw_vc_relax_out(): filename = 'files/pw.vc_relax.out' common.system('gunzip %s.gz' %filename) pp = PwMDOutputFile(filename=filename) pp.parse() common.system('gzip %s' %filename) none_attrs = ['coords', 'ekin', 'temperature', 'timestep', ] assert_attrs_not_none(pp, none_attrs=none_attrs) traj = pp.get_traj() none_attrs = [\ 'ekin', 'temperature', 'timestep', 'velocity', 'time', ] assert_attrs_not_none(traj, none_attrs=none_attrs)
def test_save_object(): filename = tools.unpack_compressed('files/pw.md.out.gz', prefix=__file__) dumpfile = os.path.join(testdir, 'pw.md.pk') c = PwMDOutputFile(filename=filename) print ">>> parsing ..." c.parse() print ">>> ... done" print ">>> saving %s ..." %dumpfile c.dump(dumpfile) print ">>> ... done" print ">>> loading ..." c2 = PwMDOutputFile() c2.load(dumpfile) print ">>> ... done" print ">>> checking equalness of attrs in loaded object ..." known_fails = {'fd': 'closed/uninitialized file', 'cont': 'container object'} arr_t = type(np.array([1])) dict_t = type({}) for attr in c.__dict__.iterkeys(): c_val = getattr(c, attr) c2_val = getattr(c2, attr) dotest = True for name, string in known_fails.iteritems(): if name == attr: print "known fail: %s: %s: %s" %(name, string, attr) dotest = False if dotest: print "testing:", attr, type(c_val), type(c2_val) type_c = type(c_val) type_c2 = type(c2_val) assert type_c is type_c2, "attr: %s: types differ: %s, %s" \ %(attr, str(type_c), str(type_c2)) if type(c_val) is arr_t: assert (c_val == c2_val).all(), "fail: %s: %s, %s" \ %(attr, c_val, c2_val) elif type(c_val) is dict_t: ade(c_val, c2_val) else: assert c_val == c2_val, "fail: %s: %s, %s" \ %(attr, c_val, c2_val)
def test_pw_vc_relax_out(): filename = 'files/pw.vc_relax_cell_unit.out' common.system('gunzip %s.gz' % filename) pp = PwMDOutputFile(filename=filename) pp.parse() common.system('gzip %s' % filename) none_attrs = [ 'coords', 'ekin', 'temperature', 'timestep', ] assert_attrs_not_none(pp, none_attrs=none_attrs) traj = pp.get_traj() none_attrs = [\ 'ekin', 'temperature', 'timestep', 'velocity', 'time', ] assert_attrs_not_none(traj, none_attrs=none_attrs) assert pp.cell_unit == 'alat' assert pp.cell.shape == (6, 3, 3) for idx in range(1, pp.cell.shape[0]): assert crys.rms(pp.cell[idx, ...] - pp.cell[0, ...]) > 0.0 # Test _get_block_header_unit, which is used in get_cell_unit(). dct = \ {'FOO': None, 'FOO alat': 'alat', 'FOO (alat)': 'alat', 'FOO {alat}': 'alat', 'FOO (alat=1.23)': 'alat', 'FOO (alat= 1.23)': 'alat', } for txt, val in dct.items(): fn = pj(testdir, 'test_block_header_unit.txt') common.file_write(fn, txt) pp.filename = fn assert pp._get_block_header_unit('FOO') == val
def test_save_object(): filename = tools.unpack_compressed('files/pw.md.out.gz', prefix=__file__) dumpfile = os.path.join(testdir, 'pw.md.pk') c = PwMDOutputFile(filename=filename) print(">>> parsing ...") c.parse() print(">>> ... done") print(">>> saving %s ..." %dumpfile) c.dump(dumpfile) print(">>> ... done") print(">>> loading ...") c2 = io.read_pickle(dumpfile) print(">>> ... done") print(">>> checking equalness of attrs in loaded object ...") known_fails = {'fd': 'closed/uninitialized file', 'cont': 'container object'} arr_t = type(np.array([1])) dict_t = type({}) for attr in c.__dict__.keys(): c_val = getattr(c, attr) c2_val = getattr(c2, attr) dotest = True for name, string in known_fails.items(): if name == attr: print("known fail: %s: %s: %s" %(name, string, attr)) dotest = False if dotest: print("testing:", attr, type(c_val), type(c2_val)) type_c = type(c_val) type_c2 = type(c2_val) assert type_c is type_c2, "attr: %s: types differ: %s, %s" \ %(attr, str(type_c), str(type_c2)) if type(c_val) is arr_t: assert (c_val == c2_val).all(), "fail: %s: %s, %s" \ %(attr, c_val, c2_val) elif type(c_val) is dict_t: ade(c_val, c2_val) else: assert c_val == c2_val, "fail: %s: %s, %s" \ %(attr, c_val, c2_val)
def test_pw_vc_relax_out(): filename = 'files/pw.vc_relax_cell_unit.out' common.system('gunzip %s.gz' %filename) pp = PwMDOutputFile(filename=filename) pp.parse() common.system('gzip %s' %filename) none_attrs = ['coords', 'ekin', 'temperature', 'timestep', ] assert_attrs_not_none(pp, none_attrs=none_attrs) traj = pp.get_traj() none_attrs = [\ 'ekin', 'temperature', 'timestep', 'velocity', 'time', ] assert_attrs_not_none(traj, none_attrs=none_attrs) assert pp.cell_unit == 'alat' assert pp.cell.shape == (6,3,3) for idx in range(1, pp.cell.shape[0]): assert crys.rms(pp.cell[idx,...] - pp.cell[0,...]) > 0.0 # Test _get_block_header_unit, which is used in get_cell_unit(). dct = \ {'FOO': None, 'FOO alat': 'alat', 'FOO (alat)': 'alat', 'FOO {alat}': 'alat', 'FOO (alat=1.23)': 'alat', 'FOO (alat= 1.23)': 'alat', } for txt,val in dct.iteritems(): fn = pj(testdir, 'test_block_header_unit.txt') common.file_write(fn, txt) pp.filename = fn assert pp._get_block_header_unit('FOO') == val
def test_pw_more_forces(): fac = Ry / eV / Bohr * Ang # MD: london=.true. filename = 'files/pw.md_london.out' common.system('gunzip %s.gz' %filename) natoms = 141 nstep = 10 # traj case pp = PwMDOutputFile(filename=filename) tr = pp.get_traj() assert tr.natoms == natoms assert tr.forces.shape == (nstep,natoms,3) assert tr.coords.shape == (nstep,natoms,3) assert pp._forces_raw.shape == (nstep+1,2*natoms,3) assert np.allclose(tr.forces, pp._forces_raw[1:,:natoms,:] * fac) # scf case, return only 1st step pp = PwSCFOutputFile(filename=filename) st = pp.get_struct() assert st.natoms == natoms assert st.forces.shape == (natoms,3) assert st.coords.shape == (natoms,3) assert pp._forces_raw.shape == (nstep+1,2*natoms,3) assert np.allclose(st.forces, pp._forces_raw[0,:natoms,:] * fac) common.system('gzip %s' %filename) # SCF: verbosity='high' + london=.true. filename = 'files/pw.scf_verbose_london.out' common.system('gunzip %s.gz' %filename) natoms = 4 nstep = 1 pp = PwSCFOutputFile(filename=filename) st = pp.get_struct() assert st.natoms == natoms assert st.forces.shape == (natoms,3) assert st.coords.shape == (natoms,3) assert pp._forces_raw.shape == (nstep, 8*natoms,3) assert np.allclose(st.forces, pp._forces_raw[0,:natoms,:] * fac) common.system('gzip %s' %filename) # MD: verbosity='high' + natoms=1 filename = 'files/pw.md_one_atom.out' common.system('gunzip %s.gz' %filename) natoms = 1 nstep = 4 # traj case pp = PwMDOutputFile(filename=filename) tr = pp.get_traj() assert tr.natoms == natoms assert tr.forces.shape == (nstep,natoms,3) assert tr.coords.shape == (nstep,natoms,3) assert pp._forces_raw.shape == (nstep+1,7*natoms,3) assert np.allclose(tr.forces, pp._forces_raw[1:,:natoms,:] * fac) # scf case, return only 1st step pp = PwSCFOutputFile(filename=filename) st = pp.get_struct() assert st.natoms == natoms assert st.forces.shape == (natoms,3) assert st.coords.shape == (natoms,3) assert pp._forces_raw.shape == (nstep+1,7*natoms,3) assert np.allclose(st.forces, pp._forces_raw[0,:natoms,:] * fac) common.system('gzip %s' %filename)
def test_return_3d_if_no_cell_unit(): filename = tools.unpack_compressed('files/pw.vc_relax_no_cell_unit.out.gz', prefix=__file__) pp = PwMDOutputFile(filename=filename) pp.parse() assert np.allclose(pp.cell, _cell*pp.get_alat())
def test_pw_md_out(): filename = tools.unpack_compressed('files/pw.md.out.gz', prefix=__file__) alat = 5.9098 # Bohr pp1 = PwMDOutputFile(filename=filename, use_alat=True) pp1.parse() none_attrs = [\ 'coords_frac', ] assert_attrs_not_none(pp1, none_attrs=none_attrs) assert np.allclose(pp1.timestep, 150.0) # tryd traj1 = pp1.get_traj() assert_attrs_not_none(traj1) pp2 = PwMDOutputFile(filename=filename, use_alat=False, units={'length': alat * Bohr / Ang}) pp2.parse() assert np.allclose(pp2.timestep, 150.0) # tryd assert_attrs_not_none(pp2, none_attrs=none_attrs) # Skip coords and cell b/c they are modified by self.alat and # pp1.alat = 1.0, pp2.alat = 5.9098 attr_lst = common.pop_from_list(pp1.attr_lst, ['coords', 'cell']) adae(pp1.__dict__, pp2.__dict__, keys=attr_lst) traj2 = pp2.get_traj() adae(traj1.__dict__, traj2.__dict__, keys=traj1.attr_lst) pp3 = PwMDOutputFile(filename=filename) assert alat == pp3.get_alat() # self.use_alat=True default