Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Documentation for this module may be created at Module:Rocketry/doc

local Gas = require("Module:Gas")
local Combustion = require("Module:Combustion")
local CombustionData = require("Module:Combustion/data")
local GasData = mw.loadData("Module:Gas/data")

local p = {}

function p.GetCombustionTemperature(fuel, oxidizer, reactiondata, combustionRatio, fuelTemperature, oxidizerTemperature)

local EnthalpyMultiplier = 1
fuelTemperature = fuelTemperature or 100
oxidizerTemperature = oxidizerTemperature or 100
combustionRatio = combustionRatio or 0.96

if not outputs then
    if not oxidizer then
        reactiondata = CombustionData[fuel]
    else
        reactiondata = CombustionData[fuel][oxidizer]
        EnthalpyMultiplier = Gas.GetGas(oxidizer).EnthalpyMultiplier
    end
end

local FuelEnthalpy = Gas.GetGas(fuel).Enthalpy
local FuelHeatEnergy = (Gas.GetGas(fuel).SpecificHeat*fuelTemperature)*reactiondata.FuelAmount
local OxidizerHeatEnergy = (Gas.GetGas(oxidizer).SpecificHeat*oxidizerTemperature)*reactiondata.OxidizerAmount
local CombustionEnergy = FuelEnthalpy*reactiondata.FuelAmount*combustionRatio*EnthalpyMultiplier
local TotalEnergy = FuelHeatEnergy + OxidizerHeatEnergy + CombustionEnergy

--add uncombusted contents to reaction outputs

  --We need a copy of the table as writing to the original reactiondata table contaminates it.
    local newreactiondata={}
    for k,v in pairs(reactiondata) do
       newreactiondata[k] = v
       if type(v) == "table" then
           newreactiondata[k] = {}
           for k2,v2 in pairs(v) do
              --it's okay to pass the gas table as a reference as we're only reading it.
              newreactiondata[k][k2]=v2
           end
        end
    end

if combustionRatio < 1 then
    table.insert(newreactiondata.Outputs,
        {
            Gas = Gas.GetGas(fuel),
            Amount = newreactiondata.FuelAmount*(1-combustionRatio)
        }
    )
    table.insert(newreactiondata.Outputs,
        {
            Gas = Gas.GetGas(oxidizer),
            Amount = newreactiondata.OxidizerAmount*(1-combustionRatio)
        }
    )
end

--calculate Heat Capacity of reaction output
local OutputHeatCapacity = 0
for k,v in pairs(newreactiondata.Outputs) do
    OutputHeatCapacity = OutputHeatCapacity + (v.Gas.SpecificHeat * v.Amount)
end

return TotalEnergy / OutputHeatCapacity, newreactiondata --i'll want that later on for exhaust velocity because of heat capacity ratio.

end

function p.GetExhaustVelocity(engineEfficiency, combustiontemperature, heatcapacityratio)
    if not heatcapacityratio then heatcapacityratio = GasData.TriatomicHeatCapacityRatio end
    if type(heatcapacityratio) == "table" and type(heatcapacityratio.Outputs) == "table" then
        --calculate heat capacity ratio by averaging out the heat capacity ratio of exhaust composition
        local TotalMoles = 0
        local TotalMolarHeatCapacityRatio = 0
        for k,v in pairs(heatcapacityratio.Outputs) do
            TotalMoles = TotalMoles + v.Amount
            TotalMolarHeatCapacityRatio = TotalMolarHeatCapacityRatio + (v.Amount * v.Gas.HeatCapacityRatio)
        end
        heatcapacityratio = TotalMolarHeatCapacityRatio/TotalMoles
    end
    if type(heatcapacityratio) == "string" then heatcapacityratio = tonumber(heatcapacityratio) end
    if type(heatcapacityratio) ~= number then
    mw.log.warn("WARN: UNABLE TO PARSE HEAT CAPACITY RATIO. ASSUMING TriatomicHeatCapacityRatio")
    heatcapacityratio = GasData.TriatomicHeatCapacityRatio
    end

    return 25*engineEfficiency*((8.3144*heatcapacityratio*combustiontemperature)^0.5)
    
end
function p.GetThrust(ExhaustVelocity, MassFlowRate)
    if type(MassFlowRate) == "table" and type(MassFlowRate.Outputs) == "table" then
        --calculate the molar mass of exhaust composition
        local TotalMoles = 0
        for k,v in pairs(heatcapacityratio.Outputs) do
            TotalMoles = TotalMoles + v.Amount
        end
        MassFlowRate = TotalMoles
    end
    return 2*ExhaustVelocity*MassFlowRate --2 ticks/second; MassFlowRate is given in mols/tick.
end

return p