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 c3d9357..05ed809 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 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 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; - 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 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); } - 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..1b8430b --- /dev/null +++ b/tests/quatt_odu_water_capacity_test.cpp @@ -0,0 +1,88 @@ +#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; + 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; + 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); + // 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"); +}