aboutsummaryrefslogtreecommitdiff
path: root/knowledge/tooling/placer.md
blob: 3e3493d913725417195575cd8ac36ddb341014e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Placer

`tools/placer/` places a board's footprints automatically by simulated annealing (Rust). The cost terms were learned from hand placement, and the [Rust router](rust-router.md) is the final judge: route the best candidates and pick by the routed result. [Place and route your own board](pnr.md) runs the whole chain.

## Run

``` sh
. tools/kicad_env.sh                                   # KPY, KC
cargo build --release --manifest-path tools/placer/Cargo.toml
$KC sch export netlist -o /tmp/b.net board.kicad_sch
$KPY tools/placer/export.py board.kicad_pcb /tmp/problem.json /tmp/b.net      # pin types from the netlist
P=tools/placer/target/release/placer
$P score /tmp/problem.json placement.toml [placement.json]   # cost terms and worst findings
$P run /tmp/problem.json placement.toml /tmp/out --runs 32 --inner 10     # rank_NN.json, runs.tsv
$P run ... --route board.json route.toml --route-top 32                   # also route and rank them
$P selftest /tmp/problem.json placement.toml                 # incremental cost == full evaluation
$KPY tools/placer/apply.py board.kicad_pcb /tmp/out/rank_00.json placed.kicad_pcb
```

`apply.py` sets positions and rotations, deletes tracks and vias, and restores the `.kicad_pro` that `SaveBoard` would reset. `PLACER_TRACE=1` prints one line per temperature.

`placer congest PROBLEM CONFIG [--cell MM] PLACEMENT...` prints RUDY overflow and a coarse global route per placement, for experiments.

**Rank by routing**: with `--route`, the best K placements are routed in-process after annealing and ranked:

1. legal placements first;
2. then split nets, failed connections and pour islands;
3. then the route.toml's `[rank]` score.

The output is `routed.tsv`, `routes_NN.json` and `routed_best.json`. `placer route PROBLEM CONFIG board.json route.toml OUT placement...` does the same for existing placement files.

## placement.toml

Most labels are inferred. The file only adds what cannot be:

- **Fixed:** locked footprints, board-only footprints and mounting holes are inferred. List more in `fixed = [...]`. A symbol field `Place_Fixed` works too.
- **Decoupling caps:** a C with one pad on a fill net (GND, or a net with a zone). Its pin is the nearest IC pin on the other net. Bulk caps (≥ 4.7 µF) may also sit at an L or FB.
  - "Nearest" is measured at the start position. If all parts start in a heap beside the IC, attach each IC decap explicitly: `[[attach]] part = "C12", pin = "U2.21"`.
- **Pin directions:**
  - 2-pad parts point along their axis.
  - ICs and connectors point by the side of the pad envelope; elongated pads decide first.
  - Centre and THT pads have none.
- **Fine-pitch fan-out band:** parts with at least 8 SMD pads closer than 0.7 mm get their pad envelope + 1 mm as a band that other parts' pads must stay out of.
- **Net weights:** by net class (`[class] Power = 0.5`), overridden by `[[net]] match = "glob", weight = x`.
- **Groups:** `[[group]] name, parts, bbox = [x0, y0, x1, y1]`. Members move together in group moves, and a bbox keeps them in a region. The symbol field `Place_Group` works too (`eth/xtal` is a subgroup of `eth`). The sidecar wins.
- **Links:** `[[link]] a = "NT1.2", b = "R11.2", max = 1.3` (`weight`, default 1). Two pads farther apart than `max` cost more, quadratically for the first 2 mm. Use it for a net tie on its Kelvin pad or an ESD diode at its pin.
- **Pulls:** `[[pull]] nets = ["*/SCLK", ...], side = "N"|"S"|"E"|"W"` (`weight`). Each pad on those nets costs per mm from that side of its group's bbox (else of the board). Use it for interface nets with a single pin on the block, which otherwise pull nowhere.
- **Replicated channels:** `[[replicate]] parts = [[CH1 parts], [CH2 parts], ...], pitch = [dx, dy]`. Channel k's part i follows channel 0's part i at k × pitch with the same rotation; same footprints are required.
- **Weights:** `[weights]` overrides the cost weights below. The defaults are in `src/model.rs`.

Example:

``` toml
fixed = ["U1", "J2"]

[class]
Ethernet = 3.0
Power = 0.5
Default = 1.0

[[group]]
name = "eth"
parts = ["U3", "Y1", "C20", "C21"]
bbox = [112.0, 60.0, 130.0, 80.0]

[[attach]]
part = "C5"
pin = "U1.19"

[[pull]]
nets = ["*/SCLK", "*/MOSI", "*/MISO"]
side = "S"
```

## Cost

| Term                    | What it stands for                                                                                                                                      |
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| length                  | MST ratsnest length × net weight                                                                                                                        |
| crossing                | crossings of ratsnest lines of different nets (a via each on 2 layers); each decap's line to its pin counts too, so caps keep pin order                 |
| block                   | ratsnest lines over another net's pads                                                                                                                  |
| escape                  | lines leaving a pin backwards                                                                                                                           |
| decap                   | distance cap → pin plus half ground pad → that IC's GND pin; beyond 3 mm (8 mm bulk) quadratic, then linear                                             |
| cover                   | IC pins on a decoupled net farther than 3 mm from any cap on it                                                                                         |
| overlap / band / bounds | courtyard overlap (courtyards grown 0.03 mm), pads in a fan-out band, outside the board / pad edge clearance / keep-outs / group bbox                    |
| link, pull              | `[[link]]` pads past their distance, `[[pull]]` pads' distance to their side                                                                            |

A move re-evaluates only what it touches: the nets of the moved parts, the ratsnest lines that changed, part pairs and part terms. `selftest` checks this against a full evaluation.

**Calibrate before trusting it.** Damage a good hand placement on purpose: swap caps, turn a ferrite, put a pull-up into a fan-out. Every variant must cost more. That is how the crossing, block and escape terms got their current form.

## Annealing

- **Moves:**
  - shift, turn, or shift + turn;
  - swap with a same-footprint part;
  - put a pad next to another pad of its net;
  - move an IC with its satellites (2-pad parts on its nets within 3 mm);
  - shift or turn a group;
  - **push**: shift a part together with every part whose courtyard it would hit, up to 8 parts.

  The grid is 0.05 mm, and single moves are clamped to the board.
- **Schedule:** VPR, with N^(4/3) × `inner` moves per temperature. Cooling and move range follow the acceptance rate.
- **Legality:** the legality terms ramp from 1 % to full weight while the temperature falls 100×. The best state is only taken at full weight, and a final greedy quench runs at 10×. Legal results rank first.
- **Stop:** when the best improved less than 0.1 % over 15 temperatures and acceptance is below 5 %.

What went wrong first, so it does not again:

- Parts ended off the board: early moves span the board diagonal, and a flat "outside" penalty gives no gradient. Fixed by clamping and a distance term.
- T0 was huge because a scrambled start's overlaps dominate. Fixed by measuring T0 at the ramp start and a linear tail on the decap limit.
- An IC got stuck by its caps: a cluster of all 2-pad parts on its nets included everything on 3V3. Satellites are now only parts within 3 mm.
- DRC found touching courtyards, a cap in an antenna keep-out and a pad too close to the edge. Fixed with a courtyard margin, pad keep-outs from rule areas, and bounds from the outline polygon instead of the Edge.Cuts bounding box.

## Experience

- Speed: about 45k moves/s per core. Quality grows with run length, not run count: 32 runs at `--inner 10` (about 7 s each on a 50-part board) beat many short runs.
- On a dense ESP32 + Ethernet board the annealer found the hand layout's structure by itself: Ethernet IC central, termination toward the jack, crystal close, buck at the side.
- **Lower placer cost does not mean better routing.** Coarse group regions taken from a designer's layout routed better (23/32 vs 17/32 complete) than a free placement with lower cost. Keep regions until the cost knows about quiet planes and feed directions.
- **The push move** fixes a class of local minimum: several parts that must move together because any one alone hits a neighbour's courtyard.
- When shrinking a block, sweep the group bbox and compare the routed `[rank]` scores. Do not stop at the first size that still places.
- Not modelled, so check by hand: Kelvin taps, and references that `silk_refs.py` cannot fit (those go to F.Fab).
- Footprints with bottom-side pads (KiCad `_ThermalVias` variants) are refused. Use the plain footprint and [Thermal vias](thermal-vias.md) after routing.

Related: [Placement check](placement-check.md), [Router (Python)](router.md), [Placement for routing](../topics/placement-for-routing.md).