#!/bin/bash



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

###########
# Pressure
###########
echo "
clear all

grid $grid
sol $sol


zone 1
subset i all j last k 1

variable M
variable p lb in
variable T r
variable x in
variable y in


list output presst.lis raw lines 55555

clear all

grid $grid
sol $sol

zone 1
subset i all j 1 k 1

variable M
variable p lb in
variable T r
variable x in
variable y in


list output pressb.lis raw lines 55555
exit
" > press.jou

cfpost_prod.exe < press.jou


int=presst.lis
outt=presst.dat

inb=pressb.lis
outb=pressb.dat

sed -e '1,3d' $int > $outt
sed -e '1,3d' $inb > $outb


rm cfpost.jou
cp presst.dat ./results/presst$1.dat
cp pressb.dat ./results/pressb$1.dat
rm press*

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

units inches

zone 1
subset i all j all k 1
cut at x 4.9922

variable y in
variable u m sec

genplot output yufive

clear all
grid $grid
solution $sol

units inches

zone 1
subset i all j all k 1
cut at x 7.9871

variable y in
variable u m sec

genplot output yueight

clear all
grid $grid
solution $sol

units inches

zone 1
subset i all j all k 1
cut at x 10.9821

variable y in
variable u m sec


genplot output yueleven

clear all
grid $grid
solution $sol

units inches

zone 1
subset i all j all k 1
cut at x 12.9794

variable y in
variable u m sec

genplot output yuthirteen
exit
" > velocity.jou

cfpost_prod.exe < velocity.jou

grep -v -e 'Exported' -e 'Strong' -e 'y' -e 'u' yufive.gen > ifive.dat
grep -v -e 'Exported' -e 'Strong' -e 'y' -e 'u' yueight.gen > ieight.dat
grep -v -e 'Exported' -e 'Strong' -e 'y' -e 'u' yueleven.gen > ieleven.dat
grep -v -e 'Exported' -e 'Strong' -e 'y' -e 'u' yuthirteen.gen > ithirteen.dat



mv *.gen ifive.dat ieight.dat ieleven.dat ithirteen.dat results/
rm velocity.jou
fi

###########
# Residuals
###########
#Extract max residuals of N-S equations for all zones
for ((nzone=1; nzone<=1; nzone++))
do
  grep -a RES run.lis | awk '{ print $2 "    " $4 "    " $9 }' | grep "   $nzone   " | awk '{ print $3 }' > maxres$nzone.dat
  grep -a S-A run.lis | awk '{ print $2 "    " $4 "    " $9 }' | grep "   $nzone   " | awk '{ print $3 }' > maxsa$nzone.dat
done

mv maxres* maxsa* results/
rm cfpost.jou
