#!/bin/csh
#
# Compare the results of GAMESS sample jobs to pre-stored correct values.
# This script can be executed from any directory with write permissions.
#
# Only the path name in the very next line needs to be customized.
#
set GMSPATH=/home/gu/gamess

# The Ntest outputs are assumed to be named elg01.log, elg02.log, ...
# but the user will be allowed to specify the directory containing them.
#
set LOGPATH=.
echo -n "Please type the full directory containing elg??.log [$LOGPATH]: "
set ans=$<
if (null$ans != null) then
   set EXAMPATH=$ans
else
   set EXAMPATH=$LOGPATH
endif
#
set Ntest=30
set ext=log
set elg=$EXAMPATH/elg
echo "Checking the results of your sample GAMESS calculations,"
echo "the output files (elg??.$ext) will be taken from $EXAMPATH"
#
if (-e $elg"01."$ext) then
else
   echo "The output files do not seem to exist, please check."
   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

set script=$GMSPATH/tools/checktst/elg
setenv chkabs $GMSPATH/tools/checktst/chkabs

#   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.
#
grep "TERMINATED NORMALLY" $elg*.$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" $elg$ii.$ext | wc -l`
     if ($istat == 0) echo $elg$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 "elg${ii}: "
  if(-e $elg$ii.$ext) then
    $script$ii $elg$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 elg-test results are correct'\!'
endif
