I was adapting mnv_time for my own purposes and noticed a few things about it while making it fit my code style -- so here's a tiny bit of unsolicited code review
-
The calculation of average Isp (ens_isp) is incorrect, as it is not weighted to factor in the thrust of the individual engines. The KSP wiki page elaborates on this, including an example of a correct average.
-
The calculation of g is incorrect; it's reflecting g at sea level rather than at ship's current altitude. (Additionally, you can use ship:body rather than ship:orbit:body, and also body rather than ship:body). It should probably read:
body:mu / (body:radius+ship:altitude)^2
or
body:mu / body:position:mag^2 // only correct if ship is the active vessel
EDIT: This is all wrong, because you actually need the altitude where the maneuver node is being executed at, not the ship's current altitude.
-
Multiplying f and m by 1000 is unnecessary; the two terms cancel.
-
You can use ship:availablethrust rather than adding the available thrust from each engine.
-
You can use skip copying engines to a second list and do your processing for average ISP in the first for loop using something like this.
FUNCTION Ship_AverageISP {
// Returns current average ISP.
PARAMETER ship IS ship.
PARAMETER activeOnly IS TRUE.
PARAMETER includeFlameOut IS FALSE.
LOCAL engines.
IF activeonly { SET engines TO Ship_ActiveEngines(ship, includeFlameOut). }
ELSE { SET engines TO Ship_Engines(ship). }
LOCAL n IS 0. // Numerator
LOCAL d IS 0. // Denominator
FOR engine IN engines {
// might want a check against 0 Isp being reported.
SET n TO n+engine:availablethrust.
SET d TO d+(engine:availablethrust / engine:isp).
}
RETURN n/d.
}
I was adapting mnv_time for my own purposes and noticed a few things about it while making it fit my code style -- so here's a tiny bit of unsolicited code review
The calculation of average Isp (
ens_isp) is incorrect, as it is not weighted to factor in the thrust of the individual engines. The KSP wiki page elaborates on this, including an example of a correct average.The calculation of
gis incorrect; it's reflecting g at sea level rather than at ship's current altitude. (Additionally, you can useship:bodyrather thanship:orbit:body, and alsobodyrather thanship:body). It should probably read:body:mu / (body:radius+ship:altitude)^2or
body:mu / body:position:mag^2 // only correct if ship is the active vesselEDIT: This is all wrong, because you actually need the altitude where the maneuver node is being executed at, not the ship's current altitude.
Multiplying
fandmby 1000 is unnecessary; the two terms cancel.You can use ship:availablethrust rather than adding the available thrust from each engine.
You can use skip copying engines to a second list and do your processing for average ISP in the first for loop using something like this.