Skip to content

Commit

Permalink
Merge pull request #446 from phonopy/phonopy-load-symfc
Browse files Browse the repository at this point in the history
Symfc as default when phonopy-load
  • Loading branch information
atztogo authored Nov 9, 2024
2 parents ec3ca0e + 35e0d81 commit c55106b
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phonopy-pytest-conda-openmp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
shell: bash -l {0}
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phonopy-pytest-conda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
shell: bash -l {0}
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]

steps:
- uses: actions/checkout@v4
Expand Down
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ http://phonopy.github.io/phonopy/

See https://phonopy.github.io/phonopy/install.html.

## Dependency

It is necessary to install `symfc` and `seekpath` either via pip, conda, etc, to
utilize the respective additional features.

## Mailing list for questions

Usual phonopy questions should be sent to phonopy mailing list
Expand Down
7 changes: 2 additions & 5 deletions doc/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,15 @@ Prepare the following Python libraries:
- matplotlib (>=2.2.2)
- python-yaml (pyyaml >= 5.3)
- python-h5py (h5py >= 3.0)
- spglib (>=2.3)
- scipy
- spglib (>=2.3)
- symfc

It is recommended to install seekpath to plot phonon band structure:

- seekpath


As an external force constants calculator, installation of symfc is recommended:

- symfc

For the CP2K interface, the following package will be needed to install:

- cp2k-input-tools
Expand Down
7 changes: 5 additions & 2 deletions doc/phonopy-load.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ command:
automatically enabled. This can be disabled by `--nonac` option.

5. When force constants are calculated from displacements and forces dataset,
force constants are automatically symmetrized. To disable this, `--no-sym-fc`
option is used.
force constants are automatically symmetrized. From phonopy v2.30.0, `symfc`
is used for the symmetrization. The behavior of the symmetrization before
v2.30.0, `--fc-calculator traditional` has to be specified. `--no-sym-fc`
option can be used to calculate force constants in the traditional force
constants calculator without symmetrization.

6. `--save-params` option is added. With this option, `phonopy_params.yaml` that
contains most of the information to run phonopy, i.e., crystal structure,
Expand Down
47 changes: 14 additions & 33 deletions phonopy/api_phonopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
symmetrize_compact_force_constants,
symmetrize_force_constants,
)
from phonopy.harmonic.force_constants import get_fc2 as get_phonopy_fc2
from phonopy.interface.calculator import get_default_physical_units
from phonopy.interface.fc_calculator import get_fc2
from phonopy.interface.phonopy_yaml import PhonopyYaml
Expand Down Expand Up @@ -101,7 +100,7 @@
isclose,
shape_supercell_matrix,
)
from phonopy.structure.dataset import forces_in_dataset, get_displacements_and_forces
from phonopy.structure.dataset import forces_in_dataset
from phonopy.structure.grid_points import length2mesh
from phonopy.structure.symmetry import Symmetry, symmetrize_borns_and_epsilon
from phonopy.units import VaspToTHz
Expand Down Expand Up @@ -3855,37 +3854,19 @@ def _run_force_constants_from_forces(
decimals=None,
) -> None:
if self._dataset is not None:
if fc_calculator is not None:
disps, forces = get_displacements_and_forces(self._dataset)
self._force_constants = get_fc2(
self._supercell,
self._primitive,
disps,
forces,
fc_calculator=fc_calculator,
fc_calculator_options=fc_calculator_options,
atom_list=distributed_atom_list,
symmetry=self._symmetry,
symprec=self._symprec,
log_level=self._log_level,
)
else:
if "displacements" in self._dataset:
lines = [
"Type-II dataset for displacements and forces was "
"given. Setting fc_calculator",
"(external force constants calculator) is required "
"to produce force constants.",
]
raise RuntimeError("\n".join(lines))
self._force_constants = get_phonopy_fc2(
self._supercell,
self._symmetry,
self._dataset,
atom_list=distributed_atom_list,
primitive=self._primitive,
decimals=decimals,
)
self._force_constants = get_fc2(
self._supercell,
self._primitive,
self._dataset,
fc_calculator=fc_calculator,
fc_calculator_options=fc_calculator_options,
atom_list=distributed_atom_list,
symmetry=self._symmetry,
symprec=self._symprec,
log_level=self._log_level,
)
if decimals:
self._force_constants = self._force_constants.round(decimals=decimals)

def _set_dynamical_matrix(self) -> None:
import phonopy._phonopy as phonoc
Expand Down
15 changes: 8 additions & 7 deletions phonopy/cui/phonopy_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,13 +778,14 @@ def get_parser(fc_symmetry=False, is_nac=False, load_phonopy_yaml=False):
parser.add_argument(
"--sigma", dest="sigma", default=None, help="Smearing width for DOS"
)
parser.add_argument(
"--symfc",
dest="use_symfc",
action="store_true",
default=None,
help="Use symfc for generating force constants",
)
if not fc_symmetry:
parser.add_argument(
"--symfc",
dest="use_symfc",
action="store_true",
default=None,
help="Use symfc for generating force constants",
)
parser.add_argument(
"--symmetry",
dest="is_check_symmetry",
Expand Down
23 changes: 19 additions & 4 deletions phonopy/cui/phonopy_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,9 @@ def read_force_sets_from_phonopy_yaml(phpy_yaml):
print_error()
sys.exit(1)

(fc_calculator, fc_calculator_options) = _get_fc_calculator_params(settings)
(fc_calculator, fc_calculator_options) = _get_fc_calculator_params(
settings, load_phonopy_yaml=False
)

phonon.dataset = force_sets
if log_level:
Expand Down Expand Up @@ -812,7 +814,9 @@ def _store_force_constants(
if cutoff_radius:
phonon.set_force_constants_zero_with_radius(cutoff_radius)

# Enforce space group symmetry to force constants
# This enforces space group symmetry to force constants.
# The force constants are supposed to be read from a file since otherwise
# the force constants are considered to follow space group symmetry.
if settings.fc_spg_symmetry:
if log_level:
print("Force constants are symmetrized by space group operations.")
Expand All @@ -830,7 +834,10 @@ def _store_force_constants(

# Imporse translational invariance and index permulation symmetry to
# force constants
if settings.fc_symmetry:
fc_calculator, _ = _get_fc_calculator_params(
settings, load_phonopy_yaml=load_phonopy_yaml
)
if settings.fc_symmetry and fc_calculator == "traditional":
phonon.symmetrize_force_constants()

# Write FORCE_CONSTANTS
Expand Down Expand Up @@ -1721,12 +1728,20 @@ def _auto_primitive_axes(primitive_matrix):
return isinstance(primitive_matrix, str) and primitive_matrix == "auto"


def _get_fc_calculator_params(settings):
def _get_fc_calculator_params(settings, load_phonopy_yaml=True):
"""Return fc_calculator and fc_calculator_params from settings."""
fc_calculator = None
if settings.fc_calculator is not None:
if settings.fc_calculator.lower() in fc_calculator_names:
fc_calculator = settings.fc_calculator.lower()
else:
if settings.fc_symmetry:
if load_phonopy_yaml:
fc_calculator = "symfc"
else:
fc_calculator = "traditional"
else:
fc_calculator = "traditional"

fc_calculator_options = None
if settings.fc_calculator_options is not None:
Expand Down
4 changes: 0 additions & 4 deletions phonopy/harmonic/force_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def get_fc2(
dataset,
atom_list=None,
primitive: Optional[Primitive] = None,
decimals=None,
):
"""Force constants are computed.
Expand Down Expand Up @@ -122,9 +121,6 @@ def get_fc2(
atom_list=atom_list,
)

if decimals:
force_constants = force_constants.round(decimals=decimals)

return force_constants


Expand Down
40 changes: 31 additions & 9 deletions phonopy/interface/fc_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,24 @@

from phonopy.structure.atoms import PhonopyAtoms
from phonopy.structure.cells import Primitive
from phonopy.structure.dataset import get_displacements_and_forces
from phonopy.structure.symmetry import Symmetry

fc_calculator_names = {"alm": "ALM", "hiphive": "hiPhive", "symfc": "symfc"}
fc_calculator_names = {
"alm": "ALM",
"hiphive": "hiPhive",
"symfc": "symfc",
"traditional": "traditional",
"none": "none",
}


# get_fc2 is called from
# phonopy.api_phonopy.Phonopy._run_force_constants_from_forces.
def get_fc2(
supercell: PhonopyAtoms,
primitive: Primitive,
displacements: np.ndarray,
forces: np.ndarray,
dataset: dict,
fc_calculator: Optional[str] = None,
fc_calculator_options: Optional[str] = None,
atom_list: Optional[Union[Sequence[int], np.ndarray]] = None,
Expand All @@ -73,12 +79,8 @@ def get_fc2(
Supercell
primitive : Primitive
Primitive cell
displacements : array_like
Displacements of atoms in supercell.
shape=(num_snapshots, num_atoms, 3), dtype='double', order='C'
forces : array_like
Forces of atoms in supercell.
shape=(num_snapshots, num_atoms, 3), dtype='double', order='C'
dataset : dict
Dataset that contains displacements, forces, and optionally energies.
fc_calculator : str, optional
Currently only 'alm' is supported. Default is None, meaning invoking
'alm'.
Expand Down Expand Up @@ -108,6 +110,26 @@ def get_fc2(
shape=(len(atom_list), num_atoms, 3, 3), dtype='double', order='C'.
"""
displacements, forces = get_displacements_and_forces(dataset)
if fc_calculator is None or fc_calculator == "traditional":
from phonopy.harmonic.force_constants import get_fc2

if "displacements" in dataset:
lines = [
"Type-II dataset for displacements and forces was "
"given. Setting fc_calculator",
"(external force constants calculator) is required "
"to produce force constants.",
]
raise RuntimeError("\n".join(lines))
return get_fc2(
supercell,
symmetry,
dataset,
atom_list=atom_list,
primitive=primitive,
)

if fc_calculator == "alm" or fc_calculator is None:
from phonopy.interface.alm import get_fc2

Expand Down
4 changes: 1 addition & 3 deletions phonopy/interface/symfc.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ def run_symfc(
" Symfc start "
"-------------------------------"
)
print(
"Symfc is a non-trivial force constants calculator. Please cite the paper:"
)
print("Symfc is a force constants calculator. See the following paper:")
print("A. Seko and A. Togo, arXiv:2403.03588.")
print("Symfc is developed at https://github.com/symfc/symfc.")
print(f"Computing {_orders} order force constants.", flush=True)
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ dependencies = [
"matplotlib>=2.2.2",
"h5py>=3.0",
"spglib>=2.3",
"scipy"
"scipy",
"symfc",
]
license = { file = "LICENSE" }

Expand All @@ -26,9 +27,8 @@ Repository = "https://github.com/phonopy/phonopy"
[project.optional-dependencies]
cp2k = ["cp2k-input-tools"]
seekpath = ["seekpath"]
symfc = ["symfc"]
pypolymlp = ["pypolymlp"]
tools = ["seekpath", "symfc", "pypolymlp"]
tools = ["seekpath", "pypolymlp"]

[project.scripts]
phonopy = "phonopy.scripts.phonopy:run"
Expand Down

0 comments on commit c55106b

Please sign in to comment.