#!/bin/bash
cd "${0%/*}" || exit                        # Run from this directory

timeOpts="-latestTime"

[ -d "processor0" ] && timeOpts="$timeOpts -processor"

time="$(foamListTimes $timeOpts)"

echo "Creating plots for time $time"

resultsDir="postProcessing/sample.lines/$time"
resultsDirCp="postProcessing/sample.aerofoil/$time"

# Mapping between OpenFOAM and experimental datasets
declare -A of_vs_exp
of_vs_exp[0.68]="001"
of_vs_exp[0.73]="002"
of_vs_exp[0.79]="003"
of_vs_exp[0.84]="004"
of_vs_exp[0.90]="005"
of_vs_exp[0.95]="006"


plotLines()
{
    pos=$1
    exp=$2

cat<<EOF
    set terminal pngcairo enhanced color font "arial,16" size 600,600
    set ylabel "(y-y_0)/c"
    set yrange [0:0.2]
    set grid

    U0=1
    c=1
    normCFD = 0.93

    m  = 0.01*4
    p  = 0.1*4
    t  = 0.01*12
    yc = (m/(1.0-p)**2)*((1.0-2.0*p) + 2.0*p*${pos} - ${pos}**2)
    dzcdx = (2.0*m/((1.0-p)**2))*(p - ${pos})
    theta = atan(dzcdx);
    ys = 5.0*t*(0.2969*sqrt(${pos}) - (0.1260*${pos}) - (0.3516*(${pos}**2)) + (0.2843*(${pos}**3)) - (0.1036*(${pos}**4)))
    y0 = yc + ys*cos(theta)

    colExp="black"
    colOF="royalblue"
    colCFL3D="light-red"

    set key left top
    set xlabel "U_x/U_0"
    set xrange [-0.2:1.4]
    set output "fig_Ux_at_xbyc${pos}.png"
    plot \
        "validation/exptData/exp_vel_xbyc${pos}.dat" \
	 u 3:(\$2-y0) every 2 w points pt 7 ps 1.5 lc rgb colExp lw 2 t "Experiment", \
        "$resultsDir/xbyc${pos}_columnAverage(UMean)_columnAverage(UPrime2Mean).xy" \
	 u (\$2/U0/normCFD):((\$1-y0)/c) w lines lc rgb colOF lw 2 t "OpenFOAM", \
        "validation/cfdData/cfl3d_vel_sa_xbyc${pos}.dat" \
	 u 3:(\$2-y0) w lines lc rgb colCFL3D lw 2 t "CFL3D"

    set key left top
    set xlabel "U_y/U_0"
    set xrange [-0.2:0.1]
    set output "fig_Uy_at_xbyc${pos}.png"
    plot \
        "validation/exptData/exp_vel_xbyc${pos}.dat" \
	 u 4:(\$2-y0) every 2 w points pt 7 ps 1.5 lc rgb colExp lw 2 t "Experiment", \
        "$resultsDir/xbyc${pos}_columnAverage(UMean)_columnAverage(UPrime2Mean).xy" \
	u (\$4/U0/normCFD):((\$1-y0)/c) w lines lc rgb colOF lw 2 t "OpenFOAM", \
        "validation/cfdData/cfl3d_vel_sa_xbyc${pos}.dat" \
	 u 4:(\$2-y0) w lines lc rgb colCFL3D lw 2 t "CFL3D"
EOF
}


plotCp()
{
cat<<EOF
    set terminal pngcairo enhanced color font "arial,16" size 600,600
    set ylabel "C_p"
    set yrange [2:-8]
    set grid

    U0=1
    c=1
    p0=0

    colExp="black"
    colOF="royalblue"
    colCFL3D="light-red"

    set key right top
    set xlabel "x/c"
    set xrange [0:1]
    set output "fig_Cp_at_aerofoil.png"
    plot \
        "validation/exptData/exp_cp.dat" \
         u 1:3 w points pt 7 ps 1.5 lc rgb colExp lw 2 t "Experiment", \
        "$resultsDirCp/pMean_aerofoil.raw" \
	 u (\$1/c):(2.0*(\$4-p0)/U0/U0) w points pt 64 ps 1 lc rgb colOF lw 2 t "OpenFOAM", \
        "validation/cfdData/cfl3d_cp_sa.dat" \
         u 1:2 w lines lc rgb colCFL3D lw 2 t "CFL3D"
EOF
}


plotCf()
{
cat<<EOF
    set terminal pngcairo enhanced color font "arial,16" size 600,600
    set ylabel "C_f"
    set yrange [-0.01:0.06]
    set grid

    U0=1
    c=1

    colOF="royalblue"
    colCFL3D="light-red"

    set key right top
    set xlabel "x/c"
    set xrange [0:1]
    set output "fig_Cf_at_aerofoil.png"
    plot \
        "$resultsDirCp/wallShearStressMean_aerofoil.raw" \
	 u (\$1/c):(\$3 > 0 ? (\$1 > 0.4 ? 2.0*sgn(\$6)*((\$4**2+\$5**2+\$6**2)**0.5)/U0/U0 : 2.0*((\$4**2+\$5**2+\$6**2)**0.5)/U0/U0) : 1/0) w points pt 64 ps 1 lc rgb colOF lw 2 t "OpenFOAM", \
        "validation/cfdData/cfl3d_cfupper_sa.dat" \
	 u 1:2 w lines lc rgb colCFL3D lw 2 t "CFL3D"
EOF
}


for i in "${!of_vs_exp[@]}"
do
    exp=${of_vs_exp[$i]}
    gnuplot<<<$(plotLines $i $exp)
done

gnuplot<<<$(plotCp)
gnuplot<<<$(plotCf)

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