60 lines
1.8 KiB
Rust
60 lines
1.8 KiB
Rust
use std::f64::consts::E;
|
|
use std::fs::File;
|
|
use std::io;
|
|
use std::io::Write;
|
|
use std::time::Instant;
|
|
use crate::bounds::Bounds;
|
|
use crate::function::Function;
|
|
use crate::linear_bounds::LinearBounds;
|
|
use crate::plot::Plot;
|
|
|
|
mod point;
|
|
mod linear_bounds;
|
|
mod bounds;
|
|
mod function;
|
|
mod plot;
|
|
|
|
const THREAD_CNT: usize = 20;
|
|
const SAMPLE_LIMIT: usize = 100_000_000;
|
|
const SUB_SAMPLES: [usize; 3] = [1, 2, 5];
|
|
const SAMPLES_PER_ITERATION: usize = 250;
|
|
|
|
fn f(x: f64) -> f64 {
|
|
E.powf(x) * (1.0 / x.sin()).cos() + x.powf(2.0)
|
|
}
|
|
|
|
fn main() {
|
|
let bounds: Bounds = Bounds::new(LinearBounds::new(3.1_f64, 3.2_f64), LinearBounds::new(-10_f64, 35_f64));
|
|
let func: Function = Function::new(f);
|
|
|
|
//let start: Instant = Instant::now();
|
|
//println!("{}\n{}ms", func.approximate(&bounds, 10_000_000, THREAD_CNT), start.elapsed().as_millis())
|
|
|
|
simulate(&bounds, &func)
|
|
}
|
|
|
|
fn simulate(bounds: &Bounds, func: &Function) {
|
|
let mut result: Plot = Plot::new();
|
|
|
|
let mut sample_cnt: usize = 1;
|
|
while sample_cnt < SAMPLE_LIMIT {
|
|
for sub_sample in SUB_SAMPLES.clone() {
|
|
let samples: usize = sub_sample * sample_cnt;
|
|
for sample in 0..SAMPLES_PER_ITERATION {
|
|
result.add_sample(samples, func.approximate(&bounds, samples, THREAD_CNT));
|
|
let progress: usize = (((sample + 1) as f64 / SAMPLES_PER_ITERATION as f64) * 25.0) as usize;
|
|
eprint!("\r{:12} [{}{}]", samples, "|".repeat(progress), " ".repeat(25 - progress));
|
|
}
|
|
println!();
|
|
}
|
|
sample_cnt *= 10;
|
|
|
|
let file: io::Result<File> = File::create(format!("output{}.json", sample_cnt.ilog10()));
|
|
if file.is_err() {
|
|
panic!("Error creating output file")
|
|
}
|
|
|
|
if let Err(_) = file.unwrap().write(result.to_json().to_string().as_bytes()) { panic!("Error writing to file") };
|
|
}
|
|
}
|