def test_base(self): """Base tests for PhononBands""" filename = abidata.ref_file("trf2_5.out_PHBST.nc") phbands = PhononBands.from_file(filename) print(phbands) assert PhononBands.as_phbands(phbands) is phbands assert np.array_equal( PhononBands.as_phbands(filename).phfreqs, phbands.phfreqs) self.serialize_with_pickle(phbands, protocols=[-1], test_eq=False) self.assertEqual(phbands.minfreq, 0.0) #self.assertEqual(phbands.maxfreq, 30) # Test XYZ vib _, filename = tempfile.mkstemp(text=True) phbands.create_xyz_vib(iqpt=0, filename=filename, max_supercell=[4, 4, 4]) # Test convertion to eigenvectors. Verify that they are orthonormal # Allow relatively large tolerance due to possible mismatching in the atomic masses between abinit and pmg eig = phbands.dyn_mat_eigenvect self.assertTrue( np.allclose(np.dot(eig[0], eig[0].T), np.eye(len(eig[0]), dtype=np.complex), atol=1e-5, rtol=1e-3))
def test_base(self): """Base tests for PhononBands""" filename = abidata.ref_file("trf2_5.out_PHBST.nc") phbands = PhononBands.from_file(filename) print(phbands) assert PhononBands.as_phbands(phbands) is phbands assert np.array_equal( PhononBands.as_phbands(filename).phfreqs, phbands.phfreqs) self.serialize_with_pickle(phbands, protocols=[-1], test_eq=False) self.assertEqual(phbands.minfreq, 0.0) #self.assertEqual(phbands.maxfreq, 30) # Test XYZ vib phbands.create_xyz_vib(iqpt=0, filename=self.get_tmpname(text=True), max_supercell=[4, 4, 4]) # Test ascii file phbands.create_ascii_vib(iqpts=0, filename=self.get_tmpname(text=True), pre_factor=1) # Test phononwebsite file phbands.create_phononwebsite_json(filename=self.get_tmpname(text=True), name='test') # Test xmgrace phbands.to_xmgrace(self.get_tmpname(text=True)) # Test convertion to eigenvectors. Verify that they are orthonormal # Allow relatively large tolerance due to possible mismatching in the atomic masses between abinit and pmg eig = phbands.dyn_mat_eigenvect assert np.allclose(np.dot(eig[0], eig[0].T), np.eye(len(eig[0]), dtype=np.complex), atol=1e-5, rtol=1e-3) if self.has_matplotlib(): phbands.plot(show=False) phbands.plot_fatbands(show=False) phbands.plot_colored_matched(show=False) # Cannot compute PHDOS with q-path with self.assertRaises(ValueError): phdos = phbands.get_phdos() # convert to pymatgen object phbands.to_pymatgen()
def test_base(self): """Base tests for PhononBands""" filename = abidata.ref_file("trf2_5.out_PHBST.nc") phbands = PhononBands.from_file(filename) print(phbands) assert PhononBands.as_phbands(phbands) is phbands assert np.array_equal(PhononBands.as_phbands(filename).phfreqs, phbands.phfreqs) self.serialize_with_pickle(phbands, protocols=[-1], test_eq=False) self.assertEqual(phbands.minfreq, 0.0) #self.assertEqual(phbands.maxfreq, 30) # Test XYZ vib _, filename = tempfile.mkstemp(text=True) phbands.create_xyz_vib(iqpt=0, filename=filename, max_supercell=[4,4,4]) # Test convertion to eigenvectors. Verify that they are orthonormal # Allow relatively large tolerance due to possible mismatching in the atomic masses between abinit and pmg eig = phbands.dyn_mat_eigenvect self.assertTrue(np.allclose(np.dot(eig[0], eig[0].T), np.eye(len(eig[0]), dtype=np.complex), atol=1e-5, rtol=1e-3))
def test_base(self): """Base tests for PhononBands""" filename = abidata.ref_file("trf2_5.out_PHBST.nc") phbands = PhononBands.from_file(filename) repr(phbands); str(phbands) assert phbands.to_string(title="Title", with_structure=False, with_qpoints=True, verbose=1) assert PhononBands.as_phbands(phbands) is phbands with self.assertRaises(TypeError): PhononBands.as_phbands({}) assert np.array_equal(PhononBands.as_phbands(filename).phfreqs, phbands.phfreqs) with abilab.abiopen(abidata.ref_file("trf2_5.out_PHBST.nc")) as nc: repr(nc); str(nc) assert nc.to_string(verbose=1) assert nc.params["nqpt"] == len(nc.qpoints) assert nc.qpoints.is_path assert any(q.name is not None for q in nc.qpoints) same_phbands_nc = PhononBands.as_phbands(nc) self.assert_equal(same_phbands_nc.phfreqs, phbands.phfreqs) assert phbands.phdispl_cart.shape == (phbands.nqpt, phbands.num_branches, phbands.num_branches) # a + b gives plotter assert hasattr(same_phbands_nc + phbands, "combiplot") self.serialize_with_pickle(phbands, protocols=[-1], test_eq=False) # From pickle file. tmp_path = self.get_tmpname(suffix=".pickle") with open(tmp_path, "wb") as fh: pickle.dump(phbands, fh) same_phbands = PhononBands.as_phbands(tmp_path) self.assert_equal(same_phbands.phfreqs, phbands.phfreqs) # a + b + c gives plotter p = phbands + same_phbands + same_phbands_nc assert hasattr(p, "combiplot") assert phbands.minfreq == 0.0 #self.assertEqual(phbands.maxfreq, 30) assert phbands.phfactor_ev2units("eV") == abu.phfactor_ev2units("eV") # Test XYZ vib phbands.create_xyz_vib(iqpt=0, filename=self.get_tmpname(text=True), max_supercell=[4, 4, 4]) # Test ascii file phbands.create_ascii_vib(iqpts=0, filename=self.get_tmpname(text=True), pre_factor=1) # Test phononwebsite file phbands.create_phononwebsite_json(filename=self.get_tmpname(text=True), name='test') assert phbands.view_phononwebsite(verbose=1, dryrun=True) == 0 # Test xmgrace phbands.to_xmgrace(self.get_tmpname(text=True)) #phbands.to_xmgrace(sys.stdout) df = phbands.get_dataframe() assert "freq" in df and "mode" in df self.assert_almost_equal(df["freq"].values.min(), 0) umodes = phbands.get_unstable_modes(below_mev=-1000) assert len(umodes) == 0 acoustic_modes = phbands.acoustic_indices((0, 0, 0)) self.assertArrayEqual(acoustic_modes, [0, 1, 2]) asr_breaking = phbands.asr_breaking() assert asr_breaking.absmax_break == 0 # Test convertion to eigenvectors. Verify that they are orthonormal # Allow relatively large tolerance due to possible mismatching in the atomic masses between abinit and pmg # (Note that amu is None here) assert phbands.amu is None eig = phbands.dyn_mat_eigenvect assert len(eig) == phbands.nqpt cidentity = np.eye(len(eig[0]), dtype=np.complex) for iq in range(len(eig)): #print("About to test iq", iq, np.dot(eig[iq], eig[iq].T)) #assert np.allclose(np.dot(eig[iq], eig[iq].T), cidentity , atol=1e-5, rtol=1e-3) assert np.allclose(np.dot(eig[iq].conjugate().T, eig[iq]), cidentity , atol=1e-5, rtol=1e-3) #self.assert_almost_equal(np.dot(eig[iq].conjugate().T, eig[iq]), cidentity) # Mapping reduced coordinates -> labels qlabels = { (0,0,0): r"$\Gamma$", (0.375, 0.375, 0.75): "K", (0.5, 0.5, 1.0): "X", (0.5, 0.5, 0.5): "L", (0.5, 0.0, 0.5): "X", (0.5, 0.25, 0.75): "W", } if self.has_matplotlib(): assert phbands.plot(units="Thz", show=False) assert phbands.plot_fatbands(units="ha", qlabels=qlabels, show=False) assert phbands.plot_fatbands(phdos_file=abidata.ref_file("trf2_5.out_PHDOS.nc"), units="thz", show=False) assert phbands.plot_colored_matched(units="cm^-1", show=False) assert phbands.plot_phdispl(qpoint=(0, 0, 0), units="cm^-1", hatches=None, show=False) assert phbands.plot_phdispl(qpoint=(0, 0, 0), units="cm^-1", hatches=None, show=False, cart_dir="x+y") assert phbands.plot_phdispl(qpoint=(0, 0, 0), units="cm^-1", hatches=None, show=False, use_sqrt=True, normalize=False) with self.assertRaises(ValueError): # No LO-TO terms assert phbands.plot_phdispl(qpoint=1, is_non_analytical_direction=True, show=False) assert phbands.plot_phdispl_cartdirs(qpoint=0, units="cm^-1", show=False) assert phbands.boxplot(units="ev", mode_range=[2, 4], show=False) # Cannot compute PHDOS with q-path with self.assertRaises(ValueError): phdos = phbands.get_phdos() # convert to pymatgen object phbands.to_pymatgen() # get frozen phonons phbands.get_frozen_phonons((0.5, 0.5, 1.0), 1, eta=0.5, max_supercell=[5,5,5]) assert not phbands.has_linewidths phbands.linewidths = np.ones(phbands.shape) assert phbands.has_linewidths
def test_base(self): """Base tests for PhononBands""" filename = abidata.ref_file("trf2_5.out_PHBST.nc") phbands = PhononBands.from_file(filename) repr(phbands) str(phbands) assert phbands.to_string(title="Title", with_structure=False, with_qpoints=True, verbose=1) assert PhononBands.as_phbands(phbands) is phbands with self.assertRaises(TypeError): PhononBands.as_phbands({}) assert np.array_equal( PhononBands.as_phbands(filename).phfreqs, phbands.phfreqs) with abilab.abiopen(abidata.ref_file("trf2_5.out_PHBST.nc")) as nc: repr(nc) str(nc) assert nc.to_string(verbose=1) assert nc.params["nqpt"] == len(nc.qpoints) assert nc.qpoints.is_path assert any(q.name is not None for q in nc.qpoints) same_phbands_nc = PhononBands.as_phbands(nc) self.assert_equal(same_phbands_nc.phfreqs, phbands.phfreqs) assert phbands.phdispl_cart.shape == (phbands.nqpt, phbands.num_branches, phbands.num_branches) # a + b gives plotter assert hasattr(same_phbands_nc + phbands, "combiplot") assert phbands.epsinf is None and phbands.zcart is None self.serialize_with_pickle(phbands, protocols=[-1], test_eq=False) # From pickle file. tmp_path = self.get_tmpname(suffix=".pickle") with open(tmp_path, "wb") as fh: pickle.dump(phbands, fh) same_phbands = PhononBands.as_phbands(tmp_path) self.assert_equal(same_phbands.phfreqs, phbands.phfreqs) # a + b + c gives plotter p = phbands + same_phbands + same_phbands_nc assert hasattr(p, "combiplot") assert phbands.minfreq == 0.0 #self.assertEqual(phbands.maxfreq, 30) assert phbands.phfactor_ev2units("eV") == abu.phfactor_ev2units("eV") # Test XYZ vib phbands.create_xyz_vib(iqpt=0, filename=self.get_tmpname(text=True), max_supercell=[4, 4, 4]) # Test ascii file phbands.create_ascii_vib(iqpts=0, filename=self.get_tmpname(text=True), pre_factor=1) # Test phononwebsite file phbands.create_phononwebsite_json(filename=self.get_tmpname(text=True), name='test') assert phbands.view_phononwebsite(verbose=1, dryrun=True) == 0 # Test xmgrace phbands.to_xmgrace(self.get_tmpname(text=True)) #phbands.to_xmgrace(sys.stdout) df = phbands.get_dataframe() assert "freq" in df and "mode" in df self.assert_almost_equal(df["freq"].values.min(), 0) umodes = phbands.get_unstable_modes(below_mev=-1000) assert len(umodes) == 0 acoustic_modes = phbands.acoustic_indices((0, 0, 0)) self.assertArrayEqual(acoustic_modes, [0, 1, 2]) asr_breaking = phbands.asr_breaking() assert asr_breaking.absmax_break == 0 # Test convertion to eigenvectors. Verify that they are orthonormal # Allow relatively large tolerance due to possible mismatching in the atomic masses between abinit and pmg # (Note that amu is None here) assert phbands.amu is None eig = phbands.dyn_mat_eigenvect assert len(eig) == phbands.nqpt cidentity = np.eye(len(eig[0]), dtype=np.complex) for iq in range(len(eig)): #print("About to test iq", iq, np.dot(eig[iq], eig[iq].T)) #assert np.allclose(np.dot(eig[iq], eig[iq].T), cidentity , atol=1e-5, rtol=1e-3) assert np.allclose(np.dot(eig[iq].conjugate().T, eig[iq]), cidentity, atol=1e-5, rtol=1e-3) #self.assert_almost_equal(np.dot(eig[iq].conjugate().T, eig[iq]), cidentity) # Mapping reduced coordinates -> labels qlabels = { (0, 0, 0): r"$\Gamma$", (0.375, 0.375, 0.75): "K", (0.5, 0.5, 1.0): "X", (0.5, 0.5, 0.5): "L", (0.5, 0.0, 0.5): "X", (0.5, 0.25, 0.75): "W", } if self.has_matplotlib(): assert phbands.plot(units="Thz", show=False, temp=300) assert phbands.plot_fatbands(units="ha", qlabels=qlabels, show=False) assert phbands.plot_fatbands( phdos_file=abidata.ref_file("trf2_5.out_PHDOS.nc"), units="thz", show=False) assert phbands.plot_colored_matched(units="cm^-1", show=False) assert phbands.plot_phdispl(qpoint=(0, 0, 0), units="cm^-1", hatches=None, show=False) assert phbands.plot_phdispl(qpoint=(0, 0, 0), units="cm^-1", hatches=None, show=False, cart_dir="x+y") assert phbands.plot_phdispl(qpoint=(0, 0, 0), units="cm^-1", hatches=None, show=False, use_sqrt=True, normalize=False) with self.assertRaises(ValueError): # No LO-TO terms assert phbands.plot_phdispl(qpoint=1, is_non_analytical_direction=True, show=False) assert phbands.plot_phdispl_cartdirs(qpoint=0, units="cm^-1", show=False) assert phbands.boxplot(units="ev", mode_range=[2, 4], show=False) # Cannot compute PHDOS with q-path with self.assertRaises(ValueError): phdos = phbands.get_phdos() # convert to pymatgen object phbands.to_pymatgen() # get frozen phonons phbands.get_frozen_phonons((0.5, 0.5, 1.0), 1, eta=0.5, max_supercell=[5, 5, 5]) assert not phbands.has_linewidths phbands.linewidths = np.ones(phbands.shape) assert phbands.has_linewidths