Advertisement
FaceDeer

asterringmaker.py

Mar 16th, 2016
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.65 KB | None | 0 0
  1. import random
  2. from datetime import datetime
  3.  
  4. # AsteroidsRingMaker
  5. # by JackDole
  6. # 2016.03.15 16:36:56
  7. # converted to Python and enhanced by FaceDeer
  8.  
  9. daysYear = 365.24218985
  10. AU = 149597870.691
  11. #eMass = 5.9736e24
  12.  
  13. #uncomment this line if you want repeatable results
  14. random.seed(100)
  15.  
  16. # Amount of asteroids
  17. NumberOfAster=200
  18. # Name of the asteroids - a serial number is appended
  19.  
  20.  
  21. AsterName='Vulkanoid'
  22.  
  23. RingCenter='Sol'
  24.  
  25. # Orbital radii in AU
  26. InnerRadius=0.15
  27. OuterRadius=0.18
  28.  
  29. # Radius of asteroids in KM
  30. MaxRadiusOfAster=3
  31. MinRadiusOfAster=0.1
  32.  
  33. #orbital eccentricity
  34. MaxEccentricity=0.001
  35. MinEccentricity=0
  36.  
  37. # Orbital inclination +- in degrees
  38. MaxInclination=10
  39.  
  40. #rotation periods in hours. See https://www.boulder.swri.edu/~bottke/rubble/node3.html
  41. MaxRotationPeriod = 12
  42. MinRotationPeriod = 0.1
  43.  
  44. ChanceOfMoon = 0.5
  45. ChanceOfBinary = 0.25
  46.  
  47. FileName= '{0}_{1}.sc'.format(AsterName, NumberOfAster)
  48.  
  49. class Orbit:
  50.     def print(self):
  51.         result = ['\tOrbit\n\t{']
  52.         if hasattr(self, 'epoch'):
  53.             result.append('\t\tEpoch\t\t\t{0}'.format(self.epoch))
  54.         else:
  55.             result.append('\t\tEpoch\t\t\t2457395.5')
  56.         if hasattr(self, 'semi'):
  57.             result.append('\t\tSemiMajorAxis\t{0}'.format(self.semi))
  58.         if hasattr(self, 'period'):
  59.             result.append('\t\tPeriod\t\t\t{0}'.format(self.period))
  60.         if hasattr(self, 'eccen'):
  61.             result.append('\t\tEccentricity\t{0}'.format(self.eccen))
  62.         if hasattr(self, 'incl'):
  63.             result.append('\t\tInclination\t\t{0}'.format(self.incl))
  64.         if hasattr(self, 'ascen'):
  65.             result.append('\t\tAscendingNode\t{0}'.format(self.ascen))
  66.         if hasattr(self, 'argof'):
  67.             result.append('\t\tArgOfPericen\t{0}'.format(self.argof))
  68.         if hasattr(self, 'refplane'):
  69.             result.append('\t\tRefPlane\t\t"{0}"'.format(self.refplane))
  70.         else:
  71.             result.append('\t\tRefPlane\t\t"Ecliptic"')
  72.         result.append('\t\tMeanAnomaly\t\t0.0')
  73.         result.append('\t}')
  74.         return '\n'.join(result)
  75.  
  76. class Asteroid:
  77.     def print(self):
  78.         result = ['Asteroid "{0}"\n{{\n\tParentBody\t"{1}"\n\tClass\t\t"Asteroid"'.format(self.name, self.parent)]
  79.         if hasattr(self, 'radius'):
  80.             result.append('\tRadius\t\t{0}'.format(self.radius))
  81.         if hasattr(self, 'rotation'):
  82.             result.append('\tRotationPeriod\t{0}'.format(self.rotation))
  83.         if hasattr(self, 'obliquity'):
  84.             result.append('\n\tObliquity\t\t{0}\n'.format(self.obliquity))
  85.         result.append(self.orbit.print())
  86.         result.append('}')
  87.         return '\n'.join(result)
  88.  
  89.     def getSafeMoonRadius(self):
  90.         return self.radius/AU * 1.2
  91.  
  92. class BinaryAsteroid:
  93.     def print(self):
  94.         result = ['Barycenter "{0}"\n{{\n\tParentBody\t"{1}"'.format(self.name, self.parent)]
  95.         result.append(orbit.print())
  96.         result.append('}\n')
  97.  
  98.         asteroid = Asteroid()
  99.         asteroid.name = self.name + " A"
  100.         asteroid.parent = self.name
  101.         asteroid.radius = self.radius1
  102.         asteroid.orbit = self.componentorbit
  103.         result.append(asteroid.print())
  104.  
  105.         asteroid.name = self.name + " B"
  106.         asteroid.radius = self.radius2
  107.         asteroid.orbit.argof = (asteroid.orbit.argof + 180) % 360
  108.         result.append(asteroid.print())
  109.         return '\n'.join(result)
  110.  
  111. #three different random number generators that give a floating point number between
  112. #the two parameters but with different distributions
  113.  
  114. def uniform(a, b):
  115.     return random.uniform(a, b)
  116.  
  117. # A normal distribution placing a and b at three standard deviations out from the mean
  118. # clamping the result so that there are no outliers beyond the desired range
  119. def gaussian(a, b):
  120.     result = random.gauss((a+b)/2.0, (b-a)/6.0)
  121.     return max(min(result, b), a)
  122.  
  123. randomGenerator = gaussian
  124.  
  125. def makeMoon(parentAsteroid, moonCount):
  126.     innerLimit = parentAsteroid.radius/AU * 1.1
  127.     outerLimit = innerLimit * 20
  128.  
  129.     moon = Asteroid()
  130.     moon.radius = random.uniform(parentAsteroid.radius/20, parentAsteroid.radius/4)
  131.     moon.name = parentAsteroid.name + '.{0}'.format(moonCount)
  132.     moon.parent = parentAsteroid.name
  133.  
  134.     orbit = Orbit()
  135.     orbit.parent = parentAsteroid.name
  136.     orbit.refplane = 'Equator'
  137.     orbit.semi = random.uniform(innerLimit, outerLimit)
  138.     orbit.incl = randomGenerator(-90, 90)
  139.     orbit.eccen = randomGenerator(0, 0.1)
  140.     orbit.argof = random.uniform(0,360)
  141.     orbit.ascen = random.uniform(0,360)
  142.  
  143.     moon.orbit = orbit
  144.  
  145.     return moon.print()
  146.  
  147.  
  148. def makeAsteroid(count, orbit):
  149.     asteroid = Asteroid()
  150.     asteroid.name = AsterName + ' {0}'.format(count)
  151.     asteroid.parent = RingCenter
  152.     asteroid.radius = randomGenerator(MinRadiusOfAster, MaxRadiusOfAster)
  153.     asteroid.rotation = randomGenerator(MinRotationPeriod,MaxRotationPeriod)
  154.     asteroid.obliquity = random.uniform(0,360)
  155.     asteroid.orbit = orbit
  156.  
  157.     output = [asteroid.print()]
  158.    
  159.     moonCount = 0
  160.     while random.random() > ChanceOfMoon and moonCount < 10:
  161.         moonCount = moonCount + 1
  162.         output.append(makeMoon(asteroid, moonCount))
  163.    
  164.     return '\n'.join(output)
  165.  
  166. def makeBinaryAsteroid(count, orbit):
  167.     binary = BinaryAsteroid()
  168.     binary.name = AsterName + ' {0}'.format(count)
  169.     binary.parent = RingCenter
  170.     binary.radius1 = randomGenerator(MinRadiusOfAster, MaxRadiusOfAster)
  171.     binary.radius2 = binary.radius1 * random.uniform(0.25,1)
  172.     binary.orbit = orbit
  173.  
  174.     componentOrbit = Orbit()
  175.     componentOrbit.period = random.uniform(0.1, 3) / daysYear
  176.     componentOrbit.incl = random.uniform(0,360)
  177.     componentOrbit.eccen = randomGenerator(0, 0.1)
  178.     componentOrbit.argof = random.uniform(0,360)
  179.     componentOrbit.ascen = random.uniform(0,360)
  180.     componentOrbit.refplane = 'Equator'
  181.  
  182.     binary.componentorbit = componentOrbit
  183.  
  184.     return binary.print()
  185.  
  186.  
  187. with open(FileName, 'w') as outputfile:
  188.     outputfile.write('// Asteroids made with AsteroidsRingMaker\n// {0}'.format(datetime.today()))
  189.  
  190.     for count in range(NumberOfAster):
  191.         orbit = Orbit()
  192.         orbit.semi = randomGenerator(InnerRadius, OuterRadius)
  193.         orbit.incl = randomGenerator(-MaxInclination, MaxInclination)
  194.         orbit.eccen = randomGenerator(MinEccentricity, MaxEccentricity)
  195.         orbit.argof = random.uniform(0,360)
  196.         orbit.ascen = random.uniform(0,360)
  197.         orbit.refplane = 'Ecliptic'
  198.  
  199.         outputfile.write('\n\n')
  200.  
  201.         if random.random() > ChanceOfBinary:
  202.             outputfile.write(makeAsteroid(count+1, orbit))
  203.         else:
  204.             outputfile.write(makeBinaryAsteroid(count+1, orbit))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement