#!/bin/csh
#
# Compare the results of GAMESS sample jobs to pre-stored correct values.
# Prior to executing this script, change directory to where the log files are,
# write permission to create temporary files in this directory is mandatory.
# Execute this script by typing a full path name,
#       chdir /to/where/logs/are
#       ~/gamess/tests/standard/checktst
#
# Written by D. G. Fedorov, AIST, July 20, 2004.
#
#   LOGPATH points to the location of the output files,
# CHECKPATH points to the directory of scripts that check for correct results.
#
set CHECKPATH=$0
set CHECKPATH=$CHECKPATH:h/scripts
if(! -e $CHECKPATH/chkabs && -e ./scripts) set CHECKPATH=./scripts
if(! -e $CHECKPATH/chkabs) then
   echo Run this script as: 
   echo "    cd ~/gamess/tests"
   echo "       (or wherever else the output files from 'runall' might be)"
   echo "    ~/gamess/tests/standard/checktst"
   exit
endif
#
# The Ntest outputs are assumed to be named exam01.log, exam02.log, ...
# and present in the directory where this script is launched from.
#
set LOGPATH=.
#
#--echo -n "Please type the full directory containing exam??.log [$LOGPATH]: "
#--set ans=$<
#--if (null$ans != null) then
#--   set LOGPATH=$ans
#--else
#--   set LOGPATH=`pwd`
#--endif
#
set Ntest=47
set ext=log
set exam=$LOGPATH/exam
set script=$CHECKPATH/exam
setenv chkabs $CHECKPATH/chkabs
#
echo "Checking the results of your sample GAMESS calculations,"
echo "the output files (exam??.$ext) will be taken from $LOGPATH"
#
if (-e $exam"01."$ext) then
else
   echo "The output files do not seem to exist."
   echo "Please change directory to the location of the log files"
   echo "prior to running 'checktst'."
   exit
endif

# Quantities to be tested:
# E energy in a.u.
# H heat of formation in kcal/mol 
# G RMS gradient in a.u.
# D RMS dipole moment
# S spin momentum (a.u.)
# L localisation sum (debye**2)
# O overlap (such as GVB)
# T T1 diagnostic from CC
# X basis set exponent 
# W vibrational frequency (cm-1)
# I IR intensity
# O polarisability
# V velocity (a.u.), such as in DRC
# R distance (in bohr), such as in IRC 
# tols are tolerances, digs are the numbers of sig. digits.
# C percent (%), such as reference weight in MCQDPT

setenv tolC 0.3
setenv tolD 0.0001
setenv tolE 0.00000001
setenv tolG 0.00001
setenv tolH 0.0001
setenv tolI 0.0001
setenv tolL 0.1
setenv tolO 0.0001
setenv tolP 0.0001
setenv tolR 0.0001
setenv tolS 0.01
setenv tolT 0.000001
setenv tolV 0.00000001
setenv tolW 0.1
setenv tolX 0.00001


#   If we don't see the message about GAMESS exiting normally,
#   then there is surely a mistake, and we want to focus the
#   tester directly on the one(s) that didn't terminate normally.
#   Anyone can be free to use test numbers 70 on up.
#
grep "TERMINATED NORMALLY" ${exam}[0-6]?.$ext >& temp.junk.trash.garbage
set Nterm_ok=`wc -l temp.junk.trash.garbage`
set Nterm_ok=$Nterm_ok[1]
rm -f temp.junk.trash.garbage
if ($Nterm_ok == $Ntest) then
   echo "All jobs terminated normally, now checking detailed numerical results"
else
   echo "Only $Nterm_ok out of $Ntest examples terminated normally."
   echo "Please check carefully each of the following runs:"
   set i=1
   while ($i <= $Ntest)
     set ii=`echo $i | awk '{ printf "%02d",$1 }'`
     set istat=`grep "TERMINATED NORMALLY" $exam$ii.$ext | wc -l`
     if ($istat == 0) echo $exam$ii.$ext
     @ i++
   end 
   echo "which did not completely finish."
endif

set i=1
set nfailed=0
 
while($i <= $Ntest)
  set ii=`echo $i | awk '{ printf "%02d",$1 }'`
  echo -n "exam${ii}: "
  if(-e $exam$ii.$ext) then
    $script$ii $exam$ii.$ext | awk '{ printf "%-63s",$0 }'
    if($status != 0 ) then
  @   nfailed++
      echo "\!\!FAILED." 
    else
      echo Passed. 
    endif 
  else 
  @ nfailed++
    echo -n not found. | awk '{ printf "%-63s",$0 }' 
    echo "\!\!FAILED." 
  endif 
@ i++
end 

if ($Nterm_ok == $Ntest) then
else
   echo "Only $Nterm_ok out of $Ntest examples terminated normally."
endif

if($nfailed != 0) then
   echo $nfailed 'job(s) got incorrect numerical results. Please examine why.'
else
   echo All $Ntest test results are correct'\!'
endif
