2008/07/20

2008/07/12

NS2 Energy Model

在 ns2 中套用有 energy 限制的環境方法如下

使用舊的 trace format 則可以看到

[energy 979.917000 ei 20.074 es 0.000 et 0.003 er 0.006]

這種 trace log

分別代表

energy: total remaining energy
ei: energy consumption in IDLE state
es: energy consumption in SLEEP state
et: energy consumed in transmitting packets
er: energy consumed in receiving packets

[code]
# ...

set opt(energymodel) EnergyModel ;# EnergyModel type
set opt(initenergy) 1.0 ;# Initial Energy, specified in Joules
set opt(rP) 1.0 ;# Receiving Power, specified in Watts
set opt(tP) 1.1 ;# Transmitting Power, specified in Watts
set opt(iP) 0.01 ;# Idle Power, specified in Watts
set opt(sP) 0.001 ;# Sleep Power, specified in Watts
set opt(transP) 0.2 ;# Transition Power, specified in Watts
set opt(transT) 0.005 ;# Transition Time, specified in Seconds

# ...

#$ns use-newtrace

# ...

$ns node-config -adhocRouting $opt(rp) \
-llType $opt(ll) \
-macType $opt(mac) \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propType $opt(prop) \
-phyType $opt(netif) \
-channel [new $opt(chan)] \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace ON \
-energyModel $opt(energymodel) \
-initialEnergy $opt(initenergy) \
-rxPower $opt(rP) \
-txPower $opt(tP) \
-idlePower $opt(iP) \
-sleepPower $opt(sP) \
-transitionPower $opt(transP) \
-transitionTime $opt(transT)

# ...
[/code]

2008/07/11

Change wireless color as in wire network

Reference: http://mailman.isi.edu/pipermail/ns-users/2002-May/023058.html?

NS2 Wireless Simulation 中要讓 node 有顏色

只能用下面這種方法

[code]
# ...

# let the node has a color property
$node($i) color black

# change the color during animation
$ns at 0.0 "$node($i) color green"

# ...
[/code]



如果設定 node 的 label-color 會讓 node 的位置變成 (0, 0)



如果想要兩 node 間有線(ex: node 1, 2 之間)

在 tcl 檔內可以用下面這種方法

[code]$ns puts-nam-traceall "l -t * -s 1 -d 2 -c RoyalBlue -S COLOR"

$ns at 0.3 "$ns puts-nam-traceall \"l -t 0.30000000000000 -s 1 -d 2 -c blue -S COLOR\""
$ns at 0.4 "$ns puts-nam-traceall \"l -t 0.40000000000000 -s 1 -d 2 -c red -S COLOR\""
$ns at 0.5 "$ns puts-nam-traceall \"l -t 0.50000000000000 -s 1 -d 2 -S out\""
$ns at 0.6 "$ns puts-nam-traceall \"l -t 0.60000000000000 -s 1 -d 2 -c blue -S COLOR\""
$ns at 0.7 "$ns puts-nam-traceall \"l -t 0.70000000000000 -s 1 -d 2 -c red -S COLOR\""
$ns at 0.8 "$ns puts-nam-traceall \"l -t 0.80000000000000 -s 1 -d 2 -S out\""
[/code]

或是寫在 Agent 用下面這種方法

[cpp]Tcl::instance().evalf("[Simulator instance] puts-nam-traceall \"l -t "TIME_FORMAT" -s %d -d %d -c %s -S COLOR\"", Scheduler::instance().clock(), 1, 2, "blue");[cpp]

其他可以使用的顏色請參考 /usr/lib/X11/rgb.txt 檔案

關於 nam 讀取的格式,可以在 X window 的環境下
用 nam -p 指令查詢

如果想要用 man 查詢 nam 請將 nam.1 複製到 /usr/man/man1
ex: cp ~/ns-allinone-2.33/nam-1.13/nam.1 /usr/man/man1/

ns2 中移動 node

1. 隨意移動

[code]
# ...

$node($i) random-motion 1

# ...

$ns at 1.0 "$node($i) start"

# ...
[/code]

把 NAM 模擬結果作成動畫

用 ns2 寫模擬很無聊 !?

不方便到沒有安裝 ns2 的電腦 demo !?

如果能把模擬結果變成 demo video 應該很讚吧 !?

以下介紹如何把 nam 看到的結果變成模擬影片 (avi)

處理 NS2 Scenarios Generator 產生的 tcl

NS2 Scenarios Generator
是個不錯的 ns2 scenario 產生工具

但是產生出來的 tcl 可能在命名上不是我所慣用的

舉例來說

他會產生以下這種表示 node 的方式

[code]$n0 set X_ 907
$n0 set Y_ 975
$n0 set Z_ 0.0[/code]

但我比較習慣寫成類似 array 的方式

例如

[code]$node(0) set X_ 907
$node(0) set Y_ 975
$node(0) set Z_ 0.0[/code]

這個時候問題來了

如果有 1000 個點

總不可能一個一個手動改吧 囧

這個時候 vim 的搜尋取代功能就非常好用了

利用以下寫法就能一口氣換過來喔

:%s/\$n\([0-9]\+\)/$node(\1)/g


想要瞭解更多 vim 的用法請參考這篇文章

以下介紹我覺得最常用到的

1. 單純置換字串

:%s/old string/new string/g
以上用法會把檔案內所有的 old string 變成 new string

2. 利用 regular expression 置換字串

:%s/[0-9]\+/number/g
以上用法會把數字(ex: 1, 12, 130, ... etc)變成字串 number

:%s/a.b/string/g
以上用法會把字串 aab, abb, a0b, a9b, a!b, ... 等,a開頭b結尾,三個字為一組的字串變成字串 string

3. 利用 regular expression 把比對出的字串重新組合出新的字串置換

:%s/\(abc\) \(def\)/This is \1 & \2/g
以上用法會把字串 abc def 變成字串 This is abc & def



vim 功能非常強大

只要用習慣了

就會覺得非常方便喔

2008/07/10

執行 nam 時出現 "Missing required flag" error

Reference: http://mailman.isi.edu/pipermail/ns-users/2003-April/031477.html

Problem: ns2 在執行 nam 在模擬結束時出現出現以下錯誤


Missing required flag -x in: W -t 5.0

Missing required flag -y in: W -t 5.0

Parsing error in event.



Solution:
發生的原因是因為找不到 nam 檔寫入
也就是說寫了關閉 nam 檔後
仍然有資料要寫入 nam 檔
因此要解決就是關閉 nam 檔前
tcl 要先執行到 $ns at $opt(stop) "$ns nam-end-wireless $opt(stop)"

關閉模擬的 example:
[code]set opt(stop) 5.0 ;# time of simulation end

# Create a ns simulator
set ns [new Simulator]

# ...

# Open the NS trace file
$ns use-newtrace
set tracefile [open out.tr w]
$ns trace-all $tracefile

# Open the NAM trace file
set namfile [open out.nam w]
$ns namtrace-all $namfile
$ns namtrace-all-wireless $namfile $opt(x) $opt(y)

# ...

#===================================
# Termination
#===================================

# Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile opt
$ns at $opt(stop) "$ns nam-end-wireless $opt(stop)"
$ns flush-trace
close $tracefile
close $namfile

puts "done"
$ns halt

puts "running nam..."
exec nam out.nam &
exit 0
}
for {set i 0} {$i < $opt(nn)} {incr i} {
$ns at $opt(stop) "$node($i) reset"
}
$ns at $opt(stop) "finish"

puts "Starting Simulation..."
$ns run[/code]

[Solution] Warning: Tracefile events are not sorted by time

Reference: [patch] NAM Time precison and wireless packet coloring (cmu-trace.cc)

Problem: NS2 shows the "Warning: Tracefile events are not sorted by time" warning.

Solution: Modify file "ns-allinone-2.32/ns-2.32/trace/cmutrace.cc"

Any %.9f characters in the file change to "TIME_FORMAT" characters.

AWK

中文:
http://linux.tnc.edu.tw/techdoc/shell/x792.html
gawk 手冊 (mirror)
http://www.study-area.org/cyril/scripts/scripts/node61.html
AWK Tutorial Guide

English:
The GNU Awk User's Guide