Skip to main content

caroli_dynamics/
driven.rs

1//! Time evolution of any region of a [`Model`] under a uniform vector potential `A(t)`.
2//!
3//! Each hopping block of the Hamiltonian picks up the Peierls factor `exp(−i A(t)·d)`, `d` being
4//! the displacement of its bond ([`Model::bonds`]), so that a constant force `F` (an electric
5//! field, the charge absorbed) is `A(t) = −F t` and the crystal momentum moves as `k + F t`. The
6//! steps are those of the fourth-order commutator-free Magnus integrator ([`magnus4_step`]): any
7//! geometry, open or periodic, with any disorder, magnetic field or hopping term of the model.
8
9use crate::{TimeDependent, magnus4_step};
10use caroli_lattice::{Cell, Region, Vec3};
11use caroli_model::{Model, SparseMatrix, c64};
12use rayon::prelude::*;
13use std::sync::Arc;
14
15/// A uniform vector potential `A(t)` (cartesian, in the inverse length unit of the lattice).
16pub type VectorPotential = Arc<dyn Fn(f64) -> Vec3 + Send + Sync>;
17
18/// The vector potential of a constant force `F` switched on at `t = 0`: `A(t) = −F t`.
19pub fn constant_force(force: Vec3) -> VectorPotential {
20    Arc::new(move |t: f64| [-force[0] * t, -force[1] * t, -force[2] * t])
21}
22
23/// Rows above which the products run in parallel.
24const PARALLEL_ROWS: usize = 1 << 14;
25
26/// The Hamiltonian `H(t)` of a region under a uniform vector potential, as a sparse matrix whose
27/// entries carry the displacement class of their bond.
28pub struct Driven {
29    n: usize,
30    row_ptr: Vec<usize>,
31    cols: Vec<u32>,
32    vals: Vec<c64>,
33    /// Displacement class of each entry; class 0 is the on-site one.
34    class: Vec<u16>,
35    displacements: Vec<Vec3>,
36    bounds: (f64, f64),
37    potential: VectorPotential,
38}
39
40impl Driven {
41    /// The region repeated along `periods` (open if empty), without field.
42    pub fn new(model: &Model, region: &Region, periods: &[Cell]) -> Self {
43        let mut displacements: Vec<Vec3> = vec![[0.0; 3]];
44        let mut keys: std::collections::HashMap<[i64; 3], u16> = [([0; 3], 0u16)].into();
45        let key = |d: Vec3| d.map(|x| (x * 1e9).round() as i64);
46        let mut entries: Vec<(usize, usize, u16, c64)> = Vec::new();
47        for (i, site) in region.sites().iter().enumerate() {
48            let m = model.onsite_matrix(site);
49            let o = region.offset(i);
50            for a in 0..m.nrows() {
51                for b in 0..m.ncols() {
52                    if m[(a, b)] != c64::new(0.0, 0.0) {
53                        entries.push((o + a, o + b, 0, m[(a, b)]));
54                    }
55                }
56            }
57        }
58        for bond in model.bonds(region, periods) {
59            let class = *keys.entry(key(bond.displacement)).or_insert_with(|| {
60                displacements.push(bond.displacement);
61                u16::try_from(displacements.len() - 1).expect("more than 65 536 bond displacements")
62            });
63            let (r, c) = (region.offset(bond.from), region.offset(bond.to));
64            for a in 0..bond.matrix.nrows() {
65                for b in 0..bond.matrix.ncols() {
66                    if bond.matrix[(a, b)] != c64::new(0.0, 0.0) {
67                        entries.push((r + a, c + b, class, bond.matrix[(a, b)]));
68                    }
69                }
70            }
71        }
72        entries.sort_unstable_by_key(|&(r, c, k, _)| (r, c, k));
73        let n = region.orbitals();
74        assert!(n <= u32::MAX as usize, "dimension {n} exceeds the u32 column index");
75        let mut row_ptr = vec![0usize; n + 1];
76        let (mut cols, mut vals, mut class) = (Vec::new(), Vec::new(), Vec::new());
77        let mut last = None;
78        for (r, c, k, v) in entries {
79            if last == Some((r, c, k)) {
80                *vals.last_mut().expect("entry") += v;
81            } else {
82                cols.push(c as u32);
83                vals.push(v);
84                class.push(k);
85                row_ptr[r + 1] += 1;
86                last = Some((r, c, k));
87            }
88        }
89        for r in 0..n {
90            row_ptr[r + 1] += row_ptr[r];
91        }
92        // Gershgorin bounds: the Peierls factors leave the moduli unchanged.
93        let mut bounds = (f64::INFINITY, f64::NEG_INFINITY);
94        for r in 0..n {
95            let (mut centre, mut radius) = (0.0, 0.0);
96            for k in row_ptr[r]..row_ptr[r + 1] {
97                if cols[k] as usize == r && class[k] == 0 {
98                    centre += vals[k].re
99                } else {
100                    radius += vals[k].norm()
101                }
102            }
103            bounds = (bounds.0.min(centre - radius), bounds.1.max(centre + radius));
104        }
105        Self { n, row_ptr, cols, vals, class, displacements, bounds, potential: Arc::new(|_| [0.0; 3]) }
106    }
107
108    /// A constant force `F` from `t = 0` (see [`constant_force`]).
109    pub fn with_force(self, force: Vec3) -> Self {
110        self.with_vector_potential(constant_force(force))
111    }
112
113    /// Any uniform vector potential `A(t)`.
114    pub fn with_vector_potential(mut self, potential: VectorPotential) -> Self {
115        self.potential = potential;
116        self
117    }
118
119    /// Dimension of the wave functions.
120    pub fn len(&self) -> usize {
121        self.n
122    }
123
124    /// Whether the dimension is zero (a region without orbitals).
125    pub fn is_empty(&self) -> bool {
126        self.n == 0
127    }
128
129    /// `Σ_i w_i exp(−i A(t_i)·d_c)` for each displacement class.
130    fn phases(&self, stages: &[(f64, f64)]) -> Vec<c64> {
131        let potentials: Vec<(Vec3, f64)> = stages.iter().map(|&(t, w)| ((self.potential)(t), w)).collect();
132        self.displacements
133            .iter()
134            .map(|d| {
135                potentials
136                    .iter()
137                    .map(|(a, w)| c64::from_polar(*w, -(a[0] * d[0] + a[1] * d[1] + a[2] * d[2])))
138                    .sum()
139            })
140            .collect()
141    }
142
143    /// `y = Σ_r M_r x` over the rows, with `M = Σ_entries v · table[class]`.
144    fn product(&self, table: &[c64], x: &[c64], y: &mut [c64]) {
145        assert_eq!((x.len(), y.len()), (self.n, self.n));
146        let row = |(r, out): (usize, &mut c64)| {
147            let mut acc = c64::new(0.0, 0.0);
148            for k in self.row_ptr[r]..self.row_ptr[r + 1] {
149                acc += self.vals[k] * table[self.class[k] as usize] * x[self.cols[k] as usize];
150            }
151            *out = acc;
152        };
153        if self.n >= PARALLEL_ROWS {
154            y.par_iter_mut().enumerate().with_min_len(4096).for_each(row);
155        } else {
156            y.iter_mut().enumerate().for_each(row);
157        }
158    }
159
160    /// `H(t)` as a sparse matrix.
161    pub fn hamiltonian(&self, t: f64) -> SparseMatrix {
162        let table = &self.phases(&[(t, 1.0)]);
163        let entries = (0..self.n)
164            .flat_map(|r| {
165                (self.row_ptr[r]..self.row_ptr[r + 1])
166                    .map(move |k| (r, self.cols[k] as usize, self.vals[k] * table[self.class[k] as usize]))
167            })
168            .collect();
169        SparseMatrix::from_triplets(self.n, entries)
170    }
171
172    /// `⟨ψ| v |ψ⟩` along `direction` at time `t`, with the velocity `v = i[H(t), r]`
173    /// (`v_ij = i H_ij(t) d_ij·direction`).
174    pub fn velocity(&self, psi: &[c64], t: f64, direction: Vec3) -> f64 {
175        let table = self.phases(&[(t, 1.0)]);
176        let along: Vec<f64> = self
177            .displacements
178            .iter()
179            .map(|d| d[0] * direction[0] + d[1] * direction[1] + d[2] * direction[2])
180            .collect();
181        (0..self.n)
182            .into_par_iter()
183            .with_min_len(4096)
184            .map(|r| {
185                let mut acc = c64::new(0.0, 0.0);
186                for k in self.row_ptr[r]..self.row_ptr[r + 1] {
187                    let c = self.class[k] as usize;
188                    acc += self.vals[k] * table[c] * along[c] * psi[self.cols[k] as usize];
189                }
190                (psi[r].conj() * c64::new(0.0, 1.0) * acc).re
191            })
192            .sum()
193    }
194
195    /// One Magnus step `ψ(t) → ψ(t + dt)`, the exponentials exact to `tol`.
196    pub fn step(&self, psi: &mut [c64], t: f64, dt: f64, tol: f64) {
197        magnus4_step(self, psi, t, dt, tol);
198    }
199}
200
201impl TimeDependent for Driven {
202    fn apply(&self, stages: &[(f64, f64)], x: &[c64], y: &mut [c64]) {
203        self.product(&self.phases(stages), x, y);
204    }
205
206    fn bounds(&self) -> (f64, f64) {
207        self.bounds
208    }
209}