fixed burn time calculations for multi stage burns

This commit is contained in:
2026-04-19 21:52:07 -04:00
parent 9a4ae5e472
commit 7431ab7946
13 changed files with 221 additions and 155 deletions
+45 -52
View File
@@ -16,8 +16,8 @@ declare function GetDragDir{
}
declare function AnyEngineActive{
list engines in allEngines.
for eng in allEngines{
list engines in egs.
for eng in egs{
if eng:ignition {
return true.
}
@@ -26,11 +26,11 @@ declare function AnyEngineActive{
}
declare function GetIsp{
LIST ENGINES IN allEngines.
LIST ENGINES IN egs.
declare local totalThrust is ship:maxThrust.
local sum is 0.
local weights is 0.
for eng in allEngines{
for eng in egs{
if eng:IGNITION and not eng:flameout{
local w is eng:AVAILABLETHRUST / totalThrust.
local ispW is eng:isp * w.
@@ -42,15 +42,18 @@ declare function GetIsp{
}
declare function GetIspForStage{
parameter engineState.
parameter engineStage.
parameter pressure is 0.
LIST ENGINES IN allEngines.
local totalThrust is GetThrustOfStage(engineState).
LIST ENGINES IN egs.
local totalThrust is GetThrustOfStage(engineStage, pressure).
if totalThrust = 0{
return 0.
}
local sum is 0.
local weights is 0.
for eng in allEngines{
if eng:stage = engineState {
local w is eng:POSSIBLETHRUST / totalThrust.
for eng in egs{
if eng:stage = engineStage {
local w is eng:POSSIBLETHRUSTAT(pressure) / totalThrust.
local ispW is eng:ispat(pressure) * w.
set sum to sum + ispW.
set weights to weights + w.
@@ -60,9 +63,9 @@ declare function GetIspForStage{
}
declare function GetMaxMassFlow{
LIST ENGINES IN allEngines.
LIST ENGINES IN egs.
local sum is 0.
for eng in allEngines{
for eng in egs{
if eng:IGNITION and not eng:flameout{
set sum to sum + eng:maxmassflow.
}
@@ -70,6 +73,18 @@ declare function GetMaxMassFlow{
return sum.
}
declare function GetMaxMassFlowForStage{
parameter engineStage.
LIST ENGINES IN egs.
local sum is 0.
for eng in egs{
if eng:stage = engineStage {
set sum to sum + eng:maxmassflow.
}
}
return sum.
}
@@ -86,9 +101,12 @@ declare function CalculateBurnDuration
parameter initialMass.
parameter massFlow.
if burnIsp = 0 {
return 0.
}
set dv to abs(dv).
local exp is -dv / (burnIsp * constant:g0).
local massRatio is ApproximateExp(-exp).
local massRatio is constant:e ^ (-exp).
local finalMass is initialMass / massRatio.
local fuelUsed is initialMass - finalMass.
@@ -151,67 +169,43 @@ declare function CalculateTimeToImpact{
function CalculateMultiStageBurnDuration{
parameter dv.
if ship:deltav:current >= dv {
print "one stage" at (0, 5).
return GetBurnTime(dv, ship:maxthrust, ship:mass).
if ship:STAGEDELTAV(ship:stagenum):current >= dv {
return CalculateBurnDuration(dv, GetIsp(), ship:mass, GetMaxMassFlow()).
}else{
print "multi stage" at (0, 5).
local burnTime is 0.
local remDv is dv.
from {local s is ship:stagenum. } until s = 0 step { set s to s-1.} do {
from {local s is ship:stagenum. } until s = -1 step { set s to s-1.} do {
if remDv <= 0 {
return burnTime.
}
local cdv is ship:stagedeltav(s):current.
local cmass is GetMassOfStage(s).
local cthrust is GetThrustOfStage(s).
local cflow is GetMaxMassFlowForStage(s).
local cisp is GetIspForStage(s).
local sdv is 0.
print cdv.
if cdv >= remDv {
set burnTime to burnTime + GetBurnTime(remDv, cmass, cthrust, cisp).
set sdv to remDv.
}else{
set burnTime to burnTime + GetBurnTime(cdv, cmass, cthrust, cisp).
set sdv to cdv.
}
set burnTime to burnTime + CalculateBurnDuration(sdv, cisp, cmass, cflow).
set remDv to remDv - cdv.
}
return burnTime.
}
}
// Burn time from rocket equation
function GetBurnTime {
parameter deltaV.
parameter stagethrust is 0.
parameter stagemass is 0.
parameter isp is 0.
if deltaV:typename() = "Vector" {
set deltaV to deltaV:mag.
}
if isp = 0 {
set isp to GetIsp().
}
if stagethrust = 0 {
set stagethrust to ship:thrust.
}
if stagemass = 0 {
set stagemass to ship:mass.
}
local burnTime is -1.
if stagethrust <> 0 {
set burnTime to stagemass * (1 - CONSTANT:E ^ (-deltaV / isp)) / (stagethrust / isp).
}
return burnTime.
}
function GetThrustOfStage {
parameter st.
parameter pressure is 0.
local totalThrust is 0.
LIST ENGINES IN allEngines.
for eng in allEngines{
LIST ENGINES IN egs.
for eng in egs{
if eng:stage = st {
set totalThrust to totalThrust + eng:POSSIBLETHRUST.
set totalThrust to totalThrust + eng:POSSIBLETHRUSTAT(pressure).
}
}
return totalThrust.
@@ -219,12 +213,11 @@ function GetThrustOfStage {
function GetMassOfStage {
parameter st.
local total is 0.
LIST PARTS IN allP.
for p in allP {
if p:stage <= st {
if p:stage <= st and p:DECOUPLEDIN < st {
set total to total + p:mass.
}
}