#!/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
#
# -------------------------------------------------------------------------

#Prog:mkgeo_grid
#NAME: @code{mkgeo_grid} -- build a strutured mesh of a parallelotope, in 1d, 2d or 3d
#@pindex mkgeo_grid
#@pindex geo
#@cindex mesh
#@fiindex @file{.geo} mesh
#SYNOPSIS:
#@example
#	mkgeo_grid @var{options} [@var{nx} [@var{ny} [@var{nz}]]]
#@end example
#EXAMPLE: 
#@noindent
#  The following command build a triangular based 2d 10x10 grid
#  of the unit square:
#@example
#	mkgeo_grid -t 10 > square-10.geo
#	geo square-10.geo
#@end example
#@noindent
#  or in one comand line:
#@example
#	mkgeo_grid -t 10 | geo -
#@end example
#DESCRIPTION:       
#  @noindent
#  This command is usefull when testing programs on simple geometries.
#  It avoid the preparation of an input file for a mesh generator.
#  The optional @var{nx}, @var{ny} and @var{nz} arguments are integer
#  that specifies the subdivision in each direction. By default 
#  @var{nx}=10, @var{ny}=@var{nx} and @var{nz}=@var{ny}.
#  The mesh files goes on standard output.
#
#  @noindent
#  The command supports all the possible element types: edges, triangles,
#  rectangles, tetraedra, prisms and hexahedra.
#ELEMENT TYPE OPTIONS:
#@table @code
#@itemx -e
#	1d mesh using edges.
#@itemx -t
#	2d mesh using triangles.
#@itemx -q
#	2d mesh using quadrangles (rectangles).
#@itemx -T
#	3d mesh using tetraedra.
#@itemx -P
#	3d mesh using prisms.
#@itemx -H
#	3d mesh using hexahedra.
#@end table
#THE GEOMETRY:
#  @noindent
#  The geometry can be any [a,b] segment, [a,b]x[c,d] rectangle
#  or [a,b]x[c,d]x[f,g] parallelotope. By default a=c=f=0 and b=d=g=1, thus,
#  the unit boxes are considered. For instance, the following
#  command meshes the [-2,2]x[-1.5, 1.5] rectangle:
#@example
#	mkgeo_grid -t 10 -a -2 -b 2 -c -1.5 -d 1.5 | geo -
#@end example
#@table @code
#@itemx -a @var{float}
#@itemx -b @var{float}
#@itemx -c @var{float}
#@itemx -d @var{float}
#@itemx -f @var{float}
#@itemx -g @var{float}
#@end table
#BOUNDARY DOMAINS:
#@table @code
#@itemx -sides
#@itemx -nosides
#  The boundary sides are representd by domains: @code{left}, @code{right},
#  @code{top}, @code{bottom},@code{front} and @code{back}.
#@itemx -boundary
#@itemx -noboundary
#  This option defines a domain named @code{boundary} that groups all sides.
#@end table
#By default, both sides and the whole boundary are defined as domains:
#@example
#	mkgeo_grid -t 10 > square.geo 
#       geo square.geo
#	mkgeo_grid -t 10 -nosides > square.geo 
#       geo square.geo
#	mkgeo_grid -t 10 -noboundary > square.geo 
#       geo square.geo
#	mkgeo_grid -t 10 -noboundary -nosides > square.geo 
#       geo square.geo
#@end example
#REGIONS:
#@table @code
#@itemx -region
#@itemx -noregion
#  The whole domain is splitted into two subdomains: @code{east} and @code{west},
#  This option is used for testing computations with subdomains (e.g. transmission
#  problem; see the user manual).
#@end table
#@example
#	mkgeo_grid -t 10 -region | geo -
#@end example
#CORNERS:
#@table @code
#@itemx -corner
#@itemx -nocorner
#  The corners (four in 2D and eight in 3D) are defined as OD-domains.
#  This could be usefull for some special boundary conditions.
#@end table
#@example
#	mkgeo_grid -t 10 -corner | geo -
#	mkgeo_grid -T  5 -corner | geo -
#@end example
#
#COORDINATE SYSTEM OPTION:
#   Most of rheolef codes are coordinate-system independant.
#   The coordinate system is specified in the geometry file @file{.geo}.
#@table @code
#@cindex axisymmetric coordinate system
#@itemx -zr
#@itemx -rz
#	the 2d mesh is axisymmetric: @code{zr} (resp. @code{rz}) stands when the symmetry is related
#       to the first (resp. second) coordinate.
#@end table
#
#FILE FORMAT OPTION:
#@table @code
#@end table
#
#DATE:
#    2 february 2004
#END:

usage="mkgeo_grid
	[-{abcdfg} float]
	[-{eptqTPH}]
	[nx [ny nz]]]
	[-[no]sides]
	[-[no]boundary]
	[-[no]region]
	[-[no]corner]
	[-rz|-zr]
	[-v4]
"

if test $# -eq 0; then
  echo ${usage} >&2
  exit 0
fi

args=""
dim=""
sys_coord="cartesian"
new_format=`rheolef-config --have-new-code 2>/dev/null`
GEO_BIN=${GEO_BIN-"geo"}
while test $# -ne 0; do
  case $1 in
  -h) echo ${usage} >&2; exit 0;;
  -e)       dim="1d"; args="$args $1";;
  -[tq])    dim="2d"; args="$args $1";;
  -[TPH])   dim="3d"; args="$args $1";;
  [\-\+0-9e]*)    args="$args $1";;
  -[abcdfg]) args="$args $1 $2"; shift;;
  -rz)       args="$args $1"; sys_coord="rz";;
  -zr)       args="$args $1"; sys_coord="zr";;
  -sides | -boundary | -region | -corner | -nosides | -noboundary | -noregion | -nocorner)
	     args="$args $1";;
  *) echo ${usage} >&2; exit 1;;
  esac
  shift
done
if test ${new_format} = true; then
  args="$args -v4"
fi
#echo "! GEO_BIN=${GEO_BIN}" >&2
#echo "! new_format=${new_format}" >&2
pkgbindir=`rheolef-config --pkglibdir`

if test $sys_coord != "cartesian" -a $dim != 2d; then
  echo "mkgeo_grid: incompatible $dim geometry and non-cartesian \"${sys_coord}\"coordinate system" >&2
  exit 1
fi

command="$pkgbindir/mkgeo_grid_$dim $args 2>/dev/null"
if test $dim = 3d || test ${new_format} = true; then
  command="$command | ${GEO_BIN} -upgrade -geo - 2>/dev/null"
fi
#echo "! $command" >&2
eval $command
status=$?
if test $status -ne 0; then
  echo "$0: command failed" 1>&2
  exit $status
fi
