#!/usr/bin/perl
#
#

$mypi = 3.14159;

##########################################
#  plot the motion of two blocks in a coupled system
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "coupled_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 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \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 samples 500  \n";
  printf CMDFILE "set ylabel 'Position (m)  '  \n";
  printf CMDFILE "set xlabel 'Time (s) ' \n";
  printf CMDFILE "set title 'Motion of blocks in a coupled system ' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [0:20][-4:4] 'make_coupled_data.out' using 2:4 ";
  $cmd .= "        with lines ls 1 lw 3 t 'left block (x1)' ";
  $cmd .= " , ";
  $cmd .= "                  'make_coupled_data.out' using 2:6 ";
  $cmd .= "        with lines ls 3 lw 3 t 'right block (x2)' ";

  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 values of normal coordinates in a coupled system
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "coupled_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 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \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 samples 500  \n";
  printf CMDFILE "set ylabel 'Position (m)  '  \n";
  printf CMDFILE "set xlabel 'Time (s) ' \n";
  printf CMDFILE "set title 'Motion of blocks in a coupled system ' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [0:20][-4.5:4.5] 'make_coupled_data.out' using 2:(\$4+\$6) ";
  $cmd .= "        with lines ls 2 lw 4 t 's1 = x1 + x2' ";
  $cmd .= " , ";
  $cmd .= "                  'make_coupled_data.out' using 2:(\$6-\$4) ";
  $cmd .= "        with lines ls 5 lw 3 t 's2 = x2 - x1' ";

  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 motion of two blocks in a coupled system
#     this is from the "Challenge" section, with different properties
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "coupled_a_2.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 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \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 samples 500  \n";
  printf CMDFILE "set ylabel 'Position (m)  '  \n";
  printf CMDFILE "set xlabel 'Time (s) ' \n";
  printf CMDFILE "set title 'Motion of blocks in Challenge system ' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [0:20][-4:4] 'make_coupled_data_2.out' using 2:4 ";
  $cmd .= "        with lines ls 1 lw 3 t 'left block (x1)' ";
  $cmd .= " , ";
  $cmd .= "                  'make_coupled_data_2.out' using 2:6 ";
  $cmd .= "        with lines ls 3 lw 3 t 'right block (x2)' ";

  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 motion of two blocks in a coupled system
#     this is from the "Challenge" section, with different properties
#     this plots the normal coordinates of the system (s1 and s2)
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "coupled_c_2.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 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \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 samples 500  \n";
  printf CMDFILE "set ylabel 'Normal coordinate (m)  '  \n";
  printf CMDFILE "set xlabel 'Time (s) ' \n";
  printf CMDFILE "set title 'Motion of blocks in Challenge system ' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [0:20][-4:4] 'make_coupled_data_2.out' using 2:((\$4+\$6)) ";
  $cmd .= "        with lines ls 2 lw 3 t 's1' ";
  $cmd .= " , ";
  $cmd .= "                  'make_coupled_data_2.out' using 2:((\$4-\$6)) ";
  $cmd .= "        with lines ls 4 lw 3 t 's2' ";

  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;
