CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Running, Solving & CFD (http://www.cfd-online.com/Forums/openfoam-solving/)
-   -   How to plot a forces.dat file (with all the brackets)? (http://www.cfd-online.com/Forums/openfoam-solving/79474-how-plot-forces-dat-file-all-brackets.html)

CapSizer August 25, 2010 03:46

How to plot a forces.dat file (with all the brackets)?
 
The wall forces calculation method in the recent versions of OF work very nicely, but produce a file (by default called forces.dat) with a lot of brackets (or parentheses, if you speak that variant of English). A typical line in the file looks like this:


(((6.57 128.05 364.35) (34.46 2.43 4.29)) (( etc .....) (......) (......)))


Now I really like to plot the force history as a means of judging convergence, but this format is not amenable to easy plotting with Gnuplot. One can obviously import into a spreadsheet, but that is mighty tedious. The question is this: Is there an easy way of telling Gnuplot to ignore the brackets, or is there another plotting program that can do it easily? I'm sure that one can give Gnuplot a "plot using " command that can deal with the format .... I just don't seem to get it right! Any advice?

rieuk August 25, 2010 23:26

I plotted my stuff in Excel but only after I'd extracted and compiled all the data into one tab delimited file using a bash script. It's not too difficult. A few hours reading of bash scripting if you have no experience should be good enough.

sinusmontis August 26, 2010 09:05

Hello,

you could use "sed" to filter your gunplot input. Use following line in gnuplot (the input for gnuplot will be the output of sed):

plot "< sed s/[\\(\\)]//g input.dat" using 1:2


Regards,

Malte

CapSizer August 26, 2010 09:24

Thanks!
 
Great, thanks for that, it has got me going along the right lines!

egp August 27, 2010 07:34

Another solution is to use python to parse and plot, e.g.,

Code:

import re
forceRegex=r"([0-9.Ee\-+]+)\s+\(+([0-9.Ee\-+]+)\s([0-9.Ee\-+]+)\s([0-9.Ee\-+]+)\)\s\(([0-9.Ee\-+]+)\s([0-9.Ee\-+]+)\s([0-9
.Ee\-+]+)\)+\s\(+([0-9.Ee\-+]+)\s([0-9.Ee\-+]+)\s([0-9.Ee\-+]+)\)\s\(([0-9.Ee\-+]+)\s([0-9.Ee\-+]+)\s([0-9.Ee\-+]+)\)
+"
t = []
fpx = []; fpy = []; fpz = []
fvx = []; fvy = []; fvz = []
mpx = []; mpy = []; mpz = []
mvx = []; mvy = []; mvz = []
pipefile=open('forces.dat','r')
lines = pipefile.readlines()
for line in lines:
        match=re.search(forceRegex,line)
                if match:
                        t.append(float(match.group(1)))
                        fpx.append(float(match.group(2)))
                        fpy.append(float(match.group(3)))
                        fpz.append(float(match.group(4)))
                        fvx.append(float(match.group(5)))
                        fvy.append(float(match.group(6)))
                        fvz.append(float(match.group(7)))
                        mpx.append(float(match.group(8)))
                        mpy.append(float(match.group(9)))
                        mpz.append(float(match.group(10)))
                        mvx.append(float(match.group(11)))
                        mpy.append(float(match.group(12)))
                        mpz.append(float(match.group(13)))
xlabel('time (sec)')
ylabel('force (N)')
grid()
title('Example of using python to parse the force/moment tuples')
plot(t,fpx,'o-')

---

Of course, you need numpy and matplotlib loaded for the above example to work. I would also recommend using ipython as it permits an improved interactive environment.

If you haven't studied regular-expressions (regex), I would recommend the python regex howto and the book by H.P. Langtangen, "Python Scripting for Computational Science."

protarius February 22, 2012 15:32

typos
 
I found some typos in the python code. The corrected code should be:

Code:

#!/usr/bin/python

import pylab

import re
forceRegex=r"([0-9.Ee\-+]+)\s+\(+([0-9.Ee\-+]+)\s([0-9.Ee\-+]+)\s([0-9.Ee\-+]+)\)\s\(([0-9.Ee\-+]+)\s([0-9.Ee\-+]+)\s([0-9 .Ee\-+]+)\)+\s\(+([0-9.Ee\-+]+)\s([0-9.Ee\-+]+)\s([0-9.Ee\-+]+)\)\s\(([0-9.Ee\-+]+)\s([0-9.Ee\-+]+)\s([0-9.Ee\-+]+)\)+"
t = []
fpx = []; fpy = []; fpz = []
fvx = []; fvy = []; fvz = []
mpx = []; mpy = []; mpz = []
mvx = []; mvy = []; mvz = []
pipefile=open('forces.dat','r')
lines = pipefile.readlines()
for line in lines:
        match=re.search(forceRegex,line)
        if match:
                t.append(float(match.group(1)))
                fpx.append(float(match.group(2)))
                fpy.append(float(match.group(3)))
                fpz.append(float(match.group(4)))
                fvx.append(float(match.group(5)))
                fvy.append(float(match.group(6)))
                fvz.append(float(match.group(7)))
                mpx.append(float(match.group(8)))
                mpy.append(float(match.group(9)))
                mpz.append(float(match.group(10)))
                mvx.append(float(match.group(11)))
                mvy.append(float(match.group(12)))
                mvz.append(float(match.group(13)))

pylab.xlabel('time (sec)')
pylab.ylabel('force (N)')
pylab.grid(True)
##title('Example of using python to parse the force/moment tuples')
pylab.plot(t,fpx,'o-')
pylab.show()

Regards

jferrari February 23, 2012 14:06

Quote:

Originally Posted by CapSizer (Post 272628)
The wall forces calculation method in the recent versions of OF work very nicely, but produce a file (by default called forces.dat) with a lot of brackets (or parentheses, if you speak that variant of English). A typical line in the file looks like this:


(((6.57 128.05 364.35) (34.46 2.43 4.29)) (( etc .....) (......) (......)))


Now I really like to plot the force history as a means of judging convergence, but this format is not amenable to easy plotting with Gnuplot. One can obviously import into a spreadsheet, but that is mighty tedious. The question is this: Is there an easy way of telling Gnuplot to ignore the brackets, or is there another plotting program that can do it easily? I'm sure that one can give Gnuplot a "plot using " command that can deal with the format .... I just don't seem to get it right! Any advice?

If you're able to get the forces.dat file you should also be able to get the force coefficients. The forceCoeffs.dat file is formatted differently and is easier to use gnuplot to check the convergence.


All times are GMT -4. The time now is 16:37.