Skip to content

Commit

Permalink
(Structure|Molecule).alphabetical_formula (#3478)
Browse files Browse the repository at this point in the history
* fix AssertionError assert structure  # for mypy

        if temperature and not structure:
            raise ValueError("If using temperature input, you must also include structure")

* Add alphabetical_formula property to
SiteCollection class

* add TestIStructure.test_alphabetical_formula

* Update PBE64Base.yaml hash in test_sets.py
  • Loading branch information
janosh committed Nov 18, 2023
1 parent 7575993 commit 27066d3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
3 changes: 1 addition & 2 deletions pymatgen/analysis/elasticity/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,6 @@ def get_tgt(self, temperature: float | None = None, structure: Structure = None,
"""
if temperature and not structure:
raise ValueError("If using temperature input, you must also include structure")
assert structure # for mypy

quad = quad or DEFAULT_QUAD
points = quad["points"]
Expand All @@ -611,7 +610,7 @@ def get_tgt(self, temperature: float | None = None, structure: Structure = None,
us = [u / np.linalg.norm(u) for u in np.transpose(us)]
for u in us:
# TODO: this should be benchmarked
if temperature:
if temperature and structure:
c = self.get_heat_capacity(temperature, structure, p, u)
num += c * self.get_ggt(p, u) * w
denom += c * w
Expand Down
5 changes: 5 additions & 0 deletions pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ def formula(self) -> str:
"""Returns the formula as a string."""
return self.composition.formula

@property
def alphabetical_formula(self) -> str:
"""Returns the formula as a string."""
return self.composition.alphabetical_formula

@property
def elements(self) -> list[Element | Species | DummySpecies]:
"""Returns the elements in the structure as a list of Element objects."""
Expand Down
24 changes: 12 additions & 12 deletions pymatgen/core/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ def __pow__(self, i):
def __iter__(self):
return iter(self._unit)

def __getitem__(self, i):
def __getitem__(self, i) -> int:
return self._unit[i]

def __len__(self):
def __len__(self) -> int:
return len(self._unit)

def __repr__(self):
Expand Down Expand Up @@ -254,16 +254,16 @@ def get_conversion_factor(self, new_unit):
Args:
new_unit: The new unit.
"""
uo_base, ofactor = self.as_base_units
un_base, nfactor = Unit(new_unit).as_base_units
units_new = sorted(un_base.items(), key=lambda d: _UNAME2UTYPE[d[0]])
units_old = sorted(uo_base.items(), key=lambda d: _UNAME2UTYPE[d[0]])
factor = ofactor / nfactor
for uo, un in zip(units_old, units_new):
if uo[1] != un[1]:
raise UnitError(f"Units {uo} and {un} are not compatible!")
c = ALL_UNITS[_UNAME2UTYPE[uo[0]]]
factor *= (c[uo[0]] / c[un[0]]) ** uo[1]
old_base, old_factor = self.as_base_units
new_base, new_factor = Unit(new_unit).as_base_units
units_new = sorted(new_base.items(), key=lambda d: _UNAME2UTYPE[d[0]])
units_old = sorted(old_base.items(), key=lambda d: _UNAME2UTYPE[d[0]])
factor = old_factor / new_factor
for old, new in zip(units_old, units_new):
if old[1] != new[1]:
raise UnitError(f"Units {old} and {new} are not compatible!")
c = ALL_UNITS[_UNAME2UTYPE[old[0]]]
factor *= (c[old[0]] / c[new[0]]) ** old[1]
return factor


Expand Down
8 changes: 8 additions & 0 deletions tests/core/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def setUp(self):
[[3.8401979337, 0, 0], [1.9200989668, 3.3257101909, 0], [0, -2.2171384943, 3.1355090603]],
pbc=(True, True, False),
)
self.V2O3 = IStructure.from_file(f"{TEST_FILES_DIR}/V2O3.cif")

@skipIf(not (mcsqs_cmd and enum_cmd), "enumlib or mcsqs executable not present")
def test_get_orderings(self):
Expand Down Expand Up @@ -145,6 +146,13 @@ def test_formula(self):
assert self.struct.formula == "Si2"
assert self.labeled_structure.formula == "Si2"
assert self.propertied_structure.formula == "Si2"
assert self.V2O3.formula == "V4 O6"

def test_alphabetical_formula(self):
assert self.struct.alphabetical_formula == "Si2"
assert self.labeled_structure.alphabetical_formula == "Si2"
assert self.propertied_structure.alphabetical_formula == "Si2"
assert self.V2O3.alphabetical_formula == "O6 V4"

def test_elements(self):
assert self.struct.elements == [Element("Si")]
Expand Down
2 changes: 1 addition & 1 deletion tests/io/vasp/test_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_sets_changed(self):
"MVLGWSet.yaml": "104ae93c3b3be19a13b0ee46ebdd0f40ceb96597",
"MVLRelax52Set.yaml": "4cfc6b1bd0548e45da3bde4a9c65b3249da13ecd",
"PBE54Base.yaml": "ec317781a7f344beb54c17a228db790c0eb49282",
"PBE64Base.yaml": "a0d6cd02c6ff8e24b0005e30c56a33b155c2280b",
"PBE64Base.yaml": "480c41c2448cb25706181de268090618e282b264",
"VASPIncarBase.yaml": "19762515f8deefb970f2968fca48a0d67f7964d4",
"vdW_parameters.yaml": "04bb09bb563d159565bcceac6a11e8bdf0152b79",
}
Expand Down

0 comments on commit 27066d3

Please sign in to comment.