henad_core/model.rs
1//! The [`SimState`] interface the runner drives.
2
3use crate::params::ParamValue;
4use crate::send_sync::WasmNotSend;
5use crate::view::{EdgeView, GridView, PointView, StatEntry};
6
7/// A running simulation, as the runner drives it.
8///
9/// The trait is object-safe, and a model entry type-erases every model behind it. A model implements
10/// one of the authoring traits, and the engine implements this trait.
11pub trait SimState: WasmNotSend + 'static {
12 /// Advances the state by one tick.
13 fn step(&mut self);
14 /// Number of ticks stepped so far.
15 fn tick(&self) -> u64;
16 /// Returns the grid layer to draw, if the state has one.
17 ///
18 /// A state can return both a grid and agents from [`SimState::point_view`], and the agents are then drawn
19 /// over the field.
20 fn grid_view(&self) -> Option<GridView<'_>> {
21 None
22 }
23 /// Returns the agents to draw, if the state has any.
24 fn point_view(&self) -> Option<PointView<'_>> {
25 None
26 }
27 /// Returns the edges to draw between the positions from [`SimState::point_view`].
28 fn edge_view(&self) -> Option<EdgeView<'_>> {
29 None
30 }
31 /// Prepares the views before a snapshot is built.
32 ///
33 /// A model turns its state into something drawable here, without paying for it every tick.
34 fn prepare_view(&mut self) {}
35 /// Returns the current statistics, one entry per declared series.
36 fn stats(&self) -> Vec<StatEntry>;
37 /// Sets parameter `index` to `value`, and returns whether the state accepted the edit.
38 ///
39 /// A state rejects an edit to a parameter declared [`ParamApply::OnReload`].
40 ///
41 /// [`ParamApply::OnReload`]: crate::params::ParamApply::OnReload
42 fn set_param(&mut self, index: usize, value: &ParamValue) -> bool;
43 /// Runs the model's action at `index`, between ticks, and returns whether it ran.
44 ///
45 /// It returns false when the model declares no such action.
46 fn act(&mut self, _index: usize) -> bool {
47 false
48 }
49 /// Turns the layout on or off, with a time budget per publish in milliseconds.
50 ///
51 /// Returns false if the state has no layout.
52 fn set_layout(&mut self, _on: bool, _budget_ms: f32) -> bool {
53 false
54 }
55 /// Runs the layout for one time budget, if it is on.
56 ///
57 /// The runner decides which publishes call this.
58 fn relax_layout(&mut self) {}
59 /// Size of the population: its cells, its agents or its live nodes.
60 fn population(&self) -> u64;
61 /// Approximate size in bytes of the memory this state owns, on the host for a CPU state and on the device
62 /// for a GPU state.
63 fn heap_bytes(&self) -> usize;
64 /// Number of jobs that one step splits into. `None` if a backend has no such split.
65 fn parallel_jobs(&self) -> Option<usize> {
66 None
67 }
68}