#!/bin/bash
export LC_ALL=en_US.UTF-8
stlStart=$(date +%s)
if [ -z ${TERM+x} ] || [ $TERM==dumb ]
then TERM=xterm-256color
TPUTTERM=' -T xterm-256color'
else TPUTTERM=''
fi
if [ -e cfgREADY ] && [ -e "NoSTL" ]
then touch cfgSTARTED
rm cfgREADY
echo "--A-- Reading parameters ----  "
source ~/.bashrc
OFdir=$(cat cfgParameters|grep 'OFdir'|cut -d' ' -f2)
if [ -z $OFdir ]
then OFdir="/opt/OpenFOAM/OpenFOAM-v2212"
else
if [[ "$OFdir" == *"2112"* ]]
then OFdir=$(echo $OFdir | sed "s/2112/2212/")
echo "$(tput${TPUTTERM} setaf 5)Warning :$(tput${TPUTTERM} sgr0) requested OF version to be v2112, but this template was made for 2212. Forcing OF v2212."
fi
fi
source $OFdir/etc/bashrc
PBdir=$(cat cfgParameters|grep 'PBdir'|cut -d' ' -f2)
if [ -z $PBdir ]
then PBdir="/opt/Paraview/ParaView-5.8.1-headless"
fi
cfgFullPath=$(pwd)
geometryUnits=$(cat cfgParameters|grep 'geometryUnits'|cut -d' ' -f2)
geometrydetail=$(cat cfgParameters|grep 'geometrydetail'|cut -d' ' -f2)
SurfaceRef=$(cat cfgParameters|grep 'SurfaceRef'|cut -d' ' -f2)
ChordRef=$(cat cfgParameters|grep 'ChordRef'|cut -d' ' -f2)
SpanRef=$(cat cfgParameters|grep 'SpanRef'|cut -d' ' -f2)
mode=$(cat cfgParameters|grep 'mode'|cut -d' ' -f2)
detail1=`echo "0.05*$geometryUnits*$geometrydetail" | bc -l`
detail2=`echo "0.005*$geometryUnits*$geometrydetail" | bc -l`
if [[ $mode == "2D" ]]
then detail1=`echo "0.01*$geometryUnits*$geometrydetail" | bc -l`
SurfaceRef=`echo "1*$ChordRef" | bc -l`
SpanRef=`echo "1*$ChordRef" | bc -l`
echo " 2D case : SurfaceRef forced to $SurfaceRef ; SpanRef forced to $SpanRef"
fi
rotations=$(cat cfgParameters|grep 'part-rotation'|sed 's/part-rotation (//'|tr -d ')')
translations=$(cat cfgParameters|grep 'part-translation'|sed 's/part-translation (//'|tr -d ')')
if [[ ! -z $rotations ]]
then
for rotline in $rotations
do pid=$(echo $rotline|cut -d ';' -f1)
angle=$(echo $rotline|cut -d ';' -f2)
Tx=$(echo $rotline|cut -d ';' -f3)
Ty=$(echo $rotline|cut -d ';' -f4)
Tz=$(echo $rotline|cut -d ';' -f5)
Vectx=$(echo $rotline|cut -d ';' -f6)
Vecty=$(echo $rotline|cut -d ';' -f7)
Vectz=$(echo $rotline|cut -d ';' -f8)
T1x=`echo "-1*$Tx/$geometryUnits" | bc -l`
T1y=`echo "-1*$Ty/$geometryUnits" | bc -l`
T1z=`echo "-1*$Tz/$geometryUnits" | bc -l`
T2x=`echo "$Tx/$geometryUnits" | bc -l`
T2y=`echo "$Ty/$geometryUnits" | bc -l`
T2z=`echo "$Tz/$geometryUnits" | bc -l`
if [ -e geometry-input/$pid.stl ]
then echo "   rotating $pid ..."
surfaceTransformPoints -translate "($T1x $T1y $T1z)" geometry-input/$pid.stl geometry-input/$pid.stl >logs/05-A1-customRotation-$pid.log 2>&1
surfaceTransformPoints -rotate-angle "(($Vectx $Vecty $Vectz) $angle)" geometry-input/$pid.stl geometry-input/$pid.stl >logs/05-A2-customRotation-$pid.log 2>&1
surfaceTransformPoints -translate "($T2x $T2y $T2z)" geometry-input/$pid.stl geometry-input/$pid.stl >logs/05-A3-customRotation.log 2>&1
else echo "$(tput${TPUTTERM} setaf 1)Error: File $pid requested for a custom rotation, but the file is missing.$(tput${TPUTTERM} sgr0)" | tee logs/05-B3-missing-file.warning
cp logs/05-B3-missing-file.warning cfgFAILED; exit 1	
fi
done
fi
if [[ ! -z $translations ]]
then
for rotline in $translations
do pid=$(echo $rotline|cut -d ';' -f1)
Tx=$(echo $rotline|cut -d ';' -f2)
Ty=$(echo $rotline|cut -d ';' -f3)
Tz=$(echo $rotline|cut -d ';' -f4)
if [ -e geometry-input/$pid.stl ]
then echo "   translating $pid ..."
surfaceTransformPoints -translate "($Tx $Ty $Tz)" geometry-input/$pid.stl geometry-input/$pid.stl >logs/05-A4-customTranslation-$pid.log 2>&1
else echo "$(tput${TPUTTERM} setaf 1)Error: File $pid requested for a custom translation, but the file is missing.$(tput${TPUTTERM} sgr0)" | tee logs/05-B4-missing-file.warning
cp logs/05-B4-missing-file.warning cfgFAILED; exit 1	
fi
done
fi
echo "--B-- Treating optional volumes and PP surfaces ----"
ls geometry-input| grep -e "vos-rotorDiskVolume.*.stl" -e "vos-porousInSurface.*.stl" -e "vos-porousOutSurface.*.stl" -e "vos-porousVolume.*.stl" -e "vos-reportSurface".*".stl"> bVolumesAndSurfacesList
if [[ $(cat bVolumesAndSurfacesList|wc -l) -gt 0 ]]
then sed -i "s/.stl//g" bVolumesAndSurfacesList
while read file; do 
echo "   treating $file ..."
surfaceTransformPoints -scale "($geometryUnits $geometryUnits $geometryUnits)" geometry-input/$file.stl geometry-input/$file.stl >logs/05-B1-scaling-$file.log 2>&1
surfaceOrient geometry-input/$file.stl "(35.5 25.5 -15.5)" constant/triSurface/$file.stl >logs/05-B2-surfaceOrient-$file.log 2>&1
rm geometry-input/$file.stl
done <bVolumesAndSurfacesList
fi
ls geometry-input| grep -e "vos-refinementVolume".*".stl"> bRefinementVolumesList
sed -i "s/.stl//g" bRefinementVolumesList
for file in vos-refinementVolume16 vos-refinementVolume8 vos-refinementVolume4 vos-refinementVolume2 vos-refinementVolume1 vos-refinementVolumex2 vos-refinementVolumex4 vos-refinementVolumex8; do 
if [[ $(cat bRefinementVolumesList|grep -e "${file}-"|wc -l) -eq 1 ]]
then echo "   ${file}-01.stl was found already concatenated and is renamed ${file}.stl."
mv geometry-input/"${file}-"01.stl constant/triSurface/$file.stl
else
if [[ $(cat bRefinementVolumesList|grep -e "${file}-"|wc -l) -gt 1 ]]
then echo "   $file will be concatenated."
if test "$geometry-input/${file}.stl"; then null=0; else rm geometry-input/$file.stl; fi
for part in $(cat bRefinementVolumesList|grep -e "${file}-")
do surfaceTransformPoints -scale "(1 1 1)" geometry-input/${part}.stl geometry-input/${part}.stl >logs/05-B3-bin-to-ascii-$file.log 2>&1
cat geometry-input/${part}.stl >> constant/triSurface/${file}.stl;
rm geometry-input/${part}.stl
done
else #there is no ${file}-*.stl file but there is one ${file}.stl file
if [[ $(cat bRefinementVolumesList|grep -e "${file}-"|wc -l) -lt 1 ]] && [ -e geometry-input/$file.stl ]
then echo "   ${file}.stl was found already concatenated."
mv geometry-input/$file.stl constant/triSurface/$file.stl
fi
fi
fi
done
for file in vos-refinementVolume16 vos-refinementVolume8 vos-refinementVolume4 vos-refinementVolume2 vos-refinementVolume1 vos-refinementVolumex2 vos-refinementVolumex4 vos-refinementVolumex8; do 
if [ -e constant/triSurface/$file.stl ]
then echo "   scaling $file ..."		
surfaceTransformPoints -scale "($geometryUnits $geometryUnits $geometryUnits)" constant/triSurface/$file.stl constant/triSurface/$file.stl >logs/05-B1-scaling-$file.log 2>&1
surfaceOrient constant/triSurface/$file.stl "(95.5 45.5 -35.5)" constant/triSurface/$file.stl >logs/05-B2-surfaceOrient-$file.log 2>&1
fi
done
echo "--C-- Scaling aircraft surfaces ---- "
ls geometry-input| grep -e ".*.stl" -e ".*.obj" > boundaryList
Nstl=$(cat boundaryList|wc -l)
ED=$(($Nstl+311+460))
echo -e "0\n$ED\nScaling optional volumes and surfaces..." > $cfgFullPath/cfgStatus
echo "   Estimated duration for geometry configuration treatment : $ED seconds"
cp boundaryList temp
sed -i "s/.stl//g" boundaryList
sed -i "s/.stl/.stl  {type triSurfaceMesh;}/g" temp
cp -r system geometry-input/system
mkdir geometry-input/constant
mkdir geometry-input/constant/triSurface
cat geometry-input/system/surfaceHookUpDict temp > tempp
mv tempp geometry-input/system/surfaceHookUpDict
mv geometry-input/system/controlDict.org geometry-input/system/controlDict
if [[ $Nstl -lt 2 ]]
then ETA=$((760))
floatprogress=`echo "100-100*$ETA/$ED" | bc -l`
progress=${floatprogress%.*}
if [ -z ${progress+x} ]
then progress=0
fi
if [ -e geometry-input/aircraft.stl ]
then cat geometry-input/aircraft.stl | tr -d '\015' | grep '^solid '| sed 's/solid //g' > boundaryListtmp
echo -e "$progress\n$ETA\nScaling aircraft..." > $cfgFullPath/cfgStatus
if [[ $(cat boundaryListtmp | wc -l) -gt 1 ]]
then mv boundaryListtmp boundaryList	#multiple PIDs found			
else echo 'aircraft' >boundaryList #file is named aircraft but with a single PID inside
fi
else
while read boundaryList
do cp geometry-input/$boundaryList.stl geometry-input/aircraft.stl
done <boundaryList
echo 'aircraft' >boundaryList #file is not named aircraft, so we rename it and consider there is only one PID inside.
fi
echo -e "$progress\n$ETA\nScaling aircraft..." > $cfgFullPath/cfgStatus
surfaceTransformPoints -scale "($geometryUnits $geometryUnits $geometryUnits)" geometry-input/aircraft.stl geometry-input/aircraft.stl >logs/05-C-scaling-aircraft.log 2>&1
ETA=$((460))
floatprogress=`echo "100-100*$ETA/$ED" | bc -l`
progress=${floatprogress%.*}
if [ -z ${progress+x} ]
then progress=0
fi
echo -e "$progress\n$ETA\nScaling aircraft..." > $cfgFullPath/cfgStatus
else i=0
while read boundaryList
do ETA=$((($Nstl-$i)+311+460))
floatprogress=`echo "100-100*$ETA/$ED" | bc -l`
progress=${floatprogress%.*}
if [ -z ${progress+x} ]
then progress=0
fi
echo -e "$progress\n$ETA\nScaling $boundaryList..." > $cfgFullPath/cfgStatus
((i++))
surfaceTransformPoints -scale "($geometryUnits $geometryUnits $geometryUnits)" geometry-input/$boundaryList.stl geometry-input/constant/triSurface/$boundaryList.stl >logs/05-C-scaling-$boundaryList.log 2>&1
done <boundaryList
ETA=$((311+460))
floatprogress=`echo "100-100*$ETA/$ED" | bc -l`
progress=${floatprogress%.*}
if [ -z ${progress+x} ]
then progress=0
fi
echo -e "$progress\n$ETA\nNaming parts..." > $cfgFullPath/cfgStatus
while read boundaryList
do cat geometry-input/constant/triSurface/$boundaryList.stl|sed "s/solid .*/solid $boundaryList/g">temp
mv temp geometry-input/constant/triSurface/$boundaryList.stl
done <boundaryList
echo "--D-- Hooking-up aircraft surfaces ---- (can be long) "
cd geometry-input
ETA=$((310+460))
floatprogress=`echo "100-100*$ETA/$ED" | bc -l`
progress=${floatprogress%.*}
if [ -z ${progress+x} ]
then progress=1
else
if [[ $progress -lt 1 ]]
then progress=1
fi
fi
echo -e "$progress\n$ETA\nHooking-up surfaces...(can be long)" > $cfgFullPath/cfgStatus
surfaceHookUp "$detail1" >../logs/05-D-surfaceHookUp.log 2>&1
cd $cfgFullPath
echo "--E-- Naming aircraft surfaces ---- "
ETA=$((460))
floatprogress=`echo "100-100*$ETA/$ED" | bc -l`
progress=${floatprogress%.*}
if [ -z ${progress+x} ]
then progress=1
else
if [[ $progress -lt 1 ]]
then progress=1
fi
fi
echo -e "$progress\n$ETA\nNaming hooked parts..." > $cfgFullPath/cfgStatus
while read boundaryList
do mv geometry-input/constant/triSurface/hookedSurface_$boundaryList.stl geometry-input/constant/triSurface/$boundaryList.stl
done <boundaryList
if test "$geometry-input/aircraft.stl"; then null=0; else rm geometry-input/aircraft.stl; fi
touch geometry-input/aircraft.stl
echo "--F-- Merging aircraft surfaces ---- "
ETA=$((455))
floatprogress=`echo "100-100*$ETA/$ED" | bc -l`
progress=${floatprogress%.*}
if [ -z ${progress+x} ]
then progress=1
else
if [[ $progress -lt 1 ]]
then progress=1
fi
fi
echo -e "$progress\n$ETA\nMerging parts..." > $cfgFullPath/cfgStatus
while read boundaryList
do cat geometry-input/constant/triSurface/$boundaryList.stl > geometry-input/temp2;
cat geometry-input/temp2 >> geometry-input/aircraft.stl;
cp geometry-input/constant/triSurface/$boundaryList.stl geometry-input/$boundaryList.stl
done <boundaryList
rm geometry-input/temp2
rm -r geometry-input/constant
rm -r geometry-input/system
surfacePointMerge geometry-input/aircraft.stl "$detail2" geometry-input/aircraft.stl >logs/05-F1-surfacePointMerge.log 2>&1
fi
else echo "$(tput${TPUTTERM} setaf 1)Error: files are not ready to be processed. cfgREADY or NoSTL file missing.$(tput${TPUTTERM} sgr0)" | tee logs/05-H-STLnotReady.warning
cp logs/05-H-STLnotReady.warning cfgFAILED; exit 1
fi
surfaceOrient -inside geometry-input/aircraft.stl "(0 0 0)" geometry-input/aircraft.stl >logs/05-F2-surfaceOrient.log 2>&1
stlsize=$(du geometry-input/aircraft.stl | tr '\t' ' '| cut -d' ' -f1)
counter=30
while [[ $counter -gt 1 ]]
do
if [[ $stlsize -le 1 ]]
then sleep 1; stlsize=$(du geometry-input/aircraft.stl | tr '\t' ' '| cut -d' ' -f1)
else break;
fi
((counter--))
done
echo "   aircraft stl file size : $stlsize Ko"
if [[ $stlsize -le 1 ]]
then echo "$(tput${TPUTTERM} setaf 1)Error: produced file is too small, check units selected.$(tput${TPUTTERM} sgr0)" | tee logs/05-F3-STLtooSmall.warning
cp logs/05-F3-STLtooSmall.warning cfgFAILED; exit 1
fi
echo "   checking bounding coordinates of the geometry..."
stlXmin=$(cat geometry-input/aircraft.stl | grep 'vertex' | sed -n -e 's/^.*vertex //p' | cut -d' ' -f1 | tr -d $'\r' | sort -g | head -1)
stlXmax=$(cat geometry-input/aircraft.stl | grep 'vertex' | sed -n -e 's/^.*vertex //p' | cut -d' ' -f1 | tr -d $'\r' | sort -g | tail -1)
stlYmin=$(cat geometry-input/aircraft.stl | grep 'vertex' | sed -n -e 's/^.*vertex //p' | cut -d' ' -f2 | tr -d $'\r' | sort -g | head -1)
stlYmax=$(cat geometry-input/aircraft.stl | grep 'vertex' | sed -n -e 's/^.*vertex //p' | cut -d' ' -f2 | tr -d $'\r' | sort -g | tail -1)
stlZmin=$(cat geometry-input/aircraft.stl | grep 'vertex' | sed -n -e 's/^.*vertex //p' | cut -d' ' -f3 | tr -d $'\r' | sort -g | head -1)
stlZmax=$(cat geometry-input/aircraft.stl | grep 'vertex' | sed -n -e 's/^.*vertex //p' | cut -d' ' -f3 | tr -d $'\r' | sort -g | tail -1)
stlXmin=`echo $stlXmin | sed -e 's/[eE]+*/\\*10\\^/' | bc -l`
stlXmax=`echo $stlXmax | sed -e 's/[eE]+*/\\*10\\^/' | bc -l`
stlYmin=`echo $stlYmin | sed -e 's/[eE]+*/\\*10\\^/' | bc -l`
stlYmax=`echo $stlYmax | sed -e 's/[eE]+*/\\*10\\^/' | bc -l`
stlZmin=`echo $stlZmin | sed -e 's/[eE]+*/\\*10\\^/' | bc -l`
stlZmax=`echo $stlZmax | sed -e 's/[eE]+*/\\*10\\^/' | bc -l`
maxSize=`echo "$stlXmax-1*$stlXmin" | bc -l`
baxSize2=`echo "$stlYmax-1*$stlYmin" | bc -l`
if [[ $mode == "2D" ]]
then stlspheresize=`echo "0.2*$maxSize" | bc -l`
else stlspheresize=`echo "0.01*$maxSize" | bc -l`
fi
test=`echo $baxSize2'>'$maxSize | bc -l`
if (( $test == 1 )); then maxSize=`echo "$baxSize2" | bc -l`; fi
baxSize2=`echo "$stlZmax-1*$stlZmin" | bc -l`
test=`echo $baxSize2'>'$maxSize | bc -l`
if (( $test == 1 )); then maxSize=`echo "$baxSize2" | bc -l`; fi
echo -e "Xmin= "$stlXmin"\nXmax= "$stlXmax"\nYmin= "$stlYmin"\nYmax= "$stlYmax"\nZmin= "$stlZmin"\nZmax= "$stlZmax"\nmaxSize= "$maxSize"\n\n">constant/triSurface/aircraftSTLbounds
ETA=$((246))
floatprogress=`echo "100-100*$ETA/$ED" | bc -l`
progress=${floatprogress%.*}
if [ -z ${progress+x} ]
then progress=0
fi
echo -e "$progress\n$ETA\nMaking visualisations of parts..." > $cfgFullPath/cfgStatus
echo "--G-- Making visualisations of the resulting geometry ----"
echo "   producing visualisations of the cleant STL..."
sed -i "s/editmaxSize/$maxSize/g" PP-STL.py
sed -i "s/editstlXmin/$stlXmin/g" PP-STL.py
sed -i "s/editstlXmax/$stlXmax/g" PP-STL.py
sed -i "s/editstlYmin/$stlYmin/g" PP-STL.py
sed -i "s/editstlYmax/$stlYmax/g" PP-STL.py
sed -i "s/editstlZmin/$stlZmin/g" PP-STL.py
sed -i "s/editstlZmax/$stlZmax/g" PP-STL.py
sed -i "s/editspheresize/$stlspheresize/g" PP-STL.py
$PBdir/bin/pvbatch --force-offscreen-rendering PP-STL.py >logs/05-G-pvbatch.log 2>&1
ETA=$((16))
floatprogress=`echo "100-100*$ETA/$ED" | bc -l`
progress=${floatprogress%.*}
if [ -z ${progress+x} ]
then progress=0
fi
echo -e "$progress\n$ETA\nMaking visualisations of parts..." > $cfgFullPath/cfgStatus
mv geometry-input/*.stl constant/triSurface/
cd $cfgFullPath
echo "--H-- Finalising exports ----"
cp system/controlDict.cfg system/controlDict
cp system/blockMeshDict.cfg system/blockMeshDict
cp system/fvSolution.simple system/fvSolution
cp system/fvSchemes.simple system/fvSchemes
blockMesh >logs/05-H-blockMesh.log 2>&1
postProcess >logs/05-H-postProcess.log 2>&1
mv postProcessing/vtk-cfg/constant/cfg_aircraft.vtp VTKexports/cfg_aircraft.vtp
stlEnd=$(date +%s)
stltime=$(($stlEnd-$stlStart))
hours=`echo "$stltime/3600" | bc -l`
hours=$(LC_ALL=C /usr/bin/printf "%.*f\n" 1 $hours)
echo "   Total time for this job : "$stltime" seconds ("$hours" hours). "
echo $stltime> cfgOK
rm cfgSTARTED
echo "--H-- Configuration has been built =="
echo " "
echo -e "100\n0\nConfiguration has been built." > $cfgFullPath/cfgStatus
