关于NS-2仿真中移动节点的设置

关于NS-2仿真中移动节点的设置

 

要做一个4个节点的Ad hoc网络仿真,实验环境和要求是:
cf&p4W�H0s1[0u1i"ED u W0采 用的无线网拓扑结构,主机节点数共有4个;节点1到4的初始位置坐标分别为(0,1 000)、(0,800)、(0,600)和(400,600),节点1和2在仿真中保持静止不动,节点3和4则以60m/s速度分别向坐标(0,0)和 (400,0)位置方向垂直移动。系统带宽为2Mbps,系统节点缺省无线传输半径约为500m。仿真使用的业务流量为FTP,每包发送512字节,发送 速度10包/s。采用静态路由方式,节点1在1.5s发包给节点2,节点3在3.5s发包给节点4。仿真时间一共10s,选取节点2处的估计可用带宽进行 分析。 西子博客sY?6M%U)e,F1j HJ3O

 

|b yZXyM0 西子博客0J~/jYw

下面是基本环境仿真的adhoc.tcl源码:西子博客^9X3dz8sb}x/P

西子博客KwQyL!L


Fdv yj-H0# This script is created by emile.Y.S Xiang西子博客5XcK,M3p$L Lx n
#===================================
6b ] ^fd-M!u$Em0#              定义模拟变量                           西子博客u"C2l]iG2R'?
#===================================
'q N2|7w8/_&O0set val(chan)   Channel/WirelessChannel            ;# channel type
QY&ZNU&_0set val(prop)   Propagation/TwoRayGround           ;# radio-propagation model
~�g�bI9@&XE0set val(netif)  Phy/WirelessPhy                    ;# network interface type
;m'G y&f&e6KK0set val(mac)    Mac/802_11                         ;# MAC type
0c.|s/X2]0set val(ifq)    Queue/DropTail/PriQueue            ;# interface queue type
:~ D q D7px&|z0set val(ll)     LL                                 ;# link layer type
-eY;{2PpCl%h0set val(ant)    Antenna/OmniAntenna                ;# antenna model
kc%Yw5S0set val(x)      1000                               ;# X dimension of topology西子博客`^#p:] _(Ms5a
set val(y)      1000                               ;# Y dimension of topology西子博客*g7v/m�?]R-H
set val(cp)     ""                                 ;# node movement model file
+TY3@8u})[%}4B BA0set val(sc)     ""                                 ;# traffic model file
f6H-X+g3`~0set val(ifqlen) 50                                 ;# max packet in ifq西子博客,]0fH E%oB3wX
set val(nn)     4                                  ;# number of mobilenodes西子博客"D/;Y-do i/TB%X
set val(seed)   0.0
1Vf3qyp/G,I0set val(stop)   10.0                               ;# time of simulation end西子博客 v?*P�?b
set val(tr)     adhoc.tr                           ;# trace file name
USw)/Qz/@UO0set val(rp)     DSDV                               ;# routing protocol
GO(_r*a8y0set AgentTrace  ON西子博客 K%FI)i ^9cW+O
set RouterTrace ON西子博客8? G!U Mu-Y$v9c
set MacTrace    OFF西子博客%g"LqkfjL
#===================================
Ke}aX"{W0#              建立相关档案         西子博客0JTU1vY4[ZzR4n v-^ o
#===================================
!}K&l)Jl/y [F0# Initialize Global Variables西子博客/u/q5V6^
set ns_    [new Simulator]                          ;#产生ns simulator实例
Ew,~2U } @ Lb0$ns_ color 1 Blue
0K*j:v4V#i9G4z+D0$ns_ color 2 Red西子博客 e5GOJs
 
}Tf BPRbS+G,F0# 设定trace file西子博客@&W`"lV0O_X tz
$ns_ use-newtrace                                   ;#使用新的trace format西子博客;zO;[,Z%fE zf']
set namfd [open adhoc.nam w]                        ;#产生nam trace file西子博客�J+rbF%v�d*[3^
$ns_ namtrace-all-wireless $namfd $val(x) $val(y)   ;#模拟时产生需要的结果文件西子博客g7X5ow1ezsd:q
set tracefd [open $val(tr) w]                       ;#产生trace file
-@5ux`Oz)w0$ns_ trace-all $tracefd                             ;#模拟时产生需要的结果文件
Fm#T&Y8Ix0 西子博客%t9[1/H[ k)^S
#建立topology对象,以记录mobilenodes在拓扑內移动的情况
vp"I$h+MLD0set topo [new Topography]
/EC vT6Aek-Q0# 拓扑的范围为 1000m x 1000m
*M)m%o-[| w9gTl4X0$topo load_flatgrid $val(x) $val(y)
6})T5v)f3D5r0r5G0 西子博客-Q4_4L8r"}*Ns'NaXX
# 建立channel
L-h'mM8S'c[g0set chan [new $val(chan)]西子博客v.g}2ly7{
 西子博客8k YE1gsm$R P e
# 创建god西子博客/g eFi$K T
set god_ [create-god $val(nn)]
$K5XEoy^)l5T0#===================================
0l*G3j-f SP0#              无线节点配置         西子博客$E v4EX.o$w8v/vC
#===================================
Q]8h9i"C3o#`8K0# Create the specified number of mobile nodes [$val(nn)] and "attach" them to西子博客:T jZE/N/qRV;x
# the channel. Four nodes are created : node(0), node(1), node(2) and node(3)西子博客*k#S"N @4j R
# 设定Mobile Node的参数西子博客HRVy5@6|
$ns_ node-config -adhocRouting $val(rp) /西子博客W/G|bAM
                 -llType $val(ll) /
/r,Wajj0                 -macType $val(mac) /
[email protected]&Wgb | s0                 -ifqType $val(ifq) /西子博客:b(EM6_7flx"n
                 -ifqLen $val(ifqlen) /西子博客3pu^!MlZ
                 -antType $val(ant) /西子博客bf8Z'rt
                 -propType $val(prop) /
}-S(x"k^F8zyl@0                 -phyType $val(netif) /西子博客n-W/X gBu"F
                 -channel $chan /
g$}Iz"V G0                 -topoInstance $topo /
+H)uMs B0bbd0                 -agentTrace ON /西子博客 x&BP _f
                 -routerTrace OFF /
:BQK$YSDwh;z0                 -macTrace OFF /
7FW;NyJ1e3wG6W GX0                 -movementTrace OFF
&noz ad0#===================================
kr @!jt _3S0U:@&p0#              新建Node              西子博客K:s![9/7a:J /u
#===================================
]0NE3z[l0for {set i 0} {$i < $val(nn) } {incr i} {
R6i7kqK%ak5pz0  set node_($i) [$ns_ node]
M-t9/mXE|w0  $node_($i) random-motion 0 ;# 使各节点非随机移动西子博客d"/)g4jc6v|#A
}
4g pjW4b.yj6Jd.[P0 
g.k4N-PKtxGH0# Provide initial (X,Y, for now Z=0) co-ordinates for mobilenodes
%d2q3u7A#~ T/K$pM0# 建立第0个Node,开始时,位置在(0.0, 1000.0)
+LNf9BL0$node_(0) set X_ 0.0
'f8g(@U0D8H/g e@0$node_(0) set Y_ 1000.0西子博客[3j5w;/2MPO;nB1f
$node_(0) set Z_ 0.0西子博客1e|wLso
# 建立第1个Node,开始时,位置在(0.0, 800.0)西子博客@8Hq ?G$S
$node_(1) set X_ 0.0西子博客Y2Hn5^)p4e)eD
$node_(1) set Y_ 800.0西子博客K p:C6`7}$|j(^5c.W ?:W
$node_(1) set Z_ 0.0西子博客GA#^$@-Rx.]
# 建立第2个Node,开始时,位置在(0.0, 600.0)西子博客 E4E? ~p b2XX'w!@b
$node_(2) set X_ 0.0
/Oe.c-Es Q5@C0$node_(2) set Y_ 600.0
8i V#_ ^8Y,^D0$node_(2) set Z_ 0.0
*s!yS9cT0# 建立第3个Node,开始时,位置在(400.0, 600.0)
;KI'kJw0Q4bo&e0$node_(3) set X_ 400.0
)AF!/ j`T)_v T�D0$node_(3) set Y_ 600.0西子博客$a$QL5B;QIm
$node_(3) set Z_ 0.0
|^v_#?5{(P S'`0 
/4i |g0m0# Load the god object with shortest hop information西子博客/g+UyW+A$]e$N
# 在节点0和节点1之间最短的hop数为1
h2T:{RHq3V5iJ0$god_ set-dist 0 1 1
5b_)]"f-qq0# 在节点1和节点2之间最短的hop数为1
/,GRU'LM)i*{S0$god_ set-dist 1 2 1
(Q$}d i#R2y {RgF0# 在节点0和节点2之间最短的hop数为2
K^0E6c0b ?0$god_ set-dist 0 2 2
`]*L5}s0# 在节点2和节点3之间最短的hop数为1
oJS4ce8we.]0$god_ set-dist 2 3 1
"Y}'m&h }E0# 在节点0和节点3之间最短的hop数为1
(iAo/%VmT0$god_ set-dist 0 3 1西子博客/BnGX4s6x2N]
# 在节点1和节点3之间最短的hop数为1
H~)d-D#E:z m a0$god_ set-dist 1 3 1
H} X;t [ J;q;/P8[ g0#===================================西子博客(or8{m7ro
#              产生Movement         西子博客�TnNW"KHx!ohr
#===================================西子博客+CC_M-v;W#O(?-h-@*Av
# Now produce some simple node movements
} ZO5_}+?lh0# Node_(2) and Node_(3) starts to move downward
Zi}.q{0set god_ [God instance]西子博客:Un)mbA,x

H#ewgZ Aln[0# 从0秒开始,节点2开始从位置(0,600)移動到(0,0),速度為60.0 m/s西子博客}'G8CB)s/Q,K
$ns_ at 0.0 "$node_(2) setdest 0.0 0.0 60.0"西子博客M6Mjv7m
# 从0秒开始,节点3开始从位置(400,600)移動到(400,0),速度為60.0 m/s
CO/~rw z!L2`0$ns_ at 0.0 "$node_(3) setdest 400.0 0.0 60.0"
e|2t#mXvj0#===================================
:`/,BT.a`N1O0#       建立FTP业务,基于TCP来承载 
fc&Y;Y_ F#l.O0#===================================
;QxO(^ F$v)M0# 在节点0和节点1间设定第0个连线(FTP-TCP),且在时间为1.5秒开始发送
4/)G&~ACz�V0set tcp0 [new Agent/TCP/Newreno]
'U-`A*y#zH0$tcp0 set fid_ 1
)[,a,E{3i$V,Y�vA0set sink0 [new Agent/TCPSink]
lLpF%g6F+VA0$ns_ attach-agent $node_(0) $tcp0西子博客hon7Fal4H5W"l
$ns_ attach-agent $node_(1) $sink0
+X/p S1RS U!~f0$ns_ connect $tcp0 $sink0西子博客1D xzLec#z|%/&lx)s
set ftp0 [new Application/FTP]西子博客;{MY)x$wo
$ftp0 attach-agent $tcp0西子博客(cKS!U Bk@
$ns_ at 1.5 "$ftp0 start"西子博客$B9jvx%^F
$ns_ at 10.0 "$ftp0 stop"
*/;O GK@*T u%K0 西子博客:~Q8p{x3w
# 在节点2和节点3间设定第1个连线(FTP-TCP),且在时间为3.5秒开始发送西子博客J9A;?g:H2p
set tcp1 [new Agent/TCP/Newreno]西子博客W?/?$/qAr9W
$tcp1 set fid_ 2
ub[_y Q/]0set sink1 [new Agent/TCPSink]
O mjTgy/|/x,A|0$ns_ attach-agent $node_(2) $tcp1
uHm*Mp0$ns_ attach-agent $node_(3) $sink1西子博客#TXu+T0/)w u
$ns_ connect $tcp1 $sink1
;P!z#P,s8o:P(b9q~ C F0set ftp1 [new Application/FTP]
r!_%[*n YG%g0$ftp1 attach-agent $tcp1西子博客'|W |!n K%h2Q%iA
$ns_ at 3.5 "$ftp1 start"西子博客!P.eu0u*EMLo
$ns_ at 10.0 "$ftp1 stop"西子博客+mBfCw)O
 西子博客*Jj]:S I
# 在nam中定义节点初始所在位置
3^2Z6/~E[8b0for {set i 0} {$i < $val(nn)} {incr i} {
:k~ o C(~;DZ0  # The function must be called after mobility model is defined.西子博客Yl{}8?Ae3a
  $ns_ initial_node_pos $node_($i) 60
e K7d+l'Y N0}西子博客-duA VYkE(z[$sfp
 西子博客"IH5tB$c6`;{
# 告诉MobileNode模拟已结束
&x/z9w#c@*~)V.I0for {set i 0} {$i < $val(nn) } {incr i} {西子博客�T-_ q5i'I%P!P ^*Y!q b
  $ns_ at $val(stop) "$node_($i) reset";
t v6Cc;l-A�f]/P0}
?[#k ~r1k0#===================================西子博客3Ac"P8wTv oc
#              结束模拟             西子博客4C?OE`E
#===================================
9McaA e2D0# 结束nam与模拟器
(i'? })J|SwdW0$ns_ at $val(stop) "$ns_ nam-end-wireless $val(stop)"西子博客3{GN d;L| IWKj ],X
$ns_ at $val(stop) "stop"西子博客P"H4o$F(]P#SH'{!l+b:p
$ns_ at $val(stop) "puts /"NS EXITING.../" ; $ns_ halt"
9W(/(i"Xb0 
4H%IJ8x~z c:c'd8d0# 设定模拟器用的stop function西子博客z�@JE/E ])~ u
proc stop {} {西子博客/_6l5tM'/ wx
  global ns_ tracefd namfd西子博客,Tl%G-M@-^
  $ns_ flush-trace西子博客-t cu0v}8`W Q
  close $tracefd西子博客#z�a3j#a;K*E8@_&W�C3f
  close $namfd西子博客6J/Rc,W:O�Gw
  exec nam adhoc.nam &西子博客R3COVB!MH
  exit 0西子博客jvF#n+o3Gz%K1Q8B$^C
}
8@D r"g[X0 
V�T;Uf,H yY0puts $tracefd "M 0.0 nn $val(nn) x $val(x) y $val(y) rp $val(rp)"
[?S/Q"M0y|�a+[&d X0puts $tracefd "M 0.0 sc $val(sc) cp $val(cp) seed $val(seed)"
I Y#_ry0puts $tracefd "M 0.0 prop $val(prop) ant $val(ant)"
&/6Q lO6F K6~*y0puts "Starting Simulation..."
3g/Az'e4ZF0$ns_ run西子博客z&m LE.PJsh

西子博客V v#bV[5`?

西子博客] Vt U)]^
运行ns adhoc.tcl命令后,报错信息如下:西子博客C}S K g8L F;v#? D
ns: _o46 setdest 0.0 600.0 60.0:
X-Bp+C.t,p)RBD0    (_o46 cmd line 1)
v-H�O'z/s2^*yV!g0    invoked from within
]]-eH$E4h|9o0"_o46 cmd setdest 0.0 600.0 60.0"
#R0|;dB/b:v@0    invoked from within西子博客 yJ1X.Or
"catch "$self cmd $args" ret"
"[${%O&UwB0    invoked from within
kC@/9[ K5?v0"if [catch "$self cmd $args" ret] {
A ~4^e JF0set cls [$self info class]西子博客5rNwmm7N
global errorInfo
`jS7B;G8n:J?)Yn0set savedInfo $errorInfo西子博客gz k/$gih
error "error when calling class $cls: $args" $..."西子博客0/bKU aSpg
    (procedure "_o46" line 2)
|J*d;wI lc^k0    (SplitObject unknown line 2)西子博客6Y$u*d+ewu-^1un
    invoked from within
F4h F{p0Ob0"_o46 setdest 0.0 600.0 60.0"西子博客o/U2@#t!p)I0C:~MZ-o

西子博客}3FV b5TN)n|

可以看出问题出在使得两个移动节点移动的代码处(tcl源码中红色部分标记),把移动节点移动的两行代码去掉,ns-2仿真就不报错了,一加上就报错。西子博客7nr3ZG7L/u
我的4个节点是2个固定,2个移动。西子博客Z(k+e'^v)l;A
 西子博客ne v"V.bg ~
反复调试不得其解,于是仔细检查了一下源代码
"U&bsB-h|f3H"m9r ~` Zw0在MobileNode::command(int argc, const char*const* argv)的处理setdest命令的接口中,有一句判断

'@ _$dMU_~8n#j0

a,D!sje&cA0西子博客 ^cL IMi1Y
CODE:if (set_destination(atof(argv[2]), atof(argv[3]), atof(argv[4])) < 0)
){6`'z~;sd^(I0    return TCL_ERROR;
ekO.at)g!FbF0再去看这个函数:MobileNode::set_destination(double x, double y, double s),会发现里面有这一段:西子博客 zH.L,hw0Y

n-i*rf K+jL!e0西子博客 Q)[*S-d S.O%z f
CODE:if(x >= T_->upperX() || x <= T_->lowerX())西子博客e6P&{;Py2QI u!j u
  return -1;
:t `(m f ]7WW0if(y >= T_->upperY() || y <= T_->lowerY())西子博客z#N�^OfZ
  return -1;西子博客"mRB*m3P[7aM
也就是说,你不能让你的Node移动到边界,所以,当把脚本改成:西子博客0wgoAR o8W/
# 从0秒开始,节点2开始从位置(0,600)移動到(0,0),速度為60.0 m/s西子博客w RtS zB ja
$ns_ at 0.0 "$node_(2) setdest 0.1 0.1 60.0 "西子博客b{.Y S3s;H
# 从0秒开始,节点3开始从位置(400,600)移動到(400,0),速度為60.0 m/s
*B? Sj2v0$ns_ at 0.0 "$node_(3) setdest 400.0 0.1 60.0 "

KQ(qL2~ZZ[q0

就好了
z$O]]!F"R9f^ Y#e0 
E7PS0B7^0解决问题的启示就是,遇到解决不了的问题不妨多参看下ns源代码。
 

你可能感兴趣的:(关于NS-2仿真中移动节点的设置)