Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-04-19 - Optimization of cold start enrichment hot path
**Learning:** In high-frequency loops (like `updateTorque` running at 2000Hz), defining functions and tables locally causes excessive garbage collection pressure due to constant allocations.
**Action:** Move static data structures and helper functions to the module level to avoid re-allocation in the hot path.
90 changes: 44 additions & 46 deletions combustionEngine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,49 @@ local torqueToPower = 0.0001404345295653085
local psToWatt = 735.499
local hydrolockThreshold = 1.9

-- Cold start enrichment using temperature-based lookup table
local enrichmentMap = {
[-30] = 3.0,
[-20] = 2.6,
[-10] = 2.2,
[0] = 1.8,
[10] = 1.5,
[20] = 1.3,
[30] = 1.15,
[40] = 1.05,
[50] = 1.02,
[60] = 1.0,
[70] = 1.0
}

local function getColdEnrichment(tempC)
-- Find the two closest temperature points
local lowerTemp = -20
local upperTemp = 80
local lowerEnrich = 3.0
local upperEnrich = 0.85

-- Find the two closest temperature points in the map
for temp, enrich in pairs(enrichmentMap) do
if temp <= tempC and temp > lowerTemp then
lowerTemp = temp
lowerEnrich = enrich
end
if temp >= tempC and temp < upperTemp then
upperTemp = temp
upperEnrich = enrich
end
end

-- Linear interpolation between the two closest points
if lowerTemp == upperTemp then
return lowerEnrich
end

local t = (tempC - lowerTemp) / (upperTemp - lowerTemp)
return lowerEnrich + (upperEnrich - lowerEnrich) * t
end

local function getTorqueData(device)
local curves = {}
local curveCounter = 1
Expand Down Expand Up @@ -1241,52 +1284,7 @@ local function updateTorque(device, dt)

-- Temperature effect on starter torque (reduces torque in cold conditions)
local tempEffectOnStarter = 1.0 - math.max(0, math.min(0.7, (0 - engineTempC) / 30))

-- Cold start enrichment using temperature-based lookup table (reduced values)
local function getColdEnrichment(tempC)
-- Temperature in Celsius to enrichment factor mapping
-- [tempC] = enrichmentMultiplier
local enrichmentMap = {
[-30] = 3.0, -- Reduced from 4.0
[-20] = 2.6, -- Reduced from 3.5
[-10] = 2.2, -- Reduced from 3.0
[0] = 1.8, -- Reduced from 2.5
[10] = 1.5, -- Reduced from 2.0
[20] = 1.3, -- Reduced from 1.5
[30] = 1.15, -- Reduced from 1.25
[40] = 1.05, -- Reduced from 1.1
[50] = 1.02, -- Reduced from 1.05
[60] = 1.0,
[70] = 1.0
}

-- Find the two closest temperature points
local lowerTemp = -20
local upperTemp = 80
local lowerEnrich = 3.0
local upperEnrich = 0.85

-- Find the two closest temperature points in the map
for temp, _ in pairs(enrichmentMap) do
if temp <= tempC and temp > lowerTemp then
lowerTemp = temp
lowerEnrich = enrichmentMap[temp]
end
if temp >= tempC and temp < upperTemp then
upperTemp = temp
upperEnrich = enrichmentMap[temp]
end
end

-- Linear interpolation between the two closest points
if lowerTemp == upperTemp then
return lowerEnrich
end

local t = (tempC - lowerTemp) / (upperTemp - lowerTemp)
return lowerEnrich + (upperEnrich - lowerEnrich) * t
end


-- Calculate cold start enrichment based on engine temperature
local coldStartEnrichment = getColdEnrichment(engineTempC)

Expand Down