CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Post-Processing (https://www.cfd-online.com/Forums/openfoam-post-processing/)
-   -   Here is a script for plotting force files in Gnuplot. (https://www.cfd-online.com/Forums/openfoam-post-processing/101915-here-script-plotting-force-files-gnuplot.html)

lordvon May 17, 2012 02:16

Here is a script for plotting force files in Gnuplot.
 
I did not find a script for plotting the force file outputs (specified in controlDict), so I made one and am sharing it.

It plots whatever forces.dat file is in the same directory as the script. There are some constants and stuff that are my own, and shouldnt be too hard to tweak for your own usage. The 'truncationFactor' cuts off the first (number of lines in forces.dat)/truncationFactor lines in the forces.dat file; this is useful for transient simulations where the first few data points may be unphysically high and mess up the plot scale.

You need to enter in by line what you want to plot in 'forcesProcess' following the command 'plot'. The numbers for each force and moment as variables are just for reference; they are the indexes of each of the force/moment pressure/viscous component as listed in order on each line in the forces.dat file.

Two files, forcesProcess and forcesPlot. Currently forces.dat needs to be in same directory (can easily change to your desired relative directory), and forcesPlot is called from the shell to generate the plot.

forcesPlot:
Code:

#!/bin/bash
truncationFactor=10
name='forces.dat'

y=$(wc $name | cut -d' ' -f3)
start=$[y/truncationFactor]
echo "Lines in force file: $y"
echo "Line to start plot: $start"
sed 's/[()]//g' < $name > processed.dat
sed -i "1,${start}d" processed.dat
gnuplot forcesProcess -

forcesProcess:
Code:

cellDepth=0.00515394
omega=2400*2*pi/60
time=1
xForcePressure=2
xForceViscous=xForcePressure+3
yForcePressure=3
xForceViscous=yForcePressure+3
zForcePressure=4
xForceViscous=zForcePressure+3
xMomentPressure=8
xMomentViscous=xMomentPressure+3
yMomentPressure=9
yMomentViscous=yMomentPressure+3
zMomentPressure=10
zMomentViscous=zMomentPressure+3

plot  "processed.dat" using 1:(($2+$5)/cellDepth) title 'Drag (Newtons)', \
      "processed.dat" using 1:(($3+$6)/cellDepth) title 'Thrust (Newtons)', \
      "processed.dat" using 1:(($10+$13)/cellDepth*omega) title 'Power (Watts)'


gabrielfelix August 18, 2021 13:43

I wrote a similar code for plotting residuals, forces or both

You just gotta execute them at terminal:
Code:

./plotResiduals
./plotForces
./plotRF

plotResiduals:
Code:

#!/bin/bash

# Code for plotting residuals

foamLog log.simpleFoam >/dev/null

gnuplot -persist > /dev/null 2>&1 << EOF
set logscale y
set title "Residual vs. Iteration"
set xlabel "Iteration"
set ylabel "Residual"
plot "logs/Ux_0" with lines,\
"logs/Uy_0" with lines,\
"logs/Uz_0" with lines,\
"logs/p_0" with lines

EOF

plotForces:
Code:

#!/bin/bash

foamLog log.simpleFoam >/dev/null

gnuplot -persist > /dev/null 2>&1 << EOF
set logscale x
set title "Forces vs. Iteration"
set xlabel "Iteration"
set ylabel "Forces"
plot '<sed -e "s/[(,)]//g" ./postProcessing/forces/0/force_0.dat' using 1:2 with lines title 'Fx [N]',\
'<sed -e "s/[(,)]//g" ./postProcessing/forces/0/force_0.dat' using 1:3 with lines title 'Fy [N]',\
'<sed -e "s/[(,)]//g" ./postProcessing/forces/0/force_0.dat' using 1:4 with lines title 'Fz [N]'

EOF

plotRF:
Code:

#!/bin/bash

# Code for plotting residuals and forces

foamLog log.simpleFoam >/dev/null

cat ./postProcessing/forces/0/force_0.dat | tr -d "("")" > ./forceClean.dat

gnuplot -persist > /dev/null 2>&1 << EOF

set terminal wxt size 500,600

set multiplot
set size 1,0.5
set origin 0.0,0.5;
set logscale y
set logscale x
set title "Residual vs. Iteration"
set xlabel "Iteration"
set ylabel "Residual";
plot "logs/Ux_0" with lines,\
"logs/Uy_0" with lines,\
"logs/Uz_0" with lines,\
"logs/p_0" with lines;


set origin 0.0,0.0;
unset logscale y
set title "Forces vs. Iteration"
set xlabel "Iteration"
set ylabel "Forces";
plot '<sed -e "s/[(,)]//g" ./postProcessing/forces/0/force_0.dat' using 1:2 with lines title 'Fx [N]',\
'<sed -e "s/[(,)]//g" ./postProcessing/forces/0/force_0.dat' using 1:3 with lines title 'Fy [N]',\
'<sed -e "s/[(,)]//g" ./postProcessing/forces/0/force_0.dat' using 1:4 with lines title 'Fz [N]'
unset multiplot

EOF



All times are GMT -4. The time now is 14:40.