#!/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 2 dt 1
set linetype 2 lc rgb 'blue'    lw 2 dt 1
set linetype 3 lc rgb 'green'   lw 2 dt 1
set linetype 4 lc rgb 'orange'  lw 2 dt 1
set linetype 5 lc rgb 'black'   lw 2 dt 2
set linetype 6 lc rgb 'black'   lw 3 dt 3
set linetype 7 lc rgb 'black'   lw 2 dt 4
set linetype 8 lc rgb 'black'   lw 2 dt 5

set grid
set key top right

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

#---Dimensions---
L = 1
g = 0.00919
Ug = sqrt(L*g)
T = L/Ug
R = 0.5
k0 = 0.5*Ug**2

print 'Ug = ', Ug, ' m s^-1'
print 'T = ', T, ' s'

TIMES =     '0'
NUM =       words(TIMES)
TIME(n) =   word(TIMES, n)

PATH =      '../postProcessing/'
RPATH =     'reference/'

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


#---PID-Controller---
ZFILE(n) =  'bubbleProperties/'.TIME(n).'/bubbleProperties.dat'
UFILE(n) =  'inletVelocity/'.TIME(n).'/surfaceFieldValue.dat'

set xlabel  't'
set ylabel  'control variable: z_b'
set ytics   nomirror
set y2label 'controlled variable: U_z'
set y2tics
set xzeroaxis lt 2 lw 2 dt 3

set output '../postProcessing/report/PIDcontroller_z'.FType
set title   'PID variables'

plot for [i=1:NUM]  PATH.ZFILE(i) u 1:4                               w l lt 1 t 'z_b - current    ', \
                   RPATH.ZFILE(1) u 1:4                               w l lt 5 t 'z_b - reference'  , \
     for [i=1:NUM]  PATH.UFILE(i) u 1:4 '%lf (%lf %lf %lf)' axes x1y2 w l lt 2 t 'U_z - current    ', \
                   RPATH.UFILE(1) u 1:4 '%lf (%lf %lf %lf)' axes x1y2 w l lt 6 t 'U_z - reference'

unset y2label
unset y2tics
set ytics


#---Radial-Profiles-Centre---
FILE      = 'radialVelocityProfile_surfaceCentre.dat'

set output '../postProcessing/report/radialProfile_U'.FType
set title   'Radial Profile of Velocity at Centre of Bubble'
set xlabel  'r/D_b'
set ylabel  '<U>/U_b'
set arrow from 0.5, graph 0 to 0.5, graph 1 nohead

plot         PATH.FILE u 1:(\$4/Ug) w l lt 1 t "<U_z> - current    "            , \
            RPATH.FILE u 1:(\$4/Ug) w l lt 5 t "<U_z> - reference"              , \
             PATH.FILE u 1:(\$2/Ug) w l lt 2 t "<U_r> - current    "            , \
            RPATH.FILE u 1:(\$2/Ug) w l lt 6 t "<U_r> - reference"              , \
             PATH.FILE u 1:(\$3/Ug) w l lt 3 t "<U_{/Symbol f}> - current    "  , \
            RPATH.FILE u 1:(\$3/Ug) w l lt 7 t "<U_{/Symbol f}> - reference"

set output '../postProcessing/report/radialProfile_UFluc_centre'.FType
set title   'Radial Profile of Velocity Fluctuation at Centre of Bubble'
set ylabel "<U_i^' U_j^'>/U_b^2"
set xrange  [0:1.2]
plot         PATH.FILE u 1:(\$7/Ug**2) w l lt 1 t "<U_z'U_z'> - current    ", \
            RPATH.FILE u 1:(\$7/Ug**2) w l lt 5 t "<U_z'U_z'> - reference", \
             PATH.FILE u 1:(\$5/Ug**2) w l lt 2 t "<U_r'U_r'> - current    ", \
            RPATH.FILE u 1:(\$5/Ug**2) w l lt 6 t "<U_r'U_r'> - reference", \
             PATH.FILE u 1:(\$6/Ug**2) w l lt 3 t "<U_{/Symbol f}'U_{/Symbol f}'> - current    ", \
            RPATH.FILE u 1:(\$6/Ug**2) w l lt 7 t "<U_{/Symbol f}'U_{/Symbol f}'> - reference", \
             PATH.FILE u 1:(\$9/Ug**2) w l lt 4 t "<U_r'U_z'> - current    ", \
            RPATH.FILE u 1:(\$9/Ug**2) w l lt 8 t "<U_r'U_z'> - reference"

set output '../postProcessing/report/radialProfile_K_centre'.FType
set title   'Radial Profile of Turbulent Kinetic Energy at Centre of Bubble'
set ylabel  '<k>/(0.5 U_b^2)'
plot         PATH.FILE u 1:(\$11/k0) w l lt 1 t "current"   , \
            RPATH.FILE u 1:(\$11/k0) w l lt 5 t "reference"

unset arrow
unset xzeroaxis
unset title


#---3D-Trajectory---
FILE =  '3Dtrajectory.dat'

set output '../postProcessing/report/trajectory3D'.FType
set grid
set view equal xy
set view 70,30,1.3,1

set xtics 1  offset 0,-0.2
set ytics 1  offset 0.9,-0.2
set ztics 10 offset 0.8
set ticslevel 0

set xlabel 'x' offset 0,-0.5
set ylabel 'y' offset 1.5,-1
set zlabel 'z' offset 2

set xrange [-2:2]
set yrange [-2:2]

splot  PATH.FILE u 2:3:4       w l lt 1        t "current"  , \
       PATH.FILE u 2:3:(\$4*0) w l lt 1 lw 0.1 notitle      , \
      RPATH.FILE u 2:3:4       w l lt 5        t "reference", \
      RPATH.FILE u 2:3:(\$4*0) w l lt 5 lw 0.1 notitle

EOF

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