(() => { const clamp = (min, max, value) => Math.min(max, Math.max(min, value)); const defaults = Object.freeze({ maxHotFlow: 0.225, coldFlow: 0.0147, coldTemp: 20, hotTemp: 100, hotValveExponent: 6, sensorTimeConstant: 2, maxFlow: 30, saturation: 60 }); const recipes = Object.freeze({ B1: Object.freeze({ targetTemp: 92, tempTolerance: 1, tempErrorWeight: 12, preMin: 6, preMax: 9, timeErrorWeight: 10, essenceCapacity: 10, referenceVolume: 300, minEssenceRatio: 1, essencePerMl: 1 / 30 }), B2: Object.freeze({ targetTemp: 93, tempTolerance: 1, tempErrorWeight: 12, preMin: 3, preMax: 5, timeErrorWeight: 10, essenceCapacity: 12, referenceVolume: 360, minEssenceRatio: 1, essencePerMl: 1 / 30 }) }); function hotFlow(machine, valve) { return machine.maxHotFlow * clamp(0, 1, valve) ** machine.hotValveExponent; } function temperature(machine, valve) { const qh = hotFlow(machine, valve); return (machine.hotTemp * qh + machine.coldTemp * machine.coldFlow) / (qh + machine.coldFlow); } function probeStep(machine, current, valve, dt) { if (dt <= 0) return current; const actual = temperature(machine, valve); return actual + (current - actual) * Math.exp(-dt / machine.sensorTimeConstant); } function temperatureSensitivity(machine, valve) { const u = clamp(0, 1, valve); const n = machine.hotValveExponent; const ratio = machine.maxHotFlow / machine.coldFlow; return (machine.hotTemp - machine.coldTemp) * ratio * n * u ** (n - 1) / (1 + ratio * u ** n) ** 2; } function peakValve(machine) { const n = machine.hotValveExponent; const ratio = machine.maxHotFlow / machine.coldFlow; return clamp(0, 1, ((n - 1) / ((n + 1) * ratio)) ** (1 / n)); } function flow(machine, lever) { return machine.maxFlow * clamp(0, 1, lever); } function idealValve(machine, target) { if (target <= machine.coldTemp) return 0; if (target >= machine.hotTemp) return null; const requiredRatio = (target - machine.coldTemp) / (machine.hotTemp - target); const valve = (machine.coldFlow * requiredRatio / machine.maxHotFlow) ** (1 / machine.hotValveExponent); return valve <= 1 ? valve : null; } function instantTemperatureScore(value, recipe) { return clamp(0, 100, 100 - recipe.tempErrorWeight * Math.abs(value - recipe.targetTemp)); } function temperatureScore(samples, recipe) { if (!samples.length) return null; const scores = samples.map(sample => Number.isFinite(sample.score) ? sample.score : instantTemperatureScore(sample.temperature, recipe)); const average = scores.reduce((sum, score) => sum + score, 0) / scores.length; const worst = Math.min(...scores); return { average, worst, score: 0.8 * average + 0.2 * worst }; } function timingDeviation(time, min, max) { if (time < min) return time - min; if (time > max) return time - max; return 0; } function timingScore(time, min, max, weight) { return clamp(0, 100, 100 - weight * Math.abs(timingDeviation(time, min, max))); } function essenceRate(machine, recipe, preTime, hotValve) { const temperatureFactor = clamp(0.5, 1.25, 1 + 0.04 * (temperature(machine, hotValve) - recipe.targetTemp)); const preFactor = preTime === null ? 1 : clamp(0.6, 1, 1 - 0.08 * Math.abs(timingDeviation(preTime, recipe.preMin, recipe.preMax))); return recipe.essencePerMl * temperatureFactor * preFactor; } class BrewSession { constructor(machine, recipe, initialSensorTemperature = null) { this.machine = { ...machine }; this.recipe = { ...recipe }; this.phase = "empty"; this.elapsed = 0; this.contactTime = null; this.saturationTime = null; this.endTime = null; this.waterIn = 0; this.preTime = null; this.extractionTime = 0; this.essenceCollected = 0; this.volume = 0; this.samples = []; this.sensorSamples = []; this.sensorTemperature = initialSensorTemperature ?? temperature(this.machine, 0); this.currentLever = 0; this.currentHotValve = 0; this.currentFlow = 0; this.finalScore = null; } load() { if (this.phase !== "empty") return; this.phase = "ready"; } sample(hotValve) { const value = temperature(this.machine, hotValve); this.samples.push({ time: this.elapsed, phase: this.phase, hotValve: clamp(0, 1, hotValve), hotFlow: hotFlow(this.machine, hotValve), temperature: value, score: instantTemperatureScore(value, this.recipe), lever: this.currentLever, flow: this.currentFlow, essenceCollected: this.essenceCollected, beverageVolume: this.volume }); this.sensorSamples.push({ time: this.elapsed, temperature: this.sensorTemperature }); } essenceRatio() { return this.recipe.essenceCapacity > 0 ? clamp(0, 1, this.essenceCollected / this.recipe.essenceCapacity) : 0; } concentration() { return this.volume > 0 ? this.essenceRatio() * this.recipe.referenceVolume / this.volume : 0; } concentrationScore() { const concentration = this.concentration(); return concentration > 0 ? Math.min(concentration, 1 / concentration) : 0; } collectionGate() { return Math.min(1, this.essenceRatio() / this.recipe.minEssenceRatio); } extractionScore() { return 100 * this.collectionGate() * this.concentrationScore(); } collectEssence(q, hotValve, dt) { if (q <= 0 || dt <= 0) return; this.essenceCollected = Math.min( this.recipe.essenceCapacity, this.essenceCollected + q * essenceRate(this.machine, this.recipe, this.preTime, hotValve) * dt ); } step(lever, hotValve, dt) { if (dt <= 0 || !["ready", "preinfusion", "extraction"].includes(this.phase)) return; this.currentLever = clamp(0, 1, lever); this.currentHotValve = clamp(0, 1, hotValve); this.currentFlow = flow(this.machine, this.currentLever); const q = this.currentFlow; if (this.phase === "ready") { if (q === 0) { this.sensorTemperature = probeStep(this.machine, this.sensorTemperature, hotValve, dt); return; } this.phase = "preinfusion"; this.contactTime = 0; this.sample(hotValve); } this.sensorTemperature = probeStep(this.machine, this.sensorTemperature, hotValve, dt); if (this.phase === "preinfusion") { const untilSaturated = q > 0 ? (this.machine.saturation - this.waterIn) / q : Infinity; const step = Math.min(dt, Math.max(0, untilSaturated)); if (q > 0) this.waterIn = Math.min(this.machine.saturation, this.waterIn + q * step); if (this.waterIn >= this.machine.saturation) { this.preTime = this.elapsed + step; this.saturationTime = this.preTime; this.phase = "extraction"; const remaining = dt - step; if (q > 0 && remaining > 0) { this.extractionTime += remaining; this.volume += q * remaining; this.collectEssence(q, hotValve, remaining); } } this.elapsed += dt; this.sample(hotValve); return; } this.elapsed += dt; if (q > 0) { this.extractionTime += dt; this.volume += q * dt; this.collectEssence(q, hotValve, dt); } this.sample(hotValve); } removeHandle(lever = this.currentLever) { this.currentLever = clamp(0, 1, lever); this.currentFlow = flow(this.machine, this.currentLever); const q = this.currentFlow; if (["ready", "preinfusion"].includes(this.phase)) { this.phase = "aborted"; this.endTime = this.elapsed; return this.phase; } if (this.phase !== "extraction") return this.phase; if (q > 0) return "running"; this.phase = "complete"; this.endTime = this.elapsed; this.sample(this.currentHotValve); this.finalScore = { ratio: this.essenceRatio(), concentration: this.concentration(), concentrationScore: this.concentrationScore(), gate: this.collectionGate(), score: this.extractionScore() }; return this.phase; } scores() { const temp = temperatureScore(this.samples, this.recipe); const extraction = this.finalScore || { ratio: this.essenceRatio(), concentration: this.concentration(), concentrationScore: this.concentrationScore(), gate: this.collectionGate(), score: this.extractionScore() }; return { temperature: temp ? temp.score : null, averageTemperature: temp ? temp.average : null, worstTemperature: temp ? temp.worst : null, preinfusion: this.preTime === null ? null : timingScore(this.preTime, this.recipe.preMin, this.recipe.preMax, this.recipe.timeErrorWeight), extraction: extraction.score, essenceRatio: extraction.ratio, concentration: extraction.concentration, concentrationScore: extraction.concentrationScore, collectionGate: extraction.gate, essenceCollected: this.essenceCollected, referenceVolume: this.recipe.referenceVolume }; } } window.CoffeeMachine = { clamp, defaults, recipes, hotFlow, temperature, probeStep, temperatureSensitivity, peakValve, flow, idealValve, instantTemperatureScore, temperatureScore, timingDeviation, timingScore, essenceRate, BrewSession }; })();