#!/usr/bin/perl
#
# Compute position of ball falling onto sphere of neutronium
#
# MWR 12/3/2012

# grav const (MKS)
$G = 6.67E-11;
# mass of sphere (kg)
$m2 = 1E12;
# mass of ball (kg)
$m1 = 1;
# timestep (s)
$dt = 0.001;

# starting and ending time (s)
$start_t = 0;
$end_t = 3.0;
# starting velocity (m/s)
$vy = 0;
# starting pos (m)
$y = 10;


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

  printf " t %9.4e  vy %9.4e  y %9.4e \n", $t, $vy, $y;

  # use simple Euler's method, count on small timestep
  $accel = -$G*$m2/($y*$y);
  if ($y < 0) {
    $accel = 0.0 - $accel;
  }

  $new_vy = $vy + $accel * $dt;
  $new_y = $y + $vy * $dt;

  $vy = $new_vy;
  $y = $new_y;
}

printf " t %9.4e  vy %9.4e  y %9.4e \n", $t, $vy, $y;




exit 0;
