1use crate::modes::{LeadModes, sigma_right};
17use crate::{GreenError, adj_mul, inverse, mul, sandwich};
18use caroli_model::{CMat, c64};
19use std::rc::Rc;
20
21#[derive(Clone, Debug)]
23pub struct SMatrix {
24 pub energy: f64,
26 pub blocks: Vec<Vec<CMat>>,
29 pub incoming: Vec<Vec<c64>>,
32 pub outgoing: Vec<Vec<c64>>,
35}
36
37impl SMatrix {
38 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 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 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
97pub(crate) struct LeadSolution {
100 pub(crate) modes: LeadModes,
101 pub(crate) rest: CMat,
102 pub(crate) g: CMat,
103}
104
105impl LeadSolution {
106 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 pub(crate) fn self_energy(&self, coupling: &CMat) -> CMat {
119 sandwich(coupling, &self.g)
120 }
121}
122
123pub(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
133pub(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
140pub(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 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 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}