def modes_from_file(filename: Union[str, os.PathLike] ) -> QpointPhononModes: """ Load phonon mode data from file Parameters ---------- filename Data file Returns ------- QpointPhononmodes """ path = pathlib.Path(filename) if path.suffix == '.phonon': return QpointPhononModes.from_castep(path) elif path.suffix == '.json': return QpointPhononModes.from_json_file(path) elif path.suffix in ('.yaml', '.hdf5'): return QpointPhononModes.from_phonopy(path=path.parent, phonon_name=path.name) else: raise ValueError("File not recognised. Should have extension " ".yaml or .hdf5 (phonopy), " ".phonon (castep) or .json (JSON from Euphonic).")
def test_calculate_dos(self, material, qpt_ph_modes_file, expected_dos_json, ebins): if qpt_ph_modes_file.endswith('.phonon'): qpt_ph_modes = QpointPhononModes.from_castep( get_castep_path(material, qpt_ph_modes_file)) else: qpt_ph_modes = QpointPhononModes.from_phonopy( phonon_name=get_phonopy_path(material, qpt_ph_modes_file)) dos = qpt_ph_modes.calculate_dos(ebins) expected_dos = get_expected_spectrum1d(expected_dos_json) check_spectrum1d(dos, expected_dos)
def test_calculate_debye_waller(self, material, qpt_ph_modes_file, expected_dw_json, temperature, kwargs): if qpt_ph_modes_file.endswith('.phonon'): qpt_ph_modes = QpointPhononModes.from_castep( get_castep_path(material, qpt_ph_modes_file)) else: qpt_ph_modes = QpointPhononModes.from_phonopy( phonon_name=get_phonopy_path(material, qpt_ph_modes_file)) dw = qpt_ph_modes.calculate_debye_waller(temperature * ureg('K'), **kwargs) expected_dw = get_expected_dw(material, expected_dw_json) check_debye_waller(dw, expected_dw, dw_atol=1e-12)
def test_create_from_phonopy_without_installed_modules_raises_err( self, material, subdir, phonopy_args, mocker): phonopy_args['path'] = get_phonopy_path(material, subdir) # Mock import of yaml, h5py to raise ModuleNotFoundError import builtins real_import = builtins.__import__ def mocked_import(name, *args, **kwargs): if name == 'h5py' or name == 'yaml': raise ModuleNotFoundError return real_import(name, *args, **kwargs) mocker.patch('builtins.__import__', side_effect=mocked_import) with pytest.raises(ImportPhonopyReaderError): QpointPhononModes.from_phonopy(**phonopy_args)
def create_from_phonopy(self, request): material, subdir, phonopy_args, json_file = request.param phonopy_args['path'] = get_phonopy_path(material, subdir) qpt_ph_modes = QpointPhononModes.from_phonopy(**phonopy_args) json_path = os.path.join(get_qpt_ph_modes_dir(material), json_file) expected_qpt_ph_modes = ExpectedQpointPhononModes(json_path) return qpt_ph_modes, expected_qpt_ph_modes
def create_from_castep(self, request): material, phonon_file, json_file = request.param qpt_ph_modes = QpointPhononModes.from_castep( get_castep_path(material, phonon_file)) expected_qpt_ph_modes = ExpectedQpointPhononModes( os.path.join(get_qpt_ph_modes_dir(material), json_file)) return qpt_ph_modes, expected_qpt_ph_modes
def test_serialise_to_json_file(self, qpt_ph_modes, tmpdir): output_file = str(tmpdir.join('tmp.test')) qpt_ph_modes.to_json_file(output_file) check_json_metadata(output_file, 'QpointPhononModes') deserialised_qpt_ph_modes = QpointPhononModes.from_json_file( output_file) check_qpt_ph_modes(qpt_ph_modes, deserialised_qpt_ph_modes, check_evecs=True)
def test_create_from_phonopy_without_cloader_is_ok(self, material, subdir, phonopy_args, json_file, mocker): # Mock 'from yaml import CLoader as Loader' to raise ImportError import builtins real_import = builtins.__import__ def mocked_import(name, globals, locals, fromlist, level): if name == 'yaml': if fromlist is not None and fromlist[0] == 'CSafeLoader': raise ImportError return real_import(name, globals, locals, fromlist, level) mocker.patch('builtins.__import__', side_effect=mocked_import) phonopy_args['path'] = get_phonopy_path(material, subdir) qpt_ph_modes = QpointPhononModes.from_phonopy(**phonopy_args) json_path = os.path.join(get_qpt_ph_modes_dir(material), json_file) expected_qpt_ph_modes = ExpectedQpointPhononModes(json_path) check_qpt_ph_modes(qpt_ph_modes, expected_qpt_ph_modes, check_evecs=True)
def get_quartz_qpt_ph_modes(): return QpointPhononModes.from_castep( get_castep_path('quartz', 'quartz_nosplit.phonon'))
def serialise_to_dict(self, request): qpt_ph_modes = get_qpt_ph_modes(request.param) # Convert to dict, then back to object to test qpt_ph_modes_dict = qpt_ph_modes.to_dict() qpt_ph_modes_from_dict = QpointPhononModes.from_dict(qpt_ph_modes_dict) return qpt_ph_modes, qpt_ph_modes_from_dict
def get_qpt_ph_modes_from_json(material, file): return QpointPhononModes.from_json_file( os.path.join(get_qpt_ph_modes_dir(material), file))
def test_faulty_object_creation(self, inject_faulty_elements): faulty_args, faulty_kwargs, expected_exception = inject_faulty_elements with pytest.raises(expected_exception): QpointPhononModes(*faulty_args, **faulty_kwargs)
def create_from_dict(self, request): material = request.param expected_qpt_ph_modes = get_expected_qpt_ph_modes(material) qpt_ph_modes = QpointPhononModes.from_dict( expected_qpt_ph_modes.to_dict()) return qpt_ph_modes, expected_qpt_ph_modes
def create_from_constructor_without_weights(self, request): expected_qpt_ph_modes = request.param args, kwargs = expected_qpt_ph_modes.to_constructor_args(weights=False) qpt_ph_modes = QpointPhononModes(*args, **kwargs) return qpt_ph_modes, expected_qpt_ph_modes
def get_si2_qpt_ph_modes(): return QpointPhononModes.from_castep( get_castep_path('Si2-sc-skew', 'Si2-sc-skew.phonon'))
def get_cahgo2_qpt_ph_modes(): return QpointPhononModes.from_phonopy( path=get_phonopy_path('CaHgO2', ''), summary_name='mp-7041-20180417.yaml', phonon_name='qpoints.yaml')
def test_create_from_phonopy_with_bad_inputs_raises_err( self, material, subdir, phonopy_args, err): phonopy_args['path'] = get_phonopy_path(material, subdir) with pytest.raises(err): QpointPhononModes.from_phonopy(**phonopy_args)