Skip to content

Commit

Permalink
Refactoring of create_FORCE_SETS
Browse files Browse the repository at this point in the history
  • Loading branch information
atztogo committed Oct 9, 2024
1 parent f0f524c commit 440aef7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
36 changes: 29 additions & 7 deletions phonopy/cui/create_force_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,27 @@ def create_FORCE_SETS(
)
force_sets = calc_dataset["forces"]
if "points" in calc_dataset:
if filename := _check_agreements_of_displacements(
supercell, disp_dataset, calc_dataset["points"], force_filenames
if force_sets_zero_mode:
range_start = 1
else:
range_start = 0
if filename := check_agreements_of_displacements(
supercell,
disp_dataset,
calc_dataset["points"][range_start:],
force_filenames[range_start:],
):
raise RuntimeError(
f'Displacements don\'t match with atomic positions in "{filename}".'
)
if force_sets_zero_mode:
if check_agreement_of_supercell_positions(
supercell, calc_dataset["points"][0]
):
raise RuntimeError(
"Supercell doesn't match with atomic positions in "
f'"{force_filenames[0]}".'
)

if interface_mode == "lammps":
rotate_lammps_forces(force_sets, supercell.cell, verbose=(log_level > 0))
Expand Down Expand Up @@ -196,18 +211,16 @@ def check_number_of_force_files(num_displacements, force_filenames, disp_filenam
"""
if num_displacements != len(force_filenames):
print("")
print("Number of files to be read (%d) don't match to" % len(force_filenames))
print(f"Number of files to be read ({len(force_filenames)}) don't match to")
print(
"the number of displacements (%d) in %s."
% (num_displacements, disp_filename)
f'the number of displacements ({num_displacements}) in "{disp_filename}".'
)
return False
else:
return True


def _check_agreements_of_displacements(
def check_agreements_of_displacements(
supercell: PhonopyAtoms,
dataset: dict,
all_points: list[np.ndarray],
Expand All @@ -224,6 +237,15 @@ def _check_agreements_of_displacements(
return filename


def check_agreement_of_supercell_positions(
supercell: PhonopyAtoms, points: np.ndarray
) -> bool:
"""Check agreement of supercell positions."""
diff = supercell.scaled_positions - points
diff -= np.rint(diff)
return (np.linalg.norm(diff @ supercell.cell, axis=1) > 1e-5).any()


def _subtract_residual_forces(force_sets):
for i in range(1, len(force_sets)):
force_sets[i] -= force_sets[0]
Expand Down
12 changes: 8 additions & 4 deletions phonopy/interface/vasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ def check_forces(forces, num_atom, filename, verbose=True):
"""Check a set of forces and show message if it is wrong."""
if len(forces) != num_atom:
if verbose:
stars = "*" * len(filename)
print("")
print("***************%s***************" % stars)
print('***** Parsing "%s" failed. *****' % filename)
print("***************%s***************" % stars)
if isinstance(filename, io.IOBase):
file_ptr_name = "forces"
else:
file_ptr_name = f'"{filename}"'
stars = "*" * len(file_ptr_name)
print(f"**************{stars}**************")
print(f"***** Parsing {file_ptr_name} failed. *****")
print(f"**************{stars}**************")
return False
else:
return True
Expand Down

0 comments on commit 440aef7

Please sign in to comment.