diff --git a/Cargo.lock b/Cargo.lock index cd39943..c26123e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2722,6 +2722,7 @@ dependencies = [ "nd_array", "num-integer", "rand", + "serde", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 8a052a1..280ce63 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,7 @@ nd_array = "0.1.0" num-integer = "0.1.45" rand = { version = "0.8.5", features = ["default", "small_rng"] } csv = "1.1" +serde = { version = "1.0.152", features = ["derive"] } [build-dependencies] cbindgen = "0.24.3" diff --git a/src/system/mod.rs b/src/system/mod.rs index 745afae..7cc21c7 100644 --- a/src/system/mod.rs +++ b/src/system/mod.rs @@ -1,4 +1,5 @@ use std::ops::Add; +use serde::{Serialize, Deserialize}; pub mod walker; pub mod storage; @@ -6,16 +7,23 @@ pub mod model; pub(crate) const DIM: u32 = 2; -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct Position(pub i32, pub i32); +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct Position { + x: i32, + y: i32, +} impl Position { + pub fn zero() -> Position { + Position { x: 0, y: 0 } + } + pub fn abs(&self) -> f32 { - ((self.0.pow(2) + self.1.pow(2)) as f32).powf(0.5) + ((self.x.pow(2) + self.y.pow(2)) as f32).powf(0.5) } pub fn in_direction(direction: u32, value: i32) -> Self { - if direction == 0 { Position(value, 0) } else { Position(0, value) } + if direction == 0 { Position { x: value, y: 0 } } else { Position { x: 0, y: value } } } pub fn neighbours(&self) -> impl Iterator + '_ { @@ -32,6 +40,6 @@ impl Add for Position { type Output = Position; fn add(self, rhs: Self) -> Self::Output { - Position(self.0 + rhs.0, self.1 + rhs.1) + Position { x: self.x + rhs.x, y: self.y + rhs.y } } } diff --git a/src/system/model.rs b/src/system/model.rs index 28b8fc6..1dc4b0b 100644 --- a/src/system/model.rs +++ b/src/system/model.rs @@ -7,6 +7,7 @@ use rand::prelude::*; use crate::system::{DIM, Position}; use crate::system::storage::{Storage, VectorStorage}; use crate::system::walker::{LocalRandomWalker, Walker}; +use serde::{Serialize, Deserialize}; pub struct DLASystem> { rng: R, @@ -49,7 +50,7 @@ impl> DLASystem { cluster_radius: 0.0, }; - sys.deposit(&Position(0, 0)); + sys.deposit(&Position::zero()); sys } @@ -97,7 +98,7 @@ impl> DLASystem { fn spawn_particle(&mut self) { let theta = self.rng.gen_range(0f32..1.0) * 2.0 * PI; let (x, y) = (self.add_circle * theta.cos(), self.add_circle * theta.sin()); - let position = Position(x as i32, y as i32); + let position = Position { x: x as i32, y: y as i32 }; if !self.storage.at(&position) { self.active_particle = Some(position); @@ -121,13 +122,13 @@ impl> DLASystem { } pub fn export_data(&self, path: &Path) -> io::Result<()> { - let mut file = File::create(path)?; - writeln!(&mut file, "x, y")?; + let mut wtr = csv::Writer::from_path(path)?; for particle in &self.particles { - writeln!(&mut file, "{}, {}", particle.0, particle.1)?; + wtr.serialize(particle)?; } + wtr.flush()?; Ok(()) } } diff --git a/src/system/storage.rs b/src/system/storage.rs index c005322..1223417 100644 --- a/src/system/storage.rs +++ b/src/system/storage.rs @@ -17,11 +17,11 @@ impl VectorStorage { } pub fn linear_index(&self, position: &Position) -> usize { - assert!(position.0 <= self.grid_size as i32 && -(self.grid_size as i32) <= position.0); - assert!(position.1 <= self.grid_size as i32 && -(self.grid_size as i32) <= position.1); + assert!(position.x <= self.grid_size as i32 && -(self.grid_size as i32) <= position.x); + assert!(position.y <= self.grid_size as i32 && -(self.grid_size as i32) <= position.y); - let x = (position.0 + (self.grid_size as i32) / 2) as usize; - let y = (position.1 + (self.grid_size as i32) / 2) as usize; + let x = (position.x + (self.grid_size as i32) / 2) as usize; + let y = (position.y + (self.grid_size as i32) / 2) as usize; return self.grid_size as usize * y + x }