Monday, August 5, 2013

NS2 energy analysis awk script

Hi All,

Awk programming is very easy to learn and simple to apply. Awk in NS2 takes the trace file for analysis. $1, $2, $3 etc are used to access column values from the trace file.

Generally an awk program will have a structure as shown below:

BEGIN {<initialization>}
<pattern 1> {<actions>}
<pattern 2> {<actions>}
.
.
.
<pattern N> {<actions>}
END {<final actions>}

Execution of AWK script:
        awk -f filename.awk tracefile.tr

Given below is the code for energy analysis using awk script.


BEGIN {
        seqno = -1;
        start_time = 0;

      }

{
action = $1;
time = $2;
node_id = $3;
layer = $4;
flags = $5;
seqno = $6;
type = $7;
size = $8;
a = $9;
b = $10;
c = $11;
d = $12;
energy = $14;

if(seqno < $6 && node_id == "_4_" && (action == "r" || action == "s")) {
        seqno = $6;
        remaining_energy = energy;
        start_time = time;
        printf("%f\t%f\n",start_time,remaining_energy);
      }
}

END {
}

Thursday, August 1, 2013

Set different initial energy for nodes in ns2

The following script can be used to initialize different nodes with different energy levels. You can also create an array of nodes and initialize different nodes at different energy levels inside a loop if you want.

# set initial energy for first node
$ns node-config -initialEnergy 50
set node_(0) [$ns node]

# set initial energy for second node
$ns node-config -initialEnergy 100

set node_(1) [$ns node]



# set initial energy for third node

$ns node-config -initialEnergy 70
set node_(2) [$ns node]


and so on for the other nodes.