depot.api.private.shrink

Shrink Areas and electrified terminus Stations to peak observed concurrency.

Internal helpers for eflips.depot.api.shrink_to_peak_usage(). Operates on events already persisted to the database (run after eflips.depot.api.add_evaluation_to_database()).

Functions

_compute_peak_concurrency(events, resolution)

Count the maximum number of simultaneously-active events.

_round_capacity_for_area_type(peak, area)

Round peak UP to the next valid capacity for area.area_type.

_shrink_areas_to_peak(scenario, session, resolution)

Shrink every Area in scenario to its peak observed usage.

_shrink_stations_to_peak(scenario, session, resolution)

Shrink electrified opportunity-charging Stations to peak observed usage.

Module Contents

depot.api.private.shrink._compute_peak_concurrency(events, resolution)

Count the maximum number of simultaneously-active events.

Each event occupies time blocks [floor(start/res), max(start+1, ceil(end/res))) relative to the earliest start. Back-to-back events at a block boundary do not overlap; sub-resolution events still count as one block.

Parameters:
  • events (Iterable[eflips.model.Event])

  • resolution (datetime.timedelta)

Return type:

int

depot.api.private.shrink._round_capacity_for_area_type(peak, area)

Round peak UP to the next valid capacity for area.area_type.

Preserves the CHECK constraint defined on eflips.model.Area:

  • DIRECT_ONESIDE: any positive int

  • DIRECT_TWOSIDE: capacity must be even

  • LINE: capacity must be a multiple of row_count (which is preserved)

Parameters:
  • peak (int)

  • area (eflips.model.Area)

Return type:

int

depot.api.private.shrink._shrink_areas_to_peak(scenario, session, resolution)

Shrink every Area in scenario to its peak observed usage.

Areas whose peak concurrency is zero are deleted along with their AssocAreaProcess rows. If we somehow compute a zero peak while events still reference the area we raise RuntimeError rather than silently delete persisted state.

Parameters:
  • scenario (eflips.model.Scenario)

  • session (sqlalchemy.orm.Session)

  • resolution (datetime.timedelta)

Return type:

None

depot.api.private.shrink._shrink_stations_to_peak(scenario, session, resolution)

Shrink electrified opportunity-charging Stations to peak observed usage.

Only Stations with is_electrified=True and charge_type=ChargeType.OPPORTUNITY are considered. If peak > 0 we update amount_charging_places and recompute power_total. If peak == 0 we un-electrify the station, atomically nulling all electrification fields to keep the CHECK constraint satisfied.

Parameters:
  • scenario (eflips.model.Scenario)

  • session (sqlalchemy.orm.Session)

  • resolution (datetime.timedelta)

Return type:

None