#!/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

#\*----------------------------General settings-------------------------------*/

TIME = system("foamListTimes -latestTime -functionEntries -case ..")
CASE = system("foamDictionary ../caseParameterDict -entry case -value")

set term pngcairo
set grid
set key left top box
set xlabel 'r/R'
set xrange [0:1]

set style line 1 dt 2 lt 1 lw 2 pt 2 lc "black"
set style line 2 lt 2 lw 5 pt 2 lc "red"
set style line 3 lt 3 lw 2 pt 2 lc "green"
set style line 4 lt 4 lw 2 pt 2 lc "violet"
set style line 5 lt 5 lw 2 pt 2 lc "cyan"

#\*-----------------------Radial solid fraction profile----------------------*/

set output '../alphaS.png'
set ylabel '{/Symbol a}_S'

plot 'reference/'.CASE.'/line.csv' u (\$1*80):3 t 'Reference' w lines ls 3, \
    '../postProcessing/sampleLine/'.TIME.'/line.csv' u (\$1*80):3 t 'Simulation' w lines ls 1

#\*-------------------Radial aggregate fraction profile----------------------*/

set output '../alphaA.png'
set ylabel '{/Symbol a}_A'

plot 'reference/'.CASE.'/line.csv' u (\$1*80):2 t 'Reference' w lines ls 3, \
    '../postProcessing/sampleLine/'.TIME.'/line.csv' u (\$1*80):2 t 'Simulation' w lines ls 1

#\*---------------------Radial surface loading profile-----------------------*/

set output '../BSL.png'
set ylabel 'BSL'

plot 'reference/'.CASE.'/line.csv' u (\$1*80):4 t 'Reference' w lines ls 3, \
    '../postProcessing/sampleLine/'.TIME.'/line.csv' u (\$1*80):4 t 'Simulation' w lines ls 1

#\*--------Axial profile of cross-sectionally averaged surface loading-------*/

set xlabel 'z/H'
set output '../AxialBSL.png'
set ylabel 'BSL'

plot 'reference/'.CASE.'/layerAverage.csv' u (\$1*0.5):2 t 'Reference' w lines ls 3, \
    '../postProcessing/layerAverage/'.TIME.'/layerAverage.csv' u (\$1*0.5):2 t 'Simulation' w lines ls 1

#\*-------Axial profile of cross-sectionally averaged aggregate density------*/

set output '../AxialrhoA.png'
set ylabel '{/Symbol r}_A'

plot 'reference/'.CASE.'/layerAverage.csv' u (\$1*0.5):3 t 'Reference' w lines ls 3, \
    '../postProcessing/layerAverage/'.TIME.'/layerAverage.csv' u (\$1*0.5):3 t 'Simulation' w lines ls 1

#\*------Axial profile of cross-sectionally averaged aggregate fraction------*/

set output '../AxialalphaA.png'
set ylabel '{/Symbol a}_A'

plot 'reference/'.CASE.'/layerAverage.csv' u (\$1*0.5):4 t 'Reference' w lines ls 3, \
    '../postProcessing/layerAverage/'.TIME.'/layerAverage.csv' u (\$1*0.5):4 t 'Simulation' w lines ls 1

#\*--Axial profile of cross-sectionally averaged aggregate relative velocity-*/

set output '../AggVel.png'
set ylabel 'W_{rel} (m/s)'

plot 'reference/'.CASE.'/layerAverage.csv' u (\$1*0.5):(\$7-\$10) t 'Reference' w lines ls 3, \
    '../postProcessing/layerAverage/'.TIME.'/layerAverage.csv' u (\$1*0.5):(\$7-\$10) t 'Simulation' w lines ls 1

EOF

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

gnuplot<<EOF

CASE = system("foamDictionary ../caseParameterDict -entry case -value")

set term pngcairo

set style line 1 lw 2 lc "black"
set style line 2 lw 2 lc "red"
set style line 3 lw 2 lc "forest-green"
set style line 4 lw 2 lc "gray"
set style line 5 lw 2 lc "orange"
set style line 6 lw 2 lc "green"

set xlabel 't (s)'
set ylabel 'residuals'
set logscale y
set format y '%.0e'

set output '../residuals.png'

plot 'reference/'.CASE.'/residuals.dat' u 1:2 w l ls 4 t 'Ref. p-rgh',\
     'reference/'.CASE.'/residuals.dat' u 1:3 w l ls 5 t 'Ref. k',\
     'reference/'.CASE.'/residuals.dat' u 1:4 w l ls 6 t 'Ref. {/Symbol w}',\
     '../postProcessing/residuals/0/residuals.dat' u 1:2 w l ls 1 t 'p',\
     '../postProcessing/residuals/0/residuals.dat' u 1:3 w l ls 2 t 'k',\
     '../postProcessing/residuals/0/residuals.dat' u 1:4 w l ls 3 t '{/Symbol w}'

EOF

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