use crate::error::{BarracudaError, Result};
#[must_use]
pub fn localization_length(disorder_strength: f64, energy: f64) -> f64 {
let w_sq = disorder_strength.mul_add(disorder_strength, 0.01);
let band_factor = energy.mul_add(-energy, 4.0).max(0.01);
105.0 * band_factor / w_sq
}
pub fn anderson_conductance(
disorder_strength: f64,
system_size: usize,
energy: f64,
) -> Result<f64> {
if disorder_strength < 0.0 {
return Err(BarracudaError::InvalidInput {
message: format!("disorder_strength must be non-negative, got {disorder_strength}"),
});
}
let xi = localization_length(disorder_strength, energy);
let l = system_size as f64;
if xi <= 0.0 {
return Ok(0.0);
}
let transmission = (-l / xi).exp();
Ok(transmission.clamp(0.0, 1.0))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn zero_disorder_conductance_ballistic() {
let g = anderson_conductance(0.0, 100, 0.0).unwrap();
assert!(
g > 0.99,
"Zero disorder should give ballistic conductance ~1.0, got {g}"
);
}
#[test]
fn strong_disorder_conductance_localized() {
let g = anderson_conductance(20.0, 1000, 0.0).unwrap();
assert!(
g < 0.01,
"Strong disorder should give conductance → 0, got {g}"
);
}
#[test]
fn localization_length_decreases_with_disorder() {
let xi_small = localization_length(1.0, 0.0);
let xi_large = localization_length(10.0, 0.0);
assert!(
xi_large < xi_small,
"Localization length should decrease with disorder: ξ(W=10)={xi_large} < ξ(W=1)={xi_small}"
);
}
#[test]
fn localization_length_positive() {
assert!(localization_length(0.0, 0.0) > 0.0);
assert!(localization_length(5.0, 1.0) > 0.0);
}
#[test]
fn anderson_conductance_invalid_disorder() {
let r = anderson_conductance(-1.0, 100, 0.0);
assert!(r.is_err());
}
}