Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
and producer formulas. `ComponentGraph.consumer_formula()` and
`ComponentGraph.producer_formula()` can now return values previously pinned
to zero: a negative consumer total, or a positive producer total.
- `PVConfig`, `WindConfig` and `BatteryConfig` take keyword arguments only.

## New Features

- `generate-config` now derives a `generation` component type, with its
`AC_POWER_ACTIVE` formula taken from `ComponentGraph.producer_formula()`, so
the aggregated producer power is emitted alongside `consumption`.
- `BatteryConfig` gains an optional `rated_power`: the inverters' rated power in Watt.
- `MicrogridConfig.assets(asset_type)` returns the PV, wind or battery entries of a microgrid.
- `AssetsConfig.rated_power(asset_types, at=..., microgrid_id=...)` returns the summed rated power bounds of a microgrid's PV, wind or battery assets installed at `at`, or `None` if the microgrid is unknown or a rating is missing. `AssetType` names the asset types.

## Bug Fixes

Expand Down
2 changes: 2 additions & 0 deletions src/frequenz/gridpool/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ._gridpool import GridpoolConfig
from ._load import load_configs
from ._microgrid import (
AssetType,
BatteryConfig,
ComponentCategory,
ComponentType,
Expand All @@ -25,6 +26,7 @@
)

__all__ = [
"AssetType",
"AssetsConfig",
"BatteryConfig",
"ComponentCategory",
Expand Down
47 changes: 46 additions & 1 deletion src/frequenz/gridpool/config/_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@

import logging
import tomllib
from collections.abc import Iterable
from dataclasses import field
from datetime import datetime
from pathlib import Path
from typing import Any, ClassVar, Self, Type

import marshmallow
from frequenz.client.assets import MarketParticipationType
from frequenz.client.assets.metrics import Bounds
from marshmallow import Schema
from marshmallow_dataclass import dataclass

from ._gridpool import GridpoolConfig
from ._microgrid import MicrogridConfig
from ._microgrid import AssetType, MicrogridConfig
from ._migrations import _CURRENT_VERSION, migrate
from ._topology import DeliveryAreaConfig, MarketLocationConfig, RelationConfig

Expand Down Expand Up @@ -133,6 +135,49 @@ def __post_init__(self) -> None:
f"Gridpool ID mismatch: key {gpid} != {gridpool.gridpool_id}"
)

def rated_power(
self,
asset_types: AssetType | Iterable[AssetType],
*,
at: datetime,
microgrid_id: int,
) -> Bounds | None:
"""Get the rated power bounds of a microgrid's assets of the given types.

Bounds follow the passive sign convention: consumption is positive. PV
and wind span `[-rated, 0]`, batteries `[-rated, rated]`. Wind
ratings count once per turbine. Only assets installed at `at` count.

Args:
asset_types: The asset type or types to sum over.
at: The instant the assets must be installed at.
microgrid_id: The microgrid whose assets to sum.

Returns:
The summed bounds, zero if there are no such assets, or `None` if
the microgrid is unknown, an installed asset has no rating, or
a type has a `ctype` entry but no assets.
"""
microgrid = self.microgrids.get(microgrid_id)
if microgrid is None:
return None
types = [asset_types] if isinstance(asset_types, str) else asset_types
lower = upper = 0.0
for asset_type in types:
assets = microgrid.assets(asset_type)
if not assets and asset_type in microgrid.ctype:
return None
for asset in assets.values():
if not asset.is_installed(at):
continue
rated = asset.total_rated_power
if rated is None:
return None
lower -= rated
if asset_type == "battery":
upper += rated
return Bounds(lower=lower, upper=upper)

def check(self) -> None:
"""Check the document as a whole, once every layer has been merged.

Expand Down
107 changes: 82 additions & 25 deletions src/frequenz/gridpool/config/_microgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@
"""Data model for microgrid configurations."""

import re
from dataclasses import field
from collections.abc import Mapping
from dataclasses import KW_ONLY, field
from datetime import datetime
from typing import Any, ClassVar, Literal, Self, Type, cast, get_args
from typing import (
Any,
ClassVar,
Literal,
Self,
Type,
assert_never,
cast,
get_args,
)

from marshmallow import Schema
from marshmallow_dataclass import dataclass
Expand All @@ -21,6 +31,9 @@
ComponentCategory = Literal["meter", "inverter", "component"]
"""Valid component categories."""

AssetType = Literal["pv", "wind", "battery"]
"""Asset types that carry a rated power."""


@dataclass
class ComponentTypeConfig:
Expand Down Expand Up @@ -99,47 +112,67 @@ def is_valid_type(cls, ctype: str) -> bool:


@dataclass(frozen=True)
class PVConfig:
"""Configuration of a PV system in a microgrid."""
class _AssetBase:
"""Installation period and rating shared by all asset configurations."""

_: KW_ONLY

start_time: datetime | None = field(default=None, metadata=toml_datetime_metadata())
"""Start time of the PV system installation."""
"""Start time of the installation."""

end_time: datetime | None = field(default=None, metadata=toml_datetime_metadata())
"""End time of the PV system installation."""
"""End time of the installation, exclusive."""

rated_power: float | None = None
"""Rated AC power in Watt."""

@property
def total_rated_power(self) -> float | None:
"""The rated power of the whole asset in Watt, `None` if unrated."""
return self.rated_power

def is_installed(self, at: datetime) -> bool:
"""Check whether the asset is installed at `at`.

Args:
at: The instant to check.

Returns:
Whether `at` lies in the installation period.
"""
if self.start_time is not None and at < self.start_time:
return False
return self.end_time is None or at < self.end_time


@dataclass(frozen=True)
class PVConfig(_AssetBase):
"""Configuration of a PV system in a microgrid."""

_: KW_ONLY

peak_power: float | None = None
"""Peak power of the PV system in Watt."""

rated_power: float | None = None
"""Rated power of the inverters in Watt."""

curtailable: bool | None = None
"""Flag to indicate if PV system can be curtailed."""


@dataclass(frozen=True)
class WindConfig:
class WindConfig(_AssetBase):
# pylint: disable=too-many-instance-attributes
"""Configuration of a wind turbine in a microgrid."""

start_time: datetime | None = field(default=None, metadata=toml_datetime_metadata())
"""Start time of the wind turbine installation."""

end_time: datetime | None = field(default=None, metadata=toml_datetime_metadata())
"""End time of the wind turbine installation."""
_: KW_ONLY

turbine_model: str | None = None
"""Model name of the wind turbine."""

rated_power: float | None = None
"""Rated power of the wind turbine in Watt."""

turbine_height: float | None = None
"""Height of the wind turbine in meters."""

number_of_turbines: int = 1
"""Number of wind turbines."""
"""Number of wind turbines, each rated at `rated_power`."""

hellmann_exponent: float | None = None
"""Hellmann exponent for wind speed extrapolation. See: https://w.wiki/FMw9"""
Expand All @@ -150,16 +183,19 @@ class WindConfig:
latitude: float | None = None
"""Geographic latitude of the wind turbine."""

@property
def total_rated_power(self) -> float | None:
"""The rated power of all turbines in Watt, `None` if unrated."""
if self.rated_power is None:
return None
return self.rated_power * self.number_of_turbines


@dataclass(frozen=True)
class BatteryConfig:
class BatteryConfig(_AssetBase):
"""Configuration of a battery in a microgrid."""

start_time: datetime | None = field(default=None, metadata=toml_datetime_metadata())
"""Start time of the battery installation."""

end_time: datetime | None = field(default=None, metadata=toml_datetime_metadata())
"""End time of the battery installation."""
_: KW_ONLY

capacity: float | None = None
"""Capacity of the battery in Wh."""
Expand Down Expand Up @@ -209,6 +245,27 @@ class MicrogridConfig:
ctype: dict[str, ComponentTypeConfig] = field(default_factory=dict)
"""Mapping of component category types to ac power component config."""

def assets(
self, asset_type: AssetType
) -> Mapping[str, PVConfig | WindConfig | BatteryConfig]:
"""Get the asset entries of one type.

Args:
asset_type: The asset type.

Returns:
The entries by label, empty if there are none.
"""
match asset_type:
case "pv":
return self.pv or {}
case "wind":
return self.wind or {}
case "battery":
return self.battery or {}
case _ as unreachable:
assert_never(unreachable)

def component_types(self) -> list[str]:
"""Get a list of all component types in the configuration."""
return list(self.ctype.keys())
Expand Down
Loading