#!/usr/bin/env bash
#
# safety check for PWD 
#
if [ -z $PWD ] ; then
  echo "ERROR: \$PWD not set. The VASP test suite requires bash."
  exit 1 
fi 
#
LC_NUMERIC=C
#
ROOTDIR=$PWD
#
# check if testfolder is present
#
if [ ! -d $ROOTDIR/tests ] ; then
  echo "ERROR: $ROOTDIR/tests not found. It seems you are running runtest from another path than VASP_ROOT/testsuite."
  exit 1 
fi 

export VASP_TESTSUITE_FILES=/fsc/home/vasp/vasp_testsuite_files
# interpreter for arguments:
# ./runtest [-f|--fast|-a|--all] [config-file]
#

key="$1"
case $key in
    -f|--fast)
    # run FAST category test by default, 
    # except user does not want to run them
    if [ -z "$VASP_TESTSUITE_RUN_FAST"   ] ; then
       export VASP_TESTSUITE_RUN_FAST=Y
    fi 
    # non-empty VASP_TESTSUITE_TESTS over-rules category fast
    if [ ! -z "$VASP_TESTSUITE_TESTS" ] ; then
       export VASP_TESTSUITE_RUN_FAST=""
    fi 
    shift
    ;;
    -a|--all)
    if [ -z "$VASP_TESTSUITE_TESTS"   ] ; then
       VASP_TESTSUITE_TESTS=""
    fi 
    shift
    ;;
esac

# build number comparison tool if not compile yet
if [ ! -f ./tools/compare_numbertable_new ] ; then
   make numbertable 
fi

#
# default for VASP_TESTSUITE_TESTS (to-do list) and VASP_TESTSUITE_SKIP_TESTS (to-skip list)
#
[ -z "$VASP_TESTSUITE_TESTS" ] && export VASP_TESTSUITE_TESTS=""
# define unstable or unused tests here
skip_me=" bulk_CoO_wurzite_ISPIN=2_PBE0_nosym_RPR"
skip_me+=" bulk_CoO_wurzite_ISPIN=2_nosym_RPR"
skip_me+=" bulk_GaAs_G0W0_nosym_RPR"
skip_me+=" bulk_InP_SOC_DFT_nosym_RPR"
skip_me+=" bulk_SiO2_LOPTICS_nosym_RPR"
skip_me+=" bulk_SiO2_LPEAD_nosym_RPR"
skip_me+=" bulk_InP_SOC_PBE0_nosym_RPR"
skip_me+=" bulk_InP_SOC_G0W0_nosym_RPR"
skip_me+=" bulk_CoO_wurzite_SOC_PBE0_nosym_RPR"
skip_me+=" bulk_CoO_wurzite_SOC_PBE0_sym_y_RPR"
skip_me+=" bulk_CoO_wurzite_SOC_PBE0_sym_x_RPR"
skip_me+=" bulk_CoO_wurzite_SOC_PBE0_sym_z_RPR"
skip_me+=" bulk_GaAs_scGW0_nosym_RPR"
skip_me+=" bulk_GaAs_scGW_nosym_RPR"
skip_me+=" bulk_GaAs_GW0_nosym_RPR"
skip_me+=" N_GW"
skip_me+=" SrVO3_CRPA_disentangle"
skip_me+=" Co_CRPA_disentangle"
skip_me+=" SrVO3_CRPA_disentangle_RPR"
skip_me+=" Co_CRPA_disentangle_RPR"
skip_me+=" SiC_2E4W"
skip_me+=" LiH_MLFF_8atoms"
skip_me+=" LiH_MLFF_8atoms_RPR"
skip_me+=" FeAl_333_RPAFORCE"
skip_me+=" kopt_mos2_loptics"
skip_me+=" C_MLPOT"
skip_me+=" ML_H2O_NaCl_TI_ISTART0"
skip_me+=" ML_H2O_NaCl_TI_ISTART1"
skip_me+=" ML_H2O_NaCl_TI_ISTART2"
skip_me+=" phelel_si_ph_self_energy"
if [ -z "$VASP_TESTSUITE_SKIP_TESTS" ] ; then
  export VASP_TESTSUITE_SKIP_TESTS="$skip_me"
else
  VASP_TESTSUITE_SKIP_TESTS+="$skip_me"
fi 
#
# The aim of this test suite is to run VASP calculations and compare to benchmark results
# that were confirmed using different compilers and architectures.
#
# This script requires bash.
#
# Invoke with: runtest [ configfile ]
#
[ -n "$1" ] && source $1
#
# default for VASP_TESTSUITE_REFERENCE (when Y the reference files are generated)
#
[ -z "$VASP_TESTSUITE_REFERENCE" ] && export VASP_TESTSUITE_REFERENCE=
#
# default for interactive (when Y the tests are run interactively)
#
[ -z "$interactive" ] && export interactive=
#
# default position of the standard VASP executable
# (N.B.: only set here when $VASP_TESTSUITE_EXE_STD is not defined at all, i.e., not even empty)
#
[ -z "${VASP_TESTSUITE_EXE_STD+1}" ] && export VASP_TESTSUITE_EXE_STD="mpirun -np 4 $ROOTDIR/../bin/vasp_std"
#
# default position of the non collinear VASP executable
# (N.B.: only set here when $VASP_TESTSUITE_EXE_NCL is not defined at all, i.e., not even empty)
#
[ -z "${VASP_TESTSUITE_EXE_NCL+1}" ] && export VASP_TESTSUITE_EXE_NCL="mpirun -np 4 $ROOTDIR/../bin/vasp_ncl"
#
# If we do not have a non-collinear executable we skip the tests that need it
#
[ -z "${VASP_TESTSUITE_EXE_NCL}" ] && export VASP_TESTSUITE_SKIP_NCL=Y
#
# default position of the gamma-only VASP executable
# (N.B.: only set here when $VASP_TESTSUITE_EXE_GAM is not defined at all, i.e., not even empty)
#
[ -z "${VASP_TESTSUITE_EXE_GAM+1}" ] && export VASP_TESTSUITE_EXE_GAM="mpirun -np 4 $ROOTDIR/../bin/vasp_gam"
#
# If we do not have a gamma-only executable we skip the tests that need it
#
[ -z "${VASP_TESTSUITE_EXE_GAM}"  ] && export VASP_TESTSUITE_SKIP_GAMMA=Y
#
# default position of the POTCARs (specific to each test)
# (N.B.: only set here when $VASP_TESTSUITE_POTENTIALS is not defined at all, i.e., not even empty)
#
[ -z "${VASP_TESTSUITE_POTENTIALS+1}" ] && export VASP_TESTSUITE_POTENTIALS=$ROOTDIR/POTCARS
#
# If VASP_TESTSUITE_SKIP_VASP45 is not defined at all, i.e., not even empty,
# then it defaults to VASP_TESTSUITE_SKIP_VASP45=Y (pretty much deprecated)
#
[ -z "${VASP_TESTSUITE_SKIP_VASP45+1}" ] && export VASP_TESTSUITE_SKIP_VASP45=Y
#
# a small tool that helps in spotting and quantifying different outputs
#
export COMPARE_NUMBERTABLE=$ROOTDIR/tools/call_compare
#
# dont skip LREAL=A tests per default 
#
[ -z "$VASP_TESTSUITE_SKIP_LREAL" ] && export VASP_TESTSUITE_SKIP_LREAL=
#
# Skip the phelel tests, generation of the electron-phonon potentials phelel_params.hdf5
#
[ -z "${VASP_TESTSUITE_SKIP_PHELEL+1}" ] && export VASP_TESTSUITE_SKIP_PHELEL=Y
#
# Skip the test requiring Python unless requested
#
[ -z "${VASP_TESTSUITE_SKIP_PYTHON+1}" ] && export VASP_TESTSUITE_SKIP_PYTHON=Y
#
# initialize ERROR_LIST 
ERROR_LIST=""
#
#
#  Specify TOLERANCE VALUES for the following properties here:
#  forces, phonon frequencies, correlation energies, bulk moduli,
#  dielectric matrices, GW-eigenvalues
#
#  Why do we need to specify tolerance thresholds for properties
#  other than the groundstate energy?
#  Note that most other properties than the groundstate energy
#  are usually not checked for convergence in the selfconsistency
#  loop of VASP. Therefore these properties can vary slightly
#  depending on the compiler and the architecture.
#  Sensible thresholds that account for these variations are
#  listed below.
#

#############################################################
# N.B. : Tolerances MUST NOT be specified in INTEGER  format.
#############################################################
#tolerance value for forces and energies in eV/A and eV, respectively
TOL_FORCE="0.0005"
#tolerance value for cartesian positions in A
TOL_POSITIONS="0.0005"
#tolerance value for eigenenergies in eV
TOL_EIGV="0.001"
#tolerance value for stress in kB
TOL_STRESS="0.1"
#tolerance value for elastic moduli in ?
TOL_ELAMOD="0.1"
#tolerance value for phonon frequencies in cm^-1
TOL_PHONFREQ="0.25"
#tolerance value for dielectric matrices in ?
TOL_DIELECTRIC="0.005"
#toletance for piezo electric tensors in C/m^2
TOL_PIEZO="0.005"
#tolerance value for GW output
TOL_GW="0.005"
#tolerance value for RPA and MP2 correlation energies in eV
TOL_ECORR="0.001"
#tolerance value for RPA Force calculations
TOL_RPA_FORCE="0.005"
#tolerance value for CRPA and NLRPA U and J energies in eV
TOL_CRPA="0.01"
#tolerance value for Wannier90 eigenvalues in eV
TOL_WAN90="0.005"
#tolerance for optimized projectors
TOL_PROJ="0.001"
#tolerance vallue for eigenvalues difference in KOPT
TOL_KOPT="0.001"
#tolerance for energy in machine learning force fields
TOL_MLFF_ENE="0.0001"
#tolerance value for OEP eigen energies
TOL_OEP="0.05"
#tolerance for energy in machine learning force fields
TOL_ML_FF_EN="0.001"
#tolerance for forces in machine learning force fields
TOL_ML_FF_FOR="0.01"
#tolerance for stress in machine learning force fields
TOL_ML_FF_STRESS="0.01"
#tolerance for heat flux in machine learning force fields
TOL_ML_FF_HEAT="0.001"
#tolerance for TI in ML_LOGFILE in machine learning force fields
TOL_ML_FF_TI="0.01"
#tolerance for real values in ML_ABN file in machine learning force fields
TOL_ML_FF_ABN="0.001"

#############################################################
# strict settings
#############################################################
# TOL_FORCE="0.0001"
# TOL_PHONFREQ="0.1"

#----------------------------------------------------------------------
# end of costumizable part
#----------------------------------------------------------------------
export WORK=$ROOTDIR/tests

#----------------------------------------------------------------------
function echox
#----------------------------------------------------------------------
{
    echo $*
}

#----------------------------------------------------------------------
echox ==================================================================
echox VASP TESTSUITE SHA: $(git log -1 --pretty=format:"%h" )
echox 
echox Reference files have been generated with 4 MPI ranks.
echox Note that tests might fail if an other number of ranks is used!
echox
echox Executables and additional INCAR tags used for this test:
echox 
echox "VASP_TESTSUITE_EXE_STD=\"$VASP_TESTSUITE_EXE_STD\" "
echox "VASP_TESTSUITE_EXE_NCL=\"$VASP_TESTSUITE_EXE_NCL\" "
echox "VASP_TESTSUITE_EXE_GAM=\"$VASP_TESTSUITE_EXE_GAM\" "
echox "VASP_TESTSUITE_INCAR_PREPEND=\"$VASP_TESTSUITE_INCAR_PREPEND\" " 
echox "VASP_TESTSUITE_REFERENCE=\"$VASP_TESTSUITE_REFERENCE\""
echox "VASP_TESTSUITE_SKIP_HYB=\"$VASP_TESTSUITE_SKIP_HYB\""
echox "VASP_TESTSUITE_SKIP_NCL=\"$VASP_TESTSUITE_SKIP_NCL\""
echox "VASP_TESTSUITE_SKIP_SOC=\"$VASP_TESTSUITE_SKIP_SOC\""
echox "VASP_TESTSUITE_SKIP_MD=\"$VASP_TESTSUITE_SKIP_MD\""
echox "VASP_TESTSUITE_SKIP_TBMD=\"$VASP_TESTSUITE_SKIP_TBMD\""
echox "VASP_TESTSUITE_SKIP_RPA=\"$VASP_TESTSUITE_SKIP_RPA\""
echox "VASP_TESTSUITE_SKIP_GW=\"$VASP_TESTSUITE_SKIP_GW\""
echox "VASP_TESTSUITE_SKIP_ACFDT=\"$VASP_TESTSUITE_SKIP_ACFDT\""
echox "VASP_TESTSUITE_SKIP_CRPA=\"$VASP_TESTSUITE_SKIP_CRPA\""
echox "VASP_TESTSUITE_SKIP_BSE=\"$VASP_TESTSUITE_SKIP_BSE\""
echox "VASP_TESTSUITE_SKIP_NOSYM=\"$VASP_TESTSUITE_SKIP_NOSYM\""
echox "VASP_TESTSUITE_SKIP_VASP6=\"$VASP_TESTSUITE_SKIP_VASP6\""
echox "VASP_TESTSUITE_SKIP_GAMMA=\"$VASP_TESTSUITE_SKIP_GAMMA\""
echox "VASP_TESTSUITE_SKIP_VASP45=\"$VASP_TESTSUITE_SKIP_VASP45\""
echox "VASP_TESTSUITE_SKIP_VASP46=\"$VASP_TESTSUITE_SKIP_VASP46\""
echox "VASP_TESTSUITE_SKIP_LREAL=\"$VASP_TESTSUITE_SKIP_LREAL\""
echox "VASP_TESTSUITE_SKIP_LRESP=\"$VASP_TESTSUITE_SKIP_LRESP\""
echox "VASP_TESTSUITE_SKIP_LTMP2=\"$VASP_TESTSUITE_SKIP_LTMP2\""
echox "VASP_TESTSUITE_SKIP_PEAD=\"$VASP_TESTSUITE_SKIP_PEAD\""
echox "VASP_TESTSUITE_SKIP_NCORE1=\"$VASP_TESTSUITE_SKIP_NCORE1\""
echox "VASP_TESTSUITE_SKIP_ELPHON=\"$VASP_TESTSUITE_SKIP_ELPHON\""
echox "VASP_TESTSUITE_SKIP_WAN90=\"$VASP_TESTSUITE_SKIP_WAN90\""
echox "VASP_TESTSUITE_SKIP_KOPT=\"$VASP_TESTSUITE_SKIP_KOPT\""
echox "VASP_TESTSUITE_SKIP_ML=\"$VASP_TESTSUITE_SKIP_ML\""
echox "VASP_TESTSUITE_SKIP_LIBXC=\"$VASP_TESTSUITE_SKIP_LIBXC\""
echox "VASP_TESTSUITE_SKIP_PYTHON=\"$VASP_TESTSUITE_SKIP_PYTHON\""
echox "VASP_TESTSUITE_SKIP_FAST=\"$VASP_TESTSUITE_SKIP_FAST\""
echox "VASP_TESTSUITE_SKIP_PHELEL=\"$VASP_TESTSUITE_SKIP_PHELEL\""
echox "VASP_TESTSUITE_SKIP_HDF5=\"$VASP_TESTSUITE_SKIP_HDF5\""
echox "VASP_TESTSUITE_SKIP_EFOR=\"$VASP_TESTSUITE_SKIP_EFOR\""
echox "VASP_TESTSUITE_RUN_HYB=\"$VASP_TESTSUITE_RUN_HYB\""
echox "VASP_TESTSUITE_RUN_NCL=\"$VASP_TESTSUITE_RUN_NCL\""
echox "VASP_TESTSUITE_RUN_SOC=\"$VASP_TESTSUITE_RUN_SOC\""
echox "VASP_TESTSUITE_RUN_MD=\"$VASP_TESTSUITE_RUN_MD\""
echox "VASP_TESTSUITE_RUN_TBMD=\"$VASP_TESTSUITE_RUN_TBMD\""
echox "VASP_TESTSUITE_RUN_RPA=\"$VASP_TESTSUITE_RUN_RPA\""
echox "VASP_TESTSUITE_RUN_GW=\"$VASP_TESTSUITE_RUN_GW\""
echox "VASP_TESTSUITE_RUN_ACFDT=\"$VASP_TESTSUITE_RUN_ACFDT\""
echox "VASP_TESTSUITE_RUN_CRPA=\"$VASP_TESTSUITE_RUN_CRPA\""
echox "VASP_TESTSUITE_RUN_BSE=\"$VASP_TESTSUITE_RUN_BSE\""
echox "VASP_TESTSUITE_RUN_NOSYM=\"$VASP_TESTSUITE_RUN_NOSYM\""
echox "VASP_TESTSUITE_RUN_VASP6=\"$VASP_TESTSUITE_RUN_VASP6\""
echox "VASP_TESTSUITE_RUN_GAMMA=\"$VASP_TESTSUITE_RUN_GAMMA\""
echox "VASP_TESTSUITE_RUN_LREAL=\"$VASP_TESTSUITE_RUN_LREAL\""
echox "VASP_TESTSUITE_RUN_LRESP=\"$VASP_TESTSUITE_RUN_LRESP\""
echox "VASP_TESTSUITE_RUN_LTMP2=\"$VASP_TESTSUITE_RUN_LTMP2\""
echox "VASP_TESTSUITE_RUN_PEAD=\"$VASP_TESTSUITE_RUN_PEAD\""
echox "VASP_TESTSUITE_RUN_NCORE1=\"$VASP_TESTSUITE_RUN_NCORE1\""
echox "VASP_TESTSUITE_RUN_ELPHON=\"$VASP_TESTSUITE_RUN_ELPHON\""
echox "VASP_TESTSUITE_RUN_WAN90=\"$VASP_TESTSUITE_RUN_WAN90\""
echox "VASP_TESTSUITE_RUN_KOPT=\"$VASP_TESTSUITE_RUN_KOPT\""
echox "VASP_TESTSUITE_RUN_ML=\"$VASP_TESTSUITE_RUN_ML\""
echox "VASP_TESTSUITE_RUN_LIBXC=\"$VASP_TESTSUITE_RUN_LIBXC\""
echox "VASP_TESTSUITE_RUN_PYTHON=\"$VASP_TESTSUITE_RUN_PYTHON\""
echox "VASP_TESTSUITE_RUN_FAST=\"$VASP_TESTSUITE_RUN_FAST\""
echox "VASP_TESTSUITE_RUN_PHELEL=\"$VASP_TESTSUITE_RUN_PHELEL\""
echox "VASP_TESTSUITE_RUN_HDF5=\"$VASP_TESTSUITE_RUN_HDF5\""
echox "VASP_TESTSUITE_RUN_EFOR=\"$VASP_TESTSUITE_RUN_EFOR\""
echox
echox Executed at: `date +%H_%M_%D`
echox ==================================================================
#----------------------------------------------------------------------
# look up test found in VASP_TESTSUITE_TESTS
function ShallIDoTest
#----------------------------------------------------------------------
{
   # in interactive mode user selects every test by hand 
   if [ "$interactive" = "Y" ]; then
      echo "execute this test (Y/N=default) ?"
      read var
      if [ "$var" = "Y" -o "$var" = "y" ]
      then
         DoTest=Y
      else
         DoTest=N
      fi
   else
      if [ ! -z "$VASP_TESTSUITE_TESTS" ] ; then
         # check if passed test is listed
         IsListed=N
         for test in $VASP_TESTSUITE_TESTS ; do 
            if [ "$1" == "$test" ] ; then
               IsListed=Y
               break
            fi
         done
      else
         # test passed by default, i.e. VASP_TESTSUITE_TESTS is empty string 
         IsListed=Y
         # skip tests optionally
         if [ ! -z "$VASP_TESTSUITE_SKIP_TESTS" ] ; then
            for test in $VASP_TESTSUITE_SKIP_TESTS ; do
               if [ "$1" == "$test" ] ; then
                  IsListed=N
                  break
               fi
            done
         fi
      fi
      # the variable CATEGORY overrules the listed test 
      do_this=Y
      # skip tests of a particular type:
      if [[ $CATEGORY =~ .*HYB.*      ]] && [[ $VASP_TESTSUITE_SKIP_HYB    = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*NCL.*      ]] && [[ $VASP_TESTSUITE_SKIP_NCL    = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*SOC.*      ]] && [[ $VASP_TESTSUITE_SKIP_SOC    = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*MD.*       ]] && [[ $VASP_TESTSUITE_SKIP_MD     = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*TBMD.*     ]] && [[ $VASP_TESTSUITE_SKIP_TBMD   = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*RPA.*      ]] && [[ $VASP_TESTSUITE_SKIP_RPA    = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*GW.*       ]] && [[ $VASP_TESTSUITE_SKIP_GW     = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*ACFDT.*    ]] && [[ $VASP_TESTSUITE_SKIP_ACFDT  = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*CRPA.*     ]] && [[ $VASP_TESTSUITE_SKIP_CRPA   = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*BSE.*      ]] && [[ $VASP_TESTSUITE_SKIP_BSE    = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*NOSYM.*    ]] && [[ $VASP_TESTSUITE_SKIP_NOSYM  = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*VASP6.*    ]] && [[ $VASP_TESTSUITE_SKIP_VASP6  = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*GAMMA.*    ]] && [[ $VASP_TESTSUITE_SKIP_GAMMA  = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*FAST.*     ]] && [[ $VASP_TESTSUITE_SKIP_FAST   = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*VASP45.*   ]] && [[ $VASP_TESTSUITE_SKIP_VASP45 = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*VASP46.*   ]] && [[ $VASP_TESTSUITE_SKIP_VASP46 = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*LREAL.*    ]] && [[ $VASP_TESTSUITE_SKIP_LREAL  = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*LRESP.*    ]] && [[ $VASP_TESTSUITE_SKIP_LRESP  = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*LTMP2.*    ]] && [[ $VASP_TESTSUITE_SKIP_LTMP2  = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*PEAD.*     ]] && [[ $VASP_TESTSUITE_SKIP_PEAD   = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*NCORE1.*   ]] && [[ $VASP_TESTSUITE_SKIP_NCORE1 = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*HDF5.*     ]] && [[ $VASP_TESTSUITE_SKIP_HDF5   = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*ELPHON.*   ]] && [[ $VASP_TESTSUITE_SKIP_ELPHON = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*WAN90.*    ]] && [[ $VASP_TESTSUITE_SKIP_WAN90  = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*KOPT.*     ]] && [[ $VASP_TESTSUITE_SKIP_KOPT   = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*ML.*       ]] && [[ $VASP_TESTSUITE_SKIP_ML     = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*LIBXC.*    ]] && [[ $VASP_TESTSUITE_SKIP_LIBXC  = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*PYTHON.*   ]] && [[ $VASP_TESTSUITE_SKIP_PYTHON = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*PHELEL.*   ]] && [[ $VASP_TESTSUITE_SKIP_PHELEL = Y ]] ; then do_this=N ; fi
      if [[ $CATEGORY =~ .*EFOR.*     ]] && [[ $VASP_TESTSUITE_SKIP_EFOR   = Y ]] ; then do_this=N ; fi
      # skip all tests that are not of a particular type:
      if [[ ! $CATEGORY =~ .*HYB.*    ]] && [[ $VASP_TESTSUITE_RUN_HYB     = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*NCL.*    ]] && [[ $VASP_TESTSUITE_RUN_NCL     = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*SOC.*    ]] && [[ $VASP_TESTSUITE_RUN_SOC     = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*MD.*     ]] && [[ $VASP_TESTSUITE_RUN_MD      = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*TBMD.*   ]] && [[ $VASP_TESTSUITE_RUN_TBMD    = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*RPA.*    ]] && [[ $VASP_TESTSUITE_RUN_RPA     = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*GW.*     ]] && [[ $VASP_TESTSUITE_RUN_GW      = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*ACFDT.*  ]] && [[ $VASP_TESTSUITE_RUN_ACFDT   = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*CRPA.*   ]] && [[ $VASP_TESTSUITE_RUN_CRPA    = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*BSE.*    ]] && [[ $VASP_TESTSUITE_RUN_BSE     = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*NOSYM.*  ]] && [[ $VASP_TESTSUITE_RUN_NOSYM   = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*VASP6.*  ]] && [[ $VASP_TESTSUITE_RUN_VASP6   = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*GAMMA.*  ]] && [[ $VASP_TESTSUITE_RUN_GAMMA   = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*FAST.*   ]] && [[ $VASP_TESTSUITE_RUN_FAST    = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*LREAL.*  ]] && [[ $VASP_TESTSUITE_RUN_LREAL   = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*LRESP.*  ]] && [[ $VASP_TESTSUITE_RUN_LRESP   = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*LTMP2.*  ]] && [[ $VASP_TESTSUITE_RUN_LTMP2   = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*PEAD.*   ]] && [[ $VASP_TESTSUITE_RUN_PEAD    = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*NCORE1.* ]] && [[ $VASP_TESTSUITE_RUN_NCORE1  = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*HDF5.*   ]] && [[ $VASP_TESTSUITE_RUN_HDF5    = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*ELPHON.* ]] && [[ $VASP_TESTSUITE_RUN_ELPHON  = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*WAN90.*  ]] && [[ $VASP_TESTSUITE_RUN_WAN90   = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*KOPT.*   ]] && [[ $VASP_TESTSUITE_RUN_KOPT    = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*ML.*     ]] && [[ $VASP_TESTSUITE_RUN_ML      = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*LIBXC.*  ]] && [[ $VASP_TESTSUITE_RUN_LIBXC   = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*PYTHON.* ]] && [[ $VASP_TESTSUITE_RUN_PYTHON  = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*PHELEL.* ]] && [[ $VASP_TESTSUITE_RUN_PHELEL  = Y ]] ; then do_this=N ; fi
      if [[ ! $CATEGORY =~ .*EFOR.*   ]] && [[ $VASP_TESTSUITE_RUN_EFOR    = Y ]] ; then do_this=N ; fi

      # in order to perform the test, the passed test must be in the list
      # and in the the right CATEGORY 
      if [ "$do_this" == "Y" ] && [ "$IsListed" == "Y" ] ; then
         DoTest=Y
      else
         DoTest=N
#         echo SKIPPING: $1 
      fi 

   fi  # interactive selction

   # checkout if directory exists at all 
   if [ ! -d $WORK/$1 ] ; then 
      echo "SKIPPING: $1, directory does not exist"
      DoTest=N
   fi  
}
#----------------------------------------------------------------------
function echoxr
#----------------------------------------------------------------------
{
    echox $*
    echox ------------------------------------------------------------------
    echo $* >&2
}
#----------------------------------------------------------------------
# DEPRECATED 
function check_energy_OLD
#----------------------------------------------------------------------
{
    grep F= OSZICAR     | awk '{ printf "%4d %2s %10.5f %2s %10.5f\n", $1,$2,$3,$4,$5 }' > oszi
    grep F= OSZICAR.ref | awk '{ printf "%4d %2s %10.5f %2s %10.5f\n", $1,$2,$3,$4,$5 }' > oszi.ref
    cat oszi > oszi3
    echo "--" >> oszi3
    cat oszi.ref >> oszi3
    

    diff oszi oszi.ref > diff
    awk '
    /--/  { compare=1 ; ifirst = i ; i = 0 }
    !/--/ { i=i+1 
       ff=$3
       ee=$5
       if (compare == 1) {
          if ( e[i]-ee > 0.001 || ee-e[i] > 0.001 ) print "entry",$4,"differs by ",e[i]-ee, " in iteration",i
          if ( f[i]-ff > 0.001 || ff-f[i] > 0.001 ) print "entry",$2,"differs by ",f[i]-ff, " in iteration",i
       } else {
          e[i] = ee
          f[i] = ff
       }
    } 
    END { 
       if (i!= ifirst) print "the number if ionic iterations differs",i,ifirst
    }'  <oszi3 >diff3

    echox "the energies are:" 
    cat oszi

    if [ -z "`cat diff`" ]
    then
       echox 
       echox "the energies are correct, run successful"
       echox "----------------------------------------"
    else
       if  [ -z "`cat diff3`" ] ; then
         echox "WARNING: the test yields slightly different energies, please check"
         echox "------------------------------------------------------------------"
       else
         echox "ERROR: the test yields different energies"
         echox "-----------------------------------------"
         cat diff3
	 echo "$1" >  FAILED
       fi
       echox "differences using diff:"
       cat diff
    fi
}
#----------------------------------------------------------------------
function check_energy
#----------------------------------------------------------------------
{
     if [ -z $2 ] ; then
        file=OUTCAR
     else
        file=OUTCAR.$2
     fi 
     awk '
        /free  energy/ { printf "%20.8f\n", $5 }
        /energy  without/ { printf "%20.8f\n", $7 }
        /ETOTAL/ { printf "%20.8f\n", $5 }
        ' <$file >energy_outcar
     awk '
        /free  energy/ { printf "%20.8f\n", $5 }
        /energy  without/ { printf "%20.8f\n", $7 }
        /ETOTAL/ { printf "%20.8f\n", $5 }
        ' <$file.ref >energy_outcar.ref

    $COMPARE_NUMBERTABLE energy_outcar  energy_outcar.ref $TOL_FORCE > diff

    if [ -z "`cat diff`" ]
    then
       echox "the energies are:"
       cat energy_outcar
       echox "the energies are correct, run successful"
       rm energy_outcar energy_outcar.ref 
    else
       echox "ERROR: the test yields different results for the energies, please check"
       echox "-----------------------------------------------------------------------"
       paste energy_outcar energy_outcar.ref
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_eigenvalues
#----------------------------------------------------------------------
{
    awk '
        /NBANDS=/ {nbands=$15}
        /Fermi energy:/ {efermi=$3}
        scan==1 { line=line+1
                  if (line>0) printf "%10.5f\n",$2-efermi
                  if (line==nbands) scan=0 }
        / band No.  band energies/ { scan=1 ; line=0 }' < OUTCAR     > eigenvalues

    awk '
        /NBANDS=/ {nbands=$15}
        /Fermi energy:/ {efermi=$3}
        scan==1 { line=line+1
                  if (line>0) printf "%10.5f\n",$2-efermi
                  if (line==nbands) scan=0 }
        / band No.  band energies/ { scan=1 ; line=0 }' < OUTCAR.ref > eigenvalues.ref

    $COMPARE_NUMBERTABLE eigenvalues eigenvalues.ref $TOL_EIGV > diff

    if [ -z "`cat diff`" ]
    then
       echox "the eigenenergies are:"
       cat eigenvalues | head -10
       echox "the eigenenergies are correct, run successful"
    else
       echox "ERROR: the test yields different results for the eigenenergies, please check"
       echox "----------------------------------------------------------------------------"
       cat eigenvalues | head -10
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_frequ
#----------------------------------------------------------------------
{
    grep "f  =" OUTCAR     | awk '{ printf "f= %10.2f cm-1\n", $8 }' > freq
    grep "f  =" OUTCAR.ref | awk '{ printf "f= %10.2f cm-1\n", $8 }' > freq.ref
    #take imaginary frequency also into account 
    grep "f/i=" OUTCAR     | awk '{ printf "f= %10.2f cm-1\n", $7 }' >> freq
    grep "f/i=" OUTCAR.ref | awk '{ printf "f= %10.2f cm-1\n", $7 }' >> freq.ref
    

    $COMPARE_NUMBERTABLE freq freq.ref $TOL_PHONFREQ > diff

    if [ -z "`cat diff`" ]
    then
       echox "the frequencies are:" 
       cat freq
       echox "the frequencies are correct, run successful"
       echox "-------------------------------------------"
    else
       echox "ERROR: the frequencies are different, please check"
       echox "--------------------------------------------------"
       cat freq
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_phonon_freq
#----------------------------------------------------------------------
{
    grep '\[cm-1\]' OUTCAR     | awk '{$1=""; print $0}' > phonon_freq
    grep '\[cm-1\]' OUTCAR.ref | awk '{$1=""; print $0}' > phonon_freq.ref

    $COMPARE_NUMBERTABLE phonon_freq phonon_freq.ref $TOL_PHONFREQ > diff

    if [ -z "`cat diff`" ]
    then
       echox "the phonon dispersion frequencies are:"
       cat phonon_freq
       echox "the frequencies are correct, run successful"
       echox "-------------------------------------------"
    else
       echox "ERROR: the phonon dispersions frequencies are different, please check"
       echox "--------------------------------------------------"
       cat phonon_freq
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_forces
#----------------------------------------------------------------------
{
     if [ -z $2 ] ; then
        file=OUTCAR
     else
        file=OUTCAR.$2
     fi 
     awk '
         /NIONS =/         { nions=$12 }
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.5f %10.5f %10.5f\n", $4,$5,$6 
                             if (line==nions) scan=0 }
        /TOTAL-FORCE / { scan=1 ; line=-1 }' <$file >force
     awk '
         /NIONS =/         { nions=$12 }
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.5f %10.5f %10.5f\n", $4,$5,$6 
                             if (line==nions) scan=0 }
        /TOTAL-FORCE / { scan=1 ; line=-1 }' <$file.ref >force.ref

    $COMPARE_NUMBERTABLE force force.ref $TOL_FORCE > diff

    if [ -z "`cat diff`" ]
    then
       echox "the forces are:" 
       cat force
       echox "the forces are correct, run successful"
       rm force force.ref
    else
       echox "ERROR: the test yields different results for the forces, please check"
       echox "---------------------------------------------------------------------"
       cat force
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_positions
#----------------------------------------------------------------------
{
     if [ -z $2 ] ; then
        file=OUTCAR
     else
        file=OUTCAR.$2
     fi 
     awk '
         /NIONS =/         { nions=$12 }
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.5f %10.5f %10.5f\n", $1,$2,$3 
                             if (line==nions) scan=0 }
        /POSITION / { scan=1 ; line=-1 }' <$file >positions
     awk '
         /NIONS =/         { nions=$12 }
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.5f %10.5f %10.5f\n", $1,$2,$3 
                             if (line==nions) scan=0 }
        /TOTAL-FORCE / { scan=1 ; line=-1 }' <$file.ref >positions.ref

    $COMPARE_NUMBERTABLE positions positions.ref $TOL_POSITIONS > diff

    if [ -z "`cat diff`" ]
    then
       echox "the cartesian positions are:" 
       cat positions
       echox "the positions are correct, run successful"
       rm positions positions.ref
    else
       echox "ERROR: the test yields different results for the cartesian positions, please check"
       echox "---------------------------------------------------------------------"
       cat positions
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_RPA_forces
#----------------------------------------------------------------------
{
     awk '
         /NIONS =/         { nions=$12 }
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.5f %10.5f %10.5f\n", $4,$5,$6 
                             if (line==nions) scan=0 }
        /TOTAL RPA FORCE / { scan=1 ; line=-1 }' <OUTCAR >force
     awk '
         /NIONS =/         { nions=$12 }
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.5f %10.5f %10.5f\n", $4,$5,$6 
                             if (line==nions) scan=0 }
        /TOTAL RPA FORCE / { scan=1 ; line=-1 }' <OUTCAR.ref >force.ref

    $COMPARE_NUMBERTABLE force force.ref $TOL_RPA_FORCE > diff

    if [ -z "`cat diff`" ]
    then
       echox "the forces are:" 
       cat force
       echox "the forces are correct, run successful"
    else
       echox "ERROR: the test yields different results for the forces, please check"
       echox "---------------------------------------------------------------------"
       cat force
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_energy_OUTCAR
#----------------------------------------------------------------------
{
     awk '
        /HF-free energy/ { printf "%10s %10s %10.5f\n", $1,$2,$5 }
        /HF single shot energy change/ { printf "%10s %10s %10.5f\n", $1,$2,$6 }
        /renormalized HF singles/ { printf "%10s %10s %10.5f\n", $1,$2,$4 }
        /Trace of density matrix/ { printf "%10s %10s %10.5f\n", $1,$3,$6 }
        /HF\+RPA corr. energy TOTEN/ { printf "%10s %10s %10.5f\n", "HF+","RPA_cut ",$6 }
        /HF\+E_corr/ { printf "%10s %10s %10.5f\n", "HF+","RPA_inf",$3 }
        /Luttinger-Ward free energy/ { printf "%10s %10s %10.5f\n", "Luttinger","Ward  ",$6 }
        /TOTEGM/ { printf "%10s %10s %10.5f\n", "Galitskii","Midgal",$6 }
        /ECHEMN/ { printf "%10s %10s %10.5f\n", "chem.","pot",$5+$6 }
        /free  energy   TOTEN/ { printf "%10s %10.5f\n", $3,$5 }
        /ACFDT corr.    EXHF_ACFDT/    { printf "%10s %10s %10.5f\n", $1,$3,$5 }' <OUTCAR >energy
     awk '
        /HF-free energy/ { printf "%10s %10s %10.5f\n", $1,$2,$5 }
        /HF single shot energy change/ { printf "%10s %10s %10.5f\n", $1,$2,$6 }
        /renormalized HF singles/ { printf "%10s %10s %10.5f\n", $1,$2,$4 }
        /Trace of density matrix/ { printf "%10s %10s %10.5f\n", $1,$3,$6 }
        /HF\+RPA corr. energy TOTEN/ { printf "%10s %10s %10.5f\n", "HF+","RPA_cut ",$6 }
        /HF\+E_corr/ { printf "%10s %10s %10.5f\n", "HF+","RPA_inf",$3 }
        /Luttinger-Ward free energy/ { printf "%10s %10s %10.5f\n", "Luttinger","Ward  ",$6 }
        /TOTEGM/ { printf "%10s %10s %10.5f\n", "Galitskii","Midgal",$6 }
        /ECHEMN/ { printf "%10s %10s %10.5f\n", "chem.","pot",$5+$6 }
        /free  energy   TOTEN/ { printf "%10s %10.5f\n", $3,$5 }
        /ACFDT corr.    EXHF_ACFDT/    { printf "%10s %10s %10.5f\n", $1,$3,$5 }' <OUTCAR.ref >energy.ref

    $COMPARE_NUMBERTABLE energy  energy.ref $TOL_FORCE > diff

    if [ -z "`cat diff`" ]
    then
       echox "the energies are:"
       cat energy
       echox "the energies are correct, run successful"
    else
       echox "ERROR: the test yields different results for the energies, please check"
       echox "-----------------------------------------------------------------------"
       cat energy
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_stress
#----------------------------------------------------------------------
{
     if [ -z $2 ] ; then
        file=OUTCAR
     else
        file=OUTCAR.$2
     fi 
    grep "in kB" $file     | awk '{ printf "%10.2f %10.2f %10.2f %10.2f %10.2f %10.2f\n",$3,$4,$5,$6,$7,$8,$9 }' > stress
    grep "in kB" $file.ref | awk '{ printf "%10.2f %10.2f %10.2f %10.2f %10.2f %10.2f\n",$3,$4,$5,$6,$7,$8,$9 }' > stress.ref
    

    $COMPARE_NUMBERTABLE stress stress.ref $TOL_STRESS > diff

    if [ -z "`cat diff`" ]
    then
       echox "the stress tensor is:" 
       cat stress
       echox "the tensor is correct, run successful"
       rm stress stress.ref
    else
       echox "ERROR: the stress tensor is different, please check"
       echox "---------------------------------------------------"
       cat stress
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_vacuum
#----------------------------------------------------------------------
{
    grep "vacuum level"  OUTCAR | awk '{ print $6 "  " $ 7 }'  > vacuum
    grep "vacuum level"  OUTCAR.ref | awk '{ print $6 "  " $ 7 }' >vacuum.ref

    $COMPARE_NUMBERTABLE vacuum vacuum.ref > diff

    if [ -z "`cat diff`" ]
    then
       echox "the vacuum levels are " `cat vacuum`
       echox "the vacuum levels are correct, test successful"
       echox "----------------------------------------------"
    else
       echox "ERROR: the test yields different results for vacuum level, please check"
       echox "-----------------------------------------------------------------------"
       cat diff
       echo "$1" >  FAILED
    fi
    rm vacuum vacuum.ref
}
#----------------------------------------------------------------------
function check_dipole
#----------------------------------------------------------------------
{
    awk <OUTCAR '
       BEGIN { store=1 }
       /dipolmoment/  { store= 0 ; printf "%10.2f %10.2f %10.2f\n",$2,$3,$4 }  
       /direction  1 min pos/ { x=$7}
       /direction  2 min pos/ { y=$7}
       /direction  3 min pos/ { z=$7}
       END { if (store==1)  printf "%10.2f %10.2f %10.2f\n",x,y,z }' | tail -1 >dipole
    awk <OUTCAR.ref '
       BEGIN { store=1 }
       /dipolmoment/  { store= 0 ; printf "%10.2f %10.2f %10.2f\n",$2,$3,$4 }  
       /direction  1 min pos/ { x=$7}
       /direction  2 min pos/ { y=$7}
       /direction  3 min pos/ { z=$7}
       END { if (store==1)  printf "%10.2f %10.2f %10.2f\n",x,y,z }' | tail -1 >dipole.ref

    $COMPARE_NUMBERTABLE dipole dipole.ref > diff

    if [ -z "`cat diff`" ]
    then
       echox "the dipole moments are:" `cat dipole`
       echox "the dipole moments are correct, test successful"
    else
       echox "ERROR: the test yields different results for the dipole moments, please check"
       echox "-----------------------------------------------------------------------------"
       cat diff
       echo "$1" >  FAILED
    fi
    rm dipole dipole.ref
}
#----------------------------------------------------------------------
function check_epsilon
#----------------------------------------------------------------------
{
    grep -A 4 "STATIC DIELECTRIC TENSOR (including local field effects in DFT)" OUTCAR | tail -3 >epsilon
    grep -A 4 "STATIC DIELECTRIC TENSOR (including local field effects in DFT)" OUTCAR.ref | tail -3 >epsilon.ref

    grep -A 4 "STATIC DIELECTRIC TENSOR (INDEPENDENT PARTICLE, excluding Hartree and local field effects)" OUTCAR | tail -3 >>epsilon
    grep -A 4 "STATIC DIELECTRIC TENSOR (INDEPENDENT PARTICLE, excluding Hartree and local field effects)" OUTCAR.ref | tail -3 >>epsilon.ref

    grep -A 4 "STATIC DIELECTRIC TENSOR IONIC CONTRIBUTION" OUTCAR | tail -3 >>epsilon
    grep -A 4 "STATIC DIELECTRIC TENSOR IONIC CONTRIBUTION" OUTCAR.ref | tail -3 >>epsilon.ref

    grep -A 4 "MACROSCOPIC STATIC DIELECTRIC TENSOR (including local field effects)" OUTCAR | tail -3 >>epsilon
    grep -A 4 "MACROSCOPIC STATIC DIELECTRIC TENSOR (including local field effects)" OUTCAR.ref | tail -3 >>epsilon.ref

    $COMPARE_NUMBERTABLE epsilon epsilon.ref $TOL_DIELECTRIC > diff

    if [ -z "`cat diff`" ]
    then
       echox "the dielectric tensors are:" 
       cat epsilon
       echox "the dielectric tensors are correct, run successful"
    else
       echox "ERROR: the test yields different results for the dielectric tensors, please check"
       echox "---------------------------------------------------------------------------------"
       cat epsilon
       cat diff
       echo "$1" >  FAILED
    fi
##    rm epsilon epsilon.ref
}
#----------------------------------------------------------------------
function check_piezo
#----------------------------------------------------------------------
{
#    echo "PIEZOELECTRIC TENSOR (electronic contribution)" >piezo
    grep -A 5 "PIEZOELECTRIC TENSOR (including local field effects)  for field in x, y, z        (C/m^2)" OUTCAR | tail -3 | \
       awk '{printf "%10.3f %10.3f %10.3f %10.3f %10.3f %10.3f\n",$2,$3,$4,$5,$6,$7}' >piezo
#    echo "PIEZOELECTRIC TENSOR (electronic contribution)" >piezo.ref
    grep -A 5 "PIEZOELECTRIC TENSOR (including local field effects)  for field in x, y, z        (C/m^2)" OUTCAR.ref | tail -3 | \
       awk '{printf "%10.3f %10.3f %10.3f %10.3f %10.3f %10.3f\n",$2,$3,$4,$5,$6,$7}' >piezo.ref
 
#    echo "PIEZOELECTRIC TENSOR (ionic contribution)" >>piezo
    grep -A 5 "PIEZOELECTRIC TENSOR IONIC" OUTCAR | tail -3 | \
       awk '{printf "%10.3f %10.3f %10.3f %10.3f %10.3f %10.3f\n",$2,$3,$4,$5,$6,$7}' >>piezo
#    echo "PIEZOELECTRIC TENSOR (ionic contribution)" >>piezo.ref
    grep -A 5 "PIEZOELECTRIC TENSOR IONIC" OUTCAR.ref | tail -3 | \
       awk '{printf "%10.3f %10.3f %10.3f %10.3f %10.3f %10.3f\n",$2,$3,$4,$5,$6,$7}' >>piezo.ref

    grep -A 5 "PIEZOELECTRIC TENSOR (including local field effects) (C/m^2)" OUTCAR | tail -3 | \
       awk '{printf "%10.3f %10.3f %10.3f %10.3f %10.3f %10.3f\n",$2,$3,$4,$5,$6,$7}' >>piezo

    grep -A 5 "PIEZOELECTRIC TENSOR (including local field effects) (C/m^2)" OUTCAR.ref | tail -3 | \
       awk '{printf "%10.3f %10.3f %10.3f %10.3f %10.3f %10.3f\n",$2,$3,$4,$5,$6,$7}' >>piezo.ref

    $COMPARE_NUMBERTABLE piezo piezo.ref $TOL_PIEZO > diff

    if [ -z "`cat diff`" ]
    then
       echox "the piezo electric tensors are:"
       cat piezo
       echox "the piezoelectric tensors are correct, run successful" 
    else
       echox "ERROR: the test yields different results for the piezoelectric tensors, please check"
       echox "------------------------------------------------------------------------------------"
       cat piezo
       cat diff
       echo "$1" >  FAILED
    fi
##    rm piezo piezo.ref
}
#----------------------------------------------------------------------
function get_eigenvalue
#----------------------------------------------------------------------
{
    awk -v select=$1 '
/NBANDS/ { nbands = $9 }
scan==1  { 
   i=i+1
   if (i==select) print $2
   if (i>nbands) scan=0
}
/band No.  band energies  /  { scan=1 }' <OUTCAR
}

#----------------------------------------------------------------------
function check_ml_logfile
#----------------------------------------------------------------------
{
   #energies
#   awk '/Error in energy \(eV atom\^-1\)\:/ {printf "%20.8f\n", $6 }
#       ' <ML_LOGFILE >en_ml_logfile
#   awk '/Error in energy \(eV atom\^-1\)\:/ {printf "%20.8f\n", $6 }
#       ' <ML_LOGFILE.ref >en_ml_logfile.ref
   awk '/^ERR/ {printf "%20.8f\n", $3 }' <ML_LOGFILE >en_ml_logfile
   awk '/^ERR/ {printf "%20.8f\n", $3 }' <ML_LOGFILE.ref >en_ml_logfile.ref
   $COMPARE_NUMBERTABLE en_ml_logfile en_ml_logfile.ref $TOL_ML_FF_EN > diff
   if [ -z "`cat diff`" ]
   then
      echox "The MLFF energies are correct, run successful"
   else
      echox "ERROR: the test yields different energies, please check"
      echox "----------------------------------------------------------------------------"
      cat en_ml_logfile | head -10
      cat diff
      echo "$1" >  FAILED
   fi

   #forces
#   awk '/Error in force  \(eV Angst\^-1\)\:/ {printf "%20.8f\n",$6 }
#       ' <ML_LOGFILE >for_ml_logfile
#   awk '/Error in force  \(eV Angst\^-1\)\:/ {printf "%20.8f\n",$6 }
#       ' <ML_LOGFILE.ref >for_ml_logfile.ref
   awk '/^ERR/ {printf "%20.8f\n", $4 }' <ML_LOGFILE >for_ml_logfile
   awk '/^ERR/ {printf "%20.8f\n", $4 }' <ML_LOGFILE.ref >for_ml_logfile.ref
   $COMPARE_NUMBERTABLE for_ml_logfile for_ml_logfile.ref $TOL_ML_FF_FOR > diff
   if [ -z "`cat diff`" ]
   then
      echox "The MLFF forces are correct, run successful"
   else
      echox "ERROR: the test yields different forces, please check"
      echox "----------------------------------------------------------------------------"
      cat for_ml_logfile | head -10
      cat diff
      echo "$1" >  FAILED
   fi

   #stress
#   awk '/Error in stress \(kB\)\:/ { printf "%20.8f\n", $5 }
#       ' <ML_LOGFILE >stress_ml_logfile
#   awk '/Error in stress \(kB\)\:/ { printf "%20.8f\n", $5 }
#       ' <ML_LOGFILE.ref >stress_ml_logfile.ref
   awk '/^ERR/ {printf "%20.8f\n", $5 }' <ML_LOGFILE >stress_ml_logfile
   awk '/^ERR/ {printf "%20.8f\n", $5 }' <ML_LOGFILE.ref >stress_ml_logfile.ref
   $COMPARE_NUMBERTABLE stress_ml_logfile stress_ml_logfile.ref $TOL_ML_FF_STRESS > diff
   if [ -z "`cat diff`" ]
   then
      echox "The MLFF stresses are correct, run successful"
   else
      echox "ERROR: the test yields different stresses, please check"
      echox "----------------------------------------------------------------------------"
      cat stress_ml_logfile | head -10
      cat diff
      echo "$1" >  FAILED
   fi
#   rm en_ml_logfile en_ml_logfile.ref
#   rm for_ml_logfile for_ml_logfile.ref
#   rm stress_ml_logfile stress_ml_logfile.ref
}


#----------------------------------------------------------------------
function check_ml_ff_beef
#----------------------------------------------------------------------
{
   awk '/^BEEF/ {printf "%20.8f\n",$3; printf "%20.8f\n",$4; printf "%20.8f\n",$5}
       ' <ML_LOGFILE >ml_ff_beef
   awk '/^BEEF/ {printf "%20.8f\n",$3; printf "%20.8f\n",$4; printf "%20.8f\n",$5}
       ' <ML_LOGFILE.ref >ml_ff_beef.ref
   $COMPARE_NUMBERTABLE ml_ff_beef ml_ff_beef.ref $TOL_ML_FF_HEAT > diff

    if [ -z "`cat diff`" ]
    then
       echox "The MLFF BEEF is correct, run successful"
    else
       echox "ERROR: the test yields different BEEF, please check"
       echox "----------------------------------------------------------------------------"
       cat ml_ff_beef | head -10
       cat diff
       echo "$1" >  FAILED
    fi
    rm ml_ff_beef ml_ff_beef.ref
}

#----------------------------------------------------------------------
function check_ml_ff_heat
#----------------------------------------------------------------------
{
   awk '{printf "%20.8f\n",$4; printf "%20.8f\n",$5; printf "%20.8f\n",$6}
       ' <ML_HEAT >ml_ff_heat
   awk '{printf "%20.8f\n",$4; printf "%20.8f\n",$5; printf "%20.8f\n",$6}
       ' <ML_HEAT.ref >ml_ff_heat.ref
   $COMPARE_NUMBERTABLE ml_ff_heat ml_ff_heat.ref $TOL_ML_FF_HEAT > diff

    if [ -z "`cat diff`" ]
    then
       echox "The MLFF heat flux is correct, run successful"
    else
       echox "ERROR: the test yields different heat flux, please check"
       echox "----------------------------------------------------------------------------"
       cat ml_ff_heat | head -10
       cat diff
       echo "$1" >  FAILED
    fi
    rm ml_ff_heat ml_ff_heat.ref
}

#----------------------------------------------------------------------
function check_mlff_TI
#----------------------------------------------------------------------
{
#    awk '/dH\/dRCOUPLE \(eV\)\:/{printf "%20.8f\n",print $3}
#        ' ML_LOGFILE > output_ml_logfile
#
#    awk '/dH\/dRCOUPLE \(eV\)\:/{printf "%20.8f\n", $3}
#        ' ML_LOGFILE.ref > output_ml_logfile.ref
    awk '/^DCOUPLE/ {printf "%20.8f\n",$3}' ML_LOGFILE > output_ml_logfile
    awk '/^DCOUPLE/ {printf "%20.8f\n",$3}' ML_LOGFILE.ref > output_ml_logfile.ref

    $COMPARE_NUMBERTABLE output_ml_logfile output_ml_logfile.ref $TOL_ML_FF_TI >> diff

    if [ -z "`cat diff`" ]
    then
       echox "The thermodynamic integration (TI) in MLFF is correct, run succesful"
       echox "-----------------------------------------------------------------------------"
       rm output_ml_logfile output_ml_logfile.ref diff
    else
       echox "ERROR: the test yields different thermodynamic integration (TI) in MLFF, please check"
       echox "---------------------------------------------------------------------"
       cat diff
       echo "$1" >  FAILED
    fi
}

#----------------------------------------------------------------------
function check_mlff_ABN
#----------------------------------------------------------------------
{
    #compare integer values first
    awk '/The number of configurations/ {getline;getline;print $1}
         /The maximum number of atom type/ {getline;getline;print $1}
         /The maximum number of atoms per system/ {getline;getline;print $1}
         /The maximum number of atoms per atom type/ {getline;getline;print $1}
         /The numbers of basis sets per atom type/ {getline;getline;for(i=1; i<=NF; i++) print $i}
    ' ML_ABN > output_ML_ABN_awked
    awk '/The number of configurations/ {getline;getline;print $1}
         /The maximum number of atom type/ {getline;getline;print $1}
         /The maximum number of atoms per system/ {getline;getline;print $1}
         /The maximum number of atoms per atom type/ {getline;getline;print $1}
         /The numbers of basis sets per atom type/ {getline;getline;for(i=1; i<=NF; i++) print $i}
    ' ML_ABN.ref > output_ML_ABN_awked.ref

    awk 'BEGIN{n1=0;n2=0}{if (NR==FNR) {a[n1]=$1;n1=n1+1} else {b[n2]=$1;n2=n2+1}}
         END{if (length(a)!=length(b)){print "ERROR: length of two files is different, exiting..."; exit 1} else
         {for (i=0;i<=(length(a)-1);++i){if (a[i]-b[i] != 0) {print a[i], b[i]}}}}
    ' output_ML_ABN_awked output_ML_ABN_awked.ref > diff

    #compare real values

    awk '/Reference atomic energy \(eV\)/ {getline;getline;for(i=1; i<=NF; i++) printf "%20.10f\n", $i}
         /Atomic mass/ {getline;getline;for(i=1; i<=NF; i++) printf "%20.10f\n", $i}
    ' ML_ABN > output_ML_ABN_awked

    awk '/Reference atomic energy \(eV\)/ {getline;getline;for(i=1; i<=NF; i++) printf "%20.10f\n", $i}
         /Atomic mass/ {getline;getline;for(i=1; i<=NF; i++) printf "%20.10f\n", $i}
    ' ML_ABN.ref > output_ML_ABN_awked.ref

    $COMPARE_NUMBERTABLE output_ML_ABN_awked output_ML_ABN_awked.ref $TOL_ML_FF_ABN >> diff

    if [ -z "`cat diff`" ]
    then
       echox "The ML_ABN file is correct, run succesful"
       echox "-----------------------------------------------------------------------------"
       rm output_ML_ABN_awked output_ML_ABN_awked.ref diff
    else
       echox "ERROR: the test yields different ML_ABN files, please check"
       echox "---------------------------------------------------------------------"
       cat diff
       echo "$1" >  FAILED
    fi
}


#----------------------------------------------------------------------
function check_mlff_ABN_refcon
#----------------------------------------------------------------------
{
    #compare list of local reference configurations in ML_ABN file
    awk 'BEGIN{status=0}{if ($1=="Basis") status=1; if ((status == 1) && !($0 ~ /----/)) 
         {if ($0 ~ /\*\*\*/){status=0} else if ($0 !~ /Basis/) {print $1;print $2} } }' ML_ABN > output_ML_ABN_awked
    awk 'BEGIN{status=0}{if ($1=="Basis") status=1; if ((status == 1) && !($0 ~ /----/)) 
         {if ($0 ~ /\*\*\*/){status=0} else if ($0 !~ /Basis/) {print $1;print $2} } }' ML_ABN.ref > output_ML_ABN_awked.ref
    awk 'BEGIN{n1=0;n2=0}{if (NR==FNR) {a[n1]=$1;n1=n1+1} else {b[n2]=$1;n2=n2+1}}
         END{if (length(a)!=length(b)){print "ERROR: length of two files is different, exiting..."; exit 1} else
         {for (i=0;i<=(length(a)-1);++i){if (a[i]-b[i] != 0) {print a[i], b[i]}}}}
    ' output_ML_ABN_awked output_ML_ABN_awked.ref > diff
    if [ -z "`cat diff`" ]
    then
       echox "The local ref. confs. in ML_ABN file are correct, run succesful"
       echox "-----------------------------------------------------------------------------"
       rm output_ML_ABN_awked output_ML_ABN_awked.ref diff
    else
       echox "ERROR: the test yields different ML_ABN local ref. confs., please check"
       echox "---------------------------------------------------------------------"
       cat diff
       echo "$1" >  FAILED
    fi
}

#----------------------------------------------------------------------
function check_mlff_FFN
#----------------------------------------------------------------------
{
    #compare integer values first
    awk '/The numbers of basis sets/ {getline;getline;for(i=1; i<=NF; i++) print $i}
         /The number of active descriptors/ {getline;getline;print $1}
         ' ML_FFN > output_ML_FFN_awked
    awk '/The numbers of basis sets/ {getline;getline;for(i=1; i<=NF; i++) print $i}
         /The number of active descriptors/ {getline;getline;print $1}
         ' ML_FFN.ref > output_ML_FFN_awked.ref

    awk 'BEGIN{n1=0;n2=0}{if (NR==FNR) {a[n1]=$1;n1=n1+1} else {b[n2]=$1;n2=n2+1}}
         END{if (length(a)!=length(b)){print "ERROR: length of two files is different, exiting..."; exit 1} else
         {for (i=0;i<=(length(a)-1);++i){if (a[i]-b[i] != 0) {print a[i], b[i]}}}}
    ' output_ML_FFN_awked output_ML_FFN_awked.ref > diff
    if [ -z "`cat diff`" ]
    then
       echox "The ML_FFN file is correct, run succesful"
       echox "-----------------------------------------------------------------------------"
       rm output_ML_FFN_awked output_ML_FFN_awked.ref diff
    else
       echox "ERROR: the test yields different ML_FFN files, please check"
       echox "---------------------------------------------------------------------"
       cat diff
       echo "$1" >  FAILED
    fi
}

#----------------------------------------------------------------------
function check_mlff_energy_error
#----------------------------------------------------------------------
{
#     awk '/Error in energy \(eV atom\^-1\)/{print $6}' ML_LOGFILE > ML_energy_error
     awk '/Error in energy \(eV atom\^-1\)/{print $6}' ML_LOGFILE.ref > ML_energy_error.ref
     awk '/^ERR/ {printf "%20.8f\n", $3 }' <ML_LOGFILE > ML_energy_error
#     awk '/^ERR/ {printf "%20.8f\n", $3 }' <ML_LOGFILE.ref > ML_energy_error.ref
     awk 'BEGIN{n1=0;n2=0}{if (NR==FNR) {a[n1]=$1;n1=n1+1} else {b[n2]=$1;n2=n2+1}}
          END{if (length(a)!=length(b)){print "ERROR: length of two files is different, exiting..."; exit 1} else 
          {for (i=0;i<=(length(a)-1);++i){ 
          printf "%10.6f\n",sqrt((a[i]-b[i])**2.0)}}}' ML_energy_error ML_energy_error.ref > ML_compare
     awk -v var="$TOL_MLFF_ENE" 'BEGIN{c=0}{ if ($1>var) c=1}END{if (c!=0) print "error of machine learning too large"}' ML_compare > diff
     if [ -z "`cat diff`" ]
     then
        echox "the error for machine learning energies are:"
        cat ML_compare
        cat ML_energy_error
        cat ML_energy_error.ref
        echox "the machine learning energies are correct, run succesful"
        echox "-----------------------------------------------------------------------------"
     else
        echox "ERROR: the test yields different results for the machine learning energies, please check"
        echox "---------------------------------------------------------------------"
        cat ML_compare
        cat diff
        echo "$1" >  FAILED
     fi
     rm ML_energy_error ML_energy_error.ref ML_compare
}
#----------------------------------------------------------------------
function check_dos
#----------------------------------------------------------------------
{
    awk 'scan==1 {line=line+1;
                  if (line>0) printf "%10.5f %10.5f %10.5f\n", $2, $3, $4;
                  if (line==100) scan=0 }
         /<\/dos>/ {scan=0}
         /<dos>/ {scan=1; line=-10}
        ' < vasprun.1.ref.xml > dos.ref
    awk 'scan==1 {line=line+1;
                  if (line>0) printf "%10.5f %10.5f %10.5f\n", $2, $3, $4;
                  if (line==100) scan=0 }
         /<\/dos>/ {scan=0}
         /<dos>/ {scan=1; line=-10}
        ' < vasprun.xml > dos

    $COMPARE_NUMBERTABLE dos dos.ref $TOL_DIELECTRIC > diff

    if [ -z "`cat diff`" ]
    then
       echox "the dos is:"
       cat dos | head -10
       echox "the electronic dos is correct, run sucessfull"
       echox "-----------------------------------------------------------------------------"
    else
       echox "ERROR: the test yields different results for the dos, please check"
       echox "---------------------------------------------------------------------"
       cat dos | head -10
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_imaginary_dielectric_function
#----------------------------------------------------------------------
{
     awk '
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f\n", $1,$2,$3,$4,$5,$6,$7
                             if (line==100) scan=0 }
        /frequency dependent IMAGINARY DIELECTRIC FUNCTION/ { scan=1 ; line=-2 }' <OUTCAR > imaginary_dielectric
     awk '
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f\n", $1,$2,$3,$4,$5,$6,$7
                             if (line==100) scan=0 }
        /frequency dependent IMAGINARY DIELECTRIC FUNCTION/ { scan=1 ; line=-2 }' <OUTCAR.ref >imaginary_dielectric.ref

    $COMPARE_NUMBERTABLE imaginary_dielectric imaginary_dielectric.ref $TOL_DIELECTRIC > diff

    if [ -z "`cat diff`" ]
    then
       echox "the imaginary dielectric matrix is:"
       cat imaginary_dielectric | head -10
       echox "the imaginary dielectric matrix is correct, run sucessful"
       echox "-----------------------------------------------------------------------------"
    else
       echox "ERROR: the test yields different results for the imaginary dielectric matrix, please check"
       echox "---------------------------------------------------------------------"
       cat imaginary_dielectric | head -10
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_real_dielectric_function
#----------------------------------------------------------------------
{
     awk '
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f\n", $1,$2,$3,$4,$5,$6,$7
                             if (line==100) scan=0 }
        /frequency dependent      REAL DIELECTRIC FUNCTION/ { scan=1 ; line=-2 }' <OUTCAR > real_dielectric
     awk '
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f\n", $1,$2,$3,$4,$5,$6,$7
                             if (line==100) scan=0 }
        /frequency dependent      REAL DIELECTRIC FUNCTION/ { scan=1 ; line=-2 }' <OUTCAR.ref >real_dielectric.ref

    $COMPARE_NUMBERTABLE real_dielectric real_dielectric.ref $TOL_DIELECTRIC > diff

    if [ -z "`cat diff`" ]
    then
       echox "the real dielectric matrix is:"
       cat real_dielectric | head -10
       echox "the real dielectric matrix is correct, run sucessful"
       echox "-----------------------------------------------------------------------------"
    else
       echox "ERROR: the test yields different results for the real dielectric matrix, please check"
       echox "---------------------------------------------------------------------"
       cat real_dielectric | head -10
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
# NOTE THAT THIS ROUTINE REQUIRES THAT NBANDSGW IS SET IN THE INCAR FILE !!!!!
function check_GW_ref
#----------------------------------------------------------------------
{
   if [ -z ${2} ] ; then
      RefOUTCAR=OUTCAR.ref
   else
      RefOUTCAR=OUTCAR.${2}.ref
   fi 
   error_string=${1}

     awk '
         /NBANDSGW /         { nbandsgw=$3 }
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.5f %10.5f %10.5f %10.5f %10.5f\n", $1,$2,$3,$4,$5
                             if (line==nbandsgw) scan=0 }
        / band No.  KS-energies/ { scan=1 ; line=-1 }' <OUTCAR > g0w0_eigenvalues
     awk '
         /NBANDSGW /         { nbandsgw=$3 }
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.5f %10.5f %10.5f %10.5f %10.5f\n", $1,$2,$3,$4,$5
                             if (line==nbandsgw) scan=0 }
        / band No.  KS-energies/ { scan=1 ; line=-1 }' < $RefOUTCAR > g0w0_eigenvalues.ref

    $COMPARE_NUMBERTABLE g0w0_eigenvalues g0w0_eigenvalues.ref $TOL_GW > diff

    if [ -z "`cat diff`" ]
    then
       echox "The DFT/GW eigenvalues etc. are:"
       cat g0w0_eigenvalues | head -10
       echox "the DFT/GW eigenvalues are correct, run sucessful"
       echox "-----------------------------------------------------------------------------"
    else
       echox "ERROR: the test yields different results for the GW output, please check"
       echox "---------------------------------------------------------------------"
       cat g0w0_eigenvalues | head -10
       cat diff
       echo "$error_string" >  FAILED
    fi

    # compare the self-consistent QP energy results

     awk '
         /NBANDSGW /         { nbandsgw=$3 }
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.5f %10.5f %10.5f %10.5f %10.5f\n", $1,$2,$3,$4,$5
                             if (line==nbandsgw) scan=0 }
        / QP-e/ { scan=1 ; line=-1 }' <OUTCAR > gw_eigenvalues
        #/ band No. DFT/ { scan=1 ; line=-1 }' <OUTCAR > gw_eigenvalues
     awk '
         /NBANDSGW /         { nbandsgw=$3 }
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.5f %10.5f %10.5f %10.5f %10.5f\n", $1,$2,$3,$4,$5
                             if (line==nbandsgw) scan=0 }
        / QP-e/ { scan=1 ; line=-1 }' < $RefOUTCAR > gw_eigenvalues.ref
        #/ band No. DFT/ { scan=1 ; line=-1 }' < $RefOUTCAR > gw_eigenvalues.ref

    $COMPARE_NUMBERTABLE gw_eigenvalues gw_eigenvalues.ref $TOL_GW > diff

    if [ -z "`cat diff`" ]
    then
       echox "The DFT/GW eigenvalues etc. are:"
       cat gw_eigenvalues | head -10
       echox "the DFT/GW eigenvalues are correct, run sucessful"
       echox "-----------------------------------------------------------------------------"
    else
       echox "ERROR: the test yields different results for the GW output, please check"
       echox "---------------------------------------------------------------------"
       cat gw_eigenvalues | head -10
       cat diff
       echo "$error_string" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_elastic_properties
#----------------------------------------------------------------------
{
   for propno in "1" "2" "3" "4"
   do
     case "$propno" in
     "1")
     propkey="SYMMETRIZED ELASTIC MODULI"; propname="symmetrized_elastic_moduli"; ;;
     "2")
     propkey="ELASTIC MODULI CONTR FROM IONIC RELAXATION"; propname="elastic_moduli_contr_from_ionic_relaxation"; ;;
     "3")
     propkey="TOTAL ELASTIC MODULI"; propname="total_elastic_moduli"; ;;
     "4")
     propkey="ELASTIC MODULI IONIC CONTR"; propname="elastic_moduli_ionic_contr"; ;;
     esac

     awk '
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.5f %10.5f %10.5f %10.5f %10.5f %10.5f\n", $2,$3,$4,$5,$6,$7
                             if (line==6) scan=0 }
        /'"$propkey"'/ { scan=1 ; line=-2 }' <OUTCAR > $propname
     awk '
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.5f %10.5f %10.5f %10.5f %10.5f %10.5f\n", $2,$3,$4,$5,$6,$7
                             if (line==6) scan=0 }
        /'"$propkey"'/ { scan=1 ; line=-2 }' <OUTCAR.ref > $propname.ref

    $COMPARE_NUMBERTABLE $propname $propname.ref $TOL_ELAMOD > diff

    if [ -z "`cat diff`" ]
    then
       echox "The $propkey is:"
       cat $propname | head -10
       echox "the $propname is correct, run sucessful"
       echox "-----------------------------------------------------------------------------"
    else
       echox "ERROR: the test yields different results for the $propname, please check"
       echox "---------------------------------------------------------------------"
       cat $propname | head -10
       cat diff
       echo "$1" >  FAILED
    fi
   done
}
#----------------------------------------------------------------------
function check_phonons
#----------------------------------------------------------------------
{
#########################################################
# first check the frequencies...
#########################################################

     awk '
         scan==1           
        /f  =/ { scan=1 ; } { if (scan==1) printf "%10.6f\n",$4 ; scan=0} ' <OUTCAR > phonon_frequencies
     awk '
         scan==1           
        /f  =/ { scan=1 ; } { if (scan==1) printf "%10.6f\n",$4 ; scan=0} ' <OUTCAR.ref > phonon_frequencies.ref

    $COMPARE_NUMBERTABLE phonon_frequencies phonon_frequencies.ref $TOL_PHONFREQ > diff

    if [ -z "`cat diff`" ]
    then
       echox "The PHONON-FREQUENCIES are:"
       cat phonon_frequencies | head -10
       echox "the PHONON-FREQUENCIES are correct, run sucessful"
       echox "-----------------------------------------------------------------------------"
    else
       echox "ERROR: the test yields different results for the PHONON-FREQUENCIES, please check"
       echox "---------------------------------------------------------------------"
       cat phonon_frequencies | head -10
       cat diff
       echo "$1" >  FAILED
    fi

################################################
# ...then check the eigenvectors
################################################

     awk '
         /NIONS =/         { nions=$12 }
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.6f %10.6f %10.6f %10.6f %10.6f %10.6f\n", $1,$2,$3,$4,$5,$6
                             if (line==nions) scan=0 }
        /f  =/ { scan=1 ; line=-1 }' <OUTCAR > phonon_vectors
     awk '
         /NIONS =/         { nions=$12 }
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.6f %10.6f %10.6f %10.6f %10.6f %10.6f\n", $1,$2,$3,$4,$5,$6
                             if (line==nions) scan=0 }
        /f  =/ { scan=1 ; line=-1 }' <OUTCAR.ref > phonon_vectors.ref

    $COMPARE_NUMBERTABLE phonon_vectors phonon_vectors.ref > diff

    if [ -z "`cat diff`" ]
    then
       echox "The PHONON-VECTORs are:"
       cat phonon_vectors | head -10
       echox "the PHONON-VECTORs are correct, run sucessful"
       echox "-----------------------------------------------------------------------------"
    else
       echox "ERROR: the test yields different results for the PHONON-VECTORs, please check"
       echox "---------------------------------------------------------------------"
       cat phonon_vectors | head -10
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_kopt
#----------------------------------------------------------------------
{

    awk -v nbands=$2 '
         /eigenvalues_kpoints_opt/ {scan_kopt=1}
         /\/eigenvalues_kpoints_opt/ {scan_kopt=0}
         /eigenvalues/ {scan_eig=1}
         /\/eigenvalues/ {scan_eig=0}
         /set comment="kpoint/ { line=-1; scan=1 }
         { line=line+1
           if (line>0 && scan_kopt==1 && scan_eig==1 && scan==1) printf "%s\n", $2
           if (line==nbands) scan=0}' < vasprun.xml > eigenvalues_kopt

    awk -v nbands=$2 '
         /eigenvalues_kpoints_opt/ {scan_kopt=1}
         /\/eigenvalues_kpoints_opt/ {scan_kopt=0}
         /eigenvalues/ {scan_eig=1}
         /\/eigenvalues/ {scan_eig=0}
         /set comment="kpoint/ { line=-1; scan=1 }
         /\/set/ {scan=0 }
         { line=line+1
           if (line>0 && scan_kopt==1 && scan_eig==1 && scan==1) printf "%s\n", $2
           if (line==nbands) scan=0}' < vasprun.1.ref.xml > eigenvalues_kopt.ref

    $COMPARE_NUMBERTABLE eigenvalues_kopt eigenvalues_kopt.ref $TOL_KOPT > diff

    if [ -z "`cat diff`" ]
    then
       echox
       echox "the eigenvalues computed with kpoints_opt are correct, run successful"
       echox "----------------------------------------------------------------"
    else
       echox "ERROR: the kpoints_opt test yields different eigenvalues"
       echox "--------------------------------------------"
       cat eigenvalues_kopt | head -10
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function get_elphon_pot
#----------------------------------------------------------------------
{
  prefix=$1
  if [[ -z $1 ]]; then
    prefix=$JOB
  fi
  destination=phelel_params_ref.hdf5
  if [[ $2 =~ "INPUT" ]]; then
    destination=phelel_params.hdf5
  fi
  #get reference electron-phonon potential (phelel_params.hdf5) from FILES folder
  if [[ -z "${VASP_TESTSUITE_FILES}" ]]; then
    cp $ROOTDIR/FILES/$prefix.phelel_params.hdf5 $destination
  else
    cp $VASP_TESTSUITE_FILES/$prefix.phelel_params.hdf5 $destination
  fi
}
#----------------------------------------------------------------------
function get_vaspelph
#----------------------------------------------------------------------
{
  prefix=$1
  if [[ -z $1 ]]; then
    prefix=$JOB
  fi
  destination=vaspelph_ref.h5
  #get reference vasppout.h5 from FILES folder
  if [[ -z "${VASP_TESTSUITE_FILES}" ]]; then
    cp $ROOTDIR/FILES/$prefix.vaspelph.h5 $destination
  else
    cp $VASP_TESTSUITE_FILES/$prefix.vaspelph.h5 $destination
  fi
}

#----------------------------------------------------------------------
function check_elphon_pot_h5diff
#----------------------------------------------------------------------
{
  # ignore these: displacements_atom_indices displacements_vectors
  # compare every element of the phelel_params.hdf5 to a reference one
  integer_variables="FFT_mesh direct_rotations p2s_map phonon_p2s_map phonon_s2p_map s2p_map spacegroup_number"
  numeric_values="Dij dDijdu dVdu force_constants dqijdu qij"
  structure="grid_point lattice_point phonon_lattice_point"
  shortest_vectors_phonon="phonon_shortest_vector_multiplicities phonon_shortest_vectors"
  shortest_vectors="shortest_vector_multiplicities shortest_vectors"
  structure_primitive="primitive_lattice primitive_masses primitive_numbers primitive_positions"
  structure_unitcell="unitcell_lattice unitcell_masses unitcell_numbers unitcell_positions transformation_matrix"
  structure_supercell="supercell_lattice supercell_masses supercell_numbers supercell_positions supercell_matrix"
  structure_phonon_supercell="phonon_supercell_lattice phonon_supercell_masses phonon_supercell_numbers phonon_supercell_positions phonon_supercell_matrix"
  for obj in $integer_variables $numeric_values $structure $shortest_vectors_phonon $shortest_vectors $structure_primitive $structure_unitcell $structure_supercell $structure_phonon_supercell; do
     h5diff -d 1e-4 phelel_params_ref.hdf5 phelel_params.hdf5 $obj
     params_status=$?
     if [ "$params_status" -ne "0" ]; then
        echo "$JOB" > FAILED
     fi
  done
}
#----------------------------------------------------------------------
function check_elphon_pot
#----------------------------------------------------------------------
{
    python $ROOTDIR/tools/compare_pot.py $@
    if [ "$?" -ne "0" ]; then
       echo "$JOB" > FAILED
    fi
}
#----------------------------------------------------------------------
function check_elphon_elselfen
#----------------------------------------------------------------------
{
    python $ROOTDIR/tools/compare_elselfen.py $@
    if [ "$?" -ne "0" ]; then
       echo "$JOB" > FAILED
    fi
}
#----------------------------------------------------------------------
function check_elphon_mels
#----------------------------------------------------------------------
{
    python $ROOTDIR/tools/compare_mels.py $@
    if [ "$?" -ne "0" ]; then
       echo "$JOB" > FAILED
    fi
}
#----------------------------------------------------------------------
function check_elphon_transport
#----------------------------------------------------------------------
{
    python $ROOTDIR/tools/compare_transport.py $@
    if [ "$?" -ne "0" ]; then
       echo "$JOB" > FAILED
    fi
}
#----------------------------------------------------------------------
function check_ACFDT
#----------------------------------------------------------------------
{
     awk '
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.3f %10.3f %10.6f %10.6f\n", $1,$2,$3,$4
                             if (line==8) scan=0 }
        /RPA   correlation/ { scan=1 ; line=-1 }' <OUTCAR > acfdt
     awk '
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.3f %10.3f %10.6f %10.6f\n", $1,$2,$3,$4
                             if (line==8) scan=0 }
        /RPA   correlation/ { scan=1 ; line=-1 }' <OUTCAR.ref >  acfdt.ref

    $COMPARE_NUMBERTABLE acfdt acfdt.ref $TOL_ECORR > diff

    if [ -z "`cat diff`" ]
    then
       echox "The ACFDT correlation energies etc. are:"
       cat acfdt | head -10
       echox "the ACFDT correlation energies correct, run sucessful"
       echox "---------------------------------------------------------------------"
    else
       echox "ERROR: test yields different ACFDT correlation energies correct, please check"
       echox "---------------------------------------------------------------------"
       cat acfdt | head -10
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_MP2_energy
#----------------------------------------------------------------------
{
    awk '
    /Fock exchange energy/{   print $NF }
    /Hartree contr. to MP2/{  print $NF }
    /Exchange contr. to MP2/{ print $NF }
    /MP2 correlation energy/{ print $NF }
    ' OUTCAR > mp2
    awk '
    /Fock exchange energy/{   print $NF }
    /Hartree contr. to MP2/{  print $NF }
    /Exchange contr. to MP2/{ print $NF }
    /MP2 correlation energy/{ print $NF }
    ' OUTCAR.ref > mp2.ref

    $COMPARE_NUMBERTABLE mp2 mp2.ref $TOL_ECORR > diff

    if [ -z "`cat diff`" ]
    then
       echox "The contribution to the MP2 energy are:"
       cat mp2
       echox "The energies are correct, run successful"
       echox "---------------------------------------------------------------------"
    else
       echox "ERROR: test yields different contributions to MP2 energies, please check"
       echox "---------------------------------------------------------------------"
       cat mp2
       cat diff
       echo "$1" > FAILED
    fi
}
#----------------------------------------------------------------------
function check_LTMP2_energy
#----------------------------------------------------------------------
{
     awk '
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.3f %10.3f %10.6f %10.6f %10.6f\n", $1,$2,$3,$4,$5
                             if (line==8) scan=0 }
        /MP2    exchange MP2/ { scan=1 ; line=-1 }' <OUTCAR > ltmp2
     awk '
         scan==1           { line=line+1 ; 
                             if (line>0) printf "%10.3f %10.3f %10.6f %10.6f %10.6f\n", $1,$2,$3,$4,$5
                             if (line==8) scan=0 }
        /MP2    exchange MP2/ { scan=1 ; line=-1 }' <OUTCAR.ref >  ltmp2.ref

    $COMPARE_NUMBERTABLE ltmp2 ltmp2.ref $TOL_ECORR > diff

    if [ -z "`cat diff`" ]
    then
       echox "The LTMP2 correlation energies etc. are:"
       cat ltmp2 | head -10
       echox "the LTMP2 correlation energies correct, run sucessful"
       echox "---------------------------------------------------------------------"
    else
       echox "ERROR: test yields different LTMP2 correlation energies correct, please check"
       echox "---------------------------------------------------------------------"
       cat ltmp2 | head -10
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_CRPA
#----------------------------------------------------------------------
{
    # list real parts of on-, off-site U and J first
    awk '/bare Hubbard / {printf "%10.3f \n", $5 }' < OUTCAR > crpa
    awk '/bare Hubbard / {printf "%10.3f \n", $5 }' < OUTCAR.ref > crpa.ref
    awk '/screened Hubbard / {printf "%10.3f \n", $5 }' < OUTCAR >> crpa
    awk '/screened Hubbard / {printf "%10.3f \n", $5 }' < OUTCAR.ref >> crpa.ref

    # list imaginary parts of on-, off-site U and J
    awk '/bare Hubbard / {printf "%10.3f \n", $6 }' < OUTCAR >> crpa
    awk '/bare Hubbard / {printf "%10.3f \n", $6 }' < OUTCAR.ref >> crpa.ref
    awk '/screened Hubbard / {printf "%10.3f \n", $6 }' < OUTCAR >> crpa
    awk '/screened Hubbard / {printf "%10.3f \n", $6 }' < OUTCAR.ref >> crpa.ref

    $COMPARE_NUMBERTABLE crpa crpa.ref $TOL_CRPA > diff

    if [ -z "`cat diff`" ]
    then
       echox "The real parts of the  on-, off-site U and J and their imaginary parts are "
       cat crpa
       echox "the interactions in the CRPA formalism are correct, run sucessful"
       echox "---------------------------------------------------------------------"
    else
       echox "ERROR: test yields different CRPA interactions, please check"
       echox "---------------------------------------------------------------------"
       cat crpa
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_WAN90
#----------------------------------------------------------------------
{
    # remove empty lines from bands_int.dat
    #sed '/^\s*$/d' bands_int.dat > bands_int_clean.dat
    #$COMPARE_NUMBERTABLE bands_int_clean.dat bands_int.ref $TOL_WAN90 > diff
    tr "\n" "|" < vasprun.xml | grep -o -P "<eigenvalues_interp comment=\"wannier\">.*</eigenvalues_interp>" | sed 's/\(<tr>\|<\/tr>\)//g;s/|/\n/g' | grep "<r>" | awk '{print $2}' > eigenvalues
    tr "\n" "|" < vasprun.ref.xml | grep -o -P "<eigenvalues_interp comment=\"wannier\">.*</eigenvalues_interp>" | sed 's/\(<tr>\|<\/tr>\)//g;s/|/\n/g' | grep "<r>" | awk '{print $2}' > eigenvalues.ref
    $COMPARE_NUMBERTABLE eigenvalues eigenvalues.ref $TOL_WAN90 > diff

    if [ -z "`cat diff`" ]
    then
       echox
       echox "the eigenvalues computed with wannier are correct, run successful"
       echox "----------------------------------------------------------------"
    else
       echox "ERROR: the wannier test yields different eigenvalues"
       echox "--------------------------------------------"
       cat diff
       echo "$1" >  FAILED
    fi

}
#----------------------------------------------------------------------
function check_NLRPA
#----------------------------------------------------------------------
{
    # list real parts of on-, off-site U and J first
    awk '/non-local screened Hubbard/ {printf "%10.3f \n", $6} ' < OUTCAR > nlrpa
    awk '/non-local screened Hubbard/ {printf "%10.3f \n", $6} ' < OUTCAR.ref > nlrpa.ref

    # list imaginary parts of on-, off-site U and J
    awk '/non-local screened Hubbard/ {printf "%10.3f \n", $7} ' < OUTCAR >> nlrpa
    awk '/non-local screened Hubbard/ {printf "%10.3f \n", $7} ' < OUTCAR.ref >> nlrpa.ref

    $COMPARE_NUMBERTABLE nlrpa nlrpa.ref $TOL_CRPA > diff

    if [ -z "`cat diff`" ]
    then
       echox "The real parts of the  on-, off-site U and J and their imaginary parts are "
       cat nlrpa
       echox "the interactions in the NLRPA formalism are correct, run sucessful"
       echox "---------------------------------------------------------------------"
    else
       echox "ERROR: test yields different CRPA interactions, please check"
       echox "---------------------------------------------------------------------"
       cat nlrpa
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_plasmon
#----------------------------------------------------------------------
{
    echo $1
    awk <OUTCAR >plasmon.$1 '
         /plasmon correlation energy/ { printf "%10s %10s %10.5f\n", $1,$2, $4} '
    awk <OUTCAR.$1.ref >plasmon.$1.ref '
         /plasmon correlation energy/ { printf "%10s %10s %10.5f\n", $1,$2, $4} '
    $COMPARE_NUMBERTABLE plasmon.$1 plasmon.$1.ref $TOL_ECORR > diff

    if [ -z "`cat diff`" ]
    then
       echox "The plasmon energies are:"
       cat plasmon.$1 | head -1
       echox "the plasmon RPA frequencies are correct, run sucessful"
       echox "---------------------------------------------------------------------"
    else
       echox "ERROR: test yields different plasmon RPA energies, please check"
       echox "---------------------------------------------------------------------"
       cat plasmon.$1 | head -1
       cat diff
       echo "$2" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_lorbit14
#----------------------------------------------------------------------
{
    awk '/optimized coefficients for linear combined projectors/ { while (NF) {getline; print $0} }' < OUTCAR.LOCPROJ | awk '{ s = ""; for (i = 7; i <= NF; i++) s = s $i " "; print s }' | sed '/^$/d' > projectors
    awk '/optimized coefficients for linear combined projectors/ { while (NF) {getline; print $0} }' < OUTCAR.ref | awk '{ s = ""; for (i = 7; i <= NF; i++) s = s $i " "; print s }' | sed '/^$/d' > projectors.ref
    $COMPARE_NUMBERTABLE projectors projectors.ref $TOL_PROJ > diff

    if [ -z "`cat diff`" ]
    then
       echox "the optimized projectors are correct, run sucessful"
       echox "---------------------------------------------------------------------"
    else
       echox "ERROR: test yields different optimized projectors, please check"
       echox "---------------------------------------------------------------------"
       cat projectors.$1 | head -1
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_localization
#----------------------------------------------------------------------
{
    grep "of the cost" OUTCAR | awk '{print $NF}' > localization
    grep "we pick" OUTCAR.ref | awk '{print $NF}' > localization.ref

    $COMPARE_NUMBERTABLE localization localization.ref $TOL_EIGV > diff

    if [ -z "`cat diff`" ]
    then
       echox "the eigenenergies are:"
       cat eigenvalues | head -10
       echox "the eigenenergies are correct, run successful"
    else
       echox "ERROR: the test yields different results for the eigenenergies, please check"
       echox "----------------------------------------------------------------------------"
       cat localization | head -10
       cat diff
       echo "$1" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_die_vasprun
#----------------------------------------------------------------------
{
    if [ $1 == "CHI" ] 
    then
     # type "CHI" implies that the calculations were done using ALGO = CHI
     # chose the right entry based on comment
     awk <vasprun.xml > die.$1.dat '
         /<dielectricfunction/ && /INVERSE MACROSCOPIC DIELECTRIC/ {set=set+1 ; scan= 1} 
         scan==1 && /<r>/ { printf "%7.4f %7.4f\n",  $2,$3 }      
        /<\/dielectricfunction/ { scan=0 }'
     awk <vasprun.$1.ref.xml >  die.$1.ref.dat '
         /<dielectricfunction/ && /INVERSE MACROSCOPIC DIELECTRIC/ {set=set+1 ; scan= 1} 
         scan==1 && /<r>/ { printf "%7.4f %7.4f\n",  $2,$3 }      
        /<\/dielectricfunction/ { scan=0 }'
    else
     awk <vasprun.xml > die.$1.dat '
         /<dielectricfunction/ {set=set+1 ; if (set<=1) scan= 1} 
         scan==1 && /<r>/ { printf "%7.4f %7.4f\n",  $2,$3 }      
        /<\/dielectricfunction/ { scan=0 }'
     awk <vasprun.$1.ref.xml >  die.$1.ref.dat '
         /<dielectricfunction/ {set=set+1 ; if (set<=1) scan= 1} 
         scan==1 && /<r>/ { printf "%7.4f %7.4f\n",  $2,$3 }      
        /<\/dielectricfunction/ { scan=0 }'
    fi
    $COMPARE_NUMBERTABLE die.$1.dat die.$1.ref.dat $TOL_DIELECTRIC> diff

    if [ -z "`cat diff`" ]
    then
       echox "The dielectric function  are:"
       cat die.$1.dat | head -4
       echox "the dielectric function is correct, run sucessful"
       echox "---------------------------------------------------------------------"
    else
       echox "ERROR: test yields different dielectric functions, please check"
       echox "---------------------------------------------------------------------"
       cat die.$1.dat | head -4
       cat diff
       echo "$2" >  FAILED
    fi
}
#----------------------------------------------------------------------
function check_oep_step
#----------------------------------------------------------------------
{
  if [[ $1 == "OEP" ]] || [[ $1 == "OEPR" ]] ; then
    awk '/band No.  band energies     occupation/ {for(i=1; i<=8; i++) {getline; print}}' OUTCAR> energies.$1
    awk '/band No.  band energies     occupation/ {for(i=1; i<=8; i++) {getline; print}}' OUTCAR.$1.ref> energies.$1.ref
  else
    awk '
       /free  energy/ { printf "%20.8f\n", $5 }
       /energy  without/ { printf "%20.8f\n", $7 }
       /ETOTAL/ { printf "%20.8f\n", $5 }
       ' <OUTCAR >energies.$1
    awk '
       /free  energy/ { printf "%20.8f\n", $5 }
       /energy  without/ { printf "%20.8f\n", $7 }
       /ETOTAL/ { printf "%20.8f\n", $5 }
       ' <OUTCAR.$1.ref >energies.$1.ref
  fi 
  if [[ $1 == "DIAG" ]]  ; then
     if [ -f diff ] ; then
        rm diff 
     fi 
     touch diff 
  else 
     $COMPARE_NUMBERTABLE energies.$1  energies.$1.ref $TOL_OEP > diff
  fi 

  if [ -z "`cat diff`" ]
  then
     echox "The OEP eigen energies are:"
     cat energies.$1 | head -8
     echox "the OEP energies are correct, run sucessful"
     echox "---------------------------------------------------------------------"
  else
     echox "ERROR: OEP test in step $1 yields different OEP energies"
     echox "---------------------------------------------------------------------"
     paste energies.$1 energies.$1.ref | head -8 
     cat diff
     echo "$2" >  FAILED
  fi
}
#----------------------------------------------------------------------
function check_mlpot_step
#----------------------------------------------------------------------
{
  # extract results 
  if [[ "$1" == "DFT" ]] || [[ "$1" == "MLPOT" ]] || [[ "$1" == "CG" ]] ; then
      # check energies 
      check_energy $2 $1 
      check_forces $2 $1 
      check_stress $2 $1 
      return
  fi   
  if [ ! -f $1 ] ; then
     echox "$2" > FAILED
     return
  fi 
  if [ ! -f $1.ref ] ; then
     echox " --------------- FILE $1.ref missing -----------------"
     echox "$2" > FAILED
     return
  fi 

  if [[ "$1" == "MLFIT" ]] ; then
      npoints=`grep "number of training points" $1 | awk '{print $1}'`
      tail -$npoints  $1 > result.$1
      tail -$npoints  $1.ref > result.$1.ref
  fi 
           
  $COMPARE_NUMBERTABLE result.$1  result.$1.ref $TOL_MLFF_ENE > diff

  if [ -z "`cat diff`" ]
  then
     echox "The MLPOT results of step $1 are correct:"
     cat result.$1 | head -8
     echox "Step $1 was sucessful"
     echox "---------------------------------------------------------------------"
  else
     echox "ERROR: MLPOT results of step $1 are incorrect"
     echox "---------------------------------------------------------------------"
     paste result.$1 result.$1.ref | head -8 
     cat diff
     echo "$2" >  FAILED
  fi

  rm result.$1 result.$1.ref
}
#----------------------------------------------------------------------
function run_vasp
#----------------------------------------------------------------------
{
    echo entering run_vasp >&2
    $VASP_TESTSUITE_EXE_STD
    errorCode=$?
    echo exiting run_vasp >&2
    return $errorCode
}
#----------------------------------------------------------------------
function run_vasp_nc
#----------------------------------------------------------------------
{
    echo entering run_vasp_nc >&2
    $VASP_TESTSUITE_EXE_NCL
    errorCode=$?
    echo exiting run_vasp_nc >&2
    return $errorCode
}
#----------------------------------------------------------------------
function run_vasp_g
#----------------------------------------------------------------------
{
    echo entering run_vasp_g >&2
    $VASP_TESTSUITE_EXE_GAM
    errorCode=$?
    echo exiting run_vasp_g >&2
    return $errorCode
}
#----------------------------------------------------------------------
function run_recipe()
#----------------------------------------------------------------------
{
    echo entering run_recipe $1 >&2

    rm -f WAVECAR WAVEDER CHGCAR W0* WFULL* \
          wannier90_* wannier90.wout wannier90.chk WANPROJ \
          OSZICAR OUTCAR vaspout.h5 vaspwave.h5 vaspelph.h5

    # _RPR extension is not be present 
    ext=$1
    if [[ $1 =~ "_RPR" ]] ; then
       ext=`echo $1 | awk 'BEGIN{FS="_RPR"}{print $1}' `
    fi 
    cp $VASP_TESTSUITE_POTENTIALS/POTCAR.$ext POTCAR

    for i in INCAR.?.* ; do
      echo $VASP_TESTSUITE_INCAR_PREPEND > INCAR
      cat $i >> INCAR
      type=${i##INCAR.}
      [ -f "KPOINTS.$type" ]    && cp KPOINTS.$type    KPOINTS
      [ -f "POSCAR.$type" ]     && cp POSCAR.$type     POSCAR
      [ -f "ICONST.$type" ]     && cp ICONST.$type     ICONST
      [ -f "PENALTYPOT.$type" ] && cp PENALTYPOT.$type PENALTYPOT
      type=${i##INCAR.*.}
      echoxr $1 step $type
      eval "$2"
      # capture error code of run
      if [ "$?" != "0" ] ; then
         touch FAILED 
         echo $1 >> FAILED 
         # no need to continue next step if one is broken
         break
      fi 
      echo
      cp OUTCAR OUTCAR.$type
    done

    echo exiting run_recipe $1 >&2
}
#----------------------------------------------------------------------
function run_recipe_in_steps()
#----------------------------------------------------------------------
{
    echo entering run_recipe_in_steps $1 >&2

    rm -f WAVE* CHGCAR OUTCAR OSZICAR *.tmp \
          vaspout.h5 vaspwave.h5 vaspelph.h5
    ext=$1
    if [[ $1 =~ "_RPR" ]] ; then
       ext=`echo $1 | awk 'BEGIN{FS="_RPR"}{print $1}' `
    fi 
    cp $VASP_TESTSUITE_POTENTIALS/POTCAR.$ext POTCAR

    for i in INCAR.?.* 
    do
      echo $VASP_TESTSUITE_INCAR_PREPEND > INCAR
      cat $i >> INCAR
      type=${i##INCAR.?.}
      echoxr $1 step $type
      run_vasp
      # capture error code of run
      if [ "$?" != "0" ] ; then
         touch FAILED 
         echo $1 >> FAILED 
      fi 
      if [ ! -z $2 ]; then
        if [ $2 == "check_plasmon" ]; then
          plasmon=${type%%_RPA*}
          if [ $plasmon == "BSE" ]
          then 
            install_ref_step $type 
            check_plasmon $type $1
          fi
        elif [ $2 == "remove_WFULL" ]; then
          if [ $type == "G0W0" ]
          then
            rm WFULL*.tmp
          fi
        fi
      else
        if [ $type == "G0W0" ] || [ $type == "QPBSE" ] 
        then
           install_ref_step $type 
           check_GW_ref $1 $type
        fi 
      fi
      cp OUTCAR OUTCAR.$type
      # special for OEP
      if [[ $1 =~ "OEP" ]] ; then
         check_oep_step $type $1
         install_ref_step $type  $1
      elif [[ $1 =~ "MLPOT" ]] ; then
         check_mlpot_step $type $1
         install_ref_step $type $1
      else
         check_die_vasprun $type $1
         install_ref_step $type 
      fi 
    done
    echox
    echo exiting run_recipe_in_steps $1 >&2
}

#----------------------------------------------------------------------
function run_recipe_mlff()
#----------------------------------------------------------------------
{
   echo entering run_recipe_mlff $1 >&2

   rm -f WAVECAR WAVEDER CHGCAR W0* WFULL* \
         wannier90_* wannier90.wout wannier90.chk WANPROJ \
         OSZICAR OUTCAR vaspout.h5 vaspwave.h5 vaspelph.h5 \
         ML_LOGFILE ML_ABN \
         ML_FFN ML_HIS ML_REG PENALTYPOT HILLSPOT

   # _RPR extension is not be present
   ext=$1
   if [[ $1 =~ "_RPR" ]] ; then
      ext=`echo $1 | awk 'BEGIN{FS="_RPR"}{print $1}' `
   fi
   cp INCAR.ML_FF INCAR
   run_vasp

   echo exiting run_recipe_mlff $1 >&2
}

#----------------------------------------------------------------------
function run_recipe_phelel
#----------------------------------------------------------------------
{
  cp SPOSCAR POSCAR-000
  #rm -rf disp-*
  for i in `seq -f %03g 0 $2`;
  do
      mkdir -p disp-$i
      cp POSCAR-$i disp-$i/POSCAR
      cp INCAR.1.DFT disp-$i
      cp KPOINTS disp-$i
      cd disp-$i; run_recipe $JOB $1; cd ..
  done
  if [[ ! -z $3 ]] ; then
      cd born
      # calculate born effective charges
      run_recipe $JOB run_vasp
      cd ..
  fi
}

#----------------------------------------------------------------------
function install_ref_ML_ABN
#----------------------------------------------------------------------
{
    if [ -n "$VASP_TESTSUITE_REFERENCE" ]
    then
        cp ML_ABN ML_ABN.ref
    fi
}

#----------------------------------------------------------------------
function install_ref_ML_FFN
#----------------------------------------------------------------------
{
    if [ -n "$VASP_TESTSUITE_REFERENCE" ]
    then
        cp ML_FFN ML_FFN.ref
    fi
}

#----------------------------------------------------------------------
function install_ref_ML_HEAT
#----------------------------------------------------------------------
{
    if [ -n "$VASP_TESTSUITE_REFERENCE" ]
    then
        cp ML_HEAT ML_HEAT.ref
    fi
}

#----------------------------------------------------------------------
function install_ref
#----------------------------------------------------------------------
{
    if [ -n "$VASP_TESTSUITE_REFERENCE" ]
    then
	cp OSZICAR OSZICAR.ref
	cp OUTCAR OUTCAR.ref
        [ -f ML_LOGFILE ] && cp ML_LOGFILE ML_LOGFILE.ref
    fi
}
#----------------------------------------------------------------------
function install_ref_step
#----------------------------------------------------------------------
{
    if [ -n "$VASP_TESTSUITE_REFERENCE" ]
    then
        cp OSZICAR OSZICAR.$1.ref
        cp OUTCAR OUTCAR.$1.ref
        if [ -z $2 ] ; then
           cp vasprun.xml vasprun.$1.ref.xml
        fi
        # create special ML references
        if [[ "$1" == "MLFIT" ]] ; then
           cp $1 $1.ref
        fi 
    fi
}
#----------------------------------------------------------------------
# Here the actual tests are done
#----------------------------------------------------------------------
if [ ! -z "$VASP_TESTSUITE_TESTS" ] ; then
   echox Following tests have been explicitly selected: 
   echox "VASP_TESTSUITE_TESTS=$VASP_TESTSUITE_TESTS"
fi
echoxr 

for directory in `ls tests ` ; do 
   if [ ! -d tests/${directory} ] ; then 
      continue 
   fi 
   #
   # enter test directory
   #
   cd tests/${directory} 
   # remove FAILED file from previous runs, if present
   if [ -f FAILED ] ; then 
      rm FAILED 
   fi 
   #
   # run recipe 
   # 
   if [ -f runrecipe.sh ] ; then 
      source runrecipe.sh 
      # 
      # maybe mark test as failed
      #
      if [ -f FAILED ] ; then
         ERROR_LIST+=" `head -1 FAILED`"
      fi 
   fi 
   cd ../../
done

if [ ! -z "${ERROR_LIST}" ] ; then
   echox ==================================================================
   echox  SUMMARY:
   echox ==================================================================
   echox "The following tests failed, please check the output file manually:"
   echox $(printf '%s\n' $ERROR_LIST | sort -f -u)
   exit 1 
else
   echox ==================================================================
   echox  SUCCESS: ALL SELECTED TESTS PASSED
   echox ==================================================================
   exit 0 
fi 
