#!/bin/sh
#--------------------------------*- C++ -*------------------------------------#
# =========                 |                                                 #
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox           #
#  \\    /   O peration     | Version:  3.0.1                                 #
#   \\  /    A nd           | Web:      www.OpenFOAM.org                      #
#    \\/     M anipulation  |                                                 #
#-----------------------------------------------------------------------------#
cd ${0%/*} || exit 1    # Run from this directory

x0=0.4
y0=0.1
z0=0.1
l=0.2
offset=0.3
tol=0.001

nx=1
ny=2
nz=2


tmpSetSet='patchify.setSet'
rm -f patchify.setSet >/dev/null 2>&1


initialiseFaceSet()
{
    echo "faceSet cubeFaceSet clear" >> $tmpSetSet;
}

addToFaceSet()
{
    name=$1
    x1=$2
    x2=$3
    y1=$4
    y2=$5
    z1=$6
    z2=$7

    echo "faceSet cubeFaceSet add boxToFace ($x1 $y1 $z1) ($x2 $y2 $z2)" >> $tmpSetSet
    echo "   $name: (x1=$x1 y1=$y1 z1=$z1) (x2=$x2 y2=$y2 z2=$z2)"
}

createSetsAndZone()
{
    name=$1
    x1=$2
    x2=$3
    y1=$4
    y2=$5
    z1=$6
    z2=$7

    echo "faceSet ${name}Faces new boxToFace ($x1 $y1 $z1) ($x2 $y2 $z2)" >> $tmpSetSet
    echo "   $name: (x1=$x1 y1=$y1 z1=$z1) (x2=$x2 y2=$y2 z2=$z2)"
}

initialiseFaceSet


# loop over cubes
n=1
x=$x0
for xi in $(seq 1 1 $nx); do
   y=$y0
   for yi in $(seq 1 1 $ny); do
       z=$z0
       for zi in $(seq 1 1 $nz); do
           echo "processing cube $n"

           pad=""
           if [ $n -lt 10]; then
               pad="0"
           fi

           # side 1
           side=1
           x1=`echo $x $tol | awk '{print $1 - $2}'`
           x2=`echo $x $l $tol | awk '{print $1 + $2 + $3}'`
           y1=`echo $y $tol | awk '{print $1 - $2}'`
           y2=`echo $y $l $tol | awk '{print $1 + $2 + $3}'`
           z1=`echo $z $l $tol | awk '{print $1 + $2 - $3}'`
           z2=`echo $z $l $tol | awk '{print $1 + $2 + $3}'`
           addToFaceSet cube${pad}${n}_side${side} $x1 $x2 $y1 $y2 $z1 $z2

           #  side 2
           side=2
           x1=`echo $x $l $tol | awk '{print $1 + $2 - $3}'`
           x2=`echo $x $l $tol | awk '{print $1 + $2 + $3}'`
           y1=`echo $y $tol | awk '{print $1 - $2}'`
           y2=`echo $y $l $tol | awk '{print $1 + $2 + $3}'`
           z1=`echo $z $tol  | awk '{print $1 - $2}'`
           z2=`echo $z $l $tol | awk '{print $1 + $2 + $3}'`
           addToFaceSet cube${pad}${n}_side${side} $x1 $x2 $y1 $y2 $z1 $z2

           # side 3
           side=3
           x1=`echo $x $tol | awk '{print $1 - $2}'`
           x2=`echo $x $l $tol | awk '{print $1 + $2 + $3}'`
           y1=`echo $y $tol | awk '{print $1 - $2}'`
           y2=`echo $y $l $tol | awk '{print $1 + $2 + $3}'`
           z1=`echo $z $tol | awk '{print $1 - $2}'`
           z2=`echo $z $tol | awk '{print $1 + $2}'`
           addToFaceSet cube${pad}${n}_side${side} $x1 $x2 $y1 $y2 $z1 $z2

           # side 4
           side=4
           x1=`echo $x $tol | awk '{print $1 - $2}'`
           x2=`echo $x $tol | awk '{print $1 + $2}'`
           y1=`echo $y $tol | awk '{print $1 - $2}'`
           y2=`echo $y $l $tol | awk '{print $1 + $2 + $3}'`
           z1=`echo $z $tol | awk '{print $1 - $2}'`
           z2=`echo $z $l $tol | awk '{print $1 + $2 + $3}'`
           addToFaceSet cube${pad}${n}_side${side} $x1 $x2 $y1 $y2 $z1 $z2

           # side 5
           side=5
           x1=`echo $x $tol | awk '{print $1 - $2}'`
           x2=`echo $x $l $tol | awk '{print $1 + $2 + $3}'`
           y1=`echo $y $l $tol | awk '{print $1 + $2 - $3}'`
           y2=`echo $y $l $tol | awk '{print $1 + $2 + $3}'`
           z1=`echo $z $tol | awk '{print $1 - $2}'`
           z2=`echo $z $l $tol | awk '{print $1 + $2 + $3}'`
           addToFaceSet cube${pad}${n}_side${side} $x1 $x2 $y1 $y2 $z1 $z2

           # side 6
           side=6
           x1=`echo $x $tol | awk '{print $1 - $2}'`
           x2=`echo $x $l $tol | awk '{print $1 + $2 + $3}'`
           y1=`echo $y $tol | awk '{print $1 - $2}'`
           y2=`echo $y $tol | awk '{print $1 + $2}'`
           z1=`echo $z $tol | awk '{print $1 - $2}'`
           z2=`echo $z $l $tol | awk '{print $1 + $2 + $3}'`
           addToFaceSet cube${pad}${n}_side${side} $x1 $x2 $y1 $y2 $z1 $z2

           n=$((n+1))

           z=`echo $z $offset | awk '{print $1 + $2}'`
       done

       y=`echo $y $offset | awk '{print $1 + $2}'`
   done

   x=`echo $x $offset | awk '{print $1 + $2}'`
done

echo "cellSet cubeFacesCells new faceToCell cubeFaceSet owner" >> $tmpSetSet
echo "faceZoneSet cubeFaces new setsToFaceZone cubeFaceSet cubeFacesCells" >> $tmpSetSet

echo "processing floor"
floorMax=`echo 1 $tol | awk '{print $1 + $2}'`
createSetsAndZone Floor -$tol $floorMax -$tol $floorMax -$tol $tol
echo "cellSet floorCells new faceToCell FloorFaces owner" >> $tmpSetSet
echo "faceZoneSet floorFaces new setsToFaceZone FloorFaces floorCells" >> $tmpSetSet

setSet -batch $tmpSetSet > log.setSet.patchifyObstacles 2>&1

# *************************************************************************
