From 43552cfd726e3f714fa26784f08dc09b72328093 Mon Sep 17 00:00:00 2001 From: birgits Date: Sun, 22 Oct 2023 14:54:54 +0200 Subject: [PATCH] Change LV reniforcement --- edisgo/flex_opt/reinforce_grid.py | 67 ++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/edisgo/flex_opt/reinforce_grid.py b/edisgo/flex_opt/reinforce_grid.py index f7ff6513..7f4fe80d 100644 --- a/edisgo/flex_opt/reinforce_grid.py +++ b/edisgo/flex_opt/reinforce_grid.py @@ -883,7 +883,7 @@ def enhanced_reinforce_grid( logger.info("Run initial grid reinforcement for single LV grids.") for lv_grid in list(edisgo_object.topology.mv_grid.lv_grids): logger.info(f"Check initial convergence for {lv_grid=}.") - _, ts_not_converged = edisgo_object.analyze( + ts_converged, ts_not_converged = edisgo_object.analyze( mode="lv", raise_not_converged=False, lv_grid_id=lv_grid.id ) if len(ts_not_converged) > 0: @@ -902,12 +902,31 @@ def enhanced_reinforce_grid( ) try: logger.info(f"Try initial mode 'lv' reinforcement for {lv_grid=}.") - edisgo_object.reinforce( - mode="lv", - lv_grid_id=lv_grid.id, - catch_convergence_problems=True, - **kwargs, - ) + if len(ts_not_converged) > 0: + # if there are time steps that did not converge, run reinforcement + # first on converged time steps + edisgo_object.reinforce( + mode="lv", + lv_grid_id=lv_grid.id, + catch_convergence_problems=False, + timesteps_pfa=ts_converged, + **kwargs, + ) + # run reinforcement again in catch-convergence mode with all time steps + edisgo_object.reinforce( + mode="lv", + lv_grid_id=lv_grid.id, + catch_convergence_problems=True, + **kwargs, + ) + else: + # if all time steps converged, run normal reinforcement + edisgo_object.reinforce( + mode="lv", + lv_grid_id=lv_grid.id, + catch_convergence_problems=False, + **kwargs, + ) logger.info(f"Initial mode 'lv' reinforcement for {lv_grid} successful.") except (ValueError, RuntimeError, exceptions.MaximumIterationError): logger.warning(f"Initial mode 'lv' reinforcement for {lv_grid} failed.") @@ -940,7 +959,7 @@ def enhanced_reinforce_grid( for lv_grid in list(edisgo_object.topology.mv_grid.lv_grids): logger.info(f"Check convergence for {lv_grid=}.") - _, ts_not_converged = edisgo_object.analyze( + ts_converged, ts_not_converged = edisgo_object.analyze( mode="lv", raise_not_converged=False, lv_grid_id=lv_grid.id ) if len(ts_not_converged) > 0: @@ -961,12 +980,32 @@ def enhanced_reinforce_grid( ) try: logger.info(f"Try mode 'lv' reinforcement for {lv_grid=}.") - edisgo_object.reinforce( - mode="lv", - lv_grid_id=lv_grid.id, - catch_convergence_problems=True, - **kwargs, - ) + if len(ts_not_converged) > 0: + # if there are time steps that did not converge, run reinforcement + # first on converged time steps + edisgo_object.reinforce( + mode="lv", + lv_grid_id=lv_grid.id, + catch_convergence_problems=False, + timesteps_pfa=ts_converged, + **kwargs, + ) + # run reinforcement again in catch-convergence mode with all time + # steps + edisgo_object.reinforce( + mode="lv", + lv_grid_id=lv_grid.id, + catch_convergence_problems=True, + **kwargs, + ) + else: + # if all time steps converged, run normal reinforcement + edisgo_object.reinforce( + mode="lv", + lv_grid_id=lv_grid.id, + catch_convergence_problems=False, + **kwargs, + ) logger.info(f"Mode 'lv' reinforcement for {lv_grid} successful.") except (ValueError, RuntimeError, exceptions.MaximumIterationError): logger.info(f"Mode 'lv' reinforcement for {lv_grid} failed.")