9 个版本

0.4.5 2023 年 10 月 30 日
0.4.4 2023 年 10 月 29 日
0.1.2 2023 年 3 月 27 日

#71模拟

Download history 30/week @ 2024-07-01 29/week @ 2024-07-22

每月 59 次下载

MIT 许可证

3MB
3K SLoC

Rust 2.5K SLoC // 0.0% comments WebGPU Shader Language 479 SLoC // 0.0% comments

✨ Heavens

使用 GPU 运行 N-Body 模拟

📦 依赖

🚀 快速入门

克隆仓库并将工作目录设置为项目根目录

git clone https://github.com/FreddyWordingham/heavens.git
cd heavens

以发布模式构建项目

cargo build --release

运行程序

cargo run --release

🎮 控制

字母 描述
- 减半时间步长
= 加倍时间步长
Q 减少缩放
E 增加缩放
Z 减半模糊半径
X 加倍模糊半径
F 减少重力强度
G 增加重力强度
A 逆时针旋转相机
D 顺时针旋转相机
W 使模拟面向相机
S 使模拟远离相机
O 减半幽灵堆栈可见性限制
P 加倍幽灵堆栈可见性限制
空格 暂停/恢复时间

📝 使用方法

将来我将创建一个运行时 TOML 配置方案。

目前,您可以将 heavens 作为库来设计自己的 N-Body 模拟

  1. 您需要这些导入
use heavens::{run, Camera, NBody, Settings};
  1. 初始化您的设置
fn init_settings() -> Settings {
    Settings {
        display_width: 1300.0,          // [pixels]
        display_height: 1300.0,         // [pixels]
        pixel_size: 1.0,                // [screen pixel per simulation pixel]
        gravitational_constant: 1.0,    // [m^3 kg^-1 s^-2]
        time_step: 1.0e1,               // [s]
        smoothing_length: 1.0,          // [m]
        ghost_mass: 1.0,                // [kg]
        ghost_stack_visible_limit: 4.0, // This many ghosts on top of each other will have an alpha of 1.0
        blur_radius: 5.0,               // [pixels]
    }
}
  1. 初始化您的相机
fn init_camera() -> Camera {
    let eye_pos = [1.0e3, 0.0, 1.0e3];          // [m]
    let tar_pos = [0.0, 0.0, 0.0];              // [m]
    let field_of_view = 90.0_f32.to_radians();  // [radians]
    let zoom = 1000.0;                          // [m]

    Camera::new(eye_pos, tar_pos, field_of_view, zoom)
}
  1. 现在有趣的部分,初始化您的模拟的初始条件
fn init_conditions(grav_const: f32) -> NBody {
    let mut rng = rand::thread_rng();

    let mut init_conditions = NBody::new(); // Construct an empty NBody simulation

    init_conditions.add_massive_system(
        &mut rng,
        grav_const,      // gravitational constant  [m^3 kg^-1 s^-2]
        [0.0, 0.0, 0.0], // centre                  [m]
        [0.0, 0.0, 0.0], // drift                   [m/s]
        1.0e3,           // radius                  [m]
        1.0e1,           // centre mass             [kg]
        1.0e-1,          // disc mass               [kg]
        (64 * 64) - 1,   // num particles
    );
    init_conditions.add_ghost_field(
        &mut rng,
        [0.0, 0.0, 0.0], // centre                  [m]
        [0.0, 0.0, 0.0], // drift                   [m/s]
        1.0e3,           // radius                  [m]
        1.0e1,           // central mass           [kg]
        655 * 64 * 4,    // num particles
        5.0,             // kind (used to colour particles)
    );

    // More bodies here...

    init_conditions
}
  1. 编写主函数
fn main() {
    env_logger::init();
    pollster::block_on(start());
}

async fn start() {
    println!("Initialising settings...");
    let settings = init_settings();
    println!("Initialising camera...");
    let camera = init_camera();
    println!("Generating initial conditions...");
    let init_conditions = init_conditions(settings.gravitational_constant);
    println!("Initial conditions generated.\nRunning simulation...");
    run(settings, camera, init_conditions).await;
}

查看示例 main.rs 以获取更完整的示例。

📚 文档

https://docs.rs/heavens/ 查找文档

🌌 TODO

  • 初始条件辅助类
  • 幽灵粒子
  • 彩色粒子
  • 相机控制
  • 编写 docstrings
  • 运行时参数化
  • 无窗口(捕获)模式

依赖项

~11–44MB
~696K SLoC