#!/bin/sh

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

time=$(foamListTimes -case .. -latestTime)

graphFile=../postProcessing/graph/$time/line.xy

gnuplot<<EOF
    set terminal postscript eps color enhanced font "Helvetica,20"
    set output '../species.eps'

    set xlabel 'l (m)'
    set ylabel 'Y (-)'
    set y2label '{/Symbol a} (-)'
    set ytics nomirror
    set y2tics nomirror
    set key center

    plot "$graphFile" u 1:2 w l t 'TiCl4',\
         "$graphFile" u 1:3 w l t 'O2',\
         "$graphFile" u 1:4 w l t 'Cl2',\
         "$graphFile" u 1:5 axis x1y2 w l t '{/Symbol a}_{particles}'
EOF

resultFile=../postProcessing/numberDensity/$time/numberDensity.xy

gnuplot<<EOF
    set terminal postscript eps color enhanced font "Helvetica,20"
    set output '../sizeDistribution.eps'

    set xlabel 'Mobility diameter ({/Symbol m}m)'
    set ylabel 'N_i/N_t/{/Symbol D}ln d'
    set y2label '{/Symbol k} (m^{-1})'
    set logscale xyy2
    set tics format '%1.e'
    set ytics nomirror
    set y2tics nomirror
    set xrange [1e-3:1]
    set yrange [1e-3:3]

    plot "$resultFile" u (\$1/1e-6):2 w lp t 'Size distribution',\
         "$resultFile" u (\$1/1e-6):(\$4/\$3) axis x1y2 w l t '{/Symbol k}',\
         "$resultFile" u (\$1/1e-6):(6./((6./pi*\$3)**(1./3.))) axis x1y2 w l t '{/Symbol k} lower bound'
EOF

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