Skip to main content

caroli_green/
smatrix.rs

1//! Scattering matrix in the basis of the propagating lead modes, as `kwant.smatrix`.
2//!
3//! A lead `p` has cells `n = 1, 2, …` going away from the device, the hopping
4//! `H1 = ⟨n| H |n+1⟩`, and the coupling `V = ⟨device| H |1⟩`. With `F` the Bloch matrix of its
5//! outgoing and decaying modes, `H1 F` is the self-energy of the cells beyond the first and
6//! `g = (E − H0 − H1 F)⁻¹` the surface Green's function of the first cell.
7//!
8//! - An incoming mode `φ λ^n` of lead `q` acts on the device as the source `V_q s` with
9//!   `s = (λ − g H1†) φ`, so the device wave is `ψ_D = G V_q s`.
10//! - The outgoing wave on the first cell of lead `p` is `ψ_1 = g V_p† ψ_D`, minus `g H1† φ` in
11//!   lead `q`; on the second cell, `H1 ψ_2 = H1 F ψ_1`.
12//! - Its amplitude on an outgoing mode `a` is the current overlap
13//!   `J(a, ψ) = i(a_1† H1 ψ_2 − a_2† H1† ψ_1)`: evanescent modes and the other propagating modes
14//!   carry no cross current, and the modes carry unit current.
15
16use crate::modes::{LeadModes, sigma_right};
17use crate::{GreenError, adj_mul, inverse, mul, sandwich};
18use caroli_model::{CMat, c64};
19use std::rc::Rc;
20
21/// Scattering matrix at one energy in the basis of the propagating lead modes.
22#[derive(Clone, Debug)]
23pub struct SMatrix {
24    /// Energy `E` of the scattering matrix.
25    pub energy: f64,
26    /// `blocks[p][q]`: amplitudes into the outgoing modes of lead `p` (rows) from the incoming
27    /// modes of lead `q` (columns), all modes normalised to unit current.
28    pub blocks: Vec<Vec<CMat>>,
29    /// Bloch factors `λ` of the incoming and outgoing modes of each lead, along the lead away
30    /// from the device (`ψ_n = φ λ^n`, `n` the cell).
31    pub incoming: Vec<Vec<c64>>,
32    /// Bloch factors `λ` of the outgoing modes of each lead: `outgoing[p]` follows the rows of
33    /// `blocks[p][q]`, as `incoming[q]` its columns.
34    pub outgoing: Vec<Vec<c64>>,
35}
36
37impl SMatrix {
38    /// `Σ |S_pq|²`: the transmission from lead `q` into lead `p` (a reflection for `p = q`).
39    pub fn transmission(&self, p: usize, q: usize) -> f64 {
40        let b = &self.blocks[p][q];
41        (0..b.ncols())
42            .flat_map(|c| (0..b.nrows()).map(move |r| (r, c)))
43            .map(|(r, c)| b[(r, c)].norm_sqr())
44            .sum()
45    }
46
47    /// The whole matrix: rows are the outgoing modes of every lead in turn, columns the incoming
48    /// ones.
49    pub fn full(&self) -> CMat {
50        let rows: Vec<usize> = self.outgoing.iter().map(Vec::len).collect();
51        let cols: Vec<usize> = self.incoming.iter().map(Vec::len).collect();
52        let mut out = CMat::zeros(rows.iter().sum(), cols.iter().sum());
53        let mut r0 = 0;
54        for (p, &nr) in rows.iter().enumerate() {
55            let mut c0 = 0;
56            for (q, &nc) in cols.iter().enumerate() {
57                out.as_mut().submatrix_mut(r0, c0, nr, nc).copy_from(&self.blocks[p][q]);
58                c0 += nc;
59            }
60            r0 += nr;
61        }
62        out
63    }
64
65    /// The scattering matrix of a device made of independent blocks: the modes of a lead are
66    /// those of every block in turn, and the blocks do not mix.
67    pub(crate) fn assemble(energy: f64, parts: Vec<BlockScattering>) -> Self {
68        let n = parts.first().map_or(0, |p| p.incoming.len());
69        let (mut incoming, mut outgoing) = (vec![Vec::new(); n], vec![Vec::new(); n]);
70        for part in &parts {
71            for p in 0..n {
72                incoming[p].extend_from_slice(&part.incoming[p]);
73                outgoing[p].extend_from_slice(&part.outgoing[p]);
74            }
75        }
76        let blocks = (0..n)
77            .map(|p| {
78                (0..n)
79                    .map(|q| {
80                        let mut out = CMat::zeros(outgoing[p].len(), incoming[q].len());
81                        let (mut r0, mut c0) = (0, 0);
82                        for part in &parts {
83                            let b = &part.blocks[p][q];
84                            out.as_mut().submatrix_mut(r0, c0, b.nrows(), b.ncols()).copy_from(b);
85                            r0 += part.outgoing[p].len();
86                            c0 += part.incoming[q].len();
87                        }
88                        out
89                    })
90                    .collect()
91            })
92            .collect();
93        Self { energy, blocks, incoming, outgoing }
94    }
95}
96
97/// A lead solved at one energy: its modes, `H1 F` (the self-energy of the cells beyond the
98/// first) and the surface Green's function `g` of its first cell.
99pub(crate) struct LeadSolution {
100    pub(crate) modes: LeadModes,
101    pub(crate) rest: CMat,
102    pub(crate) g: CMat,
103}
104
105impl LeadSolution {
106    /// The lead `(h00, h01)`, `h01` going away from the device.
107    pub(crate) fn new(h00: &CMat, h01: &CMat, z: c64) -> Result<Self, GreenError> {
108        let modes = LeadModes::at(h00, h01, z)?;
109        let rest = sigma_right(h01, &modes);
110        let k = h00.nrows();
111        let g = inverse(&CMat::from_fn(k, k, |i, j| {
112            (if i == j { z } else { c64::new(0.0, 0.0) }) - h00[(i, j)] - rest[(i, j)]
113        }));
114        Ok(Self { modes, rest, g })
115    }
116
117    /// `Σ = V g V†` on the device orbitals the lead couples to.
118    pub(crate) fn self_energy(&self, coupling: &CMat) -> CMat {
119        sandwich(coupling, &self.g)
120    }
121}
122
123/// A lead of a block for [`scattering_blocks`]: where its device orbitals sit in the root, its
124/// coupling `V` and outward hopping `H1` (borrowed from the system: they are often the same
125/// matrix), and its solution.
126pub(crate) struct ScatteringLead<'a> {
127    pub(crate) at: Vec<usize>,
128    pub(crate) coupling: &'a CMat,
129    pub(crate) h01: &'a CMat,
130    pub(crate) solution: Rc<LeadSolution>,
131}
132
133/// The scattering matrix of one block: `(incoming λ, outgoing λ, S_pq)` per lead.
134pub(crate) struct BlockScattering {
135    pub(crate) incoming: Vec<Vec<c64>>,
136    pub(crate) outgoing: Vec<Vec<c64>>,
137    pub(crate) blocks: Vec<Vec<CMat>>,
138}
139
140/// `S_pq` for every pair of leads of a block, from the Green's function `green` of the root
141/// (the device orbitals the leads couple to) with the self-energies of these leads included.
142pub(crate) fn scattering_blocks(green: &CMat, leads: &[Option<ScatteringLead<'_>>]) -> BlockScattering {
143    let n = leads.len();
144    let i = c64::new(0.0, 1.0);
145    let lambdas = |f: fn(&LeadModes) -> &Vec<c64>| -> Vec<Vec<c64>> {
146        leads.iter().map(|l| l.as_ref().map_or(Vec::new(), |l| f(&l.solution.modes).clone())).collect()
147    };
148    let incoming = lambdas(|m| &m.left_lambdas);
149    let outgoing = lambdas(|m| &m.right_lambdas);
150    let mut blocks = vec![vec![CMat::zeros(0, 0); n]; n];
151    for q in 0..n {
152        for p in 0..n {
153            blocks[p][q] = CMat::zeros(outgoing[p].len(), incoming[q].len());
154        }
155        let Some(lq) = &leads[q] else { continue };
156        let modes_q = &lq.solution.modes;
157        let (phi_in, lam_in) = (&modes_q.left_modes, &modes_q.left_lambdas);
158        // g H1† Φ_in, and the source s = Φ_in λ − g H1† Φ_in on the first cell.
159        let back = mul(&lq.solution.g, &adj_mul(lq.h01, phi_in));
160        let source =
161            CMat::from_fn(phi_in.nrows(), phi_in.ncols(), |a, c| phi_in[(a, c)] * lam_in[c] - back[(a, c)]);
162        let device_source = mul(lq.coupling, &source);
163        for p in 0..n {
164            let Some(lp) = &leads[p] else { continue };
165            let gpq = CMat::from_fn(lp.at.len(), lq.at.len(), |a, b| green[(lp.at[a], lq.at[b])]);
166            let psi_device = mul(&gpq, &device_source);
167            let mut psi1 = mul(&lp.solution.g, &adj_mul(lp.coupling, &psi_device));
168            if p == q {
169                psi1 -= &back;
170            }
171            // S = i diag(λ̄) [Φ_out† (H1 F) ψ_1 − diag(λ̄) Φ_out† H1† ψ_1].
172            let modes_p = &lp.solution.modes;
173            let (phi_out, lam_out) = (&modes_p.right_modes, &modes_p.right_lambdas);
174            let a = adj_mul(phi_out, &mul(&lp.solution.rest, &psi1));
175            let b = adj_mul(phi_out, &adj_mul(lp.h01, &psi1));
176            blocks[p][q] = CMat::from_fn(phi_out.ncols(), psi1.ncols(), |m, c| {
177                let conj = lam_out[m].conj();
178                i * conj * (a[(m, c)] - conj * b[(m, c)])
179            });
180        }
181    }
182    BlockScattering { incoming, outgoing, blocks }
183}