-
Notifications
You must be signed in to change notification settings - Fork 871
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve unphysical (greater than 1) occupancy handling in
CifParser
…
… and add missing site label `if not check_occu` (#3819) * fix tol def and add error msg * no verify * allow any occupancy when not check_occu * remove unnecessary warning * simplify warning message * add one more unit test * fix var name in comment * revise docstring * fix missing label when `check_occu` * update unit test * revert accidental change during merge * revert change to `occupancy_tolerance` * fix comment and revert a unnecessary deletion * filter valid occupancy * revert docstring change
- Loading branch information
1 parent
0c9ff40
commit a772ddb
Showing
2 changed files
with
24 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -323,9 +323,9 @@ def __init__( | |
""" | ||
Args: | ||
filename (PathLike): CIF file, gzipped or bzipped CIF files are fine too. | ||
occupancy_tolerance (float): If total occupancy of a site is between 1 and occupancy_tolerance, the | ||
occupancies will be scaled down to 1. | ||
site_tolerance (float): This tolerance is used to determine if two sites are in the same position, | ||
occupancy_tolerance (float): If total occupancy of a site is between | ||
1 and occupancy_tolerance, it will be scaled down to 1. | ||
site_tolerance (float): This tolerance is used to determine if two sites are at the same position, | ||
in which case they will be combined to a single disordered site. Defaults to 1e-4. | ||
frac_tolerance (float): This tolerance is used to determine is a coordinate should be rounded to an ideal | ||
value. e.g. 0.6667 is rounded to 2/3. This is desired if symmetry operations are going to be applied. | ||
|
@@ -1027,11 +1027,11 @@ def get_matching_coord( | |
|
||
# Get occupancy | ||
try: | ||
occu = str2float(data["_atom_site_occupancy"][idx]) | ||
occu: float = str2float(data["_atom_site_occupancy"][idx]) | ||
except (KeyError, ValueError): | ||
occu = 1 | ||
|
||
# If check_occu is True or the occupancy is greater than 0, create comp_d | ||
# If don't check_occu or the occupancy is greater than 0, create comp_dict | ||
if not check_occu or occu > 0: | ||
# Create site coordinate | ||
coord: Vector3D = ( | ||
|
@@ -1073,7 +1073,7 @@ def get_matching_coord( | |
|
||
if any(occu > 1 for occu in _sum_occupancies): | ||
msg = ( | ||
f"Some occupancies ({_sum_occupancies}) sum to > 1! If they are within " | ||
f"Some occupancies ({filter(lambda x: x<=1, _sum_occupancies)}) sum to > 1! If they are within " | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
DanielYang59
Author
Contributor
|
||
"the occupancy_tolerance, they will be rescaled. " | ||
f"The current occupancy_tolerance is set to: {self._occupancy_tolerance}" | ||
) | ||
|
@@ -1149,7 +1149,10 @@ def get_matching_coord( | |
all_species_noedit = all_species.copy() # save copy before scaling in case of check_occu=False, used below | ||
for idx, species in enumerate(all_species): | ||
total_occu = sum(species.values()) | ||
if 1 < total_occu <= self._occupancy_tolerance: | ||
if check_occu and total_occu > self._occupancy_tolerance: | ||
raise ValueError(f"Occupancy {total_occu} exceeded tolerance.") | ||
|
||
if total_occu > 1: | ||
all_species[idx] = species / total_occu | ||
|
||
if all_species and len(all_species) == len(all_coords) and len(all_species) == len(all_magmoms): | ||
|
@@ -1198,6 +1201,7 @@ def get_matching_coord( | |
all_coords[idx], | ||
lattice, | ||
properties=site_properties, | ||
label=all_labels[idx], | ||
skip_checks=True, | ||
) | ||
|
||
|
@@ -1278,8 +1282,6 @@ def parse_structures( | |
"in the CIF file as is. If you want the primitive cell, please set primitive=True explicitly.", | ||
UserWarning, | ||
) | ||
if not check_occu: # added in https://github.com/materialsproject/pymatgen/pull/2836 | ||
warnings.warn("Structures with unphysical site occupancies are not compatible with many pymatgen features.") | ||
|
||
if primitive and symmetrized: | ||
raise ValueError( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@shyuep did you actually look at the code in this PR? this should not have been merged as is.
x<=1
should bex>1
and filter returns a generator which prints like thisit's good to ask for test cases for error messages and warnings
cc @DanielYang59