inire/inire/__init__.py
Jan Petykiewicz 7476209ef3 Require triangulation and simplify automatic routing
Remove the dependency fallback and joint portal lane retry, make tiered search opt-in, and consolidate tests while adding low-length-cost routing boards.
2026-12-01 22:30:29 -08:00

200 lines
7.5 KiB
Python

"""
inire Wave-router
"""
import warnings
from collections.abc import Callable
from dataclasses import replace
import shapely as _shapely
from shapely.geometry import Polygon as _Polygon
from .geometry.components import _has_port_face
from .geometry.primitives import Port as Port # noqa: PLC0414
from .model import (
CongestionOptions as CongestionOptions,
FixedRoute as FixedRoute,
DiagnosticsOptions as DiagnosticsOptions,
NetSpec as NetSpec,
ObjectiveWeights as ObjectiveWeights,
RefinementOptions as RefinementOptions,
RoutingOptions as RoutingOptions,
RoutingProblem as RoutingProblem,
SBendOutline as SBendOutline,
SearchOptions as SearchOptions,
TopologyOptions as TopologyOptions,
resolve_bend_geometry as _resolve_bend_geometry,
) # noqa: PLC0414
from .results import ( # noqa: PLC0414
AutomaticRoutingReport as AutomaticRoutingReport,
CapacityLimit as CapacityLimit,
ComponentConflictTrace as ComponentConflictTrace,
ConflictTraceEntry as ConflictTraceEntry,
FrontierPruneSample as FrontierPruneSample,
IterationNetAttemptTrace as IterationNetAttemptTrace,
IterationTraceEntry as IterationTraceEntry,
NetConflictTrace as NetConflictTrace,
NetFrontierTrace as NetFrontierTrace,
PrePairFrontierTraceEntry as PrePairFrontierTraceEntry,
PrePairNetTrace as PrePairNetTrace,
RoutingResult as RoutingResult,
RoutingReport as _RoutingReport,
RoutingRunResult as RoutingRunResult,
RoutingStage as RoutingStage,
RoutingTiming as RoutingTiming,
TopologyReport as TopologyReport,
)
from .seeds import Bend90Seed as Bend90Seed, PathSeed as PathSeed, SBendSeed as SBendSeed, StraightSeed as StraightSeed # noqa: PLC0414
if _shapely.geos_version < (3, 10, 0):
raise ImportError(
"Inire requires GEOS 3.10 or newer. "
f"Shapely is linked against GEOS {_shapely.geos_version_string}."
)
__author__ = 'Jan Petykiewicz'
__version__ = '0.1'
def _warn_bend_port_mismatches(results: dict[str, RoutingResult], nets: dict[str, NetSpec]) -> None:
checked = set()
for net_id, result in results.items():
width = nets[net_id].width
for component in result.path:
move = component.move_spec
if not isinstance(move, Bend90Seed) or not component.physical_geometry_valid:
continue
key = (move.radius, width)
if key in checked:
continue
checked.add(key)
polygon = component.physical_geometry[0]
if not (
_has_port_face(polygon, component.start_port, arrival=False, width=width)
and _has_port_face(polygon, component.end_port, arrival=True, width=width)
):
warnings.warn(
f"Custom 90-degree bend of radius {move.radius:g} does not match the declared port faces "
f"of width {width:g} on net {net_id!r}",
stacklevel=3,
)
def route(
problem: RoutingProblem,
*,
options: RoutingOptions | None = None,
iteration_callback: Callable[[int, dict[str, RoutingResult]], None] | None = None,
) -> RoutingRunResult:
resolved_options = RoutingOptions() if options is None else options
search = resolved_options.search
requested = {net.net_id: net for net in problem.nets}
coincident = {
net.net_id: RoutingResult(
net_id=net.net_id,
path=(),
report=_RoutingReport(terminal_error=f"start and target ports {net.start.as_tuple()} coincide after grid snapping"),
requested_net=net,
effective_net=net,
)
for net in problem.nets
if net.start == net.target
}
template_errors = {}
if search.sbend_outlines and search.active_sbend_outlines:
widths = dict.fromkeys(net.width for net in problem.nets)
errors_by_width = {width: search._sbend_template_error(width) for width in widths}
template_errors = {
net.net_id: error for net in problem.nets if (error := errors_by_width[net.width]) is not None
}
seed_errors = {}
for net_id, seed in problem.initial_paths.items():
if net_id not in requested:
raise ValueError(f"Initial path provided for unknown net: {net_id}")
if net_id not in coincident and (error := search.seed_geometry_error(seed)) is not None:
seed_errors[net_id] = f"Initial path for {net_id!r} {error}"
if coincident or seed_errors:
problem = replace(
problem,
nets=tuple(net for net in problem.nets if net.net_id not in coincident),
initial_paths={
net_id: seed for net_id, seed in problem.initial_paths.items()
if net_id not in coincident and net_id not in seed_errors
},
)
def attach_requested(results: dict[str, RoutingResult]) -> dict[str, RoutingResult]:
attached = {
key: replace(result, requested_net=requested[key], effective_net=result.effective_net or requested[key])
for key, result in results.items()
}
for net_id, error in seed_errors.items():
if (result := attached.get(net_id)) is not None:
report = replace(result.report, seed_error=error, invalid_geometry=result.report.invalid_geometry or not result.path)
attached[net_id] = replace(result, report=report)
attached.update(coincident)
for net_id, error in template_errors.items():
if (result := attached.get(net_id)) is not None:
attached[net_id] = replace(result, report=replace(result.report, sbend_template_error=error))
return attached
def notify(iteration: int, results: dict[str, RoutingResult]) -> None:
assert iteration_callback is not None
iteration_callback(iteration, attach_requested(results))
callback = notify if iteration_callback is not None else None
if resolved_options.topology.mode == "automatic":
from .router._automatic import route_automatic # noqa: PLC0415
run = route_automatic(problem, resolved_options, callback)
else:
from .router._stack import route_explicit # noqa: PLC0415
run = route_explicit(problem, options=resolved_options, iteration_callback=callback)
results = attach_requested(run.results_by_net)
if coincident:
results = {net_id: results[net_id] for net_id in requested}
_, physical_bend = _resolve_bend_geometry(search)
if isinstance(physical_bend, _Polygon):
_warn_bend_port_mismatches(results, requested)
run = replace(run, results_by_net=results)
if (coincident or seed_errors or template_errors) and run.conflict_trace:
from .router._route_traces import complete_final_conflict_trace # noqa: PLC0415
run = complete_final_conflict_trace(run)
return run
__all__ = [
"AutomaticRoutingReport",
"Bend90Seed",
"CapacityLimit",
"CongestionOptions",
"FixedRoute",
"ComponentConflictTrace",
"ConflictTraceEntry",
"DiagnosticsOptions",
"NetSpec",
"NetConflictTrace",
"NetFrontierTrace",
"ObjectiveWeights",
"PathSeed",
"Port",
"FrontierPruneSample",
"IterationNetAttemptTrace",
"IterationTraceEntry",
"PrePairFrontierTraceEntry",
"PrePairNetTrace",
"RefinementOptions",
"RoutingOptions",
"RoutingProblem",
"RoutingResult",
"RoutingRunResult",
"RoutingStage",
"RoutingTiming",
"SBendOutline",
"SBendSeed",
"SearchOptions",
"StraightSeed",
"TopologyOptions",
"TopologyReport",
"route",
]