CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Running, Solving & CFD

Residual Plotting Script

Register Blogs Community New Posts Updated Threads Search

Like Tree5Likes
  • 5 Post By RodriguezFatz

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   February 14, 2014, 05:34
Default Residual Plotting Script
  #1
Senior Member
 
RodriguezFatz's Avatar
 
Philipp
Join Date: Jun 2011
Location: Germany
Posts: 1,297
Rep Power: 27
RodriguezFatz will become famous soon enough
Hi all,

I made some handy script to plot residuals during runtime using gnuplot. It works with SIMPLE and PIMPLE, probably also with other solvers.
First of all, you need to get the OpenFOAM terminal output into a file. This is done by starting OpenFoam with the ">log" command, such as:

mpirun -np 4 pimpleFoam -parallel >log


or just

simple >log

where "log" is the name of the file with the output.
Now, create some new file "plotscript" in the same folder and copy this text into it:
Code:
set logscale y
set title "Residuals"
set ylabel 'Residual'
set xlabel 'Iteration'
set key outside
set grid

nCorrectors=1
nNonOrthogonalCorrectors=3

nCont = nCorrectors
nP = (nNonOrthogonalCorrectors+1)*nCont
nPSkip = nP-1
nContSkip = nCont-1



plot "< cat log | grep 'Solving for Ux' | cut -d' ' -f9 | tr -d ','" title 'Ux' with linespoints,\
"< cat log | grep 'Solving for Uy' | cut -d' ' -f9 | tr -d ','" title 'Uy' with linespoints,\
"< cat log | grep 'Solving for Uz' | cut -d' ' -f9 | tr -d ','" title 'Uz' with linespoints,\
"< cat log | grep 'Solving for k' | cut -d' ' -f9 | tr -d ','" title 'k' with linespoints,\
"< cat log | grep 'Solving for epsilon' | cut -d' ' -f9 | tr -d ','" title 'epsilon' with linespoints,\
"< cat log | grep 'time step continuity errors :' | cut -d' ' -f9 | tr -d ','" every nCont::nContSkip title 'continuity' with linespoints,\
"< cat log | grep 'Solving for p' | cut -d' ' -f9 | tr -d ','" every nP::nPSkip title 'p' with linespoints lc 8
pause 1
xmax = GPVAL_DATA_X_MAX+2
xmin = xmax-40
set xrange [xmin:xmax]
reread
This should be pretty self-explanatory, but if you have any questions please post!
You need to adapt the upper two numbers nCorrectors and nNonOrthogonalCorrectors to your case, because they need to delete all the intermediate steps of the pressure solving.
Also, for long runs I implemented the stuff between "pause 1" and "reread" which will show only the last 40 steps. You can change it to any number or switch off completely by putting a "#" at the beginning of each line. Of course, change "epsilon" to "omega" if you use some k-omega turbulence model.

Have fun,
Philipp.
laurentb, jherb, pete20r2 and 2 others like this.
__________________
The skeleton ran out of shampoo in the shower.
RodriguezFatz is offline   Reply With Quote

Old   February 14, 2014, 10:42
Default
  #2
Senior Member
 
Joachim Herb
Join Date: Sep 2010
Posts: 650
Rep Power: 22
jherb is on a distinguished road
Using your code I get the following error message:
Code:
"Residuals", line 26: undefined variable: GPVAL_DATA_X_MAX
> gnuplot --version
gnuplot 4.2 patchlevel 3
jherb is offline   Reply With Quote

Old   February 14, 2014, 11:09
Default
  #3
Senior Member
 
RodriguezFatz's Avatar
 
Philipp
Join Date: Jun 2011
Location: Germany
Posts: 1,297
Rep Power: 27
RodriguezFatz will become famous soon enough
Hi Joachim,

I got two ideas:
i) Your version is too old, I use 4.6
ii) The initial "plot" command did not work. Variable "GPVAL_DATA_X_MAX" is created by gnuplot after a plot was successfully done. Thus, if the lines before didn't work for some reason, also the GPVAL_DATA_X_MAX variable is not known. Does the script run without these three lines?
__________________
The skeleton ran out of shampoo in the shower.
RodriguezFatz is offline   Reply With Quote

Old   February 14, 2014, 18:40
Default
  #4
Senior Member
 
Joachim Herb
Join Date: Sep 2010
Posts: 650
Rep Power: 22
jherb is on a distinguished road
If I remove the three lines between pause and reread the script works.
jherb is offline   Reply With Quote

Old   February 17, 2014, 05:27
Default
  #5
Senior Member
 
RodriguezFatz's Avatar
 
Philipp
Join Date: Jun 2011
Location: Germany
Posts: 1,297
Rep Power: 27
RodriguezFatz will become famous soon enough
Hi,

You can start a gnuplot terminal
gnuplot
Then just do some random plot
plot 1
and let gnuplot show all the created variables
show variables all
Does it show the GPVAL_DATA_X_MAX?
__________________
The skeleton ran out of shampoo in the shower.
RodriguezFatz is offline   Reply With Quote

Old   February 17, 2014, 05:45
Default
  #6
Senior Member
 
Joachim Herb
Join Date: Sep 2010
Posts: 650
Rep Power: 22
jherb is on a distinguished road
Quote:
Originally Posted by RodriguezFatz View Post
Hi,

You can start a gnuplot terminal
gnuplot
Then just do some random plot
plot 1
and let gnuplot show all the created variables
show variables all
Does it show the GPVAL_DATA_X_MAX?
Thank you for your help.

Actually, no:
Code:
gnuplot> show variables all


        All available variables:
        pi = 3.14159265358979
        NaN = NaN
        GNUTERM = "x11"
        GPVAL_TERM = "x11"
        GPVAL_TERMOPTIONS = ""
        GPVAL_OUTPUT = ""
        GPVAL_VERSION = 4.2
        GPVAL_PATCHLEVEL = "3 "
        GPVAL_COMPILE_OPTIONS = "-READLINE  +LIBREADLINE  +HISTORY  +BACKWARDS_COMPATIBILITY  +BINARY_DATA  \n+GD_PNG  +GD_JPEG  +GD_TTF  +GD_GIF  +ANIMATION  \n+NOCWDRC  +X11  +X11_POLYGON  +MULTIBYTE  +USE_MOUSE  +GNUGRAPH  +HIDDEN3D_QUADTREE  \n+DATASTRINGS  +HISTOGRAMS  +OBJECTS  +STRINGVARS  +MACROS  +THIN_SPLINES  +IMAGE  \n"
        GPVAL_X_MIN = -10.0
        GPVAL_X_MAX = 10.0
        GPVAL_X_REVERSE = 0
        GPVAL_X_LOG = 0.0
        GPVAL_Y_MIN = 0.99
        GPVAL_Y_MAX = 1.01
        GPVAL_Y_REVERSE = 0
        GPVAL_Y_LOG = 0.0
        GPVAL_X2_MIN = -10.0
        GPVAL_X2_MAX = 10.0
        GPVAL_X2_REVERSE = 0
        GPVAL_X2_LOG = 0.0
        GPVAL_Y2_MIN = 0.99
        GPVAL_Y2_MAX = 1.01
        GPVAL_Y2_REVERSE = 0
        GPVAL_Y2_LOG = 0.0
        GPVAL_Z_MIN = -10.0
        GPVAL_Z_MAX = 10.0
        GPVAL_Z_REVERSE = 0
        GPVAL_Z_LOG = 0.0
        GPVAL_CB_MIN = -8.98846567431158e+307
        GPVAL_CB_MAX = 8.98846567431158e+307
        GPVAL_CB_REVERSE = 0
        GPVAL_CB_LOG = 0.0
        GPVAL_T_MIN = -5.0
        GPVAL_T_MAX = 5.0
        GPVAL_T_REVERSE = 0
        GPVAL_T_LOG = 0.0
        GPVAL_U_MIN = -10.0
        GPVAL_U_MAX = 10.0
        GPVAL_U_REVERSE = 0
        GPVAL_U_LOG = 0.0
        GPVAL_V_MIN = -10.0
        GPVAL_V_MAX = 10.0
        GPVAL_V_REVERSE = 0
        GPVAL_V_LOG = 0.0
So I guess, my gnuplot version is too old. I will try a newer one (if I be able to compile it without root access).
jherb is offline   Reply With Quote

Old   February 17, 2014, 05:54
Default
  #7
Senior Member
 
Joachim Herb
Join Date: Sep 2010
Posts: 650
Rep Power: 22
jherb is on a distinguished road
Ok, with the current version of gnuplot (4.6.4) it is working.

Thank your for the script.
jherb is offline   Reply With Quote

Old   February 17, 2014, 05:56
Default
  #8
Senior Member
 
RodriguezFatz's Avatar
 
Philipp
Join Date: Jun 2011
Location: Germany
Posts: 1,297
Rep Power: 27
RodriguezFatz will become famous soon enough
You could have changed the GPVAL_DATA_X_MAX to GPVAL_X_MAX?
Seems like they renamed that in the newer versions...
__________________
The skeleton ran out of shampoo in the shower.
RodriguezFatz is offline   Reply With Quote

Old   September 12, 2016, 03:43
Default How to export image of finally converged solution and how to close gnuplot
  #9
Member
 
Sing
Join Date: Jan 2016
Posts: 30
Rep Power: 10
Struggle_Achieve is on a distinguished road
Hello Phillipp,
First of all thank you very much for posting this script.
I have used this script for a simulation which I do manually and its working fine.

Problem arose when I automated the process of simulation using shell script.
In the script I have used the following code.

Code:
mpirun -np 4 buoyantBoussinesqSimpleFoam -parallel >log 
gnuplot residue
reconstructPar
My residue file has this code:
Code:
set logscale y
set title "Residuals"
set ylabel 'Residual'
set xlabel 'Iteration'
set term postscript     
set output "printme.ps"

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 T' | cut -d' ' -f9 | tr -d ','" title 'T' with lines,\
"< cat log | grep 'Solving for p_rgh' | cut -d' ' -f9 | tr -d ','" title 'p_rgh' with lines,\
"< cat log | grep 'Solving for epsilon' | cut -d' ' -f9 | tr -d ','" title 'epsilon' with lines,\
"< cat log | grep 'Solving for k' | cut -d' ' -f9 | tr -d ','" title 'k' with lines

pause 1
reread
What is happening is that everything works fine till gnuplot starts running I also get the image of the finally converged solution as a file (.ps format). But I am unable to close the gnuplot using script. I have to manually close this (using Ctrl+C). And without closing the next command i.e. reconstructPar is not executing. I also tried to use command gnuplot residue & , but after using this, gnuplot goes in background and I am unable to close it in anyway.

Any help would be greatly appreciated.

Thanks and regards,
Singh.
Struggle_Achieve is offline   Reply With Quote

Old   September 13, 2016, 03:12
Default
  #10
Senior Member
 
RodriguezFatz's Avatar
 
Philipp
Join Date: Jun 2011
Location: Germany
Posts: 1,297
Rep Power: 27
RodriguezFatz will become famous soon enough
Hi Singh,

The line "pause 1" and "reread" let the gnuplot script restart itself again and again. That's why gnuplot seems to freeze.
Maybe you need to find a better way. The problem is, when you just delete them, gnuplot plots and gets closed very fast, so also the plotting window gets closed. You won't be able to see the plot.
There is probably a way to do what you want, but I never did that.

Philipp.
__________________
The skeleton ran out of shampoo in the shower.
RodriguezFatz is offline   Reply With Quote

Old   September 14, 2016, 01:42
Default Found a way!!
  #11
Member
 
Sing
Join Date: Jan 2016
Posts: 30
Rep Power: 10
Struggle_Achieve is on a distinguished road
Quote:
Originally Posted by RodriguezFatz View Post
Hi Singh,

The line "pause 1" and "reread" let the gnuplot script restart itself again and again. That's why gnuplot seems to freeze.
Maybe you need to find a better way. The problem is, when you just delete them, gnuplot plots and gets closed very fast, so also the plotting window gets closed. You won't be able to see the plot.
There is probably a way to do what you want, but I never did that.

Philipp.
Hello Rodriguez,
Thank you very much for the reply.
I have found a way to close the gnuplot which is in background. I am not sure whether this is the right way to do but it worked for me.

What I have done is that I have used the following commands in my shell script:

Code:
mpirun -np 4 buoyantBoussinesqSimpleFoam -parallel >log &
sleep 10s
gnuplot residue &

a=0
while [ a=0 ]
do
FILE=log  
# How many seconds before file is deemed "older"
OLDTIME=5
#Get current and file times
CURTIME=$(date +%s)
FILETIME=$(stat $FILE -c %Y)
TIMEDIFF=$(expr $CURTIME - $FILETIME)

# Check if file older
if [ $TIMEDIFF -gt $OLDTIME ]; then
sleep 5s
python plotEnd.py
   a=1
break
fi
done
The while loop checks if the log while is updated or not. If its not updated for 5 seconds then it runs a python script I have written. The code of the python script is as follows:
Code:
import sys 
import os

os.system("ps -aux|grep 1016914 > log_ID")
with open('log_ID','r') as LM:
    for line in LM:
        A = line.split()
        if len(A)>=2:
            #print A[10]
            if A[10]=="gnuplot":
                z = A[1];
                #print (z)
            if A[10]=="evince":
                y = A[1];
os.system("kill -9 " + z)
os.system("time.sleep(20)")
os.system("kill -9 " + y)
Basically os.system("ps -aux|grep 1016914 > log_ID") saves all the processes which I am using in the file log_ID, then I read the ID for gnuplot and kill it.

And this worked for me, hope it will be helpful to some other newbie like me.

Thanks and regards,
Prateek Singh.
Struggle_Achieve is offline   Reply With Quote

Old   June 29, 2022, 02:48
Default
  #12
New Member
 
Join Date: Apr 2022
Posts: 9
Rep Power: 4
aeronerd is on a distinguished road
starting with scripts from this page, and modifying things a bit I found this works and is quickly tuneable as the number of pressure iterations per timestep changes (15 is bad... I know...... but it works for me

set logscale y
set title "Residuals"
set ylabel 'Residual'
set xlabel 'Iteration'
set grid


plot "< cat log.pimpleFoam | grep 'Solving for Urelx' | cut -d' ' -f9 | tr -d ','" title 'Ux' with lines,\
"< cat log.pimpleFoam | grep 'Solving for Urely' | cut -d' ' -f9 | tr -d ','" title 'Uy' with lines,\
"< cat log.pimpleFoam | grep 'Solving for Urelz' | cut -d' ' -f9 | tr -d ','" title 'Uz' with lines,\
"< cat log.pimpleFoam | grep 'Solving for epsilon' | cut -d' ' -f9 | tr -d ','" title 'Uz' with lines,\
"< cat log.pimpleFoam | grep 'Solving for k' | cut -d' ' -f9 | tr -d ','" title 'k' with lines,\
"< cat log.pimpleFoam | grep 'Solving for p' | cut -d' ' -f9 | sed -n '0~15p' | tr -d ','" title 'p' with lines
pause 20
reread
aeronerd is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unstabil Simulation with chtMultiRegionFoam mbay101 OpenFOAM Running, Solving & CFD 13 December 28, 2013 14:12
Micro Scale Pore, icoFoam gooya_kabir OpenFOAM Running, Solving & CFD 2 November 2, 2013 14:58
pimpleFoam: turbulence->correct(); is not executed when using residualControl hfs OpenFOAM Running, Solving & CFD 3 October 29, 2013 09:35
calculation stops after few time steps sivakumar OpenFOAM Running, Solving & CFD 7 March 17, 2013 07:37
Orifice Plate with a fully developed flow - Problems with convergence jonmec OpenFOAM Running, Solving & CFD 3 July 28, 2011 06:24


All times are GMT -4. The time now is 15:29.