pub struct Simulation { /* private fields */ }Expand description
The simulation environment.
A Simulation is created by calling SimInit::init on a simulation bench
initializer. It contains an asynchronous executor that runs all simulation
models added beforehand to SimInit.
A Simulation object also manages an event scheduling queue and
simulation time. The scheduling queue can be accessed from the simulation
itself, but also from models via the optional &mut Context argument of input and replier port methods.
Likewise, simulation time can be accessed with the Simulation::time
method, or from models with the Context::time method.
Events and queries can be scheduled immediately, i.e. for the current
simulation time, using process_event and
process_query. Calling these methods will
block until all computations triggered by such event or query have
completed. In the case of queries, the response is returned.
Events can also be scheduled at a future simulation time using one of the
schedule_* method. These methods queue an
event without blocking.
Finally, the Simulation instance manages simulation time. A call to
step will:
- increment simulation time until that of the next scheduled event in chronological order, then
- call
Clock::synchronizewhich, unless the simulation is configured to run as fast as possible, blocks until the desired wall clock time, and finally - run all computations scheduled for the new simulation time.
The step_until method operates similarly but
iterates until the target simulation time has been reached. Finally, the
run iterates until there are no more events in the
scheduler queue or, if a Ticker was provided, until the
Scheduler::halt method is called.
See the module-level documentation for more.
Implementations§
Source§impl Simulation
impl Simulation
Sourcepub fn with_clock(&mut self, clock: impl Clock, ticker: impl Ticker)
pub fn with_clock(&mut self, clock: impl Clock, ticker: impl Ticker)
Reset the simulation clock and (re)set the ticker.
This can in particular be used to resume a simulation driven by a real-time clock after it was halted, using a new clock with an update time reference.
See also SimInit::with_clock.
Sourcepub fn with_tickless_clock(&mut self, clock: impl Clock)
pub fn with_tickless_clock(&mut self, clock: impl Clock)
Reset the simulation clock and run the simulation in tickless mode.
This can in particular be used to resume a simulation driven by a real-time clock after it was halted, using instead a clock running as fast as possible.
See also SimInit::with_tickless_clock.
Sourcepub fn with_timeout(&mut self, timeout: Duration)
Available on non-target_family=wasm only.
pub fn with_timeout(&mut self, timeout: Duration)
target_family=wasm only.Sets a timeout for each simulation step.
The timeout corresponds to the maximum wall clock time allocated for the
completion of a single simulation step before an
ExecutionError::Timeout error is raised.
A null duration disables the timeout, which is the default behavior.
See also SimInit::with_timeout.
Sourcepub fn time(&self) -> MonotonicTime
pub fn time(&self) -> MonotonicTime
Returns the current simulation time.
Sourcepub fn step(&mut self) -> Result<(), ExecutionError>
pub fn step(&mut self) -> Result<(), ExecutionError>
Advances simulation time to that of the next scheduled event, processing that event as well as all other events scheduled for the same time.
Processing is gated by a (possibly blocking) call to
Clock::synchronize on the configured simulation clock. This method
blocks until all newly processed events have completed.
Sourcepub fn step_until(
&mut self,
deadline: impl Deadline,
) -> Result<(), ExecutionError>
pub fn step_until( &mut self, deadline: impl Deadline, ) -> Result<(), ExecutionError>
Iteratively advances the simulation time until the specified deadline,
as if by calling Simulation::step repeatedly.
This method blocks until all events scheduled up to the specified target time have completed. The simulation time upon completion is equal to the specified target time, whether or not an event was scheduled for that time.
Sourcepub fn run(&mut self) -> Result<(), ExecutionError>
pub fn run(&mut self) -> Result<(), ExecutionError>
Iteratively advances the simulation time, as if by calling
Simulation::step repeatedly.
This method blocks until the simulation is halted or all scheduled events have completed.
Sourcepub fn process_event<T>(
&mut self,
event_id: &EventId<T>,
arg: T,
) -> Result<(), ExecutionError>
pub fn process_event<T>( &mut self, event_id: &EventId<T>, arg: T, ) -> Result<(), ExecutionError>
Processes an event immediately, blocking until completion.
Simulation time remains unchanged.
Sourcepub fn process_query<T, R>(
&mut self,
query_id: &QueryId<T, R>,
arg: T,
) -> Result<R, ExecutionError>
pub fn process_query<T, R>( &mut self, query_id: &QueryId<T, R>, arg: T, ) -> Result<R, ExecutionError>
Processes a query immediately, blocking until completion.
Simulation time remains unchanged. If the mailbox targeted by the query
was not found in the simulation, an ExecutionError::BadQuery is
returned.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Simulation
impl !RefUnwindSafe for Simulation
impl Send for Simulation
impl !Sync for Simulation
impl Unpin for Simulation
impl !UnwindSafe for Simulation
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request