cannot read all of values from a txt file

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

Moderators: silvia, selimgunay, Moderators

Post Reply
dkwong
Posts: 6
Joined: Mon Jun 16, 2014 7:09 am
Location: University at Buffalo

cannot read all of values from a txt file

Post by dkwong »

I tried to let opensees read a txt file. The file contains 2341 different values. But the program stops reading the file in 2323 and said "can't read OP(2323), no such element in array"
However, when I separated my code and only ran the read txt part, it work and showed the size of the file is 2341.
I wonder what the problem is. Hope someone can share their opinions.
Here is part of the code.

set infln [open eleGlobal_temp.out "r"]
set OPR [gets $infln]
set xin 0
set c 1
foreach xin $OPR {
set OP($c) $xin
incr c 1
}

#puts "OP(1): $OP(1)"
close $infln

set OPSize [array size OP]
puts "OPSize: $OPSize"
nskok
Posts: 17
Joined: Wed May 15, 2013 12:09 pm
Location: Purdue University

Re: cannot read all of values from a txt file

Post by nskok »

I suggest trying to load all the data in at once and then do your operations. The read command will load all of the data so you will have to parse it yourself. One way to parse it is to use the split command. If each value is on a new line, this method should work.

-Nick

Read command: https://www.tcl.tk/man/tcl8.0/TclCmd/read.htm
Split command: http://www.tcl.tk/man/tcl8.4/TclCmd/split.htm

# Open and read the file
set fid [open eleGlobal_temp.out r]
set OPR [read $fid]
close $fid

# Split the data by line into a list
set OPR_split [split $OPR "\n"]

set xin 0
set c 1
foreach xin $OPR_split {
set OP($c) $xin
incr c 1
}
dkwong
Posts: 6
Joined: Mon Jun 16, 2014 7:09 am
Location: University at Buffalo

Re: cannot read all of values from a txt file

Post by dkwong »

Nick,
Thanks for your reply and help.
I tried to way you advised, but it still stuck in reading the whole file.
I think I didn't explain the whole thing clearly.
The code I put on top is inside a couple of loops, so I cannot separate it.
My objective of the code is to analyze a continuous beam sitting on a couple of springs. And there is moving load imposed on the beam. For every 2 inches the load will move forward, the load cases will be more than a hundred. Every one inch is set one element, so it will be 391 elements along a 390 length beam.
Because I want to get the maximum and minimum moment of every node in all load cases, the code I wrote will keep rewriting the file called eleGlobal_temp.out by comparing the value itself. eleGlobal_temp.out has been used to record the output of my analysis.
However, it stops for some results that I don't know.
BoxSpace.txt is the spacing of the springs (12 48 54 54 54 54 54 48 12)


# build the model
wipe All

puts "System"

model basic -ndm 2 -ndf 3

set infln [open BoxSpace.txt "r"]
set BSR [gets $infln]
set BL 0
set i 0



foreach BS $BSR {set BL [expr ($BL + $BS)]
incr i 1
set BSA($i) $BS
puts "BSA($i): $BSA($i)"

}
puts "Array output above"
puts [array exists BSA]
puts [array size BSA]
set NBoxes [expr ([array size BSA] - 1)]
puts "The Number of Boxes: $NBoxes"
close $infln
puts "The Length of the Beam: $BL"

set NSpans [expr ($NBoxes - 1)]
set CBNodes [expr ($BL + 1)]
set NElements $BL
puts "The Number of Spans: $NSpans"
puts "The Number of Elements in the Beam: $NElements"
puts "The Number of Nodes in the Beam: $CBNodes"

#KS - Spring Stiffness; KB - Beam Stiffness
set E 29000
set A 9.855
set Iz 21.2085
set KS 60000

for {set i 1} {$i <= $CBNodes} {incr i 1} {
node $i [expr ($i - 1)] 0.0
}
#FXNd - Fixity Node
set FS [expr ($NBoxes/2)]
set FX 0
for {set i 1} {$i <= $FS} {incr i 1} {
set FX [expr ($FX + $BSA($i))]
}
set FXNd $FX
fixX $FXNd 1 0 0
puts "The X Coordinate Fixity node is $FXNd"

set xS 0
for {set i 1} {$i <= $NBoxes} {incr i 1} {
set xS [expr ($xS + $BSA($i))]; node [expr ($CBNodes + $i)] $xS 0.00
fix [expr ($CBNodes + $i)] 1 1 1
}

puts "Geometry In"

# add material - command: material <matType> matID <matArgs>
uniaxialMaterial Elastic 1 $KS

geomTransf Linear 1

set a 1
for {set i $a} {$i <= $BL} {incr i 1} {
element elasticBeamColumn $i $i [expr ($i + 1)] $A $E $Iz 1
}
puts "Beam Elements In"

set xS 0
for {set i 1} {$i <= $NBoxes} {incr i 1} {
set xS [expr ($xS + $BSA($i))]
element zeroLength [expr ($BL + $i)] [expr ($CBNodes + $i)] [expr ($xS + 1)] -mat 1 1 -dir 2 1
}

timeSeries Constant 1

set a 1
for {set m $a} {[expr ($m <= $NElements)]} {incr m 1} {
pattern Plain $m 1 {
eleLoad -ele $m -type -beamUniform -0.002795
}
}
puts "OKOK"
set LR 20
set a 1
#for {set l $a} {$l <= [expr ($BL - $LR)]} {incr l 2} {
for {set i 1} {$i <= [expr ($BL - $LR + 1)]} {incr i 2} {
pattern Plain [expr ($m + $i/2)] 1 {
for {set j 0} {$j <= 19} {incr j 1} {
eleLoad -ele [expr ($i + $j)] -type -beamUniform -1
}
}

#recorder Element <-file $fileName> <-time> <-ele ($ele1 $ele2 ...)> <-eleRange $startEle $endEle> <-region $regTag> <-eleall> ($arg1 $arg2 ...)
recorder Element -file eleGlobal_temp1.out -time -eleRange 1 390 force

#recorder display "eleGlobal_temp.out" 10 100 500 500 -wipe
#vup 0 1 0 # dirn defining up direction of view plane(ghab bordar movazi)
#vpn 0 0 1 # direction of outward normal to view plane(amud bar safhe ghab)
#prp 0 0 1 # eye location in local coord sys defined by viewing system
#viewWindow -300 300 -500 500
#display 2 2 200
##
puts "analysis"

# Create the system of equation
system BandGeneral

# Create the DOF numberer, the reverse Cuthill-McKee algorithm
numberer Plain

# Create the constraint handler, a Plain handler is used as homo constraints
constraints Transformation 1.0

test NormDispIncr 1e-6 50
# Create the integration scheme, the LoadControl scheme using steps of 1.0
integrator LoadControl 1

# Create the solution algorithm, a Linear algorithm is created
algorithm Newton

# create the analysis object
analysis Static

analyze 1

set infln [open eleGlobal_temp1.out "r"]
set OPR [read $infln]

set xin 0
set j 1
foreach xin $OPR {
set OP($j) $xin
puts "OP($j): $OP($j)"
incr j 1
}
close $infln

set OPSize [array size OP]
puts "OPSize: $OPSize"

#Initialize arrays
for {set j 1} {$j <= $CBNodes} {incr j 1} {
set MMax($j) 0
set MMin($j) 0
}
for {set j 1} {$j <= $NElements} {incr j 1} {
set OPj [expr (6*($j - 1) + 7)]
if {$OP($OPj) > $MMax($j)} {
set MMax($j) $OP($OPj)
puts $j
}

if {$OP($OPj) < $MMin($j)} {
set MMin($j) $OP($OPj)
}
}

remove loadPattern [expr ($m + $i)]

}

puts "Static Analysis Done"
nskok
Posts: 17
Joined: Wed May 15, 2013 12:09 pm
Location: Purdue University

Re: cannot read all of values from a txt file

Post by nskok »

The problem is with the element recorder. The recorders sometimes do not flush all the data to the file until the wipe command is invoked, as far as I know. However, using the wipe command in the middle of your for loop will delete your model.

Instead of using the element recorder to generate an output file, I suggest that you set up a for loop that calls the eleResponse command to get the forces in each element after you run the analysis. You seem comfortable writing loops, so it shouldn't be too difficult! This way you won't have to worry about writing and reading a file for each step of your analysis.

EleResponse Command: http://opensees.berkeley.edu/wiki/index ... se_Command

- Nick
fmk
Site Admin
Posts: 5884
Joined: Fri Jun 11, 2004 2:33 pm
Location: UC Berkeley
Contact:

Re: cannot read all of values from a txt file

Post by fmk »

you can also use the -closeOnWite option with the recorders.
dkwong
Posts: 6
Joined: Mon Jun 16, 2014 7:09 am
Location: University at Buffalo

Re: cannot read all of values from a txt file

Post by dkwong »

Nick, Frank,
Thanks for your reply and idea. I revised my code by using eleResponse. I did find it's more convenient in my case.
Then I used the command "set out [open MMax.txt "w"]" to output the maximum moment for each node in all cases (186 cases), but the output showed some weird results after No.230 node. The results from node 1 to 230 seem making sense. Even though I can figure out the values of the rest of nodes because of symmetry, if it is asymmetric I have to fix it. When I directly used source xxxx.tcl in the opensees window. I got the values from node 1 to 340. But it's still not all of the values I want. (THere are 391 nodes) Not sure why it stopped writing in the txt file.
dkwong
Posts: 6
Joined: Mon Jun 16, 2014 7:09 am
Location: University at Buffalo

Re: cannot read all of values from a txt file

Post by dkwong »

I fixed it.
Thanks anyway.
Post Reply