Rust可视化氢原子的波函数

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
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

0010000100

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值