#!/bin/csh -f

set fnames =  (pl1k pl2k pl4k pl8k pl12k pl16k pl24k pl32k)
set nbodies = (1024 2048 4095 8190 12285 16380 24570 32760) # (4096-1) x n

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

cd ${rootpath}/sample/direct
set mkdist = ./mkdist
set direct = ./directtest_g7
set snapdir = ./snapshots

if ($1 == "") then
cat << EOF
 integrate "in file" up to "end time" to generate
 NEMO ascii snapshots which is used by check.
 input/output files reside in g7pkg/direct/snapshots/.

 usage: ./gensnap <in file> [end time]
   in file:  initial distribution of particles stored
             in a NEMO ascii file.
   end time: time to stop the run (default:100).
EOF
exit
else
  set infile = $1
endif

set i = 1
while (${i} <= ${#fnames})
  if (${infile} == $fnames[${i}]) then
    break;
  endif
  @ i++
end # i

if (${i} > ${#fnames}) then
  echo "unknown in file name. abort."
  exit 1
endif

echo "input file: ${infile}    number of paritlces: ${nbodies[${i}]}"

${mkdist} ${nbodies[${i}]} 1 ${infile}

if ($2 == "") then
  set endt = 100
else
  set endt = $2
endif
echo "end time: ${endt}"

${direct} ./${infile} tmpsnap$$ ${endt} |& tee tmpout$$

grep "floating-point" tmpout$$
set s = ${status}
if (${s} == 0) then
  set pipetype = fp
else
  set pipetype = log
endif


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}'`
else
    set ncard = 1
    set devid = 0
endif

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

set samplefile = ./${snapdir}/model${model}${pipetype}${infile}T${endt}.snap
set outfile = ./${snapdir}/model${model}${pipetype}${infile}T${endt}.log

/bin/mv tmpsnap$$ ${samplefile}
/bin/mv tmpout$$ ${outfile}
