Skip to content

Commit

Permalink
Fine-tuning the algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
romancardenas committed Oct 17, 2023
1 parent e0b9fca commit a052bd8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ select where you want to take advantage of parallelism:
We provide additional features to select handy combinations of features:

- `par_xxc`: alias for `par_eoc` and `par_xic` (we **DO NOT** recommend this feature, it is likely to be removed).
- `par_sim_no_xcc`: alias for `par_collection` and `par_transition`.
- `par_sim`: alias for `par_xxc` and `par_sim_no_xcc` (we **DO NOT** recommend this feature, it is likely to be removed).
- `par_all_no_xcc`: alias for `par_start`, `par_sim_no_xcc`, and `par_stop` (**this is our favourite**).
- `par_sim_no_xxc`: alias for `par_collection` and `par_transition`.
- `par_sim`: alias for `par_xxc` and `par_sim_no_xxc` (we **DO NOT** recommend this feature, it is likely to be removed).
- `par_all_no_xxc`: alias for `par_start`, `par_sim_no_xxc`, and `par_stop` (**THIS IS OUR FAVOURITE**).
- `par_all`: alias for `par_xxc` and `par_all_no_xcc` (we **DO NOT** recommend this feature, it is likely to be removed).

## Work in progress 👷‍♀️👷👷‍♂️
Expand Down
10 changes: 8 additions & 2 deletions examples/devstone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ fn main() {
.parse()
.expect("depth could not be parsed");

let int_delay = 0;
let ext_delay = 0;
let int_delay = match args.get(4) {
Some(arg) => arg.parse().expect("int delay could not be parsed"),
None => 0,
};
let ext_delay = match args.get(5) {
Some(arg) => arg.parse().expect("ext delay could not be parsed"),
None => int_delay,
};

let start = Instant::now();
let coupled = match model_type.as_str() {
Expand Down
24 changes: 11 additions & 13 deletions src/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub trait Simulator: DynRef {
}

/// It starts the simulation, setting the initial time to t_start.
fn start(&mut self, t_start: f64);
fn start(&mut self, t_start: f64) -> f64;

/// It stops the simulation, setting the last time to t_stop.
fn stop(&mut self, t_stop: f64);
Expand All @@ -71,10 +71,11 @@ impl<T: Atomic + DynRef> Simulator for T {
Atomic::get_component_mut(self)
}

fn start(&mut self, t_start: f64) {
fn start(&mut self, t_start: f64) -> f64 {
Atomic::start(self);
let ta = self.ta();
self.set_sim_t(t_start, t_start + ta);
let t_next = t_start + self.ta();
self.set_sim_t(t_start, t_next);
t_next
}

fn stop(&mut self, t_stop: f64) {
Expand Down Expand Up @@ -125,17 +126,14 @@ impl Simulator for Coupled {
/// method and obtain the next simulation time.
///
/// If the feature `par_start` is activated, the iteration is parallelized.
fn start(&mut self, t_start: f64) {
fn start(&mut self, t_start: f64) -> f64 {
#[cfg(feature = "par_start")]
let iter = self.components.par_iter_mut();
#[cfg(not(feature = "par_start"))]
let iter = self.components.iter_mut();
// we obtain the minimum next time of all the subcomponents
let t_next = iter
.map(|c| {
c.start(t_start);
c.get_t_next()
})
.map(|c| c.start(t_start))
.min_by(|a, b| a.total_cmp(b))
.unwrap_or(f64::INFINITY);
// and set the inner component's last and next times
Expand All @@ -145,6 +143,8 @@ impl Simulator for Coupled {
self.build_par_xics();
#[cfg(feature = "par_eoc")]
self.build_par_eocs();

t_next
}

/// Iterates over all the subcomponents to call their [`Simulator::stop`]
Expand Down Expand Up @@ -237,12 +237,10 @@ impl<T: Simulator> RootCoordinator<T> {

/// Runs a simulation for a given period of time.
pub fn simulate(&mut self, t_end: f64) {
self.start(0.);
let mut t_next = self.get_t_next();
let mut t_next = self.start(0.);
while t_next < t_end {
self.collection(t_next);
self.transition(t_next);
t_next = self.get_t_next();
t_next = self.transition(t_next);
}
self.stop(t_next);
}
Expand Down

0 comments on commit a052bd8

Please sign in to comment.