Add a simulation-based Sud forecast, computed server-side

New components/sud_forecast.py: SudForecastEstimator simulates a whole
schedule with the same kind of plant/controller (and the same
configured params - ambient, plant params, pid_type, TempCtrl gains,
heater max power) the server uses for real, by driving a throwaway
Sud/Pot/controller trio through it exactly as tasks/sud.py's SudTask
would. It's pure CPU-bound iteration (no real time/IO), so a multi-
hour brew simulates in well under a second.

tasks/sud.py: SudTask now takes an optional forecast_estimator and
sends its result ({'Forecast': {'T': ..., 'Theta': ...}}) on the Sud
channel after every successful Save/Load, run in a worker thread via
run_in_executor so the ~100-500ms simulation doesn't stall the other
tasks. server/brewpi.py constructs one with the real server's config
and wires it in; AmbientTemp changes update it too.

client/brewpi_gui.py: the quick naive show_schedule() estimate (drawn
immediately on Load, before the server's simulation finishes) is now
superseded by show_precomputed_course() once the Forecast message
arrives - only while not actively running, so it doesn't fight the
dynamic re-anchored view.

Verified: matches a standalone run of the estimator (177 min for
sude/sud_0010.json) and replaces the old naive 164 min estimate live
within a couple seconds of loading.
This commit is contained in:
2026-06-21 16:46:52 +02:00
parent 1034765e31
commit 567ab80c9c
4 changed files with 156 additions and 3 deletions
+27
View File
@@ -163,9 +163,22 @@ class SudForecastPlot(FigureCanvasQTAgg):
return t, theta
def show_schedule(self, schedule, start_theta, name):
"""Quick, approximate preview shown immediately when a schedule is
loaded - assumes every ramp instantly achieves/sustains its
declared rate, which real plants/controllers don't. Superseded by
show_precomputed_course() shortly after, once the server's
simulation-based forecast (components/sud_forecast.py) arrives."""
t, theta = self._estimate_course(schedule, start_theta)
t_min = [seconds / 60.0 for seconds in t]
self._show_static_course(t_min, theta, name)
def show_precomputed_course(self, t_min, theta, name):
"""Like show_schedule(), but (t_min, theta) were already computed
by the server simulating the schedule with its real plant/
controller - see Window.on_sud_forecast_received()."""
self._show_static_course(t_min, theta, name)
def _show_static_course(self, t_min, theta, name):
self.line.set_data(t_min, theta)
self.line_projected.set_data([], [])
self.ax.relim()
@@ -820,8 +833,22 @@ class Window(QtWidgets.QMainWindow, Ui_MainWindow):
elif "HoldRemaining" in key:
self.sud_hold_remaining = msg['HoldRemaining']
self.update_status_step_label()
elif "Forecast" in key:
self.on_sud_forecast_received(msg['Forecast'])
self.update_sud_actions()
def on_sud_forecast_received(self, forecast):
# The server computes this asynchronously (it simulates the whole
# schedule) and it can arrive slightly after the schedule itself -
# only apply it if we're still looking at the static "not running
# yet" preview show_schedule() drew; a run already in progress (or
# one that's since finished) owns the plot via show_dynamic()/its
# frozen last frame instead.
if self.sud_empty or self.sud_start_time is not None:
return
t_min = [seconds / 60.0 for seconds in forecast['T']]
self.forecast_plot.show_precomputed_course(t_min, forecast['Theta'], self.sud_name)
def update_status_step_label(self):
if not self.sud_step_descr:
self.statusBar().clearMessage()