inire/inire/__init__.py

193 lines
7.3 KiB
Python
Raw Normal View History

2026-03-06 18:48:04 -08:00
"""
2026-03-07 08:26:29 -08:00
inire Wave-router
2026-03-06 18:48:04 -08:00
"""
import warnings
2026-03-30 19:51:37 -07:00
from collections.abc import Callable
from dataclasses import replace
2026-03-30 19:51:37 -07:00
from shapely.geometry import Polygon as _Polygon
from .geometry.components import _has_port_face
2026-03-30 19:51:37 -07:00
from .geometry.primitives import Port as Port # noqa: PLC0414
from .model import (
2026-03-30 15:32:29 -07:00
CongestionOptions as CongestionOptions,
FixedRoute as FixedRoute,
2026-03-30 15:32:29 -07:00
DiagnosticsOptions as DiagnosticsOptions,
NetSpec as NetSpec,
ObjectiveWeights as ObjectiveWeights,
RefinementOptions as RefinementOptions,
RoutingOptions as RoutingOptions,
RoutingProblem as RoutingProblem,
SBendOutline as SBendOutline,
2026-03-30 15:32:29 -07:00
SearchOptions as SearchOptions,
TopologyOptions as TopologyOptions,
resolve_bend_geometry as _resolve_bend_geometry,
2026-03-30 15:32:29 -07:00
) # 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,
2026-04-02 18:57:34 -07:00
PrePairFrontierTraceEntry as PrePairFrontierTraceEntry,
PrePairNetTrace as PrePairNetTrace,
RoutingResult as RoutingResult,
RoutingReport as _RoutingReport,
RoutingRunResult as RoutingRunResult,
RoutingStage as RoutingStage,
RoutingTiming as RoutingTiming,
TopologyReport as TopologyReport,
)
2026-03-30 19:51:37 -07:00
from .seeds import Bend90Seed as Bend90Seed, PathSeed as PathSeed, SBendSeed as SBendSeed, StraightSeed as StraightSeed # noqa: PLC0414
2026-03-06 18:48:04 -08:00
__author__ = 'Jan Petykiewicz'
__version__ = '0.1'
2026-03-30 15:32:29 -07:00
2026-03-30 19:51:37 -07:00
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,
)
2026-03-30 19:51:37 -07:00
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
2026-03-30 19:51:37 -07:00
2026-03-30 15:32:29 -07:00
__all__ = [
"AutomaticRoutingReport",
2026-03-30 19:51:37 -07:00
"Bend90Seed",
"CapacityLimit",
2026-03-30 15:32:29 -07:00
"CongestionOptions",
"FixedRoute",
"ComponentConflictTrace",
"ConflictTraceEntry",
2026-03-30 15:32:29 -07:00
"DiagnosticsOptions",
"NetSpec",
"NetConflictTrace",
"NetFrontierTrace",
2026-03-30 15:32:29 -07:00
"ObjectiveWeights",
2026-03-30 19:51:37 -07:00
"PathSeed",
2026-03-30 15:32:29 -07:00
"Port",
"FrontierPruneSample",
"IterationNetAttemptTrace",
"IterationTraceEntry",
2026-04-02 18:57:34 -07:00
"PrePairFrontierTraceEntry",
"PrePairNetTrace",
2026-03-30 15:32:29 -07:00
"RefinementOptions",
"RoutingOptions",
"RoutingProblem",
"RoutingResult",
"RoutingRunResult",
"RoutingStage",
"RoutingTiming",
"SBendOutline",
2026-03-30 19:51:37 -07:00
"SBendSeed",
2026-03-30 15:32:29 -07:00
"SearchOptions",
2026-03-30 19:51:37 -07:00
"StraightSeed",
"TopologyOptions",
"TopologyReport",
2026-03-30 15:32:29 -07:00
"route",
]