Move to serde to allow for better coordinate changing
This commit is contained in:
parent
42b87c387c
commit
7e131af96d
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -2722,6 +2722,7 @@ dependencies = [
|
||||
"nd_array",
|
||||
"num-integer",
|
||||
"rand",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@ -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"
|
||||
|
||||
@ -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<Item=Self> + '_ {
|
||||
@ -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 }
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<R: Rng, S: Storage, W: Walker<R>> {
|
||||
rng: R,
|
||||
@ -49,7 +50,7 @@ impl<R: Rng, S: Storage, W: Walker<R>> DLASystem<R, S, W> {
|
||||
cluster_radius: 0.0,
|
||||
};
|
||||
|
||||
sys.deposit(&Position(0, 0));
|
||||
sys.deposit(&Position::zero());
|
||||
|
||||
sys
|
||||
}
|
||||
@ -97,7 +98,7 @@ impl<R: Rng, S: Storage, W: Walker<R>> DLASystem<R, S, W> {
|
||||
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<R: Rng, S: Storage, W: Walker<R>> DLASystem<R, S, W> {
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user