提交 8ff3b364 编写于 作者: M march3

三体运行模拟器

上级 202044b2
...@@ -63,6 +63,9 @@ class Body(metaclass=ABCMeta): ...@@ -63,6 +63,9 @@ class Body(metaclass=ABCMeta):
self.__acceleration = np.array([0, 0, 0], dtype='float32') self.__acceleration = np.array([0, 0, 0], dtype='float32')
self.__record_history() self.__record_history()
# 是否显示
self.appeared = True
@property @property
def has_rings(self): def has_rings(self):
""" """
......
...@@ -80,96 +80,20 @@ def get_position_force(angles, force=1, radius=1, radius_offset=None, force_offs ...@@ -80,96 +80,20 @@ def get_position_force(angles, force=1, radius=1, radius_offset=None, force_offs
# return pxs, pys, fxs, fys # return pxs, pys, fxs, fys
return np.round(pxs, 2), np.round(pys, 2), -np.round(fxs, 2), np.round(fys, 2) return np.round(pxs, 2), np.round(pys, 2), -np.round(fxs, 2), np.round(fys, 2)
#
#
# import math def calculate_distance(pos1, pos2=[0, 0, 0]):
# """
# """ 计算两点间的距离
# 用python写一个函数,根据不同角度的参数列表,返回坐标 pos_x, pos_y,以及分解力fx fy :param pos1:
# :param pos2:
# def get_force(angle_list): :return:
# pos_x = 0 """
# pos_y = 0 d = pow(pow(np.array(pos1[0]) - np.array(pos2[0]), 2) +
# fx = 0 pow(np.array(pos1[1]) - np.array(pos2[1]), 2) +
# fy = 0 pow(np.array(pos1[2]) - np.array(pos2[2]), 2), 1 / 2)
# for angle in angle_list: return d
# force_x, force_y = get_force_from_angle(angle)
# pos_x += force_x
# pos_y += force_y if __name__ == '__main__':
# fx += force_x print(calculate_distance([6, 8, 0], [3, 4, 0]))
# fy += force_y
#
# return pos_x, pos_y, fx, fy
#
# def get_force_from_angle(angle):
# force_x = math.cos(math.radians(angle))
# force_y = math.sin(math.radians(angle))
# return force_x, force_y
# """
#
# """
# ```python
# def get_positions_forces_from_angles(angles,radius, force):
#
# ```
# """
#
#
# def get_position_force(angle_list, radius=1, force=1):
# fxs = []
# fys = []
# angle_list = np.array(angle_list * np.pi)
# pxs = radius * np.cos(angle_list)
# pys = radius * np.sin(angle_list)
#
# for angle in angle_list:
# fx, fy = get_force_from_angle(angle)
# fxs.append(fx * force)
# fys.append(fy * force)
#
# return np.round(pxs, 2), np.round(pys, 2), np.round(fxs, 2), np.round(fys, 2)
#
#
# def get_force_from_angle(angle):
# force_x = math.cos(angle)
# force_y = math.sin(angle)
# # force_x = math.cos(math.radians(angle))
# # force_y = math.sin(math.radians(angle))
# return force_x, force_y
#
#
# if __name__ == '__main__':
# # a = [0, 45, 90, 135, 180, 225, 270, 315, 360]
# # start:返回样本数据开始点
# # stop:返回样本数据结束点
# # num:生成的样本数据量,默认为50
# points = np.linspace(0, 2 * np.pi, 10)
# # points = np.array([0, 45, 90, 135, 180, 225, 270, 315, 360])
# # points = np.array([0. , 0.17453293, 0.34906585, 0.52359878, 0.6981317 ,
# # 0.87266463, 1.04719755, 1.22173048, 1.3962634 , 1.57079633])
# # x, y = circle_x_y(points, 1)
# # print(x, y)
# print(points)
# pos_x, pos_y, fx, fy = circle_x_y_f(points, radius=1.6, force=25.37)
# print(pos_x, pos_y, fx, fy)
#
# """
# asteroids = [
# Asteroid(size_scale=1e9, # 小行星放大 1000000000 倍,距离保持不变
# init_position=[1.6 * AU, 0, 0],
# init_velocity=[0, 25.37, 0],
# distance_scale=1),
# Asteroid(size_scale=1e9, # 小行星放大 1000000000 倍,距离保持不变
# init_position=[-1.6 * AU, 0, 0],
# init_velocity=[0, -25.37, 0],
# distance_scale=1),
# Asteroid(size_scale=1e9, # 小行星放大 1000000000 倍,距离保持不变
# init_position=[0, 1.6 * AU, 0],
# init_velocity=[-25.37, 0, 0],
# distance_scale=1),
# Asteroid(size_scale=1e9, # 小行星放大 1000000000 倍,距离保持不变
# init_position=[0, -1.6 * AU, 0],
# init_velocity=[25.37, 0, 0],
# distance_scale=1),
# ]
# """
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
import numpy as np import numpy as np
from common.consts import AU, G from common.consts import AU, G
from bodies import Body, Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto from bodies import Body, Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto
from common.func import calculate_distance
class System(object): class System(object):
...@@ -16,8 +17,9 @@ class System(object): ...@@ -16,8 +17,9 @@ class System(object):
天体系统 天体系统
""" """
def __init__(self, bodies): def __init__(self, bodies, max_distance=-1):
self.bodies = bodies self.bodies = bodies
self.max_distance = max_distance
def add(self, body): def add(self, body):
self.bodies.append(body) self.bodies.append(body)
...@@ -64,9 +66,36 @@ class System(object): ...@@ -64,9 +66,36 @@ class System(object):
计算加速度 计算加速度
:return: :return:
""" """
def valid_body(body):
"""
有效的天体
:param body:
:return:
"""
if not body.appeared:
return False
if self.max_distance > 0:
if calculate_distance(body.position) > self.max_distance:
body.appeared = False
return False
return True
self.bodies = list(filter(valid_body, self.bodies))
for body1 in self.bodies: for body1 in self.bodies:
acceleration = np.zeros(3) acceleration = np.zeros(3)
for body2 in self.bodies: for body2 in self.bodies:
if self.max_distance > 0:
if calculate_distance(body1.position) > self.max_distance: # 太远了,则小时
body1.appeared = False
if calculate_distance(body2.position) > self.max_distance: # 太远了,则小时
body2.appeared = False
if False == body1.appeared or body2.appeared == False:
continue
if body1 is body2: if body1 is body2:
continue continue
elif body1.ignore_gravity(body2) or body2.ignore_gravity(body1): elif body1.ignore_gravity(body2) or body2.ignore_gravity(body1):
......
...@@ -36,6 +36,8 @@ class BodyView(metaclass=ABCMeta): ...@@ -36,6 +36,8 @@ class BodyView(metaclass=ABCMeta):
self.raduis = None self.raduis = None
self.velocity = None self.velocity = None
self.appeared = True
def __repr__(self): def __repr__(self):
return '<%s> m=%.3e(kg), r=%.3e(km), p=[%.3e,%.3e,%.3e](km), v=%s(km/s)' % \ return '<%s> m=%.3e(kg), r=%.3e(km), p=[%.3e,%.3e,%.3e](km), v=%s(km/s)' % \
(self.name, self.mass, self.raduis, (self.name, self.mass, self.raduis,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册