#!/bin/sh
#
# $Id: sanity_test,v 1.11 2014/11/19 00:17:39 rauch Exp $
#
# Verifies the results of a small integration by comparing with a
# known good result.
#
. ./procs.sh
testing "validity of a sample integration"

cleanup() { rm -f sanity.hnb sanity.log planets?.*.dat; }


# Run the sample integration with a particular algorithm.
# $1 = tolerance
dotest() {
  echo ">`head -n 1 sanity.hnb`"
  if tryexec $BIN/hnbody -l sanity.log sanity.hnb; then
    echo `grep '^CPU' sanity.log`
    echo `grep '^Energy' sanity.log`

    tryexec $BIN/hnarith -s planets1.*.dat planets.txt |
      tryexec $BIN/hnarith -d - planets.txt |
      tryexec $BIN/hnstats |
      awk '/^data/ {printf "%-14s", $3}; \
	   /^ max/ {del=($3<0.0 ? -$3 : $3); \
		    grade=(del<'"$1"' ? "ok" : "FAIL"); \
		    printf "%9.2e --- %s\n", del, grade}' > sanity.log
    cat sanity.log
    grep -iq fail sanity.log && status=1
  fi
  result "`awk '/^Integrator/ {print $2}' sanity.hnb` algorithm"
  cleanup
}

cat - >sanity.hnb <<EOT
Integrator: Symplectic
IntegCoord: Jacobi Order2 Drift-Kick
Corrector: True
StepSize: 0.25
Include: planets.hnb
EOT
dotest 1e-9

cat - >sanity.hnb <<EOT
Integrator: Bulirsch-Stoer 1e-14
StepSize: 1
Include: planets.hnb
EOT
dotest 3e-7

cat - >sanity.hnb <<EOT
Integrator: Runge-Kutta 1e-14
StepSize: 1
Include: planets.hnb
EOT
dotest 1e-6

cleanup
exit 0
