use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
pub const BOLTZMANN_K: f64 = 1.380649e-23;
pub const EV_TO_JOULES: f64 = 1.602176634e-19;
pub const LANDAUER_LIMIT_300K: f64 = 2.87e-21;
pub const LANDAUER_LIMIT_300K_EV: f64 = 0.0179;
pub fn landauer_limit(temperature_kelvin: f64) -> f64 {
BOLTZMANN_K * temperature_kelvin * std::f64::consts::LN_2
}
#[derive(Debug, Clone, Copy)]
pub enum Operation {
BitErasure { count: u64 },
BitCopy { count: u64 },
VectorSimilarity { dimensions: usize },
MatrixVectorMultiply { rows: usize, cols: usize },
NeuralForward { parameters: u64 },
MemoryRead { bytes: u64 },
MemoryWrite { bytes: u64 },
GraphTraversal { hops: u64 },
Custom { bit_erasures: u64 },
}
impl Operation {
pub fn estimated_bit_erasures(&self) -> u64 {
match self {
Operation::BitErasure { count } => *count,
Operation::BitCopy { count } => *count / 10, Operation::VectorSimilarity { dimensions } => {
(*dimensions as u64) * 32
}
Operation::MatrixVectorMultiply { rows, cols } => {
(*rows as u64) * (*cols as u64) * 2
}
Operation::NeuralForward { parameters } => {
parameters * 2
}
Operation::MemoryRead { bytes } => {
bytes * 8 / 100
}
Operation::MemoryWrite { bytes } => {
bytes * 8 * 2
}
Operation::GraphTraversal { hops } => {
hops * 10
}
Operation::Custom { bit_erasures } => *bit_erasures,
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct EnergyEstimate {
pub landauer_minimum_joules: f64,
pub estimated_actual_joules: f64,
pub efficiency_ratio: f64,
pub bit_erasures: u64,
}
pub struct ThermodynamicTracker {
temperature: f64,
landauer_limit: f64,
total_erasures: Arc<AtomicU64>,
total_operations: Arc<AtomicU64>,
technology_multiplier: f64,
}
impl ThermodynamicTracker {
pub fn new(temperature_kelvin: f64) -> Self {
Self {
temperature: temperature_kelvin,
landauer_limit: landauer_limit(temperature_kelvin),
total_erasures: Arc::new(AtomicU64::new(0)),
total_operations: Arc::new(AtomicU64::new(0)),
technology_multiplier: 1000.0, }
}
pub fn room_temperature() -> Self {
Self::new(300.0)
}
pub fn with_technology_multiplier(mut self, multiplier: f64) -> Self {
self.technology_multiplier = multiplier;
self
}
pub fn record_operation(&self, operation: Operation) {
let erasures = operation.estimated_bit_erasures();
self.total_erasures.fetch_add(erasures, Ordering::Relaxed);
self.total_operations.fetch_add(1, Ordering::Relaxed);
}
pub fn estimate_energy(&self, operation: Operation) -> EnergyEstimate {
let bit_erasures = operation.estimated_bit_erasures();
let landauer_minimum = (bit_erasures as f64) * self.landauer_limit;
let estimated_actual = landauer_minimum * self.technology_multiplier;
EnergyEstimate {
landauer_minimum_joules: landauer_minimum,
estimated_actual_joules: estimated_actual,
efficiency_ratio: self.technology_multiplier,
bit_erasures,
}
}
pub fn total_erasures(&self) -> u64 {
self.total_erasures.load(Ordering::Relaxed)
}
pub fn total_operations(&self) -> u64 {
self.total_operations.load(Ordering::Relaxed)
}
pub fn total_landauer_minimum(&self) -> f64 {
(self.total_erasures() as f64) * self.landauer_limit
}
pub fn total_estimated_energy(&self) -> f64 {
self.total_landauer_minimum() * self.technology_multiplier
}
pub fn efficiency_report(&self) -> EfficiencyReport {
let total_erasures = self.total_erasures();
let landauer_minimum = self.total_landauer_minimum();
let estimated_actual = self.total_estimated_energy();
let reversible_potential = estimated_actual - landauer_minimum;
EfficiencyReport {
temperature_kelvin: self.temperature,
landauer_limit_per_bit: self.landauer_limit,
total_bit_erasures: total_erasures,
total_operations: self.total_operations(),
landauer_minimum_joules: landauer_minimum,
landauer_minimum_ev: landauer_minimum / EV_TO_JOULES,
estimated_actual_joules: estimated_actual,
efficiency_ratio: self.technology_multiplier,
reversible_savings_potential: reversible_potential,
reversible_improvement_factor: self.technology_multiplier,
}
}
pub fn reset(&self) {
self.total_erasures.store(0, Ordering::Relaxed);
self.total_operations.store(0, Ordering::Relaxed);
}
}
impl Default for ThermodynamicTracker {
fn default() -> Self {
Self::room_temperature()
}
}
#[derive(Debug, Clone)]
pub struct EfficiencyReport {
pub temperature_kelvin: f64,
pub landauer_limit_per_bit: f64,
pub total_bit_erasures: u64,
pub total_operations: u64,
pub landauer_minimum_joules: f64,
pub landauer_minimum_ev: f64,
pub estimated_actual_joules: f64,
pub efficiency_ratio: f64,
pub reversible_savings_potential: f64,
pub reversible_improvement_factor: f64,
}
impl std::fmt::Display for EfficiencyReport {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "=== Thermodynamic Efficiency Report ===")?;
writeln!(f, "Temperature: {:.1}K", self.temperature_kelvin)?;
writeln!(
f,
"Landauer limit: {:.2e} J/bit",
self.landauer_limit_per_bit
)?;
writeln!(f)?;
writeln!(f, "Operations tracked: {}", self.total_operations)?;
writeln!(f, "Total bit erasures: {}", self.total_bit_erasures)?;
writeln!(f)?;
writeln!(
f,
"Theoretical minimum: {:.2e} J ({:.2e} eV)",
self.landauer_minimum_joules, self.landauer_minimum_ev
)?;
writeln!(
f,
"Estimated actual: {:.2e} J",
self.estimated_actual_joules
)?;
writeln!(
f,
"Efficiency ratio: {:.0}× above Landauer",
self.efficiency_ratio
)?;
writeln!(f)?;
writeln!(f, "Reversible computing potential:")?;
writeln!(
f,
" - Savings: {:.2e} J ({:.1}%)",
self.reversible_savings_potential,
(self.reversible_savings_potential / self.estimated_actual_joules) * 100.0
)?;
writeln!(
f,
" - Improvement factor: {:.0}×",
self.reversible_improvement_factor
)?;
Ok(())
}
}
pub mod technology_profiles {
pub const CMOS_2024: f64 = 1000.0;
pub const BIOLOGICAL: f64 = 10.0;
pub const NEUROMORPHIC_PROJECTED: f64 = 100.0;
pub const REVERSIBLE_IDEAL: f64 = 1.0;
pub const REVERSIBLE_2028: f64 = 10.0;
pub const SUPERCONDUCTING: f64 = 100.0;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_landauer_limit_room_temp() {
let limit = landauer_limit(300.0);
assert!((limit - 2.87e-21).abs() < 1e-22);
}
#[test]
fn test_tracker_operations() {
let tracker = ThermodynamicTracker::room_temperature();
tracker.record_operation(Operation::BitErasure { count: 1000 });
tracker.record_operation(Operation::VectorSimilarity { dimensions: 384 });
assert_eq!(tracker.total_operations(), 2);
assert!(tracker.total_erasures() > 1000); }
#[test]
fn test_energy_estimate() {
let tracker = ThermodynamicTracker::room_temperature();
let estimate = tracker.estimate_energy(Operation::BitErasure { count: 1 });
assert!((estimate.landauer_minimum_joules - LANDAUER_LIMIT_300K).abs() < 1e-22);
assert_eq!(estimate.efficiency_ratio, 1000.0);
}
#[test]
fn test_efficiency_report() {
let tracker = ThermodynamicTracker::room_temperature().with_technology_multiplier(1000.0);
tracker.record_operation(Operation::BitErasure { count: 1_000_000 });
let report = tracker.efficiency_report();
assert_eq!(report.total_bit_erasures, 1_000_000);
assert_eq!(report.efficiency_ratio, 1000.0);
assert!(report.reversible_savings_potential > 0.0);
}
#[test]
fn test_technology_profiles() {
assert!(technology_profiles::REVERSIBLE_IDEAL < technology_profiles::BIOLOGICAL);
assert!(technology_profiles::BIOLOGICAL < technology_profiles::NEUROMORPHIC_PROJECTED);
assert!(technology_profiles::NEUROMORPHIC_PROJECTED < technology_profiles::CMOS_2024);
}
}