#!/bin/sh
#
# This file is part of Rheolef.
#
# Copyright (C) 2000-2018 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
# -------------------------------------------------------------------------
#
# for the crack with P1:
# adaptation loop with hmin = coef^pow
#
# -----------------------------
# external file dependencies: 2
# -----------------------------
# file	./crack
# file	./mk_crack
# file  ./crack-norm-grad
# file  ./fld2bb.awk

field2bb="awk -f ./fld2bb.awk"


# ----------------------
# parameters
# ----------------------
approx=P1

bamg_coef=0.5
bamg_err=0.005
bamg_nbv=1000000
bamg_suffix="msh"
pow=2

bb_suffix=${bamg_suffix}.bb
hinit=0.5

usage="$0 [-approx string=${approx}] [-coef float=$bamg_coef] [-pow float=${pow}]
[-err float=$bamg_err] [-nbv int=${bamg_nbv}]"

while test $# -ne 0; do
    case $1 in
      -h|-help)     echo $usage >&2; exit 0;;
      -coef)        bamg_coef=$2; shift;;
      -pow)         pow=$2; shift;;
      -nbv)         bamg_nbv=$2; shift;;
      -approx)      approx=$2; shift;;
      -err)         bamg_err=$2; shift;;
      -*)           /bin/echo "invalid option $1" 1>&2 ; echo $usage >&2; exit 1;;
      *)            /bin/echo "invalid option $1" 1>&2 ; echo $usage >&2; exit 1;;
    esac
    shift
done
bamg_hmin=`echo "${bamg_coef} ${pow}" | awk '{print ($1)**($2)}' ` 
if test $approx = P1; then
    key_field=cat
else
    key_field="crack-norm-grad"
fi
# ----------------------
# make initial mesh
# ----------------------
mk_crack ${hinit} > /dev/null 2> /dev/null
name="crack"
initial="crack-${hinit}"

# ----------------------
# usage: put_info i current
# ----------------------
dat=adapt-coef=${bamg_coef}-${approx}.dat
/bin/rm -f $dat
echo "# $name adaptation loops with:"  > $dat
echo "#    coef ${bamg_coef}"     >> $dat
echo "#    hmin ${bamg_hmin}"     >> $dat
echo "#        = coef**${pow}"    >> $dat
echo "#    nbv  ${bamg_nbv}"      >> $dat
echo "#    err  ${bamg_err}"      >> $dat
echo "# i n_node n_elt hmin hmax err_l2 err_infty err_h1" >> $dat
echo "! file '$dat' created." >&2
cat $dat

function put_info() {
    i=$1
    current=$2
    log=$3

    hmax="`geo -noverbose -hmax $current`"
    n_node="`geo -noverbose -n-node $current`"
    n_elt="`geo -noverbose -size $current`"
    hmin="`geo -noverbose -hmin $current`"
    error_l2=`grep error_l2 $log | awk '{print $2}'`
    error_infinity=`grep error_infinity $log | awk '{print $2}'`
    error_h1=`grep error_h1 $log | awk '{print $2}'`

    line="  $i ${n_node} ${n_elt} $hmin $hmax ${error_l2} ${error_infinity} ${error_h1}"
    echo $line
    echo $line >> $dat
}
# ----------------------
# first computation
# ----------------------
current=$initial
crack $current.geo $approx > $current.field 2> $current.log
if test $? -ne 0; then
    echo "$0: failed at crack." 1>&2
    exit 1
fi
put_info 0 $current $current.log

# ----------------------
# start adaptation
# ----------------------
n=10
i=1
previous=$current
current="crack-a${i}"

while test ${i} -le ${n}; do

	# echo "${key_field} < $previous.field | ${field2bb} > $previous.${bb_suffix}"
	(${key_field} < $previous.field | ${field2bb} > $previous.${bb_suffix}) 2>/dev/null
	(${key_field} < $previous.field > $previous-key.field) 2>/dev/null
        ${field2bb} < $previous-key.field > $previous.${bb_suffix}

	/bin/rm -f $current.${bamg_suffix}
	bamg -v 8 						\
             -AbsError 						\
             -coef  ${bamg_coef} 				\
             -hmin  ${bamg_hmin} 				\
             -err   ${bamg_err} 				\
             -nbv   ${bamg_nbv} 				\
             -NbJacobi  5 					\
             -hmax  0.5        					\
             -ratio 0    					\
             -NbSmooth 10 					\
	     -b $previous.${bamg_suffix} 			\
	     -Mbb $previous.${bb_suffix} 			\
	     -o $current.${bamg_suffix} 			\
	 2>&1 > $current.bamglog

	/bin/rm -f $current.${bb_suffix}

	if test ! -f $current.${bamg_suffix}; then
	    echo "$0: failed at bamg." 1>&2
	    exit 1
	fi

	# bamg2geo $current.${bamg_suffix} $name.dmn > $current.geo 2>/dev/null
        cat $current.${bamg_suffix} $name.dmn | geo -input-bamg -upgrade -geo - > $current.geo 2>/dev/null
	if test $? -ne 0; then
	    echo "$0: failed at bamg2geo." 1>&2
	    exit 1
	fi

	crack $current.geo $approx > $current.field 2> $current.log
	if test $? -ne 0; then
	    echo "$0: failed at crack." 1>&2
	    exit 1
	fi
	put_info $i $current $current.log

	i=`expr $i + 1`
	previous=$current
	current="crack-a${i}"

done

