def step(self): ''' Perform a simulation timestep. ''' # Simulation starts as soon as there is traffic, or pending commands if self.state == bs.INIT: if self.syst < 0.0: self.syst = time.time() if bs.traf.ntraf > 0 or len(bs.stack.get_scendata()[0]) > 0: self.op() if self.benchdt > 0.0: self.fastforward(self.benchdt) self.bencht = time.time() # When running at a fixed rate, or when in hold/init, # increment system time with sysdt and calculate remainder to sleep. remainder = self.syst - time.time() if (not self.ffmode or self.state != bs.OP) and remainder > MINSLEEP: time.sleep(remainder) # Always update stack #bs.stack.process() if self.state == bs.OP: # Plot/log the current timestep, and call preupdate functions plotter.update() datalog.update() simtime.preupdate() # Determine interval towards next timestep if remainder < 0.0 and self.rtmode: # Allow a variable timestep when we are running realtime self.simt, self.simdt = simtime.step(-remainder) else: # Don't accumulate delay when we aren't running realtime if remainder < 0.0: self.syst -= remainder self.simt, self.simdt = simtime.step() # Update UTC time self.utc += datetime.timedelta(seconds=self.simdt) # Update traffic and other update functions for the next timestep bs.traf.update() simtime.update() # Always update syst self.syst += self.simdt / self.dtmult # Stop fast-time/benchmark if enabled and set interval has passed if self.ffstop is not None and self.simt >= self.ffstop: if self.benchdt > 0.0: bs.scr.echo('Benchmark complete: %d samples in %.3f seconds.' % (bs.scr.samplecount, time.time() - self.bencht)) self.benchdt = -1.0 self.hold() else: self.op() bs.net.send_event(b'STATECHANGE', self.state) # Inform main of our state change if self.state != self.prevstate: bs.net.send_event(b'STATECHANGE', self.state) self.prevstate = self.state
def step(self): ''' Perform a simulation timestep. ''' # When running at a fixed rate, or when in hold/init, # increment system time with sysdt and calculate remainder to sleep. if not self.ffmode or not self.state == bs.OP: remainder = self.syst - time.time() if remainder > MINSLEEP: time.sleep(remainder) elif self.ffstop is not None and self.simt >= self.ffstop: if self.benchdt > 0.0: bs.scr.echo('Benchmark complete: %d samples in %.3f seconds.' % \ (bs.scr.samplecount, time.time() - self.bencht)) self.benchdt = -1.0 self.pause() else: self.op() if self.state == bs.OP: # Plugins pre-update plugin.preupdate(self.simt) # Update screen logic bs.scr.update() # Simulation starts as soon as there is traffic, or pending commands if self.state == bs.INIT: if self.syst < 0.0: self.syst = time.time() if bs.traf.ntraf > 0 or len(stack.get_scendata()[0]) > 0: self.op() if self.benchdt > 0.0: self.fastforward(self.benchdt) self.bencht = time.time() if self.state == bs.OP: stack.checkfile(self.simt) # Always update stack stack.process() if self.state == bs.OP: bs.traf.update(self.simt, self.simdt) # Update plugins plugin.update(self.simt) # Update Plotter plotter.update(self.simt) # Update loggers datalog.postupdate() # Update sim and UTC time for the next timestep self.simt += self.simdt self.utc += datetime.timedelta(seconds=self.simdt) # Always update syst self.syst += self.sysdt # Inform main of our state change if not self.state == self.prevstate: self.sendState() self.prevstate = self.state