2026-summer-project-08-n body visualization-lihaokun-2026 - #63
Open
lihaokun-2026 wants to merge 11 commits into
Open
lihaokun-2026 wants to merge 11 commits into
lihaokun-2026 wants to merge 11 commits into
Conversation
Signed-off-by: lihaokun <851563813@qq.com>
Signed-off-by: lihaokun <851563813@qq.com>
Signed-off-by: lihaokun <851563813@qq.com>
Signed-off-by: lihaokun <851563813@qq.com>
Signed-off-by: lihaokun <851563813@qq.com>
Signed-off-by: lihaokun <851563813@qq.com>
Signed-off-by: lihaokun <851563813@qq.com>
Signed-off-by: lihaokun <851563813@qq.com>
Signed-off-by: lihaokun-2026 <851563813@qq.com>
Signed-off-by: lihaokun-2026 <851563813@qq.com>
Signed-off-by: lihaokun-2026 <851563813@qq.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
N 体引力模拟与可视化
简介
你是“巡天”深空探测项目的核心算法工程师,负责模拟小行星带、卫星碎片和探测器之间的引力相互作用,以辅助航迹规划和碰撞风险评估。地面控制中心每天需要评估大量不同初始条件下的演化结果,传统 CPU 程序在粒子数量上升后很快无法满足交互式分析需求。
你的任务是开发一个 GPU 加速的 N 体引力模拟器,能够并行计算数千到数十万个天体之间的相互作用,并输出轨迹数据供 Python 可视化。最终,你需要提供一个可以展示星团演化、轨道扰动和局部碰撞风险的动画脚本,帮助工程团队直观理解模拟结果。
任务内容
开发 CUDA 程序模拟 N 体引力系统,并输出轨迹点供 Python 可视化。
N 体引力模拟器(CUDA)
实现与文件
nbody.cu:共享内存分块的直接 O(N²) 引力计算,支持半隐式 Euler 和 Leapfrog。visualize.py:NumPy 内存映射读取 BIN,提供统一 CLI,所有模式使用 MatplotlibFuncAnimation。raster_animation.py:CUDA/NumPy 投影和密度图生成,Matplotlib 绘制图像及固定粒子 ID 的连续尾迹。data/generate_cases.py:原有双体、星团、太阳系、银河盘尺度 benchmark 的确定性生成器。data/generate_orbit_demo.py:独立归一化轨道演示,不改写现有 benchmark。test_visualization.py:读取、帧选择、渲染计算、CUDA 对照与动画导出回归。编译、模拟与动画
以下在 Linux 的
08_nbody目录执行,使用的是4090的GPU,编译参数使用-arch=sm_89环境配置
# 首先安装pip依赖包 pip install -r requirements# 编译cuda文件 (4090硬件) nvcc -O3 -std=c++17 -lineinfo -arch=sm_89 nbody.cu -o nbody验证4096粒子
验证65536粒子
单粒子可视化
由于生成的
4096粒子和65536粒子在生成的mp4动画中难以观察到粒子的细致变换,因此,开发了trajectory_viewer.html这个可视化的页面,通过导入4096粒子和65536粒子的二进制的bin文件,即可逐个粒子的观察运动轨迹。可视化页面
如图所示:
导入粒子的二进制.bin文件,可以具体查看某个粒子的运动轨迹,每帧的位置,以及从xy,xz以及全景等角度观察某个具体粒子的运动轨迹。
演示视频:
4096粒子-单粒子轨迹
Learning_cuda\08_nbody\lihaokun-2026\results\verified_4096\4096粒子-单粒子轨迹.mp4整体演示视频
Learning_cuda\08_nbody\lihaokun-2026\results\verified_4096\benchmark_4096_detail.mp465536粒子-单粒子轨迹
Learning_cuda\08_nbody\lihaokun-2026\results\verified_65536\65536粒子-单粒子轨迹.mp4整体演示视频
Learning_cuda\08_nbody\lihaokun-2026\results\verified_65536\benchmark_65536_detail.mp4导入轨迹bin文件后,可以选择某个编号儿的粒子,例如这里选择了976这个编号的粒子,可以放大选中路径,然后点击播放,观看粒子的运动轨迹。
显卡适配
由于本项目主要利用cuda并行编程对粒子进行模型,因此需要进行平台适配的是
nbody.cu文件nbody.maca文件中完成对沐曦平台的粒子模拟代码适配。