#!/bin/csh -f

set devid = $1
set ncard = 1
shift

# 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$$.devid${devid}.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 for devid${devid}. It may take more than 10 hours." |& tee -a ${logfile}
echo '' |& tee -a ${logfile}

set model = 800
set infile = pl8k
set nbody  = 8190

set i = 0

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

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

while (${i} <= ${ntry})
  set outfile_direct =   ${tmpdir}/tmp$$_direct${i}.devid${devid}.log
  set outfile_tree =     ${tmpdir}/tmp$$_tree${i}.devid${devid}.log
  set snapfile_direct =  ${tmpdir}/tmp$$_direct${i}.devid${devid}.snap
  set snapfile_tree =    ${tmpdir}/tmp$$_tree${i}.devid${devid}.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
