implement sin and cos table
This commit is contained in:
33
src/model.rs
33
src/model.rs
@@ -10,6 +10,7 @@ use indicatif::{ParallelProgressIterator, ProgressBar, ProgressStyle};
|
||||
use rand_distr::{Distribution, Normal};
|
||||
use rayon::{iter::ParallelIterator, prelude::*};
|
||||
use std::{path::Path, time::Instant};
|
||||
use fastapprox::faster::{cos, sin};
|
||||
|
||||
// Top-level simulation class.
|
||||
pub struct Model {
|
||||
@@ -94,6 +95,32 @@ impl Model {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gen_table(opt: i32) -> Vec<f32> {
|
||||
let mut output: Vec<f32> = Vec::new();
|
||||
|
||||
let max: f32 = 360.0;
|
||||
let min: f32 = -360.0;
|
||||
let interval: f32 = 0.01;
|
||||
|
||||
|
||||
let mut i: f32;
|
||||
let mut tmp: f32 = 0.0;
|
||||
for i1 in ((min/interval)as i32)..((max/interval)as i32) {
|
||||
i = (i1 as f32)*interval;
|
||||
if opt == 0 {
|
||||
tmp = sin(i);
|
||||
} else if opt == 1 {
|
||||
tmp = cos(i);
|
||||
}
|
||||
output.push(tmp);
|
||||
|
||||
}
|
||||
println!("{}", output.len());
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
// Simulates `steps` # of steps
|
||||
#[inline]
|
||||
pub fn run(&mut self, steps: usize) {
|
||||
@@ -112,6 +139,10 @@ impl Model {
|
||||
let mut time_per_step_list: Vec<f64> = Vec::new();
|
||||
|
||||
let agents_num: usize = self.grids.iter().map(|grid| grid.agents.len()).sum();
|
||||
let sin_table1 = Self::gen_table(0);
|
||||
let cos_table1 = Self::gen_table(1);
|
||||
let sin_table = sin_table1.clone();
|
||||
let cos_table = cos_table1.clone();
|
||||
for i in 0..steps {
|
||||
if debug {
|
||||
println!("Starting tick for all agents...")
|
||||
@@ -125,7 +156,7 @@ impl Model {
|
||||
// Tick agents
|
||||
let diffusivity = self.diffusivity;
|
||||
self.grids.par_iter_mut().for_each(|grid| {
|
||||
grid.tick();
|
||||
grid.tick(sin_table.clone(), cos_table.clone());
|
||||
grid.diffuse(diffusivity); // Diffuse + Decay
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user