Skip to content

component_graph

frequenz.sdk.microgrid.component_graph ¤

Component graph representation for a microgrid.

Classes¤

frequenz.sdk.microgrid.component_graph.ComponentGraph ¤

Bases: ComponentGraph[Component, ComponentConnection, ComponentId]

A representation of a microgrid's component graph.

Source code in src/frequenz/sdk/microgrid/component_graph.py
class ComponentGraph(BaseComponentGraph[Component, ComponentConnection, ComponentId]):
    """A representation of a microgrid's component graph."""

    def is_pv_inverter(self, component: Component) -> bool:
        """Check if the specified component is a PV inverter.

        Args:
            component: The component to check.

        Returns:
            Whether the specified component is a PV inverter.
        """
        return isinstance(component, SolarInverter)

    def is_pv_chain(self, component: Component) -> bool:
        """Check if the specified component is part of a PV chain.

        A component is part of a PV chain if it is either a PV inverter or a PV
        meter.

        Args:
            component: The component to check.

        Returns:
            Whether the specified component is part of a PV chain.
        """
        return self.is_pv_inverter(component) or self.is_pv_meter(component)

    @override
    def is_pv_meter(self, component: Component | ComponentId) -> bool:
        """Check if the specified component is a PV meter.

        Args:
            component: The component or component ID to check.

        Returns:
            Whether the specified component is a PV meter.
        """
        if isinstance(component, Component):
            return super().is_pv_meter(component.id)
        return super().is_pv_meter(component)

    def is_ev_charger(self, component: Component) -> bool:
        """Check if the specified component is an EV charger.

        Args:
            component: The component to check.

        Returns:
            Whether the specified component is an EV charger.
        """
        return isinstance(component, EvCharger)

    def is_ev_charger_chain(self, component: Component) -> bool:
        """Check if the specified component is part of an EV charger chain.

        A component is part of an EV charger chain if it is either an EV charger or an
        EV charger meter.

        Args:
            component: The component to check.

        Returns:
            Whether the specified component is part of an EV charger chain.
        """
        return self.is_ev_charger(component) or self.is_ev_charger_meter(component)

    @override
    def is_ev_charger_meter(self, component: Component | ComponentId) -> bool:
        """Check if the specified component is an EV charger meter.

        Args:
            component: The component or component ID to check.

        Returns:
            Whether the specified component is an EV charger meter.
        """
        if isinstance(component, Component):
            return super().is_ev_charger_meter(component.id)
        return super().is_ev_charger_meter(component)

    def is_battery_inverter(self, component: Component) -> bool:
        """Check if the specified component is a battery inverter.

        Args:
            component: The component to check.

        Returns:
            Whether the specified component is a battery inverter.
        """
        return isinstance(component, BatteryInverter)

    def is_battery_chain(self, component: Component) -> bool:
        """Check if the specified component is part of a battery chain.

        A component is part of a battery chain if it is either a battery inverter or a
        battery meter.

        Args:
            component: The component to check.

        Returns:
            Whether the specified component is part of a battery chain.
        """
        return self.is_battery_inverter(component) or self.is_battery_meter(component)

    @override
    def is_battery_meter(self, component: Component | ComponentId) -> bool:
        """Check if the specified component is a battery meter.

        Args:
            component: The component or component ID to check.

        Returns:
            Whether the specified component is a battery meter.
        """
        if isinstance(component, Component):
            return super().is_battery_meter(component.id)
        return super().is_battery_meter(component)

    def is_chp(self, component: Component) -> bool:
        """Check if the specified component is a CHP.

        Args:
            component: The component to check.

        Returns:
            Whether the specified component is a CHP.
        """
        return isinstance(component, Chp)

    def is_chp_chain(self, component: Component) -> bool:
        """Check if the specified component is part of a CHP chain.

        A component is part of a CHP chain if it is either a CHP or a CHP meter.

        Args:
            component: The component to check.

        Returns:
            Whether the specified component is part of a CHP chain.
        """
        return self.is_chp(component) or self.is_chp_meter(component)

    @override
    def is_chp_meter(self, component: Component | ComponentId) -> bool:
        """Check if the specified component is a CHP meter.

        Args:
            component: The component or component ID to check.

        Returns:
            Whether the specified component is a CHP meter.
        """
        if isinstance(component, Component):
            return super().is_chp_meter(component.id)
        return super().is_chp_meter(component)
Functions¤
__init__ ¤
__init__(
    components: Iterable[ComponentT],
    connections: Iterable[ConnectionT],
    config: ComponentGraphConfig = ComponentGraphConfig(),
) -> None

Initialize this instance.

PARAMETER DESCRIPTION
components

The list of components to build the graph with.

TYPE: Iterable[ComponentT]

connections

The list of connections between the components.

TYPE: Iterable[ConnectionT]

config

The configuration for the component graph.

TYPE: ComponentGraphConfig DEFAULT: ComponentGraphConfig()

RAISES DESCRIPTION
InvalidGraphError

if a valid graph cannot be constructed from the given components and connections, based on the given configs.

battery_coalesce_formula ¤
battery_coalesce_formula(
    battery_ids: Set[ComponentIdT] | None,
) -> str

Generate the battery coalesce formula for this component graph.

PARAMETER DESCRIPTION
battery_ids

The set of battery inverter component IDs to include in the formula. If None, all battery inverters in the graph will be included.

TYPE: Set[ComponentIdT] | None

RETURNS DESCRIPTION
str

The battery coalesced formula as a string.

RAISES DESCRIPTION
FormulaGenerationError

if the given component IDs don't exist or are not batteries.

battery_formula ¤
battery_formula(
    battery_ids: Set[ComponentIdT] | None,
) -> str

Generate the battery formula for this component graph.

PARAMETER DESCRIPTION
battery_ids

The set of battery component IDs to include in the formula. If None, all batteries in the graph will be included.

TYPE: Set[ComponentIdT] | None

RETURNS DESCRIPTION
str

The battery formula as a string.

RAISES DESCRIPTION
FormulaGenerationError

if the given component IDs don't exist or are not batteries.

chp_formula ¤
chp_formula(chp_ids: Set[ComponentIdT] | None) -> str

Generate the CHP formula for this component graph.

PARAMETER DESCRIPTION
chp_ids

The set of CHP component IDs to include in the formula. If None, all CHPs in the graph will be included.

TYPE: Set[ComponentIdT] | None

RETURNS DESCRIPTION
str

The CHP formula as a string.

RAISES DESCRIPTION
FormulaGenerationError

if the given component IDs don't exist or are not CHPs.

component ¤
component(component_id: ComponentIdT) -> ComponentT

Fetch the component with the specified component_id.

PARAMETER DESCRIPTION
component_id

The id of the component to look for.

TYPE: ComponentIdT

RETURNS DESCRIPTION
ComponentT

The component with the given id.

RAISES DESCRIPTION
ValueError

if no component exists with the given ID.

components ¤
components(
    matching_ids: (
        Iterable[ComponentIdT] | ComponentIdT | None
    ) = None,
    matching_types: (
        Iterable[type[ComponentT]] | type[ComponentT] | None
    ) = None,
) -> set[ComponentT]

Fetch all components in this graph.

RETURNS DESCRIPTION
set[ComponentT]

A set of all components in this graph.

connections ¤
connections() -> Set[ConnectionT]

Fetch all connections in this graph.

RETURNS DESCRIPTION
Set[ConnectionT]

A set of all connections in this graph.

consumer_formula ¤
consumer_formula() -> str

Generate the consumer formula for this component graph.

RETURNS DESCRIPTION
str

The consumer formula as a string.

ev_charger_formula ¤
ev_charger_formula(
    ev_charger_ids: Set[ComponentIdT] | None,
) -> str

Generate the EV charger formula for this component graph.

PARAMETER DESCRIPTION
ev_charger_ids

The set of EV charger component IDs to include in the formula. If None, all EV chargers in the graph will be included.

TYPE: Set[ComponentIdT] | None

RETURNS DESCRIPTION
str

The EV charger formula as a string.

RAISES DESCRIPTION
FormulaGenerationError

if the given component IDs don't exist or are not EV chargers.

grid_coalesce_formula ¤
grid_coalesce_formula() -> str

Generate the grid coalesce formula for this component graph.

RETURNS DESCRIPTION
str

The grid coalesced formula as a string.

grid_formula ¤
grid_formula() -> str

Generate the grid formula for this component graph.

RETURNS DESCRIPTION
str

The grid formula as a string.

is_battery_chain ¤
is_battery_chain(component: Component) -> bool

Check if the specified component is part of a battery chain.

A component is part of a battery chain if it is either a battery inverter or a battery meter.

PARAMETER DESCRIPTION
component

The component to check.

TYPE: Component

RETURNS DESCRIPTION
bool

Whether the specified component is part of a battery chain.

Source code in src/frequenz/sdk/microgrid/component_graph.py
def is_battery_chain(self, component: Component) -> bool:
    """Check if the specified component is part of a battery chain.

    A component is part of a battery chain if it is either a battery inverter or a
    battery meter.

    Args:
        component: The component to check.

    Returns:
        Whether the specified component is part of a battery chain.
    """
    return self.is_battery_inverter(component) or self.is_battery_meter(component)
is_battery_inverter ¤
is_battery_inverter(component: Component) -> bool

Check if the specified component is a battery inverter.

PARAMETER DESCRIPTION
component

The component to check.

TYPE: Component

RETURNS DESCRIPTION
bool

Whether the specified component is a battery inverter.

Source code in src/frequenz/sdk/microgrid/component_graph.py
def is_battery_inverter(self, component: Component) -> bool:
    """Check if the specified component is a battery inverter.

    Args:
        component: The component to check.

    Returns:
        Whether the specified component is a battery inverter.
    """
    return isinstance(component, BatteryInverter)
is_battery_meter ¤
is_battery_meter(
    component: Component | ComponentId,
) -> bool

Check if the specified component is a battery meter.

PARAMETER DESCRIPTION
component

The component or component ID to check.

TYPE: Component | ComponentId

RETURNS DESCRIPTION
bool

Whether the specified component is a battery meter.

Source code in src/frequenz/sdk/microgrid/component_graph.py
@override
def is_battery_meter(self, component: Component | ComponentId) -> bool:
    """Check if the specified component is a battery meter.

    Args:
        component: The component or component ID to check.

    Returns:
        Whether the specified component is a battery meter.
    """
    if isinstance(component, Component):
        return super().is_battery_meter(component.id)
    return super().is_battery_meter(component)
is_chp ¤
is_chp(component: Component) -> bool

Check if the specified component is a CHP.

PARAMETER DESCRIPTION
component

The component to check.

TYPE: Component

RETURNS DESCRIPTION
bool

Whether the specified component is a CHP.

Source code in src/frequenz/sdk/microgrid/component_graph.py
def is_chp(self, component: Component) -> bool:
    """Check if the specified component is a CHP.

    Args:
        component: The component to check.

    Returns:
        Whether the specified component is a CHP.
    """
    return isinstance(component, Chp)
is_chp_chain ¤
is_chp_chain(component: Component) -> bool

Check if the specified component is part of a CHP chain.

A component is part of a CHP chain if it is either a CHP or a CHP meter.

PARAMETER DESCRIPTION
component

The component to check.

TYPE: Component

RETURNS DESCRIPTION
bool

Whether the specified component is part of a CHP chain.

Source code in src/frequenz/sdk/microgrid/component_graph.py
def is_chp_chain(self, component: Component) -> bool:
    """Check if the specified component is part of a CHP chain.

    A component is part of a CHP chain if it is either a CHP or a CHP meter.

    Args:
        component: The component to check.

    Returns:
        Whether the specified component is part of a CHP chain.
    """
    return self.is_chp(component) or self.is_chp_meter(component)
is_chp_meter ¤
is_chp_meter(component: Component | ComponentId) -> bool

Check if the specified component is a CHP meter.

PARAMETER DESCRIPTION
component

The component or component ID to check.

TYPE: Component | ComponentId

RETURNS DESCRIPTION
bool

Whether the specified component is a CHP meter.

Source code in src/frequenz/sdk/microgrid/component_graph.py
@override
def is_chp_meter(self, component: Component | ComponentId) -> bool:
    """Check if the specified component is a CHP meter.

    Args:
        component: The component or component ID to check.

    Returns:
        Whether the specified component is a CHP meter.
    """
    if isinstance(component, Component):
        return super().is_chp_meter(component.id)
    return super().is_chp_meter(component)
is_ev_charger ¤
is_ev_charger(component: Component) -> bool

Check if the specified component is an EV charger.

PARAMETER DESCRIPTION
component

The component to check.

TYPE: Component

RETURNS DESCRIPTION
bool

Whether the specified component is an EV charger.

Source code in src/frequenz/sdk/microgrid/component_graph.py
def is_ev_charger(self, component: Component) -> bool:
    """Check if the specified component is an EV charger.

    Args:
        component: The component to check.

    Returns:
        Whether the specified component is an EV charger.
    """
    return isinstance(component, EvCharger)
is_ev_charger_chain ¤
is_ev_charger_chain(component: Component) -> bool

Check if the specified component is part of an EV charger chain.

A component is part of an EV charger chain if it is either an EV charger or an EV charger meter.

PARAMETER DESCRIPTION
component

The component to check.

TYPE: Component

RETURNS DESCRIPTION
bool

Whether the specified component is part of an EV charger chain.

Source code in src/frequenz/sdk/microgrid/component_graph.py
def is_ev_charger_chain(self, component: Component) -> bool:
    """Check if the specified component is part of an EV charger chain.

    A component is part of an EV charger chain if it is either an EV charger or an
    EV charger meter.

    Args:
        component: The component to check.

    Returns:
        Whether the specified component is part of an EV charger chain.
    """
    return self.is_ev_charger(component) or self.is_ev_charger_meter(component)
is_ev_charger_meter ¤
is_ev_charger_meter(
    component: Component | ComponentId,
) -> bool

Check if the specified component is an EV charger meter.

PARAMETER DESCRIPTION
component

The component or component ID to check.

TYPE: Component | ComponentId

RETURNS DESCRIPTION
bool

Whether the specified component is an EV charger meter.

Source code in src/frequenz/sdk/microgrid/component_graph.py
@override
def is_ev_charger_meter(self, component: Component | ComponentId) -> bool:
    """Check if the specified component is an EV charger meter.

    Args:
        component: The component or component ID to check.

    Returns:
        Whether the specified component is an EV charger meter.
    """
    if isinstance(component, Component):
        return super().is_ev_charger_meter(component.id)
    return super().is_ev_charger_meter(component)
is_pv_chain ¤
is_pv_chain(component: Component) -> bool

Check if the specified component is part of a PV chain.

A component is part of a PV chain if it is either a PV inverter or a PV meter.

PARAMETER DESCRIPTION
component

The component to check.

TYPE: Component

RETURNS DESCRIPTION
bool

Whether the specified component is part of a PV chain.

Source code in src/frequenz/sdk/microgrid/component_graph.py
def is_pv_chain(self, component: Component) -> bool:
    """Check if the specified component is part of a PV chain.

    A component is part of a PV chain if it is either a PV inverter or a PV
    meter.

    Args:
        component: The component to check.

    Returns:
        Whether the specified component is part of a PV chain.
    """
    return self.is_pv_inverter(component) or self.is_pv_meter(component)
is_pv_inverter ¤
is_pv_inverter(component: Component) -> bool

Check if the specified component is a PV inverter.

PARAMETER DESCRIPTION
component

The component to check.

TYPE: Component

RETURNS DESCRIPTION
bool

Whether the specified component is a PV inverter.

Source code in src/frequenz/sdk/microgrid/component_graph.py
def is_pv_inverter(self, component: Component) -> bool:
    """Check if the specified component is a PV inverter.

    Args:
        component: The component to check.

    Returns:
        Whether the specified component is a PV inverter.
    """
    return isinstance(component, SolarInverter)
is_pv_meter ¤
is_pv_meter(component: Component | ComponentId) -> bool

Check if the specified component is a PV meter.

PARAMETER DESCRIPTION
component

The component or component ID to check.

TYPE: Component | ComponentId

RETURNS DESCRIPTION
bool

Whether the specified component is a PV meter.

Source code in src/frequenz/sdk/microgrid/component_graph.py
@override
def is_pv_meter(self, component: Component | ComponentId) -> bool:
    """Check if the specified component is a PV meter.

    Args:
        component: The component or component ID to check.

    Returns:
        Whether the specified component is a PV meter.
    """
    if isinstance(component, Component):
        return super().is_pv_meter(component.id)
    return super().is_pv_meter(component)
predecessors ¤
predecessors(component_id: ComponentIdT) -> Set[ComponentT]

Fetch all predecessors of the specified component ID.

PARAMETER DESCRIPTION
component_id

ID of the component whose predecessors should be fetched.

TYPE: ComponentIdT

RETURNS DESCRIPTION
Set[ComponentT]

A set of components that are predecessors of the given component ID.

RAISES DESCRIPTION
ValueError

if no component exists with the given ID.

producer_formula ¤
producer_formula() -> str

Generate the producer formula for this component graph.

RETURNS DESCRIPTION
str

The producer formula as a string.

pv_coalesce_formula ¤
pv_coalesce_formula(
    pv_inverter_ids: Set[ComponentIdT] | None,
) -> str

Generate the PV coalesce formula for this component graph.

PARAMETER DESCRIPTION
pv_inverter_ids

The set of PV inverter component IDs to include in the formula. If None, all PV inverters in the graph will be included.

TYPE: Set[ComponentIdT] | None

RETURNS DESCRIPTION
str

The PV coalesced formula as a string.

RAISES DESCRIPTION
FormulaGenerationError

if the given component IDs don't exist or are not PV inverters.

pv_formula ¤
pv_formula(
    pv_inverter_ids: Set[ComponentIdT] | None,
) -> str

Generate the PV formula for this component graph.

PARAMETER DESCRIPTION
pv_inverter_ids

The set of PV inverter component IDs to include in the formula. If None, all PV inverters in the graph will be included.

TYPE: Set[ComponentIdT] | None

RETURNS DESCRIPTION
str

The PV formula as a string.

RAISES DESCRIPTION
FormulaGenerationError

if the given component IDs don't exist or are not PV inverters.

successors ¤
successors(component_id: ComponentIdT) -> Set[ComponentT]

Fetch all successors of the specified component ID.

PARAMETER DESCRIPTION
component_id

ID of the component whose successors should be fetched.

TYPE: ComponentIdT

RETURNS DESCRIPTION
Set[ComponentT]

A set of components that are successors of the given component ID.

RAISES DESCRIPTION
ValueError

if no component exists with the given ID.

wind_turbine_formula ¤
wind_turbine_formula(
    wind_turbine_ids: Set[ComponentIdT] | None,
) -> str

Generate the wind turbine formula for this component graph.

PARAMETER DESCRIPTION
wind_turbine_ids

The set of wind turbine component IDs to include in the formula. If None, all wind turbines in the graph will be included.

TYPE: Set[ComponentIdT] | None

RETURNS DESCRIPTION
str

The wind turbine formula as a string.

RAISES DESCRIPTION
FormulaGenerationError

if the given component IDs don't exist or are not wind turbines.