#!/usr/bin/perl
#
#

$mypi = 3.14159;

# offsets for blocks  -- for normal mode pictures
$off1 = 0.0;
$off2 = 3.0;
$off3 = 2.0;
$off4 = 1.0;


######################################################
#  plot the 4-body problem, showing mode 1 only
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "calc4_mode1_a.ps";
  $term_options = "postscript color enhanced 'Helvetica,18' ";

  $cmdfile = "gnuplot.in";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'orange' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  # these draw vertical lines at equilib positions of each block
  printf CMDFILE "set arrow 1 from $off1,graph 0 to $off1,graph 1 nohead lt 3 lc rgb 'red'  \n";
  printf CMDFILE "set arrow 2 from $off2,graph 0 to $off2,graph 1 nohead lt 3 lc rgb 'sea-green'  \n";
  printf CMDFILE "set arrow 3 from $off3,graph 0 to $off3,graph 1 nohead lt 5 lc rgb 'blue'  \n";
  printf CMDFILE "set arrow 4 from $off4,graph 0 to $off4,graph 1 nohead lt 5 lc rgb 'orange'  \n";

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xzeroaxis  lw 3.5 \n";
  printf CMDFILE "set yzeroaxis  lw 3.5 \n";
  printf CMDFILE "set xlabel 'Position (m)  '  \n";
  printf CMDFILE "set ylabel 'Time (s) ' \n";
  printf CMDFILE "set title 'Motion of 4-block, 5-spring system, normal mode 1 ' \n";

  $cmd = "plot [-1:4][0:15] 'calc_4_mode1.out' using (\$4+$off1):2 ";
  $cmd .= "        with lines ls 1 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4_mode1.out' using (\$6+$off2):2 ";
  $cmd .= "        with lines ls 2 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4_mode1.out' using (\$8+$off3):2 ";
  $cmd .= "        with lines ls 3 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4_mode1.out' using (\$10+$off4):2 ";
  $cmd .= "        with lines ls 4 lw 5 t '' ";

  printf CMDFILE "$cmd \n";


  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }




######################################################
#  plot the 4-body problem, showing mode 2 only
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "calc4_mode2_a.ps";
  $term_options = "postscript color enhanced 'Helvetica,18' ";

  $cmdfile = "gnuplot.in";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'orange' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  # these draw vertical lines at equilib positions of each block
  printf CMDFILE "set arrow 1 from $off1,graph 0 to $off1,graph 1 nohead lt 3 lc rgb 'red'  \n";
  printf CMDFILE "set arrow 2 from $off2,graph 0 to $off2,graph 1 nohead lt 3 lc rgb 'sea-green'  \n";
  printf CMDFILE "set arrow 3 from $off3,graph 0 to $off3,graph 1 nohead lt 5 lc rgb 'blue'  \n";
  printf CMDFILE "set arrow 4 from $off4,graph 0 to $off4,graph 1 nohead lt 5 lc rgb 'orange'  \n";

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xzeroaxis  lw 3.5 \n";
  printf CMDFILE "set yzeroaxis  lw 3.5 \n";
  printf CMDFILE "set xlabel 'Position (m)  '  \n";
  printf CMDFILE "set ylabel 'Time (s) ' \n";
  printf CMDFILE "set title 'Motion of 4-block, 5-spring system, normal mode 2 ' \n";

  $cmd = "plot [-1:4][0:15] 'calc_4_mode2.out' using (\$4+$off1):2 ";
  $cmd .= "        with lines ls 1 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4_mode2.out' using (\$6+$off2):2 ";
  $cmd .= "        with lines ls 2 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4_mode2.out' using (\$8+$off3):2 ";
  $cmd .= "        with lines ls 3 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4_mode2.out' using (\$10+$off4):2 ";
  $cmd .= "        with lines ls 4 lw 5 t '' ";

  printf CMDFILE "$cmd \n";


  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }




######################################################
#  plot the 4-body problem, showing mode 3 only
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "calc4_mode3_a.ps";
  $term_options = "postscript color enhanced 'Helvetica,18' ";

  $cmdfile = "gnuplot.in";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'orange' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  # these draw vertical lines at equilib positions of each block
  printf CMDFILE "set arrow 1 from $off1,graph 0 to $off1,graph 1 nohead lt 3 lc rgb 'red'  \n";
  printf CMDFILE "set arrow 2 from $off2,graph 0 to $off2,graph 1 nohead lt 3 lc rgb 'sea-green'  \n";
  printf CMDFILE "set arrow 3 from $off3,graph 0 to $off3,graph 1 nohead lt 5 lc rgb 'blue'  \n";
  printf CMDFILE "set arrow 4 from $off4,graph 0 to $off4,graph 1 nohead lt 5 lc rgb 'orange'  \n";

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xzeroaxis  lw 3.5 \n";
  printf CMDFILE "set yzeroaxis  lw 3.5 \n";
  printf CMDFILE "set xlabel 'Position (m)  '  \n";
  printf CMDFILE "set ylabel 'Time (s) ' \n";
  printf CMDFILE "set title 'Motion of 4-block, 5-spring system, normal mode 3 ' \n";

  $cmd = "plot [-1:4][0:15] 'calc_4_mode3.out' using (\$4+$off1):2 ";
  $cmd .= "        with lines ls 1 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4_mode3.out' using (\$6+$off2):2 ";
  $cmd .= "        with lines ls 2 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4_mode3.out' using (\$8+$off3):2 ";
  $cmd .= "        with lines ls 3 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4_mode3.out' using (\$10+$off4):2 ";
  $cmd .= "        with lines ls 4 lw 5 t '' ";

  printf CMDFILE "$cmd \n";


  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



######################################################
#  plot the 4-body problem, showing mode 4 only
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "calc4_mode4_a.ps";
  $term_options = "postscript color enhanced 'Helvetica,18' ";

  $cmdfile = "gnuplot.in";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'orange' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  # these draw vertical lines at equilib positions of each block
  printf CMDFILE "set arrow 1 from $off1,graph 0 to $off1,graph 1 nohead lt 3 lc rgb 'red'  \n";
  printf CMDFILE "set arrow 2 from $off2,graph 0 to $off2,graph 1 nohead lt 3 lc rgb 'sea-green'  \n";
  printf CMDFILE "set arrow 3 from $off3,graph 0 to $off3,graph 1 nohead lt 5 lc rgb 'blue'  \n";
  printf CMDFILE "set arrow 4 from $off4,graph 0 to $off4,graph 1 nohead lt 5 lc rgb 'orange'  \n";

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xzeroaxis  lw 3.5 \n";
  printf CMDFILE "set yzeroaxis  lw 3.5 \n";
  printf CMDFILE "set xlabel 'Position (m)  '  \n";
  printf CMDFILE "set ylabel 'Time (s) ' \n";
  printf CMDFILE "set title 'Motion of 4-block, 5-spring system, normal mode 4 ' \n";

  $cmd = "plot [-1:4][0:15] 'calc_4_mode4.out' using (\$4+$off1):2 ";
  $cmd .= "        with lines ls 1 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4_mode4.out' using (\$6+$off2):2 ";
  $cmd .= "        with lines ls 2 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4_mode4.out' using (\$8+$off3):2 ";
  $cmd .= "        with lines ls 3 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4_mode4.out' using (\$10+$off4):2 ";
  $cmd .= "        with lines ls 4 lw 5 t '' ";

  printf CMDFILE "$cmd \n";


  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



# offsets for blocks  -- for pictures after initial conditions
$off1 = 0.0;
$off2 = 1.0;
$off3 = 2.0;
$off4 = 3.0;



######################################################
#  plot the 4-body problem, showing result of initial condition
#       x1 = +L, x2 = 0, x3 = 0, x4 = 0, all velocities = 0
  
  my($output_file, $term_options);

  # we scale motions by this factor
  $sf = 0.5;

  # get the arguments
  $output_file = "calc4_shiftx1_a.ps";
  $term_options = "postscript color enhanced 'Helvetica,18' ";

  $cmdfile = "gnuplot.in";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'orange' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  # these draw vertical lines at equilib positions of each block
  printf CMDFILE "set arrow 1 from $off1,graph 0 to $off1,graph 1 nohead lt 3 lc rgb 'red'  \n";
  printf CMDFILE "set arrow 2 from $off2,graph 0 to $off2,graph 1 nohead lt 3 lc rgb 'sea-green'  \n";
  printf CMDFILE "set arrow 3 from $off3,graph 0 to $off3,graph 1 nohead lt 5 lc rgb 'blue'  \n";
  printf CMDFILE "set arrow 4 from $off4,graph 0 to $off4,graph 1 nohead lt 5 lc rgb 'orange'  \n";

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xzeroaxis  lw 3.5 \n";
  printf CMDFILE "set yzeroaxis  lw 3.5 \n";
  printf CMDFILE "set xlabel 'Position (m)  '  \n";
  printf CMDFILE "set ylabel 'Time (s) ' \n";
  printf CMDFILE "set title 'Motion of 4-block, 5-spring system, shift x1 only' \n";

  $cmd = "plot [-1:4][0:15] 'calc_4.out' using (($sf*\$4)+$off1):2 ";
  $cmd .= "        with lines ls 1 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4.out' using (($sf*\$6)+$off2):2 ";
  $cmd .= "        with lines ls 2 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4.out' using (($sf*\$8)+$off3):2 ";
  $cmd .= "        with lines ls 3 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4.out' using (($sf*\$10)+$off4):2 ";
  $cmd .= "        with lines ls 4 lw 5 t '' ";

  printf CMDFILE "$cmd \n";


  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }




######################################################
#  plot the 4-body problem, showing result of initial condition
#       x1 = +L, x2 = 0, x3 = 0, x4 = 0, all velocities = 0
#       plot closeup near time t = 0
  
  my($output_file, $term_options);

  # we scale motions by this factor
  $sf = 0.5;

  # get the arguments
  $output_file = "calc4_shiftx1_b.ps";
  $term_options = "postscript color enhanced 'Helvetica,18' ";

  $cmdfile = "gnuplot.in";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'orange' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  # these draw vertical lines at equilib positions of each block
  printf CMDFILE "set arrow 1 from $off1,graph 0 to $off1,graph 1 nohead lt 3 lc rgb 'red'  \n";
  printf CMDFILE "set arrow 2 from $off2,graph 0 to $off2,graph 1 nohead lt 3 lc rgb 'sea-green'  \n";
  printf CMDFILE "set arrow 3 from $off3,graph 0 to $off3,graph 1 nohead lt 5 lc rgb 'blue'  \n";
  printf CMDFILE "set arrow 4 from $off4,graph 0 to $off4,graph 1 nohead lt 5 lc rgb 'orange'  \n";

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xzeroaxis  lw 3.5 \n";
  printf CMDFILE "set yzeroaxis  lw 3.5 \n";
  printf CMDFILE "set xlabel 'Position (m)  '  \n";
  printf CMDFILE "set ylabel 'Time (s) ' \n";
  printf CMDFILE "set title 'Motion of 4-block, 5-spring system, shift x1 only' \n";

  $cmd = "plot [-1:4][0:2] 'calc_4.out' using (($sf*\$4)+$off1):2 ";
  $cmd .= "        with lines ls 1 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4.out' using (($sf*\$6)+$off2):2 ";
  $cmd .= "        with lines ls 2 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4.out' using (($sf*\$8)+$off3):2 ";
  $cmd .= "        with lines ls 3 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4.out' using (($sf*\$10)+$off4):2 ";
  $cmd .= "        with lines ls 4 lw 5 t '' ";

  printf CMDFILE "$cmd \n";


  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }





######################################################
#  plot the 4-body problem, showing result of initial condition
#       x1 = +L, x2 = 0, x3 = 0, x4 = 0, all velocities = 0
#       show longer time span
  
  my($output_file, $term_options);

  # we scale motions by this factor
  $sf = 0.5;

  # get the arguments
  $output_file = "calc4_shiftx1_c.ps";
  $term_options = "postscript color enhanced 'Helvetica,18' ";

  $cmdfile = "gnuplot.in";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'orange' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  # these draw vertical lines at equilib positions of each block
  printf CMDFILE "set arrow 1 from $off1,graph 0 to $off1,graph 1 nohead lt 3 lc rgb 'red'  \n";
  printf CMDFILE "set arrow 2 from $off2,graph 0 to $off2,graph 1 nohead lt 3 lc rgb 'sea-green'  \n";
  printf CMDFILE "set arrow 3 from $off3,graph 0 to $off3,graph 1 nohead lt 5 lc rgb 'blue'  \n";
  printf CMDFILE "set arrow 4 from $off4,graph 0 to $off4,graph 1 nohead lt 5 lc rgb 'orange'  \n";

  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xzeroaxis  lw 3.5 \n";
  printf CMDFILE "set yzeroaxis  lw 3.5 \n";
  printf CMDFILE "set xlabel 'Position (m)  '  \n";
  printf CMDFILE "set ylabel 'Time (s) ' \n";
  printf CMDFILE "set title 'Motion of 4-block, 5-spring system, shift x1 only' \n";

  $cmd = "plot [-1:4][0:50] 'calc_4.out' using (($sf*\$4)+$off1):2 ";
  $cmd .= "        with lines ls 1 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4.out' using (($sf*\$6)+$off2):2 ";
  $cmd .= "        with lines ls 2 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4.out' using (($sf*\$8)+$off3):2 ";
  $cmd .= "        with lines ls 3 lw 5 t '' ";
  $cmd .= " , ";
  $cmd .= "                         'calc_4.out' using (($sf*\$10)+$off4):2 ";
  $cmd .= "        with lines ls 4 lw 5 t '' ";

  printf CMDFILE "$cmd \n";


  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }






exit 0;
