Skip to content

Commit

Permalink
Improve Racah Symbol implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
xnx committed Mar 1, 2024
1 parent b09fb67 commit 99499ce
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name="pyvalem",
version="2.5.15",
version="2.5.16",
description="A package for managing simple chemical species and states",
long_description=long_description,
long_description_content_type="text/x-rst",
Expand Down
29 changes: 19 additions & 10 deletions src/pyvalem/states/racah_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

orbital_labels = ("s", "s'", "p", "p'", "d", "d'", "f", "f'")
atom_orbital = pp.oneOf(orbital_labels).setResultsName("orbital")
parity_label = pp.Literal("o").setResultsName("parity")

atom_k_term = (
integer.setResultsName("k_num") + pp.Suppress("/") + integer.setResultsName("k_den")
Expand All @@ -25,8 +26,9 @@
+ atom_orbital
+ pp.Suppress("[")
+ atom_k_term
+ pp.Suppress("]_")
+ atom_j_term
+ pp.Suppress("]")
+ pp.Optional(parity_label)
+ pp.Optional("_" + atom_j_term)
)


Expand All @@ -36,7 +38,8 @@ class RacahSymbol(State):
This class is currently little more than a stub. The term symbol of the
parent configuration must be specified as a separate State; this class
defines nl[K]_J, without the parity label ("superscript-o").
defines nl[K]_J, where _J is optional to allow for the case where a
state is an average over J levels.
"""

def __init__(self, state_str):
Expand All @@ -47,6 +50,7 @@ def __init__(self, state_str):
self.k_num = None
self.k_den = None
self.j_term = None
self.parity = None
self._parse_state(state_str)

def _parse_state(self, state_str):
Expand All @@ -62,15 +66,20 @@ def _parse_state(self, state_str):
self.parent_rot = 1.5
self.k_num = int(components.k_num)
self.k_den = int(components.k_den)
self.j_term = int(components.jterm)
self.parity = "o" if components.parity else ""
if components.jterm != "":
self.j_term = int(components.jterm)

def __repr__(self):
parent = "{}{}".format(self.principal, self.orbital)
k = "{}/{}".format(self.k_num, self.k_den)
return "{}[{}]_{}".format(parent, k, self.j_term)
parent = f"{self.principal}{self.orbital}"
k = f"{self.k_num}/{self.k_den}"
s_jterm = f"_{self.j_term}" if self.j_term is not None else ""
return f"{parent}[{k}]{self.parity}{s_jterm}"

@property
def html(self):
parent = "{}{}".format(self.principal, self.orbital)
k = "{}/{}".format(self.k_num, self.k_den)
return "{}[{}]<sub>{}</sub>".format(parent, k, self.j_term)
parent = f"{self.principal}{self.orbital}"
k = f"{self.k_num}/{self.k_den}"
s_parity = "<sup>o</sup>" if self.parity else ""
s_jterm = f"<sub>{self.j_term}</sub>" if self.j_term is not None else ""
return f"{parent}[{k}]{s_parity}{s_jterm}"
21 changes: 21 additions & 0 deletions tests/test_racah_symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,34 @@ def test_racah_symbol(self):
self.assertEqual(r0.j_term, 1)

r1 = RacahSymbol("3p[5/2]_2")
self.assertEqual(repr(r1), "3p[5/2]_2")
self.assertEqual(r1.html, "3p[5/2]<sub>2</sub>")
self.assertEqual(r1.principal, 3)
self.assertEqual(r1.orbital, "p")
self.assertEqual(r1.k_num, 5)
self.assertEqual(r1.k_den, 2)
self.assertEqual(r1.j_term, 2)

r2 = RacahSymbol("4d[3/2]")
self.assertEqual(repr(r2), "4d[3/2]")
self.assertEqual(r2.html, "4d[3/2]")
self.assertEqual(r2.principal, 4)
self.assertEqual(r2.orbital, "d")
self.assertEqual(r2.k_num, 3)
self.assertEqual(r2.k_den, 2)
self.assertEqual(r2.j_term, None)
self.assertEqual(r2.parity, "")

r3 = RacahSymbol("3p[3/2]o_2")
self.assertEqual(repr(r3), "3p[3/2]o_2")
self.assertEqual(r3.html, "3p[3/2]<sup>o</sup><sub>2</sub>")
self.assertEqual(r3.principal, 3)
self.assertEqual(r3.orbital, "p")
self.assertEqual(r3.k_num, 3)
self.assertEqual(r3.k_den, 2)
self.assertEqual(r3.j_term, 2)
self.assertEqual(r3.parity, "o")


if __name__ == "__main__":
unittest.main()

0 comments on commit 99499ce

Please sign in to comment.