#!/bin/csh -f

# GRAPE-DR functionality test program (check)
# version 0.1

set cmdname  = $0:t;
set rootpath = $0:h/../..
pushd $rootpath > /dev/null
set rootpath = `pwd`
popd > /dev/null

setenv HIB_WARNLEVEL "1"

if($?GDEVICE) then
 set gdevice = ${GDEVICE};
else
 set gdevice = 0;
endif
set flag = $1

if($?GDRMODEL450) then
 set gdrmodel = ".m450";
else
 set gdrmodel = "";
endif
set flag = $1

echo "-------------------------------------------------"
echo " GRAPE-DR functionality test program (check)"
echo ""
echo "  usage: check"
echo "-------------------------------------------------"
echo "Start the tests. It may take a few minutes." 
echo

set tmpdir = ${rootpath}/tmp
set tmpout = ${tmpdir}/tmpouts9
cd ${rootpath}/sample/s9

foreach n (1k 8k 64k)
  if((${n} != "64k") || ((${flag} != "s") && (${n} == "64k"))) then     
    echo
    if (${n} == "1k") then
      echo "---------- s9: pl1k ----------------------------"
      echo 128.0 256.0 10.0 1.0 pl1k 2 8.0 7 > inputpara9.${gdevice}
    endif
    if (${n} == "8k") then
      echo "---------- s9: pl8k ----------------------------"
      echo 128.0 256.0 1.0 0.125 pl8k 2 8.0 7 > inputpara9.${gdevice}
    endif
    if (${n} == "64k") then
      echo "---------- s9: pl64k ----------------------------"
      grep DCM sample.${n}
      echo 128.0 256.0 0.5 0.0625 pl64k 2 8.0 7 > inputpara9.${gdevice}
    endif
    ./s9_gdr inputpara9.${gdevice} |& tee ${tmpout}.${gdevice}
    grep -E 'pot|total|DCM|DE' ${tmpout}.${gdevice} > ${tmpout}2.${gdevice}
    grep -E 'pot|total|DCM|DE' sample.${n}${gdrmodel} > ${tmpout}3.${gdevice}
    diff -w ${tmpout}2.${gdevice} ${tmpout}3.${gdevice} > /dev/null
    set s = ${status}
    if (${s} == 0) then
      echo OK 
    else
      echo NG 
      exit 1
    endif
  endif
end

set tmpout = ${tmpdir}/tmpouts8
cd ../s8
foreach n (1k 8k 64k)
  if((${n} != "64k") || ((${flag} != "s") && (${n} == "64k"))) then     
    echo
    if(${n} == "1k") then 
      echo "---------- s8: pl1k ----------------------------"
      echo 256.0 600.0 10.0 1.0 ../s9/pl1k 0.01 0.03 > inputpara.${gdevice}
    endif
    if(${n} == "8k") then 
      echo "---------- s8: pl8k ----------------------------"
      echo 2048.0 600.0 1.0 0.125 ../s9/pl8k 0.01 0.03 > inputpara.${gdevice}
    endif
    if(${n} == "64k") then 
      echo "---------- s8: pl64k ----------------------------"
      grep pot sample.${n}
      echo 16384.0 600.0 0.5 0.0625 ../s9/pl64k 0.01 0.03 > inputpara.${gdevice}
    endif
    ./s8_gdr inputpara.${gdevice} |& tee ${tmpout}.${gdevice}
    grep -E 'pot|total|error|nts' ${tmpout}.${gdevice} > ${tmpout}2.${gdevice}
    grep -E 'pot|total|error|nts' sample.${n}${gdrmodel} > ${tmpout}3.${gdevice}
    diff -w ${tmpout}2.${gdevice} ${tmpout}3.${gdevice} > /dev/null
    set s = ${status}
    if (${s} == 0) then
      echo OK 
    else
      echo NG 
      exit 1
    endif
  endif
end

echo
cd ../..
echo "Passed all tests." 
echo

exit 0

