185 lines
6.4 KiB
Python
185 lines
6.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Script para corregir específicamente el problema de división por cero en TSNet
|
|
Identifica y arregla el problema del timestep = 0
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
import traceback
|
|
|
|
|
|
def fix_tsnet_timestep_issue():
|
|
"""
|
|
Corrige el problema específico del timestep en TSNet
|
|
"""
|
|
print("=== Corrección específica del problema TSNet timestep ===")
|
|
|
|
# Importar dependencias
|
|
try:
|
|
import tsnet
|
|
import wntr
|
|
|
|
print(f"✓ TSNet version: {tsnet.__version__}")
|
|
print(f"✓ WNTR version: {wntr.__version__}")
|
|
except ImportError as e:
|
|
print(f"✗ Error al importar: {e}")
|
|
return False
|
|
|
|
# Archivo INP problemático
|
|
inp_file_path = (
|
|
r"c:\Users\migue\AppData\Local\Temp\TSNet\network_20250911_235556.inp"
|
|
)
|
|
|
|
if not os.path.exists(inp_file_path):
|
|
print(f"✗ Archivo INP no encontrado: {inp_file_path}")
|
|
return False
|
|
|
|
print(f"✓ Archivo INP encontrado: {inp_file_path}")
|
|
|
|
try:
|
|
print("\n=== Análisis del problema del timestep ===")
|
|
|
|
# Cargar con WNTR primero
|
|
wn = wntr.network.WaterNetworkModel(inp_file_path)
|
|
|
|
print(f"WNTR - Duración: {wn.options.time.duration}")
|
|
print(f"WNTR - Hydraulic timestep: {wn.options.time.hydraulic_timestep}")
|
|
print(f"WNTR - Report timestep: {wn.options.time.report_timestep}")
|
|
|
|
# Configurar valores seguros en WNTR
|
|
wn.options.time.duration = 1.0 # 1 segundo
|
|
wn.options.time.hydraulic_timestep = 0.1 # 0.1 segundos
|
|
wn.options.time.report_timestep = 0.1 # 0.1 segundos
|
|
|
|
print(f"WNTR CORREGIDO - Duración: {wn.options.time.duration}")
|
|
print(
|
|
f"WNTR CORREGIDO - Hydraulic timestep: {wn.options.time.hydraulic_timestep}"
|
|
)
|
|
|
|
# Guardar el modelo WNTR corregido en un archivo temporal
|
|
import tempfile
|
|
|
|
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)
|
|
|
|
print(f"✓ Archivo INP temporal corregido: {temp_inp_path}")
|
|
|
|
# Cargar con TSNet usando el archivo corregido
|
|
tm = tsnet.network.TransientModel(temp_inp_path)
|
|
|
|
print(
|
|
f"TSNet - simulation_period: {getattr(tm, 'simulation_period', 'NO DEFINIDO')}"
|
|
)
|
|
print(f"TSNet - time_step: {getattr(tm, 'time_step', 'NO DEFINIDO')}")
|
|
|
|
# Verificar y corregir atributos específicos de TSNet
|
|
if not hasattr(tm, "simulation_period") or tm.simulation_period <= 0:
|
|
print("⚠ simulation_period no definido o <= 0, configurando manualmente")
|
|
tm.simulation_period = 1.0
|
|
|
|
if not hasattr(tm, "time_step") or tm.time_step <= 0:
|
|
print("⚠ time_step no definido o <= 0, configurando manualmente")
|
|
tm.time_step = 0.1
|
|
|
|
print(f"TSNet CORREGIDO - simulation_period: {tm.simulation_period}")
|
|
print(f"TSNet CORREGIDO - time_step: {tm.time_step}")
|
|
|
|
# Verificar que la división será válida
|
|
if tm.time_step > 0 and tm.simulation_period > 0:
|
|
total_steps = int(tm.simulation_period / tm.time_step)
|
|
print(f"✓ Total de pasos de simulación: {total_steps}")
|
|
|
|
if total_steps <= 0:
|
|
print("✗ Total de pasos <= 0, ajustando timestep")
|
|
tm.time_step = tm.simulation_period / 10.0 # Al menos 10 pasos
|
|
total_steps = int(tm.simulation_period / tm.time_step)
|
|
print(f"✓ Total de pasos corregido: {total_steps}")
|
|
else:
|
|
print("✗ Parámetros temporales inválidos")
|
|
return False
|
|
|
|
print("\n=== Intentando simulación TSNet corregida ===")
|
|
|
|
# Intentar simulación
|
|
results = tsnet.simulation.MOCSimulator(
|
|
tm, results_obj="results", friction="steady"
|
|
)
|
|
|
|
print("🎉 ¡SIMULACIÓN TSNET EXITOSA!")
|
|
print("✓ El problema de división por cero está resuelto")
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"✗ Error: {e}")
|
|
traceback.print_exc()
|
|
|
|
# Análisis del error
|
|
error_str = str(e).lower()
|
|
if "division" in error_str and "zero" in error_str:
|
|
print("\n💡 ANÁLISIS DEL ERROR:")
|
|
print(
|
|
"El problema persiste en la división tm.simulation_period/tm.time_step"
|
|
)
|
|
print(
|
|
"Esto indica que TSNet no está usando correctamente los valores configurados"
|
|
)
|
|
|
|
# Investigar más a fondo
|
|
try:
|
|
print(
|
|
f"\nDEBUG - tm.simulation_period type: {type(getattr(tm, 'simulation_period', None))}"
|
|
)
|
|
print(
|
|
f"DEBUG - tm.simulation_period value: {getattr(tm, 'simulation_period', None)}"
|
|
)
|
|
print(
|
|
f"DEBUG - tm.time_step type: {type(getattr(tm, 'time_step', None))}"
|
|
)
|
|
print(f"DEBUG - tm.time_step value: {getattr(tm, 'time_step', None)}")
|
|
|
|
# Intentar configuración directa
|
|
print("\n=== Configuración directa de atributos ===")
|
|
tm.simulation_period = float(1.0)
|
|
tm.time_step = float(0.1)
|
|
|
|
print(f"POST-CONFIG - simulation_period: {tm.simulation_period}")
|
|
print(f"POST-CONFIG - time_step: {tm.time_step}")
|
|
|
|
# Verificar si hay otros atributos que controlen esto
|
|
for attr in dir(tm):
|
|
if (
|
|
"time" in attr.lower()
|
|
or "period" in attr.lower()
|
|
or "step" in attr.lower()
|
|
):
|
|
print(f"DEBUG - {attr}: {getattr(tm, attr, 'ERROR')}")
|
|
|
|
except Exception as debug_error:
|
|
print(f"Error en debug: {debug_error}")
|
|
|
|
return False
|
|
|
|
|
|
def main():
|
|
"""Función principal"""
|
|
success = fix_tsnet_timestep_issue()
|
|
|
|
if success:
|
|
print("\n🎉 ¡PROBLEMA RESUELTO!")
|
|
print("TSNet puede simular el archivo INP correctamente")
|
|
else:
|
|
print("\n❌ PROBLEMA NO RESUELTO")
|
|
print("Se requiere investigación adicional o corrección manual de TSNet")
|
|
|
|
return success
|
|
|
|
|
|
if __name__ == "__main__":
|
|
success = main()
|
|
sys.exit(0 if success else 1)
|