Coverage for calorine/tools/phonons.py: 100%
20 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-12-10 08:26 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-12-10 08:26 +0000
1from typing import Any, Dict
3import numpy as np
4from ase import Atoms
5from ase.calculators.singlepoint import SinglePointCalculator
7try:
8 from phonopy import Phonopy
9 from phonopy.structure.atoms import PhonopyAtoms
10except ModuleNotFoundError: # pragma: no cover
11 raise ModuleNotFoundError('phonopy (https://pypi.org/project/phonopy/) is '
12 'required in order to use the functionality '
13 'in the phonons module.'
14 ) # pragma: no cover
17def get_force_constants(structure: Atoms,
18 calculator: SinglePointCalculator,
19 supercell_matrix: np.ndarray,
20 kwargs_phonopy: Dict[str, Any] = {},
21 kwargs_generate_displacements: Dict[str, Any] = {}) -> Phonopy:
22 """
23 Calculates the force constants for a given structure using
24 `phonopy <https://phonopy.github.io/phonopy/>`_, which needs to be cited if this function
25 is used for generating data for publication.
26 The function returns a `Phonopy` object that can be used to calculate, e.g.,
27 the phonon dispersion, the phonon density of states as well as related quantities such
28 as the thermal displacements and the free energy.
30 Parameters
31 ----------
32 structure
33 structure for which to compute the phonon dispersion; usually this is a primitive cell
34 calculator
35 ASE calculator to use for the calculation of forces
36 supercell_matrix
37 specification of supercell size handed over to phonopy;
38 should be a tuple of three values or a matrix
39 kwargs_phonopy
40 *Expert option*:
41 keyword arguments used when initializing the `Phonopy` object;
42 this includes, e.g., the tolerance used when determining the symmetry (`symprec`) and
43 `parameters for the non-analytical corrections
44 <https://phonopy.github.io/phonopy/phonopy-module.html#non-analytical-term-correction>`_
45 (`nac_params`)
46 kwargs_generate_displacements
47 *Expert option*:
48 keyword arguments to be handed over to the `generate_displacements` method;
49 this includes in particular the `distance` keyword, which specifies the
50 magnitude of the atomic displacement imposed when calculating the force constant matrix
51 """
53 # prepare primitive unit cell for phonopy
54 structure_ph = PhonopyAtoms(symbols=structure.symbols,
55 cell=structure.cell,
56 scaled_positions=structure.get_scaled_positions(),
57 pbc=structure.pbc)
59 # make sure we are using the masses intended by the user
60 structure_ph.masses = structure.get_masses()
62 # prepare supercells
63 phonon = Phonopy(structure_ph, supercell_matrix, **kwargs_phonopy)
64 phonon.generate_displacements(**kwargs_generate_displacements)
66 # compute force constant matrix
67 forces = []
68 for structure_ph in phonon.supercells_with_displacements:
69 structure_ase = Atoms(symbols=structure_ph.symbols,
70 cell=structure_ph.cell,
71 scaled_positions=structure_ph.scaled_positions,
72 pbc=structure.pbc)
73 structure_ase.calc = calculator
74 forces.append(structure_ase.get_forces().copy())
76 phonon.forces = forces
77 phonon.produce_force_constants()
79 return phonon