#! /bin/sh

############################################################
# Script which runs 'tlsoda' with a variety of tolerance 
# settings, checks one solution using "independent residual 
# evaluation", and demonstrates dependence of results on
# number of requested output times.
############################################################

# Integrate from 0 .. 10
tmax=10.0

# Exact solution is sin(t)
utmin=0.0
dutmin=1.0

olevel=8
tols="1.0e-6 1.0e-8 1.0e-10 1.0e-12"

# Make sure executable exists, make if it isn't
test -f tlsoda || make

echo "---------------------------------------------"
echo "Running tlsoda with the following tolerances:";
echo "  $tols"
echo "---------------------------------------------"
for tol in $tols; do
   echo "Tolerance: $tol"; 
   tlsoda $tmax $utmin $dutmin $tol $olevel > tlsoda-out-$tol
#  Create file with column 1=x column 2=u
   nth 1 2 < tlsoda-out-$tol > tlsoda-u-$tol
#  Create file with column 1=x column 2=abs(u_xct - u)
   nth 1 3 < tlsoda-out-$tol | nf _1 'abs(_2)' > tlsoda-err-$tol
done
echo

checktol="1.0e-12"
echo "---------------------------------------------"
echo "Applying O(dt^2) approximation of ODE to "
echo "tol=$checktol results"
echo "---------------------------------------------"
echo "          dt                    rms(residual)"
for inc in 8 4 2 1; do
#  'lines' is a filter which selects line-number ranges
   nth 1 2 < tlsoda-out-$checktol | lines 1 . $inc | chk-tlsoda
done
echo

echo "---------------------------------------------"
echo "Demonstrating dependence of results on number"
echo "of requested output times"
echo "---------------------------------------------"
for tol in $tols; do
   echo "Tolerance: $tol";
   echo "No additional output times"
   tlsoda $tmax $utmin $dutmin $tol 0 > /dev/null
   echo "256 output times"
   tlsoda $tmax $utmin $dutmin $tol 8 > /dev/null
   echo "65536 output times"
   tlsoda $tmax $utmin $dutmin $tol 16 > /dev/null
   echo
done

# Make plots of soln and error
gnuplot < gpin
gnuplot < gpine