This commit is contained in:
Navid Sassan 2025-12-09 23:18:32 +01:00
parent e38fe600f7
commit f4ce6980aa

View File

@ -54,8 +54,9 @@ local CONFIG = {
max_temperature = 8000, -- Emergency shutdown above this (Kelvin)
critical_temperature = 7500, -- Start reducing output at this temp
-- Saturation threshold for fuel conversion
-- Saturation control for optimal fuel conversion
min_saturation = 0.20, -- Below this, reactor produces less power
target_saturation = 0.45, -- Target saturation for optimal output (lower = more power)
-- Update intervals (in seconds)
update_interval = 0.1, -- Main loop speed (faster = more responsive)
@ -402,22 +403,47 @@ local function calculateOutputRate()
result = math.floor(generationRate * 0.8)
reason = state .. " - 80% of generation"
else
-- Normal operation
-- Normal operation with saturation targeting
local tempFactor = 1.0
local tempCritical = false
if temp > CONFIG.critical_temperature then
local overTemp = temp - CONFIG.critical_temperature
local tempRange = CONFIG.max_temperature - CONFIG.critical_temperature
tempFactor = 1.0 - (overTemp / tempRange) * 0.5
reason = "temp " .. formatTemp(temp) .. " > critical, factor=" .. string.format("%.2f", tempFactor)
tempCritical = true
end
local outputRate = generationRate * tempFactor
-- Saturation-based output adjustment
-- Target saturation for optimal fuel conversion (lower sat = more power)
local targetSat = CONFIG.target_saturation
local satError = saturation - targetSat -- positive = too high, negative = too low
if saturation > 0.8 then
outputRate = generationRate * 1.1 * tempFactor
if reason == "" then reason = "high saturation (110%)" end
elseif reason == "" then
reason = "normal operation"
-- Scale factor: adjust output by up to 50% based on saturation error
local satFactor = 1.0 + satError
satFactor = math.max(0.5, math.min(1.5, satFactor)) -- Clamp to 50%-150%
-- IMPORTANT: If temperature is critical, don't increase output to drain saturation
-- Temperature safety takes priority over saturation optimization
if tempCritical and satFactor > 1.0 then
satFactor = 1.0 -- Cap at 100%, don't try to drain saturation when hot
end
local outputRate = generationRate * tempFactor * satFactor
-- Build reason string
local satPct = string.format("%.0f%%", saturation * 100)
local targetPct = string.format("%.0f%%", targetSat * 100)
if tempCritical then
reason = "TEMP CRITICAL " .. formatTemp(temp) .. ", factor=" .. string.format("%.2f", tempFactor)
if satFactor < 1.0 then
reason = reason .. ", sat " .. satPct .. " low"
end
elseif satError > 0.05 then
reason = "sat " .. satPct .. " > " .. targetPct .. " target, +" .. string.format("%.0f%%", (satFactor - 1) * 100) .. " output"
elseif satError < -0.05 then
reason = "sat " .. satPct .. " < " .. targetPct .. " target, " .. string.format("%.0f%%", (satFactor - 1) * 100) .. " output"
else
reason = "sat " .. satPct .. " at target"
end
result = math.max(0, math.floor(outputRate))