use rand::rngs::StdRng;
use rand::SeedableRng;
use rustsim::prelude::*;
#[derive(Debug, Clone)]
struct Particle {
id: AgentId,
x: f64,
vx: f64,
}
impl Agent for Particle {
fn id(&self) -> AgentId {
self.id
}
}
type ParticleModel = StandardModel<
rustsim_spaces::nothing::NothingSpace,
Particle,
HashMapStore<Particle>,
(),
StdRng,
Fastest,
>;
fn particle_step(
agent: &mut Particle,
_ctx: &mut StepContext<
'_,
rustsim_spaces::nothing::NothingSpace,
Particle,
(),
StdRng,
Fastest,
>,
) {
agent.x += agent.vx;
}
fn main() {
let mut store = HashMapStore::new();
for i in 1..=1000u64 {
store.insert(Particle {
id: i,
x: 0.0,
vx: i as f64 * 0.01,
});
}
let mut model = ParticleModel::new(
store,
rustsim_spaces::nothing::NothingSpace,
Fastest::new(),
(),
StdRng::seed_from_u64(42),
Some(Box::new(particle_step)),
None,
true,
);
println!("Running 100 steps with 1000 particles...");
model.step_n(100);
println!("Time: {}", model.time());
let ids: Vec<AgentId> = model.agents().map(|a| a.id()).collect();
let mut snapshots: Vec<(AgentId, f64)> = Vec::new();
let mut model_data: Vec<Time> = Vec::new();
collect_step(
&model,
&ids,
Some(&|agent: &Particle, _: &ParticleModel| (agent.id, agent.x)),
Some(&|m: &ParticleModel| m.time()),
&mut snapshots,
&mut model_data,
);
let mut sorted: Vec<_> = snapshots.iter().collect();
sorted.sort_by_key(|(id, _)| *id);
println!("\nSample agent positions after 100 steps:");
for (id, x) in sorted.iter().take(5) {
println!(" Agent {id}: x = {x:.2}");
}
println!(" ...");
for (id, x) in sorted.iter().rev().take(3).collect::<Vec<_>>().iter().rev() {
println!(" Agent {id}: x = {x:.2}");
}
println!("\nTotal agents: {}", model.agents_len());
}