Friday 24 May 2019

Good habits








Plotting X graph in NS2

Description:

     X, Y coordinates points for more than one graph is generated randomly and put it in the trace files. All the trace files are given as input file to xgraph to plot all the graphs in the same plot.
File name: “graph2.tcl”
#-------Event scheduler object creation--------#
set ns [new Simulator]
#Creating nam file:
set nf [open Tcpred.nam w]
$ns namtrace-all $nf
#Open the trace file
set nt [open Tcpred.tr w]
$ns trace-all $nt

# graph procedure..
$ns at 1.0 "Graph"
set g [open graph.tr w]
set g1 [open graph1.tr w]
proc Graph {} {
global ns g g1
set time 1.0
set now [$ns now]
puts $g "[expr rand()*8] [expr rand()*6]"
puts $g1 "[expr rand()*8] [expr rand()*6]"
$ns at [expr $now+$time] "Graph"
}
           
                 
#Finish Procedure..
proc finish {} {
      global ns nf nt
      exec xgraph -brb -geometry 600X500 graph.tr graph1.tr &
      exit 0
      }
#Calling finish procedure
$ns at 5.0 "finish"
$ns run
# How to run
$ns filename.tcl

Plot a graph of node energy from NS2 trace file

use the below awk script to find the energy of node
#!/usr/bin/awk -f
BEGIN{
#print "energy of node 1"
data=0
}

{
if ($1=="N" && $5==0)
{
    print $3,$7
}
}
END{
#print "Completed"
}
save this file as energy.awk
in terminal type
$gawk -f energy.awk "yourtracefile.tr" > node0.dat
on executing this command you'll get a file node0.dat
in terminal
$xgraph -P node0.dat
you'll get a graph