NS2-tcl - 将场分成正方形?

时间:2015-04-23 23:09:52

标签: tcl area ns2 divide

我正在研究ns2.35-ubuntu10.04。我想通过使用TCL脚本将传感器字段划分为9个相等大小的正方形。这是我的第一次尝试,但没有结果。 (除法的步骤由星号和attachment picture分隔。)

# 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(rp)      AODV                       ;  # routing protocol  
set val(x)       900                         ;  # X dimension of topography  
set val(y)       900                         ;  # Y dimension of topography   
set val(stop)    10                          ;  # time of simulation end  
set val(Y)        0;  
set val(X)        0;   
set val(nn)      30 ;  


#Creating simulation:  
set ns [new Simulator]  

#Creating nam and trace file:  
set tracefd [open k.tr w]  
set namtrace [open k.nam w]     
$ns trace-all $tracefd  
$ns namtrace-all-wireless $namtrace $val(x) $val(y)  

# set up topography object   
set topo [new Topography]    
$topo load_flatgrid $val(x) $val(y)    
set god_ [create-god $val(nn)]   

# 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 \  
                -agentTrace ON \  
                -routerTrace ON \  
                -macTrace OFF \  

############ BEGIN ############
set $val(X)  [expr $val(x)/300]  
set $val(Y)  [expr $val(y)/300]  
for {set i 0} {$i<=$val(X)} {incr i}   
{  
for {set j 0} {$j<=$val(Y)}  {incr i}  
{  
for {set b 0} {$b < $val(nn) } { incr b }   
 {
     set node($b) [$ns node]
}  
}  
}
############ END ############

#stop procedure..   

proc stop {}   {
    global ns tracefd namtrace  
    $ns flush-trace  
    close $tracefd  
    close $namtrace  
    exec nam k.nam &  
 }

$ns at $val(stop) "stop"  

$ns run  

1 个答案:

答案 0 :(得分:0)

只看你突出显示的代码:

set $val(X)  [expr $val(x)/300]  
set $val(Y)  [expr $val(y)/300]  
for {set i 0} {$i<=$val(X)} {incr i}   
{  
for {set j 0} {$j<=$val(Y)}  {incr i}  
{  
for {set b 0} {$b < $val(nn) } { incr b }   
 {
     set node($b) [$ns node]
}  
}  
}

主要问题是你已经在自己的线上打开括号。不要在Tcl那样做;语言非常更喜欢One True Brace风格(因为换行很重要,是命令终止符)。如果我们改为这个(缩进只是为了清晰起见):

set $val(X) [expr $val(x)/300]
set $val(Y) [expr $val(y)/300]
for {set i 0} {$i<=$val(X)} {incr i} {
    for {set j 0} {$j<=$val(Y)} {incr i} {
        for {set b 0} {$b < $val(nn)} {incr b} {
            set node($b) [$ns node]
        }
    }
}

然后代码将运行。它可能不会产生你期望的结果,因为你没有在最里面的循环中使用ij值。当你创建一个节点时,你需要以某种方式告诉它在网格中的位置。

此外,您正在使用三个嵌套循环,因此您(可能)正在制作3D网格。这是你想要的吗? (因为您还只是保存关键字中一个维度的信息,这可能意味着您只使用该网格的一小部分并完全失去对其他未分化节点的跟踪。)