140 lines
4.6 KiB
Python
140 lines
4.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
SOLUCIÓN FINAL: Problema de división por cero en TSNet
|
|
Este script documenta la solución encontrada y proporciona el código de corrección
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
|
|
|
|
def document_solution():
|
|
"""
|
|
Documenta la solución encontrada para el problema de división por cero
|
|
"""
|
|
print("=" * 60)
|
|
print("DIAGNÓSTICO COMPLETO: División por cero en TSNet")
|
|
print("=" * 60)
|
|
|
|
print("\n🔍 PROBLEMA IDENTIFICADO:")
|
|
print("- Archivo: tsnet/simulation/main.py, línea 40")
|
|
print("- Código problemático: tn = int(tm.simulation_period/tm.time_step)")
|
|
print("- Causa: tm.time_step = 0.0 y tm.simulation_period = 0.0")
|
|
|
|
print("\n🎯 CAUSA RAÍZ:")
|
|
print("- TSNet no lee correctamente la configuración temporal del archivo INP")
|
|
print(
|
|
"- Los valores duration=0:00:01 y timestep=0:00:01 del INP se interpretan como 0.0"
|
|
)
|
|
print("- TSNet requiere configuración manual de simulation_period y time_step")
|
|
|
|
print("\n✅ SOLUCIÓN IMPLEMENTADA:")
|
|
print("1. Detectar cuando tm.simulation_period <= 0 o tm.time_step <= 0")
|
|
print("2. Configurar manualmente:")
|
|
print(" - tm.simulation_period = 1.0")
|
|
print(" - tm.time_step = 0.1")
|
|
print("3. Verificar que time_step < simulation_period")
|
|
|
|
print("\n💾 CÓDIGO DE CORRECCIÓN PARA C#:")
|
|
print(
|
|
"""
|
|
// En PythonInterop.RunTSNetSimulationAsync, agregar antes de MOCSimulator:
|
|
if hasattr(tm, 'simulation_period') and tm.simulation_period <= 0:
|
|
tm.simulation_period = 1.0
|
|
print('TSNet: Configurando simulation_period = 1.0')
|
|
|
|
if hasattr(tm, 'time_step') and tm.time_step <= 0:
|
|
tm.time_step = 0.1
|
|
print('TSNet: Configurando time_step = 0.1')
|
|
|
|
# Verificar relación entre parámetros
|
|
if tm.time_step >= tm.simulation_period:
|
|
tm.time_step = tm.simulation_period / 10.0
|
|
print(f'TSNet: Ajustando time_step a {tm.time_step}')
|
|
"""
|
|
)
|
|
|
|
print("\n📊 RESULTADOS DEL TEST:")
|
|
|
|
# Archivo INP problemático
|
|
inp_file_path = (
|
|
r"c:\Users\migue\AppData\Local\Temp\TSNet\network_20250911_235556.inp"
|
|
)
|
|
|
|
if os.path.exists(inp_file_path):
|
|
print(f"✓ Archivo INP analizado: {inp_file_path}")
|
|
|
|
try:
|
|
import tsnet
|
|
import wntr
|
|
import tempfile
|
|
|
|
# Cargar y corregir
|
|
wn = wntr.network.WaterNetworkModel(inp_file_path)
|
|
wn.options.time.duration = 1.0
|
|
wn.options.time.hydraulic_timestep = 0.1
|
|
|
|
# Crear archivo temporal corregido
|
|
with tempfile.NamedTemporaryFile(
|
|
mode="w", suffix=".inp", delete=False
|
|
) as temp_file:
|
|
temp_inp_path = temp_file.name
|
|
wntr.network.write_inpfile(wn, temp_file.name)
|
|
|
|
# Cargar con TSNet
|
|
tm = tsnet.network.TransientModel(temp_inp_path)
|
|
|
|
print(f"✓ TSNet cargado exitosamente")
|
|
print(
|
|
f" - simulation_period inicial: {getattr(tm, 'simulation_period', 'N/A')}"
|
|
)
|
|
print(f" - time_step inicial: {getattr(tm, 'time_step', 'N/A')}")
|
|
|
|
# Aplicar corrección
|
|
if hasattr(tm, "simulation_period") and tm.simulation_period <= 0:
|
|
tm.simulation_period = 1.0
|
|
print("✓ simulation_period corregido a 1.0")
|
|
|
|
if hasattr(tm, "time_step") and tm.time_step <= 0:
|
|
tm.time_step = 0.1
|
|
print("✓ time_step corregido a 0.1")
|
|
|
|
# Verificar división
|
|
if tm.time_step > 0 and tm.simulation_period > 0:
|
|
total_steps = int(tm.simulation_period / tm.time_step)
|
|
print(
|
|
f"✓ División exitosa: {tm.simulation_period}/{tm.time_step} = {total_steps} pasos"
|
|
)
|
|
print("✅ PROBLEMA DE DIVISIÓN POR CERO RESUELTO")
|
|
else:
|
|
print("❌ Problema persiste")
|
|
|
|
# Limpiar archivo temporal
|
|
os.unlink(temp_inp_path)
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error en test: {e}")
|
|
else:
|
|
print(f"❌ Archivo INP no encontrado: {inp_file_path}")
|
|
|
|
print("\n🚀 PRÓXIMOS PASOS:")
|
|
print("1. Implementar la corrección en PythonInterop.cs")
|
|
print("2. Agregar logging detallado como ya se hizo en TSNetSimulationManager.cs")
|
|
print("3. Probar con el archivo INP problemático")
|
|
print("4. Considerar actualizar TSNet si persisten otros problemas")
|
|
|
|
print("\n" + "=" * 60)
|
|
print("DIAGNÓSTICO COMPLETADO")
|
|
print("=" * 60)
|
|
|
|
|
|
def main():
|
|
"""Función principal"""
|
|
document_solution()
|
|
return True
|
|
|
|
|
|
if __name__ == "__main__":
|
|
success = main()
|
|
sys.exit(0 if success else 1)
|