Unexpected exit during running a code in tcl - 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" ;

Related

FATAL ERROR: can't use non-numeric floating-point value as operand of "-"

I am performing SMD simulation using NAMD with TCL. However I get FATAL ERROR: can't use non-numeric floating-point value as operand of "-"; why?
I am doing steered molecular dynamics and the code below is to introduce a virtual spring to produce an external force.I am constraining multiple atom positions for the spring to move towards the position.
The constant velocity method can be used to pull multiple atoms.
Details of the code is below;
# Atoms selected for force application
set id1 [atomid AP1 10 CA]
set grp1 {}
lappend grp1 $id1
set a1 [addgroup $grp1]
set id2 [atomid AP1 14 CA]
set grp2 {}
lappend grp2 $id2
set a2 [addgroup $grp2]
set id3 [atomid AP1 19 CA]
set grp3 {}
lappend grp3 $id3
set a3 [addgroup $grp3]
set id4 [atomid AP1 22 CA]
set grp4 {}
lappend grp2 $id4
set a4 [addgroup $grp4]
set id5 [atomid AP1 25 CA]
set grp5 {}
lappend grp5 $id5
set a5 [addgroup $grp5]
set id6 [atomid AP1 28 CA]
set grp6 {}
lappend grp6 $id6
set a6 [addgroup $grp6]
set id7 [atomid AP1 32 CA]
set grp7 {}
lappend grp7 $id7
set a7 [addgroup $grp7]
set id8 [atomid AP1 36 CA]
set grp8 {}
lappend grp8 $id8
set a8 [addgroup $grp8]
set id9 [atomid AP1 42 CA]
set grp9 {}
lappend grp9 $id9
set a9 [addgroup $grp9]
# set the output frequency, initialize the time counter
set Tclfreq 50
set t 0
# contraint points
set c1x -19.450
set c1y -3.791
set c1z 12.790
set c2x -7.811
set c2y -8.997
set c2z 11.372
set c3x 9.267
set c3y -12.502
set c3z 13.258
set c4x 16.102
set c4y -20.086
set c4z 11.251
set c5x 24.674
set c5y -13.698
set c5z 9.676
set c6x 22.147
set c6y -3.551
set c6z 13.986
set c7x 8.271
set c7y -2.899
set c7z 18.747
set c8x 8.827
set c8y 8.698
set c8z 15.017
set c9x 22.774
set c9y 1.488
set c9z 16.176
# force constant (kcal/mol/A^2) #7.2
set k 1.0
# pulling velocity (A/timestep)
set v 0.002
set outfilename da_smd_tcl.out
open $outfilename w
proc calcforces {} {
global Tclfreq t k v a1 a2 a3 a4 a5 a6 a7 a8 a9 c1x c1y c1z c2x c2y c2z c3x c3y c3z c4x c4y c4z c5x c5y c5z c6x c6y c6z c7x c7y c7z c8x c8y c8z c9x c9y c9z outfilename
# get coordinates
loadcoords coordinate
set r1 $coordinate($a1)
set r1x [lindex $r1 0]
set r1y [lindex $r1 1]
set r1z [lindex $r1 2]
set r2 $coordinate($a2)
set r2x [lindex $r2 0]
set r2y [lindex $r2 1]
set r2z [lindex $r2 2]
set r3 $coordinate($a3)
set r3x [lindex $r3 0]
set r3y [lindex $r3 1]
set r3z [lindex $r3 2]
set r4 $coordinate($a4)
set r4x [lindex $r4 0]
set r4y [lindex $r4 1]
set r4z [lindex $r4 2]
set r5 $coordinate($a5)
set r5x [lindex $r5 0]
set r5y [lindex $r5 1]
set r5z [lindex $r5 2]
set r6 $coordinate($a6)
set r6x [lindex $r6 0]
set r6y [lindex $r6 1]
set r6z [lindex $r6 2]
set r7 $coordinate($a7)
set r7x [lindex $r7 0]
set r7y [lindex $r7 1]
set r7z [lindex $r7 2]
set r8 $coordinate($a8)
set r8x [lindex $r8 0]
set r8y [lindex $r8 1]
set r8z [lindex $r8 2]
set r9 $coordinate($a9)
set r9x [lindex $r9 0]
set r9y [lindex $r9 1]
set r9z [lindex $r9 2]
# calculate forces
set f1x [expr $k*($c1x-$r1x)]
set f1y [expr $k*($c1y-$r1y)]
set f1z [expr $k*($c1z+$v*$t-$r1z)]
lappend f1 $f1x $f1y $f1z
set f2x [expr $k*($c2x-$r2x)]
set f2y [expr $k*($c2y-$r2y)]
set f2z [expr $k*($c2z+$v*$t-$r2z)]
lappend f2 $f2x $f2y $f2z
set f3x [expr $k*($c3x-$r3x)]
set f3y [expr $k*($c3y-$r3y)]
set f3z [expr $k*($c3z+$v*$t-$r3z)]
lappend f3 $f3x $f3y $f3z
set f4x [expr $k*($c4x-$r4x)]
set f4y [expr $k*($c4y-$r4y)]
set f4z [expr $k*($c4z+$v*$t-$r4z)]
lappend f4 $f4x $f4y $f4z
set f5x [expr $k*($c5x-$r5x)]
set f5y [expr $k*($c5y-$r5y)]
set f5z [expr $k*($c5z+$v*$t-$r5z)]
lappend f5 $f5x $f5y $f5z
set f6x [expr $k*($c6x-$r6x)]
set f6y [expr $k*($c6y-$r6y)]
set f6z [expr $k*($c6z+$v*$t-$r6z)]
lappend f6 $f6x $f6y $f6z
set f7x [expr $k*($c7x-$r7x)]
set f7y [expr $k*($c7y-$r7y)]
set f7z [expr $k*($c7z+$v*$t-$r7z)]
lappend f7 $f7x $f7y $f7z
set f8x [expr $k*($c8x-$r8x)]
set f8y [expr $k*($c8y-$r8y)]
set f8z [expr $k*($c8z+$v*$t-$r8z)]
lappend f8 $f8x $f8y $f8z
set f9x [expr $k*($c9x-$r9x)]
set f9y [expr $k*($c9y-$r9y)]
set f9z [expr $k*($c9z+$v*$t-$r9z)]
lappend f9 $f9x $f9y $f9z
# apply forces
addforce $a1 $f1
addforce $a2 $f2
addforce $a3 $f3
addforce $a4 $f4
addforce $a5 $f5
addforce $a6 $f6
addforce $a7 $f7
addforce $a8 $f8
addforce $a9 $f9
# output
set foo [expr $t % $Tclfreq]
if { $foo == 0 } {
set outfile [open $outfilename a]
set time [expr $t*2/1000.0]
puts $outfile "$time $r2z $f2z"
close $outfile
}
incr t
return
}
There's something unexpected (non-numeric) in one of your variables.
Which one is the tricky bit. You don't say which line has the error (the errorInfo global will contain this) and because you don't put your expressions in braces, even the errorInfo will be not 100% informative. (Putting expressions in braces allows Tcl to compile the expressions — making them faster and safer — and makes the error message on a bad value rather more informative as the runtime engine has more information available.)
Given what error message you're receiving, it's probably not an empty string.
As a side note, you can shorten sequences like this:
set r1 $coordinate($a1)
set r1x [lindex $r1 0]
set r1y [lindex $r1 1]
set r1z [lindex $r1 2]
to this:
lassign $coordinate($a1) r1x r1y r1z

Returning Some Variable from a "proc" in Tcl

Suppose that a procedure in Tcl is as follows:
proc Section {ID x y} {
.
.
"Some calculations do here"
.
.
}
Section 1 20 30
Section 2 25 35
Section 3 30 40
Section 4 35 45
Now, I define this:
set IDsection {1 3}
Then, I would like to read all values (arbitrary number, 2 or more) into a set (IDsection) which would show the ID in above procedure and generate the corresponding y:
set Load {30 40}
How I do produce the values in {} in front of "Load"?
You could do it like this:
proc Section {ID x y} {
set ::section_data($ID) [list $x $y]
}
proc getLoads {ids} {
global section_data
foreach id $ids {
lappend loads [lindex $section_data($id) end]
}
return $loads
}
Section 1 20 30
Section 2 25 35
Section 3 30 40
Section 4 35 45
set IDsection {1 4 1 3}
set Load [getLoads $IDsection] ;# => 30 45 30 40

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
}

Loop through all combinations of arrays (variable size)

In tcl I need to execute a script for each possible combination of values of an unknown number of variables.
Describing it in words:
A goes from a0 -> a1 with steps of "da"
B goes from b0 -> b1 with steps of "db"
C goes from c0 -> c1 with steps of "dc"
....
The number of variables can vary. Note: The names of the variables are not known beforehand, 'A' could also be called 'Ape' or anything else. Same goes for the other variables.
What I have so far is:
array set min_vals {A $a0 B $b0 C $c0 ...} ;# --> These are user-defined
array set max_vals {A $a1 B $b1 C $c1 ...} ;# --> These are user-defined
array set step_vals {A $da B $db C $dc ...} ;# --> These are user-defined
# First I determine the number of variables and the number of values they can have
set nr_vars [array size min_vals] ;# Determine nr of variables
set nr_vals [list] ;# --> Set empty list for nr of values for each variable
foreach var_name [array names min_vals] {
set nr [expr {round( ( $max_vals(${var_name})-$min_vals(${var_name}) ) / $step_vals(${var_names}) )}]
set nr_vals [concat $nr_vals $nr]
}
Now I need to somehow loop through each possible combination:
[A=a0, B=b0, C=c0]
[A=a0+da, B=b0, C=c0]
[A=a0+2*da, B=b0, C=c0]
...
...
[A=a1, B=b0, C=c0]
[A=a0, B=b0+db, C=c0]
[A=a0+da, B=b0+db, C=c0]
...
...
[A=a1, B=b1, C=c1]
I hope there is an easy way to do this. The only way I could think of doing this was by having a single loop with number of iterations containing all combinations and let each iteration-number correspond to a specific combination. But I'm sure there must be a less cumbersome way.
_
Edit:
Maybe I wasn't really clear about what I exactly wanted. I don't care about the actual output. My aim is to set each variable to the correct value and run another script with these variables:
set A $a0
set B $b0
set C $c0
source run/some/script.tcl
And repeat this for each possible combination of values of A, B and C.
Use nested for loops
for {set a $min_vals(A)} {$a <= $max_vals(A)} {incr a $step_vals(A)} {
for {set b $min_vals(B)} {$b <= $max_vals(B)} {incr b $step_vals(B)} {
for {set c $min_vals(C)} {$c <= $max_vals(C)} {incr c $step_vals(C)} {
do something with [list $a $b $c]
}
}
}
Ah, needs to be more dynamic. Hmmm,
set variables {A B C}
array set min_vals {A 1 B 10 C 100}
array set max_vals {A 3 B 30 C 300}
array set step_vals {A 1 B 10 C 100}
proc build_loops {} {
global variables
# create the "seed" code: what to do with the generated tuple
set code "do_something_with \[list "
foreach var $variables {
append code "\$[loop_var $var] "
}
append code "]"
# and wrap layers of for loops around the seed
foreach var [lreverse $variables] {
set loop_var [loop_var $var]
set code [format {for {set %s $min_vals(%s)} {$%s <= $max_vals(%s)} {incr %s $step_vals(%s)} {%s}} \
$loop_var $var \
$loop_var $var \
$loop_var $var \
$code \
]
}
return $code
}
proc loop_var {varname} {
return "loop_[string tolower $varname]"
}
proc do_something_with {args} {
puts $args
}
set code [build_loops]
puts $code
eval $code
for {set loop_a $min_vals(A)} {$loop_a <= $max_vals(A)} {incr loop_a $step_vals(A)} {for {set loop_b $min_vals(B)} {$loop_b <= $max_vals(B)} {incr loop_b $step_vals(B)} {for {set loop_c $min_vals(C)} {$loop_c <= $max_vals(C)} {incr loop_c $step_vals(C)} {do_something_with [list $loop_a $loop_b $loop_c ]}}}
{1 10 100}
{1 10 200}
{1 10 300}
{1 20 100}
{1 20 200}
{1 20 300}
{1 30 100}
{1 30 200}
{1 30 300}
{2 10 100}
{2 10 200}
{2 10 300}
{2 20 100}
{2 20 200}
{2 20 300}
{2 30 100}
{2 30 200}
{2 30 300}
{3 10 100}
{3 10 200}
{3 10 300}
{3 20 100}
{3 20 200}
{3 20 300}
{3 30 100}
{3 30 200}
{3 30 300}
I keep a separate list of the variable names: [array names a] returns an unordered list of names, and (I assume) it is important to know the order of the tuple given to the do_something_with proc

NS2 NAM not displaying transmissions

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