inire/inire/tests/layout_helpers.py

80 lines
3.6 KiB
Python

"""Test helpers for placed ports, authored lengths, and process profiles."""
from __future__ import annotations
from dataclasses import dataclass, replace
from math import fsum, isfinite
from typing import TYPE_CHECKING
from inire import route
from inire.constants import TOLERANCE_LINEAR
from inire.model import FixedRoute, NetSpec
if TYPE_CHECKING:
from collections.abc import Iterable
from inire.geometry.primitives import Port
from inire.model import RoutingOptions, RoutingProblem
from inire.results import RoutingResult
@dataclass(frozen=True, slots=True)
class PlacedPort:
port: Port
width: float
def __post_init__(self) -> None:
if not isfinite(self.width) or self.width <= 0:
raise ValueError("Placed port width must be finite and positive")
def connect(self, net_id: str, target: PlacedPort) -> NetSpec:
if abs(self.width - target.width) > TOLERANCE_LINEAR:
raise ValueError(f"Net {net_id!r} joins widths {self.width:g} and {target.width:g}; place a transition first")
return NetSpec(net_id, self.port, target.port, self.width)
def preserves_length(result: RoutingResult, authored_length: float, tolerance: float) -> bool:
"""Check the recorded length of a verified replacement against an authored length."""
if not isfinite(authored_length) or authored_length <= 0 or not isfinite(tolerance) or tolerance < 0:
raise ValueError("Authored length must be finite and positive and tolerance finite and nonnegative")
return result.is_valid and abs(fsum(component.length for component in result.path) - authored_length) <= tolerance
def partition_layers(nets_with_layers: Iterable[tuple[tuple[int, int], NetSpec]]) -> dict[tuple[int, int], tuple[NetSpec, ...]]:
"""Group nets by layout layer and datatype while retaining their input order."""
layers: dict[tuple[int, int], list[NetSpec]] = {}
seen = set()
for layer, net in nets_with_layers:
if net.net_id in seen:
raise ValueError(f"Duplicate net ID across layout layers: {net.net_id!r}")
seen.add(net.net_id)
layers.setdefault(layer, []).append(net)
return {layer: tuple(nets) for layer, nets in layers.items()}
def route_profiles(
problem: RoutingProblem,
profiles: Iterable[tuple[Iterable[str], RoutingOptions]],
) -> dict[str, RoutingResult]:
"""Route each group on the same layer and reserve earlier verified geometry."""
groups = tuple((tuple(net_ids), options) for net_ids, options in profiles)
net_by_id = {net.net_id: net for net in problem.nets}
assigned = [net_id for net_ids, _ in groups for net_id in net_ids]
if len(set(assigned)) != len(assigned) or set(assigned) != net_by_id.keys():
raise ValueError("Process profiles must assign every net to exactly one group")
if unknown := problem.initial_paths.keys() - net_by_id.keys():
raise ValueError(f"Initial paths provided for unknown nets: {', '.join(sorted(unknown))}")
results: dict[str, RoutingResult] = {}
reservations: list[FixedRoute] = []
for net_ids, options in groups:
grouped = replace(
problem,
nets=tuple(net_by_id[net_id] for net_id in net_ids),
initial_paths={net_id: problem.initial_paths[net_id] for net_id in net_ids if net_id in problem.initial_paths},
fixed_routes=problem.fixed_routes + tuple(reservations),
)
current = route(grouped, options=options).results_by_net
results.update(current)
reservations.extend(FixedRoute.from_result(result) for result in current.values() if result.is_valid)
return {net_id: results[net_id] for net_id in net_by_id}