GLOBALGRID2050 / EXPLORE CODE

Part of: Explore Code / Solar and storage design / solar-electrical-topology-analysis-engine-text-based / src/solar_topology/array

Array Topology (part 2)

A Python module in Solar Electrical Topology Analysis Engine Text Based that works with solar, using SHA-256. It builds table topology (`build_table_topology`), attaches input topology (`attach_input_topology`) and adds node (`add_node`).

409 lines of Python from solar-electrical-topology-analysis-engine-text-based, in 1 file. Topic: Solar and storage design. Section 215 of 2044.

Cut 2026-12-06T03:07:51+00:00. A copy for reading; the repository at the stated commit is the source.

In the State lines below, domain stage, users and canonical status are rule-based first approximations from code and path evidence (rules explore-agent-rules-v1), not observed facts: the rules.

In this section

  1. Array Topology transform · solar-electrical-topology-analysis-engine-text-based / src/solar_topology/array/array_topology.py lines 701–1109

Array Topology

A Python module in Solar Electrical Topology Analysis Engine Text Based that works with solar, using SHA-256. It builds table topology (`build_table_topology`), attaches input topology (`attach_input_topology`) and adds node (`add_node`).

transformsolar-electrical-topology-analysis-engine-text-based / src/solar_topology/array/array_topology.py · lines 701–1109 of 1109 (continued from section 214) · Not reached from any entry point (candidate for review) · pattern StringTopology (shared by 2 files)

What it does
It calculates solar. Defines 37 functions or classes, including WiringStrategy, NodeKind, EdgeKind, Build025Limits, __post_init__. Uses libraries __future__, collections, dataclasses, enum, hashlib.
Why it exists in the grid workflow
Domain stage sub33kv_design (path word 'array'): inside-the-fence electrical and civil design below 33kV, which Ventus Ltd states is within its commercial execution boundary. Domain terms in the code: solar.
Downstream impact
Used directly by 5 files: Solar Topology — Array (solar-electrical-topology-analysis-engine-text-based); Array Engine (solar-electrical-topology-analysis-engine-text-based); Array Route Geometry (solar-electrical-topology-analysis-engine-text-based); Array Route Types (solar-electrical-topology-analysis-engine-text-based); and 1 more.
What it achieves
Build 025C topology and physical-input allocation.
Why it matters
Imported by 5 files, including Solar Topology — Array, Array Engine and Array Route Geometry.
Application
solar-electrical-topology-analysis-engine-text-based · src/solar_topology/array · Python module
Uses
Geometry Authority (import), Table String Assignment (import), Table String Validation (import), __future__, collections, dataclasses, enum, hashlib, json, math, typing
Used by
imported by Solar Topology — Array (relative import), imported by Array Engine (import), imported by Array Route Geometry (import), imported by Array Route Types (import), imported by Array Routing (import)
Produces
WiringStrategy, NodeKind, EdgeKind, Build025Limits, TopologyNode, TopologyEdge, StringTopology, TableTopologyReceipt, PhysicalInputSpec, EquipmentProfile, InputAssignment, InputAllocationReceipt
Defines here
_equipment_node, attach_input_topology, add_node, add_edge, input_spec_by_string
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
solar-electrical-topology-analysis-engine-text-based@d3b4c497144c2c9b3e8f0e82117e7e9abe4672b9 src/solar_topology/array/array_topology.py:701-1109 · blob c98d65ff7f69186f3fbb778ea6dcc93229220025 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: transform · effects: none found · domain stage: sub33kv_design · users: EPC_contractor, ICP_grid_engineer · canonical: unreached_review
    unused_inputs = tuple(
        item.input_id for item in profile.physical_inputs if item.input_id not in assigned_inputs
    )
    unused_mppts = tuple(mppt_id for mppt_id in profile.mppt_ids if mppt_id not in assigned_mppts)
    basis = {
        "schema_version": INPUT_ALLOCATION_SCHEMA_VERSION,
        "table_id": allocation.table_id,
        "assignment_hash": allocation.assignment_hash,
        "equipment_profile": equipment_profile_payload(profile),
        "assignments": [[item.string_id, item.input_id, item.mppt_id] for item in assignments],
    }
    return InputAllocationReceipt(
        table_id=allocation.table_id,
        assignment_hash=allocation.assignment_hash,
        equipment_profile_id=profile.profile_id,
        inverter_id=profile.inverter_id,
        assignments=tuple(assignments),
        unused_input_ids=unused_inputs,
        unused_mppt_ids=unused_mppts,
        allocation_hash=_hash_payload(basis),
    )



def _equipment_node(
    node_id: str,
    kind: NodeKind,
    inverter_id: str,
) -> TopologyNode:
    return TopologyNode(
        node_id=node_id,
        kind=kind,
        equipment_id=inverter_id,
    )


def attach_input_topology(
    topology: TableTopologyReceipt,
    input_allocation: InputAllocationReceipt,
    profile: EquipmentProfile,
) -> TableTopologyReceipt:
    """Attach inverter input, MPPT and DC-bus connectivity to the table graph."""

    if topology.equipment_nodes or topology.equipment_edges:
        raise ValueError("equipment topology has already been attached")
    if topology.table_id != input_allocation.table_id:
        raise ValueError("topology and input allocation table identifiers differ")
    if topology.assignment_hash != input_allocation.assignment_hash:
        raise ValueError("topology and input allocation assignments differ")
    if input_allocation.equipment_profile_id != profile.profile_id:
        raise ValueError("input allocation and equipment profile do not match")
    if input_allocation.inverter_id != profile.inverter_id:
        raise ValueError("input allocation and inverter identifiers do not match")

    topology_by_string = {item.string_id: item for item in topology.strings}
    assigned_string_ids = {
        item.string_id for item in input_allocation.assignments
    }
    if assigned_string_ids != set(topology_by_string):
        raise ValueError(
            "input allocation must cover every topology string exactly once"
        )

    input_by_id = {item.input_id: item for item in profile.physical_inputs}
    equipment_nodes: list[TopologyNode] = []
    equipment_edges: list[TopologyEdge] = []
    node_ids: set[str] = set()
    edge_ids: set[str] = set()

    def add_node(node: TopologyNode) -> None:
        if node.node_id in node_ids:
            raise ValueError(
                f"duplicate equipment node identifier: {node.node_id!r}"
            )
        node_ids.add(node.node_id)
        equipment_nodes.append(node)

    def add_edge(edge: TopologyEdge) -> None:
        if edge.edge_id in edge_ids:
            raise ValueError(
                f"duplicate equipment edge identifier: {edge.edge_id!r}"
            )
        edge_ids.add(edge.edge_id)
        equipment_edges.append(edge)

    for physical_input in profile.physical_inputs:
        add_node(
            _equipment_node(
                f"{physical_input.input_id}:N",
                NodeKind.PHYSICAL_INPUT_NEGATIVE,
                profile.inverter_id,
            )
        )
        add_node(
            _equipment_node(
                f"{physical_input.input_id}:P",
                NodeKind.PHYSICAL_INPUT_POSITIVE,
                profile.inverter_id,
            )
        )

    parallel_to_mppt: dict[str, str] = {}
    for physical_input in profile.physical_inputs:
        if not physical_input.isolated:
            if physical_input.parallel_node_id is None:
                raise ValueError(
                    "a non-isolated physical input requires a parallel node"
                )
            existing_mppt = parallel_to_mppt.setdefault(
                physical_input.parallel_node_id,
                physical_input.mppt_id,
            )
            if existing_mppt != physical_input.mppt_id:
                raise ValueError(
                    "one parallel junction cannot feed multiple MPPTs"
                )

    for parallel_node_id in parallel_to_mppt:
        add_node(
            _equipment_node(
                f"{parallel_node_id}:N",
                NodeKind.PARALLEL_JUNCTION,
                profile.inverter_id,
            )
        )
        add_node(
            _equipment_node(
                f"{parallel_node_id}:P",
                NodeKind.PARALLEL_JUNCTION,
                profile.inverter_id,
            )
        )

    for mppt_id in profile.mppt_ids:
        add_node(
            _equipment_node(
                f"{profile.inverter_id}:{mppt_id}:N",
                NodeKind.MPPT_INPUT,
                profile.inverter_id,
            )
        )
        add_node(
            _equipment_node(
                f"{profile.inverter_id}:{mppt_id}:P",
                NodeKind.MPPT_INPUT,
                profile.inverter_id,
            )
        )

    bus_negative = f"{profile.dc_bus_node_id}:N"
    bus_positive = f"{profile.dc_bus_node_id}:P"
    add_node(
        _equipment_node(
            bus_negative,
            NodeKind.INVERTER_DC_BUS,
            profile.inverter_id,
        )
    )
    add_node(
        _equipment_node(
            bus_positive,
            NodeKind.INVERTER_DC_BUS,
            profile.inverter_id,
        )
    )

    for assignment in input_allocation.assignments:
        physical_input = input_by_id.get(assignment.input_id)
        if physical_input is None:
            raise ValueError(
                f"allocation references unknown input {assignment.input_id!r}"
            )
        if physical_input.mppt_id != assignment.mppt_id:
            raise ValueError(
                "allocation MPPT label does not match equipment profile"
            )
        string = topology_by_string[assignment.string_id]
        add_edge(
            TopologyEdge(
                edge_id=f"{assignment.string_id}:TO:{assignment.input_id}:N",
                kind=EdgeKind.INPUT_LINK,
                from_node_id=string.free_negative_node_id,
                to_node_id=f"{assignment.input_id}:N",
                string_id=assignment.string_id,
            )
        )
        add_edge(
            TopologyEdge(
                edge_id=f"{assignment.input_id}:P:TO:{assignment.string_id}",
                kind=EdgeKind.INPUT_LINK,
                from_node_id=f"{assignment.input_id}:P",
                to_node_id=string.free_positive_node_id,
                string_id=assignment.string_id,
            )
        )

    for physical_input in profile.physical_inputs:
        input_negative = f"{physical_input.input_id}:N"
        input_positive = f"{physical_input.input_id}:P"
        if physical_input.parallel_node_id is None:
            target_negative = (
                f"{profile.inverter_id}:{physical_input.mppt_id}:N"
            )
            target_positive = (
                f"{profile.inverter_id}:{physical_input.mppt_id}:P"
            )
        else:
            target_negative = f"{physical_input.parallel_node_id}:N"
            target_positive = f"{physical_input.parallel_node_id}:P"

        if physical_input.protective_device_node_id is None:
            add_edge(
                TopologyEdge(
                    edge_id=f"{physical_input.input_id}:N:INTERNAL",
                    kind=EdgeKind.INPUT_LINK,
                    from_node_id=input_negative,
                    to_node_id=target_negative,
                )
            )
            add_edge(
                TopologyEdge(
                    edge_id=f"{physical_input.input_id}:P:INTERNAL",
                    kind=EdgeKind.INPUT_LINK,
                    from_node_id=target_positive,
                    to_node_id=input_positive,
                )
            )
        else:
            fuse_negative = f"{physical_input.protective_device_node_id}:N"
            fuse_positive = f"{physical_input.protective_device_node_id}:P"
            add_node(
                _equipment_node(
                    fuse_negative,
                    NodeKind.STRING_FUSE,
                    profile.inverter_id,
                )
            )
            add_node(
                _equipment_node(
                    fuse_positive,
                    NodeKind.STRING_FUSE,
                    profile.inverter_id,
                )
            )
            add_edge(
                TopologyEdge(
                    edge_id=f"{physical_input.input_id}:N:TO:FUSE",
                    kind=EdgeKind.PROTECTIVE_DEVICE_LINK,
                    from_node_id=input_negative,
                    to_node_id=fuse_negative,
                )
            )
            add_edge(
                TopologyEdge(
                    edge_id=f"{physical_input.input_id}:N:FROM:FUSE",
                    kind=EdgeKind.PROTECTIVE_DEVICE_LINK,
                    from_node_id=fuse_negative,
                    to_node_id=target_negative,
                )
            )
            add_edge(
                TopologyEdge(
                    edge_id=f"{physical_input.input_id}:P:TO:FUSE",
                    kind=EdgeKind.PROTECTIVE_DEVICE_LINK,
                    from_node_id=target_positive,
                    to_node_id=fuse_positive,
                )
            )
            add_edge(
                TopologyEdge(
                    edge_id=f"{physical_input.input_id}:P:FROM:FUSE",
                    kind=EdgeKind.PROTECTIVE_DEVICE_LINK,
                    from_node_id=fuse_positive,
                    to_node_id=input_positive,
                )
            )

    for parallel_node_id, mppt_id in parallel_to_mppt.items():
        add_edge(
            TopologyEdge(
                edge_id=f"{parallel_node_id}:N:TO:{mppt_id}",
                kind=EdgeKind.PARALLEL_LINK,
                from_node_id=f"{parallel_node_id}:N",
                to_node_id=f"{profile.inverter_id}:{mppt_id}:N",
            )
        )
        add_edge(
            TopologyEdge(
                edge_id=f"{mppt_id}:P:TO:{parallel_node_id}",
                kind=EdgeKind.PARALLEL_LINK,
                from_node_id=f"{profile.inverter_id}:{mppt_id}:P",
                to_node_id=f"{parallel_node_id}:P",
            )
        )

    for mppt_id in profile.mppt_ids:
        add_edge(
            TopologyEdge(
                edge_id=f"{profile.inverter_id}:{mppt_id}:N:TO:DC-BUS",
                kind=EdgeKind.MPPT_LINK,
                from_node_id=f"{profile.inverter_id}:{mppt_id}:N",
                to_node_id=bus_negative,
            )
        )
        add_edge(
            TopologyEdge(
                edge_id=f"DC-BUS:P:TO:{profile.inverter_id}:{mppt_id}",
                kind=EdgeKind.MPPT_LINK,
                from_node_id=bus_positive,
                to_node_id=f"{profile.inverter_id}:{mppt_id}:P",
            )
        )

    string_node_ids = {
        node.node_id for string in topology.strings for node in string.nodes
    }
    all_node_ids = string_node_ids | node_ids
    missing_endpoints = sorted(
        {
            endpoint
            for edge in equipment_edges
            for endpoint in (edge.from_node_id, edge.to_node_id)
            if endpoint not in all_node_ids
        }
    )
    if missing_endpoints:
        raise ValueError(
            "equipment topology references missing nodes: "
            f"{missing_endpoints}"
        )

    all_edge_ids = {
        edge.edge_id for string in topology.strings for edge in string.edges
    }
    duplicate_edges = sorted(all_edge_ids & edge_ids)
    duplicate_nodes = sorted(string_node_ids & node_ids)
    if duplicate_nodes or duplicate_edges:
        raise ValueError(
            "equipment topology collides with string topology identifiers; "
            f"nodes={duplicate_nodes}, edges={duplicate_edges}"
        )

    basis = _topology_basis(
        table_id=topology.table_id,
        assignment_hash=topology.assignment_hash,
        strategy=topology.strategy,
        strings=topology.strings,
        equipment_nodes=equipment_nodes,
        equipment_edges=equipment_edges,
    )
    return replace(
        topology,
        node_count=topology.node_count + len(equipment_nodes),
        edge_count=topology.edge_count + len(equipment_edges),
        topology_hash=_hash_payload(basis),
        equipment_nodes=tuple(equipment_nodes),
        equipment_edges=tuple(equipment_edges),
    )

def input_spec_by_string(
    receipt: InputAllocationReceipt,
    profile: EquipmentProfile,
) -> dict[str, PhysicalInputSpec]:
    if receipt.equipment_profile_id != profile.profile_id:
        raise ValueError("input allocation and equipment profile do not match")
    input_by_id = {item.input_id: item for item in profile.physical_inputs}
    resolved: dict[str, PhysicalInputSpec] = {}
    for assignment in receipt.assignments:
        item = input_by_id.get(assignment.input_id)
        if item is None:
            raise ValueError(f"allocation references missing physical input {assignment.input_id!r}")
        if item.mppt_id != assignment.mppt_id:
            raise ValueError("allocation MPPT label does not match the physical input profile")
        resolved[assignment.string_id] = item
    return resolved


__all__ = [
    "DEFAULT_BUILD_025_LIMITS",
    "EQUIPMENT_PROFILE_SCHEMA_VERSION",
    "INPUT_ALLOCATION_SCHEMA_VERSION",
    "STRING_ALLOCATION_SCHEMA_VERSION",
    "TOPOLOGY_SCHEMA_VERSION",
    "Build025Limits",
    "EdgeKind",
    "EquipmentProfile",
    "InputAllocationReceipt",
    "InputAssignment",
    "NodeKind",
    "PhysicalInputSpec",
    "StringAllocationReceipt",
    "StringAssignment",
    "StringTopology",
    "TableTopologyReceipt",
    "TopologyEdge",
    "TopologyNode",
    "WiringStrategy",
    "allocate_physical_inputs",
    "allocate_strings",
    "allocation_payload",
    "attach_input_topology",
    "build_table_topology",
    "electrical_module_order",
    "equipment_profile_payload",
    "input_allocation_payload",
    "input_spec_by_string",
    "topology_payload",
    "uniform_equipment_profile",
]