#!/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 -case ..")
CASE = system("foamDictionary ../caseParameterDict -entry case -value")

set term pngcairo
set grid
set key box
set xrange [-1: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 void fraction plot at z = 0.155 m------------------*/

if (CASE ne "S3" && CASE ne "S4") {
set title 'z = 0.155 m'
set output '../postProcessing/report/alpha0155.png'
set xlabel 'r/R (-)'
set ylabel '{/Symbol a}_G (-)'

plot  'reference/'.CASE.'/RadialLine0155/line.csv' u (\$1*10):2 t 'Reference' w line ls 3, \
      '../postProcessing/RadialLine0155/'.TIME.'/line.csv' u (\$1*10):2 t 'Simulation' w lines ls 1,\
      'experiment/'.CASE.'/0155/alpha.dat' u (\$1*10):2 t 'Experiment' w points ls 2}

#\*-----------------Radial void fraction plot at z = 0.65 m-------------------*/

if (CASE ne "S3" && CASE ne "S4") {
set title 'z = 0.65 m'
set output '../postProcessing/report/alpha065.png'
set xlabel 'r/R (-)'
set ylabel '{/Symbol a}_G (-)'

plot  'reference/'.CASE.'/RadialLine065/line.csv' u (\$1*10):2 t 'Reference' w line ls 3, \
      '../postProcessing/RadialLine065/'.TIME.'/line.csv' u (\$1*10):2 t 'Simulation' w lines ls 1,\
      'experiment/'.CASE.'/065/alpha.dat' u (\$1*10):2 t 'Experiment' w points ls 2}

#\*-----------------Radial solid fraction plot at z = 0.35 m------------------*/

if (CASE ne "S2" && CASE ne "S4") {
set title 'z = 0.35 m'
set output '../postProcessing/report/alpha035.png'
set xlabel 'r/R (-)'
set ylabel '{/Symbol a}_S (-)'

plot  'reference/'.CASE.'/RadialLine035/line.csv' u (\$1*10):2 t 'Reference' w line ls 3, \
      '../postProcessing/RadialLine035/'.TIME.'/line.csv' u (\$1*10):2 t 'Simulation' w lines ls 1,\
      'experiment/'.CASE.'/035/alpha.dat' u (\$1*10):2 t 'Experiment' w points ls 2}

#\*-----------------Radial solid fraction plot at z = 0.70 m------------------*/

if (CASE ne "S2" && CASE ne "S4") {
set title 'z = 0.7 m'
set output '../postProcessing/report/alpha070.png'
set xlabel 'r/R (-)'
set ylabel '{/Symbol a}_S (-)'

plot  'reference/'.CASE.'/RadialLine070/line.csv' u (\$1*10):2 t 'Reference' w line ls 3, \
      '../postProcessing/RadialLine070/'.TIME.'/line.csv' u (\$1*10):2 t 'Simulation' w lines ls 1,\
      'experiment/'.CASE.'/070/alpha.dat' u (\$1*10):2 t 'Experiment' w points ls 2}

#\*------------------Axial solid fraction plot at r = 0.0 m-------------------*/

if (CASE ne "S2") {
set xrange [0:1]
set title 'r = 0.0 m'
set output '../postProcessing/report/alpha0.png'
set xlabel 'z/H (-)'
set ylabel '{/Symbol a}_S (-)'

plot  'reference/'.CASE.'/AxialLine0/line.csv' u 1:2 t 'Reference' w line ls 3, \
      '../postProcessing/AxialLine0/'.TIME.'/line.csv' u 1:2 t 'Simulation' w lines ls 1,\
      'experiment/'.CASE.'/0/alpha.dat' u 1:2 t 'Experiment' w points ls 2}

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 logscale y
set format y '%.0e'

set output '../postProcessing/report/residuals.png'

# plotting data
plot '< gzip -dc reference/'.CASE.'/residuals.dat.gz' u 1:2 w l ls 4 t 'Ref. p',\
     '< gzip -dc reference/'.CASE.'/residuals.dat.gz' u 1:3 w l ls 5 t 'Ref. k',\
     '< gzip -dc reference/'.CASE.'/residuals.dat.gz' 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

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