From c4e8079320dc76ba6d3a49faa78c0430d5cff6ef Mon Sep 17 00:00:00 2001 From: Jeroen Jansen Date: Thu, 8 Oct 2026 09:05:41 +0200 Subject: [PATCH 1/2] Modelleer eindige waterwarmtecapaciteit bij pompstop --- .../quatt_odu_simulator_model.h | 21 ++++-- tests/quatt_odu_water_capacity_test.cpp | 71 +++++++++++++++++++ 2 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 tests/quatt_odu_water_capacity_test.cpp diff --git a/components/quatt_odu_simulator/quatt_odu_simulator_model.h b/components/quatt_odu_simulator/quatt_odu_simulator_model.h index c3d9357..35aea45 100644 --- a/components/quatt_odu_simulator/quatt_odu_simulator_model.h +++ b/components/quatt_odu_simulator/quatt_odu_simulator_model.h @@ -840,15 +840,22 @@ class QuattOduSimulatorModel { this->state_.ac_current_a = this->state_.electrical_power_w / std::max(1.0f, this->state_.ac_voltage_v * this->settings_.power_factor); - float target_out = this->state_.water_in_temperature_c; - if (this->state_.flow_lph > 1.0f && this->state_.thermal_power_w > 0.0f) { - const float delta = this->state_.thermal_power_w / ((this->state_.flow_lph / 3600.0f) * 4180.0f); + // A finite water volume prevents P/flow from diverging during a pump stop. + // Calibrate the existing response time at the default 800 L/h operating point; + // this is a simulator calibration, not a measured ODU water volume. + const float capacity_j_per_k = 4180.0f * (800.0f / 3600.0f) * this->settings_.water_response_tau_s; + if (std::isfinite(capacity_j_per_k) && capacity_j_per_k > 0.0f) { + const float conductance_w_per_k = 4180.0f * (this->state_.flow_lph / 3600.0f); const float sign = this->state_.active_mode == WorkingMode::COOLING ? -1.0f : 1.0f; - target_out += sign * delta; - if (this->state_.defrost_active()) target_out -= 4.0f; + float heat_w = sign * this->state_.thermal_power_w; + if (this->state_.defrost_active() && this->state_.thermal_power_w > 0.0f) + heat_w -= 4.0f * conductance_w_per_k; + const float a = conductance_w_per_k * dt_s / capacity_j_per_k; + const float alpha = -std::expm1(-a); + this->state_.water_out_temperature_c += + (this->state_.water_in_temperature_c - this->state_.water_out_temperature_c) * alpha + + heat_w * dt_s / capacity_j_per_k * (a > 0.0f ? alpha / a : 1.0f); } - this->state_.water_out_temperature_c += (target_out - this->state_.water_out_temperature_c) * - std::clamp(dt_s / this->settings_.water_response_tau_s, 0.0f, 1.0f); this->state_.condensing_temperature_c = this->state_.water_out_temperature_c + 5.0f + this->state_.measured_frequency_hz * 0.03f; this->state_.evaporating_temperature_c = diff --git a/tests/quatt_odu_water_capacity_test.cpp b/tests/quatt_odu_water_capacity_test.cpp new file mode 100644 index 0000000..99308c1 --- /dev/null +++ b/tests/quatt_odu_water_capacity_test.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include "../components/quatt_odu_simulator/quatt_odu_simulator_model.h" +using namespace esphome::quatt_odu_simulator; +constexpr float kCapacity = 4180.0f * 800.0f / 3600.0f * 4.0f; +QuattOduSimulatorModel steady(float flow, float frequency = 0.0f, WorkingMode mode = WorkingMode::HEATING) { + QuattOduSimulatorModel m; + m.configure(Profile::V1_5); + m.mutable_settings().flow_offset_lph = flow; + m.mutable_settings().flow_gain_lph_per_ipwm = 0.0f; + m.mutable_settings().freeze_measured_frequency = true; + m.mutable_state().force_flow_without_relay = true; + m.mutable_state().flow_lph = flow; + m.mutable_state().measured_frequency_hz = frequency; + m.mutable_state().active_mode = m.mutable_state().requested_mode = mode; + return m; +} +int main() { + // Independent analytic cooling solution; subdivision must not change it. + for (float step : {0.25f, 1.0f, 2.0f}) { + auto m = steady(800.0f); + m.mutable_state().water_out_temperature_c = 50.0f; + for (int i = 0; i < static_cast(20.0f / step); ++i) m.update(step); + assert(std::fabs(m.state().water_out_temperature_c - (30.0f + 20.0f * std::exp(-5.0f))) < 0.0001f); + } + // Closed water retains heat after the compressor stops. + auto trapped = steady(0.0f); + trapped.mutable_state().water_out_temperature_c = 50.0f; + trapped.update(20.0f); + assert(trapped.state().water_out_temperature_c == 50.0f); + // No discontinuity at the former 1 L/h cutoff; zero-flow input is energy bounded. + float previous = 0.0f; + for (float flow : {0.0f, 0.99f, 1.0f, 1.01f}) { + auto m = steady(flow, 50.0f); + m.update(0.25f); + const float rise = m.state().water_out_temperature_c - 30.0f; + assert(rise > 0.0f && rise <= m.state().thermal_power_w * 0.25f / kCapacity + 0.00001f); + if (flow > 0.0f) assert(std::fabs(rise - previous) < 0.001f); + previous = rise; + } + // Heating, cooling and defrost retain the stationary calorimetric balance. + for (int scenario = 0; scenario < 3; ++scenario) { + auto m = steady(800.0f, 50.0f, scenario == 1 ? WorkingMode::COOLING : WorkingMode::HEATING); + if (scenario == 2) m.set_defrost(true); + for (int i = 0; i < 800; ++i) m.update(0.25f); + const float conductance = 4180.0f * 800.0f / 3600.0f; + const float expected = (scenario == 1 ? -1.0f : 1.0f) * m.state().thermal_power_w - (scenario == 2 ? 4.0f * conductance : 0.0f); + assert(std::fabs(conductance * (m.state().water_out_temperature_c - 30.0f) - expected) < 0.1f); + } + // Pump-stop sequence cannot add more heat than the simulated compressor delivered. + QuattOduSimulatorModel m; + m.configure(Profile::V1_5); + m.mutable_settings().compressor_start_delay_s = m.mutable_settings().minimum_runtime_s = 0.0f; + m.write_register(1999, 8, 1); m.write_register(3999, 2, 2); + m.write_register(2010, 4096, 3); m.write_register(2015, 150, 4); + for (int i = 0; i < 120; ++i) m.update(0.25f); + const float start = m.state().water_out_temperature_c; + float energy_j = 0.0f; + m.write_register(2015, 1000, 5); + for (int i = 0; i < 80; ++i) { + m.update(0.25f); energy_j += m.state().thermal_power_w * 0.25f; + assert(std::isfinite(m.state().water_out_temperature_c)); + assert(m.state().water_out_temperature_c <= start + energy_j / kCapacity + 0.001f); + } + auto invalid = steady(0.0f, 50.0f); + invalid.mutable_settings().water_response_tau_s = 0.0f; + invalid.update(1.0f); + assert(invalid.state().water_out_temperature_c == 30.0f); + std::puts("Finite water capacity tests passed"); +} From 2ca1a6abb5294b47fb97063f1f59cb27b9f96890 Mon Sep 17 00:00:00 2001 From: Jeroen Jansen Date: Thu, 8 Oct 2026 13:11:25 +0200 Subject: [PATCH 2/2] Documenteer watermodel en dek numerieke rand af --- README.md | 11 +++++- .../quatt_odu_simulator_model.h | 8 ++-- tests/quatt_odu_water_capacity_test.cpp | 37 ++++++++++++++----- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 5338050..245f14a 100644 --- a/README.md +++ b/README.md @@ -142,8 +142,15 @@ the profile capability are recorded and capped before a frequency is chosen. Mode changes ramp the compressor to zero before restarting in the other mode. Pump writes control a settling flow model and the flowswitch. `2138` uses the -OpenQuatt conversion `round(flow_lph / 0.618)`. Water-out temperature follows -the simulated thermal power and mass flow through a first-order response. +OpenQuatt conversion `round(flow_lph / 0.618)`. Water-out temperature follows a +finite water heat capacity and the heat carried away by the flow. The existing +four-second response setting defines that capacity at 800 l/h; it is a simulator +parameter, not a measured ODU water volume. At zero flow, remaining compressor +power heats the trapped water at a finite rate instead of dividing by nearly +zero flow. After the compressor stops, trapped water retains its heat until flow +resumes; heat loss to the surroundings is not modelled. With steady flow, the +final temperature difference still corresponds to thermal power divided by +water mass flow and specific heat capacity. `ODU external system pump flow` applies the configured iPWM flow without requiring ODU pump-relay register `2010`; use it when the controller drives a separate system pump. Water-in can be set from 0.0 to 60.0 °C in 0.1 °C steps. diff --git a/components/quatt_odu_simulator/quatt_odu_simulator_model.h b/components/quatt_odu_simulator/quatt_odu_simulator_model.h index 35aea45..05ed809 100644 --- a/components/quatt_odu_simulator/quatt_odu_simulator_model.h +++ b/components/quatt_odu_simulator/quatt_odu_simulator_model.h @@ -843,15 +843,15 @@ class QuattOduSimulatorModel { // A finite water volume prevents P/flow from diverging during a pump stop. // Calibrate the existing response time at the default 800 L/h operating point; // this is a simulator calibration, not a measured ODU water volume. - const float capacity_j_per_k = 4180.0f * (800.0f / 3600.0f) * this->settings_.water_response_tau_s; + const double capacity_j_per_k = 4180.0 * (800.0 / 3600.0) * this->settings_.water_response_tau_s; if (std::isfinite(capacity_j_per_k) && capacity_j_per_k > 0.0f) { - const float conductance_w_per_k = 4180.0f * (this->state_.flow_lph / 3600.0f); + const double conductance_w_per_k = 4180.0 * (this->state_.flow_lph / 3600.0); const float sign = this->state_.active_mode == WorkingMode::COOLING ? -1.0f : 1.0f; float heat_w = sign * this->state_.thermal_power_w; if (this->state_.defrost_active() && this->state_.thermal_power_w > 0.0f) heat_w -= 4.0f * conductance_w_per_k; - const float a = conductance_w_per_k * dt_s / capacity_j_per_k; - const float alpha = -std::expm1(-a); + const double a = conductance_w_per_k * dt_s / capacity_j_per_k; + const double alpha = -std::expm1(-a); this->state_.water_out_temperature_c += (this->state_.water_in_temperature_c - this->state_.water_out_temperature_c) * alpha + heat_w * dt_s / capacity_j_per_k * (a > 0.0f ? alpha / a : 1.0f); diff --git a/tests/quatt_odu_water_capacity_test.cpp b/tests/quatt_odu_water_capacity_test.cpp index 99308c1..1b8430b 100644 --- a/tests/quatt_odu_water_capacity_test.cpp +++ b/tests/quatt_odu_water_capacity_test.cpp @@ -21,13 +21,15 @@ int main() { for (float step : {0.25f, 1.0f, 2.0f}) { auto m = steady(800.0f); m.mutable_state().water_out_temperature_c = 50.0f; - for (int i = 0; i < static_cast(20.0f / step); ++i) m.update(step); + for (int i = 0; i < static_cast(20.0f / step); ++i) + m.update(step); assert(std::fabs(m.state().water_out_temperature_c - (30.0f + 20.0f * std::exp(-5.0f))) < 0.0001f); } // Closed water retains heat after the compressor stops. auto trapped = steady(0.0f); trapped.mutable_state().water_out_temperature_c = 50.0f; - trapped.update(20.0f); + for (int i = 0; i < 20; ++i) + trapped.update(1.0f); assert(trapped.state().water_out_temperature_c == 50.0f); // No discontinuity at the former 1 L/h cutoff; zero-flow input is energy bounded. float previous = 0.0f; @@ -36,30 +38,38 @@ int main() { m.update(0.25f); const float rise = m.state().water_out_temperature_c - 30.0f; assert(rise > 0.0f && rise <= m.state().thermal_power_w * 0.25f / kCapacity + 0.00001f); - if (flow > 0.0f) assert(std::fabs(rise - previous) < 0.001f); + if (flow > 0.0f) + assert(std::fabs(rise - previous) < 0.001f); previous = rise; } // Heating, cooling and defrost retain the stationary calorimetric balance. for (int scenario = 0; scenario < 3; ++scenario) { auto m = steady(800.0f, 50.0f, scenario == 1 ? WorkingMode::COOLING : WorkingMode::HEATING); - if (scenario == 2) m.set_defrost(true); - for (int i = 0; i < 800; ++i) m.update(0.25f); + if (scenario == 2) + m.set_defrost(true); + for (int i = 0; i < 800; ++i) + m.update(0.25f); const float conductance = 4180.0f * 800.0f / 3600.0f; - const float expected = (scenario == 1 ? -1.0f : 1.0f) * m.state().thermal_power_w - (scenario == 2 ? 4.0f * conductance : 0.0f); + const float expected = + (scenario == 1 ? -1.0f : 1.0f) * m.state().thermal_power_w - (scenario == 2 ? 4.0f * conductance : 0.0f); assert(std::fabs(conductance * (m.state().water_out_temperature_c - 30.0f) - expected) < 0.1f); } // Pump-stop sequence cannot add more heat than the simulated compressor delivered. QuattOduSimulatorModel m; m.configure(Profile::V1_5); m.mutable_settings().compressor_start_delay_s = m.mutable_settings().minimum_runtime_s = 0.0f; - m.write_register(1999, 8, 1); m.write_register(3999, 2, 2); - m.write_register(2010, 4096, 3); m.write_register(2015, 150, 4); - for (int i = 0; i < 120; ++i) m.update(0.25f); + m.write_register(1999, 8, 1); + m.write_register(3999, 2, 2); + m.write_register(2010, 4096, 3); + m.write_register(2015, 150, 4); + for (int i = 0; i < 120; ++i) + m.update(0.25f); const float start = m.state().water_out_temperature_c; float energy_j = 0.0f; m.write_register(2015, 1000, 5); for (int i = 0; i < 80; ++i) { - m.update(0.25f); energy_j += m.state().thermal_power_w * 0.25f; + m.update(0.25f); + energy_j += m.state().thermal_power_w * 0.25f; assert(std::isfinite(m.state().water_out_temperature_c)); assert(m.state().water_out_temperature_c <= start + energy_j / kCapacity + 0.001f); } @@ -67,5 +77,12 @@ int main() { invalid.mutable_settings().water_response_tau_s = 0.0f; invalid.update(1.0f); assert(invalid.state().water_out_temperature_c == 30.0f); + // A tiny finite capacity reaches equilibrium without overflowing intermediates. + auto tiny = steady(800.0f, 50.0f); + tiny.mutable_settings().water_response_tau_s = 1e-40f; + tiny.update(0.25f); + const float equilibrium = 30.0f + tiny.state().thermal_power_w / (4180.0f * 800.0f / 3600.0f); + assert(std::isfinite(tiny.state().water_out_temperature_c)); + assert(std::fabs(tiny.state().water_out_temperature_c - equilibrium) < 0.0001f); std::puts("Finite water capacity tests passed"); }