#!/bin/csh -f

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 logfile = ${rootpath}/checknb.log
/bin/rm ${logfile} >& /dev/null

tee ${logfile} << DESC_END
-----------------------------------------------------------------
 GRAPE-7 neighbor search functionality test program (checknb)

 usage: ./checknb [anim]

 anim: test with animation (not recommended for strict check).
-----------------------------------------------------------------

This script checks the neighbor search function of G5nb backend design
of GRAPE-7. It performs many-body simulations and compare their
results with precalculated ones. it can be used for check with a
single card only.

DESC_END

set ncard = `cat ${rootpath}/.nhib`
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

if (${ncard} == 1) then
    goto LABEL_START
endif

cat << MCWARN_END

[For Multiple Cards Users:]
Be sure to set environment variable "G5_CARDS" to specify a single
card. For example, in order to test a card with device ID 2, you
should set the variable as:

  setenv G5_CARDS "2"

The test would fail if multiple cards are specified.

MCWARN_END

echo -n "Continue? (y/n): "
set answer = $<
if ( $answer == "y") then
  goto LABEL_START
else
  exit (1)
endif

LABEL_START:

echo "Start the tests. It may take a few minutes." |& tee -a ${logfile}
echo '' |& tee -a ${logfile}

if ($1 == "anim" || $2 == "anim") then
  set anim = y
  set direct = ${rootpath}/sample/direct/directnba_g7
else
  set anim = n
  set direct = ${rootpath}/sample/direct/directnb_g7
endif

set rad = 0.2

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

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

  case 300:
    set infile = (pl1k pl12k)
    set nbody  = (1024 12285)
  breaksw

  case 600:
    set infile = (pl1k pl24k)
    set nbody  = (1024 24570)
  breaksw

  case 800:
    if ( $?G5_CARDS ) then
      if ($ncard == 1) then
        set infile = (pl1k pl4k)
        set nbody  = (1024 4095)
      else if ($ncard == 2) then
        set infile = (pl1k pl8k)
        set nbody  = (1024 8190)
      else if ($ncard == 3) then
        set infile = (pl1k pl12k)
        set nbody  = (1024 12285)
      else
        set infile = (pl1k pl16k)
        set nbody  = (1024 16380)
      endif
    else
      set infile = (pl1k pl16k)
      set nbody  = (1024 16380)
    endif
  breaksw

endsw

set i = 1
while (${i} <= ${#infile})
  set endt = 100    
  set tmpname    = ${tmpdir}/tmp$$_${infile[${i}]}T${endt}
  set snapfile   = ${tmpname}.snap
  set nbsnapfile = ${tmpname}.nb
  set outfile    = ${tmpname}.log
  echo '---------- in:' ${infile[${i}]}  'out:' ${snapfile}  'nbout:' ${nbsnapfile} 'endt:' ${endt} '----------'\
      |& tee -a ${logfile}

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

  ${direct} ${tmpdir}/${infile[${i}]} ${tmpname} ${endt} ${rad} |& tee ${outfile}
  set s = ${status}
  if (${s} == 1) then
    echo Abort. |& tee -a ${logfile}
    exit 1
  endif

  grep 'g5_nbsearch:available' ${outfile}
  set s = ${status}
  if (${s} == 0) then
    set pipetype = nb
  else
    echo NG |& tee -a ${logfile}
    echo neighbor search function not available. |& tee -a ${logfile}
    echo check failed. |& tee -a ${logfile}
    exit 1
  endif

  mv ${tmpname} ${snapfile}
  set samplefile = ${sampledir}/model${model}${pipetype}${infile[${i}]}T${endt}.snap
  diff ${snapfile} ${samplefile} > /dev/null
  set s = ${status}
  if (${s} == 0) then
    echo Force calculation: OK |& tee -a ${logfile}
  else
    echo Force calculation: NG |& tee -a ${logfile}
    echo check failed. |& tee -a ${logfile}
    exit 1
  endif

  set samplefile = ${sampledir}/model${model}x${ncard}${pipetype}${infile[${i}]}T${endt}.nb
  diff ${nbsnapfile} ${samplefile} > /dev/null
  set s = ${status}
  if (${s} == 0) then
    echo Neighbor search: OK |& tee -a ${logfile}
    /bin/rm ${snapfile}
    /bin/rm ${nbsnapfile}
    /bin/rm ${outfile}
  else
    echo Neighbor search: NG |& tee -a ${logfile}
    echo check failed. |& tee -a ${logfile}
    exit 1
  endif

  @ i++

end # i

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

exit 0
