#!/bin/bash

if ! which gnuplot > /dev/null 2>&1
then
    echo 'gnuplot not found - skipping graph creation' >&2
    exit 1
fi

gnuplot<<EOF
    set linetype 1 lc rgb "blue"   lw 3 dt 1
    set linetype 2 lc rgb "orange" lw 3 dt 1
    set linetype 3 lc rgb "red"    lw 3 dt 1
    set linetype 4 lc rgb "black"  lw 3 dt 2
    set linetype 5 lc rgb "black"  lw 3 dt 3
    set linetype 6 lc rgb "black"  lw 3 dt 4

    set grid
    set key outside right

    set term pngcairo size 1000,1000

    #---graph---
    LIST =      system("ls -1 ../postProcessing/graph/ | sort -g")
    NUM =       words(LIST)
    TIMES(n) =  word(LIST,n)
    FILE(n) =  "../postProcessing/graph/".TIMES(n)."/line.csv"

    do for [i=1:NUM] {

        print(FILE(i))

        set output  "alphaU".sprintf("%04i", i+0).'.png'

        set multiplot layout 3,1 margins 0.09,0.82,.07,.96 spacing 0,0.04 title "t = ".sprintf("%1.3f", TIMES(i)+0)." s"

        unset xlabel

        set ylabel "r_{/Symbol a} [-]"
        set yrange [-0.05:1.05]
        r1 = "alpha.waterC"
        r2 = "alpha.airC"
        r3 = "alpha.waterD"
        r4 = "alpha.waterCaccum"
        plot FILE(i) u 1:(column(r1)*0                    ):(column(r1)                                 ) w filledcu t "water"    , \
             FILE(i) u 1:(column(r1)                      ):(column(r1)+column(r2)                      ) w filledcu t "air"      , \
             FILE(i) u 1:(column(r1)+column(r2)           ):(column(r1)+column(r2)+column(r3)           ) w filledcu t "droplets" , \
             FILE(i) u 1:(column(r1)+column(r2)+column(r3)):(column(r1)+column(r2)+column(r3)+column(r4)) w filledcu t "accum. water"

        set ylabel "r_{/Symbol a} [-]"
        set yrange [-0.05:1.05]
        plot FILE(i) u 1:(column("alpha.waterC"     )) w histeps lt 1 t "water"    , \
             FILE(i) u 1:(column("alpha.airC"       )) w histeps lt 2 t "air"      , \
             FILE(i) u 1:(column("alpha.waterD"     )) w histeps lt 3 t "droplets" , \
             FILE(i) u 1:(column("alpha.waterCaccum")) w histeps lt 4 t "accum. water"

        set xlabel  "y [m]"
        set ylabel "U_{/Symbol a} [m/s]"
        set yrange [-10.05:10.05]
        plot FILE(i) u 1:(column("U.waterC_y"     )) w histeps lt 1 t "water"    , \
             FILE(i) u 1:(column("U.airC_y"       )) w histeps lt 2 t "air"      , \
             FILE(i) u 1:(column("U.waterD_y"     )) w histeps lt 3 t "droplets" , \
             FILE(i) u 1:(column("U.waterCaccum_y")) w histeps lt 4 t "accum. water"

        unset multiplot
    }

    #---comparison to reference---
    FILERES=FILE(NUM)
    FILEREF="./line.csv"

    set output  "alphaUReference.png"

    set multiplot layout 2,1 margins 0.09,0.82,.07,.96 spacing 0,0.04 title "last vs. reference"

    unset xlabel

    set ylabel "r_{/Symbol a} [-]"
    set yrange [-0.05:1.05]
    plot FILERES u 1:(column("alpha.waterCaccum")) w histeps lt 1 t "accum. water" , \
         FILERES u 1:(column("alpha.airC"       )) w histeps lt 2 t "air"          , \
         FILERES u 1:(column("alpha.waterD"     )) w histeps lt 3 t "droplets"     , \
         FILEREF u 1:(column("alpha.waterCaccum")) w histeps lt 4 t "ref water"    , \
         FILEREF u 1:(column("alpha.airC"       )) w histeps lt 5 t "ref air"      , \
         FILEREF u 1:(column("alpha.waterD"     )) w histeps lt 6 t "ref droplets"

    set xlabel  "y [m]"
    set ylabel "U_{/Symbol a} [m/s]"
    set yrange [-10.05:10.05]
    plot FILERES u 1:(column("U.waterCaccum_y")) w histeps lt 1 t "accum. water"     , \
         FILERES u 1:(column("U.airC_y"       )) w histeps lt 2 t "air"              , \
         FILERES u 1:(column("U.waterD_y"     )) w histeps lt 3 t "droplets"         , \
         FILEREF u 1:(column("U.waterCaccum_y")) w histeps lt 4 t "ref accum. water" , \
         FILEREF u 1:(column("U.airC_y"       )) w histeps lt 5 t "ref air"          , \
         FILEREF u 1:(column("U.waterD_y"     )) w histeps lt 6 t "ref droplets"

    unset multiplot
    set key inside auto

    #---water-volume---
    FILE="../postProcessing/volumeWater/0/volFieldValue.dat"

    system("sed -i 's/^# Time/Time/' ".FILE)

    set output  "volumeWater.png"

    set term pngcairo size 800,600
    set grid

    epsilon = 50

    rho = 997

    Lx = 0.1
    Lz = 0.1

    initialDelta = 0.275
    initialPoolHeight = 0.1

    entrainmentVolumeRate = epsilon*Lx*Lz/rho
    initialVolumeTop      = initialDelta*Lx*Lz      # volume to transfer
    initialVolumeBottom   = initialPoolHeight*Lx*Lz # reservoir

    set xlabel "time [s]"
    set ylabel "volume [m^3]"
    set yrange [0:1.2*initialVolumeTop]

    plot FILE u 1:(column('volIntegrate(alpha.waterC)')) w l lt 1 t 'water volume to transfer', \
         FILE u 1:(column('volIntegrate(alpha.waterD)')+column('volIntegrate(alpha.waterCaccum)')-initialVolumeBottom) w l lt 3 t 'droplets', \
         entrainmentVolumeRate*x lt 4 title 'constant rate'

    #---water-volume-error---
    set output  "volumeWaterError.png"

    set ylabel "volume error [%]"
    set yrange [-1:1]

    plot FILE u 1:(((column('volIntegrate(alpha.waterC)')+column('volIntegrate(alpha.waterD)')+column('volIntegrate(alpha.waterCaccum)'))/(initialVolumeTop+initialVolumeBottom)-1)*100) w l lt 1 t 'water'

    print 'Finished'

EOF

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