Skip to main content

caroli_keldysh/
lib.rs

1//! Non-equilibrium steady state of two-terminal devices (Keldysh formalism).
2//!
3//! Each lead `p` injects electrons with occupation `f_p(E)`: `Σ^<_p = i f_p Γ_p`, and the lesser
4//! Green's function is `G^< = G^r (Σ^<_L + Σ^<_R) G^a`. [`Lesser`] holds, at one energy, the
5//! blocks needed for local observables (diagonal `G^<_nn` and next-neighbour `G^<_{n,n+1}`),
6//! separately for the electrons injected from each lead.
7//!
8//! Normalisations (spin included in the orbitals): the particle current through a bond is
9//! `J_{i→j} = (e/h) ∫ dE j_{i→j}(E)` with the spectral current
10//! `j_{i→j}(E) = 2 Re Tr[H_ij G^<_ji(E)]`; summed over a cross-section it equals
11//! `T(E) [f_L(E) − f_R(E)]` (Landauer). The occupied spectral density of orbital `a` is
12//! `Im G^<_aa(E) / 2π`.
13
14pub use caroli_green::BondCurrent;
15use caroli_green::{GreenError, SelfEnergyMethod, TwoTerminal, broadening};
16use caroli_model::{CMat, c64};
17
18/// Fermi–Dirac occupation; at `temperature = 0` a step with `f(μ) = 1`.
19pub fn fermi(energy: f64, mu: f64, temperature: f64) -> f64 {
20    if temperature == 0.0 {
21        return if energy > mu { 0.0 } else { 1.0 };
22    }
23    let x = (energy - mu) / temperature;
24    if x > 0.0 { (-x).exp() / ((-x).exp() + 1.0) } else { 1.0 / (1.0 + x.exp()) }
25}
26
27/// Lesser blocks of the electrons injected by one lead.
28#[derive(Clone, Debug)]
29pub struct Injected {
30    /// `G^<_{nn}` for every slice.
31    pub diag: Vec<CMat>,
32    /// `G^<_{n,n+1}` for `n = 0..N−1`.
33    pub next: Vec<CMat>,
34}
35
36/// Lesser Green's function blocks of a two-terminal device at one energy.
37#[derive(Clone, Debug)]
38pub struct Lesser {
39    /// Energy `E` of the blocks.
40    pub energy: f64,
41    /// Blocks of the electrons injected by the left lead, `G^r Σ^<_L G^a` with `Σ^<_L = i f_L Γ_L`.
42    pub left: Injected,
43    /// Blocks of the electrons injected by the right lead, `G^r Σ^<_R G^a` with `Σ^<_R = i f_R Γ_R`.
44    pub right: Injected,
45    /// Transmission at this energy (from the same Green's functions).
46    pub transmission: f64,
47    /// Occupations `(f_L, f_R)` used.
48    pub occupations: (f64, f64),
49}
50
51/// `i f Γ`.
52fn sigma_lesser(sigma: &CMat, f: f64) -> CMat {
53    let g = broadening(sigma);
54    CMat::from_fn(g.nrows(), g.ncols(), |i, j| c64::new(0.0, f) * g[(i, j)])
55}
56
57fn injected(column: &[CMat], sigma_lesser: &CMat) -> Injected {
58    let n = column.len();
59    let diag = column.iter().map(|g| g * sigma_lesser * g.adjoint()).collect();
60    let next =
61        (0..n.saturating_sub(1)).map(|k| &column[k] * sigma_lesser * column[k + 1].adjoint()).collect();
62    Injected { diag, next }
63}
64
65impl Lesser {
66    /// Lesser blocks at `energy` with lead occupations `f_left`, `f_right`.
67    pub fn compute(
68        sys: &TwoTerminal,
69        energy: f64,
70        method: SelfEnergyMethod,
71        f_left: f64,
72        f_right: f64,
73    ) -> Result<Self, GreenError> {
74        let sw = sys.sweeps(energy, method)?;
75        let first = sw.first_column(sys);
76        let last = sw.last_column(sys);
77        let left = injected(&first, &sigma_lesser(&sw.sigma_l, f_left));
78        let right = injected(&last, &sigma_lesser(&sw.sigma_r, f_right));
79        let g1n = &last[0];
80        let t = broadening(&sw.sigma_l) * g1n * broadening(&sw.sigma_r) * g1n.adjoint();
81        let transmission = caroli_green::trace(&t).re;
82        Ok(Self { energy, left, right, transmission, occupations: (f_left, f_right) })
83    }
84
85    /// `G^<_{nn}` (both leads).
86    pub fn diag(&self, n: usize) -> CMat {
87        &self.left.diag[n] + &self.right.diag[n]
88    }
89
90    /// `G^<_{n,n+1}` (both leads).
91    pub fn next(&self, n: usize) -> CMat {
92        &self.left.next[n] + &self.right.next[n]
93    }
94
95    /// Spectral particle current from slice `n` to slice `n+1`, `2 Re Tr[V_n G^<_{n+1,n}]`.
96    pub fn interface_current(&self, sys: &TwoTerminal, n: usize) -> f64 {
97        // G^<_{n+1,n} = −(G^<_{n,n+1})†.
98        let v = &sys.couplings[n];
99        let g = self.next(n);
100        let mut acc = c64::new(0.0, 0.0);
101        for i in 0..v.nrows() {
102            for j in 0..v.ncols() {
103                acc -= v[(i, j)] * g[(i, j)].conj();
104            }
105        }
106        2.0 * acc.re
107    }
108
109    /// Occupied spectral density `Im G^<_aa / 2π` of every orbital of slice `n`.
110    pub fn occupation(&self, n: usize) -> Vec<f64> {
111        let g = self.diag(n);
112        (0..g.nrows()).map(|a| g[(a, a)].im / (2.0 * std::f64::consts::PI)).collect()
113    }
114
115    /// Spectral bond currents `2 Re Tr[H_ij G^<_ji]` between every pair of coupled sites
116    /// (inside a slice and between neighbouring slices), for systems built from a model.
117    pub fn bond_currents(&self, sys: &TwoTerminal) -> Vec<BondCurrent> {
118        assert!(!sys.regions.is_empty(), "bond currents need the slice regions (TwoTerminal::from_model)");
119        let mut out = Vec::new();
120        for (n, region) in sys.regions.iter().enumerate() {
121            let h = &sys.slices[n];
122            let g = self.diag(n);
123            for i in 0..region.len() {
124                for j in (i + 1)..region.len() {
125                    if let Some(c) = bond(h, &g, region, region, i, j, false) {
126                        out.push(BondCurrent { from: region.sites()[i], to: region.sites()[j], current: c });
127                    }
128                }
129            }
130            if n + 1 < sys.regions.len() {
131                let next = &sys.regions[n + 1];
132                let v = &sys.couplings[n];
133                let g = self.next(n);
134                for i in 0..region.len() {
135                    for j in 0..next.len() {
136                        if let Some(c) = bond(v, &g, region, next, i, j, true) {
137                            out.push(BondCurrent {
138                                from: region.sites()[i],
139                                to: next.sites()[j],
140                                current: c,
141                            });
142                        }
143                    }
144                }
145            }
146        }
147        out
148    }
149}
150
151/// `2 Re Tr[H_ij G^<_ji]` for site `i` of region `a` and site `j` of region `b`, or `None` when
152/// the sites are not coupled. `h` holds `⟨a| H |b⟩`; `g` is `G^<_{ab}` and `G^<_{ji}` is read
153/// as `−conj(G^<_{ij})` when `transposed` (different slices), or directly from `g` otherwise.
154fn bond(
155    h: &CMat,
156    g: &CMat,
157    a: &caroli_lattice::Region,
158    b: &caroli_lattice::Region,
159    i: usize,
160    j: usize,
161    transposed: bool,
162) -> Option<f64> {
163    let (oi, ni, oj, nj) = (a.offset(i), a.site_orbitals(i), b.offset(j), b.site_orbitals(j));
164    let mut coupled = false;
165    let mut acc = c64::new(0.0, 0.0);
166    for p in 0..ni {
167        for q in 0..nj {
168            let hij = h[(oi + p, oj + q)];
169            if hij != c64::new(0.0, 0.0) {
170                coupled = true;
171            }
172            let gji = if transposed { -g[(oi + p, oj + q)].conj() } else { g[(oj + q, oi + p)] };
173            acc += hij * gji;
174        }
175    }
176    coupled.then_some(2.0 * acc.re)
177}
178
179#[cfg(test)]
180mod tests {
181    use super::*;
182    use caroli_green::Lead;
183
184    fn one(x: f64) -> CMat {
185        CMat::from_fn(1, 1, |_, _| c64::new(x, 0.0))
186    }
187
188    fn chain(n: usize) -> TwoTerminal {
189        TwoTerminal {
190            slices: vec![one(0.0); n],
191            couplings: vec![one(-1.0); n - 1],
192            left: Lead { h00: one(0.0), h01: one(-1.0) },
193            right: Lead { h00: one(0.0), h01: one(-1.0) },
194            regions: Vec::new(),
195        }
196    }
197
198    /// Current conservation and Landauer: every interface carries T (f_L − f_R).
199    #[test]
200    fn interface_currents_equal_landauer() {
201        let mut sys = chain(7);
202        sys.slices[3] = one(0.8);
203        for (e, fl, fr) in [(0.3, 1.0, 0.0), (-0.9, 0.7, 0.2), (1.2, 0.0, 1.0)] {
204            let l = Lesser::compute(&sys, e, SelfEnergyMethod::Modes, fl, fr).unwrap();
205            let t = sys.transmission(e).unwrap();
206            assert!((l.transmission - t).abs() < 1e-12);
207            for n in 0..6 {
208                let j = l.interface_current(&sys, n);
209                assert!((j - t * (fl - fr)).abs() < 1e-12, "E {e}, interface {n}: {j} vs {}", t * (fl - fr));
210            }
211        }
212    }
213
214    /// In equilibrium (f_L = f_R = f), G^< = −f (G^r − G^a) = −2i f Im G^r, so the occupied
215    /// density Im G^< / 2π is −f Im G^r / π.
216    #[test]
217    fn equilibrium_occupation() {
218        let mut sys = chain(5);
219        sys.slices[2] = one(-0.4);
220        let e = 0.45;
221        let l = Lesser::compute(&sys, e, SelfEnergyMethod::Modes, 1.0, 1.0).unwrap();
222        let sw = sys.sweeps(e, SelfEnergyMethod::Modes).unwrap();
223        for n in 0..5 {
224            let gr = sw.diagonal(&sys, n)[(0, 0)];
225            // G^< = −f (G^r − G^a) = −2i f Im G^r.
226            let expected = -2.0 * gr.im / (2.0 * std::f64::consts::PI);
227            assert!((l.occupation(n)[0] - expected).abs() < 1e-12);
228        }
229    }
230
231    #[test]
232    fn fermi_function() {
233        assert_eq!(fermi(0.1, 0.1, 0.0), 1.0);
234        assert_eq!(fermi(0.2, 0.1, 0.0), 0.0);
235        assert!((fermi(0.1, 0.1, 0.01) - 0.5).abs() < 1e-15);
236        assert!(fermi(10.0, 0.0, 0.01) < 1e-300);
237    }
238}