#!/bin/bash


a=1
b=1
c=2
#grid=FOR011
#sol=FOR020
grid=run.cgd
sol=run.cfl

###############
# Skin Friction
###############
if [ $a == $b ]; then
echo "
clear all

grid $grid
sol $sol
units si

zone 1
surface u 4
cut at z 0


variable x mm
variable Cfx

genplot output xCf
exit
" > xCf.jou

cfpost_prod.exe < xCf.jou

#Creating .dat files
in1=xCf.gen
out1=xCf$1.dat

sed -e '1,7d
457,682d' $in1 > $out1

rm -f xCf.jou

fi

###########
# Velocity
###########
if [ $a == $c ]; then
echo "
clear all
grid $grid
solution $sol

units si

zone 1
surface u 1
surface u 2
cut at x 0.376

variable y mm
variable u m sec

genplot output yu376

clear all
grid $grid
solution $sol

units si

zone 1
surface u 1
surface u 2
cut at x 0.396

variable y mm
variable u m sec

genplot output yu396

clear all
grid $grid
solution $sol

units si

zone 1
surface u 1
surface u 2
cut at x 0.426

variable y mm
variable u m sec

genplot output yu426

clear all
grid $grid
solution $sol

units si

zone 1
surface u 1
surface u 2
cut at x 0.449

variable y mm
variable u m sec

genplot output yu449
exit
" > velocity.jou

cfpost_prod.exe < velocity.jou

grep -v -e 'Exported' -e 'Strong' -e 'y' -e 'u' -e '^ *[0-9]* *$' yu376.gen > i376.dat
grep -v -e 'Exported' -e 'Strong' -e 'y' -e 'u' -e '^ *[0-9]* *$' yu396.gen > i396.dat
grep -v -e 'Exported' -e 'Strong' -e 'y' -e 'u' -e '^ *[0-9]* *$' yu426.gen > i426.dat
grep -v -e 'Exported' -e 'Strong' -e 'y' -e 'u' -e '^ *[0-9]* *$' yu449.gen > i449.dat

mv *.gen i3* i4* results/
rm velocity.jou
fi


###########
# Residuals
###########
#Extract max residuals of N-S equations for all zones
if [ $a == $b ]; then
for ((nzone=1; nzone<=1; nzone++))
do
  grep -a RES run.lis | awk '{ print $2 "    " $4 "    " $8 }' | grep "   $nzone   " | awk '{ print $3 }' > nsl2$nzone.dat
  grep -a *S-A run.lis | awk '{ print $2 "    " $4 "    " $8 }' | grep "   $nzone   " | awk '{ print $3 }' > sal2$nzone.dat
  grep -a RES run.lis | awk '{ print $2 "    " $4 "    " $9 }' | grep "   $nzone   " | awk '{ print $3 }' > cfl$nzone.dat
done

mv nsl2* sal2* cfl* xCf* results/
rm cfpost.jou

fi 
