#!/usr/bin/env python3
"""ЭДТС-1: размерные оценки для стратегии испытаний, версия 1.0.

Не модель орбиты, не результаты натурных испытаний и не назначение допусков.
Проектные входы: budget/LEDGER.md, блок D-062, строки 19, 24, 26–38.
Иллюстративные настройки стенда явно отделены от проектных входов.
Запуск: python3 calc/test_strategy_sizing.py
"""
import json
import math


def calculate():
    g0 = 9.80665  # стандартное ускорение, м/с²
    project = {
        "mass_kg": 14.84,
        "tether_length_m": 1000.0,
        "tether_mass_kg": 0.143,
        "tension_N": 7.45e-3,
        "current_A": 0.349,
        "voltage_V": 226.0,
        "cold_tether_R_ohm": 532.0,
        "hot_tether_R_ohm": 819.0,
        "f_light": 0.593,
        "k_at_working_point_mN_per_A": 5.21,
    }
    # Не спецификация оборудования: примеры выбора масштаба измерений.
    examples = {
        "converter_eta": 0.88,
        "force_active_length_m": 0.10,
        "force_B_T": 0.030,
        "collector_voltage_V": 300.0,
        "grid_voltage_V": 300.0,
        "useful_electron_fraction": 0.90,
        "reference_emission_density_mA_cm2": 5.0,
        "year_h": 365.0 * 24.0,
        "reported_reference_endurance_h": 1500.0,
        "insulation_R_ohm": 20e6,  # ПМИ 0.8 §5, не универсальный допуск
        "insulation_voltage_V": 400.0,
    }
    i, u = project["current_A"], project["voltage_V"]
    p_out = i * u
    weight_per_m = project["tether_mass_kg"] / project["tether_length_m"] * g0
    force = i * examples["force_active_length_m"] * examples["force_B_T"]
    t = examples["useful_electron_fraction"]
    results = {
        "nominal_resistive_load_ohm": u / i,
        "nominal_load_dissipation_W": p_out,
        "example_bus_power_W": p_out / examples["converter_eta"],
        "cold_wire_uniform_current_heating_W_per_m": i**2 * project["cold_tether_R_ohm"] / project["tether_length_m"],
        "hot_wire_uniform_current_heating_W_per_m": i**2 * project["hot_tether_R_ohm"] / project["tether_length_m"],
        "whole_cold_tether_uniform_current_heat_W": i**2 * project["cold_tether_R_ohm"],
        "whole_hot_tether_uniform_current_heat_W": i**2 * project["hot_tether_R_ohm"],
        "illustrative_insulation_leakage_uA": examples["insulation_voltage_V"] / examples["insulation_R_ohm"] * 1e6,
        "tether_weight_mN_per_m": weight_per_m * 1e3,
        "hanging_length_equal_to_nominal_tension_m": project["tension_N"] / weight_per_m,
        "nominal_tension_equivalent_mass_g": project["tension_N"] / g0 * 1e3,
        "illustrative_local_force_mN": force * 1e3,
        "illustrative_local_force_mass_equivalent_mg": force / g0 * 1e6,
        "illustrative_collector_heat_W": i * examples["collector_voltage_V"],
        "illustrative_grid_interception_current_mA": i * (1.0 / t - 1.0) * 1e3,
        "illustrative_grid_heat_W": i * (1.0 / t - 1.0) * examples["grid_voltage_V"],
        "reference_active_emission_area_cm2": i * 1e3 / examples["reference_emission_density_mA_cm2"],
        "light_only_hours_constant_beta0": examples["year_h"] * project["f_light"],
        "ratio_to_reported_reference_endurance": examples["year_h"] * project["f_light"] / examples["reported_reference_endurance_h"],
        "K5_threshold_mean_force_mN": 0.01 * project["mass_kg"],
        "simplified_working_point_mean_force_mN": project["k_at_working_point_mN_per_A"] * i * project["f_light"],
    }
    assert math.isclose(results["nominal_load_dissipation_W"], 78.874)
    assert math.isclose(results["K5_threshold_mean_force_mN"], 0.1484)
    assert 5.3 < results["hanging_length_equal_to_nominal_tension_m"] < 5.4
    assert math.isclose(results["illustrative_local_force_mN"], 1.047)
    assert math.isclose(results["illustrative_collector_heat_W"], 104.7)
    assert math.isclose(results["reference_active_emission_area_cm2"], 69.8)
    assert math.isclose(results["whole_cold_tether_uniform_current_heat_W"], 64.798132)
    assert math.isclose(results["whole_hot_tether_uniform_current_heat_W"], 99.755019)
    assert math.isclose(results["illustrative_insulation_leakage_uA"], 20.0)
    return {
        "version": "1.0",
        "status": "расчёт масштаба стенда; не измерения и не квалификация",
        "project_inputs_D062": project,
        "illustrative_inputs_not_requirements": examples,
        "results": results,
    }


if __name__ == "__main__":
    print(json.dumps(calculate(), ensure_ascii=False, indent=2))
