How to set position and orientation of nodes displayed in NAM using TCL code?

4.2k Views Asked by At

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

2

There are 2 best solutions below

10
Donal Fellows On

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).

3
Knud Larsen On

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