Skip to content

Commit

Permalink
Add set_rng to Simulation
Browse files Browse the repository at this point in the history
Add dyn_rng method for SimulationRng wrapping

Signed-off-by: Tin Svagelj <tin.svagelj@live.com>
  • Loading branch information
Caellian committed Jul 27, 2023
1 parent 2202de1 commit 0830470
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sim/src/input_modeling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ pub use random_variable::Continuous as ContinuousRandomVariable;
pub use random_variable::Discrete as DiscreteRandomVariable;
pub use random_variable::Index as IndexRandomVariable;
pub use thinning::Thinning;
pub use uniform_rng::some_dyn_rng;
pub use uniform_rng::{dyn_rng, some_dyn_rng};
6 changes: 5 additions & 1 deletion sim/src/input_modeling/uniform_rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ pub(crate) fn default_rng() -> DynRng {
Rc::new(RefCell::new(rand_pcg::Pcg64Mcg::new(42)))
}

pub fn dyn_rng<Rng: SimulationRng + 'static>(rng: Rng) -> DynRng {
Rc::new(RefCell::new(rng))
}

pub fn some_dyn_rng<Rng: SimulationRng + 'static>(rng: Rng) -> Option<DynRng> {
Some(Rc::new(RefCell::new(rng)))
Some(dyn_rng(rng))
}
7 changes: 6 additions & 1 deletion sim/src/simulator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::rc::Rc;
use serde::{Deserialize, Serialize};

use crate::input_modeling::uniform_rng::SimulationRng;
use crate::input_modeling::{dyn_rng, some_dyn_rng};
use crate::models::{DevsModel, Model, ModelMessage, ModelRecord, Reportable};
use crate::utils::errors::SimulationError;
use crate::utils::set_panic_hook;
Expand Down Expand Up @@ -70,13 +71,17 @@ impl Simulation {
models,
connectors,
services: Services {
global_rng: Rc::new(RefCell::new(global_rng)),
global_rng: dyn_rng(global_rng),
global_time: 0.0,
},
..Self::default()
}
}

pub fn set_rng(&mut self, rng: impl SimulationRng + 'static) {
self.services.global_rng = dyn_rng(rng)
}

/// This method sets the models and connectors of an existing simulation.
pub fn put(&mut self, models: Vec<Model>, connectors: Vec<Connector>) {
self.models = models;
Expand Down

0 comments on commit 0830470

Please sign in to comment.