학부 4학년 2학기에 작성했던 코드.
ns2 시뮬레이션 결과를 그리려고 했던 것이다.
여러 데이터에 대해 그래프를 그려야 되는데 매번 그리기도 어려우므로 이런 식으로 했다.
사실 gnuplot 사용법도 되지만 bashrc 사용법도 된다.
bashrc에서 루프나 변수 사용은 매번 헷갈리므로
hw4.sh
그래프 다섯 개를 그렸다.
#! /bin/bash
rm -f hw4.log
for i in 50 1000; do # paket size
for j in 0 2000; do # RTS Threshold
# clean data files
rm -f node0.dat
rm -f node1.dat
rm -f total.dat
for k in {1..5}; do # N
# run ns
ns hw4.tcl $k $i $j
done
mv total.dat total_${i}_$j.dat
# Plot
echo > hw4.plot
echo "set term png" >> hw4.plot
echo "set grid" >> hw4.plot
echo "set key default" >> hw4.plot
echo "set xlabel \"\"" >> hw4.plot
echo "set ylabel \"Mbps\"" >> hw4.plot
echo "set xrange [0:6]" >> hw4.plot
echo "set xtics 1,1,5" >> hw4.plot
echo "set title \"hw4_${i}_$j\"" >> hw4.plot
echo "set output \"hw4_${i}_$j.png\"" >> hw4.plot
echo "plot \"node0.dat\" with linespoints title \"node0\", "\
"\"node1.dat\" with linespoints title \"node1\"" >> hw4.plot
echo "show output" >> hw4.plot
gnuplot hw4.plot
done
done
# Plot
echo > hw4.plot
echo "set term png" >> hw4.plot
echo "set grid" >> hw4.plot
echo "set key default" >> hw4.plot
echo "set xlabel \"\"" >> hw4.plot
echo "set ylabel \"Mbps\"" >> hw4.plot
echo "set xrange [0:6]" >> hw4.plot
echo "set xtics 1,1,5" >> hw4.plot
echo "set title \"hw4_total\"" >> hw4.plot
echo "set output \"hw4_total.png\"" >> hw4.plot
echo "plot \"total_50_0.dat\" with linespoints title \"50_0\", "\
"\"total_50_2000.dat\" with linespoints title \"50_2000\", "\
"\"total_1000_0.dat\" with linespoints title \"1000_0\", "\
"\"total_1000_2000.dat\" with linespoints title \"1000_2000\"" >> hw4.plot
echo "show output" >> hw4.plot
gnuplot hw4.plot
# Define options
# ======================================================================
set opt(chan) Channel/WirelessChannel ;# channel type
set opt(prop) Propagation/TwoRayGround ;# radio-propagation model
set opt(netif) Phy/WirelessPhy ;# network interface type
set opt(mac) Mac/802_11 ;# MAC type
set opt(ifq) Queue/DropTail/PriQueue ;# interface queue type
set opt(ll) LL ;# link layer type
set opt(ant) Antenna/OmniAntenna ;# antenna model
set opt(ifqlen) 50 ;# max packet in ifq
set opt(rp) DSDV ;# routing protocol
set opt(start) 1
set opt(stop) 30
set opt(N) [lindex $argv 0]
set opt(nn) [expr 1 + $opt(N)] ;# number of mobilenodes
set opt(packetSize) [lindex $argv 1]
set opt(RTSThreshold) [lindex $argv 2]
set num_bs_node 1
$opt(mac) set dataRate_ 2Mb
$opt(mac) set basicRate_ 1Mb
$opt(mac) set RTSThreshold_ $opt(RTSThreshold)
#Create a simulator
set ns_ [new Simulator]
# set up for hierarchical routing
$ns_ node-config -addressType hierarchical
AddrParams set domain_num_ 2
lappend cluster_num 1 1
AddrParams set cluster_num_ $cluster_num
lappend eilastlevel 1 [expr $num_bs_node + $opt(nn)]
AddrParams set nodes_num_ $eilastlevel
# set up topography object
set topo [new Topography]
$topo load_flatgrid 700 700
# Create God
create-god [expr $num_bs_node + $opt(nn)]
#create wired nodes
set W [$ns_ node 0.0.0]
# configure node for base station
$ns_ node-config -adhocRouting $opt(rp) \
-llType $opt(ll) \
-macType $opt(mac) \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propInstance [new $opt(prop)] \
-phyType $opt(netif) \
-channel [new $opt(chan)] \
-topoInstance $topo \
-wiredRouting ON \
-agentTrace OFF \
-routerTrace OFF \
-macTrace OFF \
-movementTrace OFF
#create base station
set BS [$ns_ node 1.0.0]
$BS random-motion 0
# position of base station.
$BS set X_ 200
$BS set Y_ 0
$BS set Z_ 0
# configure for mobilenodes
$ns_ node-config -wiredRouting OFF
# create mobilenodes
for {set i 0} {$i < $opt(nn)} {incr i} {
set node_($i) [$ns_ node 1.0.[expr $i + 1]]
$node_($i) random-motion 0 ;# disable random motion
$node_($i) base-station [AddrParams addr2id [$BS node-addr]]
}
# Provide initial (X,Y, for now Z=0) co-ordinates for mobilenodes
# node at the left of the base station.
$node_(0) set X_ 0.0
$node_(0) set Y_ 0.0
$node_(0) set Z_ 0.0
# nodes at the right of the base station.
for {set i 1} {$i < $opt(nn)} {incr i} {
$node_($i) set X_ [expr 225 + 25 * $i]
$node_($i) set Y_ 0.0
$node_($i) set Z_ 0.0
}
# create a link between wired and BS node.
$ns_ duplex-link $W $BS 100Mb 2ms DropTail
# create a sink at the wired node
set sink(0) [new Agent/LossMonitor] ;#for node0.
set sink(1) [new Agent/LossMonitor] ;#for node1.
set sink(2) [new Agent/LossMonitor] ;#for the other nodes.
$ns_ attach-agent $W $sink(0)
$ns_ attach-agent $W $sink(1)
$ns_ attach-agent $W $sink(2)
# create UDP agents at mobilenodes,
# create CBR traffics,
# and make connections,
for {set i 0} {$i < $opt(nn)} {incr i} {
set udp($i) [new Agent/UDP]
$ns_ attach-agent $node_($i) $udp($i)
set cbr($i) [new Application/Traffic/CBR]
$cbr($i) set packetSize_ $opt(packetSize)
$cbr($i) set rate_ 2Mb
$cbr($i) attach-agent $udp($i)
# schedule
$ns_ at $opt(start) "$cbr($i) start"
$ns_ at $opt(stop) "$node_($i) reset";
}
$ns_ connect $udp(0) $sink(0)
$ns_ connect $udp(1) $sink(1)
for {set i 2} {$i < $opt(nn)} {incr i} {
$ns_ connect $udp($i) $sink(2)
}
# open record file
set f0 [open node0.dat a+]
set f1 [open node1.dat a+]
set f2 [open total.dat a+]
# define a stop procedure
proc stop {} {
global ns_ tracefd f0 f1 f2 sink opt
$ns_ flush-trace
close $tracefd
set time [expr $opt(stop) - $opt(start)]
# record average throughput
set throughput [expr [$sink(0) set bytes_]*8.0/$time/1000000]
puts $f0 "$opt(N) $throughput"
set throughput [expr [$sink(1) set bytes_]*8.0/$time/1000000]
puts $f1 "$opt(N) $throughput"
set total [expr [$sink(0) set bytes_]]
set total [expr $total + [$sink(1) set bytes_]]
set total [expr $total + [$sink(2) set bytes_]]
set throughput [expr $total*8.0/$time/1000000]
puts $f2 "$opt(N) $throughput"
close $f0
close $f1
close $f2
exit 0
}
# schedule
$ns_ at $opt(stop) "stop"
puts "Starting Simulation..."
$ns_ run
그림 그린 결과도 올린다.
hw3.sh도 비슷하다. unset border가 있는정도?
그래프 9 개를 그렸다.
#! /bin/bash
#ns hw3.tcl
# Plot
echo > hw3.plot
echo "set term png" >> hw3.plot
echo "set grid" >> hw3.plot
echo "set key default" >> hw3.plot
echo "set xlabel \"s\"" >> hw3.plot
echo "set ylabel \"\"" >> hw3.plot
echo "set y2label \"Mbps\"" >> hw3.plot
echo "set ytics nomirror" >> hw3.plot
echo "set y2tics" >> hw3.plot
echo "unset border" >> hw3.plot
for i in {0..7}; do
# Plot
echo "set title \"hw3_$i\"" >> hw3.plot
echo "set output \"hw3_$i.png\"" >> hw3.plot
echo "plot \"out1_$i.dat\" title \"cwnd_\" axes x1y1 with lines, "\
"\"out2_$i.dat\" title \"Throughput\" axes x1y2 with lines" >> hw3.plot
echo show output >> hw3.plot
done
# Plot2
echo > hw3_2.plot
echo "set term png" >> hw3_2.plot
echo "set grid" >> hw3_2.plot
echo "set key default" >> hw3_2.plot
echo "set xlabel \"s\"" >> hw3_2.plot
echo "set ylabel \"Mbps\"" >> hw3_2.plot
echo "unset border" >> hw3_2.plot
echo "set output \"hw3_allinone.png\"" >> hw3_2.plot
echo "set title \"hw3_allinone\"" >> hw3_2.plot
echo show output >> hw3_2.plot
echo "plot "\
"\"out2_0.dat\" title \"Throughput0\" with lines,"\
"\"out2_1.dat\" title \"Throughput1\" with lines,"\
"\"out2_2.dat\" title \"Throughput2\" with lines,"\
"\"out2_3.dat\" title \"Throughput3\" with lines,"\
"\"out2_4.dat\" title \"Throughput4\" with lines,"\
"\"out2_5.dat\" title \"Throughput5\" with lines,"\
"\"out2_6.dat\" title \"Throughput6\" with lines,"\
"\"out2_7.dat\" title \"Throughput7\" with lines" >> hw3_2.plot
gnuplot hw3.plot
gnuplot hw3_2.plot
hw2_2_3.sh는 다음과 같다.
무려 그래프 36 개를 그렸다. hw2를 보면 알겠지만 가장 먼저 작성한 코드.
36개를 수작업으로 그릴 수 없어서 이렇게 했던 것.
#! /bin/bash
# Plot
echo > hw2.plot
echo "set term png" >> hw2.plot
echo "set grid" >> hw2.plot
echo "set key default" >> hw2.plot
echo "set xlabel \"s\"" >> hw2.plot
# Plot2
echo > hw2_2.plot
echo "set term png" >> hw2_2.plot
echo "set grid" >> hw2_2.plot
echo "set key default" >> hw2_2.plot
echo "set xlabel \"s\"" >> hw2_2.plot
echo "set ylabel \"\"" >> hw2_2.plot
echo "set y2label \"Mbps\"" >> hw2_2.plot
echo "set y2tics" >> hw2_2.plot
echo "unset border" >> hw2_2.plot
it=('TCP' 'TCP/Reno' 'TCP/Newreno')
for i in 0 1 2; do
for j in '0.1Mb' '0.5Mb' '1Mb' '2Mb'; do
ns hw2_2_1.tcl $i $j
# TCP congestion window.
mv out1.tr hw2_cwnd_${i}_${j}.dat
# TCP and CBR throughput.
perl hw2_2_2.pl out.tr 3 1 > hw2_throughput_${i}_${j}.dat
# Plot
echo "set title \"hw2_cwnd_${it[$i]}_${j}\"" >> hw2.plot
echo "set ylabel \"\"" >> hw2.plot
echo "set output \"hw2_cwnd_${i}_${j}.png\"" >> hw2.plot
echo "plot \"hw2_cwnd_${i}_${j}.dat\" title \"cwnd_\" with lines" >> hw2.plot
echo "show output" >> hw2.plot
echo "set title \"hw2_throughput_${it[$i]}_${j}\"" >> hw2.plot
echo "set ylabel \"Mbps\"" >> hw2.plot
echo "set output \"hw2_throughput_${i}_${j}.png\"" >> hw2.plot
echo "plot \"hw2_throughput_${i}_${j}.dat\" using 1:2 title \"TCP\" with lines, "\
"\"hw2_throughput_${i}_${j}.dat\" using 1:3 title \"UDP\" with lines" >> hw2.plot
echo show output >> hw2.plot
# Plot2
echo "set title \"hw2_${it[$i]}_${j}\"" >> hw2_2.plot
echo "set output \"hw2_${i}_${j}.png\"" >> hw2_2.plot
echo "plot \"hw2_cwnd_${i}_${j}.dat\" title \"cwnd_\" with lines, "\
"\"hw2_throughput_${i}_${j}.dat\" using 1:2 title \"TCP\" axes x1y2 with lines, "\
"\"hw2_throughput_${i}_${j}.dat\" using 1:3 title \"UDP\" axes x1y2 with lines" >> hw2_2.plot
echo show output >> hw2_2.plot
done
done