#!/bin/bash

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

gnuplot<<EOF
    #---Parameters---
    R1 = 0.04

    rpm = 60
    Omega = rpm*2*pi/60

    tEnd = '0.25'

    degToRad(deg) = 3.14159265358979/180*deg

    Ur(  Ux, Uy, phi) =  Ux*cos(phi) + Uy*sin(phi)
    Uphi(Ux, Uy, phi) = -Ux*sin(phi) + Uy*cos(phi)

    Ur225(  Ux, Uy) = Ur(  Ux, Uy, degToRad(22.5))
    Uphi225(Ux, Uy) = Uphi(Ux, Uy, degToRad(22.5))

    Urm225(  Ux, Uy) = Ur(  Ux, Uy, degToRad(-22.5))
    Uphim225(Ux, Uy) = Uphi(Ux, Uy, degToRad(-22.5))

    #---Options---
    set terminal pngcairo enhanced
    set grid
    set key outside below

    TITLE = 'Mixer Vessel 2D, MRF, 100 RPM'

    #===VELOCITY=PROFILE===
    set output 'r_vPhi.png'
    set title TITLE.', t = '.tEnd

    path(name,time) = '../postProcessing/graph'.name.'/'.time.'/line.xy'

    set xlabel 'r'
    set xrange [0.02:0.1]
    set ylabel 'u_{/Symbol F}

    plot path('_blade',tEnd)               using 1:3                   with lines                title 'Blade, Phase 1', \
         path('_blade',tEnd)               using 1:6                   with lines                title 'Blade, Phase 2', \
         path('_22p5DegAheadOfBlade',tEnd) using 1:(Uphi225(\$2,\$3))  with lines                title '22.5 Deg Ahead of Blade, Phase 1', \
         path('_22p5DegAheadOfBlade',tEnd) using 1:(Uphi225(\$5,\$6))  with lines                title '22.5 Deg Ahead of Blade, Phase 2', \
         path('_22p5DegBehindBlade',tEnd)  using 1:(Uphim225(\$2,\$3)) with lines                title '22.5 Deg Behind Blade, Phase 1', \
         path('_22p5DegBehindBlade',tEnd)  using 1:(Uphim225(\$5,\$6)) with lines                title '22.5 Deg Behind Blade, Phase 2', \
         [0.02:0.05] (Omega*x)                                         with lines lc 8 lw 2 dt 2 title 'Solid Body Rotation'

    set output 'r_vr.png'

    set ylabel 'u_r'

    plot path('_blade',tEnd)               using 1:2                 with lines title 'Blade, Phase 1', \
         path('_blade',tEnd)               using 1:5                 with lines title 'Blade, Phase 2', \
         path('_22p5DegAheadOfBlade',tEnd) using 1:(Ur225(\$2,\$3))  with lines title '22.5 Deg Ahead of Blade, Phase 1', \
         path('_22p5DegAheadOfBlade',tEnd) using 1:(Ur225(\$5,\$6))  with lines title '22.5 Deg Ahead of Blade, Phase 2', \
         path('_22p5DegBehindBlade',tEnd)  using 1:(Urm225(\$2,\$3)) with lines title '22.5 Deg Behind Blade, Phase 1', \
         path('_22p5DegBehindBlade',tEnd)  using 1:(Urm225(\$5,\$6)) with lines title '22.5 Deg Behind Blade, Phase 2'

    unset xrange


    #===PROBES===
    set output 'probe_U.png'
    set title TITLE

    PATH = '../postProcessing/probes/0/'

    set xlabel 't'
    set ylabel 'U_{/Symbol F}'
    set xrange [1e-5:]
    set format x '%g'

    plot PATH.'U.phase1' using 1:6                  '%lf (%lf %lf %lf) (%lf %lf %lf)' with lines title 'Blade Tip, Phase 1', \
         PATH.'U.phase2' using 1:6                  '%lf (%lf %lf %lf) (%lf %lf %lf)' with lines title 'Blade Tip, Phase 2', \
         PATH.'U.phase1' using 1:(Uphi225(\$5,\$6)) '%lf (%lf %lf %lf) (%lf %lf %lf)' with lines title '22.5 Deg Ahead of Blade, Phase 1', \
         PATH.'U.phase2' using 1:(Uphi225(\$5,\$6)) '%lf (%lf %lf %lf) (%lf %lf %lf)' with lines title '22.5 Deg Ahead of Blade, Phase 2'

    unset xrange


    #===RESIDUALS===
    set output 'residuals-p_rgh.png'

    file = '../postProcessing/residuals/0/residuals.dat'

    set ylabel 'res(p\_rgh)'
    set logscale y
    set format y '10^{%T}'

    plot file using 1:2 with lines title 'current'

EOF

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