Skip to content

Commit

Permalink
Improved naming consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
jooste committed Oct 29, 2019
1 parent 3bf225c commit a3b6299
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 69 deletions.
7 changes: 1 addition & 6 deletions BlueSky_pygame.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ def main():
bs.sim.step() # Update sim
bs.scr.update() # GUI update

# Restart traffic simulation:
# if bs.sim.state == bs.INIT:
# bs.sim.reset()
# bs.scr.objdel() # Delete user defined objects

bs.sim.stop()
bs.sim.quit()
pg.quit()

print('BlueSky normal end.')
Expand Down
86 changes: 43 additions & 43 deletions bluesky/simulation/simulation.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
''' BlueSky simulation control object. '''
import time, datetime

# Local imports
import bluesky as bs
from bluesky import settings, stack
from bluesky.tools import datalog, areafilter, plugin, plotter, simtime


# Minimum sleep interval
MINSLEEP = 1e-3

# Register settings defaults
settings.set_variable_defaults(simdt=0.05)
bs.settings.set_variable_defaults(simdt=0.05)


class Simulation:
Expand All @@ -30,7 +30,7 @@ def __init__(self):
self.simt = 0.0

# Simulation timestep [seconds]
self.simdt = settings.simdt
self.simdt = bs.settings.simdt

# Simulation timestep multiplier: run sim at n x speed
self.dtmult = 1.0
Expand Down Expand Up @@ -59,7 +59,7 @@ def step(self):
bs.scr.echo('Benchmark complete: %d samples in %.3f seconds.' % \
(bs.scr.samplecount, time.time() - self.bencht))
self.benchdt = -1.0
self.pause()
self.hold()
else:
self.op()

Expand All @@ -72,17 +72,17 @@ def step(self):
if self.syst < 0.0:
self.syst = time.time()

if bs.traf.ntraf > 0 or len(stack.get_scendata()[0]) > 0:
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()

if self.state == bs.OP:
stack.checkscen()
bs.stack.checkscen()

# Always update stack
stack.process()
bs.stack.process()

if self.state == bs.OP:

Expand All @@ -106,7 +106,7 @@ def step(self):

# Inform main of our state change
if not self.state == self.prevstate:
self.sendState()
bs.net.send_event(b'STATECHANGE', self.state)
self.prevstate = self.state

def stop(self):
Expand All @@ -125,96 +125,96 @@ def quit(self):
bs.stack.saveclose() # Close reording file if it is on

def op(self):
''' Set simulation state to OPERATE. '''
self.syst = time.time()
self.ffmode = False
self.state = bs.OP
self.setDtMultiplier(1.0)
self.set_dtmult(1.0)

def pause(self):
def hold(self):
''' Set simulation state to HOLD. '''
self.syst = time.time()
self.state = bs.HOLD

def reset(self):
''' Reset all simulation objects. '''
self.state = bs.INIT
self.syst = -1.0
self.simt = 0.0
self.simdt = settings.simdt
self.simdt = bs.settings.simdt
simtime.reset()
self.utc = datetime.datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
self.ffmode = False
self.setDtMultiplier(1.0)
self.set_dtmult(1.0)
plugin.reset()
bs.navdb.reset()
bs.traf.reset()
stack.reset()
bs.stack.reset()
datalog.reset()
areafilter.reset()
bs.scr.reset()

def setdt(self, dt=None, target='simdt'):
''' Change the timestep of the simulation or other periodic functions. '''
if dt is not None and target == 'simdt':
self.simdt = abs(dt)
self.sysdt = self.simdt / self.dtmult
return simtime.setdt(dt, target)

def setDtMultiplier(self, mult):
def set_dtmult(self, mult):
''' Set simulation speed multiplier. '''
self.dtmult = mult
self.sysdt = self.simdt / self.dtmult

def setFixdt(self, flag, nsec=None):
if flag:
self.fastforward(nsec)
else:
self.op()

def fastforward(self, nsec=None):
''' Run in fast-time (for nsec seconds if specified). '''
self.ffmode = True
if nsec is not None:
self.ffstop = self.simt + nsec
else:
self.ffstop = None
self.ffstop = (self.simt + nsec) if nsec else None

def benchmark(self, fname='IC', dt=300.0):
stack.ic(fname)
''' Run a simulation benchmark.
Use scenario given by fname.
Run for <dt> seconds. '''
bs.stack.ic(fname)
self.bencht = 0.0 # Start time will be set at next sim cycle
self.benchdt = dt

def sendState(self):
bs.net.send_event(b'STATECHANGE', self.state)

def batch(self, fname):
# The contents of the scenario file are meant as a batch list: send to server and clear stack
''' Run a batch of scenarios. '''
# The contents of the scenario file are meant as a batch list:
# send to server and clear stack
self.reset()
try:
scentime, scencmd = zip(*[tc for tc in stack.readscn(fname)])
scentime, scencmd = zip(*[tc for tc in bs.stack.readscn(fname)])
bs.net.send_event(b'BATCH', (scentime, scencmd))
except FileNotFoundError:
return False, f'BATCH: File not found: {fname}'

return True

def event(self, eventname, eventdata, sender_rte):
''' Handle events coming from the network. '''
# Keep track of event processing
event_processed = False

if eventname == b'STACKCMD':
# We received a single stack command. Add it to the existing stack
stack.stack(eventdata, sender_rte)
bs.stack.stack(eventdata, sender_rte)
event_processed = True

elif eventname == b'BATCH':
# We are in a batch simulation, and received an entire scenario. Assign it to the stack.
self.reset()
stack.set_scendata(eventdata['scentime'], eventdata['scencmd'])
bs.stack.set_scendata(eventdata['scentime'], eventdata['scencmd'])
self.op()
event_processed = True

elif eventname == b'GETSIMSTATE':
# Send list of stack functions available in this sim to gui at start
stackdict = {cmd : val[0][len(cmd) + 1:] for cmd, val in stack.cmddict.items()}
stackdict = {cmd : val[0][len(cmd) + 1:] for cmd, val in bs.stack.cmddict.items()}
shapes = [shape.raw for shape in areafilter.areas.values()]
simstate = dict(pan=bs.scr.def_pan, zoom=bs.scr.def_zoom,
stackcmds=stackdict, stacksyn=stack.cmdsynon, shapes=shapes,
stackcmds=stackdict, stacksyn=bs.stack.cmdsynon, shapes=shapes,
custacclr=bs.scr.custacclr, custgrclr=bs.scr.custgrclr,
settings=bs.settings._settings_hierarchy)
bs.net.send_event(b'SIMSTATE', simstate, target=sender_rte)
Expand All @@ -225,33 +225,33 @@ def event(self, eventname, eventdata, sender_rte):
return event_processed

def setutc(self, *args):
""" Set simulated clock time offset"""
''' Set simulated clock time offset. '''
if not args:
pass # avoid error message, just give time

elif len(args) == 1:
if args[0].upper() == "RUN":
if args[0].upper() == 'RUN':
self.utc = datetime.datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)

elif args[0].upper() == "REAL":
elif args[0].upper() == 'REAL':
self.utc = datetime.datetime.today().replace(microsecond=0)

elif args[0].upper() == "UTC":
elif args[0].upper() == 'UTC':
self.utc = datetime.datetime.utcnow().replace(microsecond=0)

else:
try:
self.utc = datetime.datetime.strptime(args[0],
'%H:%M:%S.%f' if '.' in args[0] else '%H:%M:%S')
except ValueError:
return False, "Input time invalid"
return False, 'Input time invalid'

elif len(args) == 3:
day, month, year = args
try:
self.utc = datetime.datetime(year, month, day)
except ValueError:
return False, "Input date invalid."
return False, 'Input date invalid.'
elif len(args) == 4:
day, month, year, timestring = args
try:
Expand All @@ -260,8 +260,8 @@ def setutc(self, *args):
'%Y,%m,%d,%H:%M:%S.%f' if '.' in timestring else
'%Y,%m,%d,%H:%M:%S')
except ValueError:
return False, "Input date invalid."
return False, 'Input date invalid.'
else:
return False, "Syntax error"
return False, 'Syntax error'

return True, "Simulation UTC " + str(self.utc)
return True, 'Simulation UTC ' + str(self.utc)
10 changes: 5 additions & 5 deletions bluesky/stack/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def init(startup_scnfile):
"DTMULT": [
"DTMULT multiplier",
"float",
bs.sim.setDtMultiplier,
bs.sim.set_dtmult,
"Sel multiplication factor for fast-time simulation",
],
"DTNOLOOK": [
Expand Down Expand Up @@ -473,8 +473,8 @@ def init(startup_scnfile):
"FIXDT": [
"FIXDT ON/OFF [tend]",
"onoff,[time]",
bs.sim.setFixdt,
"Fix the time step",
lambda flag, *args: bs.sim.ff(*args) if flag else bs.op(),
"Legacy function for TMX compatibility",
],
"GETWIND": [
"GETWIND lat,lon,[alt]",
Expand Down Expand Up @@ -503,7 +503,7 @@ def init(startup_scnfile):
lambda *args: bs.scr.echo(showhelp(*args)),
"Show help on a command, show pdf or write list of commands to file",
],
"HOLD": ["HOLD", "", bs.sim.pause, "Pause(hold) simulation"],
"HOLD": ["HOLD", "", bs.sim.hold, "Pause(hold) simulation"],
"IC": [
"IC [IC/filename]",
"[string]",
Expand Down Expand Up @@ -589,7 +589,7 @@ def init(startup_scnfile):
bs.traf.asas.SetNoreso,
"Switch off conflict resolution for this aircraft",
],
"OP": ["OP", "", bs.sim.op, "Start/Run simulation or continue after pause"],
"OP": ["OP", "", bs.sim.op, "Start/Run simulation or continue after hold"],
"ORIG": [
"ORIG acid, latlon/airport",
"acid,wpt/latlon",
Expand Down
6 changes: 3 additions & 3 deletions bluesky/traffic/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def reset(self):

def AircraftCell(self,cells,time):
if floor(time) >= self.resettime:
bs.sim.pause()
bs.sim.hold()
self.reset()
self.resettime = self.resettime + self.deltaresettime
self.iteration = self.iteration + 1
Expand Down Expand Up @@ -579,7 +579,7 @@ def selectTraffic(self):

def applymetric(self):
time1 = time()
bs.sim.pause()
bs.sim.hold()
self.doubleconflict = 0
# relative pos x and pos y
self.step = self.step + 1
Expand Down Expand Up @@ -1433,7 +1433,7 @@ def update(self):

def plot(self):
# Pause simulation
bs.sim.pause()
bs.sim.hold()

# Open a plot window attached to a command?
# plot, showplot and other matplotlib commands
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions data/html/Command-Reference.html
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ <h2 id="command-reference">Command reference</h2>
</tr>
<tr class="odd">
<td><a href="OP.html">OP</a></td>
<td>Start simulation or continue after pause</td>
<td>Start simulation or continue after hold</td>
</tr>
<tr class="even">
<td><a href="ORIG.html">ORIG</a></td>
Expand Down Expand Up @@ -426,7 +426,7 @@ <h2 id="command-synonyms">Command synonyms</h2>
<tr class="odd">
<td>CONTINUE</td>
<td>OP</td>
<td>Start/Run simulation or continue after pause</td>
<td>Start/Run simulation or continue after hold</td>
</tr>
<tr class="even">
<td>CREATE</td>
Expand Down Expand Up @@ -531,7 +531,7 @@ <h2 id="command-synonyms">Command synonyms</h2>
<tr class="even">
<td>RUN</td>
<td>OP</td>
<td>Start/Run simulation or continue after pause</td>
<td>Start/Run simulation or continue after hold</td>
</tr>
<tr class="odd">
<td>SAVE</td>
Expand All @@ -546,7 +546,7 @@ <h2 id="command-synonyms">Command synonyms</h2>
<tr class="odd">
<td>START</td>
<td>OP</td>
<td>Start/Run simulation or continue after pause</td>
<td>Start/Run simulation or continue after hold</td>
</tr>
<tr class="even">
<td>STOP</td>
Expand Down
2 changes: 1 addition & 1 deletion data/html/op.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<div class="row">
<div class="span12">
<h1 id="op-op">OP: Op</h1>
<p>Start/Run simulation or continue after pause</p>
<p>Start/Run simulation or continue after hold</p>
<p><strong>Usage:</strong></p>
<pre><code>OP</code></pre>
<p><strong>Arguments:</strong></p>
Expand Down
Loading

0 comments on commit a3b6299

Please sign in to comment.