Further refactoring

This commit is contained in:
Atsushi Togo 2025-07-19 18:19:29 +09:00
parent dd9f626bb8
commit 7dc202dde7
4 changed files with 82 additions and 30 deletions

View File

@ -57,6 +57,7 @@ from phono3py.cui.create_force_constants import (
from phono3py.file_IO import read_fc2_from_hdf5, read_fc3_from_hdf5
from phono3py.interface.fc_calculator import (
extract_fc2_fc3_calculators,
extract_fc2_fc3_calculators_options,
update_cutoff_fc_calculator_options,
)
from phono3py.interface.phono3py_yaml import Phono3pyYaml
@ -292,6 +293,7 @@ def load(
elif phono3py_yaml is not None:
ph3py_yaml = Phono3pyYaml()
ph3py_yaml.read(phono3py_yaml)
assert ph3py_yaml.unitcell is not None
cell = ph3py_yaml.unitcell.copy()
_calculator = ph3py_yaml.calculator
smat = ph3py_yaml.supercell_matrix
@ -366,6 +368,7 @@ def load(
)
if use_pypolymlp and ph3py.fc3 is None and forces_in_dataset(ph3py.dataset):
assert ph3py.dataset is not None
ph3py.mlp_dataset = ph3py.dataset
ph3py.dataset = None
@ -430,11 +433,11 @@ def compute_force_constants_from_datasets(
"""
fc3_calculator = extract_fc2_fc3_calculators(fc_calculator, 3)
fc2_calculator = extract_fc2_fc3_calculators(fc_calculator, 2)
fc3_calc_opts = extract_fc2_fc3_calculators(fc_calculator_options, 3)
fc3_calc_opts = extract_fc2_fc3_calculators_options(fc_calculator_options, 3)
fc3_calc_opts = update_cutoff_fc_calculator_options(
fc3_calc_opts, cutoff_pair_distance
)
fc2_calc_opts = extract_fc2_fc3_calculators(fc_calculator_options, 2)
fc2_calc_opts = extract_fc2_fc3_calculators_options(fc_calculator_options, 2)
exist_fc2 = ph3py.fc2 is not None
if ph3py.fc3 is None and forces_in_dataset(ph3py.dataset):
ph3py.produce_fc3(

View File

@ -651,7 +651,8 @@ def _produce_force_constants(
except ForceCalculatorRequiredError as e:
if load_phono3py_yaml:
if log_level:
print("Symfc will be used to handle general (or random) displacements.")
print(str(e))
print("Try symfc to handle general (or random) displacements.")
else:
print_error_message(str(e))
if log_level:
@ -1262,13 +1263,22 @@ def main(**argparse_control):
# polynomial MLPs #
###################
if settings.use_pypolymlp:
_run_pypolymlp(
ph3py,
settings,
confs_dict,
output_yaml_filename=output_yaml_filename,
log_level=log_level,
)
if ph3py.fc3 is None or (
ph3py.fc2 is None and ph3py.phonon_supercell_matrix is None
):
_run_pypolymlp(
ph3py,
settings,
confs_dict,
output_yaml_filename=output_yaml_filename,
log_level=log_level,
)
else:
if log_level:
print(
"Pypolymlp was not developed or used because fc2 and fc3 "
"are available."
)
###########################
# Produce force constants #

View File

@ -63,11 +63,13 @@ def show_general_settings(
phonon_supercell_matrix = phono3py.phonon_supercell_matrix
print("-" * 29 + " General settings " + "-" * 29)
if run_mode:
if settings.use_pypolymlp:
print(f"Run mode: pypolymlp & {run_mode}")
if settings.use_pypolymlp:
if settings.create_displacements or settings.random_displacements is not None:
print(f"Run mode: pypolymlp + {run_mode}")
else:
print(f"Run mode: {run_mode}")
print("Run mode: pypolymlp")
else:
print(f"Run mode: {run_mode}")
if settings.hdf5_compression:
print(f"HDF5 data compression filter: {settings.hdf5_compression}")
if interface_mode:

View File

@ -116,14 +116,15 @@ class FC3Solver(FCSolver):
def _get_displacements_and_forces(self):
"""Return displacements and forces for fc3."""
assert self._dataset is not None
return get_displacements_and_forces_fc3(self._dataset)
def extract_fc2_fc3_calculators(
fc_calculator: Literal["traditional", "symfc", "alm"] | str | None,
order: int,
) -> Literal["traditional", "symfc", "alm"] | str | None:
"""Extract fc_calculator and fc_calculator_options for fc2 and fc3.
) -> Literal["traditional", "symfc", "alm"] | None:
"""Extract fc_calculator for fc2 and fc3.
fc_calculator : str
FC calculator. "|" separates fc2 and fc3. First and last
@ -133,20 +134,51 @@ def extract_fc2_fc3_calculators(
"""
if fc_calculator is None:
return fc_calculator
elif isinstance(fc_calculator, str):
if "|" in fc_calculator:
_fc_calculator = fc_calculator.split("|")[order - 2]
if _fc_calculator == "":
return None
else:
if fc_calculator.strip() == "":
return None
else:
_fc_calculator = fc_calculator
return _fc_calculator
return None
else:
raise RuntimeError("fc_calculator should be str or None.")
_fc_calculator = _split_fc_calculator_str(fc_calculator, order)
if _fc_calculator is None:
return None
fc_calculator_lower = _fc_calculator.lower()
if fc_calculator_lower not in ("traditional", "symfc", "alm"):
raise ValueError(
f"Unknown fc_calculator: {_fc_calculator}. "
"Available calculators are 'traditional', 'symfc', and 'alm'."
)
return fc_calculator_lower
def extract_fc2_fc3_calculators_options(
fc_calculator_opts: str | None,
order: int,
) -> str | None:
"""Extract fc_calculator_options for fc2 and fc3.
fc_calculator_opts : str
FC calculator options. "|" separates fc2 and fc3. First and last
parts separated correspond to fc2 and fc3 calculators, respectively.
order : int = 2 or 3
2 and 3 indicate fc2 and fc3, respectively.
"""
if fc_calculator_opts is None:
return None
else:
_fc_calculator_opts = _split_fc_calculator_str(fc_calculator_opts, order)
return _fc_calculator_opts
def _split_fc_calculator_str(fc_calculator: str, order: int) -> str | None:
if "|" in fc_calculator:
_fc_calculator = fc_calculator.split("|")[order - 2]
if _fc_calculator == "":
return None
else:
if fc_calculator.strip() == "":
return None
else:
_fc_calculator = fc_calculator
return _fc_calculator
def update_cutoff_fc_calculator_options(
@ -235,6 +267,11 @@ def determine_cutoff_pair_distance(
"available for symfc calculator."
)
symfc_options = {"memsize": {3: _symfc_memory_size}}
if supercell is None or primitive is None or symmetry is None:
raise RuntimeError(
"supercell, primitive, and symmetry are required to estimate "
"cutoff_pair_distance by memory size."
)
update_symfc_cutoff_by_memsize(
symfc_options, supercell, primitive, symmetry, verbose=log_level > 0
)
@ -283,7 +320,7 @@ def _get_cutoff_pair_distance(
cutoff_pair_distance,
)
symfc_options = parse_symfc_options(
extract_fc2_fc3_calculators(_fc_calculator_options, 3), 3
extract_fc2_fc3_calculators_options(_fc_calculator_options, 3), 3
)
_cutoff_pair_distance = cutoff_pair_distance