mass_to_energy.html
001| """Convert Mass to Energy.
001|
001| Calculate the energy in a given mass (in kilograms).
001| Energy is given in terms of Joules, electron-volts,
001| Kilowatt-hours, BTUs, foot-pounds, and tons of TNT.
001|
001| Run without command line arguments to see the default
001| (one kilogram) or provide the kilograms as the first
001| argument.
001| """
002|
003| from sys import argv
004| from os import path
005|
006|
007| """Speed of Light."""
008| C = 299792458
009|
010| """Speed of Light Squared."""
011| C2 = pow(C,2)
012|
013|
014| """Convert Mass to Joules (kg*(m^2)*(s^-2)). [E=mc2]"""
015| Joules = lambda m: (float(m) * float(C2))
016|
017| """Convert Joules to Electron-Volts."""
018| EVolt = lambda e: (float(e) / 1.60218e-19)
019|
020| """Convert Joules to Kilowatt-Hours."""
021| KWh = lambda e: (float(e) / 3.6e6)
022|
023| """Convert Joules to BTUs."""
024| BTU = lambda e: (float(e) / 1.05505e3)
025|
026| """Convert Joules to Foot-Pounds."""
027| FtLb = lambda e: (float(e) / 1.35575)
028|
029| """Convert Joules to Tons (of TNT)."""
030| Tons = lambda e: (float(e) / 4.184e9)
031|
032|
033|
034| def mass2energy (mass=1.0):
035| '''Convert kilograms to energy per E=mc2.'''
036|
037|
038| energy = Joules(mass)
039|
040|
041| volts = EVolt(energy)
042| watts = KWh(energy)
043| btu = BTU(energy)
044| ftlbs = FtLb(energy)
045| tons = Tons(energy)
046|
047|
048| print('%12.5e kg' % mass)
049| print('%12.5e Joules' % energy)
050| print('%12.5e eVolts' % volts)
051| print('%12.5e KW hours' % watts)
052| print('%12.5e BTU' % btu)
053| print('%12.5e foot-pounds' % ftlbs)
054| print('%12.5e tons TNT' % tons)
055| print()
056|
057|
058| return (energy, volts, watts, btu, ftlbs, tons)
059|
060|
061| def main (*args):
062| """Parse commandline argument(s) and call the mass2energy function."""
063| mass = float(args[0]) if 0 < len(args) else 1.0
064|
065|
066| stats = mass2energy(mass)
067|
068|
069| return stats
070|
071|
072| if __name__ == '__main__':
073| print(f'running: {argv[0]}')
074| print()
075|
076| rv = main(*argv[1:])
077| print()
078|
079| """eof"""
080|