320 lines
16 KiB
Python
320 lines
16 KiB
Python
import pickle
|
|
from copy import deepcopy
|
|
from dataclasses import replace
|
|
|
|
import pytest
|
|
from shapely import union_all
|
|
from shapely.geometry import Polygon, box
|
|
|
|
from inire import (
|
|
Bend90Seed, CongestionOptions, FixedRoute, NetSpec, PathSeed, Port, RefinementOptions,
|
|
RoutingOptions, RoutingProblem, SBendOutline, SBendSeed, SearchOptions, StraightSeed, TopologyOptions, route,
|
|
)
|
|
from inire.layout import PlacedPort, coplanar_geometry, partition_layers, preserves_length, route_profiles, verify_export_grid
|
|
|
|
|
|
_REPLAY = RoutingOptions(search=SearchOptions(node_limit=0), refinement=RefinementOptions(enabled=False))
|
|
|
|
|
|
def test_declared_port_width_check_requires_transition_for_wider_device() -> None:
|
|
source = PlacedPort(Port(10000, 10000, 0), 500)
|
|
target = PlacedPort(Port(110000, 10000, 0), 3000)
|
|
with pytest.raises(ValueError, match="joins widths 500 and 3000"):
|
|
source.connect("wide", target)
|
|
|
|
inlet = PlacedPort(Port(70000, 10000, 0), 500)
|
|
net = source.connect("before_taper", inlet)
|
|
assert net.width == 500
|
|
assert net.target == inlet.port
|
|
rotated = replace(source, port=source.port.rotated(90)).connect("rotated", replace(inlet, port=inlet.port.rotated(90)))
|
|
assert rotated.start.r == rotated.target.r == 90
|
|
|
|
|
|
@pytest.mark.parametrize(("outer_radius", "grid", "valid"), [(3070, 1, False), (3070, 0.01, True), (3072, 1, True)])
|
|
def test_export_verifies_actual_rounded_bend_clearance(outer_radius, grid, valid) -> None:
|
|
center_x, center_y = 3000, 6070
|
|
inner = NetSpec("inside", Port(center_x, center_y - 2500, 0), Port(center_x + 2500, center_y, 90), 490)
|
|
outer = NetSpec("outside", Port(center_x, center_y - outer_radius, 0), Port(center_x + outer_radius, center_y, 90), 509.94)
|
|
seeds = {"inside": PathSeed((Bend90Seed(2500, "CCW"),)), "outside": PathSeed((Bend90Seed(outer_radius, "CCW"),))}
|
|
problem = RoutingProblem((0, 0, 7000, 7000), (inner, outer), initial_paths=seeds, clearance=70)
|
|
options = replace(_REPLAY, search=replace(_REPLAY.search, bend_radii=(2500, outer_radius)))
|
|
results = route(problem, options=options).results_by_net
|
|
assert all(result.is_valid for result in results.values())
|
|
|
|
exported = verify_export_grid(problem, results, grid=grid)
|
|
|
|
assert exported.is_valid is valid
|
|
assert all(not report.discontinuous for report in exported.reports_by_net.values())
|
|
assert all(report.dynamic_collision_count == int(not valid) for report in exported.reports_by_net.values())
|
|
|
|
|
|
def test_export_rejects_stale_net_specs_and_accepts_legacy_results() -> None:
|
|
net = NetSpec("track", Port(10, 20, 0), Port(90, 20, 0))
|
|
problem = RoutingProblem((0, 0, 100, 50), (net,))
|
|
results = route(problem).results_by_net
|
|
result = results["track"]
|
|
assert result.requested_net == net
|
|
assert result.effective_net == net
|
|
for stale in (replace(net, width=6), replace(net, target=Port(90, 20, 90))):
|
|
with pytest.raises(ValueError, match="different"):
|
|
verify_export_grid(replace(problem, nets=(stale,)), results, grid=1)
|
|
legacy = replace(result, requested_net=None, effective_net=None)
|
|
assert verify_export_grid(problem, {"track": legacy}, grid=1).is_valid
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["automatic", "off", "guide", "seed"])
|
|
@pytest.mark.parametrize(("start_x", "y", "valid"), [(10, 22, False), (40, 22, False), (40, 23, True)])
|
|
def test_imported_fixed_routes_hold_clearance_at_edges_and_vertices(mode, start_x, y, valid) -> None:
|
|
net = NetSpec("new", Port(start_x, y, 0), Port(90, y, 0))
|
|
fixed = FixedRoute("existing", (box(10, 19, 40, 21),))
|
|
problem = RoutingProblem((0, 0, 100, 50), (net,), clearance=0, fixed_routes=(fixed,))
|
|
options = RoutingOptions(
|
|
search=SearchOptions(node_limit=20, bend_radii=(), sbend_radii=()),
|
|
topology=TopologyOptions(mode=mode),
|
|
)
|
|
|
|
results = route(problem, options=options).results_by_net
|
|
|
|
assert set(results) == {"new"}
|
|
assert results["new"].is_valid is valid
|
|
if valid:
|
|
assert verify_export_grid(problem, results, grid=1).is_valid
|
|
|
|
|
|
def test_fixed_route_from_result_preserves_polygon_and_owner() -> None:
|
|
net = NetSpec("first", Port(10, 20, 0), Port(90, 20, 0))
|
|
problem = RoutingProblem((0, 0, 100, 50), (net,))
|
|
fixed = FixedRoute.from_result(route(problem).results_by_net["first"])
|
|
assert fixed.net_id == net.net_id
|
|
assert fixed.polygons[0].equals(box(10, 19, 90, 21))
|
|
with pytest.raises(ValueError, match="Duplicate active or fixed"):
|
|
replace(problem, fixed_routes=(fixed,))
|
|
|
|
|
|
@pytest.mark.parametrize(("keepout_y", "clearance", "valid"), [(21, 0.2, False), (21.2, 0.4, False), (21.25, 0.2, True)])
|
|
def test_export_checks_keepout_clearance_smaller_than_the_grid(keepout_y, clearance, valid) -> None:
|
|
net = NetSpec("signal", Port(10, 20, 0), Port(90, 20, 0), 1.4)
|
|
keepout = box(20, keepout_y, 30, 24)
|
|
problem = RoutingProblem((0, 0, 100, 50), (net,), (keepout,), clearance=clearance)
|
|
results = route(problem).results_by_net
|
|
assert results[net.net_id].is_valid
|
|
|
|
export = verify_export_grid(problem, results, grid=1)
|
|
|
|
assert export.geometry_by_net[net.net_id][0].bounds == (10, 19, 90, 21)
|
|
assert export.is_valid is valid
|
|
assert export.reports_by_net[net.net_id].static_collision_count == int(not valid)
|
|
|
|
|
|
@pytest.mark.parametrize(("pitch", "valid"), [(28000, False), (32000, False), (36000, True)])
|
|
def test_cpw_ground_strip_is_verified_using_gap_envelope(pitch, valid) -> None:
|
|
width = 15500
|
|
nets = tuple(NetSpec(f"cpw{i}", Port(10000, 30000 + i * pitch, 0), Port(210000, 30000 + i * pitch, 0), width)
|
|
for i in range(2))
|
|
seeds = {net.net_id: PathSeed((StraightSeed(200000),)) for net in nets}
|
|
problem = RoutingProblem((0, 0, 220000, 100000), nets, initial_paths=seeds, clearance=3000)
|
|
signals = route(problem, options=_REPLAY).results_by_net
|
|
envelopes = route(replace(problem, nets=tuple(replace(net, width=29500) for net in nets)), options=_REPLAY).results_by_net
|
|
assert all(signal.is_valid for signal in signals.values())
|
|
assert all(envelope.is_valid for envelope in envelopes.values()) == valid
|
|
if valid:
|
|
shapes = [coplanar_geometry(signals[net.net_id], envelopes[net.net_id]) for net in nets]
|
|
assert shapes[0].ground_exclusion.distance(shapes[1].ground_exclusion) == 6500
|
|
assert all(shape.gaps.area == 14000 * 200000 for shape in shapes)
|
|
|
|
|
|
def test_cpw_turn_builds_wider_exclusion_from_the_verified_envelope() -> None:
|
|
radius = 15000
|
|
net = NetSpec("flux", Port(35000, 20000, 0), Port(50000, 35000, 90), 15500)
|
|
problem = RoutingProblem((0, 0, 100000, 80000), (net,), initial_paths={"flux": PathSeed((Bend90Seed(radius, "CCW"),))})
|
|
options = replace(_REPLAY, search=SearchOptions(node_limit=0, bend_radii=(radius,), sbend_radii=()))
|
|
signal = route(problem, options=options).results_by_net["flux"]
|
|
envelope = route(replace(problem, nets=(replace(net, width=29500),)), options=options).results_by_net["flux"]
|
|
|
|
cpw = coplanar_geometry(signal, envelope, ground_margin=10000)
|
|
|
|
assert cpw.ground_exclusion.covers(union_all(envelope.locked_geometry))
|
|
assert cpw.gaps.area > 0
|
|
|
|
|
|
@pytest.mark.parametrize("gap", [0, 0.5])
|
|
def test_cpw_checks_for_side_contact_between_terminal_faces(gap) -> None:
|
|
signal_outline = Polygon([(0, -1), (3, -1), (12, 5), (15, 5), (15, 7), (12, 7), (3, 1), (0, 1)])
|
|
envelope_outline = Polygon([(0, -2), (3, -1 - gap), (12, 5 - gap), (15, 4), (15, 8), (12, 8), (3, 2), (0, 2)])
|
|
net = NetSpec("signal", Port(10, 20, 0), Port(25, 26, 0), 2)
|
|
problem = RoutingProblem((0, 0, 100, 50), (net,), initial_paths={net.net_id: PathSeed((SBendSeed(6, 10),))})
|
|
options = replace(_REPLAY, search=replace(_REPLAY.search, sbend_outlines=(
|
|
SBendOutline(6, 10, 2, signal_outline), SBendOutline(6, 10, 4, envelope_outline),
|
|
)))
|
|
signal = route(problem, options=options).results_by_net[net.net_id]
|
|
envelope = route(replace(problem, nets=(replace(net, width=4),)), options=options).results_by_net[net.net_id]
|
|
assert signal.is_valid
|
|
assert envelope.is_valid
|
|
|
|
with pytest.raises(ValueError, match="nonempty gap"):
|
|
coplanar_geometry(signal, signal)
|
|
if gap:
|
|
assert coplanar_geometry(signal, envelope).gaps.area > 0
|
|
else:
|
|
with pytest.raises(ValueError, match="closes away from the terminal faces"):
|
|
coplanar_geometry(signal, envelope)
|
|
|
|
|
|
def test_cpw_coupling_etch_keeps_two_signal_sections_separate() -> None:
|
|
# The two nets abut opposite faces of the authored 5 µm interruption.
|
|
nets = (
|
|
NetSpec("before", Port(10000, 30000, 0), Port(97500, 30000, 0), 15500),
|
|
NetSpec("after", Port(102500, 30000, 0), Port(210000, 30000, 0), 15500),
|
|
)
|
|
seeds = {"before": PathSeed((StraightSeed(87500),)), "after": PathSeed((StraightSeed(107500),))}
|
|
problem = RoutingProblem((0, 0, 220000, 60000), nets, initial_paths=seeds)
|
|
run = route(problem, options=_REPLAY)
|
|
export = verify_export_grid(problem, run.results_by_net, grid=1)
|
|
assert export.is_valid
|
|
assembled = union_all([polygon for polygons in export.geometry_by_net.values() for polygon in polygons])
|
|
|
|
original_metal = box(10000, 22250, 210000, 37750)
|
|
coupling_etch = box(97500, 22250, 102500, 37750)
|
|
assert assembled.equals(original_metal.difference(coupling_etch))
|
|
assert len(assembled.geoms) == 2
|
|
assert assembled.union(coupling_etch).equals(original_metal)
|
|
|
|
|
|
def test_profile_options_apply_to_active_search_and_seed_replay() -> None:
|
|
flux = NetSpec("flux", Port(35000, 20000, 0), Port(50000, 35000, 90), 29500)
|
|
feed = NetSpec("feed", Port(100000, 20000, 0), Port(115000, 35000, 90), 29500)
|
|
problem = RoutingProblem((0, 0, 220000, 95000), (flux, feed), clearance=3000)
|
|
options = RoutingOptions(
|
|
search=SearchOptions(node_limit=80, bend_radii=(15000,), sbend_radii=()),
|
|
refinement=RefinementOptions(enabled=False),
|
|
congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False),
|
|
topology=TopologyOptions(mode="off"),
|
|
)
|
|
feed_profile = replace(options, search=replace(options.search, bend_radii=(50000,)))
|
|
profiles = ((("flux",), options), (("feed",), feed_profile))
|
|
|
|
restricted = route_profiles(problem, profiles)
|
|
|
|
assert restricted["flux"].is_valid
|
|
assert not restricted["feed"].is_valid
|
|
feed = replace(feed, target=Port(150000, 70000, 90))
|
|
open_board = replace(problem, nets=(flux, feed))
|
|
both = route_profiles(open_board, profiles)
|
|
assert both["feed"].as_seed() == PathSeed((Bend90Seed(50000, "CCW"),))
|
|
assert all(result.is_valid for result in both.values())
|
|
invalid = replace(open_board, initial_paths={"feed": PathSeed((Bend90Seed(15000, "CCW"),))})
|
|
with pytest.raises(ValueError, match="'feed'.*outside bend_radii"):
|
|
route_profiles(invalid, profiles)
|
|
|
|
|
|
@pytest.mark.parametrize(("second_y", "valid"), [(22, False), (23, True)])
|
|
def test_profile_reserves_replayed_net_against_zero_clearance_contact(second_y, valid) -> None:
|
|
first = NetSpec("first", Port(10, 20, 0), Port(90, 20, 0))
|
|
second = NetSpec("second", Port(10, second_y, 0), Port(90, second_y, 0))
|
|
seeds = {net.net_id: PathSeed((StraightSeed(80),)) for net in (first, second)}
|
|
problem = RoutingProblem((0, 0, 100, 50), (first, second), initial_paths=seeds, clearance=0)
|
|
|
|
results = route_profiles(problem, [(("first",), _REPLAY), (("second",), _REPLAY)])
|
|
|
|
assert results["first"].is_valid
|
|
assert results["second"].is_valid is valid
|
|
if valid:
|
|
assert verify_export_grid(problem, results, grid=1).is_valid
|
|
|
|
|
|
def test_independent_layer_outputs_retain_assignment_and_connectivity() -> None:
|
|
silicon = NetSpec("si", Port(1000, 10000, 0), Port(19000, 10000, 0), 500)
|
|
nitride = NetSpec("sin", Port(10000, 1000, 90), Port(10000, 19000, 90), 750)
|
|
geometry = {}
|
|
for layer, nets in partition_layers((((1, 0), silicon), ((4, 0), nitride))).items():
|
|
problem = RoutingProblem(
|
|
(0, 0, 20000, 20000), nets,
|
|
initial_paths={net.net_id: PathSeed((StraightSeed(18000),)) for net in nets},
|
|
)
|
|
results = route(problem, options=_REPLAY).results_by_net
|
|
export = verify_export_grid(problem, results, grid=1)
|
|
assert export.is_valid
|
|
geometry[layer] = union_all(tuple(polygon for shapes in export.geometry_by_net.values() for polygon in shapes))
|
|
assert set(geometry) == {(1, 0), (4, 0)}
|
|
assert all(shape.geom_type == "Polygon" for shape in geometry.values())
|
|
assert geometry[1, 0].intersection(geometry[4, 0]).area > 0
|
|
|
|
|
|
def test_authored_template_lengths_are_used_for_candidate_acceptance() -> None:
|
|
net = NetSpec("arm", Port(20, 20, 0), Port(30, 30, 90))
|
|
problem = RoutingProblem((0, 0, 100, 100), (net,))
|
|
outline = box(0, -11, 11, -9).union(box(9, -11, 11, 0))
|
|
options = RoutingOptions(search=SearchOptions(
|
|
node_limit=100, bend_radii=(10,), bend_physical_geometry=outline, bend_physical_length=18.7,
|
|
))
|
|
result = route(problem, options=options).results_by_net["arm"]
|
|
assert preserves_length(result, 18.7, tolerance=1e-6)
|
|
assert not preserves_length(result, 37.4, tolerance=1e-6)
|
|
assert result.report.total_length == 18.7
|
|
|
|
|
|
def test_authored_sbend_length_survives_seed_replay() -> None:
|
|
polygon = box(0, -1, 80, 41).intersection(
|
|
box(0, -1, 80, 1).union(box(1, 0, 79, 41)).union(box(79, 39, 80, 41)),
|
|
)
|
|
outline = SBendOutline(40, 50, 2, polygon, length=95)
|
|
net = NetSpec("shift", Port(10, 10, 0), Port(90, 50, 0))
|
|
seed = PathSeed(())
|
|
search = SearchOptions(node_limit=80, bend_radii=(), sbend_radii=(50,), sbend_outlines=(outline,))
|
|
problem = RoutingProblem((0, 0, 110, 110), (net,))
|
|
options = RoutingOptions(search=search)
|
|
result = route(problem, options=options).results_by_net["shift"]
|
|
assert result.as_seed() != seed
|
|
replay = route(
|
|
replace(problem, initial_paths={"shift": result.as_seed()}),
|
|
options=replace(options, search=replace(search, node_limit=0)),
|
|
).results_by_net["shift"]
|
|
assert preserves_length(replay, 95, tolerance=0)
|
|
|
|
|
|
def test_export_and_routing_results_are_detached_snapshots() -> None:
|
|
track = NetSpec("track", Port(10, 20, 0), Port(90, 20, 0))
|
|
seeds = {"track": PathSeed((StraightSeed(80),))}
|
|
problem = RoutingProblem((0, 0, 100, 50), (track,), initial_paths=seeds)
|
|
seeds.clear()
|
|
assert tuple(problem.initial_paths) == ("track",)
|
|
|
|
run = route(problem, options=_REPLAY, iteration_callback=lambda _, results: results.clear())
|
|
original = dict(run.results_by_net)
|
|
export = verify_export_grid(problem, original, grid=1)
|
|
original.clear()
|
|
|
|
with pytest.raises(TypeError):
|
|
problem.initial_paths["track"] = PathSeed(())
|
|
with pytest.raises(TypeError):
|
|
del run.results_by_net["track"]
|
|
with pytest.raises(TypeError):
|
|
export.geometry_by_net["track"] = (box(0, 0, 1, 1),)
|
|
with pytest.raises(TypeError):
|
|
del export.reports_by_net["track"]
|
|
with pytest.raises(TypeError):
|
|
del export.checks_by_net["track"]
|
|
|
|
copied = dict(export.geometry_by_net)
|
|
copied.clear()
|
|
assert tuple(export.geometry_by_net) == ("track",)
|
|
assert export.is_valid
|
|
assert run.results_by_net["track"].is_valid
|
|
|
|
|
|
def test_read_only_snapshots_survive_python_copy_and_pickle() -> None:
|
|
track = NetSpec("track", Port(10, 20, 0), Port(90, 20, 0))
|
|
problem = RoutingProblem((0, 0, 100, 50), (track,))
|
|
run = route(problem)
|
|
export = verify_export_grid(problem, run.results_by_net, grid=1)
|
|
for copy in (deepcopy, lambda value: pickle.loads(pickle.dumps(value))):
|
|
assert copy(problem) == problem
|
|
assert copy(export) == export
|
|
restored = copy(run)
|
|
assert restored == run
|
|
with pytest.raises(TypeError):
|
|
del restored.results_by_net["track"]
|
|
source = dict(run.results_by_net)
|
|
replacement = replace(run, results_by_net=source)
|
|
source.clear()
|
|
assert replacement.results_by_net == run.results_by_net
|