CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Running, Solving & CFD (http://www.cfd-online.com/Forums/openfoam-solving/)
-   -   Tutorial of how to plot residuals ! (http://www.cfd-online.com/Forums/openfoam-solving/64146-tutorial-how-plot-residuals.html)

 wolle1982 April 30, 2009 09:07

Tutorial of how to plot residuals !

3 Attachment(s)
Hi all,

since apearantly noone has an idea of how to plot the residuals of a calculation on-the-fly, I will give a small manual on that:

Tutorial on "How to plot the residuals (and forces) graphically on screen on-the-fly"

Step 1:
Start the calculation and make it write out a log-file. for example
turbFoam >log
"log" is the name of the log-file to be output. It is written into the main-case-folder.

Step 2:
If desired you can open a new console-window of the main-case-folder and follow the text-output by the command
tail -f log
"log" is the name of the log-file to be read in. To stop reading the file constantly just use Crtl+C

Step 3:
To plot the residuals graphically on the screen you can use gnuplot that is delivered with linux already.
Within the main-case-folder you have to put a text file with a name e.g. "Residuals"
(also see attachments).
The file should contain the following gnuplot properties:
set logscale y
set title "Residuals"
set ylabel 'Residual'
set xlabel 'Iteration'
plot "< cat log | grep 'Solving for Ux' | cut -d' ' -f9 | tr -d ','" title 'Ux' with lines,\
"< cat log | grep 'Solving for Uy' | cut -d' ' -f9 | tr -d ','" title 'Uy' with lines,\
"< cat log | grep 'Solving for Uz' | cut -d' ' -f9 | tr -d ','" title 'Uz' with lines,\
"< cat log | grep 'Solving for omega' | cut -d' ' -f9 | tr -d ','" title 'omega' with lines,\
"< cat log | grep 'Solving for k' | cut -d' ' -f9 | tr -d ','" title 'k' with lines,\
"< cat log | grep 'Solving for p' | cut -d' ' -f9 | tr -d ','" title 'p' with lines
pause 1
reread
The pause-command sets the seconds till reload. Deletion makes it faster in some cases.

Execute the command
gnuplot Residuals -
in the main-case-folder.

Step 4:
Another good indicator for the calculations convergence is the forces-plot. Therefore you have to set the function in the controlDict that calculates the forces and forceCoeffs. See thread http://www.cfd-online.com/Forums/ope...es-of15-3.html or attachments.

Be sure to have the properties for gnuplot in the main-case-folder (see attachments).

You have the adapt the folder-name where the forceCoeffs.dat is inside before.

While the calculation runs you also can use the gnuplot command
gnuplot forceCoeffs -
in the main-case-folder. When the forces seem to not change any more, the pressure allocation must be constantly what makes the convergent case proofed.

Step5:
Plotting the real forces is also easy. Proceed identically like in "Step 4" but be sure to set
magUInf 1.0; //free stream velocity magnitude
lRef 1.0; //reference length
Aref 1.632653; //reference area
in the controlDict.

Using the attached text-files, remove the ".txt" first.

Hope that helps somebody.

Greetings,
Wolfgang

 marico May 7, 2009 08:23

thanks ;) ....................

 marico May 7, 2009 08:32

what about having pressure iterations. If I have say 5 pressure loops, the pressure graph "runs" away and plots over 5x instead of x....

 flavio_galeazzo May 8, 2009 08:46

Thanks, Wolfgang! Really very useful.

 ashishnedungadi May 9, 2009 10:28

Wolfgang,
Vielen dank! As a newby to OF, I needed this tutorial.

Regards
Ashish

 Antonin May 18, 2009 06:48

Thanks, this tool is very useful.

 leonardo.morita June 3, 2009 04:16

Hello,

What should I add if I wanted to plot the final residuals, not the initial ones?

Thanks!

 anger June 4, 2009 05:50

Quote:
 Originally Posted by marico (Post 215426) what about having pressure iterations. If I have say 5 pressure loops, the pressure graph "runs" away and plots over 5x instead of x....
Hi,

you could change the line extracting the pressure to read:
cat log | grep 'Solving for p' | cut -d' ' -f9 | sed -n 'p;N;N;N;N' | tr -d ','&quot; title 'p' with lines
If you had two pressure iterations, you would change the argument to sed to read 'p;N'.
If the final residual should be plottet, replace f9 at the cut command with f13.

Best,
-Thomas

 leonardo.morita June 4, 2009 06:46

It works perfectly, thank you very much, Thomas!
By the way, what does f13 mean? Something related to characters?

Regards,

Leonardo

 anger June 4, 2009 07:03

Quote:
 Originally Posted by leonardo.morita (Post 218243) It works perfectly, thank you very much, Thomas! By the way, what does f13 mean? Something related to characters? Regards, Leonardo
f13 means field number 13 in the current set of fields which was created by cutting the line which was extracted by grep into pieces via the cut command.
"man cut" is your friend.

Best,
-Thomas

 bernarde July 8, 2009 04:36

Thanks for this tool Wolfgang - a very useful and important tool.

 sandy August 14, 2009 05:28

force.txt??

Hi Wolfgang, thanks a lot. However, I guess, maybe you forget to give a attachment about "force.txt" ? :o

 Ahmed August 15, 2009 13:32

Thank You

That is a real brilliant addition to OpenFOAM
Thanks and Good Luck

 PaMuelle September 1, 2009 10:36

Quote:
 Originally Posted by anger (Post 218235) Hi, you could change the line extracting the pressure to read: cat log | grep 'Solving for p' | cut -d' ' -f9 | sed -n 'p;N;N;N;N' | tr -d ','" title 'p' with lines If you had two pressure iterations, you would change the argument to sed to read 'p;N'. If the final residual should be plottet, replace f9 at the cut command with f13. Best, -Thomas
Sorry for pushing up such an old topic. I'm currently working with this script, thanks Wolfgang for making my life so much easier!
But I have a little problem of undestanding why in Thomases addittion the first iteration gets plottet and the other four are ignored. Isn't the last of these iterations the most accurate? Or did I just misunderstand the code and it ignores the first iterations and plots the last one?

Greetings,
Patrick

PS:
I just plotted the residuals for both versions (p;N;N vs N;N;p), here are my solutions:
p;N;N;
http://img136.imageshack.us/img136/8...sidualspnn.jpg
- N;N;p:
http://img402.imageshack.us/img402/9...sidualsnnp.jpg
To me, the N;N;p version looks much more wishable and plausible. Btw I know that the residuals look a little bit suspicious after ~30 iterations, it's a problem I'm currently working on ;)

 anger September 1, 2009 15:54

Hello Patrick,

the place where the p lives is the line which is taken into account for further processing; when you have three lines and pipe them through sed with p;N;N only the first one is kept (as far as I remember); if in doubt, just consult the manpages.
As for the residuals, I'm judging convergence only with respect to the initial value of the pressure (or velocity or whatever) residual because the second number gives you the residual after running the inner linear solver; the values calculated there will be changed again and are in no way related to the state of convergence of the whole flow under consideration.

Best regards,
-Thomas

 andersking September 1, 2009 21:30

Another way to do this is to use pyFoam, http://openfoamwiki.net/index.php/Contrib_PyFoam, in particular

Code:

```turbFoam > log & pyFoamPlotWatcher.py log```
this also plots a number of other things as well (and also provides a number of other useful utilities).

Cheers,
Andrew

 sandy September 2, 2009 00:39

Hi Thomas, I am still stunned. When should I write:

cat log | grep 'Solving for p' | cut -d' ' -f9 | sed -n 'p;N;N;N;N' | tr -d ','&quot; title 'p' with lines (four 'N')

OR

cat log | grep 'Solving for p' | cut -d' ' -f9 | sed -n 'p;N;N' | tr -d ','&quot; title 'p' with lines (just two 'N' ???)

OR

cat log | grep 'Solving for p' | cut -d' ' -f13 | sed -n 'p;N;N;N;N' | tr -d ','&quot; title 'p' with lines ???

What is the differences to three ways?? Thanks

 PaMuelle September 2, 2009 01:35

Quote:
 Originally Posted by sandy (Post 228201) Hi Thomas, I am still stunned. When should I write: cat log | grep 'Solving for p' | cut -d' ' -f9 | sed -n 'p;N;N;N;N' | tr -d ','" title 'p' with lines (four 'N') OR cat log | grep 'Solving for p' | cut -d' ' -f9 | sed -n 'p;N;N' | tr -d ','" title 'p' with lines (just two 'N' ???) OR cat log | grep 'Solving for p' | cut -d' ' -f13 | sed -n 'p;N;N;N;N' | tr -d ','" title 'p' with lines ??? What is the differences to three ways?? Thanks
Hi Sandy,

at first you determine which field you need. f13 means that you use the 13th field in the row, while you start counting with the comma after "solving for p". e.g. GAMG: Solving for p, Initial residual = 0.193462, Final residual = 1.37372e-05, No Iterations 8
The 9th field in this example would be the number 1.37e-05. The 13th would be the field after "8", which in my case doesn't exist. (Fields are seperated by an spare field, this is defined by: "by cut -d' '") Therefore you have to check your logfile and decide which field you want to plot. In the normal case this would be the 9th (-> f9).
The number of "N" you have to write is determined by the number of iterations for p per timestep. In my case GAMG iterated p three times, so I have to ignore two iterations and write two Ns. In Thomases case, p is iterated five times, so he writes four Ns.
I hope I could help you and didn't write too confusing, my first coffee still isn't done ;)

Greetings

 openfoam_user October 19, 2009 08:35

Dear Wolfgang, dear OF users,

Your utility for plotting the residuals works fine !

But I have difficulties in plotting the forces.

I have the following file. What is the gnuplot command to plot Time versus viscous force in Z-direction ?

# Time forces(pressure, viscous) moment(pressure, viscous)
1 (((1004.63 -5494.47 -84811.2) (-0.0374886 -0.0406053 198.671)) ((892.014 943.938 -82187.3) (-0.0179318 -0.0364229 -76.003)))
2 (((644.515 -3547.57 -121082) (-0.0537444 0.172973 204.824)) ((510.954 640.954 -73474.2) (-0.0724901 -0.0655304 -74.3758)))
3 (((25.51 -475.303 -90723.7) (-0.0715687 0.446436 209.538)) ((-67.716 124.304 -30308.4) (-0.115215 -0.0974344 -72.0424)))
4 (((-535.122 1943.6 -44831) (-0.0842006 0.560531 210.419)) ((-442.742 -316.419 10907.2) (-0.107376 -0.103469 -70.0699)))
5 (((-633.929 2570.15 4849.92) (-0.0969208 0.553209 210.327)) ((-433.453 -408.939 29814.8) (-0.0604264 -0.0826774 -69.0365)))
6 (((-334.82 1661.22 28439.5) (-0.0418605 0.582708 213.964)) ((-203.231 -256.368 24363.6) (-0.0309029 -0.00757822 -68.957)))
7 (((160.717 261.099 30270.3) (-0.156219 0.117149 220.562)) ((78.173 -8.60102 6858.77) (0.0489021 0.00722051 -70.0195)))
8 (((517.415 -791.764 17582.5) (-0.283258 -0.337861 224.95)) ((246.292 212.329 -9360.35) (0.0700387 -0.0303728 -71.5801)))
9 (((495.253 -1062.81 -5910.24) (-0.500476 -0.331616 224.337)) ((226.706 241.192 -16497.8) (0.0157957 -0.105875 -72.9496)))
10 (((229.94 -659.331 -22150.2) (-0.548931 -0.219443 220.292)) ((99.4034 139.613 -14183.1) (-0.0509668 -0.171714 -73.6661)))

Regards,

Stephane.

 openfoam_user October 20, 2009 08:33

the answer is

plot "< cat forces.dat | tr -d '(' | tr -d ')'" using 1:4

stephane

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