#!/bin/sh
cd "${0%/*}" || exit                        # Run from this directory
#------------------------------------------------------------------------------

plotStresses() {
    timeDir=$1
    \echo "    Plotting the normal and Reynolds stresses"

    gnuplot<<PLT_STRESSES
    set terminal pngcairo font "helvetica,20" size 1000, 800
    set xrange [0:1]
    set yrange [-1:8]
    set grid
    set key top right
    set xlabel "Channel height from the bottomWall [m]"
    set ylabel "<u_i^' u_i^'>"
    set offset .05, .05

    set style data linespoints
    set linetype 1 lc rgb 'black' lw 2
    set linetype 2 lc rgb 'red' lw 2
    set linetype 3 lc rgb 'blue' lw 2
    set linetype 4 lc rgb 'green' lw 2
    set linetype 5 lc rgb 'black' pi -8 pt 4 ps 1.5
    set linetype 6 lc rgb 'red' pi -8 pt 4 ps 1.5
    set linetype 7 lc rgb 'blue' pi -8 pt 4 ps 1.5
    set linetype 8 lc rgb 'green' pi -8 pt 4 ps 1.5

    set title "Normal and Reynolds stresses on cell"
    input="$timeDir/inletCell_UPrime2Mean.xy"
    set output 'stress-cell.png'
    plot \
        input u 1:2 w lines t "<u^' u^'>" lt 1, \
        input u 1:5 w lines t "<v^' v^'>" lt 2, \
        input u 1:7 w lines t "<w^' w^'>" lt 3, \
        input u 1:3 w lines t "<u^' v^'>" lt 4

    set title "Normal and Reynolds stresses on patch"
    input = "$timeDir/inletPatch_UPrime2Mean.xy"
    set output 'stress-patch.png'
    plot \
        input u 1:2 w lines t "<u^' u^'>" lt 1, \
        input u 1:5 w lines t "<v^' v^'>" lt 2, \
        input u 1:7 w lines t "<w^' w^'>" lt 3, \
        input u 1:3 w lines t "<u^' v^'>" lt 4

PLT_STRESSES
}


#------------------------------------------------------------------------------

# Require gnuplot
command -v gnuplot >/dev/null || {
    \echo "gnuplot not found - skipping graph creation" 1>&2
    \exit 1
}


# The latestTime in postProcessing/inletSampling
timeDir=$(foamListTimes -case postProcessing/inletSampling -latestTime 2>/dev/null)

[ -n "$timeDir" ] || {
    \echo "No postProcessing/inletSampling found - skipping graph creation" 1>&2
    \exit 2
}

timeDir="postProcessing/inletSampling/$timeDir"

plotStresses "$timeDir"


#------------------------------------------------------------------------------
