Skip to content

Commit

Permalink
fix the ruff
Browse files Browse the repository at this point in the history
Signed-off-by: Runze Liu <[email protected]>
  • Loading branch information
rul048 authored Aug 30, 2024
1 parent 1e7dc4c commit 58cf5de
Showing 1 changed file with 88 additions and 29 deletions.
117 changes: 88 additions & 29 deletions matcalc/qha.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from ase.calculators.calculator import Calculator
from pymatgen.core import Structure


@dataclass
class QHACalc(PropCalc):

Check warning on line 25 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L24-L25

Added lines #L24 - L25 were not covered by tests
"""Calculator for phonon properties under quasi-harmonic approximation.
Expand Down Expand Up @@ -68,7 +69,19 @@ class QHACalc(PropCalc):
relax_structure: bool = True
relax_calc_kwargs: dict | None = None
phonon_calc_kwargs: dict | None = None
scale_factors: Sequence[float] = (0.95, 0.96, 0.97, 0.98, 0.99, 1.00, 1.01, 1.02, 1.03, 1.04, 1.05)
scale_factors: Sequence[float] = (

Check warning on line 72 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L62-L72

Added lines #L62 - L72 were not covered by tests
0.95,
0.96,
0.97,
0.98,
0.99,
1.00,
1.01,
1.02,
1.03,
1.04,
1.05,
)
write_helmholtz_volume: bool | str | Path = False
write_volume_temperature: bool | str | Path = False
write_thermal_expansion: bool | str | Path = False
Expand All @@ -82,14 +95,46 @@ def __post_init__(self) -> None:
"""Set default paths for where to save output files."""
# map True to canonical default path, False to "" and Path to str
for key, val, default_path in (

Check warning on line 97 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L97

Added line #L97 was not covered by tests
("write_helmholtz_volume", self.write_helmholtz_volume, "helmholtz_volume.dat"),
("write_volume_temperature", self.write_volume_temperature, "volume_temperature.dat"),
("write_thermal_expansion", self.write_thermal_expansion, "thermal_expansion.dat"),
("write_gibbs_temperature", self.write_gibbs_temperature, "gibbs_temperature.dat"),
("write_bulk_modulus_temperature", self.write_bulk_modulus_temperature, "bulk_modulus_temperature.dat"),
("write_heat_capacity_p_numerical", self.write_heat_capacity_p_numerical, "Cp_temperature.dat"),
("write_heat_capacity_p_polyfit", self.write_heat_capacity_p_polyfit, "Cp_temperature_polyfit.dat"),
("write_gruneisen_temperature", self.write_gruneisen_temperature, "gruneisen_temperature.dat"),
(
"write_helmholtz_volume",
self.write_helmholtz_volume,
"helmholtz_volume.dat",
),
(
"write_volume_temperature",
self.write_volume_temperature,
"volume_temperature.dat",
),
(
"write_thermal_expansion",
self.write_thermal_expansion,
"thermal_expansion.dat",
),
(
"write_gibbs_temperature",
self.write_gibbs_temperature,
"gibbs_temperature.dat",
),
(
"write_bulk_modulus_temperature",
self.write_bulk_modulus_temperature,
"bulk_modulus_temperature.dat",
),
(
"write_heat_capacity_p_numerical",
self.write_heat_capacity_p_numerical,
"Cp_temperature.dat",
),
(
"write_heat_capacity_p_polyfit",
self.write_heat_capacity_p_polyfit,
"Cp_temperature_polyfit.dat",
),
(
"write_gruneisen_temperature",
self.write_gruneisen_temperature,
"gruneisen_temperature.dat",
),
):
setattr(self, key, str({True: default_path, False: ""}.get(val, val))) # type: ignore[arg-type]

Check warning on line 139 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L139

Added line #L139 was not covered by tests

Expand All @@ -102,31 +147,35 @@ def calc(self, structure: Structure) -> dict:
Returns:
{
"qha": Phonopy.qha object,
"scale_factors": list of scale factors of lattice constants,
"volumes": list of unit cell volumes at corresponding scale factors (in Angstrom^3),
"electronic_energies": list of electronic energies at corresponding volumes (in eV),
"temperatures": list of temperatures in ascending order (in Kelvin),
"thermal_expansion_coefficients": list of volumetric thermal expansion coefficients at corresponding
"scale_factors": List of scale factors of lattice constants,
"volumes": List of unit cell volumes at corresponding scale factors (in Angstrom^3),
"electronic_energies": List of electronic energies at corresponding volumes (in eV),
"temperatures": List of temperatures in ascending order (in Kelvin),
"thermal_expansion_coefficients": List of volumetric thermal expansion coefficients at corresponding
temperatures (in Kelvin^-1),
"gibbs_free_energies": list of Gibbs free energies at corresponding temperatures (in eV),
"bulk_modulus_P": list of bulk modulus at constant presure at corresponding temperatures (in GPa),
"heat_capacity_P": list of heat capacities at constant pressure at corresponding temperatures (in J/K/mol),
"gruneisen_parameters": list of Gruneisen parameters at corresponding temperatures,
"gibbs_free_energies": List of Gibbs free energies at corresponding temperatures (in eV),
"bulk_modulus_P": List of bulk modulus at constant presure at corresponding temperatures (in GPa),
"heat_capacity_P": List of heat capacities at constant pressure at corresponding temperatures (in J/K/mol),
"gruneisen_parameters": List of Gruneisen parameters at corresponding temperatures,
}
"""
if self.relax_structure:
relaxer = RelaxCalc(

Check warning on line 163 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L162-L163

Added lines #L162 - L163 were not covered by tests
self.calculator, fmax=self.fmax, optimizer=self.optimizer, **(self.relax_calc_kwargs or {})
self.calculator,
fmax=self.fmax,
optimizer=self.optimizer,
**(self.relax_calc_kwargs or {}),
)
structure = relaxer.calc(structure)["final_structure"]

Check warning on line 169 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L169

Added line #L169 was not covered by tests

temperatures = np.arange(self.t_min, self.t_max + self.t_step, self.t_step)
volumes, electronic_energies, free_energies, entropies, heat_capacities = self._collect_properties(structure)

Check warning on line 172 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L171-L172

Added lines #L171 - L172 were not covered by tests

qha = self._create_qha(volumes, electronic_energies, free_energies, entropies, heat_capacities)
qha = self._create_qha(volumes, electronic_energies, temperatures, free_energies, entropies, heat_capacities)

Check warning on line 174 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L174

Added line #L174 was not covered by tests

self._write_output_files(qha)

Check warning on line 176 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L176

Added line #L176 was not covered by tests

return self._generate_output_dict(qha, volumes, electronic_energies)
return self._generate_output_dict(qha, volumes, electronic_energies, temperatures)

Check warning on line 178 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L178

Added line #L178 was not covered by tests

def _collect_properties(self, structure: Structure) -> tuple[list, list, list, list, list]:

Check warning on line 180 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L180

Added line #L180 was not covered by tests
"""Helper to collect properties like volumes, electronic energies, and thermal properties.
Expand Down Expand Up @@ -164,7 +213,7 @@ def _scale_structure(self, structure: Structure, scale_factor: float) -> Structu
Pymatgen structure with scaled lattice constants.
"""
struct = copy.deepcopy(structure)
struct.scale_lattice(struct.volume * scale_factor ** 3)
struct.scale_lattice(struct.volume * scale_factor**3)
return struct

Check warning on line 217 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L215-L217

Added lines #L215 - L217 were not covered by tests

def _calculate_energy(self, structure: Structure) -> float:

Check warning on line 219 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L219

Added line #L219 was not covered by tests
Expand All @@ -189,24 +238,31 @@ def _calculate_thermal_properties(self, structure: Structure) -> dict:
Dictionary of thermal properties containing free energies, entropies and heat capacities.
"""
phonon_calc = PhononCalc(

Check warning on line 240 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L240

Added line #L240 was not covered by tests
self.calculator, t_step=self.t_step, t_max=self.t_max, t_min=self.t_min,
relax_structure=False, write_phonon=False, **(self.phonon_calc_kwargs or {})
self.calculator,
t_step=self.t_step,
t_max=self.t_max,
t_min=self.t_min,
relax_structure=False,
write_phonon=False,
**(self.phonon_calc_kwargs or {}),
)
return phonon_calc.calc(structure)["thermal_properties"]

Check warning on line 249 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L249

Added line #L249 was not covered by tests

def _create_qha(

Check warning on line 251 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L251

Added line #L251 was not covered by tests
self,
volumes: list,
electronic_energies: list,
temperatures: list,
free_energies: list,
entropies: list,
heat_capacities: list
heat_capacities: list,
) -> PhonopyQHA:
"""Helper to create a PhonopyQHA object for quasi-harmonic approximation.
Args:
volumes: List of volumes corresponding to different scale factors.
electronic_energies: List of electronic energies corresponding to different volumes.
temperatures: List of temperatures in ascending order (in Kelvin).
free_energies: List of free energies corresponding to different volumes and temperatures.
entropies: List of entropies corresponding to different volumes and temperatures.
heat_capacities: List of heat capacities corresponding to different volumes and temperatures.
Expand All @@ -217,12 +273,12 @@ def _create_qha(
return PhonopyQHA(

Check warning on line 273 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L273

Added line #L273 was not covered by tests
volumes=volumes,
electronic_energies=electronic_energies,
temperatures=np.arange(self.t_min, self.t_max + self.t_step, self.t_step),
temperatures=temperatures,
free_energy=np.transpose(free_energies),
entropy=np.transpose(entropies),
cv=np.transpose(heat_capacities),
eos=self.eos,
t_max=self.t_max
t_max=self.t_max,
)

def _write_output_files(self, qha: PhonopyQHA) -> None:

Check warning on line 284 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L284

Added line #L284 was not covered by tests
Expand All @@ -248,13 +304,16 @@ def _write_output_files(self, qha: PhonopyQHA) -> None:
if self.write_gruneisen_temperature:
qha.write_gruneisen_temperature(filename=self.write_gruneisen_temperature)

Check warning on line 305 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L290-L305

Added lines #L290 - L305 were not covered by tests

def _generate_output_dict(self, qha: PhonopyQHA, volumes: list, electronic_energies: list) -> dict:
def _generate_output_dict(

Check warning on line 307 in matcalc/qha.py

View check run for this annotation

Codecov / codecov/patch

matcalc/qha.py#L307

Added line #L307 was not covered by tests
self, qha: PhonopyQHA, volumes: list, electronic_energies: list, temperatures: list
) -> dict:
"""Helper to generate the output dictionary after QHA calculation.
Args:
qha: Phonopy.qha object.
volumes: List of volumes corresponding to different scale factors.
electronic_energies: List of electronic energies corresponding to different volumes.
temperatures: List of temperatures in ascending order (in Kelvin).
Returns:
Dictionary containing the results of QHA calculation.
Expand All @@ -264,7 +323,7 @@ def _generate_output_dict(self, qha: PhonopyQHA, volumes: list, electronic_energ
"scale_factors": self.scale_factors,
"volumes": volumes,
"electronic_energies": electronic_energies,
"temperatures": qha.temperatures,
"temperatures": temperatures,
"thermal_expansion_coefficients": qha.thermal_expansion,
"gibbs_free_energies": qha.gibbs_temperature,
"bulk_modulus_P": qha.bulk_modulus_temperature,
Expand Down

0 comments on commit 58cf5de

Please sign in to comment.