#!/bin/bash


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

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

grid $grid
sol $sol

zone 1
surface u 3
cut at z 1.15

variable x in
variable p lb in

genplot output xp
exit
" > xp.jou

cfpost_prod.exe < xp.jou

#Creating .dat files - bottom
in1=xp.gen
out1=xpi$1.dat

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

in2=xpi.dat
out2=xp$1.dat

sed -e '1i\ TITLE     = "Prandtl-Meyer Centerline Data"
1i\ VARIABLES = "x [in]"
1i\ "p [lb/in2]"
1i\ ZONE T="Case 4", F=POINT' $in2 > $out2

rm -f xp.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' yu376.gen > i376.dat
grep -v -e 'Exported' -e 'Strong' -e 'y' -e 'u' yu396.gen > i396.dat
grep -v -e 'Exported' -e 'Strong' -e 'y' -e 'u' yu426.gen > i426.dat
grep -v -e 'Exported' -e 'Strong' -e 'y' -e 'u' 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 pm15.lis | awk '{ print $2 "    " $4 "    " $8 }' | grep "   $nzone   " | awk '{ print $3 }' > nsl2$nzone.dat
  grep -a RES pm15.lis | awk '{ print $2 "    " $4 "    " $9 }' | grep "   $nzone   " | awk '{ print $3 }' > cfl$nzone.dat
done

mv nsl2* cfl* xp* results/
rm cfpost.jou

fi 
