提交 e8fb52a4 编写于 作者: 三月三net's avatar 三月三net

太阳系三体模拟器

上级 137b4388
......@@ -353,7 +353,11 @@ class Body(metaclass=ABCMeta):
with open(json_file, "r", encoding='utf-8') as read_content:
json_data = json.load(read_content)
for body_data in json_data["bodies"]:
# print(body_data)
try:
body_data = Body.exp(body_data) # print(body_data)
except Exception as e:
err_msg = f"{json_file} 格式错误:" + str(e)
raise Exception(err_msg)
body = Body(**body_data)
bodies.append(body)
if "params" in json_data:
......@@ -361,6 +365,29 @@ class Body(metaclass=ABCMeta):
# print(body.position_au())
return bodies, params
@staticmethod
def exp(body_data):
"""
进行表达式分析,将表达式改为eval执行后的结果
:param body_data:
:return:
"""
#
for k in body_data.keys():
v = body_data[k]
if isinstance(v, str):
if v.startswith("$exp:"):
exp = v[5:]
body_data[k] = eval(exp)
elif isinstance(v, list):
for idx,item in enumerate(v):
if isinstance(item, str):
if item.startswith("$exp:"):
exp = item[5:]
v[idx] = eval(exp)
return body_data
if __name__ == '__main__':
# build_bodies_from_json('../data/sun.json')
......
......@@ -5,11 +5,11 @@
"mass": 2e+30,
"init_position": [
0.0,
518222240.0,
"$exp:math.sqrt(3) * 2 * AU",
0.0
],
"init_velocity": [
-14.890000343322754,
-14.89,
0.0,
0.0
],
......@@ -27,13 +27,13 @@
"name": "绿轨太阳B",
"mass": 2e+30,
"init_position": [
-299195744.0,
"$exp: -2 * AU",
0.0,
0.0
],
"init_velocity": [
7.445000171661377,
-12.895118713378906,
"$exp: 1/2 * 14.88",
"$exp:-math.sqrt(3) / 2 * 14.88",
0.0
],
"density": 1408.0,
......@@ -50,13 +50,13 @@
"name": "蓝轨太阳C",
"mass": 2e+30,
"init_position": [
299195744.0,
"$exp: 2 * AU",
0.0,
0.0
],
"init_velocity": [
7.445000171661377,
12.895118713378906,
"$exp: 1/2 * 14.88",
"$exp:math.sqrt(3) / 2 * 14.88",
0.0
],
"density": 1408.0,
......
......@@ -13,7 +13,7 @@ from scenes.func import ursina_run
if __name__ == '__main__':
# TODO: 去掉注释,太阳演示
bodies, params = Body.build_bodies_from_json('../data/sun.json')
# bodies, params = Body.build_bodies_from_json('../data/sun.json')
# TODO: 去掉注释,太阳和地球演示
# bodies, params = Body.build_bodies_from_json('../data/sun_earth.json')
# TODO: 去掉注释,在地球上看月相演示变化过程演示
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册