Skip to content

grat.model.bottlenecks

Delivery limits shared by a group of sites.

Bottleneck

Bottleneck(
    bottleneck_id: int,
    name: str | None = None,
    capacity: float | None = None,
    wafr_sources: list | None = None,
    capacity_irrigation: float | None = None,
    irrigation_days: list | None = None,
    **kwargs,
)

A daily delivery limit shared by every site behind it.

A bottleneck caps how much water can reach the sites that name it on a given day, no matter how much WAFR is available. Each bottleneck is associated with one or more WAFR sources and only limits water from those.

Sites name the bottlenecks that restrict them via their bottleneck_ids. A site behind several bottlenecks is limited by the tightest of them.

Capacity is tracked as a running daily budget: available_throughput starts at the day's capacity and is drawn down by update_usage as sites take water.

Attributes:

Name Type Description
available_throughput

throughput still unused (acre-feet), one entry per day of the water year. Drawn down as sites take water, and may go negative.

Build a bottleneck.

Parameters:

Name Type Description Default
bottleneck_id int

bottleneck identifier, matched against a site's bottleneck_ids.

required
name str | None

human-readable name of the bottleneck.

None
capacity float | None

throughput limit outside the irrigation season (acre-feet per day).

None
wafr_sources list | None

names of the WAFR sources this bottleneck restricts. Water from any other source passes unrestricted.

None
capacity_irrigation float | None

throughput limit during the irrigation season (acre-feet per day).

None
irrigation_days list | None

zero-indexed days of the water year that use capacity_irrigation instead of capacity.

None
**kwargs

additional attributes, set directly on the instance.

{}
Source code in src/grat/model/bottlenecks.py
def __init__(
    self,
    bottleneck_id: int,
    name: str | None = None,
    capacity: float | None = None,
    wafr_sources: list | None = None,
    capacity_irrigation: float | None = None,
    irrigation_days: list | None = None,
    **kwargs,
):
    """Build a bottleneck.

    Args:
        bottleneck_id: bottleneck identifier, matched against a site's
            `bottleneck_ids`.
        name: human-readable name of the bottleneck.
        capacity: throughput limit outside the irrigation season
            (acre-feet per day).
        wafr_sources: names of the WAFR sources this bottleneck restricts. Water
            from any other source passes unrestricted.
        capacity_irrigation: throughput limit during the irrigation season
            (acre-feet per day).
        irrigation_days: zero-indexed days of the water year that use
            `capacity_irrigation` instead of `capacity`.
        **kwargs: additional attributes, set directly on the instance.
    """
    # catch all extra kwargs provided to class instance
    self.__dict__.update(kwargs)  # catch all for all unknown kwargs

    self.bottleneck_id = bottleneck_id
    self.name = name
    self.wafr_sources = wafr_sources if wafr_sources is not None else []  # names of the WAFR sources
    self.capacity = capacity
    self.capacity_irrigation = capacity_irrigation
    self.available_throughput = self._calculate_throughput(
        irrigation_days if irrigation_days is not None else []
    )  # volume in acre-feet per day

update_usage

update_usage(day: int, amount: float)

Charge a volume against the bottleneck's remaining throughput for a day.

Parameters:

Name Type Description Default
day int

zero-indexed day of the water year.

required
amount float

volume delivered through the bottleneck (acre-feet).

required
Source code in src/grat/model/bottlenecks.py
def update_usage(self, day: int, amount: float):
    """Charge a volume against the bottleneck's remaining throughput for a day.

    Args:
        day: zero-indexed day of the water year.
        amount: volume delivered through the bottleneck (acre-feet).
    """
    self.available_throughput[day] -= amount
    return

get_available_throughput

get_available_throughput(day: int)

Return the volume that can still pass through the bottleneck on a day.

Parameters:

Name Type Description Default
day int

zero-indexed day of the water year.

required

Returns:

Type Description

Remaining throughput (acre-feet). Clamped to zero, so an

over-subscribed bottleneck reports no capacity rather than a

negative one.

Source code in src/grat/model/bottlenecks.py
def get_available_throughput(self, day: int):
    """Return the volume that can still pass through the bottleneck on a day.

    Args:
        day: zero-indexed day of the water year.

    Returns:
        Remaining throughput (acre-feet). Clamped to zero, so an
        over-subscribed bottleneck reports no capacity rather than a
        negative one.
    """
    daily_throughput = self.available_throughput[day]
    if daily_throughput <= 0:
        return 0
    return daily_throughput

get_bottlenecks

get_bottlenecks(
    bottleneck_list: list, bottleneck_ids, wafr_source: str
)

Select the bottlenecks that restrict a given site's delivery from a source.

A bottleneck matches when its identifier is one the site names and it restricts the WAFR source in question.

Parameters:

Name Type Description Default
bottleneck_list list

every bottleneck in the system.

required
bottleneck_ids

the identifiers named by the site. None means the site is behind no bottlenecks.

required
wafr_source str

name of the WAFR source supplying the water.

required

Returns:

Type Description

The matching bottlenecks, empty if the site is unrestricted for this source.

Source code in src/grat/model/bottlenecks.py
def get_bottlenecks(bottleneck_list: list, bottleneck_ids, wafr_source: str):
    """Select the bottlenecks that restrict a given site's delivery from a source.

    A bottleneck matches when its identifier is one the site names *and* it
    restricts the WAFR source in question.

    Args:
        bottleneck_list: every bottleneck in the system.
        bottleneck_ids: the identifiers named by the site. `None` means the site is
            behind no bottlenecks.
        wafr_source: name of the WAFR source supplying the water.

    Returns:
        The matching bottlenecks, empty if the site is unrestricted for this source.
    """
    if bottleneck_ids is None:
        return []
    return [b for b in bottleneck_list if b.bottleneck_id in bottleneck_ids and wafr_source in b.wafr_sources]

bottleneck_capacity_logger

bottleneck_capacity_logger(bottleneck_list: list)

Report which bottlenecks ran out of capacity, day by day.

A bottleneck counts as having hit its limit on any day where its remaining throughput has fallen to zero or below.

Parameters:

Name Type Description Default
bottleneck_list list

every bottleneck in the system.

required

Returns:

Type Description

One list per day of the water year, each holding the identifiers of the

bottlenecks that hit their limit that day.

Source code in src/grat/model/bottlenecks.py
def bottleneck_capacity_logger(bottleneck_list: list):
    """Report which bottlenecks ran out of capacity, day by day.

    A bottleneck counts as having hit its limit on any day where its remaining
    throughput has fallen to zero or below.

    Args:
        bottleneck_list: every bottleneck in the system.

    Returns:
        One list per day of the water year, each holding the identifiers of the
        bottlenecks that hit their limit that day.
    """
    capacity_hit_log = [[] for _ in range(DAYS_PER_WATER_YEAR)]  # assumes multiple bottlenecks per day

    for b in bottleneck_list:
        # use list comprehension to get index positions where available_throughput is less than zero?
        hit_limit = [i for i, e in enumerate(b.available_throughput) if e <= 0]
        for h in hit_limit:
            capacity_hit_log[h].append(b.bottleneck_id)

    return capacity_hit_log