cosmo/backend/app/services/rocket_preset_service.py

386 lines
14 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""高频运载火箭型号预置。
发射事件按型号关联到 rocket_configs,因此模拟器和事件列表里应当总有当下发射
最频繁的型号。这里登记一批公开资料里的型号参数,启动时按 code 补齐缺失项:
已存在的型号一律不动(管理端改过的参数以管理端为准),被删掉的型号会在下次
启动时重新补上(与默认系统设置的行为一致)。
stage_1 沿用库里既有约定:捆绑助推器的发动机数量与推力并入一级登记
(长征五号 10 台 = 芯级 2 台 + 4 个助推器各 2 台)。
"""
import logging
from typing import Any
from sqlalchemy import func, select, text
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.db import RocketConfig
logger = logging.getLogger(__name__)
HIGH_FREQUENCY_ROCKETS: list[dict[str, Any]] = [
{
"code": "starship",
"name": "Starship",
"name_zh": "星舰",
"manufacturer": "SpaceX",
"country": "美国",
"launch_site_name": "Starbase 星港",
"launch_latitude_deg": 25.9972,
"launch_longitude_deg": -97.1575,
"description": "两级完全可重复使用超重型运载火箭,一级为超重型助推器(33 台猛禽),二级为星舰飞船。",
"color": "#c9ced4",
"height_m": 122.5,
"diameter_m": 9.0,
"payload_mass_kg": 100000,
"drag_coefficient": 0.45,
"reference_area_m2": 63.62,
"target_orbit_km": 400,
"target_velocity_mps": 7800,
"separation_delay_seconds": 1.0,
"second_stage_ignition_delay_seconds": 0.6,
"stage_1": {
"name": "超重型助推器",
"dry_mass_kg": 240000,
"fuel_mass_kg": 3400000,
"max_thrust_n": 72000000,
"specific_impulse_s": 327,
"engine_count": 33,
"length_ratio": 0.58,
},
"stage_2": {
"name": "星舰飞船",
"dry_mass_kg": 120000,
"fuel_mass_kg": 1200000,
"max_thrust_n": 13800000,
"specific_impulse_s": 350,
"engine_count": 6,
"length_ratio": 0.42,
},
"appearance": {
"booster_count": 0,
"grid_fins": True,
"fairing": False,
},
},
{
"code": "electron",
"name": "Electron",
"name_zh": "电子号",
"manufacturer": "Rocket Lab",
"country": "新西兰",
"launch_site_name": "LC-1 马希亚发射场",
"launch_latitude_deg": -39.2618,
"launch_longitude_deg": -177.864,
"description": "小型液体运载火箭,一级 9 台卢瑟福发动机电动泵压循环,用于小卫星专属与拼车发射。",
"color": "#242a30",
"height_m": 33.0,
"diameter_m": 1.2,
"payload_mass_kg": 300,
"drag_coefficient": 0.35,
"reference_area_m2": 1.13,
"target_orbit_km": 500,
"target_velocity_mps": 7600,
"separation_delay_seconds": 2.0,
"second_stage_ignition_delay_seconds": 1.0,
"stage_1": {
"name": "一级(9 台卢瑟福)",
"dry_mass_kg": 1100,
"fuel_mass_kg": 8900,
"max_thrust_n": 189000,
"specific_impulse_s": 319,
"engine_count": 9,
"length_ratio": 0.66,
},
"stage_2": {
"name": "二级(真空卢瑟福)",
"dry_mass_kg": 200,
"fuel_mass_kg": 700,
"max_thrust_n": 19600,
"specific_impulse_s": 339,
"engine_count": 1,
"length_ratio": 0.34,
},
"appearance": {
"booster_count": 0,
"grid_fins": False,
},
},
{
"code": "long-march-2f",
"name": "Long March 2F",
"name_zh": "长征二号F",
"manufacturer": "中国运载火箭技术研究院",
"country": "中国",
"launch_site_name": "酒泉卫星发射中心",
"launch_latitude_deg": 40.958,
"launch_longitude_deg": 100.291,
"description": "带四个液体助推器的两级载人运载火箭,用于神舟载人飞船与空间实验室任务。",
"color": "#eef1f5",
"height_m": 58.34,
"diameter_m": 3.35,
"payload_mass_kg": 8150,
"drag_coefficient": 0.4,
"reference_area_m2": 8.81,
"target_orbit_km": 400,
"target_velocity_mps": 7670,
"separation_delay_seconds": 2.5,
"second_stage_ignition_delay_seconds": 1.2,
"stage_1": {
"name": "芯一级与四个助推器",
"dry_mass_kg": 50000,
"fuel_mass_kg": 340000,
"max_thrust_n": 5958000,
"specific_impulse_s": 264,
"engine_count": 8,
"length_ratio": 0.6,
},
"stage_2": {
"name": "芯二级(YF-24B)",
"dry_mass_kg": 5000,
"fuel_mass_kg": 55000,
"max_thrust_n": 720000,
"specific_impulse_s": 315,
"engine_count": 1,
"length_ratio": 0.4,
},
"appearance": {
"booster_count": 4,
"engines_per_booster": 1,
"booster_diameter_ratio": 0.67,
"grid_fins": False,
},
},
{
"code": "long-march-7",
"name": "Long March 7",
"name_zh": "长征七号",
"manufacturer": "中国运载火箭技术研究院",
"country": "中国",
"launch_site_name": "文昌航天发射场",
"launch_latitude_deg": 19.6145,
"launch_longitude_deg": 110.951,
"description": "4 米级液氧煤油中型运载火箭,四个助推器各两台 YF-100,用于天舟货运飞船等高频次发射。",
"color": "#e6ebf1",
"height_m": 53.1,
"diameter_m": 3.35,
"payload_mass_kg": 13800,
"drag_coefficient": 0.4,
"reference_area_m2": 8.81,
"target_orbit_km": 400,
"target_velocity_mps": 7670,
"separation_delay_seconds": 2.5,
"second_stage_ignition_delay_seconds": 1.2,
"stage_1": {
"name": "芯一级与四个助推器(10 台 YF-100)",
"dry_mass_kg": 40000,
"fuel_mass_kg": 470000,
"max_thrust_n": 7200000,
"specific_impulse_s": 300,
"engine_count": 10,
"length_ratio": 0.62,
},
"stage_2": {
"name": "芯二级(2 台 YF-115)",
"dry_mass_kg": 3000,
"fuel_mass_kg": 40000,
"max_thrust_n": 360000,
"specific_impulse_s": 334,
"engine_count": 2,
"length_ratio": 0.38,
},
"appearance": {
"booster_count": 4,
"engines_per_booster": 2,
"booster_diameter_ratio": 0.67,
"grid_fins": False,
},
},
{
"code": "long-march-6a",
"name": "Long March 6A",
"name_zh": "长征六号甲",
"manufacturer": "上海航天技术研究院",
"country": "中国",
"launch_site_name": "太原卫星发射中心",
"launch_latitude_deg": 38.846,
"launch_longitude_deg": 111.608,
"description": "液氧煤油芯级捆绑四台固体助推器的中型运载火箭,面向太阳同步轨道高频次拼车发射。",
"color": "#e9eef4",
"height_m": 50.5,
"diameter_m": 3.35,
"payload_mass_kg": 6500,
"drag_coefficient": 0.4,
"reference_area_m2": 8.81,
"target_orbit_km": 500,
"target_velocity_mps": 7600,
"separation_delay_seconds": 2.2,
"second_stage_ignition_delay_seconds": 1.0,
"stage_1": {
"name": "芯一级与四台固体助推器",
"dry_mass_kg": 45000,
"fuel_mass_kg": 405000,
"max_thrust_n": 6400000,
"specific_impulse_s": 285,
"engine_count": 10,
"length_ratio": 0.6,
},
"stage_2": {
"name": "二级(2 台 YF-115)",
"dry_mass_kg": 3000,
"fuel_mass_kg": 40000,
"max_thrust_n": 360000,
"specific_impulse_s": 334,
"engine_count": 2,
"length_ratio": 0.4,
},
"appearance": {
"booster_count": 4,
"engines_per_booster": 2,
"booster_diameter_ratio": 0.79,
"grid_fins": False,
},
},
{
"code": "long-march-8",
"name": "Long March 8",
"name_zh": "长征八号",
"manufacturer": "中国运载火箭技术研究院",
"country": "中国",
"launch_site_name": "文昌航天发射场",
"launch_latitude_deg": 19.6145,
"launch_longitude_deg": 110.951,
"description": "两级液氧煤油中型运载火箭,捆绑两个助推器,主打太阳同步轨道商业星座发射。",
"color": "#e6ebf1",
"height_m": 50.4,
"diameter_m": 3.35,
"payload_mass_kg": 7600,
"drag_coefficient": 0.4,
"reference_area_m2": 8.81,
"target_orbit_km": 500,
"target_velocity_mps": 7600,
"separation_delay_seconds": 2.2,
"second_stage_ignition_delay_seconds": 1.0,
"stage_1": {
"name": "芯一级与两个助推器(6 台 YF-100)",
"dry_mass_kg": 38000,
"fuel_mass_kg": 380000,
"max_thrust_n": 7194000,
"specific_impulse_s": 300,
"engine_count": 6,
"length_ratio": 0.6,
},
"stage_2": {
"name": "二级(2 台 YF-115)",
"dry_mass_kg": 3500,
"fuel_mass_kg": 55000,
"max_thrust_n": 360000,
"specific_impulse_s": 334,
"engine_count": 2,
"length_ratio": 0.4,
},
"appearance": {
"booster_count": 2,
"engines_per_booster": 2,
"booster_diameter_ratio": 0.67,
"grid_fins": False,
},
},
{
"code": "falcon-heavy",
"name": "Falcon Heavy",
"name_zh": "猎鹰重型",
"manufacturer": "SpaceX",
"country": "美国",
"launch_site_name": "LC-39A 肯尼迪航天中心",
"launch_latitude_deg": 28.6083,
"launch_longitude_deg": -80.6041,
"description": "三枚猎鹰 9 一芯级并联的重型运载火箭;载荷按三芯级回收构型登记(一次性构型约 63.8 吨)。",
"color": "#f8fafc",
"height_m": 70.0,
"diameter_m": 3.7,
"payload_mass_kg": 27700,
"drag_coefficient": 0.4,
"reference_area_m2": 10.75,
"target_orbit_km": 200,
"target_velocity_mps": 7800,
"separation_delay_seconds": 2.2,
"second_stage_ignition_delay_seconds": 1.0,
"stage_1": {
"name": "三芯级(27 台梅林 1D)",
"dry_mass_kg": 76800,
"fuel_mass_kg": 1233000,
"max_thrust_n": 22821000,
"specific_impulse_s": 282,
"engine_count": 27,
"length_ratio": 0.65,
},
"stage_2": {
"name": "二级(梅林真空型)",
"dry_mass_kg": 4000,
"fuel_mass_kg": 107500,
"max_thrust_n": 981000,
"specific_impulse_s": 348,
"engine_count": 1,
"length_ratio": 0.35,
},
"appearance": {
"booster_count": 2,
"engines_per_booster": 9,
"booster_diameter_ratio": 1.0,
"grid_fins": True,
},
},
]
async def ensure_rocket_appearance(db: AsyncSession) -> list[str]:
"""为已存在的预置型号补写外观构型。
预置逻辑只补插缺失的型号,早已入库的型号不会带上 appearance,因此这里做一次
幂等回填:仅处理库里 appearance 仍为空的行,管理端填过的值一律不动。
"""
presets = {p["code"]: p["appearance"] for p in HIGH_FREQUENCY_ROCKETS if p.get("appearance")}
# 部署时 up -d 早于 upgrade_db.py,列可能还没加上;这里跳过而不是让服务起不来
column = await db.execute(
text("SELECT 1 FROM information_schema.columns "
"WHERE table_name = 'rocket_configs' AND column_name = 'appearance'")
)
if column.first() is None:
logger.warning("rocket_configs.appearance 列不存在,跳过外观构型回填(执行 upgrade_db.py --apply 后重启生效)")
return []
result = await db.execute(
select(RocketConfig.code).where(RocketConfig.appearance.is_(None), RocketConfig.code.in_(presets))
)
codes = list(result.scalars().all())
updated: list[str] = []
for code in codes:
rocket = (await db.execute(select(RocketConfig).where(RocketConfig.code == code))).scalar_one()
rocket.appearance = dict(presets[code])
updated.append(code)
if updated:
await db.flush()
logger.info("火箭外观构型回填:%s", ", ".join(updated))
return updated
async def ensure_high_frequency_rockets(db: AsyncSession) -> list[str]:
"""补齐缺失的高频型号,返回新入库的 code 列表。"""
existing = set((await db.execute(select(RocketConfig.code))).scalars().all())
next_order = (await db.scalar(select(func.max(RocketConfig.sort_order)))) or 0
added: list[str] = []
for preset in HIGH_FREQUENCY_ROCKETS:
if preset["code"] in existing:
continue
next_order += 1
db.add(RocketConfig(**preset, sort_order=next_order))
added.append(preset["code"])
if added:
await db.flush()
logger.info("火箭型号预置补齐:%s", ", ".join(added))
return added