simulator.py 2.3 KB
Newer Older
M
march3 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# -*- coding:utf-8 -*-
# title           :
# description     :
# author          :Python超人
# date            :2023-01-22
# notes           :
# python_version  :3.8
# ==============================================================================
from abc import ABCMeta, abstractmethod
from common.system import System


class Simulator(metaclass=ABCMeta):
    """
M
march3 已提交
15
    天体运行模拟器
M
march3 已提交
16 17 18 19 20 21 22 23
    """

    def __init__(self, bodies_sys: System, viewer_type: type):
        """

        :param bodies_sys: 天体系统
        :param viewer_type: BodyViewer类型
        """
M
march3 已提交
24
        self.body_views = []
M
march3 已提交
25
        self.bodies_sys = bodies_sys
M
march3 已提交
26
        self.init_views(viewer_type)
M
march3 已提交
27

M
march3 已提交
28
    def init_views(self, viewer_type: type):
M
march3 已提交
29 30 31 32 33 34
        """

        :param viewer_type: BodyViewer类型
        :return:
        """
        for body in self.bodies_sys.bodies:
M
march3 已提交
35 36
            view = viewer_type(body)
            self.body_views.append(view)
M
march3 已提交
37 38 39

    def evolve(self, dt: int):
        """
M
march3 已提交
40
        单位:秒,按时间差进行演变,值越小越精确,但演变速度会慢。
M
march3 已提交
41 42 43 44
        :param dt: 时间差(秒)
        :return:
        """
        self.bodies_sys.evolve(dt)
M
march3 已提交
45 46
        for idx, view in enumerate(self.body_views):
            body = self.bodies_sys.bodies[idx]
M
march3 已提交
47 48 49 50
            view.appeared = body.appeared
            if not view.appeared:
                view.disappear()
                continue
M
march3 已提交
51 52 53 54 55 56 57 58 59 60
            view.position = body.position * body.distance_scale
            view.name = body.name
            view.mass = body.mass
            view.acceleration = body.acceleration
            view.velocity = body.velocity
            # viewer.volume = body.volume
            view.raduis = body.raduis * body.size_scale
            view.his_position = body.his_position()
            view.is_fixed_star = body.is_fixed_star
            view.has_rings = body.has_rings
M
march3 已提交
61 62
            view.size_scale = body.size_scale
            view.distance_scale = body.distance_scale
M
march3 已提交
63 64

            view.update()
M
march3 已提交
65

M
march3 已提交
66 67 68
        self.bodies_sys.bodies = list(filter(lambda b:b.appeared, self.bodies_sys.bodies))
        self.body_views = list(filter(lambda b:b.appeared, self.body_views))

M
march3 已提交
69 70 71 72 73 74 75 76
    @abstractmethod
    def run(self, dt: int):
        """
        按时间差运行,值越小越精确,但演变速度会慢。
        :param dt: 时间差(秒)
        :return:
        """
        pass