Rust可视化氢原子的波函数

发布于:2024-08-19 ⋅ 阅读:(138) ⋅ 点赞:(0)

To visualize the hydrogen atom’s wave function in 3D using Rust, you can use the plotters crate to create 2D plots or employ a more advanced 3D plotting library like kiss3d or three-d. Below is a guide using kiss3d to visualize the probability density of the hydrogen atom’s wave function in 3D.

在这里插入图片描述

Step 1: Set Up the Project
First, add the necessary dependencies to your Cargo.toml:

[dependencies]
kiss3d = "0.35.0"

Step 2: Implement the Visualization
Here’s how you can visualize the probability density of the hydrogen atom’s wave function using kiss3d:

extern crate kiss3d;

use kiss3d::nalgebra::{Translation3, Vector3};  // Use nalgebra from kiss3d
use kiss3d::window::Window;
use kiss3d::light::Light;

/// Factorial function
fn factorial(n: usize) -> usize {
    (1..=n).product()
}

/// Compute the associated Laguerre polynomial L(n, l, r)
fn laguerre(n: u32, l: u32, r: f64) -> f64 {
    let mut sum = 0.0;
    for m in 0..=n as usize {
        let coef = factorial(n as usize + l as usize) /
                   (factorial(m) * factorial(n as usize - m) * factorial(l as usize + m));
        sum += (-1.0_f64).powi(m as i32) * coef as f64 * r.powi(m as i32);
    }
    sum
}

/// Radial part of the hydrogen wave function
fn radial_wave_function(n: u32, l: u32, r: f64) -> f64 {
    let a0 = 1.0;  // Bohr radius (in atomic units)
    let rho = 2.0 * r / (n as f64 * a0);
    let normalization = ((2.0 / (n as f64 * a0)).powi(3) * (factorial(n as usize - l as usize)) /
                         (2 * n * factorial(n as usize + l as usize))).sqrt();
    let laguerre_poly = laguerre(n - l - 1, 2 * l + 1, rho);

    normalization * (-rho / 2.0).exp() * rho.powi(l as i32) * laguerre_poly
}

/// Probability density |Ψ(r)|^2
fn probability_density(n: u32, l: u32, r: f64) -> f64 {
    let wave_func = radial_wave_function(n, l, r);
    wave_func.powi(2)
}

fn main() {
    let mut window = Window::new("3D Visualization of Hydrogen Wave Function");

    let n = 1;
    let l = 0;

    // Visualization parameters
    let scale = 10.0;
    let step = 1.0;

    for x in (-100..100).step_by(step as usize) {
        for y in (-100..100).step_by(step as usize) {
            for z in (-100..100).step_by(step as usize) {
                let r = ((x as f64).powi(2) + (y as f64).powi(2) + (z as f64).powi(2)).sqrt() * step;

                let prob_density = probability_density(n, l, r);

                // Map probability density to a scale for visualization
                if prob_density > 0.001 {
                    let mut sphere = window.add_sphere(0.01);
                    sphere.set_color(prob_density as f32 * scale, 0.0, 1.0 - prob_density as f32 * scale);

                    // Convert Vector3 to Translation3
                    let translation = Translation3::from(Vector3::new(
                        x as f32 * step as f32,
                        y as f32 * step as f32,
                        z as f32 * step as f32,
                    ));
                    sphere.append_translation(&translation);
                }
            }
        }
    }

    window.set_light(Light::StickToCamera);

    while window.render() {
        // Render loop
    }
}

Step 3: Run the Program
Compile and run the program using cargo run. This will open a window showing a 3D visualization of the probability density for the hydrogen atom’s wave function.

cargo run

网站公告

今日签到

点亮在社区的每一天
去签到