#!/bin/bash
#
# 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 square sector eometry
#
# author: Pierre.Saramito@imag.fr
#
# date: 2 february 2018
#
#Prog:mkgeo_contraction
#NAME: @code{mkgeo_sector} -- build an unstructured 2d mesh of a sector
#@pindex mkgeo_sector
#@pindex geo
#@cindex mesh
#@fiindex @file{.geo} mesh
#SYNOPSIS:
#@example
#  mkgeo_sector @var{options} [@var{n}]
#@end example
#@pindex geo
#@cindex mesh
#@fiindex @file{.geo} mesh
#EXAMPLE: 
#@noindent
#  The following command build 2d unstructured mesh
#  of the triangular region delimited by the three points (0,0), (1,0), (1,1):
#@example
#	mkgeo_sector
#	geo sector.geo
#@end example
#DESCRIPTION:       
#@pindex bamg
#  @noindent
#  This command is convenient for building a mesh for
#  a sector, as it is a very classical benchmark in complex
#  fluid flow problems.
#  It calls @code{bamg} unstructured mesh generator.
#  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 the triangular sector (0,0), (a,0), (a,b).
#  By default a=b=1.
#@table @code
#@item  -a @var{float}
#@itemx -b @var{float}
#  	These options control the geometry parameters.
#@end table
#THE DISCRETIZATION:
#  The optional @var{n} argument is an integer
#  that specifies the subdivision in each direction.
#  By default @var{n}=10.
#BOUNDARY DOMAINS:
#  The boundary sides are represented by domains: @code{axis}, @code{boundary},
#  and @code{bisector}.
#OTHERS OPTIONS:
#@table @code
#  @item  -name @var{string}
#        Set the basename for the output files.
#        By default, the basename is @code{sector}.
#  @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:

n=""
n_default=10
a=1
b=1
name="sector"
clean=true
verbose=false

usage="usage: mkgeo_contraction 
	[n=$n_default]
	[-a float=$a]
	[-b float=$b]
	[-name string=$name]
	[-[no]clean]
	[-[no]verbose]
"

while test $# -ne 0; do
  case $1 in
  [0-9]*) if test x$n = x""; then n=$1; else m=$1; fi;;
  -a) a="$2"; shift;;
  -b) b="$2"; shift;;
  -name)      name=$2; shift;;
  -clean)     clean=true;;
  -noclean)   clean=false;;
  -verbose)   verbose=true;;
  -noverbose) verbose=false;;
  -h) echo ${usage} >&2; exit 0;;
  *)  echo ${usage} >&2; exit 1;;
  esac
  shift
done
if test x"$n" = x""; then
  n=$n_default
fi

to_clean=""

h=`echo $n | awk '{printf("%.15g\n", 1./$1)}'`
cat > $name.bamgcad << EOF1
	MeshVersionFormatted
           0
         Dimension
           2
         Vertices
           3
           0  0     1
           $a 0     2
           $a $b    3
         Edges
           3
           1  2     101
           2  3     102
           3  1     103
         hVertices
           $h $h $h
EOF1
echo "! file $name.bamgcad created" 1>&2

cat > $name.dmn << EOF2
        EdgeDomainNames
          3
          axis
          boundary
          bisector
EOF2
echo "! file $name.dmn created" 1>&2

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

command="bamg2geo $name.bamg $name.dmn > $name.geo"
if $verbose; then
  echo "! $command" 1>&2
else 
  command="$command 2> $name.bamglog"
  to_clean="$to_clean $name.bamglog"
fi
eval $command
if test $? -ne 0; then
  echo "$0: command failed"
  if $verbose; then true; else cat $name.bamglog; fi
  exit 1
fi
echo "! file $name.geo created" 1>&2

if $clean; then
  command="rm -f $to_clean"
  $verbose && echo "! $command" 1>&2
  eval $command
fi
