NS2 NAM not displaying transmissions - tcl

I'm new to NS2 and trying to set up a basic dumbell-shaped network simulation; I have one node with n TCP agents (the gateway or "gw") transmitting across a link to a remote node (remote gateway, "rgw"), which is connected to n TCP sinks (each agent is connected with one sink).
Here is a picture with n = 5. Node 0 has five TCP agents with Pareto traffic generators; each is connected to a TCPSink agent attached to one of the nodes on the right.
I have the following TCL script to set up and test the network; it runs successfully, but when I view the trace in NAM, I can't see the packets moving. I ran a similar tutorial with NAM, and it looked fine, so there must be a bug in my own code. Can anyone help me find my error?
Thanks.
set ns [new Simulator]
# Set up trace
set trc [open out.trc w]
set namtrc [open out.nam w]
$ns trace-all $trc
$ns namtrace-all $namtrc
proc finish {} {
global ns trc
global ns namtrc
$ns flush-trace
close $trc
close $namtrc
exec nam out.nam &
exit 0
}
# Simulation parameters
set n 5
set bw 150000000 # 150 Mb/s
# Set up bottleneck link
set gw [$ns node]
set rgw [$ns node]
$ns duplex-link $gw $rgw $bw .30 DropTail
set snd {} # TCP senders sitting on the gateway node
set trf {} # Traffic generators for the senders
set dst {} # Destination nodes hosting the receivers
set rcv {} # TCP receivers sitting on the destination
for {set i 0} {$i<$n} {incr i} {
# Create the objects needed for a new connection
lappend snd [new Agent/TCP]
lappend rcv [new Agent/TCPSink]
lappend trf [new Application/Traffic/Pareto]
lappend srv [$ns node]
# set up source and destination nodes
$ns attach-agent $gw [lindex $snd $i]
$ns attach-agent [lindex $srv $i] [lindex $rcv $i]
[lindex $trf $i] attach-agent [lindex $snd $i]
# Connect nodes
$ns duplex-link $rgw [lindex $srv $i] 1000000000000 .100 DropTail
# Connect agents
$ns connect [lindex $snd $i] [lindex $rcv $i]
}
foreach traffic $trf {
$ns at 0.0 "$traffic start"
}
$ns at 10.0 "finish"
$ns run
And here's an excerpt from a trace generated with this code:
+ 0.214008 0 1 pareto 40 ------- 0 0.0 2.0 0 0
- 0.214008 0 1 pareto 40 ------- 0 0.0 2.0 0 0
+ 0.321715 0 1 pareto 40 ------- 0 0.3 5.0 0 1
- 0.321715 0 1 pareto 40 ------- 0 0.3 5.0 0 1
r 0.51401 0 1 pareto 40 ------- 0 0.0 2.0 0 0
+ 0.51401 1 2 pareto 40 ------- 0 0.0 2.0 0 0
- 0.51401 1 2 pareto 40 ------- 0 0.0 2.0 0 0
r 0.61401 1 2 pareto 40 ------- 0 0.0 2.0 0 0
+ 0.61401 2 1 ack 40 ------- 0 2.0 0.0 0 2
- 0.61401 2 1 ack 40 ------- 0 2.0 0.0 0 2
r 0.621717 0 1 pareto 40 ------- 0 0.3 5.0 0 1
+ 0.621717 1 5 pareto 40 ------- 0 0.3 5.0 0 1
- 0.621717 1 5 pareto 40 ------- 0 0.3 5.0 0 1
r 0.71401 2 1 ack 40 ------- 0 2.0 0.0 0 2
+ 0.71401 1 0 ack 40 ------- 0 2.0 0.0 0 2
- 0.71401 1 0 ack 40 ------- 0 2.0 0.0 0 2
r 0.721717 1 5 pareto 40 ------- 0 0.3 5.0 0 1
+ 0.721717 5 1 ack 40 ------- 0 5.0 0.3 0 3
- 0.721717 5 1 ack 40 ------- 0 5.0 0.3 0 3
+ 0.735936 0 1 pareto 40 ------- 0 0.2 4.0 0 4
- 0.735936 0 1 pareto 40 ------- 0 0.2 4.0 0 4
r 0.821717 5 1 ack 40 ------- 0 5.0 0.3 0 3
+ 0.821717 1 0 ack 40 ------- 0 5.0 0.3 0 3
- 0.821717 1 0 ack 40 ------- 0 5.0 0.3 0 3
+ 0.831634 0 1 pareto 40 ------- 0 0.4 6.0 0 5
- 0.831634 0 1 pareto 40 ------- 0 0.4 6.0 0 5
r 1.014013 1 0 ack 40 ------- 0 2.0 0.0 0 2
+ 1.014013 0 1 pareto 1040 ------- 0 0.0 2.0 0 6
- 1.014013 0 1 pareto 1040 ------- 0 0.0 2.0 0 6
+ 1.014013 0 1 pareto 1040 ------- 0 0.0 2.0 0 7
- 1.014068 0 1 pareto 1040 ------- 0 0.0 2.0 0 7
r 1.035938 0 1 pareto 40 ------- 0 0.2 4.0 0 4
+ 1.035938 1 4 pareto 40 ------- 0 0.2 4.0 0 4
- 1.035938 1 4 pareto 40 ------- 0 0.2 4.0 0 4
r 1.121719 1 0 ack 40 ------- 0 5.0 0.3 0 3
+ 1.121719 0 1 pareto 1040 ------- 0 0.3 5.0 0 8
- 1.121719 0 1 pareto 1040 ------- 0 0.3 5.0 0 8
+ 1.121719 0 1 pareto 1040 ------- 0 0.3 5.0 0 9
- 1.121775 0 1 pareto 1040 ------- 0 0.3 5.0 0 9
r 1.131636 0 1 pareto 40 ------- 0 0.4 6.0 0 5
Looks to be working fine.

Try the below code it's work fine
set ns [new Simulator]
# Set up trace
set trc [open out.trc w]
set namtrc [open out.nam w]
$ns trace-all $trc
$ns namtrace-all $namtrc
proc finish {} {
global ns trc
global ns namtrc
$ns flush-trace
close $trc
close $namtrc
exec nam out.nam &
exit 0
}
# Simulation parameters
set n 5
set bw 150000000; # 150 Mb/s
# Set up bottleneck link
set gw [$ns node]
set rgw [$ns node]
$ns duplex-link $gw $rgw $bw .3000 DropTail
set snd {}
# TCP senders sitting on the gateway node
set trf {}
# Traffic generators for the senders
set dst {}
# Destination nodes hosting the receivers
set rcv {}
# TCP receivers sitting on the destination
for {set i 0} {$i<$n} {incr i} {
# Create the objects needed for a new connection
lappend snd [new Agent/TCP]
lappend rcv [new Agent/TCPSink]
lappend trf [new Application/Traffic/Pareto]
lappend srv [$ns node]
# set up source and destination nodes
$ns attach-agent $gw [lindex $snd $i]
$ns attach-agent [lindex $srv $i] [lindex $rcv $i]
[lindex $trf $i] attach-agent [lindex $snd $i]
# Connect nodes
$ns duplex-link $rgw [lindex $srv $i] 10000 .10000 DropTail
# Connect agents
$ns connect [lindex $snd $i] [lindex $rcv $i]
}
foreach traffic $trf {
$ns at 0.0 "$traffic start"
}
$ns at 10.0 "finish"
$ns run

Related

Unexpected exit during running a code in tcl

I am working on a structural engineering application using a software called "opensees V3.0.3" that is developed based on tcl (TCL8.6.9). When I run the code it stops in the middle and gives me an exit code (Exit code -1073740940) without certain errors. However, when i try running it on other devices, it works well without any problems. What could be causing this problem to happen only when I run the code on my PC. And I have windows 10.
this is the code of the model:
wipe ;
model BasicBuilder -ndm 2 -ndf 3;
# Define Parameters
set L 5.0 ; # beam length (m)
set H 3.5 ; # column height (m)
set Ly 4.0 ; # perpendicular length (m)
set Lb [expr sqrt(pow($L/2,2)+pow($H,2))] ; # brace length (m)
# Loads (KN/m2)
set DL [expr 4.50] ; # (KN/m2)
set LL [expr 2.0] ; # (KN/m2)
set g 9.81 ; # (m/s2)
set pi 3.14 ;
set RSteel 7.85 ; # mass per unit volume of steel (t/m3)
set FileName "GVOutPut";
file mkdir $FileName;
# Define Nodes
# Base Nodes
# tag X Y
node 1 0.0 0.0 ;
node 2 $L 0.0 ;
# First Floor Nodes
node 101 0.0 $H ;
node 102 $L $H ;
# Second Floor Nodes
node 201 0.0 [expr 2*$H] ;
node 202 $L [expr 2*$H] ;
# Third Floor Nodes
node 301 0.0 [expr 3*$H] ;
node 302 $L [expr 3*$H] ;
#Damper First Floor Nodes
node 105 [expr $L/2] $H ;
node 1055 [expr $L/2] $H ;
# #Damper Second Floor Nodes
node 205 [expr $L/2] [expr 2*$H] ;
node 2055 [expr $L/2] [expr 2*$H] ;
# #Damper Third Floor Nodes
node 305 [expr $L/2] [expr 3*$H] ;
node 3055 [expr $L/2] [expr 3*$H] ;
# # Define Constraint
# SPConstraint
# SPC tag Dx Dy Rz
fix 1 1 1 1 ;
fix 2 1 1 1 ;
# MP constraints
# $rNodeTag $cNodeTag $dof1 $dof2 $dof3.
equalDOF 105 1055 2 6 ;
equalDOF 205 2055 2 6 ;
equalDOF 305 3055 2 6 ;
# # Define passive slip-loads
set N11 42; # (KN)
set N12 126; # (KN)
set N21 32; # (KN)
set N22 96; # (KN)
set N31 18; # (KN)
set N32 54; # (KN)
set Fs1 [expr $N11];
puts "Fs1=$Fs1";
set Fs2 [expr $N21];
puts "Fs2=$Fs2";
set Fs3 [expr $N31];
puts "Fs3=$Fs3";
set dy 1.0e-5;
#
# Define Materials
# $matTag $E $dy
uniaxialMaterial ElasticPP 11 [expr $Fs1/$dy] $dy;
uniaxialMaterial ElasticPP 22 [expr $Fs2/$dy] $dy;
uniaxialMaterial ElasticPP 33 [expr $Fs3/$dy] $dy;
# Tag Fy E b $R0 $cR1 $cR2
uniaxialMaterial Steel02 2 275.e3 2.0e8 0.01 18 0.925 0.15 ; # Material of beams & columns
# Tag $E
uniaxialMaterial Elastic 3 2.0e8 ; # Material of braces
# Define Sections
# # IPE Sections
# IPE 300
set h1 0.3 ;
set b1 0.15 ;
set s1 0.0071 ;
set t1 0.0107 ;
set MatTag 2 ;
set dh11 [expr ($h1/2.0)-$t1] ;
set dh21 [expr ($h1/2.0)] ;
set db1 [expr ($b1/2.0)] ;
set ds1 [expr ($s1/2.0)] ;
section Fiber 300 { ;
# Mat Nij Njk yi zi yj zj yk zk yl zl
patch quad $MatTag 3 34 $dh11 -$db1 $dh21 -$db1 $dh21 $db1 $dh11 $db1
patch quad $MatTag 38 3 -$dh11 -$ds1 $dh11 -$ds1 $dh11 $ds1 -$dh11 $ds1
patch quad $MatTag 3 34 -$dh21 -$db1 -$dh11 -$db1 -$dh11 $db1 -$dh21 $db1
} ;
puts "Section I With Tag 300 Has Been Generated";
# IPE 270
set h2 0.27 ;
set b2 0.135 ;
set s2 0.0066 ;
set t2 0.0102 ;
set MatTag 2 ;
set dh12 [expr ($h2/2.0)-$t2] ;
set dh22 [expr ($h2/2.0)] ;
set db2 [expr ($b2/2.0)] ;
set ds2 [expr ($s2/2.0)] ;
section Fiber 270 { ;
# Mat NijNjk yi zi yj zj yk zk yl zl
patch quad $MatTag 3 34 $dh12 -$db2 $dh22 -$db2 $dh22 $db2 $dh12 $db2
patch quad $MatTag 38 3 -$dh12 -$ds2 $dh12 -$ds2 $dh12 $ds2 -$dh12 $ds2
patch quad $MatTag 3 34 -$dh22 -$db2 -$dh12 -$db2 -$dh12 $db2 -$dh22 $db2
} ;
puts "Section I With Tag 270 Has Been Generated";
# # Box Sections
# Box 25X2
set MatT 2 ;
set B2 0.25 ;
set tt2 0.02 ;
set dB12 [expr ($B2/2.0)-$tt2] ;
set dB22 [expr ($B2/2.0)] ;
section Fiber 2520 { ;
# MatTag Nij Njk yi zi yj zj yk zk yl zl
patch quad $MatT 3 30 $dB12 -$dB12 $dB22 -$dB12 $dB22 $dB12 $dB12 $dB12 ;
patch quad $MatT 3 30 -$dB22 -$dB12 -$dB12 -$dB12 -$dB12 $dB12 -$dB22 $dB12 ;
patch quad $MatT 34 3 -$dB22 -$dB22 $dB22 -$dB22 $dB22 -$dB12 -$dB22 -$dB12 ;
patch quad $MatT 34 3 -$dB22 $dB12 $dB22 $dB12 $dB22 $dB22 -$dB22 $dB22 ;
} ;
puts "Section Box With Tag 2520 Has Been Generated";
# Box 20X15
set MatT 2 ;
set B3 0.20 ;
set tt3 0.015 ;
set dB13 [expr ($B3/2.0)-$tt3] ;
set dB23 [expr ($B3/2.0)] ;
section Fiber 2015 { ;
# MatTag Nij Njk yi zi yj zj yk zk yl zl
patch quad $MatT 3 30 $dB13 -$dB13 $dB23 -$dB13 $dB23 $dB13 $dB13 $dB13
patch quad $MatT 3 30 -$dB23 -$dB13 -$dB13 -$dB13 -$dB13 $dB13 -$dB23 $dB13
patch quad $MatT 34 3 -$dB23 -$dB23 $dB23 -$dB23 $dB23 -$dB13 -$dB23 -$dB13
patch quad $MatT 34 3 -$dB23 $dB13 $dB23 $dB13 $dB23 $dB23 -$dB23 $dB23
} ;
puts "Section Box With Tag 2015 Has Been Generated";
# Define the fiber Circular Hollow fiber section
# Hot-Finished Circular Hollow Sections (D=88.9,t=10)
set MatID 3; # material ID tag --
set DSec 0.1683; # Hollow Section Diameter
set t 0.0125; # Hollow Section Thickness
set ro [expr $DSec/2]; # overall (outer) radius of the section
set ri [expr $ro-$t] ; # inner radius of the section, only for hollow sections
set nfCoreR 8; # number of radial divisions in the core (number of "rings")
set nfCoreT 8; # number of theta divisions in the core (number of "wedges")
section fiberSec 8810 {
# $matTag $numSubdivCirc $numSubdivRad $yCenter $zCenter $intRad $extRad $startAng $endAng
patch circ $MatID $nfCoreT $nfCoreR 0 0 $ri $ro 0 360; # Define the Circular Hollow section patch
};
puts "End of Define Sections";
# Define Transformation
#Columns
geomTransf PDelta 1;
#Beams
geomTransf Linear 2;
# braces
geomTransf Corotational 3;
# Define Elements
#Columns
set Acol1 0.0184; # Area of Box With Tag 2520
set Acol2 0.0141; # Area of Box With Tag 2015
set massCol1 [expr (($Acol1*$RSteel)*$H)]; # Mass of Box With Tag 2520 (t)
set massCol2 [expr (($Acol2*$RSteel)*$H)]; # Mass of Box With Tag 2015 (t)
#First Floor
# Tag iNode jNode nP SecTag TransTag
element nonlinearBeamColumn 11 1 101 5 2520 1 ;
element nonlinearBeamColumn 12 2 102 5 2520 1 ;
# $eleTag $iNode $jNode $A $E $Iz $transfTag
# element elasticBeamColumn 11 1 101 0.0184 2.0e8 1.635e-4 2 ;
# element elasticBeamColumn 12 2 102 0.0184 2.0e8 1.635e-4 2 ;
#Second Floor
# Tag iNode jNode nP SecTag TransTag
element nonlinearBeamColumn 21 101 201 5 2520 1 ;
element nonlinearBeamColumn 22 102 202 5 2520 1 ;
# $eleTag $iNode $jNode $A $E $Iz $transfTag
# element elasticBeamColumn 21 101 201 0.0184 2.0e8 1.635e-4 2 ;
# element elasticBeamColumn 22 102 202 0.0184 2.0e8 1.635e-4 2 ;
#Third Floor
# Tag iNode jNode nP SecTag TransTag
element nonlinearBeamColumn 31 201 301 5 2015 1 ;
element nonlinearBeamColumn 32 202 302 5 2015 1 ;
# $eleTag $iNode $jNode $A $E $Iz $transfTag
# element elasticBeamColumn 31 201 301 0.0141 2.0e8 6.373e-5 2 ;
# element elasticBeamColumn 32 202 302 0.0141 2.0e8 6.373e-5 2 ;
# Beams
set Asec300 0.00538; # Area of I section With Tag 300
set Asec270 0.00459; # Area of I section With Tag 270
set massBeam1 [expr (((($DL+(0.2*$LL))*$Ly)/$g+($Asec300*$RSteel))*$L)]; # Mass of I section With Tag 300 (t)
set massBeam2 [expr (((($DL+(0.2*$LL))*$Ly)/$g+($Asec300*$RSteel))*$L)]; # Mass of I section With Tag 300 (t)
set massBeam3 [expr (((($DL+(0.2*$LL))*$Ly)/$g+($Asec270*$RSteel))*$L)]; # Mass of I section With Tag 300 (t)
#First Floor
# Tag iNode jNode nP SecT TransTag
element nonlinearBeamColumn 1010 101 105 5 300 2 ;
element nonlinearBeamColumn 1020 105 102 5 300 2 ;
#Second Floor
# Tag iNode jNode nP SecT TransTag
element nonlinearBeamColumn 2010 201 205 5 300 2 ;
element nonlinearBeamColumn 2020 205 202 5 300 2 ;
#Third Floor
# Tag iNode jNode nP SecT TransTag
element nonlinearBeamColumn 3010 301 305 5 270 2 ;
element nonlinearBeamColumn 3020 305 302 5 270 2 ;
# Damper Elements (Braces)
set E 2.0e8;
set Abr 61.2e-4; # Area of Circular Hollow Section With Tag 8810
set massbr [expr ($Abr*$RSteel)*$Lb];
#First Floor
# $eleTag $iNode $jNode $secTag
element corotTrussSection 120 1 1055 8810; # Circular hollow section
element corotTrussSection 121 2 1055 8810; # Circular hollow section
# Friction Node
# $eleTag $iNode $jNode -mat $matTag -dir $dir
element zeroLength 122 105 1055 -mat 11 -dir 1;
#Second Floor
# $eleTag $iNode $jNode $secTag
element corotTrussSection 220 101 2055 8810; # Circular hollow section
element corotTrussSection 221 102 2055 8810; # Circular hollow section
# Friction Node
# $eleTag $iNode $jNode -mat $matTag -dir $dir
element zeroLength 222 205 2055 -mat 22 -dir 1;
#Third Floor
# $eleTag $iNode $jNode $secTag
element corotTrussSection 320 201 3055 8810; # Circular hollow section
element corotTrussSection 321 202 3055 8810; # Circular hollow section
# Friction Node
# $eleTag $iNode $jNode -mat $matTag -dir $dir
element zeroLength 322 305 3055 -mat 33 -dir 1;
# Nodal Mass
# Assign masses to the nodes that the columns are connected to each connection takes the mass of 1/2 of each element framing into it (mass=weight/$g)
# Mass tag mx my mIz
mass 101 [expr 5*($massCol1 + $massBeam1/2 + $massbr)] [expr 5*($massCol1 + $massBeam1/2 + $massbr)] 0.0 ; # level 1
mass 201 [expr 5*($massCol1/2 + $massCol2/2 + $massBeam2/2 + $massbr)] [expr 5*($massCol1/2 + $massCol2/2 + $massBeam2/2 + $massbr)] 0.0 ; # level 2
mass 202 [expr 5*($massCol1/2 + $massCol2/2 + $massBeam2/2 + $massbr)] [expr 5*($massCol1/2 + $massCol2/2 + $massBeam2/2 + $massbr)] 0.0 ; # level 2
mass 301 [expr 5*($massCol2/2 + $massBeam3/2)] [expr 5*($massCol2/2 + $massBeam3/2)] 0.0 ; # level 3
mass 302 [expr 5*($massCol2/2 + $massBeam3/2)] [expr 5*($massCol2/2 + $massBeam3/2)] 0.0 ; # level 3
puts "End of Define Model Elements" ;
# # Eigenvalue Analysis
set pi [expr 2.0*asin(1.0)]; # Definition of pi
set nEigenI 1; # mode i = 1
set nEigenJ 2; # mode j = 2
set lambdaN [eigen [expr $nEigenJ]]; # eigenvalue analysis for nEigenJ modes
set lambdaI [lindex $lambdaN [expr 0]]; # eigenvalue mode i = 1
set lambdaJ [lindex $lambdaN [expr $nEigenJ-1]]; # eigenvalue mode j = 2
set w1 [expr pow($lambdaI,0.5)]; # w1 (1st mode circular frequency)
set w2 [expr pow($lambdaJ,0.5)]; # w2 (2nd mode circular frequency)
set T1 [expr 2.0*$pi/$w1]; # 1st mode period of the structure
set T2 [expr 2.0*$pi/$w2]; # 2nd mode period of the structure
puts "W1=$w1 Rad/Sec";
puts "T1=$T1 Sec";
puts "************";
puts "W2=$w2 Rad/Sec";
puts "T2=$T2 Sec";
puts "************";
puts "End of Define Model Geometry" ;
recorder Node -file GVOutPut/BaseNodeReaction.out -time -node 1 2 -dof 2 reaction ;
# define gravity load applied to beams and columns -- eleLoad applies loads in local coordinate axis
# beams
set WzFloor1 [expr ((($DL+$LL)*$Ly)+($Asec300*$RSteel*$g))] ;
set WzFloor2 [expr ((($DL+$LL)*$Ly)+($Asec300*$RSteel*$g))] ;
set WzFloor3 [expr ((($DL+$LL)*$Ly)+($Asec270*$RSteel*$g))] ;
# columns
set WColF1 [expr ($Acol1*$RSteel*$g)] ;
set WColF2 [expr ($Acol1*$RSteel*$g)] ;
set WColF3 [expr ($Acol2*$RSteel*$g)] ;
# braces
set Wbr [expr ($massbr*$g)] ;
#
pattern Plain 1 Linear {
eleLoad -ele 1010 1020 -type -beamUniform -$WzFloor1; # beams level 1 (in -y direction)
eleLoad -ele 2010 2020 -type -beamUniform -$WzFloor2; # beams level 2 (in -y direction)
eleLoad -ele 3010 3020 -type -beamUniform -$WzFloor3; # beams level 3 (in -y direction)
eleLoad -ele 11 12 -type -beamUniform 0.0 -$WColF1; # Box section weight per length Level 1 (in -x direction)
eleLoad -ele 21 22 -type -beamUniform 0.0 -$WColF2; # Box section weight per length Level 2 (in -x direction)
eleLoad -ele 31 32 -type -beamUniform 0.0 -$WColF3; # Box section weight per length Level 3 (in -x direction)
} ;
# Static Analysis
constraints Transformation; # how it handles boundary conditions
numberer RCM; # renumber dof's to minimize band-width (optimization), if you want to
system BandGeneral; # how to store and solve the system of equations in the analysis (large model: try UmfPack)
test EnergyIncr 1.0e-5 50; #
algorithm Newton; #
set NstepGravity 10;
set DGravity [expr 1./$NstepGravity];
integrator LoadControl $DGravity; #
analyze $NstepGravity; # apply gravity
# maintain constant gravity loads and reset time to zero
loadConst -time 0.0;
puts "End of Static Analysis" ;

Finding "lsort" indices in .tcl

How can we obtain indices of lsort?
For example:
lsort -real {1 -4 6 0}
how can I obtain indices for the code above as idx = (1, 3, 0, 2)?
The -indices option to lsort does exactly what you want:
set values {1 -4 6 0}
set indices [lsort -indices -real $values]
foreach idx $indices {
puts "[lindex $values $idx] is at $idx"
}
Output:
-4 is at 1
0 is at 3
1 is at 0
6 is at 2

writing proc avg in tcl script in ns2_new

I wrote a scenario in ns2 software that has 5 fixed nodes and 1 mobile sink.
The mobile sink should collect the nodes' information.
I want to calculate the mean avg (energy_level ,number of nodes & traffic's data on nodes) and store it in the mobile sink.
The my code that I wrote does not any error but it does not print out.
My code & trace file are in attachment
I have to calculate this avg with proc in tcl script code not with awk code.Please help me.
proc create_common_app {destination_id disseminating_type disseminating_interval energy_level } {
global val
#puts "executing create_common_app\n"
if { $val(commonApp) == "Application/SensorBaseApp/CommonNodeApp" }
{
set app_ [new $val(commonApp)]
$app_ set destination_id_ $destination_id
}
$app_ set disseminating_type_ $disseminating_type
$app_ set disseminating_interval_ $disseminating_interval
$app_ set energy_level _ $energy_level
return $app_
}
proc create_mobile_sink_app { source _id disseminating_type disseminating_interval energy_level outside_network} {
global val
set app_ [new $val(sinkApp)]
$app_ set source _id _ $source _id
$app_ set disseminating_type_ $disseminating_type
$app_ set disseminating_interval_ $disseminating_interval
$app_ set energy_level _ $energy_level
$app_ set outside_network_ $outside_network
#$app_ set avg_ $avg
return $app_
}
#########################################################################
#Me
####################################################################
proc avg { energy_level nn disseminating_interval } {
set avgen 0
for {set i 0} {$i < 5} {incr i} {
set $avgen [expr $energy_level(i) + $avgen ]
}
set $avgen [expr $avgen / $nn]
return $avgen
set xl [list $avgen $nn $disseminating_interval]
}
proc ArithmeticMean { $xl } {
set length [llength $xl]
if {$length == 0} {
return 0.0
}
set sum [::tcl::mathop::+ {*}$xl]
return [expr {double($sum) / $length}]
set avgt [expr {double($sum) / $length}]
puts "average is $avgt_"
}
#####################################################################
tracefile
M 0.00000 0 (9.00, 5.00, 0.00), (0.00, 0.00), 0.00
s 5.000000000 _1_ AGT --- 0 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [0] 0 0
r 5.000000000 _1_ RTR --- 0 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [0] 0 0
s 5.000000000 _1_ AGT --- 1 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [1] 0 0
r 5.000000000 _1_ RTR --- 1 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [1] 0 0
s 5.000000000 _1_ AGT --- 2 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [2] 0 0
r 5.000000000 _1_ RTR --- 2 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [2] 0 0
s 5.000000000 _1_ AGT --- 3 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [3] 0 0
r 5.000000000 _1_ RTR --- 3 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [3] 0 0
s 5.000000000 _1_ AGT --- 4 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [4] 0 0
r 5.000000000 _1_ RTR --- 4 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [4] 0 0
s 5.000000000 _1_ AGT --- 5 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [5] 0 0
r 5.000000000 _1_ RTR --- 5 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [5] 0 0
s 5.000000000 _1_ AGT --- 6 cbr 100 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [6] 0 0
r 5.000000000 _1_ RTR --- 6 cbr 100 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [6] 0 0
s 5.000000000 _1_ RTR --- 0 AODV 48 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:255 -1:255 30 0] [0x2 1 1 [2 0] [1 4]] (REQUEST)
s 5.000535000 _1_ MAC --- 0 AODV 106 [0 ffffffff 2 800] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:255 -1:255 30 0] [0x2 1 1 [2 0] [1 4]] (REQUEST)
N -t 5.000535 -n 0 -e 0.049980
N -t 5.000535 -n 4 -e 0.049980
N -t 5.000535 -n 2 -e 0.049980
N -t 5.000535 -n 3 -e 0.049980
r 5.001383005 _0_ MAC --- 0 AODV 48 [0 ffffffff 2 800] [energy 0.049980 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:255 -1:255 30 0] [0x2 1 1 [2 0] [1 4]] (REQUEST)
r 5.001383013 _4_ MAC --- 0 AODV 48 [0 ffffffff 2 800] [energy 0.049980 ei 0.000 es 0.000 et 0.000
.
.
.....
proc avg { energy_level nn disseminating_interval } {
set avgen 0
for {set i 0} {$i < 5} {incr i} {
set $avgen [expr $energy_level(i) + $avgen ]
}
set $avgen [expr $avgen / $nn]
return $avgen
set xl [list $avgen $nn $disseminating_interval]
}
Notes:
You can't pass arrays by value: you need to pass the array name and use upvar to connect the name to the actual array in the caller's context
Use braces around your expr expressions.
You need to dereference $i in the array expansion.
Don't use $ in the first argument to set
do you want to use $nn in the for loop, or the hard-coded value 5?
Corrected code
proc avg { energy_level_var nn disseminating_interval } {
upvar 1 $energy_level_var energy_level
if {$nn == 0} {
error "denominator cannot be zero"
}
set sum 0
for {set i 0} {$i < $nn} {incr i} {
set sum [expr {$energy_level($i) + $sum}]
}
return [expr {$sum / $nn}]
}
And call like this
array set energy_level {0 1.0 1 1.1 2 1.2 3 1.3 4 1.4 5 1.5}
puts "average is: [avg energy_level 6 "?"]" ;# average is: 1.25
Also, don't use $ in the declaration of the ArithmeticMean proc:
proc ArithmeticMean { xl } {

How can I improve performance on DRF with high CPU time

I have a REST api with DRF and start to see already a performance hit with 100 objects and 1 user requesting (me - testing).
When requesting the more complex query, I get these results for CPU, always 5 - 10s:
Resource Value
>User CPU time 5987.089 msec
System CPU time 463.929 msec
Total CPU time 6451.018 msec
Elapsed time 6800.938 msec
Context switches 9 voluntary, 773 involuntary
but the SQL query stays below 100 ms
The more simple queries show similar behaviour, with CPU times around 1s and query time around 20 ms
So far, what I have tried out:
I am doing select_related() and prefetch_related(), which did improve the query time but not CPU time
I am using Imagekit to generate pictures, on a S3 instance. I removed the whole specification to test and this had minor impact
I run a method field to fetch user-specific data. Removing this had only minor impact
I have checked logs files on the backend and nothing specific shows up here...
Backend is Nginx - supervisord - gunicorn - postgresql - django 1.8.1
Here are the serializer and view:
class ParticipationOrganizationSerializer(ModelSerializer):
organization = OrganizationSerializer(required=False, read_only=True, )
bookmark = SerializerMethodField(
required=False,
read_only=True,
)
location_map = LocationMapSerializer(
required=False,
read_only=True,
)
class Meta:
model = Participation
fields = (
'id',
'slug',
'organization',
'location_map',
'map_code',
'partner',
'looking_for',
'complex_profile',
'bookmark',
'confirmed',
)
read_only_fields = (
'id',
'slug',
'organization',
'location_map',
'map_code',
'partner',
'bookmark',
'confirmed',
)
def get_bookmark(self, obj):
request = self.context.get('request', None)
if request is not None:
if(request.user.is_authenticated()):
# print(obj.bookmarks.filter(author=request.user).count())
try:
bookmark = obj.bookmarks.get(author=request.user)
# bookmark = Bookmark.objects.get(
# author=request.user,
# participation=obj,
# )
return BookmarkSerializer(bookmark).data
except Bookmark.DoesNotExist:
# We have nothing yet
return None
except Bookmark.MultipleObjectsReturned:
# This should not happen, but in case it does, delete all
# the bookmarks for safety reasons.
Bookmark.objects.filter(
author=request.user,
participation=obj,
).delete()
return None
return None
class ParticipationOrganizationViewSet(ReadOnlyModelViewSet):
"""
A readonly ViewSet for viewing participations of a certain event.
"""
serializer_class = ParticipationOrganizationSerializer
queryset = Participation.objects.all().select_related(
'location_map',
'organization',
'organization__logo_image',
).prefetch_related(
'bookmarks',
)
lookup_field = 'slug'
def get_queryset(self):
event_slug = self.kwargs['event_slug']
# Filter for the current event
# Filter to show only the confirmed participations
participations = Participation.objects.filter(
event__slug=event_slug,
confirmed=True
).select_related(
'location_map',
'organization',
'organization__logo_image',
).prefetch_related(
'bookmarks',
)
# Filter on partners? This is a parameter passed on in the url
partners = self.request.query_params.get('partners', None)
if(partners == "true"):
participations = participations.filter(partner=True)
return participations
# http://stackoverflow.com/questions/22616973/django-rest-framework-use-different-serializers-in-the-same-modelviewset
def get_serializer_class(self):
if self.action == 'list':
return ParticipationOrganizationListSerializer
if self.action == 'retrieve':
return ParticipationOrganizationSerializer
return ParticipationOrganizationListSerializer
Any help is very much appreciated!
update
I dumped the data to my local machine and I am observing similar times. I guess this rules out the whole production setup (nginx, gunicorn)?
update 2
Here are the results of the profiler.
Also I made some progress in improving the speeds by
Simplifying my serializers
Doing the tests with curl and having Debug Toolbar off
ncalls tottime percall cumtime percall filename:lineno(function)
0 0 0 profile:0(profiler)
1 0 0 3.441 3.441 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/rest_framework/views.py:442(dispatch)
1 0 0 3.441 3.441 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/rest_framework/viewsets.py:69(view)
1 0 0 3.441 3.441 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/django/views/decorators/csrf.py:57(wrapped_view)
1 0 0 3.44 3.44 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/rest_framework/mixins.py:39(list)
1 0 0 3.438 3.438 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/rest_framework/serializers.py:605(to_representation)
1 0 0 3.438 3.438 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/rest_framework/serializers.py:225(data)
1 0 0 3.438 3.438 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/rest_framework/serializers.py:672(data)
344/114 0.015 0 3.318 0.029 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/rest_framework/serializers.py:454(to_representation)
805 0.01 0 2.936 0.004 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/rest_framework/fields.py:1368(to_representation)
2767 0.013 0 2.567 0.001 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py:166(send)
2070 0.002 0 2.52 0.001 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/imagekit/registry.py:52(existence_required_receiver)
2070 0.005 0 2.518 0.001 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/imagekit/registry.py:55(_receive)
2070 0.004 0 2.513 0.001 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/imagekit/utils.py:147(call_strategy_method)
2070 0.002 0 2.508 0.001 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/imagekit/cachefiles/strategies.py:14(on_existence_required)
2070 0.005 0 2.506 0.001 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/imagekit/cachefiles/__init__.py:86(generate)
2070 0.002 0 2.501 0.001 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/imagekit/cachefiles/backends.py:109(generate)
2070 0.003 0 2.499 0.001 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/imagekit/cachefiles/backends.py:94(generate_now)
2070 0.01 0 2.496 0.001 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/imagekit/cachefiles/backends.py:65(get_state)
690 0.001 0 2.292 0.003 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/imagekit/cachefiles/__init__.py:148(__nonzero__)
690 0.005 0 2.291 0.003 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/imagekit/cachefiles/__init__.py:124(__bool__)
2070 0.007 0 2.276 0.001 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/imagekit/cachefiles/backends.py:112(_exists)
2070 0.01 0 2.269 0.001 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/storages/backends/s3boto.py:409(exists)
4140 0.004 0 2.14 0.001 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/storages/backends/s3boto.py:282(entries)
1633 0.003 0 2.135 0.001 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/storages/backends/s3boto.py:288()
1633 0.001 0 2.129 0.001 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/boto/s3/bucketlistresultset.py:24(bucket_lister)
2 0 0 2.128 1.064 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/boto/s3/bucket.py:390(_get_all)
2 0 0 2.128 1.064 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/boto/s3/bucket.py:426(get_all_keys)
1331 0.003 0 1.288 0.001 /usr/lib/python2.7/ssl.py:335(recv)
1331 1.285 0.001 1.285 0.001 /usr/lib/python2.7/ssl.py:254(read)
2 0 0 0.983 0.491 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/boto/connection.py:886(_mexe)
2 0 0 0.983 0.491 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/boto/s3/connection.py:643(make_request)
2 0 0 0.983 0.491 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/boto/connection.py:1062(make_request)
2 0.004 0.002 0.896 0.448 /usr/lib/python2.7/httplib.py:585(_read_chunked)
2 0 0 0.896 0.448 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/boto/connection.py:393(read)
2 0 0 0.896 0.448 /usr/lib/python2.7/httplib.py:540(read)
166 0.002 0 0.777 0.005 /usr/lib/python2.7/httplib.py:643(_safe_read)
166 0.005 0 0.775 0.005 /usr/lib/python2.7/socket.py:336(read)
2 0 0 0.568 0.284 /usr/lib/python2.7/httplib.py:793(send)
2 0 0 0.568 0.284 /usr/lib/python2.7/httplib.py:998(_send_request)
2 0 0 0.568 0.284 /usr/lib/python2.7/httplib.py:820(_send_output)
2 0 0 0.568 0.284 /usr/lib/python2.7/httplib.py:977(request)
2 0 0 0.568 0.284 /usr/lib/python2.7/httplib.py:962(endheaders)
1 0 0 0.567 0.567 /usr/lib/python2.7/httplib.py:1174(connect)
1380 0.001 0 0.547 0 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/imagekit/cachefiles/__init__.py:82(url)
1380 0.007 0 0.546 0 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/imagekit/cachefiles/__init__.py:72(_storage_attr)
105 0.009 0 0.528 0.005 /usr/lib/python2.7/socket.py:406(readline)
2 0 0 0.413 0.207 /usr/lib/python2.7/httplib.py:408(begin)
2 0 0 0.413 0.207 /usr/lib/python2.7/httplib.py:1015(getresponse)
2 0 0 0.407 0.203 /usr/lib/python2.7/httplib.py:369(_read_status)
2750 0.003 0 0.337 0 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/rest_framework/fields.py:399(get_attribute)
1 0.223 0.223 0.335 0.335 /usr/lib/python2.7/socket.py:537(create_connection)
2865 0.012 0 0.334 0 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/rest_framework/fields.py:65(get_attribute)
1610 0.005 0 0.314 0 /home/my_app/.virtualenvs/my_app/src/django-s3-folder-storage/s3_folder_storage/s3.py:13(url)
1610 0.012 0 0.309 0 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/storages/backends/s3boto.py:457(url)
690 0.005 0 0.292 0 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/imagekit/models/fields/utils.py:10(__get__)
690 0.007 0 0.251 0 /home/my_app/.virtualenvs/my_app/local/lib/python2.7/site-packages/imagekit/cachefiles/__init__.py:20(__init__)
2 0 0 0.248 0.124
>>>> cutting here, low impact calls

How to find corresponding values in columns using TCL

I'm a regular matlab user who needs to do some processing in TCL. I have no experience with tcl so up to now what I have done is by searching on google.
Please pardon novice ways...
I have data in column A and B which is imported via file Data_1, I would like to get the corresponding values from column B for two numbers in column A e.g.
when A is 0.1 then B is 9 and when A is 0.3 then B is 21, store 9 & 21 in variable for later use
I'd like to open another file Data_2, which has two columns C and D.
I'd like to take all the numbers of column D that fall between Column 9 and 21 (positive numbers) in Column C and average it and put it in a variable for later use.
I've started with first trying to find corresponding values for 0.1 and 0.3, and that is where I am stuck.
I can (I think) find 0.1 and 0.3 but don't know how to get the corresponding values from column B
And then proceed ahead with the second part.
Please help.
Data_1 Data__2
Column A Column B Column C Column D
0 0 180 14.5
0.01 1.5 162 13.05
0.02 3 144 11.6
0.03 4.5 126 10.15
0.04 6 108 8.7
0.05 7.5 90 7.25
0.1 9 72 5.8
0.125 10.5 54 4.35
0.15 12 20 2.9
0.175 13.5 10 1.45
0.2 15 0 0
0.225 16.5 -10 -1.45
0.25 18 -20 -2.9
0.275 19.5 -54 -4.35
0.3 21 -72 -5.8
0.325 22.5 -90 -7.25
0.35 24 -108 -8.7
0.0.375 25.5 -126 -10.15
0.4 27 -144 -11.6
0.425 28.5 -162 -13.05
0.45 30 -180 -14.5
# Open files for reading
set input1 [open "Data_1.dat" r]
set input2 [open "Data_2.dat" r]
#read file
set file_data [read $input1]
#close file
close $input1
#split into lines
set data [split $file_data "\n"]
foreach line $data {
set val1 [lsearch -inline $line 0.1]
set val2 [lsearch -inline $line 0.3]
puts $val1
puts $val2
}
I'd write this:
set min_key 0.1
set max_key 0.3
set fid [open Data_1.dat r]
while {[gets $fid line] != -1} {
lassign $line a b
if {$a == $min_key} {
set min $b
}
if {$a == $max_key} {
set max $b
}
}
close $fid
set fid [open Data_2.dat r]
while {[gets $fid line] != -1} {
lassign $line c d
if {$min <= $d && $d <= $max} {
lappend values $c
}
}
close $fid
puts [join $values \n]
This outputs
180
162
144
126
Notes:
use a while loop to iterate over the lines of a file.
lassign assigns values of a list to variables
I didn't quite follow all of the if/else logic in your post, but there are several ways you can process the files. From your example, you can store the A and B columns as two separate lists.
set acol [list]
set bcol [list]
set data [split $file_data "\n"]
foreach line $data {
lappend acol [lindex $line 0]
lappend bcol [lindex $line 1]
}
# Find the first column A value that is 0.1.
set index [lsearch -real $acol 0.1]
# Get the corresponding B value.
puts "b value: [lindex $bcol $index]"
You can probably also just simplify the foreach-loop to this, without splitting the file data into lines.
foreach {val1 val2} $file_data {
lappend acol $val1
lappend bcol $val2
}