#!/bin/bash

if ! command -v gnuplot > /dev/null 2>&1
then
    echo 'gnuplot not found - skipping graph creation' >&2
    exit 1
else
    mkdir -p ../postProcessing/report
fi

gnuplot<<EOF

set linetype 1 lc rgb "red"   lw 3 dt 1 pt 7
set linetype 2 lc rgb "black" lw 3 dt 2

set grid
set key bottom right

TIMES = system("foamListTimes -withZero -case ..")
TNUM = words(TIMES)
TIME(n) = word(TIMES,n)

PATH =      "../postProcessing/"
RPATH =     "reference/"


#---PNG---
set terminal pngcairo color enhanced size 800,600
FType = '.png'

#---residuals---
FILE =      "residuals/0/residuals.dat"

set output "../postProcessing/report/residuals".FType
set xlabel  "t"
set ylabel  "initial residuals p_{rgh}"
set logscale y
set format y "10^{%T}"
plot PATH.FILE u 1:2 w l lw 2 notitle

unset logscale y
set format y "%g"


#---bubbleProperties---
FILE =      "bubbleProperties/0/bubbleProperties.dat"

set output "../postProcessing/report/bubblePosition".FType
set xlabel  "t"
set ylabel  "CG.y"
plot  PATH.FILE u 1:3 w l lt 1 t "current"  , \
     RPATH.FILE u 1:3 w l lt 2 t "reference"

set output "../postProcessing/report/bubbleVelocity".FType
set ylabel  "U_b.y"
plot  PATH.FILE u 1:6 w l lt 1 t "current"  , \
     RPATH.FILE u 1:6 w l lt 2 t "reference"


#---PNG---
set terminal pngcairo color enhanced size 800,600

#---bubbleSurface---
set size ratio -1
set xlabel  "x"
set xtics   0.5
set ylabel  "y"
set ytics   0.5

do for [i=1:TNUM] {
    FNAME = sprintf("shape_%.2d", (TIME(i)+0))

    set output  FNAME.FType
    set title   "t = ".TIME(i)

    BFILE =     "bubbleSurface/".TIME(i)."/isoSurface.xy"

    plot PATH.BFILE u 1:2 w p ps 0.01 lc rgb "black" notitle
}

EOF

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