Skip to main content

bima_rs/
system.rs

1use crate::body::Body;
2use crate::close_encounter::CloseEncounter;
3use crate::force::{self, ForceMethod};
4use crate::integrator::Integrator;
5use crate::timestep::{self, TimestepMethod};
6use crate::vec3::Vec3;
7use may::coroutine::{self, JoinHandle};
8use may::sync::mpsc::{self, Receiver};
9use std::collections::HashMap;
10use std::sync::mpsc::SendError;
11
12#[derive(Clone, Debug)]
13pub struct System {
14    pub t: f64,
15    pub bodies: Vec<Body>,
16    pub force_method: ForceMethod,
17    pub integrator: Integrator,
18    pub timestep_method: TimestepMethod,
19    pub close_encounter: CloseEncounter,
20    pub cache: HashMap<(usize, usize), Vec3>,
21}
22
23pub struct Data {
24    pub bodies: Option<Vec<Body>>,
25    pub percentage: f64,
26    pub t: f64,
27}
28
29impl Data {
30    fn new(bodies: Option<Vec<Body>>, percentage: f64, t: f64) -> Self {
31        Data {
32            bodies,
33            percentage,
34            t,
35        }
36    }
37}
38
39impl System {
40    pub fn new(
41        t: f64,
42        bodies: Vec<Body>,
43        force_method: ForceMethod,
44        solve_method: Integrator,
45        timestep_method: TimestepMethod,
46        close_encounter: CloseEncounter,
47    ) -> Self {
48        let mut system = System {
49            t,
50            bodies,
51            force_method,
52            integrator: solve_method,
53            timestep_method,
54            close_encounter,
55            cache: HashMap::new(),
56        };
57        for i in 0..system.bodies.len() {
58            system.bodies[i].a = force::calc(
59                &system.bodies[i],
60                &system.bodies,
61                &system.force_method,
62                &system.close_encounter,
63                Some(&mut system.cache),
64            );
65        }
66        system
67    }
68    pub fn clear_cache(&mut self) {
69        self.cache.clear();
70    }
71    pub fn integrate(
72        mut self,
73        t_stop: f64,
74    ) -> (Receiver<Data>, JoinHandle<Result<(), SendError<Data>>>) {
75        let (tx, rx) = mpsc::channel::<Data>();
76        let handle: JoinHandle<Result<(), SendError<Data>>> = unsafe {
77            coroutine::spawn(move || {
78                match self.timestep_method {
79                    TimestepMethod::Constant(dt) => {
80                        if dt <= 0.0 {
81                            return Ok(());
82                        }
83                        let mut tmp = Vec::new();
84                        let mut store = true;
85                        while self.t < t_stop {
86                            let percentage = self.t / t_stop;
87                            let bodies = if store {
88                                Some(self.bodies.clone())
89                            } else {
90                                None
91                            };
92                            let data = Data::new(bodies, percentage, self.t);
93                            tx.send(data)?;
94                            let proceed = timestep::constant_step(&mut self, dt, &mut tmp);
95                            if proceed {
96                                store = true;
97                                self.t += dt;
98                            } else {
99                                store = false;
100                            }
101                        }
102                    }
103                };
104                Ok(())
105            })
106        };
107        (rx, handle)
108    }
109}