Advertisement
Guest User

stageAnalysis.ks

a guest
Aug 4th, 2015
1,805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. lock throttle to 0.
  2. wait 0.05.
  3.  
  4. // the ultimate product of the program
  5. // will contain lists of data about each stage
  6. set stages to list().
  7.  
  8. list engines in engineList.
  9.  
  10. local g is 9.80665.
  11.  
  12. // have to activate engines to get their thrusts (for now)
  13. local activeEngines is list().
  14. for eng in engineList
  15. if eng:ignition = false
  16. eng:activate.
  17. else
  18. activeEngines:add(eng).
  19.  
  20. // tagging all decouplers as decoupler by looking at part modules
  21. for part in ship:parts
  22. for module in part:modules
  23. if part:getModule(module):name = "ModuleDecouple"
  24. or part:getModule(module):name = "ModuleAnchoredDecoupler"
  25. set part:tag to "decoupler".
  26.  
  27. // sections are groups of parts between decouplers
  28. // the roots of sections are the ship root and all decouplers
  29. local sectionRoots is list().
  30.  
  31. sectionRoots:add(ship:rootPart).
  32.  
  33. for decoupler in ship:partsTagged("decoupler")
  34. sectionRoots:add(decoupler).
  35.  
  36. // lists of (root part, mass, fuelmass, engines, and fuelflow)
  37. local sections is list().
  38.  
  39. // creates a section from the root of each section
  40. for sectionRoot in sectionRoots{
  41.  
  42. local sectionMass is 0.
  43. local sectionFuelMass is 0.
  44. local sectionEngineList is list().
  45. local fuelFlow is 0.
  46.  
  47. local sectionParts is list().
  48. sectionParts:add(sectionRoot).
  49.  
  50. // add all children down part tree from the section root to
  51. // list of section parts unless they are a decoupler or launch clamp
  52. local i is 0.
  53. until i = sectionParts:length{
  54. if sectionParts[i]:children:empty = false
  55. for child in sectionParts[i]:children
  56. if child:tag = "decoupler" = false and child:name = "LaunchClamp1" = false
  57. sectionParts:add(child).
  58. set i to i + 1.
  59. }
  60.  
  61. for part in sectionParts{
  62.  
  63. set sectionMass to sectionMass + part:mass.
  64.  
  65. // avoiding adding rcs fuel to fuelmass
  66. local rcsFlag is false.
  67.  
  68. if part:resources:empty = false{
  69. for resource in part:resources
  70. if resource:name = "monopropellant"
  71. set rcsFlag to true.
  72.  
  73. if rcsFlag = false
  74. set sectionFuelMass to sectionFuelMass + part:mass - part:drymass.
  75. }
  76.  
  77. if engineList:contains(part)
  78. sectionEngineList:add(part).
  79. }
  80.  
  81. local section is list(sectionRoot,sectionMass,sectionFuelMass,sectionEngineList,fuelFlow).
  82. sections:add(section).
  83. }
  84.  
  85. local firstStageNum is 0.
  86. for eng in engineList
  87. if eng:stage > firstStageNum
  88. set firstStageNum to eng:stage.
  89.  
  90. // counting down from first (highest number) stage
  91. // to stage 0, creating stage data and
  92. // updating mass and fuelmass of the sections as it goes
  93. // essentially "simulating" staging
  94. local i is firstStageNum.
  95. until i = -1 {
  96.  
  97. // the four things really needed
  98. local stageMass is 0.
  99. local stageThrust is 0.
  100. local stageFuelFlow is 0.
  101. local stageBurnTime is 987654321. // starts high cause need to find lowest
  102.  
  103. // other stuff it may as well calculate
  104. local stageMinA is 0.
  105. local stageMaxA is 0.
  106. local stageISP is 0.
  107. local stageDeltaV is 0.
  108.  
  109. local curStage is list().
  110.  
  111. // if the section decoupler activates on this stage remove that section
  112. // except the first section root (not a decoupler)
  113. local k is sections:length - 1.
  114. until k = 0{
  115.  
  116. if sections[k][0]:stage = i
  117. sections:remove(k).
  118. set k to k - 1.
  119. }
  120.  
  121. // generating the stage mass, thrust, fuelflow, and burntime
  122. // from the sections that make up the stage
  123. for section in sections{
  124.  
  125. local sectionMass is section[1].
  126. local sectionFuelMass is section[2].
  127. // resetting fuelflow
  128. set section[4] to 0.
  129. local sectionBurnTime is 0.
  130.  
  131. set stageMass to stageMass + sectionMass.
  132.  
  133. if section[3]:empty = false{
  134. for engine in section[3]
  135. if engine:stage >= i{
  136. set stageThrust to stageThrust + engine:maxthrustat(0).
  137. set stageFuelFlow to stageFuelFlow + engine:maxthrustat(0)/engine:visp.
  138. set section[4] to section[4] + engine:maxthrustat(0)/engine:visp.
  139. }
  140. }
  141. // if it has fuelflow (active engines)
  142. if section[4] > 0{
  143. set sectionBurnTime to g * section[2] / section[4].
  144.  
  145. // if the section will stage next stage
  146. // or this if this is the last stage
  147. if section[0]: stage = i - 1 or i = 0
  148. if sectionBurnTime < stageBurnTime
  149. set stageBurnTime to sectionBurnTime.
  150. }
  151. }
  152.  
  153. // only possible if there are no active engines this stage (or god help you)
  154. if stageBurnTime = 987654321
  155. set stageBurnTime to 0.
  156.  
  157. // calculating optional goodies
  158. if stageBurnTime > 0{
  159. local stageEndMass is stageMass - stageBurnTime * stageFuelFlow / g.
  160. set stageMinA to stageThrust / stageMass.
  161. set stageMaxA to stageThrust/ stageEndMass.
  162. set stageISP to stageThrust / stageFuelFlow.
  163. set stageDeltaV to stageISP * g * ln(stageMass / stageEndMass).
  164. }
  165.  
  166. // take a deep breath
  167. set curStage to list(stageMass,stageISP,stageThrust,stageMinA,stageMaxA,stageDeltaV,stageBurnTime).
  168. stages:add(curStage).
  169.  
  170. // reduce the mass and fuel mass of sections with active engines
  171. // according to the burn time of the stage
  172. for section in sections{
  173. set section[1] to section[1]- stageBurnTime * section[4] / g.
  174. set section[2] to section[2] - stageBurnTime * section[4] / g.
  175. }
  176.  
  177. set i to i - 1.
  178. }
  179.  
  180. // remove stages with no burn time
  181. // comment out if you're curious, should look more like KERs' "show all stages" in VAB
  182. {
  183. local i is stages:length - 1.
  184. until i = -1{
  185. if stages[i][6] = 0
  186. stages:remove(i).
  187. set i to i - 1.
  188. }
  189. }
  190. // shutting engines back down
  191. for eng in engineList
  192. if activeEngines:contains(eng) = false
  193. eng:shutdown.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement