Rustb 0.7.1

A package for calculating band, angle state, linear and nonlinear conductivities based on tight-binding models
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Kernel evaluators at quadrature points.
//!
//! Each function takes interpolated band energies $E_n(q)$ and the
//! gauge‑invariant velocity kernel $K^{ab}_{nm}(q) = v^a_{nm}v^b_{mn}$
//! at a single quadrature point $q$, then evaluates the singular
//! denominator:
//!
//! | Function | Denominator | Returns |
//! |----------|-------------|---------|
//! | `eval_berry_kernel` | $\Delta_{nm}^2 + \eta^2$ | $(g_n, \Omega_n)$ per band (all bands) |
//! | `eval_berry_band_at_lam` | $\Delta_{nm}^2 + \eta^2$ | $\Omega_n$ (single band at barycentrics) |
//! | `eval_berry_complex_at_lam` | $\Delta_{nm}^2 + \eta^2$ | $(g_n, \Omega_n)$ (single band) |
//! | `eval_intrinsic_G_at_lam` | $\Delta_{nm}^3$ | $G^{ij}_n$ (single band, no $\eta$) |
//! | `eval_optical_kernel` | $\Delta_{nm}^2 - (\omega+i\eta)^2$ | $\sum_{nm} (f_n-f_m)K_{nm}/{\rm denom}$ |
//!
//! The single‑band functions (`_at_lam`) interpolate only the $n$‑th row
//! of $K$ and avoid allocating the full $nsta\times nsta$ matrix.
//!
//! The single‑simplex quadrature helpers (`quadrature_berry_simplex`
//! etc.) loop over all quadrature points, interpolate, call the
//! evaluator, and accumulate with quadrature weights.

use ndarray::prelude::*;
use num_complex::Complex;

use crate::thermodynamics::{Occupation, fermi_derivative_from_width, fermi_from_width};

use super::quadrature::*;
use super::types::TrackedSimplex;

// ── Fermi functions ─────────────────────────────────────────────────────

#[inline]
pub(crate) fn fermi(e: f64, mu: f64, thermal_width: f64) -> f64 {
    fermi_from_width(e, mu, thermal_width)
}

#[inline]
#[allow(dead_code)]
pub(crate) fn fermi_deriv(e: f64, mu: f64, thermal_width: f64) -> f64 {
    if thermal_width == 0.0 {
        0.0
    } else {
        fermi_derivative_from_width(e, mu, thermal_width)
    }
}

// ── Berry / QGT kernel ──────────────────────────────────────────────────

/// Evaluate `G_n = Σ_{m≠n} K_nm / ((E_n−E_m)² + η²)` at one quadrature point.
///
/// Returns per‑band `(metric_n, berry_n)` where
/// `g_n = Re G_n`, `Ω_n = −2 Im G_n`.
pub(crate) fn eval_berry_kernel(
    band_q: &[f64],
    k_ab_q: &Array2<Complex<f64>>,
    eta: f64,
    nsta: usize,
) -> (Array1<f64>, Array1<f64>) {
    let mut metric = Array1::<f64>::zeros(nsta);
    let mut berry = Array1::<f64>::zeros(nsta);
    let eta2 = eta * eta;
    for n in 0..nsta {
        let mut g_sum = Complex::new(0.0, 0.0);
        for m in 0..nsta {
            if m == n {
                continue;
            }
            let de = band_q[n] - band_q[m];
            let denom = de * de + eta2;
            if denom < 1e-30 {
                continue;
            }
            g_sum += k_ab_q[[n, m]] / denom;
        }
        metric[n] = g_sum.re;
        berry[n] = -2.0 * g_sum.im;
    }
    (metric, berry)
}

/// Evaluate $\Omega_n$ for a single band at one quadrature point.
///
/// Interpolates $E_m(q)$ and $K_{nm}(q)$ at barycentric coords `lam`,
/// then computes $\Omega_n = -2\,\mathrm{Im}\sum_{m\ne n} K_{nm}/(\Delta_{nm}^2+\eta^2)$.
/// Avoids allocating the full $K$ matrix and computing $\Omega$ for other bands.
#[allow(dead_code)]
pub(crate) fn eval_berry_band_at_lam(
    n: usize,
    bands: &[Vec<f64>],
    kmats: &[Array2<Complex<f64>>],
    lam: &[f64],
    eta: f64,
    nsta: usize,
) -> f64 {
    // Interpolate E_m for all bands
    let mut e_q = vec![0.0; nsta];
    for v in 0..bands.len() {
        let lv = lam[v];
        if lv == 0.0 {
            continue;
        }
        for m in 0..nsta {
            e_q[m] += bands[v][m] * lv;
        }
    }

    // Interpolate K_{nm} for row n only
    let mut k_row = vec![Complex::new(0.0, 0.0); nsta];
    for v in 0..kmats.len() {
        let lv = lam[v];
        if lv == 0.0 {
            continue;
        }
        for m in 0..nsta {
            k_row[m] += kmats[v][[n, m]] * lv;
        }
    }

    let eta2 = eta * eta;
    let mut g_sum = Complex::new(0.0, 0.0);
    for m in 0..nsta {
        if m == n {
            continue;
        }
        let de = e_q[n] - e_q[m];
        let denom = de * de + eta2;
        if denom < 1e-30 {
            continue;
        }
        g_sum += k_row[m] / denom;
    }
    -2.0 * g_sum.im
}

/// Pre‑allocated buffer version: avoids Vec allocation on every call.
#[inline]
pub(crate) fn eval_berry_band_at_lam_buf(
    n: usize,
    bands: &[&[f64]],
    kmats: &[&Array2<Complex<f64>>],
    lam: &[f64],
    eta: f64,
    nsta: usize,
    e_buf: &mut [f64],
    k_buf: &mut [Complex<f64>],
) -> f64 {
    e_buf[..nsta].fill(0.0);
    k_buf[..nsta].fill(Complex::new(0.0, 0.0));
    for v in 0..bands.len() {
        let lv = lam[v];
        if lv == 0.0 {
            continue;
        }
        for m in 0..nsta {
            e_buf[m] += bands[v][m] * lv;
        }
    }
    for v in 0..kmats.len() {
        let lv = lam[v];
        if lv == 0.0 {
            continue;
        }
        for m in 0..nsta {
            k_buf[m] += kmats[v][[n, m]] * lv;
        }
    }
    let eta2 = eta * eta;
    let mut g_sum = Complex::new(0.0, 0.0);
    for m in 0..nsta {
        if m == n {
            continue;
        }
        let de = e_buf[n] - e_buf[m];
        let denom = de * de + eta2;
        if denom < 1e-30 {
            continue;
        }
        g_sum += k_buf[m] / denom;
    }
    -2.0 * g_sum.im
}

/// Evaluate $G_n = \Sigma_{m\ne n} K_{nm} / (\Delta_{nm}^2 + \eta^2)$
/// for a single band at barycentrics, returning `(metric_n, berry_n)`.
/// Interpolates only $E_m$ and the $n$-th row of $K$.
#[allow(dead_code)]
pub(crate) fn eval_berry_complex_at_lam(
    n: usize,
    bands: &[Vec<f64>],
    kmats: &[Array2<Complex<f64>>],
    lam: &[f64],
    eta: f64,
    nsta: usize,
) -> (f64, f64) {
    let mut e_q = vec![0.0; nsta];
    for v in 0..bands.len() {
        let lv = lam[v];
        if lv == 0.0 {
            continue;
        }
        for m in 0..nsta {
            e_q[m] += bands[v][m] * lv;
        }
    }
    let mut k_row = vec![Complex::new(0.0, 0.0); nsta];
    for v in 0..kmats.len() {
        let lv = lam[v];
        if lv == 0.0 {
            continue;
        }
        for m in 0..nsta {
            k_row[m] += kmats[v][[n, m]] * lv;
        }
    }
    let eta2 = eta * eta;
    let mut g_sum = Complex::new(0.0, 0.0);
    for m in 0..nsta {
        if m == n {
            continue;
        }
        let de = e_q[n] - e_q[m];
        let denom = de * de + eta2;
        if denom < 1e-30 {
            continue;
        }
        g_sum += k_row[m] / denom;
    }
    (g_sum.re, -2.0 * g_sum.im)
}

/// Pre‑allocated buffer version of [`eval_berry_complex_at_lam`].
#[inline]
pub(crate) fn eval_berry_complex_at_lam_buf(
    n: usize,
    bands: &[&[f64]],
    kmats: &[&Array2<Complex<f64>>],
    lam: &[f64],
    eta: f64,
    nsta: usize,
    e_buf: &mut [f64],
    k_buf: &mut [Complex<f64>],
) -> (f64, f64) {
    e_buf[..nsta].fill(0.0);
    k_buf[..nsta].fill(Complex::new(0.0, 0.0));
    for v in 0..bands.len() {
        let lv = lam[v];
        if lv == 0.0 {
            continue;
        }
        for m in 0..nsta {
            e_buf[m] += bands[v][m] * lv;
        }
    }
    for v in 0..kmats.len() {
        let lv = lam[v];
        if lv == 0.0 {
            continue;
        }
        for m in 0..nsta {
            k_buf[m] += kmats[v][[n, m]] * lv;
        }
    }
    let eta2 = eta * eta;
    let mut g_sum = Complex::new(0.0, 0.0);
    for m in 0..nsta {
        if m == n {
            continue;
        }
        let de = e_buf[n] - e_buf[m];
        let denom = de * de + eta2;
        if denom < 1e-30 {
            continue;
        }
        g_sum += k_buf[m] / denom;
    }
    (g_sum.re, -2.0 * g_sum.im)
}

/// Evaluate $G^{ij}_n = \operatorname{Re} \sum_{m\ne n} K_{nm} / (E_n-E_m)^3$
/// for a single band at barycentrics (no $\eta$ regularization — used for
/// intrinsic NLH).
#[inline]
#[allow(dead_code)]
pub(crate) fn eval_intrinsic_G_at_lam(
    n: usize,
    bands: &[Vec<f64>],
    kmats: &[Array2<Complex<f64>>],
    lam: &[f64],
    nsta: usize,
) -> f64 {
    let mut e_q = vec![0.0; nsta];
    for v in 0..bands.len() {
        let lv = lam[v];
        if lv == 0.0 {
            continue;
        }
        for m in 0..nsta {
            e_q[m] += bands[v][m] * lv;
        }
    }
    let mut k_row = vec![Complex::new(0.0, 0.0); nsta];
    for v in 0..kmats.len() {
        let lv = lam[v];
        if lv == 0.0 {
            continue;
        }
        for m in 0..nsta {
            k_row[m] += kmats[v][[n, m]] * lv;
        }
    }
    let mut g_sum = 0.0f64;
    for m in 0..nsta {
        if m == n {
            continue;
        }
        let de = e_q[n] - e_q[m];
        let de3 = de * de * de;
        if de3.abs() < 1e-30 {
            continue;
        }
        g_sum += k_row[m].re / de3;
    }
    g_sum
}

/// Pre‑allocated buffer version of [`eval_intrinsic_G_at_lam`].
#[inline]
#[allow(dead_code)]
pub(crate) fn eval_intrinsic_G_at_lam_buf(
    n: usize,
    bands: &[&[f64]],
    kmats: &[&Array2<Complex<f64>>],
    lam: &[f64],
    nsta: usize,
    e_buf: &mut [f64],
    k_buf: &mut [Complex<f64>],
) -> f64 {
    e_buf[..nsta].fill(0.0);
    k_buf[..nsta].fill(Complex::new(0.0, 0.0));
    for v in 0..bands.len() {
        let lv = lam[v];
        if lv == 0.0 {
            continue;
        }
        for m in 0..nsta {
            e_buf[m] += bands[v][m] * lv;
        }
    }
    for v in 0..kmats.len() {
        let lv = lam[v];
        if lv == 0.0 {
            continue;
        }
        for m in 0..nsta {
            k_buf[m] += kmats[v][[n, m]] * lv;
        }
    }
    let mut g_sum = 0.0f64;
    for m in 0..nsta {
        if m == n {
            continue;
        }
        let de = e_buf[n] - e_buf[m];
        let de3 = de * de * de;
        if de3.abs() < 1e-30 {
            continue;
        }
        g_sum += k_buf[m].re / de3;
    }
    g_sum
}

/// Evaluate $G^{ab}_n$, $G^{bc}_n$, $G^{ac}_n$ in one fused pass.
///
/// Interpolates $E_m$ once, then computes the three G components
/// reusing the same energy interpolation.  Saves 2 redundant energy
/// interpolations vs three separate `eval_intrinsic_G_at_lam_buf` calls.
#[inline]
pub(crate) fn eval_intrinsic_G3_at_lam_buf(
    n: usize,
    bands: &[&[f64]],
    kmat_ab: &[&Array2<Complex<f64>>],
    kmat_bc: &[&Array2<Complex<f64>>],
    kmat_ac: &[&Array2<Complex<f64>>],
    lam: &[f64],
    nsta: usize,
    e_buf: &mut [f64],
    k_buf: &mut [Complex<f64>],
) -> (f64, f64, f64) {
    // Interpolate energies + all three K rows in one pass.
    e_buf[..nsta].fill(0.0);
    let (k_ab_row, rest) = k_buf.split_at_mut(nsta);
    let (k_bc_row, k_ac_row) = rest.split_at_mut(nsta);
    k_ab_row.fill(Complex::new(0.0, 0.0));
    k_bc_row.fill(Complex::new(0.0, 0.0));
    k_ac_row.fill(Complex::new(0.0, 0.0));
    for v in 0..bands.len() {
        let lv = lam[v];
        if lv == 0.0 {
            continue;
        }
        for m in 0..nsta {
            e_buf[m] += bands[v][m] * lv;
            k_ab_row[m] += kmat_ab[v][[n, m]] * lv;
            k_bc_row[m] += kmat_bc[v][[n, m]] * lv;
            k_ac_row[m] += kmat_ac[v][[n, m]] * lv;
        }
    }
    let en = e_buf[n];
    let mut g_ab = 0.0f64;
    let mut g_bc = 0.0f64;
    let mut g_ac = 0.0f64;
    for m in 0..nsta {
        if m == n {
            continue;
        }
        let de = en - e_buf[m];
        let de3 = de * de * de;
        if de3.abs() < 1e-30 {
            continue;
        }
        g_ab += k_ab_row[m].re / de3;
        g_bc += k_bc_row[m].re / de3;
        g_ac += k_ac_row[m].re / de3;
    }
    (g_ab, g_bc, g_ac)
}

// ── Optical kernel ──────────────────────────────────────────────────────

/// Evaluate the optical conductivity kernel at one quadrature point.
///
/// ```text
/// σ_nm = (f_n − f_m) · K_nm / (d² − (ω+iη)²)
/// ```
pub(crate) fn eval_optical_kernel(
    band_q: &[f64],
    k_ab_q: &Array2<Complex<f64>>,
    omega: f64,
    eta: f64,
    mu: f64,
    thermal_width: f64,
    nsta: usize,
) -> Complex<f64> {
    let mut total = Complex::new(0.0, 0.0);
    let w_plus_ieta = Complex::new(omega, eta);
    let denom_shift = w_plus_ieta * w_plus_ieta;
    for n in 0..nsta {
        let fn_val = fermi(band_q[n], mu, thermal_width);
        for m in 0..nsta {
            if m == n {
                continue;
            }
            let fm_val = fermi(band_q[m], mu, thermal_width);
            let df = fn_val - fm_val;
            if df.abs() < 1e-30 {
                continue;
            }
            let d = band_q[n] - band_q[m];
            let denom = d * d - denom_shift;
            if denom.norm_sqr() < 1e-30 {
                continue;
            }
            total += df * k_ab_q[[n, m]] / denom;
        }
    }
    total
}

// ── Quadrature over single simplex ──────────────────────────────────────

/// Quadrature over one simplex for the Berry + metric kernel.
#[allow(dead_code)]
pub(crate) fn quadrature_berry_simplex(sim: &TrackedSimplex, eta: f64) -> (f64, f64) {
    let d = sim.vertices.len() - 1;
    let nsta = sim.vertices[0].band.len();
    let nv = d + 1;
    let bands: Vec<Vec<f64>> = (0..nv).map(|v| sim.vertices[v].band.to_vec()).collect();
    let kmats: Vec<Array2<Complex<f64>>> = (0..nv).map(|v| sim.vertices[v].k_ab.clone()).collect();

    let mut total_g = 0.0;
    let mut total_o = 0.0;
    if d == 2 {
        for iq in 0..3 {
            let lam = TRI_QUAD_PTS_3[iq].as_slice();
            let w = TRI_QUAD_WTS_3[iq];
            let band_q = bary_interp_band(&bands, lam, nsta);
            let k_ab_q = bary_interp_matrix(&kmats, lam);
            let (g_n, o_n) = eval_berry_kernel(&band_q, &k_ab_q, eta, nsta);
            total_g += w * g_n.iter().copied().sum::<f64>();
            total_o += w * o_n.iter().copied().sum::<f64>();
        }
    } else {
        for iq in 0..4 {
            let lam = TET_QUAD_PTS_4[iq].as_slice();
            let w = TET_QUAD_WTS_4[iq];
            let band_q = bary_interp_band(&bands, lam, nsta);
            let k_ab_q = bary_interp_matrix(&kmats, lam);
            let (g_n, o_n) = eval_berry_kernel(&band_q, &k_ab_q, eta, nsta);
            total_g += w * g_n.iter().copied().sum::<f64>();
            total_o += w * o_n.iter().copied().sum::<f64>();
        }
    }
    (total_g * sim.volume, total_o * sim.volume)
}

/// Occupation-weighted Berry curvature and quantum metric on one simplex.
pub(crate) fn quadrature_occupied_geometry_simplex(
    sim: &TrackedSimplex,
    eta: f64,
    chemical_potentials: &Array1<f64>,
    occupation: Occupation,
) -> (Array1<f64>, Array1<f64>) {
    let dimension = sim.vertices.len() - 1;
    let nsta = sim.vertices[0].band.len();
    let vertex_count = dimension + 1;
    let bands: Vec<Vec<f64>> = (0..vertex_count)
        .map(|vertex| sim.vertices[vertex].band.to_vec())
        .collect();
    let kernels: Vec<Array2<Complex<f64>>> = (0..vertex_count)
        .map(|vertex| sim.vertices[vertex].k_ab.clone())
        .collect();
    let mut metric = Array1::<f64>::zeros(chemical_potentials.len());
    let mut berry = Array1::<f64>::zeros(chemical_potentials.len());

    let mut accumulate = |lambda: &[f64], weight: f64| {
        let energies = bary_interp_band(&bands, lambda, nsta);
        let kernel = bary_interp_matrix(&kernels, lambda);
        let (metric_n, berry_n) = eval_berry_kernel(&energies, &kernel, eta, nsta);
        for (index, &mu) in chemical_potentials.iter().enumerate() {
            for band in 0..nsta {
                let f = occupation.value_unchecked(energies[band], mu);
                metric[index] += weight * f * metric_n[band];
                berry[index] += weight * f * berry_n[band];
            }
        }
    };

    if dimension == 2 {
        for index in 0..TRI_QUAD_PTS_3.len() {
            accumulate(&TRI_QUAD_PTS_3[index], TRI_QUAD_WTS_3[index]);
        }
    } else {
        for index in 0..TET_QUAD_PTS_4.len() {
            accumulate(&TET_QUAD_PTS_4[index], TET_QUAD_WTS_4[index]);
        }
    }
    (metric * sim.volume, berry * sim.volume)
}

pub(crate) fn quadrature_optical_simplex(
    sim: &TrackedSimplex,
    omega: f64,
    eta: f64,
    mu: f64,
    thermal_width: f64,
) -> Complex<f64> {
    let d = sim.vertices.len() - 1;
    let nsta = sim.vertices[0].band.len();
    let nv = d + 1;
    let bands: Vec<Vec<f64>> = (0..nv).map(|v| sim.vertices[v].band.to_vec()).collect();
    let kmats: Vec<Array2<Complex<f64>>> = (0..nv).map(|v| sim.vertices[v].k_ab.clone()).collect();
    let mut total = Complex::new(0.0, 0.0);
    if d == 2 {
        for iq in 0..3 {
            let lam = TRI_QUAD_PTS_3[iq].as_slice();
            let w = TRI_QUAD_WTS_3[iq];
            let band_q = bary_interp_band(&bands, lam, nsta);
            let k_ab_q = bary_interp_matrix(&kmats, lam);
            total += w * eval_optical_kernel(&band_q, &k_ab_q, omega, eta, mu, thermal_width, nsta);
        }
    } else {
        for iq in 0..4 {
            let lam = TET_QUAD_PTS_4[iq].as_slice();
            let w = TET_QUAD_WTS_4[iq];
            let band_q = bary_interp_band(&bands, lam, nsta);
            let k_ab_q = bary_interp_matrix(&kmats, lam);
            total += w * eval_optical_kernel(&band_q, &k_ab_q, omega, eta, mu, thermal_width, nsta);
        }
    }
    total * sim.volume
}