Skip to content

Instantly share code, notes, and snippets.

@samiles
Last active February 26, 2023 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samiles/bc27066b7a8d6f0eb3a7b61f54c54f99 to your computer and use it in GitHub Desktop.
Save samiles/bc27066b7a8d6f0eb3a7b61f54c54f99 to your computer and use it in GitHub Desktop.
Calculate Time Dilation on a Space Trip Using Python
"""
This script calculates the time dilation experienced during a space journey given the
velocity, acceleration, and duration of the journey.
The script uses the astropy package to handle units and time calculations.
Args:
velocity (float): Velocity of the spaceship as a fraction of the speed of light.
acceleration (float): Acceleration of the spaceship in units of g0.
duration (float): Duration of the journey in years (from the perspective of a clock on earth).
Prints:
Date on Earth at departure
Date on spaceship at journey end
Date on Earth at journey end
Lorentz factor
Time dilation factor
Time interval on spaceship
More Info:
https://samiles.com/blog/2023/02/calculating-time-dilation-in-space-travel-with-python/
"""
import argparse
from astropy import constants as const
from astropy import units as u
from astropy.time import Time
import warnings
from astropy.utils.exceptions import AstropyWarning
# Ingore warnings about odd dates
warnings.simplefilter('ignore', category=AstropyWarning)
# Parse command line arguments
parser = argparse.ArgumentParser(description='Calculate the time dilation experienced during a space journey.')
parser.add_argument('velocity', type=float, help='velocity of the spaceship as a fraction of the speed of light')
parser.add_argument('acceleration', type=float, help='acceleration of the spaceship in units of g0')
parser.add_argument('duration', type=float, help='duration of the journey in years')
args = parser.parse_args()
# Set up input parameters
v = args.velocity * const.c # velocity of the spaceship
a = args.acceleration * const.g0 # acceleration of the spaceship
duration = args.duration * u.yr # duration of the journey
earth_time = Time.now() # Define starting time on Earth (as the current time)
# Calculate the Lorentz factor, using the formula γ = 1 / sqrt(1 - v^2/c^2)
gamma = 1 / (1 - (v / const.c) ** 2) ** 0.5
# Calculate the time dilation factor
dilation_factor = 1 / gamma
# Calculate the time interval measured on the spaceship
delta_t = dilation_factor * duration
# Calculate the date on the spaceship
ship_time = earth_time + delta_t
# Calculate the date on Earth after journey ends
earth_time_after = earth_time + duration
print('Date on Earth departure:', earth_time.iso)
print('Date on spaceship at journey end:', ship_time.iso)
print('Date on Earth at journey end:', earth_time_after.iso)
print('Lorentz factor: {:.2f}'.format(gamma))
print('Time dilation factor: {:.2f}'.format(dilation_factor))
print('Time interval on spaceship: {:.2f}'.format(delta_t.to(u.yr)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment