LCOV - code coverage report
Current view: top level - tests - test_lifetime.py (source / functions) Hit Total Coverage
Test: coverage.info Lines: 35 40 87.5 %
Date: 2025-04-29 15:59:54 Functions: 4 8 50.0 %

          Line data    Source code
       1             : # SPDX-FileCopyrightText: 2025 Pairinteraction Developers
       2             : # SPDX-License-Identifier: LGPL-3.0-or-later
       3             : 
       4             : """Test calculating lifetimes."""
       5             : 
       6           1 : import numpy as np
       7           1 : import pytest
       8           1 : from scipy.optimize import curve_fit
       9             : 
      10           1 : import pairinteraction.real as pi
      11           1 : from pairinteraction import ureg
      12             : 
      13             : 
      14           1 : def test_lifetime() -> None:
      15             :     """Test calculating the lifetime of a state.
      16             : 
      17             :     Note that obtaining a reasonable value for the lifetime requires the full database and is not possible with the
      18             :     reduced database that is included in the repository!
      19             :     """
      20           1 :     ket = pi.KetAtom("Yb174_mqdt", n=64, l=0, j=0, m=0)
      21             : 
      22             :     # We use n=64 here, because this corresponds to nu~60 and we include
      23             :     # states with 50<nu<70 in the limited database that comes with the repository. In
      24             :     # addition, states with nu<20 are included so that the decay to the ground state
      25             :     # is also captured. Note that the relative error of the calculated
      26             :     # lifetime versus the actual lifetime is still quite large because of the limited
      27             :     # number of states. The full database is used and accurate results can be obtained
      28             :     # if the test is run with `pytest --database-dir "" --download-missing`.
      29             : 
      30           1 :     lifetime1 = ket.get_lifetime(temperature=300, temperature_unit="K", unit="us")
      31           1 :     lifetime2 = ket.get_lifetime(temperature=300 * ureg.K, unit="us")
      32           1 :     lifetime3 = ket.get_lifetime(temperature=300 * ureg.K)
      33           1 :     lifetime4 = ket.get_lifetime(unit="us")
      34             : 
      35           1 :     assert lifetime1 == lifetime2 == lifetime3.to(ureg.us).magnitude < lifetime4
      36           1 :     assert pytest.approx(lifetime1, rel=0.15) == 142.04845576112646  # NOSONAR
      37           1 :     assert pytest.approx(lifetime4, rel=0.15) == 494.1653414977515  # NOSONAR
      38             : 
      39             : 
      40           1 : def test_lifetime_scaling() -> None:
      41             :     """Test the scaling of the lifetime with the principal quantum number."""
      42             : 
      43           1 :     def fit_function(x, a, b):
      44           1 :         return a * x + b
      45             : 
      46           1 :     n_list = list(range(60, 70, 1))
      47             : 
      48             :     # S states
      49           1 :     kets = [pi.KetAtom("Rb", n=n, l=0, j=0.5, m=0.5) for n in n_list]
      50           1 :     nu = [ket.nu for ket in kets]
      51           1 :     lifetimes = [ket.get_lifetime(unit="us") for ket in kets]
      52           1 :     popt, _ = curve_fit(fit_function, np.log(nu), np.log(lifetimes))
      53           1 :     assert np.isclose(popt[0], 3, atol=0.02)
      54             : 
      55             :     # Circular states
      56           1 :     try:
      57           1 :         kets = [pi.KetAtom("Rb", n=n, l=n - 1, j=n - 0.5, m=n - 0.5) for n in n_list]
      58           1 :     except ValueError as err:
      59             :         # If the limited database which comes with the repository is used, creating the
      60             :         # kets will fail because the circular states are not included in the database.
      61             :         # This is expected.
      62           1 :         if "No state found" not in str(err):
      63           0 :             raise
      64             :     else:
      65           0 :         nu = [ket.nu for ket in kets]
      66           0 :         lifetimes = [ket.get_lifetime(unit="us") for ket in kets]
      67           0 :         popt, _ = curve_fit(fit_function, np.log(nu), np.log(lifetimes))
      68           0 :         assert np.isclose(popt[0], 5, atol=0.02)
      69             : 
      70             : 
      71           1 : def test_transition_rates() -> None:
      72             :     """Test calculating transition rates to other states.
      73             : 
      74             :     Note that obtaining a reasonable value for the transition rates requires the full database and is not possible
      75             :     with the reduced database that is included in the repository!
      76             :     """
      77           1 :     ket = pi.KetAtom("Yb174_mqdt", n=60, l=0, j=0, m=0)
      78             : 
      79           1 :     lifetime = ket.get_lifetime(temperature=300, temperature_unit="K", unit="us")
      80           1 :     kets_sp, rates_sp = ket.get_spontaneous_transition_rates(unit="MHz")
      81           1 :     kets_bbr, rates_bbr = ket.get_black_body_transition_rates(temperature=300, temperature_unit="K", unit="MHz")
      82             : 
      83           1 :     assert len(rates_sp) == len(kets_sp)
      84           1 :     assert len(rates_bbr) == len(kets_bbr)
      85           1 :     assert np.isclose(1 / (sum(rates_sp) + sum(rates_bbr)), lifetime)

Generated by: LCOV version 1.16