Skip to content

Commit fd3c193

Browse files
committed
Initial commit
0 parents  commit fd3c193

File tree

165 files changed

+22603
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

165 files changed

+22603
-0
lines changed

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

MANIFEST.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include solar_system/*
2+
3+
include solar_system/TESTs/*
4+
5+
include solar_system/pygame/*
6+
exclude solar_system/pygame/*.docx

README.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
在正式开始之前,先看这两张动图:See the GIFs below before we start.
2+
1.不断减速地球,地球神秘落入太阳中,却自己弹出来?
3+
Slow down the earth continuously. The earth mysteriously falls into the sun, but bounces out?
4+
![](https://img-blog.csdnimg.cn/69ef2a3fef3b4b3198b292d427e51f42.gif#pic_center)
5+
2.不断增加月球到地球的距离,月球受太阳潮汐力影响,脱离地球?
6+
Increase the distance from the moon to the earth continuously. The moon will be separated from the earth due to the influence of solar tidal force?
7+
![](https://img-blog.csdnimg.cn/3b8256294fae45cca841577167394dca.gif#pic_center)
8+
9+
这是一个Python计算机模拟万有引力、太阳系行星运动的软件,
10+
应用了万有引力等相关的物理公式计算,可以模拟出天体的椭圆轨道,
11+
以及验证开普勒三大定律,第一、第二宇宙速度。
12+
支持跟踪行星、切换行星,包含简单的发射“飞船”功能。
13+
图形部分使用tkinter(turtle)或pygame库。
14+
你能在其中创造一个属于自己的宇宙。
15+
此外,这个项目能加以扩展,用来研究其他的物理问题。
16+
17+
This is a software that simulates the gravity and planetary motion in the universe with Python,
18+
using physical formulas including universal gravitation to calculate the orbits of celestial bodies
19+
and verifying Kepler's three laws and the first and second cosmic velocities.
20+
It supports tracking, switching planets and launching simple "spaceships",
21+
with module tkinter (turtle) or pygame for graphics.
22+
You're able to create a universe belonging to yourself in this program.
23+
Additionally, this project can be further expanded to research other physical problems.
24+
**The English introduction is placed below the Chinese version.**
25+
26+
项目源代码 (Project source code):https://github.com/qfcy/python-gravity-simulation
27+
## 一.各个文件的介绍
28+
29+
#### 1.主程序
30+
solar_system.py:主程序的最新版本,也是一个公共的模块
31+
solar_system_v1.0.py:主程序的1.0版本(存档备用)
32+
solar_system_v1.1.1.py:主程序的1.1.1版本(存档备用)
33+
solar_system_v1.2.04.py:主程序的1.2.04版本(存档备用)
34+
empty.pkl和spacecraft.pkl:solar_system.py使用pickle库存储天体数据,这是天体数据的两个备用模板。(empty.pkl为空的宇宙,spacecraft.pkl是仅有一对飞船的宇宙。)
35+
36+
#### 2.程序的多个测试及改编版本
37+
(一部分是用来测试的)
38+
TESTs/solar_system_accelerate.py:使用C语言扩展提升计算的性能(经作者测试,与solar_system.py相比,速度约提升22~45倍),并增加了二阶龙格库塔法
39+
TESTs/solar_system_particles.py:使用更快的“粒子”计算小行星,用于提升计算性能(备用)
40+
TESTs/solar_system_blackhole1.py:行星掉入黑洞的模拟1
41+
TESTs/solar_system_blackhole2.py:行星掉入黑洞的模拟2(包含了turtle模块绘制旋转图片的技巧,见 [这篇文章](https://blog.csdn.net/qfcy_/article/details/120584657)
42+
TESTs/solar_system_blackhole3.py:第3个行星掉入黑洞的模拟
43+
TESTs/solar_system_hill_sphere.py:[希尔球](https://baike.baidu.com/item/%E5%B8%8C%E5%B0%94%E7%90%83)[拉格朗日点](https://baike.baidu.com/item/%E6%8B%89%E6%A0%BC%E6%9C%97%E6%97%A5%E7%82%B9/731078) 现象模拟。希尔球指卫星到行星的距离不能超过最大值,也就是前面的第二张动图
44+
TESTs/solar_system_binary_star.py:双星系统模拟
45+
TESTs/solar_system_collision1.py:太阳系行星形成粗略模拟,应用了行星的碰撞反弹算法 (参见[这篇文章](https://blog.csdn.net/qfcy_/article/details/119711166))
46+
TESTs/solar_system_collision2.py:行星的碰撞反弹算法测试
47+
TESTs/solar_system_molecule.py:分子间作用力的"粗略"模拟
48+
TESTs/solar_system_debug.py:输出调试信息,如程序中单次计算、双精度浮点数导致的精度损失等
49+
50+
教具版:验证开普勒第一、第二、第三定律、第一、二宇宙速度和模拟卫星的变轨,并含有作者编写这个程序的实践报告,和教具的各个早期版本存档。
51+
pygame:使用pygame库的实现(待完善)
52+
dist:使用pyinstaller的打包exe文件,及用Inno Setup制作的安装包。
53+
## 二.程序的原理
54+
55+
#### 1. 算法设计
56+
开始编程前,研究真实的物理问题,首先要对物理问题涉及的研究对象进行抽象与建模。
57+
在真实的宇宙中,天体与其他各个天体之间都存在引力。
58+
为简化研究,程序使用“降维”的思想,将真实的三维宇宙转换为二维的宇宙。
59+
宇宙的本身属性引力常量G使用一个常量表示。每一个行星可以表示为它的质量、速度、x坐标、y坐标的属性。
60+
**天体轨迹的计算**
61+
程序应用微分的方法,将天体的运行分解为许多步微小的直线运动。通过重复计算天体每一步的移动,就能得到天体的运行轨迹。
62+
假设有两个天体A,B,则引力为F = GMm/r^2^ ,天体之间的距离为 d=sqrt( (x~A~-x~B~)^2^+(y~A~-y~B~)^2^),
63+
天体A在x方向上的加速度为a~x~ = F/m = F |x~A~ - x~B~|/md ,在y方向上的加速度为a~y~ = F/m = F |y~A~ - y~B~| /md。
64+
设程序单步经历的时间为t,则应用欧拉法得出,新一轮天体A速度v=v~0~+at,位移x=x~0~+vt。
65+
66+
程序这样重复这一个计算,不断地循环,就能逼真地模拟天体的运动过程。t越小,模拟越精确。
67+
68+
#### 2. 主程序实现
69+
程序首先初始化屏幕、恒星和各个行星,
70+
然后重复一个不断计算和绘制的事件循环。在这个循环中:
71+
1.计算行星受到重力合力的加速度。
72+
2.计算速度和位移。
73+
3.重复1、2步骤若干次。
74+
4.调用turtle模块中的函数,绘制行星到屏幕上。
75+
流程图是这个样子:
76+
![](https://img-blog.csdnimg.cn/478371f05bdf4940b84a6d31625c82b6.png#pic_center)
77+
关于更详细的程序原理,请看“开普勒定律\程序原理(文章)”这个文件夹。
78+
79+
## 三.程序获得的奖项
80+
本项目曾获多个信息技术奖项,证书这里就不展示了。
81+
不过,仓库里面有获奖作品的申报材料,留给需要做信息技术竞赛的同学做参考。
82+
83+
## 四.常见问题
84+
Q: 为什么本项目主要使用了turtle库,而不用其他更高级的图形库?
85+
A: 首先,作者本想用tkinter库编写该程序,但考虑到直接调用Canvas控件绘制行星形状的代码量较大,而且由于基于tkinter的turtle库封装了tkinter,能够间接调用Canvas控件进行绘图,就使用turtle库绘制图形。
86+
目前,程序中的界面主要使用tkinter库设计,而基于tkinter的turtle库仅用于渲染。另外,作者基于自己的tkinter知识,重写和扩展了原版turtle模块,如`TESTS\solar_system_blackhole2.py`基于PIL库为turtle添加了图像旋转功能。
87+
其次,使用turtle库可减少图形渲染的代码,将主要精力用于物理算法的设计,以及其他功能的开发上。
88+
Q: 程序的计算有哪些已知的误差?
89+
A: 程序中的计算精度由两个因素影响:微分的精细度,也就是单次计算经过的时间间隔(dt),以及双精度浮点数精度误差。具体参见`TESTS\solar_system_debug.py`中的介绍。
90+
91+
## 五.关于作者
92+
2021年开始编写该程序时,作者尚是一位高中生。
93+
作者CSDN主页:[qfcy_](https://blog.csdn.net/qfcy_)
94+
bilibili主页:[qfcy_](https://space.bilibili.com/454233262)
95+
96+
英文版介绍(使用了翻译软件+自己修改、润色):
97+
98+
## 1.The introduction to each file
99+
100+
#### (1) Main program
101+
102+
solar_system.py: The latest version of the main program that also serves as a utility module
103+
solar_system_v1.0.py: Version 1.0 of the main program (archive)
104+
solar_system_v1.1.1.py: Version 1.1.1 of the main program (archive)
105+
solar_system_v1.2.04.py: Version 1.2.04 of the main program (archive)
106+
empty.pkl and spacecraft.pkl: As file "solar_system.py" uses module pickle to store datas, these are the examples for the storage of celestial data. (empty.pkl is an empty universe, and spacecraft.pkl is a universe with only a pair of spacecraft.)
107+
108+
#### (2) Some experimental, demo or adapted versions
109+
110+
(Part of it is for tests or just for fun)
111+
TESTs/solar_system_accelerate.py: Using C extension to improve the performance of calculations (according to the author's test, the speed is about 22 to 45 times faster compared with solar_system.py), and adding the second-order Runge-Kutta method.
112+
TESTs/solar_system_particles.py: Using faster "particles" to calculate the movement of asteroids to improve the performance.(standby)
113+
TESTs/solar_system_blackhole1.py: The first version of simulations of planets falling into black holes.
114+
TESTs/solar_system_blackhole2.py: The second version of simulations of planets falling into black holes. (including the technique of drawing rotating pictures with turtle module, see [this article](https://blog.csdn.net/qfcy_/article/details/120584657))
115+
TESTs/solar_system_blackhole3.py: The third version of simulations of planets falling into black holes.
116+
TESTs/solar_system_hill_sphere.py: [Hill Sphere](https://en.wikipedia.org/wiki/Hill_sphere) and [Lagrange Point](https://en.wikipedia.org/wiki/Lagrange_point) simulation. Hill Sphere refers to that the distance from a satellite to a planet cannot exceed the maximum value (same as the second GIF at the beginning).
117+
TESTs/solar_system_binary_star.py: Binary star system simulation
118+
TESTs/solar_system_collision1.py: A rough simulation of the formation of planets in the solar system, using the collision-rebound algorithm (see [this article](https://blog.csdn.net/qfcy_/article/details/119711166))
119+
TESTs/solar_system_collision2.py: A test of collision-rebound algorithm
120+
TESTs/solar_system_molecule.py: A "rough" simulation of intermolecular forces
121+
TESTs/solar_system_debug.py: Debug information outputing, such as the precision loss in a single calculation or caused by double-precision floating-point numbers
122+
123+
ENG: The **English** localized version, also containing the simulation of verifying Kepler's three laws, first and second cosmic velocities and satellite orbit adjustment.
124+
pygame: An implementation using pygame library (TODO: to be improved).
125+
ENG\\dist: Packaged executable files with pyinstaller and the setup program with Inno Setup.
126+
127+
##2.The principle of the program
128+
129+
#### (1) Algorithm design
130+
131+
Before starting programming, you need to abstract and model the research objects involved in physical problems before studying real physical problems.
132+
In the real universe, there is gravity between celestial bodies and other celestial bodies.
133+
To simplify the research, the program uses the idea of "dimension reduction" to convert the real three-dimensional universe into a two-dimensional universe.
134+
The gravitational constant G of the universe is expressed as a constant. Each planet can be described as its mass, speed, x coordinate, y coordinate attributes.
135+
136+
**The calculation of trajetories**
137+
138+
The program uses the method of differentiation to divide the motions of celestial bodies into many steps of tiny linear motion. By repeating the calculation of each step of the celestial body's movement, the trajectory of the celestial body can be obtained.
139+
Suppose there are two celestial bodies A and B, then the gravity is F = GMm/r^2^, and the distance between celestial bodies is d=sqrt( (x~A~-x~B~)^2^+(y~A~-y~B~)^2^).
140+
The acceleration of celestial body A in the x direction is a~x~ = F/m = F |x~A~ - x~B~|/md. As for the y direction, the acceleration is a~y~ = F/m = F |y~A~ - y~B~|/md.
141+
If the time of single step of the program is t, using Eulerian method, the new velocity of celestial body A is v=v~0~+at and the displacement is x=x~0~+vt.
142+
In this way, the program repeats this calculation and keeps looping, so that it can realistically simulate the motion process of celestial bodies. The t smaller, the more accurate the simulation.
143+
144+
#### (2) The implementation of main program
145+
146+
The program firstly initializes the screen, stars and planets,
147+
Then repeat an event cycle that is continuously calculated and drawn. In this cycle:
148+
1. Calculate the acceleration of the planet under the combined force of gravity.
149+
2. Calculate the speed and displacement.
150+
3. Repeat steps 1 and 2 several times.
151+
4. Call the function in the title module to draw the planet on the screen.
152+
The brief flow chart is as follows:
153+
![](https://img-blog.csdnimg.cn/2816259f85374130ac35060d08df3af2.png#pic_center)
154+
155+
For more detailed principles of this program in Chinese, see the folder "开普勒定律\程序原理(文章)".
156+
157+
## 3.Awards won by the program
158+
159+
This project has won many awards in IT competitions in China, but I won't show the certificates.
160+
However, there are application materials for award-winning works in the repository for students who need to participate in other IT competitions.
161+
162+
## 4.Q&A
163+
164+
Q: Why does this project primarily use the turtle library instead of other more advanced graphics libraries?
165+
A: Initially, I intended to use the tkinter library to write the program, but considering the substantial amount of code required to directly manipulate the Canvas widget for drawing planetary shapes, while the turtle module based on tkinter encapsulates many tkinter functions and can indirectly invoke the Canvas widget for drawing, the turtle library was chosen for rendering graphics. Currently, the program's interface is mainly designed using the tkinter library, while the turtle library, which is based on tkinter, is only used for rendering. Additionally, I has rewritten and extended the original turtle module based on their knowledge of tkinter, such as adding image rotation functionality to turtle using the PIL library in `TESTS\solar_system_blackhole2.py`. Furthermore, using the turtle library reduces the amount of code needed for graphic rendering, allowing the main focus to be on designing the physics algorithms and developing other features.
166+
Q: What are the known factors contributing to the precision loss in calculations?
167+
A: The accuracy of the computations in the program is influenced by two factors: the granularity of the differentiation, i.e., the time interval (dt) per computation, and the precision errors of double-precision floating-point numbers. For more details, see the description in `TESTS\solar_system_debug.py`(in Chinese).
168+
169+
## 5.About the author
170+
171+
When the development of the program started in 2021, the author was still a high school student.
172+
GitHub home page: [qfcy_](https://github.com/qfcy)
173+
CSDN home page: [qfcy_](https://blog.csdn.net/qfcy_)
174+
Bilibili home page: [qfcy_](https://space.bilibili.com/454233262)

0 commit comments

Comments
 (0)