使用NS2和NAM,我想显示包含许多节点的树形拓扑。如何设置图像中显示的节点和方向的位置。第一张图像显示在编辑和更改NAM工具中的节点位置之前。使用NAM工具手动编辑节点位置后显示第二个图像。我想看到第二个图像中显示的节点位置。我不想手动编辑。如何为此编写TCL代码?
以下是TCL代码
#Create a simulator object
set ns [new Simulator]
$ns color 1 Green
$ns color 2 Red
$ns color 3 blue
$ns color 4 magenta
#Tell the simulator to use dynamic routing
$ns rtproto DV
set tracefile [open out.tr w]
$ns trace-all $tracefile
#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns tracefile nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &
exit 0
}
#Create thirty six nodes
for {set i 0} {$i < 21} {incr i} {
set n($i) [$ns node]
}
$ns duplex-link $n(0) $n(1) 1Mb 10ms DropTail
$ns duplex-link $n(0) $n(2) 1Mb 10ms DropTail
$ns duplex-link $n(0) $n(3) 1Mb 10ms DropTail
$ns duplex-link $n(0) $n(4) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(5) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(7) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(9) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(2) $n(5) 1Mb 10ms DropTail
$ns duplex-link $n(2) $n(7) 1Mb 10ms DropTail
$ns duplex-link $n(2) $n(9) 1Mb 10ms DropTail
$ns duplex-link $n(2) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(3) $n(6) 1Mb 10ms DropTail
$ns duplex-link $n(3) $n(8) 1Mb 10ms DropTail
$ns duplex-link $n(3) $n(10) 1Mb 10ms DropTail
$ns duplex-link $n(3) $n(12) 1Mb 10ms DropTail
$ns duplex-link $n(4) $n(6) 1Mb 10ms DropTail
$ns duplex-link $n(4) $n(8) 1Mb 10ms DropTail
$ns duplex-link $n(4) $n(10) 1Mb 10ms DropTail
$ns duplex-link $n(4) $n(12) 1Mb 10ms DropTail
$ns duplex-link $n(5) $n(13) 1Mb 10ms DropTail
$ns duplex-link $n(5) $n(14) 1Mb 10ms DropTail
$ns duplex-link $n(6) $n(13) 1Mb 10ms DropTail
$ns duplex-link $n(6) $n(14) 1Mb 10ms DropTail
$ns duplex-link $n(7) $n(15) 1Mb 10ms DropTail
$ns duplex-link $n(7) $n(16) 1Mb 10ms DropTail
$ns duplex-link $n(8) $n(15) 1Mb 10ms DropTail
$ns duplex-link $n(8) $n(16) 1Mb 10ms DropTail
$ns duplex-link $n(9) $n(17) 1Mb 10ms DropTail
$ns duplex-link $n(9) $n(18) 1Mb 10ms DropTail
$ns duplex-link $n(10) $n(17) 1Mb 10ms DropTail
$ns duplex-link $n(10) $n(18) 1Mb 10ms DropTail
$ns duplex-link $n(11) $n(19) 1Mb 10ms DropTail
$ns duplex-link $n(11) $n(20) 1Mb 10ms DropTail
$ns duplex-link $n(12) $n(19) 1Mb 10ms DropTail
$ns duplex-link $n(12) $n(20) 1Mb 10ms DropTail
# Provide initial location of mobilenodes
$n(0) set X_ 0.0
$n(0) set Y_ 0.0
#Create a UDP Source Node
set udp0 [new Agent/UDP]
$udp0 set class_ 1
$ns attach-agent $n(0) $udp0
#Create a UDP1 Source Node
set udp1 [new Agent/UDP]
$udp1 set class_ 2
$ns attach-agent $n(0) $udp1
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 250
$cbr0 set interval_ 0.010
$cbr0 attach-agent $udp0
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 250
$cbr1 set interval_ 0.010
$cbr1 attach-agent $udp1
set null0 [new Agent/Null]
$ns attach-agent $n(13) $null0
set null1 [new Agent/Null]
$ns attach-agent $n(20) $null1
$ns connect $udp0 $null0
$ns connect $udp1 $null1
$ns at 0.0 "$cbr0 start"
$ns at 0.0 "$cbr1 start"
$ns at 5.0 "finish"
#Run the simulation
$ns run
答案 0 :(得分:0)
这都是传播节点的问题。有几种方法可以做到,但关键是你有一个矩形空间。
# Describe the indices of the nodes and how we approximately want to arrange them.
set indexLayout {
{0}
{1 2 3 4}
{5 6 7 8 9 10 11 12}
{13 14 15 16 17 18 19 20}
}
# Describe the space we're going to lay them out over; you might need to tune this
set originX 0
set originY 0
set width 100
set height 100
# Do the layout
set nRows [llength $indexLayout]
set rowsize [expr {$height / $nRows}]
set rowY [expr {$originY + $rowsize / 2}]
foreach row $indexLayout {
set nCols [llength $row]
set colsize [expr {$width / $nCols}]
set rowX [expr {$originX + $colsize / 2}]
foreach index $row {
$n($index) set X_ $rowX
$n($index) set Y_ $rowY
set rowX [expr {$rowX + $colsize}]
}
set rowY [expr {$rowY + $rowsize}]
}
如果您将节点放在外,我不确定是否需要显式设置链接方向,但如果这样做,您应该阅读此Stack Overflow问题:Angle based orientation ns2 not working以获取有关语法的信息。如果你需要明确地放置链接,你需要做一些几何(虽然我不明白为什么有人会默认需要它)。
答案 1 :(得分:0)
示例网格:pbcast_sim.tcl + p_bcast.tcl http://disc.ece.illinois.edu/downloads/lab108.html →→http://disc.ece.illinois.edu/downloads/pbcast_sim.tcl 和http://disc.ece.illinois.edu/downloads/p_bcast.tcl
模拟示例:$ ns pbcast_sim.tcl -prob 20