#!/bin/sh
#
# This file is part of Rheolef.
#
# Copyright (C) 2000-2009 Pierre Saramito 
#
# Rheolef is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Rheolef is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Rheolef; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# -------------------------------------------------------------------------
#
# mesh generator for the contraction geometry
#
# author: Pierre.Saramito@imag.fr
#
# date: 2 february 2018
#
#Prog:mkgeo_contraction
#NAME: @code{mkgeo_contraction} -- build an unstructured 2d mesh of an abrupt contraction
#@pindex mkgeo_contraction
#@pindex geo
#@cindex mesh
#@fiindex @file{.geo} mesh
#SYNOPSIS:
#@example
#  mkgeo_contraction @var{options} [@var{nx} [@var{ny} [@var{nz}]]]
#@end example
#@pindex mkgeo_contraction
#@pindex geo
#@cindex mesh
#@fiindex @file{.geo} mesh
#EXAMPLE: 
#@noindent
#  The following command build a triangle-based 2d unstructured mesh
#  of the [-10,0]x[0,4] u [0,10]x[0,1] mesh
#@example
#	mkgeo_contraction > contraction.geo
#	geo contraction.geo
#@end example
#DESCRIPTION:       
#@pindex bamg
#  @noindent
#  This command is convenient for building a mesh for
#  the abrupt contraction, as it is a very classical benchmark in complex
#  fluid flow problems.
#  It calls @code{bamg} unstructured mesh generator
#  with anisotropy feature.
#  The mesh files goes on @file{@var{name}.geo} where @var{name}
#  is the basename for the output (see option @code{-name} below).
#  The three auxiliary files required for automatic mesh generation
#  with Rheolef combined with @code{bamg} are also provided:
#        @file{@var{name}@code{.bamg}},
#        @file{@var{name}@code{.bamgcad}} and
#        @file{@var{name}@code{.dmn}}.
#THE GEOMETRY:
#  @noindent
#  The geometry is [-Lu,0]x[0,d] u [0,Ld]x[0,1].
#  By default c=4 is the contraction ratio and Lu=Ld=10
#  are the upstream and downstream pipe lengths.
#@table @code
#@item  -c @var{float}
#@itemx -Lu @var{float}
#@itemx -Ld @var{float}
#  	These options control the geometry parameters.
#@item  -cartesian
#@itemx -zr
#@itemx -rz
#  	These options control the geometry coordinate system.
#	Default is cartesian.
#THE DISCRETIZATION:
#  The optional @var{nx} and @var{ny} arguments are floats
#  that specifies the subdivision in each direction.
#  By default @var{nx}=1 and @var{ny}=@var{ny}.
#  Changing the density applies the factor 1/@var{n} to all the mesh edges lengths.
#@item  -hmin @var{float}
#	Controls the edge length at the re-entrant corner of the contraction.
#	Default is @code{hmin}=0.1.
#	Changing the density by the previous options 
#	applies the factor 1/@var{n} to @code{hmin} also.
#@end table
#BOUNDARY DOMAINS:
#  The boundary sides are represented by domains: @code{axis}, @code{wall},
#  @code{upstream} and @code{downstream}.
#OTHERS OPTIONS:
#@table @code
#  @item  -name @var{string}
#        Set the basename for the output files.
#        By default, the basename is @code{contraction}.
#  @item  -split
#  @itemx -nosplit
#        Split each triangle in three substriangles by inserting the barycenter as
#	 an additional node. This is useful for using the Scott and Vogelius 
#	 incompressible mixed finite element P2-P1d for velocity-pressure approximation.
#        Default is no split.
#  @item  -clean
#  @itemx -noclean
#        Clear temporary files (this is the default).
#  @item  -verbose
#  @itemx -noverbose
#        In verbose mode, print to stderr all subcommands and logs.
#@end table
#END:

nx=""
nx_default=1
c=4
Lu=20
Ld=20
hmin=0.1
split=false
clean=true
verbose=false
name="contraction"
usage="usage: mkgeo_contraction 
	[nx=$nx_default [ny=nx]]
	[-c float=$c]
	[-Lu float=$Lu]
	[-Ld float=$Ld]
	[-rz|-zr]
        [-hmin float=$hmin]
	[-name string=$name]
	[-[no]split]
	[-[no]clean]
	[-[no]verbose]
"
pkgbindir="`rheolef-config --pkglibdir`"

while test $# -ne 0; do
  case $1 in
  [0-9]*) if test x$nx = x""; then nx=$1; else ny=$1; fi;;
  -c)   c="$2"; shift;;
  -Lu) Lu="$2"; shift;;
  -Ld) Ld="$2"; shift;;
  -zr|-rz|-cartesian) sys_coord_opt="$1";;
  -name)      name=$2; shift;;
  -hmin)      hmin=$2; shift;;
  -split)     split=true;;
  -nosplit)   split=false;;
  -clean)     clean=true;;
  -noclean)   clean=false;;
  -verbose)   verbose=true;;
  -noverbose) verbose=false;;
  -h) /bin/echo -E ${usage} >&2; exit 0;;
  *)  /bin/echo -E ${usage} >&2; exit 1;;
  esac
  shift
done
if test x"$nx" = x""; then
  nx=$nx_default
fi
if test x"$ny" = x""; then
  ny=$nx
fi

to_clean=""


# edge lengths are scaled at upstream by c and in x-dir by Lu and Ld
h=0.3
h0=`echo  $hmin   $nx | awk '{print 1.0*$1/$2}'`
hd=`echo  $h      $nx | awk '{print 1.0*$1/$2}'`
hu=`echo  $h  $c  $nx | awk '{print 1.0*$1*$2/$3}'`
hux=`echo $h  $Lu $nx | awk '{print 1.0*$1*$2/$3}'`
huy=`echo $h  $c  $ny | awk '{print 1.0*$1*$2/$3}'`
hdx=`echo $h  $Ld $nx | awk '{print 1.0*$1*$2/$3}'`
hdy=`echo $h      $ny | awk '{print 1.0*$1/$2}'`

#echo "hdx=$hdx"
#echo "hdy=$hdy"

m0=` echo $h0  | awk '{print 1./($1*$1) }'`
md=` echo $hd  | awk '{print 1./($1*$1) }'`
mu=` echo $hu  | awk '{print 1./($1*$1) }'`
mux=`echo $hux | awk '{print 1./($1*$1) }'`
muy=`echo $huy | awk '{print 1./($1*$1) }'`
mdx=`echo $hdx | awk '{print 1./($1*$1) }'`
mdy=`echo $hdy | awk '{print 1./($1*$1) }'`

c2=`echo $c $Ld | awk '{ c2 = 2*$1; print (c2 < $2 ? c2 : $2) }'`

#
# background mesh for bamg mesh generator:
# with anisotropic (1/hx^2, 1/hy^2) metric
#
#  11                  10          9
# +-------------------+-----------+
# |                   |\          |
# |                   |  \  T3    |
# |                   |    \      |
# |                   |      \    |
# |      Q1           |        \  |
# |                   |          \|8           7                6
# |                   |   Q2      +-----------+---------------+
# |1                  |2          |3   Q4     |4    Q5        | 5
# +-------------------+-----------+-----------+---------------+
# -Lu                 -c          0           c2             Ld
#
cat > $name.bamgcad << EOF2
MeshVersionFormatted
  0
Dimension
  2
Vertices
  11
 -$Lu 0  1
 -$c  0  2
 -0   0  3
  $c2 0  4
  $Ld 0  5
  $Ld 1  6
  $c2 1  7
  0   1  8
  0   $c 9
 -$c  $c 10
 -$Lu $c 11
Edges
  11
  1 2    101
  2 3    101
  3 4    101
  4 5    101
  5 6    102
  6 7    103
  7 8    103
  8 9    103
  9 10   103
 10 11   103
 11 1    104
EOF2
echo "! $name.bamgcad created" 1>&2

cat > $name.mtr << EOF1b
11 3
$mux 0 $muy
$mu  0 $mu 
$md  0 $md
$md  0 $md
$mdx 0 $mdy
$mdx 0 $mdy
$md  0 $md
$m0  0 $m0
$mu  0 $mu
$mu  0 $mu
$mux 0 $muy
EOF1b
$verbose && echo "! $name.mtr created" 1>&2
to_clean="$to_clean $name.mtr"

cat > $name.dmn << EOF3
EdgeDomainNames
  4
  axis
  downstream
  wall
  upstream
EOF3
echo "! $name.dmn created" 1>&2

# bamg -coef 1 -err  0.01 -errg 0.1 -hmin 0.004 -hmax 0.3 -ratio 0 -anisomax 1e+06 -nbv 50000 -NbJacobi 1 -CutOff 1e-07 -splitpbedge  -RelError -b sector-10-P2-Bi-0.5-n-1-v2.bamg -Mbb sector-10-P2-Bi-0.5-n-1-v2-crit.bb -o sector-10-P2-Bi-0.5-n-1-v2-001.bamg 1>&2

command="bamg -g $name.bamgcad -M $name.mtr -o $name.bamg"
if $verbose; then
  command="$command 1>&2"
else
  command="$command 1> $name.bamglog"
  to_clean="$to_clean $name.bamglog"
fi
$verbose && echo "! $command" 1>&2
eval    $command
status=$?
if test $status -ne 0; then
  if $verbose; then true; else cat $name.bamglog 1>&2; fi
  echo "$0: command failed" 1>&2
  exit $status
fi
echo "! $name.bamg created" 1>&2

if $split; then
  filter="| ${pkgbindir}/geo_split | geo -upgrade -geo -"
else
  filter=""
fi
command="bamg2geo $name.bamg $name.dmn $sys_coord_opt $filter > $name.geo"
$verbose && echo "! $command" 1>&2
eval    $command
status=$?
if test $status -ne 0; then
  echo "$0: command failed" 1>&2
  exit $status
fi
echo "! $name.geo created" 1>&2
if $clean; then
  command="rm -f $to_clean"
  $verbose && echo "! $command" 1>&2
  eval    $command
fi
