#!/usr/bin/perl
#
# Create a set of datafiles which can (later) be plotted
#    to create a little movie showing wave motion.
#
# Place all the datafiles into the current directory, with
#    names like:
#
#             fluff_12.2.dat
#
#    where
#              fluff_          describes the subject (either a single
#                                    piece of fluff, or the entire rope)
#
#              12.2            describes the time (in seconds)
#
# MWR 11/24/2013

$debug = 1;

# get rid of old versions of the datafiles
$cmd = "/bin/rm fluff_[0-9][0-9].[0-9].dat";
$ret = exec_cmd($cmd);
$cmd = "/bin/rm rope_[0-9][0-9].[0-9].dat";
$ret = exec_cmd($cmd);

# amplitude of oscillations (meters)
$amplitude = 0.30;
# angular frequency of oscillation at fluff's position (rad/sec)
$omega = 1.0;
# position of left-hand end of rope (meters)
$left_pos = -30.0;
# position of righ-hand end of rope (meters)
$right_pos = 30.0;
# steps in position (meters)
$d_pos = 0.5;
# wave number of oscillation on rope (rad/meter)
$k = 0.2;



$start_time = 0;
$end_time = 60.0;
$d_time = 0.5;


for ($time = $start_time; $time <= $end_time + ($d_time/2.0); 
                 $time += $d_time) {

  # fluff datafile
  $fluff_file = sprintf "fluff_%04.1f.dat", $time;
  open(OUTFILE, ">$fluff_file") || die("can't open file $fluff_file");
  for ($t = $start_time; $t <= $time; $t += $d_time) { 
    $x = 0;
    $y = $amplitude*cos($omega*$t);
    printf OUTFILE "%8.4f %8.4f %8.4f \n", $t, $x, $y;
  }
  close(OUTFILE);


  # rope datafile
  $rope_file = sprintf "rope_%04.1f.dat", $time;
  open(OUTFILE, ">$rope_file") || die("can't open file $rope_file");
  for ($x = $left_pos; $x <= $right_pos; $x += $d_pos) { 
    $y = $amplitude*cos($k*$x + $omega*$time);
    printf OUTFILE "%8.4f %8.4f %8.4f \n", $time, $x, $y;
  }
     # and also print the position of the fluff at this time
     #    in a separate stanze, at the end of the file
    printf OUTFILE "\n";
    printf OUTFILE "\n";
    $fluff_x = 0.0;
    $fluff_y = $amplitude*cos($omega*$time);
    printf OUTFILE "%8.4f %8.4f %8.4f \n", $time, $fluff_x, $fluff_y;

  close(OUTFILE);


}


exit 0;





##############################################################################
# PROCEDURE: exec_cmd
#
# DESCRIPTION: Execute the given shell command line.  If the $debug flag
#              is set, we print to stdout the command line, and
#              also print out the result string.
#
# RETURNS:
#              the result of the command
#
#
sub exec_cmd {

  my($cmd, $ret);

  $cmd = $_[0];

  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;
  if ($debug > 0) {
    printf "ret is ..$ret.. \n";
  }

  return($ret);
}


