Jump to content
Sign in to follow this  
mkress

Add different results into a variable

Recommended Posts

Hi guys,

 

i have a problem to calculate the average of some results with a tcl script. Perhaps someone could help me here.

 

I export from HyperGraph pressure results of some nodes of a transient simulation (normally i export 20-50 nodes with 50-100 time steps).

 

A simple example can  you see in the following excel picture.

Before the comma, there is the time step, after the comma the pressure.

 

image.png.7f625641c16bcd6eea2d00bb58f9250f.png

 

The differnt nodes are seprate by an empty raw. (In this example there a three nodes)

 

How can i add the different pressures, to get the average pressure of all nodes?

 

I think the first step is, to spilt every raw by the comma.

The following picture shows my current script.

 

image.thumb.png.7b545c6ca8f3a3c7a18dbe775c86c670.png

 

I think "append" is not the right command for this problem.

 

Has anyone a proposal???

 

Thank you and best regards

Michael

 

 

test.csv

Average_curve.tcl

Share this post


Link to post
Share on other sites

Hi tinh,

thank you for your reply.

 

With lappend i create a list, but i want to calculate the average.

 

I would like to calulate the sum of the pressure of every first time steps [(p11+p12+p13)/3] , the sum of every second time steps [(p12+p22+p32)/3], the sum of the third time steps [(p13+23+33)/3],  and so on...

 

image.png.7f625641c16bcd6eea2d00bb58f9250f.jpg.2bab98dffb01bc449bfc22be93d7b9a5.jpg

 

Do you know what i meaning?

 

Perhaps this Problem could be solved with Loops?

 

 

Thank you and best regards

Michael

 

 

 

 

 

Share this post


Link to post
Share on other sites

Yes, I understand.

you have to loop for each node, example:

foreach i {1 2 3 4 5} {set SumP$i 0}

File2Buff $dir buff

set Count 0

foreach {line1 line2 line3 line4 line5 emptied} [split $buff \n] {

     incr Count

     foreach i {1 2 3 4 5} {

          set P$i [lindex [split [set line$i] ,] 1]
          set SumP$i [expr [set SumP$i]+[set P$i]]

     }

}

foreach i {1 2 3 4 5} {

     puts "Avg pressure at step $i = [expr 1.0*[set SumP$i]/$Count]"

}

 

mkress likes this

Share this post


Link to post
Share on other sites

Hi tinh,

 

thank you for your fast reply.

 

Unfortunately, i get an error from the last line...

 

 It is the line:               puts "Avg pressure at step $i = [expr 1.0*[set SumP$i]/$Count]"

 

Unbenannt.thumb.JPG.e64a099dbfd2b186a475a1c01a410632.JPG

 

I don´t understand this error. There are no characters???

 

 

 

 

 

Is it also possible to write for "1 2 3 4 5" in "foreach i {1 2 3 4 5}"   a variable $number_time_steps?

This would be important because i have always different numbers of time steps (50-100 steps).

 

 

 

Thank you very much!

 

Michael

 

 

 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
You are posting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...