inire/inire/tests/test_clearance_precision.py
2026-03-30 23:54:30 -07:00

132 lines
5 KiB
Python

from inire import CongestionOptions, RoutingOptions, RoutingProblem, SearchOptions
from inire.geometry.collision import RoutingWorld
from inire.geometry.primitives import Port
from inire.geometry.components import Straight
from inire.model import NetSpec
from inire.router._astar_types import AStarContext
from inire.router._router import PathFinder
from inire.router.cost import CostEvaluator
from inire.router.danger_map import DangerMap
def _build_pathfinder(
evaluator: CostEvaluator,
*,
bounds: tuple[float, float, float, float],
netlist: dict[str, tuple[Port, Port]],
net_widths: dict[str, float],
search: SearchOptions | None = None,
congestion: CongestionOptions | None = None,
) -> PathFinder:
nets = tuple(
NetSpec(net_id=net_id, start=start, target=target, width=net_widths.get(net_id, 2.0))
for net_id, (start, target) in netlist.items()
)
return PathFinder(
AStarContext(
evaluator,
RoutingProblem(bounds=bounds, nets=nets),
RoutingOptions(
search=SearchOptions() if search is None else search,
congestion=CongestionOptions() if congestion is None else congestion,
),
),
)
def test_clearance_thresholds():
"""
Check that clearance is correctly calculated:
two paths slightly beyond, exactly at, and slightly violating.
"""
# Clearance = 2.0, Width = 2.0
# Required Centerline-to-Centerline = (2+2)/2 + 2.0 = 4.0
ce = RoutingWorld(clearance=2.0)
# Net 1: Centerline at y=0
p1 = Port(0, 0, 0)
res1 = Straight.generate(p1, 50.0, width=2.0, dilation=1.0)
ce.add_path("net1", res1.collision_geometry, dilated_geometry=res1.dilated_collision_geometry)
# Net 2: Parallel to Net 1
# 1. Beyond minimum spacing: y=5. Gap = 5 - 2 = 3 > 2. OK.
p2_ok = Port(0, 5, 0)
res2_ok = Straight.generate(p2_ok, 50.0, width=2.0, dilation=1.0)
report_ok = ce.verify_path_report("net2", [res2_ok])
assert report_ok.is_valid, f"Gap 3 should be valid, but got {report_ok.collision_count} collisions"
# 2. Exactly at: y=4.0. Gap = 4.0 - 2.0 = 2.0. OK.
p2_exact = Port(0, 4, 0)
res2_exact = Straight.generate(p2_exact, 50.0, width=2.0, dilation=1.0)
report_exact = ce.verify_path_report("net2", [res2_exact])
assert report_exact.is_valid, f"Gap exactly 2.0 should be valid, but got {report_exact.collision_count} collisions"
# 3. Slightly violating: y=3.999. Gap = 3.999 - 2.0 = 1.999 < 2.0. FAIL.
p2_fail = Port(0, 3, 0)
res2_fail = Straight.generate(p2_fail, 50.0, width=2.0, dilation=1.0)
report_fail = ce.verify_path_report("net2", [res2_fail])
assert not report_fail.is_valid, "Gap 1.999 should be invalid"
assert report_fail.collision_count > 0
def test_verify_all_nets_cases():
"""
Validate that verify_all_nets catches some common cases and doesn't flag reasonable non-failing cases.
"""
engine = RoutingWorld(clearance=2.0)
danger_map = DangerMap(bounds=(0, 0, 100, 100))
danger_map.precompute([])
evaluator = CostEvaluator(collision_engine=engine, danger_map=danger_map)
# Case 1: Parallel paths exactly at clearance (Should be VALID)
netlist_parallel_ok = {
"net1": (Port(0, 50, 0), Port(100, 50, 0)),
"net2": (Port(0, 54, 0), Port(100, 54, 0)),
}
net_widths = {"net1": 2.0, "net2": 2.0}
results = _build_pathfinder(
evaluator,
bounds=(0, 0, 100, 100),
netlist=netlist_parallel_ok,
net_widths=net_widths,
congestion=CongestionOptions(warm_start_enabled=False, max_iterations=1),
).route_all()
assert results["net1"].is_valid, f"Exactly at clearance should be valid, collisions={results['net1'].collisions}"
assert results["net2"].is_valid
# Case 2: Parallel paths slightly within clearance (Should be INVALID)
netlist_parallel_fail = {
"net3": (Port(0, 20, 0), Port(100, 20, 0)),
"net4": (Port(0, 23, 0), Port(100, 23, 0)),
}
# Reset engine
engine.remove_path("net1")
engine.remove_path("net2")
results_p = _build_pathfinder(
evaluator,
bounds=(0, 0, 100, 100),
netlist=netlist_parallel_fail,
net_widths=net_widths,
congestion=CongestionOptions(warm_start_enabled=False, max_iterations=1),
).route_all()
# verify_all_nets should flag both as invalid because they cross-collide
assert not results_p["net3"].is_valid
assert not results_p["net4"].is_valid
# Case 3: Crossing paths (Should be INVALID)
netlist_cross = {
"net5": (Port(0, 75, 0), Port(100, 75, 0)),
"net6": (Port(50, 0, 90), Port(50, 100, 90)),
}
engine.remove_path("net3")
engine.remove_path("net4")
results_c = _build_pathfinder(
evaluator,
bounds=(0, 0, 100, 100),
netlist=netlist_cross,
net_widths=net_widths,
congestion=CongestionOptions(warm_start_enabled=False, max_iterations=1),
).route_all()
assert not results_c["net5"].is_valid
assert not results_c["net6"].is_valid