simulator.py 4.1 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
# ==============================================================================
from common.system import System
M
march3 已提交
10
from bodies import Body, Sun, Earth, Jupiter
M
march3 已提交
11 12 13 14 15 16 17 18 19

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d

X_MIN, X_MAX = -2e+14, 2e+14  # the x range of the bounding box (m)
Y_MIN, Y_MAX = -2e+14, 2e+14  # the y range of the bounding box (m)
Z_MIN, Z_MAX = -2e+14, 2e+14  # the z range of the bounding box (m)

M
march3 已提交
20 21 22 23 24 25 26 27
X_MIN, X_MAX = -2e+12, 2e+12  # the x range of the bounding box (m)
Y_MIN, Y_MAX = -2e+12, 2e+12  # the y range of the bounding box (m)
Z_MIN, Z_MAX = -2e+12, 2e+12  # the z range of the bounding box (m)

X_MIN, X_MAX = -1e+9, 1e+9  # the x range of the bounding box (m)
Y_MIN, Y_MAX = -1e+9, 1e+9  # the y range of the bounding box (m)
Z_MIN, Z_MAX = -1e+9, 1e+9  # the z range of the bounding box (m)

M
march3 已提交
28

M
march3 已提交
29 30 31 32
# X_MIN, X_MAX = -8e+8, 8e+8  # the x range of the bounding box (m)
# Y_MIN, Y_MAX = -8e+8, 8e+8  # the y range of the bounding box (m)
# Z_MIN, Z_MAX = -8e+8, 8e+8  # the z range of the bounding box (m)

M
march3 已提交
33 34

def show(bodies, idx=0):
M
march3 已提交
35 36
    # from scipy.interpolate import make_interp_spline

M
march3 已提交
37 38 39 40 41 42
    # Creating figures for the plot
    fig = plt.figure(figsize=(16, 12))
    ax = plt.axes(projection="3d")
    ax.set_xlim(X_MIN, X_MAX)
    ax.set_ylim(Y_MIN, Y_MAX)
    ax.set_zlim(Z_MIN, Z_MAX)
M
march3 已提交
43 44 45
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
M
march3 已提交
46
    # Creating a plot using the random datasets
M
march3 已提交
47 48
    colors = ['red', 'blue', 'red', 'red']
    sizes = [800, 500, 800, 800]
M
march3 已提交
49
    for idx, body in enumerate(bodies):
M
march3 已提交
50 51
        color = 'red' if str(body.name).lower().startswith("sun") else 'blue'
        size = 800 if str(body.name).lower().startswith("sun") else 500
M
march3 已提交
52
        pos = body.position
M
march3 已提交
53 54 55 56 57
        ax.text(pos[0], pos[1], pos[2] + 1e8, body.name, color=color, fontsize=20)
        ax.scatter(pos[0], pos[1], pos[2], color=color, s=size)
        # for _his_pos in body.his_position():
        #     ax.scatter3D(_his_pos[0], _his_pos[1], _his_pos[2], color=color, alpha=0.5)
        # ax.scatter(his_pos[0], his_pos[1], his_pos[2], color=colors[idx], s=10)
M
march3 已提交
58
        his_pos = body.his_position()
M
march3 已提交
59
        if len(his_pos) > 1:
M
march3 已提交
60
            _his_pos = list(zip(*his_pos))
M
march3 已提交
61
            ax.plot3D(_his_pos[0], _his_pos[1], _his_pos[2], color=color, alpha=0.8)
M
march3 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75
    plt.title("3D scatter plot %s" % idx)
    # display the  plot
    plt.show()


import time


class Simulator:
    def __init__(self, system, dt):
        self.system = system
        self.dt = dt

    def evolve(self, dt):
M
march3 已提交
76
        show(self.system.bodies)
M
march3 已提交
77
        self.system.calc_acceleration()
M
march3 已提交
78 79 80 81

        for body in self.system.bodies:
            # acceleration 加速度
            body.velocity += body.acceleration * dt
M
march3 已提交
82 83
            # body.position += 0.5 * body.acceleration * (dt ** 2)
            body.position += 0.5 * body.velocity * dt
M
march3 已提交
84 85
            print(body)
            # print(body.name, body.position)
M
march3 已提交
86 87 88 89 90

    def run(self, t):
        n = int(t / self.dt)
        for i in range(n):
            self.evolve(self.dt)
M
march3 已提交
91
            time.sleep(1)
M
march3 已提交
92 93 94 95

    def run_always(self):
        dt = 1  # 时间变化1秒
        while True:
M
march3 已提交
96
            # time.sleep(1)
M
march3 已提交
97 98 99 100
            self.evolve(dt)


if __name__ == '__main__':
M
march3 已提交
101 102 103 104 105 106 107 108 109
    t = 60 * 60 * 24 * 100

    # _sys = System([Sun(init_position=[0, 0, 149597870.700]), Earth()])

    # _sys = System([Sun(init_position=[849597870.700, 0, 0], init_velocity=[0, 9.79, 0]),
    #                Sun(init_position=[0, 0, 0], init_velocity=[0, -9.79, 0])])

    # _sys = System([Sun(), Earth()])

M
march3 已提交
110 111 112 113 114 115
    # _sys = System([Sun(name="sun1", init_position=[849597870.700, 0, 0], init_velocity=[0, 7.0, 0]),
    #                Sun(name="sun2", init_position=[0, 0, 0], init_velocity=[0, -8.0, 0]),
    #                Sun(name="sun3", init_position=[0, -849597870.700, 0], init_velocity=[18.0, 0, 0]),
    #                Earth(init_position=[0, -349597870.700, 0], init_velocity=[15.50, 0, 0])])

    _sys = System([Sun(), Earth(), Jupiter()])
M
march3 已提交
116

M
march3 已提交
117 118
    _sim = Simulator(_sys, t)
    _sim.run(t * 100)