from net.willware.Orbits import System, Vector, PointMass, Tether
from net.willware.Orbits.System import *
from java.awt import Color
import sys

incomingStartingRadius = earthRadius + 3000 * mile
outgoingStartingRadius = earthRadius + 20 * mile
incomingStartingVelocity = 14100

s = System()
LET_GO_TIME = 1.0e20
s.tmax = 3 * hour
s.dt = 0.1
s.time = 0.0

def getarg(args=sys.argv[1:]):
    return args.pop(0)

while 1:
    try:
        arg = getarg()
        if arg == "-tmax":
            s.tmax = eval(getarg())
        elif arg == "-letgo":
            LET_GO_TIME = eval(getarg())
    except IndexError:
        break

pinwheelMassRatio = 3.0
pinwheelRadius = earthRadius + 2000 * mile

lowpoint = earthRadius + 30 * mile
# 1.025 for mass ratio of 50
# 1.113 for mass ratio of 10
# 1.41 for mass ratio of 3
pinwheelVelocity = 1.4105 * circularVelocityFromRadius(pinwheelRadius)

if 1:
    totalPinMass = airplaneMass * (1 + pinwheelMassRatio)
    a = pinwheelMassRatio
    pinCog = (a * pinwheelRadius + lowpoint) / (a + 1)
    expected = ((a + 1) / a) * circularVelocityFromRadius(pinCog)
    print pinwheelVelocity / expected
    pinwheelVelocity = 1.0132 * expected

lowpointVelocity = earthRotationalVelocity

pinwheelCenter = PointMass(s, "Pinwheel",
                           pinwheelMassRatio * airplaneMass,
                           Vector(0, pinwheelRadius, 0),
                           Vector(pinwheelVelocity, 0, 0),
                           Color.MAGENTA)
ship = PointMass(s, "Ship", airplaneMass,
                 Vector(0, lowpoint, 0),
                 Vector(-lowpointVelocity, 0, 0),
                 Color.ORANGE)
tether = Tether(pinwheelRadius - lowpoint, LET_GO_TIME)
tether.setEnd1(pinwheelCenter)
tether.setEnd2(ship)
s.add(pinwheelCenter)
s.add(ship)
s.add(tether)
s.hack()

print "\nOutgoing ship:"
print ("min altitude %g miles" % (ship.minAltitude / mile))
print ("max altitude %g miles" % (ship.maxAltitude / mile))
print ("min altitude %g km" % (ship.minAltitude / 1000))
print ("max altitude %g km" % (ship.maxAltitude / 1000))
print ("max velocity %g m/sec" % ship.maxVelocity)
print ("final velocity %g m/sec" % (ship.previousVelocity))
print ("max acceleration %g Gs" %
       (ship.maxAcceleration / earthGravity))

print "\nPinwheel:"
print ("min altitude %g miles" % (pinwheelCenter.minAltitude / mile))
print ("max altitude %g miles" % (pinwheelCenter.maxAltitude / mile))
print ("min altitude %g km" % (pinwheelCenter.minAltitude / 1000))
print ("max altitude %g km" % (pinwheelCenter.maxAltitude / 1000))
print ("min velocity %g m/sec" % pinwheelCenter.minVelocity)
print ("max velocity %g m/sec" % pinwheelCenter.maxVelocity)

print "\nTether:"
print ("max tension %g newtons" % tether.getMaxTension())
