Skip to main content

powerio_synth/
lattice.rs

1//! Square 2-D lattice / grid topology. `n` is rounded up to the nearest
2//! perfect square.
3
4use rand::SeedableRng;
5use rand_chacha::ChaCha8Rng;
6
7use powerio_tx::BalancedNetwork;
8
9use super::{SynthSpec, make_branch, make_buses, net};
10
11pub fn generate_lattice(spec: &SynthSpec) -> BalancedNetwork {
12    let side = ((spec.n as f64).sqrt().ceil() as usize).max(2);
13    let n = side * side;
14    let mut rng = ChaCha8Rng::seed_from_u64(spec.seed);
15
16    let buses = make_buses(n);
17    let mut branches = Vec::with_capacity(2 * side * (side - 1));
18    for r in 0..side {
19        for c in 0..side {
20            let idx = r * side + c;
21            if c + 1 < side {
22                branches.push(make_branch(idx + 1, idx + 2, spec, &mut rng));
23            }
24            if r + 1 < side {
25                branches.push(make_branch(idx + 1, idx + side + 1, spec, &mut rng));
26            }
27        }
28    }
29
30    net(format!("synth_lattice_{side}x{side}"), buses, branches)
31}