1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2024-11-21 19:16:30 +00:00

Base mobility template

This commit is contained in:
Thibaud Gasser 2018-12-13 20:58:07 +01:00
parent 4eb750f976
commit 576112dd6a

View File

@ -11,6 +11,19 @@ import randomTrips
SCRIPTDIR = os.path.dirname(__file__) SCRIPTDIR = os.path.dirname(__file__)
TEMPLATEDIR = os.path.join(SCRIPTDIR, 'templates') TEMPLATEDIR = os.path.join(SCRIPTDIR, 'templates')
vehicle_classes = {
'passenger': {
'--vehicle-class': 'passenger',
'--vclass': 'passenger',
'--prefix': 'veh'
},
'bus': {
'--vehicle-class': 'bus',
'--vclass': 'bus',
'--prefix': 'bus'
}
}
def load_netconvert_template(osm_input, out_name): def load_netconvert_template(osm_input, out_name):
tree = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.netcfg')) tree = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.netcfg'))
@ -32,11 +45,12 @@ def load_polyconvert_template(osm_file, type_file, scenario_name):
return tree return tree
def load_sumoconfig_template(simulation_name): def load_sumoconfig_template(simulation_name, routefiles=None):
routefiles = routefiles or (f'{simulation_name}.rou.xml',)
tree = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.sumocfg')) tree = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.sumocfg'))
root = tree.getroot() root = tree.getroot()
root.find('input/net-file').set('value', f'{simulation_name}.net.xml') root.find('input/net-file').set('value', f'{simulation_name}.net.xml')
root.find('input/route-files').set('value', f'{simulation_name}.rou.xml') root.find('input/route-files').set('value', ','.join(f for f in routefiles))
root.find('input/additional-files').set('value', f'{simulation_name}.poly.xml') root.find('input/additional-files').set('value', f'{simulation_name}.poly.xml')
root.find('report/log').set('value', f'{simulation_name}.log') root.find('report/log').set('value', f'{simulation_name}.log')
return tree return tree
@ -58,45 +72,48 @@ def generate_scenario(osm_file, out_path, scenario_name):
subprocess.run(netconvertcmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) subprocess.run(netconvertcmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
subprocess.run(polyconvert_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) subprocess.run(polyconvert_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# Move files to destination # Move files to destination
for f in os.listdir(tmpdirname): ignore_patterns = shutil.ignore_patterns('*.polycfg', '*.netcfg', 'typemap')
if f.endswith('netcfg') or f.endswith('polycfg') or f == 'typemap': shutil.copytree(tmpdirname, out_path, ignore=ignore_patterns)
continue
shutil.move(os.path.join(tmpdirname, f), os.path.join(out_path, f))
def generate_mobility(path, name): def generate_mobility(path, name):
routefile = os.path.join(path, f'{name}.rou.xml')
netfile = os.path.join(path, f'{name}.net.xml') netfile = os.path.join(path, f'{name}.net.xml')
output = os.path.join(path, f'{name}.trips.xml') output = os.path.join(path, f'{name}.trips.xml')
routefiles = []
end_time = 200 end_time = 200
veh_class = 'passenger' classes = ('passenger', 'bus')
options = [ for veh_class in classes:
f'--net-file={netfile}', # simname.bus.rou.xml, simname.passenger.rou.xml, ...
f'--route-file={routefile}', routefile = f'{name}.{veh_class}.rou.xml'
f'--end={end_time}', routepath = os.path.join(path, routefile)
f'--vehicle-class={veh_class}', routefiles.append(routefile)
f'--output-trip-file={output}', options = {
'--validate', '--net-file': netfile,
'--length', '--output-trip-file': output,
] '--route-file': routepath,
print('Generating mobility…') '-e': end_time
randomTrips.main(randomTrips.get_options(options)) }
options.update(vehicle_classes[veh_class])
flags = ['-l']
generate_random_trips(flags, options)
return routefiles
def generate_sumo_configuration(path, scenario_name): def generate_random_trips(flags, options):
sumo_template = load_sumoconfig_template(scenario_name) randomTrips.main(randomTrips.get_options(dict_to_list(options) + flags))
def generate_sumo_configuration(routefiles, path, scenario_name):
sumo_template = load_sumoconfig_template(routefiles, scenario_name)
sumo_template.write(os.path.join(path, f'{scenario_name}.sumocfg')) sumo_template.write(os.path.join(path, f'{scenario_name}.sumocfg'))
def generate_all(osm_file, output_path, simulation_name): def generate_all(osm_file, path, simulation_name):
simulation_dir = os.path.join(output_path, simulation_name) simulation_dir = os.path.join(path, simulation_name)
logs_dir = os.path.join(simulation_dir, 'log') logs_dir = os.path.join(simulation_dir, 'log')
if not os.path.exists(simulation_dir):
os.mkdir(simulation_dir)
os.mkdir(logs_dir)
generate_scenario(osm_file, simulation_dir, simulation_name) generate_scenario(osm_file, simulation_dir, simulation_name)
generate_mobility(simulation_dir, simulation_name) routefiles = generate_mobility(simulation_dir, simulation_name)
generate_sumo_configuration(simulation_dir, simulation_name) generate_sumo_configuration(routefiles, simulation_dir, simulation_name)
# Move all logs to logdir # Move all logs to logdir
move_logs(simulation_dir, logs_dir) move_logs(simulation_dir, logs_dir)
@ -107,6 +124,10 @@ def move_logs(simulation_dir, logs_dir):
shutil.move(os.path.join(simulation_dir, f), logs_dir) shutil.move(os.path.join(simulation_dir, f), logs_dir)
def dict_to_list(d):
return [item for k in d for item in (k, d[k])]
if __name__ == '__main__': if __name__ == '__main__':
if os.path.isdir('/tmp/scenario/foo'): if os.path.isdir('/tmp/scenario/foo'):
shutil.rmtree('/tmp/scenario/foo') shutil.rmtree('/tmp/scenario/foo')