#!/bin/bash
cd "${0%/*}" || exit                                # Run from this directory
. ${WM_PROJECT_DIR:?}/bin/tools/RunFunctions        # Tutorial run functions
#------------------------------------------------------------------------------

# Turbulence closure models
models=('kOmegaSST' 'SpalartAllmaras' 'kEpsilonPhitF')

# Note: CFL3D data available from:
# https://turbmodels.larc.nasa.gov/bump_sa.html
# The CFL3D-SpalartAllmaras datasets of Cf and Cp:
# Cf = https://turbmodels.larc.nasa.gov/Bump/SA/cf_bump.dat
# Cp = https://turbmodels.larc.nasa.gov/Bump/SA/cp_bump.dat

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

plotCf() {
    declare -a resultSet=("${!1}")
    declare -a modelSet=("${!2}")

    graphNameCf="hill2D_cf.png"
    echo "Creating skin friction coefficient graph to $graphNameCf"
    gnuplot<<PLT_CF
    set terminal pngcairo font "helvetica,20" size 1000, 1000
    set xrange [0:1.5]
    set yrange [0:0.008]
    set grid
    set key bottom right
    set xlabel "x"
    set ylabel "C_f" rotate by 0
    set output "$graphNameCf"

    results="${resultSet[*]}"
    models="${modelSet[*]}"
    Uref = 69.44

    set lmargin 10
    set rmargin 1.5
    set bmargin 3.2

#    plot \
#        "cf_bump_cfl3d_sa.dat" every 10 u 1:2 t "CFL3D" \
#        w p ps 3 pt 6 lw 2 lc rgb "red", \
#        for [i=1:words(results)] word(results, i) \
#        u 1:(sqrt(\$2*\$2+\$3*\$3+\$4*\$4)/(0.5*Uref*Uref)) \
#        t word(models, i) with lines

    plot \
        for [i=1:words(results)] word(results, i) \
        u 1:(sqrt(\$2*\$2+\$3*\$3+\$4*\$4)/(0.5*Uref*Uref)) \
        t word(models, i) with lines

PLT_CF
}


plotCp() {
    declare -a resultSet=("${!1}")
    declare -a modelSet=("${!2}")

    graphNameCp="hill2D_cp.png"
    echo "Creating pressure coefficient graph to $graphNameCp"
    gnuplot<<PLT_CP
    set terminal pngcairo font "helvetica,20" size 1000, 1000
    set xrange [0:1.5]
    set yrange [0.4:-0.8]
    set grid
    set key bottom right
    set xlabel "x"
    set ylabel "C_p" rotate by 0
    set output "$graphNameCp"

    results="${resultSet[@]}"
    models="${modelSet[@]}"
    Uref = 69.44

    set lmargin 10
    set rmargin 1.5
    set bmargin 3.2

#    plot \
#        "cp_bump_cfl3d_sa.dat" every 10 u 1:2 t "CFL3D" \
#        w p ps 3 pt 6 lw 2 lc rgb "red", \
#        for [i=1:words(results)] word(results, i) \
#        u (\$1):(\$5) t word(models, i) with lines

    plot \
        for [i=1:words(results)] word(results, i) \
        u (\$1):(\$5) t word(models, i) with lines

PLT_CP
}


if notTest $@
then
    # Create validation plots

    # Require gnuplot
    command -v gnuplot >/dev/null || {
        echo "gnuplot not found - skipping graph creation" 1>&2
        exit 1
    }

    # Require awk
    command -v awk >/dev/null || {
        echo "awk not found - skipping graph creation" 1>&2
        exit 1
    }

    modelResults=()
    n=0
    for model in "${models[@]}"
    do
        modelResults[$n]="${model}/profiles.dat"
        n=$(($n+1))
    done

    plotCp modelResults[@] models[@]
    plotCf modelResults[@] models[@]
fi

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