#!/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_ugrid
#NAME: @code{mkgeo_ugrid} -- build an unstructured mesh of a parallelotope, in 1d, 2d or 3d
#@pindex mkgeo_ugrid
#@pindex geo
#@cindex mesh
#@fiindex @file{.geo} mesh
#SYNOPSIS:
#@example
#	mkgeo_ugrid @var{options} [@var{n}]
#@end example
#EXAMPLE: 
#@noindent
#  The following command build a triangle based 2d unstructured mesh
#  of the unit square:
#@example
#	mkgeo_ugrid -t 10 > square-10.geo
#	geo -mayavi square-10.geo
#@end example
#@noindent
#  or in one comand line:
#@example
#	mkgeo_ugrid -t 10 | geo -mayavi -
#@end example
#DESCRIPTION:       
#@pindex gmsh
#  @noindent
#  This command is usefull when testing programs on simple geometries.
#  Invocation is similar to @code{mkgeo_grid} (see @ref{mkgeo_grid command}).
#  It calls @code{gmsh} as unstructured mesh generator.
#  It avoid the preparation of an input file for a mesh generator.
#  The optional @var{n} argument is an integer
#  that specifies the subdivision in each direction. By default 
#  @var{n}=10.
#  The mesh files goes on standard output.
#
#  @noindent
#  The command supports all the possible element types: edges, triangles,
#  rectangles, tetraedra, prisms and hexahedra. It supports also mixed 2D 
#  with triangles and quadrangles:  
#@example
#	mkgeo_ugrid -tq 10 | geo -mayavi -
#@end example
#  and mixed 3D with tetraedra, prisms and/or hjexaedra:
#@example
#	mkgeo_ugrid -TP  10 | geo -mayavi -
#	mkgeo_ugrid -PH  10 | geo -mayavi -
#	mkgeo_ugrid -TPH 10 | geo -mayavi -
#@end example
#ELEMENT TYPE OPTIONS:
#@table @code
#@item -e
#	1d mesh using edges.
#@item -t
#	2d mesh using triangles.
#@item -q
#	2d mesh using quadrangles.
#@item -tq
#	2d mesh using both triangles and quadrangles.
#@item -T
#	3d mesh using tetraedra.
#@item -P
#	3d mesh using prisms.
#@item -H
#	3d mesh using hexahedra.
#@item -TP
#@itemx -PH
#@itemx -TPH
#	3d mesh using a mixt between tetraedra, prisms and/or 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_ugrid -t 10 -a -2 -b 2 -c -1.5 -d 1.5 | geo -
#@end example
#@table @code
#@item -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
#@item -sides
#@itemx -nosides
#  The boundary sides are representd by domains: @code{left}, @code{right},
#  @code{top}, @code{bottom},@code{front} and @code{back}.
#@item -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_ugrid -t 10 > square.geo 
#       geo square.geo
#	mkgeo_ugrid -t 10 -nosides > square.geo 
#       geo square.geo
#	mkgeo_ugrid -t 10 -noboundary > square.geo 
#       geo square.geo
#	mkgeo_ugrid -t 10 -noboundary -nosides > square.geo 
#       geo square.geo
#@end example
#REGIONS:
#@table @code
#@item -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_ugrid -t 10 -region | geo -
#@end example
#CORNERS:
#@table @code
#@item -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_ugrid -t 10 -corner | geo -
#	mkgeo_ugrid -T  5 -corner | geo -
#@end example
#
#THE MESH ORDER:
#@pindex gmsh
#@cindex mesh order
#@table @code
#@item -order int
#       The polynomial approximation mesh order, as defined by @code{gmsh}.
#       This option enable a possible curved boundary, when applying
#	a suitable nonlinear transformation to the mesh.
#       Defualt is order=1.
#@end table
#OTHERS OPTIONS:
#@table @code
#  @item -clean
#        clear temporary files (this is the default).
#  @item -noclean
#        does not clear temporary files.
#@end table
#
#DATE:
#    1 nov 2011
#END:

# ------------------------------------------
# utility
# ------------------------------------------
verbose=false
my_eval () {
  command="$*"
  if $verbose; then echo "! $command" 1>&2; fi
  eval $command
  if test $? -ne 0; then
    echo "$0: error on command: $command" >&2
    exit 1
  fi
}
# ------------------------------------------
# 1d case
# ------------------------------------------
mkgmsh_1d () {
  variant=$1; shift
  n=$1; shift
  sides=$1; shift;
  boundary=$1; sh                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             