import gradio as gr import numpy as np from dataclasses import dataclass, field from typing import Dict, Tuple, List from functools import lru_cache @dataclass(frozen=True) class BatterySpecs: MODELO: str = 'Husqvarna Hard Cross HC3' MOTOR: str = 'Shimano EP6 (85Nm)' CAPACIDAD_NOMINAL_AH: float = 17.5 VOLTAJE_NOMINAL: float = 36.0 BUFFER_SEGURIDAD_BMS: float = 0.05 PESO_BICI_KG: float = 24.5 PESO_EQUIPO_KG: float = 4.5 AMPERAJE_CARGADOR: float = 4.0 OCV_X: Tuple[float, ...] = (0.0, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 1.0) OCV_Y: Tuple[float, ...] = (31.0, 32.5, 34.0, 35.1, 35.8, 36.3, 36.7, 37.2, 38.0, 39.2, 40.8, 41.5, 42.0) TEMP_X: Tuple[float, ...] = (-10.0, -5.0, 0.0, 5.0, 10.0, 20.0, 40.0) TEMP_Y: Tuple[float, ...] = (0.70, 0.78, 0.85, 0.92, 0.97, 1.00, 1.00) PERFILES_TORQUE: Dict[str, List[float]] = field(default_factory=lambda: { 'ECO (Nvl 1)': [27, 5.0, 0.40, 0.96], 'Nivel 2': [34, 6.8, 0.50, 0.95], 'TRAIL (Nvl 3)':[41, 8.9, 0.60, 0.94], 'Nivel 4': [48, 11.0, 0.70, 0.92], 'Nivel 5': [55, 12.8, 0.80, 0.90], 'BOOST (Nvl 6)':[63, 14.5, 0.90, 0.88] }) SPECS = BatterySpecs() CUSTOM_CSS = """ footer {display: none !important;} .error-box {background:#fee2e2; padding:20px; color:#991b1b; text-align:center; font-weight:bold; border-radius:12px;} .ui-container {font-family: system-ui, sans-serif; max-width: 450px; margin: 0 auto; color: #111827; width: 100%; box-sizing: border-box;} @media (max-width: 480px) { .cards-row { flex-direction: column; gap: 8px; } } @media (prefers-color-scheme: dark) { .ui-container { filter: invert(0.9) hue-rotate(180deg); } .hero-card { filter: invert(1) hue-rotate(180deg); } } .hero-card {background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%); color: white; padding: 20px; border-radius: 16px; text-align: center; box-shadow: 0 4px 12px rgba(37,99,235,0.25); margin-bottom: 20px;} .hero-title {font-size: 10px; font-weight: 700; opacity: 0.9; letter-spacing: 1px;} .hero-time {font-size: 42px; font-weight: 800; line-height: 1.1;} .hero-badge {background: rgba(255,255,255,0.2); font-size: 12px; padding: 3px 10px; border-radius: 99px; display:inline-block; margin-top:6px;} .cards-row {display: flex; justify-content: space-between; margin-bottom: 20px; gap:4px;} .strategy-card {flex: 1; padding: 10px; border-radius: 12px; border: 1px solid #e5e7eb; background: #f9fafb; opacity: 0.6; transition: all 0.2s;} .strategy-card.active {background: white; opacity: 1; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1);} .strategy-card.storage.active {border: 2px solid #6366f1;} .strategy-card.daily.active {border: 2px solid #16a34a;} .strategy-card.balance.active {border: 2px solid #ea580c;} .strategy-title {font-size:9px; text-transform:uppercase; font-weight:600; color:#6b7280;} .strategy-card.storage.active .strategy-title {color: #4f46e5; font-weight: 800;} .strategy-card.daily.active .strategy-title {color: #15803d; font-weight: 800;} .strategy-card.balance.active .strategy-title {color: #c2410c; font-weight: 800;} .strategy-pct {font-size:16px; font-weight:800; color:#1f2937;} .strategy-desc {font-size:9px; color:#4b5563; line-height:1.1; margin-top:3px;} .warning-box {background: #fff1f2; border: 1px solid #fda4af; padding: 12px; border-radius: 12px; margin-bottom: 16px;} .warning-title {font-size:11px; font-weight:800; color:#be123c; margin-bottom:2px;} .warning-text {font-size:11px; color:#9f1239;} .info-box {background: #eff6ff; border: 1px solid #bfdbfe; color: #1e40af; padding: 10px 12px; border-radius: 12px; margin-bottom: 16px; font-size: 11px; display:flex; align-items:center;} .info-icon {font-size:16px; margin-right:8px;} .results-container {background: white; border: 1px solid #e5e7eb; border-radius: 16px; overflow: hidden;} .results-header {padding: 10px 16px; background: #f9fafb; border-bottom: 1px solid #e5e7eb; font-size: 10px; font-weight: 700; color: #6b7280; letter-spacing: 0.5px;} .mode-row {padding: 12px 16px; border-bottom: 1px solid #f3f4f6;} .mode-row.exhausted {background: #fef2f2;} .mode-header {display:flex; justify-content:space-between; align-items:center; margin-bottom:4px;} .mode-name {font-size: 13px; font-weight: 700; color: #1f2937;} .mode-name.exhausted {color: #991b1b;} .mode-dist {font-size: 16px; font-weight: 800;} .mode-dist.exhausted {color: #dc2626; font-size: 13px;} .progress-bar {height: 6px; background: #f3f4f6; border-radius: 3px; overflow:hidden; display:flex;} .progress-used {background: #94a3b8;} .mode-details {display:flex; justify-content:space-between; margin-top:2px; font-size: 9px; color: #9ca3af;} """ @lru_cache(maxsize=128) def get_voltage(soc: float) -> float: return float(np.interp(soc, SPECS.OCV_X, SPECS.OCV_Y)) @lru_cache(maxsize=32) def factor_clima_ruta(temp: float) -> float: return float(np.interp(temp, SPECS.TEMP_X, SPECS.TEMP_Y)) def calcular_salud(km: float, ciclos_drift: int) -> dict: ciclos = max(0.0, float(km) / 75.0) degradacion = (0.03 * (1 - np.exp(-0.05 * ciclos))) + (0.0002 * ciclos) soh = max(0.60, min(1.0, 1.0 - degradacion)) drift_pct = 0.08 * ((ciclos_drift - 5) / (ciclos_drift + 10)) if ciclos_drift > 5 else 0.0 return {"soh": soh, "drift": drift_pct} def simulador_master_v53(km: float, ciclos_drift: int, inicio: float, fin: float, temp_trastero: float) -> dict: if temp_trastero < -5 or temp_trastero > 50: return {"estado": "ERROR_TEMP"} salud = calcular_salud(km, ciclos_drift) soh = salud["soh"] if inicio >= fin: return { "estado": "CARGADO", "soh": soh, "drift": salud["drift"], "tiempo": 0.0, "energia": 0.0, "cap_final_wh": 0.0 } cap_total = SPECS.CAPACIDAD_NOMINAL_AH * soh ah_actual = cap_total * (inicio / 100.0) ah_objetivo = cap_total * (fin / 100.0) f_temp = max(0.15, 0.4 + (0.04 * temp_trastero)) if temp_trastero < 15 else 0.92 if temp_trastero > 35 else 1.0 t_min = 0.0 e_wh = 0.0 dt = 0.5 while ah_actual < ah_objetivo: soc_now = ah_actual / cap_total v_now = get_voltage(soc_now) i_target = SPECS.AMPERAJE_CARGADOR * f_temp if v_now >= (SPECS.OCV_Y[-1] - 0.25): diff = v_now - (SPECS.OCV_Y[-1] - 0.6) i_real = max(0.1, i_target * np.exp(-4.5 * max(0, diff))) else: i_real = i_target delta_ah = i_real * (dt / 60.0) * 0.98 distancia = ah_objetivo - ah_actual if delta_ah >= distancia: delta_ah = distancia t_step = (delta_ah / (i_real * 0.98)) * 60.0 if i_real > 0 else 0 ah_actual += delta_ah e_wh += (delta_ah * v_now) t_min += t_step break ah_actual += delta_ah e_wh += (delta_ah * v_now) t_min += dt return { "estado": "OK", "soh": soh, "drift": salud["drift"], "tiempo": t_min, "energia": e_wh, "cap_final_wh": ah_actual * SPECS.VOLTAJE_NOMINAL } def generar_interfaz_final(km_val: float, temp_trastero: float, temp_ruta: float, elev_val: float, peso_piloto: float, ciclos_drift: int, inicio_val: float, fin_val: float) -> str: res = simulador_master_v53(float(km_val), int(ciclos_drift), float(inicio_val), float(fin_val), float(temp_trastero)) if res["estado"] == "ERROR_TEMP": return "