#!/bin/sh -x

X="off"

test -f dumb || make 

# High tolerance circular orbit
test -f circular || \
   dumb 1.0 1000.0 0.05 1.0d-12 > circular

if [ $X = "on" ]; then
   echo 2 2 1 > ppinput 
   nf _1 _2 _3 0.0 _4 _5 0.0 < circular >> ppinput
   xfpp3d < ppinput
fi
# High tolerance elliptical orbit
test -f elliptical || \
   dumb 1.2 1000.0 0.05 1.0d-12 > elliptical

if [ $X = "on" ]; then
   echo 2 2 1 > ppinput 
   nf _1 _2 _3 0.0 _4 _5 0.0 < elliptical >> ppinput
   xfpp3d < ppinput
fi

# Low tolerance circular orbit
test -f elliptical-lo || \
    dumb 1.2 1000.0 0.05 1.0d-6 > elliptical-lo
if [ $X = "on" ]; then
   echo 2 2 1 > ppinput 
   nf _1 _2 _3 0.0 _4 _5 0.0 < elliptical-lo >> ppinput
   xfpp3d < ppinput
fi

test -f ppinput && /bin/rm ppinput

# Make plots

# Column:    1  2  3  4  5 6     7     8    9    10   11
# Quantity:  t x1 y1 x2 y2 theta omega KE_t KE_r PE_g E_tot   

test -f ket-e || nth 1 8 < elliptical > ket-e
test -f ker-e || nth 1 9 < elliptical > ker-e
test -f peg-e || nth 1 10 < elliptical > peg-e
test -f etot-e || nth 1 11 < elliptical > etot-e

test -f ket-el || nth 1 8 < elliptical-lo > ket-el
test -f ker-el || nth 1 9 < elliptical-lo > ker-el
test -f peg-el || nth 1 10 < elliptical-lo > peg-el
test -f etot-el || nth 1 11 < elliptical-lo > etot-el

test -f om-c.ps || gnuplot<<END
set terminal postscript portrait
set output "om-c.ps"
set size square
set title "Orbiting Dumbbell Problem\nCircular Orbit\
 -- Tolerance=10(-12)"
set xlabel "t"
set ylabel "omega"
plot [0:60] [0:2.2] "circular" using (\$1):(\$7)\
 notitle with lines
END


test -f om-e.ps || gnuplot<<END
set terminal postscript portrait
set output "om-e.ps"
set size square
set title "Orbiting Dumbbell Problem\nElliptical Orbit\
 -- Tolerance=10(-12)"
set xlabel "t"
set ylabel "omega"
plot [0:1000] [-1:3.5] "elliptical" using (\$1):(\$7)\
  notitle with lines
END


test -f om-ez.ps || gnuplot<<END
set terminal postscript portrait
set output "om-ez.ps"
set size square
set title "Orbiting Dumbbell Problem\nElliptical Orbit\
 -- Tolerance=10(-12)"
set xlabel "t"
set ylabel "omega"
plot [0:200] [-0.5:2.8] "elliptical" using (\$1):(\$7)\
  notitle with lines
END


test -f kerot-12.ps || gnuplot<<END
set terminal postscript portrait
set output "kerot-12.ps"
set size square
set title "Orbiting Dumbbell Problem\nElliptical Orbit\n\
Rotational Kinetic Energy -- Tolerance=10(-12)"
set xlabel "t"
set ylabel " "
plot [0:1000] [0:0.1250] "elliptical" using (\$1):(\$9)\
  notitle with lines
END

test -f e-12.ps || gnuplot<<END
set terminal postscript portrait
set output "e-12.ps"
set size square
set title "Orbiting Dumbbell Problem\nElliptical Orbit -- \
Energy uantities -- Tolerance=10(-12)\nTop to Bottom: \
KE_t, KE_r, E_tot, PE_g"
set xlabel "t"
set ylabel " "
plot [0:1000] [-2:1.5] \
     "ket-e" notitle with lines, \
     "ker-e" notitle with lines, \
     "peg-e" notitle with lines, \
     "etot-e" notitle with lines 
quit
END

etot0=`lino 1 < etot-e | nth 2`
test -f etot-12.ps || gnuplot<<END
set terminal postscript portrait
set output "etot-12.ps"
set size square
set title "Orbiting Dumbbell Problem\nElliptical Orbit -- \
Deviation in Total Energy -- Tolerance=10(-12)"
set xlabel "t"
set ylabel " "
plot [0:1000] "etot-e" using (\$1):($etot0-\$2) notitle with lines 
quit
END

test -f e-6.ps || gnuplot<<END
set terminal postscript portrait
set output "e-6.ps"
set size square
set title "Orbiting Dumbbell Problem\nElliptical Orbit -- \
Energy Quantities -- Tolerance=10(-6)\nTop to Bottom: \
KE_t, KE_r, E_tot, PE_g"
set xlabel "t"
set ylabel " "
plot [0:1000] [-2:1.5] \
     "ket-el" notitle with lines, \
     "ker-el" notitle with lines, \
     "peg-el" notitle with lines, \
     "etot-el" notitle with lines 
quit
END

ls -lt *ps
