simulator.py 2.4 KB
Newer Older
M
march3 已提交
1
# -*- coding:utf-8 -*-
M
march3 已提交
2 3
# title           :天体运行模拟器
# description     :天体运行模拟器
M
march3 已提交
4
# author          :Python超人
M
march3 已提交
5 6
# date            :2023-02-11
# link            :https://gitcode.net/pythoncr/
M
march3 已提交
7 8 9 10 11 12 13 14
# 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:
三月三net's avatar
三月三net 已提交
35
            view = viewer_type(body, self.bodies_sys)
M
march3 已提交
36
            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]
三月三net's avatar
三月三net 已提交
47
            body.dt = dt
M
march3 已提交
48 49 50 51
            view.appeared = body.appeared
            if not view.appeared:
                view.disappear()
                continue
M
march3 已提交
52 53 54 55 56 57
            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
M
march3 已提交
58
            view.raduis = body.raduis
M
march3 已提交
59 60 61
            view.his_position = body.his_position()
            view.is_fixed_star = body.is_fixed_star
            view.has_rings = body.has_rings
M
march3 已提交
62 63
            view.size_scale = body.size_scale
            view.distance_scale = body.distance_scale
M
march3 已提交
64 65

            view.update()
M
march3 已提交
66

三月三net's avatar
三月三net 已提交
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

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