Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.

Commit 7113fbc

Browse files
simplify calc_energy / calc_nu
1 parent 8845fbc commit 7113fbc

3 files changed

Lines changed: 27 additions & 68 deletions

File tree

src/ryd_numerov/rydberg_state.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from ryd_numerov.angular.utils import try_trivial_spin_addition
1313
from ryd_numerov.radial import RadialState
1414
from ryd_numerov.species.species_object import SpeciesObject
15-
from ryd_numerov.species.utils import calc_energy_from_nu, calc_nu_from_energy
15+
from ryd_numerov.species.utils import calc_energy_from_nu
1616
from ryd_numerov.units import BaseQuantities, MatrixElementType, ureg
1717

1818
if TYPE_CHECKING:
@@ -234,8 +234,7 @@ def __repr__(self) -> str:
234234
return f"{self.__class__.__name__}({species.name}, {n=}, {l=}, {j=}, {m=})"
235235

236236
def get_nu(self) -> float:
237-
energy_au = self.species.calc_energy(self.n, self.l, self.j, s_tot=1 / 2, unit="a.u.")
238-
return calc_nu_from_energy(self.species.reduced_mass_factor, energy_au)
237+
return self.species.calc_nu(self.n, self.l, self.j, s_tot=1 / 2)
239238

240239

241240
class RydbergStateAlkaliHyperfine(RydbergStateBase):
@@ -293,8 +292,7 @@ def __repr__(self) -> str:
293292
return f"{self.__class__.__name__}({species.name}, {n=}, {l=}, {j=}, {f=}, {m=})"
294293

295294
def get_nu(self) -> float:
296-
energy_au = self.species.calc_energy(self.n, self.l, self.j, s_tot=1 / 2, unit="a.u.")
297-
return calc_nu_from_energy(self.species.reduced_mass_factor, energy_au)
295+
return self.species.calc_nu(self.n, self.l, self.j, s_tot=1 / 2)
298296

299297

300298
class RydbergStateAlkalineLS(RydbergStateBase):
@@ -352,5 +350,4 @@ def __repr__(self) -> str:
352350
return f"{self.__class__.__name__}({species.name}, {n=}, {l=}, {s_tot=}, {j_tot=}, {m=})"
353351

354352
def get_nu(self) -> float:
355-
energy_au = self.species.calc_energy(self.n, self.l, self.j_tot, s_tot=self.s_tot, unit="a.u.")
356-
return calc_nu_from_energy(self.species.reduced_mass_factor, energy_au)
353+
return self.species.calc_nu(self.n, self.l, self.j_tot, s_tot=self.s_tot)

src/ryd_numerov/species/species_object.py

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import numpy as np
1212

13-
from ryd_numerov.species.utils import calc_energy_from_nu, convert_electron_configuration
13+
from ryd_numerov.species.utils import calc_nu_from_energy, convert_electron_configuration
1414
from ryd_numerov.units import rydberg_constant, ureg
1515

1616
if TYPE_CHECKING:
@@ -48,7 +48,7 @@ class SpeciesObject(ABC):
4848
_ionization_energy: tuple[float, float | None, str]
4949
"""Ionization energy with uncertainty and unit: (value, uncertainty, unit)."""
5050

51-
# Parameters for the extended Rydberg Ritz formula, see calc_energy
51+
# Parameters for the extended Rydberg Ritz formula, see calc_nu
5252
_quantum_defects: ClassVar[dict[tuple[int, float, float], tuple[float, float, float, float, float]] | None] = None
5353
"""Dictionary containing the quantum defects for each (l, j_tot, s_tot) combination, i.e.
5454
_quantum_defects[(l,j_tot,s_tot)] = (d0, d2, d4, d6, d8)
@@ -321,33 +321,7 @@ def reduced_mass_factor(self) -> float:
321321
"""
322322
return self.get_corrected_rydberg_constant("hartree") / rydberg_constant.to("hartree").m
323323

324-
@overload
325-
def calc_energy(
326-
self,
327-
n: int,
328-
l: int,
329-
j_tot: float,
330-
s_tot: float | None = None,
331-
*,
332-
use_nist_data: bool = True,
333-
nist_n_max: int = 15,
334-
unit: None = None,
335-
) -> PintFloat: ...
336-
337-
@overload
338-
def calc_energy(
339-
self,
340-
n: int,
341-
l: int,
342-
j_tot: float,
343-
s_tot: float | None = None,
344-
*,
345-
use_nist_data: bool = True,
346-
nist_n_max: int = 15,
347-
unit: str,
348-
) -> float: ...
349-
350-
def calc_energy( # noqa: C901
324+
def calc_nu(
351325
self,
352326
n: int,
353327
l: int,
@@ -356,15 +330,15 @@ def calc_energy( # noqa: C901
356330
*,
357331
use_nist_data: bool = True,
358332
nist_n_max: int = 15,
359-
unit: str | None = "hartree",
360-
) -> PintFloat | float:
361-
r"""Calculate the energy of a Rydberg state with for the given n, l, j_tot and s_tot.
333+
) -> float:
334+
r"""Calculate the effective principal quantum number nu of a Rydberg state with the given n, l, j_tot and s_tot.
362335
363-
I.e. either look up the energy for low lying states in the nist data,
364-
or calculate it via the quantum defect theory.
336+
I.e. either look up the energy for low lying states in the nist data (if use_nist_data is True),
337+
and calculate nu from the energy.
338+
Or calculate nu via the quantum defect theory.
365339
366-
The effective principal quantum number in quantum defect theory
367-
is defined as series expansion :math:`n^* = n - \delta_{lj}(n)`
340+
The effective principal quantum number nu in quantum defect theory
341+
is defined as series expansion :math:`\nu = n^* = n - \delta_{lj}(n)`
368342
where
369343
370344
.. math::
@@ -403,28 +377,18 @@ def calc_energy( # noqa: C901
403377
if j_tot % 1 != (l + s_tot) % 1:
404378
raise ValueError(f"Invalid quantum numbers: ({l=}, {j_tot=}, {s_tot=})")
405379

406-
energy_au: float | None = None
407-
if n <= nist_n_max and use_nist_data:
380+
if n <= nist_n_max and use_nist_data: # try to use NIST data
408381
if (n, l, j_tot, s_tot) in self._nist_energy_levels:
409382
energy_au = self._nist_energy_levels[(n, l, j_tot, s_tot)]
410383
energy_au -= self.get_ionization_energy("hartree")
411-
else:
412-
logger.debug(
413-
"NIST energy levels for (n=%d, l=%d, j_tot=%s, s_tot=%s) not found, using quantum defect theory.",
414-
*(n, l, j_tot, s_tot),
415-
)
416-
417-
if energy_au is None:
418-
if self._quantum_defects is None:
419-
raise ValueError(f"No quantum defect data available for species {self.name}.")
420-
d0, d2, d4, d6, d8 = self._quantum_defects.get((l, j_tot, s_tot), (0, 0, 0, 0, 0))
421-
delta_nlj = d0 + d2 / (n - d0) ** 2 + d4 / (n - d0) ** 4 + d6 / (n - d0) ** 6 + d8 / (n - d0) ** 8
422-
n_star = n - delta_nlj
423-
energy_au = calc_energy_from_nu(self.reduced_mass_factor, n_star)
424-
425-
energy: PintFloat = ureg.Quantity(energy_au, "hartree")
426-
if unit is None:
427-
return energy
428-
if unit == "a.u.":
429-
return energy.magnitude
430-
return energy.to(unit, "spectroscopy").magnitude
384+
return calc_nu_from_energy(self.reduced_mass_factor, energy_au)
385+
logger.debug(
386+
"NIST energy levels for (n=%d, l=%d, j_tot=%s, s_tot=%s) not found, using quantum defect theory.",
387+
*(n, l, j_tot, s_tot),
388+
)
389+
390+
if self._quantum_defects is None:
391+
raise ValueError(f"No quantum defect data available for species {self.name}.")
392+
d0, d2, d4, d6, d8 = self._quantum_defects.get((l, j_tot, s_tot), (0, 0, 0, 0, 0))
393+
delta_nlj = d0 + d2 / (n - d0) ** 2 + d4 / (n - d0) ** 4 + d6 / (n - d0) ** 6 + d8 / (n - d0) ** 8
394+
return n - delta_nlj

tests/test_radial_matrix_element.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from ryd_numerov.radial import RadialState
44
from ryd_numerov.rydberg_state import RydbergStateAlkali
55
from ryd_numerov.species import SpeciesObject
6-
from ryd_numerov.species.utils import calc_nu_from_energy
76

87

98
@pytest.mark.parametrize(
@@ -60,8 +59,7 @@ def test_circular_expectation_value(species_name: str, n: int, l: int, j_tot: fl
6059
<r^2>_{nl} = n^2/2 (5 n^2 - 3 l(l+1) + 1)
6160
"""
6261
species = SpeciesObject.from_name(species_name)
63-
energy_au = species.calc_energy(n, l, j_tot, unit="hartree")
64-
nu = calc_nu_from_energy(species.reduced_mass_factor, energy_au)
62+
nu = species.calc_nu(n, l, j_tot)
6563

6664
state = RadialState(species, nu=nu, l_r=l)
6765
state.set_n_for_sanity_check(n)

0 commit comments

Comments
 (0)