#!/bin/csh -f

# GRAPE-7 functionality test program (longcheck)
# version 2.1    18-Jan-2008

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

set tmpdir = ${rootpath}/tmp
set sampledir = ${rootpath}/sample/direct/snapshots
set mkdist = ${rootpath}/sample/direct/mkdist
set tmpdir = ${rootpath}/tmp
set direct = ${rootpath}/sample/direct/directtest_g7
set tree = ${rootpath}/sample/vtc/vtc_g7
set logfile = ${rootpath}/longcheck$$.log

set endt_direct = 100  # a single try takes a few tens of minutes.
# set endt_direct = 3000  # a single try takes some 10-hours even w/model600.
set endt_tree = 1

/bin/rm ${logfile} >& /dev/null

set algorithm = "toggle"
if ($1 == "") then
  set ntry = 30
  set algorithm = "toggle"
else if ($1 == "tree" || $1 == "direct") then
  set ntry = 30
  set algorithm = $1
else
  set ntry = $1
  if ($2 == "tree" || $2 == "direct") then
    set algorithm = $2
  endif
endif

echo ntry:$ntry  algorithm:$algorithm
echo `date` |& tee -a ${logfile}
echo "Start the tests. It may take more than 10 hours." |& tee -a ${logfile}
echo '' |& tee -a ${logfile}

set devid = 0
if ( $?GDEVICE ) then
  setenv G5_CARDS "$GDEVICE"
endif

if ( $?G5_CARDS ) then
    # set the device ID of the 1st card to 'devid'.
    set devid = `echo $G5_CARDS | sed 's/\([0-9]\).*$/\1/g'`
    set ncard = `echo $G5_CARDS | wc | awk '{print $2}'`
endif

set model = `${rootpath}/hibutil/lsgrape -d ${devid} | grep -v devid | sed 's/.*model\([0-9]\+\).*$/\1/g'`

switch (${model})
  case 100:
    set infile = pl4k
    set nbody  = 4095
  breaksw

  case 300:
    set infile = pl12k
    set nbody  = 12285
  breaksw

  case 600:
    set infile = pl24k
    set nbody  = 24570
  breaksw

  case 800:
    if ($?ncard) then 
      if ($ncard == 1) then
        set infile = pl8k
        set nbody  = 8190
      else if ($ncard == 2) then
        set infile = pl16k
        set nbody  = 16380
      else if ($ncard == 3) then
        set infile = pl24k
        set nbody  = 24570
      else
        set infile = pl32k
        set nbody  = 32760
      endif
    else
      set infile = pl32k
      set nbody  = 32760
    endif
  breaksw

  default:
    set infile = pl24k
  breaksw
endsw

set i = 0

set outfile_direct0 =  ${tmpdir}/tmp$$_direct0.log
set outfile_tree0 =    ${tmpdir}/tmp$$_tree0.log
set snapfile_direct0 = ${tmpdir}/tmp$$_direct0.snap
set snapfile_tree0 =   ${tmpdir}/tmp$$_tree0.snap

if (! -f ${tmpdir}/${infile}) then
  ${mkdist} ${nbody} 1 ${tmpdir}/${infile}
endif

while (${i} <= ${ntry})
  set outfile_direct =   ${tmpdir}/tmp$$_direct${i}.log
  set outfile_tree =     ${tmpdir}/tmp$$_tree${i}.log
  set snapfile_direct =  ${tmpdir}/tmp$$_direct${i}.snap
  set snapfile_tree =    ${tmpdir}/tmp$$_tree${i}.snap

  if ($algorithm == "tree") then
    goto LABEL_SKIPDIRECT
  endif

  echo '---------- try:' ${i} ' direct-summation ----------' |& tee -a ${logfile}
  ${direct} ${tmpdir}/${infile} ${snapfile_direct} ${endt_direct} |& tee ${outfile_direct}
  if (${i} != 0) then
    diff ${snapfile_direct} ${snapfile_direct0} > /dev/null
    set s = ${status}
    if (${s} == 0) then
      echo OK |& tee -a ${logfile}
      /bin/rm ${snapfile_direct}
      /bin/rm ${outfile_direct}
    else
      echo NG |& tee -a ${logfile}
      echo check failed. |& tee -a ${logfile}
      exit 1
    endif
  endif

LABEL_SKIPDIRECT:

  if ($algorithm == "direct") then
    goto LABEL_SKIPTREE
  endif

  echo '---------- try:' ${i} ' Barnes-Hut tree ----------' |& tee -a ${logfile}
  ${tree} -G -c -t 0.5 -p 1 -n 2048 -d 0.01 -e 0.02 -I 1048576 \
      -T ${endt_tree} -D ${endt_tree} -o ${snapfile_tree} |& tee ${outfile_tree}

  if (${i} != 0) then
    diff ${snapfile_tree} ${snapfile_tree0} > /dev/null
    set s = ${status}
    if (${s} == 0) then
      echo OK |& tee -a ${logfile}
      /bin/rm ${snapfile_tree}
      /bin/rm ${outfile_tree}
    else
      echo NG |& tee -a ${logfile}
      echo check failed. |& tee -a ${logfile}
      exit 1
    endif
  endif

LABEL_SKIPTREE:

  @ i++
end # i

/bin/rm ${outfile_direct0}
/bin/rm ${outfile_tree0}
/bin/rm ${snapfile_direct0}
/bin/rm ${snapfile_tree0}

echo ""  |& tee -a ${logfile}
echo "Passed all tests." |& tee -a ${logfile}
echo `date` |& tee -a ${logfile}

# chmod a+w ${logfile}

exit 0
