Compare commits

...

11 Commits

10 changed files with 542 additions and 81 deletions

281
Cargo.lock generated
View File

@@ -8,12 +8,33 @@ version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "aligned-vec" name = "aligned-vec"
version = "0.5.0" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4aa90d7ce82d4be67b64039a3d588d38dbcc6736577de4a847025ce5b0c468d1" checksum = "4aa90d7ce82d4be67b64039a3d588d38dbcc6736577de4a847025ce5b0c468d1"
[[package]]
name = "anes"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
[[package]]
name = "anstyle"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9"
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.97" version = "1.0.97"
@@ -120,6 +141,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495" checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495"
[[package]]
name = "cast"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.2.17" version = "1.2.17"
@@ -147,6 +174,58 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "ciborium"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e"
dependencies = [
"ciborium-io",
"ciborium-ll",
"serde",
]
[[package]]
name = "ciborium-io"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757"
[[package]]
name = "ciborium-ll"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9"
dependencies = [
"ciborium-io",
"half",
]
[[package]]
name = "clap"
version = "4.5.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e958897981290da2a852763fe9cdb89cd36977a5d729023127095fa94d95e2ff"
dependencies = [
"clap_builder",
]
[[package]]
name = "clap_builder"
version = "4.5.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83b0f35019843db2160b5bb19ae09b4e6411ac33fc6a712003c33e03090e2489"
dependencies = [
"anstyle",
"clap_lex",
]
[[package]]
name = "clap_lex"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6"
[[package]] [[package]]
name = "color_quant" name = "color_quant"
version = "1.1.0" version = "1.1.0"
@@ -175,6 +254,42 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "criterion"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f"
dependencies = [
"anes",
"cast",
"ciborium",
"clap",
"criterion-plot",
"is-terminal",
"itertools 0.10.5",
"num-traits",
"once_cell",
"oorandom",
"plotters",
"rayon",
"regex",
"serde",
"serde_derive",
"serde_json",
"tinytemplate",
"walkdir",
]
[[package]]
name = "criterion-plot"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1"
dependencies = [
"cast",
"itertools 0.10.5",
]
[[package]] [[package]]
name = "crossbeam-deque" name = "crossbeam-deque"
version = "0.8.6" version = "0.8.6"
@@ -319,6 +434,12 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hermit-abi"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbd780fe5cc30f81464441920d82ac8740e2e46b29a6fad543ddd075229ce37e"
[[package]] [[package]]
name = "image" name = "image"
version = "0.25.6" version = "0.25.6"
@@ -393,6 +514,26 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "is-terminal"
version = "0.4.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9"
dependencies = [
"hermit-abi",
"libc",
"windows-sys",
]
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.12.1" version = "0.12.1"
@@ -411,6 +552,12 @@ dependencies = [
"either", "either",
] ]
[[package]]
name = "itoa"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
[[package]] [[package]]
name = "jobserver" name = "jobserver"
version = "0.1.32" version = "0.1.32"
@@ -592,9 +739,15 @@ checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.21.2" version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2806eaa3524762875e21c3dcd057bc4b7bfa01ce4da8d46be1cd43649e1cc6b" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "oorandom"
version = "11.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"
[[package]] [[package]]
name = "paste" name = "paste"
@@ -606,6 +759,7 @@ checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
name = "physarum" name = "physarum"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"criterion",
"fastapprox", "fastapprox",
"image", "image",
"indicatif", "indicatif",
@@ -621,6 +775,34 @@ version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
[[package]]
name = "plotters"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747"
dependencies = [
"num-traits",
"plotters-backend",
"plotters-svg",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "plotters-backend"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a"
[[package]]
name = "plotters-svg"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670"
dependencies = [
"plotters-backend",
]
[[package]] [[package]]
name = "png" name = "png"
version = "0.17.16" version = "0.17.16"
@@ -847,6 +1029,35 @@ dependencies = [
"crossbeam-utils", "crossbeam-utils",
] ]
[[package]]
name = "regex"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]] [[package]]
name = "rgb" name = "rgb"
version = "0.8.50" version = "0.8.50"
@@ -859,6 +1070,21 @@ version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2" checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2"
[[package]]
name = "ryu"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.219" version = "1.0.219"
@@ -879,6 +1105,18 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "serde_json"
version = "1.0.140"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373"
dependencies = [
"itoa",
"memchr",
"ryu",
"serde",
]
[[package]] [[package]]
name = "serde_spanned" name = "serde_spanned"
version = "0.6.8" version = "0.6.8"
@@ -976,6 +1214,16 @@ dependencies = [
"weezl", "weezl",
] ]
[[package]]
name = "tinytemplate"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
dependencies = [
"serde",
"serde_json",
]
[[package]] [[package]]
name = "toml" name = "toml"
version = "0.8.20" version = "0.8.20"
@@ -1039,6 +1287,16 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "852e951cb7832cb45cb1169900d19760cfa39b82bc0ea9c0e5a14ae88411c98b" checksum = "852e951cb7832cb45cb1169900d19760cfa39b82bc0ea9c0e5a14ae88411c98b"
[[package]]
name = "walkdir"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
dependencies = [
"same-file",
"winapi-util",
]
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"
@@ -1112,6 +1370,16 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "web-sys"
version = "0.3.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]] [[package]]
name = "web-time" name = "web-time"
version = "1.1.0" version = "1.1.0"
@@ -1128,6 +1396,15 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082" checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082"
[[package]]
name = "winapi-util"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [
"windows-sys",
]
[[package]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.59.0" version = "0.59.0"

View File

@@ -13,6 +13,13 @@ rand_distr = "0.5"
rayon = "1.10" rayon = "1.10"
fastapprox = "0.3" fastapprox = "0.3"
[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
[[bench]]
name = "benchmark"
harness = false
[profile.release] [profile.release]
codegen-units = 1 codegen-units = 1
opt-level = 3 opt-level = 3

107
benches/benchmark.rs Normal file
View File

@@ -0,0 +1,107 @@
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
use physarum::{
agent::Agent,
grid::{combine, Grid},
model,
};
use rand::{rngs::StdRng, SeedableRng};
// Benchmark agent movement and deposition
fn agent_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("Agent Tick");
let n_agents = [1_000, 10_000, 100_000];
for &n in &n_agents {
group.bench_with_input(BenchmarkId::from_parameter(n), &n, |b, &n| {
let mut rng = StdRng::seed_from_u64(42);
let agents = (0..n).map(|_| Agent::new(256, 256, &mut rng)).collect();
let mut grid = Grid::new(256, 256, &mut rng, agents);
b.iter(|| {
grid.tick();
});
});
}
group.finish();
}
// Benchmark grid diffusion (blur)
fn diffusion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("Grid Diffusion");
let sizes = [(256, 256), (512, 512)];
let radii = [1, 3];
for &(w, h) in &sizes {
for &r in &radii {
group.bench_with_input(
BenchmarkId::new("diffuse", format!("{}x{}_r{}", w, h, r)),
&(w, h, r),
|b, &(w, h, r)| {
b.iter_batched(
|| {
let mut rng = StdRng::seed_from_u64(42);
Grid::new(w, h, &mut rng, vec![])
},
|mut grid| grid.diffuse(r),
BatchSize::SmallInput,
);
},
);
}
}
group.finish();
}
// Benchmark grid combining
fn combine_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("Combine Grids");
let populations = [2, 4];
for &np in &populations {
group.bench_with_input(BenchmarkId::from_parameter(np), &np, |b, &np| {
b.iter_batched(
|| {
let mut rng = StdRng::seed_from_u64(42);
let grids = (0..np)
.map(|_| Grid::new(256, 256, &mut rng, vec![]))
.collect::<Vec<_>>();
let attraction_table = vec![vec![1.0; np]; np];
(grids, attraction_table)
},
|(mut grids, table)| combine(&mut grids, &table),
BatchSize::SmallInput,
);
});
}
group.finish();
}
// Benchmark full model step
fn model_step_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("Model Step");
let params = [(256, 256, 2), (512, 512, 4)];
for &(w, h, np) in &params {
group.bench_with_input(
BenchmarkId::new("step", format!("{}x{}_p{}", w, h, np)),
&(w, h, np),
|b, &(w, h, np)| {
b.iter_batched(
|| model::Model::new(w, h, 1 << 16, np, 1),
|mut model| model.step(),
BatchSize::SmallInput,
);
},
);
}
group.finish();
}
criterion_group!(
benches,
agent_benchmark,
diffusion_benchmark,
combine_benchmark,
model_step_benchmark
);
criterion_main!(benches);

View File

@@ -1,3 +1,4 @@
use crate::grid::PopulationConfig;
use crate::{buffer::Buf, util::wrap}; use crate::{buffer::Buf, util::wrap};
use fastapprox::faster::{cos, sin}; use fastapprox::faster::{cos, sin};
use rand::prelude::IndexedRandom; use rand::prelude::IndexedRandom;
@@ -31,26 +32,17 @@ impl Agent {
} }
/// Tick an agent /// Tick an agent
pub fn tick( pub fn tick(&mut self, buf: &Buf, pop_config: PopulationConfig, width: usize, height: usize) {
&mut self, let xc = self.x + cos(self.heading) * pop_config.sensor_distance;
buf: &Buf, let yc = self.y + sin(self.heading) * pop_config.sensor_distance;
sensor_distance: f32,
sensor_angle: f32,
rotation_angle: f32,
step_distance: f32,
width: usize,
height: usize,
) {
let xc = self.x + cos(self.heading) * sensor_distance;
let yc = self.y + sin(self.heading) * sensor_distance;
let agent_add_sens = self.heading + sensor_angle; let agent_add_sens = self.heading + pop_config.sensor_angle;
let agent_sub_sens = self.heading - sensor_angle; let agent_sub_sens = self.heading - pop_config.sensor_angle;
let xl = self.x + cos(agent_sub_sens) * sensor_distance; let xl = self.x + cos(agent_sub_sens) * pop_config.sensor_distance;
let yl = self.y + sin(agent_sub_sens) * sensor_distance; let yl = self.y + sin(agent_sub_sens) * pop_config.sensor_distance;
let xr = self.x + cos(agent_add_sens) * sensor_distance; let xr = self.x + cos(agent_add_sens) * pop_config.sensor_distance;
let yr = self.y + sin(agent_add_sens) * sensor_distance; let yr = self.y + sin(agent_add_sens) * pop_config.sensor_distance;
// We sense from the buffer because this is where we previously combined data from all the grid. // We sense from the buffer because this is where we previously combined data from all the grid.
let center = buf.get_buf(xc, yc); let center = buf.get_buf(xc, yc);
@@ -72,10 +64,16 @@ impl Agent {
0.0 0.0
}; };
let delta_angle = rotation_angle * direction; let delta_angle = pop_config.rotation_angle * direction;
self.heading = wrap(self.heading + delta_angle, TAU); self.heading = wrap(self.heading + delta_angle, TAU);
self.x = wrap(self.x + step_distance * cos(self.heading), width as f32); self.x = wrap(
self.y = wrap(self.y + step_distance * sin(self.heading), height as f32); self.x + pop_config.step_distance * cos(self.heading),
width as f32,
);
self.y = wrap(
self.y + pop_config.step_distance * sin(self.heading),
height as f32,
);
} }
} }

View File

@@ -30,18 +30,17 @@ impl Blur {
/// Approximate 1D Gaussian filter of standard deviation sigma with N box filter passes. Each element in the output array contains the radius of the box filter for the corresponding pass. /// Approximate 1D Gaussian filter of standard deviation sigma with N box filter passes. Each element in the output array contains the radius of the box filter for the corresponding pass.
fn boxes_for_gaussian<const N: usize>(sigma: f32) -> [usize; N] { fn boxes_for_gaussian<const N: usize>(sigma: f32) -> [usize; N] {
let w_ideal = (12.0 * sigma * sigma / N as f32 + 1.0).sqrt(); let sigma_sq = sigma.powi(2);
let w_ideal = (12.0 * sigma_sq / N as f32 + 1.0).sqrt();
let mut w = w_ideal as usize; let mut w = w_ideal as usize;
w -= 1 - (w & 1); w -= 1 - (w & 1);
let mut m = 0.25 * (N * (w + 3)) as f32; let m = (0.25 * (N * (w + 3)) as f32 - 3.0 * sigma_sq / (w + 1) as f32).round() as usize;
m -= 3.0 * sigma * sigma / (w + 1) as f32;
let m = m.round() as usize;
let mut result = [0; N]; (0..N)
for (i, value) in result.iter_mut().enumerate() { .map(|i| (w + 1 - 2 * (i < m) as usize) / 2)
*value = (if i < m { w - 1 } else { w + 1 }) / 2; .collect::<Vec<_>>()
} .try_into()
result .unwrap()
} }
/// Perform one pass of the 2D box filter of the given radius. The result will be written to the src slice, while the buf slice is used as a scratch space. /// Perform one pass of the 2D box filter of the given radius. The result will be written to the src slice, while the buf slice is used as a scratch space.
@@ -67,7 +66,7 @@ impl Blur {
.for_each(|(src_row, dst_row)| { .for_each(|(src_row, dst_row)| {
// First we build a value for the beginning of each row. We assume periodic boundary conditions, so we need to push the left index to the opposite side of the row. // First we build a value for the beginning of each row. We assume periodic boundary conditions, so we need to push the left index to the opposite side of the row.
let width_sub_radius = width - radius; let width_sub_radius = width - radius;
let mut value = src_row[width - radius - 1]; let mut value = src_row[width_sub_radius - 1];
for j in 0..radius { for j in 0..radius {
value += src_row[width_sub_radius + j] + src_row[j]; value += src_row[width_sub_radius + j] + src_row[j];
} }

View File

@@ -7,10 +7,10 @@ use std::fmt::{Display, Formatter};
/// A population configuration. /// A population configuration.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct PopulationConfig { pub struct PopulationConfig {
sensor_distance: f32, pub sensor_distance: f32,
step_distance: f32, pub step_distance: f32,
sensor_angle: f32, pub sensor_angle: f32,
rotation_angle: f32, pub rotation_angle: f32,
deposition_amount: f32, deposition_amount: f32,
} }
@@ -90,25 +90,8 @@ impl Grid {
} }
pub fn tick(&mut self) { pub fn tick(&mut self) {
let (width, height) = (self.width, self.height);
let PopulationConfig {
sensor_distance,
sensor_angle,
rotation_angle,
step_distance,
..
} = self.config;
self.agents.par_iter_mut().for_each(|agent| { self.agents.par_iter_mut().for_each(|agent| {
agent.tick( agent.tick(&self.buf, self.config, self.width, self.height);
&self.buf,
sensor_distance,
sensor_angle,
rotation_angle,
step_distance,
width,
height,
);
}); });
self.deposit_all(); self.deposit_all();
} }
@@ -131,11 +114,14 @@ where
// We mutate grid buffers and read grid data. We use unsafe because we need shared/unique borrows on different fields of the same Grid struct. // We mutate grid buffers and read grid data. We use unsafe because we need shared/unique borrows on different fields of the same Grid struct.
bufs.iter().enumerate().for_each(|(i, buf)| { bufs.iter().enumerate().for_each(|(i, buf)| {
let buf_ptr = *buf as *const Vec<f32> as *mut Vec<f32>; let buf_ptr = *buf as *const Vec<f32> as *mut Vec<f32>;
unsafe { buf_ptr.as_mut() }.unwrap().fill(0.0); // SAFETY! we can take these are raw pointers because we are
// getting it from a `&mut [Grid]`
let buf_ptr_mut = unsafe { buf_ptr.as_mut().unwrap_unchecked() };
buf_ptr_mut.fill(0.0);
datas.iter().enumerate().for_each(|(j, other)| { datas.iter().enumerate().for_each(|(j, other)| {
let multiplier = attraction_table[i].as_ref()[j]; let multiplier = attraction_table[i].as_ref()[j];
unsafe { buf_ptr.as_mut() } buf_ptr_mut
.unwrap()
.iter_mut() .iter_mut()
.zip(*other) .zip(*other)
.for_each(|(to, from)| *to += from * multiplier) .for_each(|(to, from)| *to += from * multiplier)

View File

@@ -1,7 +1,7 @@
mod agent; pub mod agent;
mod blur; mod blur;
mod buffer; mod buffer;
mod grid; pub mod grid;
pub mod imgdata; // for storing image data pub mod imgdata; // for storing image data
pub mod model; pub mod model;
mod palette; mod palette;

View File

@@ -29,9 +29,7 @@ fn main() {
"-i", "-i",
"-", "-",
"-c:v", "-c:v",
"libx264", "libsvtav1",
"-preset",
"fast",
"output.mp4", "output.mp4",
]) ])
.stdin(std::process::Stdio::piped()) .stdin(std::process::Stdio::piped())

View File

@@ -57,28 +57,32 @@ impl Model {
let attraction_distr = let attraction_distr =
Normal::new(Self::ATTRACTION_FACTOR_MEAN, Self::ATTRACTION_FACTOR_STD).unwrap(); Normal::new(Self::ATTRACTION_FACTOR_MEAN, Self::ATTRACTION_FACTOR_STD).unwrap();
let repulstion_distr = let repulsion_distr =
Normal::new(Self::REPULSION_FACTOR_MEAN, Self::REPULSION_FACTOR_STD).unwrap(); Normal::new(Self::REPULSION_FACTOR_MEAN, Self::REPULSION_FACTOR_STD).unwrap();
let mut attraction_table = Vec::with_capacity(n_populations); let mut attraction_table = Vec::with_capacity(n_populations);
for i in 0..n_populations { for i in 0..n_populations {
attraction_table.push(Vec::with_capacity(n_populations)); attraction_table.push(Vec::with_capacity(n_populations));
for j in 0..n_populations { for j in 0..n_populations {
attraction_table[i].push(if i == j { attraction_table[i].push(
attraction_distr.sample(&mut rng) if i == j {
} else { &attraction_distr
repulstion_distr.sample(&mut rng) } else {
}); &repulsion_distr
}
.sample(&mut rng),
);
} }
} }
let mut grids: Vec<Grid> = Vec::new(); let grids = (0..n_populations)
for _ in 0..n_populations { .map(|_| {
let agents = (0..particles_per_grid) let agents = (0..particles_per_grid)
.map(|_| Agent::new(width, height, &mut rng)) .map(|_| Agent::new(width, height, &mut rng))
.collect(); .collect();
grids.push(Grid::new(width, height, &mut rng, agents)); Grid::new(width, height, &mut rng, agents)
} })
.collect();
Model { Model {
population_grids: grids, population_grids: grids,

View File

@@ -17,10 +17,95 @@ pub const fn index(width: usize, height: usize, x: f32, y: f32) -> usize {
mod test { mod test {
use super::*; use super::*;
#[test] mod wrap {
fn wrap_test() { use super::*;
assert_eq!(wrap(1.1, 1.0), 0.100000024); // floating point weirdness #[test]
assert_eq!(wrap(0.5, 1.0), 0.5); fn over() {
assert_eq!(wrap(-1.0, 2.0), 1.0); assert_eq!(wrap(1.1, 1.0), 0.100000024); // floating point weirdness
}
#[test]
fn middle() {
assert_eq!(wrap(0.5, 1.0), 0.5);
}
#[test]
fn under() {
assert_eq!(wrap(-1.0, 2.0), 1.0);
}
}
mod index {
use super::*;
#[test]
fn basic_positive_coordinates() {
let width = 4;
let height = 4;
assert_eq!(index(width, height, 1.5, 2.5), 9);
}
#[test]
fn negative_x_coordinate() {
let width = 8;
let height = 8;
assert_eq!(index(width, height, -3.2, 5.6), 44);
}
#[test]
fn exact_boundary_values() {
let width = 16;
let height = 16;
assert_eq!(index(width, height, 16.0, 0.0), 0);
}
#[test]
fn large_coordinates() {
let width = 2;
let height = 2;
assert_eq!(index(width, height, 1000.0, 2000.0), 0);
}
#[test]
fn negative_x_and_y() {
let width = 4;
let height = 4;
assert_eq!(index(width, height, -1.5, -0.5), 14);
}
#[test]
fn fractional_truncation() {
let width = 4;
let height = 4;
assert_eq!(index(width, height, 3.9, 3.999), 15);
}
#[test]
fn zero_coordinates() {
let width = 4;
let height = 4;
assert_eq!(index(width, height, 0.0, 0.0), 0);
}
#[test]
fn x_equals_width() {
let width = 8;
let height = 8;
assert_eq!(index(width, height, 8.0, 0.0), 0);
}
#[test]
fn y_negative_beyond_height() {
let width = 4;
let height = 4;
assert_eq!(index(width, height, 0.0, -4.5), 0);
}
#[test]
fn width_and_height_one() {
let width = 1;
let height = 1;
assert_eq!(index(width, height, 123.4, -56.7), 0);
}
} }
} }