#!/usr/bin/perl
#
# Create a set of datafiles which can (later) be plotted
#    to create static plots of various sorts.
#
# Place all the datafiles into the current directory, with
#    names like:
#
#             bob_line_12.2.dat
#
#    where
#              bob_line_       describes the quantity inside file
#                                    (Bob's world line)
#
#              12.2            describes the time when this should be plotted
#                                    (12.2 years in Earth time since start)
#
# MWR 10/14/2008

$debug = 1;

# speed of Joe's ship, as fraction of c
$v = 0.9;
$gamma = 1.0 / sqrt( 1.0 - $v*$v);


$start_time = 0;
$end_time = 60.0;
$turn_time = 30.0;
$end_time = 60.0;
$d_time = 0.1;


# create a simple datafile with two lines:
#       first line is starting point of a line to draw
#       second line is ending point of a line to draw
#
# this datafile: ship location in ST diagram, in Earth coordinates, at t=30
#      (when ship reaches Planet P27)
#
$time = 30.0;
$outfile = sprintf "./joe_earth_%04.1f.dat", $time;
open(OUTFILE, ">$outfile") || die("can't open file $outfile");
printf OUTFILE " %8.4f %8.4f \n", 0, 0;
printf OUTFILE " %8.4f %8.4f \n", 27.0, 30.0;
close(OUTFILE);

# this datafile: ship location in ST diagram, in Earth coordinates, at t=60
#      (when ship returns to Earth Planet P27)
#      (show return portion of trip only)
#
$time = 60.0;
$outfile = sprintf "./joe_earth_%04.1f.dat", $time;
open(OUTFILE, ">$outfile") || die("can't open file $outfile");
printf OUTFILE " %8.4f %8.4f \n", 27.0, 30.0;
printf OUTFILE " %8.4f %8.4f \n", 0.0, 60.0;
close(OUTFILE);


# this datafile: time when Joe reaches planet P27, according to Earth measurements
#
$time = 30.0;
$outfile = sprintf "./earth_simul_%04.1f.dat", $time;
open(OUTFILE, ">$outfile") || die("can't open file $outfile");
printf OUTFILE " %8.4f %8.4f \n", 0, 30;
printf OUTFILE " %8.4f %8.4f \n", 100.0, 30.0;
close(OUTFILE);

# this datafile: time when Joe leaves Earth, according to Red Ship's measurements
#
$time = 0.0;
$outfile = sprintf "./red_simul_%04.1f.dat", $time;
open(OUTFILE, ">$outfile") || die("can't open file $outfile");
printf OUTFILE " %8.4f %8.4f \n", 0, 0;
printf OUTFILE " %8.4f %8.4f \n", 10*30.0, 10*27.0;
close(OUTFILE);


# this datafile: time when Joe reaches planet P27, according to Red Ship's measurements
#
$time = 30.0/$gamma;
$outfile = sprintf "./red_simul_%04.1f.dat", $time;
open(OUTFILE, ">$outfile") || die("can't open file $outfile");
$start_x = 0;
$start_y = ((30.0/$gamma)/$gamma);
$slope = $v;
$end_x = $start_x + 100;
$end_y = $start_y + $slope*100;
printf OUTFILE " %8.4f %8.4f \n", $start_x, $start_y;
printf OUTFILE " %8.4f %8.4f \n", $end_x, $end_y;
#printf OUTFILE " %8.4f %8.4f \n", 0, ((30.0/$gamma)/$gamma);
#printf OUTFILE " %8.4f %8.4f \n", 27.0, 30.0;
close(OUTFILE);


# this datafile: time when Joe boards Green Flagship at planet P27, 
#       according to Green Flagship's measurements
#
$time = 30.0/$gamma;
$outfile = sprintf "./green_simul_%04.1f.dat", $time;
$start_x = 0.0;
$start_y = 60.0 - ((30.0/$gamma)/$gamma);
$slope = 0.0 - $v;
$end_x = $start_x + 100;
$end_y = $start_y + $slope*100;
open(OUTFILE, ">$outfile") || die("can't open file $outfile");
printf OUTFILE " %8.4f %8.4f \n", $start_x, $start_y;
printf OUTFILE " %8.4f %8.4f \n", $end_x, $end_y;
#printf OUTFILE " %8.4f %8.4f \n", 0, 60.0 - ((30.0/$gamma)/$gamma);
#printf OUTFILE " %8.4f %8.4f \n", 27.0, 30.0;
close(OUTFILE);


# this datafile: a single moment when Joe is on Planet P27
#
$time = 30.0;
$outfile = sprintf "./joe_on_p27_%04.1f.dat", $time;
open(OUTFILE, ">$outfile") || die("can't open file $outfile");
printf OUTFILE " %8.4f %8.4f \n", 27.0, 30.0;
close(OUTFILE);


# this datafile: a single moment when Joe is on Earth before starting
#
$time = 0.0;
$outfile = sprintf "./joe_on_earth_%04.1f.dat", $time;
open(OUTFILE, ">$outfile") || die("can't open file $outfile");
printf OUTFILE " %8.4f %8.4f \n", 0, 0;
close(OUTFILE);

# this datafile: a single moment when Joe is back on Earth, having returned
#
$time = 60.0;
$outfile = sprintf "./joe_on_earth_%04.1f.dat", $time;
open(OUTFILE, ">$outfile") || die("can't open file $outfile");
printf OUTFILE " %8.4f %8.4f \n", 0, 60;
close(OUTFILE);


# this datafile: time when Joe leaves Earth, according to Earth clocks
#
$time = 0.0;
$outfile = sprintf "./earth_simul_%04.1f.dat", $time;
open(OUTFILE, ">$outfile") || die("can't open file $outfile");
printf OUTFILE " %8.4f %8.4f \n", 0, 0;
printf OUTFILE " %8.4f %8.4f \n", 100.0, 0.0;
close(OUTFILE);

# this datafile: time when Joe returns to Earth, according to Earth clocks
#
$time = 60.0;
$outfile = sprintf "./earth_simul_%04.1f.dat", $time;
open(OUTFILE, ">$outfile") || die("can't open file $outfile");
printf OUTFILE " %8.4f %8.4f \n", 0, $time;
printf OUTFILE " %8.4f %8.4f \n", 100.0, $time;
close(OUTFILE);



# this datafile: time when Joe reaches Earth,
#       according to Green Flagship's measurements
#
$time = 30 + 30.0/$gamma;
$outfile = sprintf "./green_simul_%04.1f.dat", $time;
$start_x = 0.0;
$start_y = 60.0;
$slope = 0.0 - $v;
$end_x = $start_x + 100;
$end_y = $start_y + $slope*100;
open(OUTFILE, ">$outfile") || die("can't open file $outfile");
printf OUTFILE " %8.4f %8.4f \n", $start_x, $start_y;
printf OUTFILE " %8.4f %8.4f \n", $end_x, $end_y;
#printf OUTFILE " %8.4f %8.4f \n", 0, 60.0 - ((30.0/$gamma)/$gamma);
#printf OUTFILE " %8.4f %8.4f \n", 27.0, 30.0;
close(OUTFILE);








exit 0;




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

  # Bob's world line
  $bob_file = sprintf "bob_line_%04.1f.dat", $earth_time;
  open(OUTFILE, ">$bob_file") || die("can't open file $bob_file");
  for ($t = $start_time; $t <= $earth_time; $t += $d_time) { 
    $x = 0;
    $y = $t;
    printf OUTFILE "%8.4f %8.4f \n", $x, $y;
  }
  close(OUTFILE);

  # Bob's birthdays
  $bob_bday_file = sprintf "bob_bday_%04.1f.dat", $earth_time;
  open(OUTFILE, ">$bob_bday_file") || die("can't open file $bob_bday_file");
  for ($t = $start_time; $t <= $earth_time; $t += 1.0) { 
    $x = 0;
    $y = $t;
    printf OUTFILE "%8.4f %8.4f \n", $x, $y;
  }
  close(OUTFILE);

  # Bob's outgoing messages to Joe
  $bob_msg_file = sprintf "bob_msg_%04.1f.dat", $earth_time;
  open(OUTFILE, ">$bob_msg_file") || die("can't open file $bob_msg_file");
  # loop over all the birthdays which have occurred so far
  for ($t = 0.0; $t <= $earth_time; $t += 1.0) { 

    # special case for time before first birthday
    #     we must create some data, so plotting script won't complain,
    #     but don't want to show any message, so draw far off plot
    if ($t < 1.0) {
      printf OUTFILE " -99.0  -99.0 \n";
      next;
    }

    if ($debug > 0) {
      printf OUTFILE "start Bob message sent at birthday %5.2f \n", $t;
    }
    # we follow each message until its position is equal to, 
    #        or greater than, Joe's position at the same time
    for ($msg_t = $t; $msg_t <= $earth_time; $msg_t += 0.1) {
      $msg_x = 0 + ($msg_t - $t)*1.0;
      $msg_y = $msg_t;
      if ($msg_t <= $turn_time) {
        $joe_x = $msg_t*$v;
      }
      else {
        $joe_x = ($turn_time*$v) - $v*($msg_t - $turn_time);
      }
      if ($debug > 0) {
        printf OUTFILE "   msg_t  %5.2f   msg %5.2f %5.2f  joe_x %5.2f \n", 
                       $msg_t, $msg_x, $msg_y, $joe_x;
      }
      if ($msg_x >= $joe_x) {
        if ($debug > 0) {
          printf OUTFILE "  msg_x %5.2f >= joe_x %5.2f , so quit \n", $msg_x, $joe_x;
        }
        last;
      }
      printf OUTFILE "%8.4f %8.4f \n", $msg_x, $msg_y;
    }

  }
  close(OUTFILE);

  


    
  # Joe's world line
  $joe_file = sprintf "joe_line_%04.1f.dat", $earth_time;
  open(OUTFILE, ">$joe_file") || die("can't open file $joe_file");
  for ($t = $start_time; $t <= $earth_time; $t += $d_time) { 
    if ($t <= $turn_time) {
      $x = $t*$v;
      $y = $t;
    }
    else {
      $x = ($turn_time*$v) - $v*($t - $turn_time);
      $y = $t;
    }
    printf OUTFILE "%8.4f %8.4f \n", $x, $y;
  }
  close(OUTFILE);
    
  # Joe's birthdays
  $joe_bday_file = sprintf "joe_bday_%04.1f.dat", $earth_time;
  open(OUTFILE, ">$joe_bday_file") || die("can't open file $joe_bday_file");
  for ($joe_t = $start_time; $joe_t*$gamma <= $earth_time; $joe_t += 1.0) { 
    $earth_t = $joe_t*$gamma;
    if ($earth_t < $turn_time) {
      $x = $earth_t*$v;
      $y = $earth_t;
    }
    else {
      $x = ($turn_time*$v) - $v*($earth_t - $turn_time);
      $y = $earth_t;
    }
    printf OUTFILE "%8.4f %8.4f \n", $x, $y;
  }
  close(OUTFILE);

  # Joe's outgoing messages to Bob
  $joe_msg_file = sprintf "joe_msg_%04.1f.dat", $earth_time;
  open(OUTFILE, ">$joe_msg_file") || die("can't open file $joe_msg_file");
  # loop over all the birthdays which have occurred so far
  for ($joe_t = 0.0; $joe_t*$gamma <= $earth_time; $joe_t += 1.0) { 

    $earth_t = $joe_t*$gamma;

    # special case for time before first birthday
    #     we must create some data, so plotting script won't complain,
    #     but don't want to show any message, so draw far off plot
    if ($joe_t < 1.0) {
      printf OUTFILE " -99.0  -99.0 \n";
      next;
    }

    if ($debug > 0) {
      printf OUTFILE "start Joe message sent at birthday %5.2f \n", $joe_t;
    }
    # we follow each message until its position is equal to zero
    if ($earth_t <= $turn_time) {
      $joe_x = $earth_t*$v;
    }
    else {
      $joe_x = ($turn_time*$v) - $v*($earth_t - $turn_time);
    }
   
    for ($msg_t = $earth_t; $msg_t <= $earth_time; $msg_t += 0.1) {
      $msg_x = $joe_x - ($msg_t - $earth_t)*1.0;
      $msg_y = $msg_t;
      if ($debug > 0) {
        printf OUTFILE "   msg_t  %5.2f   msg %5.2f %5.2f  joe_x %5.2f \n", 
                       $msg_t, $msg_x, $msg_y, $joe_x;
      }
      if ($msg_x <= 0) {
        if ($debug > 0) {
          printf OUTFILE "  msg_x %5.2f <= 0 , so quit \n", $msg_x;
        }
        last;
      }
      printf OUTFILE "%8.4f %8.4f \n", $msg_x, $msg_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);
}


