qualia_core_db/specialized_libs/physics_simulation/
quantum.rs1use super::*;
2
3impl PhysicsSimulationLibrary {
4 pub fn run_quantum_stationary_states_1d(
14 &self,
15 potential: Vec<f64>,
16 dx: f64,
17 mass: f64,
18 hbar: f64,
19 num_levels: usize,
20 ) -> Result<QuantumSpectrumResult, PhysicsError> {
21 let n = potential.len();
22 if n < 2 || !(dx > 0.0 && mass > 0.0 && hbar > 0.0) {
23 return Err(PhysicsError::InvalidConfiguration(
24 "require potential length >= 2, dx > 0, mass > 0, hbar > 0".to_string(),
25 ));
26 }
27 let t = hbar * hbar / (2.0 * mass * dx * dx);
29 let mut a = vec![0.0f64; n * n];
30 for i in 0..n {
31 a[i * n + i] = 2.0 * t + potential[i];
32 if i + 1 < n {
33 a[i * n + (i + 1)] = -t;
34 a[(i + 1) * n + i] = -t;
35 }
36 }
37 let mut eigvecs = vec![0.0f64; n * n];
38 symmetric_eigen(n, &mut a, &mut eigvecs)
39 .map_err(|e| PhysicsError::SolverError(format!("symmetric_eigen: {:?}", e)))?;
40 let mut eigenvalues: Vec<f64> = (0..n).map(|i| a[i * n + i]).collect();
41 eigenvalues.sort_by(|x, y| x.partial_cmp(y).unwrap_or(std::cmp::Ordering::Equal));
42 eigenvalues.truncate(num_levels.min(n).max(1));
43 Ok(QuantumSpectrumResult {
44 eigenvalues,
45 num_grid_points: n,
46 dx,
47 })
48 }
49}