system.py 2.5 KB
Newer Older
M
march3 已提交
1 2 3 4 5 6 7 8 9
# -*- coding:utf-8 -*-
# title           :
# description     :
# author          :Python超人
# date            :2023-01-22
# notes           :
# python_version  :3.8
# ==============================================================================
import numpy as np
M
march3 已提交
10
from common.consts import AU, G
M
march3 已提交
11
from bodies import Body, Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto
M
march3 已提交
12 13 14


class System(object):
M
march3 已提交
15 16 17 18
    """
    天体系统
    """

M
march3 已提交
19 20 21 22 23 24 25 26
    def __init__(self, bodies):
        self.bodies = bodies

    def add(self, body):
        self.bodies.append(body)

    def total_mass(self):
        """
M
march3 已提交
27
        总质量
M
march3 已提交
28 29
        :return:
        """
M
march3 已提交
30
        total_mass = 0.0
M
march3 已提交
31
        for body in self.bodies:
M
march3 已提交
32 33
            total_mass += body.mass
        return total_mass
M
march3 已提交
34

M
march3 已提交
35 36
    def __repr__(self):
        return 'System({})'.format(self.bodies)
M
march3 已提交
37

M
march3 已提交
38
    def center_of_mass(self):
M
march3 已提交
39
        """
M
march3 已提交
40
        质心
M
march3 已提交
41 42
        :return:
        """
M
march3 已提交
43
        r = np.zeros(2)
M
march3 已提交
44
        for body in self.bodies:
M
march3 已提交
45 46
            r = body.mass * body.position
        return r / self.total_mass()
M
march3 已提交
47

M
march3 已提交
48
    def evolve(self, dt):
M
march3 已提交
49 50
        """

M
march3 已提交
51
        :param dt:
M
march3 已提交
52 53
        :return:
        """
M
march3 已提交
54
        self.calc_bodies_acceleration()
M
march3 已提交
55 56

        for body in self.bodies:
M
march3 已提交
57 58 59 60
            # acceleration 加速度
            body.velocity += body.acceleration * dt
            # body.position += 0.5 * body.acceleration * (dt ** 2)
            body.position += body.velocity * dt
M
march3 已提交
61

M
march3 已提交
62
    def calc_bodies_acceleration(self):
M
march3 已提交
63
        """
M
march3 已提交
64 65
        计算加速度
        :return:
M
march3 已提交
66
        """
M
march3 已提交
67
        for body1 in self.bodies:
M
march3 已提交
68
            acceleration = np.zeros(3)
M
march3 已提交
69 70 71 72 73
            for body2 in self.bodies:
                if body1 is body2:
                    continue

                r = body2.position - body1.position
M
march3 已提交
74 75 76 77
                # G = 6.67e-11 # 万有引力常数
                # m/s² = kg * m / m**3
                # km/s² = kg * m / m**3 / 1e9
                # acceleration = G * body2.mass * dx / (d ** 3)
M
march3 已提交
78
                acceleration += (G * body2.mass * r / np.linalg.norm(r) ** 3) / 1e9
M
march3 已提交
79

M
march3 已提交
80
            body1.acceleration = acceleration
M
march3 已提交
81 82


M
march3 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
if __name__ == '__main__':
    body_sys = System([
        Sun(),  # 太阳
        Mercury(),  # 水星
        Venus(),  # 金星
        Earth(),  # 地球
        Mars(),  # 火星
        Jupiter(),  # 木星
        Saturn(),  # 土星
        Uranus(),  # 天王星
        Neptune(),  # 海王星
        Pluto()  # 冥王星(从太阳系的行星中排除)
    ])

    print(body_sys)