#!/bin/csh -f

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

echo "-----------------------------------------------------------------"
echo " GRAPE-DR functionality test program (longcheck)"
echo ""
echo " usage: longcheck [ntry] [direct|its|direct1k|its1k]"
echo "-----------------------------------------------------------------"

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

if ($1 == "") then
  set ntry = 30
else
  set ntry = $1
endif

if ($2 == "its" || $2 == "direct" ||$2 == "direct1k" || $2 == "its1k") then
  set algorithm = $2
else
  set algorithm = "its1k"
endif

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

echo ${hname}
echo ntry:$ntry  algorithm:$algorithm
echo `date` 
echo "Start the tests. It may take many hours." 
echo ''

set tmpdir = ${rootpath}/tmp
set i = 0

while (${i} <= ${ntry})

  if (($algorithm == "direct")||($algorithm == "direct1k")) then
    cd ${rootpath}/sample/s9
    if ($algorithm == "direct") then
    echo '---------- try:' ${i} ' direct-summation 64k ----------' 
    echo 128.0 256.0 4.0 0.25 pl64k 2 8.0 7 > ${tmpdir}/inputpara9.${gdevice}.${hname}
    endif  
    if ($algorithm == "direct1k") then
    echo '---------- try:' ${i} ' direct-summation 1k ----------' 
    echo 128.0 256.0 10.0 10.0 pl1k 2 8.0 7 > ${tmpdir}/inputpara9.${gdevice}.${hname}
    endif  
    ./s9_gdr ${tmpdir}/inputpara9.${gdevice}.${hname} |& tee ${tmpdir}/tmpoutlong.${gdevice}.${hname}
  endif

  if (($algorithm == "its")||($algorithm == "its1k")) then
    cd ${rootpath}/sample/s8
    if ($algorithm == "its") then
    echo '---------- try:' ${i} ' individual timestep 64k ----------' 
    echo 1024.0 600.0 1.0 0.125 pl64k 0.01 0.03 > ${tmpdir}/inputpara.${gdevice}.${hname}
    endif  
    if ($algorithm == "its1k") then
    echo '---------- try:' ${i} ' individual timestep 1k ----------' 
    echo 1024.0 600.0 10.0 10.0 pl1k 0.01 0.03 > ${tmpdir}/inputpara.${gdevice}.${hname}
    endif  
    ./s8_gdr ${tmpdir}/inputpara.${gdevice}.${hname} |& tee ${tmpdir}/tmpoutlong.${gdevice}.${hname}
  endif

  grep -v speed ${tmpdir}/tmpoutlong.${gdevice}.${hname} | grep -v cputime | grep -v temp | grep -v incorrect > ${tmpdir}/tmpoutlong2.${gdevice}.${hname}
  if (${i} == 0) then
    mv ${tmpdir}/tmpoutlong2.${gdevice}.${hname} ${tmpdir}/tmpoutlong0.${gdevice}.${hname}
    echo
  endif 
  if (${i} != 0) then
    diff ${tmpdir}/tmpoutlong0.${gdevice}.${hname} ${tmpdir}/tmpoutlong2.${gdevice}.${hname} > /dev/null
    set s = ${status}
    if (${s} == 0) then
      echo
      echo OK 
      echo
    else
      echo NG 
      echo check failed. 
      exit 1
    endif
  endif
#  cd ${rootpath}
  cd ../../

  if (${i} != ${ntry}) then
    sleep 10
  endif
  @ i++

end # i

echo "" 
echo "Passed all tests." 
echo `date` 
echo

exit 0
