Cannot read 2nd column of data in libforces.so output force.dat file
I am using libforces.so in control dict. The output is forces.dat file and has the following format:
time ((number, number, numer), (number ...etc)
the problem is that there is NO spaces between the first pair of parenthesis and the number right after it. As a result, that number cannot be read as a number but has to be read as character. I have a FORTRAN code that reads this file, and I found that it was not possible to read the first number after the double parenthesis as a real. So my only option is to read it as character - which makes this data useless. Can the OpenFOAM creators please rewrite the code so that there is space between the number and the parenthesis?. Also, would it be possible to include the number of lines of data in the file in the first line? something like this:
# Time forces((.....)) moment ((......)) lines of data (......)
:confused: Why should the developers change the source code, when it works fine as it is? Specially when it is formatted in a way that OpenFOAM itself can easily re-read the data back into memory!
Nonetheless, what you might want to ask for is for a way to choose other output formats for the forces function object.
Either way, there are several ways you can fix this problem on your side! Some examples:
Please read and follow the detailed step-by-step guide, which I wrote a couple of weeks ago in this thread:
# Time forces(pressure, viscous, porous) moment(pressure, viscous, porous)
0.00116279 ((0 65.2165 0),(-1.20232e-17 0.00107097 5.21998e-07),(0 0 0)) ((15.435 0 0),(0.000198423 2.02581e-17 8.3249e-17),(0 0 0))
0.00251938 ((0 86.7929 0),(-1.10799e-16 0.00264427 1.61168e-06),(0 0 0)) ((20.5327 0 0),(0.000532886 -2.13627e-17 -1.9528e-16),(0 0 0))
0.00410207 ((0 30.7788 0),(-1.1e-16 0.00314398 4.56226e-06),(0 0 0)) ((7.26688 0 0),(0.000714734 -5.21951e-17 6.56713e-17),(0 0 0))
0.00593798 ((0 -34.7985 0),(-8.21523e-17 0.00205407 -1.62991e-06),(0 0 0)) ((-8.2514 0 0),(0.000595282 -1.95364e-17 -1.90247e-17),(0 0 0))
Now observe the second line:
0.00116279 ((0 65.2165 0), .....
I am using fortran read command as follows:
read(......) time, char, char, forceX, forceY, forceZ etc..
Fortran hangs at char, as it cannot distinguish between the "((" and the "0" right after it and gives an error saying bad real number. So now, if I revise the read statement as:
read(...) time, char, forceY, forceZ, --- in other words treat "((0" as a char, then fortran will read it as char and move the the next number. I agree that scripts can be written to remove the parenthesis, but it does not permit an elegant solution. At the programming time, all the programmer would have had to do was to add a few spaces between the parenthesis, and between the parenthesis and the number.
Well... I had to smile a bit now... I remembered to search in the official bug tracker for OpenFOAM and found this feature request: http://www.openfoam.org/mantisbt/view.php?id=777
I found it by searching in Google with:
Now, as for the problem you have at the moment, the big question is: how do you want to solve this?
I ask this because:
fortran read of force.dat file
This is what I wrote to read the lines of data. I am only interested in the x y z forces, so my read line in fortran was as follows:
double precision::time, force_x, force_y, force_z
read(unit=unit_force_dat,fmt=*) time, charA, force_y
write(fout_gplt,100) time*3.13029, force_y/9810.0
Now note the data is out put in the following format (skipping the first line):
0.1 ((0 -5.06121 0),(-3.99743e-17 -0.00915985 -0.00028693),(0 0 0)) ((-0.801432 0 0),(-0.0036854 -3.35203e-18 2.30784e-17),(0 0 0))
0.2 ((0 19.0264 0),(1.57733e-17 0.00228343 -0.000354185),(0 0 0)) ((4.92259 0 0),(0.0023383 7.10629e-18 1.67709e-18),(0 0 0))
0.3 ((0 11.5462 0),(-7.85821e-18 0.00388275 -0.000954584),(0 0 0)) ((2.82873 0 0),(0.00702285 -4.54422e-18 -1.1446e-18),(0 0 0))
So starting from the left, there is one real number, followed by space, then two parenthesis and then three real numbers. So my read statement should be:
read (...) time, charA, charB, forceX, forceY, forceZ
But it seems that the real number after the second parenthesis is so close, that fortran gives a "bad real number" error if I use this format. So the "workaround" that I have is to treat the ((0 as char and then read forceY. Perhaps you may have a better suggestion.
I forgot that I haven't actually seriously programmed in FORTRAN for several years now... I'm getting old :rolleyes:.
Attached is a zip file that demonstrates how you can filter out the stuff you don't need from each line.
Since you didn't specify any compiler limitations, nor did you specify the FOTRAN standard, I simply used the one I'm familiar with.
To detail some of the aspects to this:
very clever! thanks.
reading force data from interDyMFOAM
Your test file has space after the first real data and before the parenthesis. However the output from OpenFOAM has a tab. And this tab I cannot overcome! I look forward to your reply
Sorry for the false alarm. The code works. I randomly put tabs and the code ignored them and got the results. I tell you, if a movie was ever made about the gods of CFD, you would be the one and only star!.
Thanks again for your help!
|All times are GMT -4. The time now is 20:01.|