用 ns2 寫模擬很無聊 !?
不方便到沒有安裝 ns2 的電腦 demo !?
如果能把模擬結果變成 demo video 應該很讚吧 !?
以下介紹如何把 nam 看到的結果變成模擬影片 (avi)
2008/07/11
處理 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 功能非常強大
只要用習慣了
就會覺得非常方便喔
是個不錯的 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 在模擬結束時出現出現以下錯誤
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]
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.
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.
2008/06/28
三種解 Recursive Function 的方法
1. The iteration method (疊代法, 或代入展開法)
2. 畫 Recursion Tree
3. Master Theorem
2. 畫 Recursion Tree
3. Master Theorem
Subscribe to:
Posts (Atom)