Skip to content

Commit

Permalink
Merge branch 'develop' into publish-gh-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
atztogo committed Jun 7, 2024
2 parents e484fae + 5d1efbc commit e6c2e86
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 71 deletions.
4 changes: 4 additions & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

# Change Log

## Jun-7-2024: Version 3.1.0

- Change to write forces in phono3py-yaml as default.

## Jun-7-2024: Version 3.0.4

- Bug fix when handling different supercell size of fc2 than that of fc3.
Expand Down
2 changes: 1 addition & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
# The short X.Y version.
version = "3.0"
# The full version, including alpha/beta/rc tags.
release = "3.0.4"
release = "3.1.0"

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
43 changes: 22 additions & 21 deletions phono3py/api_phono3py.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
directions_to_displacement_dataset,
get_least_displacements,
)
from phonopy.harmonic.dynamical_matrix import DynamicalMatrix
from phonopy.harmonic.force_constants import get_fc2 as get_phonopy_fc2
from phonopy.harmonic.force_constants import (
set_permutation_symmetry,
Expand Down Expand Up @@ -179,7 +180,7 @@ def __init__(
Supercell matrix used for fc2. In phono3py, supercell matrix for fc3
and fc2 can be different to support longer range interaction of fc2
than that of fc3. Unless setting this, supercell_matrix is used.
This is only valide when unitcell or unitcell_filename is given.
This is only valid when unitcell or unitcell_filename is given.
Default is None.
cutoff_frequency : float, optional
Phonon frequency below this value is ignored when the cutoff is
Expand Down Expand Up @@ -297,7 +298,7 @@ def __init__(
self._set_band_indices()

@property
def version(self):
def version(self) -> str:
"""Return phono3py release version number.
str
Expand All @@ -317,7 +318,7 @@ def calculator(self) -> Optional[str]:
return self._calculator

@property
def fc3(self):
def fc3(self) -> Optional[np.ndarray]:
"""Setter and getter of third order force constants (fc3).
ndarray
Expand All @@ -334,7 +335,7 @@ def fc3(self, fc3):
self._fc3 = fc3

@property
def fc2(self):
def fc2(self) -> Optional[np.ndarray]:
"""Setter and getter of second order force constants (fc2).
ndarray
Expand All @@ -351,12 +352,12 @@ def fc2(self, fc2):
self._fc2 = fc2

@property
def force_constants(self):
def force_constants(self) -> Optional[np.ndarray]:
"""Return fc2. This is same as the getter attribute `fc2`."""
return self.fc2

@property
def sigmas(self):
def sigmas(self) -> list:
"""Setter and getter of smearing widths.
list
Expand Down Expand Up @@ -386,7 +387,7 @@ def sigmas(self, sigmas):
self._sigmas.append(None)

@property
def sigma_cutoff(self):
def sigma_cutoff(self) -> Optional[float]:
"""Setter and getter of Smearing cutoff width.
This is given as a multiple of the standard deviation.
Expand All @@ -403,7 +404,7 @@ def sigma_cutoff(self, sigma_cutoff):
self._sigma_cutoff = sigma_cutoff

@property
def nac_params(self):
def nac_params(self) -> Optional[dict]:
"""Setter and getter of parameters for non-analytical term correction.
dict
Expand All @@ -427,7 +428,7 @@ def nac_params(self, nac_params):
self._init_dynamical_matrix()

@property
def dynamical_matrix(self):
def dynamical_matrix(self) -> Optional[DynamicalMatrix]:
"""Return DynamicalMatrix instance.
This is not dynamical matrices but the instance of DynamicalMatrix
Expand Down Expand Up @@ -522,7 +523,7 @@ def phonon_supercell_symmetry(self) -> Symmetry:
return self._phonon_supercell_symmetry

@property
def supercell_matrix(self):
def supercell_matrix(self) -> np.ndarray:
"""Return transformation matrix to supercell cell from unit cell.
ndarray
Expand All @@ -533,7 +534,7 @@ def supercell_matrix(self):
return self._supercell_matrix

@property
def phonon_supercell_matrix(self):
def phonon_supercell_matrix(self) -> Optional[np.ndarray]:
"""Return transformation matrix to phonon supercell from unit cell.
ndarray
Expand All @@ -544,7 +545,7 @@ def phonon_supercell_matrix(self):
return self._phonon_supercell_matrix

@property
def primitive_matrix(self):
def primitive_matrix(self) -> np.ndarray:
"""Return transformation matrix to primitive cell from unit cell.
ndarray
Expand All @@ -555,7 +556,7 @@ def primitive_matrix(self):
return self._primitive_matrix

@property
def unit_conversion_factor(self):
def unit_conversion_factor(self) -> float:
"""Return phonon frequency unit conversion factor.
float
Expand All @@ -567,7 +568,7 @@ def unit_conversion_factor(self):
return self._frequency_factor_to_THz

@property
def dataset(self):
def dataset(self) -> Optional[dict]:
"""Setter and getter of displacement-force dataset.
dict
Expand Down Expand Up @@ -629,7 +630,7 @@ def dataset(self, dataset):
self._phonon_supercells_with_displacements = None

@property
def phonon_dataset(self):
def phonon_dataset(self) -> Optional[dict]:
"""Setter and getter of displacement-force dataset for fc2.
dict
Expand Down Expand Up @@ -674,10 +675,10 @@ def phonon_dataset(self, dataset):
self._phonon_supercells_with_displacements = None

@property
def band_indices(self):
def band_indices(self) -> list[np.ndarray]:
"""Setter and getter of band indices.
array_like
list[np.ndarray]
List of band indices specified to select specific bands
to computer ph-ph interaction related properties.
Expand All @@ -693,11 +694,11 @@ def _set_band_indices(self, band_indices=None):
num_band = len(self._primitive) * 3
self._band_indices = [np.arange(num_band, dtype="int_")]
else:
self._band_indices = band_indices
self._band_indices = [np.array(bi, dtype="int_") for bi in band_indices]
self._band_indices_flatten = np.hstack(self._band_indices).astype("int_")

@property
def masses(self):
def masses(self) -> np.ndarray:
"""Setter and getter of atomic masses of primitive cell."""
return self._primitive.masses

Expand All @@ -719,7 +720,7 @@ def masses(self, masses):
self._phonon_supercell.masses = s_masses

@property
def supercells_with_displacements(self):
def supercells_with_displacements(self) -> list[PhonopyAtoms]:
"""Return supercells with displacements.
list of PhonopyAtoms
Expand Down Expand Up @@ -2038,7 +2039,7 @@ def save(self, filename="phono3py_params.yaml", settings=None):
the settings expected to be updated from the following
default settings are needed to be set in the dictionary.
The possible parameters and their default settings are:
{'force_sets': False,
{'force_sets': True,
'displacements': True,
'force_constants': False,
'born_effective_charge': True,
Expand Down
21 changes: 15 additions & 6 deletions phono3py/cui/create_force_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import annotations

import copy
import os
import pathlib
import sys
from typing import Optional

Expand Down Expand Up @@ -247,7 +250,7 @@ def parse_forces(
# None is returned unless type-2.
# can emit FileNotFoundError.
if dataset is None or dataset is not None and not forces_in_dataset(dataset):
_dataset = _get_type2_dataset(
_dataset = read_type2_dataset(
natom, filename=force_filename, log_level=log_level
)
# Do not overwrite dataset when _dataset is None.
Expand Down Expand Up @@ -311,13 +314,18 @@ def parse_forces(
return dataset


def forces_in_dataset(dataset):
def forces_in_dataset(dataset: dict) -> bool:
"""Return whether forces in dataset or not."""
return "forces" in dataset or (
"first_atoms" in dataset and "forces" in dataset["first_atoms"][0]
)


def displacements_in_dataset(dataset: dict) -> bool:
"""Return whether displacements in dataset or not."""
return "displacements" in dataset or "first_atoms" in dataset


def get_fc_calculator_params(settings):
"""Return fc_calculator and fc_calculator_params from settings."""
fc_calculator = None
Expand All @@ -338,7 +346,7 @@ def get_fc_calculator_params(settings):
return fc_calculator, fc_calculator_options


def _read_phono3py_fc3(phono3py, symmetrize_fc3r, input_filename, log_level):
def _read_phono3py_fc3(phono3py: Phono3py, symmetrize_fc3r, input_filename, log_level):
if input_filename is None:
filename = "fc3.hdf5"
else:
Expand All @@ -357,7 +365,7 @@ def _read_phono3py_fc3(phono3py, symmetrize_fc3r, input_filename, log_level):
if log_level:
print_error()
sys.exit(1)
num_atom = phono3py.supercell.get_number_of_atoms()
num_atom = len(phono3py.supercell)
if fc3.shape[1] != num_atom:
print("Matrix shape of fc3 doesn't agree with supercell size.")
if log_level:
Expand Down Expand Up @@ -407,8 +415,9 @@ def _read_phono3py_fc2(phono3py, symmetrize_fc2, input_filename, log_level):
phono3py.fc2 = phonon_fc2


def _get_type2_dataset(natom, filename="FORCES_FC3", log_level=0):
if not os.path.isfile(filename):
def read_type2_dataset(natom, filename="FORCES_FC3", log_level=0) -> Optional[dict]:
"""Read type-2 FORCES_FC3."""
if not pathlib.Path(filename).exists():
return None

with open(filename, "r") as f:
Expand Down
54 changes: 31 additions & 23 deletions phono3py/cui/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@
from phonopy.structure.cells import determinant

from phono3py import Phono3py
from phono3py.cui.create_force_constants import forces_in_dataset, parse_forces
from phono3py.cui.create_force_constants import (
displacements_in_dataset,
forces_in_dataset,
parse_forces,
read_type2_dataset,
)
from phono3py.file_IO import read_fc2_from_hdf5, read_fc3_from_hdf5
from phono3py.interface.phono3py_yaml import Phono3pyYaml
from phono3py.phonon3.fc3 import show_drift_fc3
Expand Down Expand Up @@ -494,28 +499,31 @@ def _set_dataset_or_fc3(
read_fc3 = True
if log_level:
print('fc3 was read from "fc3.hdf5".')
elif pathlib.Path("FORCES_FC3").exists():
_set_dataset_for_fc3(
ph3py,
ph3py_yaml,
"FORCES_FC3",
phono3py_yaml_filename,
cutoff_pair_distance,
log_level,
)
elif (
ph3py_yaml is not None
and ph3py_yaml.dataset is not None
and forces_in_dataset(ph3py_yaml.dataset)
):
_set_dataset_for_fc3(
ph3py,
ph3py_yaml,
None,
phono3py_yaml_filename,
cutoff_pair_distance,
log_level,
)
elif dataset := read_type2_dataset(
len(ph3py.supercell), "FORCES_FC3", log_level=log_level
): # := Assignment Expressions (Python>=3.8)
ph3py.dataset = dataset
elif ph3py_yaml is not None and ph3py_yaml.dataset is not None:
if pathlib.Path("FORCES_FC3").exists() and displacements_in_dataset(
ph3py_yaml.dataset
):
_set_dataset_for_fc3(
ph3py,
ph3py_yaml,
"FORCES_FC3",
phono3py_yaml_filename,
cutoff_pair_distance,
log_level,
)
if forces_in_dataset(ph3py_yaml.dataset):
_set_dataset_for_fc3(
ph3py,
ph3py_yaml,
None,
phono3py_yaml_filename,
cutoff_pair_distance,
log_level,
)
return read_fc3


Expand Down
41 changes: 25 additions & 16 deletions phono3py/cui/phono3py_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,14 +541,7 @@ def store_force_constants(
log_level=log_level,
)
except ForceCalculatorRequiredError:
if log_level:
print("")
print(
"Built-in force constants calculator doesn't support the "
"dispalcements-forces dataset. "
"An external force calculator, e.g., ALM (--alm), has to be used "
"to compute force constants."
)
_show_fc_calculator_not_found(log_level)

if log_level:
if phono3py.fc3 is None:
Expand Down Expand Up @@ -592,15 +585,31 @@ def store_force_constants(
if log_level:
print('fc2 was written into "fc2.hdf5".')
else:
create_phono3py_force_constants(
phono3py,
settings,
ph3py_yaml=ph3py_yaml,
phono3py_yaml_filename=phono3py_yaml_filename,
input_filename=input_filename,
output_filename=output_filename,
log_level=log_level,
try:
create_phono3py_force_constants(
phono3py,
settings,
ph3py_yaml=ph3py_yaml,
phono3py_yaml_filename=phono3py_yaml_filename,
input_filename=input_filename,
output_filename=output_filename,
log_level=log_level,
)
except ForceCalculatorRequiredError:
_show_fc_calculator_not_found(log_level)


def _show_fc_calculator_not_found(log_level):
if log_level:
print("")
print(
"Built-in force constants calculator doesn't support the "
"dispalcements-forces dataset. "
"An external force calculator, e.g., ALM (--alm), has to be used "
"to compute force constants."
)
print_error()
sys.exit(1)


def run_gruneisen_then_exit(phono3py, settings, output_filename, log_level):
Expand Down
Loading

0 comments on commit e6c2e86

Please sign in to comment.