#5, First Floor, 4th Street , Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to find energy consumption of the network in ns2?


The energy model represents the energy level of nodes in the network. The energy model defined in a node has an initial value that is the level of energy the node has at the beginning of the simulation. This energy is termed as initialEnergy_. In simulation, the variable “energy” represents the energy level in a node at any specified time. The value of initialEnergy_ is passed as an input argument. A node looses a particular amount of energy for every packet transmitted and every packet received. As a result, the value of initialEnergy_ in a node gets decreased. The energy consumption level of a node at any time of the simulation can be determined by finding the difference between the current energy value and initialEnergy_ value. If an energy level of a node reaches zero, it cannot receive or transmit anymore packets. The amount of energy consumption in a node can be printed in the trace file. The energy level of a network can be determined by summing the entire node’s energy level in the network.

Sample Code

#Filename: sample23.tcl

#*************ENERGY_CONSUMPTION ***************************88

#******************ENERGY MODEL ***************************88
# Simulator Instance Creation
set ns [new Simulator]

#Fixing the co-ordinate of simutaion area
set val(x) 500
set val(y) 500
# Define options
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# MAC type
set val(ifq) Queue/DropTail/PriQueue ;# interface queue type
set val(ll) LL ;# link layer type
set val(ant) Antenna/OmniAntenna ;# antenna model
set val(ifqlen) 50 ;# max packet in ifq
set val(nn) 10 ;# number of mobilenodes
set val(rp) AODV ;# routing protocol
set val(x) 500 ;# X dimension of topography
set val(y) 400 ;# Y dimension of topography
set val(stop) 10.0 ;# time of simulation end
set val(energymodel) EnergyModel ;#Energy set up
# set up topography object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)

#Energy details file
set en [open energy_consumption.tr w]

#Transmission range setup

#********************************** UNITY GAIN, 1.5m HEIGHT OMNI DIRECTIONAL ANTENNA SET UP **************

Antenna/OmniAntenna set X_ 0
Antenna/OmniAntenna set Y_ 0
Antenna/OmniAntenna set Z_ 1.5
Antenna/OmniAntenna set Gt_ 1.0
Antenna/OmniAntenna set Gr_ 1.0

#********************************** SET UP COMMUNICATION AND SENSING RANGE ***********************************

#default communication range 250m

# Initialize the SharedMedia interface with parameters to make
# it work like the 914MHz Lucent WaveLAN DSSS radio interface
$val(netif) set CPThresh_ 10.0
$val(netif) set CSThresh_ 2.28289e-11 ;#sensing range of 500m
$val(netif) set RXThresh_ 2.28289e-11 ;#communication range of 500m
$val(netif) set Rb_ 2*1e6
$val(netif) set Pt_ 0.2818
$val(netif) set freq_ 914e+6
$val(netif) set L_ 1.0
# configure the nodes
$ns node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-energyModel $val(energymodel) \
-initialEnergy 10 \
-rxPower 0.5 \
-txPower 1.0 \
-idlePower 0.0 \
-sensePower 0.3 \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON

# Node Creation

for {set i 0} {$i < 3} {incr i} {

set energy($i) [expr rand()*500]

$ns node-config -initialEnergy $energy($i) \
-rxPower 0.5 \
-txPower 1.0 \
-idlePower 0.0 \
-sensePower 0.3

set node_($i) [$ns node]
$node_($i) color black
set E($i) $energy($i)


for {set i 0} {$i < $val(nn) } { incr i } {

set CE($i) [expr $InitialEnergy($i) – $FinalEnergy($i)]
puts $en "Energy consumption($i) = $CE($i)"

set energyConsumption 0

for {set i 0} {$i < $val(nn) } { incr i } {

set energyConsumption [expr $CE($i) + $energyConsumption]