LCOV - code coverage report
Current view: top level - src/pairinteraction/green_tensor - dynamic_green_tensor.py (source / functions) Coverage Total Hit
Test: coverage.info Lines: 75.8 % 186 141
Test Date: 2026-11-02 09:44:19 Functions: 47.1 % 17 8

            Line data    Source code
       1              : # SPDX-FileCopyrightText: 2024 PairInteraction Developers
       2              : # SPDX-License-Identifier: LGPL-3.0-or-later
       3              : 
       4              : # ruff: noqa: N802, N806
       5              : 
       6            1 : from __future__ import annotations
       7              : 
       8            1 : import math
       9            1 : from typing import TYPE_CHECKING, Literal, TypeVar
      10              : 
      11            1 : import numpy as np
      12            1 : import scipy.constants as const
      13            1 : from scipy.integrate import quad
      14              : 
      15            1 : from pairinteraction.green_tensor.bessel_function import (
      16              :     cached_bessel_function_0,
      17              :     cached_bessel_function_1,
      18              :     cached_bessel_function_2,
      19              : )
      20              : 
      21              : if TYPE_CHECKING:
      22              :     from collections.abc import Callable
      23              : 
      24              :     from pairinteraction.units import NDArray
      25              : 
      26              :     Entries = Literal["xx", "xy", "xz", "yx", "yy", "yz", "zx", "zy", "zz"]
      27              : 
      28              :     from typing_extensions import ParamSpec
      29              : 
      30              :     P = ParamSpec("P")
      31              :     R = TypeVar("R")
      32              : 
      33              :     def njit(cache: bool) -> Callable[[Callable[P, R]], Callable[P, R]]: ...
      34              : 
      35              : else:
      36            1 :     from numba import njit
      37              : 
      38              : 
      39            1 : __all__ = ["dynamic_green_tensor_homogeneous", "dynamic_green_tensor_scattered"]
      40              : 
      41              : 
      42            1 : def dynamic_green_tensor_homogeneous(
      43              :     pos1: NDArray, pos2: NDArray, omega: float, epsilon0: complex, *, only_real_part: bool = False
      44              : ) -> NDArray:
      45              :     r"""Homogeneous Green Tensor for two atoms in cartesian coordinates in an infinite homogeneous medium.
      46              : 
      47              :     The function used is from equation 2 of the paper:
      48              :     "Dispersionless subradiant photon storage in one-dimensional emitter chains"
      49              :     https://doi.org/10.1103/PhysRevA.108.L051702
      50              : 
      51              :     We calculate the scaled Green Tensor, i.e.
      52              :     .. math::
      53              :         \frac{\omega^2}{\hbar \epsilon_0 c^2} G(r_\alpha, r_\beta, \omega)
      54              : 
      55              :     Args:
      56              :         pos1: Position vector of atom A in meters
      57              :         pos2: Position vector of atom B in meters
      58              :         omega: Angular frequency (i.e. 2*pi*f) in 1/s
      59              :         epsilon0: Electric permittivity of the medium (dimensionless, complex)
      60              :         only_real_part: If True, only the real part of the Green tensor is calculated (default: False)
      61              : 
      62              :     Returns: The 3x3 scaled homogeneous Green Tensor (general complex values) (m^(-3) [hbar]^(-1) [epsilon_0]^(-1))
      63              :         :math:`\omega^2 / (\hbar \epsilon_0 c^2) G(r_\alpha, r_\beta, \omega)`
      64              : 
      65              :     """
      66            1 :     k_vac = omega / const.c  # magnitude of wave vector in vacuum
      67            1 :     k0 = k_vac * np.sqrt(epsilon0)  # magnitude of wave vector in medium with permittivity epsilon0
      68            1 :     distance_vec = pos2 - pos1
      69            1 :     distance = np.linalg.norm(distance_vec)
      70              : 
      71              :     # this is missing a 1/k0**2 compared to the paper, we absorb this in the scaled prefactor, see line below
      72            1 :     prefactor = np.exp(1j * k0 * distance) / (4 * np.pi * distance**3)
      73            1 :     prefactor *= 1 / (epsilon0 * const.epsilon_0 * const.hbar)  # epsilon0 from missing k0^2 compared to omega^2/c^2
      74            1 :     prefactor *= -1  # minus sign from H = - \hbar \sum V_{\alpha\beta} ...
      75              : 
      76            1 :     result: NDArray = prefactor * (
      77              :         (k0**2 * distance**2 + 1j * k0 * distance - 1) * np.eye(3)
      78              :         + (-(k0**2) * distance**2 - 3j * k0 * distance + 3) * np.outer(distance_vec, distance_vec) / distance**2
      79              :     )
      80              : 
      81            1 :     if only_real_part:
      82            1 :         return np.real(result)
      83            0 :     return result
      84              : 
      85              : 
      86            1 : def dynamic_green_tensor_scattered(
      87              :     pos1: NDArray,
      88              :     pos2: NDArray,
      89              :     z1: float,
      90              :     z2: float,
      91              :     omega: float,
      92              :     epsilon0: complex,
      93              :     epsilon1: complex,
      94              :     epsilon2: complex,
      95              :     *,
      96              :     only_real_part: bool = False,
      97              : ) -> NDArray:
      98              :     """Assemble the total scattering Green tensor.
      99              : 
     100              :     Args:
     101              :         pos1: Position vector of atom A (m)
     102              :         pos2: Position vector of atom B (m)
     103              :         z1: z-coordinate of the first surface (m)
     104              :         z2: z-coordinate of the second surface (m)
     105              :         omega: Angular frequency (i.e. 2*pi*f) in 1/s
     106              :         epsilon0: Electric permittivity of the medium between the two surfaces (dimensionless, complex)
     107              :         epsilon1: Electric permittivity of the upper medium (dimensionless, complex)
     108              :         epsilon2: Electric permittivity of the lower medium (dimensionless, complex)
     109              :         only_real_part: If True, only the real part of the Green tensor is calculated (default: False)
     110              : 
     111              :     Returns: The 3x3 Scattering Green Tensor (general complex values) (1/m)
     112              : 
     113              :     """
     114            1 :     if z1 > z2:
     115              :         # Ensure z1 is the lower surface and z2 is the upper surface
     116            1 :         z1, z2 = z2, z1
     117            1 :         epsilon1, epsilon2 = epsilon2, epsilon1
     118            1 :     if not (z1 < pos1[2] < z2 and z1 < pos2[2] < z2):
     119            0 :         raise ValueError("Both atoms must be located between the two surfaces (i.e. z1 < z_atom < z2).")
     120              : 
     121            1 :     distance = pos2 - pos1
     122            1 :     height = abs(z1 - z2)
     123              : 
     124            1 :     rho = np.sqrt(distance[0] ** 2 + distance[1] ** 2)
     125            1 :     phi = np.atan2(distance[1], distance[0]) if rho != 0 else 0
     126              : 
     127            1 :     z_ges = pos1[2] + pos2[2] - 2 * min(z1, z2)
     128            1 :     z_diff = pos1[2] - pos2[2]
     129              : 
     130            1 :     gt_scattered = np.zeros((3, 3), dtype=complex)
     131            1 :     for i, ix in enumerate(["x", "y", "z"]):
     132            1 :         for j, jx in enumerate(["x", "y", "z"]):
     133            1 :             entry: Entries = ix + jx  # type: ignore [assignment]
     134            1 :             g_ij_elliptic = elliptic_integral(
     135              :                 omega,
     136              :                 height,
     137              :                 rho,
     138              :                 phi,
     139              :                 epsilon0,
     140              :                 epsilon1,
     141              :                 epsilon2,
     142              :                 z_ges,
     143              :                 z_diff,
     144              :                 entry,
     145              :                 only_real_part=only_real_part,
     146              :             )
     147            1 :             g_ij_real = real_axis_integral(
     148              :                 omega,
     149              :                 height,
     150              :                 rho,
     151              :                 phi,
     152              :                 epsilon0,
     153              :                 epsilon1,
     154              :                 epsilon2,
     155              :                 z_ges,
     156              :                 z_diff,
     157              :                 entry,
     158              :                 only_real_part=only_real_part,
     159              :             )
     160              :             # prefactor see comment in dynamic_green_tensor_homogeneous
     161            1 :             prefactor = -1 / (epsilon0 * const.epsilon_0 * const.hbar)
     162            1 :             value = prefactor * (g_ij_elliptic + g_ij_real)
     163            1 :             gt_scattered[i][j] = value
     164              : 
     165            1 :     return gt_scattered
     166              : 
     167              : 
     168            1 : @njit(cache=True)
     169            1 : def branch(epsilon: complex, k: float, k_rho: complex) -> complex:
     170              :     """Calculate the perpendicular wave vector component with positive imaginary part.
     171              : 
     172              :     Args:
     173              :         epsilon: Electric permittivity of the medium (dimensionless, complex)
     174              :         k: Magnitude of wave number in vacuum (1/m)
     175              :         k_rho: In-plane wave vector component (1/m)
     176              : 
     177              :     Returns: The perpendicular wave vector component (1/m)
     178              : 
     179              :     """
     180            0 :     return np.sqrt(epsilon * k**2 - k_rho**2 + 0j)  # type: ignore [no-any-return]
     181              : 
     182              : 
     183            1 : """The following functions are used from Appendix B of the paper:
     184              : "Modified dipole-dipole interaction and dissipation in an atomic ensemble near surfaces"
     185              : https://doi.org/10.1103/PhysRevA.97.053841
     186              : and are needed to calculate the scattering Green Tensor for two atoms between two planar surfaces.
     187              : We try to keep the same notation (in particular the same function names) as in the paper.
     188              : """
     189              : 
     190              : 
     191            1 : @njit(cache=True)
     192            1 : def rs(kz: complex, k1z: complex) -> complex:
     193              :     """Calculate the Fresnel reflection coefficient for s-polarized light.
     194              : 
     195              :     Args:
     196              :         kz: Perpendicular wave vector component in the medium between the two surfaces (1/m)
     197              :         k1z: Perpendicular wave vector component in the upper or lower medium (1/m)
     198              : 
     199              :     Returns: The Fresnel reflection coefficient for s-polarized light (dimensionless, complex)
     200              : 
     201              :     """
     202            0 :     if kz == 0 and k1z == 0:
     203            0 :         return 0.0
     204            0 :     return (kz - k1z) / (kz + k1z)
     205              : 
     206              : 
     207            1 : @njit(cache=True)
     208            1 : def rp(kz: complex, k1z: complex, epsilon: complex) -> complex:
     209              :     """Calculate the Fresnel reflection coefficient for p-polarized light.
     210              : 
     211              :     Args:
     212              :         kz: Perpendicular wave vector component in the medium between the two surfaces (1/m)
     213              :         k1z: Perpendicular wave vector component in the upper or lower medium (1/m)
     214              :         epsilon: Electric permittivity of the upper or lower medium (dimensionless, complex)
     215              : 
     216              :     Returns: The Fresnel reflection coefficient for p-polarized light (dimensionless, complex)
     217              : 
     218              :     """
     219            0 :     if kz == 0 and k1z == 0:
     220            0 :         return 0.0
     221            0 :     return (epsilon * kz - k1z) / (epsilon * kz + k1z)
     222              : 
     223              : 
     224            1 : @njit(cache=True)
     225            1 : def D(r_plus: complex, r_minus: complex, kz: complex, h: float) -> complex:
     226              :     """Calculate the denominator term D used in the scattering Green Tensor matrix elements.
     227              : 
     228              :     Args:
     229              :         r_plus: Fresnel reflection coefficient for the upper surface (dimensionless, complex)
     230              :         r_minus: Fresnel reflection coefficient for the lower surface (dimensionless, complex)
     231              :         kz: Perpendicular wave vector component in the medium between the two surfaces (1/m)
     232              :         h: Distance between the two surfaces (m)
     233              : 
     234              :     Returns: The value of the denominator term D (dimensionless, complex)
     235              : 
     236              :     """
     237            0 :     return 1 - r_plus * r_minus * np.exp(2j * kz * h)  # type: ignore [no-any-return]
     238              : 
     239              : 
     240            1 : @njit(cache=True)
     241            1 : def A_plus(r_plus: complex, r_minus: complex, kz: complex, h: float, z_ges: float, z_diff: float) -> complex:
     242              :     """Calculate the numerator term A_plus used in the scattering Green Tensor matrix elements.
     243              : 
     244              :     Args:
     245              :         r_plus: Fresnel reflection coefficient for the upper surface (dimensionless, complex)
     246              :         r_minus: Fresnel reflection coefficient for the lower surface (dimensionless, complex)
     247              :         kz: Perpendicular wave vector component in the medium between the two surfaces (1/m)
     248              :         h: Distance between the two surfaces (m)
     249              :         z_ges: Total z-coordinate (m)
     250              :         z_diff: Difference in z-coordinates (m)
     251              : 
     252              :     Returns: The value of the numerator term A_plus (dimensionless, complex)
     253              : 
     254              :     """
     255            0 :     return (  # type: ignore [no-any-return]
     256              :         r_minus * np.exp(1j * kz * (z_ges - h))
     257              :         + r_plus * np.exp(-1j * kz * (z_ges - h))
     258              :         + 2 * r_plus * r_minus * np.cos(kz * z_diff) * np.exp(1j * kz * h)
     259              :     ) / D(r_plus, r_minus, kz, h)
     260              : 
     261              : 
     262            1 : @njit(cache=True)
     263            1 : def A_minus(r_plus: complex, r_minus: complex, kz: complex, h: float, z_ges: float, z_diff: float) -> complex:
     264              :     """Calculate the numerator term A_minus used in the scattering Green Tensor matrix elements.
     265              : 
     266              :     Args:
     267              :         r_plus: Fresnel reflection coefficient for the upper surface (dimensionless, complex)
     268              :         r_minus: Fresnel reflection coefficient for the lower surface (dimensionless, complex)
     269              :         kz: Perpendicular wave vector component in the medium between the two surfaces (1/m)
     270              :         h: Distance between the two surfaces (m)
     271              :         z_ges: Total z-coordinate (m)
     272              :         z_diff: Difference in z-coordinates (m)
     273              : 
     274              :     Returns: The value of the numerator term A_minus (dimensionless, complex)
     275              : 
     276              :     """
     277            0 :     return (  # type: ignore [no-any-return]
     278              :         r_minus * np.exp(1j * kz * (z_ges - h))
     279              :         + r_plus * np.exp(-1j * kz * (z_ges - h))
     280              :         - 2 * r_plus * r_minus * np.cos(kz * z_diff) * np.exp(1j * kz * h)
     281              :     ) / D(r_plus, r_minus, kz, h)
     282              : 
     283              : 
     284            1 : @njit(cache=True)
     285            1 : def B_plus(r_plus: complex, r_minus: complex, kz: complex, h: float, z_ges: float, z_diff: float) -> complex:
     286              :     """Calculate the numerator term B_plus used in the scattering Green Tensor matrix elements.
     287              : 
     288              :     Args:
     289              :         r_plus: Fresnel reflection coefficient for the upper surface (dimensionless, complex)
     290              :         r_minus: Fresnel reflection coefficient for the lower surface (dimensionless, complex)
     291              :         kz: Perpendicular wave vector component in the medium between the two surfaces (1/m)
     292              :         h: Distance between the two surfaces (m)
     293              :         z_ges: Total z-coordinate (m)
     294              :         z_diff: Difference in z-coordinates (m)
     295              : 
     296              :     Returns: The value of the numerator term B_plus (dimensionless, complex)
     297              : 
     298              :     """
     299            0 :     return (  # type: ignore [no-any-return]
     300              :         r_minus * np.exp(1j * kz * (z_ges - h))
     301              :         - r_plus * np.exp(-1j * kz * (z_ges - h))
     302              :         + 2j * r_plus * r_minus * np.sin(kz * z_diff) * np.exp(1j * kz * h)
     303              :     ) / D(r_plus, r_minus, kz, h)
     304              : 
     305              : 
     306            1 : @njit(cache=True)
     307            1 : def B_minus(r_plus: complex, r_minus: complex, kz: complex, h: float, z_ges: float, z_diff: float) -> complex:
     308              :     """Calculate the numerator term B_minus used in the scattering Green Tensor matrix elements.
     309              : 
     310              :     Args:
     311              :         r_plus: Fresnel reflection coefficient for the upper surface (dimensionless, complex)
     312              :         r_minus: Fresnel reflection coefficient for the lower surface (dimensionless, complex)
     313              :         kz: Perpendicular wave vector component in the medium between the two surfaces (1/m)
     314              :         h: Distance between the two surfaces (m)
     315              :         z_ges: Total z-coordinate (m)
     316              :         z_diff: Difference in z-coordinates (m)
     317              : 
     318              :     Returns: The value of the numerator term B_minus (dimensionless, complex)
     319              : 
     320              :     """
     321            0 :     return (  # type: ignore [no-any-return]
     322              :         r_minus * np.exp(1j * kz * (z_ges - h))
     323              :         + r_plus * np.exp(-1j * kz * (z_ges - h))
     324              :         - 2j * r_plus * r_minus * np.sin(kz * z_diff) * np.exp(1j * kz * h)
     325              :     ) / D(r_plus, r_minus, kz, h)
     326              : 
     327              : 
     328            1 : def Gs(
     329              :     kz: complex,
     330              :     h: float,
     331              :     k_rho: complex,
     332              :     rho: float,
     333              :     phi: float,
     334              :     rs_plus: complex,
     335              :     rs_minus: complex,
     336              :     z_ges: float,
     337              :     z_diff: float,
     338              :     entry: Entries,
     339              : ) -> complex:
     340              :     """Calculate the Gs part of the scattering Green Tensor."""
     341            1 :     if entry in ["xz", "yz", "zx", "zy", "zz"]:
     342            1 :         return 0
     343            1 :     As_plus = A_plus(rs_plus, rs_minus, kz, h, z_ges, z_diff)
     344            1 :     J2 = cached_bessel_function_2(k_rho * rho)
     345            1 :     if entry in ["xy", "yx"]:
     346            1 :         return -As_plus / 2 * J2 * math.sin(2 * phi)
     347            1 :     J0 = cached_bessel_function_0(k_rho * rho)
     348            1 :     if entry == "xx":
     349            1 :         return As_plus / 2 * (J0 + J2 * math.cos(2 * phi))
     350            1 :     if entry == "yy":
     351            1 :         return As_plus / 2 * (J0 - J2 * math.cos(2 * phi))
     352              : 
     353            0 :     raise ValueError(f"Invalid entry '{entry}' for Gs function.")
     354              : 
     355              : 
     356            1 : def Gp(  # noqa: PLR0911
     357              :     kz: complex,
     358              :     h: float,
     359              :     k_rho: complex,
     360              :     rho: float,
     361              :     phi: float,
     362              :     rp_plus: complex,
     363              :     rp_minus: complex,
     364              :     z_ges: float,
     365              :     z_diff: float,
     366              :     entry: Entries,
     367              : ) -> complex:
     368              :     """Calculate the Gp part of the scattering Green Tensor."""
     369            1 :     if entry in ["xz", "zx", "yz", "zy"]:
     370            1 :         J1 = cached_bessel_function_1(k_rho * rho)
     371            1 :         if entry == "zx":
     372            1 :             Bp_minus = B_minus(rp_plus, rp_minus, kz, h, z_ges, z_diff)
     373            1 :             return -1j * (k_rho / kz) * Bp_minus * J1 * math.cos(phi)
     374            1 :         if entry == "xz":
     375            1 :             Bp_plus = B_plus(rp_plus, rp_minus, kz, h, z_ges, z_diff)
     376            1 :             return 1j * (k_rho / kz) * Bp_plus * J1 * math.cos(phi)
     377            1 :         if entry == "yz":
     378            1 :             Bp_plus = B_plus(rp_plus, rp_minus, kz, h, z_ges, z_diff)
     379            1 :             return 1j * (k_rho / kz) * Bp_plus * J1 * math.sin(phi)
     380            1 :         if entry == "zy":
     381            1 :             Bp_minus = B_minus(rp_plus, rp_minus, kz, h, z_ges, z_diff)
     382            1 :             return -1j * (k_rho / kz) * Bp_minus * J1 * math.sin(phi)
     383              : 
     384            1 :     if entry == "zz":
     385            1 :         J0 = cached_bessel_function_0(k_rho * rho)
     386            1 :         Ap_plus = A_plus(rp_plus, rp_minus, kz, h, z_ges, z_diff)
     387            1 :         return -(k_rho**2 / kz**2) * Ap_plus * J0
     388              : 
     389            1 :     J2 = cached_bessel_function_2(k_rho * rho)
     390            1 :     Ap_minus = A_minus(rp_plus, rp_minus, kz, h, z_ges, z_diff)
     391            1 :     if entry in ["xy", "yx"]:
     392            1 :         return Ap_minus / 2 * J2 * math.sin(2 * phi)
     393              : 
     394            1 :     J0 = cached_bessel_function_0(k_rho * rho)
     395            1 :     if entry == "xx":
     396            1 :         return Ap_minus / 2 * (J0 - J2 * math.cos(2 * phi))
     397            1 :     if entry == "yy":
     398            1 :         return Ap_minus / 2 * (J0 + J2 * math.cos(2 * phi))
     399              : 
     400            0 :     raise ValueError(f"Invalid entry '{entry}' for Gp function.")
     401              : 
     402              : 
     403            1 : """ The integrals for the scattering Green Tensor are evaluated in two parts:
     404              :     The first part is along an elliptical path from 0 to 2*k_maj in the complex plane to avoid singularities,
     405              :     and the second part is along the real axis from 2*k_maj to an upper limit.
     406              : 
     407              :     The methods for evaluating these integrals are explained in the papers:
     408              :     - "Accurate and efficient computation of the Green's tensor for stratified media"
     409              :         section III.A (https://doi.org/10.1103/PhysRevE.62.5797)
     410              :     - "Challenges in Computational Electromagnetics: Analysis and Optimization of Planar Multilayered Structures"
     411              :         section 2.4.1 (https://doi.org/10.5075/epfl-thesis-5122)
     412              :     """
     413              : 
     414              : 
     415            1 : @njit(cache=True)
     416            1 : def integrand_ellipse_partial(
     417              :     t: complex,
     418              :     k_maj: float,
     419              :     k_min: float,
     420              :     k0: float,
     421              :     epsilon0: float,
     422              :     epsilon1: complex,
     423              :     epsilon2: complex,
     424              :     h: float,
     425              : ) -> tuple[complex, complex, complex, complex, complex, complex, complex]:
     426              :     # elliptical path, substitution
     427            0 :     k_rho = k_maj * (1 + np.cos(t)) - 1j * k_min * np.sin(t)
     428            0 :     dk_rho = -k_maj * np.sin(t) - 1j * k_min * np.cos(t)
     429              : 
     430              :     # Wave vector components
     431            0 :     kz = branch(epsilon0, k0, k_rho)
     432            0 :     k1z = branch(epsilon1, k0, k_rho)
     433            0 :     k2z = branch(epsilon2, k0, k_rho)
     434              : 
     435            0 :     rs_plus = rs(kz, k1z)
     436            0 :     rs_minus = rs(kz, k2z)
     437            0 :     rp_plus = rp(kz, k1z, epsilon1)
     438            0 :     rp_minus = rp(kz, k2z, epsilon2)
     439              : 
     440            0 :     if k_rho == 0 and dk_rho == 0:  # noqa: SIM108
     441            0 :         prefactor = 0.0
     442              :     else:
     443            0 :         prefactor = 1j / (4 * np.pi) * (k_rho / kz) * np.exp(1j * kz * h) * dk_rho
     444            0 :     return k_rho, kz, rs_plus, rs_minus, rp_plus, rp_minus, prefactor
     445              : 
     446              : 
     447            1 : def integrand_ellipse(
     448              :     t: complex,
     449              :     k_maj: float,
     450              :     k_min: float,
     451              :     k0: float,
     452              :     epsilon0: float,
     453              :     epsilon1: complex,
     454              :     epsilon2: complex,
     455              :     h: float,
     456              :     rho: float,
     457              :     phi: float,
     458              :     z_ges: float,
     459              :     z_diff: float,
     460              :     entry: Entries,
     461              :     real_or_imag: str,
     462              : ) -> complex:
     463            1 :     k_rho, kz, rs_plus, rs_minus, rp_plus, rp_minus, prefactor = integrand_ellipse_partial(
     464              :         t, k_maj, k_min, k0, epsilon0, epsilon1, epsilon2, h
     465              :     )
     466            1 :     if k0 == 0 and kz == 0:
     467            1 :         return 0.0
     468              : 
     469            0 :     gs = Gs(kz, h, k_rho, rho, phi, rs_plus, rs_minus, z_ges, z_diff, entry)
     470            0 :     gp = Gp(kz, h, k_rho, rho, phi, rp_plus, rp_minus, z_ges, z_diff, entry)
     471              : 
     472            0 :     integrand = prefactor * (k0**2 * gs - kz**2 * gp)
     473            0 :     if real_or_imag == "real":
     474            0 :         return np.real(integrand)
     475            0 :     if real_or_imag == "imag":
     476            0 :         return np.imag(integrand)
     477            0 :     raise ValueError("real_or_imag must be 'real' or 'imag'")
     478              : 
     479              : 
     480            1 : def elliptic_integral(
     481              :     omega: float,
     482              :     h: float,
     483              :     rho: float,
     484              :     phi: float,
     485              :     epsilon0: complex,
     486              :     epsilon1: complex,
     487              :     epsilon2: complex,
     488              :     z_ges: float,
     489              :     z_diff: float,
     490              :     entry: Entries,
     491              :     *,
     492              :     only_real_part: bool = False,
     493              : ) -> complex:
     494              :     """Evaluate the elliptic part of the integral.
     495              : 
     496              :     Args:
     497              :         omega: Angular frequency (i.e. 2*pi*f) in 1/s
     498              :         h: Distance between the two surfaces in meters
     499              :         rho: In-plane distance between the two atoms in meters
     500              :         phi: Angle between the in-plane distance vector and the x-axis in radians
     501              :         epsilon0: Electric permittivity of the medium between the two surfaces (dimensionless, complex)
     502              :         epsilon1: Electric permittivity of the upper medium (dimensionless, complex)
     503              :         epsilon2: Electric permittivity of the lower medium (dimensionless, complex)
     504              :         z_ges: Sum of the z-positions of the two atoms in meters
     505              :         z_diff: Difference of the z-positions of the two atoms in meters
     506              :         entry: Entry of the Green tensor to calculate
     507              :         only_real_part: If True, only the real part of the integral is calculated (default: False)
     508              : 
     509              :     Returns: The value of the integral along the elliptical path as a complex number (1/m)
     510              : 
     511              :     """
     512            1 :     k_vac = omega / const.c  # magnitude of wave vector in vacuum
     513            1 :     k0 = k_vac * np.sqrt(epsilon0)
     514              : 
     515              :     # Elliptical path in complex plane to avoid singularities (Integral from 0 to 2k_maj)
     516            1 :     k1 = k_vac * np.sqrt(epsilon1)
     517            1 :     k2 = k_vac * np.sqrt(epsilon2)
     518            1 :     kl_max = max(np.real(k0), np.real(k1), np.real(k2))
     519              : 
     520            1 :     k_maj = (kl_max + k_vac) / 2  # major axis of ellipse
     521            1 :     k_min = min(k_vac, 1 / rho) if rho != 0 else k_vac
     522              : 
     523            1 :     args = (k_maj, k_min, k0, epsilon0, epsilon1, epsilon2, h, rho, phi, z_ges, z_diff, entry)
     524              : 
     525            1 :     real_ellipse, _ = quad(integrand_ellipse, np.pi, 0, args=(*args, "real"), epsrel=1e-9, limit=1000)  # type: ignore [arg-type]
     526            1 :     if only_real_part:
     527            1 :         return real_ellipse
     528            0 :     imag_ellipse, _ = quad(integrand_ellipse, np.pi, 0, args=(*args, "imag"), epsrel=1e-9, limit=1000)  # type: ignore [arg-type]
     529            0 :     return real_ellipse + 1j * imag_ellipse
     530              : 
     531              : 
     532            1 : def integrand_real(
     533              :     k_rho: complex,
     534              :     k0: float,
     535              :     epsilon0: float,
     536              :     epsilon1: complex,
     537              :     epsilon2: complex,
     538              :     h: float,
     539              :     rho: float,
     540              :     phi: float,
     541              :     z_ges: float,
     542              :     z_diff: float,
     543              :     entry: Entries,
     544              :     real_or_imag: str,
     545              : ) -> complex:
     546            1 :     kz = branch(epsilon0, k0, k_rho)
     547            1 :     if kz == 0 and k0 == 0:
     548            0 :         return 0
     549              : 
     550            1 :     k1z = branch(epsilon1, k0, k_rho)
     551            1 :     k2z = branch(epsilon2, k0, k_rho)
     552              : 
     553            1 :     rs_plus = rs(kz, k1z)
     554            1 :     rs_minus = rs(kz, k2z)
     555            1 :     rp_plus = rp(kz, k1z, epsilon1)
     556            1 :     rp_minus = rp(kz, k2z, epsilon2)
     557              : 
     558              :     # Integrand
     559            1 :     integrand = (
     560              :         1j
     561              :         / (4 * np.pi)
     562              :         * (
     563              :             k0**2 * Gs(kz, h, k_rho, rho, phi, rs_plus, rs_minus, z_ges, z_diff, entry)
     564              :             - kz**2 * Gp(kz, h, k_rho, rho, phi, rp_plus, rp_minus, z_ges, z_diff, entry)
     565              :         )
     566              :         * (k_rho / kz)
     567              :         * np.exp(1j * kz * h)
     568              :     )
     569            1 :     if real_or_imag == "real":
     570            1 :         return np.real(integrand)  # type: ignore [no-any-return]
     571            0 :     if real_or_imag == "imag":
     572            0 :         return np.imag(integrand)  # type: ignore [no-any-return]
     573            0 :     raise ValueError("real_or_imag must be 'real' or 'imag'")
     574              : 
     575              : 
     576            1 : def real_axis_integral(
     577              :     omega: float,
     578              :     h: float,
     579              :     rho: float,
     580              :     phi: float,
     581              :     epsilon0: complex,
     582              :     epsilon1: complex,
     583              :     epsilon2: complex,
     584              :     z_ges: float,
     585              :     z_diff: float,
     586              :     entry: Entries,
     587              :     *,
     588              :     only_real_part: bool = False,
     589              : ) -> complex:
     590              :     """Evaluate the real axis part of the integral.
     591              : 
     592              :     Args:
     593              :         omega: Angular frequency (i.e. 2*pi*f) in 1/s
     594              :         h: Distance between the two surfaces in meters
     595              :         rho: In-plane distance between the two atoms in meters
     596              :         phi: Angle between the in-plane distance vector and the x-axis in radians
     597              :         epsilon0: Electric permittivity of the medium between the two surfaces (dimensionless, complex)
     598              :         epsilon1: Electric permittivity of the upper medium (dimensionless, complex)
     599              :         epsilon2: Electric permittivity of the lower medium (dimensionless, complex)
     600              :         z_ges: Sum of the z-positions of the two atoms in meters
     601              :         z_diff: Difference of the z-positions of the two atoms in meters
     602              :         entry: Entry of the Green tensor to calculate
     603              :         upper_limit: Upper limit for the real axis integral (1/m)
     604              :         only_real_part: If True, only the real part of the integral is calculated (default: False)
     605              : 
     606              :     Returns: The value of the integral along the real axis as a complex number (1/m)
     607              : 
     608              :     """
     609            1 :     k_vac = omega / const.c  # magnitude of wave vector in vacuum
     610            1 :     k0 = k_vac * np.sqrt(epsilon0)
     611            1 :     k1 = k_vac * np.sqrt(epsilon1)
     612            1 :     k2 = k_vac * np.sqrt(epsilon2)
     613              : 
     614            1 :     kl_max = max(np.real(k0), np.real(k1), np.real(k2))
     615            1 :     k_maj = (kl_max + k_vac) / 2
     616              : 
     617            1 :     args = (k0, epsilon0, epsilon1, epsilon2, h, rho, phi, z_ges, z_diff, entry)
     618              : 
     619              :     # Estimate the upper limit for the real axis integral
     620            1 :     upper_limit = np.sqrt((745 / h) ** 2 + 1)
     621              : 
     622            1 :     real_real, _ = quad(
     623              :         integrand_real,  # type: ignore [arg-type]
     624              :         2 * k_maj,
     625              :         upper_limit,
     626              :         args=(*args, "real"),  # type: ignore [arg-type]
     627              :         limit=1000,
     628              :         epsrel=1e-9,
     629              :     )
     630            1 :     if only_real_part:
     631            1 :         return real_real
     632            0 :     imag_real, _ = quad(
     633              :         integrand_real,  # type: ignore [arg-type]
     634              :         2 * k_maj,
     635              :         upper_limit,
     636              :         args=(*args, "imag"),  # type: ignore [arg-type]
     637              :         limit=1000,
     638              :         epsrel=1e-9,
     639              :     )
     640            0 :     return real_real + 1j * imag_real
        

Generated by: LCOV version 2.0-1