1

Using NS2 and NAM, I want to display a tree topology containing many nodes. How to set position of nodes and orientation as displayed in the image. First image shows before editing and changing the position of nodes in NAM tool. Second image displays after editing the node position manually using NAM tool. I want to see nodes position as displayed in the second image. I do not want to edit manually. How to write the TCL code for this ?

nodes position and orientation before editing in NAM tool

Here is the TCL Code

    #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

after editing in NAM tool

Dipankar Nalui
  • 1,121
  • 5
  • 18
  • 33

2 Answers2

0

It's all a matter of spreading the nodes. There's a few ways to do it, but the key is that you've got a rectangular space.

# 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}]
}

I'm not sure if you need to explicitly set the link orientations if you lay the nodes out, but if you do, you should read this Stack Overflow question: Angle based orientation ns2 not working for information about what the syntax is like. If you need to lay the links out explicitly, you will need to do some geometry (though I can't see why anyone would make that required by default).

Community
  • 1
  • 1
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • Thank you. The above code you provided gives the same output. Here also I have to set node position and orientation manually. I want to set node position automatically writing TCL code. – Dipankar Nalui Apr 16 '17 at 03:46
  • I tried that Angle based orientation. It works with less number of nodes. But when I am giving 20 nodes, it is not showing proper orientation. – Dipankar Nalui Apr 16 '17 at 03:50
  • `$ns duplex-link-op $n(0) $n(1) orient 200deg` this angle based orientation works with limited nodes. When tree has multiple levels, it can not produce the correct orientation. Left nodes are becoming right, Right child nodes becoming up. Parent-child-left-right-down-up orientation does not work in this tree topology. – Dipankar Nalui Apr 16 '17 at 05:06
  • `#$ns duplex-link-op $n(0) $n(1) orient down-left` I tried this also. It works only with limited Levels. When tree has 3 Levels it can not show correct position. – Dipankar Nalui Apr 16 '17 at 05:09
  • The code you provided is working when I remove all the links from all nodes. After removing all links from the tree it becomes a disconnected graph. Then all the node positions are showing correct. I removed all these links `$ns duplex-link $n(0) $n(1) 1Mb 10ms DropTail `. But I want all links to be present in the tree. I want all links and all nodes in tree format with orientation. plz help me. thank you. – Dipankar Nalui Apr 16 '17 at 05:20
  • [This test.tcl file](https://drive.google.com/file/d/0B0moMeZtYrkQaGhVMnFTc0Vyblk/view?usp=sharing) gives [this output](https://drive.google.com/file/d/0B0moMeZtYrkQbEtmVGFXakxPdVU/view?usp=sharing) – Dipankar Nalui Apr 16 '17 at 05:29
  • 1
    This works fine. [test3.tcl](https://drive.google.com/file/d/0B0moMeZtYrkQRUx0amM4cmZWWmc/view?usp=sharing) gives [this output3.png](https://drive.google.com/file/d/0B0moMeZtYrkQUFBzYVBsWllvUjg/view?usp=sharing) When we add anymore links it is not showing correct orientation. – Dipankar Nalui Apr 16 '17 at 05:49
  • Orientation is correct up to this link `$ns duplex-link $n(7) $n(15) 1Mb 10ms DropTail` But when I add this link `$ns duplex-link $n(7) $n(16) 1Mb 10ms DropTail` it fails to show correct orientation. Please solve this problem. – Dipankar Nalui Apr 16 '17 at 05:52
  • Finally, the problem is with the links (7-16), (8-15), (8-16). These are the problems `$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` – Dipankar Nalui Apr 16 '17 at 06:04
  • Nodes 7,8,15,16 have some problem. [test5.tcl](https://drive.google.com/file/d/0B0moMeZtYrkQTnFBU0lBQXRDOVU/view?usp=sharing) gives this [output.png](https://drive.google.com/file/d/0B0moMeZtYrkQUzNnMWk5WXNDWU0/view?usp=sharing) – Dipankar Nalui Apr 16 '17 at 06:08
0

Example grid : pbcast_sim.tcl + p_bcast.tcl http://disc.ece.illinois.edu/downloads/lab108.html → → http://disc.ece.illinois.edu/downloads/pbcast_sim.tcl and http://disc.ece.illinois.edu/downloads/p_bcast.tcl

Simulation example : $ ns pbcast_sim.tcl -prob 20

enter image description here

Knud Larsen
  • 5,753
  • 2
  • 14
  • 19
  • `pbcast_sim.tcl` file depends on `p_bcast.tcl`. This program displays 400 nodes. node0 to node399. `pbcast_sim.tcl` Line no 49: `set val(nn) 400 ;# default number of mobilenodes` if I change 400 to 20 nodes it gives error mentioned in the next comment. – Dipankar Nalui Apr 16 '17 at 14:18
  • `dipankar@dipankar-Lenovo ~/Desktop$ ns pbcast_sim.tcl -prob 20 num_nodes is set 20 warning: Please use -channel as shown in tcl/ex/wireless-mitf.tcl INITIALIZE THE LIST xListHead can't read "node_(20)": no such element in array while executing "$node_($a) set X_ [expr 0.0 + [ expr $i * $gridspace]]" ("for" body line 4) invoked from within "for {set i 0} {$i < $val(rlen) } {incr i} { for {set j 0} {$j < $val(rlen) } {incr j} { set a [expr $j + [expr $i * $val(rlen)]] $node_($a) se..." (file "pbcast_sim.tcl" line 153) dipankar@dipankar-Lenovo ~/Desktop$` – Dipankar Nalui Apr 16 '17 at 14:19
  • i want only node0 to nodes20. How to set that? – Dipankar Nalui Apr 16 '17 at 14:21