Error in foreach loop

Forum for OpenSees users to post questions, comments, etc. on the use of the OpenSees interpreter, OpenSees.exe

Moderators: silvia, selimgunay, Moderators

Post Reply
afshina
Posts: 15
Joined: Sat Aug 23, 2014 4:55 am

Error in foreach loop

Post by afshina »

Hello Dear Silvia
I used below code for IDA analysis with changing 7 Earthquake Records Using foreach loop.
But I had this ERROR:

[ Wrong # args: should be “foreach varlist list ? varlist list …? command”
While executing….]

Could you please explain about this ERROR?
Thanks a lot for your kindness.

::::::::::::::::::::: This is My Code , I just writed important parts, here ::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
wipe ; # clear memory of past model definitions

foreach {Name dt dtana PGA T} {chuetsuoki0909g 0.01 0.001 0.909 69.99}
{

for {set i 2} {$i<=30} {incr i 2}
{

set fileName "IDA/$Name/[expr $i./20]pga "

file mkdir $fileName


recorder Drift-file $fileName/h3driftstory1.txt -time -iNode 11 -jNode 12 -dof 1 -perpDirn 2
recorder Drift-file $fileName/h3driftstory2.txt -time -iNode 12 -jNode 13 -dof 1 -perpDirn 2
recorder Drift-file $fileName/h3driftstory3.txt -time -iNode 13 -jNode 14 -dof 1 -perpDirn 2
recorder Drift-file $fileName/h3driftstory4.txt -time -iNode 14 -jNode 15 -dof 1 -perpDirn 2
recorder Drift-file $fileName/h3driftstory5.txt -time -iNode 15 -jNode 16 -dof 1 -perpDirn 2
model BasicBuilder -ndm 2 -ndf 3; # Define the model builder, ndm = #dimension, ndf = #dofs
source DisplayModel2D.tcl; # procedure for displaying a 2D perspective of model
source DisplayPlane.tcl; # procedure for displaying a plane in a model

.
.
.
.
.
.
.
.
.
.
.

# DYNAMIC ground-motion analysis -------------------------------------------------------------
# create load pattern

set acc "series -dt $dt -filePath $Name.txt -factor [expr $i. *(1/PGA)*386/20]"
pattern UniformExcitation 2 1 -accel $ acc; # define where and how (pattern tag, dof) acceleration is applied

# display displacement shape of the column
recorder display "Displaced shape" 10 10 500 500 -wipe
prp 200. 50. 1;
vup 0 1 0;
vpn 0 0 1;
display 1 5 40

# create the analysis
wipeAnalysis; # clear previously-define analysis parameters
puts "groundmotion start!.Time: [getTime]"
constraints Plain; # how it handles boundary conditions
numberer Plain; # 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
algorithm Linear # use Linear algorithm for linear analysis
integrator Newmark 0.5 0.25 ; # determine the next time step for an analysis
analysis Transient; # define type of analysis: time-dependent
analyze [expr $T./$dtana] $dtana; # apply (npts/2 ) , (2*dt)

puts "groundmotion [expr $i./20]pga done!.End Time: [getTime]"

}
}
Jeena
Posts: 50
Joined: Tue Mar 19, 2013 12:40 pm
Location: Virginia Tech

Re: Error in foreach loop

Post by Jeena »

Hi,

There are a number of issues in your code. Modify the first part of the program as follows

foreach {Name dt dtana PGA T} {chuetsuoki0909g 0.01 0.001 0.909 69.99} {

for {set i 2} {$i<=30} {incr i 2} {

set fileName [join {IDA/ ${Name} / [expr ${i}/20] pga} ""]

file mkdir $fileName

recorder Drift -file $fileName/h3driftstory1.txt -time -iNode 11 -jNode 12 -dof 1 -perpDirn 2
recorder Drift -file $fileName/h3driftstory2.txt -time -iNode 12 -jNode 13 -dof 1 -perpDirn 2
recorder Drift -file $fileName/h3driftstory3.txt -time -iNode 13 -jNode 14 -dof 1 -perpDirn 2
recorder Drift -file $fileName/h3driftstory4.txt -time -iNode 14 -jNode 15 -dof 1 -perpDirn 2
recorder Drift -file $fileName/h3driftstory5.txt -time -iNode 15 -jNode 16 -dof 1 -perpDirn 2
model BasicBuilder -ndm 2 -ndf 3; # Define the model builder, ndm = #dimension, ndf = #dofs
source DisplayModel2D.tcl; # procedure for displaying a 2D perspective of model
source DisplayPlane.tcl; # procedure for displaying a plane in a model


# DYNAMIC ground-motion analysis -------------------------------------------------------------
# create load pattern



Many of the variables in the dynamic analysis part are missing ...

Thanks,
Jeena
afshina
Posts: 15
Joined: Sat Aug 23, 2014 4:55 am

Re: Error in foreach loop

Post by afshina »

thank you dear
hoangbo
Posts: 2
Joined: Mon Jan 25, 2016 8:44 pm
Location: Ho Chi Minh City, Vietnam
Contact:

Re: Error in foreach loop

Post by hoangbo »

Thanks you for that useful information
fmk
Site Admin
Posts: 5884
Joined: Fri Jun 11, 2004 2:33 pm
Location: UC Berkeley
Contact:

Re: Error in foreach loop

Post by fmk »

the opening brace MUST BE ON THE SAME LINE, i.e

foreach {Name dt dtana PGA T} {chuetsuoki0909g 0.01 0.001 0.909 69.99} {

NOT

foreach {Name dt dtana PGA T} {chuetsuoki0909g 0.01 0.001 0.909 69.99}
{

the for loop is also going to fail, tcl parses one line at a time (not smart enough to look at next line for body unless the { is present
afshina
Posts: 15
Joined: Sat Aug 23, 2014 4:55 am

Re: Error in foreach loop

Post by afshina »

Thank you Dear, Thanks a lot.
Post Reply