syren 0.6.0

A parallel Rust framework for agent-based models with ECS storage, scheduling, messaging, environments, and optional GPU execution.
Documentation
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Continuous 2-D agent space backed by a uniform-grid CSR index.

use crate::engine::boundary::{BoundaryContext, BoundaryResource};
use crate::engine::entity::Entity;
use crate::engine::error::ECSResult;
use crate::engine::types::ChannelID;
use crate::engine::worker_stage::WorkerStage;

use super::geometry::GridGeometry;
use super::SpaceError;

/// One indexed agent: entity plus its normalised position.
type Item = (Entity, f32, f32);

/// Per-tick spatial index over agents at continuous 2-D positions.
///
/// Register on a model via
/// `ModelBuilder::register_continuous_space` (which also wires the reindex
/// system), or at the ECS level by constructing one, registering it with
/// [`ECSManager::register_boundary`](crate::ECSManager::register_boundary),
/// and staging positions from your own system via [`stage`](Self::stage).
///
/// Queries observe the snapshot built at the boundary after the reindex
/// system ran; see the [module docs](super) for the data-flow contract.
pub struct ContinuousSpace2D {
    geometry: GridGeometry,
    channels: [ChannelID; 1],
    deterministic: bool,

    stage: WorkerStage<Item>,
    /// Reused drain buffer.
    scratch: Vec<Item>,
    /// Reused counting buffer, one slot per cell.
    counts: Vec<u32>,

    /// CSR: `cell_starts[c]..cell_starts[c + 1]` indexes `items` for cell `c`.
    cell_starts: Vec<u32>,
    items: Vec<Item>,
}

impl ContinuousSpace2D {
    /// Creates a space with the given geometry, producing/finalising on
    /// `channel`.
    pub fn new(geometry: GridGeometry, channel: ChannelID) -> Result<Self, SpaceError> {
        geometry.validate()?;
        let total_cells = geometry.total_cells();
        Ok(Self {
            geometry,
            channels: [channel],
            deterministic: true,
            stage: WorkerStage::new(),
            scratch: Vec::new(),
            counts: vec![0; total_cells],
            cell_starts: vec![0; total_cells + 1],
            items: Vec::new(),
        })
    }

    /// Enables or disables the per-cell entity-id sort that makes iteration
    /// order independent of thread scheduling (default: enabled).
    #[must_use]
    pub fn with_determinism(mut self, deterministic: bool) -> Self {
        self.deterministic = deterministic;
        self
    }

    /// The scheduler channel this space finalises on.
    #[inline]
    pub fn channel(&self) -> ChannelID {
        self.channels[0]
    }

    /// The space's geometry.
    #[inline]
    pub fn geometry(&self) -> &GridGeometry {
        &self.geometry
    }

    /// Stages one agent's position for the next index build.
    ///
    /// Called from the reindex system (Phase A); lock-free per worker.
    /// Positions are normalised (torus wrap / plane clamp) at finalise.
    #[inline]
    pub fn stage(&self, entity: Entity, x: f32, y: f32) {
        self.stage.push((entity, x, y));
    }

    /// Number of agents in the current snapshot.
    #[inline]
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// `true` if the current snapshot contains no agents.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    /// Agents in one cell of the current snapshot.
    pub fn cell_items(&self, col: u32, row: u32) -> &[Item] {
        if col >= self.geometry.cols() || row >= self.geometry.rows() {
            return &[];
        }
        let cell = self.geometry.cell_index(col, row) as usize;
        let start = self.cell_starts[cell] as usize;
        let end = self.cell_starts[cell + 1] as usize;
        &self.items[start..end]
    }

    /// Iterates agents within `radius` of `(x, y)` (exact distance,
    /// torus-aware), yielding `(entity, (x, y))` in deterministic cell-major
    /// order. Zero allocations beyond the inline candidate-rect buffer.
    pub fn neighbors_within(
        &self,
        x: f32,
        y: f32,
        radius: f32,
    ) -> impl Iterator<Item = (Entity, (f32, f32))> + '_ {
        let (qx, qy) = self.geometry.normalise(x, y);
        let radius2 = radius * radius;
        self.geometry
            .candidate_rects(qx, qy, radius)
            .into_iter()
            .flat_map(move |(col_lo, col_hi, row_lo, row_hi)| {
                (row_lo..=row_hi).flat_map(move |row| {
                    (col_lo..=col_hi).flat_map(move |col| self.cell_items(col, row).iter())
                })
            })
            .filter(move |&&(_, ax, ay)| self.geometry.distance2(qx, qy, ax, ay) <= radius2)
            .map(|&(entity, ax, ay)| (entity, (ax, ay)))
    }

    /// Collects the `k` nearest agents to `(x, y)` into `out` as
    /// `(entity, squared_distance)`, nearest first (ties broken by entity
    /// id). `out` is cleared first and used as scratch, so a reused buffer
    /// makes repeated queries allocation-free.
    ///
    /// Expanding-ring search: cell rings are visited outward until `k`
    /// candidates are held and the next ring cannot beat the current k-th
    /// distance.
    pub fn nearest_k(&self, x: f32, y: f32, k: usize, out: &mut Vec<(Entity, f32)>) {
        out.clear();
        if k == 0 || self.items.is_empty() {
            return;
        }

        let (qx, qy) = self.geometry.normalise(x, y);
        let (centre_col, centre_row) = self.geometry.cell_of(qx, qy);
        let cols = self.geometry.cols() as i64;
        let rows = self.geometry.rows() as i64;
        // Beyond this ring every cell has been visited (torus rings start
        // re-wrapping onto themselves; planar rings fall fully outside).
        let max_ring = if self.geometry.torus {
            cols.max(rows) / 2 + 1
        } else {
            cols.max(rows)
        };

        for ring in 0..=max_ring {
            // Once k candidates are held, stop when even the nearest point of
            // this ring cannot beat the current k-th best.
            if out.len() >= k {
                let ring_min = ((ring - 1).max(0) as f32) * self.geometry.cell_size;
                let kth = out[k - 1].1;
                if ring_min * ring_min > kth {
                    break;
                }
            }

            let mut any_cell = false;
            self.for_each_ring_cell(centre_col as i64, centre_row as i64, ring, |col, row| {
                any_cell = true;
                for &(entity, ax, ay) in self.cell_items(col, row) {
                    let d2 = self.geometry.distance2(qx, qy, ax, ay);
                    out.push((entity, d2));
                }
            });
            if !any_cell && ring > 0 && !self.geometry.torus {
                break; // Bounded plane: ring fully outside the grid.
            }

            // Keep the best k: sort by (distance, entity id) - the id
            // tie-break also makes torus wrap double-visits of one entity
            // adjacent, so dedup removes them.
            out.sort_by(|a, b| {
                a.1.partial_cmp(&b.1)
                    .unwrap_or(std::cmp::Ordering::Equal)
                    .then_with(|| a.0.to_raw().cmp(&b.0.to_raw()))
            });
            out.dedup_by_key(|(entity, _)| entity.to_raw());
            out.truncate(k);
        }
    }

    /// Visits every cell on the square ring at Chebyshev distance `ring`
    /// from the centre (torus-wrapped; out-of-bounds cells skipped on
    /// bounded planes). Small torii may re-visit a wrapped cell; callers
    /// dedup by entity.
    fn for_each_ring_cell(
        &self,
        centre_col: i64,
        centre_row: i64,
        ring: i64,
        mut visit: impl FnMut(u32, u32),
    ) {
        let visit_cell = |col: i64, row: i64, visit: &mut dyn FnMut(u32, u32)| {
            if let Some((c, r)) = self.geometry.wrap_cell(col, row) {
                visit(c, r);
            }
        };
        if ring == 0 {
            visit_cell(centre_col, centre_row, &mut visit);
            return;
        }
        for col in (centre_col - ring)..=(centre_col + ring) {
            visit_cell(col, centre_row - ring, &mut visit);
            visit_cell(col, centre_row + ring, &mut visit);
        }
        for row in (centre_row - ring + 1)..=(centre_row + ring - 1) {
            visit_cell(centre_col - ring, row, &mut visit);
            visit_cell(centre_col + ring, row, &mut visit);
        }
    }

    /// Rebuilds the CSR index from staged positions (counting sort +
    /// optional per-cell determinism sort).
    fn rebuild(&mut self) {
        self.scratch.clear();
        self.stage.drain_into(&mut self.scratch);

        // Normalise positions once, here.
        for item in &mut self.scratch {
            let (x, y) = self.geometry.normalise(item.1, item.2);
            item.1 = x;
            item.2 = y;
        }

        let total_cells = self.geometry.total_cells();
        self.counts.clear();
        self.counts.resize(total_cells, 0);
        for &(_, x, y) in &self.scratch {
            let (col, row) = self.geometry.cell_of(x, y);
            self.counts[self.geometry.cell_index(col, row) as usize] += 1;
        }

        self.cell_starts.clear();
        self.cell_starts.resize(total_cells + 1, 0);
        for cell in 0..total_cells {
            self.cell_starts[cell + 1] = self.cell_starts[cell] + self.counts[cell];
        }

        self.items.clear();
        self.items
            .resize(self.scratch.len(), (Entity::PLACEHOLDER, 0.0, 0.0));
        let mut cursor: Vec<u32> = self.cell_starts[..total_cells].to_vec();
        for &(entity, x, y) in &self.scratch {
            let (col, row) = self.geometry.cell_of(x, y);
            let cell = self.geometry.cell_index(col, row) as usize;
            let slot = cursor[cell] as usize;
            self.items[slot] = (entity, x, y);
            cursor[cell] += 1;
        }

        if self.deterministic {
            for cell in 0..total_cells {
                let start = self.cell_starts[cell] as usize;
                let end = self.cell_starts[cell + 1] as usize;
                self.items[start..end].sort_unstable_by_key(|&(entity, _, _)| entity.to_raw());
            }
        }
    }
}

impl BoundaryResource for ContinuousSpace2D {
    fn name(&self) -> &str {
        "ContinuousSpace2D"
    }

    fn channels(&self) -> &[ChannelID] {
        &self.channels
    }

    fn begin_tick(&mut self, _ctx: &mut BoundaryContext<'_>) -> ECSResult<()> {
        // Defensive: the stage is normally drained by finalise; the snapshot
        // itself persists until the next reindex so pre-reindex systems see
        // last tick's positions (documented semantics).
        self.stage.clear();
        Ok(())
    }

    fn finalise(
        &mut self,
        _ctx: &mut BoundaryContext<'_>,
        channels: &[ChannelID],
    ) -> ECSResult<()> {
        if channels.contains(&self.channels[0]) {
            self.rebuild();
        }
        Ok(())
    }

    fn end_tick(&mut self, _ctx: &mut BoundaryContext<'_>) -> ECSResult<()> {
        Ok(())
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn space(torus: bool) -> ContinuousSpace2D {
        ContinuousSpace2D::new(
            GridGeometry {
                width: 100.0,
                height: 100.0,
                cell_size: 10.0,
                torus,
            },
            7,
        )
        .unwrap()
    }

    fn entity(raw: u64) -> Entity {
        Entity::from_raw(raw)
    }

    fn rebuild(space: &mut ContinuousSpace2D) {
        space.rebuild();
    }

    #[test]
    fn radius_query_matches_brute_force_including_torus_seam() {
        for torus in [false, true] {
            let mut s = space(torus);
            // Deterministic pseudo-random layout.
            let mut rng = crate::engine::random::DetRng::from_seed(42);
            let mut points = Vec::new();
            for raw in 0..500u64 {
                let x = rng.next_f32() * 100.0;
                let y = rng.next_f32() * 100.0;
                points.push((entity(raw), x, y));
                s.stage(entity(raw), x, y);
            }
            rebuild(&mut s);

            for &(qx, qy, r) in &[
                (5.0f32, 5.0f32, 12.0f32),
                (99.0, 1.0, 15.0),
                (50.0, 50.0, 7.5),
            ] {
                let mut expected: Vec<u64> = points
                    .iter()
                    .filter(|&&(_, x, y)| s.geometry().distance2(qx, qy, x, y) <= r * r)
                    .map(|&(e, _, _)| e.to_raw())
                    .collect();
                expected.sort_unstable();
                let mut observed: Vec<u64> = s
                    .neighbors_within(qx, qy, r)
                    .map(|(e, _)| e.to_raw())
                    .collect();
                observed.sort_unstable();
                assert_eq!(observed, expected, "torus={torus} query=({qx},{qy},{r})");
            }
        }
    }

    #[test]
    fn nearest_k_returns_sorted_nearest() {
        let mut s = space(false);
        for raw in 0..10u64 {
            s.stage(entity(raw), raw as f32 * 5.0, 0.0); // along a line
        }
        rebuild(&mut s);

        let mut out = Vec::new();
        s.nearest_k(0.0, 0.0, 3, &mut out);
        let ids: Vec<u64> = out.iter().map(|(e, _)| e.to_raw()).collect();
        assert_eq!(ids, vec![0, 1, 2]);
        assert!(out[0].1 <= out[1].1 && out[1].1 <= out[2].1);
    }

    #[test]
    fn snapshot_is_deterministic_within_cells() {
        let mut s = space(false);
        // Same cell, staged in scrambled order.
        for &raw in &[9u64, 3, 7, 1, 5] {
            s.stage(entity(raw), 1.0 + raw as f32 * 0.1, 1.0);
        }
        rebuild(&mut s);
        let ids: Vec<u64> = s
            .cell_items(0, 0)
            .iter()
            .map(|&(e, _, _)| e.to_raw())
            .collect();
        assert_eq!(ids, vec![1, 3, 5, 7, 9]);
    }

    #[test]
    fn rebuild_replaces_the_previous_snapshot() {
        let mut s = space(false);
        s.stage(entity(1), 5.0, 5.0);
        rebuild(&mut s);
        assert_eq!(s.len(), 1);

        // Next tick: nothing staged -> empty snapshot.
        rebuild(&mut s);
        assert!(s.is_empty());
        assert_eq!(s.neighbors_within(5.0, 5.0, 50.0).count(), 0);
    }
}