small refinements

This commit is contained in:
Niko Abeler 2022-11-17 20:28:32 +01:00
parent 8759a58cb9
commit d91e829bff
2 changed files with 31 additions and 34 deletions

View File

@ -1,8 +1,8 @@
use std::thread;
use std::sync::{Arc, RwLock};
use rand::Rng;
use std::fs::File;
use std::io::prelude::*;
use std::sync::{Arc, RwLock};
use std::thread;
mod utils;
@ -69,7 +69,6 @@ fn read_graph(file_name: &str) -> (usize, EdgeMatrix) {
}
fn main() -> std::io::Result<()> {
const C_REP: f32 = 0.1;
const C_SPRING: f32 = 0.1;
const ITER: usize = 200;
@ -81,7 +80,9 @@ fn main() -> std::io::Result<()> {
let nodes = nodes_list(size);
let nodes_next = nodes_list(size);
let chunks = utils::chunk_borders(size, THREADS);
let opt_dist = 10.0 / (size as f32).sqrt();
let chunks = utils::gen_chunks(size, THREADS);
for epoch in 0..ITER {
let mut handles = vec![];
for i in 0..THREADS {
@ -111,28 +112,26 @@ fn main() -> std::io::Result<()> {
let d_x = o_x - node_x;
let d_y = o_y - node_y;
let dist = (d_x * d_x + d_y * d_y).sqrt().max(0.01);
let dist = (d_x * d_x + d_y * d_y).sqrt().max(0.0001);
let unit_x = d_x / dist;
let unit_y = d_y / dist;
let f_rep = C_REP/(dist).powi(2);
let edge = edges[n][o].weight;
if edge == 0.0 {
let f_rep = -C_REP / (dist).powi(2);
let f_rep_x = f_rep * unit_x;
let f_rep_y = f_rep * unit_y;
node_x += f_rep_x;
node_y += f_rep_y;
let edge = edges[n][o].weight;
if edge > 0.0 {
let f_spring = C_SPRING * (dist / edge).log(2.0);
} else {
let f_spring = C_SPRING * (dist / opt_dist).log(2.0);
let f_spring_x = f_spring * unit_x;
let f_spring_y = f_spring * unit_y;
node_x += f_spring_x;
node_y += f_spring_y;
}
}
let mut result = nodes_next[n].write().unwrap();
result.x = node_x;
@ -153,7 +152,6 @@ fn main() -> std::io::Result<()> {
node.y = node_next.y;
}
let mut file = File::create(format!("result/{:04}.txt", epoch))?;
for i in 0..size {
let node = nodes[i].read().unwrap();
@ -163,5 +161,4 @@ fn main() -> std::io::Result<()> {
}
Ok(())
}

View File

@ -1,6 +1,6 @@
use std::ops::Range;
pub fn chunk_borders(n: usize, chunks: usize) -> Vec<Range<usize>> {
pub fn gen_chunks(n: usize, chunks: usize) -> Vec<Range<usize>> {
let mut borders = vec![];
let chunk_size = n / chunks;
let mut start = 0;
@ -20,20 +20,20 @@ mod test {
use super::*;
#[test]
fn test_chunk_borders() {
let borders = chunk_borders(10, 3);
fn test_gen_chunks() {
let borders = gen_chunks(10, 3);
assert_eq!(borders, vec![(0..3), (3..6), (6..10)]);
}
#[test]
fn test_chunk_borders2() {
let borders = chunk_borders(10, 2);
fn test_gen_chunks2() {
let borders = gen_chunks(10, 2);
assert_eq!(borders, vec![(0..5), (5..10)]);
}
#[test]
fn test_chunk_borders3() {
let borders = chunk_borders(10, 1);
fn test_gen_chunks3() {
let borders = gen_chunks(10, 1);
assert_eq!(borders, vec![(0..10)]);
}