#!/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: mkgeo_grid -- build a regular grid mesh 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 -boundary
#  The boundary domains for boxes uses names: @code{left}, @code{right},
#  @code{top}, @code{bottom},@code{front} and @code{back}.
#  Instead of splitting the boundary in faces, this option
#  groups all boundary faces in one domain named @code{boundary}.
#@end table
#@example
#	mkgeo_grid -t 10 -boundary | geo -
#@end example
#REGIONS:
#@table @code
#@itemx -region
#  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
#  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
#@itemx -rz
#	the 2d mesh is axisymmetric.
#@end table
#
#DATE:
#    2 february 2004
#END:

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

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

args=""
dim=""
cartesian=true
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"; cartesian=false;;
  -boundary | -region | -corner) args="$args $1";;
  *) echo ${usage} >&2; exit 1;;
  esac
  shift
done

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

pkgbindir=`rheolef-config --pkglibdir`
command="$pkgbindir/mkgeo_grid_$dim $args 2>/dev/null"
if test $dim = 3d; then
  command="$command | geo -upgrade -geo - 2>/dev/null"
fi
#echo "! $command" >&2
eval $command
