#!/usr/bin/perl
# 
# generate some data to illustrate behavior of 
#   a coupled system
#
# MWR 2/19/2019

$debug = 1;

# avoid dividing by zero
$TINY = 1.0E-10;
$BIG = 1.0/$TINY;


$mypi = 3.14159;

# mass of each block (kg)
$m = 1.0;
# spring constant of each spring (N/m)
$k = 1.0;
# initial position of left-hand block (m)
$init_x1 = 0;
# initial position of right-hand block (m)
$init_x2 = 3;
$L = $init_x2;
# initial velocity of left-hand block (m/s)
$init_v1 = 0.0;
# initial velocity of right-hand block (m/s)
$init_v2 = 0.0;

# first normal freq (rad/s)
$omega_1 = sqrt($k / $m);
# second normal freq (rad/s)
$omega_2 = sqrt((3.0*$k) / $m);
# period of first normal freq (s)
$period_1 = (2.0*$mypi / $omega_1);
# period of second normal freq (s)
$period_2 = (2.0*$mypi / $omega_2);


# range of time to use in calculations
$n_per_period = 500;
$n_period = 5.0;
$start_t = 0;
$end_t = $n_period*$period_1;
$d_t = ($period_1/$n_per_period);

$outfile = "./make_coupled_data.out";
open(OUTFILE, ">$outfile") || die("can't open file $outfile");

printf OUTFILE "# data generated to illustrate coupled data \n";
printf OUTFILE "# mass m = $m (kg) \n";
printf OUTFILE "# spring constant  k = $k (N/m) \n";
printf OUTFILE "# first normal frequency omega_1 = $omega_1  (rad/s) \n";
printf OUTFILE "# second normal frequency omega_2 = $omega_2  (rad/s) \n";
printf OUTFILE "# at time t = 0, x1 = $init_x1  (m) \n";
printf OUTFILE "# at time t = 0, x2 = $init_x2  (m) \n";
printf OUTFILE "# at time t = 0, v1 = $init_v1  (m) \n";
printf OUTFILE "# at time t = 0, v2 = $init_v2  (m) \n";
printf OUTFILE "# \n";

for ($t = $start_t; $t <= $end_t; $t += $d_t) {

  # pos
  $x1 = 0.5*$L*(cos($omega_1*$t) - cos($omega_2*$t));
  $x2 = 0.5*$L*(cos($omega_1*$t) + cos($omega_2*$t));

  # velocity
  $v1 = 0.5*$L*((0.0 - $omega_1)*sin($omega_1*$t) + ($omega_2)*sin($omega_2*$t));
  $v2 = 0.5*$L*((0.0 - $omega_1)*sin($omega_1*$t) - ($omega_2)*sin($omega_2*$t));

  printf OUTFILE " t %12.5f  x1 %12.5f  x2 %12.5f   v1 %12.5f  v2 %12.5f  \n", 
            $t, $x1, $x2, $v1, $v2;
 
}


close(OUTFILE);


exit 0;

