sumolib.output.convert.ns2
This module includes functions for converting SUMO's fcd-output into data files read by ns2.
1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 2# Copyright (C) 2013-2026 German Aerospace Center (DLR) and others. 3# This program and the accompanying materials are made available under the 4# terms of the Eclipse Public License 2.0 which is available at 5# https://www.eclipse.org/legal/epl-2.0/ 6# This Source Code may also be made available under the following Secondary 7# Licenses when the conditions for such availability set forth in the Eclipse 8# Public License 2.0 are satisfied: GNU General Public License, version 2 9# or later which is available at 10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html 11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later 12 13# @file ns2.py 14# @author Daniel Krajzewicz 15# @author Jakob Erdmann 16# @author Michael Behrisch 17# @date 2013-01-15 18 19""" 20This module includes functions for converting SUMO's fcd-output into 21data files read by ns2. 22""" 23from __future__ import print_function 24from __future__ import absolute_import 25import sumolib.output 26import sumolib.net 27 28 29def fcd2ns2mobility(inpFCD, outSTRM, further): 30 vIDm = sumolib._Running(further["orig-ids"], True) 31 checkGaps = not further["ignore-gaps"] 32 begin = -1 33 end = None 34 area = [None, None, None, None] 35 vehInfo = {} 36 removed = set() 37 ignoring = set() 38 for timestep in inpFCD: 39 if begin < 0: 40 begin = timestep.time 41 end = timestep.time 42 seen = set() 43 if not timestep.vehicle and checkGaps: 44 _writeMissing(timestep.time, vIDm, seen, vehInfo, removed) 45 continue 46 for v in timestep.vehicle: 47 if v.id in ignoring: 48 continue 49 if v.id in removed: 50 print( 51 "Warning: vehicle %s reappeared after being gone and will be ignored" % v.id) 52 ignoring.add(v.id) 53 continue 54 55 seen.add(v.id) 56 if not vIDm.k(v.id): 57 nid = vIDm.g(v.id) 58 if outSTRM: 59 print("$node_(%s) set X_ %s" % (nid, v.x), file=outSTRM) 60 print("$node_(%s) set Y_ %s" % (nid, v.y), file=outSTRM) 61 print("$node_(%s) set Z_ %s" % (nid, 0), file=outSTRM) 62 vehInfo[v.id] = [nid, timestep.time, 0] 63 nid = vIDm.g(v.id) 64 if outSTRM: 65 print('$ns_ at %s "$node_(%s) setdest %s %s %s"' % 66 (timestep.time, nid, v.x, v.y, v.speed), file=outSTRM) 67 if not area[0]: 68 area[0] = v.x 69 area[1] = v.y 70 area[2] = v.x 71 area[3] = v.y 72 area[0] = min(area[0], v.x) 73 area[1] = min(area[1], v.y) 74 area[2] = max(area[2], v.x) 75 area[3] = max(area[3], v.y) 76 if checkGaps: 77 _writeMissing(timestep.time, vIDm, seen, vehInfo, removed) 78 _writeMissing(timestep.time, vIDm, seen, vehInfo, removed) 79 return vIDm, vehInfo, begin, end, area 80 81 82def writeNS2activity(outSTRM, vehInfo): 83 for v in sorted(vehInfo): 84 i = vehInfo[v] 85 print('$ns_ at %s "$g(%s) start"; # SUMO-ID: %s' % 86 (i[1], i[0], v), file=outSTRM) 87 print('$ns_ at %s "$g(%s) stop"; # SUMO-ID: %s' % 88 (i[2], i[0], v), file=outSTRM) 89 90 91def writeNS2config(outSTRM, vehInfo, ns2activityfile, ns2mobilityfile, begin, end, area): 92 print("# set number of nodes\nset opt(nn) %s\n" % 93 len(vehInfo), file=outSTRM) 94 if ns2activityfile: 95 print("# set activity file\nset opt(af) $opt(config-path)\nappend opt(af) /%s\n" % 96 ns2activityfile, file=outSTRM) 97 if ns2mobilityfile: 98 print("# set mobility file\nset opt(mf) $opt(config-path)\nappend opt(mf) /%s\n" % 99 ns2mobilityfile, file=outSTRM) 100 xmin = area[0] 101 ymin = area[1] 102 xmax = area[2] 103 ymax = area[3] 104 print("# set start/stop time\nset opt(start) %s\nset opt(stop) %s\n" % 105 (begin, end), file=outSTRM) 106 print("# set floor size\nset opt(x) %s\nset opt(y) %s\nset opt(min-x) %s\nset opt(min-y) %s\n" % 107 (xmax, ymax, xmin, ymin), file=outSTRM) 108 109 110def _writeMissing(t, vIDm, seen, vehInfo, removed): 111 toDel = [] 112 for v in vIDm._m: 113 if v in seen: 114 continue 115 vIDm.g(v) 116 vehInfo[v][2] = t 117 toDel.append(v) 118 removed.add(v) 119 for v in toDel: 120 vIDm.d(v)
def
fcd2ns2mobility(inpFCD, outSTRM, further):
30def fcd2ns2mobility(inpFCD, outSTRM, further): 31 vIDm = sumolib._Running(further["orig-ids"], True) 32 checkGaps = not further["ignore-gaps"] 33 begin = -1 34 end = None 35 area = [None, None, None, None] 36 vehInfo = {} 37 removed = set() 38 ignoring = set() 39 for timestep in inpFCD: 40 if begin < 0: 41 begin = timestep.time 42 end = timestep.time 43 seen = set() 44 if not timestep.vehicle and checkGaps: 45 _writeMissing(timestep.time, vIDm, seen, vehInfo, removed) 46 continue 47 for v in timestep.vehicle: 48 if v.id in ignoring: 49 continue 50 if v.id in removed: 51 print( 52 "Warning: vehicle %s reappeared after being gone and will be ignored" % v.id) 53 ignoring.add(v.id) 54 continue 55 56 seen.add(v.id) 57 if not vIDm.k(v.id): 58 nid = vIDm.g(v.id) 59 if outSTRM: 60 print("$node_(%s) set X_ %s" % (nid, v.x), file=outSTRM) 61 print("$node_(%s) set Y_ %s" % (nid, v.y), file=outSTRM) 62 print("$node_(%s) set Z_ %s" % (nid, 0), file=outSTRM) 63 vehInfo[v.id] = [nid, timestep.time, 0] 64 nid = vIDm.g(v.id) 65 if outSTRM: 66 print('$ns_ at %s "$node_(%s) setdest %s %s %s"' % 67 (timestep.time, nid, v.x, v.y, v.speed), file=outSTRM) 68 if not area[0]: 69 area[0] = v.x 70 area[1] = v.y 71 area[2] = v.x 72 area[3] = v.y 73 area[0] = min(area[0], v.x) 74 area[1] = min(area[1], v.y) 75 area[2] = max(area[2], v.x) 76 area[3] = max(area[3], v.y) 77 if checkGaps: 78 _writeMissing(timestep.time, vIDm, seen, vehInfo, removed) 79 _writeMissing(timestep.time, vIDm, seen, vehInfo, removed) 80 return vIDm, vehInfo, begin, end, area
def
writeNS2activity(outSTRM, vehInfo):
def
writeNS2config(outSTRM, vehInfo, ns2activityfile, ns2mobilityfile, begin, end, area):
92def writeNS2config(outSTRM, vehInfo, ns2activityfile, ns2mobilityfile, begin, end, area): 93 print("# set number of nodes\nset opt(nn) %s\n" % 94 len(vehInfo), file=outSTRM) 95 if ns2activityfile: 96 print("# set activity file\nset opt(af) $opt(config-path)\nappend opt(af) /%s\n" % 97 ns2activityfile, file=outSTRM) 98 if ns2mobilityfile: 99 print("# set mobility file\nset opt(mf) $opt(config-path)\nappend opt(mf) /%s\n" % 100 ns2mobilityfile, file=outSTRM) 101 xmin = area[0] 102 ymin = area[1] 103 xmax = area[2] 104 ymax = area[3] 105 print("# set start/stop time\nset opt(start) %s\nset opt(stop) %s\n" % 106 (begin, end), file=outSTRM) 107 print("# set floor size\nset opt(x) %s\nset opt(y) %s\nset opt(min-x) %s\nset opt(min-y) %s\n" % 108 (xmax, ymax, xmin, ymin), file=outSTRM)