From 9699c967ba709d9ceb1e6d5220f5b073bf14ebaf Mon Sep 17 00:00:00 2001 From: Ngo Nghi Truyen Huynh <129378719+nghi-truyen@users.noreply.github.com> Date: Mon, 9 Sep 2024 11:28:31 +0200 Subject: [PATCH] FIX: fix baseline and add minor corrections to previous PRs (#308) --- smash/core/simulation/_doc.py | 2 +- smash/core/simulation/optimize/optimize.py | 3 +-- smash/factory/mesh/mesh.py | 26 +++++++++++++--------- smash/factory/mesh/mw_mesh.f90 | 4 ++-- smash/tests/generate_baseline.py | 3 +-- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/smash/core/simulation/_doc.py b/smash/core/simulation/_doc.py index de3d6e64..0498c20d 100644 --- a/smash/core/simulation/_doc.py +++ b/smash/core/simulation/_doc.py @@ -25,7 +25,7 @@ - `Model.rr_parameters` - `Model.rr_initial_states` - - `Model.nn_parameters`, if using a hybrid structure model (depending on **hydrological_module**) + - `Model.nn_parameters`, if using a hybrid model structure (depending on **hydrological_module**) %(parameters_serr_mu_parameters)s %(parameters_serr_sigma_parameters)s diff --git a/smash/core/simulation/optimize/optimize.py b/smash/core/simulation/optimize/optimize.py index f05e91bb..be73ba7c 100644 --- a/smash/core/simulation/optimize/optimize.py +++ b/smash/core/simulation/optimize/optimize.py @@ -954,8 +954,7 @@ def _sbs_optimize( ret["iter_cost"] = np.array([gx]) for iter in range(1, wrap_options.optimize.maxiter * n + 1): - if dxn > ddx: - dxn = ddx + dxn = min(dxn, ddx) if ddx > 2: ddx = dxn diff --git a/smash/factory/mesh/mesh.py b/smash/factory/mesh/mesh.py index 4aa60182..4edf0d01 100644 --- a/smash/factory/mesh/mesh.py +++ b/smash/factory/mesh/mesh.py @@ -1,5 +1,6 @@ from __future__ import annotations +import warnings from typing import TYPE_CHECKING import numpy as np @@ -99,10 +100,10 @@ def generate_mesh( (i.e. in ``ASCII`` file). The **epsg** argument must be filled in. check_well: `bool`, default True - If True, check the consistency of the flow directions. If some well(s) are detected, - the function will print an error and return a dictionnary with all necessary informations - to identify the well(s). If False, this check is disabled. Notice that the presence of well(s) - could lead to unexpected behaviours such as crashs and inconsistent hydrological results. + Whether to check the consistency of the flow directions. If any wells are detected, the function + will raise a warning and return a dictionary with all necessary information to identify the well(s). + If False, this check is disabled. Note that the presence of wells could lead to unexpected behaviors, + such as crashes or inconsistent hydrological results. Returns ------- @@ -429,13 +430,13 @@ def _generate_mesh_from_bbox(flwdir_dataset: rasterio.DatasetReader, bbox: np.nd return mesh -def _check_well_in_flowdir( +def _check_well_in_flwdir( flwdir_dataset: rasterio.DatasetReader, ): (xmin, _, xres, _, ymax, yres) = _get_transform(flwdir_dataset) flwdir = _get_array(flwdir_dataset) - well = mw_mesh.check_well_in_flowdir(flwdir) + well = mw_mesh.check_well_in_flwdir(flwdir) well_coord_x = xmin + np.where(well > 0)[0] * xres well_coord_y = ymax - np.where(well > 0)[1] * yres @@ -463,15 +464,20 @@ def _generate_mesh( check_well: bool, ) -> dict: if check_well: - print(r"<\> Checking the consistency of the flow directions ...") - well = _check_well_in_flowdir(flwdir_dataset) + print(" Checking the consistency of the flow directions") + well = _check_well_in_flwdir(flwdir_dataset) if np.sum(well["well"]) != 0: - print(r"<\> Error: Well(s) detected in the flow directions.") + warnings.warn( + "Well(s) detected in the flow directions may lead to unexpected hydrological behaviors", + stacklevel=2, + ) + flwdir_dataset.close() + return well - print(r"<\> Generate the mesh ...") + print(" Generating mesh") if bbox is not None: return _generate_mesh_from_bbox(flwdir_dataset, bbox, epsg) diff --git a/smash/factory/mesh/mw_mesh.f90 b/smash/factory/mesh/mw_mesh.f90 index 8444c838..a214a247 100644 --- a/smash/factory/mesh/mw_mesh.f90 +++ b/smash/factory/mesh/mw_mesh.f90 @@ -470,7 +470,7 @@ subroutine flow_partition_variable(nrow, ncol, npar, flwpar, ncpar, cscpar, cpar end subroutine flow_partition_variable - subroutine check_well_in_flowdir(nrow, ncol, flwdir, well) + subroutine check_well_in_flwdir(nrow, ncol, flwdir, well) implicit none @@ -507,6 +507,6 @@ subroutine check_well_in_flowdir(nrow, ncol, flwdir, well) end do - end subroutine check_well_in_flowdir + end subroutine check_well_in_flwdir end module mw_mesh diff --git a/smash/tests/generate_baseline.py b/smash/tests/generate_baseline.py index a7c7b7b0..93b204c4 100644 --- a/smash/tests/generate_baseline.py +++ b/smash/tests/generate_baseline.py @@ -93,8 +93,7 @@ def compare_baseline(f: h5py.File, new_f: h5py.File): status = [] for key in all_keys: - if len(key) > max_len_name: - max_len_name = len(key) + max_len_name = max(len(key), max_len_name) test_name.append(key)