chladni.rs
3.1 kB · rust · 94 lines
1use mrlycore::json::parse;2use mrlycore::Tensor;3use mrlydemo::chladni::*;4use mrlydemo::life::life_noise;5use mrlymath::life::{design_mask, next_grid, Boundary};6use mrlymath::two::Cell2d;78fn agree(9 code: u128,10 side: usize,11 level: usize,12 birth: (f64, f64),13 survive: (f64, f64),14 born: &[usize],15 kept: &[usize],16) -> usize {17 let size = 32;18 let mask = design_mask(2, code, side, level).unwrap();19 let mut fast = life_noise(size, size, 0.5, 11);20 let mut slow = Cell2d::new(Tensor::of(fast.clone(), vec![size, size]));21 let name = code.to_string();22 for step in 0..8 {23 fast = chladni_next(24 &fast, size, &name, side, level, birth.0, birth.1, survive.0, survive.1,25 )26 .unwrap();27 slow = next_grid(&slow, born, kept, &mask, Boundary::Wrap).unwrap();28 assert_eq!(fast.as_slice(), slow.types().bytes(), "step {step}");29 }30 fast.iter().filter(|&&t| t != 0).count()31}3233#[test]34fn the_fft_step_is_the_crate_step_on_the_moore_mask() {35 let live = agree(7, 3, 1, (0.375, 0.375), (0.25, 0.375), &[3], &[2, 3]);36 assert!(live > 0);37}3839#[test]40fn the_fft_step_is_the_crate_step_on_the_level_two_carpet() {41 let born: Vec<usize> = (18..=24).collect();42 let kept: Vec<usize> = (18..=30).collect();43 let live = agree(7, 3, 2, (0.28, 0.38), (0.28, 0.48), &born, &kept);44 assert!(live > 0);45}4647#[test]48fn the_kernel_picture_centres_the_mask() {49 let picture = chladni_kernel("7", 3, 2, 32).unwrap();50 assert_eq!((picture.width, picture.height), (32, 32));51 assert_eq!(52 picture.types.iter().map(|&t| usize::from(t)).sum::<usize>(),53 6454 );55 assert_eq!(picture.types[16 * 32 + 16], 0);56 assert_eq!(picture.types[12 * 32 + 12], 1);57 assert_eq!(picture.types[11 * 32 + 12], 0);58 assert!(chladni_kernel("7", 3, 4, 64).is_err());59 assert!(chladni_kernel("7", 3, 1, 12).is_err());60}6162#[test]63fn the_spectrum_and_profile_read_a_plain_stripe() {64 let size = 32;65 let types: Vec<u8> = (0..size * size)66 .map(|i| u8::from((i % size) % 4 < 2))67 .collect();68 let spectrum = chladni_spectrum(&types, size).unwrap();69 assert_eq!(spectrum.len(), size * size);70 assert_eq!(spectrum[16 * 32 + 16], 1.0);71 let read = parse(&chladni_profile(&types, size).unwrap()).unwrap();72 assert_eq!(read["peak_ring"], 8);73 assert_eq!(read["wavelength"], 4.0);74 assert_eq!(read["profile"].as_array().unwrap().len(), 17);75 assert!(chladni_spectrum(&types, 31).is_err());76 assert!(chladni_profile(&types[1..], size).is_err());77}7879fn pinned(density: f64) -> (usize, u64) {80 let grid = chladni_run("7", 3, 3, 0.28, 0.375, 0.28, 0.48, 128, 32, density, 7).unwrap();81 let live = grid.types.iter().filter(|&&t| t != 0).count();82 let read = parse(&chladni_profile(&grid.types, 128).unwrap()).unwrap();83 println!(84 "chladni_run 7 3 3 0.28 0.375 0.28 0.48 128 32 {density} 7 live {live} peak_ring {} wavelength {}",85 read["peak_ring"], read["wavelength"]86 );87 (live, read["peak_ring"].as_u64().unwrap())88}8990#[test]91fn the_pinned_soups_and_their_profiles() {92 assert_eq!(pinned(0.5), (0, 1));93 assert_eq!(pinned(0.45), (5259, 3));94}