From 21e6c410e2528e65e15ca6c9c2a6a37321c5d516 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby Date: Thu, 9 Oct 2025 15:26:24 +0200 Subject: [PATCH 1/3] Solver: RubarBE: Add read results method. --- src/Solver/RubarBE.py | 57 ++++++++++++++++++++++++++++++++---- src/View/RunSolver/Window.py | 3 +- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/Solver/RubarBE.py b/src/Solver/RubarBE.py index 41e9d666..a3467d0f 100644 --- a/src/Solver/RubarBE.py +++ b/src/Solver/RubarBE.py @@ -20,7 +20,7 @@ import os import logging import numpy as np -from tools import timer, trace, old_pamhyr_date_to_timestamp +from tools import timer, trace, old_pamhyr_date_to_timestamp, logger_exception from Solver.CommandLine import CommandLineSolver @@ -543,8 +543,53 @@ class Rubar3(CommandLineSolver): for d0, d1 in bc.data: f.write(f"{d1} {d0}\n") - def read_hydlim(self, study, fname, results, qlog=None, name="0"): - return + def read_profil(self, study, fname, results, qlog=None, name="0"): + logger.info(f"read: profil.{name}") + with open(fname, "r") as f: + reachs = [] + for i, edge in enumerate(study.river.enable_edges()): + reach = edge.reach + # Add results reach to reach list + res_reach = results.river.add(i) + reachs.append((res_reach, len(reach) + 1)) + + def read_data_line(f): + line = f.readline().split() + return tuple(map(float, line)) + + ts = set() + end = False + while True: + line = f.readline() + if line == "": + return + + timestamp = float(line) + + for reach, lm in reachs: + # First profile + z, s, q, e = read_data_line(f) + reach.set(0, timestamp, "Z", z) + reach.set(0, timestamp, "Q", q) + reach.set(0, timestamp, "V", s) + + # For each profile + ind = 1 + pz, ps, pq, pe = read_data_line(f) + while ind < lm - 2: + z, s, q, e = read_data_line(f) + reach.set(ind, timestamp, "Z", (z + pz) / 2) + reach.set(ind, timestamp, "Q", (q + pq) / 2) + reach.set(ind, timestamp, "V", (q + ps) / 2) + + pz, ps, pq, pe = z, s, q, e + ind += 1 + + # Last profile + z, s, q, e = read_data_line(f) + reach.set(ind, timestamp, "Z", z) + reach.set(ind, timestamp, "Q", q) + reach.set(ind, timestamp, "V", s) @timer def results(self, study, repertory, qlog=None, name="0"): @@ -554,14 +599,14 @@ class Rubar3(CommandLineSolver): repertory=repertory, name=name, ) - results_file = f"hydlim.{name}" + results_file = f"profil.{name}" fname = os.path.join(repertory, results_file) if not os.path.isfile(fname): - logger.info(f"Result file {results_file} does not exist") + logger.warning(f"Result file {results_file} does not exist") return None try: - self.read_hydlim(study, fname, results, qlog, name=name) + self.read_profil(study, fname, results, qlog, name=name) except Exception as e: logger.error(f"Failed to read results") logger_exception(e) diff --git a/src/View/RunSolver/Window.py b/src/View/RunSolver/Window.py index 0b96c1d5..22b24bbc 100644 --- a/src/View/RunSolver/Window.py +++ b/src/View/RunSolver/Window.py @@ -500,7 +500,8 @@ class SolverLogWindow(PamhyrWindow): if self._results is None: def reading_fn(): self._results = self._solver.results( - self._study, self._workdir, qlog=self._output + self._study, self._workdir, + qlog=self._output, name=self._study.name ) dlg = WaitingDialog( From df3b3b604de1c6f6b1f1eec4ddc90beb5415cc4e Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby Date: Thu, 9 Oct 2025 16:49:47 +0200 Subject: [PATCH 2/3] Solver: RubarBE: Factorise some code. --- src/Solver/RubarBE.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/Solver/RubarBE.py b/src/Solver/RubarBE.py index a3467d0f..ff7636e5 100644 --- a/src/Solver/RubarBE.py +++ b/src/Solver/RubarBE.py @@ -557,6 +557,17 @@ class Rubar3(CommandLineSolver): line = f.readline().split() return tuple(map(float, line)) + def set_and_compute_limites(reach, ind, z, q, s): + reach.set(0, timestamp, "Z", z) + reach.set(0, timestamp, "Q", q) + reach.set(0, timestamp, "V", s) + + profile = reach.profile(ind) + limits = profile.geometry.get_water_limits(z) + reach.set( + ind, timestamp, "water_limits", limits + ) + ts = set() end = False while True: @@ -565,31 +576,33 @@ class Rubar3(CommandLineSolver): return timestamp = float(line) + ts.add(timestamp) for reach, lm in reachs: # First profile z, s, q, e = read_data_line(f) - reach.set(0, timestamp, "Z", z) - reach.set(0, timestamp, "Q", q) - reach.set(0, timestamp, "V", s) + set_and_compute_limites(reach, 0, z, q, s) # For each profile ind = 1 pz, ps, pq, pe = read_data_line(f) while ind < lm - 2: z, s, q, e = read_data_line(f) - reach.set(ind, timestamp, "Z", (z + pz) / 2) - reach.set(ind, timestamp, "Q", (q + pq) / 2) - reach.set(ind, timestamp, "V", (q + ps) / 2) + set_and_compute_limites( + reach, ind, + (pz + z) / 2, + (pq + q) / 2, + (ps + s) / 2 + ) pz, ps, pq, pe = z, s, q, e ind += 1 # Last profile z, s, q, e = read_data_line(f) - reach.set(ind, timestamp, "Z", z) - reach.set(ind, timestamp, "Q", q) - reach.set(ind, timestamp, "V", s) + set_and_compute_limites(reach, ind, z, q, s) + + results.set("timestamps", ts) @timer def results(self, study, repertory, qlog=None, name="0"): From e640fa1bc0ba1d6a3e1c457cce7303f74864d3ac Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby Date: Fri, 10 Oct 2025 16:41:12 +0200 Subject: [PATCH 3/3] Solver: RubarBE: Fix results reading. --- src/Solver/RubarBE.py | 21 +++++++++++---------- src/View/MainWindow.py | 19 ++++++++++++++++--- src/View/Results/Table.py | 3 ++- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/Solver/RubarBE.py b/src/Solver/RubarBE.py index ff7636e5..98ee5c53 100644 --- a/src/Solver/RubarBE.py +++ b/src/Solver/RubarBE.py @@ -558,9 +558,9 @@ class Rubar3(CommandLineSolver): return tuple(map(float, line)) def set_and_compute_limites(reach, ind, z, q, s): - reach.set(0, timestamp, "Z", z) - reach.set(0, timestamp, "Q", q) - reach.set(0, timestamp, "V", s) + reach.set(ind, timestamp, "Z", z) + reach.set(ind, timestamp, "Q", q) + reach.set(ind, timestamp, "V", s) profile = reach.profile(ind) limits = profile.geometry.get_water_limits(z) @@ -573,21 +573,24 @@ class Rubar3(CommandLineSolver): while True: line = f.readline() if line == "": + results.set("timestamps", ts) return timestamp = float(line) ts.add(timestamp) + logger.info(f"read: profil.{name}: timestamp = {timestamp}") + for reach, lm in reachs: # First profile - z, s, q, e = read_data_line(f) + e, s, q, z = read_data_line(f) set_and_compute_limites(reach, 0, z, q, s) # For each profile ind = 1 - pz, ps, pq, pe = read_data_line(f) + pe, ps, pq, pz = read_data_line(f) while ind < lm - 2: - z, s, q, e = read_data_line(f) + e, s, q, z = read_data_line(f) set_and_compute_limites( reach, ind, (pz + z) / 2, @@ -595,15 +598,13 @@ class Rubar3(CommandLineSolver): (ps + s) / 2 ) - pz, ps, pq, pe = z, s, q, e + pe, ps, pq, pz = z, s, q, e ind += 1 # Last profile - z, s, q, e = read_data_line(f) + e, s, q, z = read_data_line(f) set_and_compute_limites(reach, ind, z, q, s) - results.set("timestamps", ts) - @timer def results(self, study, repertory, qlog=None, name="0"): results = Results( diff --git a/src/View/MainWindow.py b/src/View/MainWindow.py index 2972e3ce..0821cd62 100644 --- a/src/View/MainWindow.py +++ b/src/View/MainWindow.py @@ -31,6 +31,7 @@ from platformdirs import user_cache_dir from Solver.AdisTS import AdisTS from Solver.Mage import Mage8 +from Solver.RubarBE import Rubar3 from tools import logger_exception, pamhyr_db_need_update from PyQt5 import QtGui @@ -1506,6 +1507,8 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit): return False def open_solver_results(self, solver, results=None): + logger.info(f"{solver} {results}") + def reading_fn(): self._tmp_results = results @@ -1525,7 +1528,10 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit): if type(results) is str: logger.info(f"Open results from {os.path.dirname(results)}") - name = os.path.basename(results).replace(".BIN", "") + if ".BIN" in results: + name = os.path.basename(results).replace(".BIN", "") + elif "profil." in results: + name = os.path.basename(results).replace("profil.", "") def reading_fn(): self._tmp_results = solver.results( @@ -1664,7 +1670,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit): dialog.setFileMode(QFileDialog.FileMode.ExistingFile) dialog.setDefaultSuffix(".BIN") # dialog.setFilter(dialog.filter() | QtCore.QDir.Hidden) - dialog.setNameFilters(['Mage (*.BIN)']) + dialog.setNameFilters(['Mage (*.BIN)', 'Rubar3 (profil.*)']) if self._study.filename is not None: if self._last_solver is None: @@ -1679,8 +1685,15 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit): if dialog.exec_(): file_name = dialog.selectedFiles() logger.info(f"Select results: {file_name}") + + solver = None + if ".BIN" in file_name: + solver = Mage8("Mage8") + else: + solver = Rubar3("Rubar3") + self.open_solver_results( - Mage8("Mage"), + solver, results=file_name[0] ) diff --git a/src/View/Results/Table.py b/src/View/Results/Table.py index 1b30de26..b67907d2 100644 --- a/src/View/Results/Table.py +++ b/src/View/Results/Table.py @@ -63,7 +63,8 @@ class TableModel(PamhyrTableModel): self._lst = self._parent._solvers def __init__(self, **kwargs): - self._timestamp = 0.0 + self._timestamp = max(kwargs["parent"]._timestamps) + super(TableModel, self).__init__(**kwargs) def data(self, index, role=Qt.DisplayRole):