Skip to content

Commit

Permalink
Bug fix hand analyze parameters to get_most_critical_time_steps
Browse files Browse the repository at this point in the history
  • Loading branch information
birgits committed Oct 22, 2023
1 parent 0f15fab commit b17d714
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 16 deletions.
22 changes: 13 additions & 9 deletions edisgo/flex_opt/reinforce_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,6 @@ def reinforce_grid(
f"Input {timesteps_pfa} for timesteps_pfa is not valid."
)

if reduced_analysis:
timesteps_pfa = get_most_critical_time_steps(
edisgo,
timesteps=timesteps_pfa,
num_steps_loading=kwargs.get("num_steps_loading", None),
num_steps_voltage=kwargs.get("num_steps_voltage", None),
percentage=kwargs.get("percentage", 1.0),
use_troubleshooting_mode=kwargs.get("use_troubleshooting_mode", True),
)
iteration_step = 1
lv_grid_id = kwargs.get("lv_grid_id", None)
scale_timeseries = kwargs.get("scale_timeseries", None)
Expand All @@ -176,6 +167,19 @@ def reinforce_grid(
else:
analyze_mode = mode

if reduced_analysis:
timesteps_pfa = get_most_critical_time_steps(
edisgo,
mode=analyze_mode,
timesteps=timesteps_pfa,
lv_grid_id=lv_grid_id,
scale_timeseries=scale_timeseries,
num_steps_loading=kwargs.get("num_steps_loading", None),
num_steps_voltage=kwargs.get("num_steps_voltage", None),
percentage=kwargs.get("percentage", 1.0),
use_troubleshooting_mode=kwargs.get("use_troubleshooting_mode", True),
)

edisgo.analyze(
mode=analyze_mode,
timesteps=timesteps_pfa,
Expand Down
57 changes: 50 additions & 7 deletions edisgo/tools/temporal_complexity_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,13 @@ def _scored_most_critical_voltage_issues_time_interval(
return time_intervals_df


def _troubleshooting_mode(edisgo_obj, timesteps=None):
def _troubleshooting_mode(
edisgo_obj,
mode=None,
timesteps=None,
lv_grid_id=None,
scale_timeseries=None,
):
"""
Handles non-convergence issues in power flow by iteratively reducing load and
feed-in until the power flow converges.
Expand All @@ -409,16 +415,31 @@ def _troubleshooting_mode(edisgo_obj, timesteps=None):
-----------
edisgo_obj : :class:`~.EDisGo`
The eDisGo API object
mode : str or None
Allows to toggle between power flow analysis for the whole network or just
the MV or one LV grid. See parameter `mode` in function
:attr:`~.EDisGo.analyze` for more information.
timesteps : :pandas:`pandas.DatetimeIndex<DatetimeIndex>` or \
:pandas:`pandas.Timestamp<Timestamp>`
Timesteps specifies from which time steps to select most critical ones. It
defaults to None in which case all time steps in
:attr:`~.network.timeseries.TimeSeries.timeindex` are used.
lv_grid_id : int or str
ID (e.g. 1) or name (string representation, e.g. "LVGrid_1") of LV grid
to analyze in case mode is 'lv'. Default: None.
scale_timeseries : float or None
See parameter `scale_timeseries` in function :attr:`~.EDisGo.analyze` for more
information.
"""
try:
logger.debug("Running initial power flow for temporal complexity reduction.")
edisgo_obj.analyze(timesteps=timesteps)
edisgo_obj.analyze(
mode=mode,
timesteps=timesteps,
lv_grid_id=lv_grid_id,
scale_timeseries=scale_timeseries,
)
# Exception is used, as non-convergence can also lead to RuntimeError, not only
# ValueError
except Exception:
Expand All @@ -429,13 +450,17 @@ def _troubleshooting_mode(edisgo_obj, timesteps=None):
"not all time steps converged. Power flow is run again with reduced "
"network load."
)
for fraction in np.arange(0.8, 0.0, step=-0.1):
if isinstance(scale_timeseries, float):
iter_start = scale_timeseries - 0.1
else:
iter_start = 0.8
for fraction in np.arange(iter_start, 0.0, step=-0.1):
try:
edisgo_obj.analyze(
mode=mode,
timesteps=timesteps,
troubleshooting_mode="iteration",
range_start=fraction,
range_num=1,
lv_grid_id=lv_grid_id,
scale_timeseries=fraction,
)
logger.info(
f"Power flow fully converged for a reduction factor "
Expand Down Expand Up @@ -627,7 +652,10 @@ def get_most_critical_time_intervals(

def get_most_critical_time_steps(
edisgo_obj: EDisGo,
mode=None,
timesteps=None,
lv_grid_id=None,
scale_timeseries=None,
num_steps_loading=None,
num_steps_voltage=None,
percentage: float = 1.0,
Expand All @@ -640,11 +668,21 @@ def get_most_critical_time_steps(
-----------
edisgo_obj : :class:`~.EDisGo`
The eDisGo API object
mode : str or None
Allows to toggle between power flow analysis for the whole network or just
the MV or one LV grid. See parameter `mode` in function
:attr:`~.EDisGo.analyze` for more information.
timesteps : :pandas:`pandas.DatetimeIndex<DatetimeIndex>` or \
:pandas:`pandas.Timestamp<Timestamp>`
Timesteps specifies from which time steps to select most critical ones. It
defaults to None in which case all time steps in
:attr:`~.network.timeseries.TimeSeries.timeindex` are used.
lv_grid_id : int or str
ID (e.g. 1) or name (string representation, e.g. "LVGrid_1") of LV grid
to analyze in case mode is 'lv'. Default: None.
scale_timeseries : float or None
See parameter `scale_timeseries` in function :attr:`~.EDisGo.analyze` for more
information.
num_steps_loading : int
The number of most critical overloading events to select. If None, `percentage`
is used. Default: None.
Expand Down Expand Up @@ -675,7 +713,12 @@ def get_most_critical_time_steps(
edisgo_obj = _troubleshooting_mode(edisgo_obj, timesteps=timesteps)
else:
logger.debug("Running initial power flow for temporal complexity reduction.")
edisgo_obj.analyze(timesteps=timesteps)
edisgo_obj.analyze(
mode=mode,
timesteps=timesteps,
lv_grid_id=lv_grid_id,
scale_timeseries=scale_timeseries,
)

# Select most critical steps based on current violations
loading_scores = _scored_most_critical_loading(edisgo_obj)
Expand Down

0 comments on commit b17d714

Please sign in to comment.