Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Python_超人
宇宙模拟器
提交
f206c8c5
宇宙模拟器
项目概览
Python_超人
/
宇宙模拟器
通知
19
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
宇宙模拟器
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
f206c8c5
编写于
7月 14, 2023
作者:
三月三net
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Python超人-宇宙模拟器
上级
efc1c5f5
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
39 addition
and
6 deletion
+39
-6
common/system.py
common/system.py
+37
-4
sim_scenes/science/jupiter_moon_protects_earth.py
sim_scenes/science/jupiter_moon_protects_earth.py
+2
-2
未找到文件。
common/system.py
浏览文件 @
f206c8c5
...
@@ -163,7 +163,7 @@ class System(object):
...
@@ -163,7 +163,7 @@ class System(object):
body1
.
acceleration
=
acceleration
body1
.
acceleration
=
acceleration
return
True
return
True
def
calculate_gravitational_accelerations
(
self
,
masses
,
positions
):
def
calculate_gravitational_accelerations
_np
(
self
,
masses
,
positions
):
'''Params:
'''Params:
- positions: numpy array of size (n,3)
- positions: numpy array of size (n,3)
- masses: numpy array of size (n,)
- masses: numpy array of size (n,)
...
@@ -177,9 +177,42 @@ class System(object):
...
@@ -177,9 +177,42 @@ class System(object):
forces
=
G
*
disps
*
mass_matrix
/
np
.
expand_dims
(
dists
,
2
)
**
3
forces
=
G
*
disps
*
mass_matrix
/
np
.
expand_dims
(
dists
,
2
)
**
3
return
forces
.
sum
(
axis
=
1
)
/
masses
.
reshape
(
-
1
,
1
)
return
forces
.
sum
(
axis
=
1
)
/
masses
.
reshape
(
-
1
,
1
)
def
calculate_gravitational_accelerations_cp
(
self
,
masses
,
positions
):
'''Params:
- positions: numpy array of size (n,3)
- masses: numpy array of size (n,)
pip install -i https://pypi.douban.com/simple/ cupy
nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Fri_Dec_17_18:28:54_Pacific_Standard_Time_2021
Cuda compilation tools, release 11.6, V11.6.55
Build cuda_11.6.r11.6/compiler.30794723_0
pip install -i https://pypi.douban.com/simple/ cupy-cuda116
cupy-11.6.0.tar.gz
https://pypi.doubanio.com/packages/70/e1/acc77e327548cce7cb28eb345b7f31ab85b6a3d99214479f9bcbe78e6e9b/cupy_cuda116-10.6.0-cp39-cp39-win_amd64.whl
https://pypi.doubanio.com/packages/e3/62/c808623b8000185efebd8b4542efdf76cc93d20dfd3f0a3eaeb5e5697430/cupy-11.6.0.tar.gz#sha256=53dbb840072bb32d4bfbaa6bfa072365a30c98b1fcd1f43e48969071ad98f1a7
'''
import
cupy
as
cp
masses
=
cp
.
array
(
masses
)
positions
=
cp
.
array
(
positions
)
mass_matrix
=
masses
.
reshape
((
1
,
-
1
,
1
))
*
masses
.
reshape
((
-
1
,
1
,
1
))
disps
=
positions
.
reshape
((
1
,
-
1
,
3
))
-
positions
.
reshape
((
-
1
,
1
,
3
))
# displacements
dists
=
cp
.
linalg
.
norm
(
disps
,
axis
=
2
)
dists
[
dists
==
0
]
=
1
# Avoid divide by zero warnings
forces
=
G
*
disps
*
mass_matrix
/
cp
.
expand_dims
(
dists
,
2
)
**
3
accelerations
=
forces
.
sum
(
axis
=
1
)
/
masses
.
reshape
(
-
1
,
1
)
return
accelerations
.
get
()
def
calculate_gravitational_accelerations
(
self
,
masses
,
positions
):
return
self
.
calculate_gravitational_accelerations_np
(
masses
,
positions
)
def
calc_bodies_acceleration_bak
(
self
):
def
calc_bodies_acceleration_bak
(
self
):
"""
"""
计算加速度(使用矩阵的方式,性能提高很多)
计算加速度(使用矩阵的方式,性能提高很多
,不支持指定重力对某天体有效
)
@return:
@return:
"""
"""
valid_bodies
=
list
(
filter
(
lambda
b
:
not
b
.
ignore_mass
,
self
.
bodies
))
valid_bodies
=
list
(
filter
(
lambda
b
:
not
b
.
ignore_mass
,
self
.
bodies
))
...
@@ -192,11 +225,11 @@ class System(object):
...
@@ -192,11 +225,11 @@ class System(object):
accelerations
=
self
.
calculate_gravitational_accelerations
(
masses
,
positions
)
accelerations
=
self
.
calculate_gravitational_accelerations
(
masses
,
positions
)
for
idx
,
body
in
enumerate
(
valid_bodies
):
for
idx
,
body
in
enumerate
(
valid_bodies
):
body
.
acceleration
=
accelerations
[
idx
]
/
1000
body
.
acceleration
=
accelerations
[
idx
]
/
1000
def
calc_bodies_acceleration
(
self
):
def
calc_bodies_acceleration
(
self
):
"""
"""
计算加速度(性能非常低
,代码保留
)
计算加速度(性能非常低)
@return:
@return:
"""
"""
# 如果快速计算成功,则无需再计算
# 如果快速计算成功,则无需再计算
...
...
sim_scenes/science/jupiter_moon_protects_earth.py
浏览文件 @
f206c8c5
...
@@ -19,7 +19,7 @@ import math
...
@@ -19,7 +19,7 @@ import math
import
numpy
as
np
import
numpy
as
np
class
JupiterProtectsEarthSim
:
class
Jupiter
Moon
ProtectsEarthSim
:
def
__init__
(
self
,
comet_num
=
20
):
def
__init__
(
self
,
comet_num
=
20
):
"""
"""
...
@@ -204,7 +204,7 @@ if __name__ == '__main__':
...
@@ -204,7 +204,7 @@ if __name__ == '__main__':
# 设置计时器的最小时间单位为年
# 设置计时器的最小时间单位为年
BodyTimer
().
min_unit
=
BodyTimer
.
MIN_UNIT_YEARS
BodyTimer
().
min_unit
=
BodyTimer
.
MIN_UNIT_YEARS
sim
=
Jupiter
ProtectsEarthSim
(
comet_num
=
2
0
)
sim
=
Jupiter
MoonProtectsEarthSim
(
comet_num
=
3
0
)
# 运行前会触发 on_ready
# 运行前会触发 on_ready
UrsinaEvent
.
on_ready_subscription
(
sim
.
on_ready
)
UrsinaEvent
.
on_ready_subscription
(
sim
.
on_ready
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录