Advertisement
Guest User

payload2leo.py

a guest
May 21st, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.55 KB | None | 0 0
  1. # Neph adapted this from Momo
  2. # https://repl.it/repls/WellwornBowedDemos
  3.  
  4. # Import packages
  5. import numpy as np
  6.  
  7. # Multi-stage deltaV calculator. All arguments must be lists of the same length except the int 'payload'
  8. def getdV(isp, wet, dry, payload):
  9.   dV = 0
  10.   wet.append(0)
  11.   for i in range(len(isp)):
  12.     dV += isp[i] * np.log((sum(wet[i:])+payload) / (dry[i]+sum(wet[i+1:])+payload))
  13.   return dV * 9.8
  14.  
  15. # Payload calculator
  16. def getPayload(isp, wet, dry, dV2leo):
  17.   dV = getdV(isp, wet, dry, 0)  
  18.   if dV < dV2leo:
  19.     # oof
  20.     return -1
  21.   else:
  22.     # Payload mass
  23.     payload = 0
  24.     # Iteration variables
  25.     i = 1
  26.     lastSign = 1
  27.     # Iterate until the dV equation is solved
  28.     while np.abs(dV-dV2leo) > 1:
  29.       # This helps us close in on a solution
  30.       sign = np.sign(dV-dV2leo)
  31.       if sign != lastSign: i *= 2
  32.       # Change payload mass
  33.       payload += sign*100/i
  34.       # Recalculate
  35.       dV = getdV(isp, wet, dry, payload)
  36.       # Iterate
  37.       lastSign = sign
  38.   # Solution!
  39.   return payload
  40.  
  41. # Run the thing
  42. while True:
  43.   try:
  44.     # Set up parameter lists
  45.     isp = []
  46.     wet = []
  47.     dry = []  
  48.     # Get stages
  49.     moreStages = True
  50.     stage = 1
  51.     while True:
  52.       # Get first-stage boosters
  53.       if stage == 1:
  54.         print("Are there boosters? (If so, they must have the same engines as the first stage for this calculator to work) [Y/n]: ")
  55.         if input() != 'n':
  56.           print("Input first stage/booster engine isp (seconds):")
  57.           ISP = float(input())
  58.           print("Input engine fuel consumption rate (tons/sec):")
  59.           rate = float(input())
  60.           print("Input number of first stage engines:")
  61.           enginesFirst = float(input())
  62.           print("Input first stage total mass (tons):")
  63.           wetFirst = float(input())
  64.           print("Input first stage fuel mass (tons):")
  65.           fuelFirst = float(input())
  66.           dryFirst = wetFirst - fuelFirst
  67.           print("Input number of booster engines:")
  68.           enginesBoosters = float(input())
  69.           print("Input booster total mass (tons):")
  70.           wetBoosters = float(input())
  71.           print("Input booster fuel mass (tons):")
  72.           dryBoosters = wetBoosters - float(input())
  73.           # Calculate how long it takes for boosters to burn dry
  74.           t = (wetBoosters - dryBoosters)/rate/enginesBoosters
  75.           firstFuelConsumedWithBoosters = rate*t*enginesFirst
  76.           if firstFuelConsumedWithBoosters > fuelFirst:
  77.             raise Exception("Your boosters finish after your first stage does. This calculator cannot handle that lift vehicle architecture.")
  78.           # Calculate the booster+first stage "stage"
  79.           isp.append(ISP)
  80.           wet.append(wetBoosters + firstFuelConsumedWithBoosters)
  81.           dry.append(dryBoosters)
  82.           # Caculate the remainder of the first stage "stage"
  83.           isp.append(ISP)
  84.           wet.append(wetFirst - firstFuelConsumedWithBoosters)
  85.           dry.append(dryFirst)
  86.           # More stages?
  87.           print("Are there more stages? [y/N]")
  88.           if input() == 'y':
  89.             stage += 1
  90.           else:
  91.             break
  92.       # Get stage parameters
  93.       print(f"Input stage {stage} engine isp (seconds): ")
  94.       isp.append(float(input()))  
  95.       print(f"Input stage {stage} total mass (tons): ")
  96.       wet.append(float(input()))  
  97.       print(f"Input stage {stage} fuel mass (tons): ")
  98.       dry.append(wet[-1] - float(input()))  
  99.       # More stages?
  100.       print("Are there more stages? [y/N]")
  101.       if input() == 'y':
  102.         stage += 1
  103.       else:
  104.         break
  105.     # Okay, let's get a move on
  106.     print("Without payload, you have", str(int(getdV(isp, wet, dry, 0))), "m/s of delta V.")
  107.     # Will we go to space?
  108.     payload = getPayload(isp, wet, dry, 2300.0)
  109.     if payload == -1:
  110.       # oof
  111.       print("You will not go to space today. :(")
  112.     else:
  113.       # yay
  114.       print("With a very high t/w ratio, perfect piloting and no drag, you can lift", str(int(payload)), "tons to LEO.\nDon't abuse the drag bug!")
  115.       # What would a realistic payload be?
  116.       realistic = getPayload(isp, wet, dry, 3000.0)
  117.       if realistic == -1:
  118.         print("Realistically, you'll probably have a hard time getting anything to orbit.")
  119.       else:
  120.         print("Realistically, you'll probably be able to get", str(int(realistic)), "tons to orbit.")
  121.     # Move it along now
  122.     print("\nPress Enter to continue.")
  123.     input()
  124.  
  125.   except ValueError:
  126.     print("Please give me numbers.")
  127.  
  128.   except Exception as error:
  129.     print(error)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement